Index: stable/12/sys/sys/param.h =================================================================== --- stable/12/sys/sys/param.h (revision 365815) +++ stable/12/sys/sys/param.h (revision 365816) @@ -1,365 +1,365 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1982, 1986, 1989, 1993 * The Regents of the University of California. All rights reserved. * (c) UNIX System Laboratories, Inc. * All or some portions of this file are derived from material licensed * to the University of California by American Telephone and Telegraph * Co. or Unix System Laboratories, Inc. and are reproduced herein with * the permission of UNIX System Laboratories, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)param.h 8.3 (Berkeley) 4/4/95 * $FreeBSD$ */ #ifndef _SYS_PARAM_H_ #define _SYS_PARAM_H_ #include #define BSD 199506 /* System version (year & month). */ #define BSD4_3 1 #define BSD4_4 1 /* * __FreeBSD_version numbers are documented in the Porter's Handbook. * If you bump the version for any reason, you should update the documentation * there. * Currently this lives here in the doc/ repository: * * head/en_US.ISO8859-1/books/porters-handbook/versions/chapter.xml * * scheme is: Rxx * 'R' is in the range 0 to 4 if this is a release branch or * X.0-CURRENT before releng/X.0 is created, otherwise 'R' is * in the range 5 to 9. */ #undef __FreeBSD_version -#define __FreeBSD_version 1202501 /* Master, propagated to newvers */ +#define __FreeBSD_version 1202502 /* Master, propagated to newvers */ /* * __FreeBSD_kernel__ indicates that this system uses the kernel of FreeBSD, * which by definition is always true on FreeBSD. This macro is also defined * on other systems that use the kernel of FreeBSD, such as GNU/kFreeBSD. * * It is tempting to use this macro in userland code when we want to enable * kernel-specific routines, and in fact it's fine to do this in code that * is part of FreeBSD itself. However, be aware that as presence of this * macro is still not widespread (e.g. older FreeBSD versions, 3rd party * compilers, etc), it is STRONGLY DISCOURAGED to check for this macro in * external applications without also checking for __FreeBSD__ as an * alternative. */ #undef __FreeBSD_kernel__ #define __FreeBSD_kernel__ #if defined(_KERNEL) || defined(IN_RTLD) #define P_OSREL_SIGWAIT 700000 #define P_OSREL_SIGSEGV 700004 #define P_OSREL_MAP_ANON 800104 #define P_OSREL_MAP_FSTRICT 1100036 #define P_OSREL_SHUTDOWN_ENOTCONN 1100077 #define P_OSREL_MAP_GUARD 1200035 #define P_OSREL_WRFSBASE 1200041 #define P_OSREL_CK_CYLGRP 1200046 #define P_OSREL_VMTOTAL64 1200054 #define P_OSREL_MAJOR(x) ((x) / 100000) #endif #ifndef LOCORE #include #endif /* * Machine-independent constants (some used in following include files). * Redefined constants are from POSIX 1003.1 limits file. * * MAXCOMLEN should be >= sizeof(ac_comm) (see ) */ #include #define MAXCOMLEN 19 /* max command name remembered */ #define MAXINTERP PATH_MAX /* max interpreter file name length */ #define MAXLOGNAME 33 /* max login name length (incl. NUL) */ #define MAXUPRC CHILD_MAX /* max simultaneous processes */ #define NCARGS ARG_MAX /* max bytes for an exec function */ #define NGROUPS (NGROUPS_MAX+1) /* max number groups */ #define NOFILE OPEN_MAX /* max open files per process */ #define NOGROUP 65535 /* marker for empty group set member */ #define MAXHOSTNAMELEN 256 /* max hostname size */ #define SPECNAMELEN 63 /* max length of devicename */ /* More types and definitions used throughout the kernel. */ #ifdef _KERNEL #include #include #ifndef LOCORE #include #include #endif #ifndef FALSE #define FALSE 0 #endif #ifndef TRUE #define TRUE 1 #endif #endif #ifndef _KERNEL /* Signals. */ #include #endif /* Machine type dependent parameters. */ #include #ifndef _KERNEL #include #endif #ifndef DEV_BSHIFT #define DEV_BSHIFT 9 /* log2(DEV_BSIZE) */ #endif #define DEV_BSIZE (1<>PAGE_SHIFT) #endif /* * btodb() is messy and perhaps slow because `bytes' may be an off_t. We * want to shift an unsigned type to avoid sign extension and we don't * want to widen `bytes' unnecessarily. Assume that the result fits in * a daddr_t. */ #ifndef btodb #define btodb(bytes) /* calculates (bytes / DEV_BSIZE) */ \ (sizeof (bytes) > sizeof(long) \ ? (daddr_t)((unsigned long long)(bytes) >> DEV_BSHIFT) \ : (daddr_t)((unsigned long)(bytes) >> DEV_BSHIFT)) #endif #ifndef dbtob #define dbtob(db) /* calculates (db * DEV_BSIZE) */ \ ((off_t)(db) << DEV_BSHIFT) #endif #define PRIMASK 0x0ff #define PCATCH 0x100 /* OR'd with pri for tsleep to check signals */ #define PDROP 0x200 /* OR'd with pri to stop re-entry of interlock mutex */ #define NZERO 0 /* default "nice" */ #define NBBY 8 /* number of bits in a byte */ #define NBPW sizeof(int) /* number of bytes per word (integer) */ #define CMASK 022 /* default file mask: S_IWGRP|S_IWOTH */ #define NODEV (dev_t)(-1) /* non-existent device */ /* * File system parameters and macros. * * MAXBSIZE - Filesystems are made out of blocks of at most MAXBSIZE bytes * per block. MAXBSIZE may be made larger without effecting * any existing filesystems as long as it does not exceed MAXPHYS, * and may be made smaller at the risk of not being able to use * filesystems which require a block size exceeding MAXBSIZE. * * MAXBCACHEBUF - Maximum size of a buffer in the buffer cache. This must * be >= MAXBSIZE and can be set differently for different * architectures by defining it in . * Making this larger allows NFS to do larger reads/writes. * * BKVASIZE - Nominal buffer space per buffer, in bytes. BKVASIZE is the * minimum KVM memory reservation the kernel is willing to make. * Filesystems can of course request smaller chunks. Actual * backing memory uses a chunk size of a page (PAGE_SIZE). * The default value here can be overridden on a per-architecture * basis by defining it in . * * If you make BKVASIZE too small you risk seriously fragmenting * the buffer KVM map which may slow things down a bit. If you * make it too big the kernel will not be able to optimally use * the KVM memory reserved for the buffer cache and will wind * up with too-few buffers. * * The default is 16384, roughly 2x the block size used by a * normal UFS filesystem. */ #define MAXBSIZE 65536 /* must be power of 2 */ #ifndef MAXBCACHEBUF #define MAXBCACHEBUF MAXBSIZE /* must be a power of 2 >= MAXBSIZE */ #endif #ifndef BKVASIZE #define BKVASIZE 16384 /* must be power of 2 */ #endif #define BKVAMASK (BKVASIZE-1) /* * MAXPATHLEN defines the longest permissible path length after expanding * symbolic links. It is used to allocate a temporary buffer from the buffer * pool in which to do the name expansion, hence should be a power of two, * and must be less than or equal to MAXBSIZE. MAXSYMLINKS defines the * maximum number of symbolic links that may be expanded in a path name. * It should be set high enough to allow all legitimate uses, but halt * infinite loops reasonably quickly. */ #define MAXPATHLEN PATH_MAX #define MAXSYMLINKS 32 /* Bit map related macros. */ #define setbit(a,i) (((unsigned char *)(a))[(i)/NBBY] |= 1<<((i)%NBBY)) #define clrbit(a,i) (((unsigned char *)(a))[(i)/NBBY] &= ~(1<<((i)%NBBY))) #define isset(a,i) \ (((const unsigned char *)(a))[(i)/NBBY] & (1<<((i)%NBBY))) #define isclr(a,i) \ ((((const unsigned char *)(a))[(i)/NBBY] & (1<<((i)%NBBY))) == 0) /* Macros for counting and rounding. */ #ifndef howmany #define howmany(x, y) (((x)+((y)-1))/(y)) #endif #define nitems(x) (sizeof((x)) / sizeof((x)[0])) #define rounddown(x, y) (((x)/(y))*(y)) #define rounddown2(x, y) ((x)&(~((y)-1))) /* if y is power of two */ #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */ #define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */ #define powerof2(x) ((((x)-1)&(x))==0) /* Macros for min/max. */ #define MIN(a,b) (((a)<(b))?(a):(b)) #define MAX(a,b) (((a)>(b))?(a):(b)) #ifdef _KERNEL /* * Basic byte order function prototypes for non-inline functions. */ #ifndef LOCORE #ifndef _BYTEORDER_PROTOTYPED #define _BYTEORDER_PROTOTYPED __BEGIN_DECLS __uint32_t htonl(__uint32_t); __uint16_t htons(__uint16_t); __uint32_t ntohl(__uint32_t); __uint16_t ntohs(__uint16_t); __END_DECLS #endif #endif #ifndef _BYTEORDER_FUNC_DEFINED #define _BYTEORDER_FUNC_DEFINED #define htonl(x) __htonl(x) #define htons(x) __htons(x) #define ntohl(x) __ntohl(x) #define ntohs(x) __ntohs(x) #endif /* !_BYTEORDER_FUNC_DEFINED */ #endif /* _KERNEL */ /* * Scale factor for scaled integers used to count %cpu time and load avgs. * * The number of CPU `tick's that map to a unique `%age' can be expressed * by the formula (1 / (2 ^ (FSHIFT - 11))). The maximum load average that * can be calculated (assuming 32 bits) can be closely approximated using * the formula (2 ^ (2 * (16 - FSHIFT))) for (FSHIFT < 15). * * For the scheduler to maintain a 1:1 mapping of CPU `tick' to `%age', * FSHIFT must be at least 11; this gives us a maximum load avg of ~1024. */ #define FSHIFT 11 /* bits to right of fixed binary point */ #define FSCALE (1<> (PAGE_SHIFT - DEV_BSHIFT)) #define ctodb(db) /* calculates pages to devblks */ \ ((db) << (PAGE_SHIFT - DEV_BSHIFT)) /* * Old spelling of __containerof(). */ #define member2struct(s, m, x) \ ((struct s *)(void *)((char *)(x) - offsetof(struct s, m))) /* * Access a variable length array that has been declared as a fixed * length array. */ #define __PAST_END(array, offset) (((__typeof__(*(array)) *)(array))[offset]) #endif /* _SYS_PARAM_H_ */ Index: stable/12/usr.sbin/crunch/crunchgen/crunched_main.c =================================================================== --- stable/12/usr.sbin/crunch/crunchgen/crunched_main.c (revision 365815) +++ stable/12/usr.sbin/crunch/crunchgen/crunched_main.c (revision 365816) @@ -1,116 +1,208 @@ /* * Copyright (c) 1994 University of Maryland * All Rights Reserved. * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting * documentation, and that the name of U.M. not be used in advertising or * publicity pertaining to distribution of the software without specific, * written prior permission. U.M. makes no representations about the * suitability of this software for any purpose. It is provided "as is" * without express or implied warranty. * * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M. * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * Author: James da Silva, Systems Design and Analysis Group * Computer Science Department * University of Maryland at College Park */ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright 2020 Alex Richardson + * + * This software was developed by SRI International and the University of + * Cambridge Computer Laboratory (Department of Computer Science and + * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the + * DARPA SSITH research programme. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ /* * crunched_main.c - main program for crunched binaries, it branches to a * particular subprogram based on the value of argv[0]. Also included * is a little program invoked when the crunched binary is called via * its EXECNAME. This one prints out the list of compiled-in binaries, * or calls one of them based on argv[1]. This allows the testing of * the crunched binary without creating all the links. */ #include __FBSDID("$FreeBSD$"); +#include +#include +#include + +#include #include #include #include +typedef int crunched_stub_t(int, char **, char **); + struct stub { - char *name; - int (*f)(); + const char *name; + crunched_stub_t *f; }; -extern char *__progname; +extern const char *__progname; extern struct stub entry_points[]; static void crunched_usage(void); +crunched_stub_t crunched_main; + +static struct stub * +find_entry_point(const char *basename) +{ + struct stub *ep = NULL; + + for (ep = entry_points; ep->name != NULL; ep++) + if (!strcmp(basename, ep->name)) + break; + + return (ep); +} + +static const char * +get_basename(const char *exe_path) +{ + const char *slash = strrchr(exe_path, '/'); + return (slash ? slash + 1 : exe_path); +} + int main(int argc, char **argv, char **envp) { - char *slash, *basename; - struct stub *ep; + struct stub *ep = NULL; + const char *basename = NULL; - if(argv[0] == NULL || *argv[0] == '\0') - crunched_usage(); + /* + * Look at __progname first (this will be set if the crunched binary is + * invoked directly). + */ + if (__progname) { + basename = get_basename(__progname); + ep = find_entry_point(basename); + } - slash = strrchr(argv[0], '/'); - basename = slash? slash+1 : argv[0]; + /* + * Otherwise try to find entry point based on argv[0] (this works for + * both symlinks as well as hardlinks). However, it does not work when + * su invokes a crunched shell because it sets argv[0] to _su when + * invoking the shell. In that case we look at AT_EXECPATH as a + * fallback. + */ + if (ep == NULL) { + basename = get_basename(argv[0]); + ep = find_entry_point(basename); + } - for(ep=entry_points; ep->name != NULL; ep++) - if(!strcmp(basename, ep->name)) break; + /* + * If we didn't find the entry point based on __progname or argv[0], + * try AT_EXECPATH to get the actual binary that was executed. + */ + if (ep == NULL) { + char buf[MAXPATHLEN]; + int error = elf_aux_info(AT_EXECPATH, &buf, sizeof(buf)); - if(ep->name) - return ep->f(argc, argv, envp); - else { - fprintf(stderr, "%s: %s not compiled in\n", EXECNAME, basename); - crunched_usage(); - } -} + if (error == 0) { + const char *exe_name = get_basename(buf); + /* + * Keep using argv[0] if AT_EXECPATH is the crunched + * binary so that symlinks to the crunched binary report + * "not compiled in" instead of invoking + * crunched_main(). + */ + if (strcmp(exe_name, EXECNAME) != 0) { + basename = exe_name; + ep = find_entry_point(basename); + } + } else { + warnc(error, "elf_aux_info(AT_EXECPATH) failed"); + } + } + if (basename == NULL || *basename == '\0') + crunched_usage(); + if (ep != NULL) { + return ep->f(argc, argv, envp); + } else { + fprintf(stderr, "%s: %s not compiled in\n", EXECNAME, basename); + crunched_usage(); + } +} + int crunched_main(int argc, char **argv, char **envp) { - char *slash; - struct stub *ep; - int columns, len; + if (argc <= 1) + crunched_usage(); - if(argc <= 1) - crunched_usage(); - - slash = strrchr(argv[1], '/'); - __progname = slash? slash+1 : argv[1]; - - return main(--argc, ++argv, envp); + __progname = get_basename(argv[1]); + return main(--argc, ++argv, envp); } - static void crunched_usage() { - int columns, len; - struct stub *ep; + int columns, len; + struct stub *ep; - fprintf(stderr, "usage: %s ..., where is one of:\n", - EXECNAME); - columns = 0; - for(ep=entry_points; ep->name != NULL; ep++) { - len = strlen(ep->name) + 1; - if(columns+len < 80) - columns += len; - else { - fprintf(stderr, "\n"); - columns = len; + fprintf(stderr, + "usage: %s ..., where is one of:\n", EXECNAME); + columns = 0; + for (ep = entry_points; ep->name != NULL; ep++) { + len = strlen(ep->name) + 1; + if (columns + len < 80) + columns += len; + else { + fprintf(stderr, "\n"); + columns = len; + } + fprintf(stderr, " %s", ep->name); } - fprintf(stderr, " %s", ep->name); - } - fprintf(stderr, "\n"); - exit(1); + fprintf(stderr, "\n"); + exit(1); } /* end of crunched_main.c */ Index: stable/12/usr.sbin/crunch/crunchgen/crunchgen.1 =================================================================== --- stable/12/usr.sbin/crunch/crunchgen/crunchgen.1 (revision 365815) +++ stable/12/usr.sbin/crunch/crunchgen/crunchgen.1 (revision 365816) @@ -1,475 +1,473 @@ .\" .\" Copyright (c) 1994 University of Maryland .\" All Rights Reserved. .\" .\" Permission to use, copy, modify, distribute, and sell this software and its .\" documentation for any purpose is hereby granted without fee, provided that .\" the above copyright notice appear in all copies and that both that .\" copyright notice and this permission notice appear in supporting .\" documentation, and that the name of U.M. not be used in advertising or .\" publicity pertaining to distribution of the software without specific, .\" written prior permission. U.M. makes no representations about the .\" suitability of this software for any purpose. It is provided "as is" .\" without express or implied warranty. .\" .\" U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M. .\" BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR .\" IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" .\" Author: James da Silva, Systems Design and Analysis Group .\" Computer Science Department .\" University of Maryland at College Park .\" $FreeBSD$ .\" .Dd January 6, 2017 .Dt CRUNCHGEN 1 .Os .Sh NAME .Nm crunchgen .Nd generates build environment for a crunched binary .Sh SYNOPSIS .Bk -words .Nm .Op Fl foql .Op Fl h Ar makefile-header-name .Op Fl m Ar makefile-name .Op Fl p Ar obj-prefix .Op Fl c Ar c-file-name .Op Fl e Ar exec-file-name .Ar conf-file .Ek .Sh DESCRIPTION A crunched binary is a program made up of many other programs linked together into a single executable. The crunched binary .Fn main function determines which component program to run by the contents of .Va argv[0] . The main reason to crunch programs together is for fitting as many programs as possible onto an installation or system recovery floppy. .Pp The .Nm utility reads in the specifications in .Ar conf-file for a crunched binary, and generates a .Pa Makefile and accompanying top-level C source file that when built creates the crunched executable file from the component programs. For each component program, .Nm can optionally attempt to determine the object (.o) files that make up the program from its source directory .Pa Makefile . This information is cached between runs. The .Nm utility uses the companion program .Xr crunchide 1 to eliminate link-time conflicts between the component programs by hiding all unnecessary symbols. .Pp The .Nm utility places specific requirements on package .Pa Makefile Ns s which make it unsuitable for use with .No non- Ns Bx sources. In particular, the .Pa Makefile must contain the target .Ic depend , and it must define all object files in the variable .Va OBJS . In some cases, you can use a fake .Pa Makefile : before looking for .Pa Makefile in the source directory .Pa foo , .Nm looks for the file .Pa Makefile.foo in the current directory. .Pp After .Nm is run, the crunched binary can be built by running .Dq Li make -f .mk . The component programs' object files must already be built. An .Ic objs target, included in the output makefile, will run .Xr make 1 in each component program's source dir to build the object files for the user. This is not done automatically since in release engineering circumstances it is generally not desirable to be modifying objects in other directories. .Pp The options are as follows: .Bl -tag -width indent .It Fl c Ar c-file-name Set output C file name to .Ar c-file-name . The default name is .Pa .c . .It Fl e Ar exec-file-name Set crunched binary executable file name to .Ar exec-file-name . The default name is .Pa . .It Fl f Flush cache. Forces the recalculation of cached parameters. .It Fl l List names. Lists the names this binary will respond to. .It Fl h Ar makefile-header-name Set the name of a file to be included at the beginning of the .Pa Makefile Ns s generated by .Nm . -This is useful to define some make variables such as -.Va RELEASE_CRUNCH -or similar, which might affect the behavior of +This is useful to define some make variables which might affect the behavior of .Xr make 1 and are annoying to pass through environment variables. .It Fl m Ar makefile-name Set output .Pa Makefile name to .Ar makefile-name . The default name is .Pa .mk . .It Fl o Add .Dq Li make obj rules to each program make target. .It Fl p Ar obj-prefix Set the pathname to be prepended to the .Ic srcdir when computing the .Ic objdir . If this option is not present, then the prefix used is the content of the .Ev MAKEOBJDIRPREFIX environment variable, or .Pa /usr/obj . .It Fl q Quiet operation. Status messages are suppressed. .El .Sh CRUNCHGEN CONFIGURATION FILE COMMANDS The .Nm utility reads specifications from the .Ar conf-file that describe the components of the crunched binary. In its simplest use, the component program names are merely listed along with the top-level source directories in which their sources can be found. The .Nm utility then calculates (via the source makefiles) and caches the list of object files and their locations. For more specialized situations, the user can specify by hand all the parameters that .Nm needs. .Pp The .Ar conf-file commands are as follows: .Bl -tag -width indent .It Ic srcdirs Ar dirname ... A list of source trees in which the source directories of the component programs can be found. These dirs are searched using the .Bx .Dq Pa // convention. Multiple .Ic srcdirs lines can be specified. The directories are searched in the order they are given. .It Ic progs Ar progname ... A list of programs that make up the crunched binary. Multiple .Ic progs lines can be specified. .It Ic libs Ar libspec ... A list of library specifications to be included in the crunched binary link. Multiple .Ic libs lines can be specified. .It Ic libs_so Ar libspec ... A list of library specifications to be dynamically linked in the crunched binary. These libraries will need to be made available via the run-time link-editor .Xr rtld 1 when the component program that requires them is executed from the crunched binary. Multiple .Ic libs_so lines can be specified. The .Ic libs_so directive overrides a library specified gratuitously on a .Ic libs line. .It Ic buildopts Ar buildopts ... A list of build options to be added to every make target. .It Ic ln Ar progname linkname Causes the crunched binary to invoke .Ar progname whenever .Ar linkname appears in .Va argv[0] . This allows programs that change their behavior when run under different names to operate correctly. .El .Pp To handle specialized situations, such as when the source is not available or not built via a conventional .Pa Makefile , the following .Ic special commands can be used to set .Nm parameters for a component program. .Bl -tag -width indent .It Ic special Ar progname Ic srcdir Ar pathname Set the source directory for .Ar progname . This is normally calculated by searching the specified .Ic srcdirs for a directory named .Ar progname . .It Ic special Ar progname Ic objdir Ar pathname Set the .Pa obj directory for .Ar progname . The .Pa obj directory is normally calculated by looking for a directory whose name is that of the source directory prepended by one of the following components, in order of priority: the .Fl p argument passed to the command line; or, the value of the .Ev MAKEOBJDIRPREFIX environment variable, or .Pa /usr/obj . If the directory is not found, the .Ic srcdir itself becomes the .Ic objdir . .It Ic special Ar progname Ic buildopts Ar buildopts Define a set of build options that should be added to .Xr make 1 targets in addition to those specified using .Ic buildopts when processing .Ar progname . .It Ic special Ar progname Ic objs Ar object-file-name ... Set the list of object files for program .Ar progname . This is normally calculated by constructing a temporary makefile that includes .Dq Ic srcdir Ns / Ns Pa Makefile and outputs the value of .Va $(OBJS) . .It Ic special Ar progname Ic objpaths Ar full-pathname-to-object-file ... Sets the pathnames of the object files for program .Ar progname . This is normally calculated by prepending the .Ic objdir pathname to each file in the .Ic objs list. .It Ic special Ar progname Ic objvar Ar variable_name Sets the name of the .Xr make 1 variable which holds the list of object files for program .Ar progname . This is normally .Va OBJS but some .Pa Makefile Ns s might like to use other conventions or prepend the program's name to the variable, e.g., .Va SSHD_OBJS . .It Ic special Ar progname Ic lib Ar library-name ... Specifies libraries to be linked with object files to produce .Ar progname Ns Pa .lo . This can be useful with libraries which redefine routines in the standard libraries, or poorly written libraries which reference symbols in the object files. .It Ic special Ar progname Ic keep Ar symbol-name ... Add specified list of symbols to the keep list for program .Ar progname . An underscore .Pq Ql _ is prepended to each symbol and it becomes the argument to a .Fl k option for the .Xr crunchide 1 phase. This option is to be used as a last resort as its use can cause a symbol conflict, however in certain instances it may be the only way to have a symbol resolve. .It Ic special Ar progname Ic ident Ar identifier Set the .Pa Makefile Ns / Ns Tn C identifier for .Ar progname . This is normally generated from a .Ar progname , mapping .Ql - to .Ql _ and ignoring all other non-identifier characters. This leads to programs named .Qq Li foo.bar and .Qq Li foobar to map to the same identifier. .El .Pp Only the .Ic objpaths parameter is actually needed by .Nm , but it is calculated from .Ic objdir and .Ic objs , which are in turn calculated from .Ic srcdir , so is sometimes convenient to specify the earlier parameters and let .Nm calculate forward from there if it can. .Pp The makefile produced by .Nm contains an optional .Ic objs target that will build the object files for each component program by running .Xr make 1 inside that program's source directory. For this to work the .Ic srcdir and .Ic objs parameters must also be valid. If they are not valid for a particular program, that program is skipped in the .Ic objs target. .Sh EXAMPLES Here is an example .Nm input conf file, named .Dq Pa kcopy.conf : .Bd -literal -offset indent srcdirs /usr/src/bin /usr/src/sbin progs test cp echo sh fsck halt init mount umount myinstall progs anotherprog ln test [ # test can be invoked via [ ln sh -sh # init invokes the shell with "-sh" in argv[0] special myprog objpaths /homes/leroy/src/myinstall.o # no sources special anotherprog -DNO_FOO WITHOUT_BAR=YES libs -lutil -lcrypt .Ed .Pp This conf file specifies a small crunched binary consisting of some basic system utilities plus a homegrown install program .Dq Pa myinstall , for which no source directory is specified, but its object file is specified directly with the .Ic special line. .Pp Additionally when .Dq Pa anotherprog is built the arguments .Pp .Dl -DNO_FOO WITHOUT_BAR=YES .Pp are added to all build targets. .Pp The crunched binary .Dq Pa kcopy can be built as follows: .Bd -literal -offset indent % crunchgen -m Makefile kcopy.conf # gen Makefile and kcopy.c % make objs # build the component programs' .o files % make # build the crunched binary kcopy % kcopy sh # test that this invokes a sh shell $ # it works! .Ed .Pp At this point the binary .Dq Pa kcopy can be copied onto an install floppy and hard-linked to the names of the component programs. .Pp Note that if the .Ic libs_so command had been used, copies of the libraries so named would also need to be copied to the install floppy. .Sh SEE ALSO .Xr crunchide 1 , .Xr make 1 , .Xr rtld 1 .Sh AUTHORS .An -nosplit The .Nm utility was written by .An James da Silva Aq Mt jds@cs.umd.edu . .Pp Copyright (c) 1994 University of Maryland. All Rights Reserved. .Pp The .Ic libs_so keyword was added in 2005 by .An Adrian Steinmann Aq Mt ast@marabu.ch and .An Ceri Davies Aq Mt ceri@FreeBSD.org . .Sh CAVEATS While .Nm takes care to eliminate link conflicts between the component programs of a crunched binary, conflicts are still possible between the libraries that are linked in. Some shuffling in the order of libraries may be required, and in some rare cases two libraries may have an unresolvable conflict and thus cannot be crunched together. .Pp Some versions of the .Bx build environment do not by default build the intermediate object file for single-source file programs. The .Dq Li make objs must then be used to get those object files built, or some other arrangements made. Index: stable/12/usr.sbin/crunch/crunchgen/crunchgen.c =================================================================== --- stable/12/usr.sbin/crunch/crunchgen/crunchgen.c (revision 365815) +++ stable/12/usr.sbin/crunch/crunchgen/crunchgen.c (revision 365816) @@ -1,1241 +1,1260 @@ /* * Copyright (c) 1994 University of Maryland * All Rights Reserved. * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting * documentation, and that the name of U.M. not be used in advertising or * publicity pertaining to distribution of the software without specific, * written prior permission. U.M. makes no representations about the * suitability of this software for any purpose. It is provided "as is" * without express or implied warranty. * * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M. * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * Author: James da Silva, Systems Design and Analysis Group * Computer Science Department * University of Maryland at College Park */ /* * ======================================================================== * crunchgen.c * * Generates a Makefile and main C file for a crunched executable, * from specs given in a .conf file. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include +#include #include +#include #include #include #include +#include #include #define CRUNCH_VERSION "0.2" #define MAXLINELEN 16384 #define MAXFIELDS 2048 /* internal representation of conf file: */ /* simple lists of strings suffice for most parms */ typedef struct strlst { struct strlst *next; char *str; } strlst_t; /* progs have structure, each field can be set with "special" or calculated */ typedef struct prog { struct prog *next; /* link field */ char *name; /* program name */ char *ident; /* C identifier for the program name */ char *srcdir; char *realsrcdir; char *objdir; char *objvar; /* Makefile variable to replace OBJS */ strlst_t *objs, *objpaths; strlst_t *buildopts; strlst_t *keeplist; strlst_t *links; strlst_t *libs; strlst_t *libs_so; int goterror; } prog_t; /* global state */ strlst_t *buildopts = NULL; strlst_t *srcdirs = NULL; strlst_t *libs = NULL; strlst_t *libs_so = NULL; prog_t *progs = NULL; char confname[MAXPATHLEN], infilename[MAXPATHLEN]; char outmkname[MAXPATHLEN], outcfname[MAXPATHLEN], execfname[MAXPATHLEN]; char tempfname[MAXPATHLEN], cachename[MAXPATHLEN], curfilename[MAXPATHLEN]; +bool tempfname_initialized = false; char outhdrname[MAXPATHLEN] ; /* user-supplied header for *.mk */ char *objprefix; /* where are the objects ? */ char *path_make; int linenum = -1; int goterror = 0; int verbose, readcache; /* options */ int reading_cache; int makeobj = 0; /* add 'make obj' rules to the makefile */ int list_mode; /* general library routines */ void status(const char *str); void out_of_memory(void); void add_string(strlst_t **listp, char *str); int is_dir(const char *pathname); int is_nonempty_file(const char *pathname); int subtract_strlst(strlst_t **lista, strlst_t **listb); int in_list(strlst_t **listp, char *str); /* helper routines for main() */ void usage(void); void parse_conf_file(void); void gen_outputs(void); extern char *crunched_skel[]; int main(int argc, char **argv) { char *p; int optc; verbose = 1; readcache = 1; *outmkname = *outcfname = *execfname = '\0'; path_make = getenv("MAKE"); if (path_make == NULL || *path_make == '\0') path_make = "make"; p = getenv("MAKEOBJDIRPREFIX"); if (p == NULL || *p == '\0') objprefix = "/usr/obj"; /* default */ else if ((objprefix = strdup(p)) == NULL) out_of_memory(); while((optc = getopt(argc, argv, "lh:m:c:e:p:foq")) != -1) { switch(optc) { case 'f': readcache = 0; break; case 'o': makeobj = 1; break; case 'q': verbose = 0; break; case 'm': strlcpy(outmkname, optarg, sizeof(outmkname)); break; case 'p': if ((objprefix = strdup(optarg)) == NULL) out_of_memory(); break; case 'h': strlcpy(outhdrname, optarg, sizeof(outhdrname)); break; case 'c': strlcpy(outcfname, optarg, sizeof(outcfname)); break; case 'e': strlcpy(execfname, optarg, sizeof(execfname)); break; case 'l': list_mode++; verbose = 0; break; case '?': default: usage(); } } argc -= optind; argv += optind; if (argc != 1) usage(); /* * generate filenames */ strlcpy(infilename, argv[0], sizeof(infilename)); /* confname = `basename infilename .conf` */ if ((p=strrchr(infilename, '/')) != NULL) strlcpy(confname, p + 1, sizeof(confname)); else strlcpy(confname, infilename, sizeof(confname)); if ((p=strrchr(confname, '.')) != NULL && !strcmp(p, ".conf")) *p = '\0'; if (!*outmkname) snprintf(outmkname, sizeof(outmkname), "%s.mk", confname); if (!*outcfname) snprintf(outcfname, sizeof(outcfname), "%s.c", confname); if (!*execfname) snprintf(execfname, sizeof(execfname), "%s", confname); snprintf(cachename, sizeof(cachename), "%s.cache", confname); snprintf(tempfname, sizeof(tempfname), "%s/crunchgen_%sXXXXXX", getenv("TMPDIR") ? getenv("TMPDIR") : _PATH_TMP, confname); + tempfname_initialized = false; parse_conf_file(); if (list_mode) exit(goterror); gen_outputs(); exit(goterror); } void usage(void) { fprintf(stderr, "%s%s\n\t%s%s\n", "usage: crunchgen [-foq] ", "[-h ] [-m ]", "[-p ] [-c ] [-e ] ", ""); exit(1); } /* * ======================================================================== * parse_conf_file subsystem * */ /* helper routines for parse_conf_file */ void parse_one_file(char *filename); void parse_line(char *pline, int *fc, char **fv, int nf); void add_srcdirs(int argc, char **argv); void add_progs(int argc, char **argv); void add_link(int argc, char **argv); void add_libs(int argc, char **argv); void add_libs_so(int argc, char **argv); void add_buildopts(int argc, char **argv); void add_special(int argc, char **argv); prog_t *find_prog(char *str); void add_prog(char *progname); void parse_conf_file(void) { if (!is_nonempty_file(infilename)) errx(1, "fatal: input file \"%s\" not found", infilename); parse_one_file(infilename); if (readcache && is_nonempty_file(cachename)) { reading_cache = 1; parse_one_file(cachename); } } void parse_one_file(char *filename) { char *fieldv[MAXFIELDS]; int fieldc; void (*f)(int c, char **v); FILE *cf; char line[MAXLINELEN]; snprintf(line, sizeof(line), "reading %s", filename); status(line); strlcpy(curfilename, filename, sizeof(curfilename)); if ((cf = fopen(curfilename, "r")) == NULL) { warn("%s", curfilename); goterror = 1; return; } linenum = 0; while (fgets(line, MAXLINELEN, cf) != NULL) { linenum++; parse_line(line, &fieldc, fieldv, MAXFIELDS); if (fieldc < 1) continue; if (!strcmp(fieldv[0], "srcdirs")) f = add_srcdirs; else if(!strcmp(fieldv[0], "progs")) f = add_progs; else if(!strcmp(fieldv[0], "ln")) f = add_link; else if(!strcmp(fieldv[0], "libs")) f = add_libs; else if(!strcmp(fieldv[0], "libs_so")) f = add_libs_so; else if(!strcmp(fieldv[0], "buildopts")) f = add_buildopts; else if(!strcmp(fieldv[0], "special")) f = add_special; else { warnx("%s:%d: skipping unknown command `%s'", curfilename, linenum, fieldv[0]); goterror = 1; continue; } if (fieldc < 2) { warnx("%s:%d: %s %s", curfilename, linenum, fieldv[0], "command needs at least 1 argument, skipping"); goterror = 1; continue; } f(fieldc, fieldv); } if (ferror(cf)) { warn("%s", curfilename); goterror = 1; } fclose(cf); } void parse_line(char *pline, int *fc, char **fv, int nf) { char *p; p = pline; *fc = 0; while (1) { while (isspace((unsigned char)*p)) p++; if (*p == '\0' || *p == '#') break; if (*fc < nf) fv[(*fc)++] = p; while (*p && !isspace((unsigned char)*p) && *p != '#') p++; if (*p == '\0' || *p == '#') break; *p++ = '\0'; } if (*p) *p = '\0'; /* needed for '#' case */ } void add_srcdirs(int argc, char **argv) { int i; for (i = 1; i < argc; i++) { if (is_dir(argv[i])) add_string(&srcdirs, argv[i]); else { warnx("%s:%d: `%s' is not a directory, skipping it", curfilename, linenum, argv[i]); goterror = 1; } } } void add_progs(int argc, char **argv) { int i; for (i = 1; i < argc; i++) add_prog(argv[i]); } void add_prog(char *progname) { prog_t *p1, *p2; /* add to end, but be smart about dups */ for (p1 = NULL, p2 = progs; p2 != NULL; p1 = p2, p2 = p2->next) if (!strcmp(p2->name, progname)) return; p2 = malloc(sizeof(prog_t)); if(p2) { memset(p2, 0, sizeof(prog_t)); p2->name = strdup(progname); } if (!p2 || !p2->name) out_of_memory(); p2->next = NULL; if (p1 == NULL) progs = p2; else p1->next = p2; p2->ident = NULL; p2->srcdir = NULL; p2->realsrcdir = NULL; p2->objdir = NULL; p2->links = NULL; p2->libs = NULL; p2->libs_so = NULL; p2->objs = NULL; p2->keeplist = NULL; p2->buildopts = NULL; p2->goterror = 0; if (list_mode) printf("%s\n",progname); } void add_link(int argc, char **argv) { int i; prog_t *p = find_prog(argv[1]); if (p == NULL) { warnx("%s:%d: no prog %s previously declared, skipping link", curfilename, linenum, argv[1]); goterror = 1; return; } for (i = 2; i < argc; i++) { if (list_mode) printf("%s\n",argv[i]); add_string(&p->links, argv[i]); } } void add_libs(int argc, char **argv) { int i; for(i = 1; i < argc; i++) { add_string(&libs, argv[i]); if ( in_list(&libs_so, argv[i]) ) warnx("%s:%d: " "library `%s' specified as dynamic earlier", curfilename, linenum, argv[i]); } } void add_libs_so(int argc, char **argv) { int i; for(i = 1; i < argc; i++) { add_string(&libs_so, argv[i]); if ( in_list(&libs, argv[i]) ) warnx("%s:%d: " "library `%s' specified as static earlier", curfilename, linenum, argv[i]); } } void add_buildopts(int argc, char **argv) { int i; for (i = 1; i < argc; i++) add_string(&buildopts, argv[i]); } void add_special(int argc, char **argv) { int i; prog_t *p = find_prog(argv[1]); if (p == NULL) { if (reading_cache) return; warnx("%s:%d: no prog %s previously declared, skipping special", curfilename, linenum, argv[1]); goterror = 1; return; } if (!strcmp(argv[2], "ident")) { if (argc != 4) goto argcount; if ((p->ident = strdup(argv[3])) == NULL) out_of_memory(); } else if (!strcmp(argv[2], "srcdir")) { if (argc != 4) goto argcount; if ((p->srcdir = strdup(argv[3])) == NULL) out_of_memory(); } else if (!strcmp(argv[2], "objdir")) { if(argc != 4) goto argcount; if((p->objdir = strdup(argv[3])) == NULL) out_of_memory(); } else if (!strcmp(argv[2], "objs")) { p->objs = NULL; for (i = 3; i < argc; i++) add_string(&p->objs, argv[i]); } else if (!strcmp(argv[2], "objpaths")) { p->objpaths = NULL; for (i = 3; i < argc; i++) add_string(&p->objpaths, argv[i]); } else if (!strcmp(argv[2], "keep")) { p->keeplist = NULL; for(i = 3; i < argc; i++) add_string(&p->keeplist, argv[i]); } else if (!strcmp(argv[2], "objvar")) { if(argc != 4) goto argcount; if ((p->objvar = strdup(argv[3])) == NULL) out_of_memory(); } else if (!strcmp(argv[2], "buildopts")) { p->buildopts = NULL; for (i = 3; i < argc; i++) add_string(&p->buildopts, argv[i]); } else if (!strcmp(argv[2], "lib")) { for (i = 3; i < argc; i++) add_string(&p->libs, argv[i]); } else { warnx("%s:%d: bad parameter name `%s', skipping line", curfilename, linenum, argv[2]); goterror = 1; } return; argcount: warnx("%s:%d: too %s arguments, expected \"special %s %s \"", curfilename, linenum, argc < 4? "few" : "many", argv[1], argv[2]); goterror = 1; } prog_t *find_prog(char *str) { prog_t *p; for (p = progs; p != NULL; p = p->next) if (!strcmp(p->name, str)) return p; return NULL; } /* * ======================================================================== * gen_outputs subsystem * */ /* helper subroutines */ void remove_error_progs(void); void fillin_program(prog_t *p); void gen_specials_cache(void); void gen_output_makefile(void); void gen_output_cfile(void); void fillin_program_objs(prog_t *p, char *path); void top_makefile_rules(FILE *outmk); void prog_makefile_rules(FILE *outmk, prog_t *p); void output_strlst(FILE *outf, strlst_t *lst); char *genident(char *str); char *dir_search(char *progname); void gen_outputs(void) { prog_t *p; for (p = progs; p != NULL; p = p->next) fillin_program(p); remove_error_progs(); gen_specials_cache(); gen_output_cfile(); gen_output_makefile(); status(""); fprintf(stderr, "Run \"%s -f %s\" to build crunched binary.\n", path_make, outmkname); } /* * run the makefile for the program to find which objects are necessary */ void fillin_program(prog_t *p) { char path[MAXPATHLEN]; char line[MAXLINELEN]; FILE *f; snprintf(line, MAXLINELEN, "filling in parms for %s", p->name); status(line); if (!p->ident) p->ident = genident(p->name); /* look for the source directory if one wasn't specified by a special */ if (!p->srcdir) { p->srcdir = dir_search(p->name); } /* Determine the actual srcdir (maybe symlinked). */ if (p->srcdir) { - snprintf(line, MAXLINELEN, "cd %s && echo -n `/bin/pwd`", - p->srcdir); + snprintf(line, MAXLINELEN, "cd %s && pwd -P", p->srcdir); f = popen(line,"r"); if (!f) errx(1, "Can't execute: %s\n", line); path[0] = '\0'; fgets(path, sizeof path, f); if (pclose(f)) errx(1, "Can't execute: %s\n", line); if (!*path) errx(1, "Can't perform pwd on: %s\n", p->srcdir); p->realsrcdir = strdup(path); } /* Unless the option to make object files was specified the * the objects will be built in the source directory unless * an object directory already exists. */ if (!makeobj && !p->objdir && p->srcdir) { char *auto_obj; auto_obj = NULL; snprintf(line, sizeof line, "%s/%s", objprefix, p->realsrcdir); if (is_dir(line) || ((auto_obj = getenv("MK_AUTO_OBJ")) != NULL && strcmp(auto_obj, "yes") == 0)) { if ((p->objdir = strdup(line)) == NULL) out_of_memory(); } else p->objdir = p->realsrcdir; } /* * XXX look for a Makefile.{name} in local directory first. * This lets us override the original Makefile. */ snprintf(path, sizeof(path), "Makefile.%s", p->name); if (is_nonempty_file(path)) { snprintf(line, MAXLINELEN, "Using %s for %s", path, p->name); status(line); } else if (p->srcdir) snprintf(path, sizeof(path), "%s/Makefile", p->srcdir); if (!p->objs && p->srcdir && is_nonempty_file(path)) fillin_program_objs(p, path); if (!p->srcdir && !p->objdir && verbose) warnx("%s: %s: %s", "warning: could not find source directory", infilename, p->name); if (!p->objs && verbose) warnx("%s: %s: warning: could not find any .o files", infilename, p->name); if ((!p->srcdir || !p->objdir) && !p->objs) p->goterror = 1; } void fillin_program_objs(prog_t *p, char *path) { char *obj, *cp; int fd, rc; FILE *f; char *objvar="OBJS"; strlst_t *s; char line[MAXLINELEN]; /* discover the objs from the srcdir Makefile */ - if ((fd = mkstemp(tempfname)) == -1) { - perror(tempfname); - exit(1); + /* + * We reuse the same temporary file name for multiple objects. However, + * some libc implementations (such as glibc) return EINVAL if there + * are no XXXXX characters in the template. This happens after the + * first call to mkstemp since the argument is modified in-place. + * To avoid this error we use open() instead of mkstemp() after the + * call to mkstemp(). + */ + if (tempfname_initialized) { + if ((fd = open(tempfname, O_CREAT | O_EXCL | O_RDWR, 0600)) == -1) { + err(EX_OSERR, "open(%s)", tempfname); + } + } else if ((fd = mkstemp(tempfname)) == -1) { + err(EX_OSERR, "mkstemp(%s)", tempfname); } + tempfname_initialized = true; if ((f = fdopen(fd, "w")) == NULL) { - warn("%s", tempfname); + warn("fdopen(%s)", tempfname); goterror = 1; - return; + goto out; } if (p->objvar) objvar = p->objvar; /* * XXX include outhdrname (e.g. to contain Make variables) */ if (outhdrname[0] != '\0') fprintf(f, ".include \"%s\"\n", outhdrname); fprintf(f, ".include \"%s\"\n", path); fprintf(f, ".POSIX:\n"); if (buildopts) { fprintf(f, "BUILDOPTS+="); output_strlst(f, buildopts); } fprintf(f, ".if defined(PROG)\n"); fprintf(f, "%s?=${PROG}.o\n", objvar); fprintf(f, ".endif\n"); fprintf(f, "loop:\n\t@echo 'OBJS= '${%s}\n", objvar); fprintf(f, "crunchgen_objs:\n" "\t@cd %s && %s -f %s $(BUILDOPTS) $(%s_OPTS)", p->srcdir, path_make, tempfname, p->ident); for (s = p->buildopts; s != NULL; s = s->next) fprintf(f, " %s", s->str); fprintf(f, " loop\n"); fclose(f); snprintf(line, MAXLINELEN, "cd %s && %s -f %s -B crunchgen_objs", p->srcdir, path_make, tempfname); if ((f = popen(line, "r")) == NULL) { warn("submake pipe"); goterror = 1; - return; + goto out; } while(fgets(line, MAXLINELEN, f)) { if (strncmp(line, "OBJS= ", 6)) { warnx("make error: %s", line); goterror = 1; - continue; + goto out; } cp = line + 6; while (isspace((unsigned char)*cp)) cp++; while(*cp) { obj = cp; while (*cp && !isspace((unsigned char)*cp)) cp++; if (*cp) *cp++ = '\0'; add_string(&p->objs, obj); while (isspace((unsigned char)*cp)) cp++; } } if ((rc=pclose(f)) != 0) { warnx("make error: make returned %d", rc); goterror = 1; } - +out: unlink(tempfname); } void remove_error_progs(void) { prog_t *p1, *p2; p1 = NULL; p2 = progs; while (p2 != NULL) { if (!p2->goterror) p1 = p2, p2 = p2->next; else { /* delete it from linked list */ warnx("%s: %s: ignoring program because of errors", infilename, p2->name); if (p1) p1->next = p2->next; else progs = p2->next; p2 = p2->next; } } } void gen_specials_cache(void) { FILE *cachef; prog_t *p; char line[MAXLINELEN]; snprintf(line, MAXLINELEN, "generating %s", cachename); status(line); if ((cachef = fopen(cachename, "w")) == NULL) { warn("%s", cachename); goterror = 1; return; } fprintf(cachef, "# %s - parm cache generated from %s by crunchgen " " %s\n\n", cachename, infilename, CRUNCH_VERSION); for (p = progs; p != NULL; p = p->next) { fprintf(cachef, "\n"); if (p->srcdir) fprintf(cachef, "special %s srcdir %s\n", p->name, p->srcdir); if (p->objdir) fprintf(cachef, "special %s objdir %s\n", p->name, p->objdir); if (p->objs) { fprintf(cachef, "special %s objs", p->name); output_strlst(cachef, p->objs); } if (p->objpaths) { fprintf(cachef, "special %s objpaths", p->name); output_strlst(cachef, p->objpaths); } } fclose(cachef); } void gen_output_makefile(void) { prog_t *p; FILE *outmk; char line[MAXLINELEN]; snprintf(line, MAXLINELEN, "generating %s", outmkname); status(line); if ((outmk = fopen(outmkname, "w")) == NULL) { warn("%s", outmkname); goterror = 1; return; } fprintf(outmk, "# %s - generated from %s by crunchgen %s\n\n", outmkname, infilename, CRUNCH_VERSION); if (outhdrname[0] != '\0') fprintf(outmk, ".include \"%s\"\n", outhdrname); top_makefile_rules(outmk); for (p = progs; p != NULL; p = p->next) prog_makefile_rules(outmk, p); fprintf(outmk, "\n# ========\n"); fclose(outmk); } void gen_output_cfile(void) { char **cp; FILE *outcf; prog_t *p; strlst_t *s; char line[MAXLINELEN]; snprintf(line, MAXLINELEN, "generating %s", outcfname); status(line); if((outcf = fopen(outcfname, "w")) == NULL) { warn("%s", outcfname); goterror = 1; return; } fprintf(outcf, "/* %s - generated from %s by crunchgen %s */\n", outcfname, infilename, CRUNCH_VERSION); fprintf(outcf, "#define EXECNAME \"%s\"\n", execfname); for (cp = crunched_skel; *cp != NULL; cp++) fprintf(outcf, "%s\n", *cp); for (p = progs; p != NULL; p = p->next) - fprintf(outcf, "extern int _crunched_%s_stub();\n", p->ident); + fprintf(outcf, + "extern crunched_stub_t _crunched_%s_stub;\n", + p->ident); fprintf(outcf, "\nstruct stub entry_points[] = {\n"); for (p = progs; p != NULL; p = p->next) { fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n", p->name, p->ident); for (s = p->links; s != NULL; s = s->next) fprintf(outcf, "\t{ \"%s\", _crunched_%s_stub },\n", s->str, p->ident); } fprintf(outcf, "\t{ EXECNAME, crunched_main },\n"); fprintf(outcf, "\t{ NULL, NULL }\n};\n"); fclose(outcf); } char *genident(char *str) { char *n, *s, *d; /* * generates a Makefile/C identifier from a program name, * mapping '-' to '_' and ignoring all other non-identifier * characters. This leads to programs named "foo.bar" and * "foobar" to map to the same identifier. */ if ((n = strdup(str)) == NULL) return NULL; for (d = s = n; *s != '\0'; s++) { if (*s == '-') *d++ = '_'; else if (*s == '_' || isalnum((unsigned char)*s)) *d++ = *s; } *d = '\0'; return n; } char *dir_search(char *progname) { char path[MAXPATHLEN]; strlst_t *dir; char *srcdir; for (dir = srcdirs; dir != NULL; dir = dir->next) { snprintf(path, MAXPATHLEN, "%s/%s", dir->str, progname); if (!is_dir(path)) continue; if ((srcdir = strdup(path)) == NULL) out_of_memory(); return srcdir; } return NULL; } void top_makefile_rules(FILE *outmk) { prog_t *p; fprintf(outmk, "LD?= ld\n"); if ( subtract_strlst(&libs, &libs_so) ) fprintf(outmk, "# NOTE: Some LIBS declarations below overridden by LIBS_SO\n"); fprintf(outmk, "LIBS+="); output_strlst(outmk, libs); fprintf(outmk, "LIBS_SO+="); output_strlst(outmk, libs_so); if (makeobj) { fprintf(outmk, "MAKEOBJDIRPREFIX?=%s\n", objprefix); fprintf(outmk, "MAKEENV=env MAKEOBJDIRPREFIX=$(MAKEOBJDIRPREFIX)\n"); fprintf(outmk, "CRUNCHMAKE=$(MAKEENV) $(MAKE)\n"); } else { fprintf(outmk, "CRUNCHMAKE=$(MAKE)\n"); } if (buildopts) { fprintf(outmk, "BUILDOPTS+="); output_strlst(outmk, buildopts); } fprintf(outmk, "CRUNCHED_OBJS="); for (p = progs; p != NULL; p = p->next) fprintf(outmk, " %s.lo", p->name); fprintf(outmk, "\n"); fprintf(outmk, "SUBMAKE_TARGETS="); for (p = progs; p != NULL; p = p->next) fprintf(outmk, " %s_make", p->ident); fprintf(outmk, "\nSUBCLEAN_TARGETS="); for (p = progs; p != NULL; p = p->next) fprintf(outmk, " %s_clean", p->ident); fprintf(outmk, "\n\n"); fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n"); fprintf(outmk, "exe: %s\n", execfname); fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS) $(SUBMAKE_TARGETS)\n", execfname, execfname); fprintf(outmk, ".if defined(LIBS_SO) && !empty(LIBS_SO)\n"); fprintf(outmk, "\t$(CC) -o %s %s.o $(CRUNCHED_OBJS) \\\n", execfname, execfname); fprintf(outmk, "\t\t-Xlinker -Bstatic $(LIBS) \\\n"); fprintf(outmk, "\t\t-Xlinker -Bdynamic $(LIBS_SO)\n"); fprintf(outmk, ".else\n"); fprintf(outmk, "\t$(CC) -static -o %s %s.o $(CRUNCHED_OBJS) $(LIBS)\n", execfname, execfname); fprintf(outmk, ".endif\n"); fprintf(outmk, "realclean: clean subclean\n"); fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", execfname); fprintf(outmk, "subclean: $(SUBCLEAN_TARGETS)\n"); } void prog_makefile_rules(FILE *outmk, prog_t *p) { strlst_t *lst; fprintf(outmk, "\n# -------- %s\n\n", p->name); fprintf(outmk, "%s_OBJDIR=", p->ident); if (p->objdir) fprintf(outmk, "%s", p->objdir); else fprintf(outmk, "$(MAKEOBJDIRPREFIX)/$(%s_REALSRCDIR)\n", p->ident); fprintf(outmk, "\n"); fprintf(outmk, "%s_OBJPATHS=", p->ident); if (p->objpaths) output_strlst(outmk, p->objpaths); else { for (lst = p->objs; lst != NULL; lst = lst->next) { fprintf(outmk, " $(%s_OBJDIR)/%s", p->ident, lst->str); } fprintf(outmk, "\n"); } fprintf(outmk, "$(%s_OBJPATHS): .NOMETA\n", p->ident); if (p->srcdir && p->objs) { fprintf(outmk, "%s_SRCDIR=%s\n", p->ident, p->srcdir); fprintf(outmk, "%s_REALSRCDIR=%s\n", p->ident, p->realsrcdir); fprintf(outmk, "%s_OBJS=", p->ident); output_strlst(outmk, p->objs); if (p->buildopts != NULL) { fprintf(outmk, "%s_OPTS+=", p->ident); output_strlst(outmk, p->buildopts); } #if 0 fprintf(outmk, "$(%s_OBJPATHS): %s_make\n\n", p->ident, p->ident); #endif fprintf(outmk, "%s_make:\n", p->ident); fprintf(outmk, "\t(cd $(%s_SRCDIR) && ", p->ident); if (makeobj) fprintf(outmk, "$(CRUNCHMAKE) obj && "); fprintf(outmk, "\\\n"); fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) depend &&", p->ident); fprintf(outmk, "\\\n"); fprintf(outmk, "\t\t$(CRUNCHMAKE) $(BUILDOPTS) $(%s_OPTS) " "$(%s_OBJS))", p->ident, p->ident); fprintf(outmk, "\n"); fprintf(outmk, "%s_clean:\n", p->ident); fprintf(outmk, "\t(cd $(%s_SRCDIR) && $(CRUNCHMAKE) $(BUILDOPTS) clean cleandepend)\n\n", p->ident); } else { fprintf(outmk, "%s_make:\n", p->ident); fprintf(outmk, "\t@echo \"** cannot make objs for %s\"\n\n", p->name); } if (p->libs) { fprintf(outmk, "%s_LIBS=", p->ident); output_strlst(outmk, p->libs); } fprintf(outmk, "%s_stub.c:\n", p->name); fprintf(outmk, "\techo \"" "extern int main(int argc, char **argv, char **envp); " + "int _crunched_%s_stub(int argc, char **argv, char **envp);" "int _crunched_%s_stub(int argc, char **argv, char **envp)" "{return main(argc,argv,envp);}\" >%s_stub.c\n", - p->ident, p->name); + p->ident, p->ident, p->name); fprintf(outmk, "%s.lo: %s_stub.o $(%s_OBJPATHS)", p->name, p->name, p->ident); if (p->libs) fprintf(outmk, " $(%s_LIBS)", p->ident); fprintf(outmk, "\n"); fprintf(outmk, "\t$(CC) -nostdlib -Wl,-dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)", p->name, p->name, p->ident); if (p->libs) fprintf(outmk, " $(%s_LIBS)", p->ident); fprintf(outmk, "\n"); fprintf(outmk, "\tcrunchide -k _crunched_%s_stub ", p->ident); for (lst = p->keeplist; lst != NULL; lst = lst->next) fprintf(outmk, "-k _%s ", lst->str); fprintf(outmk, "%s.lo\n", p->name); } void output_strlst(FILE *outf, strlst_t *lst) { for (; lst != NULL; lst = lst->next) if ( strlen(lst->str) ) fprintf(outf, " %s", lst->str); fprintf(outf, "\n"); } /* * ======================================================================== * general library routines * */ void status(const char *str) { static int lastlen = 0; int len, spaces; if (!verbose) return; len = strlen(str); spaces = lastlen - len; if (spaces < 1) spaces = 1; fprintf(stderr, " [%s]%*.*s\r", str, spaces, spaces, " "); fflush(stderr); lastlen = len; } void out_of_memory(void) { err(1, "%s: %d: out of memory, stopping", infilename, linenum); } void add_string(strlst_t **listp, char *str) { strlst_t *p1, *p2; /* add to end, but be smart about dups */ for (p1 = NULL, p2 = *listp; p2 != NULL; p1 = p2, p2 = p2->next) if (!strcmp(p2->str, str)) return; p2 = malloc(sizeof(strlst_t)); if (p2) { p2->next = NULL; p2->str = strdup(str); } if (!p2 || !p2->str) out_of_memory(); if (p1 == NULL) *listp = p2; else p1->next = p2; } int subtract_strlst(strlst_t **lista, strlst_t **listb) { int subtract_count = 0; strlst_t *p1; for (p1 = *listb; p1 != NULL; p1 = p1->next) if ( in_list(lista, p1->str) ) { warnx("Will compile library `%s' dynamically", p1->str); strcat(p1->str, ""); subtract_count++; } return subtract_count; } int in_list(strlst_t **listp, char *str) { strlst_t *p1; for (p1 = *listp; p1 != NULL; p1 = p1->next) if (!strcmp(p1->str, str)) return 1; return 0; } int is_dir(const char *pathname) { struct stat buf; if (stat(pathname, &buf) == -1) return 0; return S_ISDIR(buf.st_mode); } int is_nonempty_file(const char *pathname) { struct stat buf; if (stat(pathname, &buf) == -1) return 0; return S_ISREG(buf.st_mode) && buf.st_size > 0; } Index: stable/12 =================================================================== --- stable/12 (revision 365815) +++ stable/12 (revision 365816) Property changes on: stable/12 ___________________________________________________________________ Modified: svn:mergeinfo ## -0,0 +0,1 ## Merged /head:r350152,364166,364174,364234,364646-364647,365605,365705