readelflib1.c 21 KB

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