Coverage Report

Created: 2024-05-21 06:29

/src/binutils-gdb/bfd/elfcode.h
Line
Count
Source (jump to first uncovered line)
1
/* ELF executable support for BFD.
2
   Copyright (C) 1991-2024 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
304M
#define Elf_External_Ehdr NAME(Elf,External_Ehdr)
78
6.54k
#define Elf_External_Sym  NAME(Elf,External_Sym)
79
302M
#define Elf_External_Shdr NAME(Elf,External_Shdr)
80
64.0M
#define Elf_External_Phdr NAME(Elf,External_Phdr)
81
0
#define Elf_External_Rel  NAME(Elf,External_Rel)
82
57.2k
#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
16.4k
#define elf_swap_reloc_in   NAME(bfd_elf,swap_reloc_in)
96
730k
#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
509
#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
63.9M
#define elf_swap_phdr_in    NAME(bfd_elf,swap_phdr_in)
102
50
#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
57.2k
#define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
131
2.14M
#define ELF_R_SYM(X)  ELF64_R_SYM(X)
132
#define ELF_R_TYPE(X) ELF64_R_TYPE(X)
133
11.7M
#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
60.2k
#define ELF_R_SYM(X)  ELF32_R_SYM(X)
140
#define ELF_R_TYPE(X) ELF32_R_TYPE(X)
141
40.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
129k
#define H_PUT_WORD    H_PUT_64
159
57.2k
#define H_PUT_SIGNED_WORD H_PUT_S64
160
101M
#define H_GET_WORD    H_GET_64
161
20.0M
#define H_GET_SIGNED_WORD H_GET_S64
162
#endif
163
#if ARCH_SIZE == 32
164
8.58k
#define H_PUT_WORD    H_PUT_32
165
12
#define H_PUT_SIGNED_WORD H_PUT_S32
166
333M
#define H_GET_WORD    H_GET_32
167
2.39M
#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
680k
{
179
680k
  const Elf_External_Sym *src = (const Elf_External_Sym *) psrc;
180
680k
  const Elf_External_Sym_Shndx *shndx = (const Elf_External_Sym_Shndx *) pshn;
181
680k
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
182
183
680k
  dst->st_name = H_GET_32 (abfd, src->st_name);
184
680k
  if (signed_vma)
185
27.2k
    dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value);
186
653k
  else
187
653k
    dst->st_value = H_GET_WORD (abfd, src->st_value);
188
680k
  dst->st_size = H_GET_WORD (abfd, src->st_size);
189
680k
  dst->st_info = H_GET_8 (abfd, src->st_info);
190
680k
  dst->st_other = H_GET_8 (abfd, src->st_other);
191
680k
  dst->st_shndx = H_GET_16 (abfd, src->st_shndx);
192
680k
  if (dst->st_shndx == (SHN_XINDEX & 0xffff))
193
10.3k
    {
194
10.3k
      if (shndx == NULL)
195
1.20k
  return false;
196
9.14k
      dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx);
197
9.14k
    }
198
670k
  else if (dst->st_shndx >= (SHN_LORESERVE & 0xffff))
199
14.9k
    dst->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
200
679k
  dst->st_target_internal = 0;
201
679k
  return true;
202
680k
}
bfd_elf64_swap_symbol_in
Line
Count
Source
178
482k
{
179
482k
  const Elf_External_Sym *src = (const Elf_External_Sym *) psrc;
180
482k
  const Elf_External_Sym_Shndx *shndx = (const Elf_External_Sym_Shndx *) pshn;
181
482k
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
182
183
482k
  dst->st_name = H_GET_32 (abfd, src->st_name);
184
482k
  if (signed_vma)
185
17.1k
    dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value);
186
465k
  else
187
465k
    dst->st_value = H_GET_WORD (abfd, src->st_value);
188
482k
  dst->st_size = H_GET_WORD (abfd, src->st_size);
189
482k
  dst->st_info = H_GET_8 (abfd, src->st_info);
190
482k
  dst->st_other = H_GET_8 (abfd, src->st_other);
191
482k
  dst->st_shndx = H_GET_16 (abfd, src->st_shndx);
192
482k
  if (dst->st_shndx == (SHN_XINDEX & 0xffff))
193
10.2k
    {
194
10.2k
      if (shndx == NULL)
195
1.14k
  return false;
196
9.14k
      dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx);
197
9.14k
    }
198
472k
  else if (dst->st_shndx >= (SHN_LORESERVE & 0xffff))
199
7.33k
    dst->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
200
481k
  dst->st_target_internal = 0;
201
481k
  return true;
202
482k
}
bfd_elf32_swap_symbol_in
Line
Count
Source
178
198k
{
179
198k
  const Elf_External_Sym *src = (const Elf_External_Sym *) psrc;
180
198k
  const Elf_External_Sym_Shndx *shndx = (const Elf_External_Sym_Shndx *) pshn;
181
198k
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
182
183
198k
  dst->st_name = H_GET_32 (abfd, src->st_name);
184
198k
  if (signed_vma)
185
10.1k
    dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value);
186
188k
  else
187
188k
    dst->st_value = H_GET_WORD (abfd, src->st_value);
188
198k
  dst->st_size = H_GET_WORD (abfd, src->st_size);
189
198k
  dst->st_info = H_GET_8 (abfd, src->st_info);
190
198k
  dst->st_other = H_GET_8 (abfd, src->st_other);
191
198k
  dst->st_shndx = H_GET_16 (abfd, src->st_shndx);
192
198k
  if (dst->st_shndx == (SHN_XINDEX & 0xffff))
193
65
    {
194
65
      if (shndx == NULL)
195
59
  return false;
196
6
      dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx);
197
6
    }
198
198k
  else if (dst->st_shndx >= (SHN_LORESERVE & 0xffff))
199
7.63k
    dst->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
200
198k
  dst->st_target_internal = 0;
201
198k
  return true;
202
198k
}
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
6.54k
{
213
6.54k
  unsigned int tmp;
214
6.54k
  Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
215
6.54k
  H_PUT_32 (abfd, src->st_name, dst->st_name);
216
6.54k
  H_PUT_WORD (abfd, src->st_value, dst->st_value);
217
6.54k
  H_PUT_WORD (abfd, src->st_size, dst->st_size);
218
6.54k
  H_PUT_8 (abfd, src->st_info, dst->st_info);
219
6.54k
  H_PUT_8 (abfd, src->st_other, dst->st_other);
220
6.54k
  tmp = src->st_shndx;
221
6.54k
  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
6.54k
  H_PUT_16 (abfd, tmp, dst->st_shndx);
229
6.54k
}
bfd_elf64_swap_symbol_out
Line
Count
Source
212
3.01k
{
213
3.01k
  unsigned int tmp;
214
3.01k
  Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
215
3.01k
  H_PUT_32 (abfd, src->st_name, dst->st_name);
216
3.01k
  H_PUT_WORD (abfd, src->st_value, dst->st_value);
217
3.01k
  H_PUT_WORD (abfd, src->st_size, dst->st_size);
218
3.01k
  H_PUT_8 (abfd, src->st_info, dst->st_info);
219
3.01k
  H_PUT_8 (abfd, src->st_other, dst->st_other);
220
3.01k
  tmp = src->st_shndx;
221
3.01k
  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
3.01k
  H_PUT_16 (abfd, tmp, dst->st_shndx);
229
3.01k
}
bfd_elf32_swap_symbol_out
Line
Count
Source
212
3.53k
{
213
3.53k
  unsigned int tmp;
214
3.53k
  Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
215
3.53k
  H_PUT_32 (abfd, src->st_name, dst->st_name);
216
3.53k
  H_PUT_WORD (abfd, src->st_value, dst->st_value);
217
3.53k
  H_PUT_WORD (abfd, src->st_size, dst->st_size);
218
3.53k
  H_PUT_8 (abfd, src->st_info, dst->st_info);
219
3.53k
  H_PUT_8 (abfd, src->st_other, dst->st_other);
220
3.53k
  tmp = src->st_shndx;
221
3.53k
  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
3.53k
  H_PUT_16 (abfd, tmp, dst->st_shndx);
229
3.53k
}
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
14.3M
{
239
14.3M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
240
14.3M
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
241
14.3M
  dst->e_type = H_GET_16 (abfd, src->e_type);
242
14.3M
  dst->e_machine = H_GET_16 (abfd, src->e_machine);
243
14.3M
  dst->e_version = H_GET_32 (abfd, src->e_version);
244
14.3M
  if (signed_vma)
245
1.57M
    dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry);
246
12.7M
  else
247
12.7M
    dst->e_entry = H_GET_WORD (abfd, src->e_entry);
248
14.3M
  dst->e_phoff = H_GET_WORD (abfd, src->e_phoff);
249
14.3M
  dst->e_shoff = H_GET_WORD (abfd, src->e_shoff);
250
14.3M
  dst->e_flags = H_GET_32 (abfd, src->e_flags);
251
14.3M
  dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize);
252
14.3M
  dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize);
253
14.3M
  dst->e_phnum = H_GET_16 (abfd, src->e_phnum);
254
14.3M
  dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize);
255
14.3M
  dst->e_shnum = H_GET_16 (abfd, src->e_shnum);
256
14.3M
  dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx);
257
14.3M
}
elf64.c:elf_swap_ehdr_in
Line
Count
Source
238
3.25M
{
239
3.25M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
240
3.25M
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
241
3.25M
  dst->e_type = H_GET_16 (abfd, src->e_type);
242
3.25M
  dst->e_machine = H_GET_16 (abfd, src->e_machine);
243
3.25M
  dst->e_version = H_GET_32 (abfd, src->e_version);
244
3.25M
  if (signed_vma)
245
419k
    dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry);
246
2.83M
  else
247
2.83M
    dst->e_entry = H_GET_WORD (abfd, src->e_entry);
248
3.25M
  dst->e_phoff = H_GET_WORD (abfd, src->e_phoff);
249
3.25M
  dst->e_shoff = H_GET_WORD (abfd, src->e_shoff);
250
3.25M
  dst->e_flags = H_GET_32 (abfd, src->e_flags);
251
3.25M
  dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize);
252
3.25M
  dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize);
253
3.25M
  dst->e_phnum = H_GET_16 (abfd, src->e_phnum);
254
3.25M
  dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize);
255
3.25M
  dst->e_shnum = H_GET_16 (abfd, src->e_shnum);
256
3.25M
  dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx);
257
3.25M
}
elf32.c:elf_swap_ehdr_in
Line
Count
Source
238
11.0M
{
239
11.0M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
240
11.0M
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
241
11.0M
  dst->e_type = H_GET_16 (abfd, src->e_type);
242
11.0M
  dst->e_machine = H_GET_16 (abfd, src->e_machine);
243
11.0M
  dst->e_version = H_GET_32 (abfd, src->e_version);
244
11.0M
  if (signed_vma)
245
1.15M
    dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry);
246
9.93M
  else
247
9.93M
    dst->e_entry = H_GET_WORD (abfd, src->e_entry);
248
11.0M
  dst->e_phoff = H_GET_WORD (abfd, src->e_phoff);
249
11.0M
  dst->e_shoff = H_GET_WORD (abfd, src->e_shoff);
250
11.0M
  dst->e_flags = H_GET_32 (abfd, src->e_flags);
251
11.0M
  dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize);
252
11.0M
  dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize);
253
11.0M
  dst->e_phnum = H_GET_16 (abfd, src->e_phnum);
254
11.0M
  dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize);
255
11.0M
  dst->e_shnum = H_GET_16 (abfd, src->e_shnum);
256
11.0M
  dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx);
257
11.0M
}
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
27
{
267
27
  unsigned int tmp;
268
27
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
269
27
  bool no_section_header = (abfd->flags & BFD_NO_SECTION_HEADER) != 0;
270
27
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
271
  /* note that all elements of dst are *arrays of unsigned char* already...  */
272
27
  H_PUT_16 (abfd, src->e_type, dst->e_type);
273
27
  H_PUT_16 (abfd, src->e_machine, dst->e_machine);
274
27
  H_PUT_32 (abfd, src->e_version, dst->e_version);
275
27
  if (signed_vma)
276
27
    H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
277
26
  else
278
27
    H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
279
27
  H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
280
27
  if (no_section_header)
281
27
    H_PUT_WORD (abfd, 0, dst->e_shoff);
282
27
  else
283
27
    H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
284
27
  H_PUT_32 (abfd, src->e_flags, dst->e_flags);
285
27
  H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
286
27
  H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
287
27
  tmp = src->e_phnum;
288
27
  if (tmp > PN_XNUM)
289
0
    tmp = PN_XNUM;
290
27
  H_PUT_16 (abfd, tmp, dst->e_phnum);
291
27
  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
27
  else
298
27
    {
299
27
      H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
300
27
      tmp = src->e_shnum;
301
27
      if (tmp >= (SHN_LORESERVE & 0xffff))
302
0
  tmp = SHN_UNDEF;
303
27
      H_PUT_16 (abfd, tmp, dst->e_shnum);
304
27
      tmp = src->e_shstrndx;
305
27
      if (tmp >= (SHN_LORESERVE & 0xffff))
306
0
  tmp = SHN_XINDEX & 0xffff;
307
27
      H_PUT_16 (abfd, tmp, dst->e_shstrndx);
308
27
    }
309
27
}
elf64.c:elf_swap_ehdr_out
Line
Count
Source
266
15
{
267
15
  unsigned int tmp;
268
15
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
269
15
  bool no_section_header = (abfd->flags & BFD_NO_SECTION_HEADER) != 0;
270
15
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
271
  /* note that all elements of dst are *arrays of unsigned char* already...  */
272
15
  H_PUT_16 (abfd, src->e_type, dst->e_type);
273
15
  H_PUT_16 (abfd, src->e_machine, dst->e_machine);
274
15
  H_PUT_32 (abfd, src->e_version, dst->e_version);
275
15
  if (signed_vma)
276
15
    H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
277
15
  else
278
15
    H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
279
15
  H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
280
15
  if (no_section_header)
281
15
    H_PUT_WORD (abfd, 0, dst->e_shoff);
282
15
  else
283
15
    H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
284
15
  H_PUT_32 (abfd, src->e_flags, dst->e_flags);
285
15
  H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
286
15
  H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
287
15
  tmp = src->e_phnum;
288
15
  if (tmp > PN_XNUM)
289
0
    tmp = PN_XNUM;
290
15
  H_PUT_16 (abfd, tmp, dst->e_phnum);
291
15
  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
15
  else
298
15
    {
299
15
      H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
300
15
      tmp = src->e_shnum;
301
15
      if (tmp >= (SHN_LORESERVE & 0xffff))
302
0
  tmp = SHN_UNDEF;
303
15
      H_PUT_16 (abfd, tmp, dst->e_shnum);
304
15
      tmp = src->e_shstrndx;
305
15
      if (tmp >= (SHN_LORESERVE & 0xffff))
306
0
  tmp = SHN_XINDEX & 0xffff;
307
15
      H_PUT_16 (abfd, tmp, dst->e_shstrndx);
308
15
    }
309
15
}
elf32.c:elf_swap_ehdr_out
Line
Count
Source
266
12
{
267
12
  unsigned int tmp;
268
12
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
269
12
  bool no_section_header = (abfd->flags & BFD_NO_SECTION_HEADER) != 0;
270
12
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
271
  /* note that all elements of dst are *arrays of unsigned char* already...  */
272
12
  H_PUT_16 (abfd, src->e_type, dst->e_type);
273
12
  H_PUT_16 (abfd, src->e_machine, dst->e_machine);
274
12
  H_PUT_32 (abfd, src->e_version, dst->e_version);
275
12
  if (signed_vma)
276
12
    H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
277
11
  else
278
12
    H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
279
12
  H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
280
12
  if (no_section_header)
281
12
    H_PUT_WORD (abfd, 0, dst->e_shoff);
282
12
  else
283
12
    H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
284
12
  H_PUT_32 (abfd, src->e_flags, dst->e_flags);
285
12
  H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
286
12
  H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
287
12
  tmp = src->e_phnum;
288
12
  if (tmp > PN_XNUM)
289
0
    tmp = PN_XNUM;
290
12
  H_PUT_16 (abfd, tmp, dst->e_phnum);
291
12
  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
12
  else
298
12
    {
299
12
      H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
300
12
      tmp = src->e_shnum;
301
12
      if (tmp >= (SHN_LORESERVE & 0xffff))
302
0
  tmp = SHN_UNDEF;
303
12
      H_PUT_16 (abfd, tmp, dst->e_shnum);
304
12
      tmp = src->e_shstrndx;
305
12
      if (tmp >= (SHN_LORESERVE & 0xffff))
306
0
  tmp = SHN_XINDEX & 0xffff;
307
12
      H_PUT_16 (abfd, tmp, dst->e_shstrndx);
308
12
    }
309
12
}
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
4.47M
{
319
4.47M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
320
321
4.47M
  dst->sh_name = H_GET_32 (abfd, src->sh_name);
322
4.47M
  dst->sh_type = H_GET_32 (abfd, src->sh_type);
323
4.47M
  dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
324
4.47M
  if (signed_vma)
325
1.02M
    dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
326
3.45M
  else
327
3.45M
    dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
328
4.47M
  dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
329
4.47M
  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
4.47M
  if (dst->sh_type != SHT_NOBITS)
334
4.40M
    {
335
4.40M
      ufile_ptr filesize = bfd_get_file_size (abfd);
336
337
4.40M
      if (filesize != 0
338
4.40M
    && ((ufile_ptr) dst->sh_offset > filesize
339
4.40M
        || dst->sh_size > filesize - dst->sh_offset)
340
4.40M
    && !abfd->read_only)
341
490k
  {
342
490k
    _bfd_error_handler (_("warning: %pB has a section "
343
490k
        "extending past end of file"), abfd);
344
490k
    abfd->read_only = 1;
345
490k
  }
346
4.40M
    }
347
4.47M
  dst->sh_link = H_GET_32 (abfd, src->sh_link);
348
4.47M
  dst->sh_info = H_GET_32 (abfd, src->sh_info);
349
4.47M
  dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
350
4.47M
  dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
351
4.47M
  dst->bfd_section = NULL;
352
4.47M
  dst->contents = NULL;
353
4.47M
}
elf64.c:elf_swap_shdr_in
Line
Count
Source
318
1.69M
{
319
1.69M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
320
321
1.69M
  dst->sh_name = H_GET_32 (abfd, src->sh_name);
322
1.69M
  dst->sh_type = H_GET_32 (abfd, src->sh_type);
323
1.69M
  dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
324
1.69M
  if (signed_vma)
325
146k
    dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
326
1.55M
  else
327
1.55M
    dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
328
1.69M
  dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
329
1.69M
  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
1.69M
  if (dst->sh_type != SHT_NOBITS)
334
1.67M
    {
335
1.67M
      ufile_ptr filesize = bfd_get_file_size (abfd);
336
337
1.67M
      if (filesize != 0
338
1.67M
    && ((ufile_ptr) dst->sh_offset > filesize
339
1.67M
        || dst->sh_size > filesize - dst->sh_offset)
340
1.67M
    && !abfd->read_only)
341
206k
  {
342
206k
    _bfd_error_handler (_("warning: %pB has a section "
343
206k
        "extending past end of file"), abfd);
344
206k
    abfd->read_only = 1;
345
206k
  }
346
1.67M
    }
347
1.69M
  dst->sh_link = H_GET_32 (abfd, src->sh_link);
348
1.69M
  dst->sh_info = H_GET_32 (abfd, src->sh_info);
349
1.69M
  dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
350
1.69M
  dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
351
1.69M
  dst->bfd_section = NULL;
352
1.69M
  dst->contents = NULL;
353
1.69M
}
elf32.c:elf_swap_shdr_in
Line
Count
Source
318
2.78M
{
319
2.78M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
320
321
2.78M
  dst->sh_name = H_GET_32 (abfd, src->sh_name);
322
2.78M
  dst->sh_type = H_GET_32 (abfd, src->sh_type);
323
2.78M
  dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
324
2.78M
  if (signed_vma)
325
877k
    dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
326
1.90M
  else
327
1.90M
    dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
328
2.78M
  dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
329
2.78M
  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
2.78M
  if (dst->sh_type != SHT_NOBITS)
334
2.72M
    {
335
2.72M
      ufile_ptr filesize = bfd_get_file_size (abfd);
336
337
2.72M
      if (filesize != 0
338
2.72M
    && ((ufile_ptr) dst->sh_offset > filesize
339
2.72M
        || dst->sh_size > filesize - dst->sh_offset)
340
2.72M
    && !abfd->read_only)
341
284k
  {
342
284k
    _bfd_error_handler (_("warning: %pB has a section "
343
284k
        "extending past end of file"), abfd);
344
284k
    abfd->read_only = 1;
345
284k
  }
346
2.72M
    }
347
2.78M
  dst->sh_link = H_GET_32 (abfd, src->sh_link);
348
2.78M
  dst->sh_info = H_GET_32 (abfd, src->sh_info);
349
2.78M
  dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
350
2.78M
  dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
351
2.78M
  dst->bfd_section = NULL;
352
2.78M
  dst->contents = NULL;
353
2.78M
}
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
1.73k
{
363
  /* note that all elements of dst are *arrays of unsigned char* already...  */
364
1.73k
  H_PUT_32 (abfd, src->sh_name, dst->sh_name);
365
1.73k
  H_PUT_32 (abfd, src->sh_type, dst->sh_type);
366
1.73k
  H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
367
1.73k
  H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
368
1.73k
  H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
369
1.73k
  H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
370
1.73k
  H_PUT_32 (abfd, src->sh_link, dst->sh_link);
371
1.73k
  H_PUT_32 (abfd, src->sh_info, dst->sh_info);
372
1.73k
  H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
373
1.73k
  H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
374
1.73k
}
elf64.c:elf_swap_shdr_out
Line
Count
Source
362
1.53k
{
363
  /* note that all elements of dst are *arrays of unsigned char* already...  */
364
1.53k
  H_PUT_32 (abfd, src->sh_name, dst->sh_name);
365
1.53k
  H_PUT_32 (abfd, src->sh_type, dst->sh_type);
366
1.53k
  H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
367
1.53k
  H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
368
1.53k
  H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
369
1.53k
  H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
370
1.53k
  H_PUT_32 (abfd, src->sh_link, dst->sh_link);
371
1.53k
  H_PUT_32 (abfd, src->sh_info, dst->sh_info);
372
1.53k
  H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
373
1.53k
  H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
374
1.53k
}
elf32.c:elf_swap_shdr_out
Line
Count
Source
362
196
{
363
  /* note that all elements of dst are *arrays of unsigned char* already...  */
364
196
  H_PUT_32 (abfd, src->sh_name, dst->sh_name);
365
196
  H_PUT_32 (abfd, src->sh_type, dst->sh_type);
366
196
  H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
367
196
  H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
368
196
  H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
369
196
  H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
370
196
  H_PUT_32 (abfd, src->sh_link, dst->sh_link);
371
196
  H_PUT_32 (abfd, src->sh_info, dst->sh_info);
372
196
  H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
373
196
  H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
374
196
}
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
63.9M
{
384
63.9M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
385
386
63.9M
  dst->p_type = H_GET_32 (abfd, src->p_type);
387
63.9M
  dst->p_flags = H_GET_32 (abfd, src->p_flags);
388
63.9M
  dst->p_offset = H_GET_WORD (abfd, src->p_offset);
389
63.9M
  if (signed_vma)
390
9.55M
    {
391
9.55M
      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
392
9.55M
      dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
393
9.55M
    }
394
54.4M
  else
395
54.4M
    {
396
54.4M
      dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
397
54.4M
      dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
398
54.4M
    }
399
63.9M
  dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
400
63.9M
  dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
401
63.9M
  dst->p_align = H_GET_WORD (abfd, src->p_align);
402
63.9M
}
bfd_elf64_swap_phdr_in
Line
Count
Source
383
16.4M
{
384
16.4M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
385
386
16.4M
  dst->p_type = H_GET_32 (abfd, src->p_type);
387
16.4M
  dst->p_flags = H_GET_32 (abfd, src->p_flags);
388
16.4M
  dst->p_offset = H_GET_WORD (abfd, src->p_offset);
389
16.4M
  if (signed_vma)
390
9.37M
    {
391
9.37M
      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
392
9.37M
      dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
393
9.37M
    }
394
7.05M
  else
395
7.05M
    {
396
7.05M
      dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
397
7.05M
      dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
398
7.05M
    }
399
16.4M
  dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
400
16.4M
  dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
401
16.4M
  dst->p_align = H_GET_WORD (abfd, src->p_align);
402
16.4M
}
bfd_elf32_swap_phdr_in
Line
Count
Source
383
47.5M
{
384
47.5M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
385
386
47.5M
  dst->p_type = H_GET_32 (abfd, src->p_type);
387
47.5M
  dst->p_flags = H_GET_32 (abfd, src->p_flags);
388
47.5M
  dst->p_offset = H_GET_WORD (abfd, src->p_offset);
389
47.5M
  if (signed_vma)
390
170k
    {
391
170k
      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
392
170k
      dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
393
170k
    }
394
47.3M
  else
395
47.3M
    {
396
47.3M
      dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
397
47.3M
      dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
398
47.3M
    }
399
47.5M
  dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
400
47.5M
  dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
401
47.5M
  dst->p_align = H_GET_WORD (abfd, src->p_align);
402
47.5M
}
403
404
void
405
elf_swap_phdr_out (bfd *abfd,
406
       const Elf_Internal_Phdr *src,
407
       Elf_External_Phdr *dst)
408
50
{
409
50
  const struct elf_backend_data *bed;
410
50
  bfd_vma p_paddr;
411
412
50
  bed = get_elf_backend_data (abfd);
413
50
  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
50
  H_PUT_32 (abfd, src->p_type, dst->p_type);
417
50
  H_PUT_WORD (abfd, src->p_offset, dst->p_offset);
418
50
  H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr);
419
50
  H_PUT_WORD (abfd, p_paddr, dst->p_paddr);
420
50
  H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz);
421
50
  H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz);
422
50
  H_PUT_32 (abfd, src->p_flags, dst->p_flags);
423
50
  H_PUT_WORD (abfd, src->p_align, dst->p_align);
424
50
}
Unexecuted instantiation: bfd_elf64_swap_phdr_out
bfd_elf32_swap_phdr_out
Line
Count
Source
408
50
{
409
50
  const struct elf_backend_data *bed;
410
50
  bfd_vma p_paddr;
411
412
50
  bed = get_elf_backend_data (abfd);
413
50
  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
50
  H_PUT_32 (abfd, src->p_type, dst->p_type);
417
50
  H_PUT_WORD (abfd, src->p_offset, dst->p_offset);
418
50
  H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr);
419
50
  H_PUT_WORD (abfd, p_paddr, dst->p_paddr);
420
50
  H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz);
421
50
  H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz);
422
50
  H_PUT_32 (abfd, src->p_flags, dst->p_flags);
423
50
  H_PUT_WORD (abfd, src->p_align, dst->p_align);
424
50
}
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
16.4k
{
432
16.4k
  const Elf_External_Rel *src = (const Elf_External_Rel *) s;
433
16.4k
  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
434
16.4k
  dst->r_info = H_GET_WORD (abfd, src->r_info);
435
16.4k
  dst->r_addend = 0;
436
16.4k
}
Unexecuted instantiation: bfd_elf64_swap_reloc_in
bfd_elf32_swap_reloc_in
Line
Count
Source
431
16.4k
{
432
16.4k
  const Elf_External_Rel *src = (const Elf_External_Rel *) s;
433
16.4k
  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
434
16.4k
  dst->r_info = H_GET_WORD (abfd, src->r_info);
435
16.4k
  dst->r_addend = 0;
436
16.4k
}
437
438
void
439
elf_swap_reloca_in (bfd *abfd,
440
        const bfd_byte *s,
441
        Elf_Internal_Rela *dst)
442
732k
{
443
732k
  const Elf_External_Rela *src = (const Elf_External_Rela *) s;
444
732k
  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
445
732k
  dst->r_info = H_GET_WORD (abfd, src->r_info);
446
732k
  dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
447
732k
}
bfd_elf64_swap_reloca_in
Line
Count
Source
442
727k
{
443
727k
  const Elf_External_Rela *src = (const Elf_External_Rela *) s;
444
727k
  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
445
727k
  dst->r_info = H_GET_WORD (abfd, src->r_info);
446
727k
  dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
447
727k
}
bfd_elf32_swap_reloca_in
Line
Count
Source
442
4.32k
{
443
4.32k
  const Elf_External_Rela *src = (const Elf_External_Rela *) s;
444
4.32k
  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
445
4.32k
  dst->r_info = H_GET_WORD (abfd, src->r_info);
446
4.32k
  dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
447
4.32k
}
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
57.2k
{
465
57.2k
  Elf_External_Rela *dst = (Elf_External_Rela *) d;
466
57.2k
  H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
467
57.2k
  H_PUT_WORD (abfd, src->r_info, dst->r_info);
468
57.2k
  H_PUT_SIGNED_WORD (abfd, src->r_addend, dst->r_addend);
469
57.2k
}
bfd_elf64_swap_reloca_out
Line
Count
Source
464
57.2k
{
465
57.2k
  Elf_External_Rela *dst = (Elf_External_Rela *) d;
466
57.2k
  H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
467
57.2k
  H_PUT_WORD (abfd, src->r_info, dst->r_info);
468
57.2k
  H_PUT_SIGNED_WORD (abfd, src->r_addend, dst->r_addend);
469
57.2k
}
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
142k
{
476
142k
  const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
477
478
142k
  dst->d_tag = H_GET_WORD (abfd, src->d_tag);
479
142k
  dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
480
142k
}
bfd_elf64_swap_dyn_in
Line
Count
Source
475
128k
{
476
128k
  const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
477
478
128k
  dst->d_tag = H_GET_WORD (abfd, src->d_tag);
479
128k
  dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
480
128k
}
bfd_elf32_swap_dyn_in
Line
Count
Source
475
13.9k
{
476
13.9k
  const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
477
478
13.9k
  dst->d_tag = H_GET_WORD (abfd, src->d_tag);
479
13.9k
  dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
480
13.9k
}
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
279M
{
503
279M
  return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
504
279M
    && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
505
279M
    && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
506
279M
    && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
507
279M
}
elf64.c:elf_file_p
Line
Count
Source
502
66.8M
{
503
66.8M
  return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
504
66.8M
    && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
505
66.8M
    && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
506
66.8M
    && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
507
66.8M
}
elf32.c:elf_file_p
Line
Count
Source
502
213M
{
503
213M
  return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
504
213M
    && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
505
213M
    && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
506
213M
    && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
507
213M
}
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
302M
{
520
302M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
521
302M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
522
302M
  Elf_External_Shdr x_shdr;  /* Section header table entry, external form */
523
302M
  Elf_Internal_Shdr i_shdr;
524
302M
  Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
525
302M
  unsigned int shindex;
526
302M
  const struct elf_backend_data *ebd;
527
302M
  asection *s;
528
302M
  const bfd_target *target;
529
530
  /* Read in the ELF header in external format.  */
531
532
302M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
533
24.2M
    {
534
24.2M
      if (bfd_get_error () != bfd_error_system_call)
535
24.0M
  goto got_wrong_format_error;
536
118k
      else
537
118k
  goto got_no_match;
538
24.2M
    }
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
278M
  if (! elf_file_p (&x_ehdr)
547
278M
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
548
278M
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
549
251M
    goto got_wrong_format_error;
550
551
  /* Check that file's byte order matches xvec's */
552
27.5M
  switch (x_ehdr.e_ident[EI_DATA])
553
27.5M
    {
554
3.52M
    case ELFDATA2MSB:   /* Big-endian */
555
3.52M
      if (! bfd_header_big_endian (abfd))
556
1.77M
  goto got_wrong_format_error;
557
1.74M
      break;
558
24.0M
    case ELFDATA2LSB:   /* Little-endian */
559
24.0M
      if (! bfd_header_little_endian (abfd))
560
11.6M
  goto got_wrong_format_error;
561
12.3M
      break;
562
12.3M
    case ELFDATANONE:   /* No data encoding specified */
563
10.0k
    default:      /* Unknown data encoding specified */
564
10.0k
      goto got_wrong_format_error;
565
27.5M
    }
566
567
14.0M
  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
14.0M
  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
14.0M
  i_ehdrp = elf_elfheader (abfd);
577
14.0M
  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
14.0M
  if (i_ehdrp->e_type == ET_CORE)
584
182k
    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
13.8M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_type == ET_REL)
589
20.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
13.8M
  if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
595
186k
    goto got_wrong_format_error;
596
597
  /* Further sanity check.  */
598
13.6M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_shnum != 0)
599
1.06k
    goto got_wrong_format_error;
600
601
13.6M
  ebd = get_elf_backend_data (abfd);
602
13.6M
  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
13.6M
  if (ebd->elf_machine_code != i_ehdrp->e_machine
608
13.6M
      && (ebd->elf_machine_alt1 == 0
609
13.0M
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
610
13.6M
      && (ebd->elf_machine_alt2 == 0
611
13.0M
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
612
13.6M
      && ebd->elf_machine_code != EM_NONE)
613
12.7M
    goto got_wrong_format_error;
614
615
938k
  if (i_ehdrp->e_type == ET_EXEC)
616
30.5k
    abfd->flags |= EXEC_P;
617
908k
  else if (i_ehdrp->e_type == ET_DYN)
618
57.6k
    abfd->flags |= DYNAMIC;
619
620
938k
  if (i_ehdrp->e_phnum > 0)
621
432k
    abfd->flags |= D_PAGED;
622
623
938k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
624
289k
    {
625
      /* It's OK if this fails for the generic target.  */
626
289k
      if (ebd->elf_machine_code != EM_NONE)
627
1.19k
  goto got_no_match;
628
289k
    }
629
630
937k
  if (ebd->elf_machine_code != EM_NONE
631
937k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
632
937k
      && ebd->elf_osabi != ELFOSABI_NONE)
633
78.0k
    goto got_wrong_format_error;
634
635
859k
  if (i_ehdrp->e_shoff >= sizeof (x_ehdr))
636
530k
    {
637
530k
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
638
639
      /* Seek to the section header table in the file.  */
640
530k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
641
6.24k
  goto got_no_match;
642
643
      /* Read the first section header at index 0, and convert to internal
644
   form.  */
645
524k
      if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
646
8.42k
  goto got_no_match;
647
515k
      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
515k
      if (i_ehdrp->e_shnum == SHN_UNDEF)
652
40.4k
  {
653
40.4k
    i_ehdrp->e_shnum = i_shdr.sh_size;
654
40.4k
    if (i_ehdrp->e_shnum >= SHN_LORESERVE
655
40.4k
        || i_ehdrp->e_shnum != i_shdr.sh_size
656
40.4k
        || i_ehdrp->e_shnum  == 0)
657
12.7k
      goto got_wrong_format_error;
658
40.4k
  }
659
660
      /* And similarly for the string table index.  */
661
502k
      if (i_ehdrp->e_shstrndx == (SHN_XINDEX & 0xffff))
662
81.1k
  {
663
81.1k
    i_ehdrp->e_shstrndx = i_shdr.sh_link;
664
81.1k
    if (i_ehdrp->e_shstrndx != i_shdr.sh_link)
665
0
      goto got_wrong_format_error;
666
81.1k
  }
667
668
      /* And program headers.  */
669
502k
      if (i_ehdrp->e_phnum == PN_XNUM && i_shdr.sh_info != 0)
670
47.7k
  {
671
47.7k
    i_ehdrp->e_phnum = i_shdr.sh_info;
672
47.7k
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
673
0
      goto got_wrong_format_error;
674
47.7k
  }
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
502k
      if (i_ehdrp->e_shnum != 1)
679
356k
  {
680
    /* Check that we don't have a totally silly number of sections.  */
681
356k
    if (i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (x_shdr)
682
356k
        || i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (i_shdr))
683
6.10k
      goto got_wrong_format_error;
684
685
349k
    where += (i_ehdrp->e_shnum - 1) * sizeof (x_shdr);
686
349k
    if ((bfd_size_type) where <= i_ehdrp->e_shoff)
687
0
      goto got_wrong_format_error;
688
689
349k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
690
0
      goto got_no_match;
691
349k
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
692
7.72k
      goto got_no_match;
693
694
    /* Back to where we were.  */
695
342k
    where = i_ehdrp->e_shoff + sizeof (x_shdr);
696
342k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
697
0
      goto got_no_match;
698
342k
  }
699
502k
    }
700
701
  /* Allocate space for a copy of the section header table in
702
     internal form.  */
703
818k
  if (i_ehdrp->e_shnum != 0)
704
489k
    {
705
489k
      Elf_Internal_Shdr *shdrp;
706
489k
      unsigned int num_sec;
707
489k
      size_t amt;
708
709
489k
      if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*i_shdrp), &amt))
710
0
  goto got_wrong_format_error;
711
489k
      i_shdrp = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
712
489k
      if (!i_shdrp)
713
0
  goto got_no_match;
714
489k
      num_sec = i_ehdrp->e_shnum;
715
489k
      elf_numsections (abfd) = num_sec;
716
489k
      if (_bfd_mul_overflow (num_sec, sizeof (i_shdrp), &amt))
717
0
  goto got_wrong_format_error;
718
489k
      elf_elfsections (abfd) = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
719
489k
      if (!elf_elfsections (abfd))
720
0
  goto got_no_match;
721
489k
      elf_tdata (abfd)->being_created = bfd_zalloc (abfd, num_sec);
722
489k
      if (!elf_tdata (abfd)->being_created)
723
0
  goto got_no_match;
724
725
489k
      memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
726
5.06M
      for (shdrp = i_shdrp, shindex = 0; shindex < num_sec; shindex++)
727
4.57M
  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.43M
      for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
732
3.96M
  {
733
3.96M
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
734
0
      goto got_no_match;
735
3.96M
    elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
736
737
    /* Sanity check sh_link and sh_info.  */
738
3.96M
    if (i_shdrp[shindex].sh_link >= num_sec)
739
26.8k
      {
740
        /* PR 10478: Accept Solaris binaries with a sh_link
741
     field set to SHN_BEFORE or SHN_AFTER.  */
742
26.8k
        switch (ebd->elf_machine_code)
743
26.8k
    {
744
7.22k
    case EM_386:
745
7.23k
    case EM_IAMCU:
746
14.1k
    case EM_X86_64:
747
14.1k
    case EM_OLD_SPARCV9:
748
14.1k
    case EM_SPARC32PLUS:
749
14.7k
    case EM_SPARCV9:
750
15.1k
    case EM_SPARC:
751
15.1k
      if (i_shdrp[shindex].sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
752
15.1k
          || i_shdrp[shindex].sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
753
8.35k
        break;
754
      /* Otherwise fall through.  */
755
18.5k
    default:
756
18.5k
      goto got_wrong_format_error;
757
26.8k
    }
758
26.8k
      }
759
760
3.94M
    if (((i_shdrp[shindex].sh_flags & SHF_INFO_LINK)
761
3.94M
         || i_shdrp[shindex].sh_type == SHT_RELA
762
3.94M
         || i_shdrp[shindex].sh_type == SHT_REL)
763
3.94M
        && i_shdrp[shindex].sh_info >= num_sec)
764
3.71k
      goto got_wrong_format_error;
765
766
    /* If the section is loaded, but not page aligned, clear
767
       D_PAGED.  */
768
3.94M
    if (i_shdrp[shindex].sh_size != 0
769
3.94M
        && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
770
3.94M
        && i_shdrp[shindex].sh_type != SHT_NOBITS
771
3.94M
        && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
772
628k
       % ebd->minpagesize)
773
628k
      != 0))
774
373k
      abfd->flags &= ~D_PAGED;
775
3.94M
  }
776
777
466k
      if (i_ehdrp->e_shstrndx >= elf_numsections (abfd)
778
466k
    || i_shdrp[i_ehdrp->e_shstrndx].sh_type != SHT_STRTAB)
779
216k
  {
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
216k
    i_ehdrp->e_shstrndx = SHN_UNDEF;
787
216k
    if (!abfd->read_only)
788
17.1k
      {
789
17.1k
        _bfd_error_handler
790
17.1k
    (_("warning: %pB has a corrupt string table index"), abfd);
791
17.1k
        abfd->read_only = 1;
792
17.1k
      }
793
216k
  }
794
466k
    }
795
329k
  else if (i_ehdrp->e_shstrndx != SHN_UNDEF)
796
2.57k
    goto got_wrong_format_error;
797
798
  /* Read in the program headers.  */
799
793k
  if (i_ehdrp->e_phnum == 0)
800
430k
    elf_tdata (abfd)->phdr = NULL;
801
362k
  else
802
362k
    {
803
362k
      Elf_Internal_Phdr *i_phdr;
804
362k
      unsigned int i;
805
362k
      ufile_ptr filesize;
806
362k
      size_t amt;
807
808
      /* Check for a corrupt input file with an impossibly large number
809
   of program headers.  */
810
362k
      filesize = bfd_get_file_size (abfd);
811
362k
      if (filesize != 0
812
362k
    && i_ehdrp->e_phnum > filesize / sizeof (Elf_External_Phdr))
813
17.6k
  goto got_wrong_format_error;
814
345k
      if (_bfd_mul_overflow (i_ehdrp->e_phnum, sizeof (*i_phdr), &amt))
815
0
  goto got_wrong_format_error;
816
345k
      elf_tdata (abfd)->phdr
817
345k
  = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
818
345k
      if (elf_tdata (abfd)->phdr == NULL)
819
0
  goto got_no_match;
820
345k
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
821
2.14k
  goto got_no_match;
822
342k
      bool eu_strip_broken_phdrs = false;
823
342k
      i_phdr = elf_tdata (abfd)->phdr;
824
63.6M
      for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
825
63.3M
  {
826
63.3M
    Elf_External_Phdr x_phdr;
827
828
63.3M
    if (bfd_read (&x_phdr, sizeof x_phdr, abfd) != sizeof x_phdr)
829
4.51k
      goto got_no_match;
830
63.3M
    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
63.3M
    if (i_phdr->p_align != (i_phdr->p_align & -i_phdr->p_align))
834
48.0M
      {
835
48.0M
        i_phdr->p_align &= -i_phdr->p_align;
836
48.0M
        if (!abfd->read_only)
837
114k
    {
838
114k
      _bfd_error_handler (_("warning: %pB has a program header "
839
114k
          "with invalid alignment"), abfd);
840
114k
      abfd->read_only = 1;
841
114k
    }
842
48.0M
      }
843
    /* Detect eu-strip -f debug files, which have program
844
       headers that describe the original file.  */
845
63.3M
    if (i_phdr->p_filesz != 0
846
63.3M
        && (i_phdr->p_filesz > filesize
847
50.7M
      || i_phdr->p_offset > filesize - i_phdr->p_filesz))
848
48.0M
      eu_strip_broken_phdrs = true;
849
63.3M
  }
850
338k
      if (!eu_strip_broken_phdrs
851
338k
    && i_ehdrp->e_shoff == 0
852
338k
    && i_ehdrp->e_shstrndx == 0)
853
15.8k
  {
854
    /* Try to reconstruct dynamic symbol table from PT_DYNAMIC
855
       segment if there is no section header.  */
856
15.8k
    i_phdr = elf_tdata (abfd)->phdr;
857
37.8k
    for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
858
29.8k
      if (i_phdr->p_type == PT_DYNAMIC)
859
7.84k
        {
860
7.84k
    if (i_phdr->p_filesz != 0
861
7.84k
        && !_bfd_elf_get_dynamic_symbols (abfd, i_phdr,
862
4.24k
                  elf_tdata (abfd)->phdr,
863
4.24k
                  i_ehdrp->e_phnum,
864
4.24k
                  filesize))
865
4.21k
      goto got_no_match;
866
3.63k
    break;
867
7.84k
        }
868
15.8k
  }
869
338k
    }
870
871
764k
  if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff >= sizeof (x_ehdr))
872
248k
    {
873
248k
      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
248k
      num_sec = elf_numsections (abfd);
879
2.94M
      for (shindex = 1; shindex < num_sec; shindex++)
880
2.76M
  if (!bfd_section_from_shdr (abfd, shindex))
881
61.0k
    goto got_no_match;
882
883
      /* Set up ELF sections for SHF_GROUP and SHF_LINK_ORDER.  */
884
187k
      if (! _bfd_elf_setup_sections (abfd))
885
5.85k
  goto got_wrong_format_error;
886
187k
    }
887
888
  /* Let the backend double check the format and override global
889
     information.  */
890
698k
  if (ebd->elf_backend_object_p)
891
461k
    {
892
461k
      if (! (*ebd->elf_backend_object_p) (abfd))
893
42.3k
  goto got_wrong_format_error;
894
461k
    }
895
896
  /* Remember the entry point specified in the ELF file header.  */
897
655k
  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
1.85M
  for (s = abfd->sections; s != NULL; s = s->next)
902
1.20M
    {
903
1.20M
      if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL
904
1.20M
     || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
905
1.20M
    && elf_section_data (s)->this_hdr.sh_info > 0)
906
38.0k
  {
907
38.0k
    unsigned long targ_index;
908
38.0k
    asection *targ_sec;
909
910
38.0k
    targ_index = elf_section_data (s)->this_hdr.sh_info;
911
38.0k
    targ_sec = bfd_section_from_elf_index (abfd, targ_index);
912
38.0k
    if (targ_sec != NULL
913
38.0k
        && (targ_sec->flags & SEC_DEBUGGING) != 0)
914
1.81k
      s->flags |= SEC_DEBUGGING;
915
38.0k
  }
916
1.20M
    }
917
655k
  return _bfd_no_cleanup;
918
919
301M
 got_wrong_format_error:
920
301M
  bfd_set_error (bfd_error_wrong_format);
921
922
302M
 got_no_match:
923
302M
  return NULL;
924
301M
}
bfd_elf64_object_p
Line
Count
Source
519
74.8M
{
520
74.8M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
521
74.8M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
522
74.8M
  Elf_External_Shdr x_shdr;  /* Section header table entry, external form */
523
74.8M
  Elf_Internal_Shdr i_shdr;
524
74.8M
  Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
525
74.8M
  unsigned int shindex;
526
74.8M
  const struct elf_backend_data *ebd;
527
74.8M
  asection *s;
528
74.8M
  const bfd_target *target;
529
530
  /* Read in the ELF header in external format.  */
531
532
74.8M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
533
8.30M
    {
534
8.30M
      if (bfd_get_error () != bfd_error_system_call)
535
8.28M
  goto got_wrong_format_error;
536
28.8k
      else
537
28.8k
  goto got_no_match;
538
8.30M
    }
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
66.5M
  if (! elf_file_p (&x_ehdr)
547
66.5M
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
548
66.5M
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
549
60.5M
    goto got_wrong_format_error;
550
551
  /* Check that file's byte order matches xvec's */
552
5.96M
  switch (x_ehdr.e_ident[EI_DATA])
553
5.96M
    {
554
567k
    case ELFDATA2MSB:   /* Big-endian */
555
567k
      if (! bfd_header_big_endian (abfd))
556
307k
  goto got_wrong_format_error;
557
260k
      break;
558
5.39M
    case ELFDATA2LSB:   /* Little-endian */
559
5.39M
      if (! bfd_header_little_endian (abfd))
560
2.42M
  goto got_wrong_format_error;
561
2.96M
      break;
562
2.96M
    case ELFDATANONE:   /* No data encoding specified */
563
5.23k
    default:      /* Unknown data encoding specified */
564
5.23k
      goto got_wrong_format_error;
565
5.96M
    }
566
567
3.22M
  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
3.22M
  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
3.22M
  i_ehdrp = elf_elfheader (abfd);
577
3.22M
  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
3.22M
  if (i_ehdrp->e_type == ET_CORE)
584
10.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
3.21M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_type == ET_REL)
589
7.61k
    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
3.20M
  if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
595
47.2k
    goto got_wrong_format_error;
596
597
  /* Further sanity check.  */
598
3.16M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_shnum != 0)
599
263
    goto got_wrong_format_error;
600
601
3.16M
  ebd = get_elf_backend_data (abfd);
602
3.16M
  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
3.16M
  if (ebd->elf_machine_code != i_ehdrp->e_machine
608
3.16M
      && (ebd->elf_machine_alt1 == 0
609
2.87M
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
610
3.16M
      && (ebd->elf_machine_alt2 == 0
611
2.86M
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
612
3.16M
      && ebd->elf_machine_code != EM_NONE)
613
2.73M
    goto got_wrong_format_error;
614
615
427k
  if (i_ehdrp->e_type == ET_EXEC)
616
20.3k
    abfd->flags |= EXEC_P;
617
407k
  else if (i_ehdrp->e_type == ET_DYN)
618
40.4k
    abfd->flags |= DYNAMIC;
619
620
427k
  if (i_ehdrp->e_phnum > 0)
621
232k
    abfd->flags |= D_PAGED;
622
623
427k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
624
132k
    {
625
      /* It's OK if this fails for the generic target.  */
626
132k
      if (ebd->elf_machine_code != EM_NONE)
627
0
  goto got_no_match;
628
132k
    }
629
630
427k
  if (ebd->elf_machine_code != EM_NONE
631
427k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
632
427k
      && ebd->elf_osabi != ELFOSABI_NONE)
633
56.9k
    goto got_wrong_format_error;
634
635
370k
  if (i_ehdrp->e_shoff >= sizeof (x_ehdr))
636
233k
    {
637
233k
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
638
639
      /* Seek to the section header table in the file.  */
640
233k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
641
6.24k
  goto got_no_match;
642
643
      /* Read the first section header at index 0, and convert to internal
644
   form.  */
645
227k
      if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
646
5.94k
  goto got_no_match;
647
221k
      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
221k
      if (i_ehdrp->e_shnum == SHN_UNDEF)
652
19.4k
  {
653
19.4k
    i_ehdrp->e_shnum = i_shdr.sh_size;
654
19.4k
    if (i_ehdrp->e_shnum >= SHN_LORESERVE
655
19.4k
        || i_ehdrp->e_shnum != i_shdr.sh_size
656
19.4k
        || i_ehdrp->e_shnum  == 0)
657
10.0k
      goto got_wrong_format_error;
658
19.4k
  }
659
660
      /* And similarly for the string table index.  */
661
211k
      if (i_ehdrp->e_shstrndx == (SHN_XINDEX & 0xffff))
662
15.9k
  {
663
15.9k
    i_ehdrp->e_shstrndx = i_shdr.sh_link;
664
15.9k
    if (i_ehdrp->e_shstrndx != i_shdr.sh_link)
665
0
      goto got_wrong_format_error;
666
15.9k
  }
667
668
      /* And program headers.  */
669
211k
      if (i_ehdrp->e_phnum == PN_XNUM && i_shdr.sh_info != 0)
670
14.3k
  {
671
14.3k
    i_ehdrp->e_phnum = i_shdr.sh_info;
672
14.3k
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
673
0
      goto got_wrong_format_error;
674
14.3k
  }
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
211k
      if (i_ehdrp->e_shnum != 1)
679
162k
  {
680
    /* Check that we don't have a totally silly number of sections.  */
681
162k
    if (i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (x_shdr)
682
162k
        || i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (i_shdr))
683
1.86k
      goto got_wrong_format_error;
684
685
160k
    where += (i_ehdrp->e_shnum - 1) * sizeof (x_shdr);
686
160k
    if ((bfd_size_type) where <= i_ehdrp->e_shoff)
687
0
      goto got_wrong_format_error;
688
689
160k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
690
0
      goto got_no_match;
691
160k
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
692
5.47k
      goto got_no_match;
693
694
    /* Back to where we were.  */
695
155k
    where = i_ehdrp->e_shoff + sizeof (x_shdr);
696
155k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
697
0
      goto got_no_match;
698
155k
  }
699
211k
    }
700
701
  /* Allocate space for a copy of the section header table in
702
     internal form.  */
703
341k
  if (i_ehdrp->e_shnum != 0)
704
204k
    {
705
204k
      Elf_Internal_Shdr *shdrp;
706
204k
      unsigned int num_sec;
707
204k
      size_t amt;
708
709
204k
      if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*i_shdrp), &amt))
710
0
  goto got_wrong_format_error;
711
204k
      i_shdrp = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
712
204k
      if (!i_shdrp)
713
0
  goto got_no_match;
714
204k
      num_sec = i_ehdrp->e_shnum;
715
204k
      elf_numsections (abfd) = num_sec;
716
204k
      if (_bfd_mul_overflow (num_sec, sizeof (i_shdrp), &amt))
717
0
  goto got_wrong_format_error;
718
204k
      elf_elfsections (abfd) = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
719
204k
      if (!elf_elfsections (abfd))
720
0
  goto got_no_match;
721
204k
      elf_tdata (abfd)->being_created = bfd_zalloc (abfd, num_sec);
722
204k
      if (!elf_tdata (abfd)->being_created)
723
0
  goto got_no_match;
724
725
204k
      memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
726
1.92M
      for (shdrp = i_shdrp, shindex = 0; shindex < num_sec; shindex++)
727
1.72M
  elf_elfsections (abfd)[shindex] = shdrp++;
728
729
      /* Read in the rest of the section header table and convert it
730
   to internal form.  */
731
1.66M
      for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
732
1.47M
  {
733
1.47M
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
734
0
      goto got_no_match;
735
1.47M
    elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
736
737
    /* Sanity check sh_link and sh_info.  */
738
1.47M
    if (i_shdrp[shindex].sh_link >= num_sec)
739
13.5k
      {
740
        /* PR 10478: Accept Solaris binaries with a sh_link
741
     field set to SHN_BEFORE or SHN_AFTER.  */
742
13.5k
        switch (ebd->elf_machine_code)
743
13.5k
    {
744
0
    case EM_386:
745
0
    case EM_IAMCU:
746
6.46k
    case EM_X86_64:
747
6.46k
    case EM_OLD_SPARCV9:
748
6.46k
    case EM_SPARC32PLUS:
749
7.04k
    case EM_SPARCV9:
750
7.04k
    case EM_SPARC:
751
7.04k
      if (i_shdrp[shindex].sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
752
7.04k
          || i_shdrp[shindex].sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
753
2.79k
        break;
754
      /* Otherwise fall through.  */
755
10.7k
    default:
756
10.7k
      goto got_wrong_format_error;
757
13.5k
    }
758
13.5k
      }
759
760
1.46M
    if (((i_shdrp[shindex].sh_flags & SHF_INFO_LINK)
761
1.46M
         || i_shdrp[shindex].sh_type == SHT_RELA
762
1.46M
         || i_shdrp[shindex].sh_type == SHT_REL)
763
1.46M
        && i_shdrp[shindex].sh_info >= num_sec)
764
1.95k
      goto got_wrong_format_error;
765
766
    /* If the section is loaded, but not page aligned, clear
767
       D_PAGED.  */
768
1.46M
    if (i_shdrp[shindex].sh_size != 0
769
1.46M
        && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
770
1.46M
        && i_shdrp[shindex].sh_type != SHT_NOBITS
771
1.46M
        && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
772
322k
       % ebd->minpagesize)
773
322k
      != 0))
774
208k
      abfd->flags &= ~D_PAGED;
775
1.46M
  }
776
777
191k
      if (i_ehdrp->e_shstrndx >= elf_numsections (abfd)
778
191k
    || i_shdrp[i_ehdrp->e_shstrndx].sh_type != SHT_STRTAB)
779
74.4k
  {
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
74.4k
    i_ehdrp->e_shstrndx = SHN_UNDEF;
787
74.4k
    if (!abfd->read_only)
788
9.18k
      {
789
9.18k
        _bfd_error_handler
790
9.18k
    (_("warning: %pB has a corrupt string table index"), abfd);
791
9.18k
        abfd->read_only = 1;
792
9.18k
      }
793
74.4k
  }
794
191k
    }
795
136k
  else if (i_ehdrp->e_shstrndx != SHN_UNDEF)
796
1.47k
    goto got_wrong_format_error;
797
798
  /* Read in the program headers.  */
799
326k
  if (i_ehdrp->e_phnum == 0)
800
148k
    elf_tdata (abfd)->phdr = NULL;
801
178k
  else
802
178k
    {
803
178k
      Elf_Internal_Phdr *i_phdr;
804
178k
      unsigned int i;
805
178k
      ufile_ptr filesize;
806
178k
      size_t amt;
807
808
      /* Check for a corrupt input file with an impossibly large number
809
   of program headers.  */
810
178k
      filesize = bfd_get_file_size (abfd);
811
178k
      if (filesize != 0
812
178k
    && i_ehdrp->e_phnum > filesize / sizeof (Elf_External_Phdr))
813
13.6k
  goto got_wrong_format_error;
814
164k
      if (_bfd_mul_overflow (i_ehdrp->e_phnum, sizeof (*i_phdr), &amt))
815
0
  goto got_wrong_format_error;
816
164k
      elf_tdata (abfd)->phdr
817
164k
  = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
818
164k
      if (elf_tdata (abfd)->phdr == NULL)
819
0
  goto got_no_match;
820
164k
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
821
2.14k
  goto got_no_match;
822
162k
      bool eu_strip_broken_phdrs = false;
823
162k
      i_phdr = elf_tdata (abfd)->phdr;
824
16.5M
      for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
825
16.4M
  {
826
16.4M
    Elf_External_Phdr x_phdr;
827
828
16.4M
    if (bfd_read (&x_phdr, sizeof x_phdr, abfd) != sizeof x_phdr)
829
2.06k
      goto got_no_match;
830
16.4M
    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.4M
    if (i_phdr->p_align != (i_phdr->p_align & -i_phdr->p_align))
834
13.7M
      {
835
13.7M
        i_phdr->p_align &= -i_phdr->p_align;
836
13.7M
        if (!abfd->read_only)
837
105k
    {
838
105k
      _bfd_error_handler (_("warning: %pB has a program header "
839
105k
          "with invalid alignment"), abfd);
840
105k
      abfd->read_only = 1;
841
105k
    }
842
13.7M
      }
843
    /* Detect eu-strip -f debug files, which have program
844
       headers that describe the original file.  */
845
16.4M
    if (i_phdr->p_filesz != 0
846
16.4M
        && (i_phdr->p_filesz > filesize
847
14.0M
      || i_phdr->p_offset > filesize - i_phdr->p_filesz))
848
13.9M
      eu_strip_broken_phdrs = true;
849
16.4M
  }
850
160k
      if (!eu_strip_broken_phdrs
851
160k
    && i_ehdrp->e_shoff == 0
852
160k
    && i_ehdrp->e_shstrndx == 0)
853
14.7k
  {
854
    /* Try to reconstruct dynamic symbol table from PT_DYNAMIC
855
       segment if there is no section header.  */
856
14.7k
    i_phdr = elf_tdata (abfd)->phdr;
857
34.7k
    for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
858
27.5k
      if (i_phdr->p_type == PT_DYNAMIC)
859
7.57k
        {
860
7.57k
    if (i_phdr->p_filesz != 0
861
7.57k
        && !_bfd_elf_get_dynamic_symbols (abfd, i_phdr,
862
4.01k
                  elf_tdata (abfd)->phdr,
863
4.01k
                  i_ehdrp->e_phnum,
864
4.01k
                  filesize))
865
3.98k
      goto got_no_match;
866
3.59k
    break;
867
7.57k
        }
868
14.7k
  }
869
160k
    }
870
871
305k
  if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff >= sizeof (x_ehdr))
872
116k
    {
873
116k
      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
116k
      num_sec = elf_numsections (abfd);
879
1.10M
      for (shindex = 1; shindex < num_sec; shindex++)
880
1.03M
  if (!bfd_section_from_shdr (abfd, shindex))
881
48.3k
    goto got_no_match;
882
883
      /* Set up ELF sections for SHF_GROUP and SHF_LINK_ORDER.  */
884
67.8k
      if (! _bfd_elf_setup_sections (abfd))
885
4.81k
  goto got_wrong_format_error;
886
67.8k
    }
887
888
  /* Let the backend double check the format and override global
889
     information.  */
890
252k
  if (ebd->elf_backend_object_p)
891
157k
    {
892
157k
      if (! (*ebd->elf_backend_object_p) (abfd))
893
723
  goto got_wrong_format_error;
894
157k
    }
895
896
  /* Remember the entry point specified in the ELF file header.  */
897
251k
  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
742k
  for (s = abfd->sections; s != NULL; s = s->next)
902
491k
    {
903
491k
      if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL
904
491k
     || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
905
491k
    && elf_section_data (s)->this_hdr.sh_info > 0)
906
17.6k
  {
907
17.6k
    unsigned long targ_index;
908
17.6k
    asection *targ_sec;
909
910
17.6k
    targ_index = elf_section_data (s)->this_hdr.sh_info;
911
17.6k
    targ_sec = bfd_section_from_elf_index (abfd, targ_index);
912
17.6k
    if (targ_sec != NULL
913
17.6k
        && (targ_sec->flags & SEC_DEBUGGING) != 0)
914
1.31k
      s->flags |= SEC_DEBUGGING;
915
17.6k
  }
916
491k
    }
917
251k
  return _bfd_no_cleanup;
918
919
74.5M
 got_wrong_format_error:
920
74.5M
  bfd_set_error (bfd_error_wrong_format);
921
922
74.6M
 got_no_match:
923
74.6M
  return NULL;
924
74.5M
}
bfd_elf32_object_p
Line
Count
Source
519
227M
{
520
227M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
521
227M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
522
227M
  Elf_External_Shdr x_shdr;  /* Section header table entry, external form */
523
227M
  Elf_Internal_Shdr i_shdr;
524
227M
  Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
525
227M
  unsigned int shindex;
526
227M
  const struct elf_backend_data *ebd;
527
227M
  asection *s;
528
227M
  const bfd_target *target;
529
530
  /* Read in the ELF header in external format.  */
531
532
227M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
533
15.8M
    {
534
15.8M
      if (bfd_get_error () != bfd_error_system_call)
535
15.8M
  goto got_wrong_format_error;
536
89.7k
      else
537
89.7k
  goto got_no_match;
538
15.8M
    }
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
212M
  if (! elf_file_p (&x_ehdr)
547
212M
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
548
212M
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
549
190M
    goto got_wrong_format_error;
550
551
  /* Check that file's byte order matches xvec's */
552
21.5M
  switch (x_ehdr.e_ident[EI_DATA])
553
21.5M
    {
554
2.95M
    case ELFDATA2MSB:   /* Big-endian */
555
2.95M
      if (! bfd_header_big_endian (abfd))
556
1.46M
  goto got_wrong_format_error;
557
1.48M
      break;
558
18.6M
    case ELFDATA2LSB:   /* Little-endian */
559
18.6M
      if (! bfd_header_little_endian (abfd))
560
9.25M
  goto got_wrong_format_error;
561
9.35M
      break;
562
9.35M
    case ELFDATANONE:   /* No data encoding specified */
563
4.82k
    default:      /* Unknown data encoding specified */
564
4.82k
      goto got_wrong_format_error;
565
21.5M
    }
566
567
10.8M
  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
10.8M
  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
10.8M
  i_ehdrp = elf_elfheader (abfd);
577
10.8M
  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
10.8M
  if (i_ehdrp->e_type == ET_CORE)
584
171k
    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
10.6M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_type == ET_REL)
589
12.8k
    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
10.6M
  if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
595
139k
    goto got_wrong_format_error;
596
597
  /* Further sanity check.  */
598
10.5M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_shnum != 0)
599
804
    goto got_wrong_format_error;
600
601
10.5M
  ebd = get_elf_backend_data (abfd);
602
10.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
10.5M
  if (ebd->elf_machine_code != i_ehdrp->e_machine
608
10.5M
      && (ebd->elf_machine_alt1 == 0
609
10.1M
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
610
10.5M
      && (ebd->elf_machine_alt2 == 0
611
10.1M
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
612
10.5M
      && ebd->elf_machine_code != EM_NONE)
613
9.99M
    goto got_wrong_format_error;
614
615
511k
  if (i_ehdrp->e_type == ET_EXEC)
616
10.2k
    abfd->flags |= EXEC_P;
617
500k
  else if (i_ehdrp->e_type == ET_DYN)
618
17.1k
    abfd->flags |= DYNAMIC;
619
620
511k
  if (i_ehdrp->e_phnum > 0)
621
199k
    abfd->flags |= D_PAGED;
622
623
511k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
624
157k
    {
625
      /* It's OK if this fails for the generic target.  */
626
157k
      if (ebd->elf_machine_code != EM_NONE)
627
1.19k
  goto got_no_match;
628
157k
    }
629
630
509k
  if (ebd->elf_machine_code != EM_NONE
631
509k
      && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
632
509k
      && ebd->elf_osabi != ELFOSABI_NONE)
633
21.0k
    goto got_wrong_format_error;
634
635
488k
  if (i_ehdrp->e_shoff >= sizeof (x_ehdr))
636
296k
    {
637
296k
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
638
639
      /* Seek to the section header table in the file.  */
640
296k
      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
296k
      if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
646
2.48k
  goto got_no_match;
647
293k
      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
293k
      if (i_ehdrp->e_shnum == SHN_UNDEF)
652
21.0k
  {
653
21.0k
    i_ehdrp->e_shnum = i_shdr.sh_size;
654
21.0k
    if (i_ehdrp->e_shnum >= SHN_LORESERVE
655
21.0k
        || i_ehdrp->e_shnum != i_shdr.sh_size
656
21.0k
        || i_ehdrp->e_shnum  == 0)
657
2.70k
      goto got_wrong_format_error;
658
21.0k
  }
659
660
      /* And similarly for the string table index.  */
661
291k
      if (i_ehdrp->e_shstrndx == (SHN_XINDEX & 0xffff))
662
65.2k
  {
663
65.2k
    i_ehdrp->e_shstrndx = i_shdr.sh_link;
664
65.2k
    if (i_ehdrp->e_shstrndx != i_shdr.sh_link)
665
0
      goto got_wrong_format_error;
666
65.2k
  }
667
668
      /* And program headers.  */
669
291k
      if (i_ehdrp->e_phnum == PN_XNUM && i_shdr.sh_info != 0)
670
33.3k
  {
671
33.3k
    i_ehdrp->e_phnum = i_shdr.sh_info;
672
33.3k
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
673
0
      goto got_wrong_format_error;
674
33.3k
  }
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
291k
      if (i_ehdrp->e_shnum != 1)
679
193k
  {
680
    /* Check that we don't have a totally silly number of sections.  */
681
193k
    if (i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (x_shdr)
682
193k
        || i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (i_shdr))
683
4.23k
      goto got_wrong_format_error;
684
685
189k
    where += (i_ehdrp->e_shnum - 1) * sizeof (x_shdr);
686
189k
    if ((bfd_size_type) where <= i_ehdrp->e_shoff)
687
0
      goto got_wrong_format_error;
688
689
189k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
690
0
      goto got_no_match;
691
189k
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
692
2.25k
      goto got_no_match;
693
694
    /* Back to where we were.  */
695
187k
    where = i_ehdrp->e_shoff + sizeof (x_shdr);
696
187k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
697
0
      goto got_no_match;
698
187k
  }
699
291k
    }
700
701
  /* Allocate space for a copy of the section header table in
702
     internal form.  */
703
477k
  if (i_ehdrp->e_shnum != 0)
704
284k
    {
705
284k
      Elf_Internal_Shdr *shdrp;
706
284k
      unsigned int num_sec;
707
284k
      size_t amt;
708
709
284k
      if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*i_shdrp), &amt))
710
0
  goto got_wrong_format_error;
711
284k
      i_shdrp = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
712
284k
      if (!i_shdrp)
713
0
  goto got_no_match;
714
284k
      num_sec = i_ehdrp->e_shnum;
715
284k
      elf_numsections (abfd) = num_sec;
716
284k
      if (_bfd_mul_overflow (num_sec, sizeof (i_shdrp), &amt))
717
0
  goto got_wrong_format_error;
718
284k
      elf_elfsections (abfd) = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
719
284k
      if (!elf_elfsections (abfd))
720
0
  goto got_no_match;
721
284k
      elf_tdata (abfd)->being_created = bfd_zalloc (abfd, num_sec);
722
284k
      if (!elf_tdata (abfd)->being_created)
723
0
  goto got_no_match;
724
725
284k
      memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
726
3.14M
      for (shdrp = i_shdrp, shindex = 0; shindex < num_sec; shindex++)
727
2.85M
  elf_elfsections (abfd)[shindex] = shdrp++;
728
729
      /* Read in the rest of the section header table and convert it
730
   to internal form.  */
731
2.76M
      for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
732
2.48M
  {
733
2.48M
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
734
0
      goto got_no_match;
735
2.48M
    elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
736
737
    /* Sanity check sh_link and sh_info.  */
738
2.48M
    if (i_shdrp[shindex].sh_link >= num_sec)
739
13.3k
      {
740
        /* PR 10478: Accept Solaris binaries with a sh_link
741
     field set to SHN_BEFORE or SHN_AFTER.  */
742
13.3k
        switch (ebd->elf_machine_code)
743
13.3k
    {
744
7.22k
    case EM_386:
745
7.23k
    case EM_IAMCU:
746
7.72k
    case EM_X86_64:
747
7.72k
    case EM_OLD_SPARCV9:
748
7.72k
    case EM_SPARC32PLUS:
749
7.72k
    case EM_SPARCV9:
750
8.09k
    case EM_SPARC:
751
8.09k
      if (i_shdrp[shindex].sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
752
8.09k
          || i_shdrp[shindex].sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
753
5.55k
        break;
754
      /* Otherwise fall through.  */
755
7.79k
    default:
756
7.79k
      goto got_wrong_format_error;
757
13.3k
    }
758
13.3k
      }
759
760
2.47M
    if (((i_shdrp[shindex].sh_flags & SHF_INFO_LINK)
761
2.47M
         || i_shdrp[shindex].sh_type == SHT_RELA
762
2.47M
         || i_shdrp[shindex].sh_type == SHT_REL)
763
2.47M
        && i_shdrp[shindex].sh_info >= num_sec)
764
1.76k
      goto got_wrong_format_error;
765
766
    /* If the section is loaded, but not page aligned, clear
767
       D_PAGED.  */
768
2.47M
    if (i_shdrp[shindex].sh_size != 0
769
2.47M
        && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
770
2.47M
        && i_shdrp[shindex].sh_type != SHT_NOBITS
771
2.47M
        && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
772
305k
       % ebd->minpagesize)
773
305k
      != 0))
774
164k
      abfd->flags &= ~D_PAGED;
775
2.47M
  }
776
777
275k
      if (i_ehdrp->e_shstrndx >= elf_numsections (abfd)
778
275k
    || i_shdrp[i_ehdrp->e_shstrndx].sh_type != SHT_STRTAB)
779
142k
  {
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
142k
    i_ehdrp->e_shstrndx = SHN_UNDEF;
787
142k
    if (!abfd->read_only)
788
7.96k
      {
789
7.96k
        _bfd_error_handler
790
7.96k
    (_("warning: %pB has a corrupt string table index"), abfd);
791
7.96k
        abfd->read_only = 1;
792
7.96k
      }
793
142k
  }
794
275k
    }
795
192k
  else if (i_ehdrp->e_shstrndx != SHN_UNDEF)
796
1.10k
    goto got_wrong_format_error;
797
798
  /* Read in the program headers.  */
799
466k
  if (i_ehdrp->e_phnum == 0)
800
282k
    elf_tdata (abfd)->phdr = NULL;
801
184k
  else
802
184k
    {
803
184k
      Elf_Internal_Phdr *i_phdr;
804
184k
      unsigned int i;
805
184k
      ufile_ptr filesize;
806
184k
      size_t amt;
807
808
      /* Check for a corrupt input file with an impossibly large number
809
   of program headers.  */
810
184k
      filesize = bfd_get_file_size (abfd);
811
184k
      if (filesize != 0
812
184k
    && i_ehdrp->e_phnum > filesize / sizeof (Elf_External_Phdr))
813
4.00k
  goto got_wrong_format_error;
814
180k
      if (_bfd_mul_overflow (i_ehdrp->e_phnum, sizeof (*i_phdr), &amt))
815
0
  goto got_wrong_format_error;
816
180k
      elf_tdata (abfd)->phdr
817
180k
  = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
818
180k
      if (elf_tdata (abfd)->phdr == NULL)
819
0
  goto got_no_match;
820
180k
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
821
0
  goto got_no_match;
822
180k
      bool eu_strip_broken_phdrs = false;
823
180k
      i_phdr = elf_tdata (abfd)->phdr;
824
47.0M
      for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
825
46.9M
  {
826
46.9M
    Elf_External_Phdr x_phdr;
827
828
46.9M
    if (bfd_read (&x_phdr, sizeof x_phdr, abfd) != sizeof x_phdr)
829
2.45k
      goto got_no_match;
830
46.9M
    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
46.9M
    if (i_phdr->p_align != (i_phdr->p_align & -i_phdr->p_align))
834
34.2M
      {
835
34.2M
        i_phdr->p_align &= -i_phdr->p_align;
836
34.2M
        if (!abfd->read_only)
837
8.53k
    {
838
8.53k
      _bfd_error_handler (_("warning: %pB has a program header "
839
8.53k
          "with invalid alignment"), abfd);
840
8.53k
      abfd->read_only = 1;
841
8.53k
    }
842
34.2M
      }
843
    /* Detect eu-strip -f debug files, which have program
844
       headers that describe the original file.  */
845
46.9M
    if (i_phdr->p_filesz != 0
846
46.9M
        && (i_phdr->p_filesz > filesize
847
36.6M
      || i_phdr->p_offset > filesize - i_phdr->p_filesz))
848
34.1M
      eu_strip_broken_phdrs = true;
849
46.9M
  }
850
177k
      if (!eu_strip_broken_phdrs
851
177k
    && i_ehdrp->e_shoff == 0
852
177k
    && i_ehdrp->e_shstrndx == 0)
853
1.15k
  {
854
    /* Try to reconstruct dynamic symbol table from PT_DYNAMIC
855
       segment if there is no section header.  */
856
1.15k
    i_phdr = elf_tdata (abfd)->phdr;
857
3.12k
    for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
858
2.24k
      if (i_phdr->p_type == PT_DYNAMIC)
859
276
        {
860
276
    if (i_phdr->p_filesz != 0
861
276
        && !_bfd_elf_get_dynamic_symbols (abfd, i_phdr,
862
228
                  elf_tdata (abfd)->phdr,
863
228
                  i_ehdrp->e_phnum,
864
228
                  filesize))
865
228
      goto got_no_match;
866
48
    break;
867
276
        }
868
1.15k
  }
869
177k
    }
870
871
459k
  if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff >= sizeof (x_ehdr))
872
132k
    {
873
132k
      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
132k
      num_sec = elf_numsections (abfd);
879
1.84M
      for (shindex = 1; shindex < num_sec; shindex++)
880
1.72M
  if (!bfd_section_from_shdr (abfd, shindex))
881
12.6k
    goto got_no_match;
882
883
      /* Set up ELF sections for SHF_GROUP and SHF_LINK_ORDER.  */
884
119k
      if (! _bfd_elf_setup_sections (abfd))
885
1.04k
  goto got_wrong_format_error;
886
119k
    }
887
888
  /* Let the backend double check the format and override global
889
     information.  */
890
446k
  if (ebd->elf_backend_object_p)
891
304k
    {
892
304k
      if (! (*ebd->elf_backend_object_p) (abfd))
893
41.6k
  goto got_wrong_format_error;
894
304k
    }
895
896
  /* Remember the entry point specified in the ELF file header.  */
897
404k
  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
1.11M
  for (s = abfd->sections; s != NULL; s = s->next)
902
710k
    {
903
710k
      if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL
904
710k
     || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
905
710k
    && elf_section_data (s)->this_hdr.sh_info > 0)
906
20.4k
  {
907
20.4k
    unsigned long targ_index;
908
20.4k
    asection *targ_sec;
909
910
20.4k
    targ_index = elf_section_data (s)->this_hdr.sh_info;
911
20.4k
    targ_sec = bfd_section_from_elf_index (abfd, targ_index);
912
20.4k
    if (targ_sec != NULL
913
20.4k
        && (targ_sec->flags & SEC_DEBUGGING) != 0)
914
494
      s->flags |= SEC_DEBUGGING;
915
20.4k
  }
916
710k
    }
917
404k
  return _bfd_no_cleanup;
918
919
227M
 got_wrong_format_error:
920
227M
  bfd_set_error (bfd_error_wrong_format);
921
922
227M
 got_no_match:
923
227M
  return NULL;
924
227M
}
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
1.15k
{
933
1.15k
  const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
934
1.15k
  bool *failedp = (bool *) data;
935
1.15k
  Elf_Internal_Shdr *rela_hdr;
936
1.15k
  bfd_vma addr_offset;
937
1.15k
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
938
1.15k
  size_t extsize;
939
1.15k
  bfd_byte *dst_rela;
940
1.15k
  unsigned int idx;
941
1.15k
  asymbol *last_sym;
942
1.15k
  int last_sym_idx;
943
1.15k
  size_t amt;
944
945
  /* If we have already failed, don't do anything.  */
946
1.15k
  if (*failedp)
947
0
    return;
948
949
1.15k
  if ((sec->flags & SEC_RELOC) == 0)
950
649
    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
509
  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
509
  if (sec->orelocation == NULL)
963
0
    return;
964
965
509
  rela_hdr = elf_section_data (sec)->rela.hdr;
966
509
  if (rela_hdr == NULL)
967
0
    rela_hdr = elf_section_data (sec)->rel.hdr;
968
969
509
  rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
970
509
  if (_bfd_mul_overflow (sec->reloc_count, rela_hdr->sh_entsize, &amt)
971
509
      || (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
509
  if (rela_hdr->sh_type == SHT_RELA)
980
509
    {
981
509
      swap_out = elf_swap_reloca_out;
982
509
      extsize = sizeof (Elf_External_Rela);
983
509
    }
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
509
  addr_offset = 0;
998
509
  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
509
  last_sym = 0;
1003
509
  last_sym_idx = 0;
1004
509
  dst_rela = rela_hdr->contents;
1005
1006
57.7k
  for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
1007
57.2k
    {
1008
57.2k
      Elf_Internal_Rela src_rela;
1009
57.2k
      arelent *ptr;
1010
57.2k
      asymbol *sym;
1011
57.2k
      int n;
1012
1013
57.2k
      ptr = sec->orelocation[idx];
1014
57.2k
      sym = *ptr->sym_ptr_ptr;
1015
57.2k
      if (sym == last_sym)
1016
32.6k
  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
24.5k
      else if (bfd_is_abs_section (sym->section)
1022
24.5k
         && sym->value == 0
1023
24.5k
         && (sym->flags & BSF_RELC) == 0)
1024
3
  n = STN_UNDEF;
1025
24.5k
      else
1026
24.5k
  {
1027
24.5k
    last_sym = sym;
1028
24.5k
    n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
1029
24.5k
    if (n < 0)
1030
0
      {
1031
0
        *failedp = true;
1032
0
        return;
1033
0
      }
1034
24.5k
    last_sym_idx = n;
1035
24.5k
  }
1036
1037
57.2k
      if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
1038
57.2k
    && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
1039
57.2k
    && ! _bfd_elf_validate_reloc (abfd, ptr))
1040
0
  {
1041
0
    *failedp = true;
1042
0
    return;
1043
0
  }
1044
1045
57.2k
      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
57.2k
      src_rela.r_offset = ptr->address + addr_offset;
1066
57.2k
      src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
1067
57.2k
      src_rela.r_addend = ptr->addend;
1068
57.2k
      (*swap_out) (abfd, &src_rela, dst_rela);
1069
57.2k
    }
1070
1071
509
  if (elf_section_data (sec)->has_secondary_relocs
1072
509
      && !bed->write_secondary_relocs (abfd, sec))
1073
0
    {
1074
0
      *failedp = true;
1075
0
      return;
1076
0
    }
1077
509
}
bfd_elf64_write_relocs
Line
Count
Source
932
988
{
933
988
  const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
934
988
  bool *failedp = (bool *) data;
935
988
  Elf_Internal_Shdr *rela_hdr;
936
988
  bfd_vma addr_offset;
937
988
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
938
988
  size_t extsize;
939
988
  bfd_byte *dst_rela;
940
988
  unsigned int idx;
941
988
  asymbol *last_sym;
942
988
  int last_sym_idx;
943
988
  size_t amt;
944
945
  /* If we have already failed, don't do anything.  */
946
988
  if (*failedp)
947
0
    return;
948
949
988
  if ((sec->flags & SEC_RELOC) == 0)
950
479
    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
509
  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
509
  if (sec->orelocation == NULL)
963
0
    return;
964
965
509
  rela_hdr = elf_section_data (sec)->rela.hdr;
966
509
  if (rela_hdr == NULL)
967
0
    rela_hdr = elf_section_data (sec)->rel.hdr;
968
969
509
  rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
970
509
  if (_bfd_mul_overflow (sec->reloc_count, rela_hdr->sh_entsize, &amt)
971
509
      || (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
509
  if (rela_hdr->sh_type == SHT_RELA)
980
509
    {
981
509
      swap_out = elf_swap_reloca_out;
982
509
      extsize = sizeof (Elf_External_Rela);
983
509
    }
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
509
  addr_offset = 0;
998
509
  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
509
  last_sym = 0;
1003
509
  last_sym_idx = 0;
1004
509
  dst_rela = rela_hdr->contents;
1005
1006
57.7k
  for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
1007
57.2k
    {
1008
57.2k
      Elf_Internal_Rela src_rela;
1009
57.2k
      arelent *ptr;
1010
57.2k
      asymbol *sym;
1011
57.2k
      int n;
1012
1013
57.2k
      ptr = sec->orelocation[idx];
1014
57.2k
      sym = *ptr->sym_ptr_ptr;
1015
57.2k
      if (sym == last_sym)
1016
32.6k
  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
24.5k
      else if (bfd_is_abs_section (sym->section)
1022
24.5k
         && sym->value == 0
1023
24.5k
         && (sym->flags & BSF_RELC) == 0)
1024
3
  n = STN_UNDEF;
1025
24.5k
      else
1026
24.5k
  {
1027
24.5k
    last_sym = sym;
1028
24.5k
    n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
1029
24.5k
    if (n < 0)
1030
0
      {
1031
0
        *failedp = true;
1032
0
        return;
1033
0
      }
1034
24.5k
    last_sym_idx = n;
1035
24.5k
  }
1036
1037
57.2k
      if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
1038
57.2k
    && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
1039
57.2k
    && ! _bfd_elf_validate_reloc (abfd, ptr))
1040
0
  {
1041
0
    *failedp = true;
1042
0
    return;
1043
0
  }
1044
1045
57.2k
      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
57.2k
      src_rela.r_offset = ptr->address + addr_offset;
1066
57.2k
      src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
1067
57.2k
      src_rela.r_addend = ptr->addend;
1068
57.2k
      (*swap_out) (abfd, &src_rela, dst_rela);
1069
57.2k
    }
1070
1071
509
  if (elf_section_data (sec)->has_secondary_relocs
1072
509
      && !bed->write_secondary_relocs (abfd, sec))
1073
0
    {
1074
0
      *failedp = true;
1075
0
      return;
1076
0
    }
1077
509
}
bfd_elf32_write_relocs
Line
Count
Source
932
170
{
933
170
  const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
934
170
  bool *failedp = (bool *) data;
935
170
  Elf_Internal_Shdr *rela_hdr;
936
170
  bfd_vma addr_offset;
937
170
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
938
170
  size_t extsize;
939
170
  bfd_byte *dst_rela;
940
170
  unsigned int idx;
941
170
  asymbol *last_sym;
942
170
  int last_sym_idx;
943
170
  size_t amt;
944
945
  /* If we have already failed, don't do anything.  */
946
170
  if (*failedp)
947
0
    return;
948
949
170
  if ((sec->flags & SEC_RELOC) == 0)
950
170
    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
6
{
1086
56
  while (count--)
1087
50
    {
1088
50
      Elf_External_Phdr extphdr;
1089
1090
50
      elf_swap_phdr_out (abfd, phdr, &extphdr);
1091
50
      if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), abfd)
1092
50
    != sizeof (Elf_External_Phdr))
1093
0
  return -1;
1094
50
      phdr++;
1095
50
    }
1096
6
  return 0;
1097
6
}
Unexecuted instantiation: bfd_elf64_write_out_phdrs
bfd_elf32_write_out_phdrs
Line
Count
Source
1085
6
{
1086
56
  while (count--)
1087
50
    {
1088
50
      Elf_External_Phdr extphdr;
1089
1090
50
      elf_swap_phdr_out (abfd, phdr, &extphdr);
1091
50
      if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), abfd)
1092
50
    != sizeof (Elf_External_Phdr))
1093
0
  return -1;
1094
50
      phdr++;
1095
50
    }
1096
6
  return 0;
1097
6
}
1098
1099
/* Write out the section headers and the ELF file header.  */
1100
1101
bool
1102
elf_write_shdrs_and_ehdr (bfd *abfd)
1103
27
{
1104
27
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
1105
27
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1106
27
  Elf_External_Shdr *x_shdrp;  /* Section header table, external form */
1107
27
  Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
1108
27
  unsigned int count;
1109
27
  size_t amt;
1110
1111
27
  i_ehdrp = elf_elfheader (abfd);
1112
27
  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
27
  elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
1120
27
  amt = sizeof (x_ehdr);
1121
27
  if (bfd_seek (abfd, 0, SEEK_SET) != 0
1122
27
      || bfd_write (&x_ehdr, amt, abfd) != amt)
1123
0
    return false;
1124
1125
27
  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
27
  if (i_ehdrp->e_phnum >= PN_XNUM)
1131
0
    i_shdrp[0]->sh_info = i_ehdrp->e_phnum;
1132
27
  if (i_ehdrp->e_shnum >= (SHN_LORESERVE & 0xffff))
1133
0
    i_shdrp[0]->sh_size = i_ehdrp->e_shnum;
1134
27
  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
27
  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
27
  x_shdrp = (Elf_External_Shdr *) bfd_alloc (abfd, amt);
1144
27
  if (!x_shdrp)
1145
0
    return false;
1146
1147
1.76k
  for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++)
1148
1.73k
    {
1149
#if DEBUG & 2
1150
      elf_debug_section (count, *i_shdrp);
1151
#endif
1152
1.73k
      elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count);
1153
1.73k
    }
1154
27
  amt = (bfd_size_type) i_ehdrp->e_shnum * sizeof (*x_shdrp);
1155
27
  if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0
1156
27
      || bfd_write (x_shdrp, amt, abfd) != amt)
1157
0
    return false;
1158
1159
  /* need to dump the string table too...  */
1160
1161
27
  return true;
1162
27
}
bfd_elf64_write_shdrs_and_ehdr
Line
Count
Source
1103
15
{
1104
15
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
1105
15
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1106
15
  Elf_External_Shdr *x_shdrp;  /* Section header table, external form */
1107
15
  Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
1108
15
  unsigned int count;
1109
15
  size_t amt;
1110
1111
15
  i_ehdrp = elf_elfheader (abfd);
1112
15
  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
15
  elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
1120
15
  amt = sizeof (x_ehdr);
1121
15
  if (bfd_seek (abfd, 0, SEEK_SET) != 0
1122
15
      || bfd_write (&x_ehdr, amt, abfd) != amt)
1123
0
    return false;
1124
1125
15
  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
15
  if (i_ehdrp->e_phnum >= PN_XNUM)
1131
0
    i_shdrp[0]->sh_info = i_ehdrp->e_phnum;
1132
15
  if (i_ehdrp->e_shnum >= (SHN_LORESERVE & 0xffff))
1133
0
    i_shdrp[0]->sh_size = i_ehdrp->e_shnum;
1134
15
  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
15
  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
15
  x_shdrp = (Elf_External_Shdr *) bfd_alloc (abfd, amt);
1144
15
  if (!x_shdrp)
1145
0
    return false;
1146
1147
1.55k
  for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++)
1148
1.53k
    {
1149
#if DEBUG & 2
1150
      elf_debug_section (count, *i_shdrp);
1151
#endif
1152
1.53k
      elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count);
1153
1.53k
    }
1154
15
  amt = (bfd_size_type) i_ehdrp->e_shnum * sizeof (*x_shdrp);
1155
15
  if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0
1156
15
      || bfd_write (x_shdrp, amt, abfd) != amt)
1157
0
    return false;
1158
1159
  /* need to dump the string table too...  */
1160
1161
15
  return true;
1162
15
}
bfd_elf32_write_shdrs_and_ehdr
Line
Count
Source
1103
12
{
1104
12
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
1105
12
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1106
12
  Elf_External_Shdr *x_shdrp;  /* Section header table, external form */
1107
12
  Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
1108
12
  unsigned int count;
1109
12
  size_t amt;
1110
1111
12
  i_ehdrp = elf_elfheader (abfd);
1112
12
  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
12
  elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
1120
12
  amt = sizeof (x_ehdr);
1121
12
  if (bfd_seek (abfd, 0, SEEK_SET) != 0
1122
12
      || bfd_write (&x_ehdr, amt, abfd) != amt)
1123
0
    return false;
1124
1125
12
  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
12
  if (i_ehdrp->e_phnum >= PN_XNUM)
1131
0
    i_shdrp[0]->sh_info = i_ehdrp->e_phnum;
1132
12
  if (i_ehdrp->e_shnum >= (SHN_LORESERVE & 0xffff))
1133
0
    i_shdrp[0]->sh_size = i_ehdrp->e_shnum;
1134
12
  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
12
  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
12
  x_shdrp = (Elf_External_Shdr *) bfd_alloc (abfd, amt);
1144
12
  if (!x_shdrp)
1145
0
    return false;
1146
1147
208
  for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++)
1148
196
    {
1149
#if DEBUG & 2
1150
      elf_debug_section (count, *i_shdrp);
1151
#endif
1152
196
      elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count);
1153
196
    }
1154
12
  amt = (bfd_size_type) i_ehdrp->e_shnum * sizeof (*x_shdrp);
1155
12
  if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0
1156
12
      || bfd_write (x_shdrp, amt, abfd) != amt)
1157
0
    return false;
1158
1159
  /* need to dump the string table too...  */
1160
1161
12
  return true;
1162
12
}
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
2.91k
{
1241
2.91k
  Elf_Internal_Shdr *hdr;
1242
2.91k
  Elf_Internal_Shdr *verhdr;
1243
2.91k
  unsigned long symcount; /* Number of external ELF symbols */
1244
2.91k
  elf_symbol_type *sym;   /* Pointer to current bfd symbol */
1245
2.91k
  elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
1246
2.91k
  Elf_Internal_Sym *isym;
1247
2.91k
  Elf_Internal_Sym *isymend;
1248
2.91k
  Elf_Internal_Sym *isymbuf = NULL;
1249
2.91k
  Elf_External_Versym *xver;
1250
2.91k
  Elf_External_Versym *xverbuf = NULL;
1251
2.91k
  const struct elf_backend_data *ebd;
1252
2.91k
  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
2.91k
  ebd = get_elf_backend_data (abfd);
1264
1265
2.91k
  if (! dynamic)
1266
2.77k
    {
1267
2.77k
      hdr = &elf_tdata (abfd)->symtab_hdr;
1268
2.77k
      verhdr = NULL;
1269
2.77k
      symcount = hdr->sh_size / ebd->s->sizeof_sym;
1270
2.77k
    }
1271
145
  else
1272
145
    {
1273
145
      hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1274
145
      if (elf_dynversym (abfd) == 0)
1275
51
  verhdr = NULL;
1276
94
      else
1277
94
  verhdr = &elf_tdata (abfd)->dynversym_hdr;
1278
145
      if ((elf_dynverdef (abfd) != 0
1279
145
     && elf_tdata (abfd)->verdef == NULL)
1280
145
    || (elf_dynverref (abfd) != 0
1281
140
        && elf_tdata (abfd)->verref == NULL)
1282
145
    || elf_tdata (abfd)->dt_verdef != NULL
1283
145
    || elf_tdata (abfd)->dt_verneed != NULL)
1284
45
  {
1285
45
    if (!_bfd_elf_slurp_version_tables (abfd, false))
1286
24
      return -1;
1287
45
  }
1288
1289
121
      symcount = elf_tdata (abfd)->dt_symtab_count;
1290
121
    }
1291
1292
2.89k
  if (symcount == 0)
1293
250
    symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1294
1295
2.89k
  if (symcount == 0)
1296
131
    sym = symbase = NULL;
1297
2.76k
  else
1298
2.76k
    {
1299
2.76k
      size_t i;
1300
1301
2.76k
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
1302
2.76k
              NULL, NULL, NULL);
1303
2.76k
      if (isymbuf == NULL)
1304
93
  return -1;
1305
1306
2.67k
      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
2.67k
      symbase = (elf_symbol_type *) bfd_zalloc (abfd, amt);
1312
2.67k
      if (symbase == (elf_symbol_type *) NULL)
1313
0
  goto error_return;
1314
1315
      /* Read the raw ELF version symbol information.  */
1316
2.67k
      if (verhdr != NULL
1317
2.67k
    && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
1318
7
  {
1319
7
    _bfd_error_handler
1320
      /* xgettext:c-format */
1321
7
      (_("%pB: version count (%" PRId64 ")"
1322
7
         " does not match symbol count (%ld)"),
1323
7
       abfd,
1324
7
       (int64_t) (verhdr->sh_size / sizeof (Elf_External_Versym)),
1325
7
       symcount);
1326
1327
    /* Slurp in the symbols without the version information,
1328
       since that is more helpful than just quitting.  */
1329
7
    verhdr = NULL;
1330
7
  }
1331
1332
2.67k
      if (verhdr != NULL)
1333
74
  {
1334
74
    if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
1335
0
      goto error_return;
1336
74
    xverbuf = (Elf_External_Versym *)
1337
74
      _bfd_malloc_and_read (abfd, verhdr->sh_size, verhdr->sh_size);
1338
74
    if (xverbuf == NULL && verhdr->sh_size != 0)
1339
1
      goto error_return;
1340
74
  }
1341
1342
      /* Skip first symbol, which is a null dummy.  */
1343
2.66k
      xver = xverbuf;
1344
2.66k
      if (xver != NULL)
1345
73
  ++xver;
1346
2.66k
      isymend = isymbuf + symcount;
1347
2.66k
      for (isym = isymbuf + 1, sym = symbase, i = 1;
1348
645k
     isym < isymend;
1349
642k
     isym++, sym++, i++)
1350
642k
  {
1351
642k
    memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
1352
1353
642k
    sym->symbol.the_bfd = abfd;
1354
642k
    if (elf_use_dt_symtab_p (abfd))
1355
0
      sym->symbol.name = (elf_tdata (abfd)->dt_strtab
1356
0
        + isym->st_name);
1357
642k
    else
1358
642k
      sym->symbol.name = bfd_elf_sym_name (abfd, hdr, isym, NULL);
1359
642k
    sym->symbol.value = isym->st_value;
1360
1361
642k
    if (isym->st_shndx == SHN_UNDEF)
1362
237k
      {
1363
237k
        sym->symbol.section = bfd_und_section_ptr;
1364
237k
      }
1365
405k
    else if (isym->st_shndx == SHN_ABS)
1366
9.04k
      {
1367
9.04k
        sym->symbol.section = bfd_abs_section_ptr;
1368
9.04k
      }
1369
396k
    else if (isym->st_shndx == SHN_COMMON)
1370
117
      {
1371
117
        sym->symbol.section = bfd_com_section_ptr;
1372
117
        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
117
        sym->symbol.value = isym->st_size;
1391
117
      }
1392
396k
    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
396k
    else
1402
396k
      {
1403
396k
        sym->symbol.section
1404
396k
    = bfd_section_from_elf_index (abfd, isym->st_shndx);
1405
396k
        if (sym->symbol.section == NULL)
1406
182k
    {
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
182k
      sym->symbol.section = bfd_abs_section_ptr;
1414
182k
    }
1415
396k
      }
1416
1417
    /* If this is a relocatable file, then the symbol value is
1418
       already section relative.  */
1419
642k
    if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1420
275k
      sym->symbol.value -= sym->symbol.section->vma;
1421
1422
642k
    switch (ELF_ST_BIND (isym->st_info))
1423
642k
      {
1424
378k
      case STB_LOCAL:
1425
378k
        sym->symbol.flags |= BSF_LOCAL;
1426
378k
        break;
1427
142k
      case STB_GLOBAL:
1428
142k
        if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON)
1429
75.8k
    sym->symbol.flags |= BSF_GLOBAL;
1430
142k
        break;
1431
19.3k
      case STB_WEAK:
1432
19.3k
        sym->symbol.flags |= BSF_WEAK;
1433
19.3k
        break;
1434
5.91k
      case STB_GNU_UNIQUE:
1435
5.91k
        sym->symbol.flags |= BSF_GNU_UNIQUE;
1436
5.91k
        break;
1437
642k
      }
1438
1439
642k
    switch (ELF_ST_TYPE (isym->st_info))
1440
642k
      {
1441
47.5k
      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
47.5k
        sym->symbol.flags |= (BSF_SECTION_SYM
1447
47.5k
            | BSF_DEBUGGING
1448
47.5k
            | BSF_SECTION_SYM_USED);
1449
47.5k
        break;
1450
18.3k
      case STT_FILE:
1451
18.3k
        sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
1452
18.3k
        break;
1453
121k
      case STT_FUNC:
1454
121k
        sym->symbol.flags |= BSF_FUNCTION;
1455
121k
        break;
1456
9.85k
      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
9.85k
        sym->symbol.flags |= BSF_ELF_COMMON;
1460
        /* Fall through.  */
1461
106k
      case STT_OBJECT:
1462
106k
        sym->symbol.flags |= BSF_OBJECT;
1463
106k
        break;
1464
8.87k
      case STT_TLS:
1465
8.87k
        sym->symbol.flags |= BSF_THREAD_LOCAL;
1466
8.87k
        break;
1467
9.12k
      case STT_RELC:
1468
9.12k
        sym->symbol.flags |= BSF_RELC;
1469
9.12k
        break;
1470
7.68k
      case STT_SRELC:
1471
7.68k
        sym->symbol.flags |= BSF_SRELC;
1472
7.68k
        break;
1473
10.2k
      case STT_GNU_IFUNC:
1474
10.2k
        sym->symbol.flags |= BSF_GNU_INDIRECT_FUNCTION;
1475
10.2k
        break;
1476
642k
      }
1477
1478
642k
    if (dynamic)
1479
62.8k
      sym->symbol.flags |= BSF_DYNAMIC;
1480
1481
642k
    if (elf_tdata (abfd)->dt_versym)
1482
0
      sym->version = bfd_get_16 (abfd,
1483
642k
               elf_tdata (abfd)->dt_versym + 2 * i);
1484
642k
    else if (xver != NULL)
1485
54.8k
      {
1486
54.8k
        Elf_Internal_Versym iversym;
1487
1488
54.8k
        _bfd_elf_swap_versym_in (abfd, xver, &iversym);
1489
54.8k
        sym->version = iversym.vs_vers;
1490
54.8k
        xver++;
1491
54.8k
      }
1492
1493
    /* Do some backend-specific processing on this symbol.  */
1494
642k
    if (ebd->elf_backend_symbol_processing)
1495
316k
      (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
1496
642k
  }
1497
2.66k
    }
1498
1499
  /* Do some backend-specific processing on this symbol table.  */
1500
2.80k
  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
2.80k
  symcount = sym - symbase;
1506
1507
  /* Fill in the user's symbol pointer vector if needed.  */
1508
2.80k
  if (symptrs)
1509
2.80k
    {
1510
2.80k
      long l = symcount;
1511
1512
2.80k
      sym = symbase;
1513
645k
      while (l-- > 0)
1514
642k
  {
1515
642k
    *symptrs++ = &sym->symbol;
1516
642k
    sym++;
1517
642k
  }
1518
2.80k
      *symptrs = 0;   /* Final null pointer */
1519
2.80k
    }
1520
1521
2.80k
  free (xverbuf);
1522
2.80k
  if (hdr->contents != (unsigned char *) isymbuf
1523
2.80k
      && !elf_use_dt_symtab_p (abfd))
1524
2.66k
    free (isymbuf);
1525
2.80k
  return symcount;
1526
1527
1
 error_return:
1528
1
  free (xverbuf);
1529
1
  if (hdr->contents != (unsigned char *) isymbuf
1530
1
      && !elf_use_dt_symtab_p (abfd))
1531
1
    free (isymbuf);
1532
1
  return -1;
1533
2.89k
}
bfd_elf64_slurp_symbol_table
Line
Count
Source
1240
2.60k
{
1241
2.60k
  Elf_Internal_Shdr *hdr;
1242
2.60k
  Elf_Internal_Shdr *verhdr;
1243
2.60k
  unsigned long symcount; /* Number of external ELF symbols */
1244
2.60k
  elf_symbol_type *sym;   /* Pointer to current bfd symbol */
1245
2.60k
  elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
1246
2.60k
  Elf_Internal_Sym *isym;
1247
2.60k
  Elf_Internal_Sym *isymend;
1248
2.60k
  Elf_Internal_Sym *isymbuf = NULL;
1249
2.60k
  Elf_External_Versym *xver;
1250
2.60k
  Elf_External_Versym *xverbuf = NULL;
1251
2.60k
  const struct elf_backend_data *ebd;
1252
2.60k
  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
2.60k
  ebd = get_elf_backend_data (abfd);
1264
1265
2.60k
  if (! dynamic)
1266
2.53k
    {
1267
2.53k
      hdr = &elf_tdata (abfd)->symtab_hdr;
1268
2.53k
      verhdr = NULL;
1269
2.53k
      symcount = hdr->sh_size / ebd->s->sizeof_sym;
1270
2.53k
    }
1271
71
  else
1272
71
    {
1273
71
      hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1274
71
      if (elf_dynversym (abfd) == 0)
1275
18
  verhdr = NULL;
1276
53
      else
1277
53
  verhdr = &elf_tdata (abfd)->dynversym_hdr;
1278
71
      if ((elf_dynverdef (abfd) != 0
1279
71
     && elf_tdata (abfd)->verdef == NULL)
1280
71
    || (elf_dynverref (abfd) != 0
1281
66
        && elf_tdata (abfd)->verref == NULL)
1282
71
    || elf_tdata (abfd)->dt_verdef != NULL
1283
71
    || elf_tdata (abfd)->dt_verneed != NULL)
1284
27
  {
1285
27
    if (!_bfd_elf_slurp_version_tables (abfd, false))
1286
19
      return -1;
1287
27
  }
1288
1289
52
      symcount = elf_tdata (abfd)->dt_symtab_count;
1290
52
    }
1291
1292
2.58k
  if (symcount == 0)
1293
130
    symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1294
1295
2.58k
  if (symcount == 0)
1296
80
    sym = symbase = NULL;
1297
2.50k
  else
1298
2.50k
    {
1299
2.50k
      size_t i;
1300
1301
2.50k
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
1302
2.50k
              NULL, NULL, NULL);
1303
2.50k
      if (isymbuf == NULL)
1304
76
  return -1;
1305
1306
2.43k
      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
2.43k
      symbase = (elf_symbol_type *) bfd_zalloc (abfd, amt);
1312
2.43k
      if (symbase == (elf_symbol_type *) NULL)
1313
0
  goto error_return;
1314
1315
      /* Read the raw ELF version symbol information.  */
1316
2.43k
      if (verhdr != NULL
1317
2.43k
    && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
1318
4
  {
1319
4
    _bfd_error_handler
1320
      /* xgettext:c-format */
1321
4
      (_("%pB: version count (%" PRId64 ")"
1322
4
         " does not match symbol count (%ld)"),
1323
4
       abfd,
1324
4
       (int64_t) (verhdr->sh_size / sizeof (Elf_External_Versym)),
1325
4
       symcount);
1326
1327
    /* Slurp in the symbols without the version information,
1328
       since that is more helpful than just quitting.  */
1329
4
    verhdr = NULL;
1330
4
  }
1331
1332
2.43k
      if (verhdr != NULL)
1333
36
  {
1334
36
    if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
1335
0
      goto error_return;
1336
36
    xverbuf = (Elf_External_Versym *)
1337
36
      _bfd_malloc_and_read (abfd, verhdr->sh_size, verhdr->sh_size);
1338
36
    if (xverbuf == NULL && verhdr->sh_size != 0)
1339
1
      goto error_return;
1340
36
  }
1341
1342
      /* Skip first symbol, which is a null dummy.  */
1343
2.42k
      xver = xverbuf;
1344
2.42k
      if (xver != NULL)
1345
35
  ++xver;
1346
2.42k
      isymend = isymbuf + symcount;
1347
2.42k
      for (isym = isymbuf + 1, sym = symbase, i = 1;
1348
460k
     isym < isymend;
1349
458k
     isym++, sym++, i++)
1350
458k
  {
1351
458k
    memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
1352
1353
458k
    sym->symbol.the_bfd = abfd;
1354
458k
    if (elf_use_dt_symtab_p (abfd))
1355
0
      sym->symbol.name = (elf_tdata (abfd)->dt_strtab
1356
0
        + isym->st_name);
1357
458k
    else
1358
458k
      sym->symbol.name = bfd_elf_sym_name (abfd, hdr, isym, NULL);
1359
458k
    sym->symbol.value = isym->st_value;
1360
1361
458k
    if (isym->st_shndx == SHN_UNDEF)
1362
197k
      {
1363
197k
        sym->symbol.section = bfd_und_section_ptr;
1364
197k
      }
1365
261k
    else if (isym->st_shndx == SHN_ABS)
1366
1.59k
      {
1367
1.59k
        sym->symbol.section = bfd_abs_section_ptr;
1368
1.59k
      }
1369
259k
    else if (isym->st_shndx == SHN_COMMON)
1370
116
      {
1371
116
        sym->symbol.section = bfd_com_section_ptr;
1372
116
        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
116
        sym->symbol.value = isym->st_size;
1391
116
      }
1392
259k
    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
259k
    else
1402
259k
      {
1403
259k
        sym->symbol.section
1404
259k
    = bfd_section_from_elf_index (abfd, isym->st_shndx);
1405
259k
        if (sym->symbol.section == NULL)
1406
137k
    {
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
137k
      sym->symbol.section = bfd_abs_section_ptr;
1414
137k
    }
1415
259k
      }
1416
1417
    /* If this is a relocatable file, then the symbol value is
1418
       already section relative.  */
1419
458k
    if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1420
100k
      sym->symbol.value -= sym->symbol.section->vma;
1421
1422
458k
    switch (ELF_ST_BIND (isym->st_info))
1423
458k
      {
1424
308k
      case STB_LOCAL:
1425
308k
        sym->symbol.flags |= BSF_LOCAL;
1426
308k
        break;
1427
59.2k
      case STB_GLOBAL:
1428
59.2k
        if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON)
1429
22.9k
    sym->symbol.flags |= BSF_GLOBAL;
1430
59.2k
        break;
1431
12.3k
      case STB_WEAK:
1432
12.3k
        sym->symbol.flags |= BSF_WEAK;
1433
12.3k
        break;
1434
5.29k
      case STB_GNU_UNIQUE:
1435
5.29k
        sym->symbol.flags |= BSF_GNU_UNIQUE;
1436
5.29k
        break;
1437
458k
      }
1438
1439
458k
    switch (ELF_ST_TYPE (isym->st_info))
1440
458k
      {
1441
40.3k
      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
40.3k
        sym->symbol.flags |= (BSF_SECTION_SYM
1447
40.3k
            | BSF_DEBUGGING
1448
40.3k
            | BSF_SECTION_SYM_USED);
1449
40.3k
        break;
1450
8.87k
      case STT_FILE:
1451
8.87k
        sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
1452
8.87k
        break;
1453
32.8k
      case STT_FUNC:
1454
32.8k
        sym->symbol.flags |= BSF_FUNCTION;
1455
32.8k
        break;
1456
6.50k
      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
6.50k
        sym->symbol.flags |= BSF_ELF_COMMON;
1460
        /* Fall through.  */
1461
71.1k
      case STT_OBJECT:
1462
71.1k
        sym->symbol.flags |= BSF_OBJECT;
1463
71.1k
        break;
1464
7.63k
      case STT_TLS:
1465
7.63k
        sym->symbol.flags |= BSF_THREAD_LOCAL;
1466
7.63k
        break;
1467
6.98k
      case STT_RELC:
1468
6.98k
        sym->symbol.flags |= BSF_RELC;
1469
6.98k
        break;
1470
5.65k
      case STT_SRELC:
1471
5.65k
        sym->symbol.flags |= BSF_SRELC;
1472
5.65k
        break;
1473
6.77k
      case STT_GNU_IFUNC:
1474
6.77k
        sym->symbol.flags |= BSF_GNU_INDIRECT_FUNCTION;
1475
6.77k
        break;
1476
458k
      }
1477
1478
458k
    if (dynamic)
1479
20.2k
      sym->symbol.flags |= BSF_DYNAMIC;
1480
1481
458k
    if (elf_tdata (abfd)->dt_versym)
1482
0
      sym->version = bfd_get_16 (abfd,
1483
458k
               elf_tdata (abfd)->dt_versym + 2 * i);
1484
458k
    else if (xver != NULL)
1485
19.9k
      {
1486
19.9k
        Elf_Internal_Versym iversym;
1487
1488
19.9k
        _bfd_elf_swap_versym_in (abfd, xver, &iversym);
1489
19.9k
        sym->version = iversym.vs_vers;
1490
19.9k
        xver++;
1491
19.9k
      }
1492
1493
    /* Do some backend-specific processing on this symbol.  */
1494
458k
    if (ebd->elf_backend_symbol_processing)
1495
304k
      (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
1496
458k
  }
1497
2.42k
    }
1498
1499
  /* Do some backend-specific processing on this symbol table.  */
1500
2.50k
  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
2.50k
  symcount = sym - symbase;
1506
1507
  /* Fill in the user's symbol pointer vector if needed.  */
1508
2.50k
  if (symptrs)
1509
2.50k
    {
1510
2.50k
      long l = symcount;
1511
1512
2.50k
      sym = symbase;
1513
460k
      while (l-- > 0)
1514
458k
  {
1515
458k
    *symptrs++ = &sym->symbol;
1516
458k
    sym++;
1517
458k
  }
1518
2.50k
      *symptrs = 0;   /* Final null pointer */
1519
2.50k
    }
1520
1521
2.50k
  free (xverbuf);
1522
2.50k
  if (hdr->contents != (unsigned char *) isymbuf
1523
2.50k
      && !elf_use_dt_symtab_p (abfd))
1524
2.42k
    free (isymbuf);
1525
2.50k
  return symcount;
1526
1527
1
 error_return:
1528
1
  free (xverbuf);
1529
1
  if (hdr->contents != (unsigned char *) isymbuf
1530
1
      && !elf_use_dt_symtab_p (abfd))
1531
1
    free (isymbuf);
1532
1
  return -1;
1533
2.58k
}
bfd_elf32_slurp_symbol_table
Line
Count
Source
1240
313
{
1241
313
  Elf_Internal_Shdr *hdr;
1242
313
  Elf_Internal_Shdr *verhdr;
1243
313
  unsigned long symcount; /* Number of external ELF symbols */
1244
313
  elf_symbol_type *sym;   /* Pointer to current bfd symbol */
1245
313
  elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
1246
313
  Elf_Internal_Sym *isym;
1247
313
  Elf_Internal_Sym *isymend;
1248
313
  Elf_Internal_Sym *isymbuf = NULL;
1249
313
  Elf_External_Versym *xver;
1250
313
  Elf_External_Versym *xverbuf = NULL;
1251
313
  const struct elf_backend_data *ebd;
1252
313
  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
313
  ebd = get_elf_backend_data (abfd);
1264
1265
313
  if (! dynamic)
1266
239
    {
1267
239
      hdr = &elf_tdata (abfd)->symtab_hdr;
1268
239
      verhdr = NULL;
1269
239
      symcount = hdr->sh_size / ebd->s->sizeof_sym;
1270
239
    }
1271
74
  else
1272
74
    {
1273
74
      hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1274
74
      if (elf_dynversym (abfd) == 0)
1275
33
  verhdr = NULL;
1276
41
      else
1277
41
  verhdr = &elf_tdata (abfd)->dynversym_hdr;
1278
74
      if ((elf_dynverdef (abfd) != 0
1279
74
     && elf_tdata (abfd)->verdef == NULL)
1280
74
    || (elf_dynverref (abfd) != 0
1281
74
        && elf_tdata (abfd)->verref == NULL)
1282
74
    || elf_tdata (abfd)->dt_verdef != NULL
1283
74
    || elf_tdata (abfd)->dt_verneed != NULL)
1284
18
  {
1285
18
    if (!_bfd_elf_slurp_version_tables (abfd, false))
1286
5
      return -1;
1287
18
  }
1288
1289
69
      symcount = elf_tdata (abfd)->dt_symtab_count;
1290
69
    }
1291
1292
308
  if (symcount == 0)
1293
120
    symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1294
1295
308
  if (symcount == 0)
1296
51
    sym = symbase = NULL;
1297
257
  else
1298
257
    {
1299
257
      size_t i;
1300
1301
257
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
1302
257
              NULL, NULL, NULL);
1303
257
      if (isymbuf == NULL)
1304
17
  return -1;
1305
1306
240
      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
240
      symbase = (elf_symbol_type *) bfd_zalloc (abfd, amt);
1312
240
      if (symbase == (elf_symbol_type *) NULL)
1313
0
  goto error_return;
1314
1315
      /* Read the raw ELF version symbol information.  */
1316
240
      if (verhdr != NULL
1317
240
    && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
1318
3
  {
1319
3
    _bfd_error_handler
1320
      /* xgettext:c-format */
1321
3
      (_("%pB: version count (%" PRId64 ")"
1322
3
         " does not match symbol count (%ld)"),
1323
3
       abfd,
1324
3
       (int64_t) (verhdr->sh_size / sizeof (Elf_External_Versym)),
1325
3
       symcount);
1326
1327
    /* Slurp in the symbols without the version information,
1328
       since that is more helpful than just quitting.  */
1329
3
    verhdr = NULL;
1330
3
  }
1331
1332
240
      if (verhdr != NULL)
1333
38
  {
1334
38
    if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
1335
0
      goto error_return;
1336
38
    xverbuf = (Elf_External_Versym *)
1337
38
      _bfd_malloc_and_read (abfd, verhdr->sh_size, verhdr->sh_size);
1338
38
    if (xverbuf == NULL && verhdr->sh_size != 0)
1339
0
      goto error_return;
1340
38
  }
1341
1342
      /* Skip first symbol, which is a null dummy.  */
1343
240
      xver = xverbuf;
1344
240
      if (xver != NULL)
1345
38
  ++xver;
1346
240
      isymend = isymbuf + symcount;
1347
240
      for (isym = isymbuf + 1, sym = symbase, i = 1;
1348
184k
     isym < isymend;
1349
184k
     isym++, sym++, i++)
1350
184k
  {
1351
184k
    memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
1352
1353
184k
    sym->symbol.the_bfd = abfd;
1354
184k
    if (elf_use_dt_symtab_p (abfd))
1355
0
      sym->symbol.name = (elf_tdata (abfd)->dt_strtab
1356
0
        + isym->st_name);
1357
184k
    else
1358
184k
      sym->symbol.name = bfd_elf_sym_name (abfd, hdr, isym, NULL);
1359
184k
    sym->symbol.value = isym->st_value;
1360
1361
184k
    if (isym->st_shndx == SHN_UNDEF)
1362
40.2k
      {
1363
40.2k
        sym->symbol.section = bfd_und_section_ptr;
1364
40.2k
      }
1365
144k
    else if (isym->st_shndx == SHN_ABS)
1366
7.44k
      {
1367
7.44k
        sym->symbol.section = bfd_abs_section_ptr;
1368
7.44k
      }
1369
136k
    else if (isym->st_shndx == SHN_COMMON)
1370
1
      {
1371
1
        sym->symbol.section = bfd_com_section_ptr;
1372
1
        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
1
        sym->symbol.value = isym->st_size;
1391
1
      }
1392
136k
    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
136k
    else
1402
136k
      {
1403
136k
        sym->symbol.section
1404
136k
    = bfd_section_from_elf_index (abfd, isym->st_shndx);
1405
136k
        if (sym->symbol.section == NULL)
1406
45.2k
    {
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.2k
      sym->symbol.section = bfd_abs_section_ptr;
1414
45.2k
    }
1415
136k
      }
1416
1417
    /* If this is a relocatable file, then the symbol value is
1418
       already section relative.  */
1419
184k
    if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1420
174k
      sym->symbol.value -= sym->symbol.section->vma;
1421
1422
184k
    switch (ELF_ST_BIND (isym->st_info))
1423
184k
      {
1424
70.4k
      case STB_LOCAL:
1425
70.4k
        sym->symbol.flags |= BSF_LOCAL;
1426
70.4k
        break;
1427
83.5k
      case STB_GLOBAL:
1428
83.5k
        if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON)
1429
52.9k
    sym->symbol.flags |= BSF_GLOBAL;
1430
83.5k
        break;
1431
7.02k
      case STB_WEAK:
1432
7.02k
        sym->symbol.flags |= BSF_WEAK;
1433
7.02k
        break;
1434
614
      case STB_GNU_UNIQUE:
1435
614
        sym->symbol.flags |= BSF_GNU_UNIQUE;
1436
614
        break;
1437
184k
      }
1438
1439
184k
    switch (ELF_ST_TYPE (isym->st_info))
1440
184k
      {
1441
7.17k
      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
7.17k
        sym->symbol.flags |= (BSF_SECTION_SYM
1447
7.17k
            | BSF_DEBUGGING
1448
7.17k
            | BSF_SECTION_SYM_USED);
1449
7.17k
        break;
1450
9.46k
      case STT_FILE:
1451
9.46k
        sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
1452
9.46k
        break;
1453
88.9k
      case STT_FUNC:
1454
88.9k
        sym->symbol.flags |= BSF_FUNCTION;
1455
88.9k
        break;
1456
3.34k
      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
3.34k
        sym->symbol.flags |= BSF_ELF_COMMON;
1460
        /* Fall through.  */
1461
35.7k
      case STT_OBJECT:
1462
35.7k
        sym->symbol.flags |= BSF_OBJECT;
1463
35.7k
        break;
1464
1.24k
      case STT_TLS:
1465
1.24k
        sym->symbol.flags |= BSF_THREAD_LOCAL;
1466
1.24k
        break;
1467
2.13k
      case STT_RELC:
1468
2.13k
        sym->symbol.flags |= BSF_RELC;
1469
2.13k
        break;
1470
2.02k
      case STT_SRELC:
1471
2.02k
        sym->symbol.flags |= BSF_SRELC;
1472
2.02k
        break;
1473
3.49k
      case STT_GNU_IFUNC:
1474
3.49k
        sym->symbol.flags |= BSF_GNU_INDIRECT_FUNCTION;
1475
3.49k
        break;
1476
184k
      }
1477
1478
184k
    if (dynamic)
1479
42.5k
      sym->symbol.flags |= BSF_DYNAMIC;
1480
1481
184k
    if (elf_tdata (abfd)->dt_versym)
1482
0
      sym->version = bfd_get_16 (abfd,
1483
184k
               elf_tdata (abfd)->dt_versym + 2 * i);
1484
184k
    else if (xver != NULL)
1485
34.9k
      {
1486
34.9k
        Elf_Internal_Versym iversym;
1487
1488
34.9k
        _bfd_elf_swap_versym_in (abfd, xver, &iversym);
1489
34.9k
        sym->version = iversym.vs_vers;
1490
34.9k
        xver++;
1491
34.9k
      }
1492
1493
    /* Do some backend-specific processing on this symbol.  */
1494
184k
    if (ebd->elf_backend_symbol_processing)
1495
12.6k
      (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
1496
184k
  }
1497
240
    }
1498
1499
  /* Do some backend-specific processing on this symbol table.  */
1500
291
  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
291
  symcount = sym - symbase;
1506
1507
  /* Fill in the user's symbol pointer vector if needed.  */
1508
291
  if (symptrs)
1509
291
    {
1510
291
      long l = symcount;
1511
1512
291
      sym = symbase;
1513
184k
      while (l-- > 0)
1514
184k
  {
1515
184k
    *symptrs++ = &sym->symbol;
1516
184k
    sym++;
1517
184k
  }
1518
291
      *symptrs = 0;   /* Final null pointer */
1519
291
    }
1520
1521
291
  free (xverbuf);
1522
291
  if (hdr->contents != (unsigned char *) isymbuf
1523
291
      && !elf_use_dt_symtab_p (abfd))
1524
240
    free (isymbuf);
1525
291
  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
308
}
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
14.4k
{
1547
14.4k
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
1548
14.4k
  void *allocated = NULL;
1549
14.4k
  bfd_byte *native_relocs;
1550
14.4k
  arelent *relent;
1551
14.4k
  unsigned int i;
1552
14.4k
  int entsize;
1553
14.4k
  unsigned int symcount;
1554
1555
14.4k
  if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
1556
389
    return false;
1557
14.0k
  allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
1558
14.0k
  if (allocated == NULL)
1559
390
    return false;
1560
1561
13.6k
  native_relocs = (bfd_byte *) allocated;
1562
1563
13.6k
  entsize = rel_hdr->sh_entsize;
1564
13.6k
  BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
1565
13.6k
        || entsize == sizeof (Elf_External_Rela));
1566
1567
13.6k
  if (dynamic)
1568
181
    symcount = bfd_get_dynamic_symcount (abfd);
1569
13.4k
  else
1570
13.4k
    symcount = bfd_get_symcount (abfd);
1571
1572
13.6k
  for (i = 0, relent = relents;
1573
759k
       i < reloc_count;
1574
746k
       i++, relent++, native_relocs += entsize)
1575
747k
    {
1576
747k
      bool res;
1577
747k
      Elf_Internal_Rela rela;
1578
1579
747k
      if (entsize == sizeof (Elf_External_Rela))
1580
730k
  elf_swap_reloca_in (abfd, native_relocs, &rela);
1581
16.4k
      else
1582
16.4k
  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
747k
      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
1589
745k
  relent->address = rela.r_offset;
1590
2.19k
      else
1591
2.19k
  relent->address = rela.r_offset - asect->vma;
1592
1593
747k
      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
18.0k
  relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1597
729k
      else if (ELF_R_SYM (rela.r_info) > symcount)
1598
26.8k
  {
1599
26.8k
    _bfd_error_handler
1600
      /* xgettext:c-format */
1601
26.8k
      (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
1602
26.8k
       abfd, asect, i, (long) ELF_R_SYM (rela.r_info));
1603
26.8k
    bfd_set_error (bfd_error_bad_value);
1604
26.8k
    relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1605
26.8k
  }
1606
702k
      else
1607
702k
  {
1608
702k
    asymbol **ps;
1609
1610
702k
    ps = symbols + ELF_R_SYM (rela.r_info) - 1;
1611
1612
702k
    relent->sym_ptr_ptr = ps;
1613
702k
  }
1614
1615
747k
      relent->addend = rela.r_addend;
1616
1617
747k
      if ((entsize == sizeof (Elf_External_Rela)
1618
747k
     && ebd->elf_info_to_howto != NULL)
1619
747k
    || ebd->elf_info_to_howto_rel == NULL)
1620
730k
  res = ebd->elf_info_to_howto (abfd, relent, &rela);
1621
16.5k
      else
1622
16.5k
  res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
1623
1624
747k
      if (! res || relent->howto == NULL)
1625
1.13k
  goto error_return;
1626
747k
    }
1627
1628
12.5k
  free (allocated);
1629
12.5k
  return true;
1630
1631
1.13k
 error_return:
1632
1.13k
  free (allocated);
1633
1.13k
  return false;
1634
13.6k
}
elf64.c:elf_slurp_reloc_table_from_section
Line
Count
Source
1546
14.2k
{
1547
14.2k
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
1548
14.2k
  void *allocated = NULL;
1549
14.2k
  bfd_byte *native_relocs;
1550
14.2k
  arelent *relent;
1551
14.2k
  unsigned int i;
1552
14.2k
  int entsize;
1553
14.2k
  unsigned int symcount;
1554
1555
14.2k
  if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
1556
389
    return false;
1557
13.9k
  allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
1558
13.9k
  if (allocated == NULL)
1559
390
    return false;
1560
1561
13.5k
  native_relocs = (bfd_byte *) allocated;
1562
1563
13.5k
  entsize = rel_hdr->sh_entsize;
1564
13.5k
  BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
1565
13.5k
        || entsize == sizeof (Elf_External_Rela));
1566
1567
13.5k
  if (dynamic)
1568
39
    symcount = bfd_get_dynamic_symcount (abfd);
1569
13.4k
  else
1570
13.4k
    symcount = bfd_get_symcount (abfd);
1571
1572
13.5k
  for (i = 0, relent = relents;
1573
738k
       i < reloc_count;
1574
725k
       i++, relent++, native_relocs += entsize)
1575
726k
    {
1576
726k
      bool res;
1577
726k
      Elf_Internal_Rela rela;
1578
1579
726k
      if (entsize == sizeof (Elf_External_Rela))
1580
726k
  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
726k
      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
1589
724k
  relent->address = rela.r_offset;
1590
2.19k
      else
1591
2.19k
  relent->address = rela.r_offset - asect->vma;
1592
1593
726k
      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
17.0k
  relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1597
709k
      else if (ELF_R_SYM (rela.r_info) > symcount)
1598
26.6k
  {
1599
26.6k
    _bfd_error_handler
1600
      /* xgettext:c-format */
1601
26.6k
      (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
1602
26.6k
       abfd, asect, i, (long) ELF_R_SYM (rela.r_info));
1603
26.6k
    bfd_set_error (bfd_error_bad_value);
1604
26.6k
    relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1605
26.6k
  }
1606
682k
      else
1607
682k
  {
1608
682k
    asymbol **ps;
1609
1610
682k
    ps = symbols + ELF_R_SYM (rela.r_info) - 1;
1611
1612
682k
    relent->sym_ptr_ptr = ps;
1613
682k
  }
1614
1615
726k
      relent->addend = rela.r_addend;
1616
1617
726k
      if ((entsize == sizeof (Elf_External_Rela)
1618
726k
     && ebd->elf_info_to_howto != NULL)
1619
726k
    || ebd->elf_info_to_howto_rel == NULL)
1620
726k
  res = ebd->elf_info_to_howto (abfd, relent, &rela);
1621
0
      else
1622
0
  res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
1623
1624
726k
      if (! res || relent->howto == NULL)
1625
1.12k
  goto error_return;
1626
726k
    }
1627
1628
12.3k
  free (allocated);
1629
12.3k
  return true;
1630
1631
1.12k
 error_return:
1632
1.12k
  free (allocated);
1633
1.12k
  return false;
1634
13.5k
}
elf32.c:elf_slurp_reloc_table_from_section
Line
Count
Source
1546
142
{
1547
142
  const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
1548
142
  void *allocated = NULL;
1549
142
  bfd_byte *native_relocs;
1550
142
  arelent *relent;
1551
142
  unsigned int i;
1552
142
  int entsize;
1553
142
  unsigned int symcount;
1554
1555
142
  if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
1556
0
    return false;
1557
142
  allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
1558
142
  if (allocated == NULL)
1559
0
    return false;
1560
1561
142
  native_relocs = (bfd_byte *) allocated;
1562
1563
142
  entsize = rel_hdr->sh_entsize;
1564
142
  BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
1565
142
        || entsize == sizeof (Elf_External_Rela));
1566
1567
142
  if (dynamic)
1568
142
    symcount = bfd_get_dynamic_symcount (abfd);
1569
0
  else
1570
0
    symcount = bfd_get_symcount (abfd);
1571
1572
142
  for (i = 0, relent = relents;
1573
20.8k
       i < reloc_count;
1574
20.7k
       i++, relent++, native_relocs += entsize)
1575
20.7k
    {
1576
20.7k
      bool res;
1577
20.7k
      Elf_Internal_Rela rela;
1578
1579
20.7k
      if (entsize == sizeof (Elf_External_Rela))
1580
4.32k
  elf_swap_reloca_in (abfd, native_relocs, &rela);
1581
16.4k
      else
1582
16.4k
  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
20.7k
      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
1589
20.7k
  relent->address = rela.r_offset;
1590
0
      else
1591
0
  relent->address = rela.r_offset - asect->vma;
1592
1593
20.7k
      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
1.04k
  relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1597
19.7k
      else if (ELF_R_SYM (rela.r_info) > symcount)
1598
166
  {
1599
166
    _bfd_error_handler
1600
      /* xgettext:c-format */
1601
166
      (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
1602
166
       abfd, asect, i, (long) ELF_R_SYM (rela.r_info));
1603
166
    bfd_set_error (bfd_error_bad_value);
1604
166
    relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1605
166
  }
1606
19.5k
      else
1607
19.5k
  {
1608
19.5k
    asymbol **ps;
1609
1610
19.5k
    ps = symbols + ELF_R_SYM (rela.r_info) - 1;
1611
1612
19.5k
    relent->sym_ptr_ptr = ps;
1613
19.5k
  }
1614
1615
20.7k
      relent->addend = rela.r_addend;
1616
1617
20.7k
      if ((entsize == sizeof (Elf_External_Rela)
1618
20.7k
     && ebd->elf_info_to_howto != NULL)
1619
20.7k
    || ebd->elf_info_to_howto_rel == NULL)
1620
4.22k
  res = ebd->elf_info_to_howto (abfd, relent, &rela);
1621
16.5k
      else
1622
16.5k
  res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
1623
1624
20.7k
      if (! res || relent->howto == NULL)
1625
10
  goto error_return;
1626
20.7k
    }
1627
1628
132
  free (allocated);
1629
132
  return true;
1630
1631
10
 error_return:
1632
10
  free (allocated);
1633
10
  return false;
1634
142
}
1635
1636
/* Read in and swap the external relocs.  */
1637
1638
bool
1639
elf_slurp_reloc_table (bfd *abfd,
1640
           asection *asect,
1641
           asymbol **symbols,
1642
           bool dynamic)
1643
16.9k
{
1644
16.9k
  const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
1645
16.9k
  struct bfd_elf_section_data * const d = elf_section_data (asect);
1646
16.9k
  Elf_Internal_Shdr *rel_hdr;
1647
16.9k
  Elf_Internal_Shdr *rel_hdr2;
1648
16.9k
  bfd_size_type reloc_count;
1649
16.9k
  bfd_size_type reloc_count2;
1650
16.9k
  arelent *relents;
1651
16.9k
  size_t amt;
1652
1653
16.9k
  if (asect->relocation != NULL)
1654
1.62k
    return true;
1655
1656
15.3k
  if (! dynamic)
1657
15.1k
    {
1658
15.1k
      if ((asect->flags & SEC_RELOC) == 0
1659
15.1k
    || asect->reloc_count == 0)
1660
866
  return true;
1661
1662
14.2k
      rel_hdr = d->rel.hdr;
1663
14.2k
      reloc_count = rel_hdr ? NUM_SHDR_ENTRIES (rel_hdr) : 0;
1664
14.2k
      rel_hdr2 = d->rela.hdr;
1665
14.2k
      reloc_count2 = rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0;
1666
1667
      /* PR 17512: file: 0b4f81b7.  */
1668
14.2k
      if (asect->reloc_count != reloc_count + reloc_count2)
1669
0
  return false;
1670
14.2k
      BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
1671
14.2k
      || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
1672
1673
14.2k
    }
1674
183
  else
1675
183
    {
1676
      /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
1677
   case because relocations against this section may use the
1678
   dynamic symbol table, and in that case bfd_section_from_shdr
1679
   in elf.c does not update the RELOC_COUNT.  */
1680
183
      if (asect->size == 0)
1681
2
  return true;
1682
1683
181
      rel_hdr = &d->this_hdr;
1684
181
      reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
1685
181
      rel_hdr2 = NULL;
1686
181
      reloc_count2 = 0;
1687
181
    }
1688
1689
14.4k
  if (_bfd_mul_overflow (reloc_count + reloc_count2, sizeof (arelent), &amt))
1690
0
    {
1691
0
      bfd_set_error (bfd_error_file_too_big);
1692
0
      return false;
1693
0
    }
1694
14.4k
  relents = (arelent *) bfd_alloc (abfd, amt);
1695
14.4k
  if (relents == NULL)
1696
0
    return false;
1697
1698
14.4k
  if (rel_hdr
1699
14.4k
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1700
181
                rel_hdr, reloc_count,
1701
181
                relents,
1702
181
                symbols, dynamic))
1703
11
    return false;
1704
1705
14.4k
  if (rel_hdr2
1706
14.4k
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1707
14.2k
                rel_hdr2, reloc_count2,
1708
14.2k
                relents + reloc_count,
1709
14.2k
                symbols, dynamic))
1710
1.90k
    return false;
1711
1712
12.5k
  if (!bed->slurp_secondary_relocs (abfd, asect, symbols, dynamic))
1713
208
    return false;
1714
1715
12.3k
  asect->relocation = relents;
1716
12.3k
  return true;
1717
12.5k
}
bfd_elf64_slurp_reloc_table
Line
Count
Source
1643
16.4k
{
1644
16.4k
  const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
1645
16.4k
  struct bfd_elf_section_data * const d = elf_section_data (asect);
1646
16.4k
  Elf_Internal_Shdr *rel_hdr;
1647
16.4k
  Elf_Internal_Shdr *rel_hdr2;
1648
16.4k
  bfd_size_type reloc_count;
1649
16.4k
  bfd_size_type reloc_count2;
1650
16.4k
  arelent *relents;
1651
16.4k
  size_t amt;
1652
1653
16.4k
  if (asect->relocation != NULL)
1654
1.56k
    return true;
1655
1656
14.9k
  if (! dynamic)
1657
14.8k
    {
1658
14.8k
      if ((asect->flags & SEC_RELOC) == 0
1659
14.8k
    || asect->reloc_count == 0)
1660
629
  return true;
1661
1662
14.2k
      rel_hdr = d->rel.hdr;
1663
14.2k
      reloc_count = rel_hdr ? NUM_SHDR_ENTRIES (rel_hdr) : 0;
1664
14.2k
      rel_hdr2 = d->rela.hdr;
1665
14.2k
      reloc_count2 = rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0;
1666
1667
      /* PR 17512: file: 0b4f81b7.  */
1668
14.2k
      if (asect->reloc_count != reloc_count + reloc_count2)
1669
0
  return false;
1670
14.2k
      BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
1671
14.2k
      || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
1672
1673
14.2k
    }
1674
41
  else
1675
41
    {
1676
      /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
1677
   case because relocations against this section may use the
1678
   dynamic symbol table, and in that case bfd_section_from_shdr
1679
   in elf.c does not update the RELOC_COUNT.  */
1680
41
      if (asect->size == 0)
1681
2
  return true;
1682
1683
39
      rel_hdr = &d->this_hdr;
1684
39
      reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
1685
39
      rel_hdr2 = NULL;
1686
39
      reloc_count2 = 0;
1687
39
    }
1688
1689
14.2k
  if (_bfd_mul_overflow (reloc_count + reloc_count2, sizeof (arelent), &amt))
1690
0
    {
1691
0
      bfd_set_error (bfd_error_file_too_big);
1692
0
      return false;
1693
0
    }
1694
14.2k
  relents = (arelent *) bfd_alloc (abfd, amt);
1695
14.2k
  if (relents == NULL)
1696
0
    return false;
1697
1698
14.2k
  if (rel_hdr
1699
14.2k
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1700
39
                rel_hdr, reloc_count,
1701
39
                relents,
1702
39
                symbols, dynamic))
1703
1
    return false;
1704
1705
14.2k
  if (rel_hdr2
1706
14.2k
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1707
14.2k
                rel_hdr2, reloc_count2,
1708
14.2k
                relents + reloc_count,
1709
14.2k
                symbols, dynamic))
1710
1.90k
    return false;
1711
1712
12.3k
  if (!bed->slurp_secondary_relocs (abfd, asect, symbols, dynamic))
1713
208
    return false;
1714
1715
12.1k
  asect->relocation = relents;
1716
12.1k
  return true;
1717
12.3k
}
bfd_elf32_slurp_reloc_table
Line
Count
Source
1643
439
{
1644
439
  const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
1645
439
  struct bfd_elf_section_data * const d = elf_section_data (asect);
1646
439
  Elf_Internal_Shdr *rel_hdr;
1647
439
  Elf_Internal_Shdr *rel_hdr2;
1648
439
  bfd_size_type reloc_count;
1649
439
  bfd_size_type reloc_count2;
1650
439
  arelent *relents;
1651
439
  size_t amt;
1652
1653
439
  if (asect->relocation != NULL)
1654
60
    return true;
1655
1656
379
  if (! dynamic)
1657
237
    {
1658
237
      if ((asect->flags & SEC_RELOC) == 0
1659
237
    || asect->reloc_count == 0)
1660
237
  return true;
1661
1662
0
      rel_hdr = d->rel.hdr;
1663
0
      reloc_count = rel_hdr ? NUM_SHDR_ENTRIES (rel_hdr) : 0;
1664
0
      rel_hdr2 = d->rela.hdr;
1665
0
      reloc_count2 = rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0;
1666
1667
      /* PR 17512: file: 0b4f81b7.  */
1668
0
      if (asect->reloc_count != reloc_count + reloc_count2)
1669
0
  return false;
1670
0
      BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
1671
0
      || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
1672
1673
0
    }
1674
142
  else
1675
142
    {
1676
      /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
1677
   case because relocations against this section may use the
1678
   dynamic symbol table, and in that case bfd_section_from_shdr
1679
   in elf.c does not update the RELOC_COUNT.  */
1680
142
      if (asect->size == 0)
1681
0
  return true;
1682
1683
142
      rel_hdr = &d->this_hdr;
1684
142
      reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
1685
142
      rel_hdr2 = NULL;
1686
142
      reloc_count2 = 0;
1687
142
    }
1688
1689
142
  if (_bfd_mul_overflow (reloc_count + reloc_count2, sizeof (arelent), &amt))
1690
0
    {
1691
0
      bfd_set_error (bfd_error_file_too_big);
1692
0
      return false;
1693
0
    }
1694
142
  relents = (arelent *) bfd_alloc (abfd, amt);
1695
142
  if (relents == NULL)
1696
0
    return false;
1697
1698
142
  if (rel_hdr
1699
142
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1700
142
                rel_hdr, reloc_count,
1701
142
                relents,
1702
142
                symbols, dynamic))
1703
10
    return false;
1704
1705
132
  if (rel_hdr2
1706
132
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1707
0
                rel_hdr2, reloc_count2,
1708
0
                relents + reloc_count,
1709
0
                symbols, dynamic))
1710
0
    return false;
1711
1712
132
  if (!bed->slurp_secondary_relocs (abfd, asect, symbols, dynamic))
1713
0
    return false;
1714
1715
132
  asect->relocation = relents;
1716
132
  return true;
1717
132
}
1718
1719
#if DEBUG & 2
1720
static void
1721
elf_debug_section (int num, Elf_Internal_Shdr *hdr)
1722
{
1723
  fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num,
1724
     hdr->bfd_section != NULL ? hdr->bfd_section->name : "",
1725
     (long) hdr);
1726
  fprintf (stderr,
1727
     "sh_name      = %ld\tsh_type      = %ld\tsh_flags     = %ld\n",
1728
     (long) hdr->sh_name,
1729
     (long) hdr->sh_type,
1730
     (long) hdr->sh_flags);
1731
  fprintf (stderr,
1732
     "sh_addr      = %ld\tsh_offset    = %ld\tsh_size      = %ld\n",
1733
     (long) hdr->sh_addr,
1734
     (long) hdr->sh_offset,
1735
     (long) hdr->sh_size);
1736
  fprintf (stderr,
1737
     "sh_link      = %ld\tsh_info      = %ld\tsh_addralign = %ld\n",
1738
     (long) hdr->sh_link,
1739
     (long) hdr->sh_info,
1740
     (long) hdr->sh_addralign);
1741
  fprintf (stderr, "sh_entsize   = %ld\n",
1742
     (long) hdr->sh_entsize);
1743
  fflush (stderr);
1744
}
1745
#endif
1746
1747
#if DEBUG & 1
1748
static void
1749
elf_debug_file (Elf_Internal_Ehdr *ehdrp)
1750
{
1751
  fprintf (stderr, "e_entry      = 0x%.8lx\n", (long) ehdrp->e_entry);
1752
  fprintf (stderr, "e_phoff      = %ld\n", (long) ehdrp->e_phoff);
1753
  fprintf (stderr, "e_phnum      = %ld\n", (long) ehdrp->e_phnum);
1754
  fprintf (stderr, "e_phentsize  = %ld\n", (long) ehdrp->e_phentsize);
1755
  fprintf (stderr, "e_shoff      = %ld\n", (long) ehdrp->e_shoff);
1756
  fprintf (stderr, "e_shnum      = %ld\n", (long) ehdrp->e_shnum);
1757
  fprintf (stderr, "e_shentsize  = %ld\n", (long) ehdrp->e_shentsize);
1758
}
1759
#endif
1760

1761
/* Create a new BFD as if by bfd_openr.  Rather than opening a file,
1762
   reconstruct an ELF file by reading the segments out of remote
1763
   memory based on the ELF file header at EHDR_VMA and the ELF program
1764
   headers it points to.  If non-zero, SIZE is the known extent of the
1765
   object.  If not null, *LOADBASEP is filled in with the difference
1766
   between the VMAs from which the segments were read, and the VMAs
1767
   the file headers (and hence BFD's idea of each section's VMA) put
1768
   them at.
1769
1770
   The function TARGET_READ_MEMORY is called to copy LEN bytes from
1771
   the remote memory at target address VMA into the local buffer at
1772
   MYADDR; it should return zero on success or an `errno' code on
1773
   failure.  TEMPL must be a BFD for a target with the word size and
1774
   byte order found in the remote memory.  */
1775
1776
bfd *
1777
NAME(_bfd_elf,bfd_from_remote_memory)
1778
  (bfd *templ,
1779
   bfd_vma ehdr_vma    /* Bytes.  */,
1780
   bfd_size_type size  /* Octets.  */,
1781
   bfd_vma *loadbasep  /* Bytes.  */,
1782
   int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
1783
                          /* (Bytes  ,           , octets       ).  */
1784
0
{
1785
0
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
1786
0
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */
1787
0
  Elf_External_Phdr *x_phdrs;
1788
0
  Elf_Internal_Phdr *i_phdrs, *last_phdr, *first_phdr;
1789
0
  bfd *nbfd;
1790
0
  struct bfd_in_memory *bim;
1791
0
  bfd_byte *contents;
1792
0
  int err;
1793
0
  unsigned int i;
1794
0
  bfd_vma high_offset;
1795
0
  bfd_vma shdr_end;
1796
0
  bfd_vma loadbase;  /* Bytes.  */
1797
0
  size_t amt;
1798
0
  unsigned int opb = bfd_octets_per_byte (templ, NULL);
1799
1800
  /* Read in the ELF header in external format.  */
1801
0
  err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr, sizeof x_ehdr);
1802
0
  if (err)
1803
0
    {
1804
0
      bfd_set_error (bfd_error_system_call);
1805
0
      errno = err;
1806
0
      return NULL;
1807
0
    }
1808
1809
  /* Now check to see if we have a valid ELF file, and one that BFD can
1810
     make use of.  The magic number must match, the address size ('class')
1811
     and byte-swapping must match our XVEC entry.  */
1812
1813
0
  if (! elf_file_p (&x_ehdr)
1814
0
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
1815
0
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
1816
0
    {
1817
0
      bfd_set_error (bfd_error_wrong_format);
1818
0
      return NULL;
1819
0
    }
1820
1821
  /* Check that file's byte order matches xvec's */
1822
0
  switch (x_ehdr.e_ident[EI_DATA])
1823
0
    {
1824
0
    case ELFDATA2MSB:   /* Big-endian */
1825
0
      if (! bfd_header_big_endian (templ))
1826
0
  {
1827
0
    bfd_set_error (bfd_error_wrong_format);
1828
0
    return NULL;
1829
0
  }
1830
0
      break;
1831
0
    case ELFDATA2LSB:   /* Little-endian */
1832
0
      if (! bfd_header_little_endian (templ))
1833
0
  {
1834
0
    bfd_set_error (bfd_error_wrong_format);
1835
0
    return NULL;
1836
0
  }
1837
0
      break;
1838
0
    case ELFDATANONE:   /* No data encoding specified */
1839
0
    default:      /* Unknown data encoding specified */
1840
0
      bfd_set_error (bfd_error_wrong_format);
1841
0
      return NULL;
1842
0
    }
1843
1844
0
  elf_swap_ehdr_in (templ, &x_ehdr, &i_ehdr);
1845
1846
  /* The file header tells where to find the program headers.
1847
     These are what we use to actually choose what to read.  */
1848
1849
0
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
1850
0
    {
1851
0
      bfd_set_error (bfd_error_wrong_format);
1852
0
      return NULL;
1853
0
    }
1854
1855
0
  if (_bfd_mul_overflow (i_ehdr.e_phnum,
1856
0
       sizeof (*x_phdrs) + sizeof (*i_phdrs), &amt))
1857
0
    {
1858
0
      bfd_set_error (bfd_error_file_too_big);
1859
0
      return NULL;
1860
0
    }
1861
0
  x_phdrs = (Elf_External_Phdr *) bfd_malloc (amt);
1862
0
  if (x_phdrs == NULL)
1863
0
    return NULL;
1864
0
  err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs,
1865
0
          i_ehdr.e_phnum * sizeof x_phdrs[0]);
1866
0
  if (err)
1867
0
    {
1868
0
      free (x_phdrs);
1869
0
      bfd_set_error (bfd_error_system_call);
1870
0
      errno = err;
1871
0
      return NULL;
1872
0
    }
1873
0
  i_phdrs = (Elf_Internal_Phdr *) &x_phdrs[i_ehdr.e_phnum];
1874
1875
0
  high_offset = 0;
1876
0
  loadbase = 0;
1877
0
  first_phdr = NULL;
1878
0
  last_phdr = NULL;
1879
0
  for (i = 0; i < i_ehdr.e_phnum; ++i)
1880
0
    {
1881
0
      elf_swap_phdr_in (templ, &x_phdrs[i], &i_phdrs[i]);
1882
0
      if (i_phdrs[i].p_type == PT_LOAD)
1883
0
  {
1884
0
    bfd_vma segment_end = i_phdrs[i].p_offset + i_phdrs[i].p_filesz;
1885
1886
0
    if (segment_end > high_offset)
1887
0
      {
1888
0
        high_offset = segment_end;
1889
0
        last_phdr = &i_phdrs[i];
1890
0
      }
1891
1892
    /* If this program header covers offset zero, where the file
1893
       header sits, then we can figure out the loadbase.  */
1894
0
    if (first_phdr == NULL)
1895
0
      {
1896
0
        bfd_vma p_offset = i_phdrs[i].p_offset;  /* Octets.  */
1897
0
        bfd_vma p_vaddr = i_phdrs[i].p_vaddr;    /* Octets.  */
1898
1899
0
        if (i_phdrs[i].p_align > 1)
1900
0
    {
1901
0
      p_offset &= -(i_phdrs[i].p_align * opb);
1902
0
      p_vaddr &= -(i_phdrs[i].p_align * opb);
1903
0
    }
1904
0
        if (p_offset == 0)
1905
0
    {
1906
0
      loadbase = ehdr_vma - p_vaddr / opb;
1907
0
      first_phdr = &i_phdrs[i];
1908
0
    }
1909
0
      }
1910
0
  }
1911
0
    }
1912
0
  if (high_offset == 0)
1913
0
    {
1914
      /* There were no PT_LOAD segments, so we don't have anything to read.  */
1915
0
      free (x_phdrs);
1916
0
      bfd_set_error (bfd_error_wrong_format);
1917
0
      return NULL;
1918
0
    }
1919
1920
0
  shdr_end = 0;
1921
0
  if (i_ehdr.e_shoff != 0 && i_ehdr.e_shnum != 0 && i_ehdr.e_shentsize != 0)
1922
0
    {
1923
0
      shdr_end = i_ehdr.e_shoff + i_ehdr.e_shnum * i_ehdr.e_shentsize;
1924
1925
0
      if (last_phdr->p_filesz != last_phdr->p_memsz)
1926
0
  {
1927
    /* If the last PT_LOAD header has a bss area then ld.so will
1928
       have cleared anything past p_filesz, zapping the section
1929
       headers.  */
1930
0
  }
1931
0
      else if (size >= shdr_end)
1932
0
  high_offset = size;
1933
0
      else
1934
0
  {
1935
0
    bfd_vma page_size = get_elf_backend_data (templ)->minpagesize;
1936
0
    bfd_vma segment_end = last_phdr->p_offset + last_phdr->p_filesz;
1937
1938
    /* Assume we loaded full pages, allowing us to sometimes see
1939
       section headers.  */
1940
0
    if (page_size > 1 && shdr_end > segment_end)
1941
0
      {
1942
0
        bfd_vma page_end = (segment_end + page_size - 1) & -page_size;
1943
1944
0
        if (page_end >= shdr_end)
1945
    /* Whee, section headers covered.  */
1946
0
    high_offset = shdr_end;
1947
0
      }
1948
0
  }
1949
0
    }
1950
1951
  /* Now we know the size of the whole image we want read in.  */
1952
0
  contents = (bfd_byte *) bfd_zmalloc (high_offset);
1953
0
  if (contents == NULL)
1954
0
    {
1955
0
      free (x_phdrs);
1956
0
      return NULL;
1957
0
    }
1958
1959
0
  for (i = 0; i < i_ehdr.e_phnum; ++i)
1960
0
    if (i_phdrs[i].p_type == PT_LOAD)
1961
0
      {
1962
0
  bfd_vma start = i_phdrs[i].p_offset;         /* Octets.  */
1963
0
  bfd_vma end = start + i_phdrs[i].p_filesz;   /* Octets.  */
1964
0
  bfd_vma vaddr = i_phdrs[i].p_vaddr;          /* Octets.  */
1965
1966
  /* Extend the beginning of the first pt_load to cover file
1967
     header and program headers, if we proved earlier that its
1968
     aligned offset is 0.  */
1969
0
  if (first_phdr == &i_phdrs[i])
1970
0
    {
1971
0
      vaddr -= start;
1972
0
      start = 0;
1973
0
    }
1974
  /* Extend the end of the last pt_load to cover section headers.  */
1975
0
  if (last_phdr == &i_phdrs[i])
1976
0
    end = high_offset;
1977
0
  err = target_read_memory (loadbase + vaddr / opb,
1978
0
          contents + start, end - start);
1979
0
  if (err)
1980
0
    {
1981
0
      free (x_phdrs);
1982
0
      free (contents);
1983
0
      bfd_set_error (bfd_error_system_call);
1984
0
      errno = err;
1985
0
      return NULL;
1986
0
    }
1987
0
      }
1988
0
  free (x_phdrs);
1989
1990
  /* If the segments visible in memory didn't include the section headers,
1991
     then clear them from the file header.  */
1992
0
  if (high_offset < shdr_end)
1993
0
    {
1994
0
      memset (&x_ehdr.e_shoff, 0, sizeof x_ehdr.e_shoff);
1995
0
      memset (&x_ehdr.e_shnum, 0, sizeof x_ehdr.e_shnum);
1996
0
      memset (&x_ehdr.e_shstrndx, 0, sizeof x_ehdr.e_shstrndx);
1997
0
    }
1998
1999
  /* This will normally have been in the first PT_LOAD segment.  But it
2000
     conceivably could be missing, and we might have just changed it.  */
2001
0
  memcpy (contents, &x_ehdr, sizeof x_ehdr);
2002
2003
  /* Now we have a memory image of the ELF file contents.  Make a BFD.  */
2004
0
  bim = (struct bfd_in_memory *) bfd_malloc (sizeof (struct bfd_in_memory));
2005
0
  if (bim == NULL)
2006
0
    {
2007
0
      free (contents);
2008
0
      return NULL;
2009
0
    }
2010
0
  nbfd = _bfd_new_bfd ();
2011
0
  if (nbfd == NULL
2012
0
      || !bfd_set_filename (nbfd, "<in-memory>"))
2013
0
    {
2014
0
      free (bim);
2015
0
      free (contents);
2016
0
      return NULL;
2017
0
    }
2018
0
  nbfd->xvec = templ->xvec;
2019
0
  bim->size = high_offset;
2020
0
  bim->buffer = contents;
2021
0
  nbfd->iostream = bim;
2022
0
  nbfd->flags = BFD_IN_MEMORY;
2023
0
  nbfd->iovec = &_bfd_memory_iovec;
2024
0
  nbfd->origin = 0;
2025
0
  nbfd->direction = read_direction;
2026
0
  nbfd->mtime = time (NULL);
2027
0
  nbfd->mtime_set = true;
2028
2029
0
  if (loadbasep)
2030
0
    *loadbasep = loadbase;
2031
0
  return nbfd;
2032
0
}
Unexecuted instantiation: _bfd_elf64_bfd_from_remote_memory
Unexecuted instantiation: _bfd_elf32_bfd_from_remote_memory
2033
2034
/* Function for ELF_R_INFO.  */
2035
2036
bfd_vma
2037
NAME(elf,r_info) (bfd_vma sym, bfd_vma type)
2038
0
{
2039
0
  return ELF_R_INFO (sym, type);
2040
0
}
Unexecuted instantiation: elf64_r_info
Unexecuted instantiation: elf32_r_info
2041
2042
/* Function for ELF_R_SYM.  */
2043
2044
bfd_vma
2045
NAME(elf,r_sym) (bfd_vma r_info)
2046
3.81k
{
2047
3.81k
  return ELF_R_SYM (r_info);
2048
3.81k
}
elf64_r_sym
Line
Count
Source
2046
3.74k
{
2047
3.74k
  return ELF_R_SYM (r_info);
2048
3.74k
}
elf32_r_sym
Line
Count
Source
2046
70
{
2047
70
  return ELF_R_SYM (r_info);
2048
70
}
2049

2050
#include "elfcore.h"
2051

2052
/* Size-dependent data and functions.  */
2053
const struct elf_size_info NAME(_bfd_elf,size_info) = {
2054
  sizeof (Elf_External_Ehdr),
2055
  sizeof (Elf_External_Phdr),
2056
  sizeof (Elf_External_Shdr),
2057
  sizeof (Elf_External_Rel),
2058
  sizeof (Elf_External_Rela),
2059
  sizeof (Elf_External_Sym),
2060
  sizeof (Elf_External_Dyn),
2061
  sizeof (Elf_External_Note),
2062
  4,
2063
  1,
2064
  ARCH_SIZE, LOG_FILE_ALIGN,
2065
  ELFCLASS, EV_CURRENT,
2066
  elf_write_out_phdrs,
2067
  elf_write_shdrs_and_ehdr,
2068
  elf_checksum_contents,
2069
  elf_write_relocs,
2070
  elf_swap_symbol_in,
2071
  elf_swap_symbol_out,
2072
  elf_slurp_reloc_table,
2073
  elf_slurp_symbol_table,
2074
  elf_swap_dyn_in,
2075
  elf_swap_dyn_out,
2076
  elf_swap_reloc_in,
2077
  elf_swap_reloc_out,
2078
  elf_swap_reloca_in,
2079
  elf_swap_reloca_out
2080
};