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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
#!/usr/bin/bash
#
# illumos-mapfile-convert - Convert illumos v2 mapfiles to GNU ld version scripts
#
# Converts illumos v2 mapfiles ($mapfile_version 2) to GNU ld --version-script
# format, preserving the full version hierarchy so that GNU ld produces proper
# .gnu.version_d (VERDEF) sections.
#
# Previous versions of this script collapsed all versions into a single flat
# ILLUMOS_1.0 block, which caused GNU ld to omit VERDEF sections entirely.
# Binaries compiled with -Bdirect (recording symbol-to-version bindings)
# would fail at runtime with "referenced symbol not found" errors.
#
# Usage: illumos-mapfile-convert <input-mapfile> [output-file]
# If no output file specified, outputs to stdout.
#
set -e
input="$1"
output="${2:-/dev/stdout}"
if [[ -z "$input" ]] || [[ ! -f "$input" ]]; then
echo "Usage: $0 <input-mapfile> [output-file]" >&2
exit 1
fi
# Check if this is actually an illumos v2 mapfile
if ! grep -q '^\$mapfile_version' "$input" 2>/dev/null; then
# Not a v2 mapfile, just copy it through
cat "$input" > "$output"
exit 0
fi
# Convert the mapfile using awk
#
# Strategy: Parse each SYMBOL_VERSION block, preserving:
# - Version node names
# - Parent version inheritance (} PARENT;)
# - Symbol names within global:/protected: sections
# - Empty version nodes (needed for backward compat)
# - local: * directives
#
# Strip (no GNU ld equivalent):
# - TYPE, ASSERT, FLAGS annotations
# - FILTER/AUXILIARY directives (handled by gnu-ld-wrapper -F flag)
# - $mapfile_version directive
#
# Handle $if/$endif conditionals for hammerhead (amd64, ELF64 only):
# TRUE: _x86, _ELF64, _x86 && _ELF64, amd64, !(_x86 && _ELF32), amd64 || sparcv9
# FALSE: _ELF32, _sparc, _ELF32, i386, sparc32, _sparc && _ELF32, _sparc && _ELF64
#
awk '
BEGIN {
# State tracking
current_version = ""
parent_version = ""
in_global = 0
in_local = 0
skip_depth = 0 # depth of nested $if blocks being skipped
if_depth = 0 # total $if nesting depth
skip_assert = 0
brace_depth = 0
# Version data: indexed by version name
# version_symbols[name] = "sym1\nsym2\n..."
# version_parent[name] = parent name or ""
# version_has_local_star[name] = 1 if has local: *
# version_order[i] = name (preserves declaration order)
num_versions = 0
}
# Evaluate $if conditions for hammerhead (amd64, x86, ELF64 only)
function eval_condition(cond) {
# Strip leading/trailing whitespace and trailing comments
gsub(/^[[:space:]]+/, "", cond)
gsub(/[[:space:]]+$/, "", cond)
gsub(/[[:space:]]*#.*$/, "", cond)
# Negation
if (cond ~ /^!/) {
inner = substr(cond, 2)
gsub(/^[[:space:]]*\(/, "", inner)
gsub(/\)[[:space:]]*$/, "", inner)
return !eval_condition(inner)
}
# Handle || (OR)
if (cond ~ /\|\|/) {
n = split(cond, parts, /[[:space:]]*\|\|[[:space:]]*/)
for (i = 1; i <= n; i++) {
if (eval_condition(parts[i])) return 1
}
return 0
}
# Handle && (AND)
if (cond ~ /&&/) {
n = split(cond, parts, /[[:space:]]*&&[[:space:]]*/)
for (i = 1; i <= n; i++) {
if (!eval_condition(parts[i])) return 0
}
return 1
}
# Strip parens from simple terms
gsub(/[()]/, "", cond)
gsub(/^[[:space:]]+/, "", cond)
gsub(/[[:space:]]+$/, "", cond)
# Evaluate individual platform/ABI predicates
# Hammerhead is amd64 (x86, ELF64) only
if (cond == "_x86") return 1
if (cond == "_ELF64") return 1
if (cond == "amd64") return 1
if (cond == "sparcv9") return 0
if (cond == "_sparc") return 0
if (cond == "_ELF32") return 0
if (cond == "i386") return 0
if (cond == "sparc32") return 0
if (cond == "_amd64") return 1
# Unknown condition - include by default (safe)
return 1
}
# Skip the mapfile version directive
/^\$mapfile_version/ { next }
# Skip comments
/^#/ { next }
# Handle $if conditionals
/^\$if[[:space:]]/ {
if_depth++
if (skip_depth > 0) {
# Already skipping an outer block, keep skipping
skip_depth++
next
}
cond = substr($0, 5)
if (!eval_condition(cond)) {
skip_depth = 1
}
next
}
/^\$elif[[:space:]]/ {
if (skip_depth == 1) {
# Was skipping, check if this branch is true
cond = substr($0, 7)
if (eval_condition(cond)) {
skip_depth = 0
}
} else if (skip_depth == 0) {
# Previous branch was taken, now skip
skip_depth = 1
}
# If skip_depth > 1, we are in a nested skip, stay there
next
}
/^\$else/ {
if (skip_depth == 1) {
skip_depth = 0
} else if (skip_depth == 0) {
skip_depth = 1
}
# If skip_depth > 1, stay nested
next
}
/^\$endif/ {
if_depth--
if (skip_depth > 0) {
skip_depth--
}
next
}
# Skip content inside false $if blocks
skip_depth > 0 { next }
# Skip $add directives
/^\$add/ { next }
# Handle ASSERT blocks that span multiple lines
skip_assert == 1 {
line = $0
opens = gsub(/{/, "{", line)
closes = gsub(/}/, "}", line)
brace_depth = brace_depth + opens - closes
if (brace_depth <= 0) {
skip_assert = 0
brace_depth = 0
}
next
}
# Detect SYMBOL_VERSION block start
# Format: SYMBOL_VERSION VERSION_NAME { ... } [PARENT];
/^SYMBOL_VERSION[[:space:]]/ {
# Extract version name
line = $0
sub(/^SYMBOL_VERSION[[:space:]]+/, "", line)
# Version name is everything up to optional { or whitespace
match(line, /^[A-Za-z_][A-Za-z0-9_.]*/)
if (RLENGTH > 0) {
current_version = substr(line, 1, RLENGTH)
# Initialize this version if new
if (!(current_version in version_parent)) {
version_parent[current_version] = ""
version_has_local_star[current_version] = 0
version_order[num_versions++] = current_version
}
}
in_global = 0
in_local = 0
next
}
# Detect SYMBOL_SCOPE block
/^SYMBOL_SCOPE[[:space:]]*\{/ {
current_version = "__SCOPE__"
if (!(current_version in version_parent)) {
version_parent[current_version] = ""
version_has_local_star[current_version] = 0
version_order[num_versions++] = current_version
}
in_global = 0
in_local = 0
next
}
# Track global: section
/^[[:space:]]*global:/ {
in_global = 1
in_local = 0
next
}
# Track protected: section (promote to global for GNU ld)
/^[[:space:]]*protected:/ {
in_global = 1
in_local = 0
next
}
# Track local: section
/^[[:space:]]*local:/ {
in_global = 0
in_local = 1
next
}
# End of version block - capture parent
/^}/ {
if (current_version != "") {
# Extract parent version if present: } PARENT;
line = $0
sub(/^}[[:space:]]*/, "", line)
sub(/;[[:space:]]*$/, "", line)
gsub(/[[:space:]]+$/, "", line)
if (line != "" && line !~ /^#/) {
version_parent[current_version] = line
}
}
current_version = ""
in_global = 0
in_local = 0
next
}
# Handle standalone ASSERT blocks
/^[[:space:]]*ASSERT[[:space:]]*=/ {
skip_assert = 1
line = $0
opens = gsub(/{/, "{", line)
closes = gsub(/}/, "}", line)
brace_depth = opens - closes
if (brace_depth <= 0) {
skip_assert = 0
brace_depth = 0
}
next
}
# Handle symbol with inline attributes: sym { TYPE=...; FILTER=...; ASSERT=...; };
/^[[:space:]]*[a-zA-Z_][a-zA-Z0-9_]*[[:space:]]+\{/ {
if (in_global && current_version != "") {
# Extract symbol name
line = $0
gsub(/^[[:space:]]+/, "", line)
match(line, /^[a-zA-Z_][a-zA-Z0-9_]*/)
if (RLENGTH > 0) {
symbol = substr(line, 1, RLENGTH)
# Skip symbols with FLAGS = EXTERN (external refs, not exports)
if (line ~ /FLAGS[[:space:]]*=[[:space:]]*EXTERN/) next
# Check for multi-line ASSERT (count braces)
opens = gsub(/{/, "{", line)
closes = gsub(/}/, "}", line)
brace_depth = opens - closes
if (brace_depth > 0) {
skip_assert = 1
}
version_symbols[current_version] = version_symbols[current_version] symbol "\n"
}
}
next
}
# Detect version block start line (bare version name with {)
# This handles: VERSION_NAME {
/^[A-Za-z_][A-Za-z0-9_.]*[[:space:]]*\{/ {
if (current_version == "") {
# Standalone version block (not preceded by SYMBOL_VERSION)
match($0, /^[A-Za-z_][A-Za-z0-9_.]*/)
if (RLENGTH > 0) {
current_version = substr($0, 1, RLENGTH)
if (!(current_version in version_parent)) {
version_parent[current_version] = ""
version_has_local_star[current_version] = 0
version_order[num_versions++] = current_version
}
}
in_global = 0
in_local = 0
}
next
}
# Collect simple symbol lines in global section
current_version != "" && in_global == 1 {
line = $0
gsub(/^[[:space:]]+/, "", line)
gsub(/[[:space:]]+$/, "", line)
gsub(/;[[:space:]]*$/, "", line)
gsub(/[[:space:]]*#.*$/, "", line) # strip inline comments
# Skip empty lines
if (line == "") next
# Skip wildcard (handled in local section)
if (line == "*") next
# Skip lines that are just the version name (self-referencing empty versions)
# e.g., SUNW_1.5 inside SYMBOL_VERSION SUNW_1.5
if (line == current_version) next
# Skip FLAGS = EXTERN
if (line ~ /FLAGS[[:space:]]*=[[:space:]]*EXTERN/) next
# Extract just the symbol name (first identifier)
match(line, /^[a-zA-Z_][a-zA-Z0-9_]*/)
if (RLENGTH > 0) {
symbol = substr(line, 1, RLENGTH)
version_symbols[current_version] = version_symbols[current_version] symbol "\n"
}
}
# Track local: * in version block
current_version != "" && in_local == 1 && /\*/ {
version_has_local_star[current_version] = 1
}
END {
print "/* Generated from illumos v2 mapfile - version hierarchy preserved */"
print ""
# Pre-pass: fold SYMBOL_SCOPE (__SCOPE__) content into named versions.
# GNU ld rejects mixing anonymous and named version tags in one file.
# If named versions exist, merge __SCOPE__ symbols and local: * into
# the last leaf named version, then remove __SCOPE__ from the list.
if ("__SCOPE__" in version_parent) {
named_count = 0
for (i = 0; i < num_versions; i++) {
if (version_order[i] != "__SCOPE__") named_count++
}
if (named_count > 0) {
# Find leaf versions (not a parent of any other)
for (i = 0; i < num_versions; i++) {
_is_parent[version_order[i]] = 0
}
for (i = 0; i < num_versions; i++) {
_p = version_parent[version_order[i]]
if (_p != "") _is_parent[_p] = 1
}
# Attach to last leaf
for (i = num_versions - 1; i >= 0; i--) {
_v = version_order[i]
if (_v != "__SCOPE__" && !_is_parent[_v]) {
if (version_symbols["__SCOPE__"] != "") {
version_symbols[_v] = version_symbols[_v] version_symbols["__SCOPE__"]
}
if (version_has_local_star["__SCOPE__"]) {
version_has_local_star[_v] = 1
}
break
}
}
# Remove __SCOPE__ from version list
new_count = 0
for (i = 0; i < num_versions; i++) {
if (version_order[i] != "__SCOPE__") {
version_order[new_count++] = version_order[i]
}
}
num_versions = new_count
}
}
# GNU ld requires parent versions to be defined BEFORE they are
# referenced as dependencies. Topological sort: emit roots first,
# then children.
emitted_count = 0
for (i = 0; i < num_versions; i++) emitted[version_order[i]] = 0
# Iterative topo sort (safe for any DAG depth)
changed = 1
while (changed) {
changed = 0
for (i = 0; i < num_versions; i++) {
ver = version_order[i]
if (emitted[ver]) continue
parent = version_parent[ver]
# Emit if no parent or parent already emitted
if (parent == "" || emitted[parent]) {
emit_order[emitted_count++] = ver
emitted[ver] = 1
changed = 1
}
}
}
# Any remaining (orphan parent refs) - emit in original order
for (i = 0; i < num_versions; i++) {
ver = version_order[i]
if (!emitted[ver]) {
emit_order[emitted_count++] = ver
emitted[ver] = 1
}
}
for (i = 0; i < emitted_count; i++) {
ver = emit_order[i]
parent = version_parent[ver]
# __SCOPE__ only reaches here when there are no named versions
# (it was already folded into named versions in the pre-pass above)
if (ver == "__SCOPE__") {
print "{"
if (version_symbols[ver] != "") {
print " global:"
n = split(version_symbols[ver], syms, "\n")
for (s = 1; s <= n; s++) {
if (syms[s] != "") {
print " " syms[s] ";"
}
}
}
if (version_has_local_star[ver]) {
print " local:"
print " *;"
}
print "};"
print ""
continue
}
# Emit version block
print ver " {"
# Emit global symbols
if (version_symbols[ver] != "") {
print " global:"
n = split(version_symbols[ver], syms, "\n")
for (s = 1; s <= n; s++) {
if (syms[s] != "") {
print " " syms[s] ";"
}
}
}
# Emit local: * if present
if (version_has_local_star[ver]) {
print " local:"
print " *;"
}
# Close with parent inheritance
if (parent != "") {
print "} " parent ";"
} else {
print "};"
}
print ""
}
}
' "$input" > "$output"
|