1 1.30 riastrad /* $NetBSD: crt0-common.c,v 1.30 2025/05/02 23:04:06 riastradh Exp $ */ 2 1.1 joerg 3 1.1 joerg /* 4 1.1 joerg * Copyright (c) 1998 Christos Zoulas 5 1.1 joerg * Copyright (c) 1995 Christopher G. Demetriou 6 1.1 joerg * All rights reserved. 7 1.25 skrll * 8 1.1 joerg * Redistribution and use in source and binary forms, with or without 9 1.1 joerg * modification, are permitted provided that the following conditions 10 1.1 joerg * are met: 11 1.1 joerg * 1. Redistributions of source code must retain the above copyright 12 1.1 joerg * notice, this list of conditions and the following disclaimer. 13 1.1 joerg * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 joerg * notice, this list of conditions and the following disclaimer in the 15 1.1 joerg * documentation and/or other materials provided with the distribution. 16 1.1 joerg * 3. All advertising materials mentioning features or use of this software 17 1.1 joerg * must display the following acknowledgement: 18 1.1 joerg * This product includes software developed for the 19 1.1 joerg * NetBSD Project. See http://www.NetBSD.org/ for 20 1.1 joerg * information about NetBSD. 21 1.1 joerg * 4. The name of the author may not be used to endorse or promote products 22 1.1 joerg * derived from this software without specific prior written permission. 23 1.25 skrll * 24 1.1 joerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 25 1.1 joerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26 1.1 joerg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27 1.1 joerg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 28 1.1 joerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 29 1.1 joerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 1.1 joerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 1.1 joerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 1.1 joerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 33 1.1 joerg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 1.25 skrll * 35 1.1 joerg * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>> 36 1.1 joerg */ 37 1.1 joerg 38 1.1 joerg #include <sys/cdefs.h> 39 1.30 riastrad __RCSID("$NetBSD: crt0-common.c,v 1.30 2025/05/02 23:04:06 riastradh Exp $"); 40 1.1 joerg 41 1.1 joerg #include <sys/types.h> 42 1.6 joerg #include <sys/exec.h> 43 1.20 joerg #include <sys/exec_elf.h> 44 1.1 joerg #include <sys/syscall.h> 45 1.30 riastrad 46 1.1 joerg #include <machine/profile.h> 47 1.30 riastrad 48 1.30 riastrad #include <limits.h> 49 1.1 joerg #include <stdlib.h> 50 1.1 joerg #include <unistd.h> 51 1.1 joerg 52 1.24 christos #include "csu-common.h" 53 1.24 christos 54 1.1 joerg extern int main(int, char **, char **); 55 1.1 joerg 56 1.20 joerg typedef void (*fptr_t)(void); 57 1.23 christos #ifndef HAVE_INITFINI_ARRAY 58 1.1 joerg extern void _init(void); 59 1.1 joerg extern void _fini(void); 60 1.22 christos #endif 61 1.1 joerg 62 1.1 joerg /* 63 1.1 joerg * Arrange for _DYNAMIC to be weak and undefined (and therefore to show up 64 1.1 joerg * as being at address zero, unless something else defines it). That way, 65 1.1 joerg * if we happen to be compiling without -static but with without any 66 1.1 joerg * shared libs present, things will still work. 67 1.1 joerg */ 68 1.3 joerg 69 1.4 joerg __weakref_visible int rtld_DYNAMIC __weak_reference(_DYNAMIC); 70 1.1 joerg 71 1.1 joerg #ifdef MCRT0 72 1.1 joerg extern void monstartup(u_long, u_long); 73 1.1 joerg extern void _mcleanup(void); 74 1.1 joerg extern unsigned char __etext, __eprol; 75 1.1 joerg #endif /* MCRT0 */ 76 1.1 joerg 77 1.24 christos static char empty_string[] = ""; 78 1.1 joerg 79 1.24 christos char **environ __common; 80 1.24 christos struct ps_strings *__ps_strings __common = 0; 81 1.24 christos char *__progname __common = empty_string; 82 1.1 joerg 83 1.20 joerg __dead __dso_hidden void ___start(void (*)(void), struct ps_strings *); 84 1.1 joerg 85 1.1 joerg #define write(fd, s, n) __syscall(SYS_write, (fd), (s), (n)) 86 1.1 joerg 87 1.1 joerg #define _FATAL(str) \ 88 1.1 joerg do { \ 89 1.1 joerg write(2, str, sizeof(str)-1); \ 90 1.1 joerg _exit(1); \ 91 1.1 joerg } while (0) 92 1.1 joerg 93 1.9 matt /* 94 1.9 matt * If we are using INIT_ARRAY/FINI_ARRAY and we are linked statically, 95 1.9 matt * we have to process these instead of relying on RTLD to do it for us. 96 1.9 matt * 97 1.9 matt * Since we don't need .init or .fini sections, just code them in C 98 1.9 matt * to make life easier. 99 1.9 matt */ 100 1.14 joerg extern const fptr_t __preinit_array_start[] __dso_hidden; 101 1.14 joerg extern const fptr_t __preinit_array_end[] __dso_hidden __weak; 102 1.14 joerg extern const fptr_t __init_array_start[] __dso_hidden; 103 1.14 joerg extern const fptr_t __init_array_end[] __dso_hidden __weak; 104 1.14 joerg extern const fptr_t __fini_array_start[] __dso_hidden; 105 1.14 joerg extern const fptr_t __fini_array_end[] __dso_hidden __weak; 106 1.9 matt 107 1.9 matt static inline void 108 1.13 matt _preinit(void) 109 1.13 matt { 110 1.14 joerg for (const fptr_t *f = __preinit_array_start; f < __preinit_array_end; f++) { 111 1.13 matt (*f)(); 112 1.13 matt } 113 1.13 matt } 114 1.13 matt 115 1.13 matt static inline void 116 1.23 christos _initarray(void) 117 1.9 matt { 118 1.14 joerg for (const fptr_t *f = __init_array_start; f < __init_array_end; f++) { 119 1.9 matt (*f)(); 120 1.9 matt } 121 1.9 matt } 122 1.9 matt 123 1.9 matt static void 124 1.23 christos _finiarray(void) 125 1.9 matt { 126 1.14 joerg for (const fptr_t *f = __fini_array_start; f < __fini_array_end; f++) { 127 1.9 matt (*f)(); 128 1.9 matt } 129 1.9 matt } 130 1.9 matt 131 1.26 skrll #if \ 132 1.27 skrll defined(__aarch64__) || \ 133 1.26 skrll defined(__powerpc__) || \ 134 1.26 skrll defined(__sparc__) || \ 135 1.26 skrll defined(__x86_64__) 136 1.15 joerg #define HAS_IPLTA 137 1.15 joerg static void fix_iplta(void) __noinline; 138 1.26 skrll #elif \ 139 1.26 skrll defined(__arm__) || \ 140 1.26 skrll defined(__i386__) 141 1.15 joerg #define HAS_IPLT 142 1.15 joerg static void fix_iplt(void) __noinline; 143 1.15 joerg #endif 144 1.15 joerg 145 1.15 joerg 146 1.15 joerg #ifdef HAS_IPLTA 147 1.15 joerg #include <stdio.h> 148 1.15 joerg extern const Elf_Rela __rela_iplt_start[] __dso_hidden __weak; 149 1.15 joerg extern const Elf_Rela __rela_iplt_end[] __dso_hidden __weak; 150 1.16 joerg #ifdef __sparc__ 151 1.16 joerg #define IFUNC_RELOCATION R_TYPE(JMP_IREL) 152 1.16 joerg #include <machine/elf_support.h> 153 1.16 joerg #define write_plt(where, value) sparc_write_branch((void *)where, (void *)value) 154 1.16 joerg #else 155 1.16 joerg #define IFUNC_RELOCATION R_TYPE(IRELATIVE) 156 1.16 joerg #define write_plt(where, value) *where = value 157 1.16 joerg #endif 158 1.15 joerg 159 1.15 joerg static void 160 1.15 joerg fix_iplta(void) 161 1.15 joerg { 162 1.15 joerg const Elf_Rela *rela, *relalim; 163 1.15 joerg uintptr_t relocbase = 0; 164 1.15 joerg Elf_Addr *where, target; 165 1.15 joerg 166 1.15 joerg rela = __rela_iplt_start; 167 1.15 joerg relalim = __rela_iplt_end; 168 1.15 joerg for (; rela < relalim; ++rela) { 169 1.16 joerg if (ELF_R_TYPE(rela->r_info) != IFUNC_RELOCATION) 170 1.15 joerg abort(); 171 1.15 joerg where = (Elf_Addr *)(relocbase + rela->r_offset); 172 1.15 joerg target = (Elf_Addr)(relocbase + rela->r_addend); 173 1.15 joerg target = ((Elf_Addr(*)(void))target)(); 174 1.16 joerg write_plt(where, target); 175 1.15 joerg } 176 1.15 joerg } 177 1.15 joerg #endif 178 1.15 joerg #ifdef HAS_IPLT 179 1.15 joerg extern const Elf_Rel __rel_iplt_start[] __dso_hidden __weak; 180 1.15 joerg extern const Elf_Rel __rel_iplt_end[] __dso_hidden __weak; 181 1.16 joerg #define IFUNC_RELOCATION R_TYPE(IRELATIVE) 182 1.15 joerg 183 1.15 joerg static void 184 1.15 joerg fix_iplt(void) 185 1.15 joerg { 186 1.15 joerg const Elf_Rel *rel, *rellim; 187 1.15 joerg uintptr_t relocbase = 0; 188 1.15 joerg Elf_Addr *where, target; 189 1.15 joerg 190 1.15 joerg rel = __rel_iplt_start; 191 1.15 joerg rellim = __rel_iplt_end; 192 1.15 joerg for (; rel < rellim; ++rel) { 193 1.16 joerg if (ELF_R_TYPE(rel->r_info) != IFUNC_RELOCATION) 194 1.15 joerg abort(); 195 1.15 joerg where = (Elf_Addr *)(relocbase + rel->r_offset); 196 1.15 joerg target = ((Elf_Addr(*)(void))*where)(); 197 1.15 joerg *where = target; 198 1.15 joerg } 199 1.15 joerg } 200 1.15 joerg #endif 201 1.15 joerg 202 1.18 joerg #if defined(__x86_64__) || defined(__i386__) 203 1.18 joerg # define HAS_RELOCATE_SELF 204 1.18 joerg # if defined(__x86_64__) 205 1.18 joerg # define RELA 206 1.18 joerg # define REL_TAG DT_RELA 207 1.18 joerg # define RELSZ_TAG DT_RELASZ 208 1.18 joerg # define REL_TYPE Elf_Rela 209 1.18 joerg # else 210 1.18 joerg # define REL_TAG DT_REL 211 1.18 joerg # define RELSZ_TAG DT_RELSZ 212 1.18 joerg # define REL_TYPE Elf_Rel 213 1.18 joerg # endif 214 1.18 joerg 215 1.18 joerg #include <elf.h> 216 1.18 joerg 217 1.18 joerg static void relocate_self(struct ps_strings *) __noinline; 218 1.18 joerg 219 1.18 joerg static void 220 1.18 joerg relocate_self(struct ps_strings *ps_strings) 221 1.18 joerg { 222 1.18 joerg AuxInfo *aux = (AuxInfo *)(ps_strings->ps_argvstr + ps_strings->ps_nargvstr + 223 1.18 joerg ps_strings->ps_nenvstr + 2); 224 1.19 kre uintptr_t relocbase = (uintptr_t)~0U; 225 1.19 kre const Elf_Phdr *phdr = NULL; 226 1.19 kre Elf_Half phnum = (Elf_Half)~0; 227 1.18 joerg 228 1.18 joerg for (; aux->a_type != AT_NULL; ++aux) { 229 1.18 joerg switch (aux->a_type) { 230 1.18 joerg case AT_BASE: 231 1.18 joerg if (aux->a_v) 232 1.18 joerg return; 233 1.18 joerg break; 234 1.18 joerg case AT_PHDR: 235 1.18 joerg phdr = (void *)aux->a_v; 236 1.18 joerg break; 237 1.18 joerg case AT_PHNUM: 238 1.18 joerg phnum = (Elf_Half)aux->a_v; 239 1.18 joerg break; 240 1.18 joerg } 241 1.18 joerg } 242 1.19 kre 243 1.19 kre if (phdr == NULL || phnum == (Elf_Half)~0) 244 1.19 kre return; 245 1.19 kre 246 1.19 kre const Elf_Phdr *phlimit = phdr + phnum, *dynphdr = NULL; 247 1.18 joerg 248 1.18 joerg for (; phdr < phlimit; ++phdr) { 249 1.18 joerg if (phdr->p_type == PT_DYNAMIC) 250 1.18 joerg dynphdr = phdr; 251 1.18 joerg if (phdr->p_type == PT_PHDR) 252 1.18 joerg relocbase = (uintptr_t)phdr - phdr->p_vaddr; 253 1.18 joerg } 254 1.19 kre if (dynphdr == NULL || relocbase == (uintptr_t)~0U) 255 1.19 kre return; 256 1.19 kre 257 1.18 joerg Elf_Dyn *dynp = (Elf_Dyn *)((uint8_t *)dynphdr->p_vaddr + relocbase); 258 1.18 joerg 259 1.30 riastrad const Elf_Relr *relr = 0, *relrlim; 260 1.18 joerg const REL_TYPE *relocs = 0, *relocslim; 261 1.30 riastrad Elf_Addr relrsz = 0, relocssz = 0; 262 1.18 joerg 263 1.18 joerg for (; dynp->d_tag != DT_NULL; dynp++) { 264 1.18 joerg switch (dynp->d_tag) { 265 1.30 riastrad case DT_RELR: 266 1.30 riastrad relr = 267 1.30 riastrad (const Elf_Relr *)(relocbase + dynp->d_un.d_ptr); 268 1.30 riastrad break; 269 1.30 riastrad case DT_RELRSZ: 270 1.30 riastrad relrsz = dynp->d_un.d_val; 271 1.30 riastrad break; 272 1.18 joerg case REL_TAG: 273 1.18 joerg relocs = 274 1.18 joerg (const REL_TYPE *)(relocbase + dynp->d_un.d_ptr); 275 1.18 joerg break; 276 1.18 joerg case RELSZ_TAG: 277 1.18 joerg relocssz = dynp->d_un.d_val; 278 1.18 joerg break; 279 1.18 joerg } 280 1.18 joerg } 281 1.30 riastrad relrlim = (const Elf_Relr *)((const uint8_t *)relr + relrsz); 282 1.18 joerg relocslim = (const REL_TYPE *)((const uint8_t *)relocs + relocssz); 283 1.30 riastrad while (relr < relrlim) { 284 1.30 riastrad Elf_Addr *where; 285 1.30 riastrad 286 1.30 riastrad where = (Elf_Addr *)(relocbase + *relr); 287 1.30 riastrad *where++ += relocbase; 288 1.30 riastrad while (++relr < relrlim && *relr & 1) { 289 1.30 riastrad unsigned i; 290 1.30 riastrad 291 1.30 riastrad for (i = 1; i < CHAR_BIT*sizeof(*relr); i++, where++) { 292 1.30 riastrad if (*relr & ((Elf_Relr)1 << i)) 293 1.30 riastrad *where += relocbase; 294 1.30 riastrad } 295 1.30 riastrad } 296 1.30 riastrad } 297 1.18 joerg for (; relocs < relocslim; ++relocs) { 298 1.18 joerg Elf_Addr *where; 299 1.18 joerg 300 1.18 joerg where = (Elf_Addr *)(relocbase + relocs->r_offset); 301 1.18 joerg 302 1.18 joerg switch (ELF_R_TYPE(relocs->r_info)) { 303 1.18 joerg case R_TYPE(RELATIVE): /* word64 B + A */ 304 1.18 joerg #ifdef RELA 305 1.18 joerg *where = (Elf_Addr)(relocbase + relocs->r_addend); 306 1.18 joerg #else 307 1.18 joerg *where += (Elf_Addr)relocbase; 308 1.18 joerg #endif 309 1.18 joerg break; 310 1.18 joerg #ifdef IFUNC_RELOCATION 311 1.18 joerg case IFUNC_RELOCATION: 312 1.18 joerg break; 313 1.18 joerg #endif 314 1.18 joerg default: 315 1.18 joerg abort(); 316 1.18 joerg } 317 1.18 joerg } 318 1.18 joerg } 319 1.18 joerg #endif 320 1.18 joerg 321 1.1 joerg void 322 1.6 joerg ___start(void (*cleanup)(void), /* from shared loader */ 323 1.1 joerg struct ps_strings *ps_strings) 324 1.1 joerg { 325 1.18 joerg #if defined(HAS_RELOCATE_SELF) 326 1.18 joerg relocate_self(ps_strings); 327 1.18 joerg #endif 328 1.1 joerg 329 1.6 joerg if (ps_strings == NULL) 330 1.6 joerg _FATAL("ps_strings missing\n"); 331 1.6 joerg __ps_strings = ps_strings; 332 1.6 joerg 333 1.6 joerg environ = ps_strings->ps_envstr; 334 1.6 joerg 335 1.6 joerg if (ps_strings->ps_argvstr[0] != NULL) { 336 1.1 joerg char *c; 337 1.6 joerg __progname = ps_strings->ps_argvstr[0]; 338 1.6 joerg for (c = ps_strings->ps_argvstr[0]; *c; ++c) { 339 1.1 joerg if (*c == '/') 340 1.1 joerg __progname = c + 1; 341 1.1 joerg } 342 1.1 joerg } else { 343 1.1 joerg __progname = empty_string; 344 1.1 joerg } 345 1.1 joerg 346 1.5 joerg _libc_init(); 347 1.5 joerg 348 1.15 joerg if (&rtld_DYNAMIC == NULL) { 349 1.15 joerg #ifdef HAS_IPLTA 350 1.15 joerg fix_iplta(); 351 1.15 joerg #endif 352 1.15 joerg #ifdef HAS_IPLT 353 1.15 joerg fix_iplt(); 354 1.15 joerg #endif 355 1.15 joerg } 356 1.15 joerg 357 1.13 matt _preinit(); 358 1.13 matt 359 1.29 christos if (cleanup != NULL) 360 1.29 christos atexit(cleanup); 361 1.29 christos 362 1.1 joerg #ifdef MCRT0 363 1.1 joerg atexit(_mcleanup); 364 1.1 joerg monstartup((u_long)&__eprol, (u_long)&__etext); 365 1.1 joerg #endif 366 1.1 joerg 367 1.23 christos atexit(_finiarray); 368 1.23 christos _initarray(); 369 1.23 christos 370 1.23 christos #ifndef HAVE_INITFINI_ARRAY 371 1.1 joerg atexit(_fini); 372 1.1 joerg _init(); 373 1.23 christos #endif 374 1.1 joerg 375 1.6 joerg exit(main(ps_strings->ps_nargvstr, ps_strings->ps_argvstr, environ)); 376 1.1 joerg } 377