source: git/libpolys/polys/mod_raw.cc @ ba5e9e

spielwiese
Last change on this file since ba5e9e was ba5e9e, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Changed configure-scripts to generate individual public config files for each package: resources, libpolys, singular (main) fix: sources should include correct corresponding config headers.
  • Property mode set to 100644
File size: 7.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5 * ABSTRACT: machine depend code for dynamic modules
6 *
7 * Provides: dynl_check_opened()
8 *           dynl_open()
9 *           dynl_sym()
10 *           dynl_error()
11 *           dynl_close()
12*/
13
14#include <stdio.h>
15#include <string.h>
16#include <ctype.h>
17#include <sys/stat.h>
18#include <errno.h>
19
20
21#ifdef HAVE_CONFIG_H
22#include "libpolysconfig.h"
23#endif /* HAVE_CONFIG_H */
24#include <misc/auxiliary.h>
25
26#include <omalloc/omalloc.h>
27
28#include <reporter/reporter.h>
29
30#include <resources/feResource.h>
31
32#include "mod_raw.h"
33
34#ifdef HAVE_STATIC
35#undef HAVE_DL
36#endif
37/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
38#if defined(HAVE_DL)
39
40/*****************************************************************************
41 *
42 * General section
43 * These are just wrappers around the repsective dynl_* calls
44 * which look for the binary in the bin_dir of Singular and ommit warnings if
45 * somethings goes wrong
46 *
47 *****************************************************************************/
48static BOOLEAN warn_handle = FALSE;
49static BOOLEAN warn_proc = FALSE;
50#ifndef DL_TAIL
51#ifdef NDEBUG
52#define DL_TAIL ".so"
53#else
54#define DL_TAIL ".so"
55//#define DL_TAIL "_g.so"
56#endif
57#endif
58
59void* dynl_open_binary_warn(const char* binary_name, const char* msg)
60{
61  void* handle = NULL;
62  char* binary_name_so=NULL;
63
64  // try %b/MOD first
65  {
66    const char* bin_dir = feGetResource('b');
67    const int binary_name_so_length = 6 + strlen(DL_TAIL)
68               + strlen(binary_name)
69               +strlen(DIR_SEPP)*2
70               +strlen(bin_dir);
71    binary_name_so = (char *)omAlloc0( binary_name_so_length * sizeof(char) );
72    snprintf(binary_name_so, binary_name_so_length, "%s%s%s%s%s%s", bin_dir, DIR_SEPP,"MOD",DIR_SEPP,binary_name, DL_TAIL);
73    handle = dynl_open(binary_name_so);
74
75  }
76
77  // try P_PROCS_DIR (%P)
78  if (handle == NULL)
79  {
80    const char* proc_dir = feGetResource('P');
81    if (proc_dir != NULL)
82    {
83      if (binary_name_so!=NULL) omFree(binary_name_so);
84      const int binary_name_so_length = 3 + strlen(DL_TAIL) + strlen(binary_name) + strlen(DIR_SEPP) + strlen(proc_dir);
85      binary_name_so = (char *)omAlloc0( binary_name_so_length * sizeof(char) );
86      snprintf(binary_name_so, binary_name_so_length, "%s%s%s%s", proc_dir, DIR_SEPP, binary_name, DL_TAIL);
87      handle = dynl_open(binary_name_so);
88    }
89  }
90
91  if (handle == NULL && ! warn_handle)
92  {
93      Warn("Could not find dynamic library: %s%s (tried %s)",
94              binary_name, DL_TAIL,binary_name_so);
95      Warn("Error message from system: %s", dynl_error());
96      if (msg != NULL) Warn("%s", msg);
97      Warn("See the INSTALL section in the Singular manual for details.");
98      warn_handle = TRUE;
99  }
100  omfree((ADDRESS)binary_name_so );
101
102  return  handle;
103}
104
105void* dynl_sym_warn(void* handle, const char* proc, const char* msg)
106{
107  void *proc_ptr = NULL;
108  if (handle != NULL)
109  {
110    proc_ptr = dynl_sym(handle, proc);
111    if (proc_ptr == NULL && ! warn_proc)
112    {
113      Warn("Could load a procedure from a dynamic library");
114      Warn("Error message from system: %s", dynl_error());
115      if (msg != NULL) Warn("%s", msg);
116      Warn("See the INSTALL section in the Singular manual for details.");
117      warn_proc = TRUE;
118    }
119  }
120  return proc_ptr;
121}
122
123#ifdef __cplusplus
124extern "C" {
125#endif
126
127/*****************************************************************************
128 * SECTION generic ELF: ix86-linux / alpha-linux / IA64-linux /x86_64_Linux  *
129 *                      SunOS-5 / IRIX-6 / ppcMac-Darwin / FreeeBSD          *
130 *****************************************************************************/
131// relying on gcc to define __ELF__, check with cpp -dM /dev/null
132// Mac OsX is an ELF system, but does not define __ELF__
133// Solaris is an ELF system, but does not define __ELF__
134#if defined(__ELF__)
135#define HAVE_ELF_SYSTEM
136#endif
137
138#if defined(ppcMac_darwin)
139#define HAVE_ELF_SYSTEM
140#endif
141
142#if defined(ix86Mac_darwin)
143#define HAVE_ELF_SYSTEM
144#endif
145
146#if defined(x86_64Mac_darwin)
147#define HAVE_ELF_SYSTEM
148#endif
149
150#if (defined(__APPLE__) && defined(__MACH__)) && (!defined(HAVE_ELF_SYSTEM))
151#define HAVE_ELF_SYSTEM
152#endif
153
154#if defined(SunOS_5)
155#define HAVE_ELF_SYSTEM
156#endif
157
158#if defined(HAVE_ELF_SYSTEM)
159#include <dlfcn.h>
160#define DL_IMPLEMENTED
161
162static void* kernel_handle = NULL;
163int dynl_check_opened(
164  char *filename    /* I: filename to check */
165  )
166{
167  return dlopen(filename,RTLD_NOW|RTLD_NOLOAD) != NULL;
168}
169
170void *dynl_open(
171  char *filename    /* I: filename to load */
172  )
173{
174// glibc 2.2:
175  if ((filename==NULL) || (dlopen(filename,RTLD_NOW|RTLD_NOLOAD)==NULL))
176    return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL));
177  else
178    Werror("module %s already loaded",filename);
179  return NULL;
180// alternative
181//    return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL));
182}
183
184void *dynl_sym(void *handle, const char *symbol)
185{
186  if (handle == DYNL_KERNEL_HANDLE)
187  {
188    if (kernel_handle == NULL)
189      kernel_handle = dynl_open(NULL);
190    handle = kernel_handle;
191  }
192  return(dlsym(handle, symbol));
193}
194
195int dynl_close (void *handle)
196{
197  return(dlclose (handle));
198}
199
200const char *dynl_error()
201{
202  return(dlerror());
203}
204#endif /* ELF_SYSTEM */
205
206/*****************************************************************************
207 * SECTION HPUX-9/10                                                         *
208 *****************************************************************************/
209#if defined(HPUX_9) || defined(HPUX_10)
210#define DL_IMPLEMENTED
211#include <dl.h>
212
213typedef char *((*func_ptr) ());
214
215int dynl_check_opened(    /* NOTE: untested */
216  char *filename    /* I: filename to check */
217  )
218{
219  struct shl_descriptor *desc;
220  for (int idx = 0; shl_get(idx, &desc) != -1; ++idx)
221  {
222    if (strcmp(filename, desc->filename) == 0) return TRUE;
223  }
224  return FALSE;
225}
226
227void *dynl_open(char *filename)
228{
229  shl_t           handle = shl_load(filename, BIND_DEFERRED, 0);
230
231  return ((void *) handle);
232}
233
234void *dynl_sym(void *handle, const char *symbol)
235{
236  func_ptr        f;
237
238  if (handle == DYNL_KERNEL_HANDLE)
239    handle = PROG_HANDLE;
240
241  if (shl_findsym((shl_t *) & handle, symbol, TYPE_PROCEDURE, &f) == -1)
242  {
243    if (shl_findsym((shl_t *) & handle, symbol, TYPE_UNDEFINED, &f) == -1)
244    {
245      f = (func_ptr) NULL;
246    }
247  }
248  return ((void *)f);
249}
250
251int dynl_close (void *handle)
252{
253  shl_unload((shl_t) handle);
254  return(0);
255}
256
257const char *dynl_error()
258{
259  static char errmsg[] = "shl_load failed";
260
261  return errmsg;
262}
263#endif /* HPUX_9  or HPUX_10 */
264
265/*****************************************************************************
266 * SECTION generic: dynamic madules not available
267 *****************************************************************************/
268#ifndef DL_IMPLEMENTED
269
270int dynl_check_opened(char *filename)
271{
272  return FALSE;
273}
274
275void *dynl_open(char *filename)
276{
277  return(NULL);
278}
279
280void *dynl_sym(void *handle, const char *symbol)
281{
282  return(NULL);
283}
284
285int dynl_close (void *handle)
286{
287  return(0);
288}
289
290const char *dynl_error()
291{
292  static char errmsg[] = "support for dynamic loading not implemented";
293  return errmsg;
294}
295#endif
296
297#ifdef __cplusplus
298}
299#endif
300
301#endif /* HAVE_DL */
Note: See TracBrowser for help on using the repository browser.