dl-elf.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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_mmap_check_error(_dl_cache_addr)) {
  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(const 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, mylibname)) != NULL)
  134. {
  135. return tpnt1;
  136. }
  137. path_n += (_dl_strlen(path_n) + 1);
  138. }
  139. return NULL;
  140. }
  141. /* Check if the named library is already loaded... */
  142. struct elf_resolve *_dl_check_if_named_library_is_loaded(const char *full_libname)
  143. {
  144. const char *pnt, *pnt1;
  145. struct elf_resolve *tpnt1;
  146. const char *libname, *libname2;
  147. static const char *libc = "libc.so.";
  148. static const char *aborted_wrong_lib = "%s: aborted attempt to load %s!\n";
  149. pnt = libname = full_libname;
  150. #if defined (__SUPPORT_LD_DEBUG__)
  151. if(_dl_debug)
  152. _dl_dprintf(_dl_debug_file, "Checking if '%s' is already loaded\n", full_libname);
  153. #endif
  154. /* quick hack to ensure mylibname buffer doesn't overflow. don't
  155. allow full_libname or any directory to be longer than 1024. */
  156. if (_dl_strlen(full_libname) > 1024)
  157. return NULL;
  158. /* Skip over any initial initial './' and '/' stuff to
  159. * get the short form libname with no path garbage */
  160. pnt1 = _dl_strrchr(pnt, '/');
  161. if (pnt1) {
  162. libname = pnt1 + 1;
  163. }
  164. /* Make sure they are not trying to load the wrong C library!
  165. * This sometimes happens esp with shared libraries when the
  166. * library path is somehow wrong! */
  167. #define isdigit(c) (c >= '0' && c <= '9')
  168. if ((_dl_strncmp(libname, libc, 8) == 0) && _dl_strlen(libname) >=8 &&
  169. isdigit(libname[8]))
  170. {
  171. /* Abort attempts to load glibc, libc5, etc */
  172. if ( libname[8]!='0') {
  173. if (!_dl_trace_loaded_objects) {
  174. _dl_dprintf(2, aborted_wrong_lib, libname, _dl_progname);
  175. _dl_exit(1);
  176. }
  177. return NULL;
  178. }
  179. }
  180. /* Critical step! Weed out duplicates early to avoid
  181. * function aliasing, which wastes memory, and causes
  182. * really bad things to happen with weaks and globals. */
  183. for (tpnt1 = _dl_loaded_modules; tpnt1; tpnt1 = tpnt1->next) {
  184. /* Skip over any initial initial './' and '/' stuff to
  185. * get the short form libname with no path garbage */
  186. libname2 = tpnt1->libname;
  187. pnt1 = _dl_strrchr(libname2, '/');
  188. if (pnt1) {
  189. libname2 = pnt1 + 1;
  190. }
  191. if (_dl_strcmp(libname2, libname) == 0) {
  192. /* Well, that was certainly easy */
  193. return tpnt1;
  194. }
  195. }
  196. return NULL;
  197. }
  198. /*
  199. * Used to return error codes back to dlopen et. al.
  200. */
  201. unsigned long _dl_error_number;
  202. unsigned long _dl_internal_error_number;
  203. extern char *_dl_ldsopath;
  204. struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
  205. struct elf_resolve *tpnt, char *full_libname)
  206. {
  207. char *pnt, *pnt1;
  208. struct elf_resolve *tpnt1;
  209. char *libname;
  210. _dl_internal_error_number = 0;
  211. libname = full_libname;
  212. /* quick hack to ensure mylibname buffer doesn't overflow. don't
  213. allow full_libname or any directory to be longer than 1024. */
  214. if (_dl_strlen(full_libname) > 1024)
  215. goto goof;
  216. /* Skip over any initial initial './' and '/' stuff to
  217. * get the short form libname with no path garbage */
  218. pnt1 = _dl_strrchr(libname, '/');
  219. if (pnt1) {
  220. libname = pnt1 + 1;
  221. }
  222. /* Critical step! Weed out duplicates early to avoid
  223. * function aliasing, which wastes memory, and causes
  224. * really bad things to happen with weaks and globals. */
  225. if ((tpnt1=_dl_check_if_named_library_is_loaded(libname))!=NULL)
  226. return tpnt1;
  227. #if defined (__SUPPORT_LD_DEBUG__)
  228. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tfind library='%s'; searching\n", libname);
  229. #endif
  230. /* If the filename has any '/', try it straight and leave it at that.
  231. For IBCS2 compatibility under linux, we substitute the string
  232. /usr/i486-sysv4/lib for /usr/lib in library names. */
  233. if (libname != full_libname) {
  234. #if defined (__SUPPORT_LD_DEBUG__)
  235. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\ttrying file='%s'\n", full_libname);
  236. #endif
  237. tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
  238. if (tpnt1) {
  239. return tpnt1;
  240. }
  241. //goto goof;
  242. }
  243. /*
  244. * The ABI specifies that RPATH is searched before LD_*_PATH or
  245. * the default path of /usr/lib. Check in rpath directories.
  246. */
  247. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  248. if (tpnt->libtype == elf_executable) {
  249. pnt = (char *) tpnt->dynamic_info[DT_RPATH];
  250. if (pnt) {
  251. pnt += (unsigned long) tpnt->loadaddr + tpnt->dynamic_info[DT_STRTAB];
  252. #if defined (__SUPPORT_LD_DEBUG__)
  253. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching RPATH='%s'\n", pnt);
  254. #endif
  255. if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
  256. {
  257. return tpnt1;
  258. }
  259. }
  260. }
  261. }
  262. /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
  263. if (_dl_library_path) {
  264. #if defined (__SUPPORT_LD_DEBUG__)
  265. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
  266. #endif
  267. if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
  268. {
  269. return tpnt1;
  270. }
  271. }
  272. /*
  273. * Where should the cache be searched? There is no such concept in the
  274. * ABI, so we have some flexibility here. For now, search it before
  275. * the hard coded paths that follow (i.e before /lib and /usr/lib).
  276. */
  277. #ifdef USE_CACHE
  278. if (_dl_cache_addr != NULL && _dl_cache_addr != (caddr_t) - 1) {
  279. int i;
  280. header_t *header = (header_t *) _dl_cache_addr;
  281. libentry_t *libent = (libentry_t *) & header[1];
  282. char *strs = (char *) &libent[header->nlibs];
  283. #if defined (__SUPPORT_LD_DEBUG__)
  284. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching cache='%s'\n", LDSO_CACHE);
  285. #endif
  286. for (i = 0; i < header->nlibs; i++) {
  287. if ((libent[i].flags == LIB_ELF ||
  288. libent[i].flags == LIB_ELF_LIBC5) &&
  289. _dl_strcmp(libname, strs + libent[i].sooffset) == 0 &&
  290. (tpnt1 = _dl_load_elf_shared_library(secure,
  291. rpnt, strs + libent[i].liboffset)))
  292. return tpnt1;
  293. }
  294. }
  295. #endif
  296. /* Look for libraries wherever the shared library loader
  297. * was installed */
  298. #if defined (__SUPPORT_LD_DEBUG__)
  299. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching ldso dir='%s'\n", _dl_ldsopath);
  300. #endif
  301. if ((tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt)) != NULL)
  302. {
  303. return tpnt1;
  304. }
  305. /* Lastly, search the standard list of paths for the library.
  306. This list must exactly match the list in uClibc/ldso/util/ldd.c */
  307. #if defined (__SUPPORT_LD_DEBUG__)
  308. if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching full lib path list\n");
  309. #endif
  310. if ((tpnt1 = search_for_named_library(libname, secure,
  311. UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib:"
  312. UCLIBC_RUNTIME_PREFIX "usr/lib:"
  313. UCLIBC_RUNTIME_PREFIX "lib:"
  314. "/usr/lib:"
  315. "/lib", 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);
  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, PAGE_SIZE, PROT_READ | PROT_WRITE,
  390. MAP_PRIVATE | MAP_ANONYMOUS, 0, 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, PAGE_SIZE);
  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, PAGE_SIZE);
  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, PAGE_SIZE);
  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, PAGE_SIZE);
  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, PAGE_SIZE);
  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, PAGE_SIZE);
  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, PAGE_SIZE);
  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: %d\n\n",
  624. epnt->e_entry + libaddr, tpnt->ppnt, tpnt->n_phent);
  625. }
  626. #endif
  627. _dl_munmap(header, PAGE_SIZE);
  628. return tpnt;
  629. }
  630. /* Minimal printf which handles only %s, %d, and %x */
  631. void _dl_dprintf(int fd, const char *fmt, ...)
  632. {
  633. int num;
  634. va_list args;
  635. char *start, *ptr, *string;
  636. static char *buf;
  637. buf = _dl_mmap((void *) 0, PAGE_SIZE, PROT_READ | PROT_WRITE,
  638. MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  639. if (_dl_mmap_check_error(buf)) {
  640. _dl_write(fd, "mmap of a spare page failed!\n", 29);
  641. _dl_exit(20);
  642. }
  643. start = ptr = buf;
  644. if (!fmt)
  645. return;
  646. if (_dl_strlen(fmt) >= (PAGE_SIZE - 1)) {
  647. _dl_write(fd, "overflow\n", 11);
  648. _dl_exit(20);
  649. }
  650. _dl_strcpy(buf, fmt);
  651. va_start(args, fmt);
  652. while (start) {
  653. while (*ptr != '%' && *ptr) {
  654. ptr++;
  655. }
  656. if (*ptr == '%') {
  657. *ptr++ = '\0';
  658. _dl_write(fd, start, _dl_strlen(start));
  659. switch (*ptr++) {
  660. case 's':
  661. string = va_arg(args, char *);
  662. if (!string)
  663. _dl_write(fd, "(null)", 6);
  664. else
  665. _dl_write(fd, string, _dl_strlen(string));
  666. break;
  667. case 'i':
  668. case 'd':
  669. {
  670. char tmp[22];
  671. num = va_arg(args, int);
  672. string = _dl_simple_ltoa(tmp, num);
  673. _dl_write(fd, string, _dl_strlen(string));
  674. break;
  675. }
  676. case 'x':
  677. case 'X':
  678. {
  679. char tmp[22];
  680. num = va_arg(args, int);
  681. string = _dl_simple_ltoahex(tmp, num);
  682. _dl_write(fd, string, _dl_strlen(string));
  683. break;
  684. }
  685. default:
  686. _dl_write(fd, "(null)", 6);
  687. break;
  688. }
  689. start = ptr;
  690. } else {
  691. _dl_write(fd, start, _dl_strlen(start));
  692. start = NULL;
  693. }
  694. }
  695. _dl_munmap(buf, PAGE_SIZE);
  696. return;
  697. }
  698. char *_dl_strdup(const char *string)
  699. {
  700. char *retval;
  701. int len;
  702. len = _dl_strlen(string);
  703. retval = _dl_malloc(len + 1);
  704. _dl_strcpy(retval, string);
  705. return retval;
  706. }
  707. void *(*_dl_malloc_function) (size_t size) = NULL;
  708. void *_dl_malloc(int size)
  709. {
  710. void *retval;
  711. #if 0
  712. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  713. _dl_dprintf(2, "malloc: request for %d bytes\n", size);
  714. #endif
  715. #endif
  716. if (_dl_malloc_function)
  717. return (*_dl_malloc_function) (size);
  718. if (_dl_malloc_addr - _dl_mmap_zero + size > PAGE_SIZE) {
  719. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  720. _dl_dprintf(2, "malloc: mmapping more memory\n");
  721. #endif
  722. _dl_mmap_zero = _dl_malloc_addr = _dl_mmap((void *) 0, size,
  723. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  724. if (_dl_mmap_check_error(_dl_mmap_zero)) {
  725. _dl_dprintf(2, "%s: mmap of a spare page failed!\n", _dl_progname);
  726. _dl_exit(20);
  727. }
  728. }
  729. retval = _dl_malloc_addr;
  730. _dl_malloc_addr += size;
  731. /*
  732. * Align memory to 4 byte boundary. Some platforms require this, others
  733. * simply get better performance.
  734. */
  735. _dl_malloc_addr = (char *) (((unsigned long) _dl_malloc_addr + 3) & ~(3));
  736. return retval;
  737. }
  738. int _dl_fixup(struct dyn_elf *rpnt, int flag)
  739. {
  740. int goof = 0;
  741. struct elf_resolve *tpnt;
  742. if (rpnt->next)
  743. goof += _dl_fixup(rpnt->next, flag);
  744. tpnt = rpnt->dyn;
  745. #if defined (__SUPPORT_LD_DEBUG__)
  746. if(_dl_debug) _dl_dprintf(_dl_debug_file,"\nrelocation processing: %s", tpnt->libname);
  747. #endif
  748. if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
  749. #if defined (__SUPPORT_LD_DEBUG__)
  750. if(_dl_debug) {
  751. _dl_dprintf(2, "%s: can't handle %s relocation records\n",
  752. _dl_progname, UNSUPPORTED_RELOC_STR);
  753. }
  754. #endif
  755. goof++;
  756. return goof;
  757. }
  758. if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR]) {
  759. if (tpnt->init_flag & RELOCS_DONE)
  760. return goof;
  761. tpnt->init_flag |= RELOCS_DONE;
  762. goof += _dl_parse_relocation_information(rpnt,
  763. tpnt->dynamic_info[DT_RELOC_TABLE_ADDR],
  764. tpnt->dynamic_info[DT_RELOC_TABLE_SIZE], 0);
  765. }
  766. if (tpnt->dynamic_info[DT_JMPREL]) {
  767. if (tpnt->init_flag & JMP_RELOCS_DONE)
  768. return goof;
  769. tpnt->init_flag |= JMP_RELOCS_DONE;
  770. if (flag & RTLD_LAZY) {
  771. _dl_parse_lazy_relocation_information(rpnt,
  772. tpnt->dynamic_info[DT_JMPREL],
  773. tpnt->dynamic_info [DT_PLTRELSZ], 0);
  774. } else {
  775. goof += _dl_parse_relocation_information(rpnt,
  776. tpnt->dynamic_info[DT_JMPREL],
  777. tpnt->dynamic_info[DT_PLTRELSZ], 0);
  778. }
  779. }
  780. if (tpnt->init_flag & COPY_RELOCS_DONE)
  781. return goof;
  782. tpnt->init_flag |= COPY_RELOCS_DONE;
  783. goof += _dl_parse_copy_information(rpnt,
  784. tpnt->dynamic_info[DT_RELOC_TABLE_ADDR],
  785. tpnt->dynamic_info[DT_RELOC_TABLE_SIZE], 0);
  786. #if defined (__SUPPORT_LD_DEBUG__)
  787. if(_dl_debug) {
  788. _dl_dprintf(_dl_debug_file,"\nrelocation processing: %s", tpnt->libname);
  789. _dl_dprintf(_dl_debug_file,"; finished\n\n");
  790. }
  791. #endif
  792. return goof;
  793. }