summaryrefslogtreecommitdiff
path: root/load.c
blob: 076dd30e0e6e9c50f64806393df6ea1685c9548e (plain)
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
/* Loading dynamic objects for GNU Make.
Copyright (C) 2012 Free Software Foundation, Inc.
This file is part of GNU Make.

GNU Make 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 of the License, or (at your option) any later
version.

GNU Make 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/>.  */

#include "make.h"

#if MAKE_LOAD

#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <errno.h>

#define SYMBOL_EXTENSION        "_gmake_setup"

static void *global_dl = NULL;

#include "debug.h"
#include "filedef.h"
#include "variable.h"

static int
init_symbol (const struct floc *flocp, const char *ldname, load_func_t symp)
{
  int r;
  const char *p;
  int nmlen = strlen (ldname);
  char *loaded = allocated_variable_expand("$(.LOADED)");

  /* If it's already been loaded don't do it again.  */
  p = strstr (loaded, ldname);
  r = p && (p==loaded || p[-1]==' ') && (p[nmlen]=='\0' || p[nmlen]==' ');
  free (loaded);
  if (r)
    return 1;

  /* Now invoke the symbol.  */
  r = (*symp) (flocp);

  /* If it succeeded, add the symbol to the loaded variable.  */
  if (r > 0)
    do_variable_definition (flocp, ".LOADED", ldname, o_default, f_append, 0);

  return r;
}

int
load_file (const struct floc *flocp, const char *ldname, int noerror)
{
  load_func_t symp;
  const char *fp;
  char *symname = NULL;
  char *new = alloca (strlen (ldname) + CSTRLEN (SYMBOL_EXTENSION) + 1);

  if (! global_dl)
    {
      global_dl = dlopen (NULL, RTLD_NOW|RTLD_GLOBAL);
      if (! global_dl)
        fatal (flocp, _("Failed to open global symbol table: %s"), dlerror());
    }

  /* If a symbol name was provided, use it.  */
  fp = strchr (ldname, '(');
  if (fp)
    {
      const char *ep;

      /* If there's an open paren, see if there's a close paren: if so use
         that as the symbol name.  We can't have whitespace: it would have
         been chopped up before this function is called.  */
      ep = strchr (fp+1, ')');
      if (ep && ep[1] == '\0')
        {
          int l = fp - ldname;;

          ++fp;
          if (fp == ep)
            fatal (flocp, _("Empty symbol name for load: %s"), ldname);

          /* Make a copy of the ldname part.  */
          memcpy (new, ldname, l);
          new[l] = '\0';
          ldname = new;

          /* Make a copy of the symbol name part.  */
          symname = new + l + 1;
          memcpy (symname, fp, ep - fp);
          symname[ep - fp] = '\0';
        }
    }

  /* If we didn't find a symbol name yet, construct it from the ldname.  */
  if (! symname)
    {
      char *p = new;

      fp = strrchr (ldname, '/');
      if (!fp)
        fp = ldname;
      else
        ++fp;
      while (isalnum (*fp) || *fp == '_')
        *(p++) = *(fp++);
      strcpy (p, SYMBOL_EXTENSION);
      symname = new;
    }

  DB (DB_VERBOSE, (_("Loading symbol %s from %s\n"), symname, ldname));

  /* See if it's already defined.  */
  symp = (load_func_t) dlsym (global_dl, symname);
  if (! symp) {
    void *dlp = dlopen (ldname, RTLD_LAZY|RTLD_GLOBAL);
    if (! dlp)
      {
        if (noerror)
          DB (DB_BASIC, ("%s", dlerror()));
        else
          error (flocp, "%s", dlerror());
        return 0;
      }

    symp = dlsym (dlp, symname);
    if (! symp)
      fatal (flocp, _("Failed to load symbol %s from %s: %s"),
             symname, ldname, dlerror());
  }

  /* Invoke the symbol to initialize the loaded object.  */
  return init_symbol(flocp, ldname, symp);
}

#else

int
load_file (const struct floc *flocp, const char *ldname, int noerror)
{
  if (! noerror)
    fatal (flocp, _("The 'load' operation is not supported on this platform."));

  return 0;
}

#endif  /* MAKE_LOAD */