Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
* BUG: probs with resolving functions in shared library
@ 1998-08-05 16:16 Eric Ding
  1998-08-08 22:29 ` Richard Henderson
  0 siblings, 1 reply; 11+ messages in thread
From: Eric Ding @ 1998-08-05 16:16 UTC (permalink / raw)
  To: axp-list

Hi,

I've looked over the Linux-Alpha FAQ; this question is not the same as
the gdb issue with dynamic functions.  I'm running on a RedHat 5.1
system with egcs 1.0.3a and the latest binutils-2.9.1.0.4 RPM update.
Summary: the linker seems to be improperly resolving global function
references within a shared library.

I have three files: test.c, test2.c, test3.c, as included below.  I
compile test.c into an executable which loads libtest2.so, a shared
library created with test2.o and test3.o.  In test.c, I call a function
in test2.c which calls testme(), a function defined in both the
executable and in the shared library.

Note that I do link the shared library with the -Bsymbolic flag, so I
expect testme() to be resolved to the shared library's version.

1) On Linux/Intel, this produces expected results: a message "hello
there from test2".  On Linux/Alpha, I get a segmentation fault when
testme() is called from within test2().

2) If I combine the contents of test2.c and test3.c and link the
resulting object file into a shared library, then I get the expected
message rather than a segmentation fault.

While #2 is an acceptable workaround in my test case, it indicates to me
that there's a significant linker (?) bug that I can't work around in the
case of the software I'm actually porting (Applixware).  Any thoughts?
Am I doing something wrong?

Thanks,
Eric

BTW, here's my compile and link commmands:

gcc -g -c -fPIC test2.c
gcc -g -c -fPIC test3.c
gcc -shared -Xlinker -Bsymbolic -Xlinker --noinhibit-exec -o libtest2.so \
     test2.o test3.o
gcc -g -c test.c
gcc -o test test.o -ldl

And here's the test case source code:

/* ---- clip here ---- */
/* beginning of test.c */

#include <stdio.h>
#include <dlfcn.h>

void testme();

int main(int argc, char **argv) {
	void *handle;
	void* (*test2)();
	
	handle = dlopen("./libtest2.so", RTLD_NOW);
	test2 = dlsym(handle, "test2");
	(*test2)();
	dlclose(handle);
	return(0);
}

void testme() {
	fprintf(stderr, "OOPS! called testme() from test.c\n");
}

/* end of test.c */

/* ---- clip here ---- */
/* beginning of test2.c */

extern void testme();

void test2() {
	testme();
}

/* end of test2.c */

/* ---- clip here ---- */
/* beginning of test3.c */

#include <stdio.h>

void testme() {
	printf("hello there from test2\n");
}

/* end of test3.c */


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

* Re: BUG: probs with resolving functions in shared library
@ 1998-08-05 22:48 Quant-X Alpha Linux Support
  1998-08-06  2:38 ` Eric Ding
  0 siblings, 1 reply; 11+ messages in thread
From: Quant-X Alpha Linux Support @ 1998-08-05 22:48 UTC (permalink / raw)
  To: Eric Ding; +Cc: axp-list

Hi Eric !

> gcc -shared -Xlinker -Bsymbolic -Xlinker --noinhibit-exec -o libtest2.so \
>      test2.o test3.o

I don't know if it helps:

ld -shared -Bdynamic --noinhibit-exec -o libtest2.so test2.o test3.o

works here as expected. (The difference is only that I'm using
-Bdynamic instead of -Bsymbolic) 

It is a bug. But ok so.
If you want -Bdynamic work as the specs say, you need to compile the main
program (!) with -Xlinker -export-dynamic. If you do this, you
get the "OOPS!".


Hope it helps.

  ciao -
    Stefan

-- 

Quant-X Service &                         Ph:  (+43) 4212 6004-0
Consulting Ges.m.b.H.                     Fax: (+43) 4212 600420
http://www.quant-x.com                    Email: support@quant-x.com


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

* Re: BUG: probs with resolving functions in shared library
  1998-08-05 22:48 BUG: probs with resolving functions in shared library Quant-X Alpha Linux Support
@ 1998-08-06  2:38 ` Eric Ding
  1998-08-06 12:59   ` Quant-X Alpha Linux Support
  0 siblings, 1 reply; 11+ messages in thread
From: Eric Ding @ 1998-08-06  2:38 UTC (permalink / raw)
  To: axp-list

Hi Stefan,

thanks for the tip; unfortunately, we *do* need to use the -export-dynamic
flag with our main executable, so this helps but doesn't solve the problem
sufficiently to get Applixware for Linux/Alpha any closer to running.  :)

Eric

Quant-X Alpha Linux Support <stefan@sime.com> writes:

> I don't know if it helps:
> 
> ld -shared -Bdynamic --noinhibit-exec -o libtest2.so test2.o test3.o
> 
> works here as expected. (The difference is only that I'm using
> -Bdynamic instead of -Bsymbolic) 
> 
> It is a bug. But ok so.
> If you want -Bdynamic work as the specs say, you need to compile the main
> program (!) with -Xlinker -export-dynamic. If you do this, you
> get the "OOPS!".
-- 
Software Development Engineer / ericding@applix.com               <><
Applix, Inc. / 112 Turnpike Road / Westboro MA 01581-2842
TEL: (508) 870-0300 x284 / FAX: (508) 366-4873


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

* Re: BUG: probs with resolving functions in shared library
  1998-08-06  2:38 ` Eric Ding
@ 1998-08-06 12:59   ` Quant-X Alpha Linux Support
  1998-08-06 16:30     ` Eric Ding
  0 siblings, 1 reply; 11+ messages in thread
From: Quant-X Alpha Linux Support @ 1998-08-06 12:59 UTC (permalink / raw)
  To: Eric Ding; +Cc: axp-list

Hi Eric !

> thanks for the tip; unfortunately, we *do* need to use the -export-dynamic
> flag with our main executable, so this helps but doesn't solve the problem
> sufficiently to get Applixware for Linux/Alpha any closer to running.  :)

I think the problem is in your head. ;-}
The solution is easy (correct me if not...).
You only must make shure that the main program does **not** need -export-dynamic.
That's really easy (at least here, maybe not for you...)

a) make the main-program a shared library (this step needs (here) no code changes, only
   affects linking stage).

b) use a stub (linked without -export-dynamic) to start the main program (library).

c) run the stub.

Here a sample stub:

/* start stub.c */

#include <stdio.h>
#include <dlfcn.h>

int main(int argc, char **argv) {
	void *handle;
	int (*real_main)(int, char **);
	int rc;

	if((handle = dlopen("./libapplix.so", RTLD_NOW)) == NULL) {
	  fprintf(stderr, "error dlopen: %s\n", dlerror());
	  exit(1);
	}
	if((real_main = dlsym(handle, "main")) == NULL) {
	  fprintf(stderr, "error dlsym: %s\n", dlerror());
	  exit(1);
	}
	rc = (*real_main)(argc, argv);
	dlclose(handle);
	return(rc);
}

/* end stub.c */

Hope it helps (maybe this time...)


> 
> Eric
> 
> Quant-X Alpha Linux Support <stefan@sime.com> writes:
> 
> > I don't know if it helps:
> > 
> > ld -shared -Bdynamic --noinhibit-exec -o libtest2.so test2.o test3.o
> > 
> > works here as expected. (The difference is only that I'm using
> > -Bdynamic instead of -Bsymbolic) 
> > 
> > It is a bug. But ok so.
> > If you want -Bdynamic work as the specs say, you need to compile the main
> > program (!) with -Xlinker -export-dynamic. If you do this, you
> > get the "OOPS!".
> -- 
> Software Development Engineer / ericding@applix.com               <><
> Applix, Inc. / 112 Turnpike Road / Westboro MA 01581-2842
> TEL: (508) 870-0300 x284 / FAX: (508) 366-4873
> 
> -- 
> To unsubscribe: send e-mail to axp-list-request@redhat.com with
> 'unsubscribe' as the subject.  Do not send it to axp-list@redhat.com
> 

  ciao -
    Stefan

-- 

Quant-X Service &                         Ph:  (+43) 4212 6004-0
Consulting Ges.m.b.H.                     Fax: (+43) 4212 600420
http://www.quant-x.com                    Email: support@quant-x.com


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

* Re: BUG: probs with resolving functions in shared library
  1998-08-06 12:59   ` Quant-X Alpha Linux Support
@ 1998-08-06 16:30     ` Eric Ding
  1998-08-06 21:24       ` Quant-X Alpha Linux Support
  0 siblings, 1 reply; 11+ messages in thread
From: Eric Ding @ 1998-08-06 16:30 UTC (permalink / raw)
  To: axp-list

Hi Stefan,

thanks for your suggestions -- I'm not altogether sure they'll even be
possible in the real application, but let's stay with the stub for now; it
still appears that the fix only works partially.

We use the -export-dynamic flag because the shared library funcs do
occasionally need to access functions in the main executable.  Using the
method you detail below, I get a core dump when I try to access a function
in my "libmain.so" library from the libtest2.so library.  So while this
solution helps work around the scoping bug, it doesn't completely do for
our problem... any other ideas?

Eric

Quant-X Alpha Linux Support <stefan@sime.com> writes:

> I think the problem is in your head. ;-}
> The solution is easy (correct me if not...).

> You only must make shure that the main program does **not** need
> -export-dynamic.  That's really easy (at least here, maybe not for
> you...)

> a) make the main-program a shared library (this step needs (here) no code
> changes, only affects linking stage).

> b) use a stub (linked without -export-dynamic) to start the main program
> (library).


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

* Re: BUG: probs with resolving functions in shared library
  1998-08-06 16:30     ` Eric Ding
@ 1998-08-06 21:24       ` Quant-X Alpha Linux Support
  0 siblings, 0 replies; 11+ messages in thread
From: Quant-X Alpha Linux Support @ 1998-08-06 21:24 UTC (permalink / raw)
  To: Eric Ding; +Cc: axp-list

Hi Erik !

> thanks for your suggestions -- I'm not altogether sure they'll even be
> possible in the real application, but let's stay with the stub for now; it
> still appears that the fix only works partially.

Sorry, the problem was in my head. ;-(
We had here an export-dynamic discussion here in January and I had
a private in March; your problem is a bit more trickey...

I can force the bug to appear as in your first version by
dlopen using "RTLD_NOW|RTLD_GLOBAL" within the stub.
(and RTLD_GLOBAL is needed to export symbols...)

Because RTLD_GLOBAL is like "-export-dynamic"
(and this is needed for your small app (Applixware)) forget
the stub at all, my fault, sorry. It does not fix the scope bug
if you want symbols...

Fixing the "scope bug" is IMHO the best workaround.
I don't know if reporting to "bug-glibc@prep.ai.mit.edu"
is a good idea, but the bug seems to be in glibc-sources/elf.

   ciao -
     Stefan

-- 

Quant-X Service &                         Ph:  (+43) 4212 6004-0
Consulting Ges.m.b.H.                     Fax: (+43) 4212 600420
http://www.quant-x.com                    Email: support@quant-x.com


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

* Re: BUG: probs with resolving functions in shared library
  1998-08-05 16:16 Eric Ding
@ 1998-08-08 22:29 ` Richard Henderson
  1998-08-10 15:54   ` Eric Ding
  1998-08-12 17:07   ` Eric Ding
  0 siblings, 2 replies; 11+ messages in thread
From: Richard Henderson @ 1998-08-08 22:29 UTC (permalink / raw)
  To: Eric Ding, axp-list; +Cc: gafton

On Wed, Aug 05, 1998 at 12:16:57PM -0400, Eric Ding wrote:
> While #2 is an acceptable workaround in my test case, it indicates to me
> that there's a significant linker (?) bug that I can't work around in the
> case of the software I'm actually porting (Applixware).  Any thoughts?
> Am I doing something wrong?

No, you are doing nothing wrong.  This is a bug in binutils.
Here is a patch.


r~


	* elf64-alpha.c (alpha_elf_dynamic_symbol_p): Undef weak symbols
	are always dynamic.
	(elf64_alpha_calc_dynrel_sizes): Allow enough room for RELATIVE
	.got relocs in -Bsymbolic shared objects.


Index: elf64-alpha.c
===================================================================
RCS file: /cvs/cvsfiles/devo/bfd/elf64-alpha.c,v
retrieving revision 1.33
diff -u -p -r1.33 elf64-alpha.c
--- elf64-alpha.c	1998/07/21 01:54:28	1.33
+++ elf64-alpha.c	1998/08/08 22:13:39
@@ -229,7 +229,8 @@ struct alpha_elf_link_hash_table
   (((info)->shared && !(info)->symbolic && (h)->dynindx != -1)		\
    || (((h)->elf_link_hash_flags					\
 	& (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR))	\
-       == (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR)))
+       == (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR))	\
+   || (h)->root.type == bfd_link_hash_undefweak)
 
 /* Create an entry in a Alpha ELF linker hash table.  */
 
@@ -3137,39 +3138,31 @@ elf64_alpha_calc_dynrel_sizes (h, info)
 
   /* If the symbol is dynamic, we'll need all the relocations in their
      natural form.  If it has been forced local, we'll need the same 
-     number of RELATIVE relocations.  */
-  if (alpha_elf_dynamic_symbol_p (&h->root, info)
-      || (info->shared && h->root.dynindx == -1))
-    {
-      struct alpha_elf_reloc_entry *relent;
-
-      for (relent = h->reloc_entries; relent; relent = relent->next)
-	{
-	  relent->srel->_raw_size +=
-	    sizeof (Elf64_External_Rela) * relent->count;
-	}
+     number of RELATIVE relocations. 
+     Except, don't add a .got reloc if we're not using a .plt entry.  */
 
-      /* Only add a .rela.got entry if we're not using a .plt entry.  */
-      if (h->root.plt.offset == MINUS_ONE)
+  if ((alpha_elf_dynamic_symbol_p (&h->root, info)
+       || info->shared)
+      && h->root.plt.offset == MINUS_ONE)
+    {
+      bfd *dynobj = elf_hash_table(info)->dynobj;
+      struct alpha_elf_got_entry *gotent;
+      bfd_size_type count = 0;
+      asection *srel;
+
+      for (gotent = h->got_entries; gotent ; gotent = gotent->next)
+	count++;
+      if (count > 0)
 	{
-	  bfd *dynobj = elf_hash_table(info)->dynobj;
-	  struct alpha_elf_got_entry *gotent;
-	  bfd_size_type count = 0;
-	  asection *srel;
-
-	  for (gotent = h->got_entries; gotent ; gotent = gotent->next)
-	    count++;
-	  if (count > 0)
-	    {
-	      srel = bfd_get_section_by_name (dynobj, ".rela.got");
-	      BFD_ASSERT (srel != NULL);
-	      srel->_raw_size += sizeof (Elf64_External_Rela) * count;
-	    }
+	  srel = bfd_get_section_by_name (dynobj, ".rela.got");
+	  BFD_ASSERT (srel != NULL);
+	  srel->_raw_size += sizeof (Elf64_External_Rela) * count;
 	}
     }
-  /* Otherwise, shared objects require RELATIVE relocs for all REFQUAD
+
+  /* Shared objects require at least RELATIVE relocs for all REFQUAD
      and REFLONG relocations.  */
-  else if (info->shared)
+  if (info->shared)
     {
       struct alpha_elf_reloc_entry *relent;
 
@@ -3610,7 +3603,8 @@ elf64_alpha_relocate_section (output_bfd
 		    /* If the symbol has been forced local, output a
 		       RELATIVE reloc, otherwise it will be handled in
 		       finish_dynamic_symbol.  */
-		    if (info->shared && h->root.dynindx == -1)
+		    if (info->shared
+			&& !alpha_elf_dynamic_symbol_p (&h->root, info))
 		      {
 			Elf_Internal_Rela outrel;
 


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

* Re: BUG: probs with resolving functions in shared library
  1998-08-08 22:29 ` Richard Henderson
@ 1998-08-10 15:54   ` Eric Ding
  1998-08-10 17:52     ` Richard Henderson
  1998-08-12 17:07   ` Eric Ding
  1 sibling, 1 reply; 11+ messages in thread
From: Eric Ding @ 1998-08-10 15:54 UTC (permalink / raw)
  To: axp-list

Is this patch broken?  It appears that h->root.plt.offset ought to instead
by h->root.plt_offset.  I'm patching against binutils-2.9.1.0.4.

Thanks,
Eric

Richard Henderson <rth@dot.cygnus.com> writes:

> No, you are doing nothing wrong.  This is a bug in binutils.
> Here is a patch.
[...]
> +      && h->root.plt.offset == MINUS_ONE)
> +    {
> +      bfd *dynobj = elf_hash_table(info)->dynobj;
> +      struct alpha_elf_got_entry *gotent;
> +      bfd_size_type count = 0;
> +      asection *srel;
[...]


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

* Re: BUG: probs with resolving functions in shared library
  1998-08-10 15:54   ` Eric Ding
@ 1998-08-10 17:52     ` Richard Henderson
  1998-08-10 19:51       ` Eric Ding
  0 siblings, 1 reply; 11+ messages in thread
From: Richard Henderson @ 1998-08-10 17:52 UTC (permalink / raw)
  To: axp-list

On Mon, Aug 10, 1998 at 11:54:32AM -0400, Eric Ding wrote:
> Is this patch broken?  It appears that h->root.plt.offset ought to instead
> by h->root.plt_offset.  I'm patching against binutils-2.9.1.0.4.

Ah yes.  The problem being that the patch was not generated against
binutils 2.9.1, but rather current sources. 

There is another problem as well that affects static linking.  Use
the following definition instead of the one in the patch.

#define alpha_elf_dynamic_symbol_p(h, info)                             \
  ((((info)->shared && !(info)->symbolic)                               \
    || (((h)->elf_link_hash_flags                                       \
         & (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR))     \
        == (ELF_LINK_HASH_DEF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR))     \
    || (h)->root.type == bfd_link_hash_undefweak                        \
    || (h)->root.type == bfd_link_hash_defweak)                         \
   && (h)->dynindx != -1)


r~


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

* Re: BUG: probs with resolving functions in shared library
  1998-08-10 17:52     ` Richard Henderson
@ 1998-08-10 19:51       ` Eric Ding
  0 siblings, 0 replies; 11+ messages in thread
From: Eric Ding @ 1998-08-10 19:51 UTC (permalink / raw)
  To: axp-list

Richard Henderson <rth@dot.cygnus.com> writes:

> On Mon, Aug 10, 1998 at 11:54:32AM -0400, Eric Ding wrote:
> > Is this patch broken?  It appears that h->root.plt.offset ought to instead
> > by h->root.plt_offset.  I'm patching against binutils-2.9.1.0.4.
> 
> Ah yes.  The problem being that the patch was not generated against
> binutils 2.9.1, but rather current sources. 
> 
> There is another problem as well that affects static linking.  Use
> the following definition instead of the one in the patch.

The patch doesn't apply cleanly against 2.9.1.0.4, either; and even if I do
patch the appropriate function, things go awry in actually using ld.  Hm.
I'm also trying out 2.9.1.0.9, but that goes off and aborts on me during
the linking stage.  Argh.

Eric


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

* Re: BUG: probs with resolving functions in shared library
  1998-08-08 22:29 ` Richard Henderson
  1998-08-10 15:54   ` Eric Ding
@ 1998-08-12 17:07   ` Eric Ding
  1 sibling, 0 replies; 11+ messages in thread
From: Eric Ding @ 1998-08-12 17:07 UTC (permalink / raw)
  To: axp-list

Hey all,

thanks to H.J. Lu, who's fixed this bug in the upcoming 2.9.1.0.10 release
of binutils.  :)  I don't think I'm gonna be able to convince management to
put his name in Applixware proper, so this is as close as we get to saying
that he's been a crucial link toward making this a reality.  =b

Eric

Richard Henderson <rth@dot.cygnus.com> writes:

> On Wed, Aug 05, 1998 at 12:16:57PM -0400, Eric Ding wrote:
> > While #2 is an acceptable workaround in my test case, it indicates to me
> > that there's a significant linker (?) bug that I can't work around in the
> > case of the software I'm actually porting (Applixware).  Any thoughts?
> > Am I doing something wrong?
> 
> No, you are doing nothing wrong.  This is a bug in binutils.
> Here is a patch.


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

end of thread, other threads:[~1998-08-12 17:26 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1998-08-05 22:48 BUG: probs with resolving functions in shared library Quant-X Alpha Linux Support
1998-08-06  2:38 ` Eric Ding
1998-08-06 12:59   ` Quant-X Alpha Linux Support
1998-08-06 16:30     ` Eric Ding
1998-08-06 21:24       ` Quant-X Alpha Linux Support
  -- strict thread matches above, loose matches on Subject: below --
1998-08-05 16:16 Eric Ding
1998-08-08 22:29 ` Richard Henderson
1998-08-10 15:54   ` Eric Ding
1998-08-10 17:52     ` Richard Henderson
1998-08-10 19:51       ` Eric Ding
1998-08-12 17:07   ` Eric Ding

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox