A.ld
SECTIONS
{
.rel.rodata.func_reg : {
PROVIDE(func_reg_start = .);
*(.func_reg.aaa.*)
PROVIDE(func_reg_end = .);
...
}
}
INSERT AFTER .text;
A.hpp
typedef bool (*func_type)();
#define ADD_FUNC(idx, func) \
static func_type i_##idx##func __attribute__((section(".func_reg." #idx "." #func))) = (func);
A.cpp
extern uint64_t func_reg_start[];
extern uint64_t func_reg_end[];
uint64_t func_size = func_reg_end - func_reg_start;
for (uint64_t i = 0; i < func_size; i += sizeof(func_type)) {
auto func = *(func_type*)(func_reg_start + i);
func(); // Expecting execution of test1 / test2 functions at B.cpp / C.cpp
}
B.cpp
static bool test1() {
std::cout << "test1" << std::endl;
return true;
}
ADD_FUNC(aaa, test1) // Expecting test1() pointer places at ".func_reg.aaa.test1"
C.cpp
static bool test2() {
std::cout << "test2" << std::endl;
return true;
}
ADD_FUNC(aaa, test2) // Expecting test2() pointer places at ".func_reg.aaa.test2"
This implementation works well with g++ but it doesn’t work with clang-15.
It doesn’t give me a build failure using clang but func_size at A.cpp is always 0 and I don’t see pointers at that section.
First I thought it can be a lld problem, but I see “.rel.rodata.func_reg” section is added after text and func_reg_start, func_reg_end symbol is defined from my map file.
Now I am not sure attribute((section(“name”))) works as I expected in llvm…
How should I fix it to make it work?
Thanks for reading, and any advise is welcome!