| 1 | // Test that basic MachO static initializers work. The main function in this |
| 2 | // test returns the value of 'x', which is initially 1 in the data section, |
| 3 | // and reset to 0 if the _static_init function is run. If the static initializer |
| 4 | // does not run then main will return 1, causing the test to be treated as a |
| 5 | // failure. |
| 6 | // |
| 7 | // RUN: %clang -c -o %t %s |
| 8 | // RUN: %llvm_jitlink %t |
| 9 | |
| 10 | .section __TEXT,__text,regular,pure_instructions |
| 11 | .build_version macos, 12, 0 sdk_version 12, 0 |
| 12 | |
| 13 | # static initializer sets the value of 'x' to zero. |
| 14 | .globl _static_init |
| 15 | .p2align 2 |
| 16 | _static_init: |
| 17 | .cfi_startproc |
| 18 | ; %bb.0: |
| 19 | adrp x8, _x@PAGE |
| 20 | str wzr, [x8, _x@PAGEOFF] |
| 21 | ret |
| 22 | .cfi_endproc |
| 23 | |
| 24 | # main returns the value of 'x', which is defined as 1 in the data section.. |
| 25 | .globl _main |
| 26 | .p2align 2 |
| 27 | _main: |
| 28 | sub sp, sp, #16 |
| 29 | str wzr, [sp, #12] |
| 30 | adrp x8, _x@PAGE |
| 31 | ldr w0, [x8, _x@PAGEOFF] |
| 32 | add sp, sp, #16 |
| 33 | ret |
| 34 | |
| 35 | .section __DATA,__data |
| 36 | .globl _x |
| 37 | .p2align 2 |
| 38 | _x: |
| 39 | .long 1 |
| 40 | |
| 41 | .section __DATA,__mod_init_func,mod_init_funcs |
| 42 | .p2align 3 |
| 43 | .quad _static_init |
| 44 | .subsections_via_symbols |