Changeset 915


Ignore:
Timestamp:
Jan 2, 2004, 2:32:54 AM (22 years ago)
Author:
bird
Message:

Added support for .dll's.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/emx/src/emximp/emximp.c

    • Property cvs2svn:cvs-rev changed from 1.7 to 1.8
    r914 r915  
    3030#include <time.h>
    3131#include <sys/moddef.h>
     32#include <sys/types.h>
    3233#include <a_out.h>
    3334#include "defs.h"
     
    6768  M_DEF_TO_IMP,                 /* .def -> .imp */
    6869  M_DEF_TO_A,                   /* .def -> .a */
    69   M_DEF_TO_LIB                  /* .def -> .lib */
     70  M_DEF_TO_LIB,                 /* .def -> .lib */
     71  M_DLL_TO_IMP,                 /* .dll -> .imp */
     72  M_DLL_TO_A,                   /* .dll -> .a */
     73  M_DLL_TO_LIB,                 /* .dll -> .lib */
     74  M_DLL_TO_DEF                  /* .dll -> .def */
    7075};
    7176
     
    9095static char lib_errmsg[512];
    9196static char *module_name = NULL;
     97static char *description;
    9298
    9399
     
    109115  puts ("  emximp [-m] -o <output_file>.a <input_file>.imp ...");
    110116  puts ("  emximp [-m] -o <output_file>.a <input_file>.lib ...");
    111   puts ("  emximp -o <output_file>.def <input_file>.imp ...");
     117  puts ("  emximp [-m] -o <output_file>.a <input_file>.dll ...");
    112118  puts ("  emximp -o <output_file>.imp <input_file>.def ...");
    113119  puts ("  emximp -o <output_file>.imp <input_file>.lib ...");
     120  puts ("  emximp -o <output_file>.imp <input_file>.dll ...");
    114121  puts ("  emximp [-p#] -o <output_file>.lib <input_file>.def ...");
    115122  puts ("  emximp [-p#] -o <output_file>.lib <input_file>.imp...");
     123  puts ("  emximp [-p#] -o <output_file>.lib <input_file>.dll ...");
     124  puts ("  emximp -o <output_file>.def <input_file>.imp ...");
     125  puts ("  emximp -o <output_file>.def <input_file>.dll ...");
    116126  puts ("Options:");
    117127  puts ("  -p#  Set page size");
     
    10321042}
    10331043
     1044/**
     1045 * Reads a LX nametable.
     1046 */
     1047static void dll_read_names(FILE *phFile, off_t off, int fResident, int fOrdinal0Only, const char *pszFilename)
     1048{
     1049    char            szName[256];
     1050    unsigned        cchName;
     1051    unsigned        iOrd = 0;
     1052
     1053    if (fseek(phFile, off, SEEK_SET))
     1054        error("`%s': Bad LX format\n", pszFilename);
     1055
     1056
     1057    /* first name is module name / description. */
     1058    cchName = (unsigned)fgetc(phFile);
     1059    if (!cchName)
     1060        error("`%s': Bad LX format\n", pszFilename);
     1061    iOrd = 0;
     1062    if (    fread(szName, 1, cchName, phFile) != cchName
     1063        ||  fread(&iOrd, 1, 2, phFile) != 2)
     1064        error("`%s': Bad LX format\n", pszFilename);
     1065    if (iOrd != 0)
     1066        error("`%s': Bad LX format - expect 0 as first ordinal, found %d\n", pszFilename, iOrd);
     1067
     1068    szName[cchName] = '\0';
     1069    if (fResident)
     1070        module_name = xstrdup(szName);
     1071    else
     1072        description = xstrdup(szName);
     1073
     1074    if (fOrdinal0Only)
     1075        return;
     1076
     1077    /* The rest of the names are export names. */
     1078    for (;;)
     1079    {
     1080        cchName = (unsigned)fgetc(phFile);
     1081        if (!cchName)
     1082            break;
     1083        iOrd = 0;
     1084        if (    fread(szName, 1, cchName, phFile) != cchName
     1085            ||  fread(&iOrd, 1, 2, phFile) != 2)
     1086            error("`%s': Bad LX format\n", pszFilename);
     1087        szName[cchName] = '\0';
     1088
     1089        switch (mode)
     1090        {
     1091            case M_DLL_TO_IMP:
     1092                if (!fResident)
     1093                    fprintf(out_file, "%-23s %-8s %3u ?\n",   szName, module_name, iOrd);
     1094                else
     1095                    fprintf(out_file, "%-23s %-8s %-23s ?\n", szName, module_name, szName);
     1096                if (ferror(out_file))
     1097                    write_error(out_fname);
     1098                break;
     1099            case M_DLL_TO_A:
     1100                if (!fResident)
     1101                    write_a_import(szName, module_name, iOrd, NULL);
     1102                else
     1103                    write_a_import(szName, module_name, 0, szName);
     1104                break;
     1105            case M_DLL_TO_LIB:
     1106                if (!fResident)
     1107                    write_lib_import(szName, module_name, iOrd, NULL);
     1108                else
     1109                    write_lib_import(szName, module_name, 0, szName);
     1110                break;
     1111            case M_DLL_TO_DEF:
     1112                if (!fResident)
     1113                    fprintf(out_file, "  \"%s\" @%d\n", szName, iOrd);
     1114                else
     1115                    fprintf(out_file, "  \"%s\"\n", szName);
     1116                if (ferror(out_file))
     1117                    write_error(out_fname);
     1118                break;
     1119            default:
     1120                abort ();
     1121        }
     1122    } /* read restable loop */
     1123}
     1124
     1125/** reads a dll */
     1126static void read_dll(const char *pszFilename)
     1127{
     1128#pragma pack(1)
     1129#define EMAGIC 0x5A4D
     1130    struct
     1131    {
     1132        unsigned short e_magic;
     1133        unsigned short e_cblp;
     1134        unsigned short e_cp;
     1135        unsigned short e_crlc;
     1136        unsigned short e_cparhdr;
     1137        unsigned short e_minalloc;
     1138        unsigned short e_maxalloc;
     1139        unsigned short e_ss;
     1140        unsigned short e_sp;
     1141        unsigned short e_csum;
     1142        unsigned short e_ip;
     1143        unsigned short e_cs;
     1144        unsigned short e_lfarlc;
     1145        unsigned short e_ovno;
     1146        unsigned long  e_sym_tab;
     1147        unsigned short e_flags;
     1148        unsigned short e_res;
     1149        unsigned short e_oemid;
     1150        unsigned short e_oeminfo;
     1151        unsigned short e_res2[10];
     1152        unsigned long  e_lfanew;
     1153    }           doshdr;
     1154
     1155#define E32MAGIC    0x584c /* LX */
     1156#define E32MODDLL        0x08000L
     1157#define E32MODPROTDLL    0x18000L
     1158#define E32MODMASK       0x38000L
     1159#define E32LIBINIT       0x0004L
     1160#define E32LIBTERM       0x40000000L
     1161    struct
     1162    {
     1163        unsigned char   e32_magic[2];
     1164        unsigned char   e32_border;
     1165        unsigned char   e32_worder;
     1166        unsigned long   e32_level;
     1167        unsigned short  e32_cpu;
     1168        unsigned short  e32_os;
     1169        unsigned long   e32_ver;
     1170        unsigned long   e32_mflags;
     1171        unsigned long   e32_mpages;
     1172        unsigned long   e32_startobj;
     1173        unsigned long   e32_eip;
     1174        unsigned long   e32_stackobj;
     1175        unsigned long   e32_esp;
     1176        unsigned long   e32_pagesize;
     1177        unsigned long   e32_pageshift;
     1178        unsigned long   e32_fixupsize;
     1179        unsigned long   e32_fixupsum;
     1180        unsigned long   e32_ldrsize;
     1181        unsigned long   e32_ldrsum;
     1182        unsigned long   e32_objtab;
     1183        unsigned long   e32_objcnt;
     1184        unsigned long   e32_objmap;
     1185        unsigned long   e32_itermap;
     1186        unsigned long   e32_rsrctab;
     1187        unsigned long   e32_rsrccnt;
     1188        unsigned long   e32_restab;
     1189        unsigned long   e32_enttab;
     1190        unsigned long   e32_dirtab;
     1191        unsigned long   e32_dircnt;
     1192        unsigned long   e32_fpagetab;
     1193        unsigned long   e32_frectab;
     1194        unsigned long   e32_impmod;
     1195        unsigned long   e32_impmodcnt;
     1196        unsigned long   e32_impproc;
     1197        unsigned long   e32_pagesum;
     1198        unsigned long   e32_datapage;
     1199        unsigned long   e32_preload;
     1200        unsigned long   e32_nrestab;
     1201        unsigned long   e32_cbnrestab;
     1202        unsigned long   e32_nressum;
     1203        unsigned long   e32_autodata;
     1204        unsigned long   e32_debuginfo;
     1205        unsigned long   e32_debuglen;
     1206        unsigned long   e32_instpreload;
     1207        unsigned long   e32_instdemand;
     1208        unsigned long   e32_heapsize;
     1209        unsigned long   e32_stacksize;
     1210        unsigned char   e32_res3[20];
     1211    }           os2hdr;
     1212#pragma pack()
     1213    off_t       offLX;
     1214    FILE       *phFile = fopen(pszFilename, "rb");
     1215
     1216    if (!phFile)
     1217        error("Cannot open input file `%s'", pszFilename);
     1218
     1219    /* stub header */
     1220    if (fread(&doshdr, 1, sizeof(doshdr), phFile) != sizeof(doshdr))
     1221        error("Error read error while reading `%s'\n", pszFilename);
     1222    if (doshdr.e_magic != EMAGIC && doshdr.e_magic != E32MAGIC)
     1223        error("`%s' is not a LX DLL\n", pszFilename);
     1224    offLX = doshdr.e_magic == EMAGIC ? doshdr.e_lfanew : 0;
     1225
     1226    /* LX header */
     1227    if (fseek(phFile, offLX, SEEK_SET))
     1228        error("Bad LX .DLL `%s'\n", pszFilename);
     1229    if (fread(&os2hdr, 1, sizeof(os2hdr), phFile) != sizeof(os2hdr))
     1230        error("Error read error while reading `%s'\n", pszFilename);
     1231    if (*(unsigned short*)os2hdr.e32_magic != E32MAGIC || !os2hdr.e32_restab)
     1232        error("`%s': Bad LX format\n", pszFilename);
     1233    if (   (os2hdr.e32_mflags & E32MODMASK) != E32MODDLL
     1234        && (os2hdr.e32_mflags & E32MODMASK) != E32MODPROTDLL)
     1235        error("`%s': Not a LX DLL\n", pszFilename);
     1236
     1237    /* Read the tables. */
     1238    if (mode == M_DLL_TO_DEF && !first_module)
     1239    {
     1240        dll_read_names(phFile, os2hdr.e32_restab + offLX, 1, 1, pszFilename);
     1241        dll_read_names(phFile, os2hdr.e32_nrestab, 0, 1, pszFilename);
     1242
     1243        first_module = xstrdup(module_name);
     1244        fprintf(out_file, "LIBRARY %s", first_module);
     1245        fprintf(out_file, (os2hdr.e32_mflags & E32LIBINIT) ? " INITINSTANCE" : " INITGLOBAL");
     1246        fprintf(out_file, (os2hdr.e32_mflags & E32LIBTERM) ? " TERMINSTANCE" : " TERMGLOBAL");
     1247        fprintf(out_file, "\n");
     1248        if (description)
     1249            fprintf(out_file, "DESCRIPTION '%s'\n", description);
     1250        fprintf(out_file, "EXPORTS\n");
     1251        if (ferror(out_file))
     1252          write_error(out_fname);
     1253    }
     1254    dll_read_names(phFile, os2hdr.e32_restab + offLX, 1, 0, pszFilename);
     1255    if (os2hdr.e32_nrestab)
     1256        dll_read_names(phFile, os2hdr.e32_nrestab, 0, 0, pszFilename);
     1257
     1258    fclose(phFile);
     1259}
     1260
    10341261
    10351262int main (int argc, char *argv[])
    10361263{
    10371264  int i, c;
    1038   int imp_count, lib_count, def_count, page_size;
     1265  int imp_count, lib_count, def_count, dll_count, page_size;
    10391266  struct predef *pp1;
    10401267  char *q, *ext, *opt_o;
     
    10901317        }
    10911318    }
    1092   imp_count = 0; lib_count = 0; def_count = 0;
     1319  imp_count = 0; lib_count = 0; def_count = 0; dll_count = 0;
    10931320  for (i = optind; i < argc; ++i)
    10941321    {
     
    11001327      else if (ext != NULL && stricmp (ext, ".def") == 0)
    11011328        ++def_count;
     1329      else if (ext != NULL && stricmp (ext, ".dll") == 0)
     1330        ++dll_count;
    11021331      else
    11031332        error ("Input file `%s' has unknown file name extension", argv[i]);
    11041333    }
    1105   if (imp_count == 0 && lib_count == 0 && def_count == 0)
     1334  if (imp_count == 0 && lib_count == 0 && def_count == 0 && dll_count == 0)
    11061335    usage ();
    1107   if ((imp_count != 0) + (lib_count != 0) + (def_count != 0) > 1)
     1336  if ((imp_count != 0) + (lib_count != 0) + (def_count != 0) + (dll_count != 0) > 1)
    11081337    error ("More than one type of input files");
    11091338  if (opt_o == NULL)
     
    11151344        error ("Cannot convert .def files to %s files",
    11161345               (as_name == NULL ? ".s" : ".o"));
     1346      if (dll_count != 0)
     1347        error ("Cannot convert .dll files to %s files",
     1348               (as_name == NULL ? ".s" : ".o"));
    11171349      mode = M_IMP_TO_S;
    11181350    }
     
    11241356          if (imp_count != 0)
    11251357            error ("Cannot convert .imp files to .imp file");
    1126           if (lib_count != 0)
     1358          if (dll_count != 0)
     1359            mode = M_DLL_TO_IMP;
     1360          else if (lib_count != 0)
    11271361            mode = M_LIB_TO_IMP;
    11281362          else
     
    11311365      else if (ext != NULL && stricmp (ext, ".a") == 0)
    11321366        {
    1133           if (def_count != 0)
     1367          if (dll_count != 0)
     1368            mode = M_DLL_TO_A;
     1369          else if (def_count != 0)
    11341370            mode = M_DEF_TO_A;
    11351371          else if (imp_count != 0)
     
    11441380          if (lib_count != 0)
    11451381            error ("Cannot convert .lib files to .def file");
    1146           mode = M_IMP_TO_DEF;
     1382          if (dll_count != 0)
     1383            mode = M_DLL_TO_DEF;
     1384          else
     1385            mode = M_IMP_TO_DEF;
    11471386        }
    11481387      else if (ext != NULL && stricmp (ext, ".lib") == 0)
     
    11501389          if (lib_count != 0)
    11511390            error ("Cannot convert .lib files to .lib file");
    1152           if (def_count != 0)
     1391          if (dll_count != 0)
     1392            mode = M_DLL_TO_LIB;
     1393          else if (def_count != 0)
    11531394            mode = M_DEF_TO_LIB;
    11541395          else
     
    11721413      usage ();
    11731414  if (profile_flag && mode != M_DEF_TO_A && mode != M_IMP_TO_A
    1174       && mode != M_LIB_TO_A)
     1415      && mode != M_LIB_TO_A && mode != M_DEF_TO_A)
    11751416    usage ();
    11761417  switch (mode)
     
    12431484        lib_error ();
    12441485      break;
     1486    case M_DLL_TO_A:
     1487      create_output_file (TRUE);
     1488      init_archive ();
     1489      for (i = optind; i < argc; ++i)
     1490        read_dll (argv[i]);
     1491      close_output_file ();
     1492      break;
     1493    case M_DLL_TO_IMP:
     1494      create_output_file (FALSE);
     1495      for (i = optind; i < argc; ++i)
     1496        read_dll (argv[i]);
     1497      close_output_file ();
     1498      break;
     1499    case M_DLL_TO_LIB:
     1500      out_lib = omflib_create (out_fname, page_size, lib_errmsg);
     1501      if (out_lib == NULL)
     1502        lib_error ();
     1503      if (omflib_header (out_lib, lib_errmsg) != 0)
     1504        lib_error ();
     1505      for (i = optind; i < argc; ++i)
     1506        read_dll (argv[i]);
     1507      if (omflib_finish (out_lib, lib_errmsg) != 0
     1508          || omflib_close (out_lib, lib_errmsg) != 0)
     1509        lib_error ();
     1510      break;
     1511
     1512    case M_DLL_TO_DEF:
     1513      create_output_file (FALSE);
     1514      for (i = optind; i < argc; ++i)
     1515        read_dll (argv[i]);
     1516      close_output_file ();
     1517      break;
     1518
    12451519    default:
    12461520      usage ();
Note: See TracChangeset for help on using the changeset viewer.