h_r_rel.c revision 1.1 1 1.1 riastrad /* $NetBSD: h_r_rel.c,v 1.1 2025/05/02 03:26:26 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2025 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Redistribution and use in source and binary forms, with or without
8 1.1 riastrad * modification, are permitted provided that the following conditions
9 1.1 riastrad * are met:
10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
11 1.1 riastrad * notice, this list of conditions and the following disclaimer.
12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
14 1.1 riastrad * documentation and/or other materials provided with the distribution.
15 1.1 riastrad *
16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 riastrad */
28 1.1 riastrad
29 1.1 riastrad #include <sys/cdefs.h>
30 1.1 riastrad __RCSID("$NetBSD: h_r_rel.c,v 1.1 2025/05/02 03:26:26 riastradh Exp $");
31 1.1 riastrad
32 1.1 riastrad #include <stdio.h>
33 1.1 riastrad
34 1.1 riastrad /*
35 1.1 riastrad * When built as position-independent executable, the value of foop and
36 1.1 riastrad * foopp should be computed either via R_*_RELATIVE or R_*_REL32 or
37 1.1 riastrad * similar, which -- ports that support it -- may be compressed into a
38 1.1 riastrad * SHT_RELR section.
39 1.1 riastrad *
40 1.1 riastrad * One pointer indirection is enough to produce this effect, but we use
41 1.1 riastrad * two pointer indirections to increase the probability of a crash in
42 1.1 riastrad * case the relocations are done wrong.
43 1.1 riastrad */
44 1.1 riastrad static int foo = 0x5f4d7635;
45 1.1 riastrad static int *volatile foop = &foo;
46 1.1 riastrad static int *volatile *volatile foopp = &foop;
47 1.1 riastrad
48 1.1 riastrad /*
49 1.1 riastrad * The RELR section compresses relocations for adjacent addresses into
50 1.1 riastrad * bitmaps of 31 or 63 bits apiece. Create a bunch of consecutive
51 1.1 riastrad * addresses to relocate, punctuated by the occasional non-relocated
52 1.1 riastrad * address (null), to check for fencepost errors in the bitmap
53 1.1 riastrad * iteration.
54 1.1 riastrad */
55 1.1 riastrad static int bar[] = {
56 1.1 riastrad 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
57 1.1 riastrad 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
58 1.1 riastrad 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
59 1.1 riastrad 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
60 1.1 riastrad 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
61 1.1 riastrad 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
62 1.1 riastrad 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
63 1.1 riastrad 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
64 1.1 riastrad 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
65 1.1 riastrad 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
66 1.1 riastrad 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
67 1.1 riastrad 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
68 1.1 riastrad 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
69 1.1 riastrad 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
70 1.1 riastrad 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
71 1.1 riastrad 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
72 1.1 riastrad };
73 1.1 riastrad
74 1.1 riastrad static int *volatile barp[] = {
75 1.1 riastrad &bar[0x00], &bar[0x01], &bar[0x02], &bar[0x03],
76 1.1 riastrad &bar[0x04], &bar[0x05], &bar[0x06], &bar[0x07],
77 1.1 riastrad &bar[0x08], &bar[0x09], &bar[0x0a], &bar[0x0b],
78 1.1 riastrad &bar[0x0c], &bar[0x0d], &bar[0x0e], &bar[0x0f],
79 1.1 riastrad &bar[0x10], &bar[0x11], &bar[0x12], &bar[0x13],
80 1.1 riastrad &bar[0x14], &bar[0x15], &bar[0x16], &bar[0x17],
81 1.1 riastrad &bar[0x18], &bar[0x19], &bar[0x1a], &bar[0x1b],
82 1.1 riastrad &bar[0x1c], &bar[0x1d], &bar[0x1e], &bar[0x1f],
83 1.1 riastrad &bar[0x20], &bar[0x21], &bar[0x22], &bar[0x23],
84 1.1 riastrad &bar[0x24], &bar[0x25], &bar[0x26], &bar[0x27],
85 1.1 riastrad &bar[0x28], &bar[0x29], &bar[0x2a], &bar[0x2b],
86 1.1 riastrad &bar[0x2c], &bar[0x2d], &bar[0x2e], &bar[0x2f],
87 1.1 riastrad &bar[0x30], &bar[0x31], &bar[0x32], &bar[0x33],
88 1.1 riastrad &bar[0x34], &bar[0x35], &bar[0x36], &bar[0x37],
89 1.1 riastrad &bar[0x38], &bar[0x39], &bar[0x3a], &bar[0x3b],
90 1.1 riastrad &bar[0x3c], &bar[0x3d], &bar[0x3e], &bar[0x3f],
91 1.1 riastrad NULL, &bar[0x41], &bar[0x42], &bar[0x43], /* test a clear bit */
92 1.1 riastrad &bar[0x44], &bar[0x45], &bar[0x46], &bar[0x47],
93 1.1 riastrad &bar[0x48], &bar[0x49], &bar[0x4a], &bar[0x4b],
94 1.1 riastrad &bar[0x4c], &bar[0x4d], &bar[0x4e], &bar[0x4f],
95 1.1 riastrad &bar[0x50], &bar[0x51], &bar[0x52], &bar[0x53],
96 1.1 riastrad &bar[0x54], &bar[0x55], &bar[0x56], &bar[0x57],
97 1.1 riastrad &bar[0x58], &bar[0x59], &bar[0x5a], &bar[0x5b],
98 1.1 riastrad &bar[0x5c], &bar[0x5d], &bar[0x5e], &bar[0x5f],
99 1.1 riastrad &bar[0x60], &bar[0x61], &bar[0x62], &bar[0x63],
100 1.1 riastrad &bar[0x64], &bar[0x65], &bar[0x66], &bar[0x67],
101 1.1 riastrad &bar[0x68], &bar[0x69], &bar[0x6a], &bar[0x6b],
102 1.1 riastrad &bar[0x6c], &bar[0x6d], &bar[0x6e], &bar[0x6f],
103 1.1 riastrad &bar[0x70], &bar[0x71], &bar[0x72], &bar[0x73],
104 1.1 riastrad &bar[0x74], &bar[0x75], &bar[0x76], &bar[0x77],
105 1.1 riastrad &bar[0x78], &bar[0x79], &bar[0x7a], &bar[0x7b],
106 1.1 riastrad &bar[0x7c], &bar[0x7d], &bar[0x7e], &bar[0x7f],
107 1.1 riastrad NULL, /* confirm we stop at the end */
108 1.1 riastrad };
109 1.1 riastrad
110 1.1 riastrad static int baz = -0x1adbd477;
111 1.1 riastrad static int *volatile bazp = &baz;
112 1.1 riastrad
113 1.1 riastrad int
114 1.1 riastrad main(void)
115 1.1 riastrad {
116 1.1 riastrad int i, result = 0;
117 1.1 riastrad
118 1.1 riastrad if (**foopp != 0x5f4d7635) {
119 1.1 riastrad fprintf(stderr, "foo @ %p, foop = %p, *foop = %p,"
120 1.1 riastrad " **foopp = 0x%x\n",
121 1.1 riastrad &foo, foop, *foopp, **foopp);
122 1.1 riastrad result |= 1;
123 1.1 riastrad }
124 1.1 riastrad for (i = 0; i < (int)__arraycount(barp); i++) {
125 1.1 riastrad if (i == 0x40 || i == 0x80) {
126 1.1 riastrad if (barp[i] != NULL) {
127 1.1 riastrad fprintf(stderr, "barp[%u] = %p\n",
128 1.1 riastrad i, barp[i]);
129 1.1 riastrad }
130 1.1 riastrad } else {
131 1.1 riastrad if (*barp[i] != i) {
132 1.1 riastrad fprintf(stderr, "bar[%u] @ %p, barp[%u] = %p,"
133 1.1 riastrad " *barp[%u] = %u\n",
134 1.1 riastrad i, &bar[i], i, barp[i], i, *barp[i]);
135 1.1 riastrad result |= 1;
136 1.1 riastrad }
137 1.1 riastrad }
138 1.1 riastrad }
139 1.1 riastrad if (*bazp != -0x1adbd477) {
140 1.1 riastrad fprintf(stderr, "baz @ %p, bazp = %p, *bazp = 0x%x\n",
141 1.1 riastrad &baz, bazp, *bazp);
142 1.1 riastrad result |= 1;
143 1.1 riastrad }
144 1.1 riastrad
145 1.1 riastrad return result;
146 1.1 riastrad }
147