summaryrefslogtreecommitdiffhomepage
path: root/src/util/mdso_create_implib_archive.c
blob: e5e5d01738f35f3679c6caa784f5df9c46b9dc30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/****************************************************************/
/*  mdso: midipix dso scavenger                                 */
/*  Copyright (C) 2015--2017  Z. Gilboa                         */
/*  Released under GPLv2 and GPLv3; see COPYING.MDSO.           */
/****************************************************************/

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <mdso/mdso.h>
#include "mdso_errinfo_impl.h"

static void mdso_free_uctx_vector(
	struct mdso_unit_ctx **	uctxv,
	const char **		symv,
	int *			stype,
	FILE *			fout)
{
	struct mdso_unit_ctx **	puctx;

	if (symv)
		free(symv);

	if (stype)
		free(stype);

	for (puctx=uctxv; *puctx; puctx++)
		mdso_free_unit_ctx(*puctx);

	free(uctxv);
	fclose(fout);
}

int  mdso_create_implib_archive(const struct mdso_driver_ctx * dctx)
{
	int			ret;
	FILE *			fout;
	size_t			nsym;
	struct mdso_unit_ctx **	uctxv;
	struct mdso_unit_ctx **	puctx;
	const char * const *	dsym;
	const char **		unit;
	const char **		psym;
	const char **		symv;
	int *			stype;

	if (!dctx->cctx->implib)
		return MDSO_CUSTOM_ERROR(dctx,MDSO_ERR_INVALID_NAME);

	for (unit=dctx->units; *unit; unit++)
		(void)0;

	if (!(uctxv = calloc(++unit - dctx->units,sizeof(*uctxv))))
		return MDSO_SYSTEM_ERROR(dctx);

	if (!(fout = mdso_create_archive(dctx,dctx->cctx->implib)))
		return MDSO_NESTED_ERROR(dctx);

	for (puctx=uctxv,unit=dctx->units; *unit; unit++) {
		if (mdso_get_unit_ctx(dctx,*unit,puctx++)) {
			mdso_free_uctx_vector(uctxv,0,0,fout);
			return MDSO_NESTED_ERROR(dctx);
		}
	}

	for (nsym=0,puctx=uctxv; *puctx; puctx++)
		for (dsym=puctx[0]->syms; *dsym; dsym++)
			nsym++;

	if (!(symv = calloc(nsym+1,sizeof(const char *)))) {
		mdso_free_uctx_vector(uctxv,0,0,fout);
		return MDSO_SYSTEM_ERROR(dctx);
	}

	if (!(stype = calloc(nsym+1,sizeof(int)))) {
		mdso_free_uctx_vector(uctxv,symv,0,fout);
		return MDSO_SYSTEM_ERROR(dctx);
	}

	for (psym=symv,puctx=uctxv; *puctx; puctx++) {
		for (dsym=puctx[0]->syms; *dsym; dsym++) {
			stype[psym-symv] = puctx[0]->stype[dsym-puctx[0]->syms];
			*psym++ = *dsym;
		}
	}

	ret = mdso_argen_common(dctx,symv,stype,fout,0);
	mdso_free_uctx_vector(uctxv,symv,stype,fout);

	return ret ? MDSO_NESTED_ERROR(dctx) : 0;
}