17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55aefb655Srie * Common Development and Distribution License (the "License").
65aefb655Srie * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
215aefb655Srie
227c478bd9Sstevel@tonic-gate /*
23bf994817SAli Bahrami * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include <link.h>
295aefb655Srie #include <debug.h>
307c478bd9Sstevel@tonic-gate #include "msg.h"
317c478bd9Sstevel@tonic-gate #include "_libld.h"
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate /*
34cc7efc4fSrie * Determine whether a (COMDAT) group has already been encountered. If so,
350e233487SRod Evans * indicate that the group descriptor has an overriding group (gd_oisc). This
360e233487SRod Evans * indication triggers the ld_place_section() to discard this group, while the
370e233487SRod Evans * gd_oisc information provides for complete diagnostics of the override.
380e233487SRod Evans * Otherwise, this is the first occurrence of this group, therefore the group
390e233487SRod Evans * descriptor is saved for future comparisons.
40cc7efc4fSrie */
415aefb655Srie static uintptr_t
gpavl_loaded(Ofl_desc * ofl,Group_desc * gdp)42cc7efc4fSrie gpavl_loaded(Ofl_desc *ofl, Group_desc *gdp)
43cc7efc4fSrie {
446b3ba5bdSAli Bahrami Isd_node isd, *isdp;
45cc7efc4fSrie avl_tree_t *avlt;
46cc7efc4fSrie avl_index_t where;
47cc7efc4fSrie
48cc7efc4fSrie /*
496b3ba5bdSAli Bahrami * Create a groups avl tree if required.
50cc7efc4fSrie */
516b3ba5bdSAli Bahrami if ((avlt = ofl->ofl_groups) == NULL) {
52fb12490aSRichard Lowe if ((avlt = libld_calloc(1, sizeof (avl_tree_t))) == NULL)
53cc7efc4fSrie return (S_ERROR);
546b3ba5bdSAli Bahrami avl_create(avlt, isdavl_compare, sizeof (Isd_node),
556b3ba5bdSAli Bahrami SGSOFFSETOF(Isd_node, isd_avl));
56cc7efc4fSrie ofl->ofl_groups = avlt;
57cc7efc4fSrie }
58cc7efc4fSrie
59e64d0ff9SAli Bahrami /*
60e64d0ff9SAli Bahrami * An SHT_GROUP section is identified by the name of its signature
61e64d0ff9SAli Bahrami * symbol rather than section name. Although the section names are
62e64d0ff9SAli Bahrami * often unique, this is not required, and some compilers set it to
63e64d0ff9SAli Bahrami * a generic name like ".group".
64e64d0ff9SAli Bahrami */
65e64d0ff9SAli Bahrami isd.isd_name = gdp->gd_name;
66e64d0ff9SAli Bahrami isd.isd_hash = sgs_str_hash(isd.isd_name);
67cc7efc4fSrie
686b3ba5bdSAli Bahrami if ((isdp = avl_find(avlt, &isd, &where)) != NULL) {
696b3ba5bdSAli Bahrami gdp->gd_oisc = isdp->isd_isp;
70cc7efc4fSrie return (1);
71cc7efc4fSrie }
72cc7efc4fSrie
73cc7efc4fSrie /*
746b3ba5bdSAli Bahrami * This is a new group - so keep it.
75cc7efc4fSrie */
76fb12490aSRichard Lowe if ((isdp = libld_calloc(1, sizeof (Isd_node))) == NULL)
77cc7efc4fSrie return (S_ERROR);
78cc7efc4fSrie
79e64d0ff9SAli Bahrami isdp->isd_name = isd.isd_name;
806b3ba5bdSAli Bahrami isdp->isd_hash = isd.isd_hash;
81e64d0ff9SAli Bahrami isdp->isd_isp = gdp->gd_isc;
82cc7efc4fSrie
836b3ba5bdSAli Bahrami avl_insert(avlt, isdp, where);
84cc7efc4fSrie return (0);
85cc7efc4fSrie }
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate Group_desc *
ld_get_group(Ofl_desc * ofl,Is_desc * isp)885aefb655Srie ld_get_group(Ofl_desc *ofl, Is_desc *isp)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate Ifl_desc *ifl = isp->is_file;
917c478bd9Sstevel@tonic-gate uint_t scnndx = isp->is_scnndx;
92cc7efc4fSrie Group_desc *gdp;
93cce0e03bSab196087 Aliste idx;
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate /*
96cc7efc4fSrie * Scan the GROUP sections associated with this file to find the
97cc7efc4fSrie * matching group section.
987c478bd9Sstevel@tonic-gate */
99cce0e03bSab196087 for (ALIST_TRAVERSE(ifl->ifl_groups, idx, gdp)) {
100cc7efc4fSrie size_t ndx;
101cc7efc4fSrie Word *data;
102cc7efc4fSrie
1037c478bd9Sstevel@tonic-gate if (isp->is_shdr->sh_type == SHT_GROUP) {
1040e233487SRod Evans if (isp->is_scnndx == gdp->gd_isc->is_scnndx)
105cc7efc4fSrie return (gdp);
1067c478bd9Sstevel@tonic-gate continue;
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
109cc7efc4fSrie data = gdp->gd_data;
110cc7efc4fSrie for (ndx = 1; ndx < gdp->gd_cnt; ndx++) {
111cc7efc4fSrie if (data[ndx] == scnndx)
112cc7efc4fSrie return (gdp);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate }
115cc7efc4fSrie
1161007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ELF_NOGROUPSECT),
1174a8d0ea7SAli Bahrami ifl->ifl_name, EC_WORD(isp->is_scnndx), isp->is_name);
1180e233487SRod Evans return (NULL);
1190e233487SRod Evans }
1200e233487SRod Evans
121ef16f6b5SRichard Lowe /*
122ef16f6b5SRichard Lowe * When creating a .debug_macro section, in an attempt to make certain DWARF
123ef16f6b5SRichard Lowe * macro information shareable, the GNU compiler must construct group sections
124ef16f6b5SRichard Lowe * with a repeatable signature symbol while nevertheless having no actual
125ef16f6b5SRichard Lowe * symbol to refer to (because it relates to macros).
126ef16f6b5SRichard Lowe *
127ef16f6b5SRichard Lowe * We use this as yet another way to clue ourselves in that sloppy relocation
128ef16f6b5SRichard Lowe * will likely be required.
129ef16f6b5SRichard Lowe *
130ef16f6b5SRichard Lowe * The format of these gensym'd names is:
131ef16f6b5SRichard Lowe * wm<offset size>.<encoded path name>.<lineno>.<32byte hash>
132ef16f6b5SRichard Lowe * Where the encoded file name may be absent.
133ef16f6b5SRichard Lowe */
134ef16f6b5SRichard Lowe static boolean_t
is_header_gensym(const char * name)135ef16f6b5SRichard Lowe is_header_gensym(const char *name)
136ef16f6b5SRichard Lowe {
137ef16f6b5SRichard Lowe const char *c = NULL;
138ef16f6b5SRichard Lowe size_t len = strlen(name);
139ef16f6b5SRichard Lowe
140ef16f6b5SRichard Lowe /* No room for leader, hash, and periods */
141ef16f6b5SRichard Lowe if (len < 37)
142ef16f6b5SRichard Lowe return (B_FALSE);
143ef16f6b5SRichard Lowe
144ef16f6b5SRichard Lowe if ((strncmp(name, "wm4.", 4) != 0) &&
145ef16f6b5SRichard Lowe strncmp(name, "wm8.", 4) != 0)
146ef16f6b5SRichard Lowe return (B_FALSE);
147ef16f6b5SRichard Lowe
148ef16f6b5SRichard Lowe c = &name[len - 33];
149ef16f6b5SRichard Lowe if (*c++ != '.')
150ef16f6b5SRichard Lowe return (B_FALSE);
151ef16f6b5SRichard Lowe
152ef16f6b5SRichard Lowe for (; *c != '\0'; c++) {
153ef16f6b5SRichard Lowe if (!(((*c >= 'a') && (*c <= 'f')) ||
154ef16f6b5SRichard Lowe ((*c >= '0') && (*c <= '9')))) {
155ef16f6b5SRichard Lowe return (B_FALSE);
156ef16f6b5SRichard Lowe }
157ef16f6b5SRichard Lowe }
158ef16f6b5SRichard Lowe
159ef16f6b5SRichard Lowe return (B_TRUE);
160ef16f6b5SRichard Lowe }
161ef16f6b5SRichard Lowe
1620e233487SRod Evans uintptr_t
ld_group_process(Is_desc * gisc,Ofl_desc * ofl)1630e233487SRod Evans ld_group_process(Is_desc *gisc, Ofl_desc *ofl)
1640e233487SRod Evans {
1650e233487SRod Evans Ifl_desc *gifl = gisc->is_file;
1660e233487SRod Evans Shdr *sshdr, *gshdr = gisc->is_shdr;
167*d227ea68SRichard Lowe Word *new_data = NULL;
168*d227ea68SRichard Lowe Shdr *new_shdr = NULL;
1690e233487SRod Evans Is_desc *isc;
1700e233487SRod Evans Sym *sym;
1714a8d0ea7SAli Bahrami const char *str;
1720e233487SRod Evans Group_desc gd;
1730e233487SRod Evans size_t ndx;
174e64d0ff9SAli Bahrami int gnu_stt_section;
1750e233487SRod Evans
1760e233487SRod Evans /*
1770e233487SRod Evans * Confirm that the sh_link points to a valid section.
1780e233487SRod Evans */
1790e233487SRod Evans if ((gshdr->sh_link == SHN_UNDEF) ||
1800e233487SRod Evans (gshdr->sh_link >= gifl->ifl_shnum) ||
1810e233487SRod Evans ((isc = gifl->ifl_isdesc[gshdr->sh_link]) == NULL)) {
1821007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHLINK),
1834a8d0ea7SAli Bahrami gifl->ifl_name, EC_WORD(gisc->is_scnndx),
1844a8d0ea7SAli Bahrami gisc->is_name, EC_XWORD(gshdr->sh_link));
185cc7efc4fSrie return (0);
1867c478bd9Sstevel@tonic-gate }
1870e233487SRod Evans if (gshdr->sh_entsize == 0) {
1881007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHENTSIZE),
1894a8d0ea7SAli Bahrami gifl->ifl_name, EC_WORD(gisc->is_scnndx), gisc->is_name,
1904a8d0ea7SAli Bahrami EC_XWORD(gshdr->sh_entsize));
1910e233487SRod Evans return (0);
1920e233487SRod Evans }
1930e233487SRod Evans
1940e233487SRod Evans /*
1950e233487SRod Evans * Get the associated symbol table. Sanity check the sh_info field
1960e233487SRod Evans * (which points to the signature symbol table entry) against the size
1970e233487SRod Evans * of the symbol table.
1980e233487SRod Evans */
1990e233487SRod Evans sshdr = isc->is_shdr;
2000e233487SRod Evans sym = (Sym *)isc->is_indata->d_buf;
2010e233487SRod Evans
2020e233487SRod Evans if ((sshdr->sh_info == SHN_UNDEF) ||
2030e233487SRod Evans (gshdr->sh_info >= (Word)(sshdr->sh_size / sshdr->sh_entsize)) ||
2040e233487SRod Evans ((isc = gifl->ifl_isdesc[sshdr->sh_link]) == NULL)) {
2051007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
2064a8d0ea7SAli Bahrami gifl->ifl_name, EC_WORD(gisc->is_scnndx), gisc->is_name,
2074a8d0ea7SAli Bahrami EC_XWORD(gshdr->sh_info));
2080e233487SRod Evans return (0);
2090e233487SRod Evans }
2100e233487SRod Evans
2110e233487SRod Evans sym += gshdr->sh_info;
2120e233487SRod Evans
2130e233487SRod Evans /*
2140e233487SRod Evans * Get the symbol name from the associated string table.
2150e233487SRod Evans */
2160e233487SRod Evans str = (char *)isc->is_indata->d_buf;
2170e233487SRod Evans str += sym->st_name;
2180e233487SRod Evans
2190e233487SRod Evans /*
220e64d0ff9SAli Bahrami * The GNU assembler can use section symbols as the signature symbol
221e64d0ff9SAli Bahrami * as described by this comment in the gold linker (found via google):
222e64d0ff9SAli Bahrami *
223e64d0ff9SAli Bahrami * It seems that some versions of gas will create a section group
224e64d0ff9SAli Bahrami * associated with a section symbol, and then fail to give a name
225e64d0ff9SAli Bahrami * to the section symbol. In such a case, use the name of the
226e64d0ff9SAli Bahrami * section.
227e64d0ff9SAli Bahrami *
228e64d0ff9SAli Bahrami * In order to support such objects, we do the same.
229e64d0ff9SAli Bahrami */
230e64d0ff9SAli Bahrami gnu_stt_section = ((sym->st_name == 0) || (*str == '\0')) &&
231e64d0ff9SAli Bahrami (ELF_ST_TYPE(sym->st_info) == STT_SECTION);
232e64d0ff9SAli Bahrami if (gnu_stt_section)
233e64d0ff9SAli Bahrami str = gisc->is_name;
234e64d0ff9SAli Bahrami
235e64d0ff9SAli Bahrami
236e64d0ff9SAli Bahrami /*
2370e233487SRod Evans * Generate a group descriptor.
2380e233487SRod Evans */
2390e233487SRod Evans gd.gd_isc = gisc;
2400e233487SRod Evans gd.gd_oisc = NULL;
2410e233487SRod Evans gd.gd_name = str;
2420e233487SRod Evans gd.gd_data = gisc->is_indata->d_buf;
2430e233487SRod Evans gd.gd_cnt = gisc->is_indata->d_size / sizeof (Word);
2440e233487SRod Evans
2450e233487SRod Evans /*
246ef16f6b5SRichard Lowe * If the signature symbol is a name generated by the GNU compiler to
247ef16f6b5SRichard Lowe * refer to a header, we need sloppy relocation.
248ef16f6b5SRichard Lowe */
249ef16f6b5SRichard Lowe if (is_header_gensym(str)) {
250ef16f6b5SRichard Lowe if ((ofl->ofl_flags1 & FLG_OF1_NRLXREL) == 0)
251ef16f6b5SRichard Lowe ofl->ofl_flags1 |= FLG_OF1_RLXREL;
252ef16f6b5SRichard Lowe DBG_CALL(Dbg_sec_gnu_comdat(ofl->ofl_lml, gisc, TRUE,
253ef16f6b5SRichard Lowe (ofl->ofl_flags1 & FLG_OF1_RLXREL) != 0));
254ef16f6b5SRichard Lowe }
255ef16f6b5SRichard Lowe
256ef16f6b5SRichard Lowe /*
2570e233487SRod Evans * Validate the section indices within the group. If this is a COMDAT
2580e233487SRod Evans * group, mark each section as COMDAT.
2590e233487SRod Evans */
260*d227ea68SRichard Lowe for (ndx = (gd.gd_cnt - 1); ndx >= 1; ndx--) {
261a196c3ffSRichard Lowe Word gndx = gd.gd_data[ndx];
2620e233487SRod Evans
263a196c3ffSRichard Lowe if ((gndx == 0) || (gndx >= gifl->ifl_shnum)) {
2641007fd6fSAli Bahrami ld_eprintf(ofl, ERR_FATAL,
2650e233487SRod Evans MSG_INTL(MSG_GRP_INVALNDX), gifl->ifl_name,
2664a8d0ea7SAli Bahrami EC_WORD(gisc->is_scnndx), gisc->is_name, ndx, gndx);
2670e233487SRod Evans return (0);
2680e233487SRod Evans }
2690e233487SRod Evans
270*d227ea68SRichard Lowe /*
271*d227ea68SRichard Lowe * If we reach here the group was valid but has been damaged
272*d227ea68SRichard Lowe * by FLG_OF_STRIP. That is, this section in the group exists
273*d227ea68SRichard Lowe * but was ignored during input processing.
274*d227ea68SRichard Lowe *
275*d227ea68SRichard Lowe * If this entry is NULL, remove it from the group. You might
276*d227ea68SRichard Lowe * think we can make consuming code careful to be aware that
277*d227ea68SRichard Lowe * entries in a group may not exist, but if we are linking a
278*d227ea68SRichard Lowe * relocatable object (-r -s), the group must survive, and a
279*d227ea68SRichard Lowe * bogus entry must not be written out.
280*d227ea68SRichard Lowe */
281*d227ea68SRichard Lowe if (gifl->ifl_isdesc[gndx] == NULL) {
282*d227ea68SRichard Lowe /*
283*d227ea68SRichard Lowe * We need to allocate new data for the group and the
284*d227ea68SRichard Lowe * shdr, the existing data is mapped from the file.
285*d227ea68SRichard Lowe */
286*d227ea68SRichard Lowe if (new_data == NULL) {
287*d227ea68SRichard Lowe if ((new_data = libld_calloc(sizeof (Word),
288*d227ea68SRichard Lowe gd.gd_cnt)) == NULL)
289*d227ea68SRichard Lowe return (S_ERROR);
290*d227ea68SRichard Lowe
291*d227ea68SRichard Lowe /*
292*d227ea68SRichard Lowe * Copy up the whole thing, we'll shrink it in
293*d227ea68SRichard Lowe * a moment.
294*d227ea68SRichard Lowe */
295*d227ea68SRichard Lowe memcpy(new_data, gd.gd_data,
296*d227ea68SRichard Lowe sizeof (Word) * gd.gd_cnt);
297*d227ea68SRichard Lowe gisc->is_indata->d_buf = gd.gd_data = new_data;
298*d227ea68SRichard Lowe
299*d227ea68SRichard Lowe new_shdr = libld_malloc(sizeof (Shdr));
300*d227ea68SRichard Lowe if (new_shdr == NULL)
301*d227ea68SRichard Lowe return (S_ERROR);
302*d227ea68SRichard Lowe
303*d227ea68SRichard Lowe memcpy(new_shdr, gisc->is_shdr, sizeof (Shdr));
304*d227ea68SRichard Lowe gisc->is_shdr = new_shdr;
305*d227ea68SRichard Lowe }
306*d227ea68SRichard Lowe
307*d227ea68SRichard Lowe /* If there're entries after us, copy them down */
308*d227ea68SRichard Lowe if (ndx < (gd.gd_cnt - 1)) {
309*d227ea68SRichard Lowe memmove(&gd.gd_data[ndx], &gd.gd_data[ndx + 1],
310*d227ea68SRichard Lowe (gd.gd_cnt - (ndx + 1)) * sizeof (Word));
311*d227ea68SRichard Lowe }
312*d227ea68SRichard Lowe
313*d227ea68SRichard Lowe gisc->is_indata->d_size -= sizeof (Word);
314*d227ea68SRichard Lowe gisc->is_shdr->sh_size -= sizeof (Word);
315*d227ea68SRichard Lowe gd.gd_cnt -= 1;
316*d227ea68SRichard Lowe continue;
317*d227ea68SRichard Lowe }
318*d227ea68SRichard Lowe
3190e233487SRod Evans if (gd.gd_data[0] & GRP_COMDAT)
3200e233487SRod Evans gifl->ifl_isdesc[gndx]->is_flags |= FLG_IS_COMDAT;
3210e233487SRod Evans }
3220e233487SRod Evans
3230e233487SRod Evans /*
324*d227ea68SRichard Lowe * If we're left with only one item in the group -- the header --
325*d227ea68SRichard Lowe * discard it.
326*d227ea68SRichard Lowe */
327*d227ea68SRichard Lowe if (gd.gd_cnt == 1)
328*d227ea68SRichard Lowe gisc->is_flags |= FLG_IS_DISCARD;
329*d227ea68SRichard Lowe
330*d227ea68SRichard Lowe /*
3310e233487SRod Evans * If this is a COMDAT group, determine whether this group has already
3320e233487SRod Evans * been encountered, or whether this is the first instance of the group.
3330e233487SRod Evans */
3340e233487SRod Evans if ((gd.gd_data[0] & GRP_COMDAT) &&
3350e233487SRod Evans (gpavl_loaded(ofl, &gd) == S_ERROR))
3360e233487SRod Evans return (S_ERROR);
3370e233487SRod Evans
3380e233487SRod Evans /*
3390e233487SRod Evans * Associate the group descriptor with this input file.
3400e233487SRod Evans */
3410e233487SRod Evans if (alist_append(&(gifl->ifl_groups), &gd, sizeof (Group_desc),
3420e233487SRod Evans AL_CNT_IFL_GROUPS) == NULL)
3430e233487SRod Evans return (S_ERROR);
3440e233487SRod Evans
3450e233487SRod Evans return (1);
3460e233487SRod Evans }
347