summaryrefslogtreecommitdiff
path: root/src/arbits/slbt_archive_merge.c
blob: e63c6406a5ab78052e4132df10151e6367517665 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*******************************************************************/
/*  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 <time.h>
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <sys/mman.h>
#include <sys/types.h>

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

/* anonymous fun */
#ifndef MAP_ANONYMOUS
#define MAP_ANONYMOUS MAP_ANON
#endif

/* time stamp format specifier */
#define PPRII64 "%"PRIi64

/* file size format specifier */
#define PPRIU64 "%"PRIu64

struct armap_buffer_32 {
	uint32_t        moffset;
	const char *    symname;
	uint32_t        baseidx;
};

struct armap_buffer_64 {
	uint64_t        moffset;
	const char *    symname;
	uint64_t        baseidx;
};

static const char ar_signature[] = AR_SIGNATURE;

static int slbt_create_anonymous_archive_ctx(
	const struct slbt_driver_ctx *	dctx,
	size_t                          size,
	struct slbt_archive_ctx **	pctx)
{
	struct slbt_archive_ctx_impl *	ctx;

	if (!(ctx = calloc(1,sizeof(*ctx))))
		return SLBT_BUFFER_ERROR(dctx);

	ctx->map.map_size = size;
	ctx->map.map_addr = mmap(
		0,size,
		PROT_READ|PROT_WRITE,
		MAP_PRIVATE|MAP_ANONYMOUS,
		-1,0);

	if (ctx->map.map_addr == MAP_FAILED) {
		free(ctx);
		return SLBT_SYSTEM_ERROR(dctx,0);
	}

	ctx->dctx       = dctx;
	ctx->actx.map	= &ctx->map;

	*pctx = &ctx->actx;

	return 0;
}

static off_t slbt_armap_write_be_32(unsigned char * mark, uint32_t val)
{
	mark[0] = val >> 24;
	mark[1] = val >> 16;
	mark[2] = val >> 8;
	mark[3] = val;

	return sizeof(uint32_t);
}

static off_t slbt_armap_write_le_32(unsigned char * mark, uint32_t val)
{
	mark[0] = val;
	mark[1] = val >> 8;
	mark[2] = val >> 16;
	mark[3] = val >> 24;

	return sizeof(uint32_t);
}

static off_t slbt_armap_write_be_64(unsigned char * mark, uint64_t val)
{
	slbt_armap_write_be_32(&mark[0],val >> 32);
	slbt_armap_write_be_32(&mark[4],val);

	return sizeof(uint64_t);
}

static off_t slbt_armap_write_le_64(unsigned char * mark, uint64_t val)
{
	slbt_armap_write_be_32(&mark[0],val);
	slbt_armap_write_be_32(&mark[4],val >> 32);

	return sizeof(uint64_t);
}


static int slbt_merge_archives_fail(
	struct slbt_archive_ctx *   arctx,
	struct armap_buffer_32 *    bsdmap32,
	struct armap_buffer_64 *    bsdmap64,
	int                         ret)
{
	if (bsdmap32)
		free(bsdmap32);

	if (bsdmap64)
		free(bsdmap64);

	if (arctx)
		slbt_free_archive_ctx(arctx);

	return ret;
}


int slbt_merge_archives(
	struct slbt_archive_ctx * const arctxv[],
	struct slbt_archive_ctx **      arctxm)
{
	struct slbt_archive_ctx * const *       arctxp;
	struct slbt_archive_ctx *               arctx;

	const struct slbt_driver_ctx *          dctx;
	const struct slbt_archive_meta *        meta;

	struct ar_raw_file_header *             arhdr;
	struct ar_meta_member_info **           memberp;
	struct ar_meta_member_info *            meminfo;

	struct ar_meta_member_info *            armap;
	struct ar_meta_member_info *            arnames;

	const struct ar_meta_armap_common_32 *  armap32;
	const struct ar_meta_armap_common_64 *  armap64;
	const struct ar_meta_armap_ref_32 *     symref32;
	const struct ar_meta_armap_ref_64 *     symref64;

	struct armap_buffer_32 *                bsdmap32;
	struct armap_buffer_64 *                bsdmap64;
	struct armap_buffer_32 *                bsdsort32;
	struct armap_buffer_64 *                bsdsort64;

	size_t                                  nbytes;
	ssize_t                                 nwritten;

	uint32_t                                mapattr;
	uint64_t                                nmembers;
	uint64_t                                nsymrefs;

	uint64_t                                sarmap;
	uint64_t                                sarname;
	uint64_t                                sarchive;
	uint64_t                                smembers;
	uint64_t                                ssymrefs;
	uint64_t                                ssymstrs;
	uint64_t                                snamestrs;

	int64_t                                 omembers;
	int64_t                                 osymrefs;
	int64_t                                 onamestrs;
	int64_t                                 omemfixup;

	char *                                  base;
	unsigned char *                         ubase;

	char *                                  ch;
	unsigned char *                         uch;

	char *                                  namebase;
	char *                                  namestr;
	char *                                  strtbl;

	uint64_t                                idx;
	uint64_t                                mapidx;
	uint64_t                                cmpidx;

	off_t (*armap_write_uint32)(
		unsigned char *,
		uint32_t);

	off_t (*armap_write_uint64)(
		unsigned char *,
		uint64_t);

	/* init */
	nmembers = nsymrefs = ssymstrs = mapattr   = 0;
	sarchive = smembers = ssymrefs = snamestrs = 0;
	omembers = osymrefs = onamestrs = 0;

	if (!arctxv || !arctxv[0])
		return -1;

	if (!(dctx = slbt_get_archive_ictx(arctxv[0])->dctx))
		return -1;

	/* determine armap type and size of archive elements */
	for (armap=0,arnames=0,arctxp=arctxv; *arctxp; arctxp++) {
		if (slbt_get_archive_ictx(*arctxp)->dctx != dctx)
			return SLBT_CUSTOM_ERROR(
				dctx,
				SLBT_ERR_AR_DRIVER_MISMATCH);

		meta    = (*arctxp)->meta;
		armap32 = meta->a_armap_primary.ar_armap_common_32;
		armap64 = meta->a_armap_primary.ar_armap_common_64;

		for (memberp=meta->a_memberv; memberp && *memberp; memberp++) {
			meminfo = *memberp;

			switch (meminfo->ar_member_attr) {
				case AR_MEMBER_ATTR_ARMAP:
					if (armap32) {
						if (mapattr == 0) {
							armap   = meminfo;
							mapattr = armap32->ar_armap_attr;
						} else if (mapattr != armap32->ar_armap_attr) {
							return SLBT_CUSTOM_ERROR(
								dctx,
								SLBT_ERR_AR_ARMAP_MISMATCH);
						}

						nsymrefs += armap32->ar_num_of_symbols;
						ssymstrs += armap32->ar_size_of_strs;

					} else if (armap64) {
						if (mapattr == 0) {
							armap   = meminfo;
							mapattr = armap64->ar_armap_attr;
						} else if (mapattr != armap64->ar_armap_attr) {
							return SLBT_CUSTOM_ERROR(
								dctx,
								SLBT_ERR_AR_ARMAP_MISMATCH);
						}

						nsymrefs += armap64->ar_num_of_symbols;
					}

					break;

				case AR_MEMBER_ATTR_LINKINFO:
					break;

				case AR_MEMBER_ATTR_NAMESTRS:
					snamestrs += meminfo->ar_object_size;

					if (!arnames)
						arnames = meminfo;

					break;

				default:
					smembers += sizeof(struct ar_raw_file_header);
					smembers += meminfo->ar_file_header.ar_file_size;
					smembers += 1;
					smembers |= 1;
					smembers ^= 1;
					nmembers++;

					break;
			}
		}
	}

	/* armap size */
	if (sarmap = 0, sarname = 0, (mapattr == 0)) {
		(void)0;

	} else if (mapattr & AR_ARMAP_ATTR_SYSV) {
		if (mapattr & (AR_ARMAP_ATTR_LE_32|AR_ARMAP_ATTR_BE_32)) {
			sarmap += sizeof(uint32_t);
			sarmap += sizeof(uint32_t) * nsymrefs;
		} else {
			sarmap += sizeof(uint64_t);
			sarmap += sizeof(uint64_t) * nsymrefs;
		}

	} else if (mapattr & AR_ARMAP_ATTR_BSD) {
		if (mapattr & (AR_ARMAP_ATTR_LE_32|AR_ARMAP_ATTR_BE_32)) {
			sarmap += 2 * sizeof(uint32_t);
			sarmap += 2 * sizeof(uint32_t) * nsymrefs;
		} else {
			sarmap += 2 * sizeof(uint64_t);
			sarmap += 2 * sizeof(uint64_t) * nsymrefs;
		}

		sarname += armap->ar_file_header.ar_file_size;
		sarname -= armap->ar_object_size;
	} else {
		return SLBT_CUSTOM_ERROR(dctx,SLBT_ERR_FLOW_ERROR);
	}

	/* long-names member alignment */
	snamestrs += 1;
	snamestrs |= 1;
	snamestrs ^= 1;

	/* archive size */
	sarchive  = sizeof(struct ar_raw_signature);
	sarchive += armap   ? sizeof(struct ar_raw_file_header) : 0;
	sarchive += arnames ? sizeof(struct ar_raw_file_header) : 0;
	sarchive += sarname;
	sarchive += sarmap;
	sarchive += ssymstrs;
	sarchive += snamestrs;
	sarchive += smembers;

	/* offset from archive base to first public member */
	omembers  = sarchive;
	omembers -= smembers;


	/* create in-memory archive */
	if (slbt_create_anonymous_archive_ctx(dctx,sarchive,&arctx) < 0)
		return SLBT_NESTED_ERROR(dctx);


	/* get ready for writing */
	base  = arctx->map->map_addr;
	ubase = arctx->map->map_addr;


	/* archive header */
	ch = base;
	memcpy(ch,ar_signature,sizeof(struct ar_raw_signature));
	ch += sizeof(struct ar_raw_signature);


	/* armap header */
	if (armap) {
		arhdr = (struct ar_raw_file_header *)ch;
		memcpy(arhdr,armap->ar_member_data,sizeof(*arhdr)+sarname);

		nwritten = armap->ar_file_header.ar_time_date_stamp
				? sprintf(arhdr->ar_time_date_stamp,PPRII64,time(0))
				: 0;

		if (nwritten < 0)
			return slbt_merge_archives_fail(
				arctx,0,0,
				SLBT_SYSTEM_ERROR(dctx,0));

		for (nbytes=nwritten; nbytes < sizeof(arhdr->ar_time_date_stamp); nbytes++)
			arhdr->ar_time_date_stamp[nbytes] = AR_DEC_PADDING;

		nwritten = sprintf(
			arhdr->ar_file_size,PPRIU64,
			sarname + sarmap + ssymstrs);

		if (nwritten < 0)
			return slbt_merge_archives_fail(
				arctx,0,0,
				SLBT_SYSTEM_ERROR(dctx,0));

		for (nbytes=nwritten; nbytes < sizeof(arhdr->ar_file_size); nbytes++)
			arhdr->ar_file_size[nbytes] = AR_DEC_PADDING;
	}


	/* arnames header (sysv only) */
	if (arnames) {
		ch  = base;
		ch += omembers;
		ch -= snamestrs;
		ch -= sizeof(struct ar_raw_file_header);

		namebase  = ch;
		namebase += sizeof(struct ar_raw_file_header);

		memset(namebase,0,snamestrs);
		namestr = namebase;

		arhdr = (struct ar_raw_file_header *)ch;
		memcpy(arhdr,arnames->ar_member_data,sizeof(*arhdr));

		nwritten = arnames->ar_file_header.ar_time_date_stamp
				? sprintf(arhdr->ar_time_date_stamp,PPRII64,time(0))
				: 0;

		if (nwritten < 0)
			return slbt_merge_archives_fail(
				arctx,0,0,
				SLBT_SYSTEM_ERROR(dctx,0));

		for (nbytes=nwritten; nbytes < sizeof(arhdr->ar_time_date_stamp); nbytes++)
			arhdr->ar_time_date_stamp[nbytes] = AR_DEC_PADDING;

		nwritten = sprintf(
			arhdr->ar_file_size,PPRIU64,
			snamestrs);

		if (nwritten < 0)
			return slbt_merge_archives_fail(
				arctx,0,0,
				SLBT_SYSTEM_ERROR(dctx,0));

		for (nbytes=nwritten; nbytes < sizeof(arhdr->ar_file_size); nbytes++)
			arhdr->ar_file_size[nbytes] = AR_DEC_PADDING;
	}


	/* armap data (preparation) */
	armap_write_uint32 = 0;
	armap_write_uint64 = 0;

	bsdmap32 = 0;
	bsdmap64 = 0;

	if (mapattr & AR_ARMAP_ATTR_BE_32)
		armap_write_uint32 = slbt_armap_write_be_32;

	else if (mapattr & AR_ARMAP_ATTR_LE_32)
		armap_write_uint32 = slbt_armap_write_le_32;

	else if (mapattr & AR_ARMAP_ATTR_BE_64)
		armap_write_uint64 = slbt_armap_write_be_64;

	else if (mapattr & AR_ARMAP_ATTR_LE_64)
		armap_write_uint64 = slbt_armap_write_le_64;

	uch  = ubase;
	uch += sizeof(struct ar_raw_signature);
	uch += sizeof(struct ar_raw_file_header);
	uch += sarname;

	if (mapattr & AR_ARMAP_ATTR_SYSV) {
		if (armap_write_uint32) {
			uch += armap_write_uint32(uch,nsymrefs);

			ch  = base;
			ch += uch - ubase;
			ch += sizeof(uint32_t) * nsymrefs;
		} else {
			uch += armap_write_uint64(uch,nsymrefs);

			ch  = base;
			ch += uch - ubase;
			ch += sizeof(uint64_t) * nsymrefs;
		}

	} else if (mapattr & AR_ARMAP_ATTR_BSD) {
		strtbl  = base;
		strtbl += omembers;
		strtbl -= ssymstrs;

		memset(strtbl,0,ssymstrs);

		if (armap_write_uint32) {
			if (!(bsdmap32 = calloc(2*nsymrefs,sizeof(struct armap_buffer_32))))
				return slbt_merge_archives_fail(
					arctx,0,0,
					SLBT_SYSTEM_ERROR(dctx,0));

			bsdsort32 = &bsdmap32[nsymrefs];

		} else {
			if (!(bsdmap64 = calloc(2*nsymrefs,sizeof(struct armap_buffer_64))))
				return slbt_merge_archives_fail(
					arctx,0,0,
					SLBT_SYSTEM_ERROR(dctx,0));

			bsdsort64 = &bsdmap64[nsymrefs];
		}
	}

	/* main iteration (armap data, long-names, public members) */
	for (mapidx=0,arctxp=arctxv; *arctxp; arctxp++) {
		meta    = (*arctxp)->meta;
		armap32 = meta->a_armap_primary.ar_armap_common_32;
		armap64 = meta->a_armap_primary.ar_armap_common_64;

		if ((memberp = meta->a_memberv)) {
			for (omemfixup=0; *memberp && !omemfixup; memberp++) {
				meminfo = *memberp;

				switch (meminfo->ar_member_attr) {
					case AR_MEMBER_ATTR_ARMAP:
					case AR_MEMBER_ATTR_LINKINFO:
					case AR_MEMBER_ATTR_NAMESTRS:
						break;

					default:
						omemfixup  = (int64_t)meminfo->ar_member_data;
						omemfixup -= (int64_t)meta->r_archive.map_addr;
						break;
				}
			}
		}

		for (memberp=meta->a_memberv; memberp && *memberp; memberp++) {
			meminfo = *memberp;

			switch (meminfo->ar_member_attr) {
				case AR_MEMBER_ATTR_ARMAP:
					if (armap32 && (mapattr & AR_ARMAP_ATTR_SYSV)) {
						symref32 = armap32->ar_symrefs;

						for (idx=0; idx<armap32->ar_num_of_symbols; idx++) {
							uch += armap_write_uint32(
								uch,
								symref32[idx].ar_member_offset + omembers - omemfixup);

							strcpy(ch,&armap32->ar_string_table[symref32[idx].ar_name_offset]);
							ch += strlen(ch);
							ch++;
						}

					} else if (armap64 && (mapattr & AR_ARMAP_ATTR_SYSV)) {
						symref64 = armap64->ar_symrefs;

						for (idx=0; idx<armap64->ar_num_of_symbols; idx++) {
							uch += armap_write_uint64(
								uch,
								symref64[idx].ar_member_offset + omembers - omemfixup);

							strcpy(ch,&armap64->ar_string_table[symref64[idx].ar_name_offset]);
							ch += strlen(ch);
							ch++;
						}

					} else if (armap32 && (mapattr & AR_ARMAP_ATTR_BSD)) {
						symref32 = armap32->ar_symrefs;

						for (idx=0; idx<armap32->ar_num_of_symbols; idx++) {
							bsdmap32[mapidx].moffset  = symref32[idx].ar_member_offset;
							bsdmap32[mapidx].moffset += omembers - omemfixup;

							bsdmap32[mapidx].symname  = armap32->ar_string_table;
							bsdmap32[mapidx].symname += symref32[idx].ar_name_offset;

							mapidx++;
						}

					} else if (armap64 && (mapattr & AR_ARMAP_ATTR_BSD)) {
						symref64 = armap64->ar_symrefs;

						for (idx=0; idx<armap64->ar_num_of_symbols; idx++) {
							bsdmap64[mapidx].moffset  = symref64[idx].ar_member_offset;
							bsdmap64[mapidx].moffset += omembers - omemfixup;

							bsdmap64[mapidx].symname  = armap64->ar_string_table;
							bsdmap64[mapidx].symname += symref64[idx].ar_name_offset;

							mapidx++;
						}
					}

					break;

				case AR_MEMBER_ATTR_LINKINFO:
					break;

				case AR_MEMBER_ATTR_NAMESTRS:
					break;

				default:
					arhdr = meminfo->ar_member_data;

					memcpy(
						&base[omembers],arhdr,
						sizeof(*arhdr) + meminfo->ar_file_header.ar_file_size);

					if (meminfo->ar_file_header.ar_header_attr & AR_HEADER_ATTR_SYSV) {
						if (meminfo->ar_file_header.ar_header_attr & AR_HEADER_ATTR_NAME_REF) {
							nwritten = sprintf(
								&base[omembers],"/"PPRII64,
								namestr - namebase);

							if (nwritten < 0)
								SLBT_SYSTEM_ERROR(dctx,0);

							for (nbytes=nwritten; nbytes < sizeof(arhdr->ar_file_id); nbytes++)
								base[omembers + nbytes] = AR_DEC_PADDING;

							strcpy(namestr,meminfo->ar_file_header.ar_member_name);
							namestr += strlen(namestr);
							*namestr++ = '/';
							*namestr++ = AR_OBJ_PADDING;
						}
					}

					omembers += sizeof(*arhdr);
					omembers += meminfo->ar_file_header.ar_file_size;
					omembers += 1;
					omembers |= 1;
					omembers ^= 1;
					break;
			}
		}
	}

	/* bsd variant: also sort the string table (because we can:=)) */
	if (bsdmap32) {
		for (mapidx=0; mapidx<nsymrefs; mapidx++)
			for (cmpidx=0; cmpidx<nsymrefs; cmpidx++)
				if (strcmp(bsdmap32[cmpidx].symname,bsdmap32[mapidx].symname) < 0)
					bsdmap32[mapidx].baseidx++;

		/* a symbol might be present in more than one member; */
		/* see whether ar_member_offset has already been set) */
		for (mapidx=0,ch=strtbl; mapidx<nsymrefs; mapidx++) {
			idx = bsdmap32[mapidx].baseidx;

			for (; bsdsort32[idx].moffset; )
				idx++;

			bsdsort32[idx].moffset = bsdmap32[mapidx].moffset;
			bsdsort32[idx].symname = bsdmap32[mapidx].symname;
		}

		uch += armap_write_uint32(uch,2*sizeof(uint32_t)*nsymrefs);

		for (mapidx=0,ch=strtbl; mapidx<nsymrefs; mapidx++) {
			uch += armap_write_uint32(uch,ch-strtbl);
			uch += armap_write_uint32(uch,bsdsort32[mapidx].moffset);

			strcpy(ch,bsdsort32[mapidx].symname);
			ch += strlen(ch);
			ch++;
		}

		uch +=armap_write_uint32(uch,ssymstrs);

		free(bsdmap32);

	} else if (bsdmap64) {
		for (mapidx=0; mapidx<nsymrefs; mapidx++)
			for (cmpidx=0; cmpidx<nsymrefs; cmpidx++)
				if (strcmp(bsdmap64[cmpidx].symname,bsdmap64[mapidx].symname) < 0)
					bsdmap64[mapidx].baseidx++;

		/* a symbol might be present in more than one member; */
		/* see whether ar_member_offset has already been set) */
		for (mapidx=0,ch=strtbl; mapidx<nsymrefs; mapidx++) {
			idx = bsdmap64[mapidx].baseidx;

			for (; bsdsort64[idx].moffset; )
				idx++;

			bsdsort64[idx].moffset = bsdmap64[mapidx].moffset;
			bsdsort64[idx].symname = bsdmap64[mapidx].symname;
		}

		uch += armap_write_uint64(uch,2*sizeof(uint64_t)*nsymrefs);

		for (mapidx=0,ch=strtbl; mapidx<nsymrefs; mapidx++) {
			uch += armap_write_uint64(uch,ch-strtbl);
			uch += armap_write_uint64(uch,bsdsort64[mapidx].moffset);

			strcpy(ch,bsdsort64[mapidx].symname);
			ch += strlen(ch);
			ch++;
		}

		uch +=armap_write_uint64(uch,ssymstrs);

		free(bsdmap64);
	}

	struct slbt_archive_ctx_impl * ictx;
	ictx = slbt_get_archive_ictx(arctx);

	if (slbt_get_archive_meta(dctx,arctx->map,&ictx->meta) < 0)
		slbt_merge_archives_fail(
			arctx,0,0,
			SLBT_NESTED_ERROR(dctx));

	ictx->actx.meta = ictx->meta;

	*arctxm = arctx;

	return 0;
}