public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] libelf: Use posix_memalign instead of aligned_alloc.
@ 2019-03-07 16:35 Mark Wielaard
  2019-03-13 12:55 ` Mark Wielaard
  0 siblings, 1 reply; 2+ messages in thread
From: Mark Wielaard @ 2019-03-07 16:35 UTC (permalink / raw)
  To: elfutils-devel; +Cc: Mark Wielaard

Older glibc might not 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          |  5 +++++
 libelf/elf32_updatefile.c | 20 +++++++++++++++++---
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index fd908e2..d9b7749 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 457d18e..eea51a7 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);
-- 
1.8.3.1

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] libelf: Use posix_memalign instead of aligned_alloc.
  2019-03-07 16:35 [PATCH] libelf: Use posix_memalign instead of aligned_alloc Mark Wielaard
@ 2019-03-13 12:55 ` Mark Wielaard
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Wielaard @ 2019-03-13 12:55 UTC (permalink / raw)
  To: elfutils-devel

On Thu, 2019-03-07 at 17:35 +0100, Mark Wielaard wrote:
> 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.

Pushed to master.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-03-13 12:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-07 16:35 [PATCH] libelf: Use posix_memalign instead of aligned_alloc Mark Wielaard
2019-03-13 12:55 ` Mark Wielaard

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 read-only IMAP folder(s) and NNTP newsgroup(s).