Sections in C

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
pini

Sections in C

Post by pini »

How can I do to put all my C global variables in the data section ?
It seems that they're currently in the same section as my code.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Sections in C

Post by Pype.Clicker »

global variables should go into the .data section if they are initialized and in .bss (not in file) section -- that should be zeroed by the loader ...

In no way they're supposed to go in the .text section except if you stated it explicitly in your linking script (if any). However, constant string usually go to .text in formats where no .rodata section is available.
pini

Re:Sections in C

Post by pini »

Here's my linker script.
I will try to re-make my question : Can I control how the different sections (text, data, rodata) are organized in the binary file ?

[attachment deleted by admin]
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Sections in C

Post by Pype.Clicker »

you can do this by modifying the order of the sections in your script and by forcing the output virtual address of the sections. For instance

Code: Select all

. = SIZEOF_HEADERS;
  .text 0: {
        *(.text)
        . = ALIGN(0x10);
  }
  .data (ADDR(.text) + SIZEOF(.text)) : {
        *(.rodata)
        *(.data)
        . = ALIGN(0x10);
  }
will make sure that no more than 16 bytes of padding is used between section, and that the 'data' section starts just after 'text'.

As you saw, i also grouped the read-only and read-write sections together (for compatibility with my COFF/ELF->SFS tools, mainly)

note that '.' refers to the output (file-offset), not the virtual address, afaik ... So .=0x100000 is likely to skip 1Mb in your output file (though i might be wrong on this :)
Post Reply