dl-elf.c 25 KB

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