]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libelf: Use posix_memalign instead of aligned_alloc.
authorMark Wielaard <mark@klomp.org>
Thu, 7 Mar 2019 16:31:53 +0000 (17:31 +0100)
committerMark Wielaard <mark@klomp.org>
Thu, 7 Mar 2019 16:36:37 +0000 (17:36 +0100)
Older glibc might not have aligned_alloc (it is C11).
Use posix_memalign instead. posix_memalign requires the alignment to
be a multiple of sizeof (void *). So use malloc for smaller alignments.

Signed-off-by: Mark Wielaard <mark@klomp.org>
libelf/ChangeLog
libelf/elf32_updatefile.c

index fd908e280c678c473fd80729497e7c9b1e873bb4..d9b7749b4cb328f0d672d7c18524821c20b34ed0 100644 (file)
@@ -1,3 +1,8 @@
+2019-03-07  Mark Wielaard  <mark@klomp.org>
+
+       * elf32_updatefile.c (updatemmap): Use posix_memalign instead of
+       aligned_alloc.
+
 2019-03-06  Mark Wielaard  <mark@klomp.org>
 
        * elf32_updatefile.c (updatemmap): Free scns before returning
index 457d18e6a9f4b0ae7a6cbe36042fcb4e699cc4e6..eea51a7f21b78077c62039643d3f43ce88796893 100644 (file)
@@ -360,16 +360,30 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf, int change_bo, size_t shnum)
                        else
                          {
                            /* We have to do the conversion on properly
-                              aligned memory first.  */
+                              aligned memory first.  align is a power of 2,
+                              but posix_memalign only works for alignments
+                              which are a multiple of sizeof (void *).
+                              So use normal malloc for smaller alignments.  */
                            size_t size = dl->data.d.d_size;
-                           char *converted = aligned_alloc (align, size);
+                           void *converted;
+                           if (align < sizeof (void *))
+                             converted = malloc (size);
+                           else
+                             {
+                               int res;
+                               res = posix_memalign (&converted, align, size);
+                               if (res != 0)
+                                 converted = NULL;
+                             }
+
                            if (converted == NULL)
                              {
                                free (scns);
                                __libelf_seterrno (ELF_E_NOMEM);
                                return 1;
                              }
-                            (*fctp) (converted, dl->data.d.d_buf, size, 1);
+
+                           (*fctp) (converted, dl->data.d.d_buf, size, 1);
 
                            /* And then write it to the mmapped file.  */
                            memcpy (last_position, converted, size);