PE: don't corrupt section flags when linking from ELF objects
authorJan Beulich <jbeulich@novell.com>
Mon, 13 Nov 2017 11:26:12 +0000 (12:26 +0100)
committerJan Beulich <jbeulich@suse.com>
Mon, 13 Nov 2017 11:26:12 +0000 (12:26 +0100)
Linking EFI executables from ELF object files can result in corrupted
COFF section flags if the section's alignment is too high. Issue a
diagnostic in that case, erroring out if this is not a final link, and
make sure only in-range values get written to the output image.

While doing this also make tic80 use the generic alignment macros
instead of custom #ifdef-ary.

No testsuite regressions for the range of COFF/PE targets that actually
cross-build in the first place on x86-64-linux.

bfd/ChangeLog
bfd/coff-tic80.c
bfd/coffcode.h
include/ChangeLog
include/coff/pe.h

index d1b995a67f3f98bce88b9c42a04ea91bbad47e32..70d4e56fedf2b127a8ab06b2a4503b25a3711e32 100644 (file)
@@ -1,3 +1,11 @@
+2017-11-13  Jan Beulich  <jbeulich@suse.com>
+
+       * coff-tic80.c (COFF_ENCODE_ALIGNMENT, COFF_DECODE_ALIGNMENT):
+       Define.
+       * coffcode.h (coff_set_alignment_hook): Drop tic80 special case.
+       (coff_write_object_contents): Likewise. Issue diagnostic for too
+       large alignment.
+
 2017-11-12  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR ld/22423
index 253ea94e87818c18c80a6085546be7eb3114092d..d23a6f47edb970d7681f7fcf0580a5b3c946d9bb 100644 (file)
@@ -42,6 +42,8 @@
 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
 #define COFF_ALIGN_IN_SECTION_HEADER 1
 #define COFF_ALIGN_IN_SFLAGS 1
+#define COFF_ENCODE_ALIGNMENT(S,X) ((S).s_flags |= (((unsigned)(X) & 0xf) << 8))
+#define COFF_DECODE_ALIGNMENT(X) (((X) >> 8) & 0xf)
 
 #define GET_SCNHDR_FLAGS H_GET_16
 #define PUT_SCNHDR_FLAGS H_PUT_16
index 6da0afa26d29dd27dc1fc741467205c00fbd36e3..604ba6d8d91784136f925feeee73200b17242305 100644 (file)
@@ -1855,10 +1855,6 @@ coff_set_alignment_hook (bfd * abfd ATTRIBUTE_UNUSED,
     if ((1 << i) >= hdr->s_align)
       break;
 #endif
-#ifdef TIC80COFF
-  /* TI tools puts the alignment power in bits 8-11.  */
-  i = (hdr->s_flags >> 8) & 0xF ;
-#endif
 #ifdef COFF_DECODE_ALIGNMENT
   i = COFF_DECODE_ALIGNMENT(hdr->s_flags);
 #endif
@@ -3865,12 +3861,25 @@ coff_write_object_contents (bfd * abfd)
                         ? 1 << current->alignment_power
                         : 0);
 #endif
-#ifdef TIC80COFF
-      /* TI COFF puts the alignment power in bits 8-11 of the flags.  */
-      section.s_flags |= (current->alignment_power & 0xF) << 8;
-#endif
 #ifdef COFF_ENCODE_ALIGNMENT
       COFF_ENCODE_ALIGNMENT(section, current->alignment_power);
+      if ((unsigned int)COFF_DECODE_ALIGNMENT(section.s_flags)
+         != current->alignment_power)
+       {
+         bfd_boolean warn = coff_data (abfd)->link_info
+           && !bfd_link_relocatable (coff_data (abfd)->link_info);
+
+         _bfd_error_handler
+           /* xgettext:c-format */
+           (_("%B:%s section %s: alignment 2**%u not representable"),
+           abfd, warn ? " warning:" : "", current->name,
+            current->alignment_power);
+         if (!warn)
+           {
+             bfd_set_error (bfd_error_nonrepresentable_section);
+             return FALSE;
+           }  
+       }
 #endif
 
 #ifdef COFF_IMAGE_WITH_PE
index dc320cb019be538b720b9e49236727317cf2b1d7..a6e3ebcb973bdd4ffe6d72dc76663981edfd92b3 100644 (file)
@@ -1,3 +1,9 @@
+2017-11-13  Jan Beulich  <jbeulich@suse.com>
+
+       * coff/pe.h (COFF_ENCODE_ALIGNMENT): Cap value to maximum one
+       representable.
+       (COFF_DECODE_ALIGNMENT): Define.
+
 2017-11-09  Tamar Christina  <tamar.christina@arm.com>
 
        * opcode/aarch64.h (AARCH64_ARCH_V8_4): Enable DOTPROD.
index f51ceedca006daee43344f4dbab01c83160dfd24..5239916e6046e0b44b18886b241e3317054ad591 100644 (file)
 
 /* 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)))
+  ((SECTION).s_flags |= IMAGE_SCN_ALIGN_POWER_CONST ((ALIGNMENT_POWER) <= 13 \
+                                                    ? (ALIGNMENT_POWER) : 13))
+#define COFF_DECODE_ALIGNMENT(X)             \
+  IMAGE_SCN_ALIGN_POWER_NUM ((X) & IMAGE_SCN_ALIGN_POWER_BIT_MASK)
 
 #define IMAGE_SCN_LNK_NRELOC_OVFL            0x01000000  /* Section contains extended relocations. */
 #define IMAGE_SCN_MEM_NOT_CACHED             0x04000000  /* Section is not cachable.               */