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