summaryrefslogtreecommitdiff
path: root/src/logic/linkcmd/slbt_linkcmd_implib.c
blob: b2c0375edb24563d9ce7e47fc9f2ad3b76a23d88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*******************************************************************/
/*  slibtool: a strong libtool implementation, written in C        */
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
/*******************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>

#include <slibtool/slibtool.h>
#include "slibtool_driver_impl.h"
#include "slibtool_errinfo_impl.h"
#include "slibtool_linkcmd_impl.h"
#include "slibtool_mapfile_impl.h"
#include "slibtool_metafile_impl.h"
#include "slibtool_snprintf_impl.h"
#include "slibtool_symlink_impl.h"
#include "slibtool_spawn_impl.h"
#include "slibtool_visibility_impl.h"

slbt_hidden int slbt_exec_link_create_import_library(
	const struct slbt_driver_ctx *	dctx,
	struct slbt_exec_ctx *		ectx,
	char *				impfilename,
	char *				deffilename,
	char *				soname)
{
	int	fmdso;
	char ** argv;
	char ** parg;
	char ** aarg;
	char	program[PATH_MAX];
	char    as[PATH_MAX];

	/* dlltool or mdso? */
	if (dctx->cctx->drvflags & SLBT_DRIVER_IMPLIB_DSOMETA)
		fmdso = 1;

	else if (dctx->cctx->drvflags & SLBT_DRIVER_IMPLIB_IDATA)
		fmdso = 0;

	else if (!(strcmp(dctx->cctx->host.flavor,"midipix")))
		fmdso = 1;

	else
		fmdso = 0;

	/* alternate argument vector */
	ectx->argv    = ectx->altv;
	ectx->program = program;

	/* tool-specific argument vector */
	argv = (fmdso)
		? (slbt_get_driver_ictx(dctx))->host.mdso_argv
		: (slbt_get_driver_ictx(dctx))->host.dlltool_argv;

	/* tool-specific argv */
	aarg = ectx->altv;

	if ((parg = argv)) {
		ectx->program = argv[0];

		for (; *parg; )
			*aarg++ = *parg++;
	} else {
		*aarg++ = program;
	}

	/* altv */
	if (fmdso) {
		if (!argv)
			if (slbt_snprintf(program,sizeof(program),
					"%s",dctx->cctx->host.mdso) < 0)
				return SLBT_BUFFER_ERROR(dctx);

		*aarg++ = "-i";
		*aarg++ = impfilename;
		*aarg++ = "-n";
		*aarg++ = soname;
		*aarg++ = deffilename;
		*aarg   = 0;
	} else {
		if (!argv)
			if (slbt_snprintf(program,sizeof(program),
					"%s",dctx->cctx->host.dlltool) < 0)
				return SLBT_BUFFER_ERROR(dctx);

		*aarg++ = "-l";
		*aarg++ = impfilename;
		*aarg++ = "-d";
		*aarg++ = deffilename;
		*aarg++ = "-D";
		*aarg++ = soname;
		*aarg = 0;

		if (dctx->cctx->host.as) {
			if ((argv = (slbt_get_driver_ictx(dctx))->host.as_argv)) {
				*aarg++ = "-S";
				*aarg++ = argv[0];

				for (parg=&argv[1]; *parg; parg++) {
					*aarg++ = "-f";
					*aarg++ = *parg;
				}
			} else {
				if (slbt_snprintf(as,sizeof(as),
						"%s",dctx->cctx->host.as) < 0)
					return SLBT_BUFFER_ERROR(dctx);

				*aarg++ = "-S";
				*aarg++ = as;
			}

			const char * host = dctx->cctx->host.host;

			if (host && (host[0] == 'i')
					&& (host[1] >= '3')
					&& (host[1] <= '6')
					&& (host[2] == '8')
					&& (host[3] == '6')
					&& (host[4] == '-')) {
				*aarg++  = "-f";
				*aarg++ = "--32";
				*aarg++ = "-m";
				*aarg++ = "i386";
				*aarg++ = 0;
			} else {
				*aarg++ = "-f";
				*aarg++ = "--64";
				*aarg++ = "-m";
				*aarg++ = "i386:x86-64";
				*aarg   = 0;
			}
		}
	}

	/* step output */
	if (!(dctx->cctx->drvflags & SLBT_DRIVER_SILENT))
		if (slbt_output_link(ectx))
			return SLBT_NESTED_ERROR(dctx);

	/* dlltool/mdso spawn */
	if ((slbt_spawn(ectx,true) < 0) && (ectx->pid < 0)) {
		return SLBT_SPAWN_ERROR(dctx);

	} else if (ectx->exitcode) {
		return SLBT_CUSTOM_ERROR(
			dctx,
			fmdso ? SLBT_ERR_MDSO_ERROR : SLBT_ERR_DLLTOOL_ERROR);
	}

	return 0;
}