home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / sys / next / programm / 5753 < prev    next >
Encoding:
Text File  |  1992-08-22  |  3.9 KB  |  127 lines

  1. Newsgroups: comp.sys.next.programmer
  2. Path: sparky!uunet!nevada.edu!oliverm
  3. From: oliverm@nevada.edu (Moises Oliveira )
  4. Subject: Re: PLEASE help me with rld!
  5. Message-ID: <1992Aug22.154717.21218@nevada.edu>
  6. Sender: news@nevada.edu (USENET News System)
  7. Nntp-Posting-Host: helios.nevada.edu
  8. Organization: Organization? Ha, don't make me laugh
  9. References: <HRA6NUH@mailgzrz.tu-berlin.de>
  10. Date: Sat, 22 Aug 1992 15:47:17 GMT
  11. Lines: 114
  12.  
  13. In article <HRA6NUH@mailgzrz.tu-berlin.de> mauriti@cs.tu-berlin.de (Frank Hartlep) writes:
  14. >I've asked this already and nobody replied. But I can't believe there's
  15. >nobody on this big, big net who knows how to use rld.
  16. >
  17.  
  18. Probably because no one ever saw it.  I certainly didn't.
  19.  
  20. > ... and PLEASE!  PLEASE!  PLEASE!  PLEASE! tell me what's wrong with it.
  21. > Loader should load to_be_loaded.o and execute LoadedFunction.
  22. > Sounds easy, but why does it fail in rld_load_basefile (with a
  23. > floating point exception in NXCreateZone, as the debugger tells me)???
  24.  
  25.     This isn't the way rld(3) works.  There are two problems in
  26. this example.  The solution to your immediate problem
  27. first: 
  28.  
  29. >  ld -o loader.gdb loader.o /lib/crt0.o /lib/libsys_s.a -U _LoadedFunction
  30.  
  31. is wrong.   Use
  32.  
  33.     ld -o loader.gdb -lcrt0.o loader.o -lsys_s -U _LoadedFunction
  34.  
  35. That will stop the problem you are having NOW, the code
  36. will no longer crash at the first function call in the
  37. program.  But bigger problems lurk in the code you have
  38. here.  the rld(3) set of calls is not used in the way you are
  39. trying to use it.  Here's a quick primer to convert your
  40. code to working code: 
  41.  
  42. First off, your call to  rld_load_basefile is completely
  43. unnecessary.  Remove it. 
  44.  
  45. Second, if possible compose the full pathname to the file
  46. to be loaded.  The current working directory is seldom the
  47. same directory that an application resides in.  In the
  48. code below, I have hard-coded the path.  (To keep it short) 
  49.  
  50.  
  51. This is not the way rld(3) works:
  52.  
  53. >        LoadedFunction ("It works!");
  54.  
  55. It doesn't automagically change all references to
  56. LoadedFunction to point to the newly loaded code.  What
  57. you are doing ( with -U) is allowing the linker to set
  58. LoadedFunction to point to (0x0),  so that as soon as the
  59. above line is executed your program will crash anyways.
  60.  
  61. What rld(3) does is load the code and text from the
  62. MH_OBJECT file, and then provide a way for you to find the
  63. entry points of functions, and the addresses of
  64. variables, by using rld_lookup().
  65.  
  66. What you need to do is ask rld_lookup for the address of
  67. "_LoadedFunction", and then call that address using a
  68. pointer to a function.  The name that "loader" uses does
  69. not bear any relation to the name of the function in
  70. "to_be_loaded". 
  71.  
  72.  
  73. Here are the programs done correctly:
  74.  
  75.  
  76. >--- loader.c
  77.  
  78. #include <mach-o/rld.h>
  79. #include <stdio.h>
  80.  
  81. int main ()
  82. {
  83.     char        *ObjectFiles [] = {"/tmp/rld/to_be_loaded.o", NULL};
  84.     int        Status;
  85. // This is a pointer to a function called "theFunction",
  86. // which takes a ( char *) for an arguement 
  87.     long        (*theFunction) (const char *);
  88.  
  89. // we load the object files, and "check" for any errors
  90.     Status = rld_load (NULL, NULL, ObjectFiles, NULL);
  91.     fprintf (stderr, "rld_load returned %d\n", Status);
  92.  
  93. // we ask rld where the function has been loaded, and "check" for errors
  94.     rld_lookup(NULL,"_LoadedFunction",&theFunction);
  95.     fprintf (stderr, "rld_lookup returned %d\n", aStatus);
  96.  
  97. // now theFunction points to the newly loaded code. We call theFunction
  98.     (* theFunction) ("It works!");
  99.     return 0;
  100. }
  101.  
  102. >---- to_be_loaded.c
  103.  
  104. #include <stdio.h>
  105.  
  106. void LoadedFunction (char *Text)
  107. {
  108.     printf ("%s\n", Text);
  109.     return;
  110. }
  111.  
  112. Finally, we compile, link and test the program.  Notice
  113. that now that we are no longer trying to call a
  114. non-existent function, we can get rid of the -U
  115. siliness.  We can also quit calling ld directly, and use cc
  116. to save us some heartache. 
  117.  
  118. cc -g -Wall -o loader.gdb loader.c
  119. cc -g -Wall -c to_be_loaded.c
  120.  
  121. rld_load returned 1
  122. rld_lookup returned 1
  123. It works!
  124.  
  125.  
  126.