From patchwork Wed Mar 26 20:40:15 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Pluzhnikov X-Patchwork-Id: 304 Return-Path: X-Original-To: siddhesh@wilcox.dreamhost.com Delivered-To: siddhesh@wilcox.dreamhost.com Received: from homiemail-mx23.g.dreamhost.com (caibbdcaabja.dreamhost.com [208.113.200.190]) by wilcox.dreamhost.com (Postfix) with ESMTP id 7E6C336024E for ; Wed, 26 Mar 2014 13:40:53 -0700 (PDT) Received: by homiemail-mx23.g.dreamhost.com (Postfix, from userid 14307373) id 0ED6762045AAA; Wed, 26 Mar 2014 13:40:52 -0700 (PDT) X-Original-To: glibc@patchwork.siddhesh.in Delivered-To: x14307373@homiemail-mx23.g.dreamhost.com Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by homiemail-mx23.g.dreamhost.com (Postfix) with ESMTPS id D6BB762045AA7 for ; Wed, 26 Mar 2014 13:40:52 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id :mime-version:content-type; q=dns; s=default; b=DlUUFouC3a14ejTe N/P4Xlee0BO49WUiErF1Ay8P+0vF4uzi0vupxOne0C4Kd59KbgPGaYQwGuL2PCjK ZtxW1og3hKZhdSyKZ235kteD0AXXQdUQrBxIUsn5ib5TL8OzYDP+8sM+gUU7wBLs OQ/QJH9+NkSog2pgQgEepsdMHgc= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id :mime-version:content-type; s=default; bh=K1OYSjZ3W2ftyiyD08MwNq Cdsow=; b=mzbWo5C0s+SwsSFS4Crba6cDigyN32iuqnX0i/eO/aY7OFx0KG+l3r EWqzaa1citsNfX6U3tDWyso1PQ3vtPceCggLfO+b9yxoitV6QYK5+acbD/zLMGlM BpRRAheJnZ0Hm23XmG2AuBAcwhcPKzhPhRH5wQ8lC03x3ztr2hJuM= Received: (qmail 9263 invoked by alias); 26 Mar 2014 20:40:22 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 9211 invoked by uid 89); 26 Mar 2014 20:40:21 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.9 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-ig0-f201.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-type; bh=mQAQaTXRbuVA7zETqETQ0qF1g+XDe3a11iVLvW/qXWU=; b=UhqPk/JGtTcEZQ+u9W2BgSDgiBplPYYpdTpb6grd2dXxbDfRdmU4JHapWttxXcDSvT ZxyN54odRpQIoX+hJNoOdVedKjCtZfuZq23DgPp/D3ARs4eG+pbAfnwo0BukC0yrs368 JoRgZAsuh/zCUJn0igTtrn0AkIzTT9jV35fW+ss+61kINgWXEuTRPeq/J2F0JRVqojJO kpgP4yRadD/IuHhrO112rm3joqwESQb8IOF7HDMl3USTVicCq/iD1Xvh0LzZr0nbFV1G UqSbyq/MKb6x6DkWrqaUSGQFtf8SmWjS7cIS9f3H8h064PXvosYXeHDRZ186JSVu14Q8 OJeQ== X-Gm-Message-State: ALoCoQk41x1BWqWyppg0PFTZXkTl6SxQTOyPy9IT1ncdISvLoHPPraYrpkTU+zXoYh/Xtjcz5Ia3wdP87ewkocoBGFyQHQcf5I0ZANVUBjOKkkgzTnKV6rwkaLy/UGczdEB3XCL8K6JBNdy5h0r++GviH3Z/VeKnEzjSKU92b5dwWRa/t67oDdsxCgSwTJaBbQvqzGQv/3CGyNX+sEGSNgeVJzzcMkUSTg== X-Received: by 10.182.219.226 with SMTP id pr2mr18079567obc.44.1395866416906; Wed, 26 Mar 2014 13:40:16 -0700 (PDT) From: Paul Pluzhnikov To: GNU C Library Cc: ppluzhnikov@google.com, Siddhesh Poyarekar Subject: [patch] Replace most __builtin_expect's with __glibc_{un}likely Date: Wed, 26 Mar 2014 13:40:15 -0700 Message-ID: MIME-Version: 1.0 X-DH-Original-To: glibc@patchwork.siddhesh.in Greetings, As promised here: https://sourceware.org/ml/libc-alpha/2014-03/msg00676.html attached patch converts most of __builtin_expect's into __glibc_{un}likely in elf/dl-load.c This patch produces identical dl-load.{o,os} on Linux/x86_64 (except a few line numbers change). The remaining 7 __builtin_expect's caused the object file to change in unexpected ways, which means that either I didn't do them correctly, or there is something else at play. I'll try to do them next. Thanks, --- 2013-03-26 Paul Pluzhnikov * elf/dl-load.c: Convert __builtin_expect into __glibc_{un}likely. diff --git a/elf/dl-load.c b/elf/dl-load.c index 9455df2..fabe025 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -280,7 +280,7 @@ is_dst (const char *start, const char *name, const char *str, && (!is_path || name[len] != ':')) return 0; - if (__builtin_expect (secure, 0) + if (__glibc_unlikely (secure) && ((name[len] != '\0' && name[len] != '/' && (!is_path || name[len] != ':')) || (name != start + 1 && (!is_path || name[-2] != ':')))) @@ -381,7 +381,7 @@ _dl_dst_substitute (struct link_map *l, const char *name, char *result, /* In SUID/SGID programs, after $ORIGIN expansion the normalized path must be rooted in one of the trusted directories. */ - if (__builtin_expect (check_for_trusted, false) + if (__glibc_unlikely (check_for_trusted) && !is_trusted_path_normalize (last_elem, wp - last_elem)) wp = last_elem; else @@ -395,7 +395,7 @@ _dl_dst_substitute (struct link_map *l, const char *name, char *result, /* In SUID/SGID programs, after $ORIGIN expansion the normalized path must be rooted in one of the trusted directories. */ - if (__builtin_expect (check_for_trusted, false) + if (__glibc_unlikely (check_for_trusted) && !is_trusted_path_normalize (last_elem, wp - last_elem)) wp = last_elem; @@ -425,7 +425,7 @@ expand_dynamic_string_token (struct link_map *l, const char *s, int is_path) cnt = DL_DST_COUNT (s, is_path); /* If we do not have to replace anything simply copy the string. */ - if (__builtin_expect (cnt, 0) == 0) + if (__glibc_likely (cnt == 0)) return local_strdup (s); /* Determine the length of the substituted string. */ @@ -513,7 +513,7 @@ fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep, cp[len++] = '/'; /* Make sure we don't use untrusted directories if we run SUID. */ - if (__builtin_expect (check_trusted, 0) && !is_trusted_path (cp, len)) + if (__glibc_unlikely (check_trusted) && !is_trusted_path (cp, len)) { free (to_free); continue; @@ -604,7 +604,7 @@ decompose_rpath (struct r_search_path_struct *sps, /* First see whether we must forget the RUNPATH and RPATH from this object. */ - if (__builtin_expect (GLRO(dl_inhibit_rpath) != NULL, 0) + if (__glibc_unlikely (GLRO(dl_inhibit_rpath) != NULL) && !INTUSE(__libc_enable_secure)) { const char *inhp = GLRO(dl_inhibit_rpath); @@ -964,7 +964,7 @@ _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp, #ifdef SHARED /* When loading into a namespace other than the base one we must avoid loading ld.so since there can only be one copy. Ever. */ - if (__builtin_expect (nsid != LM_ID_BASE, 0) + if (__glibc_unlikely (nsid != LM_ID_BASE) && ((st.st_ino == GL(dl_rtld_map).l_ino && st.st_dev == GL(dl_rtld_map).l_dev) || _dl_name_match_p (name, &GL(dl_rtld_map)))) @@ -1026,7 +1026,7 @@ _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp, #ifdef SHARED /* Auditing checkpoint: we are going to add new objects. */ if ((mode & __RTLD_AUDIT) == 0 - && __builtin_expect (GLRO(dl_naudit) > 0, 0)) + && __glibc_unlikely (GLRO(dl_naudit) > 0)) { struct link_map *head = GL(dl_ns)[nsid]._ns_loaded; /* Do not call the functions for any auditing object. */ @@ -1124,14 +1124,13 @@ _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp, case PT_LOAD: /* A load command tells us to map in part of the file. We record the load commands and process them all later. */ - if (__builtin_expect ((ph->p_align & (GLRO(dl_pagesize) - 1)) != 0, - 0)) + if (__glibc_unlikely ((ph->p_align & (GLRO(dl_pagesize) - 1)) != 0)) { errstring = N_("ELF load command alignment not page-aligned"); goto call_lose; } - if (__builtin_expect (((ph->p_vaddr - ph->p_offset) - & (ph->p_align - 1)) != 0, 0)) + if (__glibc_unlikely (((ph->p_vaddr - ph->p_offset) + & (ph->p_align - 1)) != 0)) { errstring = N_("ELF load command address/offset not properly aligned"); @@ -1184,10 +1183,10 @@ _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp, /* If not loading the initial set of shared libraries, check whether we should permit loading a TLS segment. */ - if (__builtin_expect (l->l_type == lt_library, 1) + if (__glibc_likely (l->l_type == lt_library) /* If GL(dl_tls_dtv_slotinfo_list) == NULL, then rtld.c did not set up TLS data structures, so don't use them now. */ - || __builtin_expect (GL(dl_tls_dtv_slotinfo_list) != NULL, 1)) + || __glibc_likely (GL(dl_tls_dtv_slotinfo_list) != NULL)) { /* Assign the next available module ID. */ l->l_tls_modid = _dl_next_tls_modid (); @@ -1214,9 +1213,8 @@ _dl_map_object_from_fd (const char *name, int fd, struct filebuf *fbp, /* The first call allocates TLS bookkeeping data structures. Then we allocate the TCB for the initial thread. */ - if (__builtin_expect (_dl_tls_setup (), 0) - || __builtin_expect ((tcb = _dl_allocate_tls (NULL)) == NULL, - 0)) + if (__glibc_unlikely (_dl_tls_setup ()) + || __glibc_unlikely ((tcb = _dl_allocate_tls (NULL)) == NULL)) { errval = ENOMEM; errstring = N_("\ @@ -1430,7 +1428,7 @@ cannot allocate TLS data structures for initial thread"); /* Make sure we are not dlopen'ing an object that has the DF_1_NOOPEN flag set. */ - if (__builtin_expect (l->l_flags_1 & DF_1_NOOPEN, 0) + if (__glibc_unlikely (l->l_flags_1 & DF_1_NOOPEN) && (mode & __RTLD_DLOPEN)) { /* We are not supposed to load this object. Free all resources. */ @@ -1469,8 +1467,8 @@ cannot allocate TLS data structures for initial thread"); if (__glibc_unlikely ((stack_flags &~ GL(dl_stack_flags)) & PF_X)) { - if (__builtin_expect (__check_caller (RETURN_ADDRESS (0), allow_ldso), - 0) != 0) + + if (__glibc_unlikely (__check_caller (RETURN_ADDRESS (0), allow_ldso) != 0)) { errstring = N_("invalid caller"); goto call_lose; @@ -1560,7 +1558,7 @@ cannot enable executable stack as shared object requires"); /* If this object has DT_SYMBOLIC set modify now its scope. We don't have to do this for the main map. */ if ((mode & RTLD_DEEPBIND) == 0 - && __builtin_expect (l->l_info[DT_SYMBOLIC] != NULL, 0) + && __glibc_unlikely (l->l_info[DT_SYMBOLIC] != NULL) && &l->l_searchlist != l->l_scope[0]) { /* Create an appropriate searchlist. It contains only this map. @@ -1586,7 +1584,7 @@ cannot enable executable stack as shared object requires"); /* When we profile the SONAME might be needed for something else but loading. Add it right away. */ - if (__builtin_expect (GLRO(dl_profile) != NULL, 0) + if (__glibc_unlikely (GLRO(dl_profile) != NULL) && l->l_info[DT_SONAME] != NULL) add_name_to_object (l, ((const char *) D_PTR (l, l_info[DT_STRTAB]) + l->l_info[DT_SONAME]->d_un.d_val)); @@ -1600,7 +1598,7 @@ cannot enable executable stack as shared object requires"); #ifdef SHARED /* Auditing checkpoint: we have a new object. */ - if (__builtin_expect (GLRO(dl_naudit) > 0, 0) + if (__glibc_unlikely (GLRO(dl_naudit) > 0) && !GL(dl_ns)[l->l_ns]._ns_loaded->l_auditing) { struct audit_ifaces *afct = GLRO(dl_audit); @@ -1704,7 +1702,7 @@ open_verify (const char *name, struct filebuf *fbp, struct link_map *loader, #ifdef SHARED /* Give the auditing libraries a chance. */ - if (__builtin_expect (GLRO(dl_naudit) > 0, 0) && whatcode != 0 + if (__glibc_unlikely (GLRO(dl_naudit) > 0) && whatcode != 0 && loader->l_auditing == 0) { struct audit_ifaces *afct = GLRO(dl_audit); @@ -1748,7 +1746,7 @@ open_verify (const char *name, struct filebuf *fbp, struct link_map *loader, break; fbp->len += retlen; } - while (__builtin_expect (fbp->len < sizeof (ElfW(Ehdr)), 0)); + while (__glibc_unlikely (fbp->len < sizeof (ElfW(Ehdr)))); /* This is where the ELF header is loaded. */ ehdr = (ElfW(Ehdr) *) fbp->buf; @@ -1830,7 +1828,7 @@ open_verify (const char *name, struct filebuf *fbp, struct link_map *loader, goto call_lose; } - if (__builtin_expect (ehdr->e_version, EV_CURRENT) != EV_CURRENT) + if (__glibc_unlikely (ehdr->e_version != EV_CURRENT)) { errstring = N_("ELF file version does not match current one"); goto call_lose; @@ -1854,8 +1852,8 @@ open_verify (const char *name, struct filebuf *fbp, struct link_map *loader, errstring = N_("cannot dynamically load executable"); goto call_lose; } - else if (__builtin_expect (ehdr->e_phentsize, sizeof (ElfW(Phdr))) - != sizeof (ElfW(Phdr))) + else if (__glibc_unlikely (ehdr->e_phentsize != sizeof (ElfW(Phdr)))) + { errstring = N_("ELF file's phentsize not the expected size"); goto call_lose; @@ -1967,7 +1965,7 @@ open_path (const char *name, size_t namelen, int mode, /* If we are debugging the search for libraries print the path now if it hasn't happened now. */ - if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0) + if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS) && current_what != this_dir->what) { current_what = this_dir->what; @@ -2021,7 +2019,7 @@ open_path (const char *name, size_t namelen, int mode, /* Remember whether we found any existing directory. */ here_any |= this_dir->status[cnt] != nonexisting; - if (fd != -1 && __builtin_expect (mode & __RTLD_SECURE, 0) + if (fd != -1 && __glibc_unlikely (mode & __RTLD_SECURE) && INTUSE(__libc_enable_secure)) { /* This is an extra security effort to make sure nobody can @@ -2108,14 +2106,14 @@ _dl_map_object (struct link_map *loader, const char *name, /* If the requested name matches the soname of a loaded object, use that object. Elide this check for names that have not yet been opened. */ - if (__builtin_expect (l->l_faked, 0) != 0 + if (__glibc_unlikely (l->l_faked != 0) || __builtin_expect (l->l_removed, 0) != 0) continue; if (!_dl_name_match_p (name, l)) { const char *soname; - if (__builtin_expect (l->l_soname_added, 1) + if (__glibc_likely (l->l_soname_added) || l->l_info[DT_SONAME] == NULL) continue; @@ -2134,7 +2132,7 @@ _dl_map_object (struct link_map *loader, const char *name, } /* Display information if we are debugging. */ - if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0) + if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES) && loader != NULL) _dl_debug_printf ((mode & __RTLD_CALLMAP) == 0 ? "\nfile=%s [%lu]; needed by %s [%lu]\n" @@ -2144,7 +2142,7 @@ _dl_map_object (struct link_map *loader, const char *name, #ifdef SHARED /* Give the auditing libraries a chance to change the name before we try anything. */ - if (__builtin_expect (GLRO(dl_naudit) > 0, 0) + if (__glibc_unlikely (GLRO(dl_naudit) > 0) && (loader == NULL || loader->l_auditing == 0)) { struct audit_ifaces *afct = GLRO(dl_audit); @@ -2236,7 +2234,7 @@ _dl_map_object (struct link_map *loader, const char *name, if (fd == -1 && (__builtin_expect (! (mode & __RTLD_SECURE), 1) || ! INTUSE(__libc_enable_secure)) - && __builtin_expect (GLRO(dl_inhibit_cache) == 0, 1)) + && __glibc_likely (GLRO(dl_inhibit_cache) == 0)) { /* Check the list of libraries in the file /etc/ld.so.cache, for compatibility with Linux's ldconfig program. */ @@ -2254,7 +2252,7 @@ _dl_map_object (struct link_map *loader, const char *name, /* If the loader has the DF_1_NODEFLIB flag set we must not use a cache entry from any of these directories. */ - if (__builtin_expect (l->l_flags_1 & DF_1_NODEFLIB, 0)) + if (__glibc_unlikely (l->l_flags_1 & DF_1_NODEFLIB)) { const char *dirp = system_dirs; unsigned int cnt = 0; @@ -2297,7 +2295,7 @@ _dl_map_object (struct link_map *loader, const char *name, /* Finally, try the default path. */ if (fd == -1 && ((l = loader ?: GL(dl_ns)[nsid]._ns_loaded) == NULL - || __builtin_expect (!(l->l_flags_1 & DF_1_NODEFLIB), 1)) + || __glibc_likely (!(l->l_flags_1 & DF_1_NODEFLIB))) && rtld_search_dirs.dirs != (void *) -1) fd = open_path (name, namelen, mode, &rtld_search_dirs, &realname, &fb, l, LA_SER_DEFAULT, &found_other_class); @@ -2319,7 +2317,7 @@ _dl_map_object (struct link_map *loader, const char *name, fd = open_verify (realname, &fb, loader ?: GL(dl_ns)[nsid]._ns_loaded, 0, mode, &found_other_class, true); - if (__builtin_expect (fd, 0) == -1) + if (__glibc_unlikely (fd == -1)) free (realname); } } @@ -2333,10 +2331,10 @@ _dl_map_object (struct link_map *loader, const char *name, if (mode & __RTLD_CALLMAP) loader = NULL; - if (__builtin_expect (fd, 0) == -1) + if (__glibc_unlikely (fd == -1)) { if (trace_mode - && __builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK, 0) == 0) + && __glibc_likely ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK) == 0)) { /* We haven't found an appropriate library. But since we are only interested in the list of libraries this isn't