1 /* opncls.c -- open and close a bfd. */
3 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Diddler.
7 BFD 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 1, or (at your option)
12 BFD 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 BFD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 extern void bfd_cache_init();
28 FILE *bfd_open_file();
30 /* fdopen is a loser -- we should use stdio exclusively. Unfortunately
31 if we do that we can't use fcntl. */
35 Locking is loosely controlled by the preprocessor variable
36 BFD_LOCKS. I say loosely because Unix barely understands locking
37 -- at least in BSD it doesn't affect programs which don't
38 explicitly use it! That is to say it's practically useless, though
39 if everyone uses this library you'll be OK.
41 From among the many and varied lock facilities available, (none of
42 which, of course, knows about any other) we use the fcntl locks,
43 because they're Posix.
45 The reason that bfd_openr and bfd_fdopenr exist, yet only bfd_openw
46 exists is because of locking. When we do output, we lock the
47 filename file for output, then open a temporary file which does not
48 actually get its correct filename until closing time. This is
49 safest, but requires the asymmetry in read and write entry points.
51 Perhaps, since unix has so many different kinds of locking anyway,
52 we should use the emacs lock scheme?... */
54 #define obstack_chunk_alloc malloc
55 #define obstack_chunk_free free
57 /* Return a new BFD. All BFD's are allocated through this routine. */
63 nbfd = (bfd *)zalloc (sizeof (bfd));
67 obstack_begin((PTR)&nbfd->memory, 128);
69 nbfd->direction = no_direction;
70 nbfd->iostream = NULL;
72 nbfd->sections = (asection *)NULL;
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->sections = (asection *)NULL;
81 nbfd->cacheable = false;
82 nbfd->flags = NO_FLAGS;
87 /* Allocate a new BFD as a member of archive OBFD. */
89 bfd *new_bfd_contained_in(obfd)
92 bfd *nbfd = new_bfd();
93 nbfd->xvec = obfd->xvec;
94 nbfd->my_archive = obfd;
95 nbfd->direction = read_direction;
99 /** bfd_openr, bfd_fdopenr -- open for reading.
100 Returns a pointer to a freshly-allocated bfd on success, or NULL. */
103 DEFUN(bfd_openr, (filename, target),
104 CONST char *filename AND
108 bfd_target *target_vec;
112 bfd_error = no_memory;
116 target_vec = bfd_find_target (target, nbfd);
117 if (target_vec == NULL) {
118 bfd_error = invalid_target;
122 nbfd->filename = filename;
123 nbfd->direction = read_direction;
125 if (bfd_open_file (nbfd) == NULL) {
126 bfd_error = system_call_error; /* File didn't exist, or some such */
134 /* Don't try to `optimize' this function:
136 o - We lock using stack space so that interrupting the locking
137 won't cause a storage leak.
138 o - We open the file stream last, since we don't want to have to
139 close it if anything goes wrong. Closing the stream means closing
140 the file descriptor too, even though we didn't open it.
144 DEFUN(bfd_fdopenr,(filename, target, fd),
145 CONST char *filename AND
146 CONST char *target AND
150 bfd_target *target_vec;
153 struct flock lock, *lockp = &lock;
156 bfd_error = system_call_error;
158 fdflags = fcntl (fd, F_GETFL);
159 if (fdflags == -1) return NULL;
162 lockp->l_type = F_RDLCK;
163 if (fcntl (fd, F_SETLKW, lockp) == -1) return NULL;
169 bfd_error = no_memory;
173 target_vec = bfd_find_target (target, nbfd);
174 if (target_vec == NULL) {
175 bfd_error = invalid_target;
180 nbfd->lock = (struct flock *) (nbfd + 1);
182 /* if the fd were open for read only, this still would not hurt: */
183 nbfd->iostream = (char *) fdopen (fd, "r+");
184 if (nbfd->iostream == NULL) {
185 (void) obstack_free (&nbfd->memory, (PTR)0);
189 /* OK, put everything where it belongs */
191 nbfd->filename = filename;
193 /* As a special case we allow a FD open for read/write to
194 be written through, although doing so requires that we end
195 the previous clause with a preposition. */
196 switch (fdflags & O_ACCMODE) {
197 case O_RDONLY: nbfd->direction = read_direction; break;
198 case O_WRONLY: nbfd->direction = write_direction; break;
199 case O_RDWR: nbfd->direction = both_direction; break;
204 memcpy (nbfd->lock, lockp, sizeof (struct flock))
207 bfd_cache_init (nbfd);
212 /** bfd_openw -- open for writing.
213 Returns a pointer to a freshly-allocated bfd on success, or NULL.
215 See comment by bfd_fdopenr before you try to modify this function. */
218 DEFUN(bfd_openw,(filename, target),
219 CONST char *filename AND
223 bfd_target *target_vec;
225 bfd_error = system_call_error;
227 /* nbfd has to point to head of malloc'ed block so that bfd_close may
228 reclaim it correctly. */
232 bfd_error = no_memory;
236 target_vec = bfd_find_target (target, nbfd);
237 if (target_vec == NULL) return NULL;
239 nbfd->filename = filename;
240 nbfd->direction = write_direction;
242 if (bfd_open_file (nbfd) == NULL) {
243 bfd_error = system_call_error; /* File not writeable, etc */
244 (void) obstack_free (&nbfd->memory, (PTR)0);
250 /* Close up shop, get your deposit back. */
255 if (!bfd_read_p(abfd))
256 if (BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)) != true)
259 if (BFD_SEND (abfd, _close_and_cleanup, (abfd)) != true) return false;
261 bfd_cache_close(abfd);
263 /* If the file was open for writing and is now executable,
265 if (abfd->direction == write_direction
266 && abfd->flags & EXEC_P) {
268 stat(abfd->filename, &buf);
270 #define S_IXUSR 0100 /* Execute by owner. */
273 #define S_IXGRP 0010 /* Execute by group. */
276 #define S_IXOTH 0001 /* Execute by others. */
279 chmod(abfd->filename,buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH);
281 (void) obstack_free (&abfd->memory, (PTR)0);
282 /* FIXME, shouldn't we de-allocate the bfd as well? */
286 /* Create a bfd with no associated file or target. */
289 DEFUN(bfd_create,(filename, template),
290 CONST char *filename AND
293 bfd *nbfd = new_bfd();
294 if (nbfd == (bfd *)NULL) {
295 bfd_error = no_memory;
298 nbfd->filename = filename;
300 nbfd->xvec = template->xvec;
302 nbfd->direction = no_direction;
303 bfd_set_format(nbfd, bfd_object);
307 /* Memory allocation */
309 DEFUN(PTR bfd_alloc_by_size_t,(abfd, size),
313 PTR res = obstack_alloc(&(abfd->memory), size);
316 DEFUN(PTR bfd_alloc, (abfd, size),
320 return bfd_alloc_by_size_t(abfd, (size_t)size);
323 DEFUN(PTR bfd_zalloc,(abfd, size),
327 PTR res = bfd_alloc(abfd, size);
328 memset(res, 0, (size_t)size);
332 DEFUN(PTR bfd_realloc,(abfd, old, size),
337 PTR res = bfd_alloc(abfd, size);
338 memcpy(res, old, (size_t)size);
343 DEFUN(bfd_size_type bfd_alloc_size,(abfd),
346 struct _obstack_chunk *chunk = abfd->memory.chunk;
349 size += chunk->limit - &(chunk->contents[0]);