OSDN Git Service

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