Bug 14346 - [3.3 only] With -fpic/-fPIC, thunks jump through PLT, not directly to thunked function
Summary: [3.3 only] With -fpic/-fPIC, thunks jump through PLT, not directly to thunked...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 3.2.1
: P1 critical
Target Milestone: 3.3.4
Assignee: Hans-Peter Nilsson
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2004-02-29 16:12 UTC by Hans-Peter Nilsson
Modified: 2004-10-30 21:10 UTC (History)
1 user (show)

See Also:
Host:
Target: cris-axis-linux-gnu
Build:
Known to work: 3.4.0 4.0.0
Known to fail: 3.2.1 3.3.3
Last reconfirmed: 2004-02-29 16:14:01


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hans-Peter Nilsson 2004-02-29 16:12:33 UTC
As in summary.  Because thunks do not set up the PIC register, going through the
PLT causes a SEGV in best case.  Another PIC construct than for normal calls
must be used, but unfortunately the only (currently) available PC-relative PIC
reloc for cris-axis-linux-gnu requests a PLT.  The thunked function is global. 
Using a local symbol makes the linker omit the redirection through the PLT and
the jump goes directly to the right place.

On trunk and 3.4 branch, the thunk uses a local symbol (equated to the thunked
function, generated by generic code), so TRT happens.
See <URL:http://gcc.gnu.org/ml/gcc-patches/2003-06/msg02603.html> for
the first of the patch where the generic local-thunk was implemented (needed
follow-up patches for correctness).  I'll go with a target-local patch.

Testcase:
-------------- th1.h
class B
{
  int bb1;
  int bb2;
 public:
  virtual void b();
};
extern class B* mka();
-------------- th1.cc (compile and link with -fpic -shared -o libth1.so)
#include "th1.h"
class A
{
  int aa;
public:
  virtual void a() = 0;
};

class C : public A, public B
{
  int cc;
public:
  virtual void b();
  virtual void a();
};

extern "C" void abort (void);
extern "C" void exit (int);

class B* mka (void)
{
  return new C;
}

void C::b(void)
{
  exit (0);
}

void B::b(void)
{
  abort ();
}

void C::a(void)
{
  abort ();
}
-------------- m.cc (compile and link with -L. -lth1, expect normal exit)

#include "th1.h"
extern "C" void abort (void);
int ii[10];
int main(void)
{
  register int picreg
#if defined (__CRIS__) && !defined (__PIC__) && !defined (__pic__)
    __asm__ ("r0")
#endif
    = -1;
  class B *bb = mka();
  bb->b();
  // Make sure picreg is alive and used after bb->b() above.
  asm ("" : : "r" (picreg));
  abort ();
}
Comment 1 GCC Commits 2004-02-29 21:46:31 UTC
Subject: Bug 14346

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_3-branch
Changes by:	hp@gcc.gnu.org	2004-02-29 21:46:29

Modified files:
	gcc            : ChangeLog 
	gcc/config/cris: cris.c 

Log message:
	PR target/14346
	* config/cris/cris.c (cris_asm_output_mi_thunk): For PIC, prepend
	TAB to jumping "add.d" insn.  Avoid PLT indirection by equating a
	local symbol and jumping through it.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.16114.2.913&r2=1.16114.2.914
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/config/cris/cris.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.32.4.2&r2=1.32.4.3

Comment 2 Hans-Peter Nilsson 2004-02-29 21:52:58 UTC
See <URL:http://gcc.gnu.org/ml/gcc-patches/2004-02/msg02798.html>.