Made sure that every call to bfd_read, bfd_write, and bfd_seek
[deliverable/binutils-gdb.git] / bfd / libbfd.c
index 0d6b5fce2b5f7afbfaf76cdac6fb99a4aa92fb08..93522d9cd3f5c0ab93e1fc070d8667569b87b7db 100644 (file)
@@ -81,6 +81,14 @@ bfd_0u (ignore)
    return 0;
 }
 
+/*ARGUSED*/
+long
+bfd_0l (ignore)
+     bfd *ignore;
+{
+  return 0;
+}
+
 /*ARGSUSED*/
 void 
 bfd_void (ignore)
@@ -94,7 +102,7 @@ _bfd_dummy_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd)
      bfd *ignore_core_bfd;
      bfd *ignore_exec_bfd;
 {
-  bfd_error = invalid_operation;
+  bfd_set_error (bfd_error_invalid_operation);
   return false;
 }
 
@@ -132,65 +140,14 @@ char *
 bfd_zmalloc (size)
      bfd_size_type size;
 {
-  char *ptr = (char *) malloc ((size_t)size);
+  char *ptr = (char *) malloc ((size_t) size);
 
-  if ((ptr != NULL) && (size != 0))
-   memset(ptr,0, (size_t) size);
+  if (ptr && size)
+   memset(ptr, 0, (size_t) size);
 
   return ptr;
 }
 #endif /* bfd_zmalloc */
-
-/*
-INTERNAL_FUNCTION
-       bfd_xmalloc
-
-SYNOPSIS
-       PTR  bfd_xmalloc (bfd_size_type size);
-
-DESCRIPTION
-       Like <<malloc>>, but exit if no more memory.
-
-*/
-
-/** There is major inconsistency in how running out of memory is handled.
-  Some routines return a NULL, and set bfd_error to no_memory.
-  However, obstack routines can't do this ... */
-
-
-PTR
-bfd_xmalloc (size)
-     bfd_size_type size;
-{
-  static CONST char no_memory_message[] = "Virtual memory exhausted!\n";
-  PTR ptr;
-  if (size == 0) size = 1;
-  ptr = (PTR)malloc((size_t) size);
-  if (!ptr)
-    {
-      write (2, no_memory_message, sizeof(no_memory_message)-1);
-      exit (1);
-    }
-  return ptr;
-}
-
-/*
-INTERNAL_FUNCTION
-       bfd_xmalloc_by_size_t
-
-SYNOPSIS
-       PTR bfd_xmalloc_by_size_t (size_t size);
-
-DESCRIPTION
-       Like <<malloc>>, but exit if no more memory.
-       Uses <<size_t>>, so it's suitable for use as <<obstack_chunk_alloc>>.
- */
-PTR
-bfd_xmalloc_by_size_t (size)
-     size_t size;
-{
-  return bfd_xmalloc ((bfd_size_type) size);
-}
 \f
 /* Some IO code */
 
@@ -213,6 +170,9 @@ real_read (where, a,b, file)
   return fread(where, a,b,file);
 }
 
+/* Return value is amount read (FIXME: how are errors and end of file dealt
+   with?  We never call bfd_set_error, which is probably a mistake).  */
+
 bfd_size_type
 bfd_read (ptr, size, nitems, abfd)
      PTR ptr;
@@ -226,6 +186,22 @@ bfd_read (ptr, size, nitems, abfd)
   if (nread > 0)
     abfd->where += nread;
 #endif
+
+  /* Set bfd_error if we did not read as much data as we expected.
+
+     If the read failed due to an error set the bfd_error_system_call,
+     else set bfd_error_file_truncated.
+
+     A BFD backend may wish to override bfd_error_file_truncated to
+     provide something more useful (eg. no_symbols or wrong_format).  */
+  if (nread < (int)(size * nitems))
+    {
+      if (ferror (bfd_cache_lookup (abfd)))
+       bfd_set_error (bfd_error_system_call);
+      else
+       bfd_set_error (bfd_error_file_truncated);
+    }
+
   return nread;
 }
 
@@ -247,7 +223,7 @@ bfd_write (ptr, size, nitems, abfd)
       if (nwrote >= 0)
        errno = ENOSPC;
 #endif
-      bfd_error = system_call_error;
+      bfd_set_error (bfd_error_system_call);
     }
   return nwrote;
 }
@@ -272,7 +248,8 @@ bfd_write_bigendian_4byte_int (abfd, i)
 {
   bfd_byte buffer[4];
   bfd_putb32(i, buffer);
-  bfd_write((PTR)buffer, 4, 1, abfd);
+  if (bfd_write((PTR)buffer, 4, 1, abfd) != 4)
+    abort ();
 }
 
 long
@@ -304,6 +281,9 @@ bfd_stat (abfd, statbuf)
   return fstat (fileno(bfd_cache_lookup(abfd)), statbuf);
 }
 
+/* Returns 0 for success, nonzero for failure (in which case bfd_get_error
+   can retrieve the error code).  */
+
 int
 bfd_seek (abfd, position, direction)
      bfd * CONST abfd;
@@ -368,7 +348,7 @@ bfd_seek (abfd, position, direction)
     {
       /* Force redetermination of `where' field.  */
       bfd_tell (abfd);
-      bfd_error = system_call_error;
+      bfd_set_error (bfd_error_system_call);
     }
   else
     {
@@ -410,7 +390,7 @@ bfd_add_to_string_table (table, new_string, table_length, free_ptr)
     base = bfd_zmalloc ((bfd_size_type) space_length);
 
     if (base == NULL) {
-      bfd_error = no_memory;
+      bfd_set_error (bfd_error_no_memory);
       return false;
     }
   }
@@ -422,7 +402,7 @@ bfd_add_to_string_table (table, new_string, table_length, free_ptr)
 
     base = (char *) realloc (base, space_length);
     if (base == NULL) {
-      bfd_error = no_memory;
+      bfd_set_error (bfd_error_no_memory);
       return false;
     }
 
@@ -866,3 +846,14 @@ bfd_log2(x)
     result++;
   return result;
 }
+
+boolean
+bfd_generic_is_local_label (abfd, sym)
+     bfd *abfd;
+     asymbol *sym;
+{
+  char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
+
+  return (sym->name[0] == locals_prefix);
+}
+
This page took 0.026941 seconds and 4 git commands to generate.