静态库中不会发生静态对象数组的动态初始化

发布于 2025-01-16 20:29:54 字数 1664 浏览 2 评论 0原文

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

宏 DEFINE_UINT_PARAM 的定义:

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

ConfigMap 的定义:

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

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

问题是,如果我在没有 -fPIC 的情况下编译 IniReader.cpp 并生成静态库 libdramsim.a。并使用libdramsim.a,我发现configMap尚未动态初始化。都是 0。但是,如果我使用 -fPIC 编译 IniReader.c 并生成 libdramsim.so 并使用 libdramsim.so,则 configMap 已正确动态初始化。为什么?有什么区别?初始化有什么问题? 使用readelf,我发现configMap位于目标文件IniReader.o.bss部分。所以我认为它需要动态初始化阶段。 如果我遗漏了什么,请随时告诉我。

顺便问一下,我怎么知道configMap没有初始化呢?使用 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
}
// 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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文