0002-elf2flt.ld-reinstate-32-byte-alignment-for-.data-sec.patch 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. From 85ba5664eb368eb1cbd2c30b7cd574acd75edd4c Mon Sep 17 00:00:00 2001
  2. From: Niklas Cassel <niklas.cassel@wdc.com>
  3. Date: Mon, 4 Apr 2022 15:30:24 +0200
  4. Subject: [PATCH] elf2flt.ld: reinstate 32 byte alignment for .data section
  5. Commit 8a3e74446fe7 ("allow to build arm flat binaries") moved the
  6. following commands:
  7. . = ALIGN(0x20) ;
  8. @SYMBOL_PREFIX@_etext = . ;
  9. from the .text section to the top level in the SECTIONS node.
  10. The .text output section is being directed to a memory region using the
  11. "> flatmem :text" output section attribute. Commands in the top level in
  12. the SECTIONS node are not.
  13. This means that the ALIGN() command is no longer being appended to the
  14. flatmem memory region, it will simply update the Location Counter.
  15. The heuristic for placing an output section is described here:
  16. https://sourceware.org/binutils/docs-2.38/ld.html#Output-Section-Address
  17. "If an output memory region is set for the section then it is added to this
  18. region and its address will be the next free address in that region."
  19. Since the .data section is being directed to the same memory region as the
  20. .text section, this means that the Location Counter is not used when
  21. assigning an address to the .data output section, it will simply use the
  22. next free address.
  23. No longer directing these commands to the flatmem memory region means that
  24. the .data output section is no longer aligned to a 32 byte boundary.
  25. Before commit 8a3e74446fe7 ("allow to build arm flat binaries"):
  26. $ readelf -S busybox_unstripped.gdb | grep data
  27. [ 3] .data PROGBITS 0000000000035ac0 00036ac0
  28. $ readelf -s busybox_unstripped.gdb | grep _etext
  29. 19286: 0000000000035ac0 0 NOTYPE GLOBAL DEFAULT 1 _etext
  30. After commit 8a3e74446fe7 ("allow to build arm flat binaries"):
  31. $ readelf -S busybox_unstripped.gdb | grep data
  32. [ 3] .data PROGBITS 0000000000035ab0 00036ab0
  33. $ readelf -s busybox_unstripped.gdb | grep _etext
  34. 19287: 0000000000035ac0 0 NOTYPE GLOBAL DEFAULT 3 _etext
  35. The .data output section has to be aligned to a 32 byte boundary, see the
  36. FLAT_DATA_ALIGN 0x20 macro and its usage in fs/binfmt_flat.c:
  37. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/binfmt_flat.c?h=v5.17#n59
  38. Readd an explicit ALIGN attribute on the .data section itself, since the
  39. linker will obey this attribute regardless if being directed to a memory
  40. region or not. Also remove the ALIGN() command before the .data section,
  41. since this misleads the reader to think that the Location Counter is used
  42. when assigning an address to the .data section, when it actually is not.
  43. Fixes: 8a3e74446fe7 ("allow to build arm flat binaries")
  44. Signed-off-by: Niklas Cassel <niklas.cassel@wdc.com>
  45. ---
  46. elf2flt.ld.in | 5 +----
  47. 1 file changed, 1 insertion(+), 4 deletions(-)
  48. diff --git a/elf2flt.ld.in b/elf2flt.ld.in
  49. index 0df999d..e5aea14 100644
  50. --- a/elf2flt.ld.in
  51. +++ b/elf2flt.ld.in
  52. @@ -94,12 +94,9 @@ W_RODAT: *(.gnu.linkonce.r*)
  53. *(.ARM.exidx* .gnu.linkonce.armexidx.*)
  54. } > flatmem
  55. @SYMBOL_PREFIX@__exidx_end = .;
  56. -
  57. - . = ALIGN(0x20) ;
  58. @SYMBOL_PREFIX@_etext = . ;
  59. - .data : {
  60. - . = ALIGN(0x4) ;
  61. + .data ALIGN(0x20): {
  62. @SYMBOL_PREFIX@_sdata = . ;
  63. @SYMBOL_PREFIX@__data_start = . ;
  64. @SYMBOL_PREFIX@data_start = . ;
  65. --
  66. 2.35.1