PR27567, Linking PE files adds alignment section flags to executables
authorAlan Modra <amodra@gmail.com>
Fri, 16 Apr 2021 12:59:05 +0000 (22:29 +0930)
committerAlan Modra <amodra@gmail.com>
Fri, 16 Apr 2021 13:51:44 +0000 (23:21 +0930)
So don't set those flags for an executable.  In the patch I also test
DYNAMIC even though the PE bfd code doesn't appear to set it for dlls.
I figure it doesn't hurt to include that flag too.

PR 27567
bfd/
* coffcode.h (styp_to_sec_flags): Use an unsigned long styp_flags.
(coff_write_object_contents): Pass bfd to COFF_ENCODE_ALIGNMENT,
ignore alignment checks when return is false.  Formatting.
include/
* coff/internal.h (struct internal_scnhdr): Make s_flags unsigned long.
* coff/pe.h (COFF_ENCODE_ALIGNMENT): Don't set align flags for an
executable and return false.  Do so for a relocatable object and
evaluate to true.
* coff/ti.h (COFF_ENCODE_ALIGNMENT): Add bfd arg and evaluate to true.
(COFF_DECODE_ALIGNMENT): Formatting.
* coff/z80.h (COFF_ENCODE_ALIGNMENT): Similarly.
(COFF_DECODE_ALIGNMENT): Similarly.

bfd/ChangeLog
bfd/coffcode.h
include/ChangeLog
include/coff/internal.h
include/coff/pe.h
include/coff/ti.h
include/coff/z80.h

index 702d4026ee88af567d2cd8f2d446f7abb08991a1..513d6e9d3f3a06aa176b62d35a8cd51157951758 100644 (file)
@@ -1,3 +1,10 @@
+2021-04-16  Alan Modra  <amodra@gmail.com>
+
+       PR 27567
+       * coffcode.h (styp_to_sec_flags): Use an unsigned long styp_flags.
+       (coff_write_object_contents): Pass bfd to COFF_ENCODE_ALIGNMENT,
+       ignore alignment checks when return is false.  Formatting.
+
 2021-04-15  Nelson Chu  <nelson.chu@sifive.com>
 
        PR27584
index 8006baa25952b87c13fd86c9ed8946d3894105a4..f65f3352e4666c42e1701913613abaa75cd333cf 100644 (file)
@@ -737,7 +737,7 @@ styp_to_sec_flags (bfd *abfd,
                   flagword *flags_ptr)
 {
   struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
-  long styp_flags = internal_s->s_flags;
+  unsigned long styp_flags = internal_s->s_flags;
   flagword sec_flags = 0;
 
 #ifdef STYP_BLOCK
@@ -3643,18 +3643,18 @@ coff_write_object_contents (bfd * abfd)
 
 
 #ifdef COFF_ENCODE_ALIGNMENT
-      COFF_ENCODE_ALIGNMENT(section, current->alignment_power);
-      if ((unsigned int)COFF_DECODE_ALIGNMENT(section.s_flags)
-         != current->alignment_power)
+      if (COFF_ENCODE_ALIGNMENT (abfd, section, current->alignment_power)
+         && (COFF_DECODE_ALIGNMENT (section.s_flags)
+             != current->alignment_power))
        {
-         bool warn = coff_data (abfd)->link_info
-           && !bfd_link_relocatable (coff_data (abfd)->link_info);
+         bool warn = (coff_data (abfd)->link_info
+                      && !bfd_link_relocatable (coff_data (abfd)->link_info));
 
          _bfd_error_handler
            /* xgettext:c-format */
            (_("%pB:%s section %s: alignment 2**%u not representable"),
-           abfd, warn ? " warning:" : "", current->name,
-           current->alignment_power);
+            abfd, warn ? " warning:" : "", current->name,
+            current->alignment_power);
          if (!warn)
            {
              bfd_set_error (bfd_error_nonrepresentable_section);
index 752b8fec35f30f76624fe32dcbaa7c4eb2b25209..83ae2cb84cc75a7aebae23ce5b897e7b761b467e 100644 (file)
@@ -1,3 +1,15 @@
+2021-04-16  Alan Modra  <amodra@gmail.com>
+
+       PR 27567
+       * coff/internal.h (struct internal_scnhdr): Make s_flags unsigned long.
+       * coff/pe.h (COFF_ENCODE_ALIGNMENT): Don't set align flags for an
+       executable and return false.  Do so for a relocatable object and
+       evaluate to true.
+       * coff/ti.h (COFF_ENCODE_ALIGNMENT): Add bfd arg and evaluate to true.
+       (COFF_DECODE_ALIGNMENT): Formatting.
+       * coff/z80.h (COFF_ENCODE_ALIGNMENT): Similarly.
+       (COFF_DECODE_ALIGNMENT): Similarly.
+
 2021-04-09  Alan Modra  <amodra@gmail.com>
 
        * dis-asm.h (struct disassemble_info): Add dynrelbuf and dynrelcount.
index 90c901d29873c81992aa1fabc14215085c1bda10..b09bb0f469dc49f2c598d216e1682f4e5e1e2ddf 100644 (file)
@@ -407,7 +407,7 @@ struct internal_scnhdr
   bfd_vma s_lnnoptr;           /* file ptr to line numbers     */
   unsigned long s_nreloc;      /* number of relocation entries */
   unsigned long s_nlnno;       /* number of line number entries*/
-  long s_flags;                        /* flags                        */
+  unsigned long s_flags;       /* flags                        */
   unsigned char s_page;         /* TI COFF load page            */
 };
 
index a157812ef7f1be882b68417eda2c0ae6da49a98e..d6e84691677f1337ec931cdb62cd4470472a22bf 100644 (file)
 #define IMAGE_SCN_ALIGN_8192BYTES           IMAGE_SCN_ALIGN_POWER_CONST (13)
 
 /* Encode alignment power into IMAGE_SCN_ALIGN bits of s_flags.  */
-#define COFF_ENCODE_ALIGNMENT(SECTION, ALIGNMENT_POWER) \
-  ((SECTION).s_flags |= IMAGE_SCN_ALIGN_POWER_CONST ((ALIGNMENT_POWER) <= 13 \
-                                                    ? (ALIGNMENT_POWER) : 13))
+#define COFF_ENCODE_ALIGNMENT(ABFD, SECTION, ALIGNMENT_POWER) \
+  (((ABFD)->flags & (EXEC_P | DYNAMIC)) != 0 ? false                   \
+   : ((SECTION).s_flags                                                        \
+      |= IMAGE_SCN_ALIGN_POWER_CONST ((ALIGNMENT_POWER) < 13           \
+                                     ? (ALIGNMENT_POWER) : 13),        \
+      true))
 #define COFF_DECODE_ALIGNMENT(X)             \
   IMAGE_SCN_ALIGN_POWER_NUM ((X) & IMAGE_SCN_ALIGN_POWER_BIT_MASK)
 
index 5d242d12c247de9c4e3808fdf6f3a7d3767b9d80..e51cf96bff9b51d1732e58b42a0eef821aac7208 100644 (file)
@@ -88,9 +88,10 @@ struct external_filehdr
 #define COFF_ALIGN_IN_SECTION_HEADER 1
 #define COFF_ALIGN_IN_S_FLAGS 1
 /* requires a power-of-two argument */
-#define COFF_ENCODE_ALIGNMENT(S,X) ((S).s_flags |= (((unsigned)(X)&0xF)<<8))
+#define COFF_ENCODE_ALIGNMENT(B,S,X) \
+  ((S).s_flags |= (((unsigned) (X) & 0xF) << 8), true)
 /* result is a power of two */
-#define COFF_DECODE_ALIGNMENT(X) (((X)>>8)&0xF)
+#define COFF_DECODE_ALIGNMENT(X) (((X) >> 8) & 0xF)
 
 #define COFF0_P(ABFD) (bfd_coff_filhsz(ABFD) == FILHSZ_V0)
 #define COFF2_P(ABFD) (bfd_coff_scnhsz(ABFD) != SCNHSZ_V01)
index 1ba8e45fea242877cbf5be0f090a7678b2ea482d..1fd94fe5145d25498c2004624d512b3f258182c9 100644 (file)
 #define COFF_ALIGN_IN_S_FLAGS 1
 #define F_ALGNMASK 0x0F00
 /* requires a power-of-two argument */
-#define COFF_ENCODE_ALIGNMENT(S,X) ((S).s_flags |= (((unsigned)(X)&0xF)<<8))
+#define COFF_ENCODE_ALIGNMENT(B,S,X) \
+  ((S).s_flags |= (((unsigned) (X) & 0xF) << 8), true)
 /* result is a power of two */
-#define COFF_DECODE_ALIGNMENT(X) (((X)>>8)&0xF)
+#define COFF_DECODE_ALIGNMENT(X) (((X) >> 8) & 0xF)
 
 #define        Z80MAGIC   0x805A