]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - bfd/elfcode.h
Re: MSP430: Support relocations for subtract expressions in .uleb128 directives
[thirdparty/binutils-gdb.git] / bfd / elfcode.h
index 7745c537658fc41a962cf5d195ac15566adcfec4..606ff64fd4d785d25748ceced76a6e0f3120c981 100644 (file)
@@ -321,11 +321,14 @@ elf_swap_shdr_in (bfd *abfd,
     {
       ufile_ptr filesize = bfd_get_file_size (abfd);
 
-      if (filesize != 0 && dst->sh_size > filesize)
-       _bfd_error_handler
-         (_("warning: %pB has a corrupt section with a size (%"
-            BFD_VMA_FMT "x) larger than the file size"),
-          abfd, dst->sh_size);
+      if (filesize != 0
+         && ((ufile_ptr) dst->sh_offset > filesize
+             || dst->sh_size > filesize - dst->sh_offset))
+       {
+         abfd->read_only = 1;
+         _bfd_error_handler (_("warning: %pB has a section "
+                               "extending past end of file"), abfd);
+       }
     }
   dst->sh_link = H_GET_32 (abfd, src->sh_link);
   dst->sh_info = H_GET_32 (abfd, src->sh_info);
@@ -568,7 +571,7 @@ elf_object_p (bfd *abfd)
 
   /* If this is a relocatable file and there is no section header
      table, then we're hosed.  */
-  if (i_ehdrp->e_shoff == 0 && i_ehdrp->e_type == ET_REL)
+  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_type == ET_REL)
     goto got_wrong_format_error;
 
   /* As a simple sanity check, verify that what BFD thinks is the
@@ -578,7 +581,7 @@ elf_object_p (bfd *abfd)
     goto got_wrong_format_error;
 
   /* Further sanity check.  */
-  if (i_ehdrp->e_shoff == 0 && i_ehdrp->e_shnum != 0)
+  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_shnum != 0)
     goto got_wrong_format_error;
 
   ebd = get_elf_backend_data (abfd);
@@ -615,7 +618,7 @@ elf_object_p (bfd *abfd)
       && ebd->elf_osabi != ELFOSABI_NONE)
     goto got_wrong_format_error;
 
-  if (i_ehdrp->e_shoff != 0)
+  if (i_ehdrp->e_shoff >= sizeof (x_ehdr))
     {
       file_ptr where = (file_ptr) i_ehdrp->e_shoff;
 
@@ -764,6 +767,7 @@ elf_object_p (bfd *abfd)
             So we are kind, and reset the string index value to 0
             so that at least some processing can be done.  */
          i_ehdrp->e_shstrndx = SHN_UNDEF;
+         abfd->read_only = 1;
          _bfd_error_handler
            (_("warning: %pB has a corrupt string table index - ignoring"),
             abfd);
@@ -804,10 +808,18 @@ elf_object_p (bfd *abfd)
          if (bfd_bread (&x_phdr, sizeof x_phdr, abfd) != sizeof x_phdr)
            goto got_no_match;
          elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
+         /* Too much code in BFD relies on alignment being a power of
+            two, as required by the ELF spec.  */
+         if (i_phdr->p_align != (i_phdr->p_align & -i_phdr->p_align))
+           {
+             abfd->read_only = 1;
+             _bfd_error_handler (_("warning: %pB has a program header "
+                                   "with invalid alignment"), abfd);
+           }
        }
     }
 
-  if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff != 0)
+  if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff >= sizeof (x_ehdr))
     {
       unsigned int num_sec;
 
@@ -987,12 +999,12 @@ elf_write_relocs (bfd *abfd, asection *sec, void *data)
       (*swap_out) (abfd, &src_rela, dst_rela);
     }
 
-  if (bed->write_secondary_relocs != NULL)
-    if (! bed->write_secondary_relocs (abfd, sec))
-      {
-       *failedp = TRUE;
-       return;
-      }
+  if (elf_section_data (sec)->has_secondary_relocs
+      && !bed->write_secondary_relocs (abfd, sec))
+    {
+      *failedp = TRUE;
+      return;
+    }
 }
 
 /* Write out the program headers.  */
@@ -1145,8 +1157,7 @@ elf_checksum_contents (bfd *abfd,
       if (contents != NULL)
        {
          (*process) (contents, i_shdr.sh_size, arg);
-         if (free_contents != NULL)
-           free (free_contents);
+         free (free_contents);
        }
     }
 
@@ -1403,16 +1414,14 @@ elf_slurp_symbol_table (bfd *abfd, asymbol **symptrs, bfd_boolean dynamic)
       *symptrs = 0;            /* Final null pointer */
     }
 
-  if (xverbuf != NULL)
-    free (xverbuf);
-  if (isymbuf != NULL && hdr->contents != (unsigned char *) isymbuf)
+  free (xverbuf);
+  if (hdr->contents != (unsigned char *) isymbuf)
     free (isymbuf);
   return symcount;
 
  error_return:
-  if (xverbuf != NULL)
-    free (xverbuf);
-  if (isymbuf != NULL && hdr->contents != (unsigned char *) isymbuf)
+  free (xverbuf);
+  if (hdr->contents != (unsigned char *) isymbuf)
     free (isymbuf);
   return -1;
 }
@@ -1510,13 +1519,11 @@ elf_slurp_reloc_table_from_section (bfd *abfd,
        goto error_return;
     }
 
-  if (allocated != NULL)
-    free (allocated);
+  free (allocated);
   return TRUE;
 
  error_return:
-  if (allocated != NULL)
-    free (allocated);
+  free (allocated);
   return FALSE;
 }
 
@@ -1596,8 +1603,7 @@ elf_slurp_reloc_table (bfd *abfd,
                                              symbols, dynamic))
     return FALSE;
 
-  if (bed->slurp_secondary_relocs != NULL
-      && ! bed->slurp_secondary_relocs (abfd, asect, symbols))
+  if (!bed->slurp_secondary_relocs (abfd, asect, symbols))
     return FALSE;
 
   asect->relocation = relents;
@@ -1682,7 +1688,6 @@ NAME(_bfd_elf,bfd_from_remote_memory)
   bfd_vma high_offset;
   bfd_vma shdr_end;
   bfd_vma loadbase;  /* Bytes.  */
-  char *filename;
   size_t amt;
   unsigned int opb = bfd_octets_per_byte (templ, NULL);
 
@@ -1896,22 +1901,14 @@ NAME(_bfd_elf,bfd_from_remote_memory)
       free (contents);
       return NULL;
     }
-  filename = bfd_strdup ("<in-memory>");
-  if (filename == NULL)
-    {
-      free (bim);
-      free (contents);
-      return NULL;
-    }
   nbfd = _bfd_new_bfd ();
-  if (nbfd == NULL)
+  if (nbfd == NULL
+      || !bfd_set_filename (nbfd, "<in-memory>"))
     {
-      free (filename);
       free (bim);
       free (contents);
       return NULL;
     }
-  nbfd->filename = filename;
   nbfd->xvec = templ->xvec;
   bim->size = high_offset;
   bim->buffer = contents;