The branch main has been updated by kib:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=feaae6ba1ace0091384ac371423976cd15e59e5a

commit feaae6ba1ace0091384ac371423976cd15e59e5a
Author:     Konstantin Belousov <[email protected]>
AuthorDate: 2023-07-30 01:51:52 +0000
Commit:     Konstantin Belousov <[email protected]>
CommitDate: 2023-08-21 14:16:42 +0000

    rtld: switch from malloc_aligned() to __crt_aligned_alloc()
    
    Use regular free(), since it works now.
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D41150
---
 libexec/rtld-elf/rtld.c    | 14 +++++++-------
 libexec/rtld-elf/rtld.h    |  3 +--
 libexec/rtld-elf/xmalloc.c | 30 +++++++-----------------------
 3 files changed, 15 insertions(+), 32 deletions(-)

diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c
index feef5dd0bb5d..aa7e6020a085 100644
--- a/libexec/rtld-elf/rtld.c
+++ b/libexec/rtld-elf/rtld.c
@@ -5254,13 +5254,13 @@ allocate_tls(Obj_Entry *objs, void *oldtcb, size_t 
tcbsize, size_t tcbalign)
     tls_block_size += pre_size + tls_static_space - TLS_TCB_SIZE - post_size;
 
     /* Allocate whole TLS block */
-    tls_block = malloc_aligned(tls_block_size, maxalign, 0);
+    tls_block = xmalloc_aligned(tls_block_size, maxalign, 0);
     tcb = (Elf_Addr **)(tls_block + pre_size + extra_size);
 
     if (oldtcb != NULL) {
        memcpy(tls_block, get_tls_block_ptr(oldtcb, tcbsize),
            tls_static_space);
-       free_aligned(get_tls_block_ptr(oldtcb, tcbsize));
+       free(get_tls_block_ptr(oldtcb, tcbsize));
 
        /* Adjust the DTV. */
        dtv = tcb[0];
@@ -5324,7 +5324,7 @@ free_tls(void *tcb, size_t tcbsize, size_t tcbalign 
__unused)
        }
     }
     free(dtv);
-    free_aligned(get_tls_block_ptr(tcb, tcbsize));
+    free(get_tls_block_ptr(tcb, tcbsize));
 }
 
 #endif /* TLS_VARIANT_I */
@@ -5350,7 +5350,7 @@ allocate_tls(Obj_Entry *objs, void *oldtls, size_t 
tcbsize, size_t tcbalign)
     size = roundup(tls_static_space, ralign) + roundup(tcbsize, ralign);
 
     assert(tcbsize >= 2*sizeof(Elf_Addr));
-    tls = malloc_aligned(size, ralign, 0 /* XXX */);
+    tls = xmalloc_aligned(size, ralign, 0 /* XXX */);
     dtv = xcalloc(tls_max_index + 2, sizeof(Elf_Addr));
 
     segbase = (Elf_Addr)(tls + roundup(tls_static_space, ralign));
@@ -5429,11 +5429,11 @@ free_tls(void *tls, size_t tcbsize  __unused, size_t 
tcbalign)
     for (i = 0; i < dtvsize; i++) {
            if (dtv[i + 2] != 0 && (dtv[i + 2] < tlsstart ||
                dtv[i + 2] > tlsend)) {
-                   free_aligned((void *)dtv[i + 2]);
+                   free((void *)dtv[i + 2]);
        }
     }
 
-    free_aligned((void *)tlsstart);
+    free((void *)tlsstart);
     free((void *)dtv);
 }
 
@@ -5470,7 +5470,7 @@ allocate_module_tls(int index)
 
        obj->tls_dynamic = true;
 
-       p = malloc_aligned(obj->tlssize, obj->tlsalign, obj->tlspoffset);
+       p = xmalloc_aligned(obj->tlssize, obj->tlsalign, obj->tlspoffset);
        memcpy(p, obj->tlsinit, obj->tlsinitsize);
        memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize);
        return (p);
diff --git a/libexec/rtld-elf/rtld.h b/libexec/rtld-elf/rtld.h
index 45cb722097b2..7fe9d837d6f1 100644
--- a/libexec/rtld-elf/rtld.h
+++ b/libexec/rtld-elf/rtld.h
@@ -361,8 +361,7 @@ Obj_Entry *map_object(int, const char *, const struct stat 
*);
 void *xcalloc(size_t, size_t);
 void *xmalloc(size_t);
 char *xstrdup(const char *);
-void *malloc_aligned(size_t size, size_t align, size_t offset);
-void free_aligned(void *ptr);
+void *xmalloc_aligned(size_t size, size_t align, size_t offset);
 extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
 extern Elf_Sym sym_zero;       /* For resolving undefined weak refs. */
 extern bool ld_bind_not;
diff --git a/libexec/rtld-elf/xmalloc.c b/libexec/rtld-elf/xmalloc.c
index 80246f36dd26..5f7c1b5ba10a 100644
--- a/libexec/rtld-elf/xmalloc.c
+++ b/libexec/rtld-elf/xmalloc.c
@@ -75,34 +75,18 @@ xstrdup(const char *str)
 }
 
 void *
-malloc_aligned(size_t size, size_t align, size_t offset)
+xmalloc_aligned(size_t size, size_t align, size_t offset)
 {
-       char *mem, *res;
-       uintptr_t x;
+       void *res;
 
        offset &= align - 1;
        if (align < sizeof(void *))
                align = sizeof(void *);
 
-       mem = xmalloc(size + 3 * align + offset);
-       x = roundup((uintptr_t)mem + sizeof(void *), align);
-       x += offset;
-       res = (void *)x;
-       x -= sizeof(void *);
-       memcpy((void *)x, &mem, sizeof(mem));
+       res = __crt_aligned_alloc_offset(align, size, offset);
+       if (res == NULL) {
+               rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
+               _exit(1);
+       }
        return (res);
 }
-
-void
-free_aligned(void *ptr)
-{
-       void *mem;
-       uintptr_t x;
-
-       if (ptr == NULL)
-               return;
-       x = (uintptr_t)ptr;
-       x -= sizeof(void *);
-       memcpy(&mem, (void *)x, sizeof(mem));
-       free(mem);
-}

Reply via email to