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
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"
32 #include "../bfd/libbfd.h"
34 bool nto_lazy_stack = false;
37 unsigned char stacksize[4];
38 unsigned char stackalloc[4];
39 unsigned char execstack[4];
42 /* Generate the QNT_STACK .note section. */
44 nto_add_note_section (void) {
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)
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. */
59 einfo (_("%F%P: cannot create .note section in stub BFD.\n"));
63 if (nto_lazy_stack && !link_info.stacksize)
65 einfo (_("%F%P: error: --lazy-stack must follow -zstack-size=<size>\n"));
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);
74 einfo (_("%F%P: failed to create .note section\n"));
78 size = offsetof (Elf_External_Note, name[sizeof "QNX"]);
79 size = (size + 3) & -(bfd_size_type) 4;
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);
98 bfd_h_put_32 (stub_file->the_bfd, 4096, &n_note->stackalloc);
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);
105 bfd_h_put_32 (stub_file->the_bfd, 1, &n_note->execstack);
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}'
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}'
137 --stack <size> Set size of the initial stack\n\
138 --lazy-stack Set lazy allocation of stack\n\
142 PARSE_AND_LIST_ARGS_CASES=${PARSE_AND_LIST_ARGS_CASES}'
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
152 link_info.stacksize = -1;
155 case OPTION_LAZY_STACK:
156 nto_lazy_stack = true;
160 # Put these extra Neutrino routines in ld_${EMULATION_NAME}_emulation
163 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=nto_create_output_section_statements