Blame src/driver/mdso_driver_ctx.c

cde03b
/****************************************************************/
cde03b
/*  mdso: midipix dso scavenger                                 */
3e816c
/*  Copyright (C) 2015--2016  Z. Gilboa                         */
cde03b
/*  Released under GPLv2 and GPLv3; see COPYING.MDSO.           */
cde03b
/****************************************************************/
cde03b
cde03b
#include <stdint.h>
cde03b
#include <unistd.h>
cde03b
#include <fcntl.h>
938a47
#include <stdbool.h>
938a47
#include <string.h>
938a47
#include <sys/stat.h>
cde03b
cde03b
#define ARGV_DRIVER
cde03b
cde03b
#include <mdso/mdso.h>
8cfad6
#include <mdso/mdso_output.h>
6848b7
#include <mdso/mdso_specs.h>
26498d
#include "mdso_version.h"
cde03b
#include "mdso_driver_impl.h"
cde03b
#include "argv/argv.h"
cde03b
26498d
/* package info */
26498d
static const struct mdso_source_version mdso_src_version = {
26498d
	MDSO_TAG_VER_MAJOR,
26498d
	MDSO_TAG_VER_MINOR,
26498d
	MDSO_TAG_VER_PATCH,
26498d
	MDSO_GIT_VERSION
26498d
};
26498d
cde03b
struct mdso_driver_ctx_alloc {
cde03b
	struct argv_meta *		meta;
cde03b
	struct mdso_driver_ctx_impl	ctx;
cde03b
	uint64_t			guard;
cde03b
	const char *			units[];
cde03b
};
cde03b
cde03b
static uint32_t mdso_argv_flags(uint32_t flags)
cde03b
{
cde03b
	uint32_t ret = 0;
cde03b
cde03b
	if (flags & MDSO_DRIVER_VERBOSITY_NONE)
cde03b
		ret |= ARGV_VERBOSITY_NONE;
cde03b
cde03b
	if (flags & MDSO_DRIVER_VERBOSITY_ERRORS)
cde03b
		ret |= ARGV_VERBOSITY_ERRORS;
cde03b
cde03b
	if (flags & MDSO_DRIVER_VERBOSITY_STATUS)
cde03b
		ret |= ARGV_VERBOSITY_STATUS;
cde03b
cde03b
	return ret;
cde03b
}
cde03b
cde03b
static int mdso_driver_usage(
cde03b
	const char *			program,
cde03b
	const char *			arg,
cde03b
	const struct argv_option *	options,
cde03b
	struct argv_meta *		meta)
cde03b
{
cde03b
	char header[512];
cde03b
cde03b
	snprintf(header,sizeof(header),
cde03b
		"Usage: %s [options] <file>...\n" "Options:\n",
cde03b
		program);
cde03b
cde03b
	argv_usage(stdout,header,options,arg);
cde03b
	argv_free(meta);
cde03b
cde03b
	return MDSO_USAGE;
cde03b
}
cde03b
938a47
static struct mdso_driver_ctx_impl * mdso_driver_ctx_alloc(
938a47
	struct argv_meta *		meta,
938a47
	const struct mdso_common_ctx *	cctx,
938a47
	size_t				nunits)
cde03b
{
cde03b
	struct mdso_driver_ctx_alloc *	ictx;
cde03b
	size_t				size;
cde03b
	struct argv_entry *		entry;
cde03b
	const char **			units;
cde03b
cde03b
	size =  sizeof(struct mdso_driver_ctx_alloc);
cde03b
	size += (nunits+1)*sizeof(const char *);
cde03b
bea9e7
	if (!(ictx = calloc(1,size)))
cde03b
		return 0;
cde03b
938a47
	if (cctx)
938a47
		memcpy(&ictx->ctx.cctx,cctx,sizeof(*cctx));
938a47
cde03b
	for (entry=meta->entries,units=ictx->units; entry->fopt || entry->arg; entry++)
cde03b
		if (!entry->fopt)
cde03b
			*units++ = entry->arg;
cde03b
777073
	ictx->meta = meta;
db4864
	ictx->ctx.fdtmpin = -1;
cde03b
	ictx->ctx.ctx.units = ictx->units;
cde03b
	return &ictx->ctx;
cde03b
}
cde03b
938a47
static int mdso_dstdir_open(const struct mdso_common_ctx * cctx, const char * asmbase)
938a47
{
938a47
	int fdtop;
938a47
	int fddst;
938a47
	const int dirmode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
938a47
938a47
	if ((fdtop = openat(AT_FDCWD,cctx->dstdir,O_DIRECTORY)) < 0) {
938a47
		if (mkdirat(AT_FDCWD,cctx->dstdir,dirmode) < 0)
938a47
			return -1;
938a47
		else if ((fdtop = openat(AT_FDCWD,cctx->dstdir,O_DIRECTORY)) < 0)
938a47
			return -1;
938a47
	}
938a47
938a47
	if ((fddst = openat(fdtop,asmbase,O_DIRECTORY)) < 0) {
938a47
		if (mkdirat(fdtop,asmbase,dirmode) < 0)
938a47
			return -1;
938a47
		else if ((fddst = openat(fdtop,asmbase,O_DIRECTORY)) < 0)
938a47
			return -1;
938a47
	}
938a47
938a47
	close(fdtop);
938a47
	return fddst;
938a47
}
938a47
938a47
static int mdso_get_driver_ctx_fail(
938a47
	struct argv_meta *	meta,
938a47
	char *			asmbase,
938a47
	int			fddst)
cde03b
{
938a47
	if (fddst >= 0)
938a47
		close(fddst);
938a47
938a47
	if (asmbase)
938a47
		free(asmbase);
938a47
cde03b
	argv_free(meta);
cde03b
	return -1;
cde03b
}
cde03b
cde03b
int mdso_get_driver_ctx(
13aa9b
	char **			argv,
13aa9b
	char **			envp,
cde03b
	uint32_t		flags,
cde03b
	struct mdso_driver_ctx ** pctx)
cde03b
{
cde03b
	struct mdso_driver_ctx_impl *	ctx;
cde03b
	struct mdso_common_ctx		cctx;
cde03b
	const struct argv_option *	options;
cde03b
	struct argv_meta *		meta;
cde03b
	struct argv_entry *		entry;
cde03b
	size_t				nunits;
cde03b
	const char *			program;
8cfad6
	const char *			pretty;
938a47
	char *				asmbase;
938a47
	char *				dot;
938a47
	int				fddst;
cde03b
292143
	(void)envp;
292143
cde03b
	options = mdso_default_options;
cde03b
cde03b
	if (!(meta = argv_get(argv,options,mdso_argv_flags(flags))))
cde03b
		return -1;
cde03b
cde03b
	nunits	= 0;
8cfad6
	pretty	= 0;
938a47
	asmbase = 0;
938a47
	fddst	= -1;
cde03b
	program = argv_program_name(argv[0]);
cde03b
	memset(&cctx,0,sizeof(cctx));
3b54fd
	cctx.drvflags = flags;
cde03b
cde03b
	if (!argv[1] && (flags & MDSO_DRIVER_VERBOSITY_USAGE))
cde03b
		return mdso_driver_usage(program,0,options,meta);
cde03b
cde03b
	/* get options, count units */
cde03b
	for (entry=meta->entries; entry->fopt || entry->arg; entry++) {
cde03b
		if (entry->fopt) {
cde03b
			switch (entry->tag) {
cde03b
				case TAG_HELP:
cde03b
					if (flags & MDSO_DRIVER_VERBOSITY_USAGE)
cde03b
						return mdso_driver_usage(program,entry->arg,options,meta);
cde03b
cde03b
				case TAG_VERSION:
cde03b
					cctx.drvflags |= MDSO_DRIVER_VERSION;
cde03b
					break;
8cfad6
7c0ec4
				case TAG_QUAD_PTR:
7c0ec4
					cctx.drvflags |= MDSO_DRIVER_QUAD_PTR;
7c0ec4
					break;
7c0ec4
6848b7
				case TAG_LIBPATH:
6848b7
					if (!(strcmp(entry->arg,"loader")))
6848b7
						cctx.dsoflags |= MDSO_FLAG_LOADER_PATH;
6848b7
					if (!(strcmp(entry->arg,"peb")))
6848b7
						cctx.dsoflags |= MDSO_FLAG_PEB_PATH;
6848b7
					if (!(strcmp(entry->arg,"system32")))
6848b7
						cctx.dsoflags |= MDSO_FLAG_SYSTEM32;
6848b7
					break;
6848b7
d8c2b3
				case TAG_LIBNAME:
d8c2b3
					cctx.libname = entry->arg;
d8c2b3
					break;
d8c2b3
d8c2b3
				case TAG_DSTDIR:
d8c2b3
					cctx.dstdir = entry->arg;
d8c2b3
					break;
d8c2b3
8cfad6
				case TAG_PRETTY:
8cfad6
					pretty = entry->arg;
8cfad6
					break;
8cfad6
8cfad6
				case TAG_EXPSYMS:
8cfad6
					cctx.fmtflags |= MDSO_OUTPUT_EXPORT_SYMS;
8cfad6
					break;
cde03b
			}
cde03b
		} else
cde03b
			nunits++;
cde03b
	}
cde03b
57f0fb
	if (!nunits && !(cctx.drvflags & MDSO_DRIVER_VERSION))
57f0fb
		return mdso_driver_usage(program,0,options,meta);
57f0fb
8cfad6
	if (pretty && !strcmp(pretty,"yaml"))
8cfad6
		cctx.fmtflags |= MDSO_PRETTY_YAML;
8cfad6
938a47
	if (!cctx.libname)
938a47
		cctx.libname = "win32any";
938a47
938a47
	if (!(asmbase = strdup(cctx.libname)))
938a47
		return mdso_get_driver_ctx_fail(meta,asmbase,fddst);
938a47
938a47
	if ((dot = strchr(asmbase,'.')))
938a47
		*dot = '\0';
938a47
938a47
	if (cctx.dstdir && (fddst = mdso_dstdir_open(&cctx,asmbase)) < 0)
938a47
		return mdso_get_driver_ctx_fail(meta,asmbase,fddst);
938a47
938a47
	if (!(ctx = mdso_driver_ctx_alloc(meta,&cctx,nunits)))
938a47
		return mdso_get_driver_ctx_fail(meta,asmbase,fddst);
cde03b
938a47
	ctx->asmbase		= asmbase;
938a47
	ctx->cctx.asmbase	= asmbase;
938a47
938a47
	ctx->fddst		= fddst;
cde03b
	ctx->ctx.program	= program;
cde03b
	ctx->ctx.cctx		= &ctx->cctx;
cde03b
cde03b
	*pctx = &ctx->ctx;
cde03b
	return MDSO_OK;
cde03b
}
cde03b
cde03b
int mdso_create_driver_ctx(
cde03b
	const struct mdso_common_ctx *	cctx,
cde03b
	struct mdso_driver_ctx **	pctx)
cde03b
{
cde03b
	struct argv_meta *		meta;
cde03b
	struct mdso_driver_ctx_impl *	ctx;
938a47
	int				fddst  = -1;
13aa9b
	char *				argv[] = {"mdso_driver",0};
cde03b
cde03b
	if (!(meta = argv_get(argv,mdso_default_options,0)))
cde03b
		return -1;
cde03b
938a47
	if (cctx->dstdir && (fddst = mdso_dstdir_open(cctx,cctx->asmbase)) < 0)
938a47
		return mdso_get_driver_ctx_fail(meta,0,fddst);
938a47
938a47
	if (!(ctx = mdso_driver_ctx_alloc(meta,cctx,0)))
938a47
		return mdso_get_driver_ctx_fail(meta,0,fddst);
cde03b
cde03b
	ctx->ctx.cctx = &ctx->cctx;
cde03b
	*pctx = &ctx->ctx;
cde03b
	return MDSO_OK;
cde03b
}
cde03b
cde03b
static void mdso_free_driver_ctx_impl(struct mdso_driver_ctx_alloc * ictx)
cde03b
{
938a47
	if (ictx->ctx.fddst >= 0)
938a47
		close(ictx->ctx.fddst);
938a47
db4864
	if (ictx->ctx.fdtmpin >= 0)
db4864
		close(ictx->ctx.fdtmpin);
db4864
938a47
	if (ictx->ctx.asmbase)
938a47
		free(ictx->ctx.asmbase);
938a47
cde03b
	argv_free(ictx->meta);
cde03b
	free(ictx);
cde03b
}
cde03b
cde03b
void mdso_free_driver_ctx(struct mdso_driver_ctx * ctx)
cde03b
{
cde03b
	struct mdso_driver_ctx_alloc *	ictx;
cde03b
	uintptr_t			addr;
cde03b
cde03b
	if (ctx) {
cde03b
		addr = (uintptr_t)ctx - offsetof(struct mdso_driver_ctx_alloc,ctx);
cde03b
		addr = addr - offsetof(struct mdso_driver_ctx_impl,ctx);
cde03b
		ictx = (struct mdso_driver_ctx_alloc *)addr;
cde03b
		mdso_free_driver_ctx_impl(ictx);
cde03b
	}
cde03b
}
26498d
26498d
const struct mdso_source_version * mdso_source_version(void)
26498d
{
26498d
	return &mdso_src_version;
26498d
}