| 1 | /* Emulation code used by all ELF targets. |
| 2 | Copyright (C) 1991-2019 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of the GNU Binutils. |
| 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 3 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 |
| 18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
| 19 | MA 02110-1301, USA. */ |
| 20 | |
| 21 | #include "sysdep.h" |
| 22 | #include "bfd.h" |
| 23 | #include "bfdlink.h" |
| 24 | #include "ld.h" |
| 25 | #include "ldmain.h" |
| 26 | #include "ldmisc.h" |
| 27 | #include "ldexp.h" |
| 28 | #include "ldlang.h" |
| 29 | #include "elf-bfd.h" |
| 30 | #include "ldelfgen.h" |
| 31 | |
| 32 | void |
| 33 | ldelf_map_segments (bfd_boolean need_layout) |
| 34 | { |
| 35 | int tries = 10; |
| 36 | |
| 37 | do |
| 38 | { |
| 39 | lang_relax_sections (need_layout); |
| 40 | need_layout = FALSE; |
| 41 | |
| 42 | if (link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour |
| 43 | && !bfd_link_relocatable (&link_info)) |
| 44 | { |
| 45 | bfd_size_type phdr_size; |
| 46 | |
| 47 | phdr_size = elf_program_header_size (link_info.output_bfd); |
| 48 | /* If we don't have user supplied phdrs, throw away any |
| 49 | previous linker generated program headers. */ |
| 50 | if (lang_phdr_list == NULL) |
| 51 | elf_seg_map (link_info.output_bfd) = NULL; |
| 52 | if (!_bfd_elf_map_sections_to_segments (link_info.output_bfd, |
| 53 | &link_info)) |
| 54 | einfo (_("%F%P: map sections to segments failed: %E\n")); |
| 55 | |
| 56 | if (phdr_size != elf_program_header_size (link_info.output_bfd)) |
| 57 | { |
| 58 | if (tries > 6) |
| 59 | /* The first few times we allow any change to |
| 60 | phdr_size . */ |
| 61 | need_layout = TRUE; |
| 62 | else if (phdr_size |
| 63 | < elf_program_header_size (link_info.output_bfd)) |
| 64 | /* After that we only allow the size to grow. */ |
| 65 | need_layout = TRUE; |
| 66 | else |
| 67 | elf_program_header_size (link_info.output_bfd) = phdr_size; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | while (need_layout && --tries); |
| 72 | |
| 73 | if (tries == 0) |
| 74 | einfo (_("%F%P: looping in map_segments")); |
| 75 | } |