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