Blame src/driver/mdso_unit_ctx.c

cde03b
/****************************************************************/
cde03b
/*  mdso: midipix dso scavenger                                 */
78ab3c
/*  Copyright (C) 2015--2017  Z. Gilboa                         */
cde03b
/*  Released under GPLv2 and GPLv3; see COPYING.MDSO.           */
cde03b
/****************************************************************/
cde03b
cde03b
#include <stdint.h>
cde03b
#include <stddef.h>
cde03b
#include <stdlib.h>
cde03b
#include <string.h>
14073f
#include <unistd.h>
14073f
#include <errno.h>
cde03b
#include <sys/mman.h>
cde03b
cde03b
#include <mdso/mdso.h>
cde03b
#include "mdso_driver_impl.h"
4eae28
#include "mdso_errinfo_impl.h"
cde03b
cde03b
static int mdso_free_unit_ctx_impl(struct mdso_unit_ctx_impl * ctx, int status)
cde03b
{
cde03b
	if (ctx) {
43f283
		if (ctx->expsyms && ctx->expsyms->buffer)
43f283
			free(ctx->expsyms->buffer);
43f283
63cd76
		if (ctx->expsyms && ctx->expsyms->stype)
63cd76
			free(ctx->expsyms->stype);
63cd76
43f283
		if (ctx->expsyms)
43f283
			free(ctx->expsyms);
43f283
cde03b
		mdso_unmap_input(&ctx->map);
cde03b
		free(ctx);
cde03b
	}
cde03b
cde03b
	return status;
cde03b
}
cde03b
8a4328
static int mdso_stdin_to_tmp(const struct mdso_driver_ctx * dctx)
14073f
{
db4864
	struct mdso_driver_ctx_impl *	ictx;
db4864
	uintptr_t			addr;
db4864
	int				fdtmp;
db4864
8a4328
	ssize_t ret;
8a4328
	ssize_t cnt;
8a4328
	char *	ch;
14073f
	char	buf[4096];
8a4328
	char	template[] = "/tmp/mdso_stdin_to_tmp_XXXXXX";
14073f
db4864
	addr = (uintptr_t)dctx - offsetof(struct mdso_driver_ctx_impl,ctx);
db4864
	ictx = (struct mdso_driver_ctx_impl *)addr;
db4864
8a4328
	if (ictx->fdtmpin >= 0)
8a4328
		return dup(ictx->fdtmpin);
db4864
8a4328
	if ((fdtmp = mkstemp(template)) < 0)
8a4328
		return -1;
919cb6
8a4328
	if ((ictx->fdtmpin = dup(fdtmp)) < 0) {
8a4328
		close(fdtmp);
8a4328
		return -1;
db4864
	}
db4864
b5540e
	for (;;) {
8a4328
		ret = read(0,buf,sizeof(buf)-1);
14073f
8a4328
		while ((ret < 0) && (errno == EINTR))
8a4328
			ret = read(0,buf,sizeof(buf)-1);
db4864
8a4328
		if (ret < 0) {
8a4328
			close(fdtmp);
8a4328
			return -1;
14073f
8a4328
		} else if (ret == 0) {
8a4328
			return fdtmp;
14073f
8a4328
		} else {
8a4328
			ch  = buf;
8a4328
			cnt = ret;
8a4328
			ret = 0;
14073f
8a4328
			for (; cnt; ) {
8a4328
				ret = write(fdtmp,ch,cnt);
14073f
8a4328
				while ((ret < 0) && (errno == EINTR))
8a4328
					ret = write(fdtmp,ch,cnt);
8a4328
8a4328
				if (ret < 0) {
8a4328
					close(fdtmp);
8a4328
					return -1;
8a4328
				}
8a4328
8a4328
				ch  += ret;
8a4328
				cnt -= ret;
8a4328
			}
8a4328
		}
8a4328
	}
14073f
}
14073f
43f283
static int mdso_create_symbol_vector(struct mdso_unit_ctx_impl * ctx)
43f283
{
43f283
	int		nsyms;
43f283
	size_t		nbytes;
43f283
	size_t		size;
43f283
	char *		dst;
63cd76
	const char *	base;
43f283
	const char *	ch;
43f283
	const char **	sym;
63cd76
	int		stype;
43f283
43f283
	const char	exphdr[] = "EXPORTS\n";
43f283
	const char	imphdr[] = "IMPORTS\n";
43f283
43f283
	ch	= ctx->map.addr;
43f283
	nbytes	= ctx->map.size;
43f283
43f283
	for (nsyms=0; nbytes; ch++,nbytes--)
43f283
		nsyms += (*ch == '\n');
43f283
43f283
	size = offsetof(struct mdso_unit_ctx_impl,expsyms);
63cd76
	size += (++nsyms)*sizeof(const char *);
43f283
bea9e7
	if (!(ctx->expsyms = calloc(1,size)))
43f283
		return -1;
43f283
bea9e7
	if (!(ctx->expsyms->buffer = calloc(1,ctx->map.size)))
43f283
		return -1;
43f283
63cd76
	if (!(ctx->expsyms->stype = calloc(nsyms,sizeof(int))))
63cd76
		return -1;
63cd76
63cd76
	base	= ctx->map.addr;
43f283
	ch	= ctx->map.addr;
43f283
	nbytes	= ctx->map.size;
43f283
	sym	= ctx->expsyms->syms;
43f283
	dst	= ctx->expsyms->buffer;
43f283
	size	= strlen(exphdr);
43f283
43f283
	/* support .def input files */
43f283
	if ((nbytes >= size) && !(strncmp(ch,exphdr,size))) {
43f283
		ch	+= size;
43f283
		nbytes	-= size;
43f283
	}
43f283
43f283
	/* advance to first symbol */
43f283
	for (; nbytes && ((*ch==' ')
43f283
			|| (*ch=='\t')
43f283
			|| (*ch=='\r')
43f283
			|| (*ch=='\n')); nbytes--)
43f283
		ch++;
43f283
43f283
	/* support .def input files */
43f283
	size = strlen(imphdr);
43f283
43f283
	while (nbytes && ((nbytes < size) || (strncmp(ch,imphdr,size)))) {
43f283
		/* vector */
63cd76
		*sym = dst;
43f283
43f283
		/* symbol */
43f283
		for (; nbytes && ((*ch!=' ')
43f283
				&& (*ch!='\t')
43f283
				&& (*ch!='\r')
43f283
				&& (*ch!='\n')); nbytes--)
43f283
			*dst++ = *ch++;
43f283
43f283
		dst++;
43f283
43f283
		/* discard rest of input line */
43f283
		for (; nbytes && (*ch!='\n'); nbytes--)
43f283
			ch++;
43f283
63cd76
		/* stype */
63cd76
		stype = ((*ch=='\n')
63cd76
				&& ((ch-base) >= 6)
63cd76
				&& !strncmp(&ch[-5]," DATA\n",6))
63cd76
			? MDSO_SYMBOL_TYPE_DATA
63cd76
			: MDSO_SYMBOL_TYPE_CODE;
63cd76
63cd76
		ctx->expsyms->stype[sym - ctx->expsyms->syms] = stype;
63cd76
		sym++;
63cd76
43f283
		/* advance to next symbol */
43f283
		for (; nbytes && ((*ch==' ')
43f283
				|| (*ch=='\t')
43f283
				|| (*ch=='\r')
43f283
				|| (*ch=='\n')); nbytes--)
43f283
			ch++;
43f283
	}
43f283
43f283
	return 0;
43f283
}
43f283
cde03b
int mdso_get_unit_ctx(
cde03b
	const struct mdso_driver_ctx *	dctx,
cde03b
	const char *			path,
cde03b
	struct mdso_unit_ctx **		pctx)
cde03b
{
cde03b
	struct mdso_unit_ctx_impl *	ctx;
14073f
	int				fd;
cde03b
4eae28
	if (!dctx)
4eae28
		return MDSO_CUSTOM_ERROR(dctx,0);
4eae28
4eae28
	else if (!(ctx = calloc(1,sizeof(*ctx))))
4eae28
		return MDSO_BUFFER_ERROR(dctx);
cde03b
73d16e
	mdso_driver_set_ectx(
73d16e
		dctx,0,path);
73d16e
14073f
	if (strcmp(path,"-"))
14073f
		fd = -1;
4eae28
8a4328
	else if ((fd = mdso_stdin_to_tmp(dctx)) < 0)
4eae28
		return mdso_free_unit_ctx_impl(
4eae28
			ctx,MDSO_FILE_ERROR(dctx));
4eae28
51430e
	if (mdso_map_input(dctx,fd,path,PROT_READ,&ctx->map))
4eae28
		return mdso_free_unit_ctx_impl(
51430e
			ctx,MDSO_NESTED_ERROR(dctx));
14073f
8a4328
	if (fd >= 0)
14073f
		close(fd);
cde03b
43f283
	if (mdso_create_symbol_vector(ctx))
4eae28
		return mdso_free_unit_ctx_impl(
4eae28
			ctx,MDSO_BUFFER_ERROR(dctx));
43f283
cde03b
	memcpy(&ctx->cctx,dctx->cctx,
cde03b
		sizeof(ctx->cctx));
cde03b
cde03b
	ctx->path	= path;
cde03b
cde03b
	ctx->uctx.path	= &ctx->path;
cde03b
	ctx->uctx.map	= &ctx->map;
cde03b
	ctx->uctx.cctx	= &ctx->cctx;
43f283
	ctx->uctx.syms	= ctx->expsyms->syms;
63cd76
	ctx->uctx.stype	= ctx->expsyms->stype;
cde03b
cde03b
	*pctx = &ctx->uctx;
8a4328
cde03b
	return 0;
cde03b
}
cde03b
cde03b
void mdso_free_unit_ctx(struct mdso_unit_ctx * ctx)
cde03b
{
cde03b
	struct mdso_unit_ctx_impl *	ictx;
cde03b
	uintptr_t			addr;
cde03b
cde03b
	if (ctx) {
cde03b
		addr = (uintptr_t)ctx - offsetof(struct mdso_unit_ctx_impl,uctx);
cde03b
		ictx = (struct mdso_unit_ctx_impl *)addr;
cde03b
		mdso_free_unit_ctx_impl(ictx,0);
cde03b
	}
cde03b
}