Update.
[platform/upstream/glibc.git] / elf / dl-load.c
1 /* Map in a shared object's segments from the file.
2    Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 #include <elf.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <elf/ldsodefs.h>
27 #include <sys/mman.h>
28 #include <sys/param.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include "dynamic-link.h"
32 #include <stdio-common/_itoa.h>
33
34 #include <dl-origin.h>
35
36
37 /* On some systems, no flag bits are given to specify file mapping.  */
38 #ifndef MAP_FILE
39 #define MAP_FILE        0
40 #endif
41
42 /* The right way to map in the shared library files is MAP_COPY, which
43    makes a virtual copy of the data at the time of the mmap call; this
44    guarantees the mapped pages will be consistent even if the file is
45    overwritten.  Some losing VM systems like Linux's lack MAP_COPY.  All we
46    get is MAP_PRIVATE, which copies each page when it is modified; this
47    means if the file is overwritten, we may at some point get some pages
48    from the new version after starting with pages from the old version.  */
49 #ifndef MAP_COPY
50 #define MAP_COPY        MAP_PRIVATE
51 #endif
52
53 /* Some systems link their relocatable objects for another base address
54    than 0.  We want to know the base address for these such that we can
55    subtract this address from the segment addresses during mapping.
56    This results in a more efficient address space usage.  Defaults to
57    zero for almost all systems.  */
58 #ifndef MAP_BASE_ADDR
59 #define MAP_BASE_ADDR(l)        0
60 #endif
61
62
63 #include <endian.h>
64 #if BYTE_ORDER == BIG_ENDIAN
65 #define byteorder ELFDATA2MSB
66 #define byteorder_name "big-endian"
67 #elif BYTE_ORDER == LITTLE_ENDIAN
68 #define byteorder ELFDATA2LSB
69 #define byteorder_name "little-endian"
70 #else
71 #error "Unknown BYTE_ORDER " BYTE_ORDER
72 #define byteorder ELFDATANONE
73 #endif
74
75 #define STRING(x) __STRING (x)
76
77 #ifdef MAP_ANON
78 /* The fd is not examined when using MAP_ANON.  */
79 #define ANONFD -1
80 #else
81 int _dl_zerofd = -1;
82 #define ANONFD _dl_zerofd
83 #endif
84
85 /* Handle situations where we have a preferred location in memory for
86    the shared objects.  */
87 #ifdef ELF_PREFERRED_ADDRESS_DATA
88 ELF_PREFERRED_ADDRESS_DATA;
89 #endif
90 #ifndef ELF_PREFERRED_ADDRESS
91 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
92 #endif
93 #ifndef ELF_FIXED_ADDRESS
94 #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
95 #endif
96
97 size_t _dl_pagesize;
98
99 extern const char *_dl_platform;
100 extern size_t _dl_platformlen;
101
102 /* This is the decomposed LD_LIBRARY_PATH search path.  */
103 static struct r_search_path_elem **env_path_list;
104
105 /* List of the hardware capabilities we might end up using.  */
106 static const struct r_strlenpair *capstr;
107 static size_t ncapstr;
108 static size_t max_capstrlen;
109
110 const unsigned char _dl_pf_to_prot[8] =
111 {
112   [0] = PROT_NONE,
113   [PF_R] = PROT_READ,
114   [PF_W] = PROT_WRITE,
115   [PF_R | PF_W] = PROT_READ | PROT_WRITE,
116   [PF_X] = PROT_EXEC,
117   [PF_R | PF_X] = PROT_READ | PROT_EXEC,
118   [PF_W | PF_X] = PROT_WRITE | PROT_EXEC,
119   [PF_R | PF_W | PF_X] = PROT_READ | PROT_WRITE | PROT_EXEC
120 };
121
122
123 /* Get the generated information about the trusted directories.  */
124 #include "trusted-dirs.h"
125
126 static const char system_dirs[] = SYSTEM_DIRS;
127 static const size_t system_dirs_len[] =
128 {
129   SYSTEM_DIRS_LEN
130 };
131
132 /* This function has no public prototype.  */
133 extern ssize_t __libc_read (int, void *, size_t);
134
135
136 /* Local version of `strdup' function.  */
137 static inline char *
138 local_strdup (const char *s)
139 {
140   size_t len = strlen (s) + 1;
141   void *new = malloc (len);
142
143   if (new == NULL)
144     return NULL;
145
146   return (char *) memcpy (new, s, len);
147 }
148
149 /* Return copy of argument with all recognized dynamic string tokens
150    ($ORIGIN and $PLATFORM for now) replaced.  On some platforms it
151    might not be possible to determine the path from which the object
152    belonging to the map is loaded.  In this case the path element
153    containing $ORIGIN is left out.  */
154 static char *
155 expand_dynamic_string_token (struct link_map *l, const char *s)
156 {
157   /* We make two runs over the string.  First we determine how large the
158      resulting string is and then we copy it over.  Since this is now
159      frequently executed operation we are looking here not for performance
160      but rather for code size.  */
161   const char *sf;
162   size_t cnt = 0;
163   size_t origin_len;
164   size_t total;
165   char *result, *last_elem, *wp;
166
167   sf = strchr (s, '$');
168   while (sf != NULL)
169     {
170       size_t len = 1;
171
172       /* $ORIGIN is not expanded for SUID/GUID programs.  */
173       if ((((!__libc_enable_secure
174              && strncmp (&sf[1], "ORIGIN", 6) == 0 && (len = 7) != 0)
175             || (strncmp (&sf[1], "PLATFORM", 8) == 0 && (len = 9) != 0))
176            && (s[len] == '\0' || s[len] == '/' || s[len] == ':'))
177           || (s[1] == '{'
178               && ((!__libc_enable_secure
179                    && strncmp (&sf[2], "ORIGIN}", 7) == 0 && (len = 9) != 0)
180                   || (strncmp (&sf[2], "PLATFORM}", 9) == 0
181                       && (len = 11) != 0))))
182         ++cnt;
183
184       sf = strchr (sf + len, '$');
185     }
186
187   /* If we do not have to replace anything simply copy the string.  */
188   if (cnt == 0)
189     return local_strdup (s);
190
191   /* Now we make a guess how many extra characters on top of the length
192      of S we need to represent the result.  We know that we have CNT
193      replacements.  Each at most can use
194         MAX (strlen (ORIGIN), strlen (_dl_platform))
195      minus 7 (which is the length of "$ORIGIN").
196
197      First get the origin string if it is not available yet.  This can
198      only happen for the map of the executable.  */
199   if (l->l_origin == NULL)
200     {
201       assert (l->l_name[0] == '\0');
202       l->l_origin = get_origin ();
203       origin_len = (l->l_origin && l->l_origin != (char *) -1
204                     ? strlen (l->l_origin) : 0);
205     }
206   else
207     origin_len = l->l_origin == (char *) -1 ? 0 : strlen (l->l_origin);
208
209   total = strlen (s) + cnt * (MAX (origin_len, _dl_platformlen) - 7);
210   result = (char *) malloc (total + 1);
211   if (result == NULL)
212     return NULL;
213
214   /* Now fill the result path.  While copying over the string we keep
215      track of the start of the last path element.  When we come accross
216      a DST we copy over the value or (if the value is not available)
217      leave the entire path element out.  */
218   last_elem = wp = result;
219   do
220     {
221       if (*s == '$')
222         {
223           const char *repl;
224           size_t len;
225
226           if ((((strncmp (&s[1], "ORIGIN", 6) == 0 && (len = 7) != 0)
227                 || (strncmp (&s[1], "PLATFORM", 8) == 0 && (len = 9) != 0))
228                && (s[len] == '\0' || s[len] == '/' || s[len] == ':'))
229               || (s[1] == '{'
230                   && ((strncmp (&s[2], "ORIGIN}", 7) == 0 && (len = 9) != 0)
231                       || (strncmp (&s[2], "PLATFORM}", 9) == 0
232                           && (len = 11) != 0))))
233             {
234               repl = ((len == 7 || s[2] == 'O')
235                       ? (__libc_enable_secure ? NULL : l->l_origin)
236                       : _dl_platform);
237
238               if (repl != NULL && repl != (const char *) -1)
239                 {
240                   wp = __stpcpy (wp, repl);
241                   s += len;
242                 }
243               else
244                 {
245                   /* We cannot use this path element, the value of the
246                      replacement is unknown.  */
247                   wp = last_elem;
248                   s += len;
249                   while (*s != '\0' && *s != ':')
250                     ++s;
251                 }
252             }
253           else
254             /* No DST we recognize.  */
255             *wp++ = *s++;
256         }
257       else if (*s == ':')
258         {
259           *wp++ = *s++;
260           last_elem = wp;
261         }
262       else
263         *wp++ = *s++;
264     }
265   while (*s != '\0');
266
267   *wp = '\0';
268
269   return result;
270 }
271
272 /* Add `name' to the list of names for a particular shared object.
273    `name' is expected to have been allocated with malloc and will
274    be freed if the shared object already has this name.
275    Returns false if the object already had this name.  */
276 static void
277 internal_function
278 add_name_to_object (struct link_map *l, const char *name)
279 {
280   struct libname_list *lnp, *lastp;
281   struct libname_list *newname;
282   size_t name_len;
283
284   lastp = NULL;
285   for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
286     if (strcmp (name, lnp->name) == 0)
287       return;
288
289   name_len = strlen (name) + 1;
290   newname = malloc (sizeof *newname + name_len);
291   if (newname == NULL)
292     {
293       /* No more memory.  */
294       _dl_signal_error (ENOMEM, name, "cannot allocate name record");
295       return;
296     }
297   /* The object should have a libname set from _dl_new_object.  */
298   assert (lastp != NULL);
299
300   newname->name = memcpy (newname + 1, name, name_len);
301   newname->next = NULL;
302   lastp->next = newname;
303 }
304
305 /* All known directories in sorted order.  */
306 static struct r_search_path_elem *all_dirs;
307
308 /* Standard search directories.  */
309 static struct r_search_path_elem **rtld_search_dirs;
310
311 static size_t max_dirnamelen;
312
313 static inline struct r_search_path_elem **
314 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
315               int check_trusted, const char *what, const char *where)
316 {
317   char *cp;
318   size_t nelems = 0;
319
320   while ((cp = __strsep (&rpath, sep)) != NULL)
321     {
322       struct r_search_path_elem *dirp;
323       size_t len = strlen (cp);
324
325       /* `strsep' can pass an empty string.  This has to be
326          interpreted as `use the current directory'. */
327       if (len == 0)
328         {
329           static const char curwd[] = "./";
330           cp = (char *) curwd;
331         }
332
333       /* Remove trailing slashes (except for "/").  */
334       while (len > 1 && cp[len - 1] == '/')
335         --len;
336
337       /* Now add one if there is none so far.  */
338       if (len > 0 && cp[len - 1] != '/')
339         cp[len++] = '/';
340
341       /* Make sure we don't use untrusted directories if we run SUID.  */
342       if (check_trusted)
343         {
344           const char *trun = system_dirs;
345           size_t idx;
346
347           /* All trusted directories must be complete names.  */
348           if (cp[0] != '/')
349             continue;
350
351           for (idx = 0;
352                idx < sizeof (system_dirs_len) / sizeof (system_dirs_len[0]);
353                ++idx)
354             {
355               if (len == system_dirs_len[idx] && memcmp (trun, cp, len) == 0)
356                 /* Found it.  */
357                 break;
358
359               trun += system_dirs_len[idx] + 1;
360             }
361
362           if (idx == sizeof (system_dirs_len) / sizeof (system_dirs_len[0]))
363             /* It's no trusted directory, skip it.  */
364             continue;
365         }
366
367       /* See if this directory is already known.  */
368       for (dirp = all_dirs; dirp != NULL; dirp = dirp->next)
369         if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
370           break;
371
372       if (dirp != NULL)
373         {
374           /* It is available, see whether it's on our own list.  */
375           size_t cnt;
376           for (cnt = 0; cnt < nelems; ++cnt)
377             if (result[cnt] == dirp)
378               break;
379
380           if (cnt == nelems)
381             result[nelems++] = dirp;
382         }
383       else
384         {
385           size_t cnt;
386
387           /* It's a new directory.  Create an entry and add it.  */
388           dirp = (struct r_search_path_elem *)
389             malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status));
390           if (dirp == NULL)
391             _dl_signal_error (ENOMEM, NULL,
392                               "cannot create cache for search path");
393
394           dirp->dirname = cp;
395           dirp->dirnamelen = len;
396
397           if (len > max_dirnamelen)
398             max_dirnamelen = len;
399
400           /* We have to make sure all the relative directories are never
401              ignored.  The current directory might change and all our
402              saved information would be void.  */
403           if (cp[0] != '/')
404             for (cnt = 0; cnt < ncapstr; ++cnt)
405               dirp->status[cnt] = existing;
406           else
407             for (cnt = 0; cnt < ncapstr; ++cnt)
408               dirp->status[cnt] = unknown;
409
410           dirp->what = what;
411           dirp->where = where;
412
413           dirp->next = all_dirs;
414           all_dirs = dirp;
415
416           /* Put it in the result array.  */
417           result[nelems++] = dirp;
418         }
419     }
420
421   /* Terminate the array.  */
422   result[nelems] = NULL;
423
424   return result;
425 }
426
427
428 static struct r_search_path_elem **
429 internal_function
430 decompose_rpath (const char *rpath, struct link_map *l)
431 {
432   /* Make a copy we can work with.  */
433   const char *where = l->l_name;
434   char *copy;
435   char *cp;
436   struct r_search_path_elem **result;
437   size_t nelems;
438
439   /* First see whether we must forget the RPATH from this object.  */
440   if (_dl_inhibit_rpath != NULL && !__libc_enable_secure)
441     {
442       const char *found = strstr (_dl_inhibit_rpath, where);
443       if (found != NULL)
444         {
445           size_t len = strlen (where);
446           if ((found == _dl_inhibit_rpath || found[-1] == ':')
447               && (found[len] == '\0' || found[len] == ':'))
448             {
449               /* This object is on the list of objects for which the RPATH
450                  must not be used.  */
451               result = (struct r_search_path_elem **)
452                 malloc (sizeof (*result));
453               if (result == NULL)
454                 _dl_signal_error (ENOMEM, NULL,
455                                   "cannot create cache for search path");
456               result[0] = NULL;
457
458               return result;
459             }
460         }
461     }
462
463   /* Make a writable copy.  At the same time expand possible dynamic
464      string tokens.  */
465   copy = expand_dynamic_string_token (l, rpath);
466   if (copy == NULL)
467     _dl_signal_error (ENOMEM, NULL, "cannot create RPATH copy");
468
469   /* Count the number of necessary elements in the result array.  */
470   nelems = 0;
471   for (cp = copy; *cp != '\0'; ++cp)
472     if (*cp == ':')
473       ++nelems;
474
475   /* Allocate room for the result.  NELEMS + 1 is an upper limit for the
476      number of necessary entries.  */
477   result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1)
478                                                   * sizeof (*result));
479   if (result == NULL)
480     _dl_signal_error (ENOMEM, NULL, "cannot create cache for search path");
481
482   return fillin_rpath (copy, result, ":", 0, "RPATH", where);
483 }
484
485
486 void
487 internal_function
488 _dl_init_paths (const char *llp)
489 {
490   size_t idx;
491   const char *strp;
492   struct r_search_path_elem *pelem, **aelem;
493   size_t round_size;
494 #ifdef PIC
495   struct link_map *l;
496 #endif
497
498   /* Fill in the information about the application's RPATH and the
499      directories addressed by the LD_LIBRARY_PATH environment variable.  */
500
501   /* Get the capabilities.  */
502   capstr = _dl_important_hwcaps (_dl_platform, _dl_platformlen,
503                                  &ncapstr, &max_capstrlen);
504
505   /* First set up the rest of the default search directory entries.  */
506   aelem = rtld_search_dirs = (struct r_search_path_elem **)
507     malloc ((sizeof (system_dirs_len) / sizeof (system_dirs_len[0]))
508              * sizeof (struct r_search_path_elem *));
509   if (rtld_search_dirs == NULL)
510     _dl_signal_error (ENOMEM, NULL, "cannot create search path array");
511
512   round_size = ((2 * sizeof (struct r_search_path_elem) - 1
513                  + ncapstr * sizeof (enum r_dir_status))
514                 / sizeof (struct r_search_path_elem));
515
516   rtld_search_dirs[0] = (struct r_search_path_elem *)
517     malloc ((sizeof (system_dirs) / sizeof (system_dirs[0]) - 1)
518             * round_size * sizeof (struct r_search_path_elem));
519   if (rtld_search_dirs[0] == NULL)
520     _dl_signal_error (ENOMEM, NULL, "cannot create cache for search path");
521
522   pelem = all_dirs = rtld_search_dirs[0];
523   strp = system_dirs;
524   idx = 0;
525
526   do
527     {
528       size_t cnt;
529
530       *aelem++ = pelem;
531
532       pelem->what = "system search path";
533       pelem->where = NULL;
534
535       pelem->dirname = strp;
536       pelem->dirnamelen = system_dirs_len[idx];
537       strp += system_dirs_len[idx] + 1;
538
539       if (pelem->dirname[0] != '/')
540         for (cnt = 0; cnt < ncapstr; ++cnt)
541           pelem->status[cnt] = existing;
542       else
543         for (cnt = 0; cnt < ncapstr; ++cnt)
544           pelem->status[cnt] = unknown;
545
546       pelem->next = (++idx == (sizeof (system_dirs_len)
547                                / sizeof (system_dirs_len[0]))
548                      ? NULL : (pelem + round_size));
549
550       pelem += round_size;
551     }
552   while (idx < sizeof (system_dirs_len) / sizeof (system_dirs_len[0]));
553
554   max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
555   *aelem = NULL;
556
557 #ifdef PIC
558   /* This points to the map of the main object.  */
559   l = _dl_loaded;
560   if (l != NULL)
561     {
562       assert (l->l_type != lt_loaded);
563
564       if (l->l_info[DT_RPATH])
565         /* Allocate room for the search path and fill in information
566            from RPATH.  */
567         l->l_rpath_dirs =
568           decompose_rpath ((const void *) (l->l_info[DT_STRTAB]->d_un.d_ptr
569                                            + l->l_info[DT_RPATH]->d_un.d_val),
570                            l);
571       else
572         l->l_rpath_dirs = NULL;
573     }
574 #endif  /* PIC */
575
576   if (llp != NULL && *llp != '\0')
577     {
578       size_t nllp;
579       const char *cp = llp;
580
581       /* Decompose the LD_LIBRARY_PATH contents.  First determine how many
582          elements it has.  */
583       nllp = 1;
584       while (*cp)
585         {
586           if (*cp == ':' || *cp == ';')
587             ++nllp;
588           ++cp;
589         }
590
591       env_path_list = (struct r_search_path_elem **)
592         malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
593       if (env_path_list == NULL)
594         _dl_signal_error (ENOMEM, NULL,
595                           "cannot create cache for search path");
596
597       (void) fillin_rpath (local_strdup (llp), env_path_list, ":;",
598                            __libc_enable_secure, "LD_LIBRARY_PATH", NULL);
599     }
600 }
601
602
603 /* Think twice before changing anything in this function.  It is placed
604    here and prepared using the `alloca' magic to prevent it from being
605    inlined.  The function is only called in case of an error.  But then
606    performance does not count.  The function used to be "inlinable" and
607    the compiled did so all the time.  This increased the code size for
608    absolutely no good reason.  */
609 #define LOSE(code, s) lose (code, fd, name, realname, l, s)
610 static void
611 __attribute__ ((noreturn))
612 lose (int code, int fd, const char *name, char *realname, struct link_map *l,
613       const char *msg)
614 {
615   /* The use of `alloca' here looks ridiculous but it helps.  The goal
616      is to avoid the function from being inlined.  There is no official
617      way to do this so we use this trick.  gcc never inlines functions
618      which use `alloca'.  */
619   int *a = alloca (sizeof (int));
620   a[0] = fd;
621   (void) __close (a[0]);
622   if (l != NULL)
623     {
624       /* Remove the stillborn object from the list and free it.  */
625       if (l->l_prev)
626         l->l_prev->l_next = l->l_next;
627       if (l->l_next)
628         l->l_next->l_prev = l->l_prev;
629       free (l);
630     }
631   free (realname);
632   _dl_signal_error (code, name, msg);
633 }
634
635
636 /* Map in the shared object NAME, actually located in REALNAME, and already
637    opened on FD.  */
638
639 #ifndef EXTERNAL_MAP_FROM_FD
640 static
641 #endif
642 struct link_map *
643 _dl_map_object_from_fd (const char *name, int fd, char *realname,
644                         struct link_map *loader, int l_type)
645 {
646   /* This is the expected ELF header.  */
647 #define ELF32_CLASS ELFCLASS32
648 #define ELF64_CLASS ELFCLASS64
649   static const unsigned char expected[EI_PAD] =
650   {
651     [EI_MAG0] = ELFMAG0,
652     [EI_MAG1] = ELFMAG1,
653     [EI_MAG2] = ELFMAG2,
654     [EI_MAG3] = ELFMAG3,
655     [EI_CLASS] = ELFW(CLASS),
656     [EI_DATA] = byteorder,
657     [EI_VERSION] = EV_CURRENT,
658     [EI_OSABI] = ELFOSABI_SYSV,
659     [EI_ABIVERSION] = 0
660   };
661   struct link_map *l = NULL;
662
663   inline caddr_t map_segment (ElfW(Addr) mapstart, size_t len,
664                               int prot, int fixed, off_t offset)
665     {
666       caddr_t mapat = __mmap ((caddr_t) mapstart, len, prot,
667                               fixed|MAP_COPY|MAP_FILE,
668                               fd, offset);
669       if (mapat == MAP_FAILED)
670         LOSE (errno, "failed to map segment from shared object");
671       return mapat;
672     }
673
674   const ElfW(Ehdr) *header;
675   const ElfW(Phdr) *phdr;
676   const ElfW(Phdr) *ph;
677   size_t maplength;
678   int type;
679   char *readbuf;
680   ssize_t readlength;
681   struct stat st;
682
683   /* Get file information.  */
684   if (__fxstat (_STAT_VER, fd, &st) < 0)
685     LOSE (errno, "cannot stat shared object");
686
687   /* Look again to see if the real name matched another already loaded.  */
688   for (l = _dl_loaded; l; l = l->l_next)
689     if (l->l_ino == st.st_ino && l->l_dev == st.st_dev)
690       {
691         /* The object is already loaded.
692            Just bump its reference count and return it.  */
693         __close (fd);
694
695         /* If the name is not in the list of names for this object add
696            it.  */
697         free (realname);
698         add_name_to_object (l, name);
699         ++l->l_opencount;
700         return l;
701       }
702
703   /* Print debugging message.  */
704   if (_dl_debug_files)
705     _dl_debug_message (1, "file=", name, ";  generating link map\n", NULL);
706
707   /* Read the header directly.  */
708   readbuf = alloca (_dl_pagesize);
709   readlength = __libc_read (fd, readbuf, _dl_pagesize);
710   if (readlength < (ssize_t) sizeof (*header))
711     LOSE (errno, "cannot read file data");
712   header = (void *) readbuf;
713
714   /* Check the header for basic validity.  */
715   if (memcmp (header->e_ident, expected, EI_PAD) != 0)
716     {
717       /* Something is wrong.  */
718       if (*(Elf32_Word *) &header->e_ident !=
719 #if BYTE_ORDER == LITTLE_ENDIAN
720           ((ELFMAG0 << (EI_MAG0 * 8)) |
721            (ELFMAG1 << (EI_MAG1 * 8)) |
722            (ELFMAG2 << (EI_MAG2 * 8)) |
723            (ELFMAG3 << (EI_MAG3 * 8)))
724 #else
725           ((ELFMAG0 << (EI_MAG3 * 8)) |
726            (ELFMAG1 << (EI_MAG2 * 8)) |
727            (ELFMAG2 << (EI_MAG1 * 8)) |
728            (ELFMAG3 << (EI_MAG0 * 8)))
729 #endif
730           )
731         LOSE (0, "invalid ELF header");
732       if (header->e_ident[EI_CLASS] != ELFW(CLASS))
733         LOSE (0, "ELF file class not " STRING(__ELF_NATIVE_CLASS) "-bit");
734       if (header->e_ident[EI_DATA] != byteorder)
735         LOSE (0, "ELF file data encoding not " byteorder_name);
736       if (header->e_ident[EI_VERSION] != EV_CURRENT)
737         LOSE (0, "ELF file version ident not " STRING(EV_CURRENT));
738       /* XXX We should be able so set system specific versions which are
739          allowed here.  */
740       if (header->e_ident[EI_OSABI] != ELFOSABI_SYSV)
741         LOSE (0, "ELF file OS ABI not " STRING(ELFOSABI_SYSV));
742       if (header->e_ident[EI_ABIVERSION] != 0)
743         LOSE (0, "ELF file ABI version not 0");
744       LOSE (0, "internal error");
745     }
746
747   if (header->e_version != EV_CURRENT)
748     LOSE (0, "ELF file version not " STRING(EV_CURRENT));
749   if (! elf_machine_matches_host (header->e_machine))
750     LOSE (0, "ELF file machine architecture not " ELF_MACHINE_NAME);
751   if (header->e_phentsize != sizeof (ElfW(Phdr)))
752     LOSE (0, "ELF file's phentsize not the expected size");
753
754 #ifndef MAP_ANON
755 # define MAP_ANON 0
756   if (_dl_zerofd == -1)
757     {
758       _dl_zerofd = _dl_sysdep_open_zero_fill ();
759       if (_dl_zerofd == -1)
760         {
761           __close (fd);
762           _dl_signal_error (errno, NULL, "cannot open zero fill device");
763         }
764     }
765 #endif
766
767   /* Enter the new object in the list of loaded objects.  */
768   l = _dl_new_object (realname, name, l_type, loader);
769   if (! l)
770     LOSE (ENOMEM, "cannot create shared object descriptor");
771   l->l_opencount = 1;
772
773   /* Extract the remaining details we need from the ELF header
774      and then read in the program header table.  */
775   l->l_entry = header->e_entry;
776   type = header->e_type;
777   l->l_phnum = header->e_phnum;
778
779   maplength = header->e_phnum * sizeof (ElfW(Phdr));
780   if (header->e_phoff + maplength <= readlength)
781     phdr = (void *) (readbuf + header->e_phoff);
782   else
783     {
784       phdr = alloca (maplength);
785       __lseek (fd, SEEK_SET, header->e_phoff);
786       if (__libc_read (fd, (void *) phdr, maplength) != maplength)
787         LOSE (errno, "cannot read file data");
788     }
789
790   {
791     /* Scan the program header table, collecting its load commands.  */
792     struct loadcmd
793       {
794         ElfW(Addr) mapstart, mapend, dataend, allocend;
795         off_t mapoff;
796         int prot;
797       } loadcmds[l->l_phnum], *c;
798     size_t nloadcmds = 0;
799
800     /* The struct is initialized to zero so this is not necessary:
801     l->l_ld = 0;
802     l->l_phdr = 0;
803     l->l_addr = 0; */
804     for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
805       switch (ph->p_type)
806         {
807           /* These entries tell us where to find things once the file's
808              segments are mapped in.  We record the addresses it says
809              verbatim, and later correct for the run-time load address.  */
810         case PT_DYNAMIC:
811           l->l_ld = (void *) ph->p_vaddr;
812           break;
813         case PT_PHDR:
814           l->l_phdr = (void *) ph->p_vaddr;
815           break;
816
817         case PT_LOAD:
818           /* A load command tells us to map in part of the file.
819              We record the load commands and process them all later.  */
820           if (ph->p_align % _dl_pagesize != 0)
821             LOSE (0, "ELF load command alignment not page-aligned");
822           if ((ph->p_vaddr - ph->p_offset) % ph->p_align)
823             LOSE (0, "ELF load command address/offset not properly aligned");
824           {
825             struct loadcmd *c = &loadcmds[nloadcmds++];
826             c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
827             c->mapend = ((ph->p_vaddr + ph->p_filesz + _dl_pagesize - 1)
828                          & ~(_dl_pagesize - 1));
829             c->dataend = ph->p_vaddr + ph->p_filesz;
830             c->allocend = ph->p_vaddr + ph->p_memsz;
831             c->mapoff = ph->p_offset & ~(ph->p_align - 1);
832
833             /* Optimize a common case.  */
834             if ((PF_R | PF_W | PF_X) == 7
835                 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7)
836               c->prot = _dl_pf_to_prot[ph->p_flags & (PF_R | PF_W | PF_X)];
837             else
838               {
839                 c->prot = 0;
840                 if (ph->p_flags & PF_R)
841                   c->prot |= PROT_READ;
842                 if (ph->p_flags & PF_W)
843                   c->prot |= PROT_WRITE;
844                 if (ph->p_flags & PF_X)
845                   c->prot |= PROT_EXEC;
846               }
847             break;
848           }
849         }
850
851     /* Now process the load commands and map segments into memory.  */
852     c = loadcmds;
853
854     /* Length of the sections to be loaded.  */
855     maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
856
857     if (type == ET_DYN || type == ET_REL)
858       {
859         /* This is a position-independent shared object.  We can let the
860            kernel map it anywhere it likes, but we must have space for all
861            the segments in their specified positions relative to the first.
862            So we map the first segment without MAP_FIXED, but with its
863            extent increased to cover all the segments.  Then we remove
864            access from excess portion, and there is known sufficient space
865            there to remap from the later segments.
866
867            As a refinement, sometimes we have an address that we would
868            prefer to map such objects at; but this is only a preference,
869            the OS can do whatever it likes. */
870         caddr_t mapat;
871         ElfW(Addr) mappref;
872         mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
873                    - MAP_BASE_ADDR (l));
874         mapat = map_segment (mappref, maplength, c->prot, 0, c->mapoff);
875         l->l_addr = (ElfW(Addr)) mapat - c->mapstart;
876
877         /* Change protection on the excess portion to disallow all access;
878            the portions we do not remap later will be inaccessible as if
879            unallocated.  Then jump into the normal segment-mapping loop to
880            handle the portion of the segment past the end of the file
881            mapping.  */
882         __mprotect ((caddr_t) (l->l_addr + c->mapend),
883                     loadcmds[nloadcmds - 1].allocend - c->mapend,
884                     0);
885
886         /* Remember which part of the address space this object uses.  */
887         l->l_map_start = c->mapstart + l->l_addr;
888         l->l_map_end = l->l_map_start + maplength;
889
890         goto postmap;
891       }
892     else
893       {
894         /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
895            fixed.  */
896         ELF_FIXED_ADDRESS (loader, c->mapstart);
897       }
898
899     /* Remember which part of the address space this object uses.  */
900     l->l_map_start = c->mapstart + l->l_addr;
901     l->l_map_end = l->l_map_start + maplength;
902
903     while (c < &loadcmds[nloadcmds])
904       {
905         if (c->mapend > c->mapstart)
906           /* Map the segment contents from the file.  */
907           map_segment (l->l_addr + c->mapstart, c->mapend - c->mapstart,
908                        c->prot, MAP_FIXED, c->mapoff);
909
910       postmap:
911         if (c->allocend > c->dataend)
912           {
913             /* Extra zero pages should appear at the end of this segment,
914                after the data mapped from the file.   */
915             ElfW(Addr) zero, zeroend, zeropage;
916
917             zero = l->l_addr + c->dataend;
918             zeroend = l->l_addr + c->allocend;
919             zeropage = (zero + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
920
921             if (zeroend < zeropage)
922               /* All the extra data is in the last page of the segment.
923                  We can just zero it.  */
924               zeropage = zeroend;
925
926             if (zeropage > zero)
927               {
928                 /* Zero the final part of the last page of the segment.  */
929                 if ((c->prot & PROT_WRITE) == 0)
930                   {
931                     /* Dag nab it.  */
932                     if (__mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
933                                     _dl_pagesize, c->prot|PROT_WRITE) < 0)
934                       LOSE (errno, "cannot change memory protections");
935                   }
936                 memset ((void *) zero, 0, zeropage - zero);
937                 if ((c->prot & PROT_WRITE) == 0)
938                   __mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
939                               _dl_pagesize, c->prot);
940               }
941
942             if (zeroend > zeropage)
943               {
944                 /* Map the remaining zero pages in from the zero fill FD.  */
945                 caddr_t mapat;
946                 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
947                                 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
948                                 ANONFD, 0);
949                 if (mapat == MAP_FAILED)
950                   LOSE (errno, "cannot map zero-fill pages");
951               }
952           }
953
954         ++c;
955       }
956
957     if (l->l_phdr == 0)
958       {
959         /* There was no PT_PHDR specified.  We need to find the phdr in the
960            load image ourselves.  We assume it is in fact in the load image
961            somewhere.  */
962         for (c = loadcmds; c < &loadcmds[nloadcmds]; c++)
963           if (c->mapoff <= header->e_phoff
964               && (c->mapend - c->mapstart + c->mapoff
965                   >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
966             {
967               ElfW(Addr) bof = l->l_addr + c->mapstart;
968               l->l_phdr = (void *) (bof + header->e_phoff - c->mapoff);
969               break;
970             }
971         if (l->l_phdr == 0)
972           LOSE (0, "program headers not contained in any loaded segment");
973       }
974     else
975       /* Adjust the PT_PHDR value by the runtime load address.  */
976       (ElfW(Addr)) l->l_phdr += l->l_addr;
977   }
978
979   /* We are done mapping in the file.  We no longer need the descriptor.  */
980   __close (fd);
981
982   if (l->l_type == lt_library && type == ET_EXEC)
983     l->l_type = lt_executable;
984
985   if (l->l_ld == 0)
986     {
987       if (type == ET_DYN)
988         LOSE (0, "object file has no dynamic section");
989     }
990   else
991     (ElfW(Addr)) l->l_ld += l->l_addr;
992
993   l->l_entry += l->l_addr;
994
995   if (_dl_debug_files)
996     {
997       const size_t nibbles = sizeof (void *) * 2;
998       char buf1[nibbles + 1];
999       char buf2[nibbles + 1];
1000       char buf3[nibbles + 1];
1001
1002       buf1[nibbles] = '\0';
1003       buf2[nibbles] = '\0';
1004       buf3[nibbles] = '\0';
1005
1006       memset (buf1, '0', nibbles);
1007       memset (buf2, '0', nibbles);
1008       memset (buf3, '0', nibbles);
1009       _itoa_word ((unsigned long int) l->l_ld, &buf1[nibbles], 16, 0);
1010       _itoa_word ((unsigned long int) l->l_addr, &buf2[nibbles], 16, 0);
1011       _itoa_word (maplength, &buf3[nibbles], 16, 0);
1012
1013       _dl_debug_message (1, "  dynamic: 0x", buf1, "  base: 0x", buf2,
1014                          "   size: 0x", buf3, "\n", NULL);
1015       memset (buf1, '0', nibbles);
1016       memset (buf2, '0', nibbles);
1017       memset (buf3, ' ', nibbles);
1018       _itoa_word ((unsigned long int) l->l_entry, &buf1[nibbles], 16, 0);
1019       _itoa_word ((unsigned long int) l->l_phdr, &buf2[nibbles], 16, 0);
1020       _itoa_word (l->l_phnum, &buf3[nibbles], 10, 0);
1021       _dl_debug_message (1, "    entry: 0x", buf1, "  phdr: 0x", buf2,
1022                          "  phnum:   ", buf3, "\n\n", NULL);
1023     }
1024
1025   elf_get_dynamic_info (l->l_ld, l->l_addr, l->l_info);
1026   if (l->l_info[DT_HASH])
1027     _dl_setup_hash (l);
1028
1029   /* If this object has DT_SYMBOLIC set modify now its scope.  We don't
1030      have to do this for the main map.  */
1031   if (l->l_info[DT_SYMBOLIC] && &l->l_searchlist != l->l_scope[0])
1032     {
1033       /* Create an appropriate searchlist.  It contains only this map.
1034
1035          XXX This is the definition of DT_SYMBOLIC in SysVr4.  The old
1036          GNU ld.so implementation had a different interpretation which
1037          is more reasonable.  We are prepared to add this possibility
1038          back as part of a GNU extension of the ELF format.  */
1039       l->l_symbolic_searchlist.r_list =
1040         (struct link_map **) malloc (sizeof (struct link_map *));
1041
1042       if (l->l_symbolic_searchlist.r_list == NULL)
1043         LOSE (ENOMEM, "cannot create searchlist");
1044
1045       l->l_symbolic_searchlist.r_list[0] = l;
1046       l->l_symbolic_searchlist.r_nlist = 1;
1047       l->l_symbolic_searchlist.r_duplist = l->l_symbolic_searchlist.r_list;
1048       l->l_symbolic_searchlist.r_nduplist = 1;
1049
1050       /* Now move the existing entries one back.  */
1051       memmove (&l->l_scope[1], &l->l_scope[0],
1052                sizeof (l->l_scope) - sizeof (l->l_scope[0]));
1053
1054       /* Now add the new entry.  */
1055       l->l_scope[0] = &l->l_symbolic_searchlist;
1056     }
1057
1058   /* Finally the file information.  */
1059   l->l_dev = st.st_dev;
1060   l->l_ino = st.st_ino;
1061
1062   return l;
1063 }
1064 \f
1065 /* Print search path.  */
1066 static void
1067 print_search_path (struct r_search_path_elem **list,
1068                    const char *what, const char *name)
1069 {
1070   char buf[max_dirnamelen + max_capstrlen];
1071   int first = 1;
1072
1073   _dl_debug_message (1, " search path=", NULL);
1074
1075   while (*list != NULL && (*list)->what == what) /* Yes, ==.  */
1076     {
1077       char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1078       size_t cnt;
1079
1080       for (cnt = 0; cnt < ncapstr; ++cnt)
1081         if ((*list)->status[cnt] != nonexisting)
1082           {
1083             char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
1084             if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1085               cp[0] = '\0';
1086             else
1087               cp[-1] = '\0';
1088             _dl_debug_message (0, first ? "" : ":", buf, NULL);
1089             first = 0;
1090           }
1091
1092       ++list;
1093     }
1094
1095   if (name != NULL)
1096     _dl_debug_message (0, "\t\t(", what, " from file ",
1097                         name[0] ? name : _dl_argv[0], ")\n", NULL);
1098   else
1099     _dl_debug_message (0, "\t\t(", what, ")\n", NULL);
1100 }
1101 \f
1102 /* Try to open NAME in one of the directories in DIRS.
1103    Return the fd, or -1.  If successful, fill in *REALNAME
1104    with the malloc'd full directory name.  */
1105
1106 static int
1107 open_path (const char *name, size_t namelen, int preloaded,
1108            struct r_search_path_elem **dirs,
1109            char **realname)
1110 {
1111   char *buf;
1112   int fd = -1;
1113   const char *current_what = NULL;
1114
1115   if (dirs == NULL || *dirs == NULL)
1116     {
1117       __set_errno (ENOENT);
1118       return -1;
1119     }
1120
1121   buf = alloca (max_dirnamelen + max_capstrlen + namelen);
1122   do
1123     {
1124       struct r_search_path_elem *this_dir = *dirs;
1125       size_t buflen = 0;
1126       size_t cnt;
1127       char *edp;
1128
1129       /* If we are debugging the search for libraries print the path
1130          now if it hasn't happened now.  */
1131       if (_dl_debug_libs && current_what != this_dir->what)
1132         {
1133           current_what = this_dir->what;
1134           print_search_path (dirs, current_what, this_dir->where);
1135         }
1136
1137       edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
1138       for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
1139         {
1140           /* Skip this directory if we know it does not exist.  */
1141           if (this_dir->status[cnt] == nonexisting)
1142             continue;
1143
1144           buflen =
1145             ((char *) __mempcpy (__mempcpy (edp,
1146                                             capstr[cnt].str, capstr[cnt].len),
1147                                  name, namelen)
1148              - buf);
1149
1150           /* Print name we try if this is wanted.  */
1151           if (_dl_debug_libs)
1152             _dl_debug_message (1, "  trying file=", buf, "\n", NULL);
1153
1154           fd = __open (buf, O_RDONLY);
1155           if (this_dir->status[cnt] == unknown)
1156             {
1157               if (fd != -1)
1158                 this_dir->status[cnt] = existing;
1159               else
1160                 {
1161                   /* We failed to open machine dependent library.  Let's
1162                      test whether there is any directory at all.  */
1163                   struct stat st;
1164
1165                   buf[buflen - namelen - 1] = '\0';
1166
1167                   if (__xstat (_STAT_VER, buf, &st) != 0
1168                       || ! S_ISDIR (st.st_mode))
1169                     /* The directory does not exist or it is no directory.  */
1170                     this_dir->status[cnt] = nonexisting;
1171                   else
1172                     this_dir->status[cnt] = existing;
1173                 }
1174             }
1175
1176           if (fd != -1 && preloaded && __libc_enable_secure)
1177             {
1178               /* This is an extra security effort to make sure nobody can
1179                  preload broken shared objects which are in the trusted
1180                  directories and so exploit the bugs.  */
1181               struct stat st;
1182
1183               if (__fxstat (_STAT_VER, fd, &st) != 0
1184                   || (st.st_mode & S_ISUID) == 0)
1185                 {
1186                   /* The shared object cannot be tested for being SUID
1187                      or this bit is not set.  In this case we must not
1188                      use this object.  */
1189                   __close (fd);
1190                   fd = -1;
1191                   /* We simply ignore the file, signal this by setting
1192                      the error value which would have been set by `open'.  */
1193                   errno = ENOENT;
1194                 }
1195             }
1196         }
1197
1198       if (fd != -1)
1199         {
1200           *realname = malloc (buflen);
1201           if (*realname != NULL)
1202             {
1203               memcpy (*realname, buf, buflen);
1204               return fd;
1205             }
1206           else
1207             {
1208               /* No memory for the name, we certainly won't be able
1209                  to load and link it.  */
1210               __close (fd);
1211               return -1;
1212             }
1213         }
1214       if (errno != ENOENT && errno != EACCES)
1215         /* The file exists and is readable, but something went wrong.  */
1216         return -1;
1217     }
1218   while (*++dirs != NULL);
1219
1220   return -1;
1221 }
1222
1223 /* Map in the shared object file NAME.  */
1224
1225 struct link_map *
1226 internal_function
1227 _dl_map_object (struct link_map *loader, const char *name, int preloaded,
1228                 int type, int trace_mode)
1229 {
1230   int fd;
1231   char *realname;
1232   char *name_copy;
1233   struct link_map *l;
1234
1235   /* Look for this name among those already loaded.  */
1236   for (l = _dl_loaded; l; l = l->l_next)
1237     {
1238       /* If the requested name matches the soname of a loaded object,
1239          use that object.  Elide this check for names that have not
1240          yet been opened.  */
1241       if (l->l_opencount <= 0)
1242         continue;
1243       if (!_dl_name_match_p (name, l))
1244         {
1245           const char *soname;
1246
1247           if (l->l_info[DT_SONAME] == NULL)
1248             continue;
1249
1250           soname = (const void *) (l->l_info[DT_STRTAB]->d_un.d_ptr
1251                                    + l->l_info[DT_SONAME]->d_un.d_val);
1252           if (strcmp (name, soname) != 0)
1253             continue;
1254
1255           /* We have a match on a new name -- cache it.  */
1256           add_name_to_object (l, soname);
1257         }
1258
1259       /* We have a match -- bump the reference count and return it.  */
1260       ++l->l_opencount;
1261       return l;
1262     }
1263
1264   /* Display information if we are debugging.  */
1265   if (_dl_debug_files && loader != NULL)
1266     _dl_debug_message (1, "\nfile=", name, ";  needed by ",
1267                        loader->l_name[0] ? loader->l_name : _dl_argv[0],
1268                        "\n", NULL);
1269
1270   if (strchr (name, '/') == NULL)
1271     {
1272       /* Search for NAME in several places.  */
1273
1274       size_t namelen = strlen (name) + 1;
1275
1276       if (_dl_debug_libs)
1277         _dl_debug_message (1, "find library=", name, "; searching\n", NULL);
1278
1279       fd = -1;
1280
1281       /* First try the DT_RPATH of the dependent object that caused NAME
1282          to be loaded.  Then that object's dependent, and on up.  */
1283       for (l = loader; fd == -1 && l; l = l->l_loader)
1284         if (l->l_info[DT_RPATH])
1285           {
1286             /* Make sure the cache information is available.  */
1287             if (l->l_rpath_dirs == NULL)
1288               {
1289                 size_t ptrval = (l->l_info[DT_STRTAB]->d_un.d_ptr
1290                                  + l->l_info[DT_RPATH]->d_un.d_val);
1291                 l->l_rpath_dirs =
1292                   decompose_rpath ((const char *) ptrval, l);
1293               }
1294
1295             if (l->l_rpath_dirs != NULL)
1296               fd = open_path (name, namelen, preloaded, l->l_rpath_dirs,
1297                               &realname);
1298           }
1299
1300       /* If dynamically linked, try the DT_RPATH of the executable itself.  */
1301       l = _dl_loaded;
1302       if (fd == -1 && l && l->l_type != lt_loaded && l != loader
1303           && l->l_rpath_dirs != NULL)
1304         fd = open_path (name, namelen, preloaded, l->l_rpath_dirs, &realname);
1305
1306       /* Try the LD_LIBRARY_PATH environment variable.  */
1307       if (fd == -1 && env_path_list != NULL)
1308         fd = open_path (name, namelen, preloaded, env_path_list, &realname);
1309
1310       if (fd == -1)
1311         {
1312           /* Check the list of libraries in the file /etc/ld.so.cache,
1313              for compatibility with Linux's ldconfig program.  */
1314           extern const char *_dl_load_cache_lookup (const char *name);
1315           const char *cached = _dl_load_cache_lookup (name);
1316           if (cached)
1317             {
1318               fd = __open (cached, O_RDONLY);
1319               if (fd != -1)
1320                 {
1321                   realname = local_strdup (cached);
1322                   if (realname == NULL)
1323                     {
1324                       __close (fd);
1325                       fd = -1;
1326                     }
1327                 }
1328             }
1329         }
1330
1331       /* Finally, try the default path.  */
1332       if (fd == -1)
1333         fd = open_path (name, namelen, preloaded, rtld_search_dirs, &realname);
1334
1335       /* Add another newline when we a tracing the library loading.  */
1336       if (_dl_debug_libs)
1337         _dl_debug_message (1, "\n", NULL);
1338     }
1339   else
1340     {
1341       /* The path may contain dynamic string tokens.  */
1342       realname = (loader
1343                   ? expand_dynamic_string_token (loader, name)
1344                   : local_strdup (name));
1345       if (realname == NULL)
1346         fd = -1;
1347       else
1348         {
1349           fd = __open (realname, O_RDONLY);
1350           if (fd == -1)
1351             free (realname);
1352         }
1353     }
1354
1355   if (fd == -1)
1356     {
1357       if (trace_mode)
1358         {
1359           /* We haven't found an appropriate library.  But since we
1360              are only interested in the list of libraries this isn't
1361              so severe.  Fake an entry with all the information we
1362              have.  */
1363           static const ElfW(Symndx) dummy_bucket = STN_UNDEF;
1364
1365           /* Enter the new object in the list of loaded objects.  */
1366           if ((name_copy = local_strdup (name)) == NULL
1367               || (l = _dl_new_object (name_copy, name, type, loader)) == NULL)
1368             _dl_signal_error (ENOMEM, name,
1369                               "cannot create shared object descriptor");
1370           /* We use an opencount of 0 as a sign for the faked entry.
1371              Since the descriptor is initialized with zero we do not
1372              have do this here.
1373           l->l_opencount = 0;
1374           l->l_reserved = 0; */
1375           l->l_buckets = &dummy_bucket;
1376           l->l_nbuckets = 1;
1377           l->l_relocated = 1;
1378
1379           return l;
1380         }
1381       else
1382         _dl_signal_error (errno, name, "cannot open shared object file");
1383     }
1384
1385   return _dl_map_object_from_fd (name, fd, realname, loader, type);
1386 }