Bug 27657 - [4.2 regression] bogus undefined reference error to static var with -g and -O
Summary: [4.2 regression] bogus undefined reference error to static var with -g and -O
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.2.0
: P1 normal
Target Milestone: 4.2.0
Assignee: Richard Biener
URL:
Keywords: link-failure, monitored
: 27950 28673 29417 30305 30629 30651 (view as bug list)
Depends on:
Blocks: 27950 29417
  Show dependency treegraph
 
Reported: 2006-05-18 13:34 UTC by Martin Michlmayr
Modified: 2007-02-04 17:52 UTC (History)
15 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.1.1 4.1.2 4.3.0
Known to fail:
Last reconfirmed: 2007-01-29 15:43:49


Attachments
patch (4.00 KB, patch)
2006-08-07 11:59 UTC, Jan Hubicka
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Michlmayr 2006-05-18 13:34:55 UTC
I get an "undefined reference" error when linking two files but that variable is static.  This only happens with gcc 4.2, and only when I enable both -g and -O1 (or higher).

(sid)473:tbm@reyes: ~] cat test.c
static const char utf8_skip_data[1] = {1};
static const char * const gconf_g_utf8_skip = utf8_skip_data;
(sid)474:tbm@reyes: ~] cat main.c
int main() {
}
(sid)475:tbm@reyes: ~] gcc -c -o main.o main.c
(sid)476:tbm@reyes: ~] gcc -c -g -O2 -o test.o test.c
(sid)477:tbm@reyes: ~] gcc main.o test.o
test.o:(.debug_info+0x93): undefined reference to `utf8_skip_data'
collect2: ld returned 1 exit status
zsh: exit 1     gcc main.o test.o
(sid)478:tbm@reyes: ~] gcc-4.1 -c -g -O2 -o test.o test.c
(sid)479:tbm@reyes: ~] gcc main.o test.o
(sid)480:tbm@reyes: ~] gcc -c -g -O0 -o test.o test.c
(sid)481:tbm@reyes: ~] gcc main.o test.o
(sid)482:tbm@reyes: ~]

This is with 4.2.0 20060419.  20060517 shows the same.
Comment 1 Alan Modra 2006-05-29 03:07:05 UTC
Confirmed.  The reference is the relocation for DW_AT_const_value.
$ readelf -wi test.o
[snip]
 <1><7a>: Abbrev Number: 8 (DW_TAG_variable)
     DW_AT_name        : (indirect string, offset: 0x0): gconf_g_utf8_skip
     DW_AT_decl_file   : 1
     DW_AT_decl_line   : 2
     DW_AT_type        : <89>
     DW_AT_const_value : 0
[snip]
$ readelf -r test.o
Relocation section '.rela.debug_info' at offset 0x53c contains 8 entries:
[snip]
00000085  00000b01 R_PPC_ADDR32      00000000   utf8_skip_data + 0
Comment 2 Andrew Pinski 2006-06-29 19:21:50 UTC
*** Bug 27950 has been marked as a duplicate of this bug. ***
Comment 3 Jan Hubicka 2006-08-07 11:59:24 UTC
Created attachment 12037 [details]
patch

It looks like we should bite the bullet and let cgraph code to output the debug
info....  I am testing the attached patch

Honza
Comment 4 Andrew Pinski 2006-08-10 07:35:57 UTC
*** Bug 28673 has been marked as a duplicate of this bug. ***
Comment 5 Andrew Pinski 2006-08-27 22:32:58 UTC
Janis,
  Can you do a regression hunt on this bug?
I almost think it was caused by revision 112408.
Comment 6 Janis Johnson 2006-08-28 19:43:40 UTC
A regression hunt on powerpc-linux identified the following patch:

    http://gcc.gnu.org/viewcvs?view=rev&rev=112361

    r112361 | geoffk | 2006-03-24 21:59:48 +0000 (Fri, 24 Mar 2006)

The log message and the ChangeLog entry don't mention it, but this patch also modifies gcc/dwarf2out.c.
Comment 7 Andrew Pinski 2006-08-28 20:36:10 UTC
(In reply to comment #6)
> The log message and the ChangeLog entry don't mention it, but this patch also
> modifies gcc/dwarf2out.c.

It was backed out right after the commit:
http://gcc.gnu.org/viewcvs?view=rev&revision=112362

But that would mean it was caused by the revision which I had mentioned in comment #5 as that revision put the code back in.

Comment 8 Geoff Keating 2006-08-28 20:44:45 UTC
(In reply to comment #3)
> Created an attachment (id=12037) [edit]
> patch
> 
> It looks like we should bite the bullet and let cgraph code to output the debug
> info....  I am testing the attached patch
> 
It looks like this patch will prevent the output of debug information for variables that are optimized out, but that's wrong; the variables should be visible to the user in the debugger even if they are optimized away.

I think the actual problem is that reference_to_unused in dwarf2out.c is checking TREE_USED and expecting all used variables to be output, but that doesn't seem to be happening in this case.  Perhaps there is something in cgraph which can be called to ask if a variable is *really* going to be output?
Comment 9 Janis Johnson 2006-08-28 22:58:01 UTC
I can confirm that the latest regression is from r112408.
Comment 10 Andrew Pinski 2006-10-20 07:42:06 UTC
*** Bug 29417 has been marked as a duplicate of this bug. ***
Comment 11 Volker Reichelt 2006-12-11 20:23:02 UTC
Btw, the following C++ example fails to link even without -O:

=============================
const char s[] = "";
const char *const p = s;

int main()
{
  return 0;
}
=============================
Comment 12 Andrew Pinski 2006-12-26 17:37:35 UTC
*** Bug 30305 has been marked as a duplicate of this bug. ***
Comment 13 Richard Biener 2007-01-29 15:43:49 UTC
I'm testing

Index: dwarf2out.c
===================================================================
--- dwarf2out.c (revision 121287)
+++ dwarf2out.c (working copy)
@@ -10045,8 +10045,14 @@ reference_to_unused (tree * tp, int * wa
   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
       && ! TREE_ASM_WRITTEN (*tp))
     return *tp;
-  else
-    return NULL_TREE;
+  else if (DECL_P (*tp))
+    {
+      struct varpool_node *node = varpool_node (*tp);
+      if (!node->needed)
+       return *tp;
+    }
+
+  return NULL_TREE;
 }
 
 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
Comment 14 Richard Biener 2007-01-29 15:49:53 UTC
*** Bug 30629 has been marked as a duplicate of this bug. ***
Comment 15 Richard Biener 2007-01-30 10:23:26 UTC
Subject: Bug 27657

Author: rguenth
Date: Tue Jan 30 10:23:01 2007
New Revision: 121335

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=121335
Log:
2007-01-30  Richard Guenther  <rguenther@suse.de>

	PR middle-end/27657
	* dwarf2out.c (reference_to_unused): Query varpool if the
	variable was output.

	* g++.dg/debug/pr27657.C: New testcase.

Added:
    trunk/gcc/testsuite/g++.dg/debug/pr27657.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/dwarf2out.c
    trunk/gcc/testsuite/ChangeLog

Comment 16 Richard Biener 2007-01-30 10:23:46 UTC
Fixed on the mainline.
Comment 17 Richard Biener 2007-01-31 12:28:28 UTC
*** Bug 30651 has been marked as a duplicate of this bug. ***
Comment 18 İsmail Dönmez 2007-02-01 19:25:12 UTC
Any chance of a 4.2 backport ? Looks like varpool_node does not exist in 4.2 branch.
Comment 19 Martin Michlmayr 2007-02-01 19:32:21 UTC
(In reply to comment #18)
> Any chance of a 4.2 backport ? Looks like varpool_node does not exist in 4.2
> branch.

I noticed this as well.  It's easy enough to fix:

14:20 < richi> tbm: sure s/varpool_/cgraph_varpool_/

Richi has already tested the patch on 4.2 so I assume he'll apply it soon.
Comment 20 İsmail Dönmez 2007-02-01 19:51:00 UTC
(In reply to comment #19)
> (In reply to comment #18)
> > Any chance of a 4.2 backport ? Looks like varpool_node does not exist in 4.2
> > branch.
> 
> I noticed this as well.  It's easy enough to fix:
> 
> 14:20 < richi> tbm: sure s/varpool_/cgraph_varpool_/
> 
> Richi has already tested the patch on 4.2 so I assume he'll apply it soon.

Nice, thanks :)

Comment 21 Richard Biener 2007-02-04 15:28:03 UTC
Subject: Bug 27657

Author: rguenth
Date: Sun Feb  4 15:27:53 2007
New Revision: 121576

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=121576
Log:
2007-02-04  Richard Guenther  <rguenther@suse.de>

	Backport from mainline:
	2007-01-30  Richard Guenther  <rguenther@suse.de>

	PR middle-end/27657
	* dwarf2out.c (reference_to_unused): Query varpool if the
	variable was output.

	* g++.dg/debug/pr27657.C: New testcase.

Added:
    branches/gcc-4_2-branch/gcc/testsuite/g++.dg/debug/pr27657.C
      - copied unchanged from r121335, trunk/gcc/testsuite/g++.dg/debug/pr27657.C
Modified:
    branches/gcc-4_2-branch/gcc/ChangeLog
    branches/gcc-4_2-branch/gcc/dwarf2out.c
    branches/gcc-4_2-branch/gcc/testsuite/ChangeLog

Comment 22 Richard Biener 2007-02-04 15:29:43 UTC
Fixed.
Comment 23 Martin Michlmayr 2007-02-04 17:52:00 UTC
(In reply to comment #22)
> Fixed.

I can confirm this - with the exception of one case for which I filed a new report: PR30700.