summaryrefslogtreecommitdiff
path: root/sdas/linksrc/lklib.c
blob: 71bb4bae0f96a1d849590a418e0d7275b8671b4b (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
/* lklib.c

   Copyright (C) 1989-1995 Alan R. Baldwin
   721 Berkeley St., Kent, Ohio 44240
   Copyright (C) 2008-2009 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 <string.h>

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

static int
is_lib (FILE * libfp)
{
  return 1;
}

#ifdef INDEXLIB
/* buildlibraryindex - build an in-memory cache of the symbols contained in
 *                     the libraries
 */
static pmlibraryfile
buildlibraryindex_lib (struct lbname *lbnh, FILE * libfp, pmlibraryfile This, int type)
{
  char relfil[NINPUT];

  while (lk_readnl (relfil, sizeof (relfil), libfp) != NULL)
    {
      FILE *fp;
      char str[PATH_MAX];

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

      if ((relfil[0] == '/') || (relfil[0] == LKDIRSEP))
        {
          strcat (str, relfil + 1);
        }
      else
        {
          strcat (str, relfil);
        }

      if (strchr (relfil, FSEPX) == NULL)
        {
          sprintf (&str[strlen (str)], "%c%s", FSEPX, LKOBJEXT);
        }

      if ((fp = fopen (str, "rb")) != NULL)
        {
          /* Opened OK - create a new libraryfile object for it */
          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->libspc = lbnh->libspc;
          This->relfil = strdup (relfil);
          This->filspc = strdup (str);
          This->type = type;

          /* Start a new linked list of symbols for this module: */
          This->symbols = NULL;

          add_rel_index (fp, -1, This);
          fclose (fp);
        }                       /* Closes if object file opened OK */
      else
        {
          fprintf (stderr, "?ASlink-Warning-Cannot open library module %s\n", str);
        }
    }                           /* Ends while - processing all in libr */

  return This;
}

#else

static int
fndsym_lib (const char *name, struct lbname *lbnh, FILE * libfp, int type)
{
  char relfil[NINPUT];

  D ("Searching symbol: %s\n", name);

  while (lk_readnl (relfil, sizeof (relfil), libfp) != NULL)
    {
      char str[PATH_MAX];
      FILE *fp;

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

      if ((relfil[0] == '/') || (relfil[0] == LKDIRSEP))
        {
          strcat (str, relfil + 1);
        }
      else
        {
          strcat (str, relfil);
        }

      if (strchr (relfil, FSEPX) == NULL)
        {
          sprintf (&str[strlen (str)], "%c%s", FSEPX, LKOBJEXT);
        }

      if ((fp = fopen (str, "rb")) != NULL)
        {
          /* Opened OK - create a new libraryfile object for it */
          int ret = add_rel_file (name, lbnh, relfil, str, -1, fp, -1, type);
          fclose (fp);
          if (ret)
            {
              D ("Loaded module %s from file %s.\n", str, str);
              /* if cdb information required & adb file present */
              if (yflag && yfp)
                {
                  FILE *xfp = afile (str, "adb", 0);    //JCF: Nov 30, 2002
                  if (xfp)
                    {
                      SaveLinkedFilePath (str);
                      copyfile (yfp, xfp);
                      fclose (xfp);
                    }
                }
              return 1;         /* Found the symbol, so success! */
            }
        }                       /* Closes if object file opened OK */
      else
        {
          fprintf (stderr, "?ASlink-Warning-Cannot open library module %s\n", str);
        }
    }                           /* Ends while - processing all in libr */

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

/*)Function VOID    loadfile_lib(filspc)
 *
 *      char    *filspc     library object file specification
 *
 *  The function loadfile() links the library object module.
 *
 *  local variables:
 *      FILE    *fp         file handle
 *      int     i           input line length
 *      char    str[]       file input line
 *
 *  global variables:
 *      char    *ip         pointer to linker input string
 *
 *   functions called:
 *      int     fclose()    c_library
 *      char    *lk_readnl()  lk_readnl.c
 *      FILE *  fopen()     c_library
 *      VOID    link_main() lkmain.c
 *      int     strlen()    c_library
 *
 *  side effects:
 *      If file exists it is linked.
 */

static VOID
loadfile_lib (struct lbfile *lbfh)
{
  FILE *fp;
  fp = fopen (lbfh->filspc, "rb");
  if (fp != NULL)
    {
      load_rel (fp, -1);
      fclose (fp);
    }
  else
    {
      fprintf (stderr, "?ASlink-Error-Opening library '%s'\n", lbfh->filspc);
      fclose (fp);
      lkexit (1);
    }
}

struct aslib_target aslib_target_lib = {
  &is_lib,
#ifdef INDEXLIB
  &buildlibraryindex_lib,
#else
  &fndsym_lib,
#endif
  &loadfile_lib,
};