| 1 | //===-- Loader test to test init and fini array iteration -----------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "test/IntegrationTest/test.h" |
| 10 | |
| 11 | #include <stddef.h> |
| 12 | |
| 13 | int global_destroyed = false; |
| 14 | |
| 15 | class A { |
| 16 | private: |
| 17 | int val[1024]; |
| 18 | |
| 19 | public: |
| 20 | A(int i, int a) { |
| 21 | for (int k = 0; k < 1024; ++k) |
| 22 | val[k] = 0; |
| 23 | val[i] = a; |
| 24 | } |
| 25 | |
| 26 | ~A() { global_destroyed = true; } |
| 27 | |
| 28 | int get(int i) const { return val[i]; } |
| 29 | }; |
| 30 | |
| 31 | int GLOBAL_INDEX = 512; |
| 32 | int INITVAL_INITIALIZER = 0x600D; |
| 33 | |
| 34 | A global(GLOBAL_INDEX, INITVAL_INITIALIZER); |
| 35 | |
| 36 | int initval = 0; |
| 37 | int preinitval = 0; |
| 38 | |
| 39 | __attribute__((constructor)) void set_initval() { |
| 40 | initval = INITVAL_INITIALIZER; |
| 41 | } |
| 42 | __attribute__((destructor(1))) void reset_initval() { |
| 43 | ASSERT_TRUE(global_destroyed); |
| 44 | ASSERT_EQ(preinitval, 0); |
| 45 | initval = 0; |
| 46 | } |
| 47 | |
| 48 | void set_preinitval() { preinitval = INITVAL_INITIALIZER; } |
| 49 | __attribute__((destructor(2))) void reset_preinitval() { |
| 50 | ASSERT_TRUE(global_destroyed); |
| 51 | ASSERT_EQ(initval, INITVAL_INITIALIZER); |
| 52 | preinitval = 0; |
| 53 | } |
| 54 | |
| 55 | using PreInitFunc = void(); |
| 56 | __attribute__((section(".preinit_array"))) PreInitFunc *preinit_func_ptr = |
| 57 | &set_preinitval; |
| 58 | |
| 59 | TEST_MAIN() { |
| 60 | ASSERT_EQ(global.get(i: GLOBAL_INDEX), INITVAL_INITIALIZER); |
| 61 | ASSERT_EQ(initval, INITVAL_INITIALIZER); |
| 62 | ASSERT_EQ(preinitval, INITVAL_INITIALIZER); |
| 63 | return 0; |
| 64 | } |
| 65 |
