| 1 | // Check GNU ld's handling of absolute filenames in a musl SDK linker script. |
| 2 | module main |
| 3 | |
| 4 | import os |
| 5 | import strconv |
| 6 | |
| 7 | fn check_load_segments(path string, page u64) ! { |
| 8 | result := os.exec(['readelf', '-lW', path]) |
| 9 | if result.exit_code != 0 { return error(result.output) } |
| 10 | mut count := 0 |
| 11 | for line in result.output.split_into_lines() { |
| 12 | fields := line.fields() |
| 13 | if fields.len == 0 || fields[0] != 'LOAD' { continue } |
| 14 | offset := strconv.parse_uint(fields[1], 0, 64)! |
| 15 | address := strconv.parse_uint(fields[2], 0, 64)! |
| 16 | alignment := strconv.parse_uint(fields.last(), 0, 64)! |
| 17 | if alignment < page || address % page != offset % page { |
| 18 | return error('LOAD segment cannot use ${page}-byte Vinix pages in ${path}: ${line}') |
| 19 | } |
| 20 | count++ |
| 21 | } |
| 22 | if count == 0 { return error('no loadable ELF segments in ${path}') } |
| 23 | println('${os.base(path)} has ${count} LOAD segments compatible with ${page}-byte Vinix pages.') |
| 24 | } |
| 25 | |
| 26 | |
| 27 | fn checked(argv []string) ! { |
| 28 | result := os.exec(argv) |
| 29 | if result.exit_code != 0 { return error('${argv}: ${result.output}') } |
| 30 | } |
| 31 | |
| 32 | fn check(arch string, sdk string, work string) ! { |
| 33 | if arch !in ['x86_64', 'aarch64'] || !os.is_abs_path(sdk) || !os.is_abs_path(work) { |
| 34 | return error('expected a supported architecture and absolute SDK and work paths') |
| 35 | } |
| 36 | os.mkdir(work)! |
| 37 | root := os.join_path(work, 'sysroot') |
| 38 | lib := os.join_path(root, 'usr/lib') |
| 39 | os.mkdir_all(lib)! |
| 40 | checked(['cp', '-a', os.join_path(sdk, 'lib'), root])! |
| 41 | for name in ['musl-gcc-sysroot.specs', 'Scrt1.o', 'crti.o', 'crtn.o', 'libc.so', 'libssp_nonshared.a'] { |
| 42 | checked(['cp', '-a', os.join_path(sdk, 'usr/lib', name), lib])! |
| 43 | } |
| 44 | cc := [os.getenv('CROSS_COMPILE') + 'gcc', '--sysroot=' + root, '-specs=' + |
| 45 | os.join_path(lib, 'musl-gcc-sysroot.specs')] |
| 46 | provider := os.join_path(work, 'provider.c') |
| 47 | consumer := os.join_path(work, 'consumer.c') |
| 48 | os.write_file(provider, 'int vinix_sdk_probe(void) { return 42; }\n')! |
| 49 | os.write_file(consumer, 'extern void __stack_chk_fail_local(void);\nint vinix_sdk_probe(void);\nint main(void) { if (vinix_sdk_probe() != 42) __stack_chk_fail_local(); return 0; }\n')! |
| 50 | soname := 'libvinix-sdk-probe.so.1' |
| 51 | target_dso := os.join_path(lib, soname) |
| 52 | checked([...cc, '-shared', '-fPIC', '-Wl,-soname,' + soname, provider, '-o', target_dso])! |
| 53 | linker_script := os.join_path(lib, 'libvinix-sdk-probe.so') |
| 54 | os.write_file(linker_script, 'GROUP ( /usr/lib/${soname} AS_NEEDED ( -lc ) )\n')! |
| 55 | app := os.join_path(work, 'consumer') |
| 56 | link := [...cc, consumer, '-lvinix-sdk-probe', '-lssp_nonshared', '-Wl,-Map,' + os.join_path(work, 'consumer.map'), '-o', app] |
| 57 | checked(link)! |
| 58 | page := if arch == 'aarch64' { u64(16384) } else { u64(4096) } |
| 59 | for file in [target_dso, app, os.join_path(root, 'lib', 'ld-musl-' + arch + '.so.1')] { |
| 60 | check_load_segments(file, page)! |
| 61 | } |
| 62 | mut run := [os.join_path(root, 'lib', 'ld-musl-${arch}.so.1'), '--library-path', |
| 63 | lib + ':' + os.join_path(root, 'lib'), app] |
| 64 | if arch != os.uname().machine { run.prepend('qemu-' + arch) } |
| 65 | checked(run)! |
| 66 | map_text := os.read_file(os.join_path(work, 'consumer.map'))! |
| 67 | if !map_text.contains(os.join_path(lib, 'libssp_nonshared.a(__stack_chk_fail_local.o)')) { |
| 68 | return error('expected the packaged libssp_nonshared.a to supply __stack_chk_fail_local') |
| 69 | } |
| 70 | println('The linked stack-protector thunk came from the packaged development archive.') |
| 71 | println('${arch} linked and ran the target library from GROUP(/usr/lib/${soname} AS_NEEDED(-lc)).') |
| 72 | |
| 73 | // The same target-compatible ELF is now accessible only by a host path. |
| 74 | host_dso := os.join_path(work, soname) |
| 75 | os.rename(target_dso, host_dso)! |
| 76 | os.rm(app)! |
| 77 | os.write_file(linker_script, 'GROUP ( "${host_dso}" AS_NEEDED ( -lc ) )\n')! |
| 78 | result := os.exec(link) |
| 79 | if result.exit_code == 0 || !result.output.contains(host_dso) || !result.output.contains(root) { |
| 80 | return error('expected a sysroot lookup failure for the host-only library: ${result.output}') |
| 81 | } |
| 82 | println(result.output.trim_space()) |
| 83 | println('${arch} rejected the same SONAME stored only outside the SDK.') |
| 84 | } |
| 85 | |
| 86 | fn main() { |
| 87 | if os.args.len != 4 { |
| 88 | eprintln('usage: musl-sysroot ARCH INSTALLED_ROOT NEW_WORK_DIR') |
| 89 | exit(2) |
| 90 | } |
| 91 | check(os.args[1], os.args[2], os.args[3]) or { |
| 92 | eprintln(err) |
| 93 | exit(1) |
| 94 | } |
| 95 | } |
| 96 | |