Coverage Report

Created: 2025-07-08 11:15

/src/binutils-gdb/bfd/elfcode.h
Line
Count
Source (jump to first uncovered line)
1
/* ELF executable support for BFD.
2
   Copyright (C) 1991-2025 Free Software Foundation, Inc.
3
4
   Written by Fred Fish @ Cygnus Support, from information published
5
   in "UNIX System V Release 4, Programmers Guide: ANSI C and
6
   Programming Support Tools".  Sufficient support for gdb.
7
8
   Rewritten by Mark Eichin @ Cygnus Support, from information
9
   published in "System V Application Binary Interface", chapters 4
10
   and 5, as well as the various "Processor Supplement" documents
11
   derived from it. Added support for assembler and other object file
12
   utilities.  Further work done by Ken Raeburn (Cygnus Support), Michael
13
   Meissner (Open Software Foundation), and Peter Hoogenboom (University
14
   of Utah) to finish and extend this.
15
16
   This file is part of BFD, the Binary File Descriptor library.
17
18
   This program is free software; you can redistribute it and/or modify
19
   it under the terms of the GNU General Public License as published by
20
   the Free Software Foundation; either version 3 of the License, or
21
   (at your option) any later version.
22
23
   This program is distributed in the hope that it will be useful,
24
   but WITHOUT ANY WARRANTY; without even the implied warranty of
25
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
   GNU General Public License for more details.
27
28
   You should have received a copy of the GNU General Public License
29
   along with this program; if not, write to the Free Software
30
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
31
   MA 02110-1301, USA.  */
32
33
34
/* Problems and other issues to resolve.
35
36
   (1)  BFD expects there to be some fixed number of "sections" in
37
  the object file.  I.E. there is a "section_count" variable in the
38
  bfd structure which contains the number of sections.  However, ELF
39
  supports multiple "views" of a file.  In particular, with current
40
  implementations, executable files typically have two tables, a
41
  program header table and a section header table, both of which
42
  partition the executable.
43
44
  In ELF-speak, the "linking view" of the file uses the section header
45
  table to access "sections" within the file, and the "execution view"
46
  uses the program header table to access "segments" within the file.
47
  "Segments" typically may contain all the data from one or more
48
  "sections".
49
50
  Note that the section header table is optional in ELF executables,
51
  but it is this information that is most useful to gdb.  If the
52
  section header table is missing, then gdb should probably try
53
  to make do with the program header table.  (FIXME)
54
55
   (2)  The code in this file is compiled twice, once in 32-bit mode and
56
  once in 64-bit mode.  More of it should be made size-independent
57
  and moved into elf.c.
58
59
   (3)  ELF section symbols are handled rather sloppily now.  This should
60
  be cleaned up, and ELF section symbols reconciled with BFD section
61
  symbols.
62
63
   (4)  We need a published spec for 64-bit ELF.  We've got some stuff here
64
  that we're using for SPARC V9 64-bit chips, but don't assume that
65
  it's cast in stone.
66
 */
67
68
#include "sysdep.h"
69
#include "bfd.h"
70
#include "libiberty.h"
71
#include "bfdlink.h"
72
#include "libbfd.h"
73
#include "elf-bfd.h"
74
#include "libiberty.h"
75
76
/* Renaming structures, typedefs, macros and functions to be size-specific.  */
77
491M
#define Elf_External_Ehdr NAME(Elf,External_Ehdr)
78
32.6k
#define Elf_External_Sym  NAME(Elf,External_Sym)
79
489M
#define Elf_External_Shdr NAME(Elf,External_Shdr)
80
28.7M
#define Elf_External_Phdr NAME(Elf,External_Phdr)
81
0
#define Elf_External_Rel  NAME(Elf,External_Rel)
82
70.8k
#define Elf_External_Rela NAME(Elf,External_Rela)
83
0
#define Elf_External_Dyn  NAME(Elf,External_Dyn)
84
85
#define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command)
86
#define elf_core_file_failing_signal  NAME(bfd_elf,core_file_failing_signal)
87
#define elf_core_file_matches_executable_p \
88
  NAME(bfd_elf,core_file_matches_executable_p)
89
#define elf_core_file_pid   NAME(bfd_elf,core_file_pid)
90
#define elf_object_p      NAME(bfd_elf,object_p)
91
#define elf_core_file_p     NAME(bfd_elf,core_file_p)
92
#define elf_get_symtab_upper_bound  NAME(bfd_elf,get_symtab_upper_bound)
93
#define elf_get_dynamic_symtab_upper_bound \
94
  NAME(bfd_elf,get_dynamic_symtab_upper_bound)
95
67.2k
#define elf_swap_reloc_in   NAME(bfd_elf,swap_reloc_in)
96
2.26M
#define elf_swap_reloca_in    NAME(bfd_elf,swap_reloca_in)
97
0
#define elf_swap_reloc_out    NAME(bfd_elf,swap_reloc_out)
98
1.25k
#define elf_swap_reloca_out   NAME(bfd_elf,swap_reloca_out)
99
#define elf_swap_symbol_in    NAME(bfd_elf,swap_symbol_in)
100
#define elf_swap_symbol_out   NAME(bfd_elf,swap_symbol_out)
101
28.7M
#define elf_swap_phdr_in    NAME(bfd_elf,swap_phdr_in)
102
240
#define elf_swap_phdr_out   NAME(bfd_elf,swap_phdr_out)
103
#define elf_swap_dyn_in     NAME(bfd_elf,swap_dyn_in)
104
#define elf_swap_dyn_out    NAME(bfd_elf,swap_dyn_out)
105
#define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound)
106
#define elf_canonicalize_reloc    NAME(bfd_elf,canonicalize_reloc)
107
#define elf_slurp_symbol_table    NAME(bfd_elf,slurp_symbol_table)
108
#define elf_canonicalize_symtab   NAME(bfd_elf,canonicalize_symtab)
109
#define elf_canonicalize_dynamic_symtab \
110
  NAME(bfd_elf,canonicalize_dynamic_symtab)
111
#define elf_get_synthetic_symtab \
112
  NAME(bfd_elf,get_synthetic_symtab)
113
#define elf_make_empty_symbol   NAME(bfd_elf,make_empty_symbol)
114
#define elf_get_symbol_info   NAME(bfd_elf,get_symbol_info)
115
#define elf_get_lineno      NAME(bfd_elf,get_lineno)
116
#define elf_set_arch_mach   NAME(bfd_elf,set_arch_mach)
117
#define elf_find_nearest_line   NAME(bfd_elf,find_nearest_line)
118
#define elf_sizeof_headers    NAME(bfd_elf,sizeof_headers)
119
#define elf_set_section_contents  NAME(bfd_elf,set_section_contents)
120
#define elf_no_info_to_howto    NAME(bfd_elf,no_info_to_howto)
121
#define elf_no_info_to_howto_rel  NAME(bfd_elf,no_info_to_howto_rel)
122
#define elf_find_section    NAME(bfd_elf,find_section)
123
#define elf_write_shdrs_and_ehdr  NAME(bfd_elf,write_shdrs_and_ehdr)
124
#define elf_write_out_phdrs   NAME(bfd_elf,write_out_phdrs)
125
#define elf_checksum_contents   NAME(bfd_elf,checksum_contents)
126
#define elf_write_relocs    NAME(bfd_elf,write_relocs)
127
#define elf_slurp_reloc_table   NAME(bfd_elf,slurp_reloc_table)
128
129
#if ARCH_SIZE == 64
130
70.8k
#define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
131
6.62M
#define ELF_R_SYM(X)  ELF64_R_SYM(X)
132
#define ELF_R_TYPE(X) ELF64_R_TYPE(X)
133
19.0M
#define ELFCLASS  ELFCLASS64
134
#define FILE_ALIGN  8
135
#define LOG_FILE_ALIGN  3
136
#endif
137
#if ARCH_SIZE == 32
138
0
#define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
139
211k
#define ELF_R_SYM(X)  ELF32_R_SYM(X)
140
#define ELF_R_TYPE(X) ELF32_R_TYPE(X)
141
66.2M
#define ELFCLASS  ELFCLASS32
142
#define FILE_ALIGN  4
143
#define LOG_FILE_ALIGN  2
144
#endif
145
146
#if DEBUG & 2
147
static void elf_debug_section (int, Elf_Internal_Shdr *);
148
#endif
149
#if DEBUG & 1
150
static void elf_debug_file (Elf_Internal_Ehdr *);
151
#endif
152

153
/* Structure swapping routines */
154
155
/* Should perhaps use put_offset, put_word, etc.  For now, the two versions
156
   can be handled by explicitly specifying 32 bits or "the long type".  */
157
#if ARCH_SIZE == 64
158
180k
#define H_PUT_WORD    H_PUT_64
159
71.0k
#define H_PUT_SIGNED_WORD H_PUT_S64
160
143M
#define H_GET_WORD    H_GET_64
161
20.9M
#define H_GET_SIGNED_WORD H_GET_S64
162
#endif
163
#if ARCH_SIZE == 32
164
60.8k
#define H_PUT_WORD    H_PUT_32
165
78
#define H_PUT_SIGNED_WORD H_PUT_S32
166
151M
#define H_GET_WORD    H_GET_32
167
3.92M
#define H_GET_SIGNED_WORD H_GET_S32
168
#endif
169
170
/* Translate an ELF symbol in external format into an ELF symbol in internal
171
   format.  */
172
173
bool
174
elf_swap_symbol_in (bfd *abfd,
175
        const void *psrc,
176
        const void *pshn,
177
        Elf_Internal_Sym *dst)
178
1.55M
{
179
1.55M
  const Elf_External_Sym *src = (const Elf_External_Sym *) psrc;
180
1.55M
  const Elf_External_Sym_Shndx *shndx = (const Elf_External_Sym_Shndx *) pshn;
181
1.55M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
182
183
1.55M
  dst->st_name = H_GET_32 (abfd, src->st_name);
184
1.55M
  if (signed_vma)
185
37.3k
    dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value);
186
1.51M
  else
187
1.51M
    dst->st_value = H_GET_WORD (abfd, src->st_value);
188
1.55M
  dst->st_size = H_GET_WORD (abfd, src->st_size);
189
1.55M
  dst->st_info = H_GET_8 (abfd, src->st_info);
190
1.55M
  dst->st_other = H_GET_8 (abfd, src->st_other);
191
1.55M
  dst->st_shndx = H_GET_16 (abfd, src->st_shndx);
192
1.55M
  if (dst->st_shndx == (SHN_XINDEX & 0xffff))
193
5.24k
    {
194
5.24k
      if (shndx == NULL)
195
1.67k
  return false;
196
3.56k
      dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx);
197
3.56k
    }
198
1.55M
  else if (dst->st_shndx >= (SHN_LORESERVE & 0xffff))
199
63.0k
    dst->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
200
1.55M
  dst->st_target_internal = 0;
201
1.55M
  return true;
202
1.55M
}
bfd_elf64_swap_symbol_in
Line
Count
Source
178
764k
{
179
764k
  const Elf_External_Sym *src = (const Elf_External_Sym *) psrc;
180
764k
  const Elf_External_Sym_Shndx *shndx = (const Elf_External_Sym_Shndx *) pshn;
181
764k
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
182
183
764k
  dst->st_name = H_GET_32 (abfd, src->st_name);
184
764k
  if (signed_vma)
185
14.2k
    dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value);
186
749k
  else
187
749k
    dst->st_value = H_GET_WORD (abfd, src->st_value);
188
764k
  dst->st_size = H_GET_WORD (abfd, src->st_size);
189
764k
  dst->st_info = H_GET_8 (abfd, src->st_info);
190
764k
  dst->st_other = H_GET_8 (abfd, src->st_other);
191
764k
  dst->st_shndx = H_GET_16 (abfd, src->st_shndx);
192
764k
  if (dst->st_shndx == (SHN_XINDEX & 0xffff))
193
3.08k
    {
194
3.08k
      if (shndx == NULL)
195
1.60k
  return false;
196
1.47k
      dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx);
197
1.47k
    }
198
760k
  else if (dst->st_shndx >= (SHN_LORESERVE & 0xffff))
199
7.87k
    dst->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
200
762k
  dst->st_target_internal = 0;
201
762k
  return true;
202
764k
}
bfd_elf32_swap_symbol_in
Line
Count
Source
178
791k
{
179
791k
  const Elf_External_Sym *src = (const Elf_External_Sym *) psrc;
180
791k
  const Elf_External_Sym_Shndx *shndx = (const Elf_External_Sym_Shndx *) pshn;
181
791k
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
182
183
791k
  dst->st_name = H_GET_32 (abfd, src->st_name);
184
791k
  if (signed_vma)
185
23.1k
    dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value);
186
768k
  else
187
768k
    dst->st_value = H_GET_WORD (abfd, src->st_value);
188
791k
  dst->st_size = H_GET_WORD (abfd, src->st_size);
189
791k
  dst->st_info = H_GET_8 (abfd, src->st_info);
190
791k
  dst->st_other = H_GET_8 (abfd, src->st_other);
191
791k
  dst->st_shndx = H_GET_16 (abfd, src->st_shndx);
192
791k
  if (dst->st_shndx == (SHN_XINDEX & 0xffff))
193
2.16k
    {
194
2.16k
      if (shndx == NULL)
195
67
  return false;
196
2.09k
      dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx);
197
2.09k
    }
198
789k
  else if (dst->st_shndx >= (SHN_LORESERVE & 0xffff))
199
55.1k
    dst->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
200
791k
  dst->st_target_internal = 0;
201
791k
  return true;
202
791k
}
203
204
/* Translate an ELF symbol in internal format into an ELF symbol in external
205
   format.  */
206
207
void
208
elf_swap_symbol_out (bfd *abfd,
209
         const Elf_Internal_Sym *src,
210
         void *cdst,
211
         void *shndx)
212
32.6k
{
213
32.6k
  unsigned int tmp;
214
32.6k
  Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
215
32.6k
  H_PUT_32 (abfd, src->st_name, dst->st_name);
216
32.6k
  H_PUT_WORD (abfd, src->st_value, dst->st_value);
217
32.6k
  H_PUT_WORD (abfd, src->st_size, dst->st_size);
218
32.6k
  H_PUT_8 (abfd, src->st_info, dst->st_info);
219
32.6k
  H_PUT_8 (abfd, src->st_other, dst->st_other);
220
32.6k
  tmp = src->st_shndx;
221
32.6k
  if (tmp >= (SHN_LORESERVE & 0xffff) && tmp < SHN_LORESERVE)
222
0
    {
223
0
      if (shndx == NULL)
224
0
  abort ();
225
0
      H_PUT_32 (abfd, tmp, shndx);
226
0
      tmp = SHN_XINDEX & 0xffff;
227
0
    }
228
32.6k
  H_PUT_16 (abfd, tmp, dst->st_shndx);
229
32.6k
}
bfd_elf64_swap_symbol_out
Line
Count
Source
212
7.06k
{
213
7.06k
  unsigned int tmp;
214
7.06k
  Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
215
7.06k
  H_PUT_32 (abfd, src->st_name, dst->st_name);
216
7.06k
  H_PUT_WORD (abfd, src->st_value, dst->st_value);
217
7.06k
  H_PUT_WORD (abfd, src->st_size, dst->st_size);
218
7.06k
  H_PUT_8 (abfd, src->st_info, dst->st_info);
219
7.06k
  H_PUT_8 (abfd, src->st_other, dst->st_other);
220
7.06k
  tmp = src->st_shndx;
221
7.06k
  if (tmp >= (SHN_LORESERVE & 0xffff) && tmp < SHN_LORESERVE)
222
0
    {
223
0
      if (shndx == NULL)
224
0
  abort ();
225
0
      H_PUT_32 (abfd, tmp, shndx);
226
0
      tmp = SHN_XINDEX & 0xffff;
227
0
    }
228
7.06k
  H_PUT_16 (abfd, tmp, dst->st_shndx);
229
7.06k
}
bfd_elf32_swap_symbol_out
Line
Count
Source
212
25.6k
{
213
25.6k
  unsigned int tmp;
214
25.6k
  Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
215
25.6k
  H_PUT_32 (abfd, src->st_name, dst->st_name);
216
25.6k
  H_PUT_WORD (abfd, src->st_value, dst->st_value);
217
25.6k
  H_PUT_WORD (abfd, src->st_size, dst->st_size);
218
25.6k
  H_PUT_8 (abfd, src->st_info, dst->st_info);
219
25.6k
  H_PUT_8 (abfd, src->st_other, dst->st_other);
220
25.6k
  tmp = src->st_shndx;
221
25.6k
  if (tmp >= (SHN_LORESERVE & 0xffff) && tmp < SHN_LORESERVE)
222
0
    {
223
0
      if (shndx == NULL)
224
0
  abort ();
225
0
      H_PUT_32 (abfd, tmp, shndx);
226
0
      tmp = SHN_XINDEX & 0xffff;
227
0
    }
228
25.6k
  H_PUT_16 (abfd, tmp, dst->st_shndx);
229
25.6k
}
230
231
/* Translate an ELF file header in external format into an ELF file header in
232
   internal format.  */
233
234
static void
235
elf_swap_ehdr_in (bfd *abfd,
236
      const Elf_External_Ehdr *src,
237
      Elf_Internal_Ehdr *dst)
238
22.8M
{
239
22.8M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
240
22.8M
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
241
22.8M
  dst->e_type = H_GET_16 (abfd, src->e_type);
242
22.8M
  dst->e_machine = H_GET_16 (abfd, src->e_machine);
243
22.8M
  dst->e_version = H_GET_32 (abfd, src->e_version);
244
22.8M
  if (signed_vma)
245
2.56M
    dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry);
246
20.3M
  else
247
20.3M
    dst->e_entry = H_GET_WORD (abfd, src->e_entry);
248
22.8M
  dst->e_phoff = H_GET_WORD (abfd, src->e_phoff);
249
22.8M
  dst->e_shoff = H_GET_WORD (abfd, src->e_shoff);
250
22.8M
  dst->e_flags = H_GET_32 (abfd, src->e_flags);
251
22.8M
  dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize);
252
22.8M
  dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize);
253
22.8M
  dst->e_phnum = H_GET_16 (abfd, src->e_phnum);
254
22.8M
  dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize);
255
22.8M
  dst->e_shnum = H_GET_16 (abfd, src->e_shnum);
256
22.8M
  dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx);
257
22.8M
}
elf64.c:elf_swap_ehdr_in
Line
Count
Source
238
5.91M
{
239
5.91M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
240
5.91M
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
241
5.91M
  dst->e_type = H_GET_16 (abfd, src->e_type);
242
5.91M
  dst->e_machine = H_GET_16 (abfd, src->e_machine);
243
5.91M
  dst->e_version = H_GET_32 (abfd, src->e_version);
244
5.91M
  if (signed_vma)
245
754k
    dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry);
246
5.15M
  else
247
5.15M
    dst->e_entry = H_GET_WORD (abfd, src->e_entry);
248
5.91M
  dst->e_phoff = H_GET_WORD (abfd, src->e_phoff);
249
5.91M
  dst->e_shoff = H_GET_WORD (abfd, src->e_shoff);
250
5.91M
  dst->e_flags = H_GET_32 (abfd, src->e_flags);
251
5.91M
  dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize);
252
5.91M
  dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize);
253
5.91M
  dst->e_phnum = H_GET_16 (abfd, src->e_phnum);
254
5.91M
  dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize);
255
5.91M
  dst->e_shnum = H_GET_16 (abfd, src->e_shnum);
256
5.91M
  dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx);
257
5.91M
}
elf32.c:elf_swap_ehdr_in
Line
Count
Source
238
16.9M
{
239
16.9M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
240
16.9M
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
241
16.9M
  dst->e_type = H_GET_16 (abfd, src->e_type);
242
16.9M
  dst->e_machine = H_GET_16 (abfd, src->e_machine);
243
16.9M
  dst->e_version = H_GET_32 (abfd, src->e_version);
244
16.9M
  if (signed_vma)
245
1.80M
    dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry);
246
15.1M
  else
247
15.1M
    dst->e_entry = H_GET_WORD (abfd, src->e_entry);
248
16.9M
  dst->e_phoff = H_GET_WORD (abfd, src->e_phoff);
249
16.9M
  dst->e_shoff = H_GET_WORD (abfd, src->e_shoff);
250
16.9M
  dst->e_flags = H_GET_32 (abfd, src->e_flags);
251
16.9M
  dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize);
252
16.9M
  dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize);
253
16.9M
  dst->e_phnum = H_GET_16 (abfd, src->e_phnum);
254
16.9M
  dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize);
255
16.9M
  dst->e_shnum = H_GET_16 (abfd, src->e_shnum);
256
16.9M
  dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx);
257
16.9M
}
258
259
/* Translate an ELF file header in internal format into an ELF file header in
260
   external format.  */
261
262
static void
263
elf_swap_ehdr_out (bfd *abfd,
264
       const Elf_Internal_Ehdr *src,
265
       Elf_External_Ehdr *dst)
266
197
{
267
197
  unsigned int tmp;
268
197
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
269
197
  bool no_section_header = (abfd->flags & BFD_NO_SECTION_HEADER) != 0;
270
197
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
271
  /* note that all elements of dst are *arrays of unsigned char* already...  */
272
197
  H_PUT_16 (abfd, src->e_type, dst->e_type);
273
197
  H_PUT_16 (abfd, src->e_machine, dst->e_machine);
274
197
  H_PUT_32 (abfd, src->e_version, dst->e_version);
275
197
  if (signed_vma)
276
197
    H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
277
196
  else
278
197
    H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
279
197
  H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
280
197
  if (no_section_header)
281
197
    H_PUT_WORD (abfd, 0, dst->e_shoff);
282
197
  else
283
197
    H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
284
197
  H_PUT_32 (abfd, src->e_flags, dst->e_flags);
285
197
  H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
286
197
  H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
287
197
  tmp = src->e_phnum;
288
197
  if (tmp > PN_XNUM)
289
0
    tmp = PN_XNUM;
290
197
  H_PUT_16 (abfd, tmp, dst->e_phnum);
291
197
  if (no_section_header)
292
0
    {
293
0
      H_PUT_16 (abfd, 0, dst->e_shentsize);
294
0
      H_PUT_16 (abfd, 0, dst->e_shnum);
295
0
      H_PUT_16 (abfd, 0, dst->e_shstrndx);
296
0
    }
297
197
  else
298
197
    {
299
197
      H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
300
197
      tmp = src->e_shnum;
301
197
      if (tmp >= (SHN_LORESERVE & 0xffff))
302
0
  tmp = SHN_UNDEF;
303
197
      H_PUT_16 (abfd, tmp, dst->e_shnum);
304
197
      tmp = src->e_shstrndx;
305
197
      if (tmp >= (SHN_LORESERVE & 0xffff))
306
0
  tmp = SHN_XINDEX & 0xffff;
307
197
      H_PUT_16 (abfd, tmp, dst->e_shstrndx);
308
197
    }
309
197
}
elf64.c:elf_swap_ehdr_out
Line
Count
Source
266
119
{
267
119
  unsigned int tmp;
268
119
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
269
119
  bool no_section_header = (abfd->flags & BFD_NO_SECTION_HEADER) != 0;
270
119
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
271
  /* note that all elements of dst are *arrays of unsigned char* already...  */
272
119
  H_PUT_16 (abfd, src->e_type, dst->e_type);
273
119
  H_PUT_16 (abfd, src->e_machine, dst->e_machine);
274
119
  H_PUT_32 (abfd, src->e_version, dst->e_version);
275
119
  if (signed_vma)
276
119
    H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
277
119
  else
278
119
    H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
279
119
  H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
280
119
  if (no_section_header)
281
119
    H_PUT_WORD (abfd, 0, dst->e_shoff);
282
119
  else
283
119
    H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
284
119
  H_PUT_32 (abfd, src->e_flags, dst->e_flags);
285
119
  H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
286
119
  H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
287
119
  tmp = src->e_phnum;
288
119
  if (tmp > PN_XNUM)
289
0
    tmp = PN_XNUM;
290
119
  H_PUT_16 (abfd, tmp, dst->e_phnum);
291
119
  if (no_section_header)
292
0
    {
293
0
      H_PUT_16 (abfd, 0, dst->e_shentsize);
294
0
      H_PUT_16 (abfd, 0, dst->e_shnum);
295
0
      H_PUT_16 (abfd, 0, dst->e_shstrndx);
296
0
    }
297
119
  else
298
119
    {
299
119
      H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
300
119
      tmp = src->e_shnum;
301
119
      if (tmp >= (SHN_LORESERVE & 0xffff))
302
0
  tmp = SHN_UNDEF;
303
119
      H_PUT_16 (abfd, tmp, dst->e_shnum);
304
119
      tmp = src->e_shstrndx;
305
119
      if (tmp >= (SHN_LORESERVE & 0xffff))
306
0
  tmp = SHN_XINDEX & 0xffff;
307
119
      H_PUT_16 (abfd, tmp, dst->e_shstrndx);
308
119
    }
309
119
}
elf32.c:elf_swap_ehdr_out
Line
Count
Source
266
78
{
267
78
  unsigned int tmp;
268
78
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
269
78
  bool no_section_header = (abfd->flags & BFD_NO_SECTION_HEADER) != 0;
270
78
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
271
  /* note that all elements of dst are *arrays of unsigned char* already...  */
272
78
  H_PUT_16 (abfd, src->e_type, dst->e_type);
273
78
  H_PUT_16 (abfd, src->e_machine, dst->e_machine);
274
78
  H_PUT_32 (abfd, src->e_version, dst->e_version);
275
78
  if (signed_vma)
276
78
    H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
277
77
  else
278
78
    H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
279
78
  H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
280
78
  if (no_section_header)
281
78
    H_PUT_WORD (abfd, 0, dst->e_shoff);
282
78
  else
283
78
    H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
284
78
  H_PUT_32 (abfd, src->e_flags, dst->e_flags);
285
78
  H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
286
78
  H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
287
78
  tmp = src->e_phnum;
288
78
  if (tmp > PN_XNUM)
289
0
    tmp = PN_XNUM;
290
78
  H_PUT_16 (abfd, tmp, dst->e_phnum);
291
78
  if (no_section_header)
292
0
    {
293
0
      H_PUT_16 (abfd, 0, dst->e_shentsize);
294
0
      H_PUT_16 (abfd, 0, dst->e_shnum);
295
0
      H_PUT_16 (abfd, 0, dst->e_shstrndx);
296
0
    }
297
78
  else
298
78
    {
299
78
      H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
300
78
      tmp = src->e_shnum;
301
78
      if (tmp >= (SHN_LORESERVE & 0xffff))
302
0
  tmp = SHN_UNDEF;
303
78
      H_PUT_16 (abfd, tmp, dst->e_shnum);
304
78
      tmp = src->e_shstrndx;
305
78
      if (tmp >= (SHN_LORESERVE & 0xffff))
306
0
  tmp = SHN_XINDEX & 0xffff;
307
78
      H_PUT_16 (abfd, tmp, dst->e_shstrndx);
308
78
    }
309
78
}
310
311
/* Translate an ELF section header table entry in external format into an
312
   ELF section header table entry in internal format.  */
313
314
static void
315
elf_swap_shdr_in (bfd *abfd,
316
      const Elf_External_Shdr *src,
317
      Elf_Internal_Shdr *dst)
318
11.3M
{
319
11.3M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
320
321
11.3M
  dst->sh_name = H_GET_32 (abfd, src->sh_name);
322
11.3M
  dst->sh_type = H_GET_32 (abfd, src->sh_type);
323
11.3M
  dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
324
11.3M
  if (signed_vma)
325
1.73M
    dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
326
9.60M
  else
327
9.60M
    dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
328
11.3M
  dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
329
11.3M
  dst->sh_size = H_GET_WORD (abfd, src->sh_size);
330
  /* PR 23657.  Check for invalid section size, in sections with contents.
331
     Note - we do not set an error value here because the contents
332
     of this particular section might not be needed by the consumer.  */
333
11.3M
  if (dst->sh_type != SHT_NOBITS)
334
11.2M
    {
335
11.2M
      ufile_ptr filesize = bfd_get_file_size (abfd);
336
337
11.2M
      if (filesize != 0
338
11.2M
    && ((ufile_ptr) dst->sh_offset > filesize
339
11.2M
        || dst->sh_size > filesize - dst->sh_offset)
340
11.2M
    && !abfd->read_only)
341
869k
  {
342
869k
    _bfd_error_handler (_("warning: %pB has a section "
343
869k
        "extending past end of file"), abfd);
344
869k
    abfd->read_only = 1;
345
869k
  }
346
11.2M
    }
347
11.3M
  dst->sh_link = H_GET_32 (abfd, src->sh_link);
348
11.3M
  dst->sh_info = H_GET_32 (abfd, src->sh_info);
349
11.3M
  dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
350
11.3M
  dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
351
11.3M
  dst->bfd_section = NULL;
352
11.3M
  dst->contents = NULL;
353
11.3M
}
elf64.c:elf_swap_shdr_in
Line
Count
Source
318
6.33M
{
319
6.33M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
320
321
6.33M
  dst->sh_name = H_GET_32 (abfd, src->sh_name);
322
6.33M
  dst->sh_type = H_GET_32 (abfd, src->sh_type);
323
6.33M
  dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
324
6.33M
  if (signed_vma)
325
360k
    dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
326
5.97M
  else
327
5.97M
    dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
328
6.33M
  dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
329
6.33M
  dst->sh_size = H_GET_WORD (abfd, src->sh_size);
330
  /* PR 23657.  Check for invalid section size, in sections with contents.
331
     Note - we do not set an error value here because the contents
332
     of this particular section might not be needed by the consumer.  */
333
6.33M
  if (dst->sh_type != SHT_NOBITS)
334
6.29M
    {
335
6.29M
      ufile_ptr filesize = bfd_get_file_size (abfd);
336
337
6.29M
      if (filesize != 0
338
6.29M
    && ((ufile_ptr) dst->sh_offset > filesize
339
6.29M
        || dst->sh_size > filesize - dst->sh_offset)
340
6.29M
    && !abfd->read_only)
341
431k
  {
342
431k
    _bfd_error_handler (_("warning: %pB has a section "
343
431k
        "extending past end of file"), abfd);
344
431k
    abfd->read_only = 1;
345
431k
  }
346
6.29M
    }
347
6.33M
  dst->sh_link = H_GET_32 (abfd, src->sh_link);
348
6.33M
  dst->sh_info = H_GET_32 (abfd, src->sh_info);
349
6.33M
  dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
350
6.33M
  dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
351
6.33M
  dst->bfd_section = NULL;
352
6.33M
  dst->contents = NULL;
353
6.33M
}
elf32.c:elf_swap_shdr_in
Line
Count
Source
318
5.00M
{
319
5.00M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
320
321
5.00M
  dst->sh_name = H_GET_32 (abfd, src->sh_name);
322
5.00M
  dst->sh_type = H_GET_32 (abfd, src->sh_type);
323
5.00M
  dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
324
5.00M
  if (signed_vma)
325
1.37M
    dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
326
3.63M
  else
327
3.63M
    dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
328
5.00M
  dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
329
5.00M
  dst->sh_size = H_GET_WORD (abfd, src->sh_size);
330
  /* PR 23657.  Check for invalid section size, in sections with contents.
331
     Note - we do not set an error value here because the contents
332
     of this particular section might not be needed by the consumer.  */
333
5.00M
  if (dst->sh_type != SHT_NOBITS)
334
4.92M
    {
335
4.92M
      ufile_ptr filesize = bfd_get_file_size (abfd);
336
337
4.92M
      if (filesize != 0
338
4.92M
    && ((ufile_ptr) dst->sh_offset > filesize
339
4.92M
        || dst->sh_size > filesize - dst->sh_offset)
340
4.92M
    && !abfd->read_only)
341
437k
  {
342
437k
    _bfd_error_handler (_("warning: %pB has a section "
343
437k
        "extending past end of file"), abfd);
344
437k
    abfd->read_only = 1;
345
437k
  }
346
4.92M
    }
347
5.00M
  dst->sh_link = H_GET_32 (abfd, src->sh_link);
348
5.00M
  dst->sh_info = H_GET_32 (abfd, src->sh_info);
349
5.00M
  dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
350
5.00M
  dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
351
5.00M
  dst->bfd_section = NULL;
352
5.00M
  dst->contents = NULL;
353
5.00M
}
354
355
/* Translate an ELF section header table entry in internal format into an
356
   ELF section header table entry in external format.  */
357
358
static void
359
elf_swap_shdr_out (bfd *abfd,
360
       const Elf_Internal_Shdr *src,
361
       Elf_External_Shdr *dst)
362
5.31k
{
363
  /* note that all elements of dst are *arrays of unsigned char* already...  */
364
5.31k
  H_PUT_32 (abfd, src->sh_name, dst->sh_name);
365
5.31k
  H_PUT_32 (abfd, src->sh_type, dst->sh_type);
366
5.31k
  H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
367
5.31k
  H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
368
5.31k
  H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
369
5.31k
  H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
370
5.31k
  H_PUT_32 (abfd, src->sh_link, dst->sh_link);
371
5.31k
  H_PUT_32 (abfd, src->sh_info, dst->sh_info);
372
5.31k
  H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
373
5.31k
  H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
374
5.31k
}
elf64.c:elf_swap_shdr_out
Line
Count
Source
362
3.97k
{
363
  /* note that all elements of dst are *arrays of unsigned char* already...  */
364
3.97k
  H_PUT_32 (abfd, src->sh_name, dst->sh_name);
365
3.97k
  H_PUT_32 (abfd, src->sh_type, dst->sh_type);
366
3.97k
  H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
367
3.97k
  H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
368
3.97k
  H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
369
3.97k
  H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
370
3.97k
  H_PUT_32 (abfd, src->sh_link, dst->sh_link);
371
3.97k
  H_PUT_32 (abfd, src->sh_info, dst->sh_info);
372
3.97k
  H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
373
3.97k
  H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
374
3.97k
}
elf32.c:elf_swap_shdr_out
Line
Count
Source
362
1.34k
{
363
  /* note that all elements of dst are *arrays of unsigned char* already...  */
364
1.34k
  H_PUT_32 (abfd, src->sh_name, dst->sh_name);
365
1.34k
  H_PUT_32 (abfd, src->sh_type, dst->sh_type);
366
1.34k
  H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
367
1.34k
  H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
368
1.34k
  H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
369
1.34k
  H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
370
1.34k
  H_PUT_32 (abfd, src->sh_link, dst->sh_link);
371
1.34k
  H_PUT_32 (abfd, src->sh_info, dst->sh_info);
372
1.34k
  H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
373
1.34k
  H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
374
1.34k
}
375
376
/* Translate an ELF program header table entry in external format into an
377
   ELF program header table entry in internal format.  */
378
379
void
380
elf_swap_phdr_in (bfd *abfd,
381
      const Elf_External_Phdr *src,
382
      Elf_Internal_Phdr *dst)
383
28.7M
{
384
28.7M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
385
386
28.7M
  dst->p_type = H_GET_32 (abfd, src->p_type);
387
28.7M
  dst->p_flags = H_GET_32 (abfd, src->p_flags);
388
28.7M
  dst->p_offset = H_GET_WORD (abfd, src->p_offset);
389
28.7M
  if (signed_vma)
390
9.15M
    {
391
9.15M
      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
392
9.15M
      dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
393
9.15M
    }
394
19.6M
  else
395
19.6M
    {
396
19.6M
      dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
397
19.6M
      dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
398
19.6M
    }
399
28.7M
  dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
400
28.7M
  dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
401
28.7M
  dst->p_align = H_GET_WORD (abfd, src->p_align);
402
28.7M
}
bfd_elf64_swap_phdr_in
Line
Count
Source
383
16.6M
{
384
16.6M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
385
386
16.6M
  dst->p_type = H_GET_32 (abfd, src->p_type);
387
16.6M
  dst->p_flags = H_GET_32 (abfd, src->p_flags);
388
16.6M
  dst->p_offset = H_GET_WORD (abfd, src->p_offset);
389
16.6M
  if (signed_vma)
390
8.80M
    {
391
8.80M
      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
392
8.80M
      dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
393
8.80M
    }
394
7.84M
  else
395
7.84M
    {
396
7.84M
      dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
397
7.84M
      dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
398
7.84M
    }
399
16.6M
  dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
400
16.6M
  dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
401
16.6M
  dst->p_align = H_GET_WORD (abfd, src->p_align);
402
16.6M
}
bfd_elf32_swap_phdr_in
Line
Count
Source
383
12.1M
{
384
12.1M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
385
386
12.1M
  dst->p_type = H_GET_32 (abfd, src->p_type);
387
12.1M
  dst->p_flags = H_GET_32 (abfd, src->p_flags);
388
12.1M
  dst->p_offset = H_GET_WORD (abfd, src->p_offset);
389
12.1M
  if (signed_vma)
390
355k
    {
391
355k
      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
392
355k
      dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
393
355k
    }
394
11.7M
  else
395
11.7M
    {
396
11.7M
      dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
397
11.7M
      dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
398
11.7M
    }
399
12.1M
  dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
400
12.1M
  dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
401
12.1M
  dst->p_align = H_GET_WORD (abfd, src->p_align);
402
12.1M
}
403
404
void
405
elf_swap_phdr_out (bfd *abfd,
406
       const Elf_Internal_Phdr *src,
407
       Elf_External_Phdr *dst)
408
240
{
409
240
  const struct elf_backend_data *bed;
410
240
  bfd_vma p_paddr;
411
412
240
  bed = get_elf_backend_data (abfd);
413
240
  p_paddr = bed->want_p_paddr_set_to_zero ? 0 : src->p_paddr;
414
415
  /* note that all elements of dst are *arrays of unsigned char* already...  */
416
240
  H_PUT_32 (abfd, src->p_type, dst->p_type);
417
240
  H_PUT_WORD (abfd, src->p_offset, dst->p_offset);
418
240
  H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr);
419
240
  H_PUT_WORD (abfd, p_paddr, dst->p_paddr);
420
240
  H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz);
421
240
  H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz);
422
240
  H_PUT_32 (abfd, src->p_flags, dst->p_flags);
423
240
  H_PUT_WORD (abfd, src->p_align, dst->p_align);
424
240
}
bfd_elf64_swap_phdr_out
Line
Count
Source
408
39
{
409
39
  const struct elf_backend_data *bed;
410
39
  bfd_vma p_paddr;
411
412
39
  bed = get_elf_backend_data (abfd);
413
39
  p_paddr = bed->want_p_paddr_set_to_zero ? 0 : src->p_paddr;
414
415
  /* note that all elements of dst are *arrays of unsigned char* already...  */
416
39
  H_PUT_32 (abfd, src->p_type, dst->p_type);
417
39
  H_PUT_WORD (abfd, src->p_offset, dst->p_offset);
418
39
  H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr);
419
39
  H_PUT_WORD (abfd, p_paddr, dst->p_paddr);
420
39
  H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz);
421
39
  H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz);
422
39
  H_PUT_32 (abfd, src->p_flags, dst->p_flags);
423
39
  H_PUT_WORD (abfd, src->p_align, dst->p_align);
424
39
}
bfd_elf32_swap_phdr_out
Line
Count
Source
408
201
{
409
201
  const struct elf_backend_data *bed;
410
201
  bfd_vma p_paddr;
411
412
201
  bed = get_elf_backend_data (abfd);
413
201
  p_paddr = bed->want_p_paddr_set_to_zero ? 0 : src->p_paddr;
414
415
  /* note that all elements of dst are *arrays of unsigned char* already...  */
416
201
  H_PUT_32 (abfd, src->p_type, dst->p_type);
417
201
  H_PUT_WORD (abfd, src->p_offset, dst->p_offset);
418
201
  H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr);
419
201
  H_PUT_WORD (abfd, p_paddr, dst->p_paddr);
420
201
  H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz);
421
201
  H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz);
422
201
  H_PUT_32 (abfd, src->p_flags, dst->p_flags);
423
201
  H_PUT_WORD (abfd, src->p_align, dst->p_align);
424
201
}
425
426
/* Translate an ELF reloc from external format to internal format.  */
427
void
428
elf_swap_reloc_in (bfd *abfd,
429
       const bfd_byte *s,
430
       Elf_Internal_Rela *dst)
431
67.2k
{
432
67.2k
  const Elf_External_Rel *src = (const Elf_External_Rel *) s;
433
67.2k
  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
434
67.2k
  dst->r_info = H_GET_WORD (abfd, src->r_info);
435
67.2k
  dst->r_addend = 0;
436
67.2k
}
Unexecuted instantiation: bfd_elf64_swap_reloc_in
bfd_elf32_swap_reloc_in
Line
Count
Source
431
67.2k
{
432
67.2k
  const Elf_External_Rel *src = (const Elf_External_Rel *) s;
433
67.2k
  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
434
67.2k
  dst->r_info = H_GET_WORD (abfd, src->r_info);
435
67.2k
  dst->r_addend = 0;
436
67.2k
}
437
438
void
439
elf_swap_reloca_in (bfd *abfd,
440
        const bfd_byte *s,
441
        Elf_Internal_Rela *dst)
442
2.26M
{
443
2.26M
  const Elf_External_Rela *src = (const Elf_External_Rela *) s;
444
2.26M
  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
445
2.26M
  dst->r_info = H_GET_WORD (abfd, src->r_info);
446
2.26M
  dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
447
2.26M
}
bfd_elf64_swap_reloca_in
Line
Count
Source
442
2.25M
{
443
2.25M
  const Elf_External_Rela *src = (const Elf_External_Rela *) s;
444
2.25M
  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
445
2.25M
  dst->r_info = H_GET_WORD (abfd, src->r_info);
446
2.25M
  dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
447
2.25M
}
bfd_elf32_swap_reloca_in
Line
Count
Source
442
12.7k
{
443
12.7k
  const Elf_External_Rela *src = (const Elf_External_Rela *) s;
444
12.7k
  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
445
12.7k
  dst->r_info = H_GET_WORD (abfd, src->r_info);
446
12.7k
  dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
447
12.7k
}
448
449
/* Translate an ELF reloc from internal format to external format.  */
450
void
451
elf_swap_reloc_out (bfd *abfd,
452
        const Elf_Internal_Rela *src,
453
        bfd_byte *d)
454
0
{
455
0
  Elf_External_Rel *dst = (Elf_External_Rel *) d;
456
0
  H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
457
0
  H_PUT_WORD (abfd, src->r_info, dst->r_info);
458
0
}
Unexecuted instantiation: bfd_elf64_swap_reloc_out
Unexecuted instantiation: bfd_elf32_swap_reloc_out
459
460
void
461
elf_swap_reloca_out (bfd *abfd,
462
         const Elf_Internal_Rela *src,
463
         bfd_byte *d)
464
70.8k
{
465
70.8k
  Elf_External_Rela *dst = (Elf_External_Rela *) d;
466
70.8k
  H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
467
70.8k
  H_PUT_WORD (abfd, src->r_info, dst->r_info);
468
70.8k
  H_PUT_SIGNED_WORD (abfd, src->r_addend, dst->r_addend);
469
70.8k
}
bfd_elf64_swap_reloca_out
Line
Count
Source
464
70.8k
{
465
70.8k
  Elf_External_Rela *dst = (Elf_External_Rela *) d;
466
70.8k
  H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
467
70.8k
  H_PUT_WORD (abfd, src->r_info, dst->r_info);
468
70.8k
  H_PUT_SIGNED_WORD (abfd, src->r_addend, dst->r_addend);
469
70.8k
}
Unexecuted instantiation: bfd_elf32_swap_reloca_out
470
471
void
472
elf_swap_dyn_in (bfd *abfd,
473
     const void *p,
474
     Elf_Internal_Dyn *dst)
475
283k
{
476
283k
  const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
477
478
283k
  dst->d_tag = H_GET_WORD (abfd, src->d_tag);
479
283k
  dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
480
283k
}
bfd_elf64_swap_dyn_in
Line
Count
Source
475
271k
{
476
271k
  const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
477
478
271k
  dst->d_tag = H_GET_WORD (abfd, src->d_tag);
479
271k
  dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
480
271k
}
bfd_elf32_swap_dyn_in
Line
Count
Source
475
12.1k
{
476
12.1k
  const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
477
478
12.1k
  dst->d_tag = H_GET_WORD (abfd, src->d_tag);
479
12.1k
  dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
480
12.1k
}
481
482
void
483
elf_swap_dyn_out (bfd *abfd,
484
      const Elf_Internal_Dyn *src,
485
      void *p)
486
0
{
487
0
  Elf_External_Dyn *dst = (Elf_External_Dyn *) p;
488
489
0
  H_PUT_WORD (abfd, src->d_tag, dst->d_tag);
490
0
  H_PUT_WORD (abfd, src->d_un.d_val, dst->d_un.d_val);
491
0
}
Unexecuted instantiation: bfd_elf64_swap_dyn_out
Unexecuted instantiation: bfd_elf32_swap_dyn_out
492

493
/* ELF .o/exec file reading */
494
495
/* Begin processing a given object.
496
497
   First we validate the file by reading in the ELF header and checking
498
   the magic number.  */
499
500
static inline bool
501
elf_file_p (Elf_External_Ehdr *x_ehdrp)
502
434M
{
503
434M
  return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
504
434M
    && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
505
434M
    && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
506
434M
    && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
507
434M
}
elf64.c:elf_file_p
Line
Count
Source
502
103M
{
503
103M
  return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
504
103M
    && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
505
103M
    && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
506
103M
    && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
507
103M
}
elf32.c:elf_file_p
Line
Count
Source
502
331M
{
503
331M
  return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
504
331M
    && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
505
331M
    && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
506
331M
    && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
507
331M
}
508
509
/* Check to see if the file associated with ABFD matches the target vector
510
   that ABFD points to.
511
512
   Note that we may be called several times with the same ABFD, but different
513
   target vectors, most of which will not match.  We have to avoid leaving
514
   any side effects in ABFD, or any data it points to (like tdata), if the
515
   file does not match the target vector.  */
516
517
bfd_cleanup
518
elf_object_p (bfd *abfd)
519
489M
{
520
489M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
521
489M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
522
489M
  Elf_External_Shdr x_shdr;  /* Section header table entry, external form */
523
489M
  Elf_Internal_Shdr i_shdr;
524
489M
  Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
525
489M
  unsigned int shindex;
526
489M
  const struct elf_backend_data *ebd;
527
489M
  asection *s;
528
489M
  const bfd_target *target;
529
530
  /* Read in the ELF header in external format.  */
531
532
489M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
533
56.7M
    {
534
56.7M
      if (bfd_get_error () != bfd_error_system_call)
535
56.6M
  goto got_wrong_format_error;
536
144k
      else
537
144k
  goto got_no_match;
538
56.7M
    }
539
540
  /* Now check to see if we have a valid ELF file, and one that BFD can
541
     make use of.  The magic number must match, the address size ('class')
542
     and byte-swapping must match our XVEC entry, and it must have a
543
     section header table (FIXME: See comments re sections at top of this
544
     file).  */
545
546
433M
  if (! elf_file_p (&x_ehdr)
547
433M
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
548
433M
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
549
388M
    goto got_wrong_format_error;
550
551
  /* Check that file's byte order matches xvec's */
552
44.1M
  switch (x_ehdr.e_ident[EI_DATA])
553
44.1M
    {
554
5.32M
    case ELFDATA2MSB:   /* Big-endian */
555
5.32M
      if (! bfd_header_big_endian (abfd))
556
2.68M
  goto got_wrong_format_error;
557
2.63M
      break;
558
38.8M
    case ELFDATA2LSB:   /* Little-endian */
559
38.8M
      if (! bfd_header_little_endian (abfd))
560
18.8M
  goto got_wrong_format_error;
561
19.9M
      break;
562
19.9M
    case ELFDATANONE:   /* No data encoding specified */
563
16.1k
    default:      /* Unknown data encoding specified */
564
16.1k
      goto got_wrong_format_error;
565
44.1M
    }
566
567
22.6M
  target = abfd->xvec;
568
569
  /* Allocate an instance of the elf_obj_tdata structure and hook it up to
570
     the tdata pointer in the bfd.  */
571
572
22.6M
  if (! (*target->_bfd_set_format[bfd_object]) (abfd))
573
0
    goto got_no_match;
574
575
  /* Now that we know the byte order, swap in the rest of the header */
576
22.6M
  i_ehdrp = elf_elfheader (abfd);
577
22.6M
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
578
#if DEBUG & 1
579
  elf_debug_file (i_ehdrp);
580
#endif
581
582
  /* Reject ET_CORE (header indicates core file, not object file) */
583
22.6M
  if (i_ehdrp->e_type == ET_CORE)
584
172k
    goto got_wrong_format_error;
585
586
  /* If this is a relocatable file and there is no section header
587
     table, then we're hosed.  */
588
22.4M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_type == ET_REL)
589
34.2k
    goto got_wrong_format_error;
590
591
  /* As a simple sanity check, verify that what BFD thinks is the
592
     size of each section header table entry actually matches the size
593
     recorded in the file, but only if there are any sections.  */
594
22.4M
  if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
595
60.0k
    goto got_wrong_format_error;
596
597
  /* Further sanity check.  */
598
22.3M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_shnum != 0)
599
24.7k
    goto got_wrong_format_error;
600
601
22.3M
  ebd = get_elf_backend_data (abfd);
602
22.3M
  if (ebd->s->arch_size != ARCH_SIZE)
603
0
    goto got_wrong_format_error;
604
605
  /* Check that the ELF e_machine field matches what this particular
606
     BFD format expects.  */
607
22.3M
  if (ebd->elf_machine_code != i_ehdrp->e_machine
608
22.3M
      && (ebd->elf_machine_alt1 == 0
609
21.2M
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
610
22.3M
      && (ebd->elf_machine_alt2 == 0
611
21.1M
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
612
22.3M
      && ebd->elf_machine_code != EM_NONE)
613
20.6M
    goto got_wrong_format_error;
614
615
1.66M
  if (i_ehdrp->e_type == ET_EXEC)
616
70.5k
    abfd->flags |= EXEC_P;
617
1.59M
  else if (i_ehdrp->e_type == ET_DYN)
618
60.9k
    abfd->flags |= DYNAMIC;
619
620
1.66M
  if (i_ehdrp->e_phnum > 0)
621
621k
    abfd->flags |= D_PAGED;
622
623
1.66M
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
624
493k
    {
625
      /* It's OK if this fails for the generic target.  */
626
493k
      if (ebd->elf_machine_code != EM_NONE)
627
673
  goto got_no_match;
628
493k
    }
629
630
1.66M
  if (ebd->elf_machine_code != EM_NONE
631
1.66M
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
632
1.66M
      && ebd->elf_osabi != ELFOSABI_NONE)
633
165k
    goto got_wrong_format_error;
634
635
1.49M
  if (i_ehdrp->e_shoff >= sizeof (x_ehdr))
636
938k
    {
637
938k
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
638
639
      /* Seek to the section header table in the file.  */
640
938k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
641
2.89k
  goto got_no_match;
642
643
      /* Read the first section header at index 0, and convert to internal
644
   form.  */
645
935k
      if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
646
6.07k
  goto got_no_match;
647
929k
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
648
649
      /* If the section count is zero, the actual count is in the first
650
   section header.  */
651
929k
      if (i_ehdrp->e_shnum == SHN_UNDEF)
652
42.3k
  {
653
42.3k
    i_ehdrp->e_shnum = i_shdr.sh_size;
654
42.3k
    if (i_ehdrp->e_shnum >= SHN_LORESERVE
655
42.3k
        || i_ehdrp->e_shnum != i_shdr.sh_size
656
42.3k
        || i_ehdrp->e_shnum  == 0)
657
10.1k
      goto got_wrong_format_error;
658
42.3k
  }
659
660
      /* And similarly for the string table index.  */
661
919k
      if (i_ehdrp->e_shstrndx == (SHN_XINDEX & 0xffff))
662
59.3k
  {
663
59.3k
    i_ehdrp->e_shstrndx = i_shdr.sh_link;
664
59.3k
    if (i_ehdrp->e_shstrndx != i_shdr.sh_link)
665
0
      goto got_wrong_format_error;
666
59.3k
  }
667
668
      /* And program headers.  */
669
919k
      if (i_ehdrp->e_phnum == PN_XNUM && i_shdr.sh_info != 0)
670
24.5k
  {
671
24.5k
    i_ehdrp->e_phnum = i_shdr.sh_info;
672
24.5k
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
673
0
      goto got_wrong_format_error;
674
24.5k
  }
675
676
      /* Sanity check that we can read all of the section headers.
677
   It ought to be good enough to just read the last one.  */
678
919k
      if (i_ehdrp->e_shnum != 1)
679
792k
  {
680
    /* Check that we don't have a totally silly number of sections.  */
681
792k
    if (i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (x_shdr)
682
792k
        || i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (i_shdr))
683
7.43k
      goto got_wrong_format_error;
684
685
784k
    where += (i_ehdrp->e_shnum - 1) * sizeof (x_shdr);
686
784k
    if ((bfd_size_type) where <= i_ehdrp->e_shoff)
687
0
      goto got_wrong_format_error;
688
689
784k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
690
0
      goto got_no_match;
691
784k
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
692
8.04k
      goto got_no_match;
693
694
    /* Back to where we were.  */
695
776k
    where = i_ehdrp->e_shoff + sizeof (x_shdr);
696
776k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
697
0
      goto got_no_match;
698
776k
  }
699
919k
    }
700
701
  /* Allocate space for a copy of the section header table in
702
     internal form.  */
703
1.46M
  if (i_ehdrp->e_shnum != 0)
704
903k
    {
705
903k
      Elf_Internal_Shdr *shdrp;
706
903k
      unsigned int num_sec;
707
903k
      size_t amt;
708
709
903k
      if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*i_shdrp), &amt))
710
0
  goto got_wrong_format_error;
711
903k
      i_shdrp = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
712
903k
      if (!i_shdrp)
713
0
  goto got_no_match;
714
903k
      num_sec = i_ehdrp->e_shnum;
715
903k
      elf_numsections (abfd) = num_sec;
716
903k
      if (_bfd_mul_overflow (num_sec, sizeof (i_shdrp), &amt))
717
0
  goto got_wrong_format_error;
718
903k
      elf_elfsections (abfd) = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
719
903k
      if (!elf_elfsections (abfd))
720
0
  goto got_no_match;
721
903k
      elf_tdata (abfd)->being_created = bfd_zalloc (abfd, num_sec);
722
903k
      if (!elf_tdata (abfd)->being_created)
723
0
  goto got_no_match;
724
725
903k
      memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
726
12.3M
      for (shdrp = i_shdrp, shindex = 0; shindex < num_sec; shindex++)
727
11.4M
  elf_elfsections (abfd)[shindex] = shdrp++;
728
729
      /* Read in the rest of the section header table and convert it
730
   to internal form.  */
731
11.2M
      for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
732
10.4M
  {
733
10.4M
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
734
0
      goto got_no_match;
735
10.4M
    elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
736
737
    /* Sanity check sh_link and sh_info.  */
738
10.4M
    if (i_shdrp[shindex].sh_link >= num_sec)
739
31.2k
      {
740
        /* PR 10478: Accept Solaris binaries with a sh_link
741
     field set to SHN_BEFORE or SHN_AFTER.  */
742
31.2k
        switch (ebd->elf_machine_code)
743
31.2k
    {
744
7.91k
    case EM_386:
745
8.92k
    case EM_IAMCU:
746
18.4k
    case EM_X86_64:
747
18.4k
    case EM_OLD_SPARCV9:
748
18.4k
    case EM_SPARC32PLUS:
749
19.5k
    case EM_SPARCV9:
750
19.6k
    case EM_SPARC:
751
19.6k
      if (i_shdrp[shindex].sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
752
19.6k
          || i_shdrp[shindex].sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
753
11.0k
        break;
754
      /* Otherwise fall through.  */
755
20.2k
    default:
756
20.2k
      goto got_wrong_format_error;
757
31.2k
    }
758
31.2k
      }
759
760
10.3M
    if (((i_shdrp[shindex].sh_flags & SHF_INFO_LINK)
761
10.3M
         || i_shdrp[shindex].sh_type == SHT_RELA
762
10.3M
         || i_shdrp[shindex].sh_type == SHT_REL)
763
10.3M
        && i_shdrp[shindex].sh_info >= num_sec)
764
4.40k
      goto got_wrong_format_error;
765
766
    /* If the section is loaded, but not page aligned, clear
767
       D_PAGED.  */
768
10.3M
    if (i_shdrp[shindex].sh_size != 0
769
10.3M
        && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
770
10.3M
        && i_shdrp[shindex].sh_type != SHT_NOBITS
771
10.3M
        && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
772
2.14M
       % ebd->minpagesize)
773
2.14M
      != 0))
774
1.38M
      abfd->flags &= ~D_PAGED;
775
10.3M
  }
776
777
879k
      if (i_ehdrp->e_shstrndx >= elf_numsections (abfd)
778
879k
    || i_shdrp[i_ehdrp->e_shstrndx].sh_type != SHT_STRTAB)
779
205k
  {
780
    /* PR 2257:
781
       We used to just goto got_wrong_format_error here
782
       but there are binaries in existance for which this test
783
       will prevent the binutils from working with them at all.
784
       So we are kind, and reset the string index value to 0
785
       so that at least some processing can be done.  */
786
205k
    i_ehdrp->e_shstrndx = SHN_UNDEF;
787
205k
    if (!abfd->read_only)
788
24.8k
      {
789
24.8k
        _bfd_error_handler
790
24.8k
    (_("warning: %pB has a corrupt string table index"), abfd);
791
24.8k
        abfd->read_only = 1;
792
24.8k
      }
793
205k
  }
794
879k
    }
795
556k
  else if (i_ehdrp->e_shstrndx != SHN_UNDEF)
796
5.36k
    goto got_wrong_format_error;
797
798
  /* Read in the program headers.  */
799
1.43M
  if (i_ehdrp->e_phnum == 0)
800
888k
    elf_tdata (abfd)->phdr = NULL;
801
541k
  else
802
541k
    {
803
541k
      Elf_Internal_Phdr *i_phdr;
804
541k
      unsigned int i;
805
541k
      ufile_ptr filesize;
806
541k
      size_t amt;
807
808
      /* Check for a corrupt input file with an impossibly large number
809
   of program headers.  */
810
541k
      filesize = bfd_get_file_size (abfd);
811
541k
      if (filesize != 0
812
541k
    && i_ehdrp->e_phnum > filesize / sizeof (Elf_External_Phdr))
813
15.9k
  goto got_wrong_format_error;
814
526k
      if (_bfd_mul_overflow (i_ehdrp->e_phnum, sizeof (*i_phdr), &amt))
815
0
  goto got_wrong_format_error;
816
526k
      elf_tdata (abfd)->phdr
817
526k
  = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
818
526k
      if (elf_tdata (abfd)->phdr == NULL)
819
0
  goto got_no_match;
820
526k
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
821
1.33k
  goto got_no_match;
822
524k
      bool eu_strip_broken_phdrs = false;
823
524k
      i_phdr = elf_tdata (abfd)->phdr;
824
27.6M
      for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
825
27.1M
  {
826
27.1M
    Elf_External_Phdr x_phdr;
827
828
27.1M
    if (bfd_read (&x_phdr, sizeof x_phdr, abfd) != sizeof x_phdr)
829
6.88k
      goto got_no_match;
830
27.1M
    elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
831
    /* Too much code in BFD relies on alignment being a power of
832
       two, as required by the ELF spec.  */
833
27.1M
    if (i_phdr->p_align != (i_phdr->p_align & -i_phdr->p_align))
834
21.0M
      {
835
21.0M
        i_phdr->p_align &= -i_phdr->p_align;
836
21.0M
        if (!abfd->read_only)
837
192k
    {
838
192k
      _bfd_error_handler (_("warning: %pB has a program header "
839
192k
          "with invalid alignment"), abfd);
840
192k
      abfd->read_only = 1;
841
192k
    }
842
21.0M
      }
843
    /* Detect eu-strip -f debug files, which have program
844
       headers that describe the original file.  */
845
27.1M
    if (i_phdr->p_filesz != 0
846
27.1M
        && (i_phdr->p_filesz > filesize
847
22.1M
      || i_phdr->p_offset > filesize - i_phdr->p_filesz))
848
21.3M
      eu_strip_broken_phdrs = true;
849
27.1M
  }
850
517k
      if (!eu_strip_broken_phdrs
851
517k
    && i_ehdrp->e_shoff == 0
852
517k
    && i_ehdrp->e_shstrndx == 0)
853
26.6k
  {
854
    /* Try to reconstruct dynamic symbol table from PT_DYNAMIC
855
       segment if there is no section header.  */
856
26.6k
    i_phdr = elf_tdata (abfd)->phdr;
857
61.2k
    for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
858
48.7k
      if (i_phdr->p_type == PT_DYNAMIC)
859
14.1k
        {
860
14.1k
    if (i_phdr->p_filesz != 0
861
14.1k
        && !_bfd_elf_get_dynamic_symbols (abfd, i_phdr,
862
12.1k
                  elf_tdata (abfd)->phdr,
863
12.1k
                  i_ehdrp->e_phnum,
864
12.1k
                  filesize))
865
12.1k
      goto got_no_match;
866
2.02k
    break;
867
14.1k
        }
868
26.6k
  }
869
517k
    }
870
871
1.39M
  if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff >= sizeof (x_ehdr))
872
670k
    {
873
670k
      unsigned int num_sec;
874
875
      /* Once all of the section headers have been read and converted, we
876
   can start processing them.  Note that the first section header is
877
   a dummy placeholder entry, so we ignore it.  */
878
670k
      num_sec = elf_numsections (abfd);
879
9.14M
      for (shindex = 1; shindex < num_sec; shindex++)
880
8.64M
  if (!bfd_section_from_shdr (abfd, shindex))
881
169k
    goto got_no_match;
882
883
      /* Set up ELF sections for SHF_GROUP and SHF_LINK_ORDER.  */
884
501k
      if (! _bfd_elf_setup_sections (abfd))
885
14.7k
  goto got_wrong_format_error;
886
501k
    }
887
888
  /* Let the backend double check the format and override global
889
     information.  */
890
1.20M
  if (ebd->elf_backend_object_p)
891
768k
    {
892
768k
      if (! (*ebd->elf_backend_object_p) (abfd))
893
95.6k
  goto got_wrong_format_error;
894
768k
    }
895
896
  /* Remember the entry point specified in the ELF file header.  */
897
1.11M
  bfd_set_start_address (abfd, i_ehdrp->e_entry);
898
899
  /* If we have created any reloc sections that are associated with
900
     debugging sections, mark the reloc sections as debugging as well.  */
901
4.68M
  for (s = abfd->sections; s != NULL; s = s->next)
902
3.57M
    {
903
3.57M
      if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL
904
3.57M
     || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
905
3.57M
    && elf_section_data (s)->this_hdr.sh_info > 0)
906
219k
  {
907
219k
    unsigned long targ_index;
908
219k
    asection *targ_sec;
909
910
219k
    targ_index = elf_section_data (s)->this_hdr.sh_info;
911
219k
    targ_sec = bfd_section_from_elf_index (abfd, targ_index);
912
219k
    if (targ_sec != NULL
913
219k
        && (targ_sec->flags & SEC_DEBUGGING) != 0)
914
11.3k
      s->flags |= SEC_DEBUGGING;
915
219k
  }
916
3.57M
    }
917
1.11M
  return _bfd_no_cleanup;
918
919
488M
 got_wrong_format_error:
920
488M
  bfd_set_error (bfd_error_wrong_format);
921
922
488M
 got_no_match:
923
488M
  return NULL;
924
488M
}
bfd_elf64_object_p
Line
Count
Source
519
122M
{
520
122M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
521
122M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
522
122M
  Elf_External_Shdr x_shdr;  /* Section header table entry, external form */
523
122M
  Elf_Internal_Shdr i_shdr;
524
122M
  Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
525
122M
  unsigned int shindex;
526
122M
  const struct elf_backend_data *ebd;
527
122M
  asection *s;
528
122M
  const bfd_target *target;
529
530
  /* Read in the ELF header in external format.  */
531
532
122M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
533
19.7M
    {
534
19.7M
      if (bfd_get_error () != bfd_error_system_call)
535
19.6M
  goto got_wrong_format_error;
536
35.6k
      else
537
35.6k
  goto got_no_match;
538
19.7M
    }
539
540
  /* Now check to see if we have a valid ELF file, and one that BFD can
541
     make use of.  The magic number must match, the address size ('class')
542
     and byte-swapping must match our XVEC entry, and it must have a
543
     section header table (FIXME: See comments re sections at top of this
544
     file).  */
545
546
102M
  if (! elf_file_p (&x_ehdr)
547
102M
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
548
102M
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
549
91.9M
    goto got_wrong_format_error;
550
551
  /* Check that file's byte order matches xvec's */
552
10.8M
  switch (x_ehdr.e_ident[EI_DATA])
553
10.8M
    {
554
856k
    case ELFDATA2MSB:   /* Big-endian */
555
856k
      if (! bfd_header_big_endian (abfd))
556
461k
  goto got_wrong_format_error;
557
394k
      break;
558
9.97M
    case ELFDATA2LSB:   /* Little-endian */
559
9.97M
      if (! bfd_header_little_endian (abfd))
560
4.49M
  goto got_wrong_format_error;
561
5.47M
      break;
562
5.47M
    case ELFDATANONE:   /* No data encoding specified */
563
524
    default:      /* Unknown data encoding specified */
564
524
      goto got_wrong_format_error;
565
10.8M
    }
566
567
5.87M
  target = abfd->xvec;
568
569
  /* Allocate an instance of the elf_obj_tdata structure and hook it up to
570
     the tdata pointer in the bfd.  */
571
572
5.87M
  if (! (*target->_bfd_set_format[bfd_object]) (abfd))
573
0
    goto got_no_match;
574
575
  /* Now that we know the byte order, swap in the rest of the header */
576
5.87M
  i_ehdrp = elf_elfheader (abfd);
577
5.87M
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
578
#if DEBUG & 1
579
  elf_debug_file (i_ehdrp);
580
#endif
581
582
  /* Reject ET_CORE (header indicates core file, not object file) */
583
5.87M
  if (i_ehdrp->e_type == ET_CORE)
584
16.5k
    goto got_wrong_format_error;
585
586
  /* If this is a relocatable file and there is no section header
587
     table, then we're hosed.  */
588
5.85M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_type == ET_REL)
589
9.87k
    goto got_wrong_format_error;
590
591
  /* As a simple sanity check, verify that what BFD thinks is the
592
     size of each section header table entry actually matches the size
593
     recorded in the file, but only if there are any sections.  */
594
5.84M
  if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
595
10.5k
    goto got_wrong_format_error;
596
597
  /* Further sanity check.  */
598
5.83M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_shnum != 0)
599
10.9k
    goto got_wrong_format_error;
600
601
5.82M
  ebd = get_elf_backend_data (abfd);
602
5.82M
  if (ebd->s->arch_size != ARCH_SIZE)
603
0
    goto got_wrong_format_error;
604
605
  /* Check that the ELF e_machine field matches what this particular
606
     BFD format expects.  */
607
5.82M
  if (ebd->elf_machine_code != i_ehdrp->e_machine
608
5.82M
      && (ebd->elf_machine_alt1 == 0
609
5.28M
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
610
5.82M
      && (ebd->elf_machine_alt2 == 0
611
5.25M
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
612
5.82M
      && ebd->elf_machine_code != EM_NONE)
613
5.01M
    goto got_wrong_format_error;
614
615
806k
  if (i_ehdrp->e_type == ET_EXEC)
616
50.9k
    abfd->flags |= EXEC_P;
617
755k
  else if (i_ehdrp->e_type == ET_DYN)
618
45.7k
    abfd->flags |= DYNAMIC;
619
620
806k
  if (i_ehdrp->e_phnum > 0)
621
299k
    abfd->flags |= D_PAGED;
622
623
806k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
624
243k
    {
625
      /* It's OK if this fails for the generic target.  */
626
243k
      if (ebd->elf_machine_code != EM_NONE)
627
0
  goto got_no_match;
628
243k
    }
629
630
806k
  if (ebd->elf_machine_code != EM_NONE
631
806k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
632
806k
      && ebd->elf_osabi != ELFOSABI_NONE)
633
114k
    goto got_wrong_format_error;
634
635
691k
  if (i_ehdrp->e_shoff >= sizeof (x_ehdr))
636
470k
    {
637
470k
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
638
639
      /* Seek to the section header table in the file.  */
640
470k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
641
2.89k
  goto got_no_match;
642
643
      /* Read the first section header at index 0, and convert to internal
644
   form.  */
645
468k
      if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
646
3.08k
  goto got_no_match;
647
465k
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
648
649
      /* If the section count is zero, the actual count is in the first
650
   section header.  */
651
465k
      if (i_ehdrp->e_shnum == SHN_UNDEF)
652
14.4k
  {
653
14.4k
    i_ehdrp->e_shnum = i_shdr.sh_size;
654
14.4k
    if (i_ehdrp->e_shnum >= SHN_LORESERVE
655
14.4k
        || i_ehdrp->e_shnum != i_shdr.sh_size
656
14.4k
        || i_ehdrp->e_shnum  == 0)
657
7.19k
      goto got_wrong_format_error;
658
14.4k
  }
659
660
      /* And similarly for the string table index.  */
661
457k
      if (i_ehdrp->e_shstrndx == (SHN_XINDEX & 0xffff))
662
14.3k
  {
663
14.3k
    i_ehdrp->e_shstrndx = i_shdr.sh_link;
664
14.3k
    if (i_ehdrp->e_shstrndx != i_shdr.sh_link)
665
0
      goto got_wrong_format_error;
666
14.3k
  }
667
668
      /* And program headers.  */
669
457k
      if (i_ehdrp->e_phnum == PN_XNUM && i_shdr.sh_info != 0)
670
6.91k
  {
671
6.91k
    i_ehdrp->e_phnum = i_shdr.sh_info;
672
6.91k
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
673
0
      goto got_wrong_format_error;
674
6.91k
  }
675
676
      /* Sanity check that we can read all of the section headers.
677
   It ought to be good enough to just read the last one.  */
678
457k
      if (i_ehdrp->e_shnum != 1)
679
429k
  {
680
    /* Check that we don't have a totally silly number of sections.  */
681
429k
    if (i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (x_shdr)
682
429k
        || i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (i_shdr))
683
3.58k
      goto got_wrong_format_error;
684
685
425k
    where += (i_ehdrp->e_shnum - 1) * sizeof (x_shdr);
686
425k
    if ((bfd_size_type) where <= i_ehdrp->e_shoff)
687
0
      goto got_wrong_format_error;
688
689
425k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
690
0
      goto got_no_match;
691
425k
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
692
5.70k
      goto got_no_match;
693
694
    /* Back to where we were.  */
695
419k
    where = i_ehdrp->e_shoff + sizeof (x_shdr);
696
419k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
697
0
      goto got_no_match;
698
419k
  }
699
457k
    }
700
701
  /* Allocate space for a copy of the section header table in
702
     internal form.  */
703
669k
  if (i_ehdrp->e_shnum != 0)
704
448k
    {
705
448k
      Elf_Internal_Shdr *shdrp;
706
448k
      unsigned int num_sec;
707
448k
      size_t amt;
708
709
448k
      if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*i_shdrp), &amt))
710
0
  goto got_wrong_format_error;
711
448k
      i_shdrp = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
712
448k
      if (!i_shdrp)
713
0
  goto got_no_match;
714
448k
      num_sec = i_ehdrp->e_shnum;
715
448k
      elf_numsections (abfd) = num_sec;
716
448k
      if (_bfd_mul_overflow (num_sec, sizeof (i_shdrp), &amt))
717
0
  goto got_wrong_format_error;
718
448k
      elf_elfsections (abfd) = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
719
448k
      if (!elf_elfsections (abfd))
720
0
  goto got_no_match;
721
448k
      elf_tdata (abfd)->being_created = bfd_zalloc (abfd, num_sec);
722
448k
      if (!elf_tdata (abfd)->being_created)
723
0
  goto got_no_match;
724
725
448k
      memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
726
6.81M
      for (shdrp = i_shdrp, shindex = 0; shindex < num_sec; shindex++)
727
6.36M
  elf_elfsections (abfd)[shindex] = shdrp++;
728
729
      /* Read in the rest of the section header table and convert it
730
   to internal form.  */
731
6.30M
      for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
732
5.87M
  {
733
5.87M
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
734
0
      goto got_no_match;
735
5.87M
    elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
736
737
    /* Sanity check sh_link and sh_info.  */
738
5.87M
    if (i_shdrp[shindex].sh_link >= num_sec)
739
14.0k
      {
740
        /* PR 10478: Accept Solaris binaries with a sh_link
741
     field set to SHN_BEFORE or SHN_AFTER.  */
742
14.0k
        switch (ebd->elf_machine_code)
743
14.0k
    {
744
0
    case EM_386:
745
0
    case EM_IAMCU:
746
8.26k
    case EM_X86_64:
747
8.26k
    case EM_OLD_SPARCV9:
748
8.26k
    case EM_SPARC32PLUS:
749
9.35k
    case EM_SPARCV9:
750
9.35k
    case EM_SPARC:
751
9.35k
      if (i_shdrp[shindex].sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
752
9.35k
          || i_shdrp[shindex].sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
753
3.93k
        break;
754
      /* Otherwise fall through.  */
755
10.1k
    default:
756
10.1k
      goto got_wrong_format_error;
757
14.0k
    }
758
14.0k
      }
759
760
5.86M
    if (((i_shdrp[shindex].sh_flags & SHF_INFO_LINK)
761
5.86M
         || i_shdrp[shindex].sh_type == SHT_RELA
762
5.86M
         || i_shdrp[shindex].sh_type == SHT_REL)
763
5.86M
        && i_shdrp[shindex].sh_info >= num_sec)
764
3.00k
      goto got_wrong_format_error;
765
766
    /* If the section is loaded, but not page aligned, clear
767
       D_PAGED.  */
768
5.85M
    if (i_shdrp[shindex].sh_size != 0
769
5.85M
        && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
770
5.85M
        && i_shdrp[shindex].sh_type != SHT_NOBITS
771
5.85M
        && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
772
1.54M
       % ebd->minpagesize)
773
1.54M
      != 0))
774
1.05M
      abfd->flags &= ~D_PAGED;
775
5.85M
  }
776
777
435k
      if (i_ehdrp->e_shstrndx >= elf_numsections (abfd)
778
435k
    || i_shdrp[i_ehdrp->e_shstrndx].sh_type != SHT_STRTAB)
779
59.3k
  {
780
    /* PR 2257:
781
       We used to just goto got_wrong_format_error here
782
       but there are binaries in existance for which this test
783
       will prevent the binutils from working with them at all.
784
       So we are kind, and reset the string index value to 0
785
       so that at least some processing can be done.  */
786
59.3k
    i_ehdrp->e_shstrndx = SHN_UNDEF;
787
59.3k
    if (!abfd->read_only)
788
4.38k
      {
789
4.38k
        _bfd_error_handler
790
4.38k
    (_("warning: %pB has a corrupt string table index"), abfd);
791
4.38k
        abfd->read_only = 1;
792
4.38k
      }
793
59.3k
  }
794
435k
    }
795
220k
  else if (i_ehdrp->e_shstrndx != SHN_UNDEF)
796
1.52k
    goto got_wrong_format_error;
797
798
  /* Read in the program headers.  */
799
654k
  if (i_ehdrp->e_phnum == 0)
800
408k
    elf_tdata (abfd)->phdr = NULL;
801
246k
  else
802
246k
    {
803
246k
      Elf_Internal_Phdr *i_phdr;
804
246k
      unsigned int i;
805
246k
      ufile_ptr filesize;
806
246k
      size_t amt;
807
808
      /* Check for a corrupt input file with an impossibly large number
809
   of program headers.  */
810
246k
      filesize = bfd_get_file_size (abfd);
811
246k
      if (filesize != 0
812
246k
    && i_ehdrp->e_phnum > filesize / sizeof (Elf_External_Phdr))
813
12.2k
  goto got_wrong_format_error;
814
234k
      if (_bfd_mul_overflow (i_ehdrp->e_phnum, sizeof (*i_phdr), &amt))
815
0
  goto got_wrong_format_error;
816
234k
      elf_tdata (abfd)->phdr
817
234k
  = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
818
234k
      if (elf_tdata (abfd)->phdr == NULL)
819
0
  goto got_no_match;
820
234k
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
821
1.33k
  goto got_no_match;
822
232k
      bool eu_strip_broken_phdrs = false;
823
232k
      i_phdr = elf_tdata (abfd)->phdr;
824
16.3M
      for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
825
16.1M
  {
826
16.1M
    Elf_External_Phdr x_phdr;
827
828
16.1M
    if (bfd_read (&x_phdr, sizeof x_phdr, abfd) != sizeof x_phdr)
829
3.65k
      goto got_no_match;
830
16.1M
    elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
831
    /* Too much code in BFD relies on alignment being a power of
832
       two, as required by the ELF spec.  */
833
16.1M
    if (i_phdr->p_align != (i_phdr->p_align & -i_phdr->p_align))
834
13.5M
      {
835
13.5M
        i_phdr->p_align &= -i_phdr->p_align;
836
13.5M
        if (!abfd->read_only)
837
156k
    {
838
156k
      _bfd_error_handler (_("warning: %pB has a program header "
839
156k
          "with invalid alignment"), abfd);
840
156k
      abfd->read_only = 1;
841
156k
    }
842
13.5M
      }
843
    /* Detect eu-strip -f debug files, which have program
844
       headers that describe the original file.  */
845
16.1M
    if (i_phdr->p_filesz != 0
846
16.1M
        && (i_phdr->p_filesz > filesize
847
13.8M
      || i_phdr->p_offset > filesize - i_phdr->p_filesz))
848
13.5M
      eu_strip_broken_phdrs = true;
849
16.1M
  }
850
229k
      if (!eu_strip_broken_phdrs
851
229k
    && i_ehdrp->e_shoff == 0
852
229k
    && i_ehdrp->e_shstrndx == 0)
853
19.4k
  {
854
    /* Try to reconstruct dynamic symbol table from PT_DYNAMIC
855
       segment if there is no section header.  */
856
19.4k
    i_phdr = elf_tdata (abfd)->phdr;
857
40.7k
    for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
858
34.9k
      if (i_phdr->p_type == PT_DYNAMIC)
859
13.6k
        {
860
13.6k
    if (i_phdr->p_filesz != 0
861
13.6k
        && !_bfd_elf_get_dynamic_symbols (abfd, i_phdr,
862
12.0k
                  elf_tdata (abfd)->phdr,
863
12.0k
                  i_ehdrp->e_phnum,
864
12.0k
                  filesize))
865
12.0k
      goto got_no_match;
866
1.66k
    break;
867
13.6k
        }
868
19.4k
  }
869
229k
    }
870
871
625k
  if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff >= sizeof (x_ehdr))
872
374k
    {
873
374k
      unsigned int num_sec;
874
875
      /* Once all of the section headers have been read and converted, we
876
   can start processing them.  Note that the first section header is
877
   a dummy placeholder entry, so we ignore it.  */
878
374k
      num_sec = elf_numsections (abfd);
879
4.83M
      for (shindex = 1; shindex < num_sec; shindex++)
880
4.61M
  if (!bfd_section_from_shdr (abfd, shindex))
881
147k
    goto got_no_match;
882
883
      /* Set up ELF sections for SHF_GROUP and SHF_LINK_ORDER.  */
884
226k
      if (! _bfd_elf_setup_sections (abfd))
885
11.3k
  goto got_wrong_format_error;
886
226k
    }
887
888
  /* Let the backend double check the format and override global
889
     information.  */
890
466k
  if (ebd->elf_backend_object_p)
891
271k
    {
892
271k
      if (! (*ebd->elf_backend_object_p) (abfd))
893
676
  goto got_wrong_format_error;
894
271k
    }
895
896
  /* Remember the entry point specified in the ELF file header.  */
897
465k
  bfd_set_start_address (abfd, i_ehdrp->e_entry);
898
899
  /* If we have created any reloc sections that are associated with
900
     debugging sections, mark the reloc sections as debugging as well.  */
901
2.27M
  for (s = abfd->sections; s != NULL; s = s->next)
902
1.80M
    {
903
1.80M
      if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL
904
1.80M
     || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
905
1.80M
    && elf_section_data (s)->this_hdr.sh_info > 0)
906
108k
  {
907
108k
    unsigned long targ_index;
908
108k
    asection *targ_sec;
909
910
108k
    targ_index = elf_section_data (s)->this_hdr.sh_info;
911
108k
    targ_sec = bfd_section_from_elf_index (abfd, targ_index);
912
108k
    if (targ_sec != NULL
913
108k
        && (targ_sec->flags & SEC_DEBUGGING) != 0)
914
8.96k
      s->flags |= SEC_DEBUGGING;
915
108k
  }
916
1.80M
    }
917
465k
  return _bfd_no_cleanup;
918
919
121M
 got_wrong_format_error:
920
121M
  bfd_set_error (bfd_error_wrong_format);
921
922
122M
 got_no_match:
923
122M
  return NULL;
924
121M
}
bfd_elf32_object_p
Line
Count
Source
519
367M
{
520
367M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
521
367M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
522
367M
  Elf_External_Shdr x_shdr;  /* Section header table entry, external form */
523
367M
  Elf_Internal_Shdr i_shdr;
524
367M
  Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
525
367M
  unsigned int shindex;
526
367M
  const struct elf_backend_data *ebd;
527
367M
  asection *s;
528
367M
  const bfd_target *target;
529
530
  /* Read in the ELF header in external format.  */
531
532
367M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
533
37.0M
    {
534
37.0M
      if (bfd_get_error () != bfd_error_system_call)
535
36.9M
  goto got_wrong_format_error;
536
109k
      else
537
109k
  goto got_no_match;
538
37.0M
    }
539
540
  /* Now check to see if we have a valid ELF file, and one that BFD can
541
     make use of.  The magic number must match, the address size ('class')
542
     and byte-swapping must match our XVEC entry, and it must have a
543
     section header table (FIXME: See comments re sections at top of this
544
     file).  */
545
546
330M
  if (! elf_file_p (&x_ehdr)
547
330M
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
548
330M
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
549
297M
    goto got_wrong_format_error;
550
551
  /* Check that file's byte order matches xvec's */
552
33.3M
  switch (x_ehdr.e_ident[EI_DATA])
553
33.3M
    {
554
4.46M
    case ELFDATA2MSB:   /* Big-endian */
555
4.46M
      if (! bfd_header_big_endian (abfd))
556
2.21M
  goto got_wrong_format_error;
557
2.24M
      break;
558
28.8M
    case ELFDATA2LSB:   /* Little-endian */
559
28.8M
      if (! bfd_header_little_endian (abfd))
560
14.3M
  goto got_wrong_format_error;
561
14.5M
      break;
562
14.5M
    case ELFDATANONE:   /* No data encoding specified */
563
15.5k
    default:      /* Unknown data encoding specified */
564
15.5k
      goto got_wrong_format_error;
565
33.3M
    }
566
567
16.7M
  target = abfd->xvec;
568
569
  /* Allocate an instance of the elf_obj_tdata structure and hook it up to
570
     the tdata pointer in the bfd.  */
571
572
16.7M
  if (! (*target->_bfd_set_format[bfd_object]) (abfd))
573
0
    goto got_no_match;
574
575
  /* Now that we know the byte order, swap in the rest of the header */
576
16.7M
  i_ehdrp = elf_elfheader (abfd);
577
16.7M
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
578
#if DEBUG & 1
579
  elf_debug_file (i_ehdrp);
580
#endif
581
582
  /* Reject ET_CORE (header indicates core file, not object file) */
583
16.7M
  if (i_ehdrp->e_type == ET_CORE)
584
156k
    goto got_wrong_format_error;
585
586
  /* If this is a relocatable file and there is no section header
587
     table, then we're hosed.  */
588
16.6M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_type == ET_REL)
589
24.4k
    goto got_wrong_format_error;
590
591
  /* As a simple sanity check, verify that what BFD thinks is the
592
     size of each section header table entry actually matches the size
593
     recorded in the file, but only if there are any sections.  */
594
16.5M
  if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
595
49.4k
    goto got_wrong_format_error;
596
597
  /* Further sanity check.  */
598
16.5M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_shnum != 0)
599
13.7k
    goto got_wrong_format_error;
600
601
16.5M
  ebd = get_elf_backend_data (abfd);
602
16.5M
  if (ebd->s->arch_size != ARCH_SIZE)
603
0
    goto got_wrong_format_error;
604
605
  /* Check that the ELF e_machine field matches what this particular
606
     BFD format expects.  */
607
16.5M
  if (ebd->elf_machine_code != i_ehdrp->e_machine
608
16.5M
      && (ebd->elf_machine_alt1 == 0
609
15.9M
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
610
16.5M
      && (ebd->elf_machine_alt2 == 0
611
15.9M
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
612
16.5M
      && ebd->elf_machine_code != EM_NONE)
613
15.6M
    goto got_wrong_format_error;
614
615
854k
  if (i_ehdrp->e_type == ET_EXEC)
616
19.5k
    abfd->flags |= EXEC_P;
617
834k
  else if (i_ehdrp->e_type == ET_DYN)
618
15.1k
    abfd->flags |= DYNAMIC;
619
620
854k
  if (i_ehdrp->e_phnum > 0)
621
322k
    abfd->flags |= D_PAGED;
622
623
854k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
624
250k
    {
625
      /* It's OK if this fails for the generic target.  */
626
250k
      if (ebd->elf_machine_code != EM_NONE)
627
673
  goto got_no_match;
628
250k
    }
629
630
853k
  if (ebd->elf_machine_code != EM_NONE
631
853k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
632
853k
      && ebd->elf_osabi != ELFOSABI_NONE)
633
50.8k
    goto got_wrong_format_error;
634
635
802k
  if (i_ehdrp->e_shoff >= sizeof (x_ehdr))
636
467k
    {
637
467k
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
638
639
      /* Seek to the section header table in the file.  */
640
467k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
641
0
  goto got_no_match;
642
643
      /* Read the first section header at index 0, and convert to internal
644
   form.  */
645
467k
      if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
646
2.98k
  goto got_no_match;
647
464k
      elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
648
649
      /* If the section count is zero, the actual count is in the first
650
   section header.  */
651
464k
      if (i_ehdrp->e_shnum == SHN_UNDEF)
652
27.9k
  {
653
27.9k
    i_ehdrp->e_shnum = i_shdr.sh_size;
654
27.9k
    if (i_ehdrp->e_shnum >= SHN_LORESERVE
655
27.9k
        || i_ehdrp->e_shnum != i_shdr.sh_size
656
27.9k
        || i_ehdrp->e_shnum  == 0)
657
2.95k
      goto got_wrong_format_error;
658
27.9k
  }
659
660
      /* And similarly for the string table index.  */
661
461k
      if (i_ehdrp->e_shstrndx == (SHN_XINDEX & 0xffff))
662
45.0k
  {
663
45.0k
    i_ehdrp->e_shstrndx = i_shdr.sh_link;
664
45.0k
    if (i_ehdrp->e_shstrndx != i_shdr.sh_link)
665
0
      goto got_wrong_format_error;
666
45.0k
  }
667
668
      /* And program headers.  */
669
461k
      if (i_ehdrp->e_phnum == PN_XNUM && i_shdr.sh_info != 0)
670
17.5k
  {
671
17.5k
    i_ehdrp->e_phnum = i_shdr.sh_info;
672
17.5k
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
673
0
      goto got_wrong_format_error;
674
17.5k
  }
675
676
      /* Sanity check that we can read all of the section headers.
677
   It ought to be good enough to just read the last one.  */
678
461k
      if (i_ehdrp->e_shnum != 1)
679
363k
  {
680
    /* Check that we don't have a totally silly number of sections.  */
681
363k
    if (i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (x_shdr)
682
363k
        || i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (i_shdr))
683
3.84k
      goto got_wrong_format_error;
684
685
359k
    where += (i_ehdrp->e_shnum - 1) * sizeof (x_shdr);
686
359k
    if ((bfd_size_type) where <= i_ehdrp->e_shoff)
687
0
      goto got_wrong_format_error;
688
689
359k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
690
0
      goto got_no_match;
691
359k
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
692
2.34k
      goto got_no_match;
693
694
    /* Back to where we were.  */
695
357k
    where = i_ehdrp->e_shoff + sizeof (x_shdr);
696
357k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
697
0
      goto got_no_match;
698
357k
  }
699
461k
    }
700
701
  /* Allocate space for a copy of the section header table in
702
     internal form.  */
703
790k
  if (i_ehdrp->e_shnum != 0)
704
455k
    {
705
455k
      Elf_Internal_Shdr *shdrp;
706
455k
      unsigned int num_sec;
707
455k
      size_t amt;
708
709
455k
      if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*i_shdrp), &amt))
710
0
  goto got_wrong_format_error;
711
455k
      i_shdrp = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
712
455k
      if (!i_shdrp)
713
0
  goto got_no_match;
714
455k
      num_sec = i_ehdrp->e_shnum;
715
455k
      elf_numsections (abfd) = num_sec;
716
455k
      if (_bfd_mul_overflow (num_sec, sizeof (i_shdrp), &amt))
717
0
  goto got_wrong_format_error;
718
455k
      elf_elfsections (abfd) = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
719
455k
      if (!elf_elfsections (abfd))
720
0
  goto got_no_match;
721
455k
      elf_tdata (abfd)->being_created = bfd_zalloc (abfd, num_sec);
722
455k
      if (!elf_tdata (abfd)->being_created)
723
0
  goto got_no_match;
724
725
455k
      memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
726
5.52M
      for (shdrp = i_shdrp, shindex = 0; shindex < num_sec; shindex++)
727
5.06M
  elf_elfsections (abfd)[shindex] = shdrp++;
728
729
      /* Read in the rest of the section header table and convert it
730
   to internal form.  */
731
4.98M
      for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
732
4.54M
  {
733
4.54M
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
734
0
      goto got_no_match;
735
4.54M
    elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
736
737
    /* Sanity check sh_link and sh_info.  */
738
4.54M
    if (i_shdrp[shindex].sh_link >= num_sec)
739
17.1k
      {
740
        /* PR 10478: Accept Solaris binaries with a sh_link
741
     field set to SHN_BEFORE or SHN_AFTER.  */
742
17.1k
        switch (ebd->elf_machine_code)
743
17.1k
    {
744
7.91k
    case EM_386:
745
8.92k
    case EM_IAMCU:
746
10.1k
    case EM_X86_64:
747
10.1k
    case EM_OLD_SPARCV9:
748
10.1k
    case EM_SPARC32PLUS:
749
10.1k
    case EM_SPARCV9:
750
10.2k
    case EM_SPARC:
751
10.2k
      if (i_shdrp[shindex].sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
752
10.2k
          || i_shdrp[shindex].sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
753
7.08k
        break;
754
      /* Otherwise fall through.  */
755
10.1k
    default:
756
10.1k
      goto got_wrong_format_error;
757
17.1k
    }
758
17.1k
      }
759
760
4.53M
    if (((i_shdrp[shindex].sh_flags & SHF_INFO_LINK)
761
4.53M
         || i_shdrp[shindex].sh_type == SHT_RELA
762
4.53M
         || i_shdrp[shindex].sh_type == SHT_REL)
763
4.53M
        && i_shdrp[shindex].sh_info >= num_sec)
764
1.39k
      goto got_wrong_format_error;
765
766
    /* If the section is loaded, but not page aligned, clear
767
       D_PAGED.  */
768
4.53M
    if (i_shdrp[shindex].sh_size != 0
769
4.53M
        && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
770
4.53M
        && i_shdrp[shindex].sh_type != SHT_NOBITS
771
4.53M
        && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
772
603k
       % ebd->minpagesize)
773
603k
      != 0))
774
331k
      abfd->flags &= ~D_PAGED;
775
4.53M
  }
776
777
443k
      if (i_ehdrp->e_shstrndx >= elf_numsections (abfd)
778
443k
    || i_shdrp[i_ehdrp->e_shstrndx].sh_type != SHT_STRTAB)
779
145k
  {
780
    /* PR 2257:
781
       We used to just goto got_wrong_format_error here
782
       but there are binaries in existance for which this test
783
       will prevent the binutils from working with them at all.
784
       So we are kind, and reset the string index value to 0
785
       so that at least some processing can be done.  */
786
145k
    i_ehdrp->e_shstrndx = SHN_UNDEF;
787
145k
    if (!abfd->read_only)
788
20.5k
      {
789
20.5k
        _bfd_error_handler
790
20.5k
    (_("warning: %pB has a corrupt string table index"), abfd);
791
20.5k
        abfd->read_only = 1;
792
20.5k
      }
793
145k
  }
794
443k
    }
795
335k
  else if (i_ehdrp->e_shstrndx != SHN_UNDEF)
796
3.84k
    goto got_wrong_format_error;
797
798
  /* Read in the program headers.  */
799
775k
  if (i_ehdrp->e_phnum == 0)
800
479k
    elf_tdata (abfd)->phdr = NULL;
801
295k
  else
802
295k
    {
803
295k
      Elf_Internal_Phdr *i_phdr;
804
295k
      unsigned int i;
805
295k
      ufile_ptr filesize;
806
295k
      size_t amt;
807
808
      /* Check for a corrupt input file with an impossibly large number
809
   of program headers.  */
810
295k
      filesize = bfd_get_file_size (abfd);
811
295k
      if (filesize != 0
812
295k
    && i_ehdrp->e_phnum > filesize / sizeof (Elf_External_Phdr))
813
3.62k
  goto got_wrong_format_error;
814
291k
      if (_bfd_mul_overflow (i_ehdrp->e_phnum, sizeof (*i_phdr), &amt))
815
0
  goto got_wrong_format_error;
816
291k
      elf_tdata (abfd)->phdr
817
291k
  = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
818
291k
      if (elf_tdata (abfd)->phdr == NULL)
819
0
  goto got_no_match;
820
291k
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
821
0
  goto got_no_match;
822
291k
      bool eu_strip_broken_phdrs = false;
823
291k
      i_phdr = elf_tdata (abfd)->phdr;
824
11.3M
      for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
825
11.0M
  {
826
11.0M
    Elf_External_Phdr x_phdr;
827
828
11.0M
    if (bfd_read (&x_phdr, sizeof x_phdr, abfd) != sizeof x_phdr)
829
3.23k
      goto got_no_match;
830
11.0M
    elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
831
    /* Too much code in BFD relies on alignment being a power of
832
       two, as required by the ELF spec.  */
833
11.0M
    if (i_phdr->p_align != (i_phdr->p_align & -i_phdr->p_align))
834
7.48M
      {
835
7.48M
        i_phdr->p_align &= -i_phdr->p_align;
836
7.48M
        if (!abfd->read_only)
837
36.8k
    {
838
36.8k
      _bfd_error_handler (_("warning: %pB has a program header "
839
36.8k
          "with invalid alignment"), abfd);
840
36.8k
      abfd->read_only = 1;
841
36.8k
    }
842
7.48M
      }
843
    /* Detect eu-strip -f debug files, which have program
844
       headers that describe the original file.  */
845
11.0M
    if (i_phdr->p_filesz != 0
846
11.0M
        && (i_phdr->p_filesz > filesize
847
8.34M
      || i_phdr->p_offset > filesize - i_phdr->p_filesz))
848
7.78M
      eu_strip_broken_phdrs = true;
849
11.0M
  }
850
288k
      if (!eu_strip_broken_phdrs
851
288k
    && i_ehdrp->e_shoff == 0
852
288k
    && i_ehdrp->e_shstrndx == 0)
853
7.23k
  {
854
    /* Try to reconstruct dynamic symbol table from PT_DYNAMIC
855
       segment if there is no section header.  */
856
7.23k
    i_phdr = elf_tdata (abfd)->phdr;
857
20.5k
    for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
858
13.7k
      if (i_phdr->p_type == PT_DYNAMIC)
859
488
        {
860
488
    if (i_phdr->p_filesz != 0
861
488
        && !_bfd_elf_get_dynamic_symbols (abfd, i_phdr,
862
125
                  elf_tdata (abfd)->phdr,
863
125
                  i_ehdrp->e_phnum,
864
125
                  filesize))
865
125
      goto got_no_match;
866
363
    break;
867
488
        }
868
7.23k
  }
869
288k
    }
870
871
768k
  if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff >= sizeof (x_ehdr))
872
296k
    {
873
296k
      unsigned int num_sec;
874
875
      /* Once all of the section headers have been read and converted, we
876
   can start processing them.  Note that the first section header is
877
   a dummy placeholder entry, so we ignore it.  */
878
296k
      num_sec = elf_numsections (abfd);
879
4.30M
      for (shindex = 1; shindex < num_sec; shindex++)
880
4.02M
  if (!bfd_section_from_shdr (abfd, shindex))
881
21.3k
    goto got_no_match;
882
883
      /* Set up ELF sections for SHF_GROUP and SHF_LINK_ORDER.  */
884
274k
      if (! _bfd_elf_setup_sections (abfd))
885
3.44k
  goto got_wrong_format_error;
886
274k
    }
887
888
  /* Let the backend double check the format and override global
889
     information.  */
890
743k
  if (ebd->elf_backend_object_p)
891
497k
    {
892
497k
      if (! (*ebd->elf_backend_object_p) (abfd))
893
94.9k
  goto got_wrong_format_error;
894
497k
    }
895
896
  /* Remember the entry point specified in the ELF file header.  */
897
648k
  bfd_set_start_address (abfd, i_ehdrp->e_entry);
898
899
  /* If we have created any reloc sections that are associated with
900
     debugging sections, mark the reloc sections as debugging as well.  */
901
2.41M
  for (s = abfd->sections; s != NULL; s = s->next)
902
1.76M
    {
903
1.76M
      if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL
904
1.76M
     || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
905
1.76M
    && elf_section_data (s)->this_hdr.sh_info > 0)
906
110k
  {
907
110k
    unsigned long targ_index;
908
110k
    asection *targ_sec;
909
910
110k
    targ_index = elf_section_data (s)->this_hdr.sh_info;
911
110k
    targ_sec = bfd_section_from_elf_index (abfd, targ_index);
912
110k
    if (targ_sec != NULL
913
110k
        && (targ_sec->flags & SEC_DEBUGGING) != 0)
914
2.35k
      s->flags |= SEC_DEBUGGING;
915
110k
  }
916
1.76M
    }
917
648k
  return _bfd_no_cleanup;
918
919
366M
 got_wrong_format_error:
920
366M
  bfd_set_error (bfd_error_wrong_format);
921
922
366M
 got_no_match:
923
366M
  return NULL;
924
366M
}
925

926
/* ELF .o/exec file writing */
927
928
/* Write out the relocs.  */
929
930
void
931
elf_write_relocs (bfd *abfd, asection *sec, void *data)
932
3.50k
{
933
3.50k
  const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
934
3.50k
  bool *failedp = (bool *) data;
935
3.50k
  Elf_Internal_Shdr *rela_hdr;
936
3.50k
  bfd_vma addr_offset;
937
3.50k
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
938
3.50k
  size_t extsize;
939
3.50k
  bfd_byte *dst_rela;
940
3.50k
  unsigned int idx;
941
3.50k
  asymbol *last_sym;
942
3.50k
  int last_sym_idx;
943
3.50k
  size_t amt;
944
945
  /* If we have already failed, don't do anything.  */
946
3.50k
  if (*failedp)
947
0
    return;
948
949
3.50k
  if ((sec->flags & SEC_RELOC) == 0)
950
2.24k
    return;
951
952
  /* The linker backend writes the relocs out itself, and sets the
953
     reloc_count field to zero to inhibit writing them here.  Also,
954
     sometimes the SEC_RELOC flag gets set even when there aren't any
955
     relocs.  */
956
1.25k
  if (sec->reloc_count == 0)
957
0
    return;
958
959
  /* If we have opened an existing file for update, reloc_count may be
960
     set even though we are not linking.  In that case we have nothing
961
     to do.  */
962
1.25k
  if (sec->orelocation == NULL)
963
0
    return;
964
965
1.25k
  rela_hdr = elf_section_data (sec)->rela.hdr;
966
1.25k
  if (rela_hdr == NULL)
967
0
    rela_hdr = elf_section_data (sec)->rel.hdr;
968
969
1.25k
  rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
970
1.25k
  if (_bfd_mul_overflow (sec->reloc_count, rela_hdr->sh_entsize, &amt)
971
1.25k
      || (rela_hdr->contents = bfd_alloc (abfd, amt)) == NULL)
972
0
    {
973
0
      bfd_set_error (bfd_error_no_memory);
974
0
      *failedp = true;
975
0
      return;
976
0
    }
977
978
  /* Figure out whether the relocations are RELA or REL relocations.  */
979
1.25k
  if (rela_hdr->sh_type == SHT_RELA)
980
1.25k
    {
981
1.25k
      swap_out = elf_swap_reloca_out;
982
1.25k
      extsize = sizeof (Elf_External_Rela);
983
1.25k
    }
984
0
  else if (rela_hdr->sh_type == SHT_REL)
985
0
    {
986
0
      swap_out = elf_swap_reloc_out;
987
0
      extsize = sizeof (Elf_External_Rel);
988
0
    }
989
0
  else
990
    /* Every relocation section should be either an SHT_RELA or an
991
       SHT_REL section.  */
992
0
    abort ();
993
994
  /* The address of an ELF reloc is section relative for an object
995
     file, and absolute for an executable file or shared library.
996
     The address of a BFD reloc is always section relative.  */
997
1.25k
  addr_offset = 0;
998
1.25k
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
999
16
    addr_offset = sec->vma;
1000
1001
  /* orelocation has the data, reloc_count has the count...  */
1002
1.25k
  last_sym = 0;
1003
1.25k
  last_sym_idx = 0;
1004
1.25k
  dst_rela = rela_hdr->contents;
1005
1006
72.1k
  for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
1007
70.8k
    {
1008
70.8k
      Elf_Internal_Rela src_rela;
1009
70.8k
      arelent *ptr;
1010
70.8k
      asymbol *sym;
1011
70.8k
      int n;
1012
1013
70.8k
      ptr = sec->orelocation[idx];
1014
70.8k
      sym = *ptr->sym_ptr_ptr;
1015
70.8k
      if (sym == last_sym)
1016
38.5k
  n = last_sym_idx;
1017
      /* If the relocation is against an absolute symbol whoes value is
1018
   zero, then the symbol can be dropped, simplifying the reloc.
1019
   PR 31106: Except for complex relocations where the symbols
1020
   itself might be significant.  */
1021
32.3k
      else if (bfd_is_abs_section (sym->section)
1022
32.3k
         && sym->value == 0
1023
32.3k
         && (sym->flags & BSF_RELC) == 0)
1024
1.46k
  n = STN_UNDEF;
1025
30.8k
      else
1026
30.8k
  {
1027
30.8k
    last_sym = sym;
1028
30.8k
    n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
1029
30.8k
    if (n < 0)
1030
0
      {
1031
0
        *failedp = true;
1032
0
        return;
1033
0
      }
1034
30.8k
    last_sym_idx = n;
1035
30.8k
  }
1036
1037
70.8k
      if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
1038
70.8k
    && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
1039
70.8k
    && ! _bfd_elf_validate_reloc (abfd, ptr))
1040
0
  {
1041
0
    *failedp = true;
1042
0
    return;
1043
0
  }
1044
1045
70.8k
      if (ptr->howto == NULL)
1046
0
  {
1047
0
    *failedp = true;
1048
0
    return;
1049
0
  }
1050
1051
#if defined(BFD64) && ARCH_SIZE == 32
1052
0
      if (rela_hdr->sh_type == SHT_RELA
1053
0
    && ptr->howto->bitsize > 32
1054
0
    && ptr->addend - INT32_MIN > UINT32_MAX)
1055
0
  {
1056
0
    _bfd_error_handler (_("%pB: %pA+%" PRIx64 ": "
1057
0
        "relocation addend %" PRIx64 " too large"),
1058
0
            abfd, sec, (uint64_t) ptr->address,
1059
0
            (uint64_t) ptr->addend);
1060
0
    *failedp = true;
1061
0
    bfd_set_error (bfd_error_bad_value);
1062
0
  }
1063
#endif
1064
1065
70.8k
      src_rela.r_offset = ptr->address + addr_offset;
1066
70.8k
      src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
1067
70.8k
      src_rela.r_addend = ptr->addend;
1068
70.8k
      (*swap_out) (abfd, &src_rela, dst_rela);
1069
70.8k
    }
1070
1071
1.25k
  if (elf_section_data (sec)->has_secondary_relocs
1072
1.25k
      && !bed->write_secondary_relocs (abfd, sec))
1073
0
    {
1074
0
      *failedp = true;
1075
0
      return;
1076
0
    }
1077
1.25k
}
bfd_elf64_write_relocs
Line
Count
Source
932
2.38k
{
933
2.38k
  const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
934
2.38k
  bool *failedp = (bool *) data;
935
2.38k
  Elf_Internal_Shdr *rela_hdr;
936
2.38k
  bfd_vma addr_offset;
937
2.38k
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
938
2.38k
  size_t extsize;
939
2.38k
  bfd_byte *dst_rela;
940
2.38k
  unsigned int idx;
941
2.38k
  asymbol *last_sym;
942
2.38k
  int last_sym_idx;
943
2.38k
  size_t amt;
944
945
  /* If we have already failed, don't do anything.  */
946
2.38k
  if (*failedp)
947
0
    return;
948
949
2.38k
  if ((sec->flags & SEC_RELOC) == 0)
950
1.12k
    return;
951
952
  /* The linker backend writes the relocs out itself, and sets the
953
     reloc_count field to zero to inhibit writing them here.  Also,
954
     sometimes the SEC_RELOC flag gets set even when there aren't any
955
     relocs.  */
956
1.25k
  if (sec->reloc_count == 0)
957
0
    return;
958
959
  /* If we have opened an existing file for update, reloc_count may be
960
     set even though we are not linking.  In that case we have nothing
961
     to do.  */
962
1.25k
  if (sec->orelocation == NULL)
963
0
    return;
964
965
1.25k
  rela_hdr = elf_section_data (sec)->rela.hdr;
966
1.25k
  if (rela_hdr == NULL)
967
0
    rela_hdr = elf_section_data (sec)->rel.hdr;
968
969
1.25k
  rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
970
1.25k
  if (_bfd_mul_overflow (sec->reloc_count, rela_hdr->sh_entsize, &amt)
971
1.25k
      || (rela_hdr->contents = bfd_alloc (abfd, amt)) == NULL)
972
0
    {
973
0
      bfd_set_error (bfd_error_no_memory);
974
0
      *failedp = true;
975
0
      return;
976
0
    }
977
978
  /* Figure out whether the relocations are RELA or REL relocations.  */
979
1.25k
  if (rela_hdr->sh_type == SHT_RELA)
980
1.25k
    {
981
1.25k
      swap_out = elf_swap_reloca_out;
982
1.25k
      extsize = sizeof (Elf_External_Rela);
983
1.25k
    }
984
0
  else if (rela_hdr->sh_type == SHT_REL)
985
0
    {
986
0
      swap_out = elf_swap_reloc_out;
987
0
      extsize = sizeof (Elf_External_Rel);
988
0
    }
989
0
  else
990
    /* Every relocation section should be either an SHT_RELA or an
991
       SHT_REL section.  */
992
0
    abort ();
993
994
  /* The address of an ELF reloc is section relative for an object
995
     file, and absolute for an executable file or shared library.
996
     The address of a BFD reloc is always section relative.  */
997
1.25k
  addr_offset = 0;
998
1.25k
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
999
16
    addr_offset = sec->vma;
1000
1001
  /* orelocation has the data, reloc_count has the count...  */
1002
1.25k
  last_sym = 0;
1003
1.25k
  last_sym_idx = 0;
1004
1.25k
  dst_rela = rela_hdr->contents;
1005
1006
72.1k
  for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
1007
70.8k
    {
1008
70.8k
      Elf_Internal_Rela src_rela;
1009
70.8k
      arelent *ptr;
1010
70.8k
      asymbol *sym;
1011
70.8k
      int n;
1012
1013
70.8k
      ptr = sec->orelocation[idx];
1014
70.8k
      sym = *ptr->sym_ptr_ptr;
1015
70.8k
      if (sym == last_sym)
1016
38.5k
  n = last_sym_idx;
1017
      /* If the relocation is against an absolute symbol whoes value is
1018
   zero, then the symbol can be dropped, simplifying the reloc.
1019
   PR 31106: Except for complex relocations where the symbols
1020
   itself might be significant.  */
1021
32.3k
      else if (bfd_is_abs_section (sym->section)
1022
32.3k
         && sym->value == 0
1023
32.3k
         && (sym->flags & BSF_RELC) == 0)
1024
1.46k
  n = STN_UNDEF;
1025
30.8k
      else
1026
30.8k
  {
1027
30.8k
    last_sym = sym;
1028
30.8k
    n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
1029
30.8k
    if (n < 0)
1030
0
      {
1031
0
        *failedp = true;
1032
0
        return;
1033
0
      }
1034
30.8k
    last_sym_idx = n;
1035
30.8k
  }
1036
1037
70.8k
      if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
1038
70.8k
    && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
1039
70.8k
    && ! _bfd_elf_validate_reloc (abfd, ptr))
1040
0
  {
1041
0
    *failedp = true;
1042
0
    return;
1043
0
  }
1044
1045
70.8k
      if (ptr->howto == NULL)
1046
0
  {
1047
0
    *failedp = true;
1048
0
    return;
1049
0
  }
1050
1051
#if defined(BFD64) && ARCH_SIZE == 32
1052
      if (rela_hdr->sh_type == SHT_RELA
1053
    && ptr->howto->bitsize > 32
1054
    && ptr->addend - INT32_MIN > UINT32_MAX)
1055
  {
1056
    _bfd_error_handler (_("%pB: %pA+%" PRIx64 ": "
1057
        "relocation addend %" PRIx64 " too large"),
1058
            abfd, sec, (uint64_t) ptr->address,
1059
            (uint64_t) ptr->addend);
1060
    *failedp = true;
1061
    bfd_set_error (bfd_error_bad_value);
1062
  }
1063
#endif
1064
1065
70.8k
      src_rela.r_offset = ptr->address + addr_offset;
1066
70.8k
      src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
1067
70.8k
      src_rela.r_addend = ptr->addend;
1068
70.8k
      (*swap_out) (abfd, &src_rela, dst_rela);
1069
70.8k
    }
1070
1071
1.25k
  if (elf_section_data (sec)->has_secondary_relocs
1072
1.25k
      && !bed->write_secondary_relocs (abfd, sec))
1073
0
    {
1074
0
      *failedp = true;
1075
0
      return;
1076
0
    }
1077
1.25k
}
bfd_elf32_write_relocs
Line
Count
Source
932
1.11k
{
933
1.11k
  const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
934
1.11k
  bool *failedp = (bool *) data;
935
1.11k
  Elf_Internal_Shdr *rela_hdr;
936
1.11k
  bfd_vma addr_offset;
937
1.11k
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
938
1.11k
  size_t extsize;
939
1.11k
  bfd_byte *dst_rela;
940
1.11k
  unsigned int idx;
941
1.11k
  asymbol *last_sym;
942
1.11k
  int last_sym_idx;
943
1.11k
  size_t amt;
944
945
  /* If we have already failed, don't do anything.  */
946
1.11k
  if (*failedp)
947
0
    return;
948
949
1.11k
  if ((sec->flags & SEC_RELOC) == 0)
950
1.11k
    return;
951
952
  /* The linker backend writes the relocs out itself, and sets the
953
     reloc_count field to zero to inhibit writing them here.  Also,
954
     sometimes the SEC_RELOC flag gets set even when there aren't any
955
     relocs.  */
956
0
  if (sec->reloc_count == 0)
957
0
    return;
958
959
  /* If we have opened an existing file for update, reloc_count may be
960
     set even though we are not linking.  In that case we have nothing
961
     to do.  */
962
0
  if (sec->orelocation == NULL)
963
0
    return;
964
965
0
  rela_hdr = elf_section_data (sec)->rela.hdr;
966
0
  if (rela_hdr == NULL)
967
0
    rela_hdr = elf_section_data (sec)->rel.hdr;
968
969
0
  rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
970
0
  if (_bfd_mul_overflow (sec->reloc_count, rela_hdr->sh_entsize, &amt)
971
0
      || (rela_hdr->contents = bfd_alloc (abfd, amt)) == NULL)
972
0
    {
973
0
      bfd_set_error (bfd_error_no_memory);
974
0
      *failedp = true;
975
0
      return;
976
0
    }
977
978
  /* Figure out whether the relocations are RELA or REL relocations.  */
979
0
  if (rela_hdr->sh_type == SHT_RELA)
980
0
    {
981
0
      swap_out = elf_swap_reloca_out;
982
0
      extsize = sizeof (Elf_External_Rela);
983
0
    }
984
0
  else if (rela_hdr->sh_type == SHT_REL)
985
0
    {
986
0
      swap_out = elf_swap_reloc_out;
987
0
      extsize = sizeof (Elf_External_Rel);
988
0
    }
989
0
  else
990
    /* Every relocation section should be either an SHT_RELA or an
991
       SHT_REL section.  */
992
0
    abort ();
993
994
  /* The address of an ELF reloc is section relative for an object
995
     file, and absolute for an executable file or shared library.
996
     The address of a BFD reloc is always section relative.  */
997
0
  addr_offset = 0;
998
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
999
0
    addr_offset = sec->vma;
1000
1001
  /* orelocation has the data, reloc_count has the count...  */
1002
0
  last_sym = 0;
1003
0
  last_sym_idx = 0;
1004
0
  dst_rela = rela_hdr->contents;
1005
1006
0
  for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
1007
0
    {
1008
0
      Elf_Internal_Rela src_rela;
1009
0
      arelent *ptr;
1010
0
      asymbol *sym;
1011
0
      int n;
1012
1013
0
      ptr = sec->orelocation[idx];
1014
0
      sym = *ptr->sym_ptr_ptr;
1015
0
      if (sym == last_sym)
1016
0
  n = last_sym_idx;
1017
      /* If the relocation is against an absolute symbol whoes value is
1018
   zero, then the symbol can be dropped, simplifying the reloc.
1019
   PR 31106: Except for complex relocations where the symbols
1020
   itself might be significant.  */
1021
0
      else if (bfd_is_abs_section (sym->section)
1022
0
         && sym->value == 0
1023
0
         && (sym->flags & BSF_RELC) == 0)
1024
0
  n = STN_UNDEF;
1025
0
      else
1026
0
  {
1027
0
    last_sym = sym;
1028
0
    n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
1029
0
    if (n < 0)
1030
0
      {
1031
0
        *failedp = true;
1032
0
        return;
1033
0
      }
1034
0
    last_sym_idx = n;
1035
0
  }
1036
1037
0
      if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
1038
0
    && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
1039
0
    && ! _bfd_elf_validate_reloc (abfd, ptr))
1040
0
  {
1041
0
    *failedp = true;
1042
0
    return;
1043
0
  }
1044
1045
0
      if (ptr->howto == NULL)
1046
0
  {
1047
0
    *failedp = true;
1048
0
    return;
1049
0
  }
1050
1051
0
#if defined(BFD64) && ARCH_SIZE == 32
1052
0
      if (rela_hdr->sh_type == SHT_RELA
1053
0
    && ptr->howto->bitsize > 32
1054
0
    && ptr->addend - INT32_MIN > UINT32_MAX)
1055
0
  {
1056
0
    _bfd_error_handler (_("%pB: %pA+%" PRIx64 ": "
1057
0
        "relocation addend %" PRIx64 " too large"),
1058
0
            abfd, sec, (uint64_t) ptr->address,
1059
0
            (uint64_t) ptr->addend);
1060
0
    *failedp = true;
1061
0
    bfd_set_error (bfd_error_bad_value);
1062
0
  }
1063
0
#endif
1064
1065
0
      src_rela.r_offset = ptr->address + addr_offset;
1066
0
      src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
1067
0
      src_rela.r_addend = ptr->addend;
1068
0
      (*swap_out) (abfd, &src_rela, dst_rela);
1069
0
    }
1070
1071
0
  if (elf_section_data (sec)->has_secondary_relocs
1072
0
      && !bed->write_secondary_relocs (abfd, sec))
1073
0
    {
1074
0
      *failedp = true;
1075
0
      return;
1076
0
    }
1077
0
}
1078
1079
/* Write out the program headers.  */
1080
1081
int
1082
elf_write_out_phdrs (bfd *abfd,
1083
         const Elf_Internal_Phdr *phdr,
1084
         unsigned int count)
1085
42
{
1086
282
  while (count--)
1087
240
    {
1088
240
      Elf_External_Phdr extphdr;
1089
1090
240
      elf_swap_phdr_out (abfd, phdr, &extphdr);
1091
240
      if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), abfd)
1092
240
    != sizeof (Elf_External_Phdr))
1093
0
  return -1;
1094
240
      phdr++;
1095
240
    }
1096
42
  return 0;
1097
42
}
bfd_elf64_write_out_phdrs
Line
Count
Source
1085
3
{
1086
42
  while (count--)
1087
39
    {
1088
39
      Elf_External_Phdr extphdr;
1089
1090
39
      elf_swap_phdr_out (abfd, phdr, &extphdr);
1091
39
      if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), abfd)
1092
39
    != sizeof (Elf_External_Phdr))
1093
0
  return -1;
1094
39
      phdr++;
1095
39
    }
1096
3
  return 0;
1097
3
}
bfd_elf32_write_out_phdrs
Line
Count
Source
1085
39
{
1086
240
  while (count--)
1087
201
    {
1088
201
      Elf_External_Phdr extphdr;
1089
1090
201
      elf_swap_phdr_out (abfd, phdr, &extphdr);
1091
201
      if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), abfd)
1092
201
    != sizeof (Elf_External_Phdr))
1093
0
  return -1;
1094
201
      phdr++;
1095
201
    }
1096
39
  return 0;
1097
39
}
1098
1099
/* Write out the section headers and the ELF file header.  */
1100
1101
bool
1102
elf_write_shdrs_and_ehdr (bfd *abfd)
1103
197
{
1104
197
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
1105
197
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1106
197
  Elf_External_Shdr *x_shdrp;  /* Section header table, external form */
1107
197
  Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
1108
197
  unsigned int count;
1109
197
  size_t amt;
1110
1111
197
  i_ehdrp = elf_elfheader (abfd);
1112
197
  i_shdrp = elf_elfsections (abfd);
1113
1114
  /* swap the header before spitting it out...  */
1115
1116
#if DEBUG & 1
1117
  elf_debug_file (i_ehdrp);
1118
#endif
1119
197
  elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
1120
197
  amt = sizeof (x_ehdr);
1121
197
  if (bfd_seek (abfd, 0, SEEK_SET) != 0
1122
197
      || bfd_write (&x_ehdr, amt, abfd) != amt)
1123
0
    return false;
1124
1125
197
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
1126
0
    return true;
1127
1128
  /* Some fields in the first section header handle overflow of ehdr
1129
     fields.  */
1130
197
  if (i_ehdrp->e_phnum >= PN_XNUM)
1131
0
    i_shdrp[0]->sh_info = i_ehdrp->e_phnum;
1132
197
  if (i_ehdrp->e_shnum >= (SHN_LORESERVE & 0xffff))
1133
0
    i_shdrp[0]->sh_size = i_ehdrp->e_shnum;
1134
197
  if (i_ehdrp->e_shstrndx >= (SHN_LORESERVE & 0xffff))
1135
0
    i_shdrp[0]->sh_link = i_ehdrp->e_shstrndx;
1136
1137
  /* at this point we've concocted all the ELF sections...  */
1138
197
  if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*x_shdrp), &amt))
1139
0
    {
1140
0
      bfd_set_error (bfd_error_no_memory);
1141
0
      return false;
1142
0
    }
1143
197
  x_shdrp = (Elf_External_Shdr *) bfd_alloc (abfd, amt);
1144
197
  if (!x_shdrp)
1145
0
    return false;
1146
1147
5.51k
  for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++)
1148
5.31k
    {
1149
#if DEBUG & 2
1150
      elf_debug_section (count, *i_shdrp);
1151
#endif
1152
5.31k
      elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count);
1153
5.31k
    }
1154
197
  amt = (bfd_size_type) i_ehdrp->e_shnum * sizeof (*x_shdrp);
1155
197
  if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0
1156
197
      || bfd_write (x_shdrp, amt, abfd) != amt)
1157
0
    return false;
1158
1159
  /* need to dump the string table too...  */
1160
1161
197
  return true;
1162
197
}
bfd_elf64_write_shdrs_and_ehdr
Line
Count
Source
1103
119
{
1104
119
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
1105
119
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1106
119
  Elf_External_Shdr *x_shdrp;  /* Section header table, external form */
1107
119
  Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
1108
119
  unsigned int count;
1109
119
  size_t amt;
1110
1111
119
  i_ehdrp = elf_elfheader (abfd);
1112
119
  i_shdrp = elf_elfsections (abfd);
1113
1114
  /* swap the header before spitting it out...  */
1115
1116
#if DEBUG & 1
1117
  elf_debug_file (i_ehdrp);
1118
#endif
1119
119
  elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
1120
119
  amt = sizeof (x_ehdr);
1121
119
  if (bfd_seek (abfd, 0, SEEK_SET) != 0
1122
119
      || bfd_write (&x_ehdr, amt, abfd) != amt)
1123
0
    return false;
1124
1125
119
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
1126
0
    return true;
1127
1128
  /* Some fields in the first section header handle overflow of ehdr
1129
     fields.  */
1130
119
  if (i_ehdrp->e_phnum >= PN_XNUM)
1131
0
    i_shdrp[0]->sh_info = i_ehdrp->e_phnum;
1132
119
  if (i_ehdrp->e_shnum >= (SHN_LORESERVE & 0xffff))
1133
0
    i_shdrp[0]->sh_size = i_ehdrp->e_shnum;
1134
119
  if (i_ehdrp->e_shstrndx >= (SHN_LORESERVE & 0xffff))
1135
0
    i_shdrp[0]->sh_link = i_ehdrp->e_shstrndx;
1136
1137
  /* at this point we've concocted all the ELF sections...  */
1138
119
  if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*x_shdrp), &amt))
1139
0
    {
1140
0
      bfd_set_error (bfd_error_no_memory);
1141
0
      return false;
1142
0
    }
1143
119
  x_shdrp = (Elf_External_Shdr *) bfd_alloc (abfd, amt);
1144
119
  if (!x_shdrp)
1145
0
    return false;
1146
1147
4.09k
  for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++)
1148
3.97k
    {
1149
#if DEBUG & 2
1150
      elf_debug_section (count, *i_shdrp);
1151
#endif
1152
3.97k
      elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count);
1153
3.97k
    }
1154
119
  amt = (bfd_size_type) i_ehdrp->e_shnum * sizeof (*x_shdrp);
1155
119
  if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0
1156
119
      || bfd_write (x_shdrp, amt, abfd) != amt)
1157
0
    return false;
1158
1159
  /* need to dump the string table too...  */
1160
1161
119
  return true;
1162
119
}
bfd_elf32_write_shdrs_and_ehdr
Line
Count
Source
1103
78
{
1104
78
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
1105
78
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1106
78
  Elf_External_Shdr *x_shdrp;  /* Section header table, external form */
1107
78
  Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
1108
78
  unsigned int count;
1109
78
  size_t amt;
1110
1111
78
  i_ehdrp = elf_elfheader (abfd);
1112
78
  i_shdrp = elf_elfsections (abfd);
1113
1114
  /* swap the header before spitting it out...  */
1115
1116
#if DEBUG & 1
1117
  elf_debug_file (i_ehdrp);
1118
#endif
1119
78
  elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
1120
78
  amt = sizeof (x_ehdr);
1121
78
  if (bfd_seek (abfd, 0, SEEK_SET) != 0
1122
78
      || bfd_write (&x_ehdr, amt, abfd) != amt)
1123
0
    return false;
1124
1125
78
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
1126
0
    return true;
1127
1128
  /* Some fields in the first section header handle overflow of ehdr
1129
     fields.  */
1130
78
  if (i_ehdrp->e_phnum >= PN_XNUM)
1131
0
    i_shdrp[0]->sh_info = i_ehdrp->e_phnum;
1132
78
  if (i_ehdrp->e_shnum >= (SHN_LORESERVE & 0xffff))
1133
0
    i_shdrp[0]->sh_size = i_ehdrp->e_shnum;
1134
78
  if (i_ehdrp->e_shstrndx >= (SHN_LORESERVE & 0xffff))
1135
0
    i_shdrp[0]->sh_link = i_ehdrp->e_shstrndx;
1136
1137
  /* at this point we've concocted all the ELF sections...  */
1138
78
  if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*x_shdrp), &amt))
1139
0
    {
1140
0
      bfd_set_error (bfd_error_no_memory);
1141
0
      return false;
1142
0
    }
1143
78
  x_shdrp = (Elf_External_Shdr *) bfd_alloc (abfd, amt);
1144
78
  if (!x_shdrp)
1145
0
    return false;
1146
1147
1.42k
  for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++)
1148
1.34k
    {
1149
#if DEBUG & 2
1150
      elf_debug_section (count, *i_shdrp);
1151
#endif
1152
1.34k
      elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count);
1153
1.34k
    }
1154
78
  amt = (bfd_size_type) i_ehdrp->e_shnum * sizeof (*x_shdrp);
1155
78
  if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0
1156
78
      || bfd_write (x_shdrp, amt, abfd) != amt)
1157
0
    return false;
1158
1159
  /* need to dump the string table too...  */
1160
1161
78
  return true;
1162
78
}
1163
1164
bool
1165
elf_checksum_contents (bfd *abfd,
1166
           void (*process) (const void *, size_t, void *),
1167
           void *arg)
1168
0
{
1169
0
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
1170
0
  Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
1171
0
  Elf_Internal_Phdr *i_phdrp = elf_tdata (abfd)->phdr;
1172
0
  unsigned int count, num;
1173
1174
0
  {
1175
0
    Elf_External_Ehdr x_ehdr;
1176
0
    Elf_Internal_Ehdr i_ehdr;
1177
1178
0
    i_ehdr = *i_ehdrp;
1179
0
    i_ehdr.e_phoff = i_ehdr.e_shoff = 0;
1180
0
    elf_swap_ehdr_out (abfd, &i_ehdr, &x_ehdr);
1181
0
    (*process) (&x_ehdr, sizeof x_ehdr, arg);
1182
0
  }
1183
1184
0
  num = i_ehdrp->e_phnum;
1185
0
  for (count = 0; count < num; count++)
1186
0
    {
1187
0
      Elf_External_Phdr x_phdr;
1188
0
      elf_swap_phdr_out (abfd, &i_phdrp[count], &x_phdr);
1189
0
      (*process) (&x_phdr, sizeof x_phdr, arg);
1190
0
    }
1191
1192
0
  num = elf_numsections (abfd);
1193
0
  for (count = 0; count < num; count++)
1194
0
    {
1195
0
      Elf_Internal_Shdr i_shdr;
1196
0
      Elf_External_Shdr x_shdr;
1197
0
      bfd_byte *contents, *free_contents;
1198
0
      asection *sec = NULL;
1199
1200
0
      i_shdr = *i_shdrp[count];
1201
0
      i_shdr.sh_offset = 0;
1202
1203
0
      elf_swap_shdr_out (abfd, &i_shdr, &x_shdr);
1204
0
      (*process) (&x_shdr, sizeof x_shdr, arg);
1205
1206
      /* Process the section's contents, if it has some.
1207
   PR ld/12451: Read them in if necessary.  */
1208
0
      if (i_shdr.sh_type == SHT_NOBITS)
1209
0
  continue;
1210
0
      free_contents = NULL;
1211
0
      contents = i_shdr.contents;
1212
0
      if (contents == NULL)
1213
0
  {
1214
0
    sec = bfd_section_from_elf_index (abfd, count);
1215
0
    if (sec != NULL)
1216
0
      {
1217
0
        contents = sec->contents;
1218
0
        if (contents == NULL)
1219
0
    {
1220
      /* Force rereading from file.  */
1221
0
      sec->flags &= ~SEC_IN_MEMORY;
1222
0
      if (!_bfd_elf_mmap_section_contents (abfd, sec, &free_contents))
1223
0
        continue;
1224
0
      contents = free_contents;
1225
0
    }
1226
0
      }
1227
0
  }
1228
0
      if (contents != NULL)
1229
0
  {
1230
0
    (*process) (contents, i_shdr.sh_size, arg);
1231
0
    _bfd_elf_munmap_section_contents (sec, free_contents);
1232
0
  }
1233
0
    }
1234
1235
0
  return true;
1236
0
}
Unexecuted instantiation: bfd_elf64_checksum_contents
Unexecuted instantiation: bfd_elf32_checksum_contents
1237
1238
long
1239
elf_slurp_symbol_table (bfd *abfd, asymbol **symptrs, bool dynamic)
1240
8.31k
{
1241
8.31k
  Elf_Internal_Shdr *hdr;
1242
8.31k
  Elf_Internal_Shdr *verhdr;
1243
8.31k
  unsigned long symcount; /* Number of external ELF symbols */
1244
8.31k
  elf_symbol_type *sym;   /* Pointer to current bfd symbol */
1245
8.31k
  elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
1246
8.31k
  Elf_Internal_Sym *isym;
1247
8.31k
  Elf_Internal_Sym *isymend;
1248
8.31k
  Elf_Internal_Sym *isymbuf = NULL;
1249
8.31k
  Elf_External_Versym *xver;
1250
8.31k
  Elf_External_Versym *xverbuf = NULL;
1251
8.31k
  const struct elf_backend_data *ebd;
1252
8.31k
  size_t amt;
1253
1254
  /* Read each raw ELF symbol, converting from external ELF form to
1255
     internal ELF form, and then using the information to create a
1256
     canonical bfd symbol table entry.
1257
1258
     Note that we allocate the initial bfd canonical symbol buffer
1259
     based on a one-to-one mapping of the ELF symbols to canonical
1260
     symbols.  We actually use all the ELF symbols, so there will be no
1261
     space left over at the end.  When we have all the symbols, we
1262
     build the caller's pointer vector.  */
1263
8.31k
  ebd = get_elf_backend_data (abfd);
1264
1265
8.31k
  if (! dynamic)
1266
7.78k
    {
1267
7.78k
      hdr = &elf_tdata (abfd)->symtab_hdr;
1268
7.78k
      verhdr = NULL;
1269
7.78k
      symcount = hdr->sh_size / ebd->s->sizeof_sym;
1270
7.78k
    }
1271
531
  else
1272
531
    {
1273
531
      hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1274
531
      if (elf_dynversym (abfd) == 0)
1275
91
  verhdr = NULL;
1276
440
      else
1277
440
  verhdr = &elf_tdata (abfd)->dynversym_hdr;
1278
531
      if ((elf_dynverdef (abfd) != 0
1279
531
     && elf_tdata (abfd)->verdef == NULL)
1280
531
    || (elf_dynverref (abfd) != 0
1281
519
        && elf_tdata (abfd)->verref == NULL)
1282
531
    || elf_tdata (abfd)->dt_verdef != NULL
1283
531
    || elf_tdata (abfd)->dt_verneed != NULL)
1284
157
  {
1285
157
    if (!_bfd_elf_slurp_version_tables (abfd, false))
1286
81
      return -1;
1287
157
  }
1288
1289
450
      symcount = elf_tdata (abfd)->dt_symtab_count;
1290
450
    }
1291
1292
8.23k
  if (symcount == 0)
1293
1.11k
    symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1294
1295
8.23k
  if (symcount == 0)
1296
675
    sym = symbase = NULL;
1297
7.55k
  else
1298
7.55k
    {
1299
7.55k
      size_t i;
1300
1301
7.55k
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
1302
7.55k
              NULL, NULL, NULL);
1303
7.55k
      if (isymbuf == NULL)
1304
1.06k
  return -1;
1305
1306
6.49k
      if (_bfd_mul_overflow (symcount, sizeof (elf_symbol_type), &amt))
1307
0
  {
1308
0
    bfd_set_error (bfd_error_file_too_big);
1309
0
    goto error_return;
1310
0
  }
1311
6.49k
      symbase = (elf_symbol_type *) bfd_zalloc (abfd, amt);
1312
6.49k
      if (symbase == (elf_symbol_type *) NULL)
1313
0
  goto error_return;
1314
1315
      /* Read the raw ELF version symbol information.  */
1316
6.49k
      if (verhdr != NULL
1317
6.49k
    && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
1318
8
  {
1319
8
    _bfd_error_handler
1320
      /* xgettext:c-format */
1321
8
      (_("%pB: version count (%" PRId64 ")"
1322
8
         " does not match symbol count (%ld)"),
1323
8
       abfd,
1324
8
       (int64_t) (verhdr->sh_size / sizeof (Elf_External_Versym)),
1325
8
       symcount);
1326
1327
    /* Slurp in the symbols without the version information,
1328
       since that is more helpful than just quitting.  */
1329
8
    verhdr = NULL;
1330
8
  }
1331
1332
6.49k
      if (verhdr != NULL)
1333
326
  {
1334
326
    if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
1335
3
      goto error_return;
1336
323
    xverbuf = (Elf_External_Versym *)
1337
323
      _bfd_malloc_and_read (abfd, verhdr->sh_size, verhdr->sh_size);
1338
323
    if (xverbuf == NULL && verhdr->sh_size != 0)
1339
2
      goto error_return;
1340
323
  }
1341
1342
      /* Skip first symbol, which is a null dummy.  */
1343
6.49k
      xver = xverbuf;
1344
6.49k
      if (xver != NULL)
1345
321
  ++xver;
1346
6.49k
      isymend = isymbuf + symcount;
1347
6.49k
      for (isym = isymbuf + 1, sym = symbase, i = 1;
1348
1.42M
     isym < isymend;
1349
1.41M
     isym++, sym++, i++)
1350
1.41M
  {
1351
1.41M
    memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
1352
1353
1.41M
    sym->symbol.the_bfd = abfd;
1354
1.41M
    if (elf_use_dt_symtab_p (abfd))
1355
12
      sym->symbol.name = (elf_tdata (abfd)->dt_strtab
1356
12
        + isym->st_name);
1357
1.41M
    else
1358
1.41M
      sym->symbol.name = bfd_elf_sym_name (abfd, hdr, isym, NULL);
1359
1.41M
    sym->symbol.value = isym->st_value;
1360
1361
1.41M
    if (isym->st_shndx == SHN_UNDEF)
1362
371k
      {
1363
371k
        sym->symbol.section = bfd_und_section_ptr;
1364
371k
      }
1365
1.04M
    else if (isym->st_shndx == SHN_ABS)
1366
58.0k
      {
1367
58.0k
        sym->symbol.section = bfd_abs_section_ptr;
1368
58.0k
      }
1369
989k
    else if (isym->st_shndx == SHN_COMMON)
1370
42
      {
1371
42
        sym->symbol.section = bfd_com_section_ptr;
1372
42
        if ((abfd->flags & BFD_PLUGIN) != 0)
1373
0
    {
1374
0
      asection *xc = bfd_get_section_by_name (abfd, "COMMON");
1375
1376
0
      if (xc == NULL)
1377
0
        {
1378
0
          flagword flags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
1379
0
          | SEC_EXCLUDE);
1380
0
          xc = bfd_make_section_with_flags (abfd, "COMMON", flags);
1381
0
          if (xc == NULL)
1382
0
      goto error_return;
1383
0
        }
1384
0
      sym->symbol.section = xc;
1385
0
    }
1386
        /* Elf puts the alignment into the `value' field, and
1387
     the size into the `size' field.  BFD wants to see the
1388
     size in the value field, and doesn't care (at the
1389
     moment) about the alignment.  */
1390
42
        sym->symbol.value = isym->st_size;
1391
42
      }
1392
989k
    else if (elf_use_dt_symtab_p (abfd))
1393
1
      {
1394
1
        asection *sec;
1395
1
        sec = _bfd_elf_get_section_from_dynamic_symbol (abfd,
1396
1
                    isym);
1397
1
        if (sec == NULL)
1398
0
    goto error_return;
1399
1
        sym->symbol.section = sec;
1400
1
      }
1401
989k
    else
1402
989k
      {
1403
989k
        sym->symbol.section
1404
989k
    = bfd_section_from_elf_index (abfd, isym->st_shndx);
1405
989k
        if (sym->symbol.section == NULL)
1406
89.7k
    {
1407
      /* This symbol is in a section for which we did not
1408
         create a BFD section.  Just use bfd_abs_section,
1409
         although it is wrong.  FIXME.  Note - there is
1410
         code in elf.c:swap_out_syms that calls
1411
         symbol_section_index() in the elf backend for
1412
         cases like this.  */
1413
89.7k
      sym->symbol.section = bfd_abs_section_ptr;
1414
89.7k
    }
1415
989k
      }
1416
1417
    /* If this is a relocatable file, then the symbol value is
1418
       already section relative.  */
1419
1.41M
    if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1420
835k
      sym->symbol.value -= sym->symbol.section->vma;
1421
1422
1.41M
    switch (ELF_ST_BIND (isym->st_info))
1423
1.41M
      {
1424
794k
      case STB_LOCAL:
1425
794k
        sym->symbol.flags |= BSF_LOCAL;
1426
794k
        break;
1427
582k
      case STB_GLOBAL:
1428
582k
        if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON)
1429
322k
    sym->symbol.flags |= BSF_GLOBAL;
1430
582k
        break;
1431
31.2k
      case STB_WEAK:
1432
31.2k
        sym->symbol.flags |= BSF_WEAK;
1433
31.2k
        break;
1434
435
      case STB_GNU_UNIQUE:
1435
435
        sym->symbol.flags |= BSF_GNU_UNIQUE;
1436
435
        break;
1437
1.41M
      }
1438
1439
1.41M
    switch (ELF_ST_TYPE (isym->st_info))
1440
1.41M
      {
1441
149k
      case STT_SECTION:
1442
        /* Mark the input section symbol as used since it may be
1443
           used for relocation and section group.
1444
     NB: BSF_SECTION_SYM_USED is ignored by linker and may
1445
     be cleared by objcopy for non-relocatable inputs.  */
1446
149k
        sym->symbol.flags |= (BSF_SECTION_SYM
1447
149k
            | BSF_DEBUGGING
1448
149k
            | BSF_SECTION_SYM_USED);
1449
149k
        break;
1450
63.0k
      case STT_FILE:
1451
63.0k
        sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
1452
63.0k
        break;
1453
521k
      case STT_FUNC:
1454
521k
        sym->symbol.flags |= BSF_FUNCTION;
1455
521k
        break;
1456
2.41k
      case STT_COMMON:
1457
        /* FIXME: Do we have to put the size field into the value field
1458
     as we do with symbols in SHN_COMMON sections (see above) ?  */
1459
2.41k
        sym->symbol.flags |= BSF_ELF_COMMON;
1460
        /* Fall through.  */
1461
342k
      case STT_OBJECT:
1462
342k
        sym->symbol.flags |= BSF_OBJECT;
1463
342k
        break;
1464
3.97k
      case STT_TLS:
1465
3.97k
        sym->symbol.flags |= BSF_THREAD_LOCAL;
1466
3.97k
        break;
1467
4.91k
      case STT_RELC:
1468
4.91k
        sym->symbol.flags |= BSF_RELC;
1469
4.91k
        break;
1470
900
      case STT_SRELC:
1471
900
        sym->symbol.flags |= BSF_SRELC;
1472
900
        break;
1473
2.59k
      case STT_GNU_IFUNC:
1474
2.59k
        sym->symbol.flags |= BSF_GNU_INDIRECT_FUNCTION;
1475
2.59k
        break;
1476
1.41M
      }
1477
1478
1.41M
    if (dynamic)
1479
217k
      sym->symbol.flags |= BSF_DYNAMIC;
1480
1481
1.41M
    if (elf_tdata (abfd)->dt_versym)
1482
0
      sym->version = bfd_get_16 (abfd,
1483
1.41M
               elf_tdata (abfd)->dt_versym + 2 * i);
1484
1.41M
    else if (xver != NULL)
1485
208k
      {
1486
208k
        Elf_Internal_Versym iversym;
1487
1488
208k
        _bfd_elf_swap_versym_in (abfd, xver, &iversym);
1489
208k
        sym->version = iversym.vs_vers;
1490
208k
        xver++;
1491
208k
      }
1492
1493
    /* Do some backend-specific processing on this symbol.  */
1494
1.41M
    if (ebd->elf_backend_symbol_processing)
1495
548k
      (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
1496
1.41M
  }
1497
6.49k
    }
1498
1499
  /* Do some backend-specific processing on this symbol table.  */
1500
7.16k
  if (ebd->elf_backend_symbol_table_processing)
1501
0
    (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
1502
1503
  /* We rely on the zalloc to clear out the final symbol entry.  */
1504
1505
7.16k
  symcount = sym - symbase;
1506
1507
  /* Fill in the user's symbol pointer vector if needed.  */
1508
7.16k
  if (symptrs)
1509
7.16k
    {
1510
7.16k
      long l = symcount;
1511
1512
7.16k
      sym = symbase;
1513
1.42M
      while (l-- > 0)
1514
1.41M
  {
1515
1.41M
    *symptrs++ = &sym->symbol;
1516
1.41M
    sym++;
1517
1.41M
  }
1518
7.16k
      *symptrs = 0;   /* Final null pointer */
1519
7.16k
    }
1520
1521
7.16k
  free (xverbuf);
1522
7.16k
  if (hdr->contents != (unsigned char *) isymbuf
1523
7.16k
      && !elf_use_dt_symtab_p (abfd))
1524
6.48k
    free (isymbuf);
1525
7.16k
  return symcount;
1526
1527
5
 error_return:
1528
5
  free (xverbuf);
1529
5
  if (hdr->contents != (unsigned char *) isymbuf
1530
5
      && !elf_use_dt_symtab_p (abfd))
1531
5
    free (isymbuf);
1532
5
  return -1;
1533
8.23k
}
bfd_elf64_slurp_symbol_table
Line
Count
Source
1240
6.70k
{
1241
6.70k
  Elf_Internal_Shdr *hdr;
1242
6.70k
  Elf_Internal_Shdr *verhdr;
1243
6.70k
  unsigned long symcount; /* Number of external ELF symbols */
1244
6.70k
  elf_symbol_type *sym;   /* Pointer to current bfd symbol */
1245
6.70k
  elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
1246
6.70k
  Elf_Internal_Sym *isym;
1247
6.70k
  Elf_Internal_Sym *isymend;
1248
6.70k
  Elf_Internal_Sym *isymbuf = NULL;
1249
6.70k
  Elf_External_Versym *xver;
1250
6.70k
  Elf_External_Versym *xverbuf = NULL;
1251
6.70k
  const struct elf_backend_data *ebd;
1252
6.70k
  size_t amt;
1253
1254
  /* Read each raw ELF symbol, converting from external ELF form to
1255
     internal ELF form, and then using the information to create a
1256
     canonical bfd symbol table entry.
1257
1258
     Note that we allocate the initial bfd canonical symbol buffer
1259
     based on a one-to-one mapping of the ELF symbols to canonical
1260
     symbols.  We actually use all the ELF symbols, so there will be no
1261
     space left over at the end.  When we have all the symbols, we
1262
     build the caller's pointer vector.  */
1263
6.70k
  ebd = get_elf_backend_data (abfd);
1264
1265
6.70k
  if (! dynamic)
1266
6.32k
    {
1267
6.32k
      hdr = &elf_tdata (abfd)->symtab_hdr;
1268
6.32k
      verhdr = NULL;
1269
6.32k
      symcount = hdr->sh_size / ebd->s->sizeof_sym;
1270
6.32k
    }
1271
381
  else
1272
381
    {
1273
381
      hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1274
381
      if (elf_dynversym (abfd) == 0)
1275
61
  verhdr = NULL;
1276
320
      else
1277
320
  verhdr = &elf_tdata (abfd)->dynversym_hdr;
1278
381
      if ((elf_dynverdef (abfd) != 0
1279
381
     && elf_tdata (abfd)->verdef == NULL)
1280
381
    || (elf_dynverref (abfd) != 0
1281
369
        && elf_tdata (abfd)->verref == NULL)
1282
381
    || elf_tdata (abfd)->dt_verdef != NULL
1283
381
    || elf_tdata (abfd)->dt_verneed != NULL)
1284
130
  {
1285
130
    if (!_bfd_elf_slurp_version_tables (abfd, false))
1286
70
      return -1;
1287
130
  }
1288
1289
311
      symcount = elf_tdata (abfd)->dt_symtab_count;
1290
311
    }
1291
1292
6.63k
  if (symcount == 0)
1293
863
    symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1294
1295
6.63k
  if (symcount == 0)
1296
559
    sym = symbase = NULL;
1297
6.07k
  else
1298
6.07k
    {
1299
6.07k
      size_t i;
1300
1301
6.07k
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
1302
6.07k
              NULL, NULL, NULL);
1303
6.07k
      if (isymbuf == NULL)
1304
744
  return -1;
1305
1306
5.32k
      if (_bfd_mul_overflow (symcount, sizeof (elf_symbol_type), &amt))
1307
0
  {
1308
0
    bfd_set_error (bfd_error_file_too_big);
1309
0
    goto error_return;
1310
0
  }
1311
5.32k
      symbase = (elf_symbol_type *) bfd_zalloc (abfd, amt);
1312
5.32k
      if (symbase == (elf_symbol_type *) NULL)
1313
0
  goto error_return;
1314
1315
      /* Read the raw ELF version symbol information.  */
1316
5.32k
      if (verhdr != NULL
1317
5.32k
    && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
1318
8
  {
1319
8
    _bfd_error_handler
1320
      /* xgettext:c-format */
1321
8
      (_("%pB: version count (%" PRId64 ")"
1322
8
         " does not match symbol count (%ld)"),
1323
8
       abfd,
1324
8
       (int64_t) (verhdr->sh_size / sizeof (Elf_External_Versym)),
1325
8
       symcount);
1326
1327
    /* Slurp in the symbols without the version information,
1328
       since that is more helpful than just quitting.  */
1329
8
    verhdr = NULL;
1330
8
  }
1331
1332
5.32k
      if (verhdr != NULL)
1333
221
  {
1334
221
    if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
1335
3
      goto error_return;
1336
218
    xverbuf = (Elf_External_Versym *)
1337
218
      _bfd_malloc_and_read (abfd, verhdr->sh_size, verhdr->sh_size);
1338
218
    if (xverbuf == NULL && verhdr->sh_size != 0)
1339
2
      goto error_return;
1340
218
  }
1341
1342
      /* Skip first symbol, which is a null dummy.  */
1343
5.32k
      xver = xverbuf;
1344
5.32k
      if (xver != NULL)
1345
216
  ++xver;
1346
5.32k
      isymend = isymbuf + symcount;
1347
5.32k
      for (isym = isymbuf + 1, sym = symbase, i = 1;
1348
664k
     isym < isymend;
1349
658k
     isym++, sym++, i++)
1350
658k
  {
1351
658k
    memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
1352
1353
658k
    sym->symbol.the_bfd = abfd;
1354
658k
    if (elf_use_dt_symtab_p (abfd))
1355
12
      sym->symbol.name = (elf_tdata (abfd)->dt_strtab
1356
12
        + isym->st_name);
1357
658k
    else
1358
658k
      sym->symbol.name = bfd_elf_sym_name (abfd, hdr, isym, NULL);
1359
658k
    sym->symbol.value = isym->st_value;
1360
1361
658k
    if (isym->st_shndx == SHN_UNDEF)
1362
219k
      {
1363
219k
        sym->symbol.section = bfd_und_section_ptr;
1364
219k
      }
1365
439k
    else if (isym->st_shndx == SHN_ABS)
1366
4.32k
      {
1367
4.32k
        sym->symbol.section = bfd_abs_section_ptr;
1368
4.32k
      }
1369
434k
    else if (isym->st_shndx == SHN_COMMON)
1370
38
      {
1371
38
        sym->symbol.section = bfd_com_section_ptr;
1372
38
        if ((abfd->flags & BFD_PLUGIN) != 0)
1373
0
    {
1374
0
      asection *xc = bfd_get_section_by_name (abfd, "COMMON");
1375
1376
0
      if (xc == NULL)
1377
0
        {
1378
0
          flagword flags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
1379
0
          | SEC_EXCLUDE);
1380
0
          xc = bfd_make_section_with_flags (abfd, "COMMON", flags);
1381
0
          if (xc == NULL)
1382
0
      goto error_return;
1383
0
        }
1384
0
      sym->symbol.section = xc;
1385
0
    }
1386
        /* Elf puts the alignment into the `value' field, and
1387
     the size into the `size' field.  BFD wants to see the
1388
     size in the value field, and doesn't care (at the
1389
     moment) about the alignment.  */
1390
38
        sym->symbol.value = isym->st_size;
1391
38
      }
1392
434k
    else if (elf_use_dt_symtab_p (abfd))
1393
1
      {
1394
1
        asection *sec;
1395
1
        sec = _bfd_elf_get_section_from_dynamic_symbol (abfd,
1396
1
                    isym);
1397
1
        if (sec == NULL)
1398
0
    goto error_return;
1399
1
        sym->symbol.section = sec;
1400
1
      }
1401
434k
    else
1402
434k
      {
1403
434k
        sym->symbol.section
1404
434k
    = bfd_section_from_elf_index (abfd, isym->st_shndx);
1405
434k
        if (sym->symbol.section == NULL)
1406
44.0k
    {
1407
      /* This symbol is in a section for which we did not
1408
         create a BFD section.  Just use bfd_abs_section,
1409
         although it is wrong.  FIXME.  Note - there is
1410
         code in elf.c:swap_out_syms that calls
1411
         symbol_section_index() in the elf backend for
1412
         cases like this.  */
1413
44.0k
      sym->symbol.section = bfd_abs_section_ptr;
1414
44.0k
    }
1415
434k
      }
1416
1417
    /* If this is a relocatable file, then the symbol value is
1418
       already section relative.  */
1419
658k
    if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1420
100k
      sym->symbol.value -= sym->symbol.section->vma;
1421
1422
658k
    switch (ELF_ST_BIND (isym->st_info))
1423
658k
      {
1424
415k
      case STB_LOCAL:
1425
415k
        sym->symbol.flags |= BSF_LOCAL;
1426
415k
        break;
1427
222k
      case STB_GLOBAL:
1428
222k
        if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON)
1429
89.3k
    sym->symbol.flags |= BSF_GLOBAL;
1430
222k
        break;
1431
13.8k
      case STB_WEAK:
1432
13.8k
        sym->symbol.flags |= BSF_WEAK;
1433
13.8k
        break;
1434
319
      case STB_GNU_UNIQUE:
1435
319
        sym->symbol.flags |= BSF_GNU_UNIQUE;
1436
319
        break;
1437
658k
      }
1438
1439
658k
    switch (ELF_ST_TYPE (isym->st_info))
1440
658k
      {
1441
120k
      case STT_SECTION:
1442
        /* Mark the input section symbol as used since it may be
1443
           used for relocation and section group.
1444
     NB: BSF_SECTION_SYM_USED is ignored by linker and may
1445
     be cleared by objcopy for non-relocatable inputs.  */
1446
120k
        sym->symbol.flags |= (BSF_SECTION_SYM
1447
120k
            | BSF_DEBUGGING
1448
120k
            | BSF_SECTION_SYM_USED);
1449
120k
        break;
1450
9.29k
      case STT_FILE:
1451
9.29k
        sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
1452
9.29k
        break;
1453
101k
      case STT_FUNC:
1454
101k
        sym->symbol.flags |= BSF_FUNCTION;
1455
101k
        break;
1456
710
      case STT_COMMON:
1457
        /* FIXME: Do we have to put the size field into the value field
1458
     as we do with symbols in SHN_COMMON sections (see above) ?  */
1459
710
        sym->symbol.flags |= BSF_ELF_COMMON;
1460
        /* Fall through.  */
1461
166k
      case STT_OBJECT:
1462
166k
        sym->symbol.flags |= BSF_OBJECT;
1463
166k
        break;
1464
2.76k
      case STT_TLS:
1465
2.76k
        sym->symbol.flags |= BSF_THREAD_LOCAL;
1466
2.76k
        break;
1467
338
      case STT_RELC:
1468
338
        sym->symbol.flags |= BSF_RELC;
1469
338
        break;
1470
833
      case STT_SRELC:
1471
833
        sym->symbol.flags |= BSF_SRELC;
1472
833
        break;
1473
2.30k
      case STT_GNU_IFUNC:
1474
2.30k
        sym->symbol.flags |= BSF_GNU_INDIRECT_FUNCTION;
1475
2.30k
        break;
1476
658k
      }
1477
1478
658k
    if (dynamic)
1479
79.5k
      sym->symbol.flags |= BSF_DYNAMIC;
1480
1481
658k
    if (elf_tdata (abfd)->dt_versym)
1482
0
      sym->version = bfd_get_16 (abfd,
1483
658k
               elf_tdata (abfd)->dt_versym + 2 * i);
1484
658k
    else if (xver != NULL)
1485
78.2k
      {
1486
78.2k
        Elf_Internal_Versym iversym;
1487
1488
78.2k
        _bfd_elf_swap_versym_in (abfd, xver, &iversym);
1489
78.2k
        sym->version = iversym.vs_vers;
1490
78.2k
        xver++;
1491
78.2k
      }
1492
1493
    /* Do some backend-specific processing on this symbol.  */
1494
658k
    if (ebd->elf_backend_symbol_processing)
1495
529k
      (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
1496
658k
  }
1497
5.32k
    }
1498
1499
  /* Do some backend-specific processing on this symbol table.  */
1500
5.88k
  if (ebd->elf_backend_symbol_table_processing)
1501
0
    (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
1502
1503
  /* We rely on the zalloc to clear out the final symbol entry.  */
1504
1505
5.88k
  symcount = sym - symbase;
1506
1507
  /* Fill in the user's symbol pointer vector if needed.  */
1508
5.88k
  if (symptrs)
1509
5.88k
    {
1510
5.88k
      long l = symcount;
1511
1512
5.88k
      sym = symbase;
1513
664k
      while (l-- > 0)
1514
658k
  {
1515
658k
    *symptrs++ = &sym->symbol;
1516
658k
    sym++;
1517
658k
  }
1518
5.88k
      *symptrs = 0;   /* Final null pointer */
1519
5.88k
    }
1520
1521
5.88k
  free (xverbuf);
1522
5.88k
  if (hdr->contents != (unsigned char *) isymbuf
1523
5.88k
      && !elf_use_dt_symtab_p (abfd))
1524
5.32k
    free (isymbuf);
1525
5.88k
  return symcount;
1526
1527
5
 error_return:
1528
5
  free (xverbuf);
1529
5
  if (hdr->contents != (unsigned char *) isymbuf
1530
5
      && !elf_use_dt_symtab_p (abfd))
1531
5
    free (isymbuf);
1532
5
  return -1;
1533
6.63k
}
bfd_elf32_slurp_symbol_table
Line
Count
Source
1240
1.61k
{
1241
1.61k
  Elf_Internal_Shdr *hdr;
1242
1.61k
  Elf_Internal_Shdr *verhdr;
1243
1.61k
  unsigned long symcount; /* Number of external ELF symbols */
1244
1.61k
  elf_symbol_type *sym;   /* Pointer to current bfd symbol */
1245
1.61k
  elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
1246
1.61k
  Elf_Internal_Sym *isym;
1247
1.61k
  Elf_Internal_Sym *isymend;
1248
1.61k
  Elf_Internal_Sym *isymbuf = NULL;
1249
1.61k
  Elf_External_Versym *xver;
1250
1.61k
  Elf_External_Versym *xverbuf = NULL;
1251
1.61k
  const struct elf_backend_data *ebd;
1252
1.61k
  size_t amt;
1253
1254
  /* Read each raw ELF symbol, converting from external ELF form to
1255
     internal ELF form, and then using the information to create a
1256
     canonical bfd symbol table entry.
1257
1258
     Note that we allocate the initial bfd canonical symbol buffer
1259
     based on a one-to-one mapping of the ELF symbols to canonical
1260
     symbols.  We actually use all the ELF symbols, so there will be no
1261
     space left over at the end.  When we have all the symbols, we
1262
     build the caller's pointer vector.  */
1263
1.61k
  ebd = get_elf_backend_data (abfd);
1264
1265
1.61k
  if (! dynamic)
1266
1.46k
    {
1267
1.46k
      hdr = &elf_tdata (abfd)->symtab_hdr;
1268
1.46k
      verhdr = NULL;
1269
1.46k
      symcount = hdr->sh_size / ebd->s->sizeof_sym;
1270
1.46k
    }
1271
150
  else
1272
150
    {
1273
150
      hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1274
150
      if (elf_dynversym (abfd) == 0)
1275
30
  verhdr = NULL;
1276
120
      else
1277
120
  verhdr = &elf_tdata (abfd)->dynversym_hdr;
1278
150
      if ((elf_dynverdef (abfd) != 0
1279
150
     && elf_tdata (abfd)->verdef == NULL)
1280
150
    || (elf_dynverref (abfd) != 0
1281
150
        && elf_tdata (abfd)->verref == NULL)
1282
150
    || elf_tdata (abfd)->dt_verdef != NULL
1283
150
    || elf_tdata (abfd)->dt_verneed != NULL)
1284
27
  {
1285
27
    if (!_bfd_elf_slurp_version_tables (abfd, false))
1286
11
      return -1;
1287
27
  }
1288
1289
139
      symcount = elf_tdata (abfd)->dt_symtab_count;
1290
139
    }
1291
1292
1.59k
  if (symcount == 0)
1293
255
    symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1294
1295
1.59k
  if (symcount == 0)
1296
116
    sym = symbase = NULL;
1297
1.48k
  else
1298
1.48k
    {
1299
1.48k
      size_t i;
1300
1301
1.48k
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
1302
1.48k
              NULL, NULL, NULL);
1303
1.48k
      if (isymbuf == NULL)
1304
317
  return -1;
1305
1306
1.16k
      if (_bfd_mul_overflow (symcount, sizeof (elf_symbol_type), &amt))
1307
0
  {
1308
0
    bfd_set_error (bfd_error_file_too_big);
1309
0
    goto error_return;
1310
0
  }
1311
1.16k
      symbase = (elf_symbol_type *) bfd_zalloc (abfd, amt);
1312
1.16k
      if (symbase == (elf_symbol_type *) NULL)
1313
0
  goto error_return;
1314
1315
      /* Read the raw ELF version symbol information.  */
1316
1.16k
      if (verhdr != NULL
1317
1.16k
    && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
1318
0
  {
1319
0
    _bfd_error_handler
1320
      /* xgettext:c-format */
1321
0
      (_("%pB: version count (%" PRId64 ")"
1322
0
         " does not match symbol count (%ld)"),
1323
0
       abfd,
1324
0
       (int64_t) (verhdr->sh_size / sizeof (Elf_External_Versym)),
1325
0
       symcount);
1326
1327
    /* Slurp in the symbols without the version information,
1328
       since that is more helpful than just quitting.  */
1329
0
    verhdr = NULL;
1330
0
  }
1331
1332
1.16k
      if (verhdr != NULL)
1333
105
  {
1334
105
    if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
1335
0
      goto error_return;
1336
105
    xverbuf = (Elf_External_Versym *)
1337
105
      _bfd_malloc_and_read (abfd, verhdr->sh_size, verhdr->sh_size);
1338
105
    if (xverbuf == NULL && verhdr->sh_size != 0)
1339
0
      goto error_return;
1340
105
  }
1341
1342
      /* Skip first symbol, which is a null dummy.  */
1343
1.16k
      xver = xverbuf;
1344
1.16k
      if (xver != NULL)
1345
105
  ++xver;
1346
1.16k
      isymend = isymbuf + symcount;
1347
1.16k
      for (isym = isymbuf + 1, sym = symbase, i = 1;
1348
761k
     isym < isymend;
1349
760k
     isym++, sym++, i++)
1350
760k
  {
1351
760k
    memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
1352
1353
760k
    sym->symbol.the_bfd = abfd;
1354
760k
    if (elf_use_dt_symtab_p (abfd))
1355
0
      sym->symbol.name = (elf_tdata (abfd)->dt_strtab
1356
0
        + isym->st_name);
1357
760k
    else
1358
760k
      sym->symbol.name = bfd_elf_sym_name (abfd, hdr, isym, NULL);
1359
760k
    sym->symbol.value = isym->st_value;
1360
1361
760k
    if (isym->st_shndx == SHN_UNDEF)
1362
152k
      {
1363
152k
        sym->symbol.section = bfd_und_section_ptr;
1364
152k
      }
1365
608k
    else if (isym->st_shndx == SHN_ABS)
1366
53.6k
      {
1367
53.6k
        sym->symbol.section = bfd_abs_section_ptr;
1368
53.6k
      }
1369
554k
    else if (isym->st_shndx == SHN_COMMON)
1370
4
      {
1371
4
        sym->symbol.section = bfd_com_section_ptr;
1372
4
        if ((abfd->flags & BFD_PLUGIN) != 0)
1373
0
    {
1374
0
      asection *xc = bfd_get_section_by_name (abfd, "COMMON");
1375
1376
0
      if (xc == NULL)
1377
0
        {
1378
0
          flagword flags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
1379
0
          | SEC_EXCLUDE);
1380
0
          xc = bfd_make_section_with_flags (abfd, "COMMON", flags);
1381
0
          if (xc == NULL)
1382
0
      goto error_return;
1383
0
        }
1384
0
      sym->symbol.section = xc;
1385
0
    }
1386
        /* Elf puts the alignment into the `value' field, and
1387
     the size into the `size' field.  BFD wants to see the
1388
     size in the value field, and doesn't care (at the
1389
     moment) about the alignment.  */
1390
4
        sym->symbol.value = isym->st_size;
1391
4
      }
1392
554k
    else if (elf_use_dt_symtab_p (abfd))
1393
0
      {
1394
0
        asection *sec;
1395
0
        sec = _bfd_elf_get_section_from_dynamic_symbol (abfd,
1396
0
                    isym);
1397
0
        if (sec == NULL)
1398
0
    goto error_return;
1399
0
        sym->symbol.section = sec;
1400
0
      }
1401
554k
    else
1402
554k
      {
1403
554k
        sym->symbol.section
1404
554k
    = bfd_section_from_elf_index (abfd, isym->st_shndx);
1405
554k
        if (sym->symbol.section == NULL)
1406
45.6k
    {
1407
      /* This symbol is in a section for which we did not
1408
         create a BFD section.  Just use bfd_abs_section,
1409
         although it is wrong.  FIXME.  Note - there is
1410
         code in elf.c:swap_out_syms that calls
1411
         symbol_section_index() in the elf backend for
1412
         cases like this.  */
1413
45.6k
      sym->symbol.section = bfd_abs_section_ptr;
1414
45.6k
    }
1415
554k
      }
1416
1417
    /* If this is a relocatable file, then the symbol value is
1418
       already section relative.  */
1419
760k
    if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1420
735k
      sym->symbol.value -= sym->symbol.section->vma;
1421
1422
760k
    switch (ELF_ST_BIND (isym->st_info))
1423
760k
      {
1424
379k
      case STB_LOCAL:
1425
379k
        sym->symbol.flags |= BSF_LOCAL;
1426
379k
        break;
1427
360k
      case STB_GLOBAL:
1428
360k
        if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON)
1429
233k
    sym->symbol.flags |= BSF_GLOBAL;
1430
360k
        break;
1431
17.4k
      case STB_WEAK:
1432
17.4k
        sym->symbol.flags |= BSF_WEAK;
1433
17.4k
        break;
1434
116
      case STB_GNU_UNIQUE:
1435
116
        sym->symbol.flags |= BSF_GNU_UNIQUE;
1436
116
        break;
1437
760k
      }
1438
1439
760k
    switch (ELF_ST_TYPE (isym->st_info))
1440
760k
      {
1441
29.0k
      case STT_SECTION:
1442
        /* Mark the input section symbol as used since it may be
1443
           used for relocation and section group.
1444
     NB: BSF_SECTION_SYM_USED is ignored by linker and may
1445
     be cleared by objcopy for non-relocatable inputs.  */
1446
29.0k
        sym->symbol.flags |= (BSF_SECTION_SYM
1447
29.0k
            | BSF_DEBUGGING
1448
29.0k
            | BSF_SECTION_SYM_USED);
1449
29.0k
        break;
1450
53.7k
      case STT_FILE:
1451
53.7k
        sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
1452
53.7k
        break;
1453
420k
      case STT_FUNC:
1454
420k
        sym->symbol.flags |= BSF_FUNCTION;
1455
420k
        break;
1456
1.70k
      case STT_COMMON:
1457
        /* FIXME: Do we have to put the size field into the value field
1458
     as we do with symbols in SHN_COMMON sections (see above) ?  */
1459
1.70k
        sym->symbol.flags |= BSF_ELF_COMMON;
1460
        /* Fall through.  */
1461
176k
      case STT_OBJECT:
1462
176k
        sym->symbol.flags |= BSF_OBJECT;
1463
176k
        break;
1464
1.21k
      case STT_TLS:
1465
1.21k
        sym->symbol.flags |= BSF_THREAD_LOCAL;
1466
1.21k
        break;
1467
4.57k
      case STT_RELC:
1468
4.57k
        sym->symbol.flags |= BSF_RELC;
1469
4.57k
        break;
1470
67
      case STT_SRELC:
1471
67
        sym->symbol.flags |= BSF_SRELC;
1472
67
        break;
1473
288
      case STT_GNU_IFUNC:
1474
288
        sym->symbol.flags |= BSF_GNU_INDIRECT_FUNCTION;
1475
288
        break;
1476
760k
      }
1477
1478
760k
    if (dynamic)
1479
137k
      sym->symbol.flags |= BSF_DYNAMIC;
1480
1481
760k
    if (elf_tdata (abfd)->dt_versym)
1482
0
      sym->version = bfd_get_16 (abfd,
1483
760k
               elf_tdata (abfd)->dt_versym + 2 * i);
1484
760k
    else if (xver != NULL)
1485
130k
      {
1486
130k
        Elf_Internal_Versym iversym;
1487
1488
130k
        _bfd_elf_swap_versym_in (abfd, xver, &iversym);
1489
130k
        sym->version = iversym.vs_vers;
1490
130k
        xver++;
1491
130k
      }
1492
1493
    /* Do some backend-specific processing on this symbol.  */
1494
760k
    if (ebd->elf_backend_symbol_processing)
1495
19.3k
      (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
1496
760k
  }
1497
1.16k
    }
1498
1499
  /* Do some backend-specific processing on this symbol table.  */
1500
1.28k
  if (ebd->elf_backend_symbol_table_processing)
1501
0
    (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
1502
1503
  /* We rely on the zalloc to clear out the final symbol entry.  */
1504
1505
1.28k
  symcount = sym - symbase;
1506
1507
  /* Fill in the user's symbol pointer vector if needed.  */
1508
1.28k
  if (symptrs)
1509
1.28k
    {
1510
1.28k
      long l = symcount;
1511
1512
1.28k
      sym = symbase;
1513
762k
      while (l-- > 0)
1514
760k
  {
1515
760k
    *symptrs++ = &sym->symbol;
1516
760k
    sym++;
1517
760k
  }
1518
1.28k
      *symptrs = 0;   /* Final null pointer */
1519
1.28k
    }
1520
1521
1.28k
  free (xverbuf);
1522
1.28k
  if (hdr->contents != (unsigned char *) isymbuf
1523
1.28k
      && !elf_use_dt_symtab_p (abfd))
1524
1.16k
    free (isymbuf);
1525
1.28k
  return symcount;
1526
1527
0
 error_return:
1528
0
  free (xverbuf);
1529
0
  if (hdr->contents != (unsigned char *) isymbuf
1530
0
      && !elf_use_dt_symtab_p (abfd))
1531
0
    free (isymbuf);
1532
0
  return -1;
1533
1.59k
}
1534
1535
/* Read relocations for ASECT from REL_HDR.  There are RELOC_COUNT of
1536
   them.  */
1537
1538
static bool
1539
elf_slurp_reloc_table_from_section (bfd *abfd,
1540
            asection *asect,
1541
            Elf_Internal_Shdr *rel_hdr,
1542
            bfd_size_type reloc_count,
1543
            arelent *relents,
1544
            asymbol **symbols,
1545
            bool dynamic)
1546
49.9k
{
1547
49.9k
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
1548
49.9k
  void *allocated = NULL;
1549
49.9k
  bfd_byte *native_relocs;
1550
49.9k
  arelent *relent;
1551
49.9k
  unsigned int i;
1552
49.9k
  int entsize;
1553
49.9k
  unsigned int symcount;
1554
1555
49.9k
  if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
1556
120
    return false;
1557
49.7k
  allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
1558
49.7k
  if (allocated == NULL)
1559
179
    return false;
1560
1561
49.6k
  native_relocs = (bfd_byte *) allocated;
1562
1563
49.6k
  entsize = rel_hdr->sh_entsize;
1564
49.6k
  BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
1565
49.6k
        || entsize == sizeof (Elf_External_Rela));
1566
1567
49.6k
  if (dynamic)
1568
657
    symcount = bfd_get_dynamic_symcount (abfd);
1569
48.9k
  else
1570
48.9k
    symcount = bfd_get_symcount (abfd);
1571
1572
49.6k
  for (i = 0, relent = relents;
1573
2.38M
       i < reloc_count;
1574
2.33M
       i++, relent++, native_relocs += entsize)
1575
2.33M
    {
1576
2.33M
      bool res;
1577
2.33M
      Elf_Internal_Rela rela;
1578
1579
2.33M
      if (entsize == sizeof (Elf_External_Rela))
1580
2.26M
  elf_swap_reloca_in (abfd, native_relocs, &rela);
1581
67.2k
      else
1582
67.2k
  elf_swap_reloc_in (abfd, native_relocs, &rela);
1583
1584
      /* The address of an ELF reloc is section relative for an object
1585
   file, and absolute for an executable file or shared library.
1586
   The address of a normal BFD reloc is always section relative,
1587
   and the address of a dynamic reloc is absolute..  */
1588
2.33M
      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
1589
2.31M
  relent->address = rela.r_offset;
1590
21.5k
      else
1591
21.5k
  relent->address = rela.r_offset - asect->vma;
1592
1593
2.33M
      if (ELF_R_SYM (rela.r_info) == STN_UNDEF)
1594
  /* FIXME: This and the error case below mean that we have a
1595
     symbol on relocs that is not elf_symbol_type.  */
1596
86.3k
  relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
1597
2.24M
      else if (ELF_R_SYM (rela.r_info) > symcount)
1598
291k
  {
1599
291k
    _bfd_error_handler
1600
      /* xgettext:c-format */
1601
291k
      (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
1602
291k
       abfd, asect, i, (long) ELF_R_SYM (rela.r_info));
1603
291k
    bfd_set_error (bfd_error_bad_value);
1604
291k
    relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
1605
291k
  }
1606
1.95M
      else
1607
1.95M
  {
1608
1.95M
    asymbol **ps;
1609
1610
1.95M
    ps = symbols + ELF_R_SYM (rela.r_info) - 1;
1611
1612
1.95M
    relent->sym_ptr_ptr = ps;
1613
1.95M
  }
1614
1615
2.33M
      relent->addend = rela.r_addend;
1616
1617
2.33M
      res = false;
1618
2.33M
      if ((entsize == sizeof (Elf_External_Rela)
1619
2.33M
     || ebd->elf_info_to_howto_rel == NULL)
1620
2.33M
    && ebd->elf_info_to_howto != NULL)
1621
2.27M
  res = ebd->elf_info_to_howto (abfd, relent, &rela);
1622
65.0k
      else if (ebd->elf_info_to_howto_rel != NULL)
1623
65.0k
  res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
1624
1625
2.33M
      if (! res || relent->howto == NULL)
1626
4.67k
  goto error_return;
1627
2.33M
    }
1628
1629
44.9k
  free (allocated);
1630
44.9k
  return true;
1631
1632
4.67k
 error_return:
1633
4.67k
  free (allocated);
1634
4.67k
  return false;
1635
49.6k
}
elf64.c:elf_slurp_reloc_table_from_section
Line
Count
Source
1546
49.5k
{
1547
49.5k
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
1548
49.5k
  void *allocated = NULL;
1549
49.5k
  bfd_byte *native_relocs;
1550
49.5k
  arelent *relent;
1551
49.5k
  unsigned int i;
1552
49.5k
  int entsize;
1553
49.5k
  unsigned int symcount;
1554
1555
49.5k
  if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
1556
120
    return false;
1557
49.4k
  allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
1558
49.4k
  if (allocated == NULL)
1559
178
    return false;
1560
1561
49.2k
  native_relocs = (bfd_byte *) allocated;
1562
1563
49.2k
  entsize = rel_hdr->sh_entsize;
1564
49.2k
  BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
1565
49.2k
        || entsize == sizeof (Elf_External_Rela));
1566
1567
49.2k
  if (dynamic)
1568
392
    symcount = bfd_get_dynamic_symcount (abfd);
1569
48.8k
  else
1570
48.8k
    symcount = bfd_get_symcount (abfd);
1571
1572
49.2k
  for (i = 0, relent = relents;
1573
2.30M
       i < reloc_count;
1574
2.25M
       i++, relent++, native_relocs += entsize)
1575
2.25M
    {
1576
2.25M
      bool res;
1577
2.25M
      Elf_Internal_Rela rela;
1578
1579
2.25M
      if (entsize == sizeof (Elf_External_Rela))
1580
2.25M
  elf_swap_reloca_in (abfd, native_relocs, &rela);
1581
0
      else
1582
0
  elf_swap_reloc_in (abfd, native_relocs, &rela);
1583
1584
      /* The address of an ELF reloc is section relative for an object
1585
   file, and absolute for an executable file or shared library.
1586
   The address of a normal BFD reloc is always section relative,
1587
   and the address of a dynamic reloc is absolute..  */
1588
2.25M
      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
1589
2.23M
  relent->address = rela.r_offset;
1590
21.5k
      else
1591
21.5k
  relent->address = rela.r_offset - asect->vma;
1592
1593
2.25M
      if (ELF_R_SYM (rela.r_info) == STN_UNDEF)
1594
  /* FIXME: This and the error case below mean that we have a
1595
     symbol on relocs that is not elf_symbol_type.  */
1596
71.9k
  relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
1597
2.18M
      else if (ELF_R_SYM (rela.r_info) > symcount)
1598
284k
  {
1599
284k
    _bfd_error_handler
1600
      /* xgettext:c-format */
1601
284k
      (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
1602
284k
       abfd, asect, i, (long) ELF_R_SYM (rela.r_info));
1603
284k
    bfd_set_error (bfd_error_bad_value);
1604
284k
    relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
1605
284k
  }
1606
1.89M
      else
1607
1.89M
  {
1608
1.89M
    asymbol **ps;
1609
1610
1.89M
    ps = symbols + ELF_R_SYM (rela.r_info) - 1;
1611
1612
1.89M
    relent->sym_ptr_ptr = ps;
1613
1.89M
  }
1614
1615
2.25M
      relent->addend = rela.r_addend;
1616
1617
2.25M
      res = false;
1618
2.25M
      if ((entsize == sizeof (Elf_External_Rela)
1619
2.25M
     || ebd->elf_info_to_howto_rel == NULL)
1620
2.25M
    && ebd->elf_info_to_howto != NULL)
1621
2.25M
  res = ebd->elf_info_to_howto (abfd, relent, &rela);
1622
0
      else if (ebd->elf_info_to_howto_rel != NULL)
1623
0
  res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
1624
1625
2.25M
      if (! res || relent->howto == NULL)
1626
4.62k
  goto error_return;
1627
2.25M
    }
1628
1629
44.6k
  free (allocated);
1630
44.6k
  return true;
1631
1632
4.62k
 error_return:
1633
4.62k
  free (allocated);
1634
4.62k
  return false;
1635
49.2k
}
elf32.c:elf_slurp_reloc_table_from_section
Line
Count
Source
1546
381
{
1547
381
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
1548
381
  void *allocated = NULL;
1549
381
  bfd_byte *native_relocs;
1550
381
  arelent *relent;
1551
381
  unsigned int i;
1552
381
  int entsize;
1553
381
  unsigned int symcount;
1554
1555
381
  if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
1556
0
    return false;
1557
381
  allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
1558
381
  if (allocated == NULL)
1559
1
    return false;
1560
1561
380
  native_relocs = (bfd_byte *) allocated;
1562
1563
380
  entsize = rel_hdr->sh_entsize;
1564
380
  BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
1565
380
        || entsize == sizeof (Elf_External_Rela));
1566
1567
380
  if (dynamic)
1568
265
    symcount = bfd_get_dynamic_symcount (abfd);
1569
115
  else
1570
115
    symcount = bfd_get_symcount (abfd);
1571
1572
380
  for (i = 0, relent = relents;
1573
80.3k
       i < reloc_count;
1574
79.9k
       i++, relent++, native_relocs += entsize)
1575
80.0k
    {
1576
80.0k
      bool res;
1577
80.0k
      Elf_Internal_Rela rela;
1578
1579
80.0k
      if (entsize == sizeof (Elf_External_Rela))
1580
12.7k
  elf_swap_reloca_in (abfd, native_relocs, &rela);
1581
67.2k
      else
1582
67.2k
  elf_swap_reloc_in (abfd, native_relocs, &rela);
1583
1584
      /* The address of an ELF reloc is section relative for an object
1585
   file, and absolute for an executable file or shared library.
1586
   The address of a normal BFD reloc is always section relative,
1587
   and the address of a dynamic reloc is absolute..  */
1588
80.0k
      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
1589
80.0k
  relent->address = rela.r_offset;
1590
0
      else
1591
0
  relent->address = rela.r_offset - asect->vma;
1592
1593
80.0k
      if (ELF_R_SYM (rela.r_info) == STN_UNDEF)
1594
  /* FIXME: This and the error case below mean that we have a
1595
     symbol on relocs that is not elf_symbol_type.  */
1596
14.3k
  relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
1597
65.6k
      else if (ELF_R_SYM (rela.r_info) > symcount)
1598
6.63k
  {
1599
6.63k
    _bfd_error_handler
1600
      /* xgettext:c-format */
1601
6.63k
      (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
1602
6.63k
       abfd, asect, i, (long) ELF_R_SYM (rela.r_info));
1603
6.63k
    bfd_set_error (bfd_error_bad_value);
1604
6.63k
    relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
1605
6.63k
  }
1606
59.0k
      else
1607
59.0k
  {
1608
59.0k
    asymbol **ps;
1609
1610
59.0k
    ps = symbols + ELF_R_SYM (rela.r_info) - 1;
1611
1612
59.0k
    relent->sym_ptr_ptr = ps;
1613
59.0k
  }
1614
1615
80.0k
      relent->addend = rela.r_addend;
1616
1617
80.0k
      res = false;
1618
80.0k
      if ((entsize == sizeof (Elf_External_Rela)
1619
80.0k
     || ebd->elf_info_to_howto_rel == NULL)
1620
80.0k
    && ebd->elf_info_to_howto != NULL)
1621
15.0k
  res = ebd->elf_info_to_howto (abfd, relent, &rela);
1622
65.0k
      else if (ebd->elf_info_to_howto_rel != NULL)
1623
65.0k
  res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
1624
1625
80.0k
      if (! res || relent->howto == NULL)
1626
54
  goto error_return;
1627
80.0k
    }
1628
1629
326
  free (allocated);
1630
326
  return true;
1631
1632
54
 error_return:
1633
54
  free (allocated);
1634
54
  return false;
1635
380
}
1636
1637
/* Read in and swap the external relocs.  */
1638
1639
bool
1640
elf_slurp_reloc_table (bfd *abfd,
1641
           asection *asect,
1642
           asymbol **symbols,
1643
           bool dynamic)
1644
58.6k
{
1645
58.6k
  const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
1646
58.6k
  struct bfd_elf_section_data * const d = elf_section_data (asect);
1647
58.6k
  Elf_Internal_Shdr *rel_hdr;
1648
58.6k
  Elf_Internal_Shdr *rel_hdr2;
1649
58.6k
  bfd_size_type reloc_count;
1650
58.6k
  bfd_size_type reloc_count2;
1651
58.6k
  arelent *relents;
1652
58.6k
  size_t amt;
1653
1654
58.6k
  if (asect->relocation != NULL)
1655
6.35k
    return true;
1656
1657
52.2k
  if (! dynamic)
1658
51.6k
    {
1659
51.6k
      if ((asect->flags & SEC_RELOC) == 0
1660
51.6k
    || asect->reloc_count == 0)
1661
2.36k
  return true;
1662
1663
49.2k
      rel_hdr = d->rel.hdr;
1664
49.2k
      reloc_count = rel_hdr ? NUM_SHDR_ENTRIES (rel_hdr) : 0;
1665
49.2k
      rel_hdr2 = d->rela.hdr;
1666
49.2k
      reloc_count2 = rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0;
1667
1668
      /* PR 17512: file: 0b4f81b7.  */
1669
49.2k
      if (asect->reloc_count != reloc_count + reloc_count2)
1670
0
  return false;
1671
49.2k
      BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
1672
49.2k
      || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
1673
1674
49.2k
    }
1675
663
  else
1676
663
    {
1677
      /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
1678
   case because relocations against this section may use the
1679
   dynamic symbol table, and in that case bfd_section_from_shdr
1680
   in elf.c does not update the RELOC_COUNT.  */
1681
663
      if (asect->size == 0)
1682
3
  return true;
1683
1684
660
      rel_hdr = &d->this_hdr;
1685
660
      reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
1686
660
      rel_hdr2 = NULL;
1687
660
      reloc_count2 = 0;
1688
660
    }
1689
1690
49.9k
  if (_bfd_mul_overflow (reloc_count + reloc_count2, sizeof (arelent), &amt))
1691
0
    {
1692
0
      bfd_set_error (bfd_error_file_too_big);
1693
0
      return false;
1694
0
    }
1695
49.9k
  relents = (arelent *) bfd_alloc (abfd, amt);
1696
49.9k
  if (relents == NULL)
1697
0
    return false;
1698
1699
49.9k
  if (rel_hdr
1700
49.9k
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1701
776
                rel_hdr, reloc_count,
1702
776
                relents,
1703
776
                symbols, dynamic))
1704
88
    return false;
1705
1706
49.8k
  if (rel_hdr2
1707
49.8k
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1708
49.1k
                rel_hdr2, reloc_count2,
1709
49.1k
                relents + reloc_count,
1710
49.1k
                symbols, dynamic))
1711
4.88k
    return false;
1712
1713
44.9k
  if (!bed->slurp_secondary_relocs (abfd, asect, symbols, dynamic))
1714
40
    return false;
1715
1716
44.8k
  asect->relocation = relents;
1717
44.8k
  return true;
1718
44.9k
}
bfd_elf64_slurp_reloc_table
Line
Count
Source
1644
56.9k
{
1645
56.9k
  const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
1646
56.9k
  struct bfd_elf_section_data * const d = elf_section_data (asect);
1647
56.9k
  Elf_Internal_Shdr *rel_hdr;
1648
56.9k
  Elf_Internal_Shdr *rel_hdr2;
1649
56.9k
  bfd_size_type reloc_count;
1650
56.9k
  bfd_size_type reloc_count2;
1651
56.9k
  arelent *relents;
1652
56.9k
  size_t amt;
1653
1654
56.9k
  if (asect->relocation != NULL)
1655
6.23k
    return true;
1656
1657
50.7k
  if (! dynamic)
1658
50.3k
    {
1659
50.3k
      if ((asect->flags & SEC_RELOC) == 0
1660
50.3k
    || asect->reloc_count == 0)
1661
1.19k
  return true;
1662
1663
49.1k
      rel_hdr = d->rel.hdr;
1664
49.1k
      reloc_count = rel_hdr ? NUM_SHDR_ENTRIES (rel_hdr) : 0;
1665
49.1k
      rel_hdr2 = d->rela.hdr;
1666
49.1k
      reloc_count2 = rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0;
1667
1668
      /* PR 17512: file: 0b4f81b7.  */
1669
49.1k
      if (asect->reloc_count != reloc_count + reloc_count2)
1670
0
  return false;
1671
49.1k
      BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
1672
49.1k
      || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
1673
1674
49.1k
    }
1675
398
  else
1676
398
    {
1677
      /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
1678
   case because relocations against this section may use the
1679
   dynamic symbol table, and in that case bfd_section_from_shdr
1680
   in elf.c does not update the RELOC_COUNT.  */
1681
398
      if (asect->size == 0)
1682
3
  return true;
1683
1684
395
      rel_hdr = &d->this_hdr;
1685
395
      reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
1686
395
      rel_hdr2 = NULL;
1687
395
      reloc_count2 = 0;
1688
395
    }
1689
1690
49.5k
  if (_bfd_mul_overflow (reloc_count + reloc_count2, sizeof (arelent), &amt))
1691
0
    {
1692
0
      bfd_set_error (bfd_error_file_too_big);
1693
0
      return false;
1694
0
    }
1695
49.5k
  relents = (arelent *) bfd_alloc (abfd, amt);
1696
49.5k
  if (relents == NULL)
1697
0
    return false;
1698
1699
49.5k
  if (rel_hdr
1700
49.5k
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1701
395
                rel_hdr, reloc_count,
1702
395
                relents,
1703
395
                symbols, dynamic))
1704
33
    return false;
1705
1706
49.4k
  if (rel_hdr2
1707
49.4k
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1708
49.1k
                rel_hdr2, reloc_count2,
1709
49.1k
                relents + reloc_count,
1710
49.1k
                symbols, dynamic))
1711
4.88k
    return false;
1712
1713
44.6k
  if (!bed->slurp_secondary_relocs (abfd, asect, symbols, dynamic))
1714
40
    return false;
1715
1716
44.5k
  asect->relocation = relents;
1717
44.5k
  return true;
1718
44.6k
}
bfd_elf32_slurp_reloc_table
Line
Count
Source
1644
1.66k
{
1645
1.66k
  const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
1646
1.66k
  struct bfd_elf_section_data * const d = elf_section_data (asect);
1647
1.66k
  Elf_Internal_Shdr *rel_hdr;
1648
1.66k
  Elf_Internal_Shdr *rel_hdr2;
1649
1.66k
  bfd_size_type reloc_count;
1650
1.66k
  bfd_size_type reloc_count2;
1651
1.66k
  arelent *relents;
1652
1.66k
  size_t amt;
1653
1654
1.66k
  if (asect->relocation != NULL)
1655
117
    return true;
1656
1657
1.54k
  if (! dynamic)
1658
1.28k
    {
1659
1.28k
      if ((asect->flags & SEC_RELOC) == 0
1660
1.28k
    || asect->reloc_count == 0)
1661
1.16k
  return true;
1662
1663
116
      rel_hdr = d->rel.hdr;
1664
116
      reloc_count = rel_hdr ? NUM_SHDR_ENTRIES (rel_hdr) : 0;
1665
116
      rel_hdr2 = d->rela.hdr;
1666
116
      reloc_count2 = rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0;
1667
1668
      /* PR 17512: file: 0b4f81b7.  */
1669
116
      if (asect->reloc_count != reloc_count + reloc_count2)
1670
0
  return false;
1671
116
      BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
1672
116
      || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
1673
1674
116
    }
1675
265
  else
1676
265
    {
1677
      /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
1678
   case because relocations against this section may use the
1679
   dynamic symbol table, and in that case bfd_section_from_shdr
1680
   in elf.c does not update the RELOC_COUNT.  */
1681
265
      if (asect->size == 0)
1682
0
  return true;
1683
1684
265
      rel_hdr = &d->this_hdr;
1685
265
      reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
1686
265
      rel_hdr2 = NULL;
1687
265
      reloc_count2 = 0;
1688
265
    }
1689
1690
381
  if (_bfd_mul_overflow (reloc_count + reloc_count2, sizeof (arelent), &amt))
1691
0
    {
1692
0
      bfd_set_error (bfd_error_file_too_big);
1693
0
      return false;
1694
0
    }
1695
381
  relents = (arelent *) bfd_alloc (abfd, amt);
1696
381
  if (relents == NULL)
1697
0
    return false;
1698
1699
381
  if (rel_hdr
1700
381
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1701
381
                rel_hdr, reloc_count,
1702
381
                relents,
1703
381
                symbols, dynamic))
1704
55
    return false;
1705
1706
326
  if (rel_hdr2
1707
326
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1708
0
                rel_hdr2, reloc_count2,
1709
0
                relents + reloc_count,
1710
0
                symbols, dynamic))
1711
0
    return false;
1712
1713
326
  if (!bed->slurp_secondary_relocs (abfd, asect, symbols, dynamic))
1714
0
    return false;
1715
1716
326
  asect->relocation = relents;
1717
326
  return true;
1718
326
}
1719
1720
#if DEBUG & 2
1721
static void
1722
elf_debug_section (int num, Elf_Internal_Shdr *hdr)
1723
{
1724
  fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num,
1725
     hdr->bfd_section != NULL ? hdr->bfd_section->name : "",
1726
     (long) hdr);
1727
  fprintf (stderr,
1728
     "sh_name      = %ld\tsh_type      = %ld\tsh_flags     = %ld\n",
1729
     (long) hdr->sh_name,
1730
     (long) hdr->sh_type,
1731
     (long) hdr->sh_flags);
1732
  fprintf (stderr,
1733
     "sh_addr      = %ld\tsh_offset    = %ld\tsh_size      = %ld\n",
1734
     (long) hdr->sh_addr,
1735
     (long) hdr->sh_offset,
1736
     (long) hdr->sh_size);
1737
  fprintf (stderr,
1738
     "sh_link      = %ld\tsh_info      = %ld\tsh_addralign = %ld\n",
1739
     (long) hdr->sh_link,
1740
     (long) hdr->sh_info,
1741
     (long) hdr->sh_addralign);
1742
  fprintf (stderr, "sh_entsize   = %ld\n",
1743
     (long) hdr->sh_entsize);
1744
  fflush (stderr);
1745
}
1746
#endif
1747
1748
#if DEBUG & 1
1749
static void
1750
elf_debug_file (Elf_Internal_Ehdr *ehdrp)
1751
{
1752
  fprintf (stderr, "e_entry      = 0x%.8lx\n", (long) ehdrp->e_entry);
1753
  fprintf (stderr, "e_phoff      = %ld\n", (long) ehdrp->e_phoff);
1754
  fprintf (stderr, "e_phnum      = %ld\n", (long) ehdrp->e_phnum);
1755
  fprintf (stderr, "e_phentsize  = %ld\n", (long) ehdrp->e_phentsize);
1756
  fprintf (stderr, "e_shoff      = %ld\n", (long) ehdrp->e_shoff);
1757
  fprintf (stderr, "e_shnum      = %ld\n", (long) ehdrp->e_shnum);
1758
  fprintf (stderr, "e_shentsize  = %ld\n", (long) ehdrp->e_shentsize);
1759
}
1760
#endif
1761

1762
/* Create a new BFD as if by bfd_openr.  Rather than opening a file,
1763
   reconstruct an ELF file by reading the segments out of remote
1764
   memory based on the ELF file header at EHDR_VMA and the ELF program
1765
   headers it points to.  If non-zero, SIZE is the known extent of the
1766
   object.  If not null, *LOADBASEP is filled in with the difference
1767
   between the VMAs from which the segments were read, and the VMAs
1768
   the file headers (and hence BFD's idea of each section's VMA) put
1769
   them at.
1770
1771
   The function TARGET_READ_MEMORY is called to copy LEN bytes from
1772
   the remote memory at target address VMA into the local buffer at
1773
   MYADDR; it should return zero on success or an `errno' code on
1774
   failure.  TEMPL must be a BFD for a target with the word size and
1775
   byte order found in the remote memory.  */
1776
1777
bfd *
1778
NAME(_bfd_elf,bfd_from_remote_memory)
1779
  (bfd *templ,
1780
   bfd_vma ehdr_vma    /* Bytes.  */,
1781
   bfd_size_type size  /* Octets.  */,
1782
   bfd_vma *loadbasep  /* Bytes.  */,
1783
   int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
1784
                          /* (Bytes  ,           , octets       ).  */
1785
0
{
1786
0
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
1787
0
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */
1788
0
  Elf_External_Phdr *x_phdrs;
1789
0
  Elf_Internal_Phdr *i_phdrs, *last_phdr, *first_phdr;
1790
0
  bfd *nbfd;
1791
0
  struct bfd_in_memory *bim;
1792
0
  bfd_byte *contents;
1793
0
  int err;
1794
0
  unsigned int i;
1795
0
  bfd_vma high_offset;
1796
0
  bfd_vma shdr_end;
1797
0
  bfd_vma loadbase;  /* Bytes.  */
1798
0
  size_t amt;
1799
0
  unsigned int opb = bfd_octets_per_byte (templ, NULL);
1800
1801
  /* Read in the ELF header in external format.  */
1802
0
  err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr, sizeof x_ehdr);
1803
0
  if (err)
1804
0
    {
1805
0
      bfd_set_error (bfd_error_system_call);
1806
0
      errno = err;
1807
0
      return NULL;
1808
0
    }
1809
1810
  /* Now check to see if we have a valid ELF file, and one that BFD can
1811
     make use of.  The magic number must match, the address size ('class')
1812
     and byte-swapping must match our XVEC entry.  */
1813
1814
0
  if (! elf_file_p (&x_ehdr)
1815
0
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
1816
0
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
1817
0
    {
1818
0
      bfd_set_error (bfd_error_wrong_format);
1819
0
      return NULL;
1820
0
    }
1821
1822
  /* Check that file's byte order matches xvec's */
1823
0
  switch (x_ehdr.e_ident[EI_DATA])
1824
0
    {
1825
0
    case ELFDATA2MSB:   /* Big-endian */
1826
0
      if (! bfd_header_big_endian (templ))
1827
0
  {
1828
0
    bfd_set_error (bfd_error_wrong_format);
1829
0
    return NULL;
1830
0
  }
1831
0
      break;
1832
0
    case ELFDATA2LSB:   /* Little-endian */
1833
0
      if (! bfd_header_little_endian (templ))
1834
0
  {
1835
0
    bfd_set_error (bfd_error_wrong_format);
1836
0
    return NULL;
1837
0
  }
1838
0
      break;
1839
0
    case ELFDATANONE:   /* No data encoding specified */
1840
0
    default:      /* Unknown data encoding specified */
1841
0
      bfd_set_error (bfd_error_wrong_format);
1842
0
      return NULL;
1843
0
    }
1844
1845
0
  elf_swap_ehdr_in (templ, &x_ehdr, &i_ehdr);
1846
1847
  /* The file header tells where to find the program headers.
1848
     These are what we use to actually choose what to read.  */
1849
1850
0
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
1851
0
    {
1852
0
      bfd_set_error (bfd_error_wrong_format);
1853
0
      return NULL;
1854
0
    }
1855
1856
0
  if (_bfd_mul_overflow (i_ehdr.e_phnum,
1857
0
       sizeof (*x_phdrs) + sizeof (*i_phdrs), &amt))
1858
0
    {
1859
0
      bfd_set_error (bfd_error_file_too_big);
1860
0
      return NULL;
1861
0
    }
1862
0
  x_phdrs = (Elf_External_Phdr *) bfd_malloc (amt);
1863
0
  if (x_phdrs == NULL)
1864
0
    return NULL;
1865
0
  err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs,
1866
0
          i_ehdr.e_phnum * sizeof x_phdrs[0]);
1867
0
  if (err)
1868
0
    {
1869
0
      free (x_phdrs);
1870
0
      bfd_set_error (bfd_error_system_call);
1871
0
      errno = err;
1872
0
      return NULL;
1873
0
    }
1874
0
  i_phdrs = (Elf_Internal_Phdr *) &x_phdrs[i_ehdr.e_phnum];
1875
1876
0
  high_offset = 0;
1877
0
  loadbase = 0;
1878
0
  first_phdr = NULL;
1879
0
  last_phdr = NULL;
1880
0
  for (i = 0; i < i_ehdr.e_phnum; ++i)
1881
0
    {
1882
0
      elf_swap_phdr_in (templ, &x_phdrs[i], &i_phdrs[i]);
1883
0
      if (i_phdrs[i].p_type == PT_LOAD)
1884
0
  {
1885
0
    bfd_vma segment_end = i_phdrs[i].p_offset + i_phdrs[i].p_filesz;
1886
1887
0
    if (segment_end > high_offset)
1888
0
      {
1889
0
        high_offset = segment_end;
1890
0
        last_phdr = &i_phdrs[i];
1891
0
      }
1892
1893
    /* If this program header covers offset zero, where the file
1894
       header sits, then we can figure out the loadbase.  */
1895
0
    if (first_phdr == NULL)
1896
0
      {
1897
0
        bfd_vma p_offset = i_phdrs[i].p_offset;  /* Octets.  */
1898
0
        bfd_vma p_vaddr = i_phdrs[i].p_vaddr;    /* Octets.  */
1899
1900
0
        if (i_phdrs[i].p_align > 1)
1901
0
    {
1902
0
      p_offset &= -(i_phdrs[i].p_align * opb);
1903
0
      p_vaddr &= -(i_phdrs[i].p_align * opb);
1904
0
    }
1905
0
        if (p_offset == 0)
1906
0
    {
1907
0
      loadbase = ehdr_vma - p_vaddr / opb;
1908
0
      first_phdr = &i_phdrs[i];
1909
0
    }
1910
0
      }
1911
0
  }
1912
0
    }
1913
0
  if (high_offset == 0)
1914
0
    {
1915
      /* There were no PT_LOAD segments, so we don't have anything to read.  */
1916
0
      free (x_phdrs);
1917
0
      bfd_set_error (bfd_error_wrong_format);
1918
0
      return NULL;
1919
0
    }
1920
1921
0
  shdr_end = 0;
1922
0
  if (i_ehdr.e_shoff != 0 && i_ehdr.e_shnum != 0 && i_ehdr.e_shentsize != 0)
1923
0
    {
1924
0
      shdr_end = i_ehdr.e_shoff + i_ehdr.e_shnum * i_ehdr.e_shentsize;
1925
1926
0
      if (last_phdr->p_filesz != last_phdr->p_memsz)
1927
0
  {
1928
    /* If the last PT_LOAD header has a bss area then ld.so will
1929
       have cleared anything past p_filesz, zapping the section
1930
       headers.  */
1931
0
  }
1932
0
      else if (size >= shdr_end)
1933
0
  high_offset = size;
1934
0
      else
1935
0
  {
1936
0
    bfd_vma page_size = get_elf_backend_data (templ)->minpagesize;
1937
0
    bfd_vma segment_end = last_phdr->p_offset + last_phdr->p_filesz;
1938
1939
    /* Assume we loaded full pages, allowing us to sometimes see
1940
       section headers.  */
1941
0
    if (page_size > 1 && shdr_end > segment_end)
1942
0
      {
1943
0
        bfd_vma page_end = (segment_end + page_size - 1) & -page_size;
1944
1945
0
        if (page_end >= shdr_end)
1946
    /* Whee, section headers covered.  */
1947
0
    high_offset = shdr_end;
1948
0
      }
1949
0
  }
1950
0
    }
1951
1952
  /* Now we know the size of the whole image we want read in.  */
1953
0
  contents = (bfd_byte *) bfd_zmalloc (high_offset);
1954
0
  if (contents == NULL)
1955
0
    {
1956
0
      free (x_phdrs);
1957
0
      return NULL;
1958
0
    }
1959
1960
0
  for (i = 0; i < i_ehdr.e_phnum; ++i)
1961
0
    if (i_phdrs[i].p_type == PT_LOAD)
1962
0
      {
1963
0
  bfd_vma start = i_phdrs[i].p_offset;         /* Octets.  */
1964
0
  bfd_vma end = start + i_phdrs[i].p_filesz;   /* Octets.  */
1965
0
  bfd_vma vaddr = i_phdrs[i].p_vaddr;          /* Octets.  */
1966
1967
  /* Extend the beginning of the first pt_load to cover file
1968
     header and program headers, if we proved earlier that its
1969
     aligned offset is 0.  */
1970
0
  if (first_phdr == &i_phdrs[i])
1971
0
    {
1972
0
      vaddr -= start;
1973
0
      start = 0;
1974
0
    }
1975
  /* Extend the end of the last pt_load to cover section headers.  */
1976
0
  if (last_phdr == &i_phdrs[i])
1977
0
    end = high_offset;
1978
0
  err = target_read_memory (loadbase + vaddr / opb,
1979
0
          contents + start, end - start);
1980
0
  if (err)
1981
0
    {
1982
0
      free (x_phdrs);
1983
0
      free (contents);
1984
0
      bfd_set_error (bfd_error_system_call);
1985
0
      errno = err;
1986
0
      return NULL;
1987
0
    }
1988
0
      }
1989
0
  free (x_phdrs);
1990
1991
  /* If the segments visible in memory didn't include the section headers,
1992
     then clear them from the file header.  */
1993
0
  if (high_offset < shdr_end)
1994
0
    {
1995
0
      memset (&x_ehdr.e_shoff, 0, sizeof x_ehdr.e_shoff);
1996
0
      memset (&x_ehdr.e_shnum, 0, sizeof x_ehdr.e_shnum);
1997
0
      memset (&x_ehdr.e_shstrndx, 0, sizeof x_ehdr.e_shstrndx);
1998
0
    }
1999
2000
  /* This will normally have been in the first PT_LOAD segment.  But it
2001
     conceivably could be missing, and we might have just changed it.  */
2002
0
  memcpy (contents, &x_ehdr, sizeof x_ehdr);
2003
2004
  /* Now we have a memory image of the ELF file contents.  Make a BFD.  */
2005
0
  bim = (struct bfd_in_memory *) bfd_malloc (sizeof (struct bfd_in_memory));
2006
0
  if (bim == NULL)
2007
0
    {
2008
0
      free (contents);
2009
0
      return NULL;
2010
0
    }
2011
0
  nbfd = _bfd_new_bfd ();
2012
0
  if (nbfd == NULL
2013
0
      || !bfd_set_filename (nbfd, "<in-memory>"))
2014
0
    {
2015
0
      free (bim);
2016
0
      free (contents);
2017
0
      return NULL;
2018
0
    }
2019
0
  nbfd->xvec = templ->xvec;
2020
0
  bim->size = high_offset;
2021
0
  bim->buffer = contents;
2022
0
  nbfd->iostream = bim;
2023
0
  nbfd->flags = BFD_IN_MEMORY;
2024
0
  nbfd->iovec = &_bfd_memory_iovec;
2025
0
  nbfd->origin = 0;
2026
0
  nbfd->direction = read_direction;
2027
0
  nbfd->mtime = time (NULL);
2028
0
  nbfd->mtime_set = true;
2029
2030
0
  if (loadbasep)
2031
0
    *loadbasep = loadbase;
2032
0
  return nbfd;
2033
0
}
Unexecuted instantiation: _bfd_elf64_bfd_from_remote_memory
Unexecuted instantiation: _bfd_elf32_bfd_from_remote_memory
2034
2035
/* Function for ELF_R_INFO.  */
2036
2037
bfd_vma
2038
NAME(elf,r_info) (bfd_vma sym, bfd_vma type)
2039
0
{
2040
0
  return ELF_R_INFO (sym, type);
2041
0
}
Unexecuted instantiation: elf64_r_info
Unexecuted instantiation: elf32_r_info
2042
2043
/* Function for ELF_R_SYM.  */
2044
2045
bfd_vma
2046
NAME(elf,r_sym) (bfd_vma r_info)
2047
4.12k
{
2048
4.12k
  return ELF_R_SYM (r_info);
2049
4.12k
}
elf64_r_sym
Line
Count
Source
2047
4.12k
{
2048
4.12k
  return ELF_R_SYM (r_info);
2049
4.12k
}
elf32_r_sym
Line
Count
Source
2047
1
{
2048
1
  return ELF_R_SYM (r_info);
2049
1
}
2050

2051
#include "elfcore.h"
2052

2053
/* Size-dependent data and functions.  */
2054
const struct elf_size_info NAME(_bfd_elf,size_info) = {
2055
  sizeof (Elf_External_Ehdr),
2056
  sizeof (Elf_External_Phdr),
2057
  sizeof (Elf_External_Shdr),
2058
  sizeof (Elf_External_Rel),
2059
  sizeof (Elf_External_Rela),
2060
  sizeof (Elf_External_Sym),
2061
  sizeof (Elf_External_Dyn),
2062
  sizeof (Elf_External_Note),
2063
  4,
2064
  1,
2065
  ARCH_SIZE, LOG_FILE_ALIGN,
2066
  ELFCLASS, EV_CURRENT,
2067
  elf_write_out_phdrs,
2068
  elf_write_shdrs_and_ehdr,
2069
  elf_checksum_contents,
2070
  elf_write_relocs,
2071
  elf_swap_symbol_in,
2072
  elf_swap_symbol_out,
2073
  elf_slurp_reloc_table,
2074
  elf_slurp_symbol_table,
2075
  elf_swap_dyn_in,
2076
  elf_swap_dyn_out,
2077
  elf_swap_reloc_in,
2078
  elf_swap_reloc_out,
2079
  elf_swap_reloca_in,
2080
  elf_swap_reloca_out
2081
};