From nobody Thu Jan 18 22:31:43 2024 X-Original-To: dev-commits-src-all@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 4TGHXv7273z579Bf; Thu, 18 Jan 2024 22:31:43 +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 4TGHXv6VdXz4CSR; Thu, 18 Jan 2024 22:31:43 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705617103; 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=YGUwGJN0cP8QwkI4Aq2mwnObPcPlLqprI4tCGA9c/dI=; b=fLQ6zRSKvKfqfuLMjr5XUVhXpdMBKfpWyaQ7hSVPUrgXw9qKQty7W5+wyN+wln+rZGvXRW OCTh3LjNoi4ZgSVpdGQ5/qeaVZkntXO3i5B9tSUNd7YUxEdCclI9kKfQzYc4t6hG9uSTYb wX2wKu86ez4Ka4OAFBewZK0L4PQ9PKYK9iuddr4tmfCaLs2nUGeuIRfihQC9zi0mOybgOp wTCFNQo8XLaCmoQXKvEv4g3xULEQHkM3hFJwX3kUc5rd0DXGE1pg//2u7ytfSLCuauLyWL bNAEvpBxAEREs1eaNtjyTpiJqA1cESDVlNfNTlgWJ2sCba0YlzgKssYA2cO5kQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705617103; 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=YGUwGJN0cP8QwkI4Aq2mwnObPcPlLqprI4tCGA9c/dI=; b=Du3IsMn14M5fuOe+qsCx5xOSvIIBW9Gd3dx4pPdNFqomWoHV16/fw+fCE+wWWc4paRW7d1 exOXKDalFXyVYoqATFRjd/hpIXi4Q0H3FZL+Dx7E62QqkT0qtXTjWtu/nUL1ND10V0SweF N4SeWH25scZYIqdnq6P/rFrd3YgGTvzOaVzeqJQGoEsJjwNkJgaSTDuGMG6kmO3SeJE1CJ xu0TQKa95d76LH9rmSmuxQ9510+gyR+oKihGvJm6gn/o0laDVADS7goVvqqyikhKF3Aa/L hGgFkWs9OxzPirvFNS1UE2di29LZeR1Pcw4BFklKFnQVty2a6LHBEPpCHrqA/Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1705617103; a=rsa-sha256; cv=none; b=MeRIwTi5i6IxRzfHfE94Ty/yQR7QNZjsuFk789g2JpJ/Vm2yMSsAnXHPVRm+BRXjJXM3pv tJlhCmAQxmzjP0J5GK5n8b06GYDgOnqdgKU2EELPrE0cT89Q21ZfYB2gRuTNsV4zwpbsEL qO8Y11GmKr8YVWEpFHtFiwujN8Vvx3F0W6F1c4YlEHrZB8rZg04UJYE7GvVQ3eb0H7DWHo te2KfwSy/v4L8OCJ1JIrkTqIFKSfcuamUqJi+YPNRgJvt5hhoLE21OdSVks0nAkzoD/VJz N8Glaxmh/856NxNG7ZPzJS6V+ehO2Vi4euQKYN1rNALWkRYVdIMGzmYSEOEsAA== 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 4TGHXv5YPJzQpm; Thu, 18 Jan 2024 22:31:43 +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 40IMVh31005196; Thu, 18 Jan 2024 22:31:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 40IMVhK5005193; Thu, 18 Jan 2024 22:31:43 GMT (envelope-from git) Date: Thu, 18 Jan 2024 22:31:43 GMT Message-Id: <202401182231.40IMVhK5005193@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: John Baldwin Subject: git: d735315d4dbf - stable/14 - tools/build: Provide sys/linker_set.h when cross-building List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: d735315d4dbf3007a46f9069baad8056f2187b50 Auto-Submitted: auto-generated The branch stable/14 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=d735315d4dbf3007a46f9069baad8056f2187b50 commit d735315d4dbf3007a46f9069baad8056f2187b50 Author: Jessica Clarke AuthorDate: 2023-12-13 21:43:09 +0000 Commit: John Baldwin CommitDate: 2024-01-18 22:26:48 +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 (cherry picked from commit 881ec81372e0748f2869fe6c18e909afa8304e7a) --- 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 8ad104f904ea..bd573a38fc75 100644 --- a/tools/build/Makefile +++ b/tools/build/Makefile @@ -208,6 +208,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_ */