OSDN Git Service

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