1/*-
2 * Copyright 2012 Konstantin Belousov <kib@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: stable/11/lib/csu/common/ignore_init.c 339300 2018-10-11 00:26:15Z emaste $");
28
29#include <sys/param.h>
30#include <sys/elf_common.h>
31#include "notes.h"
32
33extern int main(int, char **, char **);
34
35extern void (*__preinit_array_start[])(int, char **, char **) __hidden;
36extern void (*__preinit_array_end[])(int, char **, char **) __hidden;
37extern void (*__init_array_start[])(int, char **, char **) __hidden;
38extern void (*__init_array_end[])(int, char **, char **) __hidden;
39extern void (*__fini_array_start[])(void) __hidden;
40extern void (*__fini_array_end[])(void) __hidden;
41extern void _fini(void) __hidden;
42extern void _init(void) __hidden;
43
44extern int _DYNAMIC;
45#pragma weak _DYNAMIC
46
47char **environ;
48const char *__progname = "";
49
50static void
51finalizer(void)
52{
53	void (*fn)(void);
54	size_t array_size, n;
55
56	array_size = __fini_array_end - __fini_array_start;
57	for (n = array_size; n > 0; n--) {
58		fn = __fini_array_start[n - 1];
59		if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
60			(fn)();
61	}
62	_fini();
63}
64
65static inline void
66handle_static_init(int argc, char **argv, char **env)
67{
68	void (*fn)(int, char **, char **);
69	size_t array_size, n;
70
71	if (&_DYNAMIC != NULL)
72		return;
73
74	atexit(finalizer);
75
76	array_size = __preinit_array_end - __preinit_array_start;
77	for (n = 0; n < array_size; n++) {
78		fn = __preinit_array_start[n];
79		if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
80			fn(argc, argv, env);
81	}
82	_init();
83	array_size = __init_array_end - __init_array_start;
84	for (n = 0; n < array_size; n++) {
85		fn = __init_array_start[n];
86		if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1)
87			fn(argc, argv, env);
88	}
89}
90
91static inline void
92handle_argv(int argc, char *argv[], char **env)
93{
94	const char *s;
95
96	if (environ == NULL)
97		environ = env;
98	if (argc > 0 && argv[0] != NULL) {
99		__progname = argv[0];
100		for (s = __progname; *s != '\0'; s++) {
101			if (*s == '/')
102				__progname = s + 1;
103		}
104	}
105}
106
107static const struct {
108	int32_t	namesz;
109	int32_t	descsz;
110	int32_t	type;
111	char	name[sizeof(NOTE_FREEBSD_VENDOR)];
112	uint32_t desc;
113} crt_noinit_tag __attribute__ ((section (NOTE_SECTION),
114    aligned(4))) __used = {
115	.namesz = sizeof(NOTE_FREEBSD_VENDOR),
116	.descsz = sizeof(uint32_t),
117	.type = NT_FREEBSD_NOINIT_TAG,
118	.name = NOTE_FREEBSD_VENDOR,
119	.desc = 0
120};
121