public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/3] ld: allow update of existing QNX stack note
@ 2023-10-13  8:02 Clément Chigot
  2023-10-13  8:02 ` [PATCH 2/3] ld: correctly handle QNX --lazy-stack without -zstack-size Clément Chigot
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Clément Chigot @ 2023-10-13  8:02 UTC (permalink / raw)
  To: binutils; +Cc: Clément Chigot

Up to now, the linker would always create a QNX stack note from scratch.
However, object files could already have such note, ending up into
duplicates. QNX loader doesn't handle that.

Update the mechanism to first search through the input files for a .note
section holding a QNX stack note. If none are found, then a new section
is created into the stub file as before. This requires this search to be
done once the file have been opened, moving the whole logic a bit later
in the emulation process.

As part for this update, also allow to request an executable stack
without necessarily having to provide its size as well.  In this case, s
etup a default lazy stack of 0x1000.

ld/ChangeLog:

        * emultempl/nto.em (nto_create_QNX_note_section): New Function.
        (nto_lookup_QNX_note_section): New Function.
        (nto_add_note_section): Move the creation of the note section
        in the above new functions.
        (nto_create_output_section_statements): rename nto_after_open
        * testsuite/ld-aarch64/aarch64-nto.exp: add new test.
        * testsuite/ld-aarch64/nto-stack-note-3.d: New test.
        * testsuite/ld-aarch64/nto-stack-note.s: New test.
---
 ld/emultempl/nto.em                        | 121 +++++++++++++++------
 ld/testsuite/ld-aarch64/aarch64-nto.exp    |   1 +
 ld/testsuite/ld-aarch64/nto-stack-note-3.d |  12 ++
 ld/testsuite/ld-aarch64/nto-stack-note.s   |  14 +++
 4 files changed, 116 insertions(+), 32 deletions(-)
 create mode 100644 ld/testsuite/ld-aarch64/nto-stack-note-3.d
 create mode 100644 ld/testsuite/ld-aarch64/nto-stack-note.s

diff --git a/ld/emultempl/nto.em b/ld/emultempl/nto.em
index b38fddd251c..0d319acc9bf 100644
--- a/ld/emultempl/nto.em
+++ b/ld/emultempl/nto.em
@@ -39,31 +39,20 @@ struct nto_stack_note
   unsigned char execstack[4];
 };
 
-/* Generate the QNT_STACK .note section.  */
-static void
-nto_add_note_section (void) {
+static asection*
+nto_create_QNX_note_section(int type)
+{
   asection *note_sec;
   flagword flags;
   Elf_External_Note *e_note;
-  bfd_size_type size, h_size;
-  struct nto_stack_note *n_note;
-
-  /* Don't create a note if the stack size isn't provided.  */
-  if (link_info.stacksize <= 0)
-    return;
+  bfd_size_type size;
 
   /* As ${ARCH}elf.em is imported and ${ARCH}_elf_create_output_section_statements
      is called before this function, stub_file should already be defined.  */
   if (!stub_file)
     {
       einfo (_("%F%P: cannot create .note section in stub BFD.\n"));
-      return;
-    }
-
-  if (nto_lazy_stack && !link_info.stacksize)
-    {
-      einfo (_("%F%P: error: --lazy-stack must follow -zstack-size=<size>\n"));
-      return;
+      return NULL;
     }
 
   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
@@ -72,12 +61,11 @@ nto_add_note_section (void) {
   if (! note_sec)
     {
       einfo (_("%F%P: failed to create .note section\n"));
-      return;
+      return NULL;
     }
 
   size = offsetof (Elf_External_Note, name[sizeof "QNX"]);
   size = (size + 3) & -(bfd_size_type) 4;
-  h_size = size;
   size += sizeof (struct nto_stack_note);
   note_sec->size = size;
 
@@ -86,31 +74,100 @@ nto_add_note_section (void) {
   e_note = (Elf_External_Note *) note_sec->contents;
   bfd_h_put_32 (stub_file->the_bfd, sizeof "QNX", &e_note->namesz);
   bfd_h_put_32 (stub_file->the_bfd, sizeof (struct nto_stack_note), &e_note->descsz);
-  bfd_h_put_32 (stub_file->the_bfd, QNT_STACK, &e_note->type);
+  bfd_h_put_32 (stub_file->the_bfd, type, &e_note->type);
   memcpy (e_note->name, "QNX", sizeof "QNX");
 
+  return note_sec;
+}
 
-  /* Generate .note content.*/
-  n_note = (struct nto_stack_note *) (note_sec->contents + h_size);
-  bfd_h_put_32 (stub_file->the_bfd, link_info.stacksize, &n_note->stacksize);
+/* Lookup for a section holding a QNX note or create a new section.  */
+static asection*
+nto_lookup_QNX_note_section(int type)
+{
+  asection *stack_note_sec = NULL;
+  bfd *abfd;
+  for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
+    {
+      Elf_External_Note *e_note;
+      asection *sec;
+
+      /* QNX notes are held under a note section simply named ".note".  */
+      sec = bfd_get_section_by_name (abfd, ".note");
+      if (!sec)
+	continue;
+
+      /* Verify that this is a QNX note of the expected type.  */
+      sec->contents = bfd_malloc(sec->size);
+      if (!bfd_get_section_contents (sec->owner, sec, sec->contents, (file_ptr) 0,
+				     sec->size))
+	einfo (_("%F%P: %pB: can't read contents of section .note: %E\n"),
+	       sec->owner);
+
+      e_note = (Elf_External_Note *) sec->contents;
+      if (! strcmp("QNX", e_note->name) && *e_note->type == type)
+	{
+	  stack_note_sec = sec;
+	  /* Allow modification of this .note content.  */
+	  stack_note_sec->flags |= SEC_IN_MEMORY;
+	  break;
+	}
+    }
 
-  if (nto_lazy_stack)
-    bfd_h_put_32 (stub_file->the_bfd, 4096, &n_note->stackalloc);
+  if (stack_note_sec)
+    return stack_note_sec;
   else
-    bfd_h_put_32 (stub_file->the_bfd, link_info.stacksize, &n_note->stackalloc);
+    return nto_create_QNX_note_section(type);
 
-  if (link_info.execstack != link_info.noexecstack && link_info.execstack)
-    bfd_h_put_32 (stub_file->the_bfd, 0, &n_note->execstack);
-  else
-    bfd_h_put_32 (stub_file->the_bfd, 1, &n_note->execstack);
+}
+
+/* Generate the QNX stack .note section.  */
+static void
+nto_add_note_section (void) {
+  asection *note_sec;
+  struct nto_stack_note *n_note;
+  bfd_size_type h_size;
+  bool is_update = false;
 
+  /* Don't create a note if none of the stack parameter have to be modified.  */
+  if (link_info.stacksize <= 0 && (link_info.execstack == link_info.noexecstack))
+    return;
+
+  if (nto_lazy_stack && !link_info.stacksize)
+    {
+      einfo (_("%F%P: error: --lazy-stack must follow -zstack-size=<size>\n"));
+      return;
+    }
+
+  note_sec = nto_lookup_QNX_note_section(QNT_STACK);
+  if (! note_sec)
+    return;
+
+  /* Update QNX stack note content.  */
+  h_size = note_sec->size - sizeof(struct nto_stack_note);
+  n_note = (struct nto_stack_note *) (note_sec->contents + h_size);
+  is_update = note_sec->owner != stub_file->the_bfd;
+
+  if (link_info.stacksize > 0)
+    bfd_h_put_32 (note_sec->owner, link_info.stacksize, &n_note->stacksize);
+  else if (!is_update)
+    bfd_h_put_32 (note_sec->owner, 0, &n_note->stacksize);
+
+  if (nto_lazy_stack || (!is_update && link_info.stacksize <= 0))
+    bfd_h_put_32 (note_sec->owner, 4096, &n_note->stackalloc);
+  else if (link_info.stacksize > 0)
+    bfd_h_put_32 (note_sec->owner, link_info.stacksize, &n_note->stackalloc);
+
+  if (link_info.execstack)
+    bfd_h_put_32 (note_sec->owner, 0, &n_note->execstack);
+  else if (!is_update || link_info.noexecstack)
+    bfd_h_put_32 (note_sec->owner, 1, &n_note->execstack);
 }
 
 static void
-nto_create_output_section_statements (void)
+nto_after_open (void)
 {
-  ${ARCH}_elf_create_output_section_statements ();
   nto_add_note_section();
+  gld${EMULATION_NAME}_after_open ();
 }
 
 EOF
@@ -160,4 +217,4 @@ PARSE_AND_LIST_ARGS_CASES=${PARSE_AND_LIST_ARGS_CASES}'
 # Put these extra Neutrino routines in ld_${EMULATION_NAME}_emulation
 #
 
-LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=nto_create_output_section_statements
+LDEMUL_AFTER_OPEN=nto_after_open
diff --git a/ld/testsuite/ld-aarch64/aarch64-nto.exp b/ld/testsuite/ld-aarch64/aarch64-nto.exp
index 4fd6245e9a2..256eb0da10e 100644
--- a/ld/testsuite/ld-aarch64/aarch64-nto.exp
+++ b/ld/testsuite/ld-aarch64/aarch64-nto.exp
@@ -26,3 +26,4 @@ if { ![istarget "aarch64*-*-nto*"] } {
 
 run_dump_test "nto-stack-note-1"
 run_dump_test "nto-stack-note-2"
+run_dump_test "nto-stack-note-3"
diff --git a/ld/testsuite/ld-aarch64/nto-stack-note-3.d b/ld/testsuite/ld-aarch64/nto-stack-note-3.d
new file mode 100644
index 00000000000..cdc71beeee0
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/nto-stack-note-3.d
@@ -0,0 +1,12 @@
+#name: nto-stack-note-3
+#source: nto-stack-note.s
+#as:
+#ld: -z noexecstack
+#readelf: -n
+
+Displaying notes found in: .note
+[ 	]+Owner[ 	]+Data size[ 	]+Description
+  QNX                  0x0000000c	QNX stack
+   Stack Size: 0x4321
+   Stack allocated: 1234
+   Executable: no
diff --git a/ld/testsuite/ld-aarch64/nto-stack-note.s b/ld/testsuite/ld-aarch64/nto-stack-note.s
new file mode 100644
index 00000000000..9e48ab8d5bc
--- /dev/null
+++ b/ld/testsuite/ld-aarch64/nto-stack-note.s
@@ -0,0 +1,14 @@
+        .global _start
+        .text
+_start:
+        nop
+
+	.section .note
+        .long 1f - 0f           /* name length */
+        .long 2f - 1f           /* data length */
+	.long 3                 /* note type */
+0:	.asciz "QNX"            /* vendore name */
+1:	.long 0x4321
+	.long 0x1234
+	.long 0x0
+2:
-- 
2.25.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/3] ld: correctly handle QNX --lazy-stack without -zstack-size
  2023-10-13  8:02 [PATCH 1/3] ld: allow update of existing QNX stack note Clément Chigot
@ 2023-10-13  8:02 ` Clément Chigot
  2023-10-13 14:06   ` Nick Clifton
  2023-10-13  8:02 ` [PATCH 3/3] ld: warn when duplicated QNX stack note are detected Clément Chigot
  2023-10-13 14:06 ` [PATCH 1/3] ld: allow update of existing QNX stack note Nick Clifton
  2 siblings, 1 reply; 6+ messages in thread
From: Clément Chigot @ 2023-10-13  8:02 UTC (permalink / raw)
  To: binutils; +Cc: Clément Chigot

The warning was skipped if -zstack-size is not provided.

ld/ChangeLog:

        * emultempl/nto.em: Move --lazy-stack warning before missing
        -zstack-size skip.
---
 ld/emultempl/nto.em | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/ld/emultempl/nto.em b/ld/emultempl/nto.em
index 0d319acc9bf..b1a61338412 100644
--- a/ld/emultempl/nto.em
+++ b/ld/emultempl/nto.em
@@ -128,16 +128,16 @@ nto_add_note_section (void) {
   bfd_size_type h_size;
   bool is_update = false;
 
-  /* Don't create a note if none of the stack parameter have to be modified.  */
-  if (link_info.stacksize <= 0 && (link_info.execstack == link_info.noexecstack))
-    return;
-
   if (nto_lazy_stack && !link_info.stacksize)
     {
       einfo (_("%F%P: error: --lazy-stack must follow -zstack-size=<size>\n"));
       return;
     }
 
+  /* Don't create a note if none of the stack parameter have to be modified.  */
+  if (link_info.stacksize <= 0 && (link_info.execstack == link_info.noexecstack))
+    return;
+
   note_sec = nto_lookup_QNX_note_section(QNT_STACK);
   if (! note_sec)
     return;
-- 
2.25.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 3/3] ld: warn when duplicated QNX stack note are detected
  2023-10-13  8:02 [PATCH 1/3] ld: allow update of existing QNX stack note Clément Chigot
  2023-10-13  8:02 ` [PATCH 2/3] ld: correctly handle QNX --lazy-stack without -zstack-size Clément Chigot
@ 2023-10-13  8:02 ` Clément Chigot
  2023-10-13 14:07   ` Nick Clifton
  2023-10-13 14:06 ` [PATCH 1/3] ld: allow update of existing QNX stack note Nick Clifton
  2 siblings, 1 reply; 6+ messages in thread
From: Clément Chigot @ 2023-10-13  8:02 UTC (permalink / raw)
  To: binutils; +Cc: Clément Chigot

This warning is triggered only when a stack parameter is given to
the linker.

ld/ChangeLog:

        * emultempl/nto.em: Add warning when several QNX .note are
        detected.
---
 ld/emultempl/nto.em | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/ld/emultempl/nto.em b/ld/emultempl/nto.em
index b1a61338412..f4c76f18fc2 100644
--- a/ld/emultempl/nto.em
+++ b/ld/emultempl/nto.em
@@ -86,6 +86,7 @@ nto_lookup_QNX_note_section(int type)
 {
   asection *stack_note_sec = NULL;
   bfd *abfd;
+  bool duplicated_notes_detected = false;
   for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
     {
       Elf_External_Note *e_note;
@@ -106,10 +107,23 @@ nto_lookup_QNX_note_section(int type)
       e_note = (Elf_External_Note *) sec->contents;
       if (! strcmp("QNX", e_note->name) && *e_note->type == type)
 	{
-	  stack_note_sec = sec;
-	  /* Allow modification of this .note content.  */
-	  stack_note_sec->flags |= SEC_IN_MEMORY;
-	  break;
+	  if (stack_note_sec)
+	    {
+	      if (!duplicated_notes_detected)
+		{
+		  einfo (_("%P: %pB: warning: duplicated QNX stack .note detected\n"),
+			 stack_note_sec->owner);
+		  duplicated_notes_detected = true;
+		}
+	      einfo (_("%P: %pB: warning: duplicated QNX stack .note detected\n"),
+		     sec->owner);
+	    }
+	  else
+	    {
+	      stack_note_sec = sec;
+	      /* Allow modification of this .note content.  */
+	      stack_note_sec->flags |= SEC_IN_MEMORY;
+	    }
 	}
     }
 
-- 
2.25.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/3] ld: allow update of existing QNX stack note
  2023-10-13  8:02 [PATCH 1/3] ld: allow update of existing QNX stack note Clément Chigot
  2023-10-13  8:02 ` [PATCH 2/3] ld: correctly handle QNX --lazy-stack without -zstack-size Clément Chigot
  2023-10-13  8:02 ` [PATCH 3/3] ld: warn when duplicated QNX stack note are detected Clément Chigot
@ 2023-10-13 14:06 ` Nick Clifton
  2 siblings, 0 replies; 6+ messages in thread
From: Nick Clifton @ 2023-10-13 14:06 UTC (permalink / raw)
  To: Clément Chigot, binutils

Hi Clément,

>          * emultempl/nto.em (nto_create_QNX_note_section): New Function.
>          (nto_lookup_QNX_note_section): New Function.
>          (nto_add_note_section): Move the creation of the note section
>          in the above new functions.
>          (nto_create_output_section_statements): rename nto_after_open
>          * testsuite/ld-aarch64/aarch64-nto.exp: add new test.
>          * testsuite/ld-aarch64/nto-stack-note-3.d: New test.
>          * testsuite/ld-aarch64/nto-stack-note.s: New test.

Approved - please apply.

Cheers
   Nick



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/3] ld: correctly handle QNX --lazy-stack without -zstack-size
  2023-10-13  8:02 ` [PATCH 2/3] ld: correctly handle QNX --lazy-stack without -zstack-size Clément Chigot
@ 2023-10-13 14:06   ` Nick Clifton
  0 siblings, 0 replies; 6+ messages in thread
From: Nick Clifton @ 2023-10-13 14:06 UTC (permalink / raw)
  To: Clément Chigot, binutils

Hi Clément,

>          * emultempl/nto.em: Move --lazy-stack warning before missing
>          -zstack-size skip.

Approved - please apply.

Cheers
   Nick



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 3/3] ld: warn when duplicated QNX stack note are detected
  2023-10-13  8:02 ` [PATCH 3/3] ld: warn when duplicated QNX stack note are detected Clément Chigot
@ 2023-10-13 14:07   ` Nick Clifton
  0 siblings, 0 replies; 6+ messages in thread
From: Nick Clifton @ 2023-10-13 14:07 UTC (permalink / raw)
  To: Clément Chigot, binutils

Hi Clément,

>          * emultempl/nto.em: Add warning when several QNX .note are
>          detected.

Approved - please apply.

Cheers
   Nick


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-10-13 14:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-13  8:02 [PATCH 1/3] ld: allow update of existing QNX stack note Clément Chigot
2023-10-13  8:02 ` [PATCH 2/3] ld: correctly handle QNX --lazy-stack without -zstack-size Clément Chigot
2023-10-13 14:06   ` Nick Clifton
2023-10-13  8:02 ` [PATCH 3/3] ld: warn when duplicated QNX stack note are detected Clément Chigot
2023-10-13 14:07   ` Nick Clifton
2023-10-13 14:06 ` [PATCH 1/3] ld: allow update of existing QNX stack note Nick Clifton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).