dl-elf.c 23 KB

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