0

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; j += sizeof(func_type)) {
    auto func = *(func_type*)(func_reg_start + i);
    func(); // Expecting execution of test() / test2() at B.cpp / C.cpp
}

B.cpp

static bool test() {
    std::cout << "test" << std::endl;
    return true;
}
ADD_FUNC(aaa, test) // Expecting test() pointer places at ".init_reg.aaa.test"

C.cpp

static bool test2() {
    std::cout << "test2" << std::endl;
    return true;
}
ADD_FUNC(aaa, test2) // Expecting test() pointer places at ".init_reg.aaa.test2"

I have this implementation using linker script, and I attach the A.ld with the CMAKE option below.

target_link_options(${BIN_TARGET} PRIVATE -Wl,-T${CMAKE_CURRENT_SOURCE_DIR}/A.ld)

It works well with g++ linker but it doesn't work with lld and mold. It doesn't give me a build failure but func_size at A.cpp is always 0 so it ends up with executing nothing. Also, I see nothing at that section from my map output file. How should I fix it to make it work?

Thanks for reading!

JSK
  • 45
  • 5
  • I found the answers of my question by myself. mold seems not to have a plan to support a custom linker descriptor. – JSK Jul 31 '23 at 02:25
  • And lld works properly but clang opitmized out the variable defined in a macro since it looks like the variable is not used anywhere. So adding "used" to attribute makes it work. – JSK Jul 31 '23 at 02:27

0 Answers0