Bug 26984 - link error with &(typeid(int)) in anonymous namespace
Summary: link error with &(typeid(int)) in anonymous namespace
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.2.0
: P3 normal
Target Milestone: 4.2.0
Assignee: Jason Merrill
URL:
Keywords: link-failure, visibility
Depends on:
Blocks: 26989 19664 21581 27000
  Show dependency treegraph
 
Reported: 2006-04-02 05:27 UTC by Rob Peters
Modified: 2006-06-30 15:29 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-04-07 06:43:48


Attachments
test case exposing the link error with -fpic with 4.2/20060401 (148 bytes, text/plain)
2006-04-02 05:29 UTC, Rob Peters
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Rob Peters 2006-04-02 05:27:18 UTC
Using g++ from 4.2.0 daily snapshot 20060401:

$ ~/local/gcc-4.2-20060401/bin/g++  -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: /lab/rjpeters/build/gcc-4.2-20060401/configure --enable-languages=c,c++ --prefix=/lab/rjpeters/local/gcc-4.2-20060401 --enable-shared --enable-threads=posix --with-system-zlib --enable-__cxa_atexit
Thread model: posix
gcc version 4.2.0 20060401 (experimental)

I get a linker error when I try to compile and link the following into a shared library. The link error only happens when I use -fpic, and it happens only when I take the address of typeid(int) inside an anonymous namespace; no linker error if I take the address in the global namespace or in a named namespace.

$ cat test.C

#include <typeinfo>

void g(const std::type_info&);

namespace { // <--- change to 'namespace X' and the error disappears
  const std::type_info* t = &(typeid(int));
}

template <class T>
struct C
{
  virtual void foo() const {
    g(typeid(int));
  }
};

C<void> c;

$ ~/local/gcc-4.2-20060401/bin/g++ -c test.C -fpic -o test.o

$ ~/local/gcc-4.2-20060401/bin/g++ -shared test.o -o test.so
/usr/bin/ld: test.o(.gnu.linkonce.t._ZNK1CIvE3fooEv+0x14): unresolvable relocation against symbol `typeinfo for int@@CXXABI_1.3'
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: ld returned 1 exit status

Not entirely sure which part of the toolchain this relates to, but I don't see the error with g++ 3.4.4 or 4.1.0 so I'm guessing there's a chance it's compiler-related.

If I compile test.C->test.o both with 3.4.4 and 4.2.0 (20060401), then diff the outputs of 'nm test.o | c++filt', I get this:

$ diff nm-3.4.4 nm-4.2
3c3
< 0000003a t global constructors keyed to _ZN35_GLOBAL__N_test.C_DFF67DD7_A20029871tE
---
> 0000003c t global constructors keyed to _ZN35_GLOBAL__N_test.C_00000000_CD2145311tE
14a15,16
> 00000000 T __i686.get_pc_thunk.bx
> 00000000 T __i686.get_pc_thunk.cx

Thanks,
Rob
Comment 1 Rob Peters 2006-04-02 05:29:11 UTC
Created attachment 11183 [details]
test case exposing the link error with -fpic with 4.2/20060401
Comment 2 Andrew Pinski 2006-04-02 08:48:57 UTC
Confirmed, this is a latent bug though.  Reduced testcase which shows the bug even in 4.0.x:
#include <typeinfo>

void g(const std::type_info&);

#pragma GCC visibility push(hidden)
const std::type_info* t = &(typeid(int));
#pragma GCC visibility pop

template <class T>
struct C
{
  virtual void foo() const {
    g(typeid(int));
  }
};
C<void> c;

-----------
And since the anonymous namespace change was introduced for 4.2.0, it makes it harder to produce correct code now.
Comment 3 Andrew Pinski 2006-04-02 16:06:16 UTC
Woops wrong person, sorry Nathan.
Comment 4 Andrew Pinski 2006-04-07 05:26:42 UTC
This is no longer a regression but it still is a bug as shown by comment #2.
Comment 5 Jason Merrill 2006-06-30 01:16:27 UTC
Subject: Bug 26984

Author: jason
Date: Fri Jun 30 01:15:56 2006
New Revision: 115086

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=115086
Log:
        PR c++/26905
        PR c++/26612
        PR c++/27000
        PR c++/26984
        PR c++/19134
        * tree.c (build_decl_stat): Don't hande #pragma visibility here.
        * c-common.c (c_determine_visibility): Handle it here.
        * c-decl.c (finish_decl): Call c_determine_visibility for
        functions, too.
        * flags.h (enum symbol_visibility): Sort from most to least visibility.
        * tree.h: Likewise.
        * varasm.c (default_assemble_visibility): Likewise.
        * c-common.c (handle_visibility_attribute): Complain about trying
        to give visibility to an already defined class, or trying to change
        declared visibility. Always attach the attribute.
        * cp/decl2.c (determine_visibility): Overhaul.
        (determine_visibility_from_class): Likewise.
        (min_vis_r, type_visibility, constrain_visibility): New fns.
        (constrain_visibility_for_template): Likewise.
        (constrain_class_visibility): Likewise.
        * cp/decl.c (cp_finish_decl): Call determine_visibility for function
        decls, too.
        * cp/name-lookup.c (pushtag): Call determine_visibility.
        * cp/decl.c (duplicate_decls): Don't copy visibility from template to
        specialization.
        * cp/pt.c (check_explicit_specialization): Likewise.
        (lookup_template_class, tsubst_decl): Call determine_visibility.
        * cp/class.c (finish_struct_1): Call constrain_class_visibility.

        PR c++/26905
        PR c++/21675
        PR c++/17470
        * cp/parser.c (cp_parser_explicit_instantiation): Pass the attributes
        to grokdeclarator.
        (cp_parser_type_specifier): Allow 'enum __attribute ((...)) E'.
        (cp_parser_enum_specifier): Likewise.
        (cp_parser_elaborated_type_specifier): Apply attributes if this
        declares only the class.
        (cp_parser_class_specifier): Apply leading attributes immediately.
        * cp/semantics.c (begin_class_definition): Add attributes parameter,
        apply them to the type.
        * attribs.c (decl_attributes): Ignore type-in-place attributes
        once the type has been defined.

        PR c++/21581
        PR c++/25915
        * cp/tree.c (decl_anon_ns_mem_p): New function.
        * cp/cp-tree.h: Declare it.
        * cp/decl2.c (determine_visibility): Make anonymous namespace
        members static.
        (min_vis_r, constrain_visibility): Likewise.
        * cp/rtti.c (create_pseudo_type_info): Set TREE_PUBLIC on
        pseudo-types.
        * cp/decl.c (cxx_init_decl_processing): Set TREE_PUBLIC on
        global_namespace.
        * cp/name-lookup.c (push_namespace_with_attribs): Don't set TREE_PUBLIC
        on anonymous namespaces.

Added:
    trunk/gcc/testsuite/g++.dg/ext/visibility/anon2.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/class1.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/prop1.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/redecl1.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/template1.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/template2.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/template3.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/template4.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/typeinfo1.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/warn1.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/warn2.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/warn3.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/warn4.C
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/attribs.c
    trunk/gcc/c-common.c
    trunk/gcc/c-decl.c
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/class.c
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/decl.c
    trunk/gcc/cp/decl2.c
    trunk/gcc/cp/name-lookup.c
    trunk/gcc/cp/parser.c
    trunk/gcc/cp/pt.c
    trunk/gcc/cp/rtti.c
    trunk/gcc/cp/semantics.c
    trunk/gcc/cp/tree.c
    trunk/gcc/doc/extend.texi
    trunk/gcc/doc/invoke.texi
    trunk/gcc/flags.h
    trunk/gcc/testsuite/g++.dg/ext/attrib14.C
    trunk/gcc/testsuite/g++.dg/ext/attrib9.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/anon1.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/assign1.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/fvisibility-override2.C
    trunk/gcc/testsuite/g++.dg/ext/visibility/virtual.C
    trunk/gcc/testsuite/g++.old-deja/g++.pt/enum5.C
    trunk/gcc/tree.c
    trunk/gcc/tree.h
    trunk/gcc/varasm.c

Comment 6 Andrew Pinski 2006-06-30 15:29:22 UTC
Fixed.
Comment 7 Jakub Jelinek 2006-08-28 12:27:11 UTC
Subject: Bug 26984

Author: jakub
Date: Mon Aug 28 12:26:41 2006
New Revision: 116505

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=116505
Log:
2006-07-06  Jason Merrill  <jason@redhat.com>

cp/
	PR c++/28279
	* decl2.c (finish_static_data_member_decl): Don't assert
	TREE_PUBLIC.

2006-07-01  Jason Merrill  <jason@redhat.com>

cp/
	PR c++/28215
	* method.c (make_thunk): Unset DECL_USE_TEMPLATE and 
	DECL_TEMPLATE_INFO.

2006-06-30  Jason Merrill  <jason@redhat.com>

objcp/
	* objcp-decl.c (objcp_start_struct): Pass null attributes argument
	to begin_class_definition.

2006-06-29  Jason Merrill  <jason@redhat.com>

	PR c++/26905
	PR c++/26612
	PR c++/27000
	PR c++/26984
	PR c++/19134
	* tree.c (build_decl_stat): Don't hande #pragma visibility here.
	* c-common.c (c_determine_visibility): Handle it here.
	* c-decl.c (finish_decl): Call c_determine_visibility for 
	functions, too.
	* flags.h (enum symbol_visibility): Sort from most to least visibility.
	* tree.h: Likewise.
	* varasm.c (default_assemble_visibility): Likewise.
	* c-common.c (handle_visibility_attribute): Complain about trying
	to give visibility to an already defined class, or trying to change
	declared visibility. Always attach the attribute.

	PR c++/26905
	PR c++/21675
	PR c++/17470
	* attribs.c (decl_attributes): Ignore type-in-place attributes
	once the type has been defined.
cp/
	PR c++/26905
	PR c++/26612
	PR c++/27000
	PR c++/26984
	PR c++/19134
	* decl2.c (determine_visibility): Overhaul.
	(determine_visibility_from_class): Likewise.
	(min_vis_r, type_visibility, constrain_visibility): New fns.
	(constrain_visibility_for_template): Likewise.
	(constrain_class_visibility): Likewise.
	* decl.c (cp_finish_decl): Call determine_visibility for function
	decls, too.
	* name-lookup.c (pushtag): Call determine_visibility.
	* decl.c (duplicate_decls): Don't copy visibility from template to
	specialization.
	* pt.c (check_explicit_specialization): Likewise.
	(lookup_template_class, tsubst_decl): Call determine_visibility.
	* class.c (finish_struct_1): Call constrain_class_visibility.

	PR c++/26905
	PR c++/21675
	PR c++/17470
	* parser.c (cp_parser_explicit_instantiation): Pass the attributes
	to grokdeclarator.
	(cp_parser_type_specifier): Allow 'enum __attribute ((...)) E'.
	(cp_parser_enum_specifier): Likewise.
	(cp_parser_elaborated_type_specifier): Apply attributes if this
	declares only the class.
	(cp_parser_class_specifier): Apply leading attributes immediately.
	* semantics.c (begin_class_definition): Add attributes parameter,
	apply them to the type.

	PR c++/21581
	PR c++/25915
	* tree.c (decl_anon_ns_mem_p): New function.
	* cp-tree.h: Declare it.
	* decl2.c (determine_visibility): Make anonymous namespace
	members static.
	(min_vis_r, constrain_visibility): Likewise.
	* rtti.c (create_pseudo_type_info): Set TREE_PUBLIC on
	pseudo-types.
	* decl.c (cxx_init_decl_processing): Set TREE_PUBLIC on
	global_namespace.
	* name-lookup.c (push_namespace_with_attribs): Don't set TREE_PUBLIC
	on anonymous namespaces.

Added:
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/anon1.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/anon2.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/class1.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/prop1.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/redecl1.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/template1.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/template2.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/template3.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/template4.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/typeinfo1.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/warn1.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/warn2.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/warn3.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/warn4.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/template/anon2.C
Modified:
    branches/redhat/gcc-4_1-branch/gcc/ChangeLog
    branches/redhat/gcc-4_1-branch/gcc/attribs.c
    branches/redhat/gcc-4_1-branch/gcc/c-common.c
    branches/redhat/gcc-4_1-branch/gcc/c-decl.c
    branches/redhat/gcc-4_1-branch/gcc/cp/ChangeLog
    branches/redhat/gcc-4_1-branch/gcc/cp/class.c
    branches/redhat/gcc-4_1-branch/gcc/cp/cp-tree.h
    branches/redhat/gcc-4_1-branch/gcc/cp/decl.c
    branches/redhat/gcc-4_1-branch/gcc/cp/decl2.c
    branches/redhat/gcc-4_1-branch/gcc/cp/method.c
    branches/redhat/gcc-4_1-branch/gcc/cp/name-lookup.c
    branches/redhat/gcc-4_1-branch/gcc/cp/parser.c
    branches/redhat/gcc-4_1-branch/gcc/cp/pt.c
    branches/redhat/gcc-4_1-branch/gcc/cp/rtti.c
    branches/redhat/gcc-4_1-branch/gcc/cp/semantics.c
    branches/redhat/gcc-4_1-branch/gcc/cp/tree.c
    branches/redhat/gcc-4_1-branch/gcc/doc/extend.texi
    branches/redhat/gcc-4_1-branch/gcc/doc/invoke.texi
    branches/redhat/gcc-4_1-branch/gcc/flags.h
    branches/redhat/gcc-4_1-branch/gcc/objcp/ChangeLog
    branches/redhat/gcc-4_1-branch/gcc/objcp/objcp-decl.c
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/attrib14.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/attrib9.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/assign1.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/fvisibility-override2.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.dg/ext/visibility/virtual.C
    branches/redhat/gcc-4_1-branch/gcc/testsuite/g++.old-deja/g++.pt/enum5.C
    branches/redhat/gcc-4_1-branch/gcc/tree.c
    branches/redhat/gcc-4_1-branch/gcc/tree.h
    branches/redhat/gcc-4_1-branch/gcc/varasm.c