1 # This shell script emits a C file. -*- C -*-
2 # Copyright 2007, 2008, 2009 Free Software Foundation, Inc.
3 # Contributed by M R Swami Reddy <MR.Swami.Reddy@nsc.com>
5 # This file is part of the GNU Binutils.
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
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
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.
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
19 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 # This file is sourced from elf32.em, and defines extra cr16-elf
30 static void check_sections (bfd *, asection *, void *);
33 /* This function is run after all the input files have been opened. */
36 cr16_elf_after_open (void)
38 /* Call the standard elf routine. */
39 gld${EMULATION_NAME}_after_open ();
41 if (command_line.embedded_relocs
42 && (! link_info.relocatable))
46 /* In the embedded relocs mode we create a .emreloc section for each
47 input file with a nonzero .data section. The BFD backend will fill in
48 these sections with magic numbers which can be used to relocate the
49 data section at run time. */
50 for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
54 /* As first-order business, make sure that each input BFD is either
55 COFF or ELF. We need to call a special BFD backend function to
56 generate the embedded relocs, and we have such functions only for
58 if (bfd_get_flavour (abfd) != bfd_target_coff_flavour
59 && bfd_get_flavour (abfd) != bfd_target_elf_flavour)
60 einfo ("%F%B: all input objects must be COFF or ELF for --embedded-relocs\n");
62 datasec = bfd_get_section_by_name (abfd, ".data.rel");
64 /* Note that we assume that the reloc_count field has already
65 been set up. We could call bfd_get_reloc_upper_bound, but
66 that returns the size of a memory buffer rather than a reloc
67 count. We do not want to call bfd_canonicalize_reloc,
68 because although it would always work it would force us to
69 read in the relocs into BFD canonical form, which would waste
70 a significant amount of time and memory. */
71 if (datasec != NULL && datasec->reloc_count > 0)
75 relsec = bfd_make_section (abfd, ".emreloc");
77 || ! bfd_set_section_flags (abfd, relsec,
82 || ! bfd_set_section_alignment (abfd, relsec, 2)
83 || ! bfd_set_section_size (abfd, relsec,
84 datasec->reloc_count * 8))
85 einfo ("%F%B: can not create .emreloc section: %E\n");
88 /* Double check that all other data sections are empty, as is
89 required for embedded PIC code. */
90 bfd_map_over_sections (abfd, check_sections, datasec);
95 /* Check that of the data sections, only the .data section has
96 relocs. This is called via bfd_map_over_sections. */
99 check_sections (bfd *abfd, asection *sec, void *datasec)
101 if ((strncmp (bfd_get_section_name (abfd, sec), ".data.rel", 9) == 0)
103 && sec->reloc_count == 0 )
104 einfo ("%B%X: section %s has relocs; can not use --embedded-relocs\n",
105 abfd, bfd_get_section_name (abfd, sec));
109 cr16elf_after_parse (void)
111 /* Always behave as if called with --sort-common command line
113 This is to emulate the CRTools' method of keeping variables
114 of different alignment in separate sections. */
115 config.sort_common = TRUE;
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;
122 after_parse_default ();
125 /* This is called after the sections have been attached to output
126 sections, but before any sizes or addresses have been set. */
129 cr16elf_before_allocation (void)
131 /* Call the default first. */
132 gld${EMULATION_NAME}_before_allocation ();
134 if (command_line.embedded_relocs
135 && (! link_info.relocatable))
140 /* If we are generating embedded relocs, call a special BFD backend
141 routine to do the work. */
142 for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link_next)
144 asection *datasec, *relsec;
147 datasec = bfd_get_section_by_name (abfd, ".data.rel");
149 if (datasec == NULL || datasec->reloc_count == 0)
152 relsec = bfd_get_section_by_name (abfd, ".emreloc");
153 ASSERT (relsec != NULL);
155 if (! bfd_cr16_elf32_create_embedded_relocs (abfd, &link_info,
160 einfo ("%B%X: can not create runtime reloc information: %E\n",
163 einfo ("%X%B: can not create runtime reloc information: %s\n",
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
173 if (RELAXATION_DISABLED_BY_DEFAULT)
179 # Put these extra cr16-elf routines in ld_${EMULATION_NAME}_emulation
181 LDEMUL_AFTER_OPEN=cr16_elf_after_open
182 LDEMUL_AFTER_PARSE=cr16elf_after_parse
183 LDEMUL_BEFORE_ALLOCATION=cr16elf_before_allocation