10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 55154Sab196087 * Common Development and Distribution License (the "License"). 65154Sab196087 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 220Sstevel@tonic-gate * Copyright (c) 1988 AT&T 230Sstevel@tonic-gate * All Rights Reserved 240Sstevel@tonic-gate * 250Sstevel@tonic-gate * 26*9131SRod.Evans@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 27*9131SRod.Evans@Sun.COM * Use is subject to license terms. 280Sstevel@tonic-gate */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate #ifndef _MCS_H 310Sstevel@tonic-gate #define _MCS_H 320Sstevel@tonic-gate 330Sstevel@tonic-gate #ifdef __cplusplus 340Sstevel@tonic-gate extern "C" { 350Sstevel@tonic-gate #endif 360Sstevel@tonic-gate 370Sstevel@tonic-gate #include <stdio.h> 380Sstevel@tonic-gate #include <stdlib.h> 390Sstevel@tonic-gate #include <stdarg.h> 400Sstevel@tonic-gate #include <unistd.h> 410Sstevel@tonic-gate #include <libelf.h> 420Sstevel@tonic-gate #include <ar.h> 430Sstevel@tonic-gate #include <string.h> 440Sstevel@tonic-gate #include <signal.h> 450Sstevel@tonic-gate #include <sys/stat.h> 460Sstevel@tonic-gate #include <fcntl.h> 470Sstevel@tonic-gate #include <memory.h> 480Sstevel@tonic-gate #include <locale.h> 490Sstevel@tonic-gate #include <sys/mman.h> 500Sstevel@tonic-gate #include "sgs.h" 510Sstevel@tonic-gate #include "gelf.h" 520Sstevel@tonic-gate 530Sstevel@tonic-gate #define FORMAT "%-16s%-12ld%-6u%-6u%-8o%-10ld%-2s" 540Sstevel@tonic-gate #define ROUNDUP(x) (((x) + 1) & ~1) 550Sstevel@tonic-gate #define TMPDIR "/tmp" 560Sstevel@tonic-gate 570Sstevel@tonic-gate #define DELETED -1 /* The section will be removed */ 580Sstevel@tonic-gate #define NULLED -2 /* The section will be nulled */ 590Sstevel@tonic-gate #define EXPANDED -3 /* The size of the section expanded */ 600Sstevel@tonic-gate #define SHRUNK -4 /* The section shrinked */ 610Sstevel@tonic-gate 620Sstevel@tonic-gate #define ACT_NOP 0x00000000 630Sstevel@tonic-gate #define ACT_DELETE 0x00000001 640Sstevel@tonic-gate #define ACT_PRINT 0x00000002 650Sstevel@tonic-gate #define ACT_COMPRESS 0x00000003 660Sstevel@tonic-gate #define ACT_APPEND 0x00000004 670Sstevel@tonic-gate #define ACT_ZAP 0x00000005 680Sstevel@tonic-gate #define SET_ACTION(x, y) x = (x & 0xfffffff0) | y 690Sstevel@tonic-gate #define GET_ACTION(x) (x & 0x0000000f) 700Sstevel@tonic-gate 710Sstevel@tonic-gate #define NOSEG 0x00000010 720Sstevel@tonic-gate #define IN 0x00000020 /* section is IN a segment */ 730Sstevel@tonic-gate #define PRIOR 0x00000030 /* section is PRIOR to a segment */ 740Sstevel@tonic-gate #define AFTER 0x00000040 /* section is AFTER a segment */ 750Sstevel@tonic-gate #define SET_LOC(x, y) x = (x & 0xffffff0f) | y 760Sstevel@tonic-gate #define GET_LOC(x) (x & 0x000000f0) 770Sstevel@tonic-gate 780Sstevel@tonic-gate #define CANDIDATE 0x00000100 790Sstevel@tonic-gate #define MOVING 0x00000200 800Sstevel@tonic-gate #define MODIFIED 0x00000400 810Sstevel@tonic-gate 820Sstevel@tonic-gate #define UNSET_CANDIDATE(x) x = x & ~CANDIDATE 830Sstevel@tonic-gate #define SET_CANDIDATE(x) x = x | CANDIDATE 840Sstevel@tonic-gate #define ISCANDIDATE(x) (x & CANDIDATE) 850Sstevel@tonic-gate #define SET_MOVING(x) x = (x | MOVING) 860Sstevel@tonic-gate #define GET_MOVING(x) (x & MOVING) 870Sstevel@tonic-gate #define SET_MODIFIED(x) x = (x | MODIFIED) 880Sstevel@tonic-gate #define GET_MODIFIED(x) (x & MODIFIED) 890Sstevel@tonic-gate 900Sstevel@tonic-gate #define FAILURE 1 910Sstevel@tonic-gate #define SUCCESS 0 920Sstevel@tonic-gate 930Sstevel@tonic-gate #define DONT_BUILD 3 /* this code is used to prevent building a new file */ 940Sstevel@tonic-gate /* because mcs was given only -p */ 950Sstevel@tonic-gate 960Sstevel@tonic-gate 970Sstevel@tonic-gate #define MCS 1 980Sstevel@tonic-gate #define STRIP 2 990Sstevel@tonic-gate #define STR_STRIP "strip" 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate /* 1020Sstevel@tonic-gate * Structure to hold section information. 1030Sstevel@tonic-gate */ 1040Sstevel@tonic-gate typedef struct section_info_table { 1050Sstevel@tonic-gate /* 1060Sstevel@tonic-gate * Section information. 1070Sstevel@tonic-gate */ 1080Sstevel@tonic-gate Elf_Scn *scn; /* Section */ 1090Sstevel@tonic-gate Elf_Data *data; /* Original data */ 1100Sstevel@tonic-gate Elf_Data *mdata; /* Modified data */ 1115154Sab196087 char *name; /* Section name, or NULL if unknown */ 1120Sstevel@tonic-gate char *rel_name; 1130Sstevel@tonic-gate GElf_Shdr shdr; 1140Sstevel@tonic-gate GElf_Word secno; /* The new index */ 1150Sstevel@tonic-gate GElf_Word osecno; /* The original index */ 1160Sstevel@tonic-gate GElf_Word rel_scn_index; 1170Sstevel@tonic-gate GElf_Xword flags; 1180Sstevel@tonic-gate GElf_Xword rel_loc; 1190Sstevel@tonic-gate } section_info_table; 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate /* 1220Sstevel@tonic-gate * Structure to hold action information 1230Sstevel@tonic-gate */ 1240Sstevel@tonic-gate typedef struct action { 1250Sstevel@tonic-gate int a_action; /* Which action to take ? */ 1260Sstevel@tonic-gate int a_cnt; /* Am I applied ? */ 1270Sstevel@tonic-gate char *a_string; /* The string to be added. */ 1280Sstevel@tonic-gate } action; 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate /* 1310Sstevel@tonic-gate * Structure to hold the section names specified. 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate typedef struct s_name { 1340Sstevel@tonic-gate char *name; 1350Sstevel@tonic-gate struct s_name *next; 1360Sstevel@tonic-gate unsigned char flags; 1370Sstevel@tonic-gate } S_Name; 1380Sstevel@tonic-gate #define SECT_NAME sect_head->name 1390Sstevel@tonic-gate #define SNAME_FLG_STRNCMP 0x01 /* Use strncmp() instead of strcmp() */ 1400Sstevel@tonic-gate /* for section name comparison. */ 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate /* 1430Sstevel@tonic-gate * Structure to hold command information 1440Sstevel@tonic-gate */ 1450Sstevel@tonic-gate typedef struct cmd_info { 146*9131SRod.Evans@Sun.COM APlist *sh_groups; /* list of SHT_GROUP sections */ 1470Sstevel@tonic-gate int no_of_append; 1480Sstevel@tonic-gate int no_of_delete; 1490Sstevel@tonic-gate int no_of_nulled; 1500Sstevel@tonic-gate int no_of_compressed; 1510Sstevel@tonic-gate int no_of_moved; 1520Sstevel@tonic-gate size_t str_size; /* size of string to be appended */ 1530Sstevel@tonic-gate int flags; /* Various flags */ 1540Sstevel@tonic-gate } Cmd_Info; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate #define MIGHT_CHG 0x0001 1570Sstevel@tonic-gate #define aFLAG 0x0002 1580Sstevel@tonic-gate #define cFLAG 0x0004 1590Sstevel@tonic-gate #define dFLAG 0x0008 1600Sstevel@tonic-gate #define lFLAG 0x0010 1610Sstevel@tonic-gate #define pFLAG 0x0020 1620Sstevel@tonic-gate #define xFLAG 0x0040 1630Sstevel@tonic-gate #define VFLAG 0x0080 1640Sstevel@tonic-gate #define zFLAG 0x0100 1650Sstevel@tonic-gate #define I_AM_STRIP 0x0200 1660Sstevel@tonic-gate #define SHF_GROUP_MOVE 0x0400 /* SHF_GROUP section moves */ 1670Sstevel@tonic-gate #define SHF_GROUP_DEL 0x0800 /* SHF_GROUP section deleted */ 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate #define CHK_OPT(_x, _y) (_x->flags & _y) 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate /* 1720Sstevel@tonic-gate * Segment Table 1730Sstevel@tonic-gate */ 1740Sstevel@tonic-gate typedef struct seg_table { 1750Sstevel@tonic-gate GElf_Off p_offset; 1760Sstevel@tonic-gate GElf_Xword p_memsz; 1770Sstevel@tonic-gate GElf_Xword p_filesz; 1780Sstevel@tonic-gate } Seg_Table; 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate /* 1818223SAli.Bahrami@Sun.COM * Temporary files 1828223SAli.Bahrami@Sun.COM */ 1838223SAli.Bahrami@Sun.COM typedef struct { 1848223SAli.Bahrami@Sun.COM const char *tmp_name; /* NULL, or name of temp file */ 1858223SAli.Bahrami@Sun.COM int tmp_unlink; /* True if should unlink prior to exit */ 1868223SAli.Bahrami@Sun.COM } Tmp_File; 1878223SAli.Bahrami@Sun.COM 1888223SAli.Bahrami@Sun.COM /* 1890Sstevel@tonic-gate * Function prototypes. 1900Sstevel@tonic-gate */ 1910Sstevel@tonic-gate int apply_action(section_info_table *, char *, Cmd_Info *); 1920Sstevel@tonic-gate int each_file(char *, Cmd_Info *); 1930Sstevel@tonic-gate void error_message(int, ...); 1940Sstevel@tonic-gate void mcs_exit(int); 1950Sstevel@tonic-gate int sectcmp(char *); 1968223SAli.Bahrami@Sun.COM void free_tempfile(Tmp_File *); 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate /* 1990Sstevel@tonic-gate * Error messages 2000Sstevel@tonic-gate */ 2010Sstevel@tonic-gate #define MALLOC_ERROR 0 2020Sstevel@tonic-gate #define USAGE_ERROR 1 2030Sstevel@tonic-gate #define ELFVER_ERROR 2 2040Sstevel@tonic-gate #define OPEN_ERROR 3 2050Sstevel@tonic-gate #define LIBELF_ERROR 4 2060Sstevel@tonic-gate #define OPEN_TEMP_ERROR 5 2070Sstevel@tonic-gate #define WRITE_ERROR 6 2080Sstevel@tonic-gate #define GETARHDR_ERROR 7 2090Sstevel@tonic-gate #define FILE_TYPE_ERROR 8 2100Sstevel@tonic-gate #define NOT_MANIPULATED_ERROR 9 2110Sstevel@tonic-gate #define WRN_MANIPULATED_ERROR 10 2120Sstevel@tonic-gate #define NO_SECT_TABLE_ERROR 11 2130Sstevel@tonic-gate #define READ_ERROR 12 2140Sstevel@tonic-gate #define READ_MANI_ERROR 13 2150Sstevel@tonic-gate #define WRITE_MANI_ERROR 14 2160Sstevel@tonic-gate #define LSEEK_MANI_ERROR 15 2170Sstevel@tonic-gate #define SYM_TAB_AR_ERROR 16 2180Sstevel@tonic-gate #define EXEC_AR_ERROR 17 2190Sstevel@tonic-gate #define READ_SYS_ERROR 18 2200Sstevel@tonic-gate #define OPEN_WRITE_ERROR 19 2210Sstevel@tonic-gate #define ACT_PRINT_ERROR 20 2220Sstevel@tonic-gate #define ACT_DELETE1_ERROR 21 2230Sstevel@tonic-gate #define ACT_DELETE2_ERROR 22 2240Sstevel@tonic-gate #define ACT_APPEND1_ERROR 23 2250Sstevel@tonic-gate #define ACT_APPEND2_ERROR 24 2260Sstevel@tonic-gate #define ACT_COMPRESS1_ERROR 25 2270Sstevel@tonic-gate #define ACT_COMPRESS2_ERROR 26 2280Sstevel@tonic-gate #define ACCESS_ERROR 27 2290Sstevel@tonic-gate #define WRITE_MANI_ERROR2 28 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate #define PLAIN_ERROR 0 2320Sstevel@tonic-gate #define LIBelf_ERROR 1 2330Sstevel@tonic-gate #define SYSTEM_ERROR 2 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate #ifdef __cplusplus 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate #endif 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate #endif /* _MCS_H */ 240