Changeset 511


Ignore:
Timestamp:
Aug 2, 2003, 5:57:20 AM (22 years ago)
Author:
bird
Message:

backup commit...

Location:
trunk/src/emx/src/emxomf
Files:
3 edited

Legend:

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

    • Property cvs2svn:cvs-rev changed from 1.14 to 1.15
    r510 r511  
    456456
    457457
    458 /* finds the full path of a library or object file, after check 
    459    default extentions an such. 
    460      
     458/* finds the full path of a library or object file, after check
     459   default extentions an such.
     460
    461461   Object files will be searched for using the LIB env.var if not found.
    462    
     462
    463463   Library hacks:
    464464     1. file with .lib extention.
     
    477477  FILE *        phFile;
    478478  char          cchname = strlen (pszname);
    479    
     479
    480480  /* Plain name first - the easist option */
    481481  memcpy (pszfullname, pszname, cchname + 1);
     
    501501  /* Search LIBS. */
    502502  if (!strpbrk (pszname, ":\\/"))
    503     { 
     503    {
    504504      int           ffound;
    505505      const char *  pszend;
     
    602602  PWLD    pwld;
    603603
    604   pwld = wld_create (WLDC_VERBOSE);
     604  pwld = wldCreate (WLDC_VERBOSE);
    605605  if (pwld)
    606606    {
     
    614614          static const char * objexts[] = { ".obj", ".o", NULL };
    615615          phfile = find_objlib (szname, pcur->name, objexts, TRUE);
    616           rc = wld_add_object (pwld, phfile, szname);
     616          rc = wldAddObject (pwld, phfile, szname);
    617617        }
    618618
     
    621621        {
    622622          phfile = fopen (def_fname, "r");
    623           rc = wld_add_deffile (pwld, phfile, def_fname);
     623          rc = wldAddDefFile (pwld, phfile, def_fname);
    624624        }
    625625
     
    629629          static const char * libexts[] = { ".lib", ".a", NULL };
    630630          phfile = find_objlib (szname, pcur->name, libexts, TRUE);
    631           rc = wld_add_library (pwld, phfile, szname);
     631          rc = wldAddLibrary (pwld, phfile, szname);
    632632          free(pcur->name);
    633633          pcur->name = xstrdup(szname);
    634634        }
    635635
     636      /* complete pass 1 */
     637      if (!rc)
     638      {
     639          rc = wldPass1 (pwld);
     640          /* ignore unresolved externals for now. */
     641          if (rc == 42)
     642              rc = 0;
     643      }
     644
    636645      /* generate weak aliases. */
    637646      if (!rc)
    638         rc = wld_generate_weakobj (pwld, weakobj_fname);
     647        rc = wldGenerateWeakobj (pwld, weakobj_fname);
    639648      if (!rc && weakobj_fname[0])
    640649        add_name_list (&add_obj_fnames, weakobj_fname);
    641650
    642651      /* cleanup the linker */
    643       wld_destroy (pwld); 
     652      wldDestroy (pwld);
    644653    }
    645654  else
     
    917926  make_env ();
    918927
    919   /* Do the weak prelinking unless GCC_WEAKSYMS are set. 
     928  /* Do the weak prelinking unless GCC_WEAKSYMS are set.
    920929     Important that this is done after make_env(). */
    921930
  • trunk/src/emx/src/emxomf/weakld.c

    • Property cvs2svn:cvs-rev changed from 1.5 to 1.6
    r510 r511  
    1 /* $Id$
    2  *
     1/* $Id$ */
     2/** @file
    33 * Weak Symbol Pre-Linker.
    44 *
     
    5555
    5656#ifdef DEBUG
    57 
    5857#define SYMDBG(pSym, pszMsg)    symDbg(pSym, pszMsg);
     58#define WLDDBG(a)               wldDbg a
     59#define WLDDBG2(a)              wldDbg a
    5960#else
    6061#define SYMDBG(pSym, pszMsg)    do {} while (0)
    61 #endif
     62#define WLDDBG(a)               do {} while (0)
     63#define WLDDBG2(a)              do {} while (0)
     64#endif
    6265
    6366
     
    8790*   Structures and Typedefs                                                    *
    8891*******************************************************************************/
     92/** @group OMF stuff
     93 * @{ */
     94
     95/** OMF record header. */
     96#pragma pack(1)
     97typedef struct OMFREC
     98{
     99  unsigned char     chType;
     100  unsigned short    cb;
     101} OMFREC, *POMFREC;
     102#pragma pack()
     103
     104
     105/** OMF library header. */
     106#pragma pack(1)
     107typedef struct OMFLIBHDR
     108{
     109  unsigned char     chType;
     110  unsigned short    cb;
     111  unsigned long     offDict;
     112  unsigned short    cDictBlocks;
     113  unsigned char     fchFlags;
     114} OMFLIBHDR, *POMFLIBHDR;
     115#pragma pack()
     116/** @} */
     117
     118
    89119/**
    90120 * Library structure
     
    96126    /** Filehandle if open */
    97127    FILE *      phFile;
     128    /** Pointer to extended dictiorary. */
     129    void *      pDict;
     130    /** Library header. */
     131    OMFLIBHDR   LibHdr;
    98132    /** Linked list next pointer. */
    99133    struct wldlib * pNext;
     
    289323
    290324    /** Linked list (FIFO) of libraries to be searched in the link. */
    291     PWLDMOD             pLibs;
    292     PWLDMOD *           ppLibsAdd;
     325    PWLDLIB             pLibs;
     326    PWLDLIB *           ppLibsAdd;
    293327
    294328    /** string pool for miscellaneous string. */
     
    301335
    302336
    303 /** @group OMF stuff
    304  * @{ */
    305 
    306 /** OMF record header. */
    307 #pragma pack(1)
    308 typedef struct OMFREC
    309 {
    310   unsigned char     chType;
    311   unsigned short    cb;
    312 } OMFREC, *POMFREC;
    313 #pragma pack()
    314 
    315 
    316 /** OMF library header. */
    317 #pragma pack(1)
    318 typedef struct OMFLIBHDR
    319 {
    320   unsigned char     chType;
    321   unsigned short    cb;
    322   unsigned long     offDict;
    323   unsigned short    cDictBlocks;
    324   unsigned char     fchFlags;
    325 } OMFLIBHDR, *POMFLIBHDR;
    326 #pragma pack()
    327 
    328 /** @} */
    329337
    330338
     
    332340extern void *xmalloc (size_t n);
    333341
     342/** @group Weak LD - Linker Methods (Private)
     343 * @{ */
     344static void         wldDbg(const char *pszFormat, ...);
    334345static void         wldInfo(const char *pszFormat, ...);
    335346static int          wldErr(PWLD pWld, const char *pszFormat, ...);
    336347static void         wldIntErr(PWLD pWld, PWLDMOD pMod, const char *pszFile, unsigned iLine, const char *pszFunction);
     348static unsigned     pass1ReadOMFMod(PWLD pWld, PWLDMOD pMod, int fLibSearch);
     349/** @} */
     350
     351/** @group Weak LD - Library Methods (Private)
     352 * @{ */
     353static FILE *       libOpen(PWLDLIB pLib);
     354static void         libClose(PWLDLIB pLib);
     355static int          libLoadDict(PWLDLIB pLib);
     356static void         libCloseDict(PWLDLIB pLib);
     357static int          libTryLoadSymbolThruDictionary(PWLD pWld, PWLDLIB pLib, PWLDSYM pSym, unsigned *pcLoaded);
     358static int          libLoadUndefSymbols(PWLD pWld, PWLDLIB pLib, PWLDSYM pSym, unsigned *pcLoaded);
     359static int          libErr(PWLDLIB pLib, const char *pszFormat, ...);
     360static void         libWarn(PWLDLIB pLib, const char *pszFormat, ...);
     361/** @} */
     362
     363/** @group Weak LD - Module Methods (Private)
     364 * @{ */
     365static FILE *       modOpen(PWLDMOD pMod);
     366static void         modClose(PWLDMOD pMod);
    337367static int          modErr(PWLDMOD pMod, const char *pszFormat, ...);
    338368static void         modWarn(PWLDMOD pMod, const char *pszFormat, ...);
    339 static FILE *       modOpen(PWLDMOD pMod);
    340 static void         modClose(PWLDMOD pMod);
    341 
    342 static inline unsigned hash(const char* pszSym, unsigned cch);
     369/** @} */
     370
     371/** @group Weak LD - Symbole Methods (Private)
     372 * @{ */
     373typedef int (*PFNSYMENUM)(PWLD pWld, PWLDSYM pSym, void *pvUser);
     374static int          symHaveUndefined(PWLD pWld);
     375static int          symEnum(PWLD pWld, PWLDSYMTAB pSymTab, unsigned fFlags, unsigned fMask, PFNSYMENUM pfnEnum, void *pvUser);
     376static int          symPrintUnDefEnum(PWLD pWld, PWLDSYM pSym, void *pvUser);
     377static int          symMatchUnDef(PWLD pWld, const unsigned char *pachPascalString, PWLDSYM pSym);
     378/** Pointed to by the pvUser parameter of symSearchLibEnum(). */
     379typedef struct symSearchLibEnum_param
     380{
     381    /** Set if there are more undefined symbols. */
     382    int         fMore;
     383    /** Library to search for symbols in. */
     384    PWLDLIB     pLib;
     385    /** Number modules which was loaded. */
     386    unsigned    cLoaded;
     387} WLDSLEPARAM, *PWLDSLEPARAM;
     388static int          symSearchLibEnum(PWLD pWld, PWLDSYM pSym, void *pvUser);
     389static inline unsigned symHash(const char* pszSym, unsigned cch);
    343390static void         symDbg(PWLDSYM pSym, const char *pszMsg);
     391static PWLDSYM      symAdd(PWLD pWld, PWLDMOD pMod, unsigned fFlags, const char *pachName, int cchName, PWLDSYMACTION peAction);
     392static PWLDSYM      symAddImport(PWLD pWld, PWLDMOD pMod, int fLibSearch,
     393                                 const char *pachIntName, int cchIntName,
     394                                 const char *pachModName, int cchModName,
     395                                 unsigned uOrdinal);
     396static PWLDSYM symAddExport(PWLD pWld, PWLDMOD pMod, int fLibSearch,
     397                            unsigned    fExport,
     398                            unsigned    cExpWords,
     399                            const char *pachExpName, int cchExpName,
     400                            const char *pachIntName, int cchIntName,
     401                            unsigned uOrdinal);
     402static PWLDSYM symAddPublic(PWLD pWld, PWLDMOD pMod, int fLibSearch,
     403                            const char *pachName, int cchName,
     404                            unsigned long ulValue, int iSegment, int iGroup);
     405static PWLDSYM symAddUnDef(PWLD pWld, PWLDMOD pMod, int fLibSearch,
     406                           const char *pachName, int cchName);
     407static PWLDSYM symAddAlias(PWLD pWld, PWLDMOD pMod, int fLibSearch,
     408                           const char *pachAliasName, int cchAliasName,
     409                           const char *pachName, int cchName);
     410static PWLDSYM symAddComdef(PWLD pWld, PWLDMOD pMod, int fLibSearch,
     411                            const char *pachName, int cchName,
     412                            signed long cElements, signed long cbElement);
     413/** @} */
    344414
    345415
     
    352422*******************************************************************************/
    353423
    354 
    355 /**
    356  * Put out a info message.
     424#ifdef DEBUG
     425/**
     426 * Put out a debug message.
    357427 * @param   pszFormat   Format string.
    358428 * @param   ...         Format arguments.
    359429 */
    360 static void         wldInfo(const char *pszFormat, ...)
     430static void         wldDbg(const char *pszFormat, ...)
    361431{
    362432    va_list     args;
    363     fprintf(stderr, "weakld: info: ");
     433    fprintf(stderr, "weakld: dbg: ");
    364434
    365435    va_start(args, pszFormat);
     
    369439        fputc('\n', stderr);
    370440}
     441#endif
    371442
    372443/**
     
    375446 * @param   ...         Format arguments.
    376447 */
     448static void         wldInfo(const char *pszFormat, ...)
     449{
     450    va_list     args;
     451    fprintf(stderr, "weakld: info: ");
     452
     453    va_start(args, pszFormat);
     454    vfprintf(stderr, pszFormat, args);
     455    va_end(args);
     456    if (pszFormat[strlen(pszFormat) - 1] != '\n')
     457        fputc('\n', stderr);
     458}
     459
     460/**
     461 * Put out a info message.
     462 * @param   pszFormat   Format string.
     463 * @param   ...         Format arguments.
     464 */
    377465static int          wldErr(PWLD pWld, const char *pszFormat, ...)
    378466{
    379467    va_list     args;
    380468    fprintf(stderr, "weakld: error: ");
    381    
     469
    382470    va_start(args, pszFormat);
    383471    vfprintf(stderr, pszFormat, args);
     
    415503
    416504
    417 /**
    418  * Report error in module.
    419  * @param   pMod        Pointer to module to report error on.
    420  * @param   pszFormat   Format string.
    421  * @param   ...         Format arguments.
    422  */
    423 static int      modErr(PWLDMOD pMod, const char *pszFormat, ...)
     505
     506
     507
     508
     509
     510
     511
     512/**
     513 * Open this library file.
     514 *
     515 * @returns Pointer to open file stream.
     516 * @returns NULL on failure.
     517 * @param   pLib    Library to open.
     518 */
     519static FILE *       libOpen(PWLDLIB pLib)
     520{
     521    if (!pLib->phFile)
     522    {
     523        pLib->phFile = fopen(pLib->pszLibName, "rb");
     524        if (pLib->phFile)
     525            libErr(pLib, "Failed to open library.");
     526    }
     527    return pLib->phFile;
     528}
     529
     530/**
     531 * Close this library file.
     532 * @param   pLib    Library to close.
     533 */
     534static void         libClose(PWLDLIB pLib)
     535{
     536    if (!pLib->phFile)
     537    {
     538        fclose(pLib->phFile);
     539        pLib->phFile = NULL;
     540    }
     541}
     542
     543/**
     544 * Load the dictionar for this library into memory.
     545 *
     546 * @returns 0 if we successfully loaded the dictionary.
     547 * @returns -1 if we fail to read the dictionary into memory.
     548 * @returns 1 if there is no dictionary.
     549 * @param   pLib    Library which dictionary is to be loaded.
     550 * @remark  This method will open the library. libClose() must be
     551 *          called after this function is used.
     552 */
     553static int          libLoadDict(PWLDLIB pLib)
     554{
     555    FILE *phFile;
     556
     557    /* been here, done that? */
     558    if (pLib->pDict)
     559        return 0;
     560
     561    /* check if it acutally is a library and have an ext dict */
     562    if (    pLib->LibHdr.chType != LIBHDR
     563        ||  pLib->LibHdr.offDict != 0
     564        ||  pLib->LibHdr.cDictBlocks != 0)
     565        return 1;
     566
     567    /* ensure it's open. */
     568    phFile = libOpen(pLib);
     569    if (!phFile)
     570        return -1;
     571
     572    /* position us */
     573    if (fseek(phFile, pLib->LibHdr.offDict, SEEK_SET))
     574        return libErr(pLib, "Failed to seek to extended dictionary (offset %d).", pLib->LibHdr.offDict);
     575
     576    /* read it */
     577    pLib->pDict = xmalloc(pLib->LibHdr.cDictBlocks * 512);
     578    if (fread(pLib->pDict, 512, pLib->LibHdr.cDictBlocks, phFile) == pLib->LibHdr.cDictBlocks)
     579        return 0;
     580    libErr(pLib, "Failed to read extended dictionary.");
     581    free(pLib->pDict);
     582    pLib->pDict = NULL;
     583    return -1;
     584}
     585
     586/**
     587 * This closes the extended dictionary.
     588 *
     589 * @param   pLib    Library which extended dictionary should be closed.
     590 * @remark  Will not close the library file, libClose() must be used for that.
     591 */
     592static void         libCloseDict(PWLDLIB pLib)
     593{
     594    if (pLib->pDict)
     595    {
     596        free(pLib->pDict);
     597        pLib->pDict = NULL;
     598    }
     599}
     600
     601
     602/**
     603 * Does a dictionary lookup on an undefined name.
     604 *
     605 * @returns 0 on non failure.
     606 * @returns 42 if not found.
     607 * @returns -1 on link abort error.
     608 * @param   pWld    Linker instance.
     609 * @param   pLib    Library to search.
     610 * @param   pSym    Undefined symbol to search for.
     611 * @param   pcLoaded    Number of modules which was loaded from this library.
     612 */
     613static int          libTryLoadSymbolThruDictionary(PWLD pWld, PWLDLIB pLib, PWLDSYM pSym, unsigned *pcLoaded)
     614{
     615    //return 42;
     616    return libLoadUndefSymbols(pWld, pLib, pSym, pcLoaded); /* @todo implement this function! */
     617}
     618
     619
     620/**
     621 * Read thru an module looking for definitions for undef symbols.
     622 * If a definition is found we'll load the module containing it.
     623 *
     624 * @returns 0 on non failure.
     625 * @returns 42 if none found.
     626 * @returns -1 on link abort error.
     627 * @param   pWld    Linker instance.
     628 * @param   pLib    Library to search.
     629 * @param   pSym    Undefined symbol to search for.
     630 *                  If NULL we'll try and see if any defined global symbol we
     631 *                  encounter is undefined.
     632 * @param   pcLoaded    Number of modules which was loaded from this library.
     633 */
     634static int          libLoadUndefSymbols(PWLD pWld, PWLDLIB pLib, PWLDSYM pSym, unsigned *pcLoaded)
     635{
     636    FILE *          phFile = pLib->phFile;
     637    unsigned char   uchEnd1, uchEnd2;
     638    OMFREC          OmfRec;
     639    off_t           offCurMod;
     640    int             fSkipRestOfModule = 0;
     641    /* generic stuff */
     642    unsigned long   ul;
     643    signed long     l2, l3;
     644    unsigned short  us, us2, us3;
     645    unsigned char   uch, uch2;
     646
     647
     648    /* Position the library at the first module record. */
     649    if (fseek(phFile, pLib->LibHdr.chType == LIBHDR ? pLib->LibHdr.cb + 3 : 0, SEEK_SET))
     650        return libErr(pLib, "Error when seeking to first module.");
     651
     652    if (pLib->LibHdr.chType != LIBHDR)
     653    {
     654        uchEnd1 = MODEND;
     655        uchEnd2 = MODEND | REC32;
     656    }
     657    else
     658        uchEnd1 = uchEnd2 = LIBEND;
     659
     660    OmfRec.chType = uchEnd1;
     661    fread(&OmfRec, sizeof(OmfRec), 1, phFile);
     662    while (OmfRec.chType != uchEnd1 &&  OmfRec.chType != uchEnd2)
     663    {
     664        int fRead = 0;
     665        int fLoad = 0;
     666        switch (OmfRec.chType)
     667        {
     668            case THEADR:
     669                fSkipRestOfModule = 0;
     670                offCurMod = ftell(phFile) - sizeof(OmfRec);
     671                break;
     672
     673            /* read */
     674            case PUBDEF: case PUBDEF | REC32:
     675            case ALIAS:  case ALIAS  | REC32:
     676            case COMDEF: case COMDEF | REC32:
     677            case COMDAT: case COMDAT | REC32:
     678            case COMENT: case COMENT | REC32:
     679                fRead = !fSkipRestOfModule;
     680                break;
     681        }
     682
     683        if (fRead)
     684        {
     685            unsigned char   achBuffer[OMF_MAX_REC + 8];
     686            union
     687            {
     688                unsigned char *     puch;
     689                signed char *       pch;
     690                unsigned short *    pus;
     691                signed short *      ps;
     692                unsigned long *     pul;
     693                signed long *       pl;
     694                void *              pv;
     695            } u, u1, u2;
     696
     697            /** macro for getting a OMF index out of the buffer */
     698            #define OMF_GETINDEX()  (*u.puch & 0x80 ? ((*u.pch++ & 0x7f) << 8) + *u.pch++ : *u.pch++)
     699            #define OMF_BYTE()      (*u.puch++)
     700            #define OMF_WORD()      (*u.pus++)
     701            #define OMF_24BITWORD() (OMF_BYTE() | (OMF_WORD() << 8))
     702            #define OMF_DWORD()     (*u.pul++)
     703            #define OMF_MORE()      (u.puch - &achBuffer[0] < (int)OmfRec.cb - 1 && !fLoad) /* (different from the next) */
     704            #define OMF_IS32BIT()   ((OmfRec.chType & REC32) != 0)
     705            #define OMF_GETTYPELEN(l) \
     706                do                                                       \
     707                {                                                        \
     708                    l = OMF_BYTE();                                      \
     709                    if (l > 128)                                         \
     710                        switch (l)                                       \
     711                        {                                                \
     712                            case 0x81: l = OMF_WORD(); break;            \
     713                            case 0x84: l = OMF_24BITWORD(); break;       \
     714                            case 0x88: l = OMF_DWORD(); break;           \
     715                            default:                                     \
     716                                libErr(pLib, "Invalid type length!");/* (different from the next) */ \
     717                                return -1;                               \
     718                        }                                                \
     719                } while (0)
     720
     721            u.pv = &achBuffer[0];
     722
     723            /* read it */
     724            if (fread(achBuffer, OmfRec.cb, 1, phFile) != 1)
     725            {
     726                libErr(pLib, "Read error. (2)");
     727                break;
     728            }
     729
     730            /* extract public symbols. */
     731            switch (OmfRec.chType)
     732            {
     733                case COMENT: case COMENT | REC32:
     734                    switch (*++u.pch)
     735                    {
     736                        case CLASS_PASS:
     737                            fSkipRestOfModule = 1;
     738                            break;
     739                        case CLASS_OMFEXT:
     740                        {
     741                            switch (OMF_BYTE())
     742                            {   /*
     743                                 * Import definition.
     744                                 */
     745                                case OMFEXT_IMPDEF:
     746                                {
     747                                    uch = OMF_BYTE();               /* flags */
     748                                    u1 = u; u.pch += 1 + *u.puch;   /* internal name */
     749                                    u2 = u; u.pch += 1 + *u.puch;   /* module name */
     750                                    ul = 0;                         /* ordinal */
     751                                    if (uch & 1)
     752                                        ul = OMF_WORD();
     753                                    if (symMatchUnDef(pWld, u1.pch, pSym))
     754                                        fLoad = 1;
     755                                    break;
     756                                }
     757                            }
     758                        }
     759                    } /* comment class */
     760                    break;
     761
     762                case PUBDEF: case PUBDEF | REC32:
     763                {
     764                    us2 = OMF_GETINDEX();           /* group index */
     765                    us3 = OMF_GETINDEX();           /* segment index */
     766                    if (!us3)
     767                        us = OMF_WORD();            /* base frame - ignored */
     768                    while (OMF_MORE())
     769                    {
     770                        u1 = u; u.pch += 1 + *u.puch;   /* public name */
     771                        ul = OMF_IS32BIT() ? OMF_DWORD() : OMF_WORD();
     772                        us = OMF_GETINDEX();            /* typeindex */
     773                        if (symMatchUnDef(pWld, u1.pch, pSym))
     774                            fLoad = 1;
     775                    }
     776                    break;
     777                }
     778
     779                case ALIAS: case ALIAS | REC32:
     780                {
     781                    while (OMF_MORE())
     782                    {
     783                        u1 = u; u.pch += 1 + *u.puch;   /* alias name */
     784                        u2 = u; u.pch += 1 + *u.puch;   /* substitutt name. */
     785                        if (symMatchUnDef(pWld, u1.pch, pSym))
     786                            fLoad = 1;
     787                    }
     788                    break;
     789                }
     790
     791                case COMDEF: case COMDEF | REC32:
     792                {
     793                    while (OMF_MORE())
     794                    {
     795                        u1 = u; u.pch += 1 + *u.puch;   /* communal name (specs say 1-2 length...) */
     796                        us2 = OMF_GETINDEX();           /* typeindex */
     797                        uch2 = OMF_BYTE();              /* date type */
     798                        switch (uch2)
     799                        {
     800                            case COMDEF_TYPEFAR:
     801                                OMF_GETTYPELEN(l2);     /* number of elements */
     802                                OMF_GETTYPELEN(l3);     /* element size */
     803                                break;
     804                            case COMDEF_TYPENEAR:
     805                                l2 = 1;                 /* number of elements */
     806                                OMF_GETTYPELEN(l3);     /* element size */
     807                                break;
     808                            default:
     809                                libErr(pLib, "Invalid COMDEF type %x.", (int)uch2);
     810                                return -1;
     811                        }
     812                        if (symMatchUnDef(pWld, u1.pch, pSym))
     813                            fLoad = 1;
     814                    }
     815                    break;
     816                }
     817
     818                case COMDAT: case COMDAT | REC32:
     819                {
     820                    /* @todo */
     821                    break;
     822                }
     823            } /* switch */
     824
     825            #undef OMF_GETINDEX
     826            #undef OMF_BYTE
     827            #undef OMF_WORD
     828            #undef OMF_24BITWORD
     829            #undef OMF_DWORD
     830            #undef OMF_MORE
     831            #undef OMF_IS32BIT
     832            #undef OMF_GETTYPELEN
     833
     834            /*
     835             * Shall we load this module?
     836             */
     837            if (fLoad)
     838            {
     839                off_t   offSave = ftell(phFile);
     840                PWLDMOD pMod;
     841                int     rc;
     842
     843                pMod = xmalloc(sizeof(*pMod));
     844                memset(pMod, 0, sizeof(*pMod));
     845                pMod->off = offCurMod;
     846                pMod->pLib = pLib;
     847                pMod->phFile = phFile;
     848                *pWld->ppObjsAdd = pMod;
     849                pWld->ppObjsAdd = &pMod->pNext;
     850
     851                rc = pass1ReadOMFMod(pWld, pMod, 0);
     852                if (rc)
     853                {
     854                    libErr(pLib, "Failed when reading module at offset %x.", offCurMod);
     855                    return rc;
     856                }
     857
     858                /* if one symbol, we're done now */
     859                if (pSym)
     860                    return 0;
     861
     862                /* Resume searching, but skip the rest of this one */
     863                fSkipRestOfModule = 1;
     864                fseek(phFile, offSave, SEEK_SET);
     865            }
     866        }
     867        else
     868        {
     869            off_t   offSkip = OmfRec.cb;
     870            /* Skip to next record. */
     871            if (OmfRec.chType == MODEND || OmfRec.chType == (MODEND | REC32))
     872            {
     873                unsigned    cbPage = pLib->LibHdr.cb + 3;
     874                off_t       off = ftell(phFile) + offSkip;
     875                off -= cbPage * (off / cbPage); /* don't trust this to be 2**n. */
     876                if (off)
     877                    offSkip += cbPage - off;
     878            }
     879            if (fseek(phFile, offSkip, SEEK_CUR))
     880            {
     881                libErr(pLib, "Seek error.");
     882                break;
     883            }
     884        }
     885
     886        /* next header */
     887        if (fread(&OmfRec, sizeof(OmfRec), 1, phFile) != 1)
     888        {
     889            libErr(pLib, "Read error.");
     890            break;
     891        }
     892    }
     893
     894
     895    return 42;
     896}
     897
     898
     899/**
     900 * Put out an error for this library.
     901 * @param   pLib        Library which the warning occured in.
     902 * @param   pszFormat   Message format.
     903 * @param   ...         Format args.
     904 */
     905static int          libErr(PWLDLIB pLib, const char *pszFormat, ...)
    424906{
    425907    va_list     args;
    426     if (pMod->pLib)
    427         fprintf(stderr, "weakld: %s(%s) - error: ", pMod->pLib->pszLibName, pMod->pszModName);
    428     else
    429         fprintf(stderr, "weakld: %s - error: ", pMod->pszModName);
     908    fprintf(stderr, "weakld: %s: error: ", pLib->pszLibName);
    430909
    431910    va_start(args, pszFormat);
     
    437916}
    438917
    439 
    440 /**
    441  * Report warning in module.
    442  * @param   pMod        Pointer to module to report warning on.
    443  * @param   pszFormat   Format string.
    444  * @param   ...         Format arguments.
    445  */
    446 static void     modWarn(PWLDMOD pMod, const char *pszFormat, ...)
     918/**
     919 * Put out a warning for this library.
     920 * @param   pLib        Library which the warning occured in.
     921 * @param   pszFormat   Message format.
     922 * @param   ...         Format args.
     923 */
     924static void         libWarn(PWLDLIB pLib, const char *pszFormat, ...)
    447925{
    448926    va_list     args;
    449     if (pMod->pLib)
    450         fprintf(stderr, "weakld: %s(%s) - warning: ", pMod->pLib->pszLibName, pMod->pszModName);
    451     else
    452         fprintf(stderr, "weakld: %s - warning: ", pMod->pszModName);
     927    fprintf(stderr, "weakld: %s: warning: ", pLib->pszLibName);
    453928
    454929    va_start(args, pszFormat);
     
    458933        fputc('\n', stderr);
    459934}
     935
     936
     937
     938
     939
     940
     941
     942
     943
    460944
    461945
     
    507991}
    508992
    509 
    510993/**
    511994 * Closes the module.
     
    5221005}
    5231006
    524 
    525 typedef int (*PFNSYMENUM)(PWLD pWld, PWLDSYM pSym, void *pvUser);
     1007/**
     1008 * Report error in module.
     1009 * @param   pMod        Pointer to module to report error on.
     1010 * @param   pszFormat   Format string.
     1011 * @param   ...         Format arguments.
     1012 */
     1013static int      modErr(PWLDMOD pMod, const char *pszFormat, ...)
     1014{
     1015    va_list     args;
     1016    if (pMod->pLib)
     1017        fprintf(stderr, "weakld: %s(%s) - error: ", pMod->pLib->pszLibName, pMod->pszModName);
     1018    else
     1019        fprintf(stderr, "weakld: %s - error: ", pMod->pszModName);
     1020
     1021    va_start(args, pszFormat);
     1022    vfprintf(stderr, pszFormat, args);
     1023    va_end(args);
     1024    if (pszFormat[strlen(pszFormat) - 1] != '\n')
     1025        fputc('\n', stderr);
     1026    return -1;
     1027}
     1028
     1029/**
     1030 * Report warning in module.
     1031 * @param   pMod        Pointer to module to report warning on.
     1032 * @param   pszFormat   Format string.
     1033 * @param   ...         Format arguments.
     1034 */
     1035static void     modWarn(PWLDMOD pMod, const char *pszFormat, ...)
     1036{
     1037    va_list     args;
     1038    if (pMod->pLib)
     1039        fprintf(stderr, "weakld: %s(%s) - warning: ", pMod->pLib->pszLibName, pMod->pszModName);
     1040    else
     1041        fprintf(stderr, "weakld: %s - warning: ", pMod->pszModName);
     1042
     1043    va_start(args, pszFormat);
     1044    vfprintf(stderr, pszFormat, args);
     1045    va_end(args);
     1046    if (pszFormat[strlen(pszFormat) - 1] != '\n')
     1047        fputc('\n', stderr);
     1048}
     1049
     1050
     1051
    5261052
    5271053/**
     
    5481074            if ((pSym->fFlags & fMask) == fFlags)
    5491075            {
    550                 rc = pfnEnum(pWld, pSym, pvUser);   
     1076                rc = pfnEnum(pWld, pSym, pvUser);
    5511077                if (rc)
    5521078                    return rc;
     
    5571083    return 0;
    5581084}
     1085
     1086
     1087/**
     1088 * Worker for wldHaveUndefined().
     1089 * @returns 42 and halts the search.
     1090 * @param   pWld    Linker instance.
     1091 * @param   pSym    Symbol.
     1092 * @param   pvUser  Pointer to a FILE stream.
     1093 */
     1094static int symHaveUndefinedEnum(PWLD pWld, PWLDSYM pSym, void *pvUser)
     1095{
     1096    return 42;
     1097}
     1098
     1099/**
     1100 * Checks if there is unresovled symbols in the link.
     1101 *
     1102 * @returns 1 if there is undefined symbols
     1103 * @returns 0 if all symbols are defined.
     1104 * @param   pWld    Linker instance.
     1105 */
     1106static int symHaveUndefined(PWLD pWld)
     1107{
     1108    return symEnum(pWld, &pWld->Global, WLDSF_UNDEF, WLDSF_TYPEMASK | WLDSF_WEAK, symHaveUndefinedEnum, NULL) == 42;
     1109}
     1110
     1111
     1112/**
     1113 * Enumerates the current undefined externals and try to resolve
     1114 * them using the current library passed in the pvUser structure.
     1115 * @returns
     1116 * @param   pWld    Linker instance.
     1117 * @param   pSym    Undefined symbol.
     1118 * @param   pvUser  Pointer to a WLDSLEPARAM structure.
     1119 *                  fMore will be set
     1120 */
     1121static int symSearchLibEnum(PWLD pWld, PWLDSYM pSym, void *pvUser)
     1122{
     1123    int             rc;
     1124    unsigned        cLoaded = 0;
     1125    PWLDSLEPARAM    pParam = (PWLDSLEPARAM)pvUser;
     1126
     1127    SYMDBG(pSym, "Searching for");
     1128
     1129    /*
     1130     * If we have a dictionary, we'll us it.
     1131     */
     1132    if (pParam->pLib->pDict)
     1133        rc = libTryLoadSymbolThruDictionary(pWld, pParam->pLib, pSym, &cLoaded);
     1134    else
     1135        rc = libLoadUndefSymbols(pWld, pParam->pLib, pSym, &cLoaded);
     1136
     1137    /* Housekeeping. */
     1138    pParam->cLoaded += cLoaded;
     1139    if (rc == 42) /* more undef from the load. */
     1140    {
     1141        pParam->fMore = 1;
     1142        rc = 0;
     1143    }
     1144
     1145    return rc;
     1146}
     1147
     1148/**
     1149 * Worker for enumerating unresolved symbols.
     1150 *
     1151 * @returns 0
     1152 * @param   pWld    Linker instance.
     1153 * @param   pSym    Undefined symbol.
     1154 * @param   pvUser  NULL
     1155 */
     1156static int          symPrintUnDefEnum(PWLD pWld, PWLDSYM pSym, void *pvUser)
     1157{
     1158    PWLDMOD pMod = pSym->pMod;
     1159
     1160    if (pMod)
     1161        modErr(pMod, "Unresolved symbol '%s'.", pSym->pszName);
     1162    else
     1163        wldErr(pWld, "Unresolved symbol '%s'.", pSym->pszName);
     1164    return 0;
     1165}
     1166
     1167/**
     1168 * Prints unresolved symbols.
     1169 *
     1170 * @param   pWld    Linker instance.
     1171 */
     1172static void         symPrintUnDefs(PWLD pWld)
     1173{
     1174    symEnum(pWld, &pWld->Global, WLDSF_UNDEF, WLDSF_TYPEMASK | WLDSF_WEAK, symPrintUnDefEnum, NULL);
     1175}
     1176
     1177/**
     1178 * Checks the OMF encoded name with the specified undefined
     1179 * symbol, or all undefined symbols.
     1180 *
     1181 * @returns 1 if symbol matches.
     1182 * @returns 0 if symbol mis-matches.
     1183 * @param   pWld                Linker instance.
     1184 * @param   pachPascalString    OMF encoded string.
     1185 * @param   pSym                If NULL match all, if !NULL match this.
     1186 */
     1187static int          symMatchUnDef(PWLD pWld, const unsigned char *pachPascalString, PWLDSYM pSym)
     1188{
     1189    int         cch = *pachPascalString;
     1190    const char *psz = pachPascalString + 1;
     1191    int (*pfn)(const char *, const char *, size_t) = (pWld->fFlags & WLDC_CASE_INSENSITIVE) ? strnicmp : strncmp;
     1192
     1193    if (pSym)
     1194        return !pfn(pSym->pszName, psz, cch) && !pSym->pszName[cch];
     1195    else
     1196    {
     1197#if 1   /* hashed */
     1198        unsigned uHash = symHash(psz, cch);
     1199        for (pSym = pWld->Global.ap[uHash]; pSym; pSym = pSym->pHashNext)
     1200        {
     1201            if ((pSym->fFlags & (WLDSF_TYPEMASK | WLDSF_WEAK)) == WLDSF_UNDEF)
     1202            {
     1203                if (!pfn(pSym->pszName, psz, cch) && !pSym->pszName[cch])
     1204                    return 1;
     1205            }
     1206        }
     1207#else
     1208        /* raw */
     1209        int i;
     1210        for (i = 0; i < sizeof(pWld->Global.ap) / sizeof(pWld->Global.ap[0]); i++)
     1211        {
     1212            for (pSym = pWld->Global.ap[i]; pSym; pSym = pSym->pHashNext);
     1213            {
     1214                if ((pSym->fFlags & (WLDSF_TYPEMASK | WLDSF_WEAK)) == WLDSF_UNDEF)
     1215                {
     1216                    if (!pfn(pSym->pszName, psz, cch) && !pSym->pszName[cch])
     1217                        return 1;
     1218                }
     1219            }
     1220        }
     1221#endif
     1222    }
     1223    return 0;
     1224}
     1225
     1226
     1227
     1228
     1229
    5591230
    5601231
     
    5961267    fprintf(stderr, "\n");
    5971268}
    598 #endif 
     1269#endif
    5991270
    6001271
     
    6041275 * @param   pszSym  Symbol to calculate it for.
    6051276 * @param   cch     Symbol length.
    606  */
    607 static inline unsigned hash(const char* pszSym, unsigned cch)
     1277 * @todo    This ain't respecting case sensitivity.
     1278 */
     1279static inline unsigned symHash(const char* pszSym, unsigned cch)
    6081280{
    6091281    unsigned uHash = 0;
    610     while (cch && *pszSym && *pszSym != '$')
     1282    while (     cch
     1283           &&   (pszSym[0] != '$' || pszSym[1] != 'w' || pszSym[2] != '$')
     1284             )
    6111285    {
    6121286        uHash = uHash * 65599 + *pszSym;
     
    6251299 *
    6261300 * We'll simply return existing symbol when:
    627  *      1. adding a UNDEF where a PUBLIC or COMM exists.
     1301 *      1. adding a UNDEF where a PUBLIC or COMM or !WEAK UNDEF exists.
    6281302 *      2. adding a WKEXT where a PUBLIC or COMM exists.
    6291303 *      3. adding a WKEXT where a UNDEF which isn't UNCERTAIN exists.
    630  *      4. adding a COMM where a !WEAK COMM exists. 
     1304 *      4. adding a COMM where a !WEAK COMM exists.
    6311305 *      5. adding a WEAK PUBLIC or WEAK COMM where a PUBLIC or COMM exists.
    6321306 *
     
    6931367            break;
    6941368        }
    695     uHash = hash(pachName, cchName);
    6961369    pszName = (pWld->fFlags & WLDC_CASE_INSENSITIVE ? strpool_addnu : strpool_addn)(pWld->pStrMisc, pachName, cchName);
     1370    uHash = symHash(pszName, cchName);
    6971371
    6981372    /* search for existing symbol  */
     
    7211395        pWld->Global.ap[uHash] = pSym;
    7221396        if (peAction) *peAction = WLDSA_NEW;
     1397        WLDDBG2(("symAdd: New symbol '%s'", pSym->pszName));
    7231398    }
    7241399    else
     
    7301405         *      2. adding a WKEXT where a PUBLIC or COMM exists.
    7311406         *      3. adding a WKEXT where a UNDEF which isn't UNCERTAIN exists.
    732          *      4. adding a COMM where a !WEAK COMM exists. 
     1407         *      4. adding a COMM where a !WEAK COMM exists.
    7331408         *      5. adding a WEAK PUBLIC or WEAK COMM where a PUBLIC or COMM exists.
    7341409         */
    7351410        if ( (     /* 1 */
    7361411                (fFlags & WLDSF_TYPEMASK) == WLDSF_UNDEF
    737              && ((pSym->fFlags & WLDSF_TYPEMASK) == WLDSF_PUBLIC || (pSym->fFlags & WLDSF_TYPEMASK) == WLDSF_COMM)
     1412             && ((pSym->fFlags & WLDSF_TYPEMASK) == WLDSF_PUBLIC || (pSym->fFlags & WLDSF_TYPEMASK) == WLDSF_COMM
     1413                 || (pSym->fFlags & (WLDSF_TYPEMASK | WLDSF_WEAK)) == WLDSF_UNDEF)
    7381414            ) || ( /* 2 */
    7391415                (fFlags & WLDSF_TYPEMASK) == WLDSF_WKEXT
     
    7511427        {
    7521428            if (peAction) *peAction = WLDSA_OLD;
     1429            WLDDBG2(("symAdd: Old symbol '%s'", pSym->pszName));
    7531430        }
    7541431        /*
     
    7631440            modWarn(pMod, "Ignoring import '%s' as it's defined already.", pszName);
    7641441            if (peAction) *peAction = WLDSA_OLD;
     1442            WLDDBG2(("symAdd: Old symbol '%s'", pSym->pszName));
    7651443        }
    7661444        /*
     
    7921470            pSym->fFlags = (pSym->fFlags & ~(WLDSF_TYPEMASK | WLDSF_WEAK | WLDSF_UNCERTAIN | WLDSF_LIBSEARCH)) | fFlags;
    7931471            if (peAction) *peAction = WLDSA_UP;
     1472            WLDDBG2(("symAdd: Upgraded symbol '%s'", pSym->pszName));
    7941473        }
    7951474        /*
     
    8111490            if (peAction) *peAction = WLDSA_UP;
    8121491            memset(&pSym->u, 0, sizeof(pSym->u));
     1492            WLDDBG2(("symAdd: Upgraded symbol '%s'", pSym->pszName));
    8131493        }
    8141494        /*
     
    8201500        else
    8211501        {
    822             modErr(pMod, "Duplicate symbol '%s'.", pszName);
     1502            modErr(pMod, "Duplicate symbol '%s' ('%s').", pszName, pSym->pszName);
    8231503            if (pSym->pMod)
    8241504                modErr(pSym->pMod, "Previous symbol defined in this module.");
     
    11311811    if (!pSym)
    11321812        return NULL;
    1133    
     1813
    11341814    cbElement *= (cElements > 0 ? cElements : 1); /* make it a size */
    11351815    switch (eAction)
     
    11531833            WLDINTERR(pWld, pMod);
    11541834    }
    1155    
     1835
    11561836    return pSym;
    11571837}
     
    11851865    int             i;
    11861866    unsigned long   ul;
    1187     signed long     l, l2, l3;
     1867    signed long     l2, l3;
    11881868    unsigned short  us, us2, us3;
    1189     unsigned char   uch, uch2, uch3;
    1190 
     1869    unsigned char   uch, uch2;
     1870
     1871    /* We're counting indexes from 1, so add dummy zero entry. */
     1872    papExts = xmalloc(sizeof(papExts[0])*64);
     1873    papExts[0] = NULL;
     1874    cExts = 1;
    11911875
    11921876
     
    12081892            void *              pv;
    12091893        } u, u1, u2, u3;
    1210        
     1894
    12111895        /** macro for getting a OMF index out of the buffer */
    12121896        #define OMF_GETINDEX()  (*u.puch & 0x80 ? ((*u.pch++ & 0x7f) << 8) + *u.pch++ : *u.pch++)
     
    12331917            } while (0)
    12341918
    1235 
    12361919        u.pv = &achBuffer[0];
    12371920
     
    12671950            case COMDAT: case COMDAT | REC32:
    12681951            case COMENT: case COMENT | REC32:
     1952            case THEADR: case THEADR | REC32:
    12691953            case LIBHDR:
    12701954                break;
     
    12831967        switch (OmfRec.chType)
    12841968        {
     1969            case THEADR:
     1970            {
     1971                if (!pMod->pszModName)
     1972                {
     1973                    pMod->pszModName = strpool_addn(pWld->pStrMisc, u.pch + 1, *u.puch);
     1974                    WLDINFO(pWld, ("%s", pMod->pszModName));
     1975                }
     1976                break;
     1977            }
     1978
    12851979            case COMENT: case COMENT | REC32:
    12861980                switch (*++u.pch)
     
    13812075                    if (!pSym) goto failure;
    13822076                    SYMDBG(pSym, "EXTDEF");
     2077                    /* put into array of externals */
     2078                    if (!(cExts % 64))
     2079                    {
     2080                        cExts += 64;
     2081                        papExts = xrealloc(papExts, sizeof(papExts[0]) * cExts);
     2082                    }
     2083                    papExts[cExts++] = pSym;
    13832084                }
    13842085                break;
     
    13962097                    ul = OMF_IS32BIT() ? OMF_DWORD() : OMF_WORD();
    13972098                    us = OMF_GETINDEX();            /* typeindex */
    1398                     pSym = symAddPublic(pWld, pMod, fLibSearch, u1.puch + 1, *u1.puch, 
     2099                    pSym = symAddPublic(pWld, pMod, fLibSearch, u1.puch + 1, *u1.puch,
    13992100                                        ul, us3, us2);
    14002101                    if (!pSym) goto failure;
     
    14102111                    u1 = u; u.pch += 1 + *u.puch;   /* alias name */
    14112112                    u2 = u; u.pch += 1 + *u.puch;   /* substitutt name. */
    1412                     pSym = symAddAlias(pWld, pMod, fLibSearch, 
    1413                                        u1.puch + 1, *u1.puch, 
     2113                    pSym = symAddAlias(pWld, pMod, fLibSearch,
     2114                                       u1.puch + 1, *u1.puch,
    14142115                                       u2.puch + 1, *u2.puch);
    14152116                    if (!pSym) goto failure;
     
    14332134                            break;
    14342135                        case COMDEF_TYPENEAR:
    1435                             l2 = 1;                 /* number of elements */ 
    1436                             OMF_GETTYPELEN(l3);     /* element size */       
     2136                            l2 = 1;                 /* number of elements */
     2137                            OMF_GETTYPELEN(l3);     /* element size */
    14372138                            break;
    14382139                        default:
     
    14412142                    }
    14422143
    1443                     pSym = symAddComdef(pWld, pMod, fLibSearch, 
    1444                                         u1.puch + 1, *u1.puch, 
     2144                    pSym = symAddComdef(pWld, pMod, fLibSearch,
     2145                                        u1.puch + 1, *u1.puch,
    14452146                                        l2, l3);
    14462147                    if (!pSym) goto failure;
     
    14812182    /* Make all the EXTDEFs uncertain. */
    14822183    for (i = 0; i < cExts; i++)
    1483         papExts[i]->fFlags &= ~WLDSF_UNCERTAIN;
     2184        if (papExts[i])
     2185            papExts[i]->fFlags &= ~WLDSF_UNCERTAIN;
    14842186
    14852187    return 0;
     
    15112213 * @param   fFlags  Linker flags as defined by enum wld_create_flags.
    15122214 */
    1513 PWLD    wld_create(int fFlags)
     2215PWLD    wldCreate(int fFlags)
    15142216{
    15152217    PWLD    pWld = xmalloc(sizeof(*pWld));
     
    15192221    pWld->fFlags = fFlags;
    15202222    pWld->pStrMisc = strpool_init();
    1521     pWld->ppLibsAdd = &pWld->pObjs;
    1522     pWld->ppObjsAdd = &pWld->pLibs;
     2223    pWld->ppObjsAdd = &pWld->pObjs;
     2224    pWld->ppLibsAdd = &pWld->pLibs;
    15232225
    15242226    /* done */
     
    15352237 * @param   pWld    Linker instance to destroy.
    15362238 */
    1537 int     wld_destroy(PWLD pWld)
     2239int     wldDestroy(PWLD pWld)
    15382240{
    15392241    if (!pWld)
     
    15652267 *          or wld_generate_weaklib()!
    15662268 */
    1567 int     wld_add_object(PWLD pWld, FILE *phFile, const char *pszName)
     2269int     wldAddObject(PWLD pWld, FILE *phFile, const char *pszName)
    15682270{
    15692271    OMFREC  OmfRec = {0,0};
     
    16552357 *          or wld_generate_weaklib()!
    16562358 */
    1657 int     wld_add_deffile(PWLD pWld, FILE *phFile, const char *pszName)
     2359int     wldAddDefFile(PWLD pWld, FILE *phFile, const char *pszName)
    16582360{
    16592361    if (!phFile)
     
    16852387 * @author  Don't call wld_add_library after wld_generate_weaklib()!
    16862388 */
    1687 int     wld_add_library(PWLD pWld, FILE *phFile, const char *pszName)
    1688 {
     2389int     wldAddLibrary(PWLD pWld, FILE *phFile, const char *pszName)
     2390{
     2391    PWLDLIB     pLib;
     2392    int         rc = 0;
     2393
    16892394    if (!phFile)
    16902395        phFile = fopen(pszName, "r");
     
    16972402        fprintf(stderr, "weakld: info: adding library %s\n", pszName);
    16982403
    1699     return 0;
     2404    /* add it to the link, do nothing till we're asked to do the searching. */
     2405    pLib = xmalloc(sizeof(*pLib));
     2406    pLib->phFile = phFile;
     2407    pLib->pszLibName = strpool_add(pWld->pStrMisc, pszName);
     2408    pLib->pNext = NULL;
     2409
     2410    /* read the library header. */
     2411    if (    !fseek(phFile, 0, SEEK_SET)
     2412        &&  fread(&pLib->LibHdr, sizeof(OMFREC), 1, phFile) == 1
     2413        &&  (   pLib->LibHdr.chType != LIBHDR
     2414             || fread(&pLib->LibHdr.offDict, sizeof(pLib->LibHdr) - sizeof(OMFREC), 1, phFile) == 1
     2415                )
     2416        )
     2417    {
     2418        /* link it in */
     2419        *pWld->ppLibsAdd = pLib;
     2420        pWld->ppLibsAdd = &pLib->pNext;
     2421        libClose(pLib);
     2422    }
     2423    else
     2424    {
     2425        /* We failed. */
     2426        libErr(pLib, "Invalid library format or read error.");
     2427        fclose(phFile);
     2428        free(pLib);
     2429        rc = -1;
     2430    }
     2431
     2432    return rc;
     2433}
     2434
     2435
     2436/**
     2437 * Does the linker pass one - chiefly library search as .def and .obj is
     2438 * already processed as pass 1.
     2439 *
     2440 * @returns 0 on success (all symbols resolved)
     2441 * @returns 42 if there are unresolved symbols.
     2442 * @returns Something else on all other errors.
     2443 * @param   pWld    Linker Instance.
     2444 */
     2445int     wldPass1(PWLD pWld)
     2446{
     2447    int     fMore;
     2448    int     cLoaded;
     2449    int     fFirstTime = 1;
     2450
     2451    WLDINFO(pWld, ("Pass 1"));
     2452    do
     2453    {
     2454        PWLDLIB     pLib;
     2455
     2456        cLoaded = fMore = 0;
     2457        for (pLib = pWld->pLibs; pLib; pLib = pLib->pNext)
     2458        {
     2459            int         rc;
     2460            WLDSLEPARAM param;
     2461            WLDINFO(pWld, ("%s", pLib->pszLibName));
     2462
     2463            /*
     2464             * Open the library
     2465             */
     2466            if (!libOpen(pLib))
     2467                continue;
     2468
     2469            /*
     2470             * Load extended dictionary if we wanna use it.
     2471             */
     2472            if (fFirstTime && !(pWld->fFlags & WLDC_NO_EXTENDED_DICTIONARY_SEARCH))
     2473                libLoadDict(pLib);
     2474            else
     2475                libCloseDict(pLib);
     2476
     2477            /*
     2478             * Enumerate undefined symbols and try load them from this library.
     2479             */
     2480            do
     2481            {
     2482                param.fMore = FALSE;
     2483                param.cLoaded = 0;
     2484                param.pLib = pLib;
     2485                if (pLib->pDict)
     2486                    rc = symEnum(pWld, &pWld->Global, WLDSF_UNDEF, WLDSF_TYPEMASK | WLDSF_WEAK, symSearchLibEnum, &param);
     2487                else
     2488                {
     2489                    rc = libLoadUndefSymbols(pWld, pLib, NULL, &param.cLoaded);
     2490                    if (rc == 42)
     2491                    {
     2492                        param.fMore = 1;
     2493                        rc = 0;
     2494                    }
     2495                }
     2496                cLoaded += param.cLoaded;
     2497                if (!param.fMore && param.cLoaded)
     2498                    param.fMore = symHaveUndefined(pWld);
     2499            } while (!rc && param.cLoaded > 0);
     2500
     2501            /* close it */
     2502            libClose(pLib);
     2503            if (rc && rc != 42)
     2504                return rc;
     2505        }
     2506
     2507        /* We only trust this if it's set. */
     2508        fMore = symHaveUndefined(pWld);
     2509        fFirstTime = 0;
     2510    } while (fMore && cLoaded > 0);
     2511
     2512    /* @todo: proper warning? */
     2513    if (fMore)
     2514        symPrintUnDefs(pWld);
     2515
     2516    return fMore ? 42 : 0;
    17002517}
    17012518
     
    17402557        {
    17412558            wldErr(pWld, "Error occured while writing weak aliases. (1)");
    1742             return -1; 
     2559            return -1;
    17432560        }
    17442561
     
    17512568        {
    17522569            wldErr(pWld, "Error occured while writing weak aliases (2).");
    1753             return -1; 
     2570            return -1;
    17542571        }
    17552572
     
    17622579        {
    17632580            wldErr(pWld, "Error occured while writing weak aliases (3).");
    1764             return -1; 
     2581            return -1;
    17652582        }
    17662583
     
    17712588        {
    17722589            wldErr(pWld, "Error occured while writing weak aliases (4).");
    1773             return -1; 
     2590            return -1;
    17742591        }
    17752592    }
     
    17902607 *                  This is an empty string if no weak symbols were found!
    17912608 */
    1792 int     wld_generate_weakobj(PWLD pWld, char *pszName)
     2609int     wldGenerateWeakobj(PWLD pWld, char *pszName)
    17932610{
    17942611    char *      psz;
    17952612    FILE *      phFile;
    1796     int         rc = -1;
    1797 
     2613    int         rc;
     2614
     2615    /* generate the file */
    17982616    *pszName = '\0';
    17992617    psz = _tempnam(NULL, "weako");
     
    18062624        tmpnam(pszName);
    18072625    strcat(pszName, "wk.obj");
    1808 
    1809     if (pWld->fFlags & WLDC_VERBOSE)
    1810         fprintf(stderr, "weakld: info: generating weakobj object file '%s'.\n", pszName);
     2626    WLDINFO(pWld, ("Generating weakobj object file '%s'.", pszName));
     2627
    18112628
    18122629    /* open the file */
     
    18372654        /* Make aliases  */
    18382655        offAlias = ftell(phFile);       /* save this to see if anything is added. */
    1839         rc = symEnum(pWld, &pWld->Global, 
     2656        rc = symEnum(pWld, &pWld->Global,
    18402657                     WLDSF_WEAK, WLDSF_WEAK,
    18412658                     weakobjEnum, phFile);
  • trunk/src/emx/src/emxomf/weakld.h

    • Property cvs2svn:cvs-rev changed from 1.2 to 1.3
    r510 r511  
    1 /* $Id$
     1/* $Id$ */
     2/** @file
    23 *
    34 * Weak Pre-Linker.
     
    3031*******************************************************************************/
    3132struct wld;
     33/** Weak LD Instance Pointer. */
    3234typedef struct wld * PWLD;
    3335
     
    4749*   Functions                                                                  *
    4850*******************************************************************************/
    49 PWLD    wld_create(int fFlags);
    50 int     wld_add_object(PWLD pWld, FILE *phFile, const char *pszName);
    51 int     wld_add_deffile(PWLD pWld, FILE *phFile, const char *pszName);
    52 int     wld_add_library(PWLD pWld, FILE *phFile, const char *pszName);
    53 int     wld_generate_weakobj(PWLD pwld, char *pszName);
    54 int     wld_destroy(PWLD pWld);
     51/** @group Weak LD - Public methods.
     52 * @{ */
     53PWLD    wldCreate(int fFlags);
     54int     wldAddObject(PWLD pWld, FILE *phFile, const char *pszName);
     55int     wldAddDefFile(PWLD pWld, FILE *phFile, const char *pszName);
     56int     wldAddLibrary(PWLD pWld, FILE *phFile, const char *pszName);
     57int     wldPass1(PWLD pWld);
     58int     wldGenerate_weakobj(PWLD pwld, char *pszName);
     59int     wldDestroy(PWLD pWld);
     60/** @} */
    5561
    5662#endif
Note: See TracChangeset for help on using the changeset viewer.