Coverage Report

Created: 2026-11-21 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elfcode.h
Line
Count
Source
1
/* ELF executable support for BFD.
2
   Copyright (C) 1991-2026 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
38.6M
#define Elf_External_Ehdr NAME(Elf,External_Ehdr)
78
1.81k
#define Elf_External_Sym  NAME(Elf,External_Sym)
79
33.7M
#define Elf_External_Shdr NAME(Elf,External_Shdr)
80
2.22M
#define Elf_External_Phdr NAME(Elf,External_Phdr)
81
0
#define Elf_External_Rel  NAME(Elf,External_Rel)
82
976
#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
36.2k
#define elf_swap_reloc_in   NAME(bfd_elf,swap_reloc_in)
96
116k
#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
117
#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
2.17M
#define elf_swap_phdr_in    NAME(bfd_elf,swap_phdr_in)
102
504
#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
497
#define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
131
260k
#define ELF_R_SYM(X)  ELF64_R_SYM(X)
132
#define ELF_R_TYPE(X) ELF64_R_TYPE(X)
133
2.31M
#define ELFCLASS  ELFCLASS64
134
#define FILE_ALIGN  8
135
#define LOG_FILE_ALIGN  3
136
#endif
137
#if ARCH_SIZE == 32
138
479
#define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
139
121k
#define ELF_R_SYM(X)  ELF32_R_SYM(X)
140
#define ELF_R_TYPE(X) ELF32_R_TYPE(X)
141
8.82M
#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
14.6k
#define H_PUT_WORD    H_PUT_64
159
698
#define H_PUT_SIGNED_WORD H_PUT_S64
160
9.19M
#define H_GET_WORD    H_GET_64
161
569k
#define H_GET_SIGNED_WORD H_GET_S64
162
#endif
163
#if ARCH_SIZE == 32
164
16.0k
#define H_PUT_WORD    H_PUT_32
165
911
#define H_PUT_SIGNED_WORD H_PUT_S32
166
20.3M
#define H_GET_WORD    H_GET_32
167
1.03M
#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
188k
{
179
188k
  const Elf_External_Sym *src = (const Elf_External_Sym *) psrc;
180
188k
  const Elf_External_Sym_Shndx *shndx = (const Elf_External_Sym_Shndx *) pshn;
181
188k
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
182
183
188k
  dst->st_name = H_GET_32 (abfd, src->st_name);
184
188k
  if (signed_vma)
185
22.6k
    dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value);
186
165k
  else
187
165k
    dst->st_value = H_GET_WORD (abfd, src->st_value);
188
188k
  dst->st_size = H_GET_WORD (abfd, src->st_size);
189
188k
  dst->st_info = H_GET_8 (abfd, src->st_info);
190
188k
  dst->st_other = H_GET_8 (abfd, src->st_other);
191
188k
  dst->st_shndx = H_GET_16 (abfd, src->st_shndx);
192
188k
  if (dst->st_shndx == (SHN_XINDEX & 0xffff))
193
2.21k
    {
194
2.21k
      if (shndx == NULL)
195
518
  return false;
196
1.69k
      dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx);
197
1.69k
    }
198
186k
  else if (dst->st_shndx >= (SHN_LORESERVE & 0xffff))
199
4.94k
    dst->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
200
187k
  dst->st_target_internal = 0;
201
187k
  return true;
202
188k
}
bfd_elf64_swap_symbol_in
Line
Count
Source
178
125k
{
179
125k
  const Elf_External_Sym *src = (const Elf_External_Sym *) psrc;
180
125k
  const Elf_External_Sym_Shndx *shndx = (const Elf_External_Sym_Shndx *) pshn;
181
125k
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
182
183
125k
  dst->st_name = H_GET_32 (abfd, src->st_name);
184
125k
  if (signed_vma)
185
12.5k
    dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value);
186
112k
  else
187
112k
    dst->st_value = H_GET_WORD (abfd, src->st_value);
188
125k
  dst->st_size = H_GET_WORD (abfd, src->st_size);
189
125k
  dst->st_info = H_GET_8 (abfd, src->st_info);
190
125k
  dst->st_other = H_GET_8 (abfd, src->st_other);
191
125k
  dst->st_shndx = H_GET_16 (abfd, src->st_shndx);
192
125k
  if (dst->st_shndx == (SHN_XINDEX & 0xffff))
193
554
    {
194
554
      if (shndx == NULL)
195
344
  return false;
196
210
      dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx);
197
210
    }
198
124k
  else if (dst->st_shndx >= (SHN_LORESERVE & 0xffff))
199
3.31k
    dst->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
200
124k
  dst->st_target_internal = 0;
201
  return true;
202
125k
}
bfd_elf32_swap_symbol_in
Line
Count
Source
178
63.3k
{
179
63.3k
  const Elf_External_Sym *src = (const Elf_External_Sym *) psrc;
180
63.3k
  const Elf_External_Sym_Shndx *shndx = (const Elf_External_Sym_Shndx *) pshn;
181
63.3k
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
182
183
63.3k
  dst->st_name = H_GET_32 (abfd, src->st_name);
184
63.3k
  if (signed_vma)
185
10.0k
    dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value);
186
53.3k
  else
187
53.3k
    dst->st_value = H_GET_WORD (abfd, src->st_value);
188
63.3k
  dst->st_size = H_GET_WORD (abfd, src->st_size);
189
63.3k
  dst->st_info = H_GET_8 (abfd, src->st_info);
190
63.3k
  dst->st_other = H_GET_8 (abfd, src->st_other);
191
63.3k
  dst->st_shndx = H_GET_16 (abfd, src->st_shndx);
192
63.3k
  if (dst->st_shndx == (SHN_XINDEX & 0xffff))
193
1.65k
    {
194
1.65k
      if (shndx == NULL)
195
174
  return false;
196
1.48k
      dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx);
197
1.48k
    }
198
61.7k
  else if (dst->st_shndx >= (SHN_LORESERVE & 0xffff))
199
1.63k
    dst->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
200
63.1k
  dst->st_target_internal = 0;
201
  return true;
202
63.3k
}
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
1.81k
{
213
1.81k
  unsigned int tmp;
214
1.81k
  Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
215
1.81k
  H_PUT_32 (abfd, src->st_name, dst->st_name);
216
1.81k
  H_PUT_WORD (abfd, src->st_value, dst->st_value);
217
1.81k
  H_PUT_WORD (abfd, src->st_size, dst->st_size);
218
1.81k
  H_PUT_8 (abfd, src->st_info, dst->st_info);
219
1.81k
  H_PUT_8 (abfd, src->st_other, dst->st_other);
220
1.81k
  tmp = src->st_shndx;
221
1.81k
  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
1.81k
  H_PUT_16 (abfd, tmp, dst->st_shndx);
229
1.81k
}
bfd_elf64_swap_symbol_out
Line
Count
Source
212
1.20k
{
213
1.20k
  unsigned int tmp;
214
1.20k
  Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
215
1.20k
  H_PUT_32 (abfd, src->st_name, dst->st_name);
216
1.20k
  H_PUT_WORD (abfd, src->st_value, dst->st_value);
217
1.20k
  H_PUT_WORD (abfd, src->st_size, dst->st_size);
218
1.20k
  H_PUT_8 (abfd, src->st_info, dst->st_info);
219
1.20k
  H_PUT_8 (abfd, src->st_other, dst->st_other);
220
1.20k
  tmp = src->st_shndx;
221
1.20k
  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
1.20k
  H_PUT_16 (abfd, tmp, dst->st_shndx);
229
1.20k
}
bfd_elf32_swap_symbol_out
Line
Count
Source
212
601
{
213
601
  unsigned int tmp;
214
601
  Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
215
601
  H_PUT_32 (abfd, src->st_name, dst->st_name);
216
601
  H_PUT_WORD (abfd, src->st_value, dst->st_value);
217
601
  H_PUT_WORD (abfd, src->st_size, dst->st_size);
218
601
  H_PUT_8 (abfd, src->st_info, dst->st_info);
219
601
  H_PUT_8 (abfd, src->st_other, dst->st_other);
220
601
  tmp = src->st_shndx;
221
601
  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
601
  H_PUT_16 (abfd, tmp, dst->st_shndx);
229
601
}
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
3.56M
{
239
3.56M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
240
3.56M
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
241
3.56M
  dst->e_type = H_GET_16 (abfd, src->e_type);
242
3.56M
  dst->e_machine = H_GET_16 (abfd, src->e_machine);
243
3.56M
  dst->e_version = H_GET_32 (abfd, src->e_version);
244
3.56M
  if (signed_vma)
245
394k
    dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry);
246
3.17M
  else
247
3.17M
    dst->e_entry = H_GET_WORD (abfd, src->e_entry);
248
3.56M
  dst->e_phoff = H_GET_WORD (abfd, src->e_phoff);
249
3.56M
  dst->e_shoff = H_GET_WORD (abfd, src->e_shoff);
250
3.56M
  dst->e_flags = H_GET_32 (abfd, src->e_flags);
251
3.56M
  dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize);
252
3.56M
  dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize);
253
3.56M
  dst->e_phnum = H_GET_16 (abfd, src->e_phnum);
254
3.56M
  dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize);
255
3.56M
  dst->e_shnum = H_GET_16 (abfd, src->e_shnum);
256
3.56M
  dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx);
257
3.56M
}
elf64.c:elf_swap_ehdr_in
Line
Count
Source
238
415k
{
239
415k
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
240
415k
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
241
415k
  dst->e_type = H_GET_16 (abfd, src->e_type);
242
415k
  dst->e_machine = H_GET_16 (abfd, src->e_machine);
243
415k
  dst->e_version = H_GET_32 (abfd, src->e_version);
244
415k
  if (signed_vma)
245
60.0k
    dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry);
246
355k
  else
247
355k
    dst->e_entry = H_GET_WORD (abfd, src->e_entry);
248
415k
  dst->e_phoff = H_GET_WORD (abfd, src->e_phoff);
249
415k
  dst->e_shoff = H_GET_WORD (abfd, src->e_shoff);
250
415k
  dst->e_flags = H_GET_32 (abfd, src->e_flags);
251
415k
  dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize);
252
415k
  dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize);
253
415k
  dst->e_phnum = H_GET_16 (abfd, src->e_phnum);
254
415k
  dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize);
255
415k
  dst->e_shnum = H_GET_16 (abfd, src->e_shnum);
256
415k
  dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx);
257
415k
}
elf32.c:elf_swap_ehdr_in
Line
Count
Source
238
3.15M
{
239
3.15M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
240
3.15M
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
241
3.15M
  dst->e_type = H_GET_16 (abfd, src->e_type);
242
3.15M
  dst->e_machine = H_GET_16 (abfd, src->e_machine);
243
3.15M
  dst->e_version = H_GET_32 (abfd, src->e_version);
244
3.15M
  if (signed_vma)
245
333k
    dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry);
246
2.81M
  else
247
2.81M
    dst->e_entry = H_GET_WORD (abfd, src->e_entry);
248
3.15M
  dst->e_phoff = H_GET_WORD (abfd, src->e_phoff);
249
3.15M
  dst->e_shoff = H_GET_WORD (abfd, src->e_shoff);
250
3.15M
  dst->e_flags = H_GET_32 (abfd, src->e_flags);
251
3.15M
  dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize);
252
3.15M
  dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize);
253
3.15M
  dst->e_phnum = H_GET_16 (abfd, src->e_phnum);
254
3.15M
  dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize);
255
3.15M
  dst->e_shnum = H_GET_16 (abfd, src->e_shnum);
256
3.15M
  dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx);
257
3.15M
}
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
633
{
267
633
  unsigned int tmp;
268
633
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
269
633
  bool no_section_header = (abfd->flags & BFD_NO_SECTION_HEADER) != 0;
270
633
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
271
  /* note that all elements of dst are *arrays of unsigned char* already...  */
272
633
  H_PUT_16 (abfd, src->e_type, dst->e_type);
273
633
  H_PUT_16 (abfd, src->e_machine, dst->e_machine);
274
633
  H_PUT_32 (abfd, src->e_version, dst->e_version);
275
633
  if (signed_vma)
276
633
    H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
277
555
  else
278
633
    H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
279
633
  H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
280
633
  if (no_section_header)
281
633
    H_PUT_WORD (abfd, 0, dst->e_shoff);
282
633
  else
283
633
    H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
284
633
  H_PUT_32 (abfd, src->e_flags, dst->e_flags);
285
633
  H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
286
633
  H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
287
633
  tmp = src->e_phnum;
288
633
  if (tmp > PN_XNUM)
289
0
    tmp = PN_XNUM;
290
633
  H_PUT_16 (abfd, tmp, dst->e_phnum);
291
633
  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
633
  else
298
633
    {
299
633
      H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
300
633
      tmp = src->e_shnum;
301
633
      if (tmp >= (SHN_LORESERVE & 0xffff))
302
0
  tmp = SHN_UNDEF;
303
633
      H_PUT_16 (abfd, tmp, dst->e_shnum);
304
633
      tmp = src->e_shstrndx;
305
633
      if (tmp >= (SHN_LORESERVE & 0xffff))
306
0
  tmp = SHN_XINDEX & 0xffff;
307
633
      H_PUT_16 (abfd, tmp, dst->e_shstrndx);
308
633
    }
309
633
}
elf64.c:elf_swap_ehdr_out
Line
Count
Source
266
201
{
267
201
  unsigned int tmp;
268
201
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
269
201
  bool no_section_header = (abfd->flags & BFD_NO_SECTION_HEADER) != 0;
270
201
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
271
  /* note that all elements of dst are *arrays of unsigned char* already...  */
272
201
  H_PUT_16 (abfd, src->e_type, dst->e_type);
273
201
  H_PUT_16 (abfd, src->e_machine, dst->e_machine);
274
201
  H_PUT_32 (abfd, src->e_version, dst->e_version);
275
201
  if (signed_vma)
276
201
    H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
277
168
  else
278
201
    H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
279
201
  H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
280
201
  if (no_section_header)
281
201
    H_PUT_WORD (abfd, 0, dst->e_shoff);
282
201
  else
283
201
    H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
284
201
  H_PUT_32 (abfd, src->e_flags, dst->e_flags);
285
201
  H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
286
201
  H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
287
201
  tmp = src->e_phnum;
288
201
  if (tmp > PN_XNUM)
289
0
    tmp = PN_XNUM;
290
201
  H_PUT_16 (abfd, tmp, dst->e_phnum);
291
201
  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
201
  else
298
201
    {
299
201
      H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
300
201
      tmp = src->e_shnum;
301
201
      if (tmp >= (SHN_LORESERVE & 0xffff))
302
0
  tmp = SHN_UNDEF;
303
201
      H_PUT_16 (abfd, tmp, dst->e_shnum);
304
201
      tmp = src->e_shstrndx;
305
201
      if (tmp >= (SHN_LORESERVE & 0xffff))
306
0
  tmp = SHN_XINDEX & 0xffff;
307
201
      H_PUT_16 (abfd, tmp, dst->e_shstrndx);
308
201
    }
309
201
}
elf32.c:elf_swap_ehdr_out
Line
Count
Source
266
432
{
267
432
  unsigned int tmp;
268
432
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
269
432
  bool no_section_header = (abfd->flags & BFD_NO_SECTION_HEADER) != 0;
270
432
  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
271
  /* note that all elements of dst are *arrays of unsigned char* already...  */
272
432
  H_PUT_16 (abfd, src->e_type, dst->e_type);
273
432
  H_PUT_16 (abfd, src->e_machine, dst->e_machine);
274
432
  H_PUT_32 (abfd, src->e_version, dst->e_version);
275
432
  if (signed_vma)
276
432
    H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
277
387
  else
278
432
    H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
279
432
  H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
280
432
  if (no_section_header)
281
432
    H_PUT_WORD (abfd, 0, dst->e_shoff);
282
432
  else
283
432
    H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
284
432
  H_PUT_32 (abfd, src->e_flags, dst->e_flags);
285
432
  H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
286
432
  H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
287
432
  tmp = src->e_phnum;
288
432
  if (tmp > PN_XNUM)
289
0
    tmp = PN_XNUM;
290
432
  H_PUT_16 (abfd, tmp, dst->e_phnum);
291
432
  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
432
  else
298
432
    {
299
432
      H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
300
432
      tmp = src->e_shnum;
301
432
      if (tmp >= (SHN_LORESERVE & 0xffff))
302
0
  tmp = SHN_UNDEF;
303
432
      H_PUT_16 (abfd, tmp, dst->e_shnum);
304
432
      tmp = src->e_shstrndx;
305
432
      if (tmp >= (SHN_LORESERVE & 0xffff))
306
0
  tmp = SHN_XINDEX & 0xffff;
307
432
      H_PUT_16 (abfd, tmp, dst->e_shstrndx);
308
432
    }
309
432
}
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 bool
315
elf_swap_shdr_in (bfd *abfd,
316
      const Elf_External_Shdr *src,
317
      Elf_Internal_Shdr *dst)
318
1.07M
{
319
1.07M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
320
321
1.07M
  dst->sh_name = H_GET_32 (abfd, src->sh_name);
322
1.07M
  dst->sh_type = H_GET_32 (abfd, src->sh_type);
323
1.07M
  dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
324
1.07M
  if (signed_vma)
325
276k
    dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
326
802k
  else
327
802k
    dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
328
1.07M
  dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
329
1.07M
  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.07M
  if (dst->sh_type != SHT_NOBITS)
334
1.06M
    {
335
1.06M
      ufile_ptr filesize = bfd_get_file_size (abfd);
336
337
1.06M
      if (filesize != 0
338
1.06M
    && ((ufile_ptr) dst->sh_offset > filesize
339
774k
        || dst->sh_size > filesize - dst->sh_offset)
340
378k
    && !abfd->read_only)
341
78.6k
  {
342
78.6k
    _bfd_error_handler (_("warning: %pB has a section "
343
78.6k
        "extending past end of file"), abfd);
344
    /* PR ld/33457: Don't match corrupt section header.  */
345
78.6k
    if (abfd->is_linker_input)
346
0
      return false;
347
78.6k
    abfd->read_only = 1;
348
78.6k
  }
349
1.06M
    }
350
1.07M
  dst->sh_link = H_GET_32 (abfd, src->sh_link);
351
1.07M
  dst->sh_info = H_GET_32 (abfd, src->sh_info);
352
1.07M
  dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
353
1.07M
  dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
354
1.07M
  dst->bfd_section = NULL;
355
1.07M
  dst->contents = NULL;
356
1.07M
  return true;
357
1.07M
}
elf64.c:elf_swap_shdr_in
Line
Count
Source
318
560k
{
319
560k
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
320
321
560k
  dst->sh_name = H_GET_32 (abfd, src->sh_name);
322
560k
  dst->sh_type = H_GET_32 (abfd, src->sh_type);
323
560k
  dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
324
560k
  if (signed_vma)
325
88.2k
    dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
326
471k
  else
327
471k
    dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
328
560k
  dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
329
560k
  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
560k
  if (dst->sh_type != SHT_NOBITS)
334
553k
    {
335
553k
      ufile_ptr filesize = bfd_get_file_size (abfd);
336
337
553k
      if (filesize != 0
338
553k
    && ((ufile_ptr) dst->sh_offset > filesize
339
429k
        || dst->sh_size > filesize - dst->sh_offset)
340
166k
    && !abfd->read_only)
341
31.3k
  {
342
31.3k
    _bfd_error_handler (_("warning: %pB has a section "
343
31.3k
        "extending past end of file"), abfd);
344
    /* PR ld/33457: Don't match corrupt section header.  */
345
31.3k
    if (abfd->is_linker_input)
346
0
      return false;
347
31.3k
    abfd->read_only = 1;
348
31.3k
  }
349
553k
    }
350
560k
  dst->sh_link = H_GET_32 (abfd, src->sh_link);
351
560k
  dst->sh_info = H_GET_32 (abfd, src->sh_info);
352
560k
  dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
353
560k
  dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
354
560k
  dst->bfd_section = NULL;
355
560k
  dst->contents = NULL;
356
  return true;
357
560k
}
elf32.c:elf_swap_shdr_in
Line
Count
Source
318
518k
{
319
518k
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
320
321
518k
  dst->sh_name = H_GET_32 (abfd, src->sh_name);
322
518k
  dst->sh_type = H_GET_32 (abfd, src->sh_type);
323
518k
  dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
324
518k
  if (signed_vma)
325
187k
    dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
326
330k
  else
327
330k
    dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
328
518k
  dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
329
518k
  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
518k
  if (dst->sh_type != SHT_NOBITS)
334
506k
    {
335
506k
      ufile_ptr filesize = bfd_get_file_size (abfd);
336
337
506k
      if (filesize != 0
338
506k
    && ((ufile_ptr) dst->sh_offset > filesize
339
345k
        || dst->sh_size > filesize - dst->sh_offset)
340
211k
    && !abfd->read_only)
341
47.2k
  {
342
47.2k
    _bfd_error_handler (_("warning: %pB has a section "
343
47.2k
        "extending past end of file"), abfd);
344
    /* PR ld/33457: Don't match corrupt section header.  */
345
47.2k
    if (abfd->is_linker_input)
346
0
      return false;
347
47.2k
    abfd->read_only = 1;
348
47.2k
  }
349
506k
    }
350
518k
  dst->sh_link = H_GET_32 (abfd, src->sh_link);
351
518k
  dst->sh_info = H_GET_32 (abfd, src->sh_info);
352
518k
  dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
353
518k
  dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
354
518k
  dst->bfd_section = NULL;
355
518k
  dst->contents = NULL;
356
  return true;
357
518k
}
358
359
/* Translate an ELF section header table entry in internal format into an
360
   ELF section header table entry in external format.  */
361
362
static void
363
elf_swap_shdr_out (bfd *abfd,
364
       const Elf_Internal_Shdr *src,
365
       Elf_External_Shdr *dst)
366
3.25k
{
367
  /* note that all elements of dst are *arrays of unsigned char* already...  */
368
3.25k
  H_PUT_32 (abfd, src->sh_name, dst->sh_name);
369
3.25k
  H_PUT_32 (abfd, src->sh_type, dst->sh_type);
370
3.25k
  H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
371
3.25k
  H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
372
3.25k
  H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
373
3.25k
  H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
374
3.25k
  H_PUT_32 (abfd, src->sh_link, dst->sh_link);
375
3.25k
  H_PUT_32 (abfd, src->sh_info, dst->sh_info);
376
3.25k
  H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
377
3.25k
  H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
378
3.25k
}
elf64.c:elf_swap_shdr_out
Line
Count
Source
366
1.52k
{
367
  /* note that all elements of dst are *arrays of unsigned char* already...  */
368
1.52k
  H_PUT_32 (abfd, src->sh_name, dst->sh_name);
369
1.52k
  H_PUT_32 (abfd, src->sh_type, dst->sh_type);
370
1.52k
  H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
371
1.52k
  H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
372
1.52k
  H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
373
1.52k
  H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
374
1.52k
  H_PUT_32 (abfd, src->sh_link, dst->sh_link);
375
1.52k
  H_PUT_32 (abfd, src->sh_info, dst->sh_info);
376
1.52k
  H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
377
1.52k
  H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
378
1.52k
}
elf32.c:elf_swap_shdr_out
Line
Count
Source
366
1.73k
{
367
  /* note that all elements of dst are *arrays of unsigned char* already...  */
368
1.73k
  H_PUT_32 (abfd, src->sh_name, dst->sh_name);
369
1.73k
  H_PUT_32 (abfd, src->sh_type, dst->sh_type);
370
1.73k
  H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
371
1.73k
  H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
372
1.73k
  H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
373
1.73k
  H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
374
1.73k
  H_PUT_32 (abfd, src->sh_link, dst->sh_link);
375
1.73k
  H_PUT_32 (abfd, src->sh_info, dst->sh_info);
376
1.73k
  H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
377
1.73k
  H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
378
1.73k
}
379
380
/* Translate an ELF program header table entry in external format into an
381
   ELF program header table entry in internal format.  */
382
383
void
384
elf_swap_phdr_in (bfd *abfd,
385
      const Elf_External_Phdr *src,
386
      Elf_Internal_Phdr *dst)
387
2.17M
{
388
2.17M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
389
390
2.17M
  dst->p_type = H_GET_32 (abfd, src->p_type);
391
2.17M
  dst->p_flags = H_GET_32 (abfd, src->p_flags);
392
2.17M
  dst->p_offset = H_GET_WORD (abfd, src->p_offset);
393
2.17M
  if (signed_vma)
394
394k
    {
395
394k
      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
396
394k
      dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
397
394k
    }
398
1.78M
  else
399
1.78M
    {
400
1.78M
      dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
401
1.78M
      dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
402
1.78M
    }
403
2.17M
  dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
404
2.17M
  dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
405
2.17M
  dst->p_align = H_GET_WORD (abfd, src->p_align);
406
2.17M
}
bfd_elf64_swap_phdr_in
Line
Count
Source
387
748k
{
388
748k
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
389
390
748k
  dst->p_type = H_GET_32 (abfd, src->p_type);
391
748k
  dst->p_flags = H_GET_32 (abfd, src->p_flags);
392
748k
  dst->p_offset = H_GET_WORD (abfd, src->p_offset);
393
748k
  if (signed_vma)
394
153k
    {
395
153k
      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
396
153k
      dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
397
153k
    }
398
594k
  else
399
594k
    {
400
594k
      dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
401
594k
      dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
402
594k
    }
403
748k
  dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
404
748k
  dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
405
748k
  dst->p_align = H_GET_WORD (abfd, src->p_align);
406
748k
}
bfd_elf32_swap_phdr_in
Line
Count
Source
387
1.42M
{
388
1.42M
  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
389
390
1.42M
  dst->p_type = H_GET_32 (abfd, src->p_type);
391
1.42M
  dst->p_flags = H_GET_32 (abfd, src->p_flags);
392
1.42M
  dst->p_offset = H_GET_WORD (abfd, src->p_offset);
393
1.42M
  if (signed_vma)
394
240k
    {
395
240k
      dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
396
240k
      dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
397
240k
    }
398
1.18M
  else
399
1.18M
    {
400
1.18M
      dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
401
1.18M
      dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
402
1.18M
    }
403
1.42M
  dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
404
1.42M
  dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
405
1.42M
  dst->p_align = H_GET_WORD (abfd, src->p_align);
406
1.42M
}
407
408
void
409
elf_swap_phdr_out (bfd *abfd,
410
       const Elf_Internal_Phdr *src,
411
       Elf_External_Phdr *dst)
412
504
{
413
504
  elf_backend_data *bed = get_elf_backend_data (abfd);
414
504
  bfd_vma p_paddr = bed->want_p_paddr_set_to_zero ? 0 : src->p_paddr;
415
416
  /* note that all elements of dst are *arrays of unsigned char* already...  */
417
504
  H_PUT_32 (abfd, src->p_type, dst->p_type);
418
504
  H_PUT_WORD (abfd, src->p_offset, dst->p_offset);
419
504
  H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr);
420
504
  H_PUT_WORD (abfd, p_paddr, dst->p_paddr);
421
504
  H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz);
422
504
  H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz);
423
504
  H_PUT_32 (abfd, src->p_flags, dst->p_flags);
424
504
  H_PUT_WORD (abfd, src->p_align, dst->p_align);
425
504
}
bfd_elf64_swap_phdr_out
Line
Count
Source
412
211
{
413
211
  elf_backend_data *bed = get_elf_backend_data (abfd);
414
211
  bfd_vma p_paddr = bed->want_p_paddr_set_to_zero ? 0 : src->p_paddr;
415
416
  /* note that all elements of dst are *arrays of unsigned char* already...  */
417
211
  H_PUT_32 (abfd, src->p_type, dst->p_type);
418
211
  H_PUT_WORD (abfd, src->p_offset, dst->p_offset);
419
211
  H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr);
420
211
  H_PUT_WORD (abfd, p_paddr, dst->p_paddr);
421
211
  H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz);
422
211
  H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz);
423
211
  H_PUT_32 (abfd, src->p_flags, dst->p_flags);
424
211
  H_PUT_WORD (abfd, src->p_align, dst->p_align);
425
211
}
bfd_elf32_swap_phdr_out
Line
Count
Source
412
293
{
413
293
  elf_backend_data *bed = get_elf_backend_data (abfd);
414
293
  bfd_vma p_paddr = bed->want_p_paddr_set_to_zero ? 0 : src->p_paddr;
415
416
  /* note that all elements of dst are *arrays of unsigned char* already...  */
417
293
  H_PUT_32 (abfd, src->p_type, dst->p_type);
418
293
  H_PUT_WORD (abfd, src->p_offset, dst->p_offset);
419
293
  H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr);
420
293
  H_PUT_WORD (abfd, p_paddr, dst->p_paddr);
421
293
  H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz);
422
293
  H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz);
423
293
  H_PUT_32 (abfd, src->p_flags, dst->p_flags);
424
293
  H_PUT_WORD (abfd, src->p_align, dst->p_align);
425
293
}
426
427
/* Translate an ELF reloc from external format to internal format.  */
428
void
429
elf_swap_reloc_in (bfd *abfd,
430
       const bfd_byte *s,
431
       Elf_Internal_Rela *dst)
432
36.2k
{
433
36.2k
  const Elf_External_Rel *src = (const Elf_External_Rel *) s;
434
36.2k
  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
435
36.2k
  dst->r_info = H_GET_WORD (abfd, src->r_info);
436
36.2k
  dst->r_addend = 0;
437
36.2k
}
Unexecuted instantiation: bfd_elf64_swap_reloc_in
bfd_elf32_swap_reloc_in
Line
Count
Source
432
36.2k
{
433
36.2k
  const Elf_External_Rel *src = (const Elf_External_Rel *) s;
434
36.2k
  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
435
36.2k
  dst->r_info = H_GET_WORD (abfd, src->r_info);
436
36.2k
  dst->r_addend = 0;
437
36.2k
}
438
439
void
440
elf_swap_reloca_in (bfd *abfd,
441
        const bfd_byte *s,
442
        Elf_Internal_Rela *dst)
443
119k
{
444
119k
  const Elf_External_Rela *src = (const Elf_External_Rela *) s;
445
119k
  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
446
119k
  dst->r_info = H_GET_WORD (abfd, src->r_info);
447
119k
  dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
448
119k
}
bfd_elf64_swap_reloca_in
Line
Count
Source
443
101k
{
444
101k
  const Elf_External_Rela *src = (const Elf_External_Rela *) s;
445
101k
  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
446
101k
  dst->r_info = H_GET_WORD (abfd, src->r_info);
447
101k
  dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
448
101k
}
bfd_elf32_swap_reloca_in
Line
Count
Source
443
18.2k
{
444
18.2k
  const Elf_External_Rela *src = (const Elf_External_Rela *) s;
445
18.2k
  dst->r_offset = H_GET_WORD (abfd, src->r_offset);
446
18.2k
  dst->r_info = H_GET_WORD (abfd, src->r_info);
447
18.2k
  dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
448
18.2k
}
449
450
/* Translate an ELF reloc from internal format to external format.  */
451
void
452
elf_swap_reloc_out (bfd *abfd,
453
        const Elf_Internal_Rela *src,
454
        bfd_byte *d)
455
0
{
456
0
  Elf_External_Rel *dst = (Elf_External_Rel *) d;
457
0
  H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
458
0
  H_PUT_WORD (abfd, src->r_info, dst->r_info);
459
0
}
Unexecuted instantiation: bfd_elf64_swap_reloc_out
Unexecuted instantiation: bfd_elf32_swap_reloc_out
460
461
void
462
elf_swap_reloca_out (bfd *abfd,
463
         const Elf_Internal_Rela *src,
464
         bfd_byte *d)
465
976
{
466
976
  Elf_External_Rela *dst = (Elf_External_Rela *) d;
467
976
  H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
468
976
  H_PUT_WORD (abfd, src->r_info, dst->r_info);
469
976
  H_PUT_SIGNED_WORD (abfd, src->r_addend, dst->r_addend);
470
976
}
bfd_elf64_swap_reloca_out
Line
Count
Source
465
497
{
466
497
  Elf_External_Rela *dst = (Elf_External_Rela *) d;
467
497
  H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
468
497
  H_PUT_WORD (abfd, src->r_info, dst->r_info);
469
497
  H_PUT_SIGNED_WORD (abfd, src->r_addend, dst->r_addend);
470
497
}
bfd_elf32_swap_reloca_out
Line
Count
Source
465
479
{
466
479
  Elf_External_Rela *dst = (Elf_External_Rela *) d;
467
479
  H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
468
479
  H_PUT_WORD (abfd, src->r_info, dst->r_info);
469
479
  H_PUT_SIGNED_WORD (abfd, src->r_addend, dst->r_addend);
470
479
}
471
472
void
473
elf_swap_dyn_in (bfd *abfd,
474
     const void *p,
475
     Elf_Internal_Dyn *dst)
476
72.6k
{
477
72.6k
  const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
478
479
72.6k
  dst->d_tag = H_GET_WORD (abfd, src->d_tag);
480
72.6k
  dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
481
72.6k
}
bfd_elf64_swap_dyn_in
Line
Count
Source
476
57.4k
{
477
57.4k
  const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
478
479
57.4k
  dst->d_tag = H_GET_WORD (abfd, src->d_tag);
480
57.4k
  dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
481
57.4k
}
bfd_elf32_swap_dyn_in
Line
Count
Source
476
15.2k
{
477
15.2k
  const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
478
479
15.2k
  dst->d_tag = H_GET_WORD (abfd, src->d_tag);
480
15.2k
  dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
481
15.2k
}
482
483
void
484
elf_swap_dyn_out (bfd *abfd,
485
      const Elf_Internal_Dyn *src,
486
      void *p)
487
0
{
488
0
  Elf_External_Dyn *dst = (Elf_External_Dyn *) p;
489
490
0
  H_PUT_WORD (abfd, src->d_tag, dst->d_tag);
491
0
  H_PUT_WORD (abfd, src->d_un.d_val, dst->d_un.d_val);
492
0
}
Unexecuted instantiation: bfd_elf64_swap_dyn_out
Unexecuted instantiation: bfd_elf32_swap_dyn_out
493

494
/* ELF .o/exec file reading */
495
496
/* Begin processing a given object.
497
498
   First we validate the file by reading in the ELF header and checking
499
   the magic number.  */
500
501
static inline bool
502
elf_file_p (Elf_External_Ehdr *x_ehdrp)
503
33.4M
{
504
33.4M
  return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
505
11.4M
    && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
506
11.4M
    && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
507
11.3M
    && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
508
33.4M
}
elf64.c:elf_file_p
Line
Count
Source
503
7.28M
{
504
7.28M
  return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
505
2.37M
    && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
506
2.37M
    && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
507
2.37M
    && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
508
7.28M
}
elf32.c:elf_file_p
Line
Count
Source
503
26.2M
{
504
26.2M
  return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
505
9.04M
    && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
506
9.03M
    && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
507
9.02M
    && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
508
26.2M
}
509
510
/* Check to see if the file associated with ABFD matches the target vector
511
   that ABFD points to.
512
513
   Note that we may be called several times with the same ABFD, but different
514
   target vectors, most of which will not match.  We have to avoid leaving
515
   any side effects in ABFD, or any data it points to (like tdata), if the
516
   file does not match the target vector.  */
517
518
bfd_cleanup
519
elf_object_p (bfd *abfd)
520
33.7M
{
521
33.7M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
522
33.7M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
523
33.7M
  Elf_External_Shdr x_shdr;  /* Section header table entry, external form */
524
33.7M
  Elf_Internal_Shdr i_shdr;
525
33.7M
  Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
526
33.7M
  unsigned int shindex;
527
33.7M
  elf_backend_data *ebd;
528
33.7M
  asection *s;
529
33.7M
  const bfd_target *target;
530
531
  /* Read in the ELF header in external format.  */
532
533
33.7M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
534
4.33M
    {
535
4.33M
      if (bfd_get_error () != bfd_error_system_call)
536
4.23M
  goto got_wrong_format_error;
537
97.7k
      else
538
97.7k
  goto got_no_match;
539
4.33M
    }
540
541
  /* Now check to see if we have a valid ELF file, and one that BFD can
542
     make use of.  The magic number must match, the address size ('class')
543
     and byte-swapping must match our XVEC entry, and it must have a
544
     section header table (FIXME: See comments re sections at top of this
545
     file).  */
546
547
29.4M
  if (! elf_file_p (&x_ehdr)
548
8.46M
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
549
8.23M
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
550
24.4M
    goto got_wrong_format_error;
551
552
  /* Check that file's byte order matches xvec's */
553
5.00M
  switch (x_ehdr.e_ident[EI_DATA])
554
5.00M
    {
555
1.75M
    case ELFDATA2MSB:   /* Big-endian */
556
1.75M
      if (! bfd_header_big_endian (abfd))
557
880k
  goto got_wrong_format_error;
558
877k
      break;
559
3.24M
    case ELFDATA2LSB:   /* Little-endian */
560
3.24M
      if (! bfd_header_little_endian (abfd))
561
1.59M
  goto got_wrong_format_error;
562
1.64M
      break;
563
1.64M
    case ELFDATANONE:   /* No data encoding specified */
564
2.13k
    default:      /* Unknown data encoding specified */
565
2.13k
      goto got_wrong_format_error;
566
5.00M
    }
567
568
2.52M
  target = abfd->xvec;
569
570
  /* Allocate an instance of the elf_obj_tdata structure and hook it up to
571
     the tdata pointer in the bfd.  */
572
573
2.52M
  if (! (*target->_bfd_set_format[bfd_object]) (abfd))
574
0
    goto got_no_match;
575
576
  /* Now that we know the byte order, swap in the rest of the header */
577
2.52M
  i_ehdrp = elf_elfheader (abfd);
578
2.52M
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
579
#if DEBUG & 1
580
  elf_debug_file (i_ehdrp);
581
#endif
582
583
  /* Reject ET_CORE (header indicates core file, not object file) */
584
2.52M
  if (i_ehdrp->e_type == ET_CORE)
585
736k
    goto got_wrong_format_error;
586
587
  /* If this is a relocatable file and there is no section header
588
     table, then we're hosed.  */
589
1.78M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_type == ET_REL)
590
25.9k
    goto got_wrong_format_error;
591
592
  /* As a simple sanity check, verify that what BFD thinks is the
593
     size of each section header table entry actually matches the size
594
     recorded in the file, but only if there are any sections.  */
595
1.76M
  if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
596
65.8k
    goto got_wrong_format_error;
597
598
  /* Further sanity check.  */
599
1.69M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_shnum != 0)
600
779
    goto got_wrong_format_error;
601
602
1.69M
  ebd = get_elf_backend_data (abfd);
603
1.69M
  if (ebd->s->arch_size != ARCH_SIZE)
604
0
    goto got_wrong_format_error;
605
606
  /* Check that the ELF e_machine field matches what this particular
607
     BFD format expects.  */
608
1.69M
  if (ebd->elf_machine_code != i_ehdrp->e_machine
609
1.61M
      && (ebd->elf_machine_alt1 == 0
610
414k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
611
1.61M
      && (ebd->elf_machine_alt2 == 0
612
53.3k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
613
1.61M
      && ebd->elf_machine_code != EM_NONE)
614
1.57M
    goto got_wrong_format_error;
615
616
117k
  if (i_ehdrp->e_type == ET_EXEC)
617
4.90k
    abfd->flags |= EXEC_P;
618
112k
  else if (i_ehdrp->e_type == ET_DYN)
619
3.85k
    abfd->flags |= DYNAMIC;
620
621
117k
  if (i_ehdrp->e_phnum > 0)
622
46.5k
    abfd->flags |= D_PAGED;
623
624
117k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
625
36.6k
    {
626
      /* It's OK if this fails for the generic target.  */
627
36.6k
      if (ebd->elf_machine_code != EM_NONE)
628
16
  goto got_no_match;
629
36.6k
    }
630
631
117k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
632
9.82k
    goto got_wrong_format_error;
633
634
107k
  if (i_ehdrp->e_shoff >= sizeof (x_ehdr))
635
89.1k
    {
636
89.1k
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
637
638
      /* Seek to the section header table in the file.  */
639
89.1k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
640
774
  goto got_no_match;
641
642
      /* Read the first section header at index 0, and convert to internal
643
   form.  */
644
88.4k
      if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr)
645
86.7k
    || !elf_swap_shdr_in (abfd, &x_shdr, &i_shdr))
646
1.68k
  goto got_no_match;
647
648
      /* If the section count is zero, the actual count is in the first
649
   section header.  */
650
86.7k
      if (i_ehdrp->e_shnum == SHN_UNDEF)
651
24.0k
  {
652
24.0k
    i_ehdrp->e_shnum = i_shdr.sh_size;
653
24.0k
    if (i_ehdrp->e_shnum >= SHN_LORESERVE
654
23.7k
        || i_ehdrp->e_shnum != i_shdr.sh_size
655
23.2k
        || i_ehdrp->e_shnum  == 0)
656
1.08k
      goto got_wrong_format_error;
657
24.0k
  }
658
659
      /* And similarly for the string table index.  */
660
85.6k
      if (i_ehdrp->e_shstrndx == (SHN_XINDEX & 0xffff))
661
993
  {
662
993
    i_ehdrp->e_shstrndx = i_shdr.sh_link;
663
993
    if (i_ehdrp->e_shstrndx != i_shdr.sh_link)
664
0
      goto got_wrong_format_error;
665
993
  }
666
667
      /* And program headers.  */
668
85.6k
      if (i_ehdrp->e_phnum == PN_XNUM && i_shdr.sh_info != 0)
669
906
  {
670
906
    i_ehdrp->e_phnum = i_shdr.sh_info;
671
906
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
672
0
      goto got_wrong_format_error;
673
906
  }
674
675
      /* Sanity check that we can read all of the section headers.
676
   It ought to be good enough to just read the last one.  */
677
85.6k
      if (i_ehdrp->e_shnum != 1)
678
83.6k
  {
679
    /* Check that we don't have a totally silly number of sections.  */
680
83.6k
    if (i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (x_shdr)
681
82.6k
        || i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (i_shdr))
682
1.21k
      goto got_wrong_format_error;
683
684
82.4k
    where += (i_ehdrp->e_shnum - 1) * sizeof (x_shdr);
685
82.4k
    if ((bfd_size_type) where <= i_ehdrp->e_shoff)
686
0
      goto got_wrong_format_error;
687
688
82.4k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
689
0
      goto got_no_match;
690
82.4k
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
691
1.34k
      goto got_no_match;
692
693
    /* Back to where we were.  */
694
81.1k
    where = i_ehdrp->e_shoff + sizeof (x_shdr);
695
81.1k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
696
0
      goto got_no_match;
697
81.1k
  }
698
85.6k
    }
699
700
  /* Allocate space for a copy of the section header table in
701
     internal form.  */
702
101k
  if (i_ehdrp->e_shnum != 0)
703
83.0k
    {
704
83.0k
      Elf_Internal_Shdr *shdrp;
705
83.0k
      unsigned int num_sec;
706
83.0k
      size_t amt;
707
708
83.0k
      if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*i_shdrp), &amt))
709
0
  goto got_wrong_format_error;
710
83.0k
      i_shdrp = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
711
83.0k
      if (!i_shdrp)
712
0
  goto got_no_match;
713
83.0k
      num_sec = i_ehdrp->e_shnum;
714
83.0k
      elf_numsections (abfd) = num_sec;
715
83.0k
      if (_bfd_mul_overflow (num_sec, sizeof (i_shdrp), &amt))
716
0
  goto got_wrong_format_error;
717
83.0k
      elf_elfsections (abfd) = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
718
83.0k
      if (!elf_elfsections (abfd))
719
0
  goto got_no_match;
720
83.0k
      elf_tdata (abfd)->being_created = bfd_zalloc (abfd, num_sec);
721
83.0k
      if (!elf_tdata (abfd)->being_created)
722
0
  goto got_no_match;
723
724
83.0k
      memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
725
1.18M
      for (shdrp = i_shdrp, shindex = 0; shindex < num_sec; shindex++)
726
1.09M
  elf_elfsections (abfd)[shindex] = shdrp++;
727
728
      /* Read in the rest of the section header table and convert it
729
   to internal form.  */
730
1.07M
      for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
731
991k
  {
732
991k
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr)
733
991k
        || !elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex))
734
0
      goto got_no_match;
735
736
    /* Sanity check sh_link and sh_info.  */
737
991k
    if (i_shdrp[shindex].sh_link >= num_sec)
738
5.21k
      {
739
        /* PR 10478: Accept Solaris binaries with a sh_link
740
     field set to SHN_BEFORE or SHN_AFTER.  */
741
5.21k
        switch (ebd->elf_machine_code)
742
5.21k
    {
743
554
    case EM_386:
744
622
    case EM_IAMCU:
745
2.53k
    case EM_X86_64:
746
2.53k
    case EM_OLD_SPARCV9:
747
2.53k
    case EM_SPARC32PLUS:
748
2.91k
    case EM_SPARCV9:
749
3.05k
    case EM_SPARC:
750
3.05k
      if (i_shdrp[shindex].sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
751
1.47k
          || i_shdrp[shindex].sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
752
1.91k
        break;
753
      /* Otherwise fall through.  */
754
3.29k
    default:
755
3.29k
      goto got_wrong_format_error;
756
5.21k
    }
757
5.21k
      }
758
759
988k
    if (((i_shdrp[shindex].sh_flags & SHF_INFO_LINK)
760
850k
         || i_shdrp[shindex].sh_type == SHT_RELA
761
802k
         || i_shdrp[shindex].sh_type == SHT_REL)
762
210k
        && i_shdrp[shindex].sh_info >= num_sec)
763
1.01k
      goto got_wrong_format_error;
764
765
    /* If the section is loaded, but not page aligned, clear
766
       D_PAGED.  */
767
987k
    if (i_shdrp[shindex].sh_size != 0
768
837k
        && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
769
245k
        && i_shdrp[shindex].sh_type != SHT_NOBITS
770
234k
        && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
771
234k
       % ebd->minpagesize)
772
234k
      != 0))
773
148k
      abfd->flags &= ~D_PAGED;
774
987k
  }
775
776
78.7k
      if (i_ehdrp->e_shstrndx >= elf_numsections (abfd)
777
76.3k
    || i_shdrp[i_ehdrp->e_shstrndx].sh_type != SHT_STRTAB)
778
4.94k
  {
779
    /* PR 2257:
780
       We used to just goto got_wrong_format_error here
781
       but there are binaries in existance for which this test
782
       will prevent the binutils from working with them at all.
783
       So we are kind, and reset the string index value to 0
784
       so that at least some processing can be done.  */
785
4.94k
    i_ehdrp->e_shstrndx = SHN_UNDEF;
786
4.94k
    if (!abfd->read_only)
787
611
      {
788
611
        _bfd_error_handler
789
611
    (_("warning: %pB has a corrupt string table index"), abfd);
790
611
        abfd->read_only = 1;
791
611
      }
792
4.94k
  }
793
78.7k
    }
794
18.0k
  else if (i_ehdrp->e_shstrndx != SHN_UNDEF)
795
4.08k
    goto got_wrong_format_error;
796
797
  /* Read in the program headers.  */
798
92.7k
  if (i_ehdrp->e_phnum == 0)
799
59.9k
    elf_tdata (abfd)->phdr = NULL;
800
32.7k
  else
801
32.7k
    {
802
32.7k
      Elf_Internal_Phdr *i_phdr;
803
32.7k
      unsigned int i;
804
32.7k
      ufile_ptr filesize;
805
32.7k
      size_t amt;
806
807
      /* Check for a corrupt input file with an impossibly large number
808
   of program headers.  */
809
32.7k
      filesize = bfd_get_file_size (abfd);
810
32.7k
      if (filesize != 0
811
32.7k
    && i_ehdrp->e_phnum > filesize / sizeof (Elf_External_Phdr))
812
1.46k
  goto got_wrong_format_error;
813
31.3k
      if (_bfd_mul_overflow (i_ehdrp->e_phnum, sizeof (*i_phdr), &amt))
814
0
  goto got_wrong_format_error;
815
31.3k
      elf_tdata (abfd)->phdr
816
31.3k
  = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
817
31.3k
      if (elf_tdata (abfd)->phdr == NULL)
818
0
  goto got_no_match;
819
31.3k
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
820
187
  goto got_no_match;
821
31.3k
      bool eu_strip_broken_phdrs = false;
822
31.1k
      i_phdr = elf_tdata (abfd)->phdr;
823
704k
      for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
824
674k
  {
825
674k
    Elf_External_Phdr x_phdr;
826
827
674k
    if (bfd_read (&x_phdr, sizeof x_phdr, abfd) != sizeof x_phdr)
828
1.45k
      goto got_no_match;
829
673k
    elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
830
    /* Too much code in BFD relies on alignment being a power of
831
       two, as required by the ELF spec.  */
832
673k
    if (i_phdr->p_align != (i_phdr->p_align & -i_phdr->p_align))
833
446k
      {
834
446k
        i_phdr->p_align &= -i_phdr->p_align;
835
446k
        if (!abfd->read_only)
836
8.93k
    {
837
8.93k
      _bfd_error_handler (_("warning: %pB has a program header "
838
8.93k
          "with invalid alignment"), abfd);
839
8.93k
      abfd->read_only = 1;
840
8.93k
    }
841
446k
      }
842
    /* Detect eu-strip -f debug files, which have program
843
       headers that describe the original file.  */
844
673k
    if (i_phdr->p_filesz != 0
845
513k
        && (i_phdr->p_filesz > filesize
846
94.3k
      || i_phdr->p_offset > filesize - i_phdr->p_filesz))
847
456k
      eu_strip_broken_phdrs = true;
848
673k
  }
849
29.6k
      if (!eu_strip_broken_phdrs
850
5.50k
    && i_ehdrp->e_shoff == 0
851
4.39k
    && i_ehdrp->e_shstrndx == 0)
852
4.39k
  {
853
    /* Try to reconstruct dynamic symbol table from PT_DYNAMIC
854
       segment if there is no section header.  */
855
4.39k
    i_phdr = elf_tdata (abfd)->phdr;
856
15.9k
    for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
857
15.5k
      if (i_phdr->p_type == PT_DYNAMIC)
858
3.91k
        {
859
3.91k
    if (i_phdr->p_filesz != 0
860
3.75k
        && !_bfd_elf_get_dynamic_symbols (abfd, i_phdr,
861
3.75k
                  elf_tdata (abfd)->phdr,
862
3.75k
                  i_ehdrp->e_phnum,
863
3.75k
                  filesize))
864
3.60k
      goto got_no_match;
865
305
    break;
866
3.91k
        }
867
4.39k
  }
868
29.6k
    }
869
870
86.0k
  if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff >= sizeof (x_ehdr))
871
73.6k
    {
872
73.6k
      unsigned int num_sec;
873
874
      /* Once all of the section headers have been read and converted, we
875
   can start processing them.  Note that the first section header is
876
   a dummy placeholder entry, so we ignore it.  */
877
73.6k
      num_sec = elf_numsections (abfd);
878
879k
      for (shindex = 1; shindex < num_sec; shindex++)
879
825k
  if (!bfd_section_from_shdr (abfd, shindex))
880
18.9k
    goto got_no_match;
881
882
      /* Set up ELF sections for SHF_GROUP and SHF_LINK_ORDER.  */
883
54.6k
      if (! _bfd_elf_setup_sections (abfd))
884
5.86k
  goto got_wrong_format_error;
885
54.6k
    }
886
887
  /* Let the backend double check the format and override global
888
     information.  */
889
61.2k
  if (ebd->elf_backend_object_p)
890
37.0k
    {
891
37.0k
      if (! (*ebd->elf_backend_object_p) (abfd))
892
5.14k
  goto got_wrong_format_error;
893
37.0k
    }
894
895
  /* Remember the entry point specified in the ELF file header.  */
896
56.0k
  bfd_set_start_address (abfd, i_ehdrp->e_entry);
897
898
  /* If we have created any reloc sections that are associated with
899
     debugging sections, mark the reloc sections as debugging as well.  */
900
385k
  for (s = abfd->sections; s != NULL; s = s->next)
901
329k
    {
902
329k
      if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL
903
327k
     || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
904
12.8k
    && elf_section_data (s)->this_hdr.sh_info > 0)
905
11.1k
  {
906
11.1k
    unsigned long targ_index;
907
11.1k
    asection *targ_sec;
908
909
11.1k
    targ_index = elf_section_data (s)->this_hdr.sh_info;
910
11.1k
    targ_sec = bfd_section_from_elf_index (abfd, targ_index);
911
11.1k
    if (targ_sec != NULL
912
9.42k
        && (targ_sec->flags & SEC_DEBUGGING) != 0)
913
1.92k
      s->flags |= SEC_DEBUGGING;
914
11.1k
  }
915
329k
    }
916
56.0k
  return _bfd_no_cleanup;
917
918
33.5M
 got_wrong_format_error:
919
33.5M
  bfd_set_error (bfd_error_wrong_format);
920
921
33.6M
 got_no_match:
922
33.6M
  if (abfd->sections != NULL)
923
    /* Unlike most early section processing, the object attribute v2
924
       code uses bfd_malloc rather than bfd_alloc.  Free memory
925
       containing attributes.  */
926
25.8k
    _bfd_elf_cleanup_object_attributes (abfd);
927
33.6M
  return NULL;
928
33.5M
}
bfd_elf64_object_p
Line
Count
Source
520
7.90M
{
521
7.90M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
522
7.90M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
523
7.90M
  Elf_External_Shdr x_shdr;  /* Section header table entry, external form */
524
7.90M
  Elf_Internal_Shdr i_shdr;
525
7.90M
  Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
526
7.90M
  unsigned int shindex;
527
7.90M
  elf_backend_data *ebd;
528
7.90M
  asection *s;
529
7.90M
  const bfd_target *target;
530
531
  /* Read in the ELF header in external format.  */
532
533
7.90M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
534
1.46M
    {
535
1.46M
      if (bfd_get_error () != bfd_error_system_call)
536
1.42M
  goto got_wrong_format_error;
537
38.9k
      else
538
38.9k
  goto got_no_match;
539
1.46M
    }
540
541
  /* Now check to see if we have a valid ELF file, and one that BFD can
542
     make use of.  The magic number must match, the address size ('class')
543
     and byte-swapping must match our XVEC entry, and it must have a
544
     section header table (FIXME: See comments re sections at top of this
545
     file).  */
546
547
6.43M
  if (! elf_file_p (&x_ehdr)
548
1.77M
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
549
1.72M
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
550
5.76M
    goto got_wrong_format_error;
551
552
  /* Check that file's byte order matches xvec's */
553
673k
  switch (x_ehdr.e_ident[EI_DATA])
554
673k
    {
555
107k
    case ELFDATA2MSB:   /* Big-endian */
556
107k
      if (! bfd_header_big_endian (abfd))
557
56.5k
  goto got_wrong_format_error;
558
51.1k
      break;
559
565k
    case ELFDATA2LSB:   /* Little-endian */
560
565k
      if (! bfd_header_little_endian (abfd))
561
262k
  goto got_wrong_format_error;
562
303k
      break;
563
303k
    case ELFDATANONE:   /* No data encoding specified */
564
286
    default:      /* Unknown data encoding specified */
565
286
      goto got_wrong_format_error;
566
673k
    }
567
568
354k
  target = abfd->xvec;
569
570
  /* Allocate an instance of the elf_obj_tdata structure and hook it up to
571
     the tdata pointer in the bfd.  */
572
573
354k
  if (! (*target->_bfd_set_format[bfd_object]) (abfd))
574
0
    goto got_no_match;
575
576
  /* Now that we know the byte order, swap in the rest of the header */
577
354k
  i_ehdrp = elf_elfheader (abfd);
578
354k
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
579
#if DEBUG & 1
580
  elf_debug_file (i_ehdrp);
581
#endif
582
583
  /* Reject ET_CORE (header indicates core file, not object file) */
584
354k
  if (i_ehdrp->e_type == ET_CORE)
585
33.4k
    goto got_wrong_format_error;
586
587
  /* If this is a relocatable file and there is no section header
588
     table, then we're hosed.  */
589
320k
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_type == ET_REL)
590
910
    goto got_wrong_format_error;
591
592
  /* As a simple sanity check, verify that what BFD thinks is the
593
     size of each section header table entry actually matches the size
594
     recorded in the file, but only if there are any sections.  */
595
319k
  if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
596
11.9k
    goto got_wrong_format_error;
597
598
  /* Further sanity check.  */
599
307k
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_shnum != 0)
600
185
    goto got_wrong_format_error;
601
602
307k
  ebd = get_elf_backend_data (abfd);
603
307k
  if (ebd->s->arch_size != ARCH_SIZE)
604
0
    goto got_wrong_format_error;
605
606
  /* Check that the ELF e_machine field matches what this particular
607
     BFD format expects.  */
608
307k
  if (ebd->elf_machine_code != i_ehdrp->e_machine
609
276k
      && (ebd->elf_machine_alt1 == 0
610
36.0k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
611
274k
      && (ebd->elf_machine_alt2 == 0
612
26.8k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
613
274k
      && ebd->elf_machine_code != EM_NONE)
614
259k
    goto got_wrong_format_error;
615
616
48.2k
  if (i_ehdrp->e_type == ET_EXEC)
617
2.31k
    abfd->flags |= EXEC_P;
618
45.9k
  else if (i_ehdrp->e_type == ET_DYN)
619
2.21k
    abfd->flags |= DYNAMIC;
620
621
48.2k
  if (i_ehdrp->e_phnum > 0)
622
17.6k
    abfd->flags |= D_PAGED;
623
624
48.2k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
625
15.4k
    {
626
      /* It's OK if this fails for the generic target.  */
627
15.4k
      if (ebd->elf_machine_code != EM_NONE)
628
0
  goto got_no_match;
629
15.4k
    }
630
631
48.2k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
632
5.85k
    goto got_wrong_format_error;
633
634
42.4k
  if (i_ehdrp->e_shoff >= sizeof (x_ehdr))
635
35.4k
    {
636
35.4k
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
637
638
      /* Seek to the section header table in the file.  */
639
35.4k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
640
774
  goto got_no_match;
641
642
      /* Read the first section header at index 0, and convert to internal
643
   form.  */
644
34.6k
      if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr)
645
33.9k
    || !elf_swap_shdr_in (abfd, &x_shdr, &i_shdr))
646
717
  goto got_no_match;
647
648
      /* If the section count is zero, the actual count is in the first
649
   section header.  */
650
33.9k
      if (i_ehdrp->e_shnum == SHN_UNDEF)
651
1.48k
  {
652
1.48k
    i_ehdrp->e_shnum = i_shdr.sh_size;
653
1.48k
    if (i_ehdrp->e_shnum >= SHN_LORESERVE
654
1.37k
        || i_ehdrp->e_shnum != i_shdr.sh_size
655
831
        || i_ehdrp->e_shnum  == 0)
656
749
      goto got_wrong_format_error;
657
1.48k
  }
658
659
      /* And similarly for the string table index.  */
660
33.1k
      if (i_ehdrp->e_shstrndx == (SHN_XINDEX & 0xffff))
661
419
  {
662
419
    i_ehdrp->e_shstrndx = i_shdr.sh_link;
663
419
    if (i_ehdrp->e_shstrndx != i_shdr.sh_link)
664
0
      goto got_wrong_format_error;
665
419
  }
666
667
      /* And program headers.  */
668
33.1k
      if (i_ehdrp->e_phnum == PN_XNUM && i_shdr.sh_info != 0)
669
361
  {
670
361
    i_ehdrp->e_phnum = i_shdr.sh_info;
671
361
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
672
0
      goto got_wrong_format_error;
673
361
  }
674
675
      /* Sanity check that we can read all of the section headers.
676
   It ought to be good enough to just read the last one.  */
677
33.1k
      if (i_ehdrp->e_shnum != 1)
678
32.6k
  {
679
    /* Check that we don't have a totally silly number of sections.  */
680
32.6k
    if (i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (x_shdr)
681
32.3k
        || i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (i_shdr))
682
263
      goto got_wrong_format_error;
683
684
32.3k
    where += (i_ehdrp->e_shnum - 1) * sizeof (x_shdr);
685
32.3k
    if ((bfd_size_type) where <= i_ehdrp->e_shoff)
686
0
      goto got_wrong_format_error;
687
688
32.3k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
689
0
      goto got_no_match;
690
32.3k
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
691
701
      goto got_no_match;
692
693
    /* Back to where we were.  */
694
31.6k
    where = i_ehdrp->e_shoff + sizeof (x_shdr);
695
31.6k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
696
0
      goto got_no_match;
697
31.6k
  }
698
33.1k
    }
699
700
  /* Allocate space for a copy of the section header table in
701
     internal form.  */
702
39.2k
  if (i_ehdrp->e_shnum != 0)
703
32.1k
    {
704
32.1k
      Elf_Internal_Shdr *shdrp;
705
32.1k
      unsigned int num_sec;
706
32.1k
      size_t amt;
707
708
32.1k
      if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*i_shdrp), &amt))
709
0
  goto got_wrong_format_error;
710
32.1k
      i_shdrp = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
711
32.1k
      if (!i_shdrp)
712
0
  goto got_no_match;
713
32.1k
      num_sec = i_ehdrp->e_shnum;
714
32.1k
      elf_numsections (abfd) = num_sec;
715
32.1k
      if (_bfd_mul_overflow (num_sec, sizeof (i_shdrp), &amt))
716
0
  goto got_wrong_format_error;
717
32.1k
      elf_elfsections (abfd) = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
718
32.1k
      if (!elf_elfsections (abfd))
719
0
  goto got_no_match;
720
32.1k
      elf_tdata (abfd)->being_created = bfd_zalloc (abfd, num_sec);
721
32.1k
      if (!elf_tdata (abfd)->being_created)
722
0
  goto got_no_match;
723
724
32.1k
      memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
725
600k
      for (shdrp = i_shdrp, shindex = 0; shindex < num_sec; shindex++)
726
567k
  elf_elfsections (abfd)[shindex] = shdrp++;
727
728
      /* Read in the rest of the section header table and convert it
729
   to internal form.  */
730
556k
      for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
731
526k
  {
732
526k
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr)
733
526k
        || !elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex))
734
0
      goto got_no_match;
735
736
    /* Sanity check sh_link and sh_info.  */
737
526k
    if (i_shdrp[shindex].sh_link >= num_sec)
738
2.99k
      {
739
        /* PR 10478: Accept Solaris binaries with a sh_link
740
     field set to SHN_BEFORE or SHN_AFTER.  */
741
2.99k
        switch (ebd->elf_machine_code)
742
2.99k
    {
743
0
    case EM_386:
744
0
    case EM_IAMCU:
745
1.78k
    case EM_X86_64:
746
1.78k
    case EM_OLD_SPARCV9:
747
1.78k
    case EM_SPARC32PLUS:
748
2.17k
    case EM_SPARCV9:
749
2.17k
    case EM_SPARC:
750
2.17k
      if (i_shdrp[shindex].sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
751
969
          || i_shdrp[shindex].sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
752
1.44k
        break;
753
      /* Otherwise fall through.  */
754
1.54k
    default:
755
1.54k
      goto got_wrong_format_error;
756
2.99k
    }
757
2.99k
      }
758
759
524k
    if (((i_shdrp[shindex].sh_flags & SHF_INFO_LINK)
760
438k
         || i_shdrp[shindex].sh_type == SHT_RELA
761
395k
         || i_shdrp[shindex].sh_type == SHT_REL)
762
129k
        && i_shdrp[shindex].sh_info >= num_sec)
763
320
      goto got_wrong_format_error;
764
765
    /* If the section is loaded, but not page aligned, clear
766
       D_PAGED.  */
767
524k
    if (i_shdrp[shindex].sh_size != 0
768
457k
        && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
769
146k
        && i_shdrp[shindex].sh_type != SHT_NOBITS
770
143k
        && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
771
143k
       % ebd->minpagesize)
772
143k
      != 0))
773
89.0k
      abfd->flags &= ~D_PAGED;
774
524k
  }
775
776
30.3k
      if (i_ehdrp->e_shstrndx >= elf_numsections (abfd)
777
29.5k
    || i_shdrp[i_ehdrp->e_shstrndx].sh_type != SHT_STRTAB)
778
1.77k
  {
779
    /* PR 2257:
780
       We used to just goto got_wrong_format_error here
781
       but there are binaries in existance for which this test
782
       will prevent the binutils from working with them at all.
783
       So we are kind, and reset the string index value to 0
784
       so that at least some processing can be done.  */
785
1.77k
    i_ehdrp->e_shstrndx = SHN_UNDEF;
786
1.77k
    if (!abfd->read_only)
787
138
      {
788
138
        _bfd_error_handler
789
138
    (_("warning: %pB has a corrupt string table index"), abfd);
790
138
        abfd->read_only = 1;
791
138
      }
792
1.77k
  }
793
30.3k
    }
794
7.03k
  else if (i_ehdrp->e_shstrndx != SHN_UNDEF)
795
160
    goto got_wrong_format_error;
796
797
  /* Read in the program headers.  */
798
37.2k
  if (i_ehdrp->e_phnum == 0)
799
24.1k
    elf_tdata (abfd)->phdr = NULL;
800
13.0k
  else
801
13.0k
    {
802
13.0k
      Elf_Internal_Phdr *i_phdr;
803
13.0k
      unsigned int i;
804
13.0k
      ufile_ptr filesize;
805
13.0k
      size_t amt;
806
807
      /* Check for a corrupt input file with an impossibly large number
808
   of program headers.  */
809
13.0k
      filesize = bfd_get_file_size (abfd);
810
13.0k
      if (filesize != 0
811
13.0k
    && i_ehdrp->e_phnum > filesize / sizeof (Elf_External_Phdr))
812
586
  goto got_wrong_format_error;
813
12.4k
      if (_bfd_mul_overflow (i_ehdrp->e_phnum, sizeof (*i_phdr), &amt))
814
0
  goto got_wrong_format_error;
815
12.4k
      elf_tdata (abfd)->phdr
816
12.4k
  = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
817
12.4k
      if (elf_tdata (abfd)->phdr == NULL)
818
0
  goto got_no_match;
819
12.4k
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
820
187
  goto got_no_match;
821
12.4k
      bool eu_strip_broken_phdrs = false;
822
12.2k
      i_phdr = elf_tdata (abfd)->phdr;
823
313k
      for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
824
301k
  {
825
301k
    Elf_External_Phdr x_phdr;
826
827
301k
    if (bfd_read (&x_phdr, sizeof x_phdr, abfd) != sizeof x_phdr)
828
530
      goto got_no_match;
829
301k
    elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
830
    /* Too much code in BFD relies on alignment being a power of
831
       two, as required by the ELF spec.  */
832
301k
    if (i_phdr->p_align != (i_phdr->p_align & -i_phdr->p_align))
833
214k
      {
834
214k
        i_phdr->p_align &= -i_phdr->p_align;
835
214k
        if (!abfd->read_only)
836
5.80k
    {
837
5.80k
      _bfd_error_handler (_("warning: %pB has a program header "
838
5.80k
          "with invalid alignment"), abfd);
839
5.80k
      abfd->read_only = 1;
840
5.80k
    }
841
214k
      }
842
    /* Detect eu-strip -f debug files, which have program
843
       headers that describe the original file.  */
844
301k
    if (i_phdr->p_filesz != 0
845
237k
        && (i_phdr->p_filesz > filesize
846
32.8k
      || i_phdr->p_offset > filesize - i_phdr->p_filesz))
847
216k
      eu_strip_broken_phdrs = true;
848
301k
  }
849
11.7k
      if (!eu_strip_broken_phdrs
850
3.67k
    && i_ehdrp->e_shoff == 0
851
3.07k
    && i_ehdrp->e_shstrndx == 0)
852
3.07k
  {
853
    /* Try to reconstruct dynamic symbol table from PT_DYNAMIC
854
       segment if there is no section header.  */
855
3.07k
    i_phdr = elf_tdata (abfd)->phdr;
856
11.0k
    for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
857
10.8k
      if (i_phdr->p_type == PT_DYNAMIC)
858
2.85k
        {
859
2.85k
    if (i_phdr->p_filesz != 0
860
2.79k
        && !_bfd_elf_get_dynamic_symbols (abfd, i_phdr,
861
2.79k
                  elf_tdata (abfd)->phdr,
862
2.79k
                  i_ehdrp->e_phnum,
863
2.79k
                  filesize))
864
2.64k
      goto got_no_match;
865
206
    break;
866
2.85k
        }
867
3.07k
  }
868
11.7k
    }
869
870
33.2k
  if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff >= sizeof (x_ehdr))
871
28.5k
    {
872
28.5k
      unsigned int num_sec;
873
874
      /* Once all of the section headers have been read and converted, we
875
   can start processing them.  Note that the first section header is
876
   a dummy placeholder entry, so we ignore it.  */
877
28.5k
      num_sec = elf_numsections (abfd);
878
440k
      for (shindex = 1; shindex < num_sec; shindex++)
879
422k
  if (!bfd_section_from_shdr (abfd, shindex))
880
10.6k
    goto got_no_match;
881
882
      /* Set up ELF sections for SHF_GROUP and SHF_LINK_ORDER.  */
883
17.8k
      if (! _bfd_elf_setup_sections (abfd))
884
3.24k
  goto got_wrong_format_error;
885
17.8k
    }
886
887
  /* Let the backend double check the format and override global
888
     information.  */
889
19.4k
  if (ebd->elf_backend_object_p)
890
11.2k
    {
891
11.2k
      if (! (*ebd->elf_backend_object_p) (abfd))
892
21
  goto got_wrong_format_error;
893
11.2k
    }
894
895
  /* Remember the entry point specified in the ELF file header.  */
896
19.3k
  bfd_set_start_address (abfd, i_ehdrp->e_entry);
897
898
  /* If we have created any reloc sections that are associated with
899
     debugging sections, mark the reloc sections as debugging as well.  */
900
175k
  for (s = abfd->sections; s != NULL; s = s->next)
901
155k
    {
902
155k
      if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL
903
155k
     || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
904
10.0k
    && elf_section_data (s)->this_hdr.sh_info > 0)
905
9.31k
  {
906
9.31k
    unsigned long targ_index;
907
9.31k
    asection *targ_sec;
908
909
9.31k
    targ_index = elf_section_data (s)->this_hdr.sh_info;
910
9.31k
    targ_sec = bfd_section_from_elf_index (abfd, targ_index);
911
9.31k
    if (targ_sec != NULL
912
7.83k
        && (targ_sec->flags & SEC_DEBUGGING) != 0)
913
1.78k
      s->flags |= SEC_DEBUGGING;
914
9.31k
  }
915
155k
    }
916
19.3k
  return _bfd_no_cleanup;
917
918
7.82M
 got_wrong_format_error:
919
7.82M
  bfd_set_error (bfd_error_wrong_format);
920
921
7.88M
 got_no_match:
922
7.88M
  if (abfd->sections != NULL)
923
    /* Unlike most early section processing, the object attribute v2
924
       code uses bfd_malloc rather than bfd_alloc.  Free memory
925
       containing attributes.  */
926
12.3k
    _bfd_elf_cleanup_object_attributes (abfd);
927
  return NULL;
928
7.82M
}
bfd_elf32_object_p
Line
Count
Source
520
25.8M
{
521
25.8M
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
522
25.8M
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
523
25.8M
  Elf_External_Shdr x_shdr;  /* Section header table entry, external form */
524
25.8M
  Elf_Internal_Shdr i_shdr;
525
25.8M
  Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
526
25.8M
  unsigned int shindex;
527
25.8M
  elf_backend_data *ebd;
528
25.8M
  asection *s;
529
25.8M
  const bfd_target *target;
530
531
  /* Read in the ELF header in external format.  */
532
533
25.8M
  if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
534
2.87M
    {
535
2.87M
      if (bfd_get_error () != bfd_error_system_call)
536
2.81M
  goto got_wrong_format_error;
537
58.7k
      else
538
58.7k
  goto got_no_match;
539
2.87M
    }
540
541
  /* Now check to see if we have a valid ELF file, and one that BFD can
542
     make use of.  The magic number must match, the address size ('class')
543
     and byte-swapping must match our XVEC entry, and it must have a
544
     section header table (FIXME: See comments re sections at top of this
545
     file).  */
546
547
22.9M
  if (! elf_file_p (&x_ehdr)
548
6.69M
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
549
6.50M
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
550
18.6M
    goto got_wrong_format_error;
551
552
  /* Check that file's byte order matches xvec's */
553
4.32M
  switch (x_ehdr.e_ident[EI_DATA])
554
4.32M
    {
555
1.65M
    case ELFDATA2MSB:   /* Big-endian */
556
1.65M
      if (! bfd_header_big_endian (abfd))
557
824k
  goto got_wrong_format_error;
558
826k
      break;
559
2.67M
    case ELFDATA2LSB:   /* Little-endian */
560
2.67M
      if (! bfd_header_little_endian (abfd))
561
1.33M
  goto got_wrong_format_error;
562
1.34M
      break;
563
1.34M
    case ELFDATANONE:   /* No data encoding specified */
564
1.84k
    default:      /* Unknown data encoding specified */
565
1.84k
      goto got_wrong_format_error;
566
4.32M
    }
567
568
2.16M
  target = abfd->xvec;
569
570
  /* Allocate an instance of the elf_obj_tdata structure and hook it up to
571
     the tdata pointer in the bfd.  */
572
573
2.16M
  if (! (*target->_bfd_set_format[bfd_object]) (abfd))
574
0
    goto got_no_match;
575
576
  /* Now that we know the byte order, swap in the rest of the header */
577
2.16M
  i_ehdrp = elf_elfheader (abfd);
578
2.16M
  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
579
#if DEBUG & 1
580
  elf_debug_file (i_ehdrp);
581
#endif
582
583
  /* Reject ET_CORE (header indicates core file, not object file) */
584
2.16M
  if (i_ehdrp->e_type == ET_CORE)
585
702k
    goto got_wrong_format_error;
586
587
  /* If this is a relocatable file and there is no section header
588
     table, then we're hosed.  */
589
1.46M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_type == ET_REL)
590
25.0k
    goto got_wrong_format_error;
591
592
  /* As a simple sanity check, verify that what BFD thinks is the
593
     size of each section header table entry actually matches the size
594
     recorded in the file, but only if there are any sections.  */
595
1.44M
  if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
596
53.9k
    goto got_wrong_format_error;
597
598
  /* Further sanity check.  */
599
1.38M
  if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_shnum != 0)
600
594
    goto got_wrong_format_error;
601
602
1.38M
  ebd = get_elf_backend_data (abfd);
603
1.38M
  if (ebd->s->arch_size != ARCH_SIZE)
604
0
    goto got_wrong_format_error;
605
606
  /* Check that the ELF e_machine field matches what this particular
607
     BFD format expects.  */
608
1.38M
  if (ebd->elf_machine_code != i_ehdrp->e_machine
609
1.34M
      && (ebd->elf_machine_alt1 == 0
610
378k
    || i_ehdrp->e_machine != ebd->elf_machine_alt1)
611
1.33M
      && (ebd->elf_machine_alt2 == 0
612
26.4k
    || i_ehdrp->e_machine != ebd->elf_machine_alt2)
613
1.33M
      && ebd->elf_machine_code != EM_NONE)
614
1.31M
    goto got_wrong_format_error;
615
616
68.8k
  if (i_ehdrp->e_type == ET_EXEC)
617
2.58k
    abfd->flags |= EXEC_P;
618
66.2k
  else if (i_ehdrp->e_type == ET_DYN)
619
1.64k
    abfd->flags |= DYNAMIC;
620
621
68.8k
  if (i_ehdrp->e_phnum > 0)
622
28.8k
    abfd->flags |= D_PAGED;
623
624
68.8k
  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
625
21.1k
    {
626
      /* It's OK if this fails for the generic target.  */
627
21.1k
      if (ebd->elf_machine_code != EM_NONE)
628
16
  goto got_no_match;
629
21.1k
    }
630
631
68.8k
  if (ebd->osabi_exact && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi)
632
3.97k
    goto got_wrong_format_error;
633
634
64.8k
  if (i_ehdrp->e_shoff >= sizeof (x_ehdr))
635
53.7k
    {
636
53.7k
      file_ptr where = (file_ptr) i_ehdrp->e_shoff;
637
638
      /* Seek to the section header table in the file.  */
639
53.7k
      if (bfd_seek (abfd, where, SEEK_SET) != 0)
640
0
  goto got_no_match;
641
642
      /* Read the first section header at index 0, and convert to internal
643
   form.  */
644
53.7k
      if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr)
645
52.8k
    || !elf_swap_shdr_in (abfd, &x_shdr, &i_shdr))
646
967
  goto got_no_match;
647
648
      /* If the section count is zero, the actual count is in the first
649
   section header.  */
650
52.8k
      if (i_ehdrp->e_shnum == SHN_UNDEF)
651
22.5k
  {
652
22.5k
    i_ehdrp->e_shnum = i_shdr.sh_size;
653
22.5k
    if (i_ehdrp->e_shnum >= SHN_LORESERVE
654
22.3k
        || i_ehdrp->e_shnum != i_shdr.sh_size
655
22.3k
        || i_ehdrp->e_shnum  == 0)
656
338
      goto got_wrong_format_error;
657
22.5k
  }
658
659
      /* And similarly for the string table index.  */
660
52.4k
      if (i_ehdrp->e_shstrndx == (SHN_XINDEX & 0xffff))
661
574
  {
662
574
    i_ehdrp->e_shstrndx = i_shdr.sh_link;
663
574
    if (i_ehdrp->e_shstrndx != i_shdr.sh_link)
664
0
      goto got_wrong_format_error;
665
574
  }
666
667
      /* And program headers.  */
668
52.4k
      if (i_ehdrp->e_phnum == PN_XNUM && i_shdr.sh_info != 0)
669
545
  {
670
545
    i_ehdrp->e_phnum = i_shdr.sh_info;
671
545
    if (i_ehdrp->e_phnum != i_shdr.sh_info)
672
0
      goto got_wrong_format_error;
673
545
  }
674
675
      /* Sanity check that we can read all of the section headers.
676
   It ought to be good enough to just read the last one.  */
677
52.4k
      if (i_ehdrp->e_shnum != 1)
678
51.0k
  {
679
    /* Check that we don't have a totally silly number of sections.  */
680
51.0k
    if (i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (x_shdr)
681
50.2k
        || i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (i_shdr))
682
954
      goto got_wrong_format_error;
683
684
50.1k
    where += (i_ehdrp->e_shnum - 1) * sizeof (x_shdr);
685
50.1k
    if ((bfd_size_type) where <= i_ehdrp->e_shoff)
686
0
      goto got_wrong_format_error;
687
688
50.1k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
689
0
      goto got_no_match;
690
50.1k
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
691
644
      goto got_no_match;
692
693
    /* Back to where we were.  */
694
49.4k
    where = i_ehdrp->e_shoff + sizeof (x_shdr);
695
49.4k
    if (bfd_seek (abfd, where, SEEK_SET) != 0)
696
0
      goto got_no_match;
697
49.4k
  }
698
52.4k
    }
699
700
  /* Allocate space for a copy of the section header table in
701
     internal form.  */
702
61.9k
  if (i_ehdrp->e_shnum != 0)
703
50.8k
    {
704
50.8k
      Elf_Internal_Shdr *shdrp;
705
50.8k
      unsigned int num_sec;
706
50.8k
      size_t amt;
707
708
50.8k
      if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*i_shdrp), &amt))
709
0
  goto got_wrong_format_error;
710
50.8k
      i_shdrp = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
711
50.8k
      if (!i_shdrp)
712
0
  goto got_no_match;
713
50.8k
      num_sec = i_ehdrp->e_shnum;
714
50.8k
      elf_numsections (abfd) = num_sec;
715
50.8k
      if (_bfd_mul_overflow (num_sec, sizeof (i_shdrp), &amt))
716
0
  goto got_wrong_format_error;
717
50.8k
      elf_elfsections (abfd) = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
718
50.8k
      if (!elf_elfsections (abfd))
719
0
  goto got_no_match;
720
50.8k
      elf_tdata (abfd)->being_created = bfd_zalloc (abfd, num_sec);
721
50.8k
      if (!elf_tdata (abfd)->being_created)
722
0
  goto got_no_match;
723
724
50.8k
      memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
725
580k
      for (shdrp = i_shdrp, shindex = 0; shindex < num_sec; shindex++)
726
529k
  elf_elfsections (abfd)[shindex] = shdrp++;
727
728
      /* Read in the rest of the section header table and convert it
729
   to internal form.  */
730
514k
      for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
731
465k
  {
732
465k
    if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr)
733
465k
        || !elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex))
734
0
      goto got_no_match;
735
736
    /* Sanity check sh_link and sh_info.  */
737
465k
    if (i_shdrp[shindex].sh_link >= num_sec)
738
2.22k
      {
739
        /* PR 10478: Accept Solaris binaries with a sh_link
740
     field set to SHN_BEFORE or SHN_AFTER.  */
741
2.22k
        switch (ebd->elf_machine_code)
742
2.22k
    {
743
554
    case EM_386:
744
622
    case EM_IAMCU:
745
748
    case EM_X86_64:
746
748
    case EM_OLD_SPARCV9:
747
748
    case EM_SPARC32PLUS:
748
748
    case EM_SPARCV9:
749
889
    case EM_SPARC:
750
889
      if (i_shdrp[shindex].sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
751
502
          || i_shdrp[shindex].sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
752
466
        break;
753
      /* Otherwise fall through.  */
754
1.75k
    default:
755
1.75k
      goto got_wrong_format_error;
756
2.22k
    }
757
2.22k
      }
758
759
464k
    if (((i_shdrp[shindex].sh_flags & SHF_INFO_LINK)
760
411k
         || i_shdrp[shindex].sh_type == SHT_RELA
761
407k
         || i_shdrp[shindex].sh_type == SHT_REL)
762
80.4k
        && i_shdrp[shindex].sh_info >= num_sec)
763
697
      goto got_wrong_format_error;
764
765
    /* If the section is loaded, but not page aligned, clear
766
       D_PAGED.  */
767
463k
    if (i_shdrp[shindex].sh_size != 0
768
380k
        && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
769
98.7k
        && i_shdrp[shindex].sh_type != SHT_NOBITS
770
91.8k
        && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
771
91.8k
       % ebd->minpagesize)
772
91.8k
      != 0))
773
59.6k
      abfd->flags &= ~D_PAGED;
774
463k
  }
775
776
48.4k
      if (i_ehdrp->e_shstrndx >= elf_numsections (abfd)
777
46.8k
    || i_shdrp[i_ehdrp->e_shstrndx].sh_type != SHT_STRTAB)
778
3.17k
  {
779
    /* PR 2257:
780
       We used to just goto got_wrong_format_error here
781
       but there are binaries in existance for which this test
782
       will prevent the binutils from working with them at all.
783
       So we are kind, and reset the string index value to 0
784
       so that at least some processing can be done.  */
785
3.17k
    i_ehdrp->e_shstrndx = SHN_UNDEF;
786
3.17k
    if (!abfd->read_only)
787
473
      {
788
473
        _bfd_error_handler
789
473
    (_("warning: %pB has a corrupt string table index"), abfd);
790
473
        abfd->read_only = 1;
791
473
      }
792
3.17k
  }
793
48.4k
    }
794
11.0k
  else if (i_ehdrp->e_shstrndx != SHN_UNDEF)
795
3.92k
    goto got_wrong_format_error;
796
797
  /* Read in the program headers.  */
798
55.5k
  if (i_ehdrp->e_phnum == 0)
799
35.8k
    elf_tdata (abfd)->phdr = NULL;
800
19.7k
  else
801
19.7k
    {
802
19.7k
      Elf_Internal_Phdr *i_phdr;
803
19.7k
      unsigned int i;
804
19.7k
      ufile_ptr filesize;
805
19.7k
      size_t amt;
806
807
      /* Check for a corrupt input file with an impossibly large number
808
   of program headers.  */
809
19.7k
      filesize = bfd_get_file_size (abfd);
810
19.7k
      if (filesize != 0
811
19.7k
    && i_ehdrp->e_phnum > filesize / sizeof (Elf_External_Phdr))
812
877
  goto got_wrong_format_error;
813
18.8k
      if (_bfd_mul_overflow (i_ehdrp->e_phnum, sizeof (*i_phdr), &amt))
814
0
  goto got_wrong_format_error;
815
18.8k
      elf_tdata (abfd)->phdr
816
18.8k
  = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
817
18.8k
      if (elf_tdata (abfd)->phdr == NULL)
818
0
  goto got_no_match;
819
18.8k
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
820
0
  goto got_no_match;
821
18.8k
      bool eu_strip_broken_phdrs = false;
822
18.8k
      i_phdr = elf_tdata (abfd)->phdr;
823
391k
      for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
824
373k
  {
825
373k
    Elf_External_Phdr x_phdr;
826
827
373k
    if (bfd_read (&x_phdr, sizeof x_phdr, abfd) != sizeof x_phdr)
828
922
      goto got_no_match;
829
372k
    elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
830
    /* Too much code in BFD relies on alignment being a power of
831
       two, as required by the ELF spec.  */
832
372k
    if (i_phdr->p_align != (i_phdr->p_align & -i_phdr->p_align))
833
232k
      {
834
232k
        i_phdr->p_align &= -i_phdr->p_align;
835
232k
        if (!abfd->read_only)
836
3.13k
    {
837
3.13k
      _bfd_error_handler (_("warning: %pB has a program header "
838
3.13k
          "with invalid alignment"), abfd);
839
3.13k
      abfd->read_only = 1;
840
3.13k
    }
841
232k
      }
842
    /* Detect eu-strip -f debug files, which have program
843
       headers that describe the original file.  */
844
372k
    if (i_phdr->p_filesz != 0
845
275k
        && (i_phdr->p_filesz > filesize
846
61.4k
      || i_phdr->p_offset > filesize - i_phdr->p_filesz))
847
239k
      eu_strip_broken_phdrs = true;
848
372k
  }
849
17.9k
      if (!eu_strip_broken_phdrs
850
1.83k
    && i_ehdrp->e_shoff == 0
851
1.32k
    && i_ehdrp->e_shstrndx == 0)
852
1.32k
  {
853
    /* Try to reconstruct dynamic symbol table from PT_DYNAMIC
854
       segment if there is no section header.  */
855
1.32k
    i_phdr = elf_tdata (abfd)->phdr;
856
4.89k
    for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
857
4.62k
      if (i_phdr->p_type == PT_DYNAMIC)
858
1.05k
        {
859
1.05k
    if (i_phdr->p_filesz != 0
860
959
        && !_bfd_elf_get_dynamic_symbols (abfd, i_phdr,
861
959
                  elf_tdata (abfd)->phdr,
862
959
                  i_ehdrp->e_phnum,
863
959
                  filesize))
864
959
      goto got_no_match;
865
99
    break;
866
1.05k
        }
867
1.32k
  }
868
17.9k
    }
869
870
52.8k
  if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff >= sizeof (x_ehdr))
871
45.1k
    {
872
45.1k
      unsigned int num_sec;
873
874
      /* Once all of the section headers have been read and converted, we
875
   can start processing them.  Note that the first section header is
876
   a dummy placeholder entry, so we ignore it.  */
877
45.1k
      num_sec = elf_numsections (abfd);
878
439k
      for (shindex = 1; shindex < num_sec; shindex++)
879
402k
  if (!bfd_section_from_shdr (abfd, shindex))
880
8.37k
    goto got_no_match;
881
882
      /* Set up ELF sections for SHF_GROUP and SHF_LINK_ORDER.  */
883
36.7k
      if (! _bfd_elf_setup_sections (abfd))
884
2.61k
  goto got_wrong_format_error;
885
36.7k
    }
886
887
  /* Let the backend double check the format and override global
888
     information.  */
889
41.8k
  if (ebd->elf_backend_object_p)
890
25.7k
    {
891
25.7k
      if (! (*ebd->elf_backend_object_p) (abfd))
892
5.12k
  goto got_wrong_format_error;
893
25.7k
    }
894
895
  /* Remember the entry point specified in the ELF file header.  */
896
36.6k
  bfd_set_start_address (abfd, i_ehdrp->e_entry);
897
898
  /* If we have created any reloc sections that are associated with
899
     debugging sections, mark the reloc sections as debugging as well.  */
900
210k
  for (s = abfd->sections; s != NULL; s = s->next)
901
174k
    {
902
174k
      if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL
903
171k
     || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
904
2.72k
    && elf_section_data (s)->this_hdr.sh_info > 0)
905
1.88k
  {
906
1.88k
    unsigned long targ_index;
907
1.88k
    asection *targ_sec;
908
909
1.88k
    targ_index = elf_section_data (s)->this_hdr.sh_info;
910
1.88k
    targ_sec = bfd_section_from_elf_index (abfd, targ_index);
911
1.88k
    if (targ_sec != NULL
912
1.59k
        && (targ_sec->flags & SEC_DEBUGGING) != 0)
913
136
      s->flags |= SEC_DEBUGGING;
914
1.88k
  }
915
174k
    }
916
36.6k
  return _bfd_no_cleanup;
917
918
25.7M
 got_wrong_format_error:
919
25.7M
  bfd_set_error (bfd_error_wrong_format);
920
921
25.8M
 got_no_match:
922
25.8M
  if (abfd->sections != NULL)
923
    /* Unlike most early section processing, the object attribute v2
924
       code uses bfd_malloc rather than bfd_alloc.  Free memory
925
       containing attributes.  */
926
13.4k
    _bfd_elf_cleanup_object_attributes (abfd);
927
  return NULL;
928
25.7M
}
929

930
/* ELF .o/exec file writing */
931
932
/* Write out the relocs.  */
933
934
void
935
elf_write_relocs (bfd *abfd, asection *sec, void *data)
936
1.54k
{
937
1.54k
  elf_backend_data *bed = get_elf_backend_data (abfd);
938
1.54k
  bool *failedp = (bool *) data;
939
1.54k
  Elf_Internal_Shdr *rela_hdr;
940
1.54k
  bfd_vma addr_offset;
941
1.54k
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
942
1.54k
  size_t extsize;
943
1.54k
  bfd_byte *dst_rela;
944
1.54k
  unsigned int idx;
945
1.54k
  asymbol *last_sym;
946
1.54k
  int last_sym_idx;
947
1.54k
  size_t amt;
948
949
  /* If we have already failed, don't do anything.  */
950
1.54k
  if (*failedp)
951
0
    return;
952
953
1.54k
  if ((sec->flags & SEC_RELOC) == 0)
954
1.42k
    return;
955
956
  /* The linker backend writes the relocs out itself, and sets the
957
     reloc_count field to zero to inhibit writing them here.  Also,
958
     sometimes the SEC_RELOC flag gets set even when there aren't any
959
     relocs.  */
960
120
  if (sec->reloc_count == 0)
961
3
    return;
962
963
  /* If we have opened an existing file for update, reloc_count may be
964
     set even though we are not linking.  In that case we have nothing
965
     to do.  */
966
117
  if (sec->orelocation == NULL)
967
0
    return;
968
969
117
  rela_hdr = elf_section_data (sec)->rela.hdr;
970
117
  if (rela_hdr == NULL)
971
0
    rela_hdr = elf_section_data (sec)->rel.hdr;
972
973
117
  rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
974
117
  if (_bfd_mul_overflow (sec->reloc_count, rela_hdr->sh_entsize, &amt)
975
117
      || (rela_hdr->contents = bfd_alloc (abfd, amt)) == NULL)
976
0
    {
977
0
      bfd_set_error (bfd_error_no_memory);
978
0
      *failedp = true;
979
0
      return;
980
0
    }
981
982
  /* Figure out whether the relocations are RELA or REL relocations.  */
983
117
  if (rela_hdr->sh_type == SHT_RELA)
984
117
    {
985
117
      swap_out = elf_swap_reloca_out;
986
117
      extsize = sizeof (Elf_External_Rela);
987
117
    }
988
0
  else if (rela_hdr->sh_type == SHT_REL)
989
0
    {
990
0
      swap_out = elf_swap_reloc_out;
991
0
      extsize = sizeof (Elf_External_Rel);
992
0
    }
993
0
  else
994
    /* Every relocation section should be either an SHT_RELA or an
995
       SHT_REL section.  */
996
0
    abort ();
997
998
  /* The address of an ELF reloc is section relative for an object
999
     file, and absolute for an executable file or shared library.
1000
     The address of a BFD reloc is always section relative.  */
1001
117
  addr_offset = 0;
1002
117
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1003
2
    addr_offset = sec->vma;
1004
1005
  /* orelocation has the data, reloc_count has the count...  */
1006
117
  last_sym = 0;
1007
117
  last_sym_idx = 0;
1008
117
  dst_rela = rela_hdr->contents;
1009
1010
1.09k
  for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
1011
975
    {
1012
975
      Elf_Internal_Rela src_rela;
1013
975
      arelent *ptr;
1014
975
      asymbol *sym;
1015
975
      int n;
1016
1017
975
      ptr = sec->orelocation[idx];
1018
975
      sym = *ptr->sym_ptr_ptr;
1019
975
      if (sym == last_sym)
1020
142
  n = last_sym_idx;
1021
      /* If the relocation is against an absolute symbol whoes value is
1022
   zero, then the symbol can be dropped, simplifying the reloc.
1023
   PR 31106: Except for complex relocations where the symbols
1024
   itself might be significant.  */
1025
833
      else if (bfd_is_abs_section (sym->section)
1026
410
         && sym->value == 0
1027
379
         && (sym->flags & BSF_RELC) == 0)
1028
379
  n = STN_UNDEF;
1029
454
      else
1030
454
  {
1031
454
    last_sym = sym;
1032
454
    n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
1033
454
    if (n < 0)
1034
0
      {
1035
0
        *failedp = true;
1036
0
        return;
1037
0
      }
1038
454
    last_sym_idx = n;
1039
454
  }
1040
1041
975
      if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
1042
599
    && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
1043
0
    && ! _bfd_elf_validate_reloc (abfd, ptr))
1044
0
  {
1045
0
    *failedp = true;
1046
0
    return;
1047
0
  }
1048
1049
975
      if (ptr->howto == NULL)
1050
0
  {
1051
0
    *failedp = true;
1052
0
    return;
1053
0
  }
1054
1055
#if defined(BFD64) && ARCH_SIZE == 32
1056
479
      if (rela_hdr->sh_type == SHT_RELA
1057
479
    && ptr->howto->bitsize > 32
1058
10
    && ptr->addend - INT32_MIN > UINT32_MAX)
1059
0
  {
1060
0
    _bfd_error_handler (_("%pB: %pA+%" PRIx64 ": "
1061
0
        "relocation addend %" PRIx64 " too large"),
1062
0
            abfd, sec, (uint64_t) ptr->address,
1063
0
            (uint64_t) ptr->addend);
1064
0
    *failedp = true;
1065
0
    bfd_set_error (bfd_error_bad_value);
1066
0
  }
1067
#endif
1068
1069
496
      src_rela.r_offset = ptr->address + addr_offset;
1070
975
      src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
1071
496
      src_rela.r_addend = ptr->addend;
1072
496
      (*swap_out) (abfd, &src_rela, dst_rela);
1073
496
    }
1074
1075
117
  if (elf_section_data (sec)->has_secondary_relocs
1076
1
      && !bed->write_secondary_relocs (abfd, sec))
1077
0
    {
1078
0
      *failedp = true;
1079
0
      return;
1080
0
    }
1081
117
}
bfd_elf64_write_relocs
Line
Count
Source
936
809
{
937
809
  elf_backend_data *bed = get_elf_backend_data (abfd);
938
809
  bool *failedp = (bool *) data;
939
809
  Elf_Internal_Shdr *rela_hdr;
940
809
  bfd_vma addr_offset;
941
809
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
942
809
  size_t extsize;
943
809
  bfd_byte *dst_rela;
944
809
  unsigned int idx;
945
809
  asymbol *last_sym;
946
809
  int last_sym_idx;
947
809
  size_t amt;
948
949
  /* If we have already failed, don't do anything.  */
950
809
  if (*failedp)
951
0
    return;
952
953
809
  if ((sec->flags & SEC_RELOC) == 0)
954
709
    return;
955
956
  /* The linker backend writes the relocs out itself, and sets the
957
     reloc_count field to zero to inhibit writing them here.  Also,
958
     sometimes the SEC_RELOC flag gets set even when there aren't any
959
     relocs.  */
960
100
  if (sec->reloc_count == 0)
961
1
    return;
962
963
  /* If we have opened an existing file for update, reloc_count may be
964
     set even though we are not linking.  In that case we have nothing
965
     to do.  */
966
99
  if (sec->orelocation == NULL)
967
0
    return;
968
969
99
  rela_hdr = elf_section_data (sec)->rela.hdr;
970
99
  if (rela_hdr == NULL)
971
0
    rela_hdr = elf_section_data (sec)->rel.hdr;
972
973
99
  rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
974
99
  if (_bfd_mul_overflow (sec->reloc_count, rela_hdr->sh_entsize, &amt)
975
99
      || (rela_hdr->contents = bfd_alloc (abfd, amt)) == NULL)
976
0
    {
977
0
      bfd_set_error (bfd_error_no_memory);
978
0
      *failedp = true;
979
0
      return;
980
0
    }
981
982
  /* Figure out whether the relocations are RELA or REL relocations.  */
983
99
  if (rela_hdr->sh_type == SHT_RELA)
984
99
    {
985
99
      swap_out = elf_swap_reloca_out;
986
99
      extsize = sizeof (Elf_External_Rela);
987
99
    }
988
0
  else if (rela_hdr->sh_type == SHT_REL)
989
0
    {
990
0
      swap_out = elf_swap_reloc_out;
991
0
      extsize = sizeof (Elf_External_Rel);
992
0
    }
993
0
  else
994
    /* Every relocation section should be either an SHT_RELA or an
995
       SHT_REL section.  */
996
0
    abort ();
997
998
  /* The address of an ELF reloc is section relative for an object
999
     file, and absolute for an executable file or shared library.
1000
     The address of a BFD reloc is always section relative.  */
1001
99
  addr_offset = 0;
1002
99
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1003
0
    addr_offset = sec->vma;
1004
1005
  /* orelocation has the data, reloc_count has the count...  */
1006
99
  last_sym = 0;
1007
99
  last_sym_idx = 0;
1008
99
  dst_rela = rela_hdr->contents;
1009
1010
595
  for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
1011
496
    {
1012
496
      Elf_Internal_Rela src_rela;
1013
496
      arelent *ptr;
1014
496
      asymbol *sym;
1015
496
      int n;
1016
1017
496
      ptr = sec->orelocation[idx];
1018
496
      sym = *ptr->sym_ptr_ptr;
1019
496
      if (sym == last_sym)
1020
141
  n = last_sym_idx;
1021
      /* If the relocation is against an absolute symbol whoes value is
1022
   zero, then the symbol can be dropped, simplifying the reloc.
1023
   PR 31106: Except for complex relocations where the symbols
1024
   itself might be significant.  */
1025
355
      else if (bfd_is_abs_section (sym->section)
1026
55
         && sym->value == 0
1027
35
         && (sym->flags & BSF_RELC) == 0)
1028
35
  n = STN_UNDEF;
1029
320
      else
1030
320
  {
1031
320
    last_sym = sym;
1032
320
    n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
1033
320
    if (n < 0)
1034
0
      {
1035
0
        *failedp = true;
1036
0
        return;
1037
0
      }
1038
320
    last_sym_idx = n;
1039
320
  }
1040
1041
496
      if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
1042
463
    && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
1043
0
    && ! _bfd_elf_validate_reloc (abfd, ptr))
1044
0
  {
1045
0
    *failedp = true;
1046
0
    return;
1047
0
  }
1048
1049
496
      if (ptr->howto == NULL)
1050
0
  {
1051
0
    *failedp = true;
1052
0
    return;
1053
0
  }
1054
1055
#if defined(BFD64) && ARCH_SIZE == 32
1056
      if (rela_hdr->sh_type == SHT_RELA
1057
    && ptr->howto->bitsize > 32
1058
    && ptr->addend - INT32_MIN > UINT32_MAX)
1059
  {
1060
    _bfd_error_handler (_("%pB: %pA+%" PRIx64 ": "
1061
        "relocation addend %" PRIx64 " too large"),
1062
            abfd, sec, (uint64_t) ptr->address,
1063
            (uint64_t) ptr->addend);
1064
    *failedp = true;
1065
    bfd_set_error (bfd_error_bad_value);
1066
  }
1067
#endif
1068
1069
496
      src_rela.r_offset = ptr->address + addr_offset;
1070
496
      src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
1071
496
      src_rela.r_addend = ptr->addend;
1072
496
      (*swap_out) (abfd, &src_rela, dst_rela);
1073
496
    }
1074
1075
99
  if (elf_section_data (sec)->has_secondary_relocs
1076
1
      && !bed->write_secondary_relocs (abfd, sec))
1077
0
    {
1078
      *failedp = true;
1079
0
      return;
1080
0
    }
1081
99
}
bfd_elf32_write_relocs
Line
Count
Source
936
738
{
937
738
  elf_backend_data *bed = get_elf_backend_data (abfd);
938
738
  bool *failedp = (bool *) data;
939
738
  Elf_Internal_Shdr *rela_hdr;
940
738
  bfd_vma addr_offset;
941
738
  void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
942
738
  size_t extsize;
943
738
  bfd_byte *dst_rela;
944
738
  unsigned int idx;
945
738
  asymbol *last_sym;
946
738
  int last_sym_idx;
947
738
  size_t amt;
948
949
  /* If we have already failed, don't do anything.  */
950
738
  if (*failedp)
951
0
    return;
952
953
738
  if ((sec->flags & SEC_RELOC) == 0)
954
718
    return;
955
956
  /* The linker backend writes the relocs out itself, and sets the
957
     reloc_count field to zero to inhibit writing them here.  Also,
958
     sometimes the SEC_RELOC flag gets set even when there aren't any
959
     relocs.  */
960
20
  if (sec->reloc_count == 0)
961
2
    return;
962
963
  /* If we have opened an existing file for update, reloc_count may be
964
     set even though we are not linking.  In that case we have nothing
965
     to do.  */
966
18
  if (sec->orelocation == NULL)
967
0
    return;
968
969
18
  rela_hdr = elf_section_data (sec)->rela.hdr;
970
18
  if (rela_hdr == NULL)
971
0
    rela_hdr = elf_section_data (sec)->rel.hdr;
972
973
18
  rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
974
18
  if (_bfd_mul_overflow (sec->reloc_count, rela_hdr->sh_entsize, &amt)
975
18
      || (rela_hdr->contents = bfd_alloc (abfd, amt)) == NULL)
976
0
    {
977
0
      bfd_set_error (bfd_error_no_memory);
978
0
      *failedp = true;
979
0
      return;
980
0
    }
981
982
  /* Figure out whether the relocations are RELA or REL relocations.  */
983
18
  if (rela_hdr->sh_type == SHT_RELA)
984
18
    {
985
18
      swap_out = elf_swap_reloca_out;
986
18
      extsize = sizeof (Elf_External_Rela);
987
18
    }
988
0
  else if (rela_hdr->sh_type == SHT_REL)
989
0
    {
990
0
      swap_out = elf_swap_reloc_out;
991
0
      extsize = sizeof (Elf_External_Rel);
992
0
    }
993
0
  else
994
    /* Every relocation section should be either an SHT_RELA or an
995
       SHT_REL section.  */
996
0
    abort ();
997
998
  /* The address of an ELF reloc is section relative for an object
999
     file, and absolute for an executable file or shared library.
1000
     The address of a BFD reloc is always section relative.  */
1001
18
  addr_offset = 0;
1002
18
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1003
2
    addr_offset = sec->vma;
1004
1005
  /* orelocation has the data, reloc_count has the count...  */
1006
18
  last_sym = 0;
1007
18
  last_sym_idx = 0;
1008
18
  dst_rela = rela_hdr->contents;
1009
1010
497
  for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
1011
479
    {
1012
479
      Elf_Internal_Rela src_rela;
1013
479
      arelent *ptr;
1014
479
      asymbol *sym;
1015
479
      int n;
1016
1017
479
      ptr = sec->orelocation[idx];
1018
479
      sym = *ptr->sym_ptr_ptr;
1019
479
      if (sym == last_sym)
1020
1
  n = last_sym_idx;
1021
      /* If the relocation is against an absolute symbol whoes value is
1022
   zero, then the symbol can be dropped, simplifying the reloc.
1023
   PR 31106: Except for complex relocations where the symbols
1024
   itself might be significant.  */
1025
478
      else if (bfd_is_abs_section (sym->section)
1026
355
         && sym->value == 0
1027
344
         && (sym->flags & BSF_RELC) == 0)
1028
344
  n = STN_UNDEF;
1029
134
      else
1030
134
  {
1031
134
    last_sym = sym;
1032
134
    n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
1033
134
    if (n < 0)
1034
0
      {
1035
0
        *failedp = true;
1036
0
        return;
1037
0
      }
1038
134
    last_sym_idx = n;
1039
134
  }
1040
1041
479
      if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
1042
136
    && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
1043
0
    && ! _bfd_elf_validate_reloc (abfd, ptr))
1044
0
  {
1045
0
    *failedp = true;
1046
0
    return;
1047
0
  }
1048
1049
479
      if (ptr->howto == NULL)
1050
0
  {
1051
0
    *failedp = true;
1052
0
    return;
1053
0
  }
1054
1055
479
#if defined(BFD64) && ARCH_SIZE == 32
1056
479
      if (rela_hdr->sh_type == SHT_RELA
1057
479
    && ptr->howto->bitsize > 32
1058
10
    && ptr->addend - INT32_MIN > UINT32_MAX)
1059
0
  {
1060
0
    _bfd_error_handler (_("%pB: %pA+%" PRIx64 ": "
1061
0
        "relocation addend %" PRIx64 " too large"),
1062
0
            abfd, sec, (uint64_t) ptr->address,
1063
0
            (uint64_t) ptr->addend);
1064
0
    *failedp = true;
1065
0
    bfd_set_error (bfd_error_bad_value);
1066
0
  }
1067
479
#endif
1068
1069
479
      src_rela.r_offset = ptr->address + addr_offset;
1070
479
      src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
1071
479
      src_rela.r_addend = ptr->addend;
1072
479
      (*swap_out) (abfd, &src_rela, dst_rela);
1073
479
    }
1074
1075
18
  if (elf_section_data (sec)->has_secondary_relocs
1076
0
      && !bed->write_secondary_relocs (abfd, sec))
1077
0
    {
1078
      *failedp = true;
1079
0
      return;
1080
0
    }
1081
18
}
1082
1083
/* Write out the program headers.  */
1084
1085
int
1086
elf_write_out_phdrs (bfd *abfd,
1087
         const Elf_Internal_Phdr *phdr,
1088
         unsigned int count)
1089
165
{
1090
669
  while (count--)
1091
504
    {
1092
504
      Elf_External_Phdr extphdr;
1093
1094
504
      elf_swap_phdr_out (abfd, phdr, &extphdr);
1095
504
      if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), abfd)
1096
504
    != sizeof (Elf_External_Phdr))
1097
0
  return -1;
1098
504
      phdr++;
1099
504
    }
1100
165
  return 0;
1101
165
}
bfd_elf64_write_out_phdrs
Line
Count
Source
1089
56
{
1090
267
  while (count--)
1091
211
    {
1092
211
      Elf_External_Phdr extphdr;
1093
1094
211
      elf_swap_phdr_out (abfd, phdr, &extphdr);
1095
211
      if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), abfd)
1096
211
    != sizeof (Elf_External_Phdr))
1097
0
  return -1;
1098
211
      phdr++;
1099
211
    }
1100
56
  return 0;
1101
56
}
bfd_elf32_write_out_phdrs
Line
Count
Source
1089
109
{
1090
402
  while (count--)
1091
293
    {
1092
293
      Elf_External_Phdr extphdr;
1093
1094
293
      elf_swap_phdr_out (abfd, phdr, &extphdr);
1095
293
      if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), abfd)
1096
293
    != sizeof (Elf_External_Phdr))
1097
0
  return -1;
1098
293
      phdr++;
1099
293
    }
1100
109
  return 0;
1101
109
}
1102
1103
/* Write out the section headers and the ELF file header.  */
1104
1105
bool
1106
elf_write_shdrs_and_ehdr (bfd *abfd)
1107
633
{
1108
633
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
1109
633
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1110
633
  Elf_External_Shdr *x_shdrp;  /* Section header table, external form */
1111
633
  Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
1112
633
  unsigned int count;
1113
633
  size_t amt;
1114
1115
633
  i_ehdrp = elf_elfheader (abfd);
1116
633
  i_shdrp = elf_elfsections (abfd);
1117
1118
  /* swap the header before spitting it out...  */
1119
1120
#if DEBUG & 1
1121
  elf_debug_file (i_ehdrp);
1122
#endif
1123
633
  elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
1124
633
  amt = sizeof (x_ehdr);
1125
633
  if (bfd_seek (abfd, 0, SEEK_SET) != 0
1126
633
      || bfd_write (&x_ehdr, amt, abfd) != amt)
1127
0
    return false;
1128
1129
633
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
1130
0
    return true;
1131
1132
  /* Some fields in the first section header handle overflow of ehdr
1133
     fields.  */
1134
633
  if (i_ehdrp->e_phnum >= PN_XNUM)
1135
0
    i_shdrp[0]->sh_info = i_ehdrp->e_phnum;
1136
633
  if (i_ehdrp->e_shnum >= (SHN_LORESERVE & 0xffff))
1137
0
    i_shdrp[0]->sh_size = i_ehdrp->e_shnum;
1138
633
  if (i_ehdrp->e_shstrndx >= (SHN_LORESERVE & 0xffff))
1139
0
    i_shdrp[0]->sh_link = i_ehdrp->e_shstrndx;
1140
1141
  /* at this point we've concocted all the ELF sections...  */
1142
633
  if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*x_shdrp), &amt))
1143
0
    {
1144
0
      bfd_set_error (bfd_error_no_memory);
1145
0
      return false;
1146
0
    }
1147
633
  x_shdrp = (Elf_External_Shdr *) bfd_alloc (abfd, amt);
1148
633
  if (!x_shdrp)
1149
0
    return false;
1150
1151
3.89k
  for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++)
1152
3.25k
    {
1153
#if DEBUG & 2
1154
      elf_debug_section (count, *i_shdrp);
1155
#endif
1156
3.25k
      elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count);
1157
3.25k
    }
1158
633
  amt = (bfd_size_type) i_ehdrp->e_shnum * sizeof (*x_shdrp);
1159
633
  if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0
1160
633
      || bfd_write (x_shdrp, amt, abfd) != amt)
1161
0
    return false;
1162
1163
  /* need to dump the string table too...  */
1164
1165
633
  return true;
1166
633
}
bfd_elf64_write_shdrs_and_ehdr
Line
Count
Source
1107
201
{
1108
201
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
1109
201
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1110
201
  Elf_External_Shdr *x_shdrp;  /* Section header table, external form */
1111
201
  Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
1112
201
  unsigned int count;
1113
201
  size_t amt;
1114
1115
201
  i_ehdrp = elf_elfheader (abfd);
1116
201
  i_shdrp = elf_elfsections (abfd);
1117
1118
  /* swap the header before spitting it out...  */
1119
1120
#if DEBUG & 1
1121
  elf_debug_file (i_ehdrp);
1122
#endif
1123
201
  elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
1124
201
  amt = sizeof (x_ehdr);
1125
201
  if (bfd_seek (abfd, 0, SEEK_SET) != 0
1126
201
      || bfd_write (&x_ehdr, amt, abfd) != amt)
1127
0
    return false;
1128
1129
201
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
1130
0
    return true;
1131
1132
  /* Some fields in the first section header handle overflow of ehdr
1133
     fields.  */
1134
201
  if (i_ehdrp->e_phnum >= PN_XNUM)
1135
0
    i_shdrp[0]->sh_info = i_ehdrp->e_phnum;
1136
201
  if (i_ehdrp->e_shnum >= (SHN_LORESERVE & 0xffff))
1137
0
    i_shdrp[0]->sh_size = i_ehdrp->e_shnum;
1138
201
  if (i_ehdrp->e_shstrndx >= (SHN_LORESERVE & 0xffff))
1139
0
    i_shdrp[0]->sh_link = i_ehdrp->e_shstrndx;
1140
1141
  /* at this point we've concocted all the ELF sections...  */
1142
201
  if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*x_shdrp), &amt))
1143
0
    {
1144
0
      bfd_set_error (bfd_error_no_memory);
1145
0
      return false;
1146
0
    }
1147
201
  x_shdrp = (Elf_External_Shdr *) bfd_alloc (abfd, amt);
1148
201
  if (!x_shdrp)
1149
0
    return false;
1150
1151
1.73k
  for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++)
1152
1.52k
    {
1153
#if DEBUG & 2
1154
      elf_debug_section (count, *i_shdrp);
1155
#endif
1156
1.52k
      elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count);
1157
1.52k
    }
1158
201
  amt = (bfd_size_type) i_ehdrp->e_shnum * sizeof (*x_shdrp);
1159
201
  if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0
1160
201
      || bfd_write (x_shdrp, amt, abfd) != amt)
1161
0
    return false;
1162
1163
  /* need to dump the string table too...  */
1164
1165
201
  return true;
1166
201
}
bfd_elf32_write_shdrs_and_ehdr
Line
Count
Source
1107
432
{
1108
432
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
1109
432
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1110
432
  Elf_External_Shdr *x_shdrp;  /* Section header table, external form */
1111
432
  Elf_Internal_Shdr **i_shdrp;  /* Section header table, internal form */
1112
432
  unsigned int count;
1113
432
  size_t amt;
1114
1115
432
  i_ehdrp = elf_elfheader (abfd);
1116
432
  i_shdrp = elf_elfsections (abfd);
1117
1118
  /* swap the header before spitting it out...  */
1119
1120
#if DEBUG & 1
1121
  elf_debug_file (i_ehdrp);
1122
#endif
1123
432
  elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
1124
432
  amt = sizeof (x_ehdr);
1125
432
  if (bfd_seek (abfd, 0, SEEK_SET) != 0
1126
432
      || bfd_write (&x_ehdr, amt, abfd) != amt)
1127
0
    return false;
1128
1129
432
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
1130
0
    return true;
1131
1132
  /* Some fields in the first section header handle overflow of ehdr
1133
     fields.  */
1134
432
  if (i_ehdrp->e_phnum >= PN_XNUM)
1135
0
    i_shdrp[0]->sh_info = i_ehdrp->e_phnum;
1136
432
  if (i_ehdrp->e_shnum >= (SHN_LORESERVE & 0xffff))
1137
0
    i_shdrp[0]->sh_size = i_ehdrp->e_shnum;
1138
432
  if (i_ehdrp->e_shstrndx >= (SHN_LORESERVE & 0xffff))
1139
0
    i_shdrp[0]->sh_link = i_ehdrp->e_shstrndx;
1140
1141
  /* at this point we've concocted all the ELF sections...  */
1142
432
  if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*x_shdrp), &amt))
1143
0
    {
1144
0
      bfd_set_error (bfd_error_no_memory);
1145
0
      return false;
1146
0
    }
1147
432
  x_shdrp = (Elf_External_Shdr *) bfd_alloc (abfd, amt);
1148
432
  if (!x_shdrp)
1149
0
    return false;
1150
1151
2.16k
  for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++)
1152
1.73k
    {
1153
#if DEBUG & 2
1154
      elf_debug_section (count, *i_shdrp);
1155
#endif
1156
1.73k
      elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count);
1157
1.73k
    }
1158
432
  amt = (bfd_size_type) i_ehdrp->e_shnum * sizeof (*x_shdrp);
1159
432
  if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0
1160
432
      || bfd_write (x_shdrp, amt, abfd) != amt)
1161
0
    return false;
1162
1163
  /* need to dump the string table too...  */
1164
1165
432
  return true;
1166
432
}
1167
1168
bool
1169
elf_checksum_contents (bfd *abfd,
1170
           void (*process) (const void *, size_t, void *),
1171
           void *arg)
1172
0
{
1173
0
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
1174
0
  Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
1175
0
  Elf_Internal_Phdr *i_phdrp = elf_tdata (abfd)->phdr;
1176
0
  unsigned int count, num;
1177
1178
0
  {
1179
0
    Elf_External_Ehdr x_ehdr;
1180
0
    Elf_Internal_Ehdr i_ehdr;
1181
1182
0
    i_ehdr = *i_ehdrp;
1183
0
    i_ehdr.e_phoff = i_ehdr.e_shoff = 0;
1184
0
    elf_swap_ehdr_out (abfd, &i_ehdr, &x_ehdr);
1185
0
    (*process) (&x_ehdr, sizeof x_ehdr, arg);
1186
0
  }
1187
1188
0
  num = i_ehdrp->e_phnum;
1189
0
  for (count = 0; count < num; count++)
1190
0
    {
1191
0
      Elf_External_Phdr x_phdr;
1192
0
      elf_swap_phdr_out (abfd, &i_phdrp[count], &x_phdr);
1193
0
      (*process) (&x_phdr, sizeof x_phdr, arg);
1194
0
    }
1195
1196
0
  num = elf_numsections (abfd);
1197
0
  for (count = 0; count < num; count++)
1198
0
    {
1199
0
      Elf_Internal_Shdr i_shdr;
1200
0
      Elf_External_Shdr x_shdr;
1201
0
      bfd_byte *contents, *free_contents;
1202
0
      asection *sec = NULL;
1203
1204
0
      i_shdr = *i_shdrp[count];
1205
0
      i_shdr.sh_offset = 0;
1206
1207
0
      elf_swap_shdr_out (abfd, &i_shdr, &x_shdr);
1208
0
      (*process) (&x_shdr, sizeof x_shdr, arg);
1209
1210
      /* Process the section's contents, if it has some.
1211
   PR ld/12451: Read them in if necessary.  */
1212
0
      if (i_shdr.sh_type == SHT_NOBITS)
1213
0
  continue;
1214
0
      free_contents = NULL;
1215
0
      contents = i_shdr.contents;
1216
0
      if (contents == NULL)
1217
0
  {
1218
0
    sec = bfd_section_from_elf_index (abfd, count);
1219
0
    if (sec != NULL)
1220
0
      {
1221
0
        contents = sec->contents;
1222
0
        if (contents == NULL)
1223
0
    {
1224
      /* Force rereading from file.  */
1225
0
      sec->flags &= ~SEC_IN_MEMORY;
1226
0
      if (!_bfd_elf_mmap_section_contents (abfd, sec, &free_contents))
1227
0
        continue;
1228
0
      contents = free_contents;
1229
0
    }
1230
0
      }
1231
0
  }
1232
0
      if (contents != NULL)
1233
0
  {
1234
0
    (*process) (contents, i_shdr.sh_size, arg);
1235
0
    _bfd_elf_munmap_section_contents (sec, free_contents);
1236
0
  }
1237
0
    }
1238
1239
0
  return true;
1240
0
}
Unexecuted instantiation: bfd_elf64_checksum_contents
Unexecuted instantiation: bfd_elf32_checksum_contents
1241
1242
long
1243
elf_slurp_symbol_table (bfd *abfd, asymbol **symptrs, bool dynamic)
1244
9.16k
{
1245
9.16k
  Elf_Internal_Shdr *hdr;
1246
9.16k
  Elf_Internal_Shdr *verhdr;
1247
9.16k
  unsigned long symcount; /* Number of external ELF symbols */
1248
9.16k
  elf_symbol_type *sym;   /* Pointer to current bfd symbol */
1249
9.16k
  elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
1250
9.16k
  Elf_Internal_Sym *isym;
1251
9.16k
  Elf_Internal_Sym *isymend;
1252
9.16k
  Elf_Internal_Sym *isymbuf = NULL;
1253
9.16k
  Elf_External_Versym *xver;
1254
9.16k
  Elf_External_Versym *xverbuf = NULL;
1255
9.16k
  elf_backend_data *ebd;
1256
9.16k
  size_t amt;
1257
1258
  /* Read each raw ELF symbol, converting from external ELF form to
1259
     internal ELF form, and then using the information to create a
1260
     canonical bfd symbol table entry.
1261
1262
     Note that we allocate the initial bfd canonical symbol buffer
1263
     based on a one-to-one mapping of the ELF symbols to canonical
1264
     symbols.  We actually use all the ELF symbols, so there will be no
1265
     space left over at the end.  When we have all the symbols, we
1266
     build the caller's pointer vector.  */
1267
9.16k
  ebd = get_elf_backend_data (abfd);
1268
1269
9.16k
  if (! dynamic)
1270
8.92k
    {
1271
8.92k
      hdr = &elf_symtab_hdr (abfd);
1272
8.92k
      verhdr = NULL;
1273
8.92k
      symcount = hdr->sh_size / ebd->s->sizeof_sym;
1274
8.92k
    }
1275
241
  else
1276
241
    {
1277
241
      hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1278
241
      if (elf_dynversym (abfd) == 0)
1279
38
  verhdr = NULL;
1280
203
      else
1281
203
  verhdr = &elf_tdata (abfd)->dynversym_hdr;
1282
241
      if ((elf_dynverdef (abfd) != 0
1283
92
     && elf_tdata (abfd)->verdef == NULL)
1284
163
    || (elf_dynverref (abfd) != 0
1285
97
        && elf_tdata (abfd)->verref == NULL)
1286
115
    || elf_tdata (abfd)->dt_verdef != NULL
1287
115
    || elf_tdata (abfd)->dt_verneed != NULL)
1288
128
  {
1289
128
    if (!_bfd_elf_slurp_version_tables (abfd, false))
1290
88
      return -1;
1291
128
  }
1292
1293
153
      symcount = elf_tdata (abfd)->dt_symtab_count;
1294
153
    }
1295
1296
9.07k
  if (symcount == 0)
1297
848
    symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1298
1299
9.07k
  if (symcount == 0)
1300
698
    sym = symbase = NULL;
1301
8.37k
  else
1302
8.37k
    {
1303
8.37k
      size_t i;
1304
1305
8.37k
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
1306
8.37k
              NULL, NULL, NULL);
1307
8.37k
      if (isymbuf == NULL)
1308
961
  return -1;
1309
1310
7.41k
      if (_bfd_mul_overflow (symcount, sizeof (elf_symbol_type), &amt))
1311
0
  {
1312
0
    bfd_set_error (bfd_error_file_too_big);
1313
0
    goto error_return;
1314
0
  }
1315
7.41k
      symbase = (elf_symbol_type *) bfd_zalloc (abfd, amt);
1316
7.41k
      if (symbase == (elf_symbol_type *) NULL)
1317
0
  goto error_return;
1318
1319
      /* Read the raw ELF version symbol information.  */
1320
7.41k
      if (verhdr != NULL
1321
110
    && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
1322
34
  {
1323
34
    _bfd_error_handler
1324
      /* xgettext:c-format */
1325
34
      (_("%pB: version count (%" PRId64 ")"
1326
34
         " does not match symbol count (%ld)"),
1327
34
       abfd,
1328
34
       (int64_t) (verhdr->sh_size / sizeof (Elf_External_Versym)),
1329
34
       symcount);
1330
1331
    /* Slurp in the symbols without the version information,
1332
       since that is more helpful than just quitting.  */
1333
34
    verhdr = NULL;
1334
34
  }
1335
1336
7.41k
      if (verhdr != NULL)
1337
76
  {
1338
76
    if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
1339
1
      goto error_return;
1340
75
    xverbuf = (Elf_External_Versym *)
1341
75
      _bfd_malloc_and_read (abfd, verhdr->sh_size, verhdr->sh_size);
1342
75
    if (xverbuf == NULL && verhdr->sh_size != 0)
1343
4
      goto error_return;
1344
75
  }
1345
1346
      /* Skip first symbol, which is a null dummy.  */
1347
7.40k
      xver = xverbuf;
1348
7.40k
      if (xver != NULL)
1349
71
  ++xver;
1350
7.40k
      isymend = isymbuf + symcount;
1351
7.40k
      for (isym = isymbuf + 1, sym = symbase, i = 1;
1352
173k
     isym < isymend;
1353
166k
     isym++, sym++, i++)
1354
166k
  {
1355
166k
    memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
1356
1357
166k
    sym->symbol.the_bfd = abfd;
1358
166k
    if (elf_use_dt_symtab_p (abfd))
1359
0
      sym->symbol.name = (elf_tdata (abfd)->dt_strtab
1360
0
        + isym->st_name);
1361
166k
    else
1362
166k
      sym->symbol.name = bfd_elf_sym_name (abfd, hdr, isym, NULL);
1363
166k
    sym->symbol.value = isym->st_value;
1364
1365
166k
    if (isym->st_shndx == SHN_UNDEF)
1366
81.7k
      {
1367
81.7k
        sym->symbol.section = bfd_und_section_ptr;
1368
81.7k
      }
1369
84.3k
    else if (isym->st_shndx == SHN_ABS)
1370
2.37k
      {
1371
2.37k
        sym->symbol.section = bfd_abs_section_ptr;
1372
2.37k
      }
1373
81.9k
    else if (isym->st_shndx == SHN_COMMON)
1374
98
      {
1375
98
        sym->symbol.section = bfd_com_section_ptr;
1376
98
        if ((abfd->flags & BFD_PLUGIN) != 0)
1377
0
    {
1378
0
      asection *xc = bfd_get_section_by_name (abfd, "COMMON");
1379
1380
0
      if (xc == NULL)
1381
0
        {
1382
0
          flagword flags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
1383
0
          | SEC_EXCLUDE);
1384
0
          xc = bfd_make_section_with_flags (abfd, "COMMON", flags);
1385
0
          if (xc == NULL)
1386
0
      goto error_return;
1387
0
        }
1388
0
      sym->symbol.section = xc;
1389
0
    }
1390
        /* Elf puts the alignment into the `value' field, and
1391
     the size into the `size' field.  BFD wants to see the
1392
     size in the value field, and doesn't care (at the
1393
     moment) about the alignment.  */
1394
98
        sym->symbol.value = isym->st_size;
1395
98
      }
1396
81.8k
    else if (elf_use_dt_symtab_p (abfd))
1397
0
      {
1398
0
        asection *sec;
1399
0
        sec = _bfd_elf_get_section_from_dynamic_symbol (abfd,
1400
0
                    isym);
1401
0
        if (sec == NULL)
1402
0
    goto error_return;
1403
0
        sym->symbol.section = sec;
1404
0
      }
1405
81.8k
    else
1406
81.8k
      {
1407
81.8k
        sym->symbol.section
1408
81.8k
    = bfd_section_from_elf_index (abfd, isym->st_shndx);
1409
81.8k
        if (sym->symbol.section == NULL)
1410
37.1k
    {
1411
      /* This symbol is in a section for which we did not
1412
         create a BFD section.  Just use bfd_abs_section,
1413
         although it is wrong.  FIXME.  Note - there is
1414
         code in elf.c:swap_out_syms that calls
1415
         symbol_section_index() in the elf backend for
1416
         cases like this.  */
1417
37.1k
      sym->symbol.section = bfd_abs_section_ptr;
1418
37.1k
    }
1419
81.8k
      }
1420
1421
    /* If this is a relocatable file, then the symbol value is
1422
       already section relative.  */
1423
166k
    if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1424
15.9k
      sym->symbol.value -= sym->symbol.section->vma;
1425
1426
166k
    switch (ELF_ST_BIND (isym->st_info))
1427
166k
      {
1428
105k
      case STB_LOCAL:
1429
105k
        sym->symbol.flags |= BSF_LOCAL;
1430
105k
        break;
1431
40.7k
      case STB_GLOBAL:
1432
40.7k
        if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON)
1433
11.1k
    sym->symbol.flags |= BSF_GLOBAL;
1434
40.7k
        break;
1435
10.6k
      case STB_WEAK:
1436
10.6k
        sym->symbol.flags |= BSF_WEAK;
1437
10.6k
        break;
1438
1.81k
      case STB_GNU_UNIQUE:
1439
1.81k
        sym->symbol.flags |= BSF_GNU_UNIQUE;
1440
1.81k
        break;
1441
166k
      }
1442
1443
166k
    switch (ELF_ST_TYPE (isym->st_info))
1444
166k
      {
1445
26.3k
      case STT_SECTION:
1446
        /* Mark the input section symbol as used since it may be
1447
           used for relocation and section group.
1448
     NB: BSF_SECTION_SYM_USED is ignored by linker and may
1449
     be cleared by objcopy for non-relocatable inputs.  */
1450
26.3k
        sym->symbol.flags |= (BSF_SECTION_SYM
1451
26.3k
            | BSF_DEBUGGING
1452
26.3k
            | BSF_SECTION_SYM_USED);
1453
26.3k
        break;
1454
4.20k
      case STT_FILE:
1455
4.20k
        sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
1456
4.20k
        break;
1457
14.2k
      case STT_FUNC:
1458
14.2k
        sym->symbol.flags |= BSF_FUNCTION;
1459
14.2k
        break;
1460
1.18k
      case STT_COMMON:
1461
        /* FIXME: Do we have to put the size field into the value field
1462
     as we do with symbols in SHN_COMMON sections (see above) ?  */
1463
1.18k
        sym->symbol.flags |= BSF_ELF_COMMON;
1464
        /* Fall through.  */
1465
9.71k
      case STT_OBJECT:
1466
9.71k
        sym->symbol.flags |= BSF_OBJECT;
1467
9.71k
        break;
1468
1.84k
      case STT_TLS:
1469
1.84k
        sym->symbol.flags |= BSF_THREAD_LOCAL;
1470
1.84k
        break;
1471
1.67k
      case STT_RELC:
1472
1.67k
        sym->symbol.flags |= BSF_RELC;
1473
1.67k
        break;
1474
1.05k
      case STT_SRELC:
1475
1.05k
        sym->symbol.flags |= BSF_SRELC;
1476
1.05k
        break;
1477
1.80k
      case STT_GNU_IFUNC:
1478
1.80k
        sym->symbol.flags |= BSF_GNU_INDIRECT_FUNCTION;
1479
1.80k
        break;
1480
166k
      }
1481
1482
166k
    if (dynamic)
1483
1.23k
      sym->symbol.flags |= BSF_DYNAMIC;
1484
1485
166k
    if (elf_tdata (abfd)->dt_versym)
1486
0
      sym->version = bfd_get_16 (abfd,
1487
166k
               elf_tdata (abfd)->dt_versym + 2 * i);
1488
166k
    else if (xver != NULL)
1489
700
      {
1490
700
        Elf_Internal_Versym iversym;
1491
1492
700
        _bfd_elf_swap_versym_in (abfd, xver, &iversym);
1493
700
        sym->version = iversym.vs_vers;
1494
700
        xver++;
1495
700
      }
1496
1497
    /* Do some backend-specific processing on this symbol.  */
1498
166k
    if (ebd->elf_backend_symbol_processing)
1499
67.6k
      (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
1500
166k
  }
1501
7.40k
    }
1502
1503
  /* Do some backend-specific processing on this symbol table.  */
1504
8.10k
  if (ebd->elf_backend_symbol_table_processing)
1505
0
    (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
1506
1507
  /* We rely on the zalloc to clear out the final symbol entry.  */
1508
1509
8.10k
  symcount = sym - symbase;
1510
1511
  /* Fill in the user's symbol pointer vector if needed.  */
1512
8.10k
  if (symptrs)
1513
8.10k
    {
1514
8.10k
      long l = symcount;
1515
1516
8.10k
      sym = symbase;
1517
174k
      while (l-- > 0)
1518
166k
  {
1519
166k
    *symptrs++ = &sym->symbol;
1520
166k
    sym++;
1521
166k
  }
1522
8.10k
      *symptrs = 0;   /* Final null pointer */
1523
8.10k
    }
1524
1525
8.10k
  free (xverbuf);
1526
8.10k
  if (hdr->contents != (unsigned char *) isymbuf
1527
7.40k
      && !elf_use_dt_symtab_p (abfd))
1528
7.40k
    free (isymbuf);
1529
8.10k
  return symcount;
1530
1531
5
 error_return:
1532
5
  free (xverbuf);
1533
5
  if (hdr->contents != (unsigned char *) isymbuf
1534
5
      && !elf_use_dt_symtab_p (abfd))
1535
5
    free (isymbuf);
1536
5
  return -1;
1537
9.07k
}
bfd_elf64_slurp_symbol_table
Line
Count
Source
1244
4.51k
{
1245
4.51k
  Elf_Internal_Shdr *hdr;
1246
4.51k
  Elf_Internal_Shdr *verhdr;
1247
4.51k
  unsigned long symcount; /* Number of external ELF symbols */
1248
4.51k
  elf_symbol_type *sym;   /* Pointer to current bfd symbol */
1249
4.51k
  elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
1250
4.51k
  Elf_Internal_Sym *isym;
1251
4.51k
  Elf_Internal_Sym *isymend;
1252
4.51k
  Elf_Internal_Sym *isymbuf = NULL;
1253
4.51k
  Elf_External_Versym *xver;
1254
4.51k
  Elf_External_Versym *xverbuf = NULL;
1255
4.51k
  elf_backend_data *ebd;
1256
4.51k
  size_t amt;
1257
1258
  /* Read each raw ELF symbol, converting from external ELF form to
1259
     internal ELF form, and then using the information to create a
1260
     canonical bfd symbol table entry.
1261
1262
     Note that we allocate the initial bfd canonical symbol buffer
1263
     based on a one-to-one mapping of the ELF symbols to canonical
1264
     symbols.  We actually use all the ELF symbols, so there will be no
1265
     space left over at the end.  When we have all the symbols, we
1266
     build the caller's pointer vector.  */
1267
4.51k
  ebd = get_elf_backend_data (abfd);
1268
1269
4.51k
  if (! dynamic)
1270
4.36k
    {
1271
4.36k
      hdr = &elf_symtab_hdr (abfd);
1272
4.36k
      verhdr = NULL;
1273
4.36k
      symcount = hdr->sh_size / ebd->s->sizeof_sym;
1274
4.36k
    }
1275
150
  else
1276
150
    {
1277
150
      hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1278
150
      if (elf_dynversym (abfd) == 0)
1279
21
  verhdr = NULL;
1280
129
      else
1281
129
  verhdr = &elf_tdata (abfd)->dynversym_hdr;
1282
150
      if ((elf_dynverdef (abfd) != 0
1283
50
     && elf_tdata (abfd)->verdef == NULL)
1284
112
    || (elf_dynverref (abfd) != 0
1285
77
        && elf_tdata (abfd)->verref == NULL)
1286
81
    || elf_tdata (abfd)->dt_verdef != NULL
1287
81
    || elf_tdata (abfd)->dt_verneed != NULL)
1288
71
  {
1289
71
    if (!_bfd_elf_slurp_version_tables (abfd, false))
1290
52
      return -1;
1291
71
  }
1292
1293
98
      symcount = elf_tdata (abfd)->dt_symtab_count;
1294
98
    }
1295
1296
4.46k
  if (symcount == 0)
1297
405
    symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1298
1299
4.46k
  if (symcount == 0)
1300
310
    sym = symbase = NULL;
1301
4.15k
  else
1302
4.15k
    {
1303
4.15k
      size_t i;
1304
1305
4.15k
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
1306
4.15k
              NULL, NULL, NULL);
1307
4.15k
      if (isymbuf == NULL)
1308
596
  return -1;
1309
1310
3.55k
      if (_bfd_mul_overflow (symcount, sizeof (elf_symbol_type), &amt))
1311
0
  {
1312
0
    bfd_set_error (bfd_error_file_too_big);
1313
0
    goto error_return;
1314
0
  }
1315
3.55k
      symbase = (elf_symbol_type *) bfd_zalloc (abfd, amt);
1316
3.55k
      if (symbase == (elf_symbol_type *) NULL)
1317
0
  goto error_return;
1318
1319
      /* Read the raw ELF version symbol information.  */
1320
3.55k
      if (verhdr != NULL
1321
76
    && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
1322
12
  {
1323
12
    _bfd_error_handler
1324
      /* xgettext:c-format */
1325
12
      (_("%pB: version count (%" PRId64 ")"
1326
12
         " does not match symbol count (%ld)"),
1327
12
       abfd,
1328
12
       (int64_t) (verhdr->sh_size / sizeof (Elf_External_Versym)),
1329
12
       symcount);
1330
1331
    /* Slurp in the symbols without the version information,
1332
       since that is more helpful than just quitting.  */
1333
12
    verhdr = NULL;
1334
12
  }
1335
1336
3.55k
      if (verhdr != NULL)
1337
64
  {
1338
64
    if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
1339
1
      goto error_return;
1340
63
    xverbuf = (Elf_External_Versym *)
1341
63
      _bfd_malloc_and_read (abfd, verhdr->sh_size, verhdr->sh_size);
1342
63
    if (xverbuf == NULL && verhdr->sh_size != 0)
1343
3
      goto error_return;
1344
63
  }
1345
1346
      /* Skip first symbol, which is a null dummy.  */
1347
3.55k
      xver = xverbuf;
1348
3.55k
      if (xver != NULL)
1349
60
  ++xver;
1350
3.55k
      isymend = isymbuf + symcount;
1351
3.55k
      for (isym = isymbuf + 1, sym = symbase, i = 1;
1352
113k
     isym < isymend;
1353
109k
     isym++, sym++, i++)
1354
109k
  {
1355
109k
    memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
1356
1357
109k
    sym->symbol.the_bfd = abfd;
1358
109k
    if (elf_use_dt_symtab_p (abfd))
1359
0
      sym->symbol.name = (elf_tdata (abfd)->dt_strtab
1360
0
        + isym->st_name);
1361
109k
    else
1362
109k
      sym->symbol.name = bfd_elf_sym_name (abfd, hdr, isym, NULL);
1363
109k
    sym->symbol.value = isym->st_value;
1364
1365
109k
    if (isym->st_shndx == SHN_UNDEF)
1366
55.7k
      {
1367
55.7k
        sym->symbol.section = bfd_und_section_ptr;
1368
55.7k
      }
1369
54.2k
    else if (isym->st_shndx == SHN_ABS)
1370
2.28k
      {
1371
2.28k
        sym->symbol.section = bfd_abs_section_ptr;
1372
2.28k
      }
1373
51.9k
    else if (isym->st_shndx == SHN_COMMON)
1374
63
      {
1375
63
        sym->symbol.section = bfd_com_section_ptr;
1376
63
        if ((abfd->flags & BFD_PLUGIN) != 0)
1377
0
    {
1378
0
      asection *xc = bfd_get_section_by_name (abfd, "COMMON");
1379
1380
0
      if (xc == NULL)
1381
0
        {
1382
0
          flagword flags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
1383
0
          | SEC_EXCLUDE);
1384
0
          xc = bfd_make_section_with_flags (abfd, "COMMON", flags);
1385
0
          if (xc == NULL)
1386
0
      goto error_return;
1387
0
        }
1388
0
      sym->symbol.section = xc;
1389
0
    }
1390
        /* Elf puts the alignment into the `value' field, and
1391
     the size into the `size' field.  BFD wants to see the
1392
     size in the value field, and doesn't care (at the
1393
     moment) about the alignment.  */
1394
63
        sym->symbol.value = isym->st_size;
1395
63
      }
1396
51.8k
    else if (elf_use_dt_symtab_p (abfd))
1397
0
      {
1398
0
        asection *sec;
1399
0
        sec = _bfd_elf_get_section_from_dynamic_symbol (abfd,
1400
0
                    isym);
1401
0
        if (sec == NULL)
1402
0
    goto error_return;
1403
0
        sym->symbol.section = sec;
1404
0
      }
1405
51.8k
    else
1406
51.8k
      {
1407
51.8k
        sym->symbol.section
1408
51.8k
    = bfd_section_from_elf_index (abfd, isym->st_shndx);
1409
51.8k
        if (sym->symbol.section == NULL)
1410
14.9k
    {
1411
      /* This symbol is in a section for which we did not
1412
         create a BFD section.  Just use bfd_abs_section,
1413
         although it is wrong.  FIXME.  Note - there is
1414
         code in elf.c:swap_out_syms that calls
1415
         symbol_section_index() in the elf backend for
1416
         cases like this.  */
1417
14.9k
      sym->symbol.section = bfd_abs_section_ptr;
1418
14.9k
    }
1419
51.8k
      }
1420
1421
    /* If this is a relocatable file, then the symbol value is
1422
       already section relative.  */
1423
109k
    if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1424
14.6k
      sym->symbol.value -= sym->symbol.section->vma;
1425
1426
109k
    switch (ELF_ST_BIND (isym->st_info))
1427
109k
      {
1428
67.0k
      case STB_LOCAL:
1429
67.0k
        sym->symbol.flags |= BSF_LOCAL;
1430
67.0k
        break;
1431
34.3k
      case STB_GLOBAL:
1432
34.3k
        if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON)
1433
6.13k
    sym->symbol.flags |= BSF_GLOBAL;
1434
34.3k
        break;
1435
5.72k
      case STB_WEAK:
1436
5.72k
        sym->symbol.flags |= BSF_WEAK;
1437
5.72k
        break;
1438
554
      case STB_GNU_UNIQUE:
1439
554
        sym->symbol.flags |= BSF_GNU_UNIQUE;
1440
554
        break;
1441
109k
      }
1442
1443
109k
    switch (ELF_ST_TYPE (isym->st_info))
1444
109k
      {
1445
22.7k
      case STT_SECTION:
1446
        /* Mark the input section symbol as used since it may be
1447
           used for relocation and section group.
1448
     NB: BSF_SECTION_SYM_USED is ignored by linker and may
1449
     be cleared by objcopy for non-relocatable inputs.  */
1450
22.7k
        sym->symbol.flags |= (BSF_SECTION_SYM
1451
22.7k
            | BSF_DEBUGGING
1452
22.7k
            | BSF_SECTION_SYM_USED);
1453
22.7k
        break;
1454
3.42k
      case STT_FILE:
1455
3.42k
        sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
1456
3.42k
        break;
1457
9.55k
      case STT_FUNC:
1458
9.55k
        sym->symbol.flags |= BSF_FUNCTION;
1459
9.55k
        break;
1460
544
      case STT_COMMON:
1461
        /* FIXME: Do we have to put the size field into the value field
1462
     as we do with symbols in SHN_COMMON sections (see above) ?  */
1463
544
        sym->symbol.flags |= BSF_ELF_COMMON;
1464
        /* Fall through.  */
1465
7.60k
      case STT_OBJECT:
1466
7.60k
        sym->symbol.flags |= BSF_OBJECT;
1467
7.60k
        break;
1468
1.33k
      case STT_TLS:
1469
1.33k
        sym->symbol.flags |= BSF_THREAD_LOCAL;
1470
1.33k
        break;
1471
401
      case STT_RELC:
1472
401
        sym->symbol.flags |= BSF_RELC;
1473
401
        break;
1474
604
      case STT_SRELC:
1475
604
        sym->symbol.flags |= BSF_SRELC;
1476
604
        break;
1477
831
      case STT_GNU_IFUNC:
1478
831
        sym->symbol.flags |= BSF_GNU_INDIRECT_FUNCTION;
1479
831
        break;
1480
109k
      }
1481
1482
109k
    if (dynamic)
1483
777
      sym->symbol.flags |= BSF_DYNAMIC;
1484
1485
109k
    if (elf_tdata (abfd)->dt_versym)
1486
0
      sym->version = bfd_get_16 (abfd,
1487
109k
               elf_tdata (abfd)->dt_versym + 2 * i);
1488
109k
    else if (xver != NULL)
1489
611
      {
1490
611
        Elf_Internal_Versym iversym;
1491
1492
611
        _bfd_elf_swap_versym_in (abfd, xver, &iversym);
1493
611
        sym->version = iversym.vs_vers;
1494
611
        xver++;
1495
611
      }
1496
1497
    /* Do some backend-specific processing on this symbol.  */
1498
109k
    if (ebd->elf_backend_symbol_processing)
1499
51.0k
      (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
1500
109k
  }
1501
3.55k
    }
1502
1503
  /* Do some backend-specific processing on this symbol table.  */
1504
3.86k
  if (ebd->elf_backend_symbol_table_processing)
1505
0
    (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
1506
1507
  /* We rely on the zalloc to clear out the final symbol entry.  */
1508
1509
3.86k
  symcount = sym - symbase;
1510
1511
  /* Fill in the user's symbol pointer vector if needed.  */
1512
3.86k
  if (symptrs)
1513
3.86k
    {
1514
3.86k
      long l = symcount;
1515
1516
3.86k
      sym = symbase;
1517
113k
      while (l-- > 0)
1518
109k
  {
1519
109k
    *symptrs++ = &sym->symbol;
1520
109k
    sym++;
1521
109k
  }
1522
3.86k
      *symptrs = 0;   /* Final null pointer */
1523
3.86k
    }
1524
1525
3.86k
  free (xverbuf);
1526
3.86k
  if (hdr->contents != (unsigned char *) isymbuf
1527
3.55k
      && !elf_use_dt_symtab_p (abfd))
1528
3.54k
    free (isymbuf);
1529
3.86k
  return symcount;
1530
1531
4
 error_return:
1532
4
  free (xverbuf);
1533
4
  if (hdr->contents != (unsigned char *) isymbuf
1534
4
      && !elf_use_dt_symtab_p (abfd))
1535
4
    free (isymbuf);
1536
4
  return -1;
1537
4.46k
}
bfd_elf32_slurp_symbol_table
Line
Count
Source
1244
4.64k
{
1245
4.64k
  Elf_Internal_Shdr *hdr;
1246
4.64k
  Elf_Internal_Shdr *verhdr;
1247
4.64k
  unsigned long symcount; /* Number of external ELF symbols */
1248
4.64k
  elf_symbol_type *sym;   /* Pointer to current bfd symbol */
1249
4.64k
  elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
1250
4.64k
  Elf_Internal_Sym *isym;
1251
4.64k
  Elf_Internal_Sym *isymend;
1252
4.64k
  Elf_Internal_Sym *isymbuf = NULL;
1253
4.64k
  Elf_External_Versym *xver;
1254
4.64k
  Elf_External_Versym *xverbuf = NULL;
1255
4.64k
  elf_backend_data *ebd;
1256
4.64k
  size_t amt;
1257
1258
  /* Read each raw ELF symbol, converting from external ELF form to
1259
     internal ELF form, and then using the information to create a
1260
     canonical bfd symbol table entry.
1261
1262
     Note that we allocate the initial bfd canonical symbol buffer
1263
     based on a one-to-one mapping of the ELF symbols to canonical
1264
     symbols.  We actually use all the ELF symbols, so there will be no
1265
     space left over at the end.  When we have all the symbols, we
1266
     build the caller's pointer vector.  */
1267
4.64k
  ebd = get_elf_backend_data (abfd);
1268
1269
4.64k
  if (! dynamic)
1270
4.55k
    {
1271
4.55k
      hdr = &elf_symtab_hdr (abfd);
1272
4.55k
      verhdr = NULL;
1273
4.55k
      symcount = hdr->sh_size / ebd->s->sizeof_sym;
1274
4.55k
    }
1275
91
  else
1276
91
    {
1277
91
      hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1278
91
      if (elf_dynversym (abfd) == 0)
1279
17
  verhdr = NULL;
1280
74
      else
1281
74
  verhdr = &elf_tdata (abfd)->dynversym_hdr;
1282
91
      if ((elf_dynverdef (abfd) != 0
1283
42
     && elf_tdata (abfd)->verdef == NULL)
1284
51
    || (elf_dynverref (abfd) != 0
1285
20
        && elf_tdata (abfd)->verref == NULL)
1286
34
    || elf_tdata (abfd)->dt_verdef != NULL
1287
34
    || elf_tdata (abfd)->dt_verneed != NULL)
1288
57
  {
1289
57
    if (!_bfd_elf_slurp_version_tables (abfd, false))
1290
36
      return -1;
1291
57
  }
1292
1293
55
      symcount = elf_tdata (abfd)->dt_symtab_count;
1294
55
    }
1295
1296
4.61k
  if (symcount == 0)
1297
443
    symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1298
1299
4.61k
  if (symcount == 0)
1300
388
    sym = symbase = NULL;
1301
4.22k
  else
1302
4.22k
    {
1303
4.22k
      size_t i;
1304
1305
4.22k
      isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
1306
4.22k
              NULL, NULL, NULL);
1307
4.22k
      if (isymbuf == NULL)
1308
365
  return -1;
1309
1310
3.86k
      if (_bfd_mul_overflow (symcount, sizeof (elf_symbol_type), &amt))
1311
0
  {
1312
0
    bfd_set_error (bfd_error_file_too_big);
1313
0
    goto error_return;
1314
0
  }
1315
3.86k
      symbase = (elf_symbol_type *) bfd_zalloc (abfd, amt);
1316
3.86k
      if (symbase == (elf_symbol_type *) NULL)
1317
0
  goto error_return;
1318
1319
      /* Read the raw ELF version symbol information.  */
1320
3.86k
      if (verhdr != NULL
1321
34
    && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
1322
22
  {
1323
22
    _bfd_error_handler
1324
      /* xgettext:c-format */
1325
22
      (_("%pB: version count (%" PRId64 ")"
1326
22
         " does not match symbol count (%ld)"),
1327
22
       abfd,
1328
22
       (int64_t) (verhdr->sh_size / sizeof (Elf_External_Versym)),
1329
22
       symcount);
1330
1331
    /* Slurp in the symbols without the version information,
1332
       since that is more helpful than just quitting.  */
1333
22
    verhdr = NULL;
1334
22
  }
1335
1336
3.86k
      if (verhdr != NULL)
1337
12
  {
1338
12
    if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
1339
0
      goto error_return;
1340
12
    xverbuf = (Elf_External_Versym *)
1341
12
      _bfd_malloc_and_read (abfd, verhdr->sh_size, verhdr->sh_size);
1342
12
    if (xverbuf == NULL && verhdr->sh_size != 0)
1343
1
      goto error_return;
1344
12
  }
1345
1346
      /* Skip first symbol, which is a null dummy.  */
1347
3.85k
      xver = xverbuf;
1348
3.85k
      if (xver != NULL)
1349
11
  ++xver;
1350
3.85k
      isymend = isymbuf + symcount;
1351
3.85k
      for (isym = isymbuf + 1, sym = symbase, i = 1;
1352
59.9k
     isym < isymend;
1353
56.1k
     isym++, sym++, i++)
1354
56.1k
  {
1355
56.1k
    memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
1356
1357
56.1k
    sym->symbol.the_bfd = abfd;
1358
56.1k
    if (elf_use_dt_symtab_p (abfd))
1359
0
      sym->symbol.name = (elf_tdata (abfd)->dt_strtab
1360
0
        + isym->st_name);
1361
56.1k
    else
1362
56.1k
      sym->symbol.name = bfd_elf_sym_name (abfd, hdr, isym, NULL);
1363
56.1k
    sym->symbol.value = isym->st_value;
1364
1365
56.1k
    if (isym->st_shndx == SHN_UNDEF)
1366
26.0k
      {
1367
26.0k
        sym->symbol.section = bfd_und_section_ptr;
1368
26.0k
      }
1369
30.0k
    else if (isym->st_shndx == SHN_ABS)
1370
87
      {
1371
87
        sym->symbol.section = bfd_abs_section_ptr;
1372
87
      }
1373
29.9k
    else if (isym->st_shndx == SHN_COMMON)
1374
35
      {
1375
35
        sym->symbol.section = bfd_com_section_ptr;
1376
35
        if ((abfd->flags & BFD_PLUGIN) != 0)
1377
0
    {
1378
0
      asection *xc = bfd_get_section_by_name (abfd, "COMMON");
1379
1380
0
      if (xc == NULL)
1381
0
        {
1382
0
          flagword flags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
1383
0
          | SEC_EXCLUDE);
1384
0
          xc = bfd_make_section_with_flags (abfd, "COMMON", flags);
1385
0
          if (xc == NULL)
1386
0
      goto error_return;
1387
0
        }
1388
0
      sym->symbol.section = xc;
1389
0
    }
1390
        /* Elf puts the alignment into the `value' field, and
1391
     the size into the `size' field.  BFD wants to see the
1392
     size in the value field, and doesn't care (at the
1393
     moment) about the alignment.  */
1394
35
        sym->symbol.value = isym->st_size;
1395
35
      }
1396
29.9k
    else if (elf_use_dt_symtab_p (abfd))
1397
0
      {
1398
0
        asection *sec;
1399
0
        sec = _bfd_elf_get_section_from_dynamic_symbol (abfd,
1400
0
                    isym);
1401
0
        if (sec == NULL)
1402
0
    goto error_return;
1403
0
        sym->symbol.section = sec;
1404
0
      }
1405
29.9k
    else
1406
29.9k
      {
1407
29.9k
        sym->symbol.section
1408
29.9k
    = bfd_section_from_elf_index (abfd, isym->st_shndx);
1409
29.9k
        if (sym->symbol.section == NULL)
1410
22.2k
    {
1411
      /* This symbol is in a section for which we did not
1412
         create a BFD section.  Just use bfd_abs_section,
1413
         although it is wrong.  FIXME.  Note - there is
1414
         code in elf.c:swap_out_syms that calls
1415
         symbol_section_index() in the elf backend for
1416
         cases like this.  */
1417
22.2k
      sym->symbol.section = bfd_abs_section_ptr;
1418
22.2k
    }
1419
29.9k
      }
1420
1421
    /* If this is a relocatable file, then the symbol value is
1422
       already section relative.  */
1423
56.1k
    if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1424
1.37k
      sym->symbol.value -= sym->symbol.section->vma;
1425
1426
56.1k
    switch (ELF_ST_BIND (isym->st_info))
1427
56.1k
      {
1428
38.6k
      case STB_LOCAL:
1429
38.6k
        sym->symbol.flags |= BSF_LOCAL;
1430
38.6k
        break;
1431
6.47k
      case STB_GLOBAL:
1432
6.47k
        if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON)
1433
5.03k
    sym->symbol.flags |= BSF_GLOBAL;
1434
6.47k
        break;
1435
4.90k
      case STB_WEAK:
1436
4.90k
        sym->symbol.flags |= BSF_WEAK;
1437
4.90k
        break;
1438
1.25k
      case STB_GNU_UNIQUE:
1439
1.25k
        sym->symbol.flags |= BSF_GNU_UNIQUE;
1440
1.25k
        break;
1441
56.1k
      }
1442
1443
56.1k
    switch (ELF_ST_TYPE (isym->st_info))
1444
56.1k
      {
1445
3.64k
      case STT_SECTION:
1446
        /* Mark the input section symbol as used since it may be
1447
           used for relocation and section group.
1448
     NB: BSF_SECTION_SYM_USED is ignored by linker and may
1449
     be cleared by objcopy for non-relocatable inputs.  */
1450
3.64k
        sym->symbol.flags |= (BSF_SECTION_SYM
1451
3.64k
            | BSF_DEBUGGING
1452
3.64k
            | BSF_SECTION_SYM_USED);
1453
3.64k
        break;
1454
783
      case STT_FILE:
1455
783
        sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
1456
783
        break;
1457
4.67k
      case STT_FUNC:
1458
4.67k
        sym->symbol.flags |= BSF_FUNCTION;
1459
4.67k
        break;
1460
645
      case STT_COMMON:
1461
        /* FIXME: Do we have to put the size field into the value field
1462
     as we do with symbols in SHN_COMMON sections (see above) ?  */
1463
645
        sym->symbol.flags |= BSF_ELF_COMMON;
1464
        /* Fall through.  */
1465
2.10k
      case STT_OBJECT:
1466
2.10k
        sym->symbol.flags |= BSF_OBJECT;
1467
2.10k
        break;
1468
514
      case STT_TLS:
1469
514
        sym->symbol.flags |= BSF_THREAD_LOCAL;
1470
514
        break;
1471
1.26k
      case STT_RELC:
1472
1.26k
        sym->symbol.flags |= BSF_RELC;
1473
1.26k
        break;
1474
446
      case STT_SRELC:
1475
446
        sym->symbol.flags |= BSF_SRELC;
1476
446
        break;
1477
972
      case STT_GNU_IFUNC:
1478
972
        sym->symbol.flags |= BSF_GNU_INDIRECT_FUNCTION;
1479
972
        break;
1480
56.1k
      }
1481
1482
56.1k
    if (dynamic)
1483
454
      sym->symbol.flags |= BSF_DYNAMIC;
1484
1485
56.1k
    if (elf_tdata (abfd)->dt_versym)
1486
0
      sym->version = bfd_get_16 (abfd,
1487
56.1k
               elf_tdata (abfd)->dt_versym + 2 * i);
1488
56.1k
    else if (xver != NULL)
1489
89
      {
1490
89
        Elf_Internal_Versym iversym;
1491
1492
89
        _bfd_elf_swap_versym_in (abfd, xver, &iversym);
1493
89
        sym->version = iversym.vs_vers;
1494
89
        xver++;
1495
89
      }
1496
1497
    /* Do some backend-specific processing on this symbol.  */
1498
56.1k
    if (ebd->elf_backend_symbol_processing)
1499
16.5k
      (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
1500
56.1k
  }
1501
3.85k
    }
1502
1503
  /* Do some backend-specific processing on this symbol table.  */
1504
4.24k
  if (ebd->elf_backend_symbol_table_processing)
1505
0
    (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
1506
1507
  /* We rely on the zalloc to clear out the final symbol entry.  */
1508
1509
4.24k
  symcount = sym - symbase;
1510
1511
  /* Fill in the user's symbol pointer vector if needed.  */
1512
4.24k
  if (symptrs)
1513
4.24k
    {
1514
4.24k
      long l = symcount;
1515
1516
4.24k
      sym = symbase;
1517
60.3k
      while (l-- > 0)
1518
56.1k
  {
1519
56.1k
    *symptrs++ = &sym->symbol;
1520
56.1k
    sym++;
1521
56.1k
  }
1522
4.24k
      *symptrs = 0;   /* Final null pointer */
1523
4.24k
    }
1524
1525
4.24k
  free (xverbuf);
1526
4.24k
  if (hdr->contents != (unsigned char *) isymbuf
1527
3.85k
      && !elf_use_dt_symtab_p (abfd))
1528
3.85k
    free (isymbuf);
1529
4.24k
  return symcount;
1530
1531
1
 error_return:
1532
1
  free (xverbuf);
1533
1
  if (hdr->contents != (unsigned char *) isymbuf
1534
1
      && !elf_use_dt_symtab_p (abfd))
1535
1
    free (isymbuf);
1536
1
  return -1;
1537
4.61k
}
1538
1539
/* Read relocations for ASECT from REL_HDR.  There are RELOC_COUNT of
1540
   them.  */
1541
1542
static bool
1543
elf_slurp_reloc_table_from_section (bfd *abfd,
1544
            asection *asect,
1545
            Elf_Internal_Shdr *rel_hdr,
1546
            bfd_size_type reloc_count,
1547
            arelent *relents,
1548
            asymbol **symbols,
1549
            bool dynamic)
1550
13.4k
{
1551
13.4k
  elf_backend_data *ebd = get_elf_backend_data (abfd);
1552
13.4k
  void *allocated = NULL;
1553
13.4k
  bfd_byte *native_relocs;
1554
13.4k
  arelent *relent;
1555
13.4k
  unsigned int i;
1556
13.4k
  int entsize;
1557
13.4k
  unsigned int symcount;
1558
1559
13.4k
  if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
1560
216
    return false;
1561
13.2k
  allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
1562
13.2k
  if (allocated == NULL)
1563
519
    return false;
1564
1565
12.7k
  native_relocs = (bfd_byte *) allocated;
1566
1567
12.7k
  entsize = rel_hdr->sh_entsize;
1568
12.7k
  BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
1569
12.7k
        || entsize == sizeof (Elf_External_Rela));
1570
1571
12.7k
  if (dynamic)
1572
95
    symcount = bfd_get_dynamic_symcount (abfd);
1573
12.6k
  else
1574
12.6k
    symcount = bfd_get_symcount (abfd);
1575
1576
12.7k
  for (i = 0, relent = relents;
1577
162k
       i < reloc_count;
1578
150k
       i++, relent++, native_relocs += entsize)
1579
153k
    {
1580
153k
      bool res;
1581
153k
      Elf_Internal_Rela rela;
1582
1583
153k
      if (entsize == sizeof (Elf_External_Rela))
1584
116k
  elf_swap_reloca_in (abfd, native_relocs, &rela);
1585
36.2k
      else
1586
36.2k
  elf_swap_reloc_in (abfd, native_relocs, &rela);
1587
1588
      /* The address of an ELF reloc is section relative for an object
1589
   file, and absolute for an executable file or shared library.
1590
   The address of a normal BFD reloc is always section relative,
1591
   and the address of a dynamic reloc is absolute..  */
1592
153k
      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
1593
149k
  relent->address = rela.r_offset;
1594
3.56k
      else
1595
3.56k
  relent->address = rela.r_offset - asect->vma;
1596
1597
153k
      if (ELF_R_SYM (rela.r_info) == STN_UNDEF)
1598
  /* FIXME: This and the error case below mean that we have a
1599
     symbol on relocs that is not elf_symbol_type.  */
1600
39.0k
  relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
1601
114k
      else if (ELF_R_SYM (rela.r_info) > symcount)
1602
41.1k
  {
1603
41.1k
    _bfd_error_handler
1604
      /* xgettext:c-format */
1605
41.1k
      (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
1606
41.1k
       abfd, asect, i, (long) ELF_R_SYM (rela.r_info));
1607
41.1k
    bfd_set_error (bfd_error_bad_value);
1608
41.1k
    relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
1609
41.1k
  }
1610
73.0k
      else
1611
73.0k
  {
1612
73.0k
    asymbol **ps;
1613
1614
73.0k
    ps = symbols + ELF_R_SYM (rela.r_info) - 1;
1615
1616
73.0k
    relent->sym_ptr_ptr = ps;
1617
73.0k
  }
1618
1619
153k
      relent->addend = rela.r_addend;
1620
1621
153k
      res = false;
1622
153k
      if ((entsize == sizeof (Elf_External_Rela)
1623
36.2k
     || ebd->elf_info_to_howto_rel == NULL)
1624
143k
    && ebd->elf_info_to_howto != NULL)
1625
141k
  res = ebd->elf_info_to_howto (abfd, relent, &rela);
1626
11.5k
      else if (ebd->elf_info_to_howto_rel != NULL)
1627
11.5k
  res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
1628
1629
153k
      if (! res || relent->howto == NULL)
1630
3.13k
  goto error_return;
1631
153k
    }
1632
1633
9.60k
  free (allocated);
1634
9.60k
  return true;
1635
1636
3.13k
 error_return:
1637
3.13k
  free (allocated);
1638
3.13k
  return false;
1639
12.7k
}
elf64.c:elf_slurp_reloc_table_from_section
Line
Count
Source
1550
10.5k
{
1551
10.5k
  elf_backend_data *ebd = get_elf_backend_data (abfd);
1552
10.5k
  void *allocated = NULL;
1553
10.5k
  bfd_byte *native_relocs;
1554
10.5k
  arelent *relent;
1555
10.5k
  unsigned int i;
1556
10.5k
  int entsize;
1557
10.5k
  unsigned int symcount;
1558
1559
10.5k
  if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
1560
216
    return false;
1561
10.3k
  allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
1562
10.3k
  if (allocated == NULL)
1563
374
    return false;
1564
1565
9.99k
  native_relocs = (bfd_byte *) allocated;
1566
1567
9.99k
  entsize = rel_hdr->sh_entsize;
1568
9.99k
  BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
1569
9.99k
        || entsize == sizeof (Elf_External_Rela));
1570
1571
9.99k
  if (dynamic)
1572
86
    symcount = bfd_get_dynamic_symcount (abfd);
1573
9.90k
  else
1574
9.90k
    symcount = bfd_get_symcount (abfd);
1575
1576
9.99k
  for (i = 0, relent = relents;
1577
106k
       i < reloc_count;
1578
96.7k
       i++, relent++, native_relocs += entsize)
1579
98.6k
    {
1580
98.6k
      bool res;
1581
98.6k
      Elf_Internal_Rela rela;
1582
1583
98.6k
      if (entsize == sizeof (Elf_External_Rela))
1584
98.6k
  elf_swap_reloca_in (abfd, native_relocs, &rela);
1585
0
      else
1586
0
  elf_swap_reloc_in (abfd, native_relocs, &rela);
1587
1588
      /* The address of an ELF reloc is section relative for an object
1589
   file, and absolute for an executable file or shared library.
1590
   The address of a normal BFD reloc is always section relative,
1591
   and the address of a dynamic reloc is absolute..  */
1592
98.6k
      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
1593
95.7k
  relent->address = rela.r_offset;
1594
2.91k
      else
1595
2.91k
  relent->address = rela.r_offset - asect->vma;
1596
1597
98.6k
      if (ELF_R_SYM (rela.r_info) == STN_UNDEF)
1598
  /* FIXME: This and the error case below mean that we have a
1599
     symbol on relocs that is not elf_symbol_type.  */
1600
18.2k
  relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
1601
80.4k
      else if (ELF_R_SYM (rela.r_info) > symcount)
1602
22.8k
  {
1603
22.8k
    _bfd_error_handler
1604
      /* xgettext:c-format */
1605
22.8k
      (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
1606
22.8k
       abfd, asect, i, (long) ELF_R_SYM (rela.r_info));
1607
22.8k
    bfd_set_error (bfd_error_bad_value);
1608
22.8k
    relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
1609
22.8k
  }
1610
57.5k
      else
1611
57.5k
  {
1612
57.5k
    asymbol **ps;
1613
1614
57.5k
    ps = symbols + ELF_R_SYM (rela.r_info) - 1;
1615
1616
57.5k
    relent->sym_ptr_ptr = ps;
1617
57.5k
  }
1618
1619
98.6k
      relent->addend = rela.r_addend;
1620
1621
98.6k
      res = false;
1622
98.6k
      if ((entsize == sizeof (Elf_External_Rela)
1623
0
     || ebd->elf_info_to_howto_rel == NULL)
1624
98.6k
    && ebd->elf_info_to_howto != NULL)
1625
98.6k
  res = ebd->elf_info_to_howto (abfd, relent, &rela);
1626
15
      else if (ebd->elf_info_to_howto_rel != NULL)
1627
0
  res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
1628
1629
98.6k
      if (! res || relent->howto == NULL)
1630
1.93k
  goto error_return;
1631
98.6k
    }
1632
1633
8.06k
  free (allocated);
1634
8.06k
  return true;
1635
1636
1.93k
 error_return:
1637
1.93k
  free (allocated);
1638
  return false;
1639
9.99k
}
elf32.c:elf_slurp_reloc_table_from_section
Line
Count
Source
1550
2.89k
{
1551
2.89k
  elf_backend_data *ebd = get_elf_backend_data (abfd);
1552
2.89k
  void *allocated = NULL;
1553
2.89k
  bfd_byte *native_relocs;
1554
2.89k
  arelent *relent;
1555
2.89k
  unsigned int i;
1556
2.89k
  int entsize;
1557
2.89k
  unsigned int symcount;
1558
1559
2.89k
  if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
1560
0
    return false;
1561
2.89k
  allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
1562
2.89k
  if (allocated == NULL)
1563
145
    return false;
1564
1565
2.75k
  native_relocs = (bfd_byte *) allocated;
1566
1567
2.75k
  entsize = rel_hdr->sh_entsize;
1568
2.75k
  BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
1569
2.75k
        || entsize == sizeof (Elf_External_Rela));
1570
1571
2.75k
  if (dynamic)
1572
9
    symcount = bfd_get_dynamic_symcount (abfd);
1573
2.74k
  else
1574
2.74k
    symcount = bfd_get_symcount (abfd);
1575
1576
2.75k
  for (i = 0, relent = relents;
1577
56.0k
       i < reloc_count;
1578
53.3k
       i++, relent++, native_relocs += entsize)
1579
54.5k
    {
1580
54.5k
      bool res;
1581
54.5k
      Elf_Internal_Rela rela;
1582
1583
54.5k
      if (entsize == sizeof (Elf_External_Rela))
1584
18.2k
  elf_swap_reloca_in (abfd, native_relocs, &rela);
1585
36.2k
      else
1586
36.2k
  elf_swap_reloc_in (abfd, native_relocs, &rela);
1587
1588
      /* The address of an ELF reloc is section relative for an object
1589
   file, and absolute for an executable file or shared library.
1590
   The address of a normal BFD reloc is always section relative,
1591
   and the address of a dynamic reloc is absolute..  */
1592
54.5k
      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
1593
53.8k
  relent->address = rela.r_offset;
1594
650
      else
1595
650
  relent->address = rela.r_offset - asect->vma;
1596
1597
54.5k
      if (ELF_R_SYM (rela.r_info) == STN_UNDEF)
1598
  /* FIXME: This and the error case below mean that we have a
1599
     symbol on relocs that is not elf_symbol_type.  */
1600
20.8k
  relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
1601
33.6k
      else if (ELF_R_SYM (rela.r_info) > symcount)
1602
18.2k
  {
1603
18.2k
    _bfd_error_handler
1604
      /* xgettext:c-format */
1605
18.2k
      (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
1606
18.2k
       abfd, asect, i, (long) ELF_R_SYM (rela.r_info));
1607
18.2k
    bfd_set_error (bfd_error_bad_value);
1608
18.2k
    relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
1609
18.2k
  }
1610
15.4k
      else
1611
15.4k
  {
1612
15.4k
    asymbol **ps;
1613
1614
15.4k
    ps = symbols + ELF_R_SYM (rela.r_info) - 1;
1615
1616
15.4k
    relent->sym_ptr_ptr = ps;
1617
15.4k
  }
1618
1619
54.5k
      relent->addend = rela.r_addend;
1620
1621
54.5k
      res = false;
1622
54.5k
      if ((entsize == sizeof (Elf_External_Rela)
1623
36.2k
     || ebd->elf_info_to_howto_rel == NULL)
1624
44.5k
    && ebd->elf_info_to_howto != NULL)
1625
42.9k
  res = ebd->elf_info_to_howto (abfd, relent, &rela);
1626
11.5k
      else if (ebd->elf_info_to_howto_rel != NULL)
1627
11.5k
  res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
1628
1629
54.5k
      if (! res || relent->howto == NULL)
1630
1.20k
  goto error_return;
1631
54.5k
    }
1632
1633
1.54k
  free (allocated);
1634
1.54k
  return true;
1635
1636
1.20k
 error_return:
1637
1.20k
  free (allocated);
1638
  return false;
1639
2.75k
}
1640
1641
/* Read in and swap the external relocs.  */
1642
1643
bool
1644
elf_slurp_reloc_table (bfd *abfd,
1645
           asection *asect,
1646
           asymbol **symbols,
1647
           bool dynamic)
1648
18.0k
{
1649
18.0k
  elf_backend_data *bed = get_elf_backend_data (abfd);
1650
18.0k
  struct bfd_elf_section_data * const d = elf_section_data (asect);
1651
18.0k
  Elf_Internal_Shdr *rel_hdr;
1652
18.0k
  Elf_Internal_Shdr *rel_hdr2;
1653
18.0k
  bfd_size_type reloc_count;
1654
18.0k
  bfd_size_type reloc_count2;
1655
18.0k
  arelent *relents;
1656
18.0k
  size_t amt;
1657
1658
18.0k
  if (asect->relocation != NULL)
1659
2.24k
    return true;
1660
1661
15.8k
  if (! dynamic)
1662
15.7k
    {
1663
15.7k
      if ((asect->flags & SEC_RELOC) == 0
1664
13.5k
    || asect->reloc_count == 0)
1665
2.32k
  return true;
1666
1667
13.3k
      rel_hdr = d->rel.hdr;
1668
13.3k
      reloc_count = rel_hdr ? NUM_SHDR_ENTRIES (rel_hdr) : 0;
1669
13.3k
      rel_hdr2 = d->rela.hdr;
1670
13.3k
      reloc_count2 = rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0;
1671
1672
      /* PR 17512: file: 0b4f81b7.  */
1673
13.3k
      if (asect->reloc_count != reloc_count + reloc_count2)
1674
0
  return false;
1675
13.3k
      BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
1676
13.3k
      || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
1677
1678
13.3k
    }
1679
99
  else
1680
99
    {
1681
      /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
1682
   case because relocations against this section may use the
1683
   dynamic symbol table, and in that case bfd_section_from_shdr
1684
   in elf.c does not update the RELOC_COUNT.  */
1685
99
      if (asect->size == 0)
1686
4
  return true;
1687
1688
95
      rel_hdr = &d->this_hdr;
1689
95
      reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
1690
95
      rel_hdr2 = NULL;
1691
95
      reloc_count2 = 0;
1692
95
    }
1693
1694
13.4k
  if (_bfd_mul_overflow (reloc_count + reloc_count2, sizeof (arelent), &amt))
1695
0
    {
1696
0
      bfd_set_error (bfd_error_file_too_big);
1697
0
      return false;
1698
0
    }
1699
13.4k
  relents = (arelent *) bfd_alloc (abfd, amt);
1700
13.4k
  if (relents == NULL)
1701
0
    return false;
1702
1703
13.4k
  if (rel_hdr
1704
2.11k
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1705
2.11k
                rel_hdr, reloc_count,
1706
2.11k
                relents,
1707
2.11k
                symbols, dynamic))
1708
709
    return false;
1709
1710
12.7k
  if (rel_hdr2
1711
11.3k
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1712
11.3k
                rel_hdr2, reloc_count2,
1713
11.3k
                relents + reloc_count,
1714
11.3k
                symbols, dynamic))
1715
3.16k
    return false;
1716
1717
9.60k
  if (!bed->slurp_secondary_relocs (abfd, asect, symbols, dynamic))
1718
32
    return false;
1719
1720
9.57k
  asect->relocation = relents;
1721
9.57k
  return true;
1722
9.60k
}
bfd_elf64_slurp_reloc_table
Line
Count
Source
1648
13.6k
{
1649
13.6k
  elf_backend_data *bed = get_elf_backend_data (abfd);
1650
13.6k
  struct bfd_elf_section_data * const d = elf_section_data (asect);
1651
13.6k
  Elf_Internal_Shdr *rel_hdr;
1652
13.6k
  Elf_Internal_Shdr *rel_hdr2;
1653
13.6k
  bfd_size_type reloc_count;
1654
13.6k
  bfd_size_type reloc_count2;
1655
13.6k
  arelent *relents;
1656
13.6k
  size_t amt;
1657
1658
13.6k
  if (asect->relocation != NULL)
1659
2.07k
    return true;
1660
1661
11.5k
  if (! dynamic)
1662
11.4k
    {
1663
11.4k
      if ((asect->flags & SEC_RELOC) == 0
1664
10.5k
    || asect->reloc_count == 0)
1665
996
  return true;
1666
1667
10.4k
      rel_hdr = d->rel.hdr;
1668
10.4k
      reloc_count = rel_hdr ? NUM_SHDR_ENTRIES (rel_hdr) : 0;
1669
10.4k
      rel_hdr2 = d->rela.hdr;
1670
10.4k
      reloc_count2 = rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0;
1671
1672
      /* PR 17512: file: 0b4f81b7.  */
1673
10.4k
      if (asect->reloc_count != reloc_count + reloc_count2)
1674
0
  return false;
1675
10.4k
      BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
1676
10.4k
      || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
1677
1678
10.4k
    }
1679
90
  else
1680
90
    {
1681
      /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
1682
   case because relocations against this section may use the
1683
   dynamic symbol table, and in that case bfd_section_from_shdr
1684
   in elf.c does not update the RELOC_COUNT.  */
1685
90
      if (asect->size == 0)
1686
4
  return true;
1687
1688
86
      rel_hdr = &d->this_hdr;
1689
86
      reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
1690
86
      rel_hdr2 = NULL;
1691
86
      reloc_count2 = 0;
1692
86
    }
1693
1694
10.5k
  if (_bfd_mul_overflow (reloc_count + reloc_count2, sizeof (arelent), &amt))
1695
0
    {
1696
0
      bfd_set_error (bfd_error_file_too_big);
1697
0
      return false;
1698
0
    }
1699
10.5k
  relents = (arelent *) bfd_alloc (abfd, amt);
1700
10.5k
  if (relents == NULL)
1701
0
    return false;
1702
1703
10.5k
  if (rel_hdr
1704
86
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1705
86
                rel_hdr, reloc_count,
1706
86
                relents,
1707
86
                symbols, dynamic))
1708
13
    return false;
1709
1710
10.5k
  if (rel_hdr2
1711
10.4k
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1712
10.4k
                rel_hdr2, reloc_count2,
1713
10.4k
                relents + reloc_count,
1714
10.4k
                symbols, dynamic))
1715
2.50k
    return false;
1716
1717
8.06k
  if (!bed->slurp_secondary_relocs (abfd, asect, symbols, dynamic))
1718
32
    return false;
1719
1720
8.03k
  asect->relocation = relents;
1721
  return true;
1722
8.06k
}
bfd_elf32_slurp_reloc_table
Line
Count
Source
1648
4.39k
{
1649
4.39k
  elf_backend_data *bed = get_elf_backend_data (abfd);
1650
4.39k
  struct bfd_elf_section_data * const d = elf_section_data (asect);
1651
4.39k
  Elf_Internal_Shdr *rel_hdr;
1652
4.39k
  Elf_Internal_Shdr *rel_hdr2;
1653
4.39k
  bfd_size_type reloc_count;
1654
4.39k
  bfd_size_type reloc_count2;
1655
4.39k
  arelent *relents;
1656
4.39k
  size_t amt;
1657
1658
4.39k
  if (asect->relocation != NULL)
1659
171
    return true;
1660
1661
4.22k
  if (! dynamic)
1662
4.21k
    {
1663
4.21k
      if ((asect->flags & SEC_RELOC) == 0
1664
2.91k
    || asect->reloc_count == 0)
1665
1.32k
  return true;
1666
1667
2.88k
      rel_hdr = d->rel.hdr;
1668
2.88k
      reloc_count = rel_hdr ? NUM_SHDR_ENTRIES (rel_hdr) : 0;
1669
2.88k
      rel_hdr2 = d->rela.hdr;
1670
2.88k
      reloc_count2 = rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0;
1671
1672
      /* PR 17512: file: 0b4f81b7.  */
1673
2.88k
      if (asect->reloc_count != reloc_count + reloc_count2)
1674
0
  return false;
1675
2.88k
      BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
1676
2.88k
      || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
1677
1678
2.88k
    }
1679
9
  else
1680
9
    {
1681
      /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
1682
   case because relocations against this section may use the
1683
   dynamic symbol table, and in that case bfd_section_from_shdr
1684
   in elf.c does not update the RELOC_COUNT.  */
1685
9
      if (asect->size == 0)
1686
0
  return true;
1687
1688
9
      rel_hdr = &d->this_hdr;
1689
9
      reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
1690
9
      rel_hdr2 = NULL;
1691
9
      reloc_count2 = 0;
1692
9
    }
1693
1694
2.89k
  if (_bfd_mul_overflow (reloc_count + reloc_count2, sizeof (arelent), &amt))
1695
0
    {
1696
0
      bfd_set_error (bfd_error_file_too_big);
1697
0
      return false;
1698
0
    }
1699
2.89k
  relents = (arelent *) bfd_alloc (abfd, amt);
1700
2.89k
  if (relents == NULL)
1701
0
    return false;
1702
1703
2.89k
  if (rel_hdr
1704
2.02k
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1705
2.02k
                rel_hdr, reloc_count,
1706
2.02k
                relents,
1707
2.02k
                symbols, dynamic))
1708
696
    return false;
1709
1710
2.20k
  if (rel_hdr2
1711
868
      && !elf_slurp_reloc_table_from_section (abfd, asect,
1712
868
                rel_hdr2, reloc_count2,
1713
868
                relents + reloc_count,
1714
868
                symbols, dynamic))
1715
656
    return false;
1716
1717
1.54k
  if (!bed->slurp_secondary_relocs (abfd, asect, symbols, dynamic))
1718
0
    return false;
1719
1720
1.54k
  asect->relocation = relents;
1721
  return true;
1722
1.54k
}
1723
1724
#if DEBUG & 2
1725
static void
1726
elf_debug_section (int num, Elf_Internal_Shdr *hdr)
1727
{
1728
  fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num,
1729
     hdr->bfd_section != NULL ? hdr->bfd_section->name : "",
1730
     (long) hdr);
1731
  fprintf (stderr,
1732
     "sh_name      = %ld\tsh_type      = %ld\tsh_flags     = %ld\n",
1733
     (long) hdr->sh_name,
1734
     (long) hdr->sh_type,
1735
     (long) hdr->sh_flags);
1736
  fprintf (stderr,
1737
     "sh_addr      = %ld\tsh_offset    = %ld\tsh_size      = %ld\n",
1738
     (long) hdr->sh_addr,
1739
     (long) hdr->sh_offset,
1740
     (long) hdr->sh_size);
1741
  fprintf (stderr,
1742
     "sh_link      = %ld\tsh_info      = %ld\tsh_addralign = %ld\n",
1743
     (long) hdr->sh_link,
1744
     (long) hdr->sh_info,
1745
     (long) hdr->sh_addralign);
1746
  fprintf (stderr, "sh_entsize   = %ld\n",
1747
     (long) hdr->sh_entsize);
1748
  fflush (stderr);
1749
}
1750
#endif
1751
1752
#if DEBUG & 1
1753
static void
1754
elf_debug_file (Elf_Internal_Ehdr *ehdrp)
1755
{
1756
  fprintf (stderr, "e_entry      = 0x%.8lx\n", (long) ehdrp->e_entry);
1757
  fprintf (stderr, "e_phoff      = %ld\n", (long) ehdrp->e_phoff);
1758
  fprintf (stderr, "e_phnum      = %ld\n", (long) ehdrp->e_phnum);
1759
  fprintf (stderr, "e_phentsize  = %ld\n", (long) ehdrp->e_phentsize);
1760
  fprintf (stderr, "e_shoff      = %ld\n", (long) ehdrp->e_shoff);
1761
  fprintf (stderr, "e_shnum      = %ld\n", (long) ehdrp->e_shnum);
1762
  fprintf (stderr, "e_shentsize  = %ld\n", (long) ehdrp->e_shentsize);
1763
}
1764
#endif
1765

1766
/* Create a new BFD as if by bfd_openr.  Rather than opening a file,
1767
   reconstruct an ELF file by reading the segments out of remote
1768
   memory based on the ELF file header at EHDR_VMA and the ELF program
1769
   headers it points to.  If non-zero, SIZE is the known extent of the
1770
   object.  If not null, *LOADBASEP is filled in with the difference
1771
   between the VMAs from which the segments were read, and the VMAs
1772
   the file headers (and hence BFD's idea of each section's VMA) put
1773
   them at.
1774
1775
   The function TARGET_READ_MEMORY is called to copy LEN bytes from
1776
   the remote memory at target address VMA into the local buffer at
1777
   MYADDR; it should return zero on success or an `errno' code on
1778
   failure.  TEMPL must be a BFD for a target with the word size and
1779
   byte order found in the remote memory.  */
1780
1781
bfd *
1782
NAME(_bfd_elf,bfd_from_remote_memory)
1783
  (bfd *templ,
1784
   bfd_vma ehdr_vma    /* Bytes.  */,
1785
   bfd_size_type size  /* Octets.  */,
1786
   bfd_vma *loadbasep  /* Bytes.  */,
1787
   int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
1788
                          /* (Bytes  ,           , octets       ).  */
1789
0
{
1790
0
  Elf_External_Ehdr x_ehdr;  /* Elf file header, external form */
1791
0
  Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */
1792
0
  Elf_External_Phdr *x_phdrs;
1793
0
  Elf_Internal_Phdr *i_phdrs, *last_phdr, *first_phdr;
1794
0
  bfd *nbfd;
1795
0
  struct bfd_in_memory *bim;
1796
0
  bfd_byte *contents;
1797
0
  int err;
1798
0
  unsigned int i;
1799
0
  bfd_vma high_offset;
1800
0
  bfd_vma shdr_end;
1801
0
  bfd_vma loadbase;  /* Bytes.  */
1802
0
  size_t amt;
1803
0
  unsigned int opb = bfd_octets_per_byte (templ, NULL);
1804
1805
  /* Read in the ELF header in external format.  */
1806
0
  err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr, sizeof x_ehdr);
1807
0
  if (err)
1808
0
    {
1809
0
      bfd_set_error (bfd_error_system_call);
1810
0
      errno = err;
1811
0
      return NULL;
1812
0
    }
1813
1814
  /* Now check to see if we have a valid ELF file, and one that BFD can
1815
     make use of.  The magic number must match, the address size ('class')
1816
     and byte-swapping must match our XVEC entry.  */
1817
1818
0
  if (! elf_file_p (&x_ehdr)
1819
0
      || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
1820
0
      || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
1821
0
    {
1822
0
      bfd_set_error (bfd_error_wrong_format);
1823
0
      return NULL;
1824
0
    }
1825
1826
  /* Check that file's byte order matches xvec's */
1827
0
  switch (x_ehdr.e_ident[EI_DATA])
1828
0
    {
1829
0
    case ELFDATA2MSB:   /* Big-endian */
1830
0
      if (! bfd_header_big_endian (templ))
1831
0
  {
1832
0
    bfd_set_error (bfd_error_wrong_format);
1833
0
    return NULL;
1834
0
  }
1835
0
      break;
1836
0
    case ELFDATA2LSB:   /* Little-endian */
1837
0
      if (! bfd_header_little_endian (templ))
1838
0
  {
1839
0
    bfd_set_error (bfd_error_wrong_format);
1840
0
    return NULL;
1841
0
  }
1842
0
      break;
1843
0
    case ELFDATANONE:   /* No data encoding specified */
1844
0
    default:      /* Unknown data encoding specified */
1845
0
      bfd_set_error (bfd_error_wrong_format);
1846
0
      return NULL;
1847
0
    }
1848
1849
0
  elf_swap_ehdr_in (templ, &x_ehdr, &i_ehdr);
1850
1851
  /* The file header tells where to find the program headers.
1852
     These are what we use to actually choose what to read.  */
1853
1854
0
  if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
1855
0
    {
1856
0
      bfd_set_error (bfd_error_wrong_format);
1857
0
      return NULL;
1858
0
    }
1859
1860
0
  if (_bfd_mul_overflow (i_ehdr.e_phnum,
1861
0
       sizeof (*x_phdrs) + sizeof (*i_phdrs), &amt))
1862
0
    {
1863
0
      bfd_set_error (bfd_error_file_too_big);
1864
0
      return NULL;
1865
0
    }
1866
0
  x_phdrs = (Elf_External_Phdr *) bfd_malloc (amt);
1867
0
  if (x_phdrs == NULL)
1868
0
    return NULL;
1869
0
  err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs,
1870
0
          i_ehdr.e_phnum * sizeof x_phdrs[0]);
1871
0
  if (err)
1872
0
    {
1873
0
      free (x_phdrs);
1874
0
      bfd_set_error (bfd_error_system_call);
1875
0
      errno = err;
1876
0
      return NULL;
1877
0
    }
1878
0
  i_phdrs = (Elf_Internal_Phdr *) &x_phdrs[i_ehdr.e_phnum];
1879
1880
0
  high_offset = 0;
1881
0
  loadbase = 0;
1882
0
  first_phdr = NULL;
1883
0
  last_phdr = NULL;
1884
0
  for (i = 0; i < i_ehdr.e_phnum; ++i)
1885
0
    {
1886
0
      elf_swap_phdr_in (templ, &x_phdrs[i], &i_phdrs[i]);
1887
0
      if (i_phdrs[i].p_type == PT_LOAD)
1888
0
  {
1889
0
    bfd_vma segment_end = i_phdrs[i].p_offset + i_phdrs[i].p_filesz;
1890
1891
0
    if (segment_end > high_offset)
1892
0
      {
1893
0
        high_offset = segment_end;
1894
0
        last_phdr = &i_phdrs[i];
1895
0
      }
1896
1897
    /* If this program header covers offset zero, where the file
1898
       header sits, then we can figure out the loadbase.  */
1899
0
    if (first_phdr == NULL)
1900
0
      {
1901
0
        bfd_vma p_offset = i_phdrs[i].p_offset;  /* Octets.  */
1902
0
        bfd_vma p_vaddr = i_phdrs[i].p_vaddr;    /* Octets.  */
1903
1904
0
        if (i_phdrs[i].p_align > 1)
1905
0
    {
1906
0
      p_offset &= -(i_phdrs[i].p_align * opb);
1907
0
      p_vaddr &= -(i_phdrs[i].p_align * opb);
1908
0
    }
1909
0
        if (p_offset == 0)
1910
0
    {
1911
0
      loadbase = ehdr_vma - p_vaddr / opb;
1912
0
      first_phdr = &i_phdrs[i];
1913
0
    }
1914
0
      }
1915
0
  }
1916
0
    }
1917
0
  if (high_offset == 0)
1918
0
    {
1919
      /* There were no PT_LOAD segments, so we don't have anything to read.  */
1920
0
      free (x_phdrs);
1921
0
      bfd_set_error (bfd_error_wrong_format);
1922
0
      return NULL;
1923
0
    }
1924
1925
0
  shdr_end = 0;
1926
0
  if (i_ehdr.e_shoff != 0 && i_ehdr.e_shnum != 0 && i_ehdr.e_shentsize != 0)
1927
0
    {
1928
0
      shdr_end = i_ehdr.e_shoff + i_ehdr.e_shnum * i_ehdr.e_shentsize;
1929
1930
0
      if (last_phdr->p_filesz != last_phdr->p_memsz)
1931
0
  {
1932
    /* If the last PT_LOAD header has a bss area then ld.so will
1933
       have cleared anything past p_filesz, zapping the section
1934
       headers.  */
1935
0
  }
1936
0
      else if (size >= shdr_end)
1937
0
  high_offset = size;
1938
0
      else
1939
0
  {
1940
0
    bfd_vma page_size = get_elf_backend_data (templ)->minpagesize;
1941
0
    bfd_vma segment_end = last_phdr->p_offset + last_phdr->p_filesz;
1942
1943
    /* Assume we loaded full pages, allowing us to sometimes see
1944
       section headers.  */
1945
0
    if (page_size > 1 && shdr_end > segment_end)
1946
0
      {
1947
0
        bfd_vma page_end = (segment_end + page_size - 1) & -page_size;
1948
1949
0
        if (page_end >= shdr_end)
1950
    /* Whee, section headers covered.  */
1951
0
    high_offset = shdr_end;
1952
0
      }
1953
0
  }
1954
0
    }
1955
1956
  /* Now we know the size of the whole image we want read in.  */
1957
0
  contents = (bfd_byte *) bfd_zmalloc (high_offset);
1958
0
  if (contents == NULL)
1959
0
    {
1960
0
      free (x_phdrs);
1961
0
      return NULL;
1962
0
    }
1963
1964
0
  for (i = 0; i < i_ehdr.e_phnum; ++i)
1965
0
    if (i_phdrs[i].p_type == PT_LOAD)
1966
0
      {
1967
0
  bfd_vma start = i_phdrs[i].p_offset;         /* Octets.  */
1968
0
  bfd_vma end = start + i_phdrs[i].p_filesz;   /* Octets.  */
1969
0
  bfd_vma vaddr = i_phdrs[i].p_vaddr;          /* Octets.  */
1970
1971
  /* Extend the beginning of the first pt_load to cover file
1972
     header and program headers, if we proved earlier that its
1973
     aligned offset is 0.  */
1974
0
  if (first_phdr == &i_phdrs[i])
1975
0
    {
1976
0
      vaddr -= start;
1977
0
      start = 0;
1978
0
    }
1979
  /* Extend the end of the last pt_load to cover section headers.  */
1980
0
  if (last_phdr == &i_phdrs[i])
1981
0
    end = high_offset;
1982
0
  err = target_read_memory (loadbase + vaddr / opb,
1983
0
          contents + start, end - start);
1984
0
  if (err)
1985
0
    {
1986
0
      free (x_phdrs);
1987
0
      free (contents);
1988
0
      bfd_set_error (bfd_error_system_call);
1989
0
      errno = err;
1990
0
      return NULL;
1991
0
    }
1992
0
      }
1993
0
  free (x_phdrs);
1994
1995
  /* If the segments visible in memory didn't include the section headers,
1996
     then clear them from the file header.  */
1997
0
  if (high_offset < shdr_end)
1998
0
    {
1999
0
      memset (&x_ehdr.e_shoff, 0, sizeof x_ehdr.e_shoff);
2000
0
      memset (&x_ehdr.e_shnum, 0, sizeof x_ehdr.e_shnum);
2001
0
      memset (&x_ehdr.e_shstrndx, 0, sizeof x_ehdr.e_shstrndx);
2002
0
    }
2003
2004
  /* This will normally have been in the first PT_LOAD segment.  But it
2005
     conceivably could be missing, and we might have just changed it.  */
2006
0
  memcpy (contents, &x_ehdr, sizeof x_ehdr);
2007
2008
  /* Now we have a memory image of the ELF file contents.  Make a BFD.  */
2009
0
  bim = (struct bfd_in_memory *) bfd_malloc (sizeof (struct bfd_in_memory));
2010
0
  if (bim == NULL)
2011
0
    {
2012
0
      free (contents);
2013
0
      return NULL;
2014
0
    }
2015
0
  nbfd = _bfd_new_bfd ();
2016
0
  if (nbfd == NULL
2017
0
      || !bfd_set_filename (nbfd, "<in-memory>"))
2018
0
    {
2019
0
      free (bim);
2020
0
      free (contents);
2021
0
      return NULL;
2022
0
    }
2023
0
  nbfd->xvec = templ->xvec;
2024
0
  bim->size = high_offset;
2025
0
  bim->capacity = high_offset;
2026
0
  bim->buffer = contents;
2027
0
  nbfd->iostream = bim;
2028
0
  nbfd->flags = BFD_IN_MEMORY;
2029
0
  nbfd->iovec = &_bfd_memory_iovec;
2030
0
  nbfd->origin = 0;
2031
0
  nbfd->direction = read_direction;
2032
0
  nbfd->mtime = time (NULL);
2033
0
  nbfd->mtime_set = true;
2034
2035
0
  if (loadbasep)
2036
0
    *loadbasep = loadbase;
2037
0
  return nbfd;
2038
0
}
Unexecuted instantiation: _bfd_elf64_bfd_from_remote_memory
Unexecuted instantiation: _bfd_elf32_bfd_from_remote_memory
2039
2040
/* Function for ELF_R_INFO.  */
2041
2042
bfd_vma
2043
NAME(elf,r_info) (bfd_vma sym, bfd_vma type)
2044
1
{
2045
1
  return ELF_R_INFO (sym, type);
2046
1
}
elf64_r_info
Line
Count
Source
2044
1
{
2045
1
  return ELF_R_INFO (sym, type);
2046
1
}
Unexecuted instantiation: elf32_r_info
2047
2048
/* Function for ELF_R_SYM.  */
2049
2050
bfd_vma
2051
NAME(elf,r_sym) (bfd_vma r_info)
2052
919
{
2053
919
  return ELF_R_SYM (r_info);
2054
919
}
elf64_r_sym
Line
Count
Source
2052
887
{
2053
887
  return ELF_R_SYM (r_info);
2054
887
}
elf32_r_sym
Line
Count
Source
2052
32
{
2053
32
  return ELF_R_SYM (r_info);
2054
32
}
2055

2056
#include "elfcore.h"
2057

2058
/* Size-dependent data and functions.  */
2059
const struct elf_size_info NAME(_bfd_elf,size_info) = {
2060
  sizeof (Elf_External_Ehdr),
2061
  sizeof (Elf_External_Phdr),
2062
  sizeof (Elf_External_Shdr),
2063
  sizeof (Elf_External_Rel),
2064
  sizeof (Elf_External_Rela),
2065
  sizeof (Elf_External_Sym),
2066
  sizeof (Elf_External_Dyn),
2067
  sizeof (Elf_External_Note),
2068
  4,
2069
  1,
2070
  ARCH_SIZE, LOG_FILE_ALIGN,
2071
  ELFCLASS, EV_CURRENT,
2072
  elf_write_out_phdrs,
2073
  elf_write_shdrs_and_ehdr,
2074
  elf_checksum_contents,
2075
  elf_write_relocs,
2076
  elf_swap_symbol_in,
2077
  elf_swap_symbol_out,
2078
  elf_slurp_reloc_table,
2079
  elf_slurp_symbol_table,
2080
  elf_swap_dyn_in,
2081
  elf_swap_dyn_out,
2082
  elf_swap_reloc_in,
2083
  elf_swap_reloc_out,
2084
  elf_swap_reloca_in,
2085
  elf_swap_reloca_out
2086
};