[PATCH] backends: Check results for NULL early in dwarf_peeled_die_type

2023-04-06 Thread Mark Wielaard
Calling dwarf_peeled_die_type with a NULL results pointer is an error,
check early that result is not NULL so dwarf_formref_die and
dwarf_peel_type won't try to set the NULL Dwarf_Die.

* backends/libebl_CPU.h (dwarf_peeled_die_type): Move check
for results == NULL to start of function.

Signed-off-by: Mark Wielaard 
---
 backends/libebl_CPU.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/backends/libebl_CPU.h b/backends/libebl_CPU.h
index 3b2cc3e4..d138f5f7 100644
--- a/backends/libebl_CPU.h
+++ b/backends/libebl_CPU.h
@@ -66,13 +66,13 @@ dwarf_peeled_die_type (Dwarf_Die *die, Dwarf_Die *result)
 /* The function has no return value, like a `void' function in C.  */
 return 0;
 
-  if (dwarf_formref_die (attr, result) == NULL)
+  if (result == NULL)
 return -1;
 
-  if (dwarf_peel_type (result, result) != 0)
+  if (dwarf_formref_die (attr, result) == NULL)
 return -1;
 
-  if (result == NULL)
+  if (dwarf_peel_type (result, result) != 0)
 return -1;
 
   int tag = dwarf_tag (result);
-- 
2.39.2



[PATCH] readelf: Handle NULL shdr in section_name

2023-04-06 Thread Mark Wielaard
In some error cases we want to show the section name but cannot
because the section header is corrupt or NULL. Make sure the
section_name always returns "???" in that case.

* src/readelf.c (section_name): Check for shdr == NULL.

Signed-off-by: Mark Wielaard 
---
 src/readelf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/readelf.c b/src/readelf.c
index 6950204e..4a1b985d 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -1606,7 +1606,7 @@ static const char *
 section_name (Ebl *ebl, GElf_Shdr *shdr)
 {
   size_t shstrndx;
-  if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
+  if (shdr == NULL || elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
 return "???";
   return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
 }
-- 
2.39.2



Some ideas for process improvements/changes

2023-04-06 Thread Mark Wielaard
Hi hackers,

In general it feels like the elfutils community is working well, there
are regular releases with bug fixes and new features. Most patches are
reviewed fairly quickly (although there are some exceptions where
patches have been pending too long). So I don't want to change too
much. But here are some small suggestions for changes to out processes
that might be helpful:

- Get rid of ChangeLog files and trivial ChangeLog entries
  I personally love ChangeLog entries. Writing them helps me
  double check I actually intended to make the changes. And
  it is a great help reviewing patches. It helps having to
  guess if some specific change was an accident or intended.

  But patches that have changes against the ChangeLog files are
  sometimes hard to rebase or move between branches. The gnulib
  git-merge-changelog driver is awesome, but is not always able
  to help. Also some commit messages for smaller changes are
  already fine describing what changed.

  So I propose to drop ChangeLog files completely and only add
  a ChangeLog entry to the commit message for larger changes
  to help the review process.

- Use patchwork more
  All patches sent to the mailing list are tracked at
  https://patchwork.sourceware.org/project/elfutils/list/
  It has helped me a lot keeping track of patches that
  have been pending for some time. Also git-pw has been
  really nice for cherry-picking patches.
  https://patchwork.readthedocs.io/projects/git-pw/en/latest/
  
  Please let me know if you would like to help maintain the
  pending patch list and I'll add your account as maintainer
  for the elfutils project.

  For using it with git-pw use these .git/config settings:
  [pw]
server = https://patchwork.sourceware.org/api/1.2/
project = elfutils
token = 
states = committed,accepted,superseded,deferred,rejected,under-review

  It would be nice if it was automated a bit more by have a git
  commit hook that flagged whether a patch was committed. And if
  the buildbot try-branch system would flag pass/fail on the patch.

- Don't require "real names" in Signed-off-by lines.
  Our current CONTRIBUTING guide say that you have to use your 
  your real name for the Signed-off-by line. This is sometimes
  problematic for people for who their real (legal) name is not
  how they identify themselves to others. I suggest to change
  the requirement as follows (this mimics what the linux kernel
  project did recently):

diff --git a/CONTRIBUTING b/CONTRIBUTING
index bb48975b..1a1c443f 100644
--- a/CONTRIBUTING
+++ b/CONTRIBUTING
@@ -45,7 +45,9 @@ then you just add a line saying
 
 Signed-off-by: Random J Developer 
 
-using your real name (sorry, no pseudonyms or anonymous
contributions.)
+using a known identity (sorry, no anonymous contributions.)
+The name you use as your identity should not be an anonymous id
+or false name that misrepresents who you are.
 
 git commit --signoff will add such a Signed-off-by line at the end of
 the commit log message for you.

- "Security" bug guidance
  Here I don't have good guidance, but I have the feeling some of
  the bugs reported (especially by some fuzzers) are sometimes
  unnecessarily marked as security issues. Which causes lots of
  unnecessary work for downstream users of our code. Especially
  if someone starts assigning CVEs to them. It would be good to
  have some explicit text to point "security" bug reporters at
  on how we will handle their bugs.

Cheers,

Mark


Re: Some ideas for process improvements/changes

2023-04-06 Thread Frank Ch. Eigler via Elfutils-devel
Hi -

> - Get rid of ChangeLog files and trivial ChangeLog entries
>   [...]

Yes please!

> - Use patchwork more
>   [...]

This doesn't seem like something for community/contributors
to do - patchwork seems mostly a maintainer/committer tool.

>   It would be nice if it was automated a bit more by have a git
>   commit hook that flagged whether a patch was committed. And if
>   the buildbot try-branch system would flag pass/fail on the patch.

Sounds like a sourceware infrastructure RFE.

> - Don't require "real names" in Signed-off-by lines.
>   [...]
> +The name you use as your identity should not be an anonymous id
> +or false name that misrepresents who you are.

(No strong opinion on this one, except that a declaration that is this
informal would have little weight, should it ever be relied upon in
legal proceedings.)


> - "Security" bug guidance
>   [...]

Yeah, a brief SECURITY file would be nice.


- FChE



Re: Some ideas for process improvements/changes

2023-04-06 Thread Mark Wielaard
Hi Frank,

On Thu, Apr 06, 2023 at 01:34:20PM -0400, Frank Ch. Eigler via Elfutils-devel 
wrote:
> > - Get rid of ChangeLog files and trivial ChangeLog entries
> >   [...]
> 
> Yes please!

So sad, on irc people are also enthousiastic about this. O well. :)

> > - Use patchwork more
> >   [...]
> 
> This doesn't seem like something for community/contributors
> to do - patchwork seems mostly a maintainer/committer tool.

But I want more community/contributors to feel like they are
maintainers/committers!

> >   It would be nice if it was automated a bit more by have a git
> >   commit hook that flagged whether a patch was committed. And if
> >   the buildbot try-branch system would flag pass/fail on the patch.
> 
> Sounds like a sourceware infrastructure RFE.

Yes, but if I RFE that then it often just comes back to me to add it
:) So I mention it here in the hope someone says "O, but that is easy,
this is exactly how to do it..."

> > - Don't require "real names" in Signed-off-by lines.
> >   [...]
> > +The name you use as your identity should not be an anonymous id
> > +or false name that misrepresents who you are.
> 
> (No strong opinion on this one, except that a declaration that is this
> informal would have little weight, should it ever be relied upon in
> legal proceedings.)

Do you feel this weakens our Developer's Certificate of Origin
process? My point is that we shouldn't judge what is a "real name" or
not. But the name shouldn't misrepresent who someone is. What we care
about is that the identity people use to sign the certificate refers
to a real person that can be contacted about their contributions when
needed.

> > - "Security" bug guidance
> >   [...]
> 
> Yeah, a brief SECURITY file would be nice.

Any suggestions about what to put in such a section or file.  My main
concern is that people are filing things we regard as simple bugs as
"security" issues and get CVEs assigned which cause lots of extra work
for some of our downstream users. I think we should be clear that we
want to fix all bugs and don't want to get dragged into embargoed
security theater.
https://daniel.haxx.se/blog/2023/03/29/pre-notification-dilemmas/

Cheers,

Mark


[Bug libdw/30272] Unwinding multithreaded musl applications fails

2023-04-06 Thread mark at klomp dot org via Elfutils-devel
https://sourceware.org/bugzilla/show_bug.cgi?id=30272

Mark Wielaard  changed:

   What|Removed |Added

 CC||mark at klomp dot org

--- Comment #3 from Mark Wielaard  ---
This does seem a bug in musl which doesn't seem to add enough cfi as the email
thread mentioned in comment #2 says.

Note that glibc does explicitly mark the end of stack in clone in cfi by
undefining the pc:

sysdeps/unix/sysv/linux/aarch64/clone.S:cfi_undefined (x30)
sysdeps/unix/sysv/linux/aarch64/clone3.S:   cfi_undefined (x30)
sysdeps/unix/sysv/linux/alpha/clone.S:  cfi_undefined(ra)
sysdeps/unix/sysv/linux/csky/abiv2/clone.S: cfi_undefined (lr)
sysdeps/unix/sysv/linux/i386/clone.S:   cfi_undefined (eip);
sysdeps/unix/sysv/linux/i386/clone3.S:  cfi_undefined (eip)
sysdeps/unix/sysv/linux/loongarch/clone.S:  cfi_undefined (1)
sysdeps/unix/sysv/linux/loongarch/clone3.S: cfi_undefined (1)
sysdeps/unix/sysv/linux/m68k/clone.S:   cfi_undefined (pc)  /* Mark end of
stack */
sysdeps/unix/sysv/linux/mips/clone.S:   cfi_undefined ($31)
sysdeps/unix/sysv/linux/nios2/clone.S:  cfi_undefined (ra)
sysdeps/unix/sysv/linux/riscv/clone.S:  cfi_undefined (ra)
sysdeps/unix/sysv/linux/s390/s390-32/clone.S:   cfi_undefined (r14)
sysdeps/unix/sysv/linux/s390/s390-64/clone.S:   cfi_undefined (r14)
sysdeps/unix/sysv/linux/x86_64/clone.S: cfi_undefined (rip);
sysdeps/unix/sysv/linux/x86_64/clone3.S:cfi_undefined (rip)

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Re: [PATCH 1/2] libelf: Sync elf.h from glibc.

2023-04-06 Thread Mark Wielaard
On Sat, Apr 01, 2023 at 02:43:43PM +0800, Youling Tang wrote:
> Adds R_LARCH_*.

Thanks, pushed.


Re: [PATCH 2/2] backends: Add new relocation type handling for LoongArch

2023-04-06 Thread Mark Wielaard
On Sat, Apr 01, 2023 at 02:43:44PM +0800, Youling Tang wrote:
> Add new relocation type handling.

Thanks, looks good. Pushed.

Cheers,

Mark


Re: [PATCH] backends: add checks for _GLOBAL_OFFSET_TABLE_ on loongarch

2023-04-06 Thread Mark Wielaard
Hi,

On Sat, Apr 01, 2023 at 11:18:53AM +0800, Youling Tang wrote:
> Add handling of _GLOBAL_OFFSET_TABLE_.
> 
> Before applying the patch:
> $ ./src/elflint --gnu-ld ./src/elflint
> section [35] '.symtab': _GLOBAL_OFFSET_TABLE_ symbol value 0x68548
> does not match .got.plt section address 0x68238
> 
> After applying the patch:
> $ ./src/elflint --gnu-ld ./src/elflint
> No errors

Thanks, code looks correct.  I agree that the other issue mentioned
(_DYNAMIC symbol size 0) is a separate issue.

Pushed,

Mark


☠ Buildbot (Sourceware): elfutils - failed test (failure) (master)

2023-04-06 Thread builder--- via Elfutils-devel
A new failure has been detected on builder elfutils-gentoo-sparc while building 
elfutils.

Full details are available at:
https://builder.sourceware.org/buildbot/#builders/225/builds/53

Build state: failed test (failure)
Revision: 6e9718089b05403947d8255423a849d425305925
Worker: gentoo-sparc
Build Reason: (unknown)
Blamelist: Youling Tang 

Steps:

- 0: worker_preparation ( success )

- 1: set package name ( success )

- 2: git checkout ( success )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/2/logs/stdio

- 3: autoreconf ( success )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/3/logs/stdio

- 4: configure ( success )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/4/logs/stdio
- config.log: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/4/logs/config_log

- 5: get version ( success )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/5/logs/stdio
- property changes: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/5/logs/property_changes

- 6: make ( warnings )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/6/logs/stdio
- warnings (3): 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/6/logs/warnings__3_

- 7: make check ( failure )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/7/logs/stdio
- test-suite.log: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/7/logs/test-suite_log

- 8: prep ( success )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/8/logs/stdio

- 9: build bunsen.cpio.gz ( success )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/9/logs/stdio

- 10: fetch bunsen.cpio.gz ( success )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/10/logs/stdio

- 11: unpack bunsen.cpio.gz ( success )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/11/logs/stdio

- 12: pass .bunsen.source.gitname ( success )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/12/logs/stdio

- 13: pass .bunsen.source.gitdescribe ( success )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/13/logs/stdio

- 14: pass .bunsen.source.gitbranch ( success )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/14/logs/stdio

- 15: pass .bunsen.source.gitrepo ( success )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/15/logs/stdio

- 16: upload to bunsen ( success )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/16/logs/stdio

- 17: clean up ( success )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/17/logs/stdio

- 18: make distclean ( success )
Logs:
- stdio: 
https://builder.sourceware.org/buildbot/#builders/225/builds/53/steps/18/logs/stdio



[PATCH 0/5] Improve LoongArch support

2023-04-06 Thread Youling Tang
Test Environment (New Toolchain):
 $ cat /etc/os-release
 NAME="My GNU/Linux System for LoongArch64"
 VERSION="6.0"
 ID=CLFS4LA64
 PRETTY_NAME="My GNU/Linux System for LoongArch64 6.0"

 $ gcc -v
 gcc version 13.0.0 20220919 (experimental) (GCC) 

Test Results:
 
 Testsuite summary for elfutils 0.189
 
 # TOTAL: 238
 # PASS:  234
 # SKIP:  4
 # XFAIL: 0
 # FAIL:  0
 # XPASS: 0
 # ERROR: 0
 


Youling Tang (5):
  backends: Add abi_cfi and register_info callbacks for LoongArch
  backends: Add set_initial_registers_tid callback for  LoongArch
  backends: Add initial return value location support for LoongArch
  backends: Add frame pointer unwinding for LoongArch
  backends: Add core_note callback for LoongArch

 backends/ChangeLog|  33 +-
 backends/Makefile.am  |   4 +-
 backends/loongarch_cfi.c  |  83 ++
 backends/loongarch_corenote.c | 114 +++
 backends/loongarch_init.c |  10 ++
 backends/loongarch_initreg.c  |  91 +++
 backends/loongarch_regs.c | 141 
 backends/loongarch_retval.c   | 202 ++
 backends/loongarch_unwind.c   |  84 ++
 9 files changed, 760 insertions(+), 2 deletions(-)
 create mode 100644 backends/loongarch_cfi.c
 create mode 100644 backends/loongarch_corenote.c
 create mode 100644 backends/loongarch_initreg.c
 create mode 100644 backends/loongarch_regs.c
 create mode 100644 backends/loongarch_retval.c
 create mode 100644 backends/loongarch_unwind.c

-- 
2.37.1



[PATCH 1/5] backends: Add abi_cfi and register_info callbacks for LoongArch

2023-04-06 Thread Youling Tang
LoongArch Reference Manual - Volume 1:
https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html

LoongArch ELF ABI:
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html

Signed-off-by: Liwei Ge 
Signed-off-by: Youling Tang 
---
 backends/ChangeLog|   9 ++-
 backends/Makefile.am  |   3 +-
 backends/loongarch_cfi.c  |  83 ++
 backends/loongarch_init.c |   4 ++
 backends/loongarch_regs.c | 141 ++
 5 files changed, 238 insertions(+), 2 deletions(-)
 create mode 100644 backends/loongarch_cfi.c
 create mode 100644 backends/loongarch_regs.c

diff --git a/backends/ChangeLog b/backends/ChangeLog
index 7bde2919..926c76ed 100644
--- a/backends/ChangeLog
+++ b/backends/ChangeLog
@@ -1,4 +1,11 @@
-2023-04-01  Youling Tang 
+2023-04-07  Youling Tang  
+
+   * Makefile.am (loongarch_SRCS): Add loongarch_cfi.c and 
loongarch_regs.c.
+   * loongarch_cfi.c: New file.
+   * loongarch_regs.c: Likewise.
+   * loongarch_init.c (loongarch_init): Hook register_info and abi_cfi.
+
+2023-04-01  Youling Tang  
 
* loongarch_init.c (loongarch_init): Hook check_special_symbol.
* loongarch_symbol.c (loongarch_check_special_symbol): New function.
diff --git a/backends/Makefile.am b/backends/Makefile.am
index f373e5fb..2b6f08ce 100644
--- a/backends/Makefile.am
+++ b/backends/Makefile.am
@@ -96,7 +96,8 @@ riscv_SRCS = riscv_init.c riscv_symbol.c riscv_cfi.c 
riscv_regs.c \
 csky_SRCS = csky_attrs.c csky_init.c csky_symbol.c csky_cfi.c \
csky_regs.c csky_initreg.c csky_corenote.c
 
-loongarch_SRCS = loongarch_init.c loongarch_symbol.c
+loongarch_SRCS = loongarch_init.c loongarch_symbol.c loongarch_cfi.c \
+   loongarch_regs.c
 
 arc_SRCS = arc_init.c arc_symbol.c
 
diff --git a/backends/loongarch_cfi.c b/backends/loongarch_cfi.c
new file mode 100644
index ..5f3cd2c7
--- /dev/null
+++ b/backends/loongarch_cfi.c
@@ -0,0 +1,83 @@
+/* LoongArch ABI-specified defaults for DWARF CFI.
+   Copyright (C) 2023 OpenAnolis community LoongArch SIG.
+   Copyright (C) 2023 Loongson Technology Corporation Limted.
+   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 .  */
+
+#ifdef HAVE_CONFIG_H
+# include 
+#endif
+
+#include 
+
+#define BACKEND loongarch_
+#include "libebl_CPU.h"
+
+/* LoongArch ELF ABI specification:
+https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_register_convention
+*/
+int
+loongarch_abi_cfi (Ebl *ebl __attribute__ ((unused)), Dwarf_CIE *abi_info)
+{
+  static const uint8_t abi_cfi[] =
+{
+  /* The initial Canonical Frame Address is the value of the
+ Stack Pointer ($r3) as setup in the previous frame. */
+  DW_CFA_def_cfa, ULEB128_7 (3), ULEB128_7 (0),
+
+  /* The Stack Pointer ($r3) is restored from CFA address by default.  */
+  DW_CFA_val_offset, ULEB128_7 (3), ULEB128_7 (0),
+
+#define SV(n) DW_CFA_same_value, ULEB128_7 (n)
+  /* The return address register contains the return address setup by
+caller.  */
+  SV (1),
+
+  /* Callee-saved registers $s0-$s7.  */
+  SV (23), SV (24), SV (25), SV (26), SV (27), SV (28),
+  SV (29), SV (30), SV (31),
+
+  /* The Frame Pointer ($fp, $r22) */
+  SV(22),
+
+  /* Callee-saved registers $fs0-$fs7.  */
+  SV (56), SV (57), SV (58), SV (59), SV (60), SV (61),
+  SV (62), SV (63),
+#undef SV
+
+  /* XXX Note: registers intentionally unused by the program,
+for example as a consequence of the procedure call standard
+should be initialized as if by DW_CFA_same_value.  */
+};
+
+  abi_info->initial_instructions = abi_cfi;
+  abi_info->initial_instructions_end = &abi_cfi[sizeof abi_cfi];
+  abi_info->data_alignment_factor = -4;
+
+  abi_info->return_address_register = 1; /* ra.  */
+
+  return 0;
+}
diff --git a/backends/loongarch_init.c b/backends/loongarch_init.c
index b641b07f..7bfaaa7f 100644
--- a/backends/loongarch_init.c
+++ b/backends/loongarch_init.c
@@ -46,6 +46,10 @@ loongarch_init 

[PATCH 2/5] backends: Add set_initial_registers_tid callback for LoongArch

2023-04-06 Thread Youling Tang
This patch implements the set_initial_registers_tid hook for LoongArch.

Signed-off-by: Liwei Ge 
Signed-off-by: Youling Tang 
---
 backends/ChangeLog   |  6 +++
 backends/Makefile.am |  2 +-
 backends/loongarch_init.c|  1 +
 backends/loongarch_initreg.c | 91 
 4 files changed, 99 insertions(+), 1 deletion(-)
 create mode 100644 backends/loongarch_initreg.c

diff --git a/backends/ChangeLog b/backends/ChangeLog
index 926c76ed..85abc8a5 100644
--- a/backends/ChangeLog
+++ b/backends/ChangeLog
@@ -1,3 +1,9 @@
+2023-04-07  Youling Tang  
+
+   * Makefile.am (loongarch_SRCS): Add loongarch_initreg.c.
+   * loongarch_initreg.c: New file.
+   * loongarch_init.c (loongarch_init): Hook set_initial_registers_tid.
+
 2023-04-07  Youling Tang  
 
* Makefile.am (loongarch_SRCS): Add loongarch_cfi.c and 
loongarch_regs.c.
diff --git a/backends/Makefile.am b/backends/Makefile.am
index 2b6f08ce..e7055d7e 100644
--- a/backends/Makefile.am
+++ b/backends/Makefile.am
@@ -97,7 +97,7 @@ csky_SRCS = csky_attrs.c csky_init.c csky_symbol.c csky_cfi.c 
\
csky_regs.c csky_initreg.c csky_corenote.c
 
 loongarch_SRCS = loongarch_init.c loongarch_symbol.c loongarch_cfi.c \
-   loongarch_regs.c
+   loongarch_regs.c loongarch_initreg.c
 
 arc_SRCS = arc_init.c arc_symbol.c
 
diff --git a/backends/loongarch_init.c b/backends/loongarch_init.c
index 7bfaaa7f..9c4b94a0 100644
--- a/backends/loongarch_init.c
+++ b/backends/loongarch_init.c
@@ -51,6 +51,7 @@ loongarch_init (Elf *elf __attribute__ ((unused)),
   /* gcc/config/ #define DWARF_FRAME_REGISTERS.  */
   eh->frame_nregs = 74;
   HOOK (eh, check_special_symbol);
+  HOOK (eh, set_initial_registers_tid);
 
   return eh;
 }
diff --git a/backends/loongarch_initreg.c b/backends/loongarch_initreg.c
new file mode 100644
index ..75497a51
--- /dev/null
+++ b/backends/loongarch_initreg.c
@@ -0,0 +1,91 @@
+/* Fetch live process registers from TID.
+   Copyright (C) 2023 OpenAnolis community LoongArch SIG.
+   Copyright (C) 2023 Loongson Technology Corporation Limted.
+   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 .  */
+
+#ifdef HAVE_CONFIG_H
+# include 
+#endif
+
+#include "system.h"
+#include 
+#if defined __loongarch__ && defined __linux__
+# include 
+# include 
+# include 
+#endif
+
+#define BACKEND loongarch_
+#include "libebl_CPU.h"
+
+bool
+loongarch_set_initial_registers_tid (pid_t tid __attribute__ ((unused)),
+ebl_tid_registers_t *setfunc __attribute__ 
((unused)),
+void *arg __attribute__ ((unused)))
+{
+#if !defined __loongarch__ || !defined __linux__
+  return false;
+#else /* __loongarch__ */
+
+  /* General registers.  */
+  struct user_regs_struct gregs;
+  struct iovec iovec;
+  iovec.iov_base = &gregs;
+  iovec.iov_len = sizeof (gregs);
+  if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iovec) != 0)
+return false;
+
+  /* $r0 is constant 0.  */
+  Dwarf_Word zero = 0;
+  if (! setfunc (0, 1, &zero, arg))
+return false;
+
+  /* $r1-$r31.  */
+  if (! setfunc (1, 32, (Dwarf_Word *) &gregs.regs[1], arg))
+return false;
+
+  /* PC.  */
+  if (! setfunc (-1, 1, (Dwarf_Word *) &gregs.csr_era, arg))
+return false;
+
+  /* Floating-point registers (only 64bits are used).  */
+  struct user_fp_struct fregs;
+  iovec.iov_base = &fregs;
+  iovec.iov_len = sizeof (fregs);
+  if (ptrace (PTRACE_GETREGSET, tid, NT_FPREGSET, &iovec) != 0)
+return false;
+
+  Dwarf_Word dwarf_fregs[32];
+  for (int r = 0; r < 32; r++)
+dwarf_fregs[r] = fregs.fpr[r] & 0x;
+
+  if (! setfunc (32, 32, dwarf_fregs, arg))
+return false;
+
+  return true;
+#endif /* __loongarch__ */
+}
-- 
2.37.1



[PATCH 4/5] backends: Add frame pointer unwinding for LoongArch

2023-04-06 Thread Youling Tang
If we don't find any debug information for a given frame, we usually
cannot unwind any further. However, the binary in question might have
been compiled with frame pointers, in which case we can look up the
well known frame pointer locations in the stack snapshot and use them
to bridge the frames without debug information.

Signed-off-by: Liwei Ge 
Signed-off-by: Youling Tang 
---
 backends/ChangeLog  |  6 +++
 backends/Makefile.am|  3 +-
 backends/loongarch_init.c   |  1 +
 backends/loongarch_unwind.c | 84 +
 4 files changed, 93 insertions(+), 1 deletion(-)
 create mode 100644 backends/loongarch_unwind.c

diff --git a/backends/ChangeLog b/backends/ChangeLog
index 40564ca7..ae385fe0 100644
--- a/backends/ChangeLog
+++ b/backends/ChangeLog
@@ -1,3 +1,9 @@
+2023-04-07  Youling Tang  
+
+   * Makefile.am (loongarch_SRCS): Add loongarch_unwind.c.
+   * loongarch_init.c (loongarch_init): Hook unwind.
+   * loongarch_unwind.c: New file.
+
 2023-04-07  Youling Tang  
 
* Makefile.am (loongarch_SRCS): Add loongarch_retval.c.
diff --git a/backends/Makefile.am b/backends/Makefile.am
index 9277ed59..848e520c 100644
--- a/backends/Makefile.am
+++ b/backends/Makefile.am
@@ -97,7 +97,8 @@ csky_SRCS = csky_attrs.c csky_init.c csky_symbol.c csky_cfi.c 
\
csky_regs.c csky_initreg.c csky_corenote.c
 
 loongarch_SRCS = loongarch_init.c loongarch_symbol.c loongarch_cfi.c \
-   loongarch_regs.c loongarch_initreg.c loongarch_retval.c
+   loongarch_regs.c loongarch_initreg.c loongarch_retval.c \
+   loongarch_unwind.c
 
 arc_SRCS = arc_init.c arc_symbol.c
 
diff --git a/backends/loongarch_init.c b/backends/loongarch_init.c
index 8892a2e6..808ff131 100644
--- a/backends/loongarch_init.c
+++ b/backends/loongarch_init.c
@@ -55,6 +55,7 @@ loongarch_init (Elf *elf __attribute__ ((unused)),
   HOOK (eh, check_special_symbol);
   HOOK (eh, set_initial_registers_tid);
   HOOK (eh, return_value_location);
+  HOOK (eh, unwind);
 
   return eh;
 }
diff --git a/backends/loongarch_unwind.c b/backends/loongarch_unwind.c
new file mode 100644
index ..fb748083
--- /dev/null
+++ b/backends/loongarch_unwind.c
@@ -0,0 +1,84 @@
+/* Get previous frame state for an existing frame state.
+   Copyright (C) 2023 OpenAnolis community LoongArch SIG.
+   Copyright (C) 2023 Loongson Technology Corporation Limited.
+   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 .  */
+
+#ifdef HAVE_CONFIG_H
+# include 
+#endif
+
+#define BACKEND loongarch_
+#define RA_REG 1
+#define SP_REG 3
+#define FP_REG 22
+
+#define RA_OFFSET 8
+#define FP_OFFSET 16
+
+#include "libebl_CPU.h"
+
+/* There was no CFI. Maybe we happen to have a frame pointer and can unwind 
from that?  */
+
+bool
+EBLHOOK(unwind) (Ebl *ebl __attribute__ ((unused)), Dwarf_Addr pc 
__attribute__ ((unused)),
+ ebl_tid_registers_t *setfunc, ebl_tid_registers_get_t 
*getfunc,
+ ebl_pid_memory_read_t *readfunc, void *arg,
+ bool *signal_framep __attribute__ ((unused)))
+{
+  Dwarf_Word fp, ra, sp;
+
+  if (!getfunc(RA_REG, 1, &ra, arg))
+return false;
+
+  if (ra == 0 || !setfunc(-1, 1, &ra, arg))
+return false;
+
+  if (!getfunc(FP_REG, 1, &fp, arg))
+fp = 0;
+
+  if (!getfunc(SP_REG, 1, &sp, arg))
+sp = 0;
+
+  Dwarf_Word newRa, newFp, newSp;
+
+  if (!readfunc(fp - RA_OFFSET, &newRa, arg))
+newRa = 0;
+
+  if (!readfunc(fp - FP_OFFSET, &newFp, arg))
+newFp = 0;
+
+  newSp = fp;
+
+  // These are not fatal if they don't work. They will just prevent unwinding 
at the next frame.
+  setfunc(RA_REG, 1, &newRa, arg);
+  setfunc(FP_REG, 1, &newFp, arg);
+  setfunc(SP_REG, 1, &newSp, arg);
+
+  // If the fp is invalid, we might still have a valid ra.
+  // But if the fp is valid, then the stack should be moving in the right 
direction.
+  return fp == 0 || newSp > sp;
+}
-- 
2.37.1



[PATCH 5/5] backends: Add core_note callback for LoongArch

2023-04-06 Thread Youling Tang
Signed-off-by: Youling Tang 
---
 backends/ChangeLog|   6 ++
 backends/Makefile.am  |   2 +-
 backends/loongarch_corenote.c | 114 ++
 backends/loongarch_init.c |   1 +
 4 files changed, 122 insertions(+), 1 deletion(-)
 create mode 100644 backends/loongarch_corenote.c

diff --git a/backends/ChangeLog b/backends/ChangeLog
index ae385fe0..4575c16b 100644
--- a/backends/ChangeLog
+++ b/backends/ChangeLog
@@ -1,3 +1,9 @@
+2023-04-07  Youling Tang  
+
+   * Makefile.am (loongarch_SRCS): Add loongarch_corenote.c.
+   * loongarch_init.c (loongarch_init): Hook core_note.
+   * loongarch_corenote.c: New file.
+
 2023-04-07  Youling Tang  
 
* Makefile.am (loongarch_SRCS): Add loongarch_unwind.c.
diff --git a/backends/Makefile.am b/backends/Makefile.am
index 848e520c..c8459128 100644
--- a/backends/Makefile.am
+++ b/backends/Makefile.am
@@ -98,7 +98,7 @@ csky_SRCS = csky_attrs.c csky_init.c csky_symbol.c csky_cfi.c 
\
 
 loongarch_SRCS = loongarch_init.c loongarch_symbol.c loongarch_cfi.c \
loongarch_regs.c loongarch_initreg.c loongarch_retval.c \
-   loongarch_unwind.c
+   loongarch_unwind.c loongarch_corenote.c
 
 arc_SRCS = arc_init.c arc_symbol.c
 
diff --git a/backends/loongarch_corenote.c b/backends/loongarch_corenote.c
new file mode 100644
index ..b549edc0
--- /dev/null
+++ b/backends/loongarch_corenote.c
@@ -0,0 +1,114 @@
+/* LoongArch specific core note handling.
+   Copyright (C) 2023 Loongson Technology Corporation Limited.
+   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 .  */
+
+#ifdef HAVE_CONFIG_H
+# include 
+#endif
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#define BACKEND loongarch_
+#include "libebl_CPU.h"
+
+#defineULONG   uint64_t
+#define PID_T  int32_t
+#defineUID_T   uint32_t
+#defineGID_T   uint32_t
+#define ALIGN_ULONG8
+#define ALIGN_PID_T4
+#define ALIGN_UID_T4
+#define ALIGN_GID_T4
+#define TYPE_ULONG ELF_T_XWORD
+#define TYPE_PID_T ELF_T_SWORD
+#define TYPE_UID_T ELF_T_WORD
+#define TYPE_GID_T ELF_T_WORD
+
+#define PRSTATUS_REGS_SIZE (45 * 8)
+
+static const Ebl_Register_Location prstatus_regs[] =
+  {
+{ .offset = 0, .regno = 0, .count = 32, .bits = 64 }, /* r0..r31 */
+  };
+
+#define PRSTATUS_REGSET_ITEMS  \
+  {\
+.name = "orig_a0", .type = ELF_T_XWORD, .format = 'x', \
+.offset = (offsetof (struct EBLHOOK(prstatus), pr_reg) \
+  + 32 * 8),   \
+.group = "register"
\
+  },   \
+  {\
+.name = "csr_era", .type = ELF_T_XWORD, .format = 'x', \
+.offset = (offsetof (struct EBLHOOK(prstatus), pr_reg) \
+  + 33 * 8),   \
+.group = "register",   \
+.pc_register = true
\
+  },   \
+  {\
+.name = "csr_badvaddr", .type = ELF_T_XWORD, .format = 'x',
\
+.offset = (offsetof (struct EBLHOOK(prstatus), pr_reg) \
+  + 34 * 8),   \
+.group = "register"
\
+  },   \
+  {\
+.name = "csr_crmd",

[PATCH 3/5] backends: Add initial return value location support for LoongArch

2023-04-06 Thread Youling Tang
LoongArch ELF ABI specification - Return values:
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html#_return_values

Signed-off-by: Liwei Ge 
Signed-off-by: Youling Tang 
---
 backends/ChangeLog  |   6 ++
 backends/Makefile.am|   2 +-
 backends/loongarch_init.c   |   3 +
 backends/loongarch_retval.c | 202 
 4 files changed, 212 insertions(+), 1 deletion(-)
 create mode 100644 backends/loongarch_retval.c

diff --git a/backends/ChangeLog b/backends/ChangeLog
index 85abc8a5..40564ca7 100644
--- a/backends/ChangeLog
+++ b/backends/ChangeLog
@@ -1,3 +1,9 @@
+2023-04-07  Youling Tang  
+
+   * Makefile.am (loongarch_SRCS): Add loongarch_retval.c.
+   * loongarch_init.c (loongarch_init): Hook return_value_location.
+   * loongarch_retval.c: New file.
+
 2023-04-07  Youling Tang  
 
* Makefile.am (loongarch_SRCS): Add loongarch_initreg.c.
diff --git a/backends/Makefile.am b/backends/Makefile.am
index e7055d7e..9277ed59 100644
--- a/backends/Makefile.am
+++ b/backends/Makefile.am
@@ -97,7 +97,7 @@ csky_SRCS = csky_attrs.c csky_init.c csky_symbol.c csky_cfi.c 
\
csky_regs.c csky_initreg.c csky_corenote.c
 
 loongarch_SRCS = loongarch_init.c loongarch_symbol.c loongarch_cfi.c \
-   loongarch_regs.c loongarch_initreg.c
+   loongarch_regs.c loongarch_initreg.c loongarch_retval.c
 
 arc_SRCS = arc_init.c arc_symbol.c
 
diff --git a/backends/loongarch_init.c b/backends/loongarch_init.c
index 9c4b94a0..8892a2e6 100644
--- a/backends/loongarch_init.c
+++ b/backends/loongarch_init.c
@@ -1,5 +1,7 @@
 /* Initialization of LoongArch specific backend library.
Copyright (C) 2022 Hengqi Chen
+   Copyright (C) 2023 OpenAnolis community LoongArch SIG.
+   Copyright (C) 2023 Loongson Technology Corporation Limted.
This file is part of elfutils.
 
This file is free software; you can redistribute it and/or modify
@@ -52,6 +54,7 @@ loongarch_init (Elf *elf __attribute__ ((unused)),
   eh->frame_nregs = 74;
   HOOK (eh, check_special_symbol);
   HOOK (eh, set_initial_registers_tid);
+  HOOK (eh, return_value_location);
 
   return eh;
 }
diff --git a/backends/loongarch_retval.c b/backends/loongarch_retval.c
new file mode 100644
index ..23b33ed5
--- /dev/null
+++ b/backends/loongarch_retval.c
@@ -0,0 +1,202 @@
+/* Function return value location for Linux/LoongArch ABI.
+   Copyright (C) 2013 Red Hat, Inc.
+   Copyright (C) 2023 OpenAnolis community LoongArch SIG.
+   Copyright (C) 2023 Loongson Technology Corporation Limited.
+
+   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 .  */
+
+#ifdef HAVE_CONFIG_H
+# include 
+#endif
+
+#include 
+#include 
+
+#include 
+#include 
+
+#define BACKEND loongarch_
+#include "libebl_CPU.h"
+
+static int
+dwarf_bytesize_aux (Dwarf_Die *die, Dwarf_Word *sizep)
+{
+  int bits;
+  if (((bits = 8 * dwarf_bytesize (die)) < 0
+   && (bits = dwarf_bitsize (die)) < 0)
+  || bits % 8 != 0)
+return -1;
+
+  *sizep = bits / 8;
+  return 0;
+}
+
+static int
+pass_in_gpr (const Dwarf_Op **locp, Dwarf_Word size)
+{
+  static const Dwarf_Op loc[] =
+{
+  { .atom = DW_OP_reg4 }, { .atom = DW_OP_piece, .number = 8 },
+  { .atom = DW_OP_reg5 }, { .atom = DW_OP_piece, .number = 8 }
+};
+
+  *locp = loc;
+  return size <= 8 ? 1 : 4;
+}
+
+static int
+pass_by_ref (const Dwarf_Op **locp)
+{
+  static const Dwarf_Op loc[] = { { .atom = DW_OP_breg4 } };
+
+  *locp = loc;
+  return 1;
+}
+
+static int
+pass_in_fpr (const Dwarf_Op **locp, Dwarf_Word size)
+{
+  static const Dwarf_Op loc[] =
+{
+  { .atom = DW_OP_regx, .number = 32 },
+  { .atom = DW_OP_piece, .number = 8 },
+  { .atom = DW_OP_regx, .number = 33 },
+  { .atom = DW_OP_piece, .number = 8 }
+};
+
+  *locp = loc;
+  return size <= 8 ? 1 : 4;
+}
+
+int
+loongarch_return_value_location(Dwarf_Die *functypedie,
+const Dwarf_Op **locp)
+{
+  /* Start with the function's type, and get the DW_AT_type attribute,
+ which is the type of