From 3064018c763df7a58eba9cf4c2459083d21cc7a4 Mon Sep 17 00:00:00 2001 From: Sandipan Das Date: Sat, 6 Feb 2021 17:27:32 +0530 Subject: [PATCH] base: Add byte order attribute for object files This adds byte order as an attribute for object files by introducing new members to the ObjectFile class. This is populated by the looking at the ELF headers. Change-Id: Ibe55699175cc0295e0c9d49bdbe02e580988bc4f Signed-off-by: Sandipan Das --- src/base/loader/elf_object.cc | 10 ++++++++++ src/base/loader/elf_object.hh | 1 + src/base/loader/object_file.hh | 3 +++ 3 files changed, 14 insertions(+) diff --git a/src/base/loader/elf_object.cc b/src/base/loader/elf_object.cc index bdcc92b2c..419887536 100644 --- a/src/base/loader/elf_object.cc +++ b/src/base/loader/elf_object.cc @@ -112,6 +112,7 @@ ElfObject::ElfObject(ImageFileDataPtr ifd) : ObjectFile(ifd) determineArch(); determineOpSys(); + determineByteOrder(); entry = ehdr.e_entry; _programHeaderCount = ehdr.e_phnum; @@ -323,6 +324,15 @@ ElfObject::determineOpSys() } } +void +ElfObject::determineByteOrder() +{ + auto edata = ehdr.e_ident[EI_DATA]; + if (edata == ELFDATANONE) + panic("invalid ELF data encoding"); + byteOrder = (edata == ELFDATA2MSB) ? ByteOrder::big : ByteOrder::little; +} + void ElfObject::handleLoadableSegment(GElf_Phdr phdr, int seg_num) { diff --git a/src/base/loader/elf_object.hh b/src/base/loader/elf_object.hh index c262912af..24d0c0c6c 100644 --- a/src/base/loader/elf_object.hh +++ b/src/base/loader/elf_object.hh @@ -64,6 +64,7 @@ class ElfObject : public ObjectFile void determineArch(); void determineOpSys(); + void determineByteOrder(); void handleLoadableSegment(GElf_Phdr phdr, int seg_num); // These values are provided to a linux process by the kernel, so we diff --git a/src/base/loader/object_file.hh b/src/base/loader/object_file.hh index 0bfd918e8..7d542a98e 100644 --- a/src/base/loader/object_file.hh +++ b/src/base/loader/object_file.hh @@ -37,6 +37,7 @@ #include "base/loader/symtab.hh" #include "base/logging.hh" #include "base/types.hh" +#include "enums/ByteOrder.hh" namespace Loader { @@ -76,6 +77,7 @@ class ObjectFile : public ImageFile protected: Arch arch = UnknownArch; OpSys opSys = UnknownOpSys; + ByteOrder byteOrder = ByteOrder::little; SymbolTable _symtab; @@ -102,6 +104,7 @@ class ObjectFile : public ImageFile Arch getArch() const { return arch; } OpSys getOpSys() const { return opSys; } + ByteOrder getByteOrder() const { return byteOrder; } const SymbolTable &symtab() const { return _symtab; } -- 2.30.2