Page 1 of 1

bss section not written?

Posted: Sun Jul 22, 2007 9:38 am
by bradbobak
Hi, I'm just wondering if I can force the linker (or objcopy) to write the bss section to my output file.. I use the file size to allocate memory to load the program.. It doesn't seem to be writing the bss variables, and this is causing me to have a page fault..
Could this be a problem in my .ld file?

Just to give some more info, I use:

ld -melf_386 --script=program.ld -o program.out program.o other_stuff.o

then

objcopy -v -S -O binary program.out program.bin

now, 'nm program.out' shows a '00000202004 b y_pos', which is 4 bytes over what is linked (thus causing my page fault)

Any help would be appreciated.

Posted: Sun Jul 22, 2007 10:18 am
by frank
Well the easiest way to do it would be like so. This is a excerpt from a linker script. This is assuming that the bss section is the last section in the file.

Code: Select all

.bss
{
    *(.bss)
    LONG(0)
}
The LONG(0) forces the bss section to be written to the file.

Posted: Sun Jul 22, 2007 3:08 pm
by artrecks
...
my kernel has the BSS section, but it's not being write to the file, so, look the code above

Code: Select all


long int vars[2048];

I guess they go to the BSS section, my kernel is pure binary. So, doing this

Code: Select all


int i;

for (i = 0; i < 2048; i++) {

  vars[i] = 0;

}

I'm initializing this variables, but, if they don't go to the file, could I be writing other memory than the memory the variables are?

Re: bss section not written?

Posted: Sun Jul 22, 2007 4:17 pm
by pcmattman
bradbobak wrote:Hi, I'm just wondering if I can force the linker (or objcopy) to write the bss section to my output file.. I use the file size to allocate memory to load the program.. It doesn't seem to be writing the bss variables, and this is causing me to have a page fault..
If you wrote an ELF loader (this only takes a small amount of code) then you would be able to have more control over the whole load process - and you wouldn't need to do the 'objcopy'.

Otherwise, it's next to impossible to find out how much BSS space is needed for a binary unless you write a special header that all programs for your OS require. Then your loader just needs to parse this header.

Posted: Sun Jul 22, 2007 5:50 pm
by bradbobak
ok, thanks.. i'm going to make an elf loader.. i just have one more question.. bss is supposed to be 'unitialized data'.. but i'm doing

Code: Select all

static uint x_pos = 0;
and this is being put into bss.. should i clear the bss section to 0 when i start?

Posted: Sun Jul 22, 2007 7:22 pm
by pcmattman
bradbobak wrote:ok, thanks.. i'm going to make an elf loader.. i just have one more question.. bss is supposed to be 'unitialized data'.. but i'm doing

Code: Select all

static uint x_pos = 0;
and this is being put into bss.. should i clear the bss section to 0 when i start?
The reason why it's put into BSS is because BSS is always initialized to 0, so you can get a performance boost (and strip size) by not putting multiple references to '0' in the data section of the program.

GCC/ld automatically does this for you.