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. */
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;
34 hash
= (hash
<< 4) + *name
++;
35 hi
= hash
& 0xf0000000;
39 /* The ELF ABI says `hash &= ~hi', but this is equivalent
40 in this case and on some machines one insn instead of two. */
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. */
54 _dl_lookup_symbol (const char *undef_name
, const ElfW(Sym
) **ref
,
55 struct link_map
*symbol_scope
[],
56 const char *reference_name
,
59 const unsigned long int hash
= _dl_elf_hash (undef_name
);
64 } weak_value
= { 0, NULL
};
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
;
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
)
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
];
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
))
98 switch (ELFW(ST_TYPE
) (sym
->st_info
))
105 /* Not a code/data definition. */
109 if (sym
!= *ref
&& strcmp (strtab
+ sym
->st_name
, undef_name
))
110 /* Not the symbol we are looking for. */
113 switch (ELFW(ST_BIND
) (sym
->st_info
))
116 /* Global definition. Just what we need. */
120 /* Weak definition. Use this value if we don't find
125 weak_value
.a
= map
->l_addr
;
129 /* Local symbols are ignored. */
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
);
152 /* Cache the location of MAP's hash table. */
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
);
159 map
->l_nbuckets
= *hash
++;
161 map
->l_buckets
= hash
;
162 hash
+= map
->l_nbuckets
;