dl-elf.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * This file contains the helper routines to load an ELF shared
  4. * library into memory and add the symbol table info to the chain.
  5. *
  6. * Copyright (C) 2000-2004 by Erik Andersen <andersen@codpoet.org>
  7. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  8. * David Engel, Hongjiu Lu and Mitch D'Souza
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. The name of the above contributors may not be
  16. * used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include "ldso.h"
  32. #ifdef __LDSO_CACHE_SUPPORT__
  33. static caddr_t _dl_cache_addr = NULL;
  34. static size_t _dl_cache_size = 0;
  35. int _dl_map_cache(void)
  36. {
  37. int fd;
  38. struct stat st;
  39. header_t *header;
  40. libentry_t *libent;
  41. int i, strtabsize;
  42. if (_dl_cache_addr == (caddr_t) - 1)
  43. return -1;
  44. else if (_dl_cache_addr != NULL)
  45. return 0;
  46. if (_dl_stat(LDSO_CACHE, &st)
  47. || (fd = _dl_open(LDSO_CACHE, O_RDONLY, 0)) < 0) {
  48. _dl_dprintf(2, "%s: can't open cache '%s'\n", _dl_progname, LDSO_CACHE);
  49. _dl_cache_addr = (caddr_t) - 1; /* so we won't try again */
  50. return -1;
  51. }
  52. _dl_cache_size = st.st_size;
  53. _dl_cache_addr = (caddr_t) _dl_mmap(0, _dl_cache_size, PROT_READ, MAP_SHARED, fd, 0);
  54. _dl_close(fd);
  55. if (_dl_mmap_check_error(_dl_cache_addr)) {
  56. _dl_dprintf(2, "%s: can't map cache '%s'\n",
  57. _dl_progname, LDSO_CACHE);
  58. return -1;
  59. }
  60. header = (header_t *) _dl_cache_addr;
  61. if (_dl_cache_size < sizeof(header_t) ||
  62. _dl_memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
  63. || _dl_memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
  64. || _dl_cache_size <
  65. (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
  66. || _dl_cache_addr[_dl_cache_size - 1] != '\0')
  67. {
  68. _dl_dprintf(2, "%s: cache '%s' is corrupt\n", _dl_progname,
  69. LDSO_CACHE);
  70. goto fail;
  71. }
  72. strtabsize = _dl_cache_size - sizeof(header_t) -
  73. header->nlibs * sizeof(libentry_t);
  74. libent = (libentry_t *) & header[1];
  75. for (i = 0; i < header->nlibs; i++) {
  76. if (libent[i].sooffset >= strtabsize ||
  77. libent[i].liboffset >= strtabsize)
  78. {
  79. _dl_dprintf(2, "%s: cache '%s' is corrupt\n", _dl_progname, LDSO_CACHE);
  80. goto fail;
  81. }
  82. }
  83. return 0;
  84. fail:
  85. _dl_munmap(_dl_cache_addr, _dl_cache_size);
  86. _dl_cache_addr = (caddr_t) - 1;
  87. return -1;
  88. }
  89. int _dl_unmap_cache(void)
  90. {
  91. if (_dl_cache_addr == NULL || _dl_cache_addr == (caddr_t) - 1)
  92. return -1;
  93. #if 1
  94. _dl_munmap(_dl_cache_addr, _dl_cache_size);
  95. _dl_cache_addr = NULL;
  96. #endif
  97. return 0;
  98. }
  99. #endif
  100. /* This function's behavior must exactly match that
  101. * in uClibc/ldso/util/ldd.c */
  102. static struct elf_resolve *
  103. search_for_named_library(const char *name, int secure, const char *path_list,
  104. struct dyn_elf **rpnt)
  105. {
  106. int i, count = 1;
  107. char *path, *path_n;
  108. char mylibname[2050];
  109. struct elf_resolve *tpnt1;
  110. if (path_list==NULL)
  111. return NULL;
  112. /* We need a writable copy of this string */
  113. path = _dl_strdup(path_list);
  114. if (!path) {
  115. _dl_dprintf(2, "Out of memory!\n");
  116. _dl_exit(0);
  117. }
  118. /* Unlike ldd.c, don't bother to eliminate double //s */
  119. /* Replace colons with zeros in path_list and count them */
  120. for(i=_dl_strlen(path); i > 0; i--) {
  121. if (path[i]==':') {
  122. path[i]=0;
  123. count++;
  124. }
  125. }
  126. path_n = path;
  127. for (i = 0; i < count; i++) {
  128. _dl_strcpy(mylibname, path_n);
  129. _dl_strcat(mylibname, "/");
  130. _dl_strcat(mylibname, name);
  131. if ((tpnt1 = _dl_load_elf_shared_library(secure, rpnt, mylibname)) != NULL)
  132. {
  133. return tpnt1;
  134. }
  135. path_n += (_dl_strlen(path_n) + 1);
  136. }
  137. return NULL;
  138. }
  139. /* Check if the named library is already loaded... */
  140. struct elf_resolve *_dl_check_if_named_library_is_loaded(const char *full_libname,
  141. int trace_loaded_objects)
  142. {
  143. const char *pnt, *pnt1;
  144. struct elf_resolve *tpnt1;
  145. const char *libname, *libname2;
  146. static const char libc[] = "libc.so.";
  147. static const char aborted_wrong_lib[] = "%s: aborted attempt to load %s!\n";
  148. pnt = libname = full_libname;
  149. #if defined (__SUPPORT_LD_DEBUG__)
  150. if(_dl_debug)
  151. _dl_dprintf(_dl_debug_file, "Checking if '%s' is already loaded\n", full_libname);
  152. #endif
  153. /* quick hack to ensure mylibname buffer doesn't overflow. don't
  154. allow full_libname or any directory to be longer than 1024. */
  155. if (_dl_strlen(full_libname) > 1024)
  156. return NULL;
  157. /* Skip over any initial initial './' and '/' stuff to
  158. * get the short form libname with no path garbage */
  159. pnt1 = _dl_strrchr(pnt, '/');
  160. if (pnt1) {
  161. libname = pnt1 + 1;
  162. }
  163. /* Make sure they are not trying to load the wrong C library!
  164. * This sometimes happens esp with shared libraries when the
  165. * library path is somehow wrong! */
  166. #define isdigit(c) (c >= '0' && c <= '9')
  167. if ((_dl_strncmp(libname, libc, 8) == 0) && _dl_strlen(libname) >=8 &&
  168. isdigit(libname[8]))
  169. {
  170. /* Abort attempts to load glibc, libc5, etc */
  171. if ( libname[8]!='0') {
  172. if (!trace_loaded_objects) {
  173. _dl_dprintf(2, aborted_wrong_lib, libname, _dl_progname);
  174. _dl_exit(1);
  175. }
  176. return NULL;
  177. }
  178. }
  179. /* Critical step! Weed out duplicates early to avoid
  180. * function aliasing, which wastes memory, and causes
  181. * really bad things to happen with weaks and globals. */
  182. for (tpnt1 = _dl_loaded_modules; tpnt1; tpnt1 = tpnt1->next) {
  183. /* Skip over any initial initial './' and '/' stuff to
  184. * get the short form libname with no path garbage */
  185. libname2 = tpnt1->libname;
  186. pnt1 = _dl_strrchr(libname2, '/');
  187. if (pnt1) {
  188. libname2 = pnt1 + 1;
  189. }
  190. if (_dl_strcmp(libname2, libname) == 0) {
  191. /* Well, that was certainly easy */
  192. return tpnt1;
  193. }
  194. }
  195. return NULL;
  196. }
  197. /* Used to return error codes back to dlopen et. al. */
  198. unsigned long _dl_error_number;
  199. unsigned long _dl_internal_error_number;
  200. struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
  201. struct elf_resolve *tpnt, char *full_libname, int __attribute__((unused)) trace_loaded_objects)
  202. {
  203. char *pnt, *pnt1;
  204. struct elf_resolve *tpnt1;
  205. char *libname;
  206. _dl_internal_error_number = 0;
  207. libname = full_libname;
  208. /* quick hack to ensure mylibname buffer doesn't overflow. don't
  209. allow full_libname or any directory to be longer than 1024. */
  210. if (_dl_strlen(full_libname) > 1024)
  211. goto goof;
  212. /* Skip over any initial initial './' and '/' stuff to
  213. * get the short form libname with no path garbage */
  214. pnt1 = _dl_strrchr(libname, '/');
  215. if (pnt1) {
  216. libname = pnt1 + 1;
  217. }
  218. #if 0
  219. /* Critical step! Weed out duplicates early to avoid
  220. * function aliasing, which wastes memory, and causes
  221. * really bad things to happen with weaks and globals. */
  222. if ((tpnt1=_dl_check_if_named_library_is_loaded(libname, trace_loaded_objects))!=NULL)
  223. return tpnt1;
  224. #endif
  225. #if defined (__SUPPORT_LD_DEBUG__)
  226. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tfind library='%s'; searching\n", libname);
  227. #endif
  228. /* If the filename has any '/', try it straight and leave it at that.
  229. For IBCS2 compatibility under linux, we substitute the string
  230. /usr/i486-sysv4/lib for /usr/lib in library names. */
  231. if (libname != full_libname) {
  232. #if defined (__SUPPORT_LD_DEBUG__)
  233. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\ttrying file='%s'\n", full_libname);
  234. #endif
  235. tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
  236. if (tpnt1) {
  237. return tpnt1;
  238. }
  239. //goto goof;
  240. }
  241. /*
  242. * The ABI specifies that RPATH is searched before LD_*_PATH or
  243. * the default path of /usr/lib. Check in rpath directories.
  244. */
  245. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  246. if (tpnt->libtype == elf_executable) {
  247. pnt = (char *) tpnt->dynamic_info[DT_RPATH];
  248. if (pnt) {
  249. pnt += (unsigned long) tpnt->loadaddr + tpnt->dynamic_info[DT_STRTAB];
  250. #if defined (__SUPPORT_LD_DEBUG__)
  251. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching RPATH='%s'\n", pnt);
  252. #endif
  253. if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
  254. {
  255. return tpnt1;
  256. }
  257. }
  258. }
  259. }
  260. /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
  261. if (_dl_library_path) {
  262. #if defined (__SUPPORT_LD_DEBUG__)
  263. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
  264. #endif
  265. if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
  266. {
  267. return tpnt1;
  268. }
  269. }
  270. /*
  271. * Where should the cache be searched? There is no such concept in the
  272. * ABI, so we have some flexibility here. For now, search it before
  273. * the hard coded paths that follow (i.e before /lib and /usr/lib).
  274. */
  275. #ifdef __LDSO_CACHE_SUPPORT__
  276. if (_dl_cache_addr != NULL && _dl_cache_addr != (caddr_t) - 1) {
  277. int i;
  278. header_t *header = (header_t *) _dl_cache_addr;
  279. libentry_t *libent = (libentry_t *) & header[1];
  280. char *strs = (char *) &libent[header->nlibs];
  281. #if defined (__SUPPORT_LD_DEBUG__)
  282. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching cache='%s'\n", LDSO_CACHE);
  283. #endif
  284. for (i = 0; i < header->nlibs; i++) {
  285. if ((libent[i].flags == LIB_ELF ||
  286. libent[i].flags == LIB_ELF_LIBC0 ||
  287. libent[i].flags == LIB_ELF_LIBC5) &&
  288. _dl_strcmp(libname, strs + libent[i].sooffset) == 0 &&
  289. (tpnt1 = _dl_load_elf_shared_library(secure,
  290. rpnt, strs + libent[i].liboffset)))
  291. return tpnt1;
  292. }
  293. }
  294. #endif
  295. /* Look for libraries wherever the shared library loader
  296. * was installed */
  297. #if defined (__SUPPORT_LD_DEBUG__)
  298. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching ldso dir='%s'\n", _dl_ldsopath);
  299. #endif
  300. if ((tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt)) != NULL)
  301. {
  302. return tpnt1;
  303. }
  304. /* Lastly, search the standard list of paths for the library.
  305. This list must exactly match the list in uClibc/ldso/util/ldd.c */
  306. #if defined (__SUPPORT_LD_DEBUG__)
  307. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching full lib path list\n");
  308. #endif
  309. if ((tpnt1 = search_for_named_library(libname, secure,
  310. UCLIBC_RUNTIME_PREFIX "lib:"
  311. UCLIBC_RUNTIME_PREFIX "usr/lib"
  312. #if !defined (__LDSO_CACHE_SUPPORT__)
  313. ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
  314. #endif
  315. , rpnt)
  316. ) != NULL)
  317. {
  318. return tpnt1;
  319. }
  320. goof:
  321. /* Well, we shot our wad on that one. All we can do now is punt */
  322. if (_dl_internal_error_number)
  323. _dl_error_number = _dl_internal_error_number;
  324. else
  325. _dl_error_number = LD_ERROR_NOFILE;
  326. #if defined (__SUPPORT_LD_DEBUG__)
  327. if(_dl_debug) _dl_dprintf(2, "Bummer: could not find '%s'!\n", libname);
  328. #endif
  329. return NULL;
  330. }
  331. /*
  332. * Read one ELF library into memory, mmap it into the correct locations and
  333. * add the symbol info to the symbol chain. Perform any relocations that
  334. * are required.
  335. */
  336. struct elf_resolve *_dl_load_elf_shared_library(int secure,
  337. struct dyn_elf **rpnt, char *libname)
  338. {
  339. ElfW(Ehdr) *epnt;
  340. unsigned long dynamic_addr = 0;
  341. unsigned long dynamic_size = 0;
  342. Elf32_Dyn *dpnt;
  343. struct elf_resolve *tpnt;
  344. ElfW(Phdr) *ppnt;
  345. char *status, *header;
  346. unsigned long dynamic_info[24];
  347. unsigned long *lpnt;
  348. unsigned long libaddr;
  349. unsigned long minvma = 0xffffffff, maxvma = 0;
  350. int i, flags, piclib, infile;
  351. /* If this file is already loaded, skip this step */
  352. tpnt = _dl_check_hashed_files(libname);
  353. if (tpnt) {
  354. if (*rpnt) {
  355. (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  356. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  357. (*rpnt)->next->prev = (*rpnt);
  358. *rpnt = (*rpnt)->next;
  359. (*rpnt)->dyn = tpnt;
  360. tpnt->symbol_scope = _dl_symbol_tables;
  361. }
  362. tpnt->usage_count++;
  363. tpnt->libtype = elf_lib;
  364. #if defined (__SUPPORT_LD_DEBUG__)
  365. if(_dl_debug) _dl_dprintf(2, "file='%s'; already loaded\n", libname);
  366. #endif
  367. return tpnt;
  368. }
  369. /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
  370. we don't load the library if it isn't setuid. */
  371. if (secure) {
  372. struct stat st;
  373. if (_dl_stat(libname, &st) || !(st.st_mode & S_ISUID))
  374. return NULL;
  375. }
  376. libaddr = 0;
  377. infile = _dl_open(libname, O_RDONLY, 0);
  378. if (infile < 0) {
  379. #if 0
  380. /*
  381. * NO! When we open shared libraries we may search several paths.
  382. * it is inappropriate to generate an error here.
  383. */
  384. _dl_dprintf(2, "%s: can't open '%s'\n", _dl_progname, libname);
  385. #endif
  386. _dl_internal_error_number = LD_ERROR_NOFILE;
  387. return NULL;
  388. }
  389. header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  390. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  391. if (_dl_mmap_check_error(header)) {
  392. _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
  393. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  394. _dl_close(infile);
  395. return NULL;
  396. };
  397. _dl_read(infile, header, _dl_pagesize);
  398. epnt = (ElfW(Ehdr) *) (intptr_t) header;
  399. if (epnt->e_ident[0] != 0x7f ||
  400. epnt->e_ident[1] != 'E' ||
  401. epnt->e_ident[2] != 'L' ||
  402. epnt->e_ident[3] != 'F')
  403. {
  404. _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  405. libname);
  406. _dl_internal_error_number = LD_ERROR_NOTELF;
  407. _dl_close(infile);
  408. _dl_munmap(header, _dl_pagesize);
  409. return NULL;
  410. };
  411. if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
  412. #ifdef MAGIC2
  413. && epnt->e_machine != MAGIC2
  414. #endif
  415. ))
  416. {
  417. _dl_internal_error_number =
  418. (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
  419. _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  420. "\n", _dl_progname, libname);
  421. _dl_close(infile);
  422. _dl_munmap(header, _dl_pagesize);
  423. return NULL;
  424. };
  425. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  426. piclib = 1;
  427. for (i = 0; i < epnt->e_phnum; i++) {
  428. if (ppnt->p_type == PT_DYNAMIC) {
  429. if (dynamic_addr)
  430. _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
  431. _dl_progname, libname);
  432. dynamic_addr = ppnt->p_vaddr;
  433. dynamic_size = ppnt->p_filesz;
  434. };
  435. if (ppnt->p_type == PT_LOAD) {
  436. /* See if this is a PIC library. */
  437. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  438. piclib = 0;
  439. minvma = ppnt->p_vaddr;
  440. }
  441. if (piclib && ppnt->p_vaddr < minvma) {
  442. minvma = ppnt->p_vaddr;
  443. }
  444. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  445. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  446. }
  447. }
  448. ppnt++;
  449. };
  450. maxvma = (maxvma + ADDR_ALIGN) & ~ADDR_ALIGN;
  451. minvma = minvma & ~0xffffU;
  452. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  453. if (!piclib)
  454. flags |= MAP_FIXED;
  455. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  456. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  457. if (_dl_mmap_check_error(status)) {
  458. _dl_dprintf(2, "%s: can't map %s\n", _dl_progname, libname);
  459. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  460. _dl_close(infile);
  461. _dl_munmap(header, _dl_pagesize);
  462. return NULL;
  463. };
  464. libaddr = (unsigned long) status;
  465. flags |= MAP_FIXED;
  466. /* Get the memory to store the library */
  467. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  468. for (i = 0; i < epnt->e_phnum; i++) {
  469. if (ppnt->p_type == PT_LOAD) {
  470. /* See if this is a PIC library. */
  471. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  472. piclib = 0;
  473. /* flags |= MAP_FIXED; */
  474. }
  475. if (ppnt->p_flags & PF_W) {
  476. unsigned long map_size;
  477. char *cpnt;
  478. status = (char *) _dl_mmap((char *) ((piclib ? libaddr : 0) +
  479. (ppnt->p_vaddr & PAGE_ALIGN)), (ppnt->p_vaddr & ADDR_ALIGN)
  480. + ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags, infile,
  481. ppnt->p_offset & OFFS_ALIGN);
  482. if (_dl_mmap_check_error(status)) {
  483. _dl_dprintf(2, "%s: can't map '%s'\n",
  484. _dl_progname, libname);
  485. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  486. _dl_munmap((char *) libaddr, maxvma - minvma);
  487. _dl_close(infile);
  488. _dl_munmap(header, _dl_pagesize);
  489. return NULL;
  490. };
  491. /* Pad the last page with zeroes. */
  492. cpnt = (char *) (status + (ppnt->p_vaddr & ADDR_ALIGN) +
  493. ppnt->p_filesz);
  494. while (((unsigned long) cpnt) & ADDR_ALIGN)
  495. *cpnt++ = 0;
  496. /* I am not quite sure if this is completely
  497. * correct to do or not, but the basic way that
  498. * we handle bss segments is that we mmap
  499. * /dev/zero if there are any pages left over
  500. * that are not mapped as part of the file */
  501. map_size = (ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) & PAGE_ALIGN;
  502. if (map_size < ppnt->p_vaddr + ppnt->p_memsz)
  503. status = (char *) _dl_mmap((char *) map_size +
  504. (piclib ? libaddr : 0),
  505. ppnt->p_vaddr + ppnt->p_memsz - map_size,
  506. LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS, -1, 0);
  507. } else
  508. status = (char *) _dl_mmap((char *) (ppnt->p_vaddr & PAGE_ALIGN)
  509. + (piclib ? libaddr : 0), (ppnt->p_vaddr & ADDR_ALIGN) +
  510. ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags,
  511. infile, ppnt->p_offset & OFFS_ALIGN);
  512. if (_dl_mmap_check_error(status)) {
  513. _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
  514. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  515. _dl_munmap((char *) libaddr, maxvma - minvma);
  516. _dl_close(infile);
  517. _dl_munmap(header, _dl_pagesize);
  518. return NULL;
  519. };
  520. /* if(libaddr == 0 && piclib) {
  521. libaddr = (unsigned long) status;
  522. flags |= MAP_FIXED;
  523. }; */
  524. };
  525. ppnt++;
  526. };
  527. _dl_close(infile);
  528. /* For a non-PIC library, the addresses are all absolute */
  529. if (piclib) {
  530. dynamic_addr += (unsigned long) libaddr;
  531. }
  532. /*
  533. * OK, the ELF library is now loaded into VM in the correct locations
  534. * The next step is to go through and do the dynamic linking (if needed).
  535. */
  536. /* Start by scanning the dynamic section to get all of the pointers */
  537. if (!dynamic_addr) {
  538. _dl_internal_error_number = LD_ERROR_NODYNAMIC;
  539. _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
  540. _dl_progname, libname);
  541. _dl_munmap(header, _dl_pagesize);
  542. return NULL;
  543. }
  544. dpnt = (Elf32_Dyn *) dynamic_addr;
  545. dynamic_size = dynamic_size / sizeof(Elf32_Dyn);
  546. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  547. #if defined(__mips__)
  548. {
  549. int indx = 1;
  550. Elf32_Dyn *dpnt = (Elf32_Dyn *) dynamic_addr;
  551. while(dpnt->d_tag) {
  552. dpnt++;
  553. indx++;
  554. }
  555. dynamic_size = indx;
  556. }
  557. #endif
  558. {
  559. unsigned long indx;
  560. for (indx = 0; indx < dynamic_size; indx++)
  561. {
  562. if (dpnt->d_tag > DT_JMPREL) {
  563. dpnt++;
  564. continue;
  565. }
  566. dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  567. if (dpnt->d_tag == DT_TEXTREL)
  568. dynamic_info[DT_TEXTREL] = 1;
  569. dpnt++;
  570. };
  571. }
  572. /* If the TEXTREL is set, this means that we need to make the pages
  573. writable before we perform relocations. Do this now. They get set
  574. back again later. */
  575. if (dynamic_info[DT_TEXTREL]) {
  576. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  577. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  578. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  579. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  580. _dl_mprotect((void *) ((piclib ? libaddr : 0) +
  581. (ppnt->p_vaddr & PAGE_ALIGN)),
  582. (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
  583. PROT_READ | PROT_WRITE | PROT_EXEC);
  584. }
  585. #else
  586. _dl_dprintf(_dl_debug_file, "Can't modify %s's text section. Use GCC option -fPIC for shared objects, please.\n",libname);
  587. _dl_exit(1);
  588. #endif
  589. }
  590. tpnt = _dl_add_elf_hash_table(libname, (char *) libaddr, dynamic_info,
  591. dynamic_addr, dynamic_size);
  592. tpnt->ppnt = (ElfW(Phdr) *)(intptr_t) (tpnt->loadaddr + epnt->e_phoff);
  593. tpnt->n_phent = epnt->e_phnum;
  594. /*
  595. * Add this object into the symbol chain
  596. */
  597. if (*rpnt) {
  598. (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  599. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  600. (*rpnt)->next->prev = (*rpnt);
  601. *rpnt = (*rpnt)->next;
  602. (*rpnt)->dyn = tpnt;
  603. tpnt->symbol_scope = _dl_symbol_tables;
  604. }
  605. tpnt->usage_count++;
  606. tpnt->libtype = elf_lib;
  607. /*
  608. * OK, the next thing we need to do is to insert the dynamic linker into
  609. * the proper entry in the GOT so that the PLT symbols can be properly
  610. * resolved.
  611. */
  612. lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
  613. if (lpnt) {
  614. lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT] +
  615. ((int) libaddr));
  616. INIT_GOT(lpnt, tpnt);
  617. };
  618. #if defined (__SUPPORT_LD_DEBUG__)
  619. if(_dl_debug) {
  620. _dl_dprintf(2, "\n\tfile='%s'; generating link map\n", libname);
  621. _dl_dprintf(2, "\t\tdynamic: %x base: %x size: %x\n",
  622. dynamic_addr, libaddr, dynamic_size);
  623. _dl_dprintf(2, "\t\t entry: %x phdr: %x phnum: %x\n\n",
  624. epnt->e_entry + libaddr, tpnt->ppnt, tpnt->n_phent);
  625. }
  626. #endif
  627. _dl_munmap(header, _dl_pagesize);
  628. return tpnt;
  629. }
  630. /* now_flag must be RTLD_NOW or zero */
  631. int _dl_fixup(struct dyn_elf *rpnt, int now_flag)
  632. {
  633. int goof = 0;
  634. struct elf_resolve *tpnt;
  635. unsigned long reloc_size;
  636. if (rpnt->next)
  637. goof += _dl_fixup(rpnt->next, now_flag);
  638. tpnt = rpnt->dyn;
  639. #if defined (__SUPPORT_LD_DEBUG__)
  640. if(_dl_debug) _dl_dprintf(_dl_debug_file,"\nrelocation processing: %s", tpnt->libname);
  641. #endif
  642. if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
  643. #if defined (__SUPPORT_LD_DEBUG__)
  644. if(_dl_debug) {
  645. _dl_dprintf(2, "%s: can't handle %s relocation records\n",
  646. _dl_progname, UNSUPPORTED_RELOC_STR);
  647. }
  648. #endif
  649. goof++;
  650. return goof;
  651. }
  652. reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
  653. /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
  654. range. Note that according to the ELF spec, this is completely legal! */
  655. #ifdef ELF_MACHINE_PLTREL_OVERLAP
  656. reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
  657. #endif
  658. if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
  659. !(tpnt->init_flag & RELOCS_DONE)) {
  660. tpnt->init_flag |= RELOCS_DONE;
  661. goof += _dl_parse_relocation_information(rpnt,
  662. tpnt->dynamic_info[DT_RELOC_TABLE_ADDR],
  663. reloc_size);
  664. }
  665. if (tpnt->dynamic_info[DT_JMPREL] &&
  666. (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
  667. (now_flag && !(tpnt->rtld_flags & now_flag)))) {
  668. tpnt->rtld_flags |= now_flag;
  669. tpnt->init_flag |= JMP_RELOCS_DONE;
  670. if (!(tpnt->rtld_flags & RTLD_NOW)) {
  671. _dl_parse_lazy_relocation_information(rpnt,
  672. tpnt->dynamic_info[DT_JMPREL],
  673. tpnt->dynamic_info [DT_PLTRELSZ]);
  674. } else {
  675. goof += _dl_parse_relocation_information(rpnt,
  676. tpnt->dynamic_info[DT_JMPREL],
  677. tpnt->dynamic_info[DT_PLTRELSZ]);
  678. }
  679. }
  680. if (tpnt->init_flag & COPY_RELOCS_DONE)
  681. return goof;
  682. tpnt->init_flag |= COPY_RELOCS_DONE;
  683. goof += _dl_parse_copy_information(rpnt,
  684. tpnt->dynamic_info[DT_RELOC_TABLE_ADDR],
  685. tpnt->dynamic_info[DT_RELOC_TABLE_SIZE]);
  686. #if defined (__SUPPORT_LD_DEBUG__)
  687. if(_dl_debug) {
  688. _dl_dprintf(_dl_debug_file,"\nrelocation processing: %s", tpnt->libname);
  689. _dl_dprintf(_dl_debug_file,"; finished\n\n");
  690. }
  691. #endif
  692. return goof;
  693. }
  694. /* Minimal printf which handles only %s, %d, and %x */
  695. void _dl_dprintf(int fd, const char *fmt, ...)
  696. {
  697. int num;
  698. va_list args;
  699. char *start, *ptr, *string;
  700. static char *buf;
  701. buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  702. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  703. if (_dl_mmap_check_error(buf)) {
  704. _dl_write(fd, "mmap of a spare page failed!\n", 29);
  705. _dl_exit(20);
  706. }
  707. start = ptr = buf;
  708. if (!fmt)
  709. return;
  710. if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
  711. _dl_write(fd, "overflow\n", 11);
  712. _dl_exit(20);
  713. }
  714. _dl_strcpy(buf, fmt);
  715. va_start(args, fmt);
  716. while (start) {
  717. while (*ptr != '%' && *ptr) {
  718. ptr++;
  719. }
  720. if (*ptr == '%') {
  721. *ptr++ = '\0';
  722. _dl_write(fd, start, _dl_strlen(start));
  723. switch (*ptr++) {
  724. case 's':
  725. string = va_arg(args, char *);
  726. if (!string)
  727. _dl_write(fd, "(null)", 6);
  728. else
  729. _dl_write(fd, string, _dl_strlen(string));
  730. break;
  731. case 'i':
  732. case 'd':
  733. {
  734. char tmp[22];
  735. num = va_arg(args, int);
  736. string = _dl_simple_ltoa(tmp, num);
  737. _dl_write(fd, string, _dl_strlen(string));
  738. break;
  739. }
  740. case 'x':
  741. case 'X':
  742. {
  743. char tmp[22];
  744. num = va_arg(args, int);
  745. string = _dl_simple_ltoahex(tmp, num);
  746. _dl_write(fd, string, _dl_strlen(string));
  747. break;
  748. }
  749. default:
  750. _dl_write(fd, "(null)", 6);
  751. break;
  752. }
  753. start = ptr;
  754. } else {
  755. _dl_write(fd, start, _dl_strlen(start));
  756. start = NULL;
  757. }
  758. }
  759. _dl_munmap(buf, _dl_pagesize);
  760. return;
  761. }
  762. char *_dl_strdup(const char *string)
  763. {
  764. char *retval;
  765. int len;
  766. len = _dl_strlen(string);
  767. retval = _dl_malloc(len + 1);
  768. _dl_strcpy(retval, string);
  769. return retval;
  770. }
  771. #ifdef __USE_GNU
  772. #if ! defined LIBDL || (! defined PIC && ! defined __PIC__)
  773. int
  774. __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info, size_t size, void *data), void *data)
  775. {
  776. struct elf_resolve *l;
  777. struct dl_phdr_info info;
  778. int ret = 0;
  779. for (l = _dl_loaded_modules; l != NULL; l = l->next) {
  780. info.dlpi_addr = l->loadaddr;
  781. info.dlpi_name = l->libname;
  782. info.dlpi_phdr = l->ppnt;
  783. info.dlpi_phnum = l->n_phent;
  784. ret = callback (&info, sizeof (struct dl_phdr_info), data);
  785. if (ret)
  786. break;
  787. }
  788. return ret;
  789. }
  790. strong_alias(__dl_iterate_phdr, dl_iterate_phdr);
  791. #endif
  792. #endif