Update.
[platform/upstream/glibc.git] / elf / dl-load.c
1 /* Map in a shared object's segments from the file.
2    Copyright (C) 1995,96,97,98,99,2000,2001 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 Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the 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    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <elf.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <libintl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <ldsodefs.h>
28 #include <sys/mman.h>
29 #include <sys/param.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include "dynamic-link.h"
33 #include <abi-tag.h>
34 #include <dl-osinfo.h>
35
36 #include <dl-dst.h>
37
38 /* On some systems, no flag bits are given to specify file mapping.  */
39 #ifndef MAP_FILE
40 # define MAP_FILE       0
41 #endif
42
43 /* The right way to map in the shared library files is MAP_COPY, which
44    makes a virtual copy of the data at the time of the mmap call; this
45    guarantees the mapped pages will be consistent even if the file is
46    overwritten.  Some losing VM systems like Linux's lack MAP_COPY.  All we
47    get is MAP_PRIVATE, which copies each page when it is modified; this
48    means if the file is overwritten, we may at some point get some pages
49    from the new version after starting with pages from the old version.  */
50 #ifndef MAP_COPY
51 # define MAP_COPY       MAP_PRIVATE
52 #endif
53
54 /* Some systems link their relocatable objects for another base address
55    than 0.  We want to know the base address for these such that we can
56    subtract this address from the segment addresses during mapping.
57    This results in a more efficient address space usage.  Defaults to
58    zero for almost all systems.  */
59 #ifndef MAP_BASE_ADDR
60 # define MAP_BASE_ADDR(l)       0
61 #endif
62
63
64 #include <endian.h>
65 #if BYTE_ORDER == BIG_ENDIAN
66 # define byteorder ELFDATA2MSB
67 #elif BYTE_ORDER == LITTLE_ENDIAN
68 # define byteorder ELFDATA2LSB
69 #else
70 # error "Unknown BYTE_ORDER " BYTE_ORDER
71 # define byteorder ELFDATANONE
72 #endif
73
74 #define STRING(x) __STRING (x)
75
76 #ifdef MAP_ANON
77 /* The fd is not examined when using MAP_ANON.  */
78 # define ANONFD -1
79 #else
80 int _dl_zerofd = -1;
81 # define ANONFD _dl_zerofd
82 #endif
83
84 /* Handle situations where we have a preferred location in memory for
85    the shared objects.  */
86 #ifdef ELF_PREFERRED_ADDRESS_DATA
87 ELF_PREFERRED_ADDRESS_DATA;
88 #endif
89 #ifndef ELF_PREFERRED_ADDRESS
90 # define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
91 #endif
92 #ifndef ELF_FIXED_ADDRESS
93 # define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
94 #endif
95
96 /* Type for the buffer we put the ELF header and hopefully the program
97    header.  This buffer does not really have to be too large.  In most
98    cases the program header follows the ELF header directly.  If this
99    is not the case all bets are off and we can make the header arbitrarily
100    large and still won't get it read.  This means the only question is
101    how large are the ELF and program header combined.  The ELF header
102    in 64-bit files is 56 bytes long.  Each program header entry is again
103    56 bytes long.  I.e., even with a file which has 17 program header
104    entries we only have to read 1kB.  And 17 program header entries is
105    plenty, normal files have < 10.  If this heuristic should really fail
106    for some file the code in `_dl_map_object_from_fd' knows how to
107    recover.  */
108 struct filebuf
109 {
110   ssize_t len;
111   char buf[1024];
112 };
113
114 size_t _dl_pagesize;
115
116 unsigned int _dl_osversion;
117
118 int _dl_clktck;
119
120 extern const char *_dl_platform;
121 extern size_t _dl_platformlen;
122
123 /* The object to be initialized first.  */
124 struct link_map *_dl_initfirst;
125
126 /* This is the decomposed LD_LIBRARY_PATH search path.  */
127 static struct r_search_path_struct env_path_list;
128
129 /* List of the hardware capabilities we might end up using.  */
130 static const struct r_strlenpair *capstr;
131 static size_t ncapstr;
132 static size_t max_capstrlen;
133
134 const unsigned char _dl_pf_to_prot[8] =
135 {
136   [0] = PROT_NONE,
137   [PF_R] = PROT_READ,
138   [PF_W] = PROT_WRITE,
139   [PF_R | PF_W] = PROT_READ | PROT_WRITE,
140   [PF_X] = PROT_EXEC,
141   [PF_R | PF_X] = PROT_READ | PROT_EXEC,
142   [PF_W | PF_X] = PROT_WRITE | PROT_EXEC,
143   [PF_R | PF_W | PF_X] = PROT_READ | PROT_WRITE | PROT_EXEC
144 };
145
146
147 /* Get the generated information about the trusted directories.  */
148 #include "trusted-dirs.h"
149
150 static const char system_dirs[] = SYSTEM_DIRS;
151 static const size_t system_dirs_len[] =
152 {
153   SYSTEM_DIRS_LEN
154 };
155 #define nsystem_dirs_len \
156   (sizeof (system_dirs_len) / sizeof (system_dirs_len[0]))
157
158
159 /* Local version of `strdup' function.  */
160 static inline char *
161 local_strdup (const char *s)
162 {
163   size_t len = strlen (s) + 1;
164   void *new = malloc (len);
165
166   if (new == NULL)
167     return NULL;
168
169   return (char *) memcpy (new, s, len);
170 }
171
172
173 size_t
174 _dl_dst_count (const char *name, int is_path)
175 {
176   const char *const start = name;
177   size_t cnt = 0;
178
179   do
180     {
181       size_t len = 1;
182
183       /* $ORIGIN is not expanded for SUID/GUID programs (except if it
184          is $ORIGIN alone) and it must always appear first in path.
185
186          Note that it is no bug that the string in the second and
187          fourth `strncmp' call is longer than the sequence which is
188          actually tested.  */
189       if (((strncmp (&name[1], "{ORIGIN}", 8) == 0 && (len = 9) != 0)
190            || (strncmp (&name[1], "{ORIGIN}" + 1, 6) == 0
191                && (name[7] == '\0' || name[7] == '/'
192                    || (is_path && name[7] == ':'))
193                && (len = 7) != 0)))
194         {
195           if ((__builtin_expect (!__libc_enable_secure, 1)
196                || name[len] == '\0' || (is_path && name[len] == ':'))
197               && (name == start || (is_path && name[-1] == ':')))
198             ++cnt;
199         }
200       else if ((strncmp (&name[1], "{PLATFORM}", 10) == 0
201                 && (len = 11) != 0)
202                || (strncmp (&name[1], "{PLATFORM}" + 1, 8) == 0
203                    && (name[9] == '\0' || name[9] == '/'
204                        || (is_path && name[9] == ':'))
205                    && (len = 9) != 0))
206         ++cnt;
207
208       name = strchr (name + len, '$');
209     }
210   while (name != NULL);
211
212   return cnt;
213 }
214
215
216 char *
217 _dl_dst_substitute (struct link_map *l, const char *name, char *result,
218                     int is_path)
219 {
220   const char *const start = name;
221   char *last_elem, *wp;
222
223   /* Now fill the result path.  While copying over the string we keep
224      track of the start of the last path element.  When we come accross
225      a DST we copy over the value or (if the value is not available)
226      leave the entire path element out.  */
227   last_elem = wp = result;
228
229   do
230     {
231       if (__builtin_expect (*name, 'a') == '$')
232         {
233           const char *repl = NULL;
234           size_t len = 1;
235
236           /* Note that it is no bug that the string in the second and
237              fourth `strncmp' call is longer than the sequence which
238              is actually tested.  */
239           if (((strncmp (&name[1], "{ORIGIN}", 8) == 0 && (len = 9) != 0)
240                || (strncmp (&name[1], "{ORIGIN}" + 1, 6) == 0
241                    && (name[7] == '\0' || name[7] == '/'
242                        || (is_path && name[7] == ':'))
243                    && (len = 7) != 0)))
244             {
245               if ((__builtin_expect (!__libc_enable_secure, 1)
246                    || name[len] == '\0' || (is_path && name[len] == ':'))
247                   && (name == start || (is_path && name[-1] == ':')))
248                 repl = l->l_origin;
249             }
250           else if ((strncmp (&name[1], "{PLATFORM}", 10) == 0
251                     && (len = 11) != 0)
252                    || (strncmp (&name[1], "{PLATFORM}" + 1, 8) == 0
253                        && (name[9] == '\0' || name[9] == '/' || name[9] == ':')
254                        && (len = 9) != 0))
255             repl = _dl_platform;
256
257
258           if (repl != NULL && repl != (const char *) -1)
259             {
260               wp = __stpcpy (wp, repl);
261               name += len;
262             }
263           else if (len > 1)
264             {
265               /* We cannot use this path element, the value of the
266                  replacement is unknown.  */
267               wp = last_elem;
268               name += len;
269               while (*name != '\0' && (!is_path || *name != ':'))
270                 ++name;
271             }
272           else
273             /* No DST we recognize.  */
274             *wp++ = *name++;
275         }
276       else
277         {
278           *wp++ = *name++;
279           if (is_path && *name == ':')
280             last_elem = wp;
281         }
282     }
283   while (*name != '\0');
284
285   *wp = '\0';
286
287   return result;
288 }
289
290
291 /* Return copy of argument with all recognized dynamic string tokens
292    ($ORIGIN and $PLATFORM for now) replaced.  On some platforms it
293    might not be possible to determine the path from which the object
294    belonging to the map is loaded.  In this case the path element
295    containing $ORIGIN is left out.  */
296 static char *
297 expand_dynamic_string_token (struct link_map *l, const char *s)
298 {
299   /* We make two runs over the string.  First we determine how large the
300      resulting string is and then we copy it over.  Since this is now
301      frequently executed operation we are looking here not for performance
302      but rather for code size.  */
303   size_t cnt;
304   size_t total;
305   char *result;
306
307   /* Determine the number of DST elements.  */
308   cnt = DL_DST_COUNT (s, 1);
309
310   /* If we do not have to replace anything simply copy the string.  */
311   if (__builtin_expect (cnt, 0) == 0)
312     return local_strdup (s);
313
314   /* Determine the length of the substituted string.  */
315   total = DL_DST_REQUIRED (l, s, strlen (s), cnt);
316
317   /* Allocate the necessary memory.  */
318   result = (char *) malloc (total + 1);
319   if (result == NULL)
320     return NULL;
321
322   return DL_DST_SUBSTITUTE (l, s, result, 1);
323 }
324
325
326 /* Add `name' to the list of names for a particular shared object.
327    `name' is expected to have been allocated with malloc and will
328    be freed if the shared object already has this name.
329    Returns false if the object already had this name.  */
330 static void
331 internal_function
332 add_name_to_object (struct link_map *l, const char *name)
333 {
334   struct libname_list *lnp, *lastp;
335   struct libname_list *newname;
336   size_t name_len;
337
338   lastp = NULL;
339   for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
340     if (strcmp (name, lnp->name) == 0)
341       return;
342
343   name_len = strlen (name) + 1;
344   newname = (struct libname_list *) malloc (sizeof *newname + name_len);
345   if (newname == NULL)
346     {
347       /* No more memory.  */
348       _dl_signal_error (ENOMEM, name, NULL, N_("cannot allocate name record"));
349       return;
350     }
351   /* The object should have a libname set from _dl_new_object.  */
352   assert (lastp != NULL);
353
354   newname->name = memcpy (newname + 1, name, name_len);
355   newname->next = NULL;
356   newname->dont_free = 0;
357   lastp->next = newname;
358 }
359
360 /* All known directories in sorted order.  */
361 struct r_search_path_elem *_dl_all_dirs;
362
363 /* All directories after startup.  */
364 struct r_search_path_elem *_dl_init_all_dirs;
365
366 /* Standard search directories.  */
367 static struct r_search_path_struct rtld_search_dirs;
368
369 static size_t max_dirnamelen;
370
371 static inline struct r_search_path_elem **
372 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
373               int check_trusted, const char *what, const char *where)
374 {
375   char *cp;
376   size_t nelems = 0;
377
378   while ((cp = __strsep (&rpath, sep)) != NULL)
379     {
380       struct r_search_path_elem *dirp;
381       size_t len = strlen (cp);
382
383       /* `strsep' can pass an empty string.  This has to be
384          interpreted as `use the current directory'. */
385       if (len == 0)
386         {
387           static const char curwd[] = "./";
388           cp = (char *) curwd;
389         }
390
391       /* Remove trailing slashes (except for "/").  */
392       while (len > 1 && cp[len - 1] == '/')
393         --len;
394
395       /* Now add one if there is none so far.  */
396       if (len > 0 && cp[len - 1] != '/')
397         cp[len++] = '/';
398
399       /* Make sure we don't use untrusted directories if we run SUID.  */
400       if (__builtin_expect (check_trusted, 0))
401         {
402           const char *trun = system_dirs;
403           size_t idx;
404           int unsecure = 1;
405
406           /* All trusted directories must be complete names.  */
407           if (cp[0] == '/')
408             {
409               for (idx = 0; idx < nsystem_dirs_len; ++idx)
410                 {
411                   if (len == system_dirs_len[idx]
412                       && memcmp (trun, cp, len) == 0)
413                     {
414                       /* Found it.  */
415                       unsecure = 0;
416                       break;
417                     }
418
419                   trun += system_dirs_len[idx] + 1;
420                 }
421             }
422
423           if (unsecure)
424             /* Simply drop this directory.  */
425             continue;
426         }
427
428       /* See if this directory is already known.  */
429       for (dirp = _dl_all_dirs; dirp != NULL; dirp = dirp->next)
430         if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
431           break;
432
433       if (dirp != NULL)
434         {
435           /* It is available, see whether it's on our own list.  */
436           size_t cnt;
437           for (cnt = 0; cnt < nelems; ++cnt)
438             if (result[cnt] == dirp)
439               break;
440
441           if (cnt == nelems)
442             result[nelems++] = dirp;
443         }
444       else
445         {
446           size_t cnt;
447           enum r_dir_status init_val;
448           size_t where_len = where ? strlen (where) + 1 : 0;
449
450           /* It's a new directory.  Create an entry and add it.  */
451           dirp = (struct r_search_path_elem *)
452             malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status)
453                     + where_len + len + 1);
454           if (dirp == NULL)
455             _dl_signal_error (ENOMEM, NULL, NULL,
456                               N_("cannot create cache for search path"));
457
458           dirp->dirname = ((char *) dirp + sizeof (*dirp)
459                            + ncapstr * sizeof (enum r_dir_status));
460           *((char *) __mempcpy ((char *) dirp->dirname, cp, len)) = '\0';
461           dirp->dirnamelen = len;
462
463           if (len > max_dirnamelen)
464             max_dirnamelen = len;
465
466           /* We have to make sure all the relative directories are
467              never ignored.  The current directory might change and
468              all our saved information would be void.  */
469           init_val = cp[0] != '/' ? existing : unknown;
470           for (cnt = 0; cnt < ncapstr; ++cnt)
471             dirp->status[cnt] = init_val;
472
473           dirp->what = what;
474           if (__builtin_expect (where != NULL, 1))
475             dirp->where = memcpy ((char *) dirp + sizeof (*dirp) + len + 1
476                                   + ncapstr * sizeof (enum r_dir_status),
477                                   where, where_len);
478           else
479             dirp->where = NULL;
480
481           dirp->next = _dl_all_dirs;
482           _dl_all_dirs = dirp;
483
484           /* Put it in the result array.  */
485           result[nelems++] = dirp;
486         }
487     }
488
489   /* Terminate the array.  */
490   result[nelems] = NULL;
491
492   return result;
493 }
494
495
496 static void
497 internal_function
498 decompose_rpath (struct r_search_path_struct *sps,
499                  const char *rpath, struct link_map *l, const char *what)
500 {
501   /* Make a copy we can work with.  */
502   const char *where = l->l_name;
503   char *copy;
504   char *cp;
505   struct r_search_path_elem **result;
506   size_t nelems;
507
508   /* First see whether we must forget the RUNPATH and RPATH from this
509      object.  */
510   if (__builtin_expect (_dl_inhibit_rpath != NULL, 0) && !__libc_enable_secure)
511     {
512       const char *found = strstr (_dl_inhibit_rpath, where);
513       if (found != NULL)
514         {
515           size_t len = strlen (where);
516           if ((found == _dl_inhibit_rpath || found[-1] == ':')
517               && (found[len] == '\0' || found[len] == ':'))
518             {
519               /* This object is on the list of objects for which the
520                  RUNPATH and RPATH must not be used.  */
521               result = (struct r_search_path_elem **)
522                 malloc (sizeof (*result));
523               if (result == NULL)
524                 _dl_signal_error (ENOMEM, NULL, NULL,
525                                   N_("cannot create cache for search path"));
526               result[0] = NULL;
527
528               sps->dirs = result;
529               sps->malloced = 1;
530
531               return;
532             }
533         }
534     }
535
536   /* Make a writable copy.  At the same time expand possible dynamic
537      string tokens.  */
538   copy = expand_dynamic_string_token (l, rpath);
539   if (copy == NULL)
540     _dl_signal_error (ENOMEM, NULL, NULL,
541                       N_("cannot create RUNPATH/RPATH copy"));
542
543   /* Count the number of necessary elements in the result array.  */
544   nelems = 0;
545   for (cp = copy; *cp != '\0'; ++cp)
546     if (*cp == ':')
547       ++nelems;
548
549   /* Allocate room for the result.  NELEMS + 1 is an upper limit for the
550      number of necessary entries.  */
551   result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1)
552                                                   * sizeof (*result));
553   if (result == NULL)
554     _dl_signal_error (ENOMEM, NULL, NULL,
555                       N_("cannot create cache for search path"));
556
557   fillin_rpath (copy, result, ":", 0, what, where);
558
559   /* Free the copied RPATH string.  `fillin_rpath' make own copies if
560      necessary.  */
561   free (copy);
562
563   sps->dirs = result;
564   /* The caller will change this value if we haven't used a real malloc.  */
565   sps->malloced = 1;
566 }
567
568
569 void
570 internal_function
571 _dl_init_paths (const char *llp)
572 {
573   size_t idx;
574   const char *strp;
575   struct r_search_path_elem *pelem, **aelem;
576   size_t round_size;
577 #ifdef SHARED
578   struct link_map *l;
579 #endif
580
581   /* Fill in the information about the application's RPATH and the
582      directories addressed by the LD_LIBRARY_PATH environment variable.  */
583
584   /* Get the capabilities.  */
585   capstr = _dl_important_hwcaps (_dl_platform, _dl_platformlen,
586                                  &ncapstr, &max_capstrlen);
587
588   /* First set up the rest of the default search directory entries.  */
589   aelem = rtld_search_dirs.dirs = (struct r_search_path_elem **)
590     malloc ((nsystem_dirs_len + 1) * sizeof (struct r_search_path_elem *));
591   if (rtld_search_dirs.dirs == NULL)
592     _dl_signal_error (ENOMEM, NULL, NULL,
593                       N_("cannot create search path array"));
594
595   round_size = ((2 * sizeof (struct r_search_path_elem) - 1
596                  + ncapstr * sizeof (enum r_dir_status))
597                 / sizeof (struct r_search_path_elem));
598
599   rtld_search_dirs.dirs[0] = (struct r_search_path_elem *)
600     malloc ((sizeof (system_dirs) / sizeof (system_dirs[0]))
601             * round_size * sizeof (struct r_search_path_elem));
602   if (rtld_search_dirs.dirs[0] == NULL)
603     _dl_signal_error (ENOMEM, NULL, NULL,
604                       N_("cannot create cache for search path"));
605
606   rtld_search_dirs.malloced = 0;
607   pelem = _dl_all_dirs = rtld_search_dirs.dirs[0];
608   strp = system_dirs;
609   idx = 0;
610
611   do
612     {
613       size_t cnt;
614
615       *aelem++ = pelem;
616
617       pelem->what = "system search path";
618       pelem->where = NULL;
619
620       pelem->dirname = strp;
621       pelem->dirnamelen = system_dirs_len[idx];
622       strp += system_dirs_len[idx] + 1;
623
624       /* System paths must be absolute.  */
625       assert (pelem->dirname[0] == '/');
626       for (cnt = 0; cnt < ncapstr; ++cnt)
627         pelem->status[cnt] = unknown;
628
629       pelem->next = (++idx == nsystem_dirs_len ? NULL : (pelem + round_size));
630
631       pelem += round_size;
632     }
633   while (idx < nsystem_dirs_len);
634
635   max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
636   *aelem = NULL;
637
638 #ifdef SHARED
639   /* This points to the map of the main object.  */
640   l = _dl_loaded;
641   if (l != NULL)
642     {
643       assert (l->l_type != lt_loaded);
644
645       if (l->l_info[DT_RUNPATH])
646         {
647           /* Allocate room for the search path and fill in information
648              from RUNPATH.  */
649           decompose_rpath (&l->l_runpath_dirs,
650                            (const void *) (D_PTR (l, l_info[DT_STRTAB])
651                                            + l->l_info[DT_RUNPATH]->d_un.d_val),
652                            l, "RUNPATH");
653
654           /* The RPATH is ignored.  */
655           l->l_rpath_dirs.dirs = (void *) -1;
656         }
657       else
658         {
659           l->l_runpath_dirs.dirs = (void *) -1;
660
661           if (l->l_info[DT_RPATH])
662             {
663               /* Allocate room for the search path and fill in information
664                  from RPATH.  */
665               decompose_rpath (&l->l_rpath_dirs,
666                                (const void *) (D_PTR (l, l_info[DT_STRTAB])
667                                                + l->l_info[DT_RPATH]->d_un.d_val),
668                                l, "RPATH");
669               l->l_rpath_dirs.malloced = 0;
670             }
671           else
672             l->l_rpath_dirs.dirs = (void *) -1;
673         }
674     }
675 #endif  /* SHARED */
676
677   if (llp != NULL && *llp != '\0')
678     {
679       size_t nllp;
680       const char *cp = llp;
681       char *llp_tmp = strdupa (llp);
682
683       /* Decompose the LD_LIBRARY_PATH contents.  First determine how many
684          elements it has.  */
685       nllp = 1;
686       while (*cp)
687         {
688           if (*cp == ':' || *cp == ';')
689             ++nllp;
690           ++cp;
691         }
692
693       env_path_list.dirs = (struct r_search_path_elem **)
694         malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
695       if (env_path_list.dirs == NULL)
696         _dl_signal_error (ENOMEM, NULL, NULL,
697                           N_("cannot create cache for search path"));
698
699       (void) fillin_rpath (llp_tmp, env_path_list.dirs, ":;",
700                            __libc_enable_secure, "LD_LIBRARY_PATH", NULL);
701
702       if (env_path_list.dirs[0] == NULL)
703         {
704           free (env_path_list.dirs);
705           env_path_list.dirs = (void *) -1;
706         }
707
708       env_path_list.malloced = 0;
709     }
710   else
711     env_path_list.dirs = (void *) -1;
712
713   /* Remember the last search directory added at startup.  */
714   _dl_init_all_dirs = _dl_all_dirs;
715 }
716
717
718 /* Think twice before changing anything in this function.  It is placed
719    here and prepared using the `alloca' magic to prevent it from being
720    inlined.  The function is only called in case of an error.  But then
721    performance does not count.  The function used to be "inlinable" and
722    the compiled did so all the time.  This increased the code size for
723    absolutely no good reason.  */
724 #define LOSE(code, s) lose (code, fd, name, realname, l, s)
725 static void
726 __attribute__ ((noreturn))
727 lose (int code, int fd, const char *name, char *realname, struct link_map *l,
728       const char *msg)
729 {
730   /* The use of `alloca' here looks ridiculous but it helps.  The goal
731      is to avoid the function from being inlined.  There is no official
732      way to do this so we use this trick.  gcc never inlines functions
733      which use `alloca'.  */
734   int *a = alloca (sizeof (int));
735   a[0] = fd;
736   (void) __close (a[0]);
737   if (l != NULL)
738     {
739       /* Remove the stillborn object from the list and free it.  */
740       if (l->l_prev)
741         l->l_prev->l_next = l->l_next;
742       if (l->l_next)
743         l->l_next->l_prev = l->l_prev;
744       --_dl_nloaded;
745       free (l);
746     }
747   free (realname);
748   _dl_signal_error (code, name, NULL, msg);
749 }
750
751
752 /* Map in the shared object NAME, actually located in REALNAME, and already
753    opened on FD.  */
754
755 #ifndef EXTERNAL_MAP_FROM_FD
756 static
757 #endif
758 struct link_map *
759 _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp,
760                         char *realname, struct link_map *loader, int l_type,
761                         int mode)
762 {
763   struct link_map *l = NULL;
764
765   auto inline caddr_t map_segment (ElfW(Addr) mapstart, size_t len,
766                                    int prot, int fixed, off_t offset);
767
768   inline caddr_t map_segment (ElfW(Addr) mapstart, size_t len,
769                               int prot, int fixed, off_t offset)
770     {
771       caddr_t mapat = __mmap ((caddr_t) mapstart, len, prot,
772                               fixed|MAP_COPY|MAP_FILE,
773                               fd, offset);
774       if (mapat == MAP_FAILED)
775         LOSE (errno, N_("failed to map segment from shared object"));
776       return mapat;
777     }
778
779   const ElfW(Ehdr) *header;
780   const ElfW(Phdr) *phdr;
781   const ElfW(Phdr) *ph;
782   size_t maplength;
783   int type;
784   struct stat64 st;
785
786   /* Get file information.  */
787   if (__fxstat64 (_STAT_VER, fd, &st) < 0)
788     LOSE (errno, N_("cannot stat shared object"));
789
790   /* Look again to see if the real name matched another already loaded.  */
791   for (l = _dl_loaded; l; l = l->l_next)
792     if (l->l_ino == st.st_ino && l->l_dev == st.st_dev)
793       {
794         /* The object is already loaded.
795            Just bump its reference count and return it.  */
796         __close (fd);
797
798         /* If the name is not in the list of names for this object add
799            it.  */
800         free (realname);
801         add_name_to_object (l, name);
802
803         return l;
804       }
805
806   if (mode & RTLD_NOLOAD)
807     /* We are not supposed to load the object unless it is already
808        loaded.  So return now.  */
809     return NULL;
810
811   /* Print debugging message.  */
812   if (__builtin_expect (_dl_debug_mask & DL_DEBUG_FILES, 0))
813     _dl_debug_printf ("file=%s;  generating link map\n", name);
814
815   /* This is the ELF header.  We read it in `open_verify'.  */
816   header = (void *) fbp->buf;
817
818 #ifndef MAP_ANON
819 # define MAP_ANON 0
820   if (_dl_zerofd == -1)
821     {
822       _dl_zerofd = _dl_sysdep_open_zero_fill ();
823       if (_dl_zerofd == -1)
824         {
825           __close (fd);
826           _dl_signal_error (errno, NULL, NULL,
827                             N_("cannot open zero fill device"));
828         }
829     }
830 #endif
831
832   /* Enter the new object in the list of loaded objects.  */
833   l = _dl_new_object (realname, name, l_type, loader);
834   if (__builtin_expect (! l, 0))
835     LOSE (ENOMEM, N_("cannot create shared object descriptor"));
836
837   /* Extract the remaining details we need from the ELF header
838      and then read in the program header table.  */
839   l->l_entry = header->e_entry;
840   type = header->e_type;
841   l->l_phnum = header->e_phnum;
842
843   maplength = header->e_phnum * sizeof (ElfW(Phdr));
844   if (header->e_phoff + maplength <= fbp->len)
845     phdr = (void *) (fbp->buf + header->e_phoff);
846   else
847     {
848       phdr = alloca (maplength);
849       __lseek (fd, SEEK_SET, header->e_phoff);
850       if (__libc_read (fd, (void *) phdr, maplength) != maplength)
851         LOSE (errno, N_("cannot read file data"));
852     }
853
854   {
855     /* Scan the program header table, collecting its load commands.  */
856     struct loadcmd
857       {
858         ElfW(Addr) mapstart, mapend, dataend, allocend;
859         off_t mapoff;
860         int prot;
861       } loadcmds[l->l_phnum], *c;
862     size_t nloadcmds = 0;
863
864     /* The struct is initialized to zero so this is not necessary:
865     l->l_ld = 0;
866     l->l_phdr = 0;
867     l->l_addr = 0; */
868     for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
869       switch (ph->p_type)
870         {
871           /* These entries tell us where to find things once the file's
872              segments are mapped in.  We record the addresses it says
873              verbatim, and later correct for the run-time load address.  */
874         case PT_DYNAMIC:
875           l->l_ld = (void *) ph->p_vaddr;
876           l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
877           break;
878         case PT_PHDR:
879           l->l_phdr = (void *) ph->p_vaddr;
880           break;
881
882         case PT_LOAD:
883           /* A load command tells us to map in part of the file.
884              We record the load commands and process them all later.  */
885           if (ph->p_align % _dl_pagesize != 0)
886             LOSE (0, N_("ELF load command alignment not page-aligned"));
887           if ((ph->p_vaddr - ph->p_offset) % ph->p_align)
888             LOSE (0,
889                   N_("ELF load command address/offset not properly aligned"));
890           {
891             struct loadcmd *c = &loadcmds[nloadcmds++];
892             c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
893             c->mapend = ((ph->p_vaddr + ph->p_filesz + _dl_pagesize - 1)
894                          & ~(_dl_pagesize - 1));
895             c->dataend = ph->p_vaddr + ph->p_filesz;
896             c->allocend = ph->p_vaddr + ph->p_memsz;
897             c->mapoff = ph->p_offset & ~(ph->p_align - 1);
898
899             /* Optimize a common case.  */
900             if ((PF_R | PF_W | PF_X) == 7
901                 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7)
902               c->prot = _dl_pf_to_prot[ph->p_flags & (PF_R | PF_W | PF_X)];
903             else
904               {
905                 c->prot = 0;
906                 if (ph->p_flags & PF_R)
907                   c->prot |= PROT_READ;
908                 if (ph->p_flags & PF_W)
909                   c->prot |= PROT_WRITE;
910                 if (ph->p_flags & PF_X)
911                   c->prot |= PROT_EXEC;
912               }
913           }
914           break;
915         }
916
917     /* Now process the load commands and map segments into memory.  */
918     c = loadcmds;
919
920     /* Length of the sections to be loaded.  */
921     maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
922
923     if (__builtin_expect (type, ET_DYN) == ET_DYN)
924       {
925         /* This is a position-independent shared object.  We can let the
926            kernel map it anywhere it likes, but we must have space for all
927            the segments in their specified positions relative to the first.
928            So we map the first segment without MAP_FIXED, but with its
929            extent increased to cover all the segments.  Then we remove
930            access from excess portion, and there is known sufficient space
931            there to remap from the later segments.
932
933            As a refinement, sometimes we have an address that we would
934            prefer to map such objects at; but this is only a preference,
935            the OS can do whatever it likes. */
936         ElfW(Addr) mappref;
937         mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
938                    - MAP_BASE_ADDR (l));
939
940         /* Remember which part of the address space this object uses.  */
941         l->l_map_start = (ElfW(Addr)) map_segment (mappref, maplength, c->prot,
942                                                    0, c->mapoff);
943         l->l_map_end = l->l_map_start + maplength;
944         l->l_addr = l->l_map_start - c->mapstart;
945
946         /* Change protection on the excess portion to disallow all access;
947            the portions we do not remap later will be inaccessible as if
948            unallocated.  Then jump into the normal segment-mapping loop to
949            handle the portion of the segment past the end of the file
950            mapping.  */
951         __mprotect ((caddr_t) (l->l_addr + c->mapend),
952                     loadcmds[nloadcmds - 1].allocend - c->mapend,
953                     PROT_NONE);
954
955         goto postmap;
956       }
957     else
958       {
959         /* This object is loaded at a fixed address.  This must never
960            happen for objects loaded with dlopen().  */
961         if (__builtin_expect (mode & __RTLD_DLOPEN, 0))
962           {
963             LOSE (0, N_("cannot dynamically load executable"));
964           }
965
966         /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
967            fixed.  */
968         ELF_FIXED_ADDRESS (loader, c->mapstart);
969       }
970
971     /* Remember which part of the address space this object uses.  */
972     l->l_map_start = c->mapstart + l->l_addr;
973     l->l_map_end = l->l_map_start + maplength;
974
975     while (c < &loadcmds[nloadcmds])
976       {
977         if (c->mapend > c->mapstart)
978           /* Map the segment contents from the file.  */
979           map_segment (l->l_addr + c->mapstart, c->mapend - c->mapstart,
980                        c->prot, MAP_FIXED, c->mapoff);
981
982       postmap:
983         if (l->l_phdr == 0
984             && c->mapoff <= header->e_phoff
985             && (c->mapend - c->mapstart + c->mapoff
986                 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
987           /* Found the program header in this segment.  */
988           l->l_phdr = (void *) (c->mapstart + header->e_phoff - c->mapoff);
989
990         if (c->allocend > c->dataend)
991           {
992             /* Extra zero pages should appear at the end of this segment,
993                after the data mapped from the file.   */
994             ElfW(Addr) zero, zeroend, zeropage;
995
996             zero = l->l_addr + c->dataend;
997             zeroend = l->l_addr + c->allocend;
998             zeropage = (zero + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
999
1000             if (zeroend < zeropage)
1001               /* All the extra data is in the last page of the segment.
1002                  We can just zero it.  */
1003               zeropage = zeroend;
1004
1005             if (zeropage > zero)
1006               {
1007                 /* Zero the final part of the last page of the segment.  */
1008                 if ((c->prot & PROT_WRITE) == 0)
1009                   {
1010                     /* Dag nab it.  */
1011                     if (__mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
1012                                     _dl_pagesize, c->prot|PROT_WRITE) < 0)
1013                       LOSE (errno, N_("cannot change memory protections"));
1014                   }
1015                 memset ((void *) zero, '\0', zeropage - zero);
1016                 if ((c->prot & PROT_WRITE) == 0)
1017                   __mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
1018                               _dl_pagesize, c->prot);
1019               }
1020
1021             if (zeroend > zeropage)
1022               {
1023                 /* Map the remaining zero pages in from the zero fill FD.  */
1024                 caddr_t mapat;
1025                 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
1026                                 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
1027                                 ANONFD, 0);
1028                 if (mapat == MAP_FAILED)
1029                   LOSE (errno, N_("cannot map zero-fill pages"));
1030               }
1031           }
1032
1033         ++c;
1034       }
1035
1036     if (l->l_phdr == NULL)
1037       {
1038         /* The program header is not contained in any of the segments.
1039            We have to allocate memory ourself and copy it over from
1040            out temporary place.  */
1041         ElfW(Phdr) *newp = (ElfW(Phdr) *) malloc (header->e_phnum
1042                                                   * sizeof (ElfW(Phdr)));
1043         if (newp == NULL)
1044           LOSE (ENOMEM, N_("cannot allocate memory for program header"));
1045
1046         l->l_phdr = memcpy (newp, phdr,
1047                             (header->e_phnum * sizeof (ElfW(Phdr))));
1048         l->l_phdr_allocated = 1;
1049       }
1050     else
1051       /* Adjust the PT_PHDR value by the runtime load address.  */
1052       (ElfW(Addr)) l->l_phdr += l->l_addr;
1053   }
1054
1055   /* We are done mapping in the file.  We no longer need the descriptor.  */
1056   __close (fd);
1057
1058   if (l->l_type == lt_library && type == ET_EXEC)
1059     l->l_type = lt_executable;
1060
1061   if (l->l_ld == 0)
1062     {
1063       if (type == ET_DYN)
1064         LOSE (0, N_("object file has no dynamic section"));
1065     }
1066   else
1067     (ElfW(Addr)) l->l_ld += l->l_addr;
1068
1069   l->l_entry += l->l_addr;
1070
1071   if (__builtin_expect (_dl_debug_mask & DL_DEBUG_FILES, 0))
1072     _dl_debug_printf ("  dynamic: 0x%0*lx  base: 0x%0*lx   size: 0x%0*Zx\n"
1073                       "    entry: 0x%0*lx  phdr: 0x%0*lx  phnum:   %*u\n\n",
1074                       (int) sizeof (void *) * 2, (unsigned long int) l->l_ld,
1075                       (int) sizeof (void *) * 2, (unsigned long int) l->l_addr,
1076                       (int) sizeof (void *) * 2, maplength,
1077                       (int) sizeof (void *) * 2, (unsigned long int) l->l_entry,
1078                       (int) sizeof (void *) * 2, (unsigned long int) l->l_phdr,
1079                       (int) sizeof (void *) * 2, l->l_phnum);
1080
1081   elf_get_dynamic_info (l);
1082
1083   /* Make sure we are dlopen()ing an object which has the DF_1_NOOPEN
1084      flag set.  */
1085   if (__builtin_expect (l->l_flags_1 & DF_1_NOOPEN, 0)
1086       && (mode & __RTLD_DLOPEN))
1087     {
1088       /* Remove from the module list.  */
1089       assert (l->l_next == NULL);
1090 #ifndef SHARED
1091       if (l->l_prev == NULL)
1092         /* No other module loaded.  */
1093         _dl_loaded = NULL;
1094       else
1095 #endif
1096         l->l_prev->l_next = NULL;
1097       --_dl_nloaded;
1098
1099       /* We are not supposed to load this object.  Free all resources.  */
1100       __munmap ((void *) l->l_map_start, l->l_map_end - l->l_map_start);
1101
1102       if (!l->l_libname->dont_free)
1103         free (l->l_libname);
1104
1105       if (l->l_phdr_allocated)
1106         free ((void *) l->l_phdr);
1107
1108       free (l);
1109
1110       _dl_signal_error (0, name, NULL,
1111                         N_("shared object cannot be dlopen()ed"));
1112     }
1113
1114   if (l->l_info[DT_HASH])
1115     _dl_setup_hash (l);
1116
1117   /* If this object has DT_SYMBOLIC set modify now its scope.  We don't
1118      have to do this for the main map.  */
1119   if (__builtin_expect (l->l_info[DT_SYMBOLIC] != NULL, 0)
1120       && &l->l_searchlist != l->l_scope[0])
1121     {
1122       /* Create an appropriate searchlist.  It contains only this map.
1123
1124          XXX This is the definition of DT_SYMBOLIC in SysVr4.  The old
1125          GNU ld.so implementation had a different interpretation which
1126          is more reasonable.  We are prepared to add this possibility
1127          back as part of a GNU extension of the ELF format.  */
1128       l->l_symbolic_searchlist.r_list =
1129         (struct link_map **) malloc (sizeof (struct link_map *));
1130
1131       if (l->l_symbolic_searchlist.r_list == NULL)
1132         LOSE (ENOMEM, N_("cannot create searchlist"));
1133
1134       l->l_symbolic_searchlist.r_list[0] = l;
1135       l->l_symbolic_searchlist.r_nlist = 1;
1136
1137       /* Now move the existing entries one back.  */
1138       memmove (&l->l_scope[1], &l->l_scope[0],
1139                (l->l_scope_max - 1) * sizeof (l->l_scope[0]));
1140
1141       /* Now add the new entry.  */
1142       l->l_scope[0] = &l->l_symbolic_searchlist;
1143     }
1144
1145   /* Remember whether this object must be initialized first.  */
1146   if (__builtin_expect (l->l_flags_1 & DF_1_INITFIRST, 0))
1147     _dl_initfirst = l;
1148
1149   /* Finally the file information.  */
1150   l->l_dev = st.st_dev;
1151   l->l_ino = st.st_ino;
1152
1153   return l;
1154 }
1155 \f
1156 /* Print search path.  */
1157 static void
1158 print_search_path (struct r_search_path_elem **list,
1159                    const char *what, const char *name)
1160 {
1161   char buf[max_dirnamelen + max_capstrlen];
1162   int first = 1;
1163
1164   _dl_debug_printf (" search path=");
1165
1166   while (*list != NULL && (*list)->what == what) /* Yes, ==.  */
1167     {
1168       char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1169       size_t cnt;
1170
1171       for (cnt = 0; cnt < ncapstr; ++cnt)
1172         if ((*list)->status[cnt] != nonexisting)
1173           {
1174             char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
1175             if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1176               cp[0] = '\0';
1177             else
1178               cp[-1] = '\0';
1179             if (first)
1180               {
1181                 _dl_debug_printf_c ("%s", buf);
1182                 first = 0;
1183               }
1184             else
1185               _dl_debug_printf_c (":%s", buf);
1186           }
1187
1188       ++list;
1189     }
1190
1191   if (name != NULL)
1192     _dl_debug_printf_c ("\t\t(%s from file %s)\n", what,
1193                         name[0] ? name : _dl_argv[0]);
1194   else
1195     _dl_debug_printf_c ("\t\t(%s)\n", what);
1196 }
1197 \f
1198 /* Open a file and verify it is an ELF file for this architecture.  We
1199    ignore only ELF files for other architectures.  Non-ELF files and
1200    ELF files with different header information cause fatal errors since
1201    this could mean there is something wrong in the installation and the
1202    user might want to know about this.  */
1203 static int
1204 open_verify (const char *name, struct filebuf *fbp)
1205 {
1206   /* This is the expected ELF header.  */
1207 #define ELF32_CLASS ELFCLASS32
1208 #define ELF64_CLASS ELFCLASS64
1209 #ifndef VALID_ELF_HEADER
1210 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
1211 # define VALID_ELF_OSABI(osabi)         (osabi == ELFOSABI_SYSV)
1212 # define VALID_ELF_ABIVERSION(ver)      (ver == 0)
1213 #endif
1214   static const unsigned char expected[EI_PAD] =
1215   {
1216     [EI_MAG0] = ELFMAG0,
1217     [EI_MAG1] = ELFMAG1,
1218     [EI_MAG2] = ELFMAG2,
1219     [EI_MAG3] = ELFMAG3,
1220     [EI_CLASS] = ELFW(CLASS),
1221     [EI_DATA] = byteorder,
1222     [EI_VERSION] = EV_CURRENT,
1223     [EI_OSABI] = ELFOSABI_SYSV,
1224     [EI_ABIVERSION] = 0
1225   };
1226   static const struct {
1227     ElfW(Word) vendorlen, datalen, type;
1228     char vendor[4];
1229   } expected_note = { 4, 16, 1, "GNU" };
1230   int fd;
1231
1232   /* Open the file.  We always open files read-only.  */
1233   fd = __open (name, O_RDONLY);
1234   if (fd != -1)
1235     {
1236       ElfW(Ehdr) *ehdr;
1237       ElfW(Phdr) *phdr, *ph;
1238       ElfW(Word) *abi_note, abi_note_buf[8];
1239       unsigned int osversion;
1240       size_t maplength;
1241
1242       /* We successfully openened the file.  Now verify it is a file
1243          we can use.  */
1244       __set_errno (0);
1245       fbp->len = __libc_read (fd, fbp->buf, sizeof (fbp->buf));
1246
1247       /* This is where the ELF header is loaded.  */
1248       assert (sizeof (fbp->buf) > sizeof (ElfW(Ehdr)));
1249       ehdr = (ElfW(Ehdr) *) fbp->buf;
1250
1251       /* Now run the tests.  */
1252       if (__builtin_expect (fbp->len < (ssize_t) sizeof (ElfW(Ehdr)), 0))
1253         lose (errno, fd, name, NULL, NULL,
1254               errno == 0 ? N_("file too short") : N_("cannot read file data"));
1255
1256       /* See whether the ELF header is what we expect.  */
1257       if (__builtin_expect (! VALID_ELF_HEADER (ehdr->e_ident, expected,
1258                                                 EI_PAD), 0))
1259         {
1260           /* Something is wrong.  */
1261           if (*(Elf32_Word *) &ehdr->e_ident !=
1262 #if BYTE_ORDER == LITTLE_ENDIAN
1263               ((ELFMAG0 << (EI_MAG0 * 8)) |
1264                (ELFMAG1 << (EI_MAG1 * 8)) |
1265                (ELFMAG2 << (EI_MAG2 * 8)) |
1266                (ELFMAG3 << (EI_MAG3 * 8)))
1267 #else
1268               ((ELFMAG0 << (EI_MAG3 * 8)) |
1269                (ELFMAG1 << (EI_MAG2 * 8)) |
1270                (ELFMAG2 << (EI_MAG1 * 8)) |
1271                (ELFMAG3 << (EI_MAG0 * 8)))
1272 #endif
1273               )
1274             lose (0, fd, name, NULL, NULL, N_("invalid ELF header"));
1275
1276           if (ehdr->e_ident[EI_CLASS] != ELFW(CLASS))
1277             /* This is not a fatal error.  On architectures where
1278                32-bit and 64-bit binaries can be run this might
1279                happen.  */
1280             goto close_and_out;
1281
1282           if (ehdr->e_ident[EI_DATA] != byteorder)
1283             {
1284               if (BYTE_ORDER == BIG_ENDIAN)
1285                 lose (0, fd, name, NULL, NULL,
1286                       "ELF file data encoding not big-endian");
1287               else
1288                 lose (0, fd, name, NULL, NULL,
1289                       "ELF file data encoding not little-endian");
1290             }
1291           if (ehdr->e_ident[EI_VERSION] != EV_CURRENT)
1292             lose (0, fd, name, NULL, NULL,
1293                   N_("ELF file version ident does not match current one"));
1294           /* XXX We should be able so set system specific versions which are
1295              allowed here.  */
1296           if (!VALID_ELF_OSABI (ehdr->e_ident[EI_OSABI]))
1297             lose (0, fd, name, NULL, NULL, N_("ELF file OS ABI invalid"));
1298           if (!VALID_ELF_ABIVERSION (ehdr->e_ident[EI_ABIVERSION]))
1299             lose (0, fd, name, NULL, NULL,
1300                   N_("ELF file ABI version invalid"));
1301           lose (0, fd, name, NULL, NULL, N_("internal error"));
1302         }
1303
1304       if (__builtin_expect (ehdr->e_version, EV_CURRENT) != EV_CURRENT)
1305         lose (0, fd, name, NULL, NULL,
1306               N_("ELF file version does not match current one"));
1307       if (! __builtin_expect (elf_machine_matches_host (ehdr), 1))
1308         goto close_and_out;
1309       else if (__builtin_expect (ehdr->e_phentsize, sizeof (ElfW(Phdr)))
1310                != sizeof (ElfW(Phdr)))
1311         lose (0, fd, name, NULL, NULL,
1312               N_("ELF file's phentsize not the expected size"));
1313       else if (__builtin_expect (ehdr->e_type, ET_DYN) != ET_DYN
1314                && __builtin_expect (ehdr->e_type, ET_EXEC) != ET_EXEC)
1315         lose (0, fd, name, NULL, NULL,
1316               N_("only ET_DYN and ET_EXEC can be loaded"));
1317
1318       maplength = ehdr->e_phnum * sizeof (ElfW(Phdr));
1319       if (ehdr->e_phoff + maplength <= fbp->len)
1320         phdr = (void *) (fbp->buf + ehdr->e_phoff);
1321       else
1322         {
1323           phdr = alloca (maplength);
1324           __lseek (fd, SEEK_SET, ehdr->e_phoff);
1325           if (__libc_read (fd, (void *) phdr, maplength) != maplength)
1326             lose (errno, fd, name, NULL, NULL, N_("cannot read file data"));
1327         }
1328
1329       /* Check .note.ABI-tag if present.  */
1330       for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph)
1331         if (ph->p_type == PT_NOTE && ph->p_filesz == 32 && ph->p_align >= 4)
1332           {
1333             if (ph->p_offset + 32 <= fbp->len)
1334               abi_note = (void *) (fbp->buf + ph->p_offset);
1335             else
1336               {
1337                 __lseek (fd, SEEK_SET, ph->p_offset);
1338                 if (__libc_read (fd, (void *) abi_note_buf, 32) != 32)
1339                   lose (errno, fd, name, NULL, NULL,
1340                         N_("cannot read file data"));
1341                 abi_note = abi_note_buf;
1342               }
1343
1344             if (memcmp (abi_note, &expected_note, sizeof (expected_note)))
1345               continue;
1346
1347             osversion = (abi_note[5] & 0xff) * 65536
1348                         + (abi_note[6] & 0xff) * 256
1349                         + (abi_note[7] & 0xff);
1350             if (abi_note[4] != __ABI_TAG_OS
1351                 || (_dl_osversion && _dl_osversion < osversion))
1352               {
1353               close_and_out:
1354                 __close (fd);
1355                 __set_errno (ENOENT);
1356                 fd = -1;
1357               }
1358
1359             break;
1360           }
1361     }
1362
1363   return fd;
1364 }
1365 \f
1366 /* Try to open NAME in one of the directories in *DIRSP.
1367    Return the fd, or -1.  If successful, fill in *REALNAME
1368    with the malloc'd full directory name.  If it turns out
1369    that none of the directories in *DIRSP exists, *DIRSP is
1370    replaced with (void *) -1, and the old value is free()d
1371    if MAY_FREE_DIRS is true.  */
1372
1373 static int
1374 open_path (const char *name, size_t namelen, int preloaded,
1375            struct r_search_path_struct *sps, char **realname,
1376            struct filebuf *fbp)
1377 {
1378   struct r_search_path_elem **dirs = sps->dirs;
1379   char *buf;
1380   int fd = -1;
1381   const char *current_what = NULL;
1382   int any = 0;
1383
1384   buf = alloca (max_dirnamelen + max_capstrlen + namelen);
1385   do
1386     {
1387       struct r_search_path_elem *this_dir = *dirs;
1388       size_t buflen = 0;
1389       size_t cnt;
1390       char *edp;
1391       int here_any = 0;
1392       int err;
1393
1394       /* If we are debugging the search for libraries print the path
1395          now if it hasn't happened now.  */
1396       if (__builtin_expect (_dl_debug_mask & DL_DEBUG_LIBS, 0)
1397           && current_what != this_dir->what)
1398         {
1399           current_what = this_dir->what;
1400           print_search_path (dirs, current_what, this_dir->where);
1401         }
1402
1403       edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
1404       for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
1405         {
1406           /* Skip this directory if we know it does not exist.  */
1407           if (this_dir->status[cnt] == nonexisting)
1408             continue;
1409
1410           buflen =
1411             ((char *) __mempcpy (__mempcpy (edp,
1412                                             capstr[cnt].str, capstr[cnt].len),
1413                                  name, namelen)
1414              - buf);
1415
1416           /* Print name we try if this is wanted.  */
1417           if (__builtin_expect (_dl_debug_mask & DL_DEBUG_LIBS, 0))
1418             _dl_debug_printf ("  trying file=%s\n", buf);
1419
1420           fd = open_verify (buf, fbp);
1421           if (this_dir->status[cnt] == unknown)
1422             {
1423               if (fd != -1)
1424                 this_dir->status[cnt] = existing;
1425               else
1426                 {
1427                   /* We failed to open machine dependent library.  Let's
1428                      test whether there is any directory at all.  */
1429                   struct stat64 st;
1430
1431                   buf[buflen - namelen - 1] = '\0';
1432
1433                   if (__xstat64 (_STAT_VER, buf, &st) != 0
1434                       || ! S_ISDIR (st.st_mode))
1435                     /* The directory does not exist or it is no directory.  */
1436                     this_dir->status[cnt] = nonexisting;
1437                   else
1438                     this_dir->status[cnt] = existing;
1439                 }
1440             }
1441
1442           /* Remember whether we found any existing directory.  */
1443           here_any |= this_dir->status[cnt] == existing;
1444
1445           if (fd != -1 && __builtin_expect (preloaded, 0)
1446               && __libc_enable_secure)
1447             {
1448               /* This is an extra security effort to make sure nobody can
1449                  preload broken shared objects which are in the trusted
1450                  directories and so exploit the bugs.  */
1451               struct stat64 st;
1452
1453               if (__fxstat64 (_STAT_VER, fd, &st) != 0
1454                   || (st.st_mode & S_ISUID) == 0)
1455                 {
1456                   /* The shared object cannot be tested for being SUID
1457                      or this bit is not set.  In this case we must not
1458                      use this object.  */
1459                   __close (fd);
1460                   fd = -1;
1461                   /* We simply ignore the file, signal this by setting
1462                      the error value which would have been set by `open'.  */
1463                   errno = ENOENT;
1464                 }
1465             }
1466         }
1467
1468       if (fd != -1)
1469         {
1470           *realname = (char *) malloc (buflen);
1471           if (*realname != NULL)
1472             {
1473               memcpy (*realname, buf, buflen);
1474               return fd;
1475             }
1476           else
1477             {
1478               /* No memory for the name, we certainly won't be able
1479                  to load and link it.  */
1480               __close (fd);
1481               return -1;
1482             }
1483         }
1484       if (here_any && (err = errno) != ENOENT && err != EACCES)
1485         /* The file exists and is readable, but something went wrong.  */
1486         return -1;
1487
1488       /* Remember whether we found anything.  */
1489       any |= here_any;
1490     }
1491   while (*++dirs != NULL);
1492
1493   /* Remove the whole path if none of the directories exists.  */
1494   if (__builtin_expect (! any, 0))
1495     {
1496       /* Paths which were allocated using the minimal malloc() in ld.so
1497          must not be freed using the general free() in libc.  */
1498       if (sps->malloced)
1499         free (sps->dirs);
1500       sps->dirs = (void *) -1;
1501     }
1502
1503   return -1;
1504 }
1505
1506 /* Map in the shared object file NAME.  */
1507
1508 struct link_map *
1509 internal_function
1510 _dl_map_object (struct link_map *loader, const char *name, int preloaded,
1511                 int type, int trace_mode, int mode)
1512 {
1513   int fd;
1514   char *realname;
1515   char *name_copy;
1516   struct link_map *l;
1517   struct filebuf fb;
1518
1519   /* Look for this name among those already loaded.  */
1520   for (l = _dl_loaded; l; l = l->l_next)
1521     {
1522       /* If the requested name matches the soname of a loaded object,
1523          use that object.  Elide this check for names that have not
1524          yet been opened.  */
1525       if (__builtin_expect (l->l_faked, 0) != 0)
1526         continue;
1527       if (!_dl_name_match_p (name, l))
1528         {
1529           const char *soname;
1530
1531           if (__builtin_expect (l->l_soname_added, 1)
1532               || l->l_info[DT_SONAME] == NULL)
1533             continue;
1534
1535           soname = ((const char *) D_PTR (l, l_info[DT_STRTAB])
1536                     + l->l_info[DT_SONAME]->d_un.d_val);
1537           if (strcmp (name, soname) != 0)
1538             continue;
1539
1540           /* We have a match on a new name -- cache it.  */
1541           add_name_to_object (l, soname);
1542           l->l_soname_added = 1;
1543         }
1544
1545       /* We have a match.  */
1546       return l;
1547     }
1548
1549   /* Display information if we are debugging.  */
1550   if (__builtin_expect (_dl_debug_mask & DL_DEBUG_FILES, 0) && loader != NULL)
1551     _dl_debug_printf ("\nfile=%s;  needed by %s\n", name,
1552                       loader->l_name[0] ? loader->l_name : _dl_argv[0]);
1553
1554   if (strchr (name, '/') == NULL)
1555     {
1556       /* Search for NAME in several places.  */
1557
1558       size_t namelen = strlen (name) + 1;
1559
1560       if (__builtin_expect (_dl_debug_mask & DL_DEBUG_LIBS, 0))
1561         _dl_debug_printf ("find library=%s; searching\n", name);
1562
1563       fd = -1;
1564
1565       /* When the object has the RUNPATH information we don't use any
1566          RPATHs.  */
1567       if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL)
1568         {
1569           /* First try the DT_RPATH of the dependent object that caused NAME
1570              to be loaded.  Then that object's dependent, and on up.  */
1571           for (l = loader; fd == -1 && l; l = l->l_loader)
1572             {
1573               if (l->l_rpath_dirs.dirs == NULL)
1574                 {
1575                   if (l->l_info[DT_RPATH] == NULL)
1576                     {
1577                       /* There is no path.  */
1578                       l->l_rpath_dirs.dirs = (void *) -1;
1579                       continue;
1580                     }
1581                   else
1582                     {
1583                       /* Make sure the cache information is available.  */
1584                       size_t ptrval = (D_PTR (l, l_info[DT_STRTAB])
1585                                        + l->l_info[DT_RPATH]->d_un.d_val);
1586                       decompose_rpath (&l->l_rpath_dirs,
1587                                        (const char *) ptrval, l, "RPATH");
1588                     }
1589                 }
1590
1591               if (l->l_rpath_dirs.dirs != (void *) -1)
1592                 fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
1593                                 &realname, &fb);
1594             }
1595
1596           /* If dynamically linked, try the DT_RPATH of the executable
1597              itself.  */
1598           l = _dl_loaded;
1599           if (fd == -1 && l && l->l_type != lt_loaded && l != loader
1600               && l->l_rpath_dirs.dirs != (void *) -1)
1601             fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
1602                             &realname, &fb);
1603         }
1604
1605       /* Try the LD_LIBRARY_PATH environment variable.  */
1606       if (fd == -1 && env_path_list.dirs != (void *) -1)
1607         fd = open_path (name, namelen, preloaded, &env_path_list,
1608                         &realname, &fb);
1609
1610       /* Look at the RUNPATH information for this binary.
1611
1612          Note that this is no real loop.  'while' is used only to enable
1613          us to use 'break' instead of a 'goto' to jump to the end.  The
1614          loop is always left after the first round.  */
1615       while (fd == -1 && loader != NULL
1616              && loader->l_runpath_dirs.dirs != (void *) -1)
1617         {
1618           if (loader->l_runpath_dirs.dirs == NULL)
1619             {
1620               if (loader->l_info[DT_RUNPATH] == NULL)
1621                 {
1622                   /* No RUNPATH.  */
1623                   loader->l_runpath_dirs.dirs = (void *) -1;
1624                   break;
1625                 }
1626               else
1627                 {
1628                   /* Make sure the cache information is available.  */
1629                   size_t ptrval = (D_PTR (loader, l_info[DT_STRTAB])
1630                                    + loader->l_info[DT_RUNPATH]->d_un.d_val);
1631                   decompose_rpath (&loader->l_runpath_dirs,
1632                                    (const char *) ptrval, loader, "RUNPATH");
1633                 }
1634             }
1635
1636           if (loader->l_runpath_dirs.dirs != (void *) -1)
1637             fd = open_path (name, namelen, preloaded,
1638                             &loader->l_runpath_dirs, &realname, &fb);
1639           break;
1640         }
1641
1642       if (fd == -1
1643           && (__builtin_expect (! preloaded, 1) || ! __libc_enable_secure))
1644         {
1645           /* Check the list of libraries in the file /etc/ld.so.cache,
1646              for compatibility with Linux's ldconfig program.  */
1647           const char *cached = _dl_load_cache_lookup (name);
1648
1649           if (cached != NULL)
1650             {
1651 #ifdef SHARED
1652               l = loader ?: _dl_loaded;
1653 #else
1654               l = loader;
1655 #endif
1656
1657               /* If the loader has the DF_1_NODEFLIB flag set we must not
1658                  use a cache entry from any of these directories.  */
1659               if (
1660 #ifndef SHARED
1661                   /* 'l' is always != NULL for dynamically linked objects.  */
1662                   l != NULL &&
1663 #endif
1664                   __builtin_expect (l->l_flags_1 & DF_1_NODEFLIB, 0))
1665                 {
1666                   const char *dirp = system_dirs;
1667                   unsigned int cnt = 0;
1668
1669                   do
1670                     {
1671                       if (memcmp (cached, dirp, system_dirs_len[cnt]) == 0)
1672                         {
1673                           /* The prefix matches.  Don't use the entry.  */
1674                           cached = NULL;
1675                           break;
1676                         }
1677
1678                       dirp += system_dirs_len[cnt] + 1;
1679                       ++cnt;
1680                     }
1681                   while (cnt < nsystem_dirs_len);
1682                 }
1683
1684               if (cached != NULL)
1685                 {
1686                   fd = open_verify (cached, &fb);
1687                   if (__builtin_expect (fd != -1, 1))
1688                     {
1689                       realname = local_strdup (cached);
1690                       if (realname == NULL)
1691                         {
1692                           __close (fd);
1693                           fd = -1;
1694                         }
1695                     }
1696                 }
1697             }
1698         }
1699
1700       /* Finally, try the default path.  */
1701       if (fd == -1
1702           && ((l = loader ?: _dl_loaded)
1703               /* 'l' is always != NULL for dynamically linked objects.  */
1704 #ifdef SHARED
1705               ,
1706 #else
1707               == NULL ||
1708 #endif
1709               __builtin_expect (!(l->l_flags_1 & DF_1_NODEFLIB), 1))
1710           && rtld_search_dirs.dirs != (void *) -1)
1711         fd = open_path (name, namelen, preloaded, &rtld_search_dirs,
1712                         &realname, &fb);
1713
1714       /* Add another newline when we a tracing the library loading.  */
1715       if (__builtin_expect (_dl_debug_mask & DL_DEBUG_LIBS, 0))
1716         _dl_debug_printf ("\n");
1717     }
1718   else
1719     {
1720       /* The path may contain dynamic string tokens.  */
1721       realname = (loader
1722                   ? expand_dynamic_string_token (loader, name)
1723                   : local_strdup (name));
1724       if (realname == NULL)
1725         fd = -1;
1726       else
1727         {
1728           fd = open_verify (realname, &fb);
1729           if (__builtin_expect (fd, 0) == -1)
1730             free (realname);
1731         }
1732     }
1733
1734   if (__builtin_expect (fd, 0) == -1)
1735     {
1736       if (trace_mode)
1737         {
1738           /* We haven't found an appropriate library.  But since we
1739              are only interested in the list of libraries this isn't
1740              so severe.  Fake an entry with all the information we
1741              have.  */
1742           static const Elf_Symndx dummy_bucket = STN_UNDEF;
1743
1744           /* Enter the new object in the list of loaded objects.  */
1745           if ((name_copy = local_strdup (name)) == NULL
1746               || (l = _dl_new_object (name_copy, name, type, loader)) == NULL)
1747             _dl_signal_error (ENOMEM, name, NULL,
1748                               N_("cannot create shared object descriptor"));
1749           /* Signal that this is a faked entry.  */
1750           l->l_faked = 1;
1751           /* Since the descriptor is initialized with zero we do not
1752              have do this here.
1753           l->l_reserved = 0; */
1754           l->l_buckets = &dummy_bucket;
1755           l->l_nbuckets = 1;
1756           l->l_relocated = 1;
1757
1758           return l;
1759         }
1760       else
1761         _dl_signal_error (errno, name, NULL,
1762                           N_("cannot open shared object file"));
1763     }
1764
1765   return _dl_map_object_from_fd (name, fd, &fb, realname, loader, type, mode);
1766 }