This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
[PATCH/PE-COFF] Encode required alignment into IMAGE_SCN_ALIGN bits of object s_flags - Take 3
- From: "Danny Smith" <dansmister at gmail dot com>
- To: binutils at sourceware dot org
- Date: Mon, 5 Nov 2007 10:55:03 +1300
- Subject: [PATCH/PE-COFF] Encode required alignment into IMAGE_SCN_ALIGN bits of object s_flags - Take 3
This is a revised version of the patch I submitted earlier.
http://sourceware.org/ml/binutils/2007-10/msg00294.html
The prior patch failed to check if the alignment power fell within valid
range for PE-COFF target,
To recap the earlier message:
Given an alignment >16-bytes, eg
.file "foo.c"
.data
.globl _x
.align 32
_x:
.long 1065353216
.space 28
gas for PE-COFF targets fails to set section alignment flags correctly,
but always uses default. So
as foo.s | objdump -x gives
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 00000000 00000000 00000000 00000000 2**4
CONTENTS, ALLOC, LOAD, READONLY, CODE
1 .data 00000020 00000000 00000000 0000008c 2**4
ALLOC, LOAD, DATA
2 .bss 00000000 00000000 00000000 00000000 2**4
ALLOC
See also the thread
Re: Fwd: ALIGNOF fails on cygwin at:
http://sourceware.org/ml/binutils/2007-07/msg00014.html
The attached fixs by encoding the section alignment_power into the
appropriate bits of the IMAGE_SCN_ALIGN s_flags using the macro
COFF_ENCODE_ALIGNMENT, defined in coff/pe.h.
Since, now we can pad only as needed, the hardcoded
COFF_SECTION_ALIGNMENT_ENTRIES bfd/pe[i]-i386.c can revert back to the
default.
After this patch we get
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 00000000 00000000 00000000 00000000 2**2
ALLOC, LOAD, READONLY, CODE
1 .data 00000020 00000000 00000000 0000008c 2**5
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000000 00000000 00000000 00000000 2**2
ALLOC
In addition, it modifies gas/read.c to enable non-default alignment
limits to be checked. The appropriate p2align limit for i386-pe is
defined for TE_PE in config/tc-i386.h as 13, corresponding to the
largest documented PE section alignment flag,IMAGE_SCN_ALIGN_8192BYTE.
No regressions after in-tree combined build of mingw-targeted
binutils/gcc.
The two testcases in ld-scripts/align.exp and alignof.exp now pass
include/ChangeLog
2007-10-24 Danny Smith <dannysmith@users.sourceforge.net>
* coff/pe.h (COFF_ENCODE_ALIGNMENT) Define.
gas/ChangeLog
* read.c (ALIGN_LIMIT): Rename to ...
(TC_ALIGN_LIMIT): Guard against prior definition.
* config/tc-i386.h (TC_ALIGN_LIMIT)[TE_PE]: Define.
bfd/ChangeLog
* pe-i386.c (COFF_SECTION_ALIGNMENT_ENTRIES): Let .data, .text
and .bss section use the default.
* pei-i386.c (COFF_SECTION_ALIGNMENT_ENTRIES): Likewise.
ld/testsuite/ChangeLog
* ld-scripts/align.exp: Enable for PECOFF.
* ld-scripts/alignof.exp: Likewise.
2007-07-26 H.J. Lu <hongjiu.lu@intel.com>
* coffcode.h (ALIGN_SET): Removed.
(ELIFALIGN_SET): Likewise.
(coff_set_alignment_hook): Handle IMAGE_SCN_ALIGN_128BYTES,
IMAGE_SCN_ALIGN_256BYTES, IMAGE_SCN_ALIGN_512BYTES,
IMAGE_SCN_ALIGN_1024BYTES, IMAGE_SCN_ALIGN_2048BYTES,
IMAGE_SCN_ALIGN_4096BYTES and IMAGE_SCN_ALIGN_8192BYTES.
include/coff/
2007-07-26 H.J. Lu <hongjiu.lu@intel.com>
* pe.h (IMAGE_SCN_ALIGN_128BYTES): New.
(IMAGE_SCN_ALIGN_256BYTES): Likewise.
(IMAGE_SCN_ALIGN_512BYTES): Likewise.
(IMAGE_SCN_ALIGN_1024BYTES): Likewise.
(IMAGE_SCN_ALIGN_2048BYTES): Likewise.
(IMAGE_SCN_ALIGN_4096BYTES): Likewise.
(IMAGE_SCN_ALIGN_8192BYTES): Likewise.
--- binutils/bfd/coffcode.h.align 2007-07-25 14:34:19.000000000 -0700
+++ binutils/bfd/coffcode.h 2007-07-26 14:01:29.000000000 -0700
@@ -1627,19 +1627,6 @@ coff_set_alignment_hook (bfd * abfd ATTR
#else /* ! COFF_ALIGN_IN_SECTION_HEADER */
#ifdef COFF_WITH_PE
-/* A couple of macros to help setting the alignment power field. */
-#define ALIGN_SET(field, x, y) \
- if (((field) & IMAGE_SCN_ALIGN_64BYTES) == x)\
- {\
- section->alignment_power = y;\
- }
-
-#define ELIFALIGN_SET(field, x, y) \
- else if (((field) & IMAGE_SCN_ALIGN_64BYTES) == x) \
- {\
- section->alignment_power = y;\
- }
-
static void
coff_set_alignment_hook (bfd * abfd ATTRIBUTE_UNUSED,
asection * section,
@@ -1647,14 +1634,29 @@ coff_set_alignment_hook (bfd * abfd ATTR
{
struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
bfd_size_type amt;
+ unsigned int alignment_power = hdr->s_flags & 0x00f00000;
- ALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_64BYTES, 6)
- ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_32BYTES, 5)
- ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_16BYTES, 4)
- ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_8BYTES, 3)
- ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_4BYTES, 2)
- ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_2BYTES, 1)
- ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_1BYTES, 0)
+ switch (alignment_power)
+ {
+ case IMAGE_SCN_ALIGN_8192BYTES:
+ case IMAGE_SCN_ALIGN_4096BYTES:
+ case IMAGE_SCN_ALIGN_2048BYTES:
+ case IMAGE_SCN_ALIGN_1024BYTES:
+ case IMAGE_SCN_ALIGN_512BYTES:
+ case IMAGE_SCN_ALIGN_256BYTES:
+ case IMAGE_SCN_ALIGN_128BYTES:
+ case IMAGE_SCN_ALIGN_64BYTES:
+ case IMAGE_SCN_ALIGN_32BYTES:
+ case IMAGE_SCN_ALIGN_16BYTES:
+ case IMAGE_SCN_ALIGN_8BYTES:
+ case IMAGE_SCN_ALIGN_4BYTES:
+ case IMAGE_SCN_ALIGN_2BYTES:
+ case IMAGE_SCN_ALIGN_1BYTES:
+ section->alignment_power = (alignment_power >> 20) - 1;
+ break;
+ default:
+ break;
+ }
/* In a PE image file, the s_paddr field holds the virtual size of a
section, while the s_size field holds the raw size. We also keep
--- binutils/include/coff/pe.h.align 2007-07-24 14:01:56.000000000 -0700
+++ binutils/include/coff/pe.h 2007-07-25 15:02:42.000000000 -0700
@@ -73,6 +73,13 @@
#define IMAGE_SCN_ALIGN_16BYTES 0x00500000 /* Default alignment if no others are specified. */
#define IMAGE_SCN_ALIGN_32BYTES 0x00600000
#define IMAGE_SCN_ALIGN_64BYTES 0x00700000
+#define IMAGE_SCN_ALIGN_128BYTES 0x00800000
+#define IMAGE_SCN_ALIGN_256BYTES 0x00900000
+#define IMAGE_SCN_ALIGN_512BYTES 0x00a00000
+#define IMAGE_SCN_ALIGN_1024BYTES 0x00b00000
+#define IMAGE_SCN_ALIGN_2048BYTES 0x00c00000
+#define IMAGE_SCN_ALIGN_4096BYTES 0x00d00000
+#define IMAGE_SCN_ALIGN_8192BYTES 0x00e00000
#define IMAGE_SCN_LNK_NRELOC_OVFL 0x01000000 /* Section contains extended relocations. */
#define IMAGE_SCN_MEM_NOT_CACHED 0x04000000 /* Section is not cachable. */