Subversion Repositories Kolibri OS

Rev

Rev 5197 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5197 Rev 6324
1
/* ELF executable support for BFD.
1
/* ELF executable support for BFD.
2
 
2
 
3
   Copyright 1993-2013 Free Software Foundation, Inc.
3
   Copyright (C) 1993-2015 Free Software Foundation, Inc.
4
 
4
 
5
   This file is part of BFD, the Binary File Descriptor library.
5
   This file is part of BFD, the Binary File Descriptor library.
6
 
6
 
7
   This program is free software; you can redistribute it and/or modify
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
10
   (at your option) any later version.
11
 
11
 
12
   This program is distributed in the hope that it will be useful,
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
15
   GNU General Public License for more details.
16
 
16
 
17
   You should have received a copy of the GNU General Public License
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
20
   MA 02110-1301, USA.  */
21
 
21
 
22
 
22
 
23
/*
23
/*
24
SECTION
24
SECTION
25
	ELF backends
25
	ELF backends
26
 
26
 
27
	BFD support for ELF formats is being worked on.
27
	BFD support for ELF formats is being worked on.
28
	Currently, the best supported back ends are for sparc and i386
28
	Currently, the best supported back ends are for sparc and i386
29
	(running svr4 or Solaris 2).
29
	(running svr4 or Solaris 2).
30
 
30
 
31
	Documentation of the internals of the support code still needs
31
	Documentation of the internals of the support code still needs
32
	to be written.  The code is changing quickly enough that we
32
	to be written.  The code is changing quickly enough that we
33
	haven't bothered yet.  */
33
	haven't bothered yet.  */
34
 
34
 
35
/* For sparc64-cross-sparc32.  */
35
/* For sparc64-cross-sparc32.  */
36
#define _SYSCALL32
36
#define _SYSCALL32
37
#include "sysdep.h"
37
#include "sysdep.h"
38
#include "bfd.h"
38
#include "bfd.h"
39
#include "bfdlink.h"
39
#include "bfdlink.h"
40
#include "libbfd.h"
40
#include "libbfd.h"
41
#define ARCH_SIZE 0
41
#define ARCH_SIZE 0
42
#include "elf-bfd.h"
42
#include "elf-bfd.h"
43
#include "libiberty.h"
43
#include "libiberty.h"
44
#include "safe-ctype.h"
44
#include "safe-ctype.h"
45
#include "elf-linux-psinfo.h"
45
#include "elf-linux-psinfo.h"
46
 
46
 
47
#ifdef CORE_HEADER
47
#ifdef CORE_HEADER
48
#include CORE_HEADER
48
#include CORE_HEADER
49
#endif
49
#endif
50
 
50
 
51
static int elf_sort_sections (const void *, const void *);
51
static int elf_sort_sections (const void *, const void *);
52
static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
52
static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
53
static bfd_boolean prep_headers (bfd *);
53
static bfd_boolean prep_headers (bfd *);
54
static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ;
54
static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ;
55
static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
55
static bfd_boolean elf_read_notes (bfd *, file_ptr, bfd_size_type) ;
56
static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
56
static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size,
57
				    file_ptr offset);
57
				    file_ptr offset);
58
 
58
 
59
/* Swap version information in and out.  The version information is
59
/* Swap version information in and out.  The version information is
60
   currently size independent.  If that ever changes, this code will
60
   currently size independent.  If that ever changes, this code will
61
   need to move into elfcode.h.  */
61
   need to move into elfcode.h.  */
62
 
62
 
63
/* Swap in a Verdef structure.  */
63
/* Swap in a Verdef structure.  */
64
 
64
 
65
void
65
void
66
_bfd_elf_swap_verdef_in (bfd *abfd,
66
_bfd_elf_swap_verdef_in (bfd *abfd,
67
			 const Elf_External_Verdef *src,
67
			 const Elf_External_Verdef *src,
68
			 Elf_Internal_Verdef *dst)
68
			 Elf_Internal_Verdef *dst)
69
{
69
{
70
  dst->vd_version = H_GET_16 (abfd, src->vd_version);
70
  dst->vd_version = H_GET_16 (abfd, src->vd_version);
71
  dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
71
  dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
72
  dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
72
  dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
73
  dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
73
  dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
74
  dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
74
  dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
75
  dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
75
  dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
76
  dst->vd_next    = H_GET_32 (abfd, src->vd_next);
76
  dst->vd_next    = H_GET_32 (abfd, src->vd_next);
77
}
77
}
78
 
78
 
79
/* Swap out a Verdef structure.  */
79
/* Swap out a Verdef structure.  */
80
 
80
 
81
void
81
void
82
_bfd_elf_swap_verdef_out (bfd *abfd,
82
_bfd_elf_swap_verdef_out (bfd *abfd,
83
			  const Elf_Internal_Verdef *src,
83
			  const Elf_Internal_Verdef *src,
84
			  Elf_External_Verdef *dst)
84
			  Elf_External_Verdef *dst)
85
{
85
{
86
  H_PUT_16 (abfd, src->vd_version, dst->vd_version);
86
  H_PUT_16 (abfd, src->vd_version, dst->vd_version);
87
  H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
87
  H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
88
  H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
88
  H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
89
  H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
89
  H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
90
  H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
90
  H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
91
  H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
91
  H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
92
  H_PUT_32 (abfd, src->vd_next, dst->vd_next);
92
  H_PUT_32 (abfd, src->vd_next, dst->vd_next);
93
}
93
}
94
 
94
 
95
/* Swap in a Verdaux structure.  */
95
/* Swap in a Verdaux structure.  */
96
 
96
 
97
void
97
void
98
_bfd_elf_swap_verdaux_in (bfd *abfd,
98
_bfd_elf_swap_verdaux_in (bfd *abfd,
99
			  const Elf_External_Verdaux *src,
99
			  const Elf_External_Verdaux *src,
100
			  Elf_Internal_Verdaux *dst)
100
			  Elf_Internal_Verdaux *dst)
101
{
101
{
102
  dst->vda_name = H_GET_32 (abfd, src->vda_name);
102
  dst->vda_name = H_GET_32 (abfd, src->vda_name);
103
  dst->vda_next = H_GET_32 (abfd, src->vda_next);
103
  dst->vda_next = H_GET_32 (abfd, src->vda_next);
104
}
104
}
105
 
105
 
106
/* Swap out a Verdaux structure.  */
106
/* Swap out a Verdaux structure.  */
107
 
107
 
108
void
108
void
109
_bfd_elf_swap_verdaux_out (bfd *abfd,
109
_bfd_elf_swap_verdaux_out (bfd *abfd,
110
			   const Elf_Internal_Verdaux *src,
110
			   const Elf_Internal_Verdaux *src,
111
			   Elf_External_Verdaux *dst)
111
			   Elf_External_Verdaux *dst)
112
{
112
{
113
  H_PUT_32 (abfd, src->vda_name, dst->vda_name);
113
  H_PUT_32 (abfd, src->vda_name, dst->vda_name);
114
  H_PUT_32 (abfd, src->vda_next, dst->vda_next);
114
  H_PUT_32 (abfd, src->vda_next, dst->vda_next);
115
}
115
}
116
 
116
 
117
/* Swap in a Verneed structure.  */
117
/* Swap in a Verneed structure.  */
118
 
118
 
119
void
119
void
120
_bfd_elf_swap_verneed_in (bfd *abfd,
120
_bfd_elf_swap_verneed_in (bfd *abfd,
121
			  const Elf_External_Verneed *src,
121
			  const Elf_External_Verneed *src,
122
			  Elf_Internal_Verneed *dst)
122
			  Elf_Internal_Verneed *dst)
123
{
123
{
124
  dst->vn_version = H_GET_16 (abfd, src->vn_version);
124
  dst->vn_version = H_GET_16 (abfd, src->vn_version);
125
  dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
125
  dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
126
  dst->vn_file    = H_GET_32 (abfd, src->vn_file);
126
  dst->vn_file    = H_GET_32 (abfd, src->vn_file);
127
  dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
127
  dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
128
  dst->vn_next    = H_GET_32 (abfd, src->vn_next);
128
  dst->vn_next    = H_GET_32 (abfd, src->vn_next);
129
}
129
}
130
 
130
 
131
/* Swap out a Verneed structure.  */
131
/* Swap out a Verneed structure.  */
132
 
132
 
133
void
133
void
134
_bfd_elf_swap_verneed_out (bfd *abfd,
134
_bfd_elf_swap_verneed_out (bfd *abfd,
135
			   const Elf_Internal_Verneed *src,
135
			   const Elf_Internal_Verneed *src,
136
			   Elf_External_Verneed *dst)
136
			   Elf_External_Verneed *dst)
137
{
137
{
138
  H_PUT_16 (abfd, src->vn_version, dst->vn_version);
138
  H_PUT_16 (abfd, src->vn_version, dst->vn_version);
139
  H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
139
  H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
140
  H_PUT_32 (abfd, src->vn_file, dst->vn_file);
140
  H_PUT_32 (abfd, src->vn_file, dst->vn_file);
141
  H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
141
  H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
142
  H_PUT_32 (abfd, src->vn_next, dst->vn_next);
142
  H_PUT_32 (abfd, src->vn_next, dst->vn_next);
143
}
143
}
144
 
144
 
145
/* Swap in a Vernaux structure.  */
145
/* Swap in a Vernaux structure.  */
146
 
146
 
147
void
147
void
148
_bfd_elf_swap_vernaux_in (bfd *abfd,
148
_bfd_elf_swap_vernaux_in (bfd *abfd,
149
			  const Elf_External_Vernaux *src,
149
			  const Elf_External_Vernaux *src,
150
			  Elf_Internal_Vernaux *dst)
150
			  Elf_Internal_Vernaux *dst)
151
{
151
{
152
  dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
152
  dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
153
  dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
153
  dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
154
  dst->vna_other = H_GET_16 (abfd, src->vna_other);
154
  dst->vna_other = H_GET_16 (abfd, src->vna_other);
155
  dst->vna_name  = H_GET_32 (abfd, src->vna_name);
155
  dst->vna_name  = H_GET_32 (abfd, src->vna_name);
156
  dst->vna_next  = H_GET_32 (abfd, src->vna_next);
156
  dst->vna_next  = H_GET_32 (abfd, src->vna_next);
157
}
157
}
158
 
158
 
159
/* Swap out a Vernaux structure.  */
159
/* Swap out a Vernaux structure.  */
160
 
160
 
161
void
161
void
162
_bfd_elf_swap_vernaux_out (bfd *abfd,
162
_bfd_elf_swap_vernaux_out (bfd *abfd,
163
			   const Elf_Internal_Vernaux *src,
163
			   const Elf_Internal_Vernaux *src,
164
			   Elf_External_Vernaux *dst)
164
			   Elf_External_Vernaux *dst)
165
{
165
{
166
  H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
166
  H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
167
  H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
167
  H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
168
  H_PUT_16 (abfd, src->vna_other, dst->vna_other);
168
  H_PUT_16 (abfd, src->vna_other, dst->vna_other);
169
  H_PUT_32 (abfd, src->vna_name, dst->vna_name);
169
  H_PUT_32 (abfd, src->vna_name, dst->vna_name);
170
  H_PUT_32 (abfd, src->vna_next, dst->vna_next);
170
  H_PUT_32 (abfd, src->vna_next, dst->vna_next);
171
}
171
}
172
 
172
 
173
/* Swap in a Versym structure.  */
173
/* Swap in a Versym structure.  */
174
 
174
 
175
void
175
void
176
_bfd_elf_swap_versym_in (bfd *abfd,
176
_bfd_elf_swap_versym_in (bfd *abfd,
177
			 const Elf_External_Versym *src,
177
			 const Elf_External_Versym *src,
178
			 Elf_Internal_Versym *dst)
178
			 Elf_Internal_Versym *dst)
179
{
179
{
180
  dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
180
  dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
181
}
181
}
182
 
182
 
183
/* Swap out a Versym structure.  */
183
/* Swap out a Versym structure.  */
184
 
184
 
185
void
185
void
186
_bfd_elf_swap_versym_out (bfd *abfd,
186
_bfd_elf_swap_versym_out (bfd *abfd,
187
			  const Elf_Internal_Versym *src,
187
			  const Elf_Internal_Versym *src,
188
			  Elf_External_Versym *dst)
188
			  Elf_External_Versym *dst)
189
{
189
{
190
  H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
190
  H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
191
}
191
}
192
 
192
 
193
/* Standard ELF hash function.  Do not change this function; you will
193
/* Standard ELF hash function.  Do not change this function; you will
194
   cause invalid hash tables to be generated.  */
194
   cause invalid hash tables to be generated.  */
195
 
195
 
196
unsigned long
196
unsigned long
197
bfd_elf_hash (const char *namearg)
197
bfd_elf_hash (const char *namearg)
198
{
198
{
199
  const unsigned char *name = (const unsigned char *) namearg;
199
  const unsigned char *name = (const unsigned char *) namearg;
200
  unsigned long h = 0;
200
  unsigned long h = 0;
201
  unsigned long g;
201
  unsigned long g;
202
  int ch;
202
  int ch;
203
 
203
 
204
  while ((ch = *name++) != '\0')
204
  while ((ch = *name++) != '\0')
205
    {
205
    {
206
      h = (h << 4) + ch;
206
      h = (h << 4) + ch;
207
      if ((g = (h & 0xf0000000)) != 0)
207
      if ((g = (h & 0xf0000000)) != 0)
208
	{
208
	{
209
	  h ^= g >> 24;
209
	  h ^= g >> 24;
210
	  /* The ELF ABI says `h &= ~g', but this is equivalent in
210
	  /* The ELF ABI says `h &= ~g', but this is equivalent in
211
	     this case and on some machines one insn instead of two.  */
211
	     this case and on some machines one insn instead of two.  */
212
	  h ^= g;
212
	  h ^= g;
213
	}
213
	}
214
    }
214
    }
215
  return h & 0xffffffff;
215
  return h & 0xffffffff;
216
}
216
}
217
 
217
 
218
/* DT_GNU_HASH hash function.  Do not change this function; you will
218
/* DT_GNU_HASH hash function.  Do not change this function; you will
219
   cause invalid hash tables to be generated.  */
219
   cause invalid hash tables to be generated.  */
220
 
220
 
221
unsigned long
221
unsigned long
222
bfd_elf_gnu_hash (const char *namearg)
222
bfd_elf_gnu_hash (const char *namearg)
223
{
223
{
224
  const unsigned char *name = (const unsigned char *) namearg;
224
  const unsigned char *name = (const unsigned char *) namearg;
225
  unsigned long h = 5381;
225
  unsigned long h = 5381;
226
  unsigned char ch;
226
  unsigned char ch;
227
 
227
 
228
  while ((ch = *name++) != '\0')
228
  while ((ch = *name++) != '\0')
229
    h = (h << 5) + h + ch;
229
    h = (h << 5) + h + ch;
230
  return h & 0xffffffff;
230
  return h & 0xffffffff;
231
}
231
}
232
 
232
 
233
/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
233
/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
234
   the object_id field of an elf_obj_tdata field set to OBJECT_ID.  */
234
   the object_id field of an elf_obj_tdata field set to OBJECT_ID.  */
235
bfd_boolean
235
bfd_boolean
236
bfd_elf_allocate_object (bfd *abfd,
236
bfd_elf_allocate_object (bfd *abfd,
237
			 size_t object_size,
237
			 size_t object_size,
238
			 enum elf_target_id object_id)
238
			 enum elf_target_id object_id)
239
{
239
{
240
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
240
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
241
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
241
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
242
  if (abfd->tdata.any == NULL)
242
  if (abfd->tdata.any == NULL)
243
    return FALSE;
243
    return FALSE;
244
 
244
 
245
  elf_object_id (abfd) = object_id;
245
  elf_object_id (abfd) = object_id;
246
  if (abfd->direction != read_direction)
246
  if (abfd->direction != read_direction)
247
    {
247
    {
248
      struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
248
      struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
249
      if (o == NULL)
249
      if (o == NULL)
250
	return FALSE;
250
	return FALSE;
251
      elf_tdata (abfd)->o = o;
251
      elf_tdata (abfd)->o = o;
252
      elf_program_header_size (abfd) = (bfd_size_type) -1;
252
      elf_program_header_size (abfd) = (bfd_size_type) -1;
253
    }
253
    }
254
  return TRUE;
254
  return TRUE;
255
}
255
}
256
 
256
 
257
 
257
 
258
bfd_boolean
258
bfd_boolean
259
bfd_elf_make_object (bfd *abfd)
259
bfd_elf_make_object (bfd *abfd)
260
{
260
{
261
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
261
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
262
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
262
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata),
263
				  bed->target_id);
263
				  bed->target_id);
264
}
264
}
265
 
265
 
266
bfd_boolean
266
bfd_boolean
267
bfd_elf_mkcorefile (bfd *abfd)
267
bfd_elf_mkcorefile (bfd *abfd)
268
{
268
{
269
  /* I think this can be done just like an object file.  */
269
  /* I think this can be done just like an object file.  */
270
  if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
270
  if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
271
    return FALSE;
271
    return FALSE;
272
  elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
272
  elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
273
  return elf_tdata (abfd)->core != NULL;
273
  return elf_tdata (abfd)->core != NULL;
274
}
274
}
275
 
275
 
276
static char *
276
static char *
277
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
277
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
278
{
278
{
279
  Elf_Internal_Shdr **i_shdrp;
279
  Elf_Internal_Shdr **i_shdrp;
280
  bfd_byte *shstrtab = NULL;
280
  bfd_byte *shstrtab = NULL;
281
  file_ptr offset;
281
  file_ptr offset;
282
  bfd_size_type shstrtabsize;
282
  bfd_size_type shstrtabsize;
283
 
283
 
284
  i_shdrp = elf_elfsections (abfd);
284
  i_shdrp = elf_elfsections (abfd);
285
  if (i_shdrp == 0
285
  if (i_shdrp == 0
286
      || shindex >= elf_numsections (abfd)
286
      || shindex >= elf_numsections (abfd)
287
      || i_shdrp[shindex] == 0)
287
      || i_shdrp[shindex] == 0)
288
    return NULL;
288
    return NULL;
289
 
289
 
290
  shstrtab = i_shdrp[shindex]->contents;
290
  shstrtab = i_shdrp[shindex]->contents;
291
  if (shstrtab == NULL)
291
  if (shstrtab == NULL)
292
    {
292
    {
293
      /* No cached one, attempt to read, and cache what we read.  */
293
      /* No cached one, attempt to read, and cache what we read.  */
294
      offset = i_shdrp[shindex]->sh_offset;
294
      offset = i_shdrp[shindex]->sh_offset;
295
      shstrtabsize = i_shdrp[shindex]->sh_size;
295
      shstrtabsize = i_shdrp[shindex]->sh_size;
296
 
296
 
297
      /* Allocate and clear an extra byte at the end, to prevent crashes
297
      /* Allocate and clear an extra byte at the end, to prevent crashes
298
	 in case the string table is not terminated.  */
298
	 in case the string table is not terminated.  */
299
      if (shstrtabsize + 1 <= 1
299
      if (shstrtabsize + 1 <= 1
-
 
300
	  || bfd_seek (abfd, offset, SEEK_SET) != 0
300
	  || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL
301
	  || (shstrtab = (bfd_byte *) bfd_alloc (abfd, shstrtabsize + 1)) == NULL)
301
	  || bfd_seek (abfd, offset, SEEK_SET) != 0)
-
 
302
	shstrtab = NULL;
302
	shstrtab = NULL;
303
      else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
303
      else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize)
304
	{
304
	{
305
	  if (bfd_get_error () != bfd_error_system_call)
305
	  if (bfd_get_error () != bfd_error_system_call)
306
	    bfd_set_error (bfd_error_file_truncated);
306
	    bfd_set_error (bfd_error_file_truncated);
-
 
307
	  bfd_release (abfd, shstrtab);
307
	  shstrtab = NULL;
308
	  shstrtab = NULL;
308
	  /* Once we've failed to read it, make sure we don't keep
309
	  /* Once we've failed to read it, make sure we don't keep
309
	     trying.  Otherwise, we'll keep allocating space for
310
	     trying.  Otherwise, we'll keep allocating space for
310
	     the string table over and over.  */
311
	     the string table over and over.  */
311
	  i_shdrp[shindex]->sh_size = 0;
312
	  i_shdrp[shindex]->sh_size = 0;
312
	}
313
	}
313
      else
314
      else
314
	shstrtab[shstrtabsize] = '\0';
315
	shstrtab[shstrtabsize] = '\0';
315
      i_shdrp[shindex]->contents = shstrtab;
316
      i_shdrp[shindex]->contents = shstrtab;
316
    }
317
    }
317
  return (char *) shstrtab;
318
  return (char *) shstrtab;
318
}
319
}
319
 
320
 
320
char *
321
char *
321
bfd_elf_string_from_elf_section (bfd *abfd,
322
bfd_elf_string_from_elf_section (bfd *abfd,
322
				 unsigned int shindex,
323
				 unsigned int shindex,
323
				 unsigned int strindex)
324
				 unsigned int strindex)
324
{
325
{
325
  Elf_Internal_Shdr *hdr;
326
  Elf_Internal_Shdr *hdr;
326
 
327
 
327
  if (strindex == 0)
328
  if (strindex == 0)
328
    return "";
329
    return "";
329
 
330
 
330
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
331
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
331
    return NULL;
332
    return NULL;
332
 
333
 
333
  hdr = elf_elfsections (abfd)[shindex];
334
  hdr = elf_elfsections (abfd)[shindex];
334
 
335
 
-
 
336
  if (hdr->contents == NULL)
-
 
337
    {
-
 
338
      if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
-
 
339
	{
-
 
340
	  /* PR 17512: file: f057ec89.  */
-
 
341
	  _bfd_error_handler (_("%B: attempt to load strings from a non-string section (number %d)"),
-
 
342
			      abfd, shindex);
-
 
343
	  return NULL;
-
 
344
	}
335
  if (hdr->contents == NULL
345
 
336
      && bfd_elf_get_str_section (abfd, shindex) == NULL)
346
      if (bfd_elf_get_str_section (abfd, shindex) == NULL)
-
 
347
	return NULL;
337
    return NULL;
348
    }
338
 
349
 
339
  if (strindex >= hdr->sh_size)
350
  if (strindex >= hdr->sh_size)
340
    {
351
    {
341
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
352
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
342
      (*_bfd_error_handler)
353
      (*_bfd_error_handler)
343
	(_("%B: invalid string offset %u >= %lu for section `%s'"),
354
	(_("%B: invalid string offset %u >= %lu for section `%s'"),
344
	 abfd, strindex, (unsigned long) hdr->sh_size,
355
	 abfd, strindex, (unsigned long) hdr->sh_size,
345
	 (shindex == shstrndx && strindex == hdr->sh_name
356
	 (shindex == shstrndx && strindex == hdr->sh_name
346
	  ? ".shstrtab"
357
	  ? ".shstrtab"
347
	  : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
358
	  : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
348
      return NULL;
359
      return NULL;
349
    }
360
    }
350
 
361
 
351
  return ((char *) hdr->contents) + strindex;
362
  return ((char *) hdr->contents) + strindex;
352
}
363
}
353
 
364
 
354
/* Read and convert symbols to internal format.
365
/* Read and convert symbols to internal format.
355
   SYMCOUNT specifies the number of symbols to read, starting from
366
   SYMCOUNT specifies the number of symbols to read, starting from
356
   symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
367
   symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
357
   are non-NULL, they are used to store the internal symbols, external
368
   are non-NULL, they are used to store the internal symbols, external
358
   symbols, and symbol section index extensions, respectively.
369
   symbols, and symbol section index extensions, respectively.
359
   Returns a pointer to the internal symbol buffer (malloced if necessary)
370
   Returns a pointer to the internal symbol buffer (malloced if necessary)
360
   or NULL if there were no symbols or some kind of problem.  */
371
   or NULL if there were no symbols or some kind of problem.  */
361
 
372
 
362
Elf_Internal_Sym *
373
Elf_Internal_Sym *
363
bfd_elf_get_elf_syms (bfd *ibfd,
374
bfd_elf_get_elf_syms (bfd *ibfd,
364
		      Elf_Internal_Shdr *symtab_hdr,
375
		      Elf_Internal_Shdr *symtab_hdr,
365
		      size_t symcount,
376
		      size_t symcount,
366
		      size_t symoffset,
377
		      size_t symoffset,
367
		      Elf_Internal_Sym *intsym_buf,
378
		      Elf_Internal_Sym *intsym_buf,
368
		      void *extsym_buf,
379
		      void *extsym_buf,
369
		      Elf_External_Sym_Shndx *extshndx_buf)
380
		      Elf_External_Sym_Shndx *extshndx_buf)
370
{
381
{
371
  Elf_Internal_Shdr *shndx_hdr;
382
  Elf_Internal_Shdr *shndx_hdr;
372
  void *alloc_ext;
383
  void *alloc_ext;
373
  const bfd_byte *esym;
384
  const bfd_byte *esym;
374
  Elf_External_Sym_Shndx *alloc_extshndx;
385
  Elf_External_Sym_Shndx *alloc_extshndx;
375
  Elf_External_Sym_Shndx *shndx;
386
  Elf_External_Sym_Shndx *shndx;
376
  Elf_Internal_Sym *alloc_intsym;
387
  Elf_Internal_Sym *alloc_intsym;
377
  Elf_Internal_Sym *isym;
388
  Elf_Internal_Sym *isym;
378
  Elf_Internal_Sym *isymend;
389
  Elf_Internal_Sym *isymend;
379
  const struct elf_backend_data *bed;
390
  const struct elf_backend_data *bed;
380
  size_t extsym_size;
391
  size_t extsym_size;
381
  bfd_size_type amt;
392
  bfd_size_type amt;
382
  file_ptr pos;
393
  file_ptr pos;
383
 
394
 
384
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
395
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
385
    abort ();
396
    abort ();
386
 
397
 
387
  if (symcount == 0)
398
  if (symcount == 0)
388
    return intsym_buf;
399
    return intsym_buf;
389
 
400
 
390
  /* Normal syms might have section extension entries.  */
401
  /* Normal syms might have section extension entries.  */
391
  shndx_hdr = NULL;
402
  shndx_hdr = NULL;
-
 
403
  if (elf_symtab_shndx_list (ibfd) != NULL)
-
 
404
    {
-
 
405
      elf_section_list * entry;
-
 
406
      Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
-
 
407
 
-
 
408
      /* Find an index section that is linked to this symtab section.  */
-
 
409
      for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
-
 
410
	if (sections[entry->hdr.sh_link] == symtab_hdr)
-
 
411
	  {
-
 
412
	    shndx_hdr = & entry->hdr;
-
 
413
	    break;
-
 
414
	  };
-
 
415
 
-
 
416
      if (shndx_hdr == NULL)
-
 
417
	{
392
  if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
418
	  if (symtab_hdr == & elf_symtab_hdr (ibfd))
-
 
419
	    /* Not really accurate, but this was how the old code used to work.  */
393
    shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
420
	    shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr;
-
 
421
	  /* Otherwise we do nothing.  The assumption is that
-
 
422
	     the index table will not be needed.  */
-
 
423
	}
-
 
424
    }
394
 
425
 
395
  /* Read the symbols.  */
426
  /* Read the symbols.  */
396
  alloc_ext = NULL;
427
  alloc_ext = NULL;
397
  alloc_extshndx = NULL;
428
  alloc_extshndx = NULL;
398
  alloc_intsym = NULL;
429
  alloc_intsym = NULL;
399
  bed = get_elf_backend_data (ibfd);
430
  bed = get_elf_backend_data (ibfd);
400
  extsym_size = bed->s->sizeof_sym;
431
  extsym_size = bed->s->sizeof_sym;
401
  amt = symcount * extsym_size;
432
  amt = symcount * extsym_size;
402
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
433
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
403
  if (extsym_buf == NULL)
434
  if (extsym_buf == NULL)
404
    {
435
    {
405
      alloc_ext = bfd_malloc2 (symcount, extsym_size);
436
      alloc_ext = bfd_malloc2 (symcount, extsym_size);
406
      extsym_buf = alloc_ext;
437
      extsym_buf = alloc_ext;
407
    }
438
    }
408
  if (extsym_buf == NULL
439
  if (extsym_buf == NULL
409
      || bfd_seek (ibfd, pos, SEEK_SET) != 0
440
      || bfd_seek (ibfd, pos, SEEK_SET) != 0
410
      || bfd_bread (extsym_buf, amt, ibfd) != amt)
441
      || bfd_bread (extsym_buf, amt, ibfd) != amt)
411
    {
442
    {
412
      intsym_buf = NULL;
443
      intsym_buf = NULL;
413
      goto out;
444
      goto out;
414
    }
445
    }
415
 
446
 
416
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
447
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
417
    extshndx_buf = NULL;
448
    extshndx_buf = NULL;
418
  else
449
  else
419
    {
450
    {
420
      amt = symcount * sizeof (Elf_External_Sym_Shndx);
451
      amt = symcount * sizeof (Elf_External_Sym_Shndx);
421
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
452
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
422
      if (extshndx_buf == NULL)
453
      if (extshndx_buf == NULL)
423
	{
454
	{
424
	  alloc_extshndx = (Elf_External_Sym_Shndx *)
455
	  alloc_extshndx = (Elf_External_Sym_Shndx *)
425
              bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
456
              bfd_malloc2 (symcount, sizeof (Elf_External_Sym_Shndx));
426
	  extshndx_buf = alloc_extshndx;
457
	  extshndx_buf = alloc_extshndx;
427
	}
458
	}
428
      if (extshndx_buf == NULL
459
      if (extshndx_buf == NULL
429
	  || bfd_seek (ibfd, pos, SEEK_SET) != 0
460
	  || bfd_seek (ibfd, pos, SEEK_SET) != 0
430
	  || bfd_bread (extshndx_buf, amt, ibfd) != amt)
461
	  || bfd_bread (extshndx_buf, amt, ibfd) != amt)
431
	{
462
	{
432
	  intsym_buf = NULL;
463
	  intsym_buf = NULL;
433
	  goto out;
464
	  goto out;
434
	}
465
	}
435
    }
466
    }
436
 
467
 
437
  if (intsym_buf == NULL)
468
  if (intsym_buf == NULL)
438
    {
469
    {
439
      alloc_intsym = (Elf_Internal_Sym *)
470
      alloc_intsym = (Elf_Internal_Sym *)
440
          bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
471
          bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym));
441
      intsym_buf = alloc_intsym;
472
      intsym_buf = alloc_intsym;
442
      if (intsym_buf == NULL)
473
      if (intsym_buf == NULL)
443
	goto out;
474
	goto out;
444
    }
475
    }
445
 
476
 
446
  /* Convert the symbols to internal form.  */
477
  /* Convert the symbols to internal form.  */
447
  isymend = intsym_buf + symcount;
478
  isymend = intsym_buf + symcount;
448
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
479
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
449
           shndx = extshndx_buf;
480
           shndx = extshndx_buf;
450
       isym < isymend;
481
       isym < isymend;
451
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
482
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
452
    if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
483
    if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
453
      {
484
      {
454
	symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
485
	symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
455
	(*_bfd_error_handler) (_("%B symbol number %lu references "
486
	(*_bfd_error_handler) (_("%B symbol number %lu references "
456
				 "nonexistent SHT_SYMTAB_SHNDX section"),
487
				 "nonexistent SHT_SYMTAB_SHNDX section"),
457
			       ibfd, (unsigned long) symoffset);
488
			       ibfd, (unsigned long) symoffset);
458
	if (alloc_intsym != NULL)
489
	if (alloc_intsym != NULL)
459
	  free (alloc_intsym);
490
	  free (alloc_intsym);
460
	intsym_buf = NULL;
491
	intsym_buf = NULL;
461
	goto out;
492
	goto out;
462
      }
493
      }
463
 
494
 
464
 out:
495
 out:
465
  if (alloc_ext != NULL)
496
  if (alloc_ext != NULL)
466
    free (alloc_ext);
497
    free (alloc_ext);
467
  if (alloc_extshndx != NULL)
498
  if (alloc_extshndx != NULL)
468
    free (alloc_extshndx);
499
    free (alloc_extshndx);
469
 
500
 
470
  return intsym_buf;
501
  return intsym_buf;
471
}
502
}
472
 
503
 
473
/* Look up a symbol name.  */
504
/* Look up a symbol name.  */
474
const char *
505
const char *
475
bfd_elf_sym_name (bfd *abfd,
506
bfd_elf_sym_name (bfd *abfd,
476
		  Elf_Internal_Shdr *symtab_hdr,
507
		  Elf_Internal_Shdr *symtab_hdr,
477
		  Elf_Internal_Sym *isym,
508
		  Elf_Internal_Sym *isym,
478
		  asection *sym_sec)
509
		  asection *sym_sec)
479
{
510
{
480
  const char *name;
511
  const char *name;
481
  unsigned int iname = isym->st_name;
512
  unsigned int iname = isym->st_name;
482
  unsigned int shindex = symtab_hdr->sh_link;
513
  unsigned int shindex = symtab_hdr->sh_link;
483
 
514
 
484
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
515
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
485
      /* Check for a bogus st_shndx to avoid crashing.  */
516
      /* Check for a bogus st_shndx to avoid crashing.  */
486
      && isym->st_shndx < elf_numsections (abfd))
517
      && isym->st_shndx < elf_numsections (abfd))
487
    {
518
    {
488
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
519
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
489
      shindex = elf_elfheader (abfd)->e_shstrndx;
520
      shindex = elf_elfheader (abfd)->e_shstrndx;
490
    }
521
    }
491
 
522
 
492
  name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
523
  name = bfd_elf_string_from_elf_section (abfd, shindex, iname);
493
  if (name == NULL)
524
  if (name == NULL)
494
    name = "(null)";
525
    name = "(null)";
495
  else if (sym_sec && *name == '\0')
526
  else if (sym_sec && *name == '\0')
496
    name = bfd_section_name (abfd, sym_sec);
527
    name = bfd_section_name (abfd, sym_sec);
497
 
528
 
498
  return name;
529
  return name;
499
}
530
}
500
 
531
 
501
/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
532
/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
502
   sections.  The first element is the flags, the rest are section
533
   sections.  The first element is the flags, the rest are section
503
   pointers.  */
534
   pointers.  */
504
 
535
 
505
typedef union elf_internal_group {
536
typedef union elf_internal_group {
506
  Elf_Internal_Shdr *shdr;
537
  Elf_Internal_Shdr *shdr;
507
  unsigned int flags;
538
  unsigned int flags;
508
} Elf_Internal_Group;
539
} Elf_Internal_Group;
509
 
540
 
510
/* Return the name of the group signature symbol.  Why isn't the
541
/* Return the name of the group signature symbol.  Why isn't the
511
   signature just a string?  */
542
   signature just a string?  */
512
 
543
 
513
static const char *
544
static const char *
514
group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
545
group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
515
{
546
{
516
  Elf_Internal_Shdr *hdr;
547
  Elf_Internal_Shdr *hdr;
517
  unsigned char esym[sizeof (Elf64_External_Sym)];
548
  unsigned char esym[sizeof (Elf64_External_Sym)];
518
  Elf_External_Sym_Shndx eshndx;
549
  Elf_External_Sym_Shndx eshndx;
519
  Elf_Internal_Sym isym;
550
  Elf_Internal_Sym isym;
520
 
551
 
521
  /* First we need to ensure the symbol table is available.  Make sure
552
  /* First we need to ensure the symbol table is available.  Make sure
522
     that it is a symbol table section.  */
553
     that it is a symbol table section.  */
523
  if (ghdr->sh_link >= elf_numsections (abfd))
554
  if (ghdr->sh_link >= elf_numsections (abfd))
524
    return NULL;
555
    return NULL;
525
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
556
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
526
  if (hdr->sh_type != SHT_SYMTAB
557
  if (hdr->sh_type != SHT_SYMTAB
527
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
558
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
528
    return NULL;
559
    return NULL;
529
 
560
 
530
  /* Go read the symbol.  */
561
  /* Go read the symbol.  */
531
  hdr = &elf_tdata (abfd)->symtab_hdr;
562
  hdr = &elf_tdata (abfd)->symtab_hdr;
532
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
563
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
533
			    &isym, esym, &eshndx) == NULL)
564
			    &isym, esym, &eshndx) == NULL)
534
    return NULL;
565
    return NULL;
535
 
566
 
536
  return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
567
  return bfd_elf_sym_name (abfd, hdr, &isym, NULL);
537
}
568
}
538
 
569
 
539
/* Set next_in_group list pointer, and group name for NEWSECT.  */
570
/* Set next_in_group list pointer, and group name for NEWSECT.  */
540
 
571
 
541
static bfd_boolean
572
static bfd_boolean
542
setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
573
setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect)
543
{
574
{
544
  unsigned int num_group = elf_tdata (abfd)->num_group;
575
  unsigned int num_group = elf_tdata (abfd)->num_group;
545
 
576
 
546
  /* If num_group is zero, read in all SHT_GROUP sections.  The count
577
  /* If num_group is zero, read in all SHT_GROUP sections.  The count
547
     is set to -1 if there are no SHT_GROUP sections.  */
578
     is set to -1 if there are no SHT_GROUP sections.  */
548
  if (num_group == 0)
579
  if (num_group == 0)
549
    {
580
    {
550
      unsigned int i, shnum;
581
      unsigned int i, shnum;
551
 
582
 
552
      /* First count the number of groups.  If we have a SHT_GROUP
583
      /* First count the number of groups.  If we have a SHT_GROUP
553
	 section with just a flag word (ie. sh_size is 4), ignore it.  */
584
	 section with just a flag word (ie. sh_size is 4), ignore it.  */
554
      shnum = elf_numsections (abfd);
585
      shnum = elf_numsections (abfd);
555
      num_group = 0;
586
      num_group = 0;
556
 
587
 
557
#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize)	\
588
#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize)	\
558
	(   (shdr)->sh_type == SHT_GROUP		\
589
	(   (shdr)->sh_type == SHT_GROUP		\
559
	 && (shdr)->sh_size >= minsize			\
590
	 && (shdr)->sh_size >= minsize			\
560
	 && (shdr)->sh_entsize == GRP_ENTRY_SIZE	\
591
	 && (shdr)->sh_entsize == GRP_ENTRY_SIZE	\
561
	 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
592
	 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0)
562
 
593
 
563
      for (i = 0; i < shnum; i++)
594
      for (i = 0; i < shnum; i++)
564
	{
595
	{
565
	  Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
596
	  Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
566
 
597
 
567
	  if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
598
	  if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
568
	    num_group += 1;
599
	    num_group += 1;
569
	}
600
	}
570
 
601
 
571
      if (num_group == 0)
602
      if (num_group == 0)
572
	{
603
	{
573
	  num_group = (unsigned) -1;
604
	  num_group = (unsigned) -1;
574
	  elf_tdata (abfd)->num_group = num_group;
605
	  elf_tdata (abfd)->num_group = num_group;
575
	}
606
	}
576
      else
607
      else
577
	{
608
	{
578
	  /* We keep a list of elf section headers for group sections,
609
	  /* We keep a list of elf section headers for group sections,
579
	     so we can find them quickly.  */
610
	     so we can find them quickly.  */
580
	  bfd_size_type amt;
611
	  bfd_size_type amt;
581
 
612
 
582
	  elf_tdata (abfd)->num_group = num_group;
613
	  elf_tdata (abfd)->num_group = num_group;
583
	  elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
614
	  elf_tdata (abfd)->group_sect_ptr = (Elf_Internal_Shdr **)
584
              bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
615
              bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *));
585
	  if (elf_tdata (abfd)->group_sect_ptr == NULL)
616
	  if (elf_tdata (abfd)->group_sect_ptr == NULL)
586
	    return FALSE;
617
	    return FALSE;
587
 
618
 
588
	  num_group = 0;
619
	  num_group = 0;
589
	  for (i = 0; i < shnum; i++)
620
	  for (i = 0; i < shnum; i++)
590
	    {
621
	    {
591
	      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
622
	      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
592
 
623
 
593
	      if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
624
	      if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE))
594
		{
625
		{
595
		  unsigned char *src;
626
		  unsigned char *src;
596
		  Elf_Internal_Group *dest;
627
		  Elf_Internal_Group *dest;
597
 
628
 
598
		  /* Add to list of sections.  */
629
		  /* Add to list of sections.  */
599
		  elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
630
		  elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
600
		  num_group += 1;
631
		  num_group += 1;
601
 
632
 
602
		  /* Read the raw contents.  */
633
		  /* Read the raw contents.  */
603
		  BFD_ASSERT (sizeof (*dest) >= 4);
634
		  BFD_ASSERT (sizeof (*dest) >= 4);
604
		  amt = shdr->sh_size * sizeof (*dest) / 4;
635
		  amt = shdr->sh_size * sizeof (*dest) / 4;
605
		  shdr->contents = (unsigned char *)
636
		  shdr->contents = (unsigned char *)
606
                      bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
637
                      bfd_alloc2 (abfd, shdr->sh_size, sizeof (*dest) / 4);
607
		  /* PR binutils/4110: Handle corrupt group headers.  */
638
		  /* PR binutils/4110: Handle corrupt group headers.  */
608
		  if (shdr->contents == NULL)
639
		  if (shdr->contents == NULL)
609
		    {
640
		    {
610
		      _bfd_error_handler
641
		      _bfd_error_handler
611
			(_("%B: Corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
642
			(_("%B: corrupt size field in group section header: 0x%lx"), abfd, shdr->sh_size);
612
		      bfd_set_error (bfd_error_bad_value);
643
		      bfd_set_error (bfd_error_bad_value);
613
		      return FALSE;
644
		      -- num_group;
-
 
645
		      continue;
614
		    }
646
		    }
615
 
647
 
616
		  memset (shdr->contents, 0, amt);
648
		  memset (shdr->contents, 0, amt);
617
 
649
 
618
		  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
650
		  if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
619
		      || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
651
		      || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
620
			  != shdr->sh_size))
652
			  != shdr->sh_size))
-
 
653
		    {
-
 
654
		      _bfd_error_handler
-
 
655
			(_("%B: invalid size field in group section header: 0x%lx"), abfd, shdr->sh_size);
-
 
656
		      bfd_set_error (bfd_error_bad_value);
-
 
657
		      -- num_group;
-
 
658
		      /* PR 17510: If the group contents are even partially
-
 
659
			 corrupt, do not allow any of the contents to be used.  */
-
 
660
		      memset (shdr->contents, 0, amt);
621
		    return FALSE;
661
		      continue;
-
 
662
		    }
622
 
663
 
623
		  /* Translate raw contents, a flag word followed by an
664
		  /* Translate raw contents, a flag word followed by an
624
		     array of elf section indices all in target byte order,
665
		     array of elf section indices all in target byte order,
625
		     to the flag word followed by an array of elf section
666
		     to the flag word followed by an array of elf section
626
		     pointers.  */
667
		     pointers.  */
627
		  src = shdr->contents + shdr->sh_size;
668
		  src = shdr->contents + shdr->sh_size;
628
		  dest = (Elf_Internal_Group *) (shdr->contents + amt);
669
		  dest = (Elf_Internal_Group *) (shdr->contents + amt);
-
 
670
 
629
		  while (1)
671
		  while (1)
630
		    {
672
		    {
631
		      unsigned int idx;
673
		      unsigned int idx;
632
 
674
 
633
		      src -= 4;
675
		      src -= 4;
634
		      --dest;
676
		      --dest;
635
		      idx = H_GET_32 (abfd, src);
677
		      idx = H_GET_32 (abfd, src);
636
		      if (src == shdr->contents)
678
		      if (src == shdr->contents)
637
			{
679
			{
638
			  dest->flags = idx;
680
			  dest->flags = idx;
639
			  if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
681
			  if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
640
			    shdr->bfd_section->flags
682
			    shdr->bfd_section->flags
641
			      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
683
			      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
642
			  break;
684
			  break;
643
			}
685
			}
644
		      if (idx >= shnum)
686
		      if (idx >= shnum)
645
			{
687
			{
646
			  ((*_bfd_error_handler)
688
			  ((*_bfd_error_handler)
647
			   (_("%B: invalid SHT_GROUP entry"), abfd));
689
			   (_("%B: invalid SHT_GROUP entry"), abfd));
648
			  idx = 0;
690
			  idx = 0;
649
			}
691
			}
650
		      dest->shdr = elf_elfsections (abfd)[idx];
692
		      dest->shdr = elf_elfsections (abfd)[idx];
651
		    }
693
		    }
652
		}
694
		}
653
	    }
695
	    }
-
 
696
 
-
 
697
	  /* PR 17510: Corrupt binaries might contain invalid groups.  */
-
 
698
	  if (num_group != (unsigned) elf_tdata (abfd)->num_group)
-
 
699
	    {
-
 
700
	      elf_tdata (abfd)->num_group = num_group;
-
 
701
 
-
 
702
	      /* If all groups are invalid then fail.  */
-
 
703
	      if (num_group == 0)
-
 
704
		{
-
 
705
		  elf_tdata (abfd)->group_sect_ptr = NULL;
-
 
706
		  elf_tdata (abfd)->num_group = num_group = -1;
-
 
707
		  (*_bfd_error_handler) (_("%B: no valid group sections found"), abfd);
-
 
708
		  bfd_set_error (bfd_error_bad_value);
-
 
709
		}
-
 
710
	    }
654
	}
711
	}
655
    }
712
    }
656
 
713
 
657
  if (num_group != (unsigned) -1)
714
  if (num_group != (unsigned) -1)
658
    {
715
    {
659
      unsigned int i;
716
      unsigned int i;
660
 
717
 
661
      for (i = 0; i < num_group; i++)
718
      for (i = 0; i < num_group; i++)
662
	{
719
	{
663
	  Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
720
	  Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
664
	  Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
721
	  Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
665
	  unsigned int n_elt = shdr->sh_size / 4;
722
	  unsigned int n_elt = shdr->sh_size / 4;
666
 
723
 
667
	  /* Look through this group's sections to see if current
724
	  /* Look through this group's sections to see if current
668
	     section is a member.  */
725
	     section is a member.  */
669
	  while (--n_elt != 0)
726
	  while (--n_elt != 0)
670
	    if ((++idx)->shdr == hdr)
727
	    if ((++idx)->shdr == hdr)
671
	      {
728
	      {
672
		asection *s = NULL;
729
		asection *s = NULL;
673
 
730
 
674
		/* We are a member of this group.  Go looking through
731
		/* We are a member of this group.  Go looking through
675
		   other members to see if any others are linked via
732
		   other members to see if any others are linked via
676
		   next_in_group.  */
733
		   next_in_group.  */
677
		idx = (Elf_Internal_Group *) shdr->contents;
734
		idx = (Elf_Internal_Group *) shdr->contents;
678
		n_elt = shdr->sh_size / 4;
735
		n_elt = shdr->sh_size / 4;
679
		while (--n_elt != 0)
736
		while (--n_elt != 0)
680
		  if ((s = (++idx)->shdr->bfd_section) != NULL
737
		  if ((s = (++idx)->shdr->bfd_section) != NULL
681
		      && elf_next_in_group (s) != NULL)
738
		      && elf_next_in_group (s) != NULL)
682
		    break;
739
		    break;
683
		if (n_elt != 0)
740
		if (n_elt != 0)
684
		  {
741
		  {
685
		    /* Snarf the group name from other member, and
742
		    /* Snarf the group name from other member, and
686
		       insert current section in circular list.  */
743
		       insert current section in circular list.  */
687
		    elf_group_name (newsect) = elf_group_name (s);
744
		    elf_group_name (newsect) = elf_group_name (s);
688
		    elf_next_in_group (newsect) = elf_next_in_group (s);
745
		    elf_next_in_group (newsect) = elf_next_in_group (s);
689
		    elf_next_in_group (s) = newsect;
746
		    elf_next_in_group (s) = newsect;
690
		  }
747
		  }
691
		else
748
		else
692
		  {
749
		  {
693
		    const char *gname;
750
		    const char *gname;
694
 
751
 
695
		    gname = group_signature (abfd, shdr);
752
		    gname = group_signature (abfd, shdr);
696
		    if (gname == NULL)
753
		    if (gname == NULL)
697
		      return FALSE;
754
		      return FALSE;
698
		    elf_group_name (newsect) = gname;
755
		    elf_group_name (newsect) = gname;
699
 
756
 
700
		    /* Start a circular list with one element.  */
757
		    /* Start a circular list with one element.  */
701
		    elf_next_in_group (newsect) = newsect;
758
		    elf_next_in_group (newsect) = newsect;
702
		  }
759
		  }
703
 
760
 
704
		/* If the group section has been created, point to the
761
		/* If the group section has been created, point to the
705
		   new member.  */
762
		   new member.  */
706
		if (shdr->bfd_section != NULL)
763
		if (shdr->bfd_section != NULL)
707
		  elf_next_in_group (shdr->bfd_section) = newsect;
764
		  elf_next_in_group (shdr->bfd_section) = newsect;
708
 
765
 
709
		i = num_group - 1;
766
		i = num_group - 1;
710
		break;
767
		break;
711
	      }
768
	      }
712
	}
769
	}
713
    }
770
    }
714
 
771
 
715
  if (elf_group_name (newsect) == NULL)
772
  if (elf_group_name (newsect) == NULL)
716
    {
773
    {
717
      (*_bfd_error_handler) (_("%B: no group info for section %A"),
774
      (*_bfd_error_handler) (_("%B: no group info for section %A"),
718
			     abfd, newsect);
775
			     abfd, newsect);
-
 
776
      return FALSE;
719
    }
777
    }
720
  return TRUE;
778
  return TRUE;
721
}
779
}
722
 
780
 
723
bfd_boolean
781
bfd_boolean
724
_bfd_elf_setup_sections (bfd *abfd)
782
_bfd_elf_setup_sections (bfd *abfd)
725
{
783
{
726
  unsigned int i;
784
  unsigned int i;
727
  unsigned int num_group = elf_tdata (abfd)->num_group;
785
  unsigned int num_group = elf_tdata (abfd)->num_group;
728
  bfd_boolean result = TRUE;
786
  bfd_boolean result = TRUE;
729
  asection *s;
787
  asection *s;
730
 
788
 
731
  /* Process SHF_LINK_ORDER.  */
789
  /* Process SHF_LINK_ORDER.  */
732
  for (s = abfd->sections; s != NULL; s = s->next)
790
  for (s = abfd->sections; s != NULL; s = s->next)
733
    {
791
    {
734
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
792
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
735
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
793
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
736
	{
794
	{
737
	  unsigned int elfsec = this_hdr->sh_link;
795
	  unsigned int elfsec = this_hdr->sh_link;
738
	  /* FIXME: The old Intel compiler and old strip/objcopy may
796
	  /* FIXME: The old Intel compiler and old strip/objcopy may
739
	     not set the sh_link or sh_info fields.  Hence we could
797
	     not set the sh_link or sh_info fields.  Hence we could
740
	     get the situation where elfsec is 0.  */
798
	     get the situation where elfsec is 0.  */
741
	  if (elfsec == 0)
799
	  if (elfsec == 0)
742
	    {
800
	    {
743
	      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
801
	      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
744
	      if (bed->link_order_error_handler)
802
	      if (bed->link_order_error_handler)
745
		bed->link_order_error_handler
803
		bed->link_order_error_handler
746
		  (_("%B: warning: sh_link not set for section `%A'"),
804
		  (_("%B: warning: sh_link not set for section `%A'"),
747
		   abfd, s);
805
		   abfd, s);
748
	    }
806
	    }
749
	  else
807
	  else
750
	    {
808
	    {
751
	      asection *linksec = NULL;
809
	      asection *linksec = NULL;
752
 
810
 
753
	      if (elfsec < elf_numsections (abfd))
811
	      if (elfsec < elf_numsections (abfd))
754
		{
812
		{
755
		  this_hdr = elf_elfsections (abfd)[elfsec];
813
		  this_hdr = elf_elfsections (abfd)[elfsec];
756
		  linksec = this_hdr->bfd_section;
814
		  linksec = this_hdr->bfd_section;
757
		}
815
		}
758
 
816
 
759
	      /* PR 1991, 2008:
817
	      /* PR 1991, 2008:
760
		 Some strip/objcopy may leave an incorrect value in
818
		 Some strip/objcopy may leave an incorrect value in
761
		 sh_link.  We don't want to proceed.  */
819
		 sh_link.  We don't want to proceed.  */
762
	      if (linksec == NULL)
820
	      if (linksec == NULL)
763
		{
821
		{
764
		  (*_bfd_error_handler)
822
		  (*_bfd_error_handler)
765
		    (_("%B: sh_link [%d] in section `%A' is incorrect"),
823
		    (_("%B: sh_link [%d] in section `%A' is incorrect"),
766
		     s->owner, s, elfsec);
824
		     s->owner, s, elfsec);
767
		  result = FALSE;
825
		  result = FALSE;
768
		}
826
		}
769
 
827
 
770
	      elf_linked_to_section (s) = linksec;
828
	      elf_linked_to_section (s) = linksec;
771
	    }
829
	    }
772
	}
830
	}
773
    }
831
    }
774
 
832
 
775
  /* Process section groups.  */
833
  /* Process section groups.  */
776
  if (num_group == (unsigned) -1)
834
  if (num_group == (unsigned) -1)
777
    return result;
835
    return result;
778
 
836
 
779
  for (i = 0; i < num_group; i++)
837
  for (i = 0; i < num_group; i++)
780
    {
838
    {
781
      Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
839
      Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
-
 
840
      Elf_Internal_Group *idx;
-
 
841
      unsigned int n_elt;
-
 
842
 
-
 
843
      /* PR binutils/18758: Beware of corrupt binaries with invalid group data.  */
-
 
844
      if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL)
-
 
845
	{
-
 
846
	  (*_bfd_error_handler)
-
 
847
	    (_("%B: section group entry number %u is corrupt"),
-
 
848
	     abfd, i);
-
 
849
	  result = FALSE;
-
 
850
	  continue;
-
 
851
	}
-
 
852
 
782
      Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
853
      idx = (Elf_Internal_Group *) shdr->contents;
783
      unsigned int n_elt = shdr->sh_size / 4;
854
      n_elt = shdr->sh_size / 4;
784
 
855
 
785
      while (--n_elt != 0)
856
      while (--n_elt != 0)
786
	if ((++idx)->shdr->bfd_section)
857
	if ((++idx)->shdr->bfd_section)
787
	  elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
858
	  elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section;
788
	else if (idx->shdr->sh_type == SHT_RELA
859
	else if (idx->shdr->sh_type == SHT_RELA
789
		 || idx->shdr->sh_type == SHT_REL)
860
		 || idx->shdr->sh_type == SHT_REL)
790
	  /* We won't include relocation sections in section groups in
861
	  /* We won't include relocation sections in section groups in
791
	     output object files. We adjust the group section size here
862
	     output object files. We adjust the group section size here
792
	     so that relocatable link will work correctly when
863
	     so that relocatable link will work correctly when
793
	     relocation sections are in section group in input object
864
	     relocation sections are in section group in input object
794
	     files.  */
865
	     files.  */
795
	  shdr->bfd_section->size -= 4;
866
	  shdr->bfd_section->size -= 4;
796
	else
867
	else
797
	  {
868
	  {
798
	    /* There are some unknown sections in the group.  */
869
	    /* There are some unknown sections in the group.  */
799
	    (*_bfd_error_handler)
870
	    (*_bfd_error_handler)
800
	      (_("%B: unknown [%d] section `%s' in group [%s]"),
871
	      (_("%B: unknown [%d] section `%s' in group [%s]"),
801
	       abfd,
872
	       abfd,
802
	       (unsigned int) idx->shdr->sh_type,
873
	       (unsigned int) idx->shdr->sh_type,
803
	       bfd_elf_string_from_elf_section (abfd,
874
	       bfd_elf_string_from_elf_section (abfd,
804
						(elf_elfheader (abfd)
875
						(elf_elfheader (abfd)
805
						 ->e_shstrndx),
876
						 ->e_shstrndx),
806
						idx->shdr->sh_name),
877
						idx->shdr->sh_name),
807
	       shdr->bfd_section->name);
878
	       shdr->bfd_section->name);
808
	    result = FALSE;
879
	    result = FALSE;
809
	  }
880
	  }
810
    }
881
    }
811
  return result;
882
  return result;
812
}
883
}
813
 
884
 
814
bfd_boolean
885
bfd_boolean
815
bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
886
bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
816
{
887
{
817
  return elf_next_in_group (sec) != NULL;
888
  return elf_next_in_group (sec) != NULL;
818
}
889
}
-
 
890
 
-
 
891
static char *
-
 
892
convert_debug_to_zdebug (bfd *abfd, const char *name)
-
 
893
{
-
 
894
  unsigned int len = strlen (name);
-
 
895
  char *new_name = bfd_alloc (abfd, len + 2);
-
 
896
  if (new_name == NULL)
-
 
897
    return NULL;
-
 
898
  new_name[0] = '.';
-
 
899
  new_name[1] = 'z';
-
 
900
  memcpy (new_name + 2, name + 1, len);
-
 
901
  return new_name;
-
 
902
}
-
 
903
 
-
 
904
static char *
-
 
905
convert_zdebug_to_debug (bfd *abfd, const char *name)
-
 
906
{
-
 
907
  unsigned int len = strlen (name);
-
 
908
  char *new_name = bfd_alloc (abfd, len);
-
 
909
  if (new_name == NULL)
-
 
910
    return NULL;
-
 
911
  new_name[0] = '.';
-
 
912
  memcpy (new_name + 1, name + 2, len - 1);
-
 
913
  return new_name;
-
 
914
}
819
 
915
 
820
/* Make a BFD section from an ELF section.  We store a pointer to the
916
/* Make a BFD section from an ELF section.  We store a pointer to the
821
   BFD section in the bfd_section field of the header.  */
917
   BFD section in the bfd_section field of the header.  */
822
 
918
 
823
bfd_boolean
919
bfd_boolean
824
_bfd_elf_make_section_from_shdr (bfd *abfd,
920
_bfd_elf_make_section_from_shdr (bfd *abfd,
825
				 Elf_Internal_Shdr *hdr,
921
				 Elf_Internal_Shdr *hdr,
826
				 const char *name,
922
				 const char *name,
827
				 int shindex)
923
				 int shindex)
828
{
924
{
829
  asection *newsect;
925
  asection *newsect;
830
  flagword flags;
926
  flagword flags;
831
  const struct elf_backend_data *bed;
927
  const struct elf_backend_data *bed;
832
 
928
 
833
  if (hdr->bfd_section != NULL)
929
  if (hdr->bfd_section != NULL)
834
    return TRUE;
930
    return TRUE;
835
 
931
 
836
  newsect = bfd_make_section_anyway (abfd, name);
932
  newsect = bfd_make_section_anyway (abfd, name);
837
  if (newsect == NULL)
933
  if (newsect == NULL)
838
    return FALSE;
934
    return FALSE;
839
 
935
 
840
  hdr->bfd_section = newsect;
936
  hdr->bfd_section = newsect;
841
  elf_section_data (newsect)->this_hdr = *hdr;
937
  elf_section_data (newsect)->this_hdr = *hdr;
842
  elf_section_data (newsect)->this_idx = shindex;
938
  elf_section_data (newsect)->this_idx = shindex;
843
 
939
 
844
  /* Always use the real type/flags.  */
940
  /* Always use the real type/flags.  */
845
  elf_section_type (newsect) = hdr->sh_type;
941
  elf_section_type (newsect) = hdr->sh_type;
846
  elf_section_flags (newsect) = hdr->sh_flags;
942
  elf_section_flags (newsect) = hdr->sh_flags;
847
 
943
 
848
  newsect->filepos = hdr->sh_offset;
944
  newsect->filepos = hdr->sh_offset;
849
 
945
 
850
  if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
946
  if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
851
      || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
947
      || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
852
      || ! bfd_set_section_alignment (abfd, newsect,
948
      || ! bfd_set_section_alignment (abfd, newsect,
853
				      bfd_log2 (hdr->sh_addralign)))
949
				      bfd_log2 (hdr->sh_addralign)))
854
    return FALSE;
950
    return FALSE;
855
 
951
 
856
  flags = SEC_NO_FLAGS;
952
  flags = SEC_NO_FLAGS;
857
  if (hdr->sh_type != SHT_NOBITS)
953
  if (hdr->sh_type != SHT_NOBITS)
858
    flags |= SEC_HAS_CONTENTS;
954
    flags |= SEC_HAS_CONTENTS;
859
  if (hdr->sh_type == SHT_GROUP)
955
  if (hdr->sh_type == SHT_GROUP)
860
    flags |= SEC_GROUP | SEC_EXCLUDE;
956
    flags |= SEC_GROUP | SEC_EXCLUDE;
861
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
957
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
862
    {
958
    {
863
      flags |= SEC_ALLOC;
959
      flags |= SEC_ALLOC;
864
      if (hdr->sh_type != SHT_NOBITS)
960
      if (hdr->sh_type != SHT_NOBITS)
865
	flags |= SEC_LOAD;
961
	flags |= SEC_LOAD;
866
    }
962
    }
867
  if ((hdr->sh_flags & SHF_WRITE) == 0)
963
  if ((hdr->sh_flags & SHF_WRITE) == 0)
868
    flags |= SEC_READONLY;
964
    flags |= SEC_READONLY;
869
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
965
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
870
    flags |= SEC_CODE;
966
    flags |= SEC_CODE;
871
  else if ((flags & SEC_LOAD) != 0)
967
  else if ((flags & SEC_LOAD) != 0)
872
    flags |= SEC_DATA;
968
    flags |= SEC_DATA;
873
  if ((hdr->sh_flags & SHF_MERGE) != 0)
969
  if ((hdr->sh_flags & SHF_MERGE) != 0)
874
    {
970
    {
875
      flags |= SEC_MERGE;
971
      flags |= SEC_MERGE;
876
      newsect->entsize = hdr->sh_entsize;
972
      newsect->entsize = hdr->sh_entsize;
877
      if ((hdr->sh_flags & SHF_STRINGS) != 0)
973
      if ((hdr->sh_flags & SHF_STRINGS) != 0)
878
	flags |= SEC_STRINGS;
974
	flags |= SEC_STRINGS;
879
    }
975
    }
880
  if (hdr->sh_flags & SHF_GROUP)
976
  if (hdr->sh_flags & SHF_GROUP)
881
    if (!setup_group (abfd, hdr, newsect))
977
    if (!setup_group (abfd, hdr, newsect))
882
      return FALSE;
978
      return FALSE;
883
  if ((hdr->sh_flags & SHF_TLS) != 0)
979
  if ((hdr->sh_flags & SHF_TLS) != 0)
884
    flags |= SEC_THREAD_LOCAL;
980
    flags |= SEC_THREAD_LOCAL;
885
  if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
981
  if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
886
    flags |= SEC_EXCLUDE;
982
    flags |= SEC_EXCLUDE;
887
 
983
 
888
  if ((flags & SEC_ALLOC) == 0)
984
  if ((flags & SEC_ALLOC) == 0)
889
    {
985
    {
890
      /* The debugging sections appear to be recognized only by name,
986
      /* The debugging sections appear to be recognized only by name,
891
	 not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
987
	 not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
892
      if (name [0] == '.')
988
      if (name [0] == '.')
893
	{
989
	{
894
	  const char *p;
990
	  const char *p;
895
	  int n;
991
	  int n;
896
	  if (name[1] == 'd')
992
	  if (name[1] == 'd')
897
	    p = ".debug", n = 6;
993
	    p = ".debug", n = 6;
898
	  else if (name[1] == 'g' && name[2] == 'n')
994
	  else if (name[1] == 'g' && name[2] == 'n')
899
	    p = ".gnu.linkonce.wi.", n = 17;
995
	    p = ".gnu.linkonce.wi.", n = 17;
900
	  else if (name[1] == 'g' && name[2] == 'd')
996
	  else if (name[1] == 'g' && name[2] == 'd')
901
	    p = ".gdb_index", n = 11; /* yes we really do mean 11.  */
997
	    p = ".gdb_index", n = 11; /* yes we really do mean 11.  */
902
	  else if (name[1] == 'l')
998
	  else if (name[1] == 'l')
903
	    p = ".line", n = 5;
999
	    p = ".line", n = 5;
904
	  else if (name[1] == 's')
1000
	  else if (name[1] == 's')
905
	    p = ".stab", n = 5;
1001
	    p = ".stab", n = 5;
906
	  else if (name[1] == 'z')
1002
	  else if (name[1] == 'z')
907
	    p = ".zdebug", n = 7;
1003
	    p = ".zdebug", n = 7;
908
	  else
1004
	  else
909
	    p = NULL, n = 0;
1005
	    p = NULL, n = 0;
910
	  if (p != NULL && strncmp (name, p, n) == 0)
1006
	  if (p != NULL && strncmp (name, p, n) == 0)
911
	    flags |= SEC_DEBUGGING;
1007
	    flags |= SEC_DEBUGGING;
912
	}
1008
	}
913
    }
1009
    }
914
 
1010
 
915
  /* As a GNU extension, if the name begins with .gnu.linkonce, we
1011
  /* As a GNU extension, if the name begins with .gnu.linkonce, we
916
     only link a single copy of the section.  This is used to support
1012
     only link a single copy of the section.  This is used to support
917
     g++.  g++ will emit each template expansion in its own section.
1013
     g++.  g++ will emit each template expansion in its own section.
918
     The symbols will be defined as weak, so that multiple definitions
1014
     The symbols will be defined as weak, so that multiple definitions
919
     are permitted.  The GNU linker extension is to actually discard
1015
     are permitted.  The GNU linker extension is to actually discard
920
     all but one of the sections.  */
1016
     all but one of the sections.  */
921
  if (CONST_STRNEQ (name, ".gnu.linkonce")
1017
  if (CONST_STRNEQ (name, ".gnu.linkonce")
922
      && elf_next_in_group (newsect) == NULL)
1018
      && elf_next_in_group (newsect) == NULL)
923
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1019
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
924
 
1020
 
925
  bed = get_elf_backend_data (abfd);
1021
  bed = get_elf_backend_data (abfd);
926
  if (bed->elf_backend_section_flags)
1022
  if (bed->elf_backend_section_flags)
927
    if (! bed->elf_backend_section_flags (&flags, hdr))
1023
    if (! bed->elf_backend_section_flags (&flags, hdr))
928
      return FALSE;
1024
      return FALSE;
929
 
1025
 
930
  if (! bfd_set_section_flags (abfd, newsect, flags))
1026
  if (! bfd_set_section_flags (abfd, newsect, flags))
931
    return FALSE;
1027
    return FALSE;
932
 
1028
 
933
  /* We do not parse the PT_NOTE segments as we are interested even in the
1029
  /* We do not parse the PT_NOTE segments as we are interested even in the
934
     separate debug info files which may have the segments offsets corrupted.
1030
     separate debug info files which may have the segments offsets corrupted.
935
     PT_NOTEs from the core files are currently not parsed using BFD.  */
1031
     PT_NOTEs from the core files are currently not parsed using BFD.  */
936
  if (hdr->sh_type == SHT_NOTE)
1032
  if (hdr->sh_type == SHT_NOTE)
937
    {
1033
    {
938
      bfd_byte *contents;
1034
      bfd_byte *contents;
939
 
1035
 
940
      if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
1036
      if (!bfd_malloc_and_get_section (abfd, newsect, &contents))
941
	return FALSE;
1037
	return FALSE;
942
 
1038
 
943
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
1039
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size, -1);
944
      free (contents);
1040
      free (contents);
945
    }
1041
    }
946
 
1042
 
947
  if ((flags & SEC_ALLOC) != 0)
1043
  if ((flags & SEC_ALLOC) != 0)
948
    {
1044
    {
949
      Elf_Internal_Phdr *phdr;
1045
      Elf_Internal_Phdr *phdr;
950
      unsigned int i, nload;
1046
      unsigned int i, nload;
951
 
1047
 
952
      /* Some ELF linkers produce binaries with all the program header
1048
      /* Some ELF linkers produce binaries with all the program header
953
	 p_paddr fields zero.  If we have such a binary with more than
1049
	 p_paddr fields zero.  If we have such a binary with more than
954
	 one PT_LOAD header, then leave the section lma equal to vma
1050
	 one PT_LOAD header, then leave the section lma equal to vma
955
	 so that we don't create sections with overlapping lma.  */
1051
	 so that we don't create sections with overlapping lma.  */
956
      phdr = elf_tdata (abfd)->phdr;
1052
      phdr = elf_tdata (abfd)->phdr;
957
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1053
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
958
	if (phdr->p_paddr != 0)
1054
	if (phdr->p_paddr != 0)
959
	  break;
1055
	  break;
960
	else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1056
	else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
961
	  ++nload;
1057
	  ++nload;
962
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1058
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
963
	return TRUE;
1059
	return TRUE;
964
 
1060
 
965
      phdr = elf_tdata (abfd)->phdr;
1061
      phdr = elf_tdata (abfd)->phdr;
966
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1062
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
967
	{
1063
	{
968
	  if (((phdr->p_type == PT_LOAD
1064
	  if (((phdr->p_type == PT_LOAD
969
		&& (hdr->sh_flags & SHF_TLS) == 0)
1065
		&& (hdr->sh_flags & SHF_TLS) == 0)
970
	       || phdr->p_type == PT_TLS)
1066
	       || phdr->p_type == PT_TLS)
971
	      && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1067
	      && ELF_SECTION_IN_SEGMENT (hdr, phdr))
972
	    {
1068
	    {
973
	      if ((flags & SEC_LOAD) == 0)
1069
	      if ((flags & SEC_LOAD) == 0)
974
		newsect->lma = (phdr->p_paddr
1070
		newsect->lma = (phdr->p_paddr
975
				+ hdr->sh_addr - phdr->p_vaddr);
1071
				+ hdr->sh_addr - phdr->p_vaddr);
976
	      else
1072
	      else
977
		/* We used to use the same adjustment for SEC_LOAD
1073
		/* We used to use the same adjustment for SEC_LOAD
978
		   sections, but that doesn't work if the segment
1074
		   sections, but that doesn't work if the segment
979
		   is packed with code from multiple VMAs.
1075
		   is packed with code from multiple VMAs.
980
		   Instead we calculate the section LMA based on
1076
		   Instead we calculate the section LMA based on
981
		   the segment LMA.  It is assumed that the
1077
		   the segment LMA.  It is assumed that the
982
		   segment will contain sections with contiguous
1078
		   segment will contain sections with contiguous
983
		   LMAs, even if the VMAs are not.  */
1079
		   LMAs, even if the VMAs are not.  */
984
		newsect->lma = (phdr->p_paddr
1080
		newsect->lma = (phdr->p_paddr
985
				+ hdr->sh_offset - phdr->p_offset);
1081
				+ hdr->sh_offset - phdr->p_offset);
986
 
1082
 
987
	      /* With contiguous segments, we can't tell from file
1083
	      /* With contiguous segments, we can't tell from file
988
		 offsets whether a section with zero size should
1084
		 offsets whether a section with zero size should
989
		 be placed at the end of one segment or the
1085
		 be placed at the end of one segment or the
990
		 beginning of the next.  Decide based on vaddr.  */
1086
		 beginning of the next.  Decide based on vaddr.  */
991
	      if (hdr->sh_addr >= phdr->p_vaddr
1087
	      if (hdr->sh_addr >= phdr->p_vaddr
992
		  && (hdr->sh_addr + hdr->sh_size
1088
		  && (hdr->sh_addr + hdr->sh_size
993
		      <= phdr->p_vaddr + phdr->p_memsz))
1089
		      <= phdr->p_vaddr + phdr->p_memsz))
994
		break;
1090
		break;
995
	    }
1091
	    }
996
	}
1092
	}
997
    }
1093
    }
998
 
1094
 
999
  /* Compress/decompress DWARF debug sections with names: .debug_* and
1095
  /* Compress/decompress DWARF debug sections with names: .debug_* and
1000
     .zdebug_*, after the section flags is set.  */
1096
     .zdebug_*, after the section flags is set.  */
1001
  if ((flags & SEC_DEBUGGING)
1097
  if ((flags & SEC_DEBUGGING)
1002
      && ((name[1] == 'd' && name[6] == '_')
1098
      && ((name[1] == 'd' && name[6] == '_')
1003
	  || (name[1] == 'z' && name[7] == '_')))
1099
	  || (name[1] == 'z' && name[7] == '_')))
1004
    {
1100
    {
1005
      enum { nothing, compress, decompress } action = nothing;
1101
      enum { nothing, compress, decompress } action = nothing;
1006
      char *new_name;
1102
      int compression_header_size;
-
 
1103
      bfd_size_type uncompressed_size;
-
 
1104
      bfd_boolean compressed
-
 
1105
	= bfd_is_section_compressed_with_header (abfd, newsect,
-
 
1106
						 &compression_header_size,
-
 
1107
						 &uncompressed_size);
1007
 
1108
 
1008
      if (bfd_is_section_compressed (abfd, newsect))
1109
      if (compressed)
1009
	{
1110
	{
1010
	  /* Compressed section.  Check if we should decompress.  */
1111
	  /* Compressed section.  Check if we should decompress.  */
1011
	  if ((abfd->flags & BFD_DECOMPRESS))
1112
	  if ((abfd->flags & BFD_DECOMPRESS))
1012
	    action = decompress;
1113
	    action = decompress;
1013
	}
1114
	}
-
 
1115
 
-
 
1116
      /* Compress the uncompressed section or convert from/to .zdebug*
-
 
1117
	 section.  Check if we should compress.  */
1014
      else
1118
      if (action == nothing)
1015
	{
1119
	{
-
 
1120
	  if (newsect->size != 0
-
 
1121
	      && (abfd->flags & BFD_COMPRESS)
1016
	  /* Normal section.  Check if we should compress.  */
1122
	      && compression_header_size >= 0
-
 
1123
	      && uncompressed_size > 0
-
 
1124
	      && (!compressed
-
 
1125
		  || ((compression_header_size > 0)
1017
	  if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0)
1126
		      != ((abfd->flags & BFD_COMPRESS_GABI) != 0))))
1018
	    action = compress;
1127
	    action = compress;
-
 
1128
	  else
-
 
1129
	    return TRUE;
1019
	}
1130
	}
1020
 
-
 
1021
      new_name = NULL;
1131
 
1022
      switch (action)
1132
      if (action == compress)
1023
	{
-
 
1024
	case nothing:
-
 
1025
	  break;
-
 
1026
	case compress:
1133
	{
1027
	  if (!bfd_init_section_compress_status (abfd, newsect))
1134
	  if (!bfd_init_section_compress_status (abfd, newsect))
1028
	    {
1135
	    {
1029
	      (*_bfd_error_handler)
1136
	      (*_bfd_error_handler)
1030
		(_("%B: unable to initialize compress status for section %s"),
1137
		(_("%B: unable to initialize compress status for section %s"),
1031
		 abfd, name);
1138
		 abfd, name);
1032
	      return FALSE;
1139
	      return FALSE;
1033
	    }
1140
	    }
1034
	  if (name[1] != 'z')
-
 
1035
	    {
-
 
1036
	      unsigned int len = strlen (name);
-
 
1037
 
-
 
1038
	      new_name = bfd_alloc (abfd, len + 2);
-
 
1039
	      if (new_name == NULL)
-
 
1040
		return FALSE;
-
 
1041
	      new_name[0] = '.';
-
 
1042
	      new_name[1] = 'z';
-
 
1043
	      memcpy (new_name + 2, name + 1, len);
-
 
1044
	    }
1141
	}
1045
	  break;
1142
      else
1046
	case decompress:
1143
	{
1047
	  if (!bfd_init_section_decompress_status (abfd, newsect))
1144
	  if (!bfd_init_section_decompress_status (abfd, newsect))
1048
	    {
1145
	    {
1049
	      (*_bfd_error_handler)
1146
	      (*_bfd_error_handler)
1050
		(_("%B: unable to initialize decompress status for section %s"),
1147
		(_("%B: unable to initialize decompress status for section %s"),
1051
		 abfd, name);
1148
		 abfd, name);
1052
	      return FALSE;
1149
	      return FALSE;
1053
	    }
1150
	    }
1054
	  if (name[1] == 'z')
-
 
1055
	    {
1151
	}
1056
	      unsigned int len = strlen (name);
-
 
-
 
1152
 
-
 
1153
      if (abfd->is_linker_input)
-
 
1154
	{
-
 
1155
	  if (name[1] == 'z'
-
 
1156
	      && (action == decompress
-
 
1157
		  || (action == compress
-
 
1158
		      && (abfd->flags & BFD_COMPRESS_GABI) != 0)))
-
 
1159
	    {
-
 
1160
	      /* Convert section name from .zdebug_* to .debug_* so
-
 
1161
		 that linker will consider this section as a debug
1057
 
1162
		 section.  */
1058
	      new_name = bfd_alloc (abfd, len);
1163
	      char *new_name = convert_zdebug_to_debug (abfd, name);
1059
	      if (new_name == NULL)
1164
	      if (new_name == NULL)
1060
		return FALSE;
-
 
1061
	      new_name[0] = '.';
1165
		return FALSE;
1062
	      memcpy (new_name + 1, name + 2, len - 1);
1166
	      bfd_rename_section (abfd, newsect, new_name);
1063
	    }
-
 
1064
	  break;
1167
	    }
1065
	}
1168
	}
-
 
1169
      else
1066
      if (new_name != NULL)
1170
	/* For objdump, don't rename the section.  For objcopy, delay
-
 
1171
	   section rename to elf_fake_sections.  */
1067
	bfd_rename_section (abfd, newsect, new_name);
1172
	newsect->flags |= SEC_ELF_RENAME;
1068
    }
1173
    }
1069
 
1174
 
1070
  return TRUE;
1175
  return TRUE;
1071
}
1176
}
1072
 
1177
 
1073
const char *const bfd_elf_section_type_names[] = {
1178
const char *const bfd_elf_section_type_names[] = {
1074
  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1179
  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1075
  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1180
  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1076
  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1181
  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1077
};
1182
};
1078
 
1183
 
1079
/* ELF relocs are against symbols.  If we are producing relocatable
1184
/* ELF relocs are against symbols.  If we are producing relocatable
1080
   output, and the reloc is against an external symbol, and nothing
1185
   output, and the reloc is against an external symbol, and nothing
1081
   has given us any additional addend, the resulting reloc will also
1186
   has given us any additional addend, the resulting reloc will also
1082
   be against the same symbol.  In such a case, we don't want to
1187
   be against the same symbol.  In such a case, we don't want to
1083
   change anything about the way the reloc is handled, since it will
1188
   change anything about the way the reloc is handled, since it will
1084
   all be done at final link time.  Rather than put special case code
1189
   all be done at final link time.  Rather than put special case code
1085
   into bfd_perform_relocation, all the reloc types use this howto
1190
   into bfd_perform_relocation, all the reloc types use this howto
1086
   function.  It just short circuits the reloc if producing
1191
   function.  It just short circuits the reloc if producing
1087
   relocatable output against an external symbol.  */
1192
   relocatable output against an external symbol.  */
1088
 
1193
 
1089
bfd_reloc_status_type
1194
bfd_reloc_status_type
1090
bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1195
bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1091
		       arelent *reloc_entry,
1196
		       arelent *reloc_entry,
1092
		       asymbol *symbol,
1197
		       asymbol *symbol,
1093
		       void *data ATTRIBUTE_UNUSED,
1198
		       void *data ATTRIBUTE_UNUSED,
1094
		       asection *input_section,
1199
		       asection *input_section,
1095
		       bfd *output_bfd,
1200
		       bfd *output_bfd,
1096
		       char **error_message ATTRIBUTE_UNUSED)
1201
		       char **error_message ATTRIBUTE_UNUSED)
1097
{
1202
{
1098
  if (output_bfd != NULL
1203
  if (output_bfd != NULL
1099
      && (symbol->flags & BSF_SECTION_SYM) == 0
1204
      && (symbol->flags & BSF_SECTION_SYM) == 0
1100
      && (! reloc_entry->howto->partial_inplace
1205
      && (! reloc_entry->howto->partial_inplace
1101
	  || reloc_entry->addend == 0))
1206
	  || reloc_entry->addend == 0))
1102
    {
1207
    {
1103
      reloc_entry->address += input_section->output_offset;
1208
      reloc_entry->address += input_section->output_offset;
1104
      return bfd_reloc_ok;
1209
      return bfd_reloc_ok;
1105
    }
1210
    }
1106
 
1211
 
1107
  return bfd_reloc_continue;
1212
  return bfd_reloc_continue;
1108
}
1213
}
1109

1214

1110
/* Copy the program header and other data from one object module to
1215
/* Copy the program header and other data from one object module to
1111
   another.  */
1216
   another.  */
1112
 
1217
 
1113
bfd_boolean
1218
bfd_boolean
1114
_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1219
_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1115
{
1220
{
1116
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1221
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1117
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1222
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1118
    return TRUE;
1223
    return TRUE;
1119
 
1224
 
1120
  BFD_ASSERT (!elf_flags_init (obfd)
-
 
1121
	      || (elf_elfheader (obfd)->e_flags
-
 
1122
		  == elf_elfheader (ibfd)->e_flags));
1225
  if (!elf_flags_init (obfd))
1123
 
-
 
1124
  elf_gp (obfd) = elf_gp (ibfd);
1226
    {
1125
  elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1227
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
-
 
1228
      elf_flags_init (obfd) = TRUE;
-
 
1229
    }
-
 
1230
 
-
 
1231
  elf_gp (obfd) = elf_gp (ibfd);
-
 
1232
 
-
 
1233
  /* Also copy the EI_OSABI field.  */
-
 
1234
  elf_elfheader (obfd)->e_ident[EI_OSABI] =
1126
  elf_flags_init (obfd) = TRUE;
1235
    elf_elfheader (ibfd)->e_ident[EI_OSABI];
1127
 
1236
 
1128
  /* Copy object attributes.  */
1237
  /* Copy object attributes.  */
1129
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
1238
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
-
 
1239
 
-
 
1240
  /* This is an feature for objcopy --only-keep-debug:  When a section's type
-
 
1241
     is changed to NOBITS, we preserve the sh_link and sh_info fields so that
-
 
1242
     they can be matched up with the original.  */
-
 
1243
  Elf_Internal_Shdr ** iheaders = elf_elfsections (ibfd);
-
 
1244
  Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
-
 
1245
 
-
 
1246
  if (iheaders != NULL && oheaders != NULL)
-
 
1247
    {
-
 
1248
      unsigned int i;
-
 
1249
 
-
 
1250
      for (i = 0; i < elf_numsections (obfd); i++)
-
 
1251
	{
-
 
1252
	  unsigned int j;
-
 
1253
	  Elf_Internal_Shdr * oheader = oheaders[i];
-
 
1254
 
-
 
1255
	  if (oheader == NULL
-
 
1256
	      || oheader->sh_type != SHT_NOBITS
-
 
1257
	      || oheader->sh_size == 0
-
 
1258
	      || (oheader->sh_info != 0 && oheader->sh_link != 0))
-
 
1259
	    continue;
-
 
1260
 
-
 
1261
	  /* Scan for the matching section in the input bfd.
-
 
1262
	     FIXME: We could use something better than a linear scan here.
-
 
1263
	     Unfortunately we cannot compare names as the output string table
-
 
1264
	     is empty, so instead we check size, address and type.  */
-
 
1265
	  for (j = 0; j < elf_numsections (ibfd); j++)
-
 
1266
	    {
-
 
1267
	      Elf_Internal_Shdr * iheader = iheaders[j];
-
 
1268
 
-
 
1269
	      /* Since --only-keep-debug turns all non-debug sections
-
 
1270
		 into SHT_NOBITS sections, the output SHT_NOBITS type
-
 
1271
		 matches any input type.  */
-
 
1272
	      if ((oheader->sh_type == SHT_NOBITS
-
 
1273
		   || iheader->sh_type == oheader->sh_type)
-
 
1274
		  && iheader->sh_flags == oheader->sh_flags
-
 
1275
		  && iheader->sh_addralign == oheader->sh_addralign
-
 
1276
		  && iheader->sh_entsize == oheader->sh_entsize
-
 
1277
		  && iheader->sh_size == oheader->sh_size
-
 
1278
		  && iheader->sh_addr == oheader->sh_addr
-
 
1279
		  && (iheader->sh_info != oheader->sh_info
-
 
1280
		      || iheader->sh_link != oheader->sh_link))
-
 
1281
		{
-
 
1282
		  /* Note: Strictly speaking these assignments are wrong.
-
 
1283
		     The sh_link and sh_info fields should point to the
-
 
1284
		     relevent sections in the output BFD, which may not be in
-
 
1285
		     the same location as they were in the input BFD.  But the
-
 
1286
		     whole point of this action is to preserve the original
-
 
1287
		     values of the sh_link and sh_info fields, so that they
-
 
1288
		     can be matched up with the section headers in the
-
 
1289
		     original file.  So strictly speaking we may be creating
-
 
1290
		     an invalid ELF file, but it is only for a file that just
-
 
1291
		     contains debug info and only for sections without any
-
 
1292
		     contents.  */
-
 
1293
		  if (oheader->sh_link == 0)
-
 
1294
		    oheader->sh_link = iheader->sh_link;
-
 
1295
		  if (oheader->sh_info == 0)
-
 
1296
		    oheader->sh_info = iheader->sh_info;
-
 
1297
		  break;
-
 
1298
		}
-
 
1299
	    }
-
 
1300
	}
-
 
1301
    }
-
 
1302
 
1130
  return TRUE;
1303
  return TRUE;
1131
}
1304
}
1132
 
1305
 
1133
static const char *
1306
static const char *
1134
get_segment_type (unsigned int p_type)
1307
get_segment_type (unsigned int p_type)
1135
{
1308
{
1136
  const char *pt;
1309
  const char *pt;
1137
  switch (p_type)
1310
  switch (p_type)
1138
    {
1311
    {
1139
    case PT_NULL: pt = "NULL"; break;
1312
    case PT_NULL: pt = "NULL"; break;
1140
    case PT_LOAD: pt = "LOAD"; break;
1313
    case PT_LOAD: pt = "LOAD"; break;
1141
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1314
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1142
    case PT_INTERP: pt = "INTERP"; break;
1315
    case PT_INTERP: pt = "INTERP"; break;
1143
    case PT_NOTE: pt = "NOTE"; break;
1316
    case PT_NOTE: pt = "NOTE"; break;
1144
    case PT_SHLIB: pt = "SHLIB"; break;
1317
    case PT_SHLIB: pt = "SHLIB"; break;
1145
    case PT_PHDR: pt = "PHDR"; break;
1318
    case PT_PHDR: pt = "PHDR"; break;
1146
    case PT_TLS: pt = "TLS"; break;
1319
    case PT_TLS: pt = "TLS"; break;
1147
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1320
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1148
    case PT_GNU_STACK: pt = "STACK"; break;
1321
    case PT_GNU_STACK: pt = "STACK"; break;
1149
    case PT_GNU_RELRO: pt = "RELRO"; break;
1322
    case PT_GNU_RELRO: pt = "RELRO"; break;
1150
    default: pt = NULL; break;
1323
    default: pt = NULL; break;
1151
    }
1324
    }
1152
  return pt;
1325
  return pt;
1153
}
1326
}
1154
 
1327
 
1155
/* Print out the program headers.  */
1328
/* Print out the program headers.  */
1156
 
1329
 
1157
bfd_boolean
1330
bfd_boolean
1158
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1331
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1159
{
1332
{
1160
  FILE *f = (FILE *) farg;
1333
  FILE *f = (FILE *) farg;
1161
  Elf_Internal_Phdr *p;
1334
  Elf_Internal_Phdr *p;
1162
  asection *s;
1335
  asection *s;
1163
  bfd_byte *dynbuf = NULL;
1336
  bfd_byte *dynbuf = NULL;
1164
 
1337
 
1165
  p = elf_tdata (abfd)->phdr;
1338
  p = elf_tdata (abfd)->phdr;
1166
  if (p != NULL)
1339
  if (p != NULL)
1167
    {
1340
    {
1168
      unsigned int i, c;
1341
      unsigned int i, c;
1169
 
1342
 
1170
      fprintf (f, _("\nProgram Header:\n"));
1343
      fprintf (f, _("\nProgram Header:\n"));
1171
      c = elf_elfheader (abfd)->e_phnum;
1344
      c = elf_elfheader (abfd)->e_phnum;
1172
      for (i = 0; i < c; i++, p++)
1345
      for (i = 0; i < c; i++, p++)
1173
	{
1346
	{
1174
	  const char *pt = get_segment_type (p->p_type);
1347
	  const char *pt = get_segment_type (p->p_type);
1175
	  char buf[20];
1348
	  char buf[20];
1176
 
1349
 
1177
	  if (pt == NULL)
1350
	  if (pt == NULL)
1178
	    {
1351
	    {
1179
	      sprintf (buf, "0x%lx", p->p_type);
1352
	      sprintf (buf, "0x%lx", p->p_type);
1180
	      pt = buf;
1353
	      pt = buf;
1181
	    }
1354
	    }
1182
	  fprintf (f, "%8s off    0x", pt);
1355
	  fprintf (f, "%8s off    0x", pt);
1183
	  bfd_fprintf_vma (abfd, f, p->p_offset);
1356
	  bfd_fprintf_vma (abfd, f, p->p_offset);
1184
	  fprintf (f, " vaddr 0x");
1357
	  fprintf (f, " vaddr 0x");
1185
	  bfd_fprintf_vma (abfd, f, p->p_vaddr);
1358
	  bfd_fprintf_vma (abfd, f, p->p_vaddr);
1186
	  fprintf (f, " paddr 0x");
1359
	  fprintf (f, " paddr 0x");
1187
	  bfd_fprintf_vma (abfd, f, p->p_paddr);
1360
	  bfd_fprintf_vma (abfd, f, p->p_paddr);
1188
	  fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1361
	  fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1189
	  fprintf (f, "         filesz 0x");
1362
	  fprintf (f, "         filesz 0x");
1190
	  bfd_fprintf_vma (abfd, f, p->p_filesz);
1363
	  bfd_fprintf_vma (abfd, f, p->p_filesz);
1191
	  fprintf (f, " memsz 0x");
1364
	  fprintf (f, " memsz 0x");
1192
	  bfd_fprintf_vma (abfd, f, p->p_memsz);
1365
	  bfd_fprintf_vma (abfd, f, p->p_memsz);
1193
	  fprintf (f, " flags %c%c%c",
1366
	  fprintf (f, " flags %c%c%c",
1194
		   (p->p_flags & PF_R) != 0 ? 'r' : '-',
1367
		   (p->p_flags & PF_R) != 0 ? 'r' : '-',
1195
		   (p->p_flags & PF_W) != 0 ? 'w' : '-',
1368
		   (p->p_flags & PF_W) != 0 ? 'w' : '-',
1196
		   (p->p_flags & PF_X) != 0 ? 'x' : '-');
1369
		   (p->p_flags & PF_X) != 0 ? 'x' : '-');
1197
	  if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1370
	  if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1198
	    fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1371
	    fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1199
	  fprintf (f, "\n");
1372
	  fprintf (f, "\n");
1200
	}
1373
	}
1201
    }
1374
    }
1202
 
1375
 
1203
  s = bfd_get_section_by_name (abfd, ".dynamic");
1376
  s = bfd_get_section_by_name (abfd, ".dynamic");
1204
  if (s != NULL)
1377
  if (s != NULL)
1205
    {
1378
    {
1206
      unsigned int elfsec;
1379
      unsigned int elfsec;
1207
      unsigned long shlink;
1380
      unsigned long shlink;
1208
      bfd_byte *extdyn, *extdynend;
1381
      bfd_byte *extdyn, *extdynend;
1209
      size_t extdynsize;
1382
      size_t extdynsize;
1210
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1383
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1211
 
1384
 
1212
      fprintf (f, _("\nDynamic Section:\n"));
1385
      fprintf (f, _("\nDynamic Section:\n"));
1213
 
1386
 
1214
      if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1387
      if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
1215
	goto error_return;
1388
	goto error_return;
1216
 
1389
 
1217
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1390
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1218
      if (elfsec == SHN_BAD)
1391
      if (elfsec == SHN_BAD)
1219
	goto error_return;
1392
	goto error_return;
1220
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1393
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1221
 
1394
 
1222
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1395
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1223
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1396
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1224
 
1397
 
1225
      extdyn = dynbuf;
1398
      extdyn = dynbuf;
-
 
1399
      /* PR 17512: file: 6f427532.  */
-
 
1400
      if (s->size < extdynsize)
-
 
1401
	goto error_return;
1226
      extdynend = extdyn + s->size;
1402
      extdynend = extdyn + s->size;
-
 
1403
      /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664.
-
 
1404
         Fix range check.  */
1227
      for (; extdyn < extdynend; extdyn += extdynsize)
1405
      for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1228
	{
1406
	{
1229
	  Elf_Internal_Dyn dyn;
1407
	  Elf_Internal_Dyn dyn;
1230
	  const char *name = "";
1408
	  const char *name = "";
1231
	  char ab[20];
1409
	  char ab[20];
1232
	  bfd_boolean stringp;
1410
	  bfd_boolean stringp;
1233
	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1411
	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1234
 
1412
 
1235
	  (*swap_dyn_in) (abfd, extdyn, &dyn);
1413
	  (*swap_dyn_in) (abfd, extdyn, &dyn);
1236
 
1414
 
1237
	  if (dyn.d_tag == DT_NULL)
1415
	  if (dyn.d_tag == DT_NULL)
1238
	    break;
1416
	    break;
1239
 
1417
 
1240
	  stringp = FALSE;
1418
	  stringp = FALSE;
1241
	  switch (dyn.d_tag)
1419
	  switch (dyn.d_tag)
1242
	    {
1420
	    {
1243
	    default:
1421
	    default:
1244
	      if (bed->elf_backend_get_target_dtag)
1422
	      if (bed->elf_backend_get_target_dtag)
1245
		name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1423
		name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1246
 
1424
 
1247
	      if (!strcmp (name, ""))
1425
	      if (!strcmp (name, ""))
1248
		{
1426
		{
1249
		  sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1427
		  sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1250
		  name = ab;
1428
		  name = ab;
1251
		}
1429
		}
1252
	      break;
1430
	      break;
1253
 
1431
 
1254
	    case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1432
	    case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1255
	    case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1433
	    case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1256
	    case DT_PLTGOT: name = "PLTGOT"; break;
1434
	    case DT_PLTGOT: name = "PLTGOT"; break;
1257
	    case DT_HASH: name = "HASH"; break;
1435
	    case DT_HASH: name = "HASH"; break;
1258
	    case DT_STRTAB: name = "STRTAB"; break;
1436
	    case DT_STRTAB: name = "STRTAB"; break;
1259
	    case DT_SYMTAB: name = "SYMTAB"; break;
1437
	    case DT_SYMTAB: name = "SYMTAB"; break;
1260
	    case DT_RELA: name = "RELA"; break;
1438
	    case DT_RELA: name = "RELA"; break;
1261
	    case DT_RELASZ: name = "RELASZ"; break;
1439
	    case DT_RELASZ: name = "RELASZ"; break;
1262
	    case DT_RELAENT: name = "RELAENT"; break;
1440
	    case DT_RELAENT: name = "RELAENT"; break;
1263
	    case DT_STRSZ: name = "STRSZ"; break;
1441
	    case DT_STRSZ: name = "STRSZ"; break;
1264
	    case DT_SYMENT: name = "SYMENT"; break;
1442
	    case DT_SYMENT: name = "SYMENT"; break;
1265
	    case DT_INIT: name = "INIT"; break;
1443
	    case DT_INIT: name = "INIT"; break;
1266
	    case DT_FINI: name = "FINI"; break;
1444
	    case DT_FINI: name = "FINI"; break;
1267
	    case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1445
	    case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1268
	    case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1446
	    case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1269
	    case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1447
	    case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1270
	    case DT_REL: name = "REL"; break;
1448
	    case DT_REL: name = "REL"; break;
1271
	    case DT_RELSZ: name = "RELSZ"; break;
1449
	    case DT_RELSZ: name = "RELSZ"; break;
1272
	    case DT_RELENT: name = "RELENT"; break;
1450
	    case DT_RELENT: name = "RELENT"; break;
1273
	    case DT_PLTREL: name = "PLTREL"; break;
1451
	    case DT_PLTREL: name = "PLTREL"; break;
1274
	    case DT_DEBUG: name = "DEBUG"; break;
1452
	    case DT_DEBUG: name = "DEBUG"; break;
1275
	    case DT_TEXTREL: name = "TEXTREL"; break;
1453
	    case DT_TEXTREL: name = "TEXTREL"; break;
1276
	    case DT_JMPREL: name = "JMPREL"; break;
1454
	    case DT_JMPREL: name = "JMPREL"; break;
1277
	    case DT_BIND_NOW: name = "BIND_NOW"; break;
1455
	    case DT_BIND_NOW: name = "BIND_NOW"; break;
1278
	    case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1456
	    case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1279
	    case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1457
	    case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1280
	    case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1458
	    case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1281
	    case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1459
	    case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1282
	    case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1460
	    case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1283
	    case DT_FLAGS: name = "FLAGS"; break;
1461
	    case DT_FLAGS: name = "FLAGS"; break;
1284
	    case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1462
	    case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1285
	    case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1463
	    case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1286
	    case DT_CHECKSUM: name = "CHECKSUM"; break;
1464
	    case DT_CHECKSUM: name = "CHECKSUM"; break;
1287
	    case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1465
	    case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1288
	    case DT_MOVEENT: name = "MOVEENT"; break;
1466
	    case DT_MOVEENT: name = "MOVEENT"; break;
1289
	    case DT_MOVESZ: name = "MOVESZ"; break;
1467
	    case DT_MOVESZ: name = "MOVESZ"; break;
1290
	    case DT_FEATURE: name = "FEATURE"; break;
1468
	    case DT_FEATURE: name = "FEATURE"; break;
1291
	    case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1469
	    case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1292
	    case DT_SYMINSZ: name = "SYMINSZ"; break;
1470
	    case DT_SYMINSZ: name = "SYMINSZ"; break;
1293
	    case DT_SYMINENT: name = "SYMINENT"; break;
1471
	    case DT_SYMINENT: name = "SYMINENT"; break;
1294
	    case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1472
	    case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1295
	    case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1473
	    case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1296
	    case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1474
	    case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1297
	    case DT_PLTPAD: name = "PLTPAD"; break;
1475
	    case DT_PLTPAD: name = "PLTPAD"; break;
1298
	    case DT_MOVETAB: name = "MOVETAB"; break;
1476
	    case DT_MOVETAB: name = "MOVETAB"; break;
1299
	    case DT_SYMINFO: name = "SYMINFO"; break;
1477
	    case DT_SYMINFO: name = "SYMINFO"; break;
1300
	    case DT_RELACOUNT: name = "RELACOUNT"; break;
1478
	    case DT_RELACOUNT: name = "RELACOUNT"; break;
1301
	    case DT_RELCOUNT: name = "RELCOUNT"; break;
1479
	    case DT_RELCOUNT: name = "RELCOUNT"; break;
1302
	    case DT_FLAGS_1: name = "FLAGS_1"; break;
1480
	    case DT_FLAGS_1: name = "FLAGS_1"; break;
1303
	    case DT_VERSYM: name = "VERSYM"; break;
1481
	    case DT_VERSYM: name = "VERSYM"; break;
1304
	    case DT_VERDEF: name = "VERDEF"; break;
1482
	    case DT_VERDEF: name = "VERDEF"; break;
1305
	    case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1483
	    case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1306
	    case DT_VERNEED: name = "VERNEED"; break;
1484
	    case DT_VERNEED: name = "VERNEED"; break;
1307
	    case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1485
	    case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1308
	    case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1486
	    case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1309
	    case DT_USED: name = "USED"; break;
1487
	    case DT_USED: name = "USED"; break;
1310
	    case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1488
	    case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1311
	    case DT_GNU_HASH: name = "GNU_HASH"; break;
1489
	    case DT_GNU_HASH: name = "GNU_HASH"; break;
1312
	    }
1490
	    }
1313
 
1491
 
1314
	  fprintf (f, "  %-20s ", name);
1492
	  fprintf (f, "  %-20s ", name);
1315
	  if (! stringp)
1493
	  if (! stringp)
1316
	    {
1494
	    {
1317
	      fprintf (f, "0x");
1495
	      fprintf (f, "0x");
1318
	      bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1496
	      bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1319
	    }
1497
	    }
1320
	  else
1498
	  else
1321
	    {
1499
	    {
1322
	      const char *string;
1500
	      const char *string;
1323
	      unsigned int tagv = dyn.d_un.d_val;
1501
	      unsigned int tagv = dyn.d_un.d_val;
1324
 
1502
 
1325
	      string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1503
	      string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1326
	      if (string == NULL)
1504
	      if (string == NULL)
1327
		goto error_return;
1505
		goto error_return;
1328
	      fprintf (f, "%s", string);
1506
	      fprintf (f, "%s", string);
1329
	    }
1507
	    }
1330
	  fprintf (f, "\n");
1508
	  fprintf (f, "\n");
1331
	}
1509
	}
1332
 
1510
 
1333
      free (dynbuf);
1511
      free (dynbuf);
1334
      dynbuf = NULL;
1512
      dynbuf = NULL;
1335
    }
1513
    }
1336
 
1514
 
1337
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1515
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1338
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1516
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1339
    {
1517
    {
1340
      if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1518
      if (! _bfd_elf_slurp_version_tables (abfd, FALSE))
1341
	return FALSE;
1519
	return FALSE;
1342
    }
1520
    }
1343
 
1521
 
1344
  if (elf_dynverdef (abfd) != 0)
1522
  if (elf_dynverdef (abfd) != 0)
1345
    {
1523
    {
1346
      Elf_Internal_Verdef *t;
1524
      Elf_Internal_Verdef *t;
1347
 
1525
 
1348
      fprintf (f, _("\nVersion definitions:\n"));
1526
      fprintf (f, _("\nVersion definitions:\n"));
1349
      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1527
      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1350
	{
1528
	{
1351
	  fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1529
	  fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1352
		   t->vd_flags, t->vd_hash,
1530
		   t->vd_flags, t->vd_hash,
1353
		   t->vd_nodename ? t->vd_nodename : "");
1531
		   t->vd_nodename ? t->vd_nodename : "");
1354
	  if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1532
	  if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1355
	    {
1533
	    {
1356
	      Elf_Internal_Verdaux *a;
1534
	      Elf_Internal_Verdaux *a;
1357
 
1535
 
1358
	      fprintf (f, "\t");
1536
	      fprintf (f, "\t");
1359
	      for (a = t->vd_auxptr->vda_nextptr;
1537
	      for (a = t->vd_auxptr->vda_nextptr;
1360
		   a != NULL;
1538
		   a != NULL;
1361
		   a = a->vda_nextptr)
1539
		   a = a->vda_nextptr)
1362
		fprintf (f, "%s ",
1540
		fprintf (f, "%s ",
1363
			 a->vda_nodename ? a->vda_nodename : "");
1541
			 a->vda_nodename ? a->vda_nodename : "");
1364
	      fprintf (f, "\n");
1542
	      fprintf (f, "\n");
1365
	    }
1543
	    }
1366
	}
1544
	}
1367
    }
1545
    }
1368
 
1546
 
1369
  if (elf_dynverref (abfd) != 0)
1547
  if (elf_dynverref (abfd) != 0)
1370
    {
1548
    {
1371
      Elf_Internal_Verneed *t;
1549
      Elf_Internal_Verneed *t;
1372
 
1550
 
1373
      fprintf (f, _("\nVersion References:\n"));
1551
      fprintf (f, _("\nVersion References:\n"));
1374
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1552
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1375
	{
1553
	{
1376
	  Elf_Internal_Vernaux *a;
1554
	  Elf_Internal_Vernaux *a;
1377
 
1555
 
1378
	  fprintf (f, _("  required from %s:\n"),
1556
	  fprintf (f, _("  required from %s:\n"),
1379
		   t->vn_filename ? t->vn_filename : "");
1557
		   t->vn_filename ? t->vn_filename : "");
1380
	  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1558
	  for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1381
	    fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1559
	    fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1382
		     a->vna_flags, a->vna_other,
1560
		     a->vna_flags, a->vna_other,
1383
		     a->vna_nodename ? a->vna_nodename : "");
1561
		     a->vna_nodename ? a->vna_nodename : "");
1384
	}
1562
	}
1385
    }
1563
    }
1386
 
1564
 
1387
  return TRUE;
1565
  return TRUE;
1388
 
1566
 
1389
 error_return:
1567
 error_return:
1390
  if (dynbuf != NULL)
1568
  if (dynbuf != NULL)
1391
    free (dynbuf);
1569
    free (dynbuf);
1392
  return FALSE;
1570
  return FALSE;
1393
}
1571
}
-
 
1572
 
-
 
1573
/* Get version string.  */
-
 
1574
 
-
 
1575
const char *
-
 
1576
_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
-
 
1577
				    bfd_boolean *hidden)
-
 
1578
{
-
 
1579
  const char *version_string = NULL;
-
 
1580
  if (elf_dynversym (abfd) != 0
-
 
1581
      && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
-
 
1582
    {
-
 
1583
      unsigned int vernum = ((elf_symbol_type *) symbol)->version;
-
 
1584
 
-
 
1585
      *hidden = (vernum & VERSYM_HIDDEN) != 0;
-
 
1586
      vernum &= VERSYM_VERSION;
-
 
1587
 
-
 
1588
      if (vernum == 0)
-
 
1589
	version_string = "";
-
 
1590
      else if (vernum == 1)
-
 
1591
	version_string = "Base";
-
 
1592
      else if (vernum <= elf_tdata (abfd)->cverdefs)
-
 
1593
	version_string =
-
 
1594
	  elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
-
 
1595
      else
-
 
1596
	{
-
 
1597
	  Elf_Internal_Verneed *t;
-
 
1598
 
-
 
1599
	  version_string = "";
-
 
1600
	  for (t = elf_tdata (abfd)->verref;
-
 
1601
	       t != NULL;
-
 
1602
	       t = t->vn_nextref)
-
 
1603
	    {
-
 
1604
	      Elf_Internal_Vernaux *a;
-
 
1605
 
-
 
1606
	      for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
-
 
1607
		{
-
 
1608
		  if (a->vna_other == vernum)
-
 
1609
		    {
-
 
1610
		      version_string = a->vna_nodename;
-
 
1611
		      break;
-
 
1612
		    }
-
 
1613
		}
-
 
1614
	    }
-
 
1615
	}
-
 
1616
    }
-
 
1617
  return version_string;
-
 
1618
}
1394
 
1619
 
1395
/* Display ELF-specific fields of a symbol.  */
1620
/* Display ELF-specific fields of a symbol.  */
1396
 
1621
 
1397
void
1622
void
1398
bfd_elf_print_symbol (bfd *abfd,
1623
bfd_elf_print_symbol (bfd *abfd,
1399
		      void *filep,
1624
		      void *filep,
1400
		      asymbol *symbol,
1625
		      asymbol *symbol,
1401
		      bfd_print_symbol_type how)
1626
		      bfd_print_symbol_type how)
1402
{
1627
{
1403
  FILE *file = (FILE *) filep;
1628
  FILE *file = (FILE *) filep;
1404
  switch (how)
1629
  switch (how)
1405
    {
1630
    {
1406
    case bfd_print_symbol_name:
1631
    case bfd_print_symbol_name:
1407
      fprintf (file, "%s", symbol->name);
1632
      fprintf (file, "%s", symbol->name);
1408
      break;
1633
      break;
1409
    case bfd_print_symbol_more:
1634
    case bfd_print_symbol_more:
1410
      fprintf (file, "elf ");
1635
      fprintf (file, "elf ");
1411
      bfd_fprintf_vma (abfd, file, symbol->value);
1636
      bfd_fprintf_vma (abfd, file, symbol->value);
1412
      fprintf (file, " %lx", (unsigned long) symbol->flags);
1637
      fprintf (file, " %lx", (unsigned long) symbol->flags);
1413
      break;
1638
      break;
1414
    case bfd_print_symbol_all:
1639
    case bfd_print_symbol_all:
1415
      {
1640
      {
1416
	const char *section_name;
1641
	const char *section_name;
1417
	const char *name = NULL;
1642
	const char *name = NULL;
1418
	const struct elf_backend_data *bed;
1643
	const struct elf_backend_data *bed;
1419
	unsigned char st_other;
1644
	unsigned char st_other;
1420
	bfd_vma val;
1645
	bfd_vma val;
-
 
1646
	const char *version_string;
-
 
1647
	bfd_boolean hidden;
1421
 
1648
 
1422
	section_name = symbol->section ? symbol->section->name : "(*none*)";
1649
	section_name = symbol->section ? symbol->section->name : "(*none*)";
1423
 
1650
 
1424
	bed = get_elf_backend_data (abfd);
1651
	bed = get_elf_backend_data (abfd);
1425
	if (bed->elf_backend_print_symbol_all)
1652
	if (bed->elf_backend_print_symbol_all)
1426
	  name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1653
	  name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1427
 
1654
 
1428
	if (name == NULL)
1655
	if (name == NULL)
1429
	  {
1656
	  {
1430
	    name = symbol->name;
1657
	    name = symbol->name;
1431
	    bfd_print_symbol_vandf (abfd, file, symbol);
1658
	    bfd_print_symbol_vandf (abfd, file, symbol);
1432
	  }
1659
	  }
1433
 
1660
 
1434
	fprintf (file, " %s\t", section_name);
1661
	fprintf (file, " %s\t", section_name);
1435
	/* Print the "other" value for a symbol.  For common symbols,
1662
	/* Print the "other" value for a symbol.  For common symbols,
1436
	   we've already printed the size; now print the alignment.
1663
	   we've already printed the size; now print the alignment.
1437
	   For other symbols, we have no specified alignment, and
1664
	   For other symbols, we have no specified alignment, and
1438
	   we've printed the address; now print the size.  */
1665
	   we've printed the address; now print the size.  */
1439
	if (symbol->section && bfd_is_com_section (symbol->section))
1666
	if (symbol->section && bfd_is_com_section (symbol->section))
1440
	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1667
	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1441
	else
1668
	else
1442
	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1669
	  val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1443
	bfd_fprintf_vma (abfd, file, val);
1670
	bfd_fprintf_vma (abfd, file, val);
1444
 
1671
 
1445
	/* If we have version information, print it.  */
1672
	/* If we have version information, print it.  */
-
 
1673
	version_string = _bfd_elf_get_symbol_version_string (abfd,
1446
	if (elf_dynversym (abfd) != 0
1674
							     symbol,
1447
	    && (elf_dynverdef (abfd) != 0
1675
							     &hidden);
1448
		|| elf_dynverref (abfd) != 0))
1676
	if (version_string)
1449
	  {
1677
	  {
1450
	    unsigned int vernum;
-
 
1451
	    const char *version_string;
-
 
1452
 
-
 
1453
	    vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
-
 
1454
 
-
 
1455
	    if (vernum == 0)
1678
	    if (!hidden)
1456
	      version_string = "";
-
 
1457
	    else if (vernum == 1)
-
 
1458
	      version_string = "Base";
-
 
1459
	    else if (vernum <= elf_tdata (abfd)->cverdefs)
-
 
1460
	      version_string =
-
 
1461
		elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
-
 
1462
	    else
-
 
1463
	      {
-
 
1464
		Elf_Internal_Verneed *t;
-
 
1465
 
-
 
1466
		version_string = "";
-
 
1467
		for (t = elf_tdata (abfd)->verref;
-
 
1468
		     t != NULL;
-
 
1469
		     t = t->vn_nextref)
-
 
1470
		  {
-
 
1471
		    Elf_Internal_Vernaux *a;
-
 
1472
 
-
 
1473
		    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
-
 
1474
		      {
-
 
1475
			if (a->vna_other == vernum)
-
 
1476
			  {
-
 
1477
			    version_string = a->vna_nodename;
-
 
1478
			    break;
-
 
1479
			  }
-
 
1480
		      }
-
 
1481
		  }
-
 
1482
	      }
-
 
1483
 
-
 
1484
	    if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
-
 
1485
	      fprintf (file, "  %-11s", version_string);
1679
	      fprintf (file, "  %-11s", version_string);
1486
	    else
1680
	    else
1487
	      {
1681
	      {
1488
		int i;
1682
		int i;
1489
 
1683
 
1490
		fprintf (file, " (%s)", version_string);
1684
		fprintf (file, " (%s)", version_string);
1491
		for (i = 10 - strlen (version_string); i > 0; --i)
1685
		for (i = 10 - strlen (version_string); i > 0; --i)
1492
		  putc (' ', file);
1686
		  putc (' ', file);
1493
	      }
1687
	      }
1494
	  }
1688
	  }
1495
 
1689
 
1496
	/* If the st_other field is not zero, print it.  */
1690
	/* If the st_other field is not zero, print it.  */
1497
	st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1691
	st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1498
 
1692
 
1499
	switch (st_other)
1693
	switch (st_other)
1500
	  {
1694
	  {
1501
	  case 0: break;
1695
	  case 0: break;
1502
	  case STV_INTERNAL:  fprintf (file, " .internal");  break;
1696
	  case STV_INTERNAL:  fprintf (file, " .internal");  break;
1503
	  case STV_HIDDEN:    fprintf (file, " .hidden");    break;
1697
	  case STV_HIDDEN:    fprintf (file, " .hidden");    break;
1504
	  case STV_PROTECTED: fprintf (file, " .protected"); break;
1698
	  case STV_PROTECTED: fprintf (file, " .protected"); break;
1505
	  default:
1699
	  default:
1506
	    /* Some other non-defined flags are also present, so print
1700
	    /* Some other non-defined flags are also present, so print
1507
	       everything hex.  */
1701
	       everything hex.  */
1508
	    fprintf (file, " 0x%02x", (unsigned int) st_other);
1702
	    fprintf (file, " 0x%02x", (unsigned int) st_other);
1509
	  }
1703
	  }
1510
 
1704
 
1511
	fprintf (file, " %s", name);
1705
	fprintf (file, " %s", name);
1512
      }
1706
      }
1513
      break;
1707
      break;
1514
    }
1708
    }
1515
}
1709
}
1516
 
-
 
1517
/* Allocate an ELF string table--force the first byte to be zero.  */
-
 
1518
 
-
 
1519
struct bfd_strtab_hash *
-
 
1520
_bfd_elf_stringtab_init (void)
-
 
1521
{
-
 
1522
  struct bfd_strtab_hash *ret;
-
 
1523
 
-
 
1524
  ret = _bfd_stringtab_init ();
-
 
1525
  if (ret != NULL)
-
 
1526
    {
-
 
1527
      bfd_size_type loc;
-
 
1528
 
-
 
1529
      loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
-
 
1530
      BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
-
 
1531
      if (loc == (bfd_size_type) -1)
-
 
1532
	{
-
 
1533
	  _bfd_stringtab_free (ret);
-
 
1534
	  ret = NULL;
-
 
1535
	}
-
 
1536
    }
-
 
1537
  return ret;
-
 
1538
}
-
 
1539

1710

1540
/* ELF .o/exec file reading */
1711
/* ELF .o/exec file reading */
1541
 
1712
 
1542
/* Create a new bfd section from an ELF section header.  */
1713
/* Create a new bfd section from an ELF section header.  */
1543
 
1714
 
1544
bfd_boolean
1715
bfd_boolean
1545
bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1716
bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
1546
{
1717
{
1547
  Elf_Internal_Shdr *hdr;
1718
  Elf_Internal_Shdr *hdr;
1548
  Elf_Internal_Ehdr *ehdr;
1719
  Elf_Internal_Ehdr *ehdr;
1549
  const struct elf_backend_data *bed;
1720
  const struct elf_backend_data *bed;
1550
  const char *name;
1721
  const char *name;
-
 
1722
  bfd_boolean ret = TRUE;
-
 
1723
  static bfd_boolean * sections_being_created = NULL;
-
 
1724
  static bfd * sections_being_created_abfd = NULL;
-
 
1725
  static unsigned int nesting = 0;
1551
 
1726
 
1552
  if (shindex >= elf_numsections (abfd))
1727
  if (shindex >= elf_numsections (abfd))
1553
    return FALSE;
1728
    return FALSE;
-
 
1729
 
-
 
1730
  if (++ nesting > 3)
-
 
1731
    {
-
 
1732
      /* PR17512: A corrupt ELF binary might contain a recursive group of
-
 
1733
	 sections, with each the string indicies pointing to the next in the
-
 
1734
	 loop.  Detect this here, by refusing to load a section that we are
-
 
1735
	 already in the process of loading.  We only trigger this test if
-
 
1736
	 we have nested at least three sections deep as normal ELF binaries
-
 
1737
	 can expect to recurse at least once.
-
 
1738
 
-
 
1739
	 FIXME: It would be better if this array was attached to the bfd,
-
 
1740
	 rather than being held in a static pointer.  */
-
 
1741
 
-
 
1742
      if (sections_being_created_abfd != abfd)
-
 
1743
	sections_being_created = NULL;
-
 
1744
      if (sections_being_created == NULL)
-
 
1745
	{
-
 
1746
	  /* FIXME: It would be more efficient to attach this array to the bfd somehow.  */
-
 
1747
	  sections_being_created = (bfd_boolean *)
-
 
1748
	    bfd_zalloc (abfd, elf_numsections (abfd) * sizeof (bfd_boolean));
-
 
1749
	  sections_being_created_abfd = abfd;
-
 
1750
	}
-
 
1751
      if (sections_being_created [shindex])
-
 
1752
	{
-
 
1753
	  (*_bfd_error_handler)
-
 
1754
	    (_("%B: warning: loop in section dependencies detected"), abfd);
-
 
1755
	  return FALSE;
-
 
1756
	}
-
 
1757
      sections_being_created [shindex] = TRUE;
-
 
1758
    }
1554
 
1759
 
1555
  hdr = elf_elfsections (abfd)[shindex];
1760
  hdr = elf_elfsections (abfd)[shindex];
1556
  ehdr = elf_elfheader (abfd);
1761
  ehdr = elf_elfheader (abfd);
1557
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1762
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
1558
					  hdr->sh_name);
1763
					  hdr->sh_name);
1559
  if (name == NULL)
1764
  if (name == NULL)
1560
    return FALSE;
1765
    goto fail;
1561
 
1766
 
1562
  bed = get_elf_backend_data (abfd);
1767
  bed = get_elf_backend_data (abfd);
1563
  switch (hdr->sh_type)
1768
  switch (hdr->sh_type)
1564
    {
1769
    {
1565
    case SHT_NULL:
1770
    case SHT_NULL:
1566
      /* Inactive section. Throw it away.  */
1771
      /* Inactive section. Throw it away.  */
1567
      return TRUE;
1772
      goto success;
1568
 
1773
 
1569
    case SHT_PROGBITS:	/* Normal section with contents.  */
1774
    case SHT_PROGBITS:		/* Normal section with contents.  */
1570
    case SHT_NOBITS:	/* .bss section.  */
1775
    case SHT_NOBITS:		/* .bss section.  */
1571
    case SHT_HASH:	/* .hash section.  */
1776
    case SHT_HASH:		/* .hash section.  */
1572
    case SHT_NOTE:	/* .note section.  */
1777
    case SHT_NOTE:		/* .note section.  */
1573
    case SHT_INIT_ARRAY:	/* .init_array section.  */
1778
    case SHT_INIT_ARRAY:	/* .init_array section.  */
1574
    case SHT_FINI_ARRAY:	/* .fini_array section.  */
1779
    case SHT_FINI_ARRAY:	/* .fini_array section.  */
1575
    case SHT_PREINIT_ARRAY:	/* .preinit_array section.  */
1780
    case SHT_PREINIT_ARRAY:	/* .preinit_array section.  */
1576
    case SHT_GNU_LIBLIST:	/* .gnu.liblist section.  */
1781
    case SHT_GNU_LIBLIST:	/* .gnu.liblist section.  */
1577
    case SHT_GNU_HASH:		/* .gnu.hash section.  */
1782
    case SHT_GNU_HASH:		/* .gnu.hash section.  */
1578
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1783
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
-
 
1784
      goto success;
1579
 
1785
 
1580
    case SHT_DYNAMIC:	/* Dynamic linking information.  */
1786
    case SHT_DYNAMIC:	/* Dynamic linking information.  */
1581
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1787
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1582
	return FALSE;
1788
	goto fail;
-
 
1789
 
1583
      if (hdr->sh_link > elf_numsections (abfd))
1790
      if (hdr->sh_link > elf_numsections (abfd))
1584
	{
1791
	{
1585
	  /* PR 10478: Accept Solaris binaries with a sh_link
1792
	  /* PR 10478: Accept Solaris binaries with a sh_link
1586
	     field set to SHN_BEFORE or SHN_AFTER.  */
1793
	     field set to SHN_BEFORE or SHN_AFTER.  */
1587
	  switch (bfd_get_arch (abfd))
1794
	  switch (bfd_get_arch (abfd))
1588
	    {
1795
	    {
1589
	    case bfd_arch_i386:
1796
	    case bfd_arch_i386:
1590
	    case bfd_arch_sparc:
1797
	    case bfd_arch_sparc:
1591
	      if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1798
	      if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
1592
		  || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1799
		  || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
1593
		break;
1800
		break;
1594
	      /* Otherwise fall through.  */
1801
	      /* Otherwise fall through.  */
1595
	    default:
1802
	    default:
1596
	      return FALSE;
1803
	      goto fail;
1597
	    }
1804
	    }
1598
	}
1805
	}
1599
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1806
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
1600
	return FALSE;
1807
	goto fail;
1601
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1808
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1602
	{
1809
	{
1603
	  Elf_Internal_Shdr *dynsymhdr;
1810
	  Elf_Internal_Shdr *dynsymhdr;
1604
 
1811
 
1605
	  /* The shared libraries distributed with hpux11 have a bogus
1812
	  /* The shared libraries distributed with hpux11 have a bogus
1606
	     sh_link field for the ".dynamic" section.  Find the
1813
	     sh_link field for the ".dynamic" section.  Find the
1607
	     string table for the ".dynsym" section instead.  */
1814
	     string table for the ".dynsym" section instead.  */
1608
	  if (elf_dynsymtab (abfd) != 0)
1815
	  if (elf_dynsymtab (abfd) != 0)
1609
	    {
1816
	    {
1610
	      dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1817
	      dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1611
	      hdr->sh_link = dynsymhdr->sh_link;
1818
	      hdr->sh_link = dynsymhdr->sh_link;
1612
	    }
1819
	    }
1613
	  else
1820
	  else
1614
	    {
1821
	    {
1615
	      unsigned int i, num_sec;
1822
	      unsigned int i, num_sec;
1616
 
1823
 
1617
	      num_sec = elf_numsections (abfd);
1824
	      num_sec = elf_numsections (abfd);
1618
	      for (i = 1; i < num_sec; i++)
1825
	      for (i = 1; i < num_sec; i++)
1619
		{
1826
		{
1620
		  dynsymhdr = elf_elfsections (abfd)[i];
1827
		  dynsymhdr = elf_elfsections (abfd)[i];
1621
		  if (dynsymhdr->sh_type == SHT_DYNSYM)
1828
		  if (dynsymhdr->sh_type == SHT_DYNSYM)
1622
		    {
1829
		    {
1623
		      hdr->sh_link = dynsymhdr->sh_link;
1830
		      hdr->sh_link = dynsymhdr->sh_link;
1624
		      break;
1831
		      break;
1625
		    }
1832
		    }
1626
		}
1833
		}
1627
	    }
1834
	    }
1628
	}
1835
	}
1629
      break;
1836
      goto success;
1630
 
1837
 
1631
    case SHT_SYMTAB:		/* A symbol table */
1838
    case SHT_SYMTAB:		/* A symbol table.  */
1632
      if (elf_onesymtab (abfd) == shindex)
1839
      if (elf_onesymtab (abfd) == shindex)
1633
	return TRUE;
1840
	goto success;
1634
 
1841
 
-
 
1842
      if (hdr->sh_entsize != bed->s->sizeof_sym)
1635
      if (hdr->sh_entsize != bed->s->sizeof_sym)
1843
	goto fail;
1636
	return FALSE;
1844
 
1637
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1845
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1638
	{
1846
	{
1639
	  if (hdr->sh_size != 0)
1847
	  if (hdr->sh_size != 0)
1640
	    return FALSE;
1848
	    goto fail;
1641
	  /* Some assemblers erroneously set sh_info to one with a
1849
	  /* Some assemblers erroneously set sh_info to one with a
1642
	     zero sh_size.  ld sees this as a global symbol count
1850
	     zero sh_size.  ld sees this as a global symbol count
1643
	     of (unsigned) -1.  Fix it here.  */
1851
	     of (unsigned) -1.  Fix it here.  */
1644
	  hdr->sh_info = 0;
1852
	  hdr->sh_info = 0;
1645
	  return TRUE;
1853
	  goto success;
-
 
1854
	}
-
 
1855
 
-
 
1856
      /* PR 18854: A binary might contain more than one symbol table.
-
 
1857
	 Unusual, but possible.  Warn, but continue.  */
-
 
1858
      if (elf_onesymtab (abfd) != 0)
-
 
1859
	{
-
 
1860
	  (*_bfd_error_handler)
-
 
1861
	    (_("%B: warning: multiple symbol tables detected - ignoring the table in section %u"),
-
 
1862
	     abfd, shindex);
-
 
1863
	  goto success;
1646
	}
1864
	}
1647
      BFD_ASSERT (elf_onesymtab (abfd) == 0);
-
 
1648
      elf_onesymtab (abfd) = shindex;
1865
      elf_onesymtab (abfd) = shindex;
1649
      elf_tdata (abfd)->symtab_hdr = *hdr;
1866
      elf_symtab_hdr (abfd) = *hdr;
1650
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1867
      elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
1651
      abfd->flags |= HAS_SYMS;
1868
      abfd->flags |= HAS_SYMS;
1652
 
1869
 
1653
      /* Sometimes a shared object will map in the symbol table.  If
1870
      /* Sometimes a shared object will map in the symbol table.  If
1654
	 SHF_ALLOC is set, and this is a shared object, then we also
1871
	 SHF_ALLOC is set, and this is a shared object, then we also
1655
	 treat this section as a BFD section.  We can not base the
1872
	 treat this section as a BFD section.  We can not base the
1656
	 decision purely on SHF_ALLOC, because that flag is sometimes
1873
	 decision purely on SHF_ALLOC, because that flag is sometimes
1657
	 set in a relocatable object file, which would confuse the
1874
	 set in a relocatable object file, which would confuse the
1658
	 linker.  */
1875
	 linker.  */
1659
      if ((hdr->sh_flags & SHF_ALLOC) != 0
1876
      if ((hdr->sh_flags & SHF_ALLOC) != 0
1660
	  && (abfd->flags & DYNAMIC) != 0
1877
	  && (abfd->flags & DYNAMIC) != 0
1661
	  && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1878
	  && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1662
						shindex))
1879
						shindex))
1663
	return FALSE;
1880
	goto fail;
1664
 
1881
 
1665
      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1882
      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
1666
	 can't read symbols without that section loaded as well.  It
1883
	 can't read symbols without that section loaded as well.  It
1667
	 is most likely specified by the next section header.  */
1884
	 is most likely specified by the next section header.  */
1668
      if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex)
-
 
1669
	{
1885
      {
-
 
1886
	elf_section_list * entry;
1670
	  unsigned int i, num_sec;
1887
	unsigned int i, num_sec;
-
 
1888
 
-
 
1889
	for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
-
 
1890
	  if (entry->hdr.sh_link == shindex)
-
 
1891
	    goto success;
1671
 
1892
 
1672
	  num_sec = elf_numsections (abfd);
1893
	num_sec = elf_numsections (abfd);
1673
	  for (i = shindex + 1; i < num_sec; i++)
1894
	for (i = shindex + 1; i < num_sec; i++)
1674
	    {
1895
	  {
1675
	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1896
	    Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
-
 
1897
 
1676
	      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1898
	    if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1677
		  && hdr2->sh_link == shindex)
1899
		&& hdr2->sh_link == shindex)
1678
		break;
1900
	      break;
1679
	    }
1901
	  }
-
 
1902
 
1680
	  if (i == num_sec)
1903
	if (i == num_sec)
1681
	    for (i = 1; i < shindex; i++)
1904
	  for (i = 1; i < shindex; i++)
1682
	      {
1905
	    {
1683
		Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1906
	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
-
 
1907
 
1684
		if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1908
	      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
1685
		    && hdr2->sh_link == shindex)
1909
		  && hdr2->sh_link == shindex)
1686
		  break;
1910
		break;
1687
	      }
1911
	    }
-
 
1912
 
1688
	  if (i != shindex)
1913
	if (i != shindex)
1689
	    return bfd_section_from_shdr (abfd, i);
1914
	  ret = bfd_section_from_shdr (abfd, i);
-
 
1915
	/* else FIXME: we have failed to find the symbol table - should we issue an error ? */
-
 
1916
	goto success;
1690
	}
1917
      }
1691
      return TRUE;
-
 
1692
 
1918
 
1693
    case SHT_DYNSYM:		/* A dynamic symbol table */
1919
    case SHT_DYNSYM:		/* A dynamic symbol table.  */
1694
      if (elf_dynsymtab (abfd) == shindex)
1920
      if (elf_dynsymtab (abfd) == shindex)
1695
	return TRUE;
1921
	goto success;
1696
 
1922
 
-
 
1923
      if (hdr->sh_entsize != bed->s->sizeof_sym)
1697
      if (hdr->sh_entsize != bed->s->sizeof_sym)
1924
	goto fail;
1698
	return FALSE;
1925
 
1699
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1926
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
1700
	{
1927
	{
1701
	  if (hdr->sh_size != 0)
1928
	  if (hdr->sh_size != 0)
1702
	    return FALSE;
1929
	    goto fail;
-
 
1930
 
1703
	  /* Some linkers erroneously set sh_info to one with a
1931
	  /* Some linkers erroneously set sh_info to one with a
1704
	     zero sh_size.  ld sees this as a global symbol count
1932
	     zero sh_size.  ld sees this as a global symbol count
1705
	     of (unsigned) -1.  Fix it here.  */
1933
	     of (unsigned) -1.  Fix it here.  */
1706
	  hdr->sh_info = 0;
1934
	  hdr->sh_info = 0;
1707
	  return TRUE;
1935
	  goto success;
-
 
1936
	}
-
 
1937
 
-
 
1938
      /* PR 18854: A binary might contain more than one dynamic symbol table.
-
 
1939
	 Unusual, but possible.  Warn, but continue.  */
-
 
1940
      if (elf_dynsymtab (abfd) != 0)
-
 
1941
	{
-
 
1942
	  (*_bfd_error_handler)
-
 
1943
	    (_("%B: warning: multiple dynamic symbol tables detected - ignoring the table in section %u"),
-
 
1944
	     abfd, shindex);
-
 
1945
	  goto success;
1708
	}
1946
	}
1709
      BFD_ASSERT (elf_dynsymtab (abfd) == 0);
-
 
1710
      elf_dynsymtab (abfd) = shindex;
1947
      elf_dynsymtab (abfd) = shindex;
1711
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1948
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1712
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1949
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1713
      abfd->flags |= HAS_SYMS;
1950
      abfd->flags |= HAS_SYMS;
1714
 
1951
 
1715
      /* Besides being a symbol table, we also treat this as a regular
1952
      /* Besides being a symbol table, we also treat this as a regular
1716
	 section, so that objcopy can handle it.  */
1953
	 section, so that objcopy can handle it.  */
1717
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
1954
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
-
 
1955
      goto success;
1718
 
1956
 
1719
    case SHT_SYMTAB_SHNDX:	/* Symbol section indices when >64k sections */
1957
    case SHT_SYMTAB_SHNDX:	/* Symbol section indices when >64k sections.  */
1720
      if (elf_symtab_shndx (abfd) == shindex)
1958
      {
1721
	return TRUE;
1959
	elf_section_list * entry;
1722
 
1960
 
-
 
1961
	for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next)
-
 
1962
	  if (entry->ndx == shindex)
-
 
1963
	    goto success;
-
 
1964
	
-
 
1965
	entry = bfd_alloc (abfd, sizeof * entry);
-
 
1966
	if (entry == NULL)
-
 
1967
	  goto fail;
1723
      BFD_ASSERT (elf_symtab_shndx (abfd) == 0);
1968
	entry->ndx = shindex;
-
 
1969
	entry->hdr = * hdr;
1724
      elf_symtab_shndx (abfd) = shindex;
1970
	entry->next = elf_symtab_shndx_list (abfd);
-
 
1971
	elf_symtab_shndx_list (abfd) = entry;
1725
      elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1972
	elf_elfsections (abfd)[shindex] = & entry->hdr;
1726
      elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1973
	goto success;
1727
      return TRUE;
1974
      }
1728
 
1975
 
-
 
1976
    case SHT_STRTAB:		/* A string table.  */
1729
    case SHT_STRTAB:		/* A string table */
1977
      if (hdr->bfd_section != NULL)
1730
      if (hdr->bfd_section != NULL)
1978
	goto success;
1731
	return TRUE;
1979
 
1732
      if (ehdr->e_shstrndx == shindex)
1980
      if (ehdr->e_shstrndx == shindex)
1733
	{
1981
	{
1734
	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
1982
	  elf_tdata (abfd)->shstrtab_hdr = *hdr;
1735
	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1983
	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1736
	  return TRUE;
1984
	  goto success;
1737
	}
1985
	}
-
 
1986
 
1738
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1987
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
1739
	{
1988
	{
1740
	symtab_strtab:
1989
	symtab_strtab:
1741
	  elf_tdata (abfd)->strtab_hdr = *hdr;
1990
	  elf_tdata (abfd)->strtab_hdr = *hdr;
1742
	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1991
	  elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
1743
	  return TRUE;
1992
	  goto success;
1744
	}
1993
	}
-
 
1994
 
1745
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1995
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
1746
	{
1996
	{
1747
	dynsymtab_strtab:
1997
	dynsymtab_strtab:
1748
	  elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1998
	  elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1749
	  hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1999
	  hdr = &elf_tdata (abfd)->dynstrtab_hdr;
1750
	  elf_elfsections (abfd)[shindex] = hdr;
2000
	  elf_elfsections (abfd)[shindex] = hdr;
1751
	  /* We also treat this as a regular section, so that objcopy
2001
	  /* We also treat this as a regular section, so that objcopy
1752
	     can handle it.  */
2002
	     can handle it.  */
1753
	  return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2003
	  ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1754
						  shindex);
2004
						 shindex);
-
 
2005
	  goto success;
1755
	}
2006
	}
1756
 
2007
 
1757
      /* If the string table isn't one of the above, then treat it as a
2008
      /* If the string table isn't one of the above, then treat it as a
1758
	 regular section.  We need to scan all the headers to be sure,
2009
	 regular section.  We need to scan all the headers to be sure,
1759
	 just in case this strtab section appeared before the above.  */
2010
	 just in case this strtab section appeared before the above.  */
1760
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2011
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
1761
	{
2012
	{
1762
	  unsigned int i, num_sec;
2013
	  unsigned int i, num_sec;
1763
 
2014
 
1764
	  num_sec = elf_numsections (abfd);
2015
	  num_sec = elf_numsections (abfd);
1765
	  for (i = 1; i < num_sec; i++)
2016
	  for (i = 1; i < num_sec; i++)
1766
	    {
2017
	    {
1767
	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2018
	      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1768
	      if (hdr2->sh_link == shindex)
2019
	      if (hdr2->sh_link == shindex)
1769
		{
2020
		{
1770
		  /* Prevent endless recursion on broken objects.  */
2021
		  /* Prevent endless recursion on broken objects.  */
1771
		  if (i == shindex)
2022
		  if (i == shindex)
1772
		    return FALSE;
2023
		    goto fail;
1773
		  if (! bfd_section_from_shdr (abfd, i))
2024
		  if (! bfd_section_from_shdr (abfd, i))
1774
		    return FALSE;
2025
		    goto fail;
1775
		  if (elf_onesymtab (abfd) == i)
2026
		  if (elf_onesymtab (abfd) == i)
1776
		    goto symtab_strtab;
2027
		    goto symtab_strtab;
1777
		  if (elf_dynsymtab (abfd) == i)
2028
		  if (elf_dynsymtab (abfd) == i)
1778
		    goto dynsymtab_strtab;
2029
		    goto dynsymtab_strtab;
1779
		}
2030
		}
1780
	    }
2031
	    }
1781
	}
2032
	}
1782
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2033
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
-
 
2034
      goto success;
1783
 
2035
 
1784
    case SHT_REL:
2036
    case SHT_REL:
1785
    case SHT_RELA:
2037
    case SHT_RELA:
1786
      /* *These* do a lot of work -- but build no sections!  */
2038
      /* *These* do a lot of work -- but build no sections!  */
1787
      {
2039
      {
1788
	asection *target_sect;
2040
	asection *target_sect;
1789
	Elf_Internal_Shdr *hdr2, **p_hdr;
2041
	Elf_Internal_Shdr *hdr2, **p_hdr;
1790
	unsigned int num_sec = elf_numsections (abfd);
2042
	unsigned int num_sec = elf_numsections (abfd);
1791
	struct bfd_elf_section_data *esdt;
2043
	struct bfd_elf_section_data *esdt;
1792
	bfd_size_type amt;
2044
	bfd_size_type amt;
1793
 
2045
 
1794
	if (hdr->sh_entsize
2046
	if (hdr->sh_entsize
1795
	    != (bfd_size_type) (hdr->sh_type == SHT_REL
2047
	    != (bfd_size_type) (hdr->sh_type == SHT_REL
1796
				? bed->s->sizeof_rel : bed->s->sizeof_rela))
2048
				? bed->s->sizeof_rel : bed->s->sizeof_rela))
1797
	  return FALSE;
2049
	  goto fail;
1798
 
2050
 
1799
	/* Check for a bogus link to avoid crashing.  */
2051
	/* Check for a bogus link to avoid crashing.  */
1800
	if (hdr->sh_link >= num_sec)
2052
	if (hdr->sh_link >= num_sec)
1801
	  {
2053
	  {
1802
	    ((*_bfd_error_handler)
2054
	    ((*_bfd_error_handler)
1803
	     (_("%B: invalid link %lu for reloc section %s (index %u)"),
2055
	     (_("%B: invalid link %lu for reloc section %s (index %u)"),
1804
	      abfd, hdr->sh_link, name, shindex));
2056
	      abfd, hdr->sh_link, name, shindex));
1805
	    return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2057
	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1806
						    shindex);
2058
						   shindex);
-
 
2059
	    goto success;
1807
	  }
2060
	  }
1808
 
2061
 
1809
	/* For some incomprehensible reason Oracle distributes
2062
	/* For some incomprehensible reason Oracle distributes
1810
	   libraries for Solaris in which some of the objects have
2063
	   libraries for Solaris in which some of the objects have
1811
	   bogus sh_link fields.  It would be nice if we could just
2064
	   bogus sh_link fields.  It would be nice if we could just
1812
	   reject them, but, unfortunately, some people need to use
2065
	   reject them, but, unfortunately, some people need to use
1813
	   them.  We scan through the section headers; if we find only
2066
	   them.  We scan through the section headers; if we find only
1814
	   one suitable symbol table, we clobber the sh_link to point
2067
	   one suitable symbol table, we clobber the sh_link to point
1815
	   to it.  I hope this doesn't break anything.
2068
	   to it.  I hope this doesn't break anything.
1816
 
2069
 
1817
	   Don't do it on executable nor shared library.  */
2070
	   Don't do it on executable nor shared library.  */
1818
	if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
2071
	if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0
1819
	    && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
2072
	    && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1820
	    && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
2073
	    && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1821
	  {
2074
	  {
1822
	    unsigned int scan;
2075
	    unsigned int scan;
1823
	    int found;
2076
	    int found;
1824
 
2077
 
1825
	    found = 0;
2078
	    found = 0;
1826
	    for (scan = 1; scan < num_sec; scan++)
2079
	    for (scan = 1; scan < num_sec; scan++)
1827
	      {
2080
	      {
1828
		if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
2081
		if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1829
		    || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
2082
		    || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1830
		  {
2083
		  {
1831
		    if (found != 0)
2084
		    if (found != 0)
1832
		      {
2085
		      {
1833
			found = 0;
2086
			found = 0;
1834
			break;
2087
			break;
1835
		      }
2088
		      }
1836
		    found = scan;
2089
		    found = scan;
1837
		  }
2090
		  }
1838
	      }
2091
	      }
1839
	    if (found != 0)
2092
	    if (found != 0)
1840
	      hdr->sh_link = found;
2093
	      hdr->sh_link = found;
1841
	  }
2094
	  }
1842
 
2095
 
1843
	/* Get the symbol table.  */
2096
	/* Get the symbol table.  */
1844
	if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2097
	if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1845
	     || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2098
	     || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
1846
	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2099
	    && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1847
	  return FALSE;
2100
	  goto fail;
1848
 
2101
 
1849
	/* If this reloc section does not use the main symbol table we
2102
	/* If this reloc section does not use the main symbol table we
1850
	   don't treat it as a reloc section.  BFD can't adequately
2103
	   don't treat it as a reloc section.  BFD can't adequately
1851
	   represent such a section, so at least for now, we don't
2104
	   represent such a section, so at least for now, we don't
1852
	   try.  We just present it as a normal section.  We also
2105
	   try.  We just present it as a normal section.  We also
1853
	   can't use it as a reloc section if it points to the null
2106
	   can't use it as a reloc section if it points to the null
1854
	   section, an invalid section, another reloc section, or its
2107
	   section, an invalid section, another reloc section, or its
1855
	   sh_link points to the null section.  */
2108
	   sh_link points to the null section.  */
1856
	if (hdr->sh_link != elf_onesymtab (abfd)
2109
	if (hdr->sh_link != elf_onesymtab (abfd)
1857
	    || hdr->sh_link == SHN_UNDEF
2110
	    || hdr->sh_link == SHN_UNDEF
1858
	    || hdr->sh_info == SHN_UNDEF
2111
	    || hdr->sh_info == SHN_UNDEF
1859
	    || hdr->sh_info >= num_sec
2112
	    || hdr->sh_info >= num_sec
1860
	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2113
	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
1861
	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2114
	    || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
-
 
2115
	  {
1862
	  return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2116
	    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1863
						  shindex);
2117
						   shindex);
-
 
2118
	    goto success;
-
 
2119
	  }
1864
 
2120
 
1865
	if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2121
	if (! bfd_section_from_shdr (abfd, hdr->sh_info))
-
 
2122
	  goto fail;
1866
	  return FALSE;
2123
 
1867
	target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2124
	target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1868
	if (target_sect == NULL)
2125
	if (target_sect == NULL)
1869
	  return FALSE;
2126
	  goto fail;
1870
 
2127
 
1871
	esdt = elf_section_data (target_sect);
2128
	esdt = elf_section_data (target_sect);
1872
	if (hdr->sh_type == SHT_RELA)
2129
	if (hdr->sh_type == SHT_RELA)
1873
	  p_hdr = &esdt->rela.hdr;
2130
	  p_hdr = &esdt->rela.hdr;
1874
	else
2131
	else
1875
	  p_hdr = &esdt->rel.hdr;
2132
	  p_hdr = &esdt->rel.hdr;
-
 
2133
 
1876
 
2134
	/* PR 17512: file: 0b4f81b7.  */
-
 
2135
	if (*p_hdr != NULL)
1877
	BFD_ASSERT (*p_hdr == NULL);
2136
	  goto fail;
1878
	amt = sizeof (*hdr2);
2137
	amt = sizeof (*hdr2);
1879
	hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
2138
	hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1880
	if (hdr2 == NULL)
2139
	if (hdr2 == NULL)
1881
	  return FALSE;
2140
	  goto fail;
1882
	*hdr2 = *hdr;
2141
	*hdr2 = *hdr;
1883
	*p_hdr = hdr2;
2142
	*p_hdr = hdr2;
1884
	elf_elfsections (abfd)[shindex] = hdr2;
2143
	elf_elfsections (abfd)[shindex] = hdr2;
1885
	target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
2144
	target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1886
	target_sect->flags |= SEC_RELOC;
2145
	target_sect->flags |= SEC_RELOC;
1887
	target_sect->relocation = NULL;
2146
	target_sect->relocation = NULL;
1888
	target_sect->rel_filepos = hdr->sh_offset;
2147
	target_sect->rel_filepos = hdr->sh_offset;
1889
	/* In the section to which the relocations apply, mark whether
2148
	/* In the section to which the relocations apply, mark whether
1890
	   its relocations are of the REL or RELA variety.  */
2149
	   its relocations are of the REL or RELA variety.  */
1891
	if (hdr->sh_size != 0)
2150
	if (hdr->sh_size != 0)
1892
	  {
2151
	  {
1893
	    if (hdr->sh_type == SHT_RELA)
2152
	    if (hdr->sh_type == SHT_RELA)
1894
	      target_sect->use_rela_p = 1;
2153
	      target_sect->use_rela_p = 1;
1895
	  }
2154
	  }
1896
	abfd->flags |= HAS_RELOC;
2155
	abfd->flags |= HAS_RELOC;
1897
	return TRUE;
2156
	goto success;
1898
      }
2157
      }
1899
 
2158
 
1900
    case SHT_GNU_verdef:
2159
    case SHT_GNU_verdef:
1901
      elf_dynverdef (abfd) = shindex;
2160
      elf_dynverdef (abfd) = shindex;
1902
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
2161
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
1903
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2162
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
-
 
2163
      goto success;
1904
 
2164
 
1905
    case SHT_GNU_versym:
2165
    case SHT_GNU_versym:
1906
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2166
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
1907
	return FALSE;
2167
	goto fail;
-
 
2168
 
1908
      elf_dynversym (abfd) = shindex;
2169
      elf_dynversym (abfd) = shindex;
1909
      elf_tdata (abfd)->dynversym_hdr = *hdr;
2170
      elf_tdata (abfd)->dynversym_hdr = *hdr;
1910
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2171
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
-
 
2172
      goto success;
1911
 
2173
 
1912
    case SHT_GNU_verneed:
2174
    case SHT_GNU_verneed:
1913
      elf_dynverref (abfd) = shindex;
2175
      elf_dynverref (abfd) = shindex;
1914
      elf_tdata (abfd)->dynverref_hdr = *hdr;
2176
      elf_tdata (abfd)->dynverref_hdr = *hdr;
1915
      return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2177
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
-
 
2178
      goto success;
1916
 
2179
 
1917
    case SHT_SHLIB:
2180
    case SHT_SHLIB:
1918
      return TRUE;
2181
      goto success;
1919
 
2182
 
1920
    case SHT_GROUP:
2183
    case SHT_GROUP:
1921
      if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
2184
      if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE))
1922
	return FALSE;
2185
	goto fail;
-
 
2186
 
1923
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2187
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1924
	return FALSE;
2188
	goto fail;
-
 
2189
 
1925
      if (hdr->contents != NULL)
2190
      if (hdr->contents != NULL)
1926
	{
2191
	{
1927
	  Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2192
	  Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1928
	  unsigned int n_elt = hdr->sh_size / GRP_ENTRY_SIZE;
2193
	  unsigned int n_elt = hdr->sh_size / sizeof (* idx);
1929
	  asection *s;
2194
	  asection *s;
-
 
2195
 
-
 
2196
	  if (n_elt == 0)
1930
 
2197
	    goto fail;
1931
	  if (idx->flags & GRP_COMDAT)
2198
	  if (idx->flags & GRP_COMDAT)
1932
	    hdr->bfd_section->flags
2199
	    hdr->bfd_section->flags
1933
	      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2200
	      |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1934
 
2201
 
1935
	  /* We try to keep the same section order as it comes in.  */
2202
	  /* We try to keep the same section order as it comes in.  */
1936
	  idx += n_elt;
2203
	  idx += n_elt;
-
 
2204
 
1937
	  while (--n_elt != 0)
2205
	  while (--n_elt != 0)
1938
	    {
2206
	    {
1939
	      --idx;
2207
	      --idx;
1940
 
2208
 
1941
	      if (idx->shdr != NULL
2209
	      if (idx->shdr != NULL
1942
		  && (s = idx->shdr->bfd_section) != NULL
2210
		  && (s = idx->shdr->bfd_section) != NULL
1943
		  && elf_next_in_group (s) != NULL)
2211
		  && elf_next_in_group (s) != NULL)
1944
		{
2212
		{
1945
		  elf_next_in_group (hdr->bfd_section) = s;
2213
		  elf_next_in_group (hdr->bfd_section) = s;
1946
		  break;
2214
		  break;
1947
		}
2215
		}
1948
	    }
2216
	    }
1949
	}
2217
	}
1950
      break;
2218
      goto success;
1951
 
2219
 
1952
    default:
2220
    default:
1953
      /* Possibly an attributes section.  */
2221
      /* Possibly an attributes section.  */
1954
      if (hdr->sh_type == SHT_GNU_ATTRIBUTES
2222
      if (hdr->sh_type == SHT_GNU_ATTRIBUTES
1955
	  || hdr->sh_type == bed->obj_attrs_section_type)
2223
	  || hdr->sh_type == bed->obj_attrs_section_type)
1956
	{
2224
	{
1957
	  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2225
	  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1958
	    return FALSE;
2226
	    goto fail;
1959
	  _bfd_elf_parse_attributes (abfd, hdr);
2227
	  _bfd_elf_parse_attributes (abfd, hdr);
1960
	  return TRUE;
2228
	  goto success;
1961
	}
2229
	}
1962
 
2230
 
1963
      /* Check for any processor-specific section types.  */
2231
      /* Check for any processor-specific section types.  */
1964
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2232
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
1965
	return TRUE;
2233
	goto success;
1966
 
2234
 
1967
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2235
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
1968
	{
2236
	{
1969
	  if ((hdr->sh_flags & SHF_ALLOC) != 0)
2237
	  if ((hdr->sh_flags & SHF_ALLOC) != 0)
1970
	    /* FIXME: How to properly handle allocated section reserved
2238
	    /* FIXME: How to properly handle allocated section reserved
1971
	       for applications?  */
2239
	       for applications?  */
1972
	    (*_bfd_error_handler)
2240
	    (*_bfd_error_handler)
1973
	      (_("%B: don't know how to handle allocated, application "
2241
	      (_("%B: don't know how to handle allocated, application "
1974
		 "specific section `%s' [0x%8x]"),
2242
		 "specific section `%s' [0x%8x]"),
1975
	       abfd, name, hdr->sh_type);
2243
	       abfd, name, hdr->sh_type);
1976
	  else
2244
	  else
-
 
2245
	    {
1977
	    /* Allow sections reserved for applications.  */
2246
	      /* Allow sections reserved for applications.  */
1978
	    return _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2247
	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
1979
						    shindex);
2248
						     shindex);
-
 
2249
	      goto success;
-
 
2250
	    }
1980
	}
2251
	}
1981
      else if (hdr->sh_type >= SHT_LOPROC
2252
      else if (hdr->sh_type >= SHT_LOPROC
1982
	       && hdr->sh_type <= SHT_HIPROC)
2253
	       && hdr->sh_type <= SHT_HIPROC)
1983
	/* FIXME: We should handle this section.  */
2254
	/* FIXME: We should handle this section.  */
1984
	(*_bfd_error_handler)
2255
	(*_bfd_error_handler)
1985
	  (_("%B: don't know how to handle processor specific section "
2256
	  (_("%B: don't know how to handle processor specific section "
1986
	     "`%s' [0x%8x]"),
2257
	     "`%s' [0x%8x]"),
1987
	   abfd, name, hdr->sh_type);
2258
	   abfd, name, hdr->sh_type);
1988
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2259
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
1989
	{
2260
	{
1990
	  /* Unrecognised OS-specific sections.  */
2261
	  /* Unrecognised OS-specific sections.  */
1991
	  if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2262
	  if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
1992
	    /* SHF_OS_NONCONFORMING indicates that special knowledge is
2263
	    /* SHF_OS_NONCONFORMING indicates that special knowledge is
1993
	       required to correctly process the section and the file should
2264
	       required to correctly process the section and the file should
1994
	       be rejected with an error message.  */
2265
	       be rejected with an error message.  */
1995
	    (*_bfd_error_handler)
2266
	    (*_bfd_error_handler)
1996
	      (_("%B: don't know how to handle OS specific section "
2267
	      (_("%B: don't know how to handle OS specific section "
1997
		 "`%s' [0x%8x]"),
2268
		 "`%s' [0x%8x]"),
1998
	       abfd, name, hdr->sh_type);
2269
	       abfd, name, hdr->sh_type);
1999
	  else
2270
	  else
-
 
2271
	    {
2000
	    /* Otherwise it should be processed.  */
2272
	      /* Otherwise it should be processed.  */
2001
	    return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2273
	      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
-
 
2274
	      goto success;
-
 
2275
	    }
2002
	}
2276
	}
2003
      else
2277
      else
2004
	/* FIXME: We should handle this section.  */
2278
	/* FIXME: We should handle this section.  */
2005
	(*_bfd_error_handler)
2279
	(*_bfd_error_handler)
2006
	  (_("%B: don't know how to handle section `%s' [0x%8x]"),
2280
	  (_("%B: don't know how to handle section `%s' [0x%8x]"),
2007
	   abfd, name, hdr->sh_type);
2281
	   abfd, name, hdr->sh_type);
2008
 
2282
 
2009
      return FALSE;
2283
      goto fail;
-
 
2284
    }
-
 
2285
 
-
 
2286
 fail:
-
 
2287
  ret = FALSE;
-
 
2288
 success:
-
 
2289
  if (sections_being_created && sections_being_created_abfd == abfd)
-
 
2290
    sections_being_created [shindex] = FALSE;
-
 
2291
  if (-- nesting == 0)
-
 
2292
    {
-
 
2293
      sections_being_created = NULL;
2010
    }
2294
      sections_being_created_abfd = abfd;
2011
 
2295
    }
2012
  return TRUE;
2296
  return ret;
2013
}
2297
}
2014
 
2298
 
2015
/* Return the local symbol specified by ABFD, R_SYMNDX.  */
2299
/* Return the local symbol specified by ABFD, R_SYMNDX.  */
2016
 
2300
 
2017
Elf_Internal_Sym *
2301
Elf_Internal_Sym *
2018
bfd_sym_from_r_symndx (struct sym_cache *cache,
2302
bfd_sym_from_r_symndx (struct sym_cache *cache,
2019
		       bfd *abfd,
2303
		       bfd *abfd,
2020
		       unsigned long r_symndx)
2304
		       unsigned long r_symndx)
2021
{
2305
{
2022
  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2306
  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2023
 
2307
 
2024
  if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2308
  if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
2025
    {
2309
    {
2026
      Elf_Internal_Shdr *symtab_hdr;
2310
      Elf_Internal_Shdr *symtab_hdr;
2027
      unsigned char esym[sizeof (Elf64_External_Sym)];
2311
      unsigned char esym[sizeof (Elf64_External_Sym)];
2028
      Elf_External_Sym_Shndx eshndx;
2312
      Elf_External_Sym_Shndx eshndx;
2029
 
2313
 
2030
      symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2314
      symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2031
      if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2315
      if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2032
				&cache->sym[ent], esym, &eshndx) == NULL)
2316
				&cache->sym[ent], esym, &eshndx) == NULL)
2033
	return NULL;
2317
	return NULL;
2034
 
2318
 
2035
      if (cache->abfd != abfd)
2319
      if (cache->abfd != abfd)
2036
	{
2320
	{
2037
	  memset (cache->indx, -1, sizeof (cache->indx));
2321
	  memset (cache->indx, -1, sizeof (cache->indx));
2038
	  cache->abfd = abfd;
2322
	  cache->abfd = abfd;
2039
	}
2323
	}
2040
      cache->indx[ent] = r_symndx;
2324
      cache->indx[ent] = r_symndx;
2041
    }
2325
    }
2042
 
2326
 
2043
  return &cache->sym[ent];
2327
  return &cache->sym[ent];
2044
}
2328
}
2045
 
2329
 
2046
/* Given an ELF section number, retrieve the corresponding BFD
2330
/* Given an ELF section number, retrieve the corresponding BFD
2047
   section.  */
2331
   section.  */
2048
 
2332
 
2049
asection *
2333
asection *
2050
bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2334
bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
2051
{
2335
{
2052
  if (sec_index >= elf_numsections (abfd))
2336
  if (sec_index >= elf_numsections (abfd))
2053
    return NULL;
2337
    return NULL;
2054
  return elf_elfsections (abfd)[sec_index]->bfd_section;
2338
  return elf_elfsections (abfd)[sec_index]->bfd_section;
2055
}
2339
}
2056
 
2340
 
2057
static const struct bfd_elf_special_section special_sections_b[] =
2341
static const struct bfd_elf_special_section special_sections_b[] =
2058
{
2342
{
2059
  { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2343
  { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
2060
  { NULL,                   0,  0, 0,            0 }
2344
  { NULL,                   0,  0, 0,            0 }
2061
};
2345
};
2062
 
2346
 
2063
static const struct bfd_elf_special_section special_sections_c[] =
2347
static const struct bfd_elf_special_section special_sections_c[] =
2064
{
2348
{
2065
  { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2349
  { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
2066
  { NULL,                       0, 0, 0,            0 }
2350
  { NULL,                       0, 0, 0,            0 }
2067
};
2351
};
2068
 
2352
 
2069
static const struct bfd_elf_special_section special_sections_d[] =
2353
static const struct bfd_elf_special_section special_sections_d[] =
2070
{
2354
{
2071
  { STRING_COMMA_LEN (".data"),         -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2355
  { STRING_COMMA_LEN (".data"),         -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2072
  { STRING_COMMA_LEN (".data1"),         0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2356
  { STRING_COMMA_LEN (".data1"),         0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2073
  /* There are more DWARF sections than these, but they needn't be added here
2357
  /* There are more DWARF sections than these, but they needn't be added here
2074
     unless you have to cope with broken compilers that don't emit section
2358
     unless you have to cope with broken compilers that don't emit section
2075
     attributes or you want to help the user writing assembler.  */
2359
     attributes or you want to help the user writing assembler.  */
2076
  { STRING_COMMA_LEN (".debug"),         0, SHT_PROGBITS, 0 },
2360
  { STRING_COMMA_LEN (".debug"),         0, SHT_PROGBITS, 0 },
2077
  { STRING_COMMA_LEN (".debug_line"),    0, SHT_PROGBITS, 0 },
2361
  { STRING_COMMA_LEN (".debug_line"),    0, SHT_PROGBITS, 0 },
2078
  { STRING_COMMA_LEN (".debug_info"),    0, SHT_PROGBITS, 0 },
2362
  { STRING_COMMA_LEN (".debug_info"),    0, SHT_PROGBITS, 0 },
2079
  { STRING_COMMA_LEN (".debug_abbrev"),  0, SHT_PROGBITS, 0 },
2363
  { STRING_COMMA_LEN (".debug_abbrev"),  0, SHT_PROGBITS, 0 },
2080
  { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2364
  { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
2081
  { STRING_COMMA_LEN (".dynamic"),       0, SHT_DYNAMIC,  SHF_ALLOC },
2365
  { STRING_COMMA_LEN (".dynamic"),       0, SHT_DYNAMIC,  SHF_ALLOC },
2082
  { STRING_COMMA_LEN (".dynstr"),        0, SHT_STRTAB,   SHF_ALLOC },
2366
  { STRING_COMMA_LEN (".dynstr"),        0, SHT_STRTAB,   SHF_ALLOC },
2083
  { STRING_COMMA_LEN (".dynsym"),        0, SHT_DYNSYM,   SHF_ALLOC },
2367
  { STRING_COMMA_LEN (".dynsym"),        0, SHT_DYNSYM,   SHF_ALLOC },
2084
  { NULL,                      0,        0, 0,            0 }
2368
  { NULL,                      0,        0, 0,            0 }
2085
};
2369
};
2086
 
2370
 
2087
static const struct bfd_elf_special_section special_sections_f[] =
2371
static const struct bfd_elf_special_section special_sections_f[] =
2088
{
2372
{
2089
  { STRING_COMMA_LEN (".fini"),       0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
2373
  { STRING_COMMA_LEN (".fini"),       0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
2090
  { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2374
  { STRING_COMMA_LEN (".fini_array"), 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
2091
  { NULL,                          0, 0, 0,              0 }
2375
  { NULL,                          0, 0, 0,              0 }
2092
};
2376
};
2093
 
2377
 
2094
static const struct bfd_elf_special_section special_sections_g[] =
2378
static const struct bfd_elf_special_section special_sections_g[] =
2095
{
2379
{
2096
  { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
2380
  { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
2097
  { STRING_COMMA_LEN (".gnu.lto_"),       -1, SHT_PROGBITS,    SHF_EXCLUDE },
2381
  { STRING_COMMA_LEN (".gnu.lto_"),       -1, SHT_PROGBITS,    SHF_EXCLUDE },
2098
  { STRING_COMMA_LEN (".got"),             0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
2382
  { STRING_COMMA_LEN (".got"),             0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
2099
  { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
2383
  { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
2100
  { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
2384
  { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
2101
  { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
2385
  { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
2102
  { STRING_COMMA_LEN (".gnu.liblist"),     0, SHT_GNU_LIBLIST, SHF_ALLOC },
2386
  { STRING_COMMA_LEN (".gnu.liblist"),     0, SHT_GNU_LIBLIST, SHF_ALLOC },
2103
  { STRING_COMMA_LEN (".gnu.conflict"),    0, SHT_RELA,        SHF_ALLOC },
2387
  { STRING_COMMA_LEN (".gnu.conflict"),    0, SHT_RELA,        SHF_ALLOC },
2104
  { STRING_COMMA_LEN (".gnu.hash"),        0, SHT_GNU_HASH,    SHF_ALLOC },
2388
  { STRING_COMMA_LEN (".gnu.hash"),        0, SHT_GNU_HASH,    SHF_ALLOC },
2105
  { NULL,                        0,        0, 0,               0 }
2389
  { NULL,                        0,        0, 0,               0 }
2106
};
2390
};
2107
 
2391
 
2108
static const struct bfd_elf_special_section special_sections_h[] =
2392
static const struct bfd_elf_special_section special_sections_h[] =
2109
{
2393
{
2110
  { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,     SHF_ALLOC },
2394
  { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,     SHF_ALLOC },
2111
  { NULL,                    0, 0, 0,            0 }
2395
  { NULL,                    0, 0, 0,            0 }
2112
};
2396
};
2113
 
2397
 
2114
static const struct bfd_elf_special_section special_sections_i[] =
2398
static const struct bfd_elf_special_section special_sections_i[] =
2115
{
2399
{
2116
  { STRING_COMMA_LEN (".init"),       0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
2400
  { STRING_COMMA_LEN (".init"),       0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
2117
  { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2401
  { STRING_COMMA_LEN (".init_array"), 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2118
  { STRING_COMMA_LEN (".interp"),     0, SHT_PROGBITS,   0 },
2402
  { STRING_COMMA_LEN (".interp"),     0, SHT_PROGBITS,   0 },
2119
  { NULL,                      0,     0, 0,              0 }
2403
  { NULL,                      0,     0, 0,              0 }
2120
};
2404
};
2121
 
2405
 
2122
static const struct bfd_elf_special_section special_sections_l[] =
2406
static const struct bfd_elf_special_section special_sections_l[] =
2123
{
2407
{
2124
  { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2408
  { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
2125
  { NULL,                    0, 0, 0,            0 }
2409
  { NULL,                    0, 0, 0,            0 }
2126
};
2410
};
2127
 
2411
 
2128
static const struct bfd_elf_special_section special_sections_n[] =
2412
static const struct bfd_elf_special_section special_sections_n[] =
2129
{
2413
{
2130
  { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2414
  { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 },
2131
  { STRING_COMMA_LEN (".note"),          -1, SHT_NOTE,     0 },
2415
  { STRING_COMMA_LEN (".note"),          -1, SHT_NOTE,     0 },
2132
  { NULL,                    0,           0, 0,            0 }
2416
  { NULL,                    0,           0, 0,            0 }
2133
};
2417
};
2134
 
2418
 
2135
static const struct bfd_elf_special_section special_sections_p[] =
2419
static const struct bfd_elf_special_section special_sections_p[] =
2136
{
2420
{
2137
  { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2421
  { STRING_COMMA_LEN (".preinit_array"), 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
2138
  { STRING_COMMA_LEN (".plt"),           0, SHT_PROGBITS,      SHF_ALLOC + SHF_EXECINSTR },
2422
  { STRING_COMMA_LEN (".plt"),           0, SHT_PROGBITS,      SHF_ALLOC + SHF_EXECINSTR },
2139
  { NULL,                   0,           0, 0,                 0 }
2423
  { NULL,                   0,           0, 0,                 0 }
2140
};
2424
};
2141
 
2425
 
2142
static const struct bfd_elf_special_section special_sections_r[] =
2426
static const struct bfd_elf_special_section special_sections_r[] =
2143
{
2427
{
2144
  { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2428
  { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
2145
  { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2429
  { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
2146
  { STRING_COMMA_LEN (".rela"),   -1, SHT_RELA,     0 },
2430
  { STRING_COMMA_LEN (".rela"),   -1, SHT_RELA,     0 },
2147
  { STRING_COMMA_LEN (".rel"),    -1, SHT_REL,      0 },
2431
  { STRING_COMMA_LEN (".rel"),    -1, SHT_REL,      0 },
2148
  { NULL,                   0,     0, 0,            0 }
2432
  { NULL,                   0,     0, 0,            0 }
2149
};
2433
};
2150
 
2434
 
2151
static const struct bfd_elf_special_section special_sections_s[] =
2435
static const struct bfd_elf_special_section special_sections_s[] =
2152
{
2436
{
2153
  { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2437
  { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
2154
  { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
2438
  { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
2155
  { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
2439
  { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
2156
  /* See struct bfd_elf_special_section declaration for the semantics of
2440
  /* See struct bfd_elf_special_section declaration for the semantics of
2157
     this special case where .prefix_length != strlen (.prefix).  */
2441
     this special case where .prefix_length != strlen (.prefix).  */
2158
  { ".stabstr",			5,  3, SHT_STRTAB, 0 },
2442
  { ".stabstr",			5,  3, SHT_STRTAB, 0 },
2159
  { NULL,                       0,  0, 0,          0 }
2443
  { NULL,                       0,  0, 0,          0 }
2160
};
2444
};
2161
 
2445
 
2162
static const struct bfd_elf_special_section special_sections_t[] =
2446
static const struct bfd_elf_special_section special_sections_t[] =
2163
{
2447
{
2164
  { STRING_COMMA_LEN (".text"),  -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2448
  { STRING_COMMA_LEN (".text"),  -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
2165
  { STRING_COMMA_LEN (".tbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
2449
  { STRING_COMMA_LEN (".tbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
2166
  { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2450
  { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
2167
  { NULL,                     0,  0, 0,            0 }
2451
  { NULL,                     0,  0, 0,            0 }
2168
};
2452
};
2169
 
2453
 
2170
static const struct bfd_elf_special_section special_sections_z[] =
2454
static const struct bfd_elf_special_section special_sections_z[] =
2171
{
2455
{
2172
  { STRING_COMMA_LEN (".zdebug_line"),    0, SHT_PROGBITS, 0 },
2456
  { STRING_COMMA_LEN (".zdebug_line"),    0, SHT_PROGBITS, 0 },
2173
  { STRING_COMMA_LEN (".zdebug_info"),    0, SHT_PROGBITS, 0 },
2457
  { STRING_COMMA_LEN (".zdebug_info"),    0, SHT_PROGBITS, 0 },
2174
  { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
2458
  { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
2175
  { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2459
  { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
2176
  { NULL,                     0,  0, 0,            0 }
2460
  { NULL,                     0,  0, 0,            0 }
2177
};
2461
};
2178
 
2462
 
2179
static const struct bfd_elf_special_section * const special_sections[] =
2463
static const struct bfd_elf_special_section * const special_sections[] =
2180
{
2464
{
2181
  special_sections_b,		/* 'b' */
2465
  special_sections_b,		/* 'b' */
2182
  special_sections_c,		/* 'c' */
2466
  special_sections_c,		/* 'c' */
2183
  special_sections_d,		/* 'd' */
2467
  special_sections_d,		/* 'd' */
2184
  NULL,				/* 'e' */
2468
  NULL,				/* 'e' */
2185
  special_sections_f,		/* 'f' */
2469
  special_sections_f,		/* 'f' */
2186
  special_sections_g,		/* 'g' */
2470
  special_sections_g,		/* 'g' */
2187
  special_sections_h,		/* 'h' */
2471
  special_sections_h,		/* 'h' */
2188
  special_sections_i,		/* 'i' */
2472
  special_sections_i,		/* 'i' */
2189
  NULL,				/* 'j' */
2473
  NULL,				/* 'j' */
2190
  NULL,				/* 'k' */
2474
  NULL,				/* 'k' */
2191
  special_sections_l,		/* 'l' */
2475
  special_sections_l,		/* 'l' */
2192
  NULL,				/* 'm' */
2476
  NULL,				/* 'm' */
2193
  special_sections_n,		/* 'n' */
2477
  special_sections_n,		/* 'n' */
2194
  NULL,				/* 'o' */
2478
  NULL,				/* 'o' */
2195
  special_sections_p,		/* 'p' */
2479
  special_sections_p,		/* 'p' */
2196
  NULL,				/* 'q' */
2480
  NULL,				/* 'q' */
2197
  special_sections_r,		/* 'r' */
2481
  special_sections_r,		/* 'r' */
2198
  special_sections_s,		/* 's' */
2482
  special_sections_s,		/* 's' */
2199
  special_sections_t,		/* 't' */
2483
  special_sections_t,		/* 't' */
2200
  NULL,				/* 'u' */
2484
  NULL,				/* 'u' */
2201
  NULL,				/* 'v' */
2485
  NULL,				/* 'v' */
2202
  NULL,				/* 'w' */
2486
  NULL,				/* 'w' */
2203
  NULL,				/* 'x' */
2487
  NULL,				/* 'x' */
2204
  NULL,				/* 'y' */
2488
  NULL,				/* 'y' */
2205
  special_sections_z		/* 'z' */
2489
  special_sections_z		/* 'z' */
2206
};
2490
};
2207
 
2491
 
2208
const struct bfd_elf_special_section *
2492
const struct bfd_elf_special_section *
2209
_bfd_elf_get_special_section (const char *name,
2493
_bfd_elf_get_special_section (const char *name,
2210
			      const struct bfd_elf_special_section *spec,
2494
			      const struct bfd_elf_special_section *spec,
2211
			      unsigned int rela)
2495
			      unsigned int rela)
2212
{
2496
{
2213
  int i;
2497
  int i;
2214
  int len;
2498
  int len;
2215
 
2499
 
2216
  len = strlen (name);
2500
  len = strlen (name);
2217
 
2501
 
2218
  for (i = 0; spec[i].prefix != NULL; i++)
2502
  for (i = 0; spec[i].prefix != NULL; i++)
2219
    {
2503
    {
2220
      int suffix_len;
2504
      int suffix_len;
2221
      int prefix_len = spec[i].prefix_length;
2505
      int prefix_len = spec[i].prefix_length;
2222
 
2506
 
2223
      if (len < prefix_len)
2507
      if (len < prefix_len)
2224
	continue;
2508
	continue;
2225
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2509
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
2226
	continue;
2510
	continue;
2227
 
2511
 
2228
      suffix_len = spec[i].suffix_length;
2512
      suffix_len = spec[i].suffix_length;
2229
      if (suffix_len <= 0)
2513
      if (suffix_len <= 0)
2230
	{
2514
	{
2231
	  if (name[prefix_len] != 0)
2515
	  if (name[prefix_len] != 0)
2232
	    {
2516
	    {
2233
	      if (suffix_len == 0)
2517
	      if (suffix_len == 0)
2234
		continue;
2518
		continue;
2235
	      if (name[prefix_len] != '.'
2519
	      if (name[prefix_len] != '.'
2236
		  && (suffix_len == -2
2520
		  && (suffix_len == -2
2237
		      || (rela && spec[i].type == SHT_REL)))
2521
		      || (rela && spec[i].type == SHT_REL)))
2238
		continue;
2522
		continue;
2239
	    }
2523
	    }
2240
	}
2524
	}
2241
      else
2525
      else
2242
	{
2526
	{
2243
	  if (len < prefix_len + suffix_len)
2527
	  if (len < prefix_len + suffix_len)
2244
	    continue;
2528
	    continue;
2245
	  if (memcmp (name + len - suffix_len,
2529
	  if (memcmp (name + len - suffix_len,
2246
		      spec[i].prefix + prefix_len,
2530
		      spec[i].prefix + prefix_len,
2247
		      suffix_len) != 0)
2531
		      suffix_len) != 0)
2248
	    continue;
2532
	    continue;
2249
	}
2533
	}
2250
      return &spec[i];
2534
      return &spec[i];
2251
    }
2535
    }
2252
 
2536
 
2253
  return NULL;
2537
  return NULL;
2254
}
2538
}
2255
 
2539
 
2256
const struct bfd_elf_special_section *
2540
const struct bfd_elf_special_section *
2257
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2541
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
2258
{
2542
{
2259
  int i;
2543
  int i;
2260
  const struct bfd_elf_special_section *spec;
2544
  const struct bfd_elf_special_section *spec;
2261
  const struct elf_backend_data *bed;
2545
  const struct elf_backend_data *bed;
2262
 
2546
 
2263
  /* See if this is one of the special sections.  */
2547
  /* See if this is one of the special sections.  */
2264
  if (sec->name == NULL)
2548
  if (sec->name == NULL)
2265
    return NULL;
2549
    return NULL;
2266
 
2550
 
2267
  bed = get_elf_backend_data (abfd);
2551
  bed = get_elf_backend_data (abfd);
2268
  spec = bed->special_sections;
2552
  spec = bed->special_sections;
2269
  if (spec)
2553
  if (spec)
2270
    {
2554
    {
2271
      spec = _bfd_elf_get_special_section (sec->name,
2555
      spec = _bfd_elf_get_special_section (sec->name,
2272
					   bed->special_sections,
2556
					   bed->special_sections,
2273
					   sec->use_rela_p);
2557
					   sec->use_rela_p);
2274
      if (spec != NULL)
2558
      if (spec != NULL)
2275
	return spec;
2559
	return spec;
2276
    }
2560
    }
2277
 
2561
 
2278
  if (sec->name[0] != '.')
2562
  if (sec->name[0] != '.')
2279
    return NULL;
2563
    return NULL;
2280
 
2564
 
2281
  i = sec->name[1] - 'b';
2565
  i = sec->name[1] - 'b';
2282
  if (i < 0 || i > 'z' - 'b')
2566
  if (i < 0 || i > 'z' - 'b')
2283
    return NULL;
2567
    return NULL;
2284
 
2568
 
2285
  spec = special_sections[i];
2569
  spec = special_sections[i];
2286
 
2570
 
2287
  if (spec == NULL)
2571
  if (spec == NULL)
2288
    return NULL;
2572
    return NULL;
2289
 
2573
 
2290
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2574
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
2291
}
2575
}
2292
 
2576
 
2293
bfd_boolean
2577
bfd_boolean
2294
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2578
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
2295
{
2579
{
2296
  struct bfd_elf_section_data *sdata;
2580
  struct bfd_elf_section_data *sdata;
2297
  const struct elf_backend_data *bed;
2581
  const struct elf_backend_data *bed;
2298
  const struct bfd_elf_special_section *ssect;
2582
  const struct bfd_elf_special_section *ssect;
2299
 
2583
 
2300
  sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2584
  sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
2301
  if (sdata == NULL)
2585
  if (sdata == NULL)
2302
    {
2586
    {
2303
      sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2587
      sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
2304
                                                          sizeof (*sdata));
2588
                                                          sizeof (*sdata));
2305
      if (sdata == NULL)
2589
      if (sdata == NULL)
2306
	return FALSE;
2590
	return FALSE;
2307
      sec->used_by_bfd = sdata;
2591
      sec->used_by_bfd = sdata;
2308
    }
2592
    }
2309
 
2593
 
2310
  /* Indicate whether or not this section should use RELA relocations.  */
2594
  /* Indicate whether or not this section should use RELA relocations.  */
2311
  bed = get_elf_backend_data (abfd);
2595
  bed = get_elf_backend_data (abfd);
2312
  sec->use_rela_p = bed->default_use_rela_p;
2596
  sec->use_rela_p = bed->default_use_rela_p;
2313
 
2597
 
2314
  /* When we read a file, we don't need to set ELF section type and
2598
  /* When we read a file, we don't need to set ELF section type and
2315
     flags.  They will be overridden in _bfd_elf_make_section_from_shdr
2599
     flags.  They will be overridden in _bfd_elf_make_section_from_shdr
2316
     anyway.  We will set ELF section type and flags for all linker
2600
     anyway.  We will set ELF section type and flags for all linker
2317
     created sections.  If user specifies BFD section flags, we will
2601
     created sections.  If user specifies BFD section flags, we will
2318
     set ELF section type and flags based on BFD section flags in
2602
     set ELF section type and flags based on BFD section flags in
2319
     elf_fake_sections.  Special handling for .init_array/.fini_array
2603
     elf_fake_sections.  Special handling for .init_array/.fini_array
2320
     output sections since they may contain .ctors/.dtors input
2604
     output sections since they may contain .ctors/.dtors input
2321
     sections.  We don't want _bfd_elf_init_private_section_data to
2605
     sections.  We don't want _bfd_elf_init_private_section_data to
2322
     copy ELF section type from .ctors/.dtors input sections.  */
2606
     copy ELF section type from .ctors/.dtors input sections.  */
2323
  if (abfd->direction != read_direction
2607
  if (abfd->direction != read_direction
2324
      || (sec->flags & SEC_LINKER_CREATED) != 0)
2608
      || (sec->flags & SEC_LINKER_CREATED) != 0)
2325
    {
2609
    {
2326
      ssect = (*bed->get_sec_type_attr) (abfd, sec);
2610
      ssect = (*bed->get_sec_type_attr) (abfd, sec);
2327
      if (ssect != NULL
2611
      if (ssect != NULL
2328
	  && (!sec->flags
2612
	  && (!sec->flags
2329
	      || (sec->flags & SEC_LINKER_CREATED) != 0
2613
	      || (sec->flags & SEC_LINKER_CREATED) != 0
2330
	      || ssect->type == SHT_INIT_ARRAY
2614
	      || ssect->type == SHT_INIT_ARRAY
2331
	      || ssect->type == SHT_FINI_ARRAY))
2615
	      || ssect->type == SHT_FINI_ARRAY))
2332
	{
2616
	{
2333
	  elf_section_type (sec) = ssect->type;
2617
	  elf_section_type (sec) = ssect->type;
2334
	  elf_section_flags (sec) = ssect->attr;
2618
	  elf_section_flags (sec) = ssect->attr;
2335
	}
2619
	}
2336
    }
2620
    }
2337
 
2621
 
2338
  return _bfd_generic_new_section_hook (abfd, sec);
2622
  return _bfd_generic_new_section_hook (abfd, sec);
2339
}
2623
}
2340
 
2624
 
2341
/* Create a new bfd section from an ELF program header.
2625
/* Create a new bfd section from an ELF program header.
2342
 
2626
 
2343
   Since program segments have no names, we generate a synthetic name
2627
   Since program segments have no names, we generate a synthetic name
2344
   of the form segment, where NUM is generally the index in the
2628
   of the form segment, where NUM is generally the index in the
2345
   program header table.  For segments that are split (see below) we
2629
   program header table.  For segments that are split (see below) we
2346
   generate the names segmenta and segmentb.
2630
   generate the names segmenta and segmentb.
2347
 
2631
 
2348
   Note that some program segments may have a file size that is different than
2632
   Note that some program segments may have a file size that is different than
2349
   (less than) the memory size.  All this means is that at execution the
2633
   (less than) the memory size.  All this means is that at execution the
2350
   system must allocate the amount of memory specified by the memory size,
2634
   system must allocate the amount of memory specified by the memory size,
2351
   but only initialize it with the first "file size" bytes read from the
2635
   but only initialize it with the first "file size" bytes read from the
2352
   file.  This would occur for example, with program segments consisting
2636
   file.  This would occur for example, with program segments consisting
2353
   of combined data+bss.
2637
   of combined data+bss.
2354
 
2638
 
2355
   To handle the above situation, this routine generates TWO bfd sections
2639
   To handle the above situation, this routine generates TWO bfd sections
2356
   for the single program segment.  The first has the length specified by
2640
   for the single program segment.  The first has the length specified by
2357
   the file size of the segment, and the second has the length specified
2641
   the file size of the segment, and the second has the length specified
2358
   by the difference between the two sizes.  In effect, the segment is split
2642
   by the difference between the two sizes.  In effect, the segment is split
2359
   into its initialized and uninitialized parts.
2643
   into its initialized and uninitialized parts.
2360
 
2644
 
2361
 */
2645
 */
2362
 
2646
 
2363
bfd_boolean
2647
bfd_boolean
2364
_bfd_elf_make_section_from_phdr (bfd *abfd,
2648
_bfd_elf_make_section_from_phdr (bfd *abfd,
2365
				 Elf_Internal_Phdr *hdr,
2649
				 Elf_Internal_Phdr *hdr,
2366
				 int hdr_index,
2650
				 int hdr_index,
2367
				 const char *type_name)
2651
				 const char *type_name)
2368
{
2652
{
2369
  asection *newsect;
2653
  asection *newsect;
2370
  char *name;
2654
  char *name;
2371
  char namebuf[64];
2655
  char namebuf[64];
2372
  size_t len;
2656
  size_t len;
2373
  int split;
2657
  int split;
2374
 
2658
 
2375
  split = ((hdr->p_memsz > 0)
2659
  split = ((hdr->p_memsz > 0)
2376
	    && (hdr->p_filesz > 0)
2660
	    && (hdr->p_filesz > 0)
2377
	    && (hdr->p_memsz > hdr->p_filesz));
2661
	    && (hdr->p_memsz > hdr->p_filesz));
2378
 
2662
 
2379
  if (hdr->p_filesz > 0)
2663
  if (hdr->p_filesz > 0)
2380
    {
2664
    {
2381
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2665
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
2382
      len = strlen (namebuf) + 1;
2666
      len = strlen (namebuf) + 1;
2383
      name = (char *) bfd_alloc (abfd, len);
2667
      name = (char *) bfd_alloc (abfd, len);
2384
      if (!name)
2668
      if (!name)
2385
	return FALSE;
2669
	return FALSE;
2386
      memcpy (name, namebuf, len);
2670
      memcpy (name, namebuf, len);
2387
      newsect = bfd_make_section (abfd, name);
2671
      newsect = bfd_make_section (abfd, name);
2388
      if (newsect == NULL)
2672
      if (newsect == NULL)
2389
	return FALSE;
2673
	return FALSE;
2390
      newsect->vma = hdr->p_vaddr;
2674
      newsect->vma = hdr->p_vaddr;
2391
      newsect->lma = hdr->p_paddr;
2675
      newsect->lma = hdr->p_paddr;
2392
      newsect->size = hdr->p_filesz;
2676
      newsect->size = hdr->p_filesz;
2393
      newsect->filepos = hdr->p_offset;
2677
      newsect->filepos = hdr->p_offset;
2394
      newsect->flags |= SEC_HAS_CONTENTS;
2678
      newsect->flags |= SEC_HAS_CONTENTS;
2395
      newsect->alignment_power = bfd_log2 (hdr->p_align);
2679
      newsect->alignment_power = bfd_log2 (hdr->p_align);
2396
      if (hdr->p_type == PT_LOAD)
2680
      if (hdr->p_type == PT_LOAD)
2397
	{
2681
	{
2398
	  newsect->flags |= SEC_ALLOC;
2682
	  newsect->flags |= SEC_ALLOC;
2399
	  newsect->flags |= SEC_LOAD;
2683
	  newsect->flags |= SEC_LOAD;
2400
	  if (hdr->p_flags & PF_X)
2684
	  if (hdr->p_flags & PF_X)
2401
	    {
2685
	    {
2402
	      /* FIXME: all we known is that it has execute PERMISSION,
2686
	      /* FIXME: all we known is that it has execute PERMISSION,
2403
		 may be data.  */
2687
		 may be data.  */
2404
	      newsect->flags |= SEC_CODE;
2688
	      newsect->flags |= SEC_CODE;
2405
	    }
2689
	    }
2406
	}
2690
	}
2407
      if (!(hdr->p_flags & PF_W))
2691
      if (!(hdr->p_flags & PF_W))
2408
	{
2692
	{
2409
	  newsect->flags |= SEC_READONLY;
2693
	  newsect->flags |= SEC_READONLY;
2410
	}
2694
	}
2411
    }
2695
    }
2412
 
2696
 
2413
  if (hdr->p_memsz > hdr->p_filesz)
2697
  if (hdr->p_memsz > hdr->p_filesz)
2414
    {
2698
    {
2415
      bfd_vma align;
2699
      bfd_vma align;
2416
 
2700
 
2417
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2701
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
2418
      len = strlen (namebuf) + 1;
2702
      len = strlen (namebuf) + 1;
2419
      name = (char *) bfd_alloc (abfd, len);
2703
      name = (char *) bfd_alloc (abfd, len);
2420
      if (!name)
2704
      if (!name)
2421
	return FALSE;
2705
	return FALSE;
2422
      memcpy (name, namebuf, len);
2706
      memcpy (name, namebuf, len);
2423
      newsect = bfd_make_section (abfd, name);
2707
      newsect = bfd_make_section (abfd, name);
2424
      if (newsect == NULL)
2708
      if (newsect == NULL)
2425
	return FALSE;
2709
	return FALSE;
2426
      newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2710
      newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2427
      newsect->lma = hdr->p_paddr + hdr->p_filesz;
2711
      newsect->lma = hdr->p_paddr + hdr->p_filesz;
2428
      newsect->size = hdr->p_memsz - hdr->p_filesz;
2712
      newsect->size = hdr->p_memsz - hdr->p_filesz;
2429
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
2713
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
2430
      align = newsect->vma & -newsect->vma;
2714
      align = newsect->vma & -newsect->vma;
2431
      if (align == 0 || align > hdr->p_align)
2715
      if (align == 0 || align > hdr->p_align)
2432
	align = hdr->p_align;
2716
	align = hdr->p_align;
2433
      newsect->alignment_power = bfd_log2 (align);
2717
      newsect->alignment_power = bfd_log2 (align);
2434
      if (hdr->p_type == PT_LOAD)
2718
      if (hdr->p_type == PT_LOAD)
2435
	{
2719
	{
2436
	  /* Hack for gdb.  Segments that have not been modified do
2720
	  /* Hack for gdb.  Segments that have not been modified do
2437
	     not have their contents written to a core file, on the
2721
	     not have their contents written to a core file, on the
2438
	     assumption that a debugger can find the contents in the
2722
	     assumption that a debugger can find the contents in the
2439
	     executable.  We flag this case by setting the fake
2723
	     executable.  We flag this case by setting the fake
2440
	     section size to zero.  Note that "real" bss sections will
2724
	     section size to zero.  Note that "real" bss sections will
2441
	     always have their contents dumped to the core file.  */
2725
	     always have their contents dumped to the core file.  */
2442
	  if (bfd_get_format (abfd) == bfd_core)
2726
	  if (bfd_get_format (abfd) == bfd_core)
2443
	    newsect->size = 0;
2727
	    newsect->size = 0;
2444
	  newsect->flags |= SEC_ALLOC;
2728
	  newsect->flags |= SEC_ALLOC;
2445
	  if (hdr->p_flags & PF_X)
2729
	  if (hdr->p_flags & PF_X)
2446
	    newsect->flags |= SEC_CODE;
2730
	    newsect->flags |= SEC_CODE;
2447
	}
2731
	}
2448
      if (!(hdr->p_flags & PF_W))
2732
      if (!(hdr->p_flags & PF_W))
2449
	newsect->flags |= SEC_READONLY;
2733
	newsect->flags |= SEC_READONLY;
2450
    }
2734
    }
2451
 
2735
 
2452
  return TRUE;
2736
  return TRUE;
2453
}
2737
}
2454
 
2738
 
2455
bfd_boolean
2739
bfd_boolean
2456
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2740
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
2457
{
2741
{
2458
  const struct elf_backend_data *bed;
2742
  const struct elf_backend_data *bed;
2459
 
2743
 
2460
  switch (hdr->p_type)
2744
  switch (hdr->p_type)
2461
    {
2745
    {
2462
    case PT_NULL:
2746
    case PT_NULL:
2463
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2747
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
2464
 
2748
 
2465
    case PT_LOAD:
2749
    case PT_LOAD:
2466
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2750
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load");
2467
 
2751
 
2468
    case PT_DYNAMIC:
2752
    case PT_DYNAMIC:
2469
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2753
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
2470
 
2754
 
2471
    case PT_INTERP:
2755
    case PT_INTERP:
2472
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2756
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
2473
 
2757
 
2474
    case PT_NOTE:
2758
    case PT_NOTE:
2475
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2759
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
2476
	return FALSE;
2760
	return FALSE;
2477
      if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2761
      if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
2478
	return FALSE;
2762
	return FALSE;
2479
      return TRUE;
2763
      return TRUE;
2480
 
2764
 
2481
    case PT_SHLIB:
2765
    case PT_SHLIB:
2482
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
2766
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
2483
 
2767
 
2484
    case PT_PHDR:
2768
    case PT_PHDR:
2485
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
2769
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
2486
 
2770
 
2487
    case PT_GNU_EH_FRAME:
2771
    case PT_GNU_EH_FRAME:
2488
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
2772
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
2489
					      "eh_frame_hdr");
2773
					      "eh_frame_hdr");
2490
 
2774
 
2491
    case PT_GNU_STACK:
2775
    case PT_GNU_STACK:
2492
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
2776
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
2493
 
2777
 
2494
    case PT_GNU_RELRO:
2778
    case PT_GNU_RELRO:
2495
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
2779
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
2496
 
2780
 
2497
    default:
2781
    default:
2498
      /* Check for any processor-specific program segment types.  */
2782
      /* Check for any processor-specific program segment types.  */
2499
      bed = get_elf_backend_data (abfd);
2783
      bed = get_elf_backend_data (abfd);
2500
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
2784
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
2501
    }
2785
    }
2502
}
2786
}
2503
 
2787
 
2504
/* Return the REL_HDR for SEC, assuming there is only a single one, either
2788
/* Return the REL_HDR for SEC, assuming there is only a single one, either
2505
   REL or RELA.  */
2789
   REL or RELA.  */
2506
 
2790
 
2507
Elf_Internal_Shdr *
2791
Elf_Internal_Shdr *
2508
_bfd_elf_single_rel_hdr (asection *sec)
2792
_bfd_elf_single_rel_hdr (asection *sec)
2509
{
2793
{
2510
  if (elf_section_data (sec)->rel.hdr)
2794
  if (elf_section_data (sec)->rel.hdr)
2511
    {
2795
    {
2512
      BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
2796
      BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
2513
      return elf_section_data (sec)->rel.hdr;
2797
      return elf_section_data (sec)->rel.hdr;
2514
    }
2798
    }
2515
  else
2799
  else
2516
    return elf_section_data (sec)->rela.hdr;
2800
    return elf_section_data (sec)->rela.hdr;
2517
}
2801
}
-
 
2802
 
-
 
2803
static bfd_boolean
-
 
2804
_bfd_elf_set_reloc_sh_name (bfd *abfd,
-
 
2805
			    Elf_Internal_Shdr *rel_hdr,
-
 
2806
			    const char *sec_name,
-
 
2807
			    bfd_boolean use_rela_p)
-
 
2808
{
-
 
2809
  char *name = (char *) bfd_alloc (abfd,
-
 
2810
				   sizeof ".rela" + strlen (sec_name));
-
 
2811
  if (name == NULL)
-
 
2812
    return FALSE;
-
 
2813
 
-
 
2814
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
-
 
2815
  rel_hdr->sh_name =
-
 
2816
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
-
 
2817
					FALSE);
-
 
2818
  if (rel_hdr->sh_name == (unsigned int) -1)
-
 
2819
    return FALSE;
-
 
2820
 
-
 
2821
  return TRUE;
-
 
2822
}
2518
 
2823
 
2519
/* Allocate and initialize a section-header for a new reloc section,
2824
/* Allocate and initialize a section-header for a new reloc section,
2520
   containing relocations against ASECT.  It is stored in RELDATA.  If
2825
   containing relocations against ASECT.  It is stored in RELDATA.  If
2521
   USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2826
   USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
2522
   relocations.  */
2827
   relocations.  */
2523
 
2828
 
2524
static bfd_boolean
2829
static bfd_boolean
2525
_bfd_elf_init_reloc_shdr (bfd *abfd,
2830
_bfd_elf_init_reloc_shdr (bfd *abfd,
2526
			  struct bfd_elf_section_reloc_data *reldata,
2831
			  struct bfd_elf_section_reloc_data *reldata,
2527
			  asection *asect,
2832
			  const char *sec_name,
2528
			  bfd_boolean use_rela_p)
2833
			  bfd_boolean use_rela_p,
-
 
2834
			  bfd_boolean delay_st_name_p)
2529
{
2835
{
2530
  Elf_Internal_Shdr *rel_hdr;
2836
  Elf_Internal_Shdr *rel_hdr;
2531
  char *name;
-
 
2532
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2837
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2533
  bfd_size_type amt;
2838
  bfd_size_type amt;
2534
 
2839
 
2535
  amt = sizeof (Elf_Internal_Shdr);
2840
  amt = sizeof (Elf_Internal_Shdr);
2536
  BFD_ASSERT (reldata->hdr == NULL);
2841
  BFD_ASSERT (reldata->hdr == NULL);
2537
  rel_hdr = bfd_zalloc (abfd, amt);
2842
  rel_hdr = bfd_zalloc (abfd, amt);
2538
  reldata->hdr = rel_hdr;
2843
  reldata->hdr = rel_hdr;
2539
 
-
 
2540
  amt = sizeof ".rela" + strlen (asect->name);
-
 
2541
  name = (char *) bfd_alloc (abfd, amt);
2844
 
2542
  if (name == NULL)
-
 
2543
    return FALSE;
-
 
2544
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2845
  if (delay_st_name_p)
2545
  rel_hdr->sh_name =
2846
    rel_hdr->sh_name = (unsigned int) -1;
2546
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2847
  else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
2547
					FALSE);
-
 
2548
  if (rel_hdr->sh_name == (unsigned int) -1)
2848
					use_rela_p))
2549
    return FALSE;
2849
    return FALSE;
2550
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2850
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2551
  rel_hdr->sh_entsize = (use_rela_p
2851
  rel_hdr->sh_entsize = (use_rela_p
2552
			 ? bed->s->sizeof_rela
2852
			 ? bed->s->sizeof_rela
2553
			 : bed->s->sizeof_rel);
2853
			 : bed->s->sizeof_rel);
2554
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2854
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
2555
  rel_hdr->sh_flags = 0;
2855
  rel_hdr->sh_flags = 0;
2556
  rel_hdr->sh_addr = 0;
2856
  rel_hdr->sh_addr = 0;
2557
  rel_hdr->sh_size = 0;
2857
  rel_hdr->sh_size = 0;
2558
  rel_hdr->sh_offset = 0;
2858
  rel_hdr->sh_offset = 0;
2559
 
2859
 
2560
  return TRUE;
2860
  return TRUE;
2561
}
2861
}
2562
 
2862
 
2563
/* Return the default section type based on the passed in section flags.  */
2863
/* Return the default section type based on the passed in section flags.  */
2564
 
2864
 
2565
int
2865
int
2566
bfd_elf_get_default_section_type (flagword flags)
2866
bfd_elf_get_default_section_type (flagword flags)
2567
{
2867
{
2568
  if ((flags & SEC_ALLOC) != 0
2868
  if ((flags & SEC_ALLOC) != 0
2569
      && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2869
      && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2570
    return SHT_NOBITS;
2870
    return SHT_NOBITS;
2571
  return SHT_PROGBITS;
2871
  return SHT_PROGBITS;
2572
}
2872
}
2573
 
2873
 
2574
struct fake_section_arg
2874
struct fake_section_arg
2575
{
2875
{
2576
  struct bfd_link_info *link_info;
2876
  struct bfd_link_info *link_info;
2577
  bfd_boolean failed;
2877
  bfd_boolean failed;
2578
};
2878
};
2579
 
2879
 
2580
/* Set up an ELF internal section header for a section.  */
2880
/* Set up an ELF internal section header for a section.  */
2581
 
2881
 
2582
static void
2882
static void
2583
elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
2883
elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
2584
{
2884
{
2585
  struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
2885
  struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
2586
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2886
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2587
  struct bfd_elf_section_data *esd = elf_section_data (asect);
2887
  struct bfd_elf_section_data *esd = elf_section_data (asect);
2588
  Elf_Internal_Shdr *this_hdr;
2888
  Elf_Internal_Shdr *this_hdr;
2589
  unsigned int sh_type;
2889
  unsigned int sh_type;
-
 
2890
  const char *name = asect->name;
-
 
2891
  bfd_boolean delay_st_name_p = FALSE;
2590
 
2892
 
2591
  if (arg->failed)
2893
  if (arg->failed)
2592
    {
2894
    {
2593
      /* We already failed; just get out of the bfd_map_over_sections
2895
      /* We already failed; just get out of the bfd_map_over_sections
2594
	 loop.  */
2896
	 loop.  */
2595
      return;
2897
      return;
2596
    }
2898
    }
2597
 
2899
 
2598
  this_hdr = &esd->this_hdr;
2900
  this_hdr = &esd->this_hdr;
-
 
2901
 
-
 
2902
  if (arg->link_info)
-
 
2903
    {
-
 
2904
      /* ld: compress DWARF debug sections with names: .debug_*.  */
-
 
2905
      if ((arg->link_info->compress_debug & COMPRESS_DEBUG)
-
 
2906
	  && (asect->flags & SEC_DEBUGGING)
-
 
2907
	  && name[1] == 'd'
-
 
2908
	  && name[6] == '_')
-
 
2909
	{
-
 
2910
	  /* Set SEC_ELF_COMPRESS to indicate this section should be
-
 
2911
	     compressed.  */
-
 
2912
	  asect->flags |= SEC_ELF_COMPRESS;
-
 
2913
 
-
 
2914
	  /* If this section will be compressed, delay adding setion
-
 
2915
	     name to section name section after it is compressed in
-
 
2916
	     _bfd_elf_assign_file_positions_for_non_load.  */
-
 
2917
	  delay_st_name_p = TRUE;
-
 
2918
	}
-
 
2919
    }
-
 
2920
  else if ((asect->flags & SEC_ELF_RENAME))
-
 
2921
    {
-
 
2922
      /* objcopy: rename output DWARF debug section.  */
-
 
2923
      if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI)))
-
 
2924
	{
-
 
2925
	  /* When we decompress or compress with SHF_COMPRESSED,
-
 
2926
	     convert section name from .zdebug_* to .debug_* if
-
 
2927
	     needed.  */
-
 
2928
	  if (name[1] == 'z')
-
 
2929
	    {
-
 
2930
	      char *new_name = convert_zdebug_to_debug (abfd, name);
-
 
2931
	      if (new_name == NULL)
-
 
2932
		{
-
 
2933
		  arg->failed = TRUE;
-
 
2934
		  return;
-
 
2935
		}
-
 
2936
	      name = new_name;
-
 
2937
	    }
-
 
2938
	}
-
 
2939
      else if (asect->compress_status == COMPRESS_SECTION_DONE)
-
 
2940
	{
-
 
2941
	  /* PR binutils/18087: Compression does not always make a
-
 
2942
	     section smaller.  So only rename the section when
-
 
2943
	     compression has actually taken place.  If input section
-
 
2944
	     name is .zdebug_*, we should never compress it again.  */
-
 
2945
	  char *new_name = convert_debug_to_zdebug (abfd, name);
-
 
2946
	  if (new_name == NULL)
-
 
2947
	    {
-
 
2948
	      arg->failed = TRUE;
-
 
2949
	      return;
-
 
2950
	    }
-
 
2951
	  BFD_ASSERT (name[1] != 'z');
-
 
2952
	  name = new_name;
-
 
2953
	}
-
 
2954
    }
-
 
2955
 
-
 
2956
  if (delay_st_name_p)
-
 
2957
    this_hdr->sh_name = (unsigned int) -1;
-
 
2958
  else
-
 
2959
    {
2599
 
2960
      this_hdr->sh_name
2600
  this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2961
	= (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2601
							  asect->name, FALSE);
2962
					      name, FALSE);
2602
  if (this_hdr->sh_name == (unsigned int) -1)
2963
      if (this_hdr->sh_name == (unsigned int) -1)
2603
    {
2964
	{
2604
      arg->failed = TRUE;
2965
	  arg->failed = TRUE;
2605
      return;
2966
	  return;
2606
    }
2967
	}
-
 
2968
    }
2607
 
2969
 
2608
  /* Don't clear sh_flags. Assembler may set additional bits.  */
2970
  /* Don't clear sh_flags. Assembler may set additional bits.  */
2609
 
2971
 
2610
  if ((asect->flags & SEC_ALLOC) != 0
2972
  if ((asect->flags & SEC_ALLOC) != 0
2611
      || asect->user_set_vma)
2973
      || asect->user_set_vma)
2612
    this_hdr->sh_addr = asect->vma;
2974
    this_hdr->sh_addr = asect->vma;
2613
  else
2975
  else
2614
    this_hdr->sh_addr = 0;
2976
    this_hdr->sh_addr = 0;
2615
 
2977
 
2616
  this_hdr->sh_offset = 0;
2978
  this_hdr->sh_offset = 0;
2617
  this_hdr->sh_size = asect->size;
2979
  this_hdr->sh_size = asect->size;
2618
  this_hdr->sh_link = 0;
2980
  this_hdr->sh_link = 0;
-
 
2981
  /* PR 17512: file: 0eb809fe, 8b0535ee.  */
-
 
2982
  if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
-
 
2983
    {
-
 
2984
      (*_bfd_error_handler)
-
 
2985
	(_("%B: error: Alignment power %d of section `%A' is too big"),
-
 
2986
	 abfd, asect, asect->alignment_power);
-
 
2987
      arg->failed = TRUE;
-
 
2988
      return;
-
 
2989
    }
2619
  this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2990
  this_hdr->sh_addralign = (bfd_vma) 1 << asect->alignment_power;
2620
  /* The sh_entsize and sh_info fields may have been set already by
2991
  /* The sh_entsize and sh_info fields may have been set already by
2621
     copy_private_section_data.  */
2992
     copy_private_section_data.  */
2622
 
2993
 
2623
  this_hdr->bfd_section = asect;
2994
  this_hdr->bfd_section = asect;
2624
  this_hdr->contents = NULL;
2995
  this_hdr->contents = NULL;
2625
 
2996
 
2626
  /* If the section type is unspecified, we set it based on
2997
  /* If the section type is unspecified, we set it based on
2627
     asect->flags.  */
2998
     asect->flags.  */
2628
  if ((asect->flags & SEC_GROUP) != 0)
2999
  if ((asect->flags & SEC_GROUP) != 0)
2629
    sh_type = SHT_GROUP;
3000
    sh_type = SHT_GROUP;
2630
  else
3001
  else
2631
    sh_type = bfd_elf_get_default_section_type (asect->flags);
3002
    sh_type = bfd_elf_get_default_section_type (asect->flags);
2632
 
3003
 
2633
  if (this_hdr->sh_type == SHT_NULL)
3004
  if (this_hdr->sh_type == SHT_NULL)
2634
    this_hdr->sh_type = sh_type;
3005
    this_hdr->sh_type = sh_type;
2635
  else if (this_hdr->sh_type == SHT_NOBITS
3006
  else if (this_hdr->sh_type == SHT_NOBITS
2636
	   && sh_type == SHT_PROGBITS
3007
	   && sh_type == SHT_PROGBITS
2637
	   && (asect->flags & SEC_ALLOC) != 0)
3008
	   && (asect->flags & SEC_ALLOC) != 0)
2638
    {
3009
    {
2639
      /* Warn if we are changing a NOBITS section to PROGBITS, but
3010
      /* Warn if we are changing a NOBITS section to PROGBITS, but
2640
	 allow the link to proceed.  This can happen when users link
3011
	 allow the link to proceed.  This can happen when users link
2641
	 non-bss input sections to bss output sections, or emit data
3012
	 non-bss input sections to bss output sections, or emit data
2642
	 to a bss output section via a linker script.  */
3013
	 to a bss output section via a linker script.  */
2643
      (*_bfd_error_handler)
3014
      (*_bfd_error_handler)
2644
	(_("warning: section `%A' type changed to PROGBITS"), asect);
3015
	(_("warning: section `%A' type changed to PROGBITS"), asect);
2645
      this_hdr->sh_type = sh_type;
3016
      this_hdr->sh_type = sh_type;
2646
    }
3017
    }
2647
 
3018
 
2648
  switch (this_hdr->sh_type)
3019
  switch (this_hdr->sh_type)
2649
    {
3020
    {
2650
    default:
3021
    default:
2651
      break;
3022
      break;
2652
 
3023
 
2653
    case SHT_STRTAB:
3024
    case SHT_STRTAB:
2654
    case SHT_INIT_ARRAY:
3025
    case SHT_INIT_ARRAY:
2655
    case SHT_FINI_ARRAY:
3026
    case SHT_FINI_ARRAY:
2656
    case SHT_PREINIT_ARRAY:
3027
    case SHT_PREINIT_ARRAY:
2657
    case SHT_NOTE:
3028
    case SHT_NOTE:
2658
    case SHT_NOBITS:
3029
    case SHT_NOBITS:
2659
    case SHT_PROGBITS:
3030
    case SHT_PROGBITS:
2660
      break;
3031
      break;
2661
 
3032
 
2662
    case SHT_HASH:
3033
    case SHT_HASH:
2663
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3034
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2664
      break;
3035
      break;
2665
 
3036
 
2666
    case SHT_DYNSYM:
3037
    case SHT_DYNSYM:
2667
      this_hdr->sh_entsize = bed->s->sizeof_sym;
3038
      this_hdr->sh_entsize = bed->s->sizeof_sym;
2668
      break;
3039
      break;
2669
 
3040
 
2670
    case SHT_DYNAMIC:
3041
    case SHT_DYNAMIC:
2671
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
3042
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
2672
      break;
3043
      break;
2673
 
3044
 
2674
    case SHT_RELA:
3045
    case SHT_RELA:
2675
      if (get_elf_backend_data (abfd)->may_use_rela_p)
3046
      if (get_elf_backend_data (abfd)->may_use_rela_p)
2676
	this_hdr->sh_entsize = bed->s->sizeof_rela;
3047
	this_hdr->sh_entsize = bed->s->sizeof_rela;
2677
      break;
3048
      break;
2678
 
3049
 
2679
     case SHT_REL:
3050
     case SHT_REL:
2680
      if (get_elf_backend_data (abfd)->may_use_rel_p)
3051
      if (get_elf_backend_data (abfd)->may_use_rel_p)
2681
	this_hdr->sh_entsize = bed->s->sizeof_rel;
3052
	this_hdr->sh_entsize = bed->s->sizeof_rel;
2682
      break;
3053
      break;
2683
 
3054
 
2684
     case SHT_GNU_versym:
3055
     case SHT_GNU_versym:
2685
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3056
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2686
      break;
3057
      break;
2687
 
3058
 
2688
     case SHT_GNU_verdef:
3059
     case SHT_GNU_verdef:
2689
      this_hdr->sh_entsize = 0;
3060
      this_hdr->sh_entsize = 0;
2690
      /* objcopy or strip will copy over sh_info, but may not set
3061
      /* objcopy or strip will copy over sh_info, but may not set
2691
	 cverdefs.  The linker will set cverdefs, but sh_info will be
3062
	 cverdefs.  The linker will set cverdefs, but sh_info will be
2692
	 zero.  */
3063
	 zero.  */
2693
      if (this_hdr->sh_info == 0)
3064
      if (this_hdr->sh_info == 0)
2694
	this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3065
	this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2695
      else
3066
      else
2696
	BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3067
	BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2697
		    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3068
		    || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2698
      break;
3069
      break;
2699
 
3070
 
2700
    case SHT_GNU_verneed:
3071
    case SHT_GNU_verneed:
2701
      this_hdr->sh_entsize = 0;
3072
      this_hdr->sh_entsize = 0;
2702
      /* objcopy or strip will copy over sh_info, but may not set
3073
      /* objcopy or strip will copy over sh_info, but may not set
2703
	 cverrefs.  The linker will set cverrefs, but sh_info will be
3074
	 cverrefs.  The linker will set cverrefs, but sh_info will be
2704
	 zero.  */
3075
	 zero.  */
2705
      if (this_hdr->sh_info == 0)
3076
      if (this_hdr->sh_info == 0)
2706
	this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3077
	this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2707
      else
3078
      else
2708
	BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3079
	BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2709
		    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3080
		    || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2710
      break;
3081
      break;
2711
 
3082
 
2712
    case SHT_GROUP:
3083
    case SHT_GROUP:
2713
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3084
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
2714
      break;
3085
      break;
2715
 
3086
 
2716
    case SHT_GNU_HASH:
3087
    case SHT_GNU_HASH:
2717
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3088
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
2718
      break;
3089
      break;
2719
    }
3090
    }
2720
 
3091
 
2721
  if ((asect->flags & SEC_ALLOC) != 0)
3092
  if ((asect->flags & SEC_ALLOC) != 0)
2722
    this_hdr->sh_flags |= SHF_ALLOC;
3093
    this_hdr->sh_flags |= SHF_ALLOC;
2723
  if ((asect->flags & SEC_READONLY) == 0)
3094
  if ((asect->flags & SEC_READONLY) == 0)
2724
    this_hdr->sh_flags |= SHF_WRITE;
3095
    this_hdr->sh_flags |= SHF_WRITE;
2725
  if ((asect->flags & SEC_CODE) != 0)
3096
  if ((asect->flags & SEC_CODE) != 0)
2726
    this_hdr->sh_flags |= SHF_EXECINSTR;
3097
    this_hdr->sh_flags |= SHF_EXECINSTR;
2727
  if ((asect->flags & SEC_MERGE) != 0)
3098
  if ((asect->flags & SEC_MERGE) != 0)
2728
    {
3099
    {
2729
      this_hdr->sh_flags |= SHF_MERGE;
3100
      this_hdr->sh_flags |= SHF_MERGE;
2730
      this_hdr->sh_entsize = asect->entsize;
3101
      this_hdr->sh_entsize = asect->entsize;
2731
      if ((asect->flags & SEC_STRINGS) != 0)
3102
      if ((asect->flags & SEC_STRINGS) != 0)
2732
	this_hdr->sh_flags |= SHF_STRINGS;
3103
	this_hdr->sh_flags |= SHF_STRINGS;
2733
    }
3104
    }
2734
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3105
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2735
    this_hdr->sh_flags |= SHF_GROUP;
3106
    this_hdr->sh_flags |= SHF_GROUP;
2736
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3107
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2737
    {
3108
    {
2738
      this_hdr->sh_flags |= SHF_TLS;
3109
      this_hdr->sh_flags |= SHF_TLS;
2739
      if (asect->size == 0
3110
      if (asect->size == 0
2740
	  && (asect->flags & SEC_HAS_CONTENTS) == 0)
3111
	  && (asect->flags & SEC_HAS_CONTENTS) == 0)
2741
	{
3112
	{
2742
	  struct bfd_link_order *o = asect->map_tail.link_order;
3113
	  struct bfd_link_order *o = asect->map_tail.link_order;
2743
 
3114
 
2744
	  this_hdr->sh_size = 0;
3115
	  this_hdr->sh_size = 0;
2745
	  if (o != NULL)
3116
	  if (o != NULL)
2746
	    {
3117
	    {
2747
	      this_hdr->sh_size = o->offset + o->size;
3118
	      this_hdr->sh_size = o->offset + o->size;
2748
	      if (this_hdr->sh_size != 0)
3119
	      if (this_hdr->sh_size != 0)
2749
		this_hdr->sh_type = SHT_NOBITS;
3120
		this_hdr->sh_type = SHT_NOBITS;
2750
	    }
3121
	    }
2751
	}
3122
	}
2752
    }
3123
    }
2753
  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3124
  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
2754
    this_hdr->sh_flags |= SHF_EXCLUDE;
3125
    this_hdr->sh_flags |= SHF_EXCLUDE;
2755
 
3126
 
2756
  /* If the section has relocs, set up a section header for the
3127
  /* If the section has relocs, set up a section header for the
2757
     SHT_REL[A] section.  If two relocation sections are required for
3128
     SHT_REL[A] section.  If two relocation sections are required for
2758
     this section, it is up to the processor-specific back-end to
3129
     this section, it is up to the processor-specific back-end to
2759
     create the other.  */
3130
     create the other.  */
2760
  if ((asect->flags & SEC_RELOC) != 0)
3131
  if ((asect->flags & SEC_RELOC) != 0)
2761
    {
3132
    {
2762
      /* When doing a relocatable link, create both REL and RELA sections if
3133
      /* When doing a relocatable link, create both REL and RELA sections if
2763
	 needed.  */
3134
	 needed.  */
2764
      if (arg->link_info
3135
      if (arg->link_info
2765
	  /* Do the normal setup if we wouldn't create any sections here.  */
3136
	  /* Do the normal setup if we wouldn't create any sections here.  */
2766
	  && esd->rel.count + esd->rela.count > 0
3137
	  && esd->rel.count + esd->rela.count > 0
-
 
3138
	  && (bfd_link_relocatable (arg->link_info)
2767
	  && (arg->link_info->relocatable || arg->link_info->emitrelocations))
3139
	      || arg->link_info->emitrelocations))
2768
	{
3140
	{
2769
	  if (esd->rel.count && esd->rel.hdr == NULL
3141
	  if (esd->rel.count && esd->rel.hdr == NULL
2770
	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, asect, FALSE))
3142
	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, FALSE,
-
 
3143
					    delay_st_name_p))
2771
	    {
3144
	    {
2772
	      arg->failed = TRUE;
3145
	      arg->failed = TRUE;
2773
	      return;
3146
	      return;
2774
	    }
3147
	    }
2775
	  if (esd->rela.count && esd->rela.hdr == NULL
3148
	  if (esd->rela.count && esd->rela.hdr == NULL
2776
	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, asect, TRUE))
3149
	      && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, TRUE,
-
 
3150
					    delay_st_name_p))
2777
	    {
3151
	    {
2778
	      arg->failed = TRUE;
3152
	      arg->failed = TRUE;
2779
	      return;
3153
	      return;
2780
	    }
3154
	    }
2781
	}
3155
	}
2782
      else if (!_bfd_elf_init_reloc_shdr (abfd,
3156
      else if (!_bfd_elf_init_reloc_shdr (abfd,
2783
					  (asect->use_rela_p
3157
					  (asect->use_rela_p
2784
					   ? &esd->rela : &esd->rel),
3158
					   ? &esd->rela : &esd->rel),
2785
					  asect,
3159
					  name,
2786
					  asect->use_rela_p))
3160
					  asect->use_rela_p,
-
 
3161
					  delay_st_name_p))
2787
	  arg->failed = TRUE;
3162
	  arg->failed = TRUE;
2788
    }
3163
    }
2789
 
3164
 
2790
  /* Check for processor-specific section types.  */
3165
  /* Check for processor-specific section types.  */
2791
  sh_type = this_hdr->sh_type;
3166
  sh_type = this_hdr->sh_type;
2792
  if (bed->elf_backend_fake_sections
3167
  if (bed->elf_backend_fake_sections
2793
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3168
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2794
    arg->failed = TRUE;
3169
    arg->failed = TRUE;
2795
 
3170
 
2796
  if (sh_type == SHT_NOBITS && asect->size != 0)
3171
  if (sh_type == SHT_NOBITS && asect->size != 0)
2797
    {
3172
    {
2798
      /* Don't change the header type from NOBITS if we are being
3173
      /* Don't change the header type from NOBITS if we are being
2799
	 called for objcopy --only-keep-debug.  */
3174
	 called for objcopy --only-keep-debug.  */
2800
      this_hdr->sh_type = sh_type;
3175
      this_hdr->sh_type = sh_type;
2801
    }
3176
    }
2802
}
3177
}
2803
 
3178
 
2804
/* Fill in the contents of a SHT_GROUP section.  Called from
3179
/* Fill in the contents of a SHT_GROUP section.  Called from
2805
   _bfd_elf_compute_section_file_positions for gas, objcopy, and
3180
   _bfd_elf_compute_section_file_positions for gas, objcopy, and
2806
   when ELF targets use the generic linker, ld.  Called for ld -r
3181
   when ELF targets use the generic linker, ld.  Called for ld -r
2807
   from bfd_elf_final_link.  */
3182
   from bfd_elf_final_link.  */
2808
 
3183
 
2809
void
3184
void
2810
bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3185
bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
2811
{
3186
{
2812
  bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
3187
  bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2813
  asection *elt, *first;
3188
  asection *elt, *first;
2814
  unsigned char *loc;
3189
  unsigned char *loc;
2815
  bfd_boolean gas;
3190
  bfd_boolean gas;
2816
 
3191
 
2817
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
3192
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
2818
     elfxx-ia64.c.  */
3193
     elfxx-ia64.c.  */
2819
  if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
3194
  if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP)
2820
      || *failedptr)
3195
      || *failedptr)
2821
    return;
3196
    return;
2822
 
3197
 
2823
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
3198
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
2824
    {
3199
    {
2825
      unsigned long symindx = 0;
3200
      unsigned long symindx = 0;
2826
 
3201
 
2827
      /* elf_group_id will have been set up by objcopy and the
3202
      /* elf_group_id will have been set up by objcopy and the
2828
	 generic linker.  */
3203
	 generic linker.  */
2829
      if (elf_group_id (sec) != NULL)
3204
      if (elf_group_id (sec) != NULL)
2830
	symindx = elf_group_id (sec)->udata.i;
3205
	symindx = elf_group_id (sec)->udata.i;
2831
 
3206
 
2832
      if (symindx == 0)
3207
      if (symindx == 0)
2833
	{
3208
	{
2834
	  /* If called from the assembler, swap_out_syms will have set up
3209
	  /* If called from the assembler, swap_out_syms will have set up
2835
	     elf_section_syms.  */
3210
	     elf_section_syms.  */
2836
	  BFD_ASSERT (elf_section_syms (abfd) != NULL);
3211
	  BFD_ASSERT (elf_section_syms (abfd) != NULL);
2837
	  symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3212
	  symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2838
	}
3213
	}
2839
      elf_section_data (sec)->this_hdr.sh_info = symindx;
3214
      elf_section_data (sec)->this_hdr.sh_info = symindx;
2840
    }
3215
    }
2841
  else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3216
  else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
2842
    {
3217
    {
2843
      /* The ELF backend linker sets sh_info to -2 when the group
3218
      /* The ELF backend linker sets sh_info to -2 when the group
2844
	 signature symbol is global, and thus the index can't be
3219
	 signature symbol is global, and thus the index can't be
2845
	 set until all local symbols are output.  */
3220
	 set until all local symbols are output.  */
2846
      asection *igroup = elf_sec_group (elf_next_in_group (sec));
3221
      asection *igroup = elf_sec_group (elf_next_in_group (sec));
2847
      struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
3222
      struct bfd_elf_section_data *sec_data = elf_section_data (igroup);
2848
      unsigned long symndx = sec_data->this_hdr.sh_info;
3223
      unsigned long symndx = sec_data->this_hdr.sh_info;
2849
      unsigned long extsymoff = 0;
3224
      unsigned long extsymoff = 0;
2850
      struct elf_link_hash_entry *h;
3225
      struct elf_link_hash_entry *h;
2851
 
3226
 
2852
      if (!elf_bad_symtab (igroup->owner))
3227
      if (!elf_bad_symtab (igroup->owner))
2853
	{
3228
	{
2854
	  Elf_Internal_Shdr *symtab_hdr;
3229
	  Elf_Internal_Shdr *symtab_hdr;
2855
 
3230
 
2856
	  symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3231
	  symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
2857
	  extsymoff = symtab_hdr->sh_info;
3232
	  extsymoff = symtab_hdr->sh_info;
2858
	}
3233
	}
2859
      h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3234
      h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
2860
      while (h->root.type == bfd_link_hash_indirect
3235
      while (h->root.type == bfd_link_hash_indirect
2861
	     || h->root.type == bfd_link_hash_warning)
3236
	     || h->root.type == bfd_link_hash_warning)
2862
	h = (struct elf_link_hash_entry *) h->root.u.i.link;
3237
	h = (struct elf_link_hash_entry *) h->root.u.i.link;
2863
 
3238
 
2864
      elf_section_data (sec)->this_hdr.sh_info = h->indx;
3239
      elf_section_data (sec)->this_hdr.sh_info = h->indx;
2865
    }
3240
    }
2866
 
3241
 
2867
  /* The contents won't be allocated for "ld -r" or objcopy.  */
3242
  /* The contents won't be allocated for "ld -r" or objcopy.  */
2868
  gas = TRUE;
3243
  gas = TRUE;
2869
  if (sec->contents == NULL)
3244
  if (sec->contents == NULL)
2870
    {
3245
    {
2871
      gas = FALSE;
3246
      gas = FALSE;
2872
      sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3247
      sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
2873
 
3248
 
2874
      /* Arrange for the section to be written out.  */
3249
      /* Arrange for the section to be written out.  */
2875
      elf_section_data (sec)->this_hdr.contents = sec->contents;
3250
      elf_section_data (sec)->this_hdr.contents = sec->contents;
2876
      if (sec->contents == NULL)
3251
      if (sec->contents == NULL)
2877
	{
3252
	{
2878
	  *failedptr = TRUE;
3253
	  *failedptr = TRUE;
2879
	  return;
3254
	  return;
2880
	}
3255
	}
2881
    }
3256
    }
2882
 
3257
 
2883
  loc = sec->contents + sec->size;
3258
  loc = sec->contents + sec->size;
2884
 
3259
 
2885
  /* Get the pointer to the first section in the group that gas
3260
  /* Get the pointer to the first section in the group that gas
2886
     squirreled away here.  objcopy arranges for this to be set to the
3261
     squirreled away here.  objcopy arranges for this to be set to the
2887
     start of the input section group.  */
3262
     start of the input section group.  */
2888
  first = elt = elf_next_in_group (sec);
3263
  first = elt = elf_next_in_group (sec);
2889
 
3264
 
2890
  /* First element is a flag word.  Rest of section is elf section
3265
  /* First element is a flag word.  Rest of section is elf section
2891
     indices for all the sections of the group.  Write them backwards
3266
     indices for all the sections of the group.  Write them backwards
2892
     just to keep the group in the same order as given in .section
3267
     just to keep the group in the same order as given in .section
2893
     directives, not that it matters.  */
3268
     directives, not that it matters.  */
2894
  while (elt != NULL)
3269
  while (elt != NULL)
2895
    {
3270
    {
2896
      asection *s;
3271
      asection *s;
2897
 
3272
 
2898
      s = elt;
3273
      s = elt;
2899
      if (!gas)
3274
      if (!gas)
2900
	s = s->output_section;
3275
	s = s->output_section;
2901
      if (s != NULL
3276
      if (s != NULL
2902
	  && !bfd_is_abs_section (s))
3277
	  && !bfd_is_abs_section (s))
2903
	{
3278
	{
2904
	  unsigned int idx = elf_section_data (s)->this_idx;
3279
	  unsigned int idx = elf_section_data (s)->this_idx;
2905
 
3280
 
2906
	  loc -= 4;
3281
	  loc -= 4;
2907
	  H_PUT_32 (abfd, idx, loc);
3282
	  H_PUT_32 (abfd, idx, loc);
2908
	}
3283
	}
2909
      elt = elf_next_in_group (elt);
3284
      elt = elf_next_in_group (elt);
2910
      if (elt == first)
3285
      if (elt == first)
2911
	break;
3286
	break;
2912
    }
3287
    }
2913
 
3288
 
2914
  if ((loc -= 4) != sec->contents)
3289
  if ((loc -= 4) != sec->contents)
2915
    abort ();
3290
    abort ();
2916
 
3291
 
2917
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
3292
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2918
}
3293
}
-
 
3294
 
-
 
3295
/* Return the section which RELOC_SEC applies to.  */
-
 
3296
 
-
 
3297
asection *
-
 
3298
_bfd_elf_get_reloc_section (asection *reloc_sec)
-
 
3299
{
-
 
3300
  const char *name;
-
 
3301
  unsigned int type;
-
 
3302
  bfd *abfd;
-
 
3303
 
-
 
3304
  if (reloc_sec == NULL)
-
 
3305
    return NULL;
-
 
3306
 
-
 
3307
  type = elf_section_data (reloc_sec)->this_hdr.sh_type;
-
 
3308
  if (type != SHT_REL && type != SHT_RELA)
-
 
3309
    return NULL;
-
 
3310
 
-
 
3311
  /* We look up the section the relocs apply to by name.  */
-
 
3312
  name = reloc_sec->name;
-
 
3313
  if (type == SHT_REL)
-
 
3314
    name += 4;
-
 
3315
  else
-
 
3316
    name += 5;
-
 
3317
 
-
 
3318
  /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
-
 
3319
     section apply to .got.plt section.  */
-
 
3320
  abfd = reloc_sec->owner;
-
 
3321
  if (get_elf_backend_data (abfd)->want_got_plt
-
 
3322
      && strcmp (name, ".plt") == 0)
-
 
3323
    {
-
 
3324
      /* .got.plt is a linker created input section.  It may be mapped
-
 
3325
	 to some other output section.  Try two likely sections.  */
-
 
3326
      name = ".got.plt";
-
 
3327
      reloc_sec = bfd_get_section_by_name (abfd, name);
-
 
3328
      if (reloc_sec != NULL)
-
 
3329
	return reloc_sec;
-
 
3330
      name = ".got";
-
 
3331
    }
-
 
3332
 
-
 
3333
  reloc_sec = bfd_get_section_by_name (abfd, name);
-
 
3334
  return reloc_sec;
-
 
3335
}
2919
 
3336
 
2920
/* Assign all ELF section numbers.  The dummy first section is handled here
3337
/* Assign all ELF section numbers.  The dummy first section is handled here
2921
   too.  The link/info pointers for the standard section types are filled
3338
   too.  The link/info pointers for the standard section types are filled
2922
   in here too, while we're at it.  */
3339
   in here too, while we're at it.  */
2923
 
3340
 
2924
static bfd_boolean
3341
static bfd_boolean
2925
assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
3342
assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
2926
{
3343
{
2927
  struct elf_obj_tdata *t = elf_tdata (abfd);
3344
  struct elf_obj_tdata *t = elf_tdata (abfd);
2928
  asection *sec;
3345
  asection *sec;
2929
  unsigned int section_number, secn;
3346
  unsigned int section_number;
2930
  Elf_Internal_Shdr **i_shdrp;
3347
  Elf_Internal_Shdr **i_shdrp;
2931
  struct bfd_elf_section_data *d;
3348
  struct bfd_elf_section_data *d;
2932
  bfd_boolean need_symtab;
3349
  bfd_boolean need_symtab;
2933
 
3350
 
2934
  section_number = 1;
3351
  section_number = 1;
2935
 
3352
 
2936
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
3353
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2937
 
3354
 
2938
  /* SHT_GROUP sections are in relocatable files only.  */
3355
  /* SHT_GROUP sections are in relocatable files only.  */
2939
  if (link_info == NULL || link_info->relocatable)
3356
  if (link_info == NULL || bfd_link_relocatable (link_info))
2940
    {
3357
    {
2941
      /* Put SHT_GROUP sections first.  */
3358
      /* Put SHT_GROUP sections first.  */
2942
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
3359
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
2943
	{
3360
	{
2944
	  d = elf_section_data (sec);
3361
	  d = elf_section_data (sec);
2945
 
3362
 
2946
	  if (d->this_hdr.sh_type == SHT_GROUP)
3363
	  if (d->this_hdr.sh_type == SHT_GROUP)
2947
	    {
3364
	    {
2948
	      if (sec->flags & SEC_LINKER_CREATED)
3365
	      if (sec->flags & SEC_LINKER_CREATED)
2949
		{
3366
		{
2950
		  /* Remove the linker created SHT_GROUP sections.  */
3367
		  /* Remove the linker created SHT_GROUP sections.  */
2951
		  bfd_section_list_remove (abfd, sec);
3368
		  bfd_section_list_remove (abfd, sec);
2952
		  abfd->section_count--;
3369
		  abfd->section_count--;
2953
		}
3370
		}
2954
	      else
3371
	      else
2955
		d->this_idx = section_number++;
3372
		d->this_idx = section_number++;
2956
	    }
3373
	    }
2957
	}
3374
	}
2958
    }
3375
    }
2959
 
3376
 
2960
  for (sec = abfd->sections; sec; sec = sec->next)
3377
  for (sec = abfd->sections; sec; sec = sec->next)
2961
    {
3378
    {
2962
      d = elf_section_data (sec);
3379
      d = elf_section_data (sec);
2963
 
3380
 
2964
      if (d->this_hdr.sh_type != SHT_GROUP)
3381
      if (d->this_hdr.sh_type != SHT_GROUP)
2965
	d->this_idx = section_number++;
3382
	d->this_idx = section_number++;
-
 
3383
      if (d->this_hdr.sh_name != (unsigned int) -1)
2966
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
3384
	_bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2967
      if (d->rel.hdr)
3385
      if (d->rel.hdr)
2968
	{
3386
	{
2969
	  d->rel.idx = section_number++;
3387
	  d->rel.idx = section_number++;
-
 
3388
	  if (d->rel.hdr->sh_name != (unsigned int) -1)
2970
	  _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
3389
	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
2971
	}
3390
	}
2972
      else
3391
      else
2973
	d->rel.idx = 0;
3392
	d->rel.idx = 0;
2974
 
3393
 
2975
      if (d->rela.hdr)
3394
      if (d->rela.hdr)
2976
	{
3395
	{
2977
	  d->rela.idx = section_number++;
3396
	  d->rela.idx = section_number++;
-
 
3397
	  if (d->rela.hdr->sh_name != (unsigned int) -1)
2978
	  _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
3398
	    _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
2979
	}
3399
	}
2980
      else
3400
      else
2981
	d->rela.idx = 0;
3401
	d->rela.idx = 0;
2982
    }
3402
    }
2983
 
3403
 
2984
  elf_shstrtab_sec (abfd) = section_number++;
3404
  elf_shstrtab_sec (abfd) = section_number++;
2985
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
3405
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2986
  elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
3406
  elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
2987
 
3407
 
2988
  need_symtab = (bfd_get_symcount (abfd) > 0
3408
  need_symtab = (bfd_get_symcount (abfd) > 0
2989
		|| (link_info == NULL
3409
		|| (link_info == NULL
2990
		    && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3410
		    && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
2991
			== HAS_RELOC)));
3411
			== HAS_RELOC)));
2992
  if (need_symtab)
3412
  if (need_symtab)
2993
    {
3413
    {
2994
      elf_onesymtab (abfd) = section_number++;
3414
      elf_onesymtab (abfd) = section_number++;
2995
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
3415
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2996
      if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
3416
      if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
2997
	{
3417
	{
-
 
3418
	  elf_section_list * entry;
-
 
3419
 
-
 
3420
	  BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
-
 
3421
 
-
 
3422
	  entry = bfd_zalloc (abfd, sizeof * entry);
-
 
3423
	  entry->ndx = section_number++;
2998
	  elf_symtab_shndx (abfd) = section_number++;
3424
	  elf_symtab_shndx_list (abfd) = entry;
2999
	  t->symtab_shndx_hdr.sh_name
3425
	  entry->hdr.sh_name
3000
	    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3426
	    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3001
						  ".symtab_shndx", FALSE);
3427
						  ".symtab_shndx", FALSE);
3002
	  if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
3428
	  if (entry->hdr.sh_name == (unsigned int) -1)
3003
	    return FALSE;
3429
	    return FALSE;
3004
	}
3430
	}
3005
      elf_strtab_sec (abfd) = section_number++;
3431
      elf_strtab_sec (abfd) = section_number++;
3006
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3432
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
3007
    }
3433
    }
3008
 
3434
 
3009
  if (section_number >= SHN_LORESERVE)
3435
  if (section_number >= SHN_LORESERVE)
3010
    {
3436
    {
3011
      _bfd_error_handler (_("%B: too many sections: %u"),
3437
      _bfd_error_handler (_("%B: too many sections: %u"),
3012
			  abfd, section_number);
3438
			  abfd, section_number);
3013
      return FALSE;
3439
      return FALSE;
3014
    }
3440
    }
3015
 
-
 
3016
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
-
 
3017
  t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
-
 
3018
 
3441
 
3019
  elf_numsections (abfd) = section_number;
3442
  elf_numsections (abfd) = section_number;
3020
  elf_elfheader (abfd)->e_shnum = section_number;
3443
  elf_elfheader (abfd)->e_shnum = section_number;
3021
 
3444
 
3022
  /* Set up the list of section header pointers, in agreement with the
3445
  /* Set up the list of section header pointers, in agreement with the
3023
     indices.  */
3446
     indices.  */
3024
  i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3447
  i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc2 (abfd, section_number,
3025
                                                sizeof (Elf_Internal_Shdr *));
3448
                                                sizeof (Elf_Internal_Shdr *));
3026
  if (i_shdrp == NULL)
3449
  if (i_shdrp == NULL)
3027
    return FALSE;
3450
    return FALSE;
3028
 
3451
 
3029
  i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3452
  i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
3030
                                                 sizeof (Elf_Internal_Shdr));
3453
                                                 sizeof (Elf_Internal_Shdr));
3031
  if (i_shdrp[0] == NULL)
3454
  if (i_shdrp[0] == NULL)
3032
    {
3455
    {
3033
      bfd_release (abfd, i_shdrp);
3456
      bfd_release (abfd, i_shdrp);
3034
      return FALSE;
3457
      return FALSE;
3035
    }
3458
    }
3036
 
3459
 
3037
  elf_elfsections (abfd) = i_shdrp;
3460
  elf_elfsections (abfd) = i_shdrp;
3038
 
3461
 
3039
  i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3462
  i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
3040
  if (need_symtab)
3463
  if (need_symtab)
3041
    {
3464
    {
3042
      i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3465
      i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
3043
      if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3466
      if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
3044
	{
3467
	{
3045
	  i_shdrp[elf_symtab_shndx (abfd)] = &t->symtab_shndx_hdr;
3468
	  elf_section_list * entry = elf_symtab_shndx_list (abfd);
-
 
3469
	  BFD_ASSERT (entry != NULL);
-
 
3470
	  i_shdrp[entry->ndx] = & entry->hdr;
3046
	  t->symtab_shndx_hdr.sh_link = elf_onesymtab (abfd);
3471
	  entry->hdr.sh_link = elf_onesymtab (abfd);
3047
	}
3472
	}
3048
      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3473
      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
3049
      t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3474
      t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
3050
    }
3475
    }
3051
 
3476
 
3052
  for (sec = abfd->sections; sec; sec = sec->next)
3477
  for (sec = abfd->sections; sec; sec = sec->next)
3053
    {
3478
    {
3054
      asection *s;
3479
      asection *s;
3055
      const char *name;
-
 
3056
 
3480
 
3057
      d = elf_section_data (sec);
3481
      d = elf_section_data (sec);
3058
 
3482
 
3059
      i_shdrp[d->this_idx] = &d->this_hdr;
3483
      i_shdrp[d->this_idx] = &d->this_hdr;
3060
      if (d->rel.idx != 0)
3484
      if (d->rel.idx != 0)
3061
	i_shdrp[d->rel.idx] = d->rel.hdr;
3485
	i_shdrp[d->rel.idx] = d->rel.hdr;
3062
      if (d->rela.idx != 0)
3486
      if (d->rela.idx != 0)
3063
	i_shdrp[d->rela.idx] = d->rela.hdr;
3487
	i_shdrp[d->rela.idx] = d->rela.hdr;
3064
 
3488
 
3065
      /* Fill in the sh_link and sh_info fields while we're at it.  */
3489
      /* Fill in the sh_link and sh_info fields while we're at it.  */
3066
 
3490
 
3067
      /* sh_link of a reloc section is the section index of the symbol
3491
      /* sh_link of a reloc section is the section index of the symbol
3068
	 table.  sh_info is the section index of the section to which
3492
	 table.  sh_info is the section index of the section to which
3069
	 the relocation entries apply.  */
3493
	 the relocation entries apply.  */
3070
      if (d->rel.idx != 0)
3494
      if (d->rel.idx != 0)
3071
	{
3495
	{
3072
	  d->rel.hdr->sh_link = elf_onesymtab (abfd);
3496
	  d->rel.hdr->sh_link = elf_onesymtab (abfd);
3073
	  d->rel.hdr->sh_info = d->this_idx;
3497
	  d->rel.hdr->sh_info = d->this_idx;
-
 
3498
	  d->rel.hdr->sh_flags |= SHF_INFO_LINK;
3074
	}
3499
	}
3075
      if (d->rela.idx != 0)
3500
      if (d->rela.idx != 0)
3076
	{
3501
	{
3077
	  d->rela.hdr->sh_link = elf_onesymtab (abfd);
3502
	  d->rela.hdr->sh_link = elf_onesymtab (abfd);
3078
	  d->rela.hdr->sh_info = d->this_idx;
3503
	  d->rela.hdr->sh_info = d->this_idx;
-
 
3504
	  d->rela.hdr->sh_flags |= SHF_INFO_LINK;
3079
	}
3505
	}
3080
 
3506
 
3081
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
3507
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
3082
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3508
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
3083
	{
3509
	{
3084
	  s = elf_linked_to_section (sec);
3510
	  s = elf_linked_to_section (sec);
3085
	  if (s)
3511
	  if (s)
3086
	    {
3512
	    {
3087
	      /* elf_linked_to_section points to the input section.  */
3513
	      /* elf_linked_to_section points to the input section.  */
3088
	      if (link_info != NULL)
3514
	      if (link_info != NULL)
3089
		{
3515
		{
3090
		  /* Check discarded linkonce section.  */
3516
		  /* Check discarded linkonce section.  */
3091
		  if (discarded_section (s))
3517
		  if (discarded_section (s))
3092
		    {
3518
		    {
3093
		      asection *kept;
3519
		      asection *kept;
3094
		      (*_bfd_error_handler)
3520
		      (*_bfd_error_handler)
3095
			(_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3521
			(_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"),
3096
			 abfd, d->this_hdr.bfd_section,
3522
			 abfd, d->this_hdr.bfd_section,
3097
			 s, s->owner);
3523
			 s, s->owner);
3098
		      /* Point to the kept section if it has the same
3524
		      /* Point to the kept section if it has the same
3099
			 size as the discarded one.  */
3525
			 size as the discarded one.  */
3100
		      kept = _bfd_elf_check_kept_section (s, link_info);
3526
		      kept = _bfd_elf_check_kept_section (s, link_info);
3101
		      if (kept == NULL)
3527
		      if (kept == NULL)
3102
			{
3528
			{
3103
			  bfd_set_error (bfd_error_bad_value);
3529
			  bfd_set_error (bfd_error_bad_value);
3104
			  return FALSE;
3530
			  return FALSE;
3105
			}
3531
			}
3106
		      s = kept;
3532
		      s = kept;
3107
		    }
3533
		    }
3108
 
3534
 
3109
		  s = s->output_section;
3535
		  s = s->output_section;
3110
		  BFD_ASSERT (s != NULL);
3536
		  BFD_ASSERT (s != NULL);
3111
		}
3537
		}
3112
	      else
3538
	      else
3113
		{
3539
		{
3114
		  /* Handle objcopy. */
3540
		  /* Handle objcopy. */
3115
		  if (s->output_section == NULL)
3541
		  if (s->output_section == NULL)
3116
		    {
3542
		    {
3117
		      (*_bfd_error_handler)
3543
		      (*_bfd_error_handler)
3118
			(_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3544
			(_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"),
3119
			 abfd, d->this_hdr.bfd_section, s, s->owner);
3545
			 abfd, d->this_hdr.bfd_section, s, s->owner);
3120
		      bfd_set_error (bfd_error_bad_value);
3546
		      bfd_set_error (bfd_error_bad_value);
3121
		      return FALSE;
3547
		      return FALSE;
3122
		    }
3548
		    }
3123
		  s = s->output_section;
3549
		  s = s->output_section;
3124
		}
3550
		}
3125
	      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3551
	      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3126
	    }
3552
	    }
3127
	  else
3553
	  else
3128
	    {
3554
	    {
3129
	      /* PR 290:
3555
	      /* PR 290:
3130
		 The Intel C compiler generates SHT_IA_64_UNWIND with
3556
		 The Intel C compiler generates SHT_IA_64_UNWIND with
3131
		 SHF_LINK_ORDER.  But it doesn't set the sh_link or
3557
		 SHF_LINK_ORDER.  But it doesn't set the sh_link or
3132
		 sh_info fields.  Hence we could get the situation
3558
		 sh_info fields.  Hence we could get the situation
3133
		 where s is NULL.  */
3559
		 where s is NULL.  */
3134
	      const struct elf_backend_data *bed
3560
	      const struct elf_backend_data *bed
3135
		= get_elf_backend_data (abfd);
3561
		= get_elf_backend_data (abfd);
3136
	      if (bed->link_order_error_handler)
3562
	      if (bed->link_order_error_handler)
3137
		bed->link_order_error_handler
3563
		bed->link_order_error_handler
3138
		  (_("%B: warning: sh_link not set for section `%A'"),
3564
		  (_("%B: warning: sh_link not set for section `%A'"),
3139
		   abfd, sec);
3565
		   abfd, sec);
3140
	    }
3566
	    }
3141
	}
3567
	}
3142
 
3568
 
3143
      switch (d->this_hdr.sh_type)
3569
      switch (d->this_hdr.sh_type)
3144
	{
3570
	{
3145
	case SHT_REL:
3571
	case SHT_REL:
3146
	case SHT_RELA:
3572
	case SHT_RELA:
3147
	  /* A reloc section which we are treating as a normal BFD
3573
	  /* A reloc section which we are treating as a normal BFD
3148
	     section.  sh_link is the section index of the symbol
3574
	     section.  sh_link is the section index of the symbol
3149
	     table.  sh_info is the section index of the section to
3575
	     table.  sh_info is the section index of the section to
3150
	     which the relocation entries apply.  We assume that an
3576
	     which the relocation entries apply.  We assume that an
3151
	     allocated reloc section uses the dynamic symbol table.
3577
	     allocated reloc section uses the dynamic symbol table.
3152
	     FIXME: How can we be sure?  */
3578
	     FIXME: How can we be sure?  */
3153
	  s = bfd_get_section_by_name (abfd, ".dynsym");
3579
	  s = bfd_get_section_by_name (abfd, ".dynsym");
3154
	  if (s != NULL)
3580
	  if (s != NULL)
3155
	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3581
	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3156
 
-
 
3157
	  /* We look up the section the relocs apply to by name.  */
-
 
3158
	  name = sec->name;
-
 
3159
	  if (d->this_hdr.sh_type == SHT_REL)
-
 
3160
	    name += 4;
-
 
3161
	  else
-
 
3162
	    name += 5;
3582
 
3163
	  s = bfd_get_section_by_name (abfd, name);
3583
	  s = get_elf_backend_data (abfd)->get_reloc_section (sec);
-
 
3584
	  if (s != NULL)
3164
	  if (s != NULL)
3585
	    {
-
 
3586
	      d->this_hdr.sh_info = elf_section_data (s)->this_idx;
-
 
3587
	      d->this_hdr.sh_flags |= SHF_INFO_LINK;
3165
	    d->this_hdr.sh_info = elf_section_data (s)->this_idx;
3588
	    }
3166
	  break;
3589
	  break;
3167
 
3590
 
3168
	case SHT_STRTAB:
3591
	case SHT_STRTAB:
3169
	  /* We assume that a section named .stab*str is a stabs
3592
	  /* We assume that a section named .stab*str is a stabs
3170
	     string section.  We look for a section with the same name
3593
	     string section.  We look for a section with the same name
3171
	     but without the trailing ``str'', and set its sh_link
3594
	     but without the trailing ``str'', and set its sh_link
3172
	     field to point to this section.  */
3595
	     field to point to this section.  */
3173
	  if (CONST_STRNEQ (sec->name, ".stab")
3596
	  if (CONST_STRNEQ (sec->name, ".stab")
3174
	      && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3597
	      && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
3175
	    {
3598
	    {
3176
	      size_t len;
3599
	      size_t len;
3177
	      char *alc;
3600
	      char *alc;
3178
 
3601
 
3179
	      len = strlen (sec->name);
3602
	      len = strlen (sec->name);
3180
	      alc = (char *) bfd_malloc (len - 2);
3603
	      alc = (char *) bfd_malloc (len - 2);
3181
	      if (alc == NULL)
3604
	      if (alc == NULL)
3182
		return FALSE;
3605
		return FALSE;
3183
	      memcpy (alc, sec->name, len - 3);
3606
	      memcpy (alc, sec->name, len - 3);
3184
	      alc[len - 3] = '\0';
3607
	      alc[len - 3] = '\0';
3185
	      s = bfd_get_section_by_name (abfd, alc);
3608
	      s = bfd_get_section_by_name (abfd, alc);
3186
	      free (alc);
3609
	      free (alc);
3187
	      if (s != NULL)
3610
	      if (s != NULL)
3188
		{
3611
		{
3189
		  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3612
		  elf_section_data (s)->this_hdr.sh_link = d->this_idx;
3190
 
3613
 
3191
		  /* This is a .stab section.  */
3614
		  /* This is a .stab section.  */
3192
		  if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3615
		  if (elf_section_data (s)->this_hdr.sh_entsize == 0)
3193
		    elf_section_data (s)->this_hdr.sh_entsize
3616
		    elf_section_data (s)->this_hdr.sh_entsize
3194
		      = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3617
		      = 4 + 2 * bfd_get_arch_size (abfd) / 8;
3195
		}
3618
		}
3196
	    }
3619
	    }
3197
	  break;
3620
	  break;
3198
 
3621
 
3199
	case SHT_DYNAMIC:
3622
	case SHT_DYNAMIC:
3200
	case SHT_DYNSYM:
3623
	case SHT_DYNSYM:
3201
	case SHT_GNU_verneed:
3624
	case SHT_GNU_verneed:
3202
	case SHT_GNU_verdef:
3625
	case SHT_GNU_verdef:
3203
	  /* sh_link is the section header index of the string table
3626
	  /* sh_link is the section header index of the string table
3204
	     used for the dynamic entries, or the symbol table, or the
3627
	     used for the dynamic entries, or the symbol table, or the
3205
	     version strings.  */
3628
	     version strings.  */
3206
	  s = bfd_get_section_by_name (abfd, ".dynstr");
3629
	  s = bfd_get_section_by_name (abfd, ".dynstr");
3207
	  if (s != NULL)
3630
	  if (s != NULL)
3208
	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3631
	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3209
	  break;
3632
	  break;
3210
 
3633
 
3211
	case SHT_GNU_LIBLIST:
3634
	case SHT_GNU_LIBLIST:
3212
	  /* sh_link is the section header index of the prelink library
3635
	  /* sh_link is the section header index of the prelink library
3213
	     list used for the dynamic entries, or the symbol table, or
3636
	     list used for the dynamic entries, or the symbol table, or
3214
	     the version strings.  */
3637
	     the version strings.  */
3215
	  s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3638
	  s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC)
3216
					     ? ".dynstr" : ".gnu.libstr");
3639
					     ? ".dynstr" : ".gnu.libstr");
3217
	  if (s != NULL)
3640
	  if (s != NULL)
3218
	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3641
	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3219
	  break;
3642
	  break;
3220
 
3643
 
3221
	case SHT_HASH:
3644
	case SHT_HASH:
3222
	case SHT_GNU_HASH:
3645
	case SHT_GNU_HASH:
3223
	case SHT_GNU_versym:
3646
	case SHT_GNU_versym:
3224
	  /* sh_link is the section header index of the symbol table
3647
	  /* sh_link is the section header index of the symbol table
3225
	     this hash table or version table is for.  */
3648
	     this hash table or version table is for.  */
3226
	  s = bfd_get_section_by_name (abfd, ".dynsym");
3649
	  s = bfd_get_section_by_name (abfd, ".dynsym");
3227
	  if (s != NULL)
3650
	  if (s != NULL)
3228
	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3651
	    d->this_hdr.sh_link = elf_section_data (s)->this_idx;
3229
	  break;
3652
	  break;
3230
 
3653
 
3231
	case SHT_GROUP:
3654
	case SHT_GROUP:
3232
	  d->this_hdr.sh_link = elf_onesymtab (abfd);
3655
	  d->this_hdr.sh_link = elf_onesymtab (abfd);
3233
	}
3656
	}
3234
    }
3657
    }
3235
 
3658
 
3236
  for (secn = 1; secn < section_number; ++secn)
3659
  /* Delay setting sh_name to _bfd_elf_write_object_contents so that
3237
    if (i_shdrp[secn] == NULL)
3660
     _bfd_elf_assign_file_positions_for_non_load can convert DWARF
3238
      i_shdrp[secn] = i_shdrp[0];
3661
     debug section name from .debug_* to .zdebug_* if needed.  */
3239
    else
-
 
3240
      i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
-
 
3241
						       i_shdrp[secn]->sh_name);
3662
 
3242
  return TRUE;
3663
  return TRUE;
3243
}
3664
}
3244
 
3665
 
3245
static bfd_boolean
3666
static bfd_boolean
3246
sym_is_global (bfd *abfd, asymbol *sym)
3667
sym_is_global (bfd *abfd, asymbol *sym)
3247
{
3668
{
3248
  /* If the backend has a special mapping, use it.  */
3669
  /* If the backend has a special mapping, use it.  */
3249
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3670
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3250
  if (bed->elf_backend_sym_is_global)
3671
  if (bed->elf_backend_sym_is_global)
3251
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
3672
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
3252
 
3673
 
3253
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3674
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
3254
	  || bfd_is_und_section (bfd_get_section (sym))
3675
	  || bfd_is_und_section (bfd_get_section (sym))
3255
	  || bfd_is_com_section (bfd_get_section (sym)));
3676
	  || bfd_is_com_section (bfd_get_section (sym)));
3256
}
3677
}
3257
 
3678
 
3258
/* Don't output section symbols for sections that are not going to be
3679
/* Don't output section symbols for sections that are not going to be
3259
   output, that are duplicates or there is no BFD section.  */
3680
   output, that are duplicates or there is no BFD section.  */
3260
 
3681
 
3261
static bfd_boolean
3682
static bfd_boolean
3262
ignore_section_sym (bfd *abfd, asymbol *sym)
3683
ignore_section_sym (bfd *abfd, asymbol *sym)
3263
{
3684
{
3264
  elf_symbol_type *type_ptr;
3685
  elf_symbol_type *type_ptr;
3265
 
3686
 
3266
  if ((sym->flags & BSF_SECTION_SYM) == 0)
3687
  if ((sym->flags & BSF_SECTION_SYM) == 0)
3267
    return FALSE;
3688
    return FALSE;
3268
 
3689
 
3269
  type_ptr = elf_symbol_from (abfd, sym);
3690
  type_ptr = elf_symbol_from (abfd, sym);
3270
  return ((type_ptr != NULL
3691
  return ((type_ptr != NULL
3271
	   && type_ptr->internal_elf_sym.st_shndx != 0
3692
	   && type_ptr->internal_elf_sym.st_shndx != 0
3272
	   && bfd_is_abs_section (sym->section))
3693
	   && bfd_is_abs_section (sym->section))
3273
	  || !(sym->section->owner == abfd
3694
	  || !(sym->section->owner == abfd
3274
	       || (sym->section->output_section->owner == abfd
3695
	       || (sym->section->output_section->owner == abfd
3275
		   && sym->section->output_offset == 0)
3696
		   && sym->section->output_offset == 0)
3276
	       || bfd_is_abs_section (sym->section)));
3697
	       || bfd_is_abs_section (sym->section)));
3277
}
3698
}
3278
 
3699
 
3279
/* Map symbol from it's internal number to the external number, moving
3700
/* Map symbol from it's internal number to the external number, moving
3280
   all local symbols to be at the head of the list.  */
3701
   all local symbols to be at the head of the list.  */
3281
 
3702
 
3282
static bfd_boolean
3703
static bfd_boolean
3283
elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
3704
elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
3284
{
3705
{
3285
  unsigned int symcount = bfd_get_symcount (abfd);
3706
  unsigned int symcount = bfd_get_symcount (abfd);
3286
  asymbol **syms = bfd_get_outsymbols (abfd);
3707
  asymbol **syms = bfd_get_outsymbols (abfd);
3287
  asymbol **sect_syms;
3708
  asymbol **sect_syms;
3288
  unsigned int num_locals = 0;
3709
  unsigned int num_locals = 0;
3289
  unsigned int num_globals = 0;
3710
  unsigned int num_globals = 0;
3290
  unsigned int num_locals2 = 0;
3711
  unsigned int num_locals2 = 0;
3291
  unsigned int num_globals2 = 0;
3712
  unsigned int num_globals2 = 0;
3292
  int max_index = 0;
3713
  unsigned int max_index = 0;
3293
  unsigned int idx;
3714
  unsigned int idx;
3294
  asection *asect;
3715
  asection *asect;
3295
  asymbol **new_syms;
3716
  asymbol **new_syms;
3296
 
3717
 
3297
#ifdef DEBUG
3718
#ifdef DEBUG
3298
  fprintf (stderr, "elf_map_symbols\n");
3719
  fprintf (stderr, "elf_map_symbols\n");
3299
  fflush (stderr);
3720
  fflush (stderr);
3300
#endif
3721
#endif
3301
 
3722
 
3302
  for (asect = abfd->sections; asect; asect = asect->next)
3723
  for (asect = abfd->sections; asect; asect = asect->next)
3303
    {
3724
    {
3304
      if (max_index < asect->index)
3725
      if (max_index < asect->index)
3305
	max_index = asect->index;
3726
	max_index = asect->index;
3306
    }
3727
    }
3307
 
3728
 
3308
  max_index++;
3729
  max_index++;
3309
  sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3730
  sect_syms = (asymbol **) bfd_zalloc2 (abfd, max_index, sizeof (asymbol *));
3310
  if (sect_syms == NULL)
3731
  if (sect_syms == NULL)
3311
    return FALSE;
3732
    return FALSE;
3312
  elf_section_syms (abfd) = sect_syms;
3733
  elf_section_syms (abfd) = sect_syms;
3313
  elf_num_section_syms (abfd) = max_index;
3734
  elf_num_section_syms (abfd) = max_index;
3314
 
3735
 
3315
  /* Init sect_syms entries for any section symbols we have already
3736
  /* Init sect_syms entries for any section symbols we have already
3316
     decided to output.  */
3737
     decided to output.  */
3317
  for (idx = 0; idx < symcount; idx++)
3738
  for (idx = 0; idx < symcount; idx++)
3318
    {
3739
    {
3319
      asymbol *sym = syms[idx];
3740
      asymbol *sym = syms[idx];
3320
 
3741
 
3321
      if ((sym->flags & BSF_SECTION_SYM) != 0
3742
      if ((sym->flags & BSF_SECTION_SYM) != 0
3322
	  && sym->value == 0
3743
	  && sym->value == 0
3323
	  && !ignore_section_sym (abfd, sym)
3744
	  && !ignore_section_sym (abfd, sym)
3324
	  && !bfd_is_abs_section (sym->section))
3745
	  && !bfd_is_abs_section (sym->section))
3325
	{
3746
	{
3326
	  asection *sec = sym->section;
3747
	  asection *sec = sym->section;
3327
 
3748
 
3328
	  if (sec->owner != abfd)
3749
	  if (sec->owner != abfd)
3329
	    sec = sec->output_section;
3750
	    sec = sec->output_section;
3330
 
3751
 
3331
	  sect_syms[sec->index] = syms[idx];
3752
	  sect_syms[sec->index] = syms[idx];
3332
	}
3753
	}
3333
    }
3754
    }
3334
 
3755
 
3335
  /* Classify all of the symbols.  */
3756
  /* Classify all of the symbols.  */
3336
  for (idx = 0; idx < symcount; idx++)
3757
  for (idx = 0; idx < symcount; idx++)
3337
    {
3758
    {
3338
      if (sym_is_global (abfd, syms[idx]))
3759
      if (sym_is_global (abfd, syms[idx]))
3339
	num_globals++;
3760
	num_globals++;
3340
      else if (!ignore_section_sym (abfd, syms[idx]))
3761
      else if (!ignore_section_sym (abfd, syms[idx]))
3341
	num_locals++;
3762
	num_locals++;
3342
    }
3763
    }
3343
 
3764
 
3344
  /* We will be adding a section symbol for each normal BFD section.  Most
3765
  /* We will be adding a section symbol for each normal BFD section.  Most
3345
     sections will already have a section symbol in outsymbols, but
3766
     sections will already have a section symbol in outsymbols, but
3346
     eg. SHT_GROUP sections will not, and we need the section symbol mapped
3767
     eg. SHT_GROUP sections will not, and we need the section symbol mapped
3347
     at least in that case.  */
3768
     at least in that case.  */
3348
  for (asect = abfd->sections; asect; asect = asect->next)
3769
  for (asect = abfd->sections; asect; asect = asect->next)
3349
    {
3770
    {
3350
      if (sect_syms[asect->index] == NULL)
3771
      if (sect_syms[asect->index] == NULL)
3351
	{
3772
	{
3352
	  if (!sym_is_global (abfd, asect->symbol))
3773
	  if (!sym_is_global (abfd, asect->symbol))
3353
	    num_locals++;
3774
	    num_locals++;
3354
	  else
3775
	  else
3355
	    num_globals++;
3776
	    num_globals++;
3356
	}
3777
	}
3357
    }
3778
    }
3358
 
3779
 
3359
  /* Now sort the symbols so the local symbols are first.  */
3780
  /* Now sort the symbols so the local symbols are first.  */
3360
  new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3781
  new_syms = (asymbol **) bfd_alloc2 (abfd, num_locals + num_globals,
3361
                                      sizeof (asymbol *));
3782
                                      sizeof (asymbol *));
3362
 
3783
 
3363
  if (new_syms == NULL)
3784
  if (new_syms == NULL)
3364
    return FALSE;
3785
    return FALSE;
3365
 
3786
 
3366
  for (idx = 0; idx < symcount; idx++)
3787
  for (idx = 0; idx < symcount; idx++)
3367
    {
3788
    {
3368
      asymbol *sym = syms[idx];
3789
      asymbol *sym = syms[idx];
3369
      unsigned int i;
3790
      unsigned int i;
3370
 
3791
 
3371
      if (sym_is_global (abfd, sym))
3792
      if (sym_is_global (abfd, sym))
3372
	i = num_locals + num_globals2++;
3793
	i = num_locals + num_globals2++;
3373
      else if (!ignore_section_sym (abfd, sym))
3794
      else if (!ignore_section_sym (abfd, sym))
3374
	i = num_locals2++;
3795
	i = num_locals2++;
3375
      else
3796
      else
3376
	continue;
3797
	continue;
3377
      new_syms[i] = sym;
3798
      new_syms[i] = sym;
3378
      sym->udata.i = i + 1;
3799
      sym->udata.i = i + 1;
3379
    }
3800
    }
3380
  for (asect = abfd->sections; asect; asect = asect->next)
3801
  for (asect = abfd->sections; asect; asect = asect->next)
3381
    {
3802
    {
3382
      if (sect_syms[asect->index] == NULL)
3803
      if (sect_syms[asect->index] == NULL)
3383
	{
3804
	{
3384
	  asymbol *sym = asect->symbol;
3805
	  asymbol *sym = asect->symbol;
3385
	  unsigned int i;
3806
	  unsigned int i;
3386
 
3807
 
3387
	  sect_syms[asect->index] = sym;
3808
	  sect_syms[asect->index] = sym;
3388
	  if (!sym_is_global (abfd, sym))
3809
	  if (!sym_is_global (abfd, sym))
3389
	    i = num_locals2++;
3810
	    i = num_locals2++;
3390
	  else
3811
	  else
3391
	    i = num_locals + num_globals2++;
3812
	    i = num_locals + num_globals2++;
3392
	  new_syms[i] = sym;
3813
	  new_syms[i] = sym;
3393
	  sym->udata.i = i + 1;
3814
	  sym->udata.i = i + 1;
3394
	}
3815
	}
3395
    }
3816
    }
3396
 
3817
 
3397
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3818
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3398
 
3819
 
3399
  *pnum_locals = num_locals;
3820
  *pnum_locals = num_locals;
3400
  return TRUE;
3821
  return TRUE;
3401
}
3822
}
3402
 
3823
 
3403
/* Align to the maximum file alignment that could be required for any
3824
/* Align to the maximum file alignment that could be required for any
3404
   ELF data structure.  */
3825
   ELF data structure.  */
3405
 
3826
 
3406
static inline file_ptr
3827
static inline file_ptr
3407
align_file_position (file_ptr off, int align)
3828
align_file_position (file_ptr off, int align)
3408
{
3829
{
3409
  return (off + align - 1) & ~(align - 1);
3830
  return (off + align - 1) & ~(align - 1);
3410
}
3831
}
3411
 
3832
 
3412
/* Assign a file position to a section, optionally aligning to the
3833
/* Assign a file position to a section, optionally aligning to the
3413
   required section alignment.  */
3834
   required section alignment.  */
3414
 
3835
 
3415
file_ptr
3836
file_ptr
3416
_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3837
_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
3417
					   file_ptr offset,
3838
					   file_ptr offset,
3418
					   bfd_boolean align)
3839
					   bfd_boolean align)
3419
{
3840
{
3420
  if (align && i_shdrp->sh_addralign > 1)
3841
  if (align && i_shdrp->sh_addralign > 1)
3421
    offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3842
    offset = BFD_ALIGN (offset, i_shdrp->sh_addralign);
3422
  i_shdrp->sh_offset = offset;
3843
  i_shdrp->sh_offset = offset;
3423
  if (i_shdrp->bfd_section != NULL)
3844
  if (i_shdrp->bfd_section != NULL)
3424
    i_shdrp->bfd_section->filepos = offset;
3845
    i_shdrp->bfd_section->filepos = offset;
3425
  if (i_shdrp->sh_type != SHT_NOBITS)
3846
  if (i_shdrp->sh_type != SHT_NOBITS)
3426
    offset += i_shdrp->sh_size;
3847
    offset += i_shdrp->sh_size;
3427
  return offset;
3848
  return offset;
3428
}
3849
}
3429
 
3850
 
3430
/* Compute the file positions we are going to put the sections at, and
3851
/* Compute the file positions we are going to put the sections at, and
3431
   otherwise prepare to begin writing out the ELF file.  If LINK_INFO
3852
   otherwise prepare to begin writing out the ELF file.  If LINK_INFO
3432
   is not NULL, this is being called by the ELF backend linker.  */
3853
   is not NULL, this is being called by the ELF backend linker.  */
3433
 
3854
 
3434
bfd_boolean
3855
bfd_boolean
3435
_bfd_elf_compute_section_file_positions (bfd *abfd,
3856
_bfd_elf_compute_section_file_positions (bfd *abfd,
3436
					 struct bfd_link_info *link_info)
3857
					 struct bfd_link_info *link_info)
3437
{
3858
{
3438
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3859
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3439
  struct fake_section_arg fsargs;
3860
  struct fake_section_arg fsargs;
3440
  bfd_boolean failed;
3861
  bfd_boolean failed;
3441
  struct bfd_strtab_hash *strtab = NULL;
3862
  struct elf_strtab_hash *strtab = NULL;
3442
  Elf_Internal_Shdr *shstrtab_hdr;
3863
  Elf_Internal_Shdr *shstrtab_hdr;
3443
  bfd_boolean need_symtab;
3864
  bfd_boolean need_symtab;
3444
 
3865
 
3445
  if (abfd->output_has_begun)
3866
  if (abfd->output_has_begun)
3446
    return TRUE;
3867
    return TRUE;
3447
 
3868
 
3448
  /* Do any elf backend specific processing first.  */
3869
  /* Do any elf backend specific processing first.  */
3449
  if (bed->elf_backend_begin_write_processing)
3870
  if (bed->elf_backend_begin_write_processing)
3450
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3871
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3451
 
3872
 
3452
  if (! prep_headers (abfd))
3873
  if (! prep_headers (abfd))
3453
    return FALSE;
3874
    return FALSE;
3454
 
3875
 
3455
  /* Post process the headers if necessary.  */
3876
  /* Post process the headers if necessary.  */
3456
  if (bed->elf_backend_post_process_headers)
-
 
3457
    (*bed->elf_backend_post_process_headers) (abfd, link_info);
3877
  (*bed->elf_backend_post_process_headers) (abfd, link_info);
3458
 
3878
 
3459
  fsargs.failed = FALSE;
3879
  fsargs.failed = FALSE;
3460
  fsargs.link_info = link_info;
3880
  fsargs.link_info = link_info;
3461
  bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
3881
  bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
3462
  if (fsargs.failed)
3882
  if (fsargs.failed)
3463
    return FALSE;
3883
    return FALSE;
3464
 
3884
 
3465
  if (!assign_section_numbers (abfd, link_info))
3885
  if (!assign_section_numbers (abfd, link_info))
3466
    return FALSE;
3886
    return FALSE;
3467
 
3887
 
3468
  /* The backend linker builds symbol table information itself.  */
3888
  /* The backend linker builds symbol table information itself.  */
3469
  need_symtab = (link_info == NULL
3889
  need_symtab = (link_info == NULL
3470
		 && (bfd_get_symcount (abfd) > 0
3890
		 && (bfd_get_symcount (abfd) > 0
3471
		     || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3891
		     || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
3472
			 == HAS_RELOC)));
3892
			 == HAS_RELOC)));
3473
  if (need_symtab)
3893
  if (need_symtab)
3474
    {
3894
    {
3475
      /* Non-zero if doing a relocatable link.  */
3895
      /* Non-zero if doing a relocatable link.  */
3476
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3896
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3477
 
3897
 
3478
      if (! swap_out_syms (abfd, &strtab, relocatable_p))
3898
      if (! swap_out_syms (abfd, &strtab, relocatable_p))
3479
	return FALSE;
3899
	return FALSE;
3480
    }
3900
    }
3481
 
3901
 
3482
  failed = FALSE;
3902
  failed = FALSE;
3483
  if (link_info == NULL)
3903
  if (link_info == NULL)
3484
    {
3904
    {
3485
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3905
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3486
      if (failed)
3906
      if (failed)
3487
	return FALSE;
3907
	return FALSE;
3488
    }
3908
    }
3489
 
3909
 
3490
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3910
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3491
  /* sh_name was set in prep_headers.  */
3911
  /* sh_name was set in prep_headers.  */
3492
  shstrtab_hdr->sh_type = SHT_STRTAB;
3912
  shstrtab_hdr->sh_type = SHT_STRTAB;
3493
  shstrtab_hdr->sh_flags = 0;
3913
  shstrtab_hdr->sh_flags = 0;
3494
  shstrtab_hdr->sh_addr = 0;
3914
  shstrtab_hdr->sh_addr = 0;
3495
  shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3915
  /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
3496
  shstrtab_hdr->sh_entsize = 0;
3916
  shstrtab_hdr->sh_entsize = 0;
3497
  shstrtab_hdr->sh_link = 0;
3917
  shstrtab_hdr->sh_link = 0;
3498
  shstrtab_hdr->sh_info = 0;
3918
  shstrtab_hdr->sh_info = 0;
3499
  /* sh_offset is set in assign_file_positions_except_relocs.  */
3919
  /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
3500
  shstrtab_hdr->sh_addralign = 1;
3920
  shstrtab_hdr->sh_addralign = 1;
3501
 
3921
 
3502
  if (!assign_file_positions_except_relocs (abfd, link_info))
3922
  if (!assign_file_positions_except_relocs (abfd, link_info))
3503
    return FALSE;
3923
    return FALSE;
3504
 
3924
 
3505
  if (need_symtab)
3925
  if (need_symtab)
3506
    {
3926
    {
3507
      file_ptr off;
3927
      file_ptr off;
3508
      Elf_Internal_Shdr *hdr;
3928
      Elf_Internal_Shdr *hdr;
3509
 
3929
 
3510
      off = elf_next_file_pos (abfd);
3930
      off = elf_next_file_pos (abfd);
3511
 
3931
 
3512
      hdr = &elf_tdata (abfd)->symtab_hdr;
3932
      hdr = & elf_symtab_hdr (abfd);
-
 
3933
      off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
-
 
3934
 
3513
      off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3935
      if (elf_symtab_shndx_list (abfd) != NULL)
3514
 
3936
	{
3515
      hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3937
	  hdr = & elf_symtab_shndx_list (abfd)->hdr;
-
 
3938
	  if (hdr->sh_size != 0)
-
 
3939
	    off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3516
      if (hdr->sh_size != 0)
3940
	  /* FIXME: What about other symtab_shndx sections in the list ?  */
3517
	off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3941
	}
3518
 
3942
 
3519
      hdr = &elf_tdata (abfd)->strtab_hdr;
3943
      hdr = &elf_tdata (abfd)->strtab_hdr;
3520
      off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3944
      off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3521
 
3945
 
3522
      elf_next_file_pos (abfd) = off;
3946
      elf_next_file_pos (abfd) = off;
3523
 
3947
 
3524
      /* Now that we know where the .strtab section goes, write it
3948
      /* Now that we know where the .strtab section goes, write it
3525
	 out.  */
3949
	 out.  */
3526
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3950
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3527
	  || ! _bfd_stringtab_emit (abfd, strtab))
3951
	  || ! _bfd_elf_strtab_emit (abfd, strtab))
3528
	return FALSE;
3952
	return FALSE;
3529
      _bfd_stringtab_free (strtab);
3953
      _bfd_elf_strtab_free (strtab);
3530
    }
3954
    }
3531
 
3955
 
3532
  abfd->output_has_begun = TRUE;
3956
  abfd->output_has_begun = TRUE;
3533
 
3957
 
3534
  return TRUE;
3958
  return TRUE;
3535
}
3959
}
3536
 
3960
 
3537
/* Make an initial estimate of the size of the program header.  If we
3961
/* Make an initial estimate of the size of the program header.  If we
3538
   get the number wrong here, we'll redo section placement.  */
3962
   get the number wrong here, we'll redo section placement.  */
3539
 
3963
 
3540
static bfd_size_type
3964
static bfd_size_type
3541
get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3965
get_program_header_size (bfd *abfd, struct bfd_link_info *info)
3542
{
3966
{
3543
  size_t segs;
3967
  size_t segs;
3544
  asection *s;
3968
  asection *s;
3545
  const struct elf_backend_data *bed;
3969
  const struct elf_backend_data *bed;
3546
 
3970
 
3547
  /* Assume we will need exactly two PT_LOAD segments: one for text
3971
  /* Assume we will need exactly two PT_LOAD segments: one for text
3548
     and one for data.  */
3972
     and one for data.  */
3549
  segs = 2;
3973
  segs = 2;
3550
 
3974
 
3551
  s = bfd_get_section_by_name (abfd, ".interp");
3975
  s = bfd_get_section_by_name (abfd, ".interp");
3552
  if (s != NULL && (s->flags & SEC_LOAD) != 0)
3976
  if (s != NULL && (s->flags & SEC_LOAD) != 0)
3553
    {
3977
    {
3554
      /* If we have a loadable interpreter section, we need a
3978
      /* If we have a loadable interpreter section, we need a
3555
	 PT_INTERP segment.  In this case, assume we also need a
3979
	 PT_INTERP segment.  In this case, assume we also need a
3556
	 PT_PHDR segment, although that may not be true for all
3980
	 PT_PHDR segment, although that may not be true for all
3557
	 targets.  */
3981
	 targets.  */
3558
      segs += 2;
3982
      segs += 2;
3559
    }
3983
    }
3560
 
3984
 
3561
  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3985
  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3562
    {
3986
    {
3563
      /* We need a PT_DYNAMIC segment.  */
3987
      /* We need a PT_DYNAMIC segment.  */
3564
      ++segs;
3988
      ++segs;
3565
    }
3989
    }
3566
 
3990
 
3567
  if (info != NULL && info->relro)
3991
  if (info != NULL && info->relro)
3568
    {
3992
    {
3569
      /* We need a PT_GNU_RELRO segment.  */
3993
      /* We need a PT_GNU_RELRO segment.  */
3570
      ++segs;
3994
      ++segs;
3571
    }
3995
    }
3572
 
3996
 
3573
  if (elf_eh_frame_hdr (abfd))
3997
  if (elf_eh_frame_hdr (abfd))
3574
    {
3998
    {
3575
      /* We need a PT_GNU_EH_FRAME segment.  */
3999
      /* We need a PT_GNU_EH_FRAME segment.  */
3576
      ++segs;
4000
      ++segs;
3577
    }
4001
    }
3578
 
4002
 
3579
  if (elf_stack_flags (abfd))
4003
  if (elf_stack_flags (abfd))
3580
    {
4004
    {
3581
      /* We need a PT_GNU_STACK segment.  */
4005
      /* We need a PT_GNU_STACK segment.  */
3582
      ++segs;
4006
      ++segs;
3583
    }
4007
    }
3584
 
4008
 
3585
  for (s = abfd->sections; s != NULL; s = s->next)
4009
  for (s = abfd->sections; s != NULL; s = s->next)
3586
    {
4010
    {
3587
      if ((s->flags & SEC_LOAD) != 0
4011
      if ((s->flags & SEC_LOAD) != 0
3588
	  && CONST_STRNEQ (s->name, ".note"))
4012
	  && CONST_STRNEQ (s->name, ".note"))
3589
	{
4013
	{
3590
	  /* We need a PT_NOTE segment.  */
4014
	  /* We need a PT_NOTE segment.  */
3591
	  ++segs;
4015
	  ++segs;
3592
	  /* Try to create just one PT_NOTE segment
4016
	  /* Try to create just one PT_NOTE segment
3593
	     for all adjacent loadable .note* sections.
4017
	     for all adjacent loadable .note* sections.
3594
	     gABI requires that within a PT_NOTE segment
4018
	     gABI requires that within a PT_NOTE segment
3595
	     (and also inside of each SHT_NOTE section)
4019
	     (and also inside of each SHT_NOTE section)
3596
	     each note is padded to a multiple of 4 size,
4020
	     each note is padded to a multiple of 4 size,
3597
	     so we check whether the sections are correctly
4021
	     so we check whether the sections are correctly
3598
	     aligned.  */
4022
	     aligned.  */
3599
	  if (s->alignment_power == 2)
4023
	  if (s->alignment_power == 2)
3600
	    while (s->next != NULL
4024
	    while (s->next != NULL
3601
		   && s->next->alignment_power == 2
4025
		   && s->next->alignment_power == 2
3602
		   && (s->next->flags & SEC_LOAD) != 0
4026
		   && (s->next->flags & SEC_LOAD) != 0
3603
		   && CONST_STRNEQ (s->next->name, ".note"))
4027
		   && CONST_STRNEQ (s->next->name, ".note"))
3604
	      s = s->next;
4028
	      s = s->next;
3605
	}
4029
	}
3606
    }
4030
    }
3607
 
4031
 
3608
  for (s = abfd->sections; s != NULL; s = s->next)
4032
  for (s = abfd->sections; s != NULL; s = s->next)
3609
    {
4033
    {
3610
      if (s->flags & SEC_THREAD_LOCAL)
4034
      if (s->flags & SEC_THREAD_LOCAL)
3611
	{
4035
	{
3612
	  /* We need a PT_TLS segment.  */
4036
	  /* We need a PT_TLS segment.  */
3613
	  ++segs;
4037
	  ++segs;
3614
	  break;
4038
	  break;
3615
	}
4039
	}
3616
    }
4040
    }
3617
 
4041
 
3618
  /* Let the backend count up any program headers it might need.  */
4042
  /* Let the backend count up any program headers it might need.  */
3619
  bed = get_elf_backend_data (abfd);
4043
  bed = get_elf_backend_data (abfd);
3620
  if (bed->elf_backend_additional_program_headers)
4044
  if (bed->elf_backend_additional_program_headers)
3621
    {
4045
    {
3622
      int a;
4046
      int a;
3623
 
4047
 
3624
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4048
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
3625
      if (a == -1)
4049
      if (a == -1)
3626
	abort ();
4050
	abort ();
3627
      segs += a;
4051
      segs += a;
3628
    }
4052
    }
3629
 
4053
 
3630
  return segs * bed->s->sizeof_phdr;
4054
  return segs * bed->s->sizeof_phdr;
3631
}
4055
}
3632
 
4056
 
3633
/* Find the segment that contains the output_section of section.  */
4057
/* Find the segment that contains the output_section of section.  */
3634
 
4058
 
3635
Elf_Internal_Phdr *
4059
Elf_Internal_Phdr *
3636
_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4060
_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
3637
{
4061
{
3638
  struct elf_segment_map *m;
4062
  struct elf_segment_map *m;
3639
  Elf_Internal_Phdr *p;
4063
  Elf_Internal_Phdr *p;
3640
 
4064
 
3641
  for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4065
  for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
3642
       m != NULL;
4066
       m != NULL;
3643
       m = m->next, p++)
4067
       m = m->next, p++)
3644
    {
4068
    {
3645
      int i;
4069
      int i;
3646
 
4070
 
3647
      for (i = m->count - 1; i >= 0; i--)
4071
      for (i = m->count - 1; i >= 0; i--)
3648
	if (m->sections[i] == section)
4072
	if (m->sections[i] == section)
3649
	  return p;
4073
	  return p;
3650
    }
4074
    }
3651
 
4075
 
3652
  return NULL;
4076
  return NULL;
3653
}
4077
}
3654
 
4078
 
3655
/* Create a mapping from a set of sections to a program segment.  */
4079
/* Create a mapping from a set of sections to a program segment.  */
3656
 
4080
 
3657
static struct elf_segment_map *
4081
static struct elf_segment_map *
3658
make_mapping (bfd *abfd,
4082
make_mapping (bfd *abfd,
3659
	      asection **sections,
4083
	      asection **sections,
3660
	      unsigned int from,
4084
	      unsigned int from,
3661
	      unsigned int to,
4085
	      unsigned int to,
3662
	      bfd_boolean phdr)
4086
	      bfd_boolean phdr)
3663
{
4087
{
3664
  struct elf_segment_map *m;
4088
  struct elf_segment_map *m;
3665
  unsigned int i;
4089
  unsigned int i;
3666
  asection **hdrpp;
4090
  asection **hdrpp;
3667
  bfd_size_type amt;
4091
  bfd_size_type amt;
3668
 
4092
 
3669
  amt = sizeof (struct elf_segment_map);
4093
  amt = sizeof (struct elf_segment_map);
3670
  amt += (to - from - 1) * sizeof (asection *);
4094
  amt += (to - from - 1) * sizeof (asection *);
3671
  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4095
  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3672
  if (m == NULL)
4096
  if (m == NULL)
3673
    return NULL;
4097
    return NULL;
3674
  m->next = NULL;
4098
  m->next = NULL;
3675
  m->p_type = PT_LOAD;
4099
  m->p_type = PT_LOAD;
3676
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4100
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3677
    m->sections[i - from] = *hdrpp;
4101
    m->sections[i - from] = *hdrpp;
3678
  m->count = to - from;
4102
  m->count = to - from;
3679
 
4103
 
3680
  if (from == 0 && phdr)
4104
  if (from == 0 && phdr)
3681
    {
4105
    {
3682
      /* Include the headers in the first PT_LOAD segment.  */
4106
      /* Include the headers in the first PT_LOAD segment.  */
3683
      m->includes_filehdr = 1;
4107
      m->includes_filehdr = 1;
3684
      m->includes_phdrs = 1;
4108
      m->includes_phdrs = 1;
3685
    }
4109
    }
3686
 
4110
 
3687
  return m;
4111
  return m;
3688
}
4112
}
3689
 
4113
 
3690
/* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
4114
/* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
3691
   on failure.  */
4115
   on failure.  */
3692
 
4116
 
3693
struct elf_segment_map *
4117
struct elf_segment_map *
3694
_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
4118
_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
3695
{
4119
{
3696
  struct elf_segment_map *m;
4120
  struct elf_segment_map *m;
3697
 
4121
 
3698
  m = (struct elf_segment_map *) bfd_zalloc (abfd,
4122
  m = (struct elf_segment_map *) bfd_zalloc (abfd,
3699
                                             sizeof (struct elf_segment_map));
4123
                                             sizeof (struct elf_segment_map));
3700
  if (m == NULL)
4124
  if (m == NULL)
3701
    return NULL;
4125
    return NULL;
3702
  m->next = NULL;
4126
  m->next = NULL;
3703
  m->p_type = PT_DYNAMIC;
4127
  m->p_type = PT_DYNAMIC;
3704
  m->count = 1;
4128
  m->count = 1;
3705
  m->sections[0] = dynsec;
4129
  m->sections[0] = dynsec;
3706
 
4130
 
3707
  return m;
4131
  return m;
3708
}
4132
}
3709
 
4133
 
3710
/* Possibly add or remove segments from the segment map.  */
4134
/* Possibly add or remove segments from the segment map.  */
3711
 
4135
 
3712
static bfd_boolean
4136
static bfd_boolean
3713
elf_modify_segment_map (bfd *abfd,
4137
elf_modify_segment_map (bfd *abfd,
3714
			struct bfd_link_info *info,
4138
			struct bfd_link_info *info,
3715
			bfd_boolean remove_empty_load)
4139
			bfd_boolean remove_empty_load)
3716
{
4140
{
3717
  struct elf_segment_map **m;
4141
  struct elf_segment_map **m;
3718
  const struct elf_backend_data *bed;
4142
  const struct elf_backend_data *bed;
3719
 
4143
 
3720
  /* The placement algorithm assumes that non allocated sections are
4144
  /* The placement algorithm assumes that non allocated sections are
3721
     not in PT_LOAD segments.  We ensure this here by removing such
4145
     not in PT_LOAD segments.  We ensure this here by removing such
3722
     sections from the segment map.  We also remove excluded
4146
     sections from the segment map.  We also remove excluded
3723
     sections.  Finally, any PT_LOAD segment without sections is
4147
     sections.  Finally, any PT_LOAD segment without sections is
3724
     removed.  */
4148
     removed.  */
3725
  m = &elf_seg_map (abfd);
4149
  m = &elf_seg_map (abfd);
3726
  while (*m)
4150
  while (*m)
3727
    {
4151
    {
3728
      unsigned int i, new_count;
4152
      unsigned int i, new_count;
3729
 
4153
 
3730
      for (new_count = 0, i = 0; i < (*m)->count; i++)
4154
      for (new_count = 0, i = 0; i < (*m)->count; i++)
3731
	{
4155
	{
3732
	  if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
4156
	  if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
3733
	      && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
4157
	      && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
3734
		  || (*m)->p_type != PT_LOAD))
4158
		  || (*m)->p_type != PT_LOAD))
3735
	    {
4159
	    {
3736
	      (*m)->sections[new_count] = (*m)->sections[i];
4160
	      (*m)->sections[new_count] = (*m)->sections[i];
3737
	      new_count++;
4161
	      new_count++;
3738
	    }
4162
	    }
3739
	}
4163
	}
3740
      (*m)->count = new_count;
4164
      (*m)->count = new_count;
3741
 
4165
 
3742
      if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
4166
      if (remove_empty_load && (*m)->p_type == PT_LOAD && (*m)->count == 0)
3743
	*m = (*m)->next;
4167
	*m = (*m)->next;
3744
      else
4168
      else
3745
	m = &(*m)->next;
4169
	m = &(*m)->next;
3746
    }
4170
    }
3747
 
4171
 
3748
  bed = get_elf_backend_data (abfd);
4172
  bed = get_elf_backend_data (abfd);
3749
  if (bed->elf_backend_modify_segment_map != NULL)
4173
  if (bed->elf_backend_modify_segment_map != NULL)
3750
    {
4174
    {
3751
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
4175
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
3752
	return FALSE;
4176
	return FALSE;
3753
    }
4177
    }
3754
 
4178
 
3755
  return TRUE;
4179
  return TRUE;
3756
}
4180
}
3757
 
4181
 
3758
/* Set up a mapping from BFD sections to program segments.  */
4182
/* Set up a mapping from BFD sections to program segments.  */
3759
 
4183
 
3760
bfd_boolean
4184
bfd_boolean
3761
_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
4185
_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info)
3762
{
4186
{
3763
  unsigned int count;
4187
  unsigned int count;
3764
  struct elf_segment_map *m;
4188
  struct elf_segment_map *m;
3765
  asection **sections = NULL;
4189
  asection **sections = NULL;
3766
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4190
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3767
  bfd_boolean no_user_phdrs;
4191
  bfd_boolean no_user_phdrs;
3768
 
4192
 
3769
  no_user_phdrs = elf_seg_map (abfd) == NULL;
4193
  no_user_phdrs = elf_seg_map (abfd) == NULL;
3770
 
4194
 
3771
  if (info != NULL)
4195
  if (info != NULL)
3772
    info->user_phdrs = !no_user_phdrs;
4196
    info->user_phdrs = !no_user_phdrs;
3773
 
4197
 
3774
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
4198
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
3775
    {
4199
    {
3776
      asection *s;
4200
      asection *s;
3777
      unsigned int i;
4201
      unsigned int i;
3778
      struct elf_segment_map *mfirst;
4202
      struct elf_segment_map *mfirst;
3779
      struct elf_segment_map **pm;
4203
      struct elf_segment_map **pm;
3780
      asection *last_hdr;
4204
      asection *last_hdr;
3781
      bfd_vma last_size;
4205
      bfd_vma last_size;
3782
      unsigned int phdr_index;
4206
      unsigned int phdr_index;
3783
      bfd_vma maxpagesize;
4207
      bfd_vma maxpagesize;
3784
      asection **hdrpp;
4208
      asection **hdrpp;
3785
      bfd_boolean phdr_in_segment = TRUE;
4209
      bfd_boolean phdr_in_segment = TRUE;
3786
      bfd_boolean writable;
4210
      bfd_boolean writable;
3787
      int tls_count = 0;
4211
      int tls_count = 0;
3788
      asection *first_tls = NULL;
4212
      asection *first_tls = NULL;
3789
      asection *dynsec, *eh_frame_hdr;
4213
      asection *dynsec, *eh_frame_hdr;
3790
      bfd_size_type amt;
4214
      bfd_size_type amt;
3791
      bfd_vma addr_mask, wrap_to = 0;
4215
      bfd_vma addr_mask, wrap_to = 0;
3792
 
4216
 
3793
      /* Select the allocated sections, and sort them.  */
4217
      /* Select the allocated sections, and sort them.  */
3794
 
4218
 
3795
      sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
4219
      sections = (asection **) bfd_malloc2 (bfd_count_sections (abfd),
3796
                                            sizeof (asection *));
4220
                                            sizeof (asection *));
3797
      if (sections == NULL)
4221
      if (sections == NULL)
3798
	goto error_return;
4222
	goto error_return;
3799
 
4223
 
3800
      /* Calculate top address, avoiding undefined behaviour of shift
4224
      /* Calculate top address, avoiding undefined behaviour of shift
3801
	 left operator when shift count is equal to size of type
4225
	 left operator when shift count is equal to size of type
3802
	 being shifted.  */
4226
	 being shifted.  */
3803
      addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
4227
      addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
3804
      addr_mask = (addr_mask << 1) + 1;
4228
      addr_mask = (addr_mask << 1) + 1;
3805
 
4229
 
3806
      i = 0;
4230
      i = 0;
3807
      for (s = abfd->sections; s != NULL; s = s->next)
4231
      for (s = abfd->sections; s != NULL; s = s->next)
3808
	{
4232
	{
3809
	  if ((s->flags & SEC_ALLOC) != 0)
4233
	  if ((s->flags & SEC_ALLOC) != 0)
3810
	    {
4234
	    {
3811
	      sections[i] = s;
4235
	      sections[i] = s;
3812
	      ++i;
4236
	      ++i;
3813
	      /* A wrapping section potentially clashes with header.  */
4237
	      /* A wrapping section potentially clashes with header.  */
3814
	      if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
4238
	      if (((s->lma + s->size) & addr_mask) < (s->lma & addr_mask))
3815
		wrap_to = (s->lma + s->size) & addr_mask;
4239
		wrap_to = (s->lma + s->size) & addr_mask;
3816
	    }
4240
	    }
3817
	}
4241
	}
3818
      BFD_ASSERT (i <= bfd_count_sections (abfd));
4242
      BFD_ASSERT (i <= bfd_count_sections (abfd));
3819
      count = i;
4243
      count = i;
3820
 
4244
 
3821
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
4245
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3822
 
4246
 
3823
      /* Build the mapping.  */
4247
      /* Build the mapping.  */
3824
 
4248
 
3825
      mfirst = NULL;
4249
      mfirst = NULL;
3826
      pm = &mfirst;
4250
      pm = &mfirst;
3827
 
4251
 
3828
      /* If we have a .interp section, then create a PT_PHDR segment for
4252
      /* If we have a .interp section, then create a PT_PHDR segment for
3829
	 the program headers and a PT_INTERP segment for the .interp
4253
	 the program headers and a PT_INTERP segment for the .interp
3830
	 section.  */
4254
	 section.  */
3831
      s = bfd_get_section_by_name (abfd, ".interp");
4255
      s = bfd_get_section_by_name (abfd, ".interp");
3832
      if (s != NULL && (s->flags & SEC_LOAD) != 0)
4256
      if (s != NULL && (s->flags & SEC_LOAD) != 0)
3833
	{
4257
	{
3834
	  amt = sizeof (struct elf_segment_map);
4258
	  amt = sizeof (struct elf_segment_map);
3835
	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4259
	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3836
	  if (m == NULL)
4260
	  if (m == NULL)
3837
	    goto error_return;
4261
	    goto error_return;
3838
	  m->next = NULL;
4262
	  m->next = NULL;
3839
	  m->p_type = PT_PHDR;
4263
	  m->p_type = PT_PHDR;
3840
	  /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not.  */
4264
	  /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not.  */
3841
	  m->p_flags = PF_R | PF_X;
4265
	  m->p_flags = PF_R | PF_X;
3842
	  m->p_flags_valid = 1;
4266
	  m->p_flags_valid = 1;
3843
	  m->includes_phdrs = 1;
4267
	  m->includes_phdrs = 1;
3844
 
4268
 
3845
	  *pm = m;
4269
	  *pm = m;
3846
	  pm = &m->next;
4270
	  pm = &m->next;
3847
 
4271
 
3848
	  amt = sizeof (struct elf_segment_map);
4272
	  amt = sizeof (struct elf_segment_map);
3849
	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4273
	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3850
	  if (m == NULL)
4274
	  if (m == NULL)
3851
	    goto error_return;
4275
	    goto error_return;
3852
	  m->next = NULL;
4276
	  m->next = NULL;
3853
	  m->p_type = PT_INTERP;
4277
	  m->p_type = PT_INTERP;
3854
	  m->count = 1;
4278
	  m->count = 1;
3855
	  m->sections[0] = s;
4279
	  m->sections[0] = s;
3856
 
4280
 
3857
	  *pm = m;
4281
	  *pm = m;
3858
	  pm = &m->next;
4282
	  pm = &m->next;
3859
	}
4283
	}
3860
 
4284
 
3861
      /* Look through the sections.  We put sections in the same program
4285
      /* Look through the sections.  We put sections in the same program
3862
	 segment when the start of the second section can be placed within
4286
	 segment when the start of the second section can be placed within
3863
	 a few bytes of the end of the first section.  */
4287
	 a few bytes of the end of the first section.  */
3864
      last_hdr = NULL;
4288
      last_hdr = NULL;
3865
      last_size = 0;
4289
      last_size = 0;
3866
      phdr_index = 0;
4290
      phdr_index = 0;
3867
      maxpagesize = bed->maxpagesize;
4291
      maxpagesize = bed->maxpagesize;
-
 
4292
      /* PR 17512: file: c8455299.
-
 
4293
	 Avoid divide-by-zero errors later on.
-
 
4294
	 FIXME: Should we abort if the maxpagesize is zero ?  */
-
 
4295
      if (maxpagesize == 0)
-
 
4296
	maxpagesize = 1;
3868
      writable = FALSE;
4297
      writable = FALSE;
3869
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
4298
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3870
      if (dynsec != NULL
4299
      if (dynsec != NULL
3871
	  && (dynsec->flags & SEC_LOAD) == 0)
4300
	  && (dynsec->flags & SEC_LOAD) == 0)
3872
	dynsec = NULL;
4301
	dynsec = NULL;
3873
 
4302
 
3874
      /* Deal with -Ttext or something similar such that the first section
4303
      /* Deal with -Ttext or something similar such that the first section
3875
	 is not adjacent to the program headers.  This is an
4304
	 is not adjacent to the program headers.  This is an
3876
	 approximation, since at this point we don't know exactly how many
4305
	 approximation, since at this point we don't know exactly how many
3877
	 program headers we will need.  */
4306
	 program headers we will need.  */
3878
      if (count > 0)
4307
      if (count > 0)
3879
	{
4308
	{
3880
	  bfd_size_type phdr_size = elf_program_header_size (abfd);
4309
	  bfd_size_type phdr_size = elf_program_header_size (abfd);
3881
 
4310
 
3882
	  if (phdr_size == (bfd_size_type) -1)
4311
	  if (phdr_size == (bfd_size_type) -1)
3883
	    phdr_size = get_program_header_size (abfd, info);
4312
	    phdr_size = get_program_header_size (abfd, info);
3884
	  phdr_size += bed->s->sizeof_ehdr;
4313
	  phdr_size += bed->s->sizeof_ehdr;
3885
	  if ((abfd->flags & D_PAGED) == 0
4314
	  if ((abfd->flags & D_PAGED) == 0
3886
	      || (sections[0]->lma & addr_mask) < phdr_size
4315
	      || (sections[0]->lma & addr_mask) < phdr_size
3887
	      || ((sections[0]->lma & addr_mask) % maxpagesize
4316
	      || ((sections[0]->lma & addr_mask) % maxpagesize
3888
		  < phdr_size % maxpagesize)
4317
		  < phdr_size % maxpagesize)
3889
	      || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
4318
	      || (sections[0]->lma & addr_mask & -maxpagesize) < wrap_to)
3890
	    phdr_in_segment = FALSE;
4319
	    phdr_in_segment = FALSE;
3891
	}
4320
	}
3892
 
4321
 
3893
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
4322
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3894
	{
4323
	{
3895
	  asection *hdr;
4324
	  asection *hdr;
3896
	  bfd_boolean new_segment;
4325
	  bfd_boolean new_segment;
3897
 
4326
 
3898
	  hdr = *hdrpp;
4327
	  hdr = *hdrpp;
3899
 
4328
 
3900
	  /* See if this section and the last one will fit in the same
4329
	  /* See if this section and the last one will fit in the same
3901
	     segment.  */
4330
	     segment.  */
3902
 
4331
 
3903
	  if (last_hdr == NULL)
4332
	  if (last_hdr == NULL)
3904
	    {
4333
	    {
3905
	      /* If we don't have a segment yet, then we don't need a new
4334
	      /* If we don't have a segment yet, then we don't need a new
3906
		 one (we build the last one after this loop).  */
4335
		 one (we build the last one after this loop).  */
3907
	      new_segment = FALSE;
4336
	      new_segment = FALSE;
3908
	    }
4337
	    }
3909
	  else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
4338
	  else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3910
	    {
4339
	    {
3911
	      /* If this section has a different relation between the
4340
	      /* If this section has a different relation between the
3912
		 virtual address and the load address, then we need a new
4341
		 virtual address and the load address, then we need a new
3913
		 segment.  */
4342
		 segment.  */
3914
	      new_segment = TRUE;
4343
	      new_segment = TRUE;
3915
	    }
4344
	    }
3916
	  else if (hdr->lma < last_hdr->lma + last_size
4345
	  else if (hdr->lma < last_hdr->lma + last_size
3917
		   || last_hdr->lma + last_size < last_hdr->lma)
4346
		   || last_hdr->lma + last_size < last_hdr->lma)
3918
	    {
4347
	    {
3919
	      /* If this section has a load address that makes it overlap
4348
	      /* If this section has a load address that makes it overlap
3920
		 the previous section, then we need a new segment.  */
4349
		 the previous section, then we need a new segment.  */
3921
	      new_segment = TRUE;
4350
	      new_segment = TRUE;
3922
	    }
4351
	    }
3923
	  /* In the next test we have to be careful when last_hdr->lma is close
4352
	  /* In the next test we have to be careful when last_hdr->lma is close
3924
	     to the end of the address space.  If the aligned address wraps
4353
	     to the end of the address space.  If the aligned address wraps
3925
	     around to the start of the address space, then there are no more
4354
	     around to the start of the address space, then there are no more
3926
	     pages left in memory and it is OK to assume that the current
4355
	     pages left in memory and it is OK to assume that the current
3927
	     section can be included in the current segment.  */
4356
	     section can be included in the current segment.  */
3928
	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4357
	  else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3929
		    > last_hdr->lma)
4358
		    > last_hdr->lma)
3930
		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
4359
		   && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) + maxpagesize
3931
		       <= hdr->lma))
4360
		       <= hdr->lma))
3932
	    {
4361
	    {
3933
	      /* If putting this section in this segment would force us to
4362
	      /* If putting this section in this segment would force us to
3934
		 skip a page in the segment, then we need a new segment.  */
4363
		 skip a page in the segment, then we need a new segment.  */
3935
	      new_segment = TRUE;
4364
	      new_segment = TRUE;
3936
	    }
4365
	    }
3937
	  else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
4366
	  else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
3938
		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
4367
		   && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0
-
 
4368
		   && ((abfd->flags & D_PAGED) == 0
-
 
4369
		       || (((last_hdr->lma + last_size - 1) & -maxpagesize)
-
 
4370
			   != (hdr->lma & -maxpagesize))))
3939
	    {
4371
	    {
3940
	      /* We don't want to put a loadable section after a
4372
	      /* We don't want to put a loaded section after a
3941
		 nonloadable section in the same segment.
4373
		 nonloaded (ie. bss style) section in the same segment
-
 
4374
		 as that will force the non-loaded section to be loaded.
3942
		 Consider .tbss sections as loadable for this purpose.  */
4375
		 Consider .tbss sections as loaded for this purpose.
-
 
4376
		 However, like the writable/non-writable case below,
-
 
4377
		 if they are on the same page then they must be put
-
 
4378
		 in the same segment.  */
3943
	      new_segment = TRUE;
4379
	      new_segment = TRUE;
3944
	    }
4380
	    }
3945
	  else if ((abfd->flags & D_PAGED) == 0)
4381
	  else if ((abfd->flags & D_PAGED) == 0)
3946
	    {
4382
	    {
3947
	      /* If the file is not demand paged, which means that we
4383
	      /* If the file is not demand paged, which means that we
3948
		 don't require the sections to be correctly aligned in the
4384
		 don't require the sections to be correctly aligned in the
3949
		 file, then there is no other reason for a new segment.  */
4385
		 file, then there is no other reason for a new segment.  */
3950
	      new_segment = FALSE;
4386
	      new_segment = FALSE;
3951
	    }
4387
	    }
3952
	  else if (! writable
4388
	  else if (! writable
3953
		   && (hdr->flags & SEC_READONLY) == 0
4389
		   && (hdr->flags & SEC_READONLY) == 0
3954
		   && (((last_hdr->lma + last_size - 1) & -maxpagesize)
4390
		   && (((last_hdr->lma + last_size - 1) & -maxpagesize)
3955
		       != (hdr->lma & -maxpagesize)))
4391
		       != (hdr->lma & -maxpagesize)))
3956
	    {
4392
	    {
3957
	      /* We don't want to put a writable section in a read only
4393
	      /* We don't want to put a writable section in a read only
3958
		 segment, unless they are on the same page in memory
4394
		 segment, unless they are on the same page in memory
3959
		 anyhow.  We already know that the last section does not
4395
		 anyhow.  We already know that the last section does not
3960
		 bring us past the current section on the page, so the
4396
		 bring us past the current section on the page, so the
3961
		 only case in which the new section is not on the same
4397
		 only case in which the new section is not on the same
3962
		 page as the previous section is when the previous section
4398
		 page as the previous section is when the previous section
3963
		 ends precisely on a page boundary.  */
4399
		 ends precisely on a page boundary.  */
3964
	      new_segment = TRUE;
4400
	      new_segment = TRUE;
3965
	    }
4401
	    }
3966
	  else
4402
	  else
3967
	    {
4403
	    {
3968
	      /* Otherwise, we can use the same segment.  */
4404
	      /* Otherwise, we can use the same segment.  */
3969
	      new_segment = FALSE;
4405
	      new_segment = FALSE;
3970
	    }
4406
	    }
3971
 
4407
 
3972
	  /* Allow interested parties a chance to override our decision.  */
4408
	  /* Allow interested parties a chance to override our decision.  */
3973
	  if (last_hdr != NULL
4409
	  if (last_hdr != NULL
3974
	      && info != NULL
4410
	      && info != NULL
3975
	      && info->callbacks->override_segment_assignment != NULL)
4411
	      && info->callbacks->override_segment_assignment != NULL)
3976
	    new_segment
4412
	    new_segment
3977
	      = info->callbacks->override_segment_assignment (info, abfd, hdr,
4413
	      = info->callbacks->override_segment_assignment (info, abfd, hdr,
3978
							      last_hdr,
4414
							      last_hdr,
3979
							      new_segment);
4415
							      new_segment);
3980
 
4416
 
3981
	  if (! new_segment)
4417
	  if (! new_segment)
3982
	    {
4418
	    {
3983
	      if ((hdr->flags & SEC_READONLY) == 0)
4419
	      if ((hdr->flags & SEC_READONLY) == 0)
3984
		writable = TRUE;
4420
		writable = TRUE;
3985
	      last_hdr = hdr;
4421
	      last_hdr = hdr;
3986
	      /* .tbss sections effectively have zero size.  */
4422
	      /* .tbss sections effectively have zero size.  */
3987
	      if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4423
	      if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3988
		  != SEC_THREAD_LOCAL)
4424
		  != SEC_THREAD_LOCAL)
3989
		last_size = hdr->size;
4425
		last_size = hdr->size;
3990
	      else
4426
	      else
3991
		last_size = 0;
4427
		last_size = 0;
3992
	      continue;
4428
	      continue;
3993
	    }
4429
	    }
3994
 
4430
 
3995
	  /* We need a new program segment.  We must create a new program
4431
	  /* We need a new program segment.  We must create a new program
3996
	     header holding all the sections from phdr_index until hdr.  */
4432
	     header holding all the sections from phdr_index until hdr.  */
3997
 
4433
 
3998
	  m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4434
	  m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3999
	  if (m == NULL)
4435
	  if (m == NULL)
4000
	    goto error_return;
4436
	    goto error_return;
4001
 
4437
 
4002
	  *pm = m;
4438
	  *pm = m;
4003
	  pm = &m->next;
4439
	  pm = &m->next;
4004
 
4440
 
4005
	  if ((hdr->flags & SEC_READONLY) == 0)
4441
	  if ((hdr->flags & SEC_READONLY) == 0)
4006
	    writable = TRUE;
4442
	    writable = TRUE;
4007
	  else
4443
	  else
4008
	    writable = FALSE;
4444
	    writable = FALSE;
4009
 
4445
 
4010
	  last_hdr = hdr;
4446
	  last_hdr = hdr;
4011
	  /* .tbss sections effectively have zero size.  */
4447
	  /* .tbss sections effectively have zero size.  */
4012
	  if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
4448
	  if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL)
4013
	    last_size = hdr->size;
4449
	    last_size = hdr->size;
4014
	  else
4450
	  else
4015
	    last_size = 0;
4451
	    last_size = 0;
4016
	  phdr_index = i;
4452
	  phdr_index = i;
4017
	  phdr_in_segment = FALSE;
4453
	  phdr_in_segment = FALSE;
4018
	}
4454
	}
4019
 
4455
 
4020
      /* Create a final PT_LOAD program segment, but not if it's just
4456
      /* Create a final PT_LOAD program segment, but not if it's just
4021
	 for .tbss.  */
4457
	 for .tbss.  */
4022
      if (last_hdr != NULL
4458
      if (last_hdr != NULL
4023
	  && (i - phdr_index != 1
4459
	  && (i - phdr_index != 1
4024
	      || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4460
	      || ((last_hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
4025
		  != SEC_THREAD_LOCAL)))
4461
		  != SEC_THREAD_LOCAL)))
4026
	{
4462
	{
4027
	  m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4463
	  m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
4028
	  if (m == NULL)
4464
	  if (m == NULL)
4029
	    goto error_return;
4465
	    goto error_return;
4030
 
4466
 
4031
	  *pm = m;
4467
	  *pm = m;
4032
	  pm = &m->next;
4468
	  pm = &m->next;
4033
	}
4469
	}
4034
 
4470
 
4035
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
4471
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
4036
      if (dynsec != NULL)
4472
      if (dynsec != NULL)
4037
	{
4473
	{
4038
	  m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4474
	  m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
4039
	  if (m == NULL)
4475
	  if (m == NULL)
4040
	    goto error_return;
4476
	    goto error_return;
4041
	  *pm = m;
4477
	  *pm = m;
4042
	  pm = &m->next;
4478
	  pm = &m->next;
4043
	}
4479
	}
4044
 
4480
 
4045
      /* For each batch of consecutive loadable .note sections,
4481
      /* For each batch of consecutive loadable .note sections,
4046
	 add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
4482
	 add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
4047
	 because if we link together nonloadable .note sections and
4483
	 because if we link together nonloadable .note sections and
4048
	 loadable .note sections, we will generate two .note sections
4484
	 loadable .note sections, we will generate two .note sections
4049
	 in the output file.  FIXME: Using names for section types is
4485
	 in the output file.  FIXME: Using names for section types is
4050
	 bogus anyhow.  */
4486
	 bogus anyhow.  */
4051
      for (s = abfd->sections; s != NULL; s = s->next)
4487
      for (s = abfd->sections; s != NULL; s = s->next)
4052
	{
4488
	{
4053
	  if ((s->flags & SEC_LOAD) != 0
4489
	  if ((s->flags & SEC_LOAD) != 0
4054
	      && CONST_STRNEQ (s->name, ".note"))
4490
	      && CONST_STRNEQ (s->name, ".note"))
4055
	    {
4491
	    {
4056
	      asection *s2;
4492
	      asection *s2;
4057
 
4493
 
4058
	      count = 1;
4494
	      count = 1;
4059
	      amt = sizeof (struct elf_segment_map);
4495
	      amt = sizeof (struct elf_segment_map);
4060
	      if (s->alignment_power == 2)
4496
	      if (s->alignment_power == 2)
4061
		for (s2 = s; s2->next != NULL; s2 = s2->next)
4497
		for (s2 = s; s2->next != NULL; s2 = s2->next)
4062
		  {
4498
		  {
4063
		    if (s2->next->alignment_power == 2
4499
		    if (s2->next->alignment_power == 2
4064
			&& (s2->next->flags & SEC_LOAD) != 0
4500
			&& (s2->next->flags & SEC_LOAD) != 0
4065
			&& CONST_STRNEQ (s2->next->name, ".note")
4501
			&& CONST_STRNEQ (s2->next->name, ".note")
4066
			&& align_power (s2->lma + s2->size, 2)
4502
			&& align_power (s2->lma + s2->size, 2)
4067
			   == s2->next->lma)
4503
			   == s2->next->lma)
4068
		      count++;
4504
		      count++;
4069
		    else
4505
		    else
4070
		      break;
4506
		      break;
4071
		  }
4507
		  }
4072
	      amt += (count - 1) * sizeof (asection *);
4508
	      amt += (count - 1) * sizeof (asection *);
4073
	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4509
	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4074
	      if (m == NULL)
4510
	      if (m == NULL)
4075
		goto error_return;
4511
		goto error_return;
4076
	      m->next = NULL;
4512
	      m->next = NULL;
4077
	      m->p_type = PT_NOTE;
4513
	      m->p_type = PT_NOTE;
4078
	      m->count = count;
4514
	      m->count = count;
4079
	      while (count > 1)
4515
	      while (count > 1)
4080
		{
4516
		{
4081
		  m->sections[m->count - count--] = s;
4517
		  m->sections[m->count - count--] = s;
4082
		  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4518
		  BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4083
		  s = s->next;
4519
		  s = s->next;
4084
		}
4520
		}
4085
	      m->sections[m->count - 1] = s;
4521
	      m->sections[m->count - 1] = s;
4086
	      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4522
	      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
4087
	      *pm = m;
4523
	      *pm = m;
4088
	      pm = &m->next;
4524
	      pm = &m->next;
4089
	    }
4525
	    }
4090
	  if (s->flags & SEC_THREAD_LOCAL)
4526
	  if (s->flags & SEC_THREAD_LOCAL)
4091
	    {
4527
	    {
4092
	      if (! tls_count)
4528
	      if (! tls_count)
4093
		first_tls = s;
4529
		first_tls = s;
4094
	      tls_count++;
4530
	      tls_count++;
4095
	    }
4531
	    }
4096
	}
4532
	}
4097
 
4533
 
4098
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
4534
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
4099
      if (tls_count > 0)
4535
      if (tls_count > 0)
4100
	{
4536
	{
4101
	  amt = sizeof (struct elf_segment_map);
4537
	  amt = sizeof (struct elf_segment_map);
4102
	  amt += (tls_count - 1) * sizeof (asection *);
4538
	  amt += (tls_count - 1) * sizeof (asection *);
4103
	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4539
	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4104
	  if (m == NULL)
4540
	  if (m == NULL)
4105
	    goto error_return;
4541
	    goto error_return;
4106
	  m->next = NULL;
4542
	  m->next = NULL;
4107
	  m->p_type = PT_TLS;
4543
	  m->p_type = PT_TLS;
4108
	  m->count = tls_count;
4544
	  m->count = tls_count;
4109
	  /* Mandated PF_R.  */
4545
	  /* Mandated PF_R.  */
4110
	  m->p_flags = PF_R;
4546
	  m->p_flags = PF_R;
4111
	  m->p_flags_valid = 1;
4547
	  m->p_flags_valid = 1;
-
 
4548
	  s = first_tls;
4112
	  for (i = 0; i < (unsigned int) tls_count; ++i)
4549
	  for (i = 0; i < (unsigned int) tls_count; ++i)
4113
	    {
4550
	    {
4114
	      BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
4551
	      if ((s->flags & SEC_THREAD_LOCAL) == 0)
-
 
4552
		{
-
 
4553
		  _bfd_error_handler
-
 
4554
		    (_("%B: TLS sections are not adjacent:"), abfd);
-
 
4555
		  s = first_tls;
-
 
4556
		  i = 0;
-
 
4557
		  while (i < (unsigned int) tls_count)
-
 
4558
		    {
-
 
4559
		      if ((s->flags & SEC_THREAD_LOCAL) != 0)
-
 
4560
			{
-
 
4561
			  _bfd_error_handler (_("	    TLS: %A"), s);
-
 
4562
			  i++;
-
 
4563
			}
-
 
4564
		      else
-
 
4565
			_bfd_error_handler (_("	non-TLS: %A"), s);
-
 
4566
		      s = s->next;
-
 
4567
		    }
-
 
4568
		  bfd_set_error (bfd_error_bad_value);
-
 
4569
		  goto error_return;
-
 
4570
		}
4115
	      m->sections[i] = first_tls;
4571
	      m->sections[i] = s;
4116
	      first_tls = first_tls->next;
4572
	      s = s->next;
4117
	    }
4573
	    }
4118
 
4574
 
4119
	  *pm = m;
4575
	  *pm = m;
4120
	  pm = &m->next;
4576
	  pm = &m->next;
4121
	}
4577
	}
4122
 
4578
 
4123
      /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4579
      /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
4124
	 segment.  */
4580
	 segment.  */
4125
      eh_frame_hdr = elf_eh_frame_hdr (abfd);
4581
      eh_frame_hdr = elf_eh_frame_hdr (abfd);
4126
      if (eh_frame_hdr != NULL
4582
      if (eh_frame_hdr != NULL
4127
	  && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
4583
	  && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
4128
	{
4584
	{
4129
	  amt = sizeof (struct elf_segment_map);
4585
	  amt = sizeof (struct elf_segment_map);
4130
	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4586
	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4131
	  if (m == NULL)
4587
	  if (m == NULL)
4132
	    goto error_return;
4588
	    goto error_return;
4133
	  m->next = NULL;
4589
	  m->next = NULL;
4134
	  m->p_type = PT_GNU_EH_FRAME;
4590
	  m->p_type = PT_GNU_EH_FRAME;
4135
	  m->count = 1;
4591
	  m->count = 1;
4136
	  m->sections[0] = eh_frame_hdr->output_section;
4592
	  m->sections[0] = eh_frame_hdr->output_section;
4137
 
4593
 
4138
	  *pm = m;
4594
	  *pm = m;
4139
	  pm = &m->next;
4595
	  pm = &m->next;
4140
	}
4596
	}
4141
 
4597
 
4142
      if (elf_stack_flags (abfd))
4598
      if (elf_stack_flags (abfd))
4143
	{
4599
	{
4144
	  amt = sizeof (struct elf_segment_map);
4600
	  amt = sizeof (struct elf_segment_map);
4145
	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4601
	  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4146
	  if (m == NULL)
4602
	  if (m == NULL)
4147
	    goto error_return;
4603
	    goto error_return;
4148
	  m->next = NULL;
4604
	  m->next = NULL;
4149
	  m->p_type = PT_GNU_STACK;
4605
	  m->p_type = PT_GNU_STACK;
4150
	  m->p_flags = elf_stack_flags (abfd);
4606
	  m->p_flags = elf_stack_flags (abfd);
4151
	  m->p_align = bed->stack_align;
4607
	  m->p_align = bed->stack_align;
4152
	  m->p_flags_valid = 1;
4608
	  m->p_flags_valid = 1;
4153
	  m->p_align_valid = m->p_align != 0;
4609
	  m->p_align_valid = m->p_align != 0;
4154
	  if (info->stacksize > 0)
4610
	  if (info->stacksize > 0)
4155
	    {
4611
	    {
4156
	      m->p_size = info->stacksize;
4612
	      m->p_size = info->stacksize;
4157
	      m->p_size_valid = 1;
4613
	      m->p_size_valid = 1;
4158
	    }
4614
	    }
4159
 
4615
 
4160
	  *pm = m;
4616
	  *pm = m;
4161
	  pm = &m->next;
4617
	  pm = &m->next;
4162
	}
4618
	}
4163
 
4619
 
4164
      if (info != NULL && info->relro)
4620
      if (info != NULL && info->relro)
4165
	{
4621
	{
4166
	  for (m = mfirst; m != NULL; m = m->next)
4622
	  for (m = mfirst; m != NULL; m = m->next)
4167
	    {
4623
	    {
4168
	      if (m->p_type == PT_LOAD
4624
	      if (m->p_type == PT_LOAD
4169
		  && m->count != 0
4625
		  && m->count != 0
4170
		  && m->sections[0]->vma >= info->relro_start
4626
		  && m->sections[0]->vma >= info->relro_start
4171
		  && m->sections[0]->vma < info->relro_end)
4627
		  && m->sections[0]->vma < info->relro_end)
4172
		{
4628
		{
4173
		  i = m->count;
4629
		  i = m->count;
4174
		  while (--i != (unsigned) -1)
4630
		  while (--i != (unsigned) -1)
4175
		    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
4631
		    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS))
4176
			== (SEC_LOAD | SEC_HAS_CONTENTS))
4632
			== (SEC_LOAD | SEC_HAS_CONTENTS))
4177
		      break;
4633
		      break;
4178
 
4634
 
4179
		  if (i == (unsigned) -1)
-
 
4180
		    continue;
-
 
4181
 
-
 
4182
		  if (m->sections[i]->vma + m->sections[i]->size
-
 
4183
		      >= info->relro_end)
4635
		  if (i != (unsigned) -1)
4184
		    break;
4636
		    break;
4185
		}
4637
		}
4186
	    }
4638
	    }
4187
 
4639
 
4188
	  /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
4640
	  /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
4189
	  if (m != NULL)
4641
	  if (m != NULL)
4190
	    {
4642
	    {
4191
	      amt = sizeof (struct elf_segment_map);
4643
	      amt = sizeof (struct elf_segment_map);
4192
	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4644
	      m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4193
	      if (m == NULL)
4645
	      if (m == NULL)
4194
		goto error_return;
4646
		goto error_return;
4195
	      m->next = NULL;
4647
	      m->next = NULL;
4196
	      m->p_type = PT_GNU_RELRO;
4648
	      m->p_type = PT_GNU_RELRO;
4197
	      m->p_flags = PF_R;
-
 
4198
	      m->p_flags_valid = 1;
-
 
4199
 
-
 
4200
	      *pm = m;
4649
	      *pm = m;
4201
	      pm = &m->next;
4650
	      pm = &m->next;
4202
	    }
4651
	    }
4203
	}
4652
	}
4204
 
4653
 
4205
      free (sections);
4654
      free (sections);
4206
      elf_seg_map (abfd) = mfirst;
4655
      elf_seg_map (abfd) = mfirst;
4207
    }
4656
    }
4208
 
4657
 
4209
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4658
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs))
4210
    return FALSE;
4659
    return FALSE;
4211
 
4660
 
4212
  for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
4661
  for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
4213
    ++count;
4662
    ++count;
4214
  elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
4663
  elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
4215
 
4664
 
4216
  return TRUE;
4665
  return TRUE;
4217
 
4666
 
4218
 error_return:
4667
 error_return:
4219
  if (sections != NULL)
4668
  if (sections != NULL)
4220
    free (sections);
4669
    free (sections);
4221
  return FALSE;
4670
  return FALSE;
4222
}
4671
}
4223
 
4672
 
4224
/* Sort sections by address.  */
4673
/* Sort sections by address.  */
4225
 
4674
 
4226
static int
4675
static int
4227
elf_sort_sections (const void *arg1, const void *arg2)
4676
elf_sort_sections (const void *arg1, const void *arg2)
4228
{
4677
{
4229
  const asection *sec1 = *(const asection **) arg1;
4678
  const asection *sec1 = *(const asection **) arg1;
4230
  const asection *sec2 = *(const asection **) arg2;
4679
  const asection *sec2 = *(const asection **) arg2;
4231
  bfd_size_type size1, size2;
4680
  bfd_size_type size1, size2;
4232
 
4681
 
4233
  /* Sort by LMA first, since this is the address used to
4682
  /* Sort by LMA first, since this is the address used to
4234
     place the section into a segment.  */
4683
     place the section into a segment.  */
4235
  if (sec1->lma < sec2->lma)
4684
  if (sec1->lma < sec2->lma)
4236
    return -1;
4685
    return -1;
4237
  else if (sec1->lma > sec2->lma)
4686
  else if (sec1->lma > sec2->lma)
4238
    return 1;
4687
    return 1;
4239
 
4688
 
4240
  /* Then sort by VMA.  Normally the LMA and the VMA will be
4689
  /* Then sort by VMA.  Normally the LMA and the VMA will be
4241
     the same, and this will do nothing.  */
4690
     the same, and this will do nothing.  */
4242
  if (sec1->vma < sec2->vma)
4691
  if (sec1->vma < sec2->vma)
4243
    return -1;
4692
    return -1;
4244
  else if (sec1->vma > sec2->vma)
4693
  else if (sec1->vma > sec2->vma)
4245
    return 1;
4694
    return 1;
4246
 
4695
 
4247
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
4696
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
4248
 
4697
 
4249
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4698
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0)
4250
 
4699
 
4251
  if (TOEND (sec1))
4700
  if (TOEND (sec1))
4252
    {
4701
    {
4253
      if (TOEND (sec2))
4702
      if (TOEND (sec2))
4254
	{
4703
	{
4255
	  /* If the indicies are the same, do not return 0
4704
	  /* If the indicies are the same, do not return 0
4256
	     here, but continue to try the next comparison.  */
4705
	     here, but continue to try the next comparison.  */
4257
	  if (sec1->target_index - sec2->target_index != 0)
4706
	  if (sec1->target_index - sec2->target_index != 0)
4258
	    return sec1->target_index - sec2->target_index;
4707
	    return sec1->target_index - sec2->target_index;
4259
	}
4708
	}
4260
      else
4709
      else
4261
	return 1;
4710
	return 1;
4262
    }
4711
    }
4263
  else if (TOEND (sec2))
4712
  else if (TOEND (sec2))
4264
    return -1;
4713
    return -1;
4265
 
4714
 
4266
#undef TOEND
4715
#undef TOEND
4267
 
4716
 
4268
  /* Sort by size, to put zero sized sections
4717
  /* Sort by size, to put zero sized sections
4269
     before others at the same address.  */
4718
     before others at the same address.  */
4270
 
4719
 
4271
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4720
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
4272
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4721
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
4273
 
4722
 
4274
  if (size1 < size2)
4723
  if (size1 < size2)
4275
    return -1;
4724
    return -1;
4276
  if (size1 > size2)
4725
  if (size1 > size2)
4277
    return 1;
4726
    return 1;
4278
 
4727
 
4279
  return sec1->target_index - sec2->target_index;
4728
  return sec1->target_index - sec2->target_index;
4280
}
4729
}
4281
 
4730
 
4282
/* Ian Lance Taylor writes:
4731
/* Ian Lance Taylor writes:
4283
 
4732
 
4284
   We shouldn't be using % with a negative signed number.  That's just
4733
   We shouldn't be using % with a negative signed number.  That's just
4285
   not good.  We have to make sure either that the number is not
4734
   not good.  We have to make sure either that the number is not
4286
   negative, or that the number has an unsigned type.  When the types
4735
   negative, or that the number has an unsigned type.  When the types
4287
   are all the same size they wind up as unsigned.  When file_ptr is a
4736
   are all the same size they wind up as unsigned.  When file_ptr is a
4288
   larger signed type, the arithmetic winds up as signed long long,
4737
   larger signed type, the arithmetic winds up as signed long long,
4289
   which is wrong.
4738
   which is wrong.
4290
 
4739
 
4291
   What we're trying to say here is something like ``increase OFF by
4740
   What we're trying to say here is something like ``increase OFF by
4292
   the least amount that will cause it to be equal to the VMA modulo
4741
   the least amount that will cause it to be equal to the VMA modulo
4293
   the page size.''  */
4742
   the page size.''  */
4294
/* In other words, something like:
4743
/* In other words, something like:
4295
 
4744
 
4296
   vma_offset = m->sections[0]->vma % bed->maxpagesize;
4745
   vma_offset = m->sections[0]->vma % bed->maxpagesize;
4297
   off_offset = off % bed->maxpagesize;
4746
   off_offset = off % bed->maxpagesize;
4298
   if (vma_offset < off_offset)
4747
   if (vma_offset < off_offset)
4299
     adjustment = vma_offset + bed->maxpagesize - off_offset;
4748
     adjustment = vma_offset + bed->maxpagesize - off_offset;
4300
   else
4749
   else
4301
     adjustment = vma_offset - off_offset;
4750
     adjustment = vma_offset - off_offset;
4302
 
4751
 
4303
   which can can be collapsed into the expression below.  */
4752
   which can can be collapsed into the expression below.  */
4304
 
4753
 
4305
static file_ptr
4754
static file_ptr
4306
vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4755
vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
4307
{
4756
{
-
 
4757
  /* PR binutils/16199: Handle an alignment of zero.  */
-
 
4758
  if (maxpagesize == 0)
-
 
4759
    maxpagesize = 1;
4308
  return ((vma - off) % maxpagesize);
4760
  return ((vma - off) % maxpagesize);
4309
}
4761
}
4310
 
4762
 
4311
static void
4763
static void
4312
print_segment_map (const struct elf_segment_map *m)
4764
print_segment_map (const struct elf_segment_map *m)
4313
{
4765
{
4314
  unsigned int j;
4766
  unsigned int j;
4315
  const char *pt = get_segment_type (m->p_type);
4767
  const char *pt = get_segment_type (m->p_type);
4316
  char buf[32];
4768
  char buf[32];
4317
 
4769
 
4318
  if (pt == NULL)
4770
  if (pt == NULL)
4319
    {
4771
    {
4320
      if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4772
      if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
4321
	sprintf (buf, "LOPROC+%7.7x",
4773
	sprintf (buf, "LOPROC+%7.7x",
4322
		 (unsigned int) (m->p_type - PT_LOPROC));
4774
		 (unsigned int) (m->p_type - PT_LOPROC));
4323
      else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4775
      else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
4324
	sprintf (buf, "LOOS+%7.7x",
4776
	sprintf (buf, "LOOS+%7.7x",
4325
		 (unsigned int) (m->p_type - PT_LOOS));
4777
		 (unsigned int) (m->p_type - PT_LOOS));
4326
      else
4778
      else
4327
	snprintf (buf, sizeof (buf), "%8.8x",
4779
	snprintf (buf, sizeof (buf), "%8.8x",
4328
		  (unsigned int) m->p_type);
4780
		  (unsigned int) m->p_type);
4329
      pt = buf;
4781
      pt = buf;
4330
    }
4782
    }
4331
  fflush (stdout);
4783
  fflush (stdout);
4332
  fprintf (stderr, "%s:", pt);
4784
  fprintf (stderr, "%s:", pt);
4333
  for (j = 0; j < m->count; j++)
4785
  for (j = 0; j < m->count; j++)
4334
    fprintf (stderr, " %s", m->sections [j]->name);
4786
    fprintf (stderr, " %s", m->sections [j]->name);
4335
  putc ('\n',stderr);
4787
  putc ('\n',stderr);
4336
  fflush (stderr);
4788
  fflush (stderr);
4337
}
4789
}
4338
 
4790
 
4339
static bfd_boolean
4791
static bfd_boolean
4340
write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
4792
write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len)
4341
{
4793
{
4342
  void *buf;
4794
  void *buf;
4343
  bfd_boolean ret;
4795
  bfd_boolean ret;
4344
 
4796
 
4345
  if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4797
  if (bfd_seek (abfd, pos, SEEK_SET) != 0)
4346
    return FALSE;
4798
    return FALSE;
4347
  buf = bfd_zmalloc (len);
4799
  buf = bfd_zmalloc (len);
4348
  if (buf == NULL)
4800
  if (buf == NULL)
4349
    return FALSE;
4801
    return FALSE;
4350
  ret = bfd_bwrite (buf, len, abfd) == len;
4802
  ret = bfd_bwrite (buf, len, abfd) == len;
4351
  free (buf);
4803
  free (buf);
4352
  return ret;
4804
  return ret;
4353
}
4805
}
4354
 
4806
 
4355
/* Assign file positions to the sections based on the mapping from
4807
/* Assign file positions to the sections based on the mapping from
4356
   sections to segments.  This function also sets up some fields in
4808
   sections to segments.  This function also sets up some fields in
4357
   the file header.  */
4809
   the file header.  */
4358
 
4810
 
4359
static bfd_boolean
4811
static bfd_boolean
4360
assign_file_positions_for_load_sections (bfd *abfd,
4812
assign_file_positions_for_load_sections (bfd *abfd,
4361
					 struct bfd_link_info *link_info)
4813
					 struct bfd_link_info *link_info)
4362
{
4814
{
4363
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4815
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4364
  struct elf_segment_map *m;
4816
  struct elf_segment_map *m;
4365
  Elf_Internal_Phdr *phdrs;
4817
  Elf_Internal_Phdr *phdrs;
4366
  Elf_Internal_Phdr *p;
4818
  Elf_Internal_Phdr *p;
4367
  file_ptr off;
4819
  file_ptr off;
4368
  bfd_size_type maxpagesize;
4820
  bfd_size_type maxpagesize;
4369
  unsigned int alloc;
4821
  unsigned int alloc;
4370
  unsigned int i, j;
4822
  unsigned int i, j;
4371
  bfd_vma header_pad = 0;
4823
  bfd_vma header_pad = 0;
4372
 
4824
 
4373
  if (link_info == NULL
4825
  if (link_info == NULL
4374
      && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4826
      && !_bfd_elf_map_sections_to_segments (abfd, link_info))
4375
    return FALSE;
4827
    return FALSE;
4376
 
4828
 
4377
  alloc = 0;
4829
  alloc = 0;
4378
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
4830
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
4379
    {
4831
    {
4380
      ++alloc;
4832
      ++alloc;
4381
      if (m->header_size)
4833
      if (m->header_size)
4382
	header_pad = m->header_size;
4834
	header_pad = m->header_size;
4383
    }
4835
    }
4384
 
4836
 
4385
  if (alloc)
4837
  if (alloc)
4386
    {
4838
    {
4387
      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4839
      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
4388
      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4840
      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
4389
    }
4841
    }
4390
  else
4842
  else
4391
    {
4843
    {
4392
      /* PR binutils/12467.  */
4844
      /* PR binutils/12467.  */
4393
      elf_elfheader (abfd)->e_phoff = 0;
4845
      elf_elfheader (abfd)->e_phoff = 0;
4394
      elf_elfheader (abfd)->e_phentsize = 0;
4846
      elf_elfheader (abfd)->e_phentsize = 0;
4395
    }
4847
    }
4396
 
4848
 
4397
  elf_elfheader (abfd)->e_phnum = alloc;
4849
  elf_elfheader (abfd)->e_phnum = alloc;
4398
 
4850
 
4399
  if (elf_program_header_size (abfd) == (bfd_size_type) -1)
4851
  if (elf_program_header_size (abfd) == (bfd_size_type) -1)
4400
    elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
4852
    elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
4401
  else
4853
  else
4402
    BFD_ASSERT (elf_program_header_size (abfd)
4854
    BFD_ASSERT (elf_program_header_size (abfd)
4403
		>= alloc * bed->s->sizeof_phdr);
4855
		>= alloc * bed->s->sizeof_phdr);
4404
 
4856
 
4405
  if (alloc == 0)
4857
  if (alloc == 0)
4406
    {
4858
    {
4407
      elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
4859
      elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
4408
      return TRUE;
4860
      return TRUE;
4409
    }
4861
    }
4410
 
4862
 
4411
  /* We're writing the size in elf_program_header_size (abfd),
4863
  /* We're writing the size in elf_program_header_size (abfd),
4412
     see assign_file_positions_except_relocs, so make sure we have
4864
     see assign_file_positions_except_relocs, so make sure we have
4413
     that amount allocated, with trailing space cleared.
4865
     that amount allocated, with trailing space cleared.
4414
     The variable alloc contains the computed need, while
4866
     The variable alloc contains the computed need, while
4415
     elf_program_header_size (abfd) contains the size used for the
4867
     elf_program_header_size (abfd) contains the size used for the
4416
     layout.
4868
     layout.
4417
     See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4869
     See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
4418
     where the layout is forced to according to a larger size in the
4870
     where the layout is forced to according to a larger size in the
4419
     last iterations for the testcase ld-elf/header.  */
4871
     last iterations for the testcase ld-elf/header.  */
4420
  BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
4872
  BFD_ASSERT (elf_program_header_size (abfd) % bed->s->sizeof_phdr
4421
	      == 0);
4873
	      == 0);
4422
  phdrs = (Elf_Internal_Phdr *)
4874
  phdrs = (Elf_Internal_Phdr *)
4423
     bfd_zalloc2 (abfd,
4875
     bfd_zalloc2 (abfd,
4424
                  (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
4876
                  (elf_program_header_size (abfd) / bed->s->sizeof_phdr),
4425
                  sizeof (Elf_Internal_Phdr));
4877
                  sizeof (Elf_Internal_Phdr));
4426
  elf_tdata (abfd)->phdr = phdrs;
4878
  elf_tdata (abfd)->phdr = phdrs;
4427
  if (phdrs == NULL)
4879
  if (phdrs == NULL)
4428
    return FALSE;
4880
    return FALSE;
4429
 
4881
 
4430
  maxpagesize = 1;
4882
  maxpagesize = 1;
4431
  if ((abfd->flags & D_PAGED) != 0)
4883
  if ((abfd->flags & D_PAGED) != 0)
4432
    maxpagesize = bed->maxpagesize;
4884
    maxpagesize = bed->maxpagesize;
4433
 
4885
 
4434
  off = bed->s->sizeof_ehdr;
4886
  off = bed->s->sizeof_ehdr;
4435
  off += alloc * bed->s->sizeof_phdr;
4887
  off += alloc * bed->s->sizeof_phdr;
4436
  if (header_pad < (bfd_vma) off)
4888
  if (header_pad < (bfd_vma) off)
4437
    header_pad = 0;
4889
    header_pad = 0;
4438
  else
4890
  else
4439
    header_pad -= off;
4891
    header_pad -= off;
4440
  off += header_pad;
4892
  off += header_pad;
4441
 
4893
 
4442
  for (m = elf_seg_map (abfd), p = phdrs, j = 0;
4894
  for (m = elf_seg_map (abfd), p = phdrs, j = 0;
4443
       m != NULL;
4895
       m != NULL;
4444
       m = m->next, p++, j++)
4896
       m = m->next, p++, j++)
4445
    {
4897
    {
4446
      asection **secpp;
4898
      asection **secpp;
4447
      bfd_vma off_adjust;
4899
      bfd_vma off_adjust;
4448
      bfd_boolean no_contents;
4900
      bfd_boolean no_contents;
4449
 
4901
 
4450
      /* If elf_segment_map is not from map_sections_to_segments, the
4902
      /* If elf_segment_map is not from map_sections_to_segments, the
4451
	 sections may not be correctly ordered.  NOTE: sorting should
4903
	 sections may not be correctly ordered.  NOTE: sorting should
4452
	 not be done to the PT_NOTE section of a corefile, which may
4904
	 not be done to the PT_NOTE section of a corefile, which may
4453
	 contain several pseudo-sections artificially created by bfd.
4905
	 contain several pseudo-sections artificially created by bfd.
4454
	 Sorting these pseudo-sections breaks things badly.  */
4906
	 Sorting these pseudo-sections breaks things badly.  */
4455
      if (m->count > 1
4907
      if (m->count > 1
4456
	  && !(elf_elfheader (abfd)->e_type == ET_CORE
4908
	  && !(elf_elfheader (abfd)->e_type == ET_CORE
4457
	       && m->p_type == PT_NOTE))
4909
	       && m->p_type == PT_NOTE))
4458
	qsort (m->sections, (size_t) m->count, sizeof (asection *),
4910
	qsort (m->sections, (size_t) m->count, sizeof (asection *),
4459
	       elf_sort_sections);
4911
	       elf_sort_sections);
4460
 
4912
 
4461
      /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4913
      /* An ELF segment (described by Elf_Internal_Phdr) may contain a
4462
	 number of sections with contents contributing to both p_filesz
4914
	 number of sections with contents contributing to both p_filesz
4463
	 and p_memsz, followed by a number of sections with no contents
4915
	 and p_memsz, followed by a number of sections with no contents
4464
	 that just contribute to p_memsz.  In this loop, OFF tracks next
4916
	 that just contribute to p_memsz.  In this loop, OFF tracks next
4465
	 available file offset for PT_LOAD and PT_NOTE segments.  */
4917
	 available file offset for PT_LOAD and PT_NOTE segments.  */
4466
      p->p_type = m->p_type;
4918
      p->p_type = m->p_type;
4467
      p->p_flags = m->p_flags;
4919
      p->p_flags = m->p_flags;
4468
 
4920
 
4469
      if (m->count == 0)
4921
      if (m->count == 0)
4470
	p->p_vaddr = 0;
4922
	p->p_vaddr = 0;
4471
      else
4923
      else
4472
	p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4924
	p->p_vaddr = m->sections[0]->vma - m->p_vaddr_offset;
4473
 
4925
 
4474
      if (m->p_paddr_valid)
4926
      if (m->p_paddr_valid)
4475
	p->p_paddr = m->p_paddr;
4927
	p->p_paddr = m->p_paddr;
4476
      else if (m->count == 0)
4928
      else if (m->count == 0)
4477
	p->p_paddr = 0;
4929
	p->p_paddr = 0;
4478
      else
4930
      else
4479
	p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4931
	p->p_paddr = m->sections[0]->lma - m->p_vaddr_offset;
4480
 
4932
 
4481
      if (p->p_type == PT_LOAD
4933
      if (p->p_type == PT_LOAD
4482
	  && (abfd->flags & D_PAGED) != 0)
4934
	  && (abfd->flags & D_PAGED) != 0)
4483
	{
4935
	{
4484
	  /* p_align in demand paged PT_LOAD segments effectively stores
4936
	  /* p_align in demand paged PT_LOAD segments effectively stores
4485
	     the maximum page size.  When copying an executable with
4937
	     the maximum page size.  When copying an executable with
4486
	     objcopy, we set m->p_align from the input file.  Use this
4938
	     objcopy, we set m->p_align from the input file.  Use this
4487
	     value for maxpagesize rather than bed->maxpagesize, which
4939
	     value for maxpagesize rather than bed->maxpagesize, which
4488
	     may be different.  Note that we use maxpagesize for PT_TLS
4940
	     may be different.  Note that we use maxpagesize for PT_TLS
4489
	     segment alignment later in this function, so we are relying
4941
	     segment alignment later in this function, so we are relying
4490
	     on at least one PT_LOAD segment appearing before a PT_TLS
4942
	     on at least one PT_LOAD segment appearing before a PT_TLS
4491
	     segment.  */
4943
	     segment.  */
4492
	  if (m->p_align_valid)
4944
	  if (m->p_align_valid)
4493
	    maxpagesize = m->p_align;
4945
	    maxpagesize = m->p_align;
4494
 
4946
 
4495
	  p->p_align = maxpagesize;
4947
	  p->p_align = maxpagesize;
4496
	}
4948
	}
4497
      else if (m->p_align_valid)
4949
      else if (m->p_align_valid)
4498
	p->p_align = m->p_align;
4950
	p->p_align = m->p_align;
4499
      else if (m->count == 0)
4951
      else if (m->count == 0)
4500
	p->p_align = 1 << bed->s->log_file_align;
4952
	p->p_align = 1 << bed->s->log_file_align;
4501
      else
4953
      else
4502
	p->p_align = 0;
4954
	p->p_align = 0;
4503
 
4955
 
4504
      no_contents = FALSE;
4956
      no_contents = FALSE;
4505
      off_adjust = 0;
4957
      off_adjust = 0;
4506
      if (p->p_type == PT_LOAD
4958
      if (p->p_type == PT_LOAD
4507
	  && m->count > 0)
4959
	  && m->count > 0)
4508
	{
4960
	{
4509
	  bfd_size_type align;
4961
	  bfd_size_type align;
4510
	  unsigned int align_power = 0;
4962
	  unsigned int align_power = 0;
4511
 
4963
 
4512
	  if (m->p_align_valid)
4964
	  if (m->p_align_valid)
4513
	    align = p->p_align;
4965
	    align = p->p_align;
4514
	  else
4966
	  else
4515
	    {
4967
	    {
4516
	      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4968
	      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4517
		{
4969
		{
4518
		  unsigned int secalign;
4970
		  unsigned int secalign;
4519
 
4971
 
4520
		  secalign = bfd_get_section_alignment (abfd, *secpp);
4972
		  secalign = bfd_get_section_alignment (abfd, *secpp);
4521
		  if (secalign > align_power)
4973
		  if (secalign > align_power)
4522
		    align_power = secalign;
4974
		    align_power = secalign;
4523
		}
4975
		}
4524
	      align = (bfd_size_type) 1 << align_power;
4976
	      align = (bfd_size_type) 1 << align_power;
4525
	      if (align < maxpagesize)
4977
	      if (align < maxpagesize)
4526
		align = maxpagesize;
4978
		align = maxpagesize;
4527
	    }
4979
	    }
4528
 
4980
 
4529
	  for (i = 0; i < m->count; i++)
4981
	  for (i = 0; i < m->count; i++)
4530
	    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4982
	    if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
4531
	      /* If we aren't making room for this section, then
4983
	      /* If we aren't making room for this section, then
4532
		 it must be SHT_NOBITS regardless of what we've
4984
		 it must be SHT_NOBITS regardless of what we've
4533
		 set via struct bfd_elf_special_section.  */
4985
		 set via struct bfd_elf_special_section.  */
4534
	      elf_section_type (m->sections[i]) = SHT_NOBITS;
4986
	      elf_section_type (m->sections[i]) = SHT_NOBITS;
4535
 
4987
 
4536
	  /* Find out whether this segment contains any loadable
4988
	  /* Find out whether this segment contains any loadable
4537
	     sections.  */
4989
	     sections.  */
4538
	  no_contents = TRUE;
4990
	  no_contents = TRUE;
4539
	  for (i = 0; i < m->count; i++)
4991
	  for (i = 0; i < m->count; i++)
4540
	    if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4992
	    if (elf_section_type (m->sections[i]) != SHT_NOBITS)
4541
	      {
4993
	      {
4542
		no_contents = FALSE;
4994
		no_contents = FALSE;
4543
		break;
4995
		break;
4544
	      }
4996
	      }
4545
 
4997
 
4546
	  off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
4998
	  off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align);
4547
	  off += off_adjust;
4999
	  off += off_adjust;
4548
	  if (no_contents)
5000
	  if (no_contents)
4549
	    {
5001
	    {
4550
	      /* We shouldn't need to align the segment on disk since
5002
	      /* We shouldn't need to align the segment on disk since
4551
		 the segment doesn't need file space, but the gABI
5003
		 the segment doesn't need file space, but the gABI
4552
		 arguably requires the alignment and glibc ld.so
5004
		 arguably requires the alignment and glibc ld.so
4553
		 checks it.  So to comply with the alignment
5005
		 checks it.  So to comply with the alignment
4554
		 requirement but not waste file space, we adjust
5006
		 requirement but not waste file space, we adjust
4555
		 p_offset for just this segment.  (OFF_ADJUST is
5007
		 p_offset for just this segment.  (OFF_ADJUST is
4556
		 subtracted from OFF later.)  This may put p_offset
5008
		 subtracted from OFF later.)  This may put p_offset
4557
		 past the end of file, but that shouldn't matter.  */
5009
		 past the end of file, but that shouldn't matter.  */
4558
	    }
5010
	    }
4559
	  else
5011
	  else
4560
	    off_adjust = 0;
5012
	    off_adjust = 0;
4561
	}
5013
	}
4562
      /* Make sure the .dynamic section is the first section in the
5014
      /* Make sure the .dynamic section is the first section in the
4563
	 PT_DYNAMIC segment.  */
5015
	 PT_DYNAMIC segment.  */
4564
      else if (p->p_type == PT_DYNAMIC
5016
      else if (p->p_type == PT_DYNAMIC
4565
	       && m->count > 1
5017
	       && m->count > 1
4566
	       && strcmp (m->sections[0]->name, ".dynamic") != 0)
5018
	       && strcmp (m->sections[0]->name, ".dynamic") != 0)
4567
	{
5019
	{
4568
	  _bfd_error_handler
5020
	  _bfd_error_handler
4569
	    (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
5021
	    (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"),
4570
	     abfd);
5022
	     abfd);
4571
	  bfd_set_error (bfd_error_bad_value);
5023
	  bfd_set_error (bfd_error_bad_value);
4572
	  return FALSE;
5024
	  return FALSE;
4573
	}
5025
	}
4574
      /* Set the note section type to SHT_NOTE.  */
5026
      /* Set the note section type to SHT_NOTE.  */
4575
      else if (p->p_type == PT_NOTE)
5027
      else if (p->p_type == PT_NOTE)
4576
	for (i = 0; i < m->count; i++)
5028
	for (i = 0; i < m->count; i++)
4577
	  elf_section_type (m->sections[i]) = SHT_NOTE;
5029
	  elf_section_type (m->sections[i]) = SHT_NOTE;
4578
 
5030
 
4579
      p->p_offset = 0;
5031
      p->p_offset = 0;
4580
      p->p_filesz = 0;
5032
      p->p_filesz = 0;
4581
      p->p_memsz = 0;
5033
      p->p_memsz = 0;
4582
 
5034
 
4583
      if (m->includes_filehdr)
5035
      if (m->includes_filehdr)
4584
	{
5036
	{
4585
	  if (!m->p_flags_valid)
5037
	  if (!m->p_flags_valid)
4586
	    p->p_flags |= PF_R;
5038
	    p->p_flags |= PF_R;
4587
	  p->p_filesz = bed->s->sizeof_ehdr;
5039
	  p->p_filesz = bed->s->sizeof_ehdr;
4588
	  p->p_memsz = bed->s->sizeof_ehdr;
5040
	  p->p_memsz = bed->s->sizeof_ehdr;
4589
	  if (m->count > 0)
5041
	  if (m->count > 0)
4590
	    {
5042
	    {
4591
	      if (p->p_vaddr < (bfd_vma) off)
5043
	      if (p->p_vaddr < (bfd_vma) off)
4592
		{
5044
		{
4593
		  (*_bfd_error_handler)
5045
		  (*_bfd_error_handler)
4594
		    (_("%B: Not enough room for program headers, try linking with -N"),
5046
		    (_("%B: Not enough room for program headers, try linking with -N"),
4595
		     abfd);
5047
		     abfd);
4596
		  bfd_set_error (bfd_error_bad_value);
5048
		  bfd_set_error (bfd_error_bad_value);
4597
		  return FALSE;
5049
		  return FALSE;
4598
		}
5050
		}
4599
 
5051
 
4600
	      p->p_vaddr -= off;
5052
	      p->p_vaddr -= off;
4601
	      if (!m->p_paddr_valid)
5053
	      if (!m->p_paddr_valid)
4602
		p->p_paddr -= off;
5054
		p->p_paddr -= off;
4603
	    }
5055
	    }
4604
	}
5056
	}
4605
 
5057
 
4606
      if (m->includes_phdrs)
5058
      if (m->includes_phdrs)
4607
	{
5059
	{
4608
	  if (!m->p_flags_valid)
5060
	  if (!m->p_flags_valid)
4609
	    p->p_flags |= PF_R;
5061
	    p->p_flags |= PF_R;
4610
 
5062
 
4611
	  if (!m->includes_filehdr)
5063
	  if (!m->includes_filehdr)
4612
	    {
5064
	    {
4613
	      p->p_offset = bed->s->sizeof_ehdr;
5065
	      p->p_offset = bed->s->sizeof_ehdr;
4614
 
5066
 
4615
	      if (m->count > 0)
5067
	      if (m->count > 0)
4616
		{
5068
		{
4617
		  p->p_vaddr -= off - p->p_offset;
5069
		  p->p_vaddr -= off - p->p_offset;
4618
		  if (!m->p_paddr_valid)
5070
		  if (!m->p_paddr_valid)
4619
		    p->p_paddr -= off - p->p_offset;
5071
		    p->p_paddr -= off - p->p_offset;
4620
		}
5072
		}
4621
	    }
5073
	    }
4622
 
5074
 
4623
	  p->p_filesz += alloc * bed->s->sizeof_phdr;
5075
	  p->p_filesz += alloc * bed->s->sizeof_phdr;
4624
	  p->p_memsz += alloc * bed->s->sizeof_phdr;
5076
	  p->p_memsz += alloc * bed->s->sizeof_phdr;
4625
	  if (m->count)
5077
	  if (m->count)
4626
	    {
5078
	    {
4627
	      p->p_filesz += header_pad;
5079
	      p->p_filesz += header_pad;
4628
	      p->p_memsz += header_pad;
5080
	      p->p_memsz += header_pad;
4629
	    }
5081
	    }
4630
	}
5082
	}
4631
 
5083
 
4632
      if (p->p_type == PT_LOAD
5084
      if (p->p_type == PT_LOAD
4633
	  || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
5085
	  || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
4634
	{
5086
	{
4635
	  if (!m->includes_filehdr && !m->includes_phdrs)
5087
	  if (!m->includes_filehdr && !m->includes_phdrs)
4636
	    p->p_offset = off;
5088
	    p->p_offset = off;
4637
	  else
5089
	  else
4638
	    {
5090
	    {
4639
	      file_ptr adjust;
5091
	      file_ptr adjust;
4640
 
5092
 
4641
	      adjust = off - (p->p_offset + p->p_filesz);
5093
	      adjust = off - (p->p_offset + p->p_filesz);
4642
	      if (!no_contents)
5094
	      if (!no_contents)
4643
		p->p_filesz += adjust;
5095
		p->p_filesz += adjust;
4644
	      p->p_memsz += adjust;
5096
	      p->p_memsz += adjust;
4645
	    }
5097
	    }
4646
	}
5098
	}
4647
 
5099
 
4648
      /* Set up p_filesz, p_memsz, p_align and p_flags from the section
5100
      /* Set up p_filesz, p_memsz, p_align and p_flags from the section
4649
	 maps.  Set filepos for sections in PT_LOAD segments, and in
5101
	 maps.  Set filepos for sections in PT_LOAD segments, and in
4650
	 core files, for sections in PT_NOTE segments.
5102
	 core files, for sections in PT_NOTE segments.
4651
	 assign_file_positions_for_non_load_sections will set filepos
5103
	 assign_file_positions_for_non_load_sections will set filepos
4652
	 for other sections and update p_filesz for other segments.  */
5104
	 for other sections and update p_filesz for other segments.  */
4653
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
5105
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
4654
	{
5106
	{
4655
	  asection *sec;
5107
	  asection *sec;
4656
	  bfd_size_type align;
5108
	  bfd_size_type align;
4657
	  Elf_Internal_Shdr *this_hdr;
5109
	  Elf_Internal_Shdr *this_hdr;
4658
 
5110
 
4659
	  sec = *secpp;
5111
	  sec = *secpp;
4660
	  this_hdr = &elf_section_data (sec)->this_hdr;
5112
	  this_hdr = &elf_section_data (sec)->this_hdr;
4661
	  align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
5113
	  align = (bfd_size_type) 1 << bfd_get_section_alignment (abfd, sec);
4662
 
5114
 
4663
	  if ((p->p_type == PT_LOAD
5115
	  if ((p->p_type == PT_LOAD
4664
	       || p->p_type == PT_TLS)
5116
	       || p->p_type == PT_TLS)
4665
	      && (this_hdr->sh_type != SHT_NOBITS
5117
	      && (this_hdr->sh_type != SHT_NOBITS
4666
		  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
5118
		  || ((this_hdr->sh_flags & SHF_ALLOC) != 0
4667
		      && ((this_hdr->sh_flags & SHF_TLS) == 0
5119
		      && ((this_hdr->sh_flags & SHF_TLS) == 0
4668
			  || p->p_type == PT_TLS))))
5120
			  || p->p_type == PT_TLS))))
4669
	    {
5121
	    {
4670
	      bfd_vma p_start = p->p_paddr;
5122
	      bfd_vma p_start = p->p_paddr;
4671
	      bfd_vma p_end = p_start + p->p_memsz;
5123
	      bfd_vma p_end = p_start + p->p_memsz;
4672
	      bfd_vma s_start = sec->lma;
5124
	      bfd_vma s_start = sec->lma;
4673
	      bfd_vma adjust = s_start - p_end;
5125
	      bfd_vma adjust = s_start - p_end;
4674
 
5126
 
4675
	      if (adjust != 0
5127
	      if (adjust != 0
4676
		  && (s_start < p_end
5128
		  && (s_start < p_end
4677
		      || p_end < p_start))
5129
		      || p_end < p_start))
4678
		{
5130
		{
4679
		  (*_bfd_error_handler)
5131
		  (*_bfd_error_handler)
4680
		    (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec,
5132
		    (_("%B: section %A lma %#lx adjusted to %#lx"), abfd, sec,
4681
		     (unsigned long) s_start, (unsigned long) p_end);
5133
		     (unsigned long) s_start, (unsigned long) p_end);
4682
		  adjust = 0;
5134
		  adjust = 0;
4683
		  sec->lma = p_end;
5135
		  sec->lma = p_end;
4684
		}
5136
		}
4685
	      p->p_memsz += adjust;
5137
	      p->p_memsz += adjust;
4686
 
5138
 
4687
	      if (this_hdr->sh_type != SHT_NOBITS)
5139
	      if (this_hdr->sh_type != SHT_NOBITS)
4688
		{
5140
		{
4689
		  if (p->p_filesz + adjust < p->p_memsz)
5141
		  if (p->p_filesz + adjust < p->p_memsz)
4690
		    {
5142
		    {
4691
		      /* We have a PROGBITS section following NOBITS ones.
5143
		      /* We have a PROGBITS section following NOBITS ones.
4692
		         Allocate file space for the NOBITS section(s) and
5144
		         Allocate file space for the NOBITS section(s) and
4693
			 zero it.  */
5145
			 zero it.  */
4694
		      adjust = p->p_memsz - p->p_filesz;
5146
		      adjust = p->p_memsz - p->p_filesz;
4695
		      if (!write_zeros (abfd, off, adjust))
5147
		      if (!write_zeros (abfd, off, adjust))
4696
			return FALSE;
5148
			return FALSE;
4697
		    }
5149
		    }
4698
		  off += adjust;
5150
		  off += adjust;
4699
		  p->p_filesz += adjust;
5151
		  p->p_filesz += adjust;
4700
		}
5152
		}
4701
	    }
5153
	    }
4702
 
5154
 
4703
	  if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
5155
	  if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
4704
	    {
5156
	    {
4705
	      /* The section at i == 0 is the one that actually contains
5157
	      /* The section at i == 0 is the one that actually contains
4706
		 everything.  */
5158
		 everything.  */
4707
	      if (i == 0)
5159
	      if (i == 0)
4708
		{
5160
		{
4709
		  this_hdr->sh_offset = sec->filepos = off;
5161
		  this_hdr->sh_offset = sec->filepos = off;
4710
		  off += this_hdr->sh_size;
5162
		  off += this_hdr->sh_size;
4711
		  p->p_filesz = this_hdr->sh_size;
5163
		  p->p_filesz = this_hdr->sh_size;
4712
		  p->p_memsz = 0;
5164
		  p->p_memsz = 0;
4713
		  p->p_align = 1;
5165
		  p->p_align = 1;
4714
		}
5166
		}
4715
	      else
5167
	      else
4716
		{
5168
		{
4717
		  /* The rest are fake sections that shouldn't be written.  */
5169
		  /* The rest are fake sections that shouldn't be written.  */
4718
		  sec->filepos = 0;
5170
		  sec->filepos = 0;
4719
		  sec->size = 0;
5171
		  sec->size = 0;
4720
		  sec->flags = 0;
5172
		  sec->flags = 0;
4721
		  continue;
5173
		  continue;
4722
		}
5174
		}
4723
	    }
5175
	    }
4724
	  else
5176
	  else
4725
	    {
5177
	    {
4726
	      if (p->p_type == PT_LOAD)
5178
	      if (p->p_type == PT_LOAD)
4727
		{
5179
		{
4728
		  this_hdr->sh_offset = sec->filepos = off;
5180
		  this_hdr->sh_offset = sec->filepos = off;
4729
		  if (this_hdr->sh_type != SHT_NOBITS)
5181
		  if (this_hdr->sh_type != SHT_NOBITS)
4730
		    off += this_hdr->sh_size;
5182
		    off += this_hdr->sh_size;
4731
		}
5183
		}
4732
	      else if (this_hdr->sh_type == SHT_NOBITS
5184
	      else if (this_hdr->sh_type == SHT_NOBITS
4733
		       && (this_hdr->sh_flags & SHF_TLS) != 0
5185
		       && (this_hdr->sh_flags & SHF_TLS) != 0
4734
		       && this_hdr->sh_offset == 0)
5186
		       && this_hdr->sh_offset == 0)
4735
		{
5187
		{
4736
		  /* This is a .tbss section that didn't get a PT_LOAD.
5188
		  /* This is a .tbss section that didn't get a PT_LOAD.
4737
		     (See _bfd_elf_map_sections_to_segments "Create a
5189
		     (See _bfd_elf_map_sections_to_segments "Create a
4738
		     final PT_LOAD".)  Set sh_offset to the value it
5190
		     final PT_LOAD".)  Set sh_offset to the value it
4739
		     would have if we had created a zero p_filesz and
5191
		     would have if we had created a zero p_filesz and
4740
		     p_memsz PT_LOAD header for the section.  This
5192
		     p_memsz PT_LOAD header for the section.  This
4741
		     also makes the PT_TLS header have the same
5193
		     also makes the PT_TLS header have the same
4742
		     p_offset value.  */
5194
		     p_offset value.  */
4743
		  bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
5195
		  bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
4744
							  off, align);
5196
							  off, align);
4745
		  this_hdr->sh_offset = sec->filepos = off + adjust;
5197
		  this_hdr->sh_offset = sec->filepos = off + adjust;
4746
		}
5198
		}
4747
 
5199
 
4748
	      if (this_hdr->sh_type != SHT_NOBITS)
5200
	      if (this_hdr->sh_type != SHT_NOBITS)
4749
		{
5201
		{
4750
		  p->p_filesz += this_hdr->sh_size;
5202
		  p->p_filesz += this_hdr->sh_size;
4751
		  /* A load section without SHF_ALLOC is something like
5203
		  /* A load section without SHF_ALLOC is something like
4752
		     a note section in a PT_NOTE segment.  These take
5204
		     a note section in a PT_NOTE segment.  These take
4753
		     file space but are not loaded into memory.  */
5205
		     file space but are not loaded into memory.  */
4754
		  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5206
		  if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4755
		    p->p_memsz += this_hdr->sh_size;
5207
		    p->p_memsz += this_hdr->sh_size;
4756
		}
5208
		}
4757
	      else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
5209
	      else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
4758
		{
5210
		{
4759
		  if (p->p_type == PT_TLS)
5211
		  if (p->p_type == PT_TLS)
4760
		    p->p_memsz += this_hdr->sh_size;
5212
		    p->p_memsz += this_hdr->sh_size;
4761
 
5213
 
4762
		  /* .tbss is special.  It doesn't contribute to p_memsz of
5214
		  /* .tbss is special.  It doesn't contribute to p_memsz of
4763
		     normal segments.  */
5215
		     normal segments.  */
4764
		  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
5216
		  else if ((this_hdr->sh_flags & SHF_TLS) == 0)
4765
		    p->p_memsz += this_hdr->sh_size;
5217
		    p->p_memsz += this_hdr->sh_size;
4766
		}
5218
		}
4767
 
5219
 
4768
	      if (align > p->p_align
5220
	      if (align > p->p_align
4769
		  && !m->p_align_valid
5221
		  && !m->p_align_valid
4770
		  && (p->p_type != PT_LOAD
5222
		  && (p->p_type != PT_LOAD
4771
		      || (abfd->flags & D_PAGED) == 0))
5223
		      || (abfd->flags & D_PAGED) == 0))
4772
		p->p_align = align;
5224
		p->p_align = align;
4773
	    }
5225
	    }
4774
 
5226
 
4775
	  if (!m->p_flags_valid)
5227
	  if (!m->p_flags_valid)
4776
	    {
5228
	    {
4777
	      p->p_flags |= PF_R;
5229
	      p->p_flags |= PF_R;
4778
	      if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
5230
	      if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
4779
		p->p_flags |= PF_X;
5231
		p->p_flags |= PF_X;
4780
	      if ((this_hdr->sh_flags & SHF_WRITE) != 0)
5232
	      if ((this_hdr->sh_flags & SHF_WRITE) != 0)
4781
		p->p_flags |= PF_W;
5233
		p->p_flags |= PF_W;
4782
	    }
5234
	    }
4783
	}
5235
	}
-
 
5236
 
4784
      off -= off_adjust;
5237
      off -= off_adjust;
4785
 
5238
 
4786
      /* Check that all sections are in a PT_LOAD segment.
5239
      /* Check that all sections are in a PT_LOAD segment.
4787
	 Don't check funky gdb generated core files.  */
5240
	 Don't check funky gdb generated core files.  */
4788
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
5241
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
4789
	{
5242
	{
4790
	  bfd_boolean check_vma = TRUE;
5243
	  bfd_boolean check_vma = TRUE;
4791
 
5244
 
4792
	  for (i = 1; i < m->count; i++)
5245
	  for (i = 1; i < m->count; i++)
4793
	    if (m->sections[i]->vma == m->sections[i - 1]->vma
5246
	    if (m->sections[i]->vma == m->sections[i - 1]->vma
4794
		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
5247
		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
4795
				       ->this_hdr), p) != 0
5248
				       ->this_hdr), p) != 0
4796
		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
5249
		&& ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
4797
				       ->this_hdr), p) != 0)
5250
				       ->this_hdr), p) != 0)
4798
	      {
5251
	      {
4799
		/* Looks like we have overlays packed into the segment.  */
5252
		/* Looks like we have overlays packed into the segment.  */
4800
		check_vma = FALSE;
5253
		check_vma = FALSE;
4801
		break;
5254
		break;
4802
	      }
5255
	      }
4803
 
5256
 
4804
	  for (i = 0; i < m->count; i++)
5257
	  for (i = 0; i < m->count; i++)
4805
	    {
5258
	    {
4806
	      Elf_Internal_Shdr *this_hdr;
5259
	      Elf_Internal_Shdr *this_hdr;
4807
	      asection *sec;
5260
	      asection *sec;
4808
 
5261
 
4809
	      sec = m->sections[i];
5262
	      sec = m->sections[i];
4810
	      this_hdr = &(elf_section_data(sec)->this_hdr);
5263
	      this_hdr = &(elf_section_data(sec)->this_hdr);
4811
	      if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
5264
	      if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
4812
		  && !ELF_TBSS_SPECIAL (this_hdr, p))
5265
		  && !ELF_TBSS_SPECIAL (this_hdr, p))
4813
		{
5266
		{
4814
		  (*_bfd_error_handler)
5267
		  (*_bfd_error_handler)
4815
		    (_("%B: section `%A' can't be allocated in segment %d"),
5268
		    (_("%B: section `%A' can't be allocated in segment %d"),
4816
		     abfd, sec, j);
5269
		     abfd, sec, j);
4817
		  print_segment_map (m);
5270
		  print_segment_map (m);
4818
		}
5271
		}
4819
	    }
5272
	    }
4820
	}
5273
	}
4821
    }
5274
    }
4822
 
5275
 
4823
  elf_next_file_pos (abfd) = off;
5276
  elf_next_file_pos (abfd) = off;
4824
  return TRUE;
5277
  return TRUE;
4825
}
5278
}
4826
 
5279
 
4827
/* Assign file positions for the other sections.  */
5280
/* Assign file positions for the other sections.  */
4828
 
5281
 
4829
static bfd_boolean
5282
static bfd_boolean
4830
assign_file_positions_for_non_load_sections (bfd *abfd,
5283
assign_file_positions_for_non_load_sections (bfd *abfd,
4831
					     struct bfd_link_info *link_info)
5284
					     struct bfd_link_info *link_info)
4832
{
5285
{
4833
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5286
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4834
  Elf_Internal_Shdr **i_shdrpp;
5287
  Elf_Internal_Shdr **i_shdrpp;
4835
  Elf_Internal_Shdr **hdrpp;
5288
  Elf_Internal_Shdr **hdrpp, **end_hdrpp;
4836
  Elf_Internal_Phdr *phdrs;
5289
  Elf_Internal_Phdr *phdrs;
4837
  Elf_Internal_Phdr *p;
5290
  Elf_Internal_Phdr *p;
4838
  struct elf_segment_map *m;
5291
  struct elf_segment_map *m;
4839
  struct elf_segment_map *hdrs_segment;
5292
  struct elf_segment_map *hdrs_segment;
4840
  bfd_vma filehdr_vaddr, filehdr_paddr;
5293
  bfd_vma filehdr_vaddr, filehdr_paddr;
4841
  bfd_vma phdrs_vaddr, phdrs_paddr;
5294
  bfd_vma phdrs_vaddr, phdrs_paddr;
4842
  file_ptr off;
5295
  file_ptr off;
4843
  unsigned int num_sec;
-
 
4844
  unsigned int i;
-
 
4845
  unsigned int count;
5296
  unsigned int count;
4846
 
5297
 
4847
  i_shdrpp = elf_elfsections (abfd);
5298
  i_shdrpp = elf_elfsections (abfd);
4848
  num_sec = elf_numsections (abfd);
5299
  end_hdrpp = i_shdrpp + elf_numsections (abfd);
4849
  off = elf_next_file_pos (abfd);
5300
  off = elf_next_file_pos (abfd);
4850
  for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
5301
  for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
4851
    {
5302
    {
4852
      Elf_Internal_Shdr *hdr;
5303
      Elf_Internal_Shdr *hdr;
4853
 
5304
 
4854
      hdr = *hdrpp;
5305
      hdr = *hdrpp;
4855
      if (hdr->bfd_section != NULL
5306
      if (hdr->bfd_section != NULL
4856
	  && (hdr->bfd_section->filepos != 0
5307
	  && (hdr->bfd_section->filepos != 0
4857
	      || (hdr->sh_type == SHT_NOBITS
5308
	      || (hdr->sh_type == SHT_NOBITS
4858
		  && hdr->contents == NULL)))
5309
		  && hdr->contents == NULL)))
4859
	BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
5310
	BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
4860
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
5311
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4861
	{
5312
	{
4862
	  if (hdr->sh_size != 0)
5313
	  if (hdr->sh_size != 0)
4863
	    (*_bfd_error_handler)
5314
	    (*_bfd_error_handler)
4864
	      (_("%B: warning: allocated section `%s' not in segment"),
5315
	      (_("%B: warning: allocated section `%s' not in segment"),
4865
	       abfd,
5316
	       abfd,
4866
	       (hdr->bfd_section == NULL
5317
	       (hdr->bfd_section == NULL
4867
		? "*unknown*"
5318
		? "*unknown*"
4868
		: hdr->bfd_section->name));
5319
		: hdr->bfd_section->name));
4869
	  /* We don't need to page align empty sections.  */
5320
	  /* We don't need to page align empty sections.  */
4870
	  if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
5321
	  if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
4871
	    off += vma_page_aligned_bias (hdr->sh_addr, off,
5322
	    off += vma_page_aligned_bias (hdr->sh_addr, off,
4872
					  bed->maxpagesize);
5323
					  bed->maxpagesize);
4873
	  else
5324
	  else
4874
	    off += vma_page_aligned_bias (hdr->sh_addr, off,
5325
	    off += vma_page_aligned_bias (hdr->sh_addr, off,
4875
					  hdr->sh_addralign);
5326
					  hdr->sh_addralign);
4876
	  off = _bfd_elf_assign_file_position_for_section (hdr, off,
5327
	  off = _bfd_elf_assign_file_position_for_section (hdr, off,
4877
							   FALSE);
5328
							   FALSE);
4878
	}
5329
	}
4879
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5330
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
4880
		&& hdr->bfd_section == NULL)
5331
		&& hdr->bfd_section == NULL)
-
 
5332
	       || (hdr->bfd_section != NULL
-
 
5333
		   && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
-
 
5334
		   /* Compress DWARF debug sections.  */
4881
	       || hdr == i_shdrpp[elf_onesymtab (abfd)]
5335
	       || hdr == i_shdrpp[elf_onesymtab (abfd)]
-
 
5336
	       || (elf_symtab_shndx_list (abfd) != NULL
-
 
5337
		   && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
4882
	       || hdr == i_shdrpp[elf_symtab_shndx (abfd)]
5338
	       || hdr == i_shdrpp[elf_strtab_sec (abfd)]
4883
	       || hdr == i_shdrpp[elf_strtab_sec (abfd)])
5339
	       || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
4884
	hdr->sh_offset = -1;
5340
	hdr->sh_offset = -1;
4885
      else
5341
      else
4886
	off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5342
	off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4887
    }
5343
    }
4888
 
5344
 
4889
  /* Now that we have set the section file positions, we can set up
5345
  /* Now that we have set the section file positions, we can set up
4890
     the file positions for the non PT_LOAD segments.  */
5346
     the file positions for the non PT_LOAD segments.  */
4891
  count = 0;
5347
  count = 0;
4892
  filehdr_vaddr = 0;
5348
  filehdr_vaddr = 0;
4893
  filehdr_paddr = 0;
5349
  filehdr_paddr = 0;
4894
  phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
5350
  phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
4895
  phdrs_paddr = 0;
5351
  phdrs_paddr = 0;
4896
  hdrs_segment = NULL;
5352
  hdrs_segment = NULL;
4897
  phdrs = elf_tdata (abfd)->phdr;
5353
  phdrs = elf_tdata (abfd)->phdr;
4898
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5354
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
4899
    {
5355
    {
4900
      ++count;
5356
      ++count;
4901
      if (p->p_type != PT_LOAD)
5357
      if (p->p_type != PT_LOAD)
4902
	continue;
5358
	continue;
4903
 
5359
 
4904
      if (m->includes_filehdr)
5360
      if (m->includes_filehdr)
4905
	{
5361
	{
4906
	  filehdr_vaddr = p->p_vaddr;
5362
	  filehdr_vaddr = p->p_vaddr;
4907
	  filehdr_paddr = p->p_paddr;
5363
	  filehdr_paddr = p->p_paddr;
4908
	}
5364
	}
4909
      if (m->includes_phdrs)
5365
      if (m->includes_phdrs)
4910
	{
5366
	{
4911
	  phdrs_vaddr = p->p_vaddr;
5367
	  phdrs_vaddr = p->p_vaddr;
4912
	  phdrs_paddr = p->p_paddr;
5368
	  phdrs_paddr = p->p_paddr;
4913
	  if (m->includes_filehdr)
5369
	  if (m->includes_filehdr)
4914
	    {
5370
	    {
4915
	      hdrs_segment = m;
5371
	      hdrs_segment = m;
4916
	      phdrs_vaddr += bed->s->sizeof_ehdr;
5372
	      phdrs_vaddr += bed->s->sizeof_ehdr;
4917
	      phdrs_paddr += bed->s->sizeof_ehdr;
5373
	      phdrs_paddr += bed->s->sizeof_ehdr;
4918
	    }
5374
	    }
4919
	}
5375
	}
4920
    }
5376
    }
4921
 
5377
 
4922
  if (hdrs_segment != NULL && link_info != NULL)
5378
  if (hdrs_segment != NULL && link_info != NULL)
4923
    {
5379
    {
4924
      /* There is a segment that contains both the file headers and the
5380
      /* There is a segment that contains both the file headers and the
4925
	 program headers, so provide a symbol __ehdr_start pointing there.
5381
	 program headers, so provide a symbol __ehdr_start pointing there.
4926
	 A program can use this to examine itself robustly.  */
5382
	 A program can use this to examine itself robustly.  */
4927
 
5383
 
4928
      struct elf_link_hash_entry *hash
5384
      struct elf_link_hash_entry *hash
4929
	= elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
5385
	= elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start",
4930
				FALSE, FALSE, TRUE);
5386
				FALSE, FALSE, TRUE);
4931
      /* If the symbol was referenced and not defined, define it.  */
5387
      /* If the symbol was referenced and not defined, define it.  */
4932
      if (hash != NULL
5388
      if (hash != NULL
4933
	  && (hash->root.type == bfd_link_hash_new
5389
	  && (hash->root.type == bfd_link_hash_new
4934
	      || hash->root.type == bfd_link_hash_undefined
5390
	      || hash->root.type == bfd_link_hash_undefined
4935
	      || hash->root.type == bfd_link_hash_undefweak
5391
	      || hash->root.type == bfd_link_hash_undefweak
4936
	      || hash->root.type == bfd_link_hash_common))
5392
	      || hash->root.type == bfd_link_hash_common))
4937
	{
5393
	{
4938
	  asection *s = NULL;
5394
	  asection *s = NULL;
4939
	  if (hdrs_segment->count != 0)
5395
	  if (hdrs_segment->count != 0)
4940
	    /* The segment contains sections, so use the first one.  */
5396
	    /* The segment contains sections, so use the first one.  */
4941
	    s = hdrs_segment->sections[0];
5397
	    s = hdrs_segment->sections[0];
4942
	  else
5398
	  else
4943
	    /* Use the first (i.e. lowest-addressed) section in any segment.  */
5399
	    /* Use the first (i.e. lowest-addressed) section in any segment.  */
4944
	    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5400
	    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
4945
	      if (m->count != 0)
5401
	      if (m->count != 0)
4946
		{
5402
		{
4947
		  s = m->sections[0];
5403
		  s = m->sections[0];
4948
		  break;
5404
		  break;
4949
		}
5405
		}
4950
 
5406
 
4951
	  if (s != NULL)
5407
	  if (s != NULL)
4952
	    {
5408
	    {
4953
	      hash->root.u.def.value = filehdr_vaddr - s->vma;
5409
	      hash->root.u.def.value = filehdr_vaddr - s->vma;
4954
	      hash->root.u.def.section = s;
5410
	      hash->root.u.def.section = s;
4955
	    }
5411
	    }
4956
	  else
5412
	  else
4957
	    {
5413
	    {
4958
	      hash->root.u.def.value = filehdr_vaddr;
5414
	      hash->root.u.def.value = filehdr_vaddr;
4959
	      hash->root.u.def.section = bfd_abs_section_ptr;
5415
	      hash->root.u.def.section = bfd_abs_section_ptr;
4960
	    }
5416
	    }
4961
 
5417
 
4962
	  hash->root.type = bfd_link_hash_defined;
5418
	  hash->root.type = bfd_link_hash_defined;
4963
	  hash->def_regular = 1;
5419
	  hash->def_regular = 1;
4964
	  hash->non_elf = 0;
5420
	  hash->non_elf = 0;
4965
	}
5421
	}
4966
    }
5422
    }
4967
 
5423
 
4968
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
5424
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
4969
    {
5425
    {
4970
      if (p->p_type == PT_GNU_RELRO)
5426
      if (p->p_type == PT_GNU_RELRO)
4971
	{
5427
	{
4972
	  const Elf_Internal_Phdr *lp;
5428
	  const Elf_Internal_Phdr *lp;
4973
	  struct elf_segment_map *lm;
5429
	  struct elf_segment_map *lm;
4974
 
5430
 
4975
	  if (link_info != NULL)
5431
	  if (link_info != NULL)
4976
	    {
5432
	    {
4977
	      /* During linking the range of the RELRO segment is passed
5433
	      /* During linking the range of the RELRO segment is passed
4978
		 in link_info.  */
5434
		 in link_info.  */
4979
	      for (lm = elf_seg_map (abfd), lp = phdrs;
5435
	      for (lm = elf_seg_map (abfd), lp = phdrs;
4980
		   lm != NULL;
5436
		   lm != NULL;
4981
		   lm = lm->next, lp++)
5437
		   lm = lm->next, lp++)
4982
		{
5438
		{
4983
		  if (lp->p_type == PT_LOAD
5439
		  if (lp->p_type == PT_LOAD
4984
		      && lp->p_vaddr < link_info->relro_end
5440
		      && lp->p_vaddr < link_info->relro_end
4985
		      && lp->p_vaddr + lp->p_filesz >= link_info->relro_end
-
 
4986
		      && lm->count != 0
5441
		      && lm->count != 0
4987
		      && lm->sections[0]->vma >= link_info->relro_start)
5442
		      && lm->sections[0]->vma >= link_info->relro_start)
4988
		    break;
5443
		    break;
4989
		}
5444
		}
4990
 
-
 
4991
	      /* PR ld/14207.  If the RELRO segment doesn't fit in the
-
 
4992
		 LOAD segment, it should be removed.  */
5445
 
4993
	      BFD_ASSERT (lm != NULL);
5446
	      BFD_ASSERT (lm != NULL);
4994
	    }
5447
	    }
4995
	  else
5448
	  else
4996
	    {
5449
	    {
4997
	      /* Otherwise we are copying an executable or shared
5450
	      /* Otherwise we are copying an executable or shared
4998
		 library, but we need to use the same linker logic.  */
5451
		 library, but we need to use the same linker logic.  */
4999
	      for (lp = phdrs; lp < phdrs + count; ++lp)
5452
	      for (lp = phdrs; lp < phdrs + count; ++lp)
5000
		{
5453
		{
5001
		  if (lp->p_type == PT_LOAD
5454
		  if (lp->p_type == PT_LOAD
5002
		      && lp->p_paddr == p->p_paddr)
5455
		      && lp->p_paddr == p->p_paddr)
5003
		    break;
5456
		    break;
5004
		}
5457
		}
5005
	    }
5458
	    }
5006
 
5459
 
5007
	  if (lp < phdrs + count)
5460
	  if (lp < phdrs + count)
5008
	    {
5461
	    {
5009
	      p->p_vaddr = lp->p_vaddr;
5462
	      p->p_vaddr = lp->p_vaddr;
5010
	      p->p_paddr = lp->p_paddr;
5463
	      p->p_paddr = lp->p_paddr;
5011
	      p->p_offset = lp->p_offset;
5464
	      p->p_offset = lp->p_offset;
5012
	      if (link_info != NULL)
5465
	      if (link_info != NULL)
5013
		p->p_filesz = link_info->relro_end - lp->p_vaddr;
5466
		p->p_filesz = link_info->relro_end - lp->p_vaddr;
5014
	      else if (m->p_size_valid)
5467
	      else if (m->p_size_valid)
5015
		p->p_filesz = m->p_size;
5468
		p->p_filesz = m->p_size;
5016
	      else
5469
	      else
5017
		abort ();
5470
		abort ();
5018
	      p->p_memsz = p->p_filesz;
5471
	      p->p_memsz = p->p_filesz;
5019
	      /* Preserve the alignment and flags if they are valid. The
5472
	      /* Preserve the alignment and flags if they are valid. The
5020
	         gold linker generates RW/4 for the PT_GNU_RELRO section.
5473
	         gold linker generates RW/4 for the PT_GNU_RELRO section.
5021
		 It is better for objcopy/strip to honor these attributes
5474
		 It is better for objcopy/strip to honor these attributes
5022
		 otherwise gdb will choke when using separate debug files.
5475
		 otherwise gdb will choke when using separate debug files.
5023
	       */
5476
	       */
5024
	      if (!m->p_align_valid)
5477
	      if (!m->p_align_valid)
5025
		p->p_align = 1;
5478
		p->p_align = 1;
5026
	      if (!m->p_flags_valid)
5479
	      if (!m->p_flags_valid)
5027
		p->p_flags = (lp->p_flags & ~PF_W);
5480
		p->p_flags = PF_R;
5028
	    }
5481
	    }
5029
	  else
5482
	  else
5030
	    {
5483
	    {
5031
	      memset (p, 0, sizeof *p);
5484
	      memset (p, 0, sizeof *p);
5032
	      p->p_type = PT_NULL;
5485
	      p->p_type = PT_NULL;
5033
	    }
5486
	    }
5034
	}
5487
	}
5035
      else if (p->p_type == PT_GNU_STACK)
5488
      else if (p->p_type == PT_GNU_STACK)
5036
	{
5489
	{
5037
	  if (m->p_size_valid)
5490
	  if (m->p_size_valid)
5038
	    p->p_memsz = m->p_size;
5491
	    p->p_memsz = m->p_size;
5039
	}
5492
	}
5040
      else if (m->count != 0)
5493
      else if (m->count != 0)
5041
	{
5494
	{
-
 
5495
	  unsigned int i;
5042
	  if (p->p_type != PT_LOAD
5496
	  if (p->p_type != PT_LOAD
5043
	      && (p->p_type != PT_NOTE
5497
	      && (p->p_type != PT_NOTE
5044
		  || bfd_get_format (abfd) != bfd_core))
5498
		  || bfd_get_format (abfd) != bfd_core))
5045
	    {
5499
	    {
5046
	      BFD_ASSERT (!m->includes_filehdr && !m->includes_phdrs);
5500
	      if (m->includes_filehdr || m->includes_phdrs)
-
 
5501
		{
-
 
5502
		  /* PR 17512: file: 2195325e.  */
-
 
5503
		  (*_bfd_error_handler)
-
 
5504
		    (_("%B: warning: non-load segment includes file header and/or program header"),
-
 
5505
		     abfd);
-
 
5506
		  return FALSE;
-
 
5507
		}
5047
 
5508
 
5048
	      p->p_filesz = 0;
5509
	      p->p_filesz = 0;
5049
	      p->p_offset = m->sections[0]->filepos;
5510
	      p->p_offset = m->sections[0]->filepos;
5050
	      for (i = m->count; i-- != 0;)
5511
	      for (i = m->count; i-- != 0;)
5051
		{
5512
		{
5052
		  asection *sect = m->sections[i];
5513
		  asection *sect = m->sections[i];
5053
		  Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
5514
		  Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
5054
		  if (hdr->sh_type != SHT_NOBITS)
5515
		  if (hdr->sh_type != SHT_NOBITS)
5055
		    {
5516
		    {
5056
		      p->p_filesz = (sect->filepos - m->sections[0]->filepos
5517
		      p->p_filesz = (sect->filepos - m->sections[0]->filepos
5057
				     + hdr->sh_size);
5518
				     + hdr->sh_size);
5058
		      break;
5519
		      break;
5059
		    }
5520
		    }
5060
		}
5521
		}
5061
	    }
5522
	    }
5062
	}
5523
	}
5063
      else if (m->includes_filehdr)
5524
      else if (m->includes_filehdr)
5064
	{
5525
	{
5065
	  p->p_vaddr = filehdr_vaddr;
5526
	  p->p_vaddr = filehdr_vaddr;
5066
	  if (! m->p_paddr_valid)
5527
	  if (! m->p_paddr_valid)
5067
	    p->p_paddr = filehdr_paddr;
5528
	    p->p_paddr = filehdr_paddr;
5068
	}
5529
	}
5069
      else if (m->includes_phdrs)
5530
      else if (m->includes_phdrs)
5070
	{
5531
	{
5071
	  p->p_vaddr = phdrs_vaddr;
5532
	  p->p_vaddr = phdrs_vaddr;
5072
	  if (! m->p_paddr_valid)
5533
	  if (! m->p_paddr_valid)
5073
	    p->p_paddr = phdrs_paddr;
5534
	    p->p_paddr = phdrs_paddr;
5074
	}
5535
	}
5075
    }
5536
    }
5076
 
5537
 
5077
  elf_next_file_pos (abfd) = off;
5538
  elf_next_file_pos (abfd) = off;
5078
 
5539
 
5079
  return TRUE;
5540
  return TRUE;
5080
}
5541
}
-
 
5542
 
-
 
5543
static elf_section_list *
-
 
5544
find_section_in_list (unsigned int i, elf_section_list * list)
-
 
5545
{
-
 
5546
  for (;list != NULL; list = list->next)
-
 
5547
    if (list->ndx == i)
-
 
5548
      break;
-
 
5549
  return list;
-
 
5550
}
5081
 
5551
 
5082
/* Work out the file positions of all the sections.  This is called by
5552
/* Work out the file positions of all the sections.  This is called by
5083
   _bfd_elf_compute_section_file_positions.  All the section sizes and
5553
   _bfd_elf_compute_section_file_positions.  All the section sizes and
5084
   VMAs must be known before this is called.
5554
   VMAs must be known before this is called.
5085
 
5555
 
5086
   Reloc sections come in two flavours: Those processed specially as
5556
   Reloc sections come in two flavours: Those processed specially as
5087
   "side-channel" data attached to a section to which they apply, and
5557
   "side-channel" data attached to a section to which they apply, and
5088
   those that bfd doesn't process as relocations.  The latter sort are
5558
   those that bfd doesn't process as relocations.  The latter sort are
5089
   stored in a normal bfd section by bfd_section_from_shdr.   We don't
5559
   stored in a normal bfd section by bfd_section_from_shdr.   We don't
5090
   consider the former sort here, unless they form part of the loadable
5560
   consider the former sort here, unless they form part of the loadable
5091
   image.  Reloc sections not assigned here will be handled later by
5561
   image.  Reloc sections not assigned here will be handled later by
5092
   assign_file_positions_for_relocs.
5562
   assign_file_positions_for_relocs.
5093
 
5563
 
5094
   We also don't set the positions of the .symtab and .strtab here.  */
5564
   We also don't set the positions of the .symtab and .strtab here.  */
5095
 
5565
 
5096
static bfd_boolean
5566
static bfd_boolean
5097
assign_file_positions_except_relocs (bfd *abfd,
5567
assign_file_positions_except_relocs (bfd *abfd,
5098
				     struct bfd_link_info *link_info)
5568
				     struct bfd_link_info *link_info)
5099
{
5569
{
5100
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
5570
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
5101
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5571
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
5102
  file_ptr off;
-
 
5103
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5572
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5104
 
5573
 
5105
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
5574
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
5106
      && bfd_get_format (abfd) != bfd_core)
5575
      && bfd_get_format (abfd) != bfd_core)
5107
    {
5576
    {
5108
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
5577
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
5109
      unsigned int num_sec = elf_numsections (abfd);
5578
      unsigned int num_sec = elf_numsections (abfd);
5110
      Elf_Internal_Shdr **hdrpp;
5579
      Elf_Internal_Shdr **hdrpp;
5111
      unsigned int i;
5580
      unsigned int i;
-
 
5581
      file_ptr off;
5112
 
5582
 
5113
      /* Start after the ELF header.  */
5583
      /* Start after the ELF header.  */
5114
      off = i_ehdrp->e_ehsize;
5584
      off = i_ehdrp->e_ehsize;
5115
 
5585
 
5116
      /* We are not creating an executable, which means that we are
5586
      /* We are not creating an executable, which means that we are
5117
	 not creating a program header, and that the actual order of
5587
	 not creating a program header, and that the actual order of
5118
	 the sections in the file is unimportant.  */
5588
	 the sections in the file is unimportant.  */
5119
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
5589
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
5120
	{
5590
	{
5121
	  Elf_Internal_Shdr *hdr;
5591
	  Elf_Internal_Shdr *hdr;
5122
 
5592
 
5123
	  hdr = *hdrpp;
5593
	  hdr = *hdrpp;
5124
	  if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5594
	  if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
5125
	       && hdr->bfd_section == NULL)
5595
	       && hdr->bfd_section == NULL)
-
 
5596
	      || (hdr->bfd_section != NULL
-
 
5597
		  && (hdr->bfd_section->flags & SEC_ELF_COMPRESS))
-
 
5598
		  /* Compress DWARF debug sections.  */
5126
	      || i == elf_onesymtab (abfd)
5599
	      || i == elf_onesymtab (abfd)
-
 
5600
	      || (elf_symtab_shndx_list (abfd) != NULL
-
 
5601
		  && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
5127
	      || i == elf_symtab_shndx (abfd)
5602
	      || i == elf_strtab_sec (abfd)
5128
	      || i == elf_strtab_sec (abfd))
5603
	      || i == elf_shstrtab_sec (abfd))
5129
	    {
5604
	    {
5130
	      hdr->sh_offset = -1;
5605
	      hdr->sh_offset = -1;
5131
	    }
5606
	    }
5132
	  else
5607
	  else
5133
	    off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5608
	    off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
5134
	}
5609
	}
-
 
5610
 
-
 
5611
      elf_next_file_pos (abfd) = off;
5135
    }
5612
    }
5136
  else
5613
  else
5137
    {
5614
    {
5138
      unsigned int alloc;
5615
      unsigned int alloc;
5139
 
5616
 
5140
      /* Assign file positions for the loaded sections based on the
5617
      /* Assign file positions for the loaded sections based on the
5141
	 assignment of sections to segments.  */
5618
	 assignment of sections to segments.  */
5142
      if (!assign_file_positions_for_load_sections (abfd, link_info))
5619
      if (!assign_file_positions_for_load_sections (abfd, link_info))
5143
	return FALSE;
5620
	return FALSE;
5144
 
5621
 
5145
      /* And for non-load sections.  */
5622
      /* And for non-load sections.  */
5146
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
5623
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
5147
	return FALSE;
5624
	return FALSE;
5148
 
5625
 
5149
      if (bed->elf_backend_modify_program_headers != NULL)
5626
      if (bed->elf_backend_modify_program_headers != NULL)
5150
	{
5627
	{
5151
	  if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
5628
	  if (!(*bed->elf_backend_modify_program_headers) (abfd, link_info))
5152
	    return FALSE;
5629
	    return FALSE;
5153
	}
5630
	}
-
 
5631
 
-
 
5632
      /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.  */
-
 
5633
      if (link_info != NULL && bfd_link_pie (link_info))
-
 
5634
	{
-
 
5635
	  unsigned int num_segments = elf_elfheader (abfd)->e_phnum;
-
 
5636
	  Elf_Internal_Phdr *segment = elf_tdata (abfd)->phdr;
-
 
5637
	  Elf_Internal_Phdr *end_segment = &segment[num_segments];
-
 
5638
 
-
 
5639
	  /* Find the lowest p_vaddr in PT_LOAD segments.  */
-
 
5640
	  bfd_vma p_vaddr = (bfd_vma) -1;
-
 
5641
	  for (; segment < end_segment; segment++)
-
 
5642
	    if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
-
 
5643
	      p_vaddr = segment->p_vaddr;
-
 
5644
 
-
 
5645
	  /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
-
 
5646
	     segments is non-zero.  */
-
 
5647
	  if (p_vaddr)
-
 
5648
	    i_ehdrp->e_type = ET_EXEC;
-
 
5649
	}
5154
 
5650
 
5155
      /* Write out the program headers.  */
5651
      /* Write out the program headers.  */
5156
      alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5652
      alloc = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5157
      if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
5653
      if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
5158
	  || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
5654
	  || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
5159
	return FALSE;
5655
	return FALSE;
5160
 
-
 
5161
      off = elf_next_file_pos (abfd);
-
 
5162
    }
5656
    }
5163
 
-
 
5164
  /* Place the section headers.  */
-
 
5165
  off = align_file_position (off, 1 << bed->s->log_file_align);
-
 
5166
  i_ehdrp->e_shoff = off;
-
 
5167
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
-
 
5168
 
-
 
5169
  elf_next_file_pos (abfd) = off;
-
 
5170
 
5657
 
5171
  return TRUE;
5658
  return TRUE;
5172
}
5659
}
5173
 
5660
 
5174
static bfd_boolean
5661
static bfd_boolean
5175
prep_headers (bfd *abfd)
5662
prep_headers (bfd *abfd)
5176
{
5663
{
5177
  Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form.  */
5664
  Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form.  */
5178
  struct elf_strtab_hash *shstrtab;
5665
  struct elf_strtab_hash *shstrtab;
5179
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5666
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5180
 
5667
 
5181
  i_ehdrp = elf_elfheader (abfd);
5668
  i_ehdrp = elf_elfheader (abfd);
5182
 
5669
 
5183
  shstrtab = _bfd_elf_strtab_init ();
5670
  shstrtab = _bfd_elf_strtab_init ();
5184
  if (shstrtab == NULL)
5671
  if (shstrtab == NULL)
5185
    return FALSE;
5672
    return FALSE;
5186
 
5673
 
5187
  elf_shstrtab (abfd) = shstrtab;
5674
  elf_shstrtab (abfd) = shstrtab;
5188
 
5675
 
5189
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
5676
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
5190
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
5677
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
5191
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
5678
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
5192
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
5679
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
5193
 
5680
 
5194
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
5681
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
5195
  i_ehdrp->e_ident[EI_DATA] =
5682
  i_ehdrp->e_ident[EI_DATA] =
5196
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
5683
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
5197
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
5684
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
5198
 
5685
 
5199
  if ((abfd->flags & DYNAMIC) != 0)
5686
  if ((abfd->flags & DYNAMIC) != 0)
5200
    i_ehdrp->e_type = ET_DYN;
5687
    i_ehdrp->e_type = ET_DYN;
5201
  else if ((abfd->flags & EXEC_P) != 0)
5688
  else if ((abfd->flags & EXEC_P) != 0)
5202
    i_ehdrp->e_type = ET_EXEC;
5689
    i_ehdrp->e_type = ET_EXEC;
5203
  else if (bfd_get_format (abfd) == bfd_core)
5690
  else if (bfd_get_format (abfd) == bfd_core)
5204
    i_ehdrp->e_type = ET_CORE;
5691
    i_ehdrp->e_type = ET_CORE;
5205
  else
5692
  else
5206
    i_ehdrp->e_type = ET_REL;
5693
    i_ehdrp->e_type = ET_REL;
5207
 
5694
 
5208
  switch (bfd_get_arch (abfd))
5695
  switch (bfd_get_arch (abfd))
5209
    {
5696
    {
5210
    case bfd_arch_unknown:
5697
    case bfd_arch_unknown:
5211
      i_ehdrp->e_machine = EM_NONE;
5698
      i_ehdrp->e_machine = EM_NONE;
5212
      break;
5699
      break;
5213
 
5700
 
5214
      /* There used to be a long list of cases here, each one setting
5701
      /* There used to be a long list of cases here, each one setting
5215
	 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5702
	 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
5216
	 in the corresponding bfd definition.  To avoid duplication,
5703
	 in the corresponding bfd definition.  To avoid duplication,
5217
	 the switch was removed.  Machines that need special handling
5704
	 the switch was removed.  Machines that need special handling
5218
	 can generally do it in elf_backend_final_write_processing(),
5705
	 can generally do it in elf_backend_final_write_processing(),
5219
	 unless they need the information earlier than the final write.
5706
	 unless they need the information earlier than the final write.
5220
	 Such need can generally be supplied by replacing the tests for
5707
	 Such need can generally be supplied by replacing the tests for
5221
	 e_machine with the conditions used to determine it.  */
5708
	 e_machine with the conditions used to determine it.  */
5222
    default:
5709
    default:
5223
      i_ehdrp->e_machine = bed->elf_machine_code;
5710
      i_ehdrp->e_machine = bed->elf_machine_code;
5224
    }
5711
    }
5225
 
5712
 
5226
  i_ehdrp->e_version = bed->s->ev_current;
5713
  i_ehdrp->e_version = bed->s->ev_current;
5227
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
5714
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
5228
 
5715
 
5229
  /* No program header, for now.  */
5716
  /* No program header, for now.  */
5230
  i_ehdrp->e_phoff = 0;
5717
  i_ehdrp->e_phoff = 0;
5231
  i_ehdrp->e_phentsize = 0;
5718
  i_ehdrp->e_phentsize = 0;
5232
  i_ehdrp->e_phnum = 0;
5719
  i_ehdrp->e_phnum = 0;
5233
 
5720
 
5234
  /* Each bfd section is section header entry.  */
5721
  /* Each bfd section is section header entry.  */
5235
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
5722
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
5236
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
5723
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
5237
 
5724
 
5238
  /* If we're building an executable, we'll need a program header table.  */
5725
  /* If we're building an executable, we'll need a program header table.  */
5239
  if (abfd->flags & EXEC_P)
5726
  if (abfd->flags & EXEC_P)
5240
    /* It all happens later.  */
5727
    /* It all happens later.  */
5241
    ;
5728
    ;
5242
  else
5729
  else
5243
    {
5730
    {
5244
      i_ehdrp->e_phentsize = 0;
5731
      i_ehdrp->e_phentsize = 0;
5245
      i_ehdrp->e_phoff = 0;
5732
      i_ehdrp->e_phoff = 0;
5246
    }
5733
    }
5247
 
5734
 
5248
  elf_tdata (abfd)->symtab_hdr.sh_name =
5735
  elf_tdata (abfd)->symtab_hdr.sh_name =
5249
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
5736
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
5250
  elf_tdata (abfd)->strtab_hdr.sh_name =
5737
  elf_tdata (abfd)->strtab_hdr.sh_name =
5251
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
5738
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
5252
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
5739
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
5253
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
5740
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
5254
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5741
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5255
      || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
5742
      || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
5256
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
5743
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
5257
    return FALSE;
5744
    return FALSE;
5258
 
5745
 
5259
  return TRUE;
5746
  return TRUE;
5260
}
5747
}
5261
 
5748
 
5262
/* Assign file positions for all the reloc sections which are not part
5749
/* Assign file positions for all the reloc sections which are not part
5263
   of the loadable file image.  */
5750
   of the loadable file image, and the file position of section headers.  */
5264
 
5751
 
5265
void
5752
static bfd_boolean
5266
_bfd_elf_assign_file_positions_for_relocs (bfd *abfd)
5753
_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
5267
{
5754
{
-
 
5755
  file_ptr off;
5268
  file_ptr off;
5756
  Elf_Internal_Shdr **shdrpp, **end_shdrpp;
5269
  unsigned int i, num_sec;
5757
  Elf_Internal_Shdr *shdrp;
-
 
5758
  Elf_Internal_Ehdr *i_ehdrp;
5270
  Elf_Internal_Shdr **shdrpp;
5759
  const struct elf_backend_data *bed;
5271
 
5760
 
5272
  off = elf_next_file_pos (abfd);
5761
  off = elf_next_file_pos (abfd);
5273
 
5762
 
5274
  num_sec = elf_numsections (abfd);
5763
  shdrpp = elf_elfsections (abfd);
-
 
5764
  end_shdrpp = shdrpp + elf_numsections (abfd);
5275
  for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
5765
  for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
5276
    {
-
 
5277
      Elf_Internal_Shdr *shdrp;
-
 
5278
 
5766
    {
-
 
5767
      shdrp = *shdrpp;
-
 
5768
      if (shdrp->sh_offset == -1)
-
 
5769
	{
5279
      shdrp = *shdrpp;
5770
	  asection *sec = shdrp->bfd_section;
5280
      if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
5771
	  bfd_boolean is_rel = (shdrp->sh_type == SHT_REL
-
 
5772
				|| shdrp->sh_type == SHT_RELA);
-
 
5773
	  if (is_rel
-
 
5774
	      || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS)))
-
 
5775
	    {
-
 
5776
	      if (!is_rel)
-
 
5777
		{
-
 
5778
		  const char *name = sec->name;
-
 
5779
		  struct bfd_elf_section_data *d;
-
 
5780
 
-
 
5781
		  /* Compress DWARF debug sections.  */
-
 
5782
		  if (!bfd_compress_section (abfd, sec,
-
 
5783
					     shdrp->contents))
-
 
5784
		    return FALSE;
-
 
5785
 
-
 
5786
		  if (sec->compress_status == COMPRESS_SECTION_DONE
-
 
5787
		      && (abfd->flags & BFD_COMPRESS_GABI) == 0)
-
 
5788
		    {
-
 
5789
		      /* If section is compressed with zlib-gnu, convert
-
 
5790
			 section name from .debug_* to .zdebug_*.  */
-
 
5791
		      char *new_name
-
 
5792
			= convert_debug_to_zdebug (abfd, name);
-
 
5793
		      if (new_name == NULL)
-
 
5794
			return FALSE;
-
 
5795
		      name = new_name;
-
 
5796
		    }
-
 
5797
		  /* Add setion name to section name section.  */
-
 
5798
		  if (shdrp->sh_name != (unsigned int) -1)
-
 
5799
		    abort ();
-
 
5800
		  shdrp->sh_name
-
 
5801
		    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
-
 
5802
							  name, FALSE);
-
 
5803
		  d = elf_section_data (sec);
-
 
5804
 
-
 
5805
		  /* Add reloc setion name to section name section.  */
-
 
5806
		  if (d->rel.hdr
-
 
5807
		      && !_bfd_elf_set_reloc_sh_name (abfd,
-
 
5808
						      d->rel.hdr,
-
 
5809
						      name, FALSE))
-
 
5810
		    return FALSE;
-
 
5811
		  if (d->rela.hdr
-
 
5812
		      && !_bfd_elf_set_reloc_sh_name (abfd,
-
 
5813
						      d->rela.hdr,
-
 
5814
						      name, TRUE))
-
 
5815
		    return FALSE;
-
 
5816
 
-
 
5817
		  /* Update section size and contents.  */
-
 
5818
		  shdrp->sh_size = sec->size;
-
 
5819
		  shdrp->contents = sec->contents;
-
 
5820
		  shdrp->bfd_section->contents = NULL;
5281
	  && shdrp->sh_offset == -1)
5821
		}
-
 
5822
	      off = _bfd_elf_assign_file_position_for_section (shdrp,
-
 
5823
							       off,
5282
	off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
5824
							       TRUE);
-
 
5825
	    }
-
 
5826
	}
-
 
5827
    }
-
 
5828
 
-
 
5829
  /* Place section name section after DWARF debug sections have been
-
 
5830
     compressed.  */
-
 
5831
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
-
 
5832
  shdrp = &elf_tdata (abfd)->shstrtab_hdr;
-
 
5833
  shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
-
 
5834
  off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
-
 
5835
 
-
 
5836
  /* Place the section headers.  */
-
 
5837
  i_ehdrp = elf_elfheader (abfd);
-
 
5838
  bed = get_elf_backend_data (abfd);
-
 
5839
  off = align_file_position (off, 1 << bed->s->log_file_align);
5283
    }
5840
  i_ehdrp->e_shoff = off;
-
 
5841
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
-
 
5842
  elf_next_file_pos (abfd) = off;
5284
 
5843
 
5285
  elf_next_file_pos (abfd) = off;
5844
  return TRUE;
5286
}
5845
}
5287
 
5846
 
5288
bfd_boolean
5847
bfd_boolean
5289
_bfd_elf_write_object_contents (bfd *abfd)
5848
_bfd_elf_write_object_contents (bfd *abfd)
5290
{
5849
{
5291
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5850
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5292
  Elf_Internal_Shdr **i_shdrp;
5851
  Elf_Internal_Shdr **i_shdrp;
5293
  bfd_boolean failed;
5852
  bfd_boolean failed;
5294
  unsigned int count, num_sec;
5853
  unsigned int count, num_sec;
5295
  struct elf_obj_tdata *t;
5854
  struct elf_obj_tdata *t;
5296
 
5855
 
5297
  if (! abfd->output_has_begun
5856
  if (! abfd->output_has_begun
5298
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
5857
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
5299
    return FALSE;
5858
    return FALSE;
5300
 
5859
 
5301
  i_shdrp = elf_elfsections (abfd);
5860
  i_shdrp = elf_elfsections (abfd);
5302
 
5861
 
5303
  failed = FALSE;
5862
  failed = FALSE;
5304
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5863
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
5305
  if (failed)
5864
  if (failed)
5306
    return FALSE;
5865
    return FALSE;
5307
 
5866
 
-
 
5867
  if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
5308
  _bfd_elf_assign_file_positions_for_relocs (abfd);
5868
    return FALSE;
5309
 
5869
 
5310
  /* After writing the headers, we need to write the sections too...  */
5870
  /* After writing the headers, we need to write the sections too...  */
5311
  num_sec = elf_numsections (abfd);
5871
  num_sec = elf_numsections (abfd);
5312
  for (count = 1; count < num_sec; count++)
5872
  for (count = 1; count < num_sec; count++)
5313
    {
5873
    {
-
 
5874
      i_shdrp[count]->sh_name
-
 
5875
	= _bfd_elf_strtab_offset (elf_shstrtab (abfd),
-
 
5876
				  i_shdrp[count]->sh_name);
5314
      if (bed->elf_backend_section_processing)
5877
      if (bed->elf_backend_section_processing)
5315
	(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5878
	(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
5316
      if (i_shdrp[count]->contents)
5879
      if (i_shdrp[count]->contents)
5317
	{
5880
	{
5318
	  bfd_size_type amt = i_shdrp[count]->sh_size;
5881
	  bfd_size_type amt = i_shdrp[count]->sh_size;
5319
 
5882
 
5320
	  if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5883
	  if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
5321
	      || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5884
	      || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
5322
	    return FALSE;
5885
	    return FALSE;
5323
	}
5886
	}
5324
    }
5887
    }
5325
 
5888
 
5326
  /* Write out the section header names.  */
5889
  /* Write out the section header names.  */
5327
  t = elf_tdata (abfd);
5890
  t = elf_tdata (abfd);
5328
  if (elf_shstrtab (abfd) != NULL
5891
  if (elf_shstrtab (abfd) != NULL
5329
      && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5892
      && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
5330
	  || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5893
	  || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
5331
    return FALSE;
5894
    return FALSE;
5332
 
5895
 
5333
  if (bed->elf_backend_final_write_processing)
5896
  if (bed->elf_backend_final_write_processing)
5334
    (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
5897
    (*bed->elf_backend_final_write_processing) (abfd, elf_linker (abfd));
5335
 
5898
 
5336
  if (!bed->s->write_shdrs_and_ehdr (abfd))
5899
  if (!bed->s->write_shdrs_and_ehdr (abfd))
5337
    return FALSE;
5900
    return FALSE;
5338
 
5901
 
5339
  /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
5902
  /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
5340
  if (t->o->build_id.after_write_object_contents != NULL)
5903
  if (t->o->build_id.after_write_object_contents != NULL)
5341
    return (*t->o->build_id.after_write_object_contents) (abfd);
5904
    return (*t->o->build_id.after_write_object_contents) (abfd);
5342
 
5905
 
5343
  return TRUE;
5906
  return TRUE;
5344
}
5907
}
5345
 
5908
 
5346
bfd_boolean
5909
bfd_boolean
5347
_bfd_elf_write_corefile_contents (bfd *abfd)
5910
_bfd_elf_write_corefile_contents (bfd *abfd)
5348
{
5911
{
5349
  /* Hopefully this can be done just like an object file.  */
5912
  /* Hopefully this can be done just like an object file.  */
5350
  return _bfd_elf_write_object_contents (abfd);
5913
  return _bfd_elf_write_object_contents (abfd);
5351
}
5914
}
5352
 
5915
 
5353
/* Given a section, search the header to find them.  */
5916
/* Given a section, search the header to find them.  */
5354
 
5917
 
5355
unsigned int
5918
unsigned int
5356
_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5919
_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
5357
{
5920
{
5358
  const struct elf_backend_data *bed;
5921
  const struct elf_backend_data *bed;
5359
  unsigned int sec_index;
5922
  unsigned int sec_index;
5360
 
5923
 
5361
  if (elf_section_data (asect) != NULL
5924
  if (elf_section_data (asect) != NULL
5362
      && elf_section_data (asect)->this_idx != 0)
5925
      && elf_section_data (asect)->this_idx != 0)
5363
    return elf_section_data (asect)->this_idx;
5926
    return elf_section_data (asect)->this_idx;
5364
 
5927
 
5365
  if (bfd_is_abs_section (asect))
5928
  if (bfd_is_abs_section (asect))
5366
    sec_index = SHN_ABS;
5929
    sec_index = SHN_ABS;
5367
  else if (bfd_is_com_section (asect))
5930
  else if (bfd_is_com_section (asect))
5368
    sec_index = SHN_COMMON;
5931
    sec_index = SHN_COMMON;
5369
  else if (bfd_is_und_section (asect))
5932
  else if (bfd_is_und_section (asect))
5370
    sec_index = SHN_UNDEF;
5933
    sec_index = SHN_UNDEF;
5371
  else
5934
  else
5372
    sec_index = SHN_BAD;
5935
    sec_index = SHN_BAD;
5373
 
5936
 
5374
  bed = get_elf_backend_data (abfd);
5937
  bed = get_elf_backend_data (abfd);
5375
  if (bed->elf_backend_section_from_bfd_section)
5938
  if (bed->elf_backend_section_from_bfd_section)
5376
    {
5939
    {
5377
      int retval = sec_index;
5940
      int retval = sec_index;
5378
 
5941
 
5379
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5942
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
5380
	return retval;
5943
	return retval;
5381
    }
5944
    }
5382
 
5945
 
5383
  if (sec_index == SHN_BAD)
5946
  if (sec_index == SHN_BAD)
5384
    bfd_set_error (bfd_error_nonrepresentable_section);
5947
    bfd_set_error (bfd_error_nonrepresentable_section);
5385
 
5948
 
5386
  return sec_index;
5949
  return sec_index;
5387
}
5950
}
5388
 
5951
 
5389
/* Given a BFD symbol, return the index in the ELF symbol table, or -1
5952
/* Given a BFD symbol, return the index in the ELF symbol table, or -1
5390
   on error.  */
5953
   on error.  */
5391
 
5954
 
5392
int
5955
int
5393
_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5956
_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
5394
{
5957
{
5395
  asymbol *asym_ptr = *asym_ptr_ptr;
5958
  asymbol *asym_ptr = *asym_ptr_ptr;
5396
  int idx;
5959
  int idx;
5397
  flagword flags = asym_ptr->flags;
5960
  flagword flags = asym_ptr->flags;
5398
 
5961
 
5399
  /* When gas creates relocations against local labels, it creates its
5962
  /* When gas creates relocations against local labels, it creates its
5400
     own symbol for the section, but does put the symbol into the
5963
     own symbol for the section, but does put the symbol into the
5401
     symbol chain, so udata is 0.  When the linker is generating
5964
     symbol chain, so udata is 0.  When the linker is generating
5402
     relocatable output, this section symbol may be for one of the
5965
     relocatable output, this section symbol may be for one of the
5403
     input sections rather than the output section.  */
5966
     input sections rather than the output section.  */
5404
  if (asym_ptr->udata.i == 0
5967
  if (asym_ptr->udata.i == 0
5405
      && (flags & BSF_SECTION_SYM)
5968
      && (flags & BSF_SECTION_SYM)
5406
      && asym_ptr->section)
5969
      && asym_ptr->section)
5407
    {
5970
    {
5408
      asection *sec;
5971
      asection *sec;
5409
      int indx;
5972
      int indx;
5410
 
5973
 
5411
      sec = asym_ptr->section;
5974
      sec = asym_ptr->section;
5412
      if (sec->owner != abfd && sec->output_section != NULL)
5975
      if (sec->owner != abfd && sec->output_section != NULL)
5413
	sec = sec->output_section;
5976
	sec = sec->output_section;
5414
      if (sec->owner == abfd
5977
      if (sec->owner == abfd
5415
	  && (indx = sec->index) < elf_num_section_syms (abfd)
5978
	  && (indx = sec->index) < elf_num_section_syms (abfd)
5416
	  && elf_section_syms (abfd)[indx] != NULL)
5979
	  && elf_section_syms (abfd)[indx] != NULL)
5417
	asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5980
	asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
5418
    }
5981
    }
5419
 
5982
 
5420
  idx = asym_ptr->udata.i;
5983
  idx = asym_ptr->udata.i;
5421
 
5984
 
5422
  if (idx == 0)
5985
  if (idx == 0)
5423
    {
5986
    {
5424
      /* This case can occur when using --strip-symbol on a symbol
5987
      /* This case can occur when using --strip-symbol on a symbol
5425
	 which is used in a relocation entry.  */
5988
	 which is used in a relocation entry.  */
5426
      (*_bfd_error_handler)
5989
      (*_bfd_error_handler)
5427
	(_("%B: symbol `%s' required but not present"),
5990
	(_("%B: symbol `%s' required but not present"),
5428
	 abfd, bfd_asymbol_name (asym_ptr));
5991
	 abfd, bfd_asymbol_name (asym_ptr));
5429
      bfd_set_error (bfd_error_no_symbols);
5992
      bfd_set_error (bfd_error_no_symbols);
5430
      return -1;
5993
      return -1;
5431
    }
5994
    }
5432
 
5995
 
5433
#if DEBUG & 4
5996
#if DEBUG & 4
5434
  {
5997
  {
5435
    fprintf (stderr,
5998
    fprintf (stderr,
5436
	     "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5999
	     "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx\n",
5437
	     (long) asym_ptr, asym_ptr->name, idx, (long) flags);
6000
	     (long) asym_ptr, asym_ptr->name, idx, (long) flags);
5438
    fflush (stderr);
6001
    fflush (stderr);
5439
  }
6002
  }
5440
#endif
6003
#endif
5441
 
6004
 
5442
  return idx;
6005
  return idx;
5443
}
6006
}
5444
 
6007
 
5445
/* Rewrite program header information.  */
6008
/* Rewrite program header information.  */
5446
 
6009
 
5447
static bfd_boolean
6010
static bfd_boolean
5448
rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
6011
rewrite_elf_program_header (bfd *ibfd, bfd *obfd)
5449
{
6012
{
5450
  Elf_Internal_Ehdr *iehdr;
6013
  Elf_Internal_Ehdr *iehdr;
5451
  struct elf_segment_map *map;
6014
  struct elf_segment_map *map;
5452
  struct elf_segment_map *map_first;
6015
  struct elf_segment_map *map_first;
5453
  struct elf_segment_map **pointer_to_map;
6016
  struct elf_segment_map **pointer_to_map;
5454
  Elf_Internal_Phdr *segment;
6017
  Elf_Internal_Phdr *segment;
5455
  asection *section;
6018
  asection *section;
5456
  unsigned int i;
6019
  unsigned int i;
5457
  unsigned int num_segments;
6020
  unsigned int num_segments;
5458
  bfd_boolean phdr_included = FALSE;
6021
  bfd_boolean phdr_included = FALSE;
5459
  bfd_boolean p_paddr_valid;
6022
  bfd_boolean p_paddr_valid;
5460
  bfd_vma maxpagesize;
6023
  bfd_vma maxpagesize;
5461
  struct elf_segment_map *phdr_adjust_seg = NULL;
6024
  struct elf_segment_map *phdr_adjust_seg = NULL;
5462
  unsigned int phdr_adjust_num = 0;
6025
  unsigned int phdr_adjust_num = 0;
5463
  const struct elf_backend_data *bed;
6026
  const struct elf_backend_data *bed;
5464
 
6027
 
5465
  bed = get_elf_backend_data (ibfd);
6028
  bed = get_elf_backend_data (ibfd);
5466
  iehdr = elf_elfheader (ibfd);
6029
  iehdr = elf_elfheader (ibfd);
5467
 
6030
 
5468
  map_first = NULL;
6031
  map_first = NULL;
5469
  pointer_to_map = &map_first;
6032
  pointer_to_map = &map_first;
5470
 
6033
 
5471
  num_segments = elf_elfheader (ibfd)->e_phnum;
6034
  num_segments = elf_elfheader (ibfd)->e_phnum;
5472
  maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
6035
  maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
5473
 
6036
 
5474
  /* Returns the end address of the segment + 1.  */
6037
  /* Returns the end address of the segment + 1.  */
5475
#define SEGMENT_END(segment, start)					\
6038
#define SEGMENT_END(segment, start)					\
5476
  (start + (segment->p_memsz > segment->p_filesz			\
6039
  (start + (segment->p_memsz > segment->p_filesz			\
5477
	    ? segment->p_memsz : segment->p_filesz))
6040
	    ? segment->p_memsz : segment->p_filesz))
5478
 
6041
 
5479
#define SECTION_SIZE(section, segment)					\
6042
#define SECTION_SIZE(section, segment)					\
5480
  (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL))		\
6043
  (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL))		\
5481
    != SEC_THREAD_LOCAL || segment->p_type == PT_TLS)			\
6044
    != SEC_THREAD_LOCAL || segment->p_type == PT_TLS)			\
5482
   ? section->size : 0)
6045
   ? section->size : 0)
5483
 
6046
 
5484
  /* Returns TRUE if the given section is contained within
6047
  /* Returns TRUE if the given section is contained within
5485
     the given segment.  VMA addresses are compared.  */
6048
     the given segment.  VMA addresses are compared.  */
5486
#define IS_CONTAINED_BY_VMA(section, segment)				\
6049
#define IS_CONTAINED_BY_VMA(section, segment)				\
5487
  (section->vma >= segment->p_vaddr					\
6050
  (section->vma >= segment->p_vaddr					\
5488
   && (section->vma + SECTION_SIZE (section, segment)			\
6051
   && (section->vma + SECTION_SIZE (section, segment)			\
5489
       <= (SEGMENT_END (segment, segment->p_vaddr))))
6052
       <= (SEGMENT_END (segment, segment->p_vaddr))))
5490
 
6053
 
5491
  /* Returns TRUE if the given section is contained within
6054
  /* Returns TRUE if the given section is contained within
5492
     the given segment.  LMA addresses are compared.  */
6055
     the given segment.  LMA addresses are compared.  */
5493
#define IS_CONTAINED_BY_LMA(section, segment, base)			\
6056
#define IS_CONTAINED_BY_LMA(section, segment, base)			\
5494
  (section->lma >= base							\
6057
  (section->lma >= base							\
5495
   && (section->lma + SECTION_SIZE (section, segment)			\
6058
   && (section->lma + SECTION_SIZE (section, segment)			\
5496
       <= SEGMENT_END (segment, base)))
6059
       <= SEGMENT_END (segment, base)))
5497
 
6060
 
5498
  /* Handle PT_NOTE segment.  */
6061
  /* Handle PT_NOTE segment.  */
5499
#define IS_NOTE(p, s)							\
6062
#define IS_NOTE(p, s)							\
5500
  (p->p_type == PT_NOTE							\
6063
  (p->p_type == PT_NOTE							\
5501
   && elf_section_type (s) == SHT_NOTE					\
6064
   && elf_section_type (s) == SHT_NOTE					\
5502
   && (bfd_vma) s->filepos >= p->p_offset				\
6065
   && (bfd_vma) s->filepos >= p->p_offset				\
5503
   && ((bfd_vma) s->filepos + s->size					\
6066
   && ((bfd_vma) s->filepos + s->size					\
5504
       <= p->p_offset + p->p_filesz))
6067
       <= p->p_offset + p->p_filesz))
5505
 
6068
 
5506
  /* Special case: corefile "NOTE" section containing regs, prpsinfo
6069
  /* Special case: corefile "NOTE" section containing regs, prpsinfo
5507
     etc.  */
6070
     etc.  */
5508
#define IS_COREFILE_NOTE(p, s)						\
6071
#define IS_COREFILE_NOTE(p, s)						\
5509
  (IS_NOTE (p, s)							\
6072
  (IS_NOTE (p, s)							\
5510
   && bfd_get_format (ibfd) == bfd_core					\
6073
   && bfd_get_format (ibfd) == bfd_core					\
5511
   && s->vma == 0							\
6074
   && s->vma == 0							\
5512
   && s->lma == 0)
6075
   && s->lma == 0)
5513
 
6076
 
5514
  /* The complicated case when p_vaddr is 0 is to handle the Solaris
6077
  /* The complicated case when p_vaddr is 0 is to handle the Solaris
5515
     linker, which generates a PT_INTERP section with p_vaddr and
6078
     linker, which generates a PT_INTERP section with p_vaddr and
5516
     p_memsz set to 0.  */
6079
     p_memsz set to 0.  */
5517
#define IS_SOLARIS_PT_INTERP(p, s)					\
6080
#define IS_SOLARIS_PT_INTERP(p, s)					\
5518
  (p->p_vaddr == 0							\
6081
  (p->p_vaddr == 0							\
5519
   && p->p_paddr == 0							\
6082
   && p->p_paddr == 0							\
5520
   && p->p_memsz == 0							\
6083
   && p->p_memsz == 0							\
5521
   && p->p_filesz > 0							\
6084
   && p->p_filesz > 0							\
5522
   && (s->flags & SEC_HAS_CONTENTS) != 0				\
6085
   && (s->flags & SEC_HAS_CONTENTS) != 0				\
5523
   && s->size > 0							\
6086
   && s->size > 0							\
5524
   && (bfd_vma) s->filepos >= p->p_offset				\
6087
   && (bfd_vma) s->filepos >= p->p_offset				\
5525
   && ((bfd_vma) s->filepos + s->size					\
6088
   && ((bfd_vma) s->filepos + s->size					\
5526
       <= p->p_offset + p->p_filesz))
6089
       <= p->p_offset + p->p_filesz))
5527
 
6090
 
5528
  /* Decide if the given section should be included in the given segment.
6091
  /* Decide if the given section should be included in the given segment.
5529
     A section will be included if:
6092
     A section will be included if:
5530
       1. It is within the address space of the segment -- we use the LMA
6093
       1. It is within the address space of the segment -- we use the LMA
5531
	  if that is set for the segment and the VMA otherwise,
6094
	  if that is set for the segment and the VMA otherwise,
5532
       2. It is an allocated section or a NOTE section in a PT_NOTE
6095
       2. It is an allocated section or a NOTE section in a PT_NOTE
5533
	  segment.
6096
	  segment.
5534
       3. There is an output section associated with it,
6097
       3. There is an output section associated with it,
5535
       4. The section has not already been allocated to a previous segment.
6098
       4. The section has not already been allocated to a previous segment.
5536
       5. PT_GNU_STACK segments do not include any sections.
6099
       5. PT_GNU_STACK segments do not include any sections.
5537
       6. PT_TLS segment includes only SHF_TLS sections.
6100
       6. PT_TLS segment includes only SHF_TLS sections.
5538
       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
6101
       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
5539
       8. PT_DYNAMIC should not contain empty sections at the beginning
6102
       8. PT_DYNAMIC should not contain empty sections at the beginning
5540
	  (with the possible exception of .dynamic).  */
6103
	  (with the possible exception of .dynamic).  */
5541
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed)		\
6104
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed)		\
5542
  ((((segment->p_paddr							\
6105
  ((((segment->p_paddr							\
5543
      ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr)	\
6106
      ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr)	\
5544
      : IS_CONTAINED_BY_VMA (section, segment))				\
6107
      : IS_CONTAINED_BY_VMA (section, segment))				\
5545
     && (section->flags & SEC_ALLOC) != 0)				\
6108
     && (section->flags & SEC_ALLOC) != 0)				\
5546
    || IS_NOTE (segment, section))					\
6109
    || IS_NOTE (segment, section))					\
5547
   && segment->p_type != PT_GNU_STACK					\
6110
   && segment->p_type != PT_GNU_STACK					\
5548
   && (segment->p_type != PT_TLS					\
6111
   && (segment->p_type != PT_TLS					\
5549
       || (section->flags & SEC_THREAD_LOCAL))				\
6112
       || (section->flags & SEC_THREAD_LOCAL))				\
5550
   && (segment->p_type == PT_LOAD					\
6113
   && (segment->p_type == PT_LOAD					\
5551
       || segment->p_type == PT_TLS					\
6114
       || segment->p_type == PT_TLS					\
5552
       || (section->flags & SEC_THREAD_LOCAL) == 0)			\
6115
       || (section->flags & SEC_THREAD_LOCAL) == 0)			\
5553
   && (segment->p_type != PT_DYNAMIC					\
6116
   && (segment->p_type != PT_DYNAMIC					\
5554
       || SECTION_SIZE (section, segment) > 0				\
6117
       || SECTION_SIZE (section, segment) > 0				\
5555
       || (segment->p_paddr						\
6118
       || (segment->p_paddr						\
5556
	   ? segment->p_paddr != section->lma				\
6119
	   ? segment->p_paddr != section->lma				\
5557
	   : segment->p_vaddr != section->vma)				\
6120
	   : segment->p_vaddr != section->vma)				\
5558
       || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic")	\
6121
       || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic")	\
5559
	   == 0))							\
6122
	   == 0))							\
5560
   && !section->segment_mark)
6123
   && !section->segment_mark)
5561
 
6124
 
5562
/* If the output section of a section in the input segment is NULL,
6125
/* If the output section of a section in the input segment is NULL,
5563
   it is removed from the corresponding output segment.   */
6126
   it is removed from the corresponding output segment.   */
5564
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed)		\
6127
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed)		\
5565
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)		\
6128
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed)		\
5566
   && section->output_section != NULL)
6129
   && section->output_section != NULL)
5567
 
6130
 
5568
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
6131
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
5569
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)			\
6132
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)			\
5570
  (seg1->field >= SEGMENT_END (seg2, seg2->field))
6133
  (seg1->field >= SEGMENT_END (seg2, seg2->field))
5571
 
6134
 
5572
  /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
6135
  /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
5573
     their VMA address ranges and their LMA address ranges overlap.
6136
     their VMA address ranges and their LMA address ranges overlap.
5574
     It is possible to have overlapping VMA ranges without overlapping LMA
6137
     It is possible to have overlapping VMA ranges without overlapping LMA
5575
     ranges.  RedBoot images for example can have both .data and .bss mapped
6138
     ranges.  RedBoot images for example can have both .data and .bss mapped
5576
     to the same VMA range, but with the .data section mapped to a different
6139
     to the same VMA range, but with the .data section mapped to a different
5577
     LMA.  */
6140
     LMA.  */
5578
#define SEGMENT_OVERLAPS(seg1, seg2)					\
6141
#define SEGMENT_OVERLAPS(seg1, seg2)					\
5579
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)			\
6142
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)			\
5580
	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))			\
6143
	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))			\
5581
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)			\
6144
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)			\
5582
	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
6145
	|| SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
5583
 
6146
 
5584
  /* Initialise the segment mark field.  */
6147
  /* Initialise the segment mark field.  */
5585
  for (section = ibfd->sections; section != NULL; section = section->next)
6148
  for (section = ibfd->sections; section != NULL; section = section->next)
5586
    section->segment_mark = FALSE;
6149
    section->segment_mark = FALSE;
5587
 
6150
 
5588
  /* The Solaris linker creates program headers in which all the
6151
  /* The Solaris linker creates program headers in which all the
5589
     p_paddr fields are zero.  When we try to objcopy or strip such a
6152
     p_paddr fields are zero.  When we try to objcopy or strip such a
5590
     file, we get confused.  Check for this case, and if we find it
6153
     file, we get confused.  Check for this case, and if we find it
5591
     don't set the p_paddr_valid fields.  */
6154
     don't set the p_paddr_valid fields.  */
5592
  p_paddr_valid = FALSE;
6155
  p_paddr_valid = FALSE;
5593
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
6156
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
5594
       i < num_segments;
6157
       i < num_segments;
5595
       i++, segment++)
6158
       i++, segment++)
5596
    if (segment->p_paddr != 0)
6159
    if (segment->p_paddr != 0)
5597
      {
6160
      {
5598
	p_paddr_valid = TRUE;
6161
	p_paddr_valid = TRUE;
5599
	break;
6162
	break;
5600
      }
6163
      }
5601
 
6164
 
5602
  /* Scan through the segments specified in the program header
6165
  /* Scan through the segments specified in the program header
5603
     of the input BFD.  For this first scan we look for overlaps
6166
     of the input BFD.  For this first scan we look for overlaps
5604
     in the loadable segments.  These can be created by weird
6167
     in the loadable segments.  These can be created by weird
5605
     parameters to objcopy.  Also, fix some solaris weirdness.  */
6168
     parameters to objcopy.  Also, fix some solaris weirdness.  */
5606
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
6169
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
5607
       i < num_segments;
6170
       i < num_segments;
5608
       i++, segment++)
6171
       i++, segment++)
5609
    {
6172
    {
5610
      unsigned int j;
6173
      unsigned int j;
5611
      Elf_Internal_Phdr *segment2;
6174
      Elf_Internal_Phdr *segment2;
5612
 
6175
 
5613
      if (segment->p_type == PT_INTERP)
6176
      if (segment->p_type == PT_INTERP)
5614
	for (section = ibfd->sections; section; section = section->next)
6177
	for (section = ibfd->sections; section; section = section->next)
5615
	  if (IS_SOLARIS_PT_INTERP (segment, section))
6178
	  if (IS_SOLARIS_PT_INTERP (segment, section))
5616
	    {
6179
	    {
5617
	      /* Mininal change so that the normal section to segment
6180
	      /* Mininal change so that the normal section to segment
5618
		 assignment code will work.  */
6181
		 assignment code will work.  */
5619
	      segment->p_vaddr = section->vma;
6182
	      segment->p_vaddr = section->vma;
5620
	      break;
6183
	      break;
5621
	    }
6184
	    }
5622
 
6185
 
5623
      if (segment->p_type != PT_LOAD)
6186
      if (segment->p_type != PT_LOAD)
5624
	{
6187
	{
5625
	  /* Remove PT_GNU_RELRO segment.  */
6188
	  /* Remove PT_GNU_RELRO segment.  */
5626
	  if (segment->p_type == PT_GNU_RELRO)
6189
	  if (segment->p_type == PT_GNU_RELRO)
5627
	    segment->p_type = PT_NULL;
6190
	    segment->p_type = PT_NULL;
5628
	  continue;
6191
	  continue;
5629
	}
6192
	}
5630
 
6193
 
5631
      /* Determine if this segment overlaps any previous segments.  */
6194
      /* Determine if this segment overlaps any previous segments.  */
5632
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
6195
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
5633
	{
6196
	{
5634
	  bfd_signed_vma extra_length;
6197
	  bfd_signed_vma extra_length;
5635
 
6198
 
5636
	  if (segment2->p_type != PT_LOAD
6199
	  if (segment2->p_type != PT_LOAD
5637
	      || !SEGMENT_OVERLAPS (segment, segment2))
6200
	      || !SEGMENT_OVERLAPS (segment, segment2))
5638
	    continue;
6201
	    continue;
5639
 
6202
 
5640
	  /* Merge the two segments together.  */
6203
	  /* Merge the two segments together.  */
5641
	  if (segment2->p_vaddr < segment->p_vaddr)
6204
	  if (segment2->p_vaddr < segment->p_vaddr)
5642
	    {
6205
	    {
5643
	      /* Extend SEGMENT2 to include SEGMENT and then delete
6206
	      /* Extend SEGMENT2 to include SEGMENT and then delete
5644
		 SEGMENT.  */
6207
		 SEGMENT.  */
5645
	      extra_length = (SEGMENT_END (segment, segment->p_vaddr)
6208
	      extra_length = (SEGMENT_END (segment, segment->p_vaddr)
5646
			      - SEGMENT_END (segment2, segment2->p_vaddr));
6209
			      - SEGMENT_END (segment2, segment2->p_vaddr));
5647
 
6210
 
5648
	      if (extra_length > 0)
6211
	      if (extra_length > 0)
5649
		{
6212
		{
5650
		  segment2->p_memsz += extra_length;
6213
		  segment2->p_memsz += extra_length;
5651
		  segment2->p_filesz += extra_length;
6214
		  segment2->p_filesz += extra_length;
5652
		}
6215
		}
5653
 
6216
 
5654
	      segment->p_type = PT_NULL;
6217
	      segment->p_type = PT_NULL;
5655
 
6218
 
5656
	      /* Since we have deleted P we must restart the outer loop.  */
6219
	      /* Since we have deleted P we must restart the outer loop.  */
5657
	      i = 0;
6220
	      i = 0;
5658
	      segment = elf_tdata (ibfd)->phdr;
6221
	      segment = elf_tdata (ibfd)->phdr;
5659
	      break;
6222
	      break;
5660
	    }
6223
	    }
5661
	  else
6224
	  else
5662
	    {
6225
	    {
5663
	      /* Extend SEGMENT to include SEGMENT2 and then delete
6226
	      /* Extend SEGMENT to include SEGMENT2 and then delete
5664
		 SEGMENT2.  */
6227
		 SEGMENT2.  */
5665
	      extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
6228
	      extra_length = (SEGMENT_END (segment2, segment2->p_vaddr)
5666
			      - SEGMENT_END (segment, segment->p_vaddr));
6229
			      - SEGMENT_END (segment, segment->p_vaddr));
5667
 
6230
 
5668
	      if (extra_length > 0)
6231
	      if (extra_length > 0)
5669
		{
6232
		{
5670
		  segment->p_memsz += extra_length;
6233
		  segment->p_memsz += extra_length;
5671
		  segment->p_filesz += extra_length;
6234
		  segment->p_filesz += extra_length;
5672
		}
6235
		}
5673
 
6236
 
5674
	      segment2->p_type = PT_NULL;
6237
	      segment2->p_type = PT_NULL;
5675
	    }
6238
	    }
5676
	}
6239
	}
5677
    }
6240
    }
5678
 
6241
 
5679
  /* The second scan attempts to assign sections to segments.  */
6242
  /* The second scan attempts to assign sections to segments.  */
5680
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
6243
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
5681
       i < num_segments;
6244
       i < num_segments;
5682
       i++, segment++)
6245
       i++, segment++)
5683
    {
6246
    {
5684
      unsigned int section_count;
6247
      unsigned int section_count;
5685
      asection **sections;
6248
      asection **sections;
5686
      asection *output_section;
6249
      asection *output_section;
5687
      unsigned int isec;
6250
      unsigned int isec;
5688
      bfd_vma matching_lma;
6251
      bfd_vma matching_lma;
5689
      bfd_vma suggested_lma;
6252
      bfd_vma suggested_lma;
5690
      unsigned int j;
6253
      unsigned int j;
5691
      bfd_size_type amt;
6254
      bfd_size_type amt;
5692
      asection *first_section;
6255
      asection *first_section;
5693
      bfd_boolean first_matching_lma;
6256
      bfd_boolean first_matching_lma;
5694
      bfd_boolean first_suggested_lma;
6257
      bfd_boolean first_suggested_lma;
5695
 
6258
 
5696
      if (segment->p_type == PT_NULL)
6259
      if (segment->p_type == PT_NULL)
5697
	continue;
6260
	continue;
5698
 
6261
 
5699
      first_section = NULL;
6262
      first_section = NULL;
5700
      /* Compute how many sections might be placed into this segment.  */
6263
      /* Compute how many sections might be placed into this segment.  */
5701
      for (section = ibfd->sections, section_count = 0;
6264
      for (section = ibfd->sections, section_count = 0;
5702
	   section != NULL;
6265
	   section != NULL;
5703
	   section = section->next)
6266
	   section = section->next)
5704
	{
6267
	{
5705
	  /* Find the first section in the input segment, which may be
6268
	  /* Find the first section in the input segment, which may be
5706
	     removed from the corresponding output segment.   */
6269
	     removed from the corresponding output segment.   */
5707
	  if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
6270
	  if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed))
5708
	    {
6271
	    {
5709
	      if (first_section == NULL)
6272
	      if (first_section == NULL)
5710
		first_section = section;
6273
		first_section = section;
5711
	      if (section->output_section != NULL)
6274
	      if (section->output_section != NULL)
5712
		++section_count;
6275
		++section_count;
5713
	    }
6276
	    }
5714
	}
6277
	}
5715
 
6278
 
5716
      /* Allocate a segment map big enough to contain
6279
      /* Allocate a segment map big enough to contain
5717
	 all of the sections we have selected.  */
6280
	 all of the sections we have selected.  */
5718
      amt = sizeof (struct elf_segment_map);
6281
      amt = sizeof (struct elf_segment_map);
5719
      amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6282
      amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5720
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6283
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
5721
      if (map == NULL)
6284
      if (map == NULL)
5722
	return FALSE;
6285
	return FALSE;
5723
 
6286
 
5724
      /* Initialise the fields of the segment map.  Default to
6287
      /* Initialise the fields of the segment map.  Default to
5725
	 using the physical address of the segment in the input BFD.  */
6288
	 using the physical address of the segment in the input BFD.  */
5726
      map->next = NULL;
6289
      map->next = NULL;
5727
      map->p_type = segment->p_type;
6290
      map->p_type = segment->p_type;
5728
      map->p_flags = segment->p_flags;
6291
      map->p_flags = segment->p_flags;
5729
      map->p_flags_valid = 1;
6292
      map->p_flags_valid = 1;
5730
 
6293
 
5731
      /* If the first section in the input segment is removed, there is
6294
      /* If the first section in the input segment is removed, there is
5732
	 no need to preserve segment physical address in the corresponding
6295
	 no need to preserve segment physical address in the corresponding
5733
	 output segment.  */
6296
	 output segment.  */
5734
      if (!first_section || first_section->output_section != NULL)
6297
      if (!first_section || first_section->output_section != NULL)
5735
	{
6298
	{
5736
	  map->p_paddr = segment->p_paddr;
6299
	  map->p_paddr = segment->p_paddr;
5737
	  map->p_paddr_valid = p_paddr_valid;
6300
	  map->p_paddr_valid = p_paddr_valid;
5738
	}
6301
	}
5739
 
6302
 
5740
      /* Determine if this segment contains the ELF file header
6303
      /* Determine if this segment contains the ELF file header
5741
	 and if it contains the program headers themselves.  */
6304
	 and if it contains the program headers themselves.  */
5742
      map->includes_filehdr = (segment->p_offset == 0
6305
      map->includes_filehdr = (segment->p_offset == 0
5743
			       && segment->p_filesz >= iehdr->e_ehsize);
6306
			       && segment->p_filesz >= iehdr->e_ehsize);
5744
      map->includes_phdrs = 0;
6307
      map->includes_phdrs = 0;
5745
 
6308
 
5746
      if (!phdr_included || segment->p_type != PT_LOAD)
6309
      if (!phdr_included || segment->p_type != PT_LOAD)
5747
	{
6310
	{
5748
	  map->includes_phdrs =
6311
	  map->includes_phdrs =
5749
	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6312
	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
5750
	     && (segment->p_offset + segment->p_filesz
6313
	     && (segment->p_offset + segment->p_filesz
5751
		 >= ((bfd_vma) iehdr->e_phoff
6314
		 >= ((bfd_vma) iehdr->e_phoff
5752
		     + iehdr->e_phnum * iehdr->e_phentsize)));
6315
		     + iehdr->e_phnum * iehdr->e_phentsize)));
5753
 
6316
 
5754
	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
6317
	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
5755
	    phdr_included = TRUE;
6318
	    phdr_included = TRUE;
5756
	}
6319
	}
5757
 
6320
 
5758
      if (section_count == 0)
6321
      if (section_count == 0)
5759
	{
6322
	{
5760
	  /* Special segments, such as the PT_PHDR segment, may contain
6323
	  /* Special segments, such as the PT_PHDR segment, may contain
5761
	     no sections, but ordinary, loadable segments should contain
6324
	     no sections, but ordinary, loadable segments should contain
5762
	     something.  They are allowed by the ELF spec however, so only
6325
	     something.  They are allowed by the ELF spec however, so only
5763
	     a warning is produced.  */
6326
	     a warning is produced.  */
5764
	  if (segment->p_type == PT_LOAD)
6327
	  if (segment->p_type == PT_LOAD)
5765
	    (*_bfd_error_handler) (_("%B: warning: Empty loadable segment"
6328
	    (*_bfd_error_handler) (_("\
5766
				     " detected, is this intentional ?\n"),
6329
%B: warning: Empty loadable segment detected, is this intentional ?"),
5767
				   ibfd);
6330
				   ibfd);
5768
 
6331
 
5769
	  map->count = 0;
6332
	  map->count = 0;
5770
	  *pointer_to_map = map;
6333
	  *pointer_to_map = map;
5771
	  pointer_to_map = &map->next;
6334
	  pointer_to_map = &map->next;
5772
 
6335
 
5773
	  continue;
6336
	  continue;
5774
	}
6337
	}
5775
 
6338
 
5776
      /* Now scan the sections in the input BFD again and attempt
6339
      /* Now scan the sections in the input BFD again and attempt
5777
	 to add their corresponding output sections to the segment map.
6340
	 to add their corresponding output sections to the segment map.
5778
	 The problem here is how to handle an output section which has
6341
	 The problem here is how to handle an output section which has
5779
	 been moved (ie had its LMA changed).  There are four possibilities:
6342
	 been moved (ie had its LMA changed).  There are four possibilities:
5780
 
6343
 
5781
	 1. None of the sections have been moved.
6344
	 1. None of the sections have been moved.
5782
	    In this case we can continue to use the segment LMA from the
6345
	    In this case we can continue to use the segment LMA from the
5783
	    input BFD.
6346
	    input BFD.
5784
 
6347
 
5785
	 2. All of the sections have been moved by the same amount.
6348
	 2. All of the sections have been moved by the same amount.
5786
	    In this case we can change the segment's LMA to match the LMA
6349
	    In this case we can change the segment's LMA to match the LMA
5787
	    of the first section.
6350
	    of the first section.
5788
 
6351
 
5789
	 3. Some of the sections have been moved, others have not.
6352
	 3. Some of the sections have been moved, others have not.
5790
	    In this case those sections which have not been moved can be
6353
	    In this case those sections which have not been moved can be
5791
	    placed in the current segment which will have to have its size,
6354
	    placed in the current segment which will have to have its size,
5792
	    and possibly its LMA changed, and a new segment or segments will
6355
	    and possibly its LMA changed, and a new segment or segments will
5793
	    have to be created to contain the other sections.
6356
	    have to be created to contain the other sections.
5794
 
6357
 
5795
	 4. The sections have been moved, but not by the same amount.
6358
	 4. The sections have been moved, but not by the same amount.
5796
	    In this case we can change the segment's LMA to match the LMA
6359
	    In this case we can change the segment's LMA to match the LMA
5797
	    of the first section and we will have to create a new segment
6360
	    of the first section and we will have to create a new segment
5798
	    or segments to contain the other sections.
6361
	    or segments to contain the other sections.
5799
 
6362
 
5800
	 In order to save time, we allocate an array to hold the section
6363
	 In order to save time, we allocate an array to hold the section
5801
	 pointers that we are interested in.  As these sections get assigned
6364
	 pointers that we are interested in.  As these sections get assigned
5802
	 to a segment, they are removed from this array.  */
6365
	 to a segment, they are removed from this array.  */
5803
 
6366
 
5804
      sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
6367
      sections = (asection **) bfd_malloc2 (section_count, sizeof (asection *));
5805
      if (sections == NULL)
6368
      if (sections == NULL)
5806
	return FALSE;
6369
	return FALSE;
5807
 
6370
 
5808
      /* Step One: Scan for segment vs section LMA conflicts.
6371
      /* Step One: Scan for segment vs section LMA conflicts.
5809
	 Also add the sections to the section array allocated above.
6372
	 Also add the sections to the section array allocated above.
5810
	 Also add the sections to the current segment.  In the common
6373
	 Also add the sections to the current segment.  In the common
5811
	 case, where the sections have not been moved, this means that
6374
	 case, where the sections have not been moved, this means that
5812
	 we have completely filled the segment, and there is nothing
6375
	 we have completely filled the segment, and there is nothing
5813
	 more to do.  */
6376
	 more to do.  */
5814
      isec = 0;
6377
      isec = 0;
5815
      matching_lma = 0;
6378
      matching_lma = 0;
5816
      suggested_lma = 0;
6379
      suggested_lma = 0;
5817
      first_matching_lma = TRUE;
6380
      first_matching_lma = TRUE;
5818
      first_suggested_lma = TRUE;
6381
      first_suggested_lma = TRUE;
5819
 
6382
 
5820
      for (section = ibfd->sections;
6383
      for (section = ibfd->sections;
5821
	   section != NULL;
6384
	   section != NULL;
5822
	   section = section->next)
6385
	   section = section->next)
5823
	if (section == first_section)
6386
	if (section == first_section)
5824
	  break;
6387
	  break;
5825
 
6388
 
5826
      for (j = 0; section != NULL; section = section->next)
6389
      for (j = 0; section != NULL; section = section->next)
5827
	{
6390
	{
5828
	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
6391
	  if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
5829
	    {
6392
	    {
5830
	      output_section = section->output_section;
6393
	      output_section = section->output_section;
5831
 
6394
 
5832
	      sections[j++] = section;
6395
	      sections[j++] = section;
5833
 
6396
 
5834
	      /* The Solaris native linker always sets p_paddr to 0.
6397
	      /* The Solaris native linker always sets p_paddr to 0.
5835
		 We try to catch that case here, and set it to the
6398
		 We try to catch that case here, and set it to the
5836
		 correct value.  Note - some backends require that
6399
		 correct value.  Note - some backends require that
5837
		 p_paddr be left as zero.  */
6400
		 p_paddr be left as zero.  */
5838
	      if (!p_paddr_valid
6401
	      if (!p_paddr_valid
5839
		  && segment->p_vaddr != 0
6402
		  && segment->p_vaddr != 0
5840
		  && !bed->want_p_paddr_set_to_zero
6403
		  && !bed->want_p_paddr_set_to_zero
5841
		  && isec == 0
6404
		  && isec == 0
5842
		  && output_section->lma != 0
6405
		  && output_section->lma != 0
5843
		  && output_section->vma == (segment->p_vaddr
6406
		  && output_section->vma == (segment->p_vaddr
5844
					     + (map->includes_filehdr
6407
					     + (map->includes_filehdr
5845
						? iehdr->e_ehsize
6408
						? iehdr->e_ehsize
5846
						: 0)
6409
						: 0)
5847
					     + (map->includes_phdrs
6410
					     + (map->includes_phdrs
5848
						? (iehdr->e_phnum
6411
						? (iehdr->e_phnum
5849
						   * iehdr->e_phentsize)
6412
						   * iehdr->e_phentsize)
5850
						: 0)))
6413
						: 0)))
5851
		map->p_paddr = segment->p_vaddr;
6414
		map->p_paddr = segment->p_vaddr;
5852
 
6415
 
5853
	      /* Match up the physical address of the segment with the
6416
	      /* Match up the physical address of the segment with the
5854
		 LMA address of the output section.  */
6417
		 LMA address of the output section.  */
5855
	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6418
	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5856
		  || IS_COREFILE_NOTE (segment, section)
6419
		  || IS_COREFILE_NOTE (segment, section)
5857
		  || (bed->want_p_paddr_set_to_zero
6420
		  || (bed->want_p_paddr_set_to_zero
5858
		      && IS_CONTAINED_BY_VMA (output_section, segment)))
6421
		      && IS_CONTAINED_BY_VMA (output_section, segment)))
5859
		{
6422
		{
5860
		  if (first_matching_lma || output_section->lma < matching_lma)
6423
		  if (first_matching_lma || output_section->lma < matching_lma)
5861
		    {
6424
		    {
5862
		      matching_lma = output_section->lma;
6425
		      matching_lma = output_section->lma;
5863
		      first_matching_lma = FALSE;
6426
		      first_matching_lma = FALSE;
5864
		    }
6427
		    }
5865
 
6428
 
5866
		  /* We assume that if the section fits within the segment
6429
		  /* We assume that if the section fits within the segment
5867
		     then it does not overlap any other section within that
6430
		     then it does not overlap any other section within that
5868
		     segment.  */
6431
		     segment.  */
5869
		  map->sections[isec++] = output_section;
6432
		  map->sections[isec++] = output_section;
5870
		}
6433
		}
5871
	      else if (first_suggested_lma)
6434
	      else if (first_suggested_lma)
5872
		{
6435
		{
5873
		  suggested_lma = output_section->lma;
6436
		  suggested_lma = output_section->lma;
5874
		  first_suggested_lma = FALSE;
6437
		  first_suggested_lma = FALSE;
5875
		}
6438
		}
5876
 
6439
 
5877
	      if (j == section_count)
6440
	      if (j == section_count)
5878
		break;
6441
		break;
5879
	    }
6442
	    }
5880
	}
6443
	}
5881
 
6444
 
5882
      BFD_ASSERT (j == section_count);
6445
      BFD_ASSERT (j == section_count);
5883
 
6446
 
5884
      /* Step Two: Adjust the physical address of the current segment,
6447
      /* Step Two: Adjust the physical address of the current segment,
5885
	 if necessary.  */
6448
	 if necessary.  */
5886
      if (isec == section_count)
6449
      if (isec == section_count)
5887
	{
6450
	{
5888
	  /* All of the sections fitted within the segment as currently
6451
	  /* All of the sections fitted within the segment as currently
5889
	     specified.  This is the default case.  Add the segment to
6452
	     specified.  This is the default case.  Add the segment to
5890
	     the list of built segments and carry on to process the next
6453
	     the list of built segments and carry on to process the next
5891
	     program header in the input BFD.  */
6454
	     program header in the input BFD.  */
5892
	  map->count = section_count;
6455
	  map->count = section_count;
5893
	  *pointer_to_map = map;
6456
	  *pointer_to_map = map;
5894
	  pointer_to_map = &map->next;
6457
	  pointer_to_map = &map->next;
5895
 
6458
 
5896
	  if (p_paddr_valid
6459
	  if (p_paddr_valid
5897
	      && !bed->want_p_paddr_set_to_zero
6460
	      && !bed->want_p_paddr_set_to_zero
5898
	      && matching_lma != map->p_paddr
6461
	      && matching_lma != map->p_paddr
5899
	      && !map->includes_filehdr
6462
	      && !map->includes_filehdr
5900
	      && !map->includes_phdrs)
6463
	      && !map->includes_phdrs)
5901
	    /* There is some padding before the first section in the
6464
	    /* There is some padding before the first section in the
5902
	       segment.  So, we must account for that in the output
6465
	       segment.  So, we must account for that in the output
5903
	       segment's vma.  */
6466
	       segment's vma.  */
5904
	    map->p_vaddr_offset = matching_lma - map->p_paddr;
6467
	    map->p_vaddr_offset = matching_lma - map->p_paddr;
5905
 
6468
 
5906
	  free (sections);
6469
	  free (sections);
5907
	  continue;
6470
	  continue;
5908
	}
6471
	}
5909
      else
6472
      else
5910
	{
6473
	{
5911
	  if (!first_matching_lma)
6474
	  if (!first_matching_lma)
5912
	    {
6475
	    {
5913
	      /* At least one section fits inside the current segment.
6476
	      /* At least one section fits inside the current segment.
5914
		 Keep it, but modify its physical address to match the
6477
		 Keep it, but modify its physical address to match the
5915
		 LMA of the first section that fitted.  */
6478
		 LMA of the first section that fitted.  */
5916
	      map->p_paddr = matching_lma;
6479
	      map->p_paddr = matching_lma;
5917
	    }
6480
	    }
5918
	  else
6481
	  else
5919
	    {
6482
	    {
5920
	      /* None of the sections fitted inside the current segment.
6483
	      /* None of the sections fitted inside the current segment.
5921
		 Change the current segment's physical address to match
6484
		 Change the current segment's physical address to match
5922
		 the LMA of the first section.  */
6485
		 the LMA of the first section.  */
5923
	      map->p_paddr = suggested_lma;
6486
	      map->p_paddr = suggested_lma;
5924
	    }
6487
	    }
5925
 
6488
 
5926
	  /* Offset the segment physical address from the lma
6489
	  /* Offset the segment physical address from the lma
5927
	     to allow for space taken up by elf headers.  */
6490
	     to allow for space taken up by elf headers.  */
5928
	  if (map->includes_filehdr)
6491
	  if (map->includes_filehdr)
5929
	    {
6492
	    {
5930
	      if (map->p_paddr >= iehdr->e_ehsize)
6493
	      if (map->p_paddr >= iehdr->e_ehsize)
5931
		map->p_paddr -= iehdr->e_ehsize;
6494
		map->p_paddr -= iehdr->e_ehsize;
5932
	      else
6495
	      else
5933
		{
6496
		{
5934
		  map->includes_filehdr = FALSE;
6497
		  map->includes_filehdr = FALSE;
5935
		  map->includes_phdrs = FALSE;
6498
		  map->includes_phdrs = FALSE;
5936
		}
6499
		}
5937
	    }
6500
	    }
5938
 
6501
 
5939
	  if (map->includes_phdrs)
6502
	  if (map->includes_phdrs)
5940
	    {
6503
	    {
5941
	      if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
6504
	      if (map->p_paddr >= iehdr->e_phnum * iehdr->e_phentsize)
5942
		{
6505
		{
5943
		  map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
6506
		  map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
5944
 
6507
 
5945
		  /* iehdr->e_phnum is just an estimate of the number
6508
		  /* iehdr->e_phnum is just an estimate of the number
5946
		     of program headers that we will need.  Make a note
6509
		     of program headers that we will need.  Make a note
5947
		     here of the number we used and the segment we chose
6510
		     here of the number we used and the segment we chose
5948
		     to hold these headers, so that we can adjust the
6511
		     to hold these headers, so that we can adjust the
5949
		     offset when we know the correct value.  */
6512
		     offset when we know the correct value.  */
5950
		  phdr_adjust_num = iehdr->e_phnum;
6513
		  phdr_adjust_num = iehdr->e_phnum;
5951
		  phdr_adjust_seg = map;
6514
		  phdr_adjust_seg = map;
5952
		}
6515
		}
5953
	      else
6516
	      else
5954
		map->includes_phdrs = FALSE;
6517
		map->includes_phdrs = FALSE;
5955
	    }
6518
	    }
5956
	}
6519
	}
5957
 
6520
 
5958
      /* Step Three: Loop over the sections again, this time assigning
6521
      /* Step Three: Loop over the sections again, this time assigning
5959
	 those that fit to the current segment and removing them from the
6522
	 those that fit to the current segment and removing them from the
5960
	 sections array; but making sure not to leave large gaps.  Once all
6523
	 sections array; but making sure not to leave large gaps.  Once all
5961
	 possible sections have been assigned to the current segment it is
6524
	 possible sections have been assigned to the current segment it is
5962
	 added to the list of built segments and if sections still remain
6525
	 added to the list of built segments and if sections still remain
5963
	 to be assigned, a new segment is constructed before repeating
6526
	 to be assigned, a new segment is constructed before repeating
5964
	 the loop.  */
6527
	 the loop.  */
5965
      isec = 0;
6528
      isec = 0;
5966
      do
6529
      do
5967
	{
6530
	{
5968
	  map->count = 0;
6531
	  map->count = 0;
5969
	  suggested_lma = 0;
6532
	  suggested_lma = 0;
5970
	  first_suggested_lma = TRUE;
6533
	  first_suggested_lma = TRUE;
5971
 
6534
 
5972
	  /* Fill the current segment with sections that fit.  */
6535
	  /* Fill the current segment with sections that fit.  */
5973
	  for (j = 0; j < section_count; j++)
6536
	  for (j = 0; j < section_count; j++)
5974
	    {
6537
	    {
5975
	      section = sections[j];
6538
	      section = sections[j];
5976
 
6539
 
5977
	      if (section == NULL)
6540
	      if (section == NULL)
5978
		continue;
6541
		continue;
5979
 
6542
 
5980
	      output_section = section->output_section;
6543
	      output_section = section->output_section;
5981
 
6544
 
5982
	      BFD_ASSERT (output_section != NULL);
6545
	      BFD_ASSERT (output_section != NULL);
5983
 
6546
 
5984
	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
6547
	      if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
5985
		  || IS_COREFILE_NOTE (segment, section))
6548
		  || IS_COREFILE_NOTE (segment, section))
5986
		{
6549
		{
5987
		  if (map->count == 0)
6550
		  if (map->count == 0)
5988
		    {
6551
		    {
5989
		      /* If the first section in a segment does not start at
6552
		      /* If the first section in a segment does not start at
5990
			 the beginning of the segment, then something is
6553
			 the beginning of the segment, then something is
5991
			 wrong.  */
6554
			 wrong.  */
5992
		      if (output_section->lma
6555
		      if (output_section->lma
5993
			  != (map->p_paddr
6556
			  != (map->p_paddr
5994
			      + (map->includes_filehdr ? iehdr->e_ehsize : 0)
6557
			      + (map->includes_filehdr ? iehdr->e_ehsize : 0)
5995
			      + (map->includes_phdrs
6558
			      + (map->includes_phdrs
5996
				 ? iehdr->e_phnum * iehdr->e_phentsize
6559
				 ? iehdr->e_phnum * iehdr->e_phentsize
5997
				 : 0)))
6560
				 : 0)))
5998
			abort ();
6561
			abort ();
5999
		    }
6562
		    }
6000
		  else
6563
		  else
6001
		    {
6564
		    {
6002
		      asection *prev_sec;
6565
		      asection *prev_sec;
6003
 
6566
 
6004
		      prev_sec = map->sections[map->count - 1];
6567
		      prev_sec = map->sections[map->count - 1];
6005
 
6568
 
6006
		      /* If the gap between the end of the previous section
6569
		      /* If the gap between the end of the previous section
6007
			 and the start of this section is more than
6570
			 and the start of this section is more than
6008
			 maxpagesize then we need to start a new segment.  */
6571
			 maxpagesize then we need to start a new segment.  */
6009
		      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
6572
		      if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
6010
				      maxpagesize)
6573
				      maxpagesize)
6011
			   < BFD_ALIGN (output_section->lma, maxpagesize))
6574
			   < BFD_ALIGN (output_section->lma, maxpagesize))
6012
			  || (prev_sec->lma + prev_sec->size
6575
			  || (prev_sec->lma + prev_sec->size
6013
			      > output_section->lma))
6576
			      > output_section->lma))
6014
			{
6577
			{
6015
			  if (first_suggested_lma)
6578
			  if (first_suggested_lma)
6016
			    {
6579
			    {
6017
			      suggested_lma = output_section->lma;
6580
			      suggested_lma = output_section->lma;
6018
			      first_suggested_lma = FALSE;
6581
			      first_suggested_lma = FALSE;
6019
			    }
6582
			    }
6020
 
6583
 
6021
			  continue;
6584
			  continue;
6022
			}
6585
			}
6023
		    }
6586
		    }
6024
 
6587
 
6025
		  map->sections[map->count++] = output_section;
6588
		  map->sections[map->count++] = output_section;
6026
		  ++isec;
6589
		  ++isec;
6027
		  sections[j] = NULL;
6590
		  sections[j] = NULL;
6028
		  section->segment_mark = TRUE;
6591
		  section->segment_mark = TRUE;
6029
		}
6592
		}
6030
	      else if (first_suggested_lma)
6593
	      else if (first_suggested_lma)
6031
		{
6594
		{
6032
		  suggested_lma = output_section->lma;
6595
		  suggested_lma = output_section->lma;
6033
		  first_suggested_lma = FALSE;
6596
		  first_suggested_lma = FALSE;
6034
		}
6597
		}
6035
	    }
6598
	    }
6036
 
6599
 
6037
	  BFD_ASSERT (map->count > 0);
6600
	  BFD_ASSERT (map->count > 0);
6038
 
6601
 
6039
	  /* Add the current segment to the list of built segments.  */
6602
	  /* Add the current segment to the list of built segments.  */
6040
	  *pointer_to_map = map;
6603
	  *pointer_to_map = map;
6041
	  pointer_to_map = &map->next;
6604
	  pointer_to_map = &map->next;
6042
 
6605
 
6043
	  if (isec < section_count)
6606
	  if (isec < section_count)
6044
	    {
6607
	    {
6045
	      /* We still have not allocated all of the sections to
6608
	      /* We still have not allocated all of the sections to
6046
		 segments.  Create a new segment here, initialise it
6609
		 segments.  Create a new segment here, initialise it
6047
		 and carry on looping.  */
6610
		 and carry on looping.  */
6048
	      amt = sizeof (struct elf_segment_map);
6611
	      amt = sizeof (struct elf_segment_map);
6049
	      amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6612
	      amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6050
	      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6613
	      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6051
	      if (map == NULL)
6614
	      if (map == NULL)
6052
		{
6615
		{
6053
		  free (sections);
6616
		  free (sections);
6054
		  return FALSE;
6617
		  return FALSE;
6055
		}
6618
		}
6056
 
6619
 
6057
	      /* Initialise the fields of the segment map.  Set the physical
6620
	      /* Initialise the fields of the segment map.  Set the physical
6058
		 physical address to the LMA of the first section that has
6621
		 physical address to the LMA of the first section that has
6059
		 not yet been assigned.  */
6622
		 not yet been assigned.  */
6060
	      map->next = NULL;
6623
	      map->next = NULL;
6061
	      map->p_type = segment->p_type;
6624
	      map->p_type = segment->p_type;
6062
	      map->p_flags = segment->p_flags;
6625
	      map->p_flags = segment->p_flags;
6063
	      map->p_flags_valid = 1;
6626
	      map->p_flags_valid = 1;
6064
	      map->p_paddr = suggested_lma;
6627
	      map->p_paddr = suggested_lma;
6065
	      map->p_paddr_valid = p_paddr_valid;
6628
	      map->p_paddr_valid = p_paddr_valid;
6066
	      map->includes_filehdr = 0;
6629
	      map->includes_filehdr = 0;
6067
	      map->includes_phdrs = 0;
6630
	      map->includes_phdrs = 0;
6068
	    }
6631
	    }
6069
	}
6632
	}
6070
      while (isec < section_count);
6633
      while (isec < section_count);
6071
 
6634
 
6072
      free (sections);
6635
      free (sections);
6073
    }
6636
    }
6074
 
6637
 
6075
  elf_seg_map (obfd) = map_first;
6638
  elf_seg_map (obfd) = map_first;
6076
 
6639
 
6077
  /* If we had to estimate the number of program headers that were
6640
  /* If we had to estimate the number of program headers that were
6078
     going to be needed, then check our estimate now and adjust
6641
     going to be needed, then check our estimate now and adjust
6079
     the offset if necessary.  */
6642
     the offset if necessary.  */
6080
  if (phdr_adjust_seg != NULL)
6643
  if (phdr_adjust_seg != NULL)
6081
    {
6644
    {
6082
      unsigned int count;
6645
      unsigned int count;
6083
 
6646
 
6084
      for (count = 0, map = map_first; map != NULL; map = map->next)
6647
      for (count = 0, map = map_first; map != NULL; map = map->next)
6085
	count++;
6648
	count++;
6086
 
6649
 
6087
      if (count > phdr_adjust_num)
6650
      if (count > phdr_adjust_num)
6088
	phdr_adjust_seg->p_paddr
6651
	phdr_adjust_seg->p_paddr
6089
	  -= (count - phdr_adjust_num) * iehdr->e_phentsize;
6652
	  -= (count - phdr_adjust_num) * iehdr->e_phentsize;
6090
    }
6653
    }
6091
 
6654
 
6092
#undef SEGMENT_END
6655
#undef SEGMENT_END
6093
#undef SECTION_SIZE
6656
#undef SECTION_SIZE
6094
#undef IS_CONTAINED_BY_VMA
6657
#undef IS_CONTAINED_BY_VMA
6095
#undef IS_CONTAINED_BY_LMA
6658
#undef IS_CONTAINED_BY_LMA
6096
#undef IS_NOTE
6659
#undef IS_NOTE
6097
#undef IS_COREFILE_NOTE
6660
#undef IS_COREFILE_NOTE
6098
#undef IS_SOLARIS_PT_INTERP
6661
#undef IS_SOLARIS_PT_INTERP
6099
#undef IS_SECTION_IN_INPUT_SEGMENT
6662
#undef IS_SECTION_IN_INPUT_SEGMENT
6100
#undef INCLUDE_SECTION_IN_SEGMENT
6663
#undef INCLUDE_SECTION_IN_SEGMENT
6101
#undef SEGMENT_AFTER_SEGMENT
6664
#undef SEGMENT_AFTER_SEGMENT
6102
#undef SEGMENT_OVERLAPS
6665
#undef SEGMENT_OVERLAPS
6103
  return TRUE;
6666
  return TRUE;
6104
}
6667
}
6105
 
6668
 
6106
/* Copy ELF program header information.  */
6669
/* Copy ELF program header information.  */
6107
 
6670
 
6108
static bfd_boolean
6671
static bfd_boolean
6109
copy_elf_program_header (bfd *ibfd, bfd *obfd)
6672
copy_elf_program_header (bfd *ibfd, bfd *obfd)
6110
{
6673
{
6111
  Elf_Internal_Ehdr *iehdr;
6674
  Elf_Internal_Ehdr *iehdr;
6112
  struct elf_segment_map *map;
6675
  struct elf_segment_map *map;
6113
  struct elf_segment_map *map_first;
6676
  struct elf_segment_map *map_first;
6114
  struct elf_segment_map **pointer_to_map;
6677
  struct elf_segment_map **pointer_to_map;
6115
  Elf_Internal_Phdr *segment;
6678
  Elf_Internal_Phdr *segment;
6116
  unsigned int i;
6679
  unsigned int i;
6117
  unsigned int num_segments;
6680
  unsigned int num_segments;
6118
  bfd_boolean phdr_included = FALSE;
6681
  bfd_boolean phdr_included = FALSE;
6119
  bfd_boolean p_paddr_valid;
6682
  bfd_boolean p_paddr_valid;
6120
 
6683
 
6121
  iehdr = elf_elfheader (ibfd);
6684
  iehdr = elf_elfheader (ibfd);
6122
 
6685
 
6123
  map_first = NULL;
6686
  map_first = NULL;
6124
  pointer_to_map = &map_first;
6687
  pointer_to_map = &map_first;
6125
 
6688
 
6126
  /* If all the segment p_paddr fields are zero, don't set
6689
  /* If all the segment p_paddr fields are zero, don't set
6127
     map->p_paddr_valid.  */
6690
     map->p_paddr_valid.  */
6128
  p_paddr_valid = FALSE;
6691
  p_paddr_valid = FALSE;
6129
  num_segments = elf_elfheader (ibfd)->e_phnum;
6692
  num_segments = elf_elfheader (ibfd)->e_phnum;
6130
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
6693
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
6131
       i < num_segments;
6694
       i < num_segments;
6132
       i++, segment++)
6695
       i++, segment++)
6133
    if (segment->p_paddr != 0)
6696
    if (segment->p_paddr != 0)
6134
      {
6697
      {
6135
	p_paddr_valid = TRUE;
6698
	p_paddr_valid = TRUE;
6136
	break;
6699
	break;
6137
      }
6700
      }
6138
 
6701
 
6139
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
6702
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
6140
       i < num_segments;
6703
       i < num_segments;
6141
       i++, segment++)
6704
       i++, segment++)
6142
    {
6705
    {
6143
      asection *section;
6706
      asection *section;
6144
      unsigned int section_count;
6707
      unsigned int section_count;
6145
      bfd_size_type amt;
6708
      bfd_size_type amt;
6146
      Elf_Internal_Shdr *this_hdr;
6709
      Elf_Internal_Shdr *this_hdr;
6147
      asection *first_section = NULL;
6710
      asection *first_section = NULL;
6148
      asection *lowest_section;
6711
      asection *lowest_section;
6149
 
6712
 
6150
      /* Compute how many sections are in this segment.  */
6713
      /* Compute how many sections are in this segment.  */
6151
      for (section = ibfd->sections, section_count = 0;
6714
      for (section = ibfd->sections, section_count = 0;
6152
	   section != NULL;
6715
	   section != NULL;
6153
	   section = section->next)
6716
	   section = section->next)
6154
	{
6717
	{
6155
	  this_hdr = &(elf_section_data(section)->this_hdr);
6718
	  this_hdr = &(elf_section_data(section)->this_hdr);
6156
	  if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6719
	  if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6157
	    {
6720
	    {
6158
	      if (first_section == NULL)
6721
	      if (first_section == NULL)
6159
		first_section = section;
6722
		first_section = section;
6160
	      section_count++;
6723
	      section_count++;
6161
	    }
6724
	    }
6162
	}
6725
	}
6163
 
6726
 
6164
      /* Allocate a segment map big enough to contain
6727
      /* Allocate a segment map big enough to contain
6165
	 all of the sections we have selected.  */
6728
	 all of the sections we have selected.  */
6166
      amt = sizeof (struct elf_segment_map);
6729
      amt = sizeof (struct elf_segment_map);
6167
      if (section_count != 0)
6730
      if (section_count != 0)
6168
	amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6731
	amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
6169
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6732
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
6170
      if (map == NULL)
6733
      if (map == NULL)
6171
	return FALSE;
6734
	return FALSE;
6172
 
6735
 
6173
      /* Initialize the fields of the output segment map with the
6736
      /* Initialize the fields of the output segment map with the
6174
	 input segment.  */
6737
	 input segment.  */
6175
      map->next = NULL;
6738
      map->next = NULL;
6176
      map->p_type = segment->p_type;
6739
      map->p_type = segment->p_type;
6177
      map->p_flags = segment->p_flags;
6740
      map->p_flags = segment->p_flags;
6178
      map->p_flags_valid = 1;
6741
      map->p_flags_valid = 1;
6179
      map->p_paddr = segment->p_paddr;
6742
      map->p_paddr = segment->p_paddr;
6180
      map->p_paddr_valid = p_paddr_valid;
6743
      map->p_paddr_valid = p_paddr_valid;
6181
      map->p_align = segment->p_align;
6744
      map->p_align = segment->p_align;
6182
      map->p_align_valid = 1;
6745
      map->p_align_valid = 1;
6183
      map->p_vaddr_offset = 0;
6746
      map->p_vaddr_offset = 0;
6184
 
6747
 
6185
      if (map->p_type == PT_GNU_RELRO
6748
      if (map->p_type == PT_GNU_RELRO
6186
	  || map->p_type == PT_GNU_STACK)
6749
	  || map->p_type == PT_GNU_STACK)
6187
	{
6750
	{
6188
	  /* The PT_GNU_RELRO segment may contain the first a few
6751
	  /* The PT_GNU_RELRO segment may contain the first a few
6189
	     bytes in the .got.plt section even if the whole .got.plt
6752
	     bytes in the .got.plt section even if the whole .got.plt
6190
	     section isn't in the PT_GNU_RELRO segment.  We won't
6753
	     section isn't in the PT_GNU_RELRO segment.  We won't
6191
	     change the size of the PT_GNU_RELRO segment.
6754
	     change the size of the PT_GNU_RELRO segment.
6192
	     Similarly, PT_GNU_STACK size is significant on uclinux
6755
	     Similarly, PT_GNU_STACK size is significant on uclinux
6193
	     systems.    */
6756
	     systems.    */
6194
	  map->p_size = segment->p_memsz;
6757
	  map->p_size = segment->p_memsz;
6195
	  map->p_size_valid = 1;
6758
	  map->p_size_valid = 1;
6196
	}
6759
	}
6197
 
6760
 
6198
      /* Determine if this segment contains the ELF file header
6761
      /* Determine if this segment contains the ELF file header
6199
	 and if it contains the program headers themselves.  */
6762
	 and if it contains the program headers themselves.  */
6200
      map->includes_filehdr = (segment->p_offset == 0
6763
      map->includes_filehdr = (segment->p_offset == 0
6201
			       && segment->p_filesz >= iehdr->e_ehsize);
6764
			       && segment->p_filesz >= iehdr->e_ehsize);
6202
 
6765
 
6203
      map->includes_phdrs = 0;
6766
      map->includes_phdrs = 0;
6204
      if (! phdr_included || segment->p_type != PT_LOAD)
6767
      if (! phdr_included || segment->p_type != PT_LOAD)
6205
	{
6768
	{
6206
	  map->includes_phdrs =
6769
	  map->includes_phdrs =
6207
	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6770
	    (segment->p_offset <= (bfd_vma) iehdr->e_phoff
6208
	     && (segment->p_offset + segment->p_filesz
6771
	     && (segment->p_offset + segment->p_filesz
6209
		 >= ((bfd_vma) iehdr->e_phoff
6772
		 >= ((bfd_vma) iehdr->e_phoff
6210
		     + iehdr->e_phnum * iehdr->e_phentsize)));
6773
		     + iehdr->e_phnum * iehdr->e_phentsize)));
6211
 
6774
 
6212
	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
6775
	  if (segment->p_type == PT_LOAD && map->includes_phdrs)
6213
	    phdr_included = TRUE;
6776
	    phdr_included = TRUE;
6214
	}
6777
	}
6215
 
6778
 
6216
      lowest_section = first_section;
6779
      lowest_section = NULL;
6217
      if (section_count != 0)
6780
      if (section_count != 0)
6218
	{
6781
	{
6219
	  unsigned int isec = 0;
6782
	  unsigned int isec = 0;
6220
 
6783
 
6221
	  for (section = first_section;
6784
	  for (section = first_section;
6222
	       section != NULL;
6785
	       section != NULL;
6223
	       section = section->next)
6786
	       section = section->next)
6224
	    {
6787
	    {
6225
	      this_hdr = &(elf_section_data(section)->this_hdr);
6788
	      this_hdr = &(elf_section_data(section)->this_hdr);
6226
	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6789
	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6227
		{
6790
		{
6228
		  map->sections[isec++] = section->output_section;
6791
		  map->sections[isec++] = section->output_section;
6229
		  if ((section->flags & SEC_ALLOC) != 0)
6792
		  if ((section->flags & SEC_ALLOC) != 0)
6230
		    {
6793
		    {
6231
		      bfd_vma seg_off;
6794
		      bfd_vma seg_off;
-
 
6795
 
6232
 
6796
		      if (lowest_section == NULL
6233
		      if (section->lma < lowest_section->lma)
6797
			  || section->lma < lowest_section->lma)
6234
			lowest_section = section;
6798
			lowest_section = section;
6235
 
6799
 
6236
		      /* Section lmas are set up from PT_LOAD header
6800
		      /* Section lmas are set up from PT_LOAD header
6237
			 p_paddr in _bfd_elf_make_section_from_shdr.
6801
			 p_paddr in _bfd_elf_make_section_from_shdr.
6238
			 If this header has a p_paddr that disagrees
6802
			 If this header has a p_paddr that disagrees
6239
			 with the section lma, flag the p_paddr as
6803
			 with the section lma, flag the p_paddr as
6240
			 invalid.  */
6804
			 invalid.  */
6241
		      if ((section->flags & SEC_LOAD) != 0)
6805
		      if ((section->flags & SEC_LOAD) != 0)
6242
			seg_off = this_hdr->sh_offset - segment->p_offset;
6806
			seg_off = this_hdr->sh_offset - segment->p_offset;
6243
		      else
6807
		      else
6244
			seg_off = this_hdr->sh_addr - segment->p_vaddr;
6808
			seg_off = this_hdr->sh_addr - segment->p_vaddr;
6245
		      if (section->lma - segment->p_paddr != seg_off)
6809
		      if (section->lma - segment->p_paddr != seg_off)
6246
			map->p_paddr_valid = FALSE;
6810
			map->p_paddr_valid = FALSE;
6247
		    }
6811
		    }
6248
		  if (isec == section_count)
6812
		  if (isec == section_count)
6249
		    break;
6813
		    break;
6250
		}
6814
		}
6251
	    }
6815
	    }
6252
	}
6816
	}
6253
 
6817
 
6254
      if (map->includes_filehdr && lowest_section != NULL)
6818
      if (map->includes_filehdr && lowest_section != NULL)
6255
	/* We need to keep the space used by the headers fixed.  */
6819
	/* We need to keep the space used by the headers fixed.  */
6256
	map->header_size = lowest_section->vma - segment->p_vaddr;
6820
	map->header_size = lowest_section->vma - segment->p_vaddr;
6257
 
6821
 
6258
      if (!map->includes_phdrs
6822
      if (!map->includes_phdrs
6259
	  && !map->includes_filehdr
6823
	  && !map->includes_filehdr
6260
	  && map->p_paddr_valid)
6824
	  && map->p_paddr_valid)
6261
	/* There is some other padding before the first section.  */
6825
	/* There is some other padding before the first section.  */
6262
	map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
6826
	map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
6263
			       - segment->p_paddr);
6827
			       - segment->p_paddr);
6264
 
6828
 
6265
      map->count = section_count;
6829
      map->count = section_count;
6266
      *pointer_to_map = map;
6830
      *pointer_to_map = map;
6267
      pointer_to_map = &map->next;
6831
      pointer_to_map = &map->next;
6268
    }
6832
    }
6269
 
6833
 
6270
  elf_seg_map (obfd) = map_first;
6834
  elf_seg_map (obfd) = map_first;
6271
  return TRUE;
6835
  return TRUE;
6272
}
6836
}
6273
 
6837
 
6274
/* Copy private BFD data.  This copies or rewrites ELF program header
6838
/* Copy private BFD data.  This copies or rewrites ELF program header
6275
   information.  */
6839
   information.  */
6276
 
6840
 
6277
static bfd_boolean
6841
static bfd_boolean
6278
copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6842
copy_private_bfd_data (bfd *ibfd, bfd *obfd)
6279
{
6843
{
6280
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6844
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6281
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6845
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6282
    return TRUE;
6846
    return TRUE;
6283
 
6847
 
6284
  if (elf_tdata (ibfd)->phdr == NULL)
6848
  if (elf_tdata (ibfd)->phdr == NULL)
6285
    return TRUE;
6849
    return TRUE;
6286
 
6850
 
6287
  if (ibfd->xvec == obfd->xvec)
6851
  if (ibfd->xvec == obfd->xvec)
6288
    {
6852
    {
6289
      /* Check to see if any sections in the input BFD
6853
      /* Check to see if any sections in the input BFD
6290
	 covered by ELF program header have changed.  */
6854
	 covered by ELF program header have changed.  */
6291
      Elf_Internal_Phdr *segment;
6855
      Elf_Internal_Phdr *segment;
6292
      asection *section, *osec;
6856
      asection *section, *osec;
6293
      unsigned int i, num_segments;
6857
      unsigned int i, num_segments;
6294
      Elf_Internal_Shdr *this_hdr;
6858
      Elf_Internal_Shdr *this_hdr;
6295
      const struct elf_backend_data *bed;
6859
      const struct elf_backend_data *bed;
6296
 
6860
 
6297
      bed = get_elf_backend_data (ibfd);
6861
      bed = get_elf_backend_data (ibfd);
6298
 
6862
 
6299
      /* Regenerate the segment map if p_paddr is set to 0.  */
6863
      /* Regenerate the segment map if p_paddr is set to 0.  */
6300
      if (bed->want_p_paddr_set_to_zero)
6864
      if (bed->want_p_paddr_set_to_zero)
6301
	goto rewrite;
6865
	goto rewrite;
6302
 
6866
 
6303
      /* Initialize the segment mark field.  */
6867
      /* Initialize the segment mark field.  */
6304
      for (section = obfd->sections; section != NULL;
6868
      for (section = obfd->sections; section != NULL;
6305
	   section = section->next)
6869
	   section = section->next)
6306
	section->segment_mark = FALSE;
6870
	section->segment_mark = FALSE;
6307
 
6871
 
6308
      num_segments = elf_elfheader (ibfd)->e_phnum;
6872
      num_segments = elf_elfheader (ibfd)->e_phnum;
6309
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
6873
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
6310
	   i < num_segments;
6874
	   i < num_segments;
6311
	   i++, segment++)
6875
	   i++, segment++)
6312
	{
6876
	{
6313
	  /* PR binutils/3535.  The Solaris linker always sets the p_paddr
6877
	  /* PR binutils/3535.  The Solaris linker always sets the p_paddr
6314
	     and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6878
	     and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
6315
	     which severly confuses things, so always regenerate the segment
6879
	     which severly confuses things, so always regenerate the segment
6316
	     map in this case.  */
6880
	     map in this case.  */
6317
	  if (segment->p_paddr == 0
6881
	  if (segment->p_paddr == 0
6318
	      && segment->p_memsz == 0
6882
	      && segment->p_memsz == 0
6319
	      && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
6883
	      && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC))
6320
	    goto rewrite;
6884
	    goto rewrite;
6321
 
6885
 
6322
	  for (section = ibfd->sections;
6886
	  for (section = ibfd->sections;
6323
	       section != NULL; section = section->next)
6887
	       section != NULL; section = section->next)
6324
	    {
6888
	    {
6325
	      /* We mark the output section so that we know it comes
6889
	      /* We mark the output section so that we know it comes
6326
		 from the input BFD.  */
6890
		 from the input BFD.  */
6327
	      osec = section->output_section;
6891
	      osec = section->output_section;
6328
	      if (osec)
6892
	      if (osec)
6329
		osec->segment_mark = TRUE;
6893
		osec->segment_mark = TRUE;
6330
 
6894
 
6331
	      /* Check if this section is covered by the segment.  */
6895
	      /* Check if this section is covered by the segment.  */
6332
	      this_hdr = &(elf_section_data(section)->this_hdr);
6896
	      this_hdr = &(elf_section_data(section)->this_hdr);
6333
	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6897
	      if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
6334
		{
6898
		{
6335
		  /* FIXME: Check if its output section is changed or
6899
		  /* FIXME: Check if its output section is changed or
6336
		     removed.  What else do we need to check?  */
6900
		     removed.  What else do we need to check?  */
6337
		  if (osec == NULL
6901
		  if (osec == NULL
6338
		      || section->flags != osec->flags
6902
		      || section->flags != osec->flags
6339
		      || section->lma != osec->lma
6903
		      || section->lma != osec->lma
6340
		      || section->vma != osec->vma
6904
		      || section->vma != osec->vma
6341
		      || section->size != osec->size
6905
		      || section->size != osec->size
6342
		      || section->rawsize != osec->rawsize
6906
		      || section->rawsize != osec->rawsize
6343
		      || section->alignment_power != osec->alignment_power)
6907
		      || section->alignment_power != osec->alignment_power)
6344
		    goto rewrite;
6908
		    goto rewrite;
6345
		}
6909
		}
6346
	    }
6910
	    }
6347
	}
6911
	}
6348
 
6912
 
6349
      /* Check to see if any output section do not come from the
6913
      /* Check to see if any output section do not come from the
6350
	 input BFD.  */
6914
	 input BFD.  */
6351
      for (section = obfd->sections; section != NULL;
6915
      for (section = obfd->sections; section != NULL;
6352
	   section = section->next)
6916
	   section = section->next)
6353
	{
6917
	{
6354
	  if (section->segment_mark == FALSE)
6918
	  if (section->segment_mark == FALSE)
6355
	    goto rewrite;
6919
	    goto rewrite;
6356
	  else
6920
	  else
6357
	    section->segment_mark = FALSE;
6921
	    section->segment_mark = FALSE;
6358
	}
6922
	}
6359
 
6923
 
6360
      return copy_elf_program_header (ibfd, obfd);
6924
      return copy_elf_program_header (ibfd, obfd);
6361
    }
6925
    }
6362
 
6926
 
6363
rewrite:
6927
rewrite:
6364
  if (ibfd->xvec == obfd->xvec)
6928
  if (ibfd->xvec == obfd->xvec)
6365
    {
6929
    {
6366
      /* When rewriting program header, set the output maxpagesize to
6930
      /* When rewriting program header, set the output maxpagesize to
6367
	 the maximum alignment of input PT_LOAD segments.  */
6931
	 the maximum alignment of input PT_LOAD segments.  */
6368
      Elf_Internal_Phdr *segment;
6932
      Elf_Internal_Phdr *segment;
6369
      unsigned int i;
6933
      unsigned int i;
6370
      unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
6934
      unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
6371
      bfd_vma maxpagesize = 0;
6935
      bfd_vma maxpagesize = 0;
6372
 
6936
 
6373
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
6937
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
6374
	   i < num_segments;
6938
	   i < num_segments;
6375
	   i++, segment++)
6939
	   i++, segment++)
6376
	if (segment->p_type == PT_LOAD
6940
	if (segment->p_type == PT_LOAD
6377
	    && maxpagesize < segment->p_align)
6941
	    && maxpagesize < segment->p_align)
-
 
6942
	  {
-
 
6943
	    /* PR 17512: file: f17299af.  */
-
 
6944
	    if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
-
 
6945
	      (*_bfd_error_handler) (_("\
-
 
6946
%B: warning: segment alignment of 0x%llx is too large"),
-
 
6947
				     ibfd, (long long) segment->p_align);
-
 
6948
	    else
6378
	  maxpagesize = segment->p_align;
6949
	      maxpagesize = segment->p_align;
-
 
6950
	  }
6379
 
6951
 
6380
      if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
6952
      if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize)
6381
	bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
6953
	bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize);
6382
    }
6954
    }
6383
 
6955
 
6384
  return rewrite_elf_program_header (ibfd, obfd);
6956
  return rewrite_elf_program_header (ibfd, obfd);
6385
}
6957
}
6386
 
6958
 
6387
/* Initialize private output section information from input section.  */
6959
/* Initialize private output section information from input section.  */
6388
 
6960
 
6389
bfd_boolean
6961
bfd_boolean
6390
_bfd_elf_init_private_section_data (bfd *ibfd,
6962
_bfd_elf_init_private_section_data (bfd *ibfd,
6391
				    asection *isec,
6963
				    asection *isec,
6392
				    bfd *obfd,
6964
				    bfd *obfd,
6393
				    asection *osec,
6965
				    asection *osec,
6394
				    struct bfd_link_info *link_info)
6966
				    struct bfd_link_info *link_info)
6395
 
6967
 
6396
{
6968
{
6397
  Elf_Internal_Shdr *ihdr, *ohdr;
6969
  Elf_Internal_Shdr *ihdr, *ohdr;
6398
  bfd_boolean final_link = link_info != NULL && !link_info->relocatable;
6970
  bfd_boolean final_link = (link_info != NULL
-
 
6971
			    && !bfd_link_relocatable (link_info));
6399
 
6972
 
6400
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
6973
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
6401
      || obfd->xvec->flavour != bfd_target_elf_flavour)
6974
      || obfd->xvec->flavour != bfd_target_elf_flavour)
6402
    return TRUE;
6975
    return TRUE;
6403
 
6976
 
6404
  BFD_ASSERT (elf_section_data (osec) != NULL);
6977
  BFD_ASSERT (elf_section_data (osec) != NULL);
6405
 
6978
 
6406
  /* For objcopy and relocatable link, don't copy the output ELF
6979
  /* For objcopy and relocatable link, don't copy the output ELF
6407
     section type from input if the output BFD section flags have been
6980
     section type from input if the output BFD section flags have been
6408
     set to something different.  For a final link allow some flags
6981
     set to something different.  For a final link allow some flags
6409
     that the linker clears to differ.  */
6982
     that the linker clears to differ.  */
6410
  if (elf_section_type (osec) == SHT_NULL
6983
  if (elf_section_type (osec) == SHT_NULL
6411
      && (osec->flags == isec->flags
6984
      && (osec->flags == isec->flags
6412
	  || (final_link
6985
	  || (final_link
6413
	      && ((osec->flags ^ isec->flags)
6986
	      && ((osec->flags ^ isec->flags)
6414
		  & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
6987
		  & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
6415
    elf_section_type (osec) = elf_section_type (isec);
6988
    elf_section_type (osec) = elf_section_type (isec);
6416
 
6989
 
6417
  /* FIXME: Is this correct for all OS/PROC specific flags?  */
6990
  /* FIXME: Is this correct for all OS/PROC specific flags?  */
6418
  elf_section_flags (osec) |= (elf_section_flags (isec)
6991
  elf_section_flags (osec) |= (elf_section_flags (isec)
6419
			       & (SHF_MASKOS | SHF_MASKPROC));
6992
			       & (SHF_MASKOS | SHF_MASKPROC));
6420
 
6993
 
6421
  /* Set things up for objcopy and relocatable link.  The output
6994
  /* Set things up for objcopy and relocatable link.  The output
6422
     SHT_GROUP section will have its elf_next_in_group pointing back
6995
     SHT_GROUP section will have its elf_next_in_group pointing back
6423
     to the input group members.  Ignore linker created group section.
6996
     to the input group members.  Ignore linker created group section.
6424
     See elfNN_ia64_object_p in elfxx-ia64.c.  */
6997
     See elfNN_ia64_object_p in elfxx-ia64.c.  */
6425
  if (!final_link)
6998
  if (!final_link)
6426
    {
6999
    {
6427
      if (elf_sec_group (isec) == NULL
7000
      if (elf_sec_group (isec) == NULL
6428
	  || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
7001
	  || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)
6429
	{
7002
	{
6430
	  if (elf_section_flags (isec) & SHF_GROUP)
7003
	  if (elf_section_flags (isec) & SHF_GROUP)
6431
	    elf_section_flags (osec) |= SHF_GROUP;
7004
	    elf_section_flags (osec) |= SHF_GROUP;
6432
	  elf_next_in_group (osec) = elf_next_in_group (isec);
7005
	  elf_next_in_group (osec) = elf_next_in_group (isec);
6433
	  elf_section_data (osec)->group = elf_section_data (isec)->group;
7006
	  elf_section_data (osec)->group = elf_section_data (isec)->group;
6434
	}
7007
	}
-
 
7008
 
-
 
7009
      /* If not decompress, preserve SHF_COMPRESSED.  */
-
 
7010
      if ((ibfd->flags & BFD_DECOMPRESS) == 0)
-
 
7011
	elf_section_flags (osec) |= (elf_section_flags (isec)
-
 
7012
				     & SHF_COMPRESSED);
6435
    }
7013
    }
6436
 
7014
 
6437
  ihdr = &elf_section_data (isec)->this_hdr;
7015
  ihdr = &elf_section_data (isec)->this_hdr;
6438
 
7016
 
6439
  /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
7017
  /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
6440
     don't use the output section of the linked-to section since it
7018
     don't use the output section of the linked-to section since it
6441
     may be NULL at this point.  */
7019
     may be NULL at this point.  */
6442
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
7020
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
6443
    {
7021
    {
6444
      ohdr = &elf_section_data (osec)->this_hdr;
7022
      ohdr = &elf_section_data (osec)->this_hdr;
6445
      ohdr->sh_flags |= SHF_LINK_ORDER;
7023
      ohdr->sh_flags |= SHF_LINK_ORDER;
6446
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
7024
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
6447
    }
7025
    }
6448
 
7026
 
6449
  osec->use_rela_p = isec->use_rela_p;
7027
  osec->use_rela_p = isec->use_rela_p;
6450
 
7028
 
6451
  return TRUE;
7029
  return TRUE;
6452
}
7030
}
6453
 
7031
 
6454
/* Copy private section information.  This copies over the entsize
7032
/* Copy private section information.  This copies over the entsize
6455
   field, and sometimes the info field.  */
7033
   field, and sometimes the info field.  */
6456
 
7034
 
6457
bfd_boolean
7035
bfd_boolean
6458
_bfd_elf_copy_private_section_data (bfd *ibfd,
7036
_bfd_elf_copy_private_section_data (bfd *ibfd,
6459
				    asection *isec,
7037
				    asection *isec,
6460
				    bfd *obfd,
7038
				    bfd *obfd,
6461
				    asection *osec)
7039
				    asection *osec)
6462
{
7040
{
6463
  Elf_Internal_Shdr *ihdr, *ohdr;
7041
  Elf_Internal_Shdr *ihdr, *ohdr;
6464
 
7042
 
6465
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
7043
  if (ibfd->xvec->flavour != bfd_target_elf_flavour
6466
      || obfd->xvec->flavour != bfd_target_elf_flavour)
7044
      || obfd->xvec->flavour != bfd_target_elf_flavour)
6467
    return TRUE;
7045
    return TRUE;
6468
 
7046
 
6469
  ihdr = &elf_section_data (isec)->this_hdr;
7047
  ihdr = &elf_section_data (isec)->this_hdr;
6470
  ohdr = &elf_section_data (osec)->this_hdr;
7048
  ohdr = &elf_section_data (osec)->this_hdr;
6471
 
7049
 
6472
  ohdr->sh_entsize = ihdr->sh_entsize;
7050
  ohdr->sh_entsize = ihdr->sh_entsize;
6473
 
7051
 
6474
  if (ihdr->sh_type == SHT_SYMTAB
7052
  if (ihdr->sh_type == SHT_SYMTAB
6475
      || ihdr->sh_type == SHT_DYNSYM
7053
      || ihdr->sh_type == SHT_DYNSYM
6476
      || ihdr->sh_type == SHT_GNU_verneed
7054
      || ihdr->sh_type == SHT_GNU_verneed
6477
      || ihdr->sh_type == SHT_GNU_verdef)
7055
      || ihdr->sh_type == SHT_GNU_verdef)
6478
    ohdr->sh_info = ihdr->sh_info;
7056
    ohdr->sh_info = ihdr->sh_info;
6479
 
7057
 
6480
  return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
7058
  return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec,
6481
					     NULL);
7059
					     NULL);
6482
}
7060
}
6483
 
7061
 
6484
/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
7062
/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
6485
   necessary if we are removing either the SHT_GROUP section or any of
7063
   necessary if we are removing either the SHT_GROUP section or any of
6486
   the group member sections.  DISCARDED is the value that a section's
7064
   the group member sections.  DISCARDED is the value that a section's
6487
   output_section has if the section will be discarded, NULL when this
7065
   output_section has if the section will be discarded, NULL when this
6488
   function is called from objcopy, bfd_abs_section_ptr when called
7066
   function is called from objcopy, bfd_abs_section_ptr when called
6489
   from the linker.  */
7067
   from the linker.  */
6490
 
7068
 
6491
bfd_boolean
7069
bfd_boolean
6492
_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
7070
_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
6493
{
7071
{
6494
  asection *isec;
7072
  asection *isec;
6495
 
7073
 
6496
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
7074
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
6497
    if (elf_section_type (isec) == SHT_GROUP)
7075
    if (elf_section_type (isec) == SHT_GROUP)
6498
      {
7076
      {
6499
	asection *first = elf_next_in_group (isec);
7077
	asection *first = elf_next_in_group (isec);
6500
	asection *s = first;
7078
	asection *s = first;
6501
	bfd_size_type removed = 0;
7079
	bfd_size_type removed = 0;
6502
 
7080
 
6503
	while (s != NULL)
7081
	while (s != NULL)
6504
	  {
7082
	  {
6505
	    /* If this member section is being output but the
7083
	    /* If this member section is being output but the
6506
	       SHT_GROUP section is not, then clear the group info
7084
	       SHT_GROUP section is not, then clear the group info
6507
	       set up by _bfd_elf_copy_private_section_data.  */
7085
	       set up by _bfd_elf_copy_private_section_data.  */
6508
	    if (s->output_section != discarded
7086
	    if (s->output_section != discarded
6509
		&& isec->output_section == discarded)
7087
		&& isec->output_section == discarded)
6510
	      {
7088
	      {
6511
		elf_section_flags (s->output_section) &= ~SHF_GROUP;
7089
		elf_section_flags (s->output_section) &= ~SHF_GROUP;
6512
		elf_group_name (s->output_section) = NULL;
7090
		elf_group_name (s->output_section) = NULL;
6513
	      }
7091
	      }
6514
	    /* Conversely, if the member section is not being output
7092
	    /* Conversely, if the member section is not being output
6515
	       but the SHT_GROUP section is, then adjust its size.  */
7093
	       but the SHT_GROUP section is, then adjust its size.  */
6516
	    else if (s->output_section == discarded
7094
	    else if (s->output_section == discarded
6517
		     && isec->output_section != discarded)
7095
		     && isec->output_section != discarded)
6518
	      removed += 4;
7096
	      removed += 4;
6519
	    s = elf_next_in_group (s);
7097
	    s = elf_next_in_group (s);
6520
	    if (s == first)
7098
	    if (s == first)
6521
	      break;
7099
	      break;
6522
	  }
7100
	  }
6523
	if (removed != 0)
7101
	if (removed != 0)
6524
	  {
7102
	  {
6525
	    if (discarded != NULL)
7103
	    if (discarded != NULL)
6526
	      {
7104
	      {
6527
		/* If we've been called for ld -r, then we need to
7105
		/* If we've been called for ld -r, then we need to
6528
		   adjust the input section size.  This function may
7106
		   adjust the input section size.  This function may
6529
		   be called multiple times, so save the original
7107
		   be called multiple times, so save the original
6530
		   size.  */
7108
		   size.  */
6531
		if (isec->rawsize == 0)
7109
		if (isec->rawsize == 0)
6532
		  isec->rawsize = isec->size;
7110
		  isec->rawsize = isec->size;
6533
		isec->size = isec->rawsize - removed;
7111
		isec->size = isec->rawsize - removed;
6534
	      }
7112
	      }
6535
	    else
7113
	    else
6536
	      {
7114
	      {
6537
		/* Adjust the output section size when called from
7115
		/* Adjust the output section size when called from
6538
		   objcopy. */
7116
		   objcopy. */
6539
		isec->output_section->size -= removed;
7117
		isec->output_section->size -= removed;
6540
	      }
7118
	      }
6541
	  }
7119
	  }
6542
      }
7120
      }
6543
 
7121
 
6544
  return TRUE;
7122
  return TRUE;
6545
}
7123
}
6546
 
7124
 
6547
/* Copy private header information.  */
7125
/* Copy private header information.  */
6548
 
7126
 
6549
bfd_boolean
7127
bfd_boolean
6550
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
7128
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
6551
{
7129
{
6552
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7130
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6553
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7131
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6554
    return TRUE;
7132
    return TRUE;
6555
 
7133
 
6556
  /* Copy over private BFD data if it has not already been copied.
7134
  /* Copy over private BFD data if it has not already been copied.
6557
     This must be done here, rather than in the copy_private_bfd_data
7135
     This must be done here, rather than in the copy_private_bfd_data
6558
     entry point, because the latter is called after the section
7136
     entry point, because the latter is called after the section
6559
     contents have been set, which means that the program headers have
7137
     contents have been set, which means that the program headers have
6560
     already been worked out.  */
7138
     already been worked out.  */
6561
  if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
7139
  if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
6562
    {
7140
    {
6563
      if (! copy_private_bfd_data (ibfd, obfd))
7141
      if (! copy_private_bfd_data (ibfd, obfd))
6564
	return FALSE;
7142
	return FALSE;
6565
    }
7143
    }
6566
 
7144
 
6567
  return _bfd_elf_fixup_group_sections (ibfd, NULL);
7145
  return _bfd_elf_fixup_group_sections (ibfd, NULL);
6568
}
7146
}
6569
 
7147
 
6570
/* Copy private symbol information.  If this symbol is in a section
7148
/* Copy private symbol information.  If this symbol is in a section
6571
   which we did not map into a BFD section, try to map the section
7149
   which we did not map into a BFD section, try to map the section
6572
   index correctly.  We use special macro definitions for the mapped
7150
   index correctly.  We use special macro definitions for the mapped
6573
   section indices; these definitions are interpreted by the
7151
   section indices; these definitions are interpreted by the
6574
   swap_out_syms function.  */
7152
   swap_out_syms function.  */
6575
 
7153
 
6576
#define MAP_ONESYMTAB (SHN_HIOS + 1)
7154
#define MAP_ONESYMTAB (SHN_HIOS + 1)
6577
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
7155
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
6578
#define MAP_STRTAB    (SHN_HIOS + 3)
7156
#define MAP_STRTAB    (SHN_HIOS + 3)
6579
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
7157
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
6580
#define MAP_SYM_SHNDX (SHN_HIOS + 5)
7158
#define MAP_SYM_SHNDX (SHN_HIOS + 5)
6581
 
7159
 
6582
bfd_boolean
7160
bfd_boolean
6583
_bfd_elf_copy_private_symbol_data (bfd *ibfd,
7161
_bfd_elf_copy_private_symbol_data (bfd *ibfd,
6584
				   asymbol *isymarg,
7162
				   asymbol *isymarg,
6585
				   bfd *obfd,
7163
				   bfd *obfd,
6586
				   asymbol *osymarg)
7164
				   asymbol *osymarg)
6587
{
7165
{
6588
  elf_symbol_type *isym, *osym;
7166
  elf_symbol_type *isym, *osym;
6589
 
7167
 
6590
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
7168
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6591
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
7169
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6592
    return TRUE;
7170
    return TRUE;
6593
 
7171
 
6594
  isym = elf_symbol_from (ibfd, isymarg);
7172
  isym = elf_symbol_from (ibfd, isymarg);
6595
  osym = elf_symbol_from (obfd, osymarg);
7173
  osym = elf_symbol_from (obfd, osymarg);
6596
 
7174
 
6597
  if (isym != NULL
7175
  if (isym != NULL
6598
      && isym->internal_elf_sym.st_shndx != 0
7176
      && isym->internal_elf_sym.st_shndx != 0
6599
      && osym != NULL
7177
      && osym != NULL
6600
      && bfd_is_abs_section (isym->symbol.section))
7178
      && bfd_is_abs_section (isym->symbol.section))
6601
    {
7179
    {
6602
      unsigned int shndx;
7180
      unsigned int shndx;
6603
 
7181
 
6604
      shndx = isym->internal_elf_sym.st_shndx;
7182
      shndx = isym->internal_elf_sym.st_shndx;
6605
      if (shndx == elf_onesymtab (ibfd))
7183
      if (shndx == elf_onesymtab (ibfd))
6606
	shndx = MAP_ONESYMTAB;
7184
	shndx = MAP_ONESYMTAB;
6607
      else if (shndx == elf_dynsymtab (ibfd))
7185
      else if (shndx == elf_dynsymtab (ibfd))
6608
	shndx = MAP_DYNSYMTAB;
7186
	shndx = MAP_DYNSYMTAB;
6609
      else if (shndx == elf_strtab_sec (ibfd))
7187
      else if (shndx == elf_strtab_sec (ibfd))
6610
	shndx = MAP_STRTAB;
7188
	shndx = MAP_STRTAB;
6611
      else if (shndx == elf_shstrtab_sec (ibfd))
7189
      else if (shndx == elf_shstrtab_sec (ibfd))
6612
	shndx = MAP_SHSTRTAB;
7190
	shndx = MAP_SHSTRTAB;
6613
      else if (shndx == elf_symtab_shndx (ibfd))
7191
      else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
6614
	shndx = MAP_SYM_SHNDX;
7192
	shndx = MAP_SYM_SHNDX;
6615
      osym->internal_elf_sym.st_shndx = shndx;
7193
      osym->internal_elf_sym.st_shndx = shndx;
6616
    }
7194
    }
6617
 
7195
 
6618
  return TRUE;
7196
  return TRUE;
6619
}
7197
}
6620
 
7198
 
6621
/* Swap out the symbols.  */
7199
/* Swap out the symbols.  */
6622
 
7200
 
6623
static bfd_boolean
7201
static bfd_boolean
6624
swap_out_syms (bfd *abfd,
7202
swap_out_syms (bfd *abfd,
6625
	       struct bfd_strtab_hash **sttp,
7203
	       struct elf_strtab_hash **sttp,
6626
	       int relocatable_p)
7204
	       int relocatable_p)
6627
{
7205
{
6628
  const struct elf_backend_data *bed;
7206
  const struct elf_backend_data *bed;
6629
  int symcount;
7207
  int symcount;
6630
  asymbol **syms;
7208
  asymbol **syms;
6631
  struct bfd_strtab_hash *stt;
7209
  struct elf_strtab_hash *stt;
6632
  Elf_Internal_Shdr *symtab_hdr;
7210
  Elf_Internal_Shdr *symtab_hdr;
6633
  Elf_Internal_Shdr *symtab_shndx_hdr;
7211
  Elf_Internal_Shdr *symtab_shndx_hdr;
6634
  Elf_Internal_Shdr *symstrtab_hdr;
7212
  Elf_Internal_Shdr *symstrtab_hdr;
-
 
7213
  struct elf_sym_strtab *symstrtab;
6635
  bfd_byte *outbound_syms;
7214
  bfd_byte *outbound_syms;
6636
  bfd_byte *outbound_shndx;
7215
  bfd_byte *outbound_shndx;
-
 
7216
  unsigned long outbound_syms_index;
-
 
7217
  unsigned long outbound_shndx_index;
6637
  int idx;
7218
  int idx;
6638
  unsigned int num_locals;
7219
  unsigned int num_locals;
6639
  bfd_size_type amt;
7220
  bfd_size_type amt;
6640
  bfd_boolean name_local_sections;
7221
  bfd_boolean name_local_sections;
6641
 
7222
 
6642
  if (!elf_map_symbols (abfd, &num_locals))
7223
  if (!elf_map_symbols (abfd, &num_locals))
6643
    return FALSE;
7224
    return FALSE;
6644
 
7225
 
6645
  /* Dump out the symtabs.  */
7226
  /* Dump out the symtabs.  */
6646
  stt = _bfd_elf_stringtab_init ();
7227
  stt = _bfd_elf_strtab_init ();
6647
  if (stt == NULL)
7228
  if (stt == NULL)
6648
    return FALSE;
7229
    return FALSE;
6649
 
7230
 
6650
  bed = get_elf_backend_data (abfd);
7231
  bed = get_elf_backend_data (abfd);
6651
  symcount = bfd_get_symcount (abfd);
7232
  symcount = bfd_get_symcount (abfd);
6652
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7233
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6653
  symtab_hdr->sh_type = SHT_SYMTAB;
7234
  symtab_hdr->sh_type = SHT_SYMTAB;
6654
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
7235
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
6655
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
7236
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
6656
  symtab_hdr->sh_info = num_locals + 1;
7237
  symtab_hdr->sh_info = num_locals + 1;
6657
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
7238
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
6658
 
7239
 
6659
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
7240
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
6660
  symstrtab_hdr->sh_type = SHT_STRTAB;
7241
  symstrtab_hdr->sh_type = SHT_STRTAB;
-
 
7242
 
-
 
7243
  /* Allocate buffer to swap out the .strtab section.  */
-
 
7244
  symstrtab = (struct elf_sym_strtab *) bfd_malloc ((symcount + 1)
-
 
7245
						    * sizeof (*symstrtab));
-
 
7246
  if (symstrtab == NULL)
-
 
7247
    {
-
 
7248
      _bfd_elf_strtab_free (stt);
-
 
7249
      return FALSE;
-
 
7250
    }
6661
 
7251
 
6662
  outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
7252
  outbound_syms = (bfd_byte *) bfd_alloc2 (abfd, 1 + symcount,
6663
                                           bed->s->sizeof_sym);
7253
                                           bed->s->sizeof_sym);
6664
  if (outbound_syms == NULL)
7254
  if (outbound_syms == NULL)
6665
    {
7255
    {
-
 
7256
error_return:
6666
      _bfd_stringtab_free (stt);
7257
      _bfd_elf_strtab_free (stt);
-
 
7258
      free (symstrtab);
6667
      return FALSE;
7259
      return FALSE;
6668
    }
7260
    }
6669
  symtab_hdr->contents = outbound_syms;
7261
  symtab_hdr->contents = outbound_syms;
-
 
7262
  outbound_syms_index = 0;
6670
 
7263
 
-
 
7264
  outbound_shndx = NULL;
-
 
7265
  outbound_shndx_index = 0;
-
 
7266
 
-
 
7267
  if (elf_symtab_shndx_list (abfd))
6671
  outbound_shndx = NULL;
7268
    {
6672
  symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
7269
      symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
6673
  if (symtab_shndx_hdr->sh_name != 0)
7270
      if (symtab_shndx_hdr->sh_name != 0)
6674
    {
7271
	{
6675
      amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
7272
	  amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
6676
      outbound_shndx =  (bfd_byte *)
7273
	  outbound_shndx =  (bfd_byte *)
6677
          bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
7274
	    bfd_zalloc2 (abfd, 1 + symcount, sizeof (Elf_External_Sym_Shndx));
6678
      if (outbound_shndx == NULL)
7275
	  if (outbound_shndx == NULL)
6679
	{
-
 
6680
	  _bfd_stringtab_free (stt);
7276
	    goto error_return;
6681
	  return FALSE;
-
 
6682
	}
-
 
6683
 
7277
 
6684
      symtab_shndx_hdr->contents = outbound_shndx;
7278
	  symtab_shndx_hdr->contents = outbound_shndx;
6685
      symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
7279
	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
6686
      symtab_shndx_hdr->sh_size = amt;
7280
	  symtab_shndx_hdr->sh_size = amt;
6687
      symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
7281
	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
6688
      symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
7282
	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
6689
    }
7283
	}
-
 
7284
      /* FIXME: What about any other headers in the list ?  */
-
 
7285
    }
6690
 
7286
 
6691
  /* Now generate the data (for "contents").  */
7287
  /* Now generate the data (for "contents").  */
6692
  {
7288
  {
6693
    /* Fill in zeroth symbol and swap it out.  */
7289
    /* Fill in zeroth symbol and swap it out.  */
6694
    Elf_Internal_Sym sym;
7290
    Elf_Internal_Sym sym;
6695
    sym.st_name = 0;
7291
    sym.st_name = 0;
6696
    sym.st_value = 0;
7292
    sym.st_value = 0;
6697
    sym.st_size = 0;
7293
    sym.st_size = 0;
6698
    sym.st_info = 0;
7294
    sym.st_info = 0;
6699
    sym.st_other = 0;
7295
    sym.st_other = 0;
6700
    sym.st_shndx = SHN_UNDEF;
7296
    sym.st_shndx = SHN_UNDEF;
6701
    sym.st_target_internal = 0;
7297
    sym.st_target_internal = 0;
-
 
7298
    symstrtab[0].sym = sym;
6702
    bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
7299
    symstrtab[0].dest_index = outbound_syms_index;
-
 
7300
    symstrtab[0].destshndx_index = outbound_shndx_index;
6703
    outbound_syms += bed->s->sizeof_sym;
7301
    outbound_syms_index++;
6704
    if (outbound_shndx != NULL)
7302
    if (outbound_shndx != NULL)
6705
      outbound_shndx += sizeof (Elf_External_Sym_Shndx);
7303
      outbound_shndx_index++;
6706
  }
7304
  }
6707
 
7305
 
6708
  name_local_sections
7306
  name_local_sections
6709
    = (bed->elf_backend_name_local_section_symbols
7307
    = (bed->elf_backend_name_local_section_symbols
6710
       && bed->elf_backend_name_local_section_symbols (abfd));
7308
       && bed->elf_backend_name_local_section_symbols (abfd));
6711
 
7309
 
6712
  syms = bfd_get_outsymbols (abfd);
7310
  syms = bfd_get_outsymbols (abfd);
6713
  for (idx = 0; idx < symcount; idx++)
7311
  for (idx = 0; idx < symcount;)
6714
    {
7312
    {
6715
      Elf_Internal_Sym sym;
7313
      Elf_Internal_Sym sym;
6716
      bfd_vma value = syms[idx]->value;
7314
      bfd_vma value = syms[idx]->value;
6717
      elf_symbol_type *type_ptr;
7315
      elf_symbol_type *type_ptr;
6718
      flagword flags = syms[idx]->flags;
7316
      flagword flags = syms[idx]->flags;
6719
      int type;
7317
      int type;
6720
 
7318
 
6721
      if (!name_local_sections
7319
      if (!name_local_sections
6722
	  && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
7320
	  && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
6723
	{
7321
	{
6724
	  /* Local section symbols have no name.  */
7322
	  /* Local section symbols have no name.  */
6725
	  sym.st_name = 0;
7323
	  sym.st_name = (unsigned long) -1;
6726
	}
7324
	}
6727
      else
7325
      else
6728
	{
7326
	{
6729
	  sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
7327
	  /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
-
 
7328
	     to get the final offset for st_name.  */
6730
							    syms[idx]->name,
7329
	  sym.st_name
-
 
7330
	    = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name,
6731
							    TRUE, FALSE);
7331
						   FALSE);
6732
	  if (sym.st_name == (unsigned long) -1)
7332
	  if (sym.st_name == (unsigned long) -1)
6733
	    {
-
 
6734
	      _bfd_stringtab_free (stt);
-
 
6735
	      return FALSE;
7333
	    goto error_return;
6736
	    }
-
 
6737
	}
7334
	}
6738
 
7335
 
6739
      type_ptr = elf_symbol_from (abfd, syms[idx]);
7336
      type_ptr = elf_symbol_from (abfd, syms[idx]);
6740
 
7337
 
6741
      if ((flags & BSF_SECTION_SYM) == 0
7338
      if ((flags & BSF_SECTION_SYM) == 0
6742
	  && bfd_is_com_section (syms[idx]->section))
7339
	  && bfd_is_com_section (syms[idx]->section))
6743
	{
7340
	{
6744
	  /* ELF common symbols put the alignment into the `value' field,
7341
	  /* ELF common symbols put the alignment into the `value' field,
6745
	     and the size into the `size' field.  This is backwards from
7342
	     and the size into the `size' field.  This is backwards from
6746
	     how BFD handles it, so reverse it here.  */
7343
	     how BFD handles it, so reverse it here.  */
6747
	  sym.st_size = value;
7344
	  sym.st_size = value;
6748
	  if (type_ptr == NULL
7345
	  if (type_ptr == NULL
6749
	      || type_ptr->internal_elf_sym.st_value == 0)
7346
	      || type_ptr->internal_elf_sym.st_value == 0)
6750
	    sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
7347
	    sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
6751
	  else
7348
	  else
6752
	    sym.st_value = type_ptr->internal_elf_sym.st_value;
7349
	    sym.st_value = type_ptr->internal_elf_sym.st_value;
6753
	  sym.st_shndx = _bfd_elf_section_from_bfd_section
7350
	  sym.st_shndx = _bfd_elf_section_from_bfd_section
6754
	    (abfd, syms[idx]->section);
7351
	    (abfd, syms[idx]->section);
6755
	}
7352
	}
6756
      else
7353
      else
6757
	{
7354
	{
6758
	  asection *sec = syms[idx]->section;
7355
	  asection *sec = syms[idx]->section;
6759
	  unsigned int shndx;
7356
	  unsigned int shndx;
6760
 
7357
 
6761
	  if (sec->output_section)
7358
	  if (sec->output_section)
6762
	    {
7359
	    {
6763
	      value += sec->output_offset;
7360
	      value += sec->output_offset;
6764
	      sec = sec->output_section;
7361
	      sec = sec->output_section;
6765
	    }
7362
	    }
6766
 
7363
 
6767
	  /* Don't add in the section vma for relocatable output.  */
7364
	  /* Don't add in the section vma for relocatable output.  */
6768
	  if (! relocatable_p)
7365
	  if (! relocatable_p)
6769
	    value += sec->vma;
7366
	    value += sec->vma;
6770
	  sym.st_value = value;
7367
	  sym.st_value = value;
6771
	  sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
7368
	  sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
6772
 
7369
 
6773
	  if (bfd_is_abs_section (sec)
7370
	  if (bfd_is_abs_section (sec)
6774
	      && type_ptr != NULL
7371
	      && type_ptr != NULL
6775
	      && type_ptr->internal_elf_sym.st_shndx != 0)
7372
	      && type_ptr->internal_elf_sym.st_shndx != 0)
6776
	    {
7373
	    {
6777
	      /* This symbol is in a real ELF section which we did
7374
	      /* This symbol is in a real ELF section which we did
6778
		 not create as a BFD section.  Undo the mapping done
7375
		 not create as a BFD section.  Undo the mapping done
6779
		 by copy_private_symbol_data.  */
7376
		 by copy_private_symbol_data.  */
6780
	      shndx = type_ptr->internal_elf_sym.st_shndx;
7377
	      shndx = type_ptr->internal_elf_sym.st_shndx;
6781
	      switch (shndx)
7378
	      switch (shndx)
6782
		{
7379
		{
6783
		case MAP_ONESYMTAB:
7380
		case MAP_ONESYMTAB:
6784
		  shndx = elf_onesymtab (abfd);
7381
		  shndx = elf_onesymtab (abfd);
6785
		  break;
7382
		  break;
6786
		case MAP_DYNSYMTAB:
7383
		case MAP_DYNSYMTAB:
6787
		  shndx = elf_dynsymtab (abfd);
7384
		  shndx = elf_dynsymtab (abfd);
6788
		  break;
7385
		  break;
6789
		case MAP_STRTAB:
7386
		case MAP_STRTAB:
6790
		  shndx = elf_strtab_sec (abfd);
7387
		  shndx = elf_strtab_sec (abfd);
6791
		  break;
7388
		  break;
6792
		case MAP_SHSTRTAB:
7389
		case MAP_SHSTRTAB:
6793
		  shndx = elf_shstrtab_sec (abfd);
7390
		  shndx = elf_shstrtab_sec (abfd);
6794
		  break;
7391
		  break;
6795
		case MAP_SYM_SHNDX:
7392
		case MAP_SYM_SHNDX:
-
 
7393
		  if (elf_symtab_shndx_list (abfd))
6796
		  shndx = elf_symtab_shndx (abfd);
7394
		    shndx = elf_symtab_shndx_list (abfd)->ndx;
6797
		  break;
7395
		  break;
6798
		default:
7396
		default:
6799
		  shndx = SHN_ABS;
7397
		  shndx = SHN_ABS;
6800
		  break;
7398
		  break;
6801
		}
7399
		}
6802
	    }
7400
	    }
6803
	  else
7401
	  else
6804
	    {
7402
	    {
6805
	      shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
7403
	      shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
6806
 
7404
 
6807
	      if (shndx == SHN_BAD)
7405
	      if (shndx == SHN_BAD)
6808
		{
7406
		{
6809
		  asection *sec2;
7407
		  asection *sec2;
6810
 
7408
 
6811
		  /* Writing this would be a hell of a lot easier if
7409
		  /* Writing this would be a hell of a lot easier if
6812
		     we had some decent documentation on bfd, and
7410
		     we had some decent documentation on bfd, and
6813
		     knew what to expect of the library, and what to
7411
		     knew what to expect of the library, and what to
6814
		     demand of applications.  For example, it
7412
		     demand of applications.  For example, it
6815
		     appears that `objcopy' might not set the
7413
		     appears that `objcopy' might not set the
6816
		     section of a symbol to be a section that is
7414
		     section of a symbol to be a section that is
6817
		     actually in the output file.  */
7415
		     actually in the output file.  */
6818
		  sec2 = bfd_get_section_by_name (abfd, sec->name);
7416
		  sec2 = bfd_get_section_by_name (abfd, sec->name);
6819
		  if (sec2 == NULL)
7417
		  if (sec2 == NULL)
6820
		    {
7418
		    {
6821
		      _bfd_error_handler (_("\
7419
		      _bfd_error_handler (_("\
6822
Unable to find equivalent output section for symbol '%s' from section '%s'"),
7420
Unable to find equivalent output section for symbol '%s' from section '%s'"),
6823
					  syms[idx]->name ? syms[idx]->name : "",
7421
					  syms[idx]->name ? syms[idx]->name : "",
6824
					  sec->name);
7422
					  sec->name);
6825
		      bfd_set_error (bfd_error_invalid_operation);
7423
		      bfd_set_error (bfd_error_invalid_operation);
6826
		      _bfd_stringtab_free (stt);
-
 
6827
		      return FALSE;
7424
		      goto error_return;
6828
		    }
7425
		    }
6829
 
7426
 
6830
		  shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
7427
		  shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
6831
		  BFD_ASSERT (shndx != SHN_BAD);
7428
		  BFD_ASSERT (shndx != SHN_BAD);
6832
		}
7429
		}
6833
	    }
7430
	    }
6834
 
7431
 
6835
	  sym.st_shndx = shndx;
7432
	  sym.st_shndx = shndx;
6836
	}
7433
	}
6837
 
7434
 
6838
      if ((flags & BSF_THREAD_LOCAL) != 0)
7435
      if ((flags & BSF_THREAD_LOCAL) != 0)
6839
	type = STT_TLS;
7436
	type = STT_TLS;
6840
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
7437
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
6841
	type = STT_GNU_IFUNC;
7438
	type = STT_GNU_IFUNC;
6842
      else if ((flags & BSF_FUNCTION) != 0)
7439
      else if ((flags & BSF_FUNCTION) != 0)
6843
	type = STT_FUNC;
7440
	type = STT_FUNC;
6844
      else if ((flags & BSF_OBJECT) != 0)
7441
      else if ((flags & BSF_OBJECT) != 0)
6845
	type = STT_OBJECT;
7442
	type = STT_OBJECT;
6846
      else if ((flags & BSF_RELC) != 0)
7443
      else if ((flags & BSF_RELC) != 0)
6847
	type = STT_RELC;
7444
	type = STT_RELC;
6848
      else if ((flags & BSF_SRELC) != 0)
7445
      else if ((flags & BSF_SRELC) != 0)
6849
	type = STT_SRELC;
7446
	type = STT_SRELC;
6850
      else
7447
      else
6851
	type = STT_NOTYPE;
7448
	type = STT_NOTYPE;
6852
 
7449
 
6853
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
7450
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
6854
	type = STT_TLS;
7451
	type = STT_TLS;
6855
 
7452
 
6856
      /* Processor-specific types.  */
7453
      /* Processor-specific types.  */
6857
      if (type_ptr != NULL
7454
      if (type_ptr != NULL
6858
	  && bed->elf_backend_get_symbol_type)
7455
	  && bed->elf_backend_get_symbol_type)
6859
	type = ((*bed->elf_backend_get_symbol_type)
7456
	type = ((*bed->elf_backend_get_symbol_type)
6860
		(&type_ptr->internal_elf_sym, type));
7457
		(&type_ptr->internal_elf_sym, type));
6861
 
7458
 
6862
      if (flags & BSF_SECTION_SYM)
7459
      if (flags & BSF_SECTION_SYM)
6863
	{
7460
	{
6864
	  if (flags & BSF_GLOBAL)
7461
	  if (flags & BSF_GLOBAL)
6865
	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
7462
	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
6866
	  else
7463
	  else
6867
	    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
7464
	    sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
6868
	}
7465
	}
6869
      else if (bfd_is_com_section (syms[idx]->section))
7466
      else if (bfd_is_com_section (syms[idx]->section))
6870
	{
7467
	{
6871
#ifdef USE_STT_COMMON
7468
#ifdef USE_STT_COMMON
6872
	  if (type == STT_OBJECT)
7469
	  if (type == STT_OBJECT)
6873
	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
7470
	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_COMMON);
6874
	  else
7471
	  else
6875
#endif
7472
#endif
6876
	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
7473
	    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
6877
	}
7474
	}
6878
      else if (bfd_is_und_section (syms[idx]->section))
7475
      else if (bfd_is_und_section (syms[idx]->section))
6879
	sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
7476
	sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
6880
				    ? STB_WEAK
7477
				    ? STB_WEAK
6881
				    : STB_GLOBAL),
7478
				    : STB_GLOBAL),
6882
				   type);
7479
				   type);
6883
      else if (flags & BSF_FILE)
7480
      else if (flags & BSF_FILE)
6884
	sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
7481
	sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
6885
      else
7482
      else
6886
	{
7483
	{
6887
	  int bind = STB_LOCAL;
7484
	  int bind = STB_LOCAL;
6888
 
7485
 
6889
	  if (flags & BSF_LOCAL)
7486
	  if (flags & BSF_LOCAL)
6890
	    bind = STB_LOCAL;
7487
	    bind = STB_LOCAL;
6891
	  else if (flags & BSF_GNU_UNIQUE)
7488
	  else if (flags & BSF_GNU_UNIQUE)
6892
	    bind = STB_GNU_UNIQUE;
7489
	    bind = STB_GNU_UNIQUE;
6893
	  else if (flags & BSF_WEAK)
7490
	  else if (flags & BSF_WEAK)
6894
	    bind = STB_WEAK;
7491
	    bind = STB_WEAK;
6895
	  else if (flags & BSF_GLOBAL)
7492
	  else if (flags & BSF_GLOBAL)
6896
	    bind = STB_GLOBAL;
7493
	    bind = STB_GLOBAL;
6897
 
7494
 
6898
	  sym.st_info = ELF_ST_INFO (bind, type);
7495
	  sym.st_info = ELF_ST_INFO (bind, type);
6899
	}
7496
	}
6900
 
7497
 
6901
      if (type_ptr != NULL)
7498
      if (type_ptr != NULL)
6902
	{
7499
	{
6903
	  sym.st_other = type_ptr->internal_elf_sym.st_other;
7500
	  sym.st_other = type_ptr->internal_elf_sym.st_other;
6904
	  sym.st_target_internal
7501
	  sym.st_target_internal
6905
	    = type_ptr->internal_elf_sym.st_target_internal;
7502
	    = type_ptr->internal_elf_sym.st_target_internal;
6906
	}
7503
	}
6907
      else
7504
      else
6908
	{
7505
	{
6909
	  sym.st_other = 0;
7506
	  sym.st_other = 0;
6910
	  sym.st_target_internal = 0;
7507
	  sym.st_target_internal = 0;
6911
	}
7508
	}
-
 
7509
 
-
 
7510
      idx++;
6912
 
7511
      symstrtab[idx].sym = sym;
-
 
7512
      symstrtab[idx].dest_index = outbound_syms_index;
-
 
7513
      symstrtab[idx].destshndx_index = outbound_shndx_index;
6913
      bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
7514
 
6914
      outbound_syms += bed->s->sizeof_sym;
7515
      outbound_syms_index++;
6915
      if (outbound_shndx != NULL)
7516
      if (outbound_shndx != NULL)
6916
	outbound_shndx += sizeof (Elf_External_Sym_Shndx);
7517
	outbound_shndx_index++;
-
 
7518
    }
-
 
7519
 
-
 
7520
  /* Finalize the .strtab section.  */
-
 
7521
  _bfd_elf_strtab_finalize (stt);
-
 
7522
 
-
 
7523
  /* Swap out the .strtab section.  */
-
 
7524
  for (idx = 0; idx <= symcount; idx++)
-
 
7525
    {
-
 
7526
      struct elf_sym_strtab *elfsym = &symstrtab[idx];
-
 
7527
      if (elfsym->sym.st_name == (unsigned long) -1)
-
 
7528
	elfsym->sym.st_name = 0;
-
 
7529
      else
-
 
7530
	elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
-
 
7531
						      elfsym->sym.st_name);
-
 
7532
      bed->s->swap_symbol_out (abfd, &elfsym->sym,
-
 
7533
			       (outbound_syms
-
 
7534
				+ (elfsym->dest_index
-
 
7535
				   * bed->s->sizeof_sym)),
-
 
7536
			       (outbound_shndx
-
 
7537
				+ (elfsym->destshndx_index
-
 
7538
				   * sizeof (Elf_External_Sym_Shndx))));
-
 
7539
    }
6917
    }
7540
  free (symstrtab);
6918
 
7541
 
6919
  *sttp = stt;
7542
  *sttp = stt;
6920
  symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
7543
  symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
6921
  symstrtab_hdr->sh_type = SHT_STRTAB;
7544
  symstrtab_hdr->sh_type = SHT_STRTAB;
6922
 
7545
 
6923
  symstrtab_hdr->sh_flags = 0;
7546
  symstrtab_hdr->sh_flags = 0;
6924
  symstrtab_hdr->sh_addr = 0;
7547
  symstrtab_hdr->sh_addr = 0;
6925
  symstrtab_hdr->sh_entsize = 0;
7548
  symstrtab_hdr->sh_entsize = 0;
6926
  symstrtab_hdr->sh_link = 0;
7549
  symstrtab_hdr->sh_link = 0;
6927
  symstrtab_hdr->sh_info = 0;
7550
  symstrtab_hdr->sh_info = 0;
6928
  symstrtab_hdr->sh_addralign = 1;
7551
  symstrtab_hdr->sh_addralign = 1;
6929
 
7552
 
6930
  return TRUE;
7553
  return TRUE;
6931
}
7554
}
6932
 
7555
 
6933
/* Return the number of bytes required to hold the symtab vector.
7556
/* Return the number of bytes required to hold the symtab vector.
6934
 
7557
 
6935
   Note that we base it on the count plus 1, since we will null terminate
7558
   Note that we base it on the count plus 1, since we will null terminate
6936
   the vector allocated based on this size.  However, the ELF symbol table
7559
   the vector allocated based on this size.  However, the ELF symbol table
6937
   always has a dummy entry as symbol #0, so it ends up even.  */
7560
   always has a dummy entry as symbol #0, so it ends up even.  */
6938
 
7561
 
6939
long
7562
long
6940
_bfd_elf_get_symtab_upper_bound (bfd *abfd)
7563
_bfd_elf_get_symtab_upper_bound (bfd *abfd)
6941
{
7564
{
6942
  long symcount;
7565
  long symcount;
6943
  long symtab_size;
7566
  long symtab_size;
6944
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
7567
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
6945
 
7568
 
6946
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7569
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6947
  symtab_size = (symcount + 1) * (sizeof (asymbol *));
7570
  symtab_size = (symcount + 1) * (sizeof (asymbol *));
6948
  if (symcount > 0)
7571
  if (symcount > 0)
6949
    symtab_size -= sizeof (asymbol *);
7572
    symtab_size -= sizeof (asymbol *);
6950
 
7573
 
6951
  return symtab_size;
7574
  return symtab_size;
6952
}
7575
}
6953
 
7576
 
6954
long
7577
long
6955
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
7578
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
6956
{
7579
{
6957
  long symcount;
7580
  long symcount;
6958
  long symtab_size;
7581
  long symtab_size;
6959
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
7582
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
6960
 
7583
 
6961
  if (elf_dynsymtab (abfd) == 0)
7584
  if (elf_dynsymtab (abfd) == 0)
6962
    {
7585
    {
6963
      bfd_set_error (bfd_error_invalid_operation);
7586
      bfd_set_error (bfd_error_invalid_operation);
6964
      return -1;
7587
      return -1;
6965
    }
7588
    }
6966
 
7589
 
6967
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
7590
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
6968
  symtab_size = (symcount + 1) * (sizeof (asymbol *));
7591
  symtab_size = (symcount + 1) * (sizeof (asymbol *));
6969
  if (symcount > 0)
7592
  if (symcount > 0)
6970
    symtab_size -= sizeof (asymbol *);
7593
    symtab_size -= sizeof (asymbol *);
6971
 
7594
 
6972
  return symtab_size;
7595
  return symtab_size;
6973
}
7596
}
6974
 
7597
 
6975
long
7598
long
6976
_bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
7599
_bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
6977
				sec_ptr asect)
7600
				sec_ptr asect)
6978
{
7601
{
6979
  return (asect->reloc_count + 1) * sizeof (arelent *);
7602
  return (asect->reloc_count + 1) * sizeof (arelent *);
6980
}
7603
}
6981
 
7604
 
6982
/* Canonicalize the relocs.  */
7605
/* Canonicalize the relocs.  */
6983
 
7606
 
6984
long
7607
long
6985
_bfd_elf_canonicalize_reloc (bfd *abfd,
7608
_bfd_elf_canonicalize_reloc (bfd *abfd,
6986
			     sec_ptr section,
7609
			     sec_ptr section,
6987
			     arelent **relptr,
7610
			     arelent **relptr,
6988
			     asymbol **symbols)
7611
			     asymbol **symbols)
6989
{
7612
{
6990
  arelent *tblptr;
7613
  arelent *tblptr;
6991
  unsigned int i;
7614
  unsigned int i;
6992
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7615
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6993
 
7616
 
6994
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
7617
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
6995
    return -1;
7618
    return -1;
6996
 
7619
 
6997
  tblptr = section->relocation;
7620
  tblptr = section->relocation;
6998
  for (i = 0; i < section->reloc_count; i++)
7621
  for (i = 0; i < section->reloc_count; i++)
6999
    *relptr++ = tblptr++;
7622
    *relptr++ = tblptr++;
7000
 
7623
 
7001
  *relptr = NULL;
7624
  *relptr = NULL;
7002
 
7625
 
7003
  return section->reloc_count;
7626
  return section->reloc_count;
7004
}
7627
}
7005
 
7628
 
7006
long
7629
long
7007
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
7630
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
7008
{
7631
{
7009
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7632
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7010
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
7633
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE);
7011
 
7634
 
7012
  if (symcount >= 0)
7635
  if (symcount >= 0)
7013
    bfd_get_symcount (abfd) = symcount;
7636
    bfd_get_symcount (abfd) = symcount;
7014
  return symcount;
7637
  return symcount;
7015
}
7638
}
7016
 
7639
 
7017
long
7640
long
7018
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
7641
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
7019
				      asymbol **allocation)
7642
				      asymbol **allocation)
7020
{
7643
{
7021
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7644
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7022
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
7645
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE);
7023
 
7646
 
7024
  if (symcount >= 0)
7647
  if (symcount >= 0)
7025
    bfd_get_dynamic_symcount (abfd) = symcount;
7648
    bfd_get_dynamic_symcount (abfd) = symcount;
7026
  return symcount;
7649
  return symcount;
7027
}
7650
}
7028
 
7651
 
7029
/* Return the size required for the dynamic reloc entries.  Any loadable
7652
/* Return the size required for the dynamic reloc entries.  Any loadable
7030
   section that was actually installed in the BFD, and has type SHT_REL
7653
   section that was actually installed in the BFD, and has type SHT_REL
7031
   or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7654
   or SHT_RELA, and uses the dynamic symbol table, is considered to be a
7032
   dynamic reloc section.  */
7655
   dynamic reloc section.  */
7033
 
7656
 
7034
long
7657
long
7035
_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
7658
_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
7036
{
7659
{
7037
  long ret;
7660
  long ret;
7038
  asection *s;
7661
  asection *s;
7039
 
7662
 
7040
  if (elf_dynsymtab (abfd) == 0)
7663
  if (elf_dynsymtab (abfd) == 0)
7041
    {
7664
    {
7042
      bfd_set_error (bfd_error_invalid_operation);
7665
      bfd_set_error (bfd_error_invalid_operation);
7043
      return -1;
7666
      return -1;
7044
    }
7667
    }
7045
 
7668
 
7046
  ret = sizeof (arelent *);
7669
  ret = sizeof (arelent *);
7047
  for (s = abfd->sections; s != NULL; s = s->next)
7670
  for (s = abfd->sections; s != NULL; s = s->next)
7048
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7671
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7049
	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7672
	&& (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7050
	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7673
	    || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7051
      ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
7674
      ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize)
7052
	      * sizeof (arelent *));
7675
	      * sizeof (arelent *));
7053
 
7676
 
7054
  return ret;
7677
  return ret;
7055
}
7678
}
7056
 
7679
 
7057
/* Canonicalize the dynamic relocation entries.  Note that we return the
7680
/* Canonicalize the dynamic relocation entries.  Note that we return the
7058
   dynamic relocations as a single block, although they are actually
7681
   dynamic relocations as a single block, although they are actually
7059
   associated with particular sections; the interface, which was
7682
   associated with particular sections; the interface, which was
7060
   designed for SunOS style shared libraries, expects that there is only
7683
   designed for SunOS style shared libraries, expects that there is only
7061
   one set of dynamic relocs.  Any loadable section that was actually
7684
   one set of dynamic relocs.  Any loadable section that was actually
7062
   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7685
   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
7063
   dynamic symbol table, is considered to be a dynamic reloc section.  */
7686
   dynamic symbol table, is considered to be a dynamic reloc section.  */
7064
 
7687
 
7065
long
7688
long
7066
_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
7689
_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
7067
				     arelent **storage,
7690
				     arelent **storage,
7068
				     asymbol **syms)
7691
				     asymbol **syms)
7069
{
7692
{
7070
  bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7693
  bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
7071
  asection *s;
7694
  asection *s;
7072
  long ret;
7695
  long ret;
7073
 
7696
 
7074
  if (elf_dynsymtab (abfd) == 0)
7697
  if (elf_dynsymtab (abfd) == 0)
7075
    {
7698
    {
7076
      bfd_set_error (bfd_error_invalid_operation);
7699
      bfd_set_error (bfd_error_invalid_operation);
7077
      return -1;
7700
      return -1;
7078
    }
7701
    }
7079
 
7702
 
7080
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7703
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
7081
  ret = 0;
7704
  ret = 0;
7082
  for (s = abfd->sections; s != NULL; s = s->next)
7705
  for (s = abfd->sections; s != NULL; s = s->next)
7083
    {
7706
    {
7084
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7707
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
7085
	  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7708
	  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
7086
	      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7709
	      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
7087
	{
7710
	{
7088
	  arelent *p;
7711
	  arelent *p;
7089
	  long count, i;
7712
	  long count, i;
7090
 
7713
 
7091
	  if (! (*slurp_relocs) (abfd, s, syms, TRUE))
7714
	  if (! (*slurp_relocs) (abfd, s, syms, TRUE))
7092
	    return -1;
7715
	    return -1;
7093
	  count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
7716
	  count = s->size / elf_section_data (s)->this_hdr.sh_entsize;
7094
	  p = s->relocation;
7717
	  p = s->relocation;
7095
	  for (i = 0; i < count; i++)
7718
	  for (i = 0; i < count; i++)
7096
	    *storage++ = p++;
7719
	    *storage++ = p++;
7097
	  ret += count;
7720
	  ret += count;
7098
	}
7721
	}
7099
    }
7722
    }
7100
 
7723
 
7101
  *storage = NULL;
7724
  *storage = NULL;
7102
 
7725
 
7103
  return ret;
7726
  return ret;
7104
}
7727
}
7105

7728

7106
/* Read in the version information.  */
7729
/* Read in the version information.  */
7107
 
7730
 
7108
bfd_boolean
7731
bfd_boolean
7109
_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
7732
_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver)
7110
{
7733
{
7111
  bfd_byte *contents = NULL;
7734
  bfd_byte *contents = NULL;
7112
  unsigned int freeidx = 0;
7735
  unsigned int freeidx = 0;
7113
 
7736
 
7114
  if (elf_dynverref (abfd) != 0)
7737
  if (elf_dynverref (abfd) != 0)
7115
    {
7738
    {
7116
      Elf_Internal_Shdr *hdr;
7739
      Elf_Internal_Shdr *hdr;
7117
      Elf_External_Verneed *everneed;
7740
      Elf_External_Verneed *everneed;
7118
      Elf_Internal_Verneed *iverneed;
7741
      Elf_Internal_Verneed *iverneed;
7119
      unsigned int i;
7742
      unsigned int i;
7120
      bfd_byte *contents_end;
7743
      bfd_byte *contents_end;
7121
 
7744
 
7122
      hdr = &elf_tdata (abfd)->dynverref_hdr;
7745
      hdr = &elf_tdata (abfd)->dynverref_hdr;
7123
 
-
 
7124
      elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
7746
 
7125
          bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
-
 
7126
      if (elf_tdata (abfd)->verref == NULL)
-
 
7127
	goto error_return;
-
 
7128
 
-
 
7129
      elf_tdata (abfd)->cverrefs = hdr->sh_info;
-
 
7130
 
-
 
7131
      contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
-
 
7132
      if (contents == NULL)
7747
      if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verneed))
-
 
7748
	{
-
 
7749
error_return_bad_verref:
-
 
7750
	  (*_bfd_error_handler)
-
 
7751
	    (_("%B: .gnu.version_r invalid entry"), abfd);
7133
	{
7752
	  bfd_set_error (bfd_error_bad_value);
7134
error_return_verref:
7753
error_return_verref:
7135
	  elf_tdata (abfd)->verref = NULL;
7754
	  elf_tdata (abfd)->verref = NULL;
7136
	  elf_tdata (abfd)->cverrefs = 0;
7755
	  elf_tdata (abfd)->cverrefs = 0;
7137
	  goto error_return;
7756
	  goto error_return;
7138
	}
7757
	}
-
 
7758
 
-
 
7759
      contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
-
 
7760
      if (contents == NULL)
-
 
7761
	goto error_return_verref;
-
 
7762
 
7139
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7763
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7140
	  || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7764
	  || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7141
	goto error_return_verref;
7765
	goto error_return_verref;
-
 
7766
 
7142
 
7767
      elf_tdata (abfd)->verref = (Elf_Internal_Verneed *)
-
 
7768
	bfd_zalloc2 (abfd, hdr->sh_info, sizeof (Elf_Internal_Verneed));
-
 
7769
 
7143
      if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed))
7770
      if (elf_tdata (abfd)->verref == NULL)
7144
	goto error_return_verref;
7771
	goto error_return_verref;
7145
 
7772
 
7146
      BFD_ASSERT (sizeof (Elf_External_Verneed)
7773
      BFD_ASSERT (sizeof (Elf_External_Verneed)
7147
		  == sizeof (Elf_External_Vernaux));
7774
		  == sizeof (Elf_External_Vernaux));
7148
      contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
7775
      contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed);
7149
      everneed = (Elf_External_Verneed *) contents;
7776
      everneed = (Elf_External_Verneed *) contents;
7150
      iverneed = elf_tdata (abfd)->verref;
7777
      iverneed = elf_tdata (abfd)->verref;
7151
      for (i = 0; i < hdr->sh_info; i++, iverneed++)
7778
      for (i = 0; i < hdr->sh_info; i++, iverneed++)
7152
	{
7779
	{
7153
	  Elf_External_Vernaux *evernaux;
7780
	  Elf_External_Vernaux *evernaux;
7154
	  Elf_Internal_Vernaux *ivernaux;
7781
	  Elf_Internal_Vernaux *ivernaux;
7155
	  unsigned int j;
7782
	  unsigned int j;
7156
 
7783
 
7157
	  _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
7784
	  _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
7158
 
7785
 
7159
	  iverneed->vn_bfd = abfd;
7786
	  iverneed->vn_bfd = abfd;
7160
 
7787
 
7161
	  iverneed->vn_filename =
7788
	  iverneed->vn_filename =
7162
	    bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7789
	    bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7163
					     iverneed->vn_file);
7790
					     iverneed->vn_file);
7164
	  if (iverneed->vn_filename == NULL)
7791
	  if (iverneed->vn_filename == NULL)
7165
	    goto error_return_verref;
7792
	    goto error_return_bad_verref;
7166
 
7793
 
7167
	  if (iverneed->vn_cnt == 0)
7794
	  if (iverneed->vn_cnt == 0)
7168
	    iverneed->vn_auxptr = NULL;
7795
	    iverneed->vn_auxptr = NULL;
7169
	  else
7796
	  else
7170
	    {
7797
	    {
7171
	      iverneed->vn_auxptr = (struct elf_internal_vernaux *)
7798
	      iverneed->vn_auxptr = (struct elf_internal_vernaux *)
7172
                  bfd_alloc2 (abfd, iverneed->vn_cnt,
7799
                  bfd_alloc2 (abfd, iverneed->vn_cnt,
7173
                              sizeof (Elf_Internal_Vernaux));
7800
                              sizeof (Elf_Internal_Vernaux));
7174
	      if (iverneed->vn_auxptr == NULL)
7801
	      if (iverneed->vn_auxptr == NULL)
7175
		goto error_return_verref;
7802
		goto error_return_verref;
7176
	    }
7803
	    }
7177
 
7804
 
7178
	  if (iverneed->vn_aux
7805
	  if (iverneed->vn_aux
7179
	      > (size_t) (contents_end - (bfd_byte *) everneed))
7806
	      > (size_t) (contents_end - (bfd_byte *) everneed))
7180
	    goto error_return_verref;
7807
	    goto error_return_bad_verref;
7181
 
7808
 
7182
	  evernaux = ((Elf_External_Vernaux *)
7809
	  evernaux = ((Elf_External_Vernaux *)
7183
		      ((bfd_byte *) everneed + iverneed->vn_aux));
7810
		      ((bfd_byte *) everneed + iverneed->vn_aux));
7184
	  ivernaux = iverneed->vn_auxptr;
7811
	  ivernaux = iverneed->vn_auxptr;
7185
	  for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
7812
	  for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
7186
	    {
7813
	    {
7187
	      _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
7814
	      _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
7188
 
7815
 
7189
	      ivernaux->vna_nodename =
7816
	      ivernaux->vna_nodename =
7190
		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7817
		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7191
						 ivernaux->vna_name);
7818
						 ivernaux->vna_name);
7192
	      if (ivernaux->vna_nodename == NULL)
7819
	      if (ivernaux->vna_nodename == NULL)
7193
		goto error_return_verref;
7820
		goto error_return_bad_verref;
-
 
7821
 
-
 
7822
	      if (ivernaux->vna_other > freeidx)
-
 
7823
		freeidx = ivernaux->vna_other;
-
 
7824
 
-
 
7825
	      ivernaux->vna_nextptr = NULL;
-
 
7826
	      if (ivernaux->vna_next == 0)
-
 
7827
		{
-
 
7828
		  iverneed->vn_cnt = j + 1;
-
 
7829
		  break;
7194
 
7830
		}
7195
	      if (j + 1 < iverneed->vn_cnt)
7831
	      if (j + 1 < iverneed->vn_cnt)
7196
		ivernaux->vna_nextptr = ivernaux + 1;
-
 
7197
	      else
-
 
7198
		ivernaux->vna_nextptr = NULL;
7832
		ivernaux->vna_nextptr = ivernaux + 1;
7199
 
7833
 
7200
	      if (ivernaux->vna_next
7834
	      if (ivernaux->vna_next
7201
		  > (size_t) (contents_end - (bfd_byte *) evernaux))
7835
		  > (size_t) (contents_end - (bfd_byte *) evernaux))
7202
		goto error_return_verref;
7836
		goto error_return_bad_verref;
7203
 
7837
 
7204
	      evernaux = ((Elf_External_Vernaux *)
7838
	      evernaux = ((Elf_External_Vernaux *)
7205
			  ((bfd_byte *) evernaux + ivernaux->vna_next));
7839
			  ((bfd_byte *) evernaux + ivernaux->vna_next));
7206
 
-
 
7207
	      if (ivernaux->vna_other > freeidx)
-
 
7208
		freeidx = ivernaux->vna_other;
-
 
7209
	    }
7840
	    }
-
 
7841
 
-
 
7842
	  iverneed->vn_nextref = NULL;
-
 
7843
	  if (iverneed->vn_next == 0)
7210
 
7844
	    break;
7211
	  if (i + 1 < hdr->sh_info)
7845
	  if (i + 1 < hdr->sh_info)
7212
	    iverneed->vn_nextref = iverneed + 1;
-
 
7213
	  else
-
 
7214
	    iverneed->vn_nextref = NULL;
7846
	    iverneed->vn_nextref = iverneed + 1;
7215
 
7847
 
7216
	  if (iverneed->vn_next
7848
	  if (iverneed->vn_next
7217
	      > (size_t) (contents_end - (bfd_byte *) everneed))
7849
	      > (size_t) (contents_end - (bfd_byte *) everneed))
7218
	    goto error_return_verref;
7850
	    goto error_return_bad_verref;
7219
 
7851
 
7220
	  everneed = ((Elf_External_Verneed *)
7852
	  everneed = ((Elf_External_Verneed *)
7221
		      ((bfd_byte *) everneed + iverneed->vn_next));
7853
		      ((bfd_byte *) everneed + iverneed->vn_next));
7222
	}
7854
	}
-
 
7855
      elf_tdata (abfd)->cverrefs = i;
7223
 
7856
 
7224
      free (contents);
7857
      free (contents);
7225
      contents = NULL;
7858
      contents = NULL;
7226
    }
7859
    }
7227
 
7860
 
7228
  if (elf_dynverdef (abfd) != 0)
7861
  if (elf_dynverdef (abfd) != 0)
7229
    {
7862
    {
7230
      Elf_Internal_Shdr *hdr;
7863
      Elf_Internal_Shdr *hdr;
7231
      Elf_External_Verdef *everdef;
7864
      Elf_External_Verdef *everdef;
7232
      Elf_Internal_Verdef *iverdef;
7865
      Elf_Internal_Verdef *iverdef;
7233
      Elf_Internal_Verdef *iverdefarr;
7866
      Elf_Internal_Verdef *iverdefarr;
7234
      Elf_Internal_Verdef iverdefmem;
7867
      Elf_Internal_Verdef iverdefmem;
7235
      unsigned int i;
7868
      unsigned int i;
7236
      unsigned int maxidx;
7869
      unsigned int maxidx;
7237
      bfd_byte *contents_end_def, *contents_end_aux;
7870
      bfd_byte *contents_end_def, *contents_end_aux;
7238
 
7871
 
7239
      hdr = &elf_tdata (abfd)->dynverdef_hdr;
7872
      hdr = &elf_tdata (abfd)->dynverdef_hdr;
-
 
7873
 
-
 
7874
      if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef))
-
 
7875
	{
-
 
7876
	error_return_bad_verdef:
-
 
7877
	  (*_bfd_error_handler)
-
 
7878
	    (_("%B: .gnu.version_d invalid entry"), abfd);
-
 
7879
	  bfd_set_error (bfd_error_bad_value);
-
 
7880
	error_return_verdef:
-
 
7881
	  elf_tdata (abfd)->verdef = NULL;
-
 
7882
	  elf_tdata (abfd)->cverdefs = 0;
-
 
7883
	  goto error_return;
-
 
7884
	}
7240
 
7885
 
7241
      contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7886
      contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
7242
      if (contents == NULL)
7887
      if (contents == NULL)
7243
	goto error_return;
7888
	goto error_return_verdef;
7244
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7889
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
7245
	  || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
-
 
7246
	goto error_return;
-
 
7247
 
-
 
7248
      if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef))
7890
	  || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size)
7249
	goto error_return;
7891
	goto error_return_verdef;
7250
 
7892
 
7251
      BFD_ASSERT (sizeof (Elf_External_Verdef)
7893
      BFD_ASSERT (sizeof (Elf_External_Verdef)
7252
		  >= sizeof (Elf_External_Verdaux));
7894
		  >= sizeof (Elf_External_Verdaux));
7253
      contents_end_def = contents + hdr->sh_size
7895
      contents_end_def = contents + hdr->sh_size
7254
			 - sizeof (Elf_External_Verdef);
7896
			 - sizeof (Elf_External_Verdef);
7255
      contents_end_aux = contents + hdr->sh_size
7897
      contents_end_aux = contents + hdr->sh_size
7256
			 - sizeof (Elf_External_Verdaux);
7898
			 - sizeof (Elf_External_Verdaux);
7257
 
7899
 
7258
      /* We know the number of entries in the section but not the maximum
7900
      /* We know the number of entries in the section but not the maximum
7259
	 index.  Therefore we have to run through all entries and find
7901
	 index.  Therefore we have to run through all entries and find
7260
	 the maximum.  */
7902
	 the maximum.  */
7261
      everdef = (Elf_External_Verdef *) contents;
7903
      everdef = (Elf_External_Verdef *) contents;
7262
      maxidx = 0;
7904
      maxidx = 0;
7263
      for (i = 0; i < hdr->sh_info; ++i)
7905
      for (i = 0; i < hdr->sh_info; ++i)
7264
	{
7906
	{
7265
	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7907
	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
-
 
7908
 
-
 
7909
	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
7266
 
7910
	    goto error_return_bad_verdef;
7267
	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
7911
	  if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
7268
	    maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
7912
	    maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
-
 
7913
 
-
 
7914
	  if (iverdefmem.vd_next == 0)
-
 
7915
	    break;
7269
 
7916
 
7270
	  if (iverdefmem.vd_next
7917
	  if (iverdefmem.vd_next
7271
	      > (size_t) (contents_end_def - (bfd_byte *) everdef))
7918
	      > (size_t) (contents_end_def - (bfd_byte *) everdef))
7272
	    goto error_return;
7919
	    goto error_return_bad_verdef;
7273
 
7920
 
7274
	  everdef = ((Elf_External_Verdef *)
7921
	  everdef = ((Elf_External_Verdef *)
7275
		     ((bfd_byte *) everdef + iverdefmem.vd_next));
7922
		     ((bfd_byte *) everdef + iverdefmem.vd_next));
7276
	}
7923
	}
7277
 
7924
 
7278
      if (default_imported_symver)
7925
      if (default_imported_symver)
7279
	{
7926
	{
7280
	  if (freeidx > maxidx)
7927
	  if (freeidx > maxidx)
7281
	    maxidx = ++freeidx;
7928
	    maxidx = ++freeidx;
7282
	  else
7929
	  else
7283
	    freeidx = ++maxidx;
7930
	    freeidx = ++maxidx;
7284
	}
7931
	}
-
 
7932
 
7285
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7933
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7286
          bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
7934
	bfd_zalloc2 (abfd, maxidx, sizeof (Elf_Internal_Verdef));
7287
      if (elf_tdata (abfd)->verdef == NULL)
7935
      if (elf_tdata (abfd)->verdef == NULL)
7288
	goto error_return;
7936
	goto error_return_verdef;
7289
 
7937
 
7290
      elf_tdata (abfd)->cverdefs = maxidx;
7938
      elf_tdata (abfd)->cverdefs = maxidx;
7291
 
7939
 
7292
      everdef = (Elf_External_Verdef *) contents;
7940
      everdef = (Elf_External_Verdef *) contents;
7293
      iverdefarr = elf_tdata (abfd)->verdef;
7941
      iverdefarr = elf_tdata (abfd)->verdef;
7294
      for (i = 0; i < hdr->sh_info; i++)
7942
      for (i = 0; i < hdr->sh_info; i++)
7295
	{
7943
	{
7296
	  Elf_External_Verdaux *everdaux;
7944
	  Elf_External_Verdaux *everdaux;
7297
	  Elf_Internal_Verdaux *iverdaux;
7945
	  Elf_Internal_Verdaux *iverdaux;
7298
	  unsigned int j;
7946
	  unsigned int j;
7299
 
7947
 
7300
	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7948
	  _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
7301
 
7949
 
7302
	  if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
7950
	  if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
7303
	    {
-
 
7304
error_return_verdef:
-
 
7305
	      elf_tdata (abfd)->verdef = NULL;
-
 
7306
	      elf_tdata (abfd)->cverdefs = 0;
-
 
7307
	      goto error_return;
7951
	    goto error_return_bad_verdef;
7308
	    }
-
 
7309
 
7952
 
7310
	  iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
7953
	  iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
7311
	  memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
7954
	  memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
7312
 
7955
 
7313
	  iverdef->vd_bfd = abfd;
7956
	  iverdef->vd_bfd = abfd;
7314
 
7957
 
7315
	  if (iverdef->vd_cnt == 0)
7958
	  if (iverdef->vd_cnt == 0)
7316
	    iverdef->vd_auxptr = NULL;
7959
	    iverdef->vd_auxptr = NULL;
7317
	  else
7960
	  else
7318
	    {
7961
	    {
7319
	      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7962
	      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
7320
                  bfd_alloc2 (abfd, iverdef->vd_cnt,
7963
                  bfd_alloc2 (abfd, iverdef->vd_cnt,
7321
                              sizeof (Elf_Internal_Verdaux));
7964
                              sizeof (Elf_Internal_Verdaux));
7322
	      if (iverdef->vd_auxptr == NULL)
7965
	      if (iverdef->vd_auxptr == NULL)
7323
		goto error_return_verdef;
7966
		goto error_return_verdef;
7324
	    }
7967
	    }
7325
 
7968
 
7326
	  if (iverdef->vd_aux
7969
	  if (iverdef->vd_aux
7327
	      > (size_t) (contents_end_aux - (bfd_byte *) everdef))
7970
	      > (size_t) (contents_end_aux - (bfd_byte *) everdef))
7328
	    goto error_return_verdef;
7971
	    goto error_return_bad_verdef;
7329
 
7972
 
7330
	  everdaux = ((Elf_External_Verdaux *)
7973
	  everdaux = ((Elf_External_Verdaux *)
7331
		      ((bfd_byte *) everdef + iverdef->vd_aux));
7974
		      ((bfd_byte *) everdef + iverdef->vd_aux));
7332
	  iverdaux = iverdef->vd_auxptr;
7975
	  iverdaux = iverdef->vd_auxptr;
7333
	  for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
7976
	  for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
7334
	    {
7977
	    {
7335
	      _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
7978
	      _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
7336
 
7979
 
7337
	      iverdaux->vda_nodename =
7980
	      iverdaux->vda_nodename =
7338
		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7981
		bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
7339
						 iverdaux->vda_name);
7982
						 iverdaux->vda_name);
7340
	      if (iverdaux->vda_nodename == NULL)
7983
	      if (iverdaux->vda_nodename == NULL)
7341
		goto error_return_verdef;
7984
		goto error_return_bad_verdef;
-
 
7985
 
-
 
7986
	      iverdaux->vda_nextptr = NULL;
-
 
7987
	      if (iverdaux->vda_next == 0)
-
 
7988
		{
-
 
7989
		  iverdef->vd_cnt = j + 1;
-
 
7990
		  break;
7342
 
7991
		}
7343
	      if (j + 1 < iverdef->vd_cnt)
7992
	      if (j + 1 < iverdef->vd_cnt)
7344
		iverdaux->vda_nextptr = iverdaux + 1;
-
 
7345
	      else
-
 
7346
		iverdaux->vda_nextptr = NULL;
7993
		iverdaux->vda_nextptr = iverdaux + 1;
7347
 
7994
 
7348
	      if (iverdaux->vda_next
7995
	      if (iverdaux->vda_next
7349
		  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
7996
		  > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
7350
		goto error_return_verdef;
7997
		goto error_return_bad_verdef;
7351
 
7998
 
7352
	      everdaux = ((Elf_External_Verdaux *)
7999
	      everdaux = ((Elf_External_Verdaux *)
7353
			  ((bfd_byte *) everdaux + iverdaux->vda_next));
8000
			  ((bfd_byte *) everdaux + iverdaux->vda_next));
7354
	    }
8001
	    }
-
 
8002
 
7355
 
8003
	  iverdef->vd_nodename = NULL;
7356
	  if (iverdef->vd_cnt)
8004
	  if (iverdef->vd_cnt)
7357
	    iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
8005
	    iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
-
 
8006
 
-
 
8007
	  iverdef->vd_nextdef = NULL;
-
 
8008
	  if (iverdef->vd_next == 0)
7358
 
8009
	    break;
7359
	  if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
8010
	  if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
7360
	    iverdef->vd_nextdef = iverdef + 1;
-
 
7361
	  else
-
 
7362
	    iverdef->vd_nextdef = NULL;
8011
	    iverdef->vd_nextdef = iverdef + 1;
7363
 
8012
 
7364
	  everdef = ((Elf_External_Verdef *)
8013
	  everdef = ((Elf_External_Verdef *)
7365
		     ((bfd_byte *) everdef + iverdef->vd_next));
8014
		     ((bfd_byte *) everdef + iverdef->vd_next));
7366
	}
8015
	}
7367
 
8016
 
7368
      free (contents);
8017
      free (contents);
7369
      contents = NULL;
8018
      contents = NULL;
7370
    }
8019
    }
7371
  else if (default_imported_symver)
8020
  else if (default_imported_symver)
7372
    {
8021
    {
7373
      if (freeidx < 3)
8022
      if (freeidx < 3)
7374
	freeidx = 3;
8023
	freeidx = 3;
7375
      else
8024
      else
7376
	freeidx++;
8025
	freeidx++;
7377
 
8026
 
7378
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
8027
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *)
7379
          bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
8028
          bfd_zalloc2 (abfd, freeidx, sizeof (Elf_Internal_Verdef));
7380
      if (elf_tdata (abfd)->verdef == NULL)
8029
      if (elf_tdata (abfd)->verdef == NULL)
7381
	goto error_return;
8030
	goto error_return;
7382
 
8031
 
7383
      elf_tdata (abfd)->cverdefs = freeidx;
8032
      elf_tdata (abfd)->cverdefs = freeidx;
7384
    }
8033
    }
7385
 
8034
 
7386
  /* Create a default version based on the soname.  */
8035
  /* Create a default version based on the soname.  */
7387
  if (default_imported_symver)
8036
  if (default_imported_symver)
7388
    {
8037
    {
7389
      Elf_Internal_Verdef *iverdef;
8038
      Elf_Internal_Verdef *iverdef;
7390
      Elf_Internal_Verdaux *iverdaux;
8039
      Elf_Internal_Verdaux *iverdaux;
7391
 
8040
 
7392
      iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
8041
      iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
7393
 
8042
 
7394
      iverdef->vd_version = VER_DEF_CURRENT;
8043
      iverdef->vd_version = VER_DEF_CURRENT;
7395
      iverdef->vd_flags = 0;
8044
      iverdef->vd_flags = 0;
7396
      iverdef->vd_ndx = freeidx;
8045
      iverdef->vd_ndx = freeidx;
7397
      iverdef->vd_cnt = 1;
8046
      iverdef->vd_cnt = 1;
7398
 
8047
 
7399
      iverdef->vd_bfd = abfd;
8048
      iverdef->vd_bfd = abfd;
7400
 
8049
 
7401
      iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
8050
      iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
7402
      if (iverdef->vd_nodename == NULL)
8051
      if (iverdef->vd_nodename == NULL)
7403
	goto error_return_verdef;
8052
	goto error_return_verdef;
7404
      iverdef->vd_nextdef = NULL;
8053
      iverdef->vd_nextdef = NULL;
7405
      iverdef->vd_auxptr = (struct elf_internal_verdaux *)
8054
      iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
7406
          bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux));
8055
			    bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
7407
      if (iverdef->vd_auxptr == NULL)
8056
      if (iverdef->vd_auxptr == NULL)
7408
	goto error_return_verdef;
8057
	goto error_return_verdef;
7409
 
8058
 
7410
      iverdaux = iverdef->vd_auxptr;
8059
      iverdaux = iverdef->vd_auxptr;
7411
      iverdaux->vda_nodename = iverdef->vd_nodename;
8060
      iverdaux->vda_nodename = iverdef->vd_nodename;
7412
      iverdaux->vda_nextptr = NULL;
-
 
7413
    }
8061
    }
7414
 
8062
 
7415
  return TRUE;
8063
  return TRUE;
7416
 
8064
 
7417
 error_return:
8065
 error_return:
7418
  if (contents != NULL)
8066
  if (contents != NULL)
7419
    free (contents);
8067
    free (contents);
7420
  return FALSE;
8068
  return FALSE;
7421
}
8069
}
7422

8070

7423
asymbol *
8071
asymbol *
7424
_bfd_elf_make_empty_symbol (bfd *abfd)
8072
_bfd_elf_make_empty_symbol (bfd *abfd)
7425
{
8073
{
7426
  elf_symbol_type *newsym;
8074
  elf_symbol_type *newsym;
7427
  bfd_size_type amt = sizeof (elf_symbol_type);
-
 
7428
 
8075
 
7429
  newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
8076
  newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof * newsym);
7430
  if (!newsym)
8077
  if (!newsym)
7431
    return NULL;
-
 
7432
  else
-
 
7433
    {
8078
    return NULL;
7434
      newsym->symbol.the_bfd = abfd;
8079
  newsym->symbol.the_bfd = abfd;
7435
      return &newsym->symbol;
8080
  return &newsym->symbol;
7436
    }
8081
}
7437
}
-
 
7438
 
8082
 
7439
void
8083
void
7440
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
8084
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
7441
			  asymbol *symbol,
8085
			  asymbol *symbol,
7442
			  symbol_info *ret)
8086
			  symbol_info *ret)
7443
{
8087
{
7444
  bfd_symbol_info (symbol, ret);
8088
  bfd_symbol_info (symbol, ret);
7445
}
8089
}
7446
 
8090
 
7447
/* Return whether a symbol name implies a local symbol.  Most targets
8091
/* Return whether a symbol name implies a local symbol.  Most targets
7448
   use this function for the is_local_label_name entry point, but some
8092
   use this function for the is_local_label_name entry point, but some
7449
   override it.  */
8093
   override it.  */
7450
 
8094
 
7451
bfd_boolean
8095
bfd_boolean
7452
_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
8096
_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
7453
			      const char *name)
8097
			      const char *name)
7454
{
8098
{
7455
  /* Normal local symbols start with ``.L''.  */
8099
  /* Normal local symbols start with ``.L''.  */
7456
  if (name[0] == '.' && name[1] == 'L')
8100
  if (name[0] == '.' && name[1] == 'L')
7457
    return TRUE;
8101
    return TRUE;
7458
 
8102
 
7459
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
8103
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
7460
     DWARF debugging symbols starting with ``..''.  */
8104
     DWARF debugging symbols starting with ``..''.  */
7461
  if (name[0] == '.' && name[1] == '.')
8105
  if (name[0] == '.' && name[1] == '.')
7462
    return TRUE;
8106
    return TRUE;
7463
 
8107
 
7464
  /* gcc will sometimes generate symbols beginning with ``_.L_'' when
8108
  /* gcc will sometimes generate symbols beginning with ``_.L_'' when
7465
     emitting DWARF debugging output.  I suspect this is actually a
8109
     emitting DWARF debugging output.  I suspect this is actually a
7466
     small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
8110
     small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
7467
     ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
8111
     ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
7468
     underscore to be emitted on some ELF targets).  For ease of use,
8112
     underscore to be emitted on some ELF targets).  For ease of use,
7469
     we treat such symbols as local.  */
8113
     we treat such symbols as local.  */
7470
  if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
8114
  if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
7471
    return TRUE;
8115
    return TRUE;
-
 
8116
 
-
 
8117
  /* Treat assembler generated fake symbols, dollar local labels and
-
 
8118
     forward-backward labels (aka local labels) as locals.
-
 
8119
     These labels have the form:
-
 
8120
 
-
 
8121
       L0^A.*                                  (fake symbols)
-
 
8122
 
-
 
8123
       [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
-
 
8124
 
-
 
8125
     Versions which start with .L will have already been matched above,
-
 
8126
     so we only need to match the rest.  */
-
 
8127
  if (name[0] == 'L' && ISDIGIT (name[1]))
-
 
8128
    {
-
 
8129
      bfd_boolean ret = FALSE;
-
 
8130
      const char * p;
-
 
8131
      char c;
-
 
8132
 
-
 
8133
      for (p = name + 2; (c = *p); p++)
-
 
8134
	{
-
 
8135
	  if (c == 1 || c == 2)
-
 
8136
	    {
-
 
8137
	      if (c == 1 && p == name + 2)
-
 
8138
		/* A fake symbol.  */
-
 
8139
		return TRUE;
-
 
8140
 
-
 
8141
	      /* FIXME: We are being paranoid here and treating symbols like
-
 
8142
		 L0^Bfoo as if there were non-local, on the grounds that the
-
 
8143
		 assembler will never generate them.  But can any symbol
-
 
8144
		 containing an ASCII value in the range 1-31 ever be anything
-
 
8145
		 other than some kind of local ?  */
-
 
8146
	      ret = TRUE;
-
 
8147
	    }
-
 
8148
 
-
 
8149
	  if (! ISDIGIT (c))
-
 
8150
	    {
-
 
8151
	      ret = FALSE;
-
 
8152
	      break;
-
 
8153
	    }
-
 
8154
	}
-
 
8155
      return ret;
-
 
8156
    }
7472
 
8157
 
7473
  return FALSE;
8158
  return FALSE;
7474
}
8159
}
7475
 
8160
 
7476
alent *
8161
alent *
7477
_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
8162
_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
7478
		     asymbol *symbol ATTRIBUTE_UNUSED)
8163
		     asymbol *symbol ATTRIBUTE_UNUSED)
7479
{
8164
{
7480
  abort ();
8165
  abort ();
7481
  return NULL;
8166
  return NULL;
7482
}
8167
}
7483
 
8168
 
7484
bfd_boolean
8169
bfd_boolean
7485
_bfd_elf_set_arch_mach (bfd *abfd,
8170
_bfd_elf_set_arch_mach (bfd *abfd,
7486
			enum bfd_architecture arch,
8171
			enum bfd_architecture arch,
7487
			unsigned long machine)
8172
			unsigned long machine)
7488
{
8173
{
7489
  /* If this isn't the right architecture for this backend, and this
8174
  /* If this isn't the right architecture for this backend, and this
7490
     isn't the generic backend, fail.  */
8175
     isn't the generic backend, fail.  */
7491
  if (arch != get_elf_backend_data (abfd)->arch
8176
  if (arch != get_elf_backend_data (abfd)->arch
7492
      && arch != bfd_arch_unknown
8177
      && arch != bfd_arch_unknown
7493
      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
8178
      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
7494
    return FALSE;
8179
    return FALSE;
7495
 
8180
 
7496
  return bfd_default_set_arch_mach (abfd, arch, machine);
8181
  return bfd_default_set_arch_mach (abfd, arch, machine);
7497
}
8182
}
7498
 
-
 
7499
/* Find the function to a particular section and offset,
-
 
7500
   for error reporting.  */
-
 
7501
 
-
 
7502
static bfd_boolean
-
 
7503
elf_find_function (bfd *abfd,
-
 
7504
		   asection *section,
-
 
7505
		   asymbol **symbols,
-
 
7506
		   bfd_vma offset,
-
 
7507
		   const char **filename_ptr,
-
 
7508
		   const char **functionname_ptr)
-
 
7509
{
-
 
7510
  struct elf_find_function_cache
-
 
7511
  {
-
 
7512
    asection *last_section;
-
 
7513
    asymbol *func;
-
 
7514
    const char *filename;
-
 
7515
    bfd_size_type func_size;
-
 
7516
  } *cache;
-
 
7517
 
-
 
7518
  if (symbols == NULL)
-
 
7519
    return FALSE;
-
 
7520
 
-
 
7521
  cache = elf_tdata (abfd)->elf_find_function_cache;
-
 
7522
  if (cache == NULL)
-
 
7523
    {
-
 
7524
      cache = bfd_zalloc (abfd, sizeof (*cache));
-
 
7525
      elf_tdata (abfd)->elf_find_function_cache = cache;
-
 
7526
      if (cache == NULL)
-
 
7527
	return FALSE;
-
 
7528
    }
-
 
7529
  if (cache->last_section != section
-
 
7530
      || cache->func == NULL
-
 
7531
      || offset < cache->func->value
-
 
7532
      || offset >= cache->func->value + cache->func_size)
-
 
7533
    {
-
 
7534
      asymbol *file;
-
 
7535
      bfd_vma low_func;
-
 
7536
      asymbol **p;
-
 
7537
      /* ??? Given multiple file symbols, it is impossible to reliably
-
 
7538
	 choose the right file name for global symbols.  File symbols are
-
 
7539
	 local symbols, and thus all file symbols must sort before any
-
 
7540
	 global symbols.  The ELF spec may be interpreted to say that a
-
 
7541
	 file symbol must sort before other local symbols, but currently
-
 
7542
	 ld -r doesn't do this.  So, for ld -r output, it is possible to
-
 
7543
	 make a better choice of file name for local symbols by ignoring
-
 
7544
	 file symbols appearing after a given local symbol.  */
-
 
7545
      enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
-
 
7546
      const struct elf_backend_data *bed = get_elf_backend_data (abfd);
-
 
7547
 
-
 
7548
      file = NULL;
-
 
7549
      low_func = 0;
-
 
7550
      state = nothing_seen;
-
 
7551
      cache->filename = NULL;
-
 
7552
      cache->func = NULL;
-
 
7553
      cache->func_size = 0;
-
 
7554
      cache->last_section = section;
-
 
7555
 
-
 
7556
      for (p = symbols; *p != NULL; p++)
-
 
7557
	{
-
 
7558
	  asymbol *sym = *p;
-
 
7559
	  bfd_vma code_off;
-
 
7560
	  bfd_size_type size;
-
 
7561
 
-
 
7562
	  if ((sym->flags & BSF_FILE) != 0)
-
 
7563
	    {
-
 
7564
	      file = sym;
-
 
7565
	      if (state == symbol_seen)
-
 
7566
		state = file_after_symbol_seen;
-
 
7567
	      continue;
-
 
7568
	    }
-
 
7569
 
-
 
7570
	  size = bed->maybe_function_sym (sym, section, &code_off);
-
 
7571
	  if (size != 0
-
 
7572
	      && code_off <= offset
-
 
7573
	      && (code_off > low_func
-
 
7574
		  || (code_off == low_func
-
 
7575
		      && size > cache->func_size)))
-
 
7576
	    {
-
 
7577
	      cache->func = sym;
-
 
7578
	      cache->func_size = size;
-
 
7579
	      cache->filename = NULL;
-
 
7580
	      low_func = code_off;
-
 
7581
	      if (file != NULL
-
 
7582
		  && ((sym->flags & BSF_LOCAL) != 0
-
 
7583
		      || state != file_after_symbol_seen))
-
 
7584
		cache->filename = bfd_asymbol_name (file);
-
 
7585
	    }
-
 
7586
	  if (state == nothing_seen)
-
 
7587
	    state = symbol_seen;
-
 
7588
	}
-
 
7589
    }
-
 
7590
 
-
 
7591
  if (cache->func == NULL)
-
 
7592
    return FALSE;
-
 
7593
 
-
 
7594
  if (filename_ptr)
-
 
7595
    *filename_ptr = cache->filename;
-
 
7596
  if (functionname_ptr)
-
 
7597
    *functionname_ptr = bfd_asymbol_name (cache->func);
-
 
7598
 
-
 
7599
  return TRUE;
-
 
7600
}
-
 
7601
 
8183
 
7602
/* Find the nearest line to a particular section and offset,
8184
/* Find the nearest line to a particular section and offset,
7603
   for error reporting.  */
8185
   for error reporting.  */
7604
 
8186
 
7605
bfd_boolean
8187
bfd_boolean
7606
_bfd_elf_find_nearest_line (bfd *abfd,
8188
_bfd_elf_find_nearest_line (bfd *abfd,
7607
			    asection *section,
-
 
7608
			    asymbol **symbols,
8189
			    asymbol **symbols,
7609
			    bfd_vma offset,
-
 
7610
			    const char **filename_ptr,
-
 
7611
			    const char **functionname_ptr,
-
 
7612
			    unsigned int *line_ptr)
-
 
7613
{
-
 
7614
  return _bfd_elf_find_nearest_line_discriminator (abfd, section, symbols,
-
 
7615
                                                   offset, filename_ptr,
-
 
7616
                                                   functionname_ptr,
-
 
7617
                                                   line_ptr,
-
 
7618
                                                   NULL);
-
 
7619
}
-
 
7620
 
-
 
7621
bfd_boolean
-
 
7622
_bfd_elf_find_nearest_line_discriminator (bfd *abfd,
-
 
7623
                                          asection *section,
8190
			    asection *section,
7624
                                          asymbol **symbols,
-
 
7625
                                          bfd_vma offset,
8191
			    bfd_vma offset,
7626
                                          const char **filename_ptr,
8192
			    const char **filename_ptr,
7627
                                          const char **functionname_ptr,
8193
			    const char **functionname_ptr,
7628
                                          unsigned int *line_ptr,
8194
			    unsigned int *line_ptr,
7629
                                          unsigned int *discriminator_ptr)
8195
			    unsigned int *discriminator_ptr)
7630
{
8196
{
7631
  bfd_boolean found;
8197
  bfd_boolean found;
7632
 
8198
 
7633
  if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
8199
  if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
7634
				     filename_ptr, functionname_ptr,
8200
				     filename_ptr, functionname_ptr,
7635
				     line_ptr))
-
 
7636
    {
8201
				     line_ptr, discriminator_ptr,
7637
      if (!*functionname_ptr)
-
 
7638
	elf_find_function (abfd, section, symbols, offset,
8202
				     dwarf_debug_sections, 0,
7639
			   *filename_ptr ? NULL : filename_ptr,
-
 
7640
			   functionname_ptr);
-
 
7641
 
-
 
7642
      return TRUE;
-
 
7643
    }
-
 
7644
 
8203
				     &elf_tdata (abfd)->dwarf2_find_line_info)
7645
  if (_bfd_dwarf2_find_nearest_line (abfd, dwarf_debug_sections,
-
 
7646
                                     section, symbols, offset,
8204
      || _bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
7647
				     filename_ptr, functionname_ptr,
8205
					filename_ptr, functionname_ptr,
7648
				     line_ptr, discriminator_ptr, 0,
-
 
7649
				     &elf_tdata (abfd)->dwarf2_find_line_info))
8206
					line_ptr))
7650
    {
8207
    {
7651
      if (!*functionname_ptr)
8208
      if (!*functionname_ptr)
7652
	elf_find_function (abfd, section, symbols, offset,
8209
	_bfd_elf_find_function (abfd, symbols, section, offset,
7653
			   *filename_ptr ? NULL : filename_ptr,
8210
				*filename_ptr ? NULL : filename_ptr,
7654
			   functionname_ptr);
-
 
7655
 
8211
				functionname_ptr);
7656
      return TRUE;
8212
      return TRUE;
7657
    }
8213
    }
7658
 
8214
 
7659
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
8215
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
7660
					     &found, filename_ptr,
8216
					     &found, filename_ptr,
7661
					     functionname_ptr, line_ptr,
8217
					     functionname_ptr, line_ptr,
7662
					     &elf_tdata (abfd)->line_info))
8218
					     &elf_tdata (abfd)->line_info))
7663
    return FALSE;
8219
    return FALSE;
7664
  if (found && (*functionname_ptr || *line_ptr))
8220
  if (found && (*functionname_ptr || *line_ptr))
7665
    return TRUE;
8221
    return TRUE;
7666
 
8222
 
7667
  if (symbols == NULL)
8223
  if (symbols == NULL)
7668
    return FALSE;
8224
    return FALSE;
7669
 
8225
 
7670
  if (! elf_find_function (abfd, section, symbols, offset,
8226
  if (! _bfd_elf_find_function (abfd, symbols, section, offset,
7671
			   filename_ptr, functionname_ptr))
8227
				filename_ptr, functionname_ptr))
7672
    return FALSE;
8228
    return FALSE;
7673
 
8229
 
7674
  *line_ptr = 0;
8230
  *line_ptr = 0;
7675
  return TRUE;
8231
  return TRUE;
7676
}
8232
}
7677
 
8233
 
7678
/* Find the line for a symbol.  */
8234
/* Find the line for a symbol.  */
7679
 
8235
 
7680
bfd_boolean
8236
bfd_boolean
7681
_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
8237
_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
7682
		    const char **filename_ptr, unsigned int *line_ptr)
8238
		    const char **filename_ptr, unsigned int *line_ptr)
7683
{
8239
{
7684
  return _bfd_elf_find_line_discriminator (abfd, symbols, symbol,
8240
  return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
7685
		                           filename_ptr, line_ptr,
8241
					filename_ptr, NULL, line_ptr, NULL,
7686
                                           NULL);
-
 
7687
}
-
 
7688
 
-
 
7689
bfd_boolean
-
 
7690
_bfd_elf_find_line_discriminator (bfd *abfd, asymbol **symbols, asymbol *symbol,
-
 
7691
                                  const char **filename_ptr,
-
 
7692
                                  unsigned int *line_ptr,
-
 
7693
                                  unsigned int *discriminator_ptr)
-
 
7694
{
-
 
7695
  return _bfd_dwarf2_find_line (abfd, symbols, symbol,
-
 
7696
				filename_ptr, line_ptr, discriminator_ptr, 0,
8242
					dwarf_debug_sections, 0,
7697
				&elf_tdata (abfd)->dwarf2_find_line_info);
8243
					&elf_tdata (abfd)->dwarf2_find_line_info);
7698
}
8244
}
7699
 
8245
 
7700
/* After a call to bfd_find_nearest_line, successive calls to
8246
/* After a call to bfd_find_nearest_line, successive calls to
7701
   bfd_find_inliner_info can be used to get source information about
8247
   bfd_find_inliner_info can be used to get source information about
7702
   each level of function inlining that terminated at the address
8248
   each level of function inlining that terminated at the address
7703
   passed to bfd_find_nearest_line.  Currently this is only supported
8249
   passed to bfd_find_nearest_line.  Currently this is only supported
7704
   for DWARF2 with appropriate DWARF3 extensions. */
8250
   for DWARF2 with appropriate DWARF3 extensions. */
7705
 
8251
 
7706
bfd_boolean
8252
bfd_boolean
7707
_bfd_elf_find_inliner_info (bfd *abfd,
8253
_bfd_elf_find_inliner_info (bfd *abfd,
7708
			    const char **filename_ptr,
8254
			    const char **filename_ptr,
7709
			    const char **functionname_ptr,
8255
			    const char **functionname_ptr,
7710
			    unsigned int *line_ptr)
8256
			    unsigned int *line_ptr)
7711
{
8257
{
7712
  bfd_boolean found;
8258
  bfd_boolean found;
7713
  found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
8259
  found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
7714
					 functionname_ptr, line_ptr,
8260
					 functionname_ptr, line_ptr,
7715
					 & elf_tdata (abfd)->dwarf2_find_line_info);
8261
					 & elf_tdata (abfd)->dwarf2_find_line_info);
7716
  return found;
8262
  return found;
7717
}
8263
}
7718
 
8264
 
7719
int
8265
int
7720
_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
8266
_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
7721
{
8267
{
7722
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8268
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
7723
  int ret = bed->s->sizeof_ehdr;
8269
  int ret = bed->s->sizeof_ehdr;
7724
 
8270
 
7725
  if (!info->relocatable)
8271
  if (!bfd_link_relocatable (info))
7726
    {
8272
    {
7727
      bfd_size_type phdr_size = elf_program_header_size (abfd);
8273
      bfd_size_type phdr_size = elf_program_header_size (abfd);
7728
 
8274
 
7729
      if (phdr_size == (bfd_size_type) -1)
8275
      if (phdr_size == (bfd_size_type) -1)
7730
	{
8276
	{
7731
	  struct elf_segment_map *m;
8277
	  struct elf_segment_map *m;
7732
 
8278
 
7733
	  phdr_size = 0;
8279
	  phdr_size = 0;
7734
	  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
8280
	  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
7735
	    phdr_size += bed->s->sizeof_phdr;
8281
	    phdr_size += bed->s->sizeof_phdr;
7736
 
8282
 
7737
	  if (phdr_size == 0)
8283
	  if (phdr_size == 0)
7738
	    phdr_size = get_program_header_size (abfd, info);
8284
	    phdr_size = get_program_header_size (abfd, info);
7739
	}
8285
	}
7740
 
8286
 
7741
      elf_program_header_size (abfd) = phdr_size;
8287
      elf_program_header_size (abfd) = phdr_size;
7742
      ret += phdr_size;
8288
      ret += phdr_size;
7743
    }
8289
    }
7744
 
8290
 
7745
  return ret;
8291
  return ret;
7746
}
8292
}
7747
 
8293
 
7748
bfd_boolean
8294
bfd_boolean
7749
_bfd_elf_set_section_contents (bfd *abfd,
8295
_bfd_elf_set_section_contents (bfd *abfd,
7750
			       sec_ptr section,
8296
			       sec_ptr section,
7751
			       const void *location,
8297
			       const void *location,
7752
			       file_ptr offset,
8298
			       file_ptr offset,
7753
			       bfd_size_type count)
8299
			       bfd_size_type count)
7754
{
8300
{
7755
  Elf_Internal_Shdr *hdr;
8301
  Elf_Internal_Shdr *hdr;
7756
  bfd_signed_vma pos;
8302
  file_ptr pos;
7757
 
8303
 
7758
  if (! abfd->output_has_begun
8304
  if (! abfd->output_has_begun
7759
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
8305
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7760
    return FALSE;
8306
    return FALSE;
-
 
8307
 
-
 
8308
  if (!count)
-
 
8309
    return TRUE;
7761
 
8310
 
-
 
8311
  hdr = &elf_section_data (section)->this_hdr;
-
 
8312
  if (hdr->sh_offset == (file_ptr) -1)
-
 
8313
    {
-
 
8314
      /* We must compress this section.  Write output to the buffer.  */
-
 
8315
      unsigned char *contents = hdr->contents;
-
 
8316
      if ((offset + count) > hdr->sh_size
-
 
8317
	  || (section->flags & SEC_ELF_COMPRESS) == 0
-
 
8318
	  || contents == NULL)
-
 
8319
	abort ();
-
 
8320
      memcpy (contents + offset, location, count);
-
 
8321
      return TRUE;
7762
  hdr = &elf_section_data (section)->this_hdr;
8322
    }
7763
  pos = hdr->sh_offset + offset;
8323
  pos = hdr->sh_offset + offset;
7764
  if (bfd_seek (abfd, pos, SEEK_SET) != 0
8324
  if (bfd_seek (abfd, pos, SEEK_SET) != 0
7765
      || bfd_bwrite (location, count, abfd) != count)
8325
      || bfd_bwrite (location, count, abfd) != count)
7766
    return FALSE;
8326
    return FALSE;
7767
 
8327
 
7768
  return TRUE;
8328
  return TRUE;
7769
}
8329
}
7770
 
8330
 
7771
void
8331
void
7772
_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
8332
_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
7773
			   arelent *cache_ptr ATTRIBUTE_UNUSED,
8333
			   arelent *cache_ptr ATTRIBUTE_UNUSED,
7774
			   Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
8334
			   Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
7775
{
8335
{
7776
  abort ();
8336
  abort ();
7777
}
8337
}
7778
 
8338
 
7779
/* Try to convert a non-ELF reloc into an ELF one.  */
8339
/* Try to convert a non-ELF reloc into an ELF one.  */
7780
 
8340
 
7781
bfd_boolean
8341
bfd_boolean
7782
_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
8342
_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
7783
{
8343
{
7784
  /* Check whether we really have an ELF howto.  */
8344
  /* Check whether we really have an ELF howto.  */
7785
 
8345
 
7786
  if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
8346
  if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
7787
    {
8347
    {
7788
      bfd_reloc_code_real_type code;
8348
      bfd_reloc_code_real_type code;
7789
      reloc_howto_type *howto;
8349
      reloc_howto_type *howto;
7790
 
8350
 
7791
      /* Alien reloc: Try to determine its type to replace it with an
8351
      /* Alien reloc: Try to determine its type to replace it with an
7792
	 equivalent ELF reloc.  */
8352
	 equivalent ELF reloc.  */
7793
 
8353
 
7794
      if (areloc->howto->pc_relative)
8354
      if (areloc->howto->pc_relative)
7795
	{
8355
	{
7796
	  switch (areloc->howto->bitsize)
8356
	  switch (areloc->howto->bitsize)
7797
	    {
8357
	    {
7798
	    case 8:
8358
	    case 8:
7799
	      code = BFD_RELOC_8_PCREL;
8359
	      code = BFD_RELOC_8_PCREL;
7800
	      break;
8360
	      break;
7801
	    case 12:
8361
	    case 12:
7802
	      code = BFD_RELOC_12_PCREL;
8362
	      code = BFD_RELOC_12_PCREL;
7803
	      break;
8363
	      break;
7804
	    case 16:
8364
	    case 16:
7805
	      code = BFD_RELOC_16_PCREL;
8365
	      code = BFD_RELOC_16_PCREL;
7806
	      break;
8366
	      break;
7807
	    case 24:
8367
	    case 24:
7808
	      code = BFD_RELOC_24_PCREL;
8368
	      code = BFD_RELOC_24_PCREL;
7809
	      break;
8369
	      break;
7810
	    case 32:
8370
	    case 32:
7811
	      code = BFD_RELOC_32_PCREL;
8371
	      code = BFD_RELOC_32_PCREL;
7812
	      break;
8372
	      break;
7813
	    case 64:
8373
	    case 64:
7814
	      code = BFD_RELOC_64_PCREL;
8374
	      code = BFD_RELOC_64_PCREL;
7815
	      break;
8375
	      break;
7816
	    default:
8376
	    default:
7817
	      goto fail;
8377
	      goto fail;
7818
	    }
8378
	    }
7819
 
8379
 
7820
	  howto = bfd_reloc_type_lookup (abfd, code);
8380
	  howto = bfd_reloc_type_lookup (abfd, code);
7821
 
8381
 
7822
	  if (areloc->howto->pcrel_offset != howto->pcrel_offset)
8382
	  if (areloc->howto->pcrel_offset != howto->pcrel_offset)
7823
	    {
8383
	    {
7824
	      if (howto->pcrel_offset)
8384
	      if (howto->pcrel_offset)
7825
		areloc->addend += areloc->address;
8385
		areloc->addend += areloc->address;
7826
	      else
8386
	      else
7827
		areloc->addend -= areloc->address; /* addend is unsigned!! */
8387
		areloc->addend -= areloc->address; /* addend is unsigned!! */
7828
	    }
8388
	    }
7829
	}
8389
	}
7830
      else
8390
      else
7831
	{
8391
	{
7832
	  switch (areloc->howto->bitsize)
8392
	  switch (areloc->howto->bitsize)
7833
	    {
8393
	    {
7834
	    case 8:
8394
	    case 8:
7835
	      code = BFD_RELOC_8;
8395
	      code = BFD_RELOC_8;
7836
	      break;
8396
	      break;
7837
	    case 14:
8397
	    case 14:
7838
	      code = BFD_RELOC_14;
8398
	      code = BFD_RELOC_14;
7839
	      break;
8399
	      break;
7840
	    case 16:
8400
	    case 16:
7841
	      code = BFD_RELOC_16;
8401
	      code = BFD_RELOC_16;
7842
	      break;
8402
	      break;
7843
	    case 26:
8403
	    case 26:
7844
	      code = BFD_RELOC_26;
8404
	      code = BFD_RELOC_26;
7845
	      break;
8405
	      break;
7846
	    case 32:
8406
	    case 32:
7847
	      code = BFD_RELOC_32;
8407
	      code = BFD_RELOC_32;
7848
	      break;
8408
	      break;
7849
	    case 64:
8409
	    case 64:
7850
	      code = BFD_RELOC_64;
8410
	      code = BFD_RELOC_64;
7851
	      break;
8411
	      break;
7852
	    default:
8412
	    default:
7853
	      goto fail;
8413
	      goto fail;
7854
	    }
8414
	    }
7855
 
8415
 
7856
	  howto = bfd_reloc_type_lookup (abfd, code);
8416
	  howto = bfd_reloc_type_lookup (abfd, code);
7857
	}
8417
	}
7858
 
8418
 
7859
      if (howto)
8419
      if (howto)
7860
	areloc->howto = howto;
8420
	areloc->howto = howto;
7861
      else
8421
      else
7862
	goto fail;
8422
	goto fail;
7863
    }
8423
    }
7864
 
8424
 
7865
  return TRUE;
8425
  return TRUE;
7866
 
8426
 
7867
 fail:
8427
 fail:
7868
  (*_bfd_error_handler)
8428
  (*_bfd_error_handler)
7869
    (_("%B: unsupported relocation type %s"),
8429
    (_("%B: unsupported relocation type %s"),
7870
     abfd, areloc->howto->name);
8430
     abfd, areloc->howto->name);
7871
  bfd_set_error (bfd_error_bad_value);
8431
  bfd_set_error (bfd_error_bad_value);
7872
  return FALSE;
8432
  return FALSE;
7873
}
8433
}
7874
 
8434
 
7875
bfd_boolean
8435
bfd_boolean
7876
_bfd_elf_close_and_cleanup (bfd *abfd)
8436
_bfd_elf_close_and_cleanup (bfd *abfd)
7877
{
8437
{
7878
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
8438
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
7879
  if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
8439
  if (bfd_get_format (abfd) == bfd_object && tdata != NULL)
7880
    {
8440
    {
7881
      if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
8441
      if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
7882
	_bfd_elf_strtab_free (elf_shstrtab (abfd));
8442
	_bfd_elf_strtab_free (elf_shstrtab (abfd));
7883
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
8443
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
7884
    }
8444
    }
7885
 
8445
 
7886
  return _bfd_generic_close_and_cleanup (abfd);
8446
  return _bfd_generic_close_and_cleanup (abfd);
7887
}
8447
}
7888
 
8448
 
7889
/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
8449
/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
7890
   in the relocation's offset.  Thus we cannot allow any sort of sanity
8450
   in the relocation's offset.  Thus we cannot allow any sort of sanity
7891
   range-checking to interfere.  There is nothing else to do in processing
8451
   range-checking to interfere.  There is nothing else to do in processing
7892
   this reloc.  */
8452
   this reloc.  */
7893
 
8453
 
7894
bfd_reloc_status_type
8454
bfd_reloc_status_type
7895
_bfd_elf_rel_vtable_reloc_fn
8455
_bfd_elf_rel_vtable_reloc_fn
7896
  (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
8456
  (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
7897
   struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
8457
   struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
7898
   void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
8458
   void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
7899
   bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
8459
   bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
7900
{
8460
{
7901
  return bfd_reloc_ok;
8461
  return bfd_reloc_ok;
7902
}
8462
}
7903

8463

7904
/* Elf core file support.  Much of this only works on native
8464
/* Elf core file support.  Much of this only works on native
7905
   toolchains, since we rely on knowing the
8465
   toolchains, since we rely on knowing the
7906
   machine-dependent procfs structure in order to pick
8466
   machine-dependent procfs structure in order to pick
7907
   out details about the corefile.  */
8467
   out details about the corefile.  */
7908
 
8468
 
7909
#ifdef HAVE_SYS_PROCFS_H
8469
#ifdef HAVE_SYS_PROCFS_H
7910
/* Needed for new procfs interface on sparc-solaris.  */
8470
/* Needed for new procfs interface on sparc-solaris.  */
7911
# define _STRUCTURED_PROC 1
8471
# define _STRUCTURED_PROC 1
7912
# include 
8472
# include 
7913
#endif
8473
#endif
7914
 
8474
 
7915
/* Return a PID that identifies a "thread" for threaded cores, or the
8475
/* Return a PID that identifies a "thread" for threaded cores, or the
7916
   PID of the main process for non-threaded cores.  */
8476
   PID of the main process for non-threaded cores.  */
7917
 
8477
 
7918
static int
8478
static int
7919
elfcore_make_pid (bfd *abfd)
8479
elfcore_make_pid (bfd *abfd)
7920
{
8480
{
7921
  int pid;
8481
  int pid;
7922
 
8482
 
7923
  pid = elf_tdata (abfd)->core->lwpid;
8483
  pid = elf_tdata (abfd)->core->lwpid;
7924
  if (pid == 0)
8484
  if (pid == 0)
7925
    pid = elf_tdata (abfd)->core->pid;
8485
    pid = elf_tdata (abfd)->core->pid;
7926
 
8486
 
7927
  return pid;
8487
  return pid;
7928
}
8488
}
7929
 
8489
 
7930
/* If there isn't a section called NAME, make one, using
8490
/* If there isn't a section called NAME, make one, using
7931
   data from SECT.  Note, this function will generate a
8491
   data from SECT.  Note, this function will generate a
7932
   reference to NAME, so you shouldn't deallocate or
8492
   reference to NAME, so you shouldn't deallocate or
7933
   overwrite it.  */
8493
   overwrite it.  */
7934
 
8494
 
7935
static bfd_boolean
8495
static bfd_boolean
7936
elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
8496
elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
7937
{
8497
{
7938
  asection *sect2;
8498
  asection *sect2;
7939
 
8499
 
7940
  if (bfd_get_section_by_name (abfd, name) != NULL)
8500
  if (bfd_get_section_by_name (abfd, name) != NULL)
7941
    return TRUE;
8501
    return TRUE;
7942
 
8502
 
7943
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
8503
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
7944
  if (sect2 == NULL)
8504
  if (sect2 == NULL)
7945
    return FALSE;
8505
    return FALSE;
7946
 
8506
 
7947
  sect2->size = sect->size;
8507
  sect2->size = sect->size;
7948
  sect2->filepos = sect->filepos;
8508
  sect2->filepos = sect->filepos;
7949
  sect2->alignment_power = sect->alignment_power;
8509
  sect2->alignment_power = sect->alignment_power;
7950
  return TRUE;
8510
  return TRUE;
7951
}
8511
}
7952
 
8512
 
7953
/* Create a pseudosection containing SIZE bytes at FILEPOS.  This
8513
/* Create a pseudosection containing SIZE bytes at FILEPOS.  This
7954
   actually creates up to two pseudosections:
8514
   actually creates up to two pseudosections:
7955
   - For the single-threaded case, a section named NAME, unless
8515
   - For the single-threaded case, a section named NAME, unless
7956
     such a section already exists.
8516
     such a section already exists.
7957
   - For the multi-threaded case, a section named "NAME/PID", where
8517
   - For the multi-threaded case, a section named "NAME/PID", where
7958
     PID is elfcore_make_pid (abfd).
8518
     PID is elfcore_make_pid (abfd).
7959
   Both pseudosections have identical contents. */
8519
   Both pseudosections have identical contents. */
7960
bfd_boolean
8520
bfd_boolean
7961
_bfd_elfcore_make_pseudosection (bfd *abfd,
8521
_bfd_elfcore_make_pseudosection (bfd *abfd,
7962
				 char *name,
8522
				 char *name,
7963
				 size_t size,
8523
				 size_t size,
7964
				 ufile_ptr filepos)
8524
				 ufile_ptr filepos)
7965
{
8525
{
7966
  char buf[100];
8526
  char buf[100];
7967
  char *threaded_name;
8527
  char *threaded_name;
7968
  size_t len;
8528
  size_t len;
7969
  asection *sect;
8529
  asection *sect;
7970
 
8530
 
7971
  /* Build the section name.  */
8531
  /* Build the section name.  */
7972
 
8532
 
7973
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
8533
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
7974
  len = strlen (buf) + 1;
8534
  len = strlen (buf) + 1;
7975
  threaded_name = (char *) bfd_alloc (abfd, len);
8535
  threaded_name = (char *) bfd_alloc (abfd, len);
7976
  if (threaded_name == NULL)
8536
  if (threaded_name == NULL)
7977
    return FALSE;
8537
    return FALSE;
7978
  memcpy (threaded_name, buf, len);
8538
  memcpy (threaded_name, buf, len);
7979
 
8539
 
7980
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
8540
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
7981
					     SEC_HAS_CONTENTS);
8541
					     SEC_HAS_CONTENTS);
7982
  if (sect == NULL)
8542
  if (sect == NULL)
7983
    return FALSE;
8543
    return FALSE;
7984
  sect->size = size;
8544
  sect->size = size;
7985
  sect->filepos = filepos;
8545
  sect->filepos = filepos;
7986
  sect->alignment_power = 2;
8546
  sect->alignment_power = 2;
7987
 
8547
 
7988
  return elfcore_maybe_make_sect (abfd, name, sect);
8548
  return elfcore_maybe_make_sect (abfd, name, sect);
7989
}
8549
}
7990
 
8550
 
7991
/* prstatus_t exists on:
8551
/* prstatus_t exists on:
7992
     solaris 2.5+
8552
     solaris 2.5+
7993
     linux 2.[01] + glibc
8553
     linux 2.[01] + glibc
7994
     unixware 4.2
8554
     unixware 4.2
7995
*/
8555
*/
7996
 
8556
 
7997
#if defined (HAVE_PRSTATUS_T)
8557
#if defined (HAVE_PRSTATUS_T)
7998
 
8558
 
7999
static bfd_boolean
8559
static bfd_boolean
8000
elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
8560
elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
8001
{
8561
{
8002
  size_t size;
8562
  size_t size;
8003
  int offset;
8563
  int offset;
8004
 
8564
 
8005
  if (note->descsz == sizeof (prstatus_t))
8565
  if (note->descsz == sizeof (prstatus_t))
8006
    {
8566
    {
8007
      prstatus_t prstat;
8567
      prstatus_t prstat;
8008
 
8568
 
8009
      size = sizeof (prstat.pr_reg);
8569
      size = sizeof (prstat.pr_reg);
8010
      offset   = offsetof (prstatus_t, pr_reg);
8570
      offset   = offsetof (prstatus_t, pr_reg);
8011
      memcpy (&prstat, note->descdata, sizeof (prstat));
8571
      memcpy (&prstat, note->descdata, sizeof (prstat));
8012
 
8572
 
8013
      /* Do not overwrite the core signal if it
8573
      /* Do not overwrite the core signal if it
8014
	 has already been set by another thread.  */
8574
	 has already been set by another thread.  */
8015
      if (elf_tdata (abfd)->core->signal == 0)
8575
      if (elf_tdata (abfd)->core->signal == 0)
8016
	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8576
	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8017
      if (elf_tdata (abfd)->core->pid == 0)
8577
      if (elf_tdata (abfd)->core->pid == 0)
8018
	elf_tdata (abfd)->core->pid = prstat.pr_pid;
8578
	elf_tdata (abfd)->core->pid = prstat.pr_pid;
8019
 
8579
 
8020
      /* pr_who exists on:
8580
      /* pr_who exists on:
8021
	 solaris 2.5+
8581
	 solaris 2.5+
8022
	 unixware 4.2
8582
	 unixware 4.2
8023
	 pr_who doesn't exist on:
8583
	 pr_who doesn't exist on:
8024
	 linux 2.[01]
8584
	 linux 2.[01]
8025
	 */
8585
	 */
8026
#if defined (HAVE_PRSTATUS_T_PR_WHO)
8586
#if defined (HAVE_PRSTATUS_T_PR_WHO)
8027
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8587
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8028
#else
8588
#else
8029
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8589
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8030
#endif
8590
#endif
8031
    }
8591
    }
8032
#if defined (HAVE_PRSTATUS32_T)
8592
#if defined (HAVE_PRSTATUS32_T)
8033
  else if (note->descsz == sizeof (prstatus32_t))
8593
  else if (note->descsz == sizeof (prstatus32_t))
8034
    {
8594
    {
8035
      /* 64-bit host, 32-bit corefile */
8595
      /* 64-bit host, 32-bit corefile */
8036
      prstatus32_t prstat;
8596
      prstatus32_t prstat;
8037
 
8597
 
8038
      size = sizeof (prstat.pr_reg);
8598
      size = sizeof (prstat.pr_reg);
8039
      offset   = offsetof (prstatus32_t, pr_reg);
8599
      offset   = offsetof (prstatus32_t, pr_reg);
8040
      memcpy (&prstat, note->descdata, sizeof (prstat));
8600
      memcpy (&prstat, note->descdata, sizeof (prstat));
8041
 
8601
 
8042
      /* Do not overwrite the core signal if it
8602
      /* Do not overwrite the core signal if it
8043
	 has already been set by another thread.  */
8603
	 has already been set by another thread.  */
8044
      if (elf_tdata (abfd)->core->signal == 0)
8604
      if (elf_tdata (abfd)->core->signal == 0)
8045
	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8605
	elf_tdata (abfd)->core->signal = prstat.pr_cursig;
8046
      if (elf_tdata (abfd)->core->pid == 0)
8606
      if (elf_tdata (abfd)->core->pid == 0)
8047
	elf_tdata (abfd)->core->pid = prstat.pr_pid;
8607
	elf_tdata (abfd)->core->pid = prstat.pr_pid;
8048
 
8608
 
8049
      /* pr_who exists on:
8609
      /* pr_who exists on:
8050
	 solaris 2.5+
8610
	 solaris 2.5+
8051
	 unixware 4.2
8611
	 unixware 4.2
8052
	 pr_who doesn't exist on:
8612
	 pr_who doesn't exist on:
8053
	 linux 2.[01]
8613
	 linux 2.[01]
8054
	 */
8614
	 */
8055
#if defined (HAVE_PRSTATUS32_T_PR_WHO)
8615
#if defined (HAVE_PRSTATUS32_T_PR_WHO)
8056
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8616
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
8057
#else
8617
#else
8058
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8618
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
8059
#endif
8619
#endif
8060
    }
8620
    }
8061
#endif /* HAVE_PRSTATUS32_T */
8621
#endif /* HAVE_PRSTATUS32_T */
8062
  else
8622
  else
8063
    {
8623
    {
8064
      /* Fail - we don't know how to handle any other
8624
      /* Fail - we don't know how to handle any other
8065
	 note size (ie. data object type).  */
8625
	 note size (ie. data object type).  */
8066
      return TRUE;
8626
      return TRUE;
8067
    }
8627
    }
8068
 
8628
 
8069
  /* Make a ".reg/999" section and a ".reg" section.  */
8629
  /* Make a ".reg/999" section and a ".reg" section.  */
8070
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
8630
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
8071
					  size, note->descpos + offset);
8631
					  size, note->descpos + offset);
8072
}
8632
}
8073
#endif /* defined (HAVE_PRSTATUS_T) */
8633
#endif /* defined (HAVE_PRSTATUS_T) */
8074
 
8634
 
8075
/* Create a pseudosection containing the exact contents of NOTE.  */
8635
/* Create a pseudosection containing the exact contents of NOTE.  */
8076
static bfd_boolean
8636
static bfd_boolean
8077
elfcore_make_note_pseudosection (bfd *abfd,
8637
elfcore_make_note_pseudosection (bfd *abfd,
8078
				 char *name,
8638
				 char *name,
8079
				 Elf_Internal_Note *note)
8639
				 Elf_Internal_Note *note)
8080
{
8640
{
8081
  return _bfd_elfcore_make_pseudosection (abfd, name,
8641
  return _bfd_elfcore_make_pseudosection (abfd, name,
8082
					  note->descsz, note->descpos);
8642
					  note->descsz, note->descpos);
8083
}
8643
}
8084
 
8644
 
8085
/* There isn't a consistent prfpregset_t across platforms,
8645
/* There isn't a consistent prfpregset_t across platforms,
8086
   but it doesn't matter, because we don't have to pick this
8646
   but it doesn't matter, because we don't have to pick this
8087
   data structure apart.  */
8647
   data structure apart.  */
8088
 
8648
 
8089
static bfd_boolean
8649
static bfd_boolean
8090
elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
8650
elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
8091
{
8651
{
8092
  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8652
  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8093
}
8653
}
8094
 
8654
 
8095
/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8655
/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
8096
   type of NT_PRXFPREG.  Just include the whole note's contents
8656
   type of NT_PRXFPREG.  Just include the whole note's contents
8097
   literally.  */
8657
   literally.  */
8098
 
8658
 
8099
static bfd_boolean
8659
static bfd_boolean
8100
elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
8660
elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
8101
{
8661
{
8102
  return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8662
  return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8103
}
8663
}
8104
 
8664
 
8105
/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8665
/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
8106
   with a note type of NT_X86_XSTATE.  Just include the whole note's
8666
   with a note type of NT_X86_XSTATE.  Just include the whole note's
8107
   contents literally.  */
8667
   contents literally.  */
8108
 
8668
 
8109
static bfd_boolean
8669
static bfd_boolean
8110
elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
8670
elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
8111
{
8671
{
8112
  return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
8672
  return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note);
8113
}
8673
}
8114
 
8674
 
8115
static bfd_boolean
8675
static bfd_boolean
8116
elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
8676
elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
8117
{
8677
{
8118
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
8678
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note);
8119
}
8679
}
8120
 
8680
 
8121
static bfd_boolean
8681
static bfd_boolean
8122
elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
8682
elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
8123
{
8683
{
8124
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
8684
  return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note);
8125
}
8685
}
8126
 
8686
 
8127
static bfd_boolean
8687
static bfd_boolean
8128
elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
8688
elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
8129
{
8689
{
8130
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
8690
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note);
8131
}
8691
}
8132
 
8692
 
8133
static bfd_boolean
8693
static bfd_boolean
8134
elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
8694
elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
8135
{
8695
{
8136
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
8696
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note);
8137
}
8697
}
8138
 
8698
 
8139
static bfd_boolean
8699
static bfd_boolean
8140
elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
8700
elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
8141
{
8701
{
8142
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
8702
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note);
8143
}
8703
}
8144
 
8704
 
8145
static bfd_boolean
8705
static bfd_boolean
8146
elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
8706
elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
8147
{
8707
{
8148
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
8708
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note);
8149
}
8709
}
8150
 
8710
 
8151
static bfd_boolean
8711
static bfd_boolean
8152
elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
8712
elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
8153
{
8713
{
8154
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
8714
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note);
8155
}
8715
}
8156
 
8716
 
8157
static bfd_boolean
8717
static bfd_boolean
8158
elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
8718
elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
8159
{
8719
{
8160
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
8720
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note);
8161
}
8721
}
8162
 
8722
 
8163
static bfd_boolean
8723
static bfd_boolean
8164
elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
8724
elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
8165
{
8725
{
8166
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
8726
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note);
8167
}
8727
}
8168
 
8728
 
8169
static bfd_boolean
8729
static bfd_boolean
8170
elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
8730
elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
8171
{
8731
{
8172
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
8732
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note);
8173
}
8733
}
8174
 
8734
 
8175
static bfd_boolean
8735
static bfd_boolean
8176
elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
8736
elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
8177
{
8737
{
8178
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
8738
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note);
8179
}
8739
}
8180
 
8740
 
8181
static bfd_boolean
8741
static bfd_boolean
-
 
8742
elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
-
 
8743
{
-
 
8744
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note);
-
 
8745
}
-
 
8746
 
-
 
8747
static bfd_boolean
-
 
8748
elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
-
 
8749
{
-
 
8750
  return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note);
-
 
8751
}
-
 
8752
 
-
 
8753
static bfd_boolean
8182
elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
8754
elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
8183
{
8755
{
8184
  return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
8756
  return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note);
8185
}
8757
}
8186
 
8758
 
8187
static bfd_boolean
8759
static bfd_boolean
8188
elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
8760
elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
8189
{
8761
{
8190
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
8762
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note);
8191
}
8763
}
8192
 
8764
 
8193
static bfd_boolean
8765
static bfd_boolean
8194
elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
8766
elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
8195
{
8767
{
8196
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
8768
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note);
8197
}
8769
}
8198
 
8770
 
8199
static bfd_boolean
8771
static bfd_boolean
8200
elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
8772
elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
8201
{
8773
{
8202
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
8774
  return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note);
8203
}
8775
}
8204
 
8776
 
8205
#if defined (HAVE_PRPSINFO_T)
8777
#if defined (HAVE_PRPSINFO_T)
8206
typedef prpsinfo_t   elfcore_psinfo_t;
8778
typedef prpsinfo_t   elfcore_psinfo_t;
8207
#if defined (HAVE_PRPSINFO32_T)		/* Sparc64 cross Sparc32 */
8779
#if defined (HAVE_PRPSINFO32_T)		/* Sparc64 cross Sparc32 */
8208
typedef prpsinfo32_t elfcore_psinfo32_t;
8780
typedef prpsinfo32_t elfcore_psinfo32_t;
8209
#endif
8781
#endif
8210
#endif
8782
#endif
8211
 
8783
 
8212
#if defined (HAVE_PSINFO_T)
8784
#if defined (HAVE_PSINFO_T)
8213
typedef psinfo_t   elfcore_psinfo_t;
8785
typedef psinfo_t   elfcore_psinfo_t;
8214
#if defined (HAVE_PSINFO32_T)		/* Sparc64 cross Sparc32 */
8786
#if defined (HAVE_PSINFO32_T)		/* Sparc64 cross Sparc32 */
8215
typedef psinfo32_t elfcore_psinfo32_t;
8787
typedef psinfo32_t elfcore_psinfo32_t;
8216
#endif
8788
#endif
8217
#endif
8789
#endif
8218
 
8790
 
8219
/* return a malloc'ed copy of a string at START which is at
8791
/* return a malloc'ed copy of a string at START which is at
8220
   most MAX bytes long, possibly without a terminating '\0'.
8792
   most MAX bytes long, possibly without a terminating '\0'.
8221
   the copy will always have a terminating '\0'.  */
8793
   the copy will always have a terminating '\0'.  */
8222
 
8794
 
8223
char *
8795
char *
8224
_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
8796
_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
8225
{
8797
{
8226
  char *dups;
8798
  char *dups;
8227
  char *end = (char *) memchr (start, '\0', max);
8799
  char *end = (char *) memchr (start, '\0', max);
8228
  size_t len;
8800
  size_t len;
8229
 
8801
 
8230
  if (end == NULL)
8802
  if (end == NULL)
8231
    len = max;
8803
    len = max;
8232
  else
8804
  else
8233
    len = end - start;
8805
    len = end - start;
8234
 
8806
 
8235
  dups = (char *) bfd_alloc (abfd, len + 1);
8807
  dups = (char *) bfd_alloc (abfd, len + 1);
8236
  if (dups == NULL)
8808
  if (dups == NULL)
8237
    return NULL;
8809
    return NULL;
8238
 
8810
 
8239
  memcpy (dups, start, len);
8811
  memcpy (dups, start, len);
8240
  dups[len] = '\0';
8812
  dups[len] = '\0';
8241
 
8813
 
8242
  return dups;
8814
  return dups;
8243
}
8815
}
8244
 
8816
 
8245
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8817
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8246
static bfd_boolean
8818
static bfd_boolean
8247
elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
8819
elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
8248
{
8820
{
8249
  if (note->descsz == sizeof (elfcore_psinfo_t))
8821
  if (note->descsz == sizeof (elfcore_psinfo_t))
8250
    {
8822
    {
8251
      elfcore_psinfo_t psinfo;
8823
      elfcore_psinfo_t psinfo;
8252
 
8824
 
8253
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
8825
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
8254
 
8826
 
8255
#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8827
#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
8256
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8828
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8257
#endif
8829
#endif
8258
      elf_tdata (abfd)->core->program
8830
      elf_tdata (abfd)->core->program
8259
	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8831
	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8260
				sizeof (psinfo.pr_fname));
8832
				sizeof (psinfo.pr_fname));
8261
 
8833
 
8262
      elf_tdata (abfd)->core->command
8834
      elf_tdata (abfd)->core->command
8263
	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8835
	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8264
				sizeof (psinfo.pr_psargs));
8836
				sizeof (psinfo.pr_psargs));
8265
    }
8837
    }
8266
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8838
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
8267
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
8839
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
8268
    {
8840
    {
8269
      /* 64-bit host, 32-bit corefile */
8841
      /* 64-bit host, 32-bit corefile */
8270
      elfcore_psinfo32_t psinfo;
8842
      elfcore_psinfo32_t psinfo;
8271
 
8843
 
8272
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
8844
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
8273
 
8845
 
8274
#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8846
#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
8275
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8847
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
8276
#endif
8848
#endif
8277
      elf_tdata (abfd)->core->program
8849
      elf_tdata (abfd)->core->program
8278
	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8850
	= _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
8279
				sizeof (psinfo.pr_fname));
8851
				sizeof (psinfo.pr_fname));
8280
 
8852
 
8281
      elf_tdata (abfd)->core->command
8853
      elf_tdata (abfd)->core->command
8282
	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8854
	= _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
8283
				sizeof (psinfo.pr_psargs));
8855
				sizeof (psinfo.pr_psargs));
8284
    }
8856
    }
8285
#endif
8857
#endif
8286
 
8858
 
8287
  else
8859
  else
8288
    {
8860
    {
8289
      /* Fail - we don't know how to handle any other
8861
      /* Fail - we don't know how to handle any other
8290
	 note size (ie. data object type).  */
8862
	 note size (ie. data object type).  */
8291
      return TRUE;
8863
      return TRUE;
8292
    }
8864
    }
8293
 
8865
 
8294
  /* Note that for some reason, a spurious space is tacked
8866
  /* Note that for some reason, a spurious space is tacked
8295
     onto the end of the args in some (at least one anyway)
8867
     onto the end of the args in some (at least one anyway)
8296
     implementations, so strip it off if it exists.  */
8868
     implementations, so strip it off if it exists.  */
8297
 
8869
 
8298
  {
8870
  {
8299
    char *command = elf_tdata (abfd)->core->command;
8871
    char *command = elf_tdata (abfd)->core->command;
8300
    int n = strlen (command);
8872
    int n = strlen (command);
8301
 
8873
 
8302
    if (0 < n && command[n - 1] == ' ')
8874
    if (0 < n && command[n - 1] == ' ')
8303
      command[n - 1] = '\0';
8875
      command[n - 1] = '\0';
8304
  }
8876
  }
8305
 
8877
 
8306
  return TRUE;
8878
  return TRUE;
8307
}
8879
}
8308
#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8880
#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
8309
 
8881
 
8310
#if defined (HAVE_PSTATUS_T)
8882
#if defined (HAVE_PSTATUS_T)
8311
static bfd_boolean
8883
static bfd_boolean
8312
elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
8884
elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
8313
{
8885
{
8314
  if (note->descsz == sizeof (pstatus_t)
8886
  if (note->descsz == sizeof (pstatus_t)
8315
#if defined (HAVE_PXSTATUS_T)
8887
#if defined (HAVE_PXSTATUS_T)
8316
      || note->descsz == sizeof (pxstatus_t)
8888
      || note->descsz == sizeof (pxstatus_t)
8317
#endif
8889
#endif
8318
      )
8890
      )
8319
    {
8891
    {
8320
      pstatus_t pstat;
8892
      pstatus_t pstat;
8321
 
8893
 
8322
      memcpy (&pstat, note->descdata, sizeof (pstat));
8894
      memcpy (&pstat, note->descdata, sizeof (pstat));
8323
 
8895
 
8324
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
8896
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
8325
    }
8897
    }
8326
#if defined (HAVE_PSTATUS32_T)
8898
#if defined (HAVE_PSTATUS32_T)
8327
  else if (note->descsz == sizeof (pstatus32_t))
8899
  else if (note->descsz == sizeof (pstatus32_t))
8328
    {
8900
    {
8329
      /* 64-bit host, 32-bit corefile */
8901
      /* 64-bit host, 32-bit corefile */
8330
      pstatus32_t pstat;
8902
      pstatus32_t pstat;
8331
 
8903
 
8332
      memcpy (&pstat, note->descdata, sizeof (pstat));
8904
      memcpy (&pstat, note->descdata, sizeof (pstat));
8333
 
8905
 
8334
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
8906
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
8335
    }
8907
    }
8336
#endif
8908
#endif
8337
  /* Could grab some more details from the "representative"
8909
  /* Could grab some more details from the "representative"
8338
     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8910
     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
8339
     NT_LWPSTATUS note, presumably.  */
8911
     NT_LWPSTATUS note, presumably.  */
8340
 
8912
 
8341
  return TRUE;
8913
  return TRUE;
8342
}
8914
}
8343
#endif /* defined (HAVE_PSTATUS_T) */
8915
#endif /* defined (HAVE_PSTATUS_T) */
8344
 
8916
 
8345
#if defined (HAVE_LWPSTATUS_T)
8917
#if defined (HAVE_LWPSTATUS_T)
8346
static bfd_boolean
8918
static bfd_boolean
8347
elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
8919
elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
8348
{
8920
{
8349
  lwpstatus_t lwpstat;
8921
  lwpstatus_t lwpstat;
8350
  char buf[100];
8922
  char buf[100];
8351
  char *name;
8923
  char *name;
8352
  size_t len;
8924
  size_t len;
8353
  asection *sect;
8925
  asection *sect;
8354
 
8926
 
8355
  if (note->descsz != sizeof (lwpstat)
8927
  if (note->descsz != sizeof (lwpstat)
8356
#if defined (HAVE_LWPXSTATUS_T)
8928
#if defined (HAVE_LWPXSTATUS_T)
8357
      && note->descsz != sizeof (lwpxstatus_t)
8929
      && note->descsz != sizeof (lwpxstatus_t)
8358
#endif
8930
#endif
8359
      )
8931
      )
8360
    return TRUE;
8932
    return TRUE;
8361
 
8933
 
8362
  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
8934
  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
8363
 
8935
 
8364
  elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
8936
  elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
8365
  /* Do not overwrite the core signal if it has already been set by
8937
  /* Do not overwrite the core signal if it has already been set by
8366
     another thread.  */
8938
     another thread.  */
8367
  if (elf_tdata (abfd)->core->signal == 0)
8939
  if (elf_tdata (abfd)->core->signal == 0)
8368
    elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
8940
    elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
8369
 
8941
 
8370
  /* Make a ".reg/999" section.  */
8942
  /* Make a ".reg/999" section.  */
8371
 
8943
 
8372
  sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
8944
  sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
8373
  len = strlen (buf) + 1;
8945
  len = strlen (buf) + 1;
8374
  name = bfd_alloc (abfd, len);
8946
  name = bfd_alloc (abfd, len);
8375
  if (name == NULL)
8947
  if (name == NULL)
8376
    return FALSE;
8948
    return FALSE;
8377
  memcpy (name, buf, len);
8949
  memcpy (name, buf, len);
8378
 
8950
 
8379
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8951
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8380
  if (sect == NULL)
8952
  if (sect == NULL)
8381
    return FALSE;
8953
    return FALSE;
8382
 
8954
 
8383
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8955
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8384
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
8956
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
8385
  sect->filepos = note->descpos
8957
  sect->filepos = note->descpos
8386
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
8958
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
8387
#endif
8959
#endif
8388
 
8960
 
8389
#if defined (HAVE_LWPSTATUS_T_PR_REG)
8961
#if defined (HAVE_LWPSTATUS_T_PR_REG)
8390
  sect->size = sizeof (lwpstat.pr_reg);
8962
  sect->size = sizeof (lwpstat.pr_reg);
8391
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
8963
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
8392
#endif
8964
#endif
8393
 
8965
 
8394
  sect->alignment_power = 2;
8966
  sect->alignment_power = 2;
8395
 
8967
 
8396
  if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
8968
  if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
8397
    return FALSE;
8969
    return FALSE;
8398
 
8970
 
8399
  /* Make a ".reg2/999" section */
8971
  /* Make a ".reg2/999" section */
8400
 
8972
 
8401
  sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
8973
  sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
8402
  len = strlen (buf) + 1;
8974
  len = strlen (buf) + 1;
8403
  name = bfd_alloc (abfd, len);
8975
  name = bfd_alloc (abfd, len);
8404
  if (name == NULL)
8976
  if (name == NULL)
8405
    return FALSE;
8977
    return FALSE;
8406
  memcpy (name, buf, len);
8978
  memcpy (name, buf, len);
8407
 
8979
 
8408
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8980
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8409
  if (sect == NULL)
8981
  if (sect == NULL)
8410
    return FALSE;
8982
    return FALSE;
8411
 
8983
 
8412
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8984
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
8413
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
8985
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
8414
  sect->filepos = note->descpos
8986
  sect->filepos = note->descpos
8415
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
8987
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
8416
#endif
8988
#endif
8417
 
8989
 
8418
#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8990
#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
8419
  sect->size = sizeof (lwpstat.pr_fpreg);
8991
  sect->size = sizeof (lwpstat.pr_fpreg);
8420
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
8992
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
8421
#endif
8993
#endif
8422
 
8994
 
8423
  sect->alignment_power = 2;
8995
  sect->alignment_power = 2;
8424
 
8996
 
8425
  return elfcore_maybe_make_sect (abfd, ".reg2", sect);
8997
  return elfcore_maybe_make_sect (abfd, ".reg2", sect);
8426
}
8998
}
8427
#endif /* defined (HAVE_LWPSTATUS_T) */
8999
#endif /* defined (HAVE_LWPSTATUS_T) */
8428
 
9000
 
8429
static bfd_boolean
9001
static bfd_boolean
8430
elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
9002
elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
8431
{
9003
{
8432
  char buf[30];
9004
  char buf[30];
8433
  char *name;
9005
  char *name;
8434
  size_t len;
9006
  size_t len;
8435
  asection *sect;
9007
  asection *sect;
8436
  int type;
9008
  int type;
8437
  int is_active_thread;
9009
  int is_active_thread;
8438
  bfd_vma base_addr;
9010
  bfd_vma base_addr;
8439
 
9011
 
8440
  if (note->descsz < 728)
9012
  if (note->descsz < 728)
8441
    return TRUE;
9013
    return TRUE;
8442
 
9014
 
8443
  if (! CONST_STRNEQ (note->namedata, "win32"))
9015
  if (! CONST_STRNEQ (note->namedata, "win32"))
8444
    return TRUE;
9016
    return TRUE;
8445
 
9017
 
8446
  type = bfd_get_32 (abfd, note->descdata);
9018
  type = bfd_get_32 (abfd, note->descdata);
8447
 
9019
 
8448
  switch (type)
9020
  switch (type)
8449
    {
9021
    {
8450
    case 1 /* NOTE_INFO_PROCESS */:
9022
    case 1 /* NOTE_INFO_PROCESS */:
8451
      /* FIXME: need to add ->core->command.  */
9023
      /* FIXME: need to add ->core->command.  */
8452
      /* process_info.pid */
9024
      /* process_info.pid */
8453
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
9025
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 8);
8454
      /* process_info.signal */
9026
      /* process_info.signal */
8455
      elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
9027
      elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 12);
8456
      break;
9028
      break;
8457
 
9029
 
8458
    case 2 /* NOTE_INFO_THREAD */:
9030
    case 2 /* NOTE_INFO_THREAD */:
8459
      /* Make a ".reg/999" section.  */
9031
      /* Make a ".reg/999" section.  */
8460
      /* thread_info.tid */
9032
      /* thread_info.tid */
8461
      sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
9033
      sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 8));
8462
 
9034
 
8463
      len = strlen (buf) + 1;
9035
      len = strlen (buf) + 1;
8464
      name = (char *) bfd_alloc (abfd, len);
9036
      name = (char *) bfd_alloc (abfd, len);
8465
      if (name == NULL)
9037
      if (name == NULL)
8466
	return FALSE;
9038
	return FALSE;
8467
 
9039
 
8468
      memcpy (name, buf, len);
9040
      memcpy (name, buf, len);
8469
 
9041
 
8470
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9042
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8471
      if (sect == NULL)
9043
      if (sect == NULL)
8472
	return FALSE;
9044
	return FALSE;
8473
 
9045
 
8474
      /* sizeof (thread_info.thread_context) */
9046
      /* sizeof (thread_info.thread_context) */
8475
      sect->size = 716;
9047
      sect->size = 716;
8476
      /* offsetof (thread_info.thread_context) */
9048
      /* offsetof (thread_info.thread_context) */
8477
      sect->filepos = note->descpos + 12;
9049
      sect->filepos = note->descpos + 12;
8478
      sect->alignment_power = 2;
9050
      sect->alignment_power = 2;
8479
 
9051
 
8480
      /* thread_info.is_active_thread */
9052
      /* thread_info.is_active_thread */
8481
      is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
9053
      is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
8482
 
9054
 
8483
      if (is_active_thread)
9055
      if (is_active_thread)
8484
	if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
9056
	if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
8485
	  return FALSE;
9057
	  return FALSE;
8486
      break;
9058
      break;
8487
 
9059
 
8488
    case 3 /* NOTE_INFO_MODULE */:
9060
    case 3 /* NOTE_INFO_MODULE */:
8489
      /* Make a ".module/xxxxxxxx" section.  */
9061
      /* Make a ".module/xxxxxxxx" section.  */
8490
      /* module_info.base_address */
9062
      /* module_info.base_address */
8491
      base_addr = bfd_get_32 (abfd, note->descdata + 4);
9063
      base_addr = bfd_get_32 (abfd, note->descdata + 4);
8492
      sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
9064
      sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
8493
 
9065
 
8494
      len = strlen (buf) + 1;
9066
      len = strlen (buf) + 1;
8495
      name = (char *) bfd_alloc (abfd, len);
9067
      name = (char *) bfd_alloc (abfd, len);
8496
      if (name == NULL)
9068
      if (name == NULL)
8497
	return FALSE;
9069
	return FALSE;
8498
 
9070
 
8499
      memcpy (name, buf, len);
9071
      memcpy (name, buf, len);
8500
 
9072
 
8501
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9073
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8502
 
9074
 
8503
      if (sect == NULL)
9075
      if (sect == NULL)
8504
	return FALSE;
9076
	return FALSE;
8505
 
9077
 
8506
      sect->size = note->descsz;
9078
      sect->size = note->descsz;
8507
      sect->filepos = note->descpos;
9079
      sect->filepos = note->descpos;
8508
      sect->alignment_power = 2;
9080
      sect->alignment_power = 2;
8509
      break;
9081
      break;
8510
 
9082
 
8511
    default:
9083
    default:
8512
      return TRUE;
9084
      return TRUE;
8513
    }
9085
    }
8514
 
9086
 
8515
  return TRUE;
9087
  return TRUE;
8516
}
9088
}
8517
 
9089
 
8518
static bfd_boolean
9090
static bfd_boolean
8519
elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
9091
elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
8520
{
9092
{
8521
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9093
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
8522
 
9094
 
8523
  switch (note->type)
9095
  switch (note->type)
8524
    {
9096
    {
8525
    default:
9097
    default:
8526
      return TRUE;
9098
      return TRUE;
8527
 
9099
 
8528
    case NT_PRSTATUS:
9100
    case NT_PRSTATUS:
8529
      if (bed->elf_backend_grok_prstatus)
9101
      if (bed->elf_backend_grok_prstatus)
8530
	if ((*bed->elf_backend_grok_prstatus) (abfd, note))
9102
	if ((*bed->elf_backend_grok_prstatus) (abfd, note))
8531
	  return TRUE;
9103
	  return TRUE;
8532
#if defined (HAVE_PRSTATUS_T)
9104
#if defined (HAVE_PRSTATUS_T)
8533
      return elfcore_grok_prstatus (abfd, note);
9105
      return elfcore_grok_prstatus (abfd, note);
8534
#else
9106
#else
8535
      return TRUE;
9107
      return TRUE;
8536
#endif
9108
#endif
8537
 
9109
 
8538
#if defined (HAVE_PSTATUS_T)
9110
#if defined (HAVE_PSTATUS_T)
8539
    case NT_PSTATUS:
9111
    case NT_PSTATUS:
8540
      return elfcore_grok_pstatus (abfd, note);
9112
      return elfcore_grok_pstatus (abfd, note);
8541
#endif
9113
#endif
8542
 
9114
 
8543
#if defined (HAVE_LWPSTATUS_T)
9115
#if defined (HAVE_LWPSTATUS_T)
8544
    case NT_LWPSTATUS:
9116
    case NT_LWPSTATUS:
8545
      return elfcore_grok_lwpstatus (abfd, note);
9117
      return elfcore_grok_lwpstatus (abfd, note);
8546
#endif
9118
#endif
8547
 
9119
 
8548
    case NT_FPREGSET:		/* FIXME: rename to NT_PRFPREG */
9120
    case NT_FPREGSET:		/* FIXME: rename to NT_PRFPREG */
8549
      return elfcore_grok_prfpreg (abfd, note);
9121
      return elfcore_grok_prfpreg (abfd, note);
8550
 
9122
 
8551
    case NT_WIN32PSTATUS:
9123
    case NT_WIN32PSTATUS:
8552
      return elfcore_grok_win32pstatus (abfd, note);
9124
      return elfcore_grok_win32pstatus (abfd, note);
8553
 
9125
 
8554
    case NT_PRXFPREG:		/* Linux SSE extension */
9126
    case NT_PRXFPREG:		/* Linux SSE extension */
8555
      if (note->namesz == 6
9127
      if (note->namesz == 6
8556
	  && strcmp (note->namedata, "LINUX") == 0)
9128
	  && strcmp (note->namedata, "LINUX") == 0)
8557
	return elfcore_grok_prxfpreg (abfd, note);
9129
	return elfcore_grok_prxfpreg (abfd, note);
8558
      else
9130
      else
8559
	return TRUE;
9131
	return TRUE;
8560
 
9132
 
8561
    case NT_X86_XSTATE:		/* Linux XSAVE extension */
9133
    case NT_X86_XSTATE:		/* Linux XSAVE extension */
8562
      if (note->namesz == 6
9134
      if (note->namesz == 6
8563
	  && strcmp (note->namedata, "LINUX") == 0)
9135
	  && strcmp (note->namedata, "LINUX") == 0)
8564
	return elfcore_grok_xstatereg (abfd, note);
9136
	return elfcore_grok_xstatereg (abfd, note);
-
 
9137
      else if (note->namesz == 8
-
 
9138
	  && strcmp (note->namedata, "FreeBSD") == 0)
-
 
9139
	return elfcore_grok_xstatereg (abfd, note);
8565
      else
9140
      else
8566
	return TRUE;
9141
	return TRUE;
8567
 
9142
 
8568
    case NT_PPC_VMX:
9143
    case NT_PPC_VMX:
8569
      if (note->namesz == 6
9144
      if (note->namesz == 6
8570
	  && strcmp (note->namedata, "LINUX") == 0)
9145
	  && strcmp (note->namedata, "LINUX") == 0)
8571
	return elfcore_grok_ppc_vmx (abfd, note);
9146
	return elfcore_grok_ppc_vmx (abfd, note);
8572
      else
9147
      else
8573
	return TRUE;
9148
	return TRUE;
8574
 
9149
 
8575
    case NT_PPC_VSX:
9150
    case NT_PPC_VSX:
8576
      if (note->namesz == 6
9151
      if (note->namesz == 6
8577
          && strcmp (note->namedata, "LINUX") == 0)
9152
          && strcmp (note->namedata, "LINUX") == 0)
8578
        return elfcore_grok_ppc_vsx (abfd, note);
9153
        return elfcore_grok_ppc_vsx (abfd, note);
8579
      else
9154
      else
8580
        return TRUE;
9155
        return TRUE;
8581
 
9156
 
8582
    case NT_S390_HIGH_GPRS:
9157
    case NT_S390_HIGH_GPRS:
8583
      if (note->namesz == 6
9158
      if (note->namesz == 6
8584
          && strcmp (note->namedata, "LINUX") == 0)
9159
          && strcmp (note->namedata, "LINUX") == 0)
8585
        return elfcore_grok_s390_high_gprs (abfd, note);
9160
        return elfcore_grok_s390_high_gprs (abfd, note);
8586
      else
9161
      else
8587
        return TRUE;
9162
        return TRUE;
8588
 
9163
 
8589
    case NT_S390_TIMER:
9164
    case NT_S390_TIMER:
8590
      if (note->namesz == 6
9165
      if (note->namesz == 6
8591
          && strcmp (note->namedata, "LINUX") == 0)
9166
          && strcmp (note->namedata, "LINUX") == 0)
8592
        return elfcore_grok_s390_timer (abfd, note);
9167
        return elfcore_grok_s390_timer (abfd, note);
8593
      else
9168
      else
8594
        return TRUE;
9169
        return TRUE;
8595
 
9170
 
8596
    case NT_S390_TODCMP:
9171
    case NT_S390_TODCMP:
8597
      if (note->namesz == 6
9172
      if (note->namesz == 6
8598
          && strcmp (note->namedata, "LINUX") == 0)
9173
          && strcmp (note->namedata, "LINUX") == 0)
8599
        return elfcore_grok_s390_todcmp (abfd, note);
9174
        return elfcore_grok_s390_todcmp (abfd, note);
8600
      else
9175
      else
8601
        return TRUE;
9176
        return TRUE;
8602
 
9177
 
8603
    case NT_S390_TODPREG:
9178
    case NT_S390_TODPREG:
8604
      if (note->namesz == 6
9179
      if (note->namesz == 6
8605
          && strcmp (note->namedata, "LINUX") == 0)
9180
          && strcmp (note->namedata, "LINUX") == 0)
8606
        return elfcore_grok_s390_todpreg (abfd, note);
9181
        return elfcore_grok_s390_todpreg (abfd, note);
8607
      else
9182
      else
8608
        return TRUE;
9183
        return TRUE;
8609
 
9184
 
8610
    case NT_S390_CTRS:
9185
    case NT_S390_CTRS:
8611
      if (note->namesz == 6
9186
      if (note->namesz == 6
8612
          && strcmp (note->namedata, "LINUX") == 0)
9187
          && strcmp (note->namedata, "LINUX") == 0)
8613
        return elfcore_grok_s390_ctrs (abfd, note);
9188
        return elfcore_grok_s390_ctrs (abfd, note);
8614
      else
9189
      else
8615
        return TRUE;
9190
        return TRUE;
8616
 
9191
 
8617
    case NT_S390_PREFIX:
9192
    case NT_S390_PREFIX:
8618
      if (note->namesz == 6
9193
      if (note->namesz == 6
8619
          && strcmp (note->namedata, "LINUX") == 0)
9194
          && strcmp (note->namedata, "LINUX") == 0)
8620
        return elfcore_grok_s390_prefix (abfd, note);
9195
        return elfcore_grok_s390_prefix (abfd, note);
8621
      else
9196
      else
8622
        return TRUE;
9197
        return TRUE;
8623
 
9198
 
8624
    case NT_S390_LAST_BREAK:
9199
    case NT_S390_LAST_BREAK:
8625
      if (note->namesz == 6
9200
      if (note->namesz == 6
8626
          && strcmp (note->namedata, "LINUX") == 0)
9201
          && strcmp (note->namedata, "LINUX") == 0)
8627
        return elfcore_grok_s390_last_break (abfd, note);
9202
        return elfcore_grok_s390_last_break (abfd, note);
8628
      else
9203
      else
8629
        return TRUE;
9204
        return TRUE;
8630
 
9205
 
8631
    case NT_S390_SYSTEM_CALL:
9206
    case NT_S390_SYSTEM_CALL:
8632
      if (note->namesz == 6
9207
      if (note->namesz == 6
8633
          && strcmp (note->namedata, "LINUX") == 0)
9208
          && strcmp (note->namedata, "LINUX") == 0)
8634
        return elfcore_grok_s390_system_call (abfd, note);
9209
        return elfcore_grok_s390_system_call (abfd, note);
8635
      else
9210
      else
8636
        return TRUE;
9211
        return TRUE;
8637
 
9212
 
8638
    case NT_S390_TDB:
9213
    case NT_S390_TDB:
8639
      if (note->namesz == 6
9214
      if (note->namesz == 6
8640
          && strcmp (note->namedata, "LINUX") == 0)
9215
          && strcmp (note->namedata, "LINUX") == 0)
8641
        return elfcore_grok_s390_tdb (abfd, note);
9216
        return elfcore_grok_s390_tdb (abfd, note);
8642
      else
9217
      else
8643
        return TRUE;
9218
        return TRUE;
-
 
9219
 
-
 
9220
    case NT_S390_VXRS_LOW:
-
 
9221
      if (note->namesz == 6
-
 
9222
	  && strcmp (note->namedata, "LINUX") == 0)
-
 
9223
	return elfcore_grok_s390_vxrs_low (abfd, note);
-
 
9224
      else
-
 
9225
	return TRUE;
-
 
9226
 
-
 
9227
    case NT_S390_VXRS_HIGH:
-
 
9228
      if (note->namesz == 6
-
 
9229
	  && strcmp (note->namedata, "LINUX") == 0)
-
 
9230
	return elfcore_grok_s390_vxrs_high (abfd, note);
-
 
9231
      else
-
 
9232
	return TRUE;
8644
 
9233
 
8645
    case NT_ARM_VFP:
9234
    case NT_ARM_VFP:
8646
      if (note->namesz == 6
9235
      if (note->namesz == 6
8647
	  && strcmp (note->namedata, "LINUX") == 0)
9236
	  && strcmp (note->namedata, "LINUX") == 0)
8648
	return elfcore_grok_arm_vfp (abfd, note);
9237
	return elfcore_grok_arm_vfp (abfd, note);
8649
      else
9238
      else
8650
	return TRUE;
9239
	return TRUE;
8651
 
9240
 
8652
    case NT_ARM_TLS:
9241
    case NT_ARM_TLS:
8653
      if (note->namesz == 6
9242
      if (note->namesz == 6
8654
	  && strcmp (note->namedata, "LINUX") == 0)
9243
	  && strcmp (note->namedata, "LINUX") == 0)
8655
	return elfcore_grok_aarch_tls (abfd, note);
9244
	return elfcore_grok_aarch_tls (abfd, note);
8656
      else
9245
      else
8657
	return TRUE;
9246
	return TRUE;
8658
 
9247
 
8659
    case NT_ARM_HW_BREAK:
9248
    case NT_ARM_HW_BREAK:
8660
      if (note->namesz == 6
9249
      if (note->namesz == 6
8661
	  && strcmp (note->namedata, "LINUX") == 0)
9250
	  && strcmp (note->namedata, "LINUX") == 0)
8662
	return elfcore_grok_aarch_hw_break (abfd, note);
9251
	return elfcore_grok_aarch_hw_break (abfd, note);
8663
      else
9252
      else
8664
	return TRUE;
9253
	return TRUE;
8665
 
9254
 
8666
    case NT_ARM_HW_WATCH:
9255
    case NT_ARM_HW_WATCH:
8667
      if (note->namesz == 6
9256
      if (note->namesz == 6
8668
	  && strcmp (note->namedata, "LINUX") == 0)
9257
	  && strcmp (note->namedata, "LINUX") == 0)
8669
	return elfcore_grok_aarch_hw_watch (abfd, note);
9258
	return elfcore_grok_aarch_hw_watch (abfd, note);
8670
      else
9259
      else
8671
	return TRUE;
9260
	return TRUE;
8672
 
9261
 
8673
    case NT_PRPSINFO:
9262
    case NT_PRPSINFO:
8674
    case NT_PSINFO:
9263
    case NT_PSINFO:
8675
      if (bed->elf_backend_grok_psinfo)
9264
      if (bed->elf_backend_grok_psinfo)
8676
	if ((*bed->elf_backend_grok_psinfo) (abfd, note))
9265
	if ((*bed->elf_backend_grok_psinfo) (abfd, note))
8677
	  return TRUE;
9266
	  return TRUE;
8678
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9267
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
8679
      return elfcore_grok_psinfo (abfd, note);
9268
      return elfcore_grok_psinfo (abfd, note);
8680
#else
9269
#else
8681
      return TRUE;
9270
      return TRUE;
8682
#endif
9271
#endif
8683
 
9272
 
8684
    case NT_AUXV:
9273
    case NT_AUXV:
8685
      {
9274
      {
8686
	asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9275
	asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8687
							     SEC_HAS_CONTENTS);
9276
							     SEC_HAS_CONTENTS);
8688
 
9277
 
8689
	if (sect == NULL)
9278
	if (sect == NULL)
8690
	  return FALSE;
9279
	  return FALSE;
8691
	sect->size = note->descsz;
9280
	sect->size = note->descsz;
8692
	sect->filepos = note->descpos;
9281
	sect->filepos = note->descpos;
8693
	sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9282
	sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8694
 
9283
 
8695
	return TRUE;
9284
	return TRUE;
8696
      }
9285
      }
8697
 
9286
 
8698
    case NT_FILE:
9287
    case NT_FILE:
8699
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
9288
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
8700
					      note);
9289
					      note);
8701
 
9290
 
8702
    case NT_SIGINFO:
9291
    case NT_SIGINFO:
8703
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
9292
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
8704
					      note);
9293
					      note);
8705
    }
9294
    }
8706
}
9295
}
8707
 
9296
 
8708
static bfd_boolean
9297
static bfd_boolean
8709
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
9298
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
8710
{
9299
{
8711
  struct elf_obj_tdata *t;
9300
  struct bfd_build_id* build_id;
8712
 
9301
 
8713
  if (note->descsz == 0)
9302
  if (note->descsz == 0)
8714
    return FALSE;
9303
    return FALSE;
8715
 
-
 
8716
  t = elf_tdata (abfd);
9304
 
8717
  t->build_id = bfd_alloc (abfd, sizeof (*t->build_id) - 1 + note->descsz);
9305
  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
8718
  if (t->build_id == NULL)
9306
  if (build_id == NULL)
8719
    return FALSE;
9307
    return FALSE;
8720
 
9308
 
-
 
9309
  build_id->size = note->descsz;
8721
  t->build_id->size = note->descsz;
9310
  memcpy (build_id->data, note->descdata, note->descsz);
8722
  memcpy (t->build_id->data, note->descdata, note->descsz);
9311
  abfd->build_id = build_id;
8723
 
9312
 
8724
  return TRUE;
9313
  return TRUE;
8725
}
9314
}
8726
 
9315
 
8727
static bfd_boolean
9316
static bfd_boolean
8728
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
9317
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
8729
{
9318
{
8730
  switch (note->type)
9319
  switch (note->type)
8731
    {
9320
    {
8732
    default:
9321
    default:
8733
      return TRUE;
9322
      return TRUE;
8734
 
9323
 
8735
    case NT_GNU_BUILD_ID:
9324
    case NT_GNU_BUILD_ID:
8736
      return elfobj_grok_gnu_build_id (abfd, note);
9325
      return elfobj_grok_gnu_build_id (abfd, note);
8737
    }
9326
    }
8738
}
9327
}
8739
 
9328
 
8740
static bfd_boolean
9329
static bfd_boolean
8741
elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
9330
elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
8742
{
9331
{
8743
  struct sdt_note *cur =
9332
  struct sdt_note *cur =
8744
    (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
9333
    (struct sdt_note *) bfd_alloc (abfd, sizeof (struct sdt_note)
8745
				   + note->descsz);
9334
				   + note->descsz);
8746
 
9335
 
8747
  cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
9336
  cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
8748
  cur->size = (bfd_size_type) note->descsz;
9337
  cur->size = (bfd_size_type) note->descsz;
8749
  memcpy (cur->data, note->descdata, note->descsz);
9338
  memcpy (cur->data, note->descdata, note->descsz);
8750
 
9339
 
8751
  elf_tdata (abfd)->sdt_note_head = cur;
9340
  elf_tdata (abfd)->sdt_note_head = cur;
8752
 
9341
 
8753
  return TRUE;
9342
  return TRUE;
8754
}
9343
}
8755
 
9344
 
8756
static bfd_boolean
9345
static bfd_boolean
8757
elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
9346
elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
8758
{
9347
{
8759
  switch (note->type)
9348
  switch (note->type)
8760
    {
9349
    {
8761
    case NT_STAPSDT:
9350
    case NT_STAPSDT:
8762
      return elfobj_grok_stapsdt_note_1 (abfd, note);
9351
      return elfobj_grok_stapsdt_note_1 (abfd, note);
8763
 
9352
 
8764
    default:
9353
    default:
8765
      return TRUE;
9354
      return TRUE;
8766
    }
9355
    }
8767
}
9356
}
8768
 
9357
 
8769
static bfd_boolean
9358
static bfd_boolean
8770
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
9359
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
8771
{
9360
{
8772
  char *cp;
9361
  char *cp;
8773
 
9362
 
8774
  cp = strchr (note->namedata, '@');
9363
  cp = strchr (note->namedata, '@');
8775
  if (cp != NULL)
9364
  if (cp != NULL)
8776
    {
9365
    {
8777
      *lwpidp = atoi(cp + 1);
9366
      *lwpidp = atoi(cp + 1);
8778
      return TRUE;
9367
      return TRUE;
8779
    }
9368
    }
8780
  return FALSE;
9369
  return FALSE;
8781
}
9370
}
8782
 
9371
 
8783
static bfd_boolean
9372
static bfd_boolean
8784
elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
9373
elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8785
{
9374
{
8786
  /* Signal number at offset 0x08. */
9375
  /* Signal number at offset 0x08. */
8787
  elf_tdata (abfd)->core->signal
9376
  elf_tdata (abfd)->core->signal
8788
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
9377
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8789
 
9378
 
8790
  /* Process ID at offset 0x50. */
9379
  /* Process ID at offset 0x50. */
8791
  elf_tdata (abfd)->core->pid
9380
  elf_tdata (abfd)->core->pid
8792
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
9381
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
8793
 
9382
 
8794
  /* Command name at 0x7c (max 32 bytes, including nul). */
9383
  /* Command name at 0x7c (max 32 bytes, including nul). */
8795
  elf_tdata (abfd)->core->command
9384
  elf_tdata (abfd)->core->command
8796
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
9385
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
8797
 
9386
 
8798
  return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
9387
  return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
8799
					  note);
9388
					  note);
8800
}
9389
}
8801
 
9390
 
8802
static bfd_boolean
9391
static bfd_boolean
8803
elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
9392
elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
8804
{
9393
{
8805
  int lwp;
9394
  int lwp;
8806
 
9395
 
8807
  if (elfcore_netbsd_get_lwpid (note, &lwp))
9396
  if (elfcore_netbsd_get_lwpid (note, &lwp))
8808
    elf_tdata (abfd)->core->lwpid = lwp;
9397
    elf_tdata (abfd)->core->lwpid = lwp;
8809
 
9398
 
8810
  if (note->type == NT_NETBSDCORE_PROCINFO)
9399
  if (note->type == NT_NETBSDCORE_PROCINFO)
8811
    {
9400
    {
8812
      /* NetBSD-specific core "procinfo".  Note that we expect to
9401
      /* NetBSD-specific core "procinfo".  Note that we expect to
8813
	 find this note before any of the others, which is fine,
9402
	 find this note before any of the others, which is fine,
8814
	 since the kernel writes this note out first when it
9403
	 since the kernel writes this note out first when it
8815
	 creates a core file.  */
9404
	 creates a core file.  */
8816
 
9405
 
8817
      return elfcore_grok_netbsd_procinfo (abfd, note);
9406
      return elfcore_grok_netbsd_procinfo (abfd, note);
8818
    }
9407
    }
8819
 
9408
 
8820
  /* As of Jan 2002 there are no other machine-independent notes
9409
  /* As of Jan 2002 there are no other machine-independent notes
8821
     defined for NetBSD core files.  If the note type is less
9410
     defined for NetBSD core files.  If the note type is less
8822
     than the start of the machine-dependent note types, we don't
9411
     than the start of the machine-dependent note types, we don't
8823
     understand it.  */
9412
     understand it.  */
8824
 
9413
 
8825
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
9414
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
8826
    return TRUE;
9415
    return TRUE;
8827
 
9416
 
8828
 
9417
 
8829
  switch (bfd_get_arch (abfd))
9418
  switch (bfd_get_arch (abfd))
8830
    {
9419
    {
8831
      /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
9420
      /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
8832
	 PT_GETFPREGS == mach+2.  */
9421
	 PT_GETFPREGS == mach+2.  */
8833
 
9422
 
8834
    case bfd_arch_alpha:
9423
    case bfd_arch_alpha:
8835
    case bfd_arch_sparc:
9424
    case bfd_arch_sparc:
8836
      switch (note->type)
9425
      switch (note->type)
8837
	{
9426
	{
8838
	case NT_NETBSDCORE_FIRSTMACH+0:
9427
	case NT_NETBSDCORE_FIRSTMACH+0:
8839
	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
9428
	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
8840
 
9429
 
8841
	case NT_NETBSDCORE_FIRSTMACH+2:
9430
	case NT_NETBSDCORE_FIRSTMACH+2:
8842
	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9431
	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8843
 
9432
 
8844
	default:
9433
	default:
8845
	  return TRUE;
9434
	  return TRUE;
8846
	}
9435
	}
8847
 
9436
 
8848
      /* On all other arch's, PT_GETREGS == mach+1 and
9437
      /* On all other arch's, PT_GETREGS == mach+1 and
8849
	 PT_GETFPREGS == mach+3.  */
9438
	 PT_GETFPREGS == mach+3.  */
8850
 
9439
 
8851
    default:
9440
    default:
8852
      switch (note->type)
9441
      switch (note->type)
8853
	{
9442
	{
8854
	case NT_NETBSDCORE_FIRSTMACH+1:
9443
	case NT_NETBSDCORE_FIRSTMACH+1:
8855
	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
9444
	  return elfcore_make_note_pseudosection (abfd, ".reg", note);
8856
 
9445
 
8857
	case NT_NETBSDCORE_FIRSTMACH+3:
9446
	case NT_NETBSDCORE_FIRSTMACH+3:
8858
	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9447
	  return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8859
 
9448
 
8860
	default:
9449
	default:
8861
	  return TRUE;
9450
	  return TRUE;
8862
	}
9451
	}
8863
    }
9452
    }
8864
    /* NOTREACHED */
9453
    /* NOTREACHED */
8865
}
9454
}
8866
 
9455
 
8867
static bfd_boolean
9456
static bfd_boolean
8868
elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
9457
elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
8869
{
9458
{
8870
  /* Signal number at offset 0x08. */
9459
  /* Signal number at offset 0x08. */
8871
  elf_tdata (abfd)->core->signal
9460
  elf_tdata (abfd)->core->signal
8872
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
9461
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
8873
 
9462
 
8874
  /* Process ID at offset 0x20. */
9463
  /* Process ID at offset 0x20. */
8875
  elf_tdata (abfd)->core->pid
9464
  elf_tdata (abfd)->core->pid
8876
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
9465
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
8877
 
9466
 
8878
  /* Command name at 0x48 (max 32 bytes, including nul). */
9467
  /* Command name at 0x48 (max 32 bytes, including nul). */
8879
  elf_tdata (abfd)->core->command
9468
  elf_tdata (abfd)->core->command
8880
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
9469
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
8881
 
9470
 
8882
  return TRUE;
9471
  return TRUE;
8883
}
9472
}
8884
 
9473
 
8885
static bfd_boolean
9474
static bfd_boolean
8886
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
9475
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
8887
{
9476
{
8888
  if (note->type == NT_OPENBSD_PROCINFO)
9477
  if (note->type == NT_OPENBSD_PROCINFO)
8889
    return elfcore_grok_openbsd_procinfo (abfd, note);
9478
    return elfcore_grok_openbsd_procinfo (abfd, note);
8890
 
9479
 
8891
  if (note->type == NT_OPENBSD_REGS)
9480
  if (note->type == NT_OPENBSD_REGS)
8892
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
9481
    return elfcore_make_note_pseudosection (abfd, ".reg", note);
8893
 
9482
 
8894
  if (note->type == NT_OPENBSD_FPREGS)
9483
  if (note->type == NT_OPENBSD_FPREGS)
8895
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
9484
    return elfcore_make_note_pseudosection (abfd, ".reg2", note);
8896
 
9485
 
8897
  if (note->type == NT_OPENBSD_XFPREGS)
9486
  if (note->type == NT_OPENBSD_XFPREGS)
8898
    return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
9487
    return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
8899
 
9488
 
8900
  if (note->type == NT_OPENBSD_AUXV)
9489
  if (note->type == NT_OPENBSD_AUXV)
8901
    {
9490
    {
8902
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
9491
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
8903
							   SEC_HAS_CONTENTS);
9492
							   SEC_HAS_CONTENTS);
8904
 
9493
 
8905
      if (sect == NULL)
9494
      if (sect == NULL)
8906
	return FALSE;
9495
	return FALSE;
8907
      sect->size = note->descsz;
9496
      sect->size = note->descsz;
8908
      sect->filepos = note->descpos;
9497
      sect->filepos = note->descpos;
8909
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9498
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8910
 
9499
 
8911
      return TRUE;
9500
      return TRUE;
8912
    }
9501
    }
8913
 
9502
 
8914
  if (note->type == NT_OPENBSD_WCOOKIE)
9503
  if (note->type == NT_OPENBSD_WCOOKIE)
8915
    {
9504
    {
8916
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
9505
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
8917
							   SEC_HAS_CONTENTS);
9506
							   SEC_HAS_CONTENTS);
8918
 
9507
 
8919
      if (sect == NULL)
9508
      if (sect == NULL)
8920
	return FALSE;
9509
	return FALSE;
8921
      sect->size = note->descsz;
9510
      sect->size = note->descsz;
8922
      sect->filepos = note->descpos;
9511
      sect->filepos = note->descpos;
8923
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
9512
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
8924
 
9513
 
8925
      return TRUE;
9514
      return TRUE;
8926
    }
9515
    }
8927
 
9516
 
8928
  return TRUE;
9517
  return TRUE;
8929
}
9518
}
8930
 
9519
 
8931
static bfd_boolean
9520
static bfd_boolean
8932
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
9521
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
8933
{
9522
{
8934
  void *ddata = note->descdata;
9523
  void *ddata = note->descdata;
8935
  char buf[100];
9524
  char buf[100];
8936
  char *name;
9525
  char *name;
8937
  asection *sect;
9526
  asection *sect;
8938
  short sig;
9527
  short sig;
8939
  unsigned flags;
9528
  unsigned flags;
8940
 
9529
 
8941
  /* nto_procfs_status 'pid' field is at offset 0.  */
9530
  /* nto_procfs_status 'pid' field is at offset 0.  */
8942
  elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
9531
  elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
8943
 
9532
 
8944
  /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
9533
  /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
8945
  *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
9534
  *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
8946
 
9535
 
8947
  /* nto_procfs_status 'flags' field is at offset 8.  */
9536
  /* nto_procfs_status 'flags' field is at offset 8.  */
8948
  flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
9537
  flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
8949
 
9538
 
8950
  /* nto_procfs_status 'what' field is at offset 14.  */
9539
  /* nto_procfs_status 'what' field is at offset 14.  */
8951
  if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
9540
  if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
8952
    {
9541
    {
8953
      elf_tdata (abfd)->core->signal = sig;
9542
      elf_tdata (abfd)->core->signal = sig;
8954
      elf_tdata (abfd)->core->lwpid = *tid;
9543
      elf_tdata (abfd)->core->lwpid = *tid;
8955
    }
9544
    }
8956
 
9545
 
8957
  /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
9546
  /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
8958
     do not come from signals so we make sure we set the current
9547
     do not come from signals so we make sure we set the current
8959
     thread just in case.  */
9548
     thread just in case.  */
8960
  if (flags & 0x00000080)
9549
  if (flags & 0x00000080)
8961
    elf_tdata (abfd)->core->lwpid = *tid;
9550
    elf_tdata (abfd)->core->lwpid = *tid;
8962
 
9551
 
8963
  /* Make a ".qnx_core_status/%d" section.  */
9552
  /* Make a ".qnx_core_status/%d" section.  */
8964
  sprintf (buf, ".qnx_core_status/%ld", *tid);
9553
  sprintf (buf, ".qnx_core_status/%ld", *tid);
8965
 
9554
 
8966
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9555
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8967
  if (name == NULL)
9556
  if (name == NULL)
8968
    return FALSE;
9557
    return FALSE;
8969
  strcpy (name, buf);
9558
  strcpy (name, buf);
8970
 
9559
 
8971
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9560
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
8972
  if (sect == NULL)
9561
  if (sect == NULL)
8973
    return FALSE;
9562
    return FALSE;
8974
 
9563
 
8975
  sect->size            = note->descsz;
9564
  sect->size            = note->descsz;
8976
  sect->filepos         = note->descpos;
9565
  sect->filepos         = note->descpos;
8977
  sect->alignment_power = 2;
9566
  sect->alignment_power = 2;
8978
 
9567
 
8979
  return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
9568
  return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
8980
}
9569
}
8981
 
9570
 
8982
static bfd_boolean
9571
static bfd_boolean
8983
elfcore_grok_nto_regs (bfd *abfd,
9572
elfcore_grok_nto_regs (bfd *abfd,
8984
		       Elf_Internal_Note *note,
9573
		       Elf_Internal_Note *note,
8985
		       long tid,
9574
		       long tid,
8986
		       char *base)
9575
		       char *base)
8987
{
9576
{
8988
  char buf[100];
9577
  char buf[100];
8989
  char *name;
9578
  char *name;
8990
  asection *sect;
9579
  asection *sect;
8991
 
9580
 
8992
  /* Make a "(base)/%d" section.  */
9581
  /* Make a "(base)/%d" section.  */
8993
  sprintf (buf, "%s/%ld", base, tid);
9582
  sprintf (buf, "%s/%ld", base, tid);
8994
 
9583
 
8995
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
9584
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
8996
  if (name == NULL)
9585
  if (name == NULL)
8997
    return FALSE;
9586
    return FALSE;
8998
  strcpy (name, buf);
9587
  strcpy (name, buf);
8999
 
9588
 
9000
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9589
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9001
  if (sect == NULL)
9590
  if (sect == NULL)
9002
    return FALSE;
9591
    return FALSE;
9003
 
9592
 
9004
  sect->size            = note->descsz;
9593
  sect->size            = note->descsz;
9005
  sect->filepos         = note->descpos;
9594
  sect->filepos         = note->descpos;
9006
  sect->alignment_power = 2;
9595
  sect->alignment_power = 2;
9007
 
9596
 
9008
  /* This is the current thread.  */
9597
  /* This is the current thread.  */
9009
  if (elf_tdata (abfd)->core->lwpid == tid)
9598
  if (elf_tdata (abfd)->core->lwpid == tid)
9010
    return elfcore_maybe_make_sect (abfd, base, sect);
9599
    return elfcore_maybe_make_sect (abfd, base, sect);
9011
 
9600
 
9012
  return TRUE;
9601
  return TRUE;
9013
}
9602
}
9014
 
9603
 
9015
#define BFD_QNT_CORE_INFO	7
9604
#define BFD_QNT_CORE_INFO	7
9016
#define BFD_QNT_CORE_STATUS	8
9605
#define BFD_QNT_CORE_STATUS	8
9017
#define BFD_QNT_CORE_GREG	9
9606
#define BFD_QNT_CORE_GREG	9
9018
#define BFD_QNT_CORE_FPREG	10
9607
#define BFD_QNT_CORE_FPREG	10
9019
 
9608
 
9020
static bfd_boolean
9609
static bfd_boolean
9021
elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
9610
elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
9022
{
9611
{
9023
  /* Every GREG section has a STATUS section before it.  Store the
9612
  /* Every GREG section has a STATUS section before it.  Store the
9024
     tid from the previous call to pass down to the next gregs
9613
     tid from the previous call to pass down to the next gregs
9025
     function.  */
9614
     function.  */
9026
  static long tid = 1;
9615
  static long tid = 1;
9027
 
9616
 
9028
  switch (note->type)
9617
  switch (note->type)
9029
    {
9618
    {
9030
    case BFD_QNT_CORE_INFO:
9619
    case BFD_QNT_CORE_INFO:
9031
      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
9620
      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
9032
    case BFD_QNT_CORE_STATUS:
9621
    case BFD_QNT_CORE_STATUS:
9033
      return elfcore_grok_nto_status (abfd, note, &tid);
9622
      return elfcore_grok_nto_status (abfd, note, &tid);
9034
    case BFD_QNT_CORE_GREG:
9623
    case BFD_QNT_CORE_GREG:
9035
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
9624
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg");
9036
    case BFD_QNT_CORE_FPREG:
9625
    case BFD_QNT_CORE_FPREG:
9037
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
9626
      return elfcore_grok_nto_regs (abfd, note, tid, ".reg2");
9038
    default:
9627
    default:
9039
      return TRUE;
9628
      return TRUE;
9040
    }
9629
    }
9041
}
9630
}
9042
 
9631
 
9043
static bfd_boolean
9632
static bfd_boolean
9044
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
9633
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
9045
{
9634
{
9046
  char *name;
9635
  char *name;
9047
  asection *sect;
9636
  asection *sect;
9048
  size_t len;
9637
  size_t len;
9049
 
9638
 
9050
  /* Use note name as section name.  */
9639
  /* Use note name as section name.  */
9051
  len = note->namesz;
9640
  len = note->namesz;
9052
  name = (char *) bfd_alloc (abfd, len);
9641
  name = (char *) bfd_alloc (abfd, len);
9053
  if (name == NULL)
9642
  if (name == NULL)
9054
    return FALSE;
9643
    return FALSE;
9055
  memcpy (name, note->namedata, len);
9644
  memcpy (name, note->namedata, len);
9056
  name[len - 1] = '\0';
9645
  name[len - 1] = '\0';
9057
 
9646
 
9058
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9647
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
9059
  if (sect == NULL)
9648
  if (sect == NULL)
9060
    return FALSE;
9649
    return FALSE;
9061
 
9650
 
9062
  sect->size            = note->descsz;
9651
  sect->size            = note->descsz;
9063
  sect->filepos         = note->descpos;
9652
  sect->filepos         = note->descpos;
9064
  sect->alignment_power = 1;
9653
  sect->alignment_power = 1;
9065
 
9654
 
9066
  return TRUE;
9655
  return TRUE;
9067
}
9656
}
9068
 
9657
 
9069
/* Function: elfcore_write_note
9658
/* Function: elfcore_write_note
9070
 
9659
 
9071
   Inputs:
9660
   Inputs:
9072
     buffer to hold note, and current size of buffer
9661
     buffer to hold note, and current size of buffer
9073
     name of note
9662
     name of note
9074
     type of note
9663
     type of note
9075
     data for note
9664
     data for note
9076
     size of data for note
9665
     size of data for note
9077
 
9666
 
9078
   Writes note to end of buffer.  ELF64 notes are written exactly as
9667
   Writes note to end of buffer.  ELF64 notes are written exactly as
9079
   for ELF32, despite the current (as of 2006) ELF gabi specifying
9668
   for ELF32, despite the current (as of 2006) ELF gabi specifying
9080
   that they ought to have 8-byte namesz and descsz field, and have
9669
   that they ought to have 8-byte namesz and descsz field, and have
9081
   8-byte alignment.  Other writers, eg. Linux kernel, do the same.
9670
   8-byte alignment.  Other writers, eg. Linux kernel, do the same.
9082
 
9671
 
9083
   Return:
9672
   Return:
9084
   Pointer to realloc'd buffer, *BUFSIZ updated.  */
9673
   Pointer to realloc'd buffer, *BUFSIZ updated.  */
9085
 
9674
 
9086
char *
9675
char *
9087
elfcore_write_note (bfd *abfd,
9676
elfcore_write_note (bfd *abfd,
9088
		    char *buf,
9677
		    char *buf,
9089
		    int *bufsiz,
9678
		    int *bufsiz,
9090
		    const char *name,
9679
		    const char *name,
9091
		    int type,
9680
		    int type,
9092
		    const void *input,
9681
		    const void *input,
9093
		    int size)
9682
		    int size)
9094
{
9683
{
9095
  Elf_External_Note *xnp;
9684
  Elf_External_Note *xnp;
9096
  size_t namesz;
9685
  size_t namesz;
9097
  size_t newspace;
9686
  size_t newspace;
9098
  char *dest;
9687
  char *dest;
9099
 
9688
 
9100
  namesz = 0;
9689
  namesz = 0;
9101
  if (name != NULL)
9690
  if (name != NULL)
9102
    namesz = strlen (name) + 1;
9691
    namesz = strlen (name) + 1;
9103
 
9692
 
9104
  newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
9693
  newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
9105
 
9694
 
9106
  buf = (char *) realloc (buf, *bufsiz + newspace);
9695
  buf = (char *) realloc (buf, *bufsiz + newspace);
9107
  if (buf == NULL)
9696
  if (buf == NULL)
9108
    return buf;
9697
    return buf;
9109
  dest = buf + *bufsiz;
9698
  dest = buf + *bufsiz;
9110
  *bufsiz += newspace;
9699
  *bufsiz += newspace;
9111
  xnp = (Elf_External_Note *) dest;
9700
  xnp = (Elf_External_Note *) dest;
9112
  H_PUT_32 (abfd, namesz, xnp->namesz);
9701
  H_PUT_32 (abfd, namesz, xnp->namesz);
9113
  H_PUT_32 (abfd, size, xnp->descsz);
9702
  H_PUT_32 (abfd, size, xnp->descsz);
9114
  H_PUT_32 (abfd, type, xnp->type);
9703
  H_PUT_32 (abfd, type, xnp->type);
9115
  dest = xnp->name;
9704
  dest = xnp->name;
9116
  if (name != NULL)
9705
  if (name != NULL)
9117
    {
9706
    {
9118
      memcpy (dest, name, namesz);
9707
      memcpy (dest, name, namesz);
9119
      dest += namesz;
9708
      dest += namesz;
9120
      while (namesz & 3)
9709
      while (namesz & 3)
9121
	{
9710
	{
9122
	  *dest++ = '\0';
9711
	  *dest++ = '\0';
9123
	  ++namesz;
9712
	  ++namesz;
9124
	}
9713
	}
9125
    }
9714
    }
9126
  memcpy (dest, input, size);
9715
  memcpy (dest, input, size);
9127
  dest += size;
9716
  dest += size;
9128
  while (size & 3)
9717
  while (size & 3)
9129
    {
9718
    {
9130
      *dest++ = '\0';
9719
      *dest++ = '\0';
9131
      ++size;
9720
      ++size;
9132
    }
9721
    }
9133
  return buf;
9722
  return buf;
9134
}
9723
}
9135
 
9724
 
9136
char *
9725
char *
9137
elfcore_write_prpsinfo (bfd  *abfd,
9726
elfcore_write_prpsinfo (bfd  *abfd,
9138
			char *buf,
9727
			char *buf,
9139
			int  *bufsiz,
9728
			int  *bufsiz,
9140
			const char *fname,
9729
			const char *fname,
9141
			const char *psargs)
9730
			const char *psargs)
9142
{
9731
{
9143
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9732
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9144
 
9733
 
9145
  if (bed->elf_backend_write_core_note != NULL)
9734
  if (bed->elf_backend_write_core_note != NULL)
9146
    {
9735
    {
9147
      char *ret;
9736
      char *ret;
9148
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9737
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9149
						 NT_PRPSINFO, fname, psargs);
9738
						 NT_PRPSINFO, fname, psargs);
9150
      if (ret != NULL)
9739
      if (ret != NULL)
9151
	return ret;
9740
	return ret;
9152
    }
9741
    }
9153
 
9742
 
9154
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9743
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
9155
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9744
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
9156
  if (bed->s->elfclass == ELFCLASS32)
9745
  if (bed->s->elfclass == ELFCLASS32)
9157
    {
9746
    {
9158
#if defined (HAVE_PSINFO32_T)
9747
#if defined (HAVE_PSINFO32_T)
9159
      psinfo32_t data;
9748
      psinfo32_t data;
9160
      int note_type = NT_PSINFO;
9749
      int note_type = NT_PSINFO;
9161
#else
9750
#else
9162
      prpsinfo32_t data;
9751
      prpsinfo32_t data;
9163
      int note_type = NT_PRPSINFO;
9752
      int note_type = NT_PRPSINFO;
9164
#endif
9753
#endif
9165
 
9754
 
9166
      memset (&data, 0, sizeof (data));
9755
      memset (&data, 0, sizeof (data));
9167
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9756
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9168
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9757
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9169
      return elfcore_write_note (abfd, buf, bufsiz,
9758
      return elfcore_write_note (abfd, buf, bufsiz,
9170
				 "CORE", note_type, &data, sizeof (data));
9759
				 "CORE", note_type, &data, sizeof (data));
9171
    }
9760
    }
9172
  else
9761
  else
9173
#endif
9762
#endif
9174
    {
9763
    {
9175
#if defined (HAVE_PSINFO_T)
9764
#if defined (HAVE_PSINFO_T)
9176
      psinfo_t data;
9765
      psinfo_t data;
9177
      int note_type = NT_PSINFO;
9766
      int note_type = NT_PSINFO;
9178
#else
9767
#else
9179
      prpsinfo_t data;
9768
      prpsinfo_t data;
9180
      int note_type = NT_PRPSINFO;
9769
      int note_type = NT_PRPSINFO;
9181
#endif
9770
#endif
9182
 
9771
 
9183
      memset (&data, 0, sizeof (data));
9772
      memset (&data, 0, sizeof (data));
9184
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9773
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
9185
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9774
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
9186
      return elfcore_write_note (abfd, buf, bufsiz,
9775
      return elfcore_write_note (abfd, buf, bufsiz,
9187
				 "CORE", note_type, &data, sizeof (data));
9776
				 "CORE", note_type, &data, sizeof (data));
9188
    }
9777
    }
9189
#endif	/* PSINFO_T or PRPSINFO_T */
9778
#endif	/* PSINFO_T or PRPSINFO_T */
9190
 
9779
 
9191
  free (buf);
9780
  free (buf);
9192
  return NULL;
9781
  return NULL;
9193
}
9782
}
9194
 
9783
 
9195
char *
9784
char *
9196
elfcore_write_linux_prpsinfo32
9785
elfcore_write_linux_prpsinfo32
9197
  (bfd *abfd, char *buf, int *bufsiz,
9786
  (bfd *abfd, char *buf, int *bufsiz,
9198
   const struct elf_internal_linux_prpsinfo *prpsinfo)
9787
   const struct elf_internal_linux_prpsinfo *prpsinfo)
9199
{
9788
{
9200
  struct elf_external_linux_prpsinfo32 data;
9789
  struct elf_external_linux_prpsinfo32 data;
9201
 
9790
 
9202
  memset (&data, 0, sizeof (data));
9791
  memset (&data, 0, sizeof (data));
9203
  LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data);
9792
  LINUX_PRPSINFO32_SWAP_FIELDS (abfd, prpsinfo, data);
9204
 
9793
 
9205
  return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
9794
  return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO,
9206
			     &data, sizeof (data));
9795
			     &data, sizeof (data));
9207
}
9796
}
9208
 
9797
 
9209
char *
9798
char *
9210
elfcore_write_linux_prpsinfo64
9799
elfcore_write_linux_prpsinfo64
9211
  (bfd *abfd, char *buf, int *bufsiz,
9800
  (bfd *abfd, char *buf, int *bufsiz,
9212
   const struct elf_internal_linux_prpsinfo *prpsinfo)
9801
   const struct elf_internal_linux_prpsinfo *prpsinfo)
9213
{
9802
{
9214
  struct elf_external_linux_prpsinfo64 data;
9803
  struct elf_external_linux_prpsinfo64 data;
9215
 
9804
 
9216
  memset (&data, 0, sizeof (data));
9805
  memset (&data, 0, sizeof (data));
9217
  LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data);
9806
  LINUX_PRPSINFO64_SWAP_FIELDS (abfd, prpsinfo, data);
9218
 
9807
 
9219
  return elfcore_write_note (abfd, buf, bufsiz,
9808
  return elfcore_write_note (abfd, buf, bufsiz,
9220
			     "CORE", NT_PRPSINFO, &data, sizeof (data));
9809
			     "CORE", NT_PRPSINFO, &data, sizeof (data));
9221
}
9810
}
9222
 
9811
 
9223
char *
9812
char *
9224
elfcore_write_prstatus (bfd *abfd,
9813
elfcore_write_prstatus (bfd *abfd,
9225
			char *buf,
9814
			char *buf,
9226
			int *bufsiz,
9815
			int *bufsiz,
9227
			long pid,
9816
			long pid,
9228
			int cursig,
9817
			int cursig,
9229
			const void *gregs)
9818
			const void *gregs)
9230
{
9819
{
9231
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9820
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9232
 
9821
 
9233
  if (bed->elf_backend_write_core_note != NULL)
9822
  if (bed->elf_backend_write_core_note != NULL)
9234
    {
9823
    {
9235
      char *ret;
9824
      char *ret;
9236
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9825
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
9237
						 NT_PRSTATUS,
9826
						 NT_PRSTATUS,
9238
						 pid, cursig, gregs);
9827
						 pid, cursig, gregs);
9239
      if (ret != NULL)
9828
      if (ret != NULL)
9240
	return ret;
9829
	return ret;
9241
    }
9830
    }
9242
 
9831
 
9243
#if defined (HAVE_PRSTATUS_T)
9832
#if defined (HAVE_PRSTATUS_T)
9244
#if defined (HAVE_PRSTATUS32_T)
9833
#if defined (HAVE_PRSTATUS32_T)
9245
  if (bed->s->elfclass == ELFCLASS32)
9834
  if (bed->s->elfclass == ELFCLASS32)
9246
    {
9835
    {
9247
      prstatus32_t prstat;
9836
      prstatus32_t prstat;
9248
 
9837
 
9249
      memset (&prstat, 0, sizeof (prstat));
9838
      memset (&prstat, 0, sizeof (prstat));
9250
      prstat.pr_pid = pid;
9839
      prstat.pr_pid = pid;
9251
      prstat.pr_cursig = cursig;
9840
      prstat.pr_cursig = cursig;
9252
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9841
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9253
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9842
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9254
				 NT_PRSTATUS, &prstat, sizeof (prstat));
9843
				 NT_PRSTATUS, &prstat, sizeof (prstat));
9255
    }
9844
    }
9256
  else
9845
  else
9257
#endif
9846
#endif
9258
    {
9847
    {
9259
      prstatus_t prstat;
9848
      prstatus_t prstat;
9260
 
9849
 
9261
      memset (&prstat, 0, sizeof (prstat));
9850
      memset (&prstat, 0, sizeof (prstat));
9262
      prstat.pr_pid = pid;
9851
      prstat.pr_pid = pid;
9263
      prstat.pr_cursig = cursig;
9852
      prstat.pr_cursig = cursig;
9264
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9853
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
9265
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9854
      return elfcore_write_note (abfd, buf, bufsiz, "CORE",
9266
				 NT_PRSTATUS, &prstat, sizeof (prstat));
9855
				 NT_PRSTATUS, &prstat, sizeof (prstat));
9267
    }
9856
    }
9268
#endif /* HAVE_PRSTATUS_T */
9857
#endif /* HAVE_PRSTATUS_T */
9269
 
9858
 
9270
  free (buf);
9859
  free (buf);
9271
  return NULL;
9860
  return NULL;
9272
}
9861
}
9273
 
9862
 
9274
#if defined (HAVE_LWPSTATUS_T)
9863
#if defined (HAVE_LWPSTATUS_T)
9275
char *
9864
char *
9276
elfcore_write_lwpstatus (bfd *abfd,
9865
elfcore_write_lwpstatus (bfd *abfd,
9277
			 char *buf,
9866
			 char *buf,
9278
			 int *bufsiz,
9867
			 int *bufsiz,
9279
			 long pid,
9868
			 long pid,
9280
			 int cursig,
9869
			 int cursig,
9281
			 const void *gregs)
9870
			 const void *gregs)
9282
{
9871
{
9283
  lwpstatus_t lwpstat;
9872
  lwpstatus_t lwpstat;
9284
  const char *note_name = "CORE";
9873
  const char *note_name = "CORE";
9285
 
9874
 
9286
  memset (&lwpstat, 0, sizeof (lwpstat));
9875
  memset (&lwpstat, 0, sizeof (lwpstat));
9287
  lwpstat.pr_lwpid  = pid >> 16;
9876
  lwpstat.pr_lwpid  = pid >> 16;
9288
  lwpstat.pr_cursig = cursig;
9877
  lwpstat.pr_cursig = cursig;
9289
#if defined (HAVE_LWPSTATUS_T_PR_REG)
9878
#if defined (HAVE_LWPSTATUS_T_PR_REG)
9290
  memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
9879
  memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
9291
#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9880
#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
9292
#if !defined(gregs)
9881
#if !defined(gregs)
9293
  memcpy (lwpstat.pr_context.uc_mcontext.gregs,
9882
  memcpy (lwpstat.pr_context.uc_mcontext.gregs,
9294
	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
9883
	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
9295
#else
9884
#else
9296
  memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
9885
  memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
9297
	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
9886
	  gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
9298
#endif
9887
#endif
9299
#endif
9888
#endif
9300
  return elfcore_write_note (abfd, buf, bufsiz, note_name,
9889
  return elfcore_write_note (abfd, buf, bufsiz, note_name,
9301
			     NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
9890
			     NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
9302
}
9891
}
9303
#endif /* HAVE_LWPSTATUS_T */
9892
#endif /* HAVE_LWPSTATUS_T */
9304
 
9893
 
9305
#if defined (HAVE_PSTATUS_T)
9894
#if defined (HAVE_PSTATUS_T)
9306
char *
9895
char *
9307
elfcore_write_pstatus (bfd *abfd,
9896
elfcore_write_pstatus (bfd *abfd,
9308
		       char *buf,
9897
		       char *buf,
9309
		       int *bufsiz,
9898
		       int *bufsiz,
9310
		       long pid,
9899
		       long pid,
9311
		       int cursig ATTRIBUTE_UNUSED,
9900
		       int cursig ATTRIBUTE_UNUSED,
9312
		       const void *gregs ATTRIBUTE_UNUSED)
9901
		       const void *gregs ATTRIBUTE_UNUSED)
9313
{
9902
{
9314
  const char *note_name = "CORE";
9903
  const char *note_name = "CORE";
9315
#if defined (HAVE_PSTATUS32_T)
9904
#if defined (HAVE_PSTATUS32_T)
9316
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9905
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9317
 
9906
 
9318
  if (bed->s->elfclass == ELFCLASS32)
9907
  if (bed->s->elfclass == ELFCLASS32)
9319
    {
9908
    {
9320
      pstatus32_t pstat;
9909
      pstatus32_t pstat;
9321
 
9910
 
9322
      memset (&pstat, 0, sizeof (pstat));
9911
      memset (&pstat, 0, sizeof (pstat));
9323
      pstat.pr_pid = pid & 0xffff;
9912
      pstat.pr_pid = pid & 0xffff;
9324
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9913
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9325
				NT_PSTATUS, &pstat, sizeof (pstat));
9914
				NT_PSTATUS, &pstat, sizeof (pstat));
9326
      return buf;
9915
      return buf;
9327
    }
9916
    }
9328
  else
9917
  else
9329
#endif
9918
#endif
9330
    {
9919
    {
9331
      pstatus_t pstat;
9920
      pstatus_t pstat;
9332
 
9921
 
9333
      memset (&pstat, 0, sizeof (pstat));
9922
      memset (&pstat, 0, sizeof (pstat));
9334
      pstat.pr_pid = pid & 0xffff;
9923
      pstat.pr_pid = pid & 0xffff;
9335
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9924
      buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
9336
				NT_PSTATUS, &pstat, sizeof (pstat));
9925
				NT_PSTATUS, &pstat, sizeof (pstat));
9337
      return buf;
9926
      return buf;
9338
    }
9927
    }
9339
}
9928
}
9340
#endif /* HAVE_PSTATUS_T */
9929
#endif /* HAVE_PSTATUS_T */
9341
 
9930
 
9342
char *
9931
char *
9343
elfcore_write_prfpreg (bfd *abfd,
9932
elfcore_write_prfpreg (bfd *abfd,
9344
		       char *buf,
9933
		       char *buf,
9345
		       int *bufsiz,
9934
		       int *bufsiz,
9346
		       const void *fpregs,
9935
		       const void *fpregs,
9347
		       int size)
9936
		       int size)
9348
{
9937
{
9349
  const char *note_name = "CORE";
9938
  const char *note_name = "CORE";
9350
  return elfcore_write_note (abfd, buf, bufsiz,
9939
  return elfcore_write_note (abfd, buf, bufsiz,
9351
			     note_name, NT_FPREGSET, fpregs, size);
9940
			     note_name, NT_FPREGSET, fpregs, size);
9352
}
9941
}
9353
 
9942
 
9354
char *
9943
char *
9355
elfcore_write_prxfpreg (bfd *abfd,
9944
elfcore_write_prxfpreg (bfd *abfd,
9356
			char *buf,
9945
			char *buf,
9357
			int *bufsiz,
9946
			int *bufsiz,
9358
			const void *xfpregs,
9947
			const void *xfpregs,
9359
			int size)
9948
			int size)
9360
{
9949
{
9361
  char *note_name = "LINUX";
9950
  char *note_name = "LINUX";
9362
  return elfcore_write_note (abfd, buf, bufsiz,
9951
  return elfcore_write_note (abfd, buf, bufsiz,
9363
			     note_name, NT_PRXFPREG, xfpregs, size);
9952
			     note_name, NT_PRXFPREG, xfpregs, size);
9364
}
9953
}
9365
 
9954
 
9366
char *
9955
char *
9367
elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
9956
elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
9368
			 const void *xfpregs, int size)
9957
			 const void *xfpregs, int size)
9369
{
9958
{
-
 
9959
  char *note_name;
-
 
9960
  if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
-
 
9961
    note_name = "FreeBSD";
-
 
9962
  else
9370
  char *note_name = "LINUX";
9963
    note_name = "LINUX";
9371
  return elfcore_write_note (abfd, buf, bufsiz,
9964
  return elfcore_write_note (abfd, buf, bufsiz,
9372
			     note_name, NT_X86_XSTATE, xfpregs, size);
9965
			     note_name, NT_X86_XSTATE, xfpregs, size);
9373
}
9966
}
9374
 
9967
 
9375
char *
9968
char *
9376
elfcore_write_ppc_vmx (bfd *abfd,
9969
elfcore_write_ppc_vmx (bfd *abfd,
9377
		       char *buf,
9970
		       char *buf,
9378
		       int *bufsiz,
9971
		       int *bufsiz,
9379
		       const void *ppc_vmx,
9972
		       const void *ppc_vmx,
9380
		       int size)
9973
		       int size)
9381
{
9974
{
9382
  char *note_name = "LINUX";
9975
  char *note_name = "LINUX";
9383
  return elfcore_write_note (abfd, buf, bufsiz,
9976
  return elfcore_write_note (abfd, buf, bufsiz,
9384
			     note_name, NT_PPC_VMX, ppc_vmx, size);
9977
			     note_name, NT_PPC_VMX, ppc_vmx, size);
9385
}
9978
}
9386
 
9979
 
9387
char *
9980
char *
9388
elfcore_write_ppc_vsx (bfd *abfd,
9981
elfcore_write_ppc_vsx (bfd *abfd,
9389
                       char *buf,
9982
                       char *buf,
9390
                       int *bufsiz,
9983
                       int *bufsiz,
9391
                       const void *ppc_vsx,
9984
                       const void *ppc_vsx,
9392
                       int size)
9985
                       int size)
9393
{
9986
{
9394
  char *note_name = "LINUX";
9987
  char *note_name = "LINUX";
9395
  return elfcore_write_note (abfd, buf, bufsiz,
9988
  return elfcore_write_note (abfd, buf, bufsiz,
9396
                             note_name, NT_PPC_VSX, ppc_vsx, size);
9989
                             note_name, NT_PPC_VSX, ppc_vsx, size);
9397
}
9990
}
9398
 
9991
 
9399
static char *
9992
static char *
9400
elfcore_write_s390_high_gprs (bfd *abfd,
9993
elfcore_write_s390_high_gprs (bfd *abfd,
9401
			      char *buf,
9994
			      char *buf,
9402
			      int *bufsiz,
9995
			      int *bufsiz,
9403
			      const void *s390_high_gprs,
9996
			      const void *s390_high_gprs,
9404
			      int size)
9997
			      int size)
9405
{
9998
{
9406
  char *note_name = "LINUX";
9999
  char *note_name = "LINUX";
9407
  return elfcore_write_note (abfd, buf, bufsiz,
10000
  return elfcore_write_note (abfd, buf, bufsiz,
9408
                             note_name, NT_S390_HIGH_GPRS,
10001
                             note_name, NT_S390_HIGH_GPRS,
9409
			     s390_high_gprs, size);
10002
			     s390_high_gprs, size);
9410
}
10003
}
9411
 
10004
 
9412
char *
10005
char *
9413
elfcore_write_s390_timer (bfd *abfd,
10006
elfcore_write_s390_timer (bfd *abfd,
9414
                          char *buf,
10007
                          char *buf,
9415
                          int *bufsiz,
10008
                          int *bufsiz,
9416
                          const void *s390_timer,
10009
                          const void *s390_timer,
9417
                          int size)
10010
                          int size)
9418
{
10011
{
9419
  char *note_name = "LINUX";
10012
  char *note_name = "LINUX";
9420
  return elfcore_write_note (abfd, buf, bufsiz,
10013
  return elfcore_write_note (abfd, buf, bufsiz,
9421
                             note_name, NT_S390_TIMER, s390_timer, size);
10014
                             note_name, NT_S390_TIMER, s390_timer, size);
9422
}
10015
}
9423
 
10016
 
9424
char *
10017
char *
9425
elfcore_write_s390_todcmp (bfd *abfd,
10018
elfcore_write_s390_todcmp (bfd *abfd,
9426
                           char *buf,
10019
                           char *buf,
9427
                           int *bufsiz,
10020
                           int *bufsiz,
9428
                           const void *s390_todcmp,
10021
                           const void *s390_todcmp,
9429
                           int size)
10022
                           int size)
9430
{
10023
{
9431
  char *note_name = "LINUX";
10024
  char *note_name = "LINUX";
9432
  return elfcore_write_note (abfd, buf, bufsiz,
10025
  return elfcore_write_note (abfd, buf, bufsiz,
9433
                             note_name, NT_S390_TODCMP, s390_todcmp, size);
10026
                             note_name, NT_S390_TODCMP, s390_todcmp, size);
9434
}
10027
}
9435
 
10028
 
9436
char *
10029
char *
9437
elfcore_write_s390_todpreg (bfd *abfd,
10030
elfcore_write_s390_todpreg (bfd *abfd,
9438
                            char *buf,
10031
                            char *buf,
9439
                            int *bufsiz,
10032
                            int *bufsiz,
9440
                            const void *s390_todpreg,
10033
                            const void *s390_todpreg,
9441
                            int size)
10034
                            int size)
9442
{
10035
{
9443
  char *note_name = "LINUX";
10036
  char *note_name = "LINUX";
9444
  return elfcore_write_note (abfd, buf, bufsiz,
10037
  return elfcore_write_note (abfd, buf, bufsiz,
9445
                             note_name, NT_S390_TODPREG, s390_todpreg, size);
10038
                             note_name, NT_S390_TODPREG, s390_todpreg, size);
9446
}
10039
}
9447
 
10040
 
9448
char *
10041
char *
9449
elfcore_write_s390_ctrs (bfd *abfd,
10042
elfcore_write_s390_ctrs (bfd *abfd,
9450
                         char *buf,
10043
                         char *buf,
9451
                         int *bufsiz,
10044
                         int *bufsiz,
9452
                         const void *s390_ctrs,
10045
                         const void *s390_ctrs,
9453
                         int size)
10046
                         int size)
9454
{
10047
{
9455
  char *note_name = "LINUX";
10048
  char *note_name = "LINUX";
9456
  return elfcore_write_note (abfd, buf, bufsiz,
10049
  return elfcore_write_note (abfd, buf, bufsiz,
9457
                             note_name, NT_S390_CTRS, s390_ctrs, size);
10050
                             note_name, NT_S390_CTRS, s390_ctrs, size);
9458
}
10051
}
9459
 
10052
 
9460
char *
10053
char *
9461
elfcore_write_s390_prefix (bfd *abfd,
10054
elfcore_write_s390_prefix (bfd *abfd,
9462
                           char *buf,
10055
                           char *buf,
9463
                           int *bufsiz,
10056
                           int *bufsiz,
9464
                           const void *s390_prefix,
10057
                           const void *s390_prefix,
9465
                           int size)
10058
                           int size)
9466
{
10059
{
9467
  char *note_name = "LINUX";
10060
  char *note_name = "LINUX";
9468
  return elfcore_write_note (abfd, buf, bufsiz,
10061
  return elfcore_write_note (abfd, buf, bufsiz,
9469
                             note_name, NT_S390_PREFIX, s390_prefix, size);
10062
                             note_name, NT_S390_PREFIX, s390_prefix, size);
9470
}
10063
}
9471
 
10064
 
9472
char *
10065
char *
9473
elfcore_write_s390_last_break (bfd *abfd,
10066
elfcore_write_s390_last_break (bfd *abfd,
9474
			       char *buf,
10067
			       char *buf,
9475
			       int *bufsiz,
10068
			       int *bufsiz,
9476
			       const void *s390_last_break,
10069
			       const void *s390_last_break,
9477
			       int size)
10070
			       int size)
9478
{
10071
{
9479
  char *note_name = "LINUX";
10072
  char *note_name = "LINUX";
9480
  return elfcore_write_note (abfd, buf, bufsiz,
10073
  return elfcore_write_note (abfd, buf, bufsiz,
9481
                             note_name, NT_S390_LAST_BREAK,
10074
                             note_name, NT_S390_LAST_BREAK,
9482
			     s390_last_break, size);
10075
			     s390_last_break, size);
9483
}
10076
}
9484
 
10077
 
9485
char *
10078
char *
9486
elfcore_write_s390_system_call (bfd *abfd,
10079
elfcore_write_s390_system_call (bfd *abfd,
9487
				char *buf,
10080
				char *buf,
9488
				int *bufsiz,
10081
				int *bufsiz,
9489
				const void *s390_system_call,
10082
				const void *s390_system_call,
9490
				int size)
10083
				int size)
9491
{
10084
{
9492
  char *note_name = "LINUX";
10085
  char *note_name = "LINUX";
9493
  return elfcore_write_note (abfd, buf, bufsiz,
10086
  return elfcore_write_note (abfd, buf, bufsiz,
9494
                             note_name, NT_S390_SYSTEM_CALL,
10087
                             note_name, NT_S390_SYSTEM_CALL,
9495
			     s390_system_call, size);
10088
			     s390_system_call, size);
9496
}
10089
}
9497
 
10090
 
9498
char *
10091
char *
9499
elfcore_write_s390_tdb (bfd *abfd,
10092
elfcore_write_s390_tdb (bfd *abfd,
9500
			char *buf,
10093
			char *buf,
9501
			int *bufsiz,
10094
			int *bufsiz,
9502
			const void *s390_tdb,
10095
			const void *s390_tdb,
9503
			int size)
10096
			int size)
9504
{
10097
{
9505
  char *note_name = "LINUX";
10098
  char *note_name = "LINUX";
9506
  return elfcore_write_note (abfd, buf, bufsiz,
10099
  return elfcore_write_note (abfd, buf, bufsiz,
9507
                             note_name, NT_S390_TDB, s390_tdb, size);
10100
                             note_name, NT_S390_TDB, s390_tdb, size);
9508
}
10101
}
9509
 
10102
 
9510
char *
10103
char *
-
 
10104
elfcore_write_s390_vxrs_low (bfd *abfd,
-
 
10105
			     char *buf,
-
 
10106
			     int *bufsiz,
-
 
10107
			     const void *s390_vxrs_low,
-
 
10108
			     int size)
-
 
10109
{
-
 
10110
  char *note_name = "LINUX";
-
 
10111
  return elfcore_write_note (abfd, buf, bufsiz,
-
 
10112
			     note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size);
-
 
10113
}
-
 
10114
 
-
 
10115
char *
-
 
10116
elfcore_write_s390_vxrs_high (bfd *abfd,
-
 
10117
			     char *buf,
-
 
10118
			     int *bufsiz,
-
 
10119
			     const void *s390_vxrs_high,
-
 
10120
			     int size)
-
 
10121
{
-
 
10122
  char *note_name = "LINUX";
-
 
10123
  return elfcore_write_note (abfd, buf, bufsiz,
-
 
10124
			     note_name, NT_S390_VXRS_HIGH,
-
 
10125
			     s390_vxrs_high, size);
-
 
10126
}
-
 
10127
 
-
 
10128
char *
9511
elfcore_write_arm_vfp (bfd *abfd,
10129
elfcore_write_arm_vfp (bfd *abfd,
9512
		       char *buf,
10130
		       char *buf,
9513
		       int *bufsiz,
10131
		       int *bufsiz,
9514
		       const void *arm_vfp,
10132
		       const void *arm_vfp,
9515
		       int size)
10133
		       int size)
9516
{
10134
{
9517
  char *note_name = "LINUX";
10135
  char *note_name = "LINUX";
9518
  return elfcore_write_note (abfd, buf, bufsiz,
10136
  return elfcore_write_note (abfd, buf, bufsiz,
9519
			     note_name, NT_ARM_VFP, arm_vfp, size);
10137
			     note_name, NT_ARM_VFP, arm_vfp, size);
9520
}
10138
}
9521
 
10139
 
9522
char *
10140
char *
9523
elfcore_write_aarch_tls (bfd *abfd,
10141
elfcore_write_aarch_tls (bfd *abfd,
9524
		       char *buf,
10142
		       char *buf,
9525
		       int *bufsiz,
10143
		       int *bufsiz,
9526
		       const void *aarch_tls,
10144
		       const void *aarch_tls,
9527
		       int size)
10145
		       int size)
9528
{
10146
{
9529
  char *note_name = "LINUX";
10147
  char *note_name = "LINUX";
9530
  return elfcore_write_note (abfd, buf, bufsiz,
10148
  return elfcore_write_note (abfd, buf, bufsiz,
9531
			     note_name, NT_ARM_TLS, aarch_tls, size);
10149
			     note_name, NT_ARM_TLS, aarch_tls, size);
9532
}
10150
}
9533
 
10151
 
9534
char *
10152
char *
9535
elfcore_write_aarch_hw_break (bfd *abfd,
10153
elfcore_write_aarch_hw_break (bfd *abfd,
9536
			    char *buf,
10154
			    char *buf,
9537
			    int *bufsiz,
10155
			    int *bufsiz,
9538
			    const void *aarch_hw_break,
10156
			    const void *aarch_hw_break,
9539
			    int size)
10157
			    int size)
9540
{
10158
{
9541
  char *note_name = "LINUX";
10159
  char *note_name = "LINUX";
9542
  return elfcore_write_note (abfd, buf, bufsiz,
10160
  return elfcore_write_note (abfd, buf, bufsiz,
9543
			     note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
10161
			     note_name, NT_ARM_HW_BREAK, aarch_hw_break, size);
9544
}
10162
}
9545
 
10163
 
9546
char *
10164
char *
9547
elfcore_write_aarch_hw_watch (bfd *abfd,
10165
elfcore_write_aarch_hw_watch (bfd *abfd,
9548
			    char *buf,
10166
			    char *buf,
9549
			    int *bufsiz,
10167
			    int *bufsiz,
9550
			    const void *aarch_hw_watch,
10168
			    const void *aarch_hw_watch,
9551
			    int size)
10169
			    int size)
9552
{
10170
{
9553
  char *note_name = "LINUX";
10171
  char *note_name = "LINUX";
9554
  return elfcore_write_note (abfd, buf, bufsiz,
10172
  return elfcore_write_note (abfd, buf, bufsiz,
9555
			     note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
10173
			     note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size);
9556
}
10174
}
9557
 
10175
 
9558
char *
10176
char *
9559
elfcore_write_register_note (bfd *abfd,
10177
elfcore_write_register_note (bfd *abfd,
9560
			     char *buf,
10178
			     char *buf,
9561
			     int *bufsiz,
10179
			     int *bufsiz,
9562
			     const char *section,
10180
			     const char *section,
9563
			     const void *data,
10181
			     const void *data,
9564
			     int size)
10182
			     int size)
9565
{
10183
{
9566
  if (strcmp (section, ".reg2") == 0)
10184
  if (strcmp (section, ".reg2") == 0)
9567
    return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
10185
    return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size);
9568
  if (strcmp (section, ".reg-xfp") == 0)
10186
  if (strcmp (section, ".reg-xfp") == 0)
9569
    return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
10187
    return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
9570
  if (strcmp (section, ".reg-xstate") == 0)
10188
  if (strcmp (section, ".reg-xstate") == 0)
9571
    return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
10189
    return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
9572
  if (strcmp (section, ".reg-ppc-vmx") == 0)
10190
  if (strcmp (section, ".reg-ppc-vmx") == 0)
9573
    return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
10191
    return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
9574
  if (strcmp (section, ".reg-ppc-vsx") == 0)
10192
  if (strcmp (section, ".reg-ppc-vsx") == 0)
9575
    return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
10193
    return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size);
9576
  if (strcmp (section, ".reg-s390-high-gprs") == 0)
10194
  if (strcmp (section, ".reg-s390-high-gprs") == 0)
9577
    return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
10195
    return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size);
9578
  if (strcmp (section, ".reg-s390-timer") == 0)
10196
  if (strcmp (section, ".reg-s390-timer") == 0)
9579
    return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
10197
    return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size);
9580
  if (strcmp (section, ".reg-s390-todcmp") == 0)
10198
  if (strcmp (section, ".reg-s390-todcmp") == 0)
9581
    return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
10199
    return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size);
9582
  if (strcmp (section, ".reg-s390-todpreg") == 0)
10200
  if (strcmp (section, ".reg-s390-todpreg") == 0)
9583
    return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
10201
    return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size);
9584
  if (strcmp (section, ".reg-s390-ctrs") == 0)
10202
  if (strcmp (section, ".reg-s390-ctrs") == 0)
9585
    return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
10203
    return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size);
9586
  if (strcmp (section, ".reg-s390-prefix") == 0)
10204
  if (strcmp (section, ".reg-s390-prefix") == 0)
9587
    return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
10205
    return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size);
9588
  if (strcmp (section, ".reg-s390-last-break") == 0)
10206
  if (strcmp (section, ".reg-s390-last-break") == 0)
9589
    return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
10207
    return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size);
9590
  if (strcmp (section, ".reg-s390-system-call") == 0)
10208
  if (strcmp (section, ".reg-s390-system-call") == 0)
9591
    return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
10209
    return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size);
9592
  if (strcmp (section, ".reg-s390-tdb") == 0)
10210
  if (strcmp (section, ".reg-s390-tdb") == 0)
9593
    return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
10211
    return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size);
-
 
10212
  if (strcmp (section, ".reg-s390-vxrs-low") == 0)
-
 
10213
    return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size);
-
 
10214
  if (strcmp (section, ".reg-s390-vxrs-high") == 0)
-
 
10215
    return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size);
9594
  if (strcmp (section, ".reg-arm-vfp") == 0)
10216
  if (strcmp (section, ".reg-arm-vfp") == 0)
9595
    return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
10217
    return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size);
9596
  if (strcmp (section, ".reg-aarch-tls") == 0)
10218
  if (strcmp (section, ".reg-aarch-tls") == 0)
9597
    return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
10219
    return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size);
9598
  if (strcmp (section, ".reg-aarch-hw-break") == 0)
10220
  if (strcmp (section, ".reg-aarch-hw-break") == 0)
9599
    return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
10221
    return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size);
9600
  if (strcmp (section, ".reg-aarch-hw-watch") == 0)
10222
  if (strcmp (section, ".reg-aarch-hw-watch") == 0)
9601
    return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
10223
    return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size);
9602
  return NULL;
10224
  return NULL;
9603
}
10225
}
9604
 
10226
 
9605
static bfd_boolean
10227
static bfd_boolean
9606
elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
10228
elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset)
9607
{
10229
{
9608
  char *p;
10230
  char *p;
9609
 
10231
 
9610
  p = buf;
10232
  p = buf;
9611
  while (p < buf + size)
10233
  while (p < buf + size)
9612
    {
10234
    {
9613
      /* FIXME: bad alignment assumption.  */
10235
      /* FIXME: bad alignment assumption.  */
9614
      Elf_External_Note *xnp = (Elf_External_Note *) p;
10236
      Elf_External_Note *xnp = (Elf_External_Note *) p;
9615
      Elf_Internal_Note in;
10237
      Elf_Internal_Note in;
9616
 
10238
 
9617
      if (offsetof (Elf_External_Note, name) > buf - p + size)
10239
      if (offsetof (Elf_External_Note, name) > buf - p + size)
9618
	return FALSE;
10240
	return FALSE;
9619
 
10241
 
9620
      in.type = H_GET_32 (abfd, xnp->type);
10242
      in.type = H_GET_32 (abfd, xnp->type);
9621
 
10243
 
9622
      in.namesz = H_GET_32 (abfd, xnp->namesz);
10244
      in.namesz = H_GET_32 (abfd, xnp->namesz);
9623
      in.namedata = xnp->name;
10245
      in.namedata = xnp->name;
9624
      if (in.namesz > buf - in.namedata + size)
10246
      if (in.namesz > buf - in.namedata + size)
9625
	return FALSE;
10247
	return FALSE;
9626
 
10248
 
9627
      in.descsz = H_GET_32 (abfd, xnp->descsz);
10249
      in.descsz = H_GET_32 (abfd, xnp->descsz);
9628
      in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
10250
      in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
9629
      in.descpos = offset + (in.descdata - buf);
10251
      in.descpos = offset + (in.descdata - buf);
9630
      if (in.descsz != 0
10252
      if (in.descsz != 0
9631
	  && (in.descdata >= buf + size
10253
	  && (in.descdata >= buf + size
9632
	      || in.descsz > buf - in.descdata + size))
10254
	      || in.descsz > buf - in.descdata + size))
9633
	return FALSE;
10255
	return FALSE;
9634
 
10256
 
9635
      switch (bfd_get_format (abfd))
10257
      switch (bfd_get_format (abfd))
9636
        {
10258
        {
9637
	default:
10259
	default:
9638
	  return TRUE;
10260
	  return TRUE;
9639
 
10261
 
9640
	case bfd_core:
10262
	case bfd_core:
9641
	  if (CONST_STRNEQ (in.namedata, "NetBSD-CORE"))
-
 
9642
	    {
10263
	  {
9643
	      if (! elfcore_grok_netbsd_note (abfd, &in))
10264
#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
9644
		return FALSE;
-
 
9645
	    }
10265
	    struct
9646
	  else if (CONST_STRNEQ (in.namedata, "OpenBSD"))
-
 
9647
	    {
10266
	    {
9648
	      if (! elfcore_grok_openbsd_note (abfd, &in))
10267
	      const char * string;
9649
		return FALSE;
10268
	      size_t len;
-
 
10269
	      bfd_boolean (* func)(bfd *, Elf_Internal_Note *);
9650
	    }
10270
	    }
9651
	  else if (CONST_STRNEQ (in.namedata, "QNX"))
10271
	    grokers[] =
9652
	    {
10272
	    {
-
 
10273
	      GROKER_ELEMENT ("", elfcore_grok_note),
-
 
10274
	      GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
-
 
10275
	      GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note),
9653
	      if (! elfcore_grok_nto_note (abfd, &in))
10276
	      GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
-
 
10277
	      GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note)
-
 
10278
	    };
9654
		return FALSE;
10279
#undef GROKER_ELEMENT
9655
	    }
10280
	    int i;
-
 
10281
 
9656
	  else if (CONST_STRNEQ (in.namedata, "SPU/"))
10282
	    for (i = ARRAY_SIZE (grokers); i--;)
9657
	    {
10283
	      {
-
 
10284
		if (in.namesz >= grokers[i].len
9658
	      if (! elfcore_grok_spu_note (abfd, &in))
10285
		    && strncmp (in.namedata, grokers[i].string,
9659
		return FALSE;
10286
				grokers[i].len) == 0)
9660
	    }
-
 
9661
	  else
-
 
9662
	    {
10287
		  {
9663
	      if (! elfcore_grok_note (abfd, &in))
10288
		    if (! grokers[i].func (abfd, & in))
9664
		return FALSE;
10289
		      return FALSE;
-
 
10290
		    break;
-
 
10291
		  }
9665
	    }
10292
	      }
9666
	  break;
10293
	    break;
-
 
10294
	  }
9667
 
10295
 
9668
	case bfd_object:
10296
	case bfd_object:
9669
	  if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
10297
	  if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
9670
	    {
10298
	    {
9671
	      if (! elfobj_grok_gnu_note (abfd, &in))
10299
	      if (! elfobj_grok_gnu_note (abfd, &in))
9672
		return FALSE;
10300
		return FALSE;
9673
	    }
10301
	    }
9674
	  else if (in.namesz == sizeof "stapsdt"
10302
	  else if (in.namesz == sizeof "stapsdt"
9675
		   && strcmp (in.namedata, "stapsdt") == 0)
10303
		   && strcmp (in.namedata, "stapsdt") == 0)
9676
	    {
10304
	    {
9677
	      if (! elfobj_grok_stapsdt_note (abfd, &in))
10305
	      if (! elfobj_grok_stapsdt_note (abfd, &in))
9678
		return FALSE;
10306
		return FALSE;
9679
	    }
10307
	    }
9680
	  break;
10308
	  break;
9681
	}
10309
	}
9682
 
10310
 
9683
      p = in.descdata + BFD_ALIGN (in.descsz, 4);
10311
      p = in.descdata + BFD_ALIGN (in.descsz, 4);
9684
    }
10312
    }
9685
 
10313
 
9686
  return TRUE;
10314
  return TRUE;
9687
}
10315
}
9688
 
10316
 
9689
static bfd_boolean
10317
static bfd_boolean
9690
elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
10318
elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size)
9691
{
10319
{
9692
  char *buf;
10320
  char *buf;
9693
 
10321
 
9694
  if (size <= 0)
10322
  if (size <= 0)
9695
    return TRUE;
10323
    return TRUE;
9696
 
10324
 
9697
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
10325
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
9698
    return FALSE;
10326
    return FALSE;
9699
 
10327
 
9700
  buf = (char *) bfd_malloc (size);
10328
  buf = (char *) bfd_malloc (size + 1);
9701
  if (buf == NULL)
10329
  if (buf == NULL)
9702
    return FALSE;
10330
    return FALSE;
-
 
10331
 
-
 
10332
  /* PR 17512: file: ec08f814
-
 
10333
     0-termintate the buffer so that string searches will not overflow.  */
-
 
10334
  buf[size] = 0;
9703
 
10335
 
9704
  if (bfd_bread (buf, size, abfd) != size
10336
  if (bfd_bread (buf, size, abfd) != size
9705
      || !elf_parse_notes (abfd, buf, size, offset))
10337
      || !elf_parse_notes (abfd, buf, size, offset))
9706
    {
10338
    {
9707
      free (buf);
10339
      free (buf);
9708
      return FALSE;
10340
      return FALSE;
9709
    }
10341
    }
9710
 
10342
 
9711
  free (buf);
10343
  free (buf);
9712
  return TRUE;
10344
  return TRUE;
9713
}
10345
}
9714

10346

9715
/* Providing external access to the ELF program header table.  */
10347
/* Providing external access to the ELF program header table.  */
9716
 
10348
 
9717
/* Return an upper bound on the number of bytes required to store a
10349
/* Return an upper bound on the number of bytes required to store a
9718
   copy of ABFD's program header table entries.  Return -1 if an error
10350
   copy of ABFD's program header table entries.  Return -1 if an error
9719
   occurs; bfd_get_error will return an appropriate code.  */
10351
   occurs; bfd_get_error will return an appropriate code.  */
9720
 
10352
 
9721
long
10353
long
9722
bfd_get_elf_phdr_upper_bound (bfd *abfd)
10354
bfd_get_elf_phdr_upper_bound (bfd *abfd)
9723
{
10355
{
9724
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
10356
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
9725
    {
10357
    {
9726
      bfd_set_error (bfd_error_wrong_format);
10358
      bfd_set_error (bfd_error_wrong_format);
9727
      return -1;
10359
      return -1;
9728
    }
10360
    }
9729
 
10361
 
9730
  return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
10362
  return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
9731
}
10363
}
9732
 
10364
 
9733
/* Copy ABFD's program header table entries to *PHDRS.  The entries
10365
/* Copy ABFD's program header table entries to *PHDRS.  The entries
9734
   will be stored as an array of Elf_Internal_Phdr structures, as
10366
   will be stored as an array of Elf_Internal_Phdr structures, as
9735
   defined in include/elf/internal.h.  To find out how large the
10367
   defined in include/elf/internal.h.  To find out how large the
9736
   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
10368
   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
9737
 
10369
 
9738
   Return the number of program header table entries read, or -1 if an
10370
   Return the number of program header table entries read, or -1 if an
9739
   error occurs; bfd_get_error will return an appropriate code.  */
10371
   error occurs; bfd_get_error will return an appropriate code.  */
9740
 
10372
 
9741
int
10373
int
9742
bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
10374
bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
9743
{
10375
{
9744
  int num_phdrs;
10376
  int num_phdrs;
9745
 
10377
 
9746
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
10378
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
9747
    {
10379
    {
9748
      bfd_set_error (bfd_error_wrong_format);
10380
      bfd_set_error (bfd_error_wrong_format);
9749
      return -1;
10381
      return -1;
9750
    }
10382
    }
9751
 
10383
 
9752
  num_phdrs = elf_elfheader (abfd)->e_phnum;
10384
  num_phdrs = elf_elfheader (abfd)->e_phnum;
9753
  memcpy (phdrs, elf_tdata (abfd)->phdr,
10385
  memcpy (phdrs, elf_tdata (abfd)->phdr,
9754
	  num_phdrs * sizeof (Elf_Internal_Phdr));
10386
	  num_phdrs * sizeof (Elf_Internal_Phdr));
9755
 
10387
 
9756
  return num_phdrs;
10388
  return num_phdrs;
9757
}
10389
}
9758
 
10390
 
9759
enum elf_reloc_type_class
10391
enum elf_reloc_type_class
9760
_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
10392
_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
9761
			   const asection *rel_sec ATTRIBUTE_UNUSED,
10393
			   const asection *rel_sec ATTRIBUTE_UNUSED,
9762
			   const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
10394
			   const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
9763
{
10395
{
9764
  return reloc_class_normal;
10396
  return reloc_class_normal;
9765
}
10397
}
9766
 
10398
 
9767
/* For RELA architectures, return the relocation value for a
10399
/* For RELA architectures, return the relocation value for a
9768
   relocation against a local symbol.  */
10400
   relocation against a local symbol.  */
9769
 
10401
 
9770
bfd_vma
10402
bfd_vma
9771
_bfd_elf_rela_local_sym (bfd *abfd,
10403
_bfd_elf_rela_local_sym (bfd *abfd,
9772
			 Elf_Internal_Sym *sym,
10404
			 Elf_Internal_Sym *sym,
9773
			 asection **psec,
10405
			 asection **psec,
9774
			 Elf_Internal_Rela *rel)
10406
			 Elf_Internal_Rela *rel)
9775
{
10407
{
9776
  asection *sec = *psec;
10408
  asection *sec = *psec;
9777
  bfd_vma relocation;
10409
  bfd_vma relocation;
9778
 
10410
 
9779
  relocation = (sec->output_section->vma
10411
  relocation = (sec->output_section->vma
9780
		+ sec->output_offset
10412
		+ sec->output_offset
9781
		+ sym->st_value);
10413
		+ sym->st_value);
9782
  if ((sec->flags & SEC_MERGE)
10414
  if ((sec->flags & SEC_MERGE)
9783
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
10415
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
9784
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
10416
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
9785
    {
10417
    {
9786
      rel->r_addend =
10418
      rel->r_addend =
9787
	_bfd_merged_section_offset (abfd, psec,
10419
	_bfd_merged_section_offset (abfd, psec,
9788
				    elf_section_data (sec)->sec_info,
10420
				    elf_section_data (sec)->sec_info,
9789
				    sym->st_value + rel->r_addend);
10421
				    sym->st_value + rel->r_addend);
9790
      if (sec != *psec)
10422
      if (sec != *psec)
9791
	{
10423
	{
9792
	  /* If we have changed the section, and our original section is
10424
	  /* If we have changed the section, and our original section is
9793
	     marked with SEC_EXCLUDE, it means that the original
10425
	     marked with SEC_EXCLUDE, it means that the original
9794
	     SEC_MERGE section has been completely subsumed in some
10426
	     SEC_MERGE section has been completely subsumed in some
9795
	     other SEC_MERGE section.  In this case, we need to leave
10427
	     other SEC_MERGE section.  In this case, we need to leave
9796
	     some info around for --emit-relocs.  */
10428
	     some info around for --emit-relocs.  */
9797
	  if ((sec->flags & SEC_EXCLUDE) != 0)
10429
	  if ((sec->flags & SEC_EXCLUDE) != 0)
9798
	    sec->kept_section = *psec;
10430
	    sec->kept_section = *psec;
9799
	  sec = *psec;
10431
	  sec = *psec;
9800
	}
10432
	}
9801
      rel->r_addend -= relocation;
10433
      rel->r_addend -= relocation;
9802
      rel->r_addend += sec->output_section->vma + sec->output_offset;
10434
      rel->r_addend += sec->output_section->vma + sec->output_offset;
9803
    }
10435
    }
9804
  return relocation;
10436
  return relocation;
9805
}
10437
}
9806
 
10438
 
9807
bfd_vma
10439
bfd_vma
9808
_bfd_elf_rel_local_sym (bfd *abfd,
10440
_bfd_elf_rel_local_sym (bfd *abfd,
9809
			Elf_Internal_Sym *sym,
10441
			Elf_Internal_Sym *sym,
9810
			asection **psec,
10442
			asection **psec,
9811
			bfd_vma addend)
10443
			bfd_vma addend)
9812
{
10444
{
9813
  asection *sec = *psec;
10445
  asection *sec = *psec;
9814
 
10446
 
9815
  if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
10447
  if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
9816
    return sym->st_value + addend;
10448
    return sym->st_value + addend;
9817
 
10449
 
9818
  return _bfd_merged_section_offset (abfd, psec,
10450
  return _bfd_merged_section_offset (abfd, psec,
9819
				     elf_section_data (sec)->sec_info,
10451
				     elf_section_data (sec)->sec_info,
9820
				     sym->st_value + addend);
10452
				     sym->st_value + addend);
9821
}
10453
}
9822
 
10454
 
9823
bfd_vma
10455
bfd_vma
9824
_bfd_elf_section_offset (bfd *abfd,
10456
_bfd_elf_section_offset (bfd *abfd,
9825
			 struct bfd_link_info *info,
10457
			 struct bfd_link_info *info,
9826
			 asection *sec,
10458
			 asection *sec,
9827
			 bfd_vma offset)
10459
			 bfd_vma offset)
9828
{
10460
{
9829
  switch (sec->sec_info_type)
10461
  switch (sec->sec_info_type)
9830
    {
10462
    {
9831
    case SEC_INFO_TYPE_STABS:
10463
    case SEC_INFO_TYPE_STABS:
9832
      return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
10464
      return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info,
9833
				       offset);
10465
				       offset);
9834
    case SEC_INFO_TYPE_EH_FRAME:
10466
    case SEC_INFO_TYPE_EH_FRAME:
9835
      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
10467
      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
9836
    default:
10468
    default:
9837
      if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
10469
      if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
9838
	{
10470
	{
9839
	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10471
	  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9840
	  bfd_size_type address_size = bed->s->arch_size / 8;
10472
	  bfd_size_type address_size = bed->s->arch_size / 8;
9841
	  offset = sec->size - offset - address_size;
10473
	  offset = sec->size - offset - address_size;
9842
	}
10474
	}
9843
      return offset;
10475
      return offset;
9844
    }
10476
    }
9845
}
10477
}
9846

10478

9847
/* Create a new BFD as if by bfd_openr.  Rather than opening a file,
10479
/* Create a new BFD as if by bfd_openr.  Rather than opening a file,
9848
   reconstruct an ELF file by reading the segments out of remote memory
10480
   reconstruct an ELF file by reading the segments out of remote memory
9849
   based on the ELF file header at EHDR_VMA and the ELF program headers it
10481
   based on the ELF file header at EHDR_VMA and the ELF program headers it
9850
   points to.  If not null, *LOADBASEP is filled in with the difference
10482
   points to.  If not null, *LOADBASEP is filled in with the difference
9851
   between the VMAs from which the segments were read, and the VMAs the
10483
   between the VMAs from which the segments were read, and the VMAs the
9852
   file headers (and hence BFD's idea of each section's VMA) put them at.
10484
   file headers (and hence BFD's idea of each section's VMA) put them at.
9853
 
10485
 
9854
   The function TARGET_READ_MEMORY is called to copy LEN bytes from the
10486
   The function TARGET_READ_MEMORY is called to copy LEN bytes from the
9855
   remote memory at target address VMA into the local buffer at MYADDR; it
10487
   remote memory at target address VMA into the local buffer at MYADDR; it
9856
   should return zero on success or an `errno' code on failure.  TEMPL must
10488
   should return zero on success or an `errno' code on failure.  TEMPL must
9857
   be a BFD for an ELF target with the word size and byte order found in
10489
   be a BFD for an ELF target with the word size and byte order found in
9858
   the remote memory.  */
10490
   the remote memory.  */
9859
 
10491
 
9860
bfd *
10492
bfd *
9861
bfd_elf_bfd_from_remote_memory
10493
bfd_elf_bfd_from_remote_memory
9862
  (bfd *templ,
10494
  (bfd *templ,
9863
   bfd_vma ehdr_vma,
10495
   bfd_vma ehdr_vma,
-
 
10496
   bfd_size_type size,
9864
   bfd_vma *loadbasep,
10497
   bfd_vma *loadbasep,
9865
   int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
10498
   int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
9866
{
10499
{
9867
  return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
10500
  return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory)
9868
    (templ, ehdr_vma, loadbasep, target_read_memory);
10501
    (templ, ehdr_vma, size, loadbasep, target_read_memory);
9869
}
10502
}
9870

10503

9871
long
10504
long
9872
_bfd_elf_get_synthetic_symtab (bfd *abfd,
10505
_bfd_elf_get_synthetic_symtab (bfd *abfd,
9873
			       long symcount ATTRIBUTE_UNUSED,
10506
			       long symcount ATTRIBUTE_UNUSED,
9874
			       asymbol **syms ATTRIBUTE_UNUSED,
10507
			       asymbol **syms ATTRIBUTE_UNUSED,
9875
			       long dynsymcount,
10508
			       long dynsymcount,
9876
			       asymbol **dynsyms,
10509
			       asymbol **dynsyms,
9877
			       asymbol **ret)
10510
			       asymbol **ret)
9878
{
10511
{
9879
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
10512
  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
9880
  asection *relplt;
10513
  asection *relplt;
9881
  asymbol *s;
10514
  asymbol *s;
9882
  const char *relplt_name;
10515
  const char *relplt_name;
9883
  bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
10516
  bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
9884
  arelent *p;
10517
  arelent *p;
9885
  long count, i, n;
10518
  long count, i, n;
9886
  size_t size;
10519
  size_t size;
9887
  Elf_Internal_Shdr *hdr;
10520
  Elf_Internal_Shdr *hdr;
9888
  char *names;
10521
  char *names;
9889
  asection *plt;
10522
  asection *plt;
9890
 
10523
 
9891
  *ret = NULL;
10524
  *ret = NULL;
9892
 
10525
 
9893
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
10526
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
9894
    return 0;
10527
    return 0;
9895
 
10528
 
9896
  if (dynsymcount <= 0)
10529
  if (dynsymcount <= 0)
9897
    return 0;
10530
    return 0;
9898
 
10531
 
9899
  if (!bed->plt_sym_val)
10532
  if (!bed->plt_sym_val)
9900
    return 0;
10533
    return 0;
9901
 
10534
 
9902
  relplt_name = bed->relplt_name;
10535
  relplt_name = bed->relplt_name;
9903
  if (relplt_name == NULL)
10536
  if (relplt_name == NULL)
9904
    relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
10537
    relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
9905
  relplt = bfd_get_section_by_name (abfd, relplt_name);
10538
  relplt = bfd_get_section_by_name (abfd, relplt_name);
9906
  if (relplt == NULL)
10539
  if (relplt == NULL)
9907
    return 0;
10540
    return 0;
9908
 
10541
 
9909
  hdr = &elf_section_data (relplt)->this_hdr;
10542
  hdr = &elf_section_data (relplt)->this_hdr;
9910
  if (hdr->sh_link != elf_dynsymtab (abfd)
10543
  if (hdr->sh_link != elf_dynsymtab (abfd)
9911
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
10544
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
9912
    return 0;
10545
    return 0;
9913
 
10546
 
9914
  plt = bfd_get_section_by_name (abfd, ".plt");
10547
  plt = bfd_get_section_by_name (abfd, ".plt");
9915
  if (plt == NULL)
10548
  if (plt == NULL)
9916
    return 0;
10549
    return 0;
9917
 
10550
 
9918
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
10551
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9919
  if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
10552
  if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
9920
    return -1;
10553
    return -1;
9921
 
10554
 
9922
  count = relplt->size / hdr->sh_entsize;
10555
  count = relplt->size / hdr->sh_entsize;
9923
  size = count * sizeof (asymbol);
10556
  size = count * sizeof (asymbol);
9924
  p = relplt->relocation;
10557
  p = relplt->relocation;
9925
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
10558
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9926
    {
10559
    {
9927
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
10560
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
9928
      if (p->addend != 0)
10561
      if (p->addend != 0)
9929
	{
10562
	{
9930
#ifdef BFD64
10563
#ifdef BFD64
9931
	  size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
10564
	  size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
9932
#else
10565
#else
9933
	  size += sizeof ("+0x") - 1 + 8;
10566
	  size += sizeof ("+0x") - 1 + 8;
9934
#endif
10567
#endif
9935
	}
10568
	}
9936
    }
10569
    }
9937
 
10570
 
9938
  s = *ret = (asymbol *) bfd_malloc (size);
10571
  s = *ret = (asymbol *) bfd_malloc (size);
9939
  if (s == NULL)
10572
  if (s == NULL)
9940
    return -1;
10573
    return -1;
9941
 
10574
 
9942
  names = (char *) (s + count);
10575
  names = (char *) (s + count);
9943
  p = relplt->relocation;
10576
  p = relplt->relocation;
9944
  n = 0;
10577
  n = 0;
9945
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
10578
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
9946
    {
10579
    {
9947
      size_t len;
10580
      size_t len;
9948
      bfd_vma addr;
10581
      bfd_vma addr;
9949
 
10582
 
9950
      addr = bed->plt_sym_val (i, plt, p);
10583
      addr = bed->plt_sym_val (i, plt, p);
9951
      if (addr == (bfd_vma) -1)
10584
      if (addr == (bfd_vma) -1)
9952
	continue;
10585
	continue;
9953
 
10586
 
9954
      *s = **p->sym_ptr_ptr;
10587
      *s = **p->sym_ptr_ptr;
9955
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
10588
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
9956
	 we are defining a symbol, ensure one of them is set.  */
10589
	 we are defining a symbol, ensure one of them is set.  */
9957
      if ((s->flags & BSF_LOCAL) == 0)
10590
      if ((s->flags & BSF_LOCAL) == 0)
9958
	s->flags |= BSF_GLOBAL;
10591
	s->flags |= BSF_GLOBAL;
9959
      s->flags |= BSF_SYNTHETIC;
10592
      s->flags |= BSF_SYNTHETIC;
9960
      s->section = plt;
10593
      s->section = plt;
9961
      s->value = addr - plt->vma;
10594
      s->value = addr - plt->vma;
9962
      s->name = names;
10595
      s->name = names;
9963
      s->udata.p = NULL;
10596
      s->udata.p = NULL;
9964
      len = strlen ((*p->sym_ptr_ptr)->name);
10597
      len = strlen ((*p->sym_ptr_ptr)->name);
9965
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
10598
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
9966
      names += len;
10599
      names += len;
9967
      if (p->addend != 0)
10600
      if (p->addend != 0)
9968
	{
10601
	{
9969
	  char buf[30], *a;
10602
	  char buf[30], *a;
9970
 
10603
 
9971
	  memcpy (names, "+0x", sizeof ("+0x") - 1);
10604
	  memcpy (names, "+0x", sizeof ("+0x") - 1);
9972
	  names += sizeof ("+0x") - 1;
10605
	  names += sizeof ("+0x") - 1;
9973
	  bfd_sprintf_vma (abfd, buf, p->addend);
10606
	  bfd_sprintf_vma (abfd, buf, p->addend);
9974
	  for (a = buf; *a == '0'; ++a)
10607
	  for (a = buf; *a == '0'; ++a)
9975
	    ;
10608
	    ;
9976
	  len = strlen (a);
10609
	  len = strlen (a);
9977
	  memcpy (names, a, len);
10610
	  memcpy (names, a, len);
9978
	  names += len;
10611
	  names += len;
9979
	}
10612
	}
9980
      memcpy (names, "@plt", sizeof ("@plt"));
10613
      memcpy (names, "@plt", sizeof ("@plt"));
9981
      names += sizeof ("@plt");
10614
      names += sizeof ("@plt");
9982
      ++s, ++n;
10615
      ++s, ++n;
9983
    }
10616
    }
9984
 
10617
 
9985
  return n;
10618
  return n;
9986
}
10619
}
9987
 
10620
 
9988
/* It is only used by x86-64 so far.  */
10621
/* It is only used by x86-64 so far.  */
9989
asection _bfd_elf_large_com_section
10622
asection _bfd_elf_large_com_section
9990
  = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
10623
  = BFD_FAKE_SECTION (_bfd_elf_large_com_section,
9991
		      SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
10624
		      SEC_IS_COMMON, NULL, "LARGE_COMMON", 0);
9992
 
10625
 
9993
void
10626
void
9994
_bfd_elf_set_osabi (bfd * abfd,
10627
_bfd_elf_post_process_headers (bfd * abfd,
9995
		    struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
10628
			       struct bfd_link_info * link_info ATTRIBUTE_UNUSED)
9996
{
10629
{
9997
  Elf_Internal_Ehdr * i_ehdrp;	/* ELF file header, internal form.  */
10630
  Elf_Internal_Ehdr * i_ehdrp;	/* ELF file header, internal form.  */
9998
 
10631
 
9999
  i_ehdrp = elf_elfheader (abfd);
10632
  i_ehdrp = elf_elfheader (abfd);
10000
 
10633
 
10001
  i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
10634
  i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
10002
 
10635
 
10003
  /* To make things simpler for the loader on Linux systems we set the
10636
  /* To make things simpler for the loader on Linux systems we set the
10004
     osabi field to ELFOSABI_GNU if the binary contains symbols of
10637
     osabi field to ELFOSABI_GNU if the binary contains symbols of
10005
     the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding.  */
10638
     the STT_GNU_IFUNC type or STB_GNU_UNIQUE binding.  */
10006
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
10639
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE
10007
      && elf_tdata (abfd)->has_gnu_symbols)
10640
      && elf_tdata (abfd)->has_gnu_symbols)
10008
    i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
10641
    i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
10009
}
10642
}
10010
 
10643
 
10011
 
10644
 
10012
/* Return TRUE for ELF symbol types that represent functions.
10645
/* Return TRUE for ELF symbol types that represent functions.
10013
   This is the default version of this function, which is sufficient for
10646
   This is the default version of this function, which is sufficient for
10014
   most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
10647
   most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
10015
 
10648
 
10016
bfd_boolean
10649
bfd_boolean
10017
_bfd_elf_is_function_type (unsigned int type)
10650
_bfd_elf_is_function_type (unsigned int type)
10018
{
10651
{
10019
  return (type == STT_FUNC
10652
  return (type == STT_FUNC
10020
	  || type == STT_GNU_IFUNC);
10653
	  || type == STT_GNU_IFUNC);
10021
}
10654
}
10022
 
10655
 
10023
/* If the ELF symbol SYM might be a function in SEC, return the
10656
/* If the ELF symbol SYM might be a function in SEC, return the
10024
   function size and set *CODE_OFF to the function's entry point,
10657
   function size and set *CODE_OFF to the function's entry point,
10025
   otherwise return zero.  */
10658
   otherwise return zero.  */
10026
 
10659
 
10027
bfd_size_type
10660
bfd_size_type
10028
_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
10661
_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
10029
			     bfd_vma *code_off)
10662
			     bfd_vma *code_off)
10030
{
10663
{
10031
  bfd_size_type size;
10664
  bfd_size_type size;
10032
 
10665
 
10033
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
10666
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
10034
		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
10667
		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
10035
      || sym->section != sec)
10668
      || sym->section != sec)
10036
    return 0;
10669
    return 0;
10037
 
10670
 
10038
  *code_off = sym->value;
10671
  *code_off = sym->value;
10039
  size = 0;
10672
  size = 0;
10040
  if (!(sym->flags & BSF_SYNTHETIC))
10673
  if (!(sym->flags & BSF_SYNTHETIC))
10041
    size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
10674
    size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
10042
  if (size == 0)
10675
  if (size == 0)
10043
    size = 1;
10676
    size = 1;
10044
  return size;
10677
  return size;
10045
}
10678
}