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