4 * (C) Copyright 1989-1995
11 * With contributions for the
12 * object libraries from
14 * kenh@cmf.nrl.navy.mil
19 * Extensions: P. Felber
30 #define EQ(A,B) !strcmp((A),(B))
31 #define MAXLINE 254 /*when using getline */
35 is_sdcclib (FILE * libfp)
37 #define SDCCLIB_MAGIC "<SDCCLIB>"
38 #define SDCCLIB_MAGIC_LEN (sizeof ("<SDCCLIB>") - 1)
40 char buf[SDCCLIB_MAGIC_LEN];
42 if (fread (buf, 1, sizeof (buf), libfp) == sizeof (buf) && memcmp (buf, SDCCLIB_MAGIC, SDCCLIB_MAGIC_LEN) == 0)
47 if (getc (libfp) == '\n')
58 /* Load a .rel file embedded in a sdcclib file */
60 LoadRel (char *libfname, FILE * libfp, char *ModName)
65 while (getline (str, sizeof (str), libfp) != NULL)
70 if (EQ (str, "<FILE>"))
72 getline (str, sizeof (str), libfp);
73 if (EQ (str, ModName))
77 fprintf (stderr, "?ASlink-Error-Bad offset in library file %s(%s)\n", libfname, ModName);
83 if (EQ (str, "<REL>"))
95 buildlibraryindex_sdcclib (struct lbname *lbnh, FILE * libfp, pmlibraryfile This, int type)
97 char ModName[NCPS] = "";
101 long IndexOffset = 0, FileOffset;
102 pmlibrarysymbol ThisSym = NULL;
104 while (getline (FLine, sizeof (FLine), libfp))
109 if (EQ (FLine, "<INDEX>"))
111 /*The next line has the size of the index */
112 getline (FLine, sizeof (FLine), libfp);
113 IndexOffset = atol (FLine);
118 if (EQ (FLine, "<MODULE>"))
120 /* The next line has the name of the module and the offset
121 of the corresponding embedded file in the library */
122 getline (FLine, sizeof (FLine), libfp);
123 sscanf (FLine, "%s %ld", ModName, &FileOffset);
126 /* Create a new libraryfile object for this module */
129 libr = This = (pmlibraryfile) new (sizeof (mlibraryfile));
133 This->next = (pmlibraryfile) new (sizeof (mlibraryfile));
138 This->offset = FileOffset + IndexOffset;
139 This->libspc = lbnh->libspc;
140 This->relfil = strdup (ModName);
141 sprintf (buff, "%s%s%c%s", lbnh->path, ModName, FSEPX, LKOBJEXT);
142 This->filename = strdup (buff);
145 This->symbols = ThisSym = NULL; /* Start a new linked list of symbols */
147 else if (EQ (FLine, "</INDEX>"))
149 return This; /* Finish, get out of here */
153 if (EQ (FLine, "</MODULE>"))
156 /* Create the index for the next module */
161 /* Add the symbols */
162 if (ThisSym == NULL) /* First symbol of the current module */
164 ThisSym = This->symbols = (pmlibrarysymbol) new (sizeof (mlibrarysymbol));
168 ThisSym->next = (pmlibrarysymbol) new (sizeof (mlibrarysymbol));
169 ThisSym = ThisSym->next;
171 ThisSym->next = NULL;
172 ThisSym->name = strdup (FLine);
177 return This; /* State machine should never reach this point, but just in case... */
182 return This; /* State machine should never reach this point, but just in case... */
187 /* Load an .adb file embedded in a sdcclib file. If there is
188 something between <ADB> and </ADB> returns 1, otherwise returns 0.
189 This way the aomf51 will not have useless empty modules. */
192 LoadAdb (FILE * libfp)
198 while (getline (str, sizeof (str), libfp) != NULL)
203 if (EQ (str, "<ADB>"))
207 if (EQ (str, "</ADB>"))
209 fprintf (dfp, "%s\n", str);
217 /* Check for a symbol in a SDCC library. If found, add the embedded .rel and
218 .adb files from the library. The library must be created with the SDCC
219 librarian 'sdcclib' since the linking process depends on the correct file offsets
220 embedded in the library file. */
223 findsym_sdcclib (const char *name, struct lbname *lbnh, FILE * libfp, int type)
225 struct lbfile *lbfh, *lbf;
226 char ModName[NCPS] = "";
229 long IndexOffset = 0, FileOffset;
231 while (getline (FLine, sizeof (FLine), libfp))
235 if (lbnh->path != NULL)
237 strcpy (str, lbnh->path);
239 if (*str != '\0' && (str[strlen (str) - 1] != '/') && (str[strlen (str) - 1] != LKDIRSEP))
241 strcat (str, LKDIRSEPSTR);
249 if (EQ (FLine, "<INDEX>"))
251 /* The next line has the size of the index */
252 getline (FLine, sizeof (FLine), libfp);
253 IndexOffset = atol (FLine);
258 if (EQ (FLine, "<MODULE>"))
260 /* The next line has the name of the module and the offset
261 of the corresponding embedded file in the library */
262 getline (FLine, sizeof (FLine), libfp);
263 sscanf (FLine, "%s %ld", ModName, &FileOffset);
266 else if (EQ (FLine, "</INDEX>"))
268 /* Reached the end of the index. The symbol is not in this library. */
273 if (EQ (FLine, "</MODULE>"))
275 /* The symbol is not in this module, try the next one */
280 /* Check if this is the symbol we are looking for. */
281 if (strncmp (name, FLine, NCPS) == 0)
283 /* The symbol is in this module. */
285 /* As in the original library format, it is assumed that the .rel
286 files reside in the same directory as the lib files. */
287 sprintf (&str[strlen (str)], "%s%c%s", ModName, FSEPX, LKOBJEXT);
289 /* If this module has been loaded already don't load it again. */
293 if (EQ (str, lbf->filspc))
294 return 1; /* Already loaded */
298 /* Add the embedded file to the list of files to be loaded in
299 the second pass. That is performed latter by the function
301 lbfh = (struct lbfile *) new (sizeof (struct lbfile));
316 lbfh->libspc = lbnh->libspc;
318 lbfh->relfil = strdup (ModName);
319 /* Library embedded file, so lbfh->offset must be >=0 */
320 lbfh->offset = IndexOffset + FileOffset;
322 /* Jump to where the .rel begins and load it. */
323 fseek (libfp, lbfh->offset, SEEK_SET);
324 LoadRel (lbnh->libspc, libfp, ModName);
326 /* if cdb information required & .adb file present */
330 SaveLinkedFilePath (str);
332 return 1; /* Found the symbol, so success! */
338 return 0; /* It should never reach this point, but just in case... */
343 return 0; /* The symbol is not in this library */
349 loadfile_sdcclib (struct lbfile *lbfh)
353 char posix_path[PATH_MAX];
354 void cygwin_conv_to_full_posix_path (char *win_path, char *posix_path);
355 cygwin_conv_to_full_posix_path (lbfh->libspc, posix_path);
356 fp = fopen (posix_path, "rb");
358 fp = fopen (lbfh->libspc, "rb");
363 fseek (fp, lbfh->offset, SEEK_SET);
364 LoadRel (lbfh->libspc, fp, lbfh->relfil);
369 fprintf (stderr, "?ASlink-Error-Opening library '%s'\n", lbfh->libspc);
375 struct aslib_target aslib_target_sdcclib = {
378 &buildlibraryindex_sdcclib,