readelflib1.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /* vi: set sw=4 ts=4: */
  2. /* Program to load an ELF binary on a linux system, and run it
  3. * after resolving ELF shared library symbols
  4. *
  5. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  6. * David Engel, Hongjiu Lu and Mitch D'Souza
  7. * Copyright (C) 2001-2002, Erik Andersen
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. The name of the above contributors may not be
  17. * used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. /* This file contains the helper routines to load an ELF sharable
  33. library into memory and add the symbol table info to the chain. */
  34. #ifdef USE_CACHE
  35. static caddr_t _dl_cache_addr = NULL;
  36. static size_t _dl_cache_size = 0;
  37. int _dl_map_cache(void)
  38. {
  39. int fd;
  40. struct stat st;
  41. header_t *header;
  42. libentry_t *libent;
  43. int i, strtabsize;
  44. if (_dl_cache_addr == (caddr_t) - 1)
  45. return -1;
  46. else if (_dl_cache_addr != NULL)
  47. return 0;
  48. if (_dl_stat(LDSO_CACHE, &st)
  49. || (fd = _dl_open(LDSO_CACHE, O_RDONLY)) < 0) {
  50. _dl_dprintf(2, "%s: can't open cache '%s'\n", _dl_progname, LDSO_CACHE);
  51. _dl_cache_addr = (caddr_t) - 1; /* so we won't try again */
  52. return -1;
  53. }
  54. _dl_cache_size = st.st_size;
  55. _dl_cache_addr = (caddr_t) _dl_mmap(0, _dl_cache_size, PROT_READ, MAP_SHARED, fd, 0);
  56. _dl_close(fd);
  57. if (_dl_cache_addr == (caddr_t) - 1) {
  58. _dl_dprintf(2, "%s: can't map cache '%s'\n",
  59. _dl_progname, LDSO_CACHE);
  60. return -1;
  61. }
  62. header = (header_t *) _dl_cache_addr;
  63. if (_dl_cache_size < sizeof(header_t) ||
  64. _dl_memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
  65. || _dl_memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
  66. || _dl_cache_size <
  67. (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
  68. || _dl_cache_addr[_dl_cache_size - 1] != '\0')
  69. {
  70. _dl_dprintf(2, "%s: cache '%s' is corrupt\n", _dl_progname,
  71. LDSO_CACHE);
  72. goto fail;
  73. }
  74. strtabsize = _dl_cache_size - sizeof(header_t) -
  75. header->nlibs * sizeof(libentry_t);
  76. libent = (libentry_t *) & header[1];
  77. for (i = 0; i < header->nlibs; i++) {
  78. if (libent[i].sooffset >= strtabsize ||
  79. libent[i].liboffset >= strtabsize)
  80. {
  81. _dl_dprintf(2, "%s: cache '%s' is corrupt\n", _dl_progname, LDSO_CACHE);
  82. goto fail;
  83. }
  84. }
  85. return 0;
  86. fail:
  87. _dl_munmap(_dl_cache_addr, _dl_cache_size);
  88. _dl_cache_addr = (caddr_t) - 1;
  89. return -1;
  90. }
  91. int _dl_unmap_cache(void)
  92. {
  93. if (_dl_cache_addr == NULL || _dl_cache_addr == (caddr_t) - 1)
  94. return -1;
  95. #if 1
  96. _dl_munmap(_dl_cache_addr, _dl_cache_size);
  97. _dl_cache_addr = NULL;
  98. #endif
  99. return 0;
  100. }
  101. #endif
  102. /* This function's behavior must exactly match that
  103. * in uClibc/ldso/util/ldd.c */
  104. static struct elf_resolve *
  105. search_for_named_library(char *name, int secure, const char *path_list,
  106. struct dyn_elf **rpnt)
  107. {
  108. int i, count = 1;
  109. char *path, *path_n;
  110. char mylibname[2050];
  111. struct elf_resolve *tpnt1;
  112. /* We need a writable copy of this string */
  113. path = _dl_strdup(path_list);
  114. if (!path) {
  115. _dl_dprintf(2, "Out of memory!\n");
  116. _dl_exit(0);
  117. }
  118. /* Unlike ldd.c, don't bother to eliminate double //s */
  119. /* Replace colons with zeros in path_list and count them */
  120. for(i=_dl_strlen(path); i > 0; i--) {
  121. if (path[i]==':') {
  122. path[i]=0;
  123. count++;
  124. }
  125. }
  126. path_n = path;
  127. for (i = 0; i < count; i++) {
  128. _dl_strcpy(mylibname, path_n);
  129. _dl_strcat(mylibname, "/");
  130. _dl_strcat(mylibname, name);
  131. if ((tpnt1 = _dl_load_elf_shared_library(secure, rpnt,
  132. mylibname)) != NULL)
  133. {
  134. return tpnt1;
  135. }
  136. path_n += (_dl_strlen(path_n) + 1);
  137. }
  138. return NULL;
  139. }
  140. /*
  141. * Used to return error codes back to dlopen et. al.
  142. */
  143. unsigned long _dl_error_number;
  144. unsigned long _dl_internal_error_number;
  145. extern char *_dl_ldsopath;
  146. struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
  147. struct elf_resolve *tpnt, char *full_libname)
  148. {
  149. char *pnt;
  150. struct elf_resolve *tpnt1;
  151. char *libname;
  152. _dl_internal_error_number = 0;
  153. pnt = libname = full_libname;
  154. /* quick hack to ensure mylibname buffer doesn't overflow. don't
  155. allow full_libname or any directory to be longer than 1024. */
  156. if (_dl_strlen(full_libname) > 1024)
  157. goto goof;
  158. while (*pnt) {
  159. if (*pnt == '/')
  160. libname = pnt + 1;
  161. pnt++;
  162. }
  163. #if defined (__SUPPORT_LD_DEBUG__)
  164. if(_dl_debug) _dl_dprintf(_dl_debug_file, "searching for library: '%s'\n", libname);
  165. #endif
  166. /* If the filename has any '/', try it straight and leave it at that.
  167. For IBCS2 compatibility under linux, we substitute the string
  168. /usr/i486-sysv4/lib for /usr/lib in library names. */
  169. if (libname != full_libname) {
  170. tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
  171. if (tpnt1)
  172. return tpnt1;
  173. goto goof;
  174. }
  175. /*
  176. * The ABI specifies that RPATH is searched before LD_*_PATH or
  177. * the default path of /usr/lib. Check in rpath directories.
  178. */
  179. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  180. if (tpnt->libtype == elf_executable) {
  181. pnt = (char *) tpnt->dynamic_info[DT_RPATH];
  182. if (pnt) {
  183. pnt += (unsigned long) tpnt->loadaddr +
  184. tpnt->dynamic_info[DT_STRTAB];
  185. #if defined (__SUPPORT_LD_DEBUG__)
  186. if(_dl_debug) _dl_dprintf(_dl_debug_file, "searching RPATH: '%s'\n", pnt);
  187. #endif
  188. if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
  189. {
  190. return tpnt1;
  191. }
  192. }
  193. }
  194. }
  195. /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
  196. if (_dl_library_path) {
  197. #if defined (__SUPPORT_LD_DEBUG__)
  198. if(_dl_debug) _dl_dprintf(_dl_debug_file, "searching _dl_library_path: '%s'\n", _dl_library_path);
  199. #endif
  200. if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
  201. {
  202. return tpnt1;
  203. }
  204. }
  205. /*
  206. * Where should the cache be searched? There is no such concept in the
  207. * ABI, so we have some flexibility here. For now, search it before
  208. * the hard coded paths that follow (i.e before /lib and /usr/lib).
  209. */
  210. #ifdef USE_CACHE
  211. if (_dl_cache_addr != NULL && _dl_cache_addr != (caddr_t) - 1) {
  212. int i;
  213. header_t *header = (header_t *) _dl_cache_addr;
  214. libentry_t *libent = (libentry_t *) & header[1];
  215. char *strs = (char *) &libent[header->nlibs];
  216. for (i = 0; i < header->nlibs; i++) {
  217. if ((libent[i].flags == LIB_ELF ||
  218. libent[i].flags == LIB_ELF_LIBC5) &&
  219. _dl_strcmp(libname, strs + libent[i].sooffset) == 0 &&
  220. (tpnt1 = _dl_load_elf_shared_library(secure,
  221. rpnt, strs + libent[i].liboffset)))
  222. return tpnt1;
  223. }
  224. }
  225. #endif
  226. /* Look for libraries wherever the shared library loader
  227. * was installed */
  228. #if defined (__SUPPORT_LD_DEBUG__)
  229. if(_dl_debug) _dl_dprintf(_dl_debug_file, "searching in ldso dir: %s\n", _dl_ldsopath);
  230. #endif
  231. if ((tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt)) != NULL)
  232. {
  233. return tpnt1;
  234. }
  235. /* Lastly, search the standard list of paths for the library.
  236. This list must exactly match the list in uClibc/ldso/util/ldd.c */
  237. #if defined (__SUPPORT_LD_DEBUG__)
  238. if(_dl_debug) _dl_dprintf(_dl_debug_file, "searching full lib path list\n");
  239. #endif
  240. if ((tpnt1 = search_for_named_library(libname, secure,
  241. UCLIBC_TARGET_PREFIX "/usr/lib:"
  242. UCLIBC_TARGET_PREFIX "/lib:"
  243. UCLIBC_DEVEL_PREFIX "/lib:"
  244. UCLIBC_BUILD_DIR "/lib:"
  245. "/usr/lib:"
  246. "/lib", rpnt)
  247. ) != NULL)
  248. {
  249. return tpnt1;
  250. }
  251. goof:
  252. /* Well, we shot our wad on that one. All we can do now is punt */
  253. if (_dl_internal_error_number)
  254. _dl_error_number = _dl_internal_error_number;
  255. else
  256. _dl_error_number = LD_ERROR_NOFILE;
  257. #if defined (__SUPPORT_LD_DEBUG__)
  258. if(_dl_debug) _dl_dprintf(2, "Bummer: could not find '%s'!\n", libname);
  259. #endif
  260. return NULL;
  261. }
  262. /*
  263. * Read one ELF library into memory, mmap it into the correct locations and
  264. * add the symbol info to the symbol chain. Perform any relocations that
  265. * are required.
  266. */
  267. struct elf_resolve *_dl_load_elf_shared_library(int secure,
  268. struct dyn_elf **rpnt, char *libname)
  269. {
  270. elfhdr *epnt;
  271. unsigned long dynamic_addr = 0;
  272. unsigned long dynamic_size = 0;
  273. Elf32_Dyn *dpnt;
  274. struct elf_resolve *tpnt;
  275. elf_phdr *ppnt;
  276. char *status;
  277. char header[4096];
  278. unsigned long dynamic_info[24];
  279. unsigned long *lpnt;
  280. unsigned long libaddr;
  281. unsigned long minvma = 0xffffffff, maxvma = 0;
  282. int i, flags, piclib, infile;
  283. /* If this file is already loaded, skip this step */
  284. tpnt = _dl_check_hashed_files(libname);
  285. if (tpnt) {
  286. if (*rpnt) {
  287. (*rpnt)->next = (struct dyn_elf *)
  288. _dl_malloc(sizeof(struct dyn_elf));
  289. _dl_memset((*rpnt)->next, 0, sizeof(*((*rpnt)->next)));
  290. (*rpnt)->next->prev = (*rpnt);
  291. *rpnt = (*rpnt)->next;
  292. (*rpnt)->dyn = tpnt;
  293. tpnt->symbol_scope = _dl_symbol_tables;
  294. }
  295. tpnt->usage_count++;
  296. tpnt->libtype = elf_lib;
  297. return tpnt;
  298. }
  299. /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
  300. we don't load the library if it isn't setuid. */
  301. if (secure) {
  302. struct stat st;
  303. if (_dl_stat(libname, &st) || !(st.st_mode & S_ISUID))
  304. return NULL;
  305. }
  306. libaddr = 0;
  307. infile = _dl_open(libname, O_RDONLY);
  308. if (infile < 0) {
  309. #if 0
  310. /*
  311. * NO! When we open shared libraries we may search several paths.
  312. * it is inappropriate to generate an error here.
  313. */
  314. _dl_dprintf(2, "%s: can't open '%s'\n", _dl_progname, libname);
  315. #endif
  316. _dl_internal_error_number = LD_ERROR_NOFILE;
  317. return NULL;
  318. }
  319. _dl_read(infile, header, sizeof(header));
  320. epnt = (elfhdr *) (intptr_t) header;
  321. if (epnt->e_ident[0] != 0x7f ||
  322. epnt->e_ident[1] != 'E' ||
  323. epnt->e_ident[2] != 'L' ||
  324. epnt->e_ident[3] != 'F')
  325. {
  326. _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  327. libname);
  328. _dl_internal_error_number = LD_ERROR_NOTELF;
  329. _dl_close(infile);
  330. return NULL;
  331. };
  332. if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
  333. #ifdef MAGIC2
  334. && epnt->e_machine != MAGIC2
  335. #endif
  336. ))
  337. {
  338. _dl_internal_error_number =
  339. (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
  340. _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  341. "\n", _dl_progname, libname);
  342. _dl_close(infile);
  343. return NULL;
  344. };
  345. ppnt = (elf_phdr *)(intptr_t) & header[epnt->e_phoff];
  346. piclib = 1;
  347. for (i = 0; i < epnt->e_phnum; i++) {
  348. if (ppnt->p_type == PT_DYNAMIC) {
  349. if (dynamic_addr)
  350. _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
  351. _dl_progname, libname);
  352. dynamic_addr = ppnt->p_vaddr;
  353. dynamic_size = ppnt->p_filesz;
  354. };
  355. if (ppnt->p_type == PT_LOAD) {
  356. /* See if this is a PIC library. */
  357. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  358. piclib = 0;
  359. minvma = ppnt->p_vaddr;
  360. }
  361. if (piclib && ppnt->p_vaddr < minvma) {
  362. minvma = ppnt->p_vaddr;
  363. }
  364. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  365. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  366. }
  367. }
  368. ppnt++;
  369. };
  370. maxvma = (maxvma + ADDR_ALIGN) & ~ADDR_ALIGN;
  371. minvma = minvma & ~0xffffU;
  372. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  373. if (!piclib)
  374. flags |= MAP_FIXED;
  375. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  376. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  377. if (_dl_mmap_check_error(status)) {
  378. _dl_dprintf(2, "%s: can't map %s\n", _dl_progname, libname);
  379. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  380. _dl_close(infile);
  381. return NULL;
  382. };
  383. libaddr = (unsigned long) status;
  384. flags |= MAP_FIXED;
  385. /* Get the memory to store the library */
  386. ppnt = (elf_phdr *)(intptr_t) & header[epnt->e_phoff];
  387. for (i = 0; i < epnt->e_phnum; i++) {
  388. if (ppnt->p_type == PT_LOAD) {
  389. /* See if this is a PIC library. */
  390. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  391. piclib = 0;
  392. /* flags |= MAP_FIXED; */
  393. }
  394. if (ppnt->p_flags & PF_W) {
  395. unsigned long map_size;
  396. char *cpnt;
  397. status = (char *) _dl_mmap((char *) ((piclib ? libaddr : 0) +
  398. (ppnt->p_vaddr & PAGE_ALIGN)), (ppnt->p_vaddr & ADDR_ALIGN)
  399. + ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags, infile,
  400. ppnt->p_offset & OFFS_ALIGN);
  401. if (_dl_mmap_check_error(status)) {
  402. _dl_dprintf(2, "%s: can't map '%s'\n",
  403. _dl_progname, libname);
  404. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  405. _dl_munmap((char *) libaddr, maxvma - minvma);
  406. _dl_close(infile);
  407. return NULL;
  408. };
  409. /* Pad the last page with zeroes. */
  410. cpnt = (char *) (status + (ppnt->p_vaddr & ADDR_ALIGN) +
  411. ppnt->p_filesz);
  412. while (((unsigned long) cpnt) & ADDR_ALIGN)
  413. *cpnt++ = 0;
  414. /* I am not quite sure if this is completely
  415. * correct to do or not, but the basic way that
  416. * we handle bss segments is that we mmap
  417. * /dev/zero if there are any pages left over
  418. * that are not mapped as part of the file */
  419. map_size = (ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) & PAGE_ALIGN;
  420. if (map_size < ppnt->p_vaddr + ppnt->p_memsz)
  421. status = (char *) _dl_mmap((char *) map_size +
  422. (piclib ? libaddr : 0),
  423. ppnt->p_vaddr + ppnt->p_memsz - map_size,
  424. LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS, -1, 0);
  425. } else
  426. status = (char *) _dl_mmap((char *) (ppnt->p_vaddr & PAGE_ALIGN)
  427. + (piclib ? libaddr : 0), (ppnt->p_vaddr & ADDR_ALIGN) +
  428. ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags,
  429. infile, ppnt->p_offset & OFFS_ALIGN);
  430. if (_dl_mmap_check_error(status)) {
  431. _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
  432. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  433. _dl_munmap((char *) libaddr, maxvma - minvma);
  434. _dl_close(infile);
  435. return NULL;
  436. };
  437. /* if(libaddr == 0 && piclib) {
  438. libaddr = (unsigned long) status;
  439. flags |= MAP_FIXED;
  440. }; */
  441. };
  442. ppnt++;
  443. };
  444. _dl_close(infile);
  445. /* For a non-PIC library, the addresses are all absolute */
  446. if (piclib) {
  447. dynamic_addr += (unsigned long) libaddr;
  448. }
  449. /*
  450. * OK, the ELF library is now loaded into VM in the correct locations
  451. * The next step is to go through and do the dynamic linking (if needed).
  452. */
  453. /* Start by scanning the dynamic section to get all of the pointers */
  454. if (!dynamic_addr) {
  455. _dl_internal_error_number = LD_ERROR_NODYNAMIC;
  456. _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
  457. _dl_progname, libname);
  458. return NULL;
  459. }
  460. dpnt = (Elf32_Dyn *) dynamic_addr;
  461. dynamic_size = dynamic_size / sizeof(Elf32_Dyn);
  462. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  463. #if defined(__mips__)
  464. {
  465. int indx = 1;
  466. Elf32_Dyn *dpnt = (Elf32_Dyn *) dynamic_addr;
  467. while(dpnt->d_tag) {
  468. dpnt++;
  469. indx++;
  470. }
  471. dynamic_size = indx;
  472. }
  473. #endif
  474. {
  475. unsigned long indx;
  476. for (indx = 0; indx < dynamic_size; indx++)
  477. {
  478. if (dpnt->d_tag > DT_JMPREL) {
  479. dpnt++;
  480. continue;
  481. }
  482. dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  483. if (dpnt->d_tag == DT_TEXTREL || SVR4_BUGCOMPAT)
  484. dynamic_info[DT_TEXTREL] = 1;
  485. dpnt++;
  486. };
  487. }
  488. /* If the TEXTREL is set, this means that we need to make the pages
  489. writable before we perform relocations. Do this now. They get set
  490. back again later. */
  491. if (dynamic_info[DT_TEXTREL]) {
  492. #ifdef FORCE_SHAREABLE_TEXT_SEGMENTS
  493. ppnt = (elf_phdr *)(intptr_t) & header[epnt->e_phoff];
  494. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  495. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  496. _dl_mprotect((void *) ((piclib ? libaddr : 0) +
  497. (ppnt->p_vaddr & PAGE_ALIGN)),
  498. (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
  499. PROT_READ | PROT_WRITE | PROT_EXEC);
  500. }
  501. #else
  502. _dl_dprintf(_dl_debug_file, "Can't modify %s's text section. Use GCC option -fPIC for shared objects, please.\n",libname);
  503. _dl_exit(1);
  504. #endif
  505. }
  506. tpnt = _dl_add_elf_hash_table(libname, (char *) libaddr, dynamic_info,
  507. dynamic_addr, dynamic_size);
  508. tpnt->ppnt = (elf_phdr *)(intptr_t) (tpnt->loadaddr + epnt->e_phoff);
  509. tpnt->n_phent = epnt->e_phnum;
  510. /*
  511. * Add this object into the symbol chain
  512. */
  513. if (*rpnt) {
  514. (*rpnt)->next = (struct dyn_elf *)
  515. _dl_malloc(sizeof(struct dyn_elf));
  516. _dl_memset((*rpnt)->next, 0, sizeof(*((*rpnt)->next)));
  517. (*rpnt)->next->prev = (*rpnt);
  518. *rpnt = (*rpnt)->next;
  519. (*rpnt)->dyn = tpnt;
  520. tpnt->symbol_scope = _dl_symbol_tables;
  521. }
  522. tpnt->usage_count++;
  523. tpnt->libtype = elf_lib;
  524. /*
  525. * OK, the next thing we need to do is to insert the dynamic linker into
  526. * the proper entry in the GOT so that the PLT symbols can be properly
  527. * resolved.
  528. */
  529. lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
  530. if (lpnt) {
  531. lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT] +
  532. ((int) libaddr));
  533. INIT_GOT(lpnt, tpnt);
  534. };
  535. return tpnt;
  536. }
  537. /* Ugly, ugly. Some versions of the SVr4 linker fail to generate COPY
  538. relocations for global variables that are present both in the image and
  539. the shared library. Go through and do it manually. If the images
  540. are guaranteed to be generated by a trustworthy linker, then this
  541. step can be skipped. */
  542. int _dl_copy_fixups(struct dyn_elf *rpnt)
  543. {
  544. int goof = 0;
  545. struct elf_resolve *tpnt;
  546. if (rpnt->next)
  547. goof += _dl_copy_fixups(rpnt->next);
  548. else
  549. return 0;
  550. tpnt = rpnt->dyn;
  551. if (tpnt->init_flag & COPY_RELOCS_DONE)
  552. return goof;
  553. tpnt->init_flag |= COPY_RELOCS_DONE;
  554. #if defined (__SUPPORT_LD_DEBUG__)
  555. if(_dl_debug) _dl_dprintf(_dl_debug_file,"\nrelocation copy fixups: %s", tpnt->libname);
  556. #endif
  557. #ifdef ELF_USES_RELOCA
  558. goof += _dl_parse_copy_information(rpnt,
  559. tpnt->dynamic_info[DT_RELA], tpnt->dynamic_info[DT_RELASZ], 0);
  560. #else
  561. goof += _dl_parse_copy_information(rpnt, tpnt->dynamic_info[DT_REL],
  562. tpnt->dynamic_info[DT_RELSZ], 0);
  563. #endif
  564. #if defined (__SUPPORT_LD_DEBUG__)
  565. if(_dl_debug) _dl_dprintf(_dl_debug_file,"\nrelocation copy fixups: %s; finished\n\n", tpnt->libname);
  566. #endif
  567. return goof;
  568. }