Page 1 of 1

linking problem

Posted: Mon Aug 06, 2007 2:34 pm
by sancho1980
hi

i have yet again a problem linking somthing. suppose i have the following object file:

Code: Select all

main.o:     file format elf32-i386

Disassembly of section .data:

00000000 <.data>:
   0:	ff                   	(bad)  
   1:	ff                   	(bad)  
   2:	ff                   	(bad)  
   3:	ff                   	.byte 0xff
Disassembly of section .text:

00000000 <.text>:
   0:	06                   	push   %es
   1:	00 00                	add    %al,(%eax)
   3:	00 06                	add    %al,(%esi)
   5:	00 00                	add    %al,(%eax)
   7:	00 48 65             	add    %cl,0x65(%eax)
   a:	6c                   	insb   (%dx),%es:(%edi)
   b:	6c                   	insb   (%dx),%es:(%edi)
   c:	6f                   	outsl  %ds:(%esi),(%dx)
   d:	2e 00 00             	add    %al,%cs:(%eax)
Disassembly of section .text:

00000000 <start>:
   0:	55                   	push   %ebp
   1:	89 e5                	mov    %esp,%ebp
   3:	68 08 00 00 00       	push   $0x8
   8:	e8 fc ff ff ff       	call   9 <start+0x9>
   d:	89 ec                	mov    %ebp,%esp
   f:	5d                   	pop    %ebp
  10:	c3                   	ret    
I link this file together with two other object files:

Code: Select all

ld main.o video.o utilities.o -e start -N -o boot.bin --oformat binary -T script
The linker script looks is as follows:

Code: Select all

SECTIONS
{
  . = 0x0;
  .text : { *(.text) }
  .data : { *(.data) }
  .bss : { *(.bss) }
}
When I disassemble the linked result, I get this:

Code: Select all

00000000  06                push es
00000001  0000              add [eax],al
00000003  0006              add [esi],al
00000005  0000              add [eax],al
00000007  004865            add [eax+0x65],cl
0000000A  6C                insb
0000000B  6C                insb
0000000C  6F                outsd
0000000D  2E0000            add [cs:eax],al
00000010  55                push ebp
00000011  89E5              mov ebp,esp

...and so on

You can see clearly here that what is placed right at the start of the linked result is the code that comes in section .text of the object file just before the function "start" EVEN THOUGH I have specified in my linker command that "start" be the entry point. The problem is that this part of section .text is not only NOT supposed to be the start of my code, but that it is really READONLY data placed into the .text section by the compiler for some reason. Can I do anything about this? If yes, how can I change it?

Thanks

Martin

Posted: Tue Aug 07, 2007 3:38 am
by os64dev
specifying the entry symbol does not mean it will be the first symbol in the file. it just specifies what symbol contains the first byte to execute. If it really is readonly data then you also need to specify rodata or rdata in your linker script.

I have a slightly different approach with the bootloader by making a section boot containing the start function and adding the boot section before the text.

Posted: Tue Aug 07, 2007 4:04 am
by sancho1980
Sounds like a plan. Now, because becasue I'm not very familiar with the possibilities I have with ld, I dont know how to do that..Could you show me how I'd have to change my linker script to achieve what you're suggesting?

Thanks

Martin

Posted: Tue Aug 07, 2007 11:47 am
by os64dev

Code: Select all

SECTIONS 
{ 
  . = 0x0; 
  .boot : { *(.boot) }
  .text : { *(.text*) } 
  .data : { *(.data*) } 
  .bss : { *(.bss*) } 
}
and in GNU asm

Code: Select all

SECTION .boot
.globl start
start:
  ret;