1.\" $NetBSD: crunchgen.1,v 1.32 2014/03/18 18:20:44 riastradh Exp $ 2.\" 3.\" Copyright (c) 1994 University of Maryland 4.\" All Rights Reserved. 5.\" 6.\" Permission to use, copy, modify, distribute, and sell this software and its 7.\" documentation for any purpose is hereby granted without fee, provided that 8.\" the above copyright notice appear in all copies and that both that 9.\" copyright notice and this permission notice appear in supporting 10.\" documentation, and that the name of U.M. not be used in advertising or 11.\" publicity pertaining to distribution of the software without specific, 12.\" written prior permission. U.M. makes no representations about the 13.\" suitability of this software for any purpose. It is provided "as is" 14.\" without express or implied warranty. 15.\" 16.\" U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL 17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M. 18.\" BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 19.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 20.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR 21.\" IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 22.\" 23.\" Author: James da Silva, Systems Design and Analysis Group 24.\" Computer Science Department 25.\" University of Maryland at College Park 26.\" 27.Dd June 10, 2013 28.Dt CRUNCHGEN 1 29.Os 30.Sh NAME 31.Nm crunchgen 32.Nd generates build environment for a crunched binary 33.Sh SYNOPSIS 34.Nm 35.Op Fl fOoq 36.Op Fl c Ar c-file-name 37.Op Fl D Ar src-root 38.Op Fl d Ar build-options 39.Op Fl e Ar exec-file-name 40.Op Fl L Ar lib-dir 41.Op Fl m Ar makefile-name 42.Op Fl v Ar var-spec 43.Ar conf-file 44.Sh DESCRIPTION 45A crunched binary is a program made up of many other programs linked 46together into a single executable. 47The crunched binary 48.Fn main 49function determines which component program to run by the contents of 50argv[0]. 51The main reason to crunch programs together is for fitting as many 52programs as possible onto an installation or system recovery floppy. 53.Pp 54.Nm 55reads in the specifications in 56.Ar conf-file 57for a crunched binary, and generates a Makefile and accompanying 58top-level C source file that when built create the crunched executable 59file from the component programs. 60For each component program, 61.Nm 62can optionally attempt to determine the object (.o) files that make up 63the program from its source directory Makefile. 64This information is cached between runs. 65.Nm 66uses the companion program 67.Em crunchide 68to eliminate link-time conflicts between the component programs by 69hiding all unnecessary symbols. 70.Pp 71After 72.Nm 73is run, the crunched binary can be built by running 74.Dq make -f Ao conf-name Ac Ns .mk . 75The component programs' object files must already be built. 76An 77.Dq objs 78target, included in the output makefile, will 79run make in each component program's source dir to build the object 80files for the user. 81This is not done automatically since in release 82engineering circumstances it is generally not desirable to be 83modifying objects in other directories. 84.Pp 85The options are as follows: 86.Bl -tag -width indent 87.It Fl c Ar c-file-name 88Set output C file name to 89.Ar c-file-name . 90The default name is 91.Dq Ao confname Ac Ns .c . 92.It Fl D Ar src-root 93Assume that relative source directory specifications begin with 94.Ar src-root . 95.It Fl d Ar build-options 96Set the DBG variable in the generated makefile to 97.Ar build-options . 98The default flags are -Os. 99.It Fl e Ar exec-file-name 100Set crunched binary executable file name to 101.Ar exec-file-name . 102The default name is 103.Dq Aq Mt conf-name . 104.It Fl f 105Flush cache. 106Forces the recalculation of cached parameters. 107.It Fl L Ar lib-dir 108Try to obtain libraries from 109.Ar lib-dir . 110.It Fl m Ar makefile-name 111Set output Makefile name to 112.Ar makefile-name . 113The default name is 114.Dq Ao conf-name Ac Ns .mk . 115.It Fl O 116Force 117.Nm 118to parse the program's Makefile in determine the list of .o files. 119Without this option 120.Nm 121expects the program's Makefile to have a program.ro target that links all 122the program objects into a single relocatable. 123.It Fl o 124Use existing object files. 125Rather than rebuilding object files via reach-over 126makefiles, instead search for and use existing object files. 127.It Fl q 128Quiet operation. 129Status messages are suppressed. 130.It Fl v Ar varspec 131Append a variable specification to the on-the fly generated Makefile. 132.El 133.Sh CRUNCHGEN CONFIGURATION FILE COMMANDS 134.Nm 135reads specifications from the 136.Ar conf-file 137that describe the components of the crunched binary. 138In its simplest 139use, the component program names are merely listed along with the 140top-level source directories in which their sources can be found. 141.Nm 142then calculates (via the source makefiles) and caches the 143list of object files and their locations. 144For more specialized 145situations, the user can specify by hand all the parameters that 146.Nm 147needs. 148.Pp 149The 150.Ar conf-file 151commands are as follows: 152.Bl -tag -width indent 153.It Nm srcdirs Ar dirname ... 154A list of source trees in which the source directories of the 155component programs can be found. 156These dirs are searched using the 157.Bx 158.Dq Ao source-dir Ac Ns / Ns Ao progname Ac Ns / 159convention. 160Multiple 161.Em srcdirs 162lines can be specified. 163The directories are searched in the order they are given. 164.It Nm progs Ar progname ... 165A list of programs that make up the crunched binary. 166Multiple 167.Em progs 168lines can be specified. 169.It Nm libs Ar libspec ... 170A list of library specifications to be included in the crunched binary link. 171Multiple 172.Em libs 173lines can be specified. 174.It Nm ln Ar progname linkname 175Causes the crunched binary to invoke 176.Ar progname 177whenever 178.Ar linkname 179appears in argv[0]. 180This allows programs that change their behavior when 181run under different names to operate correctly. 182.El 183.Pp 184To handle specialized situations, such as when the source is not 185available or not built via a conventional Makefile, the following 186.Em special 187commands can be used to set 188.Nm 189parameters for a component program. 190.Bl -tag -width indent 191.It Nm special Ar progname Nm keepsymbols Ar symbols ... 192Don't hide the specified symbols for 193.Ar progname . 194Normally all externally visible symbols for 195a program is hidden to avoid interference. 196Multiple 197.Em keepsymbols 198lines can be specified for given 199.Ar progname . 200.It Nm special Ar progname Nm srcdir Ar pathname 201Set the source directory for 202.Ar progname . 203This is normally calculated by searching the specified 204.Em srcdirs 205for a directory named 206.Ar progname . 207.It Nm special Ar progname Nm objdir Ar pathname 208Set the obj directory for 209.Ar progname . 210This is normally calculated by looking for a directory named 211.Dq Pa obj 212under the 213.Ar srcdir , 214and if that is not found, the 215.Ar srcdir 216itself becomes the 217.Ar objdir . 218.Pp 219.Nm Note : 220This option only takes effect if the -o option to use existing object files is also 221specified. 222.It Nm special Ar progname Nm objs Ar object-file-name ... 223Set the list of object files for program 224.Ar progname . 225This is normally calculated by constructing a temporary makefile that includes 226.Dq Nm srcdir / Pa Makefile 227and outputs the value of $(OBJS). 228Multiple 229.Em objs 230lines can be specified for given 231.Ar progname . 232.It Nm special Ar progname Nm objpaths Ar full-pathname-to-object-file ... 233Sets the pathnames of the object files for program 234.Ar progname . 235This is normally calculated by prepending the 236.Em objdir 237pathname to each file in the 238.Nm objs 239list. 240Multiple 241.Em objpaths 242lines can be specified for given 243.Ar progname . 244.El 245.Pp 246Only the 247.Em objpaths 248parameter is actually needed by 249.Nm 250but it is calculated from 251.Em objdir 252and 253.Em objs , 254which are in turn calculated from 255.Em srcdir , 256so is sometimes convenient to specify the earlier parameters and let 257.Nm 258calculate forward from there if it can. 259.Pp 260The makefile produced by 261.Nm 262contains an optional 263.Ar objs 264target that will build the object files for each component program by 265running make inside that program's source directory. 266For this to work the 267.Em srcdir 268and 269.Em objs 270parameters must also be valid. 271If they are not valid for a particular program, that 272program is skipped in the 273.Ar objs 274target. 275.Pp 276The makefile produced by 277.Nm 278strips certain sections from the final binary to reduce its size. 279This includes: 280.Bl -tag -width ".Li .eh_frame_hdr" 281.It Li .eh_frame 282Unwinding tables for exceptions and backtraces. 283.It Li .eh_frame_hdr 284Index of the 285.Li .eh_frame 286section. 287.It Li .note 288Optional data for the kernel and/or runtime linker. 289.It Li .comment 290Comments in the binary. 291.It Li .ident 292Embedded source revisions used by 293.Xr ident 1 . 294.It Li .copyright 295Embedded copyright notes. 296.El 297.Sh ENVIRONMENT 298.Bl -tag -width MAKEOBJDIRPREFIX 299.It Ev MAKEOBJDIRPREFIX 300If the environment variable 301.Ev MAKEOBJDIRPREFIX 302is set, the object directory will be prefixed with the path contained in this 303environment variable. 304.Pp 305.Nm Note : 306This variable is only used if the -o option to use existing object files is also 307specified. 308.It Ev MACHINE 309If the environment variable 310.Ev MACHINE 311is set, it is used as the name of the machine type, when accessing object 312directories of the form obj.MACHINE. 313If it is not set, it defaults to the machine type returned by 314.Xr uname 3 . 315.Pp 316.Nm Note : 317This option is only used if the -o option to use existing object files is also 318specified. 319.It Ev MAKE 320If the environment variable 321.Ev MAKE 322is set, it is used as the name of the 323.Xr make 1 324executable to be called. 325If this environment variable is not set, 326.Nm 327defaults to 328.Dq make . 329.El 330.Sh EXAMPLES 331Here is an example 332.Em crunchgen 333input conf file, named 334.Dq Pa kcopy.conf : 335.Pp 336.Bd -literal -offset indent 337srcdirs /usr/src/bin /usr/src/sbin 338 339progs test cp echo sh fsck halt init mount umount myinstall 340ln test [ # test can be invoked via [ 341ln sh -sh # init invokes the shell with "-sh" in argv[0] 342 343special myprog objpaths /homes/leroy/src/myinstall.o # no sources 344 345libs -lutil -lcrypt 346.Ed 347.Pp 348This conf file specifies a small crunched binary consisting of some 349basic system utilities plus a home-grown install program 350.Dq myinstall , 351for which no source directory is specified, but its object file is 352specified directly with the 353.Em special 354line. 355.Pp 356The crunched binary 357.Dq kcopy 358can be built as follows: 359.Pp 360.Bd -literal -offset indent 361% crunchgen -m Makefile kcopy.conf # gen Makefile and kcopy.c 362% make objs # build the component programs' .o files 363% make # build the crunched binary kcopy 364% kcopy sh # test that this invokes a sh shell 365$ # it works! 366.Ed 367.Pp 368At this point the binary 369.Dq kcopy 370can be copied onto an install floppy 371and hard-linked to the names of the component programs. 372.Sh SEE ALSO 373.Xr crunchide 1 , 374.Xr make 1 375.Sh AUTHORS 376.Nm 377was written by 378.An James da Silva Aq Mt jds@cs.umd.edu . 379.Pp 380Copyright (c) 1994 University of Maryland. All Rights Reserved. 381.Sh BUGS 382While 383.Nm 384takes care to eliminate link conflicts between the component programs 385of a crunched binary, conflicts are still possible between the 386libraries that are linked in. 387Some shuffling in the order of 388libraries may be required, and in some rare cases two libraries may 389have an unresolvable conflict and thus cannot be crunched together. 390.Pp 391Some versions of the 392.Bx 393build environment do not by default build the 394intermediate object file for single-source file programs. 395The 396.Dq make objs 397target must then be used to get those object files built, or 398some other arrangements made. 399.Pp 400If a program directory being searched for is found, but contains 401no objects, other directories are not searched. 402This causes the following directive to fail: 403.Pp 404.Bd -literal -offset indent 405srcdirs /usr/src/usr.bin /usr/src/usr.bin/less 406progs less gzip 407.Ed 408.Pp 409as the 410.Pa /usr/src/usr.bin/less 411directory will be found with the 412.Pa /usr/src/usr.bin 413.Em srcdirs 414entry, and as it does not contain the require objects, 415.Nm 416fails to find objects for the 417.Em less 418program. 419To avoid this problem, list specific srcdirs first, and 420the more general ones later, for e.g.: 421.Pp 422.Bd -literal -offset indent 423srcdirs /usr/src/usr.bin/less /usr/src/usr.bin 424progs less gzip 425.Ed 426.Pp 427will not have the above problem. 428