OSDN Git Service

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