// Check GNU ld's handling of absolute filenames in a musl SDK linker script. module main import os import strconv fn check_load_segments(path string, page u64) ! { result := os.exec(['readelf', '-lW', path]) if result.exit_code != 0 { return error(result.output) } mut count := 0 for line in result.output.split_into_lines() { fields := line.fields() if fields.len == 0 || fields[0] != 'LOAD' { continue } offset := strconv.parse_uint(fields[1], 0, 64)! address := strconv.parse_uint(fields[2], 0, 64)! alignment := strconv.parse_uint(fields.last(), 0, 64)! if alignment < page || address % page != offset % page { return error('LOAD segment cannot use ${page}-byte Vinix pages in ${path}: ${line}') } count++ } if count == 0 { return error('no loadable ELF segments in ${path}') } println('${os.base(path)} has ${count} LOAD segments compatible with ${page}-byte Vinix pages.') } fn checked(argv []string) ! { result := os.exec(argv) if result.exit_code != 0 { return error('${argv}: ${result.output}') } } fn check(arch string, sdk string, work string) ! { if arch !in ['x86_64', 'aarch64'] || !os.is_abs_path(sdk) || !os.is_abs_path(work) { return error('expected a supported architecture and absolute SDK and work paths') } os.mkdir(work)! root := os.join_path(work, 'sysroot') lib := os.join_path(root, 'usr/lib') os.mkdir_all(lib)! checked(['cp', '-a', os.join_path(sdk, 'lib'), root])! for name in ['musl-gcc-sysroot.specs', 'Scrt1.o', 'crti.o', 'crtn.o', 'libc.so', 'libssp_nonshared.a'] { checked(['cp', '-a', os.join_path(sdk, 'usr/lib', name), lib])! } cc := [os.getenv('CROSS_COMPILE') + 'gcc', '--sysroot=' + root, '-specs=' + os.join_path(lib, 'musl-gcc-sysroot.specs')] provider := os.join_path(work, 'provider.c') consumer := os.join_path(work, 'consumer.c') os.write_file(provider, 'int vinix_sdk_probe(void) { return 42; }\n')! 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')! soname := 'libvinix-sdk-probe.so.1' target_dso := os.join_path(lib, soname) checked([...cc, '-shared', '-fPIC', '-Wl,-soname,' + soname, provider, '-o', target_dso])! linker_script := os.join_path(lib, 'libvinix-sdk-probe.so') os.write_file(linker_script, 'GROUP ( /usr/lib/${soname} AS_NEEDED ( -lc ) )\n')! app := os.join_path(work, 'consumer') link := [...cc, consumer, '-lvinix-sdk-probe', '-lssp_nonshared', '-Wl,-Map,' + os.join_path(work, 'consumer.map'), '-o', app] checked(link)! page := if arch == 'aarch64' { u64(16384) } else { u64(4096) } for file in [target_dso, app, os.join_path(root, 'lib', 'ld-musl-' + arch + '.so.1')] { check_load_segments(file, page)! } mut run := [os.join_path(root, 'lib', 'ld-musl-${arch}.so.1'), '--library-path', lib + ':' + os.join_path(root, 'lib'), app] if arch != os.uname().machine { run.prepend('qemu-' + arch) } checked(run)! map_text := os.read_file(os.join_path(work, 'consumer.map'))! if !map_text.contains(os.join_path(lib, 'libssp_nonshared.a(__stack_chk_fail_local.o)')) { return error('expected the packaged libssp_nonshared.a to supply __stack_chk_fail_local') } println('The linked stack-protector thunk came from the packaged development archive.') println('${arch} linked and ran the target library from GROUP(/usr/lib/${soname} AS_NEEDED(-lc)).') // The same target-compatible ELF is now accessible only by a host path. host_dso := os.join_path(work, soname) os.rename(target_dso, host_dso)! os.rm(app)! os.write_file(linker_script, 'GROUP ( "${host_dso}" AS_NEEDED ( -lc ) )\n')! result := os.exec(link) if result.exit_code == 0 || !result.output.contains(host_dso) || !result.output.contains(root) { return error('expected a sysroot lookup failure for the host-only library: ${result.output}') } println(result.output.trim_space()) println('${arch} rejected the same SONAME stored only outside the SDK.') } fn main() { if os.args.len != 4 { eprintln('usage: musl-sysroot ARCH INSTALLED_ROOT NEW_WORK_DIR') exit(2) } check(os.args[1], os.args[2], os.args[3]) or { eprintln(err) exit(1) } }