[PATCH 1/2] Add support for Hexagon

2024-03-21 Thread Matheus Tavares Bernardino
This implements initial support for the Hexagon architecture.

Signed-off-by: Matheus Tavares Bernardino 
---
 backends/Makefile.am   |   6 +-
 backends/hexagon_init.c|  51 +++
 backends/hexagon_reloc.def | 130 
 backends/hexagon_symbol.c  |  58 
 libebl/eblopenbackend.c|   2 +
 libelf/elf.h   | 131 +
 src/elflint.c  |   2 +-
 7 files changed, 377 insertions(+), 3 deletions(-)
 create mode 100644 backends/hexagon_init.c
 create mode 100644 backends/hexagon_reloc.def
 create mode 100644 backends/hexagon_symbol.c

diff --git a/backends/Makefile.am b/backends/Makefile.am
index bbb2aac7..271c0eab 100644
--- a/backends/Makefile.am
+++ b/backends/Makefile.am
@@ -37,7 +37,7 @@ AM_CPPFLAGS += -I$(top_srcdir)/libebl -I$(top_srcdir)/libasm \
 noinst_LIBRARIES = libebl_backends.a libebl_backends_pic.a
 
 modules = i386 sh x86_64 ia64 alpha arm aarch64 sparc ppc ppc64 s390 \
- m68k bpf riscv csky loongarch arc
+ m68k bpf riscv csky loongarch arc hexagon
 
 i386_SRCS = i386_init.c i386_symbol.c i386_corenote.c i386_cfi.c \
i386_retval.c i386_regs.c i386_auxv.c \
@@ -102,12 +102,14 @@ loongarch_SRCS = loongarch_init.c loongarch_symbol.c 
loongarch_cfi.c \
 
 arc_SRCS = arc_init.c arc_symbol.c
 
+hexagon_SRCS = hexagon_init.c hexagon_symbol.c
+
 libebl_backends_a_SOURCES = $(i386_SRCS) $(sh_SRCS) $(x86_64_SRCS) \
$(ia64_SRCS) $(alpha_SRCS) $(arm_SRCS) \
$(aarch64_SRCS) $(sparc_SRCS) $(ppc_SRCS) \
$(ppc64_SRCS) $(s390_SRCS) \
$(m68k_SRCS) $(bpf_SRCS) $(riscv_SRCS) $(csky_SRCS) 
\
-   $(loongarch_SRCS) $(arc_SRCS)
+   $(loongarch_SRCS) $(arc_SRCS) $(hexagon_SRCS)
 
 libebl_backends_pic_a_SOURCES =
 am_libebl_backends_pic_a_OBJECTS = $(libebl_backends_a_SOURCES:.c=.os)
diff --git a/backends/hexagon_init.c b/backends/hexagon_init.c
new file mode 100644
index ..9c8c6d8d
--- /dev/null
+++ b/backends/hexagon_init.c
@@ -0,0 +1,51 @@
+/* Initialization of Hexagon specific backend library.
+   Copyright (C) 2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see <http://www.gnu.org/licenses/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include 
+#endif
+
+#define BACKEND hexagon_
+#define RELOC_PREFIXR_HEX_
+#include "libebl_CPU.h"
+
+/* This defines the common reloc hooks based on hexagon_reloc.def.  */
+#include "common-reloc.c"
+
+
+Ebl *
+hexagon_init (Elf *elf __attribute__ ((unused)),
+GElf_Half machine __attribute__ ((unused)),
+Ebl *eh)
+{
+  hexagon_init_reloc (eh);
+  HOOK (eh, reloc_simple_type);
+
+  return eh;
+}
+
diff --git a/backends/hexagon_reloc.def b/backends/hexagon_reloc.def
new file mode 100644
index ..cd55d374
--- /dev/null
+++ b/backends/hexagon_reloc.def
@@ -0,0 +1,130 @@
+/* List the relocation types for Hexagon.  -*- C -*-
+   Copyright (C) 2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Publ

[PATCH 2/2] Hexagon: implement machine flag check

2024-03-21 Thread Matheus Tavares Bernardino
This fixes the "invalid machine flag" error from eu-elflint when passing
hexagon binaries.

Signed-off-by: Matheus Tavares Bernardino 
---
 backends/hexagon_init.c   | 1 +
 backends/hexagon_symbol.c | 8 
 2 files changed, 9 insertions(+)

diff --git a/backends/hexagon_init.c b/backends/hexagon_init.c
index 9c8c6d8d..1cd27513 100644
--- a/backends/hexagon_init.c
+++ b/backends/hexagon_init.c
@@ -45,6 +45,7 @@ hexagon_init (Elf *elf __attribute__ ((unused)),
 {
   hexagon_init_reloc (eh);
   HOOK (eh, reloc_simple_type);
+  HOOK (eh, machine_flag_check);
 
   return eh;
 }
diff --git a/backends/hexagon_symbol.c b/backends/hexagon_symbol.c
index b341243e..1e681e9f 100644
--- a/backends/hexagon_symbol.c
+++ b/backends/hexagon_symbol.c
@@ -56,3 +56,11 @@ hexagon_reloc_simple_type (Ebl *ebl __attribute__ 
((unused)), int type,
   return ELF_T_NUM;
 }
 }
+
+bool
+hexagon_machine_flag_check (GElf_Word flags)
+{
+  GElf_Word arch_variant = flags &~ EF_HEXAGON_MACH;
+  /* 0x8000 covers the "tiny core" arch variants. */
+  return arch_variant == 0 || arch_variant == 0x8000;
+}
-- 
2.37.2



[PATCH 0/2] Add initial support for Hexagon

2024-03-21 Thread Matheus Tavares Bernardino
The patches were inspired by
https://sourceware.org/cgit/elfutils/commit?id=13a4d1279c5b7847049ca3045d04f2705c45ce31

Related to:
https://lore.kernel.org/all/6498586d7d0ed112e6c44be98d439abc549653c7.ca...@klomp.org/t/#u

Matheus Tavares Bernardino (2):
  Add support for Hexagon
  Hexagon: implement machine flag check

 backends/Makefile.am   |   6 +-
 backends/hexagon_init.c|  52 +++
 backends/hexagon_reloc.def | 130 
 backends/hexagon_symbol.c  |  66 +++
 libebl/eblopenbackend.c|   2 +
 libelf/elf.h   | 131 +
 src/elflint.c  |   2 +-
 7 files changed, 386 insertions(+), 3 deletions(-)
 create mode 100644 backends/hexagon_init.c
 create mode 100644 backends/hexagon_reloc.def
 create mode 100644 backends/hexagon_symbol.c

-- 
2.37.2



[PATCH v2 2/2] Hexagon: implement machine flag check

2024-04-02 Thread Matheus Tavares Bernardino
This fixes the "invalid machine flag" error from eu-elflint when passing
hexagon binaries.

Signed-off-by: Matheus Tavares Bernardino 
---
 backends/hexagon_init.c   | 1 +
 backends/hexagon_symbol.c | 8 
 2 files changed, 9 insertions(+)

diff --git a/backends/hexagon_init.c b/backends/hexagon_init.c
index 9c8c6d8d..1cd27513 100644
--- a/backends/hexagon_init.c
+++ b/backends/hexagon_init.c
@@ -45,6 +45,7 @@ hexagon_init (Elf *elf __attribute__ ((unused)),
 {
   hexagon_init_reloc (eh);
   HOOK (eh, reloc_simple_type);
+  HOOK (eh, machine_flag_check);
 
   return eh;
 }
diff --git a/backends/hexagon_symbol.c b/backends/hexagon_symbol.c
index b341243e..1e681e9f 100644
--- a/backends/hexagon_symbol.c
+++ b/backends/hexagon_symbol.c
@@ -56,3 +56,11 @@ hexagon_reloc_simple_type (Ebl *ebl __attribute__ 
((unused)), int type,
   return ELF_T_NUM;
 }
 }
+
+bool
+hexagon_machine_flag_check (GElf_Word flags)
+{
+  GElf_Word arch_variant = flags &~ EF_HEXAGON_MACH;
+  /* 0x8000 covers the "tiny core" arch variants. */
+  return arch_variant == 0 || arch_variant == 0x8000;
+}
-- 
2.37.2



[PATCH v2 0/2] Add initial support for Hexagon

2024-04-02 Thread Matheus Tavares Bernardino
v1: https://sourceware.org/pipermail/elfutils-devel/2024q1/006938.html

Changes in v2:

- Moved Hexagon constants to libelf/elf-knowledge.h
- Added link to Hexagon ABI spec at commit message
  (i.e. 
https://lists.llvm.org/pipermail/llvm-dev/attachments/20190916/21516a52/attachment-0001.pdf)
- Added hello_hexagon.ko test

Matheus Tavares Bernardino (2):
  Add support for Hexagon
  Hexagon: implement machine flag check

 backends/Makefile.am|   7 +-
 backends/hexagon_init.c |  52 ++
 backends/hexagon_reloc.def  | 130 +++
 backends/hexagon_symbol.c   |  66 ++
 libebl/eblopenbackend.c |   2 +
 libelf/elf-knowledge.h  | 131 
 src/elflint.c   |   2 +-
 tests/Makefile.am   |   2 +-
 tests/hello_hexagon.ko.bz2  | Bin 0 -> 11184 bytes
 tests/run-strip-reloc-ko.sh |   3 +-
 10 files changed, 390 insertions(+), 5 deletions(-)
 create mode 100644 backends/hexagon_init.c
 create mode 100644 backends/hexagon_reloc.def
 create mode 100644 backends/hexagon_symbol.c
 create mode 100644 tests/hello_hexagon.ko.bz2

-- 
2.37.2



[PATCH v2 1/2] Add support for Hexagon

2024-04-02 Thread Matheus Tavares Bernardino
This implements initial support for the Hexagon architecture. The
Hexagon ABI spec can be seen at
https://lists.llvm.org/pipermail/llvm-dev/attachments/20190916/21516a52/attachment-0001.pdf

A hello_hexagon.ko test is also added.

$ head tests/test-suite.log
  [...]
  # TOTAL: 275
  # PASS:  269
  # SKIP:  6
  # XFAIL: 0
  # FAIL:  0
  # XPASS: 0
  # ERROR: 0

$ cat tests/run-strip-reloc-ko.sh.log
  [...]
  runtest hello_hexagon.ko
  PASS run-strip-reloc-ko.sh (exit status: 0)

Signed-off-by: Matheus Tavares Bernardino 
---
 backends/Makefile.am|   7 +-
 backends/hexagon_init.c |  51 ++
 backends/hexagon_reloc.def  | 130 +++
 backends/hexagon_symbol.c   |  58 
 libebl/eblopenbackend.c |   2 +
 libelf/elf-knowledge.h  | 131 
 src/elflint.c   |   2 +-
 tests/Makefile.am   |   2 +-
 tests/hello_hexagon.ko.bz2  | Bin 0 -> 11184 bytes
 tests/run-strip-reloc-ko.sh |   3 +-
 10 files changed, 381 insertions(+), 5 deletions(-)
 create mode 100644 backends/hexagon_init.c
 create mode 100644 backends/hexagon_reloc.def
 create mode 100644 backends/hexagon_symbol.c
 create mode 100644 tests/hello_hexagon.ko.bz2

diff --git a/backends/Makefile.am b/backends/Makefile.am
index b946fd30..e22b522c 100644
--- a/backends/Makefile.am
+++ b/backends/Makefile.am
@@ -37,7 +37,7 @@ AM_CPPFLAGS += -I$(top_srcdir)/libebl -I$(top_srcdir)/libasm \
 noinst_LIBRARIES = libebl_backends.a libebl_backends_pic.a
 
 modules = i386 sh x86_64 ia64 alpha arm aarch64 sparc ppc ppc64 s390 \
- m68k bpf riscv csky loongarch arc mips
+ m68k bpf riscv csky loongarch arc mips hexagon
 
 i386_SRCS = i386_init.c i386_symbol.c i386_corenote.c i386_cfi.c \
i386_retval.c i386_regs.c i386_auxv.c \
@@ -104,12 +104,15 @@ arc_SRCS = arc_init.c arc_symbol.c
 
 mips_SRCS = mips_init.c mips_symbol.c
 
+hexagon_SRCS = hexagon_init.c hexagon_symbol.c
+
 libebl_backends_a_SOURCES = $(i386_SRCS) $(sh_SRCS) $(x86_64_SRCS) \
$(ia64_SRCS) $(alpha_SRCS) $(arm_SRCS) \
$(aarch64_SRCS) $(sparc_SRCS) $(ppc_SRCS) \
$(ppc64_SRCS) $(s390_SRCS) \
$(m68k_SRCS) $(bpf_SRCS) $(riscv_SRCS) $(csky_SRCS) 
\
-   $(loongarch_SRCS) $(arc_SRCS) $(mips_SRCS)
+   $(loongarch_SRCS) $(arc_SRCS) $(mips_SRCS) \
+   $(hexagon_SRCS)
 
 libebl_backends_pic_a_SOURCES =
 am_libebl_backends_pic_a_OBJECTS = $(libebl_backends_a_SOURCES:.c=.os)
diff --git a/backends/hexagon_init.c b/backends/hexagon_init.c
new file mode 100644
index ..9c8c6d8d
--- /dev/null
+++ b/backends/hexagon_init.c
@@ -0,0 +1,51 @@
+/* Initialization of Hexagon specific backend library.
+   Copyright (C) 2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see <http://www.gnu.org/licenses/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include 
+#endif
+
+#define BACKEND hexagon_
+#define RELOC_PREFIXR_HEX_
+#include "libebl_CPU.h"
+
+/* This defines the common reloc hooks based on hexagon_reloc.def.  */
+#include "common-reloc.c"
+
+
+Ebl *
+hexagon_init (Elf *elf __attribute__ ((unused)),
+GElf_Half machine __attribute__ ((unused)),
+Ebl *eh)
+{
+  hexagon_init_reloc (eh);
+  HOOK (eh, reloc_simple_type);
+
+  return eh;
+}
+
diff --git a/backends/hexagon_reloc.def b/backends/hexagon_reloc.def
new file mode 100644
index ..cd55d374
--- /dev/null
+++ b/backends/hexagon_reloc.def
@@ -0,0 +1,130 @@
+/* List the relocation types for Hexagon.  -*- C -*-
+   Copyright (C) 2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either ver

[PATCH v3] Hexagon: implement machine flag check

2024-04-04 Thread Matheus Tavares Bernardino
This fixes the "invalid machine flag" error from eu-elflint when passing
hexagon binaries.

* backends/hexagon_init.c (hexagon_init): Hook
machine_flag_check
* backends/hexagon_symbol.c (hexagon_machine_flag_check):
new function
* libelf/elf-knowledge.h: add EF_HEXAGON_TINY constant

Signed-off-by: Matheus Tavares Bernardino 
---

v2: https://sourceware.org/pipermail/elfutils-devel/2024q2/006987.html

Changes in v3:
- Added ChangeLog to commit message.
- Implemented better machine_flag_check operation, as suggested by
  bcain.
- Extracted only patch 2/2 as 1/2 was already merged.

 backends/hexagon_init.c   | 1 +
 backends/hexagon_symbol.c | 7 +++
 libelf/elf-knowledge.h| 1 +
 3 files changed, 9 insertions(+)

diff --git a/backends/hexagon_init.c b/backends/hexagon_init.c
index 9c8c6d8d..1cd27513 100644
--- a/backends/hexagon_init.c
+++ b/backends/hexagon_init.c
@@ -45,6 +45,7 @@ hexagon_init (Elf *elf __attribute__ ((unused)),
 {
   hexagon_init_reloc (eh);
   HOOK (eh, reloc_simple_type);
+  HOOK (eh, machine_flag_check);
 
   return eh;
 }
diff --git a/backends/hexagon_symbol.c b/backends/hexagon_symbol.c
index b341243e..5f6e0fe5 100644
--- a/backends/hexagon_symbol.c
+++ b/backends/hexagon_symbol.c
@@ -56,3 +56,10 @@ hexagon_reloc_simple_type (Ebl *ebl __attribute__ 
((unused)), int type,
   return ELF_T_NUM;
 }
 }
+
+bool
+hexagon_machine_flag_check (GElf_Word flags)
+{
+  GElf_Word reserved_flags = ~(EF_HEXAGON_TINY | EF_HEXAGON_MACH);
+  return (flags & reserved_flags) == 0;
+}
diff --git a/libelf/elf-knowledge.h b/libelf/elf-knowledge.h
index 71535934..23e34ca1 100644
--- a/libelf/elf-knowledge.h
+++ b/libelf/elf-knowledge.h
@@ -119,6 +119,7 @@
 #define EF_HEXAGON_MACH_V71T 0x8071 /* Hexagon V71T */
 #define EF_HEXAGON_MACH_V73  0x0073 /* Hexagon V73 */
 #define EF_HEXAGON_MACH  0x03ff /* Hexagon V.. */
+#define EF_HEXAGON_TINY  0x8000 /* Hexagon V..T */
 
 /* Special section indices.  */
 #define SHN_HEXAGON_SCOMMON0xff00 /* Other access sizes */
-- 
2.37.2



Re: [PATCH v3] Hexagon: implement machine flag check

2024-04-04 Thread Matheus Tavares Bernardino
On Thu, Apr 04, 2024 at 21:43:11 +0200, Mark Wielaard wrote:
>
> 
> Note that we also have a public-inbox instance, which is useful in
> some cases since you can address patches by message-id:
> https://inbox.sourceware.org/elfutils-devel/d198f1806a486bd5129c996f10680b947ecdc581.1712087613.git.quic_mathb...@quicinc.com/

Awesome! I much rather prefer the public-inbox interface :) Thanks!

BTW, just out of curiosity, since the last incident with xz's backdoor
(which apparently involved malicious code disguised as a test binary),
has the elfutils community already considered using something like
Dockerfiles to generate the tests/*.ko.bz2 binaries instead of checking
than in the git repo? Just something that crossed my mind while I was
developing these patches.


Re: [PATCH v3] Hexagon: implement machine flag check

2024-04-05 Thread Matheus Tavares Bernardino
On Fri, 05 Apr 2024 16:45:40 +0200 Mark Wielaard  wrote:
>
> Hi Matheus,
> 
> On Thu, 2024-04-04 at 16:56 -0300, Matheus Tavares Bernardino wrote:
> > BTW, just out of curiosity, since the last incident with xz's backdoor
> > (which apparently involved malicious code disguised as a test binary),
> > has the elfutils community already considered using something like
> > Dockerfiles to generate the tests/*.ko.bz2 binaries instead of checking
> > than in the git repo? Just something that crossed my mind while I was
> > developing these patches.
> 
> [...] 
> In the xz-backdoor case it was actually hidden in a test binary which
> wasn't actually used in the testsuite. So that is certainly something
> to watch out for. Does someone add a binary file for no good reason?
> Also this seems to be a somewhat sophisticated hack and the would
> probably found some other way to hide something.

Good point :)

> Another would be what you suggest. Create containers for all arches
> supported and (re)generate all test binaries in that container. But
> that would be a lot of containers and for some arches you like to have
> different versions of the tools to generate them. And can that be done
> for all arches? e.g. Does hexagon have qemu support?

It does :) But I was actually thinking about using the containers to
cross-build the binaries, like we do for the QEMU tests. E.g.
https://github.com/qemu/qemu/blob/master/tests/docker/dockerfiles/debian-hexagon-cross.docker

Nonetheless, yeah, that will be a lot of containers, and a significant ammount
of work.