Commit | Line | Data |
---|---|---|
3d3d428f | 1 | # This shell script emits a C file. -*- C -*- |
82704155 | 2 | # Copyright (C) 2007-2019 Free Software Foundation, Inc. |
3d3d428f NC |
3 | # Contributed by M R Swami Reddy <MR.Swami.Reddy@nsc.com> |
4 | # | |
f96b4a7b | 5 | # This file is part of the GNU Binutils. |
3d3d428f NC |
6 | # |
7 | # This program is free software; you can redistribute it and/or modify | |
8 | # it under the terms of the GNU General Public License as published by | |
f96b4a7b | 9 | # the Free Software Foundation; either version 3 of the License, or |
3d3d428f NC |
10 | # (at your option) any later version. |
11 | # | |
12 | # This program is distributed in the hope that it will be useful, | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | # GNU General Public License for more details. | |
16 | # | |
17 | # You should have received a copy of the GNU General Public License | |
18 | # along with this program; if not, write to the Free Software | |
f96b4a7b NC |
19 | # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | # MA 02110-1301, USA. | |
3d3d428f NC |
21 | # |
22 | ||
075a2b89 | 23 | # This file is sourced from elf.em, and defines extra cr16-elf |
3d3d428f NC |
24 | # specific routines. |
25 | # | |
92b93329 | 26 | fragment <<EOF |
3d3d428f NC |
27 | |
28 | #include "ldctor.h" | |
ca05ca5e | 29 | #include "elf32-cr16.h" |
3d3d428f | 30 | |
7251ccbf SR |
31 | static void check_sections (bfd *, asection *, void *); |
32 | ||
33 | ||
34 | /* This function is run after all the input files have been opened. */ | |
35 | ||
36 | static void | |
37 | cr16_elf_after_open (void) | |
38 | { | |
39 | /* Call the standard elf routine. */ | |
40 | gld${EMULATION_NAME}_after_open (); | |
41 | ||
d003af55 AM |
42 | if (command_line.embedded_relocs |
43 | && !bfd_link_relocatable (&link_info)) | |
44 | { | |
45 | bfd *abfd; | |
7251ccbf SR |
46 | |
47 | /* In the embedded relocs mode we create a .emreloc section for each | |
48 | input file with a nonzero .data section. The BFD backend will fill in | |
49 | these sections with magic numbers which can be used to relocate the | |
50 | data section at run time. */ | |
c72f2fb2 | 51 | for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next) |
7251ccbf SR |
52 | { |
53 | asection *datasec; | |
54 | ||
55 | /* As first-order business, make sure that each input BFD is either | |
56 | COFF or ELF. We need to call a special BFD backend function to | |
57 | generate the embedded relocs, and we have such functions only for | |
58 | COFF and ELF. */ | |
59 | if (bfd_get_flavour (abfd) != bfd_target_coff_flavour | |
60 | && bfd_get_flavour (abfd) != bfd_target_elf_flavour) | |
df5f2391 | 61 | einfo (_("%F%P: %pB: all input objects must be COFF or ELF " |
d003af55 | 62 | "for --embedded-relocs\n")); |
7251ccbf SR |
63 | |
64 | datasec = bfd_get_section_by_name (abfd, ".data.rel"); | |
65 | ||
66 | /* Note that we assume that the reloc_count field has already | |
67 | been set up. We could call bfd_get_reloc_upper_bound, but | |
68 | that returns the size of a memory buffer rather than a reloc | |
69 | count. We do not want to call bfd_canonicalize_reloc, | |
70 | because although it would always work it would force us to | |
71 | read in the relocs into BFD canonical form, which would waste | |
72 | a significant amount of time and memory. */ | |
73 | if (datasec != NULL && datasec->reloc_count > 0) | |
74 | { | |
75 | asection *relsec; | |
76 | ||
77 | relsec = bfd_make_section (abfd, ".emreloc"); | |
78 | if (relsec == NULL | |
fd361982 AM |
79 | || !bfd_set_section_flags (relsec, (SEC_ALLOC |
80 | | SEC_LOAD | |
81 | | SEC_HAS_CONTENTS | |
82 | | SEC_IN_MEMORY)) | |
83 | || !bfd_set_section_alignment (relsec, 2) | |
84 | || !bfd_set_section_size (relsec, datasec->reloc_count * 8)) | |
df5f2391 | 85 | einfo (_("%F%P: %pB: can not create .emreloc section: %E\n")); |
7251ccbf SR |
86 | } |
87 | ||
88 | /* Double check that all other data sections are empty, as is | |
89 | required for embedded PIC code. */ | |
e4492aa0 | 90 | bfd_map_over_sections (abfd, check_sections, datasec); |
7251ccbf SR |
91 | } |
92 | } | |
93 | } | |
94 | ||
95 | /* Check that of the data sections, only the .data section has | |
96 | relocs. This is called via bfd_map_over_sections. */ | |
97 | ||
98 | static void | |
99 | check_sections (bfd *abfd, asection *sec, void *datasec) | |
100 | { | |
fd361982 | 101 | if ((strncmp (bfd_section_name (sec), ".data.rel", 9) == 0) |
d003af55 AM |
102 | && sec != datasec |
103 | && sec->reloc_count == 0 ) | |
df5f2391 | 104 | einfo (_("%X%P: %pB: section %s has relocs; can not use --embedded-relocs\n"), |
fd361982 | 105 | abfd, bfd_section_name (sec)); |
7251ccbf SR |
106 | } |
107 | ||
3d3d428f NC |
108 | static void |
109 | cr16elf_after_parse (void) | |
110 | { | |
111 | /* Always behave as if called with --sort-common command line | |
112 | option. | |
113 | This is to emulate the CRTools' method of keeping variables | |
114 | of different alignment in separate sections. */ | |
115 | config.sort_common = TRUE; | |
116 | ||
117 | /* Don't create a demand-paged executable, since this feature isn't | |
118 | meaninful in CR16 embedded systems. Moreover, when magic_demand_paged | |
119 | is true the link sometimes fails. */ | |
120 | config.magic_demand_paged = FALSE; | |
97b11f40 | 121 | |
d871d478 | 122 | ldelf_after_parse (); |
3d3d428f NC |
123 | } |
124 | ||
125 | /* This is called after the sections have been attached to output | |
126 | sections, but before any sizes or addresses have been set. */ | |
127 | ||
128 | static void | |
129 | cr16elf_before_allocation (void) | |
130 | { | |
131 | /* Call the default first. */ | |
132 | gld${EMULATION_NAME}_before_allocation (); | |
133 | ||
d003af55 AM |
134 | if (command_line.embedded_relocs |
135 | && (!bfd_link_relocatable (&link_info))) | |
136 | { | |
7251ccbf | 137 | |
d003af55 | 138 | bfd *abfd; |
7251ccbf | 139 | |
d003af55 | 140 | /* If we are generating embedded relocs, call a special BFD backend |
7251ccbf | 141 | routine to do the work. */ |
d003af55 AM |
142 | for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next) |
143 | { | |
7251ccbf SR |
144 | asection *datasec, *relsec; |
145 | char *errmsg; | |
146 | ||
147 | datasec = bfd_get_section_by_name (abfd, ".data.rel"); | |
148 | ||
149 | if (datasec == NULL || datasec->reloc_count == 0) | |
150 | continue; | |
151 | ||
152 | relsec = bfd_get_section_by_name (abfd, ".emreloc"); | |
153 | ASSERT (relsec != NULL); | |
154 | ||
155 | if (! bfd_cr16_elf32_create_embedded_relocs (abfd, &link_info, | |
d003af55 AM |
156 | datasec, relsec, |
157 | &errmsg)) | |
158 | { | |
159 | if (errmsg == NULL) | |
df5f2391 | 160 | einfo (_("%X%P: %pB: can not create runtime reloc information: %E\n"), |
d003af55 AM |
161 | abfd); |
162 | else | |
df5f2391 | 163 | einfo (_("%X%P: %pB: can not create runtime reloc information: %s\n"), |
d003af55 AM |
164 | abfd, errmsg); |
165 | } | |
166 | } | |
167 | } | |
7251ccbf | 168 | |
3d3d428f NC |
169 | /* Enable relaxation by default if the "--no-relax" option was not |
170 | specified. This is done here instead of in the before_parse hook | |
171 | because there is a check in main() to prohibit use of --relax and | |
172 | -r together. */ | |
28d5f677 NC |
173 | if (RELAXATION_DISABLED_BY_DEFAULT) |
174 | ENABLE_RELAXATION; | |
3d3d428f NC |
175 | } |
176 | ||
177 | EOF | |
178 | ||
3d3d428f NC |
179 | # Put these extra cr16-elf routines in ld_${EMULATION_NAME}_emulation |
180 | # | |
7251ccbf | 181 | LDEMUL_AFTER_OPEN=cr16_elf_after_open |
3d3d428f NC |
182 | LDEMUL_AFTER_PARSE=cr16elf_after_parse |
183 | LDEMUL_BEFORE_ALLOCATION=cr16elf_before_allocation |