]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gas/config/obj-aout.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / gas / config / obj-aout.c
index 401b3a73cf48941315fa49877466f19a8375716d..99f879b8f974105c024c18845d3ae8a60a0a47ba 100644 (file)
@@ -1,5 +1,5 @@
 /* a.out object file format
-   Copyright (C) 1989-2015 Free Software Foundation, Inc.
+   Copyright (C) 1989-2021 Free Software Foundation, Inc.
 
    This file is part of GAS, the GNU Assembler.
 
@@ -113,22 +113,40 @@ obj_aout_frob_symbol (symbolS *sym, int *punt ATTRIBUTE_UNUSED)
            S_GET_NAME (sym));
 }
 
+/* Relocation processing may require knowing the VMAs of the sections.
+   Writing to a section will cause the BFD back end to compute the
+   VMAs.  This function also ensures that file size is large enough
+   to cover a_text and a_data should text or data be the last section
+   in the file.  */
+
 void
 obj_aout_frob_file_before_fix (void)
 {
-  /* Relocation processing may require knowing the VMAs of the sections.
-     Since writing to a section will cause the BFD back end to compute the
-     VMAs, fake it out here....  */
-  bfd_byte b = 0;
-  bfd_boolean x = TRUE;
-  if (bfd_section_size (stdoutput, text_section) != 0)
-    x = bfd_set_section_contents (stdoutput, text_section, &b, (file_ptr) 0,
-                                 (bfd_size_type) 1);
-  else if (bfd_section_size (stdoutput, data_section) != 0)
-    x = bfd_set_section_contents (stdoutput, data_section, &b, (file_ptr) 0,
-                                 (bfd_size_type) 1);
-
-  gas_assert (x);
+  asection *sec;
+  bfd_vma *sizep = NULL;
+  if ((sec = data_section)->size != 0)
+    sizep = &exec_hdr (stdoutput)->a_data;
+  else if ((sec = text_section)->size != 0)
+    sizep = &exec_hdr (stdoutput)->a_text;
+  if (sizep)
+    {
+      bfd_size_type size = sec->size;
+      bfd_byte b = 0;
+
+      gas_assert (bfd_set_section_contents (stdoutput, sec, &b, size - 1, 1));
+
+      /* We don't know the aligned size until after VMAs and sizes are
+        set on the bfd_set_section_contents call.  If that size is
+        larger than the section then write again to ensure the file
+        contents extend to cover the aligned size.  */
+      if (*sizep > size)
+       {
+         file_ptr pos = sec->filepos + *sizep;
+
+         gas_assert (bfd_seek (stdoutput, pos - 1, SEEK_SET) == 0
+                     && bfd_bwrite (&b, 1, stdoutput) == 1);
+       }
+    }
 }
 
 static void