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

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

commit e4b2ef038fe75f7ed83c7dab02baeaab1739d253
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Oct 19 10:12:51 2026 +0200

    tools/nxflat/ldnxflat: Refuse static constructors and destructors.
    
    An NXFLAT module starts at its main() and has no start-up code, so
    nothing runs its .init_array, .fini_array or .preinit_array.  The NXFLAT
    linker scripts do not place these sections either.  ld leaves them at
    address zero, on top of .data, and ldnxflat copied them there.
    
    hello++3 of apps/examples/nxflat is an example.  Its .init_array is at
    D-Space address 0, and its .bss starts at 4, inside .data.  The
    constructor pointer overwrites the first word of .data, the static
    variables share their addresses with initialized data, and the
    constructor never runs.
    
    Refuse such a module and name the section.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 tools/nxflat/ldnxflat.c   | 9 +++++++++
 tools/nxflat/nxflat_elf.h | 3 +++
 2 files changed, 12 insertions(+)

diff --git a/tools/nxflat/ldnxflat.c b/tools/nxflat/ldnxflat.c
index 845358f8966..61d095d94d4 100644
--- a/tools/nxflat/ldnxflat.c
+++ b/tools/nxflat/ldnxflat.c
@@ -343,6 +343,15 @@ static void place_sections(void)
           continue;
         }
 
+      /* No start-up code runs these arrays in a module */
+
+      if (s->sh_type == SHT_INIT_ARRAY || s->sh_type == SHT_FINI_ARRAY ||
+          s->sh_type == SHT_PREINIT_ARRAY)
+        {
+          fail("%s holds static constructors or destructors, which an "
+               "NXFLAT module cannot run", g_shstrtab + s->sh_name);
+        }
+
       if ((s->sh_flags & SHF_EXECINSTR) != 0)
         {
           g_segof[i] = SEG_TEXT;
diff --git a/tools/nxflat/nxflat_elf.h b/tools/nxflat/nxflat_elf.h
index 114ed128728..04d8485fd8f 100644
--- a/tools/nxflat/nxflat_elf.h
+++ b/tools/nxflat/nxflat_elf.h
@@ -50,6 +50,9 @@
 #define SHT_SYMTAB              2
 #define SHT_NOBITS              8
 #define SHT_REL                 9
+#define SHT_INIT_ARRAY          14
+#define SHT_FINI_ARRAY          15
+#define SHT_PREINIT_ARRAY       16
 
 #define SHF_WRITE               0x1
 #define SHF_ALLOC               0x2

Reply via email to