s/boolean/bfd_boolean/ s/true/TRUE/ s/false/FALSE/. Simplify
[deliverable/binutils-gdb.git] / bfd / opncls.c
CommitLineData
252b5132 1/* opncls.c -- open and close a BFD.
7898deda 2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
c4f3d130 3 2001, 2002
252b5132
RH
4 Free Software Foundation, Inc.
5
6 Written by Cygnus Support.
7
c4f3d130 8 This file is part of BFD, the Binary File Descriptor library.
252b5132 9
c4f3d130
NC
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.
252b5132 14
c4f3d130
NC
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.
252b5132 19
c4f3d130
NC
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. */
252b5132
RH
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
252b5132
RH
42/* Return a new BFD. All BFD's are allocated through this routine. */
43
44bfd *
45_bfd_new_bfd ()
46{
47 bfd *nbfd;
48
dc810e39 49 nbfd = (bfd *) bfd_zmalloc ((bfd_size_type) sizeof (bfd));
252b5132
RH
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);
73e87d70 57 free (nbfd);
252b5132
RH
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;
7c4a37eb
AM
66 if (!bfd_hash_table_init_n (&nbfd->section_htab,
67 bfd_section_hash_newfunc,
68 251))
73e87d70
AM
69 {
70 free (nbfd);
71 return NULL;
72 }
252b5132 73 nbfd->sections = (asection *) NULL;
73e87d70 74 nbfd->section_tail = &nbfd->sections;
252b5132
RH
75 nbfd->format = bfd_unknown;
76 nbfd->my_archive = (bfd *) NULL;
dc810e39 77 nbfd->origin = 0;
b34976b6
AM
78 nbfd->opened_once = FALSE;
79 nbfd->output_has_begun = FALSE;
252b5132
RH
80 nbfd->section_count = 0;
81 nbfd->usrdata = (PTR) NULL;
b34976b6 82 nbfd->cacheable = FALSE;
252b5132 83 nbfd->flags = BFD_NO_FLAGS;
b34976b6 84 nbfd->mtime_set = FALSE;
252b5132
RH
85
86 return nbfd;
87}
88
89/* Allocate a new BFD as a member of archive OBFD. */
90
91bfd *
92_bfd_new_bfd_contained_in (obfd)
93 bfd *obfd;
94{
95 bfd *nbfd;
96
97 nbfd = _bfd_new_bfd ();
301e3139
AM
98 if (nbfd == NULL)
99 return NULL;
252b5132
RH
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;
105}
106
73e87d70
AM
107/* Delete a BFD. */
108
109void
110_bfd_delete_bfd (abfd)
111 bfd *abfd;
112{
113 bfd_hash_table_free (&abfd->section_htab);
114 objalloc_free ((struct objalloc *) abfd->memory);
115 free (abfd);
116}
117
252b5132
RH
118/*
119SECTION
120 Opening and closing BFDs
121
122*/
123
124/*
125FUNCTION
126 bfd_openr
127
128SYNOPSIS
7c4a37eb 129 bfd *bfd_openr(const char *filename, const char *target);
252b5132
RH
130
131DESCRIPTION
132 Open the file @var{filename} (using <<fopen>>) with the target
133 @var{target}. Return a pointer to the created BFD.
134
135 Calls <<bfd_find_target>>, so @var{target} is interpreted as by
136 that function.
137
138 If <<NULL>> is returned then an error has occured. Possible errors
7c4a37eb
AM
139 are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or
140 <<system_call>> error.
252b5132
RH
141*/
142
143bfd *
144bfd_openr (filename, target)
dc810e39
AM
145 const char *filename;
146 const char *target;
252b5132
RH
147{
148 bfd *nbfd;
149 const bfd_target *target_vec;
150
151 nbfd = _bfd_new_bfd ();
152 if (nbfd == NULL)
153 return NULL;
154
155 target_vec = bfd_find_target (target, nbfd);
156 if (target_vec == NULL)
157 {
73e87d70 158 _bfd_delete_bfd (nbfd);
252b5132
RH
159 return NULL;
160 }
161
162 nbfd->filename = filename;
163 nbfd->direction = read_direction;
164
165 if (bfd_open_file (nbfd) == NULL)
166 {
c4f3d130 167 /* File didn't exist, or some such. */
252b5132 168 bfd_set_error (bfd_error_system_call);
73e87d70 169 _bfd_delete_bfd (nbfd);
252b5132
RH
170 return NULL;
171 }
172
173 return nbfd;
174}
175
176/* Don't try to `optimize' this function:
177
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
c4f3d130 182 the file descriptor too, even though we didn't open it. */
252b5132
RH
183/*
184FUNCTION
7c4a37eb 185 bfd_fdopenr
252b5132
RH
186
187SYNOPSIS
7c4a37eb 188 bfd *bfd_fdopenr(const char *filename, const char *target, int fd);
252b5132
RH
189
190DESCRIPTION
7c4a37eb
AM
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.
194
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.
204
205 Possible errors are <<bfd_error_no_memory>>,
206 <<bfd_error_invalid_target>> and <<bfd_error_system_call>>.
252b5132
RH
207*/
208
209bfd *
210bfd_fdopenr (filename, target, fd)
dc810e39
AM
211 const char *filename;
212 const char *target;
252b5132
RH
213 int fd;
214{
215 bfd *nbfd;
216 const bfd_target *target_vec;
217 int fdflags;
218
219 bfd_set_error (bfd_error_system_call);
220#if ! defined(HAVE_FCNTL) || ! defined(F_GETFL)
c4f3d130 221 fdflags = O_RDWR; /* Assume full access. */
252b5132
RH
222#else
223 fdflags = fcntl (fd, F_GETFL, NULL);
224#endif
767e34d1
AM
225 if (fdflags == -1)
226 return NULL;
252b5132
RH
227
228 nbfd = _bfd_new_bfd ();
229 if (nbfd == NULL)
230 return NULL;
231
232 target_vec = bfd_find_target (target, nbfd);
233 if (target_vec == NULL)
234 {
73e87d70 235 _bfd_delete_bfd (nbfd);
252b5132
RH
236 return NULL;
237 }
238
239#ifndef HAVE_FDOPEN
240 nbfd->iostream = (PTR) fopen (filename, FOPEN_RB);
241#else
c4f3d130 242 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
252b5132
RH
243 switch (fdflags & (O_ACCMODE))
244 {
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 ();
249 }
250#endif
251
252 if (nbfd->iostream == NULL)
253 {
73e87d70 254 _bfd_delete_bfd (nbfd);
252b5132
RH
255 return NULL;
256 }
257
c4f3d130 258 /* OK, put everything where it belongs. */
252b5132
RH
259 nbfd->filename = filename;
260
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. */
c4f3d130 264 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
d768008d 265 switch (fdflags & (O_ACCMODE))
252b5132
RH
266 {
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 ();
271 }
272
273 if (! bfd_cache_init (nbfd))
274 {
73e87d70 275 _bfd_delete_bfd (nbfd);
252b5132
RH
276 return NULL;
277 }
b34976b6 278 nbfd->opened_once = TRUE;
252b5132
RH
279
280 return nbfd;
281}
282
283/*
284FUNCTION
285 bfd_openstreamr
286
287SYNOPSIS
288 bfd *bfd_openstreamr(const char *, const char *, PTR);
289
290DESCRIPTION
291
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.
294*/
295
296bfd *
297bfd_openstreamr (filename, target, streamarg)
298 const char *filename;
299 const char *target;
300 PTR streamarg;
301{
302 FILE *stream = (FILE *) streamarg;
303 bfd *nbfd;
304 const bfd_target *target_vec;
305
306 nbfd = _bfd_new_bfd ();
307 if (nbfd == NULL)
308 return NULL;
309
310 target_vec = bfd_find_target (target, nbfd);
311 if (target_vec == NULL)
312 {
73e87d70 313 _bfd_delete_bfd (nbfd);
252b5132
RH
314 return NULL;
315 }
316
317 nbfd->iostream = (PTR) stream;
318 nbfd->filename = filename;
319 nbfd->direction = read_direction;
dc810e39 320
252b5132
RH
321 if (! bfd_cache_init (nbfd))
322 {
73e87d70 323 _bfd_delete_bfd (nbfd);
252b5132
RH
324 return NULL;
325 }
326
327 return nbfd;
328}
329\f
c4f3d130
NC
330/* bfd_openw -- open for writing.
331 Returns a pointer to a freshly-allocated BFD on success, or NULL.
252b5132 332
c4f3d130 333 See comment by bfd_fdopenr before you try to modify this function. */
252b5132
RH
334
335/*
336FUNCTION
337 bfd_openw
338
339SYNOPSIS
dc810e39 340 bfd *bfd_openw(const char *filename, const char *target);
252b5132
RH
341
342DESCRIPTION
343 Create a BFD, associated with file @var{filename}, using the
344 file format @var{target}, and return a pointer to it.
345
346 Possible errors are <<bfd_error_system_call>>, <<bfd_error_no_memory>>,
347 <<bfd_error_invalid_target>>.
348*/
349
350bfd *
351bfd_openw (filename, target)
dc810e39
AM
352 const char *filename;
353 const char *target;
252b5132
RH
354{
355 bfd *nbfd;
356 const bfd_target *target_vec;
357
252b5132 358 /* nbfd has to point to head of malloc'ed block so that bfd_close may
c4f3d130 359 reclaim it correctly. */
252b5132
RH
360 nbfd = _bfd_new_bfd ();
361 if (nbfd == NULL)
362 return NULL;
363
364 target_vec = bfd_find_target (target, nbfd);
365 if (target_vec == NULL)
366 {
73e87d70 367 _bfd_delete_bfd (nbfd);
252b5132
RH
368 return NULL;
369 }
370
371 nbfd->filename = filename;
372 nbfd->direction = write_direction;
373
374 if (bfd_open_file (nbfd) == NULL)
375 {
c4f3d130
NC
376 /* File not writeable, etc. */
377 bfd_set_error (bfd_error_system_call);
73e87d70 378 _bfd_delete_bfd (nbfd);
252b5132
RH
379 return NULL;
380 }
381
382 return nbfd;
383}
384
385/*
386
387FUNCTION
388 bfd_close
389
390SYNOPSIS
b34976b6 391 bfd_boolean bfd_close (bfd *abfd);
252b5132
RH
392
393DESCRIPTION
394
7c4a37eb
AM
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.
252b5132
RH
399
400 All memory attached to the BFD is released.
401
402 The file descriptor associated with the BFD is closed (even
403 if it was passed in to BFD by <<bfd_fdopenr>>).
404
405RETURNS
b34976b6 406 <<TRUE>> is returned if all is ok, otherwise <<FALSE>>.
252b5132
RH
407*/
408
409
b34976b6 410bfd_boolean
252b5132
RH
411bfd_close (abfd)
412 bfd *abfd;
413{
b34976b6 414 bfd_boolean ret;
252b5132 415
c4f3d130 416 if (bfd_write_p (abfd))
252b5132
RH
417 {
418 if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)))
b34976b6 419 return FALSE;
252b5132
RH
420 }
421
422 if (! BFD_SEND (abfd, _close_and_cleanup, (abfd)))
b34976b6 423 return FALSE;
252b5132
RH
424
425 ret = bfd_cache_close (abfd);
426
427 /* If the file was open for writing and is now executable,
c4f3d130 428 make it so. */
252b5132
RH
429 if (ret
430 && abfd->direction == write_direction
431 && abfd->flags & EXEC_P)
432 {
433 struct stat buf;
434
435 if (stat (abfd->filename, &buf) == 0)
436 {
7c4a37eb 437 unsigned int mask = umask (0);
c4f3d130 438
252b5132
RH
439 umask (mask);
440 chmod (abfd->filename,
441 (0777
442 & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
443 }
444 }
445
73e87d70 446 _bfd_delete_bfd (abfd);
252b5132
RH
447
448 return ret;
449}
450
451/*
452FUNCTION
453 bfd_close_all_done
454
455SYNOPSIS
b34976b6 456 bfd_boolean bfd_close_all_done (bfd *);
252b5132
RH
457
458DESCRIPTION
7c4a37eb
AM
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.
252b5132
RH
463
464 If the created file is executable, then <<chmod>> is called
465 to mark it as such.
466
467 All memory attached to the BFD is released.
468
469RETURNS
b34976b6 470 <<TRUE>> is returned if all is ok, otherwise <<FALSE>>.
252b5132
RH
471*/
472
b34976b6 473bfd_boolean
252b5132
RH
474bfd_close_all_done (abfd)
475 bfd *abfd;
476{
b34976b6 477 bfd_boolean ret;
252b5132
RH
478
479 ret = bfd_cache_close (abfd);
480
481 /* If the file was open for writing and is now executable,
c4f3d130 482 make it so. */
252b5132
RH
483 if (ret
484 && abfd->direction == write_direction
485 && abfd->flags & EXEC_P)
486 {
487 struct stat buf;
488
489 if (stat (abfd->filename, &buf) == 0)
490 {
dc810e39 491 unsigned int mask = umask (0);
c4f3d130 492
252b5132
RH
493 umask (mask);
494 chmod (abfd->filename,
b6cdd0fd 495 (0777
252b5132
RH
496 & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
497 }
498 }
499
73e87d70 500 _bfd_delete_bfd (abfd);
252b5132
RH
501
502 return ret;
503}
504
505/*
506FUNCTION
507 bfd_create
508
509SYNOPSIS
dc810e39 510 bfd *bfd_create(const char *filename, bfd *templ);
252b5132
RH
511
512DESCRIPTION
7c4a37eb
AM
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>>.
252b5132
RH
516*/
517
518bfd *
519bfd_create (filename, templ)
dc810e39 520 const char *filename;
252b5132
RH
521 bfd *templ;
522{
523 bfd *nbfd;
524
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);
c4f3d130 533
252b5132
RH
534 return nbfd;
535}
536
537/*
538FUNCTION
539 bfd_make_writable
540
541SYNOPSIS
b34976b6 542 bfd_boolean bfd_make_writable (bfd *abfd);
252b5132
RH
543
544DESCRIPTION
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.
549
550RETURNS
b34976b6 551 <<TRUE>> is returned if all is ok, otherwise <<FALSE>>.
252b5132
RH
552*/
553
b34976b6 554bfd_boolean
252b5132
RH
555bfd_make_writable(abfd)
556 bfd *abfd;
557{
558 struct bfd_in_memory *bim;
559
560 if (abfd->direction != no_direction)
561 {
562 bfd_set_error (bfd_error_invalid_operation);
b34976b6 563 return FALSE;
252b5132
RH
564 }
565
dc810e39
AM
566 bim = ((struct bfd_in_memory *)
567 bfd_malloc ((bfd_size_type) sizeof (struct bfd_in_memory)));
252b5132 568 abfd->iostream = (PTR) bim;
c4f3d130 569 /* bfd_bwrite will grow these as needed. */
252b5132
RH
570 bim->size = 0;
571 bim->buffer = 0;
572
573 abfd->flags |= BFD_IN_MEMORY;
574 abfd->direction = write_direction;
575 abfd->where = 0;
576
b34976b6 577 return TRUE;
252b5132
RH
578}
579
580/*
581FUNCTION
582 bfd_make_readable
583
584SYNOPSIS
b34976b6 585 bfd_boolean bfd_make_readable (bfd *abfd);
252b5132
RH
586
587DESCRIPTION
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.
593
594RETURNS
b34976b6 595 <<TRUE>> is returned if all is ok, otherwise <<FALSE>>. */
252b5132 596
b34976b6 597bfd_boolean
252b5132
RH
598bfd_make_readable(abfd)
599 bfd *abfd;
600{
601 if (abfd->direction != write_direction || !(abfd->flags & BFD_IN_MEMORY))
602 {
603 bfd_set_error (bfd_error_invalid_operation);
b34976b6 604 return FALSE;
252b5132
RH
605 }
606
607 if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)))
b34976b6 608 return FALSE;
252b5132
RH
609
610 if (! BFD_SEND (abfd, _close_and_cleanup, (abfd)))
b34976b6 611 return FALSE;
252b5132
RH
612
613
614 abfd->arch_info = &bfd_default_arch_struct;
615
616 abfd->where = 0;
252b5132
RH
617 abfd->format = bfd_unknown;
618 abfd->my_archive = (bfd *) NULL;
dc810e39 619 abfd->origin = 0;
b34976b6
AM
620 abfd->opened_once = FALSE;
621 abfd->output_has_begun = FALSE;
252b5132
RH
622 abfd->section_count = 0;
623 abfd->usrdata = (PTR) NULL;
b34976b6 624 abfd->cacheable = FALSE;
252b5132 625 abfd->flags = BFD_IN_MEMORY;
b34976b6 626 abfd->mtime_set = FALSE;
252b5132 627
b34976b6 628 abfd->target_defaulted = TRUE;
252b5132
RH
629 abfd->direction = read_direction;
630 abfd->sections = 0;
631 abfd->symcount = 0;
632 abfd->outsymbols = 0;
633 abfd->tdata.any = 0;
634
e54fdaa5
AM
635 bfd_section_list_clear (abfd);
636 bfd_check_format (abfd, bfd_object);
252b5132 637
b34976b6 638 return TRUE;
252b5132
RH
639}
640
641/*
642INTERNAL_FUNCTION
643 bfd_alloc
644
645SYNOPSIS
646 PTR bfd_alloc (bfd *abfd, size_t wanted);
647
648DESCRIPTION
649 Allocate a block of @var{wanted} bytes of memory attached to
650 <<abfd>> and return a pointer to it.
651*/
652
653
654PTR
655bfd_alloc (abfd, size)
656 bfd *abfd;
dc810e39 657 bfd_size_type size;
252b5132
RH
658{
659 PTR ret;
660
dc810e39
AM
661 if (size != (unsigned long) size)
662 {
663 bfd_set_error (bfd_error_no_memory);
664 return NULL;
665 }
666
252b5132
RH
667 ret = objalloc_alloc (abfd->memory, (unsigned long) size);
668 if (ret == NULL)
669 bfd_set_error (bfd_error_no_memory);
670 return ret;
671}
672
673PTR
674bfd_zalloc (abfd, size)
675 bfd *abfd;
dc810e39 676 bfd_size_type size;
252b5132
RH
677{
678 PTR res;
679
680 res = bfd_alloc (abfd, size);
681 if (res)
dc810e39 682 memset (res, 0, (size_t) size);
252b5132
RH
683 return res;
684}
685
73e87d70
AM
686/* Free a block allocated for a BFD.
687 Note: Also frees all more recently allocated blocks! */
252b5132
RH
688
689void
690bfd_release (abfd, block)
691 bfd *abfd;
692 PTR block;
693{
694 objalloc_free_block ((struct objalloc *) abfd->memory, block);
695}
This page took 0.203609 seconds and 4 git commands to generate.