1# This shell script emits a C file. -*- C -*-
2#   Copyright 2006 Free Software Foundation, Inc.
3#
4# This file is part of GLD, the Gnu Linker.
5#
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.
10#
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.
15#
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 Foundation, Inc.,
18# 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
19#
20
21# This file is sourced from elf32.em and from ELF targets that use
22# generic.em.
23#
24cat >>e${EMULATION_NAME}.c <<EOF
25
26static void
27gld${EMULATION_NAME}_map_segments (bfd_boolean need_layout)
28{
29  int tries = 10;
30
31  do
32    {
33      if (need_layout)
34	{
35	  lang_reset_memory_regions ();
36
37	  /* Resize the sections.  */
38	  lang_size_sections (NULL, TRUE);
39
40	  /* Redo special stuff.  */
41	  ldemul_after_allocation ();
42
43	  /* Do the assignments again.  */
44	  lang_do_assignments ();
45
46	  need_layout = FALSE;
47	}
48
49      if (output_bfd->xvec->flavour == bfd_target_elf_flavour
50	  && !link_info.relocatable)
51	{
52	  bfd_size_type phdr_size;
53
54	  phdr_size = elf_tdata (output_bfd)->program_header_size;
55	  /* If we don't have user supplied phdrs, throw away any
56	     previous linker generated program headers.  */
57	  if (lang_phdr_list == NULL)
58	    elf_tdata (output_bfd)->segment_map = NULL;
59	  if (!_bfd_elf_map_sections_to_segments (output_bfd, &link_info))
60	    einfo ("%F%P: map sections to segments failed: %E\n");
61
62	  if (phdr_size != elf_tdata (output_bfd)->program_header_size)
63	    {
64	      if (tries > 6)
65		/* The first few times we allow any change to
66		   phdr_size .  */
67		need_layout = TRUE;
68	      else if (phdr_size < elf_tdata (output_bfd)->program_header_size)
69		/* After that we only allow the size to grow.  */
70		need_layout = TRUE;
71	      else
72		elf_tdata (output_bfd)->program_header_size = phdr_size;
73	    }
74	}
75    }
76  while (need_layout && --tries);
77
78  if (tries == 0)
79    einfo (_("%P%F: looping in map_segments"));
80}
81EOF
82