xref: /titanic_44/usr/src/cmd/sgs/libld/common/ldmain.c (revision 67dbe2be0c0f1e2eb428b89088bb5667e8f0b9f6)
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 	/* 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, (ofl->ofl_flags & FLG_OF_DYNAMIC) != 0));
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 		if (((rpath = getenv(MSG_ORIG(MSG_LD_RUN_PATH))) != NULL) &&
245 		    (strcmp((const char *)rpath, MSG_ORIG(MSG_STR_EMPTY))))
246 			ofl->ofl_rpath = rpath;
247 	}
248 
249 	/*
250 	 * Argument pass two.  Input all libraries and objects.
251 	 */
252 	if (ld_lib_setup(ofl) == S_ERROR)
253 		return (ld_exit(ofl));
254 
255 	/*
256 	 * Call ld_start() with the etype of our output file and the
257 	 * output file name.
258 	 */
259 	if (ofl->ofl_flags & FLG_OF_SHAROBJ)
260 		etype = ET_DYN;
261 	else if (ofl->ofl_flags & FLG_OF_RELOBJ)
262 		etype = ET_REL;
263 	else
264 		etype = ET_EXEC;
265 
266 	ld_sup_start(ofl, etype, argv[0]);
267 
268 	/*
269 	 * Process all input files.
270 	 */
271 	if (ld_process_files(ofl, argc, argv) == S_ERROR)
272 		return (ld_exit(ofl));
273 	if (ofl->ofl_flags & FLG_OF_FATAL) {
274 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_FILES),
275 		    ofl->ofl_name);
276 		return (ld_exit(ofl));
277 	}
278 
279 	ld_sup_input_done(ofl);
280 
281 	/*
282 	 * Now that all input section processing is complete, validate and
283 	 * process any SHT_SUNW_move sections.
284 	 */
285 	if (ofl->ofl_ismove && (ld_process_move(ofl) == S_ERROR))
286 		return (ld_exit(ofl));
287 
288 	/*
289 	 * Before validating all symbols count the number of relocation entries.
290 	 * If copy relocations exist, COMMON symbols must be generated which are
291 	 * assigned to the executables .bss.  During sym_validate() the actual
292 	 * size and alignment of the .bss is calculated.  Doing things in this
293 	 * order reduces the number of symbol table traversals required (however
294 	 * it does take a little longer for the user to be told of any undefined
295 	 * symbol errors).
296 	 */
297 	if (ld_reloc_init(ofl) == S_ERROR)
298 		return (ld_exit(ofl));
299 
300 	/*
301 	 * Now that all symbol processing is complete see if any undefined
302 	 * references still remain.  If we observed undefined symbols the
303 	 * FLG_OF_FATAL bit will be set:  If creating a static executable, or a
304 	 * dynamic executable or shared object with the -zdefs flag set, this
305 	 * condition is fatal.  If creating a shared object with the -Bsymbolic
306 	 * flag set, this condition is simply a warning.
307 	 */
308 	if (ld_sym_validate(ofl) == S_ERROR)
309 		return (ld_exit(ofl));
310 
311 	if (ofl->ofl_flags1 & FLG_OF1_OVRFLW) {
312 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_FILES),
313 		    ofl->ofl_name);
314 		return (ld_exit(ofl));
315 	} else if (ofl->ofl_flags & FLG_OF_FATAL) {
316 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_SYM_FATAL),
317 		    ofl->ofl_name);
318 		return (ld_exit(ofl));
319 	} else if (ofl->ofl_flags & FLG_OF_WARN)
320 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_ARG_SYM_WARN));
321 
322 	/*
323 	 * Generate any necessary sections.
324 	 */
325 	if (ld_make_sections(ofl) == S_ERROR)
326 		return (ld_exit(ofl));
327 
328 	/*
329 	 * Now that all sections have been added to the output file, determine
330 	 * whether any mapfile section ordering was specified, and verify that
331 	 * all mapfile ordering directives have been matched.  Issue a warning
332 	 * for any directives that have not been matched.
333 	 * Also, if SHF_ORDERED sections exist, set up sort key values.
334 	 */
335 	if (ofl->ofl_flags & (FLG_OF_SECORDER | FLG_OF_KEY))
336 		ld_sec_validate(ofl);
337 
338 	/*
339 	 * Having collected all the input data create the initial output file
340 	 * image, assign virtual addresses to the image, and generate a load
341 	 * map if the user requested one.
342 	 */
343 	if (ld_create_outfile(ofl) == S_ERROR)
344 		return (ld_exit(ofl));
345 
346 	if (ld_update_outfile(ofl) == S_ERROR)
347 		return (ld_exit(ofl));
348 	if (ofl->ofl_flags & FLG_OF_GENMAP)
349 		ld_map_out(ofl);
350 
351 	/*
352 	 * Build relocation sections and perform any relocation updates.
353 	 */
354 	if (ld_reloc_process(ofl) == S_ERROR)
355 		return (ld_exit(ofl));
356 
357 	/*
358 	 * Fill in contents for unwind header (.eh_frame_hdr)
359 	 */
360 	if (ld_unwind_populate_hdr(ofl) == S_ERROR)
361 		return (ld_exit(ofl));
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 	DBG_CALL(Dbg_basic_finish(ofl->ofl_lml));
393 
394 	/*
395 	 * Wrap up debug output file if one is open
396 	 */
397 	dbg_cleanup();
398 
399 	/*
400 	 * For performance reasons we don't actually free up the memory we've
401 	 * allocated, it will be freed when we exit.
402 	 *
403 	 * But the below line can be uncommented if/when we want to measure how
404 	 * our memory consumption and freeing are doing.  We should be able to
405 	 * free all the memory that has been allocated as part of the link-edit
406 	 * process.
407 	 */
408 	/* ld_ofl_cleanup(ofl); */
409 	return (0);
410 }
411 
412 /*
413  * Cleanup an Ifl_desc.
414  */
415 static void
416 ifl_list_cleanup(APlist *apl)
417 {
418 	Aliste		idx;
419 	Ifl_desc	*ifl;
420 
421 	for (APLIST_TRAVERSE(apl, idx, ifl)) {
422 		if (ifl->ifl_elf)
423 			(void) elf_end(ifl->ifl_elf);
424 	}
425 }
426 
427 /*
428  * Cleanup all memory that has been dynamically allocated during libld
429  * processing and elf_end() all Elf descriptors that are still open.
430  */
431 void
432 ld_ofl_cleanup(Ofl_desc *ofl)
433 {
434 	Ld_heap		*chp, *php;
435 	Ar_desc		*adp;
436 	Aliste		idx;
437 
438 	ifl_list_cleanup(ofl->ofl_objs);
439 	ofl->ofl_objs = NULL;
440 	ifl_list_cleanup(ofl->ofl_sos);
441 	ofl->ofl_sos = NULL;
442 
443 	for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) {
444 		Ar_aux		*aup;
445 		Elf_Arsym	*arsym;
446 
447 		for (arsym = adp->ad_start, aup = adp->ad_aux;
448 		    arsym->as_name; ++arsym, ++aup) {
449 			if ((aup->au_mem) && (aup->au_mem != FLG_ARMEM_PROC)) {
450 				(void) elf_end(aup->au_mem->am_elf);
451 
452 				/*
453 				 * Null out all entries to this member so
454 				 * that we don't attempt to elf_end() it again.
455 				 */
456 				ld_ar_member(adp, arsym, aup, 0);
457 			}
458 		}
459 		(void) elf_end(adp->ad_elf);
460 	}
461 	ofl->ofl_ars = NULL;
462 
463 	(void) elf_end(ofl->ofl_elf);
464 	(void) elf_end(ofl->ofl_welf);
465 
466 	for (chp = ld_heap, php = NULL; chp; php = chp, chp = chp->lh_next) {
467 		if (php)
468 			(void) munmap((void *)php,
469 			    (size_t)php->lh_end - (size_t)php);
470 	}
471 	if (php)
472 		(void) munmap((void *)php, (size_t)php->lh_end - (size_t)php);
473 
474 	ld_heap = NULL;
475 }
476