This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

c++/7320: g++ 3.2 relocation problem


>Number:         7320
>Category:       c++
>Synopsis:       g++ 3.2 relocation problem
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Mon Jul 15 16:46:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     scott snyder
>Release:        3.2 20020715 (experimental)
>Organization:
>Environment:
System: Linux karma 2.4.9-13 #1 Tue Oct 30 20:11:04 EST 2001 i686 unknown
Architecture: i686

	
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../egcs/configure --prefix=/usr/local/egcs --enable-threads=posix --enable-long-long --enable-languages=c,c++,f77 : (reconfigured)  : (reconfigured)  : (reconfigured)  : (reconfigured)  : (reconfigured)  : (reconfigured)  : (reconfigured)  : (reconfigured)  : (reconfigured) 
>Description:


In recent versions of gcc, i've been having difficulties
building shared libraries.  Sometimes, when i try to use a type
from the library in a dynamic_cast, i get a link warning about
an unresolved relocation, and the dynamic_cast fails at run time ---
it looks like in the call to the dynamic_cast, the argument that
is supposed to be the typeinfo for the destination gets pointed
at the beginning of the shared library from which it comes.

To be complete, i should mention that the as and ld versions i'm using
are as follows:

GNU assembler 2.12.90.0.14 20020627
GNU ld version 2.12.90.0.14 20020627

I have seen the same problem with different binutils versions, though.


An example (consisting of three source files) is below.
Here's how i build it:

$ g++ -fPIC -c y4.cc
$ g++ -fPIC -shared -o yy.so y4.o
$ g++ -g -fPIC y3.cc -o y3 yy.so 
/usr/local/egcs/bin/ld: /tmp/ccqQJ3Fn.o(.text+0x41): unresolvable relocation against symbol `typeinfo for xtype'
/usr/local/egcs/bin/ld: /tmp/ccqQJ3Fn.o(.text+0x4b): unresolvable relocation against symbol `typeinfo for xatomic'
$

Note the relocation errors for the typeinfo objects.

It then crashes if i try to run it:

$ ./y3
Segmentation fault
$


Older versions of gcc (from about a month ago) do not show this behavior.

By a binary search, i found the change which introduced this problem:


2002-06-30  Nathan Sidwell  <nathan@codesourcery.com>

	* cp-tree.h (CPTI_TINFO_DECL_TYPE): Replace with ...
	(CPTI_TYPE_INFO_PTR_TYPE): ... this.
	(tinfo_decl_type): Replace with ...
	(type_info_ptr_type): ... this.
	(import_export_tinfo): Declare.
	(tinfo_decl_p): Rename to ...
	(unemitted_tinfo_decl_p): ... this.
	* decl2.c (import_export_decl): Break out tinfo handling into ...
	(import_export_tinfo): ... here. New function.
	(finish_file): Adjust.
	* rtti.c (TINFO_REAL_NAME): New macro.
	(init_rtti_processing): Create the tinfo types.
	(get_tinfo_decl_dynamic): Use type_info_ptr_type, get_tinfo_ptr.
	(get_tinfo_decl): Adjust.
	(get_tinfo_ptr): New function.
	(get_type_id): Use it.
	(tinfo_base_init): Create vtable decl here, if it doesn't exist.
	(ptr_initializer): Use get_tinfo_ptr.
	(ptm_initializer): Likewise.
	(synthesize_tinfo_var): Break into ...
	(get_pseudo_ti_init): ... this. Just create the initializer.
	(get_pseudo_ti_desc): .. and this.
	(create_real_tinfo_var): Remove.
	(create_pseudo_type_info): Don't create the vtable decl here.
	(get_vmi_pseudo_type_info): Remove.
	(create_tinfo_types): Adjust.
	(tinfo_decl_p): Rename to ...
	(unemitted_tinfo_decl_p): ... here. Adjust.
	(emit_tinfo_decl): Adjust. Create the initializer.



I found that if i make this change, the problem goes away.
This is probably not the correct fix, though.



Index: rtti.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/rtti.c,v
retrieving revision 1.138
diff -u -p -r1.138 rtti.c
--- rtti.c      5 Jul 2002 10:40:47 -0000       1.138
+++ rtti.c      15 Jul 2002 23:31:47 -0000
@@ -342,6 +342,7 @@ get_tinfo_decl (type)
       TREE_STATIC (d) = 1;
       DECL_EXTERNAL (d) = 1;
       SET_DECL_ASSEMBLER_NAME (d, name);
+      DECL_COMDAT (d) = 1;
       cp_finish_decl (d, NULL_TREE, NULL_TREE, 0);
 
       pushdecl_top_level (d);



>How-To-Repeat:


- y3.cc --------------------------------------------------------
#include "y.hh"
extern "C" int printf (...);

int main ()
{
  xtype* p = get_atomic_type ();
  xatomic* pp = dynamic_cast<xatomic*> (p);
  printf ("%x\n", pp);
  return 0;
}
- y4.cc --------------------------------------------------------
#include "y.hh"


xatomic* get_atomic_type ()
{
  return new xatomic ();
}

xatomic::~xatomic () {}
xtype::~xtype () {}
- y.hh ---------------------------------------------------------
#ifndef Y_HH
#define Y_HH


struct xtype
{
  virtual ~xtype ();
};

struct xatomic
  : public xtype
{
  ~xatomic ();
};

xatomic* get_atomic_type ();


#endif
----------------------------------------------------------------


>Fix:
	
>Release-Note:
>Audit-Trail:
>Unformatted:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]