home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dosdisas.zip / dccsrcoo.zip / Loader.h < prev    next >
C/C++ Source or Header  |  1997-10-14  |  5KB  |  123 lines

  1. /* This file contains the definition of the abstract Loader class, which
  2.     attempts to provide a relatively machine independent interface for
  3.     programs that read binary files.
  4.  
  5.     The abstraction is as follows. The named file is loaded as if at the
  6.     native address implied by the file (if not, then at a default address).
  7.     When loaded with the Load() function, it is relocated appropriately.
  8.     The loader object maintains a list of sections. (If a format, such
  9.     as the Windows NE format, is not really organised into sections,
  10.     then sections will be created, one for the symbol table information,
  11.     one for each loadable segment, and so on). Section information is
  12.     available as an array of SECTIONINFO structs; each struct has infor-
  13.     mation about where the section is loaded, whether it is text or
  14.     data, and whether it is a primary or non primary section. Primary
  15.     sections are the ones that most loader applications would be
  16.     interested in (e.g. .text and .plt sections, but not .init sections,
  17.     even though the latter may contain code).
  18.  
  19.     The Loader object keeps track of the total size of the code and
  20.     data sections, and the mapping between native and host addresses.
  21.     The program using the Loader object can read the whole image by
  22.     processing all the code and data.
  23.  
  24.     If the binary file is an archive file with multiple parts, only the
  25.     first part is initially loaded. Subsequent parts may be accessed
  26.     by calling the NextPart() function. Load() and NextPart() return
  27.     1 for success, and 0 for failure.
  28.  
  29.     Symbol tables are treated as a list of (string, value) pairs, where
  30.     the value is the native address associated with the string. Names
  31.     and values in the symbol table are in no particular order.
  32.  
  33.     The called program must call UnLoad() when done with the loaded
  34.     image; otherwise the image will not be properly freed. This is
  35.     true even if Load() fails (returns 0).
  36.  
  37.     To summarise usage:
  38.         1) Instantiate the appropriate object (derived from this abstract
  39.             class; e.g. to load elf files, instantiate an ElfLoader obj)
  40.  
  41.         2) Call Load() with the full name of the file
  42.  
  43.         3) Call GetNumSections() and GetSectionInfo() and examine the
  44.             section information. Use the pointers in the SECTIONINFO structs
  45.             to access the loaded image
  46.  
  47.         4) If required, call GetNumSyms() and GetSymTable() for symbol
  48.             table information
  49.  
  50.         5) If required, call NextPart(), and if it returns true, repeat
  51.             steps 3-5
  52.  
  53.         6) Call UnLoad(). Do not access the loaded image after this call.
  54.  
  55.  
  56.     It should be possible to use this code with any c++ compiler under
  57.     Unix or Windows, since no fancy classes like String were used.
  58. */
  59.  
  60. #include <string.h>        /* memcpy(), strcat(), strlen() */
  61.  
  62.  
  63. // Section Type flag values. See below for usage
  64. #define ST_CODE        0x01        // Section contains program code
  65. #define ST_DATA        0x02        // Section contains program data
  66. #define ST_BSS        0x04        // Section is not initialised (no file data)
  67. #define ST_PRIMARY    0x08        // Section is important (e.g. .text, not .init)
  68. #define ST_READONLY    0x10        // Section has read-only data
  69. #define ST_HEADER    0x20        // Pseudo-Section is the header
  70. #define ST_RELOC    0x40        // Pseudo-Section with reloc info (.EXE only)
  71.  
  72. // SECTIONINFO structure. GetSectionInfo returns a pointer to an array of
  73. // these structs. All information about the sections is contained in these
  74. // structures.
  75.  
  76. typedef struct sectioninfo_tag
  77. {
  78.     char*        pSectionName;        // Name of section
  79.     unsigned    fSectionFlags;        // Flags, bitwise OR of ST_XXX flags
  80.     unsigned    uNativeAddr;    // Logical or native load address
  81.     unsigned    uHostAddr;        // Host or actual address of data
  82.     unsigned    uSectionSize;        // Size of section in bytes
  83.     unsigned    uSectionEntrySize;    // Size of one section entry (if applic)
  84. } SECTIONINFO;
  85.  
  86. typedef SECTIONINFO* PSECTIONINFO;
  87.     
  88. typedef struct symtab_tag
  89. {
  90.     char*        pSymName;        // Name of the symbol
  91.     unsigned    uSymValue;        // Value of the symbol
  92. } LD_SYMTAB;
  93.  
  94. typedef LD_SYMTAB* PLD_SYMTAB;
  95.     
  96. class Loader
  97. {
  98. public:
  99.                 Loader();                    // Default constructor
  100.   virtual int    Load(const char* sName) = 0;// Load the file; pure virtual
  101.   virtual int    GetNextPart() = 0;            // Get next part of archive
  102.   virtual void    UnLoad() = 0;                // Unload file; pure virtual
  103.     int            GetNumSections() const;        // Return number of sections
  104.     PSECTIONINFO GetSectionInfo() const;        // Return array of section structs
  105.     int            GetNumSyms() const;            // Return number of sections
  106.     PLD_SYMTAB     GetSymTable() const;        // Return array of section structs
  107.                 // Find section index given name
  108.     int            GetSectionIndexByName(const char* sName);
  109.                 // Find section info given name
  110.     PSECTIONINFO GetSectionInfoByName(const char* sName);
  111.     unsigned    GetInitPC() const;            // Get the initial PC
  112.     unsigned    GetInitSP() const;            // Get the initial SP
  113.  
  114. protected:
  115.     int            m_iNumSections;                // Number of sections
  116.     PSECTIONINFO m_pSections;                // The section info
  117.     int            m_iNumSymbols;                // Number of symbols
  118.     PLD_SYMTAB    m_pSymbols;                    // The symbol table
  119.     unsigned    m_uInitPC;                    // Initial program counter
  120.     unsigned    m_uInitSP;                    // Initial stack pointer
  121. };
  122.  
  123.