summaryrefslogtreecommitdiffhomepage
path: root/src/arbits/slbt_archive_mapstrv.c
blob: 10d59f18f6d39f1383892eab7b3540e101b34f8f (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
/*******************************************************************/
/*  slibtool: a strong libtool implementation, written in C        */
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
/*******************************************************************/

#include <stdlib.h>
#include <inttypes.h>
#include <slibtool/slibtool.h>
#include "slibtool_driver_impl.h"
#include "slibtool_errinfo_impl.h"
#include "slibtool_visibility_impl.h"
#include "slibtool_ar_impl.h"

static int slbt_strcmp(const void * a, const void * b)
{
	return strcmp(*(const char **)a,*(const char **)b);
}

static int slbt_coff_strcmp(const void * a, const void * b)
{
	const char *    dot;
	const char *    mark;
	const char *    stra;
	const char *    strb;
	const char **   pstra;
	const char **   pstrb;
	char            strbufa[4096];
	char            strbufb[4096];

	pstra = (const char **)a;
	pstrb = (const char **)b;

	stra  = *pstra;
	strb  = *pstrb;

	if (!strncmp(*pstra,".weak.",6)) {
		stra = strbufa;
		mark = &(*pstra)[6];
		dot  = strchr(mark,'.');

		strncpy(strbufa,mark,dot-mark);
		strbufa[dot-mark] = '\0';
	}

	if (!strncmp(*pstrb,".weak.",6)) {
		strb = strbufb;
		mark = &(*pstrb)[6];
		dot  = strchr(mark,'.');

		strncpy(strbufb,mark,dot-mark);
		strbufb[dot-mark] = '\0';
	}

	return strcmp(stra,strb);
}

slbt_hidden int slbt_update_mapstrv(
	const struct slbt_driver_ctx *  dctx,
	struct slbt_archive_meta_impl * mctx)
{
	bool            fcoff;
	size_t          nsyms;
	const char **   symv;
	const char **   mapstrv;

	fcoff  = slbt_host_objfmt_is_coff(dctx);
	fcoff |= (mctx->ofmtattr & AR_OBJECT_ATTR_COFF);

	for (nsyms=0,symv=mctx->symstrv; *symv; symv++)
		nsyms++;

	if (!(mapstrv = calloc(nsyms+1,sizeof(const char *))))
		return SLBT_SYSTEM_ERROR(dctx,0);

	for (nsyms=0,symv=mctx->symstrv; *symv; symv++)
		mapstrv[nsyms++] = *symv;

	qsort(mapstrv,nsyms,sizeof(const char *),fcoff ? slbt_coff_strcmp : slbt_strcmp);

	mctx->mapstrv = mapstrv;

	return 0;
}