dso.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * dso.c -- DSO system function emulation for AIX
  18. *
  19. * This is *only* intended for AIX < 4.3.
  20. */
  21. /*
  22. * Based on libdl (dlfcn.c/dlfcn.h) which is
  23. * Copyright (c) 1992,1993,1995,1996,1997,1988
  24. * Jens-Uwe Mager, Helios Software GmbH, Hannover, Germany.
  25. *
  26. * Not derived from licensed software.
  27. *
  28. * Permission is granted to freely use, copy, modify, and redistribute
  29. * this software, provided that the author is not construed to be liable
  30. * for any results of using the software, alterations are clearly marked
  31. * as such, and this notice is not modified.
  32. *
  33. * Changes marked with `--jwe' were made on April 7 1996 by
  34. * John W. Eaton <jwe@bevo.che.wisc.edu> to support g++
  35. *
  36. * Bundled, stripped and adjusted on April 1998 as one single source file
  37. * for inclusion into the Apache HTTP server by
  38. * Ralf S. Engelschall <rse@apache.org>
  39. *
  40. * Added to APR by David Reid April 2000
  41. */
  42. #include <stdio.h>
  43. #include <errno.h>
  44. #include <string.h>
  45. #include <stdlib.h>
  46. #include <sys/types.h>
  47. #include <sys/ldr.h>
  48. #include <a.out.h>
  49. #include "fspr_arch_dso.h"
  50. #include "fspr_portable.h"
  51. #if APR_HAS_DSO
  52. #undef FREAD
  53. #undef FWRITE
  54. #include <ldfcn.h>
  55. /*
  56. * AIX 4.3 does remove some useful definitions from ldfcn.h. Define
  57. * these here to compensate for that lossage.
  58. */
  59. #ifndef BEGINNING
  60. #define BEGINNING SEEK_SET
  61. #endif
  62. #ifndef FSEEK
  63. #define FSEEK(ldptr,o,p) fseek(IOPTR(ldptr),(p==BEGINNING)?(OFFSET(ldptr) +o):o,p)
  64. #endif
  65. #ifndef FREAD
  66. #define FREAD(p,s,n,ldptr) fread(p,s,n,IOPTR(ldptr))
  67. #endif
  68. /*
  69. * Mode flags for the dlopen routine.
  70. */
  71. #undef RTLD_LAZY
  72. #define RTLD_LAZY 1 /* lazy function call binding */
  73. #undef RTLD_NOW
  74. #define RTLD_NOW 2 /* immediate function call binding */
  75. #undef RTLD_GLOBAL
  76. #define RTLD_GLOBAL 0x100 /* allow symbols to be global */
  77. /*
  78. * To be able to initialize, a library may provide a dl_info structure
  79. * that contains functions to be called to initialize and terminate.
  80. */
  81. struct dl_info {
  82. void (*init) (void);
  83. void (*fini) (void);
  84. };
  85. /* APR functions...
  86. *
  87. * As the AIX functions have been declared in the header file we just
  88. * add the basic "wrappers" here.
  89. */
  90. APR_DECLARE(fspr_status_t) fspr_os_dso_handle_put(fspr_dso_handle_t **aprdso,
  91. fspr_os_dso_handle_t osdso,
  92. fspr_pool_t *pool)
  93. {
  94. *aprdso = fspr_pcalloc(pool, sizeof **aprdso);
  95. (*aprdso)->handle = osdso;
  96. (*aprdso)->pool = pool;
  97. return APR_SUCCESS;
  98. }
  99. APR_DECLARE(fspr_status_t) fspr_os_dso_handle_get(fspr_os_dso_handle_t *osdso,
  100. fspr_dso_handle_t *aprdso)
  101. {
  102. *osdso = aprdso->handle;
  103. return APR_SUCCESS;
  104. }
  105. static fspr_status_t dso_cleanup(void *thedso)
  106. {
  107. fspr_dso_handle_t *dso = thedso;
  108. if (dso->handle != NULL && dlclose(dso->handle) != 0)
  109. return APR_EINIT;
  110. dso->handle = NULL;
  111. return APR_SUCCESS;
  112. }
  113. APR_DECLARE(fspr_status_t) fspr_dso_load(fspr_dso_handle_t **res_handle,
  114. const char *path, fspr_pool_t *ctx)
  115. {
  116. void *os_handle = dlopen((char *)path, RTLD_NOW | RTLD_GLOBAL);
  117. *res_handle = fspr_pcalloc(ctx, sizeof(*res_handle));
  118. if(os_handle == NULL) {
  119. (*res_handle)->errormsg = dlerror();
  120. return APR_EDSOOPEN;
  121. }
  122. (*res_handle)->handle = (void*)os_handle;
  123. (*res_handle)->pool = ctx;
  124. (*res_handle)->errormsg = NULL;
  125. fspr_pool_cleanup_register(ctx, *res_handle, dso_cleanup, fspr_pool_cleanup_null);
  126. return APR_SUCCESS;
  127. }
  128. APR_DECLARE(fspr_status_t) fspr_dso_unload(fspr_dso_handle_t *handle)
  129. {
  130. return fspr_pool_cleanup_run(handle->pool, handle, dso_cleanup);
  131. }
  132. APR_DECLARE(fspr_status_t) fspr_dso_sym(fspr_dso_handle_sym_t *ressym,
  133. fspr_dso_handle_t *handle,
  134. const char *symname)
  135. {
  136. void *retval = dlsym(handle->handle, symname);
  137. if (retval == NULL) {
  138. handle->errormsg = dlerror();
  139. return APR_ESYMNOTFOUND;
  140. }
  141. *ressym = retval;
  142. return APR_SUCCESS;
  143. }
  144. APR_DECLARE(const char *) fspr_dso_error(fspr_dso_handle_t *dso, char *buffer, fspr_size_t buflen)
  145. {
  146. if (dso->errormsg) {
  147. fspr_cpystrn(buffer, dso->errormsg, buflen);
  148. return dso->errormsg;
  149. }
  150. return "No Error";
  151. }
  152. /*
  153. * We simulate dlopen() et al. through a call to load. Because AIX has
  154. * no call to find an exported symbol we read the loader section of the
  155. * loaded module and build a list of exported symbols and their virtual
  156. * address.
  157. */
  158. typedef struct {
  159. char *name; /* the symbols's name */
  160. void *addr; /* its relocated virtual address */
  161. } Export, *ExportPtr;
  162. /*
  163. * xlC uses the following structure to list its constructors and
  164. * destructors. This is gleaned from the output of munch.
  165. */
  166. typedef struct {
  167. void (*init) (void); /* call static constructors */
  168. void (*term) (void); /* call static destructors */
  169. } Cdtor, *CdtorPtr;
  170. typedef void (*GccCDtorPtr) (void);
  171. /*
  172. * The void * handle returned from dlopen is actually a ModulePtr.
  173. */
  174. typedef struct Module {
  175. struct Module *next;
  176. char *name; /* module name for refcounting */
  177. int refCnt; /* the number of references */
  178. void *entry; /* entry point from load */
  179. struct dl_info *info; /* optional init/terminate functions */
  180. CdtorPtr cdtors; /* optional C++ constructors */
  181. GccCDtorPtr gcc_ctor; /* g++ constructors --jwe */
  182. GccCDtorPtr gcc_dtor; /* g++ destructors --jwe */
  183. int nExports; /* the number of exports found */
  184. ExportPtr exports; /* the array of exports */
  185. } Module, *ModulePtr;
  186. /*
  187. * We keep a list of all loaded modules to be able to call the fini
  188. * handlers and destructors at atexit() time.
  189. */
  190. static ModulePtr modList;
  191. /*
  192. * The last error from one of the dl* routines is kept in static
  193. * variables here. Each error is returned only once to the caller.
  194. */
  195. static char errbuf[BUFSIZ];
  196. static int errvalid;
  197. /*
  198. * The `fixed' gcc header files on AIX 3.2.5 provide a prototype for
  199. * strdup(). --jwe
  200. */
  201. extern char *strdup(const char *);
  202. static void caterr(char *);
  203. static int readExports(ModulePtr);
  204. static void terminate(void);
  205. static void *findMain(void);
  206. void *dlopen(const char *path, int mode)
  207. {
  208. register ModulePtr mp;
  209. static void *mainModule;
  210. /*
  211. * Upon the first call register a terminate handler that will
  212. * close all libraries. Also get a reference to the main module
  213. * for use with loadbind.
  214. */
  215. if (!mainModule) {
  216. if ((mainModule = findMain()) == NULL)
  217. return NULL;
  218. atexit(terminate);
  219. }
  220. /*
  221. * Scan the list of modules if we have the module already loaded.
  222. */
  223. for (mp = modList; mp; mp = mp->next)
  224. if (strcmp(mp->name, path) == 0) {
  225. mp->refCnt++;
  226. return mp;
  227. }
  228. if ((mp = (ModulePtr) calloc(1, sizeof(*mp))) == NULL) {
  229. errvalid++;
  230. strcpy(errbuf, "calloc: ");
  231. strcat(errbuf, strerror(errno));
  232. return NULL;
  233. }
  234. if ((mp->name = strdup(path)) == NULL) {
  235. errvalid++;
  236. strcpy(errbuf, "strdup: ");
  237. strcat(errbuf, strerror(errno));
  238. free(mp);
  239. return NULL;
  240. }
  241. /*
  242. * load should be declared load(const char *...). Thus we
  243. * cast the path to a normal char *. Ugly.
  244. */
  245. if ((mp->entry = (void *) loadAndInit((char *) path, L_NOAUTODEFER, NULL)) == NULL) {
  246. free(mp->name);
  247. free(mp);
  248. errvalid++;
  249. strcpy(errbuf, "dlopen: ");
  250. strcat(errbuf, path);
  251. strcat(errbuf, ": ");
  252. /*
  253. * If AIX says the file is not executable, the error
  254. * can be further described by querying the loader about
  255. * the last error.
  256. */
  257. if (errno == ENOEXEC) {
  258. char *tmp[BUFSIZ / sizeof(char *)];
  259. if (loadquery(L_GETMESSAGES, tmp, sizeof(tmp)) == -1)
  260. strcpy(errbuf, strerror(errno));
  261. else {
  262. char **p;
  263. for (p = tmp; *p; p++)
  264. caterr(*p);
  265. }
  266. }
  267. else
  268. strcat(errbuf, strerror(errno));
  269. return NULL;
  270. }
  271. mp->refCnt = 1;
  272. mp->next = modList;
  273. modList = mp;
  274. if (loadbind(0, mainModule, mp->entry) == -1) {
  275. dlclose(mp);
  276. errvalid++;
  277. strcpy(errbuf, "loadbind: ");
  278. strcat(errbuf, strerror(errno));
  279. return NULL;
  280. }
  281. /*
  282. * If the user wants global binding, loadbind against all other
  283. * loaded modules.
  284. */
  285. if (mode & RTLD_GLOBAL) {
  286. register ModulePtr mp1;
  287. for (mp1 = mp->next; mp1; mp1 = mp1->next)
  288. if (loadbind(0, mp1->entry, mp->entry) == -1) {
  289. dlclose(mp);
  290. errvalid++;
  291. strcpy(errbuf, "loadbind: ");
  292. strcat(errbuf, strerror(errno));
  293. return NULL;
  294. }
  295. }
  296. if (readExports(mp) == -1) {
  297. dlclose(mp);
  298. return NULL;
  299. }
  300. /*
  301. * If there is a dl_info structure, call the init function.
  302. */
  303. if (mp->info = (struct dl_info *) dlsym(mp, "dl_info")) {
  304. if (mp->info->init)
  305. (*mp->info->init) ();
  306. }
  307. else
  308. errvalid = 0;
  309. /*
  310. * If the shared object was compiled using xlC we will need
  311. * to call static constructors (and later on dlclose destructors).
  312. */
  313. if (mp->cdtors = (CdtorPtr) dlsym(mp, "__cdtors")) {
  314. CdtorPtr cp = mp->cdtors;
  315. while (cp->init || cp->term) {
  316. if (cp->init && cp->init != (void (*)(void)) 0xffffffff)
  317. (*cp->init) ();
  318. cp++;
  319. }
  320. /*
  321. * If the shared object was compiled using g++, we will need
  322. * to call global constructors using the _GLOBAL__DI function,
  323. * and later, global destructors using the _GLOBAL_DD
  324. * funciton. --jwe
  325. */
  326. }
  327. else if (mp->gcc_ctor = (GccCDtorPtr) dlsym(mp, "_GLOBAL__DI")) {
  328. (*mp->gcc_ctor) ();
  329. mp->gcc_dtor = (GccCDtorPtr) dlsym(mp, "_GLOBAL__DD");
  330. }
  331. else
  332. errvalid = 0;
  333. return mp;
  334. }
  335. /*
  336. * Attempt to decipher an AIX loader error message and append it
  337. * to our static error message buffer.
  338. */
  339. static void caterr(char *s)
  340. {
  341. register char *p = s;
  342. while (*p >= '0' && *p <= '9')
  343. p++;
  344. switch (atoi(s)) {
  345. case L_ERROR_TOOMANY:
  346. strcat(errbuf, "to many errors");
  347. break;
  348. case L_ERROR_NOLIB:
  349. strcat(errbuf, "can't load library");
  350. strcat(errbuf, p);
  351. break;
  352. case L_ERROR_UNDEF:
  353. strcat(errbuf, "can't find symbol");
  354. strcat(errbuf, p);
  355. break;
  356. case L_ERROR_RLDBAD:
  357. strcat(errbuf, "bad RLD");
  358. strcat(errbuf, p);
  359. break;
  360. case L_ERROR_FORMAT:
  361. strcat(errbuf, "bad exec format in");
  362. strcat(errbuf, p);
  363. break;
  364. case L_ERROR_ERRNO:
  365. strcat(errbuf, strerror(atoi(++p)));
  366. break;
  367. default:
  368. strcat(errbuf, s);
  369. break;
  370. }
  371. }
  372. void *dlsym(void *handle, const char *symbol)
  373. {
  374. register ModulePtr mp = (ModulePtr) handle;
  375. register ExportPtr ep;
  376. register int i;
  377. /*
  378. * Could speed up the search, but I assume that one assigns
  379. * the result to function pointers anyways.
  380. */
  381. for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
  382. if (strcmp(ep->name, symbol) == 0)
  383. return ep->addr;
  384. errvalid++;
  385. strcpy(errbuf, "dlsym: undefined symbol ");
  386. strcat(errbuf, symbol);
  387. return NULL;
  388. }
  389. const char *dlerror(void)
  390. {
  391. if (errvalid) {
  392. errvalid = 0;
  393. return errbuf;
  394. }
  395. return NULL;
  396. }
  397. int dlclose(void *handle)
  398. {
  399. register ModulePtr mp = (ModulePtr) handle;
  400. int result;
  401. register ModulePtr mp1;
  402. if (--mp->refCnt > 0)
  403. return 0;
  404. if (mp->info && mp->info->fini)
  405. (*mp->info->fini) ();
  406. if (mp->cdtors) {
  407. CdtorPtr cp = mp->cdtors;
  408. while (cp->init || cp->term) {
  409. if (cp->term && cp->init != (void (*)(void)) 0xffffffff)
  410. (*cp->term) ();
  411. cp++;
  412. }
  413. /*
  414. * If the function to handle global destructors for g++
  415. * exists, call it. --jwe
  416. */
  417. }
  418. else if (mp->gcc_dtor) {
  419. (*mp->gcc_dtor) ();
  420. }
  421. result = unload(mp->entry);
  422. if (result == -1) {
  423. errvalid++;
  424. strcpy(errbuf, strerror(errno));
  425. }
  426. if (mp->exports) {
  427. register ExportPtr ep;
  428. register int i;
  429. for (ep = mp->exports, i = mp->nExports; i; i--, ep++)
  430. if (ep->name)
  431. free(ep->name);
  432. free(mp->exports);
  433. }
  434. if (mp == modList)
  435. modList = mp->next;
  436. else {
  437. for (mp1 = modList; mp1; mp1 = mp1->next)
  438. if (mp1->next == mp) {
  439. mp1->next = mp->next;
  440. break;
  441. }
  442. }
  443. free(mp->name);
  444. free(mp);
  445. return result;
  446. }
  447. static void terminate(void)
  448. {
  449. while (modList)
  450. dlclose(modList);
  451. }
  452. /*
  453. * Build the export table from the XCOFF .loader section.
  454. */
  455. static int readExports(ModulePtr mp)
  456. {
  457. LDFILE *ldp = NULL;
  458. SCNHDR sh, shdata;
  459. LDHDR *lhp;
  460. char *ldbuf;
  461. LDSYM *ls;
  462. int i;
  463. ExportPtr ep;
  464. struct ld_info *lp;
  465. char *buf;
  466. int size = 4 * 1024;
  467. void *dataorg;
  468. /*
  469. * The module might be loaded due to the LIBPATH
  470. * environment variable. Search for the loaded
  471. * module using L_GETINFO.
  472. */
  473. if ((buf = malloc(size)) == NULL) {
  474. errvalid++;
  475. strcpy(errbuf, "readExports: ");
  476. strcat(errbuf, strerror(errno));
  477. return -1;
  478. }
  479. while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
  480. free(buf);
  481. size += 4 * 1024;
  482. if ((buf = malloc(size)) == NULL) {
  483. errvalid++;
  484. strcpy(errbuf, "readExports: ");
  485. strcat(errbuf, strerror(errno));
  486. return -1;
  487. }
  488. }
  489. if (i == -1) {
  490. errvalid++;
  491. strcpy(errbuf, "readExports: ");
  492. strcat(errbuf, strerror(errno));
  493. free(buf);
  494. return -1;
  495. }
  496. /*
  497. * Traverse the list of loaded modules. The entry point
  498. * returned by load() does actually point to the TOC
  499. * entry contained in the data segment.
  500. */
  501. lp = (struct ld_info *) buf;
  502. while (lp) {
  503. if ((unsigned long) mp->entry >= (unsigned long) lp->ldinfo_dataorg &&
  504. (unsigned long) mp->entry < (unsigned long) lp->ldinfo_dataorg +
  505. lp->ldinfo_datasize) {
  506. dataorg = lp->ldinfo_dataorg;
  507. ldp = ldopen(lp->ldinfo_filename, ldp);
  508. break;
  509. }
  510. if (lp->ldinfo_next == 0)
  511. lp = NULL;
  512. else
  513. lp = (struct ld_info *) ((char *) lp + lp->ldinfo_next);
  514. }
  515. free(buf);
  516. if (!ldp) {
  517. errvalid++;
  518. strcpy(errbuf, "readExports: ");
  519. strcat(errbuf, strerror(errno));
  520. return -1;
  521. }
  522. if (TYPE(ldp) != U802TOCMAGIC) {
  523. errvalid++;
  524. strcpy(errbuf, "readExports: bad magic");
  525. while (ldclose(ldp) == FAILURE);
  526. return -1;
  527. }
  528. /*
  529. * Get the padding for the data section. This is needed for
  530. * AIX 4.1 compilers. This is used when building the final
  531. * function pointer to the exported symbol.
  532. */
  533. if (ldnshread(ldp, _DATA, &shdata) != SUCCESS) {
  534. errvalid++;
  535. strcpy(errbuf, "readExports: cannot read data section header");
  536. while (ldclose(ldp) == FAILURE);
  537. return -1;
  538. }
  539. if (ldnshread(ldp, _LOADER, &sh) != SUCCESS) {
  540. errvalid++;
  541. strcpy(errbuf, "readExports: cannot read loader section header");
  542. while (ldclose(ldp) == FAILURE);
  543. return -1;
  544. }
  545. /*
  546. * We read the complete loader section in one chunk, this makes
  547. * finding long symbol names residing in the string table easier.
  548. */
  549. if ((ldbuf = (char *) malloc(sh.s_size)) == NULL) {
  550. errvalid++;
  551. strcpy(errbuf, "readExports: ");
  552. strcat(errbuf, strerror(errno));
  553. while (ldclose(ldp) == FAILURE);
  554. return -1;
  555. }
  556. if (FSEEK(ldp, sh.s_scnptr, BEGINNING) != OKFSEEK) {
  557. errvalid++;
  558. strcpy(errbuf, "readExports: cannot seek to loader section");
  559. free(ldbuf);
  560. while (ldclose(ldp) == FAILURE);
  561. return -1;
  562. }
  563. if (FREAD(ldbuf, sh.s_size, 1, ldp) != 1) {
  564. errvalid++;
  565. strcpy(errbuf, "readExports: cannot read loader section");
  566. free(ldbuf);
  567. while (ldclose(ldp) == FAILURE);
  568. return -1;
  569. }
  570. lhp = (LDHDR *) ldbuf;
  571. ls = (LDSYM *) (ldbuf + LDHDRSZ);
  572. /*
  573. * Count the number of exports to include in our export table.
  574. */
  575. for (i = lhp->l_nsyms; i; i--, ls++) {
  576. if (!LDR_EXPORT(*ls))
  577. continue;
  578. mp->nExports++;
  579. }
  580. if ((mp->exports = (ExportPtr) calloc(mp->nExports, sizeof(*mp->exports))) == NULL) {
  581. errvalid++;
  582. strcpy(errbuf, "readExports: ");
  583. strcat(errbuf, strerror(errno));
  584. free(ldbuf);
  585. while (ldclose(ldp) == FAILURE);
  586. return -1;
  587. }
  588. /*
  589. * Fill in the export table. All entries are relative to
  590. * the beginning of the data origin.
  591. */
  592. ep = mp->exports;
  593. ls = (LDSYM *) (ldbuf + LDHDRSZ);
  594. for (i = lhp->l_nsyms; i; i--, ls++) {
  595. char *symname;
  596. char tmpsym[SYMNMLEN + 1];
  597. if (!LDR_EXPORT(*ls))
  598. continue;
  599. if (ls->l_zeroes == 0)
  600. symname = ls->l_offset + lhp->l_stoff + ldbuf;
  601. else {
  602. /*
  603. * The l_name member is not zero terminated, we
  604. * must copy the first SYMNMLEN chars and make
  605. * sure we have a zero byte at the end.
  606. */
  607. strncpy(tmpsym, ls->l_name, SYMNMLEN);
  608. tmpsym[SYMNMLEN] = '\0';
  609. symname = tmpsym;
  610. }
  611. ep->name = strdup(symname);
  612. ep->addr = (void *) ((unsigned long) dataorg +
  613. ls->l_value - shdata.s_vaddr);
  614. ep++;
  615. }
  616. free(ldbuf);
  617. while (ldclose(ldp) == FAILURE);
  618. return 0;
  619. }
  620. /*
  621. * Find the main modules data origin. This is used as export pointer
  622. * for loadbind() to be able to resolve references to the main part.
  623. */
  624. static void *findMain(void)
  625. {
  626. struct ld_info *lp;
  627. char *buf;
  628. int size = 4 * 1024;
  629. int i;
  630. void *ret;
  631. if ((buf = malloc(size)) == NULL) {
  632. errvalid++;
  633. strcpy(errbuf, "findMain: ");
  634. strcat(errbuf, strerror(errno));
  635. return NULL;
  636. }
  637. while ((i = loadquery(L_GETINFO, buf, size)) == -1 && errno == ENOMEM) {
  638. free(buf);
  639. size += 4 * 1024;
  640. if ((buf = malloc(size)) == NULL) {
  641. errvalid++;
  642. strcpy(errbuf, "findMain: ");
  643. strcat(errbuf, strerror(errno));
  644. return NULL;
  645. }
  646. }
  647. if (i == -1) {
  648. errvalid++;
  649. strcpy(errbuf, "findMain: ");
  650. strcat(errbuf, strerror(errno));
  651. free(buf);
  652. return NULL;
  653. }
  654. /*
  655. * The first entry is the main module. The data segment
  656. * starts with the TOC entries for all exports, so the
  657. * data segment origin works as argument for loadbind.
  658. */
  659. lp = (struct ld_info *) buf;
  660. ret = lp->ldinfo_dataorg;
  661. free(buf);
  662. return ret;
  663. }
  664. #endif