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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <stdio.h> 30 #include <string.h> 31 #include <link.h> 32 #include "debug.h" 33 #include "msg.h" 34 #include "_libld.h" 35 36 /* 37 * Define an AVL node for maintain group names, together with a compare function 38 * for the Grp_node AVL tree. 39 */ 40 typedef struct { 41 const char *gn_name; /* group name */ 42 avl_node_t gn_avl; /* avl book-keeping (see SGSOFFSETOF) */ 43 uint_t gn_hash; /* group name hash value */ 44 } Grp_node; 45 46 static int 47 gnavl_compare(const void * n1, const void * n2) 48 { 49 uint_t hash1, hash2; 50 const char *st1, *st2; 51 int rc; 52 53 hash1 = ((Grp_node *)n1)->gn_hash; 54 hash2 = ((Grp_node *)n2)->gn_hash; 55 56 if (hash1 > hash2) 57 return (1); 58 if (hash1 < hash2) 59 return (-1); 60 61 st1 = ((Grp_node *)n1)->gn_name; 62 st2 = ((Grp_node *)n2)->gn_name; 63 64 rc = strcmp(st1, st2); 65 if (rc > 0) 66 return (1); 67 if (rc < 0) 68 return (-1); 69 return (0); 70 } 71 72 /* 73 * Determine whether a (COMDAT) group has already been encountered. If so, 74 * tag the new group having the same name as discardable. 75 */ 76 uintptr_t 77 gpavl_loaded(Ofl_desc *ofl, Group_desc * gdp) 78 { 79 Grp_node gpn, *gpnp; 80 avl_tree_t *avlt; 81 avl_index_t where; 82 83 /* 84 * Create an avl tree if required. 85 */ 86 if ((avlt = ofl->ofl_groups) == 0) { 87 if ((avlt = calloc(sizeof (avl_tree_t), 1)) == NULL) 88 return (S_ERROR); 89 avl_create(avlt, gnavl_compare, sizeof (Grp_node), 90 SGSOFFSETOF(Grp_node, gn_avl)); 91 ofl->ofl_groups = avlt; 92 } 93 94 gpn.gn_name = gdp->gd_symname; 95 gpn.gn_hash = sgs_str_hash(gdp->gd_symname); 96 97 if ((gpnp = avl_find(avlt, &gpn, &where)) != NULL) { 98 gdp->gd_flags |= GRP_FLG_DISCARD; 99 return (1); 100 } 101 102 /* 103 * This is a new group, save it. 104 */ 105 if ((gpnp = calloc(sizeof (Grp_node), 1)) == NULL) 106 return (S_ERROR); 107 108 gpnp->gn_name = gpn.gn_name; 109 gpnp->gn_hash = gpn.gn_hash; 110 111 avl_insert(avlt, gpnp, where); 112 return (0); 113 } 114 115 Group_desc * 116 get_group(Ofl_desc *ofl, Is_desc *isp) 117 { 118 Ifl_desc *ifl = isp->is_file; 119 Elf *elf = ifl->ifl_elf; 120 uint_t scnndx = isp->is_scnndx; 121 Group_desc *gdp; 122 Aliste off; 123 124 /* 125 * If this is the first SHF_GROUP section encountered for this file, 126 * establish what group sections exist. 127 */ 128 if (ifl->ifl_groups == 0) { 129 Elf_Scn *scn = 0; 130 131 while (scn = elf_nextscn(elf, scn)) { 132 Shdr *shdr, *_shdr; 133 Sym *sym; 134 Elf_Scn *_scn; 135 Elf_Data *data; 136 Group_desc gd; 137 138 shdr = elf_getshdr(scn); 139 if (shdr->sh_type != SHT_GROUP) 140 continue; 141 142 /* 143 * Confirm that the sh_link points to a valid section. 144 */ 145 if ((shdr->sh_link == SHN_UNDEF) || 146 (shdr->sh_link >= ifl->ifl_shnum)) { 147 eprintf(ERR_FATAL, MSG_INTL(MSG_FIL_INVSHLINK), 148 ifl->ifl_name, elf_strptr(elf, 149 ifl->ifl_shstrndx, shdr->sh_name), 150 EC_XWORD(shdr->sh_link)); 151 ofl->ofl_flags |= FLG_OF_FATAL; 152 continue; 153 } 154 155 if (shdr->sh_entsize == 0) { 156 eprintf(ERR_FATAL, 157 MSG_INTL(MSG_FIL_INVSHENTSIZE), 158 ifl->ifl_name, elf_strptr(elf, 159 ifl->ifl_shstrndx, shdr->sh_name), 160 EC_XWORD(shdr->sh_entsize)); 161 ofl->ofl_flags |= FLG_OF_FATAL; 162 continue; 163 } 164 165 /* 166 * Get associated symbol table. 167 */ 168 _scn = elf_getscn(elf, shdr->sh_link); 169 _shdr = elf_getshdr(_scn); 170 171 /* 172 * Sanity check the sh_link field (which points to 173 * a symbol table entry) against the size of the 174 * symbol table. 175 */ 176 if ((shdr->sh_info == SHN_UNDEF) || 177 (shdr->sh_info >= (Word)(_shdr->sh_size / 178 _shdr->sh_entsize))) { 179 eprintf(ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO), 180 ifl->ifl_name, elf_strptr(elf, 181 ifl->ifl_shstrndx, shdr->sh_name), 182 EC_XWORD(shdr->sh_info)); 183 ofl->ofl_flags |= FLG_OF_FATAL; 184 continue; 185 } 186 187 data = elf_getdata(_scn, 0); 188 sym = data->d_buf; 189 sym += shdr->sh_info; 190 data = elf_getdata(scn, 0); 191 192 gd.gd_gsectname = 193 elf_strptr(elf, ifl->ifl_shstrndx, shdr->sh_name); 194 gd.gd_symname = 195 elf_strptr(elf, _shdr->sh_link, sym->st_name); 196 gd.gd_scnndx = elf_ndxscn(scn); 197 gd.gd_data = data->d_buf; 198 gd.gd_cnt = data->d_size / sizeof (Word); 199 gd.gd_flags = 0; 200 201 /* 202 * If this group is a COMDAT group, determine whether 203 * this 'signature' symbol has already been detected. 204 */ 205 if ((gd.gd_data[0] & GRP_COMDAT) && 206 (ELF_ST_BIND(sym->st_info) != STB_LOCAL) && 207 (sym->st_shndx != SHN_UNDEF) && 208 (gpavl_loaded(ofl, &gd) == S_ERROR)) 209 return ((Group_desc *)S_ERROR); 210 211 if (alist_append(&(ifl->ifl_groups), 212 &gd, sizeof (Group_desc), AL_CNT_GROUP) == 0) 213 return ((Group_desc *)S_ERROR); 214 } 215 } 216 217 /* 218 * Scan the GROUP sections associated with this file to find the 219 * matching group section. 220 */ 221 for (ALIST_TRAVERSE(ifl->ifl_groups, off, gdp)) { 222 size_t ndx; 223 Word * data; 224 225 if (isp->is_shdr->sh_type == SHT_GROUP) { 226 if (isp->is_scnndx == gdp->gd_scnndx) 227 return (gdp); 228 continue; 229 } 230 231 data = gdp->gd_data; 232 for (ndx = 1; ndx < gdp->gd_cnt; ndx++) { 233 if (data[ndx] == scnndx) 234 return (gdp); 235 } 236 } 237 238 eprintf(ERR_FATAL, MSG_INTL(MSG_ELF_NOGROUPSECT), ifl->ifl_name, 239 isp->is_name); 240 ofl->ofl_flags |= FLG_OF_FATAL; 241 return (0); 242 } 243