xref: /onnv-gate/usr/src/cmd/sgs/mcs/common/main.c (revision 8223:628841638a21)
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
52647Srie  * Common Development and Distribution License (the "License").
62647Srie  * 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  */
212647Srie 
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
240Sstevel@tonic-gate  *	  All Rights Reserved
250Sstevel@tonic-gate  *
260Sstevel@tonic-gate  *
27*8223SAli.Bahrami@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
282647Srie  * Use is subject to license terms.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include "stdlib.h"
320Sstevel@tonic-gate #include "conv.h"
330Sstevel@tonic-gate #include "mcs.h"
340Sstevel@tonic-gate #include "extern.h"
350Sstevel@tonic-gate #define	OPTUNIT	100
360Sstevel@tonic-gate 
370Sstevel@tonic-gate static size_t optcnt = 0;
380Sstevel@tonic-gate static size_t optbufsz = OPTUNIT;
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * Function prototypes.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate static void usage(int);
440Sstevel@tonic-gate static void sigexit(int);
450Sstevel@tonic-gate static int setup_sectname(char *, int);
460Sstevel@tonic-gate static void check_swap();
470Sstevel@tonic-gate static void queue(int, char *);
480Sstevel@tonic-gate 
490Sstevel@tonic-gate int
main(int argc,char ** argv,char ** envp)500Sstevel@tonic-gate main(int argc, char ** argv, char ** envp)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	const char	*opt;
530Sstevel@tonic-gate 	char		*str;
540Sstevel@tonic-gate 	int		error_count = 0, num_sect = 0, errflag = 0, Dflag = 0;
550Sstevel@tonic-gate 	int		c, i, my_prog;
560Sstevel@tonic-gate 	Cmd_Info	*cmd_info;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	/*
590Sstevel@tonic-gate 	 * Check for a binary that better fits this architecture.
600Sstevel@tonic-gate 	 */
612647Srie 	(void) conv_check_native(argv, envp);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	/*
640Sstevel@tonic-gate 	 * mcs(1) and strip() are hard linked together, determine which command
650Sstevel@tonic-gate 	 * was invoked.
660Sstevel@tonic-gate 	 */
670Sstevel@tonic-gate 	prog = argv[0];
680Sstevel@tonic-gate 	if ((str = strrchr(prog, '/')) != NULL)
690Sstevel@tonic-gate 		str++;
700Sstevel@tonic-gate 	else
710Sstevel@tonic-gate 		str = prog;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if (strcmp(str, "mcs") == 0) {
740Sstevel@tonic-gate 		my_prog = MCS;
750Sstevel@tonic-gate 		opt = "Da:cdn:pVz?";
760Sstevel@tonic-gate 	} else if (strcmp(str, "strip") == 0) {
770Sstevel@tonic-gate 		my_prog = STRIP;
780Sstevel@tonic-gate 		opt = "DlxV?";
790Sstevel@tonic-gate 	} else
80*8223SAli.Bahrami@Sun.COM 		exit(FAILURE);
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
830Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
840Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
850Sstevel@tonic-gate #endif
860Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	for (i = 0; signum[i]; i++)
890Sstevel@tonic-gate 		if (signal(signum[i], SIG_IGN) != SIG_IGN)
900Sstevel@tonic-gate 			(void) signal(signum[i], sigexit);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	if ((Action =
930Sstevel@tonic-gate 	    malloc(optbufsz * sizeof (struct action))) == NULL) {
940Sstevel@tonic-gate 		error_message(MALLOC_ERROR, PLAIN_ERROR, (char *)0, prog);
950Sstevel@tonic-gate 		exit(FAILURE);
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/*
990Sstevel@tonic-gate 	 * Allocate command info structure
1000Sstevel@tonic-gate 	 */
1010Sstevel@tonic-gate 	cmd_info = (Cmd_Info *) calloc(1, sizeof (Cmd_Info));
1020Sstevel@tonic-gate 	if (cmd_info == NULL) {
1030Sstevel@tonic-gate 		error_message(MALLOC_ERROR, PLAIN_ERROR, (char *)0, prog);
1040Sstevel@tonic-gate 		exit(FAILURE);
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 	if (my_prog == STRIP)
1070Sstevel@tonic-gate 		cmd_info->flags |= I_AM_STRIP;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	while ((c = getopt(argc, argv, (char *)opt)) != EOF) {
1100Sstevel@tonic-gate 		switch (c) {
1110Sstevel@tonic-gate 		case 'D':
1120Sstevel@tonic-gate 			optcnt++;
1130Sstevel@tonic-gate 			Dflag++;
1140Sstevel@tonic-gate 			break;
1150Sstevel@tonic-gate 		case 'a':
1160Sstevel@tonic-gate 			optcnt++;
1170Sstevel@tonic-gate 			queue(ACT_APPEND, optarg);
1180Sstevel@tonic-gate 			cmd_info->flags |= (MIGHT_CHG | aFLAG);
1190Sstevel@tonic-gate 			cmd_info->str_size += strlen(optarg) + 1;
1200Sstevel@tonic-gate 			break;
1210Sstevel@tonic-gate 		case 'c':
1220Sstevel@tonic-gate 			optcnt++;
1230Sstevel@tonic-gate 			queue(ACT_COMPRESS, NULL);
1240Sstevel@tonic-gate 			cmd_info->flags |= (MIGHT_CHG | cFLAG);
1250Sstevel@tonic-gate 			break;
1260Sstevel@tonic-gate 		case 'd':
1270Sstevel@tonic-gate 			optcnt++;
1280Sstevel@tonic-gate 			if (CHK_OPT(cmd_info, dFLAG) == 0)
1290Sstevel@tonic-gate 				queue(ACT_DELETE, NULL);
1300Sstevel@tonic-gate 			cmd_info->flags |= (MIGHT_CHG | dFLAG);
1310Sstevel@tonic-gate 			break;
1320Sstevel@tonic-gate 		case 'z':
1330Sstevel@tonic-gate 			optcnt++;
1340Sstevel@tonic-gate 			queue(ACT_ZAP, NULL);
1350Sstevel@tonic-gate 			cmd_info->flags |= (MIGHT_CHG | zFLAG);
1360Sstevel@tonic-gate 			break;
1370Sstevel@tonic-gate 		case 'n':
1380Sstevel@tonic-gate 			(void) setup_sectname(optarg, my_prog);
1390Sstevel@tonic-gate 			num_sect++;
1400Sstevel@tonic-gate 			break;
1410Sstevel@tonic-gate 		case 'l':
1420Sstevel@tonic-gate 			optcnt++;
1430Sstevel@tonic-gate 			cmd_info->flags |= lFLAG;
1440Sstevel@tonic-gate 			break;
1450Sstevel@tonic-gate 		case 'p':
1460Sstevel@tonic-gate 			optcnt++;
1470Sstevel@tonic-gate 			queue(ACT_PRINT, NULL);
1480Sstevel@tonic-gate 			cmd_info->flags |= pFLAG;
1490Sstevel@tonic-gate 			break;
1500Sstevel@tonic-gate 		case 'x':
1510Sstevel@tonic-gate 			optcnt++;
1520Sstevel@tonic-gate 			cmd_info->flags |= xFLAG;
1530Sstevel@tonic-gate 			break;
1540Sstevel@tonic-gate 		case 'V':
1550Sstevel@tonic-gate 			cmd_info->flags |= VFLAG;
1560Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s %s\n", prog,
1570Sstevel@tonic-gate 			    (const char *)SGU_PKG, (const char *)SGU_REL);
1580Sstevel@tonic-gate 			break;
1590Sstevel@tonic-gate 		case '?':
1600Sstevel@tonic-gate 			errflag++;
1610Sstevel@tonic-gate 			break;
1620Sstevel@tonic-gate 		default:
1630Sstevel@tonic-gate 			break;
1640Sstevel@tonic-gate 		}
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (errflag) {
1680Sstevel@tonic-gate 		usage(my_prog);
1690Sstevel@tonic-gate 		exit(FAILURE);
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	if (Dflag)
1730Sstevel@tonic-gate 		check_swap();
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	/*
1760Sstevel@tonic-gate 	 * strip command may not take any options.
1770Sstevel@tonic-gate 	 */
1780Sstevel@tonic-gate 	if (my_prog != STRIP) {
1790Sstevel@tonic-gate 		if (argc == optind &&
1800Sstevel@tonic-gate 		    (CHK_OPT(cmd_info, MIGHT_CHG) || CHK_OPT(cmd_info, pFLAG) ||
1810Sstevel@tonic-gate 		    argc == 1))
1820Sstevel@tonic-gate 			usage(my_prog);
1830Sstevel@tonic-gate 		else if (!CHK_OPT(cmd_info, MIGHT_CHG) &&
1840Sstevel@tonic-gate 		    !CHK_OPT(cmd_info, pFLAG) && !CHK_OPT(cmd_info, VFLAG))
1850Sstevel@tonic-gate 			usage(my_prog);
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	/*
1890Sstevel@tonic-gate 	 * This version only allows multiple section names
1900Sstevel@tonic-gate 	 * only for -d option.
1910Sstevel@tonic-gate 	 */
1920Sstevel@tonic-gate 	if ((num_sect >= 2) && (CHK_OPT(cmd_info, pFLAG) ||
1930Sstevel@tonic-gate 	    CHK_OPT(cmd_info, aFLAG) ||
1940Sstevel@tonic-gate 	    CHK_OPT(cmd_info, cFLAG))) {
1950Sstevel@tonic-gate 		error_message(USAGE_ERROR, PLAIN_ERROR, (char *)0,  prog);
196*8223SAli.Bahrami@Sun.COM 		exit(FAILURE);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	/*
2000Sstevel@tonic-gate 	 * If no -n was specified,
2010Sstevel@tonic-gate 	 * set the default, ".comment".
2020Sstevel@tonic-gate 	 * This is for mcs only.
2030Sstevel@tonic-gate 	 */
2040Sstevel@tonic-gate 	if (num_sect == 0 && my_prog == MCS) {
2050Sstevel@tonic-gate 		(void) setup_sectname(".comment", MCS);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	/*
2090Sstevel@tonic-gate 	 * If I am strip command, then add needed
2100Sstevel@tonic-gate 	 * section names.
2110Sstevel@tonic-gate 	 */
2120Sstevel@tonic-gate 	if (my_prog == STRIP) {
2130Sstevel@tonic-gate 		(void) setup_sectname(".line", MCS);
2140Sstevel@tonic-gate 		if (CHK_OPT(cmd_info, lFLAG) == 0) {
2150Sstevel@tonic-gate 			(void) setup_sectname(".debug", STRIP);
2160Sstevel@tonic-gate 			(void) setup_sectname(".stab", STRIP);
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 		if (CHK_OPT(cmd_info, dFLAG) == 0) {
2190Sstevel@tonic-gate 			queue(ACT_DELETE, NULL);
2200Sstevel@tonic-gate 			cmd_info->flags |= MIGHT_CHG;
2210Sstevel@tonic-gate 			cmd_info->flags |= dFLAG;
2220Sstevel@tonic-gate 		}
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	(void) elf_version(EV_NONE);
2260Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
2270Sstevel@tonic-gate 		error_message(ELFVER_ERROR, LIBelf_ERROR, elf_errmsg(-1), prog);
2280Sstevel@tonic-gate 		exit(FAILURE);
2290Sstevel@tonic-gate 	}
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	if (CHK_OPT(cmd_info, pFLAG) || CHK_OPT(cmd_info, MIGHT_CHG)) {
2320Sstevel@tonic-gate 		for (; optind < argc; optind++) {
2330Sstevel@tonic-gate 			error_count = error_count +
2340Sstevel@tonic-gate 			    (each_file(argv[optind], cmd_info));
2350Sstevel@tonic-gate 		}
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	if (Dflag)
2390Sstevel@tonic-gate 		check_swap();
2400Sstevel@tonic-gate 	mcs_exit(error_count);
2410Sstevel@tonic-gate 	/*NOTREACHED*/
2420Sstevel@tonic-gate 	return (0);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * Supplementary functions
2470Sstevel@tonic-gate  */
2480Sstevel@tonic-gate static void
queue(int activity,char * string)2490Sstevel@tonic-gate queue(int activity, char *string)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate 	if (optcnt > optbufsz) {
2520Sstevel@tonic-gate 		optbufsz = optbufsz * 2;
2530Sstevel@tonic-gate 		if ((Action = realloc((struct action *)Action,
2540Sstevel@tonic-gate 		    optbufsz * sizeof (struct action))) == NULL) {
2550Sstevel@tonic-gate 		    error_message(MALLOC_ERROR, PLAIN_ERROR, (char *)0, prog);
2560Sstevel@tonic-gate 		    mcs_exit(FAILURE);
2570Sstevel@tonic-gate 		}
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 	Action[actmax].a_action = activity;
2600Sstevel@tonic-gate 	Action[actmax].a_cnt = 0;
2610Sstevel@tonic-gate 	Action[actmax].a_string = string;
2620Sstevel@tonic-gate 	actmax++;
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
265*8223SAli.Bahrami@Sun.COM /*
266*8223SAli.Bahrami@Sun.COM  * Reset a temporary file descriptor for reuse.
267*8223SAli.Bahrami@Sun.COM  * If the file requires unlinking, that is done first.
268*8223SAli.Bahrami@Sun.COM  */
269*8223SAli.Bahrami@Sun.COM void
free_tempfile(Tmp_File * temp_file)270*8223SAli.Bahrami@Sun.COM free_tempfile(Tmp_File *temp_file)
271*8223SAli.Bahrami@Sun.COM {
272*8223SAli.Bahrami@Sun.COM 	if ((temp_file->tmp_name != NULL) && (temp_file->tmp_unlink))
273*8223SAli.Bahrami@Sun.COM 		(void) unlink(temp_file->tmp_name);
274*8223SAli.Bahrami@Sun.COM 	(void) memset(temp_file, 0, sizeof (*temp_file));
275*8223SAli.Bahrami@Sun.COM }
276*8223SAli.Bahrami@Sun.COM 
2770Sstevel@tonic-gate /*ARGSUSED0*/
2780Sstevel@tonic-gate static void
sigexit(int i)2790Sstevel@tonic-gate sigexit(int i)
2800Sstevel@tonic-gate {
281*8223SAli.Bahrami@Sun.COM 	free_tempfile(&artmpfile);
282*8223SAli.Bahrami@Sun.COM 	free_tempfile(&elftmpfile);
2830Sstevel@tonic-gate 	exit(100);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate static void
usage(int me)2870Sstevel@tonic-gate usage(int me)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate 	if (me == MCS)
2900Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
2910Sstevel@tonic-gate 		"usage: %s [-cdpVz] [-a string] [-n name] file ...\n"), prog);
2920Sstevel@tonic-gate 	else
2930Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
2940Sstevel@tonic-gate 		"usage: %s [-lVx] file ...\n"), prog);
2950Sstevel@tonic-gate 	mcs_exit(FAILURE);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate void
mcs_exit(int val)2990Sstevel@tonic-gate mcs_exit(int val)
3000Sstevel@tonic-gate {
301*8223SAli.Bahrami@Sun.COM 	free_tempfile(&artmpfile);
302*8223SAli.Bahrami@Sun.COM 	free_tempfile(&elftmpfile);
3030Sstevel@tonic-gate 	exit(val);
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate /*
3070Sstevel@tonic-gate  * Insert the section name 'name' into the
3080Sstevel@tonic-gate  * section list.
3090Sstevel@tonic-gate  */
3100Sstevel@tonic-gate static int
setup_sectname(char * name,int whoami)3110Sstevel@tonic-gate setup_sectname(char *name, int whoami)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	S_Name *new;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	/*
3160Sstevel@tonic-gate 	 * Check if the name is already specified or not.
3170Sstevel@tonic-gate 	 */
3180Sstevel@tonic-gate 	if ((whoami == MCS) && (sectcmp(name) == 0))
3190Sstevel@tonic-gate 		return (0);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	/*
3220Sstevel@tonic-gate 	 * Allocate one
3230Sstevel@tonic-gate 	 */
3240Sstevel@tonic-gate 	if ((new = malloc(sizeof (S_Name))) == NULL) {
3250Sstevel@tonic-gate 		error_message(MALLOC_ERROR, PLAIN_ERROR, (char *)0, prog);
3260Sstevel@tonic-gate 		exit(FAILURE);
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 	new->name = strdup(name);
3290Sstevel@tonic-gate 	if (new->name == NULL) {
3300Sstevel@tonic-gate 		error_message(USAGE_ERROR, PLAIN_ERROR, (char *)0, prog);
3310Sstevel@tonic-gate 		exit(FAILURE);
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate 	if (whoami == STRIP)
3340Sstevel@tonic-gate 		new->flags = SNAME_FLG_STRNCMP;
3350Sstevel@tonic-gate 	new->next = NULL;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	/*
3380Sstevel@tonic-gate 	 * Put this one in the list
3390Sstevel@tonic-gate 	 */
3400Sstevel@tonic-gate 	new->next = sect_head;
3410Sstevel@tonic-gate 	sect_head = new;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	return (0);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate /*
3470Sstevel@tonic-gate  * Check if the 'name' exists in the section list.
3480Sstevel@tonic-gate  *
3490Sstevel@tonic-gate  * If found
3500Sstevel@tonic-gate  *	return 0;
3510Sstevel@tonic-gate  * else
3520Sstevel@tonic-gate  *	return 1
3530Sstevel@tonic-gate  */
3540Sstevel@tonic-gate int
sectcmp(char * name)3550Sstevel@tonic-gate sectcmp(char *name)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	/*
3580Sstevel@tonic-gate 	 * Check if the name is already specified or not.
3590Sstevel@tonic-gate 	 */
3600Sstevel@tonic-gate 	if (sect_head != NULL) {
3610Sstevel@tonic-gate 		S_Name *p1 = sect_head;
3620Sstevel@tonic-gate 		while (p1 != NULL) {
3630Sstevel@tonic-gate 			if (p1->flags & SNAME_FLG_STRNCMP) {
3640Sstevel@tonic-gate 				if (strncmp(p1->name,
3650Sstevel@tonic-gate 				    name, strlen(p1->name)) == 0)
3660Sstevel@tonic-gate 					return (0);
3670Sstevel@tonic-gate 			} else if (strcmp(p1->name, name) == 0) {
3680Sstevel@tonic-gate 				return (0);	/* silently ignore */
3690Sstevel@tonic-gate 			}
3700Sstevel@tonic-gate 			p1 = p1->next;
3710Sstevel@tonic-gate 		}
3720Sstevel@tonic-gate 	}
3730Sstevel@tonic-gate 	return (1);
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate static void
check_swap()3770Sstevel@tonic-gate check_swap()
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate 	(void) system("/usr/sbin/swap -s");
3800Sstevel@tonic-gate }
381