Merge Per Bothner's MIPS changes from 31 March 1991 (bfd-0.5.2-patch1a)
[deliverable/binutils-gdb.git] / bfd / opncls.c
1 /* opncls.c -- open and close a bfd. */
2
3 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Diddler.
6
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)
10 any later version.
11
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.
16
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. */
20
21 /* $Id$ */
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "libbfd.h"
26
27 extern void bfd_cache_init();
28 FILE *bfd_open_file();
29
30 /* fdopen is a loser -- we should use stdio exclusively. Unfortunately
31 if we do that we can't use fcntl. */
32 \f
33 /** Locking
34
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.
40
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.
44
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.
50
51 Perhaps, since unix has so many different kinds of locking anyway,
52 we should use the emacs lock scheme?... */
53
54 extern PTR malloc();
55 extern void free();
56 #define obstack_chunk_alloc malloc
57 #define obstack_chunk_free free
58
59 /* Return a new BFD. All BFD's are allocated through this routine. */
60
61 bfd *new_bfd()
62 {
63 struct obstack tmp;
64 bfd *nbfd;
65 obstack_begin(&tmp,128);
66
67 nbfd = (bfd *)obstack_alloc(&tmp,sizeof(bfd));
68 memset((PTR)nbfd, 0, sizeof (bfd)); /* Clear it */
69
70 nbfd->memory = tmp;
71
72 nbfd->direction = no_direction;
73 nbfd->iostream = NULL;
74 nbfd->where = 0;
75 nbfd->sections = (asection *)NULL;
76 nbfd->format = bfd_unknown;
77 nbfd->my_archive = (bfd *)NULL;
78 nbfd->origin = 0;
79 nbfd->opened_once = false;
80 nbfd->output_has_begun = false;
81 nbfd->section_count = 0;
82 nbfd->usrdata = (PTR)NULL;
83 nbfd->sections = (asection *)NULL;
84 nbfd->cacheable = false;
85 nbfd->flags = NO_FLAGS;
86 nbfd->mtime_set = 0;
87 return nbfd;
88 }
89
90 /* Allocate a new BFD as a member of archive OBFD. */
91
92 bfd *new_bfd_contained_in(obfd)
93 bfd *obfd;
94 {
95 bfd *nbfd = new_bfd();
96 nbfd->xvec = obfd->xvec;
97 nbfd->my_archive = obfd;
98 nbfd->direction = read_direction;
99 return nbfd;
100 }
101
102 /** bfd_openr, bfd_fdopenr -- open for reading.
103 Returns a pointer to a freshly-allocated bfd on success, or NULL. */
104
105 bfd *
106 DEFUN(bfd_openr, (filename, target),
107 CONST char *filename AND
108 CONST char *target)
109 {
110 bfd *nbfd;
111 bfd_target *target_vec;
112
113 target_vec = bfd_find_target (target);
114 if (target_vec == NULL) {
115 bfd_error = invalid_target;
116 return NULL;
117 }
118
119 bfd_error = system_call_error;
120 nbfd = new_bfd();
121 if (nbfd == NULL) {
122 bfd_error = no_memory;
123 return NULL;
124 }
125
126 nbfd->filename = filename;
127 nbfd->xvec = target_vec;
128 nbfd->direction = read_direction;
129
130 if (bfd_open_file (nbfd) == NULL) {
131 bfd_error = system_call_error; /* File didn't exist, or some such */
132 bfd_release(nbfd,0);
133 return NULL;
134 }
135 return nbfd;
136 }
137
138
139 /* Don't try to `optimize' this function:
140
141 o - We lock using stack space so that interrupting the locking
142 won't cause a storage leak.
143 o - We open the file stream last, since we don't want to have to
144 close it if anything goes wrong. Closing the stream means closing
145 the file descriptor too, even though we didn't open it.
146 */
147
148 bfd *
149 DEFUN(bfd_fdopenr,(filename, target, fd),
150 CONST char *filename AND
151 CONST char *target AND
152 int fd)
153 {
154 bfd *nbfd;
155 bfd_target *target_vec;
156 int fdflags;
157 #ifdef BFD_LOCKS
158 struct flock lock, *lockp = &lock;
159 #endif
160
161 target_vec = bfd_find_target (target);
162 if (target_vec == NULL) {
163 bfd_error = invalid_target;
164 return NULL;
165 }
166
167 bfd_error = system_call_error;
168
169 fdflags = fcntl (fd, F_GETFL);
170 if (fdflags == -1) return NULL;
171
172 #ifdef BFD_LOCKS
173 lockp->l_type = F_RDLCK;
174 if (fcntl (fd, F_SETLKW, lockp) == -1) return NULL;
175 #endif
176
177 nbfd = new_bfd();
178
179 if (nbfd == NULL) {
180 bfd_error = no_memory;
181 return NULL;
182 }
183 #ifdef BFD_LOCKS
184 nbfd->lock = (struct flock *) (nbfd + 1);
185 #endif
186 /* if the fd were open for read only, this still would not hurt: */
187 nbfd->iostream = (char *) fdopen (fd, "r+");
188 if (nbfd->iostream == NULL) {
189 (void) obstack_free (&nbfd->memory, (PTR)0);
190 return NULL;
191 }
192
193 /* OK, put everything where it belongs */
194
195 nbfd->filename = filename;
196 nbfd->xvec = target_vec;
197
198 /* As a special case we allow a FD open for read/write to
199 be written through, although doing so requires that we end
200 the previous clause with a preposition. */
201 switch (fdflags & O_ACCMODE) {
202 case O_RDONLY: nbfd->direction = read_direction; break;
203 case O_WRONLY: nbfd->direction = write_direction; break;
204 case O_RDWR: nbfd->direction = both_direction; break;
205 default: abort ();
206 }
207
208 #ifdef BFD_LOCKS
209 memcpy (nbfd->lock, lockp, sizeof (struct flock))
210 #endif
211
212 bfd_cache_init (nbfd);
213
214 return nbfd;
215 }
216 \f
217 /** bfd_openw -- open for writing.
218 Returns a pointer to a freshly-allocated bfd on success, or NULL.
219
220 See comment by bfd_fdopenr before you try to modify this function. */
221
222 bfd *
223 DEFUN(bfd_openw,(filename, target),
224 CONST char *filename AND
225 CONST char *target)
226 {
227 bfd *nbfd;
228 bfd_target *target_vec;
229
230 target_vec = bfd_find_target (target);
231 if (target_vec == NULL) return NULL;
232
233 bfd_error = system_call_error;
234
235 /* nbfd has to point to head of malloc'ed block so that bfd_close may
236 reclaim it correctly. */
237
238 nbfd = new_bfd();
239 if (nbfd == NULL) {
240 bfd_error = no_memory;
241 return NULL;
242 }
243
244 nbfd->filename = filename;
245 nbfd->xvec = target_vec;
246 nbfd->direction = write_direction;
247
248 if (bfd_open_file (nbfd) == NULL) {
249 bfd_error = system_call_error; /* File not writeable, etc */
250 (void) obstack_free (&nbfd->memory, (PTR)0);
251 return NULL;
252 }
253 return nbfd;
254 }
255
256
257 \f
258 /** Close up shop, get your deposit back. */
259 boolean
260 bfd_close (abfd)
261 bfd *abfd;
262 {
263 if (BFD_SEND (abfd, _close_and_cleanup, (abfd)) != true) return false;
264
265 bfd_cache_close(abfd);
266 /* If the file was open for writing and is now executable
267 make it so */
268 if (abfd->direction == write_direction
269 && abfd->flags & EXEC_P) {
270 struct stat buf;
271 stat(abfd->filename, &buf);
272 chmod(abfd->filename,buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH);
273 }
274 (void) obstack_free (&abfd->memory, (PTR)0);
275 return true;
276 }
277
278 /* Create a bfd with no associated file or target. */
279
280 bfd *
281 DEFUN(bfd_create,(filename, template),
282 CONST char *filename AND
283 CONST bfd *template)
284 {
285 bfd *nbfd = new_bfd();
286 if (nbfd == (bfd *)NULL) {
287 bfd_error = no_memory;
288 return (bfd *)NULL;
289 }
290 nbfd->filename = filename;
291 if(template) {
292 nbfd->xvec = template->xvec;
293 }
294 nbfd->direction = no_direction;
295 bfd_set_format(nbfd, bfd_object);
296 return nbfd;
297 }
298
299 /* Memory allocation */
300
301 DEFUN(PTR bfd_alloc, (abfd, size),
302 bfd *abfd AND
303 bfd_size_type size)
304 {
305 PTR res = obstack_alloc(&(abfd->memory), (int)size);
306 return res;
307 }
308
309 DEFUN(PTR bfd_zalloc,(abfd, size),
310 bfd *abfd AND
311 bfd_size_type size)
312 {
313 PTR res = bfd_alloc(abfd, size);
314 memset(res, 0, (size_t)size);
315 return res;
316 }
317
318 DEFUN(PTR bfd_realloc,(abfd, old, size),
319 bfd *abfd AND
320 PTR old AND
321 bfd_size_type size)
322 {
323 PTR res = bfd_alloc(abfd, size);
324 memcpy(res, old, (size_t)size);
325 return res;
326 }
327
328
329 DEFUN(bfd_size_type bfd_alloc_size,(abfd),
330 bfd *abfd)
331 {
332 struct _obstack_chunk *chunk = abfd->memory.chunk;
333 size_t size = 0;
334 while (chunk) {
335 size += chunk->limit - &(chunk->contents[0]);
336 chunk = chunk->prev;
337 }
338 return size;
339 }
This page took 0.037965 seconds and 5 git commands to generate.