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