Changeset 542


Ignore:
Timestamp:
Aug 7, 2003, 2:20:12 PM (22 years ago)
Author:
bird
Message:

Latest hacking.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/emx/src/emxomf/weakld.c

    • Property cvs2svn:cvs-rev changed from 1.17 to 1.18
    r541 r542  
    28982898
    28992899
     2900
     2901/**
     2902 * Write the start of an weak alias object.
     2903 *
     2904 * @returns 0 on success
     2905 * @returns -1 on failure.
     2906 * @param   pWld    Linker instance.
     2907 * @param   phFile  Output file.
     2908 * @param   pszName Object name.
     2909 * @remark  Link386 accepts anything as long as THEADR/LHEADR is there.
     2910 *          VACxxx want's more stuff.
     2911 */
     2912static int  wldObjStart(PWLD pWld, FILE *phFile, const char *pszName)
     2913{
     2914    #pragma pack(1)
     2915    struct omfstuff
     2916    {
     2917        OMFREC      hdr;
     2918        char        ach[128];
     2919    }       omf;
     2920    #pragma pack()
     2921    int     cch;
     2922#if 0
     2923    int     i;
     2924    static const char * apszLNAMEs[] =
     2925    {
     2926        "",                             /* 1 */
     2927        "TEXT32",                       /* 2 */
     2928        "DATA32",                       /* 3 */
     2929        "BSS32",                        /* 4 */
     2930        "CODE",                         /* 5 */
     2931        "DATA",                         /* 6 */
     2932        "BSS",                          /* 7 */
     2933        "FLAT",                         /* 8 */
     2934        "DGROUP",                       /* 9 */
     2935    };
     2936#endif
     2937
     2938    /* THEADR */
     2939    omf.hdr.chType = THEADR;
     2940    cch = strlen(pszName);
     2941    omf.hdr.cb = 2 + cch;
     2942    omf.ach[0] = cch;
     2943    memcpy(&omf.ach[1], pszName, cch);
     2944    omf.ach[cch + 1] = 0;
     2945    if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile) != 1)
     2946    {
     2947        wldErr(pWld, "Error occured while writing weak aliases. (4)");
     2948        return -1;
     2949    }
     2950
     2951    /* COMENT - IDM */
     2952    omf.hdr.chType = COMENT;
     2953    cch = strlen("GPP3DEM");
     2954    omf.hdr.cb = cch + 5;
     2955    omf.ach[0] = 0x80;                  /* flags */
     2956    omf.ach[1] = CLASS_IDMDLL;          /* class */
     2957    omf.ach[2] = cch;
     2958    memcpy(&omf.ach[3], "GPP3DEM", cch);
     2959    omf.ach[3+cch] = 0;
     2960    if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile) != 1)
     2961    {
     2962        wldErr(pWld, "Error occured while writing weak aliases. (4.1)");
     2963        return -1;
     2964    }
     2965#if 0
     2966    /* COMENT - DEBUG INFO */
     2967    omf.hdr.chType = COMENT;
     2968    omf.hdr.cb = 6;
     2969    omf.ach[0] = 0x80;                  /* flags */
     2970    omf.ach[1] = CLASS_DBGTYPE;         /* class */
     2971    omf.ach[2] = 3;
     2972    omf.ach[3] = 'H';
     2973    omf.ach[4] = 'L';
     2974    omf.ach[5] = 0;
     2975    if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile) != 1)
     2976    {
     2977        wldErr(pWld, "Error occured while writing weak aliases. (4.1)");
     2978        return -1;
     2979    }
     2980
     2981    /* LNAMES for VACxxx */
     2982    omf.hdr.chType = LNAMES;
     2983    omf.hdr.cb = 1;                     /* crc */
     2984    for (i = 0; i < sizeof(apszLNAMEs) / sizeof(apszLNAMEs[0]); i++)
     2985    {
     2986        cch = strlen(apszLNAMEs[i]);
     2987        omf.ach[omf.hdr.cb - 1] = cch;
     2988        memcpy(&omf.ach[omf.hdr.cb], apszLNAMEs[i], cch);
     2989        omf.hdr.cb += cch + 1;
     2990    }
     2991    omf.ach[omf.hdr.cb - 1] = 0;        /* crc */
     2992    if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile) != 1)
     2993    {
     2994        wldErr(pWld, "Error occured while writing weak aliases. (5)");
     2995        return -1;
     2996    }
     2997
     2998    /* SEGDEF - TEXT32 */
     2999    omf.hdr.chType = SEGDEF;
     3000    omf.hdr.cb = 7;
     3001    omf.ach[0] = 0x69;                  /* attributes */
     3002    omf.ach[1] = omf.ach[2] = 0;        /* segment size (0) */
     3003    omf.ach[3] = 2;                     /* "TEXT32" LNAME index. */
     3004    omf.ach[4] = 5;                     /* "CODE" LNAME index. */
     3005    omf.ach[5] = 1;                     /* "" LNAME index. */
     3006    omf.ach[6] = 0;                     /* crc */
     3007    if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile) != 1)
     3008    {
     3009        wldErr(pWld, "Error occured while writing weak aliases. (6)");
     3010        return -1;
     3011    }
     3012
     3013    /* SEGDEF - DATA32 */
     3014    omf.hdr.chType = SEGDEF;
     3015    omf.hdr.cb = 7;
     3016    omf.ach[0] = 0x69;                  /* attributes */
     3017    omf.ach[1] = omf.ach[2] = 0;        /* segment size (0) */
     3018    omf.ach[3] = 3;                     /* "TEXT32" LNAME index. */
     3019    omf.ach[4] = 6;                     /* "CODE" LNAME index. */
     3020    omf.ach[5] = 1;                     /* "" LNAME index. */
     3021    omf.ach[6] = 0;                     /* crc */
     3022    if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile) != 1)
     3023    {
     3024        wldErr(pWld, "Error occured while writing weak aliases. (6)");
     3025        return -1;
     3026    }
     3027
     3028    /* SEGDEF - BSS32 */
     3029    omf.hdr.chType = SEGDEF;
     3030    omf.hdr.cb = 7;
     3031    omf.ach[0] = 0x69;                  /* attributes */
     3032    omf.ach[1] = omf.ach[2] = 0;        /* segment size (0) */
     3033    omf.ach[3] = 4;                     /* "TEXT32" LNAME index. */
     3034    omf.ach[4] = 6;                     /* "CODE" LNAME index. */
     3035    omf.ach[5] = 1;                     /* "" LNAME index. */
     3036    omf.ach[6] = 0;                     /* crc */
     3037    if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile) != 1)
     3038    {
     3039        wldErr(pWld, "Error occured while writing weak aliases. (6)");
     3040        return -1;
     3041    }
     3042
     3043    /* GROUP - FLAT */
     3044    omf.hdr.chType = GRPDEF;
     3045    omf.hdr.cb = 2;
     3046    omf.ach[0] = 8;                     /* "FLAT" LNAME index */
     3047    omf.ach[1] = 0;                     /* crc */
     3048    if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile) != 1)
     3049    {
     3050        wldErr(pWld, "Error occured while writing weak aliases. (6)");
     3051        return -1;
     3052    }
     3053
     3054    /* GROUP - DGROUP  */
     3055    omf.hdr.chType = GRPDEF;
     3056    omf.hdr.cb = 6;
     3057    omf.ach[0] = 9;                     /* "DGROUP" LNAME index */
     3058    omf.ach[1] = 0xff;
     3059    omf.ach[2] = 3;                     /* "BSS32" SEGDEF index */
     3060    omf.ach[3] = 0xff;
     3061    omf.ach[4] = 2;                     /* "DATA32" SEGDEF index */
     3062    omf.ach[5] = 0;                     /* crc */
     3063    if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile) != 1)
     3064    {
     3065        wldErr(pWld, "Error occured while writing weak aliases. (6)");
     3066        return -1;
     3067    }
     3068#endif
     3069
     3070    return 0;
     3071}
     3072
     3073
     3074
     3075/**
     3076 * Write the end of object stuff for an weak alias object.
     3077 *
     3078 * @returns 0 on success.
     3079 * @returns -1 on failure.
     3080 * @param   pWld    Linker instance.
     3081 * @param   phFile  Output file.
     3082 * @param   cbPage  Library page size.
     3083 */
     3084static int wldObjEnd(PWLD pWld, FILE *phFile, int cbPage)
     3085{
     3086    #pragma pack(1)
     3087    struct omfstuff
     3088    {
     3089        OMFREC  hdr;
     3090        char        ach[32];
     3091    }       omf;
     3092    #pragma pack()
     3093
     3094#if 0
     3095    /* PASS1 end COMENT */
     3096    omf.hdr.chType = COMENT;
     3097    omf.hdr.cb = 4;
     3098    omf.ach[0] = 0x80;                  /* flags */
     3099    omf.ach[1] = CLASS_PASS;            /* class */
     3100    omf.ach[2] = 1;                     /* subclass */
     3101    omf.ach[3] = 0;                     /* crc */
     3102    if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile) != 1)
     3103    {
     3104        wldErr(pWld, "Error occured while writing weak aliases. (1)");
     3105        return -1;
     3106    }
     3107#endif
     3108
     3109    /* MODEND */
     3110    omf.hdr.chType = MODEND | REC32;
     3111    omf.hdr.cb = 2;
     3112    memset(&omf.ach[0], 0, sizeof(omf.ach));
     3113    if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile) != 1)
     3114    {
     3115        wldErr(pWld, "Error occured while writing weak aliases. (2)");
     3116        return -1;
     3117    }
     3118
     3119    /* padding */
     3120    while (ftell(phFile) % cbPage)
     3121    {
     3122        if (fputc('\0', phFile) < 0)
     3123        {
     3124            wldErr(pWld, "Error occured while writing weak aliases. (3)");
     3125            return -1;
     3126        }
     3127    }
     3128    return 0;
     3129}
     3130
     3131
    29003132/** Parameter structure for wldGenerateObjEnum(). */
    29013133typedef struct wldGenerateObjEnum_param
     
    29053137    /** Number of aliases in current object. (0 means first) */
    29063138    unsigned    cAliases;
     3139    /** Library file number */
     3140    unsigned    iLibFile;
    29073141} WLDGOEPARAM, *PWLDGOEPARAM;
    29083142
     
    29523186
    29533187        /* end the current object? */
    2954         if ((pWld->fFlags & WLDC_LINKER_LINK386) && pParam->cAliases > 1)//32)
    2955         {
    2956             off_t   off;
    2957             omf.hdr.chType = MODEND;
    2958             omf.hdr.cb = 2;                 
    2959             memset(&omf.ach[0], 0, sizeof(omf.ach));
    2960             off = (ftell(pParam->phFile) + omf.hdr.cb + sizeof(OMFREC)) % 32;
    2961             if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC) + (off ? 32 - off : 0), 1, pParam->phFile) != 1)
    2962             {
    2963                 wldErr(pWld, "Error occured while writing weak aliases. (2)");
    2964                 return -1;
    2965             }
     3188        if ((pWld->fFlags & WLDC_LINKER_LINK386) && pParam->cAliases >= 1)//32)
     3189        {
     3190            int rc = wldObjEnd(pWld, pParam->phFile, 32);
     3191            if (rc)
     3192                return rc;
    29663193            pParam->cAliases = 0;
    29673194        }
    29683195       
    29693196        /* make new object ? */
    2970         if ((pWld->fFlags & WLDC_LINKER_LINK386) && !pParam->cAliases)
    2971         {
    2972             omf.hdr.chType = THEADR;
    2973             omf.hdr.cb = 2 + strlen("weakalias.obj");
    2974             omf.ach[0] = omf.hdr.cb - 2;
    2975             memcpy(&omf.ach[1], "weakalias.obj", omf.hdr.cb - 2);
    2976             omf.ach[cch + 1] = 0;
    2977             if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, pParam->phFile) != 1)
    2978             {
    2979                 wldErr(pWld, "Error occured while writing weak aliases. (2)");
    2980                 return -1;
    2981             }
    2982         }
    2983        
     3197        if (!pParam->cAliases)
     3198        {
     3199            sprintf(omf.ach, "wk%d.obj", pParam->iLibFile++);
     3200            int rc = wldObjStart(pWld, pParam->phFile, omf.ach);
     3201            if (rc)
     3202                return rc;
     3203        }
    29843204
    29853205        /* Alias record */
     
    32183438                        _md_close(pMD);
    32193439
    3220                         /* copy the rest of the file */
    3221                         if (!rc)
     3440                        /* copy the rest of the file if any changes was made. */
     3441                        if (!rc && param.iLine > 0)
    32223442                        {
    32233443                            char szTmp[512];
     
    32253445                                if (fputs(szTmp, phFile) < 0)
    32263446                                    rc = wldErr(pWld, "Write error.");
     3447                        }
     3448                        else if (!rc)
     3449                        {
     3450                            /* no changes was made. */
     3451                            WLDINFO(pWld, ("No weak exports, removing file."));
     3452                            fclose(phFile);
     3453                            phFile = NULL;
     3454                            remove(pszDefName);
     3455                            *pszDefName = '\0';
    32273456                        }
    32283457                    }
     
    32333462                else
    32343463                    rc = wldErr(pWld, "Failed to open '%s' for reading.\n", pWld->pDef->pszModName);
    3235                 fclose(phFile);
     3464                if (phFile)
     3465                    fclose(phFile);
    32363466            }
    32373467            else
     
    32783508                off_t           offAlias;
    32793509
     3510                /* write start of file */
    32803511                if (pWld->fFlags & WLDC_LINKER_LINK386)
    32813512                {
     
    32873518                    omf.libhdr.cDictBlocks = 0;
    32883519                    omf.libhdr.fchFlags = 1;
     3520                    if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile) != 1)
     3521                    {
     3522                        wldErr(pWld, "Failed to write to '%s'.", pszObjName);
     3523                        rc = -1;
     3524                    }
    32893525                }
    3290                 else
    3291                 {
    3292                     /* Write object file header. */
    3293                     /* todo: link386 doesn't like if there are too many alias in one module btw. */
    3294                     int cch = strlen("wk.obj");
    3295                     omf.hdr.chType = THEADR;
    3296                     omf.hdr.cb = cch + 2;           /* name + crc */
    3297                     omf.ach[0] = cch;           /* module name */
    3298                     memcpy(&omf.ach[1], "wk.obj", cch);
    3299                     omf.ach[1+cch] = 0;         /* crc */
    3300                 }
    3301                 fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile);
    3302 
    3303 
    3304                 /* Make aliases  */
    3305                 offAlias = ftell(phFile);       /* save this to see if anything is added. */
    3306                 param.phFile = phFile;
    3307                 param.cAliases = 0;
    3308                 rc = symEnum(pWld, &pWld->Global,
    3309                              WLDSF_WEAK, WLDSF_WEAK | WLDSF_WEAKALIASDONE,
    3310                              wldGenerateObjEnum, &param);
     3526
     3527                /* successfully wrote start of file? */
    33113528                if (!rc)
    33123529                {
    3313                     if (ftell(phFile) != offAlias)
     3530                    /*
     3531                     * Make aliases 
     3532                     */
     3533                    offAlias = ftell(phFile);       /* save this to see if anything is added. */
     3534                    param.phFile = phFile;
     3535                    param.cAliases = 0;
     3536                    param.iLibFile = 0;
     3537                    rc = symEnum(pWld, &pWld->Global,
     3538                                 WLDSF_WEAK, WLDSF_WEAK | WLDSF_WEAKALIASDONE,
     3539                                 wldGenerateObjEnum, &param);
     3540                    if (!rc)
    33143541                    {
    3315                         #ifdef GENERATE_LIBRARY
    3316                         /* end the last object? */
    3317                         if (param.cAliases )
    3318                         {   
    3319                             off_t   off;
    3320                             omf.hdr.chType = MODEND;
    3321                             omf.hdr.cb = 2;                 
    3322                             memset(&omf.ach[0], 0, sizeof(omf.ach));
    3323                             off = (ftell(phFile) + omf.hdr.cb + sizeof(OMFREC)) % 32;
    3324                             if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC) + (off ? 32 - off : 0), 1, phFile) != 1)
     3542                        if (ftell(phFile) != offAlias)
     3543                        {
     3544                            /* end the last object? */
     3545                            if ((pWld->fFlags & WLDC_LINKER_LINK386) && param.cAliases > 1)
    33253546                            {
    3326                                 wldErr(pWld, "Error occured while writing weak aliases. (2)");
    3327                                 return -1;
     3547                                rc = wldObjEnd(pWld, phFile, 32);
     3548                                if (!rc)
     3549                                {
     3550                                    /* write end of library */
     3551                                    memset(&omf, 0, sizeof(omf));
     3552                                    omf.hdr.chType = LIBEND;
     3553                                    omf.hdr.cb = 32 - 3;
     3554                                    if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile) != 1)
     3555                                    {
     3556                                        wldErr(pWld, "Failed to write to '%s'.", pszObjName);
     3557                                        rc = -1;
     3558                                    }
     3559                                }
    33283560                            }
     3561                            else
     3562                                rc = wldObjEnd(pWld, phFile, 1);
    33293563                        }
    3330 
    3331                         /* write end of library */
    3332                         memset(&omf, 0, sizeof(omf));
    3333                         omf.hdr.chType = LIBEND;
    3334                         omf.hdr.cb = 32 - 3;
    3335                         #else
    3336                         /* write end of module */
    3337                         omf.hdr.chType = MODEND;
    3338                         omf.hdr.cb = 2;
    3339                         omf.ach[0] = omf.ach[1] = 0;
    3340                         #endif
    3341                         if (fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile) != 1)
     3564                        else
    33423565                        {
    3343                             wldErr(pWld, "Failed to write to '%s'.", pszObjName);
    3344                             rc = -1;
     3566                            WLDINFO(pWld, ("No weak alias needed, removing file."));
     3567                            fclose(phFile);
     3568                            phFile = NULL;
     3569                            remove(pszObjName);
     3570                            *pszObjName = '\0';
    33453571                        }
    3346                         fclose(phFile);
    3347                     }
    3348                     else
    3349                     {
    3350                         WLDINFO(pWld, ("No weak alias needed, deleting file."));
    3351                         fclose(phFile);
    3352                         remove(pszObjName);
    3353                         *pszObjName = '\0';
    33543572                    }
    33553573                }
    3356                 else
     3574                if (phFile)
    33573575                    fclose(phFile);
    33583576            }
Note: See TracChangeset for help on using the changeset viewer.