|
|
Linux assembly using C library functions
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)
|
|
0
|
|
|
|
Reply
|
Roeland
|
5/21/2006 10:53:00 AM |
|
>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.
|
|
0
|
|
|
|
Reply
|
Hubble
|
5/21/2006 11:31:41 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
|
|
0
|
|
|
|
Reply
|
Brian
|
5/21/2006 9:50:44 PM
|
|
> $ 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
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.
|
|
0
|
|
|
|
Reply
|
Hubble
|
5/22/2006 1:54:04 PM
|
|
|
3 Replies
447 Views
(page loaded in 0.056 seconds)
Similiar Articles: how to make calls to C library functions on Mac - comp.lang.asm ...Hi all, I need to make the assembly code developed on Linux portable to Mac OSX. And the assembly code contains plenty of calls to C library functions, such as printf ... How to implement exception handling in linux assembly? - comp.lang ...... rules (this is to allow such things as exception handling ... example, one would have to either: > not implement ... Linux assembly using C library functions - comp.unix ... Using/Porting C libraries from Windows to Linux - comp.unix ...... Linux and Windows. Header files may call all sorts of lovely "helper" functions not defined on the other system (and which require the C library ... assembly using C library ... about undefined symbol in module C - comp.lang.asm.x86Linux assembly using C library functions - comp.unix.programmer ... Linking to zlibstat.lib and still unresolved functions - comp ... about undefined symbol in module C ... how to monitor network status using C/C++ library functions - comp ...My implementation uses a global proxy function to ... I am writing a system monitor, in C for Linux ... how to monitor network status using C/C++ library functions - comp Recursive Programming and Assembly Language - comp.lang.asm.x86 ...... www-128.ibm.com/developerworks/library/l-recurs.html?ca=dnt-624 Enjoy. Jon ---- Learn to program using Linux assembly ... than jumping to the beginning of the function ... itoa() in Linux - comp.unix.programmerHi all, I'm using the itoa function in my C code but it does not compile in Linux 2.4 (Redhat). I have stdilb.h included. I thought itoa is part of the standard C library ... Where do message queues, named pipes reside? - comp.unix ...... ibm-db2 Message queue sizing on Linux - comp.databases.ibm-db2 Where do message queues, named pipes reside? - comp.unix ... ... Linux assembly using C library functions ... sin,cos math functions in assembly - comp.lang.asm.x86is it possible to use sin,cos math functions in assembly, at ... sin,cos math functions in assembly - comp.lang.asm.x86... You can always call the C run-time library routines ... Running SAS on Ubuntu via wine - comp.soft-sys.sasHow to implement exception handling in linux assembly? - comp.lang ... Linux assembly using C library functions - comp.unix ... ... Implementing delays while thread is ... Unix & Linux: Linux assembly using C library functions ...programming.itags.org: Unix & Linux question: Linux assembly using C library functions, created at:Wed, 07 May 2008 15:57:00 GMT with 1,760 bytes, last updated ... Calling C Functions in Inline Assembly - Microsoft Corporation ...An __asm block can call C functions, including C library routines. The following example calls the printf library routine: 7/23/2012 1:56:47 PM
|
|
|
|
|
|
|
|
|