xref: /minix3/sys/lib/libsa/lookup_elf32.c (revision 58a2b0008e28f606a7f7f5faaeaba4faac57a1ea)
1*58a2b000SEvgeniy Ivanov /* $NetBSD: lookup_elf32.c,v 1.3 2010/02/11 21:28:16 martin Exp $ */
2*58a2b000SEvgeniy Ivanov 
3*58a2b000SEvgeniy Ivanov /*-
4*58a2b000SEvgeniy Ivanov  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5*58a2b000SEvgeniy Ivanov  * All rights reserved.
6*58a2b000SEvgeniy Ivanov  *
7*58a2b000SEvgeniy Ivanov  * This code is derived from software contributed to The NetBSD Foundation
8*58a2b000SEvgeniy Ivanov  * by Martin Husemann.
9*58a2b000SEvgeniy Ivanov  *
10*58a2b000SEvgeniy Ivanov  * Redistribution and use in source and binary forms, with or without
11*58a2b000SEvgeniy Ivanov  * modification, are permitted provided that the following conditions
12*58a2b000SEvgeniy Ivanov  * are met:
13*58a2b000SEvgeniy Ivanov  * 1. Redistributions of source code must retain the above copyright
14*58a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer.
15*58a2b000SEvgeniy Ivanov  * 2. Redistributions in binary form must reproduce the above copyright
16*58a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer in the
17*58a2b000SEvgeniy Ivanov  *    documentation and/or other materials provided with the distribution.
18*58a2b000SEvgeniy Ivanov  *
19*58a2b000SEvgeniy Ivanov  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*58a2b000SEvgeniy Ivanov  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*58a2b000SEvgeniy Ivanov  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*58a2b000SEvgeniy Ivanov  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*58a2b000SEvgeniy Ivanov  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*58a2b000SEvgeniy Ivanov  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*58a2b000SEvgeniy Ivanov  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*58a2b000SEvgeniy Ivanov  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*58a2b000SEvgeniy Ivanov  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*58a2b000SEvgeniy Ivanov  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*58a2b000SEvgeniy Ivanov  * POSSIBILITY OF SUCH DAMAGE.
30*58a2b000SEvgeniy Ivanov  */
31*58a2b000SEvgeniy Ivanov 
32*58a2b000SEvgeniy Ivanov /* If not included by lookup_elf64.c, ELFSIZE won't be defined. */
33*58a2b000SEvgeniy Ivanov #ifndef ELFSIZE
34*58a2b000SEvgeniy Ivanov #define	ELFSIZE	32
35*58a2b000SEvgeniy Ivanov #endif
36*58a2b000SEvgeniy Ivanov 
37*58a2b000SEvgeniy Ivanov #include <lib/libkern/libkern.h>
38*58a2b000SEvgeniy Ivanov #include <sys/param.h>
39*58a2b000SEvgeniy Ivanov #include <sys/exec_elf.h>
40*58a2b000SEvgeniy Ivanov 
41*58a2b000SEvgeniy Ivanov #if ((ELFSIZE == 32) && defined(BOOT_ELF32)) || \
42*58a2b000SEvgeniy Ivanov     ((ELFSIZE == 64) && defined(BOOT_ELF64))
43*58a2b000SEvgeniy Ivanov 
44*58a2b000SEvgeniy Ivanov void * ELFNAMEEND(lookup_symbol)(const char *, void *, void *);
45*58a2b000SEvgeniy Ivanov 
46*58a2b000SEvgeniy Ivanov void *
ELFNAMEEND(lookup_symbol)47*58a2b000SEvgeniy Ivanov ELFNAMEEND(lookup_symbol)(const char *symname, void *sstab, void *estab)
48*58a2b000SEvgeniy Ivanov {
49*58a2b000SEvgeniy Ivanov 	Elf_Ehdr *elf;
50*58a2b000SEvgeniy Ivanov 	Elf_Shdr *shp;
51*58a2b000SEvgeniy Ivanov 	Elf_Sym *symtab_start, *symtab_end, *sp;
52*58a2b000SEvgeniy Ivanov 	char *strtab_start, *strtab_end;
53*58a2b000SEvgeniy Ivanov 	int i, j;
54*58a2b000SEvgeniy Ivanov 
55*58a2b000SEvgeniy Ivanov 	elf = sstab;
56*58a2b000SEvgeniy Ivanov 	if (elf->e_shoff == 0)
57*58a2b000SEvgeniy Ivanov 		return NULL;
58*58a2b000SEvgeniy Ivanov 
59*58a2b000SEvgeniy Ivanov 	switch (elf->e_machine) {
60*58a2b000SEvgeniy Ivanov 	ELFDEFNNAME(MACHDEP_ID_CASES)
61*58a2b000SEvgeniy Ivanov 	default:
62*58a2b000SEvgeniy Ivanov 		return NULL;
63*58a2b000SEvgeniy Ivanov 	}
64*58a2b000SEvgeniy Ivanov 
65*58a2b000SEvgeniy Ivanov 	symtab_start = symtab_end = NULL;
66*58a2b000SEvgeniy Ivanov 	strtab_start = strtab_end = NULL;
67*58a2b000SEvgeniy Ivanov 
68*58a2b000SEvgeniy Ivanov 	shp = (Elf_Shdr *)((char *)sstab + elf->e_shoff);
69*58a2b000SEvgeniy Ivanov 	for (i = 0; i < elf->e_shnum; i++) {
70*58a2b000SEvgeniy Ivanov 		if (shp[i].sh_type != SHT_SYMTAB)
71*58a2b000SEvgeniy Ivanov 			continue;
72*58a2b000SEvgeniy Ivanov 		if (shp[i].sh_offset == 0)
73*58a2b000SEvgeniy Ivanov 			continue;
74*58a2b000SEvgeniy Ivanov 		symtab_start = (Elf_Sym*)((char*)sstab + shp[i].sh_offset);
75*58a2b000SEvgeniy Ivanov 		symtab_end = (Elf_Sym*)((char*)sstab + shp[i].sh_offset
76*58a2b000SEvgeniy Ivanov 			+ shp[i].sh_size);
77*58a2b000SEvgeniy Ivanov 		j = shp[i].sh_link;
78*58a2b000SEvgeniy Ivanov 		if (shp[j].sh_offset == 0)
79*58a2b000SEvgeniy Ivanov 			continue;
80*58a2b000SEvgeniy Ivanov 		strtab_start = (char*)sstab + shp[j].sh_offset;
81*58a2b000SEvgeniy Ivanov 		strtab_end = (char*)sstab + shp[j].sh_offset + shp[j].sh_size;
82*58a2b000SEvgeniy Ivanov 		break;
83*58a2b000SEvgeniy Ivanov 	}
84*58a2b000SEvgeniy Ivanov 
85*58a2b000SEvgeniy Ivanov 	if (!symtab_start || !strtab_start)
86*58a2b000SEvgeniy Ivanov 		return NULL;
87*58a2b000SEvgeniy Ivanov 
88*58a2b000SEvgeniy Ivanov 	for (sp = symtab_start; sp < symtab_end; sp++)
89*58a2b000SEvgeniy Ivanov 		if (sp->st_name != 0 &&
90*58a2b000SEvgeniy Ivanov 		    strcmp(strtab_start + sp->st_name, symname) == 0)
91*58a2b000SEvgeniy Ivanov 			return (void*)(uintptr_t)sp->st_value;
92*58a2b000SEvgeniy Ivanov 
93*58a2b000SEvgeniy Ivanov 	return NULL;
94*58a2b000SEvgeniy Ivanov }
95*58a2b000SEvgeniy Ivanov 
96*58a2b000SEvgeniy Ivanov #endif /* (ELFSIZE == 32 && BOOT_ELF32) || (ELFSIZE == 64 && BOOT_ELF64) */
97