xref: /titanic_41/usr/src/cmd/sgs/libld/common/ldmain.c (revision 29949e866e40b95795203f3ee46f44a197c946e4)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	Copyright (c) 1988 AT&T
24  *	  All Rights Reserved
25  *
26  *
27  *	Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28  *	Use is subject to license terms.
29  */
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * ld -- link/editor main program
34  */
35 #include	<string.h>
36 #include	<stdio.h>
37 #include	<unistd.h>
38 #include	<locale.h>
39 #include	<stdarg.h>
40 #include	"debug.h"
41 #include	"msg.h"
42 #include	"_libld.h"
43 
44 /*
45  * default library search path used if one was not supplied
46  * on the command line.  Note:   These strings can not
47  * use MSG_ORIG() since they are modified as part of the
48  * path processing.
49  */
50 #ifdef _ELF64
51 static char	def_Plibpath[] = "/lib/64:/usr/lib/64";
52 #else
53 static char	def_Plibpath[] = "/usr/ccs/lib:/lib:/usr/lib";
54 #endif
55 
56 /*
57  * The main program
58  */
59 int
60 ld_main(int argc, char ** argv)
61 {
62 	char		*sgs_support;	/* SGS_SUPPORT environment string */
63 	Ofl_desc	*ofl = &Ofl;	/* Output file descriptor */
64 	Half		etype;
65 	uint_t		stflags;
66 	int		suplib = 0;
67 
68 	/*
69 	 * Initialize signal handlers, and output file variables.
70 	 */
71 	init();
72 	ofl->ofl_libver = EV_CURRENT;
73 	ofl->ofl_e_machine = M_MACH;
74 	ofl->ofl_e_flags = 0;
75 
76 	/*
77 	 * Build up linker version string
78 	 */
79 	if ((ofl->ofl_sgsid = (char *)libld_calloc(MSG_SGS_ID_SIZE +
80 	    strlen(link_ver_string) + 1, 1)) == NULL)
81 		return (1);
82 	(void) strcpy(ofl->ofl_sgsid, MSG_ORIG(MSG_SGS_ID));
83 	(void) strcat(ofl->ofl_sgsid, link_ver_string);
84 
85 	/*
86 	 * Argument pass one.  Get all the input flags (skip any files) and
87 	 * check for consistency.  After this point any map file processing
88 	 * would have been completed and the entrance criteria and segment
89 	 * descriptor lists will be complete.
90 	 */
91 	if (process_flags(ofl, argc, argv) == S_ERROR)
92 		return (1);
93 	if (ofl->ofl_flags & FLG_OF_FATAL) {
94 		eprintf(ERR_FATAL, MSG_INTL(MSG_ARG_FLAGS));
95 		return (1);
96 	}
97 
98 	/*
99 	 * At this point a call such as ld -V is considered complete.
100 	 */
101 	if (ofl->ofl_flags1 & FLG_OF1_DONE)
102 		return (0);
103 
104 	/*
105 	 * Initialize string tables, by default we compress the
106 	 * stringtables.
107 	 */
108 	if (ofl->ofl_flags1 & FLG_OF1_NCSTTAB)
109 		stflags = 0;
110 	else
111 		stflags = FLG_STNEW_COMPRESS;
112 
113 	if ((ofl->ofl_shdrsttab = st_new(stflags)) == 0)
114 		return (1);
115 	if ((ofl->ofl_strtab = st_new(stflags)) == 0)
116 		return (1);
117 	if ((ofl->ofl_dynstrtab = st_new(stflags)) == 0)
118 		return (1);
119 
120 	/*
121 	 * Determine whether any support libraries been loaded (either through
122 	 * the SGS_SUPPORT environment variable and/or through the -S option).
123 	 * By default the support library libldstab.so.1 is loaded provided the
124 	 * user hasn't specified their own -S libraries.
125 	 */
126 #if	defined(_LP64)
127 	if ((sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT_64))) == NULL)
128 #else
129 	if ((sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT_32))) == NULL)
130 #endif
131 		sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT));
132 
133 	if (sgs_support && (*sgs_support != '\0')) {
134 		const char	*sep = MSG_ORIG(MSG_STR_COLON);
135 		char		*lib;
136 		char		*lasts;
137 
138 		DBG_CALL(Dbg_support_req(sgs_support, DBG_SUP_ENVIRON));
139 		if ((lib = strtok_r(sgs_support, sep, &lasts)) != NULL) {
140 			do {
141 				if (strcmp(lib,
142 				    MSG_ORIG(MSG_FIL_LIBSTAB)) == 0) {
143 					if (suplib++)
144 						continue;
145 				}
146 				if (ld_support_loadso(lib) == S_ERROR)
147 					return (ldexit());
148 
149 			} while ((lib = strtok_r(NULL, sep, &lasts)) != NULL);
150 		}
151 	}
152 	if (lib_support.head) {
153 		Listnode	*lnp;
154 		char		*lib;
155 
156 		for (LIST_TRAVERSE(&lib_support, lnp, lib)) {
157 			DBG_CALL(Dbg_support_req(lib, DBG_SUP_CMDLINE));
158 			if (ld_support_loadso(lib) == S_ERROR)
159 				return (ldexit());
160 		}
161 	} else {
162 		if (suplib == 0) {
163 			DBG_CALL(Dbg_support_req(MSG_ORIG(MSG_FIL_LIBSTAB),
164 			    DBG_SUP_DEFAULT));
165 			if (ld_support_loadso(MSG_ORIG(MSG_FIL_LIBSTAB)) ==
166 			    S_ERROR)
167 				return (ldexit());
168 		}
169 	}
170 
171 	DBG_CALL(Dbg_ent_print(ofl->ofl_e_machine, &ofl->ofl_ents,
172 		(ofl->ofl_flags & FLG_OF_DYNAMIC)));
173 	DBG_CALL(Dbg_seg_list(ofl->ofl_e_machine, &ofl->ofl_segs));
174 
175 	/*
176 	 * The objscnt and soscnt variables were used to estimate the expected
177 	 * input files, and size the symbol hash buckets accordingly.  Reset
178 	 * these values now, so as to gain an accurate count from pass two, for
179 	 * later statistics diagnostics.
180 	 */
181 	ofl->ofl_objscnt = ofl->ofl_soscnt = 0;
182 
183 	/*
184 	 * Determine whether we can create the file before going any further.
185 	 */
186 	if (open_outfile(ofl) == S_ERROR)
187 		return (ldexit());
188 
189 	/*
190 	 * If the user didn't supply a library path supply a default.  And, if
191 	 * no run-path has been specified (-R), see if the environment variable
192 	 * is in use (historic).  Also assign a default starting address.
193 	 * Don't use MSG_ORIG() for these strings, they're written to later.
194 	 */
195 	if (Plibpath == NULL)
196 		Plibpath = def_Plibpath;
197 
198 	if (ofl->ofl_rpath == NULL) {
199 		char *rpath;
200 		if (((rpath = getenv(MSG_ORIG(MSG_LD_RUN_PATH))) != NULL) &&
201 		    (strcmp((const char *)rpath, MSG_ORIG(MSG_STR_EMPTY))))
202 			ofl->ofl_rpath = rpath;
203 	}
204 	if (ofl->ofl_flags & FLG_OF_EXEC)
205 		ofl->ofl_segorigin = M_SEGM_ORIGIN;
206 
207 	/*
208 	 * Argument pass two.  Input all libraries and objects.
209 	 */
210 	if (lib_setup(ofl) == S_ERROR)
211 		return (ldexit());
212 
213 	/*
214 	 * Call ld_start() with the etype of our output file and the
215 	 * output file name.
216 	 */
217 	if (ofl->ofl_flags & FLG_OF_SHAROBJ)
218 		etype = ET_DYN;
219 	else if (ofl->ofl_flags & FLG_OF_RELOBJ)
220 		etype = ET_REL;
221 	else
222 		etype = ET_EXEC;
223 
224 	lds_start(ofl->ofl_name, etype, argv[0]);
225 
226 	/*
227 	 * Process all input files.
228 	 */
229 	if (process_files(ofl, argc, argv) == S_ERROR)
230 		return (ldexit());
231 	if (ofl->ofl_flags & FLG_OF_FATAL) {
232 		eprintf(ERR_FATAL, MSG_INTL(MSG_ARG_FILES), ofl->ofl_name);
233 		return (ldexit());
234 	}
235 
236 	lds_input_done();
237 
238 	/*
239 	 * If there were any partially initialized symbol,
240 	 * do preparation works.
241 	 */
242 	if (ofl->ofl_ismove.head != 0) {
243 		if (sunwmove_preprocess(ofl) == S_ERROR)
244 			return (ldexit());
245 	}
246 
247 	/*
248 	 * Before validating all symbols count the number of relocation entries.
249 	 * If copy relocations exist, COMMON symbols must be generated which are
250 	 * assigned to the executables .bss.  During sym_validate() the actual
251 	 * size and alignment of the .bss is calculated.  Doing things in this
252 	 * order reduces the number of symbol table traversals required (however
253 	 * it does take a little longer for the user to be told of any undefined
254 	 * symbol errors).
255 	 */
256 	if (reloc_init(ofl) == S_ERROR)
257 		return (ldexit());
258 
259 	/*
260 	 * Now that all symbol processing is complete see if any undefined
261 	 * references still remain.  If we observed undefined symbols the
262 	 * FLG_OF_FATAL bit will be set:  If creating a static executable, or a
263 	 * dynamic executable or shared object with the -zdefs flag set, this
264 	 * condition is fatal.  If creating a shared object with the -Bsymbolic
265 	 * flag set, this condition is simply a warning.
266 	 */
267 	if (sym_validate(ofl) == S_ERROR)
268 		return (ldexit());
269 
270 	if (ofl->ofl_flags1 & FLG_OF1_OVRFLW) {
271 		eprintf(ERR_FATAL, MSG_INTL(MSG_ARG_FILES), ofl->ofl_name);
272 		return (ldexit());
273 	} else if (ofl->ofl_flags & FLG_OF_FATAL) {
274 		eprintf(ERR_FATAL, MSG_INTL(MSG_ARG_SYM_FATAL), ofl->ofl_name);
275 		return (ldexit());
276 	} else if (ofl->ofl_flags & FLG_OF_WARN)
277 		eprintf(ERR_WARNING, MSG_INTL(MSG_ARG_SYM_WARN));
278 
279 	/*
280 	 * Generate any necessary sections.
281 	 */
282 	if (make_sections(ofl) == S_ERROR)
283 		return (ldexit());
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 	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 (create_outfile(ofl) == S_ERROR)
299 		return (ldexit());
300 
301 	if (update_outfile(ofl) == S_ERROR)
302 		return (ldexit());
303 	if (ofl->ofl_flags & FLG_OF_GENMAP)
304 		ldmap_out(ofl);
305 
306 	/*
307 	 * Build relocation sections and perform any relocation updates.
308 	 */
309 	if (reloc_process(ofl) == S_ERROR)
310 		return (ldexit());
311 
312 
313 #if defined(__x86) && defined(_ELF64)
314 	/*
315 	 * Fill in contents for Unwind Header
316 	 */
317 	if (populate_amd64_unwindhdr(ofl) == S_ERROR)
318 		return (ldexit());
319 #endif
320 	/*
321 	 * Finally create the files elf checksum.
322 	 */
323 	if (ofl->ofl_checksum)
324 		*ofl->ofl_checksum = (Xword)elf_checksum(ofl->ofl_elf);
325 
326 	/*
327 	 * We're done, so make sure the updates are flushed to the output file.
328 	 */
329 	if ((ofl->ofl_size = elf_update(ofl->ofl_welf, ELF_C_WRITE)) == 0) {
330 		eprintf(ERR_ELF, MSG_INTL(MSG_ELF_UPDATE), ofl->ofl_name);
331 		return (ldexit());
332 	}
333 
334 	lds_atexit(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 /* VARARGS1 */
353 void
354 dbg_print(const char *format, ...)
355 {
356 	static char	*prestr = 0;
357 	va_list		args;
358 
359 	if (dbg_mask & DBG_G_SNAME) {
360 		/*
361 		 * If the debugging options have requested each diagnostic line
362 		 * be prepended by a name create a prefix string.
363 		 */
364 		if ((prestr == 0) && Ofl.ofl_name) {
365 			const char	*name, *cls;
366 			size_t		len;
367 
368 			/*
369 			 * Select the fullname or basename of the output file
370 			 * being created.
371 			 */
372 			if (dbg_mask & DBG_G_FNAME)
373 				name = Ofl.ofl_name;
374 			else {
375 				if ((name = strrchr(Ofl.ofl_name, '/')) == 0)
376 					name = Ofl.ofl_name;
377 				else
378 					name++;
379 			}
380 			len = strlen(name) +
381 			    strlen(MSG_INTL(MSG_DBG_NAME_FMT)) + 1;
382 
383 			/*
384 			 * Add the output file class if required.
385 			 */
386 			if (dbg_mask & DBG_G_CLASS) {
387 #if	defined(_ELF64)
388 				len += MSG_DBG_CLS64_FMT_SIZE;
389 				cls = MSG_ORIG(MSG_DBG_CLS64_FMT);
390 #else
391 				len += MSG_DBG_CLS32_FMT_SIZE;
392 				cls = MSG_ORIG(MSG_DBG_CLS32_FMT);
393 #endif
394 			}
395 
396 			/*
397 			 * Allocate a string to build the prefix.
398 			 */
399 			if ((prestr = libld_malloc(len)) == 0)
400 				prestr = (char *)MSG_INTL(MSG_DBG_DFLT_FMT);
401 			else {
402 				(void) snprintf(prestr, len,
403 				    MSG_INTL(MSG_DBG_NAME_FMT), name);
404 				if (dbg_mask & DBG_G_CLASS)
405 					(void) strcat(prestr, cls);
406 			}
407 		}
408 		if (prestr)
409 			(void) fputs(prestr, stderr);
410 		else
411 			(void) fputs(MSG_INTL(MSG_DBG_AOUT_FMT), stderr);
412 	} else
413 		(void) fputs(MSG_INTL(MSG_DBG_DFLT_FMT), stderr);
414 
415 	va_start(args, format);
416 	(void) vfprintf(stderr, format, args);
417 	(void) fprintf(stderr, MSG_ORIG(MSG_STR_NL));
418 	va_end(args);
419 }
420