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 
<ov3runity%yahoo.de@localhost> 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 build.sh 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
linking).

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/sys.mk
===================================================================
RCS file: /cvs/master/m-NetBSD/main/src/share/mk/sys.mk,v
retrieving revision 1.99
diff -u -r1.99 sys.mk
--- share/mk/sys.mk     7 Sep 2008 15:54:52 -0000       1.99
+++ share/mk/sys.mk     12 Apr 2010 22:34:58 -0000
@@ -14,9 +14,9 @@
 AS?=           as
 AFLAGS?=
 COMPILE.s?=    ${CC} ${AFLAGS} -c
-LINK.s?=       ${CC} ${AFLAGS} ${LDFLAGS}
+LINK.s?=       ${CC} ${AFLAGS} ${LDSTATIC} ${LDFLAGS}
 COMPILE.S?=    ${CC} ${AFLAGS} ${CPPFLAGS} -c -traditional-cpp
-LINK.S?=       ${CC} ${AFLAGS} ${CPPFLAGS} ${LDFLAGS}
+LINK.S?=       ${CC} ${AFLAGS} ${LDSTATIC} ${LDFLAGS}
 
 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
 .else
-DBG?=  -O
+OPTIM?=        -O
 .endif
-CFLAGS?=       ${DBG}
+DBG?=          # nothing -- just defined
+OPTIM?=                # nothing -- just defined
+
+CFLAGS?=       ${OPTIM} ${DBG}
 LDFLAGS?=
 COMPILE.c?=    ${CC} ${CFLAGS} ${CPPFLAGS} -c
-LINK.c?=       ${CC} ${CFLAGS} ${CPPFLAGS} ${LDFLAGS}
+LINK.c?=       ${CC} ${CFLAGS} ${LDSTATIC} ${LDFLAGS}
 
 CXX?=          c++
 CXXFLAGS?=     
${CFLAGS:N-Wno-traditional:N-Wstrict-prototypes:N-Wmissing-prototypes:N-Wno-pointer-sign:N-ffreestanding:N-std=gnu99}
@@ -58,12 +61,12 @@
 _CXXSEED?=     
${BUILDSEED:D-frandom-seed=${BUILDSEED:Q}/${__ALLSRC3:O:Q}/${.TARGET:Q}}
 
 COMPILE.cc?=   ${CXX} ${_CXXSEED} ${CXXFLAGS} ${CPPFLAGS} -c
-LINK.cc?=      ${CXX} ${CXXFLAGS} ${CPPFLAGS} ${LDFLAGS}
+LINK.cc?=      ${CXX} ${CXXFLAGS} ${LDSTATIC} ${LDFLAGS}
 
 OBJC?=         ${CC}
 OBJCFLAGS?=    ${CFLAGS}
 COMPILE.m?=    ${OBJC} ${OBJCFLAGS} ${CPPFLAGS} -c
-LINK.m?=       ${OBJC} ${OBJCFLAGS} ${CPPFLAGS} ${LDFLAGS}
+LINK.m?=       ${OBJC} ${OBJCFLAGS} ${LDSTATIC} ${LDFLAGS}
 
 CPP?=          cpp
 CPPFLAGS?=
@@ -72,11 +75,11 @@
 FFLAGS?=       -O
 RFLAGS?=
 COMPILE.f?=    ${FC} ${FFLAGS} -c
-LINK.f?=       ${FC} ${FFLAGS} ${LDFLAGS}
+LINK.f?=       ${FC} ${FFLAGS} ${LDSTATIC} ${LDFLAGS}
 COMPILE.F?=    ${FC} ${FFLAGS} ${CPPFLAGS} -c
-LINK.F?=       ${FC} ${FFLAGS} ${CPPFLAGS} ${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
 PFLAGS?=
 COMPILE.p?=    ${PC} ${PFLAGS} ${CPPFLAGS} -c
-LINK.p?=       ${PC} ${PFLAGS} ${CPPFLAGS} ${LDFLAGS}
+LINK.p?=       ${PC} ${PFLAGS} ${LDSTATIC} ${LDFLAGS}
 
 SHELL?=                sh
 
Index: share/mk/bsd.sys.mk
===================================================================
RCS file: /cvs/master/m-NetBSD/main/src/share/mk/bsd.sys.mk,v
retrieving revision 1.179
diff -u -r1.179 bsd.sys.mk
--- share/mk/bsd.sys.mk 26 Oct 2008 23:13:24 -0000      1.179
+++ share/mk/bsd.sys.mk 12 Apr 2010 17:25:45 -0000
@@ -99,9 +99,9 @@
 HOST_COMPILE.c?=${HOST_CC} ${HOST_CFLAGS} ${HOST_CPPFLAGS} -c
 HOST_COMPILE.cc?=      ${HOST_CXX} ${HOST_CXXFLAGS} ${HOST_CPPFLAGS} -c
 .if defined(HOSTPROG_CXX) 
-HOST_LINK.c?=  ${HOST_CXX} ${HOST_CXXFLAGS} ${HOST_CPPFLAGS} ${HOST_LDFLAGS}
+HOST_LINK.c?=  ${HOST_CXX} ${HOST_CXXFLAGS} ${HOST_LDSTATIC} ${HOST_LDFLAGS}
 .else
-HOST_LINK.c?=  ${HOST_CC} ${HOST_CFLAGS} ${HOST_CPPFLAGS} ${HOST_LDFLAGS}
+HOST_LINK.c?=  ${HOST_CC} ${HOST_CFLAGS} ${HOST_LDSTATIC} ${HOST_LDFLAGS}
 .endif
 
 HOST_CXX?=     c++
@@ -112,6 +112,9 @@
 
 HOST_LD?=      ld
 HOST_LDFLAGS?=
+.if defined(LDSTATIC)
+HOST_LDSTATIC?=        ${LDSTATIC}
+.endif
 
 HOST_AR?=      ar
 HOST_RANLIB?=  ranlib
Index: share/mk/bsd.prog.mk
===================================================================
RCS file: /cvs/master/m-NetBSD/main/src/share/mk/bsd.prog.mk,v
retrieving revision 1.239.2.2
diff -u -r1.239.2.2 bsd.prog.mk
--- share/mk/bsd.prog.mk        6 Jun 2009 22:10:12 -0000       1.239.2.2
+++ share/mk/bsd.prog.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
 _CCLINK=       ${CXX} ${_CCLINKFLAGS}
 .endif
 
 .if defined(PROG)
+# XXX for some compilers and/or object formats the linker needs to
+# know about any debug and optimizer flags too
 _CCLINK?=      ${CC} ${_CCLINKFLAGS}
 .  if defined(MAN)
 MAN.${PROG}=   ${MAN}
@@ -267,7 +271,9 @@
 OBJCOPTS+=  ${OBJCOPTS.${_P}}
 LDADD+=     ${LDADD.${_P}}
 LDFLAGS+=   ${LDFLAGS.${_P}}
+.if defined(LDSTATIC.${_P})
 LDSTATIC+=  ${LDSTATIC.${_P}}
+.endif
 
 _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
HOST_LDSTATIC = -static

# after 1.6? (2002/09/22) everything goes dynamic but I don't like that!
#
MKDYNAMICROOT = no
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  
woods@once:/rest/build/woods/once/netbsd-5-i386-i386-ppro-obj/rest/work/woods/m-NetBSD-5/sys/arch/i386/compile/PCE-ALIX.EMBED
 i386
        # 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.

<woods%planix.com@localhost>       +1 250 762-7675        http://www.planix.com/

Attachment: pgpn_S7jCW274.pgp
Description: PGP signature



Home | Main Index | Thread Index | Old Index