1e6e6ef9723ee1aadcc5337796cfdc774179517f
1 /* opncls.c -- open and close a BFD.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 extern void bfd_cache_init
PARAMS ((bfd
*));
26 FILE *bfd_open_file
PARAMS ((bfd
*));
28 /* fdopen is a loser -- we should use stdio exclusively. Unfortunately
29 if we do that we can't use fcntl. */
32 #define obstack_chunk_alloc bfd_xmalloc_by_size_t
33 #define obstack_chunk_free free
35 /* Return a new BFD. All BFD's are allocated through this routine. */
37 bfd
*new_bfd
PARAMS ((void))
41 nbfd
= (bfd
*)zalloc (sizeof (bfd
));
46 obstack_begin((PTR
)&nbfd
->memory
, 128);
48 nbfd
->arch_info
= &bfd_default_arch_struct
;
50 nbfd
->direction
= no_direction
;
51 nbfd
->iostream
= NULL
;
53 nbfd
->sections
= (asection
*)NULL
;
54 nbfd
->format
= bfd_unknown
;
55 nbfd
->my_archive
= (bfd
*)NULL
;
57 nbfd
->opened_once
= false;
58 nbfd
->output_has_begun
= false;
59 nbfd
->section_count
= 0;
60 nbfd
->usrdata
= (PTR
)NULL
;
61 nbfd
->sections
= (asection
*)NULL
;
62 nbfd
->cacheable
= false;
63 nbfd
->flags
= NO_FLAGS
;
64 nbfd
->mtime_set
= false;
70 /* Allocate a new BFD as a member of archive OBFD. */
72 bfd
*new_bfd_contained_in(obfd
)
75 bfd
*nbfd
= new_bfd();
76 nbfd
->xvec
= obfd
->xvec
;
77 nbfd
->my_archive
= obfd
;
78 nbfd
->direction
= read_direction
;
79 nbfd
->target_defaulted
= obfd
->target_defaulted
;
85 Opening and Closing BFDs
94 bfd *bfd_openr(CONST char *filename, CONST char*target);
97 This function opens the file supplied (using <<fopen>>) with the target
98 supplied, it returns a pointer to the created BFD.
100 If NULL is returned then an error has occured. Possible errors
101 are <<no_memory>>, <<invalid_target>> or <<system_call>> error.
105 DEFUN(bfd_openr
, (filename
, target
),
106 CONST
char *filename AND
110 bfd_target
*target_vec
;
114 bfd_error
= no_memory
;
118 target_vec
= bfd_find_target (target
, nbfd
);
119 if (target_vec
== NULL
) {
120 bfd_error
= invalid_target
;
124 nbfd
->filename
= filename
;
125 nbfd
->direction
= read_direction
;
127 if (bfd_open_file (nbfd
) == NULL
) {
128 bfd_error
= system_call_error
; /* File didn't exist, or some such */
136 /* Don't try to `optimize' this function:
138 o - We lock using stack space so that interrupting the locking
139 won't cause a storage leak.
140 o - We open the file stream last, since we don't want to have to
141 close it if anything goes wrong. Closing the stream means closing
142 the file descriptor too, even though we didn't open it.
149 bfd *bfd_fdopenr(CONST char *filename, CONST char *target, int fd);
152 bfd_fdopenr is to bfd_fopenr much like fdopen is to fopen.
153 It opens a BFD on a file already described by the @var{fd}
156 When the file is later bfd_closed, the file descriptor will be closed.
158 If the caller desires that this file descriptor be cached by BFD
159 (opened as needed, closed as needed to free descriptors for
160 other opens), with the supplied @var{fd} used as an initial
161 file descriptor (but subject to closure at any time), set
162 bfd->cacheable nonzero in the returned BFD. The default is to
163 assume no cacheing; the file descriptor will remain open until
164 bfd_close, and will not be affected by BFD operations on other
167 Possible errors are no_memory, invalid_target and system_call
172 DEFUN(bfd_fdopenr
,(filename
, target
, fd
),
173 CONST
char *filename AND
174 CONST
char *target AND
178 bfd_target
*target_vec
;
181 bfd_error
= system_call_error
;
184 fdflags
= O_RDWR
; /* Assume full access */
186 fdflags
= fcntl (fd
, F_GETFL
, NULL
);
188 if (fdflags
== -1) return NULL
;
193 bfd_error
= no_memory
;
197 target_vec
= bfd_find_target (target
, nbfd
);
198 if (target_vec
== NULL
) {
199 bfd_error
= invalid_target
;
202 #if defined(VMS) || defined(__GO32__)
203 nbfd
->iostream
= (char *)fopen(filename
, FOPEN_RB
);
205 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
206 switch (fdflags
& (O_ACCMODE
)) {
207 case O_RDONLY
: nbfd
->iostream
= (char *) fdopen (fd
, FOPEN_RB
); break;
208 case O_WRONLY
: nbfd
->iostream
= (char *) fdopen (fd
, FOPEN_RUB
); break;
209 case O_RDWR
: nbfd
->iostream
= (char *) fdopen (fd
, FOPEN_RUB
); break;
213 if (nbfd
->iostream
== NULL
) {
214 (void) obstack_free (&nbfd
->memory
, (PTR
)0);
218 /* OK, put everything where it belongs */
220 nbfd
->filename
= filename
;
222 /* As a special case we allow a FD open for read/write to
223 be written through, although doing so requires that we end
224 the previous clause with a preposition. */
225 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
226 switch (fdflags
& (O_ACCMODE
)) {
227 case O_RDONLY
: nbfd
->direction
= read_direction
; break;
228 case O_WRONLY
: nbfd
->direction
= write_direction
; break;
229 case O_RDWR
: nbfd
->direction
= both_direction
; break;
233 bfd_cache_init (nbfd
);
238 /** bfd_openw -- open for writing.
239 Returns a pointer to a freshly-allocated BFD on success, or NULL.
241 See comment by bfd_fdopenr before you try to modify this function. */
248 bfd *bfd_openw(CONST char *filename, CONST char *target);
251 Creates a BFD, associated with file @var{filename}, using the
252 file format @var{target}, and returns a pointer to it.
254 Possible errors are system_call_error, no_memory,
259 DEFUN(bfd_openw
,(filename
, target
),
260 CONST
char *filename AND
264 bfd_target
*target_vec
;
266 bfd_error
= system_call_error
;
268 /* nbfd has to point to head of malloc'ed block so that bfd_close may
269 reclaim it correctly. */
273 bfd_error
= no_memory
;
277 target_vec
= bfd_find_target (target
, nbfd
);
278 if (target_vec
== NULL
) return NULL
;
280 nbfd
->filename
= filename
;
281 nbfd
->direction
= write_direction
;
283 if (bfd_open_file (nbfd
) == NULL
) {
284 bfd_error
= system_call_error
; /* File not writeable, etc */
285 (void) obstack_free (&nbfd
->memory
, (PTR
)0);
297 boolean bfd_close(bfd *);
301 This function closes a BFD. If the BFD was open for writing,
302 then pending operations are completed and the file written out
303 and closed. If the created file is executable, then
304 <<chmod>> is called to mark it as such.
306 All memory attached to the BFD's obstacks is released.
308 The file descriptor associated with the BFD is closed (even
309 if it was passed in to BFD by bfd_fdopenr).
312 <<true>> is returned if all is ok, otherwise <<false>>.
317 DEFUN(bfd_close
,(abfd
),
322 if (!bfd_read_p(abfd
))
323 if (BFD_SEND_FMT (abfd
, _bfd_write_contents
, (abfd
)) != true)
326 if (BFD_SEND (abfd
, _close_and_cleanup
, (abfd
)) != true) return false;
328 ret
= bfd_cache_close(abfd
);
330 /* If the file was open for writing and is now executable,
333 && abfd
->direction
== write_direction
334 && abfd
->flags
& EXEC_P
) {
336 stat(abfd
->filename
, &buf
);
338 #define S_IXUSR 0100 /* Execute by owner. */
341 #define S_IXGRP 0010 /* Execute by group. */
344 #define S_IXOTH 0001 /* Execute by others. */
347 chmod(abfd
->filename
, 0777 & (buf
.st_mode
| S_IXUSR
| S_IXGRP
| S_IXOTH
));
349 (void) obstack_free (&abfd
->memory
, (PTR
)0);
359 boolean bfd_close_all_done(bfd *);
362 This function closes a BFD. It differs from <<bfd_close>>
363 since it does not complete any pending operations. This
364 routine would be used if the application had just used BFD for
365 swapping and didn't want to use any of the writing code.
367 If the created file is executable, then <<chmod>> is called
370 All memory attached to the BFD's obstacks is released.
373 <<true>> is returned if all is ok, otherwise <<false>>.
378 DEFUN(bfd_close_all_done
,(abfd
),
383 ret
= bfd_cache_close(abfd
);
385 /* If the file was open for writing and is now executable,
388 && abfd
->direction
== write_direction
389 && abfd
->flags
& EXEC_P
) {
391 stat(abfd
->filename
, &buf
);
393 #define S_IXUSR 0100 /* Execute by owner. */
396 #define S_IXGRP 0010 /* Execute by group. */
399 #define S_IXOTH 0001 /* Execute by others. */
402 chmod(abfd
->filename
, 0x777 &(buf
.st_mode
| S_IXUSR
| S_IXGRP
| S_IXOTH
));
404 (void) obstack_free (&abfd
->memory
, (PTR
)0);
415 bfd_size_type bfd_alloc_size(bfd *abfd);
418 Return the number of bytes in the obstacks connected to the
424 DEFUN(bfd_alloc_size
,(abfd
),
427 struct _obstack_chunk
*chunk
= abfd
->memory
.chunk
;
430 size
+= chunk
->limit
- &(chunk
->contents
[0]);
443 bfd *bfd_create(CONST char *filename, bfd *template);
446 This routine creates a new BFD in the manner of
447 <<bfd_openw>>, but without opening a file. The new BFD
448 takes the target from the target used by @var{template}. The
449 format is always set to <<bfd_object>>.
454 DEFUN(bfd_create
,(filename
, template),
455 CONST
char *filename AND
458 bfd
*nbfd
= new_bfd();
459 if (nbfd
== (bfd
*)NULL
) {
460 bfd_error
= no_memory
;
463 nbfd
->filename
= filename
;
465 nbfd
->xvec
= template->xvec
;
467 nbfd
->direction
= no_direction
;
468 bfd_set_format(nbfd
, bfd_object
);
477 PTR bfd_alloc_by_size_t(bfd *abfd, size_t wanted);
480 This function allocates a block of memory in the obstack
481 attatched to <<abfd>> and returns a pointer to it.
486 DEFUN(bfd_alloc_by_size_t
,(abfd
, size
),
490 PTR res
= obstack_alloc(&(abfd
->memory
), size
);
494 DEFUN(void bfd_alloc_grow
,(abfd
, ptr
, size
),
499 (void) obstack_grow(&(abfd
->memory
), ptr
, size
);
501 DEFUN(PTR bfd_alloc_finish
,(abfd
),
504 return obstack_finish(&(abfd
->memory
));
507 DEFUN(PTR bfd_alloc
, (abfd
, size
),
511 return bfd_alloc_by_size_t(abfd
, (size_t)size
);
514 DEFUN(PTR bfd_zalloc
,(abfd
, size
),
519 res
= bfd_alloc(abfd
, size
);
520 memset(res
, 0, (size_t)size
);
524 DEFUN(PTR bfd_realloc
,(abfd
, old
, size
),
529 PTR res
= bfd_alloc(abfd
, size
);
530 memcpy(res
, old
, (size_t)size
);
This page took 0.042678 seconds and 4 git commands to generate.