public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH V2] GAS fix alignment for aarch64-pe
@ 2022-11-14 17:24 Zac Walker
  2022-11-15 10:50 ` Nick Clifton
  0 siblings, 1 reply; 9+ messages in thread
From: Zac Walker @ 2022-11-14 17:24 UTC (permalink / raw)
  To: binutils

[-- Attachment #1: Type: text/plain, Size: 933 bytes --]

For the aarch64-pe target various values of '.align' causes writing of COFF files to fail.

This version of the patch differs from the first by using the write.c implementation of SUB_SEGMENT_ALIGN. This means there was no need to modify md_section_align or aarch64_handle_align. The change is specific to the aarch64-pe target.

In my testing generated aligned code was correctly padded with nop.

Thanks,

Zac


---
  gas/config/tc-aarch64.h | 3 +++
  1 file changed, 3 insertions(+)

diff --git a/gas/config/tc-aarch64.h b/gas/config/tc-aarch64.h
index 2d514ff610..0ea73021f2 100644
--- a/gas/config/tc-aarch64.h
+++ b/gas/config/tc-aarch64.h
@@ -191,7 +191,10 @@ struct aarch64_frag_type
        goto LABEL;								\
      }

+/* COFF sub section alignment calculated using the write.c implementation.  */
+#ifndef OBJ_COFF
  #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
+#endif

  #define DWARF2_LINE_MIN_INSN_LENGTH 	4

-- 
2.25.1

[-- Attachment #2: 0001-GAS-fix-alignment-for-aarch64-pe.patch --]
[-- Type: text/x-diff, Size: 719 bytes --]

From 392e919f35ad790b21185ab1ce387bb0694cddd9 Mon Sep 17 00:00:00 2001
From: Zac Walker <zac.walker@linaro.org>
Date: Mon, 14 Nov 2022 17:48:58 +0100
Subject: [PATCH] GAS fix alignment for aarch64-pe

---
 gas/config/tc-aarch64.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/gas/config/tc-aarch64.h b/gas/config/tc-aarch64.h
index 2d514ff610..0ea73021f2 100644
--- a/gas/config/tc-aarch64.h
+++ b/gas/config/tc-aarch64.h
@@ -191,7 +191,10 @@ struct aarch64_frag_type
       goto LABEL;								\
     }
 
+/* COFF sub section alignment calculated using the write.c implementation.  */
+#ifndef OBJ_COFF
 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
+#endif
 
 #define DWARF2_LINE_MIN_INSN_LENGTH 	4
 
-- 
2.25.1


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

* Re: [PATCH V2] GAS fix alignment for aarch64-pe
  2022-11-14 17:24 [PATCH V2] GAS fix alignment for aarch64-pe Zac Walker
@ 2022-11-15 10:50 ` Nick Clifton
  2022-11-15 11:23   ` Zac Walker
  0 siblings, 1 reply; 9+ messages in thread
From: Nick Clifton @ 2022-11-15 10:50 UTC (permalink / raw)
  To: Zac Walker, binutils

Hi Zac,

> For the aarch64-pe target various values of '.align' causes writing of COFF files to fail.
> 
> This version of the patch differs from the first by using the write.c implementation of SUB_SEGMENT_ALIGN. This means there was no need to modify md_section_align or 
> aarch64_handle_align. The change is specific to the aarch64-pe target.
> 
> In my testing generated aligned code was correctly padded with nop.

Patch approved - please apply.

Cheers
   Nick


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

* Re: [PATCH V2] GAS fix alignment for aarch64-pe
  2022-11-15 10:50 ` Nick Clifton
@ 2022-11-15 11:23   ` Zac Walker
  2022-11-15 21:53     ` aarch64-pe can't fill 16 bytes in section .text Alan Modra
  0 siblings, 1 reply; 9+ messages in thread
From: Zac Walker @ 2022-11-15 11:23 UTC (permalink / raw)
  To: Nick Clifton, binutils

Thanks Nick.

On 11/15/22 11:50, Nick Clifton wrote:
> Hi Zac,
> 
>> For the aarch64-pe target various values of '.align' causes writing of COFF files to fail.
>>
>> This version of the patch differs from the first by using the write.c implementation of SUB_SEGMENT_ALIGN. This means there was no need to modify md_section_align or aarch64_handle_align. The change is specific to the aarch64-pe target.
>>
>> In my testing generated aligned code was correctly padded with nop.
> 
> Patch approved - please apply.
> 
> Cheers
>    Nick
> 

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

* aarch64-pe can't fill 16 bytes in section .text
  2022-11-15 11:23   ` Zac Walker
@ 2022-11-15 21:53     ` Alan Modra
  2022-11-16 17:03       ` Zac Walker
  0 siblings, 1 reply; 9+ messages in thread
From: Alan Modra @ 2022-11-15 21:53 UTC (permalink / raw)
  To: Zac Walker; +Cc: Nick Clifton, binutils

I'm going to fix the underlying coff_frob_section problem too.
Without commit b66e671854, this:
 .p2align 4
 nop
 .p2align 3
 nop
results in an error when coff_frob_section attempts to pad out the
section to a 16-byte boundary.  Due to miscalculating the pad pattern
repeat count, write.c:write_contents attempts to shove 16 bytes of
padding into the remaining 4 bytes of the .text section.

	* config/obj-coff.c (coff_frob_section): Correct fill count.
	Don't pad after errors.

diff --git a/gas/config/obj-coff.c b/gas/config/obj-coff.c
index 98c39e43907..9be697fb62e 100644
--- a/gas/config/obj-coff.c
+++ b/gas/config/obj-coff.c
@@ -1725,7 +1725,8 @@ coff_frob_section (segT sec)
   bfd_vma align_power = (bfd_vma) sec->alignment_power + OCTETS_PER_BYTE_POWER;
   bfd_vma mask = ((bfd_vma) 1 << align_power) - 1;
 
-  if (size & mask)
+  if (!do_not_pad_sections_to_alignment
+      && (size & mask) != 0)
     {
       bfd_vma new_size;
       fragS *last;
@@ -1740,7 +1741,10 @@ coff_frob_section (segT sec)
       while (fragp->fr_next != last)
 	fragp = fragp->fr_next;
       last->fr_address = size;
-      fragp->fr_offset += new_size - size;
+      if ((new_size - size) % fragp->fr_var == 0)
+	fragp->fr_offset += (new_size - size) / fragp->fr_var;
+      else
+	abort ();
     }
 #endif
 

-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: aarch64-pe can't fill 16 bytes in section .text
  2022-11-15 21:53     ` aarch64-pe can't fill 16 bytes in section .text Alan Modra
@ 2022-11-16 17:03       ` Zac Walker
  2022-11-17  3:01         ` Alan Modra
  0 siblings, 1 reply; 9+ messages in thread
From: Zac Walker @ 2022-11-16 17:03 UTC (permalink / raw)
  To: Alan Modra; +Cc: Nick Clifton, binutils

Thanks Alan,

Do you think my SUB_SEGMENT_ALIGN patch is still needed. I didn't merge it yet because I wanted to do more testing with your fix.

Zac

---
  gas/config/tc-aarch64.h | 3 +++
  1 file changed, 3 insertions(+)

diff --git a/gas/config/tc-aarch64.h b/gas/config/tc-aarch64.h
index 2d514ff610..0ea73021f2 100644
--- a/gas/config/tc-aarch64.h
+++ b/gas/config/tc-aarch64.h
@@ -191,7 +191,10 @@ struct aarch64_frag_type
        goto LABEL;								\
      }

+/* COFF sub section alignment calculated using the write.c implementation.  */
+#ifndef OBJ_COFF
  #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
+#endif

  #define DWARF2_LINE_MIN_INSN_LENGTH 	4

-- 
2.25.1



On 11/15/22 22:53, Alan Modra wrote:
> I'm going to fix the underlying coff_frob_section problem too.
> Without commit b66e671854, this:
>   .p2align 4
>   nop
>   .p2align 3
>   nop
> results in an error when coff_frob_section attempts to pad out the
> section to a 16-byte boundary.  Due to miscalculating the pad pattern
> repeat count, write.c:write_contents attempts to shove 16 bytes of
> padding into the remaining 4 bytes of the .text section.
> 
> 	* config/obj-coff.c (coff_frob_section): Correct fill count.
> 	Don't pad after errors.
> 
> diff --git a/gas/config/obj-coff.c b/gas/config/obj-coff.c
> index 98c39e43907..9be697fb62e 100644
> --- a/gas/config/obj-coff.c
> +++ b/gas/config/obj-coff.c
> @@ -1725,7 +1725,8 @@ coff_frob_section (segT sec)
>     bfd_vma align_power = (bfd_vma) sec->alignment_power + OCTETS_PER_BYTE_POWER;
>     bfd_vma mask = ((bfd_vma) 1 << align_power) - 1;
>   
> -  if (size & mask)
> +  if (!do_not_pad_sections_to_alignment
> +      && (size & mask) != 0)
>       {
>         bfd_vma new_size;
>         fragS *last;
> @@ -1740,7 +1741,10 @@ coff_frob_section (segT sec)
>         while (fragp->fr_next != last)
>   	fragp = fragp->fr_next;
>         last->fr_address = size;
> -      fragp->fr_offset += new_size - size;
> +      if ((new_size - size) % fragp->fr_var == 0)
> +	fragp->fr_offset += (new_size - size) / fragp->fr_var;
> +      else
> +	abort ();
>       }
>   #endif
>   
> 

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

* Re: aarch64-pe can't fill 16 bytes in section .text
  2022-11-16 17:03       ` Zac Walker
@ 2022-11-17  3:01         ` Alan Modra
  2022-11-18  8:52           ` zac.walker
  0 siblings, 1 reply; 9+ messages in thread
From: Alan Modra @ 2022-11-17  3:01 UTC (permalink / raw)
  To: Zac Walker; +Cc: Nick Clifton, binutils

On Wed, Nov 16, 2022 at 06:03:56PM +0100, Zac Walker wrote:
> Thanks Alan,
> 
> Do you think my SUB_SEGMENT_ALIGN patch is still needed. I didn't merge it yet because I wanted to do more testing with your fix.

Huh, I thought it had already gone in, but it was just in my local
tree.

Your patch isn't needed with my fix to obj-coff.c, but it wouldn't be
a bad idea to apply it anyway.  We have a few too many places doing
section padding.  It would be nice to get rid of the alignment code
in coff_frob_section.

-- 
Alan Modra
Australia Development Lab, IBM

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

* RE: aarch64-pe can't fill 16 bytes in section .text
  2022-11-17  3:01         ` Alan Modra
@ 2022-11-18  8:52           ` zac.walker
  2022-11-18  9:08             ` Jan Beulich
  0 siblings, 1 reply; 9+ messages in thread
From: zac.walker @ 2022-11-18  8:52 UTC (permalink / raw)
  To: 'Alan Modra'; +Cc: 'Nick Clifton', binutils

Patch merged just now. (Was a holiday here yesterday)

I am still investigating another alignment problem so might be another follow up patch. This code from OpenBLAS:

data_ar:
 .word 0x3e44fae6
data_ai:
 .word 0x3d320fa2
start:
 ldr s20, data_ar
 ldr s21, data_ai 

Produces:

zscal-min.s:2: Warning: value 0x3e44fae6 truncated to 0xfae6
zscal-min.s:4: Warning: value 0x3d320fa2 truncated to 0xfa2
zscal-min.s:7: Error: pc-relative load offset not word aligned

Works ok with clang. I am not sure if .word data should be aligned by default in gas or this is just a difference in behaviour.

Thanks,
Zac

-----Original Message-----
From: Alan Modra <amodra@gmail.com> 
Sent: 17 November 2022 04:02
To: Zac Walker <zac.walker@linaro.org>
Cc: Nick Clifton <nickc@redhat.com>; binutils@sourceware.org
Subject: Re: aarch64-pe can't fill 16 bytes in section .text

On Wed, Nov 16, 2022 at 06:03:56PM +0100, Zac Walker wrote:
> Thanks Alan,
> 
> Do you think my SUB_SEGMENT_ALIGN patch is still needed. I didn't merge it yet because I wanted to do more testing with your fix.

Huh, I thought it had already gone in, but it was just in my local tree.

Your patch isn't needed with my fix to obj-coff.c, but it wouldn't be a bad idea to apply it anyway.  We have a few too many places
doing section padding.  It would be nice to get rid of the alignment code in coff_frob_section.

--
Alan Modra
Australia Development Lab, IBM


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

* Re: aarch64-pe can't fill 16 bytes in section .text
  2022-11-18  8:52           ` zac.walker
@ 2022-11-18  9:08             ` Jan Beulich
  2022-11-18  9:35               ` zac.walker
  0 siblings, 1 reply; 9+ messages in thread
From: Jan Beulich @ 2022-11-18  9:08 UTC (permalink / raw)
  To: zac.walker; +Cc: 'Nick Clifton', binutils, 'Alan Modra'

On 18.11.2022 09:52, Zac Walker via Binutils wrote:
> Patch merged just now. (Was a holiday here yesterday)
> 
> I am still investigating another alignment problem so might be another follow up patch. This code from OpenBLAS:
> 
> data_ar:
>  .word 0x3e44fae6
> data_ai:
>  .word 0x3d320fa2
> start:
>  ldr s20, data_ar
>  ldr s21, data_ai 
> 
> Produces:
> 
> zscal-min.s:2: Warning: value 0x3e44fae6 truncated to 0xfae6
> zscal-min.s:4: Warning: value 0x3d320fa2 truncated to 0xfa2
> zscal-min.s:7: Error: pc-relative load offset not word aligned
> 
> Works ok with clang. I am not sure if .word data should be aligned by default in gas or this is just a difference in behaviour.

.word is overridden by tc-aarch64.c only for ELF. So in your COFF (aiui)
case .word emits just two bytes instead of the expected four. Hence also
the warnings, not just the error.

Jan

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

* RE: aarch64-pe can't fill 16 bytes in section .text
  2022-11-18  9:08             ` Jan Beulich
@ 2022-11-18  9:35               ` zac.walker
  0 siblings, 0 replies; 9+ messages in thread
From: zac.walker @ 2022-11-18  9:35 UTC (permalink / raw)
  To: 'Jan Beulich'
  Cc: 'Nick Clifton', binutils, 'Alan Modra'

Thanks for the help Jan. Adding a similar override to "tc-arm.c" fixes it:

#ifdef OBJ_ELF
 ...
#else
  { "word", cons, 4},
#endif

I will do some testing on other pseudo types in case I am missing more. 

Zac

-----Original Message-----
From: Jan Beulich <jbeulich@suse.com> 
Sent: 18 November 2022 10:09
To: zac.walker@linaro.org
Cc: 'Nick Clifton' <nickc@redhat.com>; binutils@sourceware.org; 'Alan Modra' <amodra@gmail.com>
Subject: Re: aarch64-pe can't fill 16 bytes in section .text

On 18.11.2022 09:52, Zac Walker via Binutils wrote:
> Patch merged just now. (Was a holiday here yesterday)
> 
> I am still investigating another alignment problem so might be another follow up patch. This code from OpenBLAS:
> 
> data_ar:
>  .word 0x3e44fae6
> data_ai:
>  .word 0x3d320fa2
> start:
>  ldr s20, data_ar
>  ldr s21, data_ai
> 
> Produces:
> 
> zscal-min.s:2: Warning: value 0x3e44fae6 truncated to 0xfae6
> zscal-min.s:4: Warning: value 0x3d320fa2 truncated to 0xfa2
> zscal-min.s:7: Error: pc-relative load offset not word aligned
> 
> Works ok with clang. I am not sure if .word data should be aligned by default in gas or this is just a difference in behaviour.

.word is overridden by tc-aarch64.c only for ELF. So in your COFF (aiui) case .word emits just two bytes instead of the expected four. Hence also the warnings, not just the error.

Jan


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

end of thread, other threads:[~2022-11-18  9:35 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-14 17:24 [PATCH V2] GAS fix alignment for aarch64-pe Zac Walker
2022-11-15 10:50 ` Nick Clifton
2022-11-15 11:23   ` Zac Walker
2022-11-15 21:53     ` aarch64-pe can't fill 16 bytes in section .text Alan Modra
2022-11-16 17:03       ` Zac Walker
2022-11-17  3:01         ` Alan Modra
2022-11-18  8:52           ` zac.walker
2022-11-18  9:08             ` Jan Beulich
2022-11-18  9:35               ` zac.walker

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).