public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "palmer at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug middle-end/106818] code is genereated differently with or without 'extern'
Date: Fri, 02 Sep 2022 23:59:02 +0000	[thread overview]
Message-ID: <bug-106818-4-X8f2MZZ51F@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-106818-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106818

--- Comment #8 from palmer at gcc dot gnu.org ---
(In reply to Andrew Pinski from comment #7)
> (In reply to baoshan from comment #6)
> > > really of unknown alignment then sharing the lui might not work.
> > Can you elaborate why shareing the lui might not work?

Unless I've managed to screw up some bit arithmetic here, it's just overflow
that we're not detecting at link time:

$ cat test.c 
extern char glob[4];

int _start(void) {
        int *i = (int *)glob;
        return *i;
}
$ cat glob.s 
.section .sdata
.balign 4096
.global empty
empty:
.rep 2046
.byte 0
.endr
.global glob
glob:
.byte 1, 2, 3, 4
$ riscv64-linux-gnu-gcc test.c glob.s -O3 -o test -static -fno-PIE
-mcmodel=medlow -mexplicit-relocs -nostdlib
$ riscv64-linux-gnu-objdump -d test
...
000000000001010c <_start>:
   1010c:       66c9                    lui     a3,0x12
   1010e:       7ff6c703                lbu     a4,2047(a3) # 127ff <glob+0x1>
   10112:       7fe6c603                lbu     a2,2046(a3)
   10116:       8006c783                lbu     a5,-2048(a3)
   1011a:       8016c503                lbu     a0,-2047(a3)
...

So that's going to load

a3 = 0x127ff 
a2 = 0x127fd
a5 = 0x11800
a6 = 0x11801

Which is wrong.

We can't detect it at link time because both relocations are being processed
correctly, they just don't know about each other (and really can't, because
there's nothing coupling them together).

> Linker relaxation not coming in and relaxing it to be use gp offsets instead.
> It is one of the worst parts of the riscv toolchain ...

Though this time linker relaxation is actually biting us twice:

First, it's masking this problem for small programs: if these accesses are all
within range of GP we end up producing executables that function fine, as the
relaxation calculates the full addresses to use as GP offsets.

Second, the GP relaxations just don't work when we share LUIs for
possibly-misaligned symbols because we delete the LUI if the first low-half is
within GP range.  For example:

$ cat glob.s 
.section .sdata
.global empty
empty:
.rep 4090
.byte 0
.endr
.global glob
glob:
.byte 1, 2, 3, 4
$ riscv64-linux-gnu-gcc test.c glob.s -O3 -o test -static -fno-PIE
-mcmodel=medlow -mexplicit-relocs --save-temps -nostdlib
$ riscv64-linux-gnu-objdump -d test
...
000000000001010c <_start>:
   1010c:       7fb1c703                lbu     a4,2043(gp) # 12127 <glob+0x1>
   10110:       7fa1c603                lbu     a2,2042(gp) # 12126 <glob>
   10114:       1286c783                lbu     a5,296(a3)
   10118:       1296c503                lbu     a0,297(a3)
...

We had that problem with the AUIPC->GP relaxation as well, but could fix it
there because the low half points to the high half.  Here I think there's also
nothing we can do in the linker, as there's no way to tell when the result of
the LUI is completely unused -- we could deal with simple cases like this, but
with control flow there's no way to handle all of them.

  parent reply	other threads:[~2022-09-02 23:59 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-02 19:26 [Bug c/106818] New: " pangbw at gmail dot com
2022-09-02 19:28 ` [Bug c/106818] " pangbw at gmail dot com
2022-09-02 19:37 ` pinskia at gcc dot gnu.org
2022-09-02 19:38 ` [Bug middle-end/106818] " pinskia at gcc dot gnu.org
2022-09-02 20:03 ` palmer at gcc dot gnu.org
2022-09-02 22:29 ` pangbw at gmail dot com
2022-09-02 22:33 ` pangbw at gmail dot com
2022-09-02 22:37 ` pinskia at gcc dot gnu.org
2022-09-02 23:59 ` palmer at gcc dot gnu.org [this message]
2022-09-05 10:15 ` rguenth at gcc dot gnu.org
2022-10-21 16:22 ` pinskia at gcc dot gnu.org
2022-10-21 16:31 ` [Bug target/106818] riscv produces bad low_sum while doing expansion of strict aligned stores/load pinskia at gcc dot gnu.org
2022-10-21 16:33 ` pinskia at gcc dot gnu.org
2023-11-29 18:45 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-106818-4-X8f2MZZ51F@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).