xref: /titanic_50/usr/src/cmd/sgs/libld/common/ldmain.c (revision 69bb4bb45c98da60d21839c4dc3c01ea1be60585)
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 	}
144 	if (lib_support.head) {
145 		Listnode	*lnp;
146 		char		*lib;
147 
148 		for (LIST_TRAVERSE(&lib_support, lnp, lib)) {
149 			DBG_CALL(Dbg_support_req(ofl->ofl_lml, lib,
150 			    DBG_SUP_CMDLINE));
151 			if (ld_sup_loadso(ofl, lib) == S_ERROR)
152 				return (ld_exit(ofl));
153 		}
154 	} else {
155 		if (suplib == 0) {
156 			DBG_CALL(Dbg_support_req(ofl->ofl_lml,
157 			    MSG_ORIG(MSG_FIL_LIBSTAB), DBG_SUP_DEFAULT));
158 			if (ld_sup_loadso(ofl, MSG_ORIG(MSG_FIL_LIBSTAB)) ==
159 			    S_ERROR)
160 				return (ld_exit(ofl));
161 		}
162 	}
163 
164 	DBG_CALL(Dbg_ent_print(ofl->ofl_lml, ofl->ofl_dehdr->e_machine,
165 	    &ofl->ofl_ents, (ofl->ofl_flags & FLG_OF_DYNAMIC)));
166 	DBG_CALL(Dbg_seg_list(ofl->ofl_lml, ofl->ofl_dehdr->e_machine,
167 	    &ofl->ofl_segs));
168 
169 	/*
170 	 * The objscnt and soscnt variables were used to estimate the expected
171 	 * input files, and size the symbol hash buckets accordingly.  Reset
172 	 * these values now, so as to gain an accurate count from pass two, for
173 	 * later statistics diagnostics.
174 	 */
175 	ofl->ofl_objscnt = ofl->ofl_soscnt = 0;
176 
177 	/*
178 	 * Determine whether we can create the file before going any further.
179 	 */
180 	if (ld_open_outfile(ofl) == S_ERROR)
181 		return (ld_exit(ofl));
182 
183 	/*
184 	 * If the user didn't supply a library path supply a default.  And, if
185 	 * no run-path has been specified (-R), see if the environment variable
186 	 * is in use (historic).  Also assign a default starting address.
187 	 * Don't use MSG_ORIG() for these strings, they're written to later.
188 	 */
189 	if (Plibpath == NULL)
190 		Plibpath = def_Plibpath;
191 
192 	if (ofl->ofl_rpath == NULL) {
193 		char *rpath;
194 		if (((rpath = getenv(MSG_ORIG(MSG_LD_RUN_PATH))) != NULL) &&
195 		    (strcmp((const char *)rpath, MSG_ORIG(MSG_STR_EMPTY))))
196 			ofl->ofl_rpath = rpath;
197 	}
198 	if (ofl->ofl_flags & FLG_OF_EXEC)
199 		ofl->ofl_segorigin = M_SEGM_ORIGIN;
200 
201 	/*
202 	 * Argument pass two.  Input all libraries and objects.
203 	 */
204 	if (ld_lib_setup(ofl) == S_ERROR)
205 		return (ld_exit(ofl));
206 
207 	/*
208 	 * Call ld_start() with the etype of our output file and the
209 	 * output file name.
210 	 */
211 	if (ofl->ofl_flags & FLG_OF_SHAROBJ)
212 		etype = ET_DYN;
213 	else if (ofl->ofl_flags & FLG_OF_RELOBJ)
214 		etype = ET_REL;
215 	else
216 		etype = ET_EXEC;
217 
218 	ld_sup_start(ofl, etype, argv[0]);
219 
220 	/*
221 	 * Process all input files.
222 	 */
223 	if (ld_process_files(ofl, argc, argv) == S_ERROR)
224 		return (ld_exit(ofl));
225 	if (ofl->ofl_flags & FLG_OF_FATAL) {
226 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_FILES),
227 		    ofl->ofl_name);
228 		return (ld_exit(ofl));
229 	}
230 
231 	ld_sup_input_done(ofl);
232 
233 	/*
234 	 * If there were any partially initialized symbol,
235 	 * do preparation works.
236 	 */
237 	if (ofl->ofl_ismove.head != 0) {
238 		if (ld_sunwmove_preprocess(ofl) == S_ERROR)
239 			return (ld_exit(ofl));
240 	}
241 
242 	/*
243 	 * Before validating all symbols count the number of relocation entries.
244 	 * If copy relocations exist, COMMON symbols must be generated which are
245 	 * assigned to the executables .bss.  During sym_validate() the actual
246 	 * size and alignment of the .bss is calculated.  Doing things in this
247 	 * order reduces the number of symbol table traversals required (however
248 	 * it does take a little longer for the user to be told of any undefined
249 	 * symbol errors).
250 	 */
251 	if (ld_reloc_init(ofl) == S_ERROR)
252 		return (ld_exit(ofl));
253 
254 	/*
255 	 * Now that all symbol processing is complete see if any undefined
256 	 * references still remain.  If we observed undefined symbols the
257 	 * FLG_OF_FATAL bit will be set:  If creating a static executable, or a
258 	 * dynamic executable or shared object with the -zdefs flag set, this
259 	 * condition is fatal.  If creating a shared object with the -Bsymbolic
260 	 * flag set, this condition is simply a warning.
261 	 */
262 	if (ld_sym_validate(ofl) == S_ERROR)
263 		return (ld_exit(ofl));
264 
265 	if (ofl->ofl_flags1 & FLG_OF1_OVRFLW) {
266 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_FILES),
267 		    ofl->ofl_name);
268 		return (ld_exit(ofl));
269 	} else if (ofl->ofl_flags & FLG_OF_FATAL) {
270 		eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_ARG_SYM_FATAL),
271 		    ofl->ofl_name);
272 		return (ld_exit(ofl));
273 	} else if (ofl->ofl_flags & FLG_OF_WARN)
274 		eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_ARG_SYM_WARN));
275 
276 	/*
277 	 * Generate any necessary sections.
278 	 */
279 	if (ld_make_sections(ofl) == S_ERROR)
280 		return (ld_exit(ofl));
281 
282 	/*
283 	 * Now that all sections have been added to the output file, check to
284 	 * see if any section ordering was specified and if so give a warning
285 	 * if any ordering directives were not matched.
286 	 * Also, if SHF_ORDERED sections exist, set up sort key values.
287 	 */
288 	ld_sec_validate(ofl);
289 
290 	/*
291 	 * Having collected all the input data create the initial output file
292 	 * image, assign virtual addresses to the image, and generate a load
293 	 * map if the user requested one.
294 	 */
295 	if (ld_create_outfile(ofl) == S_ERROR)
296 		return (ld_exit(ofl));
297 
298 	if (ld_update_outfile(ofl) == S_ERROR)
299 		return (ld_exit(ofl));
300 	if (ofl->ofl_flags & FLG_OF_GENMAP)
301 		ld_map_out(ofl);
302 
303 	/*
304 	 * Build relocation sections and perform any relocation updates.
305 	 */
306 	if (ld_reloc_process(ofl) == S_ERROR)
307 		return (ld_exit(ofl));
308 
309 #if	defined(__x86) && defined(_ELF64)
310 	/*
311 	 * Fill in contents for Unwind Header
312 	 */
313 	if (populate_amd64_unwindhdr(ofl) == S_ERROR)
314 		return (ld_exit(ofl));
315 #endif
316 	/*
317 	 * Finally create the files elf checksum.
318 	 */
319 	if (ofl->ofl_checksum)
320 		*ofl->ofl_checksum = (Xword)elf_checksum(ofl->ofl_elf);
321 
322 	/*
323 	 * We're done, so make sure the updates are flushed to the output file.
324 	 */
325 	if ((ofl->ofl_size = elf_update(ofl->ofl_welf, ELF_C_WRITE)) == 0) {
326 		eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_UPDATE),
327 		    ofl->ofl_name);
328 		return (ld_exit(ofl));
329 	}
330 
331 	ld_sup_atexit(ofl, 0);
332 
333 	DBG_CALL(Dbg_statistics_ld(ofl));
334 
335 	/*
336 	 * For performance reasons we don't actually free up the memory we've
337 	 * allocated, it will be freed when we exit.
338 	 *
339 	 * But the below line can be uncommented if/when we want to measure how
340 	 * our memory consumption and freeing are doing.  We should be able to
341 	 * free all the memory that has been allocated as part of the link-edit
342 	 * process.
343 	 *
344 	 * ofl_cleanup(ofl);
345 	 */
346 	return (0);
347 }
348 
349 /*
350  * Cleanup an Ifl_desc.
351  */
352 static void
353 ifl_list_cleanup(List *ifl_list)
354 {
355 	Listnode	*lnp;
356 	Ifl_desc	*ifl;
357 
358 	for (LIST_TRAVERSE(ifl_list, lnp, ifl))
359 		if (ifl->ifl_elf)
360 			(void) elf_end(ifl->ifl_elf);
361 	ifl_list->head = 0;
362 	ifl_list->tail = 0;
363 }
364 
365 /*
366  * Cleanup all memory that has been dynamically allocated during libld
367  * processing and elf_end() all Elf descriptors that are still open.
368  */
369 void
370 ld_ofl_cleanup(Ofl_desc *ofl)
371 {
372 	Ld_heap		*chp, *php;
373 	Ar_desc		*adp;
374 	Listnode	*lnp;
375 
376 	ifl_list_cleanup(&ofl->ofl_objs);
377 	ifl_list_cleanup(&ofl->ofl_sos);
378 
379 	for (LIST_TRAVERSE(&ofl->ofl_ars, lnp, adp)) {
380 		Ar_aux		*aup;
381 		Elf_Arsym	*arsym;
382 
383 		for (arsym = adp->ad_start, aup = adp->ad_aux;
384 		    arsym->as_name; ++arsym, ++aup) {
385 			if ((aup->au_mem) && (aup->au_mem != FLG_ARMEM_PROC)) {
386 				(void) elf_end(aup->au_mem->am_elf);
387 
388 				/*
389 				 * Null out all entries to this member so
390 				 * that we don't attempt to elf_end() it again.
391 				 */
392 				ld_ar_member(adp, arsym, aup, 0);
393 			}
394 		}
395 		(void) elf_end(adp->ad_elf);
396 	}
397 
398 	(void) elf_end(ofl->ofl_elf);
399 	(void) elf_end(ofl->ofl_welf);
400 
401 	for (chp = ld_heap, php = 0; chp; php = chp, chp = chp->lh_next) {
402 		if (php)
403 			(void) munmap((void *)php,
404 			    (size_t)php->lh_end - (size_t)php);
405 	}
406 	if (php)
407 		(void) munmap((void *)php, (size_t)php->lh_end - (size_t)php);
408 
409 	ld_heap = 0;
410 }
411