Commands like gmirror, graid3, ... and others which use dlopen() to load

classes from say, /lib/geom, cannot be statically linked completely.
Moreover, those shared objects may require other shared objects (i.e.
 for geom, libraries like -lmd, -lcrypto).

The libs_so extension to crunchgen fixes this by allowing some libraries
to be linked in dynamically.  This requires that a copy of rtld and the
shared libraries be made available to the crunched binary, and so is not
suitable for all environments.  Crunchgen configurations which do not
use the 'libs_so' keyword are unaffected and produce identical binaries
with and without this commit.

Approved by:		murray (mentor, in spirit), jhb
In collaboration with:	Adrian Steinmann <ast at marabu dot ch>
MFC After:		6 weeks
This commit is contained in:
ceri 2005-12-23 15:31:37 +00:00
parent bb7dfbb0a8
commit 9618c3e8c0
2 changed files with 95 additions and 4 deletions

View File

@ -139,7 +139,7 @@ generated by
.Nm .
This is useful to define some make variables such as
.Va RELEASE_CRUNCH
or similar, which might affect the behaviour of
or similar, which might affect the behavior of
.Xr make 1
and are annoying to pass through environment variables.
.It Fl m Ar makefile-name
@ -210,6 +210,21 @@ 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
@ -417,9 +432,15 @@ 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 make 1 ,
.Xr rtld 1
.Sh CAVEATS
While
.Nm
@ -446,3 +467,10 @@ utility was written by
.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 ast@marabu.ch
and
.An Ceri Davies Aq ceri@FreeBSD.org .

View File

@ -74,6 +74,7 @@ typedef struct prog {
strlst_t *keeplist;
strlst_t *links;
strlst_t *libs;
strlst_t *libs_so;
int goterror;
} prog_t;
@ -83,6 +84,7 @@ typedef struct prog {
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];
@ -106,6 +108,8 @@ void out_of_memory(void);
void add_string(strlst_t **listp, char *str);
int is_dir(char *pathname);
int is_nonempty_file(char *pathname);
int subtract_strlst(strlst_t **lista, strlst_t **listb);
int in_list(strlst_t **listp, char *str);
/* helper routines for main() */
@ -238,6 +242,7 @@ 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);
@ -292,6 +297,8 @@ void parse_one_file(char *filename)
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"))
@ -408,6 +415,7 @@ void add_prog(char *progname)
p2->objdir = NULL;
p2->links = NULL;
p2->libs = NULL;
p2->libs_so = NULL;
p2->objs = NULL;
p2->keeplist = NULL;
p2->buildopts = NULL;
@ -443,8 +451,27 @@ void add_libs(int argc, char **argv)
{
int i;
for(i = 1; i < argc; 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]);
}
}
@ -922,9 +949,15 @@ void top_makefile_rules(FILE *outmk)
{
prog_t *p;
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");
@ -954,8 +987,15 @@ void top_makefile_rules(FILE *outmk)
fprintf(outmk, "all: objs exe\nobjs: $(SUBMAKE_TARGETS)\n");
fprintf(outmk, "exe: %s\n", execfname);
fprintf(outmk, "%s: %s.o $(CRUNCHED_OBJS)\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, "\tstrip %s\n", execfname);
fprintf(outmk, "realclean: clean subclean\n");
fprintf(outmk, "clean:\n\trm -f %s *.lo *.o *_stub.c\n", execfname);
@ -1031,6 +1071,7 @@ void prog_makefile_rules(FILE *outmk, prog_t *p)
p->name, p->name, p->ident);
if (p->libs)
fprintf(outmk, " $(%s_LIBS)", p->ident);
fprintf(outmk, "\n");
fprintf(outmk, "\tld -dc -r -o %s.lo %s_stub.o $(%s_OBJPATHS)",
p->name, p->name, p->ident);
@ -1046,7 +1087,8 @@ void prog_makefile_rules(FILE *outmk, prog_t *p)
void output_strlst(FILE *outf, strlst_t *lst)
{
for (; lst != NULL; lst = lst->next)
fprintf(outf, " %s", lst->str);
if ( strlen(lst->str) )
fprintf(outf, " %s", lst->str);
fprintf(outf, "\n");
}
@ -1106,6 +1148,27 @@ void add_string(strlst_t **listp, char *str)
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(char *pathname)
{