mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Ben Noordhuis <info@bnoordhuis.nl>
To: musl@lists.openwall.com
Cc: Ben Noordhuis <info@bnoordhuis.nl>
Subject: [musl] [PATCH] ldso: add basic ifunc support
Date: Tue, 23 Aug 2022 20:41:57 +0200	[thread overview]
Message-ID: <20220823184157.42134-1-info@bnoordhuis.nl> (raw)

Not perfect yet because it doesn't handle resolver functions that need
relocations themselves but basic functionality works. That is, this
works:

    typedef void (*Func)(void);
    void f(void) __attribute__((ifunc("g")));
    Func g(void) { return h; }
    void h(void) { abort(); }

But returning an extern function does not:

    typedef void (*Func)(void);
    void f(void) __attribute__((ifunc("g")));
    Func g(void) { return abort; } // segfaults
---
 arch/x86_64/reloc.h    | 1 +
 ldso/dynlink.c         | 4 ++++
 src/internal/dynlink.h | 1 +
 3 files changed, 6 insertions(+)

diff --git a/arch/x86_64/reloc.h b/arch/x86_64/reloc.h
index fac0c0ae..fb6be3ea 100644
--- a/arch/x86_64/reloc.h
+++ b/arch/x86_64/reloc.h
@@ -10,6 +10,7 @@
 #define REL_DTPOFF      R_X86_64_DTPOFF64
 #define REL_TPOFF       R_X86_64_TPOFF64
 #define REL_TLSDESC     R_X86_64_TLSDESC
+#define REL_IRELATIVE   R_X86_64_IRELATIVE
 
 #define CRTJMP(pc,sp) __asm__ __volatile__( \
 	"mov %1,%%rsp ; jmp *%0" : : "r"(pc), "r"(sp) : "memory" )
diff --git a/ldso/dynlink.c b/ldso/dynlink.c
index fd09ca69..659cb113 100644
--- a/ldso/dynlink.c
+++ b/ldso/dynlink.c
@@ -507,6 +507,10 @@ static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stri
 			reloc_addr[1] = tmp;
 #endif
 			break;
+		case REL_IRELATIVE:
+			*reloc_addr = (size_t)base + addend;
+			*reloc_addr = ((size_t(*)(void))*reloc_addr)();
+			break;
 		default:
 			error("Error relocating %s: unsupported relocation type %d",
 				dso->name, type);
diff --git a/src/internal/dynlink.h b/src/internal/dynlink.h
index 830354eb..c67bc35e 100644
--- a/src/internal/dynlink.h
+++ b/src/internal/dynlink.h
@@ -41,6 +41,7 @@ enum {
 	REL_TPOFF,
 	REL_TPOFF_NEG,
 	REL_TLSDESC,
+	REL_IRELATIVE,
 	REL_FUNCDESC,
 	REL_FUNCDESC_VAL,
 };
-- 
2.34.1


             reply	other threads:[~2022-08-23 18:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-23 18:41 Ben Noordhuis [this message]
2022-08-23 20:03 ` Szabolcs Nagy
2022-08-23 20:29   ` 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=20220823184157.42134-1-info@bnoordhuis.nl \
    --to=info@bnoordhuis.nl \
    --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).