From patchwork Wed Dec 6 17:20:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 81535 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 9D422386100C for ; Wed, 6 Dec 2023 17:20:58 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by sourceware.org (Postfix) with ESMTPS id 107F33858C31 for ; Wed, 6 Dec 2023 17:20:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 107F33858C31 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 107F33858C31 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883219; cv=none; b=oFWoRW5Snhub/vrKB6ZcT93s6zFiEGqohsbxoJOHiRZXJ5N8IW006VVes5hgeb4+WO28AeJUTFpTcBtu2/GFhFcIvcBxnyh6oPyyEzwpWbaGOIuPpSfCLZDZHvPaBVcmkky+br8TUIErh1HV+SJ1RsUazV2YuJfxquXf5/B5S3Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701883219; c=relaxed/simple; bh=dULd9GzM1XgCXwIkb/YLe/rpOcT+ioN+FmEXyduUYiY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=mdacF1vCVvoREwAm7se8TOAd4qIyRGYrPMRsv+4sTLVZL2MpZtg2uspYhZcWxowB3yqSQNF10rISVOqkmgdFN0ply+niMjp/DjSPVB5Gzsk92dn3/T5wAM58IPEv9fbMSJQSVytJsKQO1O0vJ8YrZ5g/mqzBjQjNLzRzNa3xS7I= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1d04dba2781so37911455ad.3 for ; Wed, 06 Dec 2023 09:20:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701883217; x=1702488017; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2vjVugOhYDfsulJ/fZg2RiEMmfP1r78K2xJH/vIdDs0=; b=e4KmlDX+xYfxxw4W8dZGxKbZGUHhdvfvi7QFBwHibXa9USiLz3+nK0HltJMsuKY/JY xs6UkNyaGEfu//laLfIJ+NSZn5IEMeZwvPhbjaqNItyh6AOWS35vvACJARFoWbU37Vdh JZbKRO1z3Uy96i+65S1FXCtLmbu6PauC00WdwZzArvm/8k2K53FvQzMv0Ld/CaPpPHWv fk/W0XauO0HfeqHi8KfWX4MUZUw+I/s5caI3sZPEkz0daXhPSNWgEhbv2i9YjjWSo9M3 Lod87Ol4GDdvM1J+2BVnpUDlgpA3130USiZLcunLwFNSZqnoieLTDYNLMAjfwWptxqrJ ZJIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701883217; x=1702488017; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2vjVugOhYDfsulJ/fZg2RiEMmfP1r78K2xJH/vIdDs0=; b=WdPjS7Kffpv6J0/TPOEZbILthcT/su0nXGmsxFF+WpDOhVl/ZL0hsBWl9pUCz7UZMG Lv/d2RQ5sLI/hQlNwRIbSFE4oZ2hBFAr/BLkInOUP3fI5+0inmsgM0V19SrO12uAVbgf 9MITC5K0x7S0uxDLfnM5gTHDOoPDToAZmjp6Ux6jwc1srnJLU0EJ4wZOs59J3GwgJaAX p2sV8RilcRoGrJpG+t68njgGol1sWtxbEYwWTOFoqsLXFv4PgBnvsQ+I/VdUiU2bA7VJ E1hEMhDbFt34BSgLm9HT4Qib0dO/Px+XxH7khbBJERtV2km3NV3SNhHTi6W17mEBo4hn tjDg== X-Gm-Message-State: AOJu0YyyuttiREhg47ZG1cN7/Vm77OjhVubyKIgrv1rFzM7aQ8ZfVx5e /Sg9weZLZXN1VKhZnxvm1Tk= X-Google-Smtp-Source: AGHT+IFTlxIfKDDaatY4CS0z33126TiTyY6zFYR/NL3huiLUxCB0bAIQJ9rt/Wn4NRSweXPNz9OwOA== X-Received: by 2002:a17:902:d488:b0:1d0:9e59:35f1 with SMTP id c8-20020a170902d48800b001d09e5935f1mr918892plg.114.1701883216981; Wed, 06 Dec 2023 09:20:16 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.59.161.253]) by smtp.gmail.com with ESMTPSA id jk23-20020a170903331700b001d0ca40158dsm64894plb.280.2023.12.06.09.20.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 09:20:16 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id AF420740632; Wed, 6 Dec 2023 09:20:10 -0800 (PST) From: "H.J. Lu" To: libc-alpha@sourceware.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH 09/17] x86: Check PT_GNU_PROPERTY early Date: Wed, 6 Dec 2023 09:20:02 -0800 Message-ID: <20231206172010.1023415-10-hjl.tools@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231206172010.1023415-1-hjl.tools@gmail.com> References: <20231206172010.1023415-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3024.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org The PT_GNU_PROPERTY segment is scanned before PT_NOTE. For binaries with the PT_GNU_PROPERTY segment, we can check it to avoid scan of the PT_NOTE segment. --- sysdeps/x86/dl-prop.h | 120 ++++++++++++++++++++++++++++-------------- 1 file changed, 80 insertions(+), 40 deletions(-) diff --git a/sysdeps/x86/dl-prop.h b/sysdeps/x86/dl-prop.h index b2836f3009..d2c53c2182 100644 --- a/sysdeps/x86/dl-prop.h +++ b/sysdeps/x86/dl-prop.h @@ -81,6 +81,60 @@ _dl_open_check (struct link_map *m) #endif } +/* Check the GNU property and return its value. It returns: + -1: Skip this note. + 0: Stop checking. + 1: Continue to check. + */ +static inline int +_dl_check_gnu_property (unsigned int type, unsigned int datasz, + void *ptr, unsigned int *feature_1_and, + unsigned int *needed_1, + unsigned int *isa_1_needed) +{ + if (type == GNU_PROPERTY_X86_FEATURE_1_AND + || type == GNU_PROPERTY_X86_ISA_1_NEEDED + || type == GNU_PROPERTY_1_NEEDED) + { + /* The sizes of types which we are searching for are + 4 bytes. There is no point to continue if this + note is ill-formed. */ + if (datasz != 4) + return -1; + + /* NB: Stop the scan only after seeing all types which + we are searching for. */ + _Static_assert (((GNU_PROPERTY_X86_ISA_1_NEEDED + > GNU_PROPERTY_X86_FEATURE_1_AND) + && (GNU_PROPERTY_X86_FEATURE_1_AND + > GNU_PROPERTY_1_NEEDED)), + "GNU_PROPERTY_X86_ISA_1_NEEDED > " + "GNU_PROPERTY_X86_FEATURE_1_AND && " + "GNU_PROPERTY_X86_FEATURE_1_AND > " + "GNU_PROPERTY_1_NEEDED"); + if (type == GNU_PROPERTY_X86_FEATURE_1_AND) + *feature_1_and = *(unsigned int *) ptr; + else if (type == GNU_PROPERTY_1_NEEDED) + *needed_1 = *(unsigned int *) ptr; + else + { + *isa_1_needed = *(unsigned int *) ptr; + + /* Keep searching for the next GNU property note + generated by the older linker. */ + return 0; + } + } + else if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) + { + /* Stop the scan since property type is in ascending + order. */ + return 0; + } + + return 1; +} + static inline void __attribute__ ((unused)) _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, const ElfW(Addr) size, const ElfW(Addr) align) @@ -141,45 +195,14 @@ _dl_process_property_note (struct link_map *l, const ElfW(Nhdr) *note, last_type = type; - if (type == GNU_PROPERTY_X86_FEATURE_1_AND - || type == GNU_PROPERTY_X86_ISA_1_NEEDED - || type == GNU_PROPERTY_1_NEEDED) - { - /* The sizes of types which we are searching for are - 4 bytes. There is no point to continue if this - note is ill-formed. */ - if (datasz != 4) - return; - - /* NB: Stop the scan only after seeing all types which - we are searching for. */ - _Static_assert (((GNU_PROPERTY_X86_ISA_1_NEEDED - > GNU_PROPERTY_X86_FEATURE_1_AND) - && (GNU_PROPERTY_X86_FEATURE_1_AND - > GNU_PROPERTY_1_NEEDED)), - "GNU_PROPERTY_X86_ISA_1_NEEDED > " - "GNU_PROPERTY_X86_FEATURE_1_AND && " - "GNU_PROPERTY_X86_FEATURE_1_AND > " - "GNU_PROPERTY_1_NEEDED"); - if (type == GNU_PROPERTY_X86_FEATURE_1_AND) - feature_1_and = *(unsigned int *) ptr; - else if (type == GNU_PROPERTY_1_NEEDED) - needed_1 = *(unsigned int *) ptr; - else - { - isa_1_needed = *(unsigned int *) ptr; - - /* Keep searching for the next GNU property note - generated by the older linker. */ - break; - } - } - else if (type > GNU_PROPERTY_X86_ISA_1_NEEDED) - { - /* Stop the scan since property type is in ascending - order. */ - break; - } + int result = _dl_check_gnu_property (type, datasz, ptr, + &feature_1_and, + &needed_1, + &isa_1_needed); + if (result == -1) + return; /* Skip this note. */ + else if (result == 0) + break; /* Stop checking. */ /* Check the next property item. */ ptr += ALIGN_UP (datasz, sizeof (ElfW(Addr))); @@ -217,7 +240,24 @@ static inline int __attribute__ ((always_inline)) _dl_process_gnu_property (struct link_map *l, int fd, uint32_t type, uint32_t datasz, void *data) { - return 0; + /* This is called on each GNU property. */ + unsigned int needed_1 = 0; + unsigned int feature_1_and = 0; + unsigned int isa_1_needed = 0; + int result = _dl_check_gnu_property (type, datasz, data, + &feature_1_and, &needed_1, + &isa_1_needed); + if (needed_1 != 0) + l->l_1_needed = needed_1; + if (isa_1_needed != 0) + l->l_x86_isa_1_needed = isa_1_needed; + if (feature_1_and != 0) + l->l_x86_feature_1_and = feature_1_and; + if ((needed_1 | isa_1_needed | feature_1_and) != 0) + l->l_property = lc_property_valid; + else if (l->l_property == lc_property_unknown) + l->l_property = lc_property_none; + return result <= 0 ? 0 : result; } #endif /* _DL_PROP_H */