Remove special case handling for rtems targets that are sufficiently handled by
[deliverable/binutils-gdb.git] / bfd / opncls.c
index 76030301ce1919957405f4d8f87dfc87d1429dca..c5cc252c66bdc4dc2cf8f9f44934374b5500f1b1 100644 (file)
@@ -1,6 +1,6 @@
 /* opncls.c -- open and close a BFD.
    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
-   2001, 2002, 2003
+   2001, 2002, 2003, 2004, 2005
    Free Software Foundation, Inc.
 
    Written by Cygnus Support.
@@ -19,7 +19,7 @@
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 #include "bfd.h"
 #include "sysdep.h"
@@ -77,7 +77,7 @@ _bfd_new_bfd (void)
       return NULL;
     }
   nbfd->sections = NULL;
-  nbfd->section_tail = &nbfd->sections;
+  nbfd->section_last = NULL;
   nbfd->format = bfd_unknown;
   nbfd->my_archive = NULL;
   nbfd->origin = 0;
@@ -128,25 +128,31 @@ SECTION
 
 /*
 FUNCTION
-       bfd_openr
+       bfd_fopen
 
 SYNOPSIS
-       bfd *bfd_openr (const char *filename, const char *target);
+       bfd *bfd_fopen (const char *filename, const char *target,
+                        const char *mode, int fd);
 
 DESCRIPTION
-       Open the file @var{filename} (using <<fopen>>) with the target
-       @var{target}.  Return a pointer to the created BFD.
+       Open the file @var{filename} with the target @var{target}.
+       Return a pointer to the created BFD.  If @var{fd} is not -1,
+       then <<fdopen>> is used to open the file; otherwise, <<fopen>>
+       is used.  @var{mode} is passed directly to <<fopen>> or
+       <<fdopen>>. 
 
        Calls <<bfd_find_target>>, so @var{target} is interpreted as by
        that function.
 
+       The new BFD is marked as cacheable iff @var{fd} is -1.
+
        If <<NULL>> is returned then an error has occured.   Possible errors
        are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or
        <<system_call>> error.
 */
 
 bfd *
-bfd_openr (const char *filename, const char *target)
+bfd_fopen (const char *filename, const char *target, const char *mode, int fd)
 {
   bfd *nbfd;
   const bfd_target *target_vec;
@@ -161,21 +167,74 @@ bfd_openr (const char *filename, const char *target)
       _bfd_delete_bfd (nbfd);
       return NULL;
     }
+  
+#ifdef HAVE_FDOPEN
+  if (fd != -1)
+    nbfd->iostream = fdopen (fd, mode);
+  else
+#endif
+    nbfd->iostream = fopen (filename, mode);
+  if (nbfd->iostream == NULL)
+    {
+      bfd_set_error (bfd_error_system_call);
+      _bfd_delete_bfd (nbfd);
+      return NULL;
+    }
 
+  /* OK, put everything where it belongs.  */
   nbfd->filename = filename;
-  nbfd->direction = read_direction;
 
-  if (bfd_open_file (nbfd) == NULL)
+  /* Figure out whether the user is opening the file for reading,
+     writing, or both, by looking at the MODE argument.  */
+  if ((mode[0] == 'r' || mode[0] == 'w' || mode[0] == 'a') 
+      && mode[1] == '+')
+    nbfd->direction = both_direction;
+  else if (mode[0] == 'r')
+    nbfd->direction = read_direction;
+  else
+    nbfd->direction = write_direction;
+
+  if (! bfd_cache_init (nbfd))
     {
-      /* File didn't exist, or some such.  */
-      bfd_set_error (bfd_error_system_call);
       _bfd_delete_bfd (nbfd);
       return NULL;
     }
+  nbfd->opened_once = TRUE;
+  /* If we opened the file by name, mark it cacheable; we can close it
+     and reopen it later.  However, if a file descriptor was provided,
+     then it may have been opened with special flags that make it
+     unsafe to close and reopen the file.  */
+  if (fd == -1)
+    bfd_set_cacheable (nbfd, TRUE);
 
   return nbfd;
 }
 
+/*
+FUNCTION
+       bfd_openr
+
+SYNOPSIS
+       bfd *bfd_openr (const char *filename, const char *target);
+
+DESCRIPTION
+       Open the file @var{filename} (using <<fopen>>) with the target
+       @var{target}.  Return a pointer to the created BFD.
+
+       Calls <<bfd_find_target>>, so @var{target} is interpreted as by
+       that function.
+
+       If <<NULL>> is returned then an error has occured.   Possible errors
+       are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or
+       <<system_call>> error.
+*/
+
+bfd *
+bfd_openr (const char *filename, const char *target)
+{
+  return bfd_fopen (filename, target, FOPEN_RB, -1);
+}
+
 /* Don't try to `optimize' this function:
 
    o - We lock using stack space so that interrupting the locking
@@ -212,72 +271,32 @@ DESCRIPTION
 bfd *
 bfd_fdopenr (const char *filename, const char *target, int fd)
 {
-  bfd *nbfd;
-  const bfd_target *target_vec;
+  const char *mode;
+#if defined(HAVE_FCNTL) && defined(F_GETFL)
   int fdflags;
+#endif
 
-  bfd_set_error (bfd_error_system_call);
 #if ! defined(HAVE_FCNTL) || ! defined(F_GETFL)
-  fdflags = O_RDWR;                    /* Assume full access.  */
+  mode = FOPEN_RUB; /* Assume full access.  */
 #else
   fdflags = fcntl (fd, F_GETFL, NULL);
-#endif
   if (fdflags == -1)
-    return NULL;
-
-  nbfd = _bfd_new_bfd ();
-  if (nbfd == NULL)
-    return NULL;
-
-  target_vec = bfd_find_target (target, nbfd);
-  if (target_vec == NULL)
     {
-      _bfd_delete_bfd (nbfd);
+      bfd_set_error (bfd_error_system_call);
       return NULL;
     }
 
-#ifndef HAVE_FDOPEN
-  nbfd->iostream = fopen (filename, FOPEN_RB);
-#else
   /* (O_ACCMODE) parens are to avoid Ultrix header file bug.  */
   switch (fdflags & (O_ACCMODE))
     {
-    case O_RDONLY: nbfd->iostream = fdopen (fd, FOPEN_RB);   break;
-    case O_WRONLY: nbfd->iostream = fdopen (fd, FOPEN_RUB);  break;
-    case O_RDWR:   nbfd->iostream = fdopen (fd, FOPEN_RUB);  break;
+    case O_RDONLY: mode = FOPEN_RB; break;
+    case O_WRONLY: mode = FOPEN_RUB; break;
+    case O_RDWR:   mode = FOPEN_RUB; break;
     default: abort ();
     }
 #endif
 
-  if (nbfd->iostream == NULL)
-    {
-      _bfd_delete_bfd (nbfd);
-      return NULL;
-    }
-
-  /* OK, put everything where it belongs.  */
-  nbfd->filename = filename;
-
-  /* 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.  */
-  /* (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;
-    default: abort ();
-    }
-
-  if (! bfd_cache_init (nbfd))
-    {
-      _bfd_delete_bfd (nbfd);
-      return NULL;
-    }
-  nbfd->opened_once = TRUE;
-
-  return nbfd;
+  return bfd_fopen (filename, target, mode, fd);
 }
 
 /*
@@ -405,7 +424,7 @@ static file_ptr
 opncls_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
 {
   struct opncls *vec = abfd->iostream;
-  file_ptr nread = vec->pread (abfd, vec->stream, buf, nbytes, vec->where);
+  file_ptr nread = (vec->pread) (abfd, vec->stream, buf, nbytes, vec->where);
   if (nread < 0)
     return nread;
   vec->where += nread;
@@ -428,7 +447,7 @@ opncls_bclose (struct bfd *abfd)
      free it.  */
   int status = 0;
   if (vec->close != NULL)
-    status = vec->close (abfd, vec->stream);
+    status = (vec->close) (abfd, vec->stream);
   abfd->iostream = NULL;
   return status;
 }
@@ -598,7 +617,7 @@ bfd_close (bfd *abfd)
   if (!(abfd->flags & BFD_IN_MEMORY))
     ret = abfd->iovec->bclose (abfd);
   else
-    ret = 0;
+    ret = TRUE;
 
   /* If the file was open for writing and is now executable,
      make it so.  */
@@ -820,7 +839,6 @@ DESCRIPTION
        <<abfd>> and return a pointer to it.
 */
 
-
 void *
 bfd_alloc (bfd *abfd, bfd_size_type size)
 {
@@ -838,6 +856,57 @@ bfd_alloc (bfd *abfd, bfd_size_type size)
   return ret;
 }
 
+/*
+INTERNAL_FUNCTION
+       bfd_alloc2
+
+SYNOPSIS
+       void *bfd_alloc2 (bfd *abfd, bfd_size_type nmemb, bfd_size_type size);
+
+DESCRIPTION
+       Allocate a block of @var{nmemb} elements of @var{size} bytes each
+       of memory attached to <<abfd>> and return a pointer to it.
+*/
+
+void *
+bfd_alloc2 (bfd *abfd, bfd_size_type nmemb, bfd_size_type size)
+{
+  void *ret;
+
+  if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
+      && size != 0
+      && nmemb > ~(bfd_size_type) 0 / size)
+    {
+      bfd_set_error (bfd_error_no_memory);
+      return NULL;
+    }
+
+  size *= nmemb;
+
+  if (size != (unsigned long) size)
+    {
+      bfd_set_error (bfd_error_no_memory);
+      return NULL;
+    }
+
+  ret = objalloc_alloc (abfd->memory, (unsigned long) size);
+  if (ret == NULL)
+    bfd_set_error (bfd_error_no_memory);
+  return ret;
+}
+
+/*
+INTERNAL_FUNCTION
+       bfd_zalloc
+
+SYNOPSIS
+       void *bfd_zalloc (bfd *abfd, bfd_size_type wanted);
+
+DESCRIPTION
+       Allocate a block of @var{wanted} bytes of zeroed memory
+       attached to <<abfd>> and return a pointer to it.
+*/
+
 void *
 bfd_zalloc (bfd *abfd, bfd_size_type size)
 {
@@ -849,6 +918,39 @@ bfd_zalloc (bfd *abfd, bfd_size_type size)
   return res;
 }
 
+/*
+INTERNAL_FUNCTION
+       bfd_zalloc2
+
+SYNOPSIS
+       void *bfd_zalloc2 (bfd *abfd, bfd_size_type nmemb, bfd_size_type size);
+
+DESCRIPTION
+       Allocate a block of @var{nmemb} elements of @var{size} bytes each
+       of zeroed memory attached to <<abfd>> and return a pointer to it.
+*/
+
+void *
+bfd_zalloc2 (bfd *abfd, bfd_size_type nmemb, bfd_size_type size)
+{
+  void *res;
+
+  if ((nmemb | size) >= HALF_BFD_SIZE_TYPE
+      && size != 0
+      && nmemb > ~(bfd_size_type) 0 / size)
+    {
+      bfd_set_error (bfd_error_no_memory);
+      return NULL;
+    }
+
+  size *= nmemb;
+
+  res = bfd_alloc (abfd, size);
+  if (res)
+    memset (res, 0, (size_t) size);
+  return res;
+}
+
 /* Free a block allocated for a BFD.
    Note:  Also frees all more recently allocated blocks!  */
 
@@ -859,9 +961,9 @@ bfd_release (bfd *abfd, void *block)
 }
 
 
-/* 
-   GNU Extension: separate debug-info files 
-   
+/*
+   GNU Extension: separate debug-info files
+
    The idea here is that a special section called .gnu_debuglink might be
    embedded in a binary file, which indicates that some *other* file
    contains the real debugging information. This special section contains a
@@ -889,7 +991,7 @@ DESCRIPTION
 
 RETURNS
        Return the updated CRC32 value.
-*/     
+*/
 
 unsigned long
 bfd_calc_gnu_debuglink_crc32 (unsigned long crc,
@@ -976,12 +1078,11 @@ DESCRIPTION
 static char *
 get_debug_link_info (bfd *abfd, unsigned long *crc32_out)
 {
-  asection * sect;
-  bfd_size_type debuglink_size;
+  asection *sect;
   unsigned long crc32;
-  char * contents;
+  bfd_byte *contents;
   int crc_offset;
-  bfd_boolean ret;
+  char *name;
 
   BFD_ASSERT (abfd);
   BFD_ASSERT (crc32_out);
@@ -991,27 +1092,22 @@ get_debug_link_info (bfd *abfd, unsigned long *crc32_out)
   if (sect == NULL)
     return NULL;
 
-  debuglink_size = bfd_section_size (abfd, sect);  
-
-  contents = malloc (debuglink_size);
-  if (contents == NULL)
-    return NULL;
-
-  ret = bfd_get_section_contents (abfd, sect, contents, 0, debuglink_size);
-  if (! ret)
+  if (!bfd_malloc_and_get_section (abfd, sect, &contents))
     {
-      free (contents);
+      if (contents != NULL)
+       free (contents);
       return NULL;
     }
 
   /* Crc value is stored after the filename, aligned up to 4 bytes.  */
-  crc_offset = strlen (contents) + 1;
+  name = (char *) contents;
+  crc_offset = strlen (name) + 1;
   crc_offset = (crc_offset + 3) & ~3;
 
   crc32 = bfd_get_32 (abfd, contents + crc_offset);
 
   *crc32_out = crc32;
-  return contents;
+  return name;
 }
 
 /*
@@ -1030,7 +1126,7 @@ DESCRIPTION
 static bfd_boolean
 separate_debug_file_exists (const char *name, const unsigned long crc)
 {
-  static char buffer [8 * 1024];
+  static unsigned char buffer [8 * 1024];
   unsigned long file_crc = 0;
   int fd;
   bfd_size_type count;
@@ -1100,19 +1196,19 @@ find_separate_debug_file (bfd *abfd, const char *debug_file_directory)
       return NULL;
     }
   BFD_ASSERT (strlen (dir) != 0);
-  
+
   /* Strip off filename part.  */
   for (i = strlen (dir) - 1; i >= 0; i--)
     if (IS_DIR_SEPARATOR (dir[i]))
       break;
 
   dir[i + 1] = '\0';
-  BFD_ASSERT (dir[i] == '/' || dir[0] == '\0')
+  BFD_ASSERT (dir[i] == '/' || dir[0] == '\0');
 
   debugfile = malloc (strlen (debug_file_directory) + 1
                      + strlen (dir)
                      + strlen (".debug/")
-                     + strlen (basename) 
+                     + strlen (basename)
                      + 1);
   if (debugfile == NULL)
     {
@@ -1197,10 +1293,6 @@ RETURNS
 char *
 bfd_follow_gnu_debuglink (bfd *abfd, const char *dir)
 {
-#if 0 /* Disabled until DEBUGDIR can be defined by configure.in.  */
-  if (dir == NULL)
-    dir = DEBUGDIR;
-#endif
   return find_separate_debug_file (abfd, dir);
 }
 
@@ -1219,7 +1311,7 @@ DESCRIPTION
 
 RETURNS
        A pointer to the new section is returned if all is ok.  Otherwise <<NULL>> is
-       returned and bfd_error is set.  
+       returned and bfd_error is set.
 */
 
 asection *
@@ -1236,7 +1328,7 @@ bfd_create_gnu_debuglink_section (bfd *abfd, const char *filename)
 
   /* Strip off any path components in filename.  */
   filename = lbasename (filename);
-  
+
   sect = bfd_get_section_by_name (abfd, GNU_DEBUGLINK);
   if (sect)
     {
@@ -1254,7 +1346,7 @@ bfd_create_gnu_debuglink_section (bfd *abfd, const char *filename)
     /* XXX Should we delete the section from the bfd ?  */
     return NULL;
 
-  
+
   debuglink_size = strlen (filename) + 1;
   debuglink_size += 3;
   debuglink_size &= ~3;
@@ -1263,7 +1355,7 @@ bfd_create_gnu_debuglink_section (bfd *abfd, const char *filename)
   if (! bfd_set_section_size (abfd, sect, debuglink_size))
     /* XXX Should we delete the section from the bfd ?  */
     return NULL;
-  
+
   return sect;
 }
 
@@ -1285,7 +1377,7 @@ DESCRIPTION
 
 RETURNS
        <<TRUE>> is returned if all is ok.  Otherwise <<FALSE>> is returned
-       and bfd_error is set.  
+       and bfd_error is set.
 */
 
 bfd_boolean
@@ -1298,7 +1390,7 @@ bfd_fill_in_gnu_debuglink_section (bfd *abfd,
   char * contents;
   bfd_size_type crc_offset;
   FILE * handle;
-  static char buffer[8 * 1024];
+  static unsigned char buffer[8 * 1024];
   size_t count;
 
   if (abfd == NULL || sect == NULL || filename == NULL)
@@ -1328,7 +1420,7 @@ bfd_fill_in_gnu_debuglink_section (bfd *abfd,
   /* Strip off any path components in filename,
      now that we no longer need them.  */
   filename = lbasename (filename);
-  
+
   debuglink_size = strlen (filename) + 1;
   debuglink_size += 3;
   debuglink_size &= ~3;
This page took 0.043338 seconds and 4 git commands to generate.