From patchwork Tue Mar 5 09:51:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ying Huang X-Patchwork-Id: 86787 X-Patchwork-Delegate: mark@klomp.org Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 203323858C54 for ; Tue, 5 Mar 2024 09:52:32 +0000 (GMT) X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from va-1-16.ptr.blmpb.com (va-1-16.ptr.blmpb.com [209.127.230.16]) by sourceware.org (Postfix) with ESMTPS id 528A4385842F for ; Tue, 5 Mar 2024 09:52:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 528A4385842F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=oss.cipunited.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=oss.cipunited.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 528A4385842F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=209.127.230.16 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709632326; cv=none; b=Hy7dPzDVfahwDJ//ChtuyztlKVPKKVWEkxnhiw1L17X1U32sZN6xEMvUctTsJjpiTRVP481jVnQzV7GZEbJUQBEifsoku5df4KSKYU4EU4Ix/orxH8kpei+fzAVwVhvQOfsL3pWhgKtUuavXFuKJIqVYOPu7/Ats8ucEISWVvoQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709632326; c=relaxed/simple; bh=8OdMzWx0paCdHJh7f00wHoa2yz5FvbI6BCIhMx1Yz4A=; h=DKIM-Signature:From:Subject:Date:To:Mime-Version:Message-Id; b=Ow9dWAL+Y2KxiihFftZTdhkupAKxFKw6hDEOjQfarIkU9SUjg8CpIY8ZnEO7cOlFJSyEOfQtKHpkDMDfk3B80MD33RB+ZrrdU7MsifKiMxxKbyrwVNTPodKHWOqxflSBP5GoGWa064bLDxLg6vCsEWIcGNeSvyV4phPnev/GZHg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=feishu2303200042; d=oss.cipunited.com; t=1709632312; h=from:subject:mime-version:from:date:message-id:subject:to:cc: reply-to:content-type:mime-version:in-reply-to:message-id; bh=AoZivuAetnfeGhSGiCftzE23H/nZwjJZeoO8Df9uqaU=; b=RnX7NJqeGLJ8HBHZyKUm1XjT/GE1h5aYPtwvtUQdCgVcNijTAZ6CUZNXcePjZIlM1joRUv e/Dzj7q1Cf6AE/4lC7i+wTlpFW1RaoMdcy1dCSYFpifRLl7Z2GyPo0fsIuGvZetqDQaWyr tVky5mCedezIgUg3xQZiZ47IQlROJAQPUkVy5r+ak79u2WHrnElm8VzkV3UVVzWoHlnTbk IrfSIVm45yvtHyjXaaT4nEyPfXcHFytKWNjKvqluhfrWK6E/Iw3fePlWCftOzpclph5tvC r+Koh+b0TKVde67IT3LrCvt3zUwhMOmKLx6Yz/ECUO04UccbTE6p28WCmNAnbQ== From: "Ying Huang" Subject: [PATCH v3 4/6] elflint: adapt src/elflint --gnu src/nm on mips Date: Tue, 5 Mar 2024 17:51:20 +0800 X-Original-From: Ying Huang To: Mime-Version: 1.0 Received: from localhost.localdomain ([123.161.203.50]) by smtp.feishu.cn with ESMTPS; Tue, 05 Mar 2024 17:51:50 +0800 X-Mailer: git-send-email 2.39.2 Message-Id: <20240305095122.889077-5-ying.huang@oss.cipunited.com> X-Lms-Return-Path: Cc: "Ying Huang" X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, NO_DNS_FOR_FROM, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org From: Ying Huang The errors were: $ src/elflint --gnu src/nm section [ 2] '.MIPS.options' contains unknown flag(s) 0x8000000 section [ 7] '.dynsym': symbol 165 (_DYNAMIC_LINKING): non-local section symbol section [24] '.got' contains invalid processor-specific flag(s) 0x10000000 section [25] '.sdata' contains invalid processor-specific flag(s) 0x10000000 section [29] '.debug_aranges' has wrong type: expected PROGBITS, is MIPS_DWARF section [30] '.debug_info' has wrong type: expected PROGBITS, is MIPS_DWARF section [31] '.debug_abbrev' has wrong type: expected PROGBITS, is MIPS_DWARF section [32] '.debug_line' has wrong type: expected PROGBITS, is MIPS_DWARF section [33] '.debug_frame' has wrong type: expected PROGBITS, is MIPS_DWARF section [34] '.debug_str' has wrong type: expected PROGBITS, is MIPS_DWARF section [35] '.debug_loc' has wrong type: expected PROGBITS, is MIPS_DWARF section [36] '.debug_ranges' has wrong type: expected PROGBITS, is MIPS_DWARF section [38] '.symtab': symbol 785 (_gp): st_value out of bounds section [38] '.symtab': symbol 910 (_fbss): st_value out of bounds section [38] '.symtab': symbol 1051 (_DYNAMIC_LINKING): non-local section symbol After fixing: $ src/elflint --gnu src/nm No errors Signed-off-by: Ying Huang --- backends/mips_init.c | 3 +++ backends/mips_symbol.c | 37 +++++++++++++++++++++++++++++++++++++ src/elflint.c | 26 +++++++++++++++++++++----- 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/backends/mips_init.c b/backends/mips_init.c index 6b9bd4c8..521e6e51 100644 --- a/backends/mips_init.c +++ b/backends/mips_init.c @@ -51,9 +51,12 @@ mips_init (Elf *elf __attribute__ ((unused)), HOOK (eh, section_type_name); HOOK (eh, machine_flag_check); HOOK (eh, machine_flag_name); + HOOK (eh, machine_section_flag_check); HOOK (eh, segment_type_name); HOOK (eh, dynamic_tag_check); HOOK (eh, dynamic_tag_name); HOOK (eh, check_object_attribute); + HOOK (eh, check_special_symbol); + HOOK (eh, check_reloc_target_type); return eh; } diff --git a/backends/mips_symbol.c b/backends/mips_symbol.c index 1545fc4b..af4b6e45 100644 --- a/backends/mips_symbol.c +++ b/backends/mips_symbol.c @@ -158,6 +158,43 @@ mips_section_type_name (int type, return NULL; } +bool +mips_check_reloc_target_type (Ebl *ebl __attribute__ ((unused)), Elf64_Word sh_type) +{ + return (sh_type == SHT_MIPS_DWARF); +} + +/* Check whether given symbol's st_value and st_size are OK despite failing + normal checks. */ +bool +mips_check_special_symbol (Elf *elf, + const GElf_Sym *sym __attribute__ ((unused)), + const char *name __attribute__ ((unused)), + const GElf_Shdr *destshdr) +{ + size_t shstrndx; + if (elf_getshdrstrndx (elf, &shstrndx) != 0) + return false; + const char *sname = elf_strptr (elf, shstrndx, destshdr->sh_name); + if (sname == NULL) + return false; + return (strcmp (sname, ".got") == 0 || strcmp (sname, ".bss") == 0); +} + +/* Check whether SHF_MASKPROC flags are valid. */ +bool +mips_machine_section_flag_check (GElf_Xword sh_flags) +{ + return ((sh_flags &~ (SHF_MIPS_GPREL | + SHF_MIPS_MERGE | + SHF_MIPS_ADDR | + SHF_MIPS_STRINGS | + SHF_MIPS_NOSTRIP | + SHF_MIPS_LOCAL | + SHF_MIPS_NAMES | + SHF_MIPS_NODUPE)) == 0); +} + /* Check whether machine flags are valid. */ bool mips_machine_flag_check (GElf_Word flags) diff --git a/src/elflint.c b/src/elflint.c index 864de710..092409a2 100644 --- a/src/elflint.c +++ b/src/elflint.c @@ -936,7 +936,9 @@ section [%2d] '%s': symbol %zu (%s): non-local symbol outside range described in } if (GELF_ST_TYPE (sym->st_info) == STT_SECTION - && GELF_ST_BIND (sym->st_info) != STB_LOCAL) + && GELF_ST_BIND (sym->st_info) != STB_LOCAL + && ehdr->e_machine != EM_MIPS + && strcmp (name, "_DYNAMIC_LINKING") != 0) ERROR (_("\ section [%2d] '%s': symbol %zu (%s): non-local section symbol\n"), idx, section_name (ebl, idx), cnt, name); @@ -3828,6 +3830,10 @@ cannot get section header for section [%2zu] '%s': %s\n"), && ebl_bss_plt_p (ebl)) good_type = SHT_NOBITS; + if (ehdr->e_machine == EM_MIPS + && (strstr(special_sections[s].name, ".debug") != NULL)) + good_type = SHT_MIPS_DWARF; + /* In a debuginfo file, any normal section can be SHT_NOBITS. This is only invalid for DWARF sections and .shstrtab. */ if (shdr->sh_type != good_type @@ -3988,12 +3994,21 @@ section [%2zu] '%s': size not multiple of entry size\n"), ERROR (_("section [%2zu] '%s'" " contains invalid processor-specific flag(s)" " %#" PRIx64 "\n"), - cnt, section_name (ebl, cnt), sh_flags & SHF_MASKPROC); + cnt, section_name (ebl, cnt), sh_flags & SHF_MASKPROC); sh_flags &= ~(GElf_Xword) SHF_MASKPROC; } if (sh_flags & SHF_MASKOS) - if (gnuld) - sh_flags &= ~(GElf_Xword) SHF_GNU_RETAIN; + { + if (gnuld) + sh_flags &= ~(GElf_Xword) SHF_GNU_RETAIN; + if (!ebl_machine_section_flag_check (ebl, + sh_flags & SHF_MASKOS)) + ERROR (_("section [%2zu] '%s'" + " contains invalid os-specific flag(s)" + " %#" PRIx64 "\n"), + cnt, section_name (ebl, cnt), sh_flags & SHF_MASKOS); + sh_flags &= ~(GElf_Xword) SHF_MASKOS; + } if (sh_flags != 0) ERROR (_("section [%2zu] '%s' contains unknown flag(s)" " %#" PRIx64 "\n"), @@ -4059,6 +4074,7 @@ section [%2zu] '%s': merge flag set but entry size is zero\n"), switch (shdr->sh_type) { case SHT_PROGBITS: + case SHT_MIPS_DWARF: break; case SHT_NOBITS: @@ -4716,7 +4732,7 @@ program header offset in ELF header and PHDR entry do not match")); if (shdr != NULL && ((is_debuginfo && shdr->sh_type == SHT_NOBITS) || (! is_debuginfo - && (shdr->sh_type == SHT_PROGBITS + && (is_debug_section_type(shdr->sh_type) || shdr->sh_type == SHT_X86_64_UNWIND))) && elf_strptr (ebl->elf, shstrndx, shdr->sh_name) != NULL && ! strcmp (".eh_frame_hdr",