0004-libelf-Use-posix_memalign-instead-of-aligned_alloc.patch 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. From 6bd060a23f43a842fbc37dd1bb8d6d7964eda36e Mon Sep 17 00:00:00 2001
  2. From: Mark Wielaard <mark@klomp.org>
  3. Date: Thu, 7 Mar 2019 17:31:53 +0100
  4. Subject: [PATCH] libelf: Use posix_memalign instead of aligned_alloc.
  5. Older glibc might not have aligned_alloc (it is C11).
  6. Use posix_memalign instead. posix_memalign requires the alignment to
  7. be a multiple of sizeof (void *). So use malloc for smaller alignments.
  8. Signed-off-by: Mark Wielaard <mark@klomp.org>
  9. [Retrieved (and slighlty updated to remove ChangeLog update) from:
  10. https://sourceware.org/git/?p=elfutils.git;a=patch;h=6bd060a23f43a842fbc37dd1bb8d6d7964eda36e]
  11. Signed-off-by: Fabrice Fontaine <fontaine.fabrice@gmail.com>
  12. ---
  13. libelf/elf32_updatefile.c | 20 +++++++++++++++++---
  14. 2 files changed, 22 insertions(+), 3 deletions(-)
  15. diff --git a/libelf/elf32_updatefile.c b/libelf/elf32_updatefile.c
  16. index 457d18e..eea51a7 100644
  17. --- a/libelf/elf32_updatefile.c
  18. +++ b/libelf/elf32_updatefile.c
  19. @@ -360,16 +360,30 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf, int change_bo, size_t shnum)
  20. else
  21. {
  22. /* We have to do the conversion on properly
  23. - aligned memory first. */
  24. + aligned memory first. align is a power of 2,
  25. + but posix_memalign only works for alignments
  26. + which are a multiple of sizeof (void *).
  27. + So use normal malloc for smaller alignments. */
  28. size_t size = dl->data.d.d_size;
  29. - char *converted = aligned_alloc (align, size);
  30. + void *converted;
  31. + if (align < sizeof (void *))
  32. + converted = malloc (size);
  33. + else
  34. + {
  35. + int res;
  36. + res = posix_memalign (&converted, align, size);
  37. + if (res != 0)
  38. + converted = NULL;
  39. + }
  40. +
  41. if (converted == NULL)
  42. {
  43. free (scns);
  44. __libelf_seterrno (ELF_E_NOMEM);
  45. return 1;
  46. }
  47. - (*fctp) (converted, dl->data.d.d_buf, size, 1);
  48. +
  49. + (*fctp) (converted, dl->data.d.d_buf, size, 1);
  50. /* And then write it to the mmapped file. */
  51. memcpy (last_position, converted, size);
  52. --
  53. 2.9.3