[RISCV] Add shrinkwrap test cases showing gaps in current impl
[llvm-project.git] / clang / test / CodeGenCXX / clang-sections.cpp
blobaa159e552b1b3c310cec8cd97ca3d68987e9610a
1 // RUN: %clang_cc1 -emit-llvm -triple arm-none-eabi -o - %s | FileCheck %s --check-prefixes=CHECK,ELF
2 // RUN: %clang_cc1 -emit-llvm -triple arm64-apple-ios -o - %s | FileCheck %s --check-prefixes=CHECK,MACHO
3 // Test that global variables, statics and functions are attached section-attributes
4 // as per '#pragma clang section' directives.
6 extern "C" {
7 // test with names for each section
8 #ifdef __MACH__
9 #pragma clang section bss = "__BSS,__mybss1" data = "__DATA,__mydata1" rodata = "__RODATA,__myrodata1"
10 #pragma clang section text = "__TEXT,__mytext1"
11 #else
12 #pragma clang section bss="my_bss.1" data="my_data.1" rodata="my_rodata.1"
13 #pragma clang section text="my_text.1"
14 #endif
15 int a; // my_bss.1
16 int b = 1; // my_data.1
17 int c[4]; // my_bss.1
18 short d[5] = {0}; // my_bss.1
19 short e[6] = {0, 0, 1}; // my_data.1
20 extern const int f;
21 const int f = 2; // my_rodata.1
22 int foo(void) { // my_text.1
23 return b;
25 static int g[2]; // my_bss.1
26 #pragma clang section bss=""
27 int h; // default - .bss
28 #ifdef __MACH__
29 #pragma clang section data = "" bss = "__BSS,__mybss2" text = "__TEXT,__mytext2"
30 #else
31 #pragma clang section data="" bss="my_bss.2" text="my_text.2"
32 #endif
33 int i = 0; // my_bss.2
34 extern const int j;
35 const int j = 4; // default - .rodata
36 int k; // my_bss.2
37 extern int zoo(int *x, int *y);
38 int goo(void) { // my_text.2
39 static int lstat_h; // my_bss.2
40 return zoo(g, &lstat_h);
42 #ifdef __MACH__
43 #pragma clang section rodata = "__RODATA,__myrodata2" data = "__DATA,__mydata2" relro = "__RELRO,__myrelro2"
44 #else
45 #pragma clang section rodata="my_rodata.2" data="my_data.2" relro="my_relro.2"
46 #endif
47 int l = 5; // my_data.2
48 extern const int m;
49 const int m = 6; // my_rodata.2
51 typedef int (*fptr_t)(void);
52 const fptr_t fptrs[2] = {&foo, &goo};
53 #pragma clang section rodata="" data="" bss="" text=""
54 int n; // default
55 int o = 6; // default
56 extern const int p;
57 const int p = 7; // default
58 int hoo(void) {
59 return b + fptrs[f]();
62 //CHECK: @a ={{.*}} global i32 0, align 4 #0
63 //CHECK: @b ={{.*}} global i32 1, align 4 #0
64 //CHECK: @c ={{.*}} global [4 x i32] zeroinitializer, align 4 #0
65 //CHECK: @d ={{.*}} global [5 x i16] zeroinitializer, align 2 #0
66 //CHECK: @e ={{.*}} global [6 x i16] [i16 0, i16 0, i16 1, i16 0, i16 0, i16 0], align 2 #0
67 //CHECK: @f ={{.*}} constant i32 2, align 4 #0
69 //CHECK: @h ={{.*}} global i32 0, align 4 #1
70 //CHECK: @i ={{.*}} global i32 0, align 4 #2
71 //CHECK: @j ={{.*}} constant i32 4, align 4 #2
72 //CHECK: @k ={{.*}} global i32 0, align 4 #2
73 //CHECK: @_ZZ3gooE7lstat_h = internal global i32 0, align 4 #2
74 //CHECK: @_ZL1g = internal global [2 x i32] zeroinitializer, align 4 #0
76 //CHECK: @l ={{.*}} global i32 5, align 4 #3
77 //CHECK: @m ={{.*}} constant i32 6, align 4 #3
79 //CHECK: @n ={{.*}} global i32 0, align 4
80 //CHECK: @o ={{.*}} global i32 6, align 4
81 //CHECK: @p ={{.*}} constant i32 7, align 4
82 //CHECK: @_ZL5fptrs = internal constant [2 x ptr] [ptr @foo, ptr @goo], align {{4|8}} #3
84 //ELF: define{{.*}} i32 @foo(){{.*}} section "my_text.1" {
85 //ELF: define{{.*}} i32 @goo(){{.*}} section "my_text.2" {
86 //MACHO: define{{.*}} i32 @foo(){{.*}} section "__TEXT,__mytext1" {
87 //MACHO: define{{.*}} i32 @goo(){{.*}} section "__TEXT,__mytext2" {
89 // ensure zoo/hoo don't have a section
90 //CHECK: declare i32 @zoo(ptr noundef, ptr noundef) #6{{$}}
91 //CHECK: define{{.*}} i32 @hoo() #5 {
93 //ELF: attributes #0 = { "bss-section"="my_bss.1" "data-section"="my_data.1" "rodata-section"="my_rodata.1" }
94 //ELF: attributes #1 = { "data-section"="my_data.1" "rodata-section"="my_rodata.1" }
95 //ELF: attributes #2 = { "bss-section"="my_bss.2" "rodata-section"="my_rodata.1" }
96 //ELF: attributes #3 = { "bss-section"="my_bss.2" "data-section"="my_data.2" "relro-section"="my_relro.2" "rodata-section"="my_rodata.2" }
97 //ELF: attributes #4 = { "relro-section"="my_relro.2" }
98 //MACHO: attributes #0 = { "bss-section"="__BSS,__mybss1" "data-section"="__DATA,__mydata1" "rodata-section"="__RODATA,__myrodata1" }
99 //MACHO: attributes #1 = { "data-section"="__DATA,__mydata1" "rodata-section"="__RODATA,__myrodata1" }
100 //MACHO: attributes #2 = { "bss-section"="__BSS,__mybss2" "rodata-section"="__RODATA,__myrodata1" }
101 //MACHO: attributes #3 = { "bss-section"="__BSS,__mybss2" "data-section"="__DATA,__mydata2" "relro-section"="__RELRO,__myrelro2" "rodata-section"="__RODATA,__myrodata2" }
102 //MACHO: attributes #4 = { "relro-section"="__RELRO,__myrelro2" }