xref: /illumos-gate/usr/src/cmd/sgs/mcs/common/mcs.h (revision dd72704bd9e794056c558153663c739e2012d721)
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 2009 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 /*
30  * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
31  */
32 
33 #ifndef	_MCS_H
34 #define	_MCS_H
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, or NULL if unknown */
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	si_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 	APlist	*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	ci_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)->ci_flags & (_y)) != 0)
173 #define	SET_OPT(_x, _y)	((_x)->ci_flags |= (_y))
174 
175 /*
176  * Segment Table
177  */
178 typedef struct seg_table {
179 	GElf_Off	p_offset;
180 	GElf_Xword	p_memsz;
181 	GElf_Xword	p_filesz;
182 } Seg_Table;
183 
184 /*
185  * Temporary files
186  */
187 typedef struct {
188 	const char	*tmp_name;	/* NULL, or name of temp file */
189 	int		tmp_unlink;   /* True if should unlink prior to exit */
190 } Tmp_File;
191 
192 /*
193  * Function prototypes.
194  */
195 int		apply_action(section_info_table *, char *, Cmd_Info *);
196 int		each_file(char *, Cmd_Info *);
197 void		error_message(int, ...);
198 void		mcs_exit(int);
199 int		sectcmp(char *);
200 void		free_tempfile(Tmp_File *);
201 
202 /*
203  * Error messages
204  */
205 #define	MALLOC_ERROR		0
206 #define	USAGE_ERROR		1
207 #define	ELFVER_ERROR		2
208 #define	OPEN_ERROR		3
209 #define	LIBELF_ERROR		4
210 #define	OPEN_TEMP_ERROR		5
211 #define	WRITE_ERROR		6
212 #define	GETARHDR_ERROR		7
213 #define	FILE_TYPE_ERROR		8
214 #define	NOT_MANIPULATED_ERROR	9
215 #define	WRN_MANIPULATED_ERROR	10
216 #define	NO_SECT_TABLE_ERROR	11
217 #define	READ_ERROR		12
218 #define	READ_MANI_ERROR		13
219 #define	WRITE_MANI_ERROR	14
220 #define	LSEEK_MANI_ERROR	15
221 #define	SYM_TAB_AR_ERROR	16
222 #define	EXEC_AR_ERROR		17
223 #define	READ_SYS_ERROR		18
224 #define	OPEN_WRITE_ERROR	19
225 #define	ACT_PRINT_ERROR		20
226 #define	ACT_DELETE1_ERROR	21
227 #define	ACT_DELETE2_ERROR	22
228 #define	ACT_APPEND1_ERROR	23
229 #define	ACT_APPEND2_ERROR	24
230 #define	ACT_COMPRESS1_ERROR	25
231 #define	ACT_COMPRESS2_ERROR	26
232 #define	ACCESS_ERROR		27
233 #define	WRITE_MANI_ERROR2	28
234 
235 #define	PLAIN_ERROR	0
236 #define	LIBelf_ERROR	1
237 #define	SYSTEM_ERROR	2
238 
239 #ifdef	__cplusplus
240 }
241 #endif
242 
243 #endif	/* _MCS_H */
244