<commit_before>/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2014 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU General Public License, as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any
 * later version. Please see the file LICENSE-GPL for details.
 *
 * Web Page: http://mielke.cc/brltty/
 *
 * This software is maintained by <NAME> <<EMAIL>>.
 */

#include "prologue.h"

#include <dlfcn.h>

#ifdef __MSDOS__
#include <debug/syms.h>
#include "program.h"
#endif /* __MSDOS__ */

#include "log.h"
#include "dynld.h"

static void
clearError (void) {
  dlerror();
}

static int
logError (void) {
  const char *error = dlerror();
  if (!error) return 1;

  logMessage(LOG_ERR, "%s", error);
  return 0;
}

static inline int
getSharedObjectLoadFlags (void) {
  int flags = 0;

#ifdef DL_LAZY
  flags |= DL_LAZY;
#else /* DL_LAZY */
  flags |= RTLD_LAZY | RTLD_GLOBAL;
#endif /* DL_LAZY */

  return flags;
}

void *
loadSharedObject (const char *path) {
  void *object;

  clearError();
  object = dlopen(path, getSharedObjectLoadFlags());
  if (!object) logError();
  return object;
}

void 
unloadSharedObject (void *object) {
  clearError();
  if (dlclose(object)) logError();
}

int 
findSharedSymbol (void *object, const char *symbol, void *pointerAddress) {
  void **address = pointerAddress;

  clearError(); /* clear any previous error condition */
  *address = dlsym(object, symbol);
  return logError();
}

const char *
getSharedSymbolName (void *address, ptrdiff_t *offset) {
#if defined(__MSDOS__)
  {
    static int symsInitialized = 0;

    if (!symsInitialized) {
      syms_init((char *)programPath);
      symsInitialized = 1;
    }
  }

  {
    unsigned long delta;
    char *name = syms_val2name((unsigned long)address, &delta);

    if (name) {
      if (offset) *offset = delta;
      return name;
    }
  }

#elif defined(__USE_GNU)
  Dl_info info;

  if (dladdr(address, &info)) {
    if (offset) *offset = address - info.dli_saddr;
    return info.dli_sname;
  }

#else /* get symbol name */
#warning symbol address to name lookup not supported on tghis platform
#endif /* get symbol name */

  return NULL;
}<commit_msg>No need to warn if dynld_dlfcn doesn't support address to symbol lookup. (dm)<commit_after>/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2014 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU General Public License, as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any
 * later version. Please see the file LICENSE-GPL for details.
 *
 * Web Page: http://mielke.cc/brltty/
 *
 * This software is maintained by <NAME> <<EMAIL>>.
 */

#include "prologue.h"

#include <dlfcn.h>

#ifdef __MSDOS__
#include <debug/syms.h>
#include "program.h"
#endif /* __MSDOS__ */

#include "log.h"
#include "dynld.h"

static void
clearError (void) {
  dlerror();
}

static int
logError (void) {
  const char *error = dlerror();
  if (!error) return 1;

  logMessage(LOG_ERR, "%s", error);
  return 0;
}

static inline int
getSharedObjectLoadFlags (void) {
  int flags = 0;

#ifdef DL_LAZY
  flags |= DL_LAZY;
#else /* DL_LAZY */
  flags |= RTLD_LAZY | RTLD_GLOBAL;
#endif /* DL_LAZY */

  return flags;
}

void *
loadSharedObject (const char *path) {
  void *object;

  clearError();
  object = dlopen(path, getSharedObjectLoadFlags());
  if (!object) logError();
  return object;
}

void 
unloadSharedObject (void *object) {
  clearError();
  if (dlclose(object)) logError();
}

int 
findSharedSymbol (void *object, const char *symbol, void *pointerAddress) {
  void **address = pointerAddress;

  clearError(); /* clear any previous error condition */
  *address = dlsym(object, symbol);
  return logError();
}

const char *
getSharedSymbolName (void *address, ptrdiff_t *offset) {
#if defined(__MSDOS__)
  {
    static int symsInitialized = 0;

    if (!symsInitialized) {
      syms_init((char *)programPath);
      symsInitialized = 1;
    }
  }

  {
    unsigned long delta;
    char *name = syms_val2name((unsigned long)address, &delta);

    if (name) {
      if (offset) *offset = delta;
      return name;
    }
  }

#elif defined(__USE_GNU)
  Dl_info info;

  if (dladdr(address, &info)) {
    if (offset) *offset = address - info.dli_saddr;
    return info.dli_sname;
  }

#endif /* get symbol name */

  return NULL;
}
{'fraction_non_alphanumeric': 0.12010768275005176, 'fraction_numerical': 0.006212466349140609, 'mean_word_length': 3.5267104029990626, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2448252', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1628, 'n_words': 525}