175fd0b74Schristos /* Object file "section" support for the BFD library.
2*e992f068Schristos Copyright (C) 1990-2022 Free Software Foundation, Inc.
375fd0b74Schristos Written by Cygnus Support.
475fd0b74Schristos
575fd0b74Schristos This file is part of BFD, the Binary File Descriptor library.
675fd0b74Schristos
775fd0b74Schristos This program is free software; you can redistribute it and/or modify
875fd0b74Schristos it under the terms of the GNU General Public License as published by
975fd0b74Schristos the Free Software Foundation; either version 3 of the License, or
1075fd0b74Schristos (at your option) any later version.
1175fd0b74Schristos
1275fd0b74Schristos This program is distributed in the hope that it will be useful,
1375fd0b74Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1475fd0b74Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1575fd0b74Schristos GNU General Public License for more details.
1675fd0b74Schristos
1775fd0b74Schristos You should have received a copy of the GNU General Public License
1875fd0b74Schristos along with this program; if not, write to the Free Software
1975fd0b74Schristos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
2075fd0b74Schristos MA 02110-1301, USA. */
2175fd0b74Schristos
2275fd0b74Schristos /*
2375fd0b74Schristos SECTION
2475fd0b74Schristos Sections
2575fd0b74Schristos
2675fd0b74Schristos The raw data contained within a BFD is maintained through the
2775fd0b74Schristos section abstraction. A single BFD may have any number of
2875fd0b74Schristos sections. It keeps hold of them by pointing to the first;
2975fd0b74Schristos each one points to the next in the list.
3075fd0b74Schristos
3175fd0b74Schristos Sections are supported in BFD in <<section.c>>.
3275fd0b74Schristos
3375fd0b74Schristos @menu
3475fd0b74Schristos @* Section Input::
3575fd0b74Schristos @* Section Output::
3675fd0b74Schristos @* typedef asection::
3775fd0b74Schristos @* section prototypes::
3875fd0b74Schristos @end menu
3975fd0b74Schristos
4075fd0b74Schristos INODE
4175fd0b74Schristos Section Input, Section Output, Sections, Sections
4275fd0b74Schristos SUBSECTION
4375fd0b74Schristos Section input
4475fd0b74Schristos
4575fd0b74Schristos When a BFD is opened for reading, the section structures are
4675fd0b74Schristos created and attached to the BFD.
4775fd0b74Schristos
4875fd0b74Schristos Each section has a name which describes the section in the
4975fd0b74Schristos outside world---for example, <<a.out>> would contain at least
5075fd0b74Schristos three sections, called <<.text>>, <<.data>> and <<.bss>>.
5175fd0b74Schristos
5275fd0b74Schristos Names need not be unique; for example a COFF file may have several
5375fd0b74Schristos sections named <<.data>>.
5475fd0b74Schristos
5575fd0b74Schristos Sometimes a BFD will contain more than the ``natural'' number of
5675fd0b74Schristos sections. A back end may attach other sections containing
5775fd0b74Schristos constructor data, or an application may add a section (using
5875fd0b74Schristos <<bfd_make_section>>) to the sections attached to an already open
5975fd0b74Schristos BFD. For example, the linker creates an extra section
6075fd0b74Schristos <<COMMON>> for each input file's BFD to hold information about
6175fd0b74Schristos common storage.
6275fd0b74Schristos
6375fd0b74Schristos The raw data is not necessarily read in when
6475fd0b74Schristos the section descriptor is created. Some targets may leave the
6575fd0b74Schristos data in place until a <<bfd_get_section_contents>> call is
6675fd0b74Schristos made. Other back ends may read in all the data at once. For
6775fd0b74Schristos example, an S-record file has to be read once to determine the
68ede78133Schristos size of the data.
6975fd0b74Schristos
7075fd0b74Schristos INODE
7175fd0b74Schristos Section Output, typedef asection, Section Input, Sections
7275fd0b74Schristos
7375fd0b74Schristos SUBSECTION
7475fd0b74Schristos Section output
7575fd0b74Schristos
7675fd0b74Schristos To write a new object style BFD, the various sections to be
7775fd0b74Schristos written have to be created. They are attached to the BFD in
7875fd0b74Schristos the same way as input sections; data is written to the
7975fd0b74Schristos sections using <<bfd_set_section_contents>>.
8075fd0b74Schristos
8175fd0b74Schristos Any program that creates or combines sections (e.g., the assembler
8275fd0b74Schristos and linker) must use the <<asection>> fields <<output_section>> and
8375fd0b74Schristos <<output_offset>> to indicate the file sections to which each
8475fd0b74Schristos section must be written. (If the section is being created from
8575fd0b74Schristos scratch, <<output_section>> should probably point to the section
8675fd0b74Schristos itself and <<output_offset>> should probably be zero.)
8775fd0b74Schristos
8875fd0b74Schristos The data to be written comes from input sections attached
8975fd0b74Schristos (via <<output_section>> pointers) to
9075fd0b74Schristos the output sections. The output section structure can be
9175fd0b74Schristos considered a filter for the input section: the output section
9275fd0b74Schristos determines the vma of the output data and the name, but the
9375fd0b74Schristos input section determines the offset into the output section of
9475fd0b74Schristos the data to be written.
9575fd0b74Schristos
9675fd0b74Schristos E.g., to create a section "O", starting at 0x100, 0x123 long,
9775fd0b74Schristos containing two subsections, "A" at offset 0x0 (i.e., at vma
9875fd0b74Schristos 0x100) and "B" at offset 0x20 (i.e., at vma 0x120) the <<asection>>
9975fd0b74Schristos structures would look like:
10075fd0b74Schristos
10175fd0b74Schristos | section name "A"
10275fd0b74Schristos | output_offset 0x00
10375fd0b74Schristos | size 0x20
10475fd0b74Schristos | output_section -----------> section name "O"
10575fd0b74Schristos | | vma 0x100
10675fd0b74Schristos | section name "B" | size 0x123
10775fd0b74Schristos | output_offset 0x20 |
10875fd0b74Schristos | size 0x103 |
10975fd0b74Schristos | output_section --------|
11075fd0b74Schristos
11175fd0b74Schristos SUBSECTION
11275fd0b74Schristos Link orders
11375fd0b74Schristos
11475fd0b74Schristos The data within a section is stored in a @dfn{link_order}.
11575fd0b74Schristos These are much like the fixups in <<gas>>. The link_order
11675fd0b74Schristos abstraction allows a section to grow and shrink within itself.
11775fd0b74Schristos
11875fd0b74Schristos A link_order knows how big it is, and which is the next
11975fd0b74Schristos link_order and where the raw data for it is; it also points to
12075fd0b74Schristos a list of relocations which apply to it.
12175fd0b74Schristos
12275fd0b74Schristos The link_order is used by the linker to perform relaxing on
12375fd0b74Schristos final code. The compiler creates code which is as big as
12475fd0b74Schristos necessary to make it work without relaxing, and the user can
12575fd0b74Schristos select whether to relax. Sometimes relaxing takes a lot of
12675fd0b74Schristos time. The linker runs around the relocations to see if any
12775fd0b74Schristos are attached to data which can be shrunk, if so it does it on
12875fd0b74Schristos a link_order by link_order basis.
12975fd0b74Schristos
13075fd0b74Schristos */
13175fd0b74Schristos
13275fd0b74Schristos #include "sysdep.h"
13375fd0b74Schristos #include "bfd.h"
13475fd0b74Schristos #include "libbfd.h"
13575fd0b74Schristos #include "bfdlink.h"
13675fd0b74Schristos
13775fd0b74Schristos /*
13875fd0b74Schristos DOCDD
13975fd0b74Schristos INODE
14075fd0b74Schristos typedef asection, section prototypes, Section Output, Sections
14175fd0b74Schristos SUBSECTION
14275fd0b74Schristos typedef asection
14375fd0b74Schristos
14475fd0b74Schristos Here is the section structure:
14575fd0b74Schristos
14675fd0b74Schristos CODE_FRAGMENT
14775fd0b74Schristos .
14875fd0b74Schristos .typedef struct bfd_section
14975fd0b74Schristos .{
15075fd0b74Schristos . {* The name of the section; the name isn't a copy, the pointer is
15175fd0b74Schristos . the same as that passed to bfd_make_section. *}
15275fd0b74Schristos . const char *name;
15375fd0b74Schristos .
15475fd0b74Schristos . {* The next section in the list belonging to the BFD, or NULL. *}
15575fd0b74Schristos . struct bfd_section *next;
15675fd0b74Schristos .
15775fd0b74Schristos . {* The previous section in the list belonging to the BFD, or NULL. *}
15875fd0b74Schristos . struct bfd_section *prev;
15975fd0b74Schristos .
160*e992f068Schristos . {* A unique sequence number. *}
161*e992f068Schristos . unsigned int id;
162*e992f068Schristos .
163*e992f068Schristos . {* A unique section number which can be used by assembler to
164*e992f068Schristos . distinguish different sections with the same section name. *}
165*e992f068Schristos . unsigned int section_id;
166*e992f068Schristos .
167*e992f068Schristos . {* Which section in the bfd; 0..n-1 as sections are created in a bfd. *}
168*e992f068Schristos . unsigned int index;
169*e992f068Schristos .
17075fd0b74Schristos . {* The field flags contains attributes of the section. Some
17175fd0b74Schristos . flags are read in from the object file, and some are
17275fd0b74Schristos . synthesized from other information. *}
17375fd0b74Schristos . flagword flags;
17475fd0b74Schristos .
175ede78133Schristos .#define SEC_NO_FLAGS 0x0
17675fd0b74Schristos .
17775fd0b74Schristos . {* Tells the OS to allocate space for this section when loading.
17875fd0b74Schristos . This is clear for a section containing debug information only. *}
179ede78133Schristos .#define SEC_ALLOC 0x1
18075fd0b74Schristos .
18175fd0b74Schristos . {* Tells the OS to load the section from the file when loading.
18275fd0b74Schristos . This is clear for a .bss section. *}
183ede78133Schristos .#define SEC_LOAD 0x2
18475fd0b74Schristos .
18575fd0b74Schristos . {* The section contains data still to be relocated, so there is
18675fd0b74Schristos . some relocation information too. *}
187ede78133Schristos .#define SEC_RELOC 0x4
18875fd0b74Schristos .
18975fd0b74Schristos . {* A signal to the OS that the section contains read only data. *}
190ede78133Schristos .#define SEC_READONLY 0x8
19175fd0b74Schristos .
19275fd0b74Schristos . {* The section contains code only. *}
193ede78133Schristos .#define SEC_CODE 0x10
19475fd0b74Schristos .
19575fd0b74Schristos . {* The section contains data only. *}
196ede78133Schristos .#define SEC_DATA 0x20
19775fd0b74Schristos .
19875fd0b74Schristos . {* The section will reside in ROM. *}
199ede78133Schristos .#define SEC_ROM 0x40
20075fd0b74Schristos .
20175fd0b74Schristos . {* The section contains constructor information. This section
20275fd0b74Schristos . type is used by the linker to create lists of constructors and
20375fd0b74Schristos . destructors used by <<g++>>. When a back end sees a symbol
20475fd0b74Schristos . which should be used in a constructor list, it creates a new
20575fd0b74Schristos . section for the type of name (e.g., <<__CTOR_LIST__>>), attaches
20675fd0b74Schristos . the symbol to it, and builds a relocation. To build the lists
20775fd0b74Schristos . of constructors, all the linker has to do is catenate all the
20875fd0b74Schristos . sections called <<__CTOR_LIST__>> and relocate the data
20975fd0b74Schristos . contained within - exactly the operations it would peform on
21075fd0b74Schristos . standard data. *}
211ede78133Schristos .#define SEC_CONSTRUCTOR 0x80
21275fd0b74Schristos .
21375fd0b74Schristos . {* The section has contents - a data section could be
21475fd0b74Schristos . <<SEC_ALLOC>> | <<SEC_HAS_CONTENTS>>; a debug section could be
21575fd0b74Schristos . <<SEC_HAS_CONTENTS>> *}
21675fd0b74Schristos .#define SEC_HAS_CONTENTS 0x100
21775fd0b74Schristos .
21875fd0b74Schristos . {* An instruction to the linker to not output the section
21975fd0b74Schristos . even if it has information which would normally be written. *}
22075fd0b74Schristos .#define SEC_NEVER_LOAD 0x200
22175fd0b74Schristos .
22275fd0b74Schristos . {* The section contains thread local data. *}
22375fd0b74Schristos .#define SEC_THREAD_LOCAL 0x400
22475fd0b74Schristos .
225ede78133Schristos . {* The section's size is fixed. Generic linker code will not
226ede78133Schristos . recalculate it and it is up to whoever has set this flag to
227ede78133Schristos . get the size right. *}
228ede78133Schristos .#define SEC_FIXED_SIZE 0x800
22975fd0b74Schristos .
23075fd0b74Schristos . {* The section contains common symbols (symbols may be defined
23175fd0b74Schristos . multiple times, the value of a symbol is the amount of
23275fd0b74Schristos . space it requires, and the largest symbol value is the one
23375fd0b74Schristos . used). Most targets have exactly one of these (which we
23475fd0b74Schristos . translate to bfd_com_section_ptr), but ECOFF has two. *}
23575fd0b74Schristos .#define SEC_IS_COMMON 0x1000
23675fd0b74Schristos .
23775fd0b74Schristos . {* The section contains only debugging information. For
23875fd0b74Schristos . example, this is set for ELF .debug and .stab sections.
23975fd0b74Schristos . strip tests this flag to see if a section can be
24075fd0b74Schristos . discarded. *}
24175fd0b74Schristos .#define SEC_DEBUGGING 0x2000
24275fd0b74Schristos .
24375fd0b74Schristos . {* The contents of this section are held in memory pointed to
24475fd0b74Schristos . by the contents field. This is checked by bfd_get_section_contents,
24575fd0b74Schristos . and the data is retrieved from memory if appropriate. *}
24675fd0b74Schristos .#define SEC_IN_MEMORY 0x4000
24775fd0b74Schristos .
24875fd0b74Schristos . {* The contents of this section are to be excluded by the
24975fd0b74Schristos . linker for executable and shared objects unless those
25075fd0b74Schristos . objects are to be further relocated. *}
25175fd0b74Schristos .#define SEC_EXCLUDE 0x8000
25275fd0b74Schristos .
25375fd0b74Schristos . {* The contents of this section are to be sorted based on the sum of
25475fd0b74Schristos . the symbol and addend values specified by the associated relocation
25575fd0b74Schristos . entries. Entries without associated relocation entries will be
25675fd0b74Schristos . appended to the end of the section in an unspecified order. *}
25775fd0b74Schristos .#define SEC_SORT_ENTRIES 0x10000
25875fd0b74Schristos .
25975fd0b74Schristos . {* When linking, duplicate sections of the same name should be
26075fd0b74Schristos . discarded, rather than being combined into a single section as
26175fd0b74Schristos . is usually done. This is similar to how common symbols are
26275fd0b74Schristos . handled. See SEC_LINK_DUPLICATES below. *}
26375fd0b74Schristos .#define SEC_LINK_ONCE 0x20000
26475fd0b74Schristos .
26575fd0b74Schristos . {* If SEC_LINK_ONCE is set, this bitfield describes how the linker
26675fd0b74Schristos . should handle duplicate sections. *}
26775fd0b74Schristos .#define SEC_LINK_DUPLICATES 0xc0000
26875fd0b74Schristos .
26975fd0b74Schristos . {* This value for SEC_LINK_DUPLICATES means that duplicate
27075fd0b74Schristos . sections with the same name should simply be discarded. *}
27175fd0b74Schristos .#define SEC_LINK_DUPLICATES_DISCARD 0x0
27275fd0b74Schristos .
27375fd0b74Schristos . {* This value for SEC_LINK_DUPLICATES means that the linker
27475fd0b74Schristos . should warn if there are any duplicate sections, although
27575fd0b74Schristos . it should still only link one copy. *}
27675fd0b74Schristos .#define SEC_LINK_DUPLICATES_ONE_ONLY 0x40000
27775fd0b74Schristos .
27875fd0b74Schristos . {* This value for SEC_LINK_DUPLICATES means that the linker
27975fd0b74Schristos . should warn if any duplicate sections are a different size. *}
28075fd0b74Schristos .#define SEC_LINK_DUPLICATES_SAME_SIZE 0x80000
28175fd0b74Schristos .
28275fd0b74Schristos . {* This value for SEC_LINK_DUPLICATES means that the linker
28375fd0b74Schristos . should warn if any duplicate sections contain different
28475fd0b74Schristos . contents. *}
28575fd0b74Schristos .#define SEC_LINK_DUPLICATES_SAME_CONTENTS \
28675fd0b74Schristos . (SEC_LINK_DUPLICATES_ONE_ONLY | SEC_LINK_DUPLICATES_SAME_SIZE)
28775fd0b74Schristos .
28875fd0b74Schristos . {* This section was created by the linker as part of dynamic
28975fd0b74Schristos . relocation or other arcane processing. It is skipped when
29075fd0b74Schristos . going through the first-pass output, trusting that someone
29175fd0b74Schristos . else up the line will take care of it later. *}
29275fd0b74Schristos .#define SEC_LINKER_CREATED 0x100000
29375fd0b74Schristos .
294*e992f068Schristos . {* This section contains a section ID to distinguish different
295*e992f068Schristos . sections with the same section name. *}
296*e992f068Schristos .#define SEC_ASSEMBLER_SECTION_ID 0x100000
297*e992f068Schristos .
29875fd0b74Schristos . {* This section should not be subject to garbage collection.
29975fd0b74Schristos . Also set to inform the linker that this section should not be
30075fd0b74Schristos . listed in the link map as discarded. *}
30175fd0b74Schristos .#define SEC_KEEP 0x200000
30275fd0b74Schristos .
30375fd0b74Schristos . {* This section contains "short" data, and should be placed
30475fd0b74Schristos . "near" the GP. *}
30575fd0b74Schristos .#define SEC_SMALL_DATA 0x400000
30675fd0b74Schristos .
30775fd0b74Schristos . {* Attempt to merge identical entities in the section.
30875fd0b74Schristos . Entity size is given in the entsize field. *}
30975fd0b74Schristos .#define SEC_MERGE 0x800000
31075fd0b74Schristos .
31175fd0b74Schristos . {* If given with SEC_MERGE, entities to merge are zero terminated
31275fd0b74Schristos . strings where entsize specifies character size instead of fixed
31375fd0b74Schristos . size entries. *}
31475fd0b74Schristos .#define SEC_STRINGS 0x1000000
31575fd0b74Schristos .
31675fd0b74Schristos . {* This section contains data about section groups. *}
31775fd0b74Schristos .#define SEC_GROUP 0x2000000
31875fd0b74Schristos .
31975fd0b74Schristos . {* The section is a COFF shared library section. This flag is
32075fd0b74Schristos . only for the linker. If this type of section appears in
32175fd0b74Schristos . the input file, the linker must copy it to the output file
32275fd0b74Schristos . without changing the vma or size. FIXME: Although this
32375fd0b74Schristos . was originally intended to be general, it really is COFF
32475fd0b74Schristos . specific (and the flag was renamed to indicate this). It
32575fd0b74Schristos . might be cleaner to have some more general mechanism to
32675fd0b74Schristos . allow the back end to control what the linker does with
32775fd0b74Schristos . sections. *}
32875fd0b74Schristos .#define SEC_COFF_SHARED_LIBRARY 0x4000000
32975fd0b74Schristos .
33075fd0b74Schristos . {* This input section should be copied to output in reverse order
33175fd0b74Schristos . as an array of pointers. This is for ELF linker internal use
33275fd0b74Schristos . only. *}
33375fd0b74Schristos .#define SEC_ELF_REVERSE_COPY 0x4000000
33475fd0b74Schristos .
33575fd0b74Schristos . {* This section contains data which may be shared with other
33675fd0b74Schristos . executables or shared objects. This is for COFF only. *}
33775fd0b74Schristos .#define SEC_COFF_SHARED 0x8000000
33875fd0b74Schristos .
33975fd0b74Schristos . {* This section should be compressed. This is for ELF linker
34075fd0b74Schristos . internal use only. *}
34175fd0b74Schristos .#define SEC_ELF_COMPRESS 0x8000000
34275fd0b74Schristos .
34375fd0b74Schristos . {* When a section with this flag is being linked, then if the size of
34475fd0b74Schristos . the input section is less than a page, it should not cross a page
34575fd0b74Schristos . boundary. If the size of the input section is one page or more,
34675fd0b74Schristos . it should be aligned on a page boundary. This is for TI
34775fd0b74Schristos . TMS320C54X only. *}
34875fd0b74Schristos .#define SEC_TIC54X_BLOCK 0x10000000
34975fd0b74Schristos .
35075fd0b74Schristos . {* This section should be renamed. This is for ELF linker
35175fd0b74Schristos . internal use only. *}
35275fd0b74Schristos .#define SEC_ELF_RENAME 0x10000000
35375fd0b74Schristos .
35475fd0b74Schristos . {* Conditionally link this section; do not link if there are no
35575fd0b74Schristos . references found to any symbol in the section. This is for TI
35675fd0b74Schristos . TMS320C54X only. *}
35775fd0b74Schristos .#define SEC_TIC54X_CLINK 0x20000000
35875fd0b74Schristos .
35975fd0b74Schristos . {* This section contains vliw code. This is for Toshiba MeP only. *}
36075fd0b74Schristos .#define SEC_MEP_VLIW 0x20000000
36175fd0b74Schristos .
362012573ebSchristos . {* All symbols, sizes and relocations in this section are octets
363012573ebSchristos . instead of bytes. Required for DWARF debug sections as DWARF
364012573ebSchristos . information is organized in octets, not bytes. *}
365012573ebSchristos .#define SEC_ELF_OCTETS 0x40000000
366012573ebSchristos .
36775fd0b74Schristos . {* Indicate that section has the no read flag set. This happens
36875fd0b74Schristos . when memory read flag isn't set. *}
36975fd0b74Schristos .#define SEC_COFF_NOREAD 0x40000000
37075fd0b74Schristos .
371ede78133Schristos . {* Indicate that section has the purecode flag set. *}
372ede78133Schristos .#define SEC_ELF_PURECODE 0x80000000
37375fd0b74Schristos .
37475fd0b74Schristos . {* End of section flags. *}
37575fd0b74Schristos .
37675fd0b74Schristos . {* Some internal packed boolean fields. *}
37775fd0b74Schristos .
37875fd0b74Schristos . {* See the vma field. *}
37975fd0b74Schristos . unsigned int user_set_vma : 1;
38075fd0b74Schristos .
38175fd0b74Schristos . {* A mark flag used by some of the linker backends. *}
38275fd0b74Schristos . unsigned int linker_mark : 1;
38375fd0b74Schristos .
38475fd0b74Schristos . {* Another mark flag used by some of the linker backends. Set for
38575fd0b74Schristos . output sections that have an input section. *}
38675fd0b74Schristos . unsigned int linker_has_input : 1;
38775fd0b74Schristos .
38875fd0b74Schristos . {* Mark flag used by some linker backends for garbage collection. *}
38975fd0b74Schristos . unsigned int gc_mark : 1;
39075fd0b74Schristos .
39175fd0b74Schristos . {* Section compression status. *}
39275fd0b74Schristos . unsigned int compress_status : 2;
39375fd0b74Schristos .#define COMPRESS_SECTION_NONE 0
39475fd0b74Schristos .#define COMPRESS_SECTION_DONE 1
39575fd0b74Schristos .#define DECOMPRESS_SECTION_SIZED 2
39675fd0b74Schristos .
39775fd0b74Schristos . {* The following flags are used by the ELF linker. *}
39875fd0b74Schristos .
39975fd0b74Schristos . {* Mark sections which have been allocated to segments. *}
40075fd0b74Schristos . unsigned int segment_mark : 1;
40175fd0b74Schristos .
40275fd0b74Schristos . {* Type of sec_info information. *}
40375fd0b74Schristos . unsigned int sec_info_type:3;
40475fd0b74Schristos .#define SEC_INFO_TYPE_NONE 0
40575fd0b74Schristos .#define SEC_INFO_TYPE_STABS 1
40675fd0b74Schristos .#define SEC_INFO_TYPE_MERGE 2
40775fd0b74Schristos .#define SEC_INFO_TYPE_EH_FRAME 3
40875fd0b74Schristos .#define SEC_INFO_TYPE_JUST_SYMS 4
40975fd0b74Schristos .#define SEC_INFO_TYPE_TARGET 5
41075fd0b74Schristos .#define SEC_INFO_TYPE_EH_FRAME_ENTRY 6
41175fd0b74Schristos .
41275fd0b74Schristos . {* Nonzero if this section uses RELA relocations, rather than REL. *}
41375fd0b74Schristos . unsigned int use_rela_p:1;
41475fd0b74Schristos .
41575fd0b74Schristos . {* Bits used by various backends. The generic code doesn't touch
41675fd0b74Schristos . these fields. *}
41775fd0b74Schristos .
41875fd0b74Schristos . unsigned int sec_flg0:1;
41975fd0b74Schristos . unsigned int sec_flg1:1;
42075fd0b74Schristos . unsigned int sec_flg2:1;
42175fd0b74Schristos . unsigned int sec_flg3:1;
42275fd0b74Schristos . unsigned int sec_flg4:1;
42375fd0b74Schristos . unsigned int sec_flg5:1;
42475fd0b74Schristos .
42575fd0b74Schristos . {* End of internal packed boolean fields. *}
42675fd0b74Schristos .
42775fd0b74Schristos . {* The virtual memory address of the section - where it will be
42875fd0b74Schristos . at run time. The symbols are relocated against this. The
42975fd0b74Schristos . user_set_vma flag is maintained by bfd; if it's not set, the
43075fd0b74Schristos . backend can assign addresses (for example, in <<a.out>>, where
43175fd0b74Schristos . the default address for <<.data>> is dependent on the specific
43275fd0b74Schristos . target and various flags). *}
43375fd0b74Schristos . bfd_vma vma;
43475fd0b74Schristos .
43575fd0b74Schristos . {* The load address of the section - where it would be in a
43675fd0b74Schristos . rom image; really only used for writing section header
43775fd0b74Schristos . information. *}
43875fd0b74Schristos . bfd_vma lma;
43975fd0b74Schristos .
44075fd0b74Schristos . {* The size of the section in *octets*, as it will be output.
44175fd0b74Schristos . Contains a value even if the section has no contents (e.g., the
44275fd0b74Schristos . size of <<.bss>>). *}
44375fd0b74Schristos . bfd_size_type size;
44475fd0b74Schristos .
44575fd0b74Schristos . {* For input sections, the original size on disk of the section, in
44675fd0b74Schristos . octets. This field should be set for any section whose size is
44775fd0b74Schristos . changed by linker relaxation. It is required for sections where
44875fd0b74Schristos . the linker relaxation scheme doesn't cache altered section and
44975fd0b74Schristos . reloc contents (stabs, eh_frame, SEC_MERGE, some coff relaxing
45075fd0b74Schristos . targets), and thus the original size needs to be kept to read the
45175fd0b74Schristos . section multiple times. For output sections, rawsize holds the
45275fd0b74Schristos . section size calculated on a previous linker relaxation pass. *}
45375fd0b74Schristos . bfd_size_type rawsize;
45475fd0b74Schristos .
45575fd0b74Schristos . {* The compressed size of the section in octets. *}
45675fd0b74Schristos . bfd_size_type compressed_size;
45775fd0b74Schristos .
45875fd0b74Schristos . {* If this section is going to be output, then this value is the
45975fd0b74Schristos . offset in *bytes* into the output section of the first byte in the
46075fd0b74Schristos . input section (byte ==> smallest addressable unit on the
46175fd0b74Schristos . target). In most cases, if this was going to start at the
46275fd0b74Schristos . 100th octet (8-bit quantity) in the output section, this value
46375fd0b74Schristos . would be 100. However, if the target byte size is 16 bits
46475fd0b74Schristos . (bfd_octets_per_byte is "2"), this value would be 50. *}
46575fd0b74Schristos . bfd_vma output_offset;
46675fd0b74Schristos .
46775fd0b74Schristos . {* The output section through which to map on output. *}
46875fd0b74Schristos . struct bfd_section *output_section;
46975fd0b74Schristos .
47075fd0b74Schristos . {* If an input section, a pointer to a vector of relocation
47175fd0b74Schristos . records for the data in this section. *}
47275fd0b74Schristos . struct reloc_cache_entry *relocation;
47375fd0b74Schristos .
47475fd0b74Schristos . {* If an output section, a pointer to a vector of pointers to
47575fd0b74Schristos . relocation records for the data in this section. *}
47675fd0b74Schristos . struct reloc_cache_entry **orelocation;
47775fd0b74Schristos .
47875fd0b74Schristos . {* The number of relocation records in one of the above. *}
47975fd0b74Schristos . unsigned reloc_count;
48075fd0b74Schristos .
481*e992f068Schristos . {* The alignment requirement of the section, as an exponent of 2 -
482*e992f068Schristos . e.g., 3 aligns to 2^3 (or 8). *}
483*e992f068Schristos . unsigned int alignment_power;
484*e992f068Schristos .
48575fd0b74Schristos . {* Information below is back end specific - and not always used
48675fd0b74Schristos . or updated. *}
48775fd0b74Schristos .
48875fd0b74Schristos . {* File position of section data. *}
48975fd0b74Schristos . file_ptr filepos;
49075fd0b74Schristos .
49175fd0b74Schristos . {* File position of relocation info. *}
49275fd0b74Schristos . file_ptr rel_filepos;
49375fd0b74Schristos .
49475fd0b74Schristos . {* File position of line data. *}
49575fd0b74Schristos . file_ptr line_filepos;
49675fd0b74Schristos .
49775fd0b74Schristos . {* Pointer to data for applications. *}
49875fd0b74Schristos . void *userdata;
49975fd0b74Schristos .
50075fd0b74Schristos . {* If the SEC_IN_MEMORY flag is set, this points to the actual
50175fd0b74Schristos . contents. *}
50275fd0b74Schristos . unsigned char *contents;
50375fd0b74Schristos .
50475fd0b74Schristos . {* Attached line number information. *}
50575fd0b74Schristos . alent *lineno;
50675fd0b74Schristos .
50775fd0b74Schristos . {* Number of line number records. *}
50875fd0b74Schristos . unsigned int lineno_count;
50975fd0b74Schristos .
51075fd0b74Schristos . {* Entity size for merging purposes. *}
51175fd0b74Schristos . unsigned int entsize;
51275fd0b74Schristos .
51375fd0b74Schristos . {* Points to the kept section if this section is a link-once section,
51475fd0b74Schristos . and is discarded. *}
51575fd0b74Schristos . struct bfd_section *kept_section;
51675fd0b74Schristos .
51775fd0b74Schristos . {* When a section is being output, this value changes as more
51875fd0b74Schristos . linenumbers are written out. *}
51975fd0b74Schristos . file_ptr moving_line_filepos;
52075fd0b74Schristos .
52175fd0b74Schristos . {* What the section number is in the target world. *}
52275fd0b74Schristos . int target_index;
52375fd0b74Schristos .
52475fd0b74Schristos . void *used_by_bfd;
52575fd0b74Schristos .
52675fd0b74Schristos . {* If this is a constructor section then here is a list of the
52775fd0b74Schristos . relocations created to relocate items within it. *}
52875fd0b74Schristos . struct relent_chain *constructor_chain;
52975fd0b74Schristos .
53075fd0b74Schristos . {* The BFD which owns the section. *}
53175fd0b74Schristos . bfd *owner;
53275fd0b74Schristos .
53375fd0b74Schristos . {* A symbol which points at this section only. *}
53475fd0b74Schristos . struct bfd_symbol *symbol;
53575fd0b74Schristos . struct bfd_symbol **symbol_ptr_ptr;
53675fd0b74Schristos .
53775fd0b74Schristos . {* Early in the link process, map_head and map_tail are used to build
53875fd0b74Schristos . a list of input sections attached to an output section. Later,
53975fd0b74Schristos . output sections use these fields for a list of bfd_link_order
540*e992f068Schristos . structs. The linked_to_symbol_name field is for ELF assembler
541*e992f068Schristos . internal use. *}
54275fd0b74Schristos . union {
54375fd0b74Schristos . struct bfd_link_order *link_order;
54475fd0b74Schristos . struct bfd_section *s;
545*e992f068Schristos . const char *linked_to_symbol_name;
54675fd0b74Schristos . } map_head, map_tail;
547*e992f068Schristos .
548*e992f068Schristos . {* Points to the output section this section is already assigned to,
549*e992f068Schristos . if any. This is used when support for non-contiguous memory
550*e992f068Schristos . regions is enabled. *}
551*e992f068Schristos . struct bfd_section *already_assigned;
552*e992f068Schristos .
553*e992f068Schristos . {* Explicitly specified section type, if non-zero. *}
554*e992f068Schristos . unsigned int type;
555*e992f068Schristos .
55675fd0b74Schristos .} asection;
55775fd0b74Schristos .
558012573ebSchristos .static inline const char *
559012573ebSchristos .bfd_section_name (const asection *sec)
560012573ebSchristos .{
561012573ebSchristos . return sec->name;
562012573ebSchristos .}
563012573ebSchristos .
564012573ebSchristos .static inline bfd_size_type
565012573ebSchristos .bfd_section_size (const asection *sec)
566012573ebSchristos .{
567012573ebSchristos . return sec->size;
568012573ebSchristos .}
569012573ebSchristos .
570012573ebSchristos .static inline bfd_vma
571012573ebSchristos .bfd_section_vma (const asection *sec)
572012573ebSchristos .{
573012573ebSchristos . return sec->vma;
574012573ebSchristos .}
575012573ebSchristos .
576012573ebSchristos .static inline bfd_vma
577012573ebSchristos .bfd_section_lma (const asection *sec)
578012573ebSchristos .{
579012573ebSchristos . return sec->lma;
580012573ebSchristos .}
581012573ebSchristos .
582012573ebSchristos .static inline unsigned int
583012573ebSchristos .bfd_section_alignment (const asection *sec)
584012573ebSchristos .{
585012573ebSchristos . return sec->alignment_power;
586012573ebSchristos .}
587012573ebSchristos .
588012573ebSchristos .static inline flagword
589012573ebSchristos .bfd_section_flags (const asection *sec)
590012573ebSchristos .{
591012573ebSchristos . return sec->flags;
592012573ebSchristos .}
593012573ebSchristos .
594012573ebSchristos .static inline void *
595012573ebSchristos .bfd_section_userdata (const asection *sec)
596012573ebSchristos .{
597012573ebSchristos . return sec->userdata;
598012573ebSchristos .}
599*e992f068Schristos .static inline bool
600012573ebSchristos .bfd_is_com_section (const asection *sec)
601012573ebSchristos .{
602012573ebSchristos . return (sec->flags & SEC_IS_COMMON) != 0;
603012573ebSchristos .}
604012573ebSchristos .
60575fd0b74Schristos .{* Note: the following are provided as inline functions rather than macros
60675fd0b74Schristos . because not all callers use the return value. A macro implementation
60775fd0b74Schristos . would use a comma expression, eg: "((ptr)->foo = val, TRUE)" and some
60875fd0b74Schristos . compilers will complain about comma expressions that have no effect. *}
609*e992f068Schristos .static inline bool
610012573ebSchristos .bfd_set_section_userdata (asection *sec, void *val)
61175fd0b74Schristos .{
612012573ebSchristos . sec->userdata = val;
613*e992f068Schristos . return true;
61475fd0b74Schristos .}
61575fd0b74Schristos .
616*e992f068Schristos .static inline bool
617012573ebSchristos .bfd_set_section_vma (asection *sec, bfd_vma val)
61875fd0b74Schristos .{
619012573ebSchristos . sec->vma = sec->lma = val;
620*e992f068Schristos . sec->user_set_vma = true;
621*e992f068Schristos . return true;
62275fd0b74Schristos .}
62375fd0b74Schristos .
624*e992f068Schristos .static inline bool
625012573ebSchristos .bfd_set_section_lma (asection *sec, bfd_vma val)
62675fd0b74Schristos .{
627012573ebSchristos . sec->lma = val;
628*e992f068Schristos . return true;
629012573ebSchristos .}
630012573ebSchristos .
631*e992f068Schristos .static inline bool
632012573ebSchristos .bfd_set_section_alignment (asection *sec, unsigned int val)
633012573ebSchristos .{
634012573ebSchristos . sec->alignment_power = val;
635*e992f068Schristos . return true;
63675fd0b74Schristos .}
63775fd0b74Schristos .
63875fd0b74Schristos .{* These sections are global, and are managed by BFD. The application
63975fd0b74Schristos . and target back end are not permitted to change the values in
64075fd0b74Schristos . these sections. *}
64175fd0b74Schristos .extern asection _bfd_std_section[4];
64275fd0b74Schristos .
64375fd0b74Schristos .#define BFD_ABS_SECTION_NAME "*ABS*"
64475fd0b74Schristos .#define BFD_UND_SECTION_NAME "*UND*"
64575fd0b74Schristos .#define BFD_COM_SECTION_NAME "*COM*"
64675fd0b74Schristos .#define BFD_IND_SECTION_NAME "*IND*"
64775fd0b74Schristos .
64875fd0b74Schristos .{* Pointer to the common section. *}
64975fd0b74Schristos .#define bfd_com_section_ptr (&_bfd_std_section[0])
65075fd0b74Schristos .{* Pointer to the undefined section. *}
65175fd0b74Schristos .#define bfd_und_section_ptr (&_bfd_std_section[1])
65275fd0b74Schristos .{* Pointer to the absolute section. *}
65375fd0b74Schristos .#define bfd_abs_section_ptr (&_bfd_std_section[2])
65475fd0b74Schristos .{* Pointer to the indirect section. *}
65575fd0b74Schristos .#define bfd_ind_section_ptr (&_bfd_std_section[3])
65675fd0b74Schristos .
657*e992f068Schristos .static inline bool
658012573ebSchristos .bfd_is_und_section (const asection *sec)
659012573ebSchristos .{
660012573ebSchristos . return sec == bfd_und_section_ptr;
661012573ebSchristos .}
66275fd0b74Schristos .
663*e992f068Schristos .static inline bool
664012573ebSchristos .bfd_is_abs_section (const asection *sec)
665012573ebSchristos .{
666012573ebSchristos . return sec == bfd_abs_section_ptr;
667012573ebSchristos .}
66875fd0b74Schristos .
669*e992f068Schristos .static inline bool
670012573ebSchristos .bfd_is_ind_section (const asection *sec)
671012573ebSchristos .{
672012573ebSchristos . return sec == bfd_ind_section_ptr;
673012573ebSchristos .}
674012573ebSchristos .
675*e992f068Schristos .static inline bool
676012573ebSchristos .bfd_is_const_section (const asection *sec)
677012573ebSchristos .{
678*e992f068Schristos . return (sec >= _bfd_std_section
679*e992f068Schristos . && sec < _bfd_std_section + (sizeof (_bfd_std_section)
680*e992f068Schristos . / sizeof (_bfd_std_section[0])));
681012573ebSchristos .}
682012573ebSchristos .
683012573ebSchristos .{* Return TRUE if input section SEC has been discarded. *}
684*e992f068Schristos .static inline bool
685012573ebSchristos .discarded_section (const asection *sec)
686012573ebSchristos .{
687012573ebSchristos . return (!bfd_is_abs_section (sec)
688012573ebSchristos . && bfd_is_abs_section (sec->output_section)
689012573ebSchristos . && sec->sec_info_type != SEC_INFO_TYPE_MERGE
690012573ebSchristos . && sec->sec_info_type != SEC_INFO_TYPE_JUST_SYMS);
691012573ebSchristos .}
69275fd0b74Schristos .
693ede78133Schristos .#define BFD_FAKE_SECTION(SEC, SYM, NAME, IDX, FLAGS) \
694*e992f068Schristos . {* name, next, prev, id, section_id, index, flags, user_set_vma, *} \
695*e992f068Schristos . { NAME, NULL, NULL, IDX, 0, 0, FLAGS, 0, \
69675fd0b74Schristos . \
69775fd0b74Schristos . {* linker_mark, linker_has_input, gc_mark, decompress_status, *} \
69875fd0b74Schristos . 0, 0, 1, 0, \
69975fd0b74Schristos . \
70075fd0b74Schristos . {* segment_mark, sec_info_type, use_rela_p, *} \
70175fd0b74Schristos . 0, 0, 0, \
70275fd0b74Schristos . \
70375fd0b74Schristos . {* sec_flg0, sec_flg1, sec_flg2, sec_flg3, sec_flg4, sec_flg5, *} \
70475fd0b74Schristos . 0, 0, 0, 0, 0, 0, \
70575fd0b74Schristos . \
706*e992f068Schristos . {* vma, lma, size, rawsize, compressed_size, *} \
707*e992f068Schristos . 0, 0, 0, 0, 0, \
70875fd0b74Schristos . \
709*e992f068Schristos . {* output_offset, output_section, relocation, orelocation, *} \
710*e992f068Schristos . 0, &SEC, NULL, NULL, \
71175fd0b74Schristos . \
712*e992f068Schristos . {* reloc_count, alignment_power, filepos, rel_filepos, *} \
713*e992f068Schristos . 0, 0, 0, 0, \
71475fd0b74Schristos . \
71575fd0b74Schristos . {* line_filepos, userdata, contents, lineno, lineno_count, *} \
71675fd0b74Schristos . 0, NULL, NULL, NULL, 0, \
71775fd0b74Schristos . \
71875fd0b74Schristos . {* entsize, kept_section, moving_line_filepos, *} \
71975fd0b74Schristos . 0, NULL, 0, \
72075fd0b74Schristos . \
72175fd0b74Schristos . {* target_index, used_by_bfd, constructor_chain, owner, *} \
72275fd0b74Schristos . 0, NULL, NULL, NULL, \
72375fd0b74Schristos . \
72475fd0b74Schristos . {* symbol, symbol_ptr_ptr, *} \
72575fd0b74Schristos . (struct bfd_symbol *) SYM, &SEC.symbol, \
72675fd0b74Schristos . \
727*e992f068Schristos . {* map_head, map_tail, already_assigned, type *} \
728*e992f068Schristos . { NULL }, { NULL }, NULL, 0 \
729*e992f068Schristos . \
73075fd0b74Schristos . }
73175fd0b74Schristos .
732ede78133Schristos .{* We use a macro to initialize the static asymbol structures because
733ede78133Schristos . traditional C does not permit us to initialize a union member while
734ede78133Schristos . gcc warns if we don't initialize it.
735ede78133Schristos . the_bfd, name, value, attr, section [, udata] *}
736ede78133Schristos .#ifdef __STDC__
737ede78133Schristos .#define GLOBAL_SYM_INIT(NAME, SECTION) \
738ede78133Schristos . { 0, NAME, 0, BSF_SECTION_SYM, SECTION, { 0 }}
739ede78133Schristos .#else
740ede78133Schristos .#define GLOBAL_SYM_INIT(NAME, SECTION) \
741ede78133Schristos . { 0, NAME, 0, BSF_SECTION_SYM, SECTION }
742ede78133Schristos .#endif
743ede78133Schristos .
74475fd0b74Schristos */
74575fd0b74Schristos
74675fd0b74Schristos /* These symbols are global, not specific to any BFD. Therefore, anything
74775fd0b74Schristos that tries to change them is broken, and should be repaired. */
74875fd0b74Schristos
74975fd0b74Schristos static const asymbol global_syms[] =
75075fd0b74Schristos {
75175fd0b74Schristos GLOBAL_SYM_INIT (BFD_COM_SECTION_NAME, bfd_com_section_ptr),
75275fd0b74Schristos GLOBAL_SYM_INIT (BFD_UND_SECTION_NAME, bfd_und_section_ptr),
75375fd0b74Schristos GLOBAL_SYM_INIT (BFD_ABS_SECTION_NAME, bfd_abs_section_ptr),
75475fd0b74Schristos GLOBAL_SYM_INIT (BFD_IND_SECTION_NAME, bfd_ind_section_ptr)
75575fd0b74Schristos };
75675fd0b74Schristos
75775fd0b74Schristos #define STD_SECTION(NAME, IDX, FLAGS) \
758ede78133Schristos BFD_FAKE_SECTION(_bfd_std_section[IDX], &global_syms[IDX], NAME, IDX, FLAGS)
75975fd0b74Schristos
76075fd0b74Schristos asection _bfd_std_section[] = {
76175fd0b74Schristos STD_SECTION (BFD_COM_SECTION_NAME, 0, SEC_IS_COMMON),
76275fd0b74Schristos STD_SECTION (BFD_UND_SECTION_NAME, 1, 0),
76375fd0b74Schristos STD_SECTION (BFD_ABS_SECTION_NAME, 2, 0),
76475fd0b74Schristos STD_SECTION (BFD_IND_SECTION_NAME, 3, 0)
76575fd0b74Schristos };
76675fd0b74Schristos #undef STD_SECTION
76775fd0b74Schristos
76875fd0b74Schristos /* Initialize an entry in the section hash table. */
76975fd0b74Schristos
77075fd0b74Schristos struct bfd_hash_entry *
bfd_section_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)77175fd0b74Schristos bfd_section_hash_newfunc (struct bfd_hash_entry *entry,
77275fd0b74Schristos struct bfd_hash_table *table,
77375fd0b74Schristos const char *string)
77475fd0b74Schristos {
77575fd0b74Schristos /* Allocate the structure if it has not already been allocated by a
77675fd0b74Schristos subclass. */
77775fd0b74Schristos if (entry == NULL)
77875fd0b74Schristos {
77975fd0b74Schristos entry = (struct bfd_hash_entry *)
78075fd0b74Schristos bfd_hash_allocate (table, sizeof (struct section_hash_entry));
78175fd0b74Schristos if (entry == NULL)
78275fd0b74Schristos return entry;
78375fd0b74Schristos }
78475fd0b74Schristos
78575fd0b74Schristos /* Call the allocation method of the superclass. */
78675fd0b74Schristos entry = bfd_hash_newfunc (entry, table, string);
78775fd0b74Schristos if (entry != NULL)
78875fd0b74Schristos memset (&((struct section_hash_entry *) entry)->section, 0,
78975fd0b74Schristos sizeof (asection));
79075fd0b74Schristos
79175fd0b74Schristos return entry;
79275fd0b74Schristos }
79375fd0b74Schristos
79475fd0b74Schristos #define section_hash_lookup(table, string, create, copy) \
79575fd0b74Schristos ((struct section_hash_entry *) \
79675fd0b74Schristos bfd_hash_lookup ((table), (string), (create), (copy)))
79775fd0b74Schristos
79875fd0b74Schristos /* Create a symbol whose only job is to point to this section. This
79975fd0b74Schristos is useful for things like relocs which are relative to the base
80075fd0b74Schristos of a section. */
80175fd0b74Schristos
802*e992f068Schristos bool
_bfd_generic_new_section_hook(bfd * abfd,asection * newsect)80375fd0b74Schristos _bfd_generic_new_section_hook (bfd *abfd, asection *newsect)
80475fd0b74Schristos {
80575fd0b74Schristos newsect->symbol = bfd_make_empty_symbol (abfd);
80675fd0b74Schristos if (newsect->symbol == NULL)
807*e992f068Schristos return false;
80875fd0b74Schristos
80975fd0b74Schristos newsect->symbol->name = newsect->name;
81075fd0b74Schristos newsect->symbol->value = 0;
81175fd0b74Schristos newsect->symbol->section = newsect;
81275fd0b74Schristos newsect->symbol->flags = BSF_SECTION_SYM;
81375fd0b74Schristos
81475fd0b74Schristos newsect->symbol_ptr_ptr = &newsect->symbol;
815*e992f068Schristos return true;
81675fd0b74Schristos }
81775fd0b74Schristos
818ede78133Schristos unsigned int _bfd_section_id = 0x10; /* id 0 to 3 used by STD_SECTION. */
81975fd0b74Schristos
82075fd0b74Schristos /* Initializes a new section. NEWSECT->NAME is already set. */
82175fd0b74Schristos
82275fd0b74Schristos static asection *
bfd_section_init(bfd * abfd,asection * newsect)82375fd0b74Schristos bfd_section_init (bfd *abfd, asection *newsect)
82475fd0b74Schristos {
825ede78133Schristos newsect->id = _bfd_section_id;
82675fd0b74Schristos newsect->index = abfd->section_count;
82775fd0b74Schristos newsect->owner = abfd;
82875fd0b74Schristos
82975fd0b74Schristos if (! BFD_SEND (abfd, _new_section_hook, (abfd, newsect)))
83075fd0b74Schristos return NULL;
83175fd0b74Schristos
832ede78133Schristos _bfd_section_id++;
83375fd0b74Schristos abfd->section_count++;
83475fd0b74Schristos bfd_section_list_append (abfd, newsect);
83575fd0b74Schristos return newsect;
83675fd0b74Schristos }
83775fd0b74Schristos
83875fd0b74Schristos /*
83975fd0b74Schristos DOCDD
84075fd0b74Schristos INODE
84175fd0b74Schristos section prototypes, , typedef asection, Sections
84275fd0b74Schristos SUBSECTION
84375fd0b74Schristos Section prototypes
84475fd0b74Schristos
84575fd0b74Schristos These are the functions exported by the section handling part of BFD.
84675fd0b74Schristos */
84775fd0b74Schristos
84875fd0b74Schristos /*
84975fd0b74Schristos FUNCTION
85075fd0b74Schristos bfd_section_list_clear
85175fd0b74Schristos
85275fd0b74Schristos SYNOPSIS
85375fd0b74Schristos void bfd_section_list_clear (bfd *);
85475fd0b74Schristos
85575fd0b74Schristos DESCRIPTION
85675fd0b74Schristos Clears the section list, and also resets the section count and
85775fd0b74Schristos hash table entries.
85875fd0b74Schristos */
85975fd0b74Schristos
86075fd0b74Schristos void
bfd_section_list_clear(bfd * abfd)86175fd0b74Schristos bfd_section_list_clear (bfd *abfd)
86275fd0b74Schristos {
86375fd0b74Schristos abfd->sections = NULL;
86475fd0b74Schristos abfd->section_last = NULL;
86575fd0b74Schristos abfd->section_count = 0;
86675fd0b74Schristos memset (abfd->section_htab.table, 0,
86775fd0b74Schristos abfd->section_htab.size * sizeof (struct bfd_hash_entry *));
86875fd0b74Schristos abfd->section_htab.count = 0;
86975fd0b74Schristos }
87075fd0b74Schristos
87175fd0b74Schristos /*
87275fd0b74Schristos FUNCTION
87375fd0b74Schristos bfd_get_section_by_name
87475fd0b74Schristos
87575fd0b74Schristos SYNOPSIS
87675fd0b74Schristos asection *bfd_get_section_by_name (bfd *abfd, const char *name);
87775fd0b74Schristos
87875fd0b74Schristos DESCRIPTION
87975fd0b74Schristos Return the most recently created section attached to @var{abfd}
88075fd0b74Schristos named @var{name}. Return NULL if no such section exists.
88175fd0b74Schristos */
88275fd0b74Schristos
88375fd0b74Schristos asection *
bfd_get_section_by_name(bfd * abfd,const char * name)88475fd0b74Schristos bfd_get_section_by_name (bfd *abfd, const char *name)
88575fd0b74Schristos {
88675fd0b74Schristos struct section_hash_entry *sh;
88775fd0b74Schristos
888*e992f068Schristos if (name == NULL)
889*e992f068Schristos return NULL;
890*e992f068Schristos
891*e992f068Schristos sh = section_hash_lookup (&abfd->section_htab, name, false, false);
89275fd0b74Schristos if (sh != NULL)
89375fd0b74Schristos return &sh->section;
89475fd0b74Schristos
89575fd0b74Schristos return NULL;
89675fd0b74Schristos }
89775fd0b74Schristos
89875fd0b74Schristos /*
89975fd0b74Schristos FUNCTION
90075fd0b74Schristos bfd_get_next_section_by_name
90175fd0b74Schristos
90275fd0b74Schristos SYNOPSIS
90375fd0b74Schristos asection *bfd_get_next_section_by_name (bfd *ibfd, asection *sec);
90475fd0b74Schristos
90575fd0b74Schristos DESCRIPTION
90675fd0b74Schristos Given @var{sec} is a section returned by @code{bfd_get_section_by_name},
90775fd0b74Schristos return the next most recently created section attached to the same
90875fd0b74Schristos BFD with the same name, or if no such section exists in the same BFD and
90975fd0b74Schristos IBFD is non-NULL, the next section with the same name in any input
91075fd0b74Schristos BFD following IBFD. Return NULL on finding no section.
91175fd0b74Schristos */
91275fd0b74Schristos
91375fd0b74Schristos asection *
bfd_get_next_section_by_name(bfd * ibfd,asection * sec)91475fd0b74Schristos bfd_get_next_section_by_name (bfd *ibfd, asection *sec)
91575fd0b74Schristos {
91675fd0b74Schristos struct section_hash_entry *sh;
91775fd0b74Schristos const char *name;
91875fd0b74Schristos unsigned long hash;
91975fd0b74Schristos
92075fd0b74Schristos sh = ((struct section_hash_entry *)
92175fd0b74Schristos ((char *) sec - offsetof (struct section_hash_entry, section)));
92275fd0b74Schristos
92375fd0b74Schristos hash = sh->root.hash;
92475fd0b74Schristos name = sec->name;
92575fd0b74Schristos for (sh = (struct section_hash_entry *) sh->root.next;
92675fd0b74Schristos sh != NULL;
92775fd0b74Schristos sh = (struct section_hash_entry *) sh->root.next)
92875fd0b74Schristos if (sh->root.hash == hash
92975fd0b74Schristos && strcmp (sh->root.string, name) == 0)
93075fd0b74Schristos return &sh->section;
93175fd0b74Schristos
93275fd0b74Schristos if (ibfd != NULL)
93375fd0b74Schristos {
93475fd0b74Schristos while ((ibfd = ibfd->link.next) != NULL)
93575fd0b74Schristos {
93675fd0b74Schristos asection *s = bfd_get_section_by_name (ibfd, name);
93775fd0b74Schristos if (s != NULL)
93875fd0b74Schristos return s;
93975fd0b74Schristos }
94075fd0b74Schristos }
94175fd0b74Schristos
94275fd0b74Schristos return NULL;
94375fd0b74Schristos }
94475fd0b74Schristos
94575fd0b74Schristos /*
94675fd0b74Schristos FUNCTION
94775fd0b74Schristos bfd_get_linker_section
94875fd0b74Schristos
94975fd0b74Schristos SYNOPSIS
95075fd0b74Schristos asection *bfd_get_linker_section (bfd *abfd, const char *name);
95175fd0b74Schristos
95275fd0b74Schristos DESCRIPTION
95375fd0b74Schristos Return the linker created section attached to @var{abfd}
95475fd0b74Schristos named @var{name}. Return NULL if no such section exists.
95575fd0b74Schristos */
95675fd0b74Schristos
95775fd0b74Schristos asection *
bfd_get_linker_section(bfd * abfd,const char * name)95875fd0b74Schristos bfd_get_linker_section (bfd *abfd, const char *name)
95975fd0b74Schristos {
96075fd0b74Schristos asection *sec = bfd_get_section_by_name (abfd, name);
96175fd0b74Schristos
96275fd0b74Schristos while (sec != NULL && (sec->flags & SEC_LINKER_CREATED) == 0)
96375fd0b74Schristos sec = bfd_get_next_section_by_name (NULL, sec);
96475fd0b74Schristos return sec;
96575fd0b74Schristos }
96675fd0b74Schristos
96775fd0b74Schristos /*
96875fd0b74Schristos FUNCTION
96975fd0b74Schristos bfd_get_section_by_name_if
97075fd0b74Schristos
97175fd0b74Schristos SYNOPSIS
97275fd0b74Schristos asection *bfd_get_section_by_name_if
97375fd0b74Schristos (bfd *abfd,
97475fd0b74Schristos const char *name,
975*e992f068Schristos bool (*func) (bfd *abfd, asection *sect, void *obj),
97675fd0b74Schristos void *obj);
97775fd0b74Schristos
97875fd0b74Schristos DESCRIPTION
97975fd0b74Schristos Call the provided function @var{func} for each section
98075fd0b74Schristos attached to the BFD @var{abfd} whose name matches @var{name},
98175fd0b74Schristos passing @var{obj} as an argument. The function will be called
98275fd0b74Schristos as if by
98375fd0b74Schristos
98475fd0b74Schristos | func (abfd, the_section, obj);
98575fd0b74Schristos
98675fd0b74Schristos It returns the first section for which @var{func} returns true,
98775fd0b74Schristos otherwise <<NULL>>.
98875fd0b74Schristos
98975fd0b74Schristos */
99075fd0b74Schristos
99175fd0b74Schristos asection *
bfd_get_section_by_name_if(bfd * abfd,const char * name,bool (* operation)(bfd *,asection *,void *),void * user_storage)99275fd0b74Schristos bfd_get_section_by_name_if (bfd *abfd, const char *name,
993*e992f068Schristos bool (*operation) (bfd *, asection *, void *),
99475fd0b74Schristos void *user_storage)
99575fd0b74Schristos {
99675fd0b74Schristos struct section_hash_entry *sh;
99775fd0b74Schristos unsigned long hash;
99875fd0b74Schristos
999*e992f068Schristos if (name == NULL)
1000*e992f068Schristos return NULL;
1001*e992f068Schristos
1002*e992f068Schristos sh = section_hash_lookup (&abfd->section_htab, name, false, false);
100375fd0b74Schristos if (sh == NULL)
100475fd0b74Schristos return NULL;
100575fd0b74Schristos
100675fd0b74Schristos hash = sh->root.hash;
100775fd0b74Schristos for (; sh != NULL; sh = (struct section_hash_entry *) sh->root.next)
100875fd0b74Schristos if (sh->root.hash == hash
100975fd0b74Schristos && strcmp (sh->root.string, name) == 0
101075fd0b74Schristos && (*operation) (abfd, &sh->section, user_storage))
101175fd0b74Schristos return &sh->section;
101275fd0b74Schristos
101375fd0b74Schristos return NULL;
101475fd0b74Schristos }
101575fd0b74Schristos
101675fd0b74Schristos /*
101775fd0b74Schristos FUNCTION
101875fd0b74Schristos bfd_get_unique_section_name
101975fd0b74Schristos
102075fd0b74Schristos SYNOPSIS
102175fd0b74Schristos char *bfd_get_unique_section_name
102275fd0b74Schristos (bfd *abfd, const char *templat, int *count);
102375fd0b74Schristos
102475fd0b74Schristos DESCRIPTION
102575fd0b74Schristos Invent a section name that is unique in @var{abfd} by tacking
102675fd0b74Schristos a dot and a digit suffix onto the original @var{templat}. If
102775fd0b74Schristos @var{count} is non-NULL, then it specifies the first number
102875fd0b74Schristos tried as a suffix to generate a unique name. The value
102975fd0b74Schristos pointed to by @var{count} will be incremented in this case.
103075fd0b74Schristos */
103175fd0b74Schristos
103275fd0b74Schristos char *
bfd_get_unique_section_name(bfd * abfd,const char * templat,int * count)103375fd0b74Schristos bfd_get_unique_section_name (bfd *abfd, const char *templat, int *count)
103475fd0b74Schristos {
103575fd0b74Schristos int num;
103675fd0b74Schristos unsigned int len;
103775fd0b74Schristos char *sname;
103875fd0b74Schristos
103975fd0b74Schristos len = strlen (templat);
104075fd0b74Schristos sname = (char *) bfd_malloc (len + 8);
104175fd0b74Schristos if (sname == NULL)
104275fd0b74Schristos return NULL;
104375fd0b74Schristos memcpy (sname, templat, len);
104475fd0b74Schristos num = 1;
104575fd0b74Schristos if (count != NULL)
104675fd0b74Schristos num = *count;
104775fd0b74Schristos
104875fd0b74Schristos do
104975fd0b74Schristos {
105075fd0b74Schristos /* If we have a million sections, something is badly wrong. */
105175fd0b74Schristos if (num > 999999)
105275fd0b74Schristos abort ();
105375fd0b74Schristos sprintf (sname + len, ".%d", num++);
105475fd0b74Schristos }
1055*e992f068Schristos while (section_hash_lookup (&abfd->section_htab, sname, false, false));
105675fd0b74Schristos
105775fd0b74Schristos if (count != NULL)
105875fd0b74Schristos *count = num;
105975fd0b74Schristos return sname;
106075fd0b74Schristos }
106175fd0b74Schristos
106275fd0b74Schristos /*
106375fd0b74Schristos FUNCTION
106475fd0b74Schristos bfd_make_section_old_way
106575fd0b74Schristos
106675fd0b74Schristos SYNOPSIS
106775fd0b74Schristos asection *bfd_make_section_old_way (bfd *abfd, const char *name);
106875fd0b74Schristos
106975fd0b74Schristos DESCRIPTION
107075fd0b74Schristos Create a new empty section called @var{name}
107175fd0b74Schristos and attach it to the end of the chain of sections for the
107275fd0b74Schristos BFD @var{abfd}. An attempt to create a section with a name which
107375fd0b74Schristos is already in use returns its pointer without changing the
107475fd0b74Schristos section chain.
107575fd0b74Schristos
107675fd0b74Schristos It has the funny name since this is the way it used to be
107775fd0b74Schristos before it was rewritten....
107875fd0b74Schristos
107975fd0b74Schristos Possible errors are:
108075fd0b74Schristos o <<bfd_error_invalid_operation>> -
108175fd0b74Schristos If output has already started for this BFD.
108275fd0b74Schristos o <<bfd_error_no_memory>> -
108375fd0b74Schristos If memory allocation fails.
108475fd0b74Schristos
108575fd0b74Schristos */
108675fd0b74Schristos
108775fd0b74Schristos asection *
bfd_make_section_old_way(bfd * abfd,const char * name)108875fd0b74Schristos bfd_make_section_old_way (bfd *abfd, const char *name)
108975fd0b74Schristos {
109075fd0b74Schristos asection *newsect;
109175fd0b74Schristos
109275fd0b74Schristos if (abfd->output_has_begun)
109375fd0b74Schristos {
109475fd0b74Schristos bfd_set_error (bfd_error_invalid_operation);
109575fd0b74Schristos return NULL;
109675fd0b74Schristos }
109775fd0b74Schristos
109875fd0b74Schristos if (strcmp (name, BFD_ABS_SECTION_NAME) == 0)
109975fd0b74Schristos newsect = bfd_abs_section_ptr;
110075fd0b74Schristos else if (strcmp (name, BFD_COM_SECTION_NAME) == 0)
110175fd0b74Schristos newsect = bfd_com_section_ptr;
110275fd0b74Schristos else if (strcmp (name, BFD_UND_SECTION_NAME) == 0)
110375fd0b74Schristos newsect = bfd_und_section_ptr;
110475fd0b74Schristos else if (strcmp (name, BFD_IND_SECTION_NAME) == 0)
110575fd0b74Schristos newsect = bfd_ind_section_ptr;
110675fd0b74Schristos else
110775fd0b74Schristos {
110875fd0b74Schristos struct section_hash_entry *sh;
110975fd0b74Schristos
1110*e992f068Schristos sh = section_hash_lookup (&abfd->section_htab, name, true, false);
111175fd0b74Schristos if (sh == NULL)
111275fd0b74Schristos return NULL;
111375fd0b74Schristos
111475fd0b74Schristos newsect = &sh->section;
111575fd0b74Schristos if (newsect->name != NULL)
111675fd0b74Schristos {
111775fd0b74Schristos /* Section already exists. */
111875fd0b74Schristos return newsect;
111975fd0b74Schristos }
112075fd0b74Schristos
112175fd0b74Schristos newsect->name = name;
112275fd0b74Schristos return bfd_section_init (abfd, newsect);
112375fd0b74Schristos }
112475fd0b74Schristos
112575fd0b74Schristos /* Call new_section_hook when "creating" the standard abs, com, und
112675fd0b74Schristos and ind sections to tack on format specific section data.
112775fd0b74Schristos Also, create a proper section symbol. */
112875fd0b74Schristos if (! BFD_SEND (abfd, _new_section_hook, (abfd, newsect)))
112975fd0b74Schristos return NULL;
113075fd0b74Schristos return newsect;
113175fd0b74Schristos }
113275fd0b74Schristos
113375fd0b74Schristos /*
113475fd0b74Schristos FUNCTION
113575fd0b74Schristos bfd_make_section_anyway_with_flags
113675fd0b74Schristos
113775fd0b74Schristos SYNOPSIS
113875fd0b74Schristos asection *bfd_make_section_anyway_with_flags
113975fd0b74Schristos (bfd *abfd, const char *name, flagword flags);
114075fd0b74Schristos
114175fd0b74Schristos DESCRIPTION
114275fd0b74Schristos Create a new empty section called @var{name} and attach it to the end of
114375fd0b74Schristos the chain of sections for @var{abfd}. Create a new section even if there
114475fd0b74Schristos is already a section with that name. Also set the attributes of the
114575fd0b74Schristos new section to the value @var{flags}.
114675fd0b74Schristos
114775fd0b74Schristos Return <<NULL>> and set <<bfd_error>> on error; possible errors are:
114875fd0b74Schristos o <<bfd_error_invalid_operation>> - If output has already started for @var{abfd}.
114975fd0b74Schristos o <<bfd_error_no_memory>> - If memory allocation fails.
115075fd0b74Schristos */
115175fd0b74Schristos
115275fd0b74Schristos sec_ptr
bfd_make_section_anyway_with_flags(bfd * abfd,const char * name,flagword flags)115375fd0b74Schristos bfd_make_section_anyway_with_flags (bfd *abfd, const char *name,
115475fd0b74Schristos flagword flags)
115575fd0b74Schristos {
115675fd0b74Schristos struct section_hash_entry *sh;
115775fd0b74Schristos asection *newsect;
115875fd0b74Schristos
115975fd0b74Schristos if (abfd->output_has_begun)
116075fd0b74Schristos {
116175fd0b74Schristos bfd_set_error (bfd_error_invalid_operation);
116275fd0b74Schristos return NULL;
116375fd0b74Schristos }
116475fd0b74Schristos
1165*e992f068Schristos sh = section_hash_lookup (&abfd->section_htab, name, true, false);
116675fd0b74Schristos if (sh == NULL)
116775fd0b74Schristos return NULL;
116875fd0b74Schristos
116975fd0b74Schristos newsect = &sh->section;
117075fd0b74Schristos if (newsect->name != NULL)
117175fd0b74Schristos {
117275fd0b74Schristos /* We are making a section of the same name. Put it in the
117375fd0b74Schristos section hash table. Even though we can't find it directly by a
117475fd0b74Schristos hash lookup, we'll be able to find the section by traversing
117575fd0b74Schristos sh->root.next quicker than looking at all the bfd sections. */
117675fd0b74Schristos struct section_hash_entry *new_sh;
117775fd0b74Schristos new_sh = (struct section_hash_entry *)
117875fd0b74Schristos bfd_section_hash_newfunc (NULL, &abfd->section_htab, name);
117975fd0b74Schristos if (new_sh == NULL)
118075fd0b74Schristos return NULL;
118175fd0b74Schristos
118275fd0b74Schristos new_sh->root = sh->root;
118375fd0b74Schristos sh->root.next = &new_sh->root;
118475fd0b74Schristos newsect = &new_sh->section;
118575fd0b74Schristos }
118675fd0b74Schristos
118775fd0b74Schristos newsect->flags = flags;
118875fd0b74Schristos newsect->name = name;
118975fd0b74Schristos return bfd_section_init (abfd, newsect);
119075fd0b74Schristos }
119175fd0b74Schristos
119275fd0b74Schristos /*
119375fd0b74Schristos FUNCTION
119475fd0b74Schristos bfd_make_section_anyway
119575fd0b74Schristos
119675fd0b74Schristos SYNOPSIS
119775fd0b74Schristos asection *bfd_make_section_anyway (bfd *abfd, const char *name);
119875fd0b74Schristos
119975fd0b74Schristos DESCRIPTION
120075fd0b74Schristos Create a new empty section called @var{name} and attach it to the end of
120175fd0b74Schristos the chain of sections for @var{abfd}. Create a new section even if there
120275fd0b74Schristos is already a section with that name.
120375fd0b74Schristos
120475fd0b74Schristos Return <<NULL>> and set <<bfd_error>> on error; possible errors are:
120575fd0b74Schristos o <<bfd_error_invalid_operation>> - If output has already started for @var{abfd}.
120675fd0b74Schristos o <<bfd_error_no_memory>> - If memory allocation fails.
120775fd0b74Schristos */
120875fd0b74Schristos
120975fd0b74Schristos sec_ptr
bfd_make_section_anyway(bfd * abfd,const char * name)121075fd0b74Schristos bfd_make_section_anyway (bfd *abfd, const char *name)
121175fd0b74Schristos {
121275fd0b74Schristos return bfd_make_section_anyway_with_flags (abfd, name, 0);
121375fd0b74Schristos }
121475fd0b74Schristos
121575fd0b74Schristos /*
121675fd0b74Schristos FUNCTION
121775fd0b74Schristos bfd_make_section_with_flags
121875fd0b74Schristos
121975fd0b74Schristos SYNOPSIS
122075fd0b74Schristos asection *bfd_make_section_with_flags
122175fd0b74Schristos (bfd *, const char *name, flagword flags);
122275fd0b74Schristos
122375fd0b74Schristos DESCRIPTION
122475fd0b74Schristos Like <<bfd_make_section_anyway>>, but return <<NULL>> (without calling
122575fd0b74Schristos bfd_set_error ()) without changing the section chain if there is already a
122675fd0b74Schristos section named @var{name}. Also set the attributes of the new section to
122775fd0b74Schristos the value @var{flags}. If there is an error, return <<NULL>> and set
122875fd0b74Schristos <<bfd_error>>.
122975fd0b74Schristos */
123075fd0b74Schristos
123175fd0b74Schristos asection *
bfd_make_section_with_flags(bfd * abfd,const char * name,flagword flags)123275fd0b74Schristos bfd_make_section_with_flags (bfd *abfd, const char *name,
123375fd0b74Schristos flagword flags)
123475fd0b74Schristos {
123575fd0b74Schristos struct section_hash_entry *sh;
123675fd0b74Schristos asection *newsect;
123775fd0b74Schristos
1238ede78133Schristos if (abfd == NULL || name == NULL || abfd->output_has_begun)
123975fd0b74Schristos {
124075fd0b74Schristos bfd_set_error (bfd_error_invalid_operation);
124175fd0b74Schristos return NULL;
124275fd0b74Schristos }
124375fd0b74Schristos
124475fd0b74Schristos if (strcmp (name, BFD_ABS_SECTION_NAME) == 0
124575fd0b74Schristos || strcmp (name, BFD_COM_SECTION_NAME) == 0
124675fd0b74Schristos || strcmp (name, BFD_UND_SECTION_NAME) == 0
124775fd0b74Schristos || strcmp (name, BFD_IND_SECTION_NAME) == 0)
124875fd0b74Schristos return NULL;
124975fd0b74Schristos
1250*e992f068Schristos sh = section_hash_lookup (&abfd->section_htab, name, true, false);
125175fd0b74Schristos if (sh == NULL)
125275fd0b74Schristos return NULL;
125375fd0b74Schristos
125475fd0b74Schristos newsect = &sh->section;
125575fd0b74Schristos if (newsect->name != NULL)
125675fd0b74Schristos {
125775fd0b74Schristos /* Section already exists. */
125875fd0b74Schristos return NULL;
125975fd0b74Schristos }
126075fd0b74Schristos
126175fd0b74Schristos newsect->name = name;
126275fd0b74Schristos newsect->flags = flags;
126375fd0b74Schristos return bfd_section_init (abfd, newsect);
126475fd0b74Schristos }
126575fd0b74Schristos
126675fd0b74Schristos /*
126775fd0b74Schristos FUNCTION
126875fd0b74Schristos bfd_make_section
126975fd0b74Schristos
127075fd0b74Schristos SYNOPSIS
127175fd0b74Schristos asection *bfd_make_section (bfd *, const char *name);
127275fd0b74Schristos
127375fd0b74Schristos DESCRIPTION
127475fd0b74Schristos Like <<bfd_make_section_anyway>>, but return <<NULL>> (without calling
127575fd0b74Schristos bfd_set_error ()) without changing the section chain if there is already a
127675fd0b74Schristos section named @var{name}. If there is an error, return <<NULL>> and set
127775fd0b74Schristos <<bfd_error>>.
127875fd0b74Schristos */
127975fd0b74Schristos
128075fd0b74Schristos asection *
bfd_make_section(bfd * abfd,const char * name)128175fd0b74Schristos bfd_make_section (bfd *abfd, const char *name)
128275fd0b74Schristos {
128375fd0b74Schristos return bfd_make_section_with_flags (abfd, name, 0);
128475fd0b74Schristos }
128575fd0b74Schristos
128675fd0b74Schristos /*
128775fd0b74Schristos FUNCTION
128875fd0b74Schristos bfd_set_section_flags
128975fd0b74Schristos
129075fd0b74Schristos SYNOPSIS
1291*e992f068Schristos bool bfd_set_section_flags (asection *sec, flagword flags);
129275fd0b74Schristos
129375fd0b74Schristos DESCRIPTION
1294012573ebSchristos Set the attributes of the section @var{sec} to the value @var{flags}.
1295012573ebSchristos Return <<TRUE>> on success, <<FALSE>> on error. Possible error
1296012573ebSchristos returns are:
129775fd0b74Schristos
129875fd0b74Schristos o <<bfd_error_invalid_operation>> -
129975fd0b74Schristos The section cannot have one or more of the attributes
130075fd0b74Schristos requested. For example, a .bss section in <<a.out>> may not
130175fd0b74Schristos have the <<SEC_HAS_CONTENTS>> field set.
130275fd0b74Schristos
130375fd0b74Schristos */
130475fd0b74Schristos
1305*e992f068Schristos bool
bfd_set_section_flags(asection * section,flagword flags)1306012573ebSchristos bfd_set_section_flags (asection *section, flagword flags)
130775fd0b74Schristos {
130875fd0b74Schristos section->flags = flags;
1309*e992f068Schristos return true;
131075fd0b74Schristos }
131175fd0b74Schristos
131275fd0b74Schristos /*
131375fd0b74Schristos FUNCTION
131475fd0b74Schristos bfd_rename_section
131575fd0b74Schristos
131675fd0b74Schristos SYNOPSIS
131775fd0b74Schristos void bfd_rename_section
1318012573ebSchristos (asection *sec, const char *newname);
131975fd0b74Schristos
132075fd0b74Schristos DESCRIPTION
1321012573ebSchristos Rename section @var{sec} to @var{newname}.
132275fd0b74Schristos */
132375fd0b74Schristos
132475fd0b74Schristos void
bfd_rename_section(asection * sec,const char * newname)1325012573ebSchristos bfd_rename_section (asection *sec, const char *newname)
132675fd0b74Schristos {
132775fd0b74Schristos struct section_hash_entry *sh;
132875fd0b74Schristos
132975fd0b74Schristos sh = (struct section_hash_entry *)
133075fd0b74Schristos ((char *) sec - offsetof (struct section_hash_entry, section));
133175fd0b74Schristos sh->section.name = newname;
1332012573ebSchristos bfd_hash_rename (&sec->owner->section_htab, newname, &sh->root);
133375fd0b74Schristos }
133475fd0b74Schristos
133575fd0b74Schristos /*
133675fd0b74Schristos FUNCTION
133775fd0b74Schristos bfd_map_over_sections
133875fd0b74Schristos
133975fd0b74Schristos SYNOPSIS
134075fd0b74Schristos void bfd_map_over_sections
134175fd0b74Schristos (bfd *abfd,
134275fd0b74Schristos void (*func) (bfd *abfd, asection *sect, void *obj),
134375fd0b74Schristos void *obj);
134475fd0b74Schristos
134575fd0b74Schristos DESCRIPTION
134675fd0b74Schristos Call the provided function @var{func} for each section
134775fd0b74Schristos attached to the BFD @var{abfd}, passing @var{obj} as an
134875fd0b74Schristos argument. The function will be called as if by
134975fd0b74Schristos
135075fd0b74Schristos | func (abfd, the_section, obj);
135175fd0b74Schristos
135275fd0b74Schristos This is the preferred method for iterating over sections; an
135375fd0b74Schristos alternative would be to use a loop:
135475fd0b74Schristos
135575fd0b74Schristos | asection *p;
135675fd0b74Schristos | for (p = abfd->sections; p != NULL; p = p->next)
135775fd0b74Schristos | func (abfd, p, ...)
135875fd0b74Schristos
135975fd0b74Schristos */
136075fd0b74Schristos
136175fd0b74Schristos void
bfd_map_over_sections(bfd * abfd,void (* operation)(bfd *,asection *,void *),void * user_storage)136275fd0b74Schristos bfd_map_over_sections (bfd *abfd,
136375fd0b74Schristos void (*operation) (bfd *, asection *, void *),
136475fd0b74Schristos void *user_storage)
136575fd0b74Schristos {
136675fd0b74Schristos asection *sect;
136775fd0b74Schristos unsigned int i = 0;
136875fd0b74Schristos
136975fd0b74Schristos for (sect = abfd->sections; sect != NULL; i++, sect = sect->next)
137075fd0b74Schristos (*operation) (abfd, sect, user_storage);
137175fd0b74Schristos
137275fd0b74Schristos if (i != abfd->section_count) /* Debugging */
137375fd0b74Schristos abort ();
137475fd0b74Schristos }
137575fd0b74Schristos
137675fd0b74Schristos /*
137775fd0b74Schristos FUNCTION
137875fd0b74Schristos bfd_sections_find_if
137975fd0b74Schristos
138075fd0b74Schristos SYNOPSIS
138175fd0b74Schristos asection *bfd_sections_find_if
138275fd0b74Schristos (bfd *abfd,
1383*e992f068Schristos bool (*operation) (bfd *abfd, asection *sect, void *obj),
138475fd0b74Schristos void *obj);
138575fd0b74Schristos
138675fd0b74Schristos DESCRIPTION
138775fd0b74Schristos Call the provided function @var{operation} for each section
138875fd0b74Schristos attached to the BFD @var{abfd}, passing @var{obj} as an
138975fd0b74Schristos argument. The function will be called as if by
139075fd0b74Schristos
139175fd0b74Schristos | operation (abfd, the_section, obj);
139275fd0b74Schristos
139375fd0b74Schristos It returns the first section for which @var{operation} returns true.
139475fd0b74Schristos
139575fd0b74Schristos */
139675fd0b74Schristos
139775fd0b74Schristos asection *
bfd_sections_find_if(bfd * abfd,bool (* operation)(bfd *,asection *,void *),void * user_storage)139875fd0b74Schristos bfd_sections_find_if (bfd *abfd,
1399*e992f068Schristos bool (*operation) (bfd *, asection *, void *),
140075fd0b74Schristos void *user_storage)
140175fd0b74Schristos {
140275fd0b74Schristos asection *sect;
140375fd0b74Schristos
140475fd0b74Schristos for (sect = abfd->sections; sect != NULL; sect = sect->next)
140575fd0b74Schristos if ((*operation) (abfd, sect, user_storage))
140675fd0b74Schristos break;
140775fd0b74Schristos
140875fd0b74Schristos return sect;
140975fd0b74Schristos }
141075fd0b74Schristos
141175fd0b74Schristos /*
141275fd0b74Schristos FUNCTION
141375fd0b74Schristos bfd_set_section_size
141475fd0b74Schristos
141575fd0b74Schristos SYNOPSIS
1416*e992f068Schristos bool bfd_set_section_size (asection *sec, bfd_size_type val);
141775fd0b74Schristos
141875fd0b74Schristos DESCRIPTION
141975fd0b74Schristos Set @var{sec} to the size @var{val}. If the operation is
142075fd0b74Schristos ok, then <<TRUE>> is returned, else <<FALSE>>.
142175fd0b74Schristos
142275fd0b74Schristos Possible error returns:
142375fd0b74Schristos o <<bfd_error_invalid_operation>> -
142475fd0b74Schristos Writing has started to the BFD, so setting the size is invalid.
142575fd0b74Schristos
142675fd0b74Schristos */
142775fd0b74Schristos
1428*e992f068Schristos bool
bfd_set_section_size(asection * sec,bfd_size_type val)1429012573ebSchristos bfd_set_section_size (asection *sec, bfd_size_type val)
143075fd0b74Schristos {
143175fd0b74Schristos /* Once you've started writing to any section you cannot create or change
143275fd0b74Schristos the size of any others. */
143375fd0b74Schristos
1434012573ebSchristos if (sec->owner == NULL || sec->owner->output_has_begun)
143575fd0b74Schristos {
143675fd0b74Schristos bfd_set_error (bfd_error_invalid_operation);
1437*e992f068Schristos return false;
143875fd0b74Schristos }
143975fd0b74Schristos
1440012573ebSchristos sec->size = val;
1441*e992f068Schristos return true;
144275fd0b74Schristos }
144375fd0b74Schristos
144475fd0b74Schristos /*
144575fd0b74Schristos FUNCTION
144675fd0b74Schristos bfd_set_section_contents
144775fd0b74Schristos
144875fd0b74Schristos SYNOPSIS
1449*e992f068Schristos bool bfd_set_section_contents
145075fd0b74Schristos (bfd *abfd, asection *section, const void *data,
145175fd0b74Schristos file_ptr offset, bfd_size_type count);
145275fd0b74Schristos
145375fd0b74Schristos DESCRIPTION
145475fd0b74Schristos Sets the contents of the section @var{section} in BFD
1455012573ebSchristos @var{abfd} to the data starting in memory at @var{location}.
1456012573ebSchristos The data is written to the output section starting at offset
145775fd0b74Schristos @var{offset} for @var{count} octets.
145875fd0b74Schristos
1459012573ebSchristos Normally <<TRUE>> is returned, but <<FALSE>> is returned if
1460012573ebSchristos there was an error. Possible error returns are:
146175fd0b74Schristos o <<bfd_error_no_contents>> -
146275fd0b74Schristos The output section does not have the <<SEC_HAS_CONTENTS>>
146375fd0b74Schristos attribute, so nothing can be written to it.
1464012573ebSchristos o <<bfd_error_bad_value>> -
1465012573ebSchristos The section is unable to contain all of the data.
1466012573ebSchristos o <<bfd_error_invalid_operation>> -
1467012573ebSchristos The BFD is not writeable.
1468012573ebSchristos o and some more too.
146975fd0b74Schristos
147075fd0b74Schristos This routine is front end to the back end function
147175fd0b74Schristos <<_bfd_set_section_contents>>.
147275fd0b74Schristos
147375fd0b74Schristos */
147475fd0b74Schristos
1475*e992f068Schristos bool
bfd_set_section_contents(bfd * abfd,sec_ptr section,const void * location,file_ptr offset,bfd_size_type count)147675fd0b74Schristos bfd_set_section_contents (bfd *abfd,
147775fd0b74Schristos sec_ptr section,
147875fd0b74Schristos const void *location,
147975fd0b74Schristos file_ptr offset,
148075fd0b74Schristos bfd_size_type count)
148175fd0b74Schristos {
148275fd0b74Schristos bfd_size_type sz;
148375fd0b74Schristos
1484012573ebSchristos if (!(bfd_section_flags (section) & SEC_HAS_CONTENTS))
148575fd0b74Schristos {
148675fd0b74Schristos bfd_set_error (bfd_error_no_contents);
1487*e992f068Schristos return false;
148875fd0b74Schristos }
148975fd0b74Schristos
149075fd0b74Schristos sz = section->size;
149175fd0b74Schristos if ((bfd_size_type) offset > sz
1492*e992f068Schristos || count > sz - offset
149375fd0b74Schristos || count != (size_t) count)
149475fd0b74Schristos {
149575fd0b74Schristos bfd_set_error (bfd_error_bad_value);
1496*e992f068Schristos return false;
149775fd0b74Schristos }
149875fd0b74Schristos
149975fd0b74Schristos if (!bfd_write_p (abfd))
150075fd0b74Schristos {
150175fd0b74Schristos bfd_set_error (bfd_error_invalid_operation);
1502*e992f068Schristos return false;
150375fd0b74Schristos }
150475fd0b74Schristos
150575fd0b74Schristos /* Record a copy of the data in memory if desired. */
150675fd0b74Schristos if (section->contents
150775fd0b74Schristos && location != section->contents + offset)
150875fd0b74Schristos memcpy (section->contents + offset, location, (size_t) count);
150975fd0b74Schristos
151075fd0b74Schristos if (BFD_SEND (abfd, _bfd_set_section_contents,
151175fd0b74Schristos (abfd, section, location, offset, count)))
151275fd0b74Schristos {
1513*e992f068Schristos abfd->output_has_begun = true;
1514*e992f068Schristos return true;
151575fd0b74Schristos }
151675fd0b74Schristos
1517*e992f068Schristos return false;
151875fd0b74Schristos }
151975fd0b74Schristos
152075fd0b74Schristos /*
152175fd0b74Schristos FUNCTION
152275fd0b74Schristos bfd_get_section_contents
152375fd0b74Schristos
152475fd0b74Schristos SYNOPSIS
1525*e992f068Schristos bool bfd_get_section_contents
152675fd0b74Schristos (bfd *abfd, asection *section, void *location, file_ptr offset,
152775fd0b74Schristos bfd_size_type count);
152875fd0b74Schristos
152975fd0b74Schristos DESCRIPTION
153075fd0b74Schristos Read data from @var{section} in BFD @var{abfd}
153175fd0b74Schristos into memory starting at @var{location}. The data is read at an
153275fd0b74Schristos offset of @var{offset} from the start of the input section,
153375fd0b74Schristos and is read for @var{count} bytes.
153475fd0b74Schristos
153575fd0b74Schristos If the contents of a constructor with the <<SEC_CONSTRUCTOR>>
153675fd0b74Schristos flag set are requested or if the section does not have the
153775fd0b74Schristos <<SEC_HAS_CONTENTS>> flag set, then the @var{location} is filled
153875fd0b74Schristos with zeroes. If no errors occur, <<TRUE>> is returned, else
153975fd0b74Schristos <<FALSE>>.
154075fd0b74Schristos
154175fd0b74Schristos */
1542*e992f068Schristos bool
bfd_get_section_contents(bfd * abfd,sec_ptr section,void * location,file_ptr offset,bfd_size_type count)154375fd0b74Schristos bfd_get_section_contents (bfd *abfd,
154475fd0b74Schristos sec_ptr section,
154575fd0b74Schristos void *location,
154675fd0b74Schristos file_ptr offset,
154775fd0b74Schristos bfd_size_type count)
154875fd0b74Schristos {
154975fd0b74Schristos bfd_size_type sz;
155075fd0b74Schristos
155175fd0b74Schristos if (section->flags & SEC_CONSTRUCTOR)
155275fd0b74Schristos {
155375fd0b74Schristos memset (location, 0, (size_t) count);
1554*e992f068Schristos return true;
155575fd0b74Schristos }
155675fd0b74Schristos
155775fd0b74Schristos if (abfd->direction != write_direction && section->rawsize != 0)
155875fd0b74Schristos sz = section->rawsize;
155975fd0b74Schristos else
156075fd0b74Schristos sz = section->size;
156175fd0b74Schristos if ((bfd_size_type) offset > sz
1562*e992f068Schristos || count > sz - offset
156375fd0b74Schristos || count != (size_t) count)
156475fd0b74Schristos {
156575fd0b74Schristos bfd_set_error (bfd_error_bad_value);
1566*e992f068Schristos return false;
156775fd0b74Schristos }
156875fd0b74Schristos
156975fd0b74Schristos if (count == 0)
157075fd0b74Schristos /* Don't bother. */
1571*e992f068Schristos return true;
157275fd0b74Schristos
157375fd0b74Schristos if ((section->flags & SEC_HAS_CONTENTS) == 0)
157475fd0b74Schristos {
157575fd0b74Schristos memset (location, 0, (size_t) count);
1576*e992f068Schristos return true;
157775fd0b74Schristos }
157875fd0b74Schristos
157975fd0b74Schristos if ((section->flags & SEC_IN_MEMORY) != 0)
158075fd0b74Schristos {
158175fd0b74Schristos if (section->contents == NULL)
158275fd0b74Schristos {
158375fd0b74Schristos /* This can happen because of errors earlier on in the linking process.
158475fd0b74Schristos We do not want to seg-fault here, so clear the flag and return an
158575fd0b74Schristos error code. */
158675fd0b74Schristos section->flags &= ~ SEC_IN_MEMORY;
158775fd0b74Schristos bfd_set_error (bfd_error_invalid_operation);
1588*e992f068Schristos return false;
158975fd0b74Schristos }
159075fd0b74Schristos
159175fd0b74Schristos memmove (location, section->contents + offset, (size_t) count);
1592*e992f068Schristos return true;
159375fd0b74Schristos }
159475fd0b74Schristos
159575fd0b74Schristos return BFD_SEND (abfd, _bfd_get_section_contents,
159675fd0b74Schristos (abfd, section, location, offset, count));
159775fd0b74Schristos }
159875fd0b74Schristos
159975fd0b74Schristos /*
160075fd0b74Schristos FUNCTION
160175fd0b74Schristos bfd_malloc_and_get_section
160275fd0b74Schristos
160375fd0b74Schristos SYNOPSIS
1604*e992f068Schristos bool bfd_malloc_and_get_section
160575fd0b74Schristos (bfd *abfd, asection *section, bfd_byte **buf);
160675fd0b74Schristos
160775fd0b74Schristos DESCRIPTION
160875fd0b74Schristos Read all data from @var{section} in BFD @var{abfd}
160975fd0b74Schristos into a buffer, *@var{buf}, malloc'd by this function.
161075fd0b74Schristos */
161175fd0b74Schristos
1612*e992f068Schristos bool
bfd_malloc_and_get_section(bfd * abfd,sec_ptr sec,bfd_byte ** buf)161375fd0b74Schristos bfd_malloc_and_get_section (bfd *abfd, sec_ptr sec, bfd_byte **buf)
161475fd0b74Schristos {
161575fd0b74Schristos *buf = NULL;
161675fd0b74Schristos return bfd_get_full_section_contents (abfd, sec, buf);
161775fd0b74Schristos }
161875fd0b74Schristos /*
161975fd0b74Schristos FUNCTION
162075fd0b74Schristos bfd_copy_private_section_data
162175fd0b74Schristos
162275fd0b74Schristos SYNOPSIS
1623*e992f068Schristos bool bfd_copy_private_section_data
162475fd0b74Schristos (bfd *ibfd, asection *isec, bfd *obfd, asection *osec);
162575fd0b74Schristos
162675fd0b74Schristos DESCRIPTION
162775fd0b74Schristos Copy private section information from @var{isec} in the BFD
162875fd0b74Schristos @var{ibfd} to the section @var{osec} in the BFD @var{obfd}.
162975fd0b74Schristos Return <<TRUE>> on success, <<FALSE>> on error. Possible error
163075fd0b74Schristos returns are:
163175fd0b74Schristos
163275fd0b74Schristos o <<bfd_error_no_memory>> -
163375fd0b74Schristos Not enough memory exists to create private data for @var{osec}.
163475fd0b74Schristos
163575fd0b74Schristos .#define bfd_copy_private_section_data(ibfd, isection, obfd, osection) \
163675fd0b74Schristos . BFD_SEND (obfd, _bfd_copy_private_section_data, \
163775fd0b74Schristos . (ibfd, isection, obfd, osection))
163875fd0b74Schristos */
163975fd0b74Schristos
164075fd0b74Schristos /*
164175fd0b74Schristos FUNCTION
164275fd0b74Schristos bfd_generic_is_group_section
164375fd0b74Schristos
164475fd0b74Schristos SYNOPSIS
1645*e992f068Schristos bool bfd_generic_is_group_section (bfd *, const asection *sec);
164675fd0b74Schristos
164775fd0b74Schristos DESCRIPTION
164875fd0b74Schristos Returns TRUE if @var{sec} is a member of a group.
164975fd0b74Schristos */
165075fd0b74Schristos
1651*e992f068Schristos bool
bfd_generic_is_group_section(bfd * abfd ATTRIBUTE_UNUSED,const asection * sec ATTRIBUTE_UNUSED)165275fd0b74Schristos bfd_generic_is_group_section (bfd *abfd ATTRIBUTE_UNUSED,
165375fd0b74Schristos const asection *sec ATTRIBUTE_UNUSED)
165475fd0b74Schristos {
1655*e992f068Schristos return false;
165675fd0b74Schristos }
165775fd0b74Schristos
165875fd0b74Schristos /*
165975fd0b74Schristos FUNCTION
1660012573ebSchristos bfd_generic_group_name
1661012573ebSchristos
1662012573ebSchristos SYNOPSIS
1663012573ebSchristos const char *bfd_generic_group_name (bfd *, const asection *sec);
1664012573ebSchristos
1665012573ebSchristos DESCRIPTION
1666012573ebSchristos Returns group name if @var{sec} is a member of a group.
1667012573ebSchristos */
1668012573ebSchristos
1669012573ebSchristos const char *
bfd_generic_group_name(bfd * abfd ATTRIBUTE_UNUSED,const asection * sec ATTRIBUTE_UNUSED)1670012573ebSchristos bfd_generic_group_name (bfd *abfd ATTRIBUTE_UNUSED,
1671012573ebSchristos const asection *sec ATTRIBUTE_UNUSED)
1672012573ebSchristos {
1673012573ebSchristos return NULL;
1674012573ebSchristos }
1675012573ebSchristos
1676012573ebSchristos /*
1677012573ebSchristos FUNCTION
167875fd0b74Schristos bfd_generic_discard_group
167975fd0b74Schristos
168075fd0b74Schristos SYNOPSIS
1681*e992f068Schristos bool bfd_generic_discard_group (bfd *abfd, asection *group);
168275fd0b74Schristos
168375fd0b74Schristos DESCRIPTION
168475fd0b74Schristos Remove all members of @var{group} from the output.
168575fd0b74Schristos */
168675fd0b74Schristos
1687*e992f068Schristos bool
bfd_generic_discard_group(bfd * abfd ATTRIBUTE_UNUSED,asection * group ATTRIBUTE_UNUSED)168875fd0b74Schristos bfd_generic_discard_group (bfd *abfd ATTRIBUTE_UNUSED,
168975fd0b74Schristos asection *group ATTRIBUTE_UNUSED)
169075fd0b74Schristos {
1691*e992f068Schristos return true;
169275fd0b74Schristos }
1693ede78133Schristos
1694*e992f068Schristos bool
_bfd_nowrite_set_section_contents(bfd * abfd,sec_ptr section ATTRIBUTE_UNUSED,const void * location ATTRIBUTE_UNUSED,file_ptr offset ATTRIBUTE_UNUSED,bfd_size_type count ATTRIBUTE_UNUSED)1695ede78133Schristos _bfd_nowrite_set_section_contents (bfd *abfd,
1696ede78133Schristos sec_ptr section ATTRIBUTE_UNUSED,
1697ede78133Schristos const void *location ATTRIBUTE_UNUSED,
1698ede78133Schristos file_ptr offset ATTRIBUTE_UNUSED,
1699ede78133Schristos bfd_size_type count ATTRIBUTE_UNUSED)
1700ede78133Schristos {
1701ede78133Schristos return _bfd_bool_bfd_false_error (abfd);
1702ede78133Schristos }
1703