public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Bad code for ia64 with function pointer
@ 2002-08-09  5:49 Henri-Pierre CHARLES
  2002-08-09  5:53 ` Christoph Hellwig
  2002-08-09  8:38 ` Jim Wilson
  0 siblings, 2 replies; 11+ messages in thread
From: Henri-Pierre CHARLES @ 2002-08-09  5:49 UTC (permalink / raw)
  To: gcc, gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1318 bytes --]

Hello,
I have submitted a bug #7349 archived here :
http://gcc.gnu.org/ml/gcc-bugs/2002-07/msg00568.html

The generated code for the program is not correct.It seem that without -O the 
third call are not correct. This code compiler correctly on Solaris/sparc,
FreeBSD/i386, linux/i386.

The 3.0.x branch of gcc has the same bug.

An idea somebody, or a turnaround ?

It seem strange that the intel compiler on redhat has the same bug !
(I have submit a bug report on intel forum without answer now :-( )

H-P

---------------
#include <string.h>
#include <stdio.h>

typedef  int (*pfiii)(int, int);

int add(int a, int b)
{
  return a+b;
} /* add */

int main(int argc, char * argv[])
{
  char codeBuffer1[1024];
  char *codeBuffer2 = (char *) calloc (1024, 1);
  pfiii myFunction;
  (void) printf("%d %d\n", 6, add (6, 36));
  bcopy (add, codeBuffer1, 1024);
  myFunction = (pfiii) codeBuffer1;
  (void) printf("%d %d\n", 7, myFunction(7, 35));
  bcopy (add, codeBuffer2, 1024);
  myFunction = (pfiii) codeBuffer2;
  (void) printf("%d %d\n", 8, myFunction(8, 34));
  free (codeBuffer2);
  return 0;
}
---------------


-- 
% Henri-Pierre.Charles@prism.uvsq.fr PRiSM, Université de Versailles
% Tel: 01 39 25 43 44        45, Av. des États Unis 78000 Versailles
% Il reste -82215720 secondes jusqu'au 01/01/2000

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Bad code for ia64 with function pointer
  2002-08-09  5:49 Bad code for ia64 with function pointer Henri-Pierre CHARLES
@ 2002-08-09  5:53 ` Christoph Hellwig
  2002-08-09  8:24   ` Henri-Pierre CHARLES
  2002-08-09  8:38 ` Jim Wilson
  1 sibling, 1 reply; 11+ messages in thread
From: Christoph Hellwig @ 2002-08-09  5:53 UTC (permalink / raw)
  To: Henri-Pierre CHARLES; +Cc: gcc, gcc-bugs

On Fri, Aug 09, 2002 at 02:49:41PM +0200, Henri-Pierre CHARLES wrote:
> An idea somebody, or a turnaround ?

Read the IA64 ABI..

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Bad code for ia64 with function pointer
  2002-08-09  5:53 ` Christoph Hellwig
@ 2002-08-09  8:24   ` Henri-Pierre CHARLES
  2002-08-09  8:41     ` Christoph Hellwig
  0 siblings, 1 reply; 11+ messages in thread
From: Henri-Pierre CHARLES @ 2002-08-09  8:24 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: Henri-Pierre CHARLES, gcc, gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 682 bytes --]

Hello,

Le Fri, Aug 09, 2002 at 01:53:23PM +0100, Christoph Hellwig écrivait :
> On Fri, Aug 09, 2002 at 02:49:41PM +0200, Henri-Pierre CHARLES wrote:
> > An idea somebody, or a turnaround ?
> Read the IA64 ABI..

Thanks for your answer. But if you think about the "6.3.3 Sellf modifying 
code" section, I have read it. We also write a function to flush the data 
cache, without any success.

But maybe you have another section in mind ? Could you be more explicit ?

Thanks

H-P

-- 
% Henri-Pierre.Charles@prism.uvsq.fr PRiSM, Université de Versailles
% Tel: 01 39 25 43 44        45, Av. des États Unis 78000 Versailles
% Il reste -82225200 secondes jusqu'au 01/01/2000

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Bad code for ia64 with function pointer
  2002-08-09  5:49 Bad code for ia64 with function pointer Henri-Pierre CHARLES
  2002-08-09  5:53 ` Christoph Hellwig
@ 2002-08-09  8:38 ` Jim Wilson
  2002-08-09  8:44   ` Henri-Pierre CHARLES
  2002-08-09 14:11   ` Neil Booth
  1 sibling, 2 replies; 11+ messages in thread
From: Jim Wilson @ 2002-08-09  8:38 UTC (permalink / raw)
  To: Henri-Pierre CHARLES; +Cc: gcc, gcc-bugs

Your program is broken.  You made assumptions about the form of a function
pointer which are wrong.

On IA-64, and some other targets, function pointers are represented
differently than pointers to objects.  On IA-64, a function pointer is a double
word descriptor that holds the function address and the function GP value.
Thus you can't get a valid function pointer by casting a char * pointer to a
function pointer type.

Jim

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Bad code for ia64 with function pointer
  2002-08-09  8:24   ` Henri-Pierre CHARLES
@ 2002-08-09  8:41     ` Christoph Hellwig
  0 siblings, 0 replies; 11+ messages in thread
From: Christoph Hellwig @ 2002-08-09  8:41 UTC (permalink / raw)
  To: Henri-Pierre CHARLES; +Cc: gcc, gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 694 bytes --]

On Fri, Aug 09, 2002 at 05:24:49PM +0200, Henri-Pierre CHARLES wrote:
> Hello,
> 
> Le Fri, Aug 09, 2002 at 01:53:23PM +0100, Christoph Hellwig écrivait :
> > On Fri, Aug 09, 2002 at 02:49:41PM +0200, Henri-Pierre CHARLES wrote:
> > > An idea somebody, or a turnaround ?
> > Read the IA64 ABI..
> 
> Thanks for your answer. But if you think about the "6.3.3 Sellf modifying 
> code" section, I have read it. We also write a function to flush the data 
> cache, without any success.
> 
> But maybe you have another section in mind ? Could you be more explicit ?

Section 5.3.5 Function Addresses

If you read it carefully you'll notice the undefined behaviour you rely on
can't work on ia64.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Bad code for ia64 with function pointer
  2002-08-09  8:38 ` Jim Wilson
@ 2002-08-09  8:44   ` Henri-Pierre CHARLES
  2002-08-09  9:12     ` Jim Wilson
  2002-08-09  9:31     ` David Edelsohn
  2002-08-09 14:11   ` Neil Booth
  1 sibling, 2 replies; 11+ messages in thread
From: Henri-Pierre CHARLES @ 2002-08-09  8:44 UTC (permalink / raw)
  To: Jim Wilson; +Cc: Henri-Pierre CHARLES, gcc, gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 804 bytes --]

Hello,

Le Fri, Aug 09, 2002 at 11:38:43AM -0400, Jim Wilson écrivait :
> Your program is broken.  You made assumptions about the form of a function
> pointer which are wrong.
> On IA-64, and some other targets, function pointers are represented
> differently than pointers to objects.  On IA-64, a function pointer is a double
> word descriptor that holds the function address and the function GP value.
> Thus you can't get a valid function pointer by casting a char * pointer to a
> function pointer type.

Oh ! ok, I understand.

So, it's impossible to do this kind of indirect call with C ?

H-P


-- 
% Henri-Pierre.Charles@prism.uvsq.fr PRiSM, Université de Versailles
% Tel: 01 39 25 43 44        45, Av. des États Unis 78000 Versailles
% Il reste -82226520 secondes jusqu'au 01/01/2000

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Bad code for ia64 with function pointer
  2002-08-09  8:44   ` Henri-Pierre CHARLES
@ 2002-08-09  9:12     ` Jim Wilson
  2002-08-09  9:31     ` David Edelsohn
  1 sibling, 0 replies; 11+ messages in thread
From: Jim Wilson @ 2002-08-09  9:12 UTC (permalink / raw)
  To: Henri-Pierre CHARLES; +Cc: gcc, gcc-bugs

>So, it's impossible to do this kind of indirect call with C ?

I don't know what you are trying to accomplish, so I don't know if there
is a valid way to do it.

Your example is trying to contruct a function pointer from a data address.
This is possible.  However, you need machine dependent code, you need a
good understanding of the ABIs used by your targets, and you need to know
which language rules are safe to violate with which compilers, and when
it is safe to violate them.  In practice, you don't want to do this unless
you have a really good reason to do it.  Glibc for instance has code to do
this, but glibc has a good reason for doing this, it is needed for the ELF
startup files.  See for instance DL_AUTO_FUNCTION_ADDRESS in libc/sysdeps/
ia64/dl-lookupcfg.h.

Jim

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Bad code for ia64 with function pointer
  2002-08-09  8:44   ` Henri-Pierre CHARLES
  2002-08-09  9:12     ` Jim Wilson
@ 2002-08-09  9:31     ` David Edelsohn
  1 sibling, 0 replies; 11+ messages in thread
From: David Edelsohn @ 2002-08-09  9:31 UTC (permalink / raw)
  To: Henri-Pierre CHARLES; +Cc: Jim Wilson, gcc, gcc-bugs

>>>>> Henri-Pierre CHARLES writes:

H-P> So, it's impossible to do this kind of indirect call with C ?

	An IA-64 function pointer is just a special multi-word object.
One can create that multi-word object in C and then use that object as a
pointer.  This means that data pointers and function pointers are not
interchangeable.  As Jim says, the real question is *why* you want to do
this, not how to do this.

David

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Bad code for ia64 with function pointer
  2002-08-09  8:38 ` Jim Wilson
  2002-08-09  8:44   ` Henri-Pierre CHARLES
@ 2002-08-09 14:11   ` Neil Booth
  2002-08-09 14:43     ` Mike Stump
  2002-08-12  8:38     ` Jim Wilson
  1 sibling, 2 replies; 11+ messages in thread
From: Neil Booth @ 2002-08-09 14:11 UTC (permalink / raw)
  To: Jim Wilson; +Cc: Henri-Pierre CHARLES, gcc, gcc-bugs

Jim Wilson wrote:-

> On IA-64, and some other targets, function pointers are represented
> differently than pointers to objects.  On IA-64, a function pointer is a double
> word descriptor that holds the function address and the function GP value.
> Thus you can't get a valid function pointer by casting a char * pointer to a
> function pointer type.

Hi Jim,

What is GP?  I see it written everywhere, but only as "GP".

Neil.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Bad code for ia64 with function pointer
  2002-08-09 14:11   ` Neil Booth
@ 2002-08-09 14:43     ` Mike Stump
  2002-08-12  8:38     ` Jim Wilson
  1 sibling, 0 replies; 11+ messages in thread
From: Mike Stump @ 2002-08-09 14:43 UTC (permalink / raw)
  To: Neil Booth; +Cc: Jim Wilson, Henri-Pierre CHARLES, gcc, gcc-bugs

On Friday, August 9, 2002, at 02:09 PM, Neil Booth wrote:

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Bad code for ia64 with function pointer
  2002-08-09 14:11   ` Neil Booth
  2002-08-09 14:43     ` Mike Stump
@ 2002-08-12  8:38     ` Jim Wilson
  1 sibling, 0 replies; 11+ messages in thread
From: Jim Wilson @ 2002-08-12  8:38 UTC (permalink / raw)
  To: Neil Booth; +Cc: Henri-Pierre CHARLES, gcc, gcc-bugs

GP is the global pointer.  In SVR4-style position independent code (PIC),
all static data is referenced indirectly via the GP.  Inter-module function
calls also indirect through the GP.  The GP points to a table of addresses
which is relocated (initialized) by the run-time loader (ld.so).  This table
is usually called the global offset table (GOT).  This allows the code to
be loaded at any address.  This also allows the code to be shared among
multiple processes, by giving each process its own separate GOT.  Depending
on the ABI, the GP can either be set in a function prologue by using some
pc-relative reference, or it can be set by the caller.  If the GP is set
by the caller, then that means we need a special mechanism for indirect calls.
On some machines, an indirect call goes through a trampoline that sets the GP
value.  On some machines, like IA-64, a function pointer is a 2 word descriptor
that holds both the function address and the GP value.  We load the GP value
from the descriptor before we call the function address.

Jim

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2002-08-12  8:38 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-09  5:49 Bad code for ia64 with function pointer Henri-Pierre CHARLES
2002-08-09  5:53 ` Christoph Hellwig
2002-08-09  8:24   ` Henri-Pierre CHARLES
2002-08-09  8:41     ` Christoph Hellwig
2002-08-09  8:38 ` Jim Wilson
2002-08-09  8:44   ` Henri-Pierre CHARLES
2002-08-09  9:12     ` Jim Wilson
2002-08-09  9:31     ` David Edelsohn
2002-08-09 14:11   ` Neil Booth
2002-08-09 14:43     ` Mike Stump
2002-08-12  8:38     ` Jim Wilson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).