Fix: Segmentation fault caused by npd in objdump
[binutils-gdb.git] / ld / emultempl / nto.em
blobb38fddd251c85f346661f29e6c128fcb2cd25578
1 # This shell script emits a C file. -*- C -*-
2 #   Copyright 2023 Free Software Foundation, Inc.
4 # This file is part of GLD, the Gnu Linker.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
21 # This file is sourced from elf.em, and defines extra Neutrino
22 # specific routines.
24 # NTO templates aims to refine the default ${ARCH}elf.em template.
25 . "${srcdir}/emultempl/${ARCH}elf.em"
27 cat >>e${EMULATION_NAME}.c <<EOF
29 #include "elf/internal.h"
30 #include "elf/common.h"
31 #include "elf-bfd.h"
32 #include "../bfd/libbfd.h"
34 bool nto_lazy_stack = false;
35 struct nto_stack_note
37   unsigned char stacksize[4];
38   unsigned char stackalloc[4];
39   unsigned char execstack[4];
42 /* Generate the QNT_STACK .note section.  */
43 static void
44 nto_add_note_section (void) {
45   asection *note_sec;
46   flagword flags;
47   Elf_External_Note *e_note;
48   bfd_size_type size, h_size;
49   struct nto_stack_note *n_note;
51   /* Don't create a note if the stack size isn't provided.  */
52   if (link_info.stacksize <= 0)
53     return;
55   /* As ${ARCH}elf.em is imported and ${ARCH}_elf_create_output_section_statements
56      is called before this function, stub_file should already be defined.  */
57   if (!stub_file)
58     {
59       einfo (_("%F%P: cannot create .note section in stub BFD.\n"));
60       return;
61     }
63   if (nto_lazy_stack && !link_info.stacksize)
64     {
65       einfo (_("%F%P: error: --lazy-stack must follow -zstack-size=<size>\n"));
66       return;
67     }
69   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
70            | SEC_IN_MEMORY | SEC_LINKER_CREATED);
71   note_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd, ".note", flags);
72   if (! note_sec)
73     {
74       einfo (_("%F%P: failed to create .note section\n"));
75       return;
76     }
78   size = offsetof (Elf_External_Note, name[sizeof "QNX"]);
79   size = (size + 3) & -(bfd_size_type) 4;
80   h_size = size;
81   size += sizeof (struct nto_stack_note);
82   note_sec->size = size;
84   elf_section_type (note_sec) = SHT_NOTE;
85   note_sec->contents = xmalloc (note_sec->size);
86   e_note = (Elf_External_Note *) note_sec->contents;
87   bfd_h_put_32 (stub_file->the_bfd, sizeof "QNX", &e_note->namesz);
88   bfd_h_put_32 (stub_file->the_bfd, sizeof (struct nto_stack_note), &e_note->descsz);
89   bfd_h_put_32 (stub_file->the_bfd, QNT_STACK, &e_note->type);
90   memcpy (e_note->name, "QNX", sizeof "QNX");
93   /* Generate .note content.*/
94   n_note = (struct nto_stack_note *) (note_sec->contents + h_size);
95   bfd_h_put_32 (stub_file->the_bfd, link_info.stacksize, &n_note->stacksize);
97   if (nto_lazy_stack)
98     bfd_h_put_32 (stub_file->the_bfd, 4096, &n_note->stackalloc);
99   else
100     bfd_h_put_32 (stub_file->the_bfd, link_info.stacksize, &n_note->stackalloc);
102   if (link_info.execstack != link_info.noexecstack && link_info.execstack)
103     bfd_h_put_32 (stub_file->the_bfd, 0, &n_note->execstack);
104   else
105     bfd_h_put_32 (stub_file->the_bfd, 1, &n_note->execstack);
109 static void
110 nto_create_output_section_statements (void)
112   ${ARCH}_elf_create_output_section_statements ();
113   nto_add_note_section();
118 # Define some shell vars to insert bits of code into the standard elf
119 # parse_args and list_options functions.
122 PARSE_AND_LIST_PROLOGUE=${PARSE_AND_LIST_PROLOGUE}'
123 enum nto_options
125   OPTION_STACK = 500,
126   OPTION_LAZY_STACK,
130 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
131   { "stack", required_argument, NULL, OPTION_STACK },
132   { "lazy-stack", no_argument, NULL, OPTION_LAZY_STACK },
135 PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'
136   fprintf (file, _("\
137   --stack <size>              Set size of the initial stack\n\
138   --lazy-stack                Set lazy allocation of stack\n\
139 "));
142 PARSE_AND_LIST_ARGS_CASES=${PARSE_AND_LIST_ARGS_CASES}'
143     case OPTION_STACK:
144       {
145         char *end;
146         link_info.stacksize = strtoul (optarg, &end, 0);
147         if (*end || link_info.stacksize < 0)
148           einfo (_("%F%P: invalid stack size `%s'\''\n"), optarg + 11);
149         if (!link_info.stacksize)
150           /* Use -1 for explicit no-stack, because zero means
151              'default'.   */
152           link_info.stacksize = -1;
153         break;
154       }
155     case OPTION_LAZY_STACK:
156       nto_lazy_stack = true;
157       break;
160 # Put these extra Neutrino routines in ld_${EMULATION_NAME}_emulation
163 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=nto_create_output_section_statements