xref: /illumos-gate/usr/src/cmd/sgs/librtld/common/syms.c (revision 1f5207b7604fb44407eb4342aff613f7c4508508)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 #include	<libelf.h>
37 #include	<string.h>
38 #include	"sgs.h"
39 #include	"machdep.h"
40 #include	"msg.h"
41 #include	"_librtld.h"
42 
43 void
44 update_sym(Cache *cache, Cache *_cache, Addr edata, Half endx, Addr addr)
45 {
46 	char	*strs;
47 	Sym	*syms;
48 	Shdr	*shdr;
49 	Xword	symn, cnt;
50 
51 	/*
52 	 * Set up to read the symbol table and its associated string table.
53 	 */
54 	shdr = _cache->c_shdr;
55 	syms = (Sym *)_cache->c_data->d_buf;
56 	symn = shdr->sh_size / shdr->sh_entsize;
57 
58 	strs = (char *)cache[shdr->sh_link].c_data->d_buf;
59 
60 	/*
61 	 * Loop through the symbol table looking for `_end' and `_edata'.
62 	 */
63 	for (cnt = 0; cnt < symn; cnt++, syms++) {
64 		char	*name = strs + syms->st_name;
65 
66 		if (addr) {
67 			if (syms->st_value)
68 				syms->st_value += addr;
69 		}
70 
71 		if ((name[0] != '_') || (name[1] != 'e'))
72 			continue;
73 		if (strcmp(name, MSG_ORIG(MSG_SYM_END)) &&
74 		    strcmp(name, MSG_ORIG(MSG_SYM_EDATA)))
75 			continue;
76 
77 		syms->st_value = edata + addr;
78 		if (endx)
79 			syms->st_shndx = endx;
80 	}
81 }
82 
83 int
84 syminfo(Cache *_cache, Alist **nodirect)
85 {
86 	Syminfo	*info;
87 	Shdr	*shdr;
88 	Word	num, ndx;
89 
90 	shdr = _cache->c_shdr;
91 	info = (Syminfo *)_cache->c_data->d_buf;
92 	num = (Word)(shdr->sh_size / shdr->sh_entsize);
93 
94 	/*
95 	 * Traverse the syminfo section recording the index of all nodirect
96 	 * symbols.
97 	 */
98 	for (ndx = 1, info++; ndx < num; ndx++, info++) {
99 		if ((info->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0)
100 			continue;
101 
102 		if (alist_append(nodirect, &ndx, sizeof (Word), 20) == 0)
103 			return (1);
104 	}
105 	return (0);
106 }
107