Basic C/C++ (Was: 1.5.10: problems relocating structures with function pointers)

Rolf Campbell Endlisnis@mailc.net
Wed Jul 21 17:16:00 GMT 2004


Justin Schoeman wrote:
> I have discovered what may be a bug in the linker/relocater in cygwin 
> (or, more likely, I am doing something stupid again).
> 
> When I use a structure containing function pointers, and this structure 
> is placed in an archive, then the function pointer becomes NULL. As an 
> example, compile the attached files as follows:
> 
> gcc -O2 -Wall -c inc.c
> ar rsvc inc.a inc.c
> gcc -O2 -Wall -o test test.c inc.a
> 
> Executing test.exe prints 0x0 (the address of the function cointained in 
> the structure), and subsequently segfaults.
> 
> Relinking with
> 
> gcc -O2 -Wall -o test test.c inc.o
> 
> produces a binary that works correctly.
> 
> It seems that once the object file is archived, the dynamic loader 
> losses the capability of correctly assigning the function addresses?
> 
> Any help would be greatly appreciated.
> 
> TIA,
> -justin
> 
> 
> ------------------------------------------------------------------------
> 
> #include <stdio.h>
> 
> static void junk(void)
> {
> 	fprintf(stderr, "JUNK!\n");
> }
> 
> struct js {
> 	void (*junk)(void);
> };
> 
> const struct js jsi = { junk };
> 
> 
> ------------------------------------------------------------------------
> 
> #include <stdio.h>
> 
> struct js {
> 	void (*junk)(void);
> };
> 
> struct js jsi;
extern struct js jsi;
^^^^^^

> 
> int main(void)
> {
> 	fprintf(stderr, "%p\n", jsi.junk);
> 	jsi.junk();
> 	return 0;
> }
> 
> 


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list