dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / sgs / libld / common / files.c
blob6c1139f5287c7cc7eaf0cd5c9cdd2e419aed7fa4
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) 1988 AT&T
24 * All Rights Reserved
26 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
27 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
31 * Processing of relocatable objects and shared objects.
34 #define ELF_TARGET_AMD64
35 #define ELF_TARGET_SPARC
37 #include <stdio.h>
38 #include <string.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <link.h>
42 #include <limits.h>
43 #include <sys/stat.h>
44 #include <sys/systeminfo.h>
45 #include <debug.h>
46 #include <msg.h>
47 #include <_libld.h>
50 * Decide if we can link against this input file.
52 static int
53 ifl_verify(Ehdr *ehdr, Ofl_desc *ofl, Rej_desc *rej)
56 * Check the validity of the elf header information for compatibility
57 * with this machine and our own internal elf library.
59 if ((ehdr->e_machine != ld_targ.t_m.m_mach) &&
60 ((ehdr->e_machine != ld_targ.t_m.m_machplus) &&
61 ((ehdr->e_flags & ld_targ.t_m.m_flagsplus) == 0))) {
62 rej->rej_type = SGS_REJ_MACH;
63 rej->rej_info = (uint_t)ehdr->e_machine;
64 return (0);
66 if (ehdr->e_ident[EI_DATA] != ld_targ.t_m.m_data) {
67 rej->rej_type = SGS_REJ_DATA;
68 rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA];
69 return (0);
71 if (ehdr->e_version > ofl->ofl_dehdr->e_version) {
72 rej->rej_type = SGS_REJ_VERSION;
73 rej->rej_info = (uint_t)ehdr->e_version;
74 return (0);
76 return (1);
80 * Check sanity of file header and allocate an infile descriptor
81 * for the file being processed.
83 static Ifl_desc *
84 ifl_setup(const char *name, Ehdr *ehdr, Elf *elf, Word flags, Ofl_desc *ofl,
85 Rej_desc *rej)
87 Ifl_desc *ifl;
88 Rej_desc _rej = { 0 };
90 if (ifl_verify(ehdr, ofl, &_rej) == 0) {
91 _rej.rej_name = name;
92 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
93 ld_targ.t_m.m_mach));
94 if (rej->rej_type == 0) {
95 *rej = _rej;
96 rej->rej_name = strdup(_rej.rej_name);
98 return (0);
101 if ((ifl = libld_calloc(1, sizeof (Ifl_desc))) == NULL)
102 return ((Ifl_desc *)S_ERROR);
103 ifl->ifl_name = name;
104 ifl->ifl_ehdr = ehdr;
105 ifl->ifl_elf = elf;
106 ifl->ifl_flags = flags;
109 * Is this file using 'extended Section Indexes'. If so, use the
110 * e_shnum & e_shstrndx which can be found at:
112 * e_shnum == Shdr[0].sh_size
113 * e_shstrndx == Shdr[0].sh_link
115 if ((ehdr->e_shnum == 0) && (ehdr->e_shoff != 0)) {
116 Elf_Scn *scn;
117 Shdr *shdr0;
119 if ((scn = elf_getscn(elf, 0)) == NULL) {
120 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
121 name);
122 return ((Ifl_desc *)S_ERROR);
124 if ((shdr0 = elf_getshdr(scn)) == NULL) {
125 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
126 name);
127 return ((Ifl_desc *)S_ERROR);
129 ifl->ifl_shnum = (Word)shdr0->sh_size;
130 if (ehdr->e_shstrndx == SHN_XINDEX)
131 ifl->ifl_shstrndx = shdr0->sh_link;
132 else
133 ifl->ifl_shstrndx = ehdr->e_shstrndx;
134 } else {
135 ifl->ifl_shnum = ehdr->e_shnum;
136 ifl->ifl_shstrndx = ehdr->e_shstrndx;
139 if ((ifl->ifl_isdesc = libld_calloc(ifl->ifl_shnum,
140 sizeof (Is_desc *))) == NULL)
141 return ((Ifl_desc *)S_ERROR);
144 * Record this new input file on the shared object or relocatable
145 * object input file list.
147 if (ifl->ifl_ehdr->e_type == ET_DYN) {
148 if (aplist_append(&ofl->ofl_sos, ifl, AL_CNT_OFL_LIBS) == NULL)
149 return ((Ifl_desc *)S_ERROR);
150 } else {
151 if (aplist_append(&ofl->ofl_objs, ifl, AL_CNT_OFL_OBJS) == NULL)
152 return ((Ifl_desc *)S_ERROR);
155 return (ifl);
159 * Process a generic section. The appropriate section information is added
160 * to the files input descriptor list.
162 static uintptr_t
163 process_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
164 Word ndx, int ident, Ofl_desc *ofl)
166 Is_desc *isp;
169 * Create a new input section descriptor. If this is a NOBITS
170 * section elf_getdata() will still create a data buffer (the buffer
171 * will be null and the size will reflect the actual memory size).
173 if ((isp = libld_calloc(sizeof (Is_desc), 1)) == NULL)
174 return (S_ERROR);
175 isp->is_shdr = shdr;
176 isp->is_file = ifl;
177 isp->is_name = name;
178 isp->is_scnndx = ndx;
179 isp->is_flags = FLG_IS_EXTERNAL;
180 isp->is_keyident = ident;
182 if ((isp->is_indata = elf_getdata(scn, NULL)) == NULL) {
183 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETDATA),
184 ifl->ifl_name);
185 return (0);
188 if ((shdr->sh_flags & SHF_EXCLUDE) &&
189 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
190 isp->is_flags |= FLG_IS_DISCARD;
194 * Add the new input section to the files input section list and
195 * flag whether the section needs placing in an output section. This
196 * placement is deferred until all input section processing has been
197 * completed, as SHT_GROUP sections can provide information that will
198 * affect how other sections within the file should be placed.
200 ifl->ifl_isdesc[ndx] = isp;
202 if (ident) {
203 if (shdr->sh_flags & ALL_SHF_ORDER) {
204 isp->is_flags |= FLG_IS_ORDERED;
205 ifl->ifl_flags |= FLG_IF_ORDERED;
207 isp->is_flags |= FLG_IS_PLACE;
209 return (1);
213 * Determine the software capabilities of the object being built from the
214 * capabilities of the input relocatable objects. One software capability
215 * is presently recognized, and represented with the following (sys/elf.h):
217 * SF1_SUNW_FPKNWN use/non-use of frame pointer is known, and
218 * SF1_SUNW_FPUSED the frame pointer is in use.
220 * The resolution of the present fame pointer state, and the capabilities
221 * provided by a new input relocatable object are:
223 * new input relocatable object
225 * present | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | <unknown>
226 * state | SF1_SUNW_FPUSED | |
227 * ---------------------------------------------------------------------------
228 * SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN
229 * SF1_SUNW_FPUSED | SF1_SUNW_FPUSED | | SF1_SUNW_FPUSED
230 * ---------------------------------------------------------------------------
231 * SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN
232 * | | |
233 * ---------------------------------------------------------------------------
234 * <unknown> | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | <unknown>
235 * | SF1_SUNW_FPUSED | |
237 static void
238 sf1_cap(Ofl_desc *ofl, Xword val, Ifl_desc *ifl, Is_desc *cisp)
240 #define FP_FLAGS (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)
242 Xword badval;
245 * If a mapfile has established definitions to override any object
246 * capabilities, ignore any new object capabilities.
248 if (ofl->ofl_flags1 & FLG_OF1_OVSFCAP1) {
249 DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
250 CA_SUNW_SF_1, val, ld_targ.t_m.m_mach));
251 return;
254 #if !defined(_ELF64)
255 if (ifl && (ifl->ifl_ehdr->e_type == ET_REL)) {
257 * The SF1_SUNW_ADDR32 is only meaningful when building a 64-bit
258 * object. Warn the user, and remove the setting, if we're
259 * building a 32-bit object.
261 if (val & SF1_SUNW_ADDR32) {
262 ld_eprintf(ofl, ERR_WARNING,
263 MSG_INTL(MSG_FIL_INADDR32SF1), ifl->ifl_name,
264 EC_WORD(cisp->is_scnndx), cisp->is_name);
265 val &= ~SF1_SUNW_ADDR32;
268 #endif
270 * If this object doesn't specify any capabilities, ignore it, and
271 * leave the state as is.
273 if (val == 0)
274 return;
277 * Make sure we only accept known software capabilities. Note, that
278 * an F1_SUNW_FPUSED by itself is viewed as bad practice.
280 if ((badval = (val & ~SF1_SUNW_MASK)) != 0) {
281 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
282 ifl->ifl_name, EC_WORD(cisp->is_scnndx), cisp->is_name,
283 EC_XWORD(badval));
284 val &= SF1_SUNW_MASK;
286 if ((val & FP_FLAGS) == SF1_SUNW_FPUSED) {
287 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1),
288 ifl->ifl_name, EC_WORD(cisp->is_scnndx), cisp->is_name,
289 EC_XWORD(val));
290 return;
294 * If the input file is not a relocatable object, then we're only here
295 * to warn the user of any questionable capabilities.
297 if (ifl->ifl_ehdr->e_type != ET_REL) {
298 #if defined(_ELF64)
300 * If we're building a 64-bit executable, and we come across a
301 * dependency that requires a restricted address space, then
302 * that dependencies requirement can only be satisfied if the
303 * executable triggers the restricted address space. This is a
304 * warning rather than a fatal error, as the possibility exists
305 * that an appropriate dependency will be provided at runtime.
306 * The runtime linker will refuse to use this dependency.
308 if ((val & SF1_SUNW_ADDR32) &&
309 (ofl->ofl_flags & (FLG_OF_EXEC | FLG_OF_PIE)) &&
310 ((ofl->ofl_ocapset.oc_sf_1.cm_val &
311 SF1_SUNW_ADDR32) == 0)) {
312 ld_eprintf(ofl, ERR_WARNING,
313 MSG_INTL(MSG_FIL_EXADDR32SF1), ifl->ifl_name,
314 EC_WORD(cisp->is_scnndx), cisp->is_name);
316 #endif
317 return;
320 if (DBG_ENABLED) {
321 Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_CURRENT, CA_SUNW_SF_1,
322 ofl->ofl_ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach);
323 Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_NEW, CA_SUNW_SF_1,
324 val, ld_targ.t_m.m_mach);
328 * Determine the resolution of the present frame pointer and the
329 * new input relocatable objects frame pointer.
331 if ((ofl->ofl_ocapset.oc_sf_1.cm_val & FP_FLAGS) == FP_FLAGS) {
333 * If the new relocatable object isn't using a frame pointer,
334 * reduce the present state to unused.
336 if ((val & FP_FLAGS) != FP_FLAGS)
337 ofl->ofl_ocapset.oc_sf_1.cm_val &= ~SF1_SUNW_FPUSED;
340 * Having processed the frame pointer bits, remove them from
341 * the value so they don't get OR'd in below.
343 val &= ~FP_FLAGS;
345 } else if ((ofl->ofl_ocapset.oc_sf_1.cm_val & SF1_SUNW_FPKNWN) == 0) {
347 * If the present frame pointer state is unknown, mask it out
348 * and allow the values from the new relocatable object
349 * to overwrite them.
351 ofl->ofl_ocapset.oc_sf_1.cm_val &= ~FP_FLAGS;
352 } else {
353 /* Do not take the frame pointer flags from the object */
354 val &= ~FP_FLAGS;
357 ofl->ofl_ocapset.oc_sf_1.cm_val |= val;
359 DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_RESOLVED,
360 CA_SUNW_SF_1, ofl->ofl_ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach));
362 #undef FP_FLAGS
366 * Determine the hardware capabilities of the object being built from the
367 * capabilities of the input relocatable objects. There's really little to
368 * do here, other than to offer diagnostics, hardware capabilities are simply
369 * additive.
371 static void
372 hw_cap(Ofl_desc *ofl, Xword tag, Xword val)
374 elfcap_mask_t *hwcap;
375 ofl_flag_t flags1;
377 if (tag == CA_SUNW_HW_1) {
378 hwcap = &ofl->ofl_ocapset.oc_hw_1.cm_val;
379 flags1 = FLG_OF1_OVHWCAP1;
380 } else {
381 hwcap = &ofl->ofl_ocapset.oc_hw_2.cm_val;
382 flags1 = FLG_OF1_OVHWCAP2;
386 * If a mapfile has established definitions to override any object
387 * capabilities, ignore any new object capabilities.
389 if (ofl->ofl_flags1 & flags1) {
390 DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
391 tag, val, ld_targ.t_m.m_mach));
392 return;
396 * If this object doesn't specify any capabilities, ignore it, and
397 * leave the state as is.
399 if (val == 0)
400 return;
402 if (DBG_ENABLED) {
403 Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_CURRENT, CA_SUNW_HW_1,
404 ofl->ofl_ocapset.oc_hw_1.cm_val, ld_targ.t_m.m_mach);
405 Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_NEW, CA_SUNW_HW_1,
406 val, ld_targ.t_m.m_mach);
409 *hwcap |= val;
411 DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml, DBG_STATE_RESOLVED, tag,
412 *hwcap, ld_targ.t_m.m_mach));
416 * Promote a machine capability or platform capability to the output file.
417 * Multiple instances of these names can be defined.
419 static void
420 str_cap(Ofl_desc *ofl, char *pstr, ofl_flag_t flags, Xword tag, Caplist *list)
422 Capstr *capstr;
423 Aliste idx;
424 Boolean found = FALSE;
427 * If a mapfile has established definitions to override this capability,
428 * ignore any new capability.
430 if (ofl->ofl_flags1 & flags) {
431 DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_IGNORED,
432 tag, pstr));
433 return;
436 for (ALIST_TRAVERSE(list->cl_val, idx, capstr)) {
437 DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
438 DBG_STATE_CURRENT, tag, capstr->cs_str));
439 if (strcmp(capstr->cs_str, pstr) == 0)
440 found = TRUE;
443 DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_NEW, tag, pstr));
445 if (found == FALSE) {
446 if ((capstr = alist_append(&list->cl_val, NULL,
447 sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL) {
448 ofl->ofl_flags |= FLG_OF_FATAL;
449 return;
451 capstr->cs_str = pstr;
454 if (DBG_ENABLED) {
455 for (ALIST_TRAVERSE(list->cl_val, idx, capstr)) {
456 DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
457 DBG_STATE_RESOLVED, tag, capstr->cs_str));
463 * Promote a capability identifier to the output file. A capability group can
464 * only have one identifier, and thus only the first identifier seen from any
465 * input relocatable objects is retained. An explicit user defined identifier,
466 * rather than an an identifier fabricated by ld(1) with -z symbcap processing,
467 * takes precedence. Note, a user may have defined an identifier via a mapfile,
468 * in which case the mapfile identifier is retained.
470 static void
471 id_cap(Ofl_desc *ofl, char *pstr, oc_flag_t flags)
473 Objcapset *ocapset = &ofl->ofl_ocapset;
475 if (ocapset->oc_id.cs_str) {
476 DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_CURRENT,
477 CA_SUNW_ID, ocapset->oc_id.cs_str));
479 if ((ocapset->oc_flags & FLG_OCS_USRDEFID) ||
480 ((flags & FLG_OCS_USRDEFID) == 0)) {
481 DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
482 DBG_STATE_IGNORED, CA_SUNW_ID, pstr));
483 return;
487 DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_NEW,
488 CA_SUNW_ID, pstr));
490 ocapset->oc_id.cs_str = pstr;
491 ocapset->oc_flags |= flags;
493 DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml, DBG_STATE_RESOLVED,
494 CA_SUNW_ID, pstr));
498 * Promote a capabilities group to the object capabilities. This catches a
499 * corner case. An object capabilities file can be converted to symbol
500 * capabilities with -z symbolcap. However, if the user has indicated that all
501 * the symbols should be demoted, we'd be left with a symbol capabilities file,
502 * with no associated symbols. Catch this case by promoting the symbol
503 * capabilities back to object capabilities.
505 void
506 ld_cap_move_symtoobj(Ofl_desc *ofl)
508 Cap_group *cgp;
509 Aliste idx1;
511 for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx1, cgp)) {
512 Objcapset *scapset = &cgp->cg_set;
513 Capstr *capstr;
514 Aliste idx2;
516 if (scapset->oc_id.cs_str) {
517 if (scapset->oc_flags & FLG_OCS_USRDEFID)
518 id_cap(ofl, scapset->oc_id.cs_str,
519 scapset->oc_flags);
521 if (scapset->oc_plat.cl_val) {
522 for (ALIST_TRAVERSE(scapset->oc_plat.cl_val, idx2,
523 capstr)) {
524 str_cap(ofl, capstr->cs_str, FLG_OF1_OVPLATCAP,
525 CA_SUNW_PLAT, &ofl->ofl_ocapset.oc_plat);
528 if (scapset->oc_mach.cl_val) {
529 for (ALIST_TRAVERSE(scapset->oc_mach.cl_val, idx2,
530 capstr)) {
531 str_cap(ofl, capstr->cs_str, FLG_OF1_OVMACHCAP,
532 CA_SUNW_MACH, &ofl->ofl_ocapset.oc_mach);
535 if (scapset->oc_hw_2.cm_val)
536 hw_cap(ofl, CA_SUNW_HW_2, scapset->oc_hw_2.cm_val);
538 if (scapset->oc_hw_1.cm_val)
539 hw_cap(ofl, CA_SUNW_HW_1, scapset->oc_hw_1.cm_val);
541 if (scapset->oc_sf_1.cm_val)
542 sf1_cap(ofl, scapset->oc_sf_1.cm_val, NULL, NULL);
547 * Determine whether a capabilities group already exists that describes this
548 * new capabilities group.
550 * Note, a capability group identifier, CA_SUNW_ID, isn't used as part of the
551 * comparison. This attribute simply assigns a diagnostic name to the group,
552 * and in the case of multiple identifiers, the first will be taken.
554 static Cap_group *
555 get_cap_group(Objcapset *ocapset, Word cnum, Ofl_desc *ofl, Is_desc *isp)
557 Aliste idx;
558 Cap_group *cgp;
559 Word ccnum = cnum;
562 * If the new capabilities contains a CA_SUNW_ID, drop the count of the
563 * number of comparable items.
565 if (ocapset->oc_id.cs_str)
566 ccnum--;
569 * Traverse the existing symbols capabilities groups.
571 for (APLIST_TRAVERSE(ofl->ofl_capgroups, idx, cgp)) {
572 Word onum = cgp->cg_num;
573 Alist *calp, *oalp;
575 if (cgp->cg_set.oc_id.cs_str)
576 onum--;
578 if (onum != ccnum)
579 continue;
581 if (cgp->cg_set.oc_hw_1.cm_val != ocapset->oc_hw_1.cm_val)
582 continue;
583 if (cgp->cg_set.oc_sf_1.cm_val != ocapset->oc_sf_1.cm_val)
584 continue;
585 if (cgp->cg_set.oc_hw_2.cm_val != ocapset->oc_hw_2.cm_val)
586 continue;
588 calp = cgp->cg_set.oc_plat.cl_val;
589 oalp = ocapset->oc_plat.cl_val;
590 if ((calp == NULL) && oalp)
591 continue;
592 if (calp && ((oalp == NULL) || cap_names_match(calp, oalp)))
593 continue;
595 calp = cgp->cg_set.oc_mach.cl_val;
596 oalp = ocapset->oc_mach.cl_val;
597 if ((calp == NULL) && oalp)
598 continue;
599 if (calp && ((oalp == NULL) || cap_names_match(calp, oalp)))
600 continue;
603 * If a matching group is found, then this new group has
604 * already been supplied by a previous file, and hence the
605 * existing group can be used. Record this new input section,
606 * from which we can also derive the input file name, on the
607 * existing groups input sections.
609 if (aplist_append(&(cgp->cg_secs), isp,
610 AL_CNT_CAP_SECS) == NULL)
611 return (NULL);
612 return (cgp);
616 * If a capabilities group is not found, create a new one.
618 if (((cgp = libld_calloc(sizeof (Cap_group), 1)) == NULL) ||
619 (aplist_append(&(ofl->ofl_capgroups), cgp,
620 AL_CNT_CAP_DESCS) == NULL))
621 return (NULL);
624 * If we're converting object capabilities to symbol capabilities and
625 * no CA_SUNW_ID is defined, fabricate one. This identifier is appended
626 * to all symbol names that are converted into capabilities symbols,
627 * see ld_sym_process().
629 if ((isp->is_file->ifl_flags & FLG_IF_OTOSCAP) &&
630 (ocapset->oc_id.cs_str == NULL)) {
631 size_t len;
634 * Create an identifier using the group number together with a
635 * default template. We allocate a buffer large enough for any
636 * possible number of items (way more than we need).
638 len = MSG_STR_CAPGROUPID_SIZE + CONV_INV_BUFSIZE;
639 if ((ocapset->oc_id.cs_str = libld_malloc(len)) == NULL)
640 return (NULL);
642 (void) snprintf(ocapset->oc_id.cs_str, len,
643 MSG_ORIG(MSG_STR_CAPGROUPID),
644 aplist_nitems(ofl->ofl_capgroups));
645 cnum++;
648 cgp->cg_set = *ocapset;
649 cgp->cg_num = cnum;
652 * Null the callers alist's as they've effectively been transferred
653 * to this new Cap_group.
655 ocapset->oc_plat.cl_val = ocapset->oc_mach.cl_val = NULL;
658 * Keep track of which input section, and hence input file, established
659 * this group.
661 if (aplist_append(&(cgp->cg_secs), isp, AL_CNT_CAP_SECS) == NULL)
662 return (NULL);
665 * Keep track of the number of symbol capabilities entries that will be
666 * required in the output file. Each group requires a terminating
667 * CA_SUNW_NULL.
669 ofl->ofl_capsymcnt += (cnum + 1);
670 return (cgp);
674 * Capture symbol capability family information. This data structure is focal
675 * in maintaining all symbol capability relationships, and provides for the
676 * eventual creation of a capabilities information section, and possibly a
677 * capabilities chain section.
679 * Capabilities families are lead by a CAPINFO_SUNW_GLOB symbol. This symbol
680 * provides the visible global symbol that is referenced by all external
681 * callers. This symbol may have aliases. For example, a weak/global symbol
682 * pair, such as memcpy()/_memcpy() may lead the same capabilities family.
683 * Each family contains one or more local symbol members. These members provide
684 * the capabilities specific functions, and are associated to a capabilities
685 * group. For example, the capability members memcpy%sun4u and memcpy%sun4v
686 * might be associated with the memcpy() capability family.
688 * This routine is called when a relocatable object that provides object
689 * capabilities is transformed into a symbol capabilities object, using the
690 * -z symbolcap option.
692 * This routine is also called to collect the SUNW_capinfo section information
693 * of a relocatable object that contains symbol capability definitions.
695 uintptr_t
696 ld_cap_add_family(Ofl_desc *ofl, Sym_desc *lsdp, Sym_desc *csdp, Cap_group *cgp,
697 APlist **csyms)
699 Cap_avlnode qcav, *cav;
700 avl_tree_t *avlt;
701 avl_index_t where = 0;
702 Cap_sym *mcsp;
703 Aliste idx;
706 * Make sure the capability families have an initialized AVL tree.
708 if ((avlt = ofl->ofl_capfamilies) == NULL) {
709 if ((avlt = libld_calloc(sizeof (avl_tree_t), 1)) == NULL)
710 return (S_ERROR);
711 avl_create(avlt, &ld_sym_avl_comp, sizeof (Cap_avlnode),
712 SGSOFFSETOF(Cap_avlnode, cn_symavlnode.sav_node));
713 ofl->ofl_capfamilies = avlt;
716 * When creating a dynamic object, capability family members
717 * are maintained in a .SUNW_capchain, the first entry of
718 * which is the version number of the chain.
720 ofl->ofl_capchaincnt = 1;
724 * Determine whether a family already exists, and if not, create one
725 * using the lead family symbol.
727 qcav.cn_symavlnode.sav_hash = (Word)elf_hash(lsdp->sd_name);
728 qcav.cn_symavlnode.sav_name = lsdp->sd_name;
730 if ((cav = avl_find(avlt, &qcav, &where)) == NULL) {
731 if ((cav = libld_calloc(sizeof (Cap_avlnode), 1)) == NULL)
732 return (S_ERROR);
733 cav->cn_symavlnode.sav_hash = qcav.cn_symavlnode.sav_hash;
734 cav->cn_symavlnode.sav_name = qcav.cn_symavlnode.sav_name;
735 cav->cn_symavlnode.sav_sdp = lsdp;
737 avl_insert(avlt, cav, where);
740 * When creating a dynamic object, capability family members
741 * are maintained in a .SUNW_capchain, each family starts with
742 * this lead symbol, and is terminated with a 0 element.
744 ofl->ofl_capchaincnt += 2;
748 * If no group information is provided then this request is to add a
749 * lead capability symbol, or lead symbol alias. If this is the lead
750 * symbol there's nothing more to do. Otherwise save the alias.
752 if (cgp == NULL) {
753 if ((lsdp != csdp) && (aplist_append(&cav->cn_aliases, csdp,
754 AL_CNT_CAP_ALIASES) == NULL))
755 return (S_ERROR);
757 return (0);
761 * Determine whether a member of the same group as this new member is
762 * already defined within this family. If so, we have a multiply
763 * defined symbol.
765 for (APLIST_TRAVERSE(cav->cn_members, idx, mcsp)) {
766 Sym_desc *msdp;
768 if (cgp != mcsp->cs_group)
769 continue;
772 * Diagnose that a multiple symbol definition exists.
774 msdp = mcsp->cs_sdp;
776 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_CAP_MULDEF),
777 demangle(lsdp->sd_name));
778 ld_eprintf(ofl, ERR_NONE, MSG_INTL(MSG_CAP_MULDEFSYMS),
779 msdp->sd_file->ifl_name, msdp->sd_name,
780 csdp->sd_file->ifl_name, csdp->sd_name);
784 * Add this capabilities symbol member to the family.
786 if (((mcsp = libld_malloc(sizeof (Cap_sym))) == NULL) ||
787 (aplist_append(&cav->cn_members, mcsp, AL_CNT_CAP_MEMS) == NULL))
788 return (S_ERROR);
790 mcsp->cs_sdp = csdp;
791 mcsp->cs_group = cgp;
794 * When creating a dynamic object, capability family members are
795 * maintained in a .SUNW_capchain. Account for this family member.
797 ofl->ofl_capchaincnt++;
800 * If this input file is undergoing object capabilities to symbol
801 * capabilities conversion, then this member is a new local symbol
802 * that has been generated from an original global symbol. Keep track
803 * of this symbol so that the output file symbol table can be populated
804 * with these new symbol entries.
806 if (csyms && (aplist_append(csyms, mcsp, AL_CNT_CAP_SYMS) == NULL))
807 return (S_ERROR);
809 return (0);
813 * Process a SHT_SUNW_cap capabilities section.
815 static uintptr_t
816 process_cap(Ofl_desc *ofl, Ifl_desc *ifl, Is_desc *cisp)
818 Objcapset ocapset = { 0 };
819 Cap_desc *cdp;
820 Cap *data, *cdata;
821 char *strs;
822 Word ndx, cnum;
823 int objcapndx, descapndx, symcapndx;
824 int nulls, capstrs = 0;
827 * Determine the capabilities data and size.
829 cdata = (Cap *)cisp->is_indata->d_buf;
830 cnum = (Word)(cisp->is_shdr->sh_size / cisp->is_shdr->sh_entsize);
832 if ((cdata == NULL) || (cnum == 0))
833 return (0);
835 DBG_CALL(Dbg_cap_sec_title(ofl->ofl_lml, ifl->ifl_name));
838 * Traverse the section to determine what capabilities groups are
839 * available.
841 * A capabilities section can contain one or more, CA_SUNW_NULL
842 * terminated groups.
844 * - The first group defines the object capabilities.
845 * - Additional groups define symbol capabilities.
846 * - Since the initial group is always reserved for object
847 * capabilities, any object with symbol capabilities must also
848 * have an object capabilities group. If the object has no object
849 * capabilities, an empty object group is defined, consisting of a
850 * CA_SUNW_NULL element in index [0].
851 * - If any capabilities require references to a named string, then
852 * the section header sh_info points to the associated string
853 * table.
854 * - If an object contains symbol capability groups, then the
855 * section header sh_link points to the associated capinfo table.
857 objcapndx = 0;
858 descapndx = symcapndx = -1;
859 nulls = 0;
861 for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
862 switch (data->c_tag) {
863 case CA_SUNW_NULL:
865 * If this is the first CA_SUNW_NULL entry, and no
866 * capabilities group has been found, then this object
867 * does not define any object capabilities.
869 if (nulls++ == 0) {
870 if (ndx == 0)
871 objcapndx = -1;
872 } else if ((symcapndx == -1) && (descapndx != -1))
873 symcapndx = descapndx;
875 break;
877 case CA_SUNW_PLAT:
878 case CA_SUNW_MACH:
879 case CA_SUNW_ID:
880 capstrs++;
881 /* FALLTHROUGH */
883 case CA_SUNW_HW_1:
884 case CA_SUNW_SF_1:
885 case CA_SUNW_HW_2:
887 * If this is the start of a new group, save it.
889 if (descapndx == -1)
890 descapndx = ndx;
891 break;
893 default:
894 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_UNKCAP),
895 ifl->ifl_name, EC_WORD(cisp->is_scnndx),
896 cisp->is_name, data->c_tag);
901 * If a string capabilities entry has been found, the capabilities
902 * section must reference the associated string table.
904 if (capstrs) {
905 Word info = cisp->is_shdr->sh_info;
907 if ((info == 0) || (info > ifl->ifl_shnum)) {
908 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
909 ifl->ifl_name, EC_WORD(cisp->is_scnndx),
910 cisp->is_name, EC_XWORD(info));
911 return (S_ERROR);
913 strs = (char *)ifl->ifl_isdesc[info]->is_indata->d_buf;
917 * The processing of capabilities groups is as follows:
919 * - if a relocatable object provides only object capabilities, and
920 * the -z symbolcap option is in effect, then the object
921 * capabilities are transformed into symbol capabilities and the
922 * symbol capabilities are carried over to the output file.
923 * - in all other cases, any capabilities present in an input
924 * relocatable object are carried from the input object to the
925 * output without any transformation or conversion.
927 * Capture any object capabilities that are to be carried over to the
928 * output file.
930 if ((objcapndx == 0) &&
931 ((symcapndx != -1) || ((ofl->ofl_flags & FLG_OF_OTOSCAP) == 0))) {
932 for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
934 * Object capabilities end at the first null.
936 if (data->c_tag == CA_SUNW_NULL)
937 break;
940 * Only the object software capabilities that are
941 * defined in a relocatable object become part of the
942 * object software capabilities in the output file.
943 * However, check the validity of any object software
944 * capabilities of any dependencies.
946 if (data->c_tag == CA_SUNW_SF_1) {
947 sf1_cap(ofl, data->c_un.c_val, ifl, cisp);
948 continue;
952 * The remaining capability types must come from a
953 * relocatable object in order to contribute to the
954 * output.
956 if (ifl->ifl_ehdr->e_type != ET_REL)
957 continue;
959 switch (data->c_tag) {
960 case CA_SUNW_HW_1:
961 case CA_SUNW_HW_2:
962 hw_cap(ofl, data->c_tag, data->c_un.c_val);
963 break;
965 case CA_SUNW_PLAT:
966 str_cap(ofl, strs + data->c_un.c_ptr,
967 FLG_OF1_OVPLATCAP, CA_SUNW_PLAT,
968 &ofl->ofl_ocapset.oc_plat);
969 break;
971 case CA_SUNW_MACH:
972 str_cap(ofl, strs + data->c_un.c_ptr,
973 FLG_OF1_OVMACHCAP, CA_SUNW_MACH,
974 &ofl->ofl_ocapset.oc_mach);
975 break;
977 case CA_SUNW_ID:
978 id_cap(ofl, strs + data->c_un.c_ptr,
979 FLG_OCS_USRDEFID);
980 break;
982 default:
983 assert(0); /* Unknown capability type */
988 * If there are no symbol capabilities, or this objects
989 * capabilities aren't being transformed into a symbol
990 * capabilities, then we're done.
992 if ((symcapndx == -1) &&
993 ((ofl->ofl_flags & FLG_OF_OTOSCAP) == 0))
994 return (1);
998 * If these capabilities don't originate from a relocatable object
999 * there's no further processing required.
1001 if (ifl->ifl_ehdr->e_type != ET_REL)
1002 return (1);
1005 * If this object only defines an object capabilities group, and the
1006 * -z symbolcap option is in effect, then all global function symbols
1007 * and initialized global data symbols are renamed and assigned to the
1008 * transformed symbol capabilities group.
1010 if ((objcapndx == 0) &&
1011 (symcapndx == -1) && (ofl->ofl_flags & FLG_OF_OTOSCAP))
1012 ifl->ifl_flags |= FLG_IF_OTOSCAP;
1015 * Allocate a capabilities descriptor to collect the capabilities data
1016 * for this input file. Allocate a mirror of the raw capabilities data
1017 * that points to the individual symbol capabilities groups. An APlist
1018 * is used, although it will be sparsely populated, as the list provides
1019 * a convenient mechanism for traversal later.
1021 if (((cdp = libld_calloc(sizeof (Cap_desc), 1)) == NULL) ||
1022 (aplist_append(&(cdp->ca_groups), NULL, cnum) == NULL))
1023 return (S_ERROR);
1026 * Clear the allocated APlist data array, and assign the number of
1027 * items as the total number of array items.
1029 (void) memset(&cdp->ca_groups->apl_data[0], 0,
1030 (cnum * sizeof (void *)));
1031 cdp->ca_groups->apl_nitems = cnum;
1033 ifl->ifl_caps = cdp;
1036 * Traverse the capabilities data, unpacking the data into a
1037 * capabilities set. Process each capabilities set as a unique group.
1039 descapndx = -1;
1040 nulls = 0;
1042 for (ndx = 0, data = cdata; ndx < cnum; ndx++, data++) {
1043 Capstr *capstr;
1045 switch (data->c_tag) {
1046 case CA_SUNW_NULL:
1047 nulls++;
1050 * Process the capabilities group that this null entry
1051 * terminates. The capabilities group that is returned
1052 * will either point to this file's data, or to a
1053 * matching capabilities group that has already been
1054 * processed.
1056 * Note, if this object defines object capabilities,
1057 * the first group descriptor points to these object
1058 * capabilities. It is only necessary to save this
1059 * descriptor when object capabilities are being
1060 * transformed into symbol capabilities (-z symbolcap).
1062 if (descapndx != -1) {
1063 if ((nulls > 1) ||
1064 (ifl->ifl_flags & FLG_IF_OTOSCAP)) {
1065 APlist *alp = cdp->ca_groups;
1067 if ((alp->apl_data[descapndx] =
1068 get_cap_group(&ocapset,
1069 (ndx - descapndx), ofl,
1070 cisp)) == NULL)
1071 return (S_ERROR);
1075 * Clean up the capabilities data in preparation
1076 * for processing additional groups. If the
1077 * collected capabilities strings were used to
1078 * establish a new output group, they will have
1079 * been saved in get_cap_group(). If these
1080 * descriptors still exist, then an existing
1081 * descriptor has been used to associate with
1082 * this file, and these string descriptors can
1083 * be freed.
1085 ocapset.oc_hw_1.cm_val =
1086 ocapset.oc_sf_1.cm_val =
1087 ocapset.oc_hw_2.cm_val = 0;
1088 if (ocapset.oc_plat.cl_val) {
1089 free((void *)ocapset.oc_plat.cl_val);
1090 ocapset.oc_plat.cl_val = NULL;
1092 if (ocapset.oc_mach.cl_val) {
1093 free((void *)ocapset.oc_mach.cl_val);
1094 ocapset.oc_mach.cl_val = NULL;
1096 descapndx = -1;
1098 continue;
1100 case CA_SUNW_HW_1:
1101 ocapset.oc_hw_1.cm_val = data->c_un.c_val;
1102 DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
1103 DBG_STATE_ORIGINAL, CA_SUNW_HW_1,
1104 ocapset.oc_hw_1.cm_val, ld_targ.t_m.m_mach));
1105 break;
1107 case CA_SUNW_SF_1:
1108 ocapset.oc_sf_1.cm_val = data->c_un.c_val;
1109 DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
1110 DBG_STATE_ORIGINAL, CA_SUNW_SF_1,
1111 ocapset.oc_sf_1.cm_val, ld_targ.t_m.m_mach));
1112 break;
1114 case CA_SUNW_HW_2:
1115 ocapset.oc_hw_2.cm_val = data->c_un.c_val;
1116 DBG_CALL(Dbg_cap_val_entry(ofl->ofl_lml,
1117 DBG_STATE_ORIGINAL, CA_SUNW_HW_2,
1118 ocapset.oc_hw_2.cm_val, ld_targ.t_m.m_mach));
1119 break;
1121 case CA_SUNW_PLAT:
1122 if ((capstr = alist_append(&ocapset.oc_plat.cl_val,
1123 NULL, sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL)
1124 return (S_ERROR);
1125 capstr->cs_str = strs + data->c_un.c_ptr;
1126 DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
1127 DBG_STATE_ORIGINAL, CA_SUNW_PLAT, capstr->cs_str));
1128 break;
1130 case CA_SUNW_MACH:
1131 if ((capstr = alist_append(&ocapset.oc_mach.cl_val,
1132 NULL, sizeof (Capstr), AL_CNT_CAP_NAMES)) == NULL)
1133 return (S_ERROR);
1134 capstr->cs_str = strs + data->c_un.c_ptr;
1135 DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
1136 DBG_STATE_ORIGINAL, CA_SUNW_MACH, capstr->cs_str));
1137 break;
1139 case CA_SUNW_ID:
1140 ocapset.oc_id.cs_str = strs + data->c_un.c_ptr;
1141 DBG_CALL(Dbg_cap_ptr_entry(ofl->ofl_lml,
1142 DBG_STATE_ORIGINAL, CA_SUNW_ID,
1143 ocapset.oc_id.cs_str));
1144 break;
1148 * Save the start of this new group.
1150 if (descapndx == -1)
1151 descapndx = ndx;
1153 return (1);
1157 * Capture any symbol capabilities symbols. An object file that contains symbol
1158 * capabilities has an associated .SUNW_capinfo section. This section
1159 * identifies which symbols are associated to which capabilities, together with
1160 * their associated lead symbol. Each of these symbol pairs are recorded for
1161 * processing later.
1163 static uintptr_t
1164 process_capinfo(Ofl_desc *ofl, Ifl_desc *ifl, Is_desc *isp)
1166 Cap_desc *cdp = ifl->ifl_caps;
1167 Capinfo *capinfo = isp->is_indata->d_buf;
1168 Shdr *shdr = isp->is_shdr;
1169 Word cndx, capinfonum;
1171 capinfonum = (Word)(shdr->sh_size / shdr->sh_entsize);
1173 if ((cdp == NULL) || (capinfo == NULL) || (capinfonum == 0))
1174 return (0);
1176 for (cndx = 1, capinfo++; cndx < capinfonum; cndx++, capinfo++) {
1177 Sym_desc *sdp, *lsdp;
1178 Word lndx;
1179 uchar_t gndx;
1181 if ((gndx = (uchar_t)ELF_C_GROUP(*capinfo)) == 0)
1182 continue;
1183 lndx = (Word)ELF_C_SYM(*capinfo);
1186 * Catch any anomalies. A capabilities symbol should be valid,
1187 * and the capabilities lead symbol should also be global.
1188 * Note, ld(1) -z symbolcap would create local capabilities
1189 * symbols, but we don't enforce this so as to give the
1190 * compilation environment a little more freedom.
1192 if ((sdp = ifl->ifl_oldndx[cndx]) == NULL) {
1193 ld_eprintf(ofl, ERR_WARNING,
1194 MSG_INTL(MSG_CAPINFO_INVALSYM), ifl->ifl_name,
1195 EC_WORD(isp->is_scnndx), isp->is_name, cndx,
1196 MSG_INTL(MSG_STR_UNKNOWN));
1197 continue;
1199 if ((lndx == 0) || (lndx >= ifl->ifl_symscnt) ||
1200 ((lsdp = ifl->ifl_oldndx[lndx]) == NULL) ||
1201 (ELF_ST_BIND(lsdp->sd_sym->st_info) != STB_GLOBAL)) {
1202 ld_eprintf(ofl, ERR_WARNING,
1203 MSG_INTL(MSG_CAPINFO_INVALLEAD), ifl->ifl_name,
1204 EC_WORD(isp->is_scnndx), isp->is_name, cndx, lsdp ?
1205 demangle(lsdp->sd_name) : MSG_INTL(MSG_STR_UNKNOWN),
1206 lndx);
1207 continue;
1211 * Indicate that this is a capabilities symbol.
1213 sdp->sd_flags |= FLG_SY_CAP;
1216 * Save any global capability symbols. Global capability
1217 * symbols are identified with a CAPINFO_SUNW_GLOB group id.
1218 * The lead symbol for this global capability symbol is either
1219 * the symbol itself, or an alias.
1221 if (gndx == CAPINFO_SUNW_GLOB) {
1222 if (ld_cap_add_family(ofl, lsdp, sdp,
1223 NULL, NULL) == S_ERROR)
1224 return (S_ERROR);
1225 continue;
1229 * Track the number of non-global capabilities symbols, as these
1230 * are used to size any symbol tables. If we're generating a
1231 * dynamic object, this symbol will be added to the dynamic
1232 * symbol table, therefore ensure there is space in the dynamic
1233 * string table.
1235 ofl->ofl_caploclcnt++;
1236 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) &&
1237 (st_insert(ofl->ofl_dynstrtab, sdp->sd_name) == -1))
1238 return (S_ERROR);
1241 * As we're tracking this local symbol as a capabilities symbol,
1242 * reduce the local symbol count to compensate.
1244 ofl->ofl_locscnt--;
1247 * Determine whether the associated lead symbol indicates
1248 * NODYNSORT. If so, remove this local entry from the
1249 * SUNW_dynsort section too. NODYNSORT tagging can only be
1250 * obtained from a mapfile symbol definition, and thus any
1251 * global definition that has this tagging has already been
1252 * instantiated and this instance resolved to it.
1254 if (lsdp->sd_flags & FLG_SY_NODYNSORT) {
1255 Sym *lsym = lsdp->sd_sym;
1256 uchar_t ltype = ELF_ST_TYPE(lsym->st_info);
1258 DYNSORT_COUNT(lsdp, lsym, ltype, --);
1259 lsdp->sd_flags |= FLG_SY_NODYNSORT;
1263 * Track this family member, together with its associated group.
1265 if (ld_cap_add_family(ofl, lsdp, sdp,
1266 cdp->ca_groups->apl_data[gndx], NULL) == S_ERROR)
1267 return (S_ERROR);
1270 return (0);
1274 * Simply process the section so that we have pointers to the data for use
1275 * in later routines, however don't add the section to the output section
1276 * list as we will be creating our own replacement sections later (ie.
1277 * symtab and relocation).
1279 static uintptr_t
1280 /* ARGSUSED5 */
1281 process_input(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1282 Word ndx, int ident, Ofl_desc *ofl)
1284 return (process_section(name, ifl, shdr, scn, ndx,
1285 ld_targ.t_id.id_null, ofl));
1289 * Keep a running count of relocation entries from input relocatable objects for
1290 * sizing relocation buckets later. If we're building an executable, save any
1291 * relocations from shared objects to determine if any copy relocation symbol
1292 * has a displacement relocation against it.
1294 static uintptr_t
1295 /* ARGSUSED5 */
1296 process_reloc(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1297 Word ndx, int ident, Ofl_desc *ofl)
1299 if (process_section(name, ifl,
1300 shdr, scn, ndx, ld_targ.t_id.id_null, ofl) == S_ERROR)
1301 return (S_ERROR);
1303 if (ifl->ifl_ehdr->e_type == ET_REL) {
1304 if (shdr->sh_entsize && (shdr->sh_entsize <= shdr->sh_size))
1305 /* LINTED */
1306 ofl->ofl_relocincnt +=
1307 (Word)(shdr->sh_size / shdr->sh_entsize);
1308 } else if (ofl->ofl_flags & FLG_OF_EXEC) {
1309 if (aplist_append(&ifl->ifl_relsect, ifl->ifl_isdesc[ndx],
1310 AL_CNT_IFL_RELSECS) == NULL)
1311 return (S_ERROR);
1313 return (1);
1317 * Process a string table section. A valid section contains an initial and
1318 * final null byte.
1320 static uintptr_t
1321 process_strtab(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1322 Word ndx, int ident, Ofl_desc *ofl)
1324 char *data;
1325 size_t size;
1326 Is_desc *isp;
1327 uintptr_t error;
1330 * Never include .stab.excl sections in any output file.
1331 * If the -s flag has been specified strip any .stab sections.
1333 if (((ofl->ofl_flags & FLG_OF_STRIP) && ident &&
1334 (strncmp(name, MSG_ORIG(MSG_SCN_STAB), MSG_SCN_STAB_SIZE) == 0)) ||
1335 (strcmp(name, MSG_ORIG(MSG_SCN_STABEXCL)) == 0) && ident)
1336 return (1);
1339 * If we got here to process a .shstrtab or .dynstr table, `ident' will
1340 * be null. Otherwise make sure we don't have a .strtab section as this
1341 * should not be added to the output section list either.
1343 if ((ident != ld_targ.t_id.id_null) &&
1344 (strcmp(name, MSG_ORIG(MSG_SCN_STRTAB)) == 0))
1345 ident = ld_targ.t_id.id_null;
1347 error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
1348 if ((error == 0) || (error == S_ERROR))
1349 return (error);
1352 * String tables should start and end with a NULL byte. Note, it has
1353 * been known for the assembler to create empty string tables, so check
1354 * the size before attempting to verify the data itself.
1356 isp = ifl->ifl_isdesc[ndx];
1357 size = isp->is_indata->d_size;
1358 if (size) {
1359 data = isp->is_indata->d_buf;
1360 if (data[0] != '\0' || data[size - 1] != '\0')
1361 ld_eprintf(ofl, ERR_WARNING,
1362 MSG_INTL(MSG_FIL_MALSTR), ifl->ifl_name,
1363 EC_WORD(isp->is_scnndx), name);
1364 } else
1365 isp->is_indata->d_buf = (void *)MSG_ORIG(MSG_STR_EMPTY);
1367 ifl->ifl_flags |= FLG_IF_HSTRTAB;
1368 return (1);
1372 * Invalid sections produce a warning and are skipped.
1374 static uintptr_t
1375 /* ARGSUSED3 */
1376 invalid_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1377 Word ndx, int ident, Ofl_desc *ofl)
1379 Conv_inv_buf_t inv_buf;
1381 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_INVALSEC),
1382 ifl->ifl_name, EC_WORD(ndx), name,
1383 conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
1384 ifl->ifl_ehdr->e_machine, shdr->sh_type, 0, &inv_buf));
1385 return (1);
1389 * Compare an input section name to a given string, taking the ELF '%'
1390 * section naming convention into account. If an input section name
1391 * contains a '%' character, the '%' and all following characters are
1392 * ignored in the comparison.
1394 * entry:
1395 * is_name - Name of input section
1396 * match_name - Name to compare to
1397 * match_len - strlen(match_name)
1399 * exit:
1400 * Returns True (1) if the names match, and False (0) otherwise.
1402 inline static int
1403 is_name_cmp(const char *is_name, const char *match_name, size_t match_len)
1406 * If the start of is_name is not a match for name,
1407 * the match fails.
1409 if (strncmp(is_name, match_name, match_len) != 0)
1410 return (0);
1413 * The prefix matched. The next character must be either '%', or
1414 * NULL, in order for a match to be true.
1416 is_name += match_len;
1417 return ((*is_name == '\0') || (*is_name == '%'));
1421 * Helper routine for process_progbits() to process allocable sections.
1423 * entry:
1424 * name, ifl, shdr, ndx, ident, ofl - As passed to process_progbits().
1425 * is_stab_index - TRUE if section is .index.
1426 * is_flags - Additional flags to be added to the input section.
1428 * exit:
1429 * The allocable section has been processed. *ident and *is_flags
1430 * are updated as necessary to reflect the changes. Returns TRUE
1431 * for success, FALSE for failure.
1433 /*ARGSUSED*/
1434 inline static Boolean
1435 process_progbits_alloc(const char *name, Ifl_desc *ifl, Shdr *shdr,
1436 Word ndx, int *ident, Ofl_desc *ofl, Boolean is_stab_index,
1437 Word *is_flags)
1439 Boolean done = FALSE;
1441 if (name[0] == '.') {
1442 switch (name[1]) {
1443 case 'e':
1444 if (!is_name_cmp(name, MSG_ORIG(MSG_SCN_EHFRAME),
1445 MSG_SCN_EHFRAME_SIZE))
1446 break;
1448 *ident = ld_targ.t_id.id_unwind;
1449 *is_flags |= FLG_IS_EHFRAME;
1450 done = TRUE;
1453 * Historically, the section containing the logic to
1454 * unwind stack frames -- the .eh_frame section -- was
1455 * of type SHT_PROGBITS. Apparently the most
1456 * aesthetically galling aspect of this was not the
1457 * .eh_frame section's dubious purpose or its filthy
1458 * implementation, but rather its section type; with the
1459 * introduction of the AMD64 ABI, a new section header
1460 * type (SHT_AMD64_UNWIND) was introduced for (and
1461 * dedicated to) this section. When both the Sun
1462 * compilers and the GNU compilers had been modified to
1463 * generate this new section type, the linker became
1464 * much more pedantic about .eh_frame: it refused to
1465 * link an AMD64 object that contained a .eh_frame with
1466 * the legacy SHT_PROGBITS. That this was too fussy is
1467 * evidenced by searching the net for the error message
1468 * that it generated ("section type is SHT_PROGBITS:
1469 * expected SHT_AMD64_UNWIND"), which reveals a myriad
1470 * of problems, including legacy objects, hand-coded
1471 * assembly and otherwise cross-platform objects
1472 * created on other platforms (the GNU toolchain was
1473 * only modified to create the new section type on
1474 * Solaris and derivatives). We therefore always accept
1475 * a .eh_frame of SHT_PROGBITS -- regardless of
1476 * m_sht_unwind.
1478 break;
1479 case 'g':
1480 if (is_name_cmp(name, MSG_ORIG(MSG_SCN_GOT),
1481 MSG_SCN_GOT_SIZE)) {
1482 *ident = ld_targ.t_id.id_null;
1483 done = TRUE;
1484 break;
1486 if ((ld_targ.t_m.m_sht_unwind == SHT_PROGBITS) &&
1487 is_name_cmp(name, MSG_ORIG(MSG_SCN_GCC_X_TBL),
1488 MSG_SCN_GCC_X_TBL_SIZE)) {
1489 *ident = ld_targ.t_id.id_unwind;
1490 done = TRUE;
1491 break;
1493 break;
1494 case 'p':
1495 if (is_name_cmp(name, MSG_ORIG(MSG_SCN_PLT),
1496 MSG_SCN_PLT_SIZE)) {
1497 *ident = ld_targ.t_id.id_null;
1498 done = TRUE;
1500 break;
1503 if (!done) {
1504 if (is_stab_index) {
1506 * This is a work-around for x86 compilers that have
1507 * set SHF_ALLOC for the .stab.index section.
1509 * Because of this, make sure that the .stab.index
1510 * does not end up as the last section in the text
1511 * segment. Older linkers can produce segmentation
1512 * violations when they strip (ld -s) against a
1513 * shared object whose last section in the text
1514 * segment is a .stab.
1516 *ident = ld_targ.t_id.id_interp;
1517 } else {
1518 *ident = ld_targ.t_id.id_data;
1522 return (TRUE);
1526 * Process a progbits section.
1528 static uintptr_t
1529 process_progbits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1530 Word ndx, int ident, Ofl_desc *ofl)
1532 Boolean is_stab_index = FALSE;
1533 Word is_flags = 0;
1534 uintptr_t r;
1537 * Never include .stab.excl sections in any output file.
1538 * If the -s flag has been specified strip any .stab sections.
1540 if (ident && (strncmp(name, MSG_ORIG(MSG_SCN_STAB),
1541 MSG_SCN_STAB_SIZE) == 0)) {
1542 if ((ofl->ofl_flags & FLG_OF_STRIP) ||
1543 (strcmp((name + MSG_SCN_STAB_SIZE),
1544 MSG_ORIG(MSG_SCN_EXCL)) == 0))
1545 return (1);
1547 if (strcmp((name + MSG_SCN_STAB_SIZE),
1548 MSG_ORIG(MSG_SCN_INDEX)) == 0)
1549 is_stab_index = TRUE;
1552 if ((ofl->ofl_flags & FLG_OF_STRIP) && ident) {
1553 if ((strncmp(name, MSG_ORIG(MSG_SCN_DEBUG),
1554 MSG_SCN_DEBUG_SIZE) == 0) ||
1555 (strcmp(name, MSG_ORIG(MSG_SCN_LINE)) == 0))
1556 return (1);
1560 * Update the ident to reflect the type of section we've got.
1562 * If there is any .plt or .got section to generate we'll be creating
1563 * our own version, so don't allow any input sections of these types to
1564 * be added to the output section list (why a relocatable object would
1565 * have a .plt or .got is a mystery, but stranger things have occurred).
1567 * If there are any unwind sections, and this is a platform that uses
1568 * SHT_PROGBITS for unwind sections, then set their ident to reflect
1569 * that.
1571 if (ident) {
1572 if (shdr->sh_flags & SHF_TLS) {
1573 ident = ld_targ.t_id.id_tls;
1574 } else if ((shdr->sh_flags & ~ALL_SHF_IGNORE) ==
1575 (SHF_ALLOC | SHF_EXECINSTR)) {
1576 ident = ld_targ.t_id.id_text;
1577 } else if (shdr->sh_flags & SHF_ALLOC) {
1578 if (process_progbits_alloc(name, ifl, shdr, ndx,
1579 &ident, ofl, is_stab_index, &is_flags) == FALSE)
1580 return (S_ERROR);
1581 } else {
1582 ident = ld_targ.t_id.id_note;
1586 r = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
1589 * On success, process_section() creates an input section descriptor.
1590 * Now that it exists, we can add any pending input section flags.
1592 if ((is_flags != 0) && (r == 1))
1593 ifl->ifl_isdesc[ndx]->is_flags |= is_flags;
1595 return (r);
1599 * Handles the SHT_SUNW_{DEBUG,DEBUGSTR) sections.
1601 static uintptr_t
1602 process_debug(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1603 Word ndx, int ident, Ofl_desc *ofl)
1606 * Debug information is discarded when the 'ld -s' flag is invoked.
1608 if (ofl->ofl_flags & FLG_OF_STRIP) {
1609 return (1);
1611 return (process_progbits(name, ifl, shdr, scn, ndx, ident, ofl));
1615 * Process a nobits section.
1617 static uintptr_t
1618 process_nobits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1619 Word ndx, int ident, Ofl_desc *ofl)
1621 if (ident) {
1622 if (shdr->sh_flags & SHF_TLS)
1623 ident = ld_targ.t_id.id_tlsbss;
1624 #if defined(_ELF64)
1625 else if ((shdr->sh_flags & SHF_AMD64_LARGE) &&
1626 (ld_targ.t_m.m_mach == EM_AMD64))
1627 ident = ld_targ.t_id.id_lbss;
1628 #endif
1629 else
1630 ident = ld_targ.t_id.id_bss;
1632 return (process_section(name, ifl, shdr, scn, ndx, ident, ofl));
1636 * Process a SHT_*_ARRAY section.
1638 static uintptr_t
1639 process_array(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1640 Word ndx, int ident, Ofl_desc *ofl)
1642 uintptr_t error;
1644 if (ident)
1645 ident = ld_targ.t_id.id_array;
1647 error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
1648 if ((error == 0) || (error == S_ERROR))
1649 return (error);
1651 return (1);
1654 static uintptr_t
1655 /* ARGSUSED1 */
1656 array_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1658 Os_desc *osp;
1659 Shdr *shdr;
1661 if ((isc == NULL) || ((osp = isc->is_osdesc) == NULL))
1662 return (0);
1664 shdr = isc->is_shdr;
1666 if ((shdr->sh_type == SHT_FINI_ARRAY) &&
1667 (ofl->ofl_osfiniarray == NULL))
1668 ofl->ofl_osfiniarray = osp;
1669 else if ((shdr->sh_type == SHT_INIT_ARRAY) &&
1670 (ofl->ofl_osinitarray == NULL))
1671 ofl->ofl_osinitarray = osp;
1672 else if ((shdr->sh_type == SHT_PREINIT_ARRAY) &&
1673 (ofl->ofl_ospreinitarray == NULL))
1674 ofl->ofl_ospreinitarray = osp;
1676 return (1);
1680 * Process a SHT_SYMTAB_SHNDX section.
1682 static uintptr_t
1683 process_sym_shndx(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1684 Word ndx, int ident, Ofl_desc *ofl)
1686 if (process_input(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR)
1687 return (S_ERROR);
1690 * Have we already seen the related SYMTAB - if so verify it now.
1692 if (shdr->sh_link < ndx) {
1693 Is_desc *isp = ifl->ifl_isdesc[shdr->sh_link];
1695 if ((isp == NULL) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
1696 (isp->is_shdr->sh_type != SHT_DYNSYM))) {
1697 ld_eprintf(ofl, ERR_FATAL,
1698 MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name,
1699 EC_WORD(ndx), name, EC_XWORD(shdr->sh_link));
1700 return (S_ERROR);
1702 isp->is_symshndx = ifl->ifl_isdesc[ndx];
1704 return (1);
1708 * Final processing for SHT_SYMTAB_SHNDX section.
1710 static uintptr_t
1711 /* ARGSUSED2 */
1712 sym_shndx_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
1714 if (isc->is_shdr->sh_link > isc->is_scnndx) {
1715 Is_desc *isp = ifl->ifl_isdesc[isc->is_shdr->sh_link];
1717 if ((isp == NULL) || ((isp->is_shdr->sh_type != SHT_SYMTAB) &&
1718 (isp->is_shdr->sh_type != SHT_DYNSYM))) {
1719 ld_eprintf(ofl, ERR_FATAL,
1720 MSG_INTL(MSG_FIL_INVSHLINK), isc->is_file->ifl_name,
1721 EC_WORD(isc->is_scnndx), isc->is_name,
1722 EC_XWORD(isc->is_shdr->sh_link));
1723 return (S_ERROR);
1725 isp->is_symshndx = isc;
1727 return (1);
1731 * Process .dynamic section from a relocatable object.
1733 * Note: That the .dynamic section is only considered interesting when
1734 * dlopen()ing a relocatable object (thus FLG_OF1_RELDYN can only get
1735 * set when libld is called from ld.so.1).
1737 /*ARGSUSED*/
1738 static uintptr_t
1739 process_rel_dynamic(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
1740 Word ndx, int ident, Ofl_desc *ofl)
1742 Dyn *dyn;
1743 Elf_Scn *strscn;
1744 Elf_Data *dp;
1745 char *str;
1748 * Process .dynamic sections from relocatable objects ?
1750 if ((ofl->ofl_flags1 & FLG_OF1_RELDYN) == 0)
1751 return (1);
1754 * Find the string section associated with the .dynamic section.
1756 if ((strscn = elf_getscn(ifl->ifl_elf, shdr->sh_link)) == NULL) {
1757 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
1758 ifl->ifl_name);
1759 return (0);
1761 dp = elf_getdata(strscn, NULL);
1762 str = (char *)dp->d_buf;
1765 * And get the .dynamic data
1767 dp = elf_getdata(scn, NULL);
1769 for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
1770 Ifl_desc *difl;
1772 switch (dyn->d_tag) {
1773 case DT_NEEDED:
1774 case DT_USED:
1775 if (((difl = libld_calloc(1,
1776 sizeof (Ifl_desc))) == NULL) ||
1777 (aplist_append(&ofl->ofl_sos, difl,
1778 AL_CNT_OFL_LIBS) == NULL))
1779 return (S_ERROR);
1781 difl->ifl_name = MSG_ORIG(MSG_STR_DYNAMIC);
1782 difl->ifl_soname = str + (size_t)dyn->d_un.d_val;
1783 difl->ifl_flags = FLG_IF_NEEDSTR;
1784 break;
1785 case DT_RPATH:
1786 case DT_RUNPATH:
1787 if ((ofl->ofl_rpath = add_string(ofl->ofl_rpath,
1788 (str + (size_t)dyn->d_un.d_val))) ==
1789 (const char *)S_ERROR)
1790 return (S_ERROR);
1791 break;
1792 case DT_VERSYM:
1794 * The Solaris ld does not put DT_VERSYM in the
1795 * dynamic section. If the object has DT_VERSYM,
1796 * then it must have been produced by the GNU ld,
1797 * and is using the GNU style of versioning.
1799 ifl->ifl_flags |= FLG_IF_GNUVER;
1800 break;
1803 return (1);
1807 * Expand implicit references. Dependencies can be specified in terms of the
1808 * $ORIGIN, $MACHINE, $PLATFORM, $OSREL and $OSNAME tokens, either from their
1809 * needed name, or via a runpath. In addition runpaths may also specify the
1810 * $ISALIST token.
1812 * Probably the most common reference to explicit dependencies (via -L) will be
1813 * sufficient to find any associated implicit dependencies, but just in case we
1814 * expand any occurrence of these known tokens here.
1816 * Note, if any errors occur we simply return the original name.
1818 * This code is remarkably similar to expand() in rtld/common/paths.c.
1820 static char *machine = NULL;
1821 static size_t machine_sz = 0;
1822 static char *platform = NULL;
1823 static size_t platform_sz = 0;
1824 static Isa_desc *isa = NULL;
1825 static Uts_desc *uts = NULL;
1827 static char *
1828 expand(const char *parent, const char *name, char **next)
1830 char _name[PATH_MAX], *nptr, *_next;
1831 const char *optr;
1832 size_t nrem = PATH_MAX - 1;
1833 int expanded = 0, _expanded, isaflag = 0;
1835 optr = name;
1836 nptr = _name;
1838 while (*optr) {
1839 if (nrem == 0)
1840 return ((char *)name);
1842 if (*optr != '$') {
1843 *nptr++ = *optr++, nrem--;
1844 continue;
1847 _expanded = 0;
1849 if (strncmp(optr, MSG_ORIG(MSG_STR_ORIGIN),
1850 MSG_STR_ORIGIN_SIZE) == 0) {
1851 char *eptr;
1854 * For $ORIGIN, expansion is really just a concatenation
1855 * of the parents directory name. For example, an
1856 * explicit dependency foo/bar/lib1.so with a dependency
1857 * on $ORIGIN/lib2.so would be expanded to
1858 * foo/bar/lib2.so.
1860 if ((eptr = strrchr(parent, '/')) == NULL) {
1861 *nptr++ = '.';
1862 nrem--;
1863 } else {
1864 size_t len = eptr - parent;
1866 if (len >= nrem)
1867 return ((char *)name);
1869 (void) strncpy(nptr, parent, len);
1870 nptr = nptr + len;
1871 nrem -= len;
1873 optr += MSG_STR_ORIGIN_SIZE;
1874 expanded = _expanded = 1;
1876 } else if (strncmp(optr, MSG_ORIG(MSG_STR_MACHINE),
1877 MSG_STR_MACHINE_SIZE) == 0) {
1879 * Establish the machine from sysconf - like uname -i.
1881 if ((machine == NULL) && (machine_sz == 0)) {
1882 char info[SYS_NMLN];
1883 long size;
1885 size = sysinfo(SI_MACHINE, info, SYS_NMLN);
1886 if ((size != -1) &&
1887 (machine = libld_malloc((size_t)size))) {
1888 (void) strcpy(machine, info);
1889 machine_sz = (size_t)size - 1;
1890 } else
1891 machine_sz = 1;
1893 if (machine) {
1894 if (machine_sz >= nrem)
1895 return ((char *)name);
1897 (void) strncpy(nptr, machine, machine_sz);
1898 nptr = nptr + machine_sz;
1899 nrem -= machine_sz;
1901 optr += MSG_STR_MACHINE_SIZE;
1902 expanded = _expanded = 1;
1905 } else if (strncmp(optr, MSG_ORIG(MSG_STR_PLATFORM),
1906 MSG_STR_PLATFORM_SIZE) == 0) {
1908 * Establish the platform from sysconf - like uname -i.
1910 if ((platform == NULL) && (platform_sz == 0)) {
1911 char info[SYS_NMLN];
1912 long size;
1914 size = sysinfo(SI_PLATFORM, info, SYS_NMLN);
1915 if ((size != -1) &&
1916 (platform = libld_malloc((size_t)size))) {
1917 (void) strcpy(platform, info);
1918 platform_sz = (size_t)size - 1;
1919 } else
1920 platform_sz = 1;
1922 if (platform) {
1923 if (platform_sz >= nrem)
1924 return ((char *)name);
1926 (void) strncpy(nptr, platform, platform_sz);
1927 nptr = nptr + platform_sz;
1928 nrem -= platform_sz;
1930 optr += MSG_STR_PLATFORM_SIZE;
1931 expanded = _expanded = 1;
1934 } else if (strncmp(optr, MSG_ORIG(MSG_STR_OSNAME),
1935 MSG_STR_OSNAME_SIZE) == 0) {
1937 * Establish the os name - like uname -s.
1939 if (uts == NULL)
1940 uts = conv_uts();
1942 if (uts && uts->uts_osnamesz) {
1943 if (uts->uts_osnamesz >= nrem)
1944 return ((char *)name);
1946 (void) strncpy(nptr, uts->uts_osname,
1947 uts->uts_osnamesz);
1948 nptr = nptr + uts->uts_osnamesz;
1949 nrem -= uts->uts_osnamesz;
1951 optr += MSG_STR_OSNAME_SIZE;
1952 expanded = _expanded = 1;
1955 } else if (strncmp(optr, MSG_ORIG(MSG_STR_OSREL),
1956 MSG_STR_OSREL_SIZE) == 0) {
1958 * Establish the os release - like uname -r.
1960 if (uts == NULL)
1961 uts = conv_uts();
1963 if (uts && uts->uts_osrelsz) {
1964 if (uts->uts_osrelsz >= nrem)
1965 return ((char *)name);
1967 (void) strncpy(nptr, uts->uts_osrel,
1968 uts->uts_osrelsz);
1969 nptr = nptr + uts->uts_osrelsz;
1970 nrem -= uts->uts_osrelsz;
1972 optr += MSG_STR_OSREL_SIZE;
1973 expanded = _expanded = 1;
1976 } else if ((strncmp(optr, MSG_ORIG(MSG_STR_ISALIST),
1977 MSG_STR_ISALIST_SIZE) == 0) && next && (isaflag++ == 0)) {
1979 * Establish instruction sets from sysconf. Note that
1980 * this is only meaningful from runpaths.
1982 if (isa == NULL)
1983 isa = conv_isalist();
1985 if (isa && isa->isa_listsz &&
1986 (nrem > isa->isa_opt->isa_namesz)) {
1987 size_t mlen, tlen, hlen = optr - name;
1988 size_t no;
1989 char *lptr;
1990 Isa_opt *opt = isa->isa_opt;
1992 (void) strncpy(nptr, opt->isa_name,
1993 opt->isa_namesz);
1994 nptr = nptr + opt->isa_namesz;
1995 nrem -= opt->isa_namesz;
1997 optr += MSG_STR_ISALIST_SIZE;
1998 expanded = _expanded = 1;
2000 tlen = strlen(optr);
2003 * As ISALIST expands to a number of elements,
2004 * establish a new list to return to the caller.
2005 * This will contain the present path being
2006 * processed redefined for each isalist option,
2007 * plus the original remaining list entries.
2009 mlen = ((hlen + tlen) * (isa->isa_optno - 1)) +
2010 isa->isa_listsz - opt->isa_namesz;
2011 if (*next)
2012 mlen += strlen(*next);
2013 if ((_next = lptr = libld_malloc(mlen)) == NULL)
2014 return (0);
2016 for (no = 1, opt++; no < isa->isa_optno;
2017 no++, opt++) {
2018 (void) strncpy(lptr, name, hlen);
2019 lptr = lptr + hlen;
2020 (void) strncpy(lptr, opt->isa_name,
2021 opt->isa_namesz);
2022 lptr = lptr + opt->isa_namesz;
2023 (void) strncpy(lptr, optr, tlen);
2024 lptr = lptr + tlen;
2025 *lptr++ = ':';
2027 if (*next)
2028 (void) strcpy(lptr, *next);
2029 else
2030 *--lptr = '\0';
2035 * If no expansion occurred skip the $ and continue.
2037 if (_expanded == 0)
2038 *nptr++ = *optr++, nrem--;
2042 * If any ISALIST processing has occurred not only do we return the
2043 * expanded node we're presently working on, but we must also update the
2044 * remaining list so that it is effectively prepended with this node
2045 * expanded to all remaining isalist options. Note that we can only
2046 * handle one ISALIST per node. For more than one ISALIST to be
2047 * processed we'd need a better algorithm than above to replace the
2048 * newly generated list. Whether we want to encourage the number of
2049 * pathname permutations this would provide is another question. So, for
2050 * now if more than one ISALIST is encountered we return the original
2051 * node untouched.
2053 if (isaflag) {
2054 if (isaflag == 1)
2055 *next = _next;
2056 else
2057 return ((char *)name);
2060 *nptr = '\0';
2062 if (expanded) {
2063 if ((nptr = libld_malloc(strlen(_name) + 1)) == NULL)
2064 return ((char *)name);
2065 (void) strcpy(nptr, _name);
2066 return (nptr);
2068 return ((char *)name);
2072 * The Solaris ld does not put DT_VERSYM in the dynamic section, but the
2073 * GNU ld does, and it is used by the runtime linker to implement their
2074 * versioning scheme. Use this fact to determine if the sharable object
2075 * was produced by the GNU ld rather than the Solaris one, and to set
2076 * FLG_IF_GNUVER if so. This needs to be done before the symbols are
2077 * processed, since the answer determines whether we interpret the
2078 * symbols versions according to Solaris or GNU rules.
2080 /*ARGSUSED*/
2081 static uintptr_t
2082 process_dynamic_isgnu(const char *name, Ifl_desc *ifl, Shdr *shdr,
2083 Elf_Scn *scn, Word ndx, int ident, Ofl_desc *ofl)
2085 Dyn *dyn;
2086 Elf_Data *dp;
2087 uintptr_t error;
2089 error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
2090 if ((error == 0) || (error == S_ERROR))
2091 return (error);
2093 /* Get the .dynamic data */
2094 dp = elf_getdata(scn, NULL);
2096 for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) {
2097 if (dyn->d_tag == DT_VERSYM) {
2098 ifl->ifl_flags |= FLG_IF_GNUVER;
2099 break;
2102 return (1);
2106 * Process a dynamic section. If we are processing an explicit shared object
2107 * then we need to determine if it has a recorded SONAME, if so, this name will
2108 * be recorded in the output file being generated as the NEEDED entry rather
2109 * than the shared objects filename itself.
2110 * If the mode of the link-edit indicates that no undefined symbols should
2111 * remain, then we also need to build up a list of any additional shared object
2112 * dependencies this object may have. In this case save any NEEDED entries
2113 * together with any associated run-path specifications. This information is
2114 * recorded on the `ofl_soneed' list and will be analyzed after all explicit
2115 * file processing has been completed (refer finish_libs()).
2117 static uintptr_t
2118 process_dynamic(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
2120 Dyn *data, *dyn;
2121 char *str, *rpath = NULL;
2122 const char *soname, *needed;
2123 Boolean no_undef;
2125 data = (Dyn *)isc->is_indata->d_buf;
2126 str = (char *)ifl->ifl_isdesc[isc->is_shdr->sh_link]->is_indata->d_buf;
2128 /* Determine if we need to examine the runpaths and NEEDED entries */
2129 no_undef = (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) ||
2130 OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS);
2133 * First loop through the dynamic section looking for a run path.
2135 if (no_undef) {
2136 for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
2137 if ((dyn->d_tag != DT_RPATH) &&
2138 (dyn->d_tag != DT_RUNPATH))
2139 continue;
2140 if ((rpath = str + (size_t)dyn->d_un.d_val) == NULL)
2141 continue;
2142 break;
2147 * Now look for any needed dependencies (which may use the rpath)
2148 * or a new SONAME.
2150 for (dyn = data; dyn->d_tag != DT_NULL; dyn++) {
2151 if (dyn->d_tag == DT_SONAME) {
2152 if ((soname = str + (size_t)dyn->d_un.d_val) == NULL)
2153 continue;
2156 * Update the input file structure with this new name.
2158 ifl->ifl_soname = soname;
2160 } else if ((dyn->d_tag == DT_NEEDED) ||
2161 (dyn->d_tag == DT_USED)) {
2162 Sdf_desc *sdf;
2164 if (!no_undef)
2165 continue;
2166 if ((needed = str + (size_t)dyn->d_un.d_val) == NULL)
2167 continue;
2170 * Determine if this needed entry is already recorded on
2171 * the shared object needed list, if not create a new
2172 * definition for later processing (see finish_libs()).
2174 needed = expand(ifl->ifl_name, needed, NULL);
2176 if ((sdf = sdf_find(needed, ofl->ofl_soneed)) == NULL) {
2177 if ((sdf = sdf_add(needed,
2178 &ofl->ofl_soneed)) == (Sdf_desc *)S_ERROR)
2179 return (S_ERROR);
2180 sdf->sdf_rfile = ifl->ifl_name;
2184 * Record the runpath (Note that we take the first
2185 * runpath which is exactly what ld.so.1 would do during
2186 * its dependency processing).
2188 if (rpath && (sdf->sdf_rpath == NULL))
2189 sdf->sdf_rpath = rpath;
2191 } else if (dyn->d_tag == DT_FLAGS_1) {
2192 if (dyn->d_un.d_val & (DF_1_INITFIRST | DF_1_INTERPOSE))
2193 ifl->ifl_flags &= ~FLG_IF_LAZYLD;
2194 if (dyn->d_un.d_val & DF_1_DISPRELPND)
2195 ifl->ifl_flags |= FLG_IF_DISPPEND;
2196 if (dyn->d_un.d_val & DF_1_DISPRELDNE)
2197 ifl->ifl_flags |= FLG_IF_DISPDONE;
2198 if (dyn->d_un.d_val & DF_1_NODIRECT)
2199 ifl->ifl_flags |= FLG_IF_NODIRECT;
2202 * If we are building an executable, and this
2203 * dependency is tagged as an interposer, then
2204 * assume that it is required even if symbol
2205 * resolution uncovers no evident use.
2207 * If we are building a shared object, then an
2208 * interposer dependency has no special meaning, and we
2209 * treat it as a regular dependency. By definition, all
2210 * interposers must be visible to the runtime linker
2211 * at initialization time, and cannot be added later.
2213 if ((dyn->d_un.d_val & DF_1_INTERPOSE) &&
2214 (ofl->ofl_flags & (FLG_OF_EXEC | FLG_OF_PIE)))
2215 ifl->ifl_flags |= FLG_IF_DEPREQD;
2217 } else if ((dyn->d_tag == DT_AUDIT) &&
2218 (ifl->ifl_flags & FLG_IF_NEEDED)) {
2220 * Record audit string as DT_DEPAUDIT.
2222 if ((ofl->ofl_depaudit = add_string(ofl->ofl_depaudit,
2223 (str + (size_t)dyn->d_un.d_val))) ==
2224 (const char *)S_ERROR)
2225 return (S_ERROR);
2227 } else if (dyn->d_tag == DT_SUNW_RTLDINF) {
2229 * If this dependency has the DT_SUNW_RTLDINF .dynamic
2230 * entry, then ensure no specialized dependency
2231 * processing is in effect. This tag identifies libc,
2232 * which provides critical startup information (TLS
2233 * routines, threads initialization, etc.) that must
2234 * be exercised as part of process initialization.
2236 ifl->ifl_flags &= ~MSK_IF_POSFLAG1;
2239 * libc is not subject to the usual guidance checks
2240 * for lazy loading. It cannot be lazy loaded, libld
2241 * ignores the request, and rtld would ignore the
2242 * setting if it were present.
2244 ifl->ifl_flags |= FLG_IF_RTLDINF;
2249 * Perform some SONAME sanity checks.
2251 if (ifl->ifl_flags & FLG_IF_NEEDED) {
2252 Ifl_desc *sifl;
2253 Aliste idx;
2256 * Determine if anyone else will cause the same SONAME to be
2257 * used (this is either caused by two different files having the
2258 * same SONAME, or by one file SONAME actually matching another
2259 * file basename (if no SONAME is specified within a shared
2260 * library its basename will be used)). Probably rare, but some
2261 * idiot will do it.
2263 for (APLIST_TRAVERSE(ofl->ofl_sos, idx, sifl)) {
2264 if ((strcmp(ifl->ifl_soname, sifl->ifl_soname) == 0) &&
2265 (ifl != sifl)) {
2266 const char *hint, *iflb, *siflb;
2269 * Determine the basename of each file. Perhaps
2270 * there are multiple copies of the same file
2271 * being brought in using different -L search
2272 * paths, and if so give an extra hint in the
2273 * error message.
2275 iflb = strrchr(ifl->ifl_name, '/');
2276 if (iflb == NULL)
2277 iflb = ifl->ifl_name;
2278 else
2279 iflb++;
2281 siflb = strrchr(sifl->ifl_name, '/');
2282 if (siflb == NULL)
2283 siflb = sifl->ifl_name;
2284 else
2285 siflb++;
2287 if (strcmp(iflb, siflb) == 0)
2288 hint = MSG_INTL(MSG_REC_CNFLTHINT);
2289 else
2290 hint = MSG_ORIG(MSG_STR_EMPTY);
2292 ld_eprintf(ofl, ERR_FATAL,
2293 MSG_INTL(MSG_REC_OBJCNFLT), sifl->ifl_name,
2294 ifl->ifl_name, sifl->ifl_soname, hint);
2295 return (0);
2300 * If the SONAME is the same as the name the user wishes to
2301 * record when building a dynamic library (refer -h option),
2302 * we also have a name clash.
2304 if (ofl->ofl_soname &&
2305 (strcmp(ofl->ofl_soname, ifl->ifl_soname) == 0)) {
2306 ld_eprintf(ofl, ERR_FATAL,
2307 MSG_INTL(MSG_REC_OPTCNFLT), ifl->ifl_name,
2308 MSG_INTL(MSG_MARG_SONAME), ifl->ifl_soname);
2309 return (0);
2312 return (1);
2316 * Process a progbits section from a relocatable object (ET_REL).
2317 * This is used on non-amd64 objects to recognize .eh_frame sections.
2319 /*ARGSUSED1*/
2320 static uintptr_t
2321 process_progbits_final(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
2323 if (isc->is_osdesc && (isc->is_flags & FLG_IS_EHFRAME) &&
2324 (ld_unwind_register(isc->is_osdesc, ofl) == S_ERROR))
2325 return (S_ERROR);
2327 return (1);
2331 * Process a group section.
2333 static uintptr_t
2334 process_group(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
2335 Word ndx, int ident, Ofl_desc *ofl)
2337 uintptr_t error;
2339 error = process_section(name, ifl, shdr, scn, ndx, ident, ofl);
2340 if ((error == 0) || (error == S_ERROR))
2341 return (error);
2344 * Indicate that this input file has groups to process. Groups are
2345 * processed after all input sections have been processed.
2347 ifl->ifl_flags |= FLG_IF_GROUPS;
2349 return (1);
2353 * Process a relocation entry. At this point all input sections from this
2354 * input file have been assigned an input section descriptor which is saved
2355 * in the `ifl_isdesc' array.
2357 static uintptr_t
2358 rel_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl)
2360 Word rndx;
2361 Is_desc *risc;
2362 Os_desc *osp;
2363 Shdr *shdr = isc->is_shdr;
2364 Conv_inv_buf_t inv_buf;
2367 * Make sure this is a valid relocation we can handle.
2369 if (shdr->sh_type != ld_targ.t_m.m_rel_sht_type) {
2370 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVALSEC),
2371 ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
2372 conv_sec_type(ifl->ifl_ehdr->e_ident[EI_OSABI],
2373 ifl->ifl_ehdr->e_machine, shdr->sh_type, 0, &inv_buf));
2374 return (0);
2378 * From the relocation section header information determine which
2379 * section needs the actual relocation. Determine which output section
2380 * this input section has been assigned to and add to its relocation
2381 * list. Note that the relocation section may be null if it is not
2382 * required (ie. .debug, .stabs, etc).
2384 rndx = shdr->sh_info;
2385 if (rndx >= ifl->ifl_shnum) {
2387 * Broken input file.
2389 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO),
2390 ifl->ifl_name, EC_WORD(isc->is_scnndx), isc->is_name,
2391 EC_XWORD(rndx));
2392 return (0);
2394 if (rndx == 0) {
2395 if (aplist_append(&ofl->ofl_extrarels, isc,
2396 AL_CNT_OFL_RELS) == NULL)
2397 return (S_ERROR);
2399 } else if ((risc = ifl->ifl_isdesc[rndx]) != NULL) {
2401 * Discard relocations if they are against a section
2402 * which has been discarded.
2404 if (risc->is_flags & FLG_IS_DISCARD)
2405 return (1);
2407 if ((osp = risc->is_osdesc) == NULL) {
2408 if (risc->is_shdr->sh_type == SHT_SUNW_move) {
2410 * This section is processed later in
2411 * process_movereloc().
2413 if (aplist_append(&ofl->ofl_ismoverel,
2414 isc, AL_CNT_OFL_MOVE) == NULL)
2415 return (S_ERROR);
2416 return (1);
2418 ld_eprintf(ofl, ERR_FATAL,
2419 MSG_INTL(MSG_FIL_INVRELOC1), ifl->ifl_name,
2420 EC_WORD(isc->is_scnndx), isc->is_name,
2421 EC_WORD(risc->is_scnndx), risc->is_name);
2422 return (0);
2424 if (aplist_append(&osp->os_relisdescs, isc,
2425 AL_CNT_OS_RELISDESCS) == NULL)
2426 return (S_ERROR);
2428 return (1);
2432 * SHF_EXCLUDE flags is set for this section.
2434 static uintptr_t
2435 process_exclude(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn,
2436 Word ndx, Ofl_desc *ofl)
2439 * Sections SHT_SYMTAB and SHT_DYNDYM, even if SHF_EXCLUDE is on, might
2440 * be needed for ld processing. These sections need to be in the
2441 * internal table. Later it will be determined whether they can be
2442 * eliminated or not.
2444 if (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM)
2445 return (0);
2448 * Other checks
2450 if (shdr->sh_flags & SHF_ALLOC) {
2452 * A conflict, issue an warning message, and ignore the section.
2454 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_FIL_EXCLUDE),
2455 ifl->ifl_name, EC_WORD(ndx), name);
2456 return (0);
2460 * This sections is not going to the output file.
2462 return (process_section(name, ifl, shdr, scn, ndx, 0, ofl));
2466 * Section processing state table. `Initial' describes the required initial
2467 * procedure to be called (if any), `Final' describes the final processing
2468 * procedure (ie. things that can only be done when all required sections
2469 * have been collected).
2471 typedef uintptr_t (* initial_func_t)(const char *, Ifl_desc *, Shdr *,
2472 Elf_Scn *, Word, int, Ofl_desc *);
2474 static initial_func_t Initial[SHT_NUM][2] = {
2475 /* ET_REL ET_DYN */
2477 /* SHT_NULL */ invalid_section, invalid_section,
2478 /* SHT_PROGBITS */ process_progbits, process_progbits,
2479 /* SHT_SYMTAB */ process_input, process_input,
2480 /* SHT_STRTAB */ process_strtab, process_strtab,
2481 /* SHT_RELA */ process_reloc, process_reloc,
2482 /* SHT_HASH */ invalid_section, NULL,
2483 /* SHT_DYNAMIC */ process_rel_dynamic, process_dynamic_isgnu,
2484 /* SHT_NOTE */ process_section, NULL,
2485 /* SHT_NOBITS */ process_nobits, process_nobits,
2486 /* SHT_REL */ process_reloc, process_reloc,
2487 /* SHT_SHLIB */ process_section, invalid_section,
2488 /* SHT_DYNSYM */ invalid_section, process_input,
2489 /* SHT_UNKNOWN12 */ process_progbits, process_progbits,
2490 /* SHT_UNKNOWN13 */ process_progbits, process_progbits,
2491 /* SHT_INIT_ARRAY */ process_array, NULL,
2492 /* SHT_FINI_ARRAY */ process_array, NULL,
2493 /* SHT_PREINIT_ARRAY */ process_array, NULL,
2494 /* SHT_GROUP */ process_group, invalid_section,
2495 /* SHT_SYMTAB_SHNDX */ process_sym_shndx, NULL
2498 typedef uintptr_t (* final_func_t)(Is_desc *, Ifl_desc *, Ofl_desc *);
2500 static final_func_t Final[SHT_NUM][2] = {
2501 /* ET_REL ET_DYN */
2503 /* SHT_NULL */ NULL, NULL,
2504 /* SHT_PROGBITS */ process_progbits_final, NULL,
2505 /* SHT_SYMTAB */ ld_sym_process, ld_sym_process,
2506 /* SHT_STRTAB */ NULL, NULL,
2507 /* SHT_RELA */ rel_process, NULL,
2508 /* SHT_HASH */ NULL, NULL,
2509 /* SHT_DYNAMIC */ NULL, process_dynamic,
2510 /* SHT_NOTE */ NULL, NULL,
2511 /* SHT_NOBITS */ NULL, NULL,
2512 /* SHT_REL */ rel_process, NULL,
2513 /* SHT_SHLIB */ NULL, NULL,
2514 /* SHT_DYNSYM */ NULL, ld_sym_process,
2515 /* SHT_UNKNOWN12 */ NULL, NULL,
2516 /* SHT_UNKNOWN13 */ NULL, NULL,
2517 /* SHT_INIT_ARRAY */ array_process, NULL,
2518 /* SHT_FINI_ARRAY */ array_process, NULL,
2519 /* SHT_PREINIT_ARRAY */ array_process, NULL,
2520 /* SHT_GROUP */ NULL, NULL,
2521 /* SHT_SYMTAB_SHNDX */ sym_shndx_process, NULL
2524 #define MAXNDXSIZE 10
2527 * Process an elf file. Each section is compared against the section state
2528 * table to determine whether it should be processed (saved), ignored, or
2529 * is invalid for the type of input file being processed.
2531 static uintptr_t
2532 process_elf(Ifl_desc *ifl, Elf *elf, Ofl_desc *ofl)
2534 Elf_Scn *scn;
2535 Shdr *shdr;
2536 Word ndx, sndx, ordndx = 0, ordcnt = 0;
2537 char *str, *name;
2538 Word row, column;
2539 int ident;
2540 uintptr_t error;
2541 Is_desc *vdfisp, *vndisp, *vsyisp, *sifisp;
2542 Is_desc *capinfoisp, *capisp;
2543 Sdf_desc *sdf;
2544 Place_path_info path_info_buf, *path_info;
2547 * Path information buffer used by ld_place_section() and related
2548 * routines. This information is used to evaluate entrance criteria
2549 * with non-empty file matching lists (ec_files).
2551 path_info = ld_place_path_info_init(ofl, ifl, &path_info_buf);
2554 * First process the .shstrtab section so that later sections can
2555 * reference their name.
2557 ld_sup_file(ofl, ifl->ifl_name, elf_kind(elf), ifl->ifl_flags, elf);
2559 sndx = ifl->ifl_shstrndx;
2560 if ((scn = elf_getscn(elf, (size_t)sndx)) == NULL) {
2561 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN),
2562 ifl->ifl_name);
2563 return (0);
2565 if ((shdr = elf_getshdr(scn)) == NULL) {
2566 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
2567 ifl->ifl_name);
2568 return (0);
2570 if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
2571 NULL) {
2572 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
2573 ifl->ifl_name);
2574 return (0);
2577 if (ld_sup_input_section(ofl, ifl, name, &shdr, sndx, scn,
2578 elf) == S_ERROR)
2579 return (S_ERROR);
2582 * Reset the name since the shdr->sh_name could have been changed as
2583 * part of ld_sup_input_section().
2585 if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) ==
2586 NULL) {
2587 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR),
2588 ifl->ifl_name);
2589 return (0);
2592 error = process_strtab(name, ifl, shdr, scn, sndx, FALSE, ofl);
2593 if ((error == 0) || (error == S_ERROR))
2594 return (error);
2595 str = ifl->ifl_isdesc[sndx]->is_indata->d_buf;
2598 * Determine the state table column from the input file type. Note,
2599 * shared library sections are not added to the output section list.
2601 if (ifl->ifl_ehdr->e_type == ET_DYN) {
2602 column = 1;
2603 ofl->ofl_soscnt++;
2604 ident = ld_targ.t_id.id_null;
2605 } else {
2606 column = 0;
2607 ofl->ofl_objscnt++;
2608 ident = ld_targ.t_id.id_unknown;
2611 DBG_CALL(Dbg_file_generic(ofl->ofl_lml, ifl));
2612 ndx = 0;
2613 vdfisp = vndisp = vsyisp = sifisp = capinfoisp = capisp = NULL;
2614 scn = NULL;
2615 while (scn = elf_nextscn(elf, scn)) {
2616 ndx++;
2619 * As we've already processed the .shstrtab don't do it again.
2621 if (ndx == sndx)
2622 continue;
2624 if ((shdr = elf_getshdr(scn)) == NULL) {
2625 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR),
2626 ifl->ifl_name);
2627 return (0);
2629 name = str + (size_t)(shdr->sh_name);
2631 if (ld_sup_input_section(ofl, ifl, name, &shdr, ndx, scn,
2632 elf) == S_ERROR)
2633 return (S_ERROR);
2636 * Reset the name since the shdr->sh_name could have been
2637 * changed as part of ld_sup_input_section().
2639 name = str + (size_t)(shdr->sh_name);
2641 row = shdr->sh_type;
2644 * If the section has the SHF_EXCLUDE flag on, and we're not
2645 * generating a relocatable object, exclude the section.
2647 if (((shdr->sh_flags & SHF_EXCLUDE) != 0) &&
2648 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) {
2649 if ((error = process_exclude(name, ifl, shdr, scn,
2650 ndx, ofl)) == S_ERROR)
2651 return (S_ERROR);
2652 if (error == 1)
2653 continue;
2657 * If this is a standard section type process it via the
2658 * appropriate action routine.
2660 if (row < SHT_NUM) {
2661 if (Initial[row][column] != NULL) {
2662 if (Initial[row][column](name, ifl, shdr, scn,
2663 ndx, ident, ofl) == S_ERROR)
2664 return (S_ERROR);
2666 } else {
2668 * If this section is below SHT_LOSUNW then we don't
2669 * really know what to do with it, issue a warning
2670 * message but do the basic section processing anyway.
2672 if (row < (Word)SHT_LOSUNW) {
2673 Conv_inv_buf_t inv_buf;
2675 ld_eprintf(ofl, ERR_WARNING,
2676 MSG_INTL(MSG_FIL_INVALSEC), ifl->ifl_name,
2677 EC_WORD(ndx), name, conv_sec_type(
2678 ifl->ifl_ehdr->e_ident[EI_OSABI],
2679 ifl->ifl_ehdr->e_machine,
2680 shdr->sh_type, 0, &inv_buf));
2684 * Handle sections greater than SHT_LOSUNW.
2686 switch (row) {
2687 case SHT_SUNW_dof:
2688 if (process_section(name, ifl, shdr, scn,
2689 ndx, ident, ofl) == S_ERROR)
2690 return (S_ERROR);
2691 break;
2692 case SHT_SUNW_cap:
2693 if (process_section(name, ifl, shdr, scn, ndx,
2694 ld_targ.t_id.id_null, ofl) == S_ERROR)
2695 return (S_ERROR);
2696 capisp = ifl->ifl_isdesc[ndx];
2697 break;
2698 case SHT_SUNW_capinfo:
2699 if (process_section(name, ifl, shdr, scn, ndx,
2700 ld_targ.t_id.id_null, ofl) == S_ERROR)
2701 return (S_ERROR);
2702 capinfoisp = ifl->ifl_isdesc[ndx];
2703 break;
2704 case SHT_SUNW_DEBUGSTR:
2705 case SHT_SUNW_DEBUG:
2706 if (process_debug(name, ifl, shdr, scn,
2707 ndx, ident, ofl) == S_ERROR)
2708 return (S_ERROR);
2709 break;
2710 case SHT_SUNW_move:
2711 if (process_section(name, ifl, shdr, scn, ndx,
2712 ld_targ.t_id.id_null, ofl) == S_ERROR)
2713 return (S_ERROR);
2714 break;
2715 case SHT_SUNW_syminfo:
2716 if (process_section(name, ifl, shdr, scn, ndx,
2717 ld_targ.t_id.id_null, ofl) == S_ERROR)
2718 return (S_ERROR);
2719 sifisp = ifl->ifl_isdesc[ndx];
2720 break;
2721 case SHT_SUNW_ANNOTATE:
2722 if (process_progbits(name, ifl, shdr, scn,
2723 ndx, ident, ofl) == S_ERROR)
2724 return (S_ERROR);
2725 break;
2726 case SHT_SUNW_COMDAT:
2727 if (process_progbits(name, ifl, shdr, scn,
2728 ndx, ident, ofl) == S_ERROR)
2729 return (S_ERROR);
2730 ifl->ifl_isdesc[ndx]->is_flags |= FLG_IS_COMDAT;
2731 break;
2732 case SHT_SUNW_verdef:
2733 if (process_section(name, ifl, shdr, scn, ndx,
2734 ld_targ.t_id.id_null, ofl) == S_ERROR)
2735 return (S_ERROR);
2736 vdfisp = ifl->ifl_isdesc[ndx];
2737 break;
2738 case SHT_SUNW_verneed:
2739 if (process_section(name, ifl, shdr, scn, ndx,
2740 ld_targ.t_id.id_null, ofl) == S_ERROR)
2741 return (S_ERROR);
2742 vndisp = ifl->ifl_isdesc[ndx];
2743 break;
2744 case SHT_SUNW_versym:
2745 if (process_section(name, ifl, shdr, scn, ndx,
2746 ld_targ.t_id.id_null, ofl) == S_ERROR)
2747 return (S_ERROR);
2748 vsyisp = ifl->ifl_isdesc[ndx];
2749 break;
2750 case SHT_SPARC_GOTDATA:
2752 * SHT_SPARC_GOTDATA (0x70000000) is in the
2753 * SHT_LOPROC - SHT_HIPROC range reserved
2754 * for processor-specific semantics. It is
2755 * only meaningful for sparc targets.
2757 if (ld_targ.t_m.m_mach !=
2758 LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9))
2759 goto do_default;
2760 if (process_section(name, ifl, shdr, scn, ndx,
2761 ld_targ.t_id.id_gotdata, ofl) == S_ERROR)
2762 return (S_ERROR);
2763 break;
2764 #if defined(_ELF64)
2765 case SHT_AMD64_UNWIND:
2767 * SHT_AMD64_UNWIND (0x70000001) is in the
2768 * SHT_LOPROC - SHT_HIPROC range reserved
2769 * for processor-specific semantics. It is
2770 * only meaningful for amd64 targets.
2772 if (ld_targ.t_m.m_mach != EM_AMD64)
2773 goto do_default;
2776 * Target is x86, so this really is
2777 * SHT_AMD64_UNWIND
2779 if (column == 0) {
2781 * column == ET_REL
2783 if (process_section(name, ifl, shdr,
2784 scn, ndx, ld_targ.t_id.id_unwind,
2785 ofl) == S_ERROR)
2786 return (S_ERROR);
2787 ifl->ifl_isdesc[ndx]->is_flags |=
2788 FLG_IS_EHFRAME;
2790 break;
2791 #endif
2792 default:
2793 do_default:
2794 if (process_section(name, ifl, shdr, scn, ndx,
2795 ((ident == ld_targ.t_id.id_null) ?
2796 ident : ld_targ.t_id.id_user), ofl) ==
2797 S_ERROR)
2798 return (S_ERROR);
2799 break;
2805 * Now that all input sections have been analyzed, and prior to placing
2806 * any input sections to their output sections, process any groups.
2807 * Groups can contribute COMDAT items, which may get discarded as part
2808 * of placement. In addition, COMDAT names may require transformation
2809 * to indicate different output section placement.
2811 if (ifl->ifl_flags & FLG_IF_GROUPS) {
2812 for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
2813 Is_desc *isp;
2815 if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
2816 (isp->is_shdr->sh_type != SHT_GROUP))
2817 continue;
2819 if (ld_group_process(isp, ofl) == S_ERROR)
2820 return (S_ERROR);
2825 * Now group information has been processed, we can safely validate
2826 * that nothing is fishy about the section COMDAT description. We
2827 * need to do this prior to placing the section (where any
2828 * SHT_SUNW_COMDAT sections will be restored to being PROGBITS)
2830 ld_comdat_validate(ofl, ifl);
2833 * Now that all of the input sections have been processed, place
2834 * them in the appropriate output sections.
2836 for (ndx = 1; ndx < ifl->ifl_shnum; ndx++) {
2837 Is_desc *isp;
2839 if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
2840 ((isp->is_flags & FLG_IS_PLACE) == 0))
2841 continue;
2844 * Place all non-ordered sections within their appropriate
2845 * output section.
2847 if ((isp->is_flags & FLG_IS_ORDERED) == 0) {
2848 if (ld_place_section(ofl, isp, path_info,
2849 isp->is_keyident, NULL) == (Os_desc *)S_ERROR)
2850 return (S_ERROR);
2851 continue;
2855 * Count the number of ordered sections and retain the first
2856 * ordered section index. This will be used to optimize the
2857 * ordered section loop that immediately follows this one.
2859 ordcnt++;
2860 if (ordndx == 0)
2861 ordndx = ndx;
2865 * Having placed all the non-ordered sections, it is now
2866 * safe to place SHF_ORDERED/SHF_LINK_ORDER sections.
2868 if (ifl->ifl_flags & FLG_IF_ORDERED) {
2869 for (ndx = ordndx; ndx < ifl->ifl_shnum; ndx++) {
2870 Is_desc *isp;
2872 if (((isp = ifl->ifl_isdesc[ndx]) == NULL) ||
2873 ((isp->is_flags &
2874 (FLG_IS_PLACE | FLG_IS_ORDERED)) !=
2875 (FLG_IS_PLACE | FLG_IS_ORDERED)))
2876 continue;
2878 /* ld_process_ordered() calls ld_place_section() */
2879 if (ld_process_ordered(ofl, ifl, path_info, ndx) ==
2880 S_ERROR)
2881 return (S_ERROR);
2883 /* If we've done them all, stop searching */
2884 if (--ordcnt == 0)
2885 break;
2890 * If this is a shared object explicitly specified on the command
2891 * line (as opposed to being a dependency of such an object),
2892 * determine if the user has specified a control definition. This
2893 * descriptor may specify which version definitions can be used
2894 * from this object. It may also update the dependency to USED and
2895 * supply an alternative SONAME.
2897 sdf = NULL;
2898 if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) {
2899 const char *base;
2902 * Use the basename of the input file (typically this is the
2903 * compilation environment name, ie. libfoo.so).
2905 if ((base = strrchr(ifl->ifl_name, '/')) == NULL)
2906 base = ifl->ifl_name;
2907 else
2908 base++;
2910 if ((sdf = sdf_find(base, ofl->ofl_socntl)) != NULL) {
2911 sdf->sdf_file = ifl;
2912 ifl->ifl_sdfdesc = sdf;
2917 * Before symbol processing, process any capabilities. Capabilities
2918 * can reference a string table, which is why this processing is
2919 * carried out after the initial section processing. Capabilities,
2920 * together with -z symbolcap, can require the conversion of global
2921 * symbols to local symbols.
2923 if (capisp && (process_cap(ofl, ifl, capisp) == S_ERROR))
2924 return (S_ERROR);
2927 * Process any version dependencies. These will establish shared object
2928 * `needed' entries in the same manner as will be generated from the
2929 * .dynamic's NEEDED entries.
2931 if (vndisp && ((ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) ||
2932 OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS)))
2933 if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR)
2934 return (S_ERROR);
2937 * Before processing any symbol resolution or relocations process any
2938 * version sections.
2940 if (vsyisp)
2941 (void) ld_vers_sym_process(ofl, vsyisp, ifl);
2943 if (ifl->ifl_versym &&
2944 (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT))))
2945 if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR)
2946 return (S_ERROR);
2949 * Having collected the appropriate sections carry out any additional
2950 * processing if necessary.
2952 for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) {
2953 Is_desc *isp;
2955 if ((isp = ifl->ifl_isdesc[ndx]) == NULL)
2956 continue;
2957 row = isp->is_shdr->sh_type;
2959 if ((isp->is_flags & FLG_IS_DISCARD) == 0)
2960 ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx,
2961 isp->is_indata, elf);
2964 * If this is a SHT_SUNW_move section from a relocatable file,
2965 * keep track of the section for later processing.
2967 if ((row == SHT_SUNW_move) && (column == 0)) {
2968 if (aplist_append(&(ofl->ofl_ismove), isp,
2969 AL_CNT_OFL_MOVE) == NULL)
2970 return (S_ERROR);
2974 * If this is a standard section type process it via the
2975 * appropriate action routine.
2977 if (row < SHT_NUM) {
2978 if (Final[row][column] != NULL) {
2979 if (Final[row][column](isp, ifl,
2980 ofl) == S_ERROR)
2981 return (S_ERROR);
2983 #if defined(_ELF64)
2984 } else if ((row == SHT_AMD64_UNWIND) && (column == 0)) {
2985 Os_desc *osp = isp->is_osdesc;
2988 * SHT_AMD64_UNWIND (0x70000001) is in the SHT_LOPROC -
2989 * SHT_HIPROC range reserved for processor-specific
2990 * semantics, and is only meaningful for amd64 targets.
2992 * Only process unwind contents from relocatable
2993 * objects.
2995 if (osp && (ld_targ.t_m.m_mach == EM_AMD64) &&
2996 (ld_unwind_register(osp, ofl) == S_ERROR))
2997 return (S_ERROR);
2998 #endif
3003 * Following symbol processing, if this relocatable object input file
3004 * provides symbol capabilities, tag the associated symbols so that
3005 * the symbols can be re-assigned to the new capabilities symbol
3006 * section that will be created for the output file.
3008 if (capinfoisp && (ifl->ifl_ehdr->e_type == ET_REL) &&
3009 (process_capinfo(ofl, ifl, capinfoisp) == S_ERROR))
3010 return (S_ERROR);
3013 * After processing any symbol resolution, and if this dependency
3014 * indicates it contains symbols that can't be directly bound to,
3015 * set the symbols appropriately.
3017 if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) ==
3018 (FLG_IF_NEEDED | FLG_IF_NODIRECT)))
3019 (void) ld_sym_nodirect(sifisp, ifl, ofl);
3021 return (1);
3025 * Process the current input file. There are basically three types of files
3026 * that come through here:
3028 * - files explicitly defined on the command line (ie. foo.o or bar.so),
3029 * in this case only the `name' field is valid.
3031 * - libraries determined from the -l command line option (ie. -lbar),
3032 * in this case the `soname' field contains the basename of the located
3033 * file.
3035 * Any shared object specified via the above two conventions must be recorded
3036 * as a needed dependency.
3038 * - libraries specified as dependencies of those libraries already obtained
3039 * via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1),
3040 * in this case the `soname' field contains either a full pathname (if the
3041 * needed entry contained a `/'), or the basename of the located file.
3042 * These libraries are processed to verify symbol binding but are not
3043 * recorded as dependencies of the output file being generated.
3045 * entry:
3046 * name - File name
3047 * soname - SONAME for needed sharable library, as described above
3048 * fd - Open file descriptor
3049 * elf - Open ELF handle
3050 * flags - FLG_IF_ flags applicable to file
3051 * ofl - Output file descriptor
3052 * rej - Rejection descriptor used to record rejection reason
3053 * ifl_ret - NULL, or address of pointer to receive reference to
3054 * resulting input descriptor for file. If ifl_ret is non-NULL,
3055 * the file cannot be an archive or it will be rejected.
3057 * exit:
3058 * If a error occurs in examining the file, S_ERROR is returned.
3059 * If the file can be examined, but is not suitable, *rej is updated,
3060 * and 0 is returned. If the file is acceptable, 1 is returned, and if
3061 * ifl_ret is non-NULL, *ifl_ret is set to contain the pointer to the
3062 * resulting input descriptor.
3064 uintptr_t
3065 ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf,
3066 Word flags, Ofl_desc *ofl, Rej_desc *rej, Ifl_desc **ifl_ret)
3068 Ifl_desc *ifl;
3069 Ehdr *ehdr;
3070 uintptr_t error = 0;
3071 struct stat status;
3072 Ar_desc *adp;
3073 Rej_desc _rej;
3076 * If this file was not extracted from an archive obtain its device
3077 * information. This will be used to determine if the file has already
3078 * been processed (rather than simply comparing filenames, the device
3079 * information provides a quicker comparison and detects linked files).
3081 if (fd && ((flags & FLG_IF_EXTRACT) == 0))
3082 (void) fstat(fd, &status);
3083 else {
3084 status.st_dev = 0;
3085 status.st_ino = 0;
3088 switch (elf_kind(elf)) {
3089 case ELF_K_AR:
3091 * If the caller has supplied a non-NULL ifl_ret, then
3092 * we cannot process archives, for there will be no
3093 * input file descriptor for us to return. In this case,
3094 * reject the attempt.
3096 if (ifl_ret != NULL) {
3097 _rej.rej_type = SGS_REJ_ARCHIVE;
3098 _rej.rej_name = name;
3099 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
3100 ld_targ.t_m.m_mach));
3101 if (rej->rej_type == 0) {
3102 *rej = _rej;
3103 rej->rej_name = strdup(_rej.rej_name);
3105 return (0);
3109 * Determine if we've already come across this archive file.
3111 if (!(flags & FLG_IF_EXTRACT)) {
3112 Aliste idx;
3114 for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) {
3115 if ((adp->ad_stdev != status.st_dev) ||
3116 (adp->ad_stino != status.st_ino))
3117 continue;
3120 * We've seen this file before so reuse the
3121 * original archive descriptor and discard the
3122 * new elf descriptor. Note that a file
3123 * descriptor is unnecessary, as the file is
3124 * already available in memory.
3126 DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name,
3127 adp->ad_name));
3128 (void) elf_end(elf);
3129 if (!ld_process_archive(name, -1, adp, ofl))
3130 return (S_ERROR);
3131 return (1);
3136 * As we haven't processed this file before establish a new
3137 * archive descriptor.
3139 adp = ld_ar_setup(name, elf, ofl);
3140 if ((adp == NULL) || (adp == (Ar_desc *)S_ERROR))
3141 return ((uintptr_t)adp);
3142 adp->ad_stdev = status.st_dev;
3143 adp->ad_stino = status.st_ino;
3145 ld_sup_file(ofl, name, ELF_K_AR, flags, elf);
3148 * Indicate that the ELF descriptor no longer requires a file
3149 * descriptor by reading the entire file. The file is already
3150 * read via the initial mmap(2) behind elf_begin(3elf), thus
3151 * this operation is effectively a no-op. However, a side-
3152 * effect is that the internal file descriptor, maintained in
3153 * the ELF descriptor, is set to -1. This setting will not
3154 * be compared with any file descriptor that is passed to
3155 * elf_begin(), should this archive, or one of the archive
3156 * members, be processed again from the command line or
3157 * because of a -z rescan.
3159 if (elf_cntl(elf, ELF_C_FDREAD) == -1) {
3160 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_CNTL),
3161 name);
3162 return (0);
3165 if (!ld_process_archive(name, -1, adp, ofl))
3166 return (S_ERROR);
3167 return (1);
3169 case ELF_K_ELF:
3171 * Obtain the elf header so that we can determine what type of
3172 * elf ELF_K_ELF file this is.
3174 if ((ehdr = elf_getehdr(elf)) == NULL) {
3175 int _class = gelf_getclass(elf);
3178 * This can fail for a number of reasons. Typically
3179 * the object class is incorrect (ie. user is building
3180 * 64-bit but managed to point at 32-bit libraries).
3181 * Other ELF errors can include a truncated or corrupt
3182 * file. Try to get the best error message possible.
3184 if (ld_targ.t_m.m_class != _class) {
3185 _rej.rej_type = SGS_REJ_CLASS;
3186 _rej.rej_info = (uint_t)_class;
3187 } else {
3188 _rej.rej_type = SGS_REJ_STR;
3189 _rej.rej_str = elf_errmsg(-1);
3191 _rej.rej_name = name;
3192 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
3193 ld_targ.t_m.m_mach));
3194 if (rej->rej_type == 0) {
3195 *rej = _rej;
3196 rej->rej_name = strdup(_rej.rej_name);
3198 return (0);
3202 * Determine if we've already come across this file.
3204 if (!(flags & FLG_IF_EXTRACT)) {
3205 APlist *apl;
3206 Aliste idx;
3208 if (ehdr->e_type == ET_REL)
3209 apl = ofl->ofl_objs;
3210 else
3211 apl = ofl->ofl_sos;
3214 * Traverse the appropriate file list and determine if
3215 * a dev/inode match is found.
3217 for (APLIST_TRAVERSE(apl, idx, ifl)) {
3219 * Ifl_desc generated via -Nneed, therefore no
3220 * actual file behind it.
3222 if (ifl->ifl_flags & FLG_IF_NEEDSTR)
3223 continue;
3225 if ((ifl->ifl_stino != status.st_ino) ||
3226 (ifl->ifl_stdev != status.st_dev))
3227 continue;
3230 * Disregard (skip) this image.
3232 DBG_CALL(Dbg_file_skip(ofl->ofl_lml,
3233 ifl->ifl_name, name));
3234 (void) elf_end(elf);
3237 * If the file was explicitly defined on the
3238 * command line (this is always the case for
3239 * relocatable objects, and is true for shared
3240 * objects when they weren't specified via -l or
3241 * were dragged in as an implicit dependency),
3242 * then warn the user.
3244 if ((flags & FLG_IF_CMDLINE) ||
3245 (ifl->ifl_flags & FLG_IF_CMDLINE)) {
3246 const char *errmsg;
3249 * Determine whether this is the same
3250 * file name as originally encountered
3251 * so as to provide the most
3252 * descriptive diagnostic.
3254 errmsg =
3255 (strcmp(name, ifl->ifl_name) == 0) ?
3256 MSG_INTL(MSG_FIL_MULINC_1) :
3257 MSG_INTL(MSG_FIL_MULINC_2);
3258 ld_eprintf(ofl, ERR_WARNING,
3259 errmsg, name, ifl->ifl_name);
3261 if (ifl_ret)
3262 *ifl_ret = ifl;
3263 return (1);
3268 * At this point, we know we need the file. Establish an input
3269 * file descriptor and continue processing.
3271 ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej);
3272 if ((ifl == NULL) || (ifl == (Ifl_desc *)S_ERROR))
3273 return ((uintptr_t)ifl);
3274 ifl->ifl_stdev = status.st_dev;
3275 ifl->ifl_stino = status.st_ino;
3278 * If -zignore is in effect, mark this file as a potential
3279 * candidate (the files use isn't actually determined until
3280 * symbol resolution and relocation processing are completed).
3282 if (ofl->ofl_flags1 & FLG_OF1_IGNORE)
3283 ifl->ifl_flags |= FLG_IF_IGNORE;
3285 switch (ehdr->e_type) {
3286 case ET_REL:
3287 (*ld_targ.t_mr.mr_mach_eflags)(ehdr, ofl);
3288 error = process_elf(ifl, elf, ofl);
3289 break;
3290 case ET_DYN:
3291 if ((ofl->ofl_flags & FLG_OF_STATIC) ||
3292 !(ofl->ofl_flags & FLG_OF_DYNLIBS)) {
3293 ld_eprintf(ofl, ERR_FATAL,
3294 MSG_INTL(MSG_FIL_SOINSTAT), name);
3295 return (0);
3299 * Record any additional shared object information.
3300 * If no soname is specified (eg. this file was
3301 * derived from a explicit filename declaration on the
3302 * command line, ie. bar.so) use the pathname.
3303 * This entry may be overridden if the files dynamic
3304 * section specifies an DT_SONAME value.
3306 if (soname == NULL)
3307 ifl->ifl_soname = ifl->ifl_name;
3308 else
3309 ifl->ifl_soname = soname;
3312 * If direct bindings, lazy loading, group permissions,
3313 * or deferred dependencies need to be established, mark
3314 * this object.
3316 if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT)
3317 ifl->ifl_flags |= FLG_IF_DIRECT;
3318 if (ofl->ofl_flags1 & FLG_OF1_LAZYLD)
3319 ifl->ifl_flags |= FLG_IF_LAZYLD;
3320 if (ofl->ofl_flags1 & FLG_OF1_GRPPRM)
3321 ifl->ifl_flags |= FLG_IF_GRPPRM;
3322 if (ofl->ofl_flags1 & FLG_OF1_DEFERRED)
3323 ifl->ifl_flags |=
3324 (FLG_IF_LAZYLD | FLG_IF_DEFERRED);
3326 error = process_elf(ifl, elf, ofl);
3329 * Determine whether this dependency requires a syminfo.
3331 if (ifl->ifl_flags & MSK_IF_SYMINFO)
3332 ofl->ofl_flags |= FLG_OF_SYMINFO;
3335 * Guidance: Use -z lazyload/nolazyload.
3336 * libc is exempt from this advice, because it cannot
3337 * be lazy loaded, and requests to do so are ignored.
3339 if (OFL_GUIDANCE(ofl, FLG_OFG_NO_LAZY) &&
3340 ((ifl->ifl_flags & FLG_IF_RTLDINF) == 0)) {
3341 ld_eprintf(ofl, ERR_GUIDANCE,
3342 MSG_INTL(MSG_GUIDE_LAZYLOAD));
3343 ofl->ofl_guideflags |= FLG_OFG_NO_LAZY;
3347 * Guidance: Use -B direct/nodirect or
3348 * -z direct/nodirect.
3350 if (OFL_GUIDANCE(ofl, FLG_OFG_NO_DB)) {
3351 ld_eprintf(ofl, ERR_GUIDANCE,
3352 MSG_INTL(MSG_GUIDE_DIRECT));
3353 ofl->ofl_guideflags |= FLG_OFG_NO_DB;
3356 break;
3357 default:
3358 (void) elf_errno();
3359 _rej.rej_type = SGS_REJ_UNKFILE;
3360 _rej.rej_name = name;
3361 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
3362 ld_targ.t_m.m_mach));
3363 if (rej->rej_type == 0) {
3364 *rej = _rej;
3365 rej->rej_name = strdup(_rej.rej_name);
3367 return (0);
3369 break;
3370 default:
3371 (void) elf_errno();
3372 _rej.rej_type = SGS_REJ_UNKFILE;
3373 _rej.rej_name = name;
3374 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej,
3375 ld_targ.t_m.m_mach));
3376 if (rej->rej_type == 0) {
3377 *rej = _rej;
3378 rej->rej_name = strdup(_rej.rej_name);
3380 return (0);
3382 if ((error == 0) || (error == S_ERROR))
3383 return (error);
3385 if (ifl_ret)
3386 *ifl_ret = ifl;
3387 return (1);
3391 * Having successfully opened a file, set up the necessary elf structures to
3392 * process it further. This small section of processing is slightly different
3393 * from the elf initialization required to process a relocatable object from an
3394 * archive (see libs.c: ld_process_archive()).
3396 uintptr_t
3397 ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl,
3398 Word flags, Rej_desc *rej, Ifl_desc **ifl_ret)
3400 Elf *elf;
3401 const char *npath = opath;
3402 const char *nfile = ofile;
3404 if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) {
3405 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath);
3406 return (0);
3410 * Determine whether the support library wishes to process this open.
3411 * The support library may return:
3412 * . a different ELF descriptor (in which case they should have
3413 * closed the original)
3414 * . a different file descriptor (in which case they should have
3415 * closed the original)
3416 * . a different path and file name (presumably associated with
3417 * a different file descriptor)
3419 * A file descriptor of -1, or and ELF descriptor of zero indicates
3420 * the file should be ignored.
3422 ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0,
3423 elf_kind(elf));
3425 if ((*fd == -1) || (elf == NULL))
3426 return (0);
3428 return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej,
3429 ifl_ret));
3433 * Having successfully mapped a file, set up the necessary elf structures to
3434 * process it further. This routine is patterned after ld_process_open() and
3435 * is only called by ld.so.1(1) to process a relocatable object.
3437 Ifl_desc *
3438 ld_process_mem(const char *path, const char *file, char *addr, size_t size,
3439 Ofl_desc *ofl, Rej_desc *rej)
3441 Elf *elf;
3442 uintptr_t open_ret;
3443 Ifl_desc *ifl;
3445 if ((elf = elf_memory(addr, size)) == NULL) {
3446 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_MEMORY), path);
3447 return (0);
3450 open_ret = ld_process_ifl(path, file, 0, elf, 0, ofl, rej, &ifl);
3451 if (open_ret != 1)
3452 return ((Ifl_desc *) open_ret);
3453 return (ifl);
3457 * Process a required library (i.e. the dependency of a shared object).
3458 * Combine the directory and filename, check the resultant path size, and try
3459 * opening the pathname.
3461 static Ifl_desc *
3462 process_req_lib(Sdf_desc *sdf, const char *dir, const char *file,
3463 Ofl_desc *ofl, Rej_desc *rej)
3465 size_t dlen, plen;
3466 int fd;
3467 char path[PATH_MAX];
3468 const char *_dir = dir;
3471 * Determine the sizes of the directory and filename to insure we don't
3472 * exceed our buffer.
3474 if ((dlen = strlen(dir)) == 0) {
3475 _dir = MSG_ORIG(MSG_STR_DOT);
3476 dlen = 1;
3478 dlen++;
3479 plen = dlen + strlen(file) + 1;
3480 if (plen > PATH_MAX) {
3481 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG),
3482 _dir, file);
3483 return (0);
3487 * Build the entire pathname and try and open the file.
3489 (void) strcpy(path, _dir);
3490 (void) strcat(path, MSG_ORIG(MSG_STR_SLASH));
3491 (void) strcat(path, file);
3492 DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
3493 sdf->sdf_rfile, path));
3495 if ((fd = open(path, O_RDONLY)) == -1)
3496 return (0);
3497 else {
3498 uintptr_t open_ret;
3499 Ifl_desc *ifl;
3500 char *_path;
3502 if ((_path = libld_malloc(strlen(path) + 1)) == NULL)
3503 return ((Ifl_desc *)S_ERROR);
3504 (void) strcpy(_path, path);
3505 open_ret = ld_process_open(_path, &_path[dlen], &fd, ofl,
3506 0, rej, &ifl);
3507 if (fd != -1)
3508 (void) close(fd);
3509 if (open_ret != 1)
3510 return ((Ifl_desc *)open_ret);
3511 return (ifl);
3516 * Finish any library processing. Walk the list of so's that have been listed
3517 * as "included" by shared objects we have previously processed. Examine them,
3518 * without adding them as explicit dependents of this program, in order to
3519 * complete our symbol definition process. The search path rules are:
3521 * - use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then
3523 * - use any RPATH defined within the parent shared object, then
3525 * - use the default directories, i.e. LIBPATH or -YP.
3527 uintptr_t
3528 ld_finish_libs(Ofl_desc *ofl)
3530 Aliste idx1;
3531 Sdf_desc *sdf;
3532 Rej_desc rej = { 0 };
3535 * Make sure we are back in dynamic mode.
3537 ofl->ofl_flags |= FLG_OF_DYNLIBS;
3539 for (APLIST_TRAVERSE(ofl->ofl_soneed, idx1, sdf)) {
3540 Aliste idx2;
3541 char *path, *slash = NULL;
3542 int fd;
3543 Ifl_desc *ifl;
3544 char *file = (char *)sdf->sdf_name;
3547 * See if this file has already been processed. At the time
3548 * this implicit dependency was determined there may still have
3549 * been more explicit dependencies to process. Note, if we ever
3550 * do parse the command line three times we would be able to
3551 * do all this checking when processing the dynamic section.
3553 if (sdf->sdf_file)
3554 continue;
3556 for (APLIST_TRAVERSE(ofl->ofl_sos, idx2, ifl)) {
3557 if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) &&
3558 (strcmp(file, ifl->ifl_soname) == 0)) {
3559 sdf->sdf_file = ifl;
3560 break;
3563 if (sdf->sdf_file)
3564 continue;
3567 * If the current path name element embeds a "/", then it's to
3568 * be taken "as is", with no searching involved. Process all
3569 * "/" occurrences, so that we can deduce the base file name.
3571 for (path = file; *path; path++) {
3572 if (*path == '/')
3573 slash = path;
3575 if (slash) {
3576 DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name,
3577 sdf->sdf_rfile, file));
3578 if ((fd = open(file, O_RDONLY)) == -1) {
3579 ld_eprintf(ofl, ERR_WARNING,
3580 MSG_INTL(MSG_FIL_NOTFOUND), file,
3581 sdf->sdf_rfile);
3582 } else {
3583 uintptr_t open_ret;
3584 Rej_desc _rej = { 0 };
3586 open_ret = ld_process_open(file, ++slash,
3587 &fd, ofl, 0, &_rej, &ifl);
3588 if (fd != -1)
3589 (void) close(fd);
3590 if (open_ret == S_ERROR)
3591 return (S_ERROR);
3593 if (_rej.rej_type) {
3594 Conv_reject_desc_buf_t rej_buf;
3596 ld_eprintf(ofl, ERR_WARNING,
3597 MSG_INTL(reject[_rej.rej_type]),
3598 _rej.rej_name ? rej.rej_name :
3599 MSG_INTL(MSG_STR_UNKNOWN),
3600 conv_reject_desc(&_rej, &rej_buf,
3601 ld_targ.t_m.m_mach));
3602 } else
3603 sdf->sdf_file = ifl;
3605 continue;
3609 * Now search for this file in any user defined directories.
3611 for (APLIST_TRAVERSE(ofl->ofl_ulibdirs, idx2, path)) {
3612 Rej_desc _rej = { 0 };
3614 ifl = process_req_lib(sdf, path, file, ofl, &_rej);
3615 if (ifl == (Ifl_desc *)S_ERROR) {
3616 return (S_ERROR);
3618 if (_rej.rej_type) {
3619 if (rej.rej_type == 0) {
3620 rej = _rej;
3621 rej.rej_name = strdup(_rej.rej_name);
3624 if (ifl) {
3625 sdf->sdf_file = ifl;
3626 break;
3629 if (sdf->sdf_file)
3630 continue;
3633 * Next use the local rules defined within the parent shared
3634 * object.
3636 if (sdf->sdf_rpath != NULL) {
3637 char *rpath, *next;
3639 rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1);
3640 if (rpath == NULL)
3641 return (S_ERROR);
3642 (void) strcpy(rpath, sdf->sdf_rpath);
3643 DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath,
3644 LA_SER_RUNPATH, sdf->sdf_rfile));
3645 if ((path = strtok_r(rpath,
3646 MSG_ORIG(MSG_STR_COLON), &next)) != NULL) {
3647 do {
3648 Rej_desc _rej = { 0 };
3650 path = expand(sdf->sdf_rfile, path,
3651 &next);
3653 ifl = process_req_lib(sdf, path,
3654 file, ofl, &_rej);
3655 if (ifl == (Ifl_desc *)S_ERROR) {
3656 return (S_ERROR);
3658 if ((_rej.rej_type) &&
3659 (rej.rej_type == 0)) {
3660 rej = _rej;
3661 rej.rej_name =
3662 strdup(_rej.rej_name);
3664 if (ifl) {
3665 sdf->sdf_file = ifl;
3666 break;
3668 } while ((path = strtok_r(NULL,
3669 MSG_ORIG(MSG_STR_COLON), &next)) != NULL);
3672 if (sdf->sdf_file)
3673 continue;
3676 * Finally try the default library search directories.
3678 for (APLIST_TRAVERSE(ofl->ofl_dlibdirs, idx2, path)) {
3679 Rej_desc _rej = { 0 };
3681 ifl = process_req_lib(sdf, path, file, ofl, &rej);
3682 if (ifl == (Ifl_desc *)S_ERROR) {
3683 return (S_ERROR);
3685 if (_rej.rej_type) {
3686 if (rej.rej_type == 0) {
3687 rej = _rej;
3688 rej.rej_name = strdup(_rej.rej_name);
3691 if (ifl) {
3692 sdf->sdf_file = ifl;
3693 break;
3696 if (sdf->sdf_file)
3697 continue;
3700 * If we've got this far we haven't found the shared object.
3701 * If an object was found, but was rejected for some reason,
3702 * print a diagnostic to that effect, otherwise generate a
3703 * generic "not found" diagnostic.
3705 if (rej.rej_type) {
3706 Conv_reject_desc_buf_t rej_buf;
3708 ld_eprintf(ofl, ERR_WARNING,
3709 MSG_INTL(reject[rej.rej_type]),
3710 rej.rej_name ? rej.rej_name :
3711 MSG_INTL(MSG_STR_UNKNOWN),
3712 conv_reject_desc(&rej, &rej_buf,
3713 ld_targ.t_m.m_mach));
3714 } else {
3715 ld_eprintf(ofl, ERR_WARNING,
3716 MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile);
3721 * Finally, now that all objects have been input, make sure any version
3722 * requirements have been met.
3724 return (ld_vers_verify(ofl));