103 lines · cpp
1// RUN: %clang_cc1 -emit-llvm -triple arm-none-eabi -o - %s | FileCheck %s --check-prefixes=CHECK,ELF2// RUN: %clang_cc1 -emit-llvm -triple arm64-apple-ios -o - %s | FileCheck %s --check-prefixes=CHECK,MACHO3// Test that global variables, statics and functions are attached section-attributes4// as per '#pragma clang section' directives.5 6extern "C" {7// test with names for each section8#ifdef __MACH__9#pragma clang section bss = "__BSS,__mybss1" data = "__DATA,__mydata1" rodata = "__RODATA,__myrodata1"10#pragma clang section text = "__TEXT,__mytext1"11#else12#pragma clang section bss="my_bss.1" data="my_data.1" rodata="my_rodata.1"13#pragma clang section text="my_text.1"14#endif15int a; // my_bss.116int b = 1; // my_data.117int c[4]; // my_bss.118short d[5] = {0}; // my_bss.119short e[6] = {0, 0, 1}; // my_data.120extern const int f;21const int f = 2; // my_rodata.122int foo(void) { // my_text.123 return b;24}25static int g[2]; // my_bss.126#pragma clang section bss=""27int h; // default - .bss28#ifdef __MACH__29#pragma clang section data = "" bss = "__BSS,__mybss2" text = "__TEXT,__mytext2"30#else31#pragma clang section data="" bss="my_bss.2" text="my_text.2"32#endif33int i = 0; // my_bss.234extern const int j;35const int j = 4; // default - .rodata36int k; // my_bss.237extern int zoo(int *x, int *y);38int goo(void) { // my_text.239 static int lstat_h; // my_bss.240 return zoo(g, &lstat_h);41}42#ifdef __MACH__43#pragma clang section rodata = "__RODATA,__myrodata2" data = "__DATA,__mydata2" relro = "__RELRO,__myrelro2"44#else45#pragma clang section rodata="my_rodata.2" data="my_data.2" relro="my_relro.2"46#endif47int l = 5; // my_data.248extern const int m;49const int m = 6; // my_rodata.250 51typedef int (*fptr_t)(void);52const fptr_t fptrs[2] = {&foo, &goo};53#pragma clang section rodata="" data="" bss="" text=""54int n; // default55int o = 6; // default56extern const int p;57const int p = 7; // default58int hoo(void) {59 return b + fptrs[f]();60}61}62//CHECK: @a ={{.*}} global i32 0, align 4 #063//CHECK: @b ={{.*}} global i32 1, align 4 #064//CHECK: @c ={{.*}} global [4 x i32] zeroinitializer, align 4 #065//CHECK: @d ={{.*}} global [5 x i16] zeroinitializer, align 2 #066//CHECK: @e ={{.*}} global [6 x i16] [i16 0, i16 0, i16 1, i16 0, i16 0, i16 0], align 2 #067//CHECK: @f ={{.*}} constant i32 2, align 4 #068 69//CHECK: @h ={{.*}} global i32 0, align 4 #170//CHECK: @i ={{.*}} global i32 0, align 4 #271//CHECK: @j ={{.*}} constant i32 4, align 4 #272//CHECK: @k ={{.*}} global i32 0, align 4 #273//CHECK: @_ZZ3gooE7lstat_h = internal global i32 0, align 4 #274//CHECK: @_ZL1g = internal global [2 x i32] zeroinitializer, align 4 #075 76//CHECK: @l ={{.*}} global i32 5, align 4 #377//CHECK: @m ={{.*}} constant i32 6, align 4 #378 79//CHECK: @n ={{.*}} global i32 0, align 480//CHECK: @o ={{.*}} global i32 6, align 481//CHECK: @p ={{.*}} constant i32 7, align 482//CHECK: @_ZL5fptrs = internal constant [2 x ptr] [ptr @foo, ptr @goo], align {{4|8}} #383 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" {88 89// ensure zoo/hoo don't have a section90//CHECK: declare i32 @zoo(ptr noundef, ptr noundef) #6{{$}}91//CHECK: define{{.*}} i32 @hoo() #5 {92 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" }103