# Outputting arbitrary data in an LD linker script

This topic is 3407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've written an LD linker script suitable for producing binaries for a MIPS based CPU architecture I'm working on however I'm having a problem. You are expected to initialise the .bss section with 0s when you start up a program. I'd like to avoid doing this. The simplest way would be for me to force LD to output a bunch of 0s for the .bss section into the output binary (Output format is raw binary). However as far as I can tell there's no command that can be used in a linker script to output arbitrary data, there are the BYTE, SHORT, LONG and QUAD commands but they only output fixed amounts and I don't think I can construct a loop. Anyone got any ideas? While writing this post I just had an idea which seems to work, have the .bss section before the .data section, though it'd still be nice to know if there is a cunning way to convince LD to output arbitrary data into a given section.

##### Share on other sites
It's been a while, but doesn't this work?

SECTIONS{	.bss:	{		* (.bss)	} = 0x0000}

IIRC you can also initial parts within a section individually, e.g. foo.o (.bss) = 0xABCD (max 2 byte pattern I think).

##### Share on other sites
It doesn't seem to. That simply sets the fill pattern of the section which is what is used for padding (e.g. filling gaps during alignment) in various places. The .bss section doesn't have CONTENTS and LOAD attributes set in the .elf object files and as such the linker doesn't output anything for them.

I think the trick of putting .bss before the .data section works because in outputting a raw binary file it needs to put everything in right place, so when there's a gap between sections it fills it up (so it doesn't have any contents for .bss, however there's a gap between .text and .data so it fills it with 0s).

##### Share on other sites
Umm, but the .bss section isn't something the linker sees, because it's a BSS section. It doesn't exist until load time. ld can't write the code to initialize a BSS at load time for you. You might be able to tweak your loader to automatically initialize the BSS, you can have your own init code in an .init segment, or you can just leave the memory uninitialized.

##### Share on other sites
Linkers do see .bss sections. They're just marked as ALLOC and ALLOC only in the object file (Well they don't have to be, but the ones gcc emits are), they're got an LMA, VMA and length etc just like any other segment just no data. So the program loader knows where to allocate a chunk of memory for them.

I can quite happily get the address and size of the final .bss segment in the linker script (and indeed it is necessary to get them so you can define symbols that can be used by the initialisation code to know where the .bss is so it can initialise it). All I need is way to emit an arbitrary number of 0s of a given size.

1. 1
2. 2
3. 3
4. 4
frob
12
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632172
• Total Posts
3004562

×