From owner-freebsd-questions@FreeBSD.ORG Tue Aug 30 11:11:14 2005 Return-Path: X-Original-To: freebsd-questions@freebsd.org 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 41D4516A41F for ; Tue, 30 Aug 2005 11:11:14 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from kane.otenet.gr (kane.otenet.gr [195.170.0.95]) by mx1.FreeBSD.org (Postfix) with ESMTP id 7F86043D4C for ; Tue, 30 Aug 2005 11:11:13 +0000 (GMT) (envelope-from keramida@ceid.upatras.gr) Received: from orion.daedalusnetworks.priv (aris.bedc.ondsl.gr [62.103.39.226]) by kane.otenet.gr (8.13.4/8.13.4/Debian-1) with SMTP id j7UBBAf7003045; Tue, 30 Aug 2005 14:11:11 +0300 Received: from orion.daedalusnetworks.priv (orion [127.0.0.1]) by orion.daedalusnetworks.priv (8.13.4/8.13.4) with ESMTP id j7UBBAUR080762; Tue, 30 Aug 2005 14:11:10 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by orion.daedalusnetworks.priv (8.13.4/8.13.4/Submit) id j7UBB9mx080761; Tue, 30 Aug 2005 14:11:09 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) X-Authentication-Warning: orion.daedalusnetworks.priv: keramida set sender to keramida@ceid.upatras.gr using -f Date: Tue, 30 Aug 2005 14:11:09 +0300 From: Giorgos Keramidas To: Jonathon McKitrick Message-ID: <20050830111109.GB80696@orion.daedalusnetworks.priv> References: <20050830032917.GA39730@dogma.freebsd-uk.eu.org> <20050830103702.GA80388@orion.daedalusnetworks.priv> <20050830104359.GA43823@dogma.freebsd-uk.eu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050830104359.GA43823@dogma.freebsd-uk.eu.org> Cc: freebsd-questions@freebsd.org Subject: Re: Linking standalone NASM binary with libc X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 30 Aug 2005 11:11:14 -0000 On 2005-08-30 11:43, Jonathon McKitrick wrote: >On Tue, Aug 30, 2005 at 01:37:02PM +0300, Giorgos Keramidas wrote: >>On 2005-08-30 04:29, Jonathon McKitrick wrote: >>> I'm doing some experimentation with assembly code based on the int80h.org >>> tutorials. But since I am going to use malloc and some other functions, >>> I need to make my code link with libc rather than stand totally on its own. >>> >>> ld -s -o foo foo.o -lc >>> >>> leaves 'environ' and '__progname' undefined. What is the correct way to link >>> standalone asm code with needed libraries? >> >> That depends on what the ``standalone'' code contains. If your foo.o >> object file defines a 'main' function, then you can just use cc(1): > > This is the method I've been using until now. And maybe it's the best one. I > was just wondering, though, if I want to write an app that is linked to libc, > but doesn't have 'main', and has '_start' instead, and where I want to use ld > directly rather than indirectly through cc to link. That's up to you, really. Linking to the libc library has both good and bad points. 1) Good - You don't need to reinvent the wheel as far as program startup, cleanup and other useful bits are concerned. - Since the C library has setup up things for you, you can call all the known functions that libc or other libraries that use the same calling conventions support (i.e. any library available on your FreeBSD system). 2) Bad - The extra bloat of the C startup/cleanup code. - When you call into C functions you should follow the C calling conventions, otherwise the called library functions may break in bad ways. I'm sure others can think of more points in support or linking to libc and against linking to it :-)