PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
darwin.c
Go to the documentation of this file.
1 /*
2  * Dynamic loading support for macOS (Darwin)
3  *
4  * If dlopen() is available (Darwin 10.3 and later), we just use it.
5  * Otherwise we emulate it with the older, now deprecated, NSLinkModule API.
6  *
7  * src/backend/port/dynloader/darwin.c
8  */
9 #include "postgres.h"
10 
11 #ifdef HAVE_DLOPEN
12 #include <dlfcn.h>
13 #else
14 #include <mach-o/dyld.h>
15 #endif
16 
17 #include "dynloader.h"
18 
19 
20 #ifdef HAVE_DLOPEN
21 
22 void *
23 pg_dlopen(char *filename)
24 {
25  return dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
26 }
27 
28 void
29 pg_dlclose(void *handle)
30 {
31  dlclose(handle);
32 }
33 
35 pg_dlsym(void *handle, char *funcname)
36 {
37  /* Do not prepend an underscore: see dlopen(3) */
38  return dlsym(handle, funcname);
39 }
40 
41 char *
42 pg_dlerror(void)
43 {
44  return dlerror();
45 }
46 #else /* !HAVE_DLOPEN */
47 
48 /*
49  * These routines were taken from the Apache source, but were made
50  * available with a PostgreSQL-compatible license. Kudos Wilfredo
51  * S├ínchez <wsanchez@apple.com>.
52  */
53 
54 static NSObjectFileImageReturnCode cofiff_result = NSObjectFileImageFailure;
55 
56 void *
58 {
59  NSObjectFileImage image;
60 
61  cofiff_result = NSCreateObjectFileImageFromFile(filename, &image);
62  if (cofiff_result != NSObjectFileImageSuccess)
63  return NULL;
64  return NSLinkModule(image, filename,
65  NSLINKMODULE_OPTION_BINDNOW |
66  NSLINKMODULE_OPTION_RETURN_ON_ERROR);
67 }
68 
69 void
70 pg_dlclose(void *handle)
71 {
72  NSUnLinkModule(handle, FALSE);
73 }
74 
76 pg_dlsym(void *handle, char *funcname)
77 {
78  NSSymbol symbol;
79  char *symname = (char *) malloc(strlen(funcname) + 2);
80 
81  if (!symname)
82  return NULL;
83 
84  sprintf(symname, "_%s", funcname);
85  if (NSIsSymbolNameDefined(symname))
86  {
87  symbol = NSLookupAndBindSymbol(symname);
88 
89  free(symname);
90  return (PGFunction) NSAddressOfSymbol(symbol);
91  }
92  else
93  {
94  free(symname);
95  return NULL;
96  }
97 }
98 
99 char *
101 {
102  NSLinkEditErrors c;
103  int errorNumber;
104  const char *fileName;
105  const char *errorString = NULL;
106 
107  switch (cofiff_result)
108  {
109  case NSObjectFileImageSuccess:
110  /* must have failed in NSLinkModule */
111  NSLinkEditError(&c, &errorNumber, &fileName, &errorString);
112  if (errorString == NULL || *errorString == '\0')
113  errorString = "unknown link-edit failure";
114  break;
115  case NSObjectFileImageFailure:
116  errorString = "failed to open object file";
117  break;
118  case NSObjectFileImageInappropriateFile:
119  errorString = "inappropriate object file";
120  break;
121  case NSObjectFileImageArch:
122  errorString = "object file is for wrong architecture";
123  break;
124  case NSObjectFileImageFormat:
125  errorString = "object file has wrong format";
126  break;
127  case NSObjectFileImageAccess:
128  errorString = "insufficient permissions for object file";
129  break;
130  default:
131  errorString = "unknown failure to open object file";
132  break;
133  }
134 
135  return (char *) errorString;
136 }
137 
138 #endif /* HAVE_DLOPEN */
void * pg_dlopen(char *filename)
Definition: darwin.c:57
char * dlerror(void)
Definition: win32.c:32
static NSObjectFileImageReturnCode cofiff_result
Definition: darwin.c:54
Datum(* PGFunction)(FunctionCallInfo fcinfo)
Definition: fmgr.h:40
PGFunction pg_dlsym(void *handle, char *funcname)
Definition: darwin.c:76
#define malloc(a)
Definition: header.h:45
int dlclose(void *handle)
Definition: win32.c:41
void pg_dlclose(void *handle)
Definition: darwin.c:70
#define FALSE
Definition: c.h:218
void * dlsym(void *handle, const char *symbol)
Definition: win32.c:53
char * c
char * pg_dlerror(void)
Definition: darwin.c:100
#define free(a)
Definition: header.h:60
#define NULL
Definition: c.h:226
static char * filename
Definition: pg_dumpall.c:80
#define RTLD_GLOBAL
Definition: aix.h:31
void * dlopen(const char *path, int mode)
Definition: win32.c:68
#define RTLD_NOW
Definition: aix.h:28
unsigned char symbol
Definition: api.h:2