From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by sourceware.org (Postfix) with ESMTPS id 0001A3858C42 for ; Tue, 23 Apr 2024 11:44:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0001A3858C42 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0001A3858C42 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::630 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713872682; cv=none; b=OKULkpAXF1+zGWgbFzuCmGn1MxBYfhooGB/r2hIlAX1oViin90GlnTRJh2kBufpKZq3fSf+5sMOtigR+YuenFkgkm/7EpspR5ONkFdeZuYIAtCGksgnZ9ae2LxYMuQQeqMXfwhuj0NwPxXFfGJLEJ8MFBCGD+vUmlHm457KTnKA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713872682; c=relaxed/simple; bh=kvgTaGMXk0tBpgALOjhG7ivj+4GXogKllZxEEHliuJo=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Khto0iodd6SMWfrx9iBux42NhzBMs25h85eJP+5sFkyhu+pgaHAuwNRwTcSZGZqlDdrGqME2A864Dq1gtIgpZBFC+wsAOyc4rRG3Ef5CaFzR+mGg0lyD40RB9qVbcmO0jcoCZ6VxZACVeGuJMrNbRzvfvzZWnBxnoQQeovlph9c= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1e5715a9ebdso45257565ad.2 for ; Tue, 23 Apr 2024 04:44:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713872679; x=1714477479; darn=sourceware.org; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=x2svgukUDpTl3uxg99JuIfH6zhfkeGgPNyHB0ESIbCo=; b=Kmshc0kRIszQBntS+leb5oK3f7e3JBknplqL9xgaNv/oIXndHhEc5YmTtY04/o0ljC 4mNgE4E8MAf8h4BWZo1sTkeYtZzIPaDQLZbHIku5TU2KTkZakdW3ia3WXtwX8ybS7mQF dH25FCkQpPb9ZUa0yScfPTMt9v5XRf1ilOHVI+ZPleDkz32K5CtU8teYKqkv6Ll7QLqr 1tij/bSHpUPD86eGhsi8EJzqDfu0bxc5bj0mpclDr9sQFgYSH513fuNe8YWYe88xPulm m9nrSk5zyPy/DrgNHnH4x8mrNyT6zlx9aYscNzq+qcPIrLgAuGwGlsn+qgCZt5AZBODi ZnsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713872679; x=1714477479; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=x2svgukUDpTl3uxg99JuIfH6zhfkeGgPNyHB0ESIbCo=; b=kXS4oQF4kW3nSzo/nHgf1r5d68MpAkkDSl0P7MneDFU18QsotDa19fkjNkC4z1prCy bLnEUcsvNeLEeYg9vBO4eKar5W8grcrQl/SG+iEMwXpQQg0zLfRLjPrIJWgJLVUukhod enA21lL/egmuC6lgMAGXGEBQUs1gLvJzqzCZYWK7x+nfTZ5j7yYWGOC4H4Jv8gcqokH6 +qcS0n1kyYTZ5koJc06bxn8xAAt5AFlFS6kmSEOAXnTxmylTUQfrPOhi/8g+HktjRzJa 8aLazlvk1YUcJb6OOhAywLFp2UilKK4NdJ1jCC/FvUjs+v6uXUKM7p3HUK5tBs7fklkd ltkQ== X-Gm-Message-State: AOJu0YwfpDWjobDgqSImGV4EE3pFaNWYVoRE1xHCOu6+ezD9nG2LkOsL YHgPwh0OqLyQNyPqwa4hqioHZpqdPv/AeXTdhh+ANiLghQnC1QAuRHIUDQ== X-Google-Smtp-Source: AGHT+IHa0Q7ZGTWrsYRHw2Uh9FdLA2JO+tChGCXl7wLLg2GFV275PTSjbSqcUbUqk9UE+55gq/lwLA== X-Received: by 2002:a17:902:a589:b0:1e4:4955:98d9 with SMTP id az9-20020a170902a58900b001e4495598d9mr12720685plb.45.1713872678266; Tue, 23 Apr 2024 04:44:38 -0700 (PDT) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id w16-20020a170902e89000b001e8ab6b927asm8596918plg.39.2024.04.23.04.44.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Apr 2024 04:44:37 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 6ECBB1142750; Tue, 23 Apr 2024 21:14:35 +0930 (ACST) Date: Tue, 23 Apr 2024 21:14:35 +0930 From: Alan Modra To: binutils@sourceware.org Subject: ignore some symbols in elf.c:swap_out_syms Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3032.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_STOCKGEN,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: The reason behind this patch was noticing that generic ELF targets fail to remove "bar" in the recently committed ld-elf/undefweak-1 test. (Despite that, those targets pass the test due to it being too strict when matching symbols. "bar" gets turned into a local weak defined absolute symbol.) swap_out_syms currently drops local section syms that are defined in discarded sections. Extend that to also drop other symbols in discarded sections too, even global symbols. The linker goes to quite a lot of effort to ensure globals in discarded section take a definition from the kept linkonce or comdat group section. So the global sym change should only affect cases where something is quite wrong about the set of linkonce or comdat group sections. However that change to elf_map_symbols meant we dropped _DYNAMIC_LINK / _DYNAMIC_LINKING for mips, a global absolute symbol given STT_SECTION type for some reason. That problem is fixed by reverting the pr14493 change which is no longer needed due to a) BSF_SECTION_SYM_USED on x86, and b) fixing objcopy to use copy_private_symbol_data. bfd/ PR 14493 * elf.c (ignore_sym): Rename from ignore_section_sym. Return true for any symbol without a section or in a discarded section. Revert pr14493 change. (elf_map_symbols): Tidy. Use ignore_sym on all symbols. (swap_out_syms): Tidy. ld/ * testsuite/ld-elf/undefweak-1.rd: Match any "bar". diff --git a/bfd/elf.c b/bfd/elf.c index 41ec649ebab..0f4907adc32 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -4573,36 +4573,32 @@ _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, return dst_count; } -/* Don't output section symbols for sections that are not going to be - output, that are duplicates or there is no BFD section. */ +/* Don't output symbols for sections that are not going to be output, + that are duplicates or there is no BFD section. */ static bool -ignore_section_sym (bfd *abfd, asymbol *sym) +ignore_sym (asymbol *sym) { - elf_symbol_type *type_ptr; - if (sym == NULL) return false; - if ((sym->flags & BSF_SECTION_SYM) == 0) - return false; - - /* Ignore the section symbol if it isn't used. */ - if ((sym->flags & BSF_SECTION_SYM_USED) == 0) - return true; - if (sym->section == NULL) return true; - type_ptr = elf_symbol_from (sym); - return ((type_ptr != NULL - && type_ptr->internal_elf_sym.st_shndx != 0 - && bfd_is_abs_section (sym->section)) - || !(sym->section->owner == abfd - || (sym->section->output_section != NULL - && sym->section->output_section->owner == abfd - && sym->section->output_offset == 0) - || bfd_is_abs_section (sym->section))); + if ((sym->flags & BSF_SECTION_SYM) != 0) + { + if ((sym->flags & BSF_SECTION_SYM_USED) == 0) + return true; + /* With ld -r on generic elf targets it is possible to have + multiple section symbols in the output for a given section. + We'd like to get rid of all but the first one. This drops + them if the first input section is non-zero size, but fails + to do so if the first input section is zero sized. */ + if (sym->section->output_offset != 0) + return true; + } + + return discarded_section (sym->section); } /* Map symbol from it's internal number to the external number, moving @@ -4616,8 +4612,6 @@ elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) asymbol **sect_syms; unsigned int num_locals = 0; unsigned int num_globals = 0; - unsigned int num_locals2 = 0; - unsigned int num_globals2 = 0; unsigned int max_index = 0; unsigned int idx; asection *asect; @@ -4651,7 +4645,7 @@ elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) if ((sym->flags & BSF_SECTION_SYM) != 0 && sym->value == 0 - && !ignore_section_sym (abfd, sym) + && !ignore_sym (sym) && !bfd_is_abs_section (sym->section)) { asection *sec = sym->section; @@ -4666,9 +4660,11 @@ elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) /* Classify all of the symbols. */ for (idx = 0; idx < symcount; idx++) { + if (ignore_sym (syms[idx])) + continue; if (sym_is_global (abfd, syms[idx])) num_globals++; - else if (!ignore_section_sym (abfd, syms[idx])) + else num_locals++; } @@ -4680,13 +4676,13 @@ elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) { asymbol *sym = asect->symbol; /* Don't include ignored section symbols. */ - if (!ignore_section_sym (abfd, sym) + if (!ignore_sym (sym) && sect_syms[asect->index] == NULL) { - if (!sym_is_global (abfd, asect->symbol)) - num_locals++; - else + if (sym_is_global (abfd, asect->symbol)) num_globals++; + else + num_locals++; } } @@ -4696,34 +4692,36 @@ elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) if (new_syms == NULL) return false; + unsigned int num_globals2 = 0; + unsigned int num_locals2 = 0; for (idx = 0; idx < symcount; idx++) { asymbol *sym = syms[idx]; unsigned int i; + if (ignore_sym (sym)) + continue; + if (sym_is_global (abfd, sym)) i = num_locals + num_globals2++; - /* Don't include ignored section symbols. */ - else if (!ignore_section_sym (abfd, sym)) - i = num_locals2++; else - continue; + i = num_locals2++; new_syms[i] = sym; sym->udata.i = i + 1; } for (asect = abfd->sections; asect; asect = asect->next) { asymbol *sym = asect->symbol; - if (!ignore_section_sym (abfd, sym) + if (!ignore_sym (sym) && sect_syms[asect->index] == NULL) { unsigned int i; sect_syms[asect->index] = sym; - if (!sym_is_global (abfd, sym)) - i = num_locals2++; - else + if (sym_is_global (abfd, sym)) i = num_locals + num_globals2++; + else + i = num_locals2++; new_syms[i] = sym; sym->udata.i = i + 1; } @@ -8897,8 +8895,8 @@ swap_out_syms (bfd *abfd, sym.st_other = 0; sym.st_shndx = SHN_UNDEF; sym.st_target_internal = 0; - symstrtab[0].sym = sym; - symstrtab[0].dest_index = outbound_syms_index; + symstrtab[outbound_syms_index].sym = sym; + symstrtab[outbound_syms_index].dest_index = outbound_syms_index; outbound_syms_index++; } @@ -8907,14 +8905,11 @@ swap_out_syms (bfd *abfd, && bed->elf_backend_name_local_section_symbols (abfd)); syms = bfd_get_outsymbols (abfd); - for (idx = 0; idx < symcount;) + for (idx = 0; idx < symcount; idx++) { Elf_Internal_Sym sym; - bfd_vma value = syms[idx]->value; - elf_symbol_type *type_ptr; - flagword flags = syms[idx]->flags; - int type; + flagword flags = syms[idx]->flags; if (!name_local_sections && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) { @@ -8932,10 +8927,11 @@ swap_out_syms (bfd *abfd, goto error_return; } - type_ptr = elf_symbol_from (syms[idx]); + bfd_vma value = syms[idx]->value; + elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]); + asection *sec = syms[idx]->section; - if ((flags & BSF_SECTION_SYM) == 0 - && bfd_is_com_section (syms[idx]->section)) + if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec)) { /* ELF common symbols put the alignment into the `value' field, and the size into the `size' field. This is backwards from @@ -8946,12 +8942,10 @@ swap_out_syms (bfd *abfd, sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); else sym.st_value = type_ptr->internal_elf_sym.st_value; - sym.st_shndx = _bfd_elf_section_from_bfd_section - (abfd, syms[idx]->section); + sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); } else { - asection *sec = syms[idx]->section; unsigned int shndx; if (sec->output_section) @@ -9049,6 +9043,7 @@ Unable to handle section index %x in ELF symbol. Using ABS instead."), sym.st_shndx = shndx; } + int type; if ((flags & BSF_THREAD_LOCAL) != 0) type = STT_TLS; else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) @@ -9128,10 +9123,8 @@ Unable to handle section index %x in ELF symbol. Using ABS instead."), sym.st_target_internal = 0; } - idx++; - symstrtab[idx].sym = sym; - symstrtab[idx].dest_index = outbound_syms_index; - + symstrtab[outbound_syms_index].sym = sym; + symstrtab[outbound_syms_index].dest_index = outbound_syms_index; outbound_syms_index++; } @@ -9139,7 +9132,7 @@ Unable to handle section index %x in ELF symbol. Using ABS instead."), _bfd_elf_strtab_finalize (stt); /* Swap out the .strtab section. */ - for (idx = 0; idx <= symcount; idx++) + for (idx = 0; idx < outbound_syms_index; idx++) { struct elf_sym_strtab *elfsym = &symstrtab[idx]; if (elfsym->sym.st_name == (unsigned long) -1) diff --git a/ld/testsuite/ld-elf/undefweak-1.rd b/ld/testsuite/ld-elf/undefweak-1.rd index 5b48c13c892..6bf3da0ce60 100644 --- a/ld/testsuite/ld-elf/undefweak-1.rd +++ b/ld/testsuite/ld-elf/undefweak-1.rd @@ -6,5 +6,5 @@ #failif #... -.*: 0+ +0 +FUNC +WEAK +DEFAULT +UND +bar +.* bar #pass -- Alan Modra Australia Development Lab, IBM