1255033Semaste/*	$NetBSD: symtab.c,v 1.2 2013/08/29 15:01:57 christos Exp $	*/
2254661Semaste
3254661Semaste/*-
4254661Semaste * Copyright (c) 2012 The NetBSD Foundation, Inc.
5254661Semaste * All rights reserved.
6254661Semaste *
7254661Semaste * This code is derived from software contributed to The NetBSD Foundation
8254661Semaste * by Christos Zoulas.
9254661Semaste *
10254661Semaste * Redistribution and use in source and binary forms, with or without
11254661Semaste * modification, are permitted provided that the following conditions
12254661Semaste * are met:
13254661Semaste * 1. Redistributions of source code must retain the above copyright
14254661Semaste *    notice, this list of conditions and the following disclaimer.
15254661Semaste * 2. Redistributions in binary form must reproduce the above copyright
16254661Semaste *    notice, this list of conditions and the following disclaimer in the
17254661Semaste *    documentation and/or other materials provided with the distribution.
18254661Semaste *
19254661Semaste * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20254661Semaste * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21254661Semaste * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22254661Semaste * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23254661Semaste * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24254661Semaste * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25254661Semaste * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26254661Semaste * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27254661Semaste * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28254661Semaste * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29254661Semaste * POSSIBILITY OF SUCH DAMAGE.
30254661Semaste */
31254661Semaste#include <sys/cdefs.h>
32255033Semaste__RCSID("$NetBSD: symtab.c,v 1.2 2013/08/29 15:01:57 christos Exp $");
33254661Semaste
34254661Semaste#include <stdlib.h>
35254661Semaste#include <stdio.h>
36255033Semaste#include <string.h>
37254689Semaste#include <stdint.h>
38254661Semaste#include <err.h>
39254661Semaste#include <dlfcn.h>
40254661Semaste
41254661Semaste#include <libelf.h>
42254661Semaste#include <gelf.h>
43254661Semaste#ifndef ELF_ST_BIND
44254661Semaste#define ELF_ST_BIND(x)          ((x) >> 4)
45254661Semaste#endif
46254661Semaste#ifndef ELF_ST_TYPE
47254661Semaste#define ELF_ST_TYPE(x)          (((unsigned int)x) & 0xf)
48254661Semaste#endif
49254661Semaste
50254661Semaste
51254661Semaste#include "symtab.h"
52254661Semaste
53254661Semastestruct symbol {
54254661Semaste	char *st_name;
55254661Semaste	uintptr_t st_value;
56254661Semaste	uintptr_t st_info;
57254661Semaste};
58254661Semaste
59254661Semastestruct symtab {
60254661Semaste	size_t nsymbols;
61254661Semaste	struct symbol *symbols;
62254661Semaste};
63254661Semaste
64254661Semastestatic int
65254661Semasteaddress_compare(const void *a, const void *b)
66254661Semaste{
67254661Semaste	const struct symbol *sa = a;
68254661Semaste	const struct symbol *sb = b;
69254661Semaste	return (int)(intmax_t)(sa->st_value - sb->st_value);
70254661Semaste}
71254661Semaste
72254661Semastevoid
73254661Semastesymtab_destroy(symtab_t *s)
74254661Semaste{
75254661Semaste	if (s == NULL)
76254661Semaste		return;
77254661Semaste	for (size_t i = 0; i < s->nsymbols; i++)
78254661Semaste		free(s->symbols[i].st_name);
79254661Semaste	free(s->symbols);
80254661Semaste	free(s);
81254661Semaste}
82254661Semaste
83254661Semastesymtab_t *
84254661Semastesymtab_create(int fd, int bind, int type)
85254661Semaste{
86254661Semaste	Elf *elf;
87254661Semaste	symtab_t *st;
88254661Semaste	Elf_Scn *scn = NULL;
89254661Semaste
90254661Semaste	if (elf_version(EV_CURRENT) == EV_NONE) {
91254661Semaste		warnx("Elf Library is out of date.");
92254661Semaste		return NULL;
93254661Semaste	}
94254661Semaste
95254661Semaste	elf = elf_begin(fd, ELF_C_READ, NULL);
96254661Semaste	if (elf == NULL) {
97254661Semaste		warnx("Error opening elf file: %s", elf_errmsg(elf_errno()));
98254661Semaste		return NULL;
99254661Semaste	}
100254661Semaste	st = calloc(1, sizeof(*st));
101254661Semaste	if (st == NULL) {
102254661Semaste		warnx("Error allocating symbol table");
103254661Semaste		elf_end(elf);
104254661Semaste		return NULL;
105254661Semaste	}
106254661Semaste
107254661Semaste	while ((scn = elf_nextscn(elf, scn)) != NULL) {
108254661Semaste		GElf_Shdr shdr;
109254661Semaste		Elf_Data *edata;
110254661Semaste		size_t ns;
111254661Semaste		struct symbol *s;
112254661Semaste
113254661Semaste		gelf_getshdr(scn, &shdr);
114254661Semaste		if(shdr.sh_type != SHT_SYMTAB)
115254661Semaste			continue;
116254661Semaste
117254661Semaste		edata = elf_getdata(scn, NULL);
118254661Semaste		ns = shdr.sh_size / shdr.sh_entsize;
119254661Semaste		s = calloc(ns, sizeof(*s));
120254661Semaste		if (s == NULL) {
121254661Semaste			warn("Cannot allocate %zu symbols", ns);
122254661Semaste			goto out;
123254661Semaste		}
124254661Semaste		st->symbols = s;
125254661Semaste
126254661Semaste		for (size_t i = 0; i < ns; i++) {
127254661Semaste			GElf_Sym sym;
128254661Semaste                        gelf_getsym(edata, (int)i, &sym);
129254661Semaste
130254661Semaste			if (bind != -1 &&
131254661Semaste			    (unsigned)bind != ELF_ST_BIND(sym.st_info))
132254661Semaste				continue;
133254661Semaste
134254661Semaste			if (type != -1 &&
135254661Semaste			    (unsigned)type != ELF_ST_TYPE(sym.st_info))
136254661Semaste				continue;
137254661Semaste
138254661Semaste			s->st_value = sym.st_value;
139254661Semaste			s->st_info = sym.st_info;
140254661Semaste			s->st_name = strdup(
141254661Semaste			    elf_strptr(elf, shdr.sh_link, sym.st_name));
142254661Semaste			if (s->st_name == NULL)
143254661Semaste				goto out;
144254661Semaste			s++;
145254661Semaste                }
146254661Semaste		st->nsymbols = s - st->symbols;
147254661Semaste		if (st->nsymbols == 0) {
148254661Semaste			warnx("No symbols found");
149254661Semaste			goto out;
150254661Semaste		}
151254661Semaste		qsort(st->symbols, st->nsymbols, sizeof(*st->symbols),
152254661Semaste		    address_compare);
153254661Semaste		elf_end(elf);
154254661Semaste		return st;
155254661Semaste	}
156254661Semasteout:
157254661Semaste	symtab_destroy(st);
158254661Semaste	elf_end(elf);
159254661Semaste	return NULL;
160254661Semaste}
161254661Semaste
162254661Semaste
163254661Semasteint
164254661Semastesymtab_find(const symtab_t *st, const void *p, Dl_info *dli)
165254661Semaste{
166254661Semaste	struct symbol *s = st->symbols;
167254661Semaste	size_t ns = st->nsymbols;
168254661Semaste	size_t hi = ns;
169254661Semaste	size_t lo = 0;
170254661Semaste	size_t mid = ns / 2;
171254661Semaste	uintptr_t dd, sd, me = (uintptr_t)p;
172254661Semaste
173254661Semaste	for (;;) {
174254661Semaste		if (s[mid].st_value < me)
175254661Semaste			lo = mid;
176254661Semaste		else if (s[mid].st_value > me)
177254661Semaste			hi = mid;
178254661Semaste		else
179254661Semaste			break;
180254661Semaste		if (hi - lo == 1) {
181254661Semaste			mid = lo;
182254661Semaste			break;
183254661Semaste		}
184254661Semaste		mid = (hi + lo) / 2;
185254661Semaste	}
186254661Semaste	dd = me - (uintptr_t)dli->dli_saddr;
187254661Semaste	sd = me - s[mid].st_value;
188254661Semaste	if (dd > sd) {
189254661Semaste		dli->dli_saddr = (void *)s[mid].st_value;
190254661Semaste		dli->dli_sname = s[mid].st_name;
191254661Semaste	}
192254661Semaste	return 1;
193254661Semaste}
194