aboutsummaryrefslogtreecommitdiff
path: root/elf/dl-lookup-direct.c
blob: e74ca9562f32ec480ecd200210bac079562f54f3 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* Look up a symbol in a single specified object.
   Copyright (C) 1995-2022 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <ldsodefs.h>
#include <string.h>
#include <elf_machine_sym_no_match.h>
#include <dl-hash.h>

/* This function corresponds to do_lookup_x in elf/dl-lookup.c.  The
   variant here is simplified because it requires symbol
   versioning.  */
static const ElfW(Sym) *
check_match (const struct link_map *const map, const char *const undef_name,
             const char *version, uint32_t version_hash,
             const Elf_Symndx symidx)
{
  const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
  const ElfW(Sym) *sym = &symtab[symidx];

  unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
  if (__glibc_unlikely ((sym->st_value == 0 /* No value.  */
                         && sym->st_shndx != SHN_ABS
                         && stt != STT_TLS)
                        || elf_machine_sym_no_match (sym)))
    return NULL;

  /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
     STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
     code/data definitions.  */
#define ALLOWED_STT \
  ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
   | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
  if (__glibc_unlikely (((1 << stt) & ALLOWED_STT) == 0))
    return NULL;

  const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);

  if (strcmp (strtab + sym->st_name, undef_name) != 0)
    /* Not the symbol we are looking for.  */
    return NULL;

  ElfW(Half) ndx = map->l_versyms[symidx] & 0x7fff;
  if (map->l_versions[ndx].hash != version_hash
      || strcmp (map->l_versions[ndx].name, version) != 0)
    /* It's not the version we want.  */
    return NULL;

  return sym;
}


/* This function corresponds to do_lookup_x in elf/dl-lookup.c.  The
   variant here is simplified because it does not search object
   dependencies.  It is optimized for a successful lookup.  */
const ElfW(Sym) *
_dl_lookup_direct (struct link_map *map,
                   const char *undef_name, uint32_t new_hash,
                   const char *version, uint32_t version_hash)
{
  const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
  if (__glibc_likely (bitmask != NULL))
    {
      Elf32_Word bucket = map->l_gnu_buckets[new_hash % map->l_nbuckets];
      if (bucket != 0)
        {
          const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];

          do
            if (((*hasharr ^ new_hash) >> 1) == 0)
              {
                Elf_Symndx symidx = ELF_MACHINE_HASH_SYMIDX (map, hasharr);
                const ElfW(Sym) *sym = check_match (map, undef_name,
                                                    version, version_hash,
                                                    symidx);
                if (sym != NULL)
                  return sym;
              }
          while ((*hasharr++ & 1u) == 0);
        }
    }
  else
    {
      /* Fallback code for lack of GNU_HASH support.  */
      uint32_t old_hash = _dl_elf_hash (undef_name);

      /* Use the old SysV-style hash table.  Search the appropriate
         hash bucket in this object's symbol table for a definition
         for the same symbol name.  */
      for (Elf_Symndx symidx = map->l_buckets[old_hash % map->l_nbuckets];
           symidx != STN_UNDEF;
           symidx = map->l_chain[symidx])
        {
          const ElfW(Sym) *sym = check_match (map, undef_name,
                                              version, version_hash, symidx);
          if (sym != NULL)
            return sym;
        }
    }

  return NULL;
}