h_ifunc_static.c revision 1.6
11.6Sskrll/*	$NetBSD: h_ifunc_static.c,v 1.6 2022/06/21 06:47:38 skrll Exp $	*/
21.1Sjoerg
31.1Sjoerg/*-
41.1Sjoerg * Copyright (c) 2018 The NetBSD Foundation, Inc.
51.1Sjoerg * All rights reserved.
61.1Sjoerg *
71.1Sjoerg * This code is derived from software contributed to The NetBSD Foundation
81.1Sjoerg * by Joerg Sonnenberger.
91.1Sjoerg *
101.1Sjoerg * Redistribution and use in source and binary forms, with or without
111.1Sjoerg * modification, are permitted provided that the following conditions
121.1Sjoerg * are met:
131.1Sjoerg * 1. Redistributions of source code must retain the above copyright
141.1Sjoerg *    notice, this list of conditions and the following disclaimer.
151.1Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
161.1Sjoerg *    notice, this list of conditions and the following disclaimer in the
171.1Sjoerg *    documentation and/or other materials provided with the distribution.
181.1Sjoerg *
191.1Sjoerg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sjoerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sjoerg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sjoerg * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sjoerg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sjoerg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sjoerg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sjoerg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sjoerg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sjoerg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sjoerg * POSSIBILITY OF SUCH DAMAGE.
301.1Sjoerg */
311.1Sjoerg
321.6Sskrll#if \
331.6Sskrll    defined(__arm__) || \
341.6Sskrll    defined(__i386__) || \
351.6Sskrll    defined(__powerpc__) || \
361.6Sskrll    defined(__sparc__) || \
371.6Sskrll    defined(__x86_64__)
381.1Sjoerg#include <stdlib.h>
391.1Sjoerg#include <string.h>
401.1Sjoerg
411.1Sjoergstatic long long
421.1Sjoergifunc1(void)
431.1Sjoerg{
441.1Sjoerg	return 0xdeadbeefll;
451.1Sjoerg}
461.1Sjoerg
471.1Sjoergstatic long long
481.1Sjoergifunc2(void)
491.1Sjoerg{
501.1Sjoerg	return 0xbeefdeadll;
511.1Sjoerg}
521.1Sjoerg
531.1Sjoergstatic __attribute__((used))
541.1Sjoerglong long (*resolve_ifunc(void))(void)
551.1Sjoerg{
561.1Sjoerg	const char *e = getenv("USE_IFUNC2");
571.1Sjoerg	return e && strcmp(e, "1") == 0 ? ifunc2 : ifunc1;
581.1Sjoerg}
591.1Sjoerg
601.1Sjoerg__ifunc(ifunc, resolve_ifunc);
611.1Sjoergextern long long ifunc(void);
621.1Sjoerg
631.1Sjoergint
641.1Sjoergmain(int argc, char **argv)
651.1Sjoerg{
661.1Sjoerg
671.1Sjoerg	if (argc != 2)
681.1Sjoerg		return 1;
691.1Sjoerg	return atoll(argv[1]) != ifunc();
701.1Sjoerg}
711.2Sjoerg#else
721.2Sjoergint
731.2Sjoergmain(int argc, char **argv)
741.2Sjoerg{
751.2Sjoerg	return 127;
761.2Sjoerg}
771.2Sjoerg#endif
78