libdl1.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * This is a fake version of the dynamic loading library for machines
  3. * which don't have it, but which have nlist. We fake the stuff so that
  4. * looking up in a NULL open library returns symbols in the current executable
  5. * (whose name is pointed to by object_file).
  6. */
  7. #include "sysdep.h"
  8. #include <stdlib.h>
  9. #include <nlist.h>
  10. #ifdef USCORE
  11. #include <string.h>
  12. #endif
  13. #if defined(HAVE_DLOPEN)
  14. #include <dlfcn.h>
  15. #else
  16. #include "../fake/dlfcn.h"
  17. #endif
  18. #if ! defined(NLIST_HAS_N_NAME)
  19. #define n_name n_un.n_name
  20. #endif
  21. extern char* s48_object_file;
  22. static char self[] = "Scheme 48 executable",
  23. *lasterror;
  24. extern char* s48_object_file;
  25. char *
  26. dlerror(void)
  27. {
  28. char *res;
  29. res = lasterror;
  30. lasterror = NULL;
  31. return (res);
  32. }
  33. void *
  34. dlopen(char *name, int flags)
  35. {
  36. if (name == NULL)
  37. return ((void *)self);
  38. lasterror = "Dynamic loading not supported on this machine";
  39. return (NULL);
  40. }
  41. int
  42. dlclose(void *lib)
  43. {
  44. return (0);
  45. }
  46. void *
  47. dlsym(void *lib, char *name)
  48. {
  49. struct nlist names[2];
  50. int status;
  51. #ifdef USCORE
  52. int len;
  53. char *tmp,
  54. buff[40];
  55. #endif
  56. if (lib != self) {
  57. lasterror = "Bad library pointer passed to dlsym()";
  58. return (NULL);
  59. }
  60. if (s48_object_file == NULL) {
  61. lasterror = "I don't know the name of my executable";
  62. return (NULL);
  63. }
  64. #ifdef USCORE
  65. len = 1 + strlen(name) + 1;
  66. if (len <= sizeof(buff))
  67. tmp = buff;
  68. else {
  69. tmp = (char *)malloc(len);
  70. if (tmp == NULL) {
  71. lasterror = "Out of space";
  72. return (NULL);
  73. }
  74. }
  75. tmp[0] = '_';
  76. strcpy(tmp + 1, name);
  77. name = tmp;
  78. #endif
  79. names[0].n_name = name;
  80. names[0].n_value = 0; /* for Linux */
  81. names[0].n_type = 0; /* for Linux */
  82. names[1].n_name = NULL;
  83. status = nlist(s48_object_file, names);
  84. #ifdef USCORE
  85. if (tmp != buff)
  86. free((void *)tmp);
  87. #endif
  88. if ((status != 0)
  89. || (names[0].n_value == 0 && names[0].n_type == 0)) {
  90. lasterror = "Symbol not found";
  91. return (NULL);
  92. }
  93. return (void *)(names[0].n_value);
  94. }