delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2002/11/29/18:30:25

From: sandmann AT clio DOT rice DOT edu (Charles Sandmann)
Message-Id: <10211292332.AA12905@clio.rice.edu>
Subject: Re: DXE enhancements
To: muller AT cerbere DOT u-strasbg DOT fr (Pierre Muller)
Date: Fri, 29 Nov 2002 17:32:22 -0600 (CST)
Cc: djgpp-workers AT delorie DOT com (DJGPP developers)
In-Reply-To: <5.0.2.1.2.20021129103942.0323ebe8@ics.u-strasbg.fr> from "Pierre Muller" at Nov 29, 2002 11:26:17 AM
X-Mailer: ELM [version 2.5 PL2]
Mime-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

> Yes, sorry, but I tried to patch the dxegen.c that is on CVS tree, but the 
> patch failed at places...

Here's the updated dxegen patch, should be versus the right dxegen.c now.

I've fixed a few minor things, made it a little simpler.  At the bottom,
below the patch, are 3 files which I was using for testing.  The imports
stuff is behaving pretty well - I'm going to look at multiple symbol exports 
now.

There are some simple utilities to be added (such as dxe2dxi) which will
look at a DXE and make what you need to link to it.  I'm waiting on this
piece, since doing multiple exports may just include that in the same piece.

The final step is to consider making pieces of libc an optional DXE, 
which will require some cleanup (breaking up) of some of our mega-modules.


*** dxegen.c	Fri Nov 29 10:58:00 2002
--- dxegen5.c	Fri Nov 29 16:11:46 2002
*************** int main(int argc, char **argv)
*** 71,74 ****
--- 71,87 ----
    size_t i;
    dxe_header dh;
+   int num_import, num_export = 0;
+   long *import_symndx = NULL;
+   char **import_name = NULL;
+ 
+   if (argc >= 2 && strcmp(argv[1], "-i") == 0)
+   {
+     argv++;
+     argc--;
+     num_import = 0;	/* New option to allow imports */
+     import_name = malloc(sizeof(char *));
+   }
+   else
+     num_import = -1;	/* No imports allowed */
  
    if (argc < 4)
*************** int main(int argc, char **argv)
*** 89,93 ****
    if (fh.f_nscns != 1 || argc > 4)
    {
!     char command[1024], *libdir;
      fclose(input_f);
  
--- 102,109 ----
    if (fh.f_nscns != 1 || argc > 4)
    {
!     char *command, *libdir;
!     unsigned cmdmax = 1024;
!     command = (char *)malloc(cmdmax);
! 
      fclose(input_f);
  
*************** int main(int argc, char **argv)
*** 115,127 ****
      for(i=3;argv[i];i++)
      {
        strcat(command, argv[i]);
        strcat(command, " ");
      }
      strcat(command," -T dxe.ld ");
!       
      printf("%s\n",command);
      i = system(command);
      if(i)
        return i;
  
      input_f = fopen("dxe__tmp.o", "rb");
--- 131,153 ----
      for(i=3;argv[i];i++)
      {
+       if(strlen(command) + strlen(argv[i]) + 2 > cmdmax) {
+         cmdmax += 1024;
+         command = realloc(command, cmdmax);
+       }
        strcat(command, argv[i]);
        strcat(command, " ");
      }
+ 
+     if(strlen(command) + 12 > cmdmax) {
+       cmdmax += 12;
+       command = realloc(command, cmdmax);
+     }
      strcat(command," -T dxe.ld ");
! 
      printf("%s\n",command);
      i = system(command);
      if(i)
        return i;
+     free(command);
  
      input_f = fopen("dxe__tmp.o", "rb");
*************** int main(int argc, char **argv)
*** 161,164 ****
--- 187,203 ----
    strings = malloc(strsz);
    fread(strings+4, 1, strsz-4, input_f);
+ 
+   relocs = malloc(sizeof(RELOC)*sc.s_nreloc);
+   fseek(input_f, sc.s_relptr, 0);
+   fread(relocs, sc.s_nreloc, RELSZ, input_f);
+ #if 0
+   /* Don't swap - it's in i386 order already */
+   for (i=0; i<sc.s_nreloc; i++)
+     dosswap(relocs+i, "lls");
+ #endif
+ 
+   fclose(input_f);
+   import_symndx = calloc(fh.f_nsyms, sizeof(long));
+ 
    strings[0] = 0;
    for (i=0; i<fh.f_nsyms; i++)
*************** int main(int argc, char **argv)
*** 191,196 ****
      if (sym[i].e_scnum == 0)
      {
!       printf("Error: object contains unresolved external symbols (%s)\n", name);
!       errors ++;
      }
      if (strncmp(name, argv[2], strlen(argv[2])) == 0)
--- 230,276 ----
      if (sym[i].e_scnum == 0)
      {
!       if (num_import < 0)
!       {
!         printf("Error: object contains unresolved external symbols (%s)\n", name);
!         errors ++;
!       }
!       else
!       {
!         import_symndx[i] = ++num_import;
!         import_name = realloc(import_name, num_import * sizeof(char *));
!         import_name[num_import - 1] = strdup(name);
! #if 0
!         {
!         int j;
!         for (j=0; j<sc.s_nreloc; j++)
!           if (relocs[j].r_symndx == i)
!           {
!             printf("import %s [%ld]", name, i);
!             if(relocs[j].r_type == 0x14)
!               printf("relative\n");
!             else
!               printf("absolute\n");
!             break;
!           }
!         }
! #endif
!       }
!     }
!     else if (sym[i].e_sclass == 2)
!     {
! #if 0
!       int j;
!       for (j=0; j<sc.s_nreloc; j++)
!         if(relocs[j].r_symndx == i)
!         {
!           printf("export %s ",name);
!           if(relocs[j].r_type == 0x14)
!             printf("relative\n");
!           else
!             printf("absolute\n");
!           break;
!         }
! #endif
!       num_export++;
      }
      if (strncmp(name, argv[2], strlen(argv[2])) == 0)
*************** int main(int argc, char **argv)
*** 216,227 ****
    }
  
-   relocs = malloc(sizeof(RELOC)*sc.s_nreloc);
-   fseek(input_f, sc.s_relptr, 0);
-   fread(relocs, sc.s_nreloc, RELSZ, input_f);
- #if 0
-   /* Don't swap - it's in i386 order already */
-   for (i=0; i<sc.s_nreloc; i++)
-     dosswap(relocs+i, "lls");
- #endif
  #if 0
    /* Thus, this won't work except on PCs */
--- 296,299 ----
*************** int main(int argc, char **argv)
*** 235,239 ****
  #endif
  
-   fclose(input_f);
    if (errors)
      return errors;
--- 307,310 ----
*************** int main(int argc, char **argv)
*** 247,251 ****
  
    for (i=0; i<sc.s_nreloc; i++)
!     if(*(char *)(&relocs[i].r_type) == 0x14)	/* Don't do these, they are relative */
        dh.nrelocs--;
  
--- 318,323 ----
  
    for (i=0; i<sc.s_nreloc; i++)
!     if(sym[relocs[i].r_symndx].e_scnum == 0 ||  /* Imports */
!      *(char *)(&relocs[i].r_type) == 0x14)      /* Don't do these, they are relative */
        dh.nrelocs--;
  
*************** int main(int argc, char **argv)
*** 254,260 ****
    fwrite(data, 1, sc.s_size, output_f);
    for (i=0; i<sc.s_nreloc; i++)
!     if(*(char *)(&relocs[i].r_type) != 0x14)	/* Don't do these, they are relative */
        fwrite(&(relocs[i].r_vaddr), 1, sizeof(long), output_f);
  
    fclose(output_f);
    return 0;
--- 326,370 ----
    fwrite(data, 1, sc.s_size, output_f);
    for (i=0; i<sc.s_nreloc; i++)
!     if(sym[relocs[i].r_symndx].e_scnum != 0 &&  /* Imports */
!      *(char *)(&relocs[i].r_type) != 0x14)      /* Don't do these, they are relative */
        fwrite(&(relocs[i].r_vaddr), 1, sizeof(long), output_f);
  
+   /* New Imports Section */
+   if (num_import > 0)
+   {
+     for (i=0; i<sc.s_nreloc; i++)
+       if (sym[relocs[i].r_symndx].e_scnum == 0)
+       {
+         long windx = import_symndx[relocs[i].r_symndx];
+         if(*(char *)(&relocs[i].r_type) != 0x14)
+           windx = -windx;
+         /* printf("imprec: %ld 0x%lx\n", windx, relocs[i].r_vaddr); */
+         dosswap(&windx, "l");
+         fwrite(&windx, 1, sizeof(long), output_f);
+         fwrite(&(relocs[i].r_vaddr), 1, sizeof(long), output_f);
+       }
+ 
+     i = 0;
+     fwrite(&i, 1, sizeof(long), output_f);		/* End of list */
+     for (i=0; i<num_import; i++)
+       fwrite(import_name[i], strlen(import_name[i])+1, 1, output_f);
+ 
+     fclose(output_f);
+ 
+     *(argv[1]+strlen(argv[1])-1)='i';		/* .dxe -> .dxi */
+     output_f = fopen(argv[1], "w");
+     if (!output_f)
+     {
+       perror(argv[1]);
+       return 1;
+     }
+ 
+     fprintf(output_f, "static long dxefixup[] = {\n");
+     for (i=0; i<num_import; i++)
+       fprintf(output_f, "(long)&%s,\n", import_name[i]+1);
+     fprintf(output_f, "0 };\n");
+     printf("Required imports written to %s\n",argv[1]);
+   }
+ 
    fclose(output_f);
    return 0;



dxetest.bat ---------------------------------------------------------------
gcc -c -O2 -Wall dxesub.c
dxegen -i dxesub.dxe _dxesub dxesub.o
del dxesub.o
gcc -O2 -Wall dxemain.c dxeload.c -o dxemain.exe
dxemain


dxesub.c -------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>

extern int dxeocount;

int dxeicount;

char *dxesub(void)
{
  char *t;
  t = malloc(1);
  printf("malloc(1)=%p\n",t);
  dxeicount++;
  dxeocount++;
  *t=dxeicount;
  printf("leaving dxesub\n");
  return t;
}


dxemain.c -----------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/dxe.h>

static char* (*dxesub)(void);

int dxeocount;

extern long *_dxe_fixup;

#include "dxesub.dxi"		/* Created by dxegen -i (contains dxefixup) */

int main(void)
{
  char *t;
  int i;
  printf("loading dxesub.dxe...\n");
  _dxe_fixup = &dxefixup[0];
  dxesub = _dxe_load("dxesub.dxe");
  if (dxesub == 0) {
    printf("Cannot load dxesub.dxe\n");
    return 1;
  }
  for(i=0;i<5;i++) {
    t = dxesub();
    printf("return[%d] = %d, cnt = %d\n",i,t[0],dxeocount++);
  }
  return 0;
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019