* as/link/lklib.c, as/link/lksdcclib.c, as/link/lklibr.h,
[fw/sdcc] / as / link / lklib.c
1 /* lklib.c */
2
3 /*
4  * (C) Copyright 1989-1995
5  * All Rights Reserved
6  *
7  * Alan R. Baldwin
8  * 721 Berkeley St.
9  * Kent, Ohio  44240
10  *
11  * With contributions for the
12  * object libraries from
13  * Ken Hornstein
14  * kenh@cmf.nrl.navy.mil
15  *
16  */
17
18 /*
19  * Extensions: P. Felber
20  */
21
22 #include <string.h>
23
24 #include "getline.h"
25 #include "aslink.h"
26 #include "lklibr.h"
27 #include "lkrel.h"
28
29 static int
30 is_lib (FILE * libfp)
31 {
32   return 1;
33 }
34
35 #ifdef INDEXLIB
36 /* buildlibraryindex - build an in-memory cache of the symbols contained in
37  *                     the libraries
38  */
39 static pmlibraryfile
40 buildlibraryindex_lib (struct lbname *lbnh, FILE * libfp, pmlibraryfile This, int type)
41 {
42   char relfil[NINPUT];
43
44   while (getline (relfil, sizeof (relfil), libfp) != NULL)
45     {
46       FILE *fp;
47       char str[PATH_MAX];
48
49       if (lbnh->path != NULL)
50         {
51           strcpy (str, lbnh->path);
52 #ifdef  OTHERSYSTEM
53           if ((*str != '\0') && (str[strlen (str) - 1] != '/') && (str[strlen (str) - 1] != LKDIRSEP))
54             {
55               strcat (str, LKDIRSEPSTR);
56             }
57 #endif
58         }
59       else
60         str[0] = '\0';
61
62       if ((relfil[0] == '/') || (relfil[0] == LKDIRSEP))
63         {
64           strcat (str, relfil + 1);
65         }
66       else
67         {
68           strcat (str, relfil);
69         }
70
71       if (strchr (relfil, FSEPX) == NULL)
72         {
73           sprintf (&str[strlen (str)], "%c%s", FSEPX, LKOBJEXT);
74         }
75
76       if ((fp = fopen (str, "rb")) != NULL)
77         {
78           /* Opened OK - create a new libraryfile object for it */
79           if (libr == NULL)
80             {
81               libr = This = (pmlibraryfile) new (sizeof (mlibraryfile));
82             }
83           else
84             {
85               This->next = (pmlibraryfile) new (sizeof (mlibraryfile));
86               This = This->next;
87             }
88           This->next = NULL;
89           This->loaded = -1;
90           This->libspc = lbnh->libspc;
91           This->relfil = strdup (relfil);
92           This->filename = strdup (str);
93           This->type = type;
94
95           /* Start a new linked list of symbols for this module: */
96           This->symbols = NULL;
97
98           add_rel_index (fp, -1, This);
99           fclose (fp);
100         }                       /* Closes if object file opened OK */
101       else
102         {
103           fprintf (stderr, "?ASlink-Warning-Cannot open library module %s\n", str);
104         }
105     }                           /* Ends while - processing all in libr */
106
107   return This;
108 }
109
110 #else
111
112 static int
113 fndsym_lib (const char *name, struct lbname *lbnh, FILE * libfp, int type)
114 {
115   char relfil[NINPUT];
116
117   while (getline (relfil, sizeof (relfil), libfp) != NULL)
118     {
119       char str[PATH_MAX];
120       FILE *fp;
121
122       if (lbnh->path != NULL)
123         {
124           strcpy (str, lbnh->path);
125 #ifdef  OTHERSYSTEM
126           if ((*str != '\0') && (str[strlen (str) - 1] != '/') && (str[strlen (str) - 1] != LKDIRSEP))
127             {
128               strcat (str, LKDIRSEPSTR);
129             }
130 #endif
131         }
132       else
133         str[0] = '\0';
134
135       if ((relfil[0] == '/') || (relfil[0] == LKDIRSEP))
136         {
137           strcat (str, relfil + 1);
138         }
139       else
140         {
141           strcat (str, relfil);
142         }
143
144       if (strchr (relfil, FSEPX) == NULL)
145         {
146           sprintf (&str[strlen (str)], "%c%s", FSEPX, LKOBJEXT);
147         }
148
149       if ((fp = fopen (str, "rb")) != NULL)
150         {
151           /* Opened OK - create a new libraryfile object for it */
152           int ret = add_rel_file (name, lbnh, relfil, str, -1, fp, -1, type);
153           fclose (fp);
154           if (ret)
155             {
156               /* if cdb information required & adb file present */
157               if (dflag && dfp)
158                 {
159                   FILE *xfp = afile (str, "adb", 0);    //JCF: Nov 30, 2002
160                   if (xfp)
161                     {
162                       SaveLinkedFilePath (str);
163                       copyfile (dfp, xfp);
164                       fclose (xfp);
165                     }
166                 }
167               return 1;         /* Found the symbol, so success! */
168             }
169         }                       /* Closes if object file opened OK */
170       else
171         {
172           fprintf (stderr, "?ASlink-Warning-Cannot open library module %s\n", str);
173         }
174     }                           /* Ends while - processing all in libr */
175
176   return 0;                     /* The symbol is not in this library */
177 }
178 #endif
179
180 /*)Function VOID    loadfile_lib(filspc)
181  *
182  *      char    *filspc     library object file specification
183  *
184  *  The function loadfile() links the library object module.
185  *
186  *  local variables:
187  *      FILE    *fp         file handle
188  *      int     i           input line length
189  *      char    str[]       file input line
190  *
191  *  global variables:
192  *      char    *ip         pointer to linker input string
193  *
194  *   functions called:
195  *      int     fclose()    c_library
196  *      char    *getline()  getline.c
197  *      FILE *  fopen()     c_library
198  *      VOID    link_main() lkmain.c
199  *      int     strlen()    c_library
200  *
201  *  side effects:
202  *      If file exists it is linked.
203  */
204
205 static VOID
206 loadfile_lib (struct lbfile *lbfh)
207 {
208   FILE *fp;
209 #ifdef __CYGWIN__
210   char posix_path[PATH_MAX];
211   void cygwin_conv_to_full_posix_path (char *win_path, char *posix_path);
212   cygwin_conv_to_full_posix_path (lbfh->filspc, posix_path);
213   fp = fopen (posix_path, "rb");
214 #else
215   fp = fopen (lbfh->filspc, "rb");
216 #endif
217
218   if (fp != NULL)
219     {
220       load_rel (fp, -1);
221       fclose (fp);
222     }
223   else
224     {
225       fprintf (stderr, "?ASlink-Error-Opening library '%s'\n", lbfh->filspc);
226       fclose (fp);
227       lkexit (1);
228     }
229 }
230
231 struct aslib_target aslib_target_lib = {
232   &is_lib,
233 #ifdef INDEXLIB
234   &buildlibraryindex_lib,
235 #else
236   &fndsym_lib,
237 #endif
238   &loadfile_lib,
239 };