How to get a pointer to a binary section in MSVC?

后端 未结 4 1775
说谎
说谎 2021-01-02 03:38

I\'m writing some code which stores some data structures in a special named binary section. These are all instances of the same struct which are scattered across many C file

4条回答
  •  盖世英雄少女心
    2021-01-02 04:00

    There is also a way to do this with out using an assembly file.

    #pragma section(".init$a")
    #pragma section(".init$u")
    #pragma section(".init$z")
    
    __declspec(allocate(".init$a")) int InitSectionStart = 0;
    __declspec(allocate(".init$z")) int InitSectionEnd   = 0;
    
    __declspec(allocate(".init$u")) int token1 = 0xdeadbeef;
    __declspec(allocate(".init$u")) int token2 = 0xdeadc0de;
    

    The first 3 line defines the segments. These define the sections and take the place of the assembly file. Unlike the data_seg pragma, the section pragma only create the section. The __declspec(allocate()) lines tell the compiler to put the item in that segment.

    From the microsoft page: The order here is important. Section names must be 8 characters or less. The sections with the same name before the $ are merged into one section. The order that they are merged is determined by sorting the characters after the $.

    Another important point to remember are sections are 0 padded to 256 bytes. The START and END pointers will NOT be directly before and after as you would expect.

    If you setup your table to be pointers to functions or other none NULL values, it should be easy to skip NULL entries before and after the table, due to the section padding

    See this msdn page for more details

提交回复
热议问题