From nobody Wed Dec 13 21:44:18 2023 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 4Sr8Bp61s6z54MlL; Wed, 13 Dec 2023 21:44:18 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Sr8Bp51f1z3Mvs; Wed, 13 Dec 2023 21:44:18 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1702503858; 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=n/IVazVi6gQnoa6xaiRJZW5DPLjbtp3pIcq2ulDnc9g=; b=oXFGXE4M4I2DWjKDxic5fUwQuzTAI8+cIiCkUKOgEi/fE+xk7UfY3jh+WK7ExsBm+FsRsU pGBiYU69OgY2vL/6okSuc0TFTFodl5pAL+dzqhz8dc/9ny5Hc7N+T+Sns2pB8TipLWjmb/ eDffX+14FHIbd/lMgVs8xqXsvJw2gj/X35+TnXToUAQM7kcjbumrYKDoGLgK6GhNsVZUlL tjUdSJh0rEC65c//elYkkdTZhau9WZBunzF2QwX9VHqWODslb5iorQHzrm/j6huw9gn0b/ mzaDBoHHQNAsgVA6E65S8MqhJvLCdicts8pj6SW64IpbNM+sPn6kC7CRcXZhAg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1702503858; a=rsa-sha256; cv=none; b=Xe3B69zrMWcBI5yg7XbmHG45jKaB30EsBjxDflnKcVAOU8l76P8MtVXbu0X5zbGCG3Jf5U lonlZh6uj2a9jL1kmArQdUDjmAWbeRwyHlIS/tNHAWmB3c/lLFKlJMRZ8xG/dn4cR6RXQs P5yq2btg7hlIlacZBGtLpQTbXs7dPzJts2zdQ1gTPHmWQE0IBTfhscLmm12BGtKAaA5G1r EknJcag8BMD/kon96ssscWQ1AGql8qHWt5/dDT8/jQxZXwp2hJOavAIB3f2dF9gUjMDSZ3 xBpQLqB0gnAJSvmiKYWnQUeb2uUFhbHdCiChjQSo15aGa2IexvniEya9g/VGlg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1702503858; 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=n/IVazVi6gQnoa6xaiRJZW5DPLjbtp3pIcq2ulDnc9g=; b=xkb2Z4kmEp7vnw4pFXtQqFGSF9M6JXZe21qNLELnVDZVuFeWfIZfP1knxuAHjrLFYzeIw6 vIEowEGyqGafFZ7AMhU46+BCHATMvXUk3FDXD0qkOC4ngIfcNwKNzy9PfuqZqPmJlEF3tU x+gXmxGa+lQ7YDagTSrzp/CzC6+7NI/laWS9fNk+wv8RZmwP2fOAl+IlWYtZWBB24GxmPa sGu6AwGLqVJofxx/ZToqu/KJag9B8z+jcR5QLoEWlM9mQFA7mI67+CRlosvk+caFjgsuSC XdJxKSy5BujpdL7eWGLDIRdBSjDOF0ADJKbPfD3YAENIJVB40ys84/8Jp/C80Q== 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 4Sr8Bp40Lxz1FYQ; Wed, 13 Dec 2023 21:44:18 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3BDLiI8c055957; Wed, 13 Dec 2023 21:44:18 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BDLiI1T055954; Wed, 13 Dec 2023 21:44:18 GMT (envelope-from git) Date: Wed, 13 Dec 2023 21:44:18 GMT Message-Id: <202312132144.3BDLiI1T055954@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: 881ec81372e0 - main - tools/build: Provide sys/linker_set.h when cross-building 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: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: 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: 881ec81372e0748f2869fe6c18e909afa8304e7a Auto-Submitted: auto-generated The branch main has been updated by jrtc27: URL: https://cgit.FreeBSD.org/src/commit/?id=881ec81372e0748f2869fe6c18e909afa8304e7a commit 881ec81372e0748f2869fe6c18e909afa8304e7a Author: Jessica Clarke AuthorDate: 2023-12-13 21:43:09 +0000 Commit: Jessica Clarke CommitDate: 2023-12-13 21:43:09 +0000 tools/build: Provide sys/linker_set.h when cross-building This is needed for kldxref, which will shortly become a bootstrap tool. Linux can use the same one as FreeBSD (provided the cross-building sys/cdefs.h is augmented appropriately), whilst macOS needs its own Mach-O-specific implementation. Reviewed by: brooks Differential Revision: https://reviews.freebsd.org/D43049 --- tools/build/Makefile | 4 + tools/build/cross-build/include/common/sys/cdefs.h | 13 ++ .../build/cross-build/include/mac/sys/linker_set.h | 140 +++++++++++++++++++++ 3 files changed, 157 insertions(+) diff --git a/tools/build/Makefile b/tools/build/Makefile index 668a7198e228..78c46dfeacbc 100644 --- a/tools/build/Makefile +++ b/tools/build/Makefile @@ -216,6 +216,10 @@ SRCS+= progname.c # Stub implementations of fflagstostr/strtofflags SRCS+= fflags.c +# macOS has a standalone cross-build implementation, but Linux can use the same +# ELF one as FreeBSD +SYSINCS+= ${SRCTOP}/sys/sys/linker_set.h + .endif # ${MAKE.OS} == "Linux" .if ${.MAKE.OS} == "Darwin" diff --git a/tools/build/cross-build/include/common/sys/cdefs.h b/tools/build/cross-build/include/common/sys/cdefs.h index 3fa7790841cc..3f9b7866141f 100644 --- a/tools/build/cross-build/include/common/sys/cdefs.h +++ b/tools/build/cross-build/include/common/sys/cdefs.h @@ -121,6 +121,12 @@ #endif #endif +#ifndef __WEAK +#ifdef __ELF__ +#define __WEAK(sym) __asm__(".weak " __XSTRING(sym)) +#endif +#endif + /* Some files built as part of the bootstrap libegacy use these macros, but * since we aren't actually building libc.so, we can defined them to be empty */ #ifndef __sym_compat @@ -249,6 +255,13 @@ #define __DEQUALIFY(type, var) ((type)(__uintptr_t)(const volatile void *)(var)) #endif +#ifndef __nosanitizeaddress +#if __has_attribute(no_sanitize) && defined(__clang__) +#define __nosanitizeaddress __attribute__((no_sanitize("address"))) +#else +#define __nosanitizeaddress +#endif +#endif /* Expose all declarations when using FreeBSD headers */ #define __POSIX_VISIBLE 200809 diff --git a/tools/build/cross-build/include/mac/sys/linker_set.h b/tools/build/cross-build/include/mac/sys/linker_set.h new file mode 100644 index 000000000000..c0b81747c78b --- /dev/null +++ b/tools/build/cross-build/include/mac/sys/linker_set.h @@ -0,0 +1,140 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 1999 John D. Polstra + * Copyright (c) 1999,2001 Peter Wemm + * All rights reserved. + * Copyright (c) 2023 Jessica Clarke + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef _SYS_LINKER_SET_H_ +#define _SYS_LINKER_SET_H_ + +#include +#include + +/* + * The following macros are used to declare global sets of objects, which + * are collected by the linker into a `linker_set' as defined below. + * For Mach-O, this is done by constructing a separate section for each set. + */ + +#define __MAKE_SET_CONST const + +/* + * Private macros, not to be used outside this header file. + */ + +/* + * The userspace address sanitizer inserts redzones around global variables, + * violating the assumption that linker set elements are packed. + */ +#define __NOASAN __nosanitizeaddress + +#define __MAKE_SET_QV(set, sym, qv) \ + static void const * qv \ + __NOASAN \ + __set_##set##_sym_##sym __section("__DATA,set_" #set) \ + __used = &(sym) +#define __MAKE_SET(set, sym) __MAKE_SET_QV(set, sym, __MAKE_SET_CONST) + +static inline __pure2 uint8_t * +__set_getsectiondata(const char *segname, const char *sectname, + unsigned long *size) +{ + uint32_t image_count, image_index; + const struct mach_header *mh; + uint8_t *ret; + + image_count = _dyld_image_count(); + for (image_index = 0; image_index < image_count; ++image_index) { + mh = _dyld_get_image_header(image_index); + if (mh == NULL) + continue; + + ret = getsectiondata((const struct mach_header_64 *)mh, + segname, sectname, size); + if (ret != NULL) + return (ret); + } + + return (NULL); +} + +#define __SET_RANGE(set) ({ \ + unsigned long __set_size; \ + char *__set_data; \ + __set_data = __set_getsectiondata("__DATA", \ + "set_" #set, &__set_size); \ + (struct { \ + __CONCAT(__typeof_set_,set) **begin; \ + __CONCAT(__typeof_set_,set) **limit; \ + }){ \ + .begin = (__CONCAT(__typeof_set_,set) **)__set_data, \ + .limit = (__CONCAT(__typeof_set_,set) **)(__set_data + \ + __set_size) \ + }; \ +}) + +/* + * Public macros. + */ +#define TEXT_SET(set, sym) __MAKE_SET(set, sym) +#define DATA_SET(set, sym) __MAKE_SET(set, sym) +#define DATA_WSET(set, sym) __MAKE_SET_QV(set, sym, ) +#define BSS_SET(set, sym) __MAKE_SET(set, sym) +#define ABS_SET(set, sym) __MAKE_SET(set, sym) +#define SET_ENTRY(set, sym) __MAKE_SET(set, sym) + +/* + * Initialize before referring to a given linker set. + */ +#define SET_DECLARE(set, ptype) \ + typedef ptype __CONCAT(__typeof_set_,set) + +#define SET_BEGIN(set) \ + (__SET_RANGE(set).begin) +#define SET_LIMIT(set) \ + (__SET_RANGE(set).limit) + +/* + * Iterate over all the elements of a set. + * + * Sets always contain addresses of things, and "pvar" points to words + * containing those addresses. Thus is must be declared as "type **pvar", + * and the address of each set item is obtained inside the loop by "*pvar". + */ +#define SET_FOREACH(pvar, set) \ + for (pvar = SET_BEGIN(set); pvar < SET_LIMIT(set); pvar++) + +#define SET_ITEM(set, i) \ + ((SET_BEGIN(set))[i]) + +/* + * Provide a count of the items in a set. + */ +#define SET_COUNT(set) \ + (SET_LIMIT(set) - SET_BEGIN(set)) + +#endif /* _SYS_LINKER_SET_H_ */