source: git/libpolys/polys/mod_raw.cc @ 319eab

jengelh-datetimespielwiese
Last change on this file since 319eab was 319eab, checked in by Hans Schoenemann <hannes@…>, 9 years ago
chg: singuname stuff -> standard defines
  • Property mode set to 100644
File size: 6.9 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#if defined(__ELF__)
133#define HAVE_ELF_SYSTEM
134#endif
135
136// Mac OsX is an ELF system, but does not define __ELF__
137#if (defined(__APPLE__) && defined(__MACH__)) && (!defined(HAVE_ELF_SYSTEM))
138#define HAVE_ELF_SYSTEM
139#endif
140
141// Solaris is an ELF system, but does not define __ELF__
142#if defined(__sun) && defined(__SVR4)
143#define HAVE_ELF_SYSTEM
144#endif
145
146#if defined(HAVE_ELF_SYSTEM)
147#include <dlfcn.h>
148#define DL_IMPLEMENTED
149
150static void* kernel_handle = NULL;
151int dynl_check_opened(
152  char *filename    /* I: filename to check */
153  )
154{
155  return dlopen(filename,RTLD_NOW|RTLD_NOLOAD) != NULL;
156}
157
158void *dynl_open(
159  char *filename    /* I: filename to load */
160  )
161{
162// glibc 2.2:
163  if ((filename==NULL) || (dlopen(filename,RTLD_NOW|RTLD_NOLOAD)==NULL))
164    return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL));
165  else
166    Werror("module %s already loaded",filename);
167  return NULL;
168// alternative
169//    return(dlopen(filename, RTLD_NOW|RTLD_GLOBAL));
170}
171
172void *dynl_sym(void *handle, const char *symbol)
173{
174  if (handle == DYNL_KERNEL_HANDLE)
175  {
176    if (kernel_handle == NULL)
177      kernel_handle = dynl_open(NULL);
178    handle = kernel_handle;
179  }
180  return(dlsym(handle, symbol));
181}
182
183int dynl_close (void *handle)
184{
185  return(dlclose (handle));
186}
187
188const char *dynl_error()
189{
190  return(dlerror());
191}
192#endif /* ELF_SYSTEM */
193
194/*****************************************************************************
195 * SECTION HPUX-9/10                                                         *
196 *****************************************************************************/
197#if defined(HPUX_9) || defined(HPUX_10)
198#define DL_IMPLEMENTED
199#include <dl.h>
200
201typedef char *((*func_ptr) ());
202
203int dynl_check_opened(    /* NOTE: untested */
204  char *filename    /* I: filename to check */
205  )
206{
207  struct shl_descriptor *desc;
208  for (int idx = 0; shl_get(idx, &desc) != -1; ++idx)
209  {
210    if (strcmp(filename, desc->filename) == 0) return TRUE;
211  }
212  return FALSE;
213}
214
215void *dynl_open(char *filename)
216{
217  shl_t           handle = shl_load(filename, BIND_DEFERRED, 0);
218
219  return ((void *) handle);
220}
221
222void *dynl_sym(void *handle, const char *symbol)
223{
224  func_ptr        f;
225
226  if (handle == DYNL_KERNEL_HANDLE)
227    handle = PROG_HANDLE;
228
229  if (shl_findsym((shl_t *) & handle, symbol, TYPE_PROCEDURE, &f) == -1)
230  {
231    if (shl_findsym((shl_t *) & handle, symbol, TYPE_UNDEFINED, &f) == -1)
232    {
233      f = (func_ptr) NULL;
234    }
235  }
236  return ((void *)f);
237}
238
239int dynl_close (void *handle)
240{
241  shl_unload((shl_t) handle);
242  return(0);
243}
244
245const char *dynl_error()
246{
247  static char errmsg[] = "shl_load failed";
248
249  return errmsg;
250}
251#endif /* HPUX_9  or HPUX_10 */
252
253/*****************************************************************************
254 * SECTION generic: dynamic madules not available
255 *****************************************************************************/
256#ifndef DL_IMPLEMENTED
257
258int dynl_check_opened(char *filename)
259{
260  return FALSE;
261}
262
263void *dynl_open(char *filename)
264{
265  return(NULL);
266}
267
268void *dynl_sym(void *handle, const char *symbol)
269{
270  return(NULL);
271}
272
273int dynl_close (void *handle)
274{
275  return(0);
276}
277
278const char *dynl_error()
279{
280  static char errmsg[] = "support for dynamic loading not implemented";
281  return errmsg;
282}
283#endif
284
285#ifdef __cplusplus
286}
287#endif
288
289#endif /* HAVE_DL */
Note: See TracBrowser for help on using the repository browser.