[v1] GAS: Keep symbols in their own section [PR27566]

Message ID 20241022094318.58444-1-zengxiao@eswincomputing.com
State New
Headers
Series [v1] GAS: Keep symbols in their own section [PR27566] |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm fail Test failed

Commit Message

Xiao Zeng Oct. 22, 2024, 9:43 a.m. UTC
  PR27566 is categorized under ld:
<https://sourceware.org/bugzilla/show_bug.cgi?id=27566>
but the root cause of this issue is in gas.

The following assembly example is provided:
        .section        .rodata
        .globl Sym
        .type  Sym, @object
        .set   Sym, . + 0x1200
        .size	Sym, 8

readelf reads ELF information:
Section Headers:
  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 4] .rodata           PROGBITS        0000000000000000 000040 000000 00   A  0   0  1

Symbol table '.symtab' contains 7 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     6: 0000000000001200     8 OBJECT  GLOBAL DEFAULT    4 Sym

The Sym belongs to the .rodata section, but the size of .rodata is 0.

        PR gas/27566
	* write.c (size_seg): Update section size.
	* testsuite/gas/elf/elf.exp: New test.
	* testsuite/gas/elf/pr27566.d: New file.
	* testsuite/gas/elf/pr27566.s: Likewise.

Signed-off-by: Xiao Zeng <zengxiao@eswincomputing.com>
---
 gas/testsuite/gas/elf/elf.exp   |  1 +
 gas/testsuite/gas/elf/pr27566.d | 11 +++++++++++
 gas/testsuite/gas/elf/pr27566.s |  5 +++++
 gas/write.c                     |  7 +++++++
 4 files changed, 24 insertions(+)
 create mode 100644 gas/testsuite/gas/elf/pr27566.d
 create mode 100644 gas/testsuite/gas/elf/pr27566.s
  

Comments

Alan Modra Oct. 23, 2024, 4:13 a.m. UTC | #1
On Tue, Oct 22, 2024 at 05:43:18PM +0800, Xiao Zeng wrote:
> PR27566 is categorized under ld:
> <https://sourceware.org/bugzilla/show_bug.cgi?id=27566>
> but the root cause of this issue is in gas.
> 
> The following assembly example is provided:
>         .section        .rodata
>         .globl Sym
>         .type  Sym, @object
>         .set   Sym, . + 0x1200
>         .size	Sym, 8
> 
> readelf reads ELF information:
> Section Headers:
>   [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
>   [ 4] .rodata           PROGBITS        0000000000000000 000040 000000 00   A  0   0  1
> 
> Symbol table '.symtab' contains 7 entries:
>    Num:    Value          Size Type    Bind   Vis      Ndx Name
>      6: 0000000000001200     8 OBJECT  GLOBAL DEFAULT    4 Sym
> 
> The Sym belongs to the .rodata section, but the size of .rodata is 0.

It is not unreasonable to define a symbol relative to some section
with a value outside of the section.  Automatically extending a
section to enclose a symbol should only happen in special cases.
Patch rejected.
  
Xiao Zeng Oct. 23, 2024, 6:45 a.m. UTC | #2
2024-10-23 12:43  Alan Modra <amodra@gmail.com> wrote:
>
>On Tue, Oct 22, 2024 at 05:43:18PM +0800, Xiao Zeng wrote:
>> PR27566 is categorized under ld:
>> <https://sourceware.org/bugzilla/show_bug.cgi?id=27566>
>> but the root cause of this issue is in gas.
>>
>> The following assembly example is provided:
>>         .section        .rodata
>>         .globl Sym
>>         .type  Sym, @object
>>         .set   Sym, . + 0x1200
>>         .size	Sym, 8
>>
>> readelf reads ELF information:
>> Section Headers:
>>   [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
>>   [ 4] .rodata           PROGBITS        0000000000000000 000040 000000 00   A  0   0  1
>>
>> Symbol table '.symtab' contains 7 entries:
>>    Num:    Value          Size Type    Bind   Vis      Ndx Name
>>      6: 0000000000001200     8 OBJECT  GLOBAL DEFAULT    4 Sym
>>
>> The Sym belongs to the .rodata section, but the size of .rodata is 0.
>
>It is not unreasonable to define a symbol relative to some section
>with a value outside of the section.  Automatically extending a
>section to enclose a symbol should only happen in special cases.
>Patch rejected.
>
>--
>Alan Modra

Dear maintainer, thank you for your reply. I think I should provide more detailed information:
The test case <abs-run.c> is in:
<https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/testsuite/gcc.target/riscv/rvv/autovec/unop/abs-run.c;h=f3be58ec4938e1ae8b125da3e6796f8e448b04df;hb=ee030b28004eade3da872e7ae62a526a2940a705>

1 Compilation command:
riscv64-unknown-elf-gcc                                                                                     \
-fno-schedule-insns -mcmodel=medlow -march=rv64gcv_zvfh -mabi=lp64d \
-O3 -mrvv-max-lmul=m4 -mrvv-vector-bits=zvl -ffast-math                            \
-fdata-sections abs-run.c -S -o abs-run.c.upstream.s

Compiler version information:
Supported LTO compression algorithms: zlib
gcc version 15.0.0 20240912 (experimental) (g3f212eabbba-dirty)

2 Assembly command:
riscv64-unknown-elf-as abs-run.c.upstream.s -o abs-run.c.upstream.s.o

3 readelf outputs the information:
riscv64-unknown-elf-readelf -SsW abs-run.c.upstream.o > abs-run.c.upstream.o.txt

Section Headers:
  [Nr] Name              Type                   Address                      Off    Size   ES Flg Lk Inf Al
  [ 9] .rodata           PROGBITS        0000000000000000 000c78 000d80 00   A  0   0  8

Symbol table '.symtab' contains 110 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS abs-run.c
    12: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT    9 .LANCHOR0
    13: 0000000000001000     0 NOTYPE  LOCAL  DEFAULT    9 .LANCHOR1

This situation, where a symbol is in a section but the section's size is insufficient to contain the symbol, does exist.

Thanks
Xiao Zeng
  
Xiao Zeng Oct. 23, 2024, 7:04 a.m. UTC | #3
2024-10-23 14:45  Xiao Zeng <zengxiao@eswincomputing.com> wrote:
>
>2024-10-23 12:43  Alan Modra <amodra@gmail.com> wrote:
>>
>>On Tue, Oct 22, 2024 at 05:43:18PM +0800, Xiao Zeng wrote:
>>> PR27566 is categorized under ld:
>>> <https://sourceware.org/bugzilla/show_bug.cgi?id=27566>
>>> but the root cause of this issue is in gas.
>>>
>>> The following assembly example is provided:
>>>         .section        .rodata
>>>         .globl Sym
>>>         .type  Sym, @object
>>>         .set   Sym, . + 0x1200
>>>         .size	Sym, 8
>>>
>>> readelf reads ELF information:
>>> Section Headers:
>>>   [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
>>>   [ 4] .rodata           PROGBITS        0000000000000000 000040 000000 00   A  0   0  1
>>>
>>> Symbol table '.symtab' contains 7 entries:
>>>    Num:    Value          Size Type    Bind   Vis      Ndx Name
>>>      6: 0000000000001200     8 OBJECT  GLOBAL DEFAULT    4 Sym
>>>
>>> The Sym belongs to the .rodata section, but the size of .rodata is 0.
>>
>>It is not unreasonable to define a symbol relative to some section
>>with a value outside of the section.  Automatically extending a
>>section to enclose a symbol should only happen in special cases.
>>Patch rejected.
>>
>>--
>>Alan Modra
>
>Dear maintainer, thank you for your reply. I think I should provide more detailed information:
>The test case <abs-run.c> is in:
><https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=gcc/testsuite/gcc.target/riscv/rvv/autovec/unop/abs-run.c;h=f3be58ec4938e1ae8b125da3e6796f8e448b04df;hb=ee030b28004eade3da872e7ae62a526a2940a705>
>
>1 Compilation command:
>riscv64-unknown-elf-gcc                                                                                     \
>-fno-schedule-insns -mcmodel=medlow -march=rv64gcv_zvfh -mabi=lp64d \
>-O3 -mrvv-max-lmul=m4 -mrvv-vector-bits=zvl -ffast-math                            \
>-fdata-sections abs-run.c -S -o abs-run.c.upstream.s
> 

The key assembly information is:
	.section	.rodata
	.align	3
	.set	.LANCHOR0,. + 0
	.set	.LANCHOR1,. + 4096

>Compiler version information:
>Supported LTO compression algorithms: zlib
>gcc version 15.0.0 20240912 (experimental) (g3f212eabbba-dirty)
>
>2 Assembly command:
>riscv64-unknown-elf-as abs-run.c.upstream.s -o abs-run.c.upstream.s.o
>
>3 readelf outputs the information:
>riscv64-unknown-elf-readelf -SsW abs-run.c.upstream.o > abs-run.c.upstream.o.txt
>
>Section Headers:
>  [Nr] Name              Type                   Address                      Off    Size   ES Flg Lk Inf Al
>  [ 9] .rodata           PROGBITS        0000000000000000 000c78 000d80 00   A  0   0  8
>
>Symbol table '.symtab' contains 110 entries:
>   Num:    Value          Size Type    Bind   Vis      Ndx Name
>     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
>     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS abs-run.c
>    12: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT    9 .LANCHOR0
>    13: 0000000000001000     0 NOTYPE  LOCAL  DEFAULT    9 .LANCHOR1
>
>This situation, where a symbol is in a section but the section's size is insufficient to contain the symbol, does exist.
>
>Thanks
>Xiao Zeng
>
Thanks
Xiao Zeng
  
Alan Modra Oct. 23, 2024, 8:12 a.m. UTC | #4
On Wed, Oct 23, 2024 at 03:04:27PM +0800, Xiao Zeng wrote:
> 2024-10-23 14:45  Xiao Zeng <zengxiao@eswincomputing.com> wrote:
[snip]
> The key assembly information is:
> 	.section	.rodata
> 	.align	3
> 	.set	.LANCHOR0,. + 0
> 	.set	.LANCHOR1,. + 4096
[snip]
> >This situation, where a symbol is in a section but the section's size is insufficient to contain the symbol, does exist.

Yes, and this is exactly the situation I was thinking of when I said
that it is quite reasonably to define a symbol outside of a section.
It would be completely wrong to extend .rodata to at least 4096 bytes
due to the .LANCHOR1 symbol.
  
Xiao Zeng Oct. 23, 2024, 8:42 a.m. UTC | #5
2024-10-23 16:42  Alan Modra <amodra@gmail.com> wrote:
>
>On Wed, Oct 23, 2024 at 03:04:27PM +0800, Xiao Zeng wrote:
>> 2024-10-23 14:45  Xiao Zeng <zengxiao@eswincomputing.com> wrote:
>[snip]
>> The key assembly information is:
>> .section	.rodata
>> .align	3
>> .set	.LANCHOR0,. + 0
>> .set	.LANCHOR1,. + 4096
>[snip]
>> >This situation, where a symbol is in a section but the section's size is insufficient to contain the symbol, does exist.
>
>Yes, and this is exactly the situation I was thinking of when I said
>that it is quite reasonably to define a symbol outside of a section. 

Hi, Alan, could you provide me with more information on 'symbols can be defined outside of section'?

As a new GASer, I would appreciate any information.

When performing relax optimization in RISC-V, symbols defined outside of sections can
potentially cause LD to fail. This is why I submitted the patch.

>It would be completely wrong to extend .rodata to at least 4096 bytes
>due to the .LANCHOR1 symbol.
>
>--
>Alan Modra
Thanks
Xiao Zeng
  
Alan Modra Oct. 24, 2024, 9:44 a.m. UTC | #6
On Wed, Oct 23, 2024 at 04:42:00PM +0800, Xiao Zeng wrote:
> 2024-10-23 16:42  Alan Modra <amodra@gmail.com> wrote:
> >
> >On Wed, Oct 23, 2024 at 03:04:27PM +0800, Xiao Zeng wrote:
> >> 2024-10-23 14:45  Xiao Zeng <zengxiao@eswincomputing.com> wrote:
> >[snip]
> >> The key assembly information is:
> >> .section	.rodata
> >> .align	3
> >> .set	.LANCHOR0,. + 0
> >> .set	.LANCHOR1,. + 4096
> >[snip]
> >> >This situation, where a symbol is in a section but the section's size is insufficient to contain the symbol, does exist.
> >
> >Yes, and this is exactly the situation I was thinking of when I said
> >that it is quite reasonable to define a symbol outside of a section. 
> 
> Hi, Alan, could you provide me with more information on 'symbols can be defined outside of section'?

I'm not very familiar with riscv, but I imagine a typical use of
.LANCHOR1 is to set up a base register with the value of that symbol,
then use a reg+offset addressing mode with offsets in the range -4096
to +4095 to access some data up to 8k in size.  When the data being
accessed is smaller than 8k total size it would be possible to set the
base register to any point within the data not more than 4k from
either end, but doing so requires a little more work for the compiler.
It's simpler to say offsets always start at -4096.

> As a new GASer, I would appreciate any information.
> 
> When performing relax optimization in RISC-V, symbols defined outside of sections can
> potentially cause LD to fail. This is why I submitted the patch.
> 
> >It would be completely wrong to extend .rodata to at least 4096 bytes
> >due to the .LANCHOR1 symbol.
> >
> >--
> >Alan Modra
> Thanks
> Xiao Zeng
>
  
Xiao Zeng Oct. 25, 2024, 5:40 a.m. UTC | #7
2024-10-24 18:14  Alan Modra <amodra@gmail.com> wrote:
>
>On Wed, Oct 23, 2024 at 04:42:00PM +0800, Xiao Zeng wrote:
>> 2024-10-23 16:42  Alan Modra <amodra@gmail.com> wrote:
>> >
>> >On Wed, Oct 23, 2024 at 03:04:27PM +0800, Xiao Zeng wrote:
>> >> 2024-10-23 14:45  Xiao Zeng <zengxiao@eswincomputing.com> wrote:
>> >[snip]
>> >> The key assembly information is:
>> >> .section	.rodata
>> >> .align	3
>> >> .set	.LANCHOR0,. + 0
>> >> .set	.LANCHOR1,. + 4096
>> >[snip]
>> >> >This situation, where a symbol is in a section but the section's size is insufficient to contain the symbol, does exist.
>> >
>> >Yes, and this is exactly the situation I was thinking of when I said
>> >that it is quite reasonable to define a symbol outside of a section.
>>
>> Hi, Alan, could you provide me with more information on 'symbols can be defined outside of section'?
>
>I'm not very familiar with riscv, but I imagine a typical use of
>.LANCHOR1 is to set up a base register with the value of that symbol,
>then use a reg+offset addressing mode with offsets in the range -4096
>to +4095 to access some data up to 8k in size.  When the data being
>accessed is smaller than 8k total size it would be possible to set the
>base register to any point within the data not more than 4k from
>either end, but doing so requires a little more work for the compiler.
>It's simpler to say offsets always start at -4096. 
Thank you, Alan , for the information; it's been very helpful to me.

Since this issue was introduced in the RISC-V port and is also related
to GCC, I would like to seek assistance from the RISC-V team.

>
>> As a new GASer, I would appreciate any information.
>>
>> When performing relax optimization in RISC-V, symbols defined outside of sections can
>> potentially cause LD to fail. This is why I submitted the patch.
>>
>> >It would be completely wrong to extend .rodata to at least 4096 bytes
>> >due to the .LANCHOR1 symbol.
>> >
>> >--
>> >Alan Modra
>> Thanks
>> Xiao Zeng
>>
>
>--
>Alan Modra
Thanks
Xiao Zeng
  
Xiao Zeng Oct. 25, 2024, 5:54 a.m. UTC | #8
Hi Jeff and Nelson, an issue has arisen in GCC, described here:
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115166>

Afterward, it was thought to be an ld issue, described here:
<https://sourceware.org/bugzilla/show_bug.cgi?id=27566>

Nelson has done some work on ld, but it hasn’t completely resolved the problem. I understand that this issue may actually stem from as. I have tested the following modification, which can resolve the current issue.

Here is the emails exchange I had with Alan:
<https://sourceware.org/pipermail/binutils/2024-October/137360.html>

Alan provided some excellent insights.

To help drive the resolution of this issue, could you also offer some advice when you have time?

Thank you both very much.

2024-10-22 17:43  Xiao Zeng <zengxiao@eswincomputing.com> wrote:
>
>PR27566 is categorized under ld:
><https://sourceware.org/bugzilla/show_bug.cgi?id=27566>
>but the root cause of this issue is in gas.
>
>The following assembly example is provided:
>        .section        .rodata
>        .globl Sym
>        .type  Sym, @object
>        .set   Sym, . + 0x1200
>        .size	Sym, 8
>
>readelf reads ELF information:
>Section Headers:
>  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
>  [ 4] .rodata           PROGBITS        0000000000000000 000040 000000 00   A  0   0  1
>
>Symbol table '.symtab' contains 7 entries:
>   Num:    Value          Size Type    Bind   Vis      Ndx Name
>     6: 0000000000001200     8 OBJECT  GLOBAL DEFAULT    4 Sym
>
>The Sym belongs to the .rodata section, but the size of .rodata is 0.
>
>        PR gas/27566
>	* write.c (size_seg): Update section size.
>	* testsuite/gas/elf/elf.exp: New test.
>	* testsuite/gas/elf/pr27566.d: New file.
>	* testsuite/gas/elf/pr27566.s: Likewise.
>
>Signed-off-by: Xiao Zeng <zengxiao@eswincomputing.com>
>---
> gas/testsuite/gas/elf/elf.exp   |  1 +
> gas/testsuite/gas/elf/pr27566.d | 11 +++++++++++
> gas/testsuite/gas/elf/pr27566.s |  5 +++++
> gas/write.c                     |  7 +++++++
> 4 files changed, 24 insertions(+)
> create mode 100644 gas/testsuite/gas/elf/pr27566.d
> create mode 100644 gas/testsuite/gas/elf/pr27566.s
>
>diff --git a/gas/testsuite/gas/elf/elf.exp b/gas/testsuite/gas/elf/elf.exp
>index c828c3af25c..6a69acbc109 100644
>--- a/gas/testsuite/gas/elf/elf.exp
>+++ b/gas/testsuite/gas/elf/elf.exp
>@@ -387,4 +387,5 @@ if { [is_elf_format] } then {
>     run_dump_test "bignums" $dump_opts
>     run_dump_test "section-symbol-redef"
>     run_dump_test "pr27228"
>+    run_dump_test "pr27566"
> }
>diff --git a/gas/testsuite/gas/elf/pr27566.d b/gas/testsuite/gas/elf/pr27566.d
>new file mode 100644
>index 00000000000..75268cefb20
>--- /dev/null
>+++ b/gas/testsuite/gas/elf/pr27566.d
>@@ -0,0 +1,11 @@
>+#as:
>+#name: Keep symbols in their own section (PR 27566)
>+#readelf: -S --wide
>+
>+There are [0-9]+ section headers, starting at offset 0x[0-9a-f]+:
>+
>+Section Headers:
>+ +\[Nr\] Name +Type +Addr(ess|) +Off +Size +ES +Flg +Lk +Inf +Al
>+#...
>+ *\[ [1-9]\] *\.rodata +PROGBITS +0*0 +0[0-9a-f]* +0*1208 .*
>+#pass
>diff --git a/gas/testsuite/gas/elf/pr27566.s b/gas/testsuite/gas/elf/pr27566.s
>new file mode 100644
>index 00000000000..4232df21494
>--- /dev/null
>+++ b/gas/testsuite/gas/elf/pr27566.s
>@@ -0,0 +1,5 @@
>+        .section        .rodata
>+        .globl Sym
>+        .type  Sym, @object
>+        .set   Sym, . + 0x1200
>+        .size	Sym, 8
>diff --git a/gas/write.c b/gas/write.c
>index 18cf18fc830..7f69d7aad44 100644
>--- a/gas/write.c
>+++ b/gas/write.c
>@@ -559,6 +559,7 @@ size_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx ATTRIBUTE_UNUSED)
>   segment_info_type *seginfo;
>   int x;
>   valueT size, newsize;
>+  symbolS *symp;
>
>   subseg_change (sec, 0);
>
>@@ -577,6 +578,12 @@ size_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx ATTRIBUTE_UNUSED)
>   else
>     size = 0;
>
>+  /* Keep symbols in their own section.  */
>+  for (symp = symbol_rootP; symp; symp = symbol_next (symp))
>+    if (S_GET_SEGMENT (symp) == sec
>+	&& size < (S_GET_VALUE (symp) + S_GET_SIZE (symp)))
>+      size = S_GET_VALUE (symp) + S_GET_SIZE (symp);
>+
>   flags = bfd_section_flags (sec);
>   if (size == 0 && bfd_section_size (sec) != 0 &&
>     (flags & SEC_HAS_CONTENTS) != 0)
>--
>2.17.1
Thanks
Xiao Zeng
  
Fangrui Song Oct. 25, 2024, 6:47 a.m. UTC | #9
On Thu, Oct 24, 2024 at 10:54 PM Xiao Zeng <zengxiao@eswincomputing.com> wrote:
>
> Hi Jeff and Nelson, an issue has arisen in GCC, described here:
> <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115166>
>
> Afterward, it was thought to be an ld issue, described here:
> <https://sourceware.org/bugzilla/show_bug.cgi?id=27566>
>
> Nelson has done some work on ld, but it hasn’t completely resolved the problem. I understand that this issue may actually stem from as. I have tested the following modification, which can resolve the current issue.
>
> Here is the emails exchange I had with Alan:
> <https://sourceware.org/pipermail/binutils/2024-October/137360.html>
>
> Alan provided some excellent insights.
>
> To help drive the resolution of this issue, could you also offer some advice when you have time?
>
> Thank you both very much.
>
> 2024-10-22 17:43  Xiao Zeng <zengxiao@eswincomputing.com> wrote:
> >
> >PR27566 is categorized under ld:
> ><https://sourceware.org/bugzilla/show_bug.cgi?id=27566>
> >but the root cause of this issue is in gas.
> >
> >The following assembly example is provided:
> >        .section        .rodata
> >        .globl Sym
> >        .type  Sym, @object
> >        .set   Sym, . + 0x1200
> >        .size  Sym, 8
> >
> >readelf reads ELF information:
> >Section Headers:
> >  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
> >  [ 4] .rodata           PROGBITS        0000000000000000 000040 000000 00   A  0   0  1
> >
> >Symbol table '.symtab' contains 7 entries:
> >   Num:    Value          Size Type    Bind   Vis      Ndx Name
> >     6: 0000000000001200     8 OBJECT  GLOBAL DEFAULT    4 Sym
> >
> >The Sym belongs to the .rodata section, but the size of .rodata is 0.
> >
> >        PR gas/27566
> >       * write.c (size_seg): Update section size.
> >       * testsuite/gas/elf/elf.exp: New test.
> >       * testsuite/gas/elf/pr27566.d: New file.
> >       * testsuite/gas/elf/pr27566.s: Likewise.
> >
> >Signed-off-by: Xiao Zeng <zengxiao@eswincomputing.com>
> >---
> > gas/testsuite/gas/elf/elf.exp   |  1 +
> > gas/testsuite/gas/elf/pr27566.d | 11 +++++++++++
> > gas/testsuite/gas/elf/pr27566.s |  5 +++++
> > gas/write.c                     |  7 +++++++
> > 4 files changed, 24 insertions(+)
> > create mode 100644 gas/testsuite/gas/elf/pr27566.d
> > create mode 100644 gas/testsuite/gas/elf/pr27566.s
> >
> >diff --git a/gas/testsuite/gas/elf/elf.exp b/gas/testsuite/gas/elf/elf.exp
> >index c828c3af25c..6a69acbc109 100644
> >--- a/gas/testsuite/gas/elf/elf.exp
> >+++ b/gas/testsuite/gas/elf/elf.exp
> >@@ -387,4 +387,5 @@ if { [is_elf_format] } then {
> >     run_dump_test "bignums" $dump_opts
> >     run_dump_test "section-symbol-redef"
> >     run_dump_test "pr27228"
> >+    run_dump_test "pr27566"
> > }
> >diff --git a/gas/testsuite/gas/elf/pr27566.d b/gas/testsuite/gas/elf/pr27566.d
> >new file mode 100644
> >index 00000000000..75268cefb20
> >--- /dev/null
> >+++ b/gas/testsuite/gas/elf/pr27566.d
> >@@ -0,0 +1,11 @@
> >+#as:
> >+#name: Keep symbols in their own section (PR 27566)
> >+#readelf: -S --wide
> >+
> >+There are [0-9]+ section headers, starting at offset 0x[0-9a-f]+:
> >+
> >+Section Headers:
> >+ +\[Nr\] Name +Type +Addr(ess|) +Off +Size +ES +Flg +Lk +Inf +Al
> >+#...
> >+ *\[ [1-9]\] *\.rodata +PROGBITS +0*0 +0[0-9a-f]* +0*1208 .*
> >+#pass
> >diff --git a/gas/testsuite/gas/elf/pr27566.s b/gas/testsuite/gas/elf/pr27566.s
> >new file mode 100644
> >index 00000000000..4232df21494
> >--- /dev/null
> >+++ b/gas/testsuite/gas/elf/pr27566.s
> >@@ -0,0 +1,5 @@
> >+        .section        .rodata
> >+        .globl Sym
> >+        .type  Sym, @object
> >+        .set   Sym, . + 0x1200
> >+        .size Sym, 8
> >diff --git a/gas/write.c b/gas/write.c
> >index 18cf18fc830..7f69d7aad44 100644
> >--- a/gas/write.c
> >+++ b/gas/write.c
> >@@ -559,6 +559,7 @@ size_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx ATTRIBUTE_UNUSED)
> >   segment_info_type *seginfo;
> >   int x;
> >   valueT size, newsize;
> >+  symbolS *symp;
> >
> >   subseg_change (sec, 0);
> >
> >@@ -577,6 +578,12 @@ size_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx ATTRIBUTE_UNUSED)
> >   else
> >     size = 0;
> >
> >+  /* Keep symbols in their own section.  */
> >+  for (symp = symbol_rootP; symp; symp = symbol_next (symp))
> >+    if (S_GET_SEGMENT (symp) == sec
> >+      && size < (S_GET_VALUE (symp) + S_GET_SIZE (symp)))
> >+      size = S_GET_VALUE (symp) + S_GET_SIZE (symp);
> >+
> >   flags = bfd_section_flags (sec);
> >   if (size == 0 && bfd_section_size (sec) != 0 &&
> >     (flags & SEC_HAS_CONTENTS) != 0)
> >--
> >2.17.1
> Thanks
> Xiao Zeng


I commented on the GNU ld bug report
https://sourceware.org/bugzilla/show_bug.cgi?id=27566#c11 .
This looks like a linker relaxation bug in ld riscv. I agree with
Alan's rejection of the assembler patch.
  
Xiao Zeng Oct. 28, 2024, 6:30 a.m. UTC | #10
2024-10-25 14:47  Fangrui Song <i@maskray.me> wrote:
>
>On Thu, Oct 24, 2024 at 10:54 PM Xiao Zeng <zengxiao@eswincomputing.com> wrote:
>>
>> Hi Jeff and Nelson, an issue has arisen in GCC, described here:
>> <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115166>
>>
>> Afterward, it was thought to be an ld issue, described here:
>> <https://sourceware.org/bugzilla/show_bug.cgi?id=27566>
>>
>> Nelson has done some work on ld, but it hasn’t completely resolved the problem. I understand that this issue may actually stem from as. I have tested the following modification, which can resolve the current issue.
>>
>> Here is the emails exchange I had with Alan:
>> <https://sourceware.org/pipermail/binutils/2024-October/137360.html>
>>
>> Alan provided some excellent insights.
>>
>> To help drive the resolution of this issue, could you also offer some advice when you have time?
>>
>> Thank you both very much.
>>
>> 2024-10-22 17:43  Xiao Zeng <zengxiao@eswincomputing.com> wrote:
>> >
>> >PR27566 is categorized under ld:
>> ><https://sourceware.org/bugzilla/show_bug.cgi?id=27566>
>> >but the root cause of this issue is in gas.
>> >
>> >The following assembly example is provided:
>> >        .section        .rodata
>> >        .globl Sym
>> >        .type  Sym, @object
>> >        .set   Sym, . + 0x1200
>> >        .size  Sym, 8
>> >
>> >readelf reads ELF information:
>> >Section Headers:
>> >  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
>> >  [ 4] .rodata           PROGBITS        0000000000000000 000040 000000 00   A  0   0  1
>> >
>> >Symbol table '.symtab' contains 7 entries:
>> >   Num:    Value          Size Type    Bind   Vis      Ndx Name
>> >     6: 0000000000001200     8 OBJECT  GLOBAL DEFAULT    4 Sym
>> >
>> >The Sym belongs to the .rodata section, but the size of .rodata is 0.
>> >
>> >        PR gas/27566
>> >       * write.c (size_seg): Update section size.
>> >       * testsuite/gas/elf/elf.exp: New test.
>> >       * testsuite/gas/elf/pr27566.d: New file.
>> >       * testsuite/gas/elf/pr27566.s: Likewise.
>> >
>> >Signed-off-by: Xiao Zeng <zengxiao@eswincomputing.com>
>> >---
>> > gas/testsuite/gas/elf/elf.exp   |  1 +
>> > gas/testsuite/gas/elf/pr27566.d | 11 +++++++++++
>> > gas/testsuite/gas/elf/pr27566.s |  5 +++++
>> > gas/write.c                     |  7 +++++++
>> > 4 files changed, 24 insertions(+)
>> > create mode 100644 gas/testsuite/gas/elf/pr27566.d
>> > create mode 100644 gas/testsuite/gas/elf/pr27566.s
>> >
>> >diff --git a/gas/testsuite/gas/elf/elf.exp b/gas/testsuite/gas/elf/elf.exp
>> >index c828c3af25c..6a69acbc109 100644
>> >--- a/gas/testsuite/gas/elf/elf.exp
>> >+++ b/gas/testsuite/gas/elf/elf.exp
>> >@@ -387,4 +387,5 @@ if { [is_elf_format] } then {
>> >     run_dump_test "bignums" $dump_opts
>> >     run_dump_test "section-symbol-redef"
>> >     run_dump_test "pr27228"
>> >+    run_dump_test "pr27566"
>> > }
>> >diff --git a/gas/testsuite/gas/elf/pr27566.d b/gas/testsuite/gas/elf/pr27566.d
>> >new file mode 100644
>> >index 00000000000..75268cefb20
>> >--- /dev/null
>> >+++ b/gas/testsuite/gas/elf/pr27566.d
>> >@@ -0,0 +1,11 @@
>> >+#as:
>> >+#name: Keep symbols in their own section (PR 27566)
>> >+#readelf: -S --wide
>> >+
>> >+There are [0-9]+ section headers, starting at offset 0x[0-9a-f]+:
>> >+
>> >+Section Headers:
>> >+ +\[Nr\] Name +Type +Addr(ess|) +Off +Size +ES +Flg +Lk +Inf +Al
>> >+#...
>> >+ *\[ [1-9]\] *\.rodata +PROGBITS +0*0 +0[0-9a-f]* +0*1208 .*
>> >+#pass
>> >diff --git a/gas/testsuite/gas/elf/pr27566.s b/gas/testsuite/gas/elf/pr27566.s
>> >new file mode 100644
>> >index 00000000000..4232df21494
>> >--- /dev/null
>> >+++ b/gas/testsuite/gas/elf/pr27566.s
>> >@@ -0,0 +1,5 @@
>> >+        .section        .rodata
>> >+        .globl Sym
>> >+        .type  Sym, @object
>> >+        .set   Sym, . + 0x1200
>> >+        .size Sym, 8
>> >diff --git a/gas/write.c b/gas/write.c
>> >index 18cf18fc830..7f69d7aad44 100644
>> >--- a/gas/write.c
>> >+++ b/gas/write.c
>> >@@ -559,6 +559,7 @@ size_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx ATTRIBUTE_UNUSED)
>> >   segment_info_type *seginfo;
>> >   int x;
>> >   valueT size, newsize;
>> >+  symbolS *symp;
>> >
>> >   subseg_change (sec, 0);
>> >
>> >@@ -577,6 +578,12 @@ size_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx ATTRIBUTE_UNUSED)
>> >   else
>> >     size = 0;
>> >
>> >+  /* Keep symbols in their own section.  */
>> >+  for (symp = symbol_rootP; symp; symp = symbol_next (symp))
>> >+    if (S_GET_SEGMENT (symp) == sec
>> >+      && size < (S_GET_VALUE (symp) + S_GET_SIZE (symp)))
>> >+      size = S_GET_VALUE (symp) + S_GET_SIZE (symp);
>> >+
>> >   flags = bfd_section_flags (sec);
>> >   if (size == 0 && bfd_section_size (sec) != 0 &&
>> >     (flags & SEC_HAS_CONTENTS) != 0)
>> >--
>> >2.17.1
>> Thanks
>> Xiao Zeng
>
>
>I commented on the GNU ld bug report
>https://sourceware.org/bugzilla/show_bug.cgi?id=27566#c11 .
>This looks like a linker relaxation bug in ld riscv. I agree with
>Alan's rejection of the assembler patch. 
Alright, thank you, Fangrui, for the code review feedback.
I'll take a look at the linker when I have time and continue to work on this issue.

Thanks
Xiao Zeng
  

Patch

diff --git a/gas/testsuite/gas/elf/elf.exp b/gas/testsuite/gas/elf/elf.exp
index c828c3af25c..6a69acbc109 100644
--- a/gas/testsuite/gas/elf/elf.exp
+++ b/gas/testsuite/gas/elf/elf.exp
@@ -387,4 +387,5 @@  if { [is_elf_format] } then {
     run_dump_test "bignums" $dump_opts
     run_dump_test "section-symbol-redef"
     run_dump_test "pr27228"
+    run_dump_test "pr27566"
 }
diff --git a/gas/testsuite/gas/elf/pr27566.d b/gas/testsuite/gas/elf/pr27566.d
new file mode 100644
index 00000000000..75268cefb20
--- /dev/null
+++ b/gas/testsuite/gas/elf/pr27566.d
@@ -0,0 +1,11 @@ 
+#as:
+#name: Keep symbols in their own section (PR 27566)
+#readelf: -S --wide
+
+There are [0-9]+ section headers, starting at offset 0x[0-9a-f]+:
+
+Section Headers:
+ +\[Nr\] Name +Type +Addr(ess|) +Off +Size +ES +Flg +Lk +Inf +Al
+#...
+ *\[ [1-9]\] *\.rodata +PROGBITS +0*0 +0[0-9a-f]* +0*1208 .*
+#pass
diff --git a/gas/testsuite/gas/elf/pr27566.s b/gas/testsuite/gas/elf/pr27566.s
new file mode 100644
index 00000000000..4232df21494
--- /dev/null
+++ b/gas/testsuite/gas/elf/pr27566.s
@@ -0,0 +1,5 @@ 
+        .section        .rodata
+        .globl Sym
+        .type  Sym, @object
+        .set   Sym, . + 0x1200
+        .size	Sym, 8
diff --git a/gas/write.c b/gas/write.c
index 18cf18fc830..7f69d7aad44 100644
--- a/gas/write.c
+++ b/gas/write.c
@@ -559,6 +559,7 @@  size_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx ATTRIBUTE_UNUSED)
   segment_info_type *seginfo;
   int x;
   valueT size, newsize;
+  symbolS *symp;
 
   subseg_change (sec, 0);
 
@@ -577,6 +578,12 @@  size_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx ATTRIBUTE_UNUSED)
   else
     size = 0;
 
+  /* Keep symbols in their own section.  */
+  for (symp = symbol_rootP; symp; symp = symbol_next (symp))
+    if (S_GET_SEGMENT (symp) == sec
+	&& size < (S_GET_VALUE (symp) + S_GET_SIZE (symp)))
+      size = S_GET_VALUE (symp) + S_GET_SIZE (symp);
+
   flags = bfd_section_flags (sec);
   if (size == 0 && bfd_section_size (sec) != 0 &&
     (flags & SEC_HAS_CONTENTS) != 0)