Blame src/helper/mdso_create_output.c

3b42f9
/****************************************************************/
3b42f9
/*  mdso: midipix dso scavenger                                 */
d5fc3d
/*  Copyright (C) 2015--2021  SysDeer Technologies, LLC         */
3b42f9
/*  Released under GPLv2 and GPLv3; see COPYING.MDSO.           */
3b42f9
/****************************************************************/
3b42f9
3b42f9
#include <stdint.h>
3b42f9
#include <stdlib.h>
3b42f9
#include <stdio.h>
3b42f9
#include <stddef.h>
3b42f9
#include <unistd.h>
3b42f9
#include <fcntl.h>
a3e2cf
#include <sys/mman.h>
3b42f9
3b42f9
#include <mdso/mdso.h>
3b42f9
#include "mdso_driver_impl.h"
8cb533
#include "mdso_errinfo_impl.h"
3b42f9
3d38ce
static int mdso_create_output(
3b42f9
	const struct mdso_driver_ctx *	dctx,
512db4
	const char *			name)
3b42f9
{
3d38ce
	int	fddst;
3d38ce
	int	fdout;
3b42f9
512db4
	fddst = mdso_driver_fddst(dctx);
3b42f9
512db4
	if ((fdout = openat(fddst,name,
e1fe0c
			O_CREAT|O_TRUNC|O_RDWR|O_NOCTTY|O_NOFOLLOW,
a3e2cf
			S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
3d38ce
		return MDSO_SYSTEM_ERROR(dctx);
3d38ce
3d38ce
	return fdout;
3d38ce
}
3d38ce
a3e2cf
static int mdso_map_output(
a3e2cf
	const struct mdso_driver_ctx *	dctx,
a3e2cf
	struct mdso_object *		obj,
a3e2cf
	int				fd)
a3e2cf
{
a3e2cf
	void * addr;
a3e2cf
a3e2cf
	if (ftruncate(fd,obj->size)) {
a3e2cf
		close(fd);
a3e2cf
		return MDSO_SYSTEM_ERROR(dctx);
a3e2cf
	}
a3e2cf
a3e2cf
	addr = mmap(
a3e2cf
		0,obj->size,
a3e2cf
		PROT_WRITE,MAP_SHARED,
a3e2cf
		fd,0);
a3e2cf
a3e2cf
	close(fd);
a3e2cf
a3e2cf
	if (addr == MAP_FAILED)
a3e2cf
		return MDSO_SYSTEM_ERROR(dctx);
a3e2cf
a3e2cf
	obj->addr = addr;
a3e2cf
a3e2cf
	return 0;
a3e2cf
}
a3e2cf
a3e2cf
static int mdso_create_mapped_output(
a3e2cf
	const struct mdso_driver_ctx *	dctx,
a3e2cf
	struct mdso_object *		obj)
a3e2cf
{
a3e2cf
	int fd;
a3e2cf
a3e2cf
	if ((fd = mdso_create_output(dctx,obj->name)) < 0)
a3e2cf
		return MDSO_NESTED_ERROR(dctx);
a3e2cf
a3e2cf
	if (mdso_map_output(dctx,obj,fd) < 0)
a3e2cf
		return MDSO_NESTED_ERROR(dctx);
a3e2cf
a3e2cf
	return 0;
a3e2cf
}
a3e2cf
18a091
int mdso_create_asmsrc(
3e55ee
	const struct mdso_driver_ctx *	dctx,
3e55ee
	const char *			asmname)
3e55ee
{
1c93c2
	if (dctx->cctx->dstdir)
1c93c2
		return mdso_create_output(dctx,asmname);
1c93c2
1c93c2
	else if (dctx->cctx->drvflags & MDSO_DRIVER_GENERATE_OBJECTS)
1c93c2
		return mdso_create_output(dctx,asmname);
1c93c2
1c93c2
	else
1c93c2
		return mdso_driver_fdout(dctx);
3e55ee
}
3e55ee
a3e2cf
int mdso_create_object(
3e55ee
	const struct mdso_driver_ctx *	dctx,
a3e2cf
	struct mdso_object *		obj)
3e55ee
{
a3e2cf
	return mdso_create_mapped_output(dctx,obj);
3e55ee
}
2f4c03
2f4c03
int mdso_create_archive(
2f4c03
	const struct mdso_driver_ctx *	dctx,
2f4c03
	struct mdso_object *		obj)
2f4c03
{
2f4c03
	return mdso_create_mapped_output(dctx,obj);
2f4c03
}