Changeset 537


Ignore:
Timestamp:
Aug 6, 2003, 2:16:54 AM (22 years ago)
Author:
bird
Message:

Finished (?) the .def file manipulation.

File:
1 edited

Legend:

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

    • Property cvs2svn:cvs-rev changed from 1.13 to 1.14
    r536 r537  
    3838 * type groups, but the choice was made to differenciate this using flags.
    3939 * So, symbol enumeration is done using flag masks.
    40  *
     40 * @todo: Finish this stuff.
    4141 *
    4242 */
     
    441441
    442442
    443 /*******************************************************************************
    444 *
    445 *  H e l p e r s
    446 *  H e l p e r s
    447 *  H e l p e r s
    448 *
    449 *******************************************************************************/
     443
     444
     445
     446
     447
     448
     449
     450/*=============================================================================
     451 *                                                                            *
     452 *                                                                            *
     453 *  W E A K   L I N K E R   M E T H O D S                                     *
     454 *  W E A K   L I N K E R   M E T H O D S                                     *
     455 *  W E A K   L I N K E R   M E T H O D S                                     *
     456 *  W E A K   L I N K E R   M E T H O D S                                     *
     457 *  W E A K   L I N K E R   M E T H O D S                                     *
     458 *                                                                            *
     459 *                                                                            *
     460 *============================================================================*/
     461
    450462
    451463#ifdef WLD_ENABLED_DBG
     
    552564
    553565
     566
     567/*=============================================================================
     568 *                                                                            *
     569 *                                                                            *
     570 *  L I B R A R Y   M E T H O D S                                             *
     571 *  L I B R A R Y   M E T H O D S                                             *
     572 *  L I B R A R Y   M E T H O D S                                             *
     573 *  L I B R A R Y   M E T H O D S                                             *
     574 *  L I B R A R Y   M E T H O D S                                             *
     575 *                                                                            *
     576 *                                                                            *
     577 *============================================================================*/
    554578
    555579
     
    631655    pLib->pDict = NULL;
    632656    return -1;
    633 #endif 
     657#endif
    634658}
    635659
     
    908932
    909933                /* update statistics */
    910                 if (pcLoaded)   
     934                if (pcLoaded)
    911935                    (*pcLoaded)++;
    912936
     
    9971021
    9981022
    999 
    1000 
     1023/*=============================================================================
     1024 *                                                                            *
     1025 *                                                                            *
     1026 *  M O D U L E   M E T H O D S                                               *
     1027 *  M O D U L E   M E T H O D S                                               *
     1028 *  M O D U L E   M E T H O D S                                               *
     1029 *  M O D U L E   M E T H O D S                                               *
     1030 *  M O D U L E   M E T H O D S                                               *
     1031 *                                                                            *
     1032 *                                                                            *
     1033 *============================================================================*/
    10011034
    10021035
     
    11071140
    11081141
     1142
     1143
     1144
     1145
     1146
     1147
     1148/*=============================================================================
     1149 *                                                                            *
     1150 *                                                                            *
     1151 *  S Y M B O L   M E T H O D S                                               *
     1152 *  S Y M B O L   M E T H O D S                                               *
     1153 *  S Y M B O L   M E T H O D S                                               *
     1154 *  S Y M B O L   M E T H O D S                                               *
     1155 *                                                                            *
     1156 *                                                                            *
     1157 *============================================================================*/
     1158
     1159
     1160/**
     1161 * Calculate the hash value of a symbol.
     1162 * @returns hash value.
     1163 * @param   pszSym  Symbol to calculate it for.
     1164 * @param   cch     Symbol length.
     1165 * @todo    This ain't respecting case sensitivity.
     1166 */
     1167static inline unsigned symHash(const char* pszSym, unsigned cch)
     1168{
     1169    unsigned uHash = 0;
     1170    while (     cch
     1171           &&   (pszSym[0] != '$' || pszSym[1] != 'w' || pszSym[2] != '$')
     1172             )
     1173    {
     1174        uHash = uHash * 63377 + *pszSym;
     1175        pszSym++;
     1176        cch--;
     1177    }
     1178    uHash %= WLDSYM_HASH_SIZE;
     1179    return uHash;
     1180}
     1181
     1182/**
     1183 * Find the symbol by the name pszName.
     1184 * @returns Pointer to matching symbol.
     1185 * @returns NULL if not found.
     1186 * @param   pWld    Linker Instance.
     1187 * @param   pszName Name of the symbol to find.
     1188 */
     1189static PWLDSYM      symLookup(PWLD pWld, const char *pszName)
     1190{
     1191    PWLDSYM     pSym;
     1192    const char *psz;
     1193    unsigned    cchName;
     1194    unsigned    uHash;
     1195
     1196    /*
     1197     * Calculate the hash of the symbol
     1198     *
     1199     * It's easier just to add it to the string table than starting to
     1200     * check the correct case function and such. As there is a good
     1201     * likelyhood that the symbol exists there is little expense in doing
     1202     * this compared to the convenience. If it's slow we'll optimize grow.c
     1203     * (if possible) and gain everywhere.
     1204     */
     1205    psz = strstr(pszName, "$w$");
     1206    cchName = psz ? psz - pszName : strlen(pszName);
     1207    pszName = (pWld->fFlags & WLDC_CASE_INSENSITIVE ? strpool_addnu : strpool_addn)(pWld->pStrMisc, pszName, cchName);
     1208    uHash = symHash(pszName, cchName);
     1209
     1210    /* look it up */
     1211    for (pSym = pWld->Global.ap[uHash]; pSym; pSym = pSym->pHashNext)
     1212       if (pSym->pszName == pszName)
     1213           return pSym;
     1214
     1215    return NULL;
     1216}
     1217
     1218
     1219
     1220
    11091221/**
    11101222 * Symbol enumerator.
     
    13221434#endif
    13231435
    1324 
    1325 /**
    1326  * Calculate the hash value of a symbol.
    1327  * @returns hash value.
    1328  * @param   pszSym  Symbol to calculate it for.
    1329  * @param   cch     Symbol length.
    1330  * @todo    This ain't respecting case sensitivity.
    1331  */
    1332 static inline unsigned symHash(const char* pszSym, unsigned cch)
    1333 {
    1334     unsigned uHash = 0;
    1335     while (     cch
    1336            &&   (pszSym[0] != '$' || pszSym[1] != 'w' || pszSym[2] != '$')
    1337              )
    1338     {
    1339         uHash = uHash * 65599 + *pszSym;
    1340         pszSym++;
    1341         cch--;
    1342     }
    1343     uHash %= WLDSYM_HASH_SIZE;
    1344     return uHash;
    1345 }
    13461436
    13471437/**
     
    14651555        if ( (     /* 1 */
    14661556                (fFlags & WLDSF_TYPEMASK) == WLDSF_UNDEF
    1467              && ((pSym->fFlags & WLDSF_TYPEMASK) == WLDSF_PUBLIC || (pSym->fFlags & WLDSF_TYPEMASK) == WLDSF_COMM 
     1557             && ((pSym->fFlags & WLDSF_TYPEMASK) == WLDSF_PUBLIC || (pSym->fFlags & WLDSF_TYPEMASK) == WLDSF_COMM
    14681558                 || (pSym->fFlags & (WLDSF_TYPEMASK | WLDSF_WEAK)) == WLDSF_UNDEF || (pSym->fFlags & (WLDSF_TYPEMASK)) == WLDSF_IMPORT)
    14691559            ) || ( /* 2 */
     
    19292019}
    19302020
     2021
     2022
     2023
     2024
     2025
     2026
     2027
     2028
     2029
     2030
     2031/*=============================================================================
     2032 *                                                                            *
     2033 *                                                                            *
     2034 *  M I S C   M E T H O D S                                                   *
     2035 *  M I S C   M E T H O D S                                                   *
     2036 *  M I S C   M E T H O D S                                                   *
     2037 *  M I S C   M E T H O D S                                                   *
     2038 *  M I S C   M E T H O D S                                                   *
     2039 *                                                                            *
     2040 *                                                                            *
     2041 *============================================================================*/
     2042
     2043
    19312044/**
    19322045 * Reads an OMF module from a file.
     
    22902403
    22912404
    2292 /*******************************************************************************
    2293 *
    2294 *  P u b l i c   I n t e r f a c e
    2295 *  P u b l i c   I n t e r f a c e
    2296 *  P u b l i c   I n t e r f a c e
    2297 *
    2298 *******************************************************************************/
    2299 
    2300 
    2301 
     2405
     2406
     2407
     2408
     2409
     2410
     2411/*=============================================================================
     2412 *                                                                            *
     2413 *                                                                            *
     2414 *  P U B L I C   M E T H O D S                                               *
     2415 *  P U B L I C   M E T H O D S                                               *
     2416 *  P U B L I C   M E T H O D S                                               *
     2417 *  P U B L I C   M E T H O D S                                               *
     2418 *  P U B L I C   M E T H O D S                                               *
     2419 *                                                                            *
     2420 *                                                                            *
     2421 *============================================================================*/
    23022422
    23032423/**
     
    23442464    for (pObj = pWld->pObjs; pObj;)
    23452465    {
    2346         void *pv = pObj;   
     2466        void *pv = pObj;
    23472467        modClose(pObj);
    23482468        pObj = pObj->pNext;
     
    23512471    for (pObj = pWld->pDef; pObj;)
    23522472    {
    2353         void *pv = pObj;   
     2473        void *pv = pObj;
    23542474        modClose(pObj);
    23552475        pObj = pObj->pNext;
     
    23602480    for (pLib = pWld->pLibs; pLib;)
    23612481    {
    2362         void *pv = pLib;   
     2482        void *pv = pLib;
    23632483        libClose(pLib);
    23642484        pLib = pLib->pNext;
     
    28362956
    28372957
     2958/** Parameter structure for wlddefCallback (module definition parser callback). */
     2959typedef struct wlddefCallback_param
     2960{
     2961    /** Linker Instance. */
     2962    PWLD        pWld;
     2963    /** Read file stream of the current definition file. */
     2964    FILE       *phOrgFile;
     2965    /** Write file stream of the new, modified, defintion file. */
     2966    FILE       *phNewFile;
     2967    /** Our current linenumber index. */
     2968    unsigned    iLine;
     2969} WLDDCPARAM, *PWLDDCPARAM;
    28382970
    28392971/**
     
    28452977 * @param   pStmt   Statement we're processing.
    28462978 * @param   eToken  Token we're processing.
    2847  * @param   pvArg   Pointer to file stream for the output file.
    2848  *                 
    2849  */
    2850 static int wldDefCallbackGenerateWeakAliases(struct _md *pMD, const _md_stmt *pStmt, _md_token eToken, void *pvArg)
    2851 {
    2852     unsigned        fFlags;
    2853     unsigned        uOrdinal;
    2854     unsigned        cWords;
    2855     PWLDSYM         pSym;
    2856     FILE *          phFile = (PHFILE)pvArg;
     2979 * @param   pvArg   Pointer to a WLDDCPARAM structure.
     2980 * @sketch
     2981 *
     2982 *  If it exports a weak symbol Then
     2983 *      While linenumber(_md) > linenumber phOrgFile
     2984 *          Copy line from phOrgFile to phNewFile
     2985 *      Write modifed line to phNewFile and
     2986 *      Skip a line in phOrgFile.
     2987 *  Endif
     2988 *
     2989 *  The caller of _md_parse will make sure the last chunk of the file is
     2990 *  copied to the new file.
     2991 */
     2992static int wlddefCallback(struct _md *pMD, const _md_stmt *pStmt, _md_token eToken, void *pvArg)
     2993{
     2994    PWLDDCPARAM     pParam = (PWLDDCPARAM)pvArg;
    28572995
    28582996    switch (eToken)
     
    28623000         */
    28633001        case _MD_EXPORTS:
    2864             fFlags = uOrdinal = cWords = 0;
     3002        {
     3003            char        szTmp[1024];
     3004            int         cch;
     3005            unsigned    iMDLine;
     3006            PWLDSYM     pSymInt = NULL;
     3007            PWLDSYM     pSymExp;
     3008
     3009            pSymExp = symLookup(pParam->pWld, pStmt->export.entryname);
     3010            if (pStmt->export.internalname[0])
     3011                pSymInt = symLookup(pParam->pWld, pStmt->export.internalname);
     3012            if (!pSymExp && !pSymInt)
     3013            {
     3014                wldWarn(pParam->pWld, "Failed to find exported symbols! (%s, %s, %d)",
     3015                        pStmt->export.entryname, pStmt->export.internalname, pStmt->export.ordinal);
     3016                return 0;               /* .ignore it. good idea? */
     3017            }
     3018
     3019            /* Skip it all if neither of the symbols are weak. */
     3020            if (    !(pSymExp->fFlags & WLDSF_WEAK)
     3021                &&   (!pSymInt  || !(pSymInt->fFlags & WLDSF_WEAK)))
     3022                return 0;
     3023
     3024            /* Copy line from org to new so we're up to date. */
     3025            iMDLine = _md_get_linenumber(pMD);
     3026            while (pParam->iLine < iMDLine)
     3027            {
     3028                if (!fgets(szTmp, 512 /* this is the size _md_* uses */, pParam->phOrgFile))
     3029                {
     3030                    wldErr(pParam->pWld, "Read error while reading original .def file.");
     3031                    strcpy(szTmp, ";read error");
     3032                }
     3033
     3034                if (++pParam->iLine >= iMDLine) /* skip last line. */
     3035                    break;
     3036                if (fputs(szTmp, pParam->phNewFile) < 0)
     3037                    return wldErr(pParam->pWld, "Write error.");
     3038            }
     3039
     3040            /* if the internal symbol is weak, replace it with the right $w$. */
     3041            if (pSymInt && (pSymInt->fFlags & WLDSF_WEAK))
     3042                cch = sprintf(szTmp, "  \"%s\" = \"%s\"", pStmt->export.entryname, pSymInt->pszWeakName);
     3043            else
     3044                cch = sprintf(szTmp, "  \"%s\" = \"%s\"", pStmt->export.entryname, pSymExp->pszWeakName);
    28653045            if (pStmt->export.flags & _MDEP_ORDINAL)
    2866                 uOrdinal = pStmt->export.ordinal;
     3046                cch += sprintf(&szTmp[cch], " @%d", pStmt->export.ordinal);
    28673047            if (pStmt->export.flags & _MDEP_RESIDENTNAME)
    2868                 fFlags |= WLDSEF_NONRESIDENT;
    2869             else if (pStmt->export.flags & _MDEP_NONAME)
    2870                 fFlags |= WLDSEF_NONAME;
    2871             else
    2872                 fFlags |= WLDSEF_DEFAULT;
     3048                cch += sprintf(&szTmp[cch], " RESIDENTNAME");
     3049            if (pStmt->export.flags & _MDEP_NONAME)
     3050                cch += sprintf(&szTmp[cch], " NONAME");
    28733051            if (pStmt->export.flags & _MDEP_NODATA)
    2874                 fFlags |= WLDSEF_NODATA;
     3052                cch += sprintf(&szTmp[cch], " NODATA");
    28753053            if (pStmt->export.flags & _MDEP_PWORDS)
    2876                 cWords = pStmt->export.pwords;
    2877             pSym = symAddExport(pParam->pWld, pParam->pMod, 0,
    2878                                 fFlags, cWords,
    2879                                 pStmt->export.entryname, -1,
    2880                                 pStmt->export.internalname, -1,
    2881                                 uOrdinal);
    2882             if (!pSym)
    2883             {
    2884                 pParam->rc = -4;
    2885                 return -4;
    2886             }
     3054                cch += sprintf(&szTmp[cch], " %d", pStmt->export.pwords);
     3055            cch += sprintf(&szTmp[cch], "   ; !weakld changed this!\n");
     3056
     3057            if (fputs(szTmp, pParam->phNewFile) < 0)
     3058                return wldErr(pParam->pWld, "Write error.");
    28873059            break;
     3060        }
    28883061
    28893062        /*
     
    28913064         */
    28923065        case _MD_parseerror:
    2893             wldErr(pWld, "Parse error %d on line %d. (errorcode=%d stmt=%d)",
     3066            wldErr(pParam->pWld, "Parse error %d on line %d. (errorcode=%d stmt=%d)",
    28943067                   _md_get_linenumber(pMD), pStmt->error.code, pStmt->error.stmt);
    2895             pParam->rc = -2;
    28963068            break;
    28973069
     
    29003072         */
    29013073        default:
    2902             pMD->
    29033074            break;
    29043075    }
     
    29353106        sprintf(pszFile, "%s\\%s%x%lx%d%lx%s", pszTmp, pszPrefix, pid, tv.tv_sec, c, tv.tv_usec, pszSuffix);
    29363107    } while (!stat(pszFile, &s));
    2937    
     3108
    29383109    return 0;
    29393110}
     
    29563127int     wldGenerateWeakAliases(PWLD pWld, char *pszObjName, char *pszDefName)
    29573128{
    2958     char *      psz;
    29593129    FILE *      phFile;
    29603130    int         rc;
     
    29653135        *pszDefName = '\0';
    29663136
    2967     /* generate the object  file */
     3137    /*
     3138     * Generate the object file
     3139     */
    29683140    rc = wldTempFile(pWld, pszObjName, "wk", ".obj");
    29693141    if (rc)
     
    29933165        omf.ach[1+cch] = 0;         /* crc */
    29943166        fwrite(&omf, omf.hdr.cb + sizeof(OMFREC), 1, phFile);
    2995         /* todo: check if we'll need more shit here to satisfy the linkers */
     3167        /* todo: link386 doesn't like if there are too many alias in one module btw. */
    29963168
    29973169
     
    30333205    }
    30343206
    3035     /* do the definition file */
    3036     if (!rc && pWld->pDef && pszDefName)
     3207
     3208    /*
     3209     * Do the definition file.
     3210     *      We don't need to do this if the above code failed or
     3211     *      didn't issue any aliases.
     3212     */
     3213    if (!rc && pWld->pDef && pszDefName && pszObjName[0])
    30373214    {
    30383215        rc = wldTempFile(pWld, pszDefName, "wk", ".def");
     
    30413218            WLDINFO(pWld, ("Generating definition file '%s' for weak exports.", pszDefName));
    30423219
    3043             /* open it */
     3220            /* open output file */
    30443221            phFile = fopen(pszDefName, "w");
    30453222            if (phFile)
    30463223            {
    3047                 FILE *phOrgDef = fopen(pWld->pDef->pszModName, "r");
    3048                 if (phOrgDef)
     3224                /* open original file */
     3225                FILE *phOrgFile = fopen(pWld->pDef->pszModName, "r");
     3226                if (phOrgFile)
    30493227                {
    3050                     char *pachOrgDef = fsize
    3051                     struct _md *pMD;
    3052 
    3053                     /* open original def file */
    3054                     pMD = _md_open(pWld->pDef->pszModName);
     3228                    /* open original def file with the ModDef library.  */
     3229                    struct _md *pMD = _md_open(pWld->pDef->pszModName);
    30553230                    if (pMD)
    30563231                    {
     3232                        WLDDCPARAM  param;
     3233                        param.pWld = pWld;
     3234                        param.phOrgFile = phOrgFile;
     3235                        param.phNewFile = phFile;
     3236                        param.iLine = 0;
     3237
    30573238                        /* parse it */
    30583239                        _md_next_token(pMD);
    3059                         rc = _md_parse(pMD, wldDefCallbackGenerateWeakAliases, phFile);
     3240                        rc = _md_parse(pMD, wlddefCallback, &param);
    30603241                        _md_close(pMD);
     3242
     3243                        /* copy the rest of the file */
     3244                        if (!rc)
     3245                        {
     3246                            char szTmp[512];
     3247                            while (fgets(szTmp, sizeof(szTmp), phOrgFile))
     3248                                if (fputs(szTmp, phFile) < 0)
     3249                                    rc = wldErr(pWld, "Write error.");
     3250                        }
    30613251                    }
    30623252                    else
     
    30653255                }
    30663256                else
    3067                     rc = wldErr(pWld, "Failed to open '%s' for reading.\n", pszDefName);
     3257                    rc = wldErr(pWld, "Failed to open '%s' for reading.\n", pWld->pDef->pszModName);
    30683258                fclose(phFile);
    30693259            }
Note: See TracChangeset for help on using the changeset viewer.