summaryrefslogtreecommitdiffhomepage
path: root/src/arbits/slbt_archive_syminfo.c
blob: 879278f7119499fdc7cf3b8c98cbe6e0a336e212 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*******************************************************************/
/*  slibtool: a skinny libtool implementation, written in C        */
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
/*******************************************************************/

#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <sys/wait.h>

#include <slibtool/slibtool.h>
#include "slibtool_ar_impl.h"
#include "slibtool_driver_impl.h"
#include "slibtool_dprintf_impl.h"
#include "slibtool_snprintf_impl.h"
#include "slibtool_errinfo_impl.h"

static const char ar_symbol_type_A[] = "A";
static const char ar_symbol_type_B[] = "B";
static const char ar_symbol_type_C[] = "C";
static const char ar_symbol_type_D[] = "D";
static const char ar_symbol_type_G[] = "G";
static const char ar_symbol_type_I[] = "I";
static const char ar_symbol_type_R[] = "R";
static const char ar_symbol_type_S[] = "S";
static const char ar_symbol_type_T[] = "T";
static const char ar_symbol_type_W[] = "W";

static const char * const ar_symbol_type['Z'-'A'] = {
	['A'-'A'] = ar_symbol_type_A,
	['B'-'A'] = ar_symbol_type_B,
	['C'-'A'] = ar_symbol_type_C,
	['D'-'A'] = ar_symbol_type_D,
	['G'-'A'] = ar_symbol_type_G,
	['I'-'A'] = ar_symbol_type_I,
	['R'-'A'] = ar_symbol_type_R,
	['S'-'A'] = ar_symbol_type_S,
	['T'-'A'] = ar_symbol_type_T,
	['W'-'A'] = ar_symbol_type_W,
};

static void slbt_ar_update_syminfo_child(
	char *	program,
	char *  arname,
	int	fdout)
{
	char *	argv[6];

	argv[0] = program;
	argv[1] = "-P";
	argv[2] = "-A";
	argv[3] = "-g";
	argv[4] = arname;
	argv[5] = 0;

	if (dup2(fdout,1) == 1)
		execvp(program,argv);

	_exit(EXIT_FAILURE);
}

static int slbt_obtain_nminfo(
	struct slbt_archive_ctx_impl *  ictx,
	struct slbt_exec_ctx *          ectx,
	const struct slbt_driver_ctx *  dctx,
	struct slbt_archive_meta_impl * mctx)
{
	int     pos;
	int	fdcwd;
	pid_t   pid;
	pid_t   rpid;
	int     fdout;
	char ** argv;
	char    arname [PATH_MAX];
	char    output [PATH_MAX];
	char	program[PATH_MAX];

	/* fdcwd */
	fdcwd = slbt_driver_fdcwd(dctx);

	/* tool-specific argument vector */
	argv = (slbt_get_driver_ictx(dctx))->host.nm_argv;

	/* ar alternate argument vector */
	if (argv) {
		if (slbt_snprintf(program,sizeof(program),
				"%s",argv[0]) < 0)
			return SLBT_BUFFER_ERROR(dctx);
	} else {
		if (slbt_snprintf(program,sizeof(program),
				"%s",dctx->cctx->host.nm) < 0)
			return SLBT_BUFFER_ERROR(dctx);
	}

	/* output (.nm suffix, buf treat as .syminfo) */
	pos = slbt_snprintf(
		arname,sizeof(arname)-8,"%s",
		ictx->path);

	strcpy(output,arname);
	strcpy(&output[pos],".nm");

	/* fork */
	fdout = openat(fdcwd,output,O_CREAT|O_TRUNC|O_WRONLY,0644);

	if ((pid = fork()) < 0) {
		close(fdout);
		return SLBT_SYSTEM_ERROR(dctx,0);
	}

	/* child */
	if (pid == 0)
		slbt_ar_update_syminfo_child(
			program,arname,fdout);

	/* parent */
	ectx->pid = pid;

	rpid = waitpid(
		pid,
		&ectx->exitcode,
		0);

	if (rpid < 0) {
		return SLBT_SYSTEM_ERROR(dctx,0);

	} else if (ectx->exitcode) {
		return SLBT_CUSTOM_ERROR(
			dctx,
			SLBT_ERR_FLOW_ERROR);
	}

	if (slbt_lib_get_txtfile_ctx(
			dctx,output,
			&mctx->nminfo) < 0)
		return SLBT_NESTED_ERROR(dctx);

	return 0;
}

static int slbt_get_symbol_nm_info(
	struct slbt_archive_ctx *       actx,
	struct slbt_archive_meta_impl * mctx,
	uint64_t                        idx)
{
	int                             cint;
	const char **                   pline;
	const char *                    mark;
	const char *                    cap;
	const char *                    objname;
	const char *                    symname;
	struct ar_meta_symbol_info *    syminfo;
	struct ar_meta_member_info **   pmember;

	symname = mctx->symstrv[idx];
	syminfo = &mctx->syminfo[idx];

	for (pline=mctx->nminfo->txtlinev; *pline; pline++) {
		mark = *pline;

		if (!(mark = strchr(mark,']')))
			return -1;

		if ((*++mark != ':') || (*++mark != ' '))
			return -1;

		cap = ++mark;

		for (; *cap && !isspace((cint = *cap)); )
			cap++;

		if (*cap != ' ')
			return -1;

		if (!(strncmp(symname,mark,cap-mark))) {
			mark = ++cap;

			/* space only according to posix, but ... */
			if (mark[1] && (mark[1] != ' '))
				return -1;

			switch (mark[0]) {
				case 'A':
				case 'B':
				case 'C':
				case 'D':
				case 'G':
				case 'I':
				case 'R':
				case 'S':
				case 'T':
				case 'W':
					syminfo->ar_symbol_type = ar_symbol_type[mark[0]-'A'];
					break;

				default:
					break;
			}

			if (syminfo->ar_symbol_type) {
				syminfo->ar_archive_name = *actx->path;
				syminfo->ar_symbol_name  = symname;

				if (!(mark = strchr(*pline,'[')))
					return -1;

				if (!(cap = strchr(++mark,']')))
					return -1;
			}

			pmember = mctx->memberv;

			for (; *pmember && !syminfo->ar_object_name; ) {
				objname = (*pmember)->ar_file_header.ar_member_name;

				if (!(strncmp(objname,mark,cap-mark)))
					syminfo->ar_object_name = objname;

				pmember++;
			}

			mctx->syminfv[idx] = syminfo;
		}
	}

	return (mctx->syminfv[idx] ? 0 : (-1));
}

slbt_hidden int slbt_ar_update_syminfo(
	struct slbt_archive_ctx * actx,
	struct slbt_exec_ctx *    ectx)
{
	const struct slbt_driver_ctx *  dctx;
	struct slbt_archive_ctx_impl *  ictx;
	struct slbt_archive_meta_impl * mctx;
	uint64_t                        idx;

	/* driver context, etc. */
	ictx = slbt_get_archive_ictx(actx);
	mctx = slbt_archive_meta_ictx(ictx->meta);
	dctx = ictx->dctx;

	/* nm -P -A -g */
	if (slbt_obtain_nminfo(ictx,ectx,dctx,mctx) < 0)
		return SLBT_NESTED_ERROR(dctx);

	/* free old syminfo vector */
	if (mctx->syminfv)
		free(mctx->syminfv);

	/* syminfo vector: armap symbols only */
	if (!(mctx->syminfo = calloc(
			mctx->armaps.armap_nsyms + 1,
			sizeof(*mctx->syminfo))))
		return SLBT_SYSTEM_ERROR(dctx,0);

	if (!(mctx->syminfv = calloc(
			mctx->armaps.armap_nsyms + 1,
			sizeof(*mctx->syminfv))))
		return SLBT_SYSTEM_ERROR(dctx,0);

	/* do the thing */
	for (idx=0; idx<mctx->armaps.armap_nsyms; idx++)
		if (slbt_get_symbol_nm_info(actx,mctx,idx) < 0)
			return SLBT_CUSTOM_ERROR(
				dctx,
				SLBT_ERR_FLOW_ERROR);
	/* yay */
	return 0;
}