[PATCH] ld: Pad the previous code segment with NOPs

H.J. Lu hongjiu.lu@intel.com
Fri Jan 12 03:25:00 GMT 2018


This patch pads the previous code segment with NOPs, instead of zeros.

OK for master?

H.J.
---
	* elf.c (assign_file_positions_for_load_sections): Pad the
	previous code segment with NOPs.
---
 bfd/elf.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/bfd/elf.c b/bfd/elf.c
index 1d0eefd053..1bb91424f1 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -5319,6 +5319,7 @@ assign_file_positions_for_load_sections (bfd *abfd,
     {
       asection **secpp;
       bfd_vma off_adjust;
+      bfd_vma off_prev_end;
       bfd_boolean no_contents;
 
       /* If elf_segment_map is not from map_sections_to_segments, the
@@ -5378,6 +5379,7 @@ assign_file_positions_for_load_sections (bfd *abfd,
 
       no_contents = FALSE;
       off_adjust = 0;
+      off_prev_end = off;
       if (p->p_type == PT_LOAD
 	  && m->count > 0)
 	{
@@ -5429,6 +5431,28 @@ assign_file_positions_for_load_sections (bfd *abfd,
 	      && (off & -maxpagesize) == ((off + off_adjust) & -maxpagesize))
 	    off_adjust += maxpagesize;
 	  off += off_adjust;
+
+	  if (off_adjust != 0
+	      && j > 1
+	      && p[-1].p_filesz != 0
+	      && (p[-1].p_flags & PF_X) != 0)
+	    {
+	      /* Pad the previous code segment with NOPs.  */
+	      char *fill = abfd->arch_info->fill (off_adjust,
+						  bfd_big_endian (abfd),
+						  TRUE);
+	      if (fill == NULL
+		  || bfd_seek (abfd, off_prev_end, SEEK_SET) != 0
+		  || bfd_bwrite (fill, off_adjust, abfd) != off_adjust)
+		{
+		  _bfd_error_handler
+		    (_("%B: failed to pad code segment %d"), j - 1, abfd);
+		  return FALSE;
+		}
+
+	      free (fill);
+	    }
+
 	  if (no_contents)
 	    {
 	      /* We shouldn't need to align the segment on disk since
-- 
2.14.3



More information about the Binutils mailing list