File: shlibdeps.c

package info (click to toggle)
julia 0.4.7-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 22,128 kB
  • ctags: 15,440
  • sloc: lisp: 146,606; ansic: 96,638; sh: 12,615; cpp: 11,846; makefile: 3,431; python: 1,005; pascal: 856; xml: 585; f90: 415; java: 343; asm: 86; perl: 77
file content (47 lines) | stat: -rw-r--r-- 1,193 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
 * Query soname of shared library.
 * Copyright © 2015 Peter Colberg.
 * Distributed under the MIT license.
 */

#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>

#define error(fmt, ...) fprintf(stderr, "%s: "fmt"\n", argv[0], ##__VA_ARGS__)

/*
 * For a given symbol name, this program prints the path relative to /
 * of the shared library containing that symbol. The program must be
 * linked against the shared libraries to be queried for symbols, and
 * compiled as a position-independent executable using -fPIE.
 */
int main(int argc, const char *const *argv)
{
  Dl_info info;
  void *sym;

  if (argc != 2) {
    fprintf(stderr, "Usage: %s SYMBOL\n", argv[0]);
    return 1;
  }
  if (!(sym = dlsym(RTLD_DEFAULT, argv[1]))) {
    fprintf(stderr, "%s\n", dlerror());
    return 1;
  }
  if (dladdr(sym, &info) == 0) {
    fprintf(stderr, "%s\n", dlerror());
    return 1;
  }
  if (info.dli_saddr != sym) {
    error("mismatching symbol name: %s", info.dli_sname);
    return 1;
  }
  if (info.dli_fname[0] != '/') {
    error("library name not an absolute path: %s", info.dli_fname);
    return 1;
  }
  printf("%s", info.dli_fname + 1);
  return 0;
}