静态库中不会发生静态对象数组的动态初始化
// 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论