1 # SPDX-License-Identifier: LGPLv3+
2 # Copyright (C) 2023 Jacob Lifshay <programmerjake@gmail.com>
3 # Funded by NLnet http://nlnet.nl
4 """ simple ELF test cases
8 * https://bugs.libre-soc.org/show_bug.cgi?id=1169
11 from openpower
.test
.common
import TestAccumulatorBase
, skip_case
12 from openpower
.test
.state
import ExpectedState
13 from openpower
.test
.elf
import compile_elf
14 from openpower
.consts
import MSR
, DEFAULT_MSR
17 #include <sys/syscall.h>
19 long syscall(long number, ...);
21 asm(".globl syscall\n"
23 ".type syscall,@function\n"
38 static const char msg[] = "Hello World!\n";
39 syscall(SYS_write, 1, (const void *)msg, sizeof(msg) - 1);
40 syscall(SYS_exit_group, 0);
44 hello_word_data_bss
= r
"""
45 const char msg_in_ro_data[] = "World!\n";
46 char msg_in_data[] = "Hello ";
47 char msg_in_bss[sizeof(msg_in_data)] = {};
50 for(int i = 0; i < sizeof(msg_in_data); i++)
51 msg_in_bss[i] = msg_in_data[i];
52 syscall(SYS_write, 1, (const void *)msg_in_bss, sizeof(msg_in_data) - 1);
53 syscall(SYS_write, 1, (const void *)msg_in_ro_data, sizeof(msg_in_ro_data) - 1);
54 syscall(SYS_exit_group, 0);
60 syscall(SYS_exit_group, 0);
68 printf("Hello World!\n");
73 # we have to specify *all* sprs that our binary might possibly need to
74 # read, because ISACaller is annoying like that...
75 # https://bugs.libre-soc.org/show_bug.cgi?id=1226#c2
76 INITIAL_SPRS
= ('LR', 'CTR', 'TAR', 'SVSTATE', 'SRR0', 'SRR1',
77 'SVSHAPE0', 'SVSHAPE1', 'SVSHAPE2', 'SVSHAPE3')
78 initial_sprs
= dict.fromkeys(INITIAL_SPRS
, 0)
80 DEFAULT_USER_MSR
= DEFAULT_MSR |
(1 << MSR
.PR
) # needs problem state
82 class SimpleCases(TestAccumulatorBase
):
83 def case_hello_world(self
):
84 prog
= compile_elf(SYSCALL_DEF
+ hello_world
)
85 self
.add_case(prog
, initial_sprs
=initial_sprs
.copy(),
86 initial_msr
=DEFAULT_USER_MSR
)
88 def case_hello_world_with_data_and_bss(self
):
89 prog
= compile_elf(SYSCALL_DEF
+ hello_word_data_bss
)
90 self
.add_case(prog
, initial_sprs
=initial_sprs
.copy(),
91 initial_msr
=DEFAULT_USER_MSR
)
93 def case_just_exit(self
):
94 prog
= compile_elf(SYSCALL_DEF
+ just_exit
)
95 self
.add_case(prog
, initial_sprs
=initial_sprs
.copy(),
96 initial_msr
=DEFAULT_USER_MSR
)
98 def case_static_glibc(self
):
99 compiler_args
= '-Os', '-static', '-xc'
100 prog
= compile_elf(static_glibc
, compiler_args
)
101 self
.add_case(prog
, initial_sprs
=initial_sprs
.copy(),
102 initial_msr
=DEFAULT_USER_MSR
)