Back to index

cell-binutils  2.17cvs20070401
section.c
Go to the documentation of this file.
00001 /* Object file "section" support for the BFD library.
00002    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
00003    2000, 2001, 2002, 2003, 2004, 2005, 2006
00004    Free Software Foundation, Inc.
00005    Written by Cygnus Support.
00006 
00007 This file is part of BFD, the Binary File Descriptor library.
00008 
00009 This program is free software; you can redistribute it and/or modify
00010 it under the terms of the GNU General Public License as published by
00011 the Free Software Foundation; either version 2 of the License, or
00012 (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00022 
00023 /*
00024 SECTION
00025        Sections
00026 
00027        The raw data contained within a BFD is maintained through the
00028        section abstraction.  A single BFD may have any number of
00029        sections.  It keeps hold of them by pointing to the first;
00030        each one points to the next in the list.
00031 
00032        Sections are supported in BFD in <<section.c>>.
00033 
00034 @menu
00035 @* Section Input::
00036 @* Section Output::
00037 @* typedef asection::
00038 @* section prototypes::
00039 @end menu
00040 
00041 INODE
00042 Section Input, Section Output, Sections, Sections
00043 SUBSECTION
00044        Section input
00045 
00046        When a BFD is opened for reading, the section structures are
00047        created and attached to the BFD.
00048 
00049        Each section has a name which describes the section in the
00050        outside world---for example, <<a.out>> would contain at least
00051        three sections, called <<.text>>, <<.data>> and <<.bss>>.
00052 
00053        Names need not be unique; for example a COFF file may have several
00054        sections named <<.data>>.
00055 
00056        Sometimes a BFD will contain more than the ``natural'' number of
00057        sections. A back end may attach other sections containing
00058        constructor data, or an application may add a section (using
00059        <<bfd_make_section>>) to the sections attached to an already open
00060        BFD. For example, the linker creates an extra section
00061        <<COMMON>> for each input file's BFD to hold information about
00062        common storage.
00063 
00064        The raw data is not necessarily read in when
00065        the section descriptor is created. Some targets may leave the
00066        data in place until a <<bfd_get_section_contents>> call is
00067        made. Other back ends may read in all the data at once.  For
00068        example, an S-record file has to be read once to determine the
00069        size of the data. An IEEE-695 file doesn't contain raw data in
00070        sections, but data and relocation expressions intermixed, so
00071        the data area has to be parsed to get out the data and
00072        relocations.
00073 
00074 INODE
00075 Section Output, typedef asection, Section Input, Sections
00076 
00077 SUBSECTION
00078        Section output
00079 
00080        To write a new object style BFD, the various sections to be
00081        written have to be created. They are attached to the BFD in
00082        the same way as input sections; data is written to the
00083        sections using <<bfd_set_section_contents>>.
00084 
00085        Any program that creates or combines sections (e.g., the assembler
00086        and linker) must use the <<asection>> fields <<output_section>> and
00087        <<output_offset>> to indicate the file sections to which each
00088        section must be written.  (If the section is being created from
00089        scratch, <<output_section>> should probably point to the section
00090        itself and <<output_offset>> should probably be zero.)
00091 
00092        The data to be written comes from input sections attached
00093        (via <<output_section>> pointers) to
00094        the output sections.  The output section structure can be
00095        considered a filter for the input section: the output section
00096        determines the vma of the output data and the name, but the
00097        input section determines the offset into the output section of
00098        the data to be written.
00099 
00100        E.g., to create a section "O", starting at 0x100, 0x123 long,
00101        containing two subsections, "A" at offset 0x0 (i.e., at vma
00102        0x100) and "B" at offset 0x20 (i.e., at vma 0x120) the <<asection>>
00103        structures would look like:
00104 
00105 |   section name          "A"
00106 |     output_offset   0x00
00107 |     size            0x20
00108 |     output_section ----------->  section name    "O"
00109 |                             |    vma             0x100
00110 |   section name          "B" |    size            0x123
00111 |     output_offset   0x20    |
00112 |     size            0x103   |
00113 |     output_section  --------|
00114 
00115 SUBSECTION
00116        Link orders
00117 
00118        The data within a section is stored in a @dfn{link_order}.
00119        These are much like the fixups in <<gas>>.  The link_order
00120        abstraction allows a section to grow and shrink within itself.
00121 
00122        A link_order knows how big it is, and which is the next
00123        link_order and where the raw data for it is; it also points to
00124        a list of relocations which apply to it.
00125 
00126        The link_order is used by the linker to perform relaxing on
00127        final code.  The compiler creates code which is as big as
00128        necessary to make it work without relaxing, and the user can
00129        select whether to relax.  Sometimes relaxing takes a lot of
00130        time.  The linker runs around the relocations to see if any
00131        are attached to data which can be shrunk, if so it does it on
00132        a link_order by link_order basis.
00133 
00134 */
00135 
00136 #include "bfd.h"
00137 #include "sysdep.h"
00138 #include "libbfd.h"
00139 #include "bfdlink.h"
00140 
00141 /*
00142 DOCDD
00143 INODE
00144 typedef asection, section prototypes, Section Output, Sections
00145 SUBSECTION
00146        typedef asection
00147 
00148        Here is the section structure:
00149 
00150 CODE_FRAGMENT
00151 .
00152 .typedef struct bfd_section
00153 .{
00154 .  {* The name of the section; the name isn't a copy, the pointer is
00155 .     the same as that passed to bfd_make_section.  *}
00156 .  const char *name;
00157 .
00158 .  {* A unique sequence number.  *}
00159 .  int id;
00160 .
00161 .  {* Which section in the bfd; 0..n-1 as sections are created in a bfd.  *}
00162 .  int index;
00163 .
00164 .  {* The next section in the list belonging to the BFD, or NULL.  *}
00165 .  struct bfd_section *next;
00166 .
00167 .  {* The previous section in the list belonging to the BFD, or NULL.  *}
00168 .  struct bfd_section *prev;
00169 .
00170 .  {* The field flags contains attributes of the section. Some
00171 .     flags are read in from the object file, and some are
00172 .     synthesized from other information.  *}
00173 .  flagword flags;
00174 .
00175 .#define SEC_NO_FLAGS   0x000
00176 .
00177 .  {* Tells the OS to allocate space for this section when loading.
00178 .     This is clear for a section containing debug information only.  *}
00179 .#define SEC_ALLOC      0x001
00180 .
00181 .  {* Tells the OS to load the section from the file when loading.
00182 .     This is clear for a .bss section.  *}
00183 .#define SEC_LOAD       0x002
00184 .
00185 .  {* The section contains data still to be relocated, so there is
00186 .     some relocation information too.  *}
00187 .#define SEC_RELOC      0x004
00188 .
00189 .  {* A signal to the OS that the section contains read only data.  *}
00190 .#define SEC_READONLY   0x008
00191 .
00192 .  {* The section contains code only.  *}
00193 .#define SEC_CODE       0x010
00194 .
00195 .  {* The section contains data only.  *}
00196 .#define SEC_DATA       0x020
00197 .
00198 .  {* The section will reside in ROM.  *}
00199 .#define SEC_ROM        0x040
00200 .
00201 .  {* The section contains constructor information. This section
00202 .     type is used by the linker to create lists of constructors and
00203 .     destructors used by <<g++>>. When a back end sees a symbol
00204 .     which should be used in a constructor list, it creates a new
00205 .     section for the type of name (e.g., <<__CTOR_LIST__>>), attaches
00206 .     the symbol to it, and builds a relocation. To build the lists
00207 .     of constructors, all the linker has to do is catenate all the
00208 .     sections called <<__CTOR_LIST__>> and relocate the data
00209 .     contained within - exactly the operations it would peform on
00210 .     standard data.  *}
00211 .#define SEC_CONSTRUCTOR 0x080
00212 .
00213 .  {* The section has contents - a data section could be
00214 .     <<SEC_ALLOC>> | <<SEC_HAS_CONTENTS>>; a debug section could be
00215 .     <<SEC_HAS_CONTENTS>>  *}
00216 .#define SEC_HAS_CONTENTS 0x100
00217 .
00218 .  {* An instruction to the linker to not output the section
00219 .     even if it has information which would normally be written.  *}
00220 .#define SEC_NEVER_LOAD 0x200
00221 .
00222 .  {* The section contains thread local data.  *}
00223 .#define SEC_THREAD_LOCAL 0x400
00224 .
00225 .  {* The section has GOT references.  This flag is only for the
00226 .     linker, and is currently only used by the elf32-hppa back end.
00227 .     It will be set if global offset table references were detected
00228 .     in this section, which indicate to the linker that the section
00229 .     contains PIC code, and must be handled specially when doing a
00230 .     static link.  *}
00231 .#define SEC_HAS_GOT_REF 0x800
00232 .
00233 .  {* The section contains common symbols (symbols may be defined
00234 .     multiple times, the value of a symbol is the amount of
00235 .     space it requires, and the largest symbol value is the one
00236 .     used).  Most targets have exactly one of these (which we
00237 .     translate to bfd_com_section_ptr), but ECOFF has two.  *}
00238 .#define SEC_IS_COMMON 0x1000
00239 .
00240 .  {* The section contains only debugging information.  For
00241 .     example, this is set for ELF .debug and .stab sections.
00242 .     strip tests this flag to see if a section can be
00243 .     discarded.  *}
00244 .#define SEC_DEBUGGING 0x2000
00245 .
00246 .  {* The contents of this section are held in memory pointed to
00247 .     by the contents field.  This is checked by bfd_get_section_contents,
00248 .     and the data is retrieved from memory if appropriate.  *}
00249 .#define SEC_IN_MEMORY 0x4000
00250 .
00251 .  {* The contents of this section are to be excluded by the
00252 .     linker for executable and shared objects unless those
00253 .     objects are to be further relocated.  *}
00254 .#define SEC_EXCLUDE 0x8000
00255 .
00256 .  {* The contents of this section are to be sorted based on the sum of
00257 .     the symbol and addend values specified by the associated relocation
00258 .     entries.  Entries without associated relocation entries will be
00259 .     appended to the end of the section in an unspecified order.  *}
00260 .#define SEC_SORT_ENTRIES 0x10000
00261 .
00262 .  {* When linking, duplicate sections of the same name should be
00263 .     discarded, rather than being combined into a single section as
00264 .     is usually done.  This is similar to how common symbols are
00265 .     handled.  See SEC_LINK_DUPLICATES below.  *}
00266 .#define SEC_LINK_ONCE 0x20000
00267 .
00268 .  {* If SEC_LINK_ONCE is set, this bitfield describes how the linker
00269 .     should handle duplicate sections.  *}
00270 .#define SEC_LINK_DUPLICATES 0x40000
00271 .
00272 .  {* This value for SEC_LINK_DUPLICATES means that duplicate
00273 .     sections with the same name should simply be discarded.  *}
00274 .#define SEC_LINK_DUPLICATES_DISCARD 0x0
00275 .
00276 .  {* This value for SEC_LINK_DUPLICATES means that the linker
00277 .     should warn if there are any duplicate sections, although
00278 .     it should still only link one copy.  *}
00279 .#define SEC_LINK_DUPLICATES_ONE_ONLY 0x80000
00280 .
00281 .  {* This value for SEC_LINK_DUPLICATES means that the linker
00282 .     should warn if any duplicate sections are a different size.  *}
00283 .#define SEC_LINK_DUPLICATES_SAME_SIZE 0x100000
00284 .
00285 .  {* This value for SEC_LINK_DUPLICATES means that the linker
00286 .     should warn if any duplicate sections contain different
00287 .     contents.  *}
00288 .#define SEC_LINK_DUPLICATES_SAME_CONTENTS \
00289 .  (SEC_LINK_DUPLICATES_ONE_ONLY | SEC_LINK_DUPLICATES_SAME_SIZE)
00290 .
00291 .  {* This section was created by the linker as part of dynamic
00292 .     relocation or other arcane processing.  It is skipped when
00293 .     going through the first-pass output, trusting that someone
00294 .     else up the line will take care of it later.  *}
00295 .#define SEC_LINKER_CREATED 0x200000
00296 .
00297 .  {* This section should not be subject to garbage collection.
00298 .     Also set to inform the linker that this section should not be
00299 .     listed in the link map as discarded.  *}
00300 .#define SEC_KEEP 0x400000
00301 .
00302 .  {* This section contains "short" data, and should be placed
00303 .     "near" the GP.  *}
00304 .#define SEC_SMALL_DATA 0x800000
00305 .
00306 .  {* Attempt to merge identical entities in the section.
00307 .     Entity size is given in the entsize field.  *}
00308 .#define SEC_MERGE 0x1000000
00309 .
00310 .  {* If given with SEC_MERGE, entities to merge are zero terminated
00311 .     strings where entsize specifies character size instead of fixed
00312 .     size entries.  *}
00313 .#define SEC_STRINGS 0x2000000
00314 .
00315 .  {* This section contains data about section groups.  *}
00316 .#define SEC_GROUP 0x4000000
00317 .
00318 .  {* The section is a COFF shared library section.  This flag is
00319 .     only for the linker.  If this type of section appears in
00320 .     the input file, the linker must copy it to the output file
00321 .     without changing the vma or size.  FIXME: Although this
00322 .     was originally intended to be general, it really is COFF
00323 .     specific (and the flag was renamed to indicate this).  It
00324 .     might be cleaner to have some more general mechanism to
00325 .     allow the back end to control what the linker does with
00326 .     sections.  *}
00327 .#define SEC_COFF_SHARED_LIBRARY 0x10000000
00328 .
00329 .  {* This section contains data which may be shared with other
00330 .     executables or shared objects. This is for COFF only.  *}
00331 .#define SEC_COFF_SHARED 0x20000000
00332 .
00333 .  {* When a section with this flag is being linked, then if the size of
00334 .     the input section is less than a page, it should not cross a page
00335 .     boundary.  If the size of the input section is one page or more,
00336 .     it should be aligned on a page boundary.  This is for TI
00337 .     TMS320C54X only.  *}
00338 .#define SEC_TIC54X_BLOCK 0x40000000
00339 .
00340 .  {* Conditionally link this section; do not link if there are no
00341 .     references found to any symbol in the section.  This is for TI
00342 .     TMS320C54X only.  *}
00343 .#define SEC_TIC54X_CLINK 0x80000000
00344 .
00345 .  {*  End of section flags.  *}
00346 .
00347 .  {* Some internal packed boolean fields.  *}
00348 .
00349 .  {* See the vma field.  *}
00350 .  unsigned int user_set_vma : 1;
00351 .
00352 .  {* A mark flag used by some of the linker backends.  *}
00353 .  unsigned int linker_mark : 1;
00354 .
00355 .  {* Another mark flag used by some of the linker backends.  Set for
00356 .     output sections that have an input section.  *}
00357 .  unsigned int linker_has_input : 1;
00358 .
00359 .  {* Mark flags used by some linker backends for garbage collection.  *}
00360 .  unsigned int gc_mark : 1;
00361 .  unsigned int gc_mark_from_eh : 1;
00362 .
00363 .  {* The following flags are used by the ELF linker. *}
00364 .
00365 .  {* Mark sections which have been allocated to segments.  *}
00366 .  unsigned int segment_mark : 1;
00367 .
00368 .  {* Type of sec_info information.  *}
00369 .  unsigned int sec_info_type:3;
00370 .#define ELF_INFO_TYPE_NONE      0
00371 .#define ELF_INFO_TYPE_STABS     1
00372 .#define ELF_INFO_TYPE_MERGE     2
00373 .#define ELF_INFO_TYPE_EH_FRAME  3
00374 .#define ELF_INFO_TYPE_JUST_SYMS 4
00375 .
00376 .  {* Nonzero if this section uses RELA relocations, rather than REL.  *}
00377 .  unsigned int use_rela_p:1;
00378 .
00379 .  {* Bits used by various backends.  The generic code doesn't touch
00380 .     these fields.  *}
00381 .
00382 .  {* Nonzero if this section has TLS related relocations.  *}
00383 .  unsigned int has_tls_reloc:1;
00384 .
00385 .  {* Nonzero if this section has a gp reloc.  *}
00386 .  unsigned int has_gp_reloc:1;
00387 .
00388 .  {* Nonzero if this section needs the relax finalize pass.  *}
00389 .  unsigned int need_finalize_relax:1;
00390 .
00391 .  {* Whether relocations have been processed.  *}
00392 .  unsigned int reloc_done : 1;
00393 .
00394 .  {* End of internal packed boolean fields.  *}
00395 .
00396 .  {*  The virtual memory address of the section - where it will be
00397 .      at run time.  The symbols are relocated against this.  The
00398 .      user_set_vma flag is maintained by bfd; if it's not set, the
00399 .      backend can assign addresses (for example, in <<a.out>>, where
00400 .      the default address for <<.data>> is dependent on the specific
00401 .      target and various flags).  *}
00402 .  bfd_vma vma;
00403 .
00404 .  {*  The load address of the section - where it would be in a
00405 .      rom image; really only used for writing section header
00406 .      information.  *}
00407 .  bfd_vma lma;
00408 .
00409 .  {* The size of the section in octets, as it will be output.
00410 .     Contains a value even if the section has no contents (e.g., the
00411 .     size of <<.bss>>).  *}
00412 .  bfd_size_type size;
00413 .
00414 .  {* For input sections, the original size on disk of the section, in
00415 .     octets.  This field is used by the linker relaxation code.  It is
00416 .     currently only set for sections where the linker relaxation scheme
00417 .     doesn't cache altered section and reloc contents (stabs, eh_frame,
00418 .     SEC_MERGE, some coff relaxing targets), and thus the original size
00419 .     needs to be kept to read the section multiple times.
00420 .     For output sections, rawsize holds the section size calculated on
00421 .     a previous linker relaxation pass.  *}
00422 .  bfd_size_type rawsize;
00423 .
00424 .  {* If this section is going to be output, then this value is the
00425 .     offset in *bytes* into the output section of the first byte in the
00426 .     input section (byte ==> smallest addressable unit on the
00427 .     target).  In most cases, if this was going to start at the
00428 .     100th octet (8-bit quantity) in the output section, this value
00429 .     would be 100.  However, if the target byte size is 16 bits
00430 .     (bfd_octets_per_byte is "2"), this value would be 50.  *}
00431 .  bfd_vma output_offset;
00432 .
00433 .  {* The output section through which to map on output.  *}
00434 .  struct bfd_section *output_section;
00435 .
00436 .  {* The alignment requirement of the section, as an exponent of 2 -
00437 .     e.g., 3 aligns to 2^3 (or 8).  *}
00438 .  unsigned int alignment_power;
00439 .
00440 .  {* If an input section, a pointer to a vector of relocation
00441 .     records for the data in this section.  *}
00442 .  struct reloc_cache_entry *relocation;
00443 .
00444 .  {* If an output section, a pointer to a vector of pointers to
00445 .     relocation records for the data in this section.  *}
00446 .  struct reloc_cache_entry **orelocation;
00447 .
00448 .  {* The number of relocation records in one of the above.  *}
00449 .  unsigned reloc_count;
00450 .
00451 .  {* Information below is back end specific - and not always used
00452 .     or updated.  *}
00453 .
00454 .  {* File position of section data.  *}
00455 .  file_ptr filepos;
00456 .
00457 .  {* File position of relocation info.  *}
00458 .  file_ptr rel_filepos;
00459 .
00460 .  {* File position of line data.  *}
00461 .  file_ptr line_filepos;
00462 .
00463 .  {* Pointer to data for applications.  *}
00464 .  void *userdata;
00465 .
00466 .  {* If the SEC_IN_MEMORY flag is set, this points to the actual
00467 .     contents.  *}
00468 .  unsigned char *contents;
00469 .
00470 .  {* Attached line number information.  *}
00471 .  alent *lineno;
00472 .
00473 .  {* Number of line number records.  *}
00474 .  unsigned int lineno_count;
00475 .
00476 .  {* Entity size for merging purposes.  *}
00477 .  unsigned int entsize;
00478 .
00479 .  {* Points to the kept section if this section is a link-once section,
00480 .     and is discarded.  *}
00481 .  struct bfd_section *kept_section;
00482 .
00483 .  {* When a section is being output, this value changes as more
00484 .     linenumbers are written out.  *}
00485 .  file_ptr moving_line_filepos;
00486 .
00487 .  {* What the section number is in the target world.  *}
00488 .  int target_index;
00489 .
00490 .  void *used_by_bfd;
00491 .
00492 .  {* If this is a constructor section then here is a list of the
00493 .     relocations created to relocate items within it.  *}
00494 .  struct relent_chain *constructor_chain;
00495 .
00496 .  {* The BFD which owns the section.  *}
00497 .  bfd *owner;
00498 .
00499 .  {* A symbol which points at this section only.  *}
00500 .  struct bfd_symbol *symbol;
00501 .  struct bfd_symbol **symbol_ptr_ptr;
00502 .
00503 .  {* Early in the link process, map_head and map_tail are used to build
00504 .     a list of input sections attached to an output section.  Later,
00505 .     output sections use these fields for a list of bfd_link_order
00506 .     structs.  *}
00507 .  union {
00508 .    struct bfd_link_order *link_order;
00509 .    struct bfd_section *s;
00510 .  } map_head, map_tail;
00511 .} asection;
00512 .
00513 .{* These sections are global, and are managed by BFD.  The application
00514 .   and target back end are not permitted to change the values in
00515 .   these sections.  New code should use the section_ptr macros rather
00516 .   than referring directly to the const sections.  The const sections
00517 .   may eventually vanish.  *}
00518 .#define BFD_ABS_SECTION_NAME "*ABS*"
00519 .#define BFD_UND_SECTION_NAME "*UND*"
00520 .#define BFD_COM_SECTION_NAME "*COM*"
00521 .#define BFD_IND_SECTION_NAME "*IND*"
00522 .
00523 .{* The absolute section.  *}
00524 .extern asection bfd_abs_section;
00525 .#define bfd_abs_section_ptr ((asection *) &bfd_abs_section)
00526 .#define bfd_is_abs_section(sec) ((sec) == bfd_abs_section_ptr)
00527 .{* Pointer to the undefined section.  *}
00528 .extern asection bfd_und_section;
00529 .#define bfd_und_section_ptr ((asection *) &bfd_und_section)
00530 .#define bfd_is_und_section(sec) ((sec) == bfd_und_section_ptr)
00531 .{* Pointer to the common section.  *}
00532 .extern asection bfd_com_section;
00533 .#define bfd_com_section_ptr ((asection *) &bfd_com_section)
00534 .{* Pointer to the indirect section.  *}
00535 .extern asection bfd_ind_section;
00536 .#define bfd_ind_section_ptr ((asection *) &bfd_ind_section)
00537 .#define bfd_is_ind_section(sec) ((sec) == bfd_ind_section_ptr)
00538 .
00539 .#define bfd_is_const_section(SEC)        \
00540 . (   ((SEC) == bfd_abs_section_ptr)             \
00541 .  || ((SEC) == bfd_und_section_ptr)             \
00542 .  || ((SEC) == bfd_com_section_ptr)             \
00543 .  || ((SEC) == bfd_ind_section_ptr))
00544 .
00545 .{* Macros to handle insertion and deletion of a bfd's sections.  These
00546 .   only handle the list pointers, ie. do not adjust section_count,
00547 .   target_index etc.  *}
00548 .#define bfd_section_list_remove(ABFD, S) \
00549 .  do                                            \
00550 .    {                                           \
00551 .      asection *_s = S;                         \
00552 .      asection *_next = _s->next;               \
00553 .      asection *_prev = _s->prev;               \
00554 .      if (_prev)                                \
00555 .        _prev->next = _next;                           \
00556 .      else                                      \
00557 .        (ABFD)->sections = _next;               \
00558 .      if (_next)                                \
00559 .        _next->prev = _prev;                           \
00560 .      else                                      \
00561 .        (ABFD)->section_last = _prev;                  \
00562 .    }                                           \
00563 .  while (0)
00564 .#define bfd_section_list_append(ABFD, S) \
00565 .  do                                            \
00566 .    {                                           \
00567 .      asection *_s = S;                         \
00568 .      bfd *_abfd = ABFD;                        \
00569 .      _s->next = NULL;                                 \
00570 .      if (_abfd->section_last)                         \
00571 .        {                                       \
00572 .          _s->prev = _abfd->section_last;              \
00573 .          _abfd->section_last->next = _s;              \
00574 .        }                                       \
00575 .      else                                      \
00576 .        {                                       \
00577 .          _s->prev = NULL;                      \
00578 .          _abfd->sections = _s;                 \
00579 .        }                                       \
00580 .      _abfd->section_last = _s;                 \
00581 .    }                                           \
00582 .  while (0)
00583 .#define bfd_section_list_prepend(ABFD, S) \
00584 .  do                                            \
00585 .    {                                           \
00586 .      asection *_s = S;                         \
00587 .      bfd *_abfd = ABFD;                        \
00588 .      _s->prev = NULL;                                 \
00589 .      if (_abfd->sections)                      \
00590 .        {                                       \
00591 .          _s->next = _abfd->sections;                  \
00592 .          _abfd->sections->prev = _s;                  \
00593 .        }                                       \
00594 .      else                                      \
00595 .        {                                       \
00596 .          _s->next = NULL;                      \
00597 .          _abfd->section_last = _s;                    \
00598 .        }                                       \
00599 .      _abfd->sections = _s;                            \
00600 .    }                                           \
00601 .  while (0)
00602 .#define bfd_section_list_insert_after(ABFD, A, S) \
00603 .  do                                            \
00604 .    {                                           \
00605 .      asection *_a = A;                         \
00606 .      asection *_s = S;                         \
00607 .      asection *_next = _a->next;               \
00608 .      _s->next = _next;                         \
00609 .      _s->prev = _a;                                   \
00610 .      _a->next = _s;                                   \
00611 .      if (_next)                                \
00612 .        _next->prev = _s;                       \
00613 .      else                                      \
00614 .        (ABFD)->section_last = _s;                     \
00615 .    }                                           \
00616 .  while (0)
00617 .#define bfd_section_list_insert_before(ABFD, B, S) \
00618 .  do                                            \
00619 .    {                                           \
00620 .      asection *_b = B;                         \
00621 .      asection *_s = S;                         \
00622 .      asection *_prev = _b->prev;               \
00623 .      _s->prev = _prev;                         \
00624 .      _s->next = _b;                                   \
00625 .      _b->prev = _s;                                   \
00626 .      if (_prev)                                \
00627 .        _prev->next = _s;                       \
00628 .      else                                      \
00629 .        (ABFD)->sections = _s;                         \
00630 .    }                                           \
00631 .  while (0)
00632 .#define bfd_section_removed_from_list(ABFD, S) \
00633 .  ((S)->next == NULL ? (ABFD)->section_last != (S) : (S)->next->prev != (S))
00634 .
00635 .#define BFD_FAKE_SECTION(SEC, FLAGS, SYM, NAME, IDX)                 \
00636 .  {* name, id,  index, next, prev, flags, user_set_vma,            *}       \
00637 .  { NAME,  IDX, 0,     NULL, NULL, FLAGS, 0,                         \
00638 .                                                              \
00639 .  {* linker_mark, linker_has_input, gc_mark, gc_mark_from_eh,      *}       \
00640 .     0,           0,                1,       0,               \
00641 .                                                              \
00642 .  {* segment_mark, sec_info_type, use_rela_p, has_tls_reloc,       *}       \
00643 .     0,            0,             0,          0,                     \
00644 .                                                              \
00645 .  {* has_gp_reloc, need_finalize_relax, reloc_done,                *}       \
00646 .     0,            0,                   0,                           \
00647 .                                                              \
00648 .  {* vma, lma, size, rawsize                                       *}       \
00649 .     0,   0,   0,    0,                                       \
00650 .                                                              \
00651 .  {* output_offset, output_section,              alignment_power,  *}       \
00652 .     0,             (struct bfd_section *) &SEC, 0,                  \
00653 .                                                              \
00654 .  {* relocation, orelocation, reloc_count, filepos, rel_filepos,   *}       \
00655 .     NULL,       NULL,        0,           0,       0,               \
00656 .                                                              \
00657 .  {* line_filepos, userdata, contents, lineno, lineno_count,       *}       \
00658 .     0,            NULL,     NULL,     NULL,   0,                    \
00659 .                                                              \
00660 .  {* entsize, kept_section, moving_line_filepos,                   *}       \
00661 .     0,       NULL,       0,                                  \
00662 .                                                              \
00663 .  {* target_index, used_by_bfd, constructor_chain, owner,          *}       \
00664 .     0,            NULL,        NULL,              NULL,             \
00665 .                                                              \
00666 .  {* symbol,                    symbol_ptr_ptr,                    *}       \
00667 .     (struct bfd_symbol *) SYM, &SEC.symbol,                         \
00668 .                                                              \
00669 .  {* map_head, map_tail                                            *}       \
00670 .     { NULL }, { NULL }                                       \
00671 .    }
00672 .
00673 */
00674 
00675 /* We use a macro to initialize the static asymbol structures because
00676    traditional C does not permit us to initialize a union member while
00677    gcc warns if we don't initialize it.  */
00678  /* the_bfd, name, value, attr, section [, udata] */
00679 #ifdef __STDC__
00680 #define GLOBAL_SYM_INIT(NAME, SECTION) \
00681   { 0, NAME, 0, BSF_SECTION_SYM, (asection *) SECTION, { 0 }}
00682 #else
00683 #define GLOBAL_SYM_INIT(NAME, SECTION) \
00684   { 0, NAME, 0, BSF_SECTION_SYM, (asection *) SECTION }
00685 #endif
00686 
00687 /* These symbols are global, not specific to any BFD.  Therefore, anything
00688    that tries to change them is broken, and should be repaired.  */
00689 
00690 static const asymbol global_syms[] =
00691 {
00692   GLOBAL_SYM_INIT (BFD_COM_SECTION_NAME, &bfd_com_section),
00693   GLOBAL_SYM_INIT (BFD_UND_SECTION_NAME, &bfd_und_section),
00694   GLOBAL_SYM_INIT (BFD_ABS_SECTION_NAME, &bfd_abs_section),
00695   GLOBAL_SYM_INIT (BFD_IND_SECTION_NAME, &bfd_ind_section)
00696 };
00697 
00698 #define STD_SECTION(SEC, FLAGS, NAME, IDX)                            \
00699   asection SEC = BFD_FAKE_SECTION(SEC, FLAGS, &global_syms[IDX],      \
00700                               NAME, IDX)
00701 
00702 STD_SECTION (bfd_com_section, SEC_IS_COMMON, BFD_COM_SECTION_NAME, 0);
00703 STD_SECTION (bfd_und_section, 0, BFD_UND_SECTION_NAME, 1);
00704 STD_SECTION (bfd_abs_section, 0, BFD_ABS_SECTION_NAME, 2);
00705 STD_SECTION (bfd_ind_section, 0, BFD_IND_SECTION_NAME, 3);
00706 #undef STD_SECTION
00707 
00708 /* Initialize an entry in the section hash table.  */
00709 
00710 struct bfd_hash_entry *
00711 bfd_section_hash_newfunc (struct bfd_hash_entry *entry,
00712                        struct bfd_hash_table *table,
00713                        const char *string)
00714 {
00715   /* Allocate the structure if it has not already been allocated by a
00716      subclass.  */
00717   if (entry == NULL)
00718     {
00719       entry = (struct bfd_hash_entry *)
00720        bfd_hash_allocate (table, sizeof (struct section_hash_entry));
00721       if (entry == NULL)
00722        return entry;
00723     }
00724 
00725   /* Call the allocation method of the superclass.  */
00726   entry = bfd_hash_newfunc (entry, table, string);
00727   if (entry != NULL)
00728     memset (&((struct section_hash_entry *) entry)->section, 0,
00729            sizeof (asection));
00730 
00731   return entry;
00732 }
00733 
00734 #define section_hash_lookup(table, string, create, copy) \
00735   ((struct section_hash_entry *) \
00736    bfd_hash_lookup ((table), (string), (create), (copy)))
00737 
00738 /* Create a symbol whose only job is to point to this section.  This
00739    is useful for things like relocs which are relative to the base
00740    of a section.  */
00741 
00742 bfd_boolean
00743 _bfd_generic_new_section_hook (bfd *abfd, asection *newsect)
00744 {
00745   newsect->symbol = bfd_make_empty_symbol (abfd);
00746   if (newsect->symbol == NULL)
00747     return FALSE;
00748 
00749   newsect->symbol->name = newsect->name;
00750   newsect->symbol->value = 0;
00751   newsect->symbol->section = newsect;
00752   newsect->symbol->flags = BSF_SECTION_SYM;
00753 
00754   newsect->symbol_ptr_ptr = &newsect->symbol;
00755   return TRUE;
00756 }
00757 
00758 /* Initializes a new section.  NEWSECT->NAME is already set.  */
00759 
00760 static asection *
00761 bfd_section_init (bfd *abfd, asection *newsect)
00762 {
00763   static int section_id = 0x10;  /* id 0 to 3 used by STD_SECTION.  */
00764 
00765   newsect->id = section_id;
00766   newsect->index = abfd->section_count;
00767   newsect->owner = abfd;
00768 
00769   if (! BFD_SEND (abfd, _new_section_hook, (abfd, newsect)))
00770     return NULL;
00771 
00772   section_id++;
00773   abfd->section_count++;
00774   bfd_section_list_append (abfd, newsect);
00775   return newsect;
00776 }
00777 
00778 /*
00779 DOCDD
00780 INODE
00781 section prototypes,  , typedef asection, Sections
00782 SUBSECTION
00783        Section prototypes
00784 
00785 These are the functions exported by the section handling part of BFD.
00786 */
00787 
00788 /*
00789 FUNCTION
00790        bfd_section_list_clear
00791 
00792 SYNOPSIS
00793        void bfd_section_list_clear (bfd *);
00794 
00795 DESCRIPTION
00796        Clears the section list, and also resets the section count and
00797        hash table entries.
00798 */
00799 
00800 void
00801 bfd_section_list_clear (bfd *abfd)
00802 {
00803   abfd->sections = NULL;
00804   abfd->section_last = NULL;
00805   abfd->section_count = 0;
00806   memset (abfd->section_htab.table, 0,
00807          abfd->section_htab.size * sizeof (struct bfd_hash_entry *));
00808 }
00809 
00810 /*
00811 FUNCTION
00812        bfd_get_section_by_name
00813 
00814 SYNOPSIS
00815        asection *bfd_get_section_by_name (bfd *abfd, const char *name);
00816 
00817 DESCRIPTION
00818        Run through @var{abfd} and return the one of the
00819        <<asection>>s whose name matches @var{name}, otherwise <<NULL>>.
00820        @xref{Sections}, for more information.
00821 
00822        This should only be used in special cases; the normal way to process
00823        all sections of a given name is to use <<bfd_map_over_sections>> and
00824        <<strcmp>> on the name (or better yet, base it on the section flags
00825        or something else) for each section.
00826 */
00827 
00828 asection *
00829 bfd_get_section_by_name (bfd *abfd, const char *name)
00830 {
00831   struct section_hash_entry *sh;
00832 
00833   sh = section_hash_lookup (&abfd->section_htab, name, FALSE, FALSE);
00834   if (sh != NULL)
00835     return &sh->section;
00836 
00837   return NULL;
00838 }
00839 
00840 /*
00841 FUNCTION
00842        bfd_get_section_by_name_if
00843 
00844 SYNOPSIS
00845        asection *bfd_get_section_by_name_if
00846          (bfd *abfd,
00847           const char *name,
00848           bfd_boolean (*func) (bfd *abfd, asection *sect, void *obj),
00849           void *obj);
00850 
00851 DESCRIPTION
00852        Call the provided function @var{func} for each section
00853        attached to the BFD @var{abfd} whose name matches @var{name},
00854        passing @var{obj} as an argument. The function will be called
00855        as if by
00856 
00857 |      func (abfd, the_section, obj);
00858 
00859        It returns the first section for which @var{func} returns true,
00860        otherwise <<NULL>>.
00861 
00862 */
00863 
00864 asection *
00865 bfd_get_section_by_name_if (bfd *abfd, const char *name,
00866                          bfd_boolean (*operation) (bfd *,
00867                                                 asection *,
00868                                                 void *),
00869                          void *user_storage)
00870 {
00871   struct section_hash_entry *sh;
00872   unsigned long hash;
00873 
00874   sh = section_hash_lookup (&abfd->section_htab, name, FALSE, FALSE);
00875   if (sh == NULL)
00876     return NULL;
00877 
00878   hash = sh->root.hash;
00879   do
00880     {
00881       if ((*operation) (abfd, &sh->section, user_storage))
00882        return &sh->section;
00883       sh = (struct section_hash_entry *) sh->root.next;
00884     }
00885   while (sh != NULL && sh->root.hash == hash
00886         && strcmp (sh->root.string, name) == 0);
00887 
00888   return NULL;
00889 }
00890 
00891 /*
00892 FUNCTION
00893        bfd_get_unique_section_name
00894 
00895 SYNOPSIS
00896        char *bfd_get_unique_section_name
00897          (bfd *abfd, const char *templat, int *count);
00898 
00899 DESCRIPTION
00900        Invent a section name that is unique in @var{abfd} by tacking
00901        a dot and a digit suffix onto the original @var{templat}.  If
00902        @var{count} is non-NULL, then it specifies the first number
00903        tried as a suffix to generate a unique name.  The value
00904        pointed to by @var{count} will be incremented in this case.
00905 */
00906 
00907 char *
00908 bfd_get_unique_section_name (bfd *abfd, const char *templat, int *count)
00909 {
00910   int num;
00911   unsigned int len;
00912   char *sname;
00913 
00914   len = strlen (templat);
00915   sname = bfd_malloc (len + 8);
00916   if (sname == NULL)
00917     return NULL;
00918   memcpy (sname, templat, len);
00919   num = 1;
00920   if (count != NULL)
00921     num = *count;
00922 
00923   do
00924     {
00925       /* If we have a million sections, something is badly wrong.  */
00926       if (num > 999999)
00927        abort ();
00928       sprintf (sname + len, ".%d", num++);
00929     }
00930   while (section_hash_lookup (&abfd->section_htab, sname, FALSE, FALSE));
00931 
00932   if (count != NULL)
00933     *count = num;
00934   return sname;
00935 }
00936 
00937 /*
00938 FUNCTION
00939        bfd_make_section_old_way
00940 
00941 SYNOPSIS
00942        asection *bfd_make_section_old_way (bfd *abfd, const char *name);
00943 
00944 DESCRIPTION
00945        Create a new empty section called @var{name}
00946        and attach it to the end of the chain of sections for the
00947        BFD @var{abfd}. An attempt to create a section with a name which
00948        is already in use returns its pointer without changing the
00949        section chain.
00950 
00951        It has the funny name since this is the way it used to be
00952        before it was rewritten....
00953 
00954        Possible errors are:
00955        o <<bfd_error_invalid_operation>> -
00956        If output has already started for this BFD.
00957        o <<bfd_error_no_memory>> -
00958        If memory allocation fails.
00959 
00960 */
00961 
00962 asection *
00963 bfd_make_section_old_way (bfd *abfd, const char *name)
00964 {
00965   asection *newsect;
00966 
00967   if (abfd->output_has_begun)
00968     {
00969       bfd_set_error (bfd_error_invalid_operation);
00970       return NULL;
00971     }
00972 
00973   if (strcmp (name, BFD_ABS_SECTION_NAME) == 0)
00974     newsect = bfd_abs_section_ptr;
00975   else if (strcmp (name, BFD_COM_SECTION_NAME) == 0)
00976     newsect = bfd_com_section_ptr;
00977   else if (strcmp (name, BFD_UND_SECTION_NAME) == 0)
00978     newsect = bfd_und_section_ptr;
00979   else if (strcmp (name, BFD_IND_SECTION_NAME) == 0)
00980     newsect = bfd_ind_section_ptr;
00981   else
00982     {
00983       struct section_hash_entry *sh;
00984 
00985       sh = section_hash_lookup (&abfd->section_htab, name, TRUE, FALSE);
00986       if (sh == NULL)
00987        return NULL;
00988 
00989       newsect = &sh->section;
00990       if (newsect->name != NULL)
00991        {
00992          /* Section already exists.  */
00993          return newsect;
00994        }
00995 
00996       newsect->name = name;
00997       return bfd_section_init (abfd, newsect);
00998     }
00999 
01000   /* Call new_section_hook when "creating" the standard abs, com, und
01001      and ind sections to tack on format specific section data.
01002      Also, create a proper section symbol.  */
01003   if (! BFD_SEND (abfd, _new_section_hook, (abfd, newsect)))
01004     return NULL;
01005   return newsect;
01006 }
01007 
01008 /*
01009 FUNCTION
01010        bfd_make_section_anyway_with_flags
01011 
01012 SYNOPSIS
01013        asection *bfd_make_section_anyway_with_flags
01014          (bfd *abfd, const char *name, flagword flags);
01015 
01016 DESCRIPTION
01017    Create a new empty section called @var{name} and attach it to the end of
01018    the chain of sections for @var{abfd}.  Create a new section even if there
01019    is already a section with that name.  Also set the attributes of the
01020    new section to the value @var{flags}.
01021 
01022    Return <<NULL>> and set <<bfd_error>> on error; possible errors are:
01023    o <<bfd_error_invalid_operation>> - If output has already started for @var{abfd}.
01024    o <<bfd_error_no_memory>> - If memory allocation fails.
01025 */
01026 
01027 sec_ptr
01028 bfd_make_section_anyway_with_flags (bfd *abfd, const char *name,
01029                                 flagword flags)
01030 {
01031   struct section_hash_entry *sh;
01032   asection *newsect;
01033 
01034   if (abfd->output_has_begun)
01035     {
01036       bfd_set_error (bfd_error_invalid_operation);
01037       return NULL;
01038     }
01039 
01040   sh = section_hash_lookup (&abfd->section_htab, name, TRUE, FALSE);
01041   if (sh == NULL)
01042     return NULL;
01043 
01044   newsect = &sh->section;
01045   if (newsect->name != NULL)
01046     {
01047       /* We are making a section of the same name.  Put it in the
01048         section hash table.  Even though we can't find it directly by a
01049         hash lookup, we'll be able to find the section by traversing
01050         sh->root.next quicker than looking at all the bfd sections.  */
01051       struct section_hash_entry *new_sh;
01052       new_sh = (struct section_hash_entry *)
01053        bfd_section_hash_newfunc (NULL, &abfd->section_htab, name);
01054       if (new_sh == NULL)
01055        return NULL;
01056 
01057       new_sh->root = sh->root;
01058       sh->root.next = &new_sh->root;
01059       newsect = &new_sh->section;
01060     }
01061 
01062   newsect->flags = flags;
01063   newsect->name = name;
01064   return bfd_section_init (abfd, newsect);
01065 }
01066 
01067 /*
01068 FUNCTION
01069        bfd_make_section_anyway
01070 
01071 SYNOPSIS
01072        asection *bfd_make_section_anyway (bfd *abfd, const char *name);
01073 
01074 DESCRIPTION
01075    Create a new empty section called @var{name} and attach it to the end of
01076    the chain of sections for @var{abfd}.  Create a new section even if there
01077    is already a section with that name.
01078 
01079    Return <<NULL>> and set <<bfd_error>> on error; possible errors are:
01080    o <<bfd_error_invalid_operation>> - If output has already started for @var{abfd}.
01081    o <<bfd_error_no_memory>> - If memory allocation fails.
01082 */
01083 
01084 sec_ptr
01085 bfd_make_section_anyway (bfd *abfd, const char *name)
01086 {
01087   return bfd_make_section_anyway_with_flags (abfd, name, 0);
01088 }
01089 
01090 /*
01091 FUNCTION
01092        bfd_make_section_with_flags
01093 
01094 SYNOPSIS
01095        asection *bfd_make_section_with_flags
01096          (bfd *, const char *name, flagword flags);
01097 
01098 DESCRIPTION
01099    Like <<bfd_make_section_anyway>>, but return <<NULL>> (without calling
01100    bfd_set_error ()) without changing the section chain if there is already a
01101    section named @var{name}.  Also set the attributes of the new section to
01102    the value @var{flags}.  If there is an error, return <<NULL>> and set
01103    <<bfd_error>>.
01104 */
01105 
01106 asection *
01107 bfd_make_section_with_flags (bfd *abfd, const char *name,
01108                           flagword flags)
01109 {
01110   struct section_hash_entry *sh;
01111   asection *newsect;
01112 
01113   if (abfd->output_has_begun)
01114     {
01115       bfd_set_error (bfd_error_invalid_operation);
01116       return NULL;
01117     }
01118 
01119   if (strcmp (name, BFD_ABS_SECTION_NAME) == 0
01120       || strcmp (name, BFD_COM_SECTION_NAME) == 0
01121       || strcmp (name, BFD_UND_SECTION_NAME) == 0
01122       || strcmp (name, BFD_IND_SECTION_NAME) == 0)
01123     return NULL;
01124 
01125   sh = section_hash_lookup (&abfd->section_htab, name, TRUE, FALSE);
01126   if (sh == NULL)
01127     return NULL;
01128 
01129   newsect = &sh->section;
01130   if (newsect->name != NULL)
01131     {
01132       /* Section already exists.  */
01133       return NULL;
01134     }
01135 
01136   newsect->name = name;
01137   newsect->flags = flags;
01138   return bfd_section_init (abfd, newsect);
01139 }
01140 
01141 /*
01142 FUNCTION
01143        bfd_make_section
01144 
01145 SYNOPSIS
01146        asection *bfd_make_section (bfd *, const char *name);
01147 
01148 DESCRIPTION
01149    Like <<bfd_make_section_anyway>>, but return <<NULL>> (without calling
01150    bfd_set_error ()) without changing the section chain if there is already a
01151    section named @var{name}.  If there is an error, return <<NULL>> and set
01152    <<bfd_error>>.
01153 */
01154 
01155 asection *
01156 bfd_make_section (bfd *abfd, const char *name)
01157 {
01158   return bfd_make_section_with_flags (abfd, name, 0);
01159 }
01160 
01161 /*
01162 FUNCTION
01163        bfd_set_section_flags
01164 
01165 SYNOPSIS
01166        bfd_boolean bfd_set_section_flags
01167          (bfd *abfd, asection *sec, flagword flags);
01168 
01169 DESCRIPTION
01170        Set the attributes of the section @var{sec} in the BFD
01171        @var{abfd} to the value @var{flags}. Return <<TRUE>> on success,
01172        <<FALSE>> on error. Possible error returns are:
01173 
01174        o <<bfd_error_invalid_operation>> -
01175        The section cannot have one or more of the attributes
01176        requested. For example, a .bss section in <<a.out>> may not
01177        have the <<SEC_HAS_CONTENTS>> field set.
01178 
01179 */
01180 
01181 bfd_boolean
01182 bfd_set_section_flags (bfd *abfd ATTRIBUTE_UNUSED,
01183                      sec_ptr section,
01184                      flagword flags)
01185 {
01186   section->flags = flags;
01187   return TRUE;
01188 }
01189 
01190 /*
01191 FUNCTION
01192        bfd_map_over_sections
01193 
01194 SYNOPSIS
01195        void bfd_map_over_sections
01196          (bfd *abfd,
01197           void (*func) (bfd *abfd, asection *sect, void *obj),
01198           void *obj);
01199 
01200 DESCRIPTION
01201        Call the provided function @var{func} for each section
01202        attached to the BFD @var{abfd}, passing @var{obj} as an
01203        argument. The function will be called as if by
01204 
01205 |      func (abfd, the_section, obj);
01206 
01207        This is the preferred method for iterating over sections; an
01208        alternative would be to use a loop:
01209 
01210 |         section *p;
01211 |         for (p = abfd->sections; p != NULL; p = p->next)
01212 |            func (abfd, p, ...)
01213 
01214 */
01215 
01216 void
01217 bfd_map_over_sections (bfd *abfd,
01218                      void (*operation) (bfd *, asection *, void *),
01219                      void *user_storage)
01220 {
01221   asection *sect;
01222   unsigned int i = 0;
01223 
01224   for (sect = abfd->sections; sect != NULL; i++, sect = sect->next)
01225     (*operation) (abfd, sect, user_storage);
01226 
01227   if (i != abfd->section_count)    /* Debugging */
01228     abort ();
01229 }
01230 
01231 /*
01232 FUNCTION
01233        bfd_sections_find_if
01234 
01235 SYNOPSIS
01236        asection *bfd_sections_find_if
01237          (bfd *abfd,
01238           bfd_boolean (*operation) (bfd *abfd, asection *sect, void *obj),
01239           void *obj);
01240 
01241 DESCRIPTION
01242        Call the provided function @var{operation} for each section
01243        attached to the BFD @var{abfd}, passing @var{obj} as an
01244        argument. The function will be called as if by
01245 
01246 |      operation (abfd, the_section, obj);
01247 
01248        It returns the first section for which @var{operation} returns true.
01249 
01250 */
01251 
01252 asection *
01253 bfd_sections_find_if (bfd *abfd,
01254                     bfd_boolean (*operation) (bfd *, asection *, void *),
01255                     void *user_storage)
01256 {
01257   asection *sect;
01258 
01259   for (sect = abfd->sections; sect != NULL; sect = sect->next)
01260     if ((*operation) (abfd, sect, user_storage))
01261       break;
01262 
01263   return sect;
01264 }
01265 
01266 /*
01267 FUNCTION
01268        bfd_set_section_size
01269 
01270 SYNOPSIS
01271        bfd_boolean bfd_set_section_size
01272          (bfd *abfd, asection *sec, bfd_size_type val);
01273 
01274 DESCRIPTION
01275        Set @var{sec} to the size @var{val}. If the operation is
01276        ok, then <<TRUE>> is returned, else <<FALSE>>.
01277 
01278        Possible error returns:
01279        o <<bfd_error_invalid_operation>> -
01280        Writing has started to the BFD, so setting the size is invalid.
01281 
01282 */
01283 
01284 bfd_boolean
01285 bfd_set_section_size (bfd *abfd, sec_ptr ptr, bfd_size_type val)
01286 {
01287   /* Once you've started writing to any section you cannot create or change
01288      the size of any others.  */
01289 
01290   if (abfd->output_has_begun)
01291     {
01292       bfd_set_error (bfd_error_invalid_operation);
01293       return FALSE;
01294     }
01295 
01296   ptr->size = val;
01297   return TRUE;
01298 }
01299 
01300 /*
01301 FUNCTION
01302        bfd_set_section_contents
01303 
01304 SYNOPSIS
01305        bfd_boolean bfd_set_section_contents
01306          (bfd *abfd, asection *section, const void *data,
01307           file_ptr offset, bfd_size_type count);
01308 
01309 DESCRIPTION
01310        Sets the contents of the section @var{section} in BFD
01311        @var{abfd} to the data starting in memory at @var{data}. The
01312        data is written to the output section starting at offset
01313        @var{offset} for @var{count} octets.
01314 
01315        Normally <<TRUE>> is returned, else <<FALSE>>. Possible error
01316        returns are:
01317        o <<bfd_error_no_contents>> -
01318        The output section does not have the <<SEC_HAS_CONTENTS>>
01319        attribute, so nothing can be written to it.
01320        o and some more too
01321 
01322        This routine is front end to the back end function
01323        <<_bfd_set_section_contents>>.
01324 
01325 */
01326 
01327 bfd_boolean
01328 bfd_set_section_contents (bfd *abfd,
01329                        sec_ptr section,
01330                        const void *location,
01331                        file_ptr offset,
01332                        bfd_size_type count)
01333 {
01334   bfd_size_type sz;
01335 
01336   if (!(bfd_get_section_flags (abfd, section) & SEC_HAS_CONTENTS))
01337     {
01338       bfd_set_error (bfd_error_no_contents);
01339       return FALSE;
01340     }
01341 
01342   sz = section->size;
01343   if ((bfd_size_type) offset > sz
01344       || count > sz
01345       || offset + count > sz
01346       || count != (size_t) count)
01347     {
01348       bfd_set_error (bfd_error_bad_value);
01349       return FALSE;
01350     }
01351 
01352   if (!bfd_write_p (abfd))
01353     {
01354       bfd_set_error (bfd_error_invalid_operation);
01355       return FALSE;
01356     }
01357 
01358   /* Record a copy of the data in memory if desired.  */
01359   if (section->contents
01360       && location != section->contents + offset)
01361     memcpy (section->contents + offset, location, (size_t) count);
01362 
01363   if (BFD_SEND (abfd, _bfd_set_section_contents,
01364               (abfd, section, location, offset, count)))
01365     {
01366       abfd->output_has_begun = TRUE;
01367       return TRUE;
01368     }
01369 
01370   return FALSE;
01371 }
01372 
01373 /*
01374 FUNCTION
01375        bfd_get_section_contents
01376 
01377 SYNOPSIS
01378        bfd_boolean bfd_get_section_contents
01379          (bfd *abfd, asection *section, void *location, file_ptr offset,
01380           bfd_size_type count);
01381 
01382 DESCRIPTION
01383        Read data from @var{section} in BFD @var{abfd}
01384        into memory starting at @var{location}. The data is read at an
01385        offset of @var{offset} from the start of the input section,
01386        and is read for @var{count} bytes.
01387 
01388        If the contents of a constructor with the <<SEC_CONSTRUCTOR>>
01389        flag set are requested or if the section does not have the
01390        <<SEC_HAS_CONTENTS>> flag set, then the @var{location} is filled
01391        with zeroes. If no errors occur, <<TRUE>> is returned, else
01392        <<FALSE>>.
01393 
01394 */
01395 bfd_boolean
01396 bfd_get_section_contents (bfd *abfd,
01397                        sec_ptr section,
01398                        void *location,
01399                        file_ptr offset,
01400                        bfd_size_type count)
01401 {
01402   bfd_size_type sz;
01403 
01404   if (section->flags & SEC_CONSTRUCTOR)
01405     {
01406       memset (location, 0, (size_t) count);
01407       return TRUE;
01408     }
01409 
01410   sz = section->rawsize ? section->rawsize : section->size;
01411   if ((bfd_size_type) offset > sz
01412       || count > sz
01413       || offset + count > sz
01414       || count != (size_t) count)
01415     {
01416       bfd_set_error (bfd_error_bad_value);
01417       return FALSE;
01418     }
01419 
01420   if (count == 0)
01421     /* Don't bother.  */
01422     return TRUE;
01423 
01424   if ((section->flags & SEC_HAS_CONTENTS) == 0)
01425     {
01426       memset (location, 0, (size_t) count);
01427       return TRUE;
01428     }
01429 
01430   if ((section->flags & SEC_IN_MEMORY) != 0)
01431     {
01432       memcpy (location, section->contents + offset, (size_t) count);
01433       return TRUE;
01434     }
01435 
01436   return BFD_SEND (abfd, _bfd_get_section_contents,
01437                  (abfd, section, location, offset, count));
01438 }
01439 
01440 /*
01441 FUNCTION
01442        bfd_malloc_and_get_section
01443 
01444 SYNOPSIS
01445        bfd_boolean bfd_malloc_and_get_section
01446          (bfd *abfd, asection *section, bfd_byte **buf);
01447 
01448 DESCRIPTION
01449        Read all data from @var{section} in BFD @var{abfd}
01450        into a buffer, *@var{buf}, malloc'd by this function.
01451 */
01452 
01453 bfd_boolean
01454 bfd_malloc_and_get_section (bfd *abfd, sec_ptr sec, bfd_byte **buf)
01455 {
01456   bfd_size_type sz = sec->rawsize ? sec->rawsize : sec->size;
01457   bfd_byte *p = NULL;
01458 
01459   *buf = p;
01460   if (sz == 0)
01461     return TRUE;
01462 
01463   p = bfd_malloc (sec->rawsize > sec->size ? sec->rawsize : sec->size);
01464   if (p == NULL)
01465     return FALSE;
01466   *buf = p;
01467 
01468   return bfd_get_section_contents (abfd, sec, p, 0, sz);
01469 }
01470 /*
01471 FUNCTION
01472        bfd_copy_private_section_data
01473 
01474 SYNOPSIS
01475        bfd_boolean bfd_copy_private_section_data
01476          (bfd *ibfd, asection *isec, bfd *obfd, asection *osec);
01477 
01478 DESCRIPTION
01479        Copy private section information from @var{isec} in the BFD
01480        @var{ibfd} to the section @var{osec} in the BFD @var{obfd}.
01481        Return <<TRUE>> on success, <<FALSE>> on error.  Possible error
01482        returns are:
01483 
01484        o <<bfd_error_no_memory>> -
01485        Not enough memory exists to create private data for @var{osec}.
01486 
01487 .#define bfd_copy_private_section_data(ibfd, isection, obfd, osection) \
01488 .     BFD_SEND (obfd, _bfd_copy_private_section_data, \
01489 .             (ibfd, isection, obfd, osection))
01490 */
01491 
01492 /*
01493 FUNCTION
01494        bfd_generic_is_group_section
01495 
01496 SYNOPSIS
01497        bfd_boolean bfd_generic_is_group_section (bfd *, const asection *sec);
01498 
01499 DESCRIPTION
01500        Returns TRUE if @var{sec} is a member of a group.
01501 */
01502 
01503 bfd_boolean
01504 bfd_generic_is_group_section (bfd *abfd ATTRIBUTE_UNUSED,
01505                            const asection *sec ATTRIBUTE_UNUSED)
01506 {
01507   return FALSE;
01508 }
01509 
01510 /*
01511 FUNCTION
01512        bfd_generic_discard_group
01513 
01514 SYNOPSIS
01515        bfd_boolean bfd_generic_discard_group (bfd *abfd, asection *group);
01516 
01517 DESCRIPTION
01518        Remove all members of @var{group} from the output.
01519 */
01520 
01521 bfd_boolean
01522 bfd_generic_discard_group (bfd *abfd ATTRIBUTE_UNUSED,
01523                         asection *group ATTRIBUTE_UNUSED)
01524 {
01525   return TRUE;
01526 }