Fri Dec 28 20:12:35 2018 UTC ()
Re-do previous (always make available preinit/initarray/finiarray), with
less disruption.


(christos)
diff -r1.22 -r1.23 src/lib/csu/common/crt0-common.c

cvs diff -r1.22 -r1.23 src/lib/csu/common/crt0-common.c (expand / switch to unified diff)

--- src/lib/csu/common/crt0-common.c 2018/12/28 18:17:11 1.22
+++ src/lib/csu/common/crt0-common.c 2018/12/28 20:12:35 1.23
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: crt0-common.c,v 1.22 2018/12/28 18:17:11 christos Exp $ */ 1/* $NetBSD: crt0-common.c,v 1.23 2018/12/28 20:12:35 christos Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1998 Christos Zoulas 4 * Copyright (c) 1998 Christos Zoulas
5 * Copyright (c) 1995 Christopher G. Demetriou 5 * Copyright (c) 1995 Christopher G. Demetriou
6 * All rights reserved. 6 * All rights reserved.
7 *  7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
@@ -26,41 +26,40 @@ @@ -26,41 +26,40 @@
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *  34 *
35 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>> 35 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
36 */ 36 */
37 37
38#include <sys/cdefs.h> 38#include <sys/cdefs.h>
39__RCSID("$NetBSD: crt0-common.c,v 1.22 2018/12/28 18:17:11 christos Exp $"); 39__RCSID("$NetBSD: crt0-common.c,v 1.23 2018/12/28 20:12:35 christos Exp $");
40 40
41#include <sys/types.h> 41#include <sys/types.h>
42#include <sys/exec.h> 42#include <sys/exec.h>
43#include <sys/exec_elf.h> 43#include <sys/exec_elf.h>
44#include <sys/syscall.h> 44#include <sys/syscall.h>
45#include <machine/profile.h> 45#include <machine/profile.h>
46#include <stdlib.h> 46#include <stdlib.h>
47#include <unistd.h> 47#include <unistd.h>
48 48
49extern int main(int, char **, char **); 49extern int main(int, char **, char **);
50 50
51#ifdef HAVE_INITFINI_ARRAY 
52typedef void (*fptr_t)(void); 51typedef void (*fptr_t)(void);
53#else 52#ifndef HAVE_INITFINI_ARRAY
54extern void _init(void); 53extern void _init(void);
55extern void _fini(void); 54extern void _fini(void);
56#endif 55#endif
57extern void _libc_init(void); 56extern void _libc_init(void);
58 57
59/* 58/*
60 * Arrange for _DYNAMIC to be weak and undefined (and therefore to show up 59 * Arrange for _DYNAMIC to be weak and undefined (and therefore to show up
61 * as being at address zero, unless something else defines it). That way, 60 * as being at address zero, unless something else defines it). That way,
62 * if we happen to be compiling without -static but with without any 61 * if we happen to be compiling without -static but with without any
63 * shared libs present, things will still work. 62 * shared libs present, things will still work.
64 */ 63 */
65 64
66__weakref_visible int rtld_DYNAMIC __weak_reference(_DYNAMIC); 65__weakref_visible int rtld_DYNAMIC __weak_reference(_DYNAMIC);
@@ -77,65 +76,63 @@ struct ps_strings *__ps_strings = 0; @@ -77,65 +76,63 @@ struct ps_strings *__ps_strings = 0;
77static char empty_string[] = ""; 76static char empty_string[] = "";
78char *__progname = empty_string; 77char *__progname = empty_string;
79 78
80__dead __dso_hidden void ___start(void (*)(void), struct ps_strings *); 79__dead __dso_hidden void ___start(void (*)(void), struct ps_strings *);
81 80
82#define write(fd, s, n) __syscall(SYS_write, (fd), (s), (n)) 81#define write(fd, s, n) __syscall(SYS_write, (fd), (s), (n))
83 82
84#define _FATAL(str) \ 83#define _FATAL(str) \
85do { \ 84do { \
86 write(2, str, sizeof(str)-1); \ 85 write(2, str, sizeof(str)-1); \
87 _exit(1); \ 86 _exit(1); \
88} while (0) 87} while (0)
89 88
90#ifdef HAVE_INITFINI_ARRAY 
91/* 89/*
92 * If we are using INIT_ARRAY/FINI_ARRAY and we are linked statically, 90 * If we are using INIT_ARRAY/FINI_ARRAY and we are linked statically,
93 * we have to process these instead of relying on RTLD to do it for us. 91 * we have to process these instead of relying on RTLD to do it for us.
94 * 92 *
95 * Since we don't need .init or .fini sections, just code them in C 93 * Since we don't need .init or .fini sections, just code them in C
96 * to make life easier. 94 * to make life easier.
97 */ 95 */
98extern const fptr_t __preinit_array_start[] __dso_hidden; 96extern const fptr_t __preinit_array_start[] __dso_hidden;
99extern const fptr_t __preinit_array_end[] __dso_hidden __weak; 97extern const fptr_t __preinit_array_end[] __dso_hidden __weak;
100extern const fptr_t __init_array_start[] __dso_hidden; 98extern const fptr_t __init_array_start[] __dso_hidden;
101extern const fptr_t __init_array_end[] __dso_hidden __weak; 99extern const fptr_t __init_array_end[] __dso_hidden __weak;
102extern const fptr_t __fini_array_start[] __dso_hidden; 100extern const fptr_t __fini_array_start[] __dso_hidden;
103extern const fptr_t __fini_array_end[] __dso_hidden __weak; 101extern const fptr_t __fini_array_end[] __dso_hidden __weak;
104 102
105static inline void 103static inline void
106_preinit(void) 104_preinit(void)
107{ 105{
108 for (const fptr_t *f = __preinit_array_start; f < __preinit_array_end; f++) { 106 for (const fptr_t *f = __preinit_array_start; f < __preinit_array_end; f++) {
109 (*f)(); 107 (*f)();
110 } 108 }
111} 109}
112 110
113static inline void 111static inline void
114_init(void) 112_initarray(void)
115{ 113{
116 for (const fptr_t *f = __init_array_start; f < __init_array_end; f++) { 114 for (const fptr_t *f = __init_array_start; f < __init_array_end; f++) {
117 (*f)(); 115 (*f)();
118 } 116 }
119} 117}
120 118
121static void 119static void
122_fini(void) 120_finiarray(void)
123{ 121{
124 for (const fptr_t *f = __fini_array_start; f < __fini_array_end; f++) { 122 for (const fptr_t *f = __fini_array_start; f < __fini_array_end; f++) {
125 (*f)(); 123 (*f)();
126 } 124 }
127} 125}
128#endif /* HAVE_INITFINI_ARRAY */ 
129 126
130#if defined(__x86_64__) || defined(__powerpc__) || defined(__sparc__) 127#if defined(__x86_64__) || defined(__powerpc__) || defined(__sparc__)
131#define HAS_IPLTA 128#define HAS_IPLTA
132static void fix_iplta(void) __noinline; 129static void fix_iplta(void) __noinline;
133#elif defined(__i386__) || defined(__arm__) 130#elif defined(__i386__) || defined(__arm__)
134#define HAS_IPLT 131#define HAS_IPLT
135static void fix_iplt(void) __noinline; 132static void fix_iplt(void) __noinline;
136#endif 133#endif
137 134
138 135
139#ifdef HAS_IPLTA 136#ifdef HAS_IPLTA
140#include <stdio.h> 137#include <stdio.h>
141extern const Elf_Rela __rela_iplt_start[] __dso_hidden __weak; 138extern const Elf_Rela __rela_iplt_start[] __dso_hidden __weak;
@@ -317,27 +314,30 @@ ___start(void (*cleanup)(void), /* fro @@ -317,27 +314,30 @@ ___start(void (*cleanup)(void), /* fro
317 atexit(cleanup); 314 atexit(cleanup);
318 315
319 _libc_init(); 316 _libc_init();
320 317
321 if (&rtld_DYNAMIC == NULL) { 318 if (&rtld_DYNAMIC == NULL) {
322#ifdef HAS_IPLTA 319#ifdef HAS_IPLTA
323 fix_iplta(); 320 fix_iplta();
324#endif 321#endif
325#ifdef HAS_IPLT 322#ifdef HAS_IPLT
326 fix_iplt(); 323 fix_iplt();
327#endif 324#endif
328 } 325 }
329 326
330#ifdef HAVE_INITFINI_ARRAY 
331 _preinit(); 327 _preinit();
332#endif 
333 328
334#ifdef MCRT0 329#ifdef MCRT0
335 atexit(_mcleanup); 330 atexit(_mcleanup);
336 monstartup((u_long)&__eprol, (u_long)&__etext); 331 monstartup((u_long)&__eprol, (u_long)&__etext);
337#endif 332#endif
338 333
 334 atexit(_finiarray);
 335 _initarray();
 336
 337#ifndef HAVE_INITFINI_ARRAY
339 atexit(_fini); 338 atexit(_fini);
340 _init(); 339 _init();
 340#endif
341 341
342 exit(main(ps_strings->ps_nargvstr, ps_strings->ps_argvstr, environ)); 342 exit(main(ps_strings->ps_nargvstr, ps_strings->ps_argvstr, environ));
343} 343}