xref: /openbsd-src/gnu/usr.bin/binutils/bfd/section.c (revision 007c2a4539b8b8aaa95c5e73e77620090abe113b)
12159047fSniklas /* Object file "section" support for the BFD library.
2b55d4692Sfgsch    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3c074d1c9Sdrahn    2000, 2001, 2002, 2003
4b305b0f1Sespie    Free Software Foundation, Inc.
52159047fSniklas    Written by Cygnus Support.
62159047fSniklas 
72159047fSniklas This file is part of BFD, the Binary File Descriptor library.
82159047fSniklas 
92159047fSniklas This program is free software; you can redistribute it and/or modify
102159047fSniklas it under the terms of the GNU General Public License as published by
112159047fSniklas the Free Software Foundation; either version 2 of the License, or
122159047fSniklas (at your option) any later version.
132159047fSniklas 
142159047fSniklas This program is distributed in the hope that it will be useful,
152159047fSniklas but WITHOUT ANY WARRANTY; without even the implied warranty of
162159047fSniklas MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
172159047fSniklas GNU General Public License for more details.
182159047fSniklas 
192159047fSniklas You should have received a copy of the GNU General Public License
202159047fSniklas along with this program; if not, write to the Free Software
212159047fSniklas Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
222159047fSniklas 
232159047fSniklas /*
242159047fSniklas SECTION
252159047fSniklas 	Sections
262159047fSniklas 
272159047fSniklas 	The raw data contained within a BFD is maintained through the
282159047fSniklas 	section abstraction.  A single BFD may have any number of
292159047fSniklas 	sections.  It keeps hold of them by pointing to the first;
302159047fSniklas 	each one points to the next in the list.
312159047fSniklas 
322159047fSniklas 	Sections are supported in BFD in <<section.c>>.
332159047fSniklas 
342159047fSniklas @menu
352159047fSniklas @* Section Input::
362159047fSniklas @* Section Output::
372159047fSniklas @* typedef asection::
382159047fSniklas @* section prototypes::
392159047fSniklas @end menu
402159047fSniklas 
412159047fSniklas INODE
422159047fSniklas Section Input, Section Output, Sections, Sections
432159047fSniklas SUBSECTION
442159047fSniklas 	Section input
452159047fSniklas 
462159047fSniklas 	When a BFD is opened for reading, the section structures are
472159047fSniklas 	created and attached to the BFD.
482159047fSniklas 
492159047fSniklas 	Each section has a name which describes the section in the
502159047fSniklas 	outside world---for example, <<a.out>> would contain at least
512159047fSniklas 	three sections, called <<.text>>, <<.data>> and <<.bss>>.
522159047fSniklas 
532159047fSniklas 	Names need not be unique; for example a COFF file may have several
542159047fSniklas 	sections named <<.data>>.
552159047fSniklas 
562159047fSniklas 	Sometimes a BFD will contain more than the ``natural'' number of
572159047fSniklas 	sections. A back end may attach other sections containing
582159047fSniklas 	constructor data, or an application may add a section (using
592159047fSniklas 	<<bfd_make_section>>) to the sections attached to an already open
602159047fSniklas 	BFD. For example, the linker creates an extra section
612159047fSniklas 	<<COMMON>> for each input file's BFD to hold information about
622159047fSniklas 	common storage.
632159047fSniklas 
642159047fSniklas 	The raw data is not necessarily read in when
652159047fSniklas 	the section descriptor is created. Some targets may leave the
662159047fSniklas 	data in place until a <<bfd_get_section_contents>> call is
672159047fSniklas 	made. Other back ends may read in all the data at once.  For
682159047fSniklas 	example, an S-record file has to be read once to determine the
692159047fSniklas 	size of the data. An IEEE-695 file doesn't contain raw data in
702159047fSniklas 	sections, but data and relocation expressions intermixed, so
712159047fSniklas 	the data area has to be parsed to get out the data and
722159047fSniklas 	relocations.
732159047fSniklas 
742159047fSniklas INODE
752159047fSniklas Section Output, typedef asection, Section Input, Sections
762159047fSniklas 
772159047fSniklas SUBSECTION
782159047fSniklas 	Section output
792159047fSniklas 
802159047fSniklas 	To write a new object style BFD, the various sections to be
812159047fSniklas 	written have to be created. They are attached to the BFD in
822159047fSniklas 	the same way as input sections; data is written to the
832159047fSniklas 	sections using <<bfd_set_section_contents>>.
842159047fSniklas 
852159047fSniklas 	Any program that creates or combines sections (e.g., the assembler
862159047fSniklas 	and linker) must use the <<asection>> fields <<output_section>> and
872159047fSniklas 	<<output_offset>> to indicate the file sections to which each
882159047fSniklas 	section must be written.  (If the section is being created from
892159047fSniklas 	scratch, <<output_section>> should probably point to the section
902159047fSniklas 	itself and <<output_offset>> should probably be zero.)
912159047fSniklas 
922159047fSniklas 	The data to be written comes from input sections attached
932159047fSniklas 	(via <<output_section>> pointers) to
942159047fSniklas 	the output sections.  The output section structure can be
952159047fSniklas 	considered a filter for the input section: the output section
962159047fSniklas 	determines the vma of the output data and the name, but the
972159047fSniklas 	input section determines the offset into the output section of
982159047fSniklas 	the data to be written.
992159047fSniklas 
1002159047fSniklas 	E.g., to create a section "O", starting at 0x100, 0x123 long,
1012159047fSniklas 	containing two subsections, "A" at offset 0x0 (i.e., at vma
1022159047fSniklas 	0x100) and "B" at offset 0x20 (i.e., at vma 0x120) the <<asection>>
1032159047fSniklas 	structures would look like:
1042159047fSniklas 
1052159047fSniklas |   section name          "A"
1062159047fSniklas |     output_offset   0x00
1072159047fSniklas |     size            0x20
1082159047fSniklas |     output_section ----------->  section name    "O"
1092159047fSniklas |                             |    vma             0x100
1102159047fSniklas |   section name          "B" |    size            0x123
1112159047fSniklas |     output_offset   0x20    |
1122159047fSniklas |     size            0x103   |
1132159047fSniklas |     output_section  --------|
1142159047fSniklas 
1152159047fSniklas SUBSECTION
1162159047fSniklas 	Link orders
1172159047fSniklas 
1182159047fSniklas 	The data within a section is stored in a @dfn{link_order}.
1192159047fSniklas 	These are much like the fixups in <<gas>>.  The link_order
1202159047fSniklas 	abstraction allows a section to grow and shrink within itself.
1212159047fSniklas 
1222159047fSniklas 	A link_order knows how big it is, and which is the next
1232159047fSniklas 	link_order and where the raw data for it is; it also points to
1242159047fSniklas 	a list of relocations which apply to it.
1252159047fSniklas 
1262159047fSniklas 	The link_order is used by the linker to perform relaxing on
1272159047fSniklas 	final code.  The compiler creates code which is as big as
1282159047fSniklas 	necessary to make it work without relaxing, and the user can
1292159047fSniklas 	select whether to relax.  Sometimes relaxing takes a lot of
1302159047fSniklas 	time.  The linker runs around the relocations to see if any
1312159047fSniklas 	are attached to data which can be shrunk, if so it does it on
1322159047fSniklas 	a link_order by link_order basis.
1332159047fSniklas 
1342159047fSniklas */
1352159047fSniklas 
1362159047fSniklas #include "bfd.h"
1372159047fSniklas #include "sysdep.h"
1382159047fSniklas #include "libbfd.h"
139b305b0f1Sespie #include "bfdlink.h"
1402159047fSniklas 
1412159047fSniklas /*
1422159047fSniklas DOCDD
1432159047fSniklas INODE
1442159047fSniklas typedef asection, section prototypes, Section Output, Sections
1452159047fSniklas SUBSECTION
1462159047fSniklas 	typedef asection
1472159047fSniklas 
1482159047fSniklas 	Here is the section structure:
1492159047fSniklas 
1502159047fSniklas CODE_FRAGMENT
1512159047fSniklas .
152b305b0f1Sespie .{* This structure is used for a comdat section, as in PE.  A comdat
153b305b0f1Sespie .   section is associated with a particular symbol.  When the linker
154b305b0f1Sespie .   sees a comdat section, it keeps only one of the sections with a
155b305b0f1Sespie .   given name and associated with a given symbol.  *}
156b305b0f1Sespie .
157b305b0f1Sespie .struct bfd_comdat_info
158b305b0f1Sespie .{
159b305b0f1Sespie .  {* The name of the symbol associated with a comdat section.  *}
160b305b0f1Sespie .  const char *name;
161b305b0f1Sespie .
162b305b0f1Sespie .  {* The local symbol table index of the symbol associated with a
163b305b0f1Sespie .     comdat section.  This is only meaningful to the object file format
164b305b0f1Sespie .     specific code; it is not an index into the list returned by
165b305b0f1Sespie .     bfd_canonicalize_symtab.  *}
166b305b0f1Sespie .  long symbol;
167b305b0f1Sespie .};
168b305b0f1Sespie .
169*007c2a45Smiod .typedef struct bfd_section
1702159047fSniklas .{
1712159047fSniklas .  {* The name of the section; the name isn't a copy, the pointer is
1722159047fSniklas .     the same as that passed to bfd_make_section.  *}
173b55d4692Sfgsch .  const char *name;
174b55d4692Sfgsch .
175b55d4692Sfgsch .  {* A unique sequence number.  *}
176b55d4692Sfgsch .  int id;
1772159047fSniklas .
178c074d1c9Sdrahn .  {* Which section in the bfd; 0..n-1 as sections are created in a bfd.  *}
1792159047fSniklas .  int index;
1802159047fSniklas .
1812159047fSniklas .  {* The next section in the list belonging to the BFD, or NULL.  *}
182*007c2a45Smiod .  struct bfd_section *next;
1832159047fSniklas .
1842159047fSniklas .  {* The field flags contains attributes of the section. Some
1852159047fSniklas .     flags are read in from the object file, and some are
1862159047fSniklas .     synthesized from other information.  *}
1872159047fSniklas .  flagword flags;
1882159047fSniklas .
1892159047fSniklas .#define SEC_NO_FLAGS   0x000
1902159047fSniklas .
1912159047fSniklas .  {* Tells the OS to allocate space for this section when loading.
192b55d4692Sfgsch .     This is clear for a section containing debug information only.  *}
1932159047fSniklas .#define SEC_ALLOC      0x001
1942159047fSniklas .
1952159047fSniklas .  {* Tells the OS to load the section from the file when loading.
1962159047fSniklas .     This is clear for a .bss section.  *}
1972159047fSniklas .#define SEC_LOAD       0x002
1982159047fSniklas .
1992159047fSniklas .  {* The section contains data still to be relocated, so there is
2002159047fSniklas .     some relocation information too.  *}
2012159047fSniklas .#define SEC_RELOC      0x004
2022159047fSniklas .
203c074d1c9Sdrahn .  {* ELF reserves 4 processor specific bits and 8 operating system
204c074d1c9Sdrahn .     specific bits in sh_flags; at present we can get away with just
205c074d1c9Sdrahn .     one in communicating between the assembler and BFD, but this
206c074d1c9Sdrahn .     isn't a good long-term solution.  *}
207c074d1c9Sdrahn .#define SEC_ARCH_BIT_0 0x008
2082159047fSniklas .
209b55d4692Sfgsch .  {* A signal to the OS that the section contains read only data.  *}
2102159047fSniklas .#define SEC_READONLY   0x010
2112159047fSniklas .
2122159047fSniklas .  {* The section contains code only.  *}
2132159047fSniklas .#define SEC_CODE       0x020
2142159047fSniklas .
2152159047fSniklas .  {* The section contains data only.  *}
2162159047fSniklas .#define SEC_DATA       0x040
2172159047fSniklas .
2182159047fSniklas .  {* The section will reside in ROM.  *}
2192159047fSniklas .#define SEC_ROM        0x080
2202159047fSniklas .
2212159047fSniklas .  {* The section contains constructor information. This section
2222159047fSniklas .     type is used by the linker to create lists of constructors and
2232159047fSniklas .     destructors used by <<g++>>. When a back end sees a symbol
2242159047fSniklas .     which should be used in a constructor list, it creates a new
2252159047fSniklas .     section for the type of name (e.g., <<__CTOR_LIST__>>), attaches
2262159047fSniklas .     the symbol to it, and builds a relocation. To build the lists
2272159047fSniklas .     of constructors, all the linker has to do is catenate all the
2282159047fSniklas .     sections called <<__CTOR_LIST__>> and relocate the data
2292159047fSniklas .     contained within - exactly the operations it would peform on
2302159047fSniklas .     standard data.  *}
2312159047fSniklas .#define SEC_CONSTRUCTOR 0x100
2322159047fSniklas .
2332159047fSniklas .  {* The section has contents - a data section could be
2342159047fSniklas .     <<SEC_ALLOC>> | <<SEC_HAS_CONTENTS>>; a debug section could be
2352159047fSniklas .     <<SEC_HAS_CONTENTS>>  *}
2362159047fSniklas .#define SEC_HAS_CONTENTS 0x200
2372159047fSniklas .
2382159047fSniklas .  {* An instruction to the linker to not output the section
2392159047fSniklas .     even if it has information which would normally be written.  *}
2402159047fSniklas .#define SEC_NEVER_LOAD 0x400
2412159047fSniklas .
2422159047fSniklas .  {* The section is a COFF shared library section.  This flag is
2432159047fSniklas .     only for the linker.  If this type of section appears in
2442159047fSniklas .     the input file, the linker must copy it to the output file
2452159047fSniklas .     without changing the vma or size.  FIXME: Although this
2462159047fSniklas .     was originally intended to be general, it really is COFF
2472159047fSniklas .     specific (and the flag was renamed to indicate this).  It
2482159047fSniklas .     might be cleaner to have some more general mechanism to
2492159047fSniklas .     allow the back end to control what the linker does with
2502159047fSniklas .     sections.  *}
2512159047fSniklas .#define SEC_COFF_SHARED_LIBRARY 0x800
2522159047fSniklas .
253c074d1c9Sdrahn .  {* The section contains thread local data.  *}
254c074d1c9Sdrahn .#define SEC_THREAD_LOCAL 0x1000
255c074d1c9Sdrahn .
256b55d4692Sfgsch .  {* The section has GOT references.  This flag is only for the
257b55d4692Sfgsch .     linker, and is currently only used by the elf32-hppa back end.
258b55d4692Sfgsch .     It will be set if global offset table references were detected
259b55d4692Sfgsch .     in this section, which indicate to the linker that the section
260b55d4692Sfgsch .     contains PIC code, and must be handled specially when doing a
261b55d4692Sfgsch .     static link.  *}
262b55d4692Sfgsch .#define SEC_HAS_GOT_REF 0x4000
263b55d4692Sfgsch .
264c88b1d6cSniklas .  {* The section contains common symbols (symbols may be defined
2652159047fSniklas .     multiple times, the value of a symbol is the amount of
2662159047fSniklas .     space it requires, and the largest symbol value is the one
2672159047fSniklas .     used).  Most targets have exactly one of these (which we
2682159047fSniklas .     translate to bfd_com_section_ptr), but ECOFF has two.  *}
2692159047fSniklas .#define SEC_IS_COMMON 0x8000
2702159047fSniklas .
2712159047fSniklas .  {* The section contains only debugging information.  For
2722159047fSniklas .     example, this is set for ELF .debug and .stab sections.
2732159047fSniklas .     strip tests this flag to see if a section can be
2742159047fSniklas .     discarded.  *}
2752159047fSniklas .#define SEC_DEBUGGING 0x10000
2762159047fSniklas .
2772159047fSniklas .  {* The contents of this section are held in memory pointed to
278b55d4692Sfgsch .     by the contents field.  This is checked by bfd_get_section_contents,
279b55d4692Sfgsch .     and the data is retrieved from memory if appropriate.  *}
2802159047fSniklas .#define SEC_IN_MEMORY 0x20000
2812159047fSniklas .
282c88b1d6cSniklas .  {* The contents of this section are to be excluded by the
283c88b1d6cSniklas .     linker for executable and shared objects unless those
284c88b1d6cSniklas .     objects are to be further relocated.  *}
285c88b1d6cSniklas .#define SEC_EXCLUDE 0x40000
286c88b1d6cSniklas .
287c074d1c9Sdrahn .  {* The contents of this section are to be sorted based on the sum of
288c074d1c9Sdrahn .     the symbol and addend values specified by the associated relocation
289c074d1c9Sdrahn .     entries.  Entries without associated relocation entries will be
290c074d1c9Sdrahn .     appended to the end of the section in an unspecified order.  *}
291c88b1d6cSniklas .#define SEC_SORT_ENTRIES 0x80000
292c88b1d6cSniklas .
293c88b1d6cSniklas .  {* When linking, duplicate sections of the same name should be
294c88b1d6cSniklas .     discarded, rather than being combined into a single section as
295c88b1d6cSniklas .     is usually done.  This is similar to how common symbols are
296c88b1d6cSniklas .     handled.  See SEC_LINK_DUPLICATES below.  *}
297c88b1d6cSniklas .#define SEC_LINK_ONCE 0x100000
298c88b1d6cSniklas .
299c88b1d6cSniklas .  {* If SEC_LINK_ONCE is set, this bitfield describes how the linker
300c88b1d6cSniklas .     should handle duplicate sections.  *}
301c88b1d6cSniklas .#define SEC_LINK_DUPLICATES 0x600000
302c88b1d6cSniklas .
303c88b1d6cSniklas .  {* This value for SEC_LINK_DUPLICATES means that duplicate
304c88b1d6cSniklas .     sections with the same name should simply be discarded.  *}
305c88b1d6cSniklas .#define SEC_LINK_DUPLICATES_DISCARD 0x0
306c88b1d6cSniklas .
307c88b1d6cSniklas .  {* This value for SEC_LINK_DUPLICATES means that the linker
308c88b1d6cSniklas .     should warn if there are any duplicate sections, although
309c88b1d6cSniklas .     it should still only link one copy.  *}
310c88b1d6cSniklas .#define SEC_LINK_DUPLICATES_ONE_ONLY 0x200000
311c88b1d6cSniklas .
312c88b1d6cSniklas .  {* This value for SEC_LINK_DUPLICATES means that the linker
313c88b1d6cSniklas .     should warn if any duplicate sections are a different size.  *}
314c88b1d6cSniklas .#define SEC_LINK_DUPLICATES_SAME_SIZE 0x400000
315c88b1d6cSniklas .
316c88b1d6cSniklas .  {* This value for SEC_LINK_DUPLICATES means that the linker
317c88b1d6cSniklas .     should warn if any duplicate sections contain different
318c88b1d6cSniklas .     contents.  *}
319c88b1d6cSniklas .#define SEC_LINK_DUPLICATES_SAME_CONTENTS 0x600000
320c88b1d6cSniklas .
321b305b0f1Sespie .  {* This section was created by the linker as part of dynamic
322b305b0f1Sespie .     relocation or other arcane processing.  It is skipped when
323b305b0f1Sespie .     going through the first-pass output, trusting that someone
324b305b0f1Sespie .     else up the line will take care of it later.  *}
325b305b0f1Sespie .#define SEC_LINKER_CREATED 0x800000
326b305b0f1Sespie .
327b305b0f1Sespie .  {* This section should not be subject to garbage collection.  *}
328b305b0f1Sespie .#define SEC_KEEP 0x1000000
329b305b0f1Sespie .
330b305b0f1Sespie .  {* This section contains "short" data, and should be placed
331b305b0f1Sespie .     "near" the GP.  *}
332b305b0f1Sespie .#define SEC_SMALL_DATA 0x2000000
333b305b0f1Sespie .
334b305b0f1Sespie .  {* This section contains data which may be shared with other
335b305b0f1Sespie .     executables or shared objects.  *}
336b305b0f1Sespie .#define SEC_SHARED 0x4000000
337b305b0f1Sespie .
338b55d4692Sfgsch .  {* When a section with this flag is being linked, then if the size of
339b55d4692Sfgsch .     the input section is less than a page, it should not cross a page
340b55d4692Sfgsch .     boundary.  If the size of the input section is one page or more, it
341b55d4692Sfgsch .     should be aligned on a page boundary.  *}
342b55d4692Sfgsch .#define SEC_BLOCK 0x8000000
343b55d4692Sfgsch .
344b55d4692Sfgsch .  {* Conditionally link this section; do not link if there are no
345b55d4692Sfgsch .     references found to any symbol in the section.  *}
346b55d4692Sfgsch .#define SEC_CLINK 0x10000000
347b55d4692Sfgsch .
348c074d1c9Sdrahn .  {* Attempt to merge identical entities in the section.
349c074d1c9Sdrahn .     Entity size is given in the entsize field.  *}
350c074d1c9Sdrahn .#define SEC_MERGE 0x20000000
351c074d1c9Sdrahn .
352c074d1c9Sdrahn .  {* If given with SEC_MERGE, entities to merge are zero terminated
353c074d1c9Sdrahn .     strings where entsize specifies character size instead of fixed
354c074d1c9Sdrahn .     size entries.  *}
355c074d1c9Sdrahn .#define SEC_STRINGS 0x40000000
356c074d1c9Sdrahn .
357c074d1c9Sdrahn .  {* This section contains data about section groups.  *}
358c074d1c9Sdrahn .#define SEC_GROUP 0x80000000
359c074d1c9Sdrahn .
3602159047fSniklas .  {*  End of section flags.  *}
3612159047fSniklas .
362c88b1d6cSniklas .  {* Some internal packed boolean fields.  *}
363c88b1d6cSniklas .
364c88b1d6cSniklas .  {* See the vma field.  *}
365c88b1d6cSniklas .  unsigned int user_set_vma : 1;
366c88b1d6cSniklas .
367c88b1d6cSniklas .  {* Whether relocations have been processed.  *}
368c88b1d6cSniklas .  unsigned int reloc_done : 1;
369c88b1d6cSniklas .
370c88b1d6cSniklas .  {* A mark flag used by some of the linker backends.  *}
371c88b1d6cSniklas .  unsigned int linker_mark : 1;
372c88b1d6cSniklas .
373b55d4692Sfgsch .  {* Another mark flag used by some of the linker backends.  Set for
374c074d1c9Sdrahn .     output sections that have an input section.  *}
375b55d4692Sfgsch .  unsigned int linker_has_input : 1;
376b55d4692Sfgsch .
377b305b0f1Sespie .  {* A mark flag used by some linker backends for garbage collection.  *}
378b305b0f1Sespie .  unsigned int gc_mark : 1;
379b305b0f1Sespie .
380c074d1c9Sdrahn .  {* The following flags are used by the ELF linker. *}
381c074d1c9Sdrahn .
382c074d1c9Sdrahn .  {* Mark sections which have been allocated to segments.  *}
383b55d4692Sfgsch .  unsigned int segment_mark : 1;
384b55d4692Sfgsch .
385c074d1c9Sdrahn .  {* Type of sec_info information.  *}
386c074d1c9Sdrahn .  unsigned int sec_info_type:3;
387c074d1c9Sdrahn .#define ELF_INFO_TYPE_NONE      0
388c074d1c9Sdrahn .#define ELF_INFO_TYPE_STABS     1
389c074d1c9Sdrahn .#define ELF_INFO_TYPE_MERGE     2
390c074d1c9Sdrahn .#define ELF_INFO_TYPE_EH_FRAME  3
391c074d1c9Sdrahn .#define ELF_INFO_TYPE_JUST_SYMS 4
392c074d1c9Sdrahn .
393c074d1c9Sdrahn .  {* Nonzero if this section uses RELA relocations, rather than REL.  *}
394c074d1c9Sdrahn .  unsigned int use_rela_p:1;
395c074d1c9Sdrahn .
396c074d1c9Sdrahn .  {* Bits used by various backends.  *}
397c074d1c9Sdrahn .  unsigned int has_tls_reloc:1;
398c074d1c9Sdrahn .
399c074d1c9Sdrahn .  {* Nonzero if this section needs the relax finalize pass.  *}
400c074d1c9Sdrahn .  unsigned int need_finalize_relax:1;
401c074d1c9Sdrahn .
402*007c2a45Smiod .  {* Nonzero if this section has a gp reloc.  *}
403*007c2a45Smiod .  unsigned int has_gp_reloc:1;
404*007c2a45Smiod .
405*007c2a45Smiod .  {* Unused bits.  *}
406c074d1c9Sdrahn .  unsigned int flag13:1;
407c074d1c9Sdrahn .  unsigned int flag14:1;
408c074d1c9Sdrahn .  unsigned int flag15:1;
409c074d1c9Sdrahn .  unsigned int flag16:4;
410c074d1c9Sdrahn .  unsigned int flag20:4;
411c074d1c9Sdrahn .  unsigned int flag24:8;
412c074d1c9Sdrahn .
413c88b1d6cSniklas .  {* End of internal packed boolean fields.  *}
414c88b1d6cSniklas .
4152159047fSniklas .  {*  The virtual memory address of the section - where it will be
4162159047fSniklas .      at run time.  The symbols are relocated against this.  The
4172159047fSniklas .      user_set_vma flag is maintained by bfd; if it's not set, the
4182159047fSniklas .      backend can assign addresses (for example, in <<a.out>>, where
4192159047fSniklas .      the default address for <<.data>> is dependent on the specific
4202159047fSniklas .      target and various flags).  *}
4212159047fSniklas .  bfd_vma vma;
4222159047fSniklas .
4232159047fSniklas .  {*  The load address of the section - where it would be in a
4242159047fSniklas .      rom image; really only used for writing section header
4252159047fSniklas .      information.  *}
4262159047fSniklas .  bfd_vma lma;
4272159047fSniklas .
428b305b0f1Sespie .  {* The size of the section in octets, as it will be output.
429b305b0f1Sespie .     Contains a value even if the section has no contents (e.g., the
430b305b0f1Sespie .     size of <<.bss>>).  This will be filled in after relocation.  *}
4312159047fSniklas .  bfd_size_type _cooked_size;
4322159047fSniklas .
433b305b0f1Sespie .  {* The original size on disk of the section, in octets.  Normally this
4342159047fSniklas .     value is the same as the size, but if some relaxing has
4352159047fSniklas .     been done, then this value will be bigger.  *}
4362159047fSniklas .  bfd_size_type _raw_size;
4372159047fSniklas .
4382159047fSniklas .  {* If this section is going to be output, then this value is the
439b305b0f1Sespie .     offset in *bytes* into the output section of the first byte in the
440b305b0f1Sespie .     input section (byte ==> smallest addressable unit on the
441b305b0f1Sespie .     target).  In most cases, if this was going to start at the
442b305b0f1Sespie .     100th octet (8-bit quantity) in the output section, this value
443b305b0f1Sespie .     would be 100.  However, if the target byte size is 16 bits
444b305b0f1Sespie .     (bfd_octets_per_byte is "2"), this value would be 50.  *}
4452159047fSniklas .  bfd_vma output_offset;
4462159047fSniklas .
4472159047fSniklas .  {* The output section through which to map on output.  *}
448*007c2a45Smiod .  struct bfd_section *output_section;
4492159047fSniklas .
4502159047fSniklas .  {* The alignment requirement of the section, as an exponent of 2 -
4512159047fSniklas .     e.g., 3 aligns to 2^3 (or 8).  *}
4522159047fSniklas .  unsigned int alignment_power;
4532159047fSniklas .
4542159047fSniklas .  {* If an input section, a pointer to a vector of relocation
4552159047fSniklas .     records for the data in this section.  *}
4562159047fSniklas .  struct reloc_cache_entry *relocation;
4572159047fSniklas .
4582159047fSniklas .  {* If an output section, a pointer to a vector of pointers to
4592159047fSniklas .     relocation records for the data in this section.  *}
4602159047fSniklas .  struct reloc_cache_entry **orelocation;
4612159047fSniklas .
462c074d1c9Sdrahn .  {* The number of relocation records in one of the above.  *}
4632159047fSniklas .  unsigned reloc_count;
4642159047fSniklas .
4652159047fSniklas .  {* Information below is back end specific - and not always used
4662159047fSniklas .     or updated.  *}
4672159047fSniklas .
468b55d4692Sfgsch .  {* File position of section data.  *}
4692159047fSniklas .  file_ptr filepos;
4702159047fSniklas .
471b55d4692Sfgsch .  {* File position of relocation info.  *}
4722159047fSniklas .  file_ptr rel_filepos;
4732159047fSniklas .
474b55d4692Sfgsch .  {* File position of line data.  *}
4752159047fSniklas .  file_ptr line_filepos;
4762159047fSniklas .
477b55d4692Sfgsch .  {* Pointer to data for applications.  *}
478*007c2a45Smiod .  void *userdata;
4792159047fSniklas .
4802159047fSniklas .  {* If the SEC_IN_MEMORY flag is set, this points to the actual
4812159047fSniklas .     contents.  *}
4822159047fSniklas .  unsigned char *contents;
4832159047fSniklas .
484b55d4692Sfgsch .  {* Attached line number information.  *}
4852159047fSniklas .  alent *lineno;
4862159047fSniklas .
487b55d4692Sfgsch .  {* Number of line number records.  *}
4882159047fSniklas .  unsigned int lineno_count;
4892159047fSniklas .
490c074d1c9Sdrahn .  {* Entity size for merging purposes.  *}
491c074d1c9Sdrahn .  unsigned int entsize;
492c074d1c9Sdrahn .
493b55d4692Sfgsch .  {* Optional information about a COMDAT entry; NULL if not COMDAT.  *}
494b305b0f1Sespie .  struct bfd_comdat_info *comdat;
495b305b0f1Sespie .
496*007c2a45Smiod .  {* Points to the kept section if this section is a link-once section,
497*007c2a45Smiod .     and is discarded.  *}
498*007c2a45Smiod .  struct bfd_section *kept_section;
499*007c2a45Smiod .
5002159047fSniklas .  {* When a section is being output, this value changes as more
501b55d4692Sfgsch .     linenumbers are written out.  *}
5022159047fSniklas .  file_ptr moving_line_filepos;
5032159047fSniklas .
504b55d4692Sfgsch .  {* What the section number is in the target world.  *}
5052159047fSniklas .  int target_index;
5062159047fSniklas .
507*007c2a45Smiod .  void *used_by_bfd;
5082159047fSniklas .
5092159047fSniklas .  {* If this is a constructor section then here is a list of the
5102159047fSniklas .     relocations created to relocate items within it.  *}
5112159047fSniklas .  struct relent_chain *constructor_chain;
5122159047fSniklas .
5132159047fSniklas .  {* The BFD which owns the section.  *}
5142159047fSniklas .  bfd *owner;
5152159047fSniklas .
516c074d1c9Sdrahn .  {* A symbol which points at this section only.  *}
517*007c2a45Smiod .  struct bfd_symbol *symbol;
518*007c2a45Smiod .  struct bfd_symbol **symbol_ptr_ptr;
5192159047fSniklas .
5202159047fSniklas .  struct bfd_link_order *link_order_head;
5212159047fSniklas .  struct bfd_link_order *link_order_tail;
5222159047fSniklas .} asection;
5232159047fSniklas .
5242159047fSniklas .{* These sections are global, and are managed by BFD.  The application
5252159047fSniklas .   and target back end are not permitted to change the values in
5262159047fSniklas .   these sections.  New code should use the section_ptr macros rather
5272159047fSniklas .   than referring directly to the const sections.  The const sections
5282159047fSniklas .   may eventually vanish.  *}
5292159047fSniklas .#define BFD_ABS_SECTION_NAME "*ABS*"
5302159047fSniklas .#define BFD_UND_SECTION_NAME "*UND*"
5312159047fSniklas .#define BFD_COM_SECTION_NAME "*COM*"
5322159047fSniklas .#define BFD_IND_SECTION_NAME "*IND*"
5332159047fSniklas .
534c074d1c9Sdrahn .{* The absolute section.  *}
535*007c2a45Smiod .extern asection bfd_abs_section;
5362159047fSniklas .#define bfd_abs_section_ptr ((asection *) &bfd_abs_section)
5372159047fSniklas .#define bfd_is_abs_section(sec) ((sec) == bfd_abs_section_ptr)
538c074d1c9Sdrahn .{* Pointer to the undefined section.  *}
539*007c2a45Smiod .extern asection bfd_und_section;
5402159047fSniklas .#define bfd_und_section_ptr ((asection *) &bfd_und_section)
5412159047fSniklas .#define bfd_is_und_section(sec) ((sec) == bfd_und_section_ptr)
542c074d1c9Sdrahn .{* Pointer to the common section.  *}
543*007c2a45Smiod .extern asection bfd_com_section;
5442159047fSniklas .#define bfd_com_section_ptr ((asection *) &bfd_com_section)
545c074d1c9Sdrahn .{* Pointer to the indirect section.  *}
546*007c2a45Smiod .extern asection bfd_ind_section;
5472159047fSniklas .#define bfd_ind_section_ptr ((asection *) &bfd_ind_section)
5482159047fSniklas .#define bfd_is_ind_section(sec) ((sec) == bfd_ind_section_ptr)
5492159047fSniklas .
550c074d1c9Sdrahn .#define bfd_is_const_section(SEC)		\
551c074d1c9Sdrahn . (   ((SEC) == bfd_abs_section_ptr)		\
552c074d1c9Sdrahn .  || ((SEC) == bfd_und_section_ptr)		\
553c074d1c9Sdrahn .  || ((SEC) == bfd_com_section_ptr)		\
554c074d1c9Sdrahn .  || ((SEC) == bfd_ind_section_ptr))
555c074d1c9Sdrahn .
556*007c2a45Smiod .extern const struct bfd_symbol * const bfd_abs_symbol;
557*007c2a45Smiod .extern const struct bfd_symbol * const bfd_com_symbol;
558*007c2a45Smiod .extern const struct bfd_symbol * const bfd_und_symbol;
559*007c2a45Smiod .extern const struct bfd_symbol * const bfd_ind_symbol;
5602159047fSniklas .#define bfd_get_section_size_before_reloc(section) \
561*007c2a45Smiod .     ((section)->_raw_size)
5622159047fSniklas .#define bfd_get_section_size_after_reloc(section) \
563b305b0f1Sespie .     ((section)->reloc_done ? (section)->_cooked_size \
564b305b0f1Sespie .                            : (abort (), (bfd_size_type) 1))
565c074d1c9Sdrahn .
566c074d1c9Sdrahn .{* Macros to handle insertion and deletion of a bfd's sections.  These
567c074d1c9Sdrahn .   only handle the list pointers, ie. do not adjust section_count,
568c074d1c9Sdrahn .   target_index etc.  *}
569c074d1c9Sdrahn .#define bfd_section_list_remove(ABFD, PS) \
570c074d1c9Sdrahn .  do							\
571c074d1c9Sdrahn .    {							\
572c074d1c9Sdrahn .      asection **_ps = PS;				\
573c074d1c9Sdrahn .      asection *_s = *_ps;				\
574c074d1c9Sdrahn .      *_ps = _s->next;					\
575c074d1c9Sdrahn .      if (_s->next == NULL)				\
576c074d1c9Sdrahn .        (ABFD)->section_tail = _ps;			\
577c074d1c9Sdrahn .    }							\
578c074d1c9Sdrahn .  while (0)
579c074d1c9Sdrahn .#define bfd_section_list_insert(ABFD, PS, S) \
580c074d1c9Sdrahn .  do							\
581c074d1c9Sdrahn .    {							\
582c074d1c9Sdrahn .      asection **_ps = PS;				\
583c074d1c9Sdrahn .      asection *_s = S;				\
584c074d1c9Sdrahn .      _s->next = *_ps;					\
585c074d1c9Sdrahn .      *_ps = _s;					\
586c074d1c9Sdrahn .      if (_s->next == NULL)				\
587c074d1c9Sdrahn .        (ABFD)->section_tail = &_s->next;		\
588c074d1c9Sdrahn .    }							\
589c074d1c9Sdrahn .  while (0)
590c074d1c9Sdrahn .
5912159047fSniklas */
5922159047fSniklas 
593b305b0f1Sespie /* We use a macro to initialize the static asymbol structures because
594b305b0f1Sespie    traditional C does not permit us to initialize a union member while
595b305b0f1Sespie    gcc warns if we don't initialize it.  */
596b305b0f1Sespie  /* the_bfd, name, value, attr, section [, udata] */
597b305b0f1Sespie #ifdef __STDC__
598b305b0f1Sespie #define GLOBAL_SYM_INIT(NAME, SECTION) \
599b305b0f1Sespie   { 0, NAME, 0, BSF_SECTION_SYM, (asection *) SECTION, { 0 }}
600b305b0f1Sespie #else
601b305b0f1Sespie #define GLOBAL_SYM_INIT(NAME, SECTION) \
602b305b0f1Sespie   { 0, NAME, 0, BSF_SECTION_SYM, (asection *) SECTION }
603b305b0f1Sespie #endif
604b305b0f1Sespie 
6052159047fSniklas /* These symbols are global, not specific to any BFD.  Therefore, anything
6062159047fSniklas    that tries to change them is broken, and should be repaired.  */
607b305b0f1Sespie 
6082159047fSniklas static const asymbol global_syms[] =
6092159047fSniklas {
610b305b0f1Sespie   GLOBAL_SYM_INIT (BFD_COM_SECTION_NAME, &bfd_com_section),
611b305b0f1Sespie   GLOBAL_SYM_INIT (BFD_UND_SECTION_NAME, &bfd_und_section),
612b305b0f1Sespie   GLOBAL_SYM_INIT (BFD_ABS_SECTION_NAME, &bfd_abs_section),
613b305b0f1Sespie   GLOBAL_SYM_INIT (BFD_IND_SECTION_NAME, &bfd_ind_section)
6142159047fSniklas };
6152159047fSniklas 
6162159047fSniklas #define STD_SECTION(SEC, FLAGS, SYM, NAME, IDX)				\
6172159047fSniklas   const asymbol * const SYM = (asymbol *) &global_syms[IDX]; 		\
618*007c2a45Smiod   asection SEC = 							\
619b55d4692Sfgsch     /* name, id,  index, next, flags, user_set_vma, reloc_done,      */	\
620b55d4692Sfgsch     { NAME,  IDX, 0,     NULL, FLAGS, 0,            0,			\
621b305b0f1Sespie 									\
622b55d4692Sfgsch     /* linker_mark, linker_has_input, gc_mark, segment_mark,         */	\
623b55d4692Sfgsch        0,           0,                1,       0,			\
624b305b0f1Sespie 									\
625*007c2a45Smiod     /* sec_info_type, use_rela_p, has_tls_reloc,                     */ \
626*007c2a45Smiod        0,	      0,	  0,					\
627*007c2a45Smiod 									\
628*007c2a45Smiod     /* need_finalize_relax, has_gp_reloc,                            */ \
629*007c2a45Smiod        0,		    0,						\
630c074d1c9Sdrahn 									\
631c074d1c9Sdrahn     /* flag13, flag14, flag15, flag16, flag20, flag24,               */ \
632c074d1c9Sdrahn        0,      0,      0,      0,      0,      0,			\
633c074d1c9Sdrahn 									\
634b55d4692Sfgsch     /* vma, lma, _cooked_size, _raw_size,                            */	\
635b305b0f1Sespie        0,   0,   0,            0,					\
636b305b0f1Sespie 									\
637b55d4692Sfgsch     /* output_offset, output_section,      alignment_power,          */	\
638*007c2a45Smiod        0,             (struct bfd_section *) &SEC, 0,			\
639b305b0f1Sespie 									\
640b55d4692Sfgsch     /* relocation, orelocation, reloc_count, filepos, rel_filepos,   */	\
641b55d4692Sfgsch        NULL,       NULL,        0,           0,       0,		\
642b305b0f1Sespie 									\
643b55d4692Sfgsch     /* line_filepos, userdata, contents, lineno, lineno_count,       */	\
644b55d4692Sfgsch        0,            NULL,     NULL,     NULL,   0,			\
645b55d4692Sfgsch 									\
646*007c2a45Smiod     /* entsize, comdat, kept_section, moving_line_filepos,           */	\
647*007c2a45Smiod        0,       NULL,   NULL,	      0,				\
648b55d4692Sfgsch 									\
649c074d1c9Sdrahn     /* target_index, used_by_bfd, constructor_chain, owner,          */	\
650c074d1c9Sdrahn        0,            NULL,        NULL,              NULL,		\
651b55d4692Sfgsch 									\
652b55d4692Sfgsch     /* symbol,                                                       */	\
653*007c2a45Smiod        (struct bfd_symbol *) &global_syms[IDX],				\
654b55d4692Sfgsch 									\
655b55d4692Sfgsch     /* symbol_ptr_ptr,                                               */	\
656*007c2a45Smiod        (struct bfd_symbol **) &SYM,					\
657b55d4692Sfgsch 									\
658b55d4692Sfgsch     /* link_order_head, link_order_tail                              */	\
659b55d4692Sfgsch        NULL,            NULL						\
660b305b0f1Sespie     }
6612159047fSniklas 
6622159047fSniklas STD_SECTION (bfd_com_section, SEC_IS_COMMON, bfd_com_symbol,
6632159047fSniklas 	     BFD_COM_SECTION_NAME, 0);
6642159047fSniklas STD_SECTION (bfd_und_section, 0, bfd_und_symbol, BFD_UND_SECTION_NAME, 1);
6652159047fSniklas STD_SECTION (bfd_abs_section, 0, bfd_abs_symbol, BFD_ABS_SECTION_NAME, 2);
6662159047fSniklas STD_SECTION (bfd_ind_section, 0, bfd_ind_symbol, BFD_IND_SECTION_NAME, 3);
6672159047fSniklas #undef STD_SECTION
6682159047fSniklas 
669c074d1c9Sdrahn struct section_hash_entry
670c074d1c9Sdrahn {
671c074d1c9Sdrahn   struct bfd_hash_entry root;
672c074d1c9Sdrahn   asection section;
673c074d1c9Sdrahn };
674c074d1c9Sdrahn 
675c074d1c9Sdrahn /* Initialize an entry in the section hash table.  */
676c074d1c9Sdrahn 
677c074d1c9Sdrahn struct bfd_hash_entry *
bfd_section_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)678*007c2a45Smiod bfd_section_hash_newfunc (struct bfd_hash_entry *entry,
679*007c2a45Smiod 			  struct bfd_hash_table *table,
680*007c2a45Smiod 			  const char *string)
681c074d1c9Sdrahn {
682c074d1c9Sdrahn   /* Allocate the structure if it has not already been allocated by a
683c074d1c9Sdrahn      subclass.  */
684c074d1c9Sdrahn   if (entry == NULL)
685c074d1c9Sdrahn     {
686c074d1c9Sdrahn       entry = (struct bfd_hash_entry *)
687c074d1c9Sdrahn 	bfd_hash_allocate (table, sizeof (struct section_hash_entry));
688c074d1c9Sdrahn       if (entry == NULL)
689c074d1c9Sdrahn 	return entry;
690c074d1c9Sdrahn     }
691c074d1c9Sdrahn 
692c074d1c9Sdrahn   /* Call the allocation method of the superclass.  */
693c074d1c9Sdrahn   entry = bfd_hash_newfunc (entry, table, string);
694c074d1c9Sdrahn   if (entry != NULL)
695*007c2a45Smiod     memset (&((struct section_hash_entry *) entry)->section, 0,
696*007c2a45Smiod 	    sizeof (asection));
697c074d1c9Sdrahn 
698c074d1c9Sdrahn   return entry;
699c074d1c9Sdrahn }
700c074d1c9Sdrahn 
701c074d1c9Sdrahn #define section_hash_lookup(table, string, create, copy) \
702c074d1c9Sdrahn   ((struct section_hash_entry *) \
703c074d1c9Sdrahn    bfd_hash_lookup ((table), (string), (create), (copy)))
704c074d1c9Sdrahn 
705c074d1c9Sdrahn /* Initializes a new section.  NEWSECT->NAME is already set.  */
706c074d1c9Sdrahn 
707c074d1c9Sdrahn static asection *
bfd_section_init(bfd * abfd,asection * newsect)708*007c2a45Smiod bfd_section_init (bfd *abfd, asection *newsect)
709c074d1c9Sdrahn {
710c074d1c9Sdrahn   static int section_id = 0x10;  /* id 0 to 3 used by STD_SECTION.  */
711c074d1c9Sdrahn 
712c074d1c9Sdrahn   newsect->id = section_id;
713c074d1c9Sdrahn   newsect->index = abfd->section_count;
714c074d1c9Sdrahn   newsect->owner = abfd;
715c074d1c9Sdrahn 
716c074d1c9Sdrahn   /* Create a symbol whose only job is to point to this section.  This
717c074d1c9Sdrahn      is useful for things like relocs which are relative to the base
718c074d1c9Sdrahn      of a section.  */
719c074d1c9Sdrahn   newsect->symbol = bfd_make_empty_symbol (abfd);
720c074d1c9Sdrahn   if (newsect->symbol == NULL)
721c074d1c9Sdrahn     return NULL;
722c074d1c9Sdrahn 
723c074d1c9Sdrahn   newsect->symbol->name = newsect->name;
724c074d1c9Sdrahn   newsect->symbol->value = 0;
725c074d1c9Sdrahn   newsect->symbol->section = newsect;
726c074d1c9Sdrahn   newsect->symbol->flags = BSF_SECTION_SYM;
727c074d1c9Sdrahn 
728c074d1c9Sdrahn   newsect->symbol_ptr_ptr = &newsect->symbol;
729c074d1c9Sdrahn 
730c074d1c9Sdrahn   if (! BFD_SEND (abfd, _new_section_hook, (abfd, newsect)))
731c074d1c9Sdrahn     return NULL;
732c074d1c9Sdrahn 
733c074d1c9Sdrahn   section_id++;
734c074d1c9Sdrahn   abfd->section_count++;
735c074d1c9Sdrahn   *abfd->section_tail = newsect;
736c074d1c9Sdrahn   abfd->section_tail = &newsect->next;
737c074d1c9Sdrahn   return newsect;
738c074d1c9Sdrahn }
739c074d1c9Sdrahn 
7402159047fSniklas /*
7412159047fSniklas DOCDD
7422159047fSniklas INODE
7432159047fSniklas section prototypes,  , typedef asection, Sections
7442159047fSniklas SUBSECTION
7452159047fSniklas 	Section prototypes
7462159047fSniklas 
7472159047fSniklas These are the functions exported by the section handling part of BFD.
7482159047fSniklas */
7492159047fSniklas 
7502159047fSniklas /*
7512159047fSniklas FUNCTION
752c074d1c9Sdrahn 	bfd_section_list_clear
753c074d1c9Sdrahn 
754c074d1c9Sdrahn SYNOPSIS
755c074d1c9Sdrahn 	void bfd_section_list_clear (bfd *);
756c074d1c9Sdrahn 
757c074d1c9Sdrahn DESCRIPTION
758c074d1c9Sdrahn 	Clears the section list, and also resets the section count and
759c074d1c9Sdrahn 	hash table entries.
760c074d1c9Sdrahn */
761c074d1c9Sdrahn 
762c074d1c9Sdrahn void
bfd_section_list_clear(bfd * abfd)763*007c2a45Smiod bfd_section_list_clear (bfd *abfd)
764c074d1c9Sdrahn {
765c074d1c9Sdrahn   abfd->sections = NULL;
766c074d1c9Sdrahn   abfd->section_tail = &abfd->sections;
767c074d1c9Sdrahn   abfd->section_count = 0;
768*007c2a45Smiod   memset (abfd->section_htab.table, 0,
769c074d1c9Sdrahn 	  abfd->section_htab.size * sizeof (struct bfd_hash_entry *));
770c074d1c9Sdrahn }
771c074d1c9Sdrahn 
772c074d1c9Sdrahn /*
773c074d1c9Sdrahn FUNCTION
7742159047fSniklas 	bfd_get_section_by_name
7752159047fSniklas 
7762159047fSniklas SYNOPSIS
777b55d4692Sfgsch 	asection *bfd_get_section_by_name (bfd *abfd, const char *name);
7782159047fSniklas 
7792159047fSniklas DESCRIPTION
7802159047fSniklas 	Run through @var{abfd} and return the one of the
7812159047fSniklas 	<<asection>>s whose name matches @var{name}, otherwise <<NULL>>.
7822159047fSniklas 	@xref{Sections}, for more information.
7832159047fSniklas 
7842159047fSniklas 	This should only be used in special cases; the normal way to process
7852159047fSniklas 	all sections of a given name is to use <<bfd_map_over_sections>> and
7862159047fSniklas 	<<strcmp>> on the name (or better yet, base it on the section flags
7872159047fSniklas 	or something else) for each section.
7882159047fSniklas */
7892159047fSniklas 
7902159047fSniklas asection *
bfd_get_section_by_name(bfd * abfd,const char * name)791*007c2a45Smiod bfd_get_section_by_name (bfd *abfd, const char *name)
7922159047fSniklas {
793c074d1c9Sdrahn   struct section_hash_entry *sh;
7942159047fSniklas 
795c074d1c9Sdrahn   sh = section_hash_lookup (&abfd->section_htab, name, FALSE, FALSE);
796c074d1c9Sdrahn   if (sh != NULL)
797c074d1c9Sdrahn     return &sh->section;
798c074d1c9Sdrahn 
7992159047fSniklas   return NULL;
8002159047fSniklas }
8012159047fSniklas 
802b55d4692Sfgsch /*
803b55d4692Sfgsch FUNCTION
804b55d4692Sfgsch 	bfd_get_unique_section_name
805b55d4692Sfgsch 
806b55d4692Sfgsch SYNOPSIS
807*007c2a45Smiod 	char *bfd_get_unique_section_name
808*007c2a45Smiod 	  (bfd *abfd, const char *templat, int *count);
809b55d4692Sfgsch 
810b55d4692Sfgsch DESCRIPTION
811b55d4692Sfgsch 	Invent a section name that is unique in @var{abfd} by tacking
812b55d4692Sfgsch 	a dot and a digit suffix onto the original @var{templat}.  If
813b55d4692Sfgsch 	@var{count} is non-NULL, then it specifies the first number
814b55d4692Sfgsch 	tried as a suffix to generate a unique name.  The value
815b55d4692Sfgsch 	pointed to by @var{count} will be incremented in this case.
816b55d4692Sfgsch */
817b55d4692Sfgsch 
818b55d4692Sfgsch char *
bfd_get_unique_section_name(bfd * abfd,const char * templat,int * count)819*007c2a45Smiod bfd_get_unique_section_name (bfd *abfd, const char *templat, int *count)
820b55d4692Sfgsch {
821b55d4692Sfgsch   int num;
822b55d4692Sfgsch   unsigned int len;
823b55d4692Sfgsch   char *sname;
824b55d4692Sfgsch 
825b55d4692Sfgsch   len = strlen (templat);
826*007c2a45Smiod   sname = bfd_malloc (len + 8);
827b55d4692Sfgsch   if (sname == NULL)
828b55d4692Sfgsch     return NULL;
829c074d1c9Sdrahn   memcpy (sname, templat, len);
830b55d4692Sfgsch   num = 1;
831b55d4692Sfgsch   if (count != NULL)
832b55d4692Sfgsch     num = *count;
833b55d4692Sfgsch 
834b55d4692Sfgsch   do
835b55d4692Sfgsch     {
836b55d4692Sfgsch       /* If we have a million sections, something is badly wrong.  */
837b55d4692Sfgsch       if (num > 999999)
838b55d4692Sfgsch 	abort ();
839b55d4692Sfgsch       sprintf (sname + len, ".%d", num++);
840b55d4692Sfgsch     }
841c074d1c9Sdrahn   while (section_hash_lookup (&abfd->section_htab, sname, FALSE, FALSE));
842b55d4692Sfgsch 
843b55d4692Sfgsch   if (count != NULL)
844b55d4692Sfgsch     *count = num;
845b55d4692Sfgsch   return sname;
846b55d4692Sfgsch }
8472159047fSniklas 
8482159047fSniklas /*
8492159047fSniklas FUNCTION
8502159047fSniklas 	bfd_make_section_old_way
8512159047fSniklas 
8522159047fSniklas SYNOPSIS
853b55d4692Sfgsch 	asection *bfd_make_section_old_way (bfd *abfd, const char *name);
8542159047fSniklas 
8552159047fSniklas DESCRIPTION
8562159047fSniklas 	Create a new empty section called @var{name}
8572159047fSniklas 	and attach it to the end of the chain of sections for the
8582159047fSniklas 	BFD @var{abfd}. An attempt to create a section with a name which
8592159047fSniklas 	is already in use returns its pointer without changing the
8602159047fSniklas 	section chain.
8612159047fSniklas 
8622159047fSniklas 	It has the funny name since this is the way it used to be
8632159047fSniklas 	before it was rewritten....
8642159047fSniklas 
8652159047fSniklas 	Possible errors are:
8662159047fSniklas 	o <<bfd_error_invalid_operation>> -
8672159047fSniklas 	If output has already started for this BFD.
8682159047fSniklas 	o <<bfd_error_no_memory>> -
869b305b0f1Sespie 	If memory allocation fails.
8702159047fSniklas 
8712159047fSniklas */
8722159047fSniklas 
8732159047fSniklas asection *
bfd_make_section_old_way(bfd * abfd,const char * name)874*007c2a45Smiod bfd_make_section_old_way (bfd *abfd, const char *name)
8752159047fSniklas {
876c074d1c9Sdrahn   struct section_hash_entry *sh;
877c074d1c9Sdrahn   asection *newsect;
878c074d1c9Sdrahn 
879c074d1c9Sdrahn   if (abfd->output_has_begun)
8802159047fSniklas     {
881c074d1c9Sdrahn       bfd_set_error (bfd_error_invalid_operation);
882c074d1c9Sdrahn       return NULL;
8832159047fSniklas     }
884c074d1c9Sdrahn 
885c074d1c9Sdrahn   if (strcmp (name, BFD_ABS_SECTION_NAME) == 0)
886c074d1c9Sdrahn     return bfd_abs_section_ptr;
887c074d1c9Sdrahn 
888c074d1c9Sdrahn   if (strcmp (name, BFD_COM_SECTION_NAME) == 0)
889c074d1c9Sdrahn     return bfd_com_section_ptr;
890c074d1c9Sdrahn 
891c074d1c9Sdrahn   if (strcmp (name, BFD_UND_SECTION_NAME) == 0)
892c074d1c9Sdrahn     return bfd_und_section_ptr;
893c074d1c9Sdrahn 
894c074d1c9Sdrahn   if (strcmp (name, BFD_IND_SECTION_NAME) == 0)
895c074d1c9Sdrahn     return bfd_ind_section_ptr;
896c074d1c9Sdrahn 
897c074d1c9Sdrahn   sh = section_hash_lookup (&abfd->section_htab, name, TRUE, FALSE);
898c074d1c9Sdrahn   if (sh == NULL)
899c074d1c9Sdrahn     return NULL;
900c074d1c9Sdrahn 
901c074d1c9Sdrahn   newsect = &sh->section;
902c074d1c9Sdrahn   if (newsect->name != NULL)
903c074d1c9Sdrahn     {
904c074d1c9Sdrahn       /* Section already exists.  */
905c074d1c9Sdrahn       return newsect;
906c074d1c9Sdrahn     }
907c074d1c9Sdrahn 
908c074d1c9Sdrahn   newsect->name = name;
909c074d1c9Sdrahn   return bfd_section_init (abfd, newsect);
9102159047fSniklas }
9112159047fSniklas 
9122159047fSniklas /*
9132159047fSniklas FUNCTION
9142159047fSniklas 	bfd_make_section_anyway
9152159047fSniklas 
9162159047fSniklas SYNOPSIS
917b55d4692Sfgsch 	asection *bfd_make_section_anyway (bfd *abfd, const char *name);
9182159047fSniklas 
9192159047fSniklas DESCRIPTION
9202159047fSniklas    Create a new empty section called @var{name} and attach it to the end of
9212159047fSniklas    the chain of sections for @var{abfd}.  Create a new section even if there
9222159047fSniklas    is already a section with that name.
9232159047fSniklas 
9242159047fSniklas    Return <<NULL>> and set <<bfd_error>> on error; possible errors are:
9252159047fSniklas    o <<bfd_error_invalid_operation>> - If output has already started for @var{abfd}.
926b305b0f1Sespie    o <<bfd_error_no_memory>> - If memory allocation fails.
9272159047fSniklas */
9282159047fSniklas 
9292159047fSniklas sec_ptr
bfd_make_section_anyway(bfd * abfd,const char * name)930*007c2a45Smiod bfd_make_section_anyway (bfd *abfd, const char *name)
9312159047fSniklas {
932c074d1c9Sdrahn   struct section_hash_entry *sh;
9332159047fSniklas   asection *newsect;
9342159047fSniklas 
9352159047fSniklas   if (abfd->output_has_begun)
9362159047fSniklas     {
9372159047fSniklas       bfd_set_error (bfd_error_invalid_operation);
9382159047fSniklas       return NULL;
9392159047fSniklas     }
9402159047fSniklas 
941c074d1c9Sdrahn   sh = section_hash_lookup (&abfd->section_htab, name, TRUE, FALSE);
942c074d1c9Sdrahn   if (sh == NULL)
943c074d1c9Sdrahn     return NULL;
9442159047fSniklas 
945c074d1c9Sdrahn   newsect = &sh->section;
946c074d1c9Sdrahn   if (newsect->name != NULL)
947c074d1c9Sdrahn     {
948c074d1c9Sdrahn       /* We are making a section of the same name.  It can't go in
949c074d1c9Sdrahn 	 section_htab without generating a unique section name and
950c074d1c9Sdrahn 	 that would be pointless;  We don't need to traverse the
951c074d1c9Sdrahn 	 hash table.  */
952*007c2a45Smiod       newsect = bfd_zalloc (abfd, sizeof (asection));
9532159047fSniklas       if (newsect == NULL)
9542159047fSniklas 	return NULL;
955c074d1c9Sdrahn     }
9562159047fSniklas 
9572159047fSniklas   newsect->name = name;
958c074d1c9Sdrahn   return bfd_section_init (abfd, newsect);
9592159047fSniklas }
9602159047fSniklas 
9612159047fSniklas /*
9622159047fSniklas FUNCTION
9632159047fSniklas 	bfd_make_section
9642159047fSniklas 
9652159047fSniklas SYNOPSIS
966b55d4692Sfgsch 	asection *bfd_make_section (bfd *, const char *name);
9672159047fSniklas 
9682159047fSniklas DESCRIPTION
9692159047fSniklas    Like <<bfd_make_section_anyway>>, but return <<NULL>> (without calling
9702159047fSniklas    bfd_set_error ()) without changing the section chain if there is already a
9712159047fSniklas    section named @var{name}.  If there is an error, return <<NULL>> and set
9722159047fSniklas    <<bfd_error>>.
9732159047fSniklas */
9742159047fSniklas 
9752159047fSniklas asection *
bfd_make_section(bfd * abfd,const char * name)976*007c2a45Smiod bfd_make_section (bfd *abfd, const char *name)
9772159047fSniklas {
978c074d1c9Sdrahn   struct section_hash_entry *sh;
979c074d1c9Sdrahn   asection *newsect;
9802159047fSniklas 
981c074d1c9Sdrahn   if (abfd->output_has_begun)
9822159047fSniklas     {
983c074d1c9Sdrahn       bfd_set_error (bfd_error_invalid_operation);
9842159047fSniklas       return NULL;
9852159047fSniklas     }
9862159047fSniklas 
987c074d1c9Sdrahn   if (strcmp (name, BFD_ABS_SECTION_NAME) == 0
988c074d1c9Sdrahn       || strcmp (name, BFD_COM_SECTION_NAME) == 0
989c074d1c9Sdrahn       || strcmp (name, BFD_UND_SECTION_NAME) == 0
990c074d1c9Sdrahn       || strcmp (name, BFD_IND_SECTION_NAME) == 0)
991c074d1c9Sdrahn     return NULL;
992c074d1c9Sdrahn 
993c074d1c9Sdrahn   sh = section_hash_lookup (&abfd->section_htab, name, TRUE, FALSE);
994c074d1c9Sdrahn   if (sh == NULL)
995c074d1c9Sdrahn     return NULL;
996c074d1c9Sdrahn 
997c074d1c9Sdrahn   newsect = &sh->section;
998c074d1c9Sdrahn   if (newsect->name != NULL)
999c074d1c9Sdrahn     {
1000c074d1c9Sdrahn       /* Section already exists.  */
1001*007c2a45Smiod       return NULL;
1002c074d1c9Sdrahn     }
1003c074d1c9Sdrahn 
1004c074d1c9Sdrahn   newsect->name = name;
1005c074d1c9Sdrahn   return bfd_section_init (abfd, newsect);
10062159047fSniklas }
10072159047fSniklas 
10082159047fSniklas /*
10092159047fSniklas FUNCTION
10102159047fSniklas 	bfd_set_section_flags
10112159047fSniklas 
10122159047fSniklas SYNOPSIS
1013*007c2a45Smiod 	bfd_boolean bfd_set_section_flags
1014*007c2a45Smiod 	  (bfd *abfd, asection *sec, flagword flags);
10152159047fSniklas 
10162159047fSniklas DESCRIPTION
10172159047fSniklas 	Set the attributes of the section @var{sec} in the BFD
1018c074d1c9Sdrahn 	@var{abfd} to the value @var{flags}. Return <<TRUE>> on success,
1019c074d1c9Sdrahn 	<<FALSE>> on error. Possible error returns are:
10202159047fSniklas 
10212159047fSniklas 	o <<bfd_error_invalid_operation>> -
10222159047fSniklas 	The section cannot have one or more of the attributes
10232159047fSniklas 	requested. For example, a .bss section in <<a.out>> may not
10242159047fSniklas 	have the <<SEC_HAS_CONTENTS>> field set.
10252159047fSniklas 
10262159047fSniklas */
10272159047fSniklas 
1028c074d1c9Sdrahn bfd_boolean
bfd_set_section_flags(bfd * abfd ATTRIBUTE_UNUSED,sec_ptr section,flagword flags)1029*007c2a45Smiod bfd_set_section_flags (bfd *abfd ATTRIBUTE_UNUSED,
1030*007c2a45Smiod 		       sec_ptr section,
1031*007c2a45Smiod 		       flagword flags)
10322159047fSniklas {
10332159047fSniklas #if 0
10342159047fSniklas   /* If you try to copy a text section from an input file (where it
10352159047fSniklas      has the SEC_CODE flag set) to an output file, this loses big if
10362159047fSniklas      the bfd_applicable_section_flags (abfd) doesn't have the SEC_CODE
10372159047fSniklas      set - which it doesn't, at least not for a.out.  FIXME */
10382159047fSniklas 
10392159047fSniklas   if ((flags & bfd_applicable_section_flags (abfd)) != flags)
10402159047fSniklas     {
10412159047fSniklas       bfd_set_error (bfd_error_invalid_operation);
1042c074d1c9Sdrahn       return FALSE;
10432159047fSniklas     }
10442159047fSniklas #endif
10452159047fSniklas 
10462159047fSniklas   section->flags = flags;
1047c074d1c9Sdrahn   return TRUE;
10482159047fSniklas }
10492159047fSniklas 
10502159047fSniklas /*
10512159047fSniklas FUNCTION
10522159047fSniklas 	bfd_map_over_sections
10532159047fSniklas 
10542159047fSniklas SYNOPSIS
1055*007c2a45Smiod 	void bfd_map_over_sections
1056*007c2a45Smiod 	  (bfd *abfd,
1057*007c2a45Smiod 	   void (*func) (bfd *abfd, asection *sect, void *obj),
1058*007c2a45Smiod 	   void *obj);
10592159047fSniklas 
10602159047fSniklas DESCRIPTION
10612159047fSniklas 	Call the provided function @var{func} for each section
10622159047fSniklas 	attached to the BFD @var{abfd}, passing @var{obj} as an
10632159047fSniklas 	argument. The function will be called as if by
10642159047fSniklas 
10652159047fSniklas |	func (abfd, the_section, obj);
10662159047fSniklas 
1067*007c2a45Smiod 	This is the preferred method for iterating over sections; an
10682159047fSniklas 	alternative would be to use a loop:
10692159047fSniklas 
10702159047fSniklas |	   section *p;
10712159047fSniklas |	   for (p = abfd->sections; p != NULL; p = p->next)
10722159047fSniklas |	      func (abfd, p, ...)
10732159047fSniklas 
10742159047fSniklas */
10752159047fSniklas 
10762159047fSniklas void
bfd_map_over_sections(bfd * abfd,void (* operation)(bfd *,asection *,void *),void * user_storage)1077*007c2a45Smiod bfd_map_over_sections (bfd *abfd,
1078*007c2a45Smiod 		       void (*operation) (bfd *, asection *, void *),
1079*007c2a45Smiod 		       void *user_storage)
10802159047fSniklas {
10812159047fSniklas   asection *sect;
10822159047fSniklas   unsigned int i = 0;
10832159047fSniklas 
10842159047fSniklas   for (sect = abfd->sections; sect != NULL; i++, sect = sect->next)
10852159047fSniklas     (*operation) (abfd, sect, user_storage);
10862159047fSniklas 
10872159047fSniklas   if (i != abfd->section_count)	/* Debugging */
10882159047fSniklas     abort ();
10892159047fSniklas }
10902159047fSniklas 
10912159047fSniklas /*
10922159047fSniklas FUNCTION
10932159047fSniklas 	bfd_set_section_size
10942159047fSniklas 
10952159047fSniklas SYNOPSIS
1096*007c2a45Smiod 	bfd_boolean bfd_set_section_size
1097*007c2a45Smiod 	  (bfd *abfd, asection *sec, bfd_size_type val);
10982159047fSniklas 
10992159047fSniklas DESCRIPTION
11002159047fSniklas 	Set @var{sec} to the size @var{val}. If the operation is
1101c074d1c9Sdrahn 	ok, then <<TRUE>> is returned, else <<FALSE>>.
11022159047fSniklas 
11032159047fSniklas 	Possible error returns:
11042159047fSniklas 	o <<bfd_error_invalid_operation>> -
11052159047fSniklas 	Writing has started to the BFD, so setting the size is invalid.
11062159047fSniklas 
11072159047fSniklas */
11082159047fSniklas 
1109c074d1c9Sdrahn bfd_boolean
bfd_set_section_size(bfd * abfd,sec_ptr ptr,bfd_size_type val)1110*007c2a45Smiod bfd_set_section_size (bfd *abfd, sec_ptr ptr, bfd_size_type val)
11112159047fSniklas {
11122159047fSniklas   /* Once you've started writing to any section you cannot create or change
11132159047fSniklas      the size of any others.  */
11142159047fSniklas 
11152159047fSniklas   if (abfd->output_has_begun)
11162159047fSniklas     {
11172159047fSniklas       bfd_set_error (bfd_error_invalid_operation);
1118c074d1c9Sdrahn       return FALSE;
11192159047fSniklas     }
11202159047fSniklas 
11212159047fSniklas   ptr->_cooked_size = val;
11222159047fSniklas   ptr->_raw_size = val;
11232159047fSniklas 
1124c074d1c9Sdrahn   return TRUE;
11252159047fSniklas }
11262159047fSniklas 
11272159047fSniklas /*
11282159047fSniklas FUNCTION
11292159047fSniklas 	bfd_set_section_contents
11302159047fSniklas 
11312159047fSniklas SYNOPSIS
1132*007c2a45Smiod 	bfd_boolean bfd_set_section_contents
1133*007c2a45Smiod 	  (bfd *abfd, asection *section, const void *data,
1134*007c2a45Smiod 	   file_ptr offset, bfd_size_type count);
11352159047fSniklas 
11362159047fSniklas DESCRIPTION
11372159047fSniklas 	Sets the contents of the section @var{section} in BFD
11382159047fSniklas 	@var{abfd} to the data starting in memory at @var{data}. The
11392159047fSniklas 	data is written to the output section starting at offset
1140b305b0f1Sespie 	@var{offset} for @var{count} octets.
11412159047fSniklas 
1142c074d1c9Sdrahn 	Normally <<TRUE>> is returned, else <<FALSE>>. Possible error
11432159047fSniklas 	returns are:
11442159047fSniklas 	o <<bfd_error_no_contents>> -
11452159047fSniklas 	The output section does not have the <<SEC_HAS_CONTENTS>>
11462159047fSniklas 	attribute, so nothing can be written to it.
11472159047fSniklas 	o and some more too
11482159047fSniklas 
11492159047fSniklas 	This routine is front end to the back end function
11502159047fSniklas 	<<_bfd_set_section_contents>>.
11512159047fSniklas 
11522159047fSniklas */
11532159047fSniklas 
11542159047fSniklas #define bfd_get_section_size_now(abfd, sec) \
11552159047fSniklas   (sec->reloc_done \
11562159047fSniklas    ? bfd_get_section_size_after_reloc (sec) \
11572159047fSniklas    : bfd_get_section_size_before_reloc (sec))
11582159047fSniklas 
1159c074d1c9Sdrahn bfd_boolean
bfd_set_section_contents(bfd * abfd,sec_ptr section,const void * location,file_ptr offset,bfd_size_type count)1160*007c2a45Smiod bfd_set_section_contents (bfd *abfd,
1161*007c2a45Smiod 			  sec_ptr section,
1162*007c2a45Smiod 			  const void *location,
1163*007c2a45Smiod 			  file_ptr offset,
1164*007c2a45Smiod 			  bfd_size_type count)
11652159047fSniklas {
11662159047fSniklas   bfd_size_type sz;
11672159047fSniklas 
11682159047fSniklas   if (!(bfd_get_section_flags (abfd, section) & SEC_HAS_CONTENTS))
11692159047fSniklas     {
11702159047fSniklas       bfd_set_error (bfd_error_no_contents);
1171c074d1c9Sdrahn       return FALSE;
11722159047fSniklas     }
11732159047fSniklas 
11742159047fSniklas   sz = bfd_get_section_size_now (abfd, section);
11752159047fSniklas   if ((bfd_size_type) offset > sz
11762159047fSniklas       || count > sz
1177c074d1c9Sdrahn       || offset + count > sz
1178c074d1c9Sdrahn       || count != (size_t) count)
1179c074d1c9Sdrahn     {
1180c074d1c9Sdrahn       bfd_set_error (bfd_error_bad_value);
1181c074d1c9Sdrahn       return FALSE;
1182c074d1c9Sdrahn     }
11832159047fSniklas 
11842159047fSniklas   switch (abfd->direction)
11852159047fSniklas     {
11862159047fSniklas     case read_direction:
11872159047fSniklas     case no_direction:
11882159047fSniklas       bfd_set_error (bfd_error_invalid_operation);
1189c074d1c9Sdrahn       return FALSE;
11902159047fSniklas 
11912159047fSniklas     case write_direction:
11922159047fSniklas       break;
11932159047fSniklas 
11942159047fSniklas     case both_direction:
11952159047fSniklas       /* File is opened for update. `output_has_begun' some time ago when
11962159047fSniklas 	   the file was created.  Do not recompute sections sizes or alignments
11972159047fSniklas 	   in _bfd_set_section_content.  */
1198c074d1c9Sdrahn       abfd->output_has_begun = TRUE;
11992159047fSniklas       break;
12002159047fSniklas     }
12012159047fSniklas 
1202b55d4692Sfgsch   /* Record a copy of the data in memory if desired.  */
1203b55d4692Sfgsch   if (section->contents
1204*007c2a45Smiod       && location != section->contents + offset)
1205c074d1c9Sdrahn     memcpy (section->contents + offset, location, (size_t) count);
1206b55d4692Sfgsch 
12072159047fSniklas   if (BFD_SEND (abfd, _bfd_set_section_contents,
12082159047fSniklas 		(abfd, section, location, offset, count)))
12092159047fSniklas     {
1210c074d1c9Sdrahn       abfd->output_has_begun = TRUE;
1211c074d1c9Sdrahn       return TRUE;
12122159047fSniklas     }
12132159047fSniklas 
1214c074d1c9Sdrahn   return FALSE;
12152159047fSniklas }
12162159047fSniklas 
12172159047fSniklas /*
12182159047fSniklas FUNCTION
12192159047fSniklas 	bfd_get_section_contents
12202159047fSniklas 
12212159047fSniklas SYNOPSIS
1222*007c2a45Smiod 	bfd_boolean bfd_get_section_contents
1223*007c2a45Smiod 	  (bfd *abfd, asection *section, void *location, file_ptr offset,
1224c074d1c9Sdrahn 	   bfd_size_type count);
12252159047fSniklas 
12262159047fSniklas DESCRIPTION
12272159047fSniklas 	Read data from @var{section} in BFD @var{abfd}
12282159047fSniklas 	into memory starting at @var{location}. The data is read at an
12292159047fSniklas 	offset of @var{offset} from the start of the input section,
12302159047fSniklas 	and is read for @var{count} bytes.
12312159047fSniklas 
12322159047fSniklas 	If the contents of a constructor with the <<SEC_CONSTRUCTOR>>
12332159047fSniklas 	flag set are requested or if the section does not have the
12342159047fSniklas 	<<SEC_HAS_CONTENTS>> flag set, then the @var{location} is filled
1235c074d1c9Sdrahn 	with zeroes. If no errors occur, <<TRUE>> is returned, else
1236c074d1c9Sdrahn 	<<FALSE>>.
12372159047fSniklas 
12382159047fSniklas */
1239c074d1c9Sdrahn bfd_boolean
bfd_get_section_contents(bfd * abfd,sec_ptr section,void * location,file_ptr offset,bfd_size_type count)1240*007c2a45Smiod bfd_get_section_contents (bfd *abfd,
1241*007c2a45Smiod 			  sec_ptr section,
1242*007c2a45Smiod 			  void *location,
1243*007c2a45Smiod 			  file_ptr offset,
1244*007c2a45Smiod 			  bfd_size_type count)
12452159047fSniklas {
12462159047fSniklas   bfd_size_type sz;
12472159047fSniklas 
12482159047fSniklas   if (section->flags & SEC_CONSTRUCTOR)
12492159047fSniklas     {
1250c074d1c9Sdrahn       memset (location, 0, (size_t) count);
1251c074d1c9Sdrahn       return TRUE;
12522159047fSniklas     }
12532159047fSniklas 
1254c074d1c9Sdrahn   /* Even if reloc_done is TRUE, this function reads unrelocated
12552159047fSniklas      contents, so we want the raw size.  */
12562159047fSniklas   sz = section->_raw_size;
1257c074d1c9Sdrahn   if ((bfd_size_type) offset > sz
1258c074d1c9Sdrahn       || count > sz
1259c074d1c9Sdrahn       || offset + count > sz
1260c074d1c9Sdrahn       || count != (size_t) count)
1261c074d1c9Sdrahn     {
1262c074d1c9Sdrahn       bfd_set_error (bfd_error_bad_value);
1263c074d1c9Sdrahn       return FALSE;
1264c074d1c9Sdrahn     }
12652159047fSniklas 
12662159047fSniklas   if (count == 0)
12672159047fSniklas     /* Don't bother.  */
1268c074d1c9Sdrahn     return TRUE;
12692159047fSniklas 
12702159047fSniklas   if ((section->flags & SEC_HAS_CONTENTS) == 0)
12712159047fSniklas     {
1272c074d1c9Sdrahn       memset (location, 0, (size_t) count);
1273c074d1c9Sdrahn       return TRUE;
12742159047fSniklas     }
12752159047fSniklas 
12762159047fSniklas   if ((section->flags & SEC_IN_MEMORY) != 0)
12772159047fSniklas     {
12782159047fSniklas       memcpy (location, section->contents + offset, (size_t) count);
1279c074d1c9Sdrahn       return TRUE;
12802159047fSniklas     }
12812159047fSniklas 
12822159047fSniklas   return BFD_SEND (abfd, _bfd_get_section_contents,
12832159047fSniklas 		   (abfd, section, location, offset, count));
12842159047fSniklas }
12852159047fSniklas 
12862159047fSniklas /*
12872159047fSniklas FUNCTION
12882159047fSniklas 	bfd_copy_private_section_data
12892159047fSniklas 
12902159047fSniklas SYNOPSIS
1291*007c2a45Smiod 	bfd_boolean bfd_copy_private_section_data
1292*007c2a45Smiod 	  (bfd *ibfd, asection *isec, bfd *obfd, asection *osec);
12932159047fSniklas 
12942159047fSniklas DESCRIPTION
12952159047fSniklas 	Copy private section information from @var{isec} in the BFD
12962159047fSniklas 	@var{ibfd} to the section @var{osec} in the BFD @var{obfd}.
1297c074d1c9Sdrahn 	Return <<TRUE>> on success, <<FALSE>> on error.  Possible error
12982159047fSniklas 	returns are:
12992159047fSniklas 
13002159047fSniklas 	o <<bfd_error_no_memory>> -
13012159047fSniklas 	Not enough memory exists to create private data for @var{osec}.
13022159047fSniklas 
13032159047fSniklas .#define bfd_copy_private_section_data(ibfd, isection, obfd, osection) \
1304e93f7393Sniklas .     BFD_SEND (obfd, _bfd_copy_private_section_data, \
13052159047fSniklas .		(ibfd, isection, obfd, osection))
13062159047fSniklas */
1307b305b0f1Sespie 
1308b305b0f1Sespie /*
1309b305b0f1Sespie FUNCTION
1310b305b0f1Sespie 	_bfd_strip_section_from_output
1311b305b0f1Sespie 
1312b305b0f1Sespie SYNOPSIS
1313b305b0f1Sespie 	void _bfd_strip_section_from_output
1314b305b0f1Sespie 	  (struct bfd_link_info *info, asection *section);
1315b305b0f1Sespie 
1316b305b0f1Sespie DESCRIPTION
1317b305b0f1Sespie 	Remove @var{section} from the output.  If the output section
1318c074d1c9Sdrahn 	becomes empty, remove it from the output bfd.
1319c074d1c9Sdrahn 
1320c074d1c9Sdrahn 	This function won't actually do anything except twiddle flags
1321c074d1c9Sdrahn 	if called too late in the linking process, when it's not safe
1322c074d1c9Sdrahn 	to remove sections.
1323b305b0f1Sespie */
1324b305b0f1Sespie void
_bfd_strip_section_from_output(struct bfd_link_info * info,asection * s)1325*007c2a45Smiod _bfd_strip_section_from_output (struct bfd_link_info *info, asection *s)
1326b305b0f1Sespie {
1327c074d1c9Sdrahn   asection *os;
1328c074d1c9Sdrahn   asection *is;
1329c074d1c9Sdrahn   bfd *abfd;
1330b305b0f1Sespie 
1331c074d1c9Sdrahn   s->flags |= SEC_EXCLUDE;
1332b305b0f1Sespie 
1333c074d1c9Sdrahn   /* If the section wasn't assigned to an output section, or the
1334c074d1c9Sdrahn      section has been discarded by the linker script, there's nothing
1335c074d1c9Sdrahn      more to do.  */
1336b305b0f1Sespie   os = s->output_section;
1337c074d1c9Sdrahn   if (os == NULL || os->owner == NULL)
1338b55d4692Sfgsch     return;
1339b55d4692Sfgsch 
1340c074d1c9Sdrahn   /* If the output section has other (non-excluded) input sections, we
1341c074d1c9Sdrahn      can't remove it.  */
1342b305b0f1Sespie   for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
1343b305b0f1Sespie     for (is = abfd->sections; is != NULL; is = is->next)
1344c074d1c9Sdrahn       if (is->output_section == os && (is->flags & SEC_EXCLUDE) == 0)
1345c074d1c9Sdrahn 	return;
1346c074d1c9Sdrahn 
1347c074d1c9Sdrahn   /* If the output section is empty, flag it for removal too.
1348c074d1c9Sdrahn      See ldlang.c:strip_excluded_output_sections for the action.  */
1349c074d1c9Sdrahn   os->flags |= SEC_EXCLUDE;
1350b305b0f1Sespie }
1351b305b0f1Sespie 
1352c074d1c9Sdrahn /*
1353c074d1c9Sdrahn FUNCTION
1354c074d1c9Sdrahn 	bfd_generic_discard_group
1355c074d1c9Sdrahn 
1356c074d1c9Sdrahn SYNOPSIS
1357c074d1c9Sdrahn 	bfd_boolean bfd_generic_discard_group (bfd *abfd, asection *group);
1358c074d1c9Sdrahn 
1359c074d1c9Sdrahn DESCRIPTION
1360c074d1c9Sdrahn 	Remove all members of @var{group} from the output.
1361c074d1c9Sdrahn */
1362c074d1c9Sdrahn 
1363c074d1c9Sdrahn bfd_boolean
bfd_generic_discard_group(bfd * abfd ATTRIBUTE_UNUSED,asection * group ATTRIBUTE_UNUSED)1364*007c2a45Smiod bfd_generic_discard_group (bfd *abfd ATTRIBUTE_UNUSED,
1365*007c2a45Smiod 			   asection *group ATTRIBUTE_UNUSED)
1366b305b0f1Sespie {
1367c074d1c9Sdrahn   return TRUE;
1368b305b0f1Sespie }
1369