Blame src/logic/linkcmd/slbt_linkcmd_implib.c

5db3db
/*******************************************************************/
5db3db
/*  slibtool: a skinny libtool implementation, written in C        */
49181b
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
5db3db
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
5db3db
/*******************************************************************/
5db3db
5db3db
#include <stdlib.h>
5db3db
#include <stdio.h>
5db3db
#include <string.h>
5db3db
#include <fcntl.h>
5db3db
#include <errno.h>
5db3db
#include <sys/stat.h>
5db3db
5db3db
#include <slibtool/slibtool.h>
5db3db
#include "slibtool_driver_impl.h"
5db3db
#include "slibtool_errinfo_impl.h"
5db3db
#include "slibtool_linkcmd_impl.h"
5db3db
#include "slibtool_mapfile_impl.h"
5db3db
#include "slibtool_metafile_impl.h"
5db3db
#include "slibtool_snprintf_impl.h"
5db3db
#include "slibtool_symlink_impl.h"
5db3db
#include "slibtool_spawn_impl.h"
5db3db
5db3db
int slbt_exec_link_create_import_library(
5db3db
	const struct slbt_driver_ctx *	dctx,
5db3db
	struct slbt_exec_ctx *		ectx,
5db3db
	char *				impfilename,
5db3db
	char *				deffilename,
5db3db
	char *				soname)
5db3db
{
5db3db
	int	fmdso;
2b538b
	char ** argv;
2b538b
	char ** parg;
2b538b
	char ** aarg;
5db3db
	char	program[PATH_MAX];
a6c393
	char    as[PATH_MAX];
5db3db
5db3db
	/* dlltool or mdso? */
5db3db
	if (dctx->cctx->drvflags & SLBT_DRIVER_IMPLIB_DSOMETA)
5db3db
		fmdso = 1;
5db3db
6d7ad6
	else if (dctx->cctx->drvflags & SLBT_DRIVER_IMPLIB_IDATA)
5db3db
		fmdso = 0;
5db3db
5db3db
	else if (!(strcmp(dctx->cctx->host.flavor,"midipix")))
5db3db
		fmdso = 1;
5db3db
5db3db
	else
5db3db
		fmdso = 0;
5db3db
2b538b
	/* alternate argument vector */
2b538b
	ectx->argv    = ectx->altv;
2b538b
	ectx->program = program;
2b538b
2b538b
	/* tool-specific argument vector */
2b538b
	argv = (fmdso)
2b538b
		? (slbt_get_driver_ictx(dctx))->host.mdso_argv
2b538b
		: (slbt_get_driver_ictx(dctx))->host.dlltool_argv;
2b538b
2b538b
	/* tool-specific argv */
2b538b
	aarg = ectx->altv;
2b538b
2b538b
	if ((parg = argv)) {
2b538b
		ectx->program = argv[0];
2b538b
2b538b
		for (; *parg; )
2b538b
			*aarg++ = *parg++;
2b538b
	} else {
2b538b
		*aarg++ = program;
2b538b
	}
2b538b
2b538b
	/* altv */
5db3db
	if (fmdso) {
2b538b
		if (!argv)
2b538b
			if (slbt_snprintf(program,sizeof(program),
2b538b
					"%s",dctx->cctx->host.mdso) < 0)
2b538b
				return SLBT_BUFFER_ERROR(dctx);
2b538b
2b538b
		*aarg++ = "-i";
2b538b
		*aarg++ = impfilename;
2b538b
		*aarg++ = "-n";
2b538b
		*aarg++ = soname;
2b538b
		*aarg++ = deffilename;
2b538b
		*aarg   = 0;
5db3db
	} else {
2b538b
		if (!argv)
2b538b
			if (slbt_snprintf(program,sizeof(program),
2b538b
					"%s",dctx->cctx->host.dlltool) < 0)
2b538b
				return SLBT_BUFFER_ERROR(dctx);
2b538b
2b538b
		*aarg++ = "-l";
2b538b
		*aarg++ = impfilename;
2b538b
		*aarg++ = "-d";
2b538b
		*aarg++ = deffilename;
2b538b
		*aarg++ = "-D";
2b538b
		*aarg++ = soname;
2b538b
		*aarg = 0;
a6c393
a6c393
		if (dctx->cctx->host.as) {
2b538b
			if ((argv = (slbt_get_driver_ictx(dctx))->host.as_argv)) {
2b538b
				*aarg++ = "-S";
2b538b
				*aarg++ = argv[0];
2b538b
2b538b
				for (parg=&argv[1]; *parg; parg++) {
2b538b
					*aarg++ = "-f";
2b538b
					*aarg++ = *parg;
2b538b
				}
2b538b
			} else {
2b538b
				if (slbt_snprintf(as,sizeof(as),
2b538b
						"%s",dctx->cctx->host.as) < 0)
2b538b
					return SLBT_BUFFER_ERROR(dctx);
a6c393
2b538b
				*aarg++ = "-S";
2b538b
				*aarg++ = as;
2b538b
			}
a6c393
a6c393
			const char * host = dctx->cctx->host.host;
a6c393
a6c393
			if (host && (host[0] == 'i')
a6c393
					&& (host[1] >= '3')
a6c393
					&& (host[1] <= '6')
a6c393
					&& (host[2] == '8')
a6c393
					&& (host[3] == '6')
a6c393
					&& (host[4] == '-')) {
2b538b
				*aarg++  = "-f";
2b538b
				*aarg++ = "--32";
2b538b
				*aarg++ = "-m";
2b538b
				*aarg++ = "i386";
2b538b
				*aarg++ = 0;
a6c393
			} else {
2b538b
				*aarg++ = "-f";
2b538b
				*aarg++ = "--64";
2b538b
				*aarg++ = "-m";
2b538b
				*aarg++ = "i386:x86-64";
2b538b
				*aarg   = 0;
a6c393
			}
a6c393
		}
5db3db
	}
5db3db
5db3db
	/* step output */
5db3db
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
5db3db
		if (slbt_output_link(dctx,ectx))
5db3db
			return SLBT_NESTED_ERROR(dctx);
5db3db
5db3db
	/* dlltool/mdso spawn */
5db3db
	if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0)) {
5db3db
		return SLBT_SPAWN_ERROR(dctx);
5db3db
5db3db
	} else if (ectx->exitcode) {
5db3db
		return SLBT_CUSTOM_ERROR(
5db3db
			dctx,
5db3db
			fmdso ? SLBT_ERR_MDSO_ERROR : SLBT_ERR_DLLTOOL_ERROR);
5db3db
	}
5db3db
5db3db
	return 0;
5db3db
}