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#include <string.h>
54#include "os.h"
55#include "loader.h"
56#include "loaderProcs.h"
57
58#ifdef HAVE_DLFCN_H
59
60#include <dlfcn.h>
61#include <X11/Xos.h>
62
63#else
64#error i have no dynamic linker and i must scream
65#endif
66
67#ifndef XORG_NO_SDKSYMS
68extern void *xorg_symbols[];
69#endif
70
71void
72LoaderInit(void)
73{
74#ifndef XORG_NO_SDKSYMS
75    LogMessageVerb(X_INFO, 2, "Loader magic: %p\n", (void *) xorg_symbols);
76#endif
77    LogMessageVerb(X_INFO, 2, "Module ABI versions:\n");
78    LogWrite(2, "\t%s: %d.%d\n", ABI_CLASS_ANSIC,
79             GET_ABI_MAJOR(LoaderVersionInfo.ansicVersion),
80             GET_ABI_MINOR(LoaderVersionInfo.ansicVersion));
81    LogWrite(2, "\t%s: %d.%d\n", ABI_CLASS_VIDEODRV,
82             GET_ABI_MAJOR(LoaderVersionInfo.videodrvVersion),
83             GET_ABI_MINOR(LoaderVersionInfo.videodrvVersion));
84    LogWrite(2, "\t%s : %d.%d\n", ABI_CLASS_XINPUT,
85             GET_ABI_MAJOR(LoaderVersionInfo.xinputVersion),
86             GET_ABI_MINOR(LoaderVersionInfo.xinputVersion));
87    LogWrite(2, "\t%s : %d.%d\n", ABI_CLASS_EXTENSION,
88             GET_ABI_MAJOR(LoaderVersionInfo.extensionVersion),
89             GET_ABI_MINOR(LoaderVersionInfo.extensionVersion));
90
91}
92
93/* Public Interface to the loader. */
94
95void *
96LoaderOpen(const char *module, int *errmaj)
97{
98    void *ret;
99
100#if defined(DEBUG)
101    ErrorF("LoaderOpen(%s)\n", module);
102#endif
103
104    LogMessage(X_INFO, "Loading %s\n", module);
105
106    if (!(ret = dlopen(module, RTLD_LAZY | RTLD_GLOBAL))) {
107        LogMessage(X_ERROR, "Failed to load %s: %s\n", module, dlerror());
108        if (errmaj)
109            *errmaj = LDR_NOLOAD;
110        return NULL;
111    }
112
113    return ret;
114}
115
116void *
117LoaderSymbol(const char *name)
118{
119    static void *global_scope = NULL;
120    void *p;
121
122    p = dlsym(RTLD_DEFAULT, name);
123    if (p != NULL)
124        return p;
125
126    if (!global_scope)
127        global_scope = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
128
129    if (global_scope)
130        return dlsym(global_scope, name);
131
132    return NULL;
133}
134
135void *
136LoaderSymbolFromModule(void *handle, const char *name)
137{
138    ModuleDescPtr mod = handle;
139    return dlsym(mod->handle, name);
140}
141
142void
143LoaderUnload(const char *name, void *handle)
144{
145    LogMessageVerbSigSafe(X_INFO, 1, "Unloading %s\n", name);
146    if (handle)
147        dlclose(handle);
148}
149
150unsigned long LoaderOptions = 0;
151
152void
153LoaderSetOptions(unsigned long opts)
154{
155    LoaderOptions |= opts;
156}
157
158Bool
159LoaderShouldIgnoreABI(void)
160{
161    return (LoaderOptions & LDR_OPT_ABI_MISMATCH_NONFATAL) != 0;
162}
163
164int
165LoaderGetABIVersion(const char *abiclass)
166{
167    struct {
168        const char *name;
169        int version;
170    } classes[] = {
171        {ABI_CLASS_ANSIC, LoaderVersionInfo.ansicVersion},
172        {ABI_CLASS_VIDEODRV, LoaderVersionInfo.videodrvVersion},
173        {ABI_CLASS_XINPUT, LoaderVersionInfo.xinputVersion},
174        {ABI_CLASS_EXTENSION, LoaderVersionInfo.extensionVersion},
175        {NULL, 0}
176    };
177    int i;
178
179    for (i = 0; classes[i].name; i++) {
180        if (!strcmp(classes[i].name, abiclass)) {
181            return classes[i].version;
182        }
183    }
184
185    return 0;
186}
187