public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* RFC: Objcopy: Section alignment
@ 2024-03-21 14:55 Nick Clifton
  2024-03-21 16:59 ` Jan Beulich
  0 siblings, 1 reply; 8+ messages in thread
From: Nick Clifton @ 2024-03-21 14:55 UTC (permalink / raw)
  To: binutils

Hi Guys,

  It was recently pointed out to me that objcopy's --section-alignment
  option does not actually set the alignment of any sections.  It just
  sets a field in the PE file format's optional header.  This is rather
  confusing for the user.

  Plus it turns out that even if a section's alignment is set via the
  --set=section-alignment option, its LMA and VMA will not be changed to
  match the new alignment.

  So I am proposing the patch below to fix these two things.  But I have
  a nagging feeling that there is something that I have missed, so I
  wonder what you guys think.  Any comments ?

Cheers
  Nick

diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi
index 50cc4707e14..56db97d494b 100644
--- a/binutils/doc/binutils.texi
+++ b/binutils/doc/binutils.texi
@@ -1766,7 +1766,13 @@ SHF_X86_64_LARGE.
 @item --set-section-alignment @var{sectionpattern}=@var{align}
 Set the alignment for any sections matching @var{sectionpattern}.
 @var{align} specifies the alignment in bytes and must be a power of
-two, i.e. 1, 2, 4, 8@dots{}. 
+two, i.e. 1, 2, 4, 8@dots{}.
+
+In addition, for any section matching @var{sectionpattern} its LMA and
+VMA will be increased if this is needed to ensure that it starts on an
+@var{align} boundary.  The exception to this rule is if the section's
+LMA or VMA are being altered explicitly as the result of another
+command line option.
 
 @item --add-section @var{sectionname}=@var{filename}
 Add a new section named @var{sectionname} while copying the file.  The
@@ -2132,6 +2138,11 @@ for dlls.
 Sets the section alignment field in the PE header.  Sections in memory
 will always begin at addresses which are a multiple of this number.
 Defaults to 0x1000.
+
+Also ensures that all sections in the output file are aligned to at
+least @var{num} bytes, unless their LMA or VMA address have been
+explicitly set.
+
 [This option is specific to PE targets.]
 
 @item --stack @var{reserve}
diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index a8e0f156f3e..ccf34c09ceb 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -4100,6 +4100,24 @@ setup_bfd_headers (bfd *ibfd, bfd *obfd)
   return;
 }
 
+static inline signed int
+power_of_two (bfd_vma val)
+{
+  unsigned int result = 0;
+
+  while ((val & 1) == 0)
+    {
+      val >>= 1;
+      ++result;
+    }
+
+  if (val != 1)
+    /* Number has more than one 1, i.e. wasn't a power of 2.  */
+    return -1;
+
+  return result;
+}
+
 /* Create a section in OBFD with the same
    name and attributes as ISECTION in IBFD.  */
 
@@ -4259,6 +4277,10 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
 			 SECTION_CONTEXT_SET_ALIGNMENT);
   if (p != NULL)
     alignment = p->alignment;
+  else if (pe_section_alignment != (bfd_vma) -1
+	   && bfd_get_flavour (obfd) == bfd_target_coff_flavour
+	   && bfd_pei_p (obfd))
+    alignment = power_of_two (pe_section_alignment);
   else
     alignment = bfd_section_alignment (isection);
 
@@ -4267,6 +4289,30 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
   if (!bfd_set_section_alignment (osection, alignment))
     err = _("failed to set alignment");
 
+  /* If the VMA is not aligned and it was not set by the user
+     then adjust it.  */
+  if (osection->vma & ((1 << alignment) - 1)
+      && change_section_address == 0
+      && find_section_list (bfd_section_name (isection), false,
+			    SECTION_CONTEXT_ALTER_VMA
+			    | SECTION_CONTEXT_SET_VMA) == NULL)
+    {
+      osection->vma += (1 << alignment);
+      osection->vma &= ~ ((1 << alignment) -1);
+    }
+
+  /* If the LMA is not aligned and it was not set by the user
+     then adjust it.  */
+  if (osection->lma & ((1 << alignment) - 1)
+      && change_section_address == 0
+      && find_section_list (bfd_section_name (isection), false,
+			    SECTION_CONTEXT_ALTER_LMA
+			    | SECTION_CONTEXT_SET_LMA) == NULL)
+    {
+      osection->lma += (1 << alignment);
+      osection->lma &= ~ ((1 << alignment) -1);
+    }
+
   /* Copy merge entity size.  */
   osection->entsize = isection->entsize;
 
@@ -5713,15 +5759,8 @@ copy_main (int argc, char *argv[])
 	      fatal (_("bad format for --set-section-alignment: numeric argument needed"));
 
 	    /* Convert integer alignment into a power-of-two alignment.  */
-	    palign = 0;
-	    while ((align & 1) == 0)
-	      {
-	    	align >>= 1;
-	    	++palign;
-	      }
-
-	    if (align != 1)
-	      /* Number has more than on 1, i.e. wasn't a power of 2.  */
+	    palign = power_of_two (align);
+	    if (palign == -1)
 	      fatal (_("bad format for --set-section-alignment: alignment is not a power of two"));
 
 	    /* Add the alignment setting to the section list.  */
@@ -5938,6 +5977,11 @@ copy_main (int argc, char *argv[])
 	case OPTION_PE_SECTION_ALIGNMENT:
 	  pe_section_alignment = parse_vma (optarg,
 					    "--section-alignment");
+	  if (power_of_two (pe_section_alignment) == -1)
+	    {
+	      non_fatal (_("pe_section_alignment argument is not a power of two: %s - ignoring"), optarg);
+	      pe_section_alignment = (bfd_vma) -1;
+	    }
 	  break;
 
 	case OPTION_SUBSYSTEM:


  


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-04-02  9:19 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-21 14:55 RFC: Objcopy: Section alignment Nick Clifton
2024-03-21 16:59 ` Jan Beulich
2024-03-26 12:44   ` Nick Clifton
2024-03-26 13:08     ` Jan Beulich
2024-03-28 13:08       ` Nick Clifton
2024-03-28 16:57         ` Jan Beulich
2024-04-02  8:45           ` Nick Clifton
2024-04-02  9:19             ` Jan Beulich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).