From owner-freebsd-questions@FreeBSD.ORG Thu Feb 12 12:46:11 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id BC4D616A4CE for ; Thu, 12 Feb 2004 12:46:11 -0800 (PST) Received: from hills.ccsf.cc.ca.us (hills.ccsf.cc.ca.us [147.144.1.2]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9B87843D2F for ; Thu, 12 Feb 2004 12:46:11 -0800 (PST) (envelope-from abozan01@ccsf.edu) Received: from localhost (abozan01@localhost) by hills.ccsf.cc.ca.us (8.11.3/8.11.3) with ESMTP id i1CKkBk05816 for ; Thu, 12 Feb 2004 12:46:11 -0800 (PST) X-Authentication-Warning: hills.ccsf.cc.ca.us: abozan01 owned process doing -bs Date: Thu, 12 Feb 2004 12:46:10 -0800 (PST) From: Adam Bozanich X-X-Sender: abozan01@hills.ccsf.cc.ca.us To: freebsd-questions@freebsd.org Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Subject: nasm, ld, libc, and **environ X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 12 Feb 2004 20:46:11 -0000 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 -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" If I try to use static linking, I get this: $ld -static -o use_printf use_printf.asm -lc /usr/lib/libc.a(getenv.o): In function `getenv': getenv.o(.text+0x19): undefined reference to `environ' getenv.o(.text+0x29): undefined reference to `environ' getenv.o(.text+0x63): undefined reference to `environ' /usr/lib/libc.a(getenv.o): In function `__findenv': getenv.o(.text+0xd5): undefined reference to `environ' getenv.o(.text+0xe2): undefined reference to `environ' /usr/lib/libc.a(getenv.o)(.text+0x113): more undefined references to `environ' follow /usr/lib/libc.a(getprogname.o): In function `_getprogname': getprogname.o(.text+0x4): undefined reference to `__progname' 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: (when NOT using -static) /usr/lib/libc.so: undefined reference to `environ' /usr/lib/libc.so: undefined reference to `__progname' Here's what I have: ;;;;;;;;;;;;;;;;;;;;;; BEGIN CODE ;;;;;;;;;;;;;;;;;;;;;;;;;;;; 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: ; call printf from libc 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 ;;;;;;;;;;;;;;;; END CODE ;;;;;;;;;;;;;;;;;;;;;;;;;;; Any suggestions will be greatly appreciated. Thanks -Adam