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