Page 1 of 1

Change section order in executable (using gas & ld)

Posted: Fri Jul 16, 2010 5:39 am
by hrniels
Hi all!

I'm currently trying to move everything to the GNU toolchain (crosscompiler). One step is to convert the assembler files from nasm- to gas-syntax. But this causes trouble because gas orders the sections in a different way than nasm.

Some background:
I have a higherhalf-kernel. Therefore I create a section ".setup" which will be called from the bootloader (GRUB in my case) at the beginning, sets up the GDT and makes a far-jump to my higherhalf-kernel. When I use nasm the resulting sections of the file containing just the section ".setup" are (readelf -S header.o):

Code: Select all

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .setup            PROGBITS        00000000 000130 00003a 00   A  0   0  1
  [ 2] .shstrtab         STRTAB          00000000 000170 00002d 00      0   0  1
  [ 3] .symtab           SYMTAB          00000000 0001a0 000090 10      4   6  4
  [ 4] .strtab           STRTAB          00000000 000230 000051 00      0   0  1
  [ 5] .rel.setup        REL             00000000 000290 000018 08      3   1  4
So, the .setup section is the first one (and .text isn't even there).
When I use gas I get the following:

Code: Select all

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 000000 00  AX  0   0  4
  [ 2] .data             PROGBITS        00000000 000034 000000 00  WA  0   0  4
  [ 3] .bss              NOBITS          00000000 000034 000000 00  WA  0   0  4
  [ 4] .setup            PROGBITS        00000000 000034 00003a 00      0   0  1
...
If I understand it correctly, that leads to the following:
- ld puts .setup at the end of the file (I really mean the offset in the file here; not the virtual address)
- that causes GRUB to tell me that he can't load my kernel ("Selected item cannot fit into memory").
At least thats the only difference I can see. The sections in the kernel, when using gas, are:

Code: Select all

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .setup            PROGBITS        00100000 02ddb8 00003a 00      0   0  1
  [ 2] .text             PROGBITS        c0100040 001040 020990 00  AX  0   0 16
  [ 3] .eh_frame         PROGBITS        c01209d0 0219d0 000078 00   A  0   0  4
  [ 4] .iplt             PROGBITS        c0120a48 021a48 000000 00  AX  0   0  4
  [ 5] .rel.dyn          REL             c0120a48 021a48 000000 08   A  0   0  4
  [ 6] .data             PROGBITS        c0121000 022000 00bdb8 00  WA  0   0 32
  [ 7] .igot.plt         PROGBITS        c012cdb8 02ddb8 000000 00  WA  0   0  4
  [ 8] .bss              NOBITS          c012d000 02ddb8 1f1ae0 00  WA  0   0 4096
...
As you can see, .setup is @ 0x2ddb8 in the file and .text @ 0x1040. That seems to be the problem.
When I use nasm .setup is @ 0x1000, i.e. before .text.

So my question is: Am I doing something wrong? Or is there a way to tell gas that it should order the sections like I do it in my assembler-file? Or can I specify that in the linker-script somehow?
Please let me know if you need more information (I don't want to spam you with the linker-script, the different assembly files and so on :)).

Thanks!
hrniels

Re: Change section order in executable (using gas & ld)

Posted: Fri Jul 16, 2010 6:53 am
by -m32
You can position it using the linker script.

Re: Change section order in executable (using gas & ld)

Posted: Fri Jul 16, 2010 7:13 am
by hrniels
Ok, but how? I've already read all IMO possibly helpful parts of the ld-documentation, but found nothing.

Edit: Thats my linkerscript:

Code: Select all

OUTPUT_FORMAT("elf32-i386")
ENTRY(loader)

SECTIONS
{
	. = 0x100000;

	.setup :
	{
		*(.setup)
	}

	. += 0xC0000000;
	
	.text : AT(ADDR(.text) - 0xC0000000)
	{
		*(.text)
	}

	.data ALIGN (4096) : AT(ADDR(.data) - 0xC0000000)
	{
		*(.data)
		*(.rodata*)
	}

	.bss ALIGN (4096) : AT(ADDR(.bss) - 0xC0000000)
	{
		*(COMMON*)
		*(.bss*)
	}
}

Re: Change section order in executable (using gas & ld)

Posted: Fri Jul 16, 2010 7:42 am
by gerryg400
Make sure that the file that contains .setup is first in the list of object files. That might do it. There isn't an ld option to set the section order in an output file.

Re: Change section order in executable (using gas & ld)

Posted: Fri Jul 16, 2010 7:57 am
by hrniels
gerryg400 wrote:Make sure that the file that contains .setup is first in the list of object files. That might do it. There isn't an ld option to set the section order in an output file.
It is the first one in the list. As I said, it works when I use nasm. That means the only things to change to make it not working anymore are:
- Change asm-file-content (with .setup-section) in nasm-syntax to that with gas-syntax
- run gas instead of nasm for that file
Thats it. Everything else is the same.

Re: Change section order in executable (using gas & ld)

Posted: Fri Jul 16, 2010 8:03 am
by gerryg400
I see now. Maybe you could try the ld option GROUP to force the .setup and .text together

Code: Select all

   SECTIONS
   {
           GROUP                  :
           {
                   .setup      : { }
                   .text        : { }
           }
   }
Haven't tried this, just guessing which is only sometimes wise.

Re: Change section order in executable (using gas & ld)

Posted: Fri Jul 16, 2010 8:30 am
by hrniels
I guess you mean this one: http://sourceware.org/binutils/docs-2.2 ... mands.html
So it seems to be a different thing. At least, I couldn't find an option that could be the one you said.
(I've also tried it. Leads to a syntax error or causes ld to crash ^^)

Re: Change section order in executable (using gas & ld)

Posted: Fri Jul 16, 2010 8:40 am
by gerryg400
No I meant this one. Search for "Grouping sections together". But maybe it's wrong.

Re: Change section order in executable (using gas & ld)

Posted: Fri Jul 16, 2010 8:56 am
by hrniels
gerryg400 wrote:No I meant this one. Search for "Grouping sections together". But maybe it's wrong.
Ah, while reading the stuff there, I had a different idea. Simply call it ".init" instead of ".setup". And it works =)
In the kernel there is no ".init" anyway and the purpose is similar, so I think its okay.
Thanks for the hints!