1/*
2 * Copyright 1995-1998 by Metro Link, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Metro Link, Inc. not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission.  Metro Link, Inc. makes no
11 * representations about the suitability of this software for any purpose.
12 *  It is provided "as is" without express or implied warranty.
13 *
14 * METRO LINK, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL METRO LINK, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 */
22/*
23 * Copyright (c) 1997-2003 by The XFree86 Project, Inc.
24 *
25 * Permission is hereby granted, free of charge, to any person obtaining a
26 * copy of this software and associated documentation files (the "Software"),
27 * to deal in the Software without restriction, including without limitation
28 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
29 * and/or sell copies of the Software, and to permit persons to whom the
30 * Software is furnished to do so, subject to the following conditions:
31 *
32 * The above copyright notice and this permission notice shall be included in
33 * all copies or substantial portions of the Software.
34 *
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
38 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
39 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
40 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
41 * OTHER DEALINGS IN THE SOFTWARE.
42 *
43 * Except as contained in this notice, the name of the copyright holder(s)
44 * and author(s) shall not be used in advertising or otherwise to promote
45 * the sale, use or other dealings in this Software without prior written
46 * authorization from the copyright holder(s) and author(s).
47 */
48
49#ifdef HAVE_XORG_CONFIG_H
50#include <xorg-config.h>
51#endif
52
53/*#ifdef HAVE_DIX_CONFIG_H*/
54#include <dix-config.h>
55/*#endif*/
56
57#include <errno.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <sys/types.h>
61#include <unistd.h>
62#include <sys/stat.h>
63#include <fcntl.h>
64#include <string.h>
65#include <stdarg.h>
66
67#include "os.h"
68#include "loader.h"
69#include "loaderProcs.h"
70#include "xf86.h"
71#include "xf86Priv.h"
72#include "compiler.h"
73
74#ifdef HAVE_DLFCN_H
75
76#include <dlfcn.h>
77#include <X11/Xos.h>
78
79#if defined(DL_LAZY)
80#define DLOPEN_LAZY DL_LAZY
81#elif defined(RTLD_LAZY)
82#define DLOPEN_LAZY RTLD_LAZY
83#elif defined(__FreeBSD__)
84#define DLOPEN_LAZY 1
85#else
86#define DLOPEN_LAZY 0
87#endif
88
89#if defined(LD_GLOBAL)
90#define DLOPEN_GLOBAL LD_GLOBAL
91#elif defined(RTLD_GLOBAL)
92#define DLOPEN_GLOBAL RTLD_GLOBAL
93#else
94#define DLOPEN_GLOBAL 0
95#endif
96
97#else
98#error i have no dynamic linker and i must scream
99#endif
100
101extern void *xorg_symbols[];
102
103void
104LoaderInit(void)
105{
106    xf86MsgVerb(X_INFO, 2, "Loader magic: %p\n", (void *)xorg_symbols);
107    xf86MsgVerb(X_INFO, 2, "Module ABI versions:\n");
108    xf86ErrorFVerb(2, "\t%s: %d.%d\n", ABI_CLASS_ANSIC,
109		   GET_ABI_MAJOR(LoaderVersionInfo.ansicVersion),
110		   GET_ABI_MINOR(LoaderVersionInfo.ansicVersion));
111    xf86ErrorFVerb(2, "\t%s: %d.%d\n", ABI_CLASS_VIDEODRV,
112		   GET_ABI_MAJOR(LoaderVersionInfo.videodrvVersion),
113		   GET_ABI_MINOR(LoaderVersionInfo.videodrvVersion));
114    xf86ErrorFVerb(2, "\t%s : %d.%d\n", ABI_CLASS_XINPUT,
115		   GET_ABI_MAJOR(LoaderVersionInfo.xinputVersion),
116		   GET_ABI_MINOR(LoaderVersionInfo.xinputVersion));
117    xf86ErrorFVerb(2, "\t%s : %d.%d\n", ABI_CLASS_EXTENSION,
118		   GET_ABI_MAJOR(LoaderVersionInfo.extensionVersion),
119		   GET_ABI_MINOR(LoaderVersionInfo.extensionVersion));
120
121}
122
123/* Public Interface to the loader. */
124
125void *
126LoaderOpen(const char *module, int *errmaj, int *errmin)
127{
128    void *ret;
129
130#if defined(DEBUG)
131    ErrorF("LoaderOpen(%s)\n", module);
132#endif
133
134    xf86Msg(X_INFO, "Loading %s\n", module);
135
136    if (!(ret = dlopen(module, DLOPEN_LAZY | DLOPEN_GLOBAL))) {
137	xf86Msg(X_ERROR, "Failed to load %s: %s\n", module, dlerror());
138	if (errmaj)
139	    *errmaj = LDR_NOLOAD;
140	if (errmin)
141	    *errmin = LDR_NOLOAD;
142	return NULL;
143    }
144
145    return ret;
146}
147
148void *
149LoaderSymbol(const char *name)
150{
151    static void *global_scope = NULL;
152    void *p;
153
154    p = dlsym(RTLD_DEFAULT, name);
155    if (p != NULL)
156	return p;
157
158    if (!global_scope)
159	global_scope = dlopen(NULL, DLOPEN_LAZY | DLOPEN_GLOBAL);
160
161    if (global_scope)
162	return dlsym(global_scope, name);
163
164    return NULL;
165}
166
167void
168LoaderUnload(const char *name, void *handle)
169{
170    xf86Msg(X_INFO, "Unloading %s\n", name);
171    if (handle)
172	dlclose(handle);
173}
174
175unsigned long LoaderOptions = 0;
176
177void
178LoaderSetOptions(unsigned long opts)
179{
180    LoaderOptions |= opts;
181}
182
183Bool
184LoaderShouldIgnoreABI(void)
185{
186    return (LoaderOptions & LDR_OPT_ABI_MISMATCH_NONFATAL) != 0;
187}
188
189int
190LoaderGetABIVersion(const char *abiclass)
191{
192    struct {
193        const char *name;
194        int version;
195    } classes[] = {
196        { ABI_CLASS_ANSIC,     LoaderVersionInfo.ansicVersion },
197        { ABI_CLASS_VIDEODRV,  LoaderVersionInfo.videodrvVersion },
198        { ABI_CLASS_XINPUT,    LoaderVersionInfo.xinputVersion },
199        { ABI_CLASS_EXTENSION, LoaderVersionInfo.extensionVersion },
200        { ABI_CLASS_FONT,      LoaderVersionInfo.fontVersion },
201        { NULL,                0 }
202    };
203    int i;
204
205    for(i = 0; classes[i].name; i++) {
206        if(!strcmp(classes[i].name, abiclass)) {
207            return classes[i].version;
208        }
209    }
210
211    return 0;
212}
213