Merge commit '281819e5f8b19cd8627541a22d261906fd190276' into merges
[unleashed.git] / usr / src / cmd / sgs / libld / common / version.c
blob0fd05a8dd73f22f2973c387e5a30c068d965a41a
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <string.h>
27 #include <stdio.h>
28 #include <debug.h>
29 #include "msg.h"
30 #include "_libld.h"
33 * Locate a version descriptor.
35 Ver_desc *
36 ld_vers_find(const char *name, Word hash, APlist *alp)
38 Aliste idx;
39 Ver_desc *vdp;
41 for (APLIST_TRAVERSE(alp, idx, vdp)) {
42 if (vdp->vd_hash != hash)
43 continue;
44 if (strcmp(vdp->vd_name, name) == 0)
45 return (vdp);
47 return (NULL);
51 * Add a new version descriptor to a version descriptor list. Note, users of
52 * this are responsible for determining if the version descriptor already
53 * exists (this can reduce the need to allocate storage for descriptor names
54 * until it is determined a descriptor need be created (see map_symbol())).
56 Ver_desc *
57 ld_vers_desc(const char *name, Word hash, APlist **alpp)
59 Ver_desc *vdp;
61 if ((vdp = libld_calloc(sizeof (Ver_desc), 1)) == NULL)
62 return ((Ver_desc *)S_ERROR);
64 vdp->vd_name = name;
65 vdp->vd_hash = hash;
67 if (aplist_append(alpp, vdp, AL_CNT_VERDESCS) == NULL)
68 return ((Ver_desc *)S_ERROR);
70 return (vdp);
74 * Now that all explict files have been processed validate any version
75 * definitions. Insure that any version references are available (a version
76 * has been defined when it's been assigned an index). Also calculate the
77 * number of .version section entries that will be required to hold this
78 * information.
80 #define _NUM_OF_VERS_ 40 /* twice as big as the depth for libc version */
81 typedef struct {
82 Ver_desc **ver_stk;
83 int ver_sp;
84 int ver_lmt;
85 } Ver_Stack;
87 static uintptr_t
88 vers_visit_children(Ofl_desc *ofl, Ver_desc *vp, int flag)
90 Aliste idx;
91 Ver_desc *vdp;
92 static int err = 0;
93 static Ver_Stack ver_stk = {0, 0, 0};
94 int tmp_sp;
97 * If there was any fatal error,
98 * just return.
100 if (err == S_ERROR)
101 return (err);
104 * if this is called from, ver_check_defs(), initialize sp.
106 if (flag == 0)
107 ver_stk.ver_sp = 0;
110 * Check if passed version pointer vp is already in the stack.
112 for (tmp_sp = 0; tmp_sp < ver_stk.ver_sp; tmp_sp++) {
113 Ver_desc *v;
115 v = ver_stk.ver_stk[tmp_sp];
116 if (v == vp) {
118 * cyclic dependency.
120 if (err == 0) {
121 ld_eprintf(ofl, ERR_FATAL,
122 MSG_INTL(MSG_VER_CYCLIC));
123 err = 1;
125 for (tmp_sp = 0; tmp_sp < ver_stk.ver_sp; tmp_sp++) {
126 v = ver_stk.ver_stk[tmp_sp];
127 if ((v->vd_flags & FLG_VER_CYCLIC) == 0) {
128 v->vd_flags |= FLG_VER_CYCLIC;
129 ld_eprintf(ofl, ERR_NONE,
130 MSG_INTL(MSG_VER_ADDVER),
131 v->vd_name);
134 if ((vp->vd_flags & FLG_VER_CYCLIC) == 0) {
135 vp->vd_flags |= FLG_VER_CYCLIC;
136 ld_eprintf(ofl, ERR_NONE,
137 MSG_INTL(MSG_VER_ADDVER), vp->vd_name);
139 return (err);
144 * Push version on the stack.
146 if (ver_stk.ver_sp >= ver_stk.ver_lmt) {
147 ver_stk.ver_lmt += _NUM_OF_VERS_;
148 if ((ver_stk.ver_stk = (Ver_desc **)
149 libld_realloc((void *)ver_stk.ver_stk,
150 ver_stk.ver_lmt * sizeof (Ver_desc *))) == NULL)
151 return (S_ERROR);
153 ver_stk.ver_stk[(ver_stk.ver_sp)++] = vp;
156 * Now visit children.
158 for (APLIST_TRAVERSE(vp->vd_deps, idx, vdp))
159 if (vers_visit_children(ofl, vdp, 1) == S_ERROR)
160 return (S_ERROR);
163 * Pop version from the stack.
165 (ver_stk.ver_sp)--;
167 return (err);
170 uintptr_t
171 ld_vers_check_defs(Ofl_desc *ofl)
173 Aliste idx1;
174 Ver_desc *vdp;
175 uintptr_t is_cyclic = 0;
177 DBG_CALL(Dbg_ver_def_title(ofl->ofl_lml, ofl->ofl_name));
180 * First check if there are any cyclic dependency
182 for (APLIST_TRAVERSE(ofl->ofl_verdesc, idx1, vdp))
183 if ((is_cyclic = vers_visit_children(ofl, vdp, 0)) == S_ERROR)
184 return (S_ERROR);
186 if (is_cyclic)
187 ofl->ofl_flags |= FLG_OF_FATAL;
189 for (APLIST_TRAVERSE(ofl->ofl_verdesc, idx1, vdp)) {
190 Byte cnt;
191 Sym *sym;
192 Sym_desc *sdp;
193 const char *name = vdp->vd_name;
194 uchar_t bind;
195 Ver_desc *_vdp __unused;
196 avl_index_t where;
197 Aliste idx2;
199 if (vdp->vd_ndx == 0) {
200 ld_eprintf(ofl, ERR_FATAL,
201 MSG_INTL(MSG_VER_UNDEF), name, vdp->vd_ref->vd_name,
202 vdp->vd_ref->vd_file->ifl_name);
203 continue;
206 DBG_CALL(Dbg_ver_desc_entry(ofl->ofl_lml, vdp));
209 * If a version definition contains no symbols this is possibly
210 * a mapfile error.
212 if ((vdp->vd_flags &
213 (VER_FLG_BASE | VER_FLG_WEAK | FLG_VER_REFER)) == 0)
214 DBG_CALL(Dbg_ver_nointerface(ofl->ofl_lml,
215 vdp->vd_name));
218 * Update the version entry count to account for this new
219 * version descriptor (the count is the size in bytes).
221 ofl->ofl_verdefsz += sizeof (Verdef);
224 * Traverse this versions dependency list to determine what
225 * additional version dependencies we must account for against
226 * this descriptor.
228 cnt = 1;
229 for (APLIST_TRAVERSE(vdp->vd_deps, idx2, _vdp)) {
230 cnt++;
232 ofl->ofl_verdefsz += (cnt * sizeof (Verdaux));
235 * Except for the base version descriptor, generate an absolute
236 * symbol to reflect this version.
238 if (vdp->vd_flags & VER_FLG_BASE)
239 continue;
241 if (vdp->vd_flags & VER_FLG_WEAK)
242 bind = STB_WEAK;
243 else
244 bind = STB_GLOBAL;
246 if (sdp = ld_sym_find(name, vdp->vd_hash, &where, ofl)) {
248 * If the symbol already exists and is undefined or was
249 * defined in a shared library, convert it to an
250 * absolute.
252 if ((sdp->sd_sym->st_shndx == SHN_UNDEF) ||
253 (sdp->sd_ref != REF_REL_NEED)) {
254 sdp->sd_shndx = sdp->sd_sym->st_shndx = SHN_ABS;
255 sdp->sd_sym->st_info =
256 ELF_ST_INFO(bind, STT_OBJECT);
257 sdp->sd_ref = REF_REL_NEED;
258 sdp->sd_flags |= (FLG_SY_SPECSEC |
259 FLG_SY_DEFAULT | FLG_SY_EXPDEF);
260 sdp->sd_aux->sa_overndx = vdp->vd_ndx;
263 * If the reference originated from a mapfile
264 * insure we mark the symbol as used.
266 if (sdp->sd_flags & FLG_SY_MAPREF)
267 sdp->sd_flags |= FLG_SY_MAPUSED;
269 } else if ((sdp->sd_flags & FLG_SY_SPECSEC) &&
270 (sdp->sd_sym->st_shndx != SHN_ABS) &&
271 (sdp->sd_ref == REF_REL_NEED)) {
272 ld_eprintf(ofl, ERR_WARNING,
273 MSG_INTL(MSG_VER_DEFINED), name,
274 sdp->sd_file->ifl_name);
276 } else {
278 * If the symbol does not exist create it.
280 if ((sym = libld_calloc(sizeof (Sym), 1)) == NULL)
281 return (S_ERROR);
283 sym->st_shndx = SHN_ABS;
284 sym->st_info = ELF_ST_INFO(bind, STT_OBJECT);
285 DBG_CALL(Dbg_ver_symbol(ofl->ofl_lml, name));
287 if ((sdp = ld_sym_enter(name, sym, vdp->vd_hash,
288 vdp->vd_file, ofl, 0, SHN_ABS,
289 (FLG_SY_SPECSEC | FLG_SY_DEFAULT | FLG_SY_EXPDEF),
290 &where)) == (Sym_desc *)S_ERROR)
291 return (S_ERROR);
293 sdp->sd_ref = REF_REL_NEED;
294 sdp->sd_aux->sa_overndx = vdp->vd_ndx;
297 return (1);
301 * Dereference dependencies as a part of normalizing (allows recursion).
303 static void
304 vers_derefer(Ifl_desc *ifl, Ver_desc *vdp, int weak)
306 Aliste idx;
307 Ver_desc *_vdp;
308 Ver_index *vip = &ifl->ifl_verndx[vdp->vd_ndx];
311 * Set the INFO bit on all dependencies that ld.so.1
312 * can skip verification for. These are the dependencies
313 * that are inherited by others -- verifying the inheriting
314 * version implicitily covers this one.
316 * If the head of the list was a weak then we only mark
317 * weak dependencies, but if the head of the list was 'strong'
318 * we set INFO on all dependencies.
320 if ((weak && (vdp->vd_flags & VER_FLG_WEAK)) || (!weak))
321 vip->vi_flags |= VER_FLG_INFO;
323 for (APLIST_TRAVERSE(vdp->vd_deps, idx, _vdp))
324 vers_derefer(ifl, _vdp, weak);
328 * If we need to record the versions of any needed dependencies traverse the
329 * shared object dependency list and calculate what version needed entries are
330 * required.
332 uintptr_t
333 ld_vers_check_need(Ofl_desc *ofl)
335 Aliste idx1;
336 Ifl_desc *ifl;
337 Half needndx;
338 Str_tbl *strtbl;
342 * Determine which string table is appropriate.
344 strtbl = (OFL_IS_STATIC_OBJ(ofl)) ? ofl->ofl_strtab :
345 ofl->ofl_dynstrtab;
348 * Versym indexes for needed versions start with the next
349 * available version after the final definied version.
350 * However, it can never be less than 2. 0 is always for local
351 * scope, and 1 is always the first global definition.
353 needndx = (ofl->ofl_vercnt > 0) ? (ofl->ofl_vercnt + 1) : 2;
356 * Traverse the shared object list looking for dependencies.
358 for (APLIST_TRAVERSE(ofl->ofl_sos, idx1, ifl)) {
359 Aliste idx2;
360 Ver_index *vip;
361 Ver_desc *vdp;
362 Byte cnt, need = 0;
364 if (!(ifl->ifl_flags & FLG_IF_NEEDED))
365 continue;
367 if (ifl->ifl_vercnt <= VER_NDX_GLOBAL)
368 continue;
371 * Scan the version index list and if any weak version
372 * definition has been referenced by the user promote the
373 * dependency to be non-weak. Weak version dependencies do not
374 * cause fatal errors from the runtime linker, non-weak
375 * dependencies do.
377 for (cnt = 0; cnt <= ifl->ifl_vercnt; cnt++) {
378 vip = &ifl->ifl_verndx[cnt];
379 vdp = vip->vi_desc;
381 if ((vip->vi_flags & (FLG_VER_REFER | VER_FLG_WEAK)) ==
382 (FLG_VER_REFER | VER_FLG_WEAK))
383 vdp->vd_flags &= ~VER_FLG_WEAK;
386 * Mark any weak reference as referred to so as to
387 * simplify normalization and later version dependency
388 * manipulation.
390 if (vip->vi_flags & VER_FLG_WEAK)
391 vip->vi_flags |= FLG_VER_REFER;
395 * Scan the version dependency list to normalize the referenced
396 * dependencies. Any needed version that is inherited by
397 * another like version is dereferenced as it is not necessary
398 * to make this part of the version dependencies.
400 for (APLIST_TRAVERSE(ifl->ifl_verdesc, idx2, vdp)) {
401 Aliste idx3;
402 Ver_desc *_vdp;
403 int type;
405 vip = &ifl->ifl_verndx[vdp->vd_ndx];
407 if (!(vip->vi_flags & FLG_VER_REFER))
408 continue;
410 type = vdp->vd_flags & VER_FLG_WEAK;
411 for (APLIST_TRAVERSE(vdp->vd_deps, idx3, _vdp))
412 vers_derefer(ifl, _vdp, type);
416 * Finally, determine how many of the version dependencies need
417 * to be recorded.
419 for (cnt = 0; cnt <= ifl->ifl_vercnt; cnt++) {
420 vip = &ifl->ifl_verndx[cnt];
423 * If a version has been referenced then record it as a
424 * version dependency.
426 if (vip->vi_flags & FLG_VER_REFER) {
427 /* Assign a VERSYM index for it */
428 vip->vi_overndx = needndx++;
430 ofl->ofl_verneedsz += sizeof (Vernaux);
431 if (st_insert(strtbl, vip->vi_name) == -1)
432 return (S_ERROR);
433 need++;
437 if (need) {
438 ifl->ifl_flags |= FLG_IF_VERNEED;
439 ofl->ofl_verneedsz += sizeof (Verneed);
440 if (st_insert(strtbl, ifl->ifl_soname) == -1)
441 return (S_ERROR);
446 * If no version needed information is required unset the output file
447 * flag.
449 if (ofl->ofl_verneedsz == 0)
450 ofl->ofl_flags &= ~FLG_OF_VERNEED;
452 return (1);
456 * Indicate dependency selection (allows recursion).
458 static void
459 vers_select(Ofl_desc *ofl, Ifl_desc *ifl, Ver_desc *vdp, const char *ref)
461 Aliste idx;
462 Ver_desc *_vdp;
463 Ver_index *vip = &ifl->ifl_verndx[vdp->vd_ndx];
465 vip->vi_flags |= FLG_VER_AVAIL;
466 DBG_CALL(Dbg_ver_avail_entry(ofl->ofl_lml, vip, ref));
468 for (APLIST_TRAVERSE(vdp->vd_deps, idx, _vdp))
469 vers_select(ofl, ifl, _vdp, ref);
472 static Ver_index *
473 vers_index(Ofl_desc *ofl, Ifl_desc *ifl, int avail)
475 Aliste idx1;
476 Ver_desc *vdp;
477 Ver_index *vip;
478 Sdf_desc *sdf = ifl->ifl_sdfdesc;
479 Word count = ifl->ifl_vercnt;
480 Sdv_desc *sdv;
483 * Allocate an index array large enough to hold all of the files
484 * version descriptors.
486 if ((vip = libld_calloc(sizeof (Ver_index), (count + 1))) == NULL)
487 return ((Ver_index *)S_ERROR);
489 for (APLIST_TRAVERSE(ifl->ifl_verdesc, idx1, vdp)) {
490 int ndx = vdp->vd_ndx;
492 vip[ndx].vi_name = vdp->vd_name;
493 vip[ndx].vi_desc = vdp;
496 * Any relocatable object versions, and the `base' version are
497 * always available.
499 if (avail || (vdp->vd_flags & VER_FLG_BASE))
500 vip[ndx].vi_flags |= FLG_VER_AVAIL;
503 * If this is a weak version mark it as such. Weak versions
504 * are always dragged into any version dependencies created,
505 * and if a weak version is referenced it will be promoted to
506 * a non-weak version dependency.
508 if (vdp->vd_flags & VER_FLG_WEAK)
509 vip[ndx].vi_flags |= VER_FLG_WEAK;
511 * If this version is mentioned in a mapfile using ADDVERS
512 * syntax then check to see if it corresponds to an actual
513 * version in the file.
515 if (sdf && (sdf->sdf_flags & FLG_SDF_ADDVER)) {
516 Aliste idx2;
518 for (ALIST_TRAVERSE(sdf->sdf_verneed, idx2, sdv)) {
519 if (strcmp(vip[ndx].vi_name, sdv->sdv_name))
520 continue;
522 vip[ndx].vi_flags |= FLG_VER_REFER;
523 sdv->sdv_flags |= FLG_SDV_MATCHED;
524 break;
530 * if $ADDVER was specified for this object verify that
531 * all of it's dependent upon versions were refered to.
533 if (sdf && (sdf->sdf_flags & FLG_SDF_ADDVER)) {
534 int fail = 0;
536 for (ALIST_TRAVERSE(sdf->sdf_verneed, idx1, sdv)) {
537 if (sdv->sdv_flags & FLG_SDV_MATCHED)
538 continue;
540 if (fail++ == 0) {
541 ld_eprintf(ofl, ERR_NONE,
542 MSG_INTL(MSG_VER_ADDVERS), sdf->sdf_rfile,
543 sdf->sdf_name);
545 ld_eprintf(ofl, ERR_NONE, MSG_INTL(MSG_VER_ADDVER),
546 sdv->sdv_name);
548 if (fail)
549 return ((Ver_index *)S_ERROR);
552 return (vip);
556 * Process a version symbol index section.
559 ld_vers_sym_process(Ofl_desc *ofl, Is_desc *isp, Ifl_desc *ifl)
561 Shdr *symshdr;
562 Shdr *vershdr = isp->is_shdr;
565 * Verify that the versym is the same size as the linked symbol table.
566 * If these two get out of sync the file is considered corrupted.
568 symshdr = ifl->ifl_isdesc[vershdr->sh_link]->is_shdr;
569 if ((symshdr->sh_size / symshdr->sh_entsize) != (vershdr->sh_size /
570 vershdr->sh_entsize)) {
571 Is_desc *sym_isp = ifl->ifl_isdesc[vershdr->sh_link];
573 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_ELF_VERSYM),
574 ifl->ifl_name, EC_WORD(isp->is_scnndx), isp->is_name,
575 EC_WORD(vershdr->sh_size / vershdr->sh_entsize),
576 EC_WORD(sym_isp->is_scnndx), sym_isp->is_name,
577 EC_WORD(symshdr->sh_size / symshdr->sh_entsize));
578 return (1);
580 ifl->ifl_versym = (Versym *)isp->is_indata->d_buf;
581 return (1);
585 * Process a version definition section from an input file. A list of version
586 * descriptors is created and associated with the input files descriptor. If
587 * this is a shared object these descriptors will be used to indicate the
588 * availability of each version. If this is a relocatable object then these
589 * descriptors will be promoted (concatenated) to the output files image.
591 uintptr_t
592 ld_vers_def_process(Is_desc *isp, Ifl_desc *ifl, Ofl_desc *ofl)
594 const char *str, *file = ifl->ifl_name;
595 Sdf_desc *sdf = ifl->ifl_sdfdesc;
596 Sdv_desc *sdv;
597 Word num, _num;
598 Verdef *vdf;
599 int relobj;
602 * If there is no version section then simply indicate that all version
603 * definitions asked for do not exist.
605 if (isp == NULL) {
606 Aliste idx;
608 for (ALIST_TRAVERSE(sdf->sdf_vers, idx, sdv)) {
609 ld_eprintf(ofl, ERR_FATAL,
610 MSG_INTL(MSG_VER_NOEXIST), ifl->ifl_name,
611 sdv->sdv_name, sdv->sdv_ref);
613 return (0);
616 vdf = (Verdef *)isp->is_indata->d_buf;
619 * Verify the version revision. We only check the first version
620 * structure as it is assumed all other version structures in this
621 * data section will be of the same revision.
623 if (vdf->vd_version > VER_DEF_CURRENT)
624 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_VER_HIGHER),
625 ifl->ifl_name, vdf->vd_version, VER_DEF_CURRENT);
628 num = isp->is_shdr->sh_info;
629 str = (char *)ifl->ifl_isdesc[isp->is_shdr->sh_link]->is_indata->d_buf;
631 if (ifl->ifl_ehdr->e_type == ET_REL)
632 relobj = 1;
633 else
634 relobj = 0;
636 DBG_CALL(Dbg_ver_def_title(ofl->ofl_lml, file));
639 * Loop through the version information setting up a version descriptor
640 * for each version definition.
642 for (_num = 1; _num <= num; _num++,
643 vdf = (Verdef *)((uintptr_t)vdf + vdf->vd_next)) {
644 const char *name;
645 Ver_desc *ivdp, *ovdp = NULL;
646 Word hash;
647 Half cnt = vdf->vd_cnt;
648 Half ndx = vdf->vd_ndx;
649 Verdaux *vdap = (Verdaux *)((uintptr_t)vdf +
650 vdf->vd_aux);
653 * Keep track of the largest index for use in creating a
654 * version index array later, and create a version descriptor.
656 if (ndx > ifl->ifl_vercnt)
657 ifl->ifl_vercnt = ndx;
659 name = (char *)(str + vdap->vda_name);
660 /* LINTED */
661 hash = (Word)elf_hash(name);
662 if (((ivdp = ld_vers_find(name, hash,
663 ifl->ifl_verdesc)) == NULL) &&
664 ((ivdp = ld_vers_desc(name, hash,
665 &ifl->ifl_verdesc)) == (Ver_desc *)S_ERROR))
666 return (S_ERROR);
668 ivdp->vd_ndx = ndx;
669 ivdp->vd_file = ifl;
670 ivdp->vd_flags = vdf->vd_flags;
673 * If we're processing a relocatable object then this version
674 * definition needs to be propagated to the output file.
675 * Generate a new output file version and associated this input
676 * version to it. During symbol processing the version index of
677 * the symbol will be promoted from the input file to the output
678 * files version definition.
680 if (relobj) {
681 if (!(ofl->ofl_flags & FLG_OF_RELOBJ))
682 ofl->ofl_flags |= FLG_OF_PROCRED;
684 if ((ivdp->vd_flags & VER_FLG_BASE) == 0) {
686 * If no version descriptors have yet been set
687 * up, initialize a base version to represent
688 * the output file itself. This `base' version
689 * catches any internally generated symbols
690 * (_end, _etext, etc.) and
691 * serves to initialize the output version
692 * descriptor count.
694 if (ofl->ofl_vercnt == 0) {
695 if (ld_vers_base(ofl) ==
696 (Ver_desc *)S_ERROR)
697 return (S_ERROR);
699 ofl->ofl_flags |= FLG_OF_VERDEF;
700 if ((ovdp = ld_vers_find(name, hash,
701 ofl->ofl_verdesc)) == NULL) {
702 if ((ovdp = ld_vers_desc(name, hash,
703 &ofl->ofl_verdesc)) ==
704 (Ver_desc *)S_ERROR)
705 return (S_ERROR);
707 /* LINTED */
708 ovdp->vd_ndx = (Half)++ofl->ofl_vercnt;
709 ovdp->vd_file = ifl;
710 ovdp->vd_flags = vdf->vd_flags;
715 * Maintain the association between the input version
716 * descriptor and the output version descriptor so that
717 * an associated symbols will be assigned to the
718 * correct version.
720 ivdp->vd_ref = ovdp;
724 * Process any dependencies this version may have.
726 vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next);
727 for (cnt--; cnt; cnt--,
728 vdap = (Verdaux *)((uintptr_t)vdap + vdap->vda_next)) {
729 Ver_desc *_ivdp;
731 name = (char *)(str + vdap->vda_name);
732 /* LINTED */
733 hash = (Word)elf_hash(name);
735 if (((_ivdp = ld_vers_find(name, hash,
736 ifl->ifl_verdesc)) == NULL) &&
737 ((_ivdp = ld_vers_desc(name, hash,
738 &ifl->ifl_verdesc)) == (Ver_desc *)S_ERROR))
739 return (S_ERROR);
741 if (aplist_append(&ivdp->vd_deps, _ivdp,
742 AL_CNT_VERDESCS) == NULL)
743 return (S_ERROR);
745 DBG_CALL(Dbg_ver_desc_entry(ofl->ofl_lml, ivdp));
749 * Now that we know the total number of version definitions for this
750 * file, build an index array for fast access when processing symbols.
752 if ((ifl->ifl_verndx =
753 vers_index(ofl, ifl, relobj)) == (Ver_index *)S_ERROR)
754 return (S_ERROR);
756 if (relobj)
757 return (1);
760 * If this object has version control definitions against it then these
761 * must be processed so as to select those version definitions to which
762 * symbol bindings can occur. Otherwise simply mark all versions as
763 * available.
765 DBG_CALL(Dbg_ver_avail_title(ofl->ofl_lml, file));
767 if (sdf && (sdf->sdf_flags & FLG_SDF_SELECT)) {
768 Aliste idx1;
770 for (ALIST_TRAVERSE(sdf->sdf_vers, idx1, sdv)) {
771 Aliste idx2;
772 Ver_desc *vdp;
773 int found = 0;
775 for (APLIST_TRAVERSE(ifl->ifl_verdesc, idx2, vdp)) {
776 if (strcmp(sdv->sdv_name, vdp->vd_name) == 0) {
777 found++;
778 break;
781 if (found)
782 vers_select(ofl, ifl, vdp, sdv->sdv_ref);
783 else
784 ld_eprintf(ofl, ERR_FATAL,
785 MSG_INTL(MSG_VER_NOEXIST), ifl->ifl_name,
786 sdv->sdv_name, sdv->sdv_ref);
788 } else {
789 Ver_index *vip;
790 int cnt;
792 for (cnt = VER_NDX_GLOBAL; cnt <= ifl->ifl_vercnt; cnt++) {
793 vip = &ifl->ifl_verndx[cnt];
794 vip->vi_flags |= FLG_VER_AVAIL;
795 DBG_CALL(Dbg_ver_avail_entry(ofl->ofl_lml, vip, 0));
800 * If this is an explict dependency indicate that this file is a
801 * candidate for requiring version needed information to be recorded in
802 * the image we're creating.
804 if (ifl->ifl_flags & FLG_IF_NEEDED)
805 ofl->ofl_flags |= FLG_OF_VERNEED;
807 return (1);
811 * Process a version needed section.
813 uintptr_t
814 ld_vers_need_process(Is_desc *isp, Ifl_desc *ifl, Ofl_desc *ofl)
816 const char *str, *file = ifl->ifl_name;
817 Word num, _num;
818 Verneed *vnd;
820 vnd = (Verneed *)isp->is_indata->d_buf;
823 * Verify the version revision. We only check the first version
824 * structure as it is assumed all other version structures in this
825 * data section will be of the same revision.
827 if (vnd->vn_version > VER_DEF_CURRENT) {
828 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_VER_HIGHER),
829 ifl->ifl_name, vnd->vn_version, VER_DEF_CURRENT);
832 num = isp->is_shdr->sh_info;
833 str = (char *)ifl->ifl_isdesc[isp->is_shdr->sh_link]->is_indata->d_buf;
835 DBG_CALL(Dbg_ver_need_title(ofl->ofl_lml, file));
838 * Loop through the version information setting up a version descriptor
839 * for each version definition.
841 for (_num = 1; _num <= num; _num++,
842 vnd = (Verneed *)((uintptr_t)vnd + vnd->vn_next)) {
843 Sdf_desc *sdf;
844 const char *name;
845 Half cnt = vnd->vn_cnt;
846 Vernaux *vnap = (Vernaux *)((uintptr_t)vnd +
847 vnd->vn_aux);
848 Half _cnt;
850 name = (char *)(str + vnd->vn_file);
853 * Set up a shared object descriptor and add to it the necessary
854 * needed versions. This information may also have been added
855 * by a mapfile (see map_dash()).
857 if ((sdf = sdf_find(name, ofl->ofl_soneed)) == NULL) {
858 if ((sdf = sdf_add(name, &ofl->ofl_soneed)) ==
859 (Sdf_desc *)S_ERROR)
860 return (S_ERROR);
861 sdf->sdf_rfile = file;
862 sdf->sdf_flags |= FLG_SDF_VERIFY;
865 for (_cnt = 0; cnt; _cnt++, cnt--,
866 vnap = (Vernaux *)((uintptr_t)vnap + vnap->vna_next)) {
867 Sdv_desc sdv;
869 sdv.sdv_name = str + vnap->vna_name;
870 sdv.sdv_ref = file;
871 sdv.sdv_flags = 0;
873 if (alist_append(&sdf->sdf_vers, &sdv,
874 sizeof (Sdv_desc), AL_CNT_SDF_VERSIONS) == NULL)
875 return (S_ERROR);
877 DBG_CALL(Dbg_ver_need_entry(ofl->ofl_lml, _cnt, name,
878 sdv.sdv_name));
881 return (1);
885 * If a symbol is obtained from a versioned relocatable object then the symbols
886 * version association must be promoted to the version definition as it will be
887 * represented in the output file.
889 void
890 ld_vers_promote(Sym_desc *sdp, Word ndx, Ifl_desc *ifl, Ofl_desc *ofl)
892 Half vndx;
895 * A version symbol index of 0 implies the symbol is local. A value of
896 * VER_NDX_GLOBAL implies the symbol is global but has not been
897 * assigned to a specfic version definition.
899 vndx = ifl->ifl_versym[ndx];
900 if (vndx == 0) {
901 sdp->sd_flags |= (FLG_SY_REDUCED | FLG_SY_HIDDEN);
902 return;
905 if (vndx == VER_NDX_ELIMINATE) {
906 sdp->sd_flags |= (FLG_SY_REDUCED | FLG_SY_HIDDEN | FLG_SY_ELIM);
907 return;
910 if (vndx == VER_NDX_GLOBAL) {
911 if (!SYM_IS_HIDDEN(sdp))
912 sdp->sd_flags |= (FLG_SY_DEFAULT | FLG_SY_EXPDEF);
913 if (sdp->sd_aux->sa_overndx <= VER_NDX_GLOBAL)
914 sdp->sd_aux->sa_overndx = VER_NDX_GLOBAL;
915 return;
919 * Any other version index requires association to the appropriate
920 * version definition.
922 if ((ifl->ifl_verndx == 0) || (vndx > ifl->ifl_vercnt)) {
923 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_VER_INVALNDX),
924 sdp->sd_name, ifl->ifl_name, vndx);
925 return;
928 if (!SYM_IS_HIDDEN(sdp))
929 sdp->sd_flags |= (FLG_SY_DEFAULT | FLG_SY_EXPDEF);
932 * Promote the symbols version index to the appropriate output version
933 * definition.
935 if (!(sdp->sd_flags & FLG_SY_VERSPROM)) {
936 Ver_index *vip;
938 vip = &ifl->ifl_verndx[vndx];
939 sdp->sd_aux->sa_overndx = vip->vi_desc->vd_ref->vd_ndx;
940 sdp->sd_flags |= FLG_SY_VERSPROM;
945 * If any versioning is called for make sure an initial version descriptor is
946 * assigned to represent the file itself. Known as the base version.
948 Ver_desc *
949 ld_vers_base(Ofl_desc *ofl)
951 Ver_desc *vdp;
952 const char *name;
955 * Determine the filename to associate to the version descriptor. This
956 * is either the SONAME (if one has been supplied) or the basename of
957 * the output file.
959 if ((name = ofl->ofl_soname) == NULL) {
960 const char *str = ofl->ofl_name;
962 while (*str != '\0') {
963 if (*str++ == '/')
964 name = str;
966 if (name == NULL)
967 name = ofl->ofl_name;
971 * Generate the version descriptor.
973 /* LINTED */
974 if ((vdp = ld_vers_desc(name, (Word)elf_hash(name),
975 &ofl->ofl_verdesc)) == (Ver_desc *)S_ERROR)
976 return ((Ver_desc *)S_ERROR);
979 * Assign the base index to this version and initialize the output file
980 * descriptor with the number of version descriptors presently in use.
982 vdp->vd_ndx = ofl->ofl_vercnt = VER_NDX_GLOBAL;
983 vdp->vd_flags |= VER_FLG_BASE;
985 return (vdp);
989 * Now that all input shared objects have been processed, verify that all
990 * version requirements have been met. Any version control requirements will
991 * have been specified by the user (and placed on the ofl_oscntl list) and are
992 * verified at the time the object was processed (see ver_def_process()).
993 * Here we process all version requirements established from shared objects
994 * themselves (ie,. NEEDED dependencies).
997 ld_vers_verify(Ofl_desc *ofl)
999 Aliste idx1;
1000 Sdf_desc *sdf;
1001 char *nv;
1004 * As with the runtime environment, disable all version verification if
1005 * requested.
1007 #if defined(_ELF64)
1008 if ((nv = getenv(MSG_ORIG(MSG_LD_NOVERSION_64))) == NULL)
1009 #else
1010 if ((nv = getenv(MSG_ORIG(MSG_LD_NOVERSION_32))) == NULL)
1011 #endif
1012 nv = getenv(MSG_ORIG(MSG_LD_NOVERSION));
1014 if (nv && nv[0])
1015 return (1);
1017 for (APLIST_TRAVERSE(ofl->ofl_soneed, idx1, sdf)) {
1018 Aliste idx2;
1019 Sdv_desc *sdv;
1020 Ifl_desc *ifl = sdf->sdf_file;
1022 if (!(sdf->sdf_flags & FLG_SDF_VERIFY))
1023 continue;
1026 * If this file contains no version definitions then ignore
1027 * any versioning verification. This is the same model as
1028 * carried out by ld.so.1 and is intended to allow backward
1029 * compatibility should a shared object with a version
1030 * requirement be returned to an older system on which a
1031 * non-versioned shared object exists.
1033 if ((ifl == NULL) || (ifl->ifl_verdesc == NULL))
1034 continue;
1037 * If individual versions were specified for this file make
1038 * sure that they actually exist in the appropriate file, and
1039 * that they are available for binding.
1041 for (ALIST_TRAVERSE(sdf->sdf_vers, idx2, sdv)) {
1042 Aliste idx3;
1043 Ver_desc *vdp;
1044 int found = 0;
1046 for (APLIST_TRAVERSE(ifl->ifl_verdesc, idx3, vdp)) {
1047 if (strcmp(sdv->sdv_name, vdp->vd_name) == 0) {
1048 found++;
1049 break;
1052 if (found) {
1053 Ver_index *vip;
1055 vip = &ifl->ifl_verndx[vdp->vd_ndx];
1056 if (!(vip->vi_flags & FLG_VER_AVAIL)) {
1057 ld_eprintf(ofl, ERR_FATAL,
1058 MSG_INTL(MSG_VER_UNAVAIL),
1059 ifl->ifl_name, sdv->sdv_name,
1060 sdv->sdv_ref);
1062 } else {
1063 ld_eprintf(ofl, ERR_FATAL,
1064 MSG_INTL(MSG_VER_NOEXIST), ifl->ifl_name,
1065 sdv->sdv_name, sdv->sdv_ref);
1069 return (1);