mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: Reviving planned ldso changes
Date: Wed, 4 Jan 2017 01:06:40 -0500	[thread overview]
Message-ID: <20170104060640.GM1555@brightrain.aerifal.cx> (raw)
In-Reply-To: <20170103054351.GA8459@brightrain.aerifal.cx>

[-- Attachment #1: Type: text/plain, Size: 834 bytes --]

On Tue, Jan 03, 2017 at 12:43:51AM -0500, Rich Felker wrote:
> - Dependency-order ctor execution. I think the walk order looks
>   something like, starting at a given node (initially the main app or
>   new library being loaded dynamically), traversing to its first
>   deps[] entry that hasn't been constructed, or, if none remain,
>   executing its ctors then traversing back to its needed_by. This
>   process avoids the need for any call recursion and should be
>   near-optimal (if not optimal) provided the position in a dso's
>   deps[] list is saved in the dso struct.

Attached is a proposed, completely untested patch to implement
dependency order loading, posted for review/comments. In order to
work, I think it also needs p->deps to be setup unconditionally (right
now it's only setup for dynamically loaded libs).

Rich

[-- Attachment #2: ctor_dep_order.diff --]
[-- Type: text/plain, Size: 1540 bytes --]

diff --git a/ldso/dynlink.c b/ldso/dynlink.c
index c689084..cb82b2c 100644
--- a/ldso/dynlink.c
+++ b/ldso/dynlink.c
@@ -67,6 +67,7 @@ struct dso {
 	char constructed;
 	char kernel_mapped;
 	struct dso **deps, *needed_by;
+	size_t next_dep;
 	char *rpath_orig, *rpath;
 	struct tls_module tls;
 	size_t tls_id;
@@ -1211,13 +1212,14 @@ void __libc_exit_fini()
 static void do_init_fini(struct dso *p)
 {
 	size_t dyn[DYN_CNT];
-	int need_locking = libc.threads_minus_1;
-	/* Allow recursive calls that arise when a library calls
-	 * dlopen from one of its constructors, but block any
-	 * other threads until all ctors have finished. */
-	if (need_locking) pthread_mutex_lock(&init_fini_lock);
-	for (; p; p=p->prev) {
-		if (p->constructed) continue;
+	pthread_mutex_lock(&init_fini_lock);
+	while (!p->constructed) {
+		while (p->deps[p->next_dep] && p->deps[p->next_dep]->constructed)
+			p->next_dep++;
+		if (p->deps[p->next_dep]) {
+			p = p->deps[p->next_dep++];
+			continue;
+		}
 		p->constructed = 1;
 		decode_vec(p->dynv, dyn, DYN_CNT);
 		if (dyn[0] & ((1<<DT_FINI) | (1<<DT_FINI_ARRAY))) {
@@ -1233,12 +1235,9 @@ static void do_init_fini(struct dso *p)
 			size_t *fn = laddr(p, dyn[DT_INIT_ARRAY]);
 			while (n--) ((void (*)(void))*fn++)();
 		}
-		if (!need_locking && libc.threads_minus_1) {
-			need_locking = 1;
-			pthread_mutex_lock(&init_fini_lock);
-		}
+		p = p->needed_by;
 	}
-	if (need_locking) pthread_mutex_unlock(&init_fini_lock);
+	pthread_mutex_unlock(&init_fini_lock);
 }
 
 void __libc_start_init(void)

  reply	other threads:[~2017-01-04  6:06 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-03  5:43 Rich Felker
2017-01-04  6:06 ` Rich Felker [this message]
2017-01-04  6:22   ` Rich Felker
2017-01-04 19:36     ` Rich Felker
2017-01-14 21:30       ` A. Wilcox
2017-01-15 17:44         ` Rich Felker
2017-02-26  1:04           ` Szabolcs Nagy
2017-02-26  1:39             ` Rich Felker
2017-02-26 10:28               ` Szabolcs Nagy
2017-02-26 15:20                 ` Rich Felker
2017-02-26 15:34                   ` Szabolcs Nagy
2017-02-26 21:39                     ` Rich Felker
2017-03-03  1:30                       ` Rich Felker
2017-03-04 10:58                         ` Szabolcs Nagy
2017-03-06  1:11                           ` Rich Felker
2017-03-07 22:02                             ` Rich Felker
2017-03-08 18:55                               ` Rich Felker
2017-03-06 16:25                         ` Rich Felker
2017-01-04 10:51 ` Szabolcs Nagy
2017-02-16  1:58   ` Szabolcs Nagy
2017-02-16  2:39     ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170104060640.GM1555@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).