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 if (NULL != getline (str, sizeof (str), libfp) && EQ (str, ModName))
82 return EQ (str, "<REL>") ? load_rel (libfp, -1) : 0;
91 buildlibraryindex_sdcclib (struct lbname *lbnh, FILE * libfp, pmlibraryfile This, int type)
93 char ModName[NCPS] = "";
97 long IndexOffset = 0, FileOffset;
98 pmlibrarysymbol ThisSym = NULL;
100 while (getline (FLine, sizeof (FLine), libfp))
105 if (EQ (FLine, "<INDEX>"))
107 /*The next line has the size of the index */
108 getline (FLine, sizeof (FLine), libfp);
109 IndexOffset = atol (FLine);
115 if (EQ (FLine, "<MODULE>"))
117 /* The next line has the name of the module and the offset
118 of the corresponding embedded file in the library */
119 getline (FLine, sizeof (FLine), libfp);
120 sscanf (FLine, "%s %ld", ModName, &FileOffset);
123 /* Create a new libraryfile object for this module */
126 libr = This = (pmlibraryfile) new (sizeof (mlibraryfile));
130 This->next = (pmlibraryfile) new (sizeof (mlibraryfile));
135 This->offset = FileOffset + IndexOffset;
136 This->libspc = lbnh->libspc;
137 This->relfil = strdup (ModName);
138 sprintf (buff, "%s%s%c%s", lbnh->path, ModName, FSEPX, LKOBJEXT);
139 This->filename = strdup (buff);
142 This->symbols = ThisSym = NULL; /* Start a new linked list of symbols */
144 else if (EQ (FLine, "</INDEX>"))
146 return This; /* Finish, get out of here */
151 if (EQ (FLine, "</MODULE>"))
154 /* Create the index for the next module */
159 /* Add the symbols */
160 if (ThisSym == NULL) /* First symbol of the current module */
162 ThisSym = This->symbols = (pmlibrarysymbol) new (sizeof (mlibrarysymbol));
166 ThisSym->next = (pmlibrarysymbol) new (sizeof (mlibrarysymbol));
167 ThisSym = ThisSym->next;
169 ThisSym->next = NULL;
170 ThisSym->name = strdup (FLine);
175 return This; /* State machine should never reach this point, but just in case... */
180 return This; /* State machine should never reach this point, but just in case... */
185 /* Load an .adb file embedded in a sdcclib file. If there is
186 something between <ADB> and </ADB> returns 1, otherwise returns 0.
187 This way the aomf51 will not have useless empty modules. */
190 LoadAdb (FILE * libfp)
196 while (getline (str, sizeof (str), libfp) != NULL)
201 if (EQ (str, "<ADB>"))
206 if (EQ (str, "</ADB>"))
208 fprintf (dfp, "%s\n", str);
216 /* Check for a symbol in a SDCC library. If found, add the embedded .rel and
217 .adb files from the library. The library must be created with the SDCC
218 librarian 'sdcclib' since the linking process depends on the correct file offsets
219 embedded in the library file. */
222 findsym_sdcclib (const char *name, struct lbname *lbnh, FILE * libfp, int type)
224 struct lbfile *lbfh, *lbf;
225 char ModName[NCPS] = "";
228 long IndexOffset = 0, FileOffset;
230 while (getline (FLine, sizeof (FLine), libfp))
234 if (lbnh->path != NULL)
236 strcpy (str, lbnh->path);
238 if (*str != '\0' && (str[strlen (str) - 1] != '/') && (str[strlen (str) - 1] != LKDIRSEP))
240 strcat (str, LKDIRSEPSTR);
248 if (EQ (FLine, "<INDEX>"))
250 /* The next line has the size of the index */
251 getline (FLine, sizeof (FLine), libfp);
252 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. */
274 if (EQ (FLine, "</MODULE>"))
276 /* The symbol is not in this module, try the next one */
281 /* Check if this is the symbol we are looking for. */
282 if (strncmp (name, FLine, NCPS) == 0)
284 /* The symbol is in this module. */
286 /* As in the original library format, it is assumed that the .rel
287 files reside in the same directory as the lib files. */
288 sprintf (&str[strlen (str)], "%s%c%s", ModName, FSEPX, LKOBJEXT);
290 /* If this module has been loaded already don't load it again. */
294 if (EQ (str, lbf->filspc))
295 return 1; /* Already loaded */
299 /* Add the embedded file to the list of files to be loaded in
300 the second pass. That is performed latter by the function
302 lbfh = (struct lbfile *) new (sizeof (struct lbfile));
317 lbfh->libspc = lbnh->libspc;
318 lbfh->filspc = strdup (str);
319 lbfh->relfil = strdup (ModName);
320 /* Library embedded file, so lbfh->offset must be >=0 */
321 lbfh->offset = IndexOffset + FileOffset;
323 /* Jump to where the .rel begins and load it. */
324 fseek (libfp, lbfh->offset, SEEK_SET);
325 if (!LoadRel (lbnh->libspc, libfp, ModName))
328 fprintf (stderr, "?ASlink-Error-Bad offset in library file %s(%s)\n", lbfh->libspc, ModName);
331 /* if cdb information required & .adb file present */
335 SaveLinkedFilePath (str);
337 return 1; /* Found the symbol, so success! */
343 return 0; /* It should never reach this point, but just in case... */
348 return 0; /* The symbol is not in this library */
354 loadfile_sdcclib (struct lbfile *lbfh)
360 char posix_path[PATH_MAX];
361 void cygwin_conv_to_full_posix_path (char *win_path, char *posix_path);
362 cygwin_conv_to_full_posix_path (lbfh->libspc, posix_path);
363 fp = fopen (posix_path, "rb");
365 fp = fopen (lbfh->libspc, "rb");
370 fseek (fp, lbfh->offset, SEEK_SET);
371 res = LoadRel (lbfh->libspc, fp, lbfh->relfil);
376 fprintf (stderr, "?ASlink-Error-Bad offset in library file %s(%s)\n", lbfh->libspc, lbfh->relfil);
382 fprintf (stderr, "?ASlink-Error-Opening library '%s'\n", lbfh->libspc);
387 struct aslib_target aslib_target_sdcclib = {
390 &buildlibraryindex_sdcclib,