readelflib1.c 25 KB

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