1 /****************************  cmdline.h   ***********************************
2 * Author:        Agner Fog
3 * Date created:  2006-07-25
4 * Last modified: 2006-07-25
5 * Project:       objconv
6 * Module:        cmdline.h
7 * Description:
8 * Header file for command line interpreter cmdline.cpp
9 *
10 * Copyright 2006-2008 GNU General Public License http://www.gnu.org/licenses
11 *****************************************************************************/
12 #ifndef CMDLINE_H
13 #define CMDLINE_H
14 
15 /**************************  Define constants  ******************************/
16 // Max number of response files on command line
17 #define MAX_COMMAND_FILES  10
18 
19 // Constants for output file type
20 #define CMDL_OUTPUT_DUMP          0x80       // No output file, just dump contents
21 #define CMDL_OUTPUT_ELF    FILETYPE_ELF      // ELF file
22 #define CMDL_OUTPUT_PE     FILETYPE_COFF     // MS-COFF/PE file
23 #define CMDL_OUTPUT_OMF    FILETYPE_OMF      // OMF file
24 #define CMDL_OUTPUT_MACHO  FILETYPE_MACHO_LE // Mach-O file, little endian
25 #define CMDL_OUTPUT_MASM   FILETYPE_ASM      // Disassembly
26 
27 // Constants for subtypes
28 #define SUBTYPE_MASM                 0       // Disassembly MASM/TASM
29 #define SUBTYPE_NASM                 1       // Disassembly NASM/YASM
30 #define SUBTYPE_GASM                 2       // Disassembly GAS(Intel)
31 
32 // Constants for verbose or silent console output
33 #define CMDL_VERBOSE_NO              0     // Silent. No console output if no errors or warnings
34 #define CMDL_VERBOSE_YES             1     // Output messages about file names and types
35 #define CMDL_VERBOSE_DIAGNOSTICS     2     // Output more messages
36 
37 // Constants for dump options
38 #define DUMP_NONE               0x0000     // Dump nothing
39 #define DUMP_FILEHDR            0x0001     // Dump file header
40 #define DUMP_SECTHDR            0x0002     // Dump section headers
41 #define DUMP_SYMTAB             0x0010     // Dump symbol table
42 #define DUMP_RELTAB             0x0020     // Dump relocation table
43 #define DUMP_STRINGTB           0x0040     // Dump string table
44 #define DUMP_COMMENT            0x0080     // Dump comment records
45 
46 // Constants for stripping or converting debug information from file
47 #define CMDL_DEBUG_DEFAULT           0     // Remove if output is different format
48 #define CMDL_DEBUG_STRIP             1     // Remove debugging information from file
49 #define CMDL_DEBUG_PRESERVE          2     // Leave debugging information unchanged
50 #define CMDL_DEBUG_LINNUM            4     // Convert line number information (not supported)
51 #define CMDL_DEBUG_SYMBOLS           8     // Convert symbol information (not supported)
52 
53 // Constants for stripping exception handler information from file
54 #define CMDL_EXCEPTION_DEFAULT       0     // Remove if output is different format
55 #define CMDL_EXCEPTION_STRIP         1     // Remove exception handler information from file
56 #define CMDL_EXCEPTION_PRESERVE      2     // Leave exception handler information unchanged
57 
58 // Constants for adding/removing leading underscores from symbol names
59 #define CMDL_UNDERSCORE_NOCHANGE     0     // Don't add or remove underscores
60 #define CMDL_UNDERSCORE_CHANGE       1     // Change underscores to default for target
61 #define CMDL_UNDERSCORE_REMOVE       2     // Remove underscores from symbol names
62 #define CMDL_UNDERSCORE_ADD          3     // Add underscores to symbol names
63 #define CMDL_KEEP_ALIAS          0x100     // Keep old name as alias
64 
65 // Constants for replacing leading dot with underscore or vice versa in section names
66 #define CMDL_SECTIONDOT_NOCHANGE     0     // Don't change section names
67 #define CMDL_SECTIONDOT_CHANGE       1     // Change leading character in section names to default for target
68 #define CMDL_SECTIONDOT_U2DOT        2     // Change underscore to dot in section names
69 #define CMDL_SECTIONDOT_DOT2U        3     // Change dot to underscore in unknown section names
70 
71 // Constants for library options
72 #define CMDL_LIBRARY_DEFAULT         0     // No option specified
73 #define CMDL_LIBRARY_CONVERT         1     // Convert or modify library
74 #define CMDL_LIBRARY_ADDMEMBER       2     // Add object file to library
75 #define CMDL_LIBRARY_EXTRACTMEM  0x100     // Extract specified object file(s) from library
76 #define CMDL_LIBRARY_EXTRACTALL  0x110     // Extract all object files from library
77 
78 // Constants for file input/output options
79 #define CMDL_FILE_INPUT              1     // Input file required
80 #define CMDL_FILE_IN_IF_EXISTS       2     // Read input file if it exists
81 #define CMDL_FILE_OUTPUT          0x10     // Write output file required
82 #define CMDL_FILE_IN_OUT_SAME     0x20     // Input and output files may have the same name
83 
84 #define MAXSYMBOLLENGTH           1024     // Maximum length of symbols for changing underscore or dot
85 
86 // Constants for symbol type as input to CCommandLineInterpreter::SymbolChange()
87 #define SYMT_OTHER                   0     // File name or unknown symbol type
88 #define SYMT_SECTION                 1     // Segment or section name
89 #define SYMT_LOCAL                   2     // Local symbol (not imported or exported)
90 #define SYMT_PUBLIC                  3     // Public or weak symbol (exported)
91 #define SYMT_EXTERNAL                4     // External symbol (imported)
92 #define SYMT_LIBRARYMEMBER      0x1000     // Name of library member
93 
94 // Constants for symbol change action as defined in SSymbolChange::Action
95 // and output from CCommandLineInterpreter::SymbolChange()
96 #define SYMA_NOCHANGE                0     // Do nothing
97 #define SYMA_MAKE_WEAK               1     // Make symbol weak
98 #define SYMA_MAKE_LOCAL              2     // Make symbol local
99 #define SYMA_CHANGE_NAME          0x10     // Change name of symbol
100 #define SYMA_CHANGE_PREFIX        0x11     // Change beginning of symbol name
101 #define SYMA_CHANGE_SUFFIX        0x12     // Change end of symbol name
102 #define SYMA_ALIAS               0x100     // Make alias of public symbol and keep old name, must be combined
103                                            // with SYMA_CHANGE_NAME, SYMA_CHANGE_PREFIX or SYMA_CHANGE_SUFFIX
104 #define SYMA_ADD_MEMBER         0x1001     // Add member to library
105 #define SYMA_DELETE_MEMBER      0x1002     // Remove member from library
106 #define SYMA_EXTRACT_MEMBER     0x1004     // Extract member from library
107 
108 // Structure for specifying desired change of a specific symbol
109 struct SSymbolChange {
110    char * Name1;                           // Symbol name to look for
111    char * Name2;                           // Replace with this name
112    int    Action;                          // Action to take on symbol
113    int    Done;                            // Count how many times this has been done
114 };
115 
116 // Class for interpreting command line
117 class CCommandLineInterpreter {
118 public:
119    CCommandLineInterpreter();                // Default constructor
120    ~CCommandLineInterpreter();               // Destructor
121    void ReadCommandLine(int argc, char * argv[]);     // Read and interpret command line
122    int  SymbolChange(char const * oldname, char const ** newname, int symtype); // Check if symbol has to be changed
123    int  SymbolIsInList(char const * name);   // Check if symbol is in SymbolList
124    int  SymbolChangesRequested();            // Any kind of symbol change requested on command line
125    void ReportStatistics();                  // Report statistics about name changes etc.
126    void CountDebugRemoved();                 // Increment CountDebugSectionsRemoved
127    void CountExceptionRemoved();             // Increment CountExceptionSectionsRemoved
128    void CountSymbolsHidden();                // Increment CountUnusedSymbolsHidden
129    SSymbolChange const * GetMemberToAdd();   // Get names of object files to add to library
130    void CheckExtractSuccess();               // Check if library members to extract were found
131    void CheckSymbolModifySuccess();          // Check if symbols to modify were found
132    char * InputFile;                         // Input file name
133    char * OutputFile;                        // Output file name
134    int    InputType;                         // Input file type (detected from file)
135    int    OutputType;                        // Output type (file type or dump)
136    int    SubType;                           // Subtype of output type. Assembly language dialect or library type
137    int    MemberType;                        // File type of library members
138    int    DesiredWordSize;                   // Desired word size for output file
139    uint32_t Verbose;                           // How much diagnostics to print on screen
140    uint32_t DumpOptions;                       // Options for dumping file
141    uint32_t DebugInfo;                         // Strip or convert debug info
142    uint32_t ExeptionInfo;                      // Strip or preserve exception handler info and other incompatible info
143    uint32_t Underscore;                        // Add/remove underscores in symbol names
144    uint32_t SegmentDot;                        // Change underscore/dot in beginning of segment names
145    uint32_t LibraryOptions;                    // Options for manipulating library
146    uint32_t LibrarySubtype;                    // Options for manipulating library
147    uint32_t FileOptions;                       // Options for input and output files
148    uint32_t ImageBase;                         // Specified image base
149    int    ShowHelp;                          // Help screen printed
150 protected:
151    int  libmode;                             // -lib option has been encountered
152    void ReadCommandItem(char *);             // Read one option from command line
153    void ReadCommandFile(char *);             // Read commands from file
154    void InterpretFileName(char *);           // Interpret input or output filename from command line
155    void InterpretCommandOption(char *);      // Interpret one option from command line
156    void InterpretOutputTypeOption(char *);   // Interpret output type option from command line
157    void InterpretVerboseOption(char *);      // Interpret silent/verbose option from command line
158    void InterpretDumpOption(char *);         // Interpret dump option from command line
159    void InterpretDebugInfoOption(char *);    // Interpret debug info option from command line
160    void InterpretExceptionInfoOption(char*); // Interpret exception handler info option from command line
161    void InterpretErrorOption(char *);        // Interpret error option from command line
162    void InterpretSymbolNameChangeOption(char *);  // Interpret various options for changing symbol names
163    void InterpretLibraryOption(char *);      // Interpret options for manipulating library/archive files
164    void InterpretImagebaseOption(char *);    // Interpret image base option
165    void AddObjectToLibrary(char * filename, char * membername); // Add object file to library
166    void Help();                              // Print help message
167    CArrayBuf<CFileBuffer> ResponseFiles;     // Array of up to 10 response file buffers
168    int NumBuffers;                           // Number of response file buffers
169    int SymbolChangeEntries;                  // Number of entries in SymbolList, except library entries
170    CMemoryBuffer SymbolList;                 // List of symbol names to change. Contains entries of type SSymbolChange
171    CMemoryBuffer MemberNames;                // Buffer containing truncated member names
172    uint32_t MemberNamesAllocated;              // Size of buffer in MemberNames
173    uint32_t CurrentSymbol;                     // Pointer into SymbolList
174    // Statistics counters
175    int CountUnderscoreConversions;           // Count number of times symbol leading underscores are changed
176    int CountSectionDotConversions;           // Count number of times leading character is changed on section names
177    int CountSymbolNameChanges;               // Count number of times symbol names are changed at specific command
178    int CountSymbolNameAliases;               // Count number of times symbol names are aliased at specific command or underscore command
179    int CountSymbolsWeakened;                 // Count number of times symbol names are made weak at specific command
180    int CountSymbolsMadeLocal;                // Count number of times symbol names are made local at specific command
181    int CountUnusedSymbolsHidden;             // Count number of times unused symbols are hidden
182    int CountDebugSectionsRemoved;            // Count number of debug sections removed
183    int CountExceptionSectionsRemoved;        // Count number of exception handler sections removed
184 };
185 
186 extern CCommandLineInterpreter cmd;          // Command line interpreter
187 
188 #endif // #ifndef CMDLINE_H
189