Bug 39013 - [4.3 Regression] Missing @PLT when -fpie is used
Summary: [4.3 Regression] Missing @PLT when -fpie is used
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.3.3
: P2 normal
Target Milestone: 4.3.4
Assignee: Richard Biener
URL: http://gcc.gnu.org/ml/gcc-patches/200...
Keywords: wrong-code
Depends on:
Blocks: 39026
  Show dependency treegraph
 
Reported: 2009-01-28 22:11 UTC by Magnus Granberg
Modified: 2018-02-19 04:59 UTC (History)
3 users (show)

See Also:
Host:
Target: x86_64-linux-gnu
Build:
Known to work: 4.2.4 4.3.4 4.4.0
Known to fail: 4.3.3
Last reconfirmed: 2009-01-30 20:32:12


Attachments
gre compile with gcc 4.3 and -fPIE -save-temps gre.i (36.46 KB, text/plain)
2009-01-28 22:16 UTC, Magnus Granberg
Details
A patch (620 bytes, patch)
2009-01-29 15:53 UTC, H.J. Lu
Details | Diff
fixed for gcc 4.3.3 (799 bytes, patch)
2009-01-30 22:59 UTC, Magnus Granberg
Details | Diff
Got ICE when compile some code with the old patch. (782 bytes, patch)
2009-03-31 02:37 UTC, Magnus Granberg
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Magnus Granberg 2009-01-28 22:11:27 UTC
http://bugs.gentoo.org/show_bug.cgi?id=254355
We are using patched gcc but it fail on gentoo's default compiler's to.
It works fine with GCC 4.2.4 and lower.
The fail file is part of libnet
the symbol that miss the @PLT is libnet_getgre_length
it sould have @PLT as the rest of the symbols
If i compile the fail file with -pic instead i compiles fine.
and have @PLT added 
gcc version 4.3.3
Target: x86_64-pc-linux-gnu
Configured with: /var/tmp/portage/sys-devel/gcc-4.3.3-r1/work/gcc-4.3.3/configure --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/4.3.3 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.3/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.3.3 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.3.3/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.3.3/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.3/include/g++-v4 --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --disable-altivec --disable-fixed-point --enable-nls --without-included-gettext --with-system-zlib --disable-checking --disable-werror --enable-secureplt --enable-multilib --enable-libmudflap --disable-libssp --enable-libgomp --enable-cld --disable-libgcj --enable-languages=c,c++,treelang --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu 
Thread model: posix
glibc-2.8_p20080602-r1
Intel-R-_Xeon-R-_CPU_E5420_@_2.50GHz
binutils:  2.18  it add relro as default
The file is compile with CFLAGS -fPIE and LDFLAGS -pie -z now
Comment 1 Magnus Granberg 2009-01-28 22:16:48 UTC
Created attachment 17205 [details]
gre compile with gcc 4.3 and -fPIE  -save-temps  gre.i

The .i file of gre thats compile with gcc 4.3.2 and CFLAGS -fPIE  LDFLAGS -pie
Comment 2 H.J. Lu 2009-01-29 00:47:37 UTC
[hjl@gnu-6 gcc]$ cat /tmp/i.i
inline void foo ();

void
bar ()
{
  foo ();
}
[hjl@gnu-6 gcc]$ ./xgcc -B./  -S /tmp/i.i -m32 -fPIC 
[hjl@gnu-6 gcc]$ cat i.s
	.file	"i.i"
	.text
.globl bar
	.type	bar, @function
bar:
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%ebx
	subl	$4, %esp
	call	__i686.get_pc_thunk.bx
	addl	$_GLOBAL_OFFSET_TABLE_, %ebx
	call	foo@PLT
	addl	$4, %esp
	popl	%ebx
	popl	%ebp
	ret
	.size	bar, .-bar
Comment 3 H.J. Lu 2009-01-29 00:48:12 UTC
[hjl@gnu-6 gcc]$ ./xgcc -B./ -fpie  -S /tmp/i.i -m32 -fpie
[hjl@gnu-6 gcc]$ cat i.s
	.file	"i.i"
	.text
.globl bar
	.type	bar, @function
bar:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$8, %esp
	call	foo
	leave
	ret
	.size	bar, .-bar
Comment 4 Andrew Pinski 2009-01-29 00:50:26 UTC
I don't see the issue with using the non PLT with -fPIE.
Comment 5 Andrew Pinski 2009-01-29 00:52:01 UTC
Why do you think this is a bug?

PowerPC32-linux-gnu produces:
        bl foo@local


Comment 6 Andrew Pinski 2009-01-29 00:58:31 UTC
Since default_binds_local_p_1 returns true as shlib is false for -fPIE, I still don't see any issues here except for the fact the linker not doing the correct thing.
Comment 7 H.J. Lu 2009-01-29 01:13:31 UTC
Linker does complain:

[hjl@gnu-6 gcc]$ cat /tmp/i.i
inline void foo ();

int
main ()
{
  foo ();
  return 0;
}
[hjl@gnu-6 gcc]$ gcc  /tmp/i.i  -fpie -pie 
/tmp/ccOgMGls.o: In function `main':
i.i:(.text+0xa): undefined reference to `foo'
/usr/local/bin/ld: /tmp/ccOgMGls.o: relocation R_X86_64_PC32 against undefined symbol `foo' can not be used when making a shared object; recompile with -fPIC
/usr/local/bin/ld: final link failed: Bad value
collect2: ld returned 1 exit status
[hjl@gnu-6 gcc]$ 
Comment 8 H.J. Lu 2009-01-29 01:46:17 UTC
This patch:

---
Index: varasm.c
===================================================================
--- varasm.c	(revision 5094)
+++ varasm.c	(working copy)
@@ -6321,6 +6321,11 @@ default_binds_local_p_1 (const_tree exp,
 	   && (DECL_INITIAL (exp) == NULL
 	       || DECL_INITIAL (exp) == error_mark_node))
     local_p = false;
+  /* Functions marked inline without body are not local.  */
+  else if (TREE_CODE (exp) == FUNCTION_DECL
+	   && DECL_DECLARED_INLINE_P (exp)
+	   && DECL_INITIAL (exp) == NULL)
+    local_p = false;
   /* Otherwise we're left with initialized (or non-common) global data
      which is of necessity defined locally.  */
   else
---

works for me
Comment 9 H.J. Lu 2009-01-29 04:04:27 UTC
A patch is posted at

http://gcc.gnu.org/ml/gcc-patches/2009-01/msg01423.html
Comment 10 Richard Biener 2009-01-29 09:56:29 UTC
No compiler with -fpie support manages to do this "correct".  Not a regression.

IMHO this is invalid.  6.7.4/6

"... If a function is declared with an inline function specifier, then it
shall also be defined in the same translation unit."

which is obviously not the case here.  That is C99 of course, so you might
argue that for GNU C89 the situation should be different, but then the
frontend should properly mark the declaration extern.
Comment 11 Magnus Granberg 2009-01-29 15:03:27 UTC
I don't get the link error with gcc 4.2.4 on the code in comment #7
Comment 12 Richard Biener 2009-01-29 15:20:19 UTC
My testcase is

> cat t2.c
void foo() {}

> cat t.c
inline void foo ();
int main ()
{
  foo ();
  return 0;
}

which works perfectly fine even with 4.3 and 4.4 if I build both t2.c and t.c
with -fpie and fails with all compilers supporting -fpie if I only build
t.c with -fpie but t2.c not.

They bind locally with 4.3 and 4.4 though.
Comment 13 H.J. Lu 2009-01-29 15:52:36 UTC
(In reply to comment #12)
> My testcase is
> 
> > cat t2.c
> void foo() {}

The problem happens when t2.c is in a shared library.

> > cat t.c
> inline void foo ();
> int main ()
> {
>   foo ();
>   return 0;
> }
> 
> which works perfectly fine even with 4.3 and 4.4 if I build both t2.c and t.c
> with -fpie and fails with all compilers supporting -fpie if I only build
> t.c with -fpie but t2.c not.
> 

We can argue that this code is invalid and gcc shouldn't accept it
in the first place.  But from user perspective, gcc 4.3 doesn't work
on their codes any more silently.
Comment 14 H.J. Lu 2009-01-29 15:53:41 UTC
Created attachment 17211 [details]
A patch

This patch only checks

--- gcc/varasm.c.pie	2008-11-30 08:49:54.000000000 -0800
+++ gcc/varasm.c	2009-01-29 07:50:46.000000000 -0800
@@ -6321,6 +6321,10 @@ default_binds_local_p_1 (const_tree exp,
 	   && (DECL_INITIAL (exp) == NULL
 	       || DECL_INITIAL (exp) == error_mark_node))
     local_p = false;
+  /* Functions without body are not local.  */
+  else if (TREE_CODE (exp) == FUNCTION_DECL
+	   && DECL_INITIAL (exp) == NULL)
+    local_p = false;
   /* Otherwise we're left with initialized (or non-common) global data
      which is of necessity defined locally.  */
   else
Comment 15 Magnus Granberg 2009-01-29 18:23:57 UTC
We have this in the shared library that is compile with -fPIC
inline u_int32_t
libnet_getgre_length(u_int16_t fv)
{
code
}
And the app have 
code
len = libnet_getgre_length(gre_flags);
    size += len;
code
Comment 16 Jakub Jelinek 2009-01-30 17:31:48 UTC
Patch to set DECL_EXTERNAL instead:
http://gcc.gnu.org/ml/gcc-patches/2009-01/msg01525.html
Comment 17 Jakub Jelinek 2009-01-30 20:47:08 UTC
Subject: Bug 39013

Author: jakub
Date: Fri Jan 30 20:46:32 2009
New Revision: 143803

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=143803
Log:
	PR target/39013
	* c-decl.c (pop_scope): Set DECL_EXTERNAL for functions declared
	inline but never defined.

	* gcc.target/i386/pr39013-1.c: New test.
	* gcc.target/i386/pr39013-2.c: New test.

Added:
    trunk/gcc/testsuite/gcc.target/i386/pr39013-1.c
    trunk/gcc/testsuite/gcc.target/i386/pr39013-2.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/c-decl.c
    trunk/gcc/testsuite/ChangeLog

Comment 18 Jakub Jelinek 2009-01-30 20:50:04 UTC
Fixed on the trunk so far.
Comment 19 Magnus Granberg 2009-01-30 22:59:35 UTC
Created attachment 17218 [details]
fixed for gcc 4.3.3

Thanx for the help
The patch fix the error i have.
Comment 20 Magnus Granberg 2009-03-31 02:37:45 UTC
Created attachment 17565 [details]
Got ICE when compile some code with the old patch.

See http://bugs.gentoo.org/show_bug.cgi?id=262567 for ICE
when compiling.
Comment 21 Richard Biener 2009-06-18 14:50:06 UTC
Testing a backport.
Comment 22 Richard Biener 2009-06-19 12:23:31 UTC
Subject: Bug 39013

Author: rguenth
Date: Fri Jun 19 12:23:16 2009
New Revision: 148700

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=148700
Log:
2009-06-19  Richard Guenther  <rguenther@suse.de>

	Backport from mainline:
	2009-04-22  Jakub Jelinek  <jakub@redhat.com>

	PR c/39855
	* fold-const.c (fold_binary) <case LSHIFT_EXPR>: When optimizing
	into 0, use omit_one_operand.

	* gcc.dg/torture/pr39855.c: New test.

	2009-01-30  Jakub Jelinek  <jakub@redhat.com>

	PR target/39013
	* c-decl.c (pop_scope): Set DECL_EXTERNAL for functions declared
	inline but never defined.
 
	* gcc.target/i386/pr39013-1.c: New test.
	* gcc.target/i386/pr39013-2.c: New test.

Added:
    branches/gcc-4_3-branch/gcc/testsuite/gcc.dg/torture/pr39855.c
    branches/gcc-4_3-branch/gcc/testsuite/gcc.target/i386/pr39013-1.c
    branches/gcc-4_3-branch/gcc/testsuite/gcc.target/i386/pr39013-2.c
Modified:
    branches/gcc-4_3-branch/gcc/ChangeLog
    branches/gcc-4_3-branch/gcc/c-decl.c
    branches/gcc-4_3-branch/gcc/fold-const.c
    branches/gcc-4_3-branch/gcc/testsuite/ChangeLog

Comment 23 Richard Biener 2009-06-19 12:24:20 UTC
Fixed.