* gas/cris/rd-bcnst.d, gas/cris/rd-bcnst.d: New test.
[binutils.git] / bfd / opncls.c
blobaf8ff7e4a63585a1707e906b3a13a651b49c96a1
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.
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. */
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "objalloc.h"
27 #include "libbfd.h"
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
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. */
44 bfd *
45 _bfd_new_bfd ()
47 bfd *nbfd;
49 nbfd = (bfd *) bfd_zmalloc ((bfd_size_type) sizeof (bfd));
50 if (nbfd == NULL)
51 return NULL;
53 nbfd->memory = (PTR) objalloc_create ();
54 if (nbfd->memory == NULL)
56 bfd_set_error (bfd_error_no_memory);
57 free (nbfd);
58 return NULL;
61 nbfd->arch_info = &bfd_default_arch_struct;
63 nbfd->direction = no_direction;
64 nbfd->iostream = NULL;
65 nbfd->where = 0;
66 if (!bfd_hash_table_init_n (&nbfd->section_htab,
67 bfd_section_hash_newfunc,
68 251))
70 free (nbfd);
71 return NULL;
73 nbfd->sections = (asection *) NULL;
74 nbfd->section_tail = &nbfd->sections;
75 nbfd->format = bfd_unknown;
76 nbfd->my_archive = (bfd *) NULL;
77 nbfd->origin = 0;
78 nbfd->opened_once = false;
79 nbfd->output_has_begun = false;
80 nbfd->section_count = 0;
81 nbfd->usrdata = (PTR) NULL;
82 nbfd->cacheable = false;
83 nbfd->flags = BFD_NO_FLAGS;
84 nbfd->mtime_set = false;
86 return nbfd;
89 /* Allocate a new BFD as a member of archive OBFD. */
91 bfd *
92 _bfd_new_bfd_contained_in (obfd)
93 bfd *obfd;
95 bfd *nbfd;
97 nbfd = _bfd_new_bfd ();
98 if (nbfd == NULL)
99 return NULL;
100 nbfd->xvec = obfd->xvec;
101 nbfd->my_archive = obfd;
102 nbfd->direction = read_direction;
103 nbfd->target_defaulted = obfd->target_defaulted;
104 return nbfd;
107 /* Delete a BFD. */
109 void
110 _bfd_delete_bfd (abfd)
111 bfd *abfd;
113 bfd_hash_table_free (&abfd->section_htab);
114 objalloc_free ((struct objalloc *) abfd->memory);
115 free (abfd);
119 SECTION
120 Opening and closing BFDs
125 FUNCTION
126 bfd_openr
128 SYNOPSIS
129 bfd *bfd_openr(const char *filename, const char *target);
131 DESCRIPTION
132 Open the file @var{filename} (using <<fopen>>) with the target
133 @var{target}. Return a pointer to the created BFD.
135 Calls <<bfd_find_target>>, so @var{target} is interpreted as by
136 that function.
138 If <<NULL>> is returned then an error has occured. Possible errors
139 are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or
140 <<system_call>> error.
143 bfd *
144 bfd_openr (filename, target)
145 const char *filename;
146 const char *target;
148 bfd *nbfd;
149 const bfd_target *target_vec;
151 nbfd = _bfd_new_bfd ();
152 if (nbfd == NULL)
153 return NULL;
155 target_vec = bfd_find_target (target, nbfd);
156 if (target_vec == NULL)
158 _bfd_delete_bfd (nbfd);
159 return NULL;
162 nbfd->filename = filename;
163 nbfd->direction = read_direction;
165 if (bfd_open_file (nbfd) == NULL)
167 /* File didn't exist, or some such. */
168 bfd_set_error (bfd_error_system_call);
169 _bfd_delete_bfd (nbfd);
170 return NULL;
173 return nbfd;
176 /* Don't try to `optimize' this function:
178 o - We lock using stack space so that interrupting the locking
179 won't cause a storage leak.
180 o - We open the file stream last, since we don't want to have to
181 close it if anything goes wrong. Closing the stream means closing
182 the file descriptor too, even though we didn't open it. */
184 FUNCTION
185 bfd_fdopenr
187 SYNOPSIS
188 bfd *bfd_fdopenr(const char *filename, const char *target, int fd);
190 DESCRIPTION
191 <<bfd_fdopenr>> is to <<bfd_fopenr>> much like <<fdopen>> is to
192 <<fopen>>. It opens a BFD on a file already described by the
193 @var{fd} supplied.
195 When the file is later <<bfd_close>>d, the file descriptor will
196 be closed. If the caller desires that this file descriptor be
197 cached by BFD (opened as needed, closed as needed to free
198 descriptors for other opens), with the supplied @var{fd} used as
199 an initial file descriptor (but subject to closure at any time),
200 call bfd_set_cacheable(bfd, 1) on the returned BFD. The default
201 is to assume no cacheing; the file descriptor will remain open
202 until <<bfd_close>>, and will not be affected by BFD operations
203 on other files.
205 Possible errors are <<bfd_error_no_memory>>,
206 <<bfd_error_invalid_target>> and <<bfd_error_system_call>>.
209 bfd *
210 bfd_fdopenr (filename, target, fd)
211 const char *filename;
212 const char *target;
213 int fd;
215 bfd *nbfd;
216 const bfd_target *target_vec;
217 int fdflags;
219 bfd_set_error (bfd_error_system_call);
220 #if ! defined(HAVE_FCNTL) || ! defined(F_GETFL)
221 fdflags = O_RDWR; /* Assume full access. */
222 #else
223 fdflags = fcntl (fd, F_GETFL, NULL);
224 #endif
225 if (fdflags == -1)
226 return NULL;
228 nbfd = _bfd_new_bfd ();
229 if (nbfd == NULL)
230 return NULL;
232 target_vec = bfd_find_target (target, nbfd);
233 if (target_vec == NULL)
235 _bfd_delete_bfd (nbfd);
236 return NULL;
239 #ifndef HAVE_FDOPEN
240 nbfd->iostream = (PTR) fopen (filename, FOPEN_RB);
241 #else
242 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
243 switch (fdflags & (O_ACCMODE))
245 case O_RDONLY: nbfd->iostream = (PTR) fdopen (fd, FOPEN_RB); break;
246 case O_WRONLY: nbfd->iostream = (PTR) fdopen (fd, FOPEN_RUB); break;
247 case O_RDWR: nbfd->iostream = (PTR) fdopen (fd, FOPEN_RUB); break;
248 default: abort ();
250 #endif
252 if (nbfd->iostream == NULL)
254 _bfd_delete_bfd (nbfd);
255 return NULL;
258 /* OK, put everything where it belongs. */
259 nbfd->filename = filename;
261 /* As a special case we allow a FD open for read/write to
262 be written through, although doing so requires that we end
263 the previous clause with a preposition. */
264 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
265 switch (fdflags & (O_ACCMODE))
267 case O_RDONLY: nbfd->direction = read_direction; break;
268 case O_WRONLY: nbfd->direction = write_direction; break;
269 case O_RDWR: nbfd->direction = both_direction; break;
270 default: abort ();
273 if (! bfd_cache_init (nbfd))
275 _bfd_delete_bfd (nbfd);
276 return NULL;
278 nbfd->opened_once = true;
280 return nbfd;
284 FUNCTION
285 bfd_openstreamr
287 SYNOPSIS
288 bfd *bfd_openstreamr(const char *, const char *, PTR);
290 DESCRIPTION
292 Open a BFD for read access on an existing stdio stream. When
293 the BFD is passed to <<bfd_close>>, the stream will be closed.
296 bfd *
297 bfd_openstreamr (filename, target, streamarg)
298 const char *filename;
299 const char *target;
300 PTR streamarg;
302 FILE *stream = (FILE *) streamarg;
303 bfd *nbfd;
304 const bfd_target *target_vec;
306 nbfd = _bfd_new_bfd ();
307 if (nbfd == NULL)
308 return NULL;
310 target_vec = bfd_find_target (target, nbfd);
311 if (target_vec == NULL)
313 _bfd_delete_bfd (nbfd);
314 return NULL;
317 nbfd->iostream = (PTR) stream;
318 nbfd->filename = filename;
319 nbfd->direction = read_direction;
321 if (! bfd_cache_init (nbfd))
323 _bfd_delete_bfd (nbfd);
324 return NULL;
327 return nbfd;
330 /* bfd_openw -- open for writing.
331 Returns a pointer to a freshly-allocated BFD on success, or NULL.
333 See comment by bfd_fdopenr before you try to modify this function. */
336 FUNCTION
337 bfd_openw
339 SYNOPSIS
340 bfd *bfd_openw(const char *filename, const char *target);
342 DESCRIPTION
343 Create a BFD, associated with file @var{filename}, using the
344 file format @var{target}, and return a pointer to it.
346 Possible errors are <<bfd_error_system_call>>, <<bfd_error_no_memory>>,
347 <<bfd_error_invalid_target>>.
350 bfd *
351 bfd_openw (filename, target)
352 const char *filename;
353 const char *target;
355 bfd *nbfd;
356 const bfd_target *target_vec;
358 /* nbfd has to point to head of malloc'ed block so that bfd_close may
359 reclaim it correctly. */
360 nbfd = _bfd_new_bfd ();
361 if (nbfd == NULL)
362 return NULL;
364 target_vec = bfd_find_target (target, nbfd);
365 if (target_vec == NULL)
367 _bfd_delete_bfd (nbfd);
368 return NULL;
371 nbfd->filename = filename;
372 nbfd->direction = write_direction;
374 if (bfd_open_file (nbfd) == NULL)
376 /* File not writeable, etc. */
377 bfd_set_error (bfd_error_system_call);
378 _bfd_delete_bfd (nbfd);
379 return NULL;
382 return nbfd;
387 FUNCTION
388 bfd_close
390 SYNOPSIS
391 boolean bfd_close(bfd *abfd);
393 DESCRIPTION
395 Close a BFD. If the BFD was open for writing, then pending
396 operations are completed and the file written out and closed.
397 If the created file is executable, then <<chmod>> is called
398 to mark it as such.
400 All memory attached to the BFD is released.
402 The file descriptor associated with the BFD is closed (even
403 if it was passed in to BFD by <<bfd_fdopenr>>).
405 RETURNS
406 <<true>> is returned if all is ok, otherwise <<false>>.
410 boolean
411 bfd_close (abfd)
412 bfd *abfd;
414 boolean ret;
416 if (bfd_write_p (abfd))
418 if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)))
419 return false;
422 if (! BFD_SEND (abfd, _close_and_cleanup, (abfd)))
423 return false;
425 ret = bfd_cache_close (abfd);
427 /* If the file was open for writing and is now executable,
428 make it so. */
429 if (ret
430 && abfd->direction == write_direction
431 && abfd->flags & EXEC_P)
433 struct stat buf;
435 if (stat (abfd->filename, &buf) == 0)
437 unsigned int mask = umask (0);
439 umask (mask);
440 chmod (abfd->filename,
441 (0777
442 & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
446 _bfd_delete_bfd (abfd);
448 return ret;
452 FUNCTION
453 bfd_close_all_done
455 SYNOPSIS
456 boolean bfd_close_all_done(bfd *);
458 DESCRIPTION
459 Close a BFD. Differs from <<bfd_close>> since it does not
460 complete any pending operations. This routine would be used
461 if the application had just used BFD for swapping and didn't
462 want to use any of the writing code.
464 If the created file is executable, then <<chmod>> is called
465 to mark it as such.
467 All memory attached to the BFD is released.
469 RETURNS
470 <<true>> is returned if all is ok, otherwise <<false>>.
473 boolean
474 bfd_close_all_done (abfd)
475 bfd *abfd;
477 boolean ret;
479 ret = bfd_cache_close (abfd);
481 /* If the file was open for writing and is now executable,
482 make it so. */
483 if (ret
484 && abfd->direction == write_direction
485 && abfd->flags & EXEC_P)
487 struct stat buf;
489 if (stat (abfd->filename, &buf) == 0)
491 unsigned int mask = umask (0);
493 umask (mask);
494 chmod (abfd->filename,
495 (0777
496 & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
500 _bfd_delete_bfd (abfd);
502 return ret;
506 FUNCTION
507 bfd_create
509 SYNOPSIS
510 bfd *bfd_create(const char *filename, bfd *templ);
512 DESCRIPTION
513 Create a new BFD in the manner of <<bfd_openw>>, but without
514 opening a file. The new BFD takes the target from the target
515 used by @var{template}. The format is always set to <<bfd_object>>.
518 bfd *
519 bfd_create (filename, templ)
520 const char *filename;
521 bfd *templ;
523 bfd *nbfd;
525 nbfd = _bfd_new_bfd ();
526 if (nbfd == NULL)
527 return NULL;
528 nbfd->filename = filename;
529 if (templ)
530 nbfd->xvec = templ->xvec;
531 nbfd->direction = no_direction;
532 bfd_set_format (nbfd, bfd_object);
534 return nbfd;
538 FUNCTION
539 bfd_make_writable
541 SYNOPSIS
542 boolean bfd_make_writable(bfd *abfd);
544 DESCRIPTION
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.
550 RETURNS
551 <<true>> is returned if all is ok, otherwise <<false>>.
554 boolean
555 bfd_make_writable(abfd)
556 bfd *abfd;
558 struct bfd_in_memory *bim;
560 if (abfd->direction != no_direction)
562 bfd_set_error (bfd_error_invalid_operation);
563 return false;
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. */
570 bim->size = 0;
571 bim->buffer = 0;
573 abfd->flags |= BFD_IN_MEMORY;
574 abfd->direction = write_direction;
575 abfd->where = 0;
577 return true;
581 FUNCTION
582 bfd_make_readable
584 SYNOPSIS
585 boolean bfd_make_readable(bfd *abfd);
587 DESCRIPTION
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
592 direction.
594 RETURNS
595 <<true>> is returned if all is ok, otherwise <<false>>. */
597 boolean
598 bfd_make_readable(abfd)
599 bfd *abfd;
601 if (abfd->direction != write_direction || !(abfd->flags & BFD_IN_MEMORY))
603 bfd_set_error (bfd_error_invalid_operation);
604 return false;
607 if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)))
608 return false;
610 if (! BFD_SEND (abfd, _close_and_cleanup, (abfd)))
611 return false;
614 abfd->arch_info = &bfd_default_arch_struct;
616 abfd->where = 0;
617 abfd->format = bfd_unknown;
618 abfd->my_archive = (bfd *) NULL;
619 abfd->origin = 0;
620 abfd->opened_once = false;
621 abfd->output_has_begun = false;
622 abfd->section_count = 0;
623 abfd->usrdata = (PTR) NULL;
624 abfd->cacheable = false;
625 abfd->flags = BFD_IN_MEMORY;
626 abfd->mtime_set = false;
628 abfd->target_defaulted = true;
629 abfd->direction = read_direction;
630 abfd->sections = 0;
631 abfd->symcount = 0;
632 abfd->outsymbols = 0;
633 abfd->tdata.any = 0;
635 bfd_section_list_clear (abfd);
636 bfd_check_format (abfd, bfd_object);
638 return true;
642 INTERNAL_FUNCTION
643 bfd_alloc
645 SYNOPSIS
646 PTR bfd_alloc (bfd *abfd, size_t wanted);
648 DESCRIPTION
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)
656 bfd *abfd;
657 bfd_size_type size;
659 PTR ret;
661 if (size != (unsigned long) size)
663 bfd_set_error (bfd_error_no_memory);
664 return NULL;
667 ret = objalloc_alloc (abfd->memory, (unsigned long) size);
668 if (ret == NULL)
669 bfd_set_error (bfd_error_no_memory);
670 return ret;
674 bfd_zalloc (abfd, size)
675 bfd *abfd;
676 bfd_size_type size;
678 PTR res;
680 res = bfd_alloc (abfd, size);
681 if (res)
682 memset (res, 0, (size_t) size);
683 return res;
686 /* Free a block allocated for a BFD.
687 Note: Also frees all more recently allocated blocks! */
689 void
690 bfd_release (abfd, block)
691 bfd *abfd;
692 PTR block;
694 objalloc_free_block ((struct objalloc *) abfd->memory, block);