From ffbc01ccf0e0c923db2ffa3a6b3ca6b4f6d08ac5 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Wed, 6 Jun 2012 10:50:18 +0000 Subject: bfd/ * elflink.c (elf_link_input_bfd): Provide a file symbol for each input file with local syms, if the input lacks such. (bfd_elf_final_link): Add a file symbol to mark end of locals for which we can associate with input files. (struct elf_final_link_info): Add filesym_count field. (struct elf_outext_info): Add need_second_pass and second_pass. (elf_link_output_extsym): Detect symbols defined in the output file, emit them on second pass over locals. ld/testsuite/ Update to suit added STT_FILE symbols. --- bfd/ChangeLog | 11 +++++++++ bfd/elflink.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) (limited to 'bfd') diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 9874ff1..9744a8b 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,14 @@ +2012-06-06 Alan Modra + + * elflink.c (elf_link_input_bfd): Provide a file symbol for + each input file with local syms, if the input lacks such. + (bfd_elf_final_link): Add a file symbol to mark end of locals + for which we can associate with input files. + (struct elf_final_link_info): Add filesym_count field. + (struct elf_outext_info): Add need_second_pass and second_pass. + (elf_link_output_extsym): Detect symbols defined in the output + file, emit them on second pass over locals. + 2012-06-04 Jan Kratochvil * bfd-in.h (bfd_elf_bfd_from_remote_memory): Make LEN argument diff --git a/bfd/elflink.c b/bfd/elflink.c index e715942..a9d95bd 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -7403,6 +7403,8 @@ struct elf_final_link_info size_t symbuf_size; /* And same for symshndxbuf. */ size_t shndxbuf_size; + /* Number of STT_FILE syms seen. */ + size_t filesym_count; }; /* This struct is used to pass information to elf_link_output_extsym. */ @@ -7411,6 +7413,8 @@ struct elf_outext_info { bfd_boolean failed; bfd_boolean localsyms; + bfd_boolean need_second_pass; + bfd_boolean second_pass; struct elf_final_link_info *flinfo; }; @@ -8605,6 +8609,11 @@ elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) { if (!h->forced_local) return TRUE; + if (eoinfo->second_pass + && !((h->root.type == bfd_link_hash_defined + || h->root.type == bfd_link_hash_defweak) + && h->root.u.def.section->output_section != NULL)) + return TRUE; } else { @@ -8759,6 +8768,19 @@ elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) input_sec = h->root.u.def.section; if (input_sec->output_section != NULL) { + if (eoinfo->localsyms && flinfo->filesym_count == 1) + { + bfd_boolean second_pass_sym + = (input_sec->owner == flinfo->output_bfd + || input_sec->owner == NULL + || (input_sec->flags & SEC_LINKER_CREATED) != 0 + || (input_sec->owner->flags & BFD_LINKER_CREATED) != 0); + + eoinfo->need_second_pass |= second_pass_sym; + if (eoinfo->second_pass != second_pass_sym) + return TRUE; + } + sym.st_shndx = _bfd_elf_section_from_bfd_section (flinfo->output_bfd, input_sec->output_section); @@ -9111,6 +9133,7 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) bfd_size_type address_size; bfd_vma r_type_mask; int r_sym_shift; + bfd_boolean have_file_sym = FALSE; output_bfd = flinfo->output_bfd; bed = get_elf_backend_data (output_bfd); @@ -9246,6 +9269,29 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) && bfd_is_local_label_name (input_bfd, name))) continue; + if (ELF_ST_TYPE (isym->st_info) == STT_FILE) + { + have_file_sym = TRUE; + flinfo->filesym_count += 1; + } + if (!have_file_sym) + { + /* In the absence of debug info, bfd_find_nearest_line uses + FILE symbols to determine the source file for local + function symbols. Provide a FILE symbol here if input + files lack such, so that their symbols won't be + associated with a previous input file. It's not the + source file, but the best we can do. */ + have_file_sym = TRUE; + flinfo->filesym_count += 1; + memset (&osym, 0, sizeof (osym)); + osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); + osym.st_shndx = SHN_ABS; + if (!elf_link_output_sym (flinfo, input_bfd->filename, &osym, + bfd_abs_section_ptr, NULL)) + return FALSE; + } + osym = *isym; /* Adjust the section index for the output file. */ @@ -10318,6 +10364,7 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) flinfo.symshndxbuf = NULL; flinfo.symbuf_count = 0; flinfo.shndxbuf_size = 0; + flinfo.filesym_count = 0; /* The object attributes have been merged. Remove the input sections from the link, and set the contents of the output @@ -10792,6 +10839,17 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) } } + /* Output a FILE symbol so that following locals are not associated + with the wrong input file. */ + memset (&elfsym, 0, sizeof (elfsym)); + elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); + elfsym.st_shndx = SHN_ABS; + + if (flinfo.filesym_count > 1 + && !elf_link_output_sym (&flinfo, NULL, &elfsym, + bfd_und_section_ptr, NULL)) + return FALSE; + /* Output any global symbols that got converted to local in a version script or due to symbol visibility. We do this in a separate step since ELF requires all local symbols to appear @@ -10801,10 +10859,25 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) eoinfo.failed = FALSE; eoinfo.flinfo = &flinfo; eoinfo.localsyms = TRUE; + eoinfo.need_second_pass = FALSE; + eoinfo.second_pass = FALSE; bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); if (eoinfo.failed) return FALSE; + if (flinfo.filesym_count == 1 + && !elf_link_output_sym (&flinfo, NULL, &elfsym, + bfd_und_section_ptr, NULL)) + return FALSE; + + if (eoinfo.need_second_pass) + { + eoinfo.second_pass = TRUE; + bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); + if (eoinfo.failed) + return FALSE; + } + /* If backend needs to output some local symbols not present in the hash table, do it now. */ if (bed->elf_backend_output_arch_local_syms) -- cgit v1.1