From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1944) id 3967D3865C2C; Thu, 27 Oct 2022 13:57:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3967D3865C2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1666879062; bh=rT6WyOGAk4kfKbweWSa62dimhafh8FrTBR+twqGI6kI=; h=From:To:Subject:Date:From; b=ZweK8Il5bMHOvMC1yGJ7ZtdhLso3jB+tHQRXQWf539yCykvIXkOpuEPTrMxrHPq4O INaNO6aVuVaBMwmeC/9jx3wP3X6ZDlSwTO5B/j7mP22VgV54OXSX90GWENeyR3vNFf jm7EdN9WBJfSpkg6hBPmrgFQU/LH4QGY+TXHabzQ= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Szabolcs Nagy To: glibc-cvs@sourceware.org Subject: [glibc/arm/morello/main] aarch64: morello: add purecap ld.so _start code X-Act-Checkin: glibc X-Git-Author: Szabolcs Nagy X-Git-Refname: refs/heads/arm/morello/main X-Git-Oldrev: 80b6a5c7bedb886564a2b8bc1f3999a95334a69d X-Git-Newrev: 81d3513572a7f8ea91ee49e0c9ea4ba661567119 Message-Id: <20221027135742.3967D3865C2C@sourceware.org> Date: Thu, 27 Oct 2022 13:57:42 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=81d3513572a7f8ea91ee49e0c9ea4ba661567119 commit 81d3513572a7f8ea91ee49e0c9ea4ba661567119 Author: Szabolcs Nagy Date: Thu Oct 20 13:05:19 2022 +0100 aarch64: morello: add purecap ld.so _start code The purecap version of aarch64 dl-start.S. Note: self relocation of ld.so is handled by the rtld bootstrap code. The ldso internal _dl_start still expects continuous argc, argv, envp, auxv, so that's emulated (since the purecap ELF entry passes them in separate registers). Diff: --- sysdeps/aarch64/morello/dl-start.c | 110 +++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/sysdeps/aarch64/morello/dl-start.c b/sysdeps/aarch64/morello/dl-start.c new file mode 100644 index 0000000000..f1b792d957 --- /dev/null +++ b/sysdeps/aarch64/morello/dl-start.c @@ -0,0 +1,110 @@ +/* ld.so _start code. + Copyright (C) 2022 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, see + . */ + +#include +#include + +asm("" +".global _start\n" +".type _start, %function\n" +"_start:\n" +" .cfi_startproc\n" +" .cfi_undefined c30\n" +" mov c29, czr\n" +" mov c30, czr\n" +" b __real_start\n" +" .cfi_endproc\n" +" .size _start, .-_start\n"); + +typedef void (entry_t) (int, char **, char **, void *, void (*)(void)); + +__attribute__ ((noinline, noreturn)) +void +_dl_start_user (uintptr_t *args, entry_t *entry) +{ + /* Setup argv, envp, auxv for the application. */ + uintptr_t *p; + long n; + int argc = args[0]; + p = args + 1; + n = argc + 1; + char **argv = (char **) __builtin_cheri_bounds_set (p, n * sizeof *p); + p += n; + for (n = 0; p[n]; n++); + n++; + char **envp = (char **) __builtin_cheri_bounds_set (p, n * sizeof *p); + p += n; + for (n = 0; p[n] != AT_NULL; n += 2); + n += 2; + uintptr_t *auxv = __builtin_cheri_bounds_set (p, n * sizeof *p); + + _dl_init (GL(dl_ns)[LM_ID_BASE]._ns_loaded, argc, argv, envp); + entry (argc, argv, envp, auxv, _dl_fini); + __builtin_trap (); +} + +/* Count the array length needed for traditional ELF entry. */ +static inline long +count_args (int argc, char **argv, char **envp, uintptr_t *auxv) +{ + char **p; + uintptr_t *q; + long nargs = argc + 2; + for (p = envp; *p != NULL; p++); + nargs += p - envp + 1; + for (q = auxv; *q != AT_NULL; q += 2); + nargs += q - auxv + 2; + return nargs; +} + +/* Generic ld.so start code in rtld.c. */ +uintptr_t +_dl_start (void *) attribute_hidden; + +/* ld.so entry point. */ +void +__real_start (int argc, char **argv, char **envp, void *auxv) +{ + long nargs = count_args (argc, argv, envp, auxv); + { + /* _dl_start requires continuous argv, envp, auxv. */ + uintptr_t args[nargs]; + long i = 0, j; + args[i++] = argc; + for (j = 0; argv[j] != NULL; j++) + args[i++] = (uintptr_t) argv[j]; + args[i++] = 0; + for (j = 0; envp[j] != NULL; j++) + args[i++] = (uintptr_t) envp[j]; + args[i++] = 0; + uintptr_t *a = auxv; + for (j = 0; a[j] != AT_NULL; j += 2) + { + args[i++] = a[j]; + args[i++] = a[j+1]; + } + args[i++] = AT_NULL; + args[i++] = 0; + assert (i == nargs); + + /* Run ls.so setup. */ + entry_t *entry = (entry_t *) _dl_start (args); + _dl_start_user (args, entry); + } +}