xref: /freebsd/usr.sbin/crunch/crunchgen/crunchgen.1 (revision a90b9d0159070121c221b966469c3e36d912bf82)
1.\"
2.\" Copyright (c) 1994 University of Maryland
3.\" All Rights Reserved.
4.\"
5.\" Permission to use, copy, modify, distribute, and sell this software and its
6.\" documentation for any purpose is hereby granted without fee, provided that
7.\" the above copyright notice appear in all copies and that both that
8.\" copyright notice and this permission notice appear in supporting
9.\" documentation, and that the name of U.M. not be used in advertising or
10.\" publicity pertaining to distribution of the software without specific,
11.\" written prior permission.  U.M. makes no representations about the
12.\" suitability of this software for any purpose.  It is provided "as is"
13.\" without express or implied warranty.
14.\"
15.\" U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
17.\" BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
20.\" IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21.\"
22.\" Author: James da Silva, Systems Design and Analysis Group
23.\"			   Computer Science Department
24.\"			   University of Maryland at College Park
25.\"
26.Dd January 6, 2017
27.Dt CRUNCHGEN 1
28.Os
29.Sh NAME
30.Nm crunchgen
31.Nd generates build environment for a crunched binary
32.Sh SYNOPSIS
33.Bk -words
34.Nm
35.Op Fl foql
36.Op Fl h Ar makefile-header-name
37.Op Fl m Ar makefile-name
38.Op Fl p Ar obj-prefix
39.Op Fl c Ar c-file-name
40.Op Fl e Ar exec-file-name
41.Ar conf-file
42.Ek
43.Sh DESCRIPTION
44A crunched binary is a program made up of many other programs linked
45together into a single executable.
46The crunched binary
47.Fn main
48function determines which component program to run by the contents of
49.Va argv[0] .
50The main reason to crunch programs together is for fitting
51as many programs as possible onto an installation or system recovery
52floppy.
53.Pp
54The
55.Nm
56utility reads in the specifications in
57.Ar conf-file
58for a crunched binary, and generates a
59.Pa Makefile
60and accompanying
61top-level C source file that when built creates the crunched executable
62file from the component programs.
63For each component program,
64.Nm
65can optionally attempt to determine the object (.o) files that make up
66the program from its source directory
67.Pa Makefile .
68This information is cached between runs.
69The
70.Nm
71utility uses the companion program
72.Xr crunchide 1
73to eliminate link-time conflicts between the component programs by
74hiding all unnecessary symbols.
75.Pp
76The
77.Nm
78utility places specific requirements on package
79.Pa Makefile Ns s
80which make it unsuitable for use with
81.No non- Ns Bx
82sources.
83In particular, the
84.Pa Makefile
85must contain the target
86.Ic depend ,
87and it must define all object files in the variable
88.Va OBJS .
89In some cases, you can use a fake
90.Pa Makefile :
91before looking for
92.Pa Makefile
93in the source directory
94.Pa foo ,
95.Nm
96looks for the file
97.Pa Makefile.foo
98in the current directory.
99.Pp
100After
101.Nm
102is run, the crunched binary can be built by running
103.Dq Li make -f <conf-name>.mk .
104The component programs' object files must already be built.
105An
106.Ic objs
107target, included in the output makefile, will
108run
109.Xr make 1
110in each component program's source dir to build the object
111files for the user.
112This is not done automatically since in release
113engineering circumstances it is generally not desirable to be
114modifying objects in other directories.
115.Pp
116The options are as follows:
117.Bl -tag -width indent
118.It Fl c Ar c-file-name
119Set output C file name to
120.Ar c-file-name .
121The default name is
122.Pa <conf-name>.c .
123.It Fl e Ar exec-file-name
124Set crunched binary executable file name to
125.Ar exec-file-name .
126The default name is
127.Pa <conf-name> .
128.It Fl f
129Flush cache.
130Forces the recalculation of cached parameters.
131.It Fl l
132List names.
133Lists the names this binary will respond to.
134.It Fl h Ar makefile-header-name
135Set the name of a file to be included at the beginning of the
136.Pa Makefile Ns s
137generated by
138.Nm .
139This is useful to define some make variables which might affect the behavior of
140.Xr make 1
141and are annoying to pass through environment variables.
142.It Fl m Ar makefile-name
143Set output
144.Pa Makefile
145name to
146.Ar makefile-name .
147The default name is
148.Pa <conf-name>.mk .
149.It Fl o
150Add
151.Dq Li make obj
152rules to each program make target.
153.It Fl p Ar obj-prefix
154Set the pathname to be prepended to the
155.Ic srcdir
156when computing the
157.Ic objdir .
158If this option is not present, then the prefix used
159is the content of the
160.Ev MAKEOBJDIRPREFIX
161environment variable, or
162.Pa /usr/obj .
163.It Fl q
164Quiet operation.
165Status messages are suppressed.
166.El
167.Sh CRUNCHGEN CONFIGURATION FILE COMMANDS
168The
169.Nm
170utility reads specifications from the
171.Ar conf-file
172that describe the components of the crunched binary.
173In its simplest
174use, the component program names are merely listed along with the
175top-level source directories in which their sources can be found.
176The
177.Nm
178utility then calculates (via the source makefiles) and caches the
179list of object files and their locations.
180For more specialized
181situations, the user can specify by hand all the parameters that
182.Nm
183needs.
184.Pp
185The
186.Ar conf-file
187commands are as follows:
188.Bl -tag -width indent
189.It Ic srcdirs Ar dirname ...
190A list of source trees in which the source directories of the
191component programs can be found.
192These dirs are searched using the
193.Bx
194.Dq Pa <source-dir>/<progname>/
195convention.
196Multiple
197.Ic srcdirs
198lines can be specified.
199The directories are searched in the order they are given.
200.It Ic progs Ar progname ...
201A list of programs that make up the crunched binary.
202Multiple
203.Ic progs
204lines can be specified.
205.It Ic libs Ar libspec ...
206A list of library specifications to be included in the crunched binary link.
207Multiple
208.Ic libs
209lines can be specified.
210.It Ic libs_so Ar libspec ...
211A list of library specifications to be dynamically linked in the
212crunched binary.
213These libraries will need to be made available via the run-time link-editor
214.Xr rtld 1
215when the component program that requires them is executed from
216the crunched binary.
217Multiple
218.Ic libs_so
219lines can be specified.
220The
221.Ic libs_so
222directive overrides a library specified gratuitously on a
223.Ic libs
224line.
225.It Ic buildopts Ar buildopts ...
226A list of build options to be added to every make target.
227.It Ic ln Ar progname linkname
228Causes the crunched binary to invoke
229.Ar progname
230whenever
231.Ar linkname
232appears in
233.Va argv[0] .
234This allows programs that change their behavior when
235run under different names to operate correctly.
236.El
237.Pp
238To handle specialized situations, such as when the source is not
239available or not built via a conventional
240.Pa Makefile ,
241the following
242.Ic special
243commands can be used to set
244.Nm
245parameters for a component program.
246.Bl -tag -width indent
247.It Ic special Ar progname Ic srcdir Ar pathname
248Set the source directory for
249.Ar progname .
250This is normally calculated by searching the specified
251.Ic srcdirs
252for a directory named
253.Ar progname .
254.It Ic special Ar progname Ic objdir Ar pathname
255Set the
256.Pa obj
257directory for
258.Ar progname .
259The
260.Pa obj
261directory is normally calculated by looking for a directory
262whose name is that of the source directory prepended by
263one of the following components, in order of priority:
264the
265.Fl p
266argument passed to the command line; or,
267the value of the
268.Ev MAKEOBJDIRPREFIX
269environment variable, or
270.Pa /usr/obj .
271If the directory is not found, the
272.Ic srcdir
273itself becomes the
274.Ic objdir .
275.It Ic special Ar progname Ic buildopts Ar buildopts
276Define a set of build options that should be added to
277.Xr make 1
278targets in addition to those specified using
279.Ic buildopts
280when processing
281.Ar progname .
282.It Ic special Ar progname Ic objs Ar object-file-name ...
283Set the list of object files for program
284.Ar progname .
285This is normally calculated by constructing a temporary makefile that includes
286.Dq Ic srcdir Ns / Ns Pa Makefile
287and outputs the value of
288.Va $(OBJS) .
289.It Ic special Ar progname Ic objpaths Ar full-pathname-to-object-file ...
290Sets the pathnames of the object files for program
291.Ar progname .
292This is normally calculated by prepending the
293.Ic objdir
294pathname to each file in the
295.Ic objs
296list.
297.It Ic special Ar progname Ic objvar Ar variable_name
298Sets the name of the
299.Xr make 1
300variable which holds the list of
301object files for program
302.Ar progname .
303This is normally
304.Va OBJS
305but some
306.Pa Makefile Ns s
307might like to use other conventions or
308prepend the program's name to the variable, e.g.,
309.Va SSHD_OBJS .
310.It Ic special Ar progname Ic lib Ar library-name ...
311Specifies libraries to be linked with object files to produce
312.Ar progname Ns Pa .lo .
313This can be useful with libraries which redefine routines in
314the standard libraries, or poorly written libraries which
315reference symbols in the object files.
316.It Ic special Ar progname Ic keep Ar symbol-name ...
317Add specified list of symbols to the keep list for program
318.Ar progname .
319An underscore
320.Pq Ql _
321is prepended to each symbol and it becomes the argument to a
322.Fl k
323option for the
324.Xr crunchide 1
325phase.
326This option is to be used as a last resort as its use can cause a
327symbol conflict, however in certain instances it may be the only way to
328have a symbol resolve.
329.It Ic special Ar progname Ic ident Ar identifier
330Set the
331.Pa Makefile Ns / Ns Tn C
332identifier for
333.Ar progname .
334This is normally generated from a
335.Ar progname ,
336mapping
337.Ql -
338to
339.Ql _
340and ignoring all other non-identifier characters.
341This leads to programs named
342.Qq Li foo.bar
343and
344.Qq Li foobar
345to map to the same identifier.
346.El
347.Pp
348Only the
349.Ic objpaths
350parameter is actually needed by
351.Nm ,
352but it is calculated from
353.Ic objdir
354and
355.Ic objs ,
356which are in turn calculated from
357.Ic srcdir ,
358so is sometimes convenient to specify the earlier parameters and let
359.Nm
360calculate forward from there if it can.
361.Pp
362The makefile produced by
363.Nm
364contains an optional
365.Ic objs
366target that will build the object files for each component program by
367running
368.Xr make 1
369inside that program's source directory.
370For this to work the
371.Ic srcdir
372and
373.Ic objs
374parameters must also be valid.
375If they are not valid for a particular program, that
376program is skipped in the
377.Ic objs
378target.
379.Sh EXAMPLES
380Here is an example
381.Nm
382input conf file, named
383.Dq Pa kcopy.conf :
384.Bd -literal -offset indent
385srcdirs /usr/src/bin /usr/src/sbin
386
387progs test cp echo sh fsck halt init mount umount myinstall
388progs anotherprog
389ln test [       # test can be invoked via [
390ln sh -sh       # init invokes the shell with "-sh" in argv[0]
391
392special myprog objpaths /homes/leroy/src/myinstall.o # no sources
393
394special anotherprog -DNO_FOO WITHOUT_BAR=YES
395
396libs -lutil -lcrypt
397.Ed
398.Pp
399This conf file specifies a small crunched binary consisting of some
400basic system utilities plus a homegrown install program
401.Dq Pa myinstall ,
402for which no source directory is specified, but its object file is
403specified directly with the
404.Ic special
405line.
406.Pp
407Additionally when
408.Dq Pa anotherprog
409is built the arguments
410.Pp
411.Dl -DNO_FOO WITHOUT_BAR=YES
412.Pp
413are added to all build targets.
414.Pp
415The crunched binary
416.Dq Pa kcopy
417can be built as follows:
418.Bd -literal -offset indent
419% crunchgen -m Makefile kcopy.conf    # gen Makefile and kcopy.c
420% make objs             # build the component programs' .o files
421% make                  # build the crunched binary kcopy
422% kcopy sh              # test that this invokes a sh shell
423$			# it works!
424.Ed
425.Pp
426At this point the binary
427.Dq Pa kcopy
428can be copied onto an install floppy
429and hard-linked to the names of the component programs.
430.Pp
431Note that if the
432.Ic libs_so
433command had been used, copies of the libraries so named
434would also need to be copied to the install floppy.
435.Sh SEE ALSO
436.Xr crunchide 1 ,
437.Xr make 1 ,
438.Xr rtld 1
439.Sh AUTHORS
440.An -nosplit
441The
442.Nm
443utility was written by
444.An James da Silva Aq Mt jds@cs.umd.edu .
445.Pp
446Copyright (c) 1994 University of Maryland.
447All Rights Reserved.
448.Pp
449The
450.Ic libs_so
451keyword was added in 2005 by
452.An Adrian Steinmann Aq Mt ast@marabu.ch
453and
454.An Ceri Davies Aq Mt ceri@FreeBSD.org .
455.Sh CAVEATS
456While
457.Nm
458takes care to eliminate link conflicts between the component programs
459of a crunched binary, conflicts are still possible between the
460libraries that are linked in.
461Some shuffling in the order of
462libraries may be required, and in some rare cases two libraries may
463have an unresolvable conflict and thus cannot be crunched together.
464.Pp
465Some versions of the
466.Bx
467build environment do not by default build the
468intermediate object file for single-source file programs.
469The
470.Dq Li make objs
471must then be used to get those object files built, or
472some other arrangements made.
473