xref: /illumos-gate/usr/src/cmd/sgs/mcs/common/mcs.h (revision 0d15df175afb959aa4b364918e3a8b950114dbb5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0d15df17Sab196087  * Common Development and Distribution License (the "License").
6*0d15df17Sab196087  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
237c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  *
26*0d15df17Sab196087  *	Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  *	Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #ifndef	_MCS_H
317c478bd9Sstevel@tonic-gate #define	_MCS_H
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
367c478bd9Sstevel@tonic-gate extern "C" {
377c478bd9Sstevel@tonic-gate #endif
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <stdarg.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <libelf.h>
447c478bd9Sstevel@tonic-gate #include <ar.h>
457c478bd9Sstevel@tonic-gate #include <string.h>
467c478bd9Sstevel@tonic-gate #include <signal.h>
477c478bd9Sstevel@tonic-gate #include <sys/stat.h>
487c478bd9Sstevel@tonic-gate #include <fcntl.h>
497c478bd9Sstevel@tonic-gate #include <memory.h>
507c478bd9Sstevel@tonic-gate #include <locale.h>
517c478bd9Sstevel@tonic-gate #include <sys/mman.h>
527c478bd9Sstevel@tonic-gate #include "sgs.h"
537c478bd9Sstevel@tonic-gate #include "gelf.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	FORMAT	"%-16s%-12ld%-6u%-6u%-8o%-10ld%-2s"
567c478bd9Sstevel@tonic-gate #define	ROUNDUP(x)	(((x) + 1) & ~1)
577c478bd9Sstevel@tonic-gate #define	TMPDIR	"/tmp"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	DELETED	-1	/* The section will be removed */
607c478bd9Sstevel@tonic-gate #define	NULLED  -2	/* The section will be nulled */
617c478bd9Sstevel@tonic-gate #define	EXPANDED -3	/* The size of the section expanded */
627c478bd9Sstevel@tonic-gate #define	SHRUNK -4	/* The section shrinked */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define	ACT_NOP		0x00000000
657c478bd9Sstevel@tonic-gate #define	ACT_DELETE	0x00000001
667c478bd9Sstevel@tonic-gate #define	ACT_PRINT	0x00000002
677c478bd9Sstevel@tonic-gate #define	ACT_COMPRESS	0x00000003
687c478bd9Sstevel@tonic-gate #define	ACT_APPEND	0x00000004
697c478bd9Sstevel@tonic-gate #define	ACT_ZAP		0x00000005
707c478bd9Sstevel@tonic-gate #define	SET_ACTION(x, y)	x = (x & 0xfffffff0) | y
717c478bd9Sstevel@tonic-gate #define	GET_ACTION(x)		(x & 0x0000000f)
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	NOSEG 		0x00000010
747c478bd9Sstevel@tonic-gate #define	IN    		0x00000020 /* section is IN a segment */
757c478bd9Sstevel@tonic-gate #define	PRIOR 		0x00000030 /* section is PRIOR to a segment */
767c478bd9Sstevel@tonic-gate #define	AFTER 		0x00000040 /* section is AFTER a segment */
777c478bd9Sstevel@tonic-gate #define	SET_LOC(x, y)	x = (x & 0xffffff0f) | y
787c478bd9Sstevel@tonic-gate #define	GET_LOC(x)	(x & 0x000000f0)
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #define	CANDIDATE 	0x00000100
817c478bd9Sstevel@tonic-gate #define	MOVING  	0x00000200
827c478bd9Sstevel@tonic-gate #define	MODIFIED  	0x00000400
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define	UNSET_CANDIDATE(x)	x = x & ~CANDIDATE
857c478bd9Sstevel@tonic-gate #define	SET_CANDIDATE(x)	x = x | CANDIDATE
867c478bd9Sstevel@tonic-gate #define	ISCANDIDATE(x)		(x & CANDIDATE)
877c478bd9Sstevel@tonic-gate #define	SET_MOVING(x)		x = (x | MOVING)
887c478bd9Sstevel@tonic-gate #define	GET_MOVING(x)		(x & MOVING)
897c478bd9Sstevel@tonic-gate #define	SET_MODIFIED(x)		x = (x | MODIFIED)
907c478bd9Sstevel@tonic-gate #define	GET_MODIFIED(x)		(x & MODIFIED)
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #define	FAILURE 1
937c478bd9Sstevel@tonic-gate #define	SUCCESS 0
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #define	DONT_BUILD 3 /* this code is used to prevent building a new file */
967c478bd9Sstevel@tonic-gate 		/* because mcs was given only -p */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #define	MCS	1
1007c478bd9Sstevel@tonic-gate #define	STRIP	2
1017c478bd9Sstevel@tonic-gate #define	STR_STRIP	"strip"
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * Structure to hold section information.
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate typedef struct section_info_table {
1077c478bd9Sstevel@tonic-gate 	/*
1087c478bd9Sstevel@tonic-gate 	 * Section information.
1097c478bd9Sstevel@tonic-gate 	 */
1107c478bd9Sstevel@tonic-gate 	Elf_Scn		*scn;		/* Section */
1117c478bd9Sstevel@tonic-gate 	Elf_Data	*data;		/* Original data */
1127c478bd9Sstevel@tonic-gate 	Elf_Data	*mdata; 	/* Modified data */
113*0d15df17Sab196087 	char		*name;		/* Section name, or NULL if unknown */
1147c478bd9Sstevel@tonic-gate 	char		*rel_name;
1157c478bd9Sstevel@tonic-gate 	GElf_Shdr	shdr;
1167c478bd9Sstevel@tonic-gate 	GElf_Word	secno;		/* The new index */
1177c478bd9Sstevel@tonic-gate 	GElf_Word	osecno;		/* The original index */
1187c478bd9Sstevel@tonic-gate 	GElf_Word	rel_scn_index;
1197c478bd9Sstevel@tonic-gate 	GElf_Xword	flags;
1207c478bd9Sstevel@tonic-gate 	GElf_Xword	rel_loc;
1217c478bd9Sstevel@tonic-gate } section_info_table;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Structure to hold action information
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate typedef struct action {
1277c478bd9Sstevel@tonic-gate 	int a_action;		/* Which action to take ? */
1287c478bd9Sstevel@tonic-gate 	int a_cnt;		/* Am I applied ? */
1297c478bd9Sstevel@tonic-gate 	char *a_string;		/* The string to be added. */
1307c478bd9Sstevel@tonic-gate } action;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * Structure to hold the section names specified.
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate typedef struct s_name {
1367c478bd9Sstevel@tonic-gate 	char 		*name;
1377c478bd9Sstevel@tonic-gate 	struct s_name	*next;
1387c478bd9Sstevel@tonic-gate 	unsigned char	flags;
1397c478bd9Sstevel@tonic-gate } S_Name;
1407c478bd9Sstevel@tonic-gate #define	SECT_NAME	sect_head->name
1417c478bd9Sstevel@tonic-gate #define	SNAME_FLG_STRNCMP	0x01	/* Use strncmp() instead of strcmp() */
1427c478bd9Sstevel@tonic-gate 					/* for section name comparison. */
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate  * Structure to hold command information
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate typedef struct cmd_info {
1487c478bd9Sstevel@tonic-gate 	List	sh_groups;	/* List of SHT_GROUP sections */
1497c478bd9Sstevel@tonic-gate 	int 	no_of_append;
1507c478bd9Sstevel@tonic-gate 	int	no_of_delete;
1517c478bd9Sstevel@tonic-gate 	int	no_of_nulled;
1527c478bd9Sstevel@tonic-gate 	int	no_of_compressed;
1537c478bd9Sstevel@tonic-gate 	int	no_of_moved;
1547c478bd9Sstevel@tonic-gate 	size_t	str_size;	/* size of string to be appended */
1557c478bd9Sstevel@tonic-gate 	int	flags;		/* Various flags */
1567c478bd9Sstevel@tonic-gate } Cmd_Info;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate #define	MIGHT_CHG	0x0001
1597c478bd9Sstevel@tonic-gate #define	aFLAG		0x0002
1607c478bd9Sstevel@tonic-gate #define	cFLAG		0x0004
1617c478bd9Sstevel@tonic-gate #define	dFLAG		0x0008
1627c478bd9Sstevel@tonic-gate #define	lFLAG		0x0010
1637c478bd9Sstevel@tonic-gate #define	pFLAG		0x0020
1647c478bd9Sstevel@tonic-gate #define	xFLAG		0x0040
1657c478bd9Sstevel@tonic-gate #define	VFLAG		0x0080
1667c478bd9Sstevel@tonic-gate #define	zFLAG		0x0100
1677c478bd9Sstevel@tonic-gate #define	I_AM_STRIP	0x0200
1687c478bd9Sstevel@tonic-gate #define	SHF_GROUP_MOVE	0x0400	/* SHF_GROUP section moves */
1697c478bd9Sstevel@tonic-gate #define	SHF_GROUP_DEL	0x0800	/* SHF_GROUP section deleted */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate #define	CHK_OPT(_x, _y)	(_x->flags & _y)
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate  * Segment Table
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate typedef struct seg_table {
1777c478bd9Sstevel@tonic-gate 	GElf_Off	p_offset;
1787c478bd9Sstevel@tonic-gate 	GElf_Xword	p_memsz;
1797c478bd9Sstevel@tonic-gate 	GElf_Xword	p_filesz;
1807c478bd9Sstevel@tonic-gate } Seg_Table;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  * Function prototypes.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate int		apply_action(section_info_table *, char *, Cmd_Info *);
1867c478bd9Sstevel@tonic-gate int		each_file(char *, Cmd_Info *);
1877c478bd9Sstevel@tonic-gate void		error_message(int, ...);
1887c478bd9Sstevel@tonic-gate void		mcs_exit(int);
1897c478bd9Sstevel@tonic-gate Listnode *	list_appendc(List *, const void*);
1907c478bd9Sstevel@tonic-gate int		sectcmp(char *);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /*
1937c478bd9Sstevel@tonic-gate  * Error messages
1947c478bd9Sstevel@tonic-gate  */
1957c478bd9Sstevel@tonic-gate #define	MALLOC_ERROR		0
1967c478bd9Sstevel@tonic-gate #define	USAGE_ERROR		1
1977c478bd9Sstevel@tonic-gate #define	ELFVER_ERROR		2
1987c478bd9Sstevel@tonic-gate #define	OPEN_ERROR		3
1997c478bd9Sstevel@tonic-gate #define	LIBELF_ERROR		4
2007c478bd9Sstevel@tonic-gate #define	OPEN_TEMP_ERROR		5
2017c478bd9Sstevel@tonic-gate #define	WRITE_ERROR		6
2027c478bd9Sstevel@tonic-gate #define	GETARHDR_ERROR		7
2037c478bd9Sstevel@tonic-gate #define	FILE_TYPE_ERROR		8
2047c478bd9Sstevel@tonic-gate #define	NOT_MANIPULATED_ERROR	9
2057c478bd9Sstevel@tonic-gate #define	WRN_MANIPULATED_ERROR 	10
2067c478bd9Sstevel@tonic-gate #define	NO_SECT_TABLE_ERROR 	11
2077c478bd9Sstevel@tonic-gate #define	READ_ERROR		12
2087c478bd9Sstevel@tonic-gate #define	READ_MANI_ERROR		13
2097c478bd9Sstevel@tonic-gate #define	WRITE_MANI_ERROR	14
2107c478bd9Sstevel@tonic-gate #define	LSEEK_MANI_ERROR	15
2117c478bd9Sstevel@tonic-gate #define	SYM_TAB_AR_ERROR	16
2127c478bd9Sstevel@tonic-gate #define	EXEC_AR_ERROR		17
2137c478bd9Sstevel@tonic-gate #define	READ_SYS_ERROR		18
2147c478bd9Sstevel@tonic-gate #define	OPEN_WRITE_ERROR	19
2157c478bd9Sstevel@tonic-gate #define	ACT_PRINT_ERROR		20
2167c478bd9Sstevel@tonic-gate #define	ACT_DELETE1_ERROR	21
2177c478bd9Sstevel@tonic-gate #define	ACT_DELETE2_ERROR	22
2187c478bd9Sstevel@tonic-gate #define	ACT_APPEND1_ERROR	23
2197c478bd9Sstevel@tonic-gate #define	ACT_APPEND2_ERROR	24
2207c478bd9Sstevel@tonic-gate #define	ACT_COMPRESS1_ERROR	25
2217c478bd9Sstevel@tonic-gate #define	ACT_COMPRESS2_ERROR	26
2227c478bd9Sstevel@tonic-gate #define	ACCESS_ERROR		27
2237c478bd9Sstevel@tonic-gate #define	WRITE_MANI_ERROR2	28
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate #define	PLAIN_ERROR	0
2267c478bd9Sstevel@tonic-gate #define	LIBelf_ERROR	1
2277c478bd9Sstevel@tonic-gate #define	SYSTEM_ERROR	2
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate #endif
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate #endif	/* _MCS_H */
234