summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Wielaard <mark@klomp.org>2019-03-07 17:31:53 +0100
committerMark Wielaard <mark@klomp.org>2019-03-07 17:36:37 +0100
commit6bd060a23f43a842fbc37dd1bb8d6d7964eda36e (patch)
tree0102fcecca8c1d9adcccf2647d5f72bbeeeeabb8
parentff063240ac97302f289b12ac7dff655cdc0d5d70 (diff)
downloadelfutils-6bd060a23f43a842fbc37dd1bb8d6d7964eda36e.tar.gz
libelf: Use posix_memalign instead of aligned_alloc.
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>
-rw-r--r--libelf/ChangeLog5
-rw-r--r--libelf/elf32_updatefile.c20
2 files changed, 22 insertions, 3 deletions
diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index fd908e28..d9b7749b 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -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
diff --git a/libelf/elf32_updatefile.c b/libelf/elf32_updatefile.c
index 457d18e6..eea51a7f 100644
--- a/libelf/elf32_updatefile.c
+++ b/libelf/elf32_updatefile.c
@@ -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);