source: git/libpolys/polys/mod_raw.cc @ 9ccaaf

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