struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / sdas / linksrc / lklib.c
blob71bb4bae0f96a1d849590a418e0d7275b8671b4b
1 /* lklib.c
3 Copyright (C) 1989-1995 Alan R. Baldwin
4 721 Berkeley St., Kent, Ohio 44240
5 Copyright (C) 2008-2009 Borut Razem, borut dot razem at siol dot net
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 * With contributions for the
22 * object libraries from
23 * Ken Hornstein
24 * kenh@cmf.nrl.navy.mil
29 * Extensions: P. Felber
32 #include <string.h>
34 #include "sdld.h"
35 #include "lk_readnl.h"
36 #include "aslink.h"
37 #include "lklibr.h"
38 #include "lkrel.h"
40 static int
41 is_lib (FILE * libfp)
43 return 1;
46 #ifdef INDEXLIB
47 /* buildlibraryindex - build an in-memory cache of the symbols contained in
48 * the libraries
50 static pmlibraryfile
51 buildlibraryindex_lib (struct lbname *lbnh, FILE * libfp, pmlibraryfile This, int type)
53 char relfil[NINPUT];
55 while (lk_readnl (relfil, sizeof (relfil), libfp) != NULL)
57 FILE *fp;
58 char str[PATH_MAX];
60 if (lbnh->path != NULL)
62 strcpy (str, lbnh->path);
63 #ifdef OTHERSYSTEM
64 if ((*str != '\0') && (str[strlen (str) - 1] != '/') && (str[strlen (str) - 1] != LKDIRSEP))
66 strcat (str, LKDIRSEPSTR);
68 #endif
70 else
71 str[0] = '\0';
73 if ((relfil[0] == '/') || (relfil[0] == LKDIRSEP))
75 strcat (str, relfil + 1);
77 else
79 strcat (str, relfil);
82 if (strchr (relfil, FSEPX) == NULL)
84 sprintf (&str[strlen (str)], "%c%s", FSEPX, LKOBJEXT);
87 if ((fp = fopen (str, "rb")) != NULL)
89 /* Opened OK - create a new libraryfile object for it */
90 if (libr == NULL)
92 libr = This = (pmlibraryfile) new (sizeof (mlibraryfile));
94 else
96 This->next = (pmlibraryfile) new (sizeof (mlibraryfile));
97 This = This->next;
99 This->next = NULL;
100 This->loaded = -1;
101 This->libspc = lbnh->libspc;
102 This->relfil = strdup (relfil);
103 This->filspc = strdup (str);
104 This->type = type;
106 /* Start a new linked list of symbols for this module: */
107 This->symbols = NULL;
109 add_rel_index (fp, -1, This);
110 fclose (fp);
111 } /* Closes if object file opened OK */
112 else
114 fprintf (stderr, "?ASlink-Warning-Cannot open library module %s\n", str);
116 } /* Ends while - processing all in libr */
118 return This;
121 #else
123 static int
124 fndsym_lib (const char *name, struct lbname *lbnh, FILE * libfp, int type)
126 char relfil[NINPUT];
128 D ("Searching symbol: %s\n", name);
130 while (lk_readnl (relfil, sizeof (relfil), libfp) != NULL)
132 char str[PATH_MAX];
133 FILE *fp;
135 if (lbnh->path != NULL)
137 strcpy (str, lbnh->path);
138 #ifdef OTHERSYSTEM
139 if ((*str != '\0') && (str[strlen (str) - 1] != '/') && (str[strlen (str) - 1] != LKDIRSEP))
141 strcat (str, LKDIRSEPSTR);
143 #endif
145 else
146 str[0] = '\0';
148 if ((relfil[0] == '/') || (relfil[0] == LKDIRSEP))
150 strcat (str, relfil + 1);
152 else
154 strcat (str, relfil);
157 if (strchr (relfil, FSEPX) == NULL)
159 sprintf (&str[strlen (str)], "%c%s", FSEPX, LKOBJEXT);
162 if ((fp = fopen (str, "rb")) != NULL)
164 /* Opened OK - create a new libraryfile object for it */
165 int ret = add_rel_file (name, lbnh, relfil, str, -1, fp, -1, type);
166 fclose (fp);
167 if (ret)
169 D ("Loaded module %s from file %s.\n", str, str);
170 /* if cdb information required & adb file present */
171 if (yflag && yfp)
173 FILE *xfp = afile (str, "adb", 0); //JCF: Nov 30, 2002
174 if (xfp)
176 SaveLinkedFilePath (str);
177 copyfile (yfp, xfp);
178 fclose (xfp);
181 return 1; /* Found the symbol, so success! */
183 } /* Closes if object file opened OK */
184 else
186 fprintf (stderr, "?ASlink-Warning-Cannot open library module %s\n", str);
188 } /* Ends while - processing all in libr */
190 return 0; /* The symbol is not in this library */
192 #endif
194 /*)Function VOID loadfile_lib(filspc)
196 * char *filspc library object file specification
198 * The function loadfile() links the library object module.
200 * local variables:
201 * FILE *fp file handle
202 * int i input line length
203 * char str[] file input line
205 * global variables:
206 * char *ip pointer to linker input string
208 * functions called:
209 * int fclose() c_library
210 * char *lk_readnl() lk_readnl.c
211 * FILE * fopen() c_library
212 * VOID link_main() lkmain.c
213 * int strlen() c_library
215 * side effects:
216 * If file exists it is linked.
219 static VOID
220 loadfile_lib (struct lbfile *lbfh)
222 FILE *fp;
223 fp = fopen (lbfh->filspc, "rb");
224 if (fp != NULL)
226 load_rel (fp, -1);
227 fclose (fp);
229 else
231 fprintf (stderr, "?ASlink-Error-Opening library '%s'\n", lbfh->filspc);
232 fclose (fp);
233 lkexit (1);
237 struct aslib_target aslib_target_lib = {
238 &is_lib,
239 #ifdef INDEXLIB
240 &buildlibraryindex_lib,
241 #else
242 &fndsym_lib,
243 #endif
244 &loadfile_lib,