Blame src/internal/slibtool_driver_impl.h

089c8b
/*******************************************************************/
eac61a
/*  slibtool: a strong libtool implementation, written in C        */
49181b
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
089c8b
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
089c8b
/*******************************************************************/
089c8b
582d19
#ifndef SLIBTOOL_DRIVER_IMPL_H
582d19
#define SLIBTOOL_DRIVER_IMPL_H
9ca8c4
9ca8c4
#include <stdint.h>
9ca8c4
#include <stdio.h>
a126a7
#include <sys/stat.h>
9ca8c4
#include <sys/types.h>
9ca8c4
9ca8c4
#include <slibtool/slibtool.h>
608847
#include "slibtool_dprintf_impl.h"
6ab3f1
#include "slibtool_mapfile_impl.h"
4b56de
#include "slibtool_visibility_impl.h"
9ca8c4
#include "argv/argv.h"
9ca8c4
5e0269
#define SLBT_OPTV_ELEMENTS 128
d58d2f
99eb57
#define SLBT_DRIVER_OUTPUT_MASK (SLBT_DRIVER_OUTPUT_M4_DIR    \
99eb57
				| SLBT_DRIVER_OUTPUT_AUX_DIR   \
99eb57
				| SLBT_DRIVER_OUTPUT_SHARED_EXT \
99eb57
				| SLBT_DRIVER_OUTPUT_STATIC_EXT)
99eb57
9ca8c4
extern const struct argv_option slbt_default_options[];
9ca8c4
9ca8c4
enum app_tags {
9ca8c4
	TAG_HELP,
efd47f
	TAG_HELP_ALL,
9ca8c4
	TAG_VERSION,
c57816
	TAG_INFO,
c1f216
	TAG_CONFIG,
c6ecfc
	TAG_MKVARS,
6f4115
	TAG_DUMPMACHINE,
805666
	TAG_PRINT_AUX_DIR,
805666
	TAG_PRINT_M4_DIR,
979507
	TAG_PRINT_SHARED_EXT,
979507
	TAG_PRINT_STATIC_EXT,
efd47f
	TAG_DEBUG,
efd47f
	TAG_DRY_RUN,
efd47f
	TAG_FEATURES,
0a9bff
	TAG_LEGABITS,
efd47f
	TAG_MODE,
b9575f
	TAG_FINISH,
efd47f
	TAG_WARNINGS,
2bc175
	TAG_ANNOTATE,
efd47f
	TAG_DEPS,
efd47f
	TAG_SILENT,
efd47f
	TAG_TAG,
34988f
	TAG_CCWRAP,
efd47f
	TAG_VERBOSE,
302dac
	TAG_TARGET,
302dac
	TAG_HOST,
302dac
	TAG_FLAVOR,
302dac
	TAG_AR,
6bc170
	TAG_AS,
b5e104
	TAG_NM,
302dac
	TAG_RANLIB,
9a02e2
	TAG_WINDRES,
302dac
	TAG_DLLTOOL,
fbda3a
	TAG_MDSO,
fbda3a
	TAG_IMPLIB,
116dbb
	TAG_OUTPUT,
63a1b4
	TAG_BINDIR,
523027
	TAG_LDRPATH,
bfa8ca
	TAG_SHREXT,
9c664d
	TAG_RPATH,
17e18a
	TAG_SYSROOT,
9aa1f4
	TAG_RELEASE,
a126a7
	TAG_OBJECTLIST,
05ca7e
	TAG_DLOPEN,
bb5843
	TAG_DLPREOPEN,
916050
	TAG_EXPORT_DYNAMIC,
21bb4b
	TAG_EXPSYMS_FILE,
21bb4b
	TAG_EXPSYMS_REGEX,
5a9161
	TAG_VERSION_INFO,
cd3d41
	TAG_VERSION_NUMBER,
116dbb
	TAG_NO_SUPPRESS,
b76221
	TAG_NO_INSTALL,
116dbb
	TAG_PREFER_PIC,
116dbb
	TAG_PREFER_NON_PIC,
499a71
	TAG_HEURISTICS,
116dbb
	TAG_SHARED,
116dbb
	TAG_STATIC,
56f236
	TAG_STATIC_LIBTOOL_LIBS,
f7645c
	TAG_ALL_STATIC,
0e609b
	TAG_DISABLE_STATIC,
f5fa4c
	TAG_DISABLE_SHARED,
93b62c
	TAG_NO_UNDEFINED,
a943fc
	TAG_MODULE,
68f313
	TAG_AVOID_VERSION,
116dbb
	TAG_COMPILER_FLAG,
116dbb
	TAG_VERBATIM_FLAG,
53b931
	TAG_THREAD_SAFE,
5e0269
	TAG_WEAK,
995aad
	/* ar mode */
995aad
	TAG_AR_HELP,
995aad
	TAG_AR_VERSION,
995aad
	TAG_AR_CHECK,
8afddf
	TAG_AR_PRINT,
2dc23e
	TAG_AR_MAPFILE,
dcf6a5
	TAG_AR_DLUNIT,
dcf6a5
	TAG_AR_DLSYMS,
874b59
	TAG_AR_NOSORT,
76c5e9
	TAG_AR_REGEX,
8afddf
	TAG_AR_PRETTY,
886865
	TAG_AR_POSIX,
886865
	TAG_AR_YAML,
0b177b
	TAG_AR_MERGE,
0b177b
	TAG_AR_OUTPUT,
191700
	TAG_AR_VERBOSE,
4c3fb7
	/* slibtoolize (stoolie) mode */
4c3fb7
	TAG_STLE_VERSION,
4c3fb7
	TAG_STLE_HELP,
af48d3
	TAG_STLE_COPY,
af48d3
	TAG_STLE_FORCE,
af48d3
	TAG_STLE_INSTALL,
e6c34e
	TAG_STLE_DEBUG,
e6c34e
	TAG_STLE_DRY_RUN,
8fb92c
	TAG_STLE_SILENT,
8fb92c
	TAG_STLE_VERBOSE,
e86543
	TAG_STLE_WARNINGS,
e86543
	TAG_STLE_NO_WARNINGS,
78607f
	TAG_STLE_LTDL,
9ca8c4
};
9ca8c4
0cbb20
struct slbt_split_vector {
0cbb20
	char *		dargs;
0cbb20
	char **		dargv;
0cbb20
	char **		targv;
0cbb20
	char **		cargv;
0cbb20
};
0cbb20
46ea99
struct slbt_host_strs {
c67e64
	char *		machine;
46ea99
	char *		host;
46ea99
	char *		flavor;
46ea99
	char *		ar;
6bc170
	char *		as;
b5e104
	char *		nm;
46ea99
	char *		ranlib;
9a02e2
	char *		windres;
46ea99
	char *		dlltool;
fbda3a
	char *		mdso;
5ddd2a
	char **		ar_argv;
5ddd2a
	char **		as_argv;
b5e104
	char **		nm_argv;
5ddd2a
	char **		ranlib_argv;
5ddd2a
	char **		windres_argv;
5ddd2a
	char **		dlltool_argv;
5ddd2a
	char **		mdso_argv;
46ea99
};
46ea99
a126a7
struct slbt_obj_list {
a126a7
	const char *	name;
a126a7
	void *		addr;
a126a7
	size_t		size;
a126a7
	int		objc;
a126a7
	char **		objv;
a126a7
};
a126a7
9ca8c4
struct slbt_driver_ctx_impl {
53c651
	struct argv_meta *		meta;
d577a3
	struct slbt_common_ctx          cctx;
d577a3
	struct slbt_driver_ctx          ctx;
d577a3
	struct slbt_host_strs           host;
d577a3
	struct slbt_host_strs           ahost;
d577a3
	struct slbt_fd_ctx              fdctx;
d577a3
	struct slbt_map_info            lconf;
a2aa78
	struct slbt_txtfile_ctx *       lconfctx;
c6ecfc
	struct slbt_txtfile_ctx *       mkvarsctx;
d577a3
	struct slbt_obj_list *          objlistv;
39e52e
a07095
	struct argv_entry **            dlopenv;
9da202
	size_t                          ndlopen;
7bab5a
7bab5a
	const struct slbt_archive_ctx * arctx;
7bab5a
	const char *                    arpath;
7bab5a
d577a3
	char *                          libname;
d577a3
	char *                          dargs;
d577a3
	char **                         dargv;
d577a3
	char **                         targv;
d577a3
	char **                         cargv;
d577a3
	char **                         envp;
d577a3
d577a3
	struct slbt_error_info**        errinfp;
d577a3
	struct slbt_error_info**        erricap;
d577a3
	struct slbt_error_info *        erriptr[64];
d577a3
	struct slbt_error_info          erribuf[64];
9ca8c4
};
9ca8c4
8f60d4
struct slbt_driver_ctx_alloc {
8f60d4
	struct slbt_driver_ctx_impl	ctx;
8f60d4
	uint64_t			guard;
8f60d4
};
8f60d4
9706fa
struct slbt_exec_ctx_impl {
280b99
	const struct slbt_driver_ctx *	dctx;
f42074
	struct slbt_symlist_ctx *       sctx;
f42074
	struct slbt_exec_ctx            ctx;
39e52e
	struct slbt_archive_ctx **      dlactxv;
a07095
	struct slbt_archive_ctx *       dlpreopen;
39e52e
	char **                         dlargv;
d577a3
	int                             argc;
d577a3
	char *                          args;
d577a3
	char *                          shadow;
d577a3
	char *                          dsoprefix;
d577a3
	size_t                          size;
e9a1d6
	size_t                          exts;
d577a3
	int                             fdwrapper;
39e52e
	char                            sbuf[PATH_MAX];
d577a3
	char **                         lout[2];
d577a3
	char **                         mout[2];
55d6e3
	char **                         vbuffer;
9706fa
};
9706fa
7bab5a
struct slbt_archive_ctx_impl {
b1d1a0
	const struct slbt_driver_ctx *	dctx;
7bab5a
	const char *			path;
9a3246
	char *                          pathbuf;
7bab5a
	struct slbt_raw_archive		map;
7bab5a
	struct slbt_archive_meta *	meta;
7bab5a
	struct slbt_archive_ctx		actx;
7bab5a
};
7bab5a
d42469
struct slbt_symlist_ctx_impl {
d42469
	const struct slbt_driver_ctx *	dctx;
d42469
	const char *                    path;
d42469
	char *                          pathbuf;
d42469
	char *                          symstrs;
d42469
	const char **                   symstrv;
d42469
	struct slbt_symlist_ctx         sctx;
d42469
};
d42469
8f60d4
b1be90
struct slbt_txtfile_ctx_impl {
b1be90
	const struct slbt_driver_ctx *	dctx;
b1be90
	const char *                    path;
b1be90
	char *                          pathbuf;
b1be90
	char *                          txtlines;
b1be90
	const char **                   txtlinev;
b1be90
	struct slbt_txtfile_ctx         tctx;
b1be90
};
b1be90
83bcd3
struct slbt_stoolie_ctx_impl {
83bcd3
	const struct slbt_driver_ctx *	dctx;
83bcd3
	const char *			path;
83bcd3
	char *                          pathbuf;
83bcd3
	int                             fdtgt;
83bcd3
	int                             fdaux;
83bcd3
	int                             fdm4;
83bcd3
	const char *                    auxarg;
83bcd3
	char *                          auxbuf;
83bcd3
	const char *                    m4arg;
83bcd3
	char *                          m4buf;
83bcd3
	char **                         m4argv;
83bcd3
	struct slbt_txtfile_ctx *       acinc;
83bcd3
	struct slbt_txtfile_ctx *       cfgac;
83bcd3
	struct slbt_txtfile_ctx *       makam;
83bcd3
	struct slbt_stoolie_ctx		zctx;
83bcd3
};
b1be90
99c275
const char * slbt_program_name(const char *);
99c275
99c275
c3d88b
int slbt_optv_init(
c3d88b
	const struct argv_option[],
c3d88b
	const struct argv_option **);
c3d88b
c3d88b
1ed017
uint64_t slbt_argv_flags(uint64_t flags);
1ed017
1ed017
c3d88b
void slbt_argv_scan(
c3d88b
	char **				argv,
c3d88b
	const struct argv_option **	optv,
c3d88b
	struct argv_ctx *		ctx,
c3d88b
	struct argv_meta *		meta);
c3d88b
c3d88b
c3d88b
struct argv_meta * slbt_argv_get(
c3d88b
	char **,
c3d88b
	const struct argv_option **,
c3d88b
	int flags,
c3d88b
	int fd);
c3d88b
c3d88b
void slbt_argv_free(struct argv_meta *);
c3d88b
c3d88b
6b2413
void slbt_argv_usage(
6b2413
	int		fd,
6b2413
	const char *	header,
6b2413
	const struct	argv_option **,
6b2413
	const char *	mode);
6b2413
6b2413
6b2413
void slbt_argv_usage_plain(
6b2413
	int		fd,
6b2413
	const char *	header,
6b2413
	const struct	argv_option **,
6b2413
	const char *	mode);
6b2413
6b2413
1ed017
int slbt_driver_usage(
1ed017
	int				fdout,
1ed017
	const char *			program,
1ed017
	const char *			arg,
1ed017
	const struct argv_option **	optv,
1ed017
	struct argv_meta *		meta,
1ed017
	struct slbt_split_vector *	sargv,
1ed017
	struct slbt_obj_list *		objlistv,
1ed017
	int				noclr);
1ed017
1ed017
1ed017
int slbt_split_argv(
1ed017
	char **				argv,
1ed017
	uint64_t			flags,
1ed017
	struct slbt_split_vector *	sargv,
1ed017
	struct slbt_obj_list **		aobjlistv,
1ed017
	int				fderr,
1ed017
	int				fdcwd);
1ed017
1ed017
99c275
int slbt_init_version_info(
99c275
	struct slbt_driver_ctx_impl *	ictx,
99c275
	struct slbt_version_info *	verinfo);
99c275
99c275
8f60d4
int slbt_init_host_params(
8f60d4
	const struct slbt_driver_ctx *	dctx,
8f60d4
	const struct slbt_common_ctx *	cctx,
8f60d4
	struct slbt_host_strs *		drvhost,
8f60d4
	struct slbt_host_params *	host,
8f60d4
	struct slbt_host_params *	cfgmeta,
771899
	const char *                    cfgmeta_host,
8f60d4
	const char *                    cfgmeta_ar,
b4058c
	const char *                    cfgmeta_as,
b5e104
	const char *                    cfgmeta_nm,
fc8ee9
	const char *                    cfgmeta_ranlib,
fc8ee9
	const char *                    cfgmeta_dlltool);
8f60d4
8f60d4
8f60d4
void slbt_free_host_params(struct slbt_host_strs * host);
8f60d4
8f60d4
741c4d
int slbt_init_link_params(struct slbt_driver_ctx_impl * ctx);
741c4d
741c4d
8f60d4
void slbt_init_flavor_settings(
8f60d4
	struct slbt_common_ctx *	cctx,
8f60d4
	const struct slbt_host_params * ahost,
8f60d4
	struct slbt_flavor_settings *	psettings);
8f60d4
8f60d4
8f60d4
int slbt_init_ldrpath(
8f60d4
	struct slbt_common_ctx *  cctx,
8f60d4
	struct slbt_host_params * host);
8f60d4
8f60d4
f3d47a
void slbt_reset_placeholders   (struct slbt_exec_ctx *);
f3d47a
f3d47a
void slbt_disable_placeholders (struct slbt_exec_ctx *);
f3d47a
3d2f83
int slbt_impl_get_txtfile_ctx(
3d2f83
	const struct slbt_driver_ctx *  dctx,
3d2f83
	const char *                    path,
3d2f83
	int                             fdsrc,
3d2f83
	struct slbt_txtfile_ctx **      pctx);
3d2f83
f3d47a
5d46be
static inline struct slbt_archive_ctx_impl * slbt_get_archive_ictx(const struct slbt_archive_ctx * actx)
5d46be
{
5d46be
	uintptr_t addr;
5d46be
5d46be
	if (actx) {
5d46be
		addr = (uintptr_t)actx - offsetof(struct slbt_archive_ctx_impl,actx);
5d46be
		return (struct slbt_archive_ctx_impl *)addr;
5d46be
	}
5d46be
5d46be
	return 0;
5d46be
}
5d46be
3a8090
static inline struct slbt_driver_ctx_impl * slbt_get_driver_ictx(const struct slbt_driver_ctx * dctx)
3a8090
{
3a8090
	uintptr_t addr;
3a8090
3a8090
	if (dctx) {
3a8090
		addr = (uintptr_t)dctx - offsetof(struct slbt_driver_ctx_impl,ctx);
3a8090
		return (struct slbt_driver_ctx_impl *)addr;
3a8090
	}
3a8090
3a8090
	return 0;
3a8090
}
3a8090
f7b3fd
static inline struct slbt_symlist_ctx_impl * slbt_get_symlist_ictx(const struct slbt_symlist_ctx * sctx)
f7b3fd
{
f7b3fd
	uintptr_t addr;
f7b3fd
f7b3fd
	if (sctx) {
f7b3fd
		addr = (uintptr_t)sctx - offsetof(struct slbt_symlist_ctx_impl,sctx);
f7b3fd
		return (struct slbt_symlist_ctx_impl *)addr;
f7b3fd
	}
f7b3fd
f7b3fd
	return 0;
f7b3fd
}
f7b3fd
0dcc39
static inline struct slbt_stoolie_ctx_impl * slbt_get_stoolie_ictx(const struct slbt_stoolie_ctx * stctx)
0dcc39
{
0dcc39
	uintptr_t addr;
0dcc39
0dcc39
	if (stctx) {
0dcc39
		addr = (uintptr_t)stctx - offsetof(struct slbt_stoolie_ctx_impl,zctx);
0dcc39
		return (struct slbt_stoolie_ctx_impl *)addr;
0dcc39
	}
0dcc39
0dcc39
	return 0;
0dcc39
}
0dcc39
7bab5a
static inline void slbt_driver_set_arctx(
7bab5a
	const struct slbt_driver_ctx *  dctx,
7bab5a
	const struct slbt_archive_ctx * arctx,
7bab5a
	const char *                    arpath)
7bab5a
{
7bab5a
	struct slbt_driver_ctx_impl * ictx;
7bab5a
7bab5a
7bab5a
	ictx         = slbt_get_driver_ictx(dctx);
7bab5a
	ictx->arctx  = arctx;
7bab5a
	ictx->arpath = arpath;
7bab5a
}
7bab5a
e8bfe5
static inline char ** slbt_driver_envp(const struct slbt_driver_ctx * dctx)
e8bfe5
{
e8bfe5
	struct slbt_driver_ctx_impl * ictx;
e8bfe5
	ictx = slbt_get_driver_ictx(dctx);
e8bfe5
	return ictx->envp;
e8bfe5
}
e8bfe5
a82cc2
static inline int slbt_driver_fdin(const struct slbt_driver_ctx * dctx)
a82cc2
{
a82cc2
	struct slbt_fd_ctx fdctx;
ea4137
	slbt_lib_get_driver_fdctx(dctx,&fdctx);
a82cc2
	return fdctx.fdin;
a82cc2
}
a82cc2
a82cc2
static inline int slbt_driver_fdout(const struct slbt_driver_ctx * dctx)
a82cc2
{
a82cc2
	struct slbt_fd_ctx fdctx;
ea4137
	slbt_lib_get_driver_fdctx(dctx,&fdctx);
a82cc2
	return fdctx.fdout;
a82cc2
}
a82cc2
a82cc2
static inline int slbt_driver_fderr(const struct slbt_driver_ctx * dctx)
a82cc2
{
a82cc2
	struct slbt_fd_ctx fdctx;
ea4137
	slbt_lib_get_driver_fdctx(dctx,&fdctx);
a82cc2
	return fdctx.fderr;
a82cc2
}
a82cc2
a82cc2
static inline int slbt_driver_fdlog(const struct slbt_driver_ctx * dctx)
a82cc2
{
a82cc2
	struct slbt_fd_ctx fdctx;
ea4137
	slbt_lib_get_driver_fdctx(dctx,&fdctx);
a82cc2
	return fdctx.fdlog;
a82cc2
}
a82cc2
ca72f5
static inline int slbt_driver_fdcwd(const struct slbt_driver_ctx * dctx)
ca72f5
{
ca72f5
	struct slbt_fd_ctx fdctx;
ea4137
	slbt_lib_get_driver_fdctx(dctx,&fdctx);
ca72f5
	return fdctx.fdcwd;
ca72f5
}
ca72f5
ca72f5
static inline int slbt_driver_fddst(const struct slbt_driver_ctx * dctx)
ca72f5
{
ca72f5
	struct slbt_fd_ctx fdctx;
ea4137
	slbt_lib_get_driver_fdctx(dctx,&fdctx);
ca72f5
	return fdctx.fddst;
ca72f5
}
ca72f5
9706fa
static inline struct slbt_exec_ctx_impl * slbt_get_exec_ictx(const struct slbt_exec_ctx * ectx)
9706fa
{
9706fa
	uintptr_t addr;
9706fa
9706fa
	addr = (uintptr_t)ectx - offsetof(struct slbt_exec_ctx_impl,ctx);
9706fa
	return (struct slbt_exec_ctx_impl *)addr;
9706fa
}
9706fa
9706fa
static inline int slbt_exec_get_fdwrapper(const struct slbt_exec_ctx * ectx)
9706fa
{
9706fa
	struct slbt_exec_ctx_impl * ictx;
9706fa
	ictx = slbt_get_exec_ictx(ectx);
9706fa
	return ictx->fdwrapper;
9706fa
}
9706fa
9706fa
static inline void slbt_exec_set_fdwrapper(const struct slbt_exec_ctx * ectx, int fd)
9706fa
{
9706fa
	struct slbt_exec_ctx_impl * ictx;
9706fa
	ictx = slbt_get_exec_ictx(ectx);
9706fa
	ictx->fdwrapper = fd;
9706fa
}
9706fa
9706fa
static inline void slbt_exec_close_fdwrapper(const struct slbt_exec_ctx * ectx)
9706fa
{
9706fa
	struct slbt_exec_ctx_impl * ictx;
9706fa
	ictx = slbt_get_exec_ictx(ectx);
9706fa
	close(ictx->fdwrapper);
9706fa
	ictx->fdwrapper = (-1);
9706fa
}
9706fa
9ca8c4
#endif