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 /* 61 * An SHT_GROUP section is identified by the name of its signature 62 * symbol rather than section name. Although the section names are 63 * often unique, this is not required, and some compilers set it to 64 * a generic name like ".group". 65 */ 66 isd.isd_name = gdp->gd_name; 67 isd.isd_hash = sgs_str_hash(isd.isd_name); 68 69 if ((isdp = avl_find(avlt, &isd, &where)) != NULL) { 70 gdp->gd_oisc = isdp->isd_isp; 71 return (1); 72 } 73 74 /* 75 * This is a new group - so keep it. 76 */ 77 if ((isdp = libld_calloc(sizeof (Isd_node), 1)) == NULL) 78 return (S_ERROR); 79 80 isdp->isd_name = isd.isd_name; 81 isdp->isd_hash = isd.isd_hash; 82 isdp->isd_isp = gdp->gd_isc; 83 84 avl_insert(avlt, isdp, where); 85 return (0); 86 } 87 88 Group_desc * 89 ld_get_group(Ofl_desc *ofl, Is_desc *isp) 90 { 91 Ifl_desc *ifl = isp->is_file; 92 uint_t scnndx = isp->is_scnndx; 93 Group_desc *gdp; 94 Aliste idx; 95 96 /* 97 * Scan the GROUP sections associated with this file to find the 98 * matching group section. 99 */ 100 for (ALIST_TRAVERSE(ifl->ifl_groups, idx, gdp)) { 101 size_t ndx; 102 Word *data; 103 104 if (isp->is_shdr->sh_type == SHT_GROUP) { 105 if (isp->is_scnndx == gdp->gd_isc->is_scnndx) 106 return (gdp); 107 continue; 108 } 109 110 data = gdp->gd_data; 111 for (ndx = 1; ndx < gdp->gd_cnt; ndx++) { 112 if (data[ndx] == scnndx) 113 return (gdp); 114 } 115 } 116 117 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ELF_NOGROUPSECT), 118 ifl->ifl_name, EC_WORD(isp->is_scnndx), isp->is_name); 119 ofl->ofl_flags |= FLG_OF_FATAL; 120 return (NULL); 121 } 122 123 uintptr_t 124 ld_group_process(Is_desc *gisc, Ofl_desc *ofl) 125 { 126 Ifl_desc *gifl = gisc->is_file; 127 Shdr *sshdr, *gshdr = gisc->is_shdr; 128 Is_desc *isc; 129 Sym *sym; 130 const char *str; 131 Group_desc gd; 132 size_t ndx; 133 int gnu_stt_section; 134 135 /* 136 * Confirm that the sh_link points to a valid section. 137 */ 138 if ((gshdr->sh_link == SHN_UNDEF) || 139 (gshdr->sh_link >= gifl->ifl_shnum) || 140 ((isc = gifl->ifl_isdesc[gshdr->sh_link]) == NULL)) { 141 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHLINK), 142 gifl->ifl_name, EC_WORD(gisc->is_scnndx), 143 gisc->is_name, EC_XWORD(gshdr->sh_link)); 144 ofl->ofl_flags |= FLG_OF_FATAL; 145 return (0); 146 } 147 if (gshdr->sh_entsize == 0) { 148 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHENTSIZE), 149 gifl->ifl_name, EC_WORD(gisc->is_scnndx), gisc->is_name, 150 EC_XWORD(gshdr->sh_entsize)); 151 ofl->ofl_flags |= FLG_OF_FATAL; 152 return (0); 153 } 154 155 /* 156 * Get the associated symbol table. Sanity check the sh_info field 157 * (which points to the signature symbol table entry) against the size 158 * of the symbol table. 159 */ 160 sshdr = isc->is_shdr; 161 sym = (Sym *)isc->is_indata->d_buf; 162 163 if ((sshdr->sh_info == SHN_UNDEF) || 164 (gshdr->sh_info >= (Word)(sshdr->sh_size / sshdr->sh_entsize)) || 165 ((isc = gifl->ifl_isdesc[sshdr->sh_link]) == NULL)) { 166 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO), 167 gifl->ifl_name, EC_WORD(gisc->is_scnndx), gisc->is_name, 168 EC_XWORD(gshdr->sh_info)); 169 ofl->ofl_flags |= FLG_OF_FATAL; 170 return (0); 171 } 172 173 sym += gshdr->sh_info; 174 175 /* 176 * Get the symbol name from the associated string table. 177 */ 178 str = (char *)isc->is_indata->d_buf; 179 str += sym->st_name; 180 181 /* 182 * The GNU assembler can use section symbols as the signature symbol 183 * as described by this comment in the gold linker (found via google): 184 * 185 * It seems that some versions of gas will create a section group 186 * associated with a section symbol, and then fail to give a name 187 * to the section symbol. In such a case, use the name of the 188 * section. 189 * 190 * In order to support such objects, we do the same. 191 */ 192 gnu_stt_section = ((sym->st_name == 0) || (*str == '\0')) && 193 (ELF_ST_TYPE(sym->st_info) == STT_SECTION); 194 if (gnu_stt_section) 195 str = gisc->is_name; 196 197 198 /* 199 * Generate a group descriptor. 200 */ 201 gd.gd_isc = gisc; 202 gd.gd_oisc = NULL; 203 gd.gd_name = str; 204 gd.gd_data = gisc->is_indata->d_buf; 205 gd.gd_cnt = gisc->is_indata->d_size / sizeof (Word); 206 207 /* 208 * If this group is a COMDAT group, validate the signature symbol. 209 */ 210 if ((gd.gd_data[0] & GRP_COMDAT) && !gnu_stt_section && 211 ((ELF_ST_BIND(sym->st_info) == STB_LOCAL) || 212 (sym->st_shndx == SHN_UNDEF))) { 213 /* If section symbol, construct a printable name for it */ 214 if (ELF_ST_TYPE(sym->st_info) == STT_SECTION) { 215 const char *stt_sym_name = 216 ld_stt_section_sym_name(gisc); 217 218 if (stt_sym_name != NULL) 219 str = stt_sym_name; 220 } 221 222 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_GRP_INVALSYM), 223 gifl->ifl_name, EC_WORD(gisc->is_scnndx), 224 gisc->is_name, str); 225 ofl->ofl_flags |= FLG_OF_FATAL; 226 return (0); 227 } 228 229 /* 230 * Validate the section indices within the group. If this is a COMDAT 231 * group, mark each section as COMDAT. 232 */ 233 for (ndx = 1; ndx < gd.gd_cnt; ndx++) { 234 Word gndx; 235 236 if ((gndx = gd.gd_data[ndx]) >= gifl->ifl_shnum) { 237 eprintf(ofl->ofl_lml, ERR_FATAL, 238 MSG_INTL(MSG_GRP_INVALNDX), gifl->ifl_name, 239 EC_WORD(gisc->is_scnndx), gisc->is_name, ndx, gndx); 240 ofl->ofl_flags |= FLG_OF_FATAL; 241 return (0); 242 } 243 244 if (gd.gd_data[0] & GRP_COMDAT) 245 gifl->ifl_isdesc[gndx]->is_flags |= FLG_IS_COMDAT; 246 } 247 248 /* 249 * If this is a COMDAT group, determine whether this group has already 250 * been encountered, or whether this is the first instance of the group. 251 */ 252 if ((gd.gd_data[0] & GRP_COMDAT) && 253 (gpavl_loaded(ofl, &gd) == S_ERROR)) 254 return (S_ERROR); 255 256 /* 257 * Associate the group descriptor with this input file. 258 */ 259 if (alist_append(&(gifl->ifl_groups), &gd, sizeof (Group_desc), 260 AL_CNT_IFL_GROUPS) == NULL) 261 return (S_ERROR); 262 263 return (1); 264 } 265