xref: /onnv-gate/usr/src/cmd/sgs/mcs/common/main.c (revision 2647:e440e3da2a6f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2647Srie  * Common Development and Distribution License (the "License").
6*2647Srie  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*2647Srie 
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
240Sstevel@tonic-gate  *	  All Rights Reserved
250Sstevel@tonic-gate  *
260Sstevel@tonic-gate  *
27*2647Srie  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
28*2647Srie  * Use is subject to license terms.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include "stdlib.h"
340Sstevel@tonic-gate #include "conv.h"
350Sstevel@tonic-gate #include "mcs.h"
360Sstevel@tonic-gate #include "extern.h"
370Sstevel@tonic-gate #define	OPTUNIT	100
380Sstevel@tonic-gate 
390Sstevel@tonic-gate static size_t optcnt = 0;
400Sstevel@tonic-gate static size_t optbufsz = OPTUNIT;
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * Function prototypes.
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate static void usage(int);
460Sstevel@tonic-gate static void sigexit(int);
470Sstevel@tonic-gate static int setup_sectname(char *, int);
480Sstevel@tonic-gate static void check_swap();
490Sstevel@tonic-gate static void queue(int, char *);
500Sstevel@tonic-gate 
510Sstevel@tonic-gate int
520Sstevel@tonic-gate main(int argc, char ** argv, char ** envp)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate 	const char	*opt;
550Sstevel@tonic-gate 	char		*str;
560Sstevel@tonic-gate 	int		error_count = 0, num_sect = 0, errflag = 0, Dflag = 0;
570Sstevel@tonic-gate 	int		c, i, my_prog;
580Sstevel@tonic-gate 	Cmd_Info	*cmd_info;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	/*
610Sstevel@tonic-gate 	 * Check for a binary that better fits this architecture.
620Sstevel@tonic-gate 	 */
63*2647Srie 	(void) conv_check_native(argv, envp);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	/*
660Sstevel@tonic-gate 	 * mcs(1) and strip() are hard linked together, determine which command
670Sstevel@tonic-gate 	 * was invoked.
680Sstevel@tonic-gate 	 */
690Sstevel@tonic-gate 	prog = argv[0];
700Sstevel@tonic-gate 	if ((str = strrchr(prog, '/')) != NULL)
710Sstevel@tonic-gate 		str++;
720Sstevel@tonic-gate 	else
730Sstevel@tonic-gate 		str = prog;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	if (strcmp(str, "mcs") == 0) {
760Sstevel@tonic-gate 		my_prog = MCS;
770Sstevel@tonic-gate 		opt = "Da:cdn:pVz?";
780Sstevel@tonic-gate 	} else if (strcmp(str, "strip") == 0) {
790Sstevel@tonic-gate 		my_prog = STRIP;
800Sstevel@tonic-gate 		opt = "DlxV?";
810Sstevel@tonic-gate 	} else
820Sstevel@tonic-gate 		exit(1);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
850Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
860Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
870Sstevel@tonic-gate #endif
880Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	for (i = 0; signum[i]; i++)
910Sstevel@tonic-gate 		if (signal(signum[i], SIG_IGN) != SIG_IGN)
920Sstevel@tonic-gate 			(void) signal(signum[i], sigexit);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	if ((Action =
950Sstevel@tonic-gate 	    malloc(optbufsz * sizeof (struct action))) == NULL) {
960Sstevel@tonic-gate 		error_message(MALLOC_ERROR, PLAIN_ERROR, (char *)0, prog);
970Sstevel@tonic-gate 		exit(FAILURE);
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	/*
1010Sstevel@tonic-gate 	 * Allocate command info structure
1020Sstevel@tonic-gate 	 */
1030Sstevel@tonic-gate 	cmd_info = (Cmd_Info *) calloc(1, sizeof (Cmd_Info));
1040Sstevel@tonic-gate 	if (cmd_info == NULL) {
1050Sstevel@tonic-gate 		error_message(MALLOC_ERROR, PLAIN_ERROR, (char *)0, prog);
1060Sstevel@tonic-gate 		exit(FAILURE);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 	if (my_prog == STRIP)
1090Sstevel@tonic-gate 		cmd_info->flags |= I_AM_STRIP;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	while ((c = getopt(argc, argv, (char *)opt)) != EOF) {
1120Sstevel@tonic-gate 		switch (c) {
1130Sstevel@tonic-gate 		case 'D':
1140Sstevel@tonic-gate 			optcnt++;
1150Sstevel@tonic-gate 			Dflag++;
1160Sstevel@tonic-gate 			break;
1170Sstevel@tonic-gate 		case 'a':
1180Sstevel@tonic-gate 			optcnt++;
1190Sstevel@tonic-gate 			queue(ACT_APPEND, optarg);
1200Sstevel@tonic-gate 			cmd_info->flags |= (MIGHT_CHG | aFLAG);
1210Sstevel@tonic-gate 			cmd_info->str_size += strlen(optarg) + 1;
1220Sstevel@tonic-gate 			break;
1230Sstevel@tonic-gate 		case 'c':
1240Sstevel@tonic-gate 			optcnt++;
1250Sstevel@tonic-gate 			queue(ACT_COMPRESS, NULL);
1260Sstevel@tonic-gate 			cmd_info->flags |= (MIGHT_CHG | cFLAG);
1270Sstevel@tonic-gate 			break;
1280Sstevel@tonic-gate 		case 'd':
1290Sstevel@tonic-gate 			optcnt++;
1300Sstevel@tonic-gate 			if (CHK_OPT(cmd_info, dFLAG) == 0)
1310Sstevel@tonic-gate 				queue(ACT_DELETE, NULL);
1320Sstevel@tonic-gate 			cmd_info->flags |= (MIGHT_CHG | dFLAG);
1330Sstevel@tonic-gate 			break;
1340Sstevel@tonic-gate 		case 'z':
1350Sstevel@tonic-gate 			optcnt++;
1360Sstevel@tonic-gate 			queue(ACT_ZAP, NULL);
1370Sstevel@tonic-gate 			cmd_info->flags |= (MIGHT_CHG | zFLAG);
1380Sstevel@tonic-gate 			break;
1390Sstevel@tonic-gate 		case 'n':
1400Sstevel@tonic-gate 			(void) setup_sectname(optarg, my_prog);
1410Sstevel@tonic-gate 			num_sect++;
1420Sstevel@tonic-gate 			break;
1430Sstevel@tonic-gate 		case 'l':
1440Sstevel@tonic-gate 			optcnt++;
1450Sstevel@tonic-gate 			cmd_info->flags |= lFLAG;
1460Sstevel@tonic-gate 			break;
1470Sstevel@tonic-gate 		case 'p':
1480Sstevel@tonic-gate 			optcnt++;
1490Sstevel@tonic-gate 			queue(ACT_PRINT, NULL);
1500Sstevel@tonic-gate 			cmd_info->flags |= pFLAG;
1510Sstevel@tonic-gate 			break;
1520Sstevel@tonic-gate 		case 'x':
1530Sstevel@tonic-gate 			optcnt++;
1540Sstevel@tonic-gate 			cmd_info->flags |= xFLAG;
1550Sstevel@tonic-gate 			break;
1560Sstevel@tonic-gate 		case 'V':
1570Sstevel@tonic-gate 			cmd_info->flags |= VFLAG;
1580Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s %s\n", prog,
1590Sstevel@tonic-gate 			    (const char *)SGU_PKG, (const char *)SGU_REL);
1600Sstevel@tonic-gate 			break;
1610Sstevel@tonic-gate 		case '?':
1620Sstevel@tonic-gate 			errflag++;
1630Sstevel@tonic-gate 			break;
1640Sstevel@tonic-gate 		default:
1650Sstevel@tonic-gate 			break;
1660Sstevel@tonic-gate 		}
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if (errflag) {
1700Sstevel@tonic-gate 		usage(my_prog);
1710Sstevel@tonic-gate 		exit(FAILURE);
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	if (Dflag)
1750Sstevel@tonic-gate 		check_swap();
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/*
1780Sstevel@tonic-gate 	 * strip command may not take any options.
1790Sstevel@tonic-gate 	 */
1800Sstevel@tonic-gate 	if (my_prog != STRIP) {
1810Sstevel@tonic-gate 		if (argc == optind &&
1820Sstevel@tonic-gate 		    (CHK_OPT(cmd_info, MIGHT_CHG) || CHK_OPT(cmd_info, pFLAG) ||
1830Sstevel@tonic-gate 		    argc == 1))
1840Sstevel@tonic-gate 			usage(my_prog);
1850Sstevel@tonic-gate 		else if (!CHK_OPT(cmd_info, MIGHT_CHG) &&
1860Sstevel@tonic-gate 		    !CHK_OPT(cmd_info, pFLAG) && !CHK_OPT(cmd_info, VFLAG))
1870Sstevel@tonic-gate 			usage(my_prog);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	/*
1910Sstevel@tonic-gate 	 * This version only allows multiple section names
1920Sstevel@tonic-gate 	 * only for -d option.
1930Sstevel@tonic-gate 	 */
1940Sstevel@tonic-gate 	if ((num_sect >= 2) && (CHK_OPT(cmd_info, pFLAG) ||
1950Sstevel@tonic-gate 	    CHK_OPT(cmd_info, aFLAG) ||
1960Sstevel@tonic-gate 	    CHK_OPT(cmd_info, cFLAG))) {
1970Sstevel@tonic-gate 		error_message(USAGE_ERROR, PLAIN_ERROR, (char *)0,  prog);
1980Sstevel@tonic-gate 		exit(1);
1990Sstevel@tonic-gate 	}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	/*
2020Sstevel@tonic-gate 	 * If no -n was specified,
2030Sstevel@tonic-gate 	 * set the default, ".comment".
2040Sstevel@tonic-gate 	 * This is for mcs only.
2050Sstevel@tonic-gate 	 */
2060Sstevel@tonic-gate 	if (num_sect == 0 && my_prog == MCS) {
2070Sstevel@tonic-gate 		(void) setup_sectname(".comment", MCS);
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/*
2110Sstevel@tonic-gate 	 * If I am strip command, then add needed
2120Sstevel@tonic-gate 	 * section names.
2130Sstevel@tonic-gate 	 */
2140Sstevel@tonic-gate 	if (my_prog == STRIP) {
2150Sstevel@tonic-gate 		(void) setup_sectname(".line", MCS);
2160Sstevel@tonic-gate 		if (CHK_OPT(cmd_info, lFLAG) == 0) {
2170Sstevel@tonic-gate 			(void) setup_sectname(".debug", STRIP);
2180Sstevel@tonic-gate 			(void) setup_sectname(".stab", STRIP);
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 		if (CHK_OPT(cmd_info, dFLAG) == 0) {
2210Sstevel@tonic-gate 			queue(ACT_DELETE, NULL);
2220Sstevel@tonic-gate 			cmd_info->flags |= MIGHT_CHG;
2230Sstevel@tonic-gate 			cmd_info->flags |= dFLAG;
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	(void) elf_version(EV_NONE);
2280Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
2290Sstevel@tonic-gate 		error_message(ELFVER_ERROR, LIBelf_ERROR, elf_errmsg(-1), prog);
2300Sstevel@tonic-gate 		exit(FAILURE);
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	if (CHK_OPT(cmd_info, pFLAG) || CHK_OPT(cmd_info, MIGHT_CHG)) {
2340Sstevel@tonic-gate 		for (; optind < argc; optind++) {
2350Sstevel@tonic-gate 			error_count = error_count +
2360Sstevel@tonic-gate 			    (each_file(argv[optind], cmd_info));
2370Sstevel@tonic-gate 		}
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if (Dflag)
2410Sstevel@tonic-gate 		check_swap();
2420Sstevel@tonic-gate 	mcs_exit(error_count);
2430Sstevel@tonic-gate 	/*NOTREACHED*/
2440Sstevel@tonic-gate 	return (0);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate  * Supplementary functions
2490Sstevel@tonic-gate  */
2500Sstevel@tonic-gate static void
2510Sstevel@tonic-gate queue(int activity, char *string)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate 	if (optcnt > optbufsz) {
2540Sstevel@tonic-gate 		optbufsz = optbufsz * 2;
2550Sstevel@tonic-gate 		if ((Action = realloc((struct action *)Action,
2560Sstevel@tonic-gate 		    optbufsz * sizeof (struct action))) == NULL) {
2570Sstevel@tonic-gate 		    error_message(MALLOC_ERROR, PLAIN_ERROR, (char *)0, prog);
2580Sstevel@tonic-gate 		    mcs_exit(FAILURE);
2590Sstevel@tonic-gate 		}
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 	Action[actmax].a_action = activity;
2620Sstevel@tonic-gate 	Action[actmax].a_cnt = 0;
2630Sstevel@tonic-gate 	Action[actmax].a_string = string;
2640Sstevel@tonic-gate 	actmax++;
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /*ARGSUSED0*/
2680Sstevel@tonic-gate static void
2690Sstevel@tonic-gate sigexit(int i)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	(void) unlink(artmpfile);
2720Sstevel@tonic-gate 	(void) unlink(elftmpfile);
2730Sstevel@tonic-gate 	exit(100);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate static void
2770Sstevel@tonic-gate usage(int me)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate 	if (me == MCS)
2800Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
2810Sstevel@tonic-gate 		"usage: %s [-cdpVz] [-a string] [-n name] file ...\n"), prog);
2820Sstevel@tonic-gate 	else
2830Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
2840Sstevel@tonic-gate 		"usage: %s [-lVx] file ...\n"), prog);
2850Sstevel@tonic-gate 	mcs_exit(FAILURE);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate void
2890Sstevel@tonic-gate mcs_exit(int val)
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate 	(void) unlink(artmpfile);
2920Sstevel@tonic-gate 	(void) unlink(elftmpfile);
2930Sstevel@tonic-gate 	exit(val);
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate  * Insert the section name 'name' into the
2980Sstevel@tonic-gate  * section list.
2990Sstevel@tonic-gate  */
3000Sstevel@tonic-gate static int
3010Sstevel@tonic-gate setup_sectname(char *name, int whoami)
3020Sstevel@tonic-gate {
3030Sstevel@tonic-gate 	S_Name *new;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	/*
3060Sstevel@tonic-gate 	 * Check if the name is already specified or not.
3070Sstevel@tonic-gate 	 */
3080Sstevel@tonic-gate 	if ((whoami == MCS) && (sectcmp(name) == 0))
3090Sstevel@tonic-gate 		return (0);
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	/*
3120Sstevel@tonic-gate 	 * Allocate one
3130Sstevel@tonic-gate 	 */
3140Sstevel@tonic-gate 	if ((new = malloc(sizeof (S_Name))) == NULL) {
3150Sstevel@tonic-gate 		error_message(MALLOC_ERROR, PLAIN_ERROR, (char *)0, prog);
3160Sstevel@tonic-gate 		exit(FAILURE);
3170Sstevel@tonic-gate 	}
3180Sstevel@tonic-gate 	new->name = strdup(name);
3190Sstevel@tonic-gate 	if (new->name == NULL) {
3200Sstevel@tonic-gate 		error_message(USAGE_ERROR, PLAIN_ERROR, (char *)0, prog);
3210Sstevel@tonic-gate 		exit(FAILURE);
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 	if (whoami == STRIP)
3240Sstevel@tonic-gate 		new->flags = SNAME_FLG_STRNCMP;
3250Sstevel@tonic-gate 	new->next = NULL;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	/*
3280Sstevel@tonic-gate 	 * Put this one in the list
3290Sstevel@tonic-gate 	 */
3300Sstevel@tonic-gate 	new->next = sect_head;
3310Sstevel@tonic-gate 	sect_head = new;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	return (0);
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate /*
3370Sstevel@tonic-gate  * Check if the 'name' exists in the section list.
3380Sstevel@tonic-gate  *
3390Sstevel@tonic-gate  * If found
3400Sstevel@tonic-gate  *	return 0;
3410Sstevel@tonic-gate  * else
3420Sstevel@tonic-gate  *	return 1
3430Sstevel@tonic-gate  */
3440Sstevel@tonic-gate int
3450Sstevel@tonic-gate sectcmp(char *name)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	/*
3480Sstevel@tonic-gate 	 * Check if the name is already specified or not.
3490Sstevel@tonic-gate 	 */
3500Sstevel@tonic-gate 	if (sect_head != NULL) {
3510Sstevel@tonic-gate 		S_Name *p1 = sect_head;
3520Sstevel@tonic-gate 		while (p1 != NULL) {
3530Sstevel@tonic-gate 			if (p1->flags & SNAME_FLG_STRNCMP) {
3540Sstevel@tonic-gate 				if (strncmp(p1->name,
3550Sstevel@tonic-gate 				    name, strlen(p1->name)) == 0)
3560Sstevel@tonic-gate 					return (0);
3570Sstevel@tonic-gate 			} else if (strcmp(p1->name, name) == 0) {
3580Sstevel@tonic-gate 				return (0);	/* silently ignore */
3590Sstevel@tonic-gate 			}
3600Sstevel@tonic-gate 			p1 = p1->next;
3610Sstevel@tonic-gate 		}
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 	return (1);
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate static void
3670Sstevel@tonic-gate check_swap()
3680Sstevel@tonic-gate {
3690Sstevel@tonic-gate 	(void) system("/usr/sbin/swap -s");
3700Sstevel@tonic-gate }
371