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