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