xref: /illumos-gate/usr/src/cmd/sgs/libld/common/ldmain.c (revision dd72704bd9e794056c558153663c739e2012d721)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  *	Copyright (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
27  *
28  * Copyright 2020 Joyent, Inc.
29  */
30 
31 /*
32  * Processing of relocatable objects and shared objects.
33  */
34 
35 /*
36  * ld -- link/editor main program
37  */
38 #include	<sys/types.h>
39 #include	<sys/time.h>
40 #include	<sys/mman.h>
41 #include	<string.h>
42 #include	<stdio.h>
43 #include	<locale.h>
44 #include	<stdarg.h>
45 #include	<debug.h>
46 #include	"msg.h"
47 #include	"_libld.h"
48 
49 /*
50  * All target specific code is referenced via this global variable, which
51  * is initialized in ld_main(). This allows the linker to function as
52  * a cross linker, by vectoring to the target-specific code for the
53  * current target machine.
54  */
55 Target		ld_targ;
56 
57 /*
58  * A default library search path is used if one was not supplied on the command
59  * line.  Note: these strings can not use MSG_ORIG() since they are modified as
60  * part of the path processing.
61  */
62 #if	defined(_ELF64)
63 static char	def_Plibpath[] = "/lib/64:/usr/lib/64";
64 #else
65 static char	def_Plibpath[] = "/usr/ccs/lib:/lib:/usr/lib";
66 #endif
67 
68 /*
69  * A default elf header provides for simplifying diagnostic processing.
70  */
71 static Ehdr	def_ehdr = { { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
72 			    ELFCLASSNONE, ELFDATANONE }, 0, EM_NONE,
73 			    EV_CURRENT };
74 
75 /*
76  * ld-centric wrapper on top of veprintf():
77  * - Accepts output descriptor rather than linkmap list
78  * - Sets the FLG_OF_FATAL/FLG_OF_WARN flags as necessary
79  */
80 void
81 ld_eprintf(Ofl_desc *ofl, Error error, const char *format, ...)
82 {
83 	va_list	args;
84 
85 	/* Set flag indicating type of error being issued */
86 	switch (error) {
87 	case ERR_NONE:
88 	case ERR_WARNING_NF:
89 		break;
90 	case ERR_WARNING:
91 		ofl->ofl_flags |= FLG_OF_WARN;
92 		break;
93 	case ERR_GUIDANCE:
94 		ofl->ofl_guideflags |= FLG_OFG_ISSUED;
95 		ofl->ofl_flags |= FLG_OF_WARN;
96 		break;
97 	default:
98 		ofl->ofl_flags |= FLG_OF_FATAL;
99 	}
100 
101 	/* Issue the error */
102 	va_start(args, format);
103 	veprintf(ofl->ofl_lml, error, format, args);
104 	va_end(args);
105 }
106 
107 /*
108  * Establish the global state necessary to link the desired machine
109  * target, as reflected by the ld_targ global variable.
110  */
111 int
112 ld_init_target(Lm_list *lml, Half mach)
113 {
114 	switch (mach) {
115 	case EM_386:
116 	case EM_AMD64:
117 		ld_targ = *ld_targ_init_x86();
118 		break;
119 
120 	case EM_SPARC:
121 	case EM_SPARC32PLUS:
122 	case EM_SPARCV9:
123 		ld_targ = *ld_targ_init_sparc();
124 		break;
125 
126 	default:
127 		{
128 			Conv_inv_buf_t	inv_buf;
129 
130 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_TARG_UNSUPPORTED),
131 			    conv_ehdr_mach(mach, 0, &inv_buf));
132 			return (1);
133 		}
134 	}
135 
136 	return (0);
137 }
138 
139 
140 /*
141  * The main program
142  */
143 int
144 ld_main(int argc, char **argv, Half mach)
145 {
146 	char		*sgs_support;	/* SGS_SUPPORT environment string */
147 	Half		etype;
148 	Ofl_desc	*ofl;
149 	ofl_flag_t	save_flg_of_warn;
150 
151 	/*
152 	 * Establish a base time.  Total time diagnostics are relative to
153 	 * entering the link-editor here.
154 	 */
155 	(void) gettimeofday(&DBG_TOTALTIME, NULL);
156 	DBG_DELTATIME = DBG_TOTALTIME;
157 
158 	/* Output file descriptor */
159 	if ((ofl = libld_calloc(1, sizeof (Ofl_desc))) == NULL)
160 		return (1);
161 
162 	/* Initialize target state */
163 	if (ld_init_target(NULL, mach) != 0)
164 		return (1);
165 
166 	/*
167 	 * Set up the default output ELF header to satisfy diagnostic
168 	 * requirements, and initialize the machine and class details.
169 	 */
170 	ofl->ofl_dehdr = &def_ehdr;
171 	def_ehdr.e_ident[EI_CLASS] = ld_targ.t_m.m_class;
172 	def_ehdr.e_ident[EI_DATA] = ld_targ.t_m.m_data;
173 	def_ehdr.e_machine = ld_targ.t_m.m_mach;
174 
175 	/*
176 	 * Build up linker version string
177 	 */
178 	if ((ofl->ofl_sgsid = libld_calloc(MSG_SGS_ID_SIZE +
179 	    strlen(link_ver_string) + 1, 1)) == NULL)
180 		return (1);
181 	(void) strcpy(ofl->ofl_sgsid, MSG_ORIG(MSG_SGS_ID));
182 	(void) strcat(ofl->ofl_sgsid, link_ver_string);
183 
184 	/*
185 	 * Argument pass one.  Get all the input flags (skip any files) and
186 	 * check for consistency.  Return from ld_process_flags() marks the
187 	 * end of mapfile processing.  The entrance criteria and segment
188 	 * descriptors are complete and in their final form.
189 	 */
190 	if (ld_process_flags(ofl, argc, argv) == S_ERROR) {
191 		/* If any ERR_GUIDANCE messages were issued, add a summary */
192 		if (ofl->ofl_guideflags & FLG_OFG_ISSUED)
193 			ld_eprintf(ofl, ERR_GUIDANCE,
194 			    MSG_INTL(MSG_GUIDE_SUMMARY));
195 		return (1);
196 	}
197 
198 	if (ofl->ofl_flags & FLG_OF_FATAL) {
199 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_FLAGS));
200 		/* If any ERR_GUIDANCE messages were issued, add a summary */
201 		if (ofl->ofl_guideflags & FLG_OFG_ISSUED)
202 			ld_eprintf(ofl, ERR_GUIDANCE,
203 			    MSG_INTL(MSG_GUIDE_SUMMARY));
204 		return (1);
205 	}
206 
207 	/*
208 	 * At this point a call such as ld -V is considered complete.
209 	 */
210 	if (ofl->ofl_flags1 & FLG_OF1_DONE)
211 		return (0);
212 
213 	/* Initialize signal handler */
214 	ld_init_sighandler(ofl);
215 
216 	/*
217 	 * Determine whether any support libraries should be loaded,
218 	 * (either through the SGS_SUPPORT environment variable and/or
219 	 * through the -S option).
220 	 */
221 #if	defined(_LP64)
222 	if ((sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT_64))) == NULL)
223 #else
224 	if ((sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT_32))) == NULL)
225 #endif
226 		sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT));
227 
228 	if (sgs_support && sgs_support[0]) {
229 		const char	*sep = MSG_ORIG(MSG_STR_COLON);
230 		char		*lib;
231 		char		*lasts;
232 
233 		DBG_CALL(Dbg_support_req(ofl->ofl_lml, sgs_support,
234 		    DBG_SUP_ENVIRON));
235 		if ((lib = strtok_r(sgs_support, sep, &lasts)) != NULL) {
236 			do {
237 				if (ld_sup_loadso(ofl, lib) == S_ERROR)
238 					return (ld_exit(ofl));
239 				DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
240 
241 			} while ((lib = strtok_r(NULL, sep, &lasts)) != NULL);
242 		}
243 	}
244 	if (lib_support) {
245 		Aliste	idx;
246 		char	*lib;
247 
248 		for (APLIST_TRAVERSE(lib_support, idx, lib)) {
249 			DBG_CALL(Dbg_support_req(ofl->ofl_lml, lib,
250 			    DBG_SUP_CMDLINE));
251 			if (ld_sup_loadso(ofl, lib) == S_ERROR)
252 				return (ld_exit(ofl));
253 			DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
254 		}
255 	}
256 
257 	DBG_CALL(Dbg_ent_print(ofl->ofl_lml,
258 	    ofl->ofl_dehdr->e_ident[EI_OSABI], ofl->ofl_dehdr->e_machine,
259 	    ofl->ofl_ents));
260 	DBG_CALL(Dbg_seg_list(ofl->ofl_lml,
261 	    ofl->ofl_dehdr->e_ident[EI_OSABI], ofl->ofl_dehdr->e_machine,
262 	    ofl->ofl_segs));
263 
264 	/*
265 	 * The objscnt and soscnt variables were used to estimate the expected
266 	 * input files, and size the symbol hash buckets accordingly.  Reset
267 	 * these values now, so as to gain an accurate count from pass two, for
268 	 * later statistics diagnostics.
269 	 */
270 	ofl->ofl_objscnt = ofl->ofl_soscnt = 0;
271 
272 	/*
273 	 * Determine whether we can create the file before going any further.
274 	 */
275 	if (ld_open_outfile(ofl) == S_ERROR)
276 		return (ld_exit(ofl));
277 
278 	/*
279 	 * If the user didn't supply a library path supply a default.  And, if
280 	 * no run-path has been specified (-R), see if the environment variable
281 	 * is in use (historic).
282 	 */
283 	if (Plibpath == NULL)
284 		Plibpath = def_Plibpath;
285 
286 	if (ofl->ofl_rpath == NULL) {
287 		char	*rpath;
288 
289 		if (((rpath = getenv(MSG_ORIG(MSG_LD_RUN_PATH))) != NULL) &&
290 		    rpath[0])
291 			ofl->ofl_rpath = rpath;
292 	}
293 
294 	/*
295 	 * Argument pass two.  Input all libraries and objects.
296 	 */
297 	if (ld_lib_setup(ofl) == S_ERROR)
298 		return (ld_exit(ofl));
299 
300 	/*
301 	 * Call ld_start() with the etype of our output file and the
302 	 * output file name.
303 	 */
304 	if (ofl->ofl_flags & FLG_OF_SHAROBJ)
305 		etype = ET_DYN;
306 	else if (ofl->ofl_flags & FLG_OF_RELOBJ)
307 		etype = ET_REL;
308 	else
309 		etype = ET_EXEC;
310 
311 	ld_sup_start(ofl, etype, argv[0]);
312 
313 	/*
314 	 * Process all input files.
315 	 */
316 	if (ld_process_files(ofl, argc, argv) == S_ERROR)
317 		return (ld_exit(ofl));
318 	if (ofl->ofl_flags & FLG_OF_FATAL) {
319 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_FILES),
320 		    ofl->ofl_name);
321 		return (ld_exit(ofl));
322 	}
323 
324 	ld_sup_input_done(ofl);
325 
326 	/*
327 	 * Now that all input section processing is complete, validate and
328 	 * process any SHT_SUNW_move sections.
329 	 */
330 	if (ofl->ofl_ismove && (ld_process_move(ofl) == S_ERROR))
331 		return (ld_exit(ofl));
332 
333 	/*
334 	 * Before validating all symbols count the number of relocation entries.
335 	 * If copy relocations exist, COMMON symbols must be generated which are
336 	 * assigned to the executables .bss.  During sym_validate() the actual
337 	 * size and alignment of the .bss is calculated.  Doing things in this
338 	 * order reduces the number of symbol table traversals required (however
339 	 * it does take a little longer for the user to be told of any undefined
340 	 * symbol errors).
341 	 */
342 	if (ld_reloc_init(ofl) == S_ERROR)
343 		return (ld_exit(ofl));
344 
345 	/*
346 	 * We need to know if FLG_OF_WARN is currently set, in case
347 	 * we need to honor a -z fatal-warnings request. However, we also
348 	 * need to know if a warning due to symbol validation results from
349 	 * the upcoming call to ld_sym_validate() in order to issue the
350 	 * appropriate message for it. So we save the current value,
351 	 * and clear the main flag.
352 	 */
353 	save_flg_of_warn = ofl->ofl_flags & FLG_OF_WARN;
354 	ofl->ofl_flags &= ~FLG_OF_WARN;
355 
356 	if (ld_sym_validate(ofl) == S_ERROR)
357 		return (ld_exit(ofl));
358 
359 	/*
360 	 * Now that all symbol processing is complete see if any undefined
361 	 * references still remain.  If we observed undefined symbols the
362 	 * FLG_OF_FATAL bit will be set:  If creating a static executable, or a
363 	 * dynamic executable or shared object with the -zdefs flag set, this
364 	 * condition is fatal.  If creating a shared object with the -Bsymbolic
365 	 * flag set, this condition is simply a warning.
366 	 */
367 	if (ofl->ofl_flags & FLG_OF_FATAL)
368 		ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_SYM_FATAL),
369 		    ofl->ofl_name);
370 	else if (ofl->ofl_flags & FLG_OF_WARN)
371 		ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_ARG_SYM_WARN));
372 
373 	/*
374 	 * Guidance: Use -z defs|nodefs when building shared objects.
375 	 *
376 	 * ld_sym_validate() will mask this guidance message out unless we are
377 	 * intended to send it here, so all we need to do is use OFL_GUIDANCE()
378 	 * to decide whether to issue it or not.
379 	 */
380 	if (OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS))
381 		ld_eprintf(ofl, ERR_GUIDANCE, MSG_INTL(MSG_GUIDE_DEFS));
382 
383 	/*
384 	 * Symbol processing was the final step before we start producing the
385 	 * output object. At this time, if we've seen warnings and the
386 	 * -z fatal-warnings option is specified, promote them to fatal, which
387 	 * will cause us to exit without creating an object.
388 	 *
389 	 * We didn't do this as the warnings were reported in order to
390 	 * maximize the number of problems a given link-editor invocation
391 	 * can diagnose. This is safe, since warnings are by definition events
392 	 * one can choose to ignore.
393 	 */
394 	if (((ofl->ofl_flags | save_flg_of_warn) &
395 	    (FLG_OF_WARN | FLG_OF_FATWARN)) ==
396 	    (FLG_OF_WARN | FLG_OF_FATWARN))
397 		ofl->ofl_flags |= FLG_OF_FATAL;
398 
399 	/*
400 	 * If fatal errors occurred in symbol processing, or due to warnings
401 	 * promoted by -z fatal-warnings, this is the end of the line.
402 	 */
403 	if (ofl->ofl_flags & FLG_OF_FATAL)
404 		return (ld_exit(ofl));
405 
406 	/*
407 	 * Generate any necessary sections.
408 	 */
409 	if (ld_make_sections(ofl) == S_ERROR)
410 		return (ld_exit(ofl));
411 
412 	/*
413 	 * Now that all sections have been added to the output file, determine
414 	 * whether any mapfile section ordering was specified, and verify that
415 	 * all mapfile ordering directives have been matched.  Issue a warning
416 	 * for any directives that have not been matched.
417 	 * Also, if SHF_ORDERED sections exist, set up sort key values.
418 	 */
419 	if (ofl->ofl_flags & (FLG_OF_OS_ORDER | FLG_OF_KEY))
420 		ld_sec_validate(ofl);
421 
422 	/*
423 	 * Having collected all the input data create the initial output file
424 	 * image, assign virtual addresses to the image, and generate a load
425 	 * map if the user requested one.
426 	 */
427 	if (ld_create_outfile(ofl) == S_ERROR)
428 		return (ld_exit(ofl));
429 
430 	if (ld_update_outfile(ofl) == S_ERROR)
431 		return (ld_exit(ofl));
432 	if (ofl->ofl_flags & FLG_OF_GENMAP)
433 		ld_map_out(ofl);
434 
435 	/*
436 	 * Build relocation sections and perform any relocation updates.
437 	 */
438 	if (ld_reloc_process(ofl) == S_ERROR)
439 		return (ld_exit(ofl));
440 
441 	/*
442 	 * Fill in contents for unwind header (.eh_frame_hdr)
443 	 */
444 	if (ld_unwind_populate_hdr(ofl) == S_ERROR)
445 		return (ld_exit(ofl));
446 
447 	/*
448 	 * Finally create the files elf checksum.
449 	 */
450 	if (ofl->ofl_checksum)
451 		*ofl->ofl_checksum = (Xword)elf_checksum(ofl->ofl_elf);
452 
453 	/*
454 	 * If this is a cross link to a target with a different byte
455 	 * order than the linker, swap the data to the target byte order.
456 	 */
457 	if (((ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0) &&
458 	    (_elf_swap_wrimage(ofl->ofl_elf) != 0)) {
459 		ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_SWAP_WRIMAGE),
460 		    ofl->ofl_name);
461 		return (ld_exit(ofl));
462 	}
463 
464 	/*
465 	 * We're done, so make sure the updates are flushed to the output file.
466 	 */
467 	if ((ofl->ofl_size = elf_update(ofl->ofl_welf, ELF_C_WRITE)) == 0) {
468 		ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_UPDATE),
469 		    ofl->ofl_name);
470 		return (ld_exit(ofl));
471 	}
472 
473 	ld_sup_atexit(ofl, 0);
474 
475 	DBG_CALL(Dbg_statistics_ld(ofl));
476 	DBG_CALL(Dbg_basic_finish(ofl->ofl_lml));
477 
478 	/*
479 	 * Wrap up debug output file if one is open
480 	 */
481 	dbg_cleanup();
482 
483 	/* If any ERR_GUIDANCE messages were issued, add a summary */
484 	if (ofl->ofl_guideflags & FLG_OFG_ISSUED) {
485 		ld_eprintf(ofl, ERR_GUIDANCE, MSG_INTL(MSG_GUIDE_SUMMARY));
486 		ofl->ofl_guideflags &= ~FLG_OFG_ISSUED;
487 	}
488 
489 	/*
490 	 * One final check for any new warnings we found that should fail the
491 	 * link edit.
492 	 */
493 	if ((ofl->ofl_flags & (FLG_OF_WARN | FLG_OF_FATWARN)) ==
494 	    (FLG_OF_WARN | FLG_OF_FATWARN))
495 		return (ld_exit(ofl));
496 
497 	/*
498 	 * For performance reasons we didn't used to actually free up the
499 	 * memory we'd allocated, since it'll be freed on exit.
500 	 *
501 	 * These calls does not free nearly as much memory as you would think
502 	 * they do, unfortunately.
503 	 */
504 	ld_ofl_cleanup(ofl);
505 	libld_free(ofl);
506 	return (0);
507 }
508 
509 /*
510  * Cleanup an Ifl_desc.
511  */
512 static void
513 ifl_list_cleanup(APlist *apl)
514 {
515 	Aliste		idx;
516 	Ifl_desc	*ifl;
517 
518 	for (APLIST_TRAVERSE(apl, idx, ifl)) {
519 		if (ifl->ifl_elf)
520 			(void) elf_end(ifl->ifl_elf);
521 	}
522 }
523 
524 /*
525  * Cleanup all memory that has been dynamically allocated during libld
526  * processing and elf_end() all Elf descriptors that are still open.
527  */
528 void
529 ld_ofl_cleanup(Ofl_desc *ofl)
530 {
531 	Ar_desc		*adp;
532 	Aliste		idx;
533 
534 	ifl_list_cleanup(ofl->ofl_objs);
535 	ofl->ofl_objs = NULL;
536 	ifl_list_cleanup(ofl->ofl_sos);
537 	ofl->ofl_sos = NULL;
538 
539 	for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) {
540 		Ar_aux		*aup;
541 		Elf_Arsym	*arsym;
542 
543 		/*
544 		 * Free up member information for normally processed archives.
545 		 * Archives processed under -z allextract have no member
546 		 * information or symbol table, and members have already been
547 		 * dealt with as input files.
548 		 */
549 		if (adp->ad_allextract == TRUE)
550 			continue;
551 
552 		for (arsym = adp->ad_start, aup = adp->ad_aux;
553 		    (arsym->as_name != NULL); ++arsym, ++aup) {
554 			if ((aup->au_mem != NULL) &&
555 			    (aup->au_mem != FLG_ARMEM_PROC)) {
556 				(void) elf_end(aup->au_mem->am_elf);
557 
558 				/*
559 				 * Null out all entries to this member so
560 				 * that we don't attempt to elf_end() it again.
561 				 */
562 				ld_ar_member(adp, arsym, aup, 0);
563 			}
564 		}
565 		(void) elf_end(adp->ad_elf);
566 	}
567 	ofl->ofl_ars = NULL;
568 
569 	(void) elf_end(ofl->ofl_elf);
570 	(void) elf_end(ofl->ofl_welf);
571 
572 	/*
573 	 * Note that we don't free ofl itself here, just its contents.  The
574 	 * ofl itself belongs to the caller.
575 	 */
576 }
577