On Thu, Aug 15, 2024 at 10:51:29AM +0100, Mate Kukri wrote:
> Currently we load module sections at whatever alignment gcc+ld happened
> to dump into the ELF section header, which is often less then the page
> size. Since NX protections are page based, this alignment must be
> rounded up to page size on platforms supporting NX protections.
>
> This patch switches most EFI platforms to load module sections at 4kB
> page-aligned addresses.  To do so, it adds an new per-arch function,
> grub_arch_dl_min_alignment(), which returns the alignment needed for
> dynamically loaded sections (in bytes).  Currently it sets it to 4096
> when GRUB_MACHINE_EFI is true on x86_64, i386, arm, arm64, and emu, and
> 1-byte alignment on everything else.
>
> It then changes the allocation size computation and the loader code in
> grub_dl_load_segments() to align the locations and sizes up to these
> boundaries, and fills any added padding with zeros.
>
> All of this happens before relocations are applied, so the relocations
> factor that in with no change.
>
> Signed-off-by: Peter Jones <pjo...@redhat.com>
> Signed-off-by: Laszlo Ersek <ler...@redhat.com>
> Signed-off-by: Jan Setje-Eilers <jan.setjeeil...@oracle.com>
> Signed-off-by: Mate Kukri <mate.ku...@canonical.com>
> ---
>  docs/grub-dev.texi          |  6 ++---
>  grub-core/kern/arm/dl.c     | 14 ++++++++++
>  grub-core/kern/arm64/dl.c   | 14 ++++++++++
>  grub-core/kern/dl.c         | 52 +++++++++++++++++++++++++------------
>  grub-core/kern/emu/full.c   | 14 ++++++++++
>  grub-core/kern/i386/dl.c    | 14 ++++++++++
>  grub-core/kern/ia64/dl.c    |  9 +++++++
>  grub-core/kern/mips/dl.c    |  8 ++++++
>  grub-core/kern/powerpc/dl.c |  9 +++++++
>  grub-core/kern/riscv/dl.c   | 14 ++++++++++
>  grub-core/kern/sparc64/dl.c |  9 +++++++
>  grub-core/kern/x86_64/dl.c  | 14 ++++++++++
>  include/grub/dl.h           |  4 +++
>  include/grub/efi/memory.h   |  2 ++
>  14 files changed, 164 insertions(+), 19 deletions(-)
>
> diff --git a/docs/grub-dev.texi b/docs/grub-dev.texi
> index 1276c5930..2f782cda5 100644
> --- a/docs/grub-dev.texi
> +++ b/docs/grub-dev.texi
> @@ -996,9 +996,9 @@ declare startup asm file ($cpu_$platform_startup) as well 
> as any other files
>  (e.g. init.c and callwrap.S) (e.g. $cpu_$platform = 
> kern/$cpu/$platform/init.c).
>  At this stage you will also need to add dummy dl.c and cache.S with functions
>  grub_err_t grub_arch_dl_check_header (void *ehdr), grub_err_t
> -grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr) (dl.c) and
> -void grub_arch_sync_caches (void *address, grub_size_t len) (cache.S). They
> -won't be used for now.
> +grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr) (dl.c), 
> grub_uint32_t
> +grub_arch_dl_min_alignment (void), and void grub_arch_sync_caches (void
> +*address, grub_size_t len) (cache.S). They won't be used for now.

I would put full function names in one line instead of breaking them in
random places. However, if you make suggested change below then you do not
need to update the docs...

[...]

> diff --git a/include/grub/dl.h b/include/grub/dl.h
> index 750fc8d3d..beb20cb7e 100644
> --- a/include/grub/dl.h
> +++ b/include/grub/dl.h
> @@ -264,6 +264,8 @@ grub_err_t grub_arch_dl_check_header (void *ehdr);
>  grub_err_t
>  grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
>                              Elf_Shdr *s, grub_dl_segment_t seg);
> +grub_size_t
> +grub_arch_dl_min_alignment (void);

You can drop this function and simplify this patch if you do...

>  #endif
>
>  #if defined (_mips)
> @@ -308,4 +310,6 @@ grub_arch_dl_get_tramp_got_size (const void *ehdr, 
> grub_size_t *tramp,
>
>  #endif
>
> +#define GRUB_DEFAULT_DL_ALIGN 1

#ifdef GRUB_MACHINE_EFI
#define GRUB_DL_ALIGN   GRUB_EFI_PAGE_SIZE
#else
#define GRUB_DL_ALIGN   1
#endif

... then use GRUB_DL_ALIGN instead of grub_arch_dl_min_alignment()...

Of course you need at the beginning of this file:

  #ifdef GRUB_MACHINE_EFI
  #include <grub/efi/memory.h>
  #endif

>  #endif /* ! GRUB_DL_H */
> diff --git a/include/grub/efi/memory.h b/include/grub/efi/memory.h
> index 08fe62277..3f35500b2 100644
> --- a/include/grub/efi/memory.h
> +++ b/include/grub/efi/memory.h
> @@ -31,6 +31,8 @@
>
>  #define GRUB_MMAP_REGISTER_BY_FIRMWARE  1
>
> +#define GRUB_EFI_DL_ALIGN            GRUB_EFI_PAGE_SIZE
> +
>  grub_err_t grub_machine_mmap_register (grub_uint64_t start, grub_uint64_t 
> size,
>                                      int type, int handle);
>  grub_err_t grub_machine_mmap_unregister (int handle);

Daniel

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to