(srec_write_symbols): Use sprintf_vma, in case bfd_vma is too wide for
[deliverable/binutils-gdb.git] / bfd / opncls.c
CommitLineData
6724ff46
RP
1/* opncls.c -- open and close a BFD.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
fc723380 4
6724ff46 5This file is part of BFD, the Binary File Descriptor library.
4a81b561 6
6724ff46 7This program is free software; you can redistribute it and/or modify
4a81b561 8it under the terms of the GNU General Public License as published by
6724ff46
RP
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
4a81b561 11
6724ff46 12This program is distributed in the hope that it will be useful,
4a81b561
DHW
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
6724ff46
RP
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
4a81b561
DHW
20
21/* $Id$ */
22
4a81b561 23#include "bfd.h"
ff7ce170 24#include "sysdep.h"
4a81b561 25#include "libbfd.h"
6724ff46 26#include "obstack.h"
4a81b561
DHW
27extern void bfd_cache_init();
28FILE *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. */
6724ff46 32
4a81b561 33
ff7ce170 34#define obstack_chunk_alloc bfd_xmalloc
9872a49c
SC
35#define obstack_chunk_free free
36
fc723380
JG
37/* Return a new BFD. All BFD's are allocated through this routine. */
38
4a81b561
DHW
39bfd *new_bfd()
40{
9872a49c 41 bfd *nbfd;
fc723380 42
b1847ba9
JG
43 nbfd = (bfd *)zalloc (sizeof (bfd));
44 if (!nbfd)
45 return 0;
4a81b561 46
ff7ce170
PB
47 bfd_check_init();
48 obstack_begin((PTR)&nbfd->memory, 128);
49
50 nbfd->arch_info = &bfd_default_arch_struct;
51
4a81b561
DHW
52 nbfd->direction = no_direction;
53 nbfd->iostream = NULL;
54 nbfd->where = 0;
55 nbfd->sections = (asection *)NULL;
56 nbfd->format = bfd_unknown;
57 nbfd->my_archive = (bfd *)NULL;
58 nbfd->origin = 0;
59 nbfd->opened_once = false;
60 nbfd->output_has_begun = false;
61 nbfd->section_count = 0;
9846338e 62 nbfd->usrdata = (PTR)NULL;
4a81b561
DHW
63 nbfd->sections = (asection *)NULL;
64 nbfd->cacheable = false;
65 nbfd->flags = NO_FLAGS;
9068cbe7
SC
66 nbfd->mtime_set = false;
67
68
4a81b561
DHW
69 return nbfd;
70}
fc723380
JG
71
72/* Allocate a new BFD as a member of archive OBFD. */
73
4a81b561
DHW
74bfd *new_bfd_contained_in(obfd)
75bfd *obfd;
76{
9846338e 77 bfd *nbfd = new_bfd();
4a81b561
DHW
78 nbfd->xvec = obfd->xvec;
79 nbfd->my_archive = obfd;
80 nbfd->direction = read_direction;
9068cbe7 81 nbfd->target_defaulted = obfd->target_defaulted;
4a81b561
DHW
82 return nbfd;
83}
84
b645b632
SC
85/*
86SECTION
87 Opening and Closing BFDs
6f715d66
SC
88
89*/
6f715d66 90
b645b632
SC
91/*
92FUNCTION
93 bfd_openr
94
95SYNOPSIS
96 bfd *bfd_openr(CONST char *filename, CONST char*target);
97
98DESCRIPTION
99 This function opens the file supplied (using <<fopen>>) with the target
100 supplied, it returns a pointer to the created BFD.
101
102 If NULL is returned then an error has occured. Possible errors
103 are <<no_memory>>, <<invalid_target>> or <<system_call>> error.
104*/
4a81b561
DHW
105
106bfd *
9846338e
SC
107DEFUN(bfd_openr, (filename, target),
108 CONST char *filename AND
109 CONST char *target)
4a81b561
DHW
110{
111 bfd *nbfd;
112 bfd_target *target_vec;
113
4a81b561
DHW
114 nbfd = new_bfd();
115 if (nbfd == NULL) {
116 bfd_error = no_memory;
117 return NULL;
118 }
119
c0e5039e
JG
120 target_vec = bfd_find_target (target, nbfd);
121 if (target_vec == NULL) {
122 bfd_error = invalid_target;
123 return NULL;
124 }
125
4a81b561 126 nbfd->filename = filename;
4a81b561
DHW
127 nbfd->direction = read_direction;
128
129 if (bfd_open_file (nbfd) == NULL) {
130 bfd_error = system_call_error; /* File didn't exist, or some such */
9872a49c 131 bfd_release(nbfd,0);
4a81b561
DHW
132 return NULL;
133 }
134 return nbfd;
135}
136
137
138/* Don't try to `optimize' this function:
139
140 o - We lock using stack space so that interrupting the locking
141 won't cause a storage leak.
142 o - We open the file stream last, since we don't want to have to
143 close it if anything goes wrong. Closing the stream means closing
144 the file descriptor too, even though we didn't open it.
145 */
b645b632
SC
146/*
147FUNCTION
148 bfd_fdopenr
6f715d66 149
b645b632
SC
150SYNOPSIS
151 bfd *bfd_fdopenr(CONST char *filename, CONST char *target, int fd);
152
153DESCRIPTION
154 bfd_fdopenr is to bfd_fopenr much like fdopen is to fopen.
155 It opens a BFD on a file already described by the @var{fd}
156 supplied.
157
158 Possible errors are no_memory, invalid_target and system_call
159 error.
160*/
4a81b561
DHW
161
162bfd *
9846338e
SC
163DEFUN(bfd_fdopenr,(filename, target, fd),
164 CONST char *filename AND
165 CONST char *target AND
166 int fd)
4a81b561
DHW
167{
168 bfd *nbfd;
169 bfd_target *target_vec;
170 int fdflags;
4a81b561 171
4a81b561
DHW
172 bfd_error = system_call_error;
173
fb3be09b
JG
174#ifdef NO_FCNTL
175 fdflags = O_RDWR; /* Assume full access */
176#else
6f715d66 177 fdflags = fcntl (fd, F_GETFL, NULL);
4a81b561 178#endif
fb3be09b 179 if (fdflags == -1) return NULL;
4a81b561
DHW
180
181 nbfd = new_bfd();
182
183 if (nbfd == NULL) {
184 bfd_error = no_memory;
185 return NULL;
186 }
c0e5039e
JG
187
188 target_vec = bfd_find_target (target, nbfd);
189 if (target_vec == NULL) {
190 bfd_error = invalid_target;
191 return NULL;
192 }
193
ff7ce170 194#ifdef FASCIST_FDOPEN
9068cbe7 195 nbfd->iostream = (char *) fdopen (fd, FOPEN_RB);
ff7ce170 196#else
4a81b561 197 /* if the fd were open for read only, this still would not hurt: */
9068cbe7 198 nbfd->iostream = (char *) fdopen (fd, FOPEN_RUB);
ff7ce170 199#endif
4a81b561 200 if (nbfd->iostream == NULL) {
fc723380 201 (void) obstack_free (&nbfd->memory, (PTR)0);
4a81b561
DHW
202 return NULL;
203 }
204
205 /* OK, put everything where it belongs */
206
207 nbfd->filename = filename;
4a81b561
DHW
208
209 /* As a special case we allow a FD open for read/write to
210 be written through, although doing so requires that we end
211 the previous clause with a preposition. */
ff7ce170
PB
212 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
213 switch (fdflags & (O_ACCMODE)) {
4a81b561
DHW
214 case O_RDONLY: nbfd->direction = read_direction; break;
215 case O_WRONLY: nbfd->direction = write_direction; break;
216 case O_RDWR: nbfd->direction = both_direction; break;
217 default: abort ();
218 }
219
c0e5039e 220 bfd_cache_init (nbfd);
4a81b561
DHW
221
222 return nbfd;
223}
224\f
225/** bfd_openw -- open for writing.
6724ff46 226 Returns a pointer to a freshly-allocated BFD on success, or NULL.
4a81b561
DHW
227
228 See comment by bfd_fdopenr before you try to modify this function. */
229
b645b632
SC
230/*
231FUNCTION
232 bfd_openw
233
234SYNOPSIS
235 bfd *bfd_openw(CONST char *filename, CONST char *target);
6f715d66 236
b645b632
SC
237DESCRIPTION
238 Creates a BFD, associated with file @var{filename}, using the
239 file format @var{target}, and returns a pointer to it.
240
241 Possible errors are system_call_error, no_memory,
242 invalid_target.
6f715d66
SC
243*/
244
4a81b561 245bfd *
9846338e
SC
246DEFUN(bfd_openw,(filename, target),
247 CONST char *filename AND
248 CONST char *target)
4a81b561
DHW
249{
250 bfd *nbfd;
251 bfd_target *target_vec;
252
4a81b561
DHW
253 bfd_error = system_call_error;
254
255 /* nbfd has to point to head of malloc'ed block so that bfd_close may
256 reclaim it correctly. */
257
258 nbfd = new_bfd();
259 if (nbfd == NULL) {
260 bfd_error = no_memory;
261 return NULL;
262 }
263
c0e5039e
JG
264 target_vec = bfd_find_target (target, nbfd);
265 if (target_vec == NULL) return NULL;
266
4a81b561 267 nbfd->filename = filename;
4a81b561
DHW
268 nbfd->direction = write_direction;
269
270 if (bfd_open_file (nbfd) == NULL) {
271 bfd_error = system_call_error; /* File not writeable, etc */
fc723380 272 (void) obstack_free (&nbfd->memory, (PTR)0);
4a81b561
DHW
273 return NULL;
274 }
275 return nbfd;
276}
6f715d66 277
b645b632
SC
278/*
279
280FUNCTION
281 bfd_close
282
283SYNOPSIS
284 boolean bfd_close(bfd *);
285
286DESCRIPTION
6f715d66 287
b645b632
SC
288 This function closes a BFD. If the BFD was open for writing,
289 then pending operations are completed and the file written out
290 and closed. If the created file is executable, then
291 <<chmod>> is called to mark it as such.
6f715d66 292
b645b632
SC
293 All memory attached to the BFD's obstacks is released.
294
295RETURNS
296 <<true>> is returned if all is ok, otherwise <<false>>.
6f715d66
SC
297*/
298
b645b632 299
4a81b561 300boolean
6f715d66
SC
301DEFUN(bfd_close,(abfd),
302 bfd *abfd)
4a81b561 303{
2b1d8a50
JG
304 if (!bfd_read_p(abfd))
305 if (BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)) != true)
306 return false;
307
4a81b561
DHW
308 if (BFD_SEND (abfd, _close_and_cleanup, (abfd)) != true) return false;
309
310 bfd_cache_close(abfd);
2b1d8a50
JG
311
312 /* If the file was open for writing and is now executable,
313 make it so */
4a81b561
DHW
314 if (abfd->direction == write_direction
315 && abfd->flags & EXEC_P) {
316 struct stat buf;
317 stat(abfd->filename, &buf);
7ed4093a
SC
318#ifndef S_IXUSR
319#define S_IXUSR 0100 /* Execute by owner. */
320#endif
321#ifndef S_IXGRP
322#define S_IXGRP 0010 /* Execute by group. */
323#endif
324#ifndef S_IXOTH
325#define S_IXOTH 0001 /* Execute by others. */
326#endif
327
b645b632 328 chmod(abfd->filename, 0777 & (buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH));
4a81b561 329 }
fc723380 330 (void) obstack_free (&abfd->memory, (PTR)0);
ff7ce170
PB
331 (void) free(abfd);
332 return true;
333}
334
b645b632
SC
335/*
336FUNCTION
337 bfd_close_all_done
338
339SYNOPSIS
340 boolean bfd_close_all_done(bfd *);
341
342DESCRIPTION
343 This function closes a BFD. It differs from <<bfd_close>>
344 since it does not complete any pending operations. This
345 routine would be used if the application had just used BFD for
346 swapping and didn't want to use any of the writing code.
ff7ce170 347
b645b632
SC
348 If the created file is executable, then <<chmod>> is called
349 to mark it as such.
ff7ce170 350
b645b632
SC
351 All memory attached to the BFD's obstacks is released.
352
353RETURNS
354 <<true>> is returned if all is ok, otherwise <<false>>.
ff7ce170 355
ff7ce170
PB
356*/
357
358boolean
359DEFUN(bfd_close_all_done,(abfd),
360 bfd *abfd)
361{
362 bfd_cache_close(abfd);
363
364 /* If the file was open for writing and is now executable,
365 make it so */
366 if (abfd->direction == write_direction
367 && abfd->flags & EXEC_P) {
368 struct stat buf;
369 stat(abfd->filename, &buf);
370#ifndef S_IXUSR
371#define S_IXUSR 0100 /* Execute by owner. */
372#endif
373#ifndef S_IXGRP
374#define S_IXGRP 0010 /* Execute by group. */
375#endif
376#ifndef S_IXOTH
377#define S_IXOTH 0001 /* Execute by others. */
378#endif
379
b645b632 380 chmod(abfd->filename, 0x777 &(buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH));
ff7ce170
PB
381 }
382 (void) obstack_free (&abfd->memory, (PTR)0);
383 (void) free(abfd);
4a81b561
DHW
384 return true;
385}
fc723380 386
6f715d66 387
b645b632
SC
388/*
389FUNCTION
390 bfd_alloc_size
391
392SYNOPSIS
393 bfd_size_type bfd_alloc_size(bfd *abfd);
394
395DESCRIPTION
396 Return the number of bytes in the obstacks connected to the
397 supplied BFD.
398
399*/
400
401bfd_size_type
402DEFUN(bfd_alloc_size,(abfd),
403 bfd *abfd)
404{
405 struct _obstack_chunk *chunk = abfd->memory.chunk;
406 size_t size = 0;
407 while (chunk) {
408 size += chunk->limit - &(chunk->contents[0]);
409 chunk = chunk->prev;
410 }
411 return size;
412}
413
414
415
416/*
417FUNCTION
418 bfd_create
419
420SYNOPSIS
421 bfd *bfd_create(CONST char *filename, bfd *template);
422
423DESCRIPTION
424 This routine creates a new BFD in the manner of
425 <<bfd_openw>>, but without opening a file. The new BFD
426 takes the target from the target used by @var{template}. The
427 format is always set to <<bfd_object>>.
428
6f715d66 429*/
fc723380 430
4a81b561 431bfd *
9846338e
SC
432DEFUN(bfd_create,(filename, template),
433 CONST char *filename AND
6f715d66 434 bfd *template)
4a81b561
DHW
435{
436 bfd *nbfd = new_bfd();
437 if (nbfd == (bfd *)NULL) {
438 bfd_error = no_memory;
439 return (bfd *)NULL;
440 }
441 nbfd->filename = filename;
9872a49c
SC
442 if(template) {
443 nbfd->xvec = template->xvec;
444 }
4a81b561 445 nbfd->direction = no_direction;
9872a49c 446 bfd_set_format(nbfd, bfd_object);
4a81b561 447 return nbfd;
4a81b561 448}
9872a49c 449
b645b632
SC
450/*
451INTERNAL_FUNCTION
452 bfd_alloc_by_size_t
453
454SYNOPSIS
455 PTR bfd_alloc_by_size_t(bfd *abfd, size_t wanted);
fc723380 456
b645b632
SC
457DESCRIPTION
458 This function allocates a block of memory in the obstack
459 attatched to <<abfd>> and returns a pointer to it.
460*/
461
462
463PTR
464DEFUN(bfd_alloc_by_size_t,(abfd, size),
7ed4093a
SC
465 bfd *abfd AND
466 size_t size)
467{
468 PTR res = obstack_alloc(&(abfd->memory), size);
469 return res;
470}
6f715d66
SC
471
472DEFUN(void bfd_alloc_grow,(abfd, ptr, size),
473 bfd *abfd AND
474 PTR ptr AND
475 bfd_size_type size)
476{
6724ff46 477 (void) obstack_grow(&(abfd->memory), ptr, size);
6f715d66
SC
478}
479DEFUN(PTR bfd_alloc_finish,(abfd),
480 bfd *abfd)
481{
482 return obstack_finish(&(abfd->memory));
483}
484
9872a49c
SC
485DEFUN(PTR bfd_alloc, (abfd, size),
486 bfd *abfd AND
fc723380 487 bfd_size_type size)
9872a49c 488{
7ed4093a 489 return bfd_alloc_by_size_t(abfd, (size_t)size);
9872a49c
SC
490}
491
492DEFUN(PTR bfd_zalloc,(abfd, size),
493 bfd *abfd AND
fc723380 494 bfd_size_type size)
9872a49c
SC
495{
496 PTR res = bfd_alloc(abfd, size);
fc723380 497 memset(res, 0, (size_t)size);
9872a49c
SC
498 return res;
499}
500
501DEFUN(PTR bfd_realloc,(abfd, old, size),
502 bfd *abfd AND
503 PTR old AND
fc723380 504 bfd_size_type size)
9872a49c
SC
505{
506 PTR res = bfd_alloc(abfd, size);
fc723380 507 memcpy(res, old, (size_t)size);
9872a49c
SC
508 return res;
509}
510
511
b645b632
SC
512
513
514
515
516
517
518
519
This page took 0.098117 seconds and 4 git commands to generate.