xref: /illumos-gate/usr/src/cmd/sgs/mcs/common/mcs.h (revision 56b75c05d0c84c701fbb1eb2a572b0ecee66f012)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50d15df17Sab196087  * Common Development and Distribution License (the "License").
60d15df17Sab196087  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
237c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  *
2657ef7aa9SRod Evans  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
29*56b75c05SAndy Fiddaman /*
30*56b75c05SAndy Fiddaman  * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
31*56b75c05SAndy Fiddaman  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #ifndef	_MCS_H
347c478bd9Sstevel@tonic-gate #define	_MCS_H
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
377c478bd9Sstevel@tonic-gate extern "C" {
387c478bd9Sstevel@tonic-gate #endif
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <stdarg.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <libelf.h>
457c478bd9Sstevel@tonic-gate #include <ar.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate #include <signal.h>
487c478bd9Sstevel@tonic-gate #include <sys/stat.h>
497c478bd9Sstevel@tonic-gate #include <fcntl.h>
507c478bd9Sstevel@tonic-gate #include <memory.h>
517c478bd9Sstevel@tonic-gate #include <locale.h>
527c478bd9Sstevel@tonic-gate #include <sys/mman.h>
537c478bd9Sstevel@tonic-gate #include "sgs.h"
547c478bd9Sstevel@tonic-gate #include "gelf.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	FORMAT	"%-16s%-12ld%-6u%-6u%-8o%-10ld%-2s"
577c478bd9Sstevel@tonic-gate #define	ROUNDUP(x)	(((x) + 1) & ~1)
587c478bd9Sstevel@tonic-gate #define	TMPDIR	"/tmp"
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	DELETED	-1	/* The section will be removed */
617c478bd9Sstevel@tonic-gate #define	NULLED  -2	/* The section will be nulled */
627c478bd9Sstevel@tonic-gate #define	EXPANDED -3	/* The size of the section expanded */
637c478bd9Sstevel@tonic-gate #define	SHRUNK -4	/* The section shrinked */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	ACT_NOP		0x00000000
667c478bd9Sstevel@tonic-gate #define	ACT_DELETE	0x00000001
677c478bd9Sstevel@tonic-gate #define	ACT_PRINT	0x00000002
687c478bd9Sstevel@tonic-gate #define	ACT_COMPRESS	0x00000003
697c478bd9Sstevel@tonic-gate #define	ACT_APPEND	0x00000004
707c478bd9Sstevel@tonic-gate #define	ACT_ZAP		0x00000005
717c478bd9Sstevel@tonic-gate #define	SET_ACTION(x, y)	x = (x & 0xfffffff0) | y
727c478bd9Sstevel@tonic-gate #define	GET_ACTION(x)		(x & 0x0000000f)
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #define	NOSEG		0x00000010
757c478bd9Sstevel@tonic-gate #define	IN		0x00000020 /* section is IN a segment */
767c478bd9Sstevel@tonic-gate #define	PRIOR		0x00000030 /* section is PRIOR to a segment */
777c478bd9Sstevel@tonic-gate #define	AFTER		0x00000040 /* section is AFTER a segment */
787c478bd9Sstevel@tonic-gate #define	SET_LOC(x, y)	x = (x & 0xffffff0f) | y
797c478bd9Sstevel@tonic-gate #define	GET_LOC(x)	(x & 0x000000f0)
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #define	CANDIDATE	0x00000100
827c478bd9Sstevel@tonic-gate #define	MOVING		0x00000200
837c478bd9Sstevel@tonic-gate #define	MODIFIED	0x00000400
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #define	UNSET_CANDIDATE(x)	x = x & ~CANDIDATE
867c478bd9Sstevel@tonic-gate #define	SET_CANDIDATE(x)	x = x | CANDIDATE
877c478bd9Sstevel@tonic-gate #define	ISCANDIDATE(x)		(x & CANDIDATE)
887c478bd9Sstevel@tonic-gate #define	SET_MOVING(x)		x = (x | MOVING)
897c478bd9Sstevel@tonic-gate #define	GET_MOVING(x)		(x & MOVING)
907c478bd9Sstevel@tonic-gate #define	SET_MODIFIED(x)		x = (x | MODIFIED)
917c478bd9Sstevel@tonic-gate #define	GET_MODIFIED(x)		(x & MODIFIED)
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #define	FAILURE 1
947c478bd9Sstevel@tonic-gate #define	SUCCESS 0
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	DONT_BUILD 3 /* this code is used to prevent building a new file */
977c478bd9Sstevel@tonic-gate 		/* because mcs was given only -p */
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #define	MCS	1
1017c478bd9Sstevel@tonic-gate #define	STRIP	2
1027c478bd9Sstevel@tonic-gate #define	STR_STRIP	"strip"
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Structure to hold section information.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate typedef struct section_info_table {
1087c478bd9Sstevel@tonic-gate 	/*
1097c478bd9Sstevel@tonic-gate 	 * Section information.
1107c478bd9Sstevel@tonic-gate 	 */
1117c478bd9Sstevel@tonic-gate 	Elf_Scn		*scn;		/* Section */
1127c478bd9Sstevel@tonic-gate 	Elf_Data	*data;		/* Original data */
1137c478bd9Sstevel@tonic-gate 	Elf_Data	*mdata;		/* Modified data */
1140d15df17Sab196087 	char		*name;		/* Section name, or NULL if unknown */
1157c478bd9Sstevel@tonic-gate 	char		*rel_name;
1167c478bd9Sstevel@tonic-gate 	GElf_Shdr	shdr;
1177c478bd9Sstevel@tonic-gate 	GElf_Word	secno;		/* The new index */
1187c478bd9Sstevel@tonic-gate 	GElf_Word	osecno;		/* The original index */
1197c478bd9Sstevel@tonic-gate 	GElf_Word	rel_scn_index;
120*56b75c05SAndy Fiddaman 	GElf_Xword	si_flags;
1217c478bd9Sstevel@tonic-gate 	GElf_Xword	rel_loc;
1227c478bd9Sstevel@tonic-gate } section_info_table;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * Structure to hold action information
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate typedef struct action {
1287c478bd9Sstevel@tonic-gate 	int a_action;		/* Which action to take ? */
1297c478bd9Sstevel@tonic-gate 	int a_cnt;		/* Am I applied ? */
1307c478bd9Sstevel@tonic-gate 	char *a_string;		/* The string to be added. */
1317c478bd9Sstevel@tonic-gate } action;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * Structure to hold the section names specified.
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate typedef struct s_name {
1377c478bd9Sstevel@tonic-gate 	char		*name;
1387c478bd9Sstevel@tonic-gate 	struct s_name	*next;
1397c478bd9Sstevel@tonic-gate 	unsigned char	flags;
1407c478bd9Sstevel@tonic-gate } S_Name;
1417c478bd9Sstevel@tonic-gate #define	SECT_NAME	sect_head->name
1427c478bd9Sstevel@tonic-gate #define	SNAME_FLG_STRNCMP	0x01	/* Use strncmp() instead of strcmp() */
1437c478bd9Sstevel@tonic-gate 					/* for section name comparison. */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * Structure to hold command information
1477c478bd9Sstevel@tonic-gate  */
1487c478bd9Sstevel@tonic-gate typedef struct cmd_info {
14957ef7aa9SRod Evans 	APlist	*sh_groups;	/* list of SHT_GROUP sections */
1507c478bd9Sstevel@tonic-gate 	int	no_of_append;
1517c478bd9Sstevel@tonic-gate 	int	no_of_delete;
1527c478bd9Sstevel@tonic-gate 	int	no_of_nulled;
1537c478bd9Sstevel@tonic-gate 	int	no_of_compressed;
1547c478bd9Sstevel@tonic-gate 	int	no_of_moved;
1557c478bd9Sstevel@tonic-gate 	size_t	str_size;	/* size of string to be appended */
156*56b75c05SAndy Fiddaman 	int	ci_flags;	/* Various flags */
1577c478bd9Sstevel@tonic-gate } Cmd_Info;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #define	MIGHT_CHG	0x0001
1607c478bd9Sstevel@tonic-gate #define	aFLAG		0x0002
1617c478bd9Sstevel@tonic-gate #define	cFLAG		0x0004
1627c478bd9Sstevel@tonic-gate #define	dFLAG		0x0008
1637c478bd9Sstevel@tonic-gate #define	lFLAG		0x0010
1647c478bd9Sstevel@tonic-gate #define	pFLAG		0x0020
1657c478bd9Sstevel@tonic-gate #define	xFLAG		0x0040
1667c478bd9Sstevel@tonic-gate #define	VFLAG		0x0080
1677c478bd9Sstevel@tonic-gate #define	zFLAG		0x0100
1687c478bd9Sstevel@tonic-gate #define	I_AM_STRIP	0x0200
1697c478bd9Sstevel@tonic-gate #define	SHF_GROUP_MOVE	0x0400	/* SHF_GROUP section moves */
1707c478bd9Sstevel@tonic-gate #define	SHF_GROUP_DEL	0x0800	/* SHF_GROUP section deleted */
1717c478bd9Sstevel@tonic-gate 
172*56b75c05SAndy Fiddaman #define	CHK_OPT(_x, _y)	(((_x)->ci_flags & (_y)) != 0)
173*56b75c05SAndy Fiddaman #define	SET_OPT(_x, _y)	((_x)->ci_flags |= (_y))
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * Segment Table
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate typedef struct seg_table {
1797c478bd9Sstevel@tonic-gate 	GElf_Off	p_offset;
1807c478bd9Sstevel@tonic-gate 	GElf_Xword	p_memsz;
1817c478bd9Sstevel@tonic-gate 	GElf_Xword	p_filesz;
1827c478bd9Sstevel@tonic-gate } Seg_Table;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
185d1477c77SAli Bahrami  * Temporary files
186d1477c77SAli Bahrami  */
187d1477c77SAli Bahrami typedef struct {
188d1477c77SAli Bahrami 	const char	*tmp_name;	/* NULL, or name of temp file */
189d1477c77SAli Bahrami 	int		tmp_unlink;   /* True if should unlink prior to exit */
190d1477c77SAli Bahrami } Tmp_File;
191d1477c77SAli Bahrami 
192d1477c77SAli Bahrami /*
1937c478bd9Sstevel@tonic-gate  * Function prototypes.
1947c478bd9Sstevel@tonic-gate  */
1957c478bd9Sstevel@tonic-gate int		apply_action(section_info_table *, char *, Cmd_Info *);
1967c478bd9Sstevel@tonic-gate int		each_file(char *, Cmd_Info *);
1977c478bd9Sstevel@tonic-gate void		error_message(int, ...);
1987c478bd9Sstevel@tonic-gate void		mcs_exit(int);
1997c478bd9Sstevel@tonic-gate int		sectcmp(char *);
200d1477c77SAli Bahrami void		free_tempfile(Tmp_File *);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  * Error messages
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate #define	MALLOC_ERROR		0
2067c478bd9Sstevel@tonic-gate #define	USAGE_ERROR		1
2077c478bd9Sstevel@tonic-gate #define	ELFVER_ERROR		2
2087c478bd9Sstevel@tonic-gate #define	OPEN_ERROR		3
2097c478bd9Sstevel@tonic-gate #define	LIBELF_ERROR		4
2107c478bd9Sstevel@tonic-gate #define	OPEN_TEMP_ERROR		5
2117c478bd9Sstevel@tonic-gate #define	WRITE_ERROR		6
2127c478bd9Sstevel@tonic-gate #define	GETARHDR_ERROR		7
2137c478bd9Sstevel@tonic-gate #define	FILE_TYPE_ERROR		8
2147c478bd9Sstevel@tonic-gate #define	NOT_MANIPULATED_ERROR	9
2157c478bd9Sstevel@tonic-gate #define	WRN_MANIPULATED_ERROR	10
2167c478bd9Sstevel@tonic-gate #define	NO_SECT_TABLE_ERROR	11
2177c478bd9Sstevel@tonic-gate #define	READ_ERROR		12
2187c478bd9Sstevel@tonic-gate #define	READ_MANI_ERROR		13
2197c478bd9Sstevel@tonic-gate #define	WRITE_MANI_ERROR	14
2207c478bd9Sstevel@tonic-gate #define	LSEEK_MANI_ERROR	15
2217c478bd9Sstevel@tonic-gate #define	SYM_TAB_AR_ERROR	16
2227c478bd9Sstevel@tonic-gate #define	EXEC_AR_ERROR		17
2237c478bd9Sstevel@tonic-gate #define	READ_SYS_ERROR		18
2247c478bd9Sstevel@tonic-gate #define	OPEN_WRITE_ERROR	19
2257c478bd9Sstevel@tonic-gate #define	ACT_PRINT_ERROR		20
2267c478bd9Sstevel@tonic-gate #define	ACT_DELETE1_ERROR	21
2277c478bd9Sstevel@tonic-gate #define	ACT_DELETE2_ERROR	22
2287c478bd9Sstevel@tonic-gate #define	ACT_APPEND1_ERROR	23
2297c478bd9Sstevel@tonic-gate #define	ACT_APPEND2_ERROR	24
2307c478bd9Sstevel@tonic-gate #define	ACT_COMPRESS1_ERROR	25
2317c478bd9Sstevel@tonic-gate #define	ACT_COMPRESS2_ERROR	26
2327c478bd9Sstevel@tonic-gate #define	ACCESS_ERROR		27
2337c478bd9Sstevel@tonic-gate #define	WRITE_MANI_ERROR2	28
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate #define	PLAIN_ERROR	0
2367c478bd9Sstevel@tonic-gate #define	LIBelf_ERROR	1
2377c478bd9Sstevel@tonic-gate #define	SYSTEM_ERROR	2
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate #endif
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate #endif	/* _MCS_H */
244