dl-elf.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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, const 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. unsigned int rtld_flags;
  297. int i, flags, piclib, infile;
  298. ElfW(Addr) relro_addr = 0;
  299. size_t relro_size = 0;
  300. struct stat st;
  301. uint32_t *p32;
  302. DL_LOADADDR_TYPE lib_loadaddr = 0;
  303. DL_INIT_LOADADDR_EXTRA_DECLS
  304. libaddr = 0;
  305. infile = _dl_open(libname, O_RDONLY, 0);
  306. if (infile < 0) {
  307. _dl_internal_error_number = LD_ERROR_NOFILE;
  308. return NULL;
  309. }
  310. if (_dl_fstat(infile, &st) < 0) {
  311. _dl_internal_error_number = LD_ERROR_NOFILE;
  312. _dl_close(infile);
  313. return NULL;
  314. }
  315. /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
  316. we don't load the library if it isn't setuid. */
  317. if (secure) {
  318. if (!(st.st_mode & S_ISUID)) {
  319. _dl_close(infile);
  320. return NULL;
  321. }
  322. }
  323. /* Check if file is already loaded */
  324. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  325. if (tpnt->st_dev == st.st_dev && tpnt->st_ino == st.st_ino) {
  326. /* Already loaded */
  327. tpnt->usage_count++;
  328. _dl_close(infile);
  329. return tpnt;
  330. }
  331. }
  332. header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  333. MAP_PRIVATE | MAP_ANONYMOUS | MAP_UNINITIALIZE, -1, 0);
  334. if (_dl_mmap_check_error(header)) {
  335. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  336. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  337. _dl_close(infile);
  338. return NULL;
  339. }
  340. _dl_read(infile, header, _dl_pagesize);
  341. epnt = (ElfW(Ehdr) *) (intptr_t) header;
  342. p32 = (uint32_t*)&epnt->e_ident;
  343. if (*p32 != ELFMAG_U32) {
  344. _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  345. libname);
  346. _dl_internal_error_number = LD_ERROR_NOTELF;
  347. _dl_close(infile);
  348. _dl_munmap(header, _dl_pagesize);
  349. return NULL;
  350. }
  351. if ((epnt->e_type != ET_DYN
  352. #ifdef __LDSO_STANDALONE_SUPPORT__
  353. && epnt->e_type != ET_EXEC
  354. #endif
  355. ) || (epnt->e_machine != MAGIC1
  356. #ifdef MAGIC2
  357. && epnt->e_machine != MAGIC2
  358. #endif
  359. ))
  360. {
  361. _dl_internal_error_number =
  362. (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
  363. _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  364. "\n", _dl_progname, libname);
  365. _dl_close(infile);
  366. _dl_munmap(header, _dl_pagesize);
  367. return NULL;
  368. }
  369. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  370. piclib = 1;
  371. for (i = 0; i < epnt->e_phnum; i++) {
  372. if (ppnt->p_type == PT_DYNAMIC) {
  373. if (dynamic_addr)
  374. _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
  375. _dl_progname, libname);
  376. dynamic_addr = ppnt->p_vaddr;
  377. }
  378. if (ppnt->p_type == PT_LOAD) {
  379. /* See if this is a PIC library. */
  380. if (minvma == 0xffffffff && ppnt->p_vaddr > 0x1000000) {
  381. piclib = 0;
  382. minvma = ppnt->p_vaddr;
  383. }
  384. if (piclib && ppnt->p_vaddr < minvma) {
  385. minvma = ppnt->p_vaddr;
  386. }
  387. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  388. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  389. }
  390. }
  391. if (ppnt->p_type == PT_TLS) {
  392. #if defined(USE_TLS) && USE_TLS
  393. if (ppnt->p_memsz == 0)
  394. /* Nothing to do for an empty segment. */
  395. continue;
  396. else
  397. /* Save for after 'tpnt' is actually allocated. */
  398. tlsppnt = ppnt;
  399. #else
  400. /*
  401. * Yup, the user was an idiot and tried to sneak in a library with
  402. * TLS in it and we don't support it. Let's fall on our own sword
  403. * and scream at the luser while we die.
  404. */
  405. _dl_dprintf(2, "%s: '%s' library contains unsupported TLS\n",
  406. _dl_progname, libname);
  407. _dl_internal_error_number = LD_ERROR_TLS_FAILED;
  408. _dl_close(infile);
  409. _dl_munmap(header, _dl_pagesize);
  410. return NULL;
  411. #endif
  412. }
  413. ppnt++;
  414. }
  415. #ifdef __LDSO_STANDALONE_SUPPORT__
  416. if (epnt->e_type == ET_EXEC)
  417. piclib = 0;
  418. #endif
  419. DL_CHECK_LIB_TYPE (epnt, piclib, _dl_progname, libname);
  420. maxvma = (maxvma + ADDR_ALIGN) & PAGE_ALIGN;
  421. minvma = minvma & ~ADDR_ALIGN;
  422. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  423. if (piclib == 0 || piclib == 1) {
  424. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  425. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  426. if (_dl_mmap_check_error(status)) {
  427. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  428. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  429. _dl_close(infile);
  430. _dl_munmap(header, _dl_pagesize);
  431. return NULL;
  432. }
  433. libaddr = (unsigned long) status;
  434. flags |= MAP_FIXED;
  435. }
  436. /* Get the memory to store the library */
  437. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  438. DL_INIT_LOADADDR(lib_loadaddr, libaddr - minvma, ppnt, epnt->e_phnum);
  439. for (i = 0; i < epnt->e_phnum; i++) {
  440. if (DL_IS_SPECIAL_SEGMENT (epnt, ppnt)) {
  441. char *addr;
  442. addr = DL_MAP_SEGMENT (epnt, ppnt, infile, flags);
  443. if (addr == NULL)
  444. goto cant_map;
  445. DL_INIT_LOADADDR_HDR (lib_loadaddr, addr, ppnt);
  446. ppnt++;
  447. continue;
  448. }
  449. if (ppnt->p_type == PT_GNU_RELRO) {
  450. relro_addr = ppnt->p_vaddr;
  451. relro_size = ppnt->p_memsz;
  452. }
  453. if (ppnt->p_type == PT_LOAD) {
  454. char *tryaddr;
  455. ssize_t size;
  456. if (ppnt->p_flags & PF_W) {
  457. unsigned long map_size;
  458. char *cpnt;
  459. char *piclib2map = 0;
  460. if (piclib == 2 &&
  461. /* We might be able to avoid this
  462. call if memsz doesn't require
  463. an additional page, but this
  464. would require mmap to always
  465. return page-aligned addresses
  466. and a whole number of pages
  467. allocated. Unfortunately on
  468. uClinux may return misaligned
  469. addresses and may allocate
  470. partial pages, so we may end up
  471. doing unnecessary mmap calls.
  472. This is what we could do if we
  473. knew mmap would always return
  474. aligned pages:
  475. ((ppnt->p_vaddr + ppnt->p_filesz
  476. + ADDR_ALIGN)
  477. & PAGE_ALIGN)
  478. < ppnt->p_vaddr + ppnt->p_memsz)
  479. Instead, we have to do this: */
  480. ppnt->p_filesz < ppnt->p_memsz)
  481. {
  482. piclib2map = (char *)
  483. _dl_mmap(0, (ppnt->p_vaddr & ADDR_ALIGN)
  484. + ppnt->p_memsz,
  485. LXFLAGS(ppnt->p_flags),
  486. flags | MAP_ANONYMOUS, -1, 0);
  487. if (_dl_mmap_check_error(piclib2map))
  488. goto cant_map;
  489. DL_INIT_LOADADDR_HDR
  490. (lib_loadaddr, piclib2map
  491. + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
  492. }
  493. tryaddr = piclib == 2 ? piclib2map
  494. : ((char*) (piclib ? libaddr : lib_loadaddr) +
  495. (ppnt->p_vaddr & PAGE_ALIGN));
  496. size = (ppnt->p_vaddr & ADDR_ALIGN)
  497. + ppnt->p_filesz;
  498. /* For !MMU, mmap to fixed address will fail.
  499. So instead of desperately call mmap and fail,
  500. we set status to MAP_FAILED to save a call
  501. to mmap (). */
  502. #ifndef __ARCH_USE_MMU__
  503. if (piclib2map == 0)
  504. #endif
  505. status = (char *) _dl_mmap
  506. (tryaddr, size, LXFLAGS(ppnt->p_flags),
  507. flags | (piclib2map ? MAP_FIXED : 0),
  508. infile, ppnt->p_offset & OFFS_ALIGN);
  509. #ifndef __ARCH_USE_MMU__
  510. else
  511. status = MAP_FAILED;
  512. #endif
  513. #ifdef _DL_PREAD
  514. if (_dl_mmap_check_error(status) && piclib2map
  515. && (_DL_PREAD (infile, tryaddr, size,
  516. ppnt->p_offset & OFFS_ALIGN)
  517. == size))
  518. status = tryaddr;
  519. #endif
  520. if (_dl_mmap_check_error(status)
  521. || (tryaddr && tryaddr != status)) {
  522. cant_map:
  523. _dl_dprintf(2, "%s:%i: can't map '%s'\n",
  524. _dl_progname, __LINE__, libname);
  525. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  526. DL_LOADADDR_UNMAP (lib_loadaddr, maxvma - minvma);
  527. _dl_close(infile);
  528. _dl_munmap(header, _dl_pagesize);
  529. return NULL;
  530. }
  531. if (! piclib2map) {
  532. DL_INIT_LOADADDR_HDR
  533. (lib_loadaddr, status
  534. + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
  535. }
  536. /* Now we want to allocate and
  537. zero-out any data from the end of
  538. the region we mapped in from the
  539. file (filesz) to the end of the
  540. loadable segment (memsz). We may
  541. need additional pages for memsz,
  542. that we map in below, and we can
  543. count on the kernel to zero them
  544. out, but we have to zero out stuff
  545. in the last page that we mapped in
  546. from the file. However, we can't
  547. assume to have actually obtained
  548. full pages from the kernel, since
  549. we didn't ask for them, and uClibc
  550. may not give us full pages for
  551. small allocations. So only zero
  552. out up to memsz or the end of the
  553. page, whichever comes first. */
  554. /* CPNT is the beginning of the memsz
  555. portion not backed by filesz. */
  556. cpnt = (char *) (status + size);
  557. /* MAP_SIZE is the address of the
  558. beginning of the next page. */
  559. map_size = (ppnt->p_vaddr + ppnt->p_filesz
  560. + ADDR_ALIGN) & PAGE_ALIGN;
  561. #ifndef MIN
  562. # define MIN(a,b) ((a) < (b) ? (a) : (b))
  563. #endif
  564. _dl_memset (cpnt, 0,
  565. MIN (map_size
  566. - (ppnt->p_vaddr
  567. + ppnt->p_filesz),
  568. ppnt->p_memsz
  569. - ppnt->p_filesz));
  570. if (map_size < ppnt->p_vaddr + ppnt->p_memsz
  571. && !piclib2map) {
  572. tryaddr = map_size + (char*)(piclib ? libaddr : lib_loadaddr);
  573. status = (char *) _dl_mmap(tryaddr,
  574. ppnt->p_vaddr + ppnt->p_memsz - map_size,
  575. LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  576. if (_dl_mmap_check_error(status)
  577. || tryaddr != status)
  578. goto cant_map;
  579. }
  580. } else {
  581. tryaddr = (piclib == 2 ? 0
  582. : (char *) (ppnt->p_vaddr & PAGE_ALIGN)
  583. + (piclib ? libaddr : lib_loadaddr));
  584. size = (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_filesz;
  585. status = (char *) _dl_mmap
  586. (tryaddr, size, LXFLAGS(ppnt->p_flags),
  587. flags | (piclib == 2 ? MAP_EXECUTABLE
  588. | MAP_DENYWRITE : 0),
  589. infile, ppnt->p_offset & OFFS_ALIGN);
  590. if (_dl_mmap_check_error(status)
  591. || (tryaddr && tryaddr != status))
  592. goto cant_map;
  593. DL_INIT_LOADADDR_HDR
  594. (lib_loadaddr, status
  595. + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
  596. }
  597. /* if (libaddr == 0 && piclib) {
  598. libaddr = (unsigned long) status;
  599. flags |= MAP_FIXED;
  600. } */
  601. }
  602. ppnt++;
  603. }
  604. _dl_close(infile);
  605. /*
  606. * The dynamic_addr must be take into acount lib_loadaddr value, to note
  607. * it is zero when the SO has been mapped to the elf's physical addr
  608. */
  609. if (lib_loadaddr) {
  610. dynamic_addr = (unsigned long) DL_RELOC_ADDR(lib_loadaddr, dynamic_addr);
  611. }
  612. /*
  613. * OK, the ELF library is now loaded into VM in the correct locations
  614. * The next step is to go through and do the dynamic linking (if needed).
  615. */
  616. /* Start by scanning the dynamic section to get all of the pointers */
  617. if (!dynamic_addr) {
  618. _dl_internal_error_number = LD_ERROR_NODYNAMIC;
  619. _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
  620. _dl_progname, libname);
  621. _dl_munmap(header, _dl_pagesize);
  622. return NULL;
  623. }
  624. dpnt = (ElfW(Dyn) *) dynamic_addr;
  625. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  626. rtld_flags = _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
  627. /* If the TEXTREL is set, this means that we need to make the pages
  628. writable before we perform relocations. Do this now. They get set
  629. back again later. */
  630. if (dynamic_info[DT_TEXTREL]) {
  631. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  632. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  633. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  634. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W)) {
  635. _dl_mprotect((void *) ((piclib ? libaddr : lib_loadaddr) +
  636. (ppnt->p_vaddr & PAGE_ALIGN)),
  637. (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
  638. PROT_READ | PROT_WRITE | PROT_EXEC);
  639. }
  640. }
  641. #else
  642. _dl_dprintf(_dl_debug_file, "Can't modify %s's text section."
  643. " Use GCC option -fPIC for shared objects, please.\n",
  644. libname);
  645. _dl_exit(1);
  646. #endif
  647. }
  648. tpnt = _dl_add_elf_hash_table(libname, lib_loadaddr, dynamic_info,
  649. dynamic_addr, 0);
  650. tpnt->mapaddr = libaddr;
  651. tpnt->relro_addr = relro_addr;
  652. tpnt->relro_size = relro_size;
  653. tpnt->st_dev = st.st_dev;
  654. tpnt->st_ino = st.st_ino;
  655. tpnt->ppnt = (ElfW(Phdr) *) DL_RELOC_ADDR(tpnt->mapaddr, epnt->e_phoff);
  656. tpnt->n_phent = epnt->e_phnum;
  657. tpnt->rtld_flags |= rtld_flags;
  658. #ifdef __LDSO_STANDALONE_SUPPORT__
  659. tpnt->l_entry = epnt->e_entry;
  660. #endif
  661. #if defined(USE_TLS) && USE_TLS
  662. if (tlsppnt) {
  663. _dl_debug_early("Found TLS header for %s\n", libname);
  664. # if NO_TLS_OFFSET != 0
  665. tpnt->l_tls_offset = NO_TLS_OFFSET;
  666. # endif
  667. tpnt->l_tls_blocksize = tlsppnt->p_memsz;
  668. tpnt->l_tls_align = tlsppnt->p_align;
  669. if (tlsppnt->p_align == 0)
  670. tpnt->l_tls_firstbyte_offset = 0;
  671. else
  672. tpnt->l_tls_firstbyte_offset = tlsppnt->p_vaddr &
  673. (tlsppnt->p_align - 1);
  674. tpnt->l_tls_initimage_size = tlsppnt->p_filesz;
  675. tpnt->l_tls_initimage = (void *) tlsppnt->p_vaddr;
  676. /* Assign the next available module ID. */
  677. tpnt->l_tls_modid = _dl_next_tls_modid ();
  678. /* We know the load address, so add it to the offset. */
  679. #ifdef __LDSO_STANDALONE_SUPPORT__
  680. if ((tpnt->l_tls_initimage != NULL) && piclib)
  681. #else
  682. if (tpnt->l_tls_initimage != NULL)
  683. #endif
  684. {
  685. # ifdef __SUPPORT_LD_DEBUG_EARLY__
  686. unsigned int tmp = (unsigned int) tpnt->l_tls_initimage;
  687. tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
  688. _dl_debug_early("Relocated TLS initial image from %x to %x (size = %x)\n", tmp, tpnt->l_tls_initimage, tpnt->l_tls_initimage_size);
  689. tmp = 0;
  690. # else
  691. tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
  692. # endif
  693. }
  694. }
  695. #endif
  696. /*
  697. * Add this object into the symbol chain
  698. */
  699. if (*rpnt
  700. #ifdef __LDSO_STANDALONE_SUPPORT__
  701. /* Do not create a new chain entry for the main executable */
  702. && (*rpnt)->dyn
  703. #endif
  704. ) {
  705. (*rpnt)->next = _dl_malloc(sizeof(struct dyn_elf));
  706. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  707. (*rpnt)->next->prev = (*rpnt);
  708. *rpnt = (*rpnt)->next;
  709. }
  710. #ifndef SHARED
  711. /* When statically linked, the first time we dlopen a DSO
  712. * the *rpnt is NULL, so we need to allocate memory for it,
  713. * and initialize the _dl_symbol_table.
  714. */
  715. else {
  716. *rpnt = _dl_symbol_tables = _dl_malloc(sizeof(struct dyn_elf));
  717. _dl_memset(*rpnt, 0, sizeof(struct dyn_elf));
  718. }
  719. #endif
  720. (*rpnt)->dyn = tpnt;
  721. tpnt->usage_count++;
  722. #ifdef __LDSO_STANDALONE_SUPPORT__
  723. tpnt->libtype = (epnt->e_type == ET_DYN) ? elf_lib : elf_executable;
  724. #else
  725. tpnt->libtype = elf_lib;
  726. #endif
  727. /*
  728. * OK, the next thing we need to do is to insert the dynamic linker into
  729. * the proper entry in the GOT so that the PLT symbols can be properly
  730. * resolved.
  731. */
  732. lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
  733. if (lpnt) {
  734. lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
  735. INIT_GOT(lpnt, tpnt);
  736. }
  737. _dl_if_debug_dprint("\n\tfile='%s'; generating link map\n", libname);
  738. _dl_if_debug_dprint("\t\tdynamic: %x base: %x\n", dynamic_addr, DL_LOADADDR_BASE(lib_loadaddr));
  739. _dl_if_debug_dprint("\t\t entry: %x phdr: %x phnum: %x\n\n",
  740. DL_RELOC_ADDR(lib_loadaddr, epnt->e_entry), tpnt->ppnt, tpnt->n_phent);
  741. _dl_munmap(header, _dl_pagesize);
  742. return tpnt;
  743. }
  744. /* now_flag must be RTLD_NOW or zero */
  745. int _dl_fixup(struct dyn_elf *rpnt, struct r_scope_elem *scope, int now_flag)
  746. {
  747. int goof = 0;
  748. struct elf_resolve *tpnt;
  749. ElfW(Word) reloc_size, relative_count;
  750. ElfW(Addr) reloc_addr;
  751. if (rpnt->next)
  752. goof = _dl_fixup(rpnt->next, scope, now_flag);
  753. if (goof)
  754. return goof;
  755. tpnt = rpnt->dyn;
  756. if (!(tpnt->init_flag & RELOCS_DONE))
  757. _dl_if_debug_dprint("relocation processing: %s\n", tpnt->libname);
  758. if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
  759. _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
  760. _dl_progname, UNSUPPORTED_RELOC_STR);
  761. goof++;
  762. return goof;
  763. }
  764. reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
  765. /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
  766. range. Note that according to the ELF spec, this is completely legal! */
  767. #ifdef ELF_MACHINE_PLTREL_OVERLAP
  768. reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
  769. #endif
  770. if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
  771. !(tpnt->init_flag & RELOCS_DONE)) {
  772. reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
  773. relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
  774. if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
  775. reloc_size -= relative_count * sizeof(ELF_RELOC);
  776. if (tpnt->loadaddr
  777. #ifdef __LDSO_PRELINK_SUPPORT__
  778. || (!tpnt->dynamic_info[DT_GNU_PRELINKED_IDX])
  779. #endif
  780. )
  781. elf_machine_relative(tpnt->loadaddr, reloc_addr, relative_count);
  782. reloc_addr += relative_count * sizeof(ELF_RELOC);
  783. }
  784. goof += _dl_parse_relocation_information(rpnt, scope,
  785. reloc_addr,
  786. reloc_size);
  787. tpnt->init_flag |= RELOCS_DONE;
  788. }
  789. if (tpnt->dynamic_info[DT_BIND_NOW])
  790. now_flag = RTLD_NOW;
  791. if (tpnt->dynamic_info[DT_JMPREL] &&
  792. (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
  793. (now_flag && !(tpnt->rtld_flags & now_flag)))) {
  794. tpnt->rtld_flags |= now_flag;
  795. if (!(tpnt->rtld_flags & RTLD_NOW)) {
  796. _dl_parse_lazy_relocation_information(rpnt,
  797. tpnt->dynamic_info[DT_JMPREL],
  798. tpnt->dynamic_info [DT_PLTRELSZ]);
  799. } else {
  800. goof += _dl_parse_relocation_information(rpnt, scope,
  801. tpnt->dynamic_info[DT_JMPREL],
  802. tpnt->dynamic_info[DT_PLTRELSZ]);
  803. }
  804. tpnt->init_flag |= JMP_RELOCS_DONE;
  805. }
  806. #if 0
  807. /* _dl_add_to_slotinfo is called by init_tls() for initial DSO
  808. or by dlopen() for dynamically loaded DSO. */
  809. #if defined(USE_TLS) && USE_TLS
  810. /* Add object to slot information data if necessasy. */
  811. if (tpnt->l_tls_blocksize != 0 && tls_init_tp_called)
  812. _dl_add_to_slotinfo ((struct link_map *) tpnt);
  813. #endif
  814. #endif
  815. return goof;
  816. }
  817. /* Minimal printf which handles only %s, %d, and %x */
  818. void _dl_dprintf(int fd, const char *fmt, ...)
  819. {
  820. #if __WORDSIZE > 32
  821. long int num;
  822. #else
  823. int num;
  824. #endif
  825. va_list args;
  826. char *start, *ptr, *string;
  827. char *buf;
  828. if (!fmt)
  829. return;
  830. buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  831. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  832. if (_dl_mmap_check_error(buf)) {
  833. _dl_write(fd, "mmap of a spare page failed!\n", 29);
  834. _dl_exit(20);
  835. }
  836. start = ptr = buf;
  837. if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
  838. _dl_write(fd, "overflow\n", 11);
  839. _dl_exit(20);
  840. }
  841. _dl_strcpy(buf, fmt);
  842. va_start(args, fmt);
  843. while (start) {
  844. while (*ptr != '%' && *ptr) {
  845. ptr++;
  846. }
  847. if (*ptr == '%') {
  848. *ptr++ = '\0';
  849. _dl_write(fd, start, _dl_strlen(start));
  850. switch (*ptr++) {
  851. case 's':
  852. string = va_arg(args, char *);
  853. if (!string)
  854. _dl_write(fd, "(null)", 6);
  855. else
  856. _dl_write(fd, string, _dl_strlen(string));
  857. break;
  858. case 'i':
  859. case 'd':
  860. {
  861. char tmp[22];
  862. #if __WORDSIZE > 32
  863. num = va_arg(args, long int);
  864. #else
  865. num = va_arg(args, int);
  866. #endif
  867. string = _dl_simple_ltoa(tmp, num);
  868. _dl_write(fd, string, _dl_strlen(string));
  869. break;
  870. }
  871. case 'x':
  872. case 'X':
  873. {
  874. char tmp[22];
  875. #if __WORDSIZE > 32
  876. num = va_arg(args, long int);
  877. #else
  878. num = va_arg(args, int);
  879. #endif
  880. string = _dl_simple_ltoahex(tmp, num);
  881. _dl_write(fd, string, _dl_strlen(string));
  882. break;
  883. }
  884. default:
  885. _dl_write(fd, "(null)", 6);
  886. break;
  887. }
  888. start = ptr;
  889. } else {
  890. _dl_write(fd, start, _dl_strlen(start));
  891. start = NULL;
  892. }
  893. }
  894. _dl_munmap(buf, _dl_pagesize);
  895. return;
  896. }
  897. char *_dl_strdup(const char *string)
  898. {
  899. char *retval;
  900. int len;
  901. len = _dl_strlen(string);
  902. retval = _dl_malloc(len + 1);
  903. _dl_strcpy(retval, string);
  904. return retval;
  905. }
  906. unsigned int _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[],
  907. void *debug_addr, DL_LOADADDR_TYPE load_off)
  908. {
  909. return __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);
  910. }