This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] PE: don't corrupt section flags when linking from ELF objects


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/
2017-11-10  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.

include/
2017-11-10  Jan Beulich  <jbeulich@suse.com>

	* coff/pe.h (COFF_ENCODE_ALIGNMENT): Cap value to maximum one
	representable.
	(COFF_DECODE_ALIGNMENT): Define.

--- 2017-11-10/bfd/coff-tic80.c
+++ 2017-11-10/bfd/coff-tic80.c
@@ -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
--- 2017-11-10/bfd/coffcode.h
+++ 2017-11-10/bfd/coffcode.h
@@ -1855,10 +1855,6 @@ coff_set_alignment_hook (bfd * abfd ATTR
     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
--- 2017-11-10/include/coff/pe.h
+++ 2017-11-10/include/coff/pe.h
@@ -104,7 +104,10 @@
 
 /* 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.               */




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]