summaryrefslogtreecommitdiffhomepage
path: root/src/logic/mdso_create_implib_sources.c
blob: 4bc8796199eaff6625a1457a699dd74948f10f42 (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
/****************************************************************/
/*  mdso: midipix dso scavenger                                 */
/*  Copyright (C) 2015--2016  Z. Gilboa                         */
/*  Released under GPLv2 and GPLv3; see COPYING.MDSO.           */
/****************************************************************/

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

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

static void mdso_init_asmname(char * buf, const char * fmt, const char * str)
{
	char			hexstr[24];
	long long unsigned int	crc64;

	if (strlen(str) + strlen(fmt) > (PATH_MAX - 1)) {
		crc64 = mdso_crc64_mbstr((const unsigned char *)str,0);
		sprintf(hexstr,"%llx",crc64);
		sprintf(buf,fmt,hexstr);
	} else
		sprintf(buf,fmt,str);
}

mdso_api int  mdso_create_implib_sources(const struct mdso_driver_ctx * dctx)
{
	struct mdso_unit_ctx *	uctx;
	const char **		unit;
	FILE *			fout;
	char			asmname[PATH_MAX];
	const char * const *	sym;
	int			ret;

	mdso_init_asmname(asmname,"__%s_dso_meta.s",dctx->cctx->libname);

	if (!(fout = mdso_create_asm_source(dctx,asmname)))
		return MDSO_NESTED_ERROR(dctx);

	ret = mdso_generate_dsometa(dctx,fout);

	if (fout != stdout)
		fclose(fout);

	if (ret < 0)
		return MDSO_NESTED_ERROR(dctx);

	for (unit=dctx->units; *unit; unit++) {
		if (mdso_get_unit_ctx(dctx,*unit,&uctx))
			return MDSO_NESTED_ERROR(dctx);

		for (sym=uctx->syms; *sym; sym++) {
			mdso_init_asmname(asmname,"__%s_sym_entry.s",*sym);

			if (!(fout = mdso_create_asm_source(dctx,asmname)))
				return MDSO_NESTED_ERROR(dctx);

			ret = mdso_generate_symentry(dctx,*sym,fout);

			if (fout != stdout)
				fclose(fout);

			if (ret < 0)
				return MDSO_NESTED_ERROR(dctx);

			mdso_init_asmname(asmname,"__%s_sym_fn.s",*sym);

			if (!(fout = mdso_create_asm_source(dctx,asmname)))
				return MDSO_NESTED_ERROR(dctx);

			ret = mdso_generate_symfn(dctx,*sym,fout);

			if (fout != stdout)
				fclose(fout);

			if (ret < 0)
				return MDSO_NESTED_ERROR(dctx);
		}

		mdso_free_unit_ctx(uctx);
	}

	return 0;
}