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