0005-elf2flt-fix-fatal-error-regression-on-m68k-xtensa-ri.patch 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. From 65ac5f9e69cfb989d970da74c41e478774d29be5 Mon Sep 17 00:00:00 2001
  2. From: Niklas Cassel <niklas.cassel@wdc.com>
  3. Date: Tue, 9 Aug 2022 21:06:05 +0200
  4. Subject: [PATCH] elf2flt: fix fatal error regression on m68k, xtensa,
  5. riscv64
  6. Commit ba379d08bb78 ("elf2flt: fix for segfault on some ARM ELFs")
  7. changed the condition of which input sections that should be included
  8. in the .text output section from:
  9. ((a->flags & (SEC_DATA | SEC_READONLY)) == (SEC_DATA | SEC_READONLY))
  10. to:
  11. ((a->flags & (SEC_DATA | SEC_READONLY | SEC_RELOC)) ==
  12. (SEC_DATA | SEC_READONLY | SEC_RELOC))
  13. On ARM, the .eh_frame input section does not have the SEC_RELOC flag
  14. set, so this specific change had no effect on ARM.
  15. However, on e.g. m68k and riscv64, the .eh_frame input section does
  16. have the SEC_RELOC flag set, which means that after commit ba379d08bb78
  17. ("elf2flt: fix for segfault on some ARM ELFs"), read-only relocation
  18. data sections were placed in .text output section, instead of .data
  19. output section.
  20. This will result in a fatal error on m68k, xtensa and riscv64:
  21. ERROR: text=0x3bab8 overlaps data=0x33f60 ?
  22. This is because elf2flt cannot append to .text after .data has been
  23. appended to.
  24. Note that the binutils maintainer says that the correct thing is
  25. to put read-only relocation data sections in .text:
  26. https://sourceware.org/legacy-ml/binutils/2019-10/msg00132.html
  27. So the proper fix is probably to rewrite elf2flt so that it can append
  28. to .text after .data has been appended to (which will require elf2flt
  29. to move/relocate everything that has already been appended to .data,
  30. since the virtual addresses are contiguous).
  31. However, for now, add an exception for m68k, xtensa and riscv64
  32. (specifically for the problematic input section, .eh_frame), so that we
  33. get the same behavior as older elf2flt releases, where we put read-only
  34. relocation data in .data, which was working perfectly fine.
  35. Signed-off-by: Niklas Cassel <niklas.cassel@wdc.com>
  36. ---
  37. elf2flt.c | 11 +++++++++--
  38. 1 file changed, 9 insertions(+), 2 deletions(-)
  39. diff --git a/elf2flt.c b/elf2flt.c
  40. index 9c32f9a..a680c89 100644
  41. --- a/elf2flt.c
  42. +++ b/elf2flt.c
  43. @@ -340,8 +340,15 @@ compare_relocs (const void *pa, const void *pb)
  44. static bool
  45. ro_reloc_data_section_should_be_in_text(asection *s)
  46. {
  47. - return (s->flags & (SEC_DATA | SEC_READONLY | SEC_RELOC)) ==
  48. - (SEC_DATA | SEC_READONLY | SEC_RELOC);
  49. + if ((s->flags & (SEC_DATA | SEC_READONLY | SEC_RELOC)) ==
  50. + (SEC_DATA | SEC_READONLY | SEC_RELOC)) {
  51. +#if defined(TARGET_m68k) || defined(TARGET_riscv64) || defined(TARGET_xtensa)
  52. + if (!strcmp(".eh_frame", s->name))
  53. + return false;
  54. +#endif
  55. + return true;
  56. + }
  57. + return false;
  58. }
  59. static uint32_t *
  60. --
  61. 2.37.1