update from main archive 960814
[glibc.git] / elf / dl-lookup.c
blobe57887bfddd06807c46bc460c1c9e6e1c39b1149
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #include <stddef.h>
21 #include <link.h>
22 #include <assert.h>
23 #include <string.h>
26 /* This is the hashing function specified by the ELF ABI. */
27 static inline unsigned
28 _dl_elf_hash (const char *name)
30 unsigned long int hash = 0;
31 while (*name != '\0')
33 unsigned long int hi;
34 hash = (hash << 4) + *name++;
35 hi = hash & 0xf0000000;
36 if (hi != 0)
38 hash ^= hi >> 24;
39 /* The ELF ABI says `hash &= ~hi', but this is equivalent
40 in this case and on some machines one insn instead of two. */
41 hash ^= hi;
44 return hash;
47 /* Search loaded objects' symbol tables for a definition of the symbol
48 UNDEF_NAME. FLAGS is a set of flags. If DL_LOOKUP_NOEXEC is set,
49 then don't search the executable for a definition; this used for
50 copy relocs. If DL_LOOKUP_NOPLT is set, then a PLT entry cannot
51 satisfy the reference; some different binding must be found. */
53 ElfW(Addr)
54 _dl_lookup_symbol (const char *undef_name, const ElfW(Sym) **ref,
55 struct link_map *symbol_scope[],
56 const char *reference_name,
57 int flags)
59 const unsigned long int hash = _dl_elf_hash (undef_name);
60 struct
62 ElfW(Addr) a;
63 const ElfW(Sym) *s;
64 } weak_value = { 0, NULL };
65 size_t i;
66 struct link_map **scope, *map;
68 /* Search the relevant loaded objects for a definition. */
69 for (scope = symbol_scope; *scope; ++scope)
70 for (i = 0; i < (*scope)->l_nsearchlist; ++i)
72 const ElfW(Sym) *symtab;
73 const char *strtab;
74 ElfW(Symndx) symidx;
76 map = (*scope)->l_searchlist[i];
78 /* Don't search the executable when resolving a copy reloc. */
79 if (flags & DL_LOOKUP_NOEXEC && map->l_type == lt_executable)
80 continue;
82 symtab = ((void *) map->l_addr + map->l_info[DT_SYMTAB]->d_un.d_ptr);
83 strtab = ((void *) map->l_addr + map->l_info[DT_STRTAB]->d_un.d_ptr);
85 /* Search the appropriate hash bucket in this object's symbol table
86 for a definition for the same symbol name. */
87 for (symidx = map->l_buckets[hash % map->l_nbuckets];
88 symidx != STN_UNDEF;
89 symidx = map->l_chain[symidx])
91 const ElfW(Sym) *sym = &symtab[symidx];
93 if (sym->st_value == 0 || /* No value. */
94 ((flags & DL_LOOKUP_NOPLT) != 0 /* Reject PLT entry. */
95 && sym->st_shndx == SHN_UNDEF))
96 continue;
98 switch (ELFW(ST_TYPE) (sym->st_info))
100 case STT_NOTYPE:
101 case STT_FUNC:
102 case STT_OBJECT:
103 break;
104 default:
105 /* Not a code/data definition. */
106 continue;
109 if (sym != *ref && strcmp (strtab + sym->st_name, undef_name))
110 /* Not the symbol we are looking for. */
111 continue;
113 switch (ELFW(ST_BIND) (sym->st_info))
115 case STB_GLOBAL:
116 /* Global definition. Just what we need. */
117 *ref = sym;
118 return map->l_addr;
119 case STB_WEAK:
120 /* Weak definition. Use this value if we don't find
121 another. */
122 if (! weak_value.s)
124 weak_value.s = sym;
125 weak_value.a = map->l_addr;
127 break;
128 default:
129 /* Local symbols are ignored. */
130 break;
135 if (weak_value.s == NULL &&
136 (*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK))
138 /* We could find no value for a strong reference. */
139 const char msg[] = "undefined symbol: ";
140 const size_t len = strlen (undef_name);
141 char buf[sizeof msg + len];
142 memcpy (buf, msg, sizeof msg - 1);
143 memcpy (&buf[sizeof msg - 1], undef_name, len + 1);
144 _dl_signal_error (0, reference_name, buf);
147 *ref = weak_value.s;
148 return weak_value.a;
152 /* Cache the location of MAP's hash table. */
154 void
155 _dl_setup_hash (struct link_map *map)
157 ElfW(Symndx) *hash = (void *)(map->l_addr + map->l_info[DT_HASH]->d_un.d_ptr);
158 ElfW(Symndx) nchain;
159 map->l_nbuckets = *hash++;
160 nchain = *hash++;
161 map->l_buckets = hash;
162 hash += map->l_nbuckets;
163 map->l_chain = hash;