1*fae548d3Szrj /* Emulation code used by all ELF targets.
2*fae548d3Szrj Copyright (C) 1991-2020 Free Software Foundation, Inc.
3*fae548d3Szrj
4*fae548d3Szrj This file is part of the GNU Binutils.
5*fae548d3Szrj
6*fae548d3Szrj This program is free software; you can redistribute it and/or modify
7*fae548d3Szrj it under the terms of the GNU General Public License as published by
8*fae548d3Szrj the Free Software Foundation; either version 3 of the License, or
9*fae548d3Szrj (at your option) any later version.
10*fae548d3Szrj
11*fae548d3Szrj This program is distributed in the hope that it will be useful,
12*fae548d3Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
13*fae548d3Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*fae548d3Szrj GNU General Public License for more details.
15*fae548d3Szrj
16*fae548d3Szrj You should have received a copy of the GNU General Public License
17*fae548d3Szrj along with this program; if not, write to the Free Software
18*fae548d3Szrj Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19*fae548d3Szrj MA 02110-1301, USA. */
20*fae548d3Szrj
21*fae548d3Szrj #include "sysdep.h"
22*fae548d3Szrj #include "bfd.h"
23*fae548d3Szrj #include "bfdlink.h"
24*fae548d3Szrj #include "ctf-api.h"
25*fae548d3Szrj #include "ld.h"
26*fae548d3Szrj #include "ldmain.h"
27*fae548d3Szrj #include "ldmisc.h"
28*fae548d3Szrj #include "ldexp.h"
29*fae548d3Szrj #include "ldlang.h"
30*fae548d3Szrj #include "elf-bfd.h"
31*fae548d3Szrj #include "ldelfgen.h"
32*fae548d3Szrj
33*fae548d3Szrj void
ldelf_map_segments(bfd_boolean need_layout)34*fae548d3Szrj ldelf_map_segments (bfd_boolean need_layout)
35*fae548d3Szrj {
36*fae548d3Szrj int tries = 10;
37*fae548d3Szrj
38*fae548d3Szrj do
39*fae548d3Szrj {
40*fae548d3Szrj lang_relax_sections (need_layout);
41*fae548d3Szrj need_layout = FALSE;
42*fae548d3Szrj
43*fae548d3Szrj if (link_info.output_bfd->xvec->flavour == bfd_target_elf_flavour
44*fae548d3Szrj && !bfd_link_relocatable (&link_info))
45*fae548d3Szrj {
46*fae548d3Szrj bfd_size_type phdr_size;
47*fae548d3Szrj
48*fae548d3Szrj phdr_size = elf_program_header_size (link_info.output_bfd);
49*fae548d3Szrj /* If we don't have user supplied phdrs, throw away any
50*fae548d3Szrj previous linker generated program headers. */
51*fae548d3Szrj if (lang_phdr_list == NULL)
52*fae548d3Szrj elf_seg_map (link_info.output_bfd) = NULL;
53*fae548d3Szrj if (!_bfd_elf_map_sections_to_segments (link_info.output_bfd,
54*fae548d3Szrj &link_info))
55*fae548d3Szrj einfo (_("%F%P: map sections to segments failed: %E\n"));
56*fae548d3Szrj
57*fae548d3Szrj if (phdr_size != elf_program_header_size (link_info.output_bfd))
58*fae548d3Szrj {
59*fae548d3Szrj if (tries > 6)
60*fae548d3Szrj /* The first few times we allow any change to
61*fae548d3Szrj phdr_size . */
62*fae548d3Szrj need_layout = TRUE;
63*fae548d3Szrj else if (phdr_size
64*fae548d3Szrj < elf_program_header_size (link_info.output_bfd))
65*fae548d3Szrj /* After that we only allow the size to grow. */
66*fae548d3Szrj need_layout = TRUE;
67*fae548d3Szrj else
68*fae548d3Szrj elf_program_header_size (link_info.output_bfd) = phdr_size;
69*fae548d3Szrj }
70*fae548d3Szrj }
71*fae548d3Szrj }
72*fae548d3Szrj while (need_layout && --tries);
73*fae548d3Szrj
74*fae548d3Szrj if (tries == 0)
75*fae548d3Szrj einfo (_("%F%P: looping in map_segments"));
76*fae548d3Szrj }
77*fae548d3Szrj
78*fae548d3Szrj /* We want to emit CTF early if and only if we are not targetting ELF with this
79*fae548d3Szrj invocation. */
80*fae548d3Szrj
81*fae548d3Szrj int
ldelf_emit_ctf_early(void)82*fae548d3Szrj ldelf_emit_ctf_early (void)
83*fae548d3Szrj {
84*fae548d3Szrj if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
85*fae548d3Szrj return 0;
86*fae548d3Szrj return 1;
87*fae548d3Szrj }
88*fae548d3Szrj
89*fae548d3Szrj /* Callbacks used to map from bfd types to libctf types, under libctf's
90*fae548d3Szrj control. */
91*fae548d3Szrj
92*fae548d3Szrj struct ctf_strsym_iter_cb_arg
93*fae548d3Szrj {
94*fae548d3Szrj struct elf_sym_strtab *syms;
95*fae548d3Szrj bfd_size_type symcount;
96*fae548d3Szrj struct elf_strtab_hash *symstrtab;
97*fae548d3Szrj size_t next_i;
98*fae548d3Szrj size_t next_idx;
99*fae548d3Szrj };
100*fae548d3Szrj
101*fae548d3Szrj /* Return strings from the strtab to libctf, one by one. Returns NULL when
102*fae548d3Szrj iteration is complete. */
103*fae548d3Szrj
104*fae548d3Szrj static const char *
ldelf_ctf_strtab_iter_cb(uint32_t * offset,void * arg_)105*fae548d3Szrj ldelf_ctf_strtab_iter_cb (uint32_t *offset, void *arg_)
106*fae548d3Szrj {
107*fae548d3Szrj bfd_size_type off;
108*fae548d3Szrj const char *ret;
109*fae548d3Szrj
110*fae548d3Szrj struct ctf_strsym_iter_cb_arg *arg =
111*fae548d3Szrj (struct ctf_strsym_iter_cb_arg *) arg_;
112*fae548d3Szrj
113*fae548d3Szrj /* There is no zeroth string. */
114*fae548d3Szrj if (arg->next_i == 0)
115*fae548d3Szrj arg->next_i = 1;
116*fae548d3Szrj
117*fae548d3Szrj if (arg->next_i >= _bfd_elf_strtab_len (arg->symstrtab))
118*fae548d3Szrj {
119*fae548d3Szrj arg->next_i = 0;
120*fae548d3Szrj return NULL;
121*fae548d3Szrj }
122*fae548d3Szrj
123*fae548d3Szrj ret = _bfd_elf_strtab_str (arg->symstrtab, arg->next_i++, &off);
124*fae548d3Szrj *offset = off;
125*fae548d3Szrj
126*fae548d3Szrj /* If we've overflowed, we cannot share any further strings: the CTF
127*fae548d3Szrj format cannot encode strings with such high offsets. */
128*fae548d3Szrj if (*offset != off)
129*fae548d3Szrj return NULL;
130*fae548d3Szrj
131*fae548d3Szrj return ret;
132*fae548d3Szrj }
133*fae548d3Szrj
134*fae548d3Szrj /* Return symbols from the symbol table to libctf, one by one. We assume (and
135*fae548d3Szrj assert) that the symbols in the elf_link_hash_table are in strictly ascending
136*fae548d3Szrj order, and that none will be added in between existing ones. Returns NULL
137*fae548d3Szrj when iteration is complete. */
138*fae548d3Szrj
139*fae548d3Szrj static struct ctf_link_sym *
ldelf_ctf_symbols_iter_cb(struct ctf_link_sym * dest,void * arg_)140*fae548d3Szrj ldelf_ctf_symbols_iter_cb (struct ctf_link_sym *dest,
141*fae548d3Szrj void *arg_)
142*fae548d3Szrj {
143*fae548d3Szrj struct ctf_strsym_iter_cb_arg *arg =
144*fae548d3Szrj (struct ctf_strsym_iter_cb_arg *) arg_;
145*fae548d3Szrj
146*fae548d3Szrj if (arg->next_i > arg->symcount)
147*fae548d3Szrj {
148*fae548d3Szrj arg->next_i = 0;
149*fae548d3Szrj arg->next_idx = 0;
150*fae548d3Szrj return NULL;
151*fae548d3Szrj }
152*fae548d3Szrj
153*fae548d3Szrj ASSERT (arg->syms[arg->next_i].dest_index == arg->next_idx);
154*fae548d3Szrj dest->st_name = _bfd_elf_strtab_str (arg->symstrtab, arg->next_i, NULL);
155*fae548d3Szrj dest->st_shndx = arg->syms[arg->next_i].sym.st_shndx;
156*fae548d3Szrj dest->st_type = ELF_ST_TYPE (arg->syms[arg->next_i].sym.st_info);
157*fae548d3Szrj dest->st_value = arg->syms[arg->next_i].sym.st_value;
158*fae548d3Szrj arg->next_i++;
159*fae548d3Szrj return dest;
160*fae548d3Szrj }
161*fae548d3Szrj
162*fae548d3Szrj void
ldelf_examine_strtab_for_ctf(struct ctf_file * ctf_output,struct elf_sym_strtab * syms,bfd_size_type symcount,struct elf_strtab_hash * symstrtab)163*fae548d3Szrj ldelf_examine_strtab_for_ctf
164*fae548d3Szrj (struct ctf_file *ctf_output, struct elf_sym_strtab *syms,
165*fae548d3Szrj bfd_size_type symcount, struct elf_strtab_hash *symstrtab)
166*fae548d3Szrj {
167*fae548d3Szrj struct ctf_strsym_iter_cb_arg args = { syms, symcount, symstrtab,
168*fae548d3Szrj 0, 0 };
169*fae548d3Szrj if (!ctf_output)
170*fae548d3Szrj return;
171*fae548d3Szrj
172*fae548d3Szrj if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
173*fae548d3Szrj && !bfd_link_relocatable (&link_info))
174*fae548d3Szrj {
175*fae548d3Szrj if (ctf_link_add_strtab (ctf_output, ldelf_ctf_strtab_iter_cb,
176*fae548d3Szrj &args) < 0)
177*fae548d3Szrj einfo (_("%F%P: warning: CTF strtab association failed; strings will "
178*fae548d3Szrj "not be shared: %s\n"),
179*fae548d3Szrj ctf_errmsg (ctf_errno (ctf_output)));
180*fae548d3Szrj
181*fae548d3Szrj if (ctf_link_shuffle_syms (ctf_output, ldelf_ctf_symbols_iter_cb,
182*fae548d3Szrj &args) < 0)
183*fae548d3Szrj einfo (_("%F%P: warning: CTF symbol shuffling failed; slight space "
184*fae548d3Szrj "cost: %s\n"), ctf_errmsg (ctf_errno (ctf_output)));
185*fae548d3Szrj }
186*fae548d3Szrj }
187