summaryrefslogtreecommitdiff
path: root/sdas/linksrc/lksdcclib.c
blob: b0ea35c4abdec3b5ecae92135f95d5b0f337561a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* lksdcclib.c - sdcc library format handling

   Copyright (C) 1989-1995 Alan R. Baldwin
   721 Berkeley St., Kent, Ohio 44240
   Copyright (C) 2008 Borut Razem, borut dot razem at siol dot net

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>. */

/*
 * With contributions for the
 * object libraries from
 * Ken Hornstein
 * kenh@cmf.nrl.navy.mil
 *
 */

/*
 * Extensions: P. Felber
 */

#include <stdlib.h>
#include <string.h>

#include "sdld.h"
#include "lk_readnl.h"
#include "aslink.h"
#include "lklibr.h"
#include "lkrel.h"

#define EQ(A,B) !strcmp((A),(B))
#define MAXLINE 254             /* when using lk_readnl */


static int
is_sdcclib (FILE * libfp)
{
#define SDCCLIB_MAGIC "<SDCCLIB>"
#define SDCCLIB_MAGIC_LEN (sizeof ("<SDCCLIB>") - 1)

  char buf[SDCCLIB_MAGIC_LEN];

  if (fread (buf, 1, sizeof (buf), libfp) == sizeof (buf) && memcmp (buf, SDCCLIB_MAGIC, SDCCLIB_MAGIC_LEN) == 0)
    {
      switch (getc (libfp))
        {
        case '\r':
          if (getc (libfp) == '\n')
            return 1;

        case '\n':
          return 1;
        }
    }
  rewind (libfp);
  return 0;
}

/* Load a .rel file embedded in a sdcclib file */
static int
LoadRel (char *libfname, FILE * libfp, char *ModName)
{
  char str[NINPUT];
  int state = 0;

  while (lk_readnl (str, sizeof (str), libfp) != NULL)
    {
      switch (state)
        {
        case 0:
          if (EQ (str, "<FILE>"))
            {
              if (NULL != lk_readnl (str, sizeof (str), libfp) && EQ (str, ModName))
                state = 1;
              else
                return 0;
            }
          else
            return 0;
          break;

        case 1:
          return EQ (str, "<REL>") ? load_rel (libfp, -1) : 0;
        }
    }

  return 0;
}

#ifdef INDEXLIB
static pmlibraryfile
buildlibraryindex_sdcclib (struct lbname *lbnh, FILE * libfp, pmlibraryfile This, int type)
{
  char FLine[MAXLINE];
  int state = 0;
  long IndexOffset = 0;
  pmlibrarysymbol ThisSym = NULL;

  while (lk_readnl (FLine, sizeof (FLine), libfp))
    {
      switch (state)
        {
        case 0:
          if (EQ (FLine, "<INDEX>"))
            {
              /*The next line has the size of the index */
              lk_readnl (FLine, sizeof (FLine), libfp);
              IndexOffset = atol (FLine);
              state = 1;
            }
          break;

        case 1:
          if (EQ (FLine, "<MODULE>"))
            {
              char buff[PATH_MAX];
              char ModName[NCPS] = "";
              long FileOffset;

              /* The next line has the name of the module and the offset
                 of the corresponding embedded file in the library */
              lk_readnl (FLine, sizeof (FLine), libfp);
              sscanf (FLine, "%s %ld", ModName, &FileOffset);
              state = 2;

              /* Create a new libraryfile object for this module */
              if (libr == NULL)
                {
                  libr = This = (pmlibraryfile) new (sizeof (mlibraryfile));
                }
              else
                {
                  This->next = (pmlibraryfile) new (sizeof (mlibraryfile));
                  This = This->next;
                }
              This->next = NULL;
              This->loaded = -1;
              This->offset = FileOffset + IndexOffset;
              This->libspc = lbnh->libspc;
              This->relfil = strdup (ModName);
              sprintf (buff, "%s%s%c%s", lbnh->path, ModName, FSEPX, LKOBJEXT);
              This->filspc = strdup (buff);
              This->type = type;

              This->symbols = ThisSym = NULL;   /* Start a new linked list of symbols */
            }
          else if (EQ (FLine, "</INDEX>"))
            {
              return This;      /* Finish, get out of here */
            }
          break;

        case 2:
          if (EQ (FLine, "</MODULE>"))
            {
              This->loaded = 0;
              /* Create the index for the next module */
              state = 1;
            }
          else
            {
              /* Add the symbols */
              if (ThisSym == NULL)      /* First symbol of the current module */
                {
                  ThisSym = This->symbols = (pmlibrarysymbol) new (sizeof (mlibrarysymbol));
                }
              else
                {
                  ThisSym->next = (pmlibrarysymbol) new (sizeof (mlibrarysymbol));
                  ThisSym = ThisSym->next;
                }
              ThisSym->next = NULL;
              ThisSym->name = strdup (FLine);
            }
          break;

        default:
          return This;          /* State machine should never reach this point, but just in case... */
          break;
        }
    }

  return This;                  /* State machine should never reach this point, but just in case... */
}

#else

/* Load an .adb file embedded in a sdcclib file. If there is
something between <ADB> and </ADB> returns 1, otherwise returns 0.
This way the aomf51 will not have useless empty modules. */

static int
LoadAdb (FILE * libfp)
{
  char str[MAXLINE];
  int state = 0;
  int ret = 0;

  while (lk_readnl (str, sizeof (str), libfp) != NULL)
    {
      switch (state)
        {
        case 0:
          if (EQ (str, "<ADB>"))
            state = 1;
          break;

        case 1:
          if (EQ (str, "</ADB>"))
            return ret;
          fprintf (yfp, "%s\n", str);
          ret = 1;
          break;
        }
    }
  return ret;
}

/* Check for a symbol in a SDCC library. If found, add the embedded .rel and
   .adb files from the library.  The library must be created with the SDCC
   librarian 'sdcclib' since the linking process depends on the correct file offsets
   embedded in the library file. */

static int
findsym_sdcclib (const char *name, struct lbname *lbnh, FILE * libfp, int type)
{
  struct lbfile *lbfh;
  char ModName[NCPS] = "";
  char FLine[MAXLINE];
  int state = 0;
  long IndexOffset = 0, FileOffset;

  while (lk_readnl (FLine, sizeof (FLine), libfp))
    {
      char filspc[PATH_MAX];

      if (lbnh->path != NULL)
        {
          strcpy (filspc, lbnh->path);
#ifdef  OTHERSYSTEM
          if (*filspc != '\0' && (filspc[strlen (filspc) - 1] != '/') && (filspc[strlen (filspc) - 1] != LKDIRSEP))
            {
              strcat (filspc, LKDIRSEPSTR);
            }
#endif
        }

      switch (state)
        {
        case 0:
          if (EQ (FLine, "<INDEX>"))
            {
              /* The next line has the size of the index */
              lk_readnl (FLine, sizeof (FLine), libfp);
              IndexOffset = atol (FLine);
              state = 1;
            }
          break;

        case 1:
          if (EQ (FLine, "<MODULE>"))
            {
              /* The next line has the name of the module and the offset
                 of the corresponding embedded file in the library */
              lk_readnl (FLine, sizeof (FLine), libfp);
              sscanf (FLine, "%s %ld", ModName, &FileOffset);
              state = 2;
            }
          else if (EQ (FLine, "</INDEX>"))
            {
              /* Reached the end of the index.  The symbol is not in this library. */
              return 0;
            }
          break;

        case 2:
          if (EQ (FLine, "</MODULE>"))
            {
              /* The symbol is not in this module, try the next one */
              state = 1;
            }
          else
            {
              /* Check if this is the symbol we are looking for. */
              if (strncmp (name, FLine, NCPS) == 0)
                {
                  /* The symbol is in this module. */

                  /* As in the original library format, it is assumed that the .rel
                     files reside in the same directory as the lib files. */
                  sprintf (&filspc[strlen (filspc)], "%s%c%s", ModName, FSEPX, LKOBJEXT);

                  /* If this module has been loaded already don't load it again. */
                  if (is_module_loaded (filspc))
                    return 1;

                  /* Add the embedded file to the list of files to be loaded in
                     the second pass.  That is performed latter by the function
                     library() below. */
                  lbfh = (struct lbfile *) new (sizeof (struct lbfile));
                  if (lbfhead == NULL)
                    {
                      lbfhead = lbfh;
                    }
                  else
                    {
                      struct lbfile *lbf;

                      for (lbf = lbfhead; lbf->next; lbf = lbf->next)
                        ;

                      lbf->next = lbfh;
                    }

                  lbfh->libspc = lbnh->libspc;
                  lbfh->filspc = strdup (filspc);
                  lbfh->relfil = strdup (ModName);
                  lbfh->f_obj = lbnh->f_obj;
                  /* Library embedded file, so lbfh->offset must be >=0 */
                  lbfh->offset = IndexOffset + FileOffset;
                  obj_flag = lbfh->f_obj;

                  /* Jump to where the .rel begins and load it. */
                  fseek (libfp, lbfh->offset, SEEK_SET);
                  if (!LoadRel (lbnh->libspc, libfp, ModName))
                    {
                      fclose (libfp);
                      fprintf (stderr, "?ASlink-Error-Bad offset in library file %s(%s)\n", lbfh->libspc, ModName);
                      lkexit (1);
                    }
                  /* if cdb information required & .adb file present */
                  if (yflag && yfp)
                    {
                      if (LoadAdb (libfp))
                        SaveLinkedFilePath (filspc);
                    }
                  return 1;     /* Found the symbol, so success! */
                }
            }
          break;

        default:
          return 0;             /* It should never reach this point, but just in case... */
          break;
        }
    }

  return 0;                     /* The symbol is not in this library */
}

#endif

static void
loadfile_sdcclib (struct lbfile *lbfh)
{
  FILE *fp;
  int res;
  fp = fopen (lbfh->libspc, "rb");
  if (fp != NULL)
    {
      fseek (fp, lbfh->offset, SEEK_SET);
      res = LoadRel (lbfh->libspc, fp, lbfh->relfil);
      fclose (fp);

      if (!res)
        {
          fprintf (stderr, "?ASlink-Error-Bad offset in library file %s(%s)\n", lbfh->libspc, lbfh->relfil);
          lkexit (1);
        }
    }
  else
    {
      fprintf (stderr, "?ASlink-Error-Opening library '%s'\n", lbfh->libspc);
      lkexit (1);
    }
}

struct aslib_target aslib_target_sdcclib = {
  &is_sdcclib,
#ifdef INDEXLIB
  &buildlibraryindex_sdcclib,
#else
  &findsym_sdcclib,
#endif
  &loadfile_sdcclib,
};