From nobody Tue May 6 22:15:34 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4ZsXlW4grPz5vXP6; Tue, 06 May 2025 22:15:35 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4ZsXlW0Wt9z3d4M; Tue, 06 May 2025 22:15:35 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1746569735; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=IAwC2p8+p7RfePJd9JhWo1dQrSqI/BLEDUbcFAA2CrU=; b=A4jW0XZKOdniu0cDOtgrbBH/0w9Rcs21Ac3rz9F9Zy2IPXmIcskhJjv8H9Es1zoHW19KPs m4+hQ1x8gGM8rmQW3sKTyW6SK6z1Q++1pJFDddu4jRVAyng+2Ai93aIagVyTzYTygrBC8n K4b5aGoVj2lSaV/K4FqB23VrpyRdckIcJkT5p2fFDRBaezcUtCJ59RqrGQ5rxdfBD+1Pps 2Bg0JJGty7tfEc6PQ9CPz5FyvPq0TAitgsGscEtRsEB5yzVij6LHDpQCg3phEX1wrpw//1 fiN8y/klaiXsJciihqN/WrskZnlLga+dbuMafOeDTbwBtX44EifZafD7ZBBZKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1746569735; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=IAwC2p8+p7RfePJd9JhWo1dQrSqI/BLEDUbcFAA2CrU=; b=lsxjp6zF7OEFlLv66Pl728hkoIR7wRUFHyIfoBavt+7K0Q5WeRlVy+iPL0gCH/ORjoAZ1o TXfB8XGSrYPaGN3rFRdWCOzNBR992zFGr8rSuLAxavnOFNeyPYbb3aEKHHo7rRKvL0fupS 1wfW3rw3fNB/EOGKungdHp9W3o/GbAbbSYu8bkVBcyC8xNcRnJuJRt86/P91ULtRdHP2UP BlH3lYHGaGNVzQLKNhJS/hwSmpYPX/Cozt5dWu6mxAaNSl74wLtg8yDx1oqqlMIf+3YUQc bfRHQG3jw2MW6BBF9eBw1oiF77Jvv5cbnga0bBpkYK0ACogtf7T6+NnMSy+Q/Q== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1746569735; a=rsa-sha256; cv=none; b=jFSsbbJyf1+EJUqXas67VUN/jguVWiNbcxvvc0adpTOiaPU/cQgvdGfVY4y5YMLXlzYbLv YvPBQlDR8mPCKCddIk04htSP5D05kQ+I78nc8DOeCuGrPoHwa/b0LR77hqmq/ePwaC1FCe Q+tI7tOS9uMjFsQOgFeWRCZLBqgXMCK8hcw1wJdhsIqFcQk2zxbaHfP54eSnqDjD9sxU/C Gjq1hBxa8s2v6OzyvpsfR3rYuzq+gpRHJkKw4dYOWjgtczi5aq/NO8AxcjazeTdpaTXwt0 HvZwtM2+fD+NojqYArSJ9P/tRVHv7Cmr9r00ubtJMOsHZtE9kptTxm0ZHJu0Pw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4ZsXlW07Dyz1HMn; Tue, 06 May 2025 22:15:35 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 546MFYUa063080; Tue, 6 May 2025 22:15:34 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 546MFYfa063077; Tue, 6 May 2025 22:15:34 GMT (envelope-from git) Date: Tue, 6 May 2025 22:15:34 GMT Message-Id: <202505062215.546MFYfa063077@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Jessica Clarke Subject: git: a08d92def20a - main - rtld-elf: Fix executable's TLS module index for direct exec List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jrtc27 X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a08d92def20a41243d4afc97cf4a2124be5386b9 Auto-Submitted: auto-generated The branch main has been updated by jrtc27: URL: https://cgit.FreeBSD.org/src/commit/?id=a08d92def20a41243d4afc97cf4a2124be5386b9 commit a08d92def20a41243d4afc97cf4a2124be5386b9 Author: Jessica Clarke AuthorDate: 2025-05-06 22:14:51 +0000 Commit: Jessica Clarke CommitDate: 2025-05-06 22:14:51 +0000 rtld-elf: Fix executable's TLS module index for direct exec For direct exec mode we reuse map_object, but tls_max_index is initialised to 1. As a result, the executable ends up being assigned module 2 (and the generation is pointlessly incremented, unlike in digest_phdr for the normal case). For most architectures this is harmless, since TLS linker relaxation will optimise General Dynamic accesses to Initial Exec or Local Exec for executables, but on RISC-V this relaxation does not exist, yet the linker will initialise the tls_index in the GOT with module 1, and at run time the call to __tls_get_addr will fail with: ld-elf.so.1: Can't find module with TLS index 1 Fix this by making map_object use 1 for obj->tlsindex when it's loading the main executable, and don't bother to increment tls_dtv_generation either, matching digest_phdr (though that one is harmless). (Note this also applies to MIPS on stable/13) Reviewed by: kib Fixes: 0fc65b0ab82c ("Make ld-elf.so.1 directly executable.") MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D50186 --- libexec/rtld-elf/map_object.c | 10 +++++++--- libexec/rtld-elf/rtld.c | 4 ++-- libexec/rtld-elf/rtld.h | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/libexec/rtld-elf/map_object.c b/libexec/rtld-elf/map_object.c index 148b6dc4ca6e..04d17072af77 100644 --- a/libexec/rtld-elf/map_object.c +++ b/libexec/rtld-elf/map_object.c @@ -60,7 +60,7 @@ phdr_in_zero_page(const Elf_Ehdr *hdr) * for the shared object. Returns NULL on failure. */ Obj_Entry * -map_object(int fd, const char *path, const struct stat *sb) +map_object(int fd, const char *path, const struct stat *sb, bool ismain) { Obj_Entry *obj; Elf_Ehdr *hdr; @@ -327,8 +327,12 @@ map_object(int fd, const char *path, const struct stat *sb) obj->interp = (const char *)(obj->relocbase + phinterp->p_vaddr); if (phtls != NULL) { - tls_dtv_generation++; - obj->tlsindex = ++tls_max_index; + if (ismain) + obj->tlsindex = 1; + else { + tls_dtv_generation++; + obj->tlsindex = ++tls_max_index; + } obj->tlssize = phtls->p_memsz; obj->tlsalign = phtls->p_align; obj->tlspoffset = phtls->p_offset; diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c index d7ac1e36d70f..eb04a2f17603 100644 --- a/libexec/rtld-elf/rtld.c +++ b/libexec/rtld-elf/rtld.c @@ -784,7 +784,7 @@ _rtld(Elf_Addr *sp, func_ptr_type *exit_proc, Obj_Entry **objp) */ if (fd != -1) { /* Load the main program. */ dbg("loading main program"); - obj_main = map_object(fd, argv0, NULL); + obj_main = map_object(fd, argv0, NULL, true); close(fd); if (obj_main == NULL) rtld_die(); @@ -2920,7 +2920,7 @@ do_load_object(int fd, const char *name, char *path, struct stat *sbp, } dbg("loading \"%s\"", printable_path(path)); - obj = map_object(fd, printable_path(path), sbp); + obj = map_object(fd, printable_path(path), sbp, false); if (obj == NULL) return (NULL); diff --git a/libexec/rtld-elf/rtld.h b/libexec/rtld-elf/rtld.h index d1ea00b96627..a15c1d6d159f 100644 --- a/libexec/rtld-elf/rtld.h +++ b/libexec/rtld-elf/rtld.h @@ -380,7 +380,7 @@ enum { void _rtld_error(const char *, ...) __printflike(1, 2) __exported; void rtld_die(void) __dead2; const char *rtld_strerror(int); -Obj_Entry *map_object(int, const char *, const struct stat *); +Obj_Entry *map_object(int, const char *, const struct stat *, bool); void *xcalloc(size_t, size_t); void *xmalloc(size_t); char *xstrdup(const char *);