Fixed minor typos.
[deliverable/binutils-gdb.git] / bfd / opncls.c
1 /* opncls.c -- open and close a BFD.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
20
21 /* $Id$ */
22
23 #include <sysdep.h>
24 #include "bfd.h"
25 #include "libbfd.h"
26 #include "obstack.h"
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
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 @code{bfd_openr} and @code{bfd_fdopenr} exist, yet
46 only @code{bfd_openw} exists is because of locking. When we do
47 output, we lock the filename file for output, then open a temporary
48 file which does not actually get its correct filename until closing
49 time. This is safest, but requires the asymmetry in read and write
50 entry points.
51
52 Perhaps, since unix has so many different kinds of locking anyway,
53 we should use the emacs lock scheme?... */
54
55 #define obstack_chunk_alloc malloc
56 #define obstack_chunk_free free
57
58 /* Return a new BFD. All BFD's are allocated through this routine. */
59
60 bfd *new_bfd()
61 {
62 bfd *nbfd;
63
64 nbfd = (bfd *)zalloc (sizeof (bfd));
65 if (!nbfd)
66 return 0;
67
68 obstack_begin((PTR)&nbfd->memory, 128);
69
70 nbfd->direction = no_direction;
71 nbfd->iostream = NULL;
72 nbfd->where = 0;
73 nbfd->sections = (asection *)NULL;
74 nbfd->format = bfd_unknown;
75 nbfd->my_archive = (bfd *)NULL;
76 nbfd->origin = 0;
77 nbfd->opened_once = false;
78 nbfd->output_has_begun = false;
79 nbfd->section_count = 0;
80 nbfd->usrdata = (PTR)NULL;
81 nbfd->sections = (asection *)NULL;
82 nbfd->cacheable = false;
83 nbfd->flags = NO_FLAGS;
84 nbfd->mtime_set = 0;
85 return nbfd;
86 }
87
88 /* Allocate a new BFD as a member of archive OBFD. */
89
90 bfd *new_bfd_contained_in(obfd)
91 bfd *obfd;
92 {
93 bfd *nbfd = new_bfd();
94 nbfd->xvec = obfd->xvec;
95 nbfd->my_archive = obfd;
96 nbfd->direction = read_direction;
97 return nbfd;
98 }
99
100 /*doc*
101 @section Opening and Closing BFDs
102
103 */
104 /*proto*
105 *i bfd_openr
106 Opens the file supplied (using @code{fopen}) with the target supplied, it
107 returns a pointer to the created BFD.
108
109 If NULL is returned then an error has occured.
110 Possible errors are no_memory, invalid_target or system_call error.
111 *; PROTO(bfd*, bfd_openr, (CONST char *filename,CONST char*target));
112 *-*/
113
114 bfd *
115 DEFUN(bfd_openr, (filename, target),
116 CONST char *filename AND
117 CONST char *target)
118 {
119 bfd *nbfd;
120 bfd_target *target_vec;
121
122 nbfd = new_bfd();
123 if (nbfd == NULL) {
124 bfd_error = no_memory;
125 return NULL;
126 }
127
128 target_vec = bfd_find_target (target, nbfd);
129 if (target_vec == NULL) {
130 bfd_error = invalid_target;
131 return NULL;
132 }
133
134 nbfd->filename = filename;
135 nbfd->direction = read_direction;
136
137 if (bfd_open_file (nbfd) == NULL) {
138 bfd_error = system_call_error; /* File didn't exist, or some such */
139 bfd_release(nbfd,0);
140 return NULL;
141 }
142 return nbfd;
143 }
144
145
146 /* Don't try to `optimize' this function:
147
148 o - We lock using stack space so that interrupting the locking
149 won't cause a storage leak.
150 o - We open the file stream last, since we don't want to have to
151 close it if anything goes wrong. Closing the stream means closing
152 the file descriptor too, even though we didn't open it.
153 */
154 /*proto*
155 *i bfd_fdopenr
156 bfd_fdopenr is to bfd_fopenr much like fdopen is to fopen. It opens a BFD on
157 a file already described by the @var{fd} supplied.
158
159 Possible errors are no_memory, invalid_target and system_call error.
160 *; PROTO(bfd *, bfd_fdopenr,
161 (CONST char *filename, CONST char *target, int fd));
162 *-*/
163
164 bfd *
165 DEFUN(bfd_fdopenr,(filename, target, fd),
166 CONST char *filename AND
167 CONST char *target AND
168 int fd)
169 {
170 bfd *nbfd;
171 bfd_target *target_vec;
172 int fdflags;
173 #ifdef BFD_LOCKS
174 struct flock lock, *lockp = &lock;
175 #endif
176
177 bfd_error = system_call_error;
178
179 fdflags = fcntl (fd, F_GETFL, NULL);
180 if (fdflags == -1) return NULL;
181
182 #ifdef BFD_LOCKS
183 lockp->l_type = F_RDLCK;
184 if (fcntl (fd, F_SETLKW, lockp) == -1) return NULL;
185 #endif
186
187 nbfd = new_bfd();
188
189 if (nbfd == NULL) {
190 bfd_error = no_memory;
191 return NULL;
192 }
193
194 target_vec = bfd_find_target (target, nbfd);
195 if (target_vec == NULL) {
196 bfd_error = invalid_target;
197 return NULL;
198 }
199
200 #ifdef BFD_LOCKS
201 nbfd->lock = (struct flock *) (nbfd + 1);
202 #endif
203 /* if the fd were open for read only, this still would not hurt: */
204 nbfd->iostream = (char *) fdopen (fd, "r+");
205 if (nbfd->iostream == NULL) {
206 (void) obstack_free (&nbfd->memory, (PTR)0);
207 return NULL;
208 }
209
210 /* OK, put everything where it belongs */
211
212 nbfd->filename = filename;
213
214 /* As a special case we allow a FD open for read/write to
215 be written through, although doing so requires that we end
216 the previous clause with a preposition. */
217 switch (fdflags & O_ACCMODE) {
218 case O_RDONLY: nbfd->direction = read_direction; break;
219 case O_WRONLY: nbfd->direction = write_direction; break;
220 case O_RDWR: nbfd->direction = both_direction; break;
221 default: abort ();
222 }
223
224 #ifdef BFD_LOCKS
225 memcpy (nbfd->lock, lockp, sizeof (struct flock))
226 #endif
227
228 bfd_cache_init (nbfd);
229
230 return nbfd;
231 }
232 \f
233 /** bfd_openw -- open for writing.
234 Returns a pointer to a freshly-allocated BFD on success, or NULL.
235
236 See comment by bfd_fdopenr before you try to modify this function. */
237
238 /*proto* bfd_openw
239 Creates a BFD, associated with file @var{filename}, using the file
240 format @var{target}, and returns a pointer to it.
241
242 Possible errors are system_call_error, no_memory, invalid_target.
243 *; PROTO(bfd *, bfd_openw, (CONST char *filename, CONST char *target));
244 */
245
246 bfd *
247 DEFUN(bfd_openw,(filename, target),
248 CONST char *filename AND
249 CONST char *target)
250 {
251 bfd *nbfd;
252 bfd_target *target_vec;
253
254 bfd_error = system_call_error;
255
256 /* nbfd has to point to head of malloc'ed block so that bfd_close may
257 reclaim it correctly. */
258
259 nbfd = new_bfd();
260 if (nbfd == NULL) {
261 bfd_error = no_memory;
262 return NULL;
263 }
264
265 target_vec = bfd_find_target (target, nbfd);
266 if (target_vec == NULL) return NULL;
267
268 nbfd->filename = filename;
269 nbfd->direction = write_direction;
270
271 if (bfd_open_file (nbfd) == NULL) {
272 bfd_error = system_call_error; /* File not writeable, etc */
273 (void) obstack_free (&nbfd->memory, (PTR)0);
274 return NULL;
275 }
276 return nbfd;
277 }
278
279 /*proto* bfd_close
280 This function closes a BFD. If the BFD was open for writing, then
281 pending operations are completed and the file written out and closed.
282 If the created file is executable, then @code{chmod} is called to mark
283 it as such.
284
285 All memory attached to the BFD's obstacks is released.
286
287 @code{true} is returned if all is ok, otherwise @code{false}.
288 *; PROTO(boolean, bfd_close,(bfd *));
289 */
290
291 boolean
292 DEFUN(bfd_close,(abfd),
293 bfd *abfd)
294 {
295 if (!bfd_read_p(abfd))
296 if (BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)) != true)
297 return false;
298
299 if (BFD_SEND (abfd, _close_and_cleanup, (abfd)) != true) return false;
300
301 bfd_cache_close(abfd);
302
303 /* If the file was open for writing and is now executable,
304 make it so */
305 if (abfd->direction == write_direction
306 && abfd->flags & EXEC_P) {
307 struct stat buf;
308 stat(abfd->filename, &buf);
309 #ifndef S_IXUSR
310 #define S_IXUSR 0100 /* Execute by owner. */
311 #endif
312 #ifndef S_IXGRP
313 #define S_IXGRP 0010 /* Execute by group. */
314 #endif
315 #ifndef S_IXOTH
316 #define S_IXOTH 0001 /* Execute by others. */
317 #endif
318
319 chmod(abfd->filename,buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH);
320 }
321 (void) obstack_free (&abfd->memory, (PTR)0);
322 /* FIXME, shouldn't we de-allocate the bfd as well? */
323 return true;
324 }
325
326 /*proto* bfd_create
327 This routine creates a new BFD in the manner of @code{bfd_openw}, but without
328 opening a file. The new BFD takes the target from the target used by
329 @var{template}. The format is always set to @code{bfd_object}.
330
331 *; PROTO(bfd *, bfd_create, (CONST char *filename, bfd *template));
332 */
333
334 bfd *
335 DEFUN(bfd_create,(filename, template),
336 CONST char *filename AND
337 bfd *template)
338 {
339 bfd *nbfd = new_bfd();
340 if (nbfd == (bfd *)NULL) {
341 bfd_error = no_memory;
342 return (bfd *)NULL;
343 }
344 nbfd->filename = filename;
345 if(template) {
346 nbfd->xvec = template->xvec;
347 }
348 nbfd->direction = no_direction;
349 bfd_set_format(nbfd, bfd_object);
350 return nbfd;
351 }
352
353 /* Memory allocation */
354
355 DEFUN(PTR bfd_alloc_by_size_t,(abfd, size),
356 bfd *abfd AND
357 size_t size)
358 {
359 PTR res = obstack_alloc(&(abfd->memory), size);
360 return res;
361 }
362
363 DEFUN(void bfd_alloc_grow,(abfd, ptr, size),
364 bfd *abfd AND
365 PTR ptr AND
366 bfd_size_type size)
367 {
368 (void) obstack_grow(&(abfd->memory), ptr, size);
369 }
370 DEFUN(PTR bfd_alloc_finish,(abfd),
371 bfd *abfd)
372 {
373 return obstack_finish(&(abfd->memory));
374 }
375
376 DEFUN(PTR bfd_alloc, (abfd, size),
377 bfd *abfd AND
378 bfd_size_type size)
379 {
380 return bfd_alloc_by_size_t(abfd, (size_t)size);
381 }
382
383 DEFUN(PTR bfd_zalloc,(abfd, size),
384 bfd *abfd AND
385 bfd_size_type size)
386 {
387 PTR res = bfd_alloc(abfd, size);
388 memset(res, 0, (size_t)size);
389 return res;
390 }
391
392 DEFUN(PTR bfd_realloc,(abfd, old, size),
393 bfd *abfd AND
394 PTR old AND
395 bfd_size_type size)
396 {
397 PTR res = bfd_alloc(abfd, size);
398 memcpy(res, old, (size_t)size);
399 return res;
400 }
401
402 /*proto* bfd_alloc_size
403 Return the number of bytes in the obstacks connected to the supplied
404 BFD.
405 *; PROTO(bfd_size_type,bfd_alloc_size,(bfd *abfd));
406 */
407
408 bfd_size_type
409 DEFUN( bfd_alloc_size,(abfd),
410 bfd *abfd)
411 {
412 struct _obstack_chunk *chunk = abfd->memory.chunk;
413 size_t size = 0;
414 while (chunk) {
415 size += chunk->limit - &(chunk->contents[0]);
416 chunk = chunk->prev;
417 }
418 return size;
419 }
This page took 0.03931 seconds and 5 git commands to generate.