提问人:super_jh 提问时间:11/16/2023 最后编辑:John Kugelmansuper_jh 更新时间:11/16/2023 访问量:51
使用 lazy_static!符号表中定义变量的大小为 0
Using lazy_static! the size of the defined variable in the symbol table is 0
问:
下面的 rust 代码,在 x86_64 平台编译中。
#[macro_use]
extern crate lazy_static;
use std::collections::HashMap;
lazy_static! {
static ref HASHMAP: HashMap<u32, &'static str> = {
let mut m = HashMap::new();
m.insert(0, "foo");
m.insert(1, "bar");
m.insert(2, "baz");
m
};
}
fn main() {
// First access to `HASHMAP` initializes it
println!("The entry for `0` is \"{}\".", HASHMAP.get(&0).unwrap());
// Any further access to `HASHMAP` just returns the computed value
println!("The entry for `1` is \"{}\".", HASHMAP.get(&1).unwrap());
}
我使用 readelf 命令查看符号表中 HASHMAP 变量的大小:
readelf -sW target/debug/deps/section_test-4d7d6a03c56fdde3.o
Symbol table '.symtab' contains 590 entries:
Num: Value Size Type Bind Vis Ndx Name
452: 0000000000000000 0 OBJECT LOCAL DEFAULT 498 _ZN12section_test7HASHMAP17hbc6de818c577d166E
我们可以看到 The Size 是 0。
readelf -SW target/debug/deps/section_test-4d7d6a03c56fdde3.o
There are 527 section headers, starting at offset 0x98690:
Section Headers:
[Nr] Name Type Address Off Size ES Flg Lk Inf Al
[498] .rodata._ZN12section_test7HASHMAP17hbc6de818c577d166E PROGBITS 0000000000000000 00b528 000000 00 A 0 0 1
我们来看一下符号所在的部分,该符号位于 rodata 部分并且该部分的大小也是 0。
既然长度为0,是否意味着无法存储任何数据,运行时HashMap内存分配在哪里? 我可以用 C 语言在符号表中定义长度为 0 的变量吗?
答:
评论