- bfd_byte b;
-
- b = 0;
- if (bfd_seek (abfd,
- (file_ptr) (obj_datasec (abfd)->filepos
- + exec_hdr (abfd)->a_data
- - 1),
- SEEK_SET) != 0
- || bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
- goto error_return;
+ /* The layout of a typical a.out file is header, text, data,
+ relocs, symbols, string table. When there are no relocs,
+ symbols or string table, the last thing in the file is data
+ and a_data may be rounded up. However we may have a smaller
+ sized .data section and thus not written final padding. The
+ same thing can happen with text if there is no data. Write
+ final padding here to extend the file. */
+ file_ptr pos = 0;
+
+ if (exec_hdr (abfd)->a_data > obj_datasec (abfd)->size)
+ pos = obj_datasec (abfd)->filepos + exec_hdr (abfd)->a_data;
+ else if (obj_datasec (abfd)->size == 0
+ && exec_hdr (abfd)->a_text > obj_textsec (abfd)->size)
+ pos = obj_textsec (abfd)->filepos + exec_hdr (abfd)->a_text;
+ if (pos != 0)
+ {
+ bfd_byte b = 0;
+
+ if (bfd_seek (abfd, pos - 1, SEEK_SET) != 0
+ || bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
+ goto error_return;
+ }