xref: /titanic_51/usr/src/cmd/sgs/libld/common/ldmain.c (revision 7bebe46c240b554f47faeed19186123896281967)
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 2008 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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/mman.h>
40 #include	<string.h>
41 #include	<stdio.h>
42 #include	<locale.h>
43 #include	<stdarg.h>
44 #include	<debug.h>
45 #include	"msg.h"
46 #include	"_libld.h"
47 
48 /*
49  * All target specific code is referenced via this global variable, which
50  * is initialized in ld_main(). This allows the linker to function as
51  * a cross linker, by vectoring to the target-specific code for the
52  * current target machine.
53  */
54 Target		ld_targ;
55 
56 /*
57  * A default library search path is used if one was not supplied on the command
58  * line.  Note: these strings can not use MSG_ORIG() since they are modified as
59  * part of the path processing.
60  */
61 #if	defined(_ELF64)
62 static char	def_Plibpath[] = "/lib/64:/usr/lib/64";
63 #else
64 static char	def_Plibpath[] = "/usr/ccs/lib:/lib:/usr/lib";
65 #endif
66 
67 /*
68  * A default elf header provides for simplifying diagnostic processing.
69  */
70 static Ehdr	def_ehdr = { { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
71 			    ELFCLASSNONE, ELFDATANONE }, 0, EM_NONE,
72 			    EV_CURRENT };
73 
74 /*
75  * Establish the global state necessary to link the desired machine
76  * target, as reflected by the ld_targ global variable.
77  */
78 int
79 ld_init_target(Lm_list *lml, Half mach)
80 {
81 	switch (mach) {
82 	case EM_386:
83 	case EM_AMD64:
84 		ld_targ = *ld_targ_init_x86();
85 		break;
86 
87 	case EM_SPARC:
88 	case EM_SPARC32PLUS:
89 	case EM_SPARCV9:
90 		ld_targ = *ld_targ_init_sparc();
91 		break;
92 
93 	default:
94 		{
95 			Conv_inv_buf_t	inv_buf;
96 
97 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_TARG_UNSUPPORTED),
98 			    conv_ehdr_mach(mach, 0, &inv_buf));
99 			return (1);
100 		}
101 	}
102 
103 	return (0);
104 }
105 
106 
107 /*
108  * The main program
109  */
110 int
111 ld_main(int argc, char **argv, Half mach)
112 {
113 	char		*sgs_support;	/* SGS_SUPPORT environment string */
114 	Half		etype;
115 	int		suplib = 0;
116 	Ofl_desc	*ofl;
117 
118 	/*
119 	 * Initialize signal handlers, and output file variables.  Establish a
120 	 * default output ELF header to satisfy diagnostic requirements.
121 	 */
122 	if ((ofl = libld_calloc(1, sizeof (Ofl_desc))) == 0)
123 		return (1);
124 
125 	/* Initilize target state */
126 	if (ld_init_target(NULL, mach) != 0)
127 		return (1);
128 
129 	/*
130 	 * Set up the output ELF header, and initialize the machine
131 	 * and class details.
132 	 */
133 	ofl->ofl_dehdr = &def_ehdr;
134 	def_ehdr.e_ident[EI_CLASS] = ld_targ.t_m.m_class;
135 	def_ehdr.e_ident[EI_DATA] = ld_targ.t_m.m_data;
136 	def_ehdr.e_machine = ld_targ.t_m.m_mach;
137 
138 	ld_init(ofl);
139 
140 	/*
141 	 * Build up linker version string
142 	 */
143 	if ((ofl->ofl_sgsid = (char *)libld_calloc(MSG_SGS_ID_SIZE +
144 	    strlen(link_ver_string) + 1, 1)) == NULL)
145 		return (1);
146 	(void) strcpy(ofl->ofl_sgsid, MSG_ORIG(MSG_SGS_ID));
147 	(void) strcat(ofl->ofl_sgsid, link_ver_string);
148 
149 	/*
150 	 * Argument pass one.  Get all the input flags (skip any files) and
151 	 * check for consistency.  After this point any map file processing
152 	 * would have been completed and the entrance criteria and segment
153 	 * descriptor lists will be complete.
154 	 */
155 	if (ld_process_flags(ofl, argc, argv) == S_ERROR)
156 		return (1);
157 	if (ofl->ofl_flags & FLG_OF_FATAL) {
158 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_FLAGS));
159 		return (1);
160 	}
161 
162 	/*
163 	 * At this point a call such as ld -V is considered complete.
164 	 */
165 	if (ofl->ofl_flags1 & FLG_OF1_DONE)
166 		return (0);
167 
168 	/*
169 	 * Determine whether any support libraries been loaded (either through
170 	 * the SGS_SUPPORT environment variable and/or through the -S option).
171 	 * By default the support library libldstab.so.1 is loaded provided the
172 	 * user hasn't specified their own -S libraries.
173 	 */
174 #if	defined(_LP64)
175 	if ((sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT_64))) == NULL)
176 #else
177 	if ((sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT_32))) == NULL)
178 #endif
179 		sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT));
180 
181 	if (sgs_support && (*sgs_support != '\0')) {
182 		const char	*sep = MSG_ORIG(MSG_STR_COLON);
183 		char		*lib;
184 		char		*lasts;
185 
186 		DBG_CALL(Dbg_support_req(ofl->ofl_lml, sgs_support,
187 		    DBG_SUP_ENVIRON));
188 		if ((lib = strtok_r(sgs_support, sep, &lasts)) != NULL) {
189 			do {
190 				if (strcmp(lib,
191 				    MSG_ORIG(MSG_FIL_LIBSTAB)) == 0) {
192 					if (suplib++)
193 						continue;
194 				}
195 				if (ld_sup_loadso(ofl, lib) == S_ERROR)
196 					return (ld_exit(ofl));
197 
198 			} while ((lib = strtok_r(NULL, sep, &lasts)) != NULL);
199 		}
200 		DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
201 	}
202 	if (lib_support.head) {
203 		Listnode	*lnp;
204 		char		*lib;
205 
206 		for (LIST_TRAVERSE(&lib_support, lnp, lib)) {
207 			DBG_CALL(Dbg_support_req(ofl->ofl_lml, lib,
208 			    DBG_SUP_CMDLINE));
209 			if (ld_sup_loadso(ofl, lib) == S_ERROR)
210 				return (ld_exit(ofl));
211 		}
212 		DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
213 	} else {
214 		if (suplib == 0) {
215 			DBG_CALL(Dbg_support_req(ofl->ofl_lml,
216 			    MSG_ORIG(MSG_FIL_LIBSTAB), DBG_SUP_DEFAULT));
217 			if (ld_sup_loadso(ofl, MSG_ORIG(MSG_FIL_LIBSTAB)) ==
218 			    S_ERROR)
219 				return (ld_exit(ofl));
220 		}
221 		DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
222 	}
223 
224 	DBG_CALL(Dbg_ent_print(ofl->ofl_lml, ofl->ofl_dehdr->e_machine,
225 	    &ofl->ofl_ents, (ofl->ofl_flags & FLG_OF_DYNAMIC) != 0));
226 	DBG_CALL(Dbg_seg_list(ofl->ofl_lml, ofl->ofl_dehdr->e_machine,
227 	    &ofl->ofl_segs));
228 
229 	/*
230 	 * The objscnt and soscnt variables were used to estimate the expected
231 	 * input files, and size the symbol hash buckets accordingly.  Reset
232 	 * these values now, so as to gain an accurate count from pass two, for
233 	 * later statistics diagnostics.
234 	 */
235 	ofl->ofl_objscnt = ofl->ofl_soscnt = 0;
236 
237 	/*
238 	 * Determine whether we can create the file before going any further.
239 	 */
240 	if (ld_open_outfile(ofl) == S_ERROR)
241 		return (ld_exit(ofl));
242 
243 	/*
244 	 * If the user didn't supply a library path supply a default.  And, if
245 	 * no run-path has been specified (-R), see if the environment variable
246 	 * is in use (historic).  Also assign a default starting address.
247 	 * Don't use MSG_ORIG() for these strings, they're written to later.
248 	 */
249 	if (Plibpath == NULL)
250 		Plibpath = def_Plibpath;
251 
252 	if (ofl->ofl_rpath == NULL) {
253 		char *rpath;
254 		if (((rpath = getenv(MSG_ORIG(MSG_LD_RUN_PATH))) != NULL) &&
255 		    (strcmp((const char *)rpath, MSG_ORIG(MSG_STR_EMPTY))))
256 			ofl->ofl_rpath = rpath;
257 	}
258 	if (ofl->ofl_flags & FLG_OF_EXEC)
259 		ofl->ofl_segorigin = ld_targ.t_m.m_segm_origin;
260 
261 	/*
262 	 * Argument pass two.  Input all libraries and objects.
263 	 */
264 	if (ld_lib_setup(ofl) == S_ERROR)
265 		return (ld_exit(ofl));
266 
267 	/*
268 	 * Call ld_start() with the etype of our output file and the
269 	 * output file name.
270 	 */
271 	if (ofl->ofl_flags & FLG_OF_SHAROBJ)
272 		etype = ET_DYN;
273 	else if (ofl->ofl_flags & FLG_OF_RELOBJ)
274 		etype = ET_REL;
275 	else
276 		etype = ET_EXEC;
277 
278 	ld_sup_start(ofl, etype, argv[0]);
279 
280 	/*
281 	 * Process all input files.
282 	 */
283 	if (ld_process_files(ofl, argc, argv) == S_ERROR)
284 		return (ld_exit(ofl));
285 	if (ofl->ofl_flags & FLG_OF_FATAL) {
286 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_FILES),
287 		    ofl->ofl_name);
288 		return (ld_exit(ofl));
289 	}
290 
291 	ld_sup_input_done(ofl);
292 
293 	/*
294 	 * If there were any partially initialized symbol,
295 	 * do preparation works.
296 	 */
297 	if (ofl->ofl_ismove.head != 0) {
298 		if (ld_sunwmove_preprocess(ofl) == S_ERROR)
299 			return (ld_exit(ofl));
300 	}
301 
302 	/*
303 	 * Before validating all symbols count the number of relocation entries.
304 	 * If copy relocations exist, COMMON symbols must be generated which are
305 	 * assigned to the executables .bss.  During sym_validate() the actual
306 	 * size and alignment of the .bss is calculated.  Doing things in this
307 	 * order reduces the number of symbol table traversals required (however
308 	 * it does take a little longer for the user to be told of any undefined
309 	 * symbol errors).
310 	 */
311 	if (ld_reloc_init(ofl) == S_ERROR)
312 		return (ld_exit(ofl));
313 
314 	/*
315 	 * Now that all symbol processing is complete see if any undefined
316 	 * references still remain.  If we observed undefined symbols the
317 	 * FLG_OF_FATAL bit will be set:  If creating a static executable, or a
318 	 * dynamic executable or shared object with the -zdefs flag set, this
319 	 * condition is fatal.  If creating a shared object with the -Bsymbolic
320 	 * flag set, this condition is simply a warning.
321 	 */
322 	if (ld_sym_validate(ofl) == S_ERROR)
323 		return (ld_exit(ofl));
324 
325 	if (ofl->ofl_flags1 & FLG_OF1_OVRFLW) {
326 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_FILES),
327 		    ofl->ofl_name);
328 		return (ld_exit(ofl));
329 	} else if (ofl->ofl_flags & FLG_OF_FATAL) {
330 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_SYM_FATAL),
331 		    ofl->ofl_name);
332 		return (ld_exit(ofl));
333 	} else if (ofl->ofl_flags & FLG_OF_WARN)
334 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_ARG_SYM_WARN));
335 
336 	/*
337 	 * Generate any necessary sections.
338 	 */
339 	if (ld_make_sections(ofl) == S_ERROR)
340 		return (ld_exit(ofl));
341 
342 	/*
343 	 * Now that all sections have been added to the output file, check to
344 	 * see if any section ordering was specified and if so give a warning
345 	 * if any ordering directives were not matched.
346 	 * Also, if SHF_ORDERED sections exist, set up sort key values.
347 	 */
348 	ld_sec_validate(ofl);
349 
350 	/*
351 	 * Having collected all the input data create the initial output file
352 	 * image, assign virtual addresses to the image, and generate a load
353 	 * map if the user requested one.
354 	 */
355 	if (ld_create_outfile(ofl) == S_ERROR)
356 		return (ld_exit(ofl));
357 
358 	if (ld_update_outfile(ofl) == S_ERROR)
359 		return (ld_exit(ofl));
360 	if (ofl->ofl_flags & FLG_OF_GENMAP)
361 		ld_map_out(ofl);
362 
363 	/*
364 	 * Build relocation sections and perform any relocation updates.
365 	 */
366 	if (ld_reloc_process(ofl) == S_ERROR)
367 		return (ld_exit(ofl));
368 
369 #if	defined(_ELF64)
370 	/*
371 	 * Fill in contents for Unwind Header
372 	 */
373 	if ((ld_targ.t_uw.uw_populate_unwindhdr != NULL) &&
374 	    ((*ld_targ.t_uw.uw_populate_unwindhdr)(ofl) == S_ERROR))
375 		return (ld_exit(ofl));
376 #endif
377 
378 	/*
379 	 * Finally create the files elf checksum.
380 	 */
381 	if (ofl->ofl_checksum)
382 		*ofl->ofl_checksum = (Xword)elf_checksum(ofl->ofl_elf);
383 
384 	/*
385 	 * If this is a cross link to a target with a different byte
386 	 * order than the linker, swap the data to the target byte order.
387 	 */
388 	if (((ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0) &&
389 	    (_elf_swap_wrimage(ofl->ofl_elf) != 0)) {
390 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_SWAP_WRIMAGE),
391 		    ofl->ofl_name);
392 		return (ld_exit(ofl));
393 	}
394 
395 	/*
396 	 * We're done, so make sure the updates are flushed to the output file.
397 	 */
398 	if ((ofl->ofl_size = elf_update(ofl->ofl_welf, ELF_C_WRITE)) == 0) {
399 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_UPDATE),
400 		    ofl->ofl_name);
401 		return (ld_exit(ofl));
402 	}
403 
404 	ld_sup_atexit(ofl, 0);
405 
406 	DBG_CALL(Dbg_statistics_ld(ofl));
407 
408 	/*
409 	 * For performance reasons we don't actually free up the memory we've
410 	 * allocated, it will be freed when we exit.
411 	 *
412 	 * But the below line can be uncommented if/when we want to measure how
413 	 * our memory consumption and freeing are doing.  We should be able to
414 	 * free all the memory that has been allocated as part of the link-edit
415 	 * process.
416 	 *
417 	 * ofl_cleanup(ofl);
418 	 */
419 	return (0);
420 }
421 
422 /*
423  * Cleanup an Ifl_desc.
424  */
425 static void
426 ifl_list_cleanup(List *ifl_list)
427 {
428 	Listnode	*lnp;
429 	Ifl_desc	*ifl;
430 
431 	for (LIST_TRAVERSE(ifl_list, lnp, ifl))
432 		if (ifl->ifl_elf)
433 			(void) elf_end(ifl->ifl_elf);
434 	ifl_list->head = 0;
435 	ifl_list->tail = 0;
436 }
437 
438 /*
439  * Cleanup all memory that has been dynamically allocated during libld
440  * processing and elf_end() all Elf descriptors that are still open.
441  */
442 void
443 ld_ofl_cleanup(Ofl_desc *ofl)
444 {
445 	Ld_heap		*chp, *php;
446 	Ar_desc		*adp;
447 	Listnode	*lnp;
448 
449 	ifl_list_cleanup(&ofl->ofl_objs);
450 	ifl_list_cleanup(&ofl->ofl_sos);
451 
452 	for (LIST_TRAVERSE(&ofl->ofl_ars, lnp, adp)) {
453 		Ar_aux		*aup;
454 		Elf_Arsym	*arsym;
455 
456 		for (arsym = adp->ad_start, aup = adp->ad_aux;
457 		    arsym->as_name; ++arsym, ++aup) {
458 			if ((aup->au_mem) && (aup->au_mem != FLG_ARMEM_PROC)) {
459 				(void) elf_end(aup->au_mem->am_elf);
460 
461 				/*
462 				 * Null out all entries to this member so
463 				 * that we don't attempt to elf_end() it again.
464 				 */
465 				ld_ar_member(adp, arsym, aup, 0);
466 			}
467 		}
468 		(void) elf_end(adp->ad_elf);
469 	}
470 
471 	(void) elf_end(ofl->ofl_elf);
472 	(void) elf_end(ofl->ofl_welf);
473 
474 	for (chp = ld_heap, php = 0; chp; php = chp, chp = chp->lh_next) {
475 		if (php)
476 			(void) munmap((void *)php,
477 			    (size_t)php->lh_end - (size_t)php);
478 	}
479 	if (php)
480 		(void) munmap((void *)php, (size_t)php->lh_end - (size_t)php);
481 
482 	ld_heap = 0;
483 }
484