xref: /illumos-gate/usr/src/cmd/sgs/mcs/common/mcs.h (revision 57ef7aa9)
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
50d15df17Sab196087  * Common Development and Distribution License (the "License").
60d15df17Sab196087  * 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*57ef7aa9SRod Evans  * Copyright 2009 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 #ifdef	__cplusplus
347c478bd9Sstevel@tonic-gate extern "C" {
357c478bd9Sstevel@tonic-gate #endif
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <stdarg.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <libelf.h>
427c478bd9Sstevel@tonic-gate #include <ar.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <signal.h>
457c478bd9Sstevel@tonic-gate #include <sys/stat.h>
467c478bd9Sstevel@tonic-gate #include <fcntl.h>
477c478bd9Sstevel@tonic-gate #include <memory.h>
487c478bd9Sstevel@tonic-gate #include <locale.h>
497c478bd9Sstevel@tonic-gate #include <sys/mman.h>
507c478bd9Sstevel@tonic-gate #include "sgs.h"
517c478bd9Sstevel@tonic-gate #include "gelf.h"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	FORMAT	"%-16s%-12ld%-6u%-6u%-8o%-10ld%-2s"
547c478bd9Sstevel@tonic-gate #define	ROUNDUP(x)	(((x) + 1) & ~1)
557c478bd9Sstevel@tonic-gate #define	TMPDIR	"/tmp"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	DELETED	-1	/* The section will be removed */
587c478bd9Sstevel@tonic-gate #define	NULLED  -2	/* The section will be nulled */
597c478bd9Sstevel@tonic-gate #define	EXPANDED -3	/* The size of the section expanded */
607c478bd9Sstevel@tonic-gate #define	SHRUNK -4	/* The section shrinked */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define	ACT_NOP		0x00000000
637c478bd9Sstevel@tonic-gate #define	ACT_DELETE	0x00000001
647c478bd9Sstevel@tonic-gate #define	ACT_PRINT	0x00000002
657c478bd9Sstevel@tonic-gate #define	ACT_COMPRESS	0x00000003
667c478bd9Sstevel@tonic-gate #define	ACT_APPEND	0x00000004
677c478bd9Sstevel@tonic-gate #define	ACT_ZAP		0x00000005
687c478bd9Sstevel@tonic-gate #define	SET_ACTION(x, y)	x = (x & 0xfffffff0) | y
697c478bd9Sstevel@tonic-gate #define	GET_ACTION(x)		(x & 0x0000000f)
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #define	NOSEG 		0x00000010
727c478bd9Sstevel@tonic-gate #define	IN    		0x00000020 /* section is IN a segment */
737c478bd9Sstevel@tonic-gate #define	PRIOR 		0x00000030 /* section is PRIOR to a segment */
747c478bd9Sstevel@tonic-gate #define	AFTER 		0x00000040 /* section is AFTER a segment */
757c478bd9Sstevel@tonic-gate #define	SET_LOC(x, y)	x = (x & 0xffffff0f) | y
767c478bd9Sstevel@tonic-gate #define	GET_LOC(x)	(x & 0x000000f0)
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	CANDIDATE 	0x00000100
797c478bd9Sstevel@tonic-gate #define	MOVING  	0x00000200
807c478bd9Sstevel@tonic-gate #define	MODIFIED  	0x00000400
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	UNSET_CANDIDATE(x)	x = x & ~CANDIDATE
837c478bd9Sstevel@tonic-gate #define	SET_CANDIDATE(x)	x = x | CANDIDATE
847c478bd9Sstevel@tonic-gate #define	ISCANDIDATE(x)		(x & CANDIDATE)
857c478bd9Sstevel@tonic-gate #define	SET_MOVING(x)		x = (x | MOVING)
867c478bd9Sstevel@tonic-gate #define	GET_MOVING(x)		(x & MOVING)
877c478bd9Sstevel@tonic-gate #define	SET_MODIFIED(x)		x = (x | MODIFIED)
887c478bd9Sstevel@tonic-gate #define	GET_MODIFIED(x)		(x & MODIFIED)
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #define	FAILURE 1
917c478bd9Sstevel@tonic-gate #define	SUCCESS 0
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #define	DONT_BUILD 3 /* this code is used to prevent building a new file */
947c478bd9Sstevel@tonic-gate 		/* because mcs was given only -p */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #define	MCS	1
987c478bd9Sstevel@tonic-gate #define	STRIP	2
997c478bd9Sstevel@tonic-gate #define	STR_STRIP	"strip"
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * Structure to hold section information.
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate typedef struct section_info_table {
1057c478bd9Sstevel@tonic-gate 	/*
1067c478bd9Sstevel@tonic-gate 	 * Section information.
1077c478bd9Sstevel@tonic-gate 	 */
1087c478bd9Sstevel@tonic-gate 	Elf_Scn		*scn;		/* Section */
1097c478bd9Sstevel@tonic-gate 	Elf_Data	*data;		/* Original data */
1107c478bd9Sstevel@tonic-gate 	Elf_Data	*mdata; 	/* Modified data */
1110d15df17Sab196087 	char		*name;		/* Section name, or NULL if unknown */
1127c478bd9Sstevel@tonic-gate 	char		*rel_name;
1137c478bd9Sstevel@tonic-gate 	GElf_Shdr	shdr;
1147c478bd9Sstevel@tonic-gate 	GElf_Word	secno;		/* The new index */
1157c478bd9Sstevel@tonic-gate 	GElf_Word	osecno;		/* The original index */
1167c478bd9Sstevel@tonic-gate 	GElf_Word	rel_scn_index;
1177c478bd9Sstevel@tonic-gate 	GElf_Xword	flags;
1187c478bd9Sstevel@tonic-gate 	GElf_Xword	rel_loc;
1197c478bd9Sstevel@tonic-gate } section_info_table;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * Structure to hold action information
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate typedef struct action {
1257c478bd9Sstevel@tonic-gate 	int a_action;		/* Which action to take ? */
1267c478bd9Sstevel@tonic-gate 	int a_cnt;		/* Am I applied ? */
1277c478bd9Sstevel@tonic-gate 	char *a_string;		/* The string to be added. */
1287c478bd9Sstevel@tonic-gate } action;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  * Structure to hold the section names specified.
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate typedef struct s_name {
1347c478bd9Sstevel@tonic-gate 	char 		*name;
1357c478bd9Sstevel@tonic-gate 	struct s_name	*next;
1367c478bd9Sstevel@tonic-gate 	unsigned char	flags;
1377c478bd9Sstevel@tonic-gate } S_Name;
1387c478bd9Sstevel@tonic-gate #define	SECT_NAME	sect_head->name
1397c478bd9Sstevel@tonic-gate #define	SNAME_FLG_STRNCMP	0x01	/* Use strncmp() instead of strcmp() */
1407c478bd9Sstevel@tonic-gate 					/* for section name comparison. */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * Structure to hold command information
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate typedef struct cmd_info {
146*57ef7aa9SRod Evans 	APlist	*sh_groups;	/* list of SHT_GROUP sections */
1477c478bd9Sstevel@tonic-gate 	int 	no_of_append;
1487c478bd9Sstevel@tonic-gate 	int	no_of_delete;
1497c478bd9Sstevel@tonic-gate 	int	no_of_nulled;
1507c478bd9Sstevel@tonic-gate 	int	no_of_compressed;
1517c478bd9Sstevel@tonic-gate 	int	no_of_moved;
1527c478bd9Sstevel@tonic-gate 	size_t	str_size;	/* size of string to be appended */
1537c478bd9Sstevel@tonic-gate 	int	flags;		/* Various flags */
1547c478bd9Sstevel@tonic-gate } Cmd_Info;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate #define	MIGHT_CHG	0x0001
1577c478bd9Sstevel@tonic-gate #define	aFLAG		0x0002
1587c478bd9Sstevel@tonic-gate #define	cFLAG		0x0004
1597c478bd9Sstevel@tonic-gate #define	dFLAG		0x0008
1607c478bd9Sstevel@tonic-gate #define	lFLAG		0x0010
1617c478bd9Sstevel@tonic-gate #define	pFLAG		0x0020
1627c478bd9Sstevel@tonic-gate #define	xFLAG		0x0040
1637c478bd9Sstevel@tonic-gate #define	VFLAG		0x0080
1647c478bd9Sstevel@tonic-gate #define	zFLAG		0x0100
1657c478bd9Sstevel@tonic-gate #define	I_AM_STRIP	0x0200
1667c478bd9Sstevel@tonic-gate #define	SHF_GROUP_MOVE	0x0400	/* SHF_GROUP section moves */
1677c478bd9Sstevel@tonic-gate #define	SHF_GROUP_DEL	0x0800	/* SHF_GROUP section deleted */
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate #define	CHK_OPT(_x, _y)	(_x->flags & _y)
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * Segment Table
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate typedef struct seg_table {
1757c478bd9Sstevel@tonic-gate 	GElf_Off	p_offset;
1767c478bd9Sstevel@tonic-gate 	GElf_Xword	p_memsz;
1777c478bd9Sstevel@tonic-gate 	GElf_Xword	p_filesz;
1787c478bd9Sstevel@tonic-gate } Seg_Table;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
181d1477c77SAli Bahrami  * Temporary files
182d1477c77SAli Bahrami  */
183d1477c77SAli Bahrami typedef struct {
184d1477c77SAli Bahrami 	const char	*tmp_name;	/* NULL, or name of temp file */
185d1477c77SAli Bahrami 	int		tmp_unlink;   /* True if should unlink prior to exit */
186d1477c77SAli Bahrami } Tmp_File;
187d1477c77SAli Bahrami 
188d1477c77SAli Bahrami /*
1897c478bd9Sstevel@tonic-gate  * Function prototypes.
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate int		apply_action(section_info_table *, char *, Cmd_Info *);
1927c478bd9Sstevel@tonic-gate int		each_file(char *, Cmd_Info *);
1937c478bd9Sstevel@tonic-gate void		error_message(int, ...);
1947c478bd9Sstevel@tonic-gate void		mcs_exit(int);
1957c478bd9Sstevel@tonic-gate int		sectcmp(char *);
196d1477c77SAli Bahrami void		free_tempfile(Tmp_File *);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * Error messages
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate #define	MALLOC_ERROR		0
2027c478bd9Sstevel@tonic-gate #define	USAGE_ERROR		1
2037c478bd9Sstevel@tonic-gate #define	ELFVER_ERROR		2
2047c478bd9Sstevel@tonic-gate #define	OPEN_ERROR		3
2057c478bd9Sstevel@tonic-gate #define	LIBELF_ERROR		4
2067c478bd9Sstevel@tonic-gate #define	OPEN_TEMP_ERROR		5
2077c478bd9Sstevel@tonic-gate #define	WRITE_ERROR		6
2087c478bd9Sstevel@tonic-gate #define	GETARHDR_ERROR		7
2097c478bd9Sstevel@tonic-gate #define	FILE_TYPE_ERROR		8
2107c478bd9Sstevel@tonic-gate #define	NOT_MANIPULATED_ERROR	9
2117c478bd9Sstevel@tonic-gate #define	WRN_MANIPULATED_ERROR 	10
2127c478bd9Sstevel@tonic-gate #define	NO_SECT_TABLE_ERROR 	11
2137c478bd9Sstevel@tonic-gate #define	READ_ERROR		12
2147c478bd9Sstevel@tonic-gate #define	READ_MANI_ERROR		13
2157c478bd9Sstevel@tonic-gate #define	WRITE_MANI_ERROR	14
2167c478bd9Sstevel@tonic-gate #define	LSEEK_MANI_ERROR	15
2177c478bd9Sstevel@tonic-gate #define	SYM_TAB_AR_ERROR	16
2187c478bd9Sstevel@tonic-gate #define	EXEC_AR_ERROR		17
2197c478bd9Sstevel@tonic-gate #define	READ_SYS_ERROR		18
2207c478bd9Sstevel@tonic-gate #define	OPEN_WRITE_ERROR	19
2217c478bd9Sstevel@tonic-gate #define	ACT_PRINT_ERROR		20
2227c478bd9Sstevel@tonic-gate #define	ACT_DELETE1_ERROR	21
2237c478bd9Sstevel@tonic-gate #define	ACT_DELETE2_ERROR	22
2247c478bd9Sstevel@tonic-gate #define	ACT_APPEND1_ERROR	23
2257c478bd9Sstevel@tonic-gate #define	ACT_APPEND2_ERROR	24
2267c478bd9Sstevel@tonic-gate #define	ACT_COMPRESS1_ERROR	25
2277c478bd9Sstevel@tonic-gate #define	ACT_COMPRESS2_ERROR	26
2287c478bd9Sstevel@tonic-gate #define	ACCESS_ERROR		27
2297c478bd9Sstevel@tonic-gate #define	WRITE_MANI_ERROR2	28
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate #define	PLAIN_ERROR	0
2327c478bd9Sstevel@tonic-gate #define	LIBelf_ERROR	1
2337c478bd9Sstevel@tonic-gate #define	SYSTEM_ERROR	2
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate #endif
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate #endif	/* _MCS_H */
240