bss section not written?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
bradbobak
Member
Member
Posts: 26
Joined: Fri Apr 14, 2006 11:00 pm

bss section not written?

Post 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.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post 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.
artrecks
Posts: 23
Joined: Wed Jul 11, 2007 8:24 pm

Post 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?
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: bss section not written?

Post 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.
bradbobak
Member
Member
Posts: 26
Joined: Fri Apr 14, 2006 11:00 pm

Post 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?
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post 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.
Post Reply