Blame src/logic/linkcmd/slbt_linkcmd_deps.c

b08054
/*******************************************************************/
b08054
/*  slibtool: a skinny libtool implementation, written in C        */
49181b
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
b08054
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
b08054
/*******************************************************************/
b08054
b08054
#include <stdlib.h>
b08054
#include <stdio.h>
b08054
#include <string.h>
b08054
#include <fcntl.h>
b08054
#include <errno.h>
b08054
#include <sys/stat.h>
b08054
b08054
#include <slibtool/slibtool.h>
b08054
#include "slibtool_driver_impl.h"
b08054
#include "slibtool_errinfo_impl.h"
b08054
#include "slibtool_linkcmd_impl.h"
b08054
#include "slibtool_mapfile_impl.h"
b08054
#include "slibtool_metafile_impl.h"
43ee04
#include "slibtool_realpath_impl.h"
b08054
#include "slibtool_snprintf_impl.h"
4b56de
#include "slibtool_visibility_impl.h"
b08054
4b56de
slbt_hidden int slbt_get_deps_meta(
b08054
	const struct slbt_driver_ctx *	dctx,
b08054
	char *				libfilename,
b08054
	int				fexternal,
b08054
	struct slbt_deps_meta *		depsmeta)
b08054
{
b08054
	int			fdcwd;
b08054
	char *			ch;
b08054
	char *			cap;
b08054
	char *			base;
b08054
	size_t			libexlen;
b08054
	struct stat		st;
b08054
	struct slbt_map_info *	mapinfo;
b08054
	char			depfile[PATH_MAX];
b08054
b08054
	/* fdcwd */
b08054
	fdcwd = slbt_driver_fdcwd(dctx);
b08054
b08054
	/* -rpath */
b08054
	if (slbt_snprintf(depfile,sizeof(depfile),
b08054
				"%s.slibtool.rpath",
b08054
				libfilename) < 0)
b08054
		return SLBT_BUFFER_ERROR(dctx);
b08054
b08054
	/* -Wl,%s */
b08054
	if (!fstatat(fdcwd,depfile,&st,AT_SYMLINK_NOFOLLOW)) {
b08054
		depsmeta->infolen += st.st_size + 4;
b08054
		depsmeta->infolen++;
b08054
	}
b08054
b08054
	/* .deps */
b08054
	if (slbt_snprintf(depfile,sizeof(depfile),
b08054
				"%s.slibtool.deps",
b08054
				libfilename) < 0)
b08054
		return SLBT_BUFFER_ERROR(dctx);
b08054
b08054
	/* mapinfo */
b08054
	if (!(mapinfo = slbt_map_file(fdcwd,depfile,SLBT_MAP_INPUT)))
b08054
		return (fexternal && (errno == ENOENT))
b08054
			? 0 : SLBT_SYSTEM_ERROR(dctx,depfile);
b08054
b08054
	/* copied length */
b08054
	depsmeta->infolen += mapinfo->size;
b08054
	depsmeta->infolen++;
b08054
b08054
	/* libexlen */
b08054
	libexlen = (base = strrchr(libfilename,'/'))
b08054
		? strlen(depfile) + 2 + (base - libfilename)
b08054
		: strlen(depfile) + 2;
b08054
b08054
	/* iterate */
b08054
	ch  = mapinfo->addr;
b08054
	cap = mapinfo->cap;
b08054
b08054
	for (; ch
b08054
		if (*ch++ == '\n') {
b08054
			depsmeta->infolen += libexlen;
b08054
			depsmeta->depscnt++;
b08054
		}
b08054
	}
b08054
b08054
	slbt_unmap_file(mapinfo);
b08054
b08054
	return 0;
b08054
}
b08054
b08054
43ee04
static int slbt_exec_link_normalize_dep_file(
43ee04
	const struct slbt_driver_ctx *  dctx,
43ee04
	int                             deps,
43ee04
	char                            (*depfile)[PATH_MAX])
43ee04
{
43ee04
	int                             ret;
43ee04
	int                             fdcwd;
43ee04
	int                             fdtgt;
43ee04
	char *                          dot;
43ee04
	char *                          slash;
43ee04
	const char *                    mark;
43ee04
	struct slbt_txtfile_ctx *       tctx;
43ee04
	const char **                   pline;
43ee04
	char *                          tgtmark;
43ee04
	char *                          depmark;
43ee04
	char *                          relmark;
43ee04
	char *                          tgtnext;
43ee04
	char *                          depnext;
43ee04
	char                            tgtdir  [PATH_MAX];
43ee04
	char                            tgtpath [PATH_MAX];
43ee04
	char                            deppath [PATH_MAX];
43ee04
	char                            pathbuf [PATH_MAX];
43ee04
	char                            depsbuf [PATH_MAX];
43ee04
	char                            relapath[PATH_MAX];
43ee04
43ee04
	/* fdcwd */
43ee04
	fdcwd = slbt_driver_fdcwd(dctx);
43ee04
43ee04
	/* first-pass dependency file */
43ee04
	if (slbt_impl_get_txtfile_ctx(dctx,*depfile,deps,&tctx) < 0)
43ee04
		return SLBT_NESTED_ERROR(dctx);
43ee04
43ee04
	/* second-pass dependency file */
43ee04
	dot = strrchr(*depfile,'.');
43ee04
	strcpy(dot,".tmp2");
43ee04
43ee04
	if ((deps = openat(fdcwd,*depfile,O_RDWR|O_CREAT|O_TRUNC,0644)) < 0) {
43ee04
		slbt_lib_free_txtfile_ctx(tctx);
43ee04
		return SLBT_SYSTEM_ERROR(dctx,depfile);
43ee04
	}
43ee04
43ee04
	/* fdtgt */
43ee04
	strcpy(tgtdir,*depfile);
43ee04
43ee04
	if (!(slash = strrchr(tgtdir,'/')))
43ee04
		slash = tgtdir;
43ee04
43ee04
	slash[0] = '\0';
43ee04
43ee04
	if ((slash = strrchr(tgtdir,'/'))) {
43ee04
		slash[0] = '\0';
43ee04
		slash++;
43ee04
	} else {
43ee04
		slash    = tgtdir;
43ee04
		slash[0] = '.';
43ee04
		slash[1] = '/';
43ee04
		slash[2] = '\0';
43ee04
	}
43ee04
43ee04
	if ((slash > tgtdir) && strcmp(slash,".libs")) {
43ee04
		close(deps);
43ee04
		slbt_lib_free_txtfile_ctx(tctx);
43ee04
43ee04
		return SLBT_CUSTOM_ERROR(
43ee04
			dctx,
43ee04
			SLBT_ERR_FLOW_ERROR);
43ee04
	}
43ee04
43ee04
	if ((fdtgt = openat(fdcwd,tgtdir,O_DIRECTORY|O_CLOEXEC,0)) < 0) {
43ee04
		close(deps);
43ee04
		slbt_lib_free_txtfile_ctx(tctx);
43ee04
43ee04
		return SLBT_CUSTOM_ERROR(
43ee04
			dctx,
43ee04
			SLBT_ERR_FLOW_ERROR);
43ee04
	}
43ee04
43ee04
	if (slbt_realpath(fdcwd,tgtdir,0,tgtpath,sizeof(tgtpath)) < 0) {
43ee04
				close(fdtgt);
43ee04
				close(deps);
43ee04
				slbt_lib_free_txtfile_ctx(tctx);
43ee04
43ee04
				return SLBT_CUSTOM_ERROR(
43ee04
					dctx,
43ee04
					SLBT_ERR_FLOW_ERROR);
43ee04
	}
43ee04
43ee04
	strcpy(pathbuf,tgtpath);
43ee04
43ee04
	/* normalize dependency lines as needed */
43ee04
	for (pline=tctx->txtlinev; *pline; pline++) {
43ee04
		if ((mark = *pline)) {
43ee04
			if ((mark[0] == '-') && (mark[1] == 'L'))
43ee04
					mark = &mark[2];
43ee04
43ee04
			if ((mark > *pline) && (mark[0] == '/'))
43ee04
				mark = *pline;
43ee04
		} else {
43ee04
			mark = *pline;
43ee04
		}
43ee04
43ee04
		if (mark > *pline) {
43ee04
			if (slbt_realpath(
43ee04
					fdtgt,mark,0,deppath,
43ee04
					sizeof(deppath)) < 0)
43ee04
				mark = *pline;
43ee04
43ee04
			else if ((tgtpath[0] != '/') || (deppath[0] != '/'))
43ee04
				mark = 0;
43ee04
43ee04
			else
43ee04
				strcpy(depsbuf,deppath);
43ee04
		}
43ee04
43ee04
		if ((mark > *pline) && strcmp(tgtpath,deppath)) {
43ee04
			tgtmark = tgtpath;
43ee04
			depmark = deppath;
43ee04
43ee04
			tgtnext = strchr(tgtmark,'/');
43ee04
			depnext = strchr(depmark,'/');
43ee04
43ee04
			while (tgtnext && depnext) {
43ee04
				*tgtnext = '\0';
43ee04
				*depnext = '\0';
43ee04
43ee04
				if (strcmp(tgtmark,depmark)) {
43ee04
					tgtnext = 0;
43ee04
					depnext = 0;
43ee04
				} else {
43ee04
					tgtmark = &tgtnext[1];
43ee04
					depmark = &depnext[1];
43ee04
43ee04
					if (*tgtmark && *depmark) {
43ee04
						tgtnext = strchr(tgtmark,'/');
43ee04
						depnext = strchr(depmark,'/');
43ee04
					} else {
43ee04
						tgtnext = 0;
43ee04
						depnext = 0;
43ee04
					}
43ee04
				}
43ee04
			}
43ee04
43ee04
			strcpy(tgtmark,&pathbuf[tgtmark-tgtpath]);
43ee04
			strcpy(depmark,&depsbuf[depmark-deppath]);
43ee04
43ee04
			if ((tgtmark - tgtpath) == (depmark - deppath)) {
43ee04
				mark = &depmark[strlen(tgtmark)];
43ee04
43ee04
				if ((mark[0] == '/') && !strncmp(tgtmark,depmark,mark-depmark))
43ee04
					sprintf(relapath,"-L.%s",mark);
43ee04
				else
43ee04
					mark = relapath;
43ee04
			} else {
43ee04
				mark = relapath;
43ee04
			}
43ee04
43ee04
			if (mark == relapath) {
43ee04
				relmark = relapath;
43ee04
				relmark += sprintf(relapath,"-L../");
43ee04
43ee04
				while ((tgtnext = strchr(tgtmark,'/'))) {
43ee04
					tgtmark = &tgtnext[1];
43ee04
					relmark += sprintf(relmark,"../");
43ee04
				}
43ee04
43ee04
				strcpy(relmark,depmark);
43ee04
			}
43ee04
43ee04
			mark = relapath;
43ee04
43ee04
		} else if (mark > *pline) {
43ee04
			strcpy(relapath,"-L.");
43ee04
			mark = relapath;
43ee04
		}
43ee04
43ee04
		ret = mark ? slbt_dprintf(deps,"%s\n",mark) : (-1);
43ee04
43ee04
		if (ret < 0) {
43ee04
			close(deps);
43ee04
			close(fdtgt);
43ee04
			slbt_lib_free_txtfile_ctx(tctx);
43ee04
43ee04
			return mark
43ee04
				? SLBT_SYSTEM_ERROR(dctx,0)
43ee04
				: SLBT_NESTED_ERROR(dctx);
43ee04
		}
43ee04
43ee04
		if (mark == relapath)
43ee04
			strcpy(tgtpath,pathbuf);
43ee04
	}
43ee04
43ee04
	close(fdtgt);
43ee04
	slbt_lib_free_txtfile_ctx(tctx);
43ee04
43ee04
	return deps;
43ee04
}
43ee04
43ee04
43ee04
static int slbt_exec_link_compact_dep_file(
43ee04
	const struct slbt_driver_ctx *  dctx,
43ee04
	int                             deps,
43ee04
	char                            (*depfile)[PATH_MAX])
43ee04
{
43ee04
	int                             fdcwd;
43ee04
	struct slbt_txtfile_ctx *       tctx;
43ee04
	const char **                   pline;
43ee04
	const char **                   pcomp;
43ee04
	const char *                    depline;
43ee04
	char *                          dot;
43ee04
43ee04
	/* fdcwd */
43ee04
	fdcwd = slbt_driver_fdcwd(dctx);
43ee04
43ee04
	/* second-pass dependency file */
43ee04
	if (slbt_impl_get_txtfile_ctx(dctx,*depfile,deps,&tctx) < 0)
43ee04
		return SLBT_NESTED_ERROR(dctx);
43ee04
43ee04
	/* second-pass dependency file */
43ee04
	dot = strrchr(*depfile,'.');
43ee04
	dot[0] = '\0';
43ee04
43ee04
	if ((deps = openat(fdcwd,*depfile,O_RDWR|O_CREAT|O_TRUNC,0644)) < 0) {
43ee04
		slbt_lib_free_txtfile_ctx(tctx);
43ee04
		return SLBT_SYSTEM_ERROR(dctx,depfile);
43ee04
	}
43ee04
43ee04
	/* iterate, only write unique entries */
43ee04
	for (pline=tctx->txtlinev; *pline; pline++) {
43ee04
		depline = *pline;
43ee04
43ee04
		if ((depline[0] == '-') && (depline[1] == 'L'))
43ee04
			for (pcomp=tctx->txtlinev; depline && pcomp
43ee04
				if (!strcmp(*pcomp,depline))
43ee04
					depline = 0;
43ee04
43ee04
		if (depline && (slbt_dprintf(deps,"%s\n",depline) < 0)) {
43ee04
			close(deps);
43ee04
			slbt_lib_free_txtfile_ctx(tctx);
43ee04
			return SLBT_SYSTEM_ERROR(dctx,0);
43ee04
		}
43ee04
	}
43ee04
43ee04
	slbt_lib_free_txtfile_ctx(tctx);
43ee04
43ee04
	return deps;
43ee04
}
43ee04
43ee04
4b56de
slbt_hidden int slbt_exec_link_create_dep_file(
b08054
	const struct slbt_driver_ctx *	dctx,
b08054
	struct slbt_exec_ctx *		ectx,
b08054
	char **				altv,
b08054
	const char *			libfilename,
b08054
	bool				farchive)
b08054
{
b08054
	int			ret;
b08054
	int			deps;
43ee04
	int			fdtmp;
b08054
	int			slen;
b08054
	int			fdcwd;
b08054
	char **			parg;
b08054
	char *			popt;
b08054
	char *			plib;
b08054
	char *			path;
b08054
	char *			mark;
b08054
	char *			base;
b08054
	size_t			size;
b08054
	char			deplib [PATH_MAX];
b08054
	bool			is_reladir;
b08054
	char			reladir[PATH_MAX];
b08054
	char			depfile[PATH_MAX];
43ee04
	char			pathbuf[PATH_MAX];
b08054
	struct stat		st;
b08054
	int			ldepth;
b08054
	int			fdyndep;
b08054
	struct slbt_map_info *  mapinfo;
b08054
b08054
	/* fdcwd */
b08054
	fdcwd = slbt_driver_fdcwd(dctx);
b08054
b08054
	/* depfile */
b08054
	if (slbt_snprintf(depfile,sizeof(depfile),
43ee04
			"%s.slibtool.deps.tmp1",
b08054
			libfilename) < 0)
b08054
		return SLBT_BUFFER_ERROR(dctx);
b08054
43ee04
	strcpy(pathbuf,depfile);
43ee04
b08054
	/* deps */
b08054
	if ((deps = openat(fdcwd,depfile,O_RDWR|O_CREAT|O_TRUNC,0644)) < 0)
b08054
		return SLBT_SYSTEM_ERROR(dctx,depfile);
b08054
b08054
	/* iterate */
b08054
	for (parg=altv; *parg; parg++) {
b08054
		popt    = 0;
b08054
		plib    = 0;
b08054
		path    = 0;
b08054
		mapinfo = 0;
b08054
b08054
		if (!strncmp(*parg,"-l",2)) {
b08054
			popt = *parg;
b08054
			plib = popt + 2;
b08054
b08054
		} else if (!strncmp(*parg,"-L",2)) {
b08054
			popt = *parg;
b08054
			path = popt + 2;
b08054
b08054
		} else if (!strncmp(*parg,"-f",2)) {
b08054
			(void)0;
b08054
b08054
		} else if ((popt = strrchr(*parg,'.')) && !strcmp(popt,".la")) {
b08054
			/* import dependency list */
b08054
			if ((base = strrchr(*parg,'/')))
b08054
				base++;
b08054
			else
b08054
				base = *parg;
b08054
b08054
			/* [relative .la directory] */
b08054
			if (base > *parg) {
b08054
				slen = slbt_snprintf(
b08054
					reladir,
b08054
					sizeof(reladir),
b08054
					"%s",*parg);
b08054
b08054
				if (slen < 0) {
b08054
					close(deps);
b08054
					return SLBT_BUFFER_ERROR(dctx);
b08054
				}
b08054
b08054
				is_reladir = true;
b08054
				reladir[base - *parg - 1] = 0;
b08054
			} else {
b08054
				is_reladir = false;
b08054
				reladir[0] = '.';
b08054
				reladir[1] = 0;
b08054
			}
b08054
b08054
b08054
			/* dynamic library dependency? */
b08054
			strcpy(depfile,*parg);
b08054
			mark = depfile + (base - *parg);
b08054
			size = sizeof(depfile) - (base - *parg);
b08054
			slen = slbt_snprintf(mark,size,".libs/%s",base);
b08054
b08054
			if (slen < 0) {
b08054
				close(deps);
b08054
				return SLBT_BUFFER_ERROR(dctx);
b08054
			}
b08054
b08054
			mark = strrchr(mark,'.');
b08054
			strcpy(mark,dctx->cctx->settings.dsosuffix);
b08054
b08054
			fdyndep = !fstatat(fdcwd,depfile,&st,0);
b08054
b08054
			/* [-L... as needed] */
b08054
			if (fdyndep && (ectx->ldirdepth >= 0)) {
b08054
				if (slbt_dprintf(deps,"-L") < 0) {
b08054
					close(deps);
b08054
					return SLBT_SYSTEM_ERROR(dctx,0);
b08054
				}
b08054
b08054
				for (ldepth=ectx->ldirdepth; ldepth; ldepth--) {
b08054
					if (slbt_dprintf(deps,"../") < 0) {
b08054
						close(deps);
b08054
						return SLBT_SYSTEM_ERROR(dctx,0);
b08054
					}
b08054
				}
b08054
b08054
				if (slbt_dprintf(deps,"%s%s.libs\n",
b08054
						 (is_reladir ? reladir : ""),
b08054
						 (is_reladir ? "/" : "")) < 0) {
b08054
					close(deps);
b08054
					return SLBT_SYSTEM_ERROR(dctx,0);
b08054
				}
b08054
			}
b08054
b08054
			/* -ldeplib */
b08054
			if (fdyndep) {
b08054
				*popt = 0;
b08054
				mark  = base;
b08054
				mark += strlen(dctx->cctx->settings.dsoprefix);
b08054
b08054
				if (slbt_dprintf(deps,"-l%s\n",mark) < 0) {
b08054
					close(deps);
b08054
					return SLBT_SYSTEM_ERROR(dctx,0);
b08054
				}
b08054
b08054
				*popt = '.';
b08054
			}
b08054
b08054
			/* [open dependency list] */
b08054
			strcpy(depfile,*parg);
b08054
			mark = depfile + (base - *parg);
b08054
			size = sizeof(depfile) - (base - *parg);
b08054
			slen = slbt_snprintf(mark,size,".libs/%s",base);
b08054
b08054
			if (slen < 0) {
b08054
				close(deps);
b08054
				return SLBT_BUFFER_ERROR(dctx);
b08054
			}
b08054
b08054
			mapinfo = 0;
b08054
b08054
			mark = strrchr(mark,'.');
b08054
			size = sizeof(depfile) - (mark - depfile);
b08054
b08054
			if (!farchive) {
b08054
				slen = slbt_snprintf(mark,size,
b08054
					"%s.slibtool.deps",
b08054
					dctx->cctx->settings.dsosuffix);
b08054
b08054
				if (slen < 0) {
b08054
					close(deps);
b08054
					return SLBT_BUFFER_ERROR(dctx);
b08054
				}
b08054
b08054
				mapinfo = slbt_map_file(
b08054
					fdcwd,depfile,
b08054
					SLBT_MAP_INPUT);
b08054
b08054
				if (!mapinfo && (errno != ENOENT)) {
b08054
					close(deps);
b08054
					return SLBT_SYSTEM_ERROR(dctx,0);
b08054
				}
b08054
			}
b08054
b08054
			if (!mapinfo) {
b08054
				slen = slbt_snprintf(mark,size,
b08054
					".a.slibtool.deps");
b08054
b08054
				if (slen < 0) {
b08054
					close(deps);
b08054
					return SLBT_BUFFER_ERROR(dctx);
b08054
				}
b08054
b08054
				mapinfo = slbt_map_file(
b08054
					fdcwd,depfile,
b08054
					SLBT_MAP_INPUT);
b08054
b08054
				if (!mapinfo) {
b08054
					strcpy(mark,".a.disabled");
b08054
b08054
					if (fstatat(fdcwd,depfile,&st,AT_SYMLINK_NOFOLLOW)) {
b08054
						close(deps);
b08054
						return SLBT_SYSTEM_ERROR(dctx,depfile);
b08054
					}
b08054
				}
b08054
			}
b08054
b08054
			/* [-l... as needed] */
b08054
			while (mapinfo && (mapinfo->mark < mapinfo->cap)) {
b08054
				ret = slbt_mapped_readline(
b08054
					dctx,mapinfo,
b08054
					deplib,sizeof(deplib));
b08054
b08054
				if (ret) {
b08054
					close(deps);
b08054
					return SLBT_NESTED_ERROR(dctx);
b08054
				}
b08054
b08054
				ret = ((deplib[0] == '-')
b08054
						&& (deplib[1] == 'L')
b08054
						&& (deplib[2] != '/'))
b08054
					? slbt_dprintf(
b08054
						deps,"-L%s/%s",
b08054
						reladir,&deplib[2])
b08054
					: slbt_dprintf(
b08054
						deps,"%s",
b08054
						deplib);
b08054
b08054
				if (ret < 0) {
b08054
					close(deps);
b08054
					return SLBT_SYSTEM_ERROR(dctx,0);
b08054
				}
b08054
			}
b08054
b08054
			if (mapinfo)
b08054
				slbt_unmap_file(mapinfo);
b08054
		}
b08054
b08054
		if (plib && (slbt_dprintf(deps,"-l%s\n",plib) < 0)) {
b08054
			close(deps);
b08054
			return SLBT_SYSTEM_ERROR(dctx,0);
b08054
		}
b08054
b08054
		if (path && (slbt_dprintf(deps,"-L%s\n",path) < 0)) {
b08054
			close(deps);
b08054
			return SLBT_SYSTEM_ERROR(dctx,0);
b08054
		}
b08054
	}
b08054
43ee04
	if ((fdtmp = slbt_exec_link_normalize_dep_file(dctx,deps,&pathbuf)) < 0)
43ee04
		return SLBT_NESTED_ERROR(dctx);
43ee04
43ee04
	close(deps);
43ee04
43ee04
	if ((deps = slbt_exec_link_compact_dep_file(dctx,fdtmp,&pathbuf)) < 0)
43ee04
		return SLBT_NESTED_ERROR(dctx);
43ee04
43ee04
	close(deps);
43ee04
	close(fdtmp);
43ee04
b08054
	return 0;
b08054
}