From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1944) id 70C603856089; Wed, 12 Oct 2022 14:17:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 70C603856089 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1665584241; bh=ptDF07W067pKhmq2bayRe1YdoDawOtSW9nGzD1uY9N4=; h=From:To:Subject:Date:From; b=HJ09W3m7x6/fPMWfh9PWWrGXKTlIZwhjxHPwzhNNSnncawiAWpmVO0Bxfl9Plw4ZB Z332/oMiBHywzJUSf35L8r8VHdOd/2lIiV4TLWGgHfF1D+24z+1vrjqnvVJVWpwLad PCFnQxdHABKxv0D6DzaWoMN1RKd/ZKskNeaZvYgw= 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 elf_machine_rtld_base_setup X-Act-Checkin: glibc X-Git-Author: Szabolcs Nagy X-Git-Refname: refs/heads/arm/morello/main X-Git-Oldrev: 1b0716e08364e9b9985cd801e0f35edb6550e501 X-Git-Newrev: 74085ebc1f762c9fd70af4b99b5615dc5358dee0 Message-Id: <20221012141721.70C603856089@sourceware.org> Date: Wed, 12 Oct 2022 14:17:21 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=74085ebc1f762c9fd70af4b99b5615dc5358dee0 commit 74085ebc1f762c9fd70af4b99b5615dc5358dee0 Author: Szabolcs Nagy Date: Mon Aug 8 11:29:21 2022 +0100 aarch64: morello: Add elf_machine_rtld_base_setup Use a new hook to do the rtld bootstrap map base address and root capability setup on CHERI. This will be needed to use separate per module RX and RW root caps. Diff: --- sysdeps/aarch64/morello/dl-machine.h | 72 ++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/sysdeps/aarch64/morello/dl-machine.h b/sysdeps/aarch64/morello/dl-machine.h index 19110d75c5..ff5169d779 100644 --- a/sysdeps/aarch64/morello/dl-machine.h +++ b/sysdeps/aarch64/morello/dl-machine.h @@ -101,6 +101,78 @@ elf_machine_runtime_dynamic (void) return p; } +/* PCC relative access to ehdr before relocations are processed. */ +static const ElfW(Ehdr) * +elf_machine_ehdr (void) +{ + const void *p; + asm ("" + ".weak __ehdr_start\n" + ".hidden __ehdr_start\n" + "adrp %0, __ehdr_start\n" + "add %0, %0, :lo12:__ehdr_start\n" : "=r"(p)); + return p; +} + +/* Set up ld.so root capabilities and base address from args. */ +static void __attribute__ ((unused)) +elf_machine_rtld_base_setup (struct link_map *map, void *args) +{ + uintptr_t *sp; + long argc; + uintptr_t cap_rx, cap_rw, cap_exe_rx, cap_exe_rw; + unsigned long ldso_base = 0; + + sp = args; + argc = sp[0]; + /* Skip argv. */ + sp += argc + 2; + /* Skip environ. */ + for (; *sp; sp++); + sp++; + cap_rx = cap_rw = cap_exe_rx = cap_exe_rw = 0; + for (; *sp != AT_NULL; sp += 2) + { + long t = sp[0]; + if (t == AT_BASE) + ldso_base = sp[1]; + if (t == AT_CHERI_INTERP_RX_CAP) + cap_rx = sp[1]; + if (t == AT_CHERI_INTERP_RW_CAP) + cap_rw = sp[1]; + if (t == AT_CHERI_EXEC_RX_CAP) + cap_exe_rx = sp[1]; + if (t == AT_CHERI_EXEC_RW_CAP) + cap_exe_rw = sp[1]; + } + /* Check if ldso is the executable. */ + if (ldso_base == 0) + { + cap_rx = cap_exe_rx; + cap_rw = cap_exe_rw; + ldso_base = cap_rx; /* Assume load segments start at vaddr 0. */ + } + map->l_addr = ldso_base; + map->l_map_start = cap_rx; + map->l_rw_start = cap_rw; + + /* Set up the RW ranges of ld.so, required for symbolic relocations. */ + const ElfW(Ehdr) *ehdr = elf_machine_ehdr (); + const ElfW(Phdr) *phdr = (const void *) ehdr + ehdr->e_phoff; + if (sizeof *phdr != ehdr->e_phentsize) + __builtin_trap (); + for (const ElfW(Phdr) *ph = phdr; ph < phdr + ehdr->e_phnum; ph++) + if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W)) + { + uintptr_t allocend = map->l_addr + ph->p_vaddr + ph->p_memsz; + if (map->l_rw_count >= DL_MAX_RW_COUNT) + __builtin_trap (); + map->l_rw_range[map->l_rw_count].start = map->l_addr + ph->p_vaddr; + map->l_rw_range[map->l_rw_count].end = allocend; + map->l_rw_count++; + } +} + /* Load address of the dynamic linker with correct bounds. */ static uintptr_t __attribute__ ((unused)) elf_machine_load_address_from_args (void *arg)