Blame src/logic/slbt_exec_link.c

2bd749
/*******************************************************************/
2bd749
/*  slibtool: a skinny libtool implementation, written in C        */
6803d8
/*  Copyright (C) 2016--2018  Z. Gilboa                            */
2bd749
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
2bd749
/*******************************************************************/
2bd749
8b7d50
#include <stdlib.h>
f0d5f3
#include <stdio.h>
2bd749
#include <string.h>
2bd749
#include <stdbool.h>
2bd749
#include <fcntl.h>
2bd749
#include <errno.h>
2bd749
#include <sys/stat.h>
2bd749
2bd749
#include <slibtool/slibtool.h>
2bd749
#include "slibtool_spawn_impl.h"
3895af
#include "slibtool_mkdir_impl.h"
9706fa
#include "slibtool_driver_impl.h"
c178e3
#include "slibtool_dprintf_impl.h"
9c9f28
#include "slibtool_errinfo_impl.h"
05ea52
#include "slibtool_mapfile_impl.h"
a9cfe4
#include "slibtool_metafile_impl.h"
50b6ef
#include "slibtool_readlink_impl.h"
6529aa
#include "slibtool_symlink_impl.h"
2bd749
8b7d50
struct slbt_deps_meta {
8b7d50
	char ** altv;
8b7d50
	char *	args;
8b7d50
	int	depscnt;
8b7d50
	int	infolen;
8b7d50
};
8b7d50
5cc3b3
/*******************************************************************/
5cc3b3
/*                                                                 */
5cc3b3
/* -o <ltlib>  switches              input   result                */
5cc3b3
/* ----------  --------------------- -----   ------                */
b07789
/* libfoo.a    [-shared|-static]     bar.lo  libfoo.a              */
5cc3b3
/*                                                                 */
0330cf
/* ar crs libfoo.a bar.o                                           */
5cc3b3
/*                                                                 */
5cc3b3
/*******************************************************************/
5cc3b3
a0c318
/*******************************************************************/
a0c318
/*                                                                 */
a0c318
/* -o <ltlib>  switches              input   result                */
a0c318
/* ----------  --------------------- -----   ------                */
a0c318
/* libfoo.la   -shared               bar.lo  libfoo.la             */
a0c318
/*                                           .libs/libfoo.a        */
a0c318
/*                                           .libs/libfoo.la (lnk) */
a0c318
/*                                                                 */
0330cf
/* ar crs .libs/libfoo.a .libs/bar.o                               */
a0c318
/* (generate libfoo.la)                                            */
a0c318
/* ln -s ../libfoo.la .libs/libfoo.la                              */
f730ac
/*                                                                 */
f730ac
/*******************************************************************/
f730ac
f730ac
/*******************************************************************/
f730ac
/*                                                                 */
f730ac
/* -o <ltlib>  switches              input   result                */
f730ac
/* ----------  --------------------- -----   ------                */
f730ac
/* libfoo.la   -static               bar.lo  libfoo.la             */
f730ac
/*                                           .libs/libfoo.a        */
f730ac
/*                                           .libs/libfoo.la (lnk) */
f730ac
/*                                                                 */
0330cf
/* ar crs .libs/libfoo.a bar.o                                     */
f730ac
/* (generate libfoo.la)                                            */
f730ac
/* ln -s ../libfoo.la .libs/libfoo.la                              */
a0c318
/*                                                                 */
a0c318
/*******************************************************************/
a0c318
3be47d
static int slbt_exec_link_exit(
3be47d
	struct slbt_deps_meta *	depsmeta,
3be47d
	int			ret)
3be47d
{
3be47d
	if (depsmeta->altv)
3be47d
		free(depsmeta->altv);
3be47d
3be47d
	if (depsmeta->args)
3be47d
		free(depsmeta->args);
3be47d
3be47d
	return ret;
3be47d
}
3be47d
8b7d50
static int slbt_get_deps_meta(
9c9f28
	const struct slbt_driver_ctx *	dctx,
ba8a74
	char *				libfilename,
ba8a74
	int				fexternal,
ba8a74
	struct slbt_deps_meta *		depsmeta)
8b7d50
{
5fac6c
	int			fdcwd;
05ea52
	char *			ch;
05ea52
	char *			cap;
05ea52
	char *			base;
05ea52
	size_t			libexlen;
05ea52
	struct stat		st;
05ea52
	struct slbt_map_info *	mapinfo;
05ea52
	char			depfile[PATH_MAX];
9c9f28
0f5ca7
	/* -rpath */
05ea52
	if ((size_t)snprintf(depfile,sizeof(depfile),
05ea52
				"%s.slibtool.rpath",
0f5ca7
				libfilename)
0f5ca7
			>= sizeof(depfile))
9c9f28
		return SLBT_BUFFER_ERROR(dctx);
0f5ca7
05ea52
	/* -Wl,%s */
0f5ca7
	if (!(lstat(depfile,&st))) {
0f5ca7
		depsmeta->infolen += st.st_size + 4;
0f5ca7
		depsmeta->infolen++;
0f5ca7
	}
0f5ca7
0f5ca7
	/* .deps */
05ea52
	if ((size_t)snprintf(depfile,sizeof(depfile),
05ea52
				"%s.slibtool.deps",
8b7d50
				libfilename)
8b7d50
			>= sizeof(depfile))
9c9f28
		return SLBT_BUFFER_ERROR(dctx);
8b7d50
5fac6c
	/* fdcwd */
5fac6c
	fdcwd = slbt_driver_fdcwd(dctx);
5fac6c
05ea52
	/* mapinfo */
5fac6c
	if (!(mapinfo = slbt_map_file(fdcwd,depfile,SLBT_MAP_INPUT)))
ba8a74
		return (fexternal && (errno == ENOENT))
ba8a74
			? 0 : SLBT_SYSTEM_ERROR(dctx);
8b7d50
05ea52
	/* copied length */
05ea52
	depsmeta->infolen += mapinfo->size;
8b7d50
	depsmeta->infolen++;
8b7d50
05ea52
	/* libexlen */
05ea52
	libexlen = (base = strrchr(libfilename,'/'))
05ea52
		? strlen(depfile) + 2 + (base - libfilename)
05ea52
		: strlen(depfile) + 2;
e64108
05ea52
	/* iterate */
05ea52
	ch  = mapinfo->addr;
05ea52
	cap = mapinfo->cap;
6fda2b
05ea52
	for (; ch
86b72f
		if (*ch++ == '\n') {
05ea52
			depsmeta->infolen += libexlen;
05ea52
			depsmeta->depscnt++;
05ea52
		}
78abd7
	}
8b7d50
05ea52
	slbt_unmap_file(mapinfo);
085f3a
05ea52
	return 0;
8b7d50
}
8b7d50
932031
static bool slbt_adjust_object_argument(
14d60a
	char *		arg,
2fd6a3
	bool		fpic,
2fd6a3
	bool		fany,
2fd6a3
	int		fdcwd)
5cc3b3
{
53aa85
	char *	slash;
5cc3b3
	char *	dot;
53aa85
	char	base[PATH_MAX];
5cc3b3
5cc3b3
	if (*arg == '-')
5cc3b3
		return false;
5cc3b3
2fd6a3
	/* object argument: foo.lo or foo.o */
5cc3b3
	if (!(dot = strrchr(arg,'.')))
5cc3b3
		return false;
5cc3b3
2fd6a3
	if ((dot[1]=='l') && (dot[2]=='o') && !dot[3]) {
2fd6a3
		dot[1] = 'o';
2fd6a3
		dot[2] = 0;
2fd6a3
2fd6a3
	} else if ((dot[1]=='o') && !dot[2]) {
2fd6a3
		(void)0;
2fd6a3
2fd6a3
	} else {
5cc3b3
		return false;
2fd6a3
	}
5cc3b3
2fd6a3
	/* foo.o requested and is present? */
2fd6a3
	if (!fpic && !faccessat(fdcwd,arg,0,0))
2fd6a3
		return true;
53aa85
2fd6a3
	/* .libs/foo.o */
2fd6a3
	if ((slash = strrchr(arg,'/')))
2fd6a3
		slash++;
2fd6a3
	else
2fd6a3
		slash = arg;
53aa85
2fd6a3
	if ((size_t)snprintf(base,sizeof(base),"%s",
2fd6a3
			slash) >= sizeof(base))
2fd6a3
		return false;
2fd6a3
2fd6a3
	sprintf(slash,".libs/%s",base);
2fd6a3
2fd6a3
	if (!faccessat(fdcwd,arg,0,0))
2fd6a3
		return true;
2fd6a3
2fd6a3
	/* foo.o requested and neither is present? */
2fd6a3
	if (!fpic) {
2fd6a3
		strcpy(slash,base);
2fd6a3
		return true;
5cc3b3
	}
53aa85
2fd6a3
	/* .libs/foo.o explicitly requested and is not present? */
2fd6a3
	if (!fany)
2fd6a3
		return true;
2fd6a3
2fd6a3
	/* use foo.o in place of .libs/foo.o */
2fd6a3
	strcpy(slash,base);
2fd6a3
2fd6a3
	if (faccessat(fdcwd,arg,0,0))
2fd6a3
		sprintf(slash,".libs/%s",base);
2fd6a3
932031
	return true;
932031
}
932031
932031
static bool slbt_adjust_wrapper_argument(
932031
	char *		arg,
932031
	bool		fpic)
932031
{
932031
	char *	slash;
932031
	char *	dot;
932031
	char	base[PATH_MAX];
932031
932031
	if (*arg == '-')
932031
		return false;
932031
932031
	if (!(dot = strrchr(arg,'.')))
932031
		return false;
932031
932031
	if (strcmp(dot,".la"))
932031
		return false;
932031
932031
	if (fpic) {
932031
		if ((slash = strrchr(arg,'/')))
932031
			slash++;
932031
		else
932031
			slash = arg;
932031
932031
		if ((size_t)snprintf(base,sizeof(base),"%s",
932031
				slash) >= sizeof(base))
932031
			return false;
932031
932031
		sprintf(slash,".libs/%s",base);
932031
		dot = strrchr(arg,'.');
932031
	}
932031
932031
	strcpy(dot,".a");
53aa85
	return true;
5cc3b3
}
5cc3b3
afaba2
static int slbt_adjust_linker_argument(
9c9f28
	const struct slbt_driver_ctx *	dctx,
618537
	char *				arg,
46aa6f
	char **				xarg,
618537
	bool				fpic,
618537
	const char *			dsosuffix,
618537
	const char *			arsuffix,
618537
	struct slbt_deps_meta * 	depsmeta)
b3940a
{
cb9119
	int	fdcwd;
b3940a
	int	fdlib;
b3940a
	char *	slash;
b3940a
	char *	dot;
b3940a
	char	base[PATH_MAX];
b3940a
b3940a
	if (*arg == '-')
afaba2
		return 0;
b3940a
b3940a
	if (!(dot = strrchr(arg,'.')))
afaba2
		return 0;
b3940a
46aa6f
	if (!(strcmp(dot,arsuffix))) {
46aa6f
		*xarg = arg;
ba8a74
		return slbt_get_deps_meta(dctx,arg,1,depsmeta);
46aa6f
	}
6a1ae7
6a1ae7
	if (!(strcmp(dot,dsosuffix)))
ba8a74
		return slbt_get_deps_meta(dctx,arg,1,depsmeta);
6a1ae7
b3940a
	if (strcmp(dot,".la"))
afaba2
		return 0;
b3940a
b3940a
	if (fpic) {
b3940a
		if ((slash = strrchr(arg,'/')))
b3940a
			slash++;
b3940a
		else
b3940a
			slash = arg;
b3940a
b3940a
		if ((size_t)snprintf(base,sizeof(base),"%s",
b3940a
				slash) >= sizeof(base))
afaba2
			return 0;
b3940a
b3940a
		sprintf(slash,".libs/%s",base);
b3940a
		dot = strrchr(arg,'.');
b3940a
	}
b3940a
cb9119
	/* fdcwd */
cb9119
	fdcwd = slbt_driver_fdcwd(dctx);
cb9119
b3940a
	/* shared library dependency? */
b3940a
	if (fpic) {
b3940a
		sprintf(dot,"%s",dsosuffix);
b3940a
2baf1c
		if (slbt_symlink_is_a_placeholder(arg))
50b6ef
			sprintf(dot,"%s",arsuffix);
cb9119
		else if ((fdlib = openat(fdcwd,arg,O_RDONLY)) >= 0)
b3940a
			close(fdlib);
b3940a
		else
b3940a
			sprintf(dot,"%s",arsuffix);
b3940a
ba8a74
		return slbt_get_deps_meta(dctx,arg,0,depsmeta);
b3940a
	}
b3940a
b3940a
	/* input archive */
b3940a
	sprintf(dot,"%s",arsuffix);
ba8a74
	return slbt_get_deps_meta(dctx,arg,0,depsmeta);
b3940a
}
b3940a
2ef5be
static int slbt_exec_link_adjust_argument_vector(
2ef5be
	const struct slbt_driver_ctx *	dctx,
2ef5be
	struct slbt_exec_ctx *		ectx,
3be47d
	struct slbt_deps_meta *		depsmeta,
79c501
	const char *			cwd,
2ef5be
	bool				flibrary)
2ef5be
{
9ae217
	int			fd;
9706fa
	int			fdwrap;
5fac6c
	int			fdcwd;
9ae217
	char ** 		carg;
9ae217
	char ** 		aarg;
9ae217
	char *			ldir;
9ae217
	char *			slash;
9ae217
	char *			mark;
9ae217
	char *			darg;
9ae217
	char *			dot;
9ae217
	char *			base;
9ae217
	char *			dpath;
9ae217
	int			argc;
9ae217
	char			arg[PATH_MAX];
9ae217
	char			lib[PATH_MAX];
9ae217
	char			depdir  [PATH_MAX];
9ae217
	char			rpathdir[PATH_MAX];
9ae217
	char			rpathlnk[PATH_MAX];
9ae217
	struct stat		st;
9ae217
	size_t			size;
9ae217
	size_t			dlen;
9ae217
	struct slbt_map_info *	mapinfo;
9ae217
	bool			fwholearchive = false;
2ef5be
3be47d
	for (argc=0,carg=ectx->cargv; *carg; carg++)
3be47d
		argc++;
3be47d
3be47d
	if (!(depsmeta->args = calloc(1,depsmeta->infolen)))
9c9f28
		return SLBT_SYSTEM_ERROR(dctx);
3be47d
3be47d
	argc *= 3;
3be47d
	argc += depsmeta->depscnt;
3be47d
3be47d
	if (!(depsmeta->altv = calloc(argc,sizeof(char *))))
b332ad
		return slbt_exec_link_exit(
b332ad
			depsmeta,
b332ad
			SLBT_SYSTEM_ERROR(dctx));
3be47d
5fac6c
	fdcwd = slbt_driver_fdcwd(dctx);
5fac6c
2ef5be
	carg = ectx->cargv;
3be47d
	aarg = depsmeta->altv;
3be47d
	darg = depsmeta->args;
9ae217
	size = depsmeta->infolen;
2ef5be
2ef5be
	for (; *carg; ) {
3be47d
		dpath = 0;
3be47d
2ef5be
		if (!strcmp(*carg,"-Wl,--whole-archive"))
2ef5be
			fwholearchive = true;
2ef5be
		else if (!strcmp(*carg,"-Wl,--no-whole-archive"))
2ef5be
			fwholearchive = false;
2ef5be
2ef5be
2ef5be
df07fb
		/* output annotation */
df07fb
		if (carg == ectx->lout[0]) {
df07fb
			ectx->mout[0] = &aarg[0];
df07fb
			ectx->mout[1] = &aarg[1];
df07fb
		}
df07fb
df07fb
		/* argument translation */
738321
		mark = *carg;
738321
738321
		if ((mark[0] == '-') && (mark[1] == 'L')) {
738321
			if (mark[2]) {
738321
				ldir = &mark[2];
738321
			} else {
738321
				*aarg++ = *carg++;
738321
				ldir    = *carg;
738321
			}
738321
738321
			mark = ldir + strlen(ldir);
738321
738321
			if (mark[-1] == '/')
738321
				strcpy(mark,".libs");
738321
			else
738321
				strcpy(mark,"/.libs");
738321
cb9119
			if ((fd = openat(fdcwd,ldir,O_DIRECTORY,0)) < 0)
738321
				*mark = 0;
738321
			else
738321
				close(fd);
738321
738321
			*aarg++ = *carg++;
738321
738321
		} else if (**carg == '-') {
2ef5be
			*aarg++ = *carg++;
2ef5be
2ef5be
		} else if (!(dot = strrchr(*carg,'.'))) {
2ef5be
			*aarg++ = *carg++;
2ef5be
46aa6f
		} else if (ectx->xargv[carg - ectx->cargv]) {
46aa6f
			*aarg++ = *carg++;
46aa6f
2ef5be
		} else if (!(strcmp(dot,".a"))) {
2ef5be
			if (flibrary && !fwholearchive)
2ef5be
				*aarg++ = "-Wl,--whole-archive";
2ef5be
3be47d
			dpath = lib;
3be47d
			sprintf(lib,"%s.slibtool.deps",*carg);
2ef5be
			*aarg++ = *carg++;
2ef5be
2ef5be
			if (flibrary && !fwholearchive)
2ef5be
				*aarg++ = "-Wl,--no-whole-archive";
2ef5be
2ef5be
		} else if (strcmp(dot,dctx->cctx->settings.dsosuffix)) {
2ef5be
			*aarg++ = *carg++;
2ef5be
867d1e
		} else if (carg == ectx->lout[1]) {
867d1e
			/* ^^^hoppla^^^ */
867d1e
			*aarg++ = *carg++;
2ef5be
		} else {
0f5ca7
			/* -rpath */
0f5ca7
			sprintf(rpathlnk,"%s.slibtool.rpath",*carg);
0f5ca7
0f5ca7
			if (!(lstat(rpathlnk,&st))) {
0f5ca7
				if (slbt_readlink(
0f5ca7
						rpathlnk,\
0f5ca7
						rpathdir,
0f5ca7
						sizeof(rpathdir)))
b332ad
					return slbt_exec_link_exit(
b332ad
						depsmeta,
b332ad
						SLBT_SYSTEM_ERROR(dctx));
0f5ca7
0f5ca7
				sprintf(darg,"-Wl,%s",rpathdir);
0f5ca7
				*aarg++ = "-Wl,-rpath";
0f5ca7
				*aarg++ = darg;
0f5ca7
				darg   += strlen(darg);
0f5ca7
				darg++;
0f5ca7
			}
0f5ca7
3be47d
			dpath = lib;
3be47d
			sprintf(lib,"%s.slibtool.deps",*carg);
3be47d
2ef5be
			/* account for {'-','L','-','l'} */
b332ad
			if ((size_t)snprintf(arg,sizeof(arg),
b332ad
						"%s",*carg)
b332ad
					>= (sizeof(arg) - 4))
b332ad
				return slbt_exec_link_exit(
b332ad
					depsmeta,
b332ad
					SLBT_BUFFER_ERROR(dctx));
2ef5be
2ef5be
			if ((slash = strrchr(arg,'/'))) {
2ef5be
				sprintf(*carg,"-L%s",arg);
2ef5be
2ef5be
				mark   = strrchr(*carg,'/');
5b792c
				*mark  = 0;
2ef5be
9706fa
				if ((fdwrap = slbt_exec_get_fdwrapper(ectx)) >= 0) {
5b792c
					*slash = 0;
5ee0d1
9706fa
					if (slbt_dprintf(fdwrap,
79c501
							"DL_PATH=\"$DL_PATH$COLON%s/%s\"\n"
5ee0d1
							"COLON=':'\n\n",
79c501
							cwd,arg) < 0)
b332ad
						return slbt_exec_link_exit(
b332ad
							depsmeta,
b332ad
							SLBT_SYSTEM_ERROR(dctx));
5ee0d1
				}
5ee0d1
2ef5be
				*aarg++ = *carg++;
2ef5be
				*aarg++ = ++mark;
2ef5be
2ef5be
				++slash;
2ef5be
				slash += strlen(dctx->cctx->settings.dsoprefix);
2ef5be
2ef5be
				sprintf(mark,"-l%s",slash);
2ef5be
				dot  = strrchr(mark,'.');
5b792c
				*dot = 0;
2ef5be
			} else {
2ef5be
				*aarg++ = *carg++;
2ef5be
			}
2ef5be
		}
3be47d
9ae217
		if (dpath && !stat(dpath,&st)) {
9ae217
			if (!(mapinfo = slbt_map_file(
5fac6c
					fdcwd,dpath,
9ae217
					SLBT_MAP_INPUT)))
9ae217
				return slbt_exec_link_exit(
9ae217
					depsmeta,
9ae217
					SLBT_SYSTEM_ERROR(dctx));
9ae217
9ae217
			if (!(strncmp(lib,".libs/",6))) {
9ae217
				*aarg++ = "-L.libs";
9ae217
				lib[1] = 0;
9ae217
			} else if ((base = strrchr(lib,'/'))) {
9ae217
				if (base - lib == 5) {
9ae217
					if (!(strncmp(&base[-5],".libs/",6)))
9ae217
						base -= 4;
9ae217
9ae217
				} else if (base - lib >= 6) {
9ae217
					if (!(strncmp(&base[-6],"/.libs/",7)))
9ae217
						base -= 6;
94ae74
				}
3be47d
9ae217
				*base = 0;
9ae217
			} else {
9ae217
				lib[0] = '.';
9ae217
				lib[1] = 0;
9ae217
			}
b332ad
9ae217
			while (mapinfo->mark < mapinfo->cap) {
9ae217
				if (slbt_mapped_readline(dctx,mapinfo,darg,size))
b332ad
					return slbt_exec_link_exit(
b332ad
						depsmeta,
9ae217
						SLBT_NESTED_ERROR(dctx));
b332ad
9ae217
				*aarg++   = darg;
9ae217
				mark      = darg;
9ae217
9ae217
				dlen      = strlen(darg);
9ae217
				size     -= dlen;
9ae217
				darg     += dlen;
9ae217
				darg[-1]  = 0;
9ae217
9ae217
				/* handle -L... as needed */
9ae217
				if ((mark[0] == '-')
9ae217
						&& (mark[1] == 'L')
9ae217
						&& (mark[2] != '/')) {
9ae217
					if (strlen(mark) >= sizeof(depdir) - 1)
9ae217
						return slbt_exec_link_exit(
9ae217
							depsmeta,
9ae217
							SLBT_BUFFER_ERROR(dctx));
9ae217
9ae217
					darg = mark;
9ae217
					strcpy(depdir,&mark[2]);
9ae217
					sprintf(darg,"-L%s/%s",lib,depdir);
9ae217
9ae217
					darg += strlen(darg);
9ae217
					darg++;
9ae217
				}
3be47d
			}
3be47d
		}
2ef5be
	}
2ef5be
f5983e
	if (dctx->cctx->drvflags & SLBT_DRIVER_EXPORT_DYNAMIC)
f5983e
		*aarg++ = "-Wl,--export-dynamic";
f5983e
2ef5be
	return 0;
2ef5be
}
2ef5be
671454
static int slbt_exec_link_finalize_argument_vector(
671454
	const struct slbt_driver_ctx *	dctx,
671454
	struct slbt_exec_ctx *		ectx)
671454
{
671454
	char *		sargv[1024];
671454
	char **		sargvbuf;
671454
	char **		base;
c6d4dc
	char **		parg;
c6d4dc
	char **		aarg;
c6d4dc
	char **		oarg;
2480bd
	char **		larg;
2480bd
	char **		darg;
2480bd
	char **		earg;
2480bd
	char **		rarg;
c6d4dc
	char **		aargv;
c6d4dc
	char **		oargv;
c6d4dc
	char **		cap;
c6d4dc
	char **		src;
c6d4dc
	char **		dst;
671454
	char *		arg;
671454
	char *		dot;
c6d4dc
	const char *	arsuffix;
671454
c6d4dc
	/* vector size */
c6d4dc
	base     = ectx->argv;
c6d4dc
	arsuffix = dctx->cctx->settings.arsuffix;
671454
c6d4dc
	for (parg=base; *parg; parg++)
c6d4dc
		(void)0;
671454
671454
	/* buffer */
c6d4dc
	if (parg - base < 512) {
c6d4dc
		aargv    = &sargv[0];
c6d4dc
		oargv    = &sargv[512];
c6d4dc
		aarg     = aargv;
c6d4dc
		oarg     = oargv;
671454
		sargvbuf = 0;
671454
c6d4dc
	} else if (!(sargvbuf = calloc(2*(parg-base+1),sizeof(char *)))) {
671454
		return SLBT_SYSTEM_ERROR(dctx);
671454
671454
	} else {
c6d4dc
		aargv = &sargvbuf[0];
c6d4dc
		oargv = &sargvbuf[parg-base+1];
c6d4dc
		aarg  = aargv;
c6d4dc
		oarg  = oargv;
671454
	}
671454
c6d4dc
	/* (program name) */
c6d4dc
	parg = &base[1];
c6d4dc
2480bd
	/* split object args from all other args, record output */
2480bd
	/* annotation, and remove redundant -l arguments       */
c6d4dc
	for (; *parg; ) {
c6d4dc
		if (ectx->lout[0] == parg) {
c6d4dc
			ectx->lout[0] = &aarg[0];
c6d4dc
			ectx->lout[1] = &aarg[1];
c6d4dc
		}
c6d4dc
c6d4dc
		if (ectx->mout[0] == parg) {
208722
			ectx->mout[0] = &aarg[0];
208722
			ectx->mout[1] = &aarg[1];
c6d4dc
		}
c6d4dc
c6d4dc
		arg = *parg;
c6d4dc
		dot = strrchr(arg,'.');
671454
890281
		/* object input argument? */
c6d4dc
		if (dot && (!strcmp(dot,".o") || !strcmp(dot,".lo"))) {
c6d4dc
			*oarg++ = *parg++;
671454
890281
		/* --whole-archive input argument? */
c6d4dc
		} else if ((arg[0] == '-')
c6d4dc
				&& (arg[1] == 'W')
c6d4dc
				&& (arg[2] == 'l')
c6d4dc
				&& (arg[3] == ',')
c6d4dc
				&& !strcmp(&arg[4],"--whole-archive")
c6d4dc
				&& parg[1] && parg[2]
c6d4dc
				&& !strcmp(parg[2],"-Wl,--no-whole-archive")
c6d4dc
				&& (dot = strrchr(parg[1],'.'))
c6d4dc
				&& !strcmp(dot,arsuffix)) {
c6d4dc
			*oarg++ = *parg++;
c6d4dc
			*oarg++ = *parg++;
c6d4dc
			*oarg++ = *parg++;
dc48ad
890281
		/* local archive input argument? */
dc48ad
		} else if (dot && !strcmp(dot,arsuffix)) {
dc48ad
			*oarg++ = *parg++;
dc48ad
890281
		/* -l argument? */
890281
		} else if ((parg[0][0] == '-') && (parg[0][1] == 'l')) {
890281
			/* find the previous occurence of this -l argument */
890281
			for (rarg=0, larg=&aarg[-1]; !rarg && (larg>=aargv); larg--)
890281
				if (!strcmp(*larg,*parg))
890281
					rarg = larg;
2480bd
890281
			/* first occurence of this specific -l argument? */
890281
			if (!rarg) {
890281
				*aarg++ = *parg++;
2480bd
890281
			} else {
890281
				larg = rarg;
890281
890281
				/* if all -l arguments following the previous */
890281
				/* occurence had already appeared before the */
890281
				/* previous argument, then the current      */
890281
				/* occurence is redundant.                 */
890281
890281
				for (darg=&larg[1]; rarg && darg
890281
					/* only test -l arguments */
890281
					if ((darg[0][0] == '-') && (darg[0][1] == 'l')) {
890281
						for (rarg=0, earg=aargv; !rarg && earg
890281
							if (!strcmp(*earg,*darg))
890281
								rarg = darg;
2480bd
					}
890281
				}
2480bd
890281
				/* final verdict: repeated -l argument? */
890281
				if (rarg) {
890281
					parg++;
2480bd
890281
				} else {
890281
					*aarg++ = *parg++;
2480bd
				}
2480bd
			}
890281
85f619
		/* -L argument? */
85f619
		} else if ((parg[0][0] == '-') && (parg[0][1] == 'L')) {
85f619
			/* find a previous occurence of this -L argument */
85f619
			for (rarg=0, larg=aargv; !rarg && (larg
85f619
				if (!strcmp(*larg,*parg))
85f619
					rarg = larg;
85f619
85f619
			/* repeated -L argument? */
85f619
			if (rarg) {
85f619
				parg++;
85f619
			} else {
85f619
				*aarg++ = *parg++;
85f619
			}
85f619
890281
		/* placeholder argument? */
890281
		} else if (!strncmp(*parg,"-USLIBTOOL_PLACEHOLDER_",23)) {
890281
			parg++;
890281
890281
		/* all other arguments */
890281
		} else {
890281
			*aarg++ = *parg++;
c6d4dc
		}
c6d4dc
	}
671454
c6d4dc
	/* (program name) */
c6d4dc
	dst = &base[1];
671454
c6d4dc
	/* join object args */
c6d4dc
	src = oargv;
c6d4dc
	cap = oarg;
671454
c6d4dc
	for (; src
c6d4dc
		*dst++ = *src++;
671454
c6d4dc
	/* join all other args */
c6d4dc
	src = aargv;
c6d4dc
	cap = aarg;
671454
c6d4dc
	for (; src
c6d4dc
		*dst++ = *src++;
671454
2480bd
	/* properly null-terminate argv, accounting for redundant -l arguments */
2480bd
	*dst = 0;
2480bd
671454
	/* output annotation */
c6d4dc
	if (ectx->lout[0]) {
c6d4dc
		ectx->lout[0] = &base[1] + (oarg - oargv) + (ectx->lout[0] - aargv);
c6d4dc
		ectx->lout[1] = ectx->lout[0] + 1;
671454
	}
671454
c6d4dc
	if (ectx->mout[0]) {
c6d4dc
		ectx->mout[0] = &base[1] + (oarg - oargv) + (ectx->mout[0] - aargv);
c6d4dc
		ectx->mout[1] = ectx->mout[0] + 1;
671454
	}
671454
671454
	/* all done */
671454
	if (sargvbuf)
671454
		free(sargvbuf);
671454
671454
	return 0;
671454
}
671454
628cde
static int slbt_exec_link_remove_file(
628cde
	const struct slbt_driver_ctx *	dctx,
628cde
	struct slbt_exec_ctx *		ectx,
628cde
	const char *			target)
628cde
{
d3ca02
	(void)ectx;
d3ca02
628cde
	/* remove target (if any) */
628cde
	if (!(unlink(target)) || (errno == ENOENT))
628cde
		return 0;
628cde
017c47
	return SLBT_SYSTEM_ERROR(dctx);
628cde
}
628cde
0913c3
static int slbt_exec_link_create_dep_file(
9c9f28
	const struct slbt_driver_ctx *	dctx,
618537
	struct slbt_exec_ctx *		ectx,
618537
	char **				altv,
618537
	const char *			libfilename,
618537
	bool				farchive)
0913c3
{
c178e3
	int			ret;
c178e3
	int			deps;
5fac6c
	int			fdcwd;
c178e3
	char **			parg;
c178e3
	char *			popt;
c178e3
	char *			plib;
c178e3
	char *			path;
c178e3
	char *			mark;
c178e3
	char *			base;
c178e3
	size_t			size;
c178e3
	size_t			slen;
c178e3
	char			deplib [PATH_MAX];
c178e3
	char			reladir[PATH_MAX];
c178e3
	char			depfile[PATH_MAX];
c178e3
	struct stat		st;
c178e3
	int			ldepth;
c178e3
	int			fdyndep;
c178e3
	int			fnodeps;
c178e3
	struct slbt_map_info *  mapinfo;
0913c3
5fac6c
	/* fdcwd */
5fac6c
	fdcwd = slbt_driver_fdcwd(dctx);
5fac6c
c178e3
	/* depfile */
c178e3
	slen = snprintf(depfile,sizeof(depfile),
c178e3
		"%s.slibtool.deps",
c178e3
		libfilename);
c178e3
c178e3
	if (slen >= sizeof(depfile))
9c9f28
		return SLBT_BUFFER_ERROR(dctx);
0913c3
c178e3
	/* deps */
5fac6c
	if ((deps = openat(fdcwd,depfile,O_RDWR|O_CREAT|O_TRUNC,0644)) < 0)
9c9f28
		return SLBT_SYSTEM_ERROR(dctx);
0913c3
c178e3
	/* iterate */
0913c3
	for (parg=altv; *parg; parg++) {
c178e3
		popt    = 0;
c178e3
		plib    = 0;
c178e3
		path    = 0;
c178e3
		mapinfo = 0;
0913c3
ecd300
		if (!strncmp(*parg,"-l",2)) {
0913c3
			popt = *parg;
0913c3
			plib = popt + 2;
ecd300
40a467
		} else if (!strncmp(*parg,"-L",2)) {
40a467
			popt = *parg;
40a467
			path = popt + 2;
ecd300
4f56fd
		} else if (!strncmp(*parg,"-f",2)) {
4f56fd
			(void)0;
ecd300
fe97e7
		} else if ((popt = strrchr(*parg,'.')) && !strcmp(popt,".la")) {
fe97e7
			/* import dependency list */
fe97e7
			if ((base = strrchr(*parg,'/')))
fe97e7
				base++;
fe97e7
			else
fe97e7
				base = *parg;
fe97e7
6fda2b
			/* [relative .la directory] */
6fda2b
			if (base > *parg) {
c178e3
				slen = snprintf(reladir,sizeof(reladir),
c178e3
					"%s",*parg);
c178e3
c178e3
				if (slen >= sizeof(reladir)) {
c178e3
					close(deps);
c178e3
					return SLBT_BUFFER_ERROR(dctx);
c178e3
				}
6fda2b
6fda2b
				reladir[base - *parg - 1] = 0;
6fda2b
			} else {
6fda2b
				reladir[0] = '.';
6fda2b
				reladir[1] = 0;
6fda2b
			}
6fda2b
39f31e
39f31e
			/* dynamic library dependency? */
39f31e
			strcpy(depfile,*parg);
39f31e
			mark = depfile + (base - *parg);
39f31e
			size = sizeof(depfile) - (base - *parg);
c178e3
			slen = snprintf(mark,size,".libs/%s",base);
39f31e
c178e3
			if (slen >= size) {
c178e3
				close(deps);
39f31e
				return SLBT_BUFFER_ERROR(dctx);
c178e3
			}
39f31e
39f31e
			mark = strrchr(mark,'.');
6527a9
			strcpy(mark,dctx->cctx->settings.dsosuffix);
39f31e
39f31e
			fdyndep = !stat(depfile,&st);
fb50c9
			fnodeps = farchive && fdyndep;
39f31e
6fda2b
			/* [-L... as needed] */
39f31e
			if (fdyndep && (base > *parg) && (ectx->ldirdepth >= 0)) {
c178e3
				if (slbt_dprintf(deps,"-L") < 0) {
c178e3
					close(deps);
7f35de
					return SLBT_SYSTEM_ERROR(dctx);
c178e3
				}
7f35de
c178e3
				for (ldepth=ectx->ldirdepth; ldepth; ldepth--) {
c178e3
					if (slbt_dprintf(deps,"../") < 0) {
c178e3
						close(deps);
7f35de
						return SLBT_SYSTEM_ERROR(dctx);
c178e3
					}
c178e3
				}
7f35de
c178e3
				if (slbt_dprintf(deps,"%s/.libs\n",reladir) < 0) {
c178e3
					close(deps);
6fda2b
					return SLBT_SYSTEM_ERROR(dctx);
c178e3
				}
6fda2b
			}
6fda2b
09421c
			/* -ldeplib */
39f31e
			if (fdyndep) {
1a8018
				*popt = 0;
1a8018
				mark  = base;
1a8018
				mark += strlen(dctx->cctx->settings.dsoprefix);
09421c
c178e3
				if (slbt_dprintf(deps,"-l%s\n",mark) < 0) {
c178e3
					close(deps);
1a8018
					return SLBT_SYSTEM_ERROR(dctx);
c178e3
				}
09421c
1a8018
				*popt = '.';
1a8018
			}
09421c
6fda2b
			/* [open dependency list] */
fe97e7
			strcpy(depfile,*parg);
fe97e7
			mark = depfile + (base - *parg);
fe97e7
			size = sizeof(depfile) - (base - *parg);
c178e3
			slen = snprintf(mark,size,".libs/%s",base);
fe97e7
c178e3
			if (slen >= size) {
c178e3
				close(deps);
9c9f28
				return SLBT_BUFFER_ERROR(dctx);
c178e3
			}
fe97e7
fe97e7
			mark = strrchr(mark,'.');
fe97e7
			size = sizeof(depfile) - (mark - depfile);
fe97e7
1a8018
			if (!farchive) {
c178e3
				slen = snprintf(mark,size,
c178e3
					"%s.slibtool.deps",
c178e3
					dctx->cctx->settings.dsosuffix);
c178e3
c178e3
				if (slen >= size) {
c178e3
					close(deps);
1a8018
					return SLBT_BUFFER_ERROR(dctx);
c178e3
				}
1a8018
c178e3
				mapinfo = slbt_map_file(
5fac6c
					fdcwd,depfile,
c178e3
					SLBT_MAP_INPUT);
c178e3
c178e3
				if (!mapinfo && (errno != ENOENT)) {
c178e3
					close(deps);
c178e3
					return SLBT_SYSTEM_ERROR(dctx);
1a8018
				}
1a8018
			}
fe97e7
c178e3
			if (!mapinfo && !fnodeps) {
c178e3
				slen = snprintf(mark,size,
c178e3
					".a.slibtool.deps");
c178e3
c178e3
				if (slen >= size) {
c178e3
					close(deps);
1a8018
					return SLBT_BUFFER_ERROR(dctx);
c178e3
				}
fe97e7
c178e3
				mapinfo = slbt_map_file(
5fac6c
					fdcwd,depfile,
c178e3
					SLBT_MAP_INPUT);
1a8018
c178e3
				if (!mapinfo) {
c178e3
					close(deps);
1a8018
					return SLBT_SYSTEM_ERROR(dctx);
c178e3
				}
1a8018
			}
fe97e7
6fda2b
			/* [-l... as needed] */
c178e3
			while (mapinfo && (mapinfo->mark < mapinfo->cap)) {
c178e3
				ret = slbt_mapped_readline(
c178e3
					dctx,mapinfo,
c178e3
					deplib,sizeof(deplib));
c178e3
c178e3
				if (ret) {
c178e3
					close(deps);
c178e3
					return SLBT_NESTED_ERROR(dctx);
e49ee9
				}
fe97e7
c178e3
				ret = ((deplib[0] == '-')
c178e3
						&& (deplib[1] == 'L')
c178e3
						&& (deplib[2] != '/'))
c178e3
					? slbt_dprintf(
c178e3
						deps,"-L%s/%s",
c178e3
						reladir,&deplib[2])
c178e3
					: slbt_dprintf(
c178e3
						deps,"%s",
c178e3
						deplib);
c178e3
c178e3
				if (ret < 0) {
c178e3
					close(deps);
c178e3
					return SLBT_SYSTEM_ERROR(dctx);
c178e3
				}
fe97e7
			}
e49ee9
c178e3
			if (mapinfo)
c178e3
				slbt_unmap_file(mapinfo);
0913c3
		}
0913c3
c178e3
		if (plib && (slbt_dprintf(deps,"-l%s\n",plib) < 0)) {
c178e3
			close(deps);
c178e3
			return SLBT_SYSTEM_ERROR(dctx);
c178e3
		}
40a467
c178e3
		if (path && (slbt_dprintf(deps,"-L%s\n",path) < 0)) {
c178e3
			close(deps);
c178e3
			return SLBT_SYSTEM_ERROR(dctx);
c178e3
		}
0913c3
	}
0913c3
0913c3
	return 0;
0913c3
}
0913c3
9112ac
static int slbt_exec_link_create_import_library(
9112ac
	const struct slbt_driver_ctx *	dctx,
9112ac
	struct slbt_exec_ctx *		ectx,
9112ac
	char *				impfilename,
9112ac
	char *				deffilename,
4fdf35
	char *				soname,
4fdf35
	bool				ftag)
9112ac
{
bfe950
	int	fmdso;
4fdf35
	char *	slash;
bfe950
	char *	eargv[8];
9112ac
	char	program[PATH_MAX];
4fdf35
	char	hosttag[PATH_MAX];
4fdf35
	char	hostlnk[PATH_MAX];
4fdf35
4fdf35
	/* libfoo.so.def.{flavor} */
4fdf35
	if (ftag) {
4fdf35
		if ((size_t)snprintf(hosttag,sizeof(hosttag),"%s.%s",
4fdf35
				deffilename,
4fdf35
				dctx->cctx->host.flavor) >= sizeof(hosttag))
9c9f28
			return SLBT_BUFFER_ERROR(dctx);
4fdf35
4fdf35
		if ((size_t)snprintf(hostlnk,sizeof(hostlnk),"%s.host",
4fdf35
				deffilename) >= sizeof(hostlnk))
9c9f28
			return SLBT_BUFFER_ERROR(dctx);
4fdf35
4fdf35
		/* libfoo.so.def is under .libs/ */
4fdf35
		if (!(slash = strrchr(deffilename,'/')))
Kylie McClain 7ce25c
			return SLBT_CUSTOM_ERROR(dctx,SLBT_ERR_LINK_FLOW);
4fdf35
4fdf35
		if (slbt_create_symlink(
4fdf35
				dctx,ectx,
4fdf35
				deffilename,
4fdf35
				hosttag,
4fdf35
				false))
9c9f28
			return SLBT_NESTED_ERROR(dctx);
4fdf35
4fdf35
		/* libfoo.so.def.{flavor} is under .libs/ */
4fdf35
		if (!(slash = strrchr(hosttag,'/')))
Kylie McClain 7ce25c
			return SLBT_CUSTOM_ERROR(dctx,SLBT_ERR_LINK_FLOW);
4fdf35
4fdf35
		if (slbt_create_symlink(
4fdf35
				dctx,ectx,
4fdf35
				++slash,
4fdf35
				hostlnk,
4fdf35
				false))
9c9f28
			return SLBT_NESTED_ERROR(dctx);
4fdf35
	}
9112ac
bfe950
	/* dlltool or mdso? */
bfe950
	if (dctx->cctx->drvflags & SLBT_DRIVER_IMPLIB_DSOMETA)
bfe950
		fmdso = 1;
bfe950
bfe950
	else if (dctx->cctx->drvflags & SLBT_DRIVER_IMPLIB_DSOMETA)
bfe950
		fmdso = 0;
bfe950
bfe950
	else if (!(strcmp(dctx->cctx->host.flavor,"midipix")))
bfe950
		fmdso = 1;
9112ac
bfe950
	else
bfe950
		fmdso = 0;
bfe950
bfe950
	/* eargv */
bfe950
	if (fmdso) {
bfe950
		if ((size_t)snprintf(program,sizeof(program),"%s",
bfe950
				dctx->cctx->host.mdso) >= sizeof(program))
bfe950
			return SLBT_BUFFER_ERROR(dctx);
bfe950
bfe950
		eargv[0] = program;
bfe950
		eargv[1] = "-i";
bfe950
		eargv[2] = impfilename;
bfe950
		eargv[3] = "-n";
bfe950
		eargv[4] = soname;
bfe950
		eargv[5] = deffilename;
bfe950
		eargv[6] = 0;
bfe950
	} else {
bfe950
		if ((size_t)snprintf(program,sizeof(program),"%s",
bfe950
				dctx->cctx->host.dlltool) >= sizeof(program))
bfe950
			return SLBT_BUFFER_ERROR(dctx);
bfe950
bfe950
		eargv[0] = program;
bfe950
		eargv[1] = "-l";
bfe950
		eargv[2] = impfilename;
bfe950
		eargv[3] = "-d";
bfe950
		eargv[4] = deffilename;
bfe950
		eargv[5] = "-D";
bfe950
		eargv[6] = soname;
bfe950
		eargv[7] = 0;
bfe950
	}
9112ac
9112ac
	/* alternate argument vector */
bfe950
	ectx->argv    = eargv;
9112ac
	ectx->program = program;
9112ac
9112ac
	/* step output */
9112ac
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
9112ac
		if (slbt_output_link(dctx,ectx))
9c9f28
			return SLBT_NESTED_ERROR(dctx);
9112ac
bfe950
	/* dlltool/mdso spawn */
9112ac
	if ((slbt_spawn(ectx,true) < 0) || ectx->exitcode)
9c9f28
		return SLBT_SPAWN_ERROR(dctx);
9112ac
9112ac
	return 0;
9112ac
}
9112ac
ec5e56
static int slbt_exec_link_create_noop_symlink(
ec5e56
	const struct slbt_driver_ctx *	dctx,
ec5e56
	struct slbt_exec_ctx *		ectx,
ec5e56
	const char *			arfilename)
ec5e56
{
ec5e56
	struct stat st;
ec5e56
ec5e56
	/* file exists? */
ec5e56
	if (!(lstat(arfilename,&st)))
ec5e56
		return 0;
ec5e56
ec5e56
	/* needed? */
ec5e56
	if (errno == ENOENT) {
ec5e56
		if (slbt_create_symlink(
ec5e56
				dctx,ectx,
ec5e56
				"/dev/null",
ec5e56
				arfilename,
ec5e56
                                false))
ec5e56
			return SLBT_NESTED_ERROR(dctx);
ec5e56
		return 0;
ec5e56
	}
ec5e56
ec5e56
	return SLBT_SYSTEM_ERROR(dctx);
ec5e56
}
ec5e56
3b5e3d
static int slbt_exec_link_create_archive(
5cc3b3
	const struct slbt_driver_ctx *	dctx,
a51ace
	struct slbt_exec_ctx *		ectx,
3b5e3d
	const char *			arfilename,
02ae4d
	bool				fpic,
02ae4d
	bool				fprimary)
5cc3b3
{
2fd6a3
	int		fdcwd;
5cc3b3
	char ** 	aarg;
5cc3b3
	char ** 	parg;
02ae4d
	char *		base;
02ae4d
	char *		mark;
02ae4d
	char *		slash;
3c594d
	char		program[PATH_MAX];
3c594d
	char		output [PATH_MAX];
02ae4d
	char		arfile [PATH_MAX];
02ae4d
	char		arlink [PATH_MAX];
5cc3b3
175362
	/* -disable-static? */
175362
	if (dctx->cctx->drvflags & SLBT_DRIVER_DISABLE_STATIC)
Felix Janda a64776
		if (dctx->cctx->rpath)
ec5e56
			return slbt_exec_link_create_noop_symlink(
ec5e56
				dctx,ectx,arfilename);
175362
e7ddb2
	/* initial state */
e7ddb2
	slbt_reset_arguments(ectx);
e7ddb2
5cc3b3
	/* placeholders */
5cc3b3
	slbt_reset_placeholders(ectx);
5cc3b3
5cc3b3
	/* alternate program (ar, ranlib) */
5cc3b3
	ectx->program = program;
5cc3b3
a51ace
	/* output */
a51ace
	if ((size_t)snprintf(output,sizeof(output),"%s",
a51ace
			arfilename) >= sizeof(output))
9c9f28
		return SLBT_BUFFER_ERROR(dctx);
a51ace
5cc3b3
	/* ar alternate argument vector */
5cc3b3
	if ((size_t)snprintf(program,sizeof(program),"%s",
5cc3b3
			dctx->cctx->host.ar) >= sizeof(program))
9c9f28
		return SLBT_BUFFER_ERROR(dctx);
5cc3b3
2fd6a3
2fd6a3
	/* fdcwd */
2fd6a3
	fdcwd   = slbt_driver_fdcwd(dctx);
2fd6a3
2fd6a3
	/* input argument adjustment */
5cc3b3
	aarg    = ectx->altv;
5cc3b3
	*aarg++ = program;
0330cf
	*aarg++ = "crs";
a51ace
	*aarg++ = output;
5cc3b3
5cc3b3
	for (parg=ectx->cargv; *parg; parg++)
2fd6a3
		if (slbt_adjust_object_argument(*parg,fpic,!fpic,fdcwd))
5cc3b3
			*aarg++ = *parg;
5cc3b3
5cc3b3
	*aarg = 0;
5cc3b3
	ectx->argv = ectx->altv;
5cc3b3
5cc3b3
	/* step output */
5cc3b3
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
5cc3b3
		if (slbt_output_link(dctx,ectx))
9c9f28
			return SLBT_NESTED_ERROR(dctx);
5cc3b3
628cde
	/* remove old archive as needed */
628cde
	if (slbt_exec_link_remove_file(dctx,ectx,output))
9c9f28
		return SLBT_NESTED_ERROR(dctx);
628cde
0913c3
	/* .deps */
e3d03d
	if (slbt_exec_link_create_dep_file(
e3d03d
			dctx,ectx,ectx->cargv,
e3d03d
			arfilename,true))
9c9f28
		return SLBT_NESTED_ERROR(dctx);
0913c3
5cc3b3
	/* ar spawn */
5cc3b3
	if ((slbt_spawn(ectx,true) < 0) || ectx->exitcode)
9c9f28
		return SLBT_SPAWN_ERROR(dctx);
69bc4a
69bc4a
	/* input objects associated with .la archives */
69bc4a
	for (parg=ectx->cargv; *parg; parg++)
932031
		if (slbt_adjust_wrapper_argument(*parg,true))
69bc4a
			if (slbt_archive_import(dctx,ectx,output,*parg))
9c9f28
				return SLBT_NESTED_ERROR(dctx);
5cc3b3
02ae4d
	if (fprimary && (dctx->cctx->drvflags & SLBT_DRIVER_DISABLE_SHARED)) {
02ae4d
		strcpy(arlink,output);
02ae4d
		mark  = strrchr(arlink,'/');
02ae4d
		*mark = 0;
02ae4d
02ae4d
		base  = output + (mark - arlink);
02ae4d
		base++;
02ae4d
02ae4d
		if ((slash = strrchr(arlink,'/')))
02ae4d
			slash++;
02ae4d
		else
02ae4d
			slash = arlink;
02ae4d
02ae4d
		strcpy(slash,base);
02ae4d
		sprintf(arfile,".libs/%s",base);
02ae4d
02ae4d
		if (slbt_exec_link_remove_file(dctx,ectx,arlink))
9c9f28
			return SLBT_NESTED_ERROR(dctx);
02ae4d
02ae4d
		if (symlink(arfile,arlink))
9c9f28
			return SLBT_SYSTEM_ERROR(dctx);
02ae4d
	}
02ae4d
5cc3b3
	return 0;
5cc3b3
}
5cc3b3
b3940a
static int slbt_exec_link_create_library(
b3940a
	const struct slbt_driver_ctx *	dctx,
b3940a
	struct slbt_exec_ctx *		ectx,
08f5f9
	const char *			dsobasename,
112a2b
	const char *			dsofilename,
112a2b
	const char *			relfilename)
b3940a
{
2fd6a3
	int	fdcwd;
b3940a
	char ** parg;
46aa6f
	char ** xarg;
79c501
	char	cwd    [PATH_MAX];
b3940a
	char	output [PATH_MAX];
0f8591
	char	soname [PATH_MAX];
fbb80b
	char	symfile[PATH_MAX];
bd9cc9
	struct slbt_deps_meta depsmeta = {0,0,0,0};
b3940a
b3940a
	/* initial state */
b3940a
	slbt_reset_arguments(ectx);
b3940a
b3940a
	/* placeholders */
b3940a
	slbt_reset_placeholders(ectx);
b3940a
2fd6a3
	/* fdcwd */
2fd6a3
	fdcwd = slbt_driver_fdcwd(dctx);
2fd6a3
b3940a
	/* input argument adjustment */
b3940a
	for (parg=ectx->cargv; *parg; parg++)
2fd6a3
		slbt_adjust_object_argument(*parg,true,false,fdcwd);
b3940a
6fda2b
	/* .deps */
e3d03d
	if (slbt_exec_link_create_dep_file(
e3d03d
			dctx,ectx,ectx->cargv,
e3d03d
			dsofilename,false))
6fda2b
		return slbt_exec_link_exit(
6fda2b
			&depsmeta,
6fda2b
			SLBT_NESTED_ERROR(dctx));
6fda2b
b3940a
	/* linker argument adjustment */
46aa6f
	for (parg=ectx->cargv, xarg=ectx->xargv; *parg; parg++, xarg++)
afaba2
		if (slbt_adjust_linker_argument(
9c9f28
				dctx,
46aa6f
				*parg,xarg,true,
afaba2
				dctx->cctx->settings.dsosuffix,
8b7d50
				dctx->cctx->settings.arsuffix,
8b7d50
				&depsmeta) < 0)
9c9f28
			return SLBT_NESTED_ERROR(dctx);
b3940a
b3940a
	/* --no-undefined */
b3940a
	if (dctx->cctx->drvflags & SLBT_DRIVER_NO_UNDEFINED)
b3940a
		*ectx->noundef = "-Wl,--no-undefined";
b3940a
0f8591
	/* -soname */
b1778b
	if ((dctx->cctx->drvflags & SLBT_DRIVER_IMAGE_MACHO)) {
b1778b
		(void)0;
600957
600957
	} else if (relfilename && dctx->cctx->verinfo.verinfo) {
08f5f9
		if ((size_t)snprintf(soname,sizeof(soname),"-Wl,%s%s-%s%s.%d%s",
2f9f52
					ectx->sonameprefix,
867d1e
					dctx->cctx->libname,
600957
					dctx->cctx->release,
08f5f9
					dctx->cctx->settings.osdsuffix,
08f5f9
					dctx->cctx->verinfo.major,
08f5f9
					dctx->cctx->settings.osdfussix)
867d1e
				>= sizeof(soname))
9c9f28
			return SLBT_BUFFER_ERROR(dctx);
0f8591
867d1e
		*ectx->soname  = "-Wl,-soname";
867d1e
		*ectx->lsoname = soname;
600957
112a2b
	} else if (relfilename) {
112a2b
		if ((size_t)snprintf(soname,sizeof(soname),"-Wl,%s%s-%s%s",
2f9f52
					ectx->sonameprefix,
112a2b
					dctx->cctx->libname,
112a2b
					dctx->cctx->release,
112a2b
					dctx->cctx->settings.dsosuffix)
112a2b
				>= sizeof(soname))
9c9f28
			return SLBT_BUFFER_ERROR(dctx);
112a2b
112a2b
		*ectx->soname  = "-Wl,-soname";
112a2b
		*ectx->lsoname = soname;
600957
600957
	} else if (!(dctx->cctx->drvflags & SLBT_DRIVER_AVOID_VERSION)) {
08f5f9
		if ((size_t)snprintf(soname,sizeof(soname),"-Wl,%s%s%s.%d%s",
600957
					ectx->sonameprefix,
600957
					dctx->cctx->libname,
08f5f9
					dctx->cctx->settings.osdsuffix,
08f5f9
					dctx->cctx->verinfo.major,
08f5f9
					dctx->cctx->settings.osdfussix)
600957
				>= sizeof(soname))
600957
			return SLBT_BUFFER_ERROR(dctx);
600957
600957
		*ectx->soname  = "-Wl,-soname";
600957
		*ectx->lsoname = soname;
867d1e
	}
0f8591
fbb80b
	/* PE: --output-def */
fbb80b
	if (dctx->cctx->drvflags & SLBT_DRIVER_IMAGE_PE) {
fbb80b
		if ((size_t)snprintf(symfile,sizeof(symfile),"-Wl,%s",
fbb80b
					ectx->deffilename)
fbb80b
				>= sizeof(output))
9c9f28
			return SLBT_BUFFER_ERROR(dctx);
fbb80b
fbb80b
		*ectx->symdefs = "-Wl,--output-def";
fbb80b
		*ectx->symfile = symfile;
fbb80b
	}
fbb80b
372423
	/* shared/static */
372423
	if (dctx->cctx->drvflags & SLBT_DRIVER_ALL_STATIC) {
372423
		*ectx->dpic = "-static";
11e277
	} else if (dctx->cctx->settings.picswitch) {
11e277
		*ectx->dpic = "-shared";
11e277
		*ectx->fpic = dctx->cctx->settings.picswitch;
372423
	} else {
372423
		*ectx->dpic = "-shared";
372423
	}
b3940a
b3940a
	/* output */
112a2b
	if (relfilename) {
112a2b
		strcpy(output,relfilename);
112a2b
	} else if (dctx->cctx->drvflags & SLBT_DRIVER_AVOID_VERSION) {
867d1e
		strcpy(output,dsofilename);
867d1e
	} else {
08f5f9
		if ((size_t)snprintf(output,sizeof(output),"%s%s.%d.%d.%d%s",
08f5f9
					dsobasename,
08f5f9
					dctx->cctx->settings.osdsuffix,
867d1e
					dctx->cctx->verinfo.major,
867d1e
					dctx->cctx->verinfo.minor,
08f5f9
					dctx->cctx->verinfo.revision,
08f5f9
					dctx->cctx->settings.osdfussix)
867d1e
				>= sizeof(output))
9c9f28
			return SLBT_BUFFER_ERROR(dctx);
867d1e
	}
b3940a
b3940a
	*ectx->lout[0] = "-o";
b3940a
	*ectx->lout[1] = output;
b3940a
0f5ca7
	/* ldrpath */
0f5ca7
	if (dctx->cctx->host.ldrpath) {
0f5ca7
		if (slbt_exec_link_remove_file(dctx,ectx,ectx->rpathfilename))
9c9f28
			return SLBT_NESTED_ERROR(dctx);
0f5ca7
0f5ca7
		if (symlink(dctx->cctx->host.ldrpath,ectx->rpathfilename))
9c9f28
			return SLBT_SYSTEM_ERROR(dctx);
0f5ca7
	}
0f5ca7
79c501
	/* cwd */
79c501
	if (!getcwd(cwd,sizeof(cwd)))
9c9f28
		return SLBT_SYSTEM_ERROR(dctx);
79c501
2ef5be
	/* .libs/libfoo.so --> -L.libs -lfoo */
2ef5be
	if (slbt_exec_link_adjust_argument_vector(
3be47d
			dctx,ectx,&depsmeta,cwd,true))
9c9f28
		return SLBT_NESTED_ERROR(dctx);
2ef5be
2ef5be
	/* using alternate argument vector */
3be47d
	ectx->argv    = depsmeta.altv;
3be47d
	ectx->program = depsmeta.altv[0];
2ef5be
671454
	/* sigh */
671454
	if (slbt_exec_link_finalize_argument_vector(dctx,ectx))
671454
		return SLBT_NESTED_ERROR(dctx);
671454
b3940a
	/* step output */
b3940a
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
b3940a
		if (slbt_output_link(dctx,ectx))
9c9f28
			return slbt_exec_link_exit(
9c9f28
				&depsmeta,
9c9f28
				SLBT_NESTED_ERROR(dctx));
b3940a
b3940a
	/* spawn */
b3940a
	if ((slbt_spawn(ectx,true) < 0) || ectx->exitcode)
9c9f28
		return slbt_exec_link_exit(
9c9f28
			&depsmeta,
9c9f28
			SLBT_SPAWN_ERROR(dctx));
b3940a
3be47d
	return slbt_exec_link_exit(&depsmeta,0);
b3940a
}
b3940a
f6ccbe
static int slbt_exec_link_create_executable(
f6ccbe
	const struct slbt_driver_ctx *	dctx,
f6ccbe
	struct slbt_exec_ctx *		ectx,
f6ccbe
	const char *			exefilename)
f6ccbe
{
5fac6c
	int	fdcwd;
9706fa
	int	fdwrap;
f6ccbe
	char ** parg;
46aa6f
	char ** xarg;
e9dbdf
	char *	base;
5ee0d1
	char	cwd    [PATH_MAX];
f6ccbe
	char	output [PATH_MAX];
5ee0d1
	char	wrapper[PATH_MAX];
5e5804
	char	wraplnk[PATH_MAX];
238670
	bool	fabspath;
372423
	bool	fpic;
dac9cd
	const struct slbt_source_version * verinfo;
bd9cc9
	struct slbt_deps_meta depsmeta = {0,0,0,0};
f6ccbe
f6ccbe
	/* initial state */
f6ccbe
	slbt_reset_arguments(ectx);
f6ccbe
f6ccbe
	/* placeholders */
f6ccbe
	slbt_reset_placeholders(ectx);
f6ccbe
5fac6c
	/* fdcwd */
5fac6c
	fdcwd = slbt_driver_fdcwd(dctx);
5fac6c
372423
	/* fpic */
372423
	fpic = !(dctx->cctx->drvflags & SLBT_DRIVER_ALL_STATIC);
372423
f6ccbe
	/* input argument adjustment */
f6ccbe
	for (parg=ectx->cargv; *parg; parg++)
2fd6a3
		slbt_adjust_object_argument(*parg,fpic,true,fdcwd);
f6ccbe
f6ccbe
	/* linker argument adjustment */
46aa6f
	for (parg=ectx->cargv, xarg=ectx->xargv; *parg; parg++, xarg++)
afaba2
		if (slbt_adjust_linker_argument(
9c9f28
				dctx,
46aa6f
				*parg,xarg,true,
afaba2
				dctx->cctx->settings.dsosuffix,
8b7d50
				dctx->cctx->settings.arsuffix,
8b7d50
				&depsmeta) < 0)
9c9f28
			return SLBT_NESTED_ERROR(dctx);
f6ccbe
f6ccbe
	/* --no-undefined */
f6ccbe
	if (dctx->cctx->drvflags & SLBT_DRIVER_NO_UNDEFINED)
f6ccbe
		*ectx->noundef = "-Wl,--no-undefined";
f6ccbe
9706fa
	/* executable wrapper: create */
9706fa
	if ((size_t)snprintf(wrapper,sizeof(wrapper),
9706fa
				"%s.wrapper.tmp",
5ee0d1
				dctx->cctx->output)
5ee0d1
			>= sizeof(wrapper))
9c9f28
		return SLBT_BUFFER_ERROR(dctx);
5ee0d1
5fac6c
	if ((fdwrap = openat(fdcwd,wrapper,O_RDWR|O_CREAT|O_TRUNC,0644)) < 0)
9c9f28
		return SLBT_SYSTEM_ERROR(dctx);
5ee0d1
9706fa
	slbt_exec_set_fdwrapper(ectx,fdwrap);
9706fa
9706fa
	/* executable wrapper: header */
dac9cd
	verinfo = slbt_source_version();
dac9cd
9706fa
	if (slbt_dprintf(fdwrap,
5ee0d1
			"#!/bin/sh\n"
821d55
			"# libtool compatible executable wrapper\n"
dac9cd
			"# Generated by %s (slibtool %d.%d.%d)\n"
dac9cd
			"# [commit reference: %s]\n\n"
1f87fd
5ee0d1
			"if [ -z \"$%s\" ]; then\n"
5ee0d1
			"\tDL_PATH=\n"
5ee0d1
			"\tCOLON=\n"
5ee0d1
			"\tLCOLON=\n"
5ee0d1
			"else\n"
5ee0d1
			"\tDL_PATH=\n"
5ee0d1
			"\tCOLON=\n"
5ee0d1
			"\tLCOLON=':'\n"
5ee0d1
			"fi\n\n",
1f87fd
1f87fd
			dctx->program,
dac9cd
			verinfo->major,verinfo->minor,verinfo->revision,
dac9cd
			verinfo->commit,
5ee0d1
			dctx->cctx->settings.ldpathenv) < 0)
9c9f28
		return SLBT_SYSTEM_ERROR(dctx);
5ee0d1
f6ccbe
	/* output */
f6ccbe
	if ((size_t)snprintf(output,sizeof(output),"%s",
f6ccbe
				exefilename)
f6ccbe
			>= sizeof(output))
9c9f28
		return SLBT_BUFFER_ERROR(dctx);
f6ccbe
f6ccbe
	*ectx->lout[0] = "-o";
f6ccbe
	*ectx->lout[1] = output;
f6ccbe
e5d83d
	/* static? */
e5d83d
	if (dctx->cctx->drvflags & SLBT_DRIVER_ALL_STATIC)
e5d83d
		*ectx->dpic = "-static";
e5d83d
79c501
	/* cwd */
79c501
	if (!getcwd(cwd,sizeof(cwd)))
9c9f28
		return SLBT_SYSTEM_ERROR(dctx);
79c501
2ef5be
	/* .libs/libfoo.so --> -L.libs -lfoo */
2ef5be
	if (slbt_exec_link_adjust_argument_vector(
3be47d
			dctx,ectx,&depsmeta,cwd,false))
9c9f28
		return SLBT_NESTED_ERROR(dctx);
2ef5be
2ef5be
	/* using alternate argument vector */
3be47d
	ectx->argv    = depsmeta.altv;
3be47d
	ectx->program = depsmeta.altv[0];
2ef5be
4a0a6c
	/* executable wrapper symlink */
4a0a6c
	if ((size_t)snprintf(wraplnk,sizeof(wraplnk),"%s.exe.wrapper",
4a0a6c
			dctx->cctx->output) >= sizeof(wraplnk))
9c9f28
		return slbt_exec_link_exit(
9c9f28
			&depsmeta,
9c9f28
			SLBT_BUFFER_ERROR(dctx));
4a0a6c
e9dbdf
	/* executable wrapper: base name */
4a0a6c
	if ((base = strrchr(wraplnk,'/')))
e9dbdf
		base++;
e9dbdf
	else
4a0a6c
		base = wraplnk;
e9dbdf
5ee0d1
	/* executable wrapper: footer */
238670
	fabspath = (exefilename[0] == '/');
238670
9706fa
	if (slbt_dprintf(fdwrap,
5ee0d1
			"DL_PATH=\"$DL_PATH$LCOLON$%s\"\n\n"
5ee0d1
			"export %s=$DL_PATH\n\n"
4a0a6c
			"if [ `basename \"$0\"` = \"%s\" ]; then\n"
e9dbdf
			"\tprogram=\"$1\"; shift\n"
e9dbdf
			"\texec \"$program\" \"$@\"\n"
e9dbdf
			"fi\n\n"
5ee0d1
			"exec %s/%s \"$@\"\n",
5ee0d1
			dctx->cctx->settings.ldpathenv,
5ee0d1
			dctx->cctx->settings.ldpathenv,
e9dbdf
			base,
238670
			fabspath ? "" : cwd,
238670
			fabspath ? &exefilename[1] : exefilename) < 0)
9c9f28
		return slbt_exec_link_exit(
9c9f28
			&depsmeta,
9c9f28
			SLBT_SYSTEM_ERROR(dctx));
5ee0d1
671454
	/* sigh */
671454
	if (slbt_exec_link_finalize_argument_vector(dctx,ectx))
671454
		return SLBT_NESTED_ERROR(dctx);
671454
f6ccbe
	/* step output */
f6ccbe
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
f6ccbe
		if (slbt_output_link(dctx,ectx))
9c9f28
			return slbt_exec_link_exit(
9c9f28
				&depsmeta,
9c9f28
				SLBT_NESTED_ERROR(dctx));
f6ccbe
f6ccbe
	/* spawn */
f6ccbe
	if ((slbt_spawn(ectx,true) < 0) || ectx->exitcode)
9c9f28
		return slbt_exec_link_exit(
9c9f28
			&depsmeta,
9c9f28
			SLBT_SPAWN_ERROR(dctx));
f6ccbe
5ee0d1
	/* executable wrapper: finalize */
9706fa
	slbt_exec_close_fdwrapper(ectx);
5ee0d1
5e5804
	if (slbt_create_symlink(
5e5804
			dctx,ectx,
5e5804
			dctx->cctx->output,wraplnk,
5e5804
			false))
9c9f28
		return slbt_exec_link_exit(
9c9f28
			&depsmeta,
9c9f28
			SLBT_NESTED_ERROR(dctx));
5e5804
5ee0d1
	if (rename(wrapper,dctx->cctx->output))
9c9f28
		return slbt_exec_link_exit(
9c9f28
			&depsmeta,
9c9f28
			SLBT_SYSTEM_ERROR(dctx));
5ee0d1
5ee0d1
	if (chmod(dctx->cctx->output,0755))
9c9f28
		return slbt_exec_link_exit(
9c9f28
			&depsmeta,
9c9f28
			SLBT_SYSTEM_ERROR(dctx));
5ee0d1
3be47d
	return slbt_exec_link_exit(&depsmeta,0);
f6ccbe
}
f6ccbe
b3940a
static int slbt_exec_link_create_library_symlink(
b3940a
	const struct slbt_driver_ctx *	dctx,
b3940a
	struct slbt_exec_ctx *		ectx,
b3940a
	bool				fmajor)
b3940a
{
b3940a
	char	target[PATH_MAX];
b3940a
	char	lnkname[PATH_MAX];
b3940a
112a2b
	if (ectx->relfilename) {
112a2b
		strcpy(target,ectx->relfilename);
112a2b
		sprintf(lnkname,"%s.release",ectx->dsofilename);
112a2b
112a2b
		if (slbt_create_symlink(
112a2b
				dctx,ectx,
112a2b
				target,lnkname,
112a2b
				false))
9c9f28
			return SLBT_NESTED_ERROR(dctx);
600957
	} else {
08f5f9
		sprintf(target,"%s%s.%d.%d.%d%s",
08f5f9
			ectx->dsobasename,
08f5f9
			dctx->cctx->settings.osdsuffix,
112a2b
			dctx->cctx->verinfo.major,
112a2b
			dctx->cctx->verinfo.minor,
08f5f9
			dctx->cctx->verinfo.revision,
08f5f9
			dctx->cctx->settings.osdfussix);
600957
	}
600957
b3940a
600957
	if (fmajor && ectx->dsorellnkname) {
600957
		sprintf(lnkname,"%s.%d",
600957
			ectx->dsorellnkname,
600957
			dctx->cctx->verinfo.major);
600957
600957
	} else if (fmajor) {
08f5f9
		sprintf(lnkname,"%s%s.%d%s",
08f5f9
			ectx->dsobasename,
08f5f9
			dctx->cctx->settings.osdsuffix,
08f5f9
			dctx->cctx->verinfo.major,
08f5f9
			dctx->cctx->settings.osdfussix);
b3940a
600957
	} else {
b3940a
		strcpy(lnkname,ectx->dsofilename);
600957
	}
600957
b3940a
dbd229
	if (fmajor && (dctx->cctx->drvflags & SLBT_DRIVER_IMAGE_PE))
dbd229
		return slbt_copy_file(
dbd229
			dctx,ectx,
dbd229
			target,lnkname);
dbd229
	else
dbd229
		return slbt_create_symlink(
dbd229
			dctx,ectx,
dbd229
			target,lnkname,
dbd229
			false);
b3940a
}
b3940a
2bd749
int slbt_exec_link(
2bd749
	const struct slbt_driver_ctx *	dctx,
2bd749
	struct slbt_exec_ctx *		ectx)
2bd749
{
2bd749
	int			ret;
a51ace
	const char *		output;
2bd749
	char *			dot;
2bd749
	struct slbt_exec_ctx *	actx;
372423
	bool			fpic;
372423
	bool			fstaticonly;
9112ac
	char			soname[PATH_MAX];
4469ed
	char			soxyz [PATH_MAX];
4469ed
	char			solnk [PATH_MAX];
4469ed
	char			arname[PATH_MAX];
4469ed
c4a389
	/* dry run */
c4a389
	if (dctx->cctx->drvflags & SLBT_DRIVER_DRY_RUN)
c4a389
		return 0;
c4a389
2f9f52
	/* context */
2f9f52
	if (ectx)
2f9f52
		actx = 0;
2f9f52
	else if ((ret = slbt_get_exec_ctx(dctx,&ectx)))
2f9f52
		return SLBT_NESTED_ERROR(dctx);
2f9f52
	else
2f9f52
		actx = ectx;
2f9f52
4469ed
	/* libfoo.so.x.y.z */
08f5f9
	if ((size_t)snprintf(soxyz,sizeof(soxyz),"%s%s%s.%d.%d.%d%s",
2f9f52
				ectx->sonameprefix,
4469ed
				dctx->cctx->libname,
08f5f9
				dctx->cctx->settings.osdsuffix,
4469ed
				dctx->cctx->verinfo.major,
4469ed
				dctx->cctx->verinfo.minor,
08f5f9
				dctx->cctx->verinfo.revision,
08f5f9
				dctx->cctx->settings.osdfussix)
2f9f52
			>= sizeof(soxyz)) {
2f9f52
		slbt_free_exec_ctx(actx);
9c9f28
		return SLBT_BUFFER_ERROR(dctx);
2f9f52
	}
4469ed
4469ed
	/* libfoo.so.x */
08f5f9
	sprintf(soname,"%s%s%s.%d%s",
2f9f52
		ectx->sonameprefix,
4469ed
		dctx->cctx->libname,
08f5f9
		dctx->cctx->settings.osdsuffix,
08f5f9
		dctx->cctx->verinfo.major,
08f5f9
		dctx->cctx->settings.osdfussix);
4469ed
4469ed
	/* libfoo.so */
4469ed
	sprintf(solnk,"%s%s%s",
2f9f52
		ectx->sonameprefix,
4469ed
		dctx->cctx->libname,
4469ed
		dctx->cctx->settings.dsosuffix);
4469ed
4469ed
	/* libfoo.a */
4469ed
	sprintf(arname,"%s%s%s",
4469ed
		dctx->cctx->settings.arprefix,
4469ed
		dctx->cctx->libname,
4469ed
		dctx->cctx->settings.arsuffix);
2bd749
5cc3b3
	/* output suffix */
a51ace
	output = dctx->cctx->output;
a51ace
	dot    = strrchr(output,'.');
5cc3b3
2bd749
	/* .libs directory */
267766
	if (slbt_mkdir(dctx,ectx->ldirname)) {
723ef0
		slbt_free_exec_ctx(actx);
9c9f28
		return SLBT_SYSTEM_ERROR(dctx);
723ef0
	}
2bd749
5cc3b3
	/* non-pic libfoo.a */
5cc3b3
	if (dot && !strcmp(dot,".a"))
02ae4d
		if (slbt_exec_link_create_archive(dctx,ectx,output,false,false)) {
5cc3b3
			slbt_free_exec_ctx(actx);
9c9f28
			return SLBT_NESTED_ERROR(dctx);
5cc3b3
		}
5cc3b3
372423
	/* fpic, fstaticonly */
372423
	if (dctx->cctx->drvflags & SLBT_DRIVER_ALL_STATIC) {
372423
		fstaticonly = true;
372423
		fpic        = false;
f782a1
	} else if (dctx->cctx->drvflags & SLBT_DRIVER_DISABLE_SHARED) {
f782a1
		fstaticonly = true;
f782a1
		fpic        = false;
2c2879
	} else if (dctx->cctx->drvflags & SLBT_DRIVER_DISABLE_STATIC) {
2c2879
		fstaticonly = false;
2c2879
		fpic        = true;
372423
	} else if (dctx->cctx->drvflags & SLBT_DRIVER_SHARED) {
372423
		fstaticonly = false;
372423
		fpic        = true;
372423
	} else {
372423
		fstaticonly = false;
372423
		fpic        = false;
372423
	}
372423
a0c318
	/* pic libfoo.a */
a0c318
	if (dot && !strcmp(dot,".la"))
a0c318
		if (slbt_exec_link_create_archive(
a0c318
				dctx,ectx,
a0c318
				ectx->arfilename,
02ae4d
				fpic,true)) {
a0c318
			slbt_free_exec_ctx(actx);
9c9f28
			return SLBT_NESTED_ERROR(dctx);
a0c318
		}
a0c318
372423
	/* -all-static library */
372423
	if (fstaticonly && dctx->cctx->libname)
372423
		if (slbt_create_symlink(
372423
				dctx,ectx,
372423
				"/dev/null",
372423
				ectx->dsofilename,
372423
				false))
9c9f28
			return SLBT_NESTED_ERROR(dctx);
372423
b3940a
	/* dynamic library */
372423
	if (dot && !strcmp(dot,".la") && dctx->cctx->rpath && !fstaticonly) {
b3940a
		/* linking: libfoo.so.x.y.z */
b3940a
		if (slbt_exec_link_create_library(
b3940a
				dctx,ectx,
08f5f9
				ectx->dsobasename,
112a2b
				ectx->dsofilename,
112a2b
				ectx->relfilename)) {
b3940a
			slbt_free_exec_ctx(actx);
9c9f28
			return SLBT_NESTED_ERROR(dctx);
b3940a
		}
b3940a
867d1e
		if (!(dctx->cctx->drvflags & SLBT_DRIVER_AVOID_VERSION)) {
867d1e
			/* symlink: libfoo.so.x --> libfoo.so.x.y.z */
867d1e
			if (slbt_exec_link_create_library_symlink(
867d1e
					dctx,ectx,
867d1e
					true)) {
867d1e
				slbt_free_exec_ctx(actx);
9c9f28
				return SLBT_NESTED_ERROR(dctx);
867d1e
			}
b3940a
867d1e
			/* symlink: libfoo.so --> libfoo.so.x.y.z */
867d1e
			if (slbt_exec_link_create_library_symlink(
867d1e
					dctx,ectx,
867d1e
					false)) {
867d1e
				slbt_free_exec_ctx(actx);
9c9f28
				return SLBT_NESTED_ERROR(dctx);
867d1e
			}
112a2b
		} else if (ectx->relfilename) {
112a2b
			/* symlink: libfoo.so --> libfoo-x.y.z.so */
112a2b
			if (slbt_exec_link_create_library_symlink(
112a2b
					dctx,ectx,
112a2b
					false)) {
112a2b
				slbt_free_exec_ctx(actx);
9c9f28
				return SLBT_NESTED_ERROR(dctx);
112a2b
			}
b3940a
		}
9112ac
9112ac
		/* PE import libraries */
9112ac
		if (dctx->cctx->drvflags & SLBT_DRIVER_IMAGE_PE) {
ff4b97
			/* libfoo.x.lib.a */
9112ac
			if (slbt_exec_link_create_import_library(
9112ac
					dctx,ectx,
ff4b97
					ectx->pimpfilename,
9112ac
					ectx->deffilename,
4fdf35
					soname,
4fdf35
					true))
9c9f28
				return SLBT_NESTED_ERROR(dctx);
9112ac
ff4b97
			/* symlink: libfoo.lib.a --> libfoo.x.lib.a */
ff4b97
			if (slbt_create_symlink(
9112ac
					dctx,ectx,
9112ac
					ectx->pimpfilename,
ff4b97
					ectx->dimpfilename,
ff4b97
					false))
9c9f28
				return SLBT_NESTED_ERROR(dctx);
9112ac
9112ac
			/* libfoo.x.y.z.lib.a */
9112ac
			if (slbt_exec_link_create_import_library(
9112ac
					dctx,ectx,
9112ac
					ectx->vimpfilename,
9112ac
					ectx->deffilename,
4469ed
					soxyz,
4fdf35
					false))
9c9f28
				return SLBT_NESTED_ERROR(dctx);
9112ac
		}
b3940a
	}
b3940a
f6ccbe
	/* executable */
a02c0e
	if (!dctx->cctx->libname) {
f6ccbe
		/* linking: .libs/exefilename */
f6ccbe
		if (slbt_exec_link_create_executable(
f6ccbe
				dctx,ectx,
f6ccbe
				ectx->exefilename)) {
f6ccbe
			slbt_free_exec_ctx(actx);
9c9f28
			return SLBT_NESTED_ERROR(dctx);
f6ccbe
		}
f6ccbe
	}
f6ccbe
2bd749
	/* no wrapper? */
5cc3b3
	if (!dot || strcmp(dot,".la")) {
2bd749
		slbt_free_exec_ctx(actx);
2bd749
		return 0;
2bd749
	}
2bd749
a9cfe4
	/* library wrapper */
a9cfe4
	if (slbt_create_library_wrapper(
a9cfe4
			dctx,ectx,
a9cfe4
			arname,soname,soxyz,solnk)) {
2bd749
		slbt_free_exec_ctx(actx);
9c9f28
		return SLBT_NESTED_ERROR(dctx);
2bd749
	}
2bd749
4c76f7
	/* wrapper symlink */
9c9f28
	if ((ret = slbt_create_symlink(
a9cfe4
			dctx,ectx,
a9cfe4
			output,
a9cfe4
			ectx->lafilename,
9c9f28
			true)))
9c9f28
		SLBT_NESTED_ERROR(dctx);
4c76f7
26ea30
	/* .lai wrapper symlink */
26ea30
	if (ret == 0)
9c9f28
		if ((ret = slbt_create_symlink(
26ea30
				dctx,ectx,
26ea30
				output,
26ea30
				ectx->laifilename,
9c9f28
				true)))
9c9f28
			SLBT_NESTED_ERROR(dctx);
26ea30
2bd749
	/* all done */
2bd749
	slbt_free_exec_ctx(actx);
2bd749
4469ed
	return ret;
2bd749
}