dlfcn.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. /*
  2. Copyright (c) 2002 Jorge Acereda <jacereda@users.sourceforge.net> &
  3. Peter O'Gorman <ogorman@users.sourceforge.net>
  4. Portions may be copyright others, see the AUTHORS file included with this
  5. distribution.
  6. Maintained by Peter O'Gorman <ogorman@users.sourceforge.net>
  7. Bug Reports and other queries should go to <ogorman@users.sourceforge.net>
  8. Permission is hereby granted, free of charge, to any person obtaining
  9. a copy of this software and associated documentation files (the
  10. "Software"), to deal in the Software without restriction, including
  11. without limitation the rights to use, copy, modify, merge, publish,
  12. distribute, sublicense, and/or sell copies of the Software, and to
  13. permit persons to whom the Software is furnished to do so, subject to
  14. the following conditions:
  15. The above copyright notice and this permission notice shall be
  16. included in all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <pthread.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <stdarg.h>
  32. #include <limits.h>
  33. #include <mach-o/dyld.h>
  34. #include <mach-o/nlist.h>
  35. #include <mach-o/getsect.h>
  36. /* Just playing to see if it would compile with the freebsd headers, it does,
  37. * but because of the different values for RTLD_LOCAL etc, it would break binary
  38. * compat... oh well
  39. */
  40. #ifndef __BSD_VISIBLE
  41. #define __BSD_VISIBLE 1
  42. #endif
  43. #include <asterisk/dlfcn-compat.h>
  44. #ifndef dl_restrict
  45. #define dl_restrict __restrict
  46. #endif
  47. /* This is not available on 10.1 */
  48. #ifndef LC_LOAD_WEAK_DYLIB
  49. #define LC_LOAD_WEAK_DYLIB (0x18 | LC_REQ_DYLD)
  50. #endif
  51. /* With this stuff here, this thing may actually compile/run on 10.0 systems
  52. * Not that I have a 10.0 system to test it on anylonger
  53. */
  54. #ifndef LC_REQ_DYLD
  55. #define LC_REQ_DYLD 0x80000000
  56. #endif
  57. #ifndef NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED
  58. #define NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED 0x4
  59. #endif
  60. #ifndef NSADDIMAGE_OPTION_RETURN_ON_ERROR
  61. #define NSADDIMAGE_OPTION_RETURN_ON_ERROR 0x1
  62. #endif
  63. #ifndef NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
  64. #define NSLOOKUPSYMBOLINIMAGE_OPTION_BIND 0x0
  65. #endif
  66. #ifndef NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR
  67. #define NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR 0x4
  68. #endif
  69. /* These symbols will be looked for in dyld */
  70. static const struct mach_header *(*dyld_NSAddImage) (const char *, unsigned long) = 0;
  71. static int (*dyld_NSIsSymbolNameDefinedInImage) (const struct mach_header *, const char *) = 0;
  72. static NSSymbol(*dyld_NSLookupSymbolInImage)
  73. (const struct mach_header *, const char *, unsigned long) = 0;
  74. /* Define this to make dlcompat reuse data block. This way in theory we save
  75. * a little bit of overhead. However we then couldn't correctly catch excess
  76. * calls to dlclose(). Hence we don't use this feature
  77. */
  78. #undef REUSE_STATUS
  79. /* Size of the internal error message buffer (used by dlerror()) */
  80. #define ERR_STR_LEN 251
  81. /* Maximum number of search paths supported by getSearchPath */
  82. #define MAX_SEARCH_PATHS 32
  83. #define MAGIC_DYLIB_OFI ((NSObjectFileImage) 'DYOF')
  84. #define MAGIC_DYLIB_MOD ((NSModule) 'DYMO')
  85. /* internal flags */
  86. #define DL_IN_LIST 0x01
  87. /* our mutex */
  88. static pthread_mutex_t dlcompat_mutex;
  89. /* Our thread specific storage
  90. */
  91. static pthread_key_t dlerror_key;
  92. struct dlthread
  93. {
  94. int lockcnt;
  95. unsigned char errset;
  96. char errstr[ERR_STR_LEN];
  97. };
  98. /* This is our central data structure. Whenever a module is loaded via
  99. * dlopen(), we create such a struct.
  100. */
  101. struct dlstatus
  102. {
  103. struct dlstatus *next; /* pointer to next element in the linked list */
  104. NSModule module;
  105. const struct mach_header *lib;
  106. int refs; /* reference count */
  107. int mode; /* mode in which this module was loaded */
  108. dev_t device;
  109. ino_t inode;
  110. int flags; /* Any internal flags we may need */
  111. };
  112. /* Head node of the dlstatus list */
  113. static struct dlstatus mainStatus = { 0, MAGIC_DYLIB_MOD, NULL, -1, RTLD_GLOBAL, 0, 0, 0 };
  114. static struct dlstatus *stqueue = &mainStatus;
  115. /* Storage for the last error message (used by dlerror()) */
  116. /* static char err_str[ERR_STR_LEN]; */
  117. /* static int err_filled = 0; */
  118. /* Prototypes to internal functions */
  119. static void debug(const char *fmt, ...);
  120. static void error(const char *str, ...);
  121. static const char *safegetenv(const char *s);
  122. static const char *searchList(void);
  123. static const char *getSearchPath(int i);
  124. static const char *getFullPath(int i, const char *file);
  125. static const struct stat *findFile(const char *file, const char **fullPath);
  126. static int isValidStatus(struct dlstatus *status);
  127. static inline int isFlagSet(int mode, int flag);
  128. static struct dlstatus *lookupStatus(const struct stat *sbuf);
  129. static void insertStatus(struct dlstatus *dls, const struct stat *sbuf);
  130. static int promoteLocalToGlobal(struct dlstatus *dls);
  131. static void *reference(struct dlstatus *dls, int mode);
  132. static void *dlsymIntern(struct dlstatus *dls, const char *symbol, int canSetError);
  133. static struct dlstatus *allocStatus(void);
  134. static struct dlstatus *loadModule(const char *path, const struct stat *sbuf, int mode);
  135. static NSSymbol *search_linked_libs(const struct mach_header *mh, const char *symbol);
  136. static const char *get_lib_name(const struct mach_header *mh);
  137. static const struct mach_header *get_mach_header_from_NSModule(NSModule * mod);
  138. static void dlcompat_init_func(void);
  139. static inline void dolock(void);
  140. static inline void dounlock(void);
  141. static void dlerrorfree(void *data);
  142. static void resetdlerror(void);
  143. static const struct mach_header *my_find_image(const char *name);
  144. static const struct mach_header *image_for_address(const void *address);
  145. static void dlcompat_cleanup(void);
  146. static inline const char *dyld_error_str(void);
  147. #if FINK_BUILD
  148. /* Two Global Functions */
  149. void *dlsym_prepend_underscore(void *handle, const char *symbol);
  150. void *dlsym_auto_underscore(void *handle, const char *symbol);
  151. /* And their _intern counterparts */
  152. static void *dlsym_prepend_underscore_intern(void *handle, const char *symbol);
  153. static void *dlsym_auto_underscore_intern(void *handle, const char *symbol);
  154. #endif
  155. /* Functions */
  156. static void debug(const char *fmt, ...)
  157. {
  158. #if DEBUG > 1
  159. va_list arg;
  160. va_start(arg, fmt);
  161. fprintf(stderr, "DLDEBUG: ");
  162. vfprintf(stderr, fmt, arg);
  163. fprintf(stderr, "\n");
  164. fflush(stderr);
  165. va_end(arg);
  166. #endif
  167. }
  168. static void error(const char *str, ...)
  169. {
  170. va_list arg;
  171. struct dlthread *tss;
  172. char * err_str;
  173. va_start(arg, str);
  174. tss = pthread_getspecific(dlerror_key);
  175. err_str = tss->errstr;
  176. strncpy(err_str, "dlcompat: ", ERR_STR_LEN);
  177. vsnprintf(err_str + 10, ERR_STR_LEN - 10, str, arg);
  178. va_end(arg);
  179. debug("ERROR: %s\n", err_str);
  180. tss->errset = 1;
  181. }
  182. static void warning(const char *str)
  183. {
  184. #if DEBUG > 0
  185. fprintf(stderr, "WARNING: dlcompat: %s\n", str);
  186. #endif
  187. }
  188. static const char *safegetenv(const char *s)
  189. {
  190. const char *ss = getenv(s);
  191. return ss ? ss : "";
  192. }
  193. /* because this is only used for debugging and error reporting functions, we
  194. * don't really care about how elegant it is... it could use the load
  195. * commands to find the install name of the library, but...
  196. */
  197. static const char *get_lib_name(const struct mach_header *mh)
  198. {
  199. unsigned long count = _dyld_image_count();
  200. unsigned long i;
  201. const char *val = NULL;
  202. if (mh)
  203. {
  204. for (i = 0; i < count; i++)
  205. {
  206. if (mh == _dyld_get_image_header(i))
  207. {
  208. val = _dyld_get_image_name(i);
  209. break;
  210. }
  211. }
  212. }
  213. return val;
  214. }
  215. /* Returns the mach_header for the module bu going through all the loaded images
  216. * and finding the one with the same name as the module. There really ought to be
  217. * an api for doing this, would be faster, but there isn't one right now
  218. */
  219. static const struct mach_header *get_mach_header_from_NSModule(NSModule * mod)
  220. {
  221. const char *mod_name = NSNameOfModule(mod);
  222. struct mach_header *mh = NULL;
  223. unsigned long count = _dyld_image_count();
  224. unsigned long i;
  225. debug("Module name: %s", mod_name);
  226. for (i = 0; i < count; i++)
  227. {
  228. if (!strcmp(mod_name, _dyld_get_image_name(i)))
  229. {
  230. mh = _dyld_get_image_header(i);
  231. break;
  232. }
  233. }
  234. return mh;
  235. }
  236. /* Compute and return a list of all directories that we should search when
  237. * trying to locate a module. We first look at the values of LD_LIBRARY_PATH
  238. * and DYLD_LIBRARY_PATH, and then finally fall back to looking into
  239. * /usr/lib and /lib. Since both of the environments variables can contain a
  240. * list of colon separated paths, we simply concat them and the two other paths
  241. * into one big string, which we then can easily parse.
  242. * Splitting this string into the actual path list is done by getSearchPath()
  243. */
  244. static const char *searchList()
  245. {
  246. size_t buf_size;
  247. static char *buf=NULL;
  248. const char *ldlp = safegetenv("LD_LIBRARY_PATH");
  249. const char *dyldlp = safegetenv("DYLD_LIBRARY_PATH");
  250. const char *stdpath = getenv("DYLD_FALLBACK_LIBRARY_PATH");
  251. if (!stdpath)
  252. stdpath = "/usr/local/lib:/lib:/usr/lib";
  253. if (!buf)
  254. {
  255. buf_size = strlen(ldlp) + strlen(dyldlp) + strlen(stdpath) + 4;
  256. buf = malloc(buf_size);
  257. snprintf(buf, buf_size, "%s%s%s%s%s%c", dyldlp, (dyldlp[0] ? ":" : ""), ldlp, (ldlp[0] ? ":" : ""),
  258. stdpath, '\0');
  259. }
  260. return buf;
  261. }
  262. /* Returns the ith search path from the list as computed by searchList() */
  263. static const char *getSearchPath(int i)
  264. {
  265. static const char *list = 0;
  266. static char **path = (char **)0;
  267. static int end = 0;
  268. static int numsize = MAX_SEARCH_PATHS;
  269. static char **tmp;
  270. /* So we can call free() in the "destructor" we use i=-1 to return the alloc'd array */
  271. if (i == -1)
  272. {
  273. return (const char*)path;
  274. }
  275. if (!path)
  276. {
  277. path = (char **)calloc(MAX_SEARCH_PATHS, sizeof(char **));
  278. }
  279. if (!list && !end)
  280. list = searchList();
  281. if (i >= (numsize))
  282. {
  283. debug("Increasing size for long PATH");
  284. tmp = (char **)calloc((MAX_SEARCH_PATHS + numsize), sizeof(char **));
  285. if (tmp)
  286. {
  287. memcpy(tmp, path, sizeof(char **) * numsize);
  288. free(path);
  289. path = tmp;
  290. numsize += MAX_SEARCH_PATHS;
  291. }
  292. else
  293. {
  294. return 0;
  295. }
  296. }
  297. while (!path[i] && !end)
  298. {
  299. path[i] = strsep((char **)&list, ":");
  300. if (path[i][0] == 0)
  301. path[i] = 0;
  302. end = (list == 0);
  303. }
  304. return path[i];
  305. }
  306. static const char *getFullPath(int i, const char *file)
  307. {
  308. static char buf[PATH_MAX];
  309. const char *path = getSearchPath(i);
  310. if (path)
  311. {
  312. snprintf(buf, PATH_MAX, "%s/%s", path, file);
  313. }
  314. return path ? buf : 0;
  315. }
  316. /* Given a file name, try to determine the full path for that file. Starts
  317. * its search in the current directory, and then tries all paths in the
  318. * search list in the order they are specified there.
  319. */
  320. static const struct stat *findFile(const char *file, const char **fullPath)
  321. {
  322. int i = 0;
  323. static struct stat sbuf;
  324. char *fileName;
  325. debug("finding file %s", file);
  326. *fullPath = file;
  327. if (0 == stat(file, &sbuf))
  328. return &sbuf;
  329. if (strchr(file, '/'))
  330. return 0; /* If the path had a / we don't look in env var places */
  331. fileName = NULL;
  332. if (!fileName)
  333. fileName = (char *)file;
  334. while ((*fullPath = getFullPath(i++, fileName)))
  335. {
  336. if (0 == stat(*fullPath, &sbuf))
  337. return &sbuf;
  338. }
  339. ;
  340. return 0;
  341. }
  342. /* Determine whether a given dlstatus is valid or not */
  343. static int isValidStatus(struct dlstatus *status)
  344. {
  345. /* Walk the list to verify status is contained in it */
  346. struct dlstatus *dls = stqueue;
  347. while (dls && status != dls)
  348. dls = dls->next;
  349. if (dls == 0)
  350. error("invalid handle");
  351. else if ((dls->module == 0) || (dls->refs == 0))
  352. error("handle to closed library");
  353. else
  354. return TRUE;
  355. return FALSE;
  356. }
  357. static inline int isFlagSet(int mode, int flag)
  358. {
  359. return (mode & flag) == flag;
  360. }
  361. static struct dlstatus *lookupStatus(const struct stat *sbuf)
  362. {
  363. struct dlstatus *dls = stqueue;
  364. debug("looking for status");
  365. while (dls && ( /* isFlagSet(dls->mode, RTLD_UNSHARED) */ 0
  366. || sbuf->st_dev != dls->device || sbuf->st_ino != dls->inode))
  367. dls = dls->next;
  368. return dls;
  369. }
  370. static void insertStatus(struct dlstatus *dls, const struct stat *sbuf)
  371. {
  372. debug("inserting status");
  373. dls->inode = sbuf->st_ino;
  374. dls->device = sbuf->st_dev;
  375. dls->refs = 0;
  376. dls->mode = 0;
  377. if ((dls->flags & DL_IN_LIST) == 0)
  378. {
  379. dls->next = stqueue;
  380. stqueue = dls;
  381. dls->flags |= DL_IN_LIST;
  382. }
  383. }
  384. static struct dlstatus *allocStatus()
  385. {
  386. struct dlstatus *dls;
  387. #ifdef REUSE_STATUS
  388. dls = stqueue;
  389. while (dls && dls->module)
  390. dls = dls->next;
  391. if (!dls)
  392. #endif
  393. dls = malloc(sizeof(*dls));
  394. dls->flags = 0;
  395. return dls;
  396. }
  397. static int promoteLocalToGlobal(struct dlstatus *dls)
  398. {
  399. static int (*p) (NSModule module) = 0;
  400. debug("promoting");
  401. if (!p)
  402. _dyld_func_lookup("__dyld_NSMakePrivateModulePublic", (unsigned long *)&p);
  403. return (dls->module == MAGIC_DYLIB_MOD) || (p && p(dls->module));
  404. }
  405. static void *reference(struct dlstatus *dls, int mode)
  406. {
  407. if (dls)
  408. {
  409. if (dls->module == MAGIC_DYLIB_MOD && !isFlagSet(mode, RTLD_GLOBAL))
  410. {
  411. warning("trying to open a .dylib with RTLD_LOCAL");
  412. error("unable to open a .dylib with RTLD_LOCAL");
  413. return NULL;
  414. }
  415. if (isFlagSet(mode, RTLD_GLOBAL) &&
  416. !isFlagSet(dls->mode, RTLD_GLOBAL) && !promoteLocalToGlobal(dls))
  417. {
  418. error("unable to promote local module to global");
  419. return NULL;
  420. }
  421. dls->mode |= mode;
  422. dls->refs++;
  423. }
  424. else
  425. debug("reference called with NULL argument");
  426. return dls;
  427. }
  428. static const struct mach_header *my_find_image(const char *name)
  429. {
  430. const struct mach_header *mh = 0;
  431. const char *id = NULL;
  432. int i = _dyld_image_count();
  433. int j;
  434. mh = (struct mach_header *)
  435. dyld_NSAddImage(name, NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED |
  436. NSADDIMAGE_OPTION_RETURN_ON_ERROR);
  437. if (!mh)
  438. {
  439. for (j = 0; j < i; j++)
  440. {
  441. id = _dyld_get_image_name(j);
  442. if (!strcmp(id, name))
  443. {
  444. mh = _dyld_get_image_header(j);
  445. break;
  446. }
  447. }
  448. }
  449. return mh;
  450. }
  451. /*
  452. * dyld adds libraries by first adding the directly dependant libraries in link order, and
  453. * then adding the dependencies for those libraries, so we should do the same... but we don't
  454. * bother adding the extra dependencies, if the symbols are neither in the loaded image nor
  455. * any of it's direct dependencies, then it probably isn't there.
  456. */
  457. NSSymbol *search_linked_libs(const struct mach_header * mh, const char *symbol)
  458. {
  459. int n;
  460. struct load_command *lc = 0;
  461. struct mach_header *wh;
  462. NSSymbol *nssym = 0;
  463. if (dyld_NSAddImage && dyld_NSIsSymbolNameDefinedInImage && dyld_NSLookupSymbolInImage)
  464. {
  465. lc = (struct load_command *)((char *)mh + sizeof(struct mach_header));
  466. for (n = 0; n < mh->ncmds; n++, lc = (struct load_command *)((char *)lc + lc->cmdsize))
  467. {
  468. if ((LC_LOAD_DYLIB == lc->cmd) || (LC_LOAD_WEAK_DYLIB == lc->cmd))
  469. {
  470. if ((wh = (struct mach_header *)
  471. my_find_image((char *)(((struct dylib_command *)lc)->dylib.name.offset +
  472. (char *)lc))))
  473. {
  474. if (dyld_NSIsSymbolNameDefinedInImage(wh, symbol))
  475. {
  476. nssym = dyld_NSLookupSymbolInImage(wh,
  477. symbol,
  478. NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
  479. NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
  480. break;
  481. }
  482. }
  483. }
  484. }
  485. if ((!nssym) && NSIsSymbolNameDefined(symbol))
  486. {
  487. /* I've never seen this debug message...*/
  488. debug("Symbol \"%s\" is defined but was not found", symbol);
  489. }
  490. }
  491. return nssym;
  492. }
  493. /* Up to the caller to free() returned string */
  494. static inline const char *dyld_error_str()
  495. {
  496. NSLinkEditErrors dylder;
  497. int dylderno;
  498. const char *dylderrstr;
  499. const char *dyldfile;
  500. const char* retStr = NULL;
  501. NSLinkEditError(&dylder, &dylderno, &dyldfile, &dylderrstr);
  502. if (dylderrstr && strlen(dylderrstr))
  503. {
  504. retStr = malloc(strlen(dylderrstr) +1);
  505. strcpy((char*)retStr,dylderrstr);
  506. }
  507. return retStr;
  508. }
  509. static void *dlsymIntern(struct dlstatus *dls, const char *symbol, int canSetError)
  510. {
  511. NSSymbol *nssym = 0;
  512. void *caller = __builtin_return_address(1); /* Be *very* careful about inlining */
  513. const struct mach_header *caller_mh = 0;
  514. const char* savedErrorStr = NULL;
  515. resetdlerror();
  516. #ifndef RTLD_SELF
  517. #define RTLD_SELF ((void *) -3)
  518. #endif
  519. if (NULL == dls)
  520. dls = RTLD_SELF;
  521. if ((RTLD_NEXT == dls) || (RTLD_SELF == dls))
  522. {
  523. if (dyld_NSIsSymbolNameDefinedInImage && dyld_NSLookupSymbolInImage)
  524. {
  525. caller_mh = image_for_address(caller);
  526. if (RTLD_SELF == dls)
  527. {
  528. /* FIXME: We should be using the NSModule api, if SELF is an MH_BUNDLE
  529. * But it appears to work anyway, and looking at the code in dyld_libfuncs.c
  530. * this is acceptable.
  531. */
  532. if (dyld_NSIsSymbolNameDefinedInImage(caller_mh, symbol))
  533. {
  534. nssym = dyld_NSLookupSymbolInImage(caller_mh,
  535. symbol,
  536. NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
  537. NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
  538. }
  539. }
  540. if (!nssym)
  541. {
  542. if (RTLD_SELF == dls)
  543. savedErrorStr = dyld_error_str();
  544. nssym = search_linked_libs(caller_mh, symbol);
  545. }
  546. }
  547. else
  548. {
  549. if (canSetError)
  550. error("RTLD_SELF and RTLD_NEXT are not supported");
  551. return NULL;
  552. }
  553. }
  554. if (!nssym)
  555. {
  556. if (RTLD_DEFAULT == dls)
  557. {
  558. dls = &mainStatus;
  559. }
  560. if (!isValidStatus(dls))
  561. return NULL;
  562. if (dls->module != MAGIC_DYLIB_MOD)
  563. {
  564. nssym = NSLookupSymbolInModule(dls->module, symbol);
  565. if (!nssym && NSIsSymbolNameDefined(symbol))
  566. {
  567. debug("Searching dependencies");
  568. savedErrorStr = dyld_error_str();
  569. nssym = search_linked_libs(get_mach_header_from_NSModule(dls->module), symbol);
  570. }
  571. }
  572. else if (dls->lib && dyld_NSIsSymbolNameDefinedInImage && dyld_NSLookupSymbolInImage)
  573. {
  574. if (dyld_NSIsSymbolNameDefinedInImage(dls->lib, symbol))
  575. {
  576. nssym = dyld_NSLookupSymbolInImage(dls->lib,
  577. symbol,
  578. NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
  579. NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
  580. }
  581. else if (NSIsSymbolNameDefined(symbol))
  582. {
  583. debug("Searching dependencies");
  584. savedErrorStr = dyld_error_str();
  585. nssym = search_linked_libs(dls->lib, symbol);
  586. }
  587. }
  588. else if (dls->module == MAGIC_DYLIB_MOD)
  589. {
  590. /* Global context, use NSLookupAndBindSymbol */
  591. if (NSIsSymbolNameDefined(symbol))
  592. {
  593. /* There doesn't seem to be a return on error option for this call???
  594. this is potentially broken, if binding fails, it will improperly
  595. exit the application. */
  596. nssym = NSLookupAndBindSymbol(symbol);
  597. }
  598. else
  599. {
  600. if (savedErrorStr)
  601. free((char*)savedErrorStr);
  602. savedErrorStr = malloc(256);
  603. snprintf((char*)savedErrorStr, 256, "Symbol \"%s\" not in global context",symbol);
  604. }
  605. }
  606. }
  607. /* Error reporting */
  608. if (!nssym)
  609. {
  610. if (!savedErrorStr || !strlen(savedErrorStr))
  611. {
  612. if (savedErrorStr)
  613. free((char*)savedErrorStr);
  614. savedErrorStr = malloc(256);
  615. snprintf((char*)savedErrorStr, 256,"Symbol \"%s\" not found",symbol);
  616. }
  617. if (canSetError)
  618. {
  619. error(savedErrorStr);
  620. }
  621. else
  622. {
  623. debug(savedErrorStr);
  624. }
  625. if (savedErrorStr)
  626. free((char*)savedErrorStr);
  627. return NULL;
  628. }
  629. return NSAddressOfSymbol(nssym);
  630. }
  631. static struct dlstatus *loadModule(const char *path, const struct stat *sbuf, int mode)
  632. {
  633. NSObjectFileImage ofi = 0;
  634. NSObjectFileImageReturnCode ofirc;
  635. struct dlstatus *dls;
  636. NSLinkEditErrors ler;
  637. int lerno;
  638. const char *errstr;
  639. const char *file;
  640. void (*init) (void);
  641. ofirc = NSCreateObjectFileImageFromFile(path, &ofi);
  642. switch (ofirc)
  643. {
  644. case NSObjectFileImageSuccess:
  645. break;
  646. case NSObjectFileImageInappropriateFile:
  647. if (dyld_NSAddImage && dyld_NSIsSymbolNameDefinedInImage && dyld_NSLookupSymbolInImage)
  648. {
  649. if (!isFlagSet(mode, RTLD_GLOBAL))
  650. {
  651. warning("trying to open a .dylib with RTLD_LOCAL");
  652. error("unable to open this file with RTLD_LOCAL");
  653. return NULL;
  654. }
  655. }
  656. else
  657. {
  658. error("opening this file is unsupported on this system");
  659. return NULL;
  660. }
  661. break;
  662. case NSObjectFileImageFailure:
  663. error("object file setup failure");
  664. return NULL;
  665. case NSObjectFileImageArch:
  666. error("no object for this architecture");
  667. return NULL;
  668. case NSObjectFileImageFormat:
  669. error("bad object file format");
  670. return NULL;
  671. case NSObjectFileImageAccess:
  672. error("can't read object file");
  673. return NULL;
  674. default:
  675. error("unknown error from NSCreateObjectFileImageFromFile()");
  676. return NULL;
  677. }
  678. dls = lookupStatus(sbuf);
  679. if (!dls)
  680. {
  681. dls = allocStatus();
  682. }
  683. if (!dls)
  684. {
  685. error("unable to allocate memory");
  686. return NULL;
  687. }
  688. dls->lib = 0;
  689. if (ofirc == NSObjectFileImageInappropriateFile)
  690. {
  691. if ((dls->lib = dyld_NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR)))
  692. {
  693. debug("Dynamic lib loaded at %ld", dls->lib);
  694. ofi = MAGIC_DYLIB_OFI;
  695. dls->module = MAGIC_DYLIB_MOD;
  696. ofirc = NSObjectFileImageSuccess;
  697. /* Although it is possible with a bit of work to modify this so it works and
  698. functions with RTLD_NOW, I don't deem it necessary at the moment */
  699. }
  700. if (!(dls->module))
  701. {
  702. NSLinkEditError(&ler, &lerno, &file, &errstr);
  703. if (!errstr || (!strlen(errstr)))
  704. error("Can't open this file type");
  705. else
  706. error(errstr);
  707. if ((dls->flags & DL_IN_LIST) == 0)
  708. {
  709. free(dls);
  710. }
  711. return NULL;
  712. }
  713. }
  714. else
  715. {
  716. dls->module = NSLinkModule(ofi, path,
  717. NSLINKMODULE_OPTION_RETURN_ON_ERROR |
  718. NSLINKMODULE_OPTION_PRIVATE |
  719. (isFlagSet(mode, RTLD_NOW) ? NSLINKMODULE_OPTION_BINDNOW : 0));
  720. NSDestroyObjectFileImage(ofi);
  721. if (dls->module)
  722. {
  723. dls->lib = get_mach_header_from_NSModule(dls->module);
  724. }
  725. }
  726. if (!dls->module)
  727. {
  728. NSLinkEditError(&ler, &lerno, &file, &errstr);
  729. if ((dls->flags & DL_IN_LIST) == 0)
  730. {
  731. free(dls);
  732. }
  733. error(errstr);
  734. return NULL;
  735. }
  736. insertStatus(dls, sbuf);
  737. dls = reference(dls, mode);
  738. if ((init = dlsymIntern(dls, "__init", 0)))
  739. {
  740. debug("calling _init()");
  741. init();
  742. }
  743. return dls;
  744. }
  745. static void dlcompat_init_func(void)
  746. {
  747. static int inited = 0;
  748. if (!inited)
  749. {
  750. inited = 1;
  751. _dyld_func_lookup("__dyld_NSAddImage", (unsigned long *)&dyld_NSAddImage);
  752. _dyld_func_lookup("__dyld_NSIsSymbolNameDefinedInImage",
  753. (unsigned long *)&dyld_NSIsSymbolNameDefinedInImage);
  754. _dyld_func_lookup("__dyld_NSLookupSymbolInImage", (unsigned long *)&dyld_NSLookupSymbolInImage);
  755. if (pthread_mutex_init(&dlcompat_mutex, NULL))
  756. exit(1);
  757. if (pthread_key_create(&dlerror_key, &dlerrorfree))
  758. exit(1);
  759. /* And be neat and tidy and clean up after ourselves */
  760. atexit(dlcompat_cleanup);
  761. }
  762. }
  763. #if 0
  764. #pragma CALL_ON_LOAD dlcompat_init_func
  765. #endif
  766. static void dlcompat_cleanup(void)
  767. {
  768. struct dlstatus *dls;
  769. struct dlstatus *next;
  770. char *data;
  771. data = (char *)searchList();
  772. if ( data )
  773. free( data );
  774. data = (char *)getSearchPath(-1);
  775. if ( data )
  776. free( data );
  777. pthread_mutex_destroy(&dlcompat_mutex);
  778. pthread_key_delete(dlerror_key);
  779. next = stqueue;
  780. while (next && (next != &mainStatus))
  781. {
  782. dls = next;
  783. next = dls->next;
  784. free(dls);
  785. }
  786. }
  787. static void resetdlerror()
  788. {
  789. struct dlthread *tss;
  790. tss = pthread_getspecific(dlerror_key);
  791. tss->errset = 0;
  792. }
  793. static void dlerrorfree(void *data)
  794. {
  795. free(data);
  796. }
  797. /* We kind of want a recursive lock here, but meet a little trouble
  798. * because they are not available pre OS X 10.2, so we fake it
  799. * using thread specific storage to keep a lock count
  800. */
  801. static inline void dolock(void)
  802. {
  803. int err = 0;
  804. struct dlthread *tss;
  805. tss = pthread_getspecific(dlerror_key);
  806. if (!tss)
  807. {
  808. tss = malloc(sizeof(struct dlthread));
  809. tss->lockcnt = 0;
  810. tss->errset = 0;
  811. if (pthread_setspecific(dlerror_key, tss))
  812. {
  813. fprintf(stderr,"dlcompat: pthread_setspecific failed\n");
  814. exit(1);
  815. }
  816. }
  817. if (!tss->lockcnt)
  818. err = pthread_mutex_lock(&dlcompat_mutex);
  819. tss->lockcnt = tss->lockcnt +1;
  820. if (err)
  821. exit(err);
  822. }
  823. static inline void dounlock(void)
  824. {
  825. int err = 0;
  826. struct dlthread *tss;
  827. tss = pthread_getspecific(dlerror_key);
  828. tss->lockcnt = tss->lockcnt -1;
  829. if (!tss->lockcnt)
  830. err = pthread_mutex_unlock(&dlcompat_mutex);
  831. if (err)
  832. exit(err);
  833. }
  834. void *dlopen(const char *path, int mode)
  835. {
  836. const struct stat *sbuf;
  837. struct dlstatus *dls;
  838. const char *fullPath;
  839. dlcompat_init_func(); /* Just in case */
  840. dolock();
  841. resetdlerror();
  842. if (!path)
  843. {
  844. dls = &mainStatus;
  845. goto dlopenok;
  846. }
  847. if (!(sbuf = findFile(path, &fullPath)))
  848. {
  849. error("file \"%s\" not found", path);
  850. goto dlopenerror;
  851. }
  852. /* Now checks that it hasn't been closed already */
  853. if ((dls = lookupStatus(sbuf)) && (dls->refs > 0))
  854. {
  855. /* debug("status found"); */
  856. dls = reference(dls, mode);
  857. goto dlopenok;
  858. }
  859. #ifdef RTLD_NOLOAD
  860. if (isFlagSet(mode, RTLD_NOLOAD))
  861. {
  862. error("no existing handle and RTLD_NOLOAD specified");
  863. goto dlopenerror;
  864. }
  865. #endif
  866. if (isFlagSet(mode, RTLD_LAZY) && isFlagSet(mode, RTLD_NOW))
  867. {
  868. error("how can I load something both RTLD_LAZY and RTLD_NOW?");
  869. goto dlopenerror;
  870. }
  871. dls = loadModule(fullPath, sbuf, mode);
  872. dlopenok:
  873. dounlock();
  874. return (void *)dls;
  875. dlopenerror:
  876. dounlock();
  877. return NULL;
  878. }
  879. #if !FINK_BUILD
  880. void *dlsym(void * dl_restrict handle, const char * dl_restrict symbol)
  881. {
  882. int sym_len = strlen(symbol);
  883. void *value = NULL;
  884. char *malloc_sym = NULL;
  885. dolock();
  886. malloc_sym = malloc(sym_len + 2);
  887. if (malloc_sym)
  888. {
  889. sprintf(malloc_sym, "_%s", symbol);
  890. value = dlsymIntern(handle, malloc_sym, 1);
  891. free(malloc_sym);
  892. }
  893. else
  894. {
  895. error("Unable to allocate memory");
  896. goto dlsymerror;
  897. }
  898. dounlock();
  899. return value;
  900. dlsymerror:
  901. dounlock();
  902. return NULL;
  903. }
  904. #endif
  905. #if FINK_BUILD
  906. void *dlsym_prepend_underscore(void *handle, const char *symbol)
  907. {
  908. void *answer;
  909. dolock();
  910. answer = dlsym_prepend_underscore_intern(handle, symbol);
  911. dounlock();
  912. return answer;
  913. }
  914. static void *dlsym_prepend_underscore_intern(void *handle, const char *symbol)
  915. {
  916. /*
  917. * A quick and easy way for porting packages which call dlsym(handle,"sym")
  918. * If the porter adds -Ddlsym=dlsym_prepend_underscore to the CFLAGS then
  919. * this function will be called, and will add the required underscore.
  920. *
  921. * Note that I haven't figured out yet which should be "standard", prepend
  922. * the underscore always, or not at all. These global functions need to go away
  923. * for opendarwin.
  924. */
  925. int sym_len = strlen(symbol);
  926. void *value = NULL;
  927. char *malloc_sym = NULL;
  928. malloc_sym = malloc(sym_len + 2);
  929. if (malloc_sym)
  930. {
  931. sprintf(malloc_sym, "_%s", symbol);
  932. value = dlsymIntern(handle, malloc_sym, 1);
  933. free(malloc_sym);
  934. }
  935. else
  936. {
  937. error("Unable to allocate memory");
  938. }
  939. return value;
  940. }
  941. void *dlsym_auto_underscore(void *handle, const char *symbol)
  942. {
  943. void *answer;
  944. dolock();
  945. answer = dlsym_auto_underscore_intern(handle, symbol);
  946. dounlock();
  947. return answer;
  948. }
  949. static void *dlsym_auto_underscore_intern(void *handle, const char *symbol)
  950. {
  951. struct dlstatus *dls = handle;
  952. void *addr = 0;
  953. addr = dlsymIntern(dls, symbol, 0);
  954. if (!addr)
  955. addr = dlsym_prepend_underscore_intern(handle, symbol);
  956. return addr;
  957. }
  958. void *dlsym(void * dl_restrict handle, const char * dl_restrict symbol)
  959. {
  960. struct dlstatus *dls = handle;
  961. void *addr = 0;
  962. dolock();
  963. addr = dlsymIntern(dls, symbol, 1);
  964. dounlock();
  965. return addr;
  966. }
  967. #endif
  968. int dlclose(void *handle)
  969. {
  970. struct dlstatus *dls = handle;
  971. dolock();
  972. resetdlerror();
  973. if (!isValidStatus(dls))
  974. {
  975. goto dlcloseerror;
  976. }
  977. if (dls->module == MAGIC_DYLIB_MOD)
  978. {
  979. const char *name;
  980. if (!dls->lib)
  981. {
  982. name = "global context";
  983. }
  984. else
  985. {
  986. name = get_lib_name(dls->lib);
  987. }
  988. warning("trying to close a .dylib!");
  989. error("Not closing \"%s\" - dynamic libraries cannot be closed", name);
  990. goto dlcloseerror;
  991. }
  992. if (!dls->module)
  993. {
  994. error("module already closed");
  995. goto dlcloseerror;
  996. }
  997. if (dls->refs == 1)
  998. {
  999. unsigned long options = 0;
  1000. void (*fini) (void);
  1001. if ((fini = dlsymIntern(dls, "__fini", 0)))
  1002. {
  1003. debug("calling _fini()");
  1004. fini();
  1005. }
  1006. #ifdef __ppc__
  1007. options |= NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES;
  1008. #endif
  1009. #if 1
  1010. /* Currently, if a module contains c++ static destructors and it is unloaded, we
  1011. * get a segfault in atexit(), due to compiler and dynamic loader differences of
  1012. * opinion, this works around that.
  1013. * I really need a way to figure out from code if this is still necessary.
  1014. */
  1015. if ((const struct section *)NULL !=
  1016. getsectbynamefromheader(get_mach_header_from_NSModule(dls->module),
  1017. "__DATA", "__mod_term_func"))
  1018. {
  1019. options |= NSUNLINKMODULE_OPTION_KEEP_MEMORY_MAPPED;
  1020. }
  1021. #endif
  1022. #ifdef RTLD_NODELETE
  1023. if (isFlagSet(dls->mode, RTLD_NODELETE))
  1024. options |= NSUNLINKMODULE_OPTION_KEEP_MEMORY_MAPPED;
  1025. #endif
  1026. if (!NSUnLinkModule(dls->module, options))
  1027. {
  1028. error("unable to unlink module");
  1029. goto dlcloseerror;
  1030. }
  1031. dls->refs--;
  1032. dls->module = 0;
  1033. /* Note: the dlstatus struct dls is neither removed from the list
  1034. * nor is the memory it occupies freed. This shouldn't pose a
  1035. * problem in mostly all cases, though.
  1036. */
  1037. }
  1038. dounlock();
  1039. return 0;
  1040. dlcloseerror:
  1041. dounlock();
  1042. return 1;
  1043. }
  1044. const char *dlerror(void)
  1045. {
  1046. struct dlthread *tss;
  1047. char * err_str;
  1048. tss = pthread_getspecific(dlerror_key);
  1049. err_str = tss->errstr;
  1050. tss = pthread_getspecific(dlerror_key);
  1051. if (tss->errset == 0)
  1052. return 0;
  1053. tss->errset = 0;
  1054. return (err_str );
  1055. }
  1056. /* Given an address, return the mach_header for the image containing it
  1057. * or zero if the given address is not contained in any loaded images.
  1058. */
  1059. const struct mach_header *image_for_address(const void *address)
  1060. {
  1061. unsigned long i;
  1062. unsigned long j;
  1063. unsigned long count = _dyld_image_count();
  1064. struct mach_header *mh = 0;
  1065. struct load_command *lc = 0;
  1066. unsigned long addr = NULL;
  1067. for (i = 0; i < count; i++)
  1068. {
  1069. addr = (unsigned long)address - _dyld_get_image_vmaddr_slide(i);
  1070. mh = _dyld_get_image_header(i);
  1071. if (mh)
  1072. {
  1073. lc = (struct load_command *)((char *)mh + sizeof(struct mach_header));
  1074. for (j = 0; j < mh->ncmds; j++, lc = (struct load_command *)((char *)lc + lc->cmdsize))
  1075. {
  1076. if (LC_SEGMENT == lc->cmd &&
  1077. addr >= ((struct segment_command *)lc)->vmaddr &&
  1078. addr <
  1079. ((struct segment_command *)lc)->vmaddr + ((struct segment_command *)lc)->vmsize)
  1080. {
  1081. goto image_found;
  1082. }
  1083. }
  1084. }
  1085. mh = 0;
  1086. }
  1087. image_found:
  1088. return mh;
  1089. }
  1090. int dladdr(const void * dl_restrict p, Dl_info * dl_restrict info)
  1091. {
  1092. /*
  1093. FIXME: USe the routine image_for_address.
  1094. */
  1095. unsigned long i;
  1096. unsigned long j;
  1097. unsigned long count = _dyld_image_count();
  1098. struct mach_header *mh = 0;
  1099. struct load_command *lc = 0;
  1100. unsigned long addr = NULL;
  1101. unsigned long table_off = (unsigned long)0;
  1102. int found = 0;
  1103. if (!info)
  1104. return 0;
  1105. dolock();
  1106. resetdlerror();
  1107. info->dli_fname = 0;
  1108. info->dli_fbase = 0;
  1109. info->dli_sname = 0;
  1110. info->dli_saddr = 0;
  1111. /* Some of this was swiped from code posted by Douglas Davidson <ddavidso AT apple DOT com>
  1112. * to darwin-development AT lists DOT apple DOT com and slightly modified
  1113. */
  1114. for (i = 0; i < count; i++)
  1115. {
  1116. addr = (unsigned long)p - _dyld_get_image_vmaddr_slide(i);
  1117. mh = _dyld_get_image_header(i);
  1118. if (mh)
  1119. {
  1120. lc = (struct load_command *)((char *)mh + sizeof(struct mach_header));
  1121. for (j = 0; j < mh->ncmds; j++, lc = (struct load_command *)((char *)lc + lc->cmdsize))
  1122. {
  1123. if (LC_SEGMENT == lc->cmd &&
  1124. addr >= ((struct segment_command *)lc)->vmaddr &&
  1125. addr <
  1126. ((struct segment_command *)lc)->vmaddr + ((struct segment_command *)lc)->vmsize)
  1127. {
  1128. info->dli_fname = _dyld_get_image_name(i);
  1129. info->dli_fbase = (void *)mh;
  1130. found = 1;
  1131. break;
  1132. }
  1133. }
  1134. if (found)
  1135. break;
  1136. }
  1137. }
  1138. if (!found)
  1139. {
  1140. dounlock();
  1141. return 0;
  1142. }
  1143. lc = (struct load_command *)((char *)mh + sizeof(struct mach_header));
  1144. for (j = 0; j < mh->ncmds; j++, lc = (struct load_command *)((char *)lc + lc->cmdsize))
  1145. {
  1146. if (LC_SEGMENT == lc->cmd)
  1147. {
  1148. if (!strcmp(((struct segment_command *)lc)->segname, "__LINKEDIT"))
  1149. break;
  1150. }
  1151. }
  1152. table_off =
  1153. ((unsigned long)((struct segment_command *)lc)->vmaddr) -
  1154. ((unsigned long)((struct segment_command *)lc)->fileoff) + _dyld_get_image_vmaddr_slide(i);
  1155. debug("table off %x", table_off);
  1156. lc = (struct load_command *)((char *)mh + sizeof(struct mach_header));
  1157. for (j = 0; j < mh->ncmds; j++, lc = (struct load_command *)((char *)lc + lc->cmdsize))
  1158. {
  1159. if (LC_SYMTAB == lc->cmd)
  1160. {
  1161. struct nlist *symtable = (struct nlist *)(((struct symtab_command *)lc)->symoff + table_off);
  1162. unsigned long numsyms = ((struct symtab_command *)lc)->nsyms;
  1163. struct nlist *nearest = NULL;
  1164. unsigned long diff = 0xffffffff;
  1165. unsigned long strtable = (unsigned long)(((struct symtab_command *)lc)->stroff + table_off);
  1166. debug("symtable %x", symtable);
  1167. for (i = 0; i < numsyms; i++)
  1168. {
  1169. /* Ignore the following kinds of Symbols */
  1170. if ((!symtable->n_value) /* Undefined */
  1171. || (symtable->n_type >= N_PEXT) /* Debug symbol */
  1172. || (!(symtable->n_type & N_EXT)) /* Local Symbol */
  1173. )
  1174. {
  1175. symtable++;
  1176. continue;
  1177. }
  1178. if ((addr >= symtable->n_value) && (diff >= (symtable->n_value - addr)))
  1179. {
  1180. diff = (unsigned long)symtable->n_value - addr;
  1181. nearest = symtable;
  1182. }
  1183. symtable++;
  1184. }
  1185. if (nearest)
  1186. {
  1187. info->dli_saddr = nearest->n_value + ((void *)p - addr);
  1188. info->dli_sname = (char *)(strtable + nearest->n_un.n_strx);
  1189. }
  1190. }
  1191. }
  1192. dounlock();
  1193. return 1;
  1194. }
  1195. /*
  1196. * Implement the dlfunc() interface, which behaves exactly the same as
  1197. * dlsym() except that it returns a function pointer instead of a data
  1198. * pointer. This can be used by applications to avoid compiler warnings
  1199. * about undefined behavior, and is intended as prior art for future
  1200. * POSIX standardization. This function requires that all pointer types
  1201. * have the same representation, which is true on all platforms FreeBSD
  1202. * runs on, but is not guaranteed by the C standard.
  1203. */
  1204. #if 0
  1205. dlfunc_t dlfunc(void * dl_restrict handle, const char * dl_restrict symbol)
  1206. {
  1207. union
  1208. {
  1209. void *d;
  1210. dlfunc_t f;
  1211. } rv;
  1212. int sym_len = strlen(symbol);
  1213. char *malloc_sym = NULL;
  1214. dolock();
  1215. malloc_sym = malloc(sym_len + 2);
  1216. if (malloc_sym)
  1217. {
  1218. sprintf(malloc_sym, "_%s", symbol);
  1219. rv.d = dlsymIntern(handle, malloc_sym, 1);
  1220. free(malloc_sym);
  1221. }
  1222. else
  1223. {
  1224. error("Unable to allocate memory");
  1225. goto dlfuncerror;
  1226. }
  1227. dounlock();
  1228. return rv.f;
  1229. dlfuncerror:
  1230. dounlock();
  1231. return NULL;
  1232. }
  1233. #endif