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