|
|
|
|
| 为什么elf 文件section header table 中bss段的大小总为0? |
 为什么elf 文件section header table 中bss段的大小总为0? - charlyisme [ 2005-08-31 10:13 | 1,430 byte(s)]
 Re: 为什么elf 文件section header table 中bss段的大小总为0? - lonelyflyer [ 2005-08-31 18:55 | 172 byte(s)]
 Re: 为什么elf 文件section header table 中bss段的大小总为0? - charlyisme [ 2005-09-01 09:35 | 513 byte(s)]
 Re: 为什么elf 文件section header table 中bss段的大小总为0? - zhllg [ 2005-08-31 13:03 | 1,506 byte(s)]
 Re: 为什么elf 文件section header table 中bss段的大小总为0? - charlyisme [ 2005-09-01 09:36 | 44 byte(s)]
 Re: 为什么elf 文件section header table 中bss段的大小总为0? - zhllg [ 2005-09-01 11:18 | 140 byte(s)]
 改了一下程序 - charlyisme [ 2005-09-02 09:33 | 1,263 byte(s)]
 Re: 改了一下程序 - zhllg [ 2005-09-02 09:59 | 578 byte(s)]
 谢谢zhllg对本问题的关注 - charlyisme [ 2005-09-02 09:27 | 1,253 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
为什么elf 文件section header table 中bss段的大小总为0?
t.c
char abss[12];
int main(void){
return 0;
}
这里abss应该是放在bss段吧,也就是生成elf的section header table中bss
段的大小应该是12吧,否则startup时初始化bss段为0怎么知道bss段大小,
怎么知道应该填多少0?
gcc -c t.c -o t
objdump -x t
Sections:
Idx Name Size VMA LMA File off Algn
0 .text 00000017 00000000 00000000 00000034 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
1 .data 00000000 00000000 00000000 0000004c 2**2
CONTENTS, ALLOC, LOAD, DATA
2 .bss 00000000 00000000 00000000 0000004c 2**2
ALLOC
3 .comment 00000033 00000000 00000000 0000004c 2**0
CONTENTS, READONLY
SYMBOL TABLE:
00000000 l df *ABS* 00000000 t.c
00000000 l d .text 00000000
00000000 l d .data 00000000
00000000 l d .bss 00000000
00000000 l d .comment 00000000
00000000 g F .text 00000017 main
0000000c O *COM* 00000001 abss
如上.bss section size为0,另外abss的*COM*是什么意思呢?
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
每一个section header table entry中的第6个字就是该section的size
typedef struct {
uint32_t sh_name;
uint32_t sh_type;
uint32_t sh_flags;
Elf32_Addr sh_addr;
Elf32_Off sh_offset;
uint32_t sh_size;
uint32_t sh_link;
uint32_t sh_info;
uint32_t sh_addralign;
uint32_t sh_entsize;
} Elf32_Shdr;
一个汇编程序:
.section .bss
.lcomm BUF, 4
.section .text
.globl _start
_start:
movl $10, %eax
movl $2, BUF
xorl %edx, %edx
idivl BUF
movl %eax, %ebx
movl $1, %eax
int $0x80
编译:
gcc test.s -nostdlib
查看:
readelf -S a.out
There are 7 section headers, starting at offset 0xe0:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
...
[ 3] .bss NOBITS 080490b4 0000b4 000004 00 WA 0 0 4
...
|
|
|
----
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
|
ELF没研究过,不过,没有初始化的全局数组,应该不会在数据段里分配同样大小,不然,你定义一个十兆的数组,程序会大十兆吗?当然不会。
|
|
----
OpenH323 is buggy ACE is memory greedy
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
谢谢lonelyflyer您的回答,但您好像没有理解我的意思
“ELF没研究过,不过,没有初始化的全局数组,应该不会在数据段里分配同样大小,不然,你定义一个十兆的数组,程序会大十兆吗?当然不会。 ”
BSS当然不会在数据段中分配对应的大小,因为都会被全置为0,没有必要分配,loader在装入elf文件时后,_startup会将bss段全清零。
但是section header table 中肯定会记录bss段的大小,注意我说的是section header table只是记录大小,并不会实际分配。如果不记录,那么清零时怎么断定有多大呢?
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
[root@localhost test]# readelf -S t
There are 8 section headers, starting at offset 0xb4:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .text PROGBITS 00000000 000034 000017 00 AX 0 0 4
[ 2] .data PROGBITS 00000000 00004c 000000 00 WA 0 0 4
[ 3] .bss NOBITS 00000000 00004c 000000 00 WA 0 0 4
[ 4] .comment PROGBITS 00000000 00004c 000033 00 0 0 1
[ 5] .shstrtab STRTAB 00000000 00007f 000035 00 0 0 1
[ 6] .symtab SYMTAB 00000000 0001f4 000080 10 7 6 4
[ 7] .strtab STRTAB 00000000 000274 00000f 00 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings)
I (info), L (link order), G (group), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
结果还是bss在Section Headers中记录的大小为0
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
按照zhllg的建议,使用一下abss这个变量
char abss[12];
int main(void){
abss[1]=56;
return (int)abss[1];
}
结果还是一样
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .text PROGBITS 00000000 000034 000020 00 AX 0 0 4
[ 2] .rel.text REL 00000000 0002b8 000010 08 7 1 4
[ 3] .data PROGBITS 00000000 000054 000000 00 WA 0 0 4
[ 4] .bss NOBITS 00000000 000054 000000 00 WA 0 0 4
[ 5] .comment PROGBITS 00000000 000054 000033 00 0 0 1
[ 6] .shstrtab STRTAB 00000000 000087 000039 00 0 0 1
[ 7] .symtab SYMTAB 00000000 000228 000080 10 8 6 4
[ 8] .strtab STRTAB 00000000 0002a8 00000f 00 0 0 1
Key to Flags:
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
没看清楚
gcc -c 编译出来的是object file
gcc -c t.c自动生成t.o
object file的命名规则一般就是把文件后缀名的c改成o
经过连接生成的可执行文件里的section header table entry里才会记录bss段的大小
object file里记录bss段大小的是symble table entry
readelf -s可以查看
objdump -x的结果里也有,不过标志不明确。你的结果里abss那一行第一个0000000c就是它的大小
*COM*是special section header table index, 值为0xfff2,就是用来标志c里面的未初始化全局变量的,还有fortran里COMMON区域
elf specification里这些都有
|
|
|
----
|
|
[Original]
[Print]
[Top]
|
|
|