using libc from nasm

  • Follow


Hi all.  I am trying to learn asm with NASM on a FreeBSD system.  I really need
to debug my programs while I learn, so I want to use printf.  This is what I am
using to assemble and link:

nasm -f elf use_printf.asm
ld -s -o use_printf use_printf.asm -lc  

but then when I run the program:

$./use_printf
/usr/libexec/ld-elf.so.1: /lib/libc.so.5: Undefined symbol "environ"

Can somebody see where I am going wrong?  This is kindof holding me back.  I
added the 'extern environ' and 'extern __progname' beause I get this otherwise:

/usr/lib/libc.so: undefined reference to `environ'
/usr/lib/libc.so: undefined reference to `__progname'

Here's what I have( I think I have commented out the bsd specific stuff... ):

extern  printf
extern  environ
extern  __progname

section .data
mesg            db       'the number is %d\n',0
mesglen         equ      $-mesg

errormesg       db       'libc error',0ah,0     
errormesglen    equ      $-errormesg

newline         db      10
number          dw      0x10

;kernel:
;       int 80h
;       ret

align 4
section .text
global _start
_start:

        push dword      number 
        push dword      mesg 
        call printf

        ; error if eax < 1  ( we should have wrote some chars )
        cmp eax , 0x1
        jl      .error   

        ; use write() system call to print message
;       push    dword   mesglen
;       push    dword   mesg
;       push    dword   0x1   ; stdout
;       mov     eax   , 0x4   ; 4 == write system call
;       call    kernel

;       ; output a newline
;       push    dword   1
;       push    dword   newline
;       push    dword   0x1     
;       mov     eax   , 0x4 
;       call    kernel


;       mov  eax  , 0x1 ; exit syscall number
;       push dword  0x0 ; exit status
;       call kernel

..error:
;       push dword  errormesglen
;       push dword  errormesg
;       push dword  0x1
;       mov         eax , 0x4
;       call        kernel

;       mov         eax  , 0x1
;       push        dword  0xff
;       call        kernel



0
Reply Adam 2/12/2004 8:38:23 PM

Adam Bozanich <adambozanich@sbcglobal.net> writes:

> Hi all.  I am trying to learn asm with NASM on a FreeBSD system.  I really need
> to debug my programs while I learn, so I want to use printf.  This is what I am
> using to assemble and link:
> 
> nasm -f elf use_printf.asm
> ld -s -o use_printf use_printf.asm -lc  
>
> but then when I run the program:
> 
> $./use_printf
> /usr/libexec/ld-elf.so.1: /lib/libc.so.5: Undefined symbol "environ"
> 
> Can somebody see where I am going wrong?  This is kindof holding me back.  I
> added the 'extern environ' and 'extern __progname' beause I get this otherwise:
> 
> /usr/lib/libc.so: undefined reference to `environ'
> /usr/lib/libc.so: undefined reference to `__progname'
> 
> Here's what I have( I think I have commented out the bsd specific stuff... ):
> 
> extern  printf
> extern  environ

Who do you think is supplying the definition of environ?

Phil

-- 
Unpatched IE vulnerability: DNSError folder disclosure
Description: Gaining access to local security zones
Reference: http://msgs.securepoint.com/cgi-bin/get/bugtraq0306/52.html

0
Reply Phil 2/13/2004 7:12:48 AM


Adam Bozanich wrote:
> Hi all.  I am trying to learn asm with NASM on a FreeBSD system.

Hi Adam,

I've never run FreeBSD. I know it's different from Linux, but I'll take 
a guess anyway! :)

> I really need
> to debug my programs while I learn, so I want to use printf.  This is what I am
> using to assemble and link:
> 
> nasm -f elf use_printf.asm
> ld -s -o use_printf use_printf.asm -lc  

This has gotta be "use_printf.o", not "use_printf.asm", right? I forget 
what the error message is if you try to link your .asm file, but I know 
from experience it doesn't work. :)

> /usr/lib/libc.so: undefined reference to `environ'
> /usr/lib/libc.so: undefined reference to `__progname'

I don't think declaring these "extern" in your file is going to help. 
I'm not sure, but I suspect these variables must be declared in the C 
startup code - just guessing from their names. Try linking with gcc 
instead of directly with ld. I've been getting away with linking 
straight with ld in Linux, although some people have warned me that it 
might not always work - maybe this is one of these times. See if:

gcc -s -o use_printf use_printf.o -lc

helps any. You'll have to change your entrypoint to "main", too - 
"global main", and "main" where you've got "_start"... If it works, you 
may be able to see what the command line to ld is (add a "-v" switch?) 
and copy it to go back to linking direct with ld, if you want to.

> Here's what I have( I think I have commented out the bsd specific stuff... ):
> 
> extern  printf

No underscore for ELF - you got that right! Printf is *used* in your 
program, but the function is elsewhere - that's what you want "extern" for.

> extern  environ
> extern  __progname

These *don't* appear in your program, so declaring them "extern" won't 
do any good. I think the problem is that they're declared "extern' 
somewhere else - maybe ld-elf.so? - and wherever they actually exist 
isn't being linked in.
> 
> section .data
> mesg            db       'the number is %d\n',0
> mesglen         equ      $-mesg
> 
> errormesg       db       'libc error',0ah,0     
> errormesglen    equ      $-errormesg
> 
> newline         db      10
> number          dw      0x10
> 
> ;kernel:
> ;       int 80h
> ;       ret
> 
> align 4
> section .text
> global _start
> _start:
> 
>         push dword      number 

Number's only declared as a word, so you're pushing some garbage besides 
your number. Actually, you're pushing the address of the number, not the 
number itself. Try changing the number to "dd 0x10" instead of "dw 
0x10", and "push dword [number]"...

>         push dword      mesg 
>         call printf

Probably want to "add esp, 8" here - C expects the caller to clean up 
stack. This hasn't got anything to do with the problem you're having, 
but once you get it to link, I think this'll cause a seg-fault somewhere 
down the line.

You wanted to use "printf" 'cause it's "easier" than displaying the 
number yourself, right? Maybe for floating point numbers it is...

Best,
Frank

0
Reply Frank 2/13/2004 9:55:19 AM

Hello Adam, and others...

Maybe you have already found all the answers you need to go further with your
assembly language explorations, but anyway. If you haven't already done so, you
should read the FreeBSD developers handbook, chapter IV. The link is found
below.
Another good thing to do is to get really acquainted with the linker. Browse
the
manual and the supplementary documents.

Last time I did anything in assembly on a UNIX system, all names in the
libraries had
an underscroe prefixing every name.  I.e. environ would be _environ in an
assembly
language program. This is actually a compiler convention. One way to find out
more
about global name references is to look at the output from the C compiler.

Try "gcc -S whatever.c" which will produce the file whatever.s which is the
assembly
program. See "man 1 gcc" for more info.

Here is the link to the FreeBSD developers manual.

 http://www.freebsd.org/doc/en_US.ISO8859-1/books/developers-handbook/

There is a plethora of documents at the FreeBSD.org site.

By the way, is there a particular reason for using nasm?

Good luck.

  /Ulf Andersson

"Adam Bozanich" <adambozanich@sbcglobal.net> wrote in message
news:20040211234704.65d707a7.adambozanich@sbcglobal.net...
> Hi all.  I am trying to learn asm with NASM on a FreeBSD system.  I really
need
> to debug my programs while I learn, so I want to use printf.  This is what I
am
> using to assemble and link:
>
> nasm -f elf use_printf.asm
> ld -s -o use_printf use_printf.asm -lc
>
> but then when I run the program:
>
> $./use_printf
> /usr/libexec/ld-elf.so.1: /lib/libc.so.5: Undefined symbol "environ"
>
> Can somebody see where I am going wrong?  This is kindof holding me back.  I
> added the 'extern environ' and 'extern __progname' beause I get this
otherwise:
>
> /usr/lib/libc.so: undefined reference to `environ'
> /usr/lib/libc.so: undefined reference to `__progname'
>
> Here's what I have( I think I have commented out the bsd specific stuff... ):
>
> extern  printf
> extern  environ
> extern  __progname
>
> section .data
> mesg            db       'the number is %d\n',0
> mesglen         equ      $-mesg
>
> errormesg       db       'libc error',0ah,0
> errormesglen    equ      $-errormesg
>
> newline         db      10
> number          dw      0x10
>
> ;kernel:
> ;       int 80h
> ;       ret
>
> align 4
> section .text
> global _start
> _start:
>
>         push dword      number
>         push dword      mesg
>         call printf
>
>         ; error if eax < 1  ( we should have wrote some chars )
>         cmp eax , 0x1
>         jl      .error
>
>         ; use write() system call to print message
> ;       push    dword   mesglen
> ;       push    dword   mesg
> ;       push    dword   0x1   ; stdout
> ;       mov     eax   , 0x4   ; 4 == write system call
> ;       call    kernel
>
> ;       ; output a newline
> ;       push    dword   1
> ;       push    dword   newline
> ;       push    dword   0x1
> ;       mov     eax   , 0x4
> ;       call    kernel
>
>
> ;       mov  eax  , 0x1 ; exit syscall number
> ;       push dword  0x0 ; exit status
> ;       call kernel
>
> .error:
> ;       push dword  errormesglen
> ;       push dword  errormesg
> ;       push dword  0x1
> ;       mov         eax , 0x4
> ;       call        kernel
>
> ;       mov         eax  , 0x1
> ;       push        dword  0xff
> ;       call        kernel
>
>
>


0
Reply Ulf 2/15/2004 7:41:31 PM

On Sun, 15 Feb 2004 19:41:31 +0000 (UTC)
"Ulf Andersson" <ulf.andersson@ieee.org> wrote:

> Hello Adam, and others...
> 
> Maybe you have already found all the answers you need to go further with your
> assembly language explorations, but anyway. If you haven't already done so, you
> should read the FreeBSD developers handbook, chapter IV. The link is found
> below.


That's where I started!  It's a great tutorial.

> 
> Here is the link to the FreeBSD developers manual.
> 
>  http://www.freebsd.org/doc/en_US.ISO8859-1/books/developers-handbook/
> 
> There is a plethora of documents at the FreeBSD.org site.
> 
> By the way, is there a particular reason for using nasm?

Because the developers-handbook used it ;)  Also, the syntax is a little easier
to understand at first, and I think that it might be a bit more "portable".

I have used gas ( GNU Assembler ) a little now,  but prefer to use Nasm.

using nasm and then 'objdump -d file.o' is a nice way to see the differences.

Thanks a lot to all who replied.  I am sorry for the delay.  Frank, your
suggestions fixed the problem.  I appreciate it a lot.

-Adam

0
Reply Adam 2/21/2004 10:09:40 PM

4 Replies
283 Views

(page loaded in 0.097 seconds)

Similiar Articles:






7/11/2012 10:40:54 AM


Reply: