home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / unix / internal / 2089 < prev    next >
Encoding:
Text File  |  1993-01-03  |  4.9 KB  |  178 lines

  1. Newsgroups: comp.unix.internals
  2. Path: sparky!uunet!spooky!witr
  3. From: witr@rwwa.COM (Robert Withrow)
  4. Subject: Problem with symbol resolving in dyn-loaded modules.
  5. Message-ID: <1993Jan4.013348.26310@rwwa.COM>
  6. Sender: news@rwwa.COM (News Administrator)
  7. Nntp-Posting-Host: spooky
  8. Reply-To: witr@rwwa.com
  9. Organization: R.W. Withrow Associates
  10. Distribution: usa
  11. Date: Mon, 4 Jan 1993 01:33:48 GMT
  12. Lines: 164
  13.  
  14. I am using (on SVR4.0.3.0/386) the dlxxx(3X) routines to load modules.
  15. These modules need to reference routines (symbols) in the a.out that
  16. executed the dlopen.  The documentation seems to imply that either this
  17. can or cannot be done depending on how you twist your glasses.  I've found
  18. a slimy hack to make it work, but it seems like I shouldn't have to do this.
  19. Example files are appended, but let me be more explicit:
  20.  
  21. assume that a.c looks like this:
  22.  
  23.   main()
  24.   {
  25.      dlopen("./b.so",RTLD_NOW)
  26.   
  27.      routineInB()
  28.   }
  29.  
  30.   routineInA() {}
  31.  
  32. and that b.c looks like:
  33.  
  34.   routineInB()
  35.   {
  36.     routineInA();
  37.   }
  38.  
  39. Without my slimy hack the dlopen fails complaining it can't resolve routineInA.
  40. The hack seems to force ld to export symbol routineInA so that the loader
  41. can see it.  Surely there must be a non-hack way of making this happen?
  42.  
  43. P.S. the hack involves creating a shared object that simply references
  44. the global text symbols in the a.out, and linking it with the a.out.
  45. This is distastefull because it slows down the startup of the a.out and
  46. generally clutters things up...
  47.  
  48. -=-=-=-=- Example -=-=-=-=-=
  49.  
  50. #! /bin/sh
  51. # This is a shell archive.  Remove anything before this line, then unpack
  52. # it by saving it into a file and typing "sh file".  To overwrite existing
  53. # files, type "sh file -c".  You can also feed this as standard input via
  54. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  55. # will see the following message at the end:
  56. #        "End of shell archive."
  57. # Contents:  Makefile fee.c foo.c fum.c
  58. # Wrapped by witr@spooky on Sun Jan  3 20:28:29 1993
  59. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  60. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  61.   echo shar: Will not clobber existing file \"'Makefile'\"
  62. else
  63. echo shar: Extracting \"'Makefile'\" \(478 characters\)
  64. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  65. X# makes two versions of the foo program, one *with* the hack of
  66. X# fum.so and one without.  The *with* program succeeds.  Why?
  67. X
  68. X.SUFFIXES: .so .o .c 
  69. X
  70. X.o.so:
  71. X    cc -G -o $(@F) $(<F)
  72. X
  73. Xall:    with without fee.so
  74. X
  75. Xwithout: foo.o
  76. X    $(CC) $(CFLAGS) -o $(@F) foo.o -ldl
  77. X
  78. Xwith:    foo.o fum.so
  79. X    $(CC) $(CFLAGS) -o $(@F) foo.o ./fum.so -ldl
  80. X
  81. Xfum.o:    fum.c
  82. X    $(CC) $(CFLAGS) -K PIC -c fum.c
  83. X
  84. Xfee.o:    fee.c
  85. X    $(CC) $(CFLAGS) -K PIC -c fee.c
  86. X
  87. Xclean:
  88. X    rm -f foo.o fee.o fee.so fum.o fum.so with without
  89. END_OF_FILE
  90. if test 478 -ne `wc -c <'Makefile'`; then
  91.     echo shar: \"'Makefile'\" unpacked with wrong size!
  92. fi
  93. # end of 'Makefile'
  94. fi
  95. if test -f 'fee.c' -a "${1}" != "-c" ; then 
  96.   echo shar: Will not clobber existing file \"'fee.c'\"
  97. else
  98. echo shar: Extracting \"'fee.c'\" \(137 characters\)
  99. sed "s/^X//" >'fee.c' <<'END_OF_FILE'
  100. Xint TheFunc(int arg)
  101. X{
  102. X  extern int YetAnotherFunc(int arg);
  103. X
  104. X  printf("TheFunc(%d) got called.\n",arg);
  105. X  return YetAnotherFunc(arg);
  106. X}
  107. END_OF_FILE
  108. if test 137 -ne `wc -c <'fee.c'`; then
  109.     echo shar: \"'fee.c'\" unpacked with wrong size!
  110. fi
  111. # end of 'fee.c'
  112. fi
  113. if test -f 'foo.c' -a "${1}" != "-c" ; then 
  114.   echo shar: Will not clobber existing file \"'foo.c'\"
  115. else
  116. echo shar: Extracting \"'foo.c'\" \(715 characters\)
  117. sed "s/^X//" >'foo.c' <<'END_OF_FILE'
  118. X#include <stdio.h>
  119. X#include <stdlib.h>
  120. X#include <dlfcn.h>
  121. X
  122. Xtypedef int afunc(int);
  123. X
  124. Xmain(int argc,char *argv[])
  125. X{
  126. X  void *dlhandle;
  127. X  afunc *thefunc;
  128. X  char file[] = "./fee.so";
  129. X  char func[] = "TheFunc";
  130. X  int funcret;
  131. X
  132. X  fprintf(stderr,"Loading %s\n",file);
  133. X  if ((dlhandle = dlopen(file,RTLD_NOW)) == NULL) {
  134. X    fprintf(stderr,"Load of %s failed: %s.\n",file,dlerror());
  135. X    exit(EXIT_FAILURE);
  136. X  }
  137. X
  138. X  if ((thefunc = (afunc *)dlsym(dlhandle,func)) == NULL) {
  139. X    fprintf(stderr,"Couldn't resolve %s: %s.\n",func,dlerror());
  140. X    exit(EXIT_FAILURE);
  141. X  }
  142. X
  143. X  funcret = thefunc(42);
  144. X  exit(funcret);
  145. X}
  146. X
  147. Xint YetAnotherFunc(int arg)
  148. X{
  149. X  fprintf(stderr,"YetAnotherFunc(%d) was called.\n",arg);
  150. X  return EXIT_SUCCESS;
  151. X}
  152. END_OF_FILE
  153. if test 715 -ne `wc -c <'foo.c'`; then
  154.     echo shar: \"'foo.c'\" unpacked with wrong size!
  155. fi
  156. # end of 'foo.c'
  157. fi
  158. if test -f 'fum.c' -a "${1}" != "-c" ; then 
  159.   echo shar: Will not clobber existing file \"'fum.c'\"
  160. else
  161. echo shar: Extracting \"'fum.c'\" \(104 characters\)
  162. sed "s/^X//" >'fum.c' <<'END_OF_FILE'
  163. Xtypedef void f(void);
  164. Xextern void YetAnotherFunc(void);
  165. Xstatic f *localYetAnotherFunc = YetAnotherFunc;
  166. END_OF_FILE
  167. if test 104 -ne `wc -c <'fum.c'`; then
  168.     echo shar: \"'fum.c'\" unpacked with wrong size!
  169. fi
  170. # end of 'fum.c'
  171. fi
  172. echo shar: End of shell archive.
  173. exit 0
  174.  
  175. -- 
  176.  Robert Withrow, Tel: +1 617 598 4480, Fax: +1 617 598 4430, Net: witr@rwwa.COM
  177.  R.W. Withrow Associates, 21 Railroad Ave, Swampscott MA 01907-1821 USA
  178.