Coverage Report

Created: 2023-06-29 07:03

/src/binutils-gdb/bfd/section.c
Line
Count
Source (jump to first uncovered line)
1
/* Object file "section" support for the BFD library.
2
   Copyright (C) 1990-2023 Free Software Foundation, Inc.
3
   Written by Cygnus Support.
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
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
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
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
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
/*
23
SECTION
24
  Sections
25
26
  The raw data contained within a BFD is maintained through the
27
  section abstraction.  A single BFD may have any number of
28
  sections.  It keeps hold of them by pointing to the first;
29
  each one points to the next in the list.
30
31
  Sections are supported in BFD in <<section.c>>.
32
33
@menu
34
@* Section Input::
35
@* Section Output::
36
@* typedef asection::
37
@* section prototypes::
38
@end menu
39
40
INODE
41
Section Input, Section Output, Sections, Sections
42
SUBSECTION
43
  Section input
44
45
  When a BFD is opened for reading, the section structures are
46
  created and attached to the BFD.
47
48
  Each section has a name which describes the section in the
49
  outside world---for example, <<a.out>> would contain at least
50
  three sections, called <<.text>>, <<.data>> and <<.bss>>.
51
52
  Names need not be unique; for example a COFF file may have several
53
  sections named <<.data>>.
54
55
  Sometimes a BFD will contain more than the ``natural'' number of
56
  sections. A back end may attach other sections containing
57
  constructor data, or an application may add a section (using
58
  <<bfd_make_section>>) to the sections attached to an already open
59
  BFD. For example, the linker creates an extra section
60
  <<COMMON>> for each input file's BFD to hold information about
61
  common storage.
62
63
  The raw data is not necessarily read in when
64
  the section descriptor is created. Some targets may leave the
65
  data in place until a <<bfd_get_section_contents>> call is
66
  made. Other back ends may read in all the data at once.  For
67
  example, an S-record file has to be read once to determine the
68
  size of the data.
69
70
INODE
71
Section Output, typedef asection, Section Input, Sections
72
73
SUBSECTION
74
  Section output
75
76
  To write a new object style BFD, the various sections to be
77
  written have to be created. They are attached to the BFD in
78
  the same way as input sections; data is written to the
79
  sections using <<bfd_set_section_contents>>.
80
81
  Any program that creates or combines sections (e.g., the assembler
82
  and linker) must use the <<asection>> fields <<output_section>> and
83
  <<output_offset>> to indicate the file sections to which each
84
  section must be written.  (If the section is being created from
85
  scratch, <<output_section>> should probably point to the section
86
  itself and <<output_offset>> should probably be zero.)
87
88
  The data to be written comes from input sections attached
89
  (via <<output_section>> pointers) to
90
  the output sections.  The output section structure can be
91
  considered a filter for the input section: the output section
92
  determines the vma of the output data and the name, but the
93
  input section determines the offset into the output section of
94
  the data to be written.
95
96
  E.g., to create a section "O", starting at 0x100, 0x123 long,
97
  containing two subsections, "A" at offset 0x0 (i.e., at vma
98
  0x100) and "B" at offset 0x20 (i.e., at vma 0x120) the <<asection>>
99
  structures would look like:
100
101
|   section name          "A"
102
|     output_offset   0x00
103
|     size            0x20
104
|     output_section ----------->  section name    "O"
105
|                             |    vma             0x100
106
|   section name          "B" |    size            0x123
107
|     output_offset   0x20    |
108
|     size            0x103   |
109
|     output_section  --------|
110
111
SUBSECTION
112
  Link orders
113
114
  The data within a section is stored in a @dfn{link_order}.
115
  These are much like the fixups in <<gas>>.  The link_order
116
  abstraction allows a section to grow and shrink within itself.
117
118
  A link_order knows how big it is, and which is the next
119
  link_order and where the raw data for it is; it also points to
120
  a list of relocations which apply to it.
121
122
  The link_order is used by the linker to perform relaxing on
123
  final code.  The compiler creates code which is as big as
124
  necessary to make it work without relaxing, and the user can
125
  select whether to relax.  Sometimes relaxing takes a lot of
126
  time.  The linker runs around the relocations to see if any
127
  are attached to data which can be shrunk, if so it does it on
128
  a link_order by link_order basis.
129
130
*/
131
132
#include "sysdep.h"
133
#include "bfd.h"
134
#include "libbfd.h"
135
#include "bfdlink.h"
136
137
/*
138
DOCDD
139
INODE
140
  typedef asection, section prototypes, Section Output, Sections
141
SUBSECTION
142
  typedef asection
143
144
  Here is the section structure:
145
146
EXTERNAL
147
.{* Linenumber stuff.  *}
148
.typedef struct lineno_cache_entry
149
.{
150
.  unsigned int line_number;  {* Linenumber from start of function.  *}
151
.  union
152
.  {
153
.    struct bfd_symbol *sym;  {* Function name.  *}
154
.    bfd_vma offset;    {* Offset into section.  *}
155
.  } u;
156
.}
157
.alent;
158
.
159
160
CODE_FRAGMENT
161
.typedef struct bfd_section
162
.{
163
.  {* The name of the section; the name isn't a copy, the pointer is
164
.     the same as that passed to bfd_make_section.  *}
165
.  const char *name;
166
.
167
.  {* The next section in the list belonging to the BFD, or NULL.  *}
168
.  struct bfd_section *next;
169
.
170
.  {* The previous section in the list belonging to the BFD, or NULL.  *}
171
.  struct bfd_section *prev;
172
.
173
.  {* A unique sequence number.  *}
174
.  unsigned int id;
175
.
176
.  {* A unique section number which can be used by assembler to
177
.     distinguish different sections with the same section name.  *}
178
.  unsigned int section_id;
179
.
180
.  {* Which section in the bfd; 0..n-1 as sections are created in a bfd.  *}
181
.  unsigned int index;
182
.
183
.  {* The field flags contains attributes of the section. Some
184
.     flags are read in from the object file, and some are
185
.     synthesized from other information.  *}
186
.  flagword flags;
187
.
188
.#define SEC_NO_FLAGS                      0x0
189
.
190
.  {* Tells the OS to allocate space for this section when loading.
191
.     This is clear for a section containing debug information only.  *}
192
.#define SEC_ALLOC                         0x1
193
.
194
.  {* Tells the OS to load the section from the file when loading.
195
.     This is clear for a .bss section.  *}
196
.#define SEC_LOAD                          0x2
197
.
198
.  {* The section contains data still to be relocated, so there is
199
.     some relocation information too.  *}
200
.#define SEC_RELOC                         0x4
201
.
202
.  {* A signal to the OS that the section contains read only data.  *}
203
.#define SEC_READONLY                      0x8
204
.
205
.  {* The section contains code only.  *}
206
.#define SEC_CODE                         0x10
207
.
208
.  {* The section contains data only.  *}
209
.#define SEC_DATA                         0x20
210
.
211
.  {* The section will reside in ROM.  *}
212
.#define SEC_ROM                          0x40
213
.
214
.  {* The section contains constructor information. This section
215
.     type is used by the linker to create lists of constructors and
216
.     destructors used by <<g++>>. When a back end sees a symbol
217
.     which should be used in a constructor list, it creates a new
218
.     section for the type of name (e.g., <<__CTOR_LIST__>>), attaches
219
.     the symbol to it, and builds a relocation. To build the lists
220
.     of constructors, all the linker has to do is catenate all the
221
.     sections called <<__CTOR_LIST__>> and relocate the data
222
.     contained within - exactly the operations it would peform on
223
.     standard data.  *}
224
.#define SEC_CONSTRUCTOR                  0x80
225
.
226
.  {* The section has contents - a data section could be
227
.     <<SEC_ALLOC>> | <<SEC_HAS_CONTENTS>>; a debug section could be
228
.     <<SEC_HAS_CONTENTS>>  *}
229
.#define SEC_HAS_CONTENTS                0x100
230
.
231
.  {* An instruction to the linker to not output the section
232
.     even if it has information which would normally be written.  *}
233
.#define SEC_NEVER_LOAD                  0x200
234
.
235
.  {* The section contains thread local data.  *}
236
.#define SEC_THREAD_LOCAL                0x400
237
.
238
.  {* The section's size is fixed.  Generic linker code will not
239
.     recalculate it and it is up to whoever has set this flag to
240
.     get the size right.  *}
241
.#define SEC_FIXED_SIZE                  0x800
242
.
243
.  {* The section contains common symbols (symbols may be defined
244
.     multiple times, the value of a symbol is the amount of
245
.     space it requires, and the largest symbol value is the one
246
.     used).  Most targets have exactly one of these (which we
247
.     translate to bfd_com_section_ptr), but ECOFF has two.  *}
248
.#define SEC_IS_COMMON                  0x1000
249
.
250
.  {* The section contains only debugging information.  For
251
.     example, this is set for ELF .debug and .stab sections.
252
.     strip tests this flag to see if a section can be
253
.     discarded.  *}
254
.#define SEC_DEBUGGING                  0x2000
255
.
256
.  {* The contents of this section are held in memory pointed to
257
.     by the contents field.  This is checked by bfd_get_section_contents,
258
.     and the data is retrieved from memory if appropriate.  *}
259
.#define SEC_IN_MEMORY                  0x4000
260
.
261
.  {* The contents of this section are to be excluded by the
262
.     linker for executable and shared objects unless those
263
.     objects are to be further relocated.  *}
264
.#define SEC_EXCLUDE                    0x8000
265
.
266
.  {* The contents of this section are to be sorted based on the sum of
267
.     the symbol and addend values specified by the associated relocation
268
.     entries.  Entries without associated relocation entries will be
269
.     appended to the end of the section in an unspecified order.  *}
270
.#define SEC_SORT_ENTRIES              0x10000
271
.
272
.  {* When linking, duplicate sections of the same name should be
273
.     discarded, rather than being combined into a single section as
274
.     is usually done.  This is similar to how common symbols are
275
.     handled.  See SEC_LINK_DUPLICATES below.  *}
276
.#define SEC_LINK_ONCE                 0x20000
277
.
278
.  {* If SEC_LINK_ONCE is set, this bitfield describes how the linker
279
.     should handle duplicate sections.  *}
280
.#define SEC_LINK_DUPLICATES           0xc0000
281
.
282
.  {* This value for SEC_LINK_DUPLICATES means that duplicate
283
.     sections with the same name should simply be discarded.  *}
284
.#define SEC_LINK_DUPLICATES_DISCARD       0x0
285
.
286
.  {* This value for SEC_LINK_DUPLICATES means that the linker
287
.     should warn if there are any duplicate sections, although
288
.     it should still only link one copy.  *}
289
.#define SEC_LINK_DUPLICATES_ONE_ONLY  0x40000
290
.
291
.  {* This value for SEC_LINK_DUPLICATES means that the linker
292
.     should warn if any duplicate sections are a different size.  *}
293
.#define SEC_LINK_DUPLICATES_SAME_SIZE 0x80000
294
.
295
.  {* This value for SEC_LINK_DUPLICATES means that the linker
296
.     should warn if any duplicate sections contain different
297
.     contents.  *}
298
.#define SEC_LINK_DUPLICATES_SAME_CONTENTS \
299
.  (SEC_LINK_DUPLICATES_ONE_ONLY | SEC_LINK_DUPLICATES_SAME_SIZE)
300
.
301
.  {* This section was created by the linker as part of dynamic
302
.     relocation or other arcane processing.  It is skipped when
303
.     going through the first-pass output, trusting that someone
304
.     else up the line will take care of it later.  *}
305
.#define SEC_LINKER_CREATED           0x100000
306
.
307
.  {* This section contains a section ID to distinguish different
308
.     sections with the same section name.  *}
309
.#define SEC_ASSEMBLER_SECTION_ID     0x100000
310
.
311
.  {* This section should not be subject to garbage collection.
312
.     Also set to inform the linker that this section should not be
313
.     listed in the link map as discarded.  *}
314
.#define SEC_KEEP                     0x200000
315
.
316
.  {* This section contains "short" data, and should be placed
317
.     "near" the GP.  *}
318
.#define SEC_SMALL_DATA               0x400000
319
.
320
.  {* Attempt to merge identical entities in the section.
321
.     Entity size is given in the entsize field.  *}
322
.#define SEC_MERGE                    0x800000
323
.
324
.  {* If given with SEC_MERGE, entities to merge are zero terminated
325
.     strings where entsize specifies character size instead of fixed
326
.     size entries.  *}
327
.#define SEC_STRINGS                 0x1000000
328
.
329
.  {* This section contains data about section groups.  *}
330
.#define SEC_GROUP                   0x2000000
331
.
332
.  {* The section is a COFF shared library section.  This flag is
333
.     only for the linker.  If this type of section appears in
334
.     the input file, the linker must copy it to the output file
335
.     without changing the vma or size.  FIXME: Although this
336
.     was originally intended to be general, it really is COFF
337
.     specific (and the flag was renamed to indicate this).  It
338
.     might be cleaner to have some more general mechanism to
339
.     allow the back end to control what the linker does with
340
.     sections.  *}
341
.#define SEC_COFF_SHARED_LIBRARY     0x4000000
342
.
343
.  {* This input section should be copied to output in reverse order
344
.     as an array of pointers.  This is for ELF linker internal use
345
.     only.  *}
346
.#define SEC_ELF_REVERSE_COPY        0x4000000
347
.
348
.  {* This section contains data which may be shared with other
349
.     executables or shared objects. This is for COFF only.  *}
350
.#define SEC_COFF_SHARED             0x8000000
351
.
352
.  {* Indicate that section has the purecode flag set.  *}
353
.#define SEC_ELF_PURECODE            0x8000000
354
.
355
.  {* When a section with this flag is being linked, then if the size of
356
.     the input section is less than a page, it should not cross a page
357
.     boundary.  If the size of the input section is one page or more,
358
.     it should be aligned on a page boundary.  This is for TI
359
.     TMS320C54X only.  *}
360
.#define SEC_TIC54X_BLOCK           0x10000000
361
.
362
.  {* Conditionally link this section; do not link if there are no
363
.     references found to any symbol in the section.  This is for TI
364
.     TMS320C54X only.  *}
365
.#define SEC_TIC54X_CLINK           0x20000000
366
.
367
.  {* This section contains vliw code.  This is for Toshiba MeP only.  *}
368
.#define SEC_MEP_VLIW               0x20000000
369
.
370
.  {* All symbols, sizes and relocations in this section are octets
371
.     instead of bytes.  Required for DWARF debug sections as DWARF
372
.     information is organized in octets, not bytes.  *}
373
.#define SEC_ELF_OCTETS             0x40000000
374
.
375
.  {* Indicate that section has the no read flag set. This happens
376
.     when memory read flag isn't set. *}
377
.#define SEC_COFF_NOREAD            0x40000000
378
.
379
.  {*  End of section flags.  *}
380
.
381
.  {* Some internal packed boolean fields.  *}
382
.
383
.  {* See the vma field.  *}
384
.  unsigned int user_set_vma : 1;
385
.
386
.  {* A mark flag used by some of the linker backends.  *}
387
.  unsigned int linker_mark : 1;
388
.
389
.  {* Another mark flag used by some of the linker backends.  Set for
390
.     output sections that have an input section.  *}
391
.  unsigned int linker_has_input : 1;
392
.
393
.  {* Mark flag used by some linker backends for garbage collection.  *}
394
.  unsigned int gc_mark : 1;
395
.
396
.  {* Section compression status.  *}
397
.  unsigned int compress_status : 2;
398
.#define COMPRESS_SECTION_NONE    0
399
.#define COMPRESS_SECTION_DONE    1
400
.#define DECOMPRESS_SECTION_ZLIB  2
401
.#define DECOMPRESS_SECTION_ZSTD  3
402
.
403
.  {* The following flags are used by the ELF linker. *}
404
.
405
.  {* Mark sections which have been allocated to segments.  *}
406
.  unsigned int segment_mark : 1;
407
.
408
.  {* Type of sec_info information.  *}
409
.  unsigned int sec_info_type:3;
410
.#define SEC_INFO_TYPE_NONE      0
411
.#define SEC_INFO_TYPE_STABS     1
412
.#define SEC_INFO_TYPE_MERGE     2
413
.#define SEC_INFO_TYPE_EH_FRAME  3
414
.#define SEC_INFO_TYPE_JUST_SYMS 4
415
.#define SEC_INFO_TYPE_TARGET    5
416
.#define SEC_INFO_TYPE_EH_FRAME_ENTRY 6
417
.#define SEC_INFO_TYPE_SFRAME  7
418
.
419
.  {* Nonzero if this section uses RELA relocations, rather than REL.  *}
420
.  unsigned int use_rela_p:1;
421
.
422
.  {* Bits used by various backends.  The generic code doesn't touch
423
.     these fields.  *}
424
.
425
.  unsigned int sec_flg0:1;
426
.  unsigned int sec_flg1:1;
427
.  unsigned int sec_flg2:1;
428
.  unsigned int sec_flg3:1;
429
.  unsigned int sec_flg4:1;
430
.  unsigned int sec_flg5:1;
431
.
432
.  {* End of internal packed boolean fields.  *}
433
.
434
.  {*  The virtual memory address of the section - where it will be
435
.      at run time.  The symbols are relocated against this.  The
436
.      user_set_vma flag is maintained by bfd; if it's not set, the
437
.      backend can assign addresses (for example, in <<a.out>>, where
438
.      the default address for <<.data>> is dependent on the specific
439
.      target and various flags).  *}
440
.  bfd_vma vma;
441
.
442
.  {*  The load address of the section - where it would be in a
443
.      rom image; really only used for writing section header
444
.      information.  *}
445
.  bfd_vma lma;
446
.
447
.  {* The size of the section in *octets*, as it will be output.
448
.     Contains a value even if the section has no contents (e.g., the
449
.     size of <<.bss>>).  *}
450
.  bfd_size_type size;
451
.
452
.  {* For input sections, the original size on disk of the section, in
453
.     octets.  This field should be set for any section whose size is
454
.     changed by linker relaxation.  It is required for sections where
455
.     the linker relaxation scheme doesn't cache altered section and
456
.     reloc contents (stabs, eh_frame, SEC_MERGE, some coff relaxing
457
.     targets), and thus the original size needs to be kept to read the
458
.     section multiple times.  For output sections, rawsize holds the
459
.     section size calculated on a previous linker relaxation pass.  *}
460
.  bfd_size_type rawsize;
461
.
462
.  {* The compressed size of the section in octets.  *}
463
.  bfd_size_type compressed_size;
464
.
465
.  {* If this section is going to be output, then this value is the
466
.     offset in *bytes* into the output section of the first byte in the
467
.     input section (byte ==> smallest addressable unit on the
468
.     target).  In most cases, if this was going to start at the
469
.     100th octet (8-bit quantity) in the output section, this value
470
.     would be 100.  However, if the target byte size is 16 bits
471
.     (bfd_octets_per_byte is "2"), this value would be 50.  *}
472
.  bfd_vma output_offset;
473
.
474
.  {* The output section through which to map on output.  *}
475
.  struct bfd_section *output_section;
476
.
477
.  {* If an input section, a pointer to a vector of relocation
478
.     records for the data in this section.  *}
479
.  struct reloc_cache_entry *relocation;
480
.
481
.  {* If an output section, a pointer to a vector of pointers to
482
.     relocation records for the data in this section.  *}
483
.  struct reloc_cache_entry **orelocation;
484
.
485
.  {* The number of relocation records in one of the above.  *}
486
.  unsigned reloc_count;
487
.
488
.  {* The alignment requirement of the section, as an exponent of 2 -
489
.     e.g., 3 aligns to 2^3 (or 8).  *}
490
.  unsigned int alignment_power;
491
.
492
.  {* Information below is back end specific - and not always used
493
.     or updated.  *}
494
.
495
.  {* File position of section data.  *}
496
.  file_ptr filepos;
497
.
498
.  {* File position of relocation info.  *}
499
.  file_ptr rel_filepos;
500
.
501
.  {* File position of line data.  *}
502
.  file_ptr line_filepos;
503
.
504
.  {* Pointer to data for applications.  *}
505
.  void *userdata;
506
.
507
.  {* If the SEC_IN_MEMORY flag is set, this points to the actual
508
.     contents.  *}
509
.  bfd_byte *contents;
510
.
511
.  {* Attached line number information.  *}
512
.  alent *lineno;
513
.
514
.  {* Number of line number records.  *}
515
.  unsigned int lineno_count;
516
.
517
.  {* Entity size for merging purposes.  *}
518
.  unsigned int entsize;
519
.
520
.  {* Points to the kept section if this section is a link-once section,
521
.     and is discarded.  *}
522
.  struct bfd_section *kept_section;
523
.
524
.  {* When a section is being output, this value changes as more
525
.     linenumbers are written out.  *}
526
.  file_ptr moving_line_filepos;
527
.
528
.  {* What the section number is in the target world.  *}
529
.  int target_index;
530
.
531
.  void *used_by_bfd;
532
.
533
.  {* If this is a constructor section then here is a list of the
534
.     relocations created to relocate items within it.  *}
535
.  struct relent_chain *constructor_chain;
536
.
537
.  {* The BFD which owns the section.  *}
538
.  bfd *owner;
539
.
540
.  {* A symbol which points at this section only.  *}
541
.  struct bfd_symbol *symbol;
542
.  struct bfd_symbol **symbol_ptr_ptr;
543
.
544
.  {* Early in the link process, map_head and map_tail are used to build
545
.     a list of input sections attached to an output section.  Later,
546
.     output sections use these fields for a list of bfd_link_order
547
.     structs.  The linked_to_symbol_name field is for ELF assembler
548
.     internal use.  *}
549
.  union {
550
.    struct bfd_link_order *link_order;
551
.    struct bfd_section *s;
552
.    const char *linked_to_symbol_name;
553
.  } map_head, map_tail;
554
.
555
.  {* Points to the output section this section is already assigned to,
556
.     if any.  This is used when support for non-contiguous memory
557
.     regions is enabled.  *}
558
.  struct bfd_section *already_assigned;
559
.
560
.  {* Explicitly specified section type, if non-zero.  *}
561
.  unsigned int type;
562
.
563
.} asection;
564
.
565
566
EXTERNAL
567
.static inline const char *
568
.bfd_section_name (const asection *sec)
569
.{
570
.  return sec->name;
571
.}
572
.
573
.static inline bfd_size_type
574
.bfd_section_size (const asection *sec)
575
.{
576
.  return sec->size;
577
.}
578
.
579
.static inline bfd_vma
580
.bfd_section_vma (const asection *sec)
581
.{
582
.  return sec->vma;
583
.}
584
.
585
.static inline bfd_vma
586
.bfd_section_lma (const asection *sec)
587
.{
588
.  return sec->lma;
589
.}
590
.
591
.static inline unsigned int
592
.bfd_section_alignment (const asection *sec)
593
.{
594
.  return sec->alignment_power;
595
.}
596
.
597
.static inline flagword
598
.bfd_section_flags (const asection *sec)
599
.{
600
.  return sec->flags;
601
.}
602
.
603
.static inline void *
604
.bfd_section_userdata (const asection *sec)
605
.{
606
.  return sec->userdata;
607
.}
608
.static inline bool
609
.bfd_is_com_section (const asection *sec)
610
.{
611
.  return (sec->flags & SEC_IS_COMMON) != 0;
612
.}
613
.
614
.{* Note: the following are provided as inline functions rather than macros
615
.   because not all callers use the return value.  A macro implementation
616
.   would use a comma expression, eg: "((ptr)->foo = val, TRUE)" and some
617
.   compilers will complain about comma expressions that have no effect.  *}
618
.static inline bool
619
.bfd_set_section_userdata (asection *sec, void *val)
620
.{
621
.  sec->userdata = val;
622
.  return true;
623
.}
624
.
625
.static inline bool
626
.bfd_set_section_vma (asection *sec, bfd_vma val)
627
.{
628
.  sec->vma = sec->lma = val;
629
.  sec->user_set_vma = true;
630
.  return true;
631
.}
632
.
633
.static inline bool
634
.bfd_set_section_lma (asection *sec, bfd_vma val)
635
.{
636
.  sec->lma = val;
637
.  return true;
638
.}
639
.
640
.static inline bool
641
.bfd_set_section_alignment (asection *sec, unsigned int val)
642
.{
643
.  if (val >= sizeof (bfd_vma) * 8 - 1)
644
.    return false;
645
.  sec->alignment_power = val;
646
.  return true;
647
.}
648
.
649
.{* These sections are global, and are managed by BFD.  The application
650
.   and target back end are not permitted to change the values in
651
.   these sections.  *}
652
.extern asection _bfd_std_section[4];
653
.
654
.#define BFD_ABS_SECTION_NAME "*ABS*"
655
.#define BFD_UND_SECTION_NAME "*UND*"
656
.#define BFD_COM_SECTION_NAME "*COM*"
657
.#define BFD_IND_SECTION_NAME "*IND*"
658
.
659
.{* Pointer to the common section.  *}
660
.#define bfd_com_section_ptr (&_bfd_std_section[0])
661
.{* Pointer to the undefined section.  *}
662
.#define bfd_und_section_ptr (&_bfd_std_section[1])
663
.{* Pointer to the absolute section.  *}
664
.#define bfd_abs_section_ptr (&_bfd_std_section[2])
665
.{* Pointer to the indirect section.  *}
666
.#define bfd_ind_section_ptr (&_bfd_std_section[3])
667
.
668
.static inline bool
669
.bfd_is_und_section (const asection *sec)
670
.{
671
.  return sec == bfd_und_section_ptr;
672
.}
673
.
674
.static inline bool
675
.bfd_is_abs_section (const asection *sec)
676
.{
677
.  return sec == bfd_abs_section_ptr;
678
.}
679
.
680
.static inline bool
681
.bfd_is_ind_section (const asection *sec)
682
.{
683
.  return sec == bfd_ind_section_ptr;
684
.}
685
.
686
.static inline bool
687
.bfd_is_const_section (const asection *sec)
688
.{
689
.  return (sec >= _bfd_std_section
690
.          && sec < _bfd_std_section + (sizeof (_bfd_std_section)
691
.                                       / sizeof (_bfd_std_section[0])));
692
.}
693
.
694
.{* Return TRUE if input section SEC has been discarded.  *}
695
.static inline bool
696
.discarded_section (const asection *sec)
697
.{
698
.  return (!bfd_is_abs_section (sec)
699
.          && bfd_is_abs_section (sec->output_section)
700
.          && sec->sec_info_type != SEC_INFO_TYPE_MERGE
701
.          && sec->sec_info_type != SEC_INFO_TYPE_JUST_SYMS);
702
.}
703
.
704
.#define BFD_FAKE_SECTION(SEC, SYM, NAME, IDX, FLAGS)     \
705
.  {* name, next, prev, id,  section_id, index, flags, user_set_vma, *} \
706
.  {  NAME, NULL, NULL, IDX, 0,          0,     FLAGS, 0,   \
707
.                 \
708
.  {* linker_mark, linker_has_input, gc_mark, decompress_status,     *} \
709
.     0,           0,                1,       0,      \
710
.                 \
711
.  {* segment_mark, sec_info_type, use_rela_p,                       *} \
712
.     0,            0,             0,         \
713
.                 \
714
.  {* sec_flg0, sec_flg1, sec_flg2, sec_flg3, sec_flg4, sec_flg5,    *} \
715
.     0,        0,        0,        0,        0,        0,    \
716
.                 \
717
.  {* vma, lma, size, rawsize, compressed_size,                      *} \
718
.     0,   0,   0,    0,       0,         \
719
.                 \
720
.  {* output_offset, output_section, relocation, orelocation,        *} \
721
.     0,             &SEC,           NULL,       NULL,      \
722
.                 \
723
.  {* reloc_count, alignment_power, filepos, rel_filepos,            *} \
724
.     0,           0,               0,       0,       \
725
.                 \
726
.  {* line_filepos, userdata, contents, lineno, lineno_count,        *} \
727
.     0,            NULL,     NULL,     NULL,   0,      \
728
.                 \
729
.  {* entsize, kept_section, moving_line_filepos,                    *} \
730
.     0,       NULL,         0,           \
731
.                 \
732
.  {* target_index, used_by_bfd, constructor_chain, owner,           *} \
733
.     0,            NULL,        NULL,              NULL,   \
734
.                 \
735
.  {* symbol,                    symbol_ptr_ptr,                     *} \
736
.     (struct bfd_symbol *) SYM, &SEC.symbol,       \
737
.                 \
738
.  {* map_head, map_tail, already_assigned, type                     *} \
739
.     { NULL }, { NULL }, NULL,             0       \
740
.                 \
741
.    }
742
.
743
.{* We use a macro to initialize the static asymbol structures because
744
.   traditional C does not permit us to initialize a union member while
745
.   gcc warns if we don't initialize it.
746
.   the_bfd, name, value, attr, section [, udata]  *}
747
.#ifdef __STDC__
748
.#define GLOBAL_SYM_INIT(NAME, SECTION) \
749
.  { 0, NAME, 0, BSF_SECTION_SYM, SECTION, { 0 }}
750
.#else
751
.#define GLOBAL_SYM_INIT(NAME, SECTION) \
752
.  { 0, NAME, 0, BSF_SECTION_SYM, SECTION }
753
.#endif
754
.
755
*/
756
757
/* These symbols are global, not specific to any BFD.  Therefore, anything
758
   that tries to change them is broken, and should be repaired.  */
759
760
static const asymbol global_syms[] =
761
{
762
  GLOBAL_SYM_INIT (BFD_COM_SECTION_NAME, bfd_com_section_ptr),
763
  GLOBAL_SYM_INIT (BFD_UND_SECTION_NAME, bfd_und_section_ptr),
764
  GLOBAL_SYM_INIT (BFD_ABS_SECTION_NAME, bfd_abs_section_ptr),
765
  GLOBAL_SYM_INIT (BFD_IND_SECTION_NAME, bfd_ind_section_ptr)
766
};
767
768
#define STD_SECTION(NAME, IDX, FLAGS) \
769
  BFD_FAKE_SECTION(_bfd_std_section[IDX], &global_syms[IDX], NAME, IDX, FLAGS)
770
771
asection _bfd_std_section[] = {
772
  STD_SECTION (BFD_COM_SECTION_NAME, 0, SEC_IS_COMMON),
773
  STD_SECTION (BFD_UND_SECTION_NAME, 1, 0),
774
  STD_SECTION (BFD_ABS_SECTION_NAME, 2, 0),
775
  STD_SECTION (BFD_IND_SECTION_NAME, 3, 0)
776
};
777
#undef STD_SECTION
778
779
/* Initialize an entry in the section hash table.  */
780
781
struct bfd_hash_entry *
782
bfd_section_hash_newfunc (struct bfd_hash_entry *entry,
783
        struct bfd_hash_table *table,
784
        const char *string)
785
0
{
786
  /* Allocate the structure if it has not already been allocated by a
787
     subclass.  */
788
0
  if (entry == NULL)
789
0
    {
790
0
      entry = (struct bfd_hash_entry *)
791
0
  bfd_hash_allocate (table, sizeof (struct section_hash_entry));
792
0
      if (entry == NULL)
793
0
  return entry;
794
0
    }
795
796
  /* Call the allocation method of the superclass.  */
797
0
  entry = bfd_hash_newfunc (entry, table, string);
798
0
  if (entry != NULL)
799
0
    memset (&((struct section_hash_entry *) entry)->section, 0,
800
0
      sizeof (asection));
801
802
0
  return entry;
803
0
}
804
805
#define section_hash_lookup(table, string, create, copy) \
806
0
  ((struct section_hash_entry *) \
807
0
   bfd_hash_lookup ((table), (string), (create), (copy)))
808
809
/* Create a symbol whose only job is to point to this section.  This
810
   is useful for things like relocs which are relative to the base
811
   of a section.  */
812
813
bool
814
_bfd_generic_new_section_hook (bfd *abfd, asection *newsect)
815
0
{
816
0
  newsect->symbol = bfd_make_empty_symbol (abfd);
817
0
  if (newsect->symbol == NULL)
818
0
    return false;
819
820
0
  newsect->symbol->name = newsect->name;
821
0
  newsect->symbol->value = 0;
822
0
  newsect->symbol->section = newsect;
823
0
  newsect->symbol->flags = BSF_SECTION_SYM;
824
825
0
  newsect->symbol_ptr_ptr = &newsect->symbol;
826
0
  return true;
827
0
}
828
829
unsigned int _bfd_section_id = 0x10;  /* id 0 to 3 used by STD_SECTION.  */
830
831
/* Initializes a new section.  NEWSECT->NAME is already set.  */
832
833
static asection *
834
bfd_section_init (bfd *abfd, asection *newsect)
835
0
{
836
0
  newsect->id = _bfd_section_id;
837
0
  newsect->index = abfd->section_count;
838
0
  newsect->owner = abfd;
839
840
0
  if (! BFD_SEND (abfd, _new_section_hook, (abfd, newsect)))
841
0
    return NULL;
842
843
0
  _bfd_section_id++;
844
0
  abfd->section_count++;
845
0
  bfd_section_list_append (abfd, newsect);
846
0
  return newsect;
847
0
}
848
849
/*
850
DOCDD
851
INODE
852
section prototypes,  , typedef asection, Sections
853
SUBSECTION
854
  Section prototypes
855
856
These are the functions exported by the section handling part of BFD.
857
*/
858
859
/*
860
FUNCTION
861
  bfd_section_list_clear
862
863
SYNOPSIS
864
  void bfd_section_list_clear (bfd *);
865
866
DESCRIPTION
867
  Clears the section list, and also resets the section count and
868
  hash table entries.
869
*/
870
871
void
872
bfd_section_list_clear (bfd *abfd)
873
0
{
874
0
  abfd->sections = NULL;
875
0
  abfd->section_last = NULL;
876
0
  abfd->section_count = 0;
877
0
  memset (abfd->section_htab.table, 0,
878
0
    abfd->section_htab.size * sizeof (struct bfd_hash_entry *));
879
0
  abfd->section_htab.count = 0;
880
0
}
881
882
/*
883
FUNCTION
884
  bfd_get_section_by_name
885
886
SYNOPSIS
887
  asection *bfd_get_section_by_name (bfd *abfd, const char *name);
888
889
DESCRIPTION
890
  Return the most recently created section attached to @var{abfd}
891
  named @var{name}.  Return NULL if no such section exists.
892
*/
893
894
asection *
895
bfd_get_section_by_name (bfd *abfd, const char *name)
896
0
{
897
0
  struct section_hash_entry *sh;
898
899
0
  if (name == NULL)
900
0
    return NULL;
901
902
0
  sh = section_hash_lookup (&abfd->section_htab, name, false, false);
903
0
  if (sh != NULL)
904
0
    return &sh->section;
905
906
0
  return NULL;
907
0
}
908
909
/*
910
FUNCTION
911
       bfd_get_next_section_by_name
912
913
SYNOPSIS
914
       asection *bfd_get_next_section_by_name (bfd *ibfd, asection *sec);
915
916
DESCRIPTION
917
       Given @var{sec} is a section returned by @code{bfd_get_section_by_name},
918
       return the next most recently created section attached to the same
919
       BFD with the same name, or if no such section exists in the same BFD and
920
       IBFD is non-NULL, the next section with the same name in any input
921
       BFD following IBFD.  Return NULL on finding no section.
922
*/
923
924
asection *
925
bfd_get_next_section_by_name (bfd *ibfd, asection *sec)
926
0
{
927
0
  struct section_hash_entry *sh;
928
0
  const char *name;
929
0
  unsigned long hash;
930
931
0
  sh = ((struct section_hash_entry *)
932
0
  ((char *) sec - offsetof (struct section_hash_entry, section)));
933
934
0
  hash = sh->root.hash;
935
0
  name = sec->name;
936
0
  for (sh = (struct section_hash_entry *) sh->root.next;
937
0
       sh != NULL;
938
0
       sh = (struct section_hash_entry *) sh->root.next)
939
0
    if (sh->root.hash == hash
940
0
       && strcmp (sh->root.string, name) == 0)
941
0
      return &sh->section;
942
943
0
  if (ibfd != NULL)
944
0
    {
945
0
      while ((ibfd = ibfd->link.next) != NULL)
946
0
  {
947
0
    asection *s = bfd_get_section_by_name (ibfd, name);
948
0
    if (s != NULL)
949
0
      return s;
950
0
  }
951
0
    }
952
953
0
  return NULL;
954
0
}
955
956
/*
957
FUNCTION
958
  bfd_get_linker_section
959
960
SYNOPSIS
961
  asection *bfd_get_linker_section (bfd *abfd, const char *name);
962
963
DESCRIPTION
964
  Return the linker created section attached to @var{abfd}
965
  named @var{name}.  Return NULL if no such section exists.
966
*/
967
968
asection *
969
bfd_get_linker_section (bfd *abfd, const char *name)
970
0
{
971
0
  asection *sec = bfd_get_section_by_name (abfd, name);
972
973
0
  while (sec != NULL && (sec->flags & SEC_LINKER_CREATED) == 0)
974
0
    sec = bfd_get_next_section_by_name (NULL, sec);
975
0
  return sec;
976
0
}
977
978
/*
979
FUNCTION
980
  bfd_get_section_by_name_if
981
982
SYNOPSIS
983
  asection *bfd_get_section_by_name_if
984
    (bfd *abfd,
985
     const char *name,
986
     bool (*func) (bfd *abfd, asection *sect, void *obj),
987
     void *obj);
988
989
DESCRIPTION
990
  Call the provided function @var{func} for each section
991
  attached to the BFD @var{abfd} whose name matches @var{name},
992
  passing @var{obj} as an argument. The function will be called
993
  as if by
994
995
| func (abfd, the_section, obj);
996
997
  It returns the first section for which @var{func} returns true,
998
  otherwise <<NULL>>.
999
1000
*/
1001
1002
asection *
1003
bfd_get_section_by_name_if (bfd *abfd, const char *name,
1004
          bool (*operation) (bfd *, asection *, void *),
1005
          void *user_storage)
1006
0
{
1007
0
  struct section_hash_entry *sh;
1008
0
  unsigned long hash;
1009
1010
0
  if (name == NULL)
1011
0
    return NULL;
1012
1013
0
  sh = section_hash_lookup (&abfd->section_htab, name, false, false);
1014
0
  if (sh == NULL)
1015
0
    return NULL;
1016
1017
0
  hash = sh->root.hash;
1018
0
  for (; sh != NULL; sh = (struct section_hash_entry *) sh->root.next)
1019
0
    if (sh->root.hash == hash
1020
0
  && strcmp (sh->root.string, name) == 0
1021
0
  && (*operation) (abfd, &sh->section, user_storage))
1022
0
      return &sh->section;
1023
1024
0
  return NULL;
1025
0
}
1026
1027
/*
1028
FUNCTION
1029
  bfd_get_unique_section_name
1030
1031
SYNOPSIS
1032
  char *bfd_get_unique_section_name
1033
    (bfd *abfd, const char *templat, int *count);
1034
1035
DESCRIPTION
1036
  Invent a section name that is unique in @var{abfd} by tacking
1037
  a dot and a digit suffix onto the original @var{templat}.  If
1038
  @var{count} is non-NULL, then it specifies the first number
1039
  tried as a suffix to generate a unique name.  The value
1040
  pointed to by @var{count} will be incremented in this case.
1041
*/
1042
1043
char *
1044
bfd_get_unique_section_name (bfd *abfd, const char *templat, int *count)
1045
0
{
1046
0
  int num;
1047
0
  unsigned int len;
1048
0
  char *sname;
1049
1050
0
  len = strlen (templat);
1051
0
  sname = (char *) bfd_malloc (len + 8);
1052
0
  if (sname == NULL)
1053
0
    return NULL;
1054
0
  memcpy (sname, templat, len);
1055
0
  num = 1;
1056
0
  if (count != NULL)
1057
0
    num = *count;
1058
1059
0
  do
1060
0
    {
1061
      /* If we have a million sections, something is badly wrong.  */
1062
0
      if (num > 999999)
1063
0
  abort ();
1064
0
      sprintf (sname + len, ".%d", num++);
1065
0
    }
1066
0
  while (section_hash_lookup (&abfd->section_htab, sname, false, false));
1067
1068
0
  if (count != NULL)
1069
0
    *count = num;
1070
0
  return sname;
1071
0
}
1072
1073
/*
1074
FUNCTION
1075
  bfd_make_section_old_way
1076
1077
SYNOPSIS
1078
  asection *bfd_make_section_old_way (bfd *abfd, const char *name);
1079
1080
DESCRIPTION
1081
  Create a new empty section called @var{name}
1082
  and attach it to the end of the chain of sections for the
1083
  BFD @var{abfd}. An attempt to create a section with a name which
1084
  is already in use returns its pointer without changing the
1085
  section chain.
1086
1087
  It has the funny name since this is the way it used to be
1088
  before it was rewritten....
1089
1090
  Possible errors are:
1091
  o <<bfd_error_invalid_operation>> -
1092
  If output has already started for this BFD.
1093
  o <<bfd_error_no_memory>> -
1094
  If memory allocation fails.
1095
1096
*/
1097
1098
asection *
1099
bfd_make_section_old_way (bfd *abfd, const char *name)
1100
0
{
1101
0
  asection *newsect;
1102
1103
0
  if (abfd->output_has_begun)
1104
0
    {
1105
0
      bfd_set_error (bfd_error_invalid_operation);
1106
0
      return NULL;
1107
0
    }
1108
1109
0
  if (strcmp (name, BFD_ABS_SECTION_NAME) == 0)
1110
0
    newsect = bfd_abs_section_ptr;
1111
0
  else if (strcmp (name, BFD_COM_SECTION_NAME) == 0)
1112
0
    newsect = bfd_com_section_ptr;
1113
0
  else if (strcmp (name, BFD_UND_SECTION_NAME) == 0)
1114
0
    newsect = bfd_und_section_ptr;
1115
0
  else if (strcmp (name, BFD_IND_SECTION_NAME) == 0)
1116
0
    newsect = bfd_ind_section_ptr;
1117
0
  else
1118
0
    {
1119
0
      struct section_hash_entry *sh;
1120
1121
0
      sh = section_hash_lookup (&abfd->section_htab, name, true, false);
1122
0
      if (sh == NULL)
1123
0
  return NULL;
1124
1125
0
      newsect = &sh->section;
1126
0
      if (newsect->name != NULL)
1127
0
  {
1128
    /* Section already exists.  */
1129
0
    return newsect;
1130
0
  }
1131
1132
0
      newsect->name = name;
1133
0
      return bfd_section_init (abfd, newsect);
1134
0
    }
1135
1136
  /* Call new_section_hook when "creating" the standard abs, com, und
1137
     and ind sections to tack on format specific section data.
1138
     Also, create a proper section symbol.  */
1139
0
  if (! BFD_SEND (abfd, _new_section_hook, (abfd, newsect)))
1140
0
    return NULL;
1141
0
  return newsect;
1142
0
}
1143
1144
/*
1145
FUNCTION
1146
  bfd_make_section_anyway_with_flags
1147
1148
SYNOPSIS
1149
  asection *bfd_make_section_anyway_with_flags
1150
    (bfd *abfd, const char *name, flagword flags);
1151
1152
DESCRIPTION
1153
   Create a new empty section called @var{name} and attach it to the end of
1154
   the chain of sections for @var{abfd}.  Create a new section even if there
1155
   is already a section with that name.  Also set the attributes of the
1156
   new section to the value @var{flags}.
1157
1158
   Return <<NULL>> and set <<bfd_error>> on error; possible errors are:
1159
   o <<bfd_error_invalid_operation>> - If output has already started for @var{abfd}.
1160
   o <<bfd_error_no_memory>> - If memory allocation fails.
1161
*/
1162
1163
sec_ptr
1164
bfd_make_section_anyway_with_flags (bfd *abfd, const char *name,
1165
            flagword flags)
1166
0
{
1167
0
  struct section_hash_entry *sh;
1168
0
  asection *newsect;
1169
1170
0
  if (abfd->output_has_begun)
1171
0
    {
1172
0
      bfd_set_error (bfd_error_invalid_operation);
1173
0
      return NULL;
1174
0
    }
1175
1176
0
  sh = section_hash_lookup (&abfd->section_htab, name, true, false);
1177
0
  if (sh == NULL)
1178
0
    return NULL;
1179
1180
0
  newsect = &sh->section;
1181
0
  if (newsect->name != NULL)
1182
0
    {
1183
      /* We are making a section of the same name.  Put it in the
1184
   section hash table.  Even though we can't find it directly by a
1185
   hash lookup, we'll be able to find the section by traversing
1186
   sh->root.next quicker than looking at all the bfd sections.  */
1187
0
      struct section_hash_entry *new_sh;
1188
0
      new_sh = (struct section_hash_entry *)
1189
0
  bfd_section_hash_newfunc (NULL, &abfd->section_htab, name);
1190
0
      if (new_sh == NULL)
1191
0
  return NULL;
1192
1193
0
      new_sh->root = sh->root;
1194
0
      sh->root.next = &new_sh->root;
1195
0
      newsect = &new_sh->section;
1196
0
    }
1197
1198
0
  newsect->flags = flags;
1199
0
  newsect->name = name;
1200
0
  return bfd_section_init (abfd, newsect);
1201
0
}
1202
1203
/*
1204
FUNCTION
1205
  bfd_make_section_anyway
1206
1207
SYNOPSIS
1208
  asection *bfd_make_section_anyway (bfd *abfd, const char *name);
1209
1210
DESCRIPTION
1211
   Create a new empty section called @var{name} and attach it to the end of
1212
   the chain of sections for @var{abfd}.  Create a new section even if there
1213
   is already a section with that name.
1214
1215
   Return <<NULL>> and set <<bfd_error>> on error; possible errors are:
1216
   o <<bfd_error_invalid_operation>> - If output has already started for @var{abfd}.
1217
   o <<bfd_error_no_memory>> - If memory allocation fails.
1218
*/
1219
1220
sec_ptr
1221
bfd_make_section_anyway (bfd *abfd, const char *name)
1222
0
{
1223
0
  return bfd_make_section_anyway_with_flags (abfd, name, 0);
1224
0
}
1225
1226
/*
1227
FUNCTION
1228
  bfd_make_section_with_flags
1229
1230
SYNOPSIS
1231
  asection *bfd_make_section_with_flags
1232
    (bfd *, const char *name, flagword flags);
1233
1234
DESCRIPTION
1235
   Like <<bfd_make_section_anyway>>, but return <<NULL>> (without calling
1236
   bfd_set_error ()) without changing the section chain if there is already a
1237
   section named @var{name}.  Also set the attributes of the new section to
1238
   the value @var{flags}.  If there is an error, return <<NULL>> and set
1239
   <<bfd_error>>.
1240
*/
1241
1242
asection *
1243
bfd_make_section_with_flags (bfd *abfd, const char *name,
1244
           flagword flags)
1245
0
{
1246
0
  struct section_hash_entry *sh;
1247
0
  asection *newsect;
1248
1249
0
  if (abfd == NULL || name == NULL || abfd->output_has_begun)
1250
0
    {
1251
0
      bfd_set_error (bfd_error_invalid_operation);
1252
0
      return NULL;
1253
0
    }
1254
1255
0
  if (strcmp (name, BFD_ABS_SECTION_NAME) == 0
1256
0
      || strcmp (name, BFD_COM_SECTION_NAME) == 0
1257
0
      || strcmp (name, BFD_UND_SECTION_NAME) == 0
1258
0
      || strcmp (name, BFD_IND_SECTION_NAME) == 0)
1259
0
    return NULL;
1260
1261
0
  sh = section_hash_lookup (&abfd->section_htab, name, true, false);
1262
0
  if (sh == NULL)
1263
0
    return NULL;
1264
1265
0
  newsect = &sh->section;
1266
0
  if (newsect->name != NULL)
1267
0
    {
1268
      /* Section already exists.  */
1269
0
      return NULL;
1270
0
    }
1271
1272
0
  newsect->name = name;
1273
0
  newsect->flags = flags;
1274
0
  return bfd_section_init (abfd, newsect);
1275
0
}
1276
1277
/*
1278
FUNCTION
1279
  bfd_make_section
1280
1281
SYNOPSIS
1282
  asection *bfd_make_section (bfd *, const char *name);
1283
1284
DESCRIPTION
1285
   Like <<bfd_make_section_anyway>>, but return <<NULL>> (without calling
1286
   bfd_set_error ()) without changing the section chain if there is already a
1287
   section named @var{name}.  If there is an error, return <<NULL>> and set
1288
   <<bfd_error>>.
1289
*/
1290
1291
asection *
1292
bfd_make_section (bfd *abfd, const char *name)
1293
0
{
1294
0
  return bfd_make_section_with_flags (abfd, name, 0);
1295
0
}
1296
1297
/*
1298
FUNCTION
1299
  bfd_set_section_flags
1300
1301
SYNOPSIS
1302
  bool bfd_set_section_flags (asection *sec, flagword flags);
1303
1304
DESCRIPTION
1305
  Set the attributes of the section @var{sec} to the value @var{flags}.
1306
  Return <<TRUE>> on success, <<FALSE>> on error.  Possible error
1307
  returns are:
1308
1309
  o <<bfd_error_invalid_operation>> -
1310
  The section cannot have one or more of the attributes
1311
  requested. For example, a .bss section in <<a.out>> may not
1312
  have the <<SEC_HAS_CONTENTS>> field set.
1313
1314
*/
1315
1316
bool
1317
bfd_set_section_flags (asection *section, flagword flags)
1318
0
{
1319
0
  section->flags = flags;
1320
0
  return true;
1321
0
}
1322
1323
/*
1324
FUNCTION
1325
  bfd_rename_section
1326
1327
SYNOPSIS
1328
  void bfd_rename_section
1329
    (asection *sec, const char *newname);
1330
1331
DESCRIPTION
1332
  Rename section @var{sec} to @var{newname}.
1333
*/
1334
1335
void
1336
bfd_rename_section (asection *sec, const char *newname)
1337
0
{
1338
0
  struct section_hash_entry *sh;
1339
1340
0
  sh = (struct section_hash_entry *)
1341
0
    ((char *) sec - offsetof (struct section_hash_entry, section));
1342
0
  sh->section.name = newname;
1343
0
  bfd_hash_rename (&sec->owner->section_htab, newname, &sh->root);
1344
0
}
1345
1346
/*
1347
FUNCTION
1348
  bfd_map_over_sections
1349
1350
SYNOPSIS
1351
  void bfd_map_over_sections
1352
    (bfd *abfd,
1353
     void (*func) (bfd *abfd, asection *sect, void *obj),
1354
     void *obj);
1355
1356
DESCRIPTION
1357
  Call the provided function @var{func} for each section
1358
  attached to the BFD @var{abfd}, passing @var{obj} as an
1359
  argument. The function will be called as if by
1360
1361
| func (abfd, the_section, obj);
1362
1363
  This is the preferred method for iterating over sections; an
1364
  alternative would be to use a loop:
1365
1366
|    asection *p;
1367
|    for (p = abfd->sections; p != NULL; p = p->next)
1368
|       func (abfd, p, ...)
1369
1370
*/
1371
1372
void
1373
bfd_map_over_sections (bfd *abfd,
1374
           void (*operation) (bfd *, asection *, void *),
1375
           void *user_storage)
1376
0
{
1377
0
  asection *sect;
1378
0
  unsigned int i = 0;
1379
1380
0
  for (sect = abfd->sections; sect != NULL; i++, sect = sect->next)
1381
0
    (*operation) (abfd, sect, user_storage);
1382
1383
0
  if (i != abfd->section_count) /* Debugging */
1384
0
    abort ();
1385
0
}
1386
1387
/*
1388
FUNCTION
1389
  bfd_sections_find_if
1390
1391
SYNOPSIS
1392
  asection *bfd_sections_find_if
1393
    (bfd *abfd,
1394
     bool (*operation) (bfd *abfd, asection *sect, void *obj),
1395
     void *obj);
1396
1397
DESCRIPTION
1398
  Call the provided function @var{operation} for each section
1399
  attached to the BFD @var{abfd}, passing @var{obj} as an
1400
  argument. The function will be called as if by
1401
1402
| operation (abfd, the_section, obj);
1403
1404
  It returns the first section for which @var{operation} returns true.
1405
1406
*/
1407
1408
asection *
1409
bfd_sections_find_if (bfd *abfd,
1410
          bool (*operation) (bfd *, asection *, void *),
1411
          void *user_storage)
1412
0
{
1413
0
  asection *sect;
1414
1415
0
  for (sect = abfd->sections; sect != NULL; sect = sect->next)
1416
0
    if ((*operation) (abfd, sect, user_storage))
1417
0
      break;
1418
1419
0
  return sect;
1420
0
}
1421
1422
/*
1423
FUNCTION
1424
  bfd_set_section_size
1425
1426
SYNOPSIS
1427
  bool bfd_set_section_size (asection *sec, bfd_size_type val);
1428
1429
DESCRIPTION
1430
  Set @var{sec} to the size @var{val}. If the operation is
1431
  ok, then <<TRUE>> is returned, else <<FALSE>>.
1432
1433
  Possible error returns:
1434
  o <<bfd_error_invalid_operation>> -
1435
  Writing has started to the BFD, so setting the size is invalid.
1436
1437
*/
1438
1439
bool
1440
bfd_set_section_size (asection *sec, bfd_size_type val)
1441
0
{
1442
  /* Once you've started writing to any section you cannot create or change
1443
     the size of any others.  */
1444
1445
0
  if (sec->owner == NULL || sec->owner->output_has_begun)
1446
0
    {
1447
0
      bfd_set_error (bfd_error_invalid_operation);
1448
0
      return false;
1449
0
    }
1450
1451
0
  sec->size = val;
1452
0
  return true;
1453
0
}
1454
1455
/*
1456
FUNCTION
1457
  bfd_set_section_contents
1458
1459
SYNOPSIS
1460
  bool bfd_set_section_contents
1461
    (bfd *abfd, asection *section, const void *data,
1462
     file_ptr offset, bfd_size_type count);
1463
1464
DESCRIPTION
1465
  Sets the contents of the section @var{section} in BFD
1466
  @var{abfd} to the data starting in memory at @var{location}.
1467
  The data is written to the output section starting at offset
1468
  @var{offset} for @var{count} octets.
1469
1470
  Normally <<TRUE>> is returned, but <<FALSE>> is returned if
1471
  there was an error.  Possible error returns are:
1472
  o <<bfd_error_no_contents>> -
1473
  The output section does not have the <<SEC_HAS_CONTENTS>>
1474
  attribute, so nothing can be written to it.
1475
  o <<bfd_error_bad_value>> -
1476
  The section is unable to contain all of the data.
1477
  o <<bfd_error_invalid_operation>> -
1478
  The BFD is not writeable.
1479
  o and some more too.
1480
1481
  This routine is front end to the back end function
1482
  <<_bfd_set_section_contents>>.
1483
1484
*/
1485
1486
bool
1487
bfd_set_section_contents (bfd *abfd,
1488
        sec_ptr section,
1489
        const void *location,
1490
        file_ptr offset,
1491
        bfd_size_type count)
1492
0
{
1493
0
  bfd_size_type sz;
1494
1495
0
  if (!(bfd_section_flags (section) & SEC_HAS_CONTENTS))
1496
0
    {
1497
0
      bfd_set_error (bfd_error_no_contents);
1498
0
      return false;
1499
0
    }
1500
1501
0
  sz = section->size;
1502
0
  if ((bfd_size_type) offset > sz
1503
0
      || count > sz - offset
1504
0
      || count != (size_t) count)
1505
0
    {
1506
0
      bfd_set_error (bfd_error_bad_value);
1507
0
      return false;
1508
0
    }
1509
1510
0
  if (!bfd_write_p (abfd))
1511
0
    {
1512
0
      bfd_set_error (bfd_error_invalid_operation);
1513
0
      return false;
1514
0
    }
1515
1516
  /* Record a copy of the data in memory if desired.  */
1517
0
  if (section->contents
1518
0
      && location != section->contents + offset)
1519
0
    memcpy (section->contents + offset, location, (size_t) count);
1520
1521
0
  if (BFD_SEND (abfd, _bfd_set_section_contents,
1522
0
    (abfd, section, location, offset, count)))
1523
0
    {
1524
0
      abfd->output_has_begun = true;
1525
0
      return true;
1526
0
    }
1527
1528
0
  return false;
1529
0
}
1530
1531
/*
1532
FUNCTION
1533
  bfd_get_section_contents
1534
1535
SYNOPSIS
1536
  bool bfd_get_section_contents
1537
    (bfd *abfd, asection *section, void *location, file_ptr offset,
1538
     bfd_size_type count);
1539
1540
DESCRIPTION
1541
  Read data from @var{section} in BFD @var{abfd}
1542
  into memory starting at @var{location}. The data is read at an
1543
  offset of @var{offset} from the start of the input section,
1544
  and is read for @var{count} bytes.
1545
1546
  If the contents of a constructor with the <<SEC_CONSTRUCTOR>>
1547
  flag set are requested or if the section does not have the
1548
  <<SEC_HAS_CONTENTS>> flag set, then the @var{location} is filled
1549
  with zeroes. If no errors occur, <<TRUE>> is returned, else
1550
  <<FALSE>>.
1551
1552
*/
1553
bool
1554
bfd_get_section_contents (bfd *abfd,
1555
        sec_ptr section,
1556
        void *location,
1557
        file_ptr offset,
1558
        bfd_size_type count)
1559
0
{
1560
0
  bfd_size_type sz;
1561
1562
0
  if (section->flags & SEC_CONSTRUCTOR)
1563
0
    {
1564
0
      memset (location, 0, (size_t) count);
1565
0
      return true;
1566
0
    }
1567
1568
0
  sz = bfd_get_section_limit_octets (abfd, section);
1569
0
  if ((bfd_size_type) offset > sz
1570
0
      || count > sz - offset
1571
0
      || count != (size_t) count)
1572
0
    {
1573
0
      bfd_set_error (bfd_error_bad_value);
1574
0
      return false;
1575
0
    }
1576
1577
0
  if (count == 0)
1578
    /* Don't bother.  */
1579
0
    return true;
1580
1581
0
  if ((section->flags & SEC_HAS_CONTENTS) == 0)
1582
0
    {
1583
0
      memset (location, 0, (size_t) count);
1584
0
      return true;
1585
0
    }
1586
1587
0
  if ((section->flags & SEC_IN_MEMORY) != 0)
1588
0
    {
1589
0
      if (section->contents == NULL)
1590
0
  {
1591
    /* This can happen because of errors earlier on in the linking process.
1592
       We do not want to seg-fault here, so clear the flag and return an
1593
       error code.  */
1594
0
    section->flags &= ~ SEC_IN_MEMORY;
1595
0
    bfd_set_error (bfd_error_invalid_operation);
1596
0
    return false;
1597
0
  }
1598
1599
0
      memmove (location, section->contents + offset, (size_t) count);
1600
0
      return true;
1601
0
    }
1602
1603
0
  return BFD_SEND (abfd, _bfd_get_section_contents,
1604
0
       (abfd, section, location, offset, count));
1605
0
}
1606
1607
/*
1608
FUNCTION
1609
  bfd_malloc_and_get_section
1610
1611
SYNOPSIS
1612
  bool bfd_malloc_and_get_section
1613
    (bfd *abfd, asection *section, bfd_byte **buf);
1614
1615
DESCRIPTION
1616
  Read all data from @var{section} in BFD @var{abfd}
1617
  into a buffer, *@var{buf}, malloc'd by this function.
1618
  Return @code{true} on success, @code{false} on failure in which
1619
  case *@var{buf} will be NULL.
1620
*/
1621
1622
bool
1623
bfd_malloc_and_get_section (bfd *abfd, sec_ptr sec, bfd_byte **buf)
1624
0
{
1625
0
  *buf = NULL;
1626
0
  return bfd_get_full_section_contents (abfd, sec, buf);
1627
0
}
1628
/*
1629
FUNCTION
1630
  bfd_copy_private_section_data
1631
1632
SYNOPSIS
1633
  bool bfd_copy_private_section_data
1634
    (bfd *ibfd, asection *isec, bfd *obfd, asection *osec);
1635
1636
DESCRIPTION
1637
  Copy private section information from @var{isec} in the BFD
1638
  @var{ibfd} to the section @var{osec} in the BFD @var{obfd}.
1639
  Return <<TRUE>> on success, <<FALSE>> on error.  Possible error
1640
  returns are:
1641
1642
  o <<bfd_error_no_memory>> -
1643
  Not enough memory exists to create private data for @var{osec}.
1644
1645
.#define bfd_copy_private_section_data(ibfd, isection, obfd, osection) \
1646
. BFD_SEND (obfd, _bfd_copy_private_section_data, \
1647
.     (ibfd, isection, obfd, osection))
1648
*/
1649
1650
/*
1651
FUNCTION
1652
  bfd_generic_is_group_section
1653
1654
SYNOPSIS
1655
  bool bfd_generic_is_group_section (bfd *, const asection *sec);
1656
1657
DESCRIPTION
1658
  Returns TRUE if @var{sec} is a member of a group.
1659
*/
1660
1661
bool
1662
bfd_generic_is_group_section (bfd *abfd ATTRIBUTE_UNUSED,
1663
            const asection *sec ATTRIBUTE_UNUSED)
1664
0
{
1665
0
  return false;
1666
0
}
1667
1668
/*
1669
FUNCTION
1670
  bfd_generic_group_name
1671
1672
SYNOPSIS
1673
  const char *bfd_generic_group_name (bfd *, const asection *sec);
1674
1675
DESCRIPTION
1676
  Returns group name if @var{sec} is a member of a group.
1677
*/
1678
1679
const char *
1680
bfd_generic_group_name (bfd *abfd ATTRIBUTE_UNUSED,
1681
      const asection *sec ATTRIBUTE_UNUSED)
1682
0
{
1683
0
  return NULL;
1684
0
}
1685
1686
/*
1687
FUNCTION
1688
  bfd_generic_discard_group
1689
1690
SYNOPSIS
1691
  bool bfd_generic_discard_group (bfd *abfd, asection *group);
1692
1693
DESCRIPTION
1694
  Remove all members of @var{group} from the output.
1695
*/
1696
1697
bool
1698
bfd_generic_discard_group (bfd *abfd ATTRIBUTE_UNUSED,
1699
         asection *group ATTRIBUTE_UNUSED)
1700
0
{
1701
0
  return true;
1702
0
}
1703
1704
bool
1705
_bfd_nowrite_set_section_contents (bfd *abfd,
1706
           sec_ptr section ATTRIBUTE_UNUSED,
1707
           const void *location ATTRIBUTE_UNUSED,
1708
           file_ptr offset ATTRIBUTE_UNUSED,
1709
           bfd_size_type count ATTRIBUTE_UNUSED)
1710
0
{
1711
0
  return _bfd_bool_bfd_false_error (abfd);
1712
0
}
1713
1714
/*
1715
INTERNAL_FUNCTION
1716
  _bfd_section_size_insane
1717
1718
SYNOPSIS
1719
  bool _bfd_section_size_insane (bfd *abfd, asection *sec);
1720
1721
DESCRIPTION
1722
  Returns true if the given section has a size that indicates
1723
  it cannot be read from file.  Return false if the size is OK
1724
  *or* this function can't say one way or the other.
1725
1726
*/
1727
1728
bool
1729
_bfd_section_size_insane (bfd *abfd, asection *sec)
1730
0
{
1731
0
  bfd_size_type size = bfd_get_section_limit_octets (abfd, sec);
1732
0
  if (size == 0)
1733
0
    return false;
1734
1735
0
  if ((bfd_section_flags (sec) & SEC_IN_MEMORY) != 0
1736
      /* PR 24753: Linker created sections can be larger than
1737
   the file size, eg if they are being used to hold stubs.  */
1738
0
      || (bfd_section_flags (sec) & SEC_LINKER_CREATED) != 0
1739
      /* PR 24753: Sections which have no content should also be
1740
   excluded as they contain no size on disk.  */
1741
0
      || (bfd_section_flags (sec) & SEC_HAS_CONTENTS) == 0
1742
      /* The MMO file format supports its own special compression
1743
   technique, but it uses COMPRESS_SECTION_NONE when loading
1744
   a section's contents.  */
1745
0
      || bfd_get_flavour (abfd) == bfd_target_mmo_flavour)
1746
0
    return false;
1747
1748
0
  ufile_ptr filesize = bfd_get_file_size (abfd);
1749
0
  if (filesize == 0)
1750
0
    return false;
1751
1752
0
  if (sec->compress_status == DECOMPRESS_SECTION_ZSTD
1753
0
      || sec->compress_status == DECOMPRESS_SECTION_ZLIB)
1754
0
    {
1755
      /* PR26946, PR28834: Sanity check compress header uncompressed
1756
   size against the original file size, and check that the
1757
   compressed section can be read from file.  We choose an
1758
   arbitrary uncompressed size of 10x the file size, rather than
1759
   a compress ratio.  The reason being that compiling
1760
   "int aaa..a;" with "a" repeated enough times can result in
1761
   compression ratios without limit for .debug_str, whereas such
1762
   a file will usually also have the enormous symbol
1763
   uncompressed in .symtab.  */
1764
0
     if (size / 10 > filesize)
1765
0
       {
1766
0
   bfd_set_error (bfd_error_bad_value);
1767
0
   return true;
1768
0
       }
1769
0
     size = sec->compressed_size;
1770
0
    }
1771
1772
0
  if ((ufile_ptr) sec->filepos > filesize || size > filesize - sec->filepos)
1773
0
    {
1774
0
      bfd_set_error (bfd_error_file_truncated);
1775
0
      return true;
1776
0
    }
1777
0
  return false;
1778
0
}