Search code examples
c++cpragmaportable-executable

Why would one want to create a different code/data/bss section in a PE file?


As someone with a cursory understanding of executable file formats, I found the following #pragma directives in MSDN's documentation very interesting:

What exactly is the advantage of putting certain variables and function bodies in a different .section in the PE/OBJ file than the default sections for such things?


Solution

  • Well, traditionally literal are constant (e.g. static const char* const = "hello world";) in-part because they're located in the const_seg area, if they were located in the data_seg or bss_seg they could be mutable (though not resizable-in-place for obvious reasons).

    Another reason (in the same vein) might be to pre-initialize a complex data structure at compile-time yet allow it to be manipulated at runtime, for example a large hashtable of known-values, though this would require compiler-support.