You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2020/03/16 13:55:02 UTC

[incubator-nuttx] branch master updated (5a716b7 -> f4e7845)

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

gnutt pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git.


    from 5a716b7  xtensa_user_handler: rsync after wsr.ps
     new 3f80f80  xtensa: Add module/elf definitions
     new d624434  include/elf.h: Add EM_XTENSA
     new 8657305  arch/xtensa/include/elf.h: Initial version
     new 5e1b24f  xtensa: Add modlib boilerplate
     new c7526e5  arch_elf.c: Appease nxstyle errors
     new b262722  Update file paths of copy-and-paste'ed files
     new 2776a5b  Replace a few Gregory Nutt's copyright notices with Apache 2.0
     new e135c93  xtensa: Implement a few relocations
     new bf2cfd4  Add -mtext-section-literals to avoid out of range relocations
     new 03a916a  Kconfig: Add kconfig options for module text allocator
     new 855751b  Introduce instruction memory allocator
     new 061b796  Adapt dlfcn/modlib to use the instruction memory allocator
     new df44909  xtensa/arch_elf.c: Ignore R_XTENSA_NONE
     new f4e7845  esp32: emulate byte access for module text

The 14 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 arch/Kconfig                                       |  14 +
 arch/xtensa/Kconfig                                |   1 +
 include/sys/file.h => arch/xtensa/include/elf.h    |  26 +-
 .../up_lowputs.c => xtensa/include/loadstore.h}    |  32 +--
 arch/xtensa/src/common/xtensa.h                    |   3 +-
 arch/xtensa/src/common/xtensa_assert.c             |   2 +-
 .../src/common/xtensa_loadstore.S}                 |  55 ++--
 arch/xtensa/src/common/xtensa_user_handler.S       |  31 ++-
 arch/xtensa/src/esp32/Make.defs                    |   6 +
 .../src/esp32/esp32_modtext.c}                     |  61 +++--
 arch/xtensa/src/esp32/esp32_user.c                 | 302 +++++++++++++++++++++
 boards/xtensa/esp32/esp32-core/scripts/Make.defs   |  23 ++
 .../xtensa/esp32/esp32-core/scripts/esp32.template |   4 +
 .../xtensa/esp32/esp32-core/scripts/esp32_flash.ld |   4 +
 .../xtensa/esp32/esp32-core/scripts/esp32_iram.ld  |   3 +
 .../esp32/esp32-core}/scripts/gnu-elf.ld           |   4 +-
 include/elf.h                                      |   1 +
 include/nuttx/arch.h                               |  37 +++
 include/nuttx/lib/modlib.h                         |   5 +
 libs/libc/machine/Kconfig                          |   3 +
 libs/libc/machine/Make.defs                        |   3 +
 {net/procfs => libs/libc/machine/xtensa}/Kconfig   |   0
 .../libc/machine/{risc-v/rv64 => xtensa}/Make.defs |   6 +-
 libs/libc/machine/xtensa/arch_elf.c                | 237 ++++++++++++++++
 libs/libc/modlib/Kconfig                           |   1 +
 libs/libc/modlib/modlib_load.c                     |  25 ++
 libs/libc/modlib/modlib_unload.c                   |  12 +
 sched/Kconfig                                      |   1 +
 sched/init/nx_start.c                              |   4 +
 sched/module/mod_insmod.c                          |   5 +
 sched/module/mod_procfs.c                          |  11 +-
 sched/module/mod_rmmod.c                           |  20 +-
 32 files changed, 849 insertions(+), 93 deletions(-)
 copy include/sys/file.h => arch/xtensa/include/elf.h (73%)
 copy arch/{x86_64/src/common/up_lowputs.c => xtensa/include/loadstore.h} (85%)
 copy arch/{x86_64/src/common/up_interruptcontext.c => xtensa/src/common/xtensa_loadstore.S} (76%)
 copy arch/{x86_64/src/common/up_mdelay.c => xtensa/src/esp32/esp32_modtext.c} (75%)
 create mode 100644 arch/xtensa/src/esp32/esp32_user.c
 copy boards/{risc-v/k210/maix-bit => xtensa/esp32/esp32-core}/scripts/gnu-elf.ld (97%)
 copy {net/procfs => libs/libc/machine/xtensa}/Kconfig (100%)
 copy libs/libc/machine/{risc-v/rv64 => xtensa}/Make.defs (89%)
 create mode 100644 libs/libc/machine/xtensa/arch_elf.c


[incubator-nuttx] 06/14: Update file paths of copy-and-paste'ed files

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit b2627225bdb17764267d8f4d6043f3a4cc8879fe
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Fri Mar 6 11:28:03 2020 +0900

    Update file paths of copy-and-paste'ed files
---
 boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld | 2 +-
 libs/libc/machine/xtensa/Make.defs                | 2 +-
 libs/libc/machine/xtensa/arch_elf.c               | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld b/boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld
index 95c7d2e..880537c 100644
--- a/boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld
+++ b/boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld
@@ -1,5 +1,5 @@
 /****************************************************************************
- * binfmt/libelf/gnu-elf.ld
+ * boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld
  *
  *   Copyright (C) 2012 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <gn...@nuttx.org>
diff --git a/libs/libc/machine/xtensa/Make.defs b/libs/libc/machine/xtensa/Make.defs
index fc8c884..a3b2444 100644
--- a/libs/libc/machine/xtensa/Make.defs
+++ b/libs/libc/machine/xtensa/Make.defs
@@ -1,5 +1,5 @@
 ############################################################################
-# libs/libc/machine/arm/armv6-m/Make.defs
+# libs/libc/machine/xtensa/Make.defs
 #
 #   Copyright (C) 2017 Gregory Nutt. All rights reserved.
 #   Author: Gregory Nutt <gn...@nuttx.org>
diff --git a/libs/libc/machine/xtensa/arch_elf.c b/libs/libc/machine/xtensa/arch_elf.c
index 464e9a8..ea385a3 100644
--- a/libs/libc/machine/xtensa/arch_elf.c
+++ b/libs/libc/machine/xtensa/arch_elf.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * libs/libc/machine/arm/armv6-m/arch_elf.c
+ * libs/libc/machine/xtensa/arch_elf.c
  *
  *   Copyright (C) 2013-2014, 2017 Gregory Nutt. All rights reserved.
  *   Author: Gregory Nutt <gn...@nuttx.org>


[incubator-nuttx] 08/14: xtensa: Implement a few relocations

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit e135c938d2466f21225966d04b337922bbfa9da8
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Thu Mar 5 19:11:32 2020 +0900

    xtensa: Implement a few relocations
---
 arch/xtensa/include/elf.h           |  5 ++-
 libs/libc/machine/xtensa/arch_elf.c | 65 +++++++++++++++++++++++++++++++++++--
 2 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/arch/xtensa/include/elf.h b/arch/xtensa/include/elf.h
index 829b717..c46144b 100644
--- a/arch/xtensa/include/elf.h
+++ b/arch/xtensa/include/elf.h
@@ -27,6 +27,9 @@
 
 /* Relocation codes */
 
-#define R_XTENSA_NONE 0
+#define R_XTENSA_NONE       0
+#define R_XTENSA_32         1
+#define R_XTENSA_ASM_EXPAND 11
+#define R_XTENSA_SLOT0_OP   20
 
 #endif /* __ARCH_XTENSA_INCLUDE_ELF_H */
diff --git a/libs/libc/machine/xtensa/arch_elf.c b/libs/libc/machine/xtensa/arch_elf.c
index 9d567fb..84a8848 100644
--- a/libs/libc/machine/xtensa/arch_elf.c
+++ b/libs/libc/machine/xtensa/arch_elf.c
@@ -24,6 +24,7 @@
 
 #include <nuttx/config.h>
 
+#include <assert.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <debug.h>
@@ -43,6 +44,11 @@
  * Private Functions
  ****************************************************************************/
 
+static bool is_l32r(FAR const unsigned char *p)
+{
+  return (p[0] & 0xf) == 1;
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -85,6 +91,7 @@ bool up_checkarch(FAR const Elf32_Ehdr *ehdr)
   /* Verify endian-ness */
 
 #ifdef CONFIG_ENDIAN_BIG
+#error not implemented
   if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB)
 #else
   if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB)
@@ -146,7 +153,7 @@ int up_relocate(FAR const Elf32_Rel *rel, FAR const Elf32_Sym *sym,
       break;
 
     default:
-      berr("ERROR: Unsupported relocation: %d\n", ELF32_R_TYPE(rel->r_info));
+      berr("ERROR: Unsupported relocation: %u\n", relotype);
       return -EINVAL;
     }
 
@@ -156,6 +163,58 @@ int up_relocate(FAR const Elf32_Rel *rel, FAR const Elf32_Sym *sym,
 int up_relocateadd(FAR const Elf32_Rela *rel, FAR const Elf32_Sym *sym,
                    uintptr_t addr)
 {
-  berr("ERROR: RELA relocation not supported\n");
-  return -ENOSYS;
+  unsigned int relotype;
+  unsigned char *p;
+  uint32_t value;
+
+  relotype = ELF32_R_TYPE(rel->r_info);
+  value = sym->st_value + rel->r_addend;
+
+  /* Handle the relocation by relocation type */
+
+  switch (relotype)
+    {
+    case R_XTENSA_32:
+      (*(FAR uint32_t *)addr) += value;
+      break;
+
+    case R_XTENSA_ASM_EXPAND:
+      bwarn("WARNING: Ignoring RELA relocation R_XTENSA_ASM_EXPAND %u\n",
+          relotype);
+      break;
+
+    case R_XTENSA_SLOT0_OP:
+      p = (FAR unsigned char *)addr;
+      if (is_l32r(p))
+        {
+          /* Xtensa ISA:
+           * L32R forms a virtual address by adding the 16-bit one-extended
+           * constant value encoded in the instruction word shifted left by
+           * two to the address of the L32R plus three with the two least
+           * significant bits cleared.
+           */
+
+          uintptr_t base = (addr + 3) & ~3;
+          uint16_t imm = (value - base) >> 2;
+          if (base + (0xfffc0000 | ((uint32_t)imm << 2)) != value)
+            {
+              berr("ERROR: Out of range rellocation at %p\n", p);
+              return -EINVAL;
+            }
+
+          p[1] = imm & 0xff;
+          p[2] = (imm >> 8) & 0xff;
+          break;
+        }
+
+      bwarn("WARNING: Ignoring RELA relocation R_XTENSA_SLOT0_OP %u\n",
+            relotype);
+      break;
+
+    default:
+      berr("ERROR: RELA relocation %u not supported\n", relotype);
+      return -EINVAL;
+    }
+
+  return OK;
 }


[incubator-nuttx] 07/14: Replace a few Gregory Nutt's copyright notices with Apache 2.0

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 2776a5bbea3bba4c851f960c1a136f0f027d7acc
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Fri Mar 6 11:33:16 2020 +0900

    Replace a few Gregory Nutt's copyright notices with Apache 2.0
    
    The text was copied from sched/sched/sched_getcpu.c.
---
 boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld | 39 +++++++----------------
 libs/libc/machine/xtensa/Make.defs                | 39 +++++++----------------
 libs/libc/machine/xtensa/arch_elf.c               | 39 +++++++----------------
 3 files changed, 36 insertions(+), 81 deletions(-)

diff --git a/boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld b/boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld
index 880537c..e867083 100644
--- a/boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld
+++ b/boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld
@@ -1,35 +1,20 @@
 /****************************************************************************
  * boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld
  *
- *   Copyright (C) 2012 Gregory Nutt. All rights reserved.
- *   Author: Gregory Nutt <gn...@nuttx.org>
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
+ *   http://www.apache.org/licenses/LICENSE-2.0
  *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- * 3. Neither the name NuttX nor the names of its contributors may be
- *    used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
  *
  ****************************************************************************/
 
diff --git a/libs/libc/machine/xtensa/Make.defs b/libs/libc/machine/xtensa/Make.defs
index a3b2444..8f33a82 100644
--- a/libs/libc/machine/xtensa/Make.defs
+++ b/libs/libc/machine/xtensa/Make.defs
@@ -1,35 +1,20 @@
 ############################################################################
 # libs/libc/machine/xtensa/Make.defs
 #
-#   Copyright (C) 2017 Gregory Nutt. All rights reserved.
-#   Author: Gregory Nutt <gn...@nuttx.org>
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
 #
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions
-# are met:
+#   http://www.apache.org/licenses/LICENSE-2.0
 #
-# 1. Redistributions of source code must retain the above copyright
-#    notice, this list of conditions and the following disclaimer.
-# 2. Redistributions in binary form must reproduce the above copyright
-#    notice, this list of conditions and the following disclaimer in
-#    the documentation and/or other materials provided with the
-#    distribution.
-# 3. Neither the name NuttX nor the names of its contributors may be
-#    used to endorse or promote products derived from this software
-#    without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
-# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
-# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
-# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
-# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
-# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
-# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-# POSSIBILITY OF SUCH DAMAGE.
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
 #
 ############################################################################
 
diff --git a/libs/libc/machine/xtensa/arch_elf.c b/libs/libc/machine/xtensa/arch_elf.c
index ea385a3..9d567fb 100644
--- a/libs/libc/machine/xtensa/arch_elf.c
+++ b/libs/libc/machine/xtensa/arch_elf.c
@@ -1,35 +1,20 @@
 /****************************************************************************
  * libs/libc/machine/xtensa/arch_elf.c
  *
- *   Copyright (C) 2013-2014, 2017 Gregory Nutt. All rights reserved.
- *   Author: Gregory Nutt <gn...@nuttx.org>
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
+ *   http://www.apache.org/licenses/LICENSE-2.0
  *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in
- *    the documentation and/or other materials provided with the
- *    distribution.
- * 3. Neither the name NuttX nor the names of its contributors may be
- *    used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
- * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
  *
  ****************************************************************************/
 


[incubator-nuttx] 13/14: xtensa/arch_elf.c: Ignore R_XTENSA_NONE

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit df44909b30a5a864777477a202027e78cad2768d
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Tue Mar 10 18:46:06 2020 +0900

    xtensa/arch_elf.c: Ignore R_XTENSA_NONE
    
    Now examples/sotest passes on qemu.
---
 libs/libc/machine/xtensa/arch_elf.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/libs/libc/machine/xtensa/arch_elf.c b/libs/libc/machine/xtensa/arch_elf.c
index 84a8848..7d63840 100644
--- a/libs/libc/machine/xtensa/arch_elf.c
+++ b/libs/libc/machine/xtensa/arch_elf.c
@@ -167,13 +167,30 @@ int up_relocateadd(FAR const Elf32_Rela *rel, FAR const Elf32_Sym *sym,
   unsigned char *p;
   uint32_t value;
 
+  /* All relocations except NONE depend upon having valid symbol
+   * information.
+   */
+
   relotype = ELF32_R_TYPE(rel->r_info);
-  value = sym->st_value + rel->r_addend;
+  if (sym == NULL)
+    {
+      if (relotype != R_XTENSA_NONE)
+        {
+          return -EINVAL;
+        }
+    }
+  else
+    {
+      value = sym->st_value + rel->r_addend;
+    }
 
   /* Handle the relocation by relocation type */
 
   switch (relotype)
     {
+    case R_XTENSA_NONE:
+      break;
+
     case R_XTENSA_32:
       (*(FAR uint32_t *)addr) += value;
       break;


[incubator-nuttx] 05/14: arch_elf.c: Appease nxstyle errors

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit c7526e551e1ce2060de86dbb88a83d6451c84ee8
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Tue Mar 10 15:06:34 2020 +0900

    arch_elf.c: Appease nxstyle errors
---
 libs/libc/machine/xtensa/arch_elf.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/libs/libc/machine/xtensa/arch_elf.c b/libs/libc/machine/xtensa/arch_elf.c
index 97fdbc2..464e9a8 100644
--- a/libs/libc/machine/xtensa/arch_elf.c
+++ b/libs/libc/machine/xtensa/arch_elf.c
@@ -92,7 +92,8 @@ bool up_checkarch(FAR const Elf32_Ehdr *ehdr)
 
   if (ehdr->e_ident[EI_CLASS] != ELFCLASS32)
     {
-      berr("ERROR: Need 32-bit objects: e_ident[EI_CLASS]=%02x\n", ehdr->e_ident[EI_CLASS]);
+      berr("ERROR: Need 32-bit objects: e_ident[EI_CLASS]=%02x\n",
+           ehdr->e_ident[EI_CLASS]);
       return false;
     }
 
@@ -104,7 +105,8 @@ bool up_checkarch(FAR const Elf32_Ehdr *ehdr)
   if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB)
 #endif
     {
-      berr("ERROR: Wrong endian-ness: e_ident[EI_DATA]=%02x\n", ehdr->e_ident[EI_DATA]);
+      berr("ERROR: Wrong endian-ness: e_ident[EI_DATA]=%02x\n",
+           ehdr->e_ident[EI_DATA]);
       return false;
     }
 


[incubator-nuttx] 12/14: Adapt dlfcn/modlib to use the instruction memory allocator

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 061b796d472e8858a0e09aae4ec1b2de7b078f4a
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Fri Mar 6 17:05:25 2020 +0900

    Adapt dlfcn/modlib to use the instruction memory allocator
---
 include/nuttx/lib/modlib.h       |  5 +++++
 libs/libc/modlib/Kconfig         |  1 +
 libs/libc/modlib/modlib_load.c   | 25 +++++++++++++++++++++++++
 libs/libc/modlib/modlib_unload.c | 12 ++++++++++++
 sched/Kconfig                    |  1 +
 sched/init/nx_start.c            |  4 ++++
 sched/module/mod_insmod.c        |  5 +++++
 sched/module/mod_procfs.c        | 11 ++++++++++-
 sched/module/mod_rmmod.c         | 20 +++++++++++++++-----
 9 files changed, 78 insertions(+), 6 deletions(-)

diff --git a/include/nuttx/lib/modlib.h b/include/nuttx/lib/modlib.h
index 3271a94..baf969e 100644
--- a/include/nuttx/lib/modlib.h
+++ b/include/nuttx/lib/modlib.h
@@ -172,7 +172,12 @@ struct module_s
   mod_initializer_t initializer;       /* Module initializer function */
 #endif
   struct mod_info_s modinfo;           /* Module information */
+#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
+  FAR void *textalloc;                 /* Allocated kernel text memory */
+  FAR void *dataalloc;                 /* Allocated kernel memory */
+#else
   FAR void *alloc;                     /* Allocated kernel memory */
+#endif
 #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
   size_t textsize;                     /* Size of the kernel .text memory allocation */
   size_t datasize;                     /* Size of the kernel .bss/.data memory allocation */
diff --git a/libs/libc/modlib/Kconfig b/libs/libc/modlib/Kconfig
index c03fdbb..4f9e78d 100644
--- a/libs/libc/modlib/Kconfig
+++ b/libs/libc/modlib/Kconfig
@@ -7,6 +7,7 @@ config LIBC_MODLIB
 	bool
 	default n
 	select LIBC_ARCH_ELF
+	select ARCH_USE_MODULE_TEXT if ARCH_HAVE_MODULE_TEXT
 
 menu "Module library configuration"
 	depends on LIBC_MODLIB
diff --git a/libs/libc/modlib/modlib_load.c b/libs/libc/modlib/modlib_load.c
index 8e16312..ecae40d 100644
--- a/libs/libc/modlib/modlib_load.c
+++ b/libs/libc/modlib/modlib_load.c
@@ -262,6 +262,30 @@ int modlib_load(FAR struct mod_loadinfo_s *loadinfo)
 
   /* Allocate memory to hold the ELF image */
 
+#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
+  if (loadinfo->textsize > 0)
+    {
+      loadinfo->textalloc = (uintptr_t)
+                            up_module_text_alloc(loadinfo->textsize);
+      if (!loadinfo->textalloc)
+        {
+          berr("ERROR: Failed to allocate memory for the module text\n");
+          ret = -ENOMEM;
+          goto errout_with_buffers;
+        }
+    }
+
+  if (loadinfo->datasize > 0)
+    {
+      loadinfo->datastart = (uintptr_t)lib_malloc(loadinfo->datasize);
+      if (!loadinfo->datastart)
+        {
+          berr("ERROR: Failed to allocate memory for the module data\n");
+          ret = -ENOMEM;
+          goto errout_with_buffers;
+        }
+    }
+#else
   loadinfo->textalloc = (uintptr_t)lib_malloc(loadinfo->textsize +
                                               loadinfo->datasize);
   if (!loadinfo->textalloc)
@@ -272,6 +296,7 @@ int modlib_load(FAR struct mod_loadinfo_s *loadinfo)
     }
 
   loadinfo->datastart = loadinfo->textalloc + loadinfo->textsize;
+#endif
 
   /* Load ELF section data into memory */
 
diff --git a/libs/libc/modlib/modlib_unload.c b/libs/libc/modlib/modlib_unload.c
index 6288971..8d31901 100644
--- a/libs/libc/modlib/modlib_unload.c
+++ b/libs/libc/modlib/modlib_unload.c
@@ -73,10 +73,22 @@ int modlib_unload(struct mod_loadinfo_s *loadinfo)
 
   /* Release memory holding the relocated ELF image */
 
+#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
+  if (loadinfo->textalloc != 0)
+    {
+      up_module_text_free((FAR void *)loadinfo->textalloc);
+    }
+
+  if (loadinfo->datastart != 0)
+    {
+      lib_free((FAR void *)loadinfo->datastart);
+    }
+#else
   if (loadinfo->textalloc != 0)
     {
       lib_free((FAR void *)loadinfo->textalloc);
     }
+#endif
 
   /* Clear out all indications of the allocated address environment */
 
diff --git a/sched/Kconfig b/sched/Kconfig
index 5093b08..5cebb24 100644
--- a/sched/Kconfig
+++ b/sched/Kconfig
@@ -1572,6 +1572,7 @@ config MODULE
 	bool "Enable loadable OS modules"
 	default n
 	select LIBC_MODLIB
+	select ARCH_USE_MODULE_TEXT if ARCH_HAVE_MODULE_TEXT
 	---help---
 		Enable support for loadable OS modules.  Default: n
 
diff --git a/sched/init/nx_start.c b/sched/init/nx_start.c
index cffd033..bf8f724 100644
--- a/sched/init/nx_start.c
+++ b/sched/init/nx_start.c
@@ -599,6 +599,10 @@ void nx_start(void)
       kmm_initialize(heap_start, heap_size);
 #endif
 
+#ifdef CONFIG_ARCH_USE_MODULE_TEXT
+    up_module_text_init();
+#endif
+
 #ifdef CONFIG_MM_PGALLOC
       /* If there is a page allocator in the configuration, then get the page
        * heap information from the platform-specific code and configure the
diff --git a/sched/module/mod_insmod.c b/sched/module/mod_insmod.c
index 3d0d266..26bdb45 100644
--- a/sched/module/mod_insmod.c
+++ b/sched/module/mod_insmod.c
@@ -237,7 +237,12 @@ FAR void *insmod(FAR const char *filename, FAR const char *modname)
 
   /* Save the load information */
 
+#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
+  modp->textalloc   = (FAR void *)loadinfo.textalloc;
+  modp->dataalloc   = (FAR void *)loadinfo.datastart;
+#else
   modp->alloc       = (FAR void *)loadinfo.textalloc;
+#endif
 #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
   modp->textsize    = loadinfo.textsize;
   modp->datasize    = loadinfo.datasize;
diff --git a/sched/module/mod_procfs.c b/sched/module/mod_procfs.c
index b740dee..8aece06 100644
--- a/sched/module/mod_procfs.c
+++ b/sched/module/mod_procfs.c
@@ -152,9 +152,18 @@ static int modprocfs_callback(FAR struct module_s *modp, FAR void *arg)
                       "%s,%p,%p,%p,%u,%p,%lu,%p,%lu\n",
                       modp->modname, modp->initializer,
                       modp->modinfo.uninitializer, modp->modinfo.arg,
-                      modp->modinfo.nexports, modp->alloc,
+                      modp->modinfo.nexports,
+#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
+                      modp->textalloc,
+#else
+                      modp->alloc,
+#endif
                       (unsigned long)modp->textsize,
+#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
+                      (FAR uint8_t *)modp->dataalloc,
+#else
                       (FAR uint8_t *)modp->alloc + modp->textsize,
+#endif
                       (unsigned long)modp->datasize);
   copysize = procfs_memcpy(priv->line, linesize, priv->buffer,
                            priv->remaining, &priv->offset);
diff --git a/sched/module/mod_rmmod.c b/sched/module/mod_rmmod.c
index 35d34a4..cdb2112 100644
--- a/sched/module/mod_rmmod.c
+++ b/sched/module/mod_rmmod.c
@@ -128,15 +128,25 @@ int rmmod(FAR void *handle)
 
   /* Release resources held by the module */
 
+#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
+  if (modp->textalloc != NULL || modp->dataalloc != NULL)
+#else
   if (modp->alloc != NULL)
+#endif
     {
-      /* Free the module memory */
-
+      /* Free the module memory
+       * and nullify so that the memory cannot be freed again
+       */
+
+#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
+      up_module_text_free((FAR void *)modp->textalloc);
+      kmm_free((FAR void *)modp->dataalloc);
+      modp->textalloc = NULL;
+      modp->dataalloc = NULL;
+#else
       kmm_free((FAR void *)modp->alloc);
-
-      /* Nullify so that the memory cannot be freed again */
-
       modp->alloc = NULL;
+#endif
 #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
       modp->textsize  = 0;
       modp->datasize  = 0;


[incubator-nuttx] 10/14: Kconfig: Add kconfig options for module text allocator

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 03a916acb8b5d3656bce055808fb3f0abc31d606
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Mon Mar 9 13:03:49 2020 +0900

    Kconfig: Add kconfig options for module text allocator
    
    Enable it for ESP32.
---
 arch/Kconfig        | 14 ++++++++++++++
 arch/xtensa/Kconfig |  1 +
 2 files changed, 15 insertions(+)

diff --git a/arch/Kconfig b/arch/Kconfig
index aaf1c61..15421c6 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -213,6 +213,10 @@ config ARCH_NEED_ADDRENV_MAPPING
 	bool
 	default n
 
+config ARCH_HAVE_MODULE_TEXT
+	bool "Special memory region for dynamic code loading"
+	default n
+
 config ARCH_HAVE_MULTICPU
 	bool
 	default n
@@ -321,6 +325,16 @@ config ARCH_USE_MPU
 		is enabled by other, platform-specific logic.  In those cases, this
 		selection will always be forced.
 
+config ARCH_USE_MODULE_TEXT
+	bool "Enable module text allocator"
+	default n
+	depends on ARCH_HAVE_MODULE_TEXT
+	---help---
+		This option enable architecture-sepecific memory allocator
+		for dynamic code loading. For example, ESP32 has a separate memory
+		regions for instruction and data and the memory region used for
+		usual malloc doesn't work for instruction.
+
 menuconfig ARCH_ADDRENV
 	bool "Address environments"
 	default n
diff --git a/arch/xtensa/Kconfig b/arch/xtensa/Kconfig
index 1bedd53..5569e1b 100644
--- a/arch/xtensa/Kconfig
+++ b/arch/xtensa/Kconfig
@@ -14,6 +14,7 @@ config ARCH_CHIP_ESP32
 	select ARCH_FAMILY_LX6
 	select XTENSA_HAVE_INTERRUPTS
 	select ARCH_HAVE_MULTICPU
+	select ARCH_HAVE_MODULE_TEXT
 	select ARCH_TOOLCHAIN_GNU
 	select ARCH_GLOBAL_IRQDISABLE
 	---help---


[incubator-nuttx] 11/14: Introduce instruction memory allocator

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 855751b534403877e31c286a672d696826165c05
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Fri Mar 6 14:48:15 2020 +0900

    Introduce instruction memory allocator
    
    Necessary for dlfcn etc on ESP32, which has separate memory regions
    for instruction and data.
    
    known issues/todo
     * consider something similar to dual heaps for PROTOECTED
     * consider to adapt binfmt as well
---
 arch/xtensa/src/esp32/Make.defs                    |  4 +
 arch/xtensa/src/esp32/esp32_modtext.c              | 86 ++++++++++++++++++++++
 .../xtensa/esp32/esp32-core/scripts/esp32.template |  4 +
 .../xtensa/esp32/esp32-core/scripts/esp32_flash.ld |  4 +
 .../xtensa/esp32/esp32-core/scripts/esp32_iram.ld  |  3 +
 include/nuttx/arch.h                               | 37 ++++++++++
 6 files changed, 138 insertions(+)

diff --git a/arch/xtensa/src/esp32/Make.defs b/arch/xtensa/src/esp32/Make.defs
index 2d8d84b..d8c71d8 100644
--- a/arch/xtensa/src/esp32/Make.defs
+++ b/arch/xtensa/src/esp32/Make.defs
@@ -106,3 +106,7 @@ endif
 ifeq ($(CONFIG_ESP32_UART),y)
 CMN_CSRCS += esp32_serial.c
 endif
+
+ifeq ($(CONFIG_ARCH_USE_MODULE_TEXT),y)
+CHIP_CSRCS += esp32_modtext.c
+endif
diff --git a/arch/xtensa/src/esp32/esp32_modtext.c b/arch/xtensa/src/esp32/esp32_modtext.c
new file mode 100644
index 0000000..fc7d78d
--- /dev/null
+++ b/arch/xtensa/src/esp32/esp32_modtext.c
@@ -0,0 +1,86 @@
+/****************************************************************************
+ * arch/xtensa/src/esp32/esp32_modtext.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/arch.h>
+#include <nuttx/mm/mm.h>
+
+#include <sys/types.h>
+#include <debug.h>
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+extern uint32_t _smodtext;
+extern uint32_t _emodtext;
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+struct mm_heap_s g_module_text;
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: up_module_text_init
+ *
+ * Description:
+ *   Initialize the module text allocator
+ *
+ ****************************************************************************/
+
+void up_module_text_init()
+{
+  mm_initialize(&g_module_text, &_smodtext, &_emodtext - &_smodtext);
+}
+
+/****************************************************************************
+ * Name: up_module_text_alloc
+ *
+ * Description:
+ *   Allocate memory for module text.
+ *
+ ****************************************************************************/
+
+FAR void *up_module_text_alloc(size_t size)
+{
+  return mm_malloc(&g_module_text, size);
+}
+
+/****************************************************************************
+ * Name: up_module_text_free
+ *
+ * Description:
+ *   Free memory for module text.
+ *
+ ****************************************************************************/
+
+void up_module_text_free(FAR void *p)
+{
+  return mm_free(&g_module_text, p);
+}
diff --git a/boards/xtensa/esp32/esp32-core/scripts/esp32.template b/boards/xtensa/esp32/esp32-core/scripts/esp32.template
index e7099a0..7bb7395 100644
--- a/boards/xtensa/esp32/esp32-core/scripts/esp32.template
+++ b/boards/xtensa/esp32/esp32-core/scripts/esp32.template
@@ -60,3 +60,7 @@ MEMORY
 /* Heap ends at top of dram0_0_seg */
 
 _eheap = 0x40000000 - CONFIG_ESP32_TRACEMEM_RESERVE_DRAM;
+
+/* Module text area ends at top of dram0_0_seg */
+
+_emodtext = 0x400a0000;
diff --git a/boards/xtensa/esp32/esp32-core/scripts/esp32_flash.ld b/boards/xtensa/esp32/esp32-core/scripts/esp32_flash.ld
index 3cc70c1..bccf2aa 100644
--- a/boards/xtensa/esp32/esp32-core/scripts/esp32_flash.ld
+++ b/boards/xtensa/esp32/esp32-core/scripts/esp32_flash.ld
@@ -59,6 +59,10 @@ SECTIONS
     *libpp.a:(.literal .text .literal.* .text.*)
     *libhal.a:(.literal .text .literal.* .text.*)
     _iram_text_end = ABSOLUTE(.);
+
+    /* Module text area starts at the end of iram0_0_seg */
+
+    _smodtext = ABSOLUTE(.);
   } > iram0_0_seg
 
   /* Shared RAM */
diff --git a/boards/xtensa/esp32/esp32-core/scripts/esp32_iram.ld b/boards/xtensa/esp32/esp32-core/scripts/esp32_iram.ld
index 61b6fa6..54dcfef 100644
--- a/boards/xtensa/esp32/esp32-core/scripts/esp32_iram.ld
+++ b/boards/xtensa/esp32/esp32-core/scripts/esp32_iram.ld
@@ -70,6 +70,9 @@ SECTIONS
     _text_end = ABSOLUTE(.);
     _etext = .;
 
+    /* Module text area starts at the end of iram0_0_seg */
+
+    _smodtext = ABSOLUTE(.);
   } > iram0_0_seg
 
   /* Shared RAM */
diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h
index 815a0f6..6203a99 100644
--- a/include/nuttx/arch.h
+++ b/include/nuttx/arch.h
@@ -160,6 +160,7 @@ EXTERN volatile bool g_rtc_enabled;
  */
 
 /* EXTERN const irq_mapped_t g_irqmap[NR_IRQS]; */
+
 #endif
 
 /****************************************************************************
@@ -733,6 +734,42 @@ uintptr_t pgalloc(uintptr_t brkaddr, unsigned int npages);
 #endif
 
 /****************************************************************************
+ * Name: up_module_text_init
+ *
+ * Description:
+ *   Initialize the module text allocator
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
+void up_module_text_init(void);
+#endif
+
+/****************************************************************************
+ * Name: up_module_text_alloc
+ *
+ * Description:
+ *   Allocate memory for module text.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
+FAR void *up_module_text_alloc(size_t size);
+#endif
+
+/****************************************************************************
+ * Name: up_module_text_free
+ *
+ * Description:
+ *   Free memory for module text.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_ARCH_USE_MODULE_TEXT)
+void up_module_text_free(FAR void *p);
+#endif
+
+/****************************************************************************
  * Name: up_sched_have_garbage and up_sched_garbage_collection
  *
  * Description:


[incubator-nuttx] 14/14: esp32: emulate byte access for module text

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit f4e7845b8577af9ff76d66b81f35374760809ee2
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Mon Mar 16 15:07:36 2020 +0900

    esp32: emulate byte access for module text
    
    Tested on ESP-EYE.
---
 arch/xtensa/include/loadstore.h              |  51 +++++
 arch/xtensa/src/common/xtensa.h              |   3 +-
 arch/xtensa/src/common/xtensa_assert.c       |   2 +-
 arch/xtensa/src/common/xtensa_loadstore.S    |  76 +++++++
 arch/xtensa/src/common/xtensa_user_handler.S |  31 ++-
 arch/xtensa/src/esp32/Make.defs              |   2 +
 arch/xtensa/src/esp32/esp32_user.c           | 302 +++++++++++++++++++++++++++
 7 files changed, 459 insertions(+), 8 deletions(-)

diff --git a/arch/xtensa/include/loadstore.h b/arch/xtensa/include/loadstore.h
new file mode 100644
index 0000000..69d219e
--- /dev/null
+++ b/arch/xtensa/include/loadstore.h
@@ -0,0 +1,51 @@
+/****************************************************************************
+ * arch/xtensa/include/loadstore.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_XTENSA_INCLUDE_LOADSTORE_H
+#define __ARCH_XTENSA_INCLUDE_LOADSTORE_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Types
+ ****************************************************************************/
+
+/****************************************************************************
+ * Inline functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+uint32_t l32i(const uint32_t *p);
+void s32i(uint32_t *p, uint32_t value);
+
+#endif /* __ARCH_XTENSA_INCLUDE_LOADSTORE_H */
diff --git a/arch/xtensa/src/common/xtensa.h b/arch/xtensa/src/common/xtensa.h
index ab6c1fb..bff7093 100644
--- a/arch/xtensa/src/common/xtensa.h
+++ b/arch/xtensa/src/common/xtensa.h
@@ -267,7 +267,8 @@ uint32_t *xtensa_irq_dispatch(int irq, uint32_t *regs);
 uint32_t xtensa_enable_cpuint(uint32_t *shadow, uint32_t intmask);
 uint32_t xtensa_disable_cpuint(uint32_t *shadow, uint32_t intmask);
 void xtensa_panic(int xptcode, uint32_t *regs) noreturn_function;
-void xtensa_user(int exccause, uint32_t *regs) noreturn_function;
+void xtensa_user_panic(int exccause, uint32_t *regs) noreturn_function;
+uint32_t *xtensa_user(int exccause, uint32_t *regs);
 
 /* Software interrupt handler */
 
diff --git a/arch/xtensa/src/common/xtensa_assert.c b/arch/xtensa/src/common/xtensa_assert.c
index e18838b..dcf9382 100644
--- a/arch/xtensa/src/common/xtensa_assert.c
+++ b/arch/xtensa/src/common/xtensa_assert.c
@@ -314,7 +314,7 @@ void xtensa_panic(int xptcode, uint32_t *regs)
  *
  ****************************************************************************/
 
-void xtensa_user(int exccause, uint32_t *regs)
+void xtensa_user_panic(int exccause, uint32_t *regs)
 {
 #if CONFIG_TASK_NAME_SIZE > 0 && defined(CONFIG_DEBUG_ALERT)
   struct tcb_s *rtcb = running_task();
diff --git a/arch/xtensa/src/common/xtensa_loadstore.S b/arch/xtensa/src/common/xtensa_loadstore.S
new file mode 100644
index 0000000..b14fdfe
--- /dev/null
+++ b/arch/xtensa/src/common/xtensa_loadstore.S
@@ -0,0 +1,76 @@
+/****************************************************************************
+ * arch/xtensa/src/common/xtensa_loadstore.S
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include "xtensa_abi.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: l32i
+ *
+ * Description:
+ *   Execute a L32I instruction
+ *
+ * Entry Conditions:
+ *   A2     - a pointer
+ *
+ ****************************************************************************/
+
+    .global l32i
+    .type   l32i, @function
+    .align  4
+
+l32i:
+    ENTRY0
+
+    l32i	a2, a2, 0
+
+    RET0
+
+/****************************************************************************
+ * Name: s32i
+ *
+ * Description:
+ *   Execute a S32I instruction
+ *
+ * Entry Conditions:
+ *   A2     - a pointer
+ *   A3     - a value to store
+ *
+ ****************************************************************************/
+
+    .global s32i
+    .type   s32i, @function
+    .align  4
+
+s32i:
+    ENTRY0
+
+    s32i	a3, a2, 0
+
+    RET0
diff --git a/arch/xtensa/src/common/xtensa_user_handler.S b/arch/xtensa/src/common/xtensa_user_handler.S
index b8e9adb..9e7b44c 100644
--- a/arch/xtensa/src/common/xtensa_user_handler.S
+++ b/arch/xtensa/src/common/xtensa_user_handler.S
@@ -238,11 +238,30 @@ _xtensa_user_handler:
 	rsr		a6, EXCCAUSE					/* Argument 1 (a6) = EXCCAUSE */
 	mov		a7, sp							/* Argument 2 (a7) = pointer to register save area */
 	call4	xtensa_user						/* Call xtensa_user */
+	mov		a2, a6
 #endif
 
-	/* xtensa_user should not return */
+	/* Restore registers in preparation to return from interrupt */
 
-1:	j		1b
+	call0	_xtensa_context_restore			/* (Preserves a2) */
+
+	/* Restore only level-specific regs (the rest were already restored) */
+
+	l32i	a0, a2, (4 * REG_PS)			/* Retrieve interruptee's PS */
+	wsr		a0, PS
+	l32i	a0, a2, (4 * REG_PC)			/* Retrieve interruptee's PC */
+	wsr		a0, EPC_1
+	l32i	a0, a2, (4 * REG_A0)			/* Retrieve interruptee's A0 */
+	l32i	sp, a2, (4 * REG_A1)			/* Remove interrupt stack frame */
+	l32i	a2, a2, (4 * REG_A2)			/* Retrieve interruptee's A2 */
+	rsync									/* Ensure PS and EPC written */
+
+	/* Return from exception. RFE returns from either the UserExceptionVector
+	 * or the KernelExceptionVector.  RFE sets PS.EXCM back to 0, and then
+	 * jumps to the address in EPC[1]. PS.UM and PS.WOE are left unchanged.
+	 */
+
+	rfe
 
 /****************************************************************************
  * Name: _xtensa_syscall_handler
@@ -464,21 +483,21 @@ _xtensa_coproc_handler:
 #endif
 	wsr		a0, PS
 
-	/* Call xtensa_user, passing both the EXCCAUSE and a pointer to the
+	/* Call xtensa_user_panic, passing both the EXCCAUSE and a pointer to the
 	 * beginning of the register save area.
 	 */
 
 #ifdef __XTENSA_CALL0_ABI__
 	rsr		a2, EXCCAUSE					/* Argument 1 (a2) = EXCCAUSE */
 	mov		a3, sp							/* Argument 2 (a2) = pointer to register save area */
-	calx0	xtensa_user						/* Call xtensa_user */
+	calx0	xtensa_user_panic				/* Call xtensa_user_panic */
 #else
 	rsr		a6, EXCCAUSE					/* Argument 1 (a2) = EXCCAUSE */
 	mov		a7, sp							/* Argument 2 (a2) = pointer to register save area */
-	call4	xtensa_user						/* Call xtensa_user */
+	call4	xtensa_user_panic				/* Call xtensa_user_panic */
 #endif
 
-	/* xtensa_user should not return */
+	/* xtensa_user_panic should not return */
 
 1:	j		1b
 
diff --git a/arch/xtensa/src/esp32/Make.defs b/arch/xtensa/src/esp32/Make.defs
index d8c71d8..3c4a036 100644
--- a/arch/xtensa/src/esp32/Make.defs
+++ b/arch/xtensa/src/esp32/Make.defs
@@ -95,6 +95,7 @@ CHIP_ASRCS  =
 CHIP_CSRCS  = esp32_allocateheap.c esp32_clockconfig.c esp32_cpuint.c
 CHIP_CSRCS += esp32_gpio.c esp32_intdecode.c esp32_irq.c esp32_region.c
 CHIP_CSRCS += esp32_timerisr.c
+CHIP_CSRCS += esp32_user.c
 
 # Configuration-dependent ESP32 files
 
@@ -109,4 +110,5 @@ endif
 
 ifeq ($(CONFIG_ARCH_USE_MODULE_TEXT),y)
 CHIP_CSRCS += esp32_modtext.c
+CMN_ASRCS += xtensa_loadstore.S
 endif
diff --git a/arch/xtensa/src/esp32/esp32_user.c b/arch/xtensa/src/esp32/esp32_user.c
new file mode 100644
index 0000000..5de680c
--- /dev/null
+++ b/arch/xtensa/src/esp32/esp32_user.c
@@ -0,0 +1,302 @@
+/****************************************************************************
+ * arch/xtensa/src/esp32/esp32_user.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/arch.h>
+
+#include <arch/loadstore.h>
+#include <arch/xtensa/core.h>
+
+#include <sys/types.h>
+#include <debug.h>
+
+#include "xtensa.h"
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+#ifdef CONFIG_ARCH_USE_MODULE_TEXT
+extern uint32_t _smodtext;
+extern uint32_t _emodtext;
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+#ifdef CONFIG_ARCH_USE_MODULE_TEXT
+#ifdef CONFIG_ENDIAN_BIG
+#error not implemented
+#endif
+#if defined(CONFIG_BUILD_PROTECTED) || defined (CONFIG_BUILD_KERNEL)
+#error permission check not implemented
+#endif
+
+/****************************************************************************
+ * Name: load_uint8
+ *
+ * Description:
+ *   Fetch a byte using 32-bit aligned access.
+ *
+ ****************************************************************************/
+
+static uint8_t load_uint8(const uint8_t *p)
+{
+  const uint32_t *aligned;
+  uint32_t value;
+  unsigned int offset;
+
+  aligned = (const uint32_t *)(((uintptr_t)p) & ~3);
+  value = l32i(aligned);
+  offset = ((uintptr_t)p) & 3;
+  switch (offset)
+    {
+      case 0:
+        return value & 0xff;
+      case 1:
+        return (value >> 8) & 0xff;
+      case 2:
+        return (value >> 16) & 0xff;
+      case 3:
+        return (value >> 24) & 0xff;
+    }
+
+  /* not reached */
+
+  PANIC();
+}
+
+/****************************************************************************
+ * Name: store_uint8
+ *
+ * Description:
+ *   Store a byte using 32-bit aligned access.
+ *
+ ****************************************************************************/
+
+static void store_uint8(uint8_t *p, uint8_t v)
+{
+  uint32_t *aligned;
+  uint32_t value;
+  unsigned int offset;
+
+  aligned = (uint32_t *)(((uintptr_t)p) & ~3);
+  value = l32i(aligned);
+  offset = ((uintptr_t)p) & 3;
+  switch (offset)
+    {
+      case 0:
+        value = (value & 0xffffff00) | v;
+        break;
+      case 1:
+        value = (value & 0xffff00ff) | (v << 8);
+        break;
+      case 2:
+        value = (value & 0xff00ffff) | (v << 16);
+        break;
+      case 3:
+        value = (value & 0x00ffffff) | (v << 24);
+        break;
+    }
+
+  s32i(aligned, value);
+}
+
+/****************************************************************************
+ * Name: decode_s8i
+ *
+ * Description:
+ *   Decode S8I instruction using 32-bit aligned access.
+ *   Return non-zero on successful decoding.
+ *
+ ****************************************************************************/
+
+static int decode_s8i(const uint8_t *p, uint8_t *imm8, uint8_t *s,
+                      uint8_t *t)
+{
+  /*  23           16 15   12 11    8 7     4 3     0
+   * | imm8          |0 1 0 0| s     | t     |0 0 1 0|
+   */
+
+  uint8_t b0 = load_uint8(p);
+  uint8_t b1 = load_uint8(p + 1);
+
+  if ((b0 & 0xf) == 2 && (b1 & 0xf0) == 0x40)
+    {
+      *t = b0 >> 4;
+      *s = b1 & 0xf;
+      *imm8 = load_uint8(p + 2);
+      return 1;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: decode_l8ui
+ *
+ * Description:
+ *   Decode L8UI instruction using 32-bit aligned access.
+ *   Return non-zero on successful decoding.
+ *
+ ****************************************************************************/
+
+static int decode_l8ui(const uint8_t *p, uint8_t *imm8, uint8_t *s,
+                       uint8_t *t)
+{
+  /*  23           16 15   12 11    8 7     4 3     0
+   * | imm8          |0 0 0 0| s     | t     |0 0 1 0|
+   */
+
+  uint8_t b0 = load_uint8(p);
+  uint8_t b1 = load_uint8(p + 1);
+
+  if ((b0 & 0xf) == 2 && (b1 & 0xf0) == 0)
+    {
+      *t = b0 >> 4;
+      *s = b1 & 0xf;
+      *imm8 = load_uint8(p + 2);
+      return 1;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: advance_pc
+ *
+ * Description:
+ *   Advance PC register by the given value.
+ *
+ ****************************************************************************/
+
+static void advance_pc(uint32_t *regs, int diff)
+{
+  uint32_t nextpc;
+
+  /* Advance to the next instruction. */
+
+  nextpc = regs[REG_PC] + diff;
+#ifdef XCHAL_HAVE_LOOPS
+  /* See Xtensa ISA 4.3.2.4 Loopback Semantics */
+
+  if (regs[REG_LCOUNT] != 0 && nextpc == regs[REG_LEND])
+    {
+      regs[REG_LCOUNT]--;
+      nextpc = regs[REG_LBEG];
+    }
+
+#endif
+  regs[REG_PC] = nextpc;
+}
+
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: xtensa_user
+ *
+ * Description:
+ *   ESP32-specific user exception handler.
+ *
+ ****************************************************************************/
+
+uint32_t *xtensa_user(int exccause, uint32_t *regs)
+{
+#ifdef CONFIG_ARCH_USE_MODULE_TEXT
+  /* Emulate byte access for module text.
+   *
+   * ESP32 only allows word-aligned accesses to the instruction memory
+   * regions.  A non-aligned access raises a LoadStoreErrorCause exception.
+   * We catch those exception and emulate byte access here because it's
+   * necessary in a few places during dynamic code loading:
+   *
+   *  - memcpy as a part of read(2) when loading code from a file system.
+   *  - relocation needs to inspect and modify text.
+   *
+   * (thus binfo() is used below)
+   */
+
+  if (exccause == XCHAL_EXCCAUSE_LOAD_STORE_ERROR &&
+      (uintptr_t)&_smodtext <= regs[REG_EXCVADDR] &&
+      (uintptr_t)&_emodtext > regs[REG_EXCVADDR])
+    {
+      uint8_t *pc = (uint8_t *)regs[REG_PC];
+      uint8_t imm8;
+      uint8_t s;
+      uint8_t t;
+
+      binfo("XCHAL_EXCCAUSE_LOAD_STORE_ERROR at %p, pc=%p\n",
+            (FAR void *)regs[REG_EXCVADDR],
+            pc);
+
+      if (decode_s8i(pc, &imm8, &s, &t))
+        {
+          binfo("Emulating S8I imm8=%u, s=%u (%p), t=%u (%p)\n",
+                (unsigned int)imm8,
+                (unsigned int)s,
+                (void *)regs[REG_A0 + s],
+                (unsigned int)t,
+                (void *)regs[REG_A0 + t]);
+
+          DEBUGASSERT(regs[REG_A0 + s] + imm8 == regs[REG_EXCVADDR]);
+          store_uint8(((uint8_t *)regs[REG_A0 + s]) + imm8,
+                      regs[REG_A0 + t]);
+          advance_pc(regs, 3);
+          return regs;
+        }
+      else if (decode_l8ui(pc, &imm8, &s, &t))
+        {
+          binfo("Emulating L8UI imm8=%u, s=%u (%p), t=%u (%p)\n",
+                (unsigned int)imm8,
+                (unsigned int)s,
+                (void *)regs[REG_A0 + s],
+                (unsigned int)t,
+                (void *)regs[REG_A0 + t]);
+
+          DEBUGASSERT(regs[REG_A0 + s] + imm8 == regs[REG_EXCVADDR]);
+          regs[REG_A0 + t] = load_uint8(((uint8_t *)regs[REG_A0 + s]) +
+                                        imm8);
+          advance_pc(regs, 3);
+          return regs;
+        }
+    }
+
+#endif
+  /* xtensa_user_panic never returns. */
+
+  xtensa_user_panic(exccause, regs);
+
+  while (1)
+    {
+    }
+}


[incubator-nuttx] 01/14: xtensa: Add module/elf definitions

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 3f80f806ef8d50a9d1f07953d0af6c5b9ea615c4
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Thu Mar 5 10:07:57 2020 +0900

    xtensa: Add module/elf definitions
---
 boards/xtensa/esp32/esp32-core/scripts/Make.defs  |  23 ++++
 boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld | 130 ++++++++++++++++++++++
 2 files changed, 153 insertions(+)

diff --git a/boards/xtensa/esp32/esp32-core/scripts/Make.defs b/boards/xtensa/esp32/esp32-core/scripts/Make.defs
index 1f6682c..d85b550 100644
--- a/boards/xtensa/esp32/esp32-core/scripts/Make.defs
+++ b/boards/xtensa/esp32/esp32-core/scripts/Make.defs
@@ -102,6 +102,29 @@ NXFLATLDFLAGS1 = -r -d -warn-common
 NXFLATLDFLAGS2 = $(NXFLATLDFLAGS1) -T$(TOPDIR)/binfmt/libnxflat/gnu-nxflat-pcrel.ld -no-check-sections
 LDNXFLATFLAGS = -e main -s 2048
 
+# Loadable module definitions
+
+CMODULEFLAGS = $(CFLAGS)
+
+LDMODULEFLAGS = -r -e module_initialize
+ifeq ($(WINTOOL),y)
+  LDMODULEFLAGS += -T "${shell cygpath -w $(TOPDIR)/libs/libc/modlib/gnu-elf.ld}"
+else
+  LDMODULEFLAGS += -T $(TOPDIR)/libs/libc/modlib/gnu-elf.ld
+endif
+
+# ELF module definitions
+
+CELFFLAGS = $(CFLAGS)
+CXXELFFLAGS = $(CXXFLAGS)
+
+LDELFFLAGS = -r -e main
+ifeq ($(WINTOOL),y)
+  LDELFFLAGS += -T "${shell cygpath -w $(TOPDIR)/boards/$(CONFIG_ARCH)/$(CONFIG_ARCH_CHIP)/$(CONFIG_ARCH_BOARD)/scripts/gnu-elf.ld}"
+else
+  LDELFFLAGS += -T $(TOPDIR)/boards/$(CONFIG_ARCH)/$(CONFIG_ARCH_CHIP)/$(CONFIG_ARCH_BOARD)/scripts/gnu-elf.ld
+endif
+
 ASMEXT = .S
 OBJEXT = .o
 LIBEXT = .a
diff --git a/boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld b/boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld
new file mode 100644
index 0000000..95c7d2e
--- /dev/null
+++ b/boards/xtensa/esp32/esp32-core/scripts/gnu-elf.ld
@@ -0,0 +1,130 @@
+/****************************************************************************
+ * binfmt/libelf/gnu-elf.ld
+ *
+ *   Copyright (C) 2012 Gregory Nutt. All rights reserved.
+ *   Author: Gregory Nutt <gn...@nuttx.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ *    used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+SECTIONS
+{
+  .text 0x00000000 :
+    {
+      _stext = . ;
+      *(.text)
+      *(.text.*)
+      *(.gnu.warning)
+      *(.stub)
+      *(.glue_7)
+      *(.glue_7t)
+      *(.jcr)
+
+      /* C++ support:  The .init and .fini sections contain specific logic
+       * to manage static constructors and destructors.
+       */
+
+      *(.gnu.linkonce.t.*)
+      *(.init)             /* Old ABI */
+      *(.fini)             /* Old ABI */
+      _etext = . ;
+    }
+
+  .rodata :
+    {
+      _srodata = . ;
+      *(.rodata)
+      *(.rodata1)
+      *(.rodata.*)
+      *(.gnu.linkonce.r*)
+      _erodata = . ;
+    }
+
+  .data :
+    {
+      _sdata = . ;
+      *(.data)
+      *(.data1)
+      *(.data.*)
+      *(.gnu.linkonce.d*)
+      . = ALIGN(4);
+      _edata = . ;
+    }
+
+  /* C++ support. For each global and static local C++ object,
+   * GCC creates a small subroutine to construct the object. Pointers
+   * to these routines (not the routines themselves) are stored as
+   * simple, linear arrays in the .ctors section of the object file.
+   * Similarly, pointers to global/static destructor routines are
+   * stored in .dtors.
+   */
+
+  .ctors :
+    {
+      _sctors = . ;
+      *(.ctors)       /* Old ABI:  Unallocated */
+      *(.init_array)  /* New ABI:  Allocated */
+      _ectors = . ;
+    }
+
+  .dtors :
+    {
+      _sdtors = . ;
+      *(.dtors)       /* Old ABI:  Unallocated */
+      *(.fini_array)  /* New ABI:  Allocated */
+      _edtors = . ;
+    }
+
+  .bss :
+    {
+      _sbss = . ;
+      *(.bss)
+      *(.bss.*)
+      *(.sbss)
+      *(.sbss.*)
+      *(.gnu.linkonce.b*)
+      *(COMMON)
+      _ebss = . ;
+    }
+
+    /* Stabs debugging sections.    */
+
+    .stab 0 : { *(.stab) }
+    .stabstr 0 : { *(.stabstr) }
+    .stab.excl 0 : { *(.stab.excl) }
+    .stab.exclstr 0 : { *(.stab.exclstr) }
+    .stab.index 0 : { *(.stab.index) }
+    .stab.indexstr 0 : { *(.stab.indexstr) }
+    .comment 0 : { *(.comment) }
+    .debug_abbrev 0 : { *(.debug_abbrev) }
+    .debug_info 0 : { *(.debug_info) }
+    .debug_line 0 : { *(.debug_line) }
+    .debug_pubnames 0 : { *(.debug_pubnames) }
+    .debug_aranges 0 : { *(.debug_aranges) }
+}


[incubator-nuttx] 03/14: arch/xtensa/include/elf.h: Initial version

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 865730588305c5a6512e742e78eb360c6b8379ab
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Thu Mar 5 11:23:21 2020 +0900

    arch/xtensa/include/elf.h: Initial version
---
 arch/xtensa/include/elf.h | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/arch/xtensa/include/elf.h b/arch/xtensa/include/elf.h
new file mode 100644
index 0000000..829b717
--- /dev/null
+++ b/arch/xtensa/include/elf.h
@@ -0,0 +1,32 @@
+/****************************************************************************
+ * arch/xtensa/include/elf.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_XTENSA_INCLUDE_ELF_H
+#define __ARCH_XTENSA_INCLUDE_ELF_H
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Relocation codes */
+
+#define R_XTENSA_NONE 0
+
+#endif /* __ARCH_XTENSA_INCLUDE_ELF_H */


[incubator-nuttx] 04/14: xtensa: Add modlib boilerplate

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 5e1b24f01204aa1cb7cb922750243fdc20098e9c
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Thu Mar 5 11:26:21 2020 +0900

    xtensa: Add modlib boilerplate
    
    This is a modified copy of armv6-m/armv7-m version.
---
 libs/libc/machine/Kconfig                |   3 +
 libs/libc/machine/Make.defs              |   3 +
 libs/libc/machine/xtensa/Kconfig         |   4 +
 libs/libc/machine/{ => xtensa}/Make.defs |  20 ++--
 libs/libc/machine/xtensa/arch_elf.c      | 174 +++++++++++++++++++++++++++++++
 5 files changed, 192 insertions(+), 12 deletions(-)

diff --git a/libs/libc/machine/Kconfig b/libs/libc/machine/Kconfig
index aea5619..f0c34ac 100644
--- a/libs/libc/machine/Kconfig
+++ b/libs/libc/machine/Kconfig
@@ -162,5 +162,8 @@ endif
 if ARCH_X86
 source libs/libc/machine/x86/Kconfig
 endif
+if ARCH_XTENSA
+source libs/libc/machine/xtensa/Kconfig
+endif
 
 endmenu # Architecture-Specific Support
diff --git a/libs/libc/machine/Make.defs b/libs/libc/machine/Make.defs
index efe95aa..8cfd702 100644
--- a/libs/libc/machine/Make.defs
+++ b/libs/libc/machine/Make.defs
@@ -45,3 +45,6 @@ endif
 ifeq ($(CONFIG_ARCH_X86),y)
 include ${TOPDIR}/libs/libc/machine/x86/Make.defs
 endif
+ifeq ($(CONFIG_ARCH_XTENSA),y)
+include ${TOPDIR}/libs/libc/machine/xtensa/Make.defs
+endif
diff --git a/libs/libc/machine/xtensa/Kconfig b/libs/libc/machine/xtensa/Kconfig
new file mode 100644
index 0000000..f72f3c0
--- /dev/null
+++ b/libs/libc/machine/xtensa/Kconfig
@@ -0,0 +1,4 @@
+#
+# For a description of the syntax of this configuration file,
+# see the file kconfig-language.txt in the NuttX tools repository.
+#
diff --git a/libs/libc/machine/Make.defs b/libs/libc/machine/xtensa/Make.defs
similarity index 82%
copy from libs/libc/machine/Make.defs
copy to libs/libc/machine/xtensa/Make.defs
index efe95aa..fc8c884 100644
--- a/libs/libc/machine/Make.defs
+++ b/libs/libc/machine/xtensa/Make.defs
@@ -1,5 +1,5 @@
 ############################################################################
-# libs/libc/machine/Make.defs
+# libs/libc/machine/arm/armv6-m/Make.defs
 #
 #   Copyright (C) 2017 Gregory Nutt. All rights reserved.
 #   Author: Gregory Nutt <gn...@nuttx.org>
@@ -33,15 +33,11 @@
 #
 ############################################################################
 
-ifeq ($(CONFIG_ARCH_ARM),y)
-include ${TOPDIR}/libs/libc/machine/arm/Make.defs
-endif
-ifeq ($(CONFIG_ARCH_RISCV),y)
-include ${TOPDIR}/libs/libc/machine/risc-v/Make.defs
-endif
-ifeq ($(CONFIG_ARCH_SIM),y)
-include ${TOPDIR}/libs/libc/machine/sim/Make.defs
-endif
-ifeq ($(CONFIG_ARCH_X86),y)
-include ${TOPDIR}/libs/libc/machine/x86/Make.defs
+ifeq ($(CONFIG_LIBC_ARCH_ELF),y)
+
+CSRCS += arch_elf.c
+
+DEPPATH += --dep-path machine/xtensa
+VPATH += :machine/xtensa
+
 endif
diff --git a/libs/libc/machine/xtensa/arch_elf.c b/libs/libc/machine/xtensa/arch_elf.c
new file mode 100644
index 0000000..97fdbc2
--- /dev/null
+++ b/libs/libc/machine/xtensa/arch_elf.c
@@ -0,0 +1,174 @@
+/****************************************************************************
+ * libs/libc/machine/arm/armv6-m/arch_elf.c
+ *
+ *   Copyright (C) 2013-2014, 2017 Gregory Nutt. All rights reserved.
+ *   Author: Gregory Nutt <gn...@nuttx.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ *    used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdlib.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <arch/elf.h>
+#include <nuttx/elf.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: up_checkarch
+ *
+ * Description:
+ *   Given the ELF header in 'hdr', verify that the ELF file is appropriate
+ *   for the current, configured architecture.  Every architecture that uses
+ *   the ELF loader must provide this function.
+ *
+ * Input Parameters:
+ *   hdr - The ELF header read from the ELF file.
+ *
+ * Returned Value:
+ *   True if the architecture supports this ELF file.
+ *
+ ****************************************************************************/
+
+bool up_checkarch(FAR const Elf32_Ehdr *ehdr)
+{
+  /* Make sure it's an Xtensa executable */
+
+  if (ehdr->e_machine != EM_XTENSA)
+    {
+      berr("ERROR: Not for Xtensa: e_machine=%04x\n", ehdr->e_machine);
+      return false;
+    }
+
+  /* Make sure that 32-bit objects are supported */
+
+  if (ehdr->e_ident[EI_CLASS] != ELFCLASS32)
+    {
+      berr("ERROR: Need 32-bit objects: e_ident[EI_CLASS]=%02x\n", ehdr->e_ident[EI_CLASS]);
+      return false;
+    }
+
+  /* Verify endian-ness */
+
+#ifdef CONFIG_ENDIAN_BIG
+  if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB)
+#else
+  if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB)
+#endif
+    {
+      berr("ERROR: Wrong endian-ness: e_ident[EI_DATA]=%02x\n", ehdr->e_ident[EI_DATA]);
+      return false;
+    }
+
+  return true;
+}
+
+/****************************************************************************
+ * Name: up_relocate and up_relocateadd
+ *
+ * Description:
+ *   Perform on architecture-specific ELF relocation.  Every architecture
+ *   that uses the ELF loader must provide this function.
+ *
+ * Input Parameters:
+ *   rel - The relocation type
+ *   sym - The ELF symbol structure containing the fully resolved value.
+ *         There are a few relocation types for a few architectures that do
+ *         not require symbol information.  For those, this value will be
+ *         NULL.  Implementations of these functions must be able to handle
+ *         that case.
+ *   addr - The address that requires the relocation.
+ *
+ * Returned Value:
+ *   Zero (OK) if the relocation was successful.  Otherwise, a negated errno
+ *   value indicating the cause of the relocation failure.
+ *
+ ****************************************************************************/
+
+int up_relocate(FAR const Elf32_Rel *rel, FAR const Elf32_Sym *sym,
+                uintptr_t addr)
+{
+  unsigned int relotype;
+
+  /* All relocations except NONE depend upon having valid symbol
+   * information.
+   */
+
+  relotype = ELF32_R_TYPE(rel->r_info);
+  if (sym == NULL && relotype != R_XTENSA_NONE)
+    {
+      return -EINVAL;
+    }
+
+  /* Handle the relocation by relocation type */
+
+  switch (relotype)
+    {
+    case R_XTENSA_NONE:
+      {
+        /* No relocation */
+      }
+      break;
+
+    default:
+      berr("ERROR: Unsupported relocation: %d\n", ELF32_R_TYPE(rel->r_info));
+      return -EINVAL;
+    }
+
+  return OK;
+}
+
+int up_relocateadd(FAR const Elf32_Rela *rel, FAR const Elf32_Sym *sym,
+                   uintptr_t addr)
+{
+  berr("ERROR: RELA relocation not supported\n");
+  return -ENOSYS;
+}


[incubator-nuttx] 09/14: Add -mtext-section-literals to avoid out of range relocations

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit bf2cfd4b1ae8cccad8278195d4d49dc66ba4736a
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Thu Mar 5 19:14:54 2020 +0900

    Add -mtext-section-literals to avoid out of range relocations
---
 boards/xtensa/esp32/esp32-core/scripts/Make.defs | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/boards/xtensa/esp32/esp32-core/scripts/Make.defs b/boards/xtensa/esp32/esp32-core/scripts/Make.defs
index d85b550..c230ad6 100644
--- a/boards/xtensa/esp32/esp32-core/scripts/Make.defs
+++ b/boards/xtensa/esp32/esp32-core/scripts/Make.defs
@@ -104,7 +104,7 @@ LDNXFLATFLAGS = -e main -s 2048
 
 # Loadable module definitions
 
-CMODULEFLAGS = $(CFLAGS)
+CMODULEFLAGS = $(CFLAGS) -mtext-section-literals
 
 LDMODULEFLAGS = -r -e module_initialize
 ifeq ($(WINTOOL),y)
@@ -115,8 +115,8 @@ endif
 
 # ELF module definitions
 
-CELFFLAGS = $(CFLAGS)
-CXXELFFLAGS = $(CXXFLAGS)
+CELFFLAGS = $(CFLAGS) -mtext-section-literals
+CXXELFFLAGS = $(CXXFLAGS) -mtext-section-literals
 
 LDELFFLAGS = -r -e main
 ifeq ($(WINTOOL),y)


[incubator-nuttx] 02/14: include/elf.h: Add EM_XTENSA

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit d6244342822ed879bff891161565a14a33682cbe
Author: YAMAMOTO Takashi <ya...@midokura.com>
AuthorDate: Thu Mar 5 11:21:28 2020 +0900

    include/elf.h: Add EM_XTENSA
---
 include/elf.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/elf.h b/include/elf.h
index f931826..bff9a49 100644
--- a/include/elf.h
+++ b/include/elf.h
@@ -81,6 +81,7 @@
 #define EM_CRIS            76     /* Axis Communications 32-bit embedded processor */
 #define EM_V850            87     /* NEC v850 */
 #define EM_M32R            88     /* Renesas M32R */
+#define EM_XTENSA          94     /* Tensilica Xtensa */
 #define EM_RISCV           243    /* RISC-V */
 #define EM_ALPHA           0x9026
 #define EM_CYGNUS_V850     0x9080