Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2013 Intel Corporation
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21.  * IN THE SOFTWARE.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include "dri_util.h"
  26. #include <dlfcn.h>
  27. #include "main/macros.h"
  28.  
  29. /* We need GNU extensions to dlfcn.h in order to provide backward
  30.  * compatibility for the older DRI driver loader mechanism. (dladdr,
  31.  * Dl_info, and RTLD_DEFAULT)
  32.  */
  33. #if defined(RTLD_DEFAULT) && defined(HAVE_DLADDR)
  34.  
  35. #define MEGADRIVER_STUB_MAX_EXTENSIONS 10
  36. #define LIB_PATH_SUFFIX "_dri.so"
  37. #define LIB_PATH_SUFFIX_LENGTH (sizeof(LIB_PATH_SUFFIX)-1)
  38.  
  39. /* This is the table of extensions that the loader will dlsym() for.
  40.  *
  41.  * Initially it is empty for the megadriver stub, but the library
  42.  * constructor may initialize it based on the name of the library that
  43.  * is being loaded.
  44.  */
  45. PUBLIC const __DRIextension *
  46. __driDriverExtensions[MEGADRIVER_STUB_MAX_EXTENSIONS] = {
  47.    NULL
  48. };
  49.  
  50. /**
  51.  * This is a constructor function for the megadriver dynamic library.
  52.  *
  53.  * When the driver is dlopen'ed, this function will run. It will
  54.  * search for the name of the foo_dri.so file that was opened using
  55.  * the dladdr function.
  56.  *
  57.  * After finding foo's name, it will call __driDriverGetExtensions_foo
  58.  * and use the return to update __driDriverExtensions to enable
  59.  * compatibility with older DRI driver loaders.
  60.  */
  61. __attribute__((constructor)) static void
  62. megadriver_stub_init(void)
  63. {
  64.    Dl_info info;
  65.    char *driver_name;
  66.    size_t name_len;
  67.    char *get_extensions_name;
  68.    const __DRIextension **(*get_extensions)(void);
  69.    const __DRIextension **extensions;
  70.    int i;
  71.  
  72.    /* Call dladdr on __driDriverExtensions. We are really
  73.     * interested in the returned info.dli_fname so we can
  74.     * figure out the path name of the library being loaded.
  75.     */
  76.    i = dladdr((void*) __driDriverExtensions, &info);
  77.    if (i == 0)
  78.       return;
  79.  
  80.    /* Search for the last '/' character in the path. */
  81.    driver_name = strrchr(info.dli_fname, '/');
  82.    if (driver_name != NULL) {
  83.       /* Skip '/' character */
  84.       driver_name++;
  85.    } else {
  86.       /* Try using the start of the path */
  87.       driver_name = (char*) info.dli_fname;
  88.    }
  89.  
  90.    /* Make sure the path ends with _dri.so */
  91.    name_len = strlen(driver_name);
  92.    i = name_len - LIB_PATH_SUFFIX_LENGTH;
  93.    if (i < 0 || strcmp(driver_name + i, LIB_PATH_SUFFIX) != 0)
  94.       return;
  95.  
  96.    /* Duplicate the string so we can modify it.
  97.     * So far we've been using info.dli_fname.
  98.     */
  99.    driver_name = strdup(driver_name);
  100.    if (!driver_name)
  101.       return;
  102.  
  103.    /* The path ends with _dri.so. Chop this part of the
  104.     * string off. Then we'll have the driver's final name.
  105.     */
  106.    driver_name[i] = '\0';
  107.  
  108.    i = asprintf(&get_extensions_name, "%s_%s",
  109.                 __DRI_DRIVER_GET_EXTENSIONS, driver_name);
  110.    free(driver_name);
  111.    if (i == -1)
  112.       return;
  113.  
  114.    /* dlsym to get the driver's get extensions function. We
  115.     * don't have the dlopen handle, so we have to use
  116.     * RTLD_DEFAULT. It seems unlikely that the symbol will
  117.     * be found in another library, but this isn't optimal.
  118.     */
  119.    get_extensions = dlsym(RTLD_DEFAULT, get_extensions_name);
  120.    free(get_extensions_name);
  121.    if (!get_extensions)
  122.       return;
  123.  
  124.    /* Use the newer DRI loader entrypoint to find extensions.
  125.     * We will then expose these extensions via the older
  126.     * __driDriverExtensions symbol.
  127.     */
  128.    extensions = get_extensions();
  129.  
  130.    /* Copy the extensions into the __driDriverExtensions array
  131.     * we declared.
  132.     */
  133.    for (i = 0; i < ARRAY_SIZE(__driDriverExtensions); i++) {
  134.       __driDriverExtensions[i] = extensions[i];
  135.       if (extensions[i] == NULL)
  136.          break;
  137.    }
  138.  
  139.    /* If the driver had more extensions than we reserved, then
  140.     * bail out.
  141.     */
  142.    if (i == ARRAY_SIZE(__driDriverExtensions)) {
  143.       __driDriverExtensions[0] = NULL;
  144.       fprintf(stderr, "Megadriver stub did not reserve enough extension "
  145.               "slots.\n");
  146.       return;
  147.    }
  148. }
  149.  
  150. #endif /* RTLD_DEFAULT && HAVE_DLADDR */
  151.  
  152. static const
  153. __DRIconfig **stub_error_init_screen(__DRIscreen *psp)
  154. {
  155.    fprintf(stderr, "An updated DRI driver loader (libGL.so or X Server) is "
  156.            "required for this Mesa driver.\n");
  157.    return NULL;
  158. }
  159.  
  160. /**
  161.  * This is a stub driDriverAPI that is referenced by dri_util.c but should
  162.  * never be used.
  163.  */
  164. const struct __DriverAPIRec driDriverAPI = {
  165.    .InitScreen = stub_error_init_screen,
  166. };
  167.