4 * (C) Copyright 1989-1995
11 * With contributions for the
12 * object libraries from
14 * kenh@cmf.nrl.navy.mil
18 #define EQ(A,B) !strcmp((A),(B))
19 #define MAXLINE 254 /*when using fgets*/
28 * The module lklibr.c contains the functions which
29 * (1) specify the path(s) to library files [.LIB]
30 * (2) specify the library file(s) [.LIB] to search
31 * (3) search the library files for specific symbols
32 * and link the module containing this symbol
34 * lklibr.c contains the following functions:
45 /*)Function VOID addpath()
47 * The function addpath() creates a linked structure containing
48 * the paths to various object module library files.
51 * lbpath *lbph pointer to new path structure
52 * lbpath *lbp temporary pointer
55 * lbpath *lbphead The pointer to the first
59 * char getnb() lklex.c
60 * VOID * new() lksym.c
61 * int strlen() c_library
62 * char * strcpy() c_library
63 * VOID unget() lklex.c
66 * An lbpath structure may be created.
72 struct lbpath *lbph, *lbp;
74 lbph = (struct lbpath *) new (sizeof(struct lbpath));
75 if (lbphead == NULL) {
84 lbph->path = (char *) new (strlen(ip)+1);
85 strcpy(lbph->path, ip);
88 /*)Function VOID addlib()
90 * The function addlib() tests for the existance of a
91 * library path structure to determine the method of
92 * adding this library file to the library search structure.
94 * This function calls the function addfile() to actually
95 * add the library file to the search list.
98 * lbpath *lbph pointer to path structure
101 * lbpath *lbphead The pointer to the first
103 * ip a pointer to the library name
106 * VOID addfile() lklibr.c
107 * char getnb() lklex.c
108 * VOID unget() lklex.c
111 * The function addfile() may add the file to
112 * the library search list.
125 foundcount=addfile(NULL, ip);
129 for (lbph=lbphead; lbph; lbph=lbph->next)
131 foundcount+=addfile(lbph->path, ip);
136 printf("?ASlink-Warning-Couldn't find library '%s'\n", ip);
140 /*)Function int addfile(path,libfil)
142 * char *path library path specification
143 * char *libfil library file specification
145 * The function addfile() searches for the library file
146 * by concatenating the path and libfil specifications.
147 * if the library is found, an lbname structure is created
148 * and linked to any previously defined structures. This
149 * linked list is used by the function fndsym() to attempt
150 * to find any undefined symbols.
152 * The function does not give report an error on invalid
153 * path / file specifications or if the file is not found.
156 * lbname *lbnh pointer to new name structure
157 * lbname *lbn temporary pointer
160 * lbname *lbnhead The pointer to the first
164 * char getnb() lklex.c
165 * VOID * new() lksym.c
166 * int strlen() c_library
167 * char * strcpy() c_library
168 * VOID unget() lklex.c
171 * An lbname structure may be created.
174 * 1: the library was found
175 * 0: the library was not found
178 int addfile(char * path, char * libfil)
182 struct lbname *lbnh, *lbn;
189 str = (char *) new (strlen(path) + strlen(libfil) + 6);
192 if (str[strlen(str)-1] != '/')
200 str = (char *) new (strlen(libfil) + 5);
204 if (libfil[0] == '/')
212 if(strchr(libfil, FSEPX) == NULL)
214 sprintf(&str[strlen(str)], "%clib", FSEPX);
220 /*Ok, that didn't work. Try with the 'libfil' name only*/
222 if(libfilinc) libfil--;
224 fp=fopen(libfil, "r");
227 /*Bingo! 'libfil' is the absolute path of the library*/
229 path=NULL;/*This way 'libfil' and 'path' will be rebuilt from 'str'*/
235 /*'path' can not be null since it is needed to find the '.rel' files associated with
236 the library. So, get 'path' from 'str' and then chop it off and recreate 'libfil'.
237 That way putting 'path' and 'libfil' together will result into the original filepath
238 as contained in 'str'.*/
240 path = (char *) new (strlen(str));
242 for(j=strlen(path)-1; j>=0; j--)
244 if((path[j]=='\\')||(path[j]=='/'))
246 strcpy(libfil, &path[j+1]);
257 lbnh = (struct lbname *) new (sizeof(struct lbname));
271 lbnh->libfil = (char *) new (strlen(libfil) + 1);
272 strcpy(lbnh->libfil,libfil);
283 /*)Function VOID search()
285 * The function search() looks through all the symbol tables
286 * at the end of pass 1. If any undefined symbols are found
287 * then the function fndsym() is called. Function fndsym()
288 * searches any specified library files to automagically
289 * import the object modules containing the needed symbol.
291 * After a symbol is found and imported by the function
292 * fndsym() the symbol tables are again searched. The
293 * symbol tables are search until no more symbols can be
294 * resolved within the library files. This ensures that
295 * back references from one library module to another are
299 * int i temporary counter
300 * sym *sp pointer to a symbol structure
301 * int symfnd found a symbol flag
304 * sym *symhash[] array of pointers to symbol tables
307 * int fndsym() lklibr.c
310 * If a symbol is found then the library object module
311 * containing the symbol will be imported and linked.
317 register struct sym *sp;
318 register int i,symfnd;
321 * Look for undefined symbols. Keep
322 * searching until no more symbols are resolved.
328 * Look through all the symbols
330 for (i=0; i<NHASH; ++i) {
333 /* If we find an undefined symbol
334 * (one where S_DEF is not set), then
335 * try looking for it. If we find it
336 * in any of the libraries then
337 * increment symfnd. This will force
338 * another pass of symbol searching and
339 * make sure that back references work.
341 if ((sp->s_type & S_DEF) == 0) {
342 if (fndsym(sp->s_id)) {
352 /*Load a .rel file embedded in a sdcclib file*/
353 void LoadRel(FILE * libfp, char * ModName)
358 while (fgets(str, NINPUT, libfp) != NULL)
360 str[NINPUT+1] = '\0';
365 if(EQ(str, "<FILE>"))
367 fgets(str, NINPUT, libfp);
368 str[NINPUT+1] = '\0';
370 if(EQ(str, ModName)) state=1;
373 printf("Bad offset in library file str=%s, Modname=%s\n", str, ModName);
379 if(EQ(str, "<REL>")) state=2;
382 if(EQ(str, "</REL>")) return;
390 /*Load an .adb file embedded in a sdcclib file. If there is
391 something between <ADB> and </ADB> returns 1, otherwise returns 0.
392 This way the aomf51 will not have uselless empty modules. */
394 int LoadAdb(FILE * libfp)
400 while (fgets(str, MAXLINE, libfp) != NULL)
402 str[NINPUT+1] = '\0';
407 if(EQ(str, "<ADB>")) state=1;
410 if(EQ(str, "</ADB>")) return ToReturn;
411 fprintf(dfp, "%s\n", str);
419 /*Check for a symbol in a SDCC library. If found, add the embedded .rel and
420 .adb files from the library. The library must be created with the SDCC
421 librarian 'sdcclib' since the linking process depends on the correct file offsets
422 embedded in the library file.*/
424 int SdccLib(char * PathLib, FILE * libfp, char * DirLib, char * SymName)
426 struct lbfile *lbfh, *lbf;
427 char ModName[NCPS]="";
428 char FLine[MAXLINE+1];
430 long IndexOffset=0, FileOffset;
435 fgets(FLine, MAXLINE, libfp);
441 if(EQ(FLine, "<INDEX>"))
443 /*The next line has the size of the index*/
445 fgets(FLine, MAXLINE, libfp);
447 IndexOffset=atol(FLine);
452 if(EQ(FLine, "<MODULE>"))
454 /*The next line has the name of the module and the offset
455 of the corresponding embedded file in the library*/
457 fgets(FLine, MAXLINE, libfp);
459 sscanf(FLine, "%s %ld", ModName, &FileOffset);
462 else if(EQ(FLine, "</INDEX>"))
464 /*Reached the end of the index. The symbol is not in this library.*/
469 if(EQ(FLine, "</MODULE>"))
471 /*The symbol is not in this module, try the next one*/
476 /*Check if this is the symbol we are looking for.*/
477 if (strncmp(SymName, FLine, NCPS)==0)
479 /*The symbol is in this module.*/
481 /*As in the original library format, it is assumed that the .rel
482 files reside in the same directory as the lib files.*/
483 strcat(DirLib, ModName);
484 sprintf(&DirLib[strlen(DirLib)], "%crel", FSEPX);
486 /*If this module has been loaded already don't load it again.*/
490 if(EQ(DirLib, lbf->filspc)) return 1;/*Already loaded*/
494 /*Add the embedded file to the list of files to be loaded in
495 the second pass. That is performed latter by the function
497 lbfh = (struct lbfile *) new (sizeof(struct lbfile));
510 lbfh->libspc = PathLib;
511 lbfh->filspc = DirLib;
512 lbfh->relfil = (char *) new (strlen(ModName) + 1);
513 strcpy(lbfh->relfil, ModName);
514 /*Library embedded file, so lbfh->offset must be >=0*/
515 lbfh->offset = IndexOffset+FileOffset;
517 /*Jump to where the .rel begins and load it.*/
518 fseek(libfp, lbfh->offset, SEEK_SET);
519 LoadRel(libfp, ModName);
521 /* if cdb information required & .adb file present */
525 SaveLinkedFilePath(DirLib);
527 return 1; /*Found the symbol, so success!*/
533 return 0; /*It should never reach this point, but just in case...*/
538 return 0; /*The symbol is not in this library*/
541 /*)Function VOID fndsym(name)
543 * char *name symbol name to find
545 * The function fndsym() searches through all combinations of the
546 * library path specifications (input by the -k option) and the
547 * library file specifications (input by the -l option) that
548 * lead to an existing file.
550 * The file specicifation may be formed in one of two ways:
552 * (1) If the library file contained an absolute
553 * path/file specification then this becomes filspc.
556 * (2) If the library file contains a relative path/file
557 * specification then the concatenation of the path
558 * and this file specification becomes filspc.
561 * The structure lbfile is created for the first library
562 * object file which contains the definition for the
563 * specified undefined symbol.
565 * If the library file [.LIB] contains file specifications for
566 * non existant files, no errors are returned.
569 * char buf[] [.REL] file input line
570 * char c [.REL] file input character
571 * FILE *fp file handle for object file
572 * lbfile *lbf temporary pointer
573 * lbfile *lbfh pointer to lbfile structure
574 * FILE *libfp file handle for library file
575 * lbname *lbnh pointer to lbname structure
576 * char *path file specification path
577 * char relfil[] [.REL] file specification
578 * char *str combined path and file specification
579 * char symname[] [.REL] file symbol string
582 * lbname *lbnhead The pointer to the first
584 * lbfile *lbfhead The pointer to the first
588 * int fclose() c_library
589 * int fgets() c_library
590 * FILE *fopen() c_library
591 * VOID free() c_library
592 * char getnb() lklex.c
593 * VOID lkexit() lkmain.c
594 * VOID loadfile() lklibr.c
595 * VOID * new() lksym.c
596 * char * sprintf() c_library
597 * int sscanf() c_library
598 * char * strcat() c_library
599 * char * strchr() c_library
600 * char * strcpy() c_library
601 * int strlen() c_library
602 * int strncmp() c_library
603 * VOID unget() lklex.c
606 * If the symbol is found then a new lbfile structure
607 * is created and added to the linked list of lbfile
608 * structures. The file containing the found symbol
618 struct lbfile *lbfh, *lbf;
619 char relfil[NINPUT+2];
621 char symname[NINPUT];
627 * Search through every library in the linked list "lbnhead".
630 for (lbnh=lbnhead; lbnh; lbnh=lbnh->next)
632 if ((libfp = fopen(lbnh->libspc, "r")) == NULL)
634 fprintf(stderr, "Cannot open library file %s\n",
641 * Read in a line from the library file.
642 * This is the relative file specification
643 * for a .REL file in this library.
646 while (fgets(relfil, NINPUT, libfp) != NULL)
648 relfil[NINPUT+1] = '\0';
652 str = (char *) new (strlen(path)+strlen(relfil)+6);
655 if (str[strlen(str)-1] != '/')
663 str = (char *) new (strlen(relfil) + 5);
666 if(strcmp(relfil, "<SDCCLIB>")==0)
668 result=SdccLib(lbnh->libspc, libfp, str, name);
670 if(result) return(1); /*Found the symbol*/
672 /*The symbol is not in the current library,
673 check the next library in the list*/
677 /*From here down is the support for libraries in the original format*/
678 if (relfil[0] == '\\')
680 strcat(str,relfil+1);
687 if(strchr(relfil, FSEPX) == NULL)
689 sprintf(&str[strlen(str)], "%crel", FSEPX);
692 if ((fp = fopen(str, "r")) != NULL)
696 * Read in the object file. Look for lines that
697 * begin with "S" and end with "D". These are
698 * symbol table definitions. If we find one, see
699 * if it is our symbol. Make sure we only read in
700 * our object file and don't go into the next one.
703 while (fgets(buf, NINPUT, fp) != NULL)
705 buf[NINPUT+1] = '\0';
708 * Skip everything that's not a symbol record.
710 if (buf[0] != 'S') continue;
713 * When a 'T line' is found terminate file scan.
714 * All 'S line's preceed 'T line's in .REL files.
716 if (buf[0] == 'T') break;
718 sscanf(buf, "S %s %c", symname, &c);
721 * If we find a symbol definition for the
722 * symbol we're looking for, load in the
723 * file and add it to lbfhead so it gets
724 * loaded on pass number 2.
726 if (strncmp(symname, name, NCPS) == 0 && c == 'D')
728 lbfh = (struct lbfile *) new (sizeof(struct lbfile));
741 lbfh->libspc = lbnh->libspc;
743 lbfh->relfil = (char *) new (strlen(relfil) + 1);
744 lbfh->offset = -1; /*Stand alone rel file*/
745 strcpy(lbfh->relfil,relfil);
749 /* if cdb information required & adb file present */
752 FILE *xfp = afile(str,"adb",0); //JCF: Nov 30, 2002
755 SaveLinkedFilePath(str);
773 void loadfile_SdccLib(char * libspc, char * module, long offset)
777 if ((fp = fopen(libspc,"r")) != NULL)
779 fseek(fp, offset, SEEK_SET);
785 /*)Function VOID library()
787 * The function library() links all the library object files
788 * contained in the lbfile structures.
791 * lbfile *lbfh pointer to lbfile structure
794 * lbfile *lbfhead pointer to first lbfile structure
797 * VOID loadfile lklibr.c
800 * Links all files contained in the lbfile structures.
808 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next)
812 /*Stand alone rel file (original lib format)*/
813 loadfile(lbfh->filspc);
817 /*rel file embedded in lib (new lib format)*/
818 loadfile_SdccLib(lbfh->libspc, lbfh->relfil, lbfh->offset);
823 /*)Function VOID loadfile(filspc)
825 * char *filspc library object file specification
827 * The function loadfile() links the library object module.
830 * FILE *fp file handle
831 * int i input line length
832 * char str[] file input line
835 * char *ip pointer to linker input string
838 * int fclose() c_library
839 * int fgets() c_library
840 * FILE * fopen() c_library
841 * VOID link_main() lkmain.c
842 * int strlen() c_library
845 * If file exists it is linked.
855 if ((fp = fopen(filspc,"r")) != NULL) {
856 while (fgets(str, NINPUT, fp) != NULL) {
857 str[NINPUT+1] = '\0';