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