xref: /illumos-gate/usr/src/cmd/sgs/librtld/common/syms.c (revision 9b9d39d2a32ff806d2431dbcc50968ef1e6d46b2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Update the symbol table entries:
27  *
28  *  o	If addr is non-zero then every symbol entry is updated to indicate the
29  *	new location to which the object will be mapped.
30  *
31  *  o	The address of the `_edata' and `_end' symbols, and their associated
32  *	section, is updated to reflect any new heap addition.
33  */
34 
35 #include	<libelf.h>
36 #include	<string.h>
37 #include	"sgs.h"
38 #include	"machdep.h"
39 #include	"msg.h"
40 #include	"_librtld.h"
41 
42 void
43 update_sym(Cache *cache, Cache *_cache, Addr edata, Half endx, Addr addr)
44 {
45 	char	*strs;
46 	Sym	*syms;
47 	Shdr	*shdr;
48 	Xword	symn, cnt;
49 
50 	/*
51 	 * Set up to read the symbol table and its associated string table.
52 	 */
53 	shdr = _cache->c_shdr;
54 	syms = (Sym *)_cache->c_data->d_buf;
55 	symn = shdr->sh_size / shdr->sh_entsize;
56 
57 	strs = (char *)cache[shdr->sh_link].c_data->d_buf;
58 
59 	/*
60 	 * Loop through the symbol table looking for `_end' and `_edata'.
61 	 */
62 	for (cnt = 0; cnt < symn; cnt++, syms++) {
63 		char	*name = strs + syms->st_name;
64 
65 		if (addr) {
66 			if (syms->st_value)
67 				syms->st_value += addr;
68 		}
69 
70 		if ((name[0] != '_') || (name[1] != 'e'))
71 			continue;
72 		if (strcmp(name, MSG_ORIG(MSG_SYM_END)) &&
73 		    strcmp(name, MSG_ORIG(MSG_SYM_EDATA)))
74 			continue;
75 
76 		syms->st_value = edata + addr;
77 		if (endx)
78 			syms->st_shndx = endx;
79 	}
80 }
81 
82 int
83 syminfo(Cache *_cache, Alist **nodirect)
84 {
85 	Syminfo	*info;
86 	Shdr	*shdr;
87 	Word	num, ndx;
88 
89 	shdr = _cache->c_shdr;
90 	info = (Syminfo *)_cache->c_data->d_buf;
91 	num = (Word)(shdr->sh_size / shdr->sh_entsize);
92 
93 	/*
94 	 * Traverse the syminfo section recording the index of all nodirect
95 	 * symbols.
96 	 */
97 	for (ndx = 1, info++; ndx < num; ndx++, info++) {
98 		if ((info->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0)
99 			continue;
100 
101 		if (alist_append(nodirect, &ndx, sizeof (Word), 20) == 0)
102 			return (1);
103 	}
104 	return (0);
105 }
106