From patchwork Mon Aug 17 12:09:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xiaoming Ni X-Patchwork-Id: 40279 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 0C7DB3851C1A; Mon, 17 Aug 2020 12:09:24 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from huawei.com (szxga07-in.huawei.com [45.249.212.35]) by sourceware.org (Postfix) with ESMTPS id 741503851C0A for ; Mon, 17 Aug 2020 12:09:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 741503851C0A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=huawei.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nixiaoming@huawei.com Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 531DA521EFBF9031E011; Mon, 17 Aug 2020 20:09:17 +0800 (CST) Received: from use12-sp2.huawei.com (10.67.189.174) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.487.0; Mon, 17 Aug 2020 20:09:09 +0800 From: Xiaoming Ni To: , , , , , Subject: [PATCH v2] elf: Sort only uninitialized objects in _dl_map_object_deps() Date: Mon, 17 Aug 2020 20:09:05 +0800 Message-ID: <20200817120906.55044-1-nixiaoming@huawei.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Originating-IP: [10.67.189.174] X-CFilter-Loop: Reflected X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_MANYTO, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: wangle6@huawei.com, nixiaoming@huawei.com Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" TIS ELF Version 1.2 Initialization and Termination Functions: 1. Before the initialization code for any object A is called, the initialization code for any other objects that object A depends on are called. 2. The order in which the dynamic linker calls termination functions is the exact reverse order of their corresponding initialization functions. 3. The dynamic linker ensures that it will not execute any initialization or termination functions more than once. According to 1 and 2: _dl_sort_maps() is used for sorting when dlopen/dlclose. According to 3: At dlopen, only the uninitialized objects need to be sorted. v1: https://public-inbox.org/libc-alpha/20200725105205.103328-1-nixiaoming@huawei.com/ The new function _dl_sort_uninit_maps(): sorts the linked list based on whether the object has been initialized, v2: the Chung-Lin Tang patch has optimized the sorting algorithm complexity to linear: https://patchwork.sourceware.org/project/glibc/patch/1427b370-7400-afd0-16e8-55c1072db20e@mentor.com/ https://patchwork.sourceware.org/project/glibc/patch/5de3ab61-3dca-b400-15c6-92ff5ae80877@mentor.com/ When the algorithm complexity of _dl_sort_maps() has been optimized, the benefits brought by _dl_sort_uninit_maps() are not obvious. https://public-inbox.org/libc-alpha/accfd786-0d1e-bb27-d950-76ab30633767@mentor.com/ so _dl_sort_uninit_maps() is deleted from v2. Avoid unnecessary sorting only when the entire linked list has been initialized. --- elf/dl-deps.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/elf/dl-deps.c b/elf/dl-deps.c index b5a43232a7..8f377302bf 100644 --- a/elf/dl-deps.c +++ b/elf/dl-deps.c @@ -611,7 +611,20 @@ Filters not supported with LD_TRACE_PRELINKING")); memcpy (l_initfini, map->l_searchlist.r_list, nlist * sizeof (struct link_map *)); - _dl_sort_maps (&l_initfini[1], nlist - 1, NULL, false); + /* TIS ELF Version 1.2 + * Initialization and Termination Functions: + * 1. Before the initialization code for any object A is called, the + * initialization code for any other objects that object A depends on are called. + * 2. The order in which the dynamic linker calls termination functions is the + * exact reverse order of their corresponding initialization functions. + * 3. The dynamic linker ensures that it will not execute any initialization + * or termination functions more than once. + * + * According to 1 and 2, _dl_sort_maps() is used for sorting when dlopen/dlclose. + * According to 3, we only need to sort the uninitialized objects. + */ + if (map->l_init_called == 0) + _dl_sort_maps (&l_initfini[1], nlist - 1, NULL, false); /* Terminate the list of dependencies. */ l_initfini[nlist] = NULL;