about summary refs log tree commit diff
path: root/sysdeps/unix/sysv/aix/libc-start.c
blob: 1184664b5780a3661d72bf14ef4d553eb4354653 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* Initialization code run first thing by the XCOFF startup code.  AIX version.
   Copyright (C) 2001 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

/* hack to use uchar's */
typedef unsigned char uchar;
#include <xcoff.h>
#include <rtinit.h>
#include <dlldr.h>
#include <bits/libc-lock.h>

extern void __libc_init_first (int argc, char **argv, char **envp);

/* XXX disable for now
extern int _dl_starting_up;
weak_extern (_dl_starting_up)
extern int __libc_multiple_libcs; */

/* XXX normally defined in generic/dl-sydep.c, hack it into existance
extern void *__libc_stack_end; */
void *__libc_stack_end;

  struct __libc_start_data_rec {
    void *stack;
    void *toc;
    int argc;
    char **argv;
    char **envp;
    char *data;
    char *text;
    unsigned mcount;
    unsigned special;
    int (*main)(int, char **, char **);
    void (*init)(void);
    void (*fini)(void);
    void (*rtld_fini)(void);
  };

extern struct __libc_start_data_rec __libc_start_data;
extern int errno;

/* The first piece of initialized data.  */
int __data_start = 0;

#ifndef HAVE_ELF
/* Since gcc/crtstuff.c won't define it unless the ELF format is used
   we will need to define it here.  */
void *__dso_handle = NULL;
#endif

/* AIX kernel function */
extern int __loadx (int flag, void *module, void *arg1, void *arg2,
		    void *arg3);
/* Needed by setenv */
char  **__environ;

/* Needed by dl-support.c */
/* XXX stubbing out dl-support.c for now..
   size_t _dl_pagesize = 0; */

/*
 * Find __rtinit symbol
 *
 * __RTINIT *find_rtinit()
 *
 * __RTINIT        *rti - pointer to __rtinit data structure
 */

static __RTINIT *
find_rtinit (void)
{
  struct xcoffhdr *xcoff_hdr;
  SCNHDR *sec_hdr;
  SCNHDR *ldr_sec_hdr;
  SCNHDR *data_sec_hdr;
  LDSYM *ldsym_hdr;
  __RTINIT *rtl;

  xcoff_hdr = (struct xcoffhdr *) __libc_start_data.text;
  sec_hdr   = (SCNHDR *) ((caddr_t) &xcoff_hdr->aouthdr
			  + xcoff_hdr->filehdr.f_opthdr);
  ldr_sec_hdr = (SCNHDR *) (sec_hdr + (xcoff_hdr->aouthdr.o_snloader - 1));
  ldsym_hdr   = (LDSYM  *) ((caddr_t) xcoff_hdr + ldr_sec_hdr->s_scnptr
			    + LDHDRSZ);

  if (__libc_start_data.mcount <= 0)
    {
      if (!ldr_sec_hdr->s_scnptr)
	return NULL;

      if (memcmp (ldsym_hdr, RTINIT_NAME, sizeof(RTINIT_NAME) - 1) != 0)
	return NULL;
    }

  data_sec_hdr   = (SCNHDR *) (sec_hdr + (xcoff_hdr->aouthdr.o_sndata - 1));
  rtl = (__RTINIT *) (ldsym_hdr->l_value
		      + (__libc_start_data.data - data_sec_hdr->s_vaddr));
  return rtl;
}

/* The mod_init1 calls every initialization function
   for a given module.

     void mod_init1(handler, rti)

     void *handler - if NULL init funtions for modules loaded at exec time
                     are being executed. Otherwise, the handler points to the
                     module loaded.

     __RTINIT *rti - pointer to __rtinit data structure (with rti->init_offset
                     not equal to zero)
 */

static void
mod_init1 (void *handler,__RTINIT *rtl)
{
  __RTINIT_DESCRIPTOR *descriptor;

  descriptor = (__RTINIT_DESCRIPTOR *) ((caddr_t) &rtl->rtl
					+ rtl->init_offset);
  while (descriptor->f != NULL)
    {
      if (!(descriptor->flags & _RT_CALLED))
	{
	  descriptor->flags |=  _RT_CALLED;
	  /* Execute init/fini.  */
	  descriptor->f (handler, rtl, descriptor);
	}
      descriptor = (__RTINIT_DESCRIPTOR *) ((caddr_t) descriptor
					    + rtl->__rtinit_descriptor_size);
    }
}

/* The modinit() function performs run-time linking, if enabled, and calling
   the init() function for all loaded modules.

   int modinit()
 */

#define DL_BUFFER_SIZE 1000

static int
modinit (void)
{
  int *handler = NULL;
  __RTINIT *rtinit_info = NULL;
  int flag;
  DL_INFO dl_buffer[DL_BUFFER_SIZE];
  DL_INFO *dl_info = dl_buffer;
  int i;

  /* Find __rtinit symbols */
  rtinit_info = find_rtinit ();

  flag = DL_EXECQ;
  if (rtinit_info && rtinit_info->rtl)
    flag |= DL_LOAD_RTL;

  /* Get a list of modules that have __rtinit.  */
  if (__loadx (flag, dl_info, (void *) sizeof (dl_buffer), NULL, NULL))
    exit (0x90);

  if (( dl_info[0].dlinfo_xflags & DL_INFO_OK))
    {
      rtinit_info = find_rtinit ();
      if ((rtinit_info != NULL) & (rtinit_info->rtl != NULL))
	{
	  if ((*rtinit_info->rtl) (dl_info, 0))
	    exit (0x90);
	}
    }

  /* Initialization each module loaded that has __rtinit. */
  if (dl_info[0].dlinfo_xflags & DL_INFO_OK)
    {
      for (i = 1; i < dl_info[0].dlinfo_arraylen + 1; ++i)
	if (dl_info[i].dlinfo_flags & DL_HAS_RTINIT)
	  {
	    rtinit_info = find_rtinit ();
	    if (rtinit_info)
	      mod_init1 (handler, rtinit_info);
	  }
    }

  return 0;
}


void
__libc_start_init (void)
{
  /* Do run-time linking, if enabled and call the init()
     for all loaded modules. */
  if (__libc_start_data.mcount != __libc_start_data.special)
    modinit ();
}

/* For now these are just stubs. */
void
__libc_start_fini (void)
{
}

void
__libc_start_rtld_fini (void)
{
}

void
__libc_start_main (void)
{
#ifndef SHARED

  /* The next variable is only here to work around a bug in gcc <= 2.7.2.2.
     If the address would be taken inside the expression the optimizer
     would try to be too smart and throws it away.  Grrr.  */

  /* XXX disable for now
  int *dummy_addr = &_dl_starting_up;

  __libc_multiple_libcs = dummy_addr && !_dl_starting_up; */
#endif

  /* Store the lowest stack address.  */
  __libc_stack_end = __libc_start_data.stack;

  /* Used by setenv */
  __environ = __libc_start_data.envp;

#ifndef SHARED
  /* Clear errno. */
    errno = 0;

  /* Some security at this point.  Prevent starting a SUID binary where
     the standard file descriptors are not opened.  We have to do this
     only for statically linked applications since otherwise the dynamic
     loader did the work already.  */
  if (__builtin_expect (__libc_enable_secure, 0))
    __libc_check_standard_fds ();

#endif

  /* Register the destructor of the dynamic linker if there is any.  */
  if (__builtin_expect (__libc_start_data.rtld_fini != NULL, 1))
    __cxa_atexit ((void (*) (void *)) __libc_start_data.rtld_fini, NULL, NULL);

  /* Call the initializer of the libc.  This is only needed here if we
     are compiling for the static library in which case we haven't
     run the constructors in `_dl_start_user'.  */
#ifndef SHARED
  __libc_init_first (__libc_start_data.argc, __libc_start_data.argv,
		     __libc_start_data.envp);
#endif

  /* Register the destructor of the program, if any.  */
  if (__libc_start_data.fini)
    __cxa_atexit ((void (*) (void *)) __libc_start_data.fini, NULL, NULL);

  /* Call the initializer of the program, if any.  */
#ifdef SHARED
  if (__builtin_expect (_dl_debug_mask & DL_DEBUG_IMPCALLS, 0))
    _dl_debug_printf ("\ninitialize program: %s\n\n",
		      __libc_start_data.argv[0]);
#endif
  if (__libc_start_data.init)
    (*__libc_start_data.init) ();

#ifdef SHARED
  if (__builtin_expect (_dl_debug_mask & DL_DEBUG_IMPCALLS, 0))
    _dl_debug_printf ("\ntransferring control: %s\n\n",
		      __libc_start_data.argv[0]);
#endif

  exit ((*__libc_start_data.main) (__libc_start_data.argc,
				   __libc_start_data.argv,
				   __libc_start_data.envp));
}