Blame src/util/mdso_create_implib_archive.c

c70c0a
/****************************************************************/
c70c0a
/*  mdso: midipix dso scavenger                                 */
c70c0a
/*  Copyright (C) 2015--2017  Z. Gilboa                         */
c70c0a
/*  Released under GPLv2 and GPLv3; see COPYING.MDSO.           */
c70c0a
/****************************************************************/
c70c0a
c70c0a
#include <stdint.h>
c70c0a
#include <stdio.h>
c70c0a
#include <stdlib.h>
c70c0a
#include <string.h>
c70c0a
c70c0a
#include <mdso/mdso.h>
c70c0a
#include "mdso_errinfo_impl.h"
c70c0a
12fb1d
static void mdso_free_uctx_vector(struct mdso_unit_ctx ** uctxv, FILE * fout)
c70c0a
{
c70c0a
	struct mdso_unit_ctx **	puctx;
c70c0a
c70c0a
	for (puctx=uctxv; *puctx; puctx++)
c70c0a
		mdso_free_unit_ctx(*puctx);
c70c0a
c70c0a
	free(uctxv);
12fb1d
	fclose(fout);
c70c0a
}
c70c0a
e3b1f2
static int mdso_symcmp(const void * src, const void * dst)
e3b1f2
{
e3b1f2
	return strcmp(*(const char **)src,*(const char **)dst);
e3b1f2
}
e3b1f2
c70c0a
int  mdso_create_implib_archive(const struct mdso_driver_ctx * dctx)
c70c0a
{
c70c0a
	int			ret;
c70c0a
	FILE *			fout;
c70c0a
	size_t			nsym;
c70c0a
	struct mdso_unit_ctx **	uctxv;
c70c0a
	struct mdso_unit_ctx **	puctx;
c70c0a
	const char * const *	dsym;
c70c0a
	const char **		unit;
c70c0a
	const char **		psym;
c70c0a
	const char **		symv;
c70c0a
	const char *		asym[512];
c70c0a
c70c0a
	if (!dctx->cctx->implib)
c70c0a
		return MDSO_CUSTOM_ERROR(dctx,MDSO_ERR_INVALID_NAME);
c70c0a
c70c0a
	for (unit=dctx->units; *unit; unit++)
c70c0a
		(void)0;
c70c0a
c70c0a
	if (!(uctxv = calloc(++unit - dctx->units,sizeof(*uctxv))))
c70c0a
		return MDSO_SYSTEM_ERROR(dctx);
c70c0a
12fb1d
	if (!(fout = mdso_create_archive(dctx,dctx->cctx->implib)))
12fb1d
		return MDSO_NESTED_ERROR(dctx);
12fb1d
c70c0a
	for (puctx=uctxv,unit=dctx->units; *unit; unit++) {
c70c0a
		if (mdso_get_unit_ctx(dctx,*unit,puctx)) {
12fb1d
			mdso_free_uctx_vector(uctxv,fout);
c70c0a
			return MDSO_NESTED_ERROR(dctx);
c70c0a
		}
c70c0a
	}
c70c0a
c70c0a
	for (nsym=0,puctx=uctxv; *puctx; puctx++)
c70c0a
		for (dsym=puctx[0]->syms; *dsym; dsym++)
c70c0a
			nsym++;
c70c0a
c70c0a
	if (nsym < 512) {
c70c0a
		memset(asym,0,sizeof(asym));
c70c0a
		symv = asym;
c70c0a
c70c0a
	} else if (!(symv = calloc(nsym+1,sizeof(const char *)))) {
12fb1d
		mdso_free_uctx_vector(uctxv,fout);
c70c0a
		return MDSO_SYSTEM_ERROR(dctx);
c70c0a
	}
c70c0a
c70c0a
	for (psym=symv,puctx=uctxv; *puctx; puctx++)
c70c0a
		for (dsym=puctx[0]->syms; *dsym; dsym++)
c70c0a
			*psym++ = *dsym;
c70c0a
e3b1f2
	qsort(symv,nsym,sizeof(*symv),mdso_symcmp);
c70c0a
	ret = mdso_argen_common(dctx,symv,fout,0);
c70c0a
c70c0a
	if (symv != asym)
c70c0a
		free(symv);
c70c0a
12fb1d
	mdso_free_uctx_vector(uctxv,fout);
c70c0a
c70c0a
	return ret ? MDSO_NESTED_ERROR(dctx) : 0;
c70c0a
}