xref: /illumos-gate/usr/src/test/elf-tests/tests/linker-sets/simple-src.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
1 /* The meat of this file is a copy of the FreeBSD sys/link_set.h */
2 /*
3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4  *
5  * Copyright (c) 1999 John D. Polstra
6  * Copyright (c) 1999,2001 Peter Wemm <peter@FreeBSD.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 #include <stdio.h>
34 #include <sys/linker_set.h>
35 
36 struct foo {
37 	char buf[128];
38 };
39 
40 SET_DECLARE(foo, struct foo);
41 
42 struct foo a = { "foo" };
43 struct foo b = { "bar" };
44 struct foo c = { "baz" };
45 
46 SET_ENTRY(foo, a);
47 SET_ENTRY(foo, b);
48 SET_ENTRY(foo, c);
49 
50 int
51 main(int __attribute__((unused)) argc, char __attribute__((unused)) **argv)
52 {
53 	struct foo **c;
54 	int i = 0;
55 
56 	printf("Set count: %d\n", (int)SET_COUNT(foo));
57 
58 
59 	printf("a: %s\n", ((struct foo *)__set_foo_sym_a)->buf);
60 	printf("b: %s\n", ((struct foo *)__set_foo_sym_b)->buf);
61 	printf("c: %s\n", ((struct foo *)__set_foo_sym_c)->buf);
62 
63 	printf("item(foo, 0): %s\n", SET_ITEM(foo, 0)->buf);
64 	printf("item(foo, 1): %s\n", SET_ITEM(foo, 1)->buf);
65 	printf("item(foo, 2): %s\n", SET_ITEM(foo, 2)->buf);
66 
67 	SET_FOREACH(c, foo) {
68 		printf("foo[%d]: %s\n", i, (*c)->buf);
69 		i++;
70 	}
71 
72 	return (0);
73 }
74