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