xref: /titanic_51/usr/src/cmd/sgs/libld/common/ldmain.c (revision 65cf7c958eb9457cf2c83a853d128e4ecfc187a6)
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 	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).  Also assign a default starting address.
230 	 * Don't use MSG_ORIG() for these strings, they're written to later.
231 	 */
232 	if (Plibpath == NULL)
233 		Plibpath = def_Plibpath;
234 
235 	if (ofl->ofl_rpath == NULL) {
236 		char *rpath;
237 		if (((rpath = getenv(MSG_ORIG(MSG_LD_RUN_PATH))) != NULL) &&
238 		    (strcmp((const char *)rpath, MSG_ORIG(MSG_STR_EMPTY))))
239 			ofl->ofl_rpath = rpath;
240 	}
241 	if (ofl->ofl_flags & FLG_OF_EXEC)
242 		ofl->ofl_segorigin = ld_targ.t_m.m_segm_origin;
243 
244 	/*
245 	 * Argument pass two.  Input all libraries and objects.
246 	 */
247 	if (ld_lib_setup(ofl) == S_ERROR)
248 		return (ld_exit(ofl));
249 
250 	/*
251 	 * Call ld_start() with the etype of our output file and the
252 	 * output file name.
253 	 */
254 	if (ofl->ofl_flags & FLG_OF_SHAROBJ)
255 		etype = ET_DYN;
256 	else if (ofl->ofl_flags & FLG_OF_RELOBJ)
257 		etype = ET_REL;
258 	else
259 		etype = ET_EXEC;
260 
261 	ld_sup_start(ofl, etype, argv[0]);
262 
263 	/*
264 	 * Process all input files.
265 	 */
266 	if (ld_process_files(ofl, argc, argv) == S_ERROR)
267 		return (ld_exit(ofl));
268 	if (ofl->ofl_flags & FLG_OF_FATAL) {
269 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_FILES),
270 		    ofl->ofl_name);
271 		return (ld_exit(ofl));
272 	}
273 
274 	ld_sup_input_done(ofl);
275 
276 	/*
277 	 * If there were any partially initialized symbol,
278 	 * do preparation works.
279 	 */
280 	if (ofl->ofl_ismove.head != 0) {
281 		if (ld_sunwmove_preprocess(ofl) == S_ERROR)
282 			return (ld_exit(ofl));
283 	}
284 
285 	/*
286 	 * Before validating all symbols count the number of relocation entries.
287 	 * If copy relocations exist, COMMON symbols must be generated which are
288 	 * assigned to the executables .bss.  During sym_validate() the actual
289 	 * size and alignment of the .bss is calculated.  Doing things in this
290 	 * order reduces the number of symbol table traversals required (however
291 	 * it does take a little longer for the user to be told of any undefined
292 	 * symbol errors).
293 	 */
294 	if (ld_reloc_init(ofl) == S_ERROR)
295 		return (ld_exit(ofl));
296 
297 	/*
298 	 * Now that all symbol processing is complete see if any undefined
299 	 * references still remain.  If we observed undefined symbols the
300 	 * FLG_OF_FATAL bit will be set:  If creating a static executable, or a
301 	 * dynamic executable or shared object with the -zdefs flag set, this
302 	 * condition is fatal.  If creating a shared object with the -Bsymbolic
303 	 * flag set, this condition is simply a warning.
304 	 */
305 	if (ld_sym_validate(ofl) == S_ERROR)
306 		return (ld_exit(ofl));
307 
308 	if (ofl->ofl_flags1 & FLG_OF1_OVRFLW) {
309 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_FILES),
310 		    ofl->ofl_name);
311 		return (ld_exit(ofl));
312 	} else if (ofl->ofl_flags & FLG_OF_FATAL) {
313 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_SYM_FATAL),
314 		    ofl->ofl_name);
315 		return (ld_exit(ofl));
316 	} else if (ofl->ofl_flags & FLG_OF_WARN)
317 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_ARG_SYM_WARN));
318 
319 	/*
320 	 * Generate any necessary sections.
321 	 */
322 	if (ld_make_sections(ofl) == S_ERROR)
323 		return (ld_exit(ofl));
324 
325 	/*
326 	 * Now that all sections have been added to the output file, determine
327 	 * whether any mapfile section ordering was specified, and verify that
328 	 * all mapfile ordering directives have been matched.  Issue a warning
329 	 * for any directives that have not been matched.
330 	 * Also, if SHF_ORDERED sections exist, set up sort key values.
331 	 */
332 	if (ofl->ofl_flags & (FLG_OF_SECORDER | FLG_OF_KEY))
333 		ld_sec_validate(ofl);
334 
335 	/*
336 	 * Having collected all the input data create the initial output file
337 	 * image, assign virtual addresses to the image, and generate a load
338 	 * map if the user requested one.
339 	 */
340 	if (ld_create_outfile(ofl) == S_ERROR)
341 		return (ld_exit(ofl));
342 
343 	if (ld_update_outfile(ofl) == S_ERROR)
344 		return (ld_exit(ofl));
345 	if (ofl->ofl_flags & FLG_OF_GENMAP)
346 		ld_map_out(ofl);
347 
348 	/*
349 	 * Build relocation sections and perform any relocation updates.
350 	 */
351 	if (ld_reloc_process(ofl) == S_ERROR)
352 		return (ld_exit(ofl));
353 
354 #if	defined(_ELF64)
355 	/*
356 	 * Fill in contents for Unwind Header
357 	 */
358 	if ((ld_targ.t_uw.uw_populate_unwindhdr != NULL) &&
359 	    ((*ld_targ.t_uw.uw_populate_unwindhdr)(ofl) == S_ERROR))
360 		return (ld_exit(ofl));
361 #endif
362 
363 	/*
364 	 * Finally create the files elf checksum.
365 	 */
366 	if (ofl->ofl_checksum)
367 		*ofl->ofl_checksum = (Xword)elf_checksum(ofl->ofl_elf);
368 
369 	/*
370 	 * If this is a cross link to a target with a different byte
371 	 * order than the linker, swap the data to the target byte order.
372 	 */
373 	if (((ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0) &&
374 	    (_elf_swap_wrimage(ofl->ofl_elf) != 0)) {
375 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_SWAP_WRIMAGE),
376 		    ofl->ofl_name);
377 		return (ld_exit(ofl));
378 	}
379 
380 	/*
381 	 * We're done, so make sure the updates are flushed to the output file.
382 	 */
383 	if ((ofl->ofl_size = elf_update(ofl->ofl_welf, ELF_C_WRITE)) == 0) {
384 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_UPDATE),
385 		    ofl->ofl_name);
386 		return (ld_exit(ofl));
387 	}
388 
389 	ld_sup_atexit(ofl, 0);
390 
391 	DBG_CALL(Dbg_statistics_ld(ofl));
392 
393 	/*
394 	 * For performance reasons we don't actually free up the memory we've
395 	 * allocated, it will be freed when we exit.
396 	 *
397 	 * But the below line can be uncommented if/when we want to measure how
398 	 * our memory consumption and freeing are doing.  We should be able to
399 	 * free all the memory that has been allocated as part of the link-edit
400 	 * process.
401 	 *
402 	 * ofl_cleanup(ofl);
403 	 */
404 	return (0);
405 }
406 
407 /*
408  * Cleanup an Ifl_desc.
409  */
410 static void
411 ifl_list_cleanup(List *ifl_list)
412 {
413 	Listnode	*lnp;
414 	Ifl_desc	*ifl;
415 
416 	for (LIST_TRAVERSE(ifl_list, lnp, ifl))
417 		if (ifl->ifl_elf)
418 			(void) elf_end(ifl->ifl_elf);
419 	ifl_list->head = 0;
420 	ifl_list->tail = 0;
421 }
422 
423 /*
424  * Cleanup all memory that has been dynamically allocated during libld
425  * processing and elf_end() all Elf descriptors that are still open.
426  */
427 void
428 ld_ofl_cleanup(Ofl_desc *ofl)
429 {
430 	Ld_heap		*chp, *php;
431 	Ar_desc		*adp;
432 	Listnode	*lnp;
433 
434 	ifl_list_cleanup(&ofl->ofl_objs);
435 	ifl_list_cleanup(&ofl->ofl_sos);
436 
437 	for (LIST_TRAVERSE(&ofl->ofl_ars, lnp, adp)) {
438 		Ar_aux		*aup;
439 		Elf_Arsym	*arsym;
440 
441 		for (arsym = adp->ad_start, aup = adp->ad_aux;
442 		    arsym->as_name; ++arsym, ++aup) {
443 			if ((aup->au_mem) && (aup->au_mem != FLG_ARMEM_PROC)) {
444 				(void) elf_end(aup->au_mem->am_elf);
445 
446 				/*
447 				 * Null out all entries to this member so
448 				 * that we don't attempt to elf_end() it again.
449 				 */
450 				ld_ar_member(adp, arsym, aup, 0);
451 			}
452 		}
453 		(void) elf_end(adp->ad_elf);
454 	}
455 
456 	(void) elf_end(ofl->ofl_elf);
457 	(void) elf_end(ofl->ofl_welf);
458 
459 	for (chp = ld_heap, php = 0; chp; php = chp, chp = chp->lh_next) {
460 		if (php)
461 			(void) munmap((void *)php,
462 			    (size_t)php->lh_end - (size_t)php);
463 	}
464 	if (php)
465 		(void) munmap((void *)php, (size_t)php->lh_end - (size_t)php);
466 
467 	ld_heap = 0;
468 }
469