clang-sections.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // RUN: %clang_cc1 -emit-llvm -triple arm-none-eabi -o - %s | FileCheck %s
  2. // Test that global variables, statics and functions are attached section-attributes
  3. // as per '#pragma clang section' directives.
  4. extern "C" {
  5. // test with names for each section
  6. #pragma clang section bss="my_bss.1" data="my_data.1" rodata="my_rodata.1"
  7. #pragma clang section text="my_text.1"
  8. int a; // my_bss.1
  9. int b = 1; // my_data.1
  10. int c[4]; // my_bss.1
  11. short d[5] = {0}; // my_bss.1
  12. short e[6] = {0, 0, 1}; // my_data.1
  13. extern const int f;
  14. const int f = 2; // my_rodata.1
  15. int foo(void) { // my_text.1
  16. return b;
  17. }
  18. static int g[2]; // my_bss.1
  19. #pragma clang section bss=""
  20. int h; // default - .bss
  21. #pragma clang section data="" bss="my_bss.2" text="my_text.2"
  22. int i = 0; // my_bss.2
  23. extern const int j;
  24. const int j = 4; // default - .rodata
  25. int k; // my_bss.2
  26. extern int zoo(int *x, int *y);
  27. int goo(void) { // my_text.2
  28. static int lstat_h; // my_bss.2
  29. return zoo(g, &lstat_h);
  30. }
  31. #pragma clang section rodata="my_rodata.2" data="my_data.2" relro="my_relro.2"
  32. int l = 5; // my_data.2
  33. extern const int m;
  34. const int m = 6; // my_rodata.2
  35. typedef int (*fptr_t)(void);
  36. const fptr_t fptrs[2] = {&foo, &goo};
  37. #pragma clang section rodata="" data="" bss="" text=""
  38. int n; // default
  39. int o = 6; // default
  40. extern const int p;
  41. const int p = 7; // default
  42. int hoo(void) {
  43. return b + fptrs[f]();
  44. }
  45. }
  46. //CHECK: @a = global i32 0, align 4 #0
  47. //CHECK: @b = global i32 1, align 4 #0
  48. //CHECK: @c = global [4 x i32] zeroinitializer, align 4 #0
  49. //CHECK: @d = global [5 x i16] zeroinitializer, align 2 #0
  50. //CHECK: @e = global [6 x i16] [i16 0, i16 0, i16 1, i16 0, i16 0, i16 0], align 2 #0
  51. //CHECK: @f = constant i32 2, align 4 #0
  52. //CHECK: @h = global i32 0, align 4 #1
  53. //CHECK: @i = global i32 0, align 4 #2
  54. //CHECK: @j = constant i32 4, align 4 #2
  55. //CHECK: @k = global i32 0, align 4 #2
  56. //CHECK: @_ZZ3gooE7lstat_h = internal global i32 0, align 4 #2
  57. //CHECK: @_ZL1g = internal global [2 x i32] zeroinitializer, align 4 #0
  58. //CHECK: @l = global i32 5, align 4 #3
  59. //CHECK: @m = constant i32 6, align 4 #3
  60. //CHECK: @n = global i32 0, align 4
  61. //CHECK: @o = global i32 6, align 4
  62. //CHECK: @p = constant i32 7, align 4
  63. //CHECK: @_ZL5fptrs = internal constant [2 x i32 ()*] [i32 ()* @foo, i32 ()* @goo], align 4 #3
  64. //CHECK: define i32 @foo() #5 {
  65. //CHECK: define i32 @goo() #6 {
  66. //CHECK: declare i32 @zoo(i32*, i32*) #7
  67. //CHECK: define i32 @hoo() #8 {
  68. //CHECK: attributes #0 = { "bss-section"="my_bss.1" "data-section"="my_data.1" "rodata-section"="my_rodata.1" }
  69. //CHECK: attributes #1 = { "data-section"="my_data.1" "rodata-section"="my_rodata.1" }
  70. //CHECK: attributes #2 = { "bss-section"="my_bss.2" "rodata-section"="my_rodata.1" }
  71. //CHECK: attributes #3 = { "bss-section"="my_bss.2" "data-section"="my_data.2" "relro-section"="my_relro.2" "rodata-section"="my_rodata.2" }
  72. //CHECK: attributes #4 = { "relro-section"="my_relro.2" }
  73. //CHECK: attributes #5 = { {{.*"implicit-section-name"="my_text.1".*}} }
  74. //CHECK: attributes #6 = { {{.*"implicit-section-name"="my_text.2".*}} }
  75. //CHECK-NOT: attributes #7 = { {{.*"implicit-section-name".*}} }
  76. //CHECK-NOT: attributes #8 = { {{.*"implicit-section-name".*}} }