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 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include <stdio.h> 28 #include <string.h> 29 #include <link.h> 30 #include <debug.h> 31 #include "msg.h" 32 #include "_libld.h" 33 34 /* 35 * Determine whether a (COMDAT) group has already been encountered. If so, 36 * indicate that the group descriptor has an overriding group (gd_oisc). This 37 * indication triggers the ld_place_section() to discard this group, while the 38 * gd_oisc information provides for complete diagnostics of the override. 39 * Otherwise, this is the first occurrence of this group, therefore the group 40 * descriptor is saved for future comparisons. 41 */ 42 static uintptr_t 43 gpavl_loaded(Ofl_desc *ofl, Group_desc *gdp) 44 { 45 Isd_node isd, *isdp; 46 avl_tree_t *avlt; 47 avl_index_t where; 48 49 /* 50 * Create a groups avl tree if required. 51 */ 52 if ((avlt = ofl->ofl_groups) == NULL) { 53 if ((avlt = libld_calloc(sizeof (avl_tree_t), 1)) == NULL) 54 return (S_ERROR); 55 avl_create(avlt, isdavl_compare, sizeof (Isd_node), 56 SGSOFFSETOF(Isd_node, isd_avl)); 57 ofl->ofl_groups = avlt; 58 } 59 60 isd.isd_hash = sgs_str_hash(gdp->gd_isc->is_name); 61 isd.isd_isp = gdp->gd_isc; 62 63 if ((isdp = avl_find(avlt, &isd, &where)) != NULL) { 64 gdp->gd_oisc = isdp->isd_isp; 65 return (1); 66 } 67 68 /* 69 * This is a new group - so keep it. 70 */ 71 if ((isdp = libld_calloc(sizeof (Isd_node), 1)) == NULL) 72 return (S_ERROR); 73 74 isdp->isd_hash = isd.isd_hash; 75 isdp->isd_isp = isd.isd_isp; 76 77 avl_insert(avlt, isdp, where); 78 return (0); 79 } 80 81 Group_desc * 82 ld_get_group(Ofl_desc *ofl, Is_desc *isp) 83 { 84 Ifl_desc *ifl = isp->is_file; 85 uint_t scnndx = isp->is_scnndx; 86 Group_desc *gdp; 87 Aliste idx; 88 89 /* 90 * Scan the GROUP sections associated with this file to find the 91 * matching group section. 92 */ 93 for (ALIST_TRAVERSE(ifl->ifl_groups, idx, gdp)) { 94 size_t ndx; 95 Word *data; 96 97 if (isp->is_shdr->sh_type == SHT_GROUP) { 98 if (isp->is_scnndx == gdp->gd_isc->is_scnndx) 99 return (gdp); 100 continue; 101 } 102 103 data = gdp->gd_data; 104 for (ndx = 1; ndx < gdp->gd_cnt; ndx++) { 105 if (data[ndx] == scnndx) 106 return (gdp); 107 } 108 } 109 110 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ELF_NOGROUPSECT), 111 ifl->ifl_name, isp->is_name); 112 ofl->ofl_flags |= FLG_OF_FATAL; 113 return (NULL); 114 } 115 116 uintptr_t 117 ld_group_process(Is_desc *gisc, Ofl_desc *ofl) 118 { 119 Ifl_desc *gifl = gisc->is_file; 120 Shdr *sshdr, *gshdr = gisc->is_shdr; 121 Is_desc *isc; 122 Sym *sym; 123 char *str; 124 Group_desc gd; 125 size_t ndx; 126 127 /* 128 * Confirm that the sh_link points to a valid section. 129 */ 130 if ((gshdr->sh_link == SHN_UNDEF) || 131 (gshdr->sh_link >= gifl->ifl_shnum) || 132 ((isc = gifl->ifl_isdesc[gshdr->sh_link]) == NULL)) { 133 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHLINK), 134 gifl->ifl_name, gisc->is_name, EC_XWORD(gshdr->sh_link)); 135 ofl->ofl_flags |= FLG_OF_FATAL; 136 return (0); 137 } 138 if (gshdr->sh_entsize == 0) { 139 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHENTSIZE), 140 gifl->ifl_name, gisc->is_name, EC_XWORD(gshdr->sh_entsize)); 141 ofl->ofl_flags |= FLG_OF_FATAL; 142 return (0); 143 } 144 145 /* 146 * Get the associated symbol table. Sanity check the sh_info field 147 * (which points to the signature symbol table entry) against the size 148 * of the symbol table. 149 */ 150 sshdr = isc->is_shdr; 151 sym = (Sym *)isc->is_indata->d_buf; 152 153 if ((sshdr->sh_info == SHN_UNDEF) || 154 (gshdr->sh_info >= (Word)(sshdr->sh_size / sshdr->sh_entsize)) || 155 ((isc = gifl->ifl_isdesc[sshdr->sh_link]) == NULL)) { 156 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO), 157 gifl->ifl_name, gisc->is_name, EC_XWORD(gshdr->sh_info)); 158 ofl->ofl_flags |= FLG_OF_FATAL; 159 return (0); 160 } 161 162 sym += gshdr->sh_info; 163 164 /* 165 * Get the symbol name from the associated string table. 166 */ 167 str = (char *)isc->is_indata->d_buf; 168 str += sym->st_name; 169 170 /* 171 * Generate a group descriptor. 172 */ 173 gd.gd_isc = gisc; 174 gd.gd_oisc = NULL; 175 gd.gd_name = str; 176 gd.gd_data = gisc->is_indata->d_buf; 177 gd.gd_cnt = gisc->is_indata->d_size / sizeof (Word); 178 179 /* 180 * If this group is a COMDAT group, validate the signature symbol. 181 */ 182 if ((gd.gd_data[0] & GRP_COMDAT) && 183 ((ELF_ST_BIND(sym->st_info) == STB_LOCAL) || 184 (sym->st_shndx == SHN_UNDEF))) { 185 /* FATAL or ignore? */ 186 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_GRP_INVALSYM), 187 gifl->ifl_name, gisc->is_name, str); 188 ofl->ofl_flags |= FLG_OF_FATAL; 189 return (0); 190 } 191 192 /* 193 * Validate the section indices within the group. If this is a COMDAT 194 * group, mark each section as COMDAT. 195 */ 196 for (ndx = 1; ndx < gd.gd_cnt; ndx++) { 197 Word gndx; 198 199 if ((gndx = gd.gd_data[ndx]) >= gifl->ifl_shnum) { 200 eprintf(ofl->ofl_lml, ERR_FATAL, 201 MSG_INTL(MSG_GRP_INVALNDX), gifl->ifl_name, 202 gisc->is_name, ndx, gndx); 203 ofl->ofl_flags |= FLG_OF_FATAL; 204 return (0); 205 } 206 207 if (gd.gd_data[0] & GRP_COMDAT) 208 gifl->ifl_isdesc[gndx]->is_flags |= FLG_IS_COMDAT; 209 } 210 211 /* 212 * If this is a COMDAT group, determine whether this group has already 213 * been encountered, or whether this is the first instance of the group. 214 */ 215 if ((gd.gd_data[0] & GRP_COMDAT) && 216 (gpavl_loaded(ofl, &gd) == S_ERROR)) 217 return (S_ERROR); 218 219 /* 220 * Associate the group descriptor with this input file. 221 */ 222 if (alist_append(&(gifl->ifl_groups), &gd, sizeof (Group_desc), 223 AL_CNT_IFL_GROUPS) == NULL) 224 return (S_ERROR); 225 226 return (1); 227 } 228