Blame src/util/mdso_create_implib_sources.c

81f462
/****************************************************************/
81f462
/*  mdso: midipix dso scavenger                                 */
78ab3c
/*  Copyright (C) 2015--2017  Z. Gilboa                         */
81f462
/*  Released under GPLv2 and GPLv3; see COPYING.MDSO.           */
81f462
/****************************************************************/
81f462
81f462
#include <stdint.h>
81f462
#include <stdio.h>
81f462
#include <string.h>
27f67f
81f462
#include <mdso/mdso.h>
27f67f
#include "mdso_errinfo_impl.h"
81f462
81f462
static void mdso_init_asmname(char * buf, const char * fmt, const char * str)
81f462
{
81f462
	char			hexstr[24];
81f462
	long long unsigned int	crc64;
81f462
81f462
	if (strlen(str) + strlen(fmt) > (PATH_MAX - 1)) {
81f462
		crc64 = mdso_crc64_mbstr((const unsigned char *)str,0);
81f462
		sprintf(hexstr,"%llx",crc64);
81f462
		sprintf(buf,fmt,hexstr);
81f462
	} else
81f462
		sprintf(buf,fmt,str);
81f462
}
81f462
81f462
mdso_api int  mdso_create_implib_sources(const struct mdso_driver_ctx * dctx)
81f462
{
81f462
	struct mdso_unit_ctx *	uctx;
81f462
	const char **		unit;
81f462
	FILE *			fout;
81f462
	char			asmname[PATH_MAX];
81f462
	const char * const *	sym;
81f462
	int			ret;
81f462
81f462
4cffdc
	/* symentry, symfn */
81f462
	for (unit=dctx->units; *unit; unit++) {
81f462
		if (mdso_get_unit_ctx(dctx,*unit,&uctx))
27f67f
			return MDSO_NESTED_ERROR(dctx);
81f462
81f462
		for (sym=uctx->syms; *sym; sym++) {
c0e12c
			mdso_init_asmname(asmname,".%s_symentry.s",*sym);
81f462
19f61b
			if (!(fout = mdso_create_asmsrc(dctx,asmname)))
27f67f
				return MDSO_NESTED_ERROR(dctx);
81f462
2c190d
			ret = mdso_asmgen_symentry(dctx,*sym,fout);
81f462
81f462
			if (fout != stdout)
81f462
				fclose(fout);
81f462
81f462
			if (ret < 0)
27f67f
				return MDSO_NESTED_ERROR(dctx);
81f462
080fdc
			if (uctx->stype[sym-uctx->syms] == MDSO_SYMBOL_TYPE_CODE) {
080fdc
				mdso_init_asmname(asmname,".%s_symfn.s",*sym);
81f462
19f61b
				if (!(fout = mdso_create_asmsrc(dctx,asmname)))
080fdc
					return MDSO_NESTED_ERROR(dctx);
81f462
080fdc
				ret = mdso_asmgen_symfn(dctx,*sym,fout);
81f462
080fdc
				if (fout != stdout)
080fdc
					fclose(fout);
81f462
080fdc
				if (ret < 0)
080fdc
					return MDSO_NESTED_ERROR(dctx);
080fdc
			}
81f462
		}
81f462
81f462
		mdso_free_unit_ctx(uctx);
81f462
	}
81f462
4cffdc
	/* dsometa */
4cffdc
	mdso_init_asmname(asmname,".dsometa_%s.s",dctx->cctx->libname);
4cffdc
19f61b
	if (!(fout = mdso_create_asmsrc(dctx,asmname)))
4cffdc
		return MDSO_NESTED_ERROR(dctx);
4cffdc
4cffdc
	ret = mdso_asmgen_dsometa(dctx,fout);
4cffdc
4cffdc
	if (fout != stdout)
4cffdc
		fclose(fout);
4cffdc
4cffdc
	return (ret < 0) ? MDSO_NESTED_ERROR(dctx) : 0;
81f462
}