| 1 | /* Emulation code used by all ELF targets. |
| 2 | Copyright (C) 1991-2020 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 "ctf-api.h" |
| 25 | #include "ld.h" |
| 26 | #include "ldmain.h" |
| 27 | #include "ldmisc.h" |
| 28 | #include "ldexp.h" |
| 29 | #include "ldlang.h" |
| 30 | #include "elf-bfd.h" |
| 31 | #include "elf/internal.h" |
| 32 | #include "ldelfgen.h" |
| 33 | |
| 34 | void |
| 35 | ldelf_map_segments (bfd_boolean need_layout) |
| 36 | { |
| 37 | int tries = 10; |
| 38 | |
| 39 | do |
| 40 | { |
| 41 | lang_relax_sections (need_layout); |
| 42 | need_layout = FALSE; |
| 43 | |
| 44 | if (link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour |
| 45 | && !bfd_link_relocatable (&link_info)) |
| 46 | { |
| 47 | bfd_size_type phdr_size; |
| 48 | |
| 49 | phdr_size = elf_program_header_size (link_info.output_bfd); |
| 50 | /* If we don't have user supplied phdrs, throw away any |
| 51 | previous linker generated program headers. */ |
| 52 | if (lang_phdr_list == NULL) |
| 53 | elf_seg_map (link_info.output_bfd) = NULL; |
| 54 | if (!_bfd_elf_map_sections_to_segments (link_info.output_bfd, |
| 55 | &link_info)) |
| 56 | einfo (_("%F%P: map sections to segments failed: %E\n")); |
| 57 | |
| 58 | if (phdr_size != elf_program_header_size (link_info.output_bfd)) |
| 59 | { |
| 60 | if (tries > 6) |
| 61 | /* The first few times we allow any change to |
| 62 | phdr_size . */ |
| 63 | need_layout = TRUE; |
| 64 | else if (phdr_size |
| 65 | < elf_program_header_size (link_info.output_bfd)) |
| 66 | /* After that we only allow the size to grow. */ |
| 67 | need_layout = TRUE; |
| 68 | else |
| 69 | elf_program_header_size (link_info.output_bfd) = phdr_size; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | while (need_layout && --tries); |
| 74 | |
| 75 | if (tries == 0) |
| 76 | einfo (_("%F%P: looping in map_segments")); |
| 77 | |
| 78 | if (link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour |
| 79 | && lang_phdr_list == NULL) |
| 80 | { |
| 81 | /* If we don't have user supplied phdrs, strip zero-sized dynamic |
| 82 | sections and regenerate program headers. */ |
| 83 | const struct elf_backend_data *bed |
| 84 | = get_elf_backend_data (link_info.output_bfd); |
| 85 | if (bed->elf_backend_strip_zero_sized_dynamic_sections |
| 86 | && !bed->elf_backend_strip_zero_sized_dynamic_sections |
| 87 | (&link_info)) |
| 88 | einfo (_("%F%P: failed to strip zero-sized dynamic sections")); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | #ifdef ENABLE_LIBCTF |
| 93 | /* We want to emit CTF early if and only if we are not targetting ELF with this |
| 94 | invocation. */ |
| 95 | |
| 96 | int |
| 97 | ldelf_emit_ctf_early (void) |
| 98 | { |
| 99 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour) |
| 100 | return 0; |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | /* Callbacks used to map from bfd types to libctf types, under libctf's |
| 105 | control. */ |
| 106 | |
| 107 | struct ctf_strtab_iter_cb_arg |
| 108 | { |
| 109 | struct elf_strtab_hash *strtab; |
| 110 | size_t next_i; |
| 111 | size_t next_idx; |
| 112 | }; |
| 113 | |
| 114 | /* Return strings from the strtab to libctf, one by one. Returns NULL when |
| 115 | iteration is complete. */ |
| 116 | |
| 117 | static const char * |
| 118 | ldelf_ctf_strtab_iter_cb (uint32_t *offset, void *arg_) |
| 119 | { |
| 120 | bfd_size_type off; |
| 121 | const char *ret; |
| 122 | |
| 123 | struct ctf_strtab_iter_cb_arg *arg = |
| 124 | (struct ctf_strtab_iter_cb_arg *) arg_; |
| 125 | |
| 126 | /* There is no zeroth string. */ |
| 127 | if (arg->next_i == 0) |
| 128 | arg->next_i = 1; |
| 129 | |
| 130 | if (arg->next_i >= _bfd_elf_strtab_len (arg->strtab)) |
| 131 | { |
| 132 | arg->next_i = 0; |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | ret = _bfd_elf_strtab_str (arg->strtab, arg->next_i++, &off); |
| 137 | *offset = off; |
| 138 | |
| 139 | /* If we've overflowed, we cannot share any further strings: the CTF |
| 140 | format cannot encode strings with such high offsets. */ |
| 141 | if (*offset != off) |
| 142 | return NULL; |
| 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | ldelf_acquire_strings_for_ctf |
| 149 | (struct ctf_dict *ctf_output, struct elf_strtab_hash *strtab) |
| 150 | { |
| 151 | struct ctf_strtab_iter_cb_arg args = { strtab, 0, 0 }; |
| 152 | if (!ctf_output) |
| 153 | return; |
| 154 | |
| 155 | if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour) |
| 156 | { |
| 157 | if (ctf_link_add_strtab (ctf_output, ldelf_ctf_strtab_iter_cb, |
| 158 | &args) < 0) |
| 159 | einfo (_("%F%P: warning: CTF strtab association failed; strings will " |
| 160 | "not be shared: %s\n"), |
| 161 | ctf_errmsg (ctf_errno (ctf_output))); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | ldelf_new_dynsym_for_ctf (struct ctf_dict *ctf_output, int symidx, |
| 167 | struct elf_internal_sym *sym) |
| 168 | { |
| 169 | ctf_link_sym_t lsym; |
| 170 | |
| 171 | if (!ctf_output) |
| 172 | return; |
| 173 | |
| 174 | /* New symbol. */ |
| 175 | if (sym != NULL) |
| 176 | { |
| 177 | lsym.st_name = NULL; |
| 178 | lsym.st_nameidx = sym->st_name; |
| 179 | lsym.st_nameidx_set = 1; |
| 180 | lsym.st_symidx = symidx; |
| 181 | lsym.st_shndx = sym->st_shndx; |
| 182 | lsym.st_type = ELF_ST_TYPE (sym->st_info); |
| 183 | lsym.st_value = sym->st_value; |
| 184 | if (ctf_link_add_linker_symbol (ctf_output, &lsym) < 0) |
| 185 | { |
| 186 | einfo (_("%F%P: warning: CTF symbol addition failed; CTF will " |
| 187 | "not be tied to symbols: %s\n"), |
| 188 | ctf_errmsg (ctf_errno (ctf_output))); |
| 189 | } |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | /* Shuffle all the symbols. */ |
| 194 | |
| 195 | if (ctf_link_shuffle_syms (ctf_output) < 0) |
| 196 | einfo (_("%F%P: warning: CTF symbol shuffling failed; CTF will " |
| 197 | "not be tied to symbols: %s\n"), |
| 198 | ctf_errmsg (ctf_errno (ctf_output))); |
| 199 | } |
| 200 | } |
| 201 | #else |
| 202 | int |
| 203 | ldelf_emit_ctf_early (void) |
| 204 | { |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | ldelf_acquire_strings_for_ctf (struct ctf_dict *ctf_output ATTRIBUTE_UNUSED, |
| 210 | struct elf_strtab_hash *strtab ATTRIBUTE_UNUSED) |
| 211 | {} |
| 212 | void |
| 213 | ldelf_new_dynsym_for_ctf (struct ctf_dict *ctf_output ATTRIBUTE_UNUSED, |
| 214 | int symidx ATTRIBUTE_UNUSED, |
| 215 | struct elf_internal_sym *sym ATTRIBUTE_UNUSED) |
| 216 | {} |
| 217 | #endif |