Sun Jul 28 23:46:14 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[platform/upstream/linaro-glibc.git] / sysdeps / generic / dl-sysdep.c
1 /* Operating system support for run-time dynamic linker.  Generic Unix version.
2 Copyright (C) 1995, 96 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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA.  */
19
20 #include <elf.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24 #include <fcntl.h>
25 #include <link.h>
26 #include <unistd.h>
27 #include <stdarg.h>
28 #include <string.h>
29
30
31 extern int _dl_argc;
32 extern char **_dl_argv;
33 extern char **_environ;
34 extern void _start (void);
35
36 int __libc_enable_secure;
37
38 ElfW(Addr)
39 _dl_sysdep_start (void **start_argptr,
40                   void (*dl_main) (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
41                                    ElfW(Addr) *user_entry))
42 {
43   const ElfW(Phdr) *phdr;
44   ElfW(Word) phnum;
45   ElfW(Addr) user_entry;
46   ElfW(auxv_t) *av;
47   uid_t uid, euid;
48   gid_t gid, egid;
49   unsigned int seen;
50
51   user_entry = (ElfW(Addr)) &_start;
52   _dl_argc = *(long *) start_argptr;
53   _dl_argv = (char **) start_argptr + 1;
54   _environ = &_dl_argv[_dl_argc + 1];
55   start_argptr = (void **) _environ;
56   while (*start_argptr)
57     ++start_argptr;
58
59   seen = 0;
60 #define M(type) (1 << (type))
61
62   for (av = (void *) ++start_argptr;
63        av->a_type != AT_NULL;
64        seen |= M ((++av)->a_type))
65     switch (av->a_type)
66       {
67       case AT_PHDR:
68         phdr = av->a_un.a_ptr;
69         break;
70       case AT_PHNUM:
71         phnum = av->a_un.a_val;
72         break;
73       case AT_ENTRY:
74         user_entry = av->a_un.a_val;
75         break;
76       case AT_UID:
77         uid = av->a_un.a_val;
78         break;
79       case AT_GID:
80         gid = av->a_un.a_val;
81         break;
82       case AT_EUID:
83         euid = av->a_un.a_val;
84         break;
85       case AT_EGID:
86         egid = av->a_un.a_val;
87         break;
88       }
89
90   /* Linux doesn't provide us with any of these values on the stack
91      when the dynamic linker is run directly as a program.  */
92
93 #define SEE(UID, uid) if ((seen & M (AT_##UID)) == 0) uid = __get##uid ()
94   SEE (UID, uid);
95   SEE (GID, gid);
96   SEE (EUID, euid);
97   SEE (EGID, egid);
98
99
100   __libc_enable_secure = uid != euid || gid != egid;
101
102 #ifdef DL_SYSDEP_INIT
103   DL_SYSDEP_INIT;
104 #endif
105
106   (*dl_main) (phdr, phnum, &user_entry);
107   return user_entry;
108 }
109
110 void
111 _dl_sysdep_start_cleanup (void)
112 {
113 }
114 \f
115 #ifndef MAP_ANON
116 /* This is only needed if the system doesn't support MAP_ANON.  */
117
118 int
119 _dl_sysdep_open_zero_fill (void)
120 {
121   return __open ("/dev/zero", O_RDONLY);
122 }
123 #endif
124
125 /* Read the whole contents of FILE into new mmap'd space with given
126    protections.  *SIZEP gets the size of the file.  */
127
128 void *
129 _dl_sysdep_read_whole_file (const char *file, size_t *sizep, int prot)
130 {
131   void *result;
132   struct stat st;
133   int fd = __open (file, O_RDONLY);
134   if (fd < 0)
135     return NULL;
136   if (__fstat (fd, &st) < 0)
137     result = NULL;
138   else
139     {
140       /* Map a copy of the file contents.  */
141       result = __mmap (0, st.st_size, prot,
142 #ifdef MAP_COPY
143                        MAP_COPY
144 #else
145                        MAP_PRIVATE
146 #endif
147 #ifdef MAP_FILE
148                        | MAP_FILE
149 #endif
150                        , fd, 0);
151       if (result == (void *) -1)
152         result = NULL;
153       else
154         *sizep = st.st_size;
155     }
156   __close (fd);
157   return result;
158 }
159
160 void
161 _dl_sysdep_fatal (const char *msg, ...)
162 {
163   va_list ap;
164
165   va_start (ap, msg);
166   do
167     {
168       size_t len = strlen (msg);
169       __write (STDERR_FILENO, msg, len);
170       msg = va_arg (ap, const char *);
171     } while (msg);
172   va_end (ap);
173
174   _exit (127);
175 }
176
177
178 void
179 _dl_sysdep_message (const char *msg, ...)
180 {
181   va_list ap;
182
183   va_start (ap, msg);
184   do
185     {
186       size_t len = strlen (msg);
187       __write (STDOUT_FILENO, msg, len);
188       msg = va_arg (ap, const char *);
189     } while (msg);
190   va_end (ap);
191 }