Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /****************************  converters.h   ********************************
  2. * Author:        Agner Fog
  3. * Date created:  2006-07-15
  4. * Last modified: 2008-05-25
  5. * Project:       objconv
  6. * Module:        converters.h
  7. * Description:
  8. * Header file for file conversion classes.
  9. *
  10. * Copyright 2006-2008 GNU General Public License http://www.gnu.org/licenses
  11. *****************************************************************************/
  12.  
  13. /*******************************   Classes   ********************************
  14.  
  15. This header file declares various classes for interpreting and converting
  16. different types of object files. These classes are all derived from the
  17. container class CFileBuffer, declared in containers.h.
  18.  
  19. See containers.h for an explanation of the container classes and the
  20. operators >> and << which can transfer a data buffer from an object of one
  21. class to an object of another class.
  22.  
  23. *****************************************************************************/
  24.  
  25. #ifndef CONVERTERS_H
  26. #define CONVERTERS_H
  27.  
  28.  
  29. // Structure for string index entry in library
  30. struct SStringEntry {
  31.    uint32 String;                      // Offset to string
  32.    uint32 Member;                      // Library member
  33. };
  34.  
  35.  
  36. // Class CResponseFileBuffer is used for storage of a command line response file
  37. class CResponseFileBuffer : public CFileBuffer {
  38. public:
  39.    CResponseFileBuffer(char const * filename);   // Constructor
  40.    ~CResponseFileBuffer();                       // Destructor
  41.    CResponseFileBuffer * next;                   // Linked list if more than one buffer
  42. };
  43.  
  44.  
  45. // Class for deciding what to do with input file
  46. // Its memory buffer contains the input file and later the output file
  47. class CMain : public CFileBuffer {
  48. public:
  49.    CMain();                            // Constructor
  50.    void Go();                          // Do whatever the command line parameters say
  51. };
  52.  
  53.  
  54. // Class CConverter is used for converting or dumping a file of any type
  55. class CConverter : public CFileBuffer {
  56. public:
  57.    CConverter();                       // Constructor
  58.    void Go();                          // Do whatever the command line parameters say
  59. protected:
  60.    void DumpCOF();                     // Dump PE/COFF file
  61.    void DumpELF();                     // Dump ELF file
  62.    void DumpMACHO();                   // Dump Mach-O file
  63.    void DumpOMF();                     // Dump OMF file
  64.    void ParseMACUnivBin();             // Dump Mac universal binary
  65.    void COF2COF();                     // Make changes in PE file
  66.    void COF2ELF();                     // Convert PE/COFF to ELF file
  67.    void COF2OMF();                     // Convert PE/COFF to OMF file
  68.    void ELF2ELF();                     // Make changes in ELF file
  69.    void ELF2COF();                     // Convert ELF to PE file
  70.    void ELF2MAC();                     // Convert ELF to Mach-O file
  71.    void OMF2COF();                     // Convert OMF file to PE/COFF
  72.    void COF2ASM();                     // Disassemble PE/COFF file
  73.    void ELF2ASM();                     // Disassemble ELF file
  74.    void MAC2ELF();                     // Convert Mach-O file to ELF file
  75.    void MAC2MAC();                     // Make changes in Mach-O file
  76.    void MAC2ASM();                     // Disassemble Mach-O file
  77.    void OMF2ASM();                     // Disassemble OMF file
  78. };
  79.  
  80. // Class for interpreting and dumping PE/COFF files
  81. class CCOFF : public CFileBuffer {
  82. public:
  83.    CCOFF();                                      // Default constructor
  84.    void ParseFile();                             // Parse file buffer
  85.    void Dump(int options);                       // Dump file
  86.    void PrintSymbolTable(int symnum);            // Dump symbol table entries
  87.    void PrintImportExport();                     // Print imported and exported symbols
  88.    static void PrintSegmentCharacteristics(uint32 flags); // Print segment characteristics
  89.    char const * GetSymbolName(int8* Symbol);     // Get symbol name from 8 byte entry
  90.    char const * GetSectionName(int8* Symbol);    // Get section name from 8 byte entry
  91.    const char * GetFileName(SCOFF_SymTableEntry *);    // Get file name from records in symbol table
  92.    const char * GetShortFileName(SCOFF_SymTableEntry*);// Same as above. Strips path before filename
  93.    char const * GetStorageClassName(uint8 sc);   // Get storage class name
  94.    void PublicNames(CMemoryBuffer * Strings, CSList<SStringEntry> * Index, int m); // Make list of public names
  95.    int  GetImageDir(uint32 n, SCOFF_ImageDirAddress * dir); // Find address of image directory for executable files
  96. protected:
  97.    CArrayBuf<SCOFF_SectionHeader> SectionHeaders;// Copy of section headers
  98.    int NSections;                                // Number of sections
  99.    SCOFF_FileHeader * FileHeader;                // File header
  100.    SCOFF_SymTableEntry * SymbolTable;            // Pointer to symbol table (for object files)
  101.    char * StringTable;                           // Pointer to string table (for object files)
  102.    uint32 StringTableSize;                       // Size of string table (for object files)
  103.    int NumberOfSymbols;                          // Number of symbol table entries (for object files)
  104.    uint64 ImageBase;                             // Image base (for executable files)
  105.    SCOFF_OptionalHeader * OptionalHeader;        // Optional header (for executable files)
  106.    SCOFF_IMAGE_DATA_DIRECTORY * pImageDirs;      // Pointer to image directories (for executable files)
  107.    uint32 NumImageDirs;                          // Number of image directories (for executable files)
  108.    uint32 EntryPoint;                            // Entry point (for executable files)
  109. };
  110.  
  111.  
  112. // Class for interpreting and dumping ELF files. Has templates for 32 and 64 bit version
  113. template <class TFileHeader, class TSectionHeader, class TSymbol, class TRelocation>
  114. class CELF : public CFileBuffer {
  115. public:
  116.    CELF();                                       // Default constructor
  117.    void ParseFile();                             // Parse file buffer
  118.    void Dump(int options);                       // Dump file
  119.    void PublicNames(CMemoryBuffer * Strings, CSList<SStringEntry> * Index, int m); // Make list of public names
  120. protected:
  121.    const char * SymbolName(uint32 index);        // Get name of symbol
  122.    TFileHeader FileHeader;                       // Copy of file header
  123.    char * SecStringTable;                        // Section header string table
  124.    uint32 SecStringTableLen;                     // Length of section header string table
  125.    uint32 NSections;                             // Number of sections
  126.    int SectionHeaderSize;                        // Size of each section header
  127.    CArrayBuf<TSectionHeader> SectionHeaders;     // Copy of section headers
  128.    uint32 SymbolTableOffset;                     // Offset to symbol table
  129.    uint32 SymbolTableEntrySize;                  // Entry size of symbol table
  130.    uint32 SymbolTableEntries;                    // Number of symbols
  131.    uint32 SymbolStringTableOffset;               // Offset to symbol string table
  132.    uint32 SymbolStringTableSize;                 // Size of symbol string table
  133. };
  134.  
  135.  
  136. // Class for interpreting and dumping Mach-O files
  137. class COMF : public CFileBuffer {
  138. public:
  139.    COMF();                                       // Default constructor
  140.    void ParseFile();                             // Parse file buffer
  141.    void Dump(int options);                       // Dump file
  142.    void PublicNames(CMemoryBuffer * Strings, CSList<SStringEntry> * Index, int m); // Make list of public names
  143. protected:
  144.    uint32 NumRecords;                            // Number of records
  145.    CSList<SOMFRecordPointer> Records;            // Record pointers (List is 0-based)
  146.    CMemoryBuffer NameBuffer;                     // Store segment names and symbol names
  147.    CSList<uint32> LocalNameOffset;               // Offset into NameBuffer of segment names by name index
  148.    CSList<uint32> SegmentNameOffset;             // Offset into NameBuffer of segment names by segment index
  149.    CSList<uint32> SymbolNameOffset;              // Offset into NameBuffer of external symbol names
  150.    CSList<uint32> GroupNameOffset;               // Offset into NameBuffer of group names
  151.    char * GetLocalName(uint32 i);                // Get segment name by name index
  152.    uint32 GetLocalNameO(uint32 i);               // Get segment name by converting name index offset into NameBuffer
  153.    const char * GetSegmentName(uint32 i);        // Get segment name by segment index
  154.    const char * GetSymbolName(uint32 i);         // Get external symbol name
  155.    const char * GetGroupName(uint32 i);          // Get group name by index
  156.    static const char * GetRecordTypeName(uint32 i);// Get OMF record type name
  157.    void DumpRecordTypes();                       // Dump summary of records
  158.    void DumpNames();                             // Dump local names records
  159.    void DumpSymbols();                           // Dump public and external names records
  160.    void DumpSegments();                          // Dump segment records
  161.    void DumpRelocations();                       // Dump fixup records
  162.    void DumpComments();                          // Dump coment records
  163. };
  164.  
  165. // Class for interpreting and dumping Mach-O files. Has templates for 32 and 64 bit version
  166. template <class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt>
  167. class CMACHO : public CFileBuffer {
  168. public:
  169.    CMACHO();                                     // Default constructor
  170.    void ParseFile();                             // Parse file buffer
  171.    void Dump(int options);                       // Dump file
  172.    void PublicNames(CMemoryBuffer * Strings, CSList<SStringEntry> * Index, int m); // Make list of public names
  173. protected:
  174.    TMAC_header FileHeader;                       // Copy of file header
  175.    uint64 ImageBase;                             // Image base for executable file
  176.    uint32 SegmentOffset;                         // File offset of segment
  177.    uint32 SegmentSize;                           // Size of segment
  178.    uint32 SectionHeaderOffset;                   // File offset of section headers
  179.    uint32 NumSections;                           // Number of sections
  180.    uint32 SymTabOffset;                          // File offset of symbol table
  181.    uint32 SymTabNumber;                          // Number of entries in symbol table
  182.    uint32 StringTabOffset;                       // File offset of string table
  183.    uint32 StringTabSize;                         // Size of string table
  184.    uint32 ilocalsym;                                // index to local symbols
  185.    uint32 nlocalsym;                                // number of local symbols
  186.    uint32 iextdefsym;                            // index to public symbols
  187.    uint32 nextdefsym;                            // number of public symbols
  188.    uint32 iundefsym;                                // index to external symbols
  189.    uint32 nundefsym;                                // number of external symbols
  190.    uint32 IndirectSymTabOffset;                  // file offset to the indirect symbol table
  191.    uint32 IndirectSymTabNumber;                  // number of indirect symbol table entries
  192. };
  193.  
  194. // Class for parsing Macintosh universal binary
  195. class CMACUNIV : public CFileBuffer {
  196. public:
  197.    CMACUNIV();                                   // Default constructor
  198.    void Go(int options);                         // Apply command line options to all components
  199. };
  200.  
  201.  
  202. // class CCOF2ELF handles conversion from PE/COFF file to ELF file. Has templates for 32 and 64 bit version
  203. template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
  204. class CCOF2ELF : public CCOFF {
  205. public:
  206.    CCOF2ELF();                                    // Constructor
  207.    void Convert();                                // Do the conversion
  208. protected:
  209.    void MakeSegments();                           // Convert subfunction: Segments
  210.    void MakeSymbolTable();                        // Convert subfunction: Symbol table and string tables
  211.    void MakeRelocationTables();                   // Convert subfunction: Relocation tables
  212.    void MakeBinaryFile();                         // Convert subfunction: Putting sections together
  213.    int symtab;                                    // Symbol table section number
  214.    int shstrtab;                                  // Section name string table section number
  215.    int strtab;                                    // Object name string table section number
  216.    int stabstr;                                   // Debug string table section number
  217.    int NumSectionsNew;                            // Number of sections generated for 'to' file
  218.    int MaxSectionsNew;                            // Number of section buffers allocated for 'to' file
  219.    CArrayBuf<CMemoryBuffer> NewSections;          // Buffers for building each section
  220.    CArrayBuf<TELF_SectionHeader> NewSectionHeaders;// Buffer for temporary section headers
  221.    CArrayBuf<int> NewSectIndex;                   // Buffers for array of new section indices
  222.    CArrayBuf<int> NewSymbolIndex;                 // Buffers for array of new symbol indices
  223.    CFileBuffer ToFile;                            // File buffer for ELF file
  224.    TELF_Header NewFileHeader;                     // New file header
  225. };
  226.  
  227.  
  228. // class CCOF2OMF handles conversion from PE/COFF file to OMF file
  229. class CCOF2OMF : public CCOFF {
  230. public:
  231.    CCOF2OMF();                                    // Constructor
  232.    void Convert();                                // Do the conversion
  233. protected:
  234.    void MakeSegmentList();                        // Make temporary segment conversion list
  235.    void MakeSymbolList();                         // Make temporary symbol conversion list
  236.    void MakeRelocationsList();                    // Make temporary list of relocations (fixups) and sort it
  237.    void MakeLNAMES();                             // Make THEADR and LNAMES records
  238.    void MakeSEGDEF();                             // Make SEGDEF and GRPDEF records
  239.    void MakeEXTDEF();                             // Make EXTDEF records
  240.    void MakePUBDEF();                             // Make PUBDEF records
  241.    void MakeLEDATA();                             // Make LEDATA, LIDATA and FIXUPP records
  242.    void MakeMODEND();                             // Make MODEND record and finish file
  243.    CArrayBuf<SOMFSegmentList> SectionBuffer;      // Summarize old sections. Translate section index to segment index
  244.    CArrayBuf<SOMFSymbolList> SymbolBuffer;        // Translate old symbol index to new public/external index
  245.    CSList<SOMFRelocation> RelocationBuffer;       // Summarize and sort relocations
  246.    CMemoryBuffer NameBuffer;                      // Temporary storage of text strings
  247.    COMFFileBuilder ToFile;                        // File buffer for new OMF file
  248.    int  NumSegments;                              // Number of segments in new file
  249.    int  SectionBufferNum;                         // Number of entries in SectionBuffer
  250.    uint32 NumPublicSymbols;                       // Number of public symbols in new file
  251.    uint32 NumExternalSymbols;                     // Number of external symbols in new file
  252.    uint32 NumRelocations;                         // Number of entries in RelocationBuffer
  253. };
  254.  
  255.  
  256. // class COMF2COF handles conversion from OMF file to PE/COFF file
  257. class COMF2COF : public COMF {
  258. public:
  259.    COMF2COF();                                    // Constructor
  260.    void Convert();                                // Do the conversion
  261. protected:
  262.    // Convert subfunctions:
  263.    void MakeFileHeader();                        // File header
  264.    void MakeSymbolTable1();                      // Make symbol table and string table entries for file and segments
  265.    void MakeSymbolTable2();                      // Make symbol table and string table entries for external symbols
  266.    void MakeSymbolTable3();                      // Make symbol table and string table entries for public symbols
  267.    void MakeSymbolTable4();                      // Make symbol table and string table entries for communal symbols
  268.    void MakeSymbolTable5();                      // Make symbol table and string table entries for local symbols
  269.    void MakeSections();                          // Make sections and relocation tables
  270.    void MakeBinaryFile();                        // Putting sections together
  271.    void CheckUnsupportedRecords();               // Make warnings if file containes unsupported record types
  272.    int  NumSectionsNew;                          // Number of sections in new file
  273.    CFileBuffer ToFile;                           // File buffer for PE/COFF file
  274.    CSList<SCOFF_SymTableEntry> NewSymbolTable;   // New symbol table entries
  275.    CSList<SCOFF_SectionHeader> NewSectionHeaders;// New section headers
  276.    CMemoryBuffer NewStringTable;                 // Buffer for building new string table
  277.    CMemoryBuffer NewData;                        // Raw data for each section in new file and its relocation table
  278.    CSList<uint32> SegmentTranslation;            // Translate old segment number to new symbol table index
  279.    CSList<uint32> ExtdefTranslation;             // Translate old external symbol number to new symbol table index
  280.    CSList<SOMFLocalSymbol> LocalSymbols;         // List for assigning names to unnamed local symbols
  281.    SCOFF_FileHeader NewFileHeader;               // New file header
  282. };
  283.  
  284.  
  285. // class CELF2COF handles conversion from ELF file to PE/COFF file. Has templates for 32 and 64 bit version
  286. template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
  287. class CELF2COF : public CELF<ELFSTRUCTURES> {
  288. public:
  289.    CELF2COF();                                   // Constructor
  290.    void Convert();                               // Do the conversion
  291. protected:
  292.    void MakeFileHeader();                        // Convert subfunction: File header
  293.    void MakeSectionsIndex();                     // Convert subfunction: Make section index translation table
  294.    void MakeSections();                          // Convert subfunction: Make sections and relocation tables
  295.    void MakeSymbolTable();                       // Convert subfunction: Symbol table and string tables
  296.    void HideUnusedSymbols();                     // Convert subfunction: Hide unused symbols
  297.    void MakeBinaryFile();                        // Convert subfunction: Putting sections together
  298.    int NumSectionsNew;                           // Number of sections in new file
  299.    CArrayBuf<int32> NewSectIndex;                // Array of new section indices
  300.    CArrayBuf<int32> SymbolsUsed;                 // Array of new symbol indices
  301.    CSList<int32> NewSymbolIndex;                 // Buffer for array of new symbol indices
  302.    CMemoryBuffer NewSymbolTable;                 // Buffer for building new symbol table
  303.    CMemoryBuffer NewStringTable;                 // Buffer for building new string table
  304.    CMemoryBuffer NewRawData;                     // Buffer for building new raw data area
  305.    uint32 RawDataOffset;                         // File offset for raw data
  306.    CFileBuffer ToFile;                           // File buffer for PE/COFF file
  307.    SCOFF_FileHeader NewFileHeader;               // New file header
  308. };
  309.  
  310.  
  311. // class CELF2MAC handles conversion from ELF file to Mach-O file
  312. template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation,
  313.           class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt>
  314. class CELF2MAC : public CELF<ELFSTRUCTURES> {
  315. public:
  316.    CELF2MAC();                         // Constructor
  317.    void Convert();                     // Do the conversion
  318. protected:
  319.    void MakeFileHeader();              // Convert subfunction: File header
  320.    void MakeSectionsIndex();           // Convert subfunction: Make section index translation table
  321.    void MakeSections();                // Convert subfunction: Make sections and relocation tables
  322.    void MakeSymbolTable();             // Convert subfunction: Symbol table and string tables
  323.    void FindUnusedSymbols();           // Convert subfunction: Check if symbols used, remove unused symbols
  324.    void MakeBinaryFile();              // Convert subfunction: Putting sections together
  325.    // Translate relocations, seperate function for 32 and 64 bits:
  326.    void Elf2MacRelocations(Elf32_Shdr &, MAC_section_32 &, uint32 NewRawDataOffset, uint32 oldsec);
  327.    void Elf2MacRelocations(Elf64_Shdr &, MAC_section_64 &, uint32 NewRawDataOffset, uint32 oldsec);
  328.    int  GetImagebaseSymbol();          // Symbol table index of __mh_execute_header
  329.    CFileBuffer   ToFile;               // File buffer for new Mach-O file
  330.    CMemoryBuffer NewRawData;           // Buffer for building new raw data area
  331.    CMemoryBuffer NewRelocationTab;     // Buffer for new relocation tables
  332.    CMemoryBuffer NewStringTable;       // Buffer for building new string table
  333.    CMemoryBuffer UnnamedSymbolsTable;  // Buffer for assigning names to unnamed symbols
  334.    CArrayBuf<int> NewSectIndex;        // Array of new section indices
  335.    CArrayBuf<MInt> NewSectOffset;      // Array of new section offsets
  336.    CArrayBuf<int> OldSymbolScope;      // Table of symbol bindings: 0 = local, 1 = public, 2 = external
  337.    CArrayBuf<int> OldSymbolUsed;       // Check if symbol is used
  338.    MacSymbolTableBuilder<TMAC_nlist, MInt> NewSymTab[3]; // New symbol tables for local, public, external symbols
  339.    uint32 NumSymbols[4];               // Accumulated number of entries in each NewSymTab[]
  340.    uint32 NewSectHeadOffset;           // File offset to first section header
  341.    uint32 NewSymtabOffset;             // File offset to symtab command
  342.    int NumSectionsNew;                 // Number of sections in new file
  343.    uint32 RawDataOffset;               // Offset to raw data in old file
  344.    uint32 NumOldSymbols;               // Number of symbols in old file
  345.    uint32 CommandOffset;               // Offset to first load command = segment header
  346. };
  347.  
  348. // class MAC2ELF handles conversion from Mach-O file to ELF file
  349. template <class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt,
  350.           class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
  351. class CMAC2ELF : public CMACHO<MACSTRUCTURES> {
  352. public:
  353.    CMAC2ELF();                         // Constructor
  354.    void Convert();                     // Do the conversion
  355. protected:
  356.    void MakeSegments();                           // Convert subfunction: Segments
  357.    void MakeSymbolTable();                        // Convert subfunction: Symbol table and string tables
  358.    void MakeRelocationTables(MAC_header_32&);     // Convert subfunction: Relocation tables, 32-bit version
  359.    void MakeRelocationTables(MAC_header_64&);     // Convert subfunction: Relocation tables, 64-bit version
  360.    void MakeImportTables();                       // Convert subfunction: Fill import tables
  361.    void MakeBinaryFile();                         // Convert subfunction: Putting sections together
  362.    void TranslateAddress(MInt addr, uint32 & section, uint32 & offset); // Translate address to section + offset
  363.    uint32 MakeGOTEntry(int symbol);               // Make entry in fake GOT for symbol
  364.    void MakeGOT();                                // Make fake Global Offset Table
  365.    int symtab;                                    // Symbol table section number
  366.    int shstrtab;                                  // Section name string table section number
  367.    int strtab;                                    // Object name string table section number
  368.    int stabstr;                                   // Debug string table section number
  369.    uint32 NumSectionsNew;                         // Number of sections generated for 'to' file
  370.    uint32 MaxSectionsNew;                         // Number of section buffers allocated for 'to' file
  371.    uint32 HasGOT;                                 // Contains references to global offset table
  372.    int FakeGOTSection;                            // Fake GOT section number
  373.    int FakeGOTSymbol;                             // Symbol index for fake GOT
  374.    TELF_Header NewFileHeader;                     // New file header
  375.    CArrayBuf<CMemoryBuffer> NewSections;          // Buffers for building each section
  376.    CArrayBuf<TELF_SectionHeader> NewSectionHeaders;// Array of temporary section headers
  377.    CArrayBuf<int> NewSectIndex;                   // Array of new section indices
  378.    CArrayBuf<int> NewSymbolIndex;                 // Array of new symbol indices
  379.    CArrayBuf<int> SectionSymbols;                 // Array of new symbol indices for sections
  380.    CFileBuffer ToFile;                            // File buffer for ELF file
  381.    CSList<int> GOTSymbols;                        // List of symbols needing GOT entry
  382. };
  383.  
  384.  
  385. // class CCOF2COF handles symbol changes in a PE/COFF file
  386. class CCOF2COF : public CCOFF {
  387. public:
  388.    CCOF2COF();                                   // Constructor
  389.    void Convert();                               // Do the conversion
  390. protected:
  391.    void MakeSymbolTable();                       // Convert subfunction: Symbol table and string tables
  392.    void MakeBinaryFile();                        // Convert subfunction: Putting sections together
  393.    CMemoryBuffer NewSymbolTable;                 // Buffers for building new symbol table
  394.    CMemoryBuffer NewStringTable;                 // Buffers for building new string table
  395.    CFileBuffer ToFile;                           // File buffer for modified PE file
  396. };
  397.  
  398.  
  399. // class CELF2ELF handles symbol changes in ELF file. Has templates for 32 and 64 bit version
  400. template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
  401. class CELF2ELF : public CELF<ELFSTRUCTURES> {
  402. public:
  403.    CELF2ELF();                                   // Constructor
  404.    void Convert();                               // Do the conversion
  405. protected:
  406.    void MakeSymbolTable();                       // Convert subfunction: Symbol table and string tables
  407.    void ChangeSections();                        // Convert subfunction: Change section names if needed
  408.    void MakeBinaryFile();                        // Convert subfunction: Putting sections together
  409.    uint32 isymtab[2];                            // static and dynamic symbol table section number
  410.    uint32 istrtab[4];                            // string table section number: symbols, dynamic symbols, sections, debug
  411.    CMemoryBuffer NewSymbolTable[2];              // Buffers for building new symbol tables: static, dynamic
  412.    CMemoryBuffer NewStringTable[4];              // Buffers for building new string tables: symbols, dynamic symbols, sections, debug
  413.    CArrayBuf<uint32> NewSymbolIndex;             // Array for translating old to new symbol indices
  414.    uint32 NumOldSymbols;                         // Size of NewSymbolIndex table
  415.    uint32 FirstGlobalSymbol;                     // Index to first global symbol in .symtab
  416.    CFileBuffer ToFile;                           // File buffer for modified PE file
  417. };
  418.  
  419.  
  420. // class CMAC2MAC handles symbol changes in Mach-O file. Has templates for 32 and 64 bit version
  421. template <class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt>
  422. class CMAC2MAC : public CMACHO<MACSTRUCTURES> {
  423. public:
  424.    CMAC2MAC();                                   // Constructor
  425.    void Convert();                               // Do the conversion
  426. protected:
  427.    void MakeSymbolTable();                       // Convert subfunction: Symbol table and string tables
  428.    void ChangeSegments();                        // Convert subfunction: Change segment names if needed
  429.    void ChangeSections(uint32 HeaderOffset, uint32 Num);// Convert subfunction: Change section names and relocation records if needed
  430.    void ChangeImportTable(uint32 FileOffset, uint32 Num);// Convert subfunction: Change symbol indices in import table if needed
  431.    void MakeBinaryFile();                        // Convert subfunction: Putting sections together
  432.    int  NewSymbolIndex(int OldIndex);            // Convert subfunction: Translate old to new symbol index
  433.    uint32 NewFileOffset(uint32 OldOffset);       // Convert subfunction: Translate old to new file offset
  434.    MacSymbolTableBuilder<TMAC_nlist, MInt> NewSymbols[3];// Buffers for building new symbol tables: local, public, external
  435.    CMemoryBuffer NewSymbolTable;                 // Buffer for building new symbol table
  436.    CMemoryBuffer NewStringTable;                 // Buffer for building new string table
  437.    CFileBuffer ToFile;                           // File buffer for modified PE file
  438.    uint32 NumOldSymbols;                         // Size of NewSymbolIndex table
  439.    uint32 NewIlocalsym;                          // index to local symbols
  440.    uint32 NewNlocalsym;                          // number of local symbols
  441.    uint32 NewIextdefsym;                              // index to public symbols
  442.    uint32 NewNextdefsym;                              // number of public symbols
  443.    uint32 NewIundefsym;                          // index to external symbols
  444.    uint32 NewNundefsym;                          // number of external symbols
  445.    uint32 NewSymtabOffset;                       // Offset to new symbol table
  446.    uint32 NewStringtabOffset;                    // Offset to new string table
  447.    uint32 NewStringtabEnd;                       // Offset to end of new string table
  448.    uint32 OldTablesEnd;                          // End of old symbol table and string table
  449.    int32  SizeDifference;                        // Size of new file minus size of old file
  450. };
  451.  
  452.  
  453. // class CCOF2ASM handles disassembly of PE/COFF file
  454. class CCOF2ASM : public CCOFF {
  455. public:
  456.    CCOF2ASM();                                   // Constructor
  457.    void Convert();                               // Do the conversion
  458. protected:
  459.    CDisassembler Disasm;                         // Disassembler
  460.    void MakeSectionList();                       // Make Sections list and Relocations list in Disasm
  461.    void MakeSymbolList();                        // Make Symbols list in Disasm
  462.    void MakeDynamicRelocations();                // Make dynamic base relocations for executable files
  463.    void MakeImportList();                        // Make imported symbols for executable files
  464.    void MakeExportList();                        // Make exported symbols for executable files
  465.    void MakeListLabels();                        // Attach names to all image directories
  466. };
  467.  
  468. // class CELF2ASM handles disassembly of ELF file
  469. template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation>
  470. class CELF2ASM : public CELF<ELFSTRUCTURES> {
  471. public:
  472.    CELF2ASM();                                   // Constructor
  473.    void Convert();                               // Do the conversion
  474. protected:
  475.    CDisassembler Disasm;                         // Disassembler
  476.    CArrayBuf<int32>SectionNumberTranslate;       // Translate section numbers in source file to section numbers in asm file
  477.    CArrayBuf<uint32>SymbolTableOffset;           // Addend to add to symbol number for each symbol table
  478.    int64 ImageBase;                              // Image base if executable file
  479.    uint32 ExeType;                               // File type: 0 = object, 1 = DLL/shared object, 2 = executable
  480.    uint32 NumSymbols;                            // Number of symbols defined
  481.    void FindImageBase();                         // Find image base
  482.    void MakeSectionList();                       // Make Sections list in Disasm
  483.    void MakeSymbolList();                        // Make Symbols list in Disasm
  484.    void MakeRelocations();                       // Make relocations for object and executable files
  485.    void MakeImportList();                        // Make imported symbols for executable files
  486.    void MakeExportList();                        // Make exported symbols for executable files
  487.    void MakeListLabels();                        // Attach names to all image directories
  488. };
  489.  
  490. // class CMAC2ASM handles disassembly of Mach-O file
  491. template <class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt>
  492. class CMAC2ASM : public CMACHO<MACSTRUCTURES> {
  493. public:
  494.    CMAC2ASM();                                   // Constructor
  495.    void Convert();                               // Do the conversion
  496. protected:
  497.    void MakeSectionList();                       // Make Sections list in Disasm
  498.    void MakeSymbolList();                        // Make Symbols list in Disasm
  499.    void MakeRelocations();                       // Make relocation list in Disasm
  500.    void MakeImports();                           // Make symbol entries for imported symbols
  501.    CDisassembler Disasm;                         // Disassembler
  502.    CMemoryBuffer StringBuffer;                   // Buffer for making section names
  503.    CSList<MAC_SECT_WITH_RELOC> RelocationQueue;  // List of relocation tables
  504.    CSList<TMAC_section*> ImportSections;          // List of sections needing extra symbols: import tables, literals, etc.
  505. };
  506.  
  507. // class COMF2ASM handles disassembly of OMF object files
  508. class COMF2ASM : public COMF {
  509. public:
  510.    COMF2ASM();                                   // Constructor
  511.    void Convert();                               // Do the conversion
  512. protected:
  513.    void CountSegments();                         // Make temporary Segments table
  514.    void MakeExternalSymbolsTable();              // Make external symbols in Disasm
  515.    void MakePublicSymbolsTable();                // Make symbol table entries for public symbols
  516.    void MakeCommunalSymbolsTable();              // Make symbol table entries for communal symbols
  517.    void MakeGroupDefinitions();                  // Make segment group definitions
  518.    void MakeSegmentList();                       // Make Segments list in Disasm
  519.    void MakeRelocations(int32 Segment, uint32 RecNum, uint32 SOffset, uint32 RSize, uint8 * SData);// Make relocation list in Disasm
  520.    CDisassembler Disasm;                         // Disassembler
  521.    CSList<SOMFSegment> Segments;                 // Name, size, etc. of all segments
  522.    CSList<uint32> ExtdefTranslation;             // Translate old external symbol number to disasm symbol table index
  523.    CSList<uint32> PubdefTranslation;             // Translate old public symbol number to disasm symbol table index
  524.    CMemoryBuffer SegmentData;                    // Binary segment data
  525.    int32 NumSegments;                            // Number of segments
  526.    int32 FirstComDatSection;                     // First COMDAT section. All sections before this are SEGDEF segments
  527. };
  528.  
  529. #endif // #ifndef CONVERTERS_H
  530.