dl-elf.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. 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;
  346. char header[4096];
  347. unsigned long dynamic_info[24];
  348. unsigned long *lpnt;
  349. unsigned long libaddr;
  350. unsigned long minvma = 0xffffffff, maxvma = 0;
  351. int i, flags, piclib, infile;
  352. /* If this file is already loaded, skip this step */
  353. tpnt = _dl_check_hashed_files(libname);
  354. if (tpnt) {
  355. if (*rpnt) {
  356. (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  357. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  358. (*rpnt)->next->prev = (*rpnt);
  359. *rpnt = (*rpnt)->next;
  360. (*rpnt)->dyn = tpnt;
  361. tpnt->symbol_scope = _dl_symbol_tables;
  362. }
  363. tpnt->usage_count++;
  364. tpnt->libtype = elf_lib;
  365. #if defined (__SUPPORT_LD_DEBUG__)
  366. if(_dl_debug) _dl_dprintf(2, "file='%s'; already loaded\n", libname);
  367. #endif
  368. return tpnt;
  369. }
  370. /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
  371. we don't load the library if it isn't setuid. */
  372. if (secure) {
  373. struct stat st;
  374. if (_dl_stat(libname, &st) || !(st.st_mode & S_ISUID))
  375. return NULL;
  376. }
  377. libaddr = 0;
  378. infile = _dl_open(libname, O_RDONLY);
  379. if (infile < 0) {
  380. #if 0
  381. /*
  382. * NO! When we open shared libraries we may search several paths.
  383. * it is inappropriate to generate an error here.
  384. */
  385. _dl_dprintf(2, "%s: can't open '%s'\n", _dl_progname, libname);
  386. #endif
  387. _dl_internal_error_number = LD_ERROR_NOFILE;
  388. return NULL;
  389. }
  390. _dl_read(infile, header, sizeof(header));
  391. epnt = (ElfW(Ehdr) *) (intptr_t) header;
  392. if (epnt->e_ident[0] != 0x7f ||
  393. epnt->e_ident[1] != 'E' ||
  394. epnt->e_ident[2] != 'L' ||
  395. epnt->e_ident[3] != 'F')
  396. {
  397. _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  398. libname);
  399. _dl_internal_error_number = LD_ERROR_NOTELF;
  400. _dl_close(infile);
  401. return NULL;
  402. };
  403. if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
  404. #ifdef MAGIC2
  405. && epnt->e_machine != MAGIC2
  406. #endif
  407. ))
  408. {
  409. _dl_internal_error_number =
  410. (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
  411. _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  412. "\n", _dl_progname, libname);
  413. _dl_close(infile);
  414. return NULL;
  415. };
  416. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  417. piclib = 1;
  418. for (i = 0; i < epnt->e_phnum; i++) {
  419. if (ppnt->p_type == PT_DYNAMIC) {
  420. if (dynamic_addr)
  421. _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
  422. _dl_progname, libname);
  423. dynamic_addr = ppnt->p_vaddr;
  424. dynamic_size = ppnt->p_filesz;
  425. };
  426. if (ppnt->p_type == PT_LOAD) {
  427. /* See if this is a PIC library. */
  428. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  429. piclib = 0;
  430. minvma = ppnt->p_vaddr;
  431. }
  432. if (piclib && ppnt->p_vaddr < minvma) {
  433. minvma = ppnt->p_vaddr;
  434. }
  435. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  436. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  437. }
  438. }
  439. ppnt++;
  440. };
  441. maxvma = (maxvma + ADDR_ALIGN) & ~ADDR_ALIGN;
  442. minvma = minvma & ~0xffffU;
  443. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  444. if (!piclib)
  445. flags |= MAP_FIXED;
  446. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  447. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  448. if (_dl_mmap_check_error(status)) {
  449. _dl_dprintf(2, "%s: can't map %s\n", _dl_progname, libname);
  450. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  451. _dl_close(infile);
  452. return NULL;
  453. };
  454. libaddr = (unsigned long) status;
  455. flags |= MAP_FIXED;
  456. /* Get the memory to store the library */
  457. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  458. for (i = 0; i < epnt->e_phnum; i++) {
  459. if (ppnt->p_type == PT_LOAD) {
  460. /* See if this is a PIC library. */
  461. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  462. piclib = 0;
  463. /* flags |= MAP_FIXED; */
  464. }
  465. if (ppnt->p_flags & PF_W) {
  466. unsigned long map_size;
  467. char *cpnt;
  468. status = (char *) _dl_mmap((char *) ((piclib ? libaddr : 0) +
  469. (ppnt->p_vaddr & PAGE_ALIGN)), (ppnt->p_vaddr & ADDR_ALIGN)
  470. + ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags, infile,
  471. ppnt->p_offset & OFFS_ALIGN);
  472. if (_dl_mmap_check_error(status)) {
  473. _dl_dprintf(2, "%s: can't map '%s'\n",
  474. _dl_progname, libname);
  475. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  476. _dl_munmap((char *) libaddr, maxvma - minvma);
  477. _dl_close(infile);
  478. return NULL;
  479. };
  480. /* Pad the last page with zeroes. */
  481. cpnt = (char *) (status + (ppnt->p_vaddr & ADDR_ALIGN) +
  482. ppnt->p_filesz);
  483. while (((unsigned long) cpnt) & ADDR_ALIGN)
  484. *cpnt++ = 0;
  485. /* I am not quite sure if this is completely
  486. * correct to do or not, but the basic way that
  487. * we handle bss segments is that we mmap
  488. * /dev/zero if there are any pages left over
  489. * that are not mapped as part of the file */
  490. map_size = (ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) & PAGE_ALIGN;
  491. if (map_size < ppnt->p_vaddr + ppnt->p_memsz)
  492. status = (char *) _dl_mmap((char *) map_size +
  493. (piclib ? libaddr : 0),
  494. ppnt->p_vaddr + ppnt->p_memsz - map_size,
  495. LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS, -1, 0);
  496. } else
  497. status = (char *) _dl_mmap((char *) (ppnt->p_vaddr & PAGE_ALIGN)
  498. + (piclib ? libaddr : 0), (ppnt->p_vaddr & ADDR_ALIGN) +
  499. ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags,
  500. infile, ppnt->p_offset & OFFS_ALIGN);
  501. if (_dl_mmap_check_error(status)) {
  502. _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
  503. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  504. _dl_munmap((char *) libaddr, maxvma - minvma);
  505. _dl_close(infile);
  506. return NULL;
  507. };
  508. /* if(libaddr == 0 && piclib) {
  509. libaddr = (unsigned long) status;
  510. flags |= MAP_FIXED;
  511. }; */
  512. };
  513. ppnt++;
  514. };
  515. _dl_close(infile);
  516. /* For a non-PIC library, the addresses are all absolute */
  517. if (piclib) {
  518. dynamic_addr += (unsigned long) libaddr;
  519. }
  520. /*
  521. * OK, the ELF library is now loaded into VM in the correct locations
  522. * The next step is to go through and do the dynamic linking (if needed).
  523. */
  524. /* Start by scanning the dynamic section to get all of the pointers */
  525. if (!dynamic_addr) {
  526. _dl_internal_error_number = LD_ERROR_NODYNAMIC;
  527. _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
  528. _dl_progname, libname);
  529. return NULL;
  530. }
  531. dpnt = (Elf32_Dyn *) dynamic_addr;
  532. dynamic_size = dynamic_size / sizeof(Elf32_Dyn);
  533. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  534. #if defined(__mips__)
  535. {
  536. int indx = 1;
  537. Elf32_Dyn *dpnt = (Elf32_Dyn *) dynamic_addr;
  538. while(dpnt->d_tag) {
  539. dpnt++;
  540. indx++;
  541. }
  542. dynamic_size = indx;
  543. }
  544. #endif
  545. {
  546. unsigned long indx;
  547. for (indx = 0; indx < dynamic_size; indx++)
  548. {
  549. if (dpnt->d_tag > DT_JMPREL) {
  550. dpnt++;
  551. continue;
  552. }
  553. dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  554. if (dpnt->d_tag == DT_TEXTREL)
  555. dynamic_info[DT_TEXTREL] = 1;
  556. dpnt++;
  557. };
  558. }
  559. /* If the TEXTREL is set, this means that we need to make the pages
  560. writable before we perform relocations. Do this now. They get set
  561. back again later. */
  562. if (dynamic_info[DT_TEXTREL]) {
  563. #ifndef FORCE_SHAREABLE_TEXT_SEGMENTS
  564. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  565. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  566. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  567. _dl_mprotect((void *) ((piclib ? libaddr : 0) +
  568. (ppnt->p_vaddr & PAGE_ALIGN)),
  569. (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
  570. PROT_READ | PROT_WRITE | PROT_EXEC);
  571. }
  572. #else
  573. _dl_dprintf(_dl_debug_file, "Can't modify %s's text section. Use GCC option -fPIC for shared objects, please.\n",libname);
  574. _dl_exit(1);
  575. #endif
  576. }
  577. tpnt = _dl_add_elf_hash_table(libname, (char *) libaddr, dynamic_info,
  578. dynamic_addr, dynamic_size);
  579. tpnt->ppnt = (ElfW(Phdr) *)(intptr_t) (tpnt->loadaddr + epnt->e_phoff);
  580. tpnt->n_phent = epnt->e_phnum;
  581. /*
  582. * Add this object into the symbol chain
  583. */
  584. if (*rpnt) {
  585. (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  586. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  587. (*rpnt)->next->prev = (*rpnt);
  588. *rpnt = (*rpnt)->next;
  589. (*rpnt)->dyn = tpnt;
  590. tpnt->symbol_scope = _dl_symbol_tables;
  591. }
  592. tpnt->usage_count++;
  593. tpnt->libtype = elf_lib;
  594. /*
  595. * OK, the next thing we need to do is to insert the dynamic linker into
  596. * the proper entry in the GOT so that the PLT symbols can be properly
  597. * resolved.
  598. */
  599. lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
  600. if (lpnt) {
  601. lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT] +
  602. ((int) libaddr));
  603. INIT_GOT(lpnt, tpnt);
  604. };
  605. #if defined (__SUPPORT_LD_DEBUG__)
  606. if(_dl_debug) {
  607. _dl_dprintf(2, "\n\tfile='%s'; generating link map\n", libname);
  608. _dl_dprintf(2, "\t\tdynamic: %x base: %x size: %x\n",
  609. dynamic_addr, libaddr, dynamic_size);
  610. _dl_dprintf(2, "\t\t entry: %x phdr: %x phnum: %d\n\n",
  611. epnt->e_entry + libaddr, tpnt->ppnt, tpnt->n_phent);
  612. }
  613. #endif
  614. return tpnt;
  615. }
  616. /* Ugly, ugly. Some versions of the SVr4 linker fail to generate COPY
  617. relocations for global variables that are present both in the image and
  618. the shared library. Go through and do it manually. If the images
  619. are guaranteed to be generated by a trustworthy linker, then this
  620. step can be skipped. */
  621. int _dl_copy_fixups(struct dyn_elf *rpnt)
  622. {
  623. int goof = 0;
  624. struct elf_resolve *tpnt;
  625. if (rpnt->next)
  626. goof += _dl_copy_fixups(rpnt->next);
  627. else
  628. return 0;
  629. tpnt = rpnt->dyn;
  630. if (tpnt->init_flag & COPY_RELOCS_DONE)
  631. return goof;
  632. tpnt->init_flag |= COPY_RELOCS_DONE;
  633. #if defined (__SUPPORT_LD_DEBUG__)
  634. if(_dl_debug) _dl_dprintf(_dl_debug_file,"\nrelocation copy fixups: %s", tpnt->libname);
  635. #endif
  636. #ifdef ELF_USES_RELOCA
  637. goof += _dl_parse_copy_information(rpnt,
  638. tpnt->dynamic_info[DT_RELA], tpnt->dynamic_info[DT_RELASZ], 0);
  639. #else
  640. goof += _dl_parse_copy_information(rpnt, tpnt->dynamic_info[DT_REL],
  641. tpnt->dynamic_info[DT_RELSZ], 0);
  642. #endif
  643. #if defined (__SUPPORT_LD_DEBUG__)
  644. if(_dl_debug) _dl_dprintf(_dl_debug_file,"\nrelocation copy fixups: %s; finished\n\n", tpnt->libname);
  645. #endif
  646. return goof;
  647. }
  648. /* Minimal printf which handles only %s, %d, and %x */
  649. void _dl_dprintf(int fd, const char *fmt, ...)
  650. {
  651. int num;
  652. va_list args;
  653. char *start, *ptr, *string;
  654. char buf[2048];
  655. start = ptr = buf;
  656. if (!fmt)
  657. return;
  658. if (_dl_strlen(fmt) >= (sizeof(buf) - 1))
  659. _dl_write(fd, "(overflow)\n", 10);
  660. _dl_strcpy(buf, fmt);
  661. va_start(args, fmt);
  662. while (start) {
  663. while (*ptr != '%' && *ptr) {
  664. ptr++;
  665. }
  666. if (*ptr == '%') {
  667. *ptr++ = '\0';
  668. _dl_write(fd, start, _dl_strlen(start));
  669. switch (*ptr++) {
  670. case 's':
  671. string = va_arg(args, char *);
  672. if (!string)
  673. _dl_write(fd, "(null)", 6);
  674. else
  675. _dl_write(fd, string, _dl_strlen(string));
  676. break;
  677. case 'i':
  678. case 'd':
  679. {
  680. char tmp[22];
  681. num = va_arg(args, int);
  682. string = _dl_simple_ltoa(tmp, num);
  683. _dl_write(fd, string, _dl_strlen(string));
  684. break;
  685. }
  686. case 'x':
  687. case 'X':
  688. {
  689. char tmp[22];
  690. num = va_arg(args, int);
  691. string = _dl_simple_ltoahex(tmp, num);
  692. _dl_write(fd, string, _dl_strlen(string));
  693. break;
  694. }
  695. default:
  696. _dl_write(fd, "(null)", 6);
  697. break;
  698. }
  699. start = ptr;
  700. } else {
  701. _dl_write(fd, start, _dl_strlen(start));
  702. start = NULL;
  703. }
  704. }
  705. return;
  706. }
  707. char *_dl_strdup(const char *string)
  708. {
  709. char *retval;
  710. int len;
  711. len = _dl_strlen(string);
  712. retval = _dl_malloc(len + 1);
  713. _dl_strcpy(retval, string);
  714. return retval;
  715. }
  716. void *(*_dl_malloc_function) (size_t size) = NULL;
  717. void *_dl_malloc(int size)
  718. {
  719. void *retval;
  720. #if 0
  721. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  722. _dl_dprintf(_dl_debug_file, "malloc: request for %d bytes\n", size);
  723. #endif
  724. #endif
  725. if (_dl_malloc_function)
  726. return (*_dl_malloc_function) (size);
  727. if (_dl_malloc_addr - _dl_mmap_zero + size > 4096) {
  728. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  729. _dl_dprintf(_dl_debug_file, "malloc: mmapping more memory\n");
  730. #endif
  731. _dl_mmap_zero = _dl_malloc_addr = _dl_mmap((void *) 0, size,
  732. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  733. if (_dl_mmap_check_error(_dl_mmap_zero)) {
  734. _dl_dprintf(2, "%s: mmap of a spare page failed!\n", _dl_progname);
  735. _dl_exit(20);
  736. }
  737. }
  738. retval = _dl_malloc_addr;
  739. _dl_malloc_addr += size;
  740. /*
  741. * Align memory to 4 byte boundary. Some platforms require this, others
  742. * simply get better performance.
  743. */
  744. _dl_malloc_addr = (char *) (((unsigned long) _dl_malloc_addr + 3) & ~(3));
  745. return retval;
  746. }