xref: /titanic_51/usr/src/cmd/sgs/libld/common/ldmain.c (revision dfe2803f795c652b44d48bd64b83f039273b34ee)
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.  Return from ld_process_flags() marks the
150 	 * end of mapfile processing.  The entrance criteria and segment
151 	 * descriptors are complete and in their final form.
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) {
195 		Aliste	idx;
196 		char	*lib;
197 
198 		for (APLIST_TRAVERSE(lib_support, idx, 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 	 * Now that all input section processing is complete, validate and
275 	 * process any SHT_SUNW_move sections.
276 	 */
277 	if (ofl->ofl_ismove && (ld_process_move(ofl) == S_ERROR))
278 		return (ld_exit(ofl));
279 
280 	/*
281 	 * Before validating all symbols count the number of relocation entries.
282 	 * If copy relocations exist, COMMON symbols must be generated which are
283 	 * assigned to the executables .bss.  During sym_validate() the actual
284 	 * size and alignment of the .bss is calculated.  Doing things in this
285 	 * order reduces the number of symbol table traversals required (however
286 	 * it does take a little longer for the user to be told of any undefined
287 	 * symbol errors).
288 	 */
289 	if (ld_reloc_init(ofl) == S_ERROR)
290 		return (ld_exit(ofl));
291 
292 	/*
293 	 * Now that all symbol processing is complete see if any undefined
294 	 * references still remain.  If we observed undefined symbols the
295 	 * FLG_OF_FATAL bit will be set:  If creating a static executable, or a
296 	 * dynamic executable or shared object with the -zdefs flag set, this
297 	 * condition is fatal.  If creating a shared object with the -Bsymbolic
298 	 * flag set, this condition is simply a warning.
299 	 */
300 	if (ld_sym_validate(ofl) == S_ERROR)
301 		return (ld_exit(ofl));
302 
303 	if (ofl->ofl_flags1 & FLG_OF1_OVRFLW) {
304 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_FILES),
305 		    ofl->ofl_name);
306 		return (ld_exit(ofl));
307 	} else if (ofl->ofl_flags & FLG_OF_FATAL) {
308 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_SYM_FATAL),
309 		    ofl->ofl_name);
310 		return (ld_exit(ofl));
311 	} else if (ofl->ofl_flags & FLG_OF_WARN)
312 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_ARG_SYM_WARN));
313 
314 	/*
315 	 * Generate any necessary sections.
316 	 */
317 	if (ld_make_sections(ofl) == S_ERROR)
318 		return (ld_exit(ofl));
319 
320 	/*
321 	 * Now that all sections have been added to the output file, determine
322 	 * whether any mapfile section ordering was specified, and verify that
323 	 * all mapfile ordering directives have been matched.  Issue a warning
324 	 * for any directives that have not been matched.
325 	 * Also, if SHF_ORDERED sections exist, set up sort key values.
326 	 */
327 	if (ofl->ofl_flags & (FLG_OF_SECORDER | FLG_OF_KEY))
328 		ld_sec_validate(ofl);
329 
330 	/*
331 	 * Having collected all the input data create the initial output file
332 	 * image, assign virtual addresses to the image, and generate a load
333 	 * map if the user requested one.
334 	 */
335 	if (ld_create_outfile(ofl) == S_ERROR)
336 		return (ld_exit(ofl));
337 
338 	if (ld_update_outfile(ofl) == S_ERROR)
339 		return (ld_exit(ofl));
340 	if (ofl->ofl_flags & FLG_OF_GENMAP)
341 		ld_map_out(ofl);
342 
343 	/*
344 	 * Build relocation sections and perform any relocation updates.
345 	 */
346 	if (ld_reloc_process(ofl) == S_ERROR)
347 		return (ld_exit(ofl));
348 
349 	/*
350 	 * Fill in contents for unwind header (.eh_frame_hdr)
351 	 */
352 	if (ld_unwind_populate_hdr(ofl) == S_ERROR)
353 		return (ld_exit(ofl));
354 
355 	/*
356 	 * Finally create the files elf checksum.
357 	 */
358 	if (ofl->ofl_checksum)
359 		*ofl->ofl_checksum = (Xword)elf_checksum(ofl->ofl_elf);
360 
361 	/*
362 	 * If this is a cross link to a target with a different byte
363 	 * order than the linker, swap the data to the target byte order.
364 	 */
365 	if (((ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0) &&
366 	    (_elf_swap_wrimage(ofl->ofl_elf) != 0)) {
367 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_SWAP_WRIMAGE),
368 		    ofl->ofl_name);
369 		return (ld_exit(ofl));
370 	}
371 
372 	/*
373 	 * We're done, so make sure the updates are flushed to the output file.
374 	 */
375 	if ((ofl->ofl_size = elf_update(ofl->ofl_welf, ELF_C_WRITE)) == 0) {
376 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_UPDATE),
377 		    ofl->ofl_name);
378 		return (ld_exit(ofl));
379 	}
380 
381 	ld_sup_atexit(ofl, 0);
382 
383 	DBG_CALL(Dbg_statistics_ld(ofl));
384 
385 	/*
386 	 * For performance reasons we don't actually free up the memory we've
387 	 * allocated, it will be freed when we exit.
388 	 *
389 	 * But the below line can be uncommented if/when we want to measure how
390 	 * our memory consumption and freeing are doing.  We should be able to
391 	 * free all the memory that has been allocated as part of the link-edit
392 	 * process.
393 	 *
394 	 * ofl_cleanup(ofl);
395 	 */
396 	return (0);
397 }
398 
399 /*
400  * Cleanup an Ifl_desc.
401  */
402 static void
403 ifl_list_cleanup(APlist *apl)
404 {
405 	Aliste		idx;
406 	Ifl_desc	*ifl;
407 
408 	for (APLIST_TRAVERSE(apl, idx, ifl)) {
409 		if (ifl->ifl_elf)
410 			(void) elf_end(ifl->ifl_elf);
411 	}
412 }
413 
414 /*
415  * Cleanup all memory that has been dynamically allocated during libld
416  * processing and elf_end() all Elf descriptors that are still open.
417  */
418 void
419 ld_ofl_cleanup(Ofl_desc *ofl)
420 {
421 	Ld_heap		*chp, *php;
422 	Ar_desc		*adp;
423 	Aliste		idx;
424 
425 	ifl_list_cleanup(ofl->ofl_objs);
426 	ofl->ofl_objs = NULL;
427 	ifl_list_cleanup(ofl->ofl_sos);
428 	ofl->ofl_sos = NULL;
429 
430 	for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) {
431 		Ar_aux		*aup;
432 		Elf_Arsym	*arsym;
433 
434 		for (arsym = adp->ad_start, aup = adp->ad_aux;
435 		    arsym->as_name; ++arsym, ++aup) {
436 			if ((aup->au_mem) && (aup->au_mem != FLG_ARMEM_PROC)) {
437 				(void) elf_end(aup->au_mem->am_elf);
438 
439 				/*
440 				 * Null out all entries to this member so
441 				 * that we don't attempt to elf_end() it again.
442 				 */
443 				ld_ar_member(adp, arsym, aup, 0);
444 			}
445 		}
446 		(void) elf_end(adp->ad_elf);
447 	}
448 	ofl->ofl_ars = NULL;
449 
450 	(void) elf_end(ofl->ofl_elf);
451 	(void) elf_end(ofl->ofl_welf);
452 
453 	for (chp = ld_heap, php = NULL; chp; php = chp, chp = chp->lh_next) {
454 		if (php)
455 			(void) munmap((void *)php,
456 			    (size_t)php->lh_end - (size_t)php);
457 	}
458 	if (php)
459 		(void) munmap((void *)php, (size_t)php->lh_end - (size_t)php);
460 
461 	ld_heap = NULL;
462 }
463