/* $Id$ */
-#include <sysdep.h>
#include "bfd.h"
+#include "sysdep.h"
#include "libbfd.h"
#include "obstack.h"
extern void bfd_cache_init();
if we do that we can't use fcntl. */
-#define obstack_chunk_alloc malloc
+#define obstack_chunk_alloc bfd_xmalloc
#define obstack_chunk_free free
/* Return a new BFD. All BFD's are allocated through this routine. */
if (!nbfd)
return 0;
- obstack_begin(&nbfd->memory, 128);
-
+ bfd_check_init();
+ obstack_begin((PTR)&nbfd->memory, 128);
+
+ nbfd->arch_info = &bfd_default_arch_struct;
+
nbfd->direction = no_direction;
nbfd->iostream = NULL;
nbfd->where = 0;
nbfd->sections = (asection *)NULL;
nbfd->cacheable = false;
nbfd->flags = NO_FLAGS;
- nbfd->mtime_set = 0;
+ nbfd->mtime_set = false;
+
+
return nbfd;
}
nbfd->xvec = obfd->xvec;
nbfd->my_archive = obfd;
nbfd->direction = read_direction;
+ nbfd->target_defaulted = obfd->target_defaulted;
return nbfd;
}
-/*doc*
-@section Opening and Closing BFDs
+/*
+SECTION
+ Opening and Closing BFDs
*/
-/*proto*
-*i bfd_openr
-Opens the file supplied (using @code{fopen}) with the target supplied, it
-returns a pointer to the created BFD.
-If NULL is returned then an error has occured.
-Possible errors are no_memory, invalid_target or system_call error.
-*; PROTO(bfd*, bfd_openr, (CONST char *filename,CONST char*target));
-*-*/
+/*
+FUNCTION
+ bfd_openr
+
+SYNOPSIS
+ bfd *bfd_openr(CONST char *filename, CONST char*target);
+
+DESCRIPTION
+ This function opens the file supplied (using <<fopen>>) with the target
+ supplied, it returns a pointer to the created BFD.
+
+ If NULL is returned then an error has occured. Possible errors
+ are <<no_memory>>, <<invalid_target>> or <<system_call>> error.
+*/
bfd *
DEFUN(bfd_openr, (filename, target),
close it if anything goes wrong. Closing the stream means closing
the file descriptor too, even though we didn't open it.
*/
-/*proto*
-*i bfd_fdopenr
-bfd_fdopenr is to bfd_fopenr much like fdopen is to fopen. It opens a BFD on
-a file already described by the @var{fd} supplied.
+/*
+FUNCTION
+ bfd_fdopenr
+
+SYNOPSIS
+ bfd *bfd_fdopenr(CONST char *filename, CONST char *target, int fd);
-Possible errors are no_memory, invalid_target and system_call error.
-*; PROTO(bfd *, bfd_fdopenr,
- (CONST char *filename, CONST char *target, int fd));
-*-*/
+DESCRIPTION
+ bfd_fdopenr is to bfd_fopenr much like fdopen is to fopen.
+ It opens a BFD on a file already described by the @var{fd}
+ supplied.
+
+ Possible errors are no_memory, invalid_target and system_call
+ error.
+*/
bfd *
DEFUN(bfd_fdopenr,(filename, target, fd),
return NULL;
}
+#ifdef FASCIST_FDOPEN
+ nbfd->iostream = (char *) fdopen (fd, FOPEN_RB);
+#else
/* if the fd were open for read only, this still would not hurt: */
- nbfd->iostream = (char *) fdopen (fd, "r+");
+ nbfd->iostream = (char *) fdopen (fd, FOPEN_RUB);
+#endif
if (nbfd->iostream == NULL) {
(void) obstack_free (&nbfd->memory, (PTR)0);
return NULL;
/* As a special case we allow a FD open for read/write to
be written through, although doing so requires that we end
the previous clause with a preposition. */
- switch (fdflags & O_ACCMODE) {
+ /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
+ switch (fdflags & (O_ACCMODE)) {
case O_RDONLY: nbfd->direction = read_direction; break;
case O_WRONLY: nbfd->direction = write_direction; break;
case O_RDWR: nbfd->direction = both_direction; break;
See comment by bfd_fdopenr before you try to modify this function. */
-/*proto* bfd_openw
-Creates a BFD, associated with file @var{filename}, using the file
-format @var{target}, and returns a pointer to it.
+/*
+FUNCTION
+ bfd_openw
+
+SYNOPSIS
+ bfd *bfd_openw(CONST char *filename, CONST char *target);
-Possible errors are system_call_error, no_memory, invalid_target.
-*; PROTO(bfd *, bfd_openw, (CONST char *filename, CONST char *target));
+DESCRIPTION
+ Creates a BFD, associated with file @var{filename}, using the
+ file format @var{target}, and returns a pointer to it.
+
+ Possible errors are system_call_error, no_memory,
+ invalid_target.
*/
bfd *
return nbfd;
}
-/*proto* bfd_close
-This function closes a BFD. If the BFD was open for writing, then
-pending operations are completed and the file written out and closed.
-If the created file is executable, then @code{chmod} is called to mark
-it as such.
+/*
+
+FUNCTION
+ bfd_close
+
+SYNOPSIS
+ boolean bfd_close(bfd *);
+
+DESCRIPTION
+
+ This function closes a BFD. If the BFD was open for writing,
+ then pending operations are completed and the file written out
+ and closed. If the created file is executable, then
+ <<chmod>> is called to mark it as such.
-All memory attached to the BFD's obstacks is released.
+ All memory attached to the BFD's obstacks is released.
-@code{true} is returned if all is ok, otherwise @code{false}.
-*; PROTO(boolean, bfd_close,(bfd *));
+RETURNS
+ <<true>> is returned if all is ok, otherwise <<false>>.
*/
+
boolean
DEFUN(bfd_close,(abfd),
bfd *abfd)
#define S_IXOTH 0001 /* Execute by others. */
#endif
- chmod(abfd->filename,buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH);
+ chmod(abfd->filename, 0777 & (buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH));
}
(void) obstack_free (&abfd->memory, (PTR)0);
- /* FIXME, shouldn't we de-allocate the bfd as well? */
+ (void) free(abfd);
return true;
}
-/*proto* bfd_create
-This routine creates a new BFD in the manner of @code{bfd_openw}, but without
-opening a file. The new BFD takes the target from the target used by
-@var{template}. The format is always set to @code{bfd_object}.
+/*
+FUNCTION
+ bfd_close_all_done
+
+SYNOPSIS
+ boolean bfd_close_all_done(bfd *);
+
+DESCRIPTION
+ This function closes a BFD. It differs from <<bfd_close>>
+ since it does not complete any pending operations. This
+ routine would be used if the application had just used BFD for
+ swapping and didn't want to use any of the writing code.
+
+ If the created file is executable, then <<chmod>> is called
+ to mark it as such.
+
+ All memory attached to the BFD's obstacks is released.
+
+RETURNS
+ <<true>> is returned if all is ok, otherwise <<false>>.
+
+*/
+
+boolean
+DEFUN(bfd_close_all_done,(abfd),
+ bfd *abfd)
+{
+ bfd_cache_close(abfd);
+
+ /* If the file was open for writing and is now executable,
+ make it so */
+ if (abfd->direction == write_direction
+ && abfd->flags & EXEC_P) {
+ struct stat buf;
+ stat(abfd->filename, &buf);
+#ifndef S_IXUSR
+#define S_IXUSR 0100 /* Execute by owner. */
+#endif
+#ifndef S_IXGRP
+#define S_IXGRP 0010 /* Execute by group. */
+#endif
+#ifndef S_IXOTH
+#define S_IXOTH 0001 /* Execute by others. */
+#endif
+
+ chmod(abfd->filename, 0x777 &(buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH));
+ }
+ (void) obstack_free (&abfd->memory, (PTR)0);
+ (void) free(abfd);
+ return true;
+}
+
+
+/*
+FUNCTION
+ bfd_alloc_size
+
+SYNOPSIS
+ bfd_size_type bfd_alloc_size(bfd *abfd);
+
+DESCRIPTION
+ Return the number of bytes in the obstacks connected to the
+ supplied BFD.
+
+*/
+
+bfd_size_type
+DEFUN(bfd_alloc_size,(abfd),
+ bfd *abfd)
+{
+ struct _obstack_chunk *chunk = abfd->memory.chunk;
+ size_t size = 0;
+ while (chunk) {
+ size += chunk->limit - &(chunk->contents[0]);
+ chunk = chunk->prev;
+ }
+ return size;
+}
+
+
+
+/*
+FUNCTION
+ bfd_create
+
+SYNOPSIS
+ bfd *bfd_create(CONST char *filename, bfd *template);
+
+DESCRIPTION
+ This routine creates a new BFD in the manner of
+ <<bfd_openw>>, but without opening a file. The new BFD
+ takes the target from the target used by @var{template}. The
+ format is always set to <<bfd_object>>.
-*; PROTO(bfd *, bfd_create, (CONST char *filename, bfd *template));
*/
bfd *
return nbfd;
}
-/* Memory allocation */
+/*
+INTERNAL_FUNCTION
+ bfd_alloc_by_size_t
+
+SYNOPSIS
+ PTR bfd_alloc_by_size_t(bfd *abfd, size_t wanted);
+
+DESCRIPTION
+ This function allocates a block of memory in the obstack
+ attatched to <<abfd>> and returns a pointer to it.
+*/
-DEFUN(PTR bfd_alloc_by_size_t,(abfd, size),
+
+PTR
+DEFUN(bfd_alloc_by_size_t,(abfd, size),
bfd *abfd AND
size_t size)
{
return res;
}
-/*proto* bfd_alloc_size
-Return the number of bytes in the obstacks connected to the supplied
-BFD.
-*; PROTO(bfd_size_type,bfd_alloc_size,(bfd *abfd));
-*/
-bfd_size_type
-DEFUN( bfd_alloc_size,(abfd),
- bfd *abfd)
-{
- struct _obstack_chunk *chunk = abfd->memory.chunk;
- size_t size = 0;
- while (chunk) {
- size += chunk->limit - &(chunk->contents[0]);
- chunk = chunk->prev;
- }
- return size;
-}
+
+
+
+
+
+
+
+