From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 433B43858D3C; Mon, 5 Sep 2022 08:53:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 433B43858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662367997; bh=X1P2YNKCcPhg1+x7cMsUWWdPjLxLD6NRXfN0AEDFZGM=; h=From:To:Subject:Date:From; b=gnqDJN6PdtVt1tfHbTqykIf7/Dm0KLXlx55KV6kNlr39sxbh7W89/D3gpvpaPab3p SkduFQ3XfGhg2zo3JlD+yqk0hFkceofkLIKjJYHuP5UyoWfsXfBjSggxUHtfZWM8r+ AiJJSmuGIEAWcRtiG3k8mddtZcB4INQgxT1S0cCg= From: "rui314 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/106835] New: [i386] Taking an address of _GLOBAL_OFFSET_TABLE_ produces a wrong value Date: Mon, 05 Sep 2022 08:53:16 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 11.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: rui314 at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D106835 Bug ID: 106835 Summary: [i386] Taking an address of _GLOBAL_OFFSET_TABLE_ produces a wrong value Product: gcc Version: 11.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: rui314 at gmail dot com Target Milestone: --- On i386, _GLOBAL_OFFSET_TABLE_ should reference the location of .got.plt. However, the following piece of code prints out a bogus value: ``` $ cat foo.c #include extern char _GLOBAL_OFFSET_TABLE_[]; char *ptr =3D _GLOBAL_OFFSET_TABLE_; int main() { printf("%lx\n", (unsigned long)ptr); } $ i686-linux-gnu-gcc-12 -m32 -c foo.c $ i686-linux-gnu-gcc-12 -m32 -o foo foo.o $ ./foo ffffffd0 ``` This is because the relocation for .data is of type R_386_GOTPC. It should = be R_386_32. ``` $ readelf -r foo.o Relocation section '.rel.text' at offset 0x234 contains 5 entries: Offset Info Type Sym. Value Symbol's Name 00000010 00000802 R_386_PC32 00000000 __x86.get_pc_thunk.ax 00000015 0000060a R_386_GOTPC 00000000 _GLOBAL_OFFSET_TABLE_ 0000001b 00000509 R_386_GOTOFF 00000000 ptr 00000025 00000309 R_386_GOTOFF 00000000 .rodata 0000002d 00000904 R_386_PLT32 00000000 printf Relocation section '.rel.data.rel' at offset 0x25c contains 1 entry: Offset Info Type Sym. Value Symbol's Name 00000000 0000060a R_386_GOTPC 00000000 _GLOBAL_OFFSET_TABLE_ ``` I found this bug when writing a test for the mold linker. Related to: https://github.com/rui314/mold/issues/693=