[PATCH V4 0/1] Add C-SKY support

2019-07-16 Thread Mao Han
> > The ELF_ARCH was defined as 39 inside linux kernel(before 4.20), so
> > the the core file generated was EM_RCE.
> > I'v regenerated the core file with ELF_ARCH defined as 252:
> > https://github.com/c-sky/test-result/blob/master/elfutils/zeroptr
> > https://github.com/c-sky/test-result/raw/master/elfutils/core.zeroptr
> > We have extended our core dump reg set last year, linux kernel have
> > upstreamed this change, but the gdb does not. I'v only got an old
> > released toolchain(EM_RCE) and the upstream toolchain(out of date
> > reg set) in my enviroment, so I can't generate/verify EM_CSKY core
> > file with any toolchain I have.
> 
> So the zeroptr binary looks correct now.
> But the core.zeroptr is probably generated by an older linux kernel and
> still is EM_RCE. This confusion is a bit confusing since I makes it
> really hard to check the details (register numbering, etc.) are really
> correct. I can tweak the code a bit to accept EM_RCE as EM_CSKY but
> then the PRSTATUS core note doesn't look correct. I am not sure that is
> because the register set as dumped by the kernel is bogus or because
> the prstatus_regs definition in csky_corenote.c is incorrect.

vmh@vmh-VirtualBox:~/workspace/test-result/elfutils$ file core.zeroptr
core.zeroptr: ELF 32-bit LSB core file *unknown arch 0xfc* version 1 (SYSV), 
SVR4-style, from 'root/zeroptr'

The core.zeroptr should be EM_CSKY now. I've tested the core file with
non-public released toolchain with EM_CSKY(the upstream gdb don't
support csky coredump). GPR seems loaded correctly. I think the
prstatus_regs have the correct reg size (36 * 4), problem may come
from the coredump regs->dwarf regs mapping(the mapping and coredump
support for binutil is not upsteamed).
https://github.com/c-sky/binutils-gdb/blob/fecb9cfcf9ac6e43ec2d971ce064fbb2dccb0847/gdb/csky-tdep.c:
/* Offset mapping table from core_section to regcache of general
   registers for ck810.  */
static int csky_gregset_offset_v2[] =
{
  72,  1,  0, 89,  2,  /* pc, r1, r0, psr, r2.  */
   3,  4,  5,  6,  7,  /* r3 ~ r32.  */
   8,  9, 10, 11, 12,
  13, 14, 15, 16, 17,
  18, 19, 20, 21, 22,
  23, 24, 25, 26, 27,
  28, 29, 30, 31
};
/home/vmh/workspace/buildroot2/buildroot/output/host/bin/csky-linux-gdb zeroptr 
/home/vmh/workspace/buildroot2/buildroot/output/host/bin/csky-linux-gdb: 
warning: A handler for the OS ABI "GNU/Linux" is not built into this 
configuration
of GDB.  Attempting to continue with the default csky settings.

GNU gdb (GDB) 7.12
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=x86_64-pc-linux-gnu 
--target=csky-buildroot-linux-gnuabiv2".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
.
Find the GDB manual and other documentation resources online at:
.
For help, type "help".
Type "apropos word" to search for commands related to "word"...

warning: A handler for the OS ABI "GNU/Linux" is not built into this 
configuration
of GDB.  Attempting to continue with the default csky settings.

Reading symbols from zeroptr...done.
(cskygdb) core-file core.zeroptr
[New process 143]
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x in ?? ()
(cskygdb) i r
r0 0x1  1
r1 0x7fbbce24   2143014436
r2 0x0  0
r3 0x0  0
r4 0xc4430  803888
r5 0x0  0
r6 0x0  0
r7 0x8bfc   35836
r8 0x7fbbcd50   2143014224
r9 0xc4430  803888
r100xc43ec  803820
r110xc4190  803216
r120xf946246b   -112843669
r130x1b81c  112668
r140x7fbbcd4c   0x7fbbcd4c
r150x8656   34390
r160xc443c  803900
r170x   -1
r180x67180  422272
r190x   -1
r200x2f2f2f2f   791621423
r210x0  0
r220x1000   4096
r230x0  0
r240x0  0
r250x663ac  418732
r260x2ab56f50   716533584
r270x0  0
r280x2abecbd8   717147096
r290x0  0
r300x0  0
r310x68478  427128
pc 0x0  0x0
epc
psr0x140340 1311552
epsr   


> > These attributes including cpu name and some other ISA related
> > descriptions.
> > Some thing like:
> > CSKY_ARCH_NAME: "ck810"
> > CSKY_CPU_NAME:  "ck810f"
> > CSKY_ISA_FLAG:  0x12345678
> > CSKY_ISA_EXT_FLAG:  5
> > They are not documented yet.
> > I'v ask the person who is responsible for these to 

[PATCH V4 1/1] Add backend support for C-SKY

2019-07-16 Thread Mao Han
C-SKY V2 ABI manual:
https://github.com/c-sky/csky-doc/blob/master/C-SKY_V2_CPU_Applications_Binary_Interface_Standards_Manual.pdf
C-SKY architecture user guide:
https://github.com/c-sky/csky-doc/blob/master/CSKY%20Architecture%20user_guide.pdf

Signed-off-by: Mao Han 
---
 backends/ChangeLog   |  12 ++
 backends/Makefile.am |   9 -
 backends/csky_attrs.c|  67 +++
 backends/csky_cfi.c  |  60 
 backends/csky_corenote.c |  61 
 backends/csky_init.c |  65 ++
 backends/csky_initreg.c  |  87 
 backends/csky_regs.c | 101 +++
 backends/csky_reloc.def  |  86 
 backends/csky_symbol.c   |  77 
 libebl/ChangeLog |   4 ++
 libebl/eblopenbackend.c  |   1 +
 src/ChangeLog|   4 ++
 src/elflint.c|   2 +-
 src/readelf.c|   4 +-
 tests/ChangeLog  |   7 
 tests/hello_csky.ko.bz2  | Bin 0 -> 26448 bytes
 tests/run-addrcfi.sh |  43 
 tests/run-strip-reloc.sh |   3 +-
 tests/testfilecsky.bz2   | Bin 0 -> 3145 bytes
 20 files changed, 688 insertions(+), 5 deletions(-)
 create mode 100644 backends/csky_attrs.c
 create mode 100644 backends/csky_cfi.c
 create mode 100644 backends/csky_corenote.c
 create mode 100644 backends/csky_init.c
 create mode 100644 backends/csky_initreg.c
 create mode 100644 backends/csky_regs.c
 create mode 100644 backends/csky_reloc.def
 create mode 100644 backends/csky_symbol.c
 create mode 100644 tests/hello_csky.ko.bz2
 create mode 100644 tests/testfilecsky.bz2

diff --git a/backends/ChangeLog b/backends/ChangeLog
index af1a40e..f1eaf14 100644
--- a/backends/ChangeLog
+++ b/backends/ChangeLog
@@ -1,3 +1,15 @@
+2019-07-13 Mao Han 
+
+   * Makefile.am: Add C-SKY.
+   * csky_attrs.c: New file.
+   * csky_cfi.c: New file.
+   * csky_corenote.c: Likewise.
+   * csky_init.c: Likewise.
+   * csky_initreg.c: Likewise.
+   * csky_regs.c: Likewise.
+   * csky_reloc.def: Likewise.
+   * csky_symbol.c: Likewise.
+
 2019-06-28  Mark Wielaard  
 
* aarch64_init.c (aarch64_init.c): Remove eh->name;
diff --git a/backends/Makefile.am b/backends/Makefile.am
index 2126a2e..175468f 100644
--- a/backends/Makefile.am
+++ b/backends/Makefile.am
@@ -33,12 +33,13 @@ AM_CPPFLAGS += -I$(top_srcdir)/libebl 
-I$(top_srcdir)/libasm \
 
 
 modules = i386 sh x86_64 ia64 alpha arm aarch64 sparc ppc ppc64 s390 \
- tilegx m68k bpf riscv
+ tilegx m68k bpf riscv csky
 libebl_pic = libebl_i386_pic.a libebl_sh_pic.a libebl_x86_64_pic.a\
 libebl_ia64_pic.a libebl_alpha_pic.a libebl_arm_pic.a\
 libebl_aarch64_pic.a libebl_sparc_pic.a libebl_ppc_pic.a \
 libebl_ppc64_pic.a libebl_s390_pic.a libebl_tilegx_pic.a \
-libebl_m68k_pic.a libebl_bpf_pic.a libebl_riscv_pic.a
+libebl_m68k_pic.a libebl_bpf_pic.a libebl_riscv_pic.a\
+libebl_csky_pic.a
 noinst_LIBRARIES = $(libebl_pic)
 noinst_DATA = $(libebl_pic:_pic.a=.so)
 
@@ -136,6 +137,10 @@ riscv_SRCS = riscv_init.c riscv_symbol.c riscv_cfi.c 
riscv_regs.c \
 libebl_riscv_pic_a_SOURCES = $(riscv_SRCS)
 am_libebl_riscv_pic_a_OBJECTS = $(riscv_SRCS:.c=.os)
 
+csky_SRCS = csky_attrs.c csky_init.c csky_symbol.c csky_cfi.c \
+   csky_regs.c csky_initreg.c csky_corenote.c
+libebl_csky_pic_a_SOURCES = $(csky_SRCS)
+am_libebl_csky_pic_a_OBJECTS = $(csky_SRCS:.c=.os)
 
 libebl_%.so libebl_%.map: libebl_%_pic.a $(libelf) $(libdw) $(libeu)
@rm -f $(@:.so=.map)
diff --git a/backends/csky_attrs.c b/backends/csky_attrs.c
new file mode 100644
index 000..9b236f1
--- /dev/null
+++ b/backends/csky_attrs.c
@@ -0,0 +1,67 @@
+/* C-SKY ABI-specified defaults for DWARF CFI.
+   Copyright (C) 2019 Hangzhou C-SKY Microsystems co.,ltd.
+   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 

Re: [PATCH V4 0/1] Add C-SKY support

2019-07-16 Thread Mark Wielaard
On Tue, 2019-07-16 at 19:14 +0800, Mao Han wrote:
> > > The ELF_ARCH was defined as 39 inside linux kernel(before 4.20),
> > > so
> > > the the core file generated was EM_RCE.
> > > I'v regenerated the core file with ELF_ARCH defined as 252:
> > > https://github.com/c-sky/test-result/blob/master/elfutils/zeroptr
> > > 
https://github.com/c-sky/test-result/raw/master/elfutils/core.zeroptr
> > > We have extended our core dump reg set last year, linux kernel
> > > have
> > > upstreamed this change, but the gdb does not. I'v only got an old
> > > released toolchain(EM_RCE) and the upstream toolchain(out of date
> > > reg set) in my enviroment, so I can't generate/verify EM_CSKY
> > > core
> > > file with any toolchain I have.
> > 
> > So the zeroptr binary looks correct now.
> > But the core.zeroptr is probably generated by an older linux kernel
> > and
> > still is EM_RCE. This confusion is a bit confusing since I makes it
> > really hard to check the details (register numbering, etc.) are
> > really
> > correct. I can tweak the code a bit to accept EM_RCE as EM_CSKY but
> > then the PRSTATUS core note doesn't look correct. I am not sure
> > that is
> > because the register set as dumped by the kernel is bogus or
> > because
> > the prstatus_regs definition in csky_corenote.c is incorrect.
> 
> vmh@vmh-VirtualBox:~/workspace/test-result/elfutils$ file
> core.zeroptr
> core.zeroptr: ELF 32-bit LSB core file *unknown arch 0xfc* version 1
> (SYSV), SVR4-style, from 'root/zeroptr'
> 
> The core.zeroptr should be EM_CSKY now. I've tested the core file
> with
> non-public released toolchain with EM_CSKY(the upstream gdb don't
> support csky coredump). GPR seems loaded correctly. I think the
> prstatus_regs have the correct reg size (36 * 4), problem may come
> from the coredump regs->dwarf regs mapping(the mapping and coredump
> support for binutil is not upsteamed).

You are right. Things look a little garbled because we use
ebl_register_info () and the hook to get the names and types for the
register numbers in various places. But ebl_register_info assumes DWARF
register numbers. We do have ebl_dwarf_to_regno, but we really also
need ebl_regno_to_dwarf. But we don't have it and we should audit the
code to make sure it is used in all the right places.

We have been going over this new backend for a while now. I think the
register mapping are slightly unique in this case (ppc has some
oddness, but we hacked around it without proper hooks...). Lets decide
to tackle this after the release, when we are going to update the ebl
backends anyway. Hopefully by then the rest of the toolchain has been
updated too to the C-SKY ABIv2 making creating testcases easier.

> > > These attributes including cpu name and some other ISA related
> > > descriptions.
> > > Some thing like:
> > > CSKY_ARCH_NAME: "ck810"
> > > CSKY_CPU_NAME:  "ck810f"
> > > CSKY_ISA_FLAG:  0x12345678
> > > CSKY_ISA_EXT_FLAG:  5
> > > They are not documented yet.
> > > I'v ask the person who is responsible for these to update the ABI
> > > documents, but I think it will take a quite long time for them to
> > > do that. They are quite busy at present.
> > 
> > OK. If you can add that tweak to src/readelf.c and add an
> > check_object_attribute hook that handles the above attributes that
> > would be good.
> > 
> > Ideally you also add a testcase for tests/readelf-A.sh
> > Some of those tests cheat and create the attributes by hand.
> > But it would be nice if you could generate a small .o file with the
> > latest toolchain to be used as testcase in some other tests.
> 
> I'm not sure about how to handle different data type here. It seems
> only tag_name is required when data type is string, I could not
> found how to handle int here.
> The binary with csky.attribute currently can not be generate with
> public
> released toolchain, so I don't know how to add the testcase.

OK, lets add a testcase once this has gone upstream in the rest of the
toolchain. Good point about the tag representing a string or number.
But I think this is (accidentially) handled correctly already. See this
comment in readelf.c (print_attributes):

   /* GNU style tags have either a uleb128 value,
  when lowest bit is not set, or a string
  when the lowest bit is set.
  "compatibility" (32) is special.  It has
  both a string and a uleb128 value.  For
  non-gnu we assume 6 till 31 only take ints.
  XXX see arm backend, do we need a separate
  hook?  */

We probably need another hook one day, but it looks like csky follows
this assumption (4 and 5 are strings, 6 and 7 are numbers).

There is one bug in the implementation though. The vendor check is
wrong, checks for "gnu", should obviously be "csky":

diff --git a/backends/csky_attrs.c b/backends/csky_attrs.c
index 9b236f1c..177f0ba2 100644
--- a/backends/csky_attrs.c
+++ b/backends/csky_attrs.c
@@ -43,7 +43,7 @@ csky_check_object_attribute (Ebl *ebl __attribute__ 
((unused)),
const

Re: [PATCH V4 0/1] Add C-SKY support

2019-07-16 Thread Mao Han
On Tue, Jul 16, 2019 at 03:48:22PM +0200, Mark Wielaard wrote:
> > > > These attributes including cpu name and some other ISA related
> > > > descriptions.
> > > > Some thing like:
> > > > CSKY_ARCH_NAME: "ck810"
> > > > CSKY_CPU_NAME:  "ck810f"
> > > > CSKY_ISA_FLAG:  0x12345678
> > > > CSKY_ISA_EXT_FLAG:  5
> > > > They are not documented yet.
> > > > I'v ask the person who is responsible for these to update the ABI
> > > > documents, but I think it will take a quite long time for them to
> > > > do that. They are quite busy at present.
> > > 
> > > OK. If you can add that tweak to src/readelf.c and add an
> > > check_object_attribute hook that handles the above attributes that
> > > would be good.
> > > 
> > > Ideally you also add a testcase for tests/readelf-A.sh
> > > Some of those tests cheat and create the attributes by hand.
> > > But it would be nice if you could generate a small .o file with the
> > > latest toolchain to be used as testcase in some other tests.
> > 
> > I'm not sure about how to handle different data type here. It seems
> > only tag_name is required when data type is string, I could not
> > found how to handle int here.
> > The binary with csky.attribute currently can not be generate with
> > public
> > released toolchain, so I don't know how to add the testcase.
> 
> OK, lets add a testcase once this has gone upstream in the rest of the
> toolchain. Good point about the tag representing a string or number.
> But I think this is (accidentially) handled correctly already. See this
> comment in readelf.c (print_attributes):
> 
>/* GNU style tags have either a uleb128 value,
>   when lowest bit is not set, or a string
>   when the lowest bit is set.
>   "compatibility" (32) is special.  It has
>   both a string and a uleb128 value.  For
>   non-gnu we assume 6 till 31 only take ints.
>   XXX see arm backend, do we need a separate
>   hook?  */
> 
> We probably need another hook one day, but it looks like csky follows
> this assumption (4 and 5 are strings, 6 and 7 are numbers).

Yes, csky follows this assumption, so it is handled correctly already.
 
> There is one bug in the implementation though. The vendor check is
> wrong, checks for "gnu", should obviously be "csky":
> 
> diff --git a/backends/csky_attrs.c b/backends/csky_attrs.c
> index 9b236f1c..177f0ba2 100644
> --- a/backends/csky_attrs.c
> +++ b/backends/csky_attrs.c
> @@ -43,7 +43,7 @@ csky_check_object_attribute (Ebl *ebl __attribute__ 
> ((unused)),
> const char **tag_name,
> const char **value_name __attribute__ ((unused)))
>  {
> -  if (!strcmp (vendor, "gnu"))
> +  if (!strcmp (vendor, "csky"))
>  switch (tag)
>{
>case 4:
> 
> > Tested on x86
> > 
> > Testsuite summary for elfutils 0.176
> > 
> > # TOTAL: 209
> > # PASS:  204
> > # SKIP:  5
> > # XFAIL: 0
> > # FAIL:  0
> > # XPASS: 0
> > # ERROR: 0
> > 
> > 
> > Changes since v1:
> >   - Add the Signed-off-by line and the copyright
> > 
> > Changes since v2:
> >   - move changelog to corresponding entries
> >   - correct core dump registers size
> >   - remove unused fpu DWARF register
> > 
> > Changes since v3:
> >   - add testfilecsky.bz2 and hello_csky.ko.bz2
> >   - add csky_check_object_attribute
> > 
> > Mao Han (1):
> >   Add backend support for C-SKY
> 
> The new patch looks really good. Thanks. The addition of the testcases
> really helps showing things look good. I can make that one small fix
> s/gnu/csky/ in csky_attrs.c if you agree that is what was intended.
> Then I'll push it to master.

Yes, please. Thanks for your review and help improveing the patch.

> And after the next release we can add some more testcases and handle to
> register mappings more correctly.
OK.

Thanks,
Mao Han