xref: /onnv-gate/usr/src/cmd/sgs/mcs/common/mcs.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
24*0Sstevel@tonic-gate  *	  All Rights Reserved
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  *
27*0Sstevel@tonic-gate  *	Copyright 1999-2003 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  *	Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #ifndef	_MCS_H
32*0Sstevel@tonic-gate #define	_MCS_H
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #ifdef	__cplusplus
37*0Sstevel@tonic-gate extern "C" {
38*0Sstevel@tonic-gate #endif
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <stdio.h>
41*0Sstevel@tonic-gate #include <stdlib.h>
42*0Sstevel@tonic-gate #include <stdarg.h>
43*0Sstevel@tonic-gate #include <unistd.h>
44*0Sstevel@tonic-gate #include <libelf.h>
45*0Sstevel@tonic-gate #include <ar.h>
46*0Sstevel@tonic-gate #include <string.h>
47*0Sstevel@tonic-gate #include <signal.h>
48*0Sstevel@tonic-gate #include <sys/stat.h>
49*0Sstevel@tonic-gate #include <fcntl.h>
50*0Sstevel@tonic-gate #include <memory.h>
51*0Sstevel@tonic-gate #include <locale.h>
52*0Sstevel@tonic-gate #include <sys/mman.h>
53*0Sstevel@tonic-gate #include "sgs.h"
54*0Sstevel@tonic-gate #include "gelf.h"
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate #define	FORMAT	"%-16s%-12ld%-6u%-6u%-8o%-10ld%-2s"
57*0Sstevel@tonic-gate #define	ROUNDUP(x)	(((x) + 1) & ~1)
58*0Sstevel@tonic-gate #define	TMPDIR	"/tmp"
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #define	DELETED	-1	/* The section will be removed */
61*0Sstevel@tonic-gate #define	NULLED  -2	/* The section will be nulled */
62*0Sstevel@tonic-gate #define	EXPANDED -3	/* The size of the section expanded */
63*0Sstevel@tonic-gate #define	SHRUNK -4	/* The section shrinked */
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate #define	ACT_NOP		0x00000000
66*0Sstevel@tonic-gate #define	ACT_DELETE	0x00000001
67*0Sstevel@tonic-gate #define	ACT_PRINT	0x00000002
68*0Sstevel@tonic-gate #define	ACT_COMPRESS	0x00000003
69*0Sstevel@tonic-gate #define	ACT_APPEND	0x00000004
70*0Sstevel@tonic-gate #define	ACT_ZAP		0x00000005
71*0Sstevel@tonic-gate #define	SET_ACTION(x, y)	x = (x & 0xfffffff0) | y
72*0Sstevel@tonic-gate #define	GET_ACTION(x)		(x & 0x0000000f)
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate #define	NOSEG 		0x00000010
75*0Sstevel@tonic-gate #define	IN    		0x00000020 /* section is IN a segment */
76*0Sstevel@tonic-gate #define	PRIOR 		0x00000030 /* section is PRIOR to a segment */
77*0Sstevel@tonic-gate #define	AFTER 		0x00000040 /* section is AFTER a segment */
78*0Sstevel@tonic-gate #define	SET_LOC(x, y)	x = (x & 0xffffff0f) | y
79*0Sstevel@tonic-gate #define	GET_LOC(x)	(x & 0x000000f0)
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate #define	CANDIDATE 	0x00000100
82*0Sstevel@tonic-gate #define	MOVING  	0x00000200
83*0Sstevel@tonic-gate #define	MODIFIED  	0x00000400
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate #define	UNSET_CANDIDATE(x)	x = x & ~CANDIDATE
86*0Sstevel@tonic-gate #define	SET_CANDIDATE(x)	x = x | CANDIDATE
87*0Sstevel@tonic-gate #define	ISCANDIDATE(x)		(x & CANDIDATE)
88*0Sstevel@tonic-gate #define	SET_MOVING(x)		x = (x | MOVING)
89*0Sstevel@tonic-gate #define	GET_MOVING(x)		(x & MOVING)
90*0Sstevel@tonic-gate #define	SET_MODIFIED(x)		x = (x | MODIFIED)
91*0Sstevel@tonic-gate #define	GET_MODIFIED(x)		(x & MODIFIED)
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate #define	FAILURE 1
94*0Sstevel@tonic-gate #define	SUCCESS 0
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate #define	DONT_BUILD 3 /* this code is used to prevent building a new file */
97*0Sstevel@tonic-gate 		/* because mcs was given only -p */
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate #define	MCS	1
101*0Sstevel@tonic-gate #define	STRIP	2
102*0Sstevel@tonic-gate #define	STR_STRIP	"strip"
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate /*
105*0Sstevel@tonic-gate  * Structure to hold section information.
106*0Sstevel@tonic-gate  */
107*0Sstevel@tonic-gate typedef struct section_info_table {
108*0Sstevel@tonic-gate 	/*
109*0Sstevel@tonic-gate 	 * Section information.
110*0Sstevel@tonic-gate 	 */
111*0Sstevel@tonic-gate 	Elf_Scn		*scn;		/* Section */
112*0Sstevel@tonic-gate 	Elf_Data	*data;		/* Original data */
113*0Sstevel@tonic-gate 	Elf_Data	*mdata; 	/* Modified data */
114*0Sstevel@tonic-gate 	char		*name;		/* Section name */
115*0Sstevel@tonic-gate 	char		*rel_name;
116*0Sstevel@tonic-gate 	GElf_Shdr	shdr;
117*0Sstevel@tonic-gate 	GElf_Word	secno;		/* The new index */
118*0Sstevel@tonic-gate 	GElf_Word	osecno;		/* The original index */
119*0Sstevel@tonic-gate 	GElf_Word	rel_scn_index;
120*0Sstevel@tonic-gate 	GElf_Xword	flags;
121*0Sstevel@tonic-gate 	GElf_Xword	rel_loc;
122*0Sstevel@tonic-gate } section_info_table;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate /*
125*0Sstevel@tonic-gate  * Structure to hold action information
126*0Sstevel@tonic-gate  */
127*0Sstevel@tonic-gate typedef struct action {
128*0Sstevel@tonic-gate 	int a_action;		/* Which action to take ? */
129*0Sstevel@tonic-gate 	int a_cnt;		/* Am I applied ? */
130*0Sstevel@tonic-gate 	char *a_string;		/* The string to be added. */
131*0Sstevel@tonic-gate } action;
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate /*
134*0Sstevel@tonic-gate  * Structure to hold the section names specified.
135*0Sstevel@tonic-gate  */
136*0Sstevel@tonic-gate typedef struct s_name {
137*0Sstevel@tonic-gate 	char 		*name;
138*0Sstevel@tonic-gate 	struct s_name	*next;
139*0Sstevel@tonic-gate 	unsigned char	flags;
140*0Sstevel@tonic-gate } S_Name;
141*0Sstevel@tonic-gate #define	SECT_NAME	sect_head->name
142*0Sstevel@tonic-gate #define	SNAME_FLG_STRNCMP	0x01	/* Use strncmp() instead of strcmp() */
143*0Sstevel@tonic-gate 					/* for section name comparison. */
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate /*
146*0Sstevel@tonic-gate  * Structure to hold command information
147*0Sstevel@tonic-gate  */
148*0Sstevel@tonic-gate typedef struct cmd_info {
149*0Sstevel@tonic-gate 	List	sh_groups;	/* List of SHT_GROUP sections */
150*0Sstevel@tonic-gate 	int 	no_of_append;
151*0Sstevel@tonic-gate 	int	no_of_delete;
152*0Sstevel@tonic-gate 	int	no_of_nulled;
153*0Sstevel@tonic-gate 	int	no_of_compressed;
154*0Sstevel@tonic-gate 	int	no_of_moved;
155*0Sstevel@tonic-gate 	size_t	str_size;	/* size of string to be appended */
156*0Sstevel@tonic-gate 	int	flags;		/* Various flags */
157*0Sstevel@tonic-gate } Cmd_Info;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate #define	MIGHT_CHG	0x0001
160*0Sstevel@tonic-gate #define	aFLAG		0x0002
161*0Sstevel@tonic-gate #define	cFLAG		0x0004
162*0Sstevel@tonic-gate #define	dFLAG		0x0008
163*0Sstevel@tonic-gate #define	lFLAG		0x0010
164*0Sstevel@tonic-gate #define	pFLAG		0x0020
165*0Sstevel@tonic-gate #define	xFLAG		0x0040
166*0Sstevel@tonic-gate #define	VFLAG		0x0080
167*0Sstevel@tonic-gate #define	zFLAG		0x0100
168*0Sstevel@tonic-gate #define	I_AM_STRIP	0x0200
169*0Sstevel@tonic-gate #define	SHF_GROUP_MOVE	0x0400	/* SHF_GROUP section moves */
170*0Sstevel@tonic-gate #define	SHF_GROUP_DEL	0x0800	/* SHF_GROUP section deleted */
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate #define	CHK_OPT(_x, _y)	(_x->flags & _y)
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate /*
175*0Sstevel@tonic-gate  * Segment Table
176*0Sstevel@tonic-gate  */
177*0Sstevel@tonic-gate typedef struct seg_table {
178*0Sstevel@tonic-gate 	GElf_Off	p_offset;
179*0Sstevel@tonic-gate 	GElf_Xword	p_memsz;
180*0Sstevel@tonic-gate 	GElf_Xword	p_filesz;
181*0Sstevel@tonic-gate } Seg_Table;
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate /*
184*0Sstevel@tonic-gate  * Function prototypes.
185*0Sstevel@tonic-gate  */
186*0Sstevel@tonic-gate int		apply_action(section_info_table *, char *, Cmd_Info *);
187*0Sstevel@tonic-gate int		each_file(char *, Cmd_Info *);
188*0Sstevel@tonic-gate void		error_message(int, ...);
189*0Sstevel@tonic-gate void		mcs_exit(int);
190*0Sstevel@tonic-gate Listnode *	list_appendc(List *, const void*);
191*0Sstevel@tonic-gate int		sectcmp(char *);
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate /*
194*0Sstevel@tonic-gate  * Error messages
195*0Sstevel@tonic-gate  */
196*0Sstevel@tonic-gate #define	MALLOC_ERROR		0
197*0Sstevel@tonic-gate #define	USAGE_ERROR		1
198*0Sstevel@tonic-gate #define	ELFVER_ERROR		2
199*0Sstevel@tonic-gate #define	OPEN_ERROR		3
200*0Sstevel@tonic-gate #define	LIBELF_ERROR		4
201*0Sstevel@tonic-gate #define	OPEN_TEMP_ERROR		5
202*0Sstevel@tonic-gate #define	WRITE_ERROR		6
203*0Sstevel@tonic-gate #define	GETARHDR_ERROR		7
204*0Sstevel@tonic-gate #define	FILE_TYPE_ERROR		8
205*0Sstevel@tonic-gate #define	NOT_MANIPULATED_ERROR	9
206*0Sstevel@tonic-gate #define	WRN_MANIPULATED_ERROR 	10
207*0Sstevel@tonic-gate #define	NO_SECT_TABLE_ERROR 	11
208*0Sstevel@tonic-gate #define	READ_ERROR		12
209*0Sstevel@tonic-gate #define	READ_MANI_ERROR		13
210*0Sstevel@tonic-gate #define	WRITE_MANI_ERROR	14
211*0Sstevel@tonic-gate #define	LSEEK_MANI_ERROR	15
212*0Sstevel@tonic-gate #define	SYM_TAB_AR_ERROR	16
213*0Sstevel@tonic-gate #define	EXEC_AR_ERROR		17
214*0Sstevel@tonic-gate #define	READ_SYS_ERROR		18
215*0Sstevel@tonic-gate #define	OPEN_WRITE_ERROR	19
216*0Sstevel@tonic-gate #define	ACT_PRINT_ERROR		20
217*0Sstevel@tonic-gate #define	ACT_DELETE1_ERROR	21
218*0Sstevel@tonic-gate #define	ACT_DELETE2_ERROR	22
219*0Sstevel@tonic-gate #define	ACT_APPEND1_ERROR	23
220*0Sstevel@tonic-gate #define	ACT_APPEND2_ERROR	24
221*0Sstevel@tonic-gate #define	ACT_COMPRESS1_ERROR	25
222*0Sstevel@tonic-gate #define	ACT_COMPRESS2_ERROR	26
223*0Sstevel@tonic-gate #define	ACCESS_ERROR		27
224*0Sstevel@tonic-gate #define	WRITE_MANI_ERROR2	28
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate #define	PLAIN_ERROR	0
227*0Sstevel@tonic-gate #define	LIBelf_ERROR	1
228*0Sstevel@tonic-gate #define	SYSTEM_ERROR	2
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate #ifdef	__cplusplus
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate #endif
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate #endif	/* _MCS_H */
235