Subject: RE: Linking Assembly Code - Can't resolve printf
From: "David Lariviere" <[email-masked]>
To: "'Kai Tietz'" <[email-masked]>
Cc: <[email-masked]>
Date: Wed, 20 Sep 2006 03:51:11 -0400

Hi Kai,

Thank you so much. That got it compiling. I had tried it before, being
inspired by the results of objdump on libc.a, but it didn't work at that
point because I hadn't yet read about the importance of ordering the -lc
after the object. I hadn't gone back and tried it since making that change,
until your suggestion. Thanks!!

The code is actually taken out of "Professional Assembly Language" by
Richard Blum.

I presume by entry-point your referring to the use of _start instead of
main. According to the book (I have no way of knowing beyond testing on a
separate linux box if it is true though), using gcc directly requires "main"
entrypoint but 'as' expects _start as the default entry-point. 

In the first piece of code I pasted, I am guessing the 0x80 signal interrupt
to write to console is a linux only feature that won't somehow get
translated by cygwin/gcc (I had hoped otherwise, but figured as much). 

Ignoring 0x80 issue (assuming that was the problem you were alluding), I can
now get it compiling but not running. Below is an earlier program from the
same book which doesn't use 0x80. 
 
#cpuid2.s View the CPUID Vendor ID string using C library 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

It now compiles fine, but crashes as soon as it runs. I am hoping it is not
the entry-point issue, since the same syntax compiled and ran successfully
on my fedora box. 

In the book, the author says that the program will crash because it is
necessary to specify the runtime dynamic linker. In the linux platform
example for which the book targets, it states to use: 
ld -dyanmic-linker /lib/ld-linux.so.2 -o cpuid2 -lc cpuid2.o

The problem is that I have no idea what the cygwin-equivalent for that would
be, assuming it's even possible. If not, assuming file size isn't a concern,
is there a way to get the compiled program to at least statically link in
all of the required libraries so that I can run it? 

Thanks again, I really appreciate the help. 

- David

-----Original Message-----
From: Kai Tietz [mailto:[email-masked]] 
Sent: Wednesday, September 20, 2006 3:30 AM
To: David Lariviere
Cc: [email-masked]
Subject: Re: Linking Assembly Code - Can't resolve printf

Hallo David,

Some OS's - as cygwin - expand names by underscores. Therefore simply 
write instead of "printf" just "_printf". That should work for this 
unresolved symbol. But the rest does not seems to be ABI compatible for 
cygwin. Additionally the entry-point won't work this way ...

Chears,
 i.A. Kai Tietz

"David Lariviere" <[email-masked]> 
Sent by: [email-masked]
20.09.2006 09:16

To
<[email-masked]>
cc

Subject
Linking Assembly Code - Can't resolve printf

I have a simple assembly program that I am trying to compile, but ld 
cannot
resolve printf. 
------
#movtest3.s - Example using index memory locations
.section .data
output:
                 .asciz "The value is %d\n"
values:
                 .int 10,15,20,25,30,35,40,45,50,55,60
                 .section .text
                 .globl _start
_start:
                 nop
                 movl $0, %edi
loop:
                 movl values(, %edi, 4), %eax
                 pushl %eax
                 pushl $output
                 call printf
                 addl $8, %esp
                 inc %edi
                 cmpl $11, %edi
                 jne loop
                 movl $0, %ebx
                 movl $1, %eax
                 int $0x80
#end of ASM

#Makefile
test:
                 as -o indexedMemory.o indexedMemory.s
                 ld --verbose -o indexedMemory indexedMemory.o -lc
#end of Makefile

Result is always:
....
attempt to open indexedMemory.o succeeded
indexedMemory.o
attempt to open /usr/bin/../lib/libc.dll.a failed
attempt to open /usr/bin/../lib/c.dll.a failed
attempt to open /usr/bin/../lib/libc.a succeeded
indexedMemory.o:fake:(.text+0x14): undefined reference to `printf'
make: *** [test] Error 1
#end of result

-------------

I've tried linking in numerous libraries, hoping one would resolve printf,
and in numerous order of where to include the -lxxx, but I can't get it to
compile. I've also tried it on numerous computers, including those with a
first-time fresh install of cygwin. Compiling a simple helloworld C 
program
works fine. 

I've compiled the same program fine in linux using as/ld. 

What am I doing wrong???

Thank you for any help, 

- David

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/