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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. From a8c9f650b82109abf7aa730f298ea5182ed62613 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, riscv64
  5. Commit ba379d08bb78 ("elf2flt: fix for segfault on some ARM ELFs")
  6. changed the condition of which input sections that should be included
  7. in the .text output section from:
  8. ((a->flags & (SEC_DATA | SEC_READONLY)) == (SEC_DATA | SEC_READONLY))
  9. to:
  10. ((a->flags & (SEC_DATA | SEC_READONLY | SEC_RELOC)) ==
  11. (SEC_DATA | SEC_READONLY | SEC_RELOC))
  12. On ARM, the .eh_frame input section does not have the SEC_RELOC flag set,
  13. so on ARM, this change caused .eh_frame to move from .text to .data.
  14. However, on e.g. m68k, xtensa and riscv64, the .eh_frame input section
  15. does have the SEC_RELOC flag set, which means that the change in
  16. commit ba379d08bb78 ("elf2flt: fix for segfault on some ARM ELFs")
  17. caused .eh_frame to move in an opposite way, i.e. from .data to .text.
  18. This resulted in a fatal error on m68k, xtensa and riscv64:
  19. ERROR: text=0x3bab8 overlaps data=0x33f60 ?
  20. This is because elf2flt cannot append to .text after .data has been
  21. appended to.
  22. Note that the binutils maintainer says that the correct thing is
  23. to put read-only relocation data sections in .text:
  24. https://sourceware.org/legacy-ml/binutils/2019-10/msg00132.html
  25. So the proper fix is probably to rewrite elf2flt so that it can append
  26. to .text after .data has been appended to (which will require elf2flt
  27. to move/relocate everything that has already been appended to .data,
  28. since the virtual addresses are contiguous).
  29. However, for now, add an exception for input sections which have all
  30. three flags SEC_DATA, SEC_READONLY, and SEC_RELOC set, and which have a
  31. name equal to a problematic input section (.eh_frame, .gcc_except_table).
  32. That way, we get the same behavior as older elf2flt releases for m68k,
  33. xtensa and riscv64, where we put read-only relocation data in .data,
  34. which was working perfectly fine.
  35. This exception will not change any behavior on ARM, as the .eh_frame
  36. input section does not have flag SEC_RELOC set.
  37. Signed-off-by: Niklas Cassel <niklas.cassel@wdc.com>
  38. ---
  39. elf2flt.c | 9 +++++++--
  40. 1 file changed, 7 insertions(+), 2 deletions(-)
  41. diff --git a/elf2flt.c b/elf2flt.c
  42. index e0d7891..39d035f 100644
  43. --- a/elf2flt.c
  44. +++ b/elf2flt.c
  45. @@ -341,8 +341,13 @@ compare_relocs (const void *pa, const void *pb)
  46. static bool
  47. ro_reloc_data_section_should_be_in_text(asection *s)
  48. {
  49. - return (s->flags & (SEC_DATA | SEC_READONLY | SEC_RELOC)) ==
  50. - (SEC_DATA | SEC_READONLY | SEC_RELOC);
  51. + if ((s->flags & (SEC_DATA | SEC_READONLY | SEC_RELOC)) ==
  52. + (SEC_DATA | SEC_READONLY | SEC_RELOC)) {
  53. + if (!strcmp(".eh_frame", s->name) || !strcmp(".gcc_except_table", s->name))
  54. + return false;
  55. + return true;
  56. + }
  57. + return false;
  58. }
  59. static uint32_t *
  60. --
  61. 2.39.0