Re: using libc from nasm

From: Frank Kotler (fbkotler_at_comcast.net)
Date: 02/13/04

  • Next message: Nudge: "Re: SSE2/nasm/linux seg-fault"
    Date: Fri, 13 Feb 2004 09:55:19 +0000 (UTC)
    
    

    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


  • Next message: Nudge: "Re: SSE2/nasm/linux seg-fault"

    Relevant Pages

    • Re: extern
      ... >> without extern. ... > I'd *highly* suggest you use the extern keyword when declaring external ... 6.9.2/2 "A declaration of an identifier for an object that has file ... If a translation unit contains one or more tentative ...
      (comp.lang.c)
    • Re: Extracting data symbols from an archive using dlsym
      ... I tried declaring it extern. ... why I tried going for the dlopen/dlsym approach. ... The library is in archive format. ...
      (comp.unix.programmer)
    • Re: Why not auto?
      ... >>> be extern or auto makes even less sense than declaring it register. ...
      (comp.lang.c)
    • extern declaration of a function pointer
      ... I have a c program in which in a file i am declaring the following ... I want to use the same variable in another file as an extern. ... The compiler is giving me warnings and error. ... warning say conflicting type of a pointing to the same line. ...
      (alt.comp.lang.learn.c-cpp)
    • linking asm and c procedures
      ... I've got problem with linking asm and c procedures. ... extern int buffer; // coded in asm ... the problem is only when i try to link both OBJ files. ...
      (comp.lang.asm.x86)