-
Notifications
You must be signed in to change notification settings - Fork 0
/
weakld.c
3944 lines (3526 loc) · 134 KB
/
weakld.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $Id: weakld.c 3753 2012-03-04 21:23:36Z bird $ */
/** @file
* Weak Symbol Pre-Linker.
*
* Copyright (c) 2003 InnoTek Systemberatung GmbH
* Author: knut st. osmundsen <[email protected]>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with This program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/** @page weakld Weak Pre-Linker
*
* In order to get the weak symbols somewhat right it looks like we have to do
* the pass1 of the linking process in order to resolve the weak symbols.
*
*
*
* @subsection Symbols
*
* There is a couple of symbol types, but we can skip most of them for this
* pre-linking operation. We use one symbol type which is public or global
* symbols if you like. Perhaps it would be wise to devide them into separat
* type groups, but the choice was made to differenciate this using flags.
* So, symbol enumeration is done using flag masks.
* @todo: Finish this stuff.
*
*/
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
#define WLDSYM_HASH_SIZE 211
#define OMF_MAX_REC 1024
/** helper to make verbose only output. */
#define WLDINFO(pWld, a) do { if (pWld->fFlags & WLDC_VERBOSE) wldInfo a; } while (0)
/** Internal error */
#define WLDINTERR(pWld, pMod) wldIntErr(pWld, pMod, __FILE__, __LINE__, __FUNCTION__);
//#define WLD_ENABLED_DBG
#ifdef WLD_ENABLED_DBG
#define SYMDBG(pSym, pszMsg) symDbg(pSym, pszMsg);
#define WLDDBG(a) wldDbg a
#define WLDDBG2(a) wldDbg a
#else
#define SYMDBG(pSym, pszMsg) do {} while (0)
#define WLDDBG(a) do {} while (0)
#define WLDDBG2(a) do {} while (0)
#endif
/** Helpers for checking if a symbol is defined strongly. */
#define SYM_IS_DEFINED(fFlags) ( (fFlags & (WLDSF_TYPEMASK | WLDSF_WEAK)) == WLDSF_PUBLIC \
|| (fFlags & (WLDSF_TYPEMASK | WLDSF_WEAK)) == WLDSF_COMM \
|| (fFlags & (WLDSF_TYPEMASK | WLDSF_WEAK)) == WLDSF_IMPORT \
)
/** Compares a existing symbol with a new symbol. */
#define SYM_EQUAL(pWld, pSym, _pszName, _fFlags, _uHash, _cchName) \
( (pSym)->uHash == (_uHash) \
&& (pSym)->pszName == (_pszName) \
)
/** Compares a existing symbol with a potential symbol. */
#define SYM_EQUAL2(pWld, pSym, _pszName, _fFlags, _uHash, _cchName, _pfn) \
( (pSym)->uHash == (_uHash) \
&& !_pfn((pSym)->pszName, (_pszName), (_cchName)) \
&& !(pSym)->pszName[(_cchName)] \
)
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <process.h>
#include <sys/omflib.h>
#include <sys/moddef.h>
#include "defs.h"
#include "grow.h"
#include "weakld.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** @group OMF stuff
* @{ */
/** OMF record header. */
#pragma pack(1)
typedef struct OMFREC
{
unsigned char chType;
unsigned short cb;
} OMFREC, *POMFREC;
#pragma pack()
/** OMF library header. */
#pragma pack(1)
typedef struct OMFLIBHDR
{
unsigned char chType;
unsigned short cb;
unsigned long offDict;
unsigned short cDictBlocks;
unsigned char fchFlags;
} OMFLIBHDR, *POMFLIBHDR;
#pragma pack()
/** OMF library header - for combining with OMFREC. */
#pragma pack(1)
typedef struct OMFLIBHDRX
{
unsigned long offDict;
unsigned short cDictBlocks;
unsigned char fchFlags;
} OMFLIBHDRX, *POMFLIBHDRX;
#pragma pack()
/** @} */
/**
* Library structure
*/
typedef struct wldlib
{
/** Library name. */
const char *pszLibName;
/** Filehandle if open */
FILE * phFile;
/** Pointer to extended dictiorary. */
void * pDict;
/** Library header. */
OMFLIBHDR LibHdr;
/** Linked list next pointer. */
struct wldlib * pNext;
} WLDLIB, *PWLDLIB;
/**
* Module structure
*/
typedef struct wldmod
{
/** Module name. */
const char *pszModName;
/** Filehandle if open. */
FILE * phFile;
/** Module offset into the file. */
off_t off;
/** Library relation - not used for libraries added thru wld_add_object(). */
PWLDLIB pLib;
/* Linked list next pointer */
struct wldmod *pNext;
} WLDMOD, *PWLDMOD;
/**
* Symbol structure.
*/
typedef struct wldsym
{
/** Symbol name. */
const char * pszName;
/** Weak name - for weak symbols only. */
const char * pszWeakName;
/** The full hash value. */
unsigned uHash;
/** Symbol flags. */
enum {
/** @group Symbol Type
* @{ */
/* Mask of the symbol type. */
WLDSF_TYPEMASK = 0x000f,
/** Strong symbol.
* A strong definition exists for this symbol (PUBDEF). */
WLDSF_PUBLIC = 0x0001,
/** Communal data/code.
* Communal definition exists for this symbol.
* If a PUBDEF is found for this symbol it will become strong. */
WLDSF_COMM = 0x0002,
/** Imported symbol.
* This symbol is imported.
* Note that when combined with WLDSF_LIBSEARCH a symbol in this type
* will yield to stronger definitions with a little warning. */
WLDSF_IMPORT = 0x0003,
/** Undefined symbol.
* This symbol is not yet defined anywhere. */
WLDSF_UNDEF = 0x0004,
/** Weak external.
* This symbol doesn't need to be resolved as it have a default
* resolution. pWeakDefault is pointing to the default resolution.
* If an non weak EXTDEF is found in another module, it will become
* WLDSF_UNDEF.
*/
WLDSF_WKEXT = 0x0005,
/** Exported name.
* This is used when the exported name differs from the internal one.
* This name is not considered in the linking, only the internal one. */
WLDSF_EXP_NM = 0x0006,
/** @} */
/** Uncertain undefined symbol.
* We're still processing the module containing this and the uncertainty
* we're facing is that a WLDSF_UNDEF may changed to an WLDSF_WKEXT
* upon encountering a WKEXT COMENT record. */
WLDSF_UNCERTAIN = 0x0100,
/** Symbol found during library search.
* If this is an attribute to a symbol of the WLDSF_IMPORT type, the
* symbol is actually kind of weak.
*/
WLDSF_LIBSEARCH = 0x0200,
/** Weak symbol.
* This symbol doesn't need to be resolved (if WLDSF_UNDEF), or
* it may be overridden by a EXTDEF with no WKEXT.
* If this is an weak undefined symbol (extern weak, local default)
* pWeakDefault Will point at it.
*/
WLDSF_WEAK = 0x0400,
/** Alias symbol.
* This symbol is an alias for another symbol. pAliasFor will be set. */
WLDSF_ALIAS = 0x0800,
/** Internal flags which indicates that the symbol was exported in
* the definition file. This to select symbols which only appears in
* __declspec(dllexport) and put them into the definition file for
* proper weak handling. */
WLDSF_EXPORT_DEF = 0x2000,
/** Internal flag to say that we've already aliased this weak
* in the definition file. */
WLDSF_WEAKALIASDONE = 0x8000
} fFlags;
/** The module this symbol is defined in. */
PWLDMOD pMod;
/** Array of modules refering this symbol. */
PWLDMOD * paReferers;
/** Number of modules in the array. */
unsigned cReferers;
/** Per type data union */
union type_data
{
struct import_data
{
/** @group Import Attributes
* Valid when type is WLDSF_IMPORT.
* @{ */
/** Import name. */
const char * pszImpName;
/** Import module. */
const char * pszImpMod;
/** Import Ordinal (WLDSF_IMPORT).
* 0 means no ordinal. */
unsigned uImpOrd;
/** @} */
} import;
struct comm_data
{
/** Size of comm object */
signed long cb;
/** Number of elements. */
signed long cElements;
} comm;
} u;
/** Weak default resolution.
* Valid if the symbol is of type WLDSF_WKEXT or have the WLDSF_WEAK flag set.
* Indicates a default resolution for the symbol.
* For WLDSF_WEAK this only make sense if the type is WLDSF_UNDEF.
*/
struct wldsym * pWeakDefault;
/** Symbol this is an alias for.
* Valid when WLDSF_ALIAS or WLDSF_EXP_NM are set. */
struct wldsym * pAliasFor;
/** @group Export Attributes
* Valid when WLDSF_EXP_NM is set.
* @{ */
/** Export flags. */
enum
{
/** @group Name */
/** Name type mask. */
WLDSEF_NAMEMASK = 0x03,
/** Default action depending on if it have an ordinal or not.
* If it have an ordinal it shall be non-resident, if it hasn't
* it shall be resident.
*/
WLDSEF_DEFAULT = 0x00,
/** The name shall be in the resident nametable. */
WLDSEF_RESIDENT = 0x01,
/** The name shall be in the resident nametable. */
WLDSEF_NONRESIDENT = 0x02,
/** The export shall only have ordinal. */
WLDSEF_NONAME = 0x03,
/** @} */
/** no idea what this implies */
WLDSEF_NODATA = 0x04,
/** From an EXPORT section of a .DEF-file. */
WLDSEF_DEF_FILE = 0x80
} fExport;
/** Export word count. */
unsigned cExpWords;
/** Export Ordinal.
* 0 means no ordinal. */
unsigned uExpOrd;
/** @} */
/** Next node in the hash bucket. */
struct wldsym * pHashNext;
} WLDSYM, *PWLDSYM;
/**
* Symbol table.
*/
typedef struct wldsymtab
{
PWLDSYM ap[WLDSYM_HASH_SIZE];
} WLDSYMTAB, *PWLDSYMTAB;
/**
* Weak Pre-Linker Instance.
*/
struct wld
{
/** Linker flags. */
unsigned fFlags;
/** Global symbols. */
WLDSYMTAB Global;
/** Exported symbols. */
WLDSYMTAB Exported;
/** Module definition file. */
PWLDMOD pDef;
/** Linked list (FIFO) of objects included in the link. */
PWLDMOD pObjs;
PWLDMOD * ppObjsAdd;
/** Linked list (FIFO) of libraries to be searched in the link. */
PWLDLIB pLibs;
PWLDLIB * ppLibsAdd;
/** string pool for miscellaneous string. */
struct strpool * pStrMisc;
/** @group ILINK Crash Workaround
* @{ */
/** Maximum number of externals in a object module. */
unsigned cMaxObjExts;
/** Maximum number of externals in a library module. */
unsigned cMaxLibExts;
/** @} */
};
typedef struct wld WLD;
/** symAdd Action. */
typedef enum { WLDSA_NEW, WLDSA_UP, WLDSA_OLD, WLDSA_ERR } WLDSYMACTION, *PWLDSYMACTION;
extern void *xrealloc (void *ptr, size_t n);
extern void *xmalloc (size_t n);
/** @group Weak LD - Linker Methods (Private)
* @{ */
#ifdef WLD_ENABLED_DBG
static void wldDbg(const char *pszFormat, ...);
#endif
static void wldInfo(const char *pszFormat, ...);
static int wldWarn(PWLD pWld, const char *pszFormat, ...);
static int wldErr(PWLD pWld, const char *pszFormat, ...);
static void wldIntErr(PWLD pWld, PWLDMOD pMod, const char *pszFile, unsigned iLine, const char *pszFunction);
static unsigned pass1ReadOMFMod(PWLD pWld, PWLDMOD pMod, int fLibSearch);
/** Parameter structure for wldDefCallback(). */
typedef struct wldDefCallback_param
{
/** Linker instance. */
PWLD pWld;
/** Name of .def file. */
PWLDMOD pMod;
/** Callback return code. Zero ok; non-zero failure; */
int rc;
} WLDDEFCBPARAM, *PWLDDEFCBPARAM;
static int wldDefCallback(struct _md *pMD, const _md_stmt *pStmt, _md_token eToken, void *pvArg);
/** @} */
/** @group Weak LD - Library Methods (Private)
* @{ */
static FILE * libOpen(PWLDLIB pLib);
static void libClose(PWLDLIB pLib);
static int libLoadDict(PWLDLIB pLib);
static void libCloseDict(PWLDLIB pLib);
static int libTryLoadSymbolThruDictionary(PWLD pWld, PWLDLIB pLib, PWLDSYM pSym, unsigned *pcLoaded);
static int libLoadUndefSymbols(PWLD pWld, PWLDLIB pLib, PWLDSYM pSym, unsigned *pcLoaded);
static int libErr(PWLDLIB pLib, const char *pszFormat, ...);
#if 0
static void libWarn(PWLDLIB pLib, const char *pszFormat, ...);
#endif
/** @} */
/** @group Weak LD - Module Methods (Private)
* @{ */
static FILE * modOpen(PWLDMOD pMod);
static void modClose(PWLDMOD pMod);
static int modErr(PWLDMOD pMod, const char *pszFormat, ...);
static void modWarn(PWLDMOD pMod, const char *pszFormat, ...);
/** @} */
/** @group Weak LD - Symbole Methods (Private)
* @{ */
typedef int (*PFNSYMENUM)(PWLD pWld, PWLDSYM pSym, void *pvUser);
static int symHaveUndefined(PWLD pWld);
static int symEnum(PWLD pWld, PWLDSYMTAB pSymTab, unsigned fFlags, unsigned fMask, PFNSYMENUM pfnEnum, void *pvUser);
static int symPrintUnDefEnum(PWLD pWld, PWLDSYM pSym, void *pvUser);
static int symMatchUnDef(PWLD pWld, const unsigned char *pachPascalString, PWLDSYM pSym);
/** Pointed to by the pvUser parameter of symSearchLibEnum(). */
typedef struct symSearchLibEnum_param
{
/** Library to search for symbols in. */
PWLDLIB pLib;
/** Number modules which was loaded. */
unsigned cLoaded;
} WLDSLEPARAM, *PWLDSLEPARAM;
static int symSearchLibEnum(PWLD pWld, PWLDSYM pSym, void *pvUser);
static inline unsigned symHash(const char* pszSym, unsigned cch, unsigned fWldCaseFlag);
static const char * symGetDescr(PWLDSYM pSym);
static void symDumpReferers(PWLDSYM pSym);
static void symDbg(PWLDSYM pSym, const char *pszMsg);
static PWLDSYM symAdd(PWLD pWld, PWLDMOD pMod, unsigned fFlags, const char *pachName, int cchName, PWLDSYMTAB pSymTab, PWLDSYMACTION peAction);
static PWLDSYM symAddImport(PWLD pWld, PWLDMOD pMod, int fLibSearch,
const char *pachName, int cchName,
const char *pachImpName, int cchImpName,
const char *pachModName, int cchModName,
unsigned uOrdinal);
static PWLDSYM symAddExport(PWLD pWld, PWLDMOD pMod, int fLibSearch,
unsigned fExport,
unsigned cExpWords,
const char *pachExpName, int cchExpName,
const char *pachIntName, int cchIntName,
unsigned uOrdinal);
static PWLDSYM symAddPublic(PWLD pWld, PWLDMOD pMod, int fLibSearch,
const char *pachName, int cchName,
unsigned long ulValue, int iSegment, int iGroup);
static PWLDSYM symAddUnDef(PWLD pWld, PWLDMOD pMod, int fLibSearch,
const char *pachName, int cchName);
static PWLDSYM symAddAlias(PWLD pWld, PWLDMOD pMod, int fLibSearch,
const char *pachAliasName, int cchAliasName,
const char *pachName, int cchName);
static PWLDSYM symAddComdef(PWLD pWld, PWLDMOD pMod, int fLibSearch,
const char *pachName, int cchName,
signed long cElements, signed long cbElement);
/** @} */
/*=============================================================================
* *
* *
* W E A K L I N K E R M E T H O D S *
* W E A K L I N K E R M E T H O D S *
* W E A K L I N K E R M E T H O D S *
* W E A K L I N K E R M E T H O D S *
* W E A K L I N K E R M E T H O D S *
* *
* *
*============================================================================*/
#ifdef WLD_ENABLED_DBG
/**
* Put out a debug message.
* @param pszFormat Format string.
* @param ... Format arguments.
*/
static void wldDbg(const char *pszFormat, ...)
{
va_list args;
fprintf(stderr, "weakld: dbg: ");
va_start(args, pszFormat);
vfprintf(stderr, pszFormat, args);
va_end(args);
if (pszFormat[strlen(pszFormat) - 1] != '\n')
fputc('\n', stderr);
}
#endif
/**
* Put out a info message.
* @param pszFormat Format string.
* @param ... Format arguments.
*/
static void wldInfo(const char *pszFormat, ...)
{
va_list args;
fprintf(stderr, "weakld: info: ");
va_start(args, pszFormat);
vfprintf(stderr, pszFormat, args);
va_end(args);
if (pszFormat[strlen(pszFormat) - 1] != '\n')
fputc('\n', stderr);
}
/**
* Put out a warning message.
* @param pszFormat Format string.
* @param ... Format arguments.
*/
static int wldWarn(PWLD pWld, const char *pszFormat, ...)
{
va_list args;
fprintf(stderr, "weakld: warning: ");
va_start(args, pszFormat);
vfprintf(stderr, pszFormat, args);
va_end(args);
if (pszFormat[strlen(pszFormat) - 1] != '\n')
fputc('\n', stderr);
return 4;
}
/**
* Put out a error message.
* @param pszFormat Format string.
* @param ... Format arguments.
*/
static int wldErr(PWLD pWld, const char *pszFormat, ...)
{
va_list args;
fprintf(stderr, "weakld: error: ");
va_start(args, pszFormat);
vfprintf(stderr, pszFormat, args);
va_end(args);
if (pszFormat[strlen(pszFormat) - 1] != '\n')
fputc('\n', stderr);
return -1;
}
/**
* Internal error.
*
* @returns don't return, it aborts the process.
* @param pWld Pointer to linker instance (optional).
* @param pMod Pointe to module (optional).
* @param pszFile File name of the error.
* @param iLine Line number of the error.
* @param pszFunction The function in which the error occured.
*/
static void wldIntErr(PWLD pWld, PWLDMOD pMod, const char *pszFile, unsigned iLine, const char *pszFunction)
{
fprintf(stderr, "\nweakld: ");
if (pMod)
{
if (pMod->pLib)
fprintf(stderr, "%s(%s) ", pMod->pLib->pszLibName, pMod->pszModName);
else
fprintf(stderr, "%s ", pMod->pszModName);
}
fprintf(stderr, "internal error!");
fprintf(stderr, "file: %s line: %d function: %s\n", pszFile, iLine, pszFunction);
abort();
}
/*=============================================================================
* *
* *
* L I B R A R Y M E T H O D S *
* L I B R A R Y M E T H O D S *
* L I B R A R Y M E T H O D S *
* L I B R A R Y M E T H O D S *
* L I B R A R Y M E T H O D S *
* *
* *
*============================================================================*/
/**
* Open this library file.
*
* @returns Pointer to open file stream.
* @returns NULL on failure.
* @param pLib Library to open.
*/
static FILE * libOpen(PWLDLIB pLib)
{
if (!pLib->phFile)
{
pLib->phFile = fopen(pLib->pszLibName, "rb");
if (!pLib->phFile)
libErr(pLib, "Failed to open library.");
}
return pLib->phFile;
}
/**
* Close this library file.
* @param pLib Library to close.
*/
static void libClose(PWLDLIB pLib)
{
if (pLib->phFile)
{
fclose(pLib->phFile);
pLib->phFile = NULL;
}
}
/**
* Load the dictionar for this library into memory.
*
* @returns 0 if we successfully loaded the dictionary.
* @returns -1 if we fail to read the dictionary into memory.
* @returns 1 if there is no dictionary.
* @param pLib Library which dictionary is to be loaded.
* @remark This method will open the library. libClose() must be
* called after this function is used.
*/
static int libLoadDict(PWLDLIB pLib)
{
#if 0
FILE *phFile;
/* been here, done that? */
if (pLib->pDict)
return 0;
/* check if it acutally is a library and have an ext dict */
if ( pLib->LibHdr.chType != LIBHDR
|| pLib->LibHdr.offDict != 0
|| pLib->LibHdr.cDictBlocks != 0)
return 1;
/* ensure it's open. */
phFile = libOpen(pLib);
if (!phFile)
return -1;
/* position us */
if (fseek(phFile, pLib->LibHdr.offDict, SEEK_SET))
return libErr(pLib, "Failed to seek to extended dictionary (offset %d).", (int)pLib->LibHdr.offDict);
/* read it */
pLib->pDict = xmalloc(pLib->LibHdr.cDictBlocks * 512);
if (fread(pLib->pDict, 512, pLib->LibHdr.cDictBlocks, phFile) == pLib->LibHdr.cDictBlocks)
return 0;
libErr(pLib, "Failed to read extended dictionary.");
free(pLib->pDict);
pLib->pDict = NULL;
return -1;
#else
/* till we support processing the dictionary, we pretend there is none. */
pLib->pDict = NULL;
return -1;
#endif
}
/**
* This closes the extended dictionary.
*
* @param pLib Library which extended dictionary should be closed.
* @remark Will not close the library file, libClose() must be used for that.
*/
static void libCloseDict(PWLDLIB pLib)
{
if (pLib->pDict)
{
free(pLib->pDict);
pLib->pDict = NULL;
}
}
/**
* Does a dictionary lookup on an undefined name.
*
* @returns 0 on non failure.
* @returns 42 if not found.
* @returns -1 on link abort error.
* @param pWld Linker instance.
* @param pLib Library to search.
* @param pSym Undefined symbol to search for.
* @param pcLoaded Number of modules which was loaded from this library.
*/
static int libTryLoadSymbolThruDictionary(PWLD pWld, PWLDLIB pLib, PWLDSYM pSym, unsigned *pcLoaded)
{
return libLoadUndefSymbols(pWld, pLib, pSym, pcLoaded); /* @todo implement this function! */
}
/**
* Read thru an module looking for definitions for undef symbols.
* If a definition is found we'll load the module containing it.
*
* @returns 0 on non failure.
* @returns 42 if none found.
* @returns -1 on link abort error.
* @param pWld Linker instance.
* @param pLib Library to search.
* @param pSym Undefined symbol to search for.
* If NULL we'll try and see if any defined global symbol we
* encounter is undefined.
* @param pcLoaded Number of modules which was loaded from this library.
*/
static int libLoadUndefSymbols(PWLD pWld, PWLDLIB pLib, PWLDSYM pSym, unsigned *pcLoaded)
{
FILE * phFile = pLib->phFile;
unsigned char uchEnd1, uchEnd2;
OMFREC OmfRec;
off_t offCurMod = 0;
int fSkipRestOfModule = 0;
/* generic stuff */
unsigned long ul;
signed long l2, l3;
unsigned short us, us2, us3;
unsigned char uch, uch2;
/* Position the library at the first module record. */
if (fseek(phFile, pLib->LibHdr.chType == LIBHDR ? pLib->LibHdr.cb + 3 : 0, SEEK_SET))
return libErr(pLib, "Error when seeking to first module.");
if (pLib->LibHdr.chType != LIBHDR)
{
uchEnd1 = MODEND;
uchEnd2 = MODEND | REC32;
}
else
uchEnd1 = uchEnd2 = LIBEND;
OmfRec.chType = uchEnd1;
fread(&OmfRec, sizeof(OmfRec), 1, phFile);
while (OmfRec.chType != uchEnd1 && OmfRec.chType != uchEnd2)
{
int fRead = 0;
int fLoad = 0;
switch (OmfRec.chType)
{
case THEADR:
fSkipRestOfModule = 0;
offCurMod = ftell(phFile) - sizeof(OmfRec);
break;
/* read */
case PUBDEF: case PUBDEF | REC32:
case ALIAS: case ALIAS | REC32:
case COMDEF: case COMDEF | REC32:
case COMDAT: case COMDAT | REC32:
case COMENT: case COMENT | REC32:
fRead = !fSkipRestOfModule;
break;
}
if (fRead)
{
unsigned char achBuffer[OMF_MAX_REC + 8];
union
{
unsigned char * puch;
signed char * pch;
unsigned short * pus;
signed short * ps;
unsigned long * pul;
signed long * pl;
void * pv;
} u, u1, u2;
/** macro for getting a OMF index out of the buffer */
#define OMF_GETINDEX() (*u.puch & 0x80 ? ((*u.pch++ & 0x7f) << 8) + *u.pch++ : *u.pch++)
#define OMF_BYTE() (*u.puch++)
#define OMF_WORD() (*u.pus++)
#define OMF_24BITWORD() (OMF_BYTE() | (OMF_WORD() << 8))
#define OMF_DWORD() (*u.pul++)
#define OMF_MORE() (u.puch - &achBuffer[0] < (int)OmfRec.cb - 1 && !fLoad) /* (different from the next) */
#define OMF_IS32BIT() ((OmfRec.chType & REC32) != 0)
#define OMF_GETTYPELEN(l) \
do \
{ \
l = OMF_BYTE(); \
if (l > 128) \
switch (l) \
{ \
case 0x81: l = OMF_WORD(); break; \
case 0x84: l = OMF_24BITWORD(); break; \
case 0x88: l = OMF_DWORD(); break; \
default: \
libErr(pLib, "Invalid type length!");/* (different from the next) */ \
return -1; \
} \
} while (0)
u.pv = &achBuffer[0];
/* read it */
if (fread(achBuffer, OmfRec.cb, 1, phFile) != 1)
{
libErr(pLib, "Read error. (2)");
break;
}
/* extract public symbols. */
switch (OmfRec.chType)
{
case COMENT: case COMENT | REC32:
uch = OMF_BYTE(); /* comment type */
uch = OMF_BYTE(); /* comment class */
switch (uch)
{
case CLASS_PASS:
fSkipRestOfModule = 1;
break;
case CLASS_OMFEXT:
{
switch (OMF_BYTE())
{ /*
* Import definition.
*/
case OMFEXT_IMPDEF:
{
uch = OMF_BYTE(); /* flags */
u1 = u; u.pch += 1 + *u.puch; /* internal name */
u2 = u; u.pch += 1 + *u.puch; /* module name */
ul = 0; /* ordinal */
if (uch & 1)
ul = OMF_WORD();
if (symMatchUnDef(pWld, u1.pch, pSym))
fLoad = 1;
break;
}
}
}
} /* comment class */
break;
case PUBDEF: case PUBDEF | REC32:
{
us2 = OMF_GETINDEX(); /* group index */
us3 = OMF_GETINDEX(); /* segment index */
if (!us3)
us = OMF_WORD(); /* base frame - ignored */
while (OMF_MORE())
{
u1 = u; u.pch += 1 + *u.puch; /* public name */
ul = OMF_IS32BIT() ? OMF_DWORD() : OMF_WORD();
us = OMF_GETINDEX(); /* typeindex */
if (symMatchUnDef(pWld, u1.pch, pSym))
fLoad = 1;
}
break;
}
case ALIAS: case ALIAS | REC32:
{
while (OMF_MORE())
{
u1 = u; u.pch += 1 + *u.puch; /* alias name */
u2 = u; u.pch += 1 + *u.puch; /* substitutt name. */
if (symMatchUnDef(pWld, u1.pch, pSym))
fLoad = 1;
}
break;
}
case COMDEF: case COMDEF | REC32:
{
while (OMF_MORE())
{
u1 = u; u.pch += 1 + *u.puch; /* communal name (specs say 1-2 length...) */
us2 = OMF_GETINDEX(); /* typeindex */
uch2 = OMF_BYTE(); /* date type */
switch (uch2)
{
case COMDEF_TYPEFAR:
OMF_GETTYPELEN(l2); /* number of elements */
OMF_GETTYPELEN(l3); /* element size */
break;
case COMDEF_TYPENEAR:
l2 = 1; /* number of elements */
OMF_GETTYPELEN(l3); /* element size */
break;
default:
libErr(pLib, "Invalid COMDEF type %x.", (int)uch2);
return -1;
}
if (symMatchUnDef(pWld, u1.pch, pSym))
fLoad = 1;
}
break;
}
case COMDAT: case COMDAT | REC32:
{
/* @todo */
break;
}
} /* switch */
#undef OMF_GETINDEX
#undef OMF_BYTE
#undef OMF_WORD
#undef OMF_24BITWORD
#undef OMF_DWORD
#undef OMF_MORE
#undef OMF_IS32BIT
#undef OMF_GETTYPELEN
/*
* Shall we load this module?
*/
if (fLoad)
{
off_t offSave = ftell(phFile);
PWLDMOD pMod;
int rc;
pMod = xmalloc(sizeof(*pMod));
memset(pMod, 0, sizeof(*pMod));
pMod->off = offCurMod;
pMod->pLib = pLib;
pMod->phFile = phFile;
*pWld->ppObjsAdd = pMod;
pWld->ppObjsAdd = &pMod->pNext;
rc = pass1ReadOMFMod(pWld, pMod, 1);
if (rc)
{
libErr(pLib, "Failed when reading module at offset %x.", (int)offCurMod);
return rc;
}
/* update statistics */
if (pcLoaded)
(*pcLoaded)++;
/* if one symbol, we're done now */
if (pSym)
return 0;
/* Resume searching, but skip the rest of this one */
fSkipRestOfModule = 1;
fseek(phFile, offSave, SEEK_SET);
}
}