1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Given several files containing CTF data, merge and uniquify that data into
30  * a single CTF section in an output file.
31  *
32  * Merges can proceed independently.  As such, we perform the merges in parallel
33  * using a worker thread model.  A given glob of CTF data (either all of the CTF
34  * data from a single input file, or the result of one or more merges) can only
35  * be involved in a single merge at any given time, so the process decreases in
36  * parallelism, especially towards the end, as more and more files are
37  * consolidated, finally resulting in a single merge of two large CTF graphs.
38  * Unfortunately, the last merge is also the slowest, as the two graphs being
39  * merged are each the product of merges of half of the input files.
40  *
41  * The algorithm consists of two phases, described in detail below.  The first
42  * phase entails the merging of CTF data in groups of eight.  The second phase
43  * takes the results of Phase I, and merges them two at a time.  This disparity
44  * is due to an observation that the merge time increases at least quadratically
45  * with the size of the CTF data being merged.  As such, merges of CTF graphs
46  * newly read from input files are much faster than merges of CTF graphs that
47  * are themselves the results of prior merges.
48  *
49  * A further complication is the need to ensure the repeatability of CTF merges.
50  * That is, a merge should produce the same output every time, given the same
51  * input.  In both phases, this consistency requirement is met by imposing an
52  * ordering on the merge process, thus ensuring that a given set of input files
53  * are merged in the same order every time.
54  *
55  *   Phase I
56  *
57  *   The main thread reads the input files one by one, transforming the CTF
58  *   data they contain into tdata structures.  When a given file has been read
59  *   and parsed, it is placed on the work queue for retrieval by worker threads.
60  *
61  *   Central to Phase I is the Work In Progress (wip) array, which is used to
62  *   merge batches of files in a predictable order.  Files are read by the main
63  *   thread, and are merged into wip array elements in round-robin order.  When
64  *   the number of files merged into a given array slot equals the batch size,
65  *   the merged CTF graph in that array is added to the done slot in order by
66  *   array slot.
67  *
68  *   For example, consider a case where we have five input files, a batch size
69  *   of two, a wip array size of two, and two worker threads (T1 and T2).
70  *
71  *    1. The wip array elements are assigned initial batch numbers 0 and 1.
72  *    2. T1 reads an input file from the input queue (wq_queue).  This is the
73  *       first input file, so it is placed into wip[0].  The second file is
74  *       similarly read and placed into wip[1].  The wip array slots now contain
75  *       one file each (wip_nmerged == 1).
76  *    3. T1 reads the third input file, which it merges into wip[0].  The
77  *       number of files in wip[0] is equal to the batch size.
78  *    4. T2 reads the fourth input file, which it merges into wip[1].  wip[1]
79  *       is now full too.
80  *    5. T2 attempts to place the contents of wip[1] on the done queue
81  *       (wq_done_queue), but it can't, since the batch ID for wip[1] is 1.
82  *       Batch 0 needs to be on the done queue before batch 1 can be added, so
83  *       T2 blocks on wip[1]'s cv.
84  *    6. T1 attempts to place the contents of wip[0] on the done queue, and
85  *       succeeds, updating wq_lastdonebatch to 0.  It clears wip[0], and sets
86  *       its batch ID to 2.  T1 then signals wip[1]'s cv to awaken T2.
87  *    7. T2 wakes up, notices that wq_lastdonebatch is 0, which means that
88  *       batch 1 can now be added.  It adds wip[1] to the done queue, clears
89  *       wip[1], and sets its batch ID to 3.  It signals wip[0]'s cv, and
90  *       restarts.
91  *
92  *   The above process continues until all input files have been consumed.  At
93  *   this point, a pair of barriers are used to allow a single thread to move
94  *   any partial batches from the wip array to the done array in batch ID order.
95  *   When this is complete, wq_done_queue is moved to wq_queue, and Phase II
96  *   begins.
97  *
98  *	Locking Semantics (Phase I)
99  *
100  *	The input queue (wq_queue) and the done queue (wq_done_queue) are
101  *	protected by separate mutexes - wq_queue_lock and wq_done_queue.  wip
102  *	array slots are protected by their own mutexes, which must be grabbed
103  *	before releasing the input queue lock.  The wip array lock is dropped
104  *	when the thread restarts the loop.  If the array slot was full, the
105  *	array lock will be held while the slot contents are added to the done
106  *	queue.  The done queue lock is used to protect the wip slot cv's.
107  *
108  *	The pow number is protected by the queue lock.  The master batch ID
109  *	and last completed batch (wq_lastdonebatch) counters are protected *in
110  *	Phase I* by the done queue lock.
111  *
112  *   Phase II
113  *
114  *   When Phase II begins, the queue consists of the merged batches from the
115  *   first phase.  Assume we have five batches:
116  *
117  *	Q:	a b c d e
118  *
119  *   Using the same batch ID mechanism we used in Phase I, but without the wip
120  *   array, worker threads remove two entries at a time from the beginning of
121  *   the queue.  These two entries are merged, and are added back to the tail
122  *   of the queue, as follows:
123  *
124  *	Q:	a b c d e	# start
125  *	Q:	c d e ab	# a, b removed, merged, added to end
126  *	Q:	e ab cd		# c, d removed, merged, added to end
127  *	Q:	cd eab		# e, ab removed, merged, added to end
128  *	Q:	cdeab		# cd, eab removed, merged, added to end
129  *
130  *   When one entry remains on the queue, with no merges outstanding, Phase II
131  *   finishes.  We pre-determine the stopping point by pre-calculating the
132  *   number of nodes that will appear on the list.  In the example above, the
133  *   number (wq_ninqueue) is 9.  When ninqueue is 1, we conclude Phase II by
134  *   signaling the main thread via wq_done_cv.
135  *
136  *	Locking Semantics (Phase II)
137  *
138  *	The queue (wq_queue), ninqueue, and the master batch ID and last
139  *	completed batch counters are protected by wq_queue_lock.  The done
140  *	queue and corresponding lock are unused in Phase II as is the wip array.
141  *
142  *   Uniquification
143  *
144  *   We want the CTF data that goes into a given module to be as small as
145  *   possible.  For example, we don't want it to contain any type data that may
146  *   be present in another common module.  As such, after creating the master
147  *   tdata_t for a given module, we can, if requested by the user, uniquify it
148  *   against the tdata_t from another module (genunix in the case of the SunOS
149  *   kernel).  We perform a merge between the tdata_t for this module and the
150  *   tdata_t from genunix.  Nodes found in this module that are not present in
151  *   genunix are added to a third tdata_t - the uniquified tdata_t.
152  *
153  *   Additive Merges
154  *
155  *   In some cases, for example if we are issuing a new version of a common
156  *   module in a patch, we need to make sure that the CTF data already present
157  *   in that module does not change.  Changes to this data would void the CTF
158  *   data in any module that uniquified against the common module.  To preserve
159  *   the existing data, we can perform what is known as an additive merge.  In
160  *   this case, a final uniquification is performed against the CTF data in the
161  *   previous version of the module.  The result will be the placement of new
162  *   and changed data after the existing data, thus preserving the existing type
163  *   ID space.
164  *
165  *   Saving the result
166  *
167  *   When the merges are complete, the resulting tdata_t is placed into the
168  *   output file, replacing the .SUNW_ctf section (if any) already in that file.
169  *
170  * The person who changes the merging thread code in this file without updating
171  * this comment will not live to see the stock hit five.
172  */
173 
174 #if HAVE_NBTOOL_CONFIG_H
175 # include "nbtool_config.h"
176 #endif
177 
178 #include <stdio.h>
179 #include <stdlib.h>
180 #ifndef _NETBSD_SOURCE
181 #define _NETBSD_SOURCE	/* XXX TBD fix this */
182 #include <unistd.h>
183 #undef _NETBSD_SOURCE
184 #else
185 #include <unistd.h>
186 #endif
187 #include <pthread.h>
188 #include <assert.h>
189 #if defined(sun)
190 #include <synch.h>
191 #endif
192 #include <signal.h>
193 #include <libgen.h>
194 #include <string.h>
195 #include <errno.h>
196 #if defined(sun)
197 #include <alloca.h>
198 #endif
199 #include <sys/param.h>
200 #include <sys/types.h>
201 #include <sys/mman.h>
202 #if defined(sun)
203 #include <sys/sysconf.h>
204 #endif
205 
206 #include "ctf_headers.h"
207 #include "ctftools.h"
208 #include "ctfmerge.h"
209 #include "traverse.h"
210 #include "memory.h"
211 #include "fifo.h"
212 #include "barrier.h"
213 
214 #pragma init(bigheap)
215 
216 #define	MERGE_PHASE1_BATCH_SIZE		8
217 #define	MERGE_PHASE1_MAX_SLOTS		5
218 #define	MERGE_INPUT_THROTTLE_LEN	10
219 
220 const char *progname;
221 static char *outfile = NULL;
222 static char *tmpname = NULL;
223 static int dynsym;
224 int debug_level = DEBUG_LEVEL;
225 static size_t maxpgsize = 0x400000;
226 
227 
228 void
229 usage(void)
230 {
231 	(void) fprintf(stderr,
232 	    "Usage: %s [-fgstv] -l label | -L labelenv -o outfile file ...\n"
233 	    "       %s [-fgstv] -l label | -L labelenv -o outfile -d uniqfile\n"
234 	    "       %*s [-g] [-D uniqlabel] file ...\n"
235 	    "       %s [-fgstv] -l label | -L labelenv -o outfile -w withfile "
236 	    "file ...\n"
237 	    "       %s [-g] -c srcfile destfile\n"
238 	    "\n"
239 	    "  Note: if -L labelenv is specified and labelenv is not set in\n"
240 	    "  the environment, a default value is used.\n",
241 	    progname, progname, strlen(progname), " ",
242 	    progname, progname);
243 }
244 
245 #if defined(sun)
246 static void
247 bigheap(void)
248 {
249 	size_t big, *size;
250 	int sizes;
251 	struct memcntl_mha mha;
252 
253 	/*
254 	 * First, get the available pagesizes.
255 	 */
256 	if ((sizes = getpagesizes(NULL, 0)) == -1)
257 		return;
258 
259 	if (sizes == 1 || (size = alloca(sizeof (size_t) * sizes)) == NULL)
260 		return;
261 
262 	if (getpagesizes(size, sizes) == -1)
263 		return;
264 
265 	while (size[sizes - 1] > maxpgsize)
266 		sizes--;
267 
268 	/* set big to the largest allowed page size */
269 	big = size[sizes - 1];
270 	if (big & (big - 1)) {
271 		/*
272 		 * The largest page size is not a power of two for some
273 		 * inexplicable reason; return.
274 		 */
275 		return;
276 	}
277 
278 	/*
279 	 * Now, align our break to the largest page size.
280 	 */
281 	if (brk((void *)((((uintptr_t)sbrk(0) - 1) & ~(big - 1)) + big)) != 0)
282 		return;
283 
284 	/*
285 	 * set the preferred page size for the heap
286 	 */
287 	mha.mha_cmd = MHA_MAPSIZE_BSSBRK;
288 	mha.mha_flags = 0;
289 	mha.mha_pagesize = big;
290 
291 	(void) memcntl(NULL, 0, MC_HAT_ADVISE, (caddr_t)&mha, 0, 0);
292 }
293 #endif
294 
295 static void
296 finalize_phase_one(workqueue_t *wq)
297 {
298 	int startslot, i;
299 
300 	/*
301 	 * wip slots are cleared out only when maxbatchsz td's have been merged
302 	 * into them.  We're not guaranteed that the number of files we're
303 	 * merging is a multiple of maxbatchsz, so there will be some partial
304 	 * groups in the wip array.  Move them to the done queue in batch ID
305 	 * order, starting with the slot containing the next batch that would
306 	 * have been placed on the done queue, followed by the others.
307 	 * One thread will be doing this while the others wait at the barrier
308 	 * back in worker_thread(), so we don't need to worry about pesky things
309 	 * like locks.
310 	 */
311 
312 	for (startslot = -1, i = 0; i < wq->wq_nwipslots; i++) {
313 		if (wq->wq_wip[i].wip_batchid == wq->wq_lastdonebatch + 1) {
314 			startslot = i;
315 			break;
316 		}
317 	}
318 
319 	assert(startslot != -1);
320 
321 	for (i = startslot; i < startslot + wq->wq_nwipslots; i++) {
322 		int slotnum = i % wq->wq_nwipslots;
323 		wip_t *wipslot = &wq->wq_wip[slotnum];
324 
325 		if (wipslot->wip_td != NULL) {
326 			debug(2, "clearing slot %d (%d) (saving %d)\n",
327 			    slotnum, i, wipslot->wip_nmerged);
328 		} else
329 			debug(2, "clearing slot %d (%d)\n", slotnum, i);
330 
331 		if (wipslot->wip_td != NULL) {
332 			fifo_add(wq->wq_donequeue, wipslot->wip_td);
333 			wq->wq_wip[slotnum].wip_td = NULL;
334 		}
335 	}
336 
337 	wq->wq_lastdonebatch = wq->wq_next_batchid++;
338 
339 	debug(2, "phase one done: donequeue has %d items\n",
340 	    fifo_len(wq->wq_donequeue));
341 }
342 
343 static void
344 init_phase_two(workqueue_t *wq)
345 {
346 	int num;
347 
348 	/*
349 	 * We're going to continually merge the first two entries on the queue,
350 	 * placing the result on the end, until there's nothing left to merge.
351 	 * At that point, everything will have been merged into one.  The
352 	 * initial value of ninqueue needs to be equal to the total number of
353 	 * entries that will show up on the queue, both at the start of the
354 	 * phase and as generated by merges during the phase.
355 	 */
356 	wq->wq_ninqueue = num = fifo_len(wq->wq_donequeue);
357 	while (num != 1) {
358 		wq->wq_ninqueue += num / 2;
359 		num = num / 2 + num % 2;
360 	}
361 
362 	/*
363 	 * Move the done queue to the work queue.  We won't be using the done
364 	 * queue in phase 2.
365 	 */
366 	assert(fifo_len(wq->wq_queue) == 0);
367 	fifo_free(wq->wq_queue, NULL);
368 	wq->wq_queue = wq->wq_donequeue;
369 }
370 
371 static void
372 wip_save_work(workqueue_t *wq, wip_t *slot, int slotnum)
373 {
374 	pthread_mutex_lock(&wq->wq_donequeue_lock);
375 
376 	while (wq->wq_lastdonebatch + 1 < slot->wip_batchid)
377 		 pthread_cond_wait(&slot->wip_cv, &wq->wq_donequeue_lock);
378 	assert(wq->wq_lastdonebatch + 1 == slot->wip_batchid);
379 
380 	fifo_add(wq->wq_donequeue, slot->wip_td);
381 	wq->wq_lastdonebatch++;
382 	pthread_cond_signal(&wq->wq_wip[(slotnum + 1) %
383 	    wq->wq_nwipslots].wip_cv);
384 
385 	/* reset the slot for next use */
386 	slot->wip_td = NULL;
387 	slot->wip_batchid = wq->wq_next_batchid++;
388 
389 	pthread_mutex_unlock(&wq->wq_donequeue_lock);
390 }
391 
392 static void
393 wip_add_work(wip_t *slot, tdata_t *pow)
394 {
395 	if (slot->wip_td == NULL) {
396 		slot->wip_td = pow;
397 		slot->wip_nmerged = 1;
398 	} else {
399 		debug(2, "%d: merging %p into %p\n", pthread_self(),
400 		    (void *)pow, (void *)slot->wip_td);
401 
402 		merge_into_master(pow, slot->wip_td, NULL, 0);
403 		tdata_free(pow);
404 
405 		slot->wip_nmerged++;
406 	}
407 }
408 
409 static void
410 worker_runphase1(workqueue_t *wq)
411 {
412 	wip_t *wipslot;
413 	tdata_t *pow;
414 	int wipslotnum, pownum;
415 
416 	for (;;) {
417 		pthread_mutex_lock(&wq->wq_queue_lock);
418 
419 		while (fifo_empty(wq->wq_queue)) {
420 			if (wq->wq_nomorefiles == 1) {
421 				pthread_cond_broadcast(&wq->wq_work_avail);
422 				pthread_mutex_unlock(&wq->wq_queue_lock);
423 
424 				/* on to phase 2 ... */
425 				return;
426 			}
427 
428 			pthread_cond_wait(&wq->wq_work_avail,
429 			    &wq->wq_queue_lock);
430 		}
431 
432 		/* there's work to be done! */
433 		pow = fifo_remove(wq->wq_queue);
434 		pownum = wq->wq_nextpownum++;
435 		pthread_cond_broadcast(&wq->wq_work_removed);
436 
437 		assert(pow != NULL);
438 
439 		/* merge it into the right slot */
440 		wipslotnum = pownum % wq->wq_nwipslots;
441 		wipslot = &wq->wq_wip[wipslotnum];
442 
443 		pthread_mutex_lock(&wipslot->wip_lock);
444 
445 		pthread_mutex_unlock(&wq->wq_queue_lock);
446 
447 		wip_add_work(wipslot, pow);
448 
449 		if (wipslot->wip_nmerged == wq->wq_maxbatchsz)
450 			wip_save_work(wq, wipslot, wipslotnum);
451 
452 		pthread_mutex_unlock(&wipslot->wip_lock);
453 	}
454 }
455 
456 static void
457 worker_runphase2(workqueue_t *wq)
458 {
459 	tdata_t *pow1, *pow2;
460 	int batchid;
461 
462 	for (;;) {
463 		pthread_mutex_lock(&wq->wq_queue_lock);
464 
465 		if (wq->wq_ninqueue == 1) {
466 			pthread_cond_broadcast(&wq->wq_work_avail);
467 			pthread_mutex_unlock(&wq->wq_queue_lock);
468 
469 			debug(2, "%d: entering p2 completion barrier\n",
470 			    pthread_self());
471 			if (barrier_wait(&wq->wq_bar1)) {
472 				pthread_mutex_lock(&wq->wq_queue_lock);
473 				wq->wq_alldone = 1;
474 				pthread_cond_signal(&wq->wq_alldone_cv);
475 				pthread_mutex_unlock(&wq->wq_queue_lock);
476 			}
477 
478 			return;
479 		}
480 
481 		if (fifo_len(wq->wq_queue) < 2) {
482 			pthread_cond_wait(&wq->wq_work_avail,
483 			    &wq->wq_queue_lock);
484 			pthread_mutex_unlock(&wq->wq_queue_lock);
485 			continue;
486 		}
487 
488 		/* there's work to be done! */
489 		pow1 = fifo_remove(wq->wq_queue);
490 		pow2 = fifo_remove(wq->wq_queue);
491 		wq->wq_ninqueue -= 2;
492 
493 		batchid = wq->wq_next_batchid++;
494 
495 		pthread_mutex_unlock(&wq->wq_queue_lock);
496 
497 		debug(2, "%d: merging %p into %p\n", pthread_self(),
498 		    (void *)pow1, (void *)pow2);
499 		merge_into_master(pow1, pow2, NULL, 0);
500 		tdata_free(pow1);
501 
502 		/*
503 		 * merging is complete.  place at the tail of the queue in
504 		 * proper order.
505 		 */
506 		pthread_mutex_lock(&wq->wq_queue_lock);
507 		while (wq->wq_lastdonebatch + 1 != batchid) {
508 			pthread_cond_wait(&wq->wq_done_cv,
509 			    &wq->wq_queue_lock);
510 		}
511 
512 		wq->wq_lastdonebatch = batchid;
513 
514 		fifo_add(wq->wq_queue, pow2);
515 		debug(2, "%d: added %p to queue, len now %d, ninqueue %d\n",
516 		    pthread_self(), (void *)pow2, fifo_len(wq->wq_queue),
517 		    wq->wq_ninqueue);
518 		pthread_cond_broadcast(&wq->wq_done_cv);
519 		pthread_cond_signal(&wq->wq_work_avail);
520 		pthread_mutex_unlock(&wq->wq_queue_lock);
521 	}
522 }
523 
524 /*
525  * Main loop for worker threads.
526  */
527 static void
528 worker_thread(workqueue_t *wq)
529 {
530 	worker_runphase1(wq);
531 
532 	debug(2, "%d: entering first barrier\n", pthread_self());
533 
534 	if (barrier_wait(&wq->wq_bar1)) {
535 
536 		debug(2, "%d: doing work in first barrier\n", pthread_self());
537 
538 		finalize_phase_one(wq);
539 
540 		init_phase_two(wq);
541 
542 		debug(2, "%d: ninqueue is %d, %d on queue\n", pthread_self(),
543 		    wq->wq_ninqueue, fifo_len(wq->wq_queue));
544 	}
545 
546 	debug(2, "%d: entering second barrier\n", pthread_self());
547 
548 	(void) barrier_wait(&wq->wq_bar2);
549 
550 	debug(2, "%d: phase 1 complete\n", pthread_self());
551 
552 	worker_runphase2(wq);
553 }
554 
555 /*
556  * Pass a tdata_t tree, built from an input file, off to the work queue for
557  * consumption by worker threads.
558  */
559 static int
560 merge_ctf_cb(tdata_t *td, char *name, void *arg)
561 {
562 	workqueue_t *wq = arg;
563 
564 	debug(3, "Adding tdata %p for processing\n", (void *)td);
565 
566 	pthread_mutex_lock(&wq->wq_queue_lock);
567 	while (fifo_len(wq->wq_queue) > wq->wq_ithrottle) {
568 		debug(2, "Throttling input (len = %d, throttle = %d)\n",
569 		    fifo_len(wq->wq_queue), wq->wq_ithrottle);
570 		pthread_cond_wait(&wq->wq_work_removed, &wq->wq_queue_lock);
571 	}
572 
573 	fifo_add(wq->wq_queue, td);
574 	debug(1, "Thread %d announcing %s\n", pthread_self(), name);
575 	pthread_cond_broadcast(&wq->wq_work_avail);
576 	pthread_mutex_unlock(&wq->wq_queue_lock);
577 
578 	return (1);
579 }
580 
581 /*
582  * This program is intended to be invoked from a Makefile, as part of the build.
583  * As such, in the event of a failure or user-initiated interrupt (^C), we need
584  * to ensure that a subsequent re-make will cause ctfmerge to be executed again.
585  * Unfortunately, ctfmerge will usually be invoked directly after (and as part
586  * of the same Makefile rule as) a link, and will operate on the linked file
587  * in place.  If we merely exit upon receipt of a SIGINT, a subsequent make
588  * will notice that the *linked* file is newer than the object files, and thus
589  * will not reinvoke ctfmerge.  The only way to ensure that a subsequent make
590  * reinvokes ctfmerge, is to remove the file to which we are adding CTF
591  * data (confusingly named the output file).  This means that the link will need
592  * to happen again, but links are generally fast, and we can't allow the merge
593  * to be skipped.
594  *
595  * Another possibility would be to block SIGINT entirely - to always run to
596  * completion.  The run time of ctfmerge can, however, be measured in minutes
597  * in some cases, so this is not a valid option.
598  */
599 static void
600 handle_sig(int sig)
601 {
602 	terminate("Caught signal %d - exiting\n", sig);
603 }
604 
605 static void
606 terminate_cleanup(void)
607 {
608 	int dounlink = getenv("CTFMERGE_TERMINATE_NO_UNLINK") ? 0 : 1;
609 
610 	if (tmpname != NULL && dounlink)
611 		unlink(tmpname);
612 
613 	if (outfile == NULL)
614 		return;
615 
616 #if !defined(__FreeBSD__)
617 	if (dounlink) {
618 		fprintf(stderr, "Removing %s\n", outfile);
619 		unlink(outfile);
620 	}
621 #endif
622 }
623 
624 static void
625 copy_ctf_data(char *srcfile, char *destfile, int keep_stabs)
626 {
627 	tdata_t *srctd;
628 
629 	if (read_ctf(&srcfile, 1, NULL, read_ctf_save_cb, &srctd, 1) == 0)
630 		terminate("No CTF data found in source file %s\n", srcfile);
631 
632 	tmpname = mktmpname(destfile, ".ctf");
633 	write_ctf(srctd, destfile, tmpname, CTF_COMPRESS | keep_stabs);
634 	if (rename(tmpname, destfile) != 0) {
635 		terminate("Couldn't rename temp file %s to %s", tmpname,
636 		    destfile);
637 	}
638 	free(tmpname);
639 	tdata_free(srctd);
640 }
641 
642 static void
643 wq_init(workqueue_t *wq, int nfiles)
644 {
645 	int throttle, nslots, i;
646 
647 	if (getenv("CTFMERGE_MAX_SLOTS"))
648 		nslots = atoi(getenv("CTFMERGE_MAX_SLOTS"));
649 	else
650 		nslots = MERGE_PHASE1_MAX_SLOTS;
651 
652 	if (getenv("CTFMERGE_PHASE1_BATCH_SIZE"))
653 		wq->wq_maxbatchsz = atoi(getenv("CTFMERGE_PHASE1_BATCH_SIZE"));
654 	else
655 		wq->wq_maxbatchsz = MERGE_PHASE1_BATCH_SIZE;
656 
657 	nslots = MIN(nslots, (nfiles + wq->wq_maxbatchsz - 1) /
658 	    wq->wq_maxbatchsz);
659 
660 	wq->wq_wip = xcalloc(sizeof (wip_t) * nslots);
661 	wq->wq_nwipslots = nslots;
662 	wq->wq_nthreads = MIN(sysconf(_SC_NPROCESSORS_ONLN) * 3 / 2, nslots);
663 	wq->wq_thread = xmalloc(sizeof (pthread_t) * wq->wq_nthreads);
664 
665 	if (getenv("CTFMERGE_INPUT_THROTTLE"))
666 		throttle = atoi(getenv("CTFMERGE_INPUT_THROTTLE"));
667 	else
668 		throttle = MERGE_INPUT_THROTTLE_LEN;
669 	wq->wq_ithrottle = throttle * wq->wq_nthreads;
670 
671 	debug(1, "Using %d slots, %d threads\n", wq->wq_nwipslots,
672 	    wq->wq_nthreads);
673 
674 	wq->wq_next_batchid = 0;
675 
676 	for (i = 0; i < nslots; i++) {
677 		pthread_mutex_init(&wq->wq_wip[i].wip_lock, NULL);
678 		pthread_cond_init(&wq->wq_wip[i].wip_cv, NULL);
679 		wq->wq_wip[i].wip_batchid = wq->wq_next_batchid++;
680 	}
681 
682 	pthread_mutex_init(&wq->wq_queue_lock, NULL);
683 	wq->wq_queue = fifo_new();
684 	pthread_cond_init(&wq->wq_work_avail, NULL);
685 	pthread_cond_init(&wq->wq_work_removed, NULL);
686 	wq->wq_ninqueue = nfiles;
687 	wq->wq_nextpownum = 0;
688 
689 	pthread_mutex_init(&wq->wq_donequeue_lock, NULL);
690 	wq->wq_donequeue = fifo_new();
691 	wq->wq_lastdonebatch = -1;
692 
693 	pthread_cond_init(&wq->wq_done_cv, NULL);
694 
695 	pthread_cond_init(&wq->wq_alldone_cv, NULL);
696 	wq->wq_alldone = 0;
697 
698 	barrier_init(&wq->wq_bar1, wq->wq_nthreads);
699 	barrier_init(&wq->wq_bar2, wq->wq_nthreads);
700 
701 	wq->wq_nomorefiles = 0;
702 }
703 
704 static void
705 start_threads(workqueue_t *wq)
706 {
707 	pthread_t thrid;
708 	sigset_t sets;
709 	int i;
710 
711 	sigemptyset(&sets);
712 	sigaddset(&sets, SIGINT);
713 	sigaddset(&sets, SIGQUIT);
714 	sigaddset(&sets, SIGTERM);
715 	pthread_sigmask(SIG_BLOCK, &sets, NULL);
716 
717 	for (i = 0; i < wq->wq_nthreads; i++) {
718 		pthread_create(&wq->wq_thread[i], NULL,
719 		    (void *(*)(void *))worker_thread, wq);
720 	}
721 
722 #if defined(sun)
723 	sigset(SIGINT, handle_sig);
724 	sigset(SIGQUIT, handle_sig);
725 	sigset(SIGTERM, handle_sig);
726 #else
727 	signal(SIGINT, handle_sig);
728 	signal(SIGQUIT, handle_sig);
729 	signal(SIGTERM, handle_sig);
730 #endif
731 	pthread_sigmask(SIG_UNBLOCK, &sets, NULL);
732 }
733 
734 static void
735 join_threads(workqueue_t *wq)
736 {
737 	int i;
738 
739 	for (i = 0; i < wq->wq_nthreads; i++) {
740 		pthread_join(wq->wq_thread[i], NULL);
741 	}
742 }
743 
744 
745 static int
746 strcompare(const void *p1, const void *p2)
747 {
748 	char *s1 = *((char **)p1);
749 	char *s2 = *((char **)p2);
750 
751 	return (strcmp(s1, s2));
752 }
753 
754 /*
755  * Core work queue structure; passed to worker threads on thread creation
756  * as the main point of coordination.  Allocate as a static structure; we
757  * could have put this into a local variable in main, but passing a pointer
758  * into your stack to another thread is fragile at best and leads to some
759  * hard-to-debug failure modes.
760  */
761 static workqueue_t wq;
762 
763 int
764 main(int argc, char **argv)
765 {
766 	tdata_t *mstrtd, *savetd;
767 	char *uniqfile = NULL, *uniqlabel = NULL;
768 	char *withfile = NULL;
769 	char *label = NULL;
770 	char **ifiles, **tifiles;
771 	int verbose = 0, docopy = 0;
772 	int write_fuzzy_match = 0;
773 	int keep_stabs = 0;
774 	int require_ctf = 0;
775 	int nifiles, nielems;
776 	int c, i, idx, tidx, err;
777 
778 	progname = basename(argv[0]);
779 
780 	if (getenv("CTFMERGE_DEBUG_LEVEL"))
781 		debug_level = atoi(getenv("CTFMERGE_DEBUG_LEVEL"));
782 
783 	err = 0;
784 	while ((c = getopt(argc, argv, ":cd:D:fgl:L:o:tvw:s")) != EOF) {
785 		switch (c) {
786 		case 'c':
787 			docopy = 1;
788 			break;
789 		case 'd':
790 			/* Uniquify against `uniqfile' */
791 			uniqfile = optarg;
792 			break;
793 		case 'D':
794 			/* Uniquify against label `uniqlabel' in `uniqfile' */
795 			uniqlabel = optarg;
796 			break;
797 		case 'f':
798 			write_fuzzy_match = CTF_FUZZY_MATCH;
799 			break;
800 		case 'g':
801 			keep_stabs = CTF_KEEP_STABS;
802 			break;
803 		case 'l':
804 			/* Label merged types with `label' */
805 			label = optarg;
806 			break;
807 		case 'L':
808 			/* Label merged types with getenv(`label`) */
809 			if ((label = getenv(optarg)) == NULL)
810 				label = CTF_DEFAULT_LABEL;
811 			break;
812 		case 'o':
813 			/* Place merged types in CTF section in `outfile' */
814 			outfile = optarg;
815 			break;
816 		case 't':
817 			/* Insist *all* object files built from C have CTF */
818 			require_ctf = 1;
819 			break;
820 		case 'v':
821 			/* More debugging information */
822 			verbose = 1;
823 			break;
824 		case 'w':
825 			/* Additive merge with data from `withfile' */
826 			withfile = optarg;
827 			break;
828 		case 's':
829 			/* use the dynsym rather than the symtab */
830 			dynsym = CTF_USE_DYNSYM;
831 			break;
832 		default:
833 			usage();
834 			exit(2);
835 		}
836 	}
837 
838 	/* Validate arguments */
839 	if (docopy) {
840 		if (uniqfile != NULL || uniqlabel != NULL || label != NULL ||
841 		    outfile != NULL || withfile != NULL || dynsym != 0)
842 			err++;
843 
844 		if (argc - optind != 2)
845 			err++;
846 	} else {
847 		if (uniqfile != NULL && withfile != NULL)
848 			err++;
849 
850 		if (uniqlabel != NULL && uniqfile == NULL)
851 			err++;
852 
853 		if (outfile == NULL || label == NULL)
854 			err++;
855 
856 		if (argc - optind == 0)
857 			err++;
858 	}
859 
860 	if (err) {
861 		usage();
862 		exit(2);
863 	}
864 
865 	if (getenv("STRIPSTABS_KEEP_STABS") != NULL)
866 		keep_stabs = CTF_KEEP_STABS;
867 
868 	if (uniqfile && access(uniqfile, R_OK) != 0) {
869 		warning("Uniquification file %s couldn't be opened and "
870 		    "will be ignored.\n", uniqfile);
871 		uniqfile = NULL;
872 	}
873 	if (withfile && access(withfile, R_OK) != 0) {
874 		warning("With file %s couldn't be opened and will be "
875 		    "ignored.\n", withfile);
876 		withfile = NULL;
877 	}
878 	if (outfile && access(outfile, R_OK|W_OK) != 0)
879 		terminate("Cannot open output file %s for r/w", outfile);
880 
881 	/*
882 	 * This is ugly, but we don't want to have to have a separate tool
883 	 * (yet) just for copying an ELF section with our specific requirements,
884 	 * so we shoe-horn a copier into ctfmerge.
885 	 */
886 	if (docopy) {
887 		copy_ctf_data(argv[optind], argv[optind + 1], keep_stabs);
888 
889 		exit(0);
890 	}
891 
892 	set_terminate_cleanup(terminate_cleanup);
893 
894 	/* Sort the input files and strip out duplicates */
895 	nifiles = argc - optind;
896 	ifiles = xmalloc(sizeof (char *) * nifiles);
897 	tifiles = xmalloc(sizeof (char *) * nifiles);
898 
899 	for (i = 0; i < nifiles; i++)
900 		tifiles[i] = argv[optind + i];
901 	qsort(tifiles, nifiles, sizeof (char *), (int (*)())strcompare);
902 
903 	ifiles[0] = tifiles[0];
904 	for (idx = 0, tidx = 1; tidx < nifiles; tidx++) {
905 		if (strcmp(ifiles[idx], tifiles[tidx]) != 0)
906 			ifiles[++idx] = tifiles[tidx];
907 	}
908 	nifiles = idx + 1;
909 
910 	/* Make sure they all exist */
911 	if ((nielems = count_files(ifiles, nifiles)) < 0)
912 		terminate("Some input files were inaccessible\n");
913 
914 	/* Prepare for the merge */
915 	wq_init(&wq, nielems);
916 
917 	start_threads(&wq);
918 
919 	/*
920 	 * Start the merge
921 	 *
922 	 * We're reading everything from each of the object files, so we
923 	 * don't need to specify labels.
924 	 */
925 	if (read_ctf(ifiles, nifiles, NULL, merge_ctf_cb,
926 	    &wq, require_ctf) == 0) {
927 		/*
928 		 * If we're verifying that C files have CTF, it's safe to
929 		 * assume that in this case, we're building only from assembly
930 		 * inputs.
931 		 */
932 		if (require_ctf)
933 			exit(0);
934 		terminate("No ctf sections found to merge\n");
935 	}
936 
937 	pthread_mutex_lock(&wq.wq_queue_lock);
938 	wq.wq_nomorefiles = 1;
939 	pthread_cond_broadcast(&wq.wq_work_avail);
940 	pthread_mutex_unlock(&wq.wq_queue_lock);
941 
942 	pthread_mutex_lock(&wq.wq_queue_lock);
943 	while (wq.wq_alldone == 0)
944 		pthread_cond_wait(&wq.wq_alldone_cv, &wq.wq_queue_lock);
945 	pthread_mutex_unlock(&wq.wq_queue_lock);
946 
947 	join_threads(&wq);
948 
949 	/*
950 	 * All requested files have been merged, with the resulting tree in
951 	 * mstrtd.  savetd is the tree that will be placed into the output file.
952 	 *
953 	 * Regardless of whether we're doing a normal uniquification or an
954 	 * additive merge, we need a type tree that has been uniquified
955 	 * against uniqfile or withfile, as appropriate.
956 	 *
957 	 * If we're doing a uniquification, we stuff the resulting tree into
958 	 * outfile.  Otherwise, we add the tree to the tree already in withfile.
959 	 */
960 	assert(fifo_len(wq.wq_queue) == 1);
961 	mstrtd = fifo_remove(wq.wq_queue);
962 
963 	if (verbose || debug_level) {
964 		debug(2, "Statistics for td %p\n", (void *)mstrtd);
965 
966 		iidesc_stats(mstrtd->td_iihash);
967 	}
968 
969 	if (uniqfile != NULL || withfile != NULL) {
970 		char *reffile, *reflabel = NULL;
971 		tdata_t *reftd;
972 
973 		if (uniqfile != NULL) {
974 			reffile = uniqfile;
975 			reflabel = uniqlabel;
976 		} else
977 			reffile = withfile;
978 
979 		if (read_ctf(&reffile, 1, reflabel, read_ctf_save_cb,
980 		    &reftd, require_ctf) == 0) {
981 			terminate("No CTF data found in reference file %s\n",
982 			    reffile);
983 		}
984 
985 		savetd = tdata_new();
986 
987 		if (CTF_TYPE_ISCHILD(reftd->td_nextid))
988 			terminate("No room for additional types in master\n");
989 
990 		savetd->td_nextid = withfile ? reftd->td_nextid :
991 		    CTF_INDEX_TO_TYPE(1, TRUE);
992 		merge_into_master(mstrtd, reftd, savetd, 0);
993 
994 		tdata_label_add(savetd, label, CTF_LABEL_LASTIDX);
995 
996 		if (withfile) {
997 			/*
998 			 * savetd holds the new data to be added to the withfile
999 			 */
1000 			tdata_t *withtd = reftd;
1001 
1002 			tdata_merge(withtd, savetd);
1003 
1004 			savetd = withtd;
1005 		} else {
1006 			char uniqname[MAXPATHLEN];
1007 			labelent_t *parle;
1008 
1009 			parle = tdata_label_top(reftd);
1010 
1011 			savetd->td_parlabel = xstrdup(parle->le_name);
1012 
1013 			strncpy(uniqname, reffile, sizeof (uniqname));
1014 			uniqname[MAXPATHLEN - 1] = '\0';
1015 			savetd->td_parname = xstrdup(basename(uniqname));
1016 		}
1017 
1018 	} else {
1019 		/*
1020 		 * No post processing.  Write the merged tree as-is into the
1021 		 * output file.
1022 		 */
1023 		tdata_label_free(mstrtd);
1024 		tdata_label_add(mstrtd, label, CTF_LABEL_LASTIDX);
1025 
1026 		savetd = mstrtd;
1027 	}
1028 
1029 	tmpname = mktmpname(outfile, ".ctf");
1030 	write_ctf(savetd, outfile, tmpname,
1031 	    CTF_COMPRESS | write_fuzzy_match | dynsym | keep_stabs);
1032 	if (rename(tmpname, outfile) != 0)
1033 		terminate("Couldn't rename output temp file %s", tmpname);
1034 	free(tmpname);
1035 
1036 	return (0);
1037 }
1038