1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
#!/bin/sh
set -eu
: "${CPU_AS:?}" "${CPU_LD:?}" "${CPU_RUN:?}"
TMP=${TMPDIR:-/tmp}/libmcpu-symbol-scope-$$
mkdir -p "$TMP/lib"
trap 'rm -rf "$TMP"' EXIT HUP INT TERM
cat > "$TMP/main-import.s" <<'EOS'
.global _life
.extern foo
.text
_start_life:
LDI r64@0x00000, 40
CALL foo
MOV r64@0x00018, r64@0x00000
LDI r32@0x00030, 0
EXIT r32@0x00030
EOS
cat > "$TMP/a.s" <<'EOS'
.global foo
.text
foo:
LDI r64@0x00008, 2
IADD r64@0x00000, r64@0x00000, r64@0x00008
RET
EOS
cat > "$TMP/b.s" <<'EOS'
.global foo
.text
foo:
LDI r64@0x00008, 3
IADD r64@0x00000, r64@0x00000, r64@0x00008
RET
EOS
"$CPU_AS" -o "$TMP/main-import.o" "$TMP/main-import.s"
"$CPU_AS" -o "$TMP/a.o" "$TMP/a.s"
"$CPU_AS" -o "$TMP/b.o" "$TMP/b.s"
"$CPU_LD" --shared --soname=liba.so.1 -o "$TMP/lib/liba.so.1.0.0" "$TMP/a.o"
"$CPU_LD" --shared --soname=libb.so.1 -o "$TMP/lib/libb.so.1.0.0" "$TMP/b.o"
ln -s liba.so.1.0.0 "$TMP/lib/liba.so.1"
ln -s liba.so.1 "$TMP/lib/liba.so"
ln -s libb.so.1.0.0 "$TMP/lib/libb.so.1"
ln -s libb.so.1 "$TMP/lib/libb.so"
# Normal DT_NEEDED scope is breadth-first and left-to-right. For equal
# GLOBAL definitions the first direct dependency wins.
"$CPU_LD" --dynamic --stack-size=4096 --no-heap \
-L"$TMP/lib" -la -lb -o "$TMP/main-ab" "$TMP/main-import.o"
[ "`"$CPU_RUN" --library-path="$TMP/lib" --int-reg=r64@0x00018 "$TMP/main-ab"`" = 42 ]
"$CPU_LD" --dynamic --stack-size=4096 --no-heap \
-L"$TMP/lib" -lb -la -o "$TMP/main-ba" "$TMP/main-import.o"
[ "`"$CPU_RUN" --library-path="$TMP/lib" --int-reg=r64@0x00018 "$TMP/main-ba"`" = 43 ]
# A main-executable GLOBAL outranks normal dependencies.
cat > "$TMP/main-own.s" <<'EOS'
.global _life
.global foo
.text
_start_life:
LDI r64@0x00000, 40
CALL foo
MOV r64@0x00018, r64@0x00000
LDI r32@0x00030, 0
EXIT r32@0x00030
foo:
LDI r64@0x00008, 1
IADD r64@0x00000, r64@0x00000, r64@0x00008
RET
EOS
"$CPU_AS" -o "$TMP/main-own.o" "$TMP/main-own.s"
"$CPU_LD" --dynamic --stack-size=4096 --no-heap \
-L"$TMP/lib" -la -o "$TMP/main-own" "$TMP/main-own.o"
[ "`"$CPU_RUN" --library-path="$TMP/lib" --int-reg=r64@0x00018 "$TMP/main-own"`" = 41 ]
# Explicit preload is the intentional override scope. Equal-strength GLOBAL
# preloads keep the historical rightmost-higher-priority rule.
[ "`"$CPU_RUN" --library-path="$TMP/lib" \
--ldpreload=liba.so:libb.so \
--int-reg=r64@0x00018 "$TMP/main-own"`" = 43 ]
[ "`"$CPU_RUN" --library-path="$TMP/lib" \
--ldpreload=libb.so:liba.so \
--int-reg=r64@0x00018 "$TMP/main-own"`" = 42 ]
# Binding strength outranks scope class: a WEAK preload must not preempt a
# normal GLOBAL definition.
cat > "$TMP/weak.s" <<'EOS'
.weak foo
.text
foo:
LDI r64@0x00008, 9
IADD r64@0x00000, r64@0x00000, r64@0x00008
RET
EOS
"$CPU_AS" -o "$TMP/weak.o" "$TMP/weak.s"
"$CPU_LD" --shared --soname=libweak.so.1 -o "$TMP/libweak.so" "$TMP/weak.o"
[ "`"$CPU_RUN" --library-path="$TMP/lib" --ldpreload="$TMP/libweak.so" \
--int-reg=r64@0x00018 "$TMP/main-ab"`" = 42 ]
# If only WEAK definitions exist, the same scope ordering applies: first normal
# dependency wins, while rightmost explicit preload wins.
cat > "$TMP/weak-main.s" <<'EOS'
.global _life
.weak foo
.text
_start_life:
LDI r64@0x00000, 40
CALL foo
MOV r64@0x00018, r64@0x00000
LDI r32@0x00030, 0
EXIT r32@0x00030
EOS
cat > "$TMP/wa.s" <<'EOS'
.weak foo
.text
foo:
LDI r64@0x00008, 2
IADD r64@0x00000, r64@0x00000, r64@0x00008
RET
EOS
cat > "$TMP/wb.s" <<'EOS'
.weak foo
.text
foo:
LDI r64@0x00008, 3
IADD r64@0x00000, r64@0x00000, r64@0x00008
RET
EOS
"$CPU_AS" -o "$TMP/weak-main.o" "$TMP/weak-main.s"
"$CPU_AS" -o "$TMP/wa.o" "$TMP/wa.s"
"$CPU_AS" -o "$TMP/wb.o" "$TMP/wb.s"
"$CPU_LD" --shared --soname=libwa.so.1 -o "$TMP/lib/libwa.so.1" "$TMP/wa.o"
"$CPU_LD" --shared --soname=libwb.so.1 -o "$TMP/lib/libwb.so.1" "$TMP/wb.o"
ln -s libwa.so.1 "$TMP/lib/libwa.so"
ln -s libwb.so.1 "$TMP/lib/libwb.so"
"$CPU_LD" --dynamic --stack-size=4096 --no-heap \
-L"$TMP/lib" -lwa -lwb -o "$TMP/weak-main" "$TMP/weak-main.o"
[ "`"$CPU_RUN" --library-path="$TMP/lib" --int-reg=r64@0x00018 "$TMP/weak-main"`" = 42 ]
[ "`"$CPU_RUN" --ldpreload="$TMP/lib/libwa.so.1:$TMP/lib/libwb.so.1" \
--int-reg=r64@0x00018 "$TMP/weak-main"`" = 43 ]
# A direct dependency outranks a conflicting transitive dependency. The
# resolver must therefore build a true breadth-first scope rather than expose
# DFS traversal order to symbol resolution.
cat > "$TMP/deep.s" <<'EOS'
.global deep_anchor
.global foo
.text
deep_anchor:
RET
foo:
LDI r64@0x00008, 9
IADD r64@0x00000, r64@0x00000, r64@0x00008
RET
EOS
cat > "$TMP/mid.s" <<'EOS'
.global mid_anchor
.extern deep_anchor
.text
mid_anchor:
CALL deep_anchor
RET
EOS
cat > "$TMP/direct.s" <<'EOS'
.global foo
.text
foo:
LDI r64@0x00008, 3
IADD r64@0x00000, r64@0x00000, r64@0x00008
RET
EOS
cat > "$TMP/bfs-main.s" <<'EOS'
.global _life
.extern mid_anchor
.extern foo
.text
_start_life:
LDI r64@0x00000, 40
CALL mid_anchor
CALL foo
MOV r64@0x00018, r64@0x00000
LDI r32@0x00030, 0
EXIT r32@0x00030
EOS
"$CPU_AS" -o "$TMP/deep.o" "$TMP/deep.s"
"$CPU_AS" -o "$TMP/mid.o" "$TMP/mid.s"
"$CPU_AS" -o "$TMP/direct.o" "$TMP/direct.s"
"$CPU_AS" -o "$TMP/bfs-main.o" "$TMP/bfs-main.s"
"$CPU_LD" --shared --soname=libdeep.so.1 -o "$TMP/lib/libdeep.so.1.0.0" "$TMP/deep.o"
ln -s libdeep.so.1.0.0 "$TMP/lib/libdeep.so.1"
ln -s libdeep.so.1 "$TMP/lib/libdeep.so"
"$CPU_LD" --shared --soname=libmid.so.1 -L"$TMP/lib" -ldeep \
-o "$TMP/lib/libmid.so.1.0.0" "$TMP/mid.o"
ln -s libmid.so.1.0.0 "$TMP/lib/libmid.so.1"
ln -s libmid.so.1 "$TMP/lib/libmid.so"
"$CPU_LD" --shared --soname=libdirect.so.1 -o "$TMP/lib/libdirect.so.1.0.0" "$TMP/direct.o"
ln -s libdirect.so.1.0.0 "$TMP/lib/libdirect.so.1"
ln -s libdirect.so.1 "$TMP/lib/libdirect.so"
"$CPU_LD" --dynamic --stack-size=4096 --no-heap \
-L"$TMP/lib" -lmid -ldirect -o "$TMP/bfs-main" "$TMP/bfs-main.o"
[ "`"$CPU_RUN" --library-path="$TMP/lib" --int-reg=r64@0x00018 "$TMP/bfs-main"`" = 43 ]
# Diamond graph: A and B both depend on C. SONAME deduplication loads C once,
# while the direct A/B scope still follows left-to-right order.
cat > "$TMP/common.s" <<'EOS'
.global common_anchor
.text
common_anchor:
RET
EOS
cat > "$TMP/da.s" <<'EOS'
.global a_anchor
.global foo
.extern common_anchor
.text
a_anchor:
CALL common_anchor
RET
foo:
LDI r64@0x00008, 2
IADD r64@0x00000, r64@0x00000, r64@0x00008
RET
EOS
cat > "$TMP/db.s" <<'EOS'
.global b_anchor
.global foo
.extern common_anchor
.text
b_anchor:
CALL common_anchor
RET
foo:
LDI r64@0x00008, 3
IADD r64@0x00000, r64@0x00000, r64@0x00008
RET
EOS
cat > "$TMP/diamond-main.s" <<'EOS'
.global _life
.extern a_anchor
.extern b_anchor
.extern foo
.text
_start_life:
LDI r64@0x00000, 40
CALL a_anchor
CALL b_anchor
CALL foo
MOV r64@0x00018, r64@0x00000
LDI r32@0x00030, 0
EXIT r32@0x00030
EOS
"$CPU_AS" -o "$TMP/common.o" "$TMP/common.s"
"$CPU_AS" -o "$TMP/da.o" "$TMP/da.s"
"$CPU_AS" -o "$TMP/db.o" "$TMP/db.s"
"$CPU_AS" -o "$TMP/diamond-main.o" "$TMP/diamond-main.s"
"$CPU_LD" --shared --soname=libcommon.so.1 -o "$TMP/lib/libcommon.so.1.0.0" "$TMP/common.o"
ln -s libcommon.so.1.0.0 "$TMP/lib/libcommon.so.1"
ln -s libcommon.so.1 "$TMP/lib/libcommon.so"
"$CPU_LD" --shared --soname=libda.so.1 -L"$TMP/lib" -lcommon \
-o "$TMP/lib/libda.so.1.0.0" "$TMP/da.o"
ln -s libda.so.1.0.0 "$TMP/lib/libda.so.1"
ln -s libda.so.1 "$TMP/lib/libda.so"
"$CPU_LD" --shared --soname=libdb.so.1 -L"$TMP/lib" -lcommon \
-o "$TMP/lib/libdb.so.1.0.0" "$TMP/db.o"
ln -s libdb.so.1.0.0 "$TMP/lib/libdb.so.1"
ln -s libdb.so.1 "$TMP/lib/libdb.so"
"$CPU_LD" --dynamic --stack-size=4096 --no-heap \
-L"$TMP/lib" -lda -ldb -o "$TMP/diamond-main" "$TMP/diamond-main.o"
[ "`"$CPU_RUN" --library-path="$TMP/lib" --int-reg=r64@0x00018 "$TMP/diamond-main"`" = 42 ]
exit 0
|