dl-elf.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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-2006 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:%i: can't map '%s'\n",
  56. _dl_progname, __LINE__, 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) base = (ElfW(Addr)) DL_RELOC_ADDR (l->relro_addr,
  103. l->loadaddr);
  104. ElfW(Addr) start = (base & ~(_dl_pagesize - 1));
  105. ElfW(Addr) end = ((base + l->relro_size) & ~(_dl_pagesize - 1));
  106. _dl_if_debug_dprint("RELRO protecting %s: start:%x, end:%x\n", l->libname, start, end);
  107. if (start != end &&
  108. _dl_mprotect ((void *) start, end - start, PROT_READ) < 0) {
  109. _dl_dprintf(2, "%s: cannot apply additional memory protection after relocation", l->libname);
  110. _dl_exit(0);
  111. }
  112. }
  113. /* This function's behavior must exactly match that
  114. * in uClibc/ldso/util/ldd.c */
  115. static struct elf_resolve *
  116. search_for_named_library(const char *name, int secure, const char *path_list,
  117. struct dyn_elf **rpnt)
  118. {
  119. char *path, *path_n, *mylibname;
  120. struct elf_resolve *tpnt;
  121. int done;
  122. if (path_list==NULL)
  123. return NULL;
  124. /* We need a writable copy of this string, but we don't
  125. * need this allocated permanently since we don't want
  126. * to leak memory, so use alloca to put path on the stack */
  127. done = _dl_strlen(path_list);
  128. path = alloca(done + 1);
  129. /* another bit of local storage */
  130. mylibname = alloca(2050);
  131. /* gcc inlines alloca using a single instruction adjusting
  132. * the stack pointer and no stack overflow check and thus
  133. * no NULL error return. No point leaving in dead code... */
  134. #if 0
  135. if (!path || !mylibname) {
  136. _dl_dprintf(2, "Out of memory!\n");
  137. _dl_exit(0);
  138. }
  139. #endif
  140. _dl_memcpy(path, path_list, done+1);
  141. /* Unlike ldd.c, don't bother to eliminate double //s */
  142. /* Replace colons with zeros in path_list */
  143. /* : at the beginning or end of path maps to CWD */
  144. /* :: anywhere maps CWD */
  145. /* "" maps to CWD */
  146. done = 0;
  147. path_n = path;
  148. do {
  149. if (*path == 0) {
  150. *path = ':';
  151. done = 1;
  152. }
  153. if (*path == ':') {
  154. *path = 0;
  155. if (*path_n)
  156. _dl_strcpy(mylibname, path_n);
  157. else
  158. _dl_strcpy(mylibname, "."); /* Assume current dir if empty path */
  159. _dl_strcat(mylibname, "/");
  160. _dl_strcat(mylibname, name);
  161. if ((tpnt = _dl_load_elf_shared_library(secure, rpnt, mylibname)) != NULL)
  162. return tpnt;
  163. path_n = path+1;
  164. }
  165. path++;
  166. } while (!done);
  167. return NULL;
  168. }
  169. /* Used to return error codes back to dlopen et. al. */
  170. unsigned long _dl_error_number;
  171. unsigned long _dl_internal_error_number;
  172. struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
  173. struct elf_resolve *tpnt, char *full_libname, int __attribute__((unused)) trace_loaded_objects)
  174. {
  175. char *pnt;
  176. struct elf_resolve *tpnt1;
  177. char *libname;
  178. _dl_internal_error_number = 0;
  179. libname = full_libname;
  180. /* quick hack to ensure mylibname buffer doesn't overflow. don't
  181. allow full_libname or any directory to be longer than 1024. */
  182. if (_dl_strlen(full_libname) > 1024)
  183. goto goof;
  184. /* Skip over any initial initial './' and '/' stuff to
  185. * get the short form libname with no path garbage */
  186. pnt = _dl_strrchr(libname, '/');
  187. if (pnt) {
  188. libname = pnt + 1;
  189. }
  190. _dl_if_debug_dprint("\tfind library='%s'; searching\n", libname);
  191. /* If the filename has any '/', try it straight and leave it at that.
  192. For IBCS2 compatibility under linux, we substitute the string
  193. /usr/i486-sysv4/lib for /usr/lib in library names. */
  194. if (libname != full_libname) {
  195. _dl_if_debug_dprint("\ttrying file='%s'\n", full_libname);
  196. tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
  197. if (tpnt1) {
  198. return tpnt1;
  199. }
  200. }
  201. /*
  202. * The ABI specifies that RPATH is searched before LD_LIBRARY_PATH or
  203. * the default path of /usr/lib. Check in rpath directories.
  204. */
  205. #ifdef __LDSO_RUNPATH__
  206. pnt = (tpnt ? (char *) tpnt->dynamic_info[DT_RPATH] : NULL);
  207. if (pnt) {
  208. pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
  209. _dl_if_debug_dprint("\tsearching RPATH='%s'\n", pnt);
  210. if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
  211. return tpnt1;
  212. }
  213. #endif
  214. /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
  215. if (_dl_library_path) {
  216. _dl_if_debug_dprint("\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
  217. if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
  218. {
  219. return tpnt1;
  220. }
  221. }
  222. /*
  223. * The ABI specifies that RUNPATH is searched after LD_LIBRARY_PATH.
  224. */
  225. #ifdef __LDSO_RUNPATH__
  226. pnt = (tpnt ? (char *)tpnt->dynamic_info[DT_RUNPATH] : NULL);
  227. if (pnt) {
  228. pnt += (intptr_t) DL_RELOC_ADDR (tpnt->dynamic_info[DT_STRTAB],
  229. tpnt->loadaddr);
  230. _dl_if_debug_dprint("\tsearching RUNPATH='%s'\n", pnt);
  231. if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
  232. return tpnt1;
  233. }
  234. #endif
  235. /*
  236. * Where should the cache be searched? There is no such concept in the
  237. * ABI, so we have some flexibility here. For now, search it before
  238. * the hard coded paths that follow (i.e before /lib and /usr/lib).
  239. */
  240. #ifdef __LDSO_CACHE_SUPPORT__
  241. if (_dl_cache_addr != NULL && _dl_cache_addr != (caddr_t) - 1) {
  242. int i;
  243. header_t *header = (header_t *) _dl_cache_addr;
  244. libentry_t *libent = (libentry_t *) & header[1];
  245. char *strs = (char *) &libent[header->nlibs];
  246. _dl_if_debug_dprint("\tsearching cache='%s'\n", LDSO_CACHE);
  247. for (i = 0; i < header->nlibs; i++) {
  248. if ((libent[i].flags == LIB_ELF ||
  249. libent[i].flags == LIB_ELF_LIBC0 ||
  250. libent[i].flags == LIB_ELF_LIBC5) &&
  251. _dl_strcmp(libname, strs + libent[i].sooffset) == 0 &&
  252. (tpnt1 = _dl_load_elf_shared_library(secure,
  253. rpnt, strs + libent[i].liboffset)))
  254. return tpnt1;
  255. }
  256. }
  257. #endif
  258. /* Look for libraries wherever the shared library loader
  259. * was installed */
  260. _dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath);
  261. if ((tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt)) != NULL)
  262. {
  263. return tpnt1;
  264. }
  265. /* Lastly, search the standard list of paths for the library.
  266. This list must exactly match the list in uClibc/ldso/util/ldd.c */
  267. _dl_if_debug_dprint("\tsearching full lib path list\n");
  268. if ((tpnt1 = search_for_named_library(libname, secure,
  269. UCLIBC_RUNTIME_PREFIX "lib:"
  270. UCLIBC_RUNTIME_PREFIX "usr/lib"
  271. #ifndef __LDSO_CACHE_SUPPORT__
  272. ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
  273. #endif
  274. , rpnt)
  275. ) != NULL)
  276. {
  277. return tpnt1;
  278. }
  279. goof:
  280. /* Well, we shot our wad on that one. All we can do now is punt */
  281. if (_dl_internal_error_number)
  282. _dl_error_number = _dl_internal_error_number;
  283. else
  284. _dl_error_number = LD_ERROR_NOFILE;
  285. _dl_if_debug_dprint("Bummer: could not find '%s'!\n", libname);
  286. return NULL;
  287. }
  288. /*
  289. * Read one ELF library into memory, mmap it into the correct locations and
  290. * add the symbol info to the symbol chain. Perform any relocations that
  291. * are required.
  292. */
  293. struct elf_resolve *_dl_load_elf_shared_library(int secure,
  294. struct dyn_elf **rpnt, char *libname)
  295. {
  296. ElfW(Ehdr) *epnt;
  297. unsigned long dynamic_addr = 0;
  298. ElfW(Dyn) *dpnt;
  299. struct elf_resolve *tpnt;
  300. ElfW(Phdr) *ppnt;
  301. char *status, *header;
  302. unsigned long dynamic_info[DYNAMIC_SIZE];
  303. unsigned long *lpnt;
  304. unsigned long libaddr;
  305. unsigned long minvma = 0xffffffff, maxvma = 0;
  306. int i, flags, piclib, infile;
  307. ElfW(Addr) relro_addr = 0;
  308. size_t relro_size = 0;
  309. struct stat st;
  310. DL_LOADADDR_TYPE lib_loadaddr;
  311. DL_INIT_LOADADDR_EXTRA_DECLS
  312. libaddr = 0;
  313. infile = _dl_open(libname, O_RDONLY, 0);
  314. if (infile < 0) {
  315. _dl_internal_error_number = LD_ERROR_NOFILE;
  316. return NULL;
  317. }
  318. if (_dl_fstat(infile, &st) < 0) {
  319. _dl_internal_error_number = LD_ERROR_NOFILE;
  320. _dl_close(infile);
  321. return NULL;
  322. }
  323. /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
  324. we don't load the library if it isn't setuid. */
  325. if (secure)
  326. if (!(st.st_mode & S_ISUID)) {
  327. _dl_close(infile);
  328. return NULL;
  329. }
  330. /* Check if file is already loaded */
  331. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  332. if (tpnt->st_dev == st.st_dev && tpnt->st_ino == st.st_ino) {
  333. /* Already loaded */
  334. tpnt->usage_count++;
  335. _dl_close(infile);
  336. return tpnt;
  337. }
  338. }
  339. header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  340. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  341. if (_dl_mmap_check_error(header)) {
  342. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  343. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  344. _dl_close(infile);
  345. return NULL;
  346. }
  347. _dl_read(infile, header, _dl_pagesize);
  348. epnt = (ElfW(Ehdr) *) (intptr_t) header;
  349. if (epnt->e_ident[0] != 0x7f ||
  350. epnt->e_ident[1] != 'E' ||
  351. epnt->e_ident[2] != 'L' ||
  352. epnt->e_ident[3] != 'F')
  353. {
  354. _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  355. libname);
  356. _dl_internal_error_number = LD_ERROR_NOTELF;
  357. _dl_close(infile);
  358. _dl_munmap(header, _dl_pagesize);
  359. return NULL;
  360. }
  361. if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
  362. #ifdef MAGIC2
  363. && epnt->e_machine != MAGIC2
  364. #endif
  365. ))
  366. {
  367. _dl_internal_error_number =
  368. (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
  369. _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  370. "\n", _dl_progname, libname);
  371. _dl_close(infile);
  372. _dl_munmap(header, _dl_pagesize);
  373. return NULL;
  374. }
  375. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  376. piclib = 1;
  377. for (i = 0; i < epnt->e_phnum; i++) {
  378. if (ppnt->p_type == PT_DYNAMIC) {
  379. if (dynamic_addr)
  380. _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
  381. _dl_progname, libname);
  382. dynamic_addr = ppnt->p_vaddr;
  383. }
  384. if (ppnt->p_type == PT_LOAD) {
  385. /* See if this is a PIC library. */
  386. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  387. piclib = 0;
  388. minvma = ppnt->p_vaddr;
  389. }
  390. if (piclib && ppnt->p_vaddr < minvma) {
  391. minvma = ppnt->p_vaddr;
  392. }
  393. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  394. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  395. }
  396. }
  397. ppnt++;
  398. }
  399. maxvma = (maxvma + ADDR_ALIGN) & ~ADDR_ALIGN;
  400. minvma = minvma & ~0xffffU;
  401. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  402. if (!piclib)
  403. flags |= MAP_FIXED;
  404. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  405. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  406. if (_dl_mmap_check_error(status)) {
  407. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  408. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  409. _dl_close(infile);
  410. _dl_munmap(header, _dl_pagesize);
  411. return NULL;
  412. }
  413. libaddr = (unsigned long) status;
  414. flags |= MAP_FIXED;
  415. DL_INIT_LOADADDR (lib_loadaddr, libaddr,
  416. (Elf32_Phdr *)& header[epnt->e_phoff],
  417. epnt->e_phnum);
  418. /* Get the memory to store the library */
  419. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  420. for (i = 0; i < epnt->e_phnum; i++) {
  421. if (ppnt->p_type == PT_GNU_RELRO) {
  422. relro_addr = ppnt->p_vaddr;
  423. relro_size = ppnt->p_memsz;
  424. }
  425. if (ppnt->p_type == PT_LOAD) {
  426. /* See if this is a PIC library. */
  427. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  428. piclib = 0;
  429. /* flags |= MAP_FIXED; */
  430. }
  431. if (ppnt->p_flags & PF_W) {
  432. unsigned long map_size;
  433. char *cpnt;
  434. status = (char *) _dl_mmap((char *) ((piclib ? libaddr : 0) +
  435. (ppnt->p_vaddr & PAGE_ALIGN)), (ppnt->p_vaddr & ADDR_ALIGN)
  436. + ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags, infile,
  437. ppnt->p_offset & OFFS_ALIGN);
  438. if (_dl_mmap_check_error(status)) {
  439. _dl_dprintf(2, "%s:%i: can't map '%s'\n",
  440. _dl_progname, __LINE__, libname);
  441. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  442. _dl_munmap((char *) libaddr, maxvma - minvma);
  443. _dl_close(infile);
  444. _dl_munmap(header, _dl_pagesize);
  445. return NULL;
  446. }
  447. /* Pad the last page with zeroes. */
  448. cpnt = (char *) (status + (ppnt->p_vaddr & ADDR_ALIGN) +
  449. ppnt->p_filesz);
  450. while (((unsigned long) cpnt) & ADDR_ALIGN)
  451. *cpnt++ = 0;
  452. /* I am not quite sure if this is completely
  453. * correct to do or not, but the basic way that
  454. * we handle bss segments is that we mmap
  455. * /dev/zero if there are any pages left over
  456. * that are not mapped as part of the file */
  457. map_size = (ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) & PAGE_ALIGN;
  458. if (map_size < ppnt->p_vaddr + ppnt->p_memsz)
  459. status = (char *) _dl_mmap((char *) map_size +
  460. (piclib ? libaddr : 0),
  461. ppnt->p_vaddr + ppnt->p_memsz - map_size,
  462. LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS, -1, 0);
  463. } else
  464. status = (char *) _dl_mmap((char *) (ppnt->p_vaddr & PAGE_ALIGN)
  465. + (piclib ? libaddr : 0), (ppnt->p_vaddr & ADDR_ALIGN) +
  466. ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags,
  467. infile, ppnt->p_offset & OFFS_ALIGN);
  468. if (_dl_mmap_check_error(status)) {
  469. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  470. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  471. _dl_munmap((char *) libaddr, maxvma - minvma);
  472. _dl_close(infile);
  473. _dl_munmap(header, _dl_pagesize);
  474. return NULL;
  475. }
  476. /* if (libaddr == 0 && piclib) {
  477. libaddr = (unsigned long) status;
  478. flags |= MAP_FIXED;
  479. } */
  480. }
  481. ppnt++;
  482. }
  483. _dl_close(infile);
  484. /* For a non-PIC library, the addresses are all absolute */
  485. if (piclib) {
  486. dynamic_addr = (unsigned long) DL_RELOC_ADDR (dynamic_addr,
  487. lib_loadaddr);
  488. }
  489. /*
  490. * OK, the ELF library is now loaded into VM in the correct locations
  491. * The next step is to go through and do the dynamic linking (if needed).
  492. */
  493. /* Start by scanning the dynamic section to get all of the pointers */
  494. if (!dynamic_addr) {
  495. _dl_internal_error_number = LD_ERROR_NODYNAMIC;
  496. _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
  497. _dl_progname, libname);
  498. _dl_munmap(header, _dl_pagesize);
  499. return NULL;
  500. }
  501. dpnt = (ElfW(Dyn) *) dynamic_addr;
  502. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  503. _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
  504. /* If the TEXTREL is set, this means that we need to make the pages
  505. writable before we perform relocations. Do this now. They get set
  506. back again later. */
  507. if (dynamic_info[DT_TEXTREL]) {
  508. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  509. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  510. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  511. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  512. _dl_mprotect((void *) ((piclib ? libaddr : 0) +
  513. (ppnt->p_vaddr & PAGE_ALIGN)),
  514. (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
  515. PROT_READ | PROT_WRITE | PROT_EXEC);
  516. }
  517. #else
  518. _dl_dprintf(_dl_debug_file, "Can't modify %s's text section. Use GCC option -fPIC for shared objects, please.\n",libname);
  519. _dl_exit(1);
  520. #endif
  521. }
  522. tpnt = _dl_add_elf_hash_table(libname, lib_loadaddr, dynamic_info,
  523. dynamic_addr, 0);
  524. tpnt->relro_addr = relro_addr;
  525. tpnt->relro_size = relro_size;
  526. tpnt->st_dev = st.st_dev;
  527. tpnt->st_ino = st.st_ino;
  528. tpnt->ppnt = (ElfW(Phdr) *) DL_RELOC_ADDR (epnt->e_phoff,
  529. tpnt->loadaddr);
  530. tpnt->n_phent = epnt->e_phnum;
  531. /*
  532. * Add this object into the symbol chain
  533. */
  534. if (*rpnt) {
  535. (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  536. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  537. (*rpnt)->next->prev = (*rpnt);
  538. *rpnt = (*rpnt)->next;
  539. (*rpnt)->dyn = tpnt;
  540. tpnt->symbol_scope = _dl_symbol_tables;
  541. }
  542. tpnt->usage_count++;
  543. tpnt->libtype = elf_lib;
  544. /*
  545. * OK, the next thing we need to do is to insert the dynamic linker into
  546. * the proper entry in the GOT so that the PLT symbols can be properly
  547. * resolved.
  548. */
  549. lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
  550. if (lpnt) {
  551. lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
  552. INIT_GOT(lpnt, tpnt);
  553. }
  554. _dl_if_debug_dprint("\n\tfile='%s'; generating link map\n", libname);
  555. _dl_if_debug_dprint("\t\tdynamic: %x base: %x\n", dynamic_addr,
  556. DL_LOADADDR_BASE (libaddr));
  557. _dl_if_debug_dprint("\t\t entry: %x phdr: %x phnum: %x\n\n",
  558. DL_RELOC_ADDR (epnt->e_entry, lib_loadaddr),
  559. tpnt->ppnt, tpnt->n_phent);
  560. _dl_munmap(header, _dl_pagesize);
  561. return tpnt;
  562. }
  563. /* now_flag must be RTLD_NOW or zero */
  564. int _dl_fixup(struct dyn_elf *rpnt, int now_flag)
  565. {
  566. int goof = 0;
  567. struct elf_resolve *tpnt;
  568. ElfW(Word) reloc_size, relative_count;
  569. ElfW(Addr) reloc_addr;
  570. if (rpnt->next)
  571. goof = _dl_fixup(rpnt->next, now_flag);
  572. if (goof)
  573. return goof;
  574. tpnt = rpnt->dyn;
  575. if (!(tpnt->init_flag & RELOCS_DONE))
  576. _dl_if_debug_dprint("relocation processing: %s\n", tpnt->libname);
  577. if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
  578. _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
  579. _dl_progname, UNSUPPORTED_RELOC_STR);
  580. goof++;
  581. return goof;
  582. }
  583. reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
  584. /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
  585. range. Note that according to the ELF spec, this is completely legal! */
  586. #ifdef ELF_MACHINE_PLTREL_OVERLAP
  587. reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
  588. #endif
  589. if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
  590. !(tpnt->init_flag & RELOCS_DONE)) {
  591. reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
  592. relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
  593. if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
  594. reloc_size -= relative_count * sizeof(ELF_RELOC);
  595. elf_machine_relative(tpnt->loadaddr, reloc_addr, relative_count);
  596. reloc_addr += relative_count * sizeof(ELF_RELOC);
  597. }
  598. goof += _dl_parse_relocation_information(rpnt,
  599. reloc_addr,
  600. reloc_size);
  601. tpnt->init_flag |= RELOCS_DONE;
  602. }
  603. if (tpnt->dynamic_info[DT_BIND_NOW])
  604. now_flag = RTLD_NOW;
  605. if (tpnt->dynamic_info[DT_JMPREL] &&
  606. (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
  607. (now_flag && !(tpnt->rtld_flags & now_flag)))) {
  608. tpnt->rtld_flags |= now_flag;
  609. if (!(tpnt->rtld_flags & RTLD_NOW)) {
  610. _dl_parse_lazy_relocation_information(rpnt,
  611. tpnt->dynamic_info[DT_JMPREL],
  612. tpnt->dynamic_info [DT_PLTRELSZ]);
  613. } else {
  614. goof += _dl_parse_relocation_information(rpnt,
  615. tpnt->dynamic_info[DT_JMPREL],
  616. tpnt->dynamic_info[DT_PLTRELSZ]);
  617. }
  618. tpnt->init_flag |= JMP_RELOCS_DONE;
  619. }
  620. return goof;
  621. }
  622. /* Minimal printf which handles only %s, %d, and %x */
  623. void _dl_dprintf(int fd, const char *fmt, ...)
  624. {
  625. #if __WORDSIZE > 32
  626. long int num;
  627. #else
  628. int num;
  629. #endif
  630. va_list args;
  631. char *start, *ptr, *string;
  632. static char *buf;
  633. if (!fmt)
  634. return;
  635. buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  636. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  637. if (_dl_mmap_check_error(buf)) {
  638. _dl_write(fd, "mmap of a spare page failed!\n", 29);
  639. _dl_exit(20);
  640. }
  641. start = ptr = buf;
  642. if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
  643. _dl_write(fd, "overflow\n", 11);
  644. _dl_exit(20);
  645. }
  646. _dl_strcpy(buf, fmt);
  647. va_start(args, fmt);
  648. while (start) {
  649. while (*ptr != '%' && *ptr) {
  650. ptr++;
  651. }
  652. if (*ptr == '%') {
  653. *ptr++ = '\0';
  654. _dl_write(fd, start, _dl_strlen(start));
  655. switch (*ptr++) {
  656. case 's':
  657. string = va_arg(args, char *);
  658. if (!string)
  659. _dl_write(fd, "(null)", 6);
  660. else
  661. _dl_write(fd, string, _dl_strlen(string));
  662. break;
  663. case 'i':
  664. case 'd':
  665. {
  666. char tmp[22];
  667. #if __WORDSIZE > 32
  668. num = va_arg(args, long int);
  669. #else
  670. num = va_arg(args, int);
  671. #endif
  672. string = _dl_simple_ltoa(tmp, num);
  673. _dl_write(fd, string, _dl_strlen(string));
  674. break;
  675. }
  676. case 'x':
  677. case 'X':
  678. {
  679. char tmp[22];
  680. #if __WORDSIZE > 32
  681. num = va_arg(args, long int);
  682. #else
  683. num = va_arg(args, int);
  684. #endif
  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. _dl_munmap(buf, _dl_pagesize);
  700. return;
  701. }
  702. char *_dl_strdup(const char *string)
  703. {
  704. char *retval;
  705. int len;
  706. len = _dl_strlen(string);
  707. retval = _dl_malloc(len + 1);
  708. _dl_strcpy(retval, string);
  709. return retval;
  710. }
  711. void _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[], void *debug_addr, DL_LOADADDR_TYPE load_off)
  712. {
  713. __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);
  714. }
  715. /* we want this in ldso.so and libdl.a but nowhere else */
  716. #ifdef __USE_GNU
  717. #if defined IS_IN_rtld || (defined IS_IN_libdl && ! defined SHARED)
  718. extern __typeof(dl_iterate_phdr) __dl_iterate_phdr;
  719. int
  720. __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info, size_t size, void *data), void *data)
  721. {
  722. struct elf_resolve *l;
  723. struct dl_phdr_info info;
  724. int ret = 0;
  725. for (l = _dl_loaded_modules; l != NULL; l = l->next) {
  726. info.dlpi_addr = (ElfW(Addr))DL_LOADADDR_BASE (l->loadaddr);
  727. info.dlpi_name = l->libname;
  728. info.dlpi_phdr = l->ppnt;
  729. info.dlpi_phnum = l->n_phent;
  730. ret = callback (&info, sizeof (struct dl_phdr_info), data);
  731. if (ret)
  732. break;
  733. }
  734. return ret;
  735. }
  736. strong_alias(__dl_iterate_phdr, dl_iterate_phdr)
  737. #endif
  738. #endif