Linux assembly using C library functions
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Linux assembly using C library functions




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Linux assembly using C library functions  
Roeland Hemsteede


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-21-06 12:15 PM

I am trying to assemble and link an assembly program which uses C
library functions. When I assemble and link the program using gas and ld
everything goes fine and the program is build properly. However when
using GCC it wont. I'm wondering why this is happening. Below are the
errors I get when using GCC followed by the program code

-- Using gas and ld --
roeland@rootland:$ as cpuid2.s -o cpuid2.o roeland@rootland:$ ld
-dynamic-linker /lib/ld-linux.so.2 -o cpuid2 -lc cpuid2.o
roeland@rootland:$ ./cpuid2
The processor Vendor ID is 'AuthenticAMD'


-- GCC errors --
roeland@rootland:$ gcc -o cpuid2 cpuid2.s
/tmp/ccHXqNul.o(.text+0x0): In function `_start':
: multiple definition of `_start'
/usr/lib/gcc-lib/i486-linux/3.3.5/../../../crt1.o(.text+0x0):../sysdeps/i386
/elf/start.S:47:
first defined here
/usr/lib/gcc-lib/i486-linux/3.3.5/../../../crt1.o(.text+0x18): In
function `_start':
../sysdeps/i386/elf/start.S:98: undefined reference to `main'
collect2: ld returned 1 exit status
roeland@rootland:$

-- Program Code --
# CPUID program 2, view the Vendor ID string using system calls
.section .data
output:
.asciz "The processor Vendor ID is '%s'\n"

.section .bss
.lcomm buffer, 12

.section .text
.globl _start
_start:
movl $0, %eax
cpuid
movl $buffer, %edi
movl %ebx, (%edi)
movl %edx, 4(%edi)
movl %ecx, 8(%edi)
pushl $buffer
pushl $output
call printf
addl $8, %esp
pushl $0
call exit


Can someone explain why gcc comes up with these errors? (I also tried "
gcc -dynamic-linker /lib/ld-linux.so.2 -o cpuid2 -lc cpuid2.s" with the
same problems)





[ Post a follow-up to this message ]



    Re: Linux assembly using C library functions  
Hubble


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-21-06 12:15 PM

>I am trying to assemble and link an assembly program which uses C
>library functions. When I assemble and link the program using gas and ld
>  everything goes fine and the program is build properly. However when
>using GCC it wont. I'm wondering why this is happening.
>...
>-- GCC errors --
>roeland@rootland:$ gcc -o cpuid2 cpuid2.s
>/tmp/ccHXqNul.o(.text+0x0): In function `_start':
>: multiple definition of `_start'

Note that gcc adds startup code and default libraries. crt0.o (or so)
calls main and after return exits. It is linked by default before any
other object files. You must add linker options to prevent this. Like
(untested)

$ gcc -nostartfiles -nostdlib -nodefaultlibs -ocpuid2 cpuid2.s -lc

I do not know if you can prevent linking crt0.o and use libc.

Hubble.






[ Post a follow-up to this message ]



    Re: Linux assembly using C library functions  
Brian Raiter


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-22-06 12:14 AM

> $ gcc -nostartfiles -nostdlib -nodefaultlibs -ocpuid2 cpuid2.s -lc
>
> I do not know if you can prevent linking crt0.o and use libc.

Unfortunately, you often can. Doing so can produce irregular bugs. If
you don't care about portability and reliability, and if it seems to
works on your machine, then go for it. Otherwise, you must choose:
Either write your own _start and do without libc, or else use main().

b





[ Post a follow-up to this message ]



    Re: Linux assembly using C library functions  
Hubble


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-22-06 06:15 PM

> $ gcc -nostartfiles -nostdlib -nodefaultlibs -ocpuid2 cpuid2.s -lc 

[vbcol=seagreen]
>Unfortunately, you often can. Doing so can produce irregular bugs. If
>you don't care about portability and reliability, and if it seems to
>works on your machine, then go for it. Otherwise, you must choose:
>Either write your own _start and do without libc, or else use main().
>b

So the best way would be to replace the _start symbol with _main and
let crt0.o go in. Perhaps Roeland should write an empty main(), compile
it with gcc -S and look at the assemlby code produced by the
C-compiler.

Hubble.






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:36 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register