Coverage Report

Created: 2025-06-24 06:45

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