NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: 2 Questions: Static building: How is it done? / A.out unsupported?

At Wed, 23 Mar 2011 00:40:13 +0000 (GMT), ov3runity 
<> wrote:
Subject: 2 Questions: Static building: How is it done? / A.out unsupported?
> while trying building 5.1 amd64 i tried out several ways for building
> NetBSD statically linked, but none of them worked. Thes way included
> running in an environment with CRUNCHEDPROG=1 (inspirated by
> the /rescue Makefile) and of course the way described in the NetBSD
> guide, setting LDSTATIC in /etc/mk.conf.
> Sadly neither of these worked, so i want to ask humble what the way
> for making static builds is the preferred one these days.

Basic static linking of everything still works OK for me, and I've been
doing that since the 1.3 days on several different ports including
alpha, i386, and sparc.

Static linking does indeed provide many advantages, not the least of
which is avoiding running a stupidly complex and expensive program for
every exec of every process.  :-)  It also avoids entirely a whole class
of complexity problems when dealing with multiple ABIs (not unlike the
horrid mess of incompatible DLLs more common on Microsoft Windows).

As Matthias mentioned in his reply though a.out is not a possible binary
format, especially not for amd64.  You really don't need/want it.  :-)

Also, CRUNCHEDPROG=1 doesn't do what I think you think it does, at least
for the overall build.  More below.

I have made some changes to assist with basic static linking.

There is one think you have to turn off in your build if you want a
working system: PAM.

I also don't build many other things in the base OS, such as AMD,
NIS/YP, HESIOD, Postfix, OpenSSH, Kerberos, and of course CSH, but they
should all work in static-linked systems.  At the moment I still don't
build/use INET6 stuff either.

I've also integrated the PR which allows the CITRUS iconv library to
have compile-time selected charset support built in, though normally I
only include UTF8 and UTF1632 locales in my builds. 

I'm not 100% certain, but at least the following changes to the share/mk
files may also be necessary (I have a several more changes in the mk
files, but I think these are the only ones mostly relevant to static

Basically they replace the unnecessary use of $CPPFLAGS on linker lines
with the probably necessary use of $LDSTATIC.  They also dis-entangle
the overloaded meaning of $DBG and add a better named $OTPIM:

Index: share/mk/
RCS file: /cvs/master/m-NetBSD/main/src/share/mk/,v
retrieving revision 1.99
diff -u -r1.99
--- share/mk/     7 Sep 2008 15:54:52 -0000       1.99
+++ share/mk/     12 Apr 2010 22:34:58 -0000
@@ -14,9 +14,9 @@
 AS?=           as
 COMPILE.s?=    ${CC} ${AFLAGS} -c
-LINK.s?=       ${CC} ${AFLAGS} ${LDFLAGS}
+LINK.s?=       ${CC} ${AFLAGS} ${LDSTATIC} ${LDFLAGS}
 COMPILE.S?=    ${CC} ${AFLAGS} ${CPPFLAGS} -c -traditional-cpp
 CC?=           cc
 .if ${MACHINE_ARCH} == "alpha" || \
@@ -31,22 +31,25 @@
     ${MACHINE_ARCH} == "powerpc" || \
     ${MACHINE_ARCH} == "sparc" || \
     ${MACHINE_ARCH} == "sparc64"
-DBG?=  -O2
+OPTIM?=        -O2
 .elif ${MACHINE_ARCH} == "sh3el" || ${MACHINE_ARCH} == "sh3eb"
 # -O2 is too -falign-* zealous for low-memory sh3 machines
-DBG?=  -Os -freorder-blocks
+OPTIM?=        -Os -freorder-blocks
 .elif ${MACHINE_ARCH} == "vax"
-DBG?=  -O1 -fgcse -fstrength-reduce -fgcse-after-reload
+OPTIM?=   -O1 -fgcse -fstrength-reduce -fgcse-after-reload
 .elif ${MACHINE_ARCH} == "m68000"
 # see src/doc/HACKS for details
-DBG?=  -O1
+OPTIM?=        -O1
-DBG?=  -O
+OPTIM?=        -O
-CFLAGS?=       ${DBG}
+DBG?=          # nothing -- just defined
+OPTIM?=                # nothing -- just defined
+CFLAGS?=       ${OPTIM} ${DBG}
 COMPILE.c?=    ${CC} ${CFLAGS} ${CPPFLAGS} -c
-LINK.c?=       ${CC} ${CFLAGS} ${CPPFLAGS} ${LDFLAGS}
+LINK.c?=       ${CC} ${CFLAGS} ${LDSTATIC} ${LDFLAGS}
 CXX?=          c++
@@ -58,12 +61,12 @@
 OBJC?=         ${CC}
 CPP?=          cpp
@@ -72,11 +75,11 @@
 FFLAGS?=       -O
 COMPILE.f?=    ${FC} ${FFLAGS} -c
-LINK.f?=       ${FC} ${FFLAGS} ${LDFLAGS}
+LINK.f?=       ${FC} ${FFLAGS} ${LDSTATIC} ${LDFLAGS}
 COMPILE.r?=    ${FC} ${FFLAGS} ${RFLAGS} -c
-LINK.r?=       ${FC} ${FFLAGS} ${RFLAGS} ${LDFLAGS}
+LINK.r?=       ${FC} ${FFLAGS} ${LDSTATIC} ${LDFLAGS}
 INSTALL?=      install
@@ -98,7 +101,7 @@
 PC?=           pc
 COMPILE.p?=    ${PC} ${PFLAGS} ${CPPFLAGS} -c
-LINK.p?=       ${PC} ${PFLAGS} ${CPPFLAGS} ${LDFLAGS}
+LINK.p?=       ${PC} ${PFLAGS} ${LDSTATIC} ${LDFLAGS}
 SHELL?=                sh
Index: share/mk/
RCS file: /cvs/master/m-NetBSD/main/src/share/mk/,v
retrieving revision 1.179
diff -u -r1.179
--- share/mk/ 26 Oct 2008 23:13:24 -0000      1.179
+++ share/mk/ 12 Apr 2010 17:25:45 -0000
@@ -99,9 +99,9 @@
 .if defined(HOSTPROG_CXX) 
 HOST_CXX?=     c++
@@ -112,6 +112,9 @@
 HOST_LD?=      ld
+.if defined(LDSTATIC)
 HOST_AR?=      ar
 HOST_RANLIB?=  ranlib
Index: share/mk/
RCS file: /cvs/master/m-NetBSD/main/src/share/mk/,v
retrieving revision
diff -u -r1.239.2.2
--- share/mk/        6 Jun 2009 22:10:12 -0000
+++ share/mk/        12 Apr 2010 22:31:50 -0000
@@ -199,10 +199,14 @@
 .if defined(PROG_CXX)
 PROG=          ${PROG_CXX}
+# XXX for some compilers and/or object formats the linker needs to
+# know about any debug and optimizer flags too
 .if defined(PROG)
+# XXX for some compilers and/or object formats the linker needs to
+# know about any debug and optimizer flags too
 .  if defined(MAN)
 MAN.${PROG}=   ${MAN}
@@ -267,7 +271,9 @@
 LDADD+=     ${LDADD.${_P}}
+.if defined(LDSTATIC.${_P})
 _COPTS.${_P}=          ${COPTS}    ${COPTS.${_P}}
 _CPPFLAGS.${_P}=       ${CPPFLAGS} ${CPPFLAGS.${_P}}

From there I simply set the following in my mk.conf:

# set LDSTATIC=-static in order to build a static-only system
# (note you need a *LOT* of disk for '-g' and '-static' -- the debug set
# is _huge_)
LDSTATIC =      -static

# after 1.6? (2002/09/22) everything goes dynamic but I don't like that!
STRIPFLAG=      -s

I've have gone one step further to build an entirely crunchgen'ed
static-linked system, at least in netbsd-4 (still testing this in
netbsd-5 -- the build works A-OK and it boots and gives me a working
single user shell in virtualbox).  The single crunchgen binary I build
and run in a ramdisk-based system includes almost all of the entire base
set of tools and it is so efficient that an entire system can be
contained in as little as 8 MB in a ramdisk.  I think the only thing I
had to leave out was "named" because of incompatible APIs in the
different variants of common code included with both "named" and
"dhcpd".  I don't use "named" any more though so I don't care one bit
about it.  I will have to integrate its replacement into my custom
netbsd sources though so that I can crunchgen it into embedded platforms
like this.

    $ ls -l *EMBED.gz
    -r--r--r--  3 woods  wheel  7366921 Apr  1 09:41 netbsd-PCE-ALIX.EMBED.gz

There are some useful runtime files still missing from this (as well as
all the other stuff I don't normally build in the base OS, though
OpenSSH _is_ included in there), but this much gives the basis for a
quite capable embedded system, such as an access point or router.

From within the above system:

        # uname -a
        NetBSD  5.1_STABLE NetBSD 5.1_STABLE (PCE-ALIX.EMBED) #3: Wed Mar 30 
21:35:55 PDT 2011  
        # df
        Filesystem    1K-blocks       Used      Avail %Cap Mounted on
        /dev/md0a         12415      10651       1764  85% /
        mfs:3               131         38         87  30% /dev
        # ls -l /bin/sh
        -r-xr-xr-x  295 root  wheel  9501160 Apr  1 16:40 /bin/sh

                                                Greg A. Woods
                                                Planix, Inc.

<>       +1 250 762-7675

Attachment: pgpn_S7jCW274.pgp
Description: PGP signature

Home | Main Index | Thread Index | Old Index