home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / XSRVMTE.C < prev    next >
Text File  |  1996-03-20  |  57KB  |  1,019 lines

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   srvrmte.c                                                            822*/
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*                                                                           */
  7. /*  Mte table handling functions.                                            */
  8. /*                                                                           */
  9. /* History:                                                                  */
  10. /*                                                                           */
  11. /*   05/04/93 Created.                                                       */
  12. /*                                                                           */
  13. /*...Release 1.04 (04/30/93)                                                 */
  14. /*...                                                                        */
  15. /*... 05/04/93  822   Joe       Add mte table handling.                      */
  16. /*...                                                                        */
  17. /*****************************************************************************/
  18. #include "all.h"
  19.  
  20. /*****************************************************************************/
  21. /* AddToModuleLoadTable()                                                    */
  22. /*                                                                           */
  23. /* Description:                                                              */
  24. /*                                                                           */
  25. /*   Add a module load notification link to a linked list of                 */
  26. /*   notifications. There will be a link for each load and free.             */
  27. /*                                                                           */
  28. /*             --------       --------                                       */
  29. /*      ----->|        |---->|        |----                                  */
  30. /*            | next   |     | next   |    |                                 */
  31. /*             --------       --------     -                                 */
  32. /*            | sizeof |     | sizeof |                                      */
  33. /*            | entry  |     | entry  |                                      */
  34. /*             --------       --------     -                                 */
  35. /*            |        |     |        |                                      */
  36. /*            | Table  |     | Table  |                                      */
  37. /*            | Entry  |     | Entry  |                                      */
  38. /*            |(see    |     |        |                                      */
  39. /*            | below) |     |        |                                      */
  40. /*            |        |     |        |                                      */
  41. /*             --------       --------                                       */
  42. /*                                                                           */
  43. /*   Table Entries look like so:                                             */
  44. /*                                                                           */
  45. /*   Note: "record length" does not include the length of the                */
  46. /*         "record length" field.                                            */
  47. /*                                                                           */
  48. /*     4        4     4              var            var                      */
  49. /*    --------------------------------------//-----------//-------           */
  50. /*   |        |      |             |              |               |          */
  51. /*   | record | mte  | module name | module name  | object table  |          */
  52. /*   | length |      | length      |              |               |          */
  53. /*    --------------------------------------//-----------//-------           */
  54. /*   |        |      |             |              |               |          */
  55. /*   | record | mte  | module name | module name  | object table  |          */
  56. /*   | length |      | length      |              |               |          */
  57. /*    --------------------------------------//-----------//-------           */
  58. /*        .                                                                  */
  59. /*        .                                                                  */
  60. /*        .                                                                  */
  61. /*                                                                           */
  62. /*                                                                           */
  63. /*   The object table will look like so:                                     */
  64. /*                                                                           */
  65. /*     4            4        4          4          1     1                   */
  66. /*    ------------------------------------------------------------           */
  67. /*   | Number of  |        |          |          |      |         |          */
  68. /*   | objects in | Object | LoadAddr | LoadSize | Type | Bitness |          */
  69. /*   | the table  | Number |          |          |      |         |          */
  70. /*    ------------------------------------------------------------           */
  71. /*                |        |          |          |      |         |          */
  72. /*                | Object | LoadAddr | LoadSize | Type | Bitness |          */
  73. /*                | Number |          |          |      |         |          */
  74. /*                 -----------------------------------------------           */
  75. /*                   .                                                       */
  76. /*                   .                                                       */
  77. /*                   .                                                       */
  78. /*                                                                           */
  79. /* Parameters:                                                               */
  80. /*                                                                           */
  81. /*  ppModuleList input - Address of a pointer to the module list.            */
  82. /*  Pid          input - Process id owning the mte.                          */
  83. /*  mte          input - EXE/DLL handle of freed dll.                        */
  84. /*                                                                           */
  85. /* Return:                                                                   */
  86. /*                                                                           */
  87. /*   A link will be added to the end of the list pointed to by               */
  88. /*   ppModuleList.                                                           */
  89. /*                                                                           */
  90. /* Assumptions:                                                              */
  91. /*                                                                           */
  92. /*  Pid and mte are valid.                                                   */
  93. /*                                                                           */
  94. /*****************************************************************************/
  95. void AddToModuleLoadTable( UINT **ppModuleList, ULONG Pid, ULONG mte)
  96. {
  97.  char  ModuleName[CCHMAXPATH];
  98.  FILE *fpModule;
  99.  UINT *pObjectTable;
  100.  UINT *ptr;
  101.  UINT *p;
  102.  int  NumObjects;
  103.  int  SizeOfObjectTable;
  104.  int  ModuleNameLength;
  105.  int  TableSize;
  106.  
  107.  /****************************************************************************/
  108.  /* - Get the module name.                                                   */
  109.  /* - Open the module and if it fails then we'll simply proceed              */
  110.  /*   as if we never received the notification.                              */
  111.  /* - Get the object table.                                                  */
  112.  /****************************************************************************/
  113.  DosQueryModuleName( mte,CCHMAXPATH,ModuleName);
  114.  fpModule = fopen( ModuleName,"rb");
  115.  if( fpModule == NULL )
  116.   return;
  117.  pObjectTable = GetObjectTable( fpModule, Pid, mte );
  118.  if(pObjectTable )
  119.  {
  120.  
  121.   /***************************************************************************/
  122.   /* - Add the table to the x-server list of tables.                         */
  123.   /***************************************************************************/
  124.   AddObjTableToList( pObjectTable,mte);
  125.  
  126.   /***************************************************************************/
  127.   /* We're going to add a table to the linked list of module tables.         */
  128.   /* - Get the size of the module table.                                     */
  129.   /* - Get the length of the module name + 1 for z-string termination.       */
  130.   /* - Get the table size not including the "next" pointer or the            */
  131.   /*   size of the rest of the table.                                        */
  132.   /* - Allocate the storage needed for the table.                            */
  133.   /* - Fill in the table entries.                                            */
  134.   /* - Get a ptr to the last module table in the list.                       */
  135.   /* - Add the table to the list of tables.                                  */
  136.   /***************************************************************************/
  137.   NumObjects = *pObjectTable;
  138.   SizeOfObjectTable =  sizeof(NumObjects) + NumObjects*sizeof(OBJTABLEENTRY);
  139.   ModuleNameLength = strlen(ModuleName);
  140.   ModuleNameLength += 1;
  141.  
  142.   TableSize = 4 +                       /* size of mte.                      */
  143.               4 +                       /* size of module name length.       */
  144.               ModuleNameLength +        /* length of the module name.        */
  145.               SizeOfObjectTable;        /* size of object table. duh!        */
  146.  
  147.   p=ptr=Talloc(TableSize +
  148.                4 +                      /* size of next->.                   */
  149.                4 );                     /* size of length of rest of table.  */
  150.  
  151.   *(p)++ = NULL;                        /* ground the node.                  */
  152.   *p++ = TableSize;                     /* size of the rest of the table.    */
  153.   *p++ = mte;
  154.   *p++ = ModuleNameLength;
  155.   strcpy((char*)p,ModuleName);
  156.   p = (UINT*)( (UCHAR*)p + ModuleNameLength);
  157.   memcpy(p,pObjectTable,SizeOfObjectTable);
  158.   Tfree(pObjectTable);
  159.   for(p=(UINT*)ppModuleList;*p!=NULL;p= (UINT*)*p){;}
  160.   *p = (UINT)ptr;
  161.  }
  162.  fclose(fpModule);
  163. }
  164.  
  165. /*****************************************************************************/
  166. /* AddFreeToModuleLoadTable()                                             822*/
  167. /*                                                                           */
  168. /* Description:                                                              */
  169. /*                                                                           */
  170. /*  Add a module free notification link  to the module load table.           */
  171. /*  (See the comments under AddToModuleLoadTable() for a description         */
  172. /*   of linked list. )                                                       */
  173. /*                                                                           */
  174. /*  The entry has the following format:                                      */
  175. /*                                                                           */
  176. /*                                                |->object table            */
  177. /*                                                |                          */
  178. /*     4        4     4              1              4                        */
  179. /*    ------------------------------------------------------------           */
  180. /*   |        |      |             |              |               |          */
  181. /*   | record | mte  | 1           | '\0'         | 0 objects     |          */
  182. /*   | length |      | name len    | null z-string|               |          */
  183. /*    ------------------------------------------------------------           */
  184. /*                                                                           */
  185. /* Parameters:                                                               */
  186. /*                                                                           */
  187. /*  ppModuleList input - Address of a pointer to the module list.            */
  188. /*  Pid          input - Process id owning the mte.                          */
  189. /*  mte          input - EXE/DLL handle of freed dll.                        */
  190. /*                                                                           */
  191. /* Return:                                                                   */
  192. /*                                                                           */
  193. /*   A link will be added to the end of the list pointed to by               */
  194. /*   ppModuleList.                                                           */
  195. /*                                                                           */
  196. /* Assumptions:                                                              */
  197. /*                                                                           */
  198. /*  Pid and mte are valid.                                                   */
  199. /*                                                                           */
  200. /*****************************************************************************/
  201. void AddFreeToModuleLoadTable( UINT **ppModuleList, ULONG mte)
  202. {
  203.  UINT *ptr;
  204.  UINT *p;
  205.  int  TableSize;
  206.  
  207.  /****************************************************************************/
  208.  /* - First, remove from x-server list of tables.                            */
  209.  /****************************************************************************/
  210.  DelObjTableFromList( mte );
  211.  
  212.  TableSize = 4 +                        /* size of mte.                      */
  213.              4 +                        /* size of module name length.       */
  214.              1 +                        /* length of the module name.        */
  215.              4;                         /* size of object table.             */
  216.  
  217.  p=ptr=Talloc(TableSize +
  218.               4 +                       /* size of next->.                   */
  219.               4 );                      /* size of length of rest of table.  */
  220.  
  221.  /****************************************************************************/
  222.  /* - Since Talloc() zeros the allocated space we only have to add the       */
  223.  /*   non-zero entries.                                                      */
  224.  /****************************************************************************/
  225.  *(p)++ = NULL;                         /* ground the node.                  */
  226.  *p++ = TableSize;                      /* size of the rest of the table.    */
  227.  *p++ = mte;
  228.  *p++ = 1;
  229.  
  230.  /****************************************************************************/
  231.  /* Add the entry to the linked list of modules.                             */
  232.  /****************************************************************************/
  233.  for(p=(UINT*)ppModuleList;*p!=NULL;p= (UINT*)*p){;}
  234.  *p = (UINT)ptr;
  235. }
  236.  
  237. /*****************************************************************************/
  238. /* GetObjectTable()                                                       822*/
  239. /*                                                                           */
  240. /* Description:                                                              */
  241. /*  Get the memory object table from the EXE/DLL header.                     */
  242. /*                                                                           */
  243. /*  The table has the following format:                                      */
  244. /*                                                                           */
  245. /*     4            4        4          4          1     1                   */
  246. /*    ------------------------------------------------------------           */
  247. /*   | Number of  |        |          |          |      |         |          */
  248. /*   | objects in | Object | LoadAddr | LoadSize | Type | Bitness |          */
  249. /*   | the table  | Number |          |          |      |         |          */
  250. /*    ------------------------------------------------------------           */
  251. /*                |        |          |          |      |         |          */
  252. /*                | Object | LoadAddr | LoadSize | Type | Bitness |          */
  253. /*                | Number |          |          |      |         |          */
  254. /*                 -----------------------------------------------           */
  255. /*                   .                                                       */
  256. /*                   .                                                       */
  257. /*                   .                                                       */
  258. /*                                                                           */
  259. /* Parameters:                                                               */
  260. /*                                                                           */
  261. /*  fp        input - file handle for the exe/dll.                           */
  262. /*  Pid       input - Process id owning the mte.                             */
  263. /*  mte       input - EXE/DLL handle.                                        */
  264. /*                                                                           */
  265. /* Return:                                                                   */
  266. /*                                                                           */
  267. /*  tableptr  pointer to the table.                                          */
  268. /*                                                                           */
  269. /* Assumptions:                                                              */
  270. /*                                                                           */
  271. /*  Pid and mte are valid.                                                   */
  272. /*                                                                           */
  273. /*****************************************************************************/
  274. #define OFFSETOFOS2HDR   0x3CL          /* loc contains offset of start of   */
  275.                                         /* OS/2 exe header.                  */
  276.                                         /*                                   */
  277. /* The following offsets are relative to the start of the EXE header         */
  278. /* for 16 bit EXE/DLLs.                                                      */
  279.                                         /*                                   */
  280. #define OFFNUMOBJS16      0x1CL         /* loc with # of 16 bit segs.        */
  281. #define OFFOBJTAB16       0x22L         /* loc with # start of 16 bit        */
  282.                                         /* segment table.                    */
  283. #define SIZENUMOBJS16     2             /* bytes to read for num of segs.    */
  284. #define SIZEOBJTAB16      2             /* bytes to read for segment         */
  285.                                         /* table offset.                     */
  286. #define SIZETABENTRY16    8             /* table entry size in file.         */
  287. #define CODEBIT16         0             /* flag bit for code/data object.    */
  288. #define OFFOFFLAGS16      4             /* offs of flags field in tbl ent.   */
  289. #define OFFOFOBJSIZE16    2             /* offs of size  field in tbl ent.   */
  290.  
  291. /* The following offsets are relative to the start of the EXE header         */
  292. /* for 32 bit EXE/DLLs.                                                      */
  293.                                         /*                                   */
  294. #define OFFNUMOBJS32      0x44L         /* loc with # of 32 bit objects.     */
  295. #define OFFOBJTAB32       0x40L         /* loc with # start of 32 bit        */
  296.                                         /* object table.                     */
  297. #define SIZENUMOBJS32     4             /* bytes to read for num of objs.    */
  298. #define SIZEOBJTAB32      4             /* bytes to read for obj table       */
  299.                                         /* offset.                           */
  300. #define SIZETABENTRY32    24            /* table entry size in file.         */
  301. #define CODEBIT32         2             /* flag bit for code object.         */
  302. #define OFFOFFLAGS32      8             /* offs of flags field in tbl ent.   */
  303. #define OFFOFOBJSIZE32    0             /* offs of size  field in tbl ent.   */
  304. #define BIGDEFAULT        13            /* bit for 32 or 16 bitness.         */
  305. #define MAXBUFFERSIZE     SIZETABENTRY32/* max read buffer size.             */
  306.  
  307.  
  308.  UINT *
  309. GetObjectTable( FILE *fp , ULONG Pid, ULONG mte )
  310. {
  311.  char            buffer[MAXBUFFERSIZE]; /* file table entry read buffer.     */
  312.  ULONG           StartOS2Hdr;           /* loc of the OS/2 EXE/DLL hdr.      */
  313.  UINT            NumOfObjs;             /* number of memory objects.         */
  314.  UINT            StartOfTable;          /* start of object table in file.    */
  315.  UINT            flags;                 /* object flags.                     */
  316.  UINT            ObjNum;                /* ordinal object number.            */
  317.  int             NumObjs;               /* number of code objects.           */
  318.  UINT           *tableptr;              /* -> to table we're building.       */
  319.  OBJTABLEENTRY  *tp;                    /* -> to object table entries.       */
  320.  PtraceBuffer    ptb;                   /* DosDebug buffer.                  */
  321.  UCHAR           ExeType;               /* 16 bit/32 bit EXE/DLL.            */
  322.  ULONG           OffOfNumOfObjects;     /* location containing number of     */
  323.                                         /* objects in object table.          */
  324.  ULONG           OffOfObjTable;         /* location containing the offset    */
  325.                                         /* of the object table.              */
  326.  int             NumBytesToRead;        /* bytes to read from the file.      */
  327.  int             OffOfFlags;            /* offs of flags field in tbl ent.   */
  328.  int             OffOfObjSize;          /* offs of size  field in tbl ent.   */
  329.  int             SizeOfTable;           /* size of table we're building.     */
  330.  APIRET          rc;                    /* DosDebug return code.             */
  331.  
  332. /*****************************************************************************/
  333. /* - Get the location of the OS/2 EXE/DLL header.                            */
  334. /* - Establish 16 or 32 bitness.                                             */
  335. /* - Read the location containing the number of memory objects.              */
  336. /* - Read the number of memory objects.                                      */
  337. /* - Read the location containing the offset of the object table.            */
  338. /* - Read the location of the object table.                                  */
  339. /* - Read the object table to get the number of objects.                     */
  340. /* - Allocate the table.                                                     */
  341. /* - Build the table and convert object numbers to load addresses.           */
  342. /*                                                                           */
  343. /*****************************************************************************/
  344.  fseek(fp,OFFSETOFOS2HDR,SEEK_SET);
  345.  fread( buffer,1,4,fp);
  346.  StartOS2Hdr = *( ULONG *)buffer;
  347.  
  348.  /****************************************************************************/
  349.  /* Establish 16 or 32 bitness.                                              */
  350.  /****************************************************************************/
  351.  fseek(fp, StartOS2Hdr,SEEK_SET);
  352.  fread( buffer , 1,1 , fp);
  353.  ExeType = BIT16;
  354.  if( buffer[0] == 'L' )
  355.   ExeType = BIT32;
  356.  
  357.  /****************************************************************************/
  358.  /* Read the number of memory ojbects.                                       */
  359.  /****************************************************************************/
  360.  OffOfNumOfObjects = OFFNUMOBJS16;
  361.  NumBytesToRead    = SIZENUMOBJS16;
  362.  if ( ExeType == BIT32 )
  363.  {
  364.   OffOfNumOfObjects = OFFNUMOBJS32;
  365.   NumBytesToRead    = SIZENUMOBJS32;
  366.  }
  367.  memset(buffer,0,sizeof(buffer) );
  368.  fseek(fp, StartOS2Hdr + OffOfNumOfObjects,SEEK_SET);
  369.  fread( buffer , 1,NumBytesToRead , fp);
  370.  NumOfObjs  = *( int *)buffer;
  371.  
  372.  /****************************************************************************/
  373.  /* Get the file offset start of the table.                                  */
  374.  /****************************************************************************/
  375.  OffOfObjTable     = OFFOBJTAB16;
  376.  NumBytesToRead    = SIZEOBJTAB16;
  377.  if ( ExeType == BIT32 )
  378.  {
  379.   NumBytesToRead    = SIZEOBJTAB32;
  380.   OffOfObjTable     = OFFOBJTAB32;
  381.  }
  382.  memset(buffer,0,sizeof(buffer) );
  383.  fseek(fp, StartOS2Hdr + OffOfObjTable,SEEK_SET);
  384.  fread( buffer,1,NumBytesToRead,fp);
  385.  StartOfTable = *( UINT *)buffer;
  386.  
  387.  /****************************************************************************/
  388.  /* Read the table once to get the number of objects. We have to have this   */
  389.  /* before we know how much memory to allocate.                              */
  390.  /*                                                                          */
  391.  /*  - read table entries and check the appropriate flags for code or data   */
  392.  /*  - if no objects, then return a null pointer.                            */
  393.  /*                                                                          */
  394.  /* Note:                                                                    */
  395.  /*                                                                          */
  396.  /*  The high order part of the flags on a 16 bit read are garbage but       */
  397.  /*  we're not doing anything with it so it doesn't matter.                  */
  398.  /*                                                                          */
  399.  /****************************************************************************/
  400.  NumObjs = 0;
  401.  fseek(fp,StartOS2Hdr+StartOfTable,SEEK_SET);
  402.  NumBytesToRead = SIZETABENTRY16;
  403.  OffOfFlags = OFFOFFLAGS16;
  404.  if( ExeType == BIT32 )
  405.  {
  406.   NumBytesToRead = SIZETABENTRY32;
  407.   OffOfFlags = OFFOFFLAGS32;
  408.  }
  409.  for ( ObjNum = 1; ObjNum <= NumOfObjs; ObjNum++ )
  410.  {
  411.   memset(buffer,0,sizeof(buffer) );
  412.   fread( buffer,1,NumBytesToRead,fp);
  413.   flags = *(UINT *)&buffer[OffOfFlags];
  414.   NumObjs++;
  415.  }
  416.  
  417.  /****************************************************************************/
  418.  /* Add this code if we don't want dlls with 0 objects.                      */
  419.  /****************************************************************************/
  420.  
  421.  if(NumObjs == 0 )
  422.   return(NULL);
  423.  
  424.  
  425.  /****************************************************************************/
  426.  /* Now read the table entries and fill in the valuse.                       */
  427.  /*                                                                          */
  428.  /* - Allocate the memory for the table.( number of objects + size. )        */
  429.  /* - Make first table entry the number of objects in the table.             */
  430.  /* - Read the table entries.                                                */
  431.  /* - Convert object numbers to addresses.                                   */
  432.  /*                                                                          */
  433.  /****************************************************************************/
  434.  SizeOfTable = NumObjs*sizeof(OBJTABLEENTRY) + sizeof(int);
  435.  tableptr    = (void *)Talloc( SizeOfTable );
  436.  *tableptr   = NumObjs;
  437.  tp          = (OBJTABLEENTRY *)(tableptr + 1);
  438.  
  439.  fseek(fp,StartOS2Hdr+StartOfTable,SEEK_SET);
  440.  NumBytesToRead = SIZETABENTRY16;
  441.  OffOfFlags     = OFFOFFLAGS16;
  442.  OffOfObjSize   = OFFOFOBJSIZE16;
  443.  if( ExeType == BIT32 )
  444.  {
  445.   NumBytesToRead = SIZETABENTRY32;
  446.   OffOfFlags     = OFFOFFLAGS32;
  447.   OffOfObjSize   = OFFOFOBJSIZE32;
  448.  }
  449.  
  450.  for ( ObjNum = 1; ObjNum <= NumOfObjs; ObjNum++,tp++)
  451.  {
  452.   memset(buffer,0,sizeof(buffer) );
  453.   fread( buffer,1,NumBytesToRead,fp);
  454.   flags = *(UINT *)&buffer[OffOfFlags];
  455.   switch( ExeType )
  456.   {
  457.    case BIT32:
  458.     if( TestBit(flags,CODEBIT32) == FALSE )
  459.       tp->ObjType = DATA;
  460.     else
  461.       tp->ObjType = CODE;
  462.     tp->ObjLoadAddr = ObjNum;
  463.     tp->ObjLoadSize = *(UINT *)&buffer[OffOfObjSize];
  464.     tp->ObjBitness = (UCHAR)( (TestBit(flags,BIGDEFAULT) )?BIT32:BIT16);
  465.     break;
  466.  
  467.    case BIT16:
  468.     if( TestBit(flags,CODEBIT16) == TRUE  )
  469.       tp->ObjType = DATA;
  470.     else
  471.       tp->ObjType = CODE;
  472.     tp->ObjLoadAddr = ObjNum;
  473.     tp->ObjLoadSize = (UINT)(*(USHORT *)&buffer[OffOfObjSize]);
  474.     tp->ObjBitness  = BIT16;
  475.     break;
  476.   }
  477.  
  478.   /***************************************************************************/
  479.   /* Convert object number to a load address. If there is an error, then     */
  480.   /* assign a 0 load address to the object number.                           */
  481.   /***************************************************************************/
  482.   memset(&ptb,0,sizeof(ptb));
  483.   ptb.Pid   = Pid;
  484.   ptb.MTE   = mte;
  485.   ptb.Cmd   = DBG_C_NumToAddr;
  486.   ptb.Value = ObjNum;
  487.   rc=DosDebug(&ptb);
  488.   if( rc || ptb.Cmd != 0)
  489.   {
  490.    tp->ObjNum      =  0;
  491.    tp->ObjLoadAddr =  0;
  492.    tp->ObjLoadSize =  0;
  493.    tp->ObjBitness  =  BIT32;
  494.   }
  495.   else
  496.   {
  497.    tp->ObjNum = ObjNum;
  498.    tp->ObjLoadAddr =  ptb.Addr;
  499.   }
  500.  
  501.   /***************************************************************************/
  502.   /* Now put in the 16:16 base address for 16 bit objects.                   */
  503.   /***************************************************************************/
  504.  
  505.   if( tp->ObjBitness == BIT16 )
  506.    Sys_Flat2SelOff(tp->ObjLoadAddr,&tp->ObjLoadSel,(USHORT*)&tp->ObjLoadOff);
  507.  }
  508.  return( tableptr );
  509. }                                       /* end GetObjectTable().             */
  510.  
  511. /*****************************************************************************/
  512. /* PackModuleLoadTable()                                                  822*/
  513. /*                                                                           */
  514. /* Description:                                                              */
  515. /*                                                                           */
  516. /*   Take the linked list of module load information and pack it for         */
  517. /*   return to the caller.                                                   */
  518. /*                                                                           */
  519. /*   The packed table will look like so:                                     */
  520. /*                                                                           */
  521. /*                                                                           */
  522. /*     4                                                                     */
  523. /*     -------                                                               */
  524. /*    |       |<--The number of mte entries in this table                    */
  525. /*    |       |                                                              */
  526. /*     -------                                                               */
  527. /*       .                                                                   */
  528. /*       .                                                                   */
  529. /*       .                                                                   */
  530. /*                                                                           */
  531. /*     4        4     4              var            var                      */
  532. /*    --------------------------------------//-----------//-------           */
  533. /*   |        |      |             |              |               |          */
  534. /*   | record | mte  | module name | module name  | object table  |          */
  535. /*   | length |      | length      |              |               |          */
  536. /*    --------------------------------------//-----------//-------           */
  537. /*   |        |      |             |              |               |          */
  538. /*   | record | mte  | module name | module name  | object table  |          */
  539. /*   | length |      | length      |              |               |          */
  540. /*    --------------------------------------//-----------//-------           */
  541. /*        .                                                                  */
  542. /*        .                                                                  */
  543. /*        .                                                                  */
  544. /*                                                                           */
  545. /*                                                                           */
  546. /*   The object table will look like so:                                     */
  547. /*                                                                           */
  548. /*     4            4        4          4          1     1                   */
  549. /*    ------------------------------------------------------------           */
  550. /*   | Number of  |        |          |          |      |         |          */
  551. /*   | objects in | Object | LoadAddr | LoadSize | Type | Bitness |          */
  552. /*   | the table  | Number |          |          |      |         |          */
  553. /*    ------------------------------------------------------------           */
  554. /*                |        |          |          |      |         |          */
  555. /*                | Object | LoadAddr | LoadSize | Type | Bitness |          */
  556. /*                | Number |          |          |      |         |          */
  557. /*                 -----------------------------------------------           */
  558. /*                   .                                                       */
  559. /*                   .                                                       */
  560. /*                   .                                                       */
  561. /*                                                                           */
  562. /* Parameters:                                                               */
  563. /*                                                                           */
  564. /*   pModuleList        input  - -> to the linked list to be packed.         */
  565. /*   pModuleTableLength output - -> to the length of the table built.        */
  566. /*                                                                           */
  567. /* Return:                                                                   */
  568. /*                                                                           */
  569. /*   pModuleTable  -> to the packed module table.                            */
  570. /*                                                                           */
  571. /* Assumptions:                                                              */
  572. /*                                                                           */
  573. /*   none                                                                    */
  574. /*                                                                           */
  575. /*****************************************************************************/
  576. UINT *PackModuleLoadTable(UINT *pModuleList , int *pModuleTableLength )
  577. {
  578.  int    ModuleTableLength = 0;
  579.  int    NumBytesToCopy;
  580.  UCHAR *ptr;
  581.  UINT  *pModuleTable;
  582.  UINT   NumModules;
  583.  
  584.  typedef struct _link
  585.  {
  586.   struct _link *pnext;
  587.   UINT          LinkSize;
  588.   UINT          mte;
  589.  }LINK;
  590.  
  591.  LINK *p;
  592.  
  593.  /****************************************************************************/
  594.  /* - Compute the size of the block needed to store the table and            */
  595.  /*   the number of modules that will be in the table.                       */
  596.  /****************************************************************************/
  597.  NumModules = 0;
  598.  for( p=(LINK*)pModuleList; p!=NULL; p=p->pnext)
  599.   if(p->mte != 0 )
  600.    {
  601.     ModuleTableLength += 4 + p->LinkSize;
  602.     NumModules++;
  603.    }
  604.  
  605.  ModuleTableLength += sizeof(NumModules);
  606.  /****************************************************************************/
  607.  /* Now pack the table.                                                      */
  608.  /*  - Allocate the storage.                                                 */
  609.  /*  - Put the number of modules in the table.                               */
  610.  /*  - Copy the individual tables to the big block.                          */
  611.  /****************************************************************************/
  612.  pModuleTable = Talloc(ModuleTableLength);
  613.  *pModuleTable = NumModules;
  614.  
  615.  ptr = (UCHAR*)pModuleTable + sizeof(UINT);
  616.  for( p=(LINK*)pModuleList; p!=NULL; p=p->pnext)
  617.   if(p->mte != 0 )
  618.   {
  619.    NumBytesToCopy = p->LinkSize + sizeof(p->LinkSize);
  620.    memcpy(ptr,&p->LinkSize,NumBytesToCopy);
  621.    ptr += NumBytesToCopy;
  622.   }
  623.  
  624.  /****************************************************************************/
  625.  /* Now, free the storage allocated for the linked list.                     */
  626.  /****************************************************************************/
  627.  p=(LINK*)pModuleList;
  628.  while( p!=NULL)
  629.  {
  630.   LINK *pnext = p->pnext;
  631.   Tfree(p);
  632.   p=pnext;
  633.  }
  634.  /****************************************************************************/
  635.  /* return the pointer to THE module table.                                  */
  636.  /****************************************************************************/
  637.  *pModuleTableLength = ModuleTableLength;
  638.  return( pModuleTable);
  639. }
  640. /*****************************************************************************/
  641. /* AddModuleLoadTables()                                                     */
  642. /*                                                                           */
  643. /* Description:                                                              */
  644. /*                                                                           */
  645. /*   Combine two load tables.                                                */
  646. /*                                                                           */
  647. /*                                                                           */
  648. /* Parameters:                                                               */
  649. /*                                                                           */
  650. /*  p1           -> to a module load table.                                  */
  651. /*  len1         length of table p1 points to.                               */
  652. /*  p2           -> to a module load table.                                  */
  653. /*  len2         length of table p2 points to.                               */
  654. /*  plen         -> to receiver of combined table length.                    */
  655. /*                                                                           */
  656. /* Return:                                                                   */
  657. /*                                                                           */
  658. /*   p           -> to the combined table.                                   */
  659. /*                                                                           */
  660. /* Assumptions:                                                              */
  661. /*                                                                           */
  662. /*****************************************************************************/
  663. UINT *AddModuleLoadTables(UINT *p1,int len1, UINT *p2,int len2 ,int *plen)
  664. {
  665.  int   len;
  666.  UINT *p;
  667.  UINT *q;
  668.  int   nbytes;
  669.  
  670.  /****************************************************************************/
  671.  /* - compute the length of the combined table. Each table contains an       */
  672.  /*   initial 4 byte field containing the number of entries in the table.    */
  673.  /*   Subtract 4 bytes to adjust the combined length.                        */
  674.  /* - allocate the combined table.                                           */
  675.  /* - put in the combined number of entries.                                 */
  676.  /* - copy the tables.                                                       */
  677.  /* - give the caller the combined length.                                   */
  678.  /* - return a pointer to the combined table.                                */
  679.  /****************************************************************************/
  680.  len = len1 + len2 - 4;
  681.  p = q = Talloc(len);
  682.  *p++ = *p1++ + *p2++;
  683.  nbytes = len1 - 4;
  684.  memcpy(p,p1,nbytes);
  685.  p = (UINT*)((UCHAR*)p + nbytes);
  686.  nbytes = len2 - 4;
  687.  memcpy(p,p2,nbytes);
  688.  *plen = len;
  689.  return(q);
  690. }
  691.  
  692. /*****************************************************************************/
  693. /* AddObjTableToList()                                                       */
  694. /*                                                                           */
  695. /* Description:                                                              */
  696. /*                                                                           */
  697. /*   Add an object table to the list of tables in the x-server.              */
  698. /*                                                                           */
  699. /* Parameters:                                                               */
  700. /*                                                                           */
  701. /*  pObjectTable    -> to the table to be added.                             */
  702. /*  mte             mte handle of exe/dll.                                   */
  703. /*                                                                           */
  704. /* Return:                                                                   */
  705. /*                                                                           */
  706. /*   void                                                                    */
  707. /*                                                                           */
  708. /* Assumptions:                                                              */
  709. /*                                                                           */
  710. /*  pObjectTable != NULL                                                     */
  711. /*                                                                           */
  712. /*****************************************************************************/
  713. static XSRVOBJTABLEENTRY *pObjTableList;
  714. void AddObjTableToList( UINT *pObjectTable, ULONG mte )
  715. {
  716.  XSRVOBJTABLEENTRY *ptr;
  717.  ULONG              NumEntries;
  718.  int                size;
  719.  
  720.  
  721.  /****************************************************************************/
  722.  /* - compute size & allocate the link.                                      */
  723.  /****************************************************************************/
  724.  NumEntries = *(ULONG *)pObjectTable;
  725.  size = sizeof(XSRVOBJTABLEENTRY) + (NumEntries-1)*sizeof(OBJTABLEENTRY);
  726.  ptr = Talloc( size );
  727.  
  728.  /****************************************************************************/
  729.  /* - copy the info to the link.                                             */
  730.  /****************************************************************************/
  731.  ptr->NumEntries = NumEntries;
  732.  ptr->mte        = mte;
  733.  memcpy( &ptr->ObjEntry , ++pObjectTable , NumEntries*sizeof(OBJTABLEENTRY) );
  734.  
  735.  /****************************************************************************/
  736.  /* - add this link to the list.                                             */
  737.  /****************************************************************************/
  738.  ptr->pnext = pObjTableList;
  739.  pObjTableList = ptr;
  740. }
  741.  
  742. void FreeObjectList( void )
  743. {
  744.  XSRVOBJTABLEENTRY *ptr;
  745.  XSRVOBJTABLEENTRY *ptrnext;
  746.  
  747.  for( ptr = pObjTableList; ptr != NULL; )
  748.  {
  749.   ptrnext=ptr->pnext;
  750.   Tfree((void*)ptr);
  751.   ptr=ptrnext;
  752.  }
  753.  pObjTableList = NULL;
  754. }
  755.  
  756. /*****************************************************************************/
  757. /* DelObjTableFromList()                                                     */
  758. /*                                                                           */
  759. /* Description:                                                              */
  760. /*                                                                           */
  761. /*   Delete an object table to the list of tables in the x-server.           */
  762. /*                                                                           */
  763. /* Parameters:                                                               */
  764. /*                                                                           */
  765. /*  mte             mte handle of exe/dll.                                   */
  766. /*                                                                           */
  767. /* Return:                                                                   */
  768. /*                                                                           */
  769. /*   void                                                                    */
  770. /*                                                                           */
  771. /* Assumptions:                                                              */
  772. /*                                                                           */
  773. /*****************************************************************************/
  774. void DelObjTableFromList( ULONG mte )
  775. {
  776.  XSRVOBJTABLEENTRY *p;
  777.  XSRVOBJTABLEENTRY *q;
  778.  
  779.  p=pObjTableList;
  780.  q=(XSRVOBJTABLEENTRY *)&pObjTableList;
  781.  for( ; p && (p->mte != mte ); q=p,p=p->pnext ){;}
  782.  
  783.  if( p )
  784.  {
  785.   q->pnext = p->pnext;
  786.   Tfree(p);
  787.  }
  788. }
  789.  
  790. /*****************************************************************************/
  791. /* _GetBitness()                                                             */
  792. /*                                                                           */
  793. /* Description:                                                              */
  794. /*                                                                           */
  795. /*   Get the 16 or 32 bitness of the address parameter.                      */
  796. /*                                                                           */
  797. /* Parameters:                                                               */
  798. /*                                                                           */
  799. /*   addr       address.                                                     */
  800. /*                                                                           */
  801. /* Return:                                                                   */
  802. /*   BIT32      address is 32 bit.                                           */
  803. /*   BIT16      address is 16 bit.                                           */
  804. /*   BITUNKNOWN can't determine.                                             */
  805. /*                                                                           */
  806. /*****************************************************************************/
  807. UCHAR _GetBitness( ULONG addr )
  808. {
  809.  XSRVOBJTABLEENTRY *ptr;
  810.  ULONG              NumEntries;
  811.  OBJTABLEENTRY     *te;
  812.  int                i;
  813.  
  814.  ptr = pObjTableList;
  815.  for( ; ptr; ptr=ptr->pnext )
  816.  {
  817.   NumEntries = ptr->NumEntries;
  818.   if ( NumEntries == 0 )
  819.    continue;
  820.   te = (OBJTABLEENTRY *)&ptr->ObjEntry;
  821.   for(i=1; i <= NumEntries; i++,te++ )
  822.   {
  823.    if( addr >= te->ObjLoadAddr &&
  824.        addr <  te->ObjLoadAddr + te->ObjLoadSize )
  825.     return(te->ObjBitness);
  826.   }
  827.  }
  828.  return(BITUNKNOWN);
  829. }
  830.  
  831. /*****************************************************************************/
  832. /* GetBaseAddr()                                                             */
  833. /*                                                                           */
  834. /* Description:                                                              */
  835. /*                                                                           */
  836. /*   Get the 16 or 32 bitness of the address parameter.                      */
  837. /*                                                                           */
  838. /* Parameters:                                                               */
  839. /*                                                                           */
  840. /*   addr       address.                                                     */
  841. /*                                                                           */
  842. /* Return:                                                                   */
  843. /*                                                                           */
  844. /*   BaseAddr   base address of the object containing the addr.              */
  845. /*                                                                           */
  846. /*****************************************************************************/
  847. ULONG GetBaseAddr( ULONG addr )
  848. {
  849.  XSRVOBJTABLEENTRY *ptr;
  850.  ULONG              NumEntries;
  851.  OBJTABLEENTRY     *te;
  852.  int                i;
  853.  
  854.  ptr = pObjTableList;
  855.  for( ; ptr; ptr=ptr->pnext )
  856.  {
  857.   NumEntries = ptr->NumEntries;
  858.   if ( NumEntries == 0 )
  859.    continue;
  860.   te = (OBJTABLEENTRY *)&ptr->ObjEntry;
  861.   for(i=1; i <= NumEntries; i++,te++ )
  862.   {
  863.    if( addr >= te->ObjLoadAddr &&
  864.        addr <  te->ObjLoadAddr + te->ObjLoadSize )
  865.     return(te->ObjLoadAddr);
  866.   }
  867.  }
  868.  return(0);
  869. }
  870.  
  871. /*****************************************************************************/
  872. /* _GetLoadAddr()                                                            */
  873. /*                                                                           */
  874. /* Description:                                                              */
  875. /*                                                                           */
  876. /*   Get the load address of an object number in an mte.                     */
  877. /*                                                                           */
  878. /* Parameters:                                                               */
  879. /*                                                                           */
  880. /*   mte          input - mte of the module the object is in.                */
  881. /*   ObjectNumber input - the object number. duh.                            */
  882. /*                                                                           */
  883. /* Return:                                                                   */
  884. /*                                                                           */
  885. /*   The base address of the Object or 0 if the ObjNum is not in the table.  */
  886. /*                                                                           */
  887. /* Assumptions:                                                              */
  888. /*                                                                           */
  889. /*****************************************************************************/
  890. ULONG _GetLoadAddr( ULONG mte, UINT ObjectNumber)
  891. {
  892.  XSRVOBJTABLEENTRY *ptr;
  893.  ULONG              NumEntries;
  894.  OBJTABLEENTRY     *te;
  895.  int                i;
  896.  ULONG              LoadAddr = 0;
  897.  
  898.  /****************************************************************************/
  899.  /* - scan to the object tables for this mte.                                */
  900.  /****************************************************************************/
  901.  for(ptr = pObjTableList; ptr && (ptr->mte != mte); ptr=ptr->pnext ){;}
  902.  
  903.  /****************************************************************************/
  904.  /* - scan the object table and get the ObjectNumber.                        */
  905.  /****************************************************************************/
  906.  if( ptr &&
  907.      (ptr->mte == mte) &&
  908.      (NumEntries = ptr->NumEntries)
  909.    )
  910.  {
  911.   te = (OBJTABLEENTRY *)(&ptr->ObjEntry);
  912.   for(i=1; i <= NumEntries; i++,te++ )
  913.   {
  914.    if( te->ObjNum == ObjectNumber )
  915.    {
  916.     LoadAddr = te->ObjLoadAddr;
  917.     break;
  918.    }
  919.   }
  920.  }
  921.  return( LoadAddr );
  922. }
  923.  
  924. /*****************************************************************************/
  925. /* GetPtrToObjectTable( ULONG mte )                                          */
  926. /*                                                                           */
  927. /* Description:                                                              */
  928. /*                                                                           */
  929. /*   Get a pointer to the object table for an mte. Skip past the             */
  930. /*   NumEntries field.                                                       */
  931. /*                                                                           */
  932. /* Parameters:                                                               */
  933. /*                                                                           */
  934. /*   mte          mte of the module the object is in.                        */
  935. /*                                                                           */
  936. /* Return:                                                                   */
  937. /*                                                                           */
  938. /*   te           -> to object table for this mte.                           */
  939. /*                                                                           */
  940. /* Assumptions:                                                              */
  941. /*                                                                           */
  942. /*****************************************************************************/
  943. OBJTABLEENTRY *GetPtrToObjectTable( ULONG mte )
  944. {
  945.  XSRVOBJTABLEENTRY *ptr;
  946.  OBJTABLEENTRY     *te = NULL;
  947.  
  948.  /****************************************************************************/
  949.  /* - scan to the object tables for this mte.                                */
  950.  /****************************************************************************/
  951.  for(ptr = pObjTableList; ptr && (ptr->mte != mte); ptr=ptr->pnext ){;}
  952.  
  953.  /****************************************************************************/
  954.  /* - scan the object table and get the ObjectNumber.                        */
  955.  /****************************************************************************/
  956.  if(  ptr &&
  957.      (ptr->mte == mte) &&
  958.       ptr->NumEntries
  959.    )
  960.  {
  961.   te = (OBJTABLEENTRY *)(&ptr->ObjEntry);
  962.  }
  963.  return( te );
  964. }
  965.  
  966. /*****************************************************************************/
  967. /* CoalesceTables( )                                                         */
  968. /*                                                                           */
  969. /* Description:                                                              */
  970. /*                                                                           */
  971. /*   Coalesce two object tables.                                             */
  972. /*                                                                           */
  973. /* Parameters:                                                               */
  974. /*                                                                           */
  975. /*   pModuleLoadTableAccum     previously coalesced table.                   */
  976. /*   ModuleTableLengthAccum    previously coalesced table length.            */
  977. /*   pModuleLoadTable          table to be coalesced.                        */
  978. /*   ModuleTableLength,        table to be coalesced length.                 */
  979. /*   pModuleTableLengthAccum   length of the resultant table.                */
  980. /*                                                                           */
  981. /* Return:                                                                   */
  982. /*                                                                           */
  983. /*   pModuleLoadTableAccum     ->to resultant table.                         */
  984. /*                                                                           */
  985. /* Assumptions:                                                              */
  986. /*                                                                           */
  987. /*****************************************************************************/
  988. UINT *CoalesceTables( UINT *pModuleLoadTableAccum,
  989.                       int   ModuleTableLengthAccum,
  990.                       UINT *pModuleLoadTable,
  991.                       int   ModuleTableLength,
  992.                       int  *pModuleTableLengthAccum )
  993. {
  994.  int   length = 0;
  995.  UINT *p;
  996.  
  997.  if(pModuleLoadTableAccum == NULL)
  998.  {
  999.   pModuleLoadTableAccum = pModuleLoadTable;
  1000.   length= ModuleTableLength;
  1001.  }
  1002.  else
  1003.  {
  1004.   /*************************************************************************/
  1005.   /* - coalesce the load tables.                                           */
  1006.   /*************************************************************************/
  1007.   p =pModuleLoadTableAccum;
  1008.   pModuleLoadTableAccum = AddModuleLoadTables( pModuleLoadTableAccum,
  1009.                                                ModuleTableLengthAccum,
  1010.                                                pModuleLoadTable,
  1011.                                                ModuleTableLength,
  1012.                                                &length );
  1013.   Tfree(pModuleLoadTable);
  1014.   Tfree(p);
  1015.  }
  1016.  *pModuleTableLengthAccum = length;
  1017.  return(pModuleLoadTableAccum);
  1018. }
  1019.