OSDN Git Service

Move _dl_printf and _dl_malloc to ldso, which is a more sensible
[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) {
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
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
265 #if defined (__SUPPORT_LD_DEBUG__)
266         if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tfind library='%s'; searching\n", libname);
267 #endif
268         /* If the filename has any '/', try it straight and leave it at that.
269            For IBCS2 compatibility under linux, we substitute the string
270            /usr/i486-sysv4/lib for /usr/lib in library names. */
271
272         if (libname != full_libname) {
273 #if defined (__SUPPORT_LD_DEBUG__)
274                 if(_dl_debug) _dl_dprintf(_dl_debug_file, "\ttrying file='%s'\n", full_libname);
275 #endif
276                 tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
277                 if (tpnt1) {
278                         return tpnt1;
279                 }
280                 //goto goof;
281         }
282
283         /*
284          * The ABI specifies that RPATH is searched before LD_*_PATH or
285          * the default path of /usr/lib.  Check in rpath directories.
286          */
287         for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
288                 if (tpnt->libtype == elf_executable) {
289                         pnt = (char *) tpnt->dynamic_info[DT_RPATH];
290                         if (pnt) {
291                                 pnt += (unsigned long) tpnt->loadaddr + tpnt->dynamic_info[DT_STRTAB];
292 #if defined (__SUPPORT_LD_DEBUG__)
293                                 if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching RPATH='%s'\n", pnt);
294 #endif
295                                 if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
296                                 {
297                                         return tpnt1;
298                                 }
299                         }
300                 }
301         }
302
303         /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
304         if (_dl_library_path) {
305 #if defined (__SUPPORT_LD_DEBUG__)
306                 if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
307 #endif
308                 if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
309                 {
310                         return tpnt1;
311                 }
312         }
313
314         /*
315          * Where should the cache be searched?  There is no such concept in the
316          * ABI, so we have some flexibility here.  For now, search it before
317          * the hard coded paths that follow (i.e before /lib and /usr/lib).
318          */
319 #ifdef USE_CACHE
320         if (_dl_cache_addr != NULL && _dl_cache_addr != (caddr_t) - 1) {
321                 int i;
322                 header_t *header = (header_t *) _dl_cache_addr;
323                 libentry_t *libent = (libentry_t *) & header[1];
324                 char *strs = (char *) &libent[header->nlibs];
325
326 #if defined (__SUPPORT_LD_DEBUG__)
327                 if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching cache='%s'\n", LDSO_CACHE);
328 #endif
329                 for (i = 0; i < header->nlibs; i++) {
330                         if ((libent[i].flags == LIB_ELF ||
331                                                 libent[i].flags == LIB_ELF_LIBC5) &&
332                                         _dl_strcmp(libname, strs + libent[i].sooffset) == 0 &&
333                                         (tpnt1 = _dl_load_elf_shared_library(secure,
334                                                                                                                  rpnt, strs + libent[i].liboffset)))
335                                 return tpnt1;
336                 }
337         }
338 #endif
339
340         /* Look for libraries wherever the shared library loader
341          * was installed */
342 #if defined (__SUPPORT_LD_DEBUG__)
343         if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching ldso dir='%s'\n", _dl_ldsopath);
344 #endif
345         if ((tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt)) != NULL)
346         {
347                 return tpnt1;
348         }
349
350
351         /* Lastly, search the standard list of paths for the library.
352            This list must exactly match the list in uClibc/ldso/util/ldd.c */
353 #if defined (__SUPPORT_LD_DEBUG__)
354         if(_dl_debug) _dl_dprintf(_dl_debug_file, "\tsearching full lib path list\n");
355 #endif
356         if ((tpnt1 = search_for_named_library(libname, secure,
357                                         UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib:"
358                                         UCLIBC_RUNTIME_PREFIX "usr/lib:"
359                                         UCLIBC_RUNTIME_PREFIX "lib:"
360                                         "/usr/lib:"
361                                         "/lib", rpnt)
362                 ) != NULL)
363         {
364                 return tpnt1;
365         }
366
367 goof:
368         /* Well, we shot our wad on that one.  All we can do now is punt */
369         if (_dl_internal_error_number)
370                 _dl_error_number = _dl_internal_error_number;
371         else
372                 _dl_error_number = LD_ERROR_NOFILE;
373 #if defined (__SUPPORT_LD_DEBUG__)
374         if(_dl_debug) _dl_dprintf(2, "Bummer: could not find '%s'!\n", libname);
375 #endif
376         return NULL;
377 }
378
379
380 /*
381  * Read one ELF library into memory, mmap it into the correct locations and
382  * add the symbol info to the symbol chain.  Perform any relocations that
383  * are required.
384  */
385
386 struct elf_resolve *_dl_load_elf_shared_library(int secure,
387         struct dyn_elf **rpnt, char *libname)
388 {
389         ElfW(Ehdr) *epnt;
390         unsigned long dynamic_addr = 0;
391         unsigned long dynamic_size = 0;
392         Elf32_Dyn *dpnt;
393         struct elf_resolve *tpnt;
394         ElfW(Phdr) *ppnt;
395         char *status, *header;
396         unsigned long dynamic_info[24];
397         unsigned long *lpnt;
398         unsigned long libaddr;
399         unsigned long minvma = 0xffffffff, maxvma = 0;
400         int i, flags, piclib, infile;
401
402         /* If this file is already loaded, skip this step */
403         tpnt = _dl_check_hashed_files(libname);
404         if (tpnt) {
405                 if (*rpnt) {
406                         (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
407                         _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
408                         (*rpnt)->next->prev = (*rpnt);
409                         *rpnt = (*rpnt)->next;
410                         (*rpnt)->dyn = tpnt;
411                         tpnt->symbol_scope = _dl_symbol_tables;
412                 }
413                 tpnt->usage_count++;
414                 tpnt->libtype = elf_lib;
415 #if defined (__SUPPORT_LD_DEBUG__)
416                 if(_dl_debug) _dl_dprintf(2, "file='%s';  already loaded\n", libname);
417 #endif
418                 return tpnt;
419         }
420
421         /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
422            we don't load the library if it isn't setuid. */
423
424         if (secure) {
425                 struct stat st;
426
427                 if (_dl_stat(libname, &st) || !(st.st_mode & S_ISUID))
428                         return NULL;
429         }
430
431         libaddr = 0;
432         infile = _dl_open(libname, O_RDONLY);
433         if (infile < 0) {
434 #if 0
435                 /*
436                  * NO!  When we open shared libraries we may search several paths.
437                  * it is inappropriate to generate an error here.
438                  */
439                 _dl_dprintf(2, "%s: can't open '%s'\n", _dl_progname, libname);
440 #endif
441                 _dl_internal_error_number = LD_ERROR_NOFILE;
442                 return NULL;
443         }
444
445         header = _dl_mmap((void *) 0, PAGE_SIZE, PROT_READ | PROT_WRITE,
446                         MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
447         if (_dl_mmap_check_error(header)) {
448                 _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
449                 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
450                 _dl_close(infile);
451                 return NULL;
452         };
453
454         _dl_read(infile, header, PAGE_SIZE);
455         epnt = (ElfW(Ehdr) *) (intptr_t) header;
456         if (epnt->e_ident[0] != 0x7f ||
457                         epnt->e_ident[1] != 'E' ||
458                         epnt->e_ident[2] != 'L' ||
459                         epnt->e_ident[3] != 'F')
460         {
461                 _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
462                                 libname);
463                 _dl_internal_error_number = LD_ERROR_NOTELF;
464                 _dl_close(infile);
465                 _dl_munmap(header, PAGE_SIZE);
466                 return NULL;
467         };
468
469         if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
470 #ifdef MAGIC2
471                                 && epnt->e_machine != MAGIC2
472 #endif
473                                 ))
474         {
475                 _dl_internal_error_number =
476                         (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
477                 _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
478                                 "\n", _dl_progname, libname);
479                 _dl_close(infile);
480                 _dl_munmap(header, PAGE_SIZE);
481                 return NULL;
482         };
483
484         ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
485
486         piclib = 1;
487         for (i = 0; i < epnt->e_phnum; i++) {
488
489                 if (ppnt->p_type == PT_DYNAMIC) {
490                         if (dynamic_addr)
491                                 _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
492                                                 _dl_progname, libname);
493                         dynamic_addr = ppnt->p_vaddr;
494                         dynamic_size = ppnt->p_filesz;
495                 };
496
497                 if (ppnt->p_type == PT_LOAD) {
498                         /* See if this is a PIC library. */
499                         if (i == 0 && ppnt->p_vaddr > 0x1000000) {
500                                 piclib = 0;
501                                 minvma = ppnt->p_vaddr;
502                         }
503                         if (piclib && ppnt->p_vaddr < minvma) {
504                                 minvma = ppnt->p_vaddr;
505                         }
506                         if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
507                                 maxvma = ppnt->p_vaddr + ppnt->p_memsz;
508                         }
509                 }
510                 ppnt++;
511         };
512
513         maxvma = (maxvma + ADDR_ALIGN) & ~ADDR_ALIGN;
514         minvma = minvma & ~0xffffU;
515
516         flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
517         if (!piclib)
518                 flags |= MAP_FIXED;
519
520         status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
521                         maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
522         if (_dl_mmap_check_error(status)) {
523                 _dl_dprintf(2, "%s: can't map %s\n", _dl_progname, libname);
524                 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
525                 _dl_close(infile);
526                 _dl_munmap(header, PAGE_SIZE);
527                 return NULL;
528         };
529         libaddr = (unsigned long) status;
530         flags |= MAP_FIXED;
531
532         /* Get the memory to store the library */
533         ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
534
535         for (i = 0; i < epnt->e_phnum; i++) {
536                 if (ppnt->p_type == PT_LOAD) {
537
538                         /* See if this is a PIC library. */
539                         if (i == 0 && ppnt->p_vaddr > 0x1000000) {
540                                 piclib = 0;
541                                 /* flags |= MAP_FIXED; */
542                         }
543
544
545
546                         if (ppnt->p_flags & PF_W) {
547                                 unsigned long map_size;
548                                 char *cpnt;
549
550                                 status = (char *) _dl_mmap((char *) ((piclib ? libaddr : 0) +
551                                                         (ppnt->p_vaddr & PAGE_ALIGN)), (ppnt->p_vaddr & ADDR_ALIGN)
552                                                 + ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags, infile,
553                                                 ppnt->p_offset & OFFS_ALIGN);
554
555                                 if (_dl_mmap_check_error(status)) {
556                                         _dl_dprintf(2, "%s: can't map '%s'\n",
557                                                         _dl_progname, libname);
558                                         _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
559                                         _dl_munmap((char *) libaddr, maxvma - minvma);
560                                         _dl_close(infile);
561                                         _dl_munmap(header, PAGE_SIZE);
562                                         return NULL;
563                                 };
564
565                                 /* Pad the last page with zeroes. */
566                                 cpnt = (char *) (status + (ppnt->p_vaddr & ADDR_ALIGN) +
567                                                 ppnt->p_filesz);
568                                 while (((unsigned long) cpnt) & ADDR_ALIGN)
569                                         *cpnt++ = 0;
570
571                                 /* I am not quite sure if this is completely
572                                  * correct to do or not, but the basic way that
573                                  * we handle bss segments is that we mmap
574                                  * /dev/zero if there are any pages left over
575                                  * that are not mapped as part of the file */
576
577                                 map_size = (ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) & PAGE_ALIGN;
578
579                                 if (map_size < ppnt->p_vaddr + ppnt->p_memsz)
580                                         status = (char *) _dl_mmap((char *) map_size +
581                                                         (piclib ? libaddr : 0),
582                                                         ppnt->p_vaddr + ppnt->p_memsz - map_size,
583                                                         LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS, -1, 0);
584                         } else
585                                 status = (char *) _dl_mmap((char *) (ppnt->p_vaddr & PAGE_ALIGN)
586                                                 + (piclib ? libaddr : 0), (ppnt->p_vaddr & ADDR_ALIGN) +
587                                                 ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags,
588                                                 infile, ppnt->p_offset & OFFS_ALIGN);
589                         if (_dl_mmap_check_error(status)) {
590                                 _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
591                                 _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
592                                 _dl_munmap((char *) libaddr, maxvma - minvma);
593                                 _dl_close(infile);
594                                 _dl_munmap(header, PAGE_SIZE);
595                                 return NULL;
596                         };
597
598                         /* if(libaddr == 0 && piclib) {
599                            libaddr = (unsigned long) status;
600                            flags |= MAP_FIXED;
601                            }; */
602                 };
603                 ppnt++;
604         };
605         _dl_close(infile);
606
607         /* For a non-PIC library, the addresses are all absolute */
608         if (piclib) {
609                 dynamic_addr += (unsigned long) libaddr;
610         }
611
612         /*
613          * OK, the ELF library is now loaded into VM in the correct locations
614          * The next step is to go through and do the dynamic linking (if needed).
615          */
616
617         /* Start by scanning the dynamic section to get all of the pointers */
618
619         if (!dynamic_addr) {
620                 _dl_internal_error_number = LD_ERROR_NODYNAMIC;
621                 _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
622                                 _dl_progname, libname);
623                 _dl_munmap(header, PAGE_SIZE);
624                 return NULL;
625         }
626
627         dpnt = (Elf32_Dyn *) dynamic_addr;
628
629         dynamic_size = dynamic_size / sizeof(Elf32_Dyn);
630         _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
631
632 #if defined(__mips__)
633         {
634
635                 int indx = 1;
636                 Elf32_Dyn *dpnt = (Elf32_Dyn *) dynamic_addr;
637
638                 while(dpnt->d_tag) {
639                         dpnt++;
640                         indx++;
641                 }
642                 dynamic_size = indx;
643         }
644 #endif
645
646         {
647                 unsigned long indx;
648
649                 for (indx = 0; indx < dynamic_size; indx++)
650                 {
651                         if (dpnt->d_tag > DT_JMPREL) {
652                                 dpnt++;
653                                 continue;
654                         }
655                         dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
656                         if (dpnt->d_tag == DT_TEXTREL)
657                                 dynamic_info[DT_TEXTREL] = 1;
658                         dpnt++;
659                 };
660         }
661
662         /* If the TEXTREL is set, this means that we need to make the pages
663            writable before we perform relocations.  Do this now. They get set
664            back again later. */
665
666         if (dynamic_info[DT_TEXTREL]) {
667 #ifndef FORCE_SHAREABLE_TEXT_SEGMENTS
668                 ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
669                 for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
670                         if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
671                                 _dl_mprotect((void *) ((piclib ? libaddr : 0) +
672                                                         (ppnt->p_vaddr & PAGE_ALIGN)),
673                                                 (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
674                                                 PROT_READ | PROT_WRITE | PROT_EXEC);
675                 }
676 #else
677                 _dl_dprintf(_dl_debug_file, "Can't modify %s's text section. Use GCC option -fPIC for shared objects, please.\n",libname);
678                 _dl_exit(1);
679 #endif
680         }
681
682         tpnt = _dl_add_elf_hash_table(libname, (char *) libaddr, dynamic_info,
683                         dynamic_addr, dynamic_size);
684
685         tpnt->ppnt = (ElfW(Phdr) *)(intptr_t) (tpnt->loadaddr + epnt->e_phoff);
686         tpnt->n_phent = epnt->e_phnum;
687
688         /*
689          * Add this object into the symbol chain
690          */
691         if (*rpnt) {
692                 (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
693                 _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
694                 (*rpnt)->next->prev = (*rpnt);
695                 *rpnt = (*rpnt)->next;
696                 (*rpnt)->dyn = tpnt;
697                 tpnt->symbol_scope = _dl_symbol_tables;
698         }
699         tpnt->usage_count++;
700         tpnt->libtype = elf_lib;
701
702         /*
703          * OK, the next thing we need to do is to insert the dynamic linker into
704          * the proper entry in the GOT so that the PLT symbols can be properly
705          * resolved.
706          */
707
708         lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
709
710         if (lpnt) {
711                 lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT] +
712                                 ((int) libaddr));
713                 INIT_GOT(lpnt, tpnt);
714         };
715
716 #if defined (__SUPPORT_LD_DEBUG__)
717         if(_dl_debug) {
718                 _dl_dprintf(2, "\n\tfile='%s';  generating link map\n", libname);
719                 _dl_dprintf(2, "\t\tdynamic: %x  base: %x   size: %x\n",
720                                 dynamic_addr, libaddr, dynamic_size);
721                 _dl_dprintf(2, "\t\t  entry: %x  phdr: %x  phnum: %d\n\n",
722                                 epnt->e_entry + libaddr, tpnt->ppnt, tpnt->n_phent);
723
724         }
725 #endif
726         _dl_munmap(header, PAGE_SIZE);
727
728         return tpnt;
729 }
730
731 int _dl_fixup(struct dyn_elf *rpnt, int flag)
732 {
733         int goof = 0;
734         struct elf_resolve *tpnt;
735
736         if (rpnt->next)
737                 goof += _dl_fixup(rpnt->next, flag);
738         tpnt = rpnt->dyn;
739
740 #if defined (__SUPPORT_LD_DEBUG__)
741         if(_dl_debug) _dl_dprintf(_dl_debug_file,"\nrelocation processing: %s", tpnt->libname);
742 #endif
743
744         if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
745 #if defined (__SUPPORT_LD_DEBUG__)
746                 if(_dl_debug) {
747                         _dl_dprintf(2, "%s: can't handle %s relocation records\n",
748                                         _dl_progname, UNSUPPORTED_RELOC_STR);
749                 }
750 #endif
751                 goof++;
752                 return goof;
753         }
754
755         if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR]) {
756                 if (tpnt->init_flag & RELOCS_DONE)
757                         return goof;
758                 tpnt->init_flag |= RELOCS_DONE;
759                 goof += _dl_parse_relocation_information(rpnt,
760                                 tpnt->dynamic_info[DT_RELOC_TABLE_ADDR],
761                                 tpnt->dynamic_info[DT_RELOC_TABLE_SIZE], 0);
762         }
763
764         if (tpnt->dynamic_info[DT_JMPREL]) {
765                 if (tpnt->init_flag & JMP_RELOCS_DONE)
766                         return goof;
767                 tpnt->init_flag |= JMP_RELOCS_DONE;
768                 if (flag & RTLD_LAZY) {
769                         _dl_parse_lazy_relocation_information(rpnt,
770                                         tpnt->dynamic_info[DT_JMPREL],
771                                         tpnt->dynamic_info [DT_PLTRELSZ], 0);
772                 } else {
773                         goof += _dl_parse_relocation_information(rpnt,
774                                         tpnt->dynamic_info[DT_JMPREL],
775                                         tpnt->dynamic_info[DT_PLTRELSZ], 0);
776                 }
777         }
778
779         if (tpnt->init_flag & COPY_RELOCS_DONE)
780                 return goof;
781         tpnt->init_flag |= COPY_RELOCS_DONE;
782         goof += _dl_parse_copy_information(rpnt,
783                         tpnt->dynamic_info[DT_RELOC_TABLE_ADDR],
784                         tpnt->dynamic_info[DT_RELOC_TABLE_SIZE], 0);
785
786 #if defined (__SUPPORT_LD_DEBUG__)
787         if(_dl_debug) {
788                 _dl_dprintf(_dl_debug_file,"\nrelocation processing: %s", tpnt->libname);
789                 _dl_dprintf(_dl_debug_file,"; finished\n\n");
790         }
791 #endif
792
793         return goof;
794 }
795