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