vinix-ports / tests / toolchain / musl-sysroot.v
70 lines · 65 sloc · 3.11 KB · c2a13bc9c09b6c0588cf4a00ec02b2503f0e84c6
Raw
1// Check GNU ld's handling of absolute filenames in a musl SDK linker script.
2module main
3
4import os
5
6fn checked(argv []string) ! {
7 result := os.exec(argv)
8 if result.exit_code != 0 { return error('${argv}: ${result.output}') }
9}
10
11fn check(arch string, sdk string, work string) ! {
12 if arch !in ['x86_64', 'aarch64'] || !os.is_abs_path(sdk) || !os.is_abs_path(work) {
13 return error('expected a supported architecture and absolute SDK and work paths')
14 }
15 os.mkdir(work)!
16 root := os.join_path(work, 'sysroot')
17 lib := os.join_path(root, 'usr/lib')
18 os.mkdir_all(lib)!
19 checked(['cp', '-a', os.join_path(sdk, 'lib'), root])!
20 for name in ['musl-gcc-sysroot.specs', 'Scrt1.o', 'crti.o', 'crtn.o', 'libc.so', 'libssp_nonshared.a'] {
21 checked(['cp', '-a', os.join_path(sdk, 'usr/lib', name), lib])!
22 }
23 cc := [os.getenv('CROSS_COMPILE') + 'gcc', '--sysroot=' + root, '-specs=' +
24 os.join_path(lib, 'musl-gcc-sysroot.specs')]
25 provider := os.join_path(work, 'provider.c')
26 consumer := os.join_path(work, 'consumer.c')
27 os.write_file(provider, 'int vinix_sdk_probe(void) { return 42; }\n')!
28 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')!
29 soname := 'libvinix-sdk-probe.so.1'
30 target_dso := os.join_path(lib, soname)
31 checked([...cc, '-shared', '-fPIC', '-Wl,-soname,' + soname, provider, '-o', target_dso])!
32 linker_script := os.join_path(lib, 'libvinix-sdk-probe.so')
33 os.write_file(linker_script, 'GROUP ( /usr/lib/${soname} AS_NEEDED ( -lc ) )\n')!
34 app := os.join_path(work, 'consumer')
35 link := [...cc, consumer, '-lvinix-sdk-probe', '-lssp_nonshared', '-Wl,-Map,' + os.join_path(work, 'consumer.map'), '-o', app]
36 checked(link)!
37 mut run := [os.join_path(root, 'lib', 'ld-musl-${arch}.so.1'), '--library-path',
38 lib + ':' + os.join_path(root, 'lib'), app]
39 if arch != os.uname().machine { run.prepend('qemu-' + arch) }
40 checked(run)!
41 map_text := os.read_file(os.join_path(work, 'consumer.map'))!
42 if !map_text.contains(os.join_path(lib, 'libssp_nonshared.a(__stack_chk_fail_local.o)')) {
43 return error('expected the packaged libssp_nonshared.a to supply __stack_chk_fail_local')
44 }
45 println('The linked stack-protector thunk came from the packaged development archive.')
46 println('${arch} linked and ran the target library from GROUP(/usr/lib/${soname} AS_NEEDED(-lc)).')
47
48 // The same target-compatible ELF is now accessible only by a host path.
49 host_dso := os.join_path(work, soname)
50 os.rename(target_dso, host_dso)!
51 os.rm(app)!
52 os.write_file(linker_script, 'GROUP ( "${host_dso}" AS_NEEDED ( -lc ) )\n')!
53 result := os.exec(link)
54 if result.exit_code == 0 || !result.output.contains(host_dso) || !result.output.contains(root) {
55 return error('expected a sysroot lookup failure for the host-only library: ${result.output}')
56 }
57 println(result.output.trim_space())
58 println('${arch} rejected the same SONAME stored only outside the SDK.')
59}
60
61fn main() {
62 if os.args.len != 4 {
63 eprintln('usage: musl-sysroot ARCH INSTALLED_ROOT NEW_WORK_DIR')
64 exit(2)
65 }
66 check(os.args[1], os.args[2], os.args[3]) or {
67 eprintln(err)
68 exit(1)
69 }
70}
71