grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [GRUB2 PATCH v3 4/4] multiboot2: Add support for relocatable images


From: Daniel Kiper
Subject: Re: [GRUB2 PATCH v3 4/4] multiboot2: Add support for relocatable images
Date: Fri, 11 Mar 2016 17:06:35 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

On Thu, Mar 10, 2016 at 09:41:26PM +0100, Vladimir 'phcoder' Serbinenko wrote:
> On Wednesday, March 2, 2016, Daniel Kiper <address@hidden> wrote:
>
> > Currently multiboot2 protocol loads image exactly at address specified in
> > ELF or multiboot2 header. This solution works quite well on legacy BIOS
> > platforms. It is possible because memory regions are placed at predictable
> > addresses (though I was not able to find any spec which says that it is
> > strong requirement, so, it looks that it is just a goodwill of hardware
> > designers). However, EFI platforms are more volatile. Even if required
> > memory regions live at specific addresses then they are sometimes simply
> > not free (e.g. used by boot/runtime services on Dell PowerEdge R820 and
> > OVMF). This means that you are not able to simply set up final image
> > destination on build time. You have to provide method to relocate image
> > contents to real load address which is usually different than load address
> > specified in ELF and multiboot2 headers.
> >
> > This patch provides all needed machinery to do self relocation in image
> > code.
> > First of all GRUB2 reads min_addr (min. load addr), max_addr (max. load
> > addr),
> > align (required image alignment), preference (it says which memory regions
> > are
> > preferred by image, e.g. none, low, high) from
> > multiboot_header_tag_relocatable
> > header tag contained in binary. Later loader tries to fulfill request (not
> > only
> > that one) and if it succeeds then it informs image about real load address
> > via
> > multiboot_tag_base_addr tag. At this stage GRUB2 role is finished. Starting
> > from now executable must cope with relocations itself using whole static
> > and dynamic knowledge provided by boot loader.
> >
> > This patch does not provide functionality which could do relocations using
> > ELF relocation data. However, I was asked by Konrad Rzeszutek Wilk and
> > Vladimir
> > 'phcoder' Serbinenko to investigate that thing. It looks that relevant
> > machinery
> > could be added to existing code (including this patch) without huge effort.
> > Additionally, ELF relocation could live in parallel with self relocation
> > provided
> > by this patch. However, during research I realized that first of all we
> > should
> > establish the details how ELF relocatable image should look like and how
> > it should
> > be build. At least to build proper test/example files.
> >
> > As I saw multiboot2 protocol is able to consume ET_EXEC and ET_DYN ELF
> > files.
> > Potentially we can use ET_DYN file type. It can be build with gcc/ld -pie
> > option.
> > However, it contains a lot of unneeded stuff (e.g. INTERP, DYNAMIC,
> > GNU_EH_FRAME
> > program headers) and it could be quite difficult to drop them (Hmmm... Is
> > it
> > possible to build it properly with custom ld script?).
>
> How big are they? Are they a real problem?

ET_DYN file is ~2.5 times bigger then normal ET_EXEC (I has checked 
multiboot2.elf
from GRUB2). There is a chance that we can ignore most of stuff in ET_DYN, 
however,
it does not look nice. IMO, image should have only what is needed by loader.

> > So, I have checked ET_EXEC
> > file type. Sadly in this case linker by default resolves all local symbol
> > relocations
> > and removes relocation related sections. Fortunately it is possible to
> > leave them
> > as is with simple -q/--emit-relocs ld option. However, output file is
> > quite fragile
> > and any operation on it should be done with great care (e.g. strip should
> > be called
> > with --strip-unneeded option). So, this solution is not perfect too. It
> > means that
> > maybe we should look for better solution. However, I think that we should
> > not use
> > any custom tools and focus on functionalities provided by compiler and
> > binutils.
> > In this context ld scripts looks quite promising but maybe you have better
> > solutions.
> > So, what do you think about that?
> >
>  Another possibility is to use intermediary .o files like we do for modules
> and like Linux does for modules AFAIR.

Correct but I think that it would be better to have normal ET_EXEC or ET_DYN 
file.

> > This patch was tested with Xen image which uses that functionality.
> > However, this Xen
> > feature is still under development and new patchset will be released in
> > about 3-4 weeks.
> >
> > Signed-off-by: Daniel Kiper <address@hidden <javascript:;>>
> > ---
> > v3 - suggestions/fixes:
> >    - reduce number of casts
> >      (suggested by Konrad Rzeszutek Wilk),
> >    - remove unneeded space at the end of line
> >      (suggested by Konrad Rzeszutek Wilk),
> >    - improve commit message
> >      (suggested by Konrad Rzeszutek Wilk).
> > ---
> >  grub-core/loader/i386/multiboot_mbi.c |    6 ++-
> >  grub-core/loader/multiboot.c          |   12 ++++--
> >  grub-core/loader/multiboot_elfxx.c    |   28 ++++++++++----
> >  grub-core/loader/multiboot_mbi2.c     |   65
> > ++++++++++++++++++++++++++++++---
> >  include/grub/multiboot.h              |    4 +-
> >  include/multiboot2.h                  |   24 ++++++++++++
> >  6 files changed, 120 insertions(+), 19 deletions(-)
> >
> > diff --git a/grub-core/loader/i386/multiboot_mbi.c
> > b/grub-core/loader/i386/multiboot_mbi.c
> > index f60b702..4fc83ed 100644
> > --- a/grub-core/loader/i386/multiboot_mbi.c
> > +++ b/grub-core/loader/i386/multiboot_mbi.c
> > @@ -72,7 +72,8 @@ load_kernel (grub_file_t file, const char *filename,
> >    grub_err_t err;
> >    if (grub_multiboot_quirks & GRUB_MULTIBOOT_QUIRK_BAD_KLUDGE)
> >      {
> > -      err = grub_multiboot_load_elf (file, filename, buffer);
> > +      err = grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
> > +                                    GRUB_RELOCATOR_PREFERENCE_NONE, NULL,
> > 0);
> >        if (err == GRUB_ERR_NONE) {
> >         return GRUB_ERR_NONE;
> >        }
> > @@ -121,7 +122,8 @@ load_kernel (grub_file_t file, const char *filename,
> >        return GRUB_ERR_NONE;
> >      }
> >
> > -  return grub_multiboot_load_elf (file, filename, buffer);
> > +  return grub_multiboot_load_elf (file, filename, buffer, 0, 0, 0, 0,
> > +                                 GRUB_RELOCATOR_PREFERENCE_NONE, NULL, 0);
> >  }
> >
> >  static struct multiboot_header *
> > diff --git a/grub-core/loader/multiboot.c b/grub-core/loader/multiboot.c
> > index 18038fd..c0f51b6 100644
> > --- a/grub-core/loader/multiboot.c
> > +++ b/grub-core/loader/multiboot.c
> > @@ -208,12 +208,18 @@ static grub_uint64_t highest_load;
> >  /* Load ELF32 or ELF64.  */
> >  grub_err_t
> >  grub_multiboot_load_elf (grub_file_t file, const char *filename,
> > -                        void *buffer)
> > +                        void *buffer, int relocatable, grub_uint32_t
> > min_addr,
> > +                        grub_uint32_t max_addr, grub_size_t align,
> > grub_uint32_t preference,
> > +                        grub_uint32_t *base_addr, int
> > avoid_efi_boot_services)
> >  {
> >    if (grub_multiboot_is_elf32 (buffer))
> > -    return grub_multiboot_load_elf32 (file, filename, buffer);
> > +    return grub_multiboot_load_elf32 (file, filename, buffer, relocatable,
> > +                                     min_addr, max_addr, align,
> > preference,
> > +                                     base_addr, avoid_efi_boot_services);
> >    else if (grub_multiboot_is_elf64 (buffer))
> > -    return grub_multiboot_load_elf64 (file, filename, buffer);
> > +    return grub_multiboot_load_elf64 (file, filename, buffer, relocatable,
> > +                                     min_addr, max_addr, align,
> > preference,
> > +                                     base_addr, avoid_efi_boot_services);
> >
> >    return grub_error (GRUB_ERR_UNKNOWN_OS, N_("invalid arch-dependent ELF
> > magic"));
> >  }
> > diff --git a/grub-core/loader/multiboot_elfxx.c
> > b/grub-core/loader/multiboot_elfxx.c
> > index e3a39b6..0c01569 100644
> > --- a/grub-core/loader/multiboot_elfxx.c
> > +++ b/grub-core/loader/multiboot_elfxx.c
> > @@ -51,7 +51,10 @@ CONCAT(grub_multiboot_is_elf, XX) (void *buffer)
> >  }
> >
> >  static grub_err_t
> > -CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char
> > *filename, void *buffer)
> > +CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file, const char
> > *filename,
> > +                                    void *buffer, int relocatable,
> > grub_uint32_t min_addr,
> > +                                    grub_uint32_t max_addr, grub_size_t
> > align, grub_uint32_t preference,
> > +                                    grub_uint32_t *base_addr, int
> > avoid_efi_boot_services)
> >  {
> >    Elf_Ehdr *ehdr = (Elf_Ehdr *) buffer;
> >    char *phdr_base;
> > @@ -89,19 +92,30 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file,
> > const char *filename, voi
> >           if (phdr(i)->p_paddr + phdr(i)->p_memsz > highest_load)
> >             highest_load = phdr(i)->p_paddr + phdr(i)->p_memsz;
> >
> > -         grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx,
> > memsz=0x%lx, vaddr=0x%lx\n",
> > -                       i, (long) phdr(i)->p_paddr, (long)
> > phdr(i)->p_memsz, (long) phdr(i)->p_vaddr);
> > +         grub_dprintf ("multiboot_loader", "segment %d: paddr=0x%lx,
> > memsz=0x%lx, vaddr=0x%lx,"
> > +                       "align=0x%lx, relocatable=%d,
> > avoid_efi_boot_services=%d\n", i,
> > +                       (long) phdr(i)->p_paddr, (long) phdr(i)->p_memsz,
> > (long) phdr(i)->p_vaddr,
> > +                       (long) align, relocatable,
> > avoid_efi_boot_services);
> >
> >           {
> >             grub_relocator_chunk_t ch;
> > -           err = grub_relocator_alloc_chunk_addr
> > (grub_multiboot_relocator,
> > -                                                  &ch, phdr(i)->p_paddr,
> > -                                                  phdr(i)->p_memsz);
> > +
> > +           if (relocatable)
> > +             err = grub_relocator_alloc_chunk_align
> > (grub_multiboot_relocator, &ch,
> > +                                                     min_addr, max_addr -
> > phdr(i)->p_memsz,
> > +                                                     phdr(i)->p_memsz,
> > align ? align : 1,
> > +                                                     preference,
> > avoid_efi_boot_services);
> > +           else
> > +             err = grub_relocator_alloc_chunk_addr
> > (grub_multiboot_relocator,
> > +                                                    &ch, phdr(i)->p_paddr,
> > +                                                    phdr(i)->p_memsz);
> >             if (err)
> >               {
> >                 grub_dprintf ("multiboot_loader", "Error loading phdr
> > %d\n", i);
> >                 return err;
> >               }
> > +           if (base_addr)
> > +             *base_addr = get_physical_target_address (ch);
> >             source = get_virtual_current_address (ch);
> >           }
> >
> > @@ -208,7 +222,7 @@ CONCAT(grub_multiboot_load_elf, XX) (grub_file_t file,
> > const char *filename, voi
> >                                                     + 1, sh->sh_size,
> >                                                     sh->sh_addralign,
> >
> > GRUB_RELOCATOR_PREFERENCE_NONE,
> > -                                                   0);
> > +
> >  avoid_efi_boot_services);
> >             if (err)
> >               {
> >                 grub_dprintf ("multiboot_loader", "Error loading shdr
> > %d\n", i);
> > diff --git a/grub-core/loader/multiboot_mbi2.c
> > b/grub-core/loader/multiboot_mbi2.c
> > index ce68f48..03725a1 100644
> > --- a/grub-core/loader/multiboot_mbi2.c
> > +++ b/grub-core/loader/multiboot_mbi2.c
> > @@ -68,6 +68,7 @@ static grub_size_t elf_sec_num, elf_sec_entsize;
> >  static unsigned elf_sec_shstrndx;
> >  static void *elf_sections;
> >  static int keep_bs = 0;
> > +static grub_uint32_t base_addr = 0;
> >
> >  void
> >  grub_multiboot_add_elfsyms (grub_size_t num, grub_size_t entsize,
> > @@ -107,11 +108,14 @@ grub_multiboot_load (grub_file_t file, const char
> > *filename)
> >    grub_err_t err;
> >    struct multiboot_header_tag *tag;
> >    struct multiboot_header_tag_address *addr_tag = NULL;
> > -  int entry_specified = 0, efi_entry_specified = 0;
> > +  struct multiboot_header_tag_relocatable *rel_tag;
> > +  int entry_specified = 0, efi_entry_specified = 0, relocatable = 0;
> >    grub_addr_t entry = 0, efi_entry = 0;
> > -  grub_uint32_t console_required = 0;
> > +  grub_uint32_t console_required = 0, min_addr = 0;
> > +  grub_uint32_t max_addr = 0, preference = GRUB_RELOCATOR_PREFERENCE_NONE;
> >    struct multiboot_header_tag_framebuffer *fbtag = NULL;
> >    int accepted_consoles = GRUB_MULTIBOOT_CONSOLE_EGA_TEXT;
> > +  grub_size_t align = 0;
> >
> >    buffer = grub_malloc (MULTIBOOT_SEARCH);
> >    if (!buffer)
> > @@ -174,6 +178,7 @@ grub_multiboot_load (grub_file_t file, const char
> > *filename)
> >               case MULTIBOOT_TAG_TYPE_EFI_BS:
> >               case MULTIBOOT_TAG_TYPE_EFI32_IH:
> >               case MULTIBOOT_TAG_TYPE_EFI64_IH:
> > +             case MULTIBOOT_TAG_TYPE_BASE_ADDR:
> >                 break;
> >
> >               default:
> > @@ -215,6 +220,27 @@ grub_multiboot_load (grub_file_t file, const char
> > *filename)
> >         accepted_consoles |= GRUB_MULTIBOOT_CONSOLE_FRAMEBUFFER;
> >         break;
> >
> > +      case MULTIBOOT_HEADER_TAG_RELOCATABLE:
> > +       relocatable = 1;
> > +       rel_tag = (struct multiboot_header_tag_relocatable *) tag;
> > +       min_addr = rel_tag->min_addr;
> > +       max_addr = rel_tag->max_addr;
> > +       align = rel_tag->align;
> > +       switch (rel_tag->preference)
> > +         {
> > +         case MULTIBOOT_LOAD_PREFERENCE_LOW:
> > +           preference = GRUB_RELOCATOR_PREFERENCE_LOW;
> > +           break;
> > +
> > +         case MULTIBOOT_LOAD_PREFERENCE_HIGH:
> > +           preference = GRUB_RELOCATOR_PREFERENCE_HIGH;
> > +           break;
> > +
> > +         default:
> > +           preference = GRUB_RELOCATOR_PREFERENCE_NONE;
> > +         }
> > +       break;
> > +
> >         /* GRUB always page-aligns modules.  */
> >        case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
> >         break;
> > @@ -260,15 +286,22 @@ grub_multiboot_load (grub_file_t file, const char
> > *filename)
> >        else
> >         code_size = load_size;
> >
> > -      err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
> > -                                            &ch, load_addr,
> > -                                            code_size);
> > +      if (relocatable)
> > +       err = grub_relocator_alloc_chunk_align (grub_multiboot_relocator,
> > &ch,
> > +                                               min_addr, max_addr -
> > code_size,
> > +                                               code_size, align ? align :
> > 1,
> > +                                               preference, keep_bs);
> > +      else
> > +       err = grub_relocator_alloc_chunk_addr (grub_multiboot_relocator,
> > +                                              &ch, load_addr,
> > +                                              code_size);
> >        if (err)
> >         {
> >           grub_dprintf ("multiboot_loader", "Error loading aout kludge\n");
> >           grub_free (buffer);
> >           return err;
> >         }
> > +      base_addr = get_physical_target_address (ch);
> >        source = get_virtual_current_address (ch);
> >
> >        if ((grub_file_seek (file, offset)) == (grub_off_t) -1)
> > @@ -290,7 +323,9 @@ grub_multiboot_load (grub_file_t file, const char
> > *filename)
> >      }
> >    else
> >      {
> > -      err = grub_multiboot_load_elf (file, filename, buffer);
> > +      err = grub_multiboot_load_elf (file, filename, buffer,
> > +                                    relocatable, min_addr, max_addr,
> > +                                    align, preference, &base_addr,
> > keep_bs);
> >        if (err)
> >         {
> >           grub_free (buffer);
> > @@ -303,6 +338,14 @@ grub_multiboot_load (grub_file_t file, const char
> > *filename)
> >    else if (entry_specified)
> >      grub_multiboot_payload_eip = entry;
> >
> > +  if (relocatable)
> > +    {
> > +      if (base_addr > min_addr)
> > +       grub_multiboot_payload_eip += base_addr - min_addr;
> > +      else
> > +       grub_multiboot_payload_eip -= min_addr - base_addr;
> > +    }
> > +
> >
> Why is it relative to min_addr? Sounds like it should be just an offset

Ugh... IIRC, it has meaning but I forgot what. I will check it.
However, this means that I must put comment here.

> from base addr. What do ET_DYN files use?

I will take a look.

Daniel



reply via email to

[Prev in Thread] Current Thread [Next in Thread]