dynamic initialization for a static object array does not happen in the static library

Viewed 20
// IniReader.cpp
static ConfigMap configMap[] =
{
    DEFINE_UINT_PARAM(NUM_BANKS,DEV_PARAM),
    DEFINE_UINT_PARAM(NUM_ROWS,DEV_PARAM),
  // ...
}

define of macro DEFINE_UINT_PARAM:

#define DEFINE_UINT_PARAM(name, paramtype) {#name, &name, UINT, paramtype, false}

define of ConfigMap:

typedef struct _configMap
{
    string iniKey; //for example "tRCD"

    void *variablePtr;
    varType variableType; // enum
    paramType parameterType; // enum
    bool wasSet;
} ConfigMap;

The problem is that, if I compile IniReader.cpp without -fPIC and generate a static library libdramsim.a. and use libdramsim.a, I find that configMap has not been dynamically initialized. It is all 0s. However, if I compile IniReader.c with -fPIC and generate libdramsim.so and use libdramsim.so, configMap has been dynamically initialized correctly. Why? What is the difference? What's problem with initialization? With readelf, I find that, configMap is in .bss section in the object file IniReader.o. So I think it needs dynamic initialization phase. Please feel free to tell me if I omit something.

By the way, how do I know that configMap is not initialized? With gdb: libdramsim.a

Breakpoint 1, DRAMSim::IniReader::SetKey (
    key=<error reading variable: Cannot access memory at address 0x0>, 
    valueString=<error reading variable: Cannot access memory at address 0x0>, isSystemParam=false, 
    lineNumber=0) at IniReader.cpp:266
266 {
(gdb) n
272     for (i=0; configMap[i].variablePtr != NULL; i++)
(gdb) p configMap[0]
$1 = {
  iniKey = "",
  variablePtr = 0x0,
  variableType = DRAMSim::STRING, // 0
  parameterType = DRAMSim::SYS_PARAM, // 0
  wasSet = false
}
0 Answers
Related