xref: /titanic_50/usr/src/cmd/sgs/libld/common/ldmain.c (revision c77a61a72b5ecdc507d6cf104142edd371a16c84)
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 2006 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * ld -- link/editor main program
33  */
34 #include	<sys/types.h>
35 #include	<sys/mman.h>
36 #include	<string.h>
37 #include	<stdio.h>
38 #include	<locale.h>
39 #include	<stdarg.h>
40 #include	<debug.h>
41 #include	"msg.h"
42 #include	"_libld.h"
43 
44 /*
45  * A default library search path is used if one was not supplied on the command
46  * line.  Note: these strings can not use MSG_ORIG() since they are modified as
47  * part of the path processing.
48  */
49 #if	defined(_ELF64)
50 static char	def_Plibpath[] = "/lib/64:/usr/lib/64";
51 #else
52 static char	def_Plibpath[] = "/usr/ccs/lib:/lib:/usr/lib";
53 #endif
54 
55 /*
56  * A default elf header provides for simplifying diagnostic processing.
57  */
58 static Ehdr	def_ehdr = { { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3,
59 			    M_CLASS, M_DATA }, 0, M_MACH, EV_CURRENT };
60 
61 /*
62  * The main program
63  */
64 int
65 ld_main(int argc, char **argv)
66 {
67 	char		*sgs_support;	/* SGS_SUPPORT environment string */
68 	Half		etype;
69 	int		suplib = 0;
70 	Ofl_desc	*ofl;
71 
72 	/*
73 	 * Initialize signal handlers, and output file variables.  Establish a
74 	 * default output ELF header to satisfy diagnostic requirements.
75 	 */
76 	if ((ofl = libld_calloc(1, sizeof (Ofl_desc))) == 0)
77 		return (1);
78 
79 	ofl->ofl_dehdr = &def_ehdr;
80 
81 	ld_init(ofl);
82 
83 	/*
84 	 * Build up linker version string
85 	 */
86 	if ((ofl->ofl_sgsid = (char *)libld_calloc(MSG_SGS_ID_SIZE +
87 	    strlen(link_ver_string) + 1, 1)) == NULL)
88 		return (1);
89 	(void) strcpy(ofl->ofl_sgsid, MSG_ORIG(MSG_SGS_ID));
90 	(void) strcat(ofl->ofl_sgsid, link_ver_string);
91 
92 	/*
93 	 * Argument pass one.  Get all the input flags (skip any files) and
94 	 * check for consistency.  After this point any map file processing
95 	 * would have been completed and the entrance criteria and segment
96 	 * descriptor lists will be complete.
97 	 */
98 	if (ld_process_flags(ofl, argc, argv) == S_ERROR)
99 		return (1);
100 	if (ofl->ofl_flags & FLG_OF_FATAL) {
101 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_FLAGS));
102 		return (1);
103 	}
104 
105 	/*
106 	 * At this point a call such as ld -V is considered complete.
107 	 */
108 	if (ofl->ofl_flags1 & FLG_OF1_DONE)
109 		return (0);
110 
111 	/*
112 	 * Determine whether any support libraries been loaded (either through
113 	 * the SGS_SUPPORT environment variable and/or through the -S option).
114 	 * By default the support library libldstab.so.1 is loaded provided the
115 	 * user hasn't specified their own -S libraries.
116 	 */
117 #if	defined(_LP64)
118 	if ((sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT_64))) == NULL)
119 #else
120 	if ((sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT_32))) == NULL)
121 #endif
122 		sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT));
123 
124 	if (sgs_support && (*sgs_support != '\0')) {
125 		const char	*sep = MSG_ORIG(MSG_STR_COLON);
126 		char		*lib;
127 		char		*lasts;
128 
129 		DBG_CALL(Dbg_support_req(ofl->ofl_lml, sgs_support,
130 		    DBG_SUP_ENVIRON));
131 		if ((lib = strtok_r(sgs_support, sep, &lasts)) != NULL) {
132 			do {
133 				if (strcmp(lib,
134 				    MSG_ORIG(MSG_FIL_LIBSTAB)) == 0) {
135 					if (suplib++)
136 						continue;
137 				}
138 				if (ld_sup_loadso(ofl, lib) == S_ERROR)
139 					return (ld_exit(ofl));
140 
141 			} while ((lib = strtok_r(NULL, sep, &lasts)) != NULL);
142 		}
143 		DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
144 	}
145 	if (lib_support.head) {
146 		Listnode	*lnp;
147 		char		*lib;
148 
149 		for (LIST_TRAVERSE(&lib_support, lnp, lib)) {
150 			DBG_CALL(Dbg_support_req(ofl->ofl_lml, lib,
151 			    DBG_SUP_CMDLINE));
152 			if (ld_sup_loadso(ofl, lib) == S_ERROR)
153 				return (ld_exit(ofl));
154 		}
155 		DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
156 	} else {
157 		if (suplib == 0) {
158 			DBG_CALL(Dbg_support_req(ofl->ofl_lml,
159 			    MSG_ORIG(MSG_FIL_LIBSTAB), DBG_SUP_DEFAULT));
160 			if (ld_sup_loadso(ofl, MSG_ORIG(MSG_FIL_LIBSTAB)) ==
161 			    S_ERROR)
162 				return (ld_exit(ofl));
163 		}
164 		DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD));
165 	}
166 
167 	DBG_CALL(Dbg_ent_print(ofl->ofl_lml, ofl->ofl_dehdr->e_machine,
168 	    &ofl->ofl_ents, (ofl->ofl_flags & FLG_OF_DYNAMIC)));
169 	DBG_CALL(Dbg_seg_list(ofl->ofl_lml, ofl->ofl_dehdr->e_machine,
170 	    &ofl->ofl_segs));
171 
172 	/*
173 	 * The objscnt and soscnt variables were used to estimate the expected
174 	 * input files, and size the symbol hash buckets accordingly.  Reset
175 	 * these values now, so as to gain an accurate count from pass two, for
176 	 * later statistics diagnostics.
177 	 */
178 	ofl->ofl_objscnt = ofl->ofl_soscnt = 0;
179 
180 	/*
181 	 * Determine whether we can create the file before going any further.
182 	 */
183 	if (ld_open_outfile(ofl) == S_ERROR)
184 		return (ld_exit(ofl));
185 
186 	/*
187 	 * If the user didn't supply a library path supply a default.  And, if
188 	 * no run-path has been specified (-R), see if the environment variable
189 	 * is in use (historic).  Also assign a default starting address.
190 	 * Don't use MSG_ORIG() for these strings, they're written to later.
191 	 */
192 	if (Plibpath == NULL)
193 		Plibpath = def_Plibpath;
194 
195 	if (ofl->ofl_rpath == NULL) {
196 		char *rpath;
197 		if (((rpath = getenv(MSG_ORIG(MSG_LD_RUN_PATH))) != NULL) &&
198 		    (strcmp((const char *)rpath, MSG_ORIG(MSG_STR_EMPTY))))
199 			ofl->ofl_rpath = rpath;
200 	}
201 	if (ofl->ofl_flags & FLG_OF_EXEC)
202 		ofl->ofl_segorigin = M_SEGM_ORIGIN;
203 
204 	/*
205 	 * Argument pass two.  Input all libraries and objects.
206 	 */
207 	if (ld_lib_setup(ofl) == S_ERROR)
208 		return (ld_exit(ofl));
209 
210 	/*
211 	 * Call ld_start() with the etype of our output file and the
212 	 * output file name.
213 	 */
214 	if (ofl->ofl_flags & FLG_OF_SHAROBJ)
215 		etype = ET_DYN;
216 	else if (ofl->ofl_flags & FLG_OF_RELOBJ)
217 		etype = ET_REL;
218 	else
219 		etype = ET_EXEC;
220 
221 	ld_sup_start(ofl, etype, argv[0]);
222 
223 	/*
224 	 * Process all input files.
225 	 */
226 	if (ld_process_files(ofl, argc, argv) == S_ERROR)
227 		return (ld_exit(ofl));
228 	if (ofl->ofl_flags & FLG_OF_FATAL) {
229 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_FILES),
230 		    ofl->ofl_name);
231 		return (ld_exit(ofl));
232 	}
233 
234 	ld_sup_input_done(ofl);
235 
236 	/*
237 	 * If there were any partially initialized symbol,
238 	 * do preparation works.
239 	 */
240 	if (ofl->ofl_ismove.head != 0) {
241 		if (ld_sunwmove_preprocess(ofl) == S_ERROR)
242 			return (ld_exit(ofl));
243 	}
244 
245 	/*
246 	 * Before validating all symbols count the number of relocation entries.
247 	 * If copy relocations exist, COMMON symbols must be generated which are
248 	 * assigned to the executables .bss.  During sym_validate() the actual
249 	 * size and alignment of the .bss is calculated.  Doing things in this
250 	 * order reduces the number of symbol table traversals required (however
251 	 * it does take a little longer for the user to be told of any undefined
252 	 * symbol errors).
253 	 */
254 	if (ld_reloc_init(ofl) == S_ERROR)
255 		return (ld_exit(ofl));
256 
257 	/*
258 	 * Now that all symbol processing is complete see if any undefined
259 	 * references still remain.  If we observed undefined symbols the
260 	 * FLG_OF_FATAL bit will be set:  If creating a static executable, or a
261 	 * dynamic executable or shared object with the -zdefs flag set, this
262 	 * condition is fatal.  If creating a shared object with the -Bsymbolic
263 	 * flag set, this condition is simply a warning.
264 	 */
265 	if (ld_sym_validate(ofl) == S_ERROR)
266 		return (ld_exit(ofl));
267 
268 	if (ofl->ofl_flags1 & FLG_OF1_OVRFLW) {
269 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_FILES),
270 		    ofl->ofl_name);
271 		return (ld_exit(ofl));
272 	} else if (ofl->ofl_flags & FLG_OF_FATAL) {
273 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_SYM_FATAL),
274 		    ofl->ofl_name);
275 		return (ld_exit(ofl));
276 	} else if (ofl->ofl_flags & FLG_OF_WARN)
277 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_ARG_SYM_WARN));
278 
279 	/*
280 	 * Generate any necessary sections.
281 	 */
282 	if (ld_make_sections(ofl) == S_ERROR)
283 		return (ld_exit(ofl));
284 
285 	/*
286 	 * Now that all sections have been added to the output file, check to
287 	 * see if any section ordering was specified and if so give a warning
288 	 * if any ordering directives were not matched.
289 	 * Also, if SHF_ORDERED sections exist, set up sort key values.
290 	 */
291 	ld_sec_validate(ofl);
292 
293 	/*
294 	 * Having collected all the input data create the initial output file
295 	 * image, assign virtual addresses to the image, and generate a load
296 	 * map if the user requested one.
297 	 */
298 	if (ld_create_outfile(ofl) == S_ERROR)
299 		return (ld_exit(ofl));
300 
301 	if (ld_update_outfile(ofl) == S_ERROR)
302 		return (ld_exit(ofl));
303 	if (ofl->ofl_flags & FLG_OF_GENMAP)
304 		ld_map_out(ofl);
305 
306 	/*
307 	 * Build relocation sections and perform any relocation updates.
308 	 */
309 	if (ld_reloc_process(ofl) == S_ERROR)
310 		return (ld_exit(ofl));
311 
312 #if	defined(__x86) && defined(_ELF64)
313 	/*
314 	 * Fill in contents for Unwind Header
315 	 */
316 	if (populate_amd64_unwindhdr(ofl) == S_ERROR)
317 		return (ld_exit(ofl));
318 #endif
319 	/*
320 	 * Finally create the files elf checksum.
321 	 */
322 	if (ofl->ofl_checksum)
323 		*ofl->ofl_checksum = (Xword)elf_checksum(ofl->ofl_elf);
324 
325 	/*
326 	 * We're done, so make sure the updates are flushed to the output file.
327 	 */
328 	if ((ofl->ofl_size = elf_update(ofl->ofl_welf, ELF_C_WRITE)) == 0) {
329 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_UPDATE),
330 		    ofl->ofl_name);
331 		return (ld_exit(ofl));
332 	}
333 
334 	ld_sup_atexit(ofl, 0);
335 
336 	DBG_CALL(Dbg_statistics_ld(ofl));
337 
338 	/*
339 	 * For performance reasons we don't actually free up the memory we've
340 	 * allocated, it will be freed when we exit.
341 	 *
342 	 * But the below line can be uncommented if/when we want to measure how
343 	 * our memory consumption and freeing are doing.  We should be able to
344 	 * free all the memory that has been allocated as part of the link-edit
345 	 * process.
346 	 *
347 	 * ofl_cleanup(ofl);
348 	 */
349 	return (0);
350 }
351 
352 /*
353  * Cleanup an Ifl_desc.
354  */
355 static void
356 ifl_list_cleanup(List *ifl_list)
357 {
358 	Listnode	*lnp;
359 	Ifl_desc	*ifl;
360 
361 	for (LIST_TRAVERSE(ifl_list, lnp, ifl))
362 		if (ifl->ifl_elf)
363 			(void) elf_end(ifl->ifl_elf);
364 	ifl_list->head = 0;
365 	ifl_list->tail = 0;
366 }
367 
368 /*
369  * Cleanup all memory that has been dynamically allocated during libld
370  * processing and elf_end() all Elf descriptors that are still open.
371  */
372 void
373 ld_ofl_cleanup(Ofl_desc *ofl)
374 {
375 	Ld_heap		*chp, *php;
376 	Ar_desc		*adp;
377 	Listnode	*lnp;
378 
379 	ifl_list_cleanup(&ofl->ofl_objs);
380 	ifl_list_cleanup(&ofl->ofl_sos);
381 
382 	for (LIST_TRAVERSE(&ofl->ofl_ars, lnp, adp)) {
383 		Ar_aux		*aup;
384 		Elf_Arsym	*arsym;
385 
386 		for (arsym = adp->ad_start, aup = adp->ad_aux;
387 		    arsym->as_name; ++arsym, ++aup) {
388 			if ((aup->au_mem) && (aup->au_mem != FLG_ARMEM_PROC)) {
389 				(void) elf_end(aup->au_mem->am_elf);
390 
391 				/*
392 				 * Null out all entries to this member so
393 				 * that we don't attempt to elf_end() it again.
394 				 */
395 				ld_ar_member(adp, arsym, aup, 0);
396 			}
397 		}
398 		(void) elf_end(adp->ad_elf);
399 	}
400 
401 	(void) elf_end(ofl->ofl_elf);
402 	(void) elf_end(ofl->ofl_welf);
403 
404 	for (chp = ld_heap, php = 0; chp; php = chp, chp = chp->lh_next) {
405 		if (php)
406 			(void) munmap((void *)php,
407 			    (size_t)php->lh_end - (size_t)php);
408 	}
409 	if (php)
410 		(void) munmap((void *)php, (size_t)php->lh_end - (size_t)php);
411 
412 	ld_heap = 0;
413 }
414