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.
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.
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]
23 * Copyright (c) 1988 AT&T
26 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Processing of relocatable objects and shared objects.
34 * ld -- link/editor main program
36 #include <sys/types.h>
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.
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.
61 static char def_Plibpath
[] = "/lib/64:/usr/lib/64";
63 static char def_Plibpath
[] = "/usr/ccs/lib:/lib:/usr/lib";
67 * A default elf header provides for simplifying diagnostic processing.
69 static Ehdr def_ehdr
= { { ELFMAG0
, ELFMAG1
, ELFMAG2
, ELFMAG3
,
70 ELFCLASSNONE
, ELFDATANONE
}, 0, EM_NONE
,
74 * ld-centric wrapper on top of veprintf():
75 * - Accepts output descriptor rather than linkmap list
76 * - Sets the FLG_OF_FATAL/FLG_OF_WARN flags as necessary
79 ld_eprintf(Ofl_desc
*ofl
, Error error
, const char *format
, ...)
83 /* Set flag indicating type of error being issued */
89 ofl
->ofl_flags
|= FLG_OF_WARN
;
92 if ((ofl
->ofl_guideflags
& FLG_OFG_ENABLE
) == 0)
94 ofl
->ofl_guideflags
|= FLG_OFG_ISSUED
;
95 ofl
->ofl_flags
|= FLG_OF_WARN
;
98 ofl
->ofl_flags
|= FLG_OF_FATAL
;
101 /* Issue the error */
102 va_start(args
, format
);
103 veprintf(ofl
->ofl_lml
, error
, format
, args
);
108 * Establish the global state necessary to link the desired machine
109 * target, as reflected by the ld_targ global variable.
112 ld_init_target(Lm_list
*lml
, Half mach
)
117 ld_targ
= *ld_targ_init_x86();
123 ld_targ
= *ld_targ_init_sparc();
128 Conv_inv_buf_t inv_buf
;
130 eprintf(lml
, ERR_FATAL
, MSG_INTL(MSG_TARG_UNSUPPORTED
),
131 conv_ehdr_mach(mach
, 0, &inv_buf
));
144 ld_main(int argc
, char **argv
, Half mach
)
146 char *sgs_support
; /* SGS_SUPPORT environment string */
149 ofl_flag_t save_flg_of_warn
;
152 * Establish a base time. Total time diagnostics are relative to
153 * entering the link-editor here.
155 (void) gettimeofday(&DBG_TOTALTIME
, NULL
);
156 DBG_DELTATIME
= DBG_TOTALTIME
;
158 /* Output file descriptor */
159 if ((ofl
= libld_calloc(1, sizeof (Ofl_desc
))) == 0)
162 /* Initialize target state */
163 if (ld_init_target(NULL
, mach
) != 0)
167 * Set up the default output ELF header to satisfy diagnostic
168 * requirements, and initialize the machine and class details.
170 ofl
->ofl_dehdr
= &def_ehdr
;
171 def_ehdr
.e_ident
[EI_CLASS
] = ld_targ
.t_m
.m_class
;
172 def_ehdr
.e_ident
[EI_DATA
] = ld_targ
.t_m
.m_data
;
173 def_ehdr
.e_machine
= ld_targ
.t_m
.m_mach
;
176 * Build up linker version string
178 if ((ofl
->ofl_sgsid
= (char *)libld_calloc(MSG_SGS_ID_SIZE
+
179 strlen(link_ver_string
) + 1, 1)) == NULL
)
181 (void) strcpy(ofl
->ofl_sgsid
, MSG_ORIG(MSG_SGS_ID
));
182 (void) strcat(ofl
->ofl_sgsid
, link_ver_string
);
185 * Argument pass one. Get all the input flags (skip any files) and
186 * check for consistency. Return from ld_process_flags() marks the
187 * end of mapfile processing. The entrance criteria and segment
188 * descriptors are complete and in their final form.
190 if (ld_process_flags(ofl
, argc
, argv
) == S_ERROR
) {
191 /* If any ERR_GUIDANCE messages were issued, add a summary */
192 if (ofl
->ofl_guideflags
& FLG_OFG_ISSUED
)
193 ld_eprintf(ofl
, ERR_GUIDANCE
,
194 MSG_INTL(MSG_GUIDE_SUMMARY
));
197 if (ofl
->ofl_flags
& FLG_OF_FATAL
) {
198 ld_eprintf(ofl
, ERR_FATAL
, MSG_INTL(MSG_ARG_FLAGS
));
199 /* If any ERR_GUIDANCE messages were issued, add a summary */
200 if (ofl
->ofl_guideflags
& FLG_OFG_ISSUED
)
201 ld_eprintf(ofl
, ERR_GUIDANCE
,
202 MSG_INTL(MSG_GUIDE_SUMMARY
));
207 * At this point a call such as ld -V is considered complete.
209 if (ofl
->ofl_flags1
& FLG_OF1_DONE
)
212 /* Initialize signal handler */
213 ld_init_sighandler(ofl
);
216 * Determine whether any support libraries should be loaded,
217 * (either through the SGS_SUPPORT environment variable and/or
218 * through the -S option).
221 if ((sgs_support
= getenv(MSG_ORIG(MSG_SGS_SUPPORT_64
))) == NULL
)
223 if ((sgs_support
= getenv(MSG_ORIG(MSG_SGS_SUPPORT_32
))) == NULL
)
225 sgs_support
= getenv(MSG_ORIG(MSG_SGS_SUPPORT
));
227 if (sgs_support
&& sgs_support
[0]) {
228 const char *sep
= MSG_ORIG(MSG_STR_COLON
);
232 DBG_CALL(Dbg_support_req(ofl
->ofl_lml
, sgs_support
,
234 if ((lib
= strtok_r(sgs_support
, sep
, &lasts
)) != NULL
) {
236 if (ld_sup_loadso(ofl
, lib
) == S_ERROR
)
237 return (ld_exit(ofl
));
238 DBG_CALL(Dbg_util_nl(ofl
->ofl_lml
, DBG_NL_STD
));
240 } while ((lib
= strtok_r(NULL
, sep
, &lasts
)) != NULL
);
247 for (APLIST_TRAVERSE(lib_support
, idx
, lib
)) {
248 DBG_CALL(Dbg_support_req(ofl
->ofl_lml
, lib
,
250 if (ld_sup_loadso(ofl
, lib
) == S_ERROR
)
251 return (ld_exit(ofl
));
252 DBG_CALL(Dbg_util_nl(ofl
->ofl_lml
, DBG_NL_STD
));
256 DBG_CALL(Dbg_ent_print(ofl
->ofl_lml
,
257 ofl
->ofl_dehdr
->e_ident
[EI_OSABI
], ofl
->ofl_dehdr
->e_machine
,
259 DBG_CALL(Dbg_seg_list(ofl
->ofl_lml
,
260 ofl
->ofl_dehdr
->e_ident
[EI_OSABI
], ofl
->ofl_dehdr
->e_machine
,
264 * The objscnt and soscnt variables were used to estimate the expected
265 * input files, and size the symbol hash buckets accordingly. Reset
266 * these values now, so as to gain an accurate count from pass two, for
267 * later statistics diagnostics.
269 ofl
->ofl_objscnt
= ofl
->ofl_soscnt
= 0;
272 * Determine whether we can create the file before going any further.
274 if (ld_open_outfile(ofl
) == S_ERROR
)
275 return (ld_exit(ofl
));
278 * If the user didn't supply a library path supply a default. And, if
279 * no run-path has been specified (-R), see if the environment variable
280 * is in use (historic).
282 if (Plibpath
== NULL
)
283 Plibpath
= def_Plibpath
;
285 if (ofl
->ofl_rpath
== NULL
) {
288 if (((rpath
= getenv(MSG_ORIG(MSG_LD_RUN_PATH
))) != NULL
) &&
290 ofl
->ofl_rpath
= rpath
;
294 * Argument pass two. Input all libraries and objects.
296 if (ld_lib_setup(ofl
) == S_ERROR
)
297 return (ld_exit(ofl
));
300 * Call ld_start() with the etype of our output file and the
303 if (ofl
->ofl_flags
& FLG_OF_SHAROBJ
)
305 else if (ofl
->ofl_flags
& FLG_OF_RELOBJ
)
310 ld_sup_start(ofl
, etype
, argv
[0]);
313 * Process all input files.
315 if (ld_process_files(ofl
, argc
, argv
) == S_ERROR
)
316 return (ld_exit(ofl
));
317 if (ofl
->ofl_flags
& FLG_OF_FATAL
) {
318 ld_eprintf(ofl
, ERR_FATAL
, MSG_INTL(MSG_ARG_FILES
),
320 return (ld_exit(ofl
));
323 ld_sup_input_done(ofl
);
326 * Now that all input section processing is complete, validate and
327 * process any SHT_SUNW_move sections.
329 if (ofl
->ofl_ismove
&& (ld_process_move(ofl
) == S_ERROR
))
330 return (ld_exit(ofl
));
333 * Before validating all symbols count the number of relocation entries.
334 * If copy relocations exist, COMMON symbols must be generated which are
335 * assigned to the executables .bss. During sym_validate() the actual
336 * size and alignment of the .bss is calculated. Doing things in this
337 * order reduces the number of symbol table traversals required (however
338 * it does take a little longer for the user to be told of any undefined
341 if (ld_reloc_init(ofl
) == S_ERROR
)
342 return (ld_exit(ofl
));
345 * We need to know if FLG_OF_WARN is currently set, in case
346 * we need to honor a -z fatal-warnings request. However, we also
347 * need to know if a warning due to symbol validation results from
348 * the upcoming call to ld_sym_validate() in order to issue the
349 * appropriate message for it. So we save the current value,
350 * and clear the main flag.
352 save_flg_of_warn
= ofl
->ofl_flags
& FLG_OF_WARN
;
353 ofl
->ofl_flags
&= ~FLG_OF_WARN
;
355 if (ld_sym_validate(ofl
) == S_ERROR
)
356 return (ld_exit(ofl
));
359 * Now that all symbol processing is complete see if any undefined
360 * references still remain. If we observed undefined symbols the
361 * FLG_OF_FATAL bit will be set: If creating a static executable, or a
362 * dynamic executable or shared object with the -zdefs flag set, this
363 * condition is fatal. If creating a shared object with the -Bsymbolic
364 * flag set, this condition is simply a warning.
366 if (ofl
->ofl_flags
& FLG_OF_FATAL
)
367 ld_eprintf(ofl
, ERR_FATAL
, MSG_INTL(MSG_ARG_SYM_FATAL
),
369 else if (ofl
->ofl_flags
& FLG_OF_WARN
)
370 ld_eprintf(ofl
, ERR_WARNING
, MSG_INTL(MSG_ARG_SYM_WARN
));
373 * Guidance: Use -z defs|nodefs when building shared objects.
375 * ld_sym_validate() will mask this guidance message out unless we are
376 * intended to send it here, so all we need to do is use OFL_GUIDANCE()
377 * to decide whether to issue it or not.
379 if (OFL_GUIDANCE(ofl
, FLG_OFG_NO_DEFS
))
380 ld_eprintf(ofl
, ERR_GUIDANCE
, MSG_INTL(MSG_GUIDE_DEFS
));
383 * Symbol processing was the final step before we start producing the
384 * output object. At this time, if we've seen warnings and the
385 * -z fatal-warnings option is specified, promote them to fatal, which
386 * will cause us to exit without creating an object.
388 * We didn't do this as the warnings were reported in order to
389 * maximize the number of problems a given link-editor invocation
390 * can diagnose. This is safe, since warnings are by definition events
391 * one can choose to ignore.
393 if (((ofl
->ofl_flags
| save_flg_of_warn
) &
394 (FLG_OF_WARN
| FLG_OF_FATWARN
)) ==
395 (FLG_OF_WARN
| FLG_OF_FATWARN
))
396 ofl
->ofl_flags
|= FLG_OF_FATAL
;
399 * If fatal errors occurred in symbol processing, or due to warnings
400 * promoted by -z fatal-warnings, this is the end of the line.
402 if (ofl
->ofl_flags
& FLG_OF_FATAL
)
403 return (ld_exit(ofl
));
406 * Generate any necessary sections.
408 if (ld_make_sections(ofl
) == S_ERROR
)
409 return (ld_exit(ofl
));
412 * Now that all sections have been added to the output file, determine
413 * whether any mapfile section ordering was specified, and verify that
414 * all mapfile ordering directives have been matched. Issue a warning
415 * for any directives that have not been matched.
416 * Also, if SHF_ORDERED sections exist, set up sort key values.
418 if (ofl
->ofl_flags
& (FLG_OF_OS_ORDER
| FLG_OF_KEY
))
419 ld_sec_validate(ofl
);
422 * Having collected all the input data create the initial output file
423 * image, assign virtual addresses to the image, and generate a load
424 * map if the user requested one.
426 if (ld_create_outfile(ofl
) == S_ERROR
)
427 return (ld_exit(ofl
));
429 if (ld_update_outfile(ofl
) == S_ERROR
)
430 return (ld_exit(ofl
));
431 if (ofl
->ofl_flags
& FLG_OF_GENMAP
)
435 * Build relocation sections and perform any relocation updates.
437 if (ld_reloc_process(ofl
) == S_ERROR
)
438 return (ld_exit(ofl
));
441 * Fill in contents for unwind header (.eh_frame_hdr)
443 if (ld_unwind_populate_hdr(ofl
) == S_ERROR
)
444 return (ld_exit(ofl
));
447 * Finally create the files elf checksum.
449 if (ofl
->ofl_checksum
)
450 *ofl
->ofl_checksum
= (Xword
)elf_checksum(ofl
->ofl_elf
);
453 * If this is a cross link to a target with a different byte
454 * order than the linker, swap the data to the target byte order.
456 if (((ofl
->ofl_flags1
& FLG_OF1_ENCDIFF
) != 0) &&
457 (_elf_swap_wrimage(ofl
->ofl_elf
) != 0)) {
458 ld_eprintf(ofl
, ERR_ELF
, MSG_INTL(MSG_ELF_SWAP_WRIMAGE
),
460 return (ld_exit(ofl
));
464 * We're done, so make sure the updates are flushed to the output file.
466 if ((ofl
->ofl_size
= elf_update(ofl
->ofl_welf
, ELF_C_WRITE
)) == 0) {
467 ld_eprintf(ofl
, ERR_ELF
, MSG_INTL(MSG_ELF_UPDATE
),
469 return (ld_exit(ofl
));
472 ld_sup_atexit(ofl
, 0);
474 DBG_CALL(Dbg_statistics_ld(ofl
));
475 DBG_CALL(Dbg_basic_finish(ofl
->ofl_lml
));
478 * Wrap up debug output file if one is open
482 /* If any ERR_GUIDANCE messages were issued, add a summary */
483 if (ofl
->ofl_guideflags
& FLG_OFG_ISSUED
)
484 ld_eprintf(ofl
, ERR_GUIDANCE
, MSG_INTL(MSG_GUIDE_SUMMARY
));
487 * For performance reasons we don't actually free up the memory we've
488 * allocated, it will be freed when we exit.
490 * But the below line can be uncommented if/when we want to measure how
491 * our memory consumption and freeing are doing. We should be able to
492 * free all the memory that has been allocated as part of the link-edit
495 /* ld_ofl_cleanup(ofl); */
500 * Cleanup an Ifl_desc.
503 ifl_list_cleanup(APlist
*apl
)
508 for (APLIST_TRAVERSE(apl
, idx
, ifl
)) {
510 (void) elf_end(ifl
->ifl_elf
);
515 * Cleanup all memory that has been dynamically allocated during libld
516 * processing and elf_end() all Elf descriptors that are still open.
519 ld_ofl_cleanup(Ofl_desc
*ofl
)
525 ifl_list_cleanup(ofl
->ofl_objs
);
526 ofl
->ofl_objs
= NULL
;
527 ifl_list_cleanup(ofl
->ofl_sos
);
530 for (APLIST_TRAVERSE(ofl
->ofl_ars
, idx
, adp
)) {
534 for (arsym
= adp
->ad_start
, aup
= adp
->ad_aux
;
535 arsym
->as_name
; ++arsym
, ++aup
) {
536 if ((aup
->au_mem
) && (aup
->au_mem
!= FLG_ARMEM_PROC
)) {
537 (void) elf_end(aup
->au_mem
->am_elf
);
540 * Null out all entries to this member so
541 * that we don't attempt to elf_end() it again.
543 ld_ar_member(adp
, arsym
, aup
, 0);
546 (void) elf_end(adp
->ad_elf
);
550 (void) elf_end(ofl
->ofl_elf
);
551 (void) elf_end(ofl
->ofl_welf
);
553 for (chp
= ld_heap
, php
= NULL
; chp
; php
= chp
, chp
= chp
->lh_next
) {
555 (void) munmap((void *)php
,
556 (size_t)php
->lh_end
- (size_t)php
);
559 (void) munmap((void *)php
, (size_t)php
->lh_end
- (size_t)php
);