public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Strange Linking Problem
@ 2005-03-12 14:01 Matthias-Christian Ott
  2005-03-12 14:34 ` linux-os
  0 siblings, 1 reply; 3+ messages in thread
From: Matthias-Christian Ott @ 2005-03-12 14:01 UTC (permalink / raw)
  To: Linux Kernel Mailing List

Hi!
I hope I'm right here. I've the following assembler code:

SECTION .DATA
        hello:     db 'Hello world!',10
        helloLen:  equ $-hello

SECTION .TEXT
        GLOBAL main

main:



        ; Write 'Hello world!' to the screen
        mov eax,4            ; 'write' system call
        mov ebx,1            ; file descriptor 1 = screen
        mov ecx,hello        ; string to write
        mov edx,helloLen     ; length of string to write
        int 80h              ; call the kernel

        ; Terminate program
        mov eax,1            ; 'exit' system call
        mov ebx,0            ; exit with error code 0
        int 80h              ; call the kernel


Then I run:

nasm -f elf hello.asm


I link it with ld and run it:

ld -s -o hello hello.o
./hello
segmentation fault


I link it with the gcc and run it:

gcc hello.o -o hello
./hello
Hello world!


What's wrong with the ld?

Matthias-Christian Ott

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Strange Linking Problem
  2005-03-12 14:01 Strange Linking Problem Matthias-Christian Ott
@ 2005-03-12 14:34 ` linux-os
  2005-03-13  9:33   ` Matthias-Christian Ott
  0 siblings, 1 reply; 3+ messages in thread
From: linux-os @ 2005-03-12 14:34 UTC (permalink / raw)
  To: Matthias-Christian Ott; +Cc: Linux Kernel Mailing List

On Sat, 12 Mar 2005, Matthias-Christian Ott wrote:

> Hi!
> I hope I'm right here. I've the following assembler code:
>
> SECTION .DATA
>       hello:     db 'Hello world!',10
>       helloLen:  equ $-hello
>
> SECTION .TEXT
>       GLOBAL main
>
> main:
>
>
>
>       ; Write 'Hello world!' to the screen
>       mov eax,4            ; 'write' system call
>       mov ebx,1            ; file descriptor 1 = screen
>       mov ecx,hello        ; string to write
>       mov edx,helloLen     ; length of string to write
>       int 80h              ; call the kernel
>
>       ; Terminate program
>       mov eax,1            ; 'exit' system call
>       mov ebx,0            ; exit with error code 0
>       int 80h              ; call the kernel
>
>
> Then I run:
>
> nasm -f elf hello.asm
>
>
> I link it with ld and run it:
>
> ld -s -o hello hello.o
> ./hello
> segmentation fault
>
>
> I link it with the gcc and run it:
>
> gcc hello.o -o hello
> ./hello
> Hello world!
>
>
> What's wrong with the ld?
>

Nothing at all. Where is _start: ?

Remove the 'main' label and substitute _start:

It is 'C' convention that programs start with main(). They
really don't. With the Linux API, they start at _start: and
do some housekeeping before calling main. That's what the
crt.o file that the 'C' tool-chain uses, does.


Cheers,
Dick Johnson
Penguin : Linux version 2.6.11 on an i686 machine (5537.79 BogoMips).
  Notice : All mail here is now cached for review by Dictator Bush.
                  98.36% of all statistics are fiction.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Strange Linking Problem
  2005-03-12 14:34 ` linux-os
@ 2005-03-13  9:33   ` Matthias-Christian Ott
  0 siblings, 0 replies; 3+ messages in thread
From: Matthias-Christian Ott @ 2005-03-13  9:33 UTC (permalink / raw)
  To: linux-os; +Cc: Linux Kernel Mailing List

linux-os wrote:

> On Sat, 12 Mar 2005, Matthias-Christian Ott wrote:
>
>> Hi!
>> I hope I'm right here. I've the following assembler code:
>>
>> SECTION .DATA
>>       hello:     db 'Hello world!',10
>>       helloLen:  equ $-hello
>>
>> SECTION .TEXT
>>       GLOBAL main
>>
>> main:
>>
>>
>>
>>       ; Write 'Hello world!' to the screen
>>       mov eax,4            ; 'write' system call
>>       mov ebx,1            ; file descriptor 1 = screen
>>       mov ecx,hello        ; string to write
>>       mov edx,helloLen     ; length of string to write
>>       int 80h              ; call the kernel
>>
>>       ; Terminate program
>>       mov eax,1            ; 'exit' system call
>>       mov ebx,0            ; exit with error code 0
>>       int 80h              ; call the kernel
>>
>>
>> Then I run:
>>
>> nasm -f elf hello.asm
>>
>>
>> I link it with ld and run it:
>>
>> ld -s -o hello hello.o
>> ./hello
>> segmentation fault
>>
>>
>> I link it with the gcc and run it:
>>
>> gcc hello.o -o hello
>> ./hello
>> Hello world!
>>
>>
>> What's wrong with the ld?
>>
>
> Nothing at all. Where is _start: ?
>
> Remove the 'main' label and substitute _start:
>
> It is 'C' convention that programs start with main(). They
> really don't. With the Linux API, they start at _start: and
> do some housekeeping before calling main. That's what the
> crt.o file that the 'C' tool-chain uses, does.
>
>
> Cheers,
> Dick Johnson
> Penguin : Linux version 2.6.11 on an i686 machine (5537.79 BogoMips).
>  Notice : All mail here is now cached for review by Dictator Bush.
>                  98.36% of all statistics are fiction.
>
Ofcourse you have to edit it, but this is not the problem (the linker 
will give an error message if you don't change it). Why does it cause a 
segementation fault?

Matthias-Christian Ott

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-03-13  9:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-03-12 14:01 Strange Linking Problem Matthias-Christian Ott
2005-03-12 14:34 ` linux-os
2005-03-13  9:33   ` Matthias-Christian Ott

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox