dl-elf.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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 == MAP_FAILED)
  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|O_CLOEXEC, 0)) < 0) {
  48. _dl_cache_addr = MAP_FAILED; /* so we won't try again */
  49. return -1;
  50. }
  51. _dl_cache_size = st.st_size;
  52. _dl_cache_addr = _dl_mmap(0, _dl_cache_size, PROT_READ, LDSO_CACHE_MMAP_FLAGS, 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 = MAP_FAILED;
  86. return -1;
  87. }
  88. int _dl_unmap_cache(void)
  89. {
  90. if (_dl_cache_addr == NULL || _dl_cache_addr == MAP_FAILED)
  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->loadaddr, l->relro_addr);
  103. ElfW(Addr) start = (base & PAGE_ALIGN);
  104. ElfW(Addr) end = ((base + l->relro_size) & PAGE_ALIGN);
  105. _dl_if_debug_dprint("RELRO protecting %s: start:%x, end:%x\n", l->libname, start, end);
  106. if (start != end &&
  107. _dl_mprotect ((void *) start, end - start, PROT_READ) < 0) {
  108. _dl_dprintf(2, "%s: cannot apply additional memory protection after relocation", l->libname);
  109. _dl_exit(0);
  110. }
  111. }
  112. /* This function's behavior must exactly match that
  113. * in uClibc/ldso/util/ldd.c */
  114. static struct elf_resolve *
  115. search_for_named_library(const char *name, int secure, const char *path_list,
  116. struct dyn_elf **rpnt)
  117. {
  118. char *path, *path_n, *mylibname;
  119. struct elf_resolve *tpnt;
  120. int done;
  121. if (path_list==NULL)
  122. return NULL;
  123. /* We need a writable copy of this string, but we don't
  124. * need this allocated permanently since we don't want
  125. * to leak memory, so use alloca to put path on the stack */
  126. done = _dl_strlen(path_list);
  127. path = alloca(done + 1);
  128. /* another bit of local storage */
  129. mylibname = alloca(2050);
  130. _dl_memcpy(path, path_list, done+1);
  131. /* Unlike ldd.c, don't bother to eliminate double //s */
  132. /* Replace colons with zeros in path_list */
  133. /* : at the beginning or end of path maps to CWD */
  134. /* :: anywhere maps CWD */
  135. /* "" maps to CWD */
  136. done = 0;
  137. path_n = path;
  138. do {
  139. if (*path == 0) {
  140. *path = ':';
  141. done = 1;
  142. }
  143. if (*path == ':') {
  144. *path = 0;
  145. if (*path_n)
  146. _dl_strcpy(mylibname, path_n);
  147. else
  148. _dl_strcpy(mylibname, "."); /* Assume current dir if empty path */
  149. _dl_strcat(mylibname, "/");
  150. _dl_strcat(mylibname, name);
  151. if ((tpnt = _dl_load_elf_shared_library(secure, rpnt, mylibname)) != NULL)
  152. return tpnt;
  153. path_n = path+1;
  154. }
  155. path++;
  156. } while (!done);
  157. return NULL;
  158. }
  159. /* Used to return error codes back to dlopen et. al. */
  160. unsigned long _dl_error_number;
  161. unsigned long _dl_internal_error_number;
  162. struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
  163. struct elf_resolve *tpnt, char *full_libname, int __attribute__((unused)) trace_loaded_objects)
  164. {
  165. char *pnt;
  166. struct elf_resolve *tpnt1;
  167. char *libname;
  168. _dl_internal_error_number = 0;
  169. libname = full_libname;
  170. /* quick hack to ensure mylibname buffer doesn't overflow. don't
  171. allow full_libname or any directory to be longer than 1024. */
  172. if (_dl_strlen(full_libname) > 1024)
  173. goto goof;
  174. /* Skip over any initial initial './' and '/' stuff to
  175. * get the short form libname with no path garbage */
  176. pnt = _dl_strrchr(libname, '/');
  177. if (pnt) {
  178. libname = pnt + 1;
  179. }
  180. _dl_if_debug_dprint("\tfind library='%s'; searching\n", libname);
  181. /* If the filename has any '/', try it straight and leave it at that.
  182. For IBCS2 compatibility under linux, we substitute the string
  183. /usr/i486-sysv4/lib for /usr/lib in library names. */
  184. if (libname != full_libname) {
  185. _dl_if_debug_dprint("\ttrying file='%s'\n", full_libname);
  186. tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
  187. if (tpnt1) {
  188. return tpnt1;
  189. }
  190. }
  191. /*
  192. * The ABI specifies that RPATH is searched before LD_LIBRARY_PATH or
  193. * the default path of /usr/lib. Check in rpath directories.
  194. */
  195. #ifdef __LDSO_RUNPATH__
  196. pnt = (tpnt ? (char *) tpnt->dynamic_info[DT_RPATH] : NULL);
  197. if (pnt) {
  198. pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
  199. _dl_if_debug_dprint("\tsearching RPATH='%s'\n", pnt);
  200. if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
  201. return tpnt1;
  202. }
  203. #endif
  204. /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
  205. if (_dl_library_path) {
  206. _dl_if_debug_dprint("\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
  207. if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
  208. {
  209. return tpnt1;
  210. }
  211. }
  212. /*
  213. * The ABI specifies that RUNPATH is searched after LD_LIBRARY_PATH.
  214. */
  215. #ifdef __LDSO_RUNPATH__
  216. pnt = (tpnt ? (char *)tpnt->dynamic_info[DT_RUNPATH] : NULL);
  217. if (pnt) {
  218. pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
  219. _dl_if_debug_dprint("\tsearching RUNPATH='%s'\n", pnt);
  220. if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
  221. return tpnt1;
  222. }
  223. #endif
  224. /*
  225. * Where should the cache be searched? There is no such concept in the
  226. * ABI, so we have some flexibility here. For now, search it before
  227. * the hard coded paths that follow (i.e before /lib and /usr/lib).
  228. */
  229. #ifdef __LDSO_CACHE_SUPPORT__
  230. if (_dl_cache_addr != NULL && _dl_cache_addr != MAP_FAILED) {
  231. int i;
  232. header_t *header = (header_t *) _dl_cache_addr;
  233. libentry_t *libent = (libentry_t *) & header[1];
  234. char *strs = (char *) &libent[header->nlibs];
  235. _dl_if_debug_dprint("\tsearching cache='%s'\n", LDSO_CACHE);
  236. for (i = 0; i < header->nlibs; i++) {
  237. if ((libent[i].flags == LIB_ELF
  238. || libent[i].flags == LIB_ELF_LIBC0
  239. || libent[i].flags == LIB_ELF_LIBC5)
  240. && _dl_strcmp(libname, strs + libent[i].sooffset) == 0
  241. && (tpnt1 = _dl_load_elf_shared_library(secure, rpnt, strs + libent[i].liboffset))
  242. ) {
  243. return tpnt1;
  244. }
  245. }
  246. }
  247. #endif
  248. /* Look for libraries wherever the shared library loader
  249. * was installed */
  250. _dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath);
  251. tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt);
  252. if (tpnt1 != NULL)
  253. return tpnt1;
  254. /* Lastly, search the standard list of paths for the library.
  255. This list must exactly match the list in uClibc/ldso/util/ldd.c */
  256. _dl_if_debug_dprint("\tsearching full lib path list\n");
  257. tpnt1 = search_for_named_library(libname, secure,
  258. UCLIBC_RUNTIME_PREFIX "lib:"
  259. UCLIBC_RUNTIME_PREFIX "usr/lib"
  260. #ifndef __LDSO_CACHE_SUPPORT__
  261. ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
  262. #endif
  263. , rpnt);
  264. if (tpnt1 != NULL)
  265. return tpnt1;
  266. goof:
  267. /* Well, we shot our wad on that one. All we can do now is punt */
  268. if (_dl_internal_error_number)
  269. _dl_error_number = _dl_internal_error_number;
  270. else
  271. _dl_error_number = LD_ERROR_NOFILE;
  272. _dl_if_debug_dprint("Bummer: could not find '%s'!\n", libname);
  273. return NULL;
  274. }
  275. /*
  276. * Read one ELF library into memory, mmap it into the correct locations and
  277. * add the symbol info to the symbol chain. Perform any relocations that
  278. * are required.
  279. */
  280. struct elf_resolve *_dl_load_elf_shared_library(int secure,
  281. struct dyn_elf **rpnt, char *libname)
  282. {
  283. ElfW(Ehdr) *epnt;
  284. unsigned long dynamic_addr = 0;
  285. ElfW(Dyn) *dpnt;
  286. struct elf_resolve *tpnt;
  287. ElfW(Phdr) *ppnt;
  288. #if defined(USE_TLS) && USE_TLS
  289. ElfW(Phdr) *tlsppnt = NULL;
  290. #endif
  291. char *status, *header;
  292. unsigned long dynamic_info[DYNAMIC_SIZE];
  293. unsigned long *lpnt;
  294. unsigned long libaddr;
  295. unsigned long minvma = 0xffffffff, maxvma = 0;
  296. int i, flags, piclib, infile;
  297. ElfW(Addr) relro_addr = 0;
  298. size_t relro_size = 0;
  299. struct stat st;
  300. uint32_t *p32;
  301. DL_LOADADDR_TYPE lib_loadaddr;
  302. DL_INIT_LOADADDR_EXTRA_DECLS
  303. libaddr = 0;
  304. infile = _dl_open(libname, O_RDONLY, 0);
  305. if (infile < 0) {
  306. _dl_internal_error_number = LD_ERROR_NOFILE;
  307. return NULL;
  308. }
  309. if (_dl_fstat(infile, &st) < 0) {
  310. _dl_internal_error_number = LD_ERROR_NOFILE;
  311. _dl_close(infile);
  312. return NULL;
  313. }
  314. /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
  315. we don't load the library if it isn't setuid. */
  316. if (secure) {
  317. if (!(st.st_mode & S_ISUID)) {
  318. _dl_close(infile);
  319. return NULL;
  320. }
  321. }
  322. /* Check if file is already loaded */
  323. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  324. if (tpnt->st_dev == st.st_dev && tpnt->st_ino == st.st_ino) {
  325. /* Already loaded */
  326. tpnt->usage_count++;
  327. _dl_close(infile);
  328. return tpnt;
  329. }
  330. }
  331. header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  332. MAP_PRIVATE | MAP_ANONYMOUS | MAP_UNINITIALIZE, -1, 0);
  333. if (_dl_mmap_check_error(header)) {
  334. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  335. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  336. _dl_close(infile);
  337. return NULL;
  338. }
  339. _dl_read(infile, header, _dl_pagesize);
  340. epnt = (ElfW(Ehdr) *) (intptr_t) header;
  341. p32 = (uint32_t*)&epnt->e_ident;
  342. if (*p32 != ELFMAG_U32) {
  343. _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  344. libname);
  345. _dl_internal_error_number = LD_ERROR_NOTELF;
  346. _dl_close(infile);
  347. _dl_munmap(header, _dl_pagesize);
  348. return NULL;
  349. }
  350. if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
  351. #ifdef MAGIC2
  352. && epnt->e_machine != MAGIC2
  353. #endif
  354. ))
  355. {
  356. _dl_internal_error_number =
  357. (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
  358. _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  359. "\n", _dl_progname, libname);
  360. _dl_close(infile);
  361. _dl_munmap(header, _dl_pagesize);
  362. return NULL;
  363. }
  364. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  365. piclib = 1;
  366. for (i = 0; i < epnt->e_phnum; i++) {
  367. if (ppnt->p_type == PT_DYNAMIC) {
  368. if (dynamic_addr)
  369. _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
  370. _dl_progname, libname);
  371. dynamic_addr = ppnt->p_vaddr;
  372. }
  373. if (ppnt->p_type == PT_LOAD) {
  374. /* See if this is a PIC library. */
  375. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  376. piclib = 0;
  377. minvma = ppnt->p_vaddr;
  378. }
  379. if (piclib && ppnt->p_vaddr < minvma) {
  380. minvma = ppnt->p_vaddr;
  381. }
  382. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  383. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  384. }
  385. }
  386. if (ppnt->p_type == PT_TLS) {
  387. #if defined(USE_TLS) && USE_TLS
  388. if (ppnt->p_memsz == 0)
  389. /* Nothing to do for an empty segment. */
  390. continue;
  391. else
  392. /* Save for after 'tpnt' is actually allocated. */
  393. tlsppnt = ppnt;
  394. #else
  395. /*
  396. * Yup, the user was an idiot and tried to sneak in a library with
  397. * TLS in it and we don't support it. Let's fall on our own sword
  398. * and scream at the luser while we die.
  399. */
  400. _dl_dprintf(2, "%s: '%s' library contains unsupported TLS\n",
  401. _dl_progname, libname);
  402. _dl_internal_error_number = LD_ERROR_TLS_FAILED;
  403. _dl_close(infile);
  404. _dl_munmap(header, _dl_pagesize);
  405. return NULL;
  406. #endif
  407. }
  408. ppnt++;
  409. }
  410. DL_CHECK_LIB_TYPE (epnt, piclib, _dl_progname, libname);
  411. maxvma = (maxvma + ADDR_ALIGN) & PAGE_ALIGN;
  412. minvma = minvma & ~0xffffU;
  413. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  414. if (!piclib)
  415. flags |= MAP_FIXED;
  416. if (piclib == 0 || piclib == 1) {
  417. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  418. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  419. if (_dl_mmap_check_error(status)) {
  420. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  421. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  422. _dl_close(infile);
  423. _dl_munmap(header, _dl_pagesize);
  424. return NULL;
  425. }
  426. libaddr = (unsigned long) status;
  427. flags |= MAP_FIXED;
  428. }
  429. /* Get the memory to store the library */
  430. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  431. DL_INIT_LOADADDR(lib_loadaddr, libaddr, ppnt, epnt->e_phnum);
  432. for (i = 0; i < epnt->e_phnum; i++) {
  433. if (DL_IS_SPECIAL_SEGMENT (epnt, ppnt)) {
  434. char *addr;
  435. addr = DL_MAP_SEGMENT (epnt, ppnt, infile, flags);
  436. if (addr == NULL)
  437. goto cant_map;
  438. DL_INIT_LOADADDR_HDR (lib_loadaddr, addr, ppnt);
  439. ppnt++;
  440. continue;
  441. }
  442. if (ppnt->p_type == PT_GNU_RELRO) {
  443. relro_addr = ppnt->p_vaddr;
  444. relro_size = ppnt->p_memsz;
  445. }
  446. if (ppnt->p_type == PT_LOAD) {
  447. char *tryaddr;
  448. ssize_t size;
  449. /* See if this is a PIC library. */
  450. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  451. piclib = 0;
  452. /* flags |= MAP_FIXED; */
  453. }
  454. if (ppnt->p_flags & PF_W) {
  455. unsigned long map_size;
  456. char *cpnt;
  457. char *piclib2map = 0;
  458. if (piclib == 2 &&
  459. /* We might be able to avoid this
  460. call if memsz doesn't require
  461. an additional page, but this
  462. would require mmap to always
  463. return page-aligned addresses
  464. and a whole number of pages
  465. allocated. Unfortunately on
  466. uClinux may return misaligned
  467. addresses and may allocate
  468. partial pages, so we may end up
  469. doing unnecessary mmap calls.
  470. This is what we could do if we
  471. knew mmap would always return
  472. aligned pages:
  473. ((ppnt->p_vaddr + ppnt->p_filesz
  474. + ADDR_ALIGN)
  475. & PAGE_ALIGN)
  476. < ppnt->p_vaddr + ppnt->p_memsz)
  477. Instead, we have to do this: */
  478. ppnt->p_filesz < ppnt->p_memsz)
  479. {
  480. piclib2map = (char *)
  481. _dl_mmap(0, (ppnt->p_vaddr & ADDR_ALIGN)
  482. + ppnt->p_memsz,
  483. LXFLAGS(ppnt->p_flags),
  484. flags | MAP_ANONYMOUS, -1, 0);
  485. if (_dl_mmap_check_error(piclib2map))
  486. goto cant_map;
  487. DL_INIT_LOADADDR_HDR
  488. (lib_loadaddr, piclib2map
  489. + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
  490. }
  491. tryaddr = piclib == 2 ? piclib2map
  492. : ((char*) (piclib ? libaddr : 0) +
  493. (ppnt->p_vaddr & PAGE_ALIGN));
  494. size = (ppnt->p_vaddr & ADDR_ALIGN)
  495. + ppnt->p_filesz;
  496. /* For !MMU, mmap to fixed address will fail.
  497. So instead of desperately call mmap and fail,
  498. we set status to MAP_FAILED to save a call
  499. to mmap (). */
  500. #ifndef __ARCH_USE_MMU__
  501. if (piclib2map == 0)
  502. #endif
  503. status = (char *) _dl_mmap
  504. (tryaddr, size, LXFLAGS(ppnt->p_flags),
  505. flags | (piclib2map ? MAP_FIXED : 0),
  506. infile, ppnt->p_offset & OFFS_ALIGN);
  507. #ifndef __ARCH_USE_MMU__
  508. else
  509. status = MAP_FAILED;
  510. #endif
  511. #ifdef _DL_PREAD
  512. if (_dl_mmap_check_error(status) && piclib2map
  513. && (_DL_PREAD (infile, tryaddr, size,
  514. ppnt->p_offset & OFFS_ALIGN)
  515. == size))
  516. status = tryaddr;
  517. #endif
  518. if (_dl_mmap_check_error(status)
  519. || (tryaddr && tryaddr != status)) {
  520. cant_map:
  521. _dl_dprintf(2, "%s:%i: can't map '%s'\n",
  522. _dl_progname, __LINE__, libname);
  523. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  524. DL_LOADADDR_UNMAP (lib_loadaddr, maxvma - minvma);
  525. _dl_close(infile);
  526. _dl_munmap(header, _dl_pagesize);
  527. return NULL;
  528. }
  529. if (! piclib2map) {
  530. DL_INIT_LOADADDR_HDR
  531. (lib_loadaddr, status
  532. + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
  533. }
  534. /* Now we want to allocate and
  535. zero-out any data from the end of
  536. the region we mapped in from the
  537. file (filesz) to the end of the
  538. loadable segment (memsz). We may
  539. need additional pages for memsz,
  540. that we map in below, and we can
  541. count on the kernel to zero them
  542. out, but we have to zero out stuff
  543. in the last page that we mapped in
  544. from the file. However, we can't
  545. assume to have actually obtained
  546. full pages from the kernel, since
  547. we didn't ask for them, and uClibc
  548. may not give us full pages for
  549. small allocations. So only zero
  550. out up to memsz or the end of the
  551. page, whichever comes first. */
  552. /* CPNT is the beginning of the memsz
  553. portion not backed by filesz. */
  554. cpnt = (char *) (status + size);
  555. /* MAP_SIZE is the address of the
  556. beginning of the next page. */
  557. map_size = (ppnt->p_vaddr + ppnt->p_filesz
  558. + ADDR_ALIGN) & PAGE_ALIGN;
  559. #ifndef MIN
  560. # define MIN(a,b) ((a) < (b) ? (a) : (b))
  561. #endif
  562. _dl_memset (cpnt, 0,
  563. MIN (map_size
  564. - (ppnt->p_vaddr
  565. + ppnt->p_filesz),
  566. ppnt->p_memsz
  567. - ppnt->p_filesz));
  568. if (map_size < ppnt->p_vaddr + ppnt->p_memsz
  569. && !piclib2map) {
  570. tryaddr = map_size + (char*)(piclib ? libaddr : 0);
  571. status = (char *) _dl_mmap(tryaddr,
  572. ppnt->p_vaddr + ppnt->p_memsz - map_size,
  573. LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  574. if (_dl_mmap_check_error(status)
  575. || tryaddr != status)
  576. goto cant_map;
  577. }
  578. } else {
  579. tryaddr = (piclib == 2 ? 0
  580. : (char *) (ppnt->p_vaddr & PAGE_ALIGN)
  581. + (piclib ? libaddr : 0));
  582. size = (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_filesz;
  583. status = (char *) _dl_mmap
  584. (tryaddr, size, LXFLAGS(ppnt->p_flags),
  585. flags | (piclib == 2 ? MAP_EXECUTABLE
  586. | MAP_DENYWRITE : 0),
  587. infile, ppnt->p_offset & OFFS_ALIGN);
  588. if (_dl_mmap_check_error(status)
  589. || (tryaddr && tryaddr != status))
  590. goto cant_map;
  591. DL_INIT_LOADADDR_HDR
  592. (lib_loadaddr, status
  593. + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
  594. }
  595. /* if (libaddr == 0 && piclib) {
  596. libaddr = (unsigned long) status;
  597. flags |= MAP_FIXED;
  598. } */
  599. }
  600. ppnt++;
  601. }
  602. _dl_close(infile);
  603. /* For a non-PIC library, the addresses are all absolute */
  604. if (piclib) {
  605. dynamic_addr = (unsigned long) DL_RELOC_ADDR(lib_loadaddr, dynamic_addr);
  606. }
  607. /*
  608. * OK, the ELF library is now loaded into VM in the correct locations
  609. * The next step is to go through and do the dynamic linking (if needed).
  610. */
  611. /* Start by scanning the dynamic section to get all of the pointers */
  612. if (!dynamic_addr) {
  613. _dl_internal_error_number = LD_ERROR_NODYNAMIC;
  614. _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
  615. _dl_progname, libname);
  616. _dl_munmap(header, _dl_pagesize);
  617. return NULL;
  618. }
  619. dpnt = (ElfW(Dyn) *) dynamic_addr;
  620. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  621. _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
  622. /* If the TEXTREL is set, this means that we need to make the pages
  623. writable before we perform relocations. Do this now. They get set
  624. back again later. */
  625. if (dynamic_info[DT_TEXTREL]) {
  626. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  627. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  628. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  629. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W)) {
  630. _dl_mprotect((void *) ((piclib ? libaddr : 0) +
  631. (ppnt->p_vaddr & PAGE_ALIGN)),
  632. (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
  633. PROT_READ | PROT_WRITE | PROT_EXEC);
  634. }
  635. }
  636. #else
  637. _dl_dprintf(_dl_debug_file, "Can't modify %s's text section."
  638. " Use GCC option -fPIC for shared objects, please.\n",
  639. libname);
  640. _dl_exit(1);
  641. #endif
  642. }
  643. tpnt = _dl_add_elf_hash_table(libname, lib_loadaddr, dynamic_info,
  644. dynamic_addr, 0);
  645. tpnt->relro_addr = relro_addr;
  646. tpnt->relro_size = relro_size;
  647. tpnt->st_dev = st.st_dev;
  648. tpnt->st_ino = st.st_ino;
  649. tpnt->ppnt = (ElfW(Phdr) *) DL_RELOC_ADDR(tpnt->loadaddr, epnt->e_phoff);
  650. tpnt->n_phent = epnt->e_phnum;
  651. #if defined(USE_TLS) && USE_TLS
  652. if (tlsppnt) {
  653. _dl_debug_early("Found TLS header for %s\n", libname);
  654. #if NO_TLS_OFFSET != 0
  655. tpnt->l_tls_offset = NO_TLS_OFFSET;
  656. #endif
  657. tpnt->l_tls_blocksize = tlsppnt->p_memsz;
  658. tpnt->l_tls_align = tlsppnt->p_align;
  659. if (tlsppnt->p_align == 0)
  660. tpnt->l_tls_firstbyte_offset = 0;
  661. else
  662. tpnt->l_tls_firstbyte_offset = tlsppnt->p_vaddr &
  663. (tlsppnt->p_align - 1);
  664. tpnt->l_tls_initimage_size = tlsppnt->p_filesz;
  665. tpnt->l_tls_initimage = (void *) tlsppnt->p_vaddr;
  666. /* Assign the next available module ID. */
  667. tpnt->l_tls_modid = _dl_next_tls_modid ();
  668. /* We know the load address, so add it to the offset. */
  669. if (tpnt->l_tls_initimage != NULL)
  670. {
  671. unsigned int tmp = (unsigned int) tpnt->l_tls_initimage;
  672. tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
  673. _dl_debug_early("Relocated TLS initial image from %x to %x (size = %x)\n", tmp, tpnt->l_tls_initimage, tpnt->l_tls_initimage_size);
  674. tmp = 0;
  675. }
  676. }
  677. #endif
  678. /*
  679. * Add this object into the symbol chain
  680. */
  681. if (*rpnt) {
  682. (*rpnt)->next = _dl_malloc(sizeof(struct dyn_elf));
  683. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  684. (*rpnt)->next->prev = (*rpnt);
  685. *rpnt = (*rpnt)->next;
  686. }
  687. #ifndef SHARED
  688. /* When statically linked, the first time we dlopen a DSO
  689. * the *rpnt is NULL, so we need to allocate memory for it,
  690. * and initialize the _dl_symbol_table.
  691. */
  692. else {
  693. *rpnt = _dl_symbol_tables = _dl_malloc(sizeof(struct dyn_elf));
  694. _dl_memset(*rpnt, 0, sizeof(struct dyn_elf));
  695. }
  696. #endif
  697. (*rpnt)->dyn = tpnt;
  698. tpnt->symbol_scope = _dl_symbol_tables;
  699. tpnt->usage_count++;
  700. tpnt->libtype = elf_lib;
  701. /*
  702. * OK, the next thing we need to do is to insert the dynamic linker into
  703. * the proper entry in the GOT so that the PLT symbols can be properly
  704. * resolved.
  705. */
  706. lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
  707. if (lpnt) {
  708. lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
  709. INIT_GOT(lpnt, tpnt);
  710. }
  711. _dl_if_debug_dprint("\n\tfile='%s'; generating link map\n", libname);
  712. _dl_if_debug_dprint("\t\tdynamic: %x base: %x\n", dynamic_addr, DL_LOADADDR_BASE(lib_loadaddr));
  713. _dl_if_debug_dprint("\t\t entry: %x phdr: %x phnum: %x\n\n",
  714. DL_RELOC_ADDR(lib_loadaddr, epnt->e_entry), tpnt->ppnt, tpnt->n_phent);
  715. _dl_munmap(header, _dl_pagesize);
  716. return tpnt;
  717. }
  718. /* now_flag must be RTLD_NOW or zero */
  719. int _dl_fixup(struct dyn_elf *rpnt, int now_flag)
  720. {
  721. int goof = 0;
  722. struct elf_resolve *tpnt;
  723. ElfW(Word) reloc_size, relative_count;
  724. ElfW(Addr) reloc_addr;
  725. if (rpnt->next)
  726. goof = _dl_fixup(rpnt->next, now_flag);
  727. if (goof)
  728. return goof;
  729. tpnt = rpnt->dyn;
  730. if (!(tpnt->init_flag & RELOCS_DONE))
  731. _dl_if_debug_dprint("relocation processing: %s\n", tpnt->libname);
  732. if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
  733. _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
  734. _dl_progname, UNSUPPORTED_RELOC_STR);
  735. goof++;
  736. return goof;
  737. }
  738. reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
  739. /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
  740. range. Note that according to the ELF spec, this is completely legal! */
  741. #ifdef ELF_MACHINE_PLTREL_OVERLAP
  742. reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
  743. #endif
  744. if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
  745. !(tpnt->init_flag & RELOCS_DONE)) {
  746. reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
  747. relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
  748. if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
  749. reloc_size -= relative_count * sizeof(ELF_RELOC);
  750. elf_machine_relative(tpnt->loadaddr, reloc_addr, relative_count);
  751. reloc_addr += relative_count * sizeof(ELF_RELOC);
  752. }
  753. goof += _dl_parse_relocation_information(rpnt,
  754. reloc_addr,
  755. reloc_size);
  756. tpnt->init_flag |= RELOCS_DONE;
  757. }
  758. if (tpnt->dynamic_info[DT_BIND_NOW])
  759. now_flag = RTLD_NOW;
  760. if (tpnt->dynamic_info[DT_JMPREL] &&
  761. (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
  762. (now_flag && !(tpnt->rtld_flags & now_flag)))) {
  763. tpnt->rtld_flags |= now_flag;
  764. if (!(tpnt->rtld_flags & RTLD_NOW)) {
  765. _dl_parse_lazy_relocation_information(rpnt,
  766. tpnt->dynamic_info[DT_JMPREL],
  767. tpnt->dynamic_info [DT_PLTRELSZ]);
  768. } else {
  769. goof += _dl_parse_relocation_information(rpnt,
  770. tpnt->dynamic_info[DT_JMPREL],
  771. tpnt->dynamic_info[DT_PLTRELSZ]);
  772. }
  773. tpnt->init_flag |= JMP_RELOCS_DONE;
  774. }
  775. #if 0
  776. /* _dl_add_to_slotinfo is called by init_tls() for initial DSO
  777. or by dlopen() for dynamically loaded DSO. */
  778. #if defined(USE_TLS) && USE_TLS
  779. /* Add object to slot information data if necessasy. */
  780. if (tpnt->l_tls_blocksize != 0 && tls_init_tp_called)
  781. _dl_add_to_slotinfo ((struct link_map *) tpnt);
  782. #endif
  783. #endif
  784. return goof;
  785. }
  786. /* Minimal printf which handles only %s, %d, and %x */
  787. void _dl_dprintf(int fd, const char *fmt, ...)
  788. {
  789. #if __WORDSIZE > 32
  790. long int num;
  791. #else
  792. int num;
  793. #endif
  794. va_list args;
  795. char *start, *ptr, *string;
  796. static char *buf;
  797. if (!fmt)
  798. return;
  799. buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  800. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  801. if (_dl_mmap_check_error(buf)) {
  802. _dl_write(fd, "mmap of a spare page failed!\n", 29);
  803. _dl_exit(20);
  804. }
  805. start = ptr = buf;
  806. if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
  807. _dl_write(fd, "overflow\n", 11);
  808. _dl_exit(20);
  809. }
  810. _dl_strcpy(buf, fmt);
  811. va_start(args, fmt);
  812. while (start) {
  813. while (*ptr != '%' && *ptr) {
  814. ptr++;
  815. }
  816. if (*ptr == '%') {
  817. *ptr++ = '\0';
  818. _dl_write(fd, start, _dl_strlen(start));
  819. switch (*ptr++) {
  820. case 's':
  821. string = va_arg(args, char *);
  822. if (!string)
  823. _dl_write(fd, "(null)", 6);
  824. else
  825. _dl_write(fd, string, _dl_strlen(string));
  826. break;
  827. case 'i':
  828. case 'd':
  829. {
  830. char tmp[22];
  831. #if __WORDSIZE > 32
  832. num = va_arg(args, long int);
  833. #else
  834. num = va_arg(args, int);
  835. #endif
  836. string = _dl_simple_ltoa(tmp, num);
  837. _dl_write(fd, string, _dl_strlen(string));
  838. break;
  839. }
  840. case 'x':
  841. case 'X':
  842. {
  843. char tmp[22];
  844. #if __WORDSIZE > 32
  845. num = va_arg(args, long int);
  846. #else
  847. num = va_arg(args, int);
  848. #endif
  849. string = _dl_simple_ltoahex(tmp, num);
  850. _dl_write(fd, string, _dl_strlen(string));
  851. break;
  852. }
  853. default:
  854. _dl_write(fd, "(null)", 6);
  855. break;
  856. }
  857. start = ptr;
  858. } else {
  859. _dl_write(fd, start, _dl_strlen(start));
  860. start = NULL;
  861. }
  862. }
  863. _dl_munmap(buf, _dl_pagesize);
  864. return;
  865. }
  866. char *_dl_strdup(const char *string)
  867. {
  868. char *retval;
  869. int len;
  870. len = _dl_strlen(string);
  871. retval = _dl_malloc(len + 1);
  872. _dl_strcpy(retval, string);
  873. return retval;
  874. }
  875. void _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[],
  876. void *debug_addr, DL_LOADADDR_TYPE load_off)
  877. {
  878. __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);
  879. }