dl-elf.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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 USE_CACHE
  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 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 USE_CACHE
  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_LIBC5) &&
  287. _dl_strcmp(libname, strs + libent[i].sooffset) == 0 &&
  288. (tpnt1 = _dl_load_elf_shared_library(secure,
  289. rpnt, strs + libent[i].liboffset)))
  290. return tpnt1;
  291. }
  292. }
  293. #endif
  294. /* Look for libraries wherever the shared library loader
  295. * was installed */
  296. #if defined (__SUPPORT_LD_DEBUG__)
  297. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching ldso dir='%s'\n", _dl_ldsopath);
  298. #endif
  299. if ((tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt)) != NULL)
  300. {
  301. return tpnt1;
  302. }
  303. /* Lastly, search the standard list of paths for the library.
  304. This list must exactly match the list in uClibc/ldso/util/ldd.c */
  305. #if defined (__SUPPORT_LD_DEBUG__)
  306. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching full lib path list\n");
  307. #endif
  308. if ((tpnt1 = search_for_named_library(libname, secure,
  309. UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib:"
  310. UCLIBC_RUNTIME_PREFIX "usr/lib:"
  311. UCLIBC_RUNTIME_PREFIX "lib:"
  312. "/usr/lib:"
  313. "/lib", rpnt)
  314. ) != NULL)
  315. {
  316. return tpnt1;
  317. }
  318. goof:
  319. /* Well, we shot our wad on that one. All we can do now is punt */
  320. if (_dl_internal_error_number)
  321. _dl_error_number = _dl_internal_error_number;
  322. else
  323. _dl_error_number = LD_ERROR_NOFILE;
  324. #if defined (__SUPPORT_LD_DEBUG__)
  325. if(_dl_debug) _dl_dprintf(2, "Bummer: could not find '%s'!\n", libname);
  326. #endif
  327. return NULL;
  328. }
  329. /*
  330. * Read one ELF library into memory, mmap it into the correct locations and
  331. * add the symbol info to the symbol chain. Perform any relocations that
  332. * are required.
  333. */
  334. struct elf_resolve *_dl_load_elf_shared_library(int secure,
  335. struct dyn_elf **rpnt, char *libname)
  336. {
  337. ElfW(Ehdr) *epnt;
  338. unsigned long dynamic_addr = 0;
  339. unsigned long dynamic_size = 0;
  340. Elf32_Dyn *dpnt;
  341. struct elf_resolve *tpnt;
  342. ElfW(Phdr) *ppnt;
  343. char *status, *header;
  344. unsigned long dynamic_info[24];
  345. unsigned long *lpnt;
  346. unsigned long libaddr;
  347. unsigned long minvma = 0xffffffff, maxvma = 0;
  348. int i, flags, piclib, infile;
  349. /* If this file is already loaded, skip this step */
  350. tpnt = _dl_check_hashed_files(libname);
  351. if (tpnt) {
  352. if (*rpnt) {
  353. (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  354. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  355. (*rpnt)->next->prev = (*rpnt);
  356. *rpnt = (*rpnt)->next;
  357. (*rpnt)->dyn = tpnt;
  358. tpnt->symbol_scope = _dl_symbol_tables;
  359. }
  360. tpnt->usage_count++;
  361. tpnt->libtype = elf_lib;
  362. #if defined (__SUPPORT_LD_DEBUG__)
  363. if(_dl_debug) _dl_dprintf(2, "file='%s'; already loaded\n", libname);
  364. #endif
  365. return tpnt;
  366. }
  367. /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
  368. we don't load the library if it isn't setuid. */
  369. if (secure) {
  370. struct stat st;
  371. if (_dl_stat(libname, &st) || !(st.st_mode & S_ISUID))
  372. return NULL;
  373. }
  374. libaddr = 0;
  375. infile = _dl_open(libname, O_RDONLY, 0);
  376. if (infile < 0) {
  377. #if 0
  378. /*
  379. * NO! When we open shared libraries we may search several paths.
  380. * it is inappropriate to generate an error here.
  381. */
  382. _dl_dprintf(2, "%s: can't open '%s'\n", _dl_progname, libname);
  383. #endif
  384. _dl_internal_error_number = LD_ERROR_NOFILE;
  385. return NULL;
  386. }
  387. header = _dl_mmap((void *) 0, PAGE_SIZE, PROT_READ | PROT_WRITE,
  388. MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  389. if (_dl_mmap_check_error(header)) {
  390. _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
  391. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  392. _dl_close(infile);
  393. return NULL;
  394. };
  395. _dl_read(infile, header, PAGE_SIZE);
  396. epnt = (ElfW(Ehdr) *) (intptr_t) header;
  397. if (epnt->e_ident[0] != 0x7f ||
  398. epnt->e_ident[1] != 'E' ||
  399. epnt->e_ident[2] != 'L' ||
  400. epnt->e_ident[3] != 'F')
  401. {
  402. _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  403. libname);
  404. _dl_internal_error_number = LD_ERROR_NOTELF;
  405. _dl_close(infile);
  406. _dl_munmap(header, PAGE_SIZE);
  407. return NULL;
  408. };
  409. if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
  410. #ifdef MAGIC2
  411. && epnt->e_machine != MAGIC2
  412. #endif
  413. ))
  414. {
  415. _dl_internal_error_number =
  416. (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
  417. _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  418. "\n", _dl_progname, libname);
  419. _dl_close(infile);
  420. _dl_munmap(header, PAGE_SIZE);
  421. return NULL;
  422. };
  423. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  424. piclib = 1;
  425. for (i = 0; i < epnt->e_phnum; i++) {
  426. if (ppnt->p_type == PT_DYNAMIC) {
  427. if (dynamic_addr)
  428. _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
  429. _dl_progname, libname);
  430. dynamic_addr = ppnt->p_vaddr;
  431. dynamic_size = ppnt->p_filesz;
  432. };
  433. if (ppnt->p_type == PT_LOAD) {
  434. /* See if this is a PIC library. */
  435. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  436. piclib = 0;
  437. minvma = ppnt->p_vaddr;
  438. }
  439. if (piclib && ppnt->p_vaddr < minvma) {
  440. minvma = ppnt->p_vaddr;
  441. }
  442. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  443. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  444. }
  445. }
  446. ppnt++;
  447. };
  448. maxvma = (maxvma + ADDR_ALIGN) & ~ADDR_ALIGN;
  449. minvma = minvma & ~0xffffU;
  450. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  451. if (!piclib)
  452. flags |= MAP_FIXED;
  453. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  454. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  455. if (_dl_mmap_check_error(status)) {
  456. _dl_dprintf(2, "%s: can't map %s\n", _dl_progname, libname);
  457. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  458. _dl_close(infile);
  459. _dl_munmap(header, PAGE_SIZE);
  460. return NULL;
  461. };
  462. libaddr = (unsigned long) status;
  463. flags |= MAP_FIXED;
  464. /* Get the memory to store the library */
  465. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  466. for (i = 0; i < epnt->e_phnum; i++) {
  467. if (ppnt->p_type == PT_LOAD) {
  468. /* See if this is a PIC library. */
  469. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  470. piclib = 0;
  471. /* flags |= MAP_FIXED; */
  472. }
  473. if (ppnt->p_flags & PF_W) {
  474. unsigned long map_size;
  475. char *cpnt;
  476. status = (char *) _dl_mmap((char *) ((piclib ? libaddr : 0) +
  477. (ppnt->p_vaddr & PAGE_ALIGN)), (ppnt->p_vaddr & ADDR_ALIGN)
  478. + ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags, infile,
  479. ppnt->p_offset & OFFS_ALIGN);
  480. if (_dl_mmap_check_error(status)) {
  481. _dl_dprintf(2, "%s: can't map '%s'\n",
  482. _dl_progname, libname);
  483. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  484. _dl_munmap((char *) libaddr, maxvma - minvma);
  485. _dl_close(infile);
  486. _dl_munmap(header, PAGE_SIZE);
  487. return NULL;
  488. };
  489. /* Pad the last page with zeroes. */
  490. cpnt = (char *) (status + (ppnt->p_vaddr & ADDR_ALIGN) +
  491. ppnt->p_filesz);
  492. while (((unsigned long) cpnt) & ADDR_ALIGN)
  493. *cpnt++ = 0;
  494. /* I am not quite sure if this is completely
  495. * correct to do or not, but the basic way that
  496. * we handle bss segments is that we mmap
  497. * /dev/zero if there are any pages left over
  498. * that are not mapped as part of the file */
  499. map_size = (ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) & PAGE_ALIGN;
  500. if (map_size < ppnt->p_vaddr + ppnt->p_memsz)
  501. status = (char *) _dl_mmap((char *) map_size +
  502. (piclib ? libaddr : 0),
  503. ppnt->p_vaddr + ppnt->p_memsz - map_size,
  504. LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS, -1, 0);
  505. } else
  506. status = (char *) _dl_mmap((char *) (ppnt->p_vaddr & PAGE_ALIGN)
  507. + (piclib ? libaddr : 0), (ppnt->p_vaddr & ADDR_ALIGN) +
  508. ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags,
  509. infile, ppnt->p_offset & OFFS_ALIGN);
  510. if (_dl_mmap_check_error(status)) {
  511. _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
  512. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  513. _dl_munmap((char *) libaddr, maxvma - minvma);
  514. _dl_close(infile);
  515. _dl_munmap(header, PAGE_SIZE);
  516. return NULL;
  517. };
  518. /* if(libaddr == 0 && piclib) {
  519. libaddr = (unsigned long) status;
  520. flags |= MAP_FIXED;
  521. }; */
  522. };
  523. ppnt++;
  524. };
  525. _dl_close(infile);
  526. /* For a non-PIC library, the addresses are all absolute */
  527. if (piclib) {
  528. dynamic_addr += (unsigned long) libaddr;
  529. }
  530. /*
  531. * OK, the ELF library is now loaded into VM in the correct locations
  532. * The next step is to go through and do the dynamic linking (if needed).
  533. */
  534. /* Start by scanning the dynamic section to get all of the pointers */
  535. if (!dynamic_addr) {
  536. _dl_internal_error_number = LD_ERROR_NODYNAMIC;
  537. _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
  538. _dl_progname, libname);
  539. _dl_munmap(header, PAGE_SIZE);
  540. return NULL;
  541. }
  542. dpnt = (Elf32_Dyn *) dynamic_addr;
  543. dynamic_size = dynamic_size / sizeof(Elf32_Dyn);
  544. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  545. #if defined(__mips__)
  546. {
  547. int indx = 1;
  548. Elf32_Dyn *dpnt = (Elf32_Dyn *) dynamic_addr;
  549. while(dpnt->d_tag) {
  550. dpnt++;
  551. indx++;
  552. }
  553. dynamic_size = indx;
  554. }
  555. #endif
  556. {
  557. unsigned long indx;
  558. for (indx = 0; indx < dynamic_size; indx++)
  559. {
  560. if (dpnt->d_tag > DT_JMPREL) {
  561. dpnt++;
  562. continue;
  563. }
  564. dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  565. if (dpnt->d_tag == DT_TEXTREL)
  566. dynamic_info[DT_TEXTREL] = 1;
  567. dpnt++;
  568. };
  569. }
  570. /* If the TEXTREL is set, this means that we need to make the pages
  571. writable before we perform relocations. Do this now. They get set
  572. back again later. */
  573. if (dynamic_info[DT_TEXTREL]) {
  574. #ifndef FORCE_SHAREABLE_TEXT_SEGMENTS
  575. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  576. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  577. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  578. _dl_mprotect((void *) ((piclib ? libaddr : 0) +
  579. (ppnt->p_vaddr & PAGE_ALIGN)),
  580. (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
  581. PROT_READ | PROT_WRITE | PROT_EXEC);
  582. }
  583. #else
  584. _dl_dprintf(_dl_debug_file, "Can't modify %s's text section. Use GCC option -fPIC for shared objects, please.\n",libname);
  585. _dl_exit(1);
  586. #endif
  587. }
  588. tpnt = _dl_add_elf_hash_table(libname, (char *) libaddr, dynamic_info,
  589. dynamic_addr, dynamic_size);
  590. tpnt->ppnt = (ElfW(Phdr) *)(intptr_t) (tpnt->loadaddr + epnt->e_phoff);
  591. tpnt->n_phent = epnt->e_phnum;
  592. /*
  593. * Add this object into the symbol chain
  594. */
  595. if (*rpnt) {
  596. (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  597. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  598. (*rpnt)->next->prev = (*rpnt);
  599. *rpnt = (*rpnt)->next;
  600. (*rpnt)->dyn = tpnt;
  601. tpnt->symbol_scope = _dl_symbol_tables;
  602. }
  603. tpnt->usage_count++;
  604. tpnt->libtype = elf_lib;
  605. /*
  606. * OK, the next thing we need to do is to insert the dynamic linker into
  607. * the proper entry in the GOT so that the PLT symbols can be properly
  608. * resolved.
  609. */
  610. lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
  611. if (lpnt) {
  612. lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT] +
  613. ((int) libaddr));
  614. INIT_GOT(lpnt, tpnt);
  615. };
  616. #if defined (__SUPPORT_LD_DEBUG__)
  617. if(_dl_debug) {
  618. _dl_dprintf(2, "\n\tfile='%s'; generating link map\n", libname);
  619. _dl_dprintf(2, "\t\tdynamic: %x base: %x size: %x\n",
  620. dynamic_addr, libaddr, dynamic_size);
  621. _dl_dprintf(2, "\t\t entry: %x phdr: %x phnum: %d\n\n",
  622. epnt->e_entry + libaddr, tpnt->ppnt, tpnt->n_phent);
  623. }
  624. #endif
  625. _dl_munmap(header, PAGE_SIZE);
  626. return tpnt;
  627. }
  628. int _dl_fixup(struct dyn_elf *rpnt, int flag)
  629. {
  630. int goof = 0;
  631. struct elf_resolve *tpnt;
  632. if (rpnt->next)
  633. goof += _dl_fixup(rpnt->next, flag);
  634. tpnt = rpnt->dyn;
  635. #if defined (__SUPPORT_LD_DEBUG__)
  636. if(_dl_debug) _dl_dprintf(_dl_debug_file,"\nrelocation processing: %s", tpnt->libname);
  637. #endif
  638. if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
  639. #if defined (__SUPPORT_LD_DEBUG__)
  640. if(_dl_debug) {
  641. _dl_dprintf(2, "%s: can't handle %s relocation records\n",
  642. _dl_progname, UNSUPPORTED_RELOC_STR);
  643. }
  644. #endif
  645. goof++;
  646. return goof;
  647. }
  648. if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR]) {
  649. if (tpnt->init_flag & RELOCS_DONE)
  650. return goof;
  651. tpnt->init_flag |= RELOCS_DONE;
  652. goof += _dl_parse_relocation_information(rpnt,
  653. tpnt->dynamic_info[DT_RELOC_TABLE_ADDR],
  654. tpnt->dynamic_info[DT_RELOC_TABLE_SIZE], 0);
  655. }
  656. if (tpnt->dynamic_info[DT_JMPREL]) {
  657. if (tpnt->init_flag & JMP_RELOCS_DONE)
  658. return goof;
  659. tpnt->init_flag |= JMP_RELOCS_DONE;
  660. if (flag & RTLD_LAZY) {
  661. _dl_parse_lazy_relocation_information(rpnt,
  662. tpnt->dynamic_info[DT_JMPREL],
  663. tpnt->dynamic_info [DT_PLTRELSZ], 0);
  664. } else {
  665. goof += _dl_parse_relocation_information(rpnt,
  666. tpnt->dynamic_info[DT_JMPREL],
  667. tpnt->dynamic_info[DT_PLTRELSZ], 0);
  668. }
  669. }
  670. if (tpnt->init_flag & COPY_RELOCS_DONE)
  671. return goof;
  672. tpnt->init_flag |= COPY_RELOCS_DONE;
  673. goof += _dl_parse_copy_information(rpnt,
  674. tpnt->dynamic_info[DT_RELOC_TABLE_ADDR],
  675. tpnt->dynamic_info[DT_RELOC_TABLE_SIZE], 0);
  676. #if defined (__SUPPORT_LD_DEBUG__)
  677. if(_dl_debug) {
  678. _dl_dprintf(_dl_debug_file,"\nrelocation processing: %s", tpnt->libname);
  679. _dl_dprintf(_dl_debug_file,"; finished\n\n");
  680. }
  681. #endif
  682. return goof;
  683. }
  684. /* Minimal printf which handles only %s, %d, and %x */
  685. void _dl_dprintf(int fd, const char *fmt, ...)
  686. {
  687. int num;
  688. va_list args;
  689. char *start, *ptr, *string;
  690. static char *buf;
  691. buf = _dl_mmap((void *) 0, PAGE_SIZE, PROT_READ | PROT_WRITE,
  692. MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  693. if (_dl_mmap_check_error(buf)) {
  694. _dl_write(fd, "mmap of a spare page failed!\n", 29);
  695. _dl_exit(20);
  696. }
  697. start = ptr = buf;
  698. if (!fmt)
  699. return;
  700. if (_dl_strlen(fmt) >= (PAGE_SIZE - 1)) {
  701. _dl_write(fd, "overflow\n", 11);
  702. _dl_exit(20);
  703. }
  704. _dl_strcpy(buf, fmt);
  705. va_start(args, fmt);
  706. while (start) {
  707. while (*ptr != '%' && *ptr) {
  708. ptr++;
  709. }
  710. if (*ptr == '%') {
  711. *ptr++ = '\0';
  712. _dl_write(fd, start, _dl_strlen(start));
  713. switch (*ptr++) {
  714. case 's':
  715. string = va_arg(args, char *);
  716. if (!string)
  717. _dl_write(fd, "(null)", 6);
  718. else
  719. _dl_write(fd, string, _dl_strlen(string));
  720. break;
  721. case 'i':
  722. case 'd':
  723. {
  724. char tmp[22];
  725. num = va_arg(args, int);
  726. string = _dl_simple_ltoa(tmp, num);
  727. _dl_write(fd, string, _dl_strlen(string));
  728. break;
  729. }
  730. case 'x':
  731. case 'X':
  732. {
  733. char tmp[22];
  734. num = va_arg(args, int);
  735. string = _dl_simple_ltoahex(tmp, num);
  736. _dl_write(fd, string, _dl_strlen(string));
  737. break;
  738. }
  739. default:
  740. _dl_write(fd, "(null)", 6);
  741. break;
  742. }
  743. start = ptr;
  744. } else {
  745. _dl_write(fd, start, _dl_strlen(start));
  746. start = NULL;
  747. }
  748. }
  749. _dl_munmap(buf, PAGE_SIZE);
  750. return;
  751. }
  752. char *_dl_strdup(const char *string)
  753. {
  754. char *retval;
  755. int len;
  756. len = _dl_strlen(string);
  757. retval = _dl_malloc(len + 1);
  758. _dl_strcpy(retval, string);
  759. return retval;
  760. }
  761. void *(*_dl_malloc_function) (size_t size) = NULL;
  762. void *_dl_malloc(int size)
  763. {
  764. void *retval;
  765. #if 0
  766. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  767. _dl_dprintf(2, "malloc: request for %d bytes\n", size);
  768. #endif
  769. #endif
  770. if (_dl_malloc_function)
  771. return (*_dl_malloc_function) (size);
  772. if (_dl_malloc_addr - _dl_mmap_zero + size > PAGE_SIZE) {
  773. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  774. _dl_dprintf(2, "malloc: mmapping more memory\n");
  775. #endif
  776. _dl_mmap_zero = _dl_malloc_addr = _dl_mmap((void *) 0, size,
  777. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  778. if (_dl_mmap_check_error(_dl_mmap_zero)) {
  779. _dl_dprintf(2, "%s: mmap of a spare page failed!\n", _dl_progname);
  780. _dl_exit(20);
  781. }
  782. }
  783. retval = _dl_malloc_addr;
  784. _dl_malloc_addr += size;
  785. /*
  786. * Align memory to 4 byte boundary. Some platforms require this, others
  787. * simply get better performance.
  788. */
  789. _dl_malloc_addr = (unsigned char *) (((unsigned long) _dl_malloc_addr + 3) & ~(3));
  790. return retval;
  791. }