This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 090a52c5fb elf/coredump: alignment stack buffer to 64 to match gdb 
request
090a52c5fb is described below

commit 090a52c5fba57184ba6a968914713280cb6df6b9
Author: chao an <[email protected]>
AuthorDate: Thu May 25 14:51:24 2023 +0800

    elf/coredump: alignment stack buffer to 64 to match gdb request
    
    Signed-off-by: chao an <[email protected]>
---
 binfmt/libelf/libelf_coredump.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/binfmt/libelf/libelf_coredump.c b/binfmt/libelf/libelf_coredump.c
index 8e427d5a9b..006d43a6b0 100644
--- a/binfmt/libelf/libelf_coredump.c
+++ b/binfmt/libelf/libelf_coredump.c
@@ -50,7 +50,10 @@
 #  define ELF_PAGESIZE 1024
 #endif
 
-#define ROUNDUP(x, y)   ((x + (y - 1)) / (y)) * (y)
+#define PROGRAM_ALIGNMENT 64
+
+#define ROUNDUP(x, y)     ((x + (y - 1)) / (y)) * (y)
+#define ROUNDDOWN(x ,y)   (((x) / (y)) * (y))
 
 /****************************************************************************
  * Private Data
@@ -335,7 +338,7 @@ static void elf_emit_note(FAR struct elf_dumpinfo_s *cinfo)
 static void elf_emit_tcb_stack(FAR struct elf_dumpinfo_s *cinfo,
                                FAR struct tcb_s *tcb)
 {
-  FAR void *buf = NULL;
+  uintptr_t buf = 0;
   uintptr_t sp;
   size_t len;
 
@@ -348,26 +351,30 @@ static void elf_emit_tcb_stack(FAR struct elf_dumpinfo_s 
*cinfo,
         {
           len = ((uintptr_t)tcb->stack_base_ptr +
                             tcb->adj_stack_size) - sp;
-          buf = (FAR void *)sp;
+          buf = sp;
         }
 #ifdef CONFIG_STACK_COLORATION
       else
         {
           len = up_check_tcbstack(tcb);
-          buf = (FAR void *)((uintptr_t)tcb->stack_base_ptr +
-                             (tcb->adj_stack_size - len));
+          buf = (uintptr_t)tcb->stack_base_ptr +
+                           (tcb->adj_stack_size - len);
         }
 #endif
     }
 
-  if (buf == NULL)
+  if (buf == 0)
     {
-      buf = (FAR void *)tcb->stack_alloc_ptr;
+      buf = (uintptr_t)tcb->stack_alloc_ptr;
       len = tcb->adj_stack_size +
             (tcb->stack_base_ptr - tcb->stack_alloc_ptr);
     }
 
-  elf_emit(cinfo, buf, len);
+  sp  = ROUNDDOWN(buf, PROGRAM_ALIGNMENT);
+  len = ROUNDUP(len + (buf - sp), PROGRAM_ALIGNMENT);
+  buf = sp;
+
+  elf_emit(cinfo, (FAR void *)buf, len);
 
   /* Align to page */
 
@@ -470,6 +477,11 @@ static void elf_emit_tcb_phdr(FAR struct elf_dumpinfo_s 
*cinfo,
                       (tcb->stack_base_ptr - tcb->stack_alloc_ptr);
     }
 
+  sp = ROUNDDOWN(phdr->p_vaddr, PROGRAM_ALIGNMENT);
+  phdr->p_filesz = ROUNDUP(phdr->p_filesz +
+                           (phdr->p_vaddr - sp), PROGRAM_ALIGNMENT);
+  phdr->p_vaddr  = sp;
+
   phdr->p_type   = PT_LOAD;
   phdr->p_offset = ROUNDUP(*offset, ELF_PAGESIZE);
   phdr->p_paddr  = phdr->p_vaddr;

Reply via email to