Put static const member of template class in specific section in GCC

2

For a custom build step I need to put some data into a specific section. On GCC and clang this can be done with

__attribute__((section(".my_section"))) const int variable_in_this_section = 42;

To make the creation easier and less error prone I created a template class:

#include <cstdint>

#ifdef _MSC_VER
    #define SECTION(x) __declspec(section x)
#else
    #define SECTION(x) __attribute__((section(x)))
#endif

#define DATA_SECTION SECTION(".my_data")

// the type to store in the .my_data section
struct hook_table_entry_t
{
    std::uint32_t game_addr, hook_addr;
};

template<std::uint32_t address, std::uint32_t function>
struct bind_hook_t
{
    DATA_SECTION static const hook_table_entry_t s_entry;
};
template<std::uint32_t address, std::uint32_t function>
const hook_table_entry_t bind_hook_t<address, function>::s_entry {address, function};

// instantiate template to create value
template struct bind_hook_t<0xffffffff, 0xf0f0f0f0>;

When I compiled this with clang 3.8 my object file looked like this:

Section .my_data:
0000 ffffffff f0f0f0f0                    ........        

Perfect! But when testing it in GCC 5 & 7 I got this:

Section .rodata._ZN11bind_hook_tILj4294967295ELj4042322160EE7s_entryE:
0000 ffffffff f0f0f0f0                    ........        

Is there a way to make GCC put the data in .my_data as well? Note: it works when bind_hook_t is not a template, but that defeats its whole purpose.

c++
gcc
clang
sections
asked on Stack Overflow Nov 15, 2018 by HenrikS

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0