diff mbox

[v9,1/8] loader: Allow ELF loader to auto-detect the ELF arch

Message ID 6160031ceab98811f73ab3d81d546bd568ef8e41.1468454556.git.alistair.francis@xilinx.com (mailing list archive)
State New, archived
Headers show

Commit Message

Alistair Francis July 14, 2016, 12:03 a.m. UTC
If the caller didn't specify an architecture for the ELF machine
the load_elf() function will auto detect it based on the ELF file.

Signed-off-by: Alistair Francis <alistair.francis@xilinx.com>
---
V9:
 - Update documentation
V8:
 - Move into load_elf64/load_elf32
V7:
 - Fix typo

 include/hw/elf_ops.h | 5 +++++
 include/hw/loader.h  | 3 +++
 2 files changed, 8 insertions(+)

Comments

Peter Maydell July 29, 2016, 3:42 p.m. UTC | #1
On 14 July 2016 at 01:03, Alistair Francis <alistair.francis@xilinx.com> wrote:
> If the caller didn't specify an architecture for the ELF machine
> the load_elf() function will auto detect it based on the ELF file.
>
> Signed-off-by: Alistair Francis <alistair.francis@xilinx.com>
> ---
> V9:
>  - Update documentation
> V8:
>  - Move into load_elf64/load_elf32
> V7:
>  - Fix typo
>
>  include/hw/elf_ops.h | 5 +++++
>  include/hw/loader.h  | 3 +++
>  2 files changed, 8 insertions(+)
>
> diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h
> index f510e7e..db70c11 100644
> --- a/include/hw/elf_ops.h
> +++ b/include/hw/elf_ops.h
> @@ -280,6 +280,11 @@ static int glue(load_elf, SZ)(const char *name, int fd,
>          glue(bswap_ehdr, SZ)(&ehdr);
>      }
>
> +    if (elf_machine < 1) {
> +        /* The caller didn't specify an ARCH, we can figure it out */
> +        elf_machine = ehdr.e_machine;
> +    }
> +
>      switch (elf_machine) {
>          case EM_PPC64:
>              if (ehdr.e_machine != EM_PPC64) {
> diff --git a/include/hw/loader.h b/include/hw/loader.h
> index 4879b63..fd540fc 100644
> --- a/include/hw/loader.h
> +++ b/include/hw/loader.h
> @@ -68,6 +68,9 @@ const char *load_elf_strerror(int error);
>   * load will fail if the target ELF does not match. Some architectures
>   * have some architecture-specific behaviours that come into effect when
>   * their particular values for @elf_machine are set.
> + * If no @elf_machine is provided the machine will default to the value
> + * in the ELFs header and no checks will be carried out against the
> + * machine type.
>   */

The argument is mandatory, you can't not provide it.
Should we make this "if @elf_machine is EM_NONE then the
machine type will be read from the ELF header" ? (EM_NONE is 0).

thanks
-- PMM
Alistair Francis Aug. 2, 2016, 12:03 a.m. UTC | #2
On Fri, Jul 29, 2016 at 8:42 AM, Peter Maydell <peter.maydell@linaro.org> wrote:
> On 14 July 2016 at 01:03, Alistair Francis <alistair.francis@xilinx.com> wrote:
>> If the caller didn't specify an architecture for the ELF machine
>> the load_elf() function will auto detect it based on the ELF file.
>>
>> Signed-off-by: Alistair Francis <alistair.francis@xilinx.com>
>> ---
>> V9:
>>  - Update documentation
>> V8:
>>  - Move into load_elf64/load_elf32
>> V7:
>>  - Fix typo
>>
>>  include/hw/elf_ops.h | 5 +++++
>>  include/hw/loader.h  | 3 +++
>>  2 files changed, 8 insertions(+)
>>
>> diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h
>> index f510e7e..db70c11 100644
>> --- a/include/hw/elf_ops.h
>> +++ b/include/hw/elf_ops.h
>> @@ -280,6 +280,11 @@ static int glue(load_elf, SZ)(const char *name, int fd,
>>          glue(bswap_ehdr, SZ)(&ehdr);
>>      }
>>
>> +    if (elf_machine < 1) {
>> +        /* The caller didn't specify an ARCH, we can figure it out */
>> +        elf_machine = ehdr.e_machine;
>> +    }
>> +
>>      switch (elf_machine) {
>>          case EM_PPC64:
>>              if (ehdr.e_machine != EM_PPC64) {
>> diff --git a/include/hw/loader.h b/include/hw/loader.h
>> index 4879b63..fd540fc 100644
>> --- a/include/hw/loader.h
>> +++ b/include/hw/loader.h
>> @@ -68,6 +68,9 @@ const char *load_elf_strerror(int error);
>>   * load will fail if the target ELF does not match. Some architectures
>>   * have some architecture-specific behaviours that come into effect when
>>   * their particular values for @elf_machine are set.
>> + * If no @elf_machine is provided the machine will default to the value
>> + * in the ELFs header and no checks will be carried out against the
>> + * machine type.
>>   */
>
> The argument is mandatory, you can't not provide it.
> Should we make this "if @elf_machine is EM_NONE then the
> machine type will be read from the ELF header" ? (EM_NONE is 0).

Good point, fixed.

Thanks,

Alistair

>
> thanks
> -- PMM
>
diff mbox

Patch

diff --git a/include/hw/elf_ops.h b/include/hw/elf_ops.h
index f510e7e..db70c11 100644
--- a/include/hw/elf_ops.h
+++ b/include/hw/elf_ops.h
@@ -280,6 +280,11 @@  static int glue(load_elf, SZ)(const char *name, int fd,
         glue(bswap_ehdr, SZ)(&ehdr);
     }
 
+    if (elf_machine < 1) {
+        /* The caller didn't specify an ARCH, we can figure it out */
+        elf_machine = ehdr.e_machine;
+    }
+
     switch (elf_machine) {
         case EM_PPC64:
             if (ehdr.e_machine != EM_PPC64) {
diff --git a/include/hw/loader.h b/include/hw/loader.h
index 4879b63..fd540fc 100644
--- a/include/hw/loader.h
+++ b/include/hw/loader.h
@@ -68,6 +68,9 @@  const char *load_elf_strerror(int error);
  * load will fail if the target ELF does not match. Some architectures
  * have some architecture-specific behaviours that come into effect when
  * their particular values for @elf_machine are set.
+ * If no @elf_machine is provided the machine will default to the value
+ * in the ELFs header and no checks will be carried out against the
+ * machine type.
  */
 
 int load_elf(const char *filename, uint64_t (*translate_fn)(void *, uint64_t),