section.c revision 77298
1/* Object file "section" support for the BFD library.
2   Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3   Free Software Foundation, Inc.
4   Written by Cygnus Support.
5
6This file is part of BFD, the Binary File Descriptor library.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22/*
23SECTION
24	Sections
25
26	The raw data contained within a BFD is maintained through the
27	section abstraction.  A single BFD may have any number of
28	sections.  It keeps hold of them by pointing to the first;
29	each one points to the next in the list.
30
31	Sections are supported in BFD in <<section.c>>.
32
33@menu
34@* Section Input::
35@* Section Output::
36@* typedef asection::
37@* section prototypes::
38@end menu
39
40INODE
41Section Input, Section Output, Sections, Sections
42SUBSECTION
43	Section input
44
45	When a BFD is opened for reading, the section structures are
46	created and attached to the BFD.
47
48	Each section has a name which describes the section in the
49	outside world---for example, <<a.out>> would contain at least
50	three sections, called <<.text>>, <<.data>> and <<.bss>>.
51
52	Names need not be unique; for example a COFF file may have several
53	sections named <<.data>>.
54
55	Sometimes a BFD will contain more than the ``natural'' number of
56	sections. A back end may attach other sections containing
57	constructor data, or an application may add a section (using
58	<<bfd_make_section>>) to the sections attached to an already open
59	BFD. For example, the linker creates an extra section
60	<<COMMON>> for each input file's BFD to hold information about
61	common storage.
62
63	The raw data is not necessarily read in when
64	the section descriptor is created. Some targets may leave the
65	data in place until a <<bfd_get_section_contents>> call is
66	made. Other back ends may read in all the data at once.  For
67	example, an S-record file has to be read once to determine the
68	size of the data. An IEEE-695 file doesn't contain raw data in
69	sections, but data and relocation expressions intermixed, so
70	the data area has to be parsed to get out the data and
71	relocations.
72
73INODE
74Section Output, typedef asection, Section Input, Sections
75
76SUBSECTION
77	Section output
78
79	To write a new object style BFD, the various sections to be
80	written have to be created. They are attached to the BFD in
81	the same way as input sections; data is written to the
82	sections using <<bfd_set_section_contents>>.
83
84	Any program that creates or combines sections (e.g., the assembler
85	and linker) must use the <<asection>> fields <<output_section>> and
86	<<output_offset>> to indicate the file sections to which each
87	section must be written.  (If the section is being created from
88	scratch, <<output_section>> should probably point to the section
89	itself and <<output_offset>> should probably be zero.)
90
91	The data to be written comes from input sections attached
92	(via <<output_section>> pointers) to
93	the output sections.  The output section structure can be
94	considered a filter for the input section: the output section
95	determines the vma of the output data and the name, but the
96	input section determines the offset into the output section of
97	the data to be written.
98
99	E.g., to create a section "O", starting at 0x100, 0x123 long,
100	containing two subsections, "A" at offset 0x0 (i.e., at vma
101	0x100) and "B" at offset 0x20 (i.e., at vma 0x120) the <<asection>>
102	structures would look like:
103
104|   section name          "A"
105|     output_offset   0x00
106|     size            0x20
107|     output_section ----------->  section name    "O"
108|                             |    vma             0x100
109|   section name          "B" |    size            0x123
110|     output_offset   0x20    |
111|     size            0x103   |
112|     output_section  --------|
113
114SUBSECTION
115	Link orders
116
117	The data within a section is stored in a @dfn{link_order}.
118	These are much like the fixups in <<gas>>.  The link_order
119	abstraction allows a section to grow and shrink within itself.
120
121	A link_order knows how big it is, and which is the next
122	link_order and where the raw data for it is; it also points to
123	a list of relocations which apply to it.
124
125	The link_order is used by the linker to perform relaxing on
126	final code.  The compiler creates code which is as big as
127	necessary to make it work without relaxing, and the user can
128	select whether to relax.  Sometimes relaxing takes a lot of
129	time.  The linker runs around the relocations to see if any
130	are attached to data which can be shrunk, if so it does it on
131	a link_order by link_order basis.
132
133*/
134
135#include "bfd.h"
136#include "sysdep.h"
137#include "libbfd.h"
138#include "bfdlink.h"
139
140/*
141DOCDD
142INODE
143typedef asection, section prototypes, Section Output, Sections
144SUBSECTION
145	typedef asection
146
147	Here is the section structure:
148
149CODE_FRAGMENT
150.
151.{* This structure is used for a comdat section, as in PE.  A comdat
152.   section is associated with a particular symbol.  When the linker
153.   sees a comdat section, it keeps only one of the sections with a
154.   given name and associated with a given symbol.  *}
155.
156.struct bfd_comdat_info
157.{
158.  {* The name of the symbol associated with a comdat section.  *}
159.  const char *name;
160.
161.  {* The local symbol table index of the symbol associated with a
162.     comdat section.  This is only meaningful to the object file format
163.     specific code; it is not an index into the list returned by
164.     bfd_canonicalize_symtab.  *}
165.  long symbol;
166.};
167.
168.typedef struct sec
169.{
170.  {* The name of the section; the name isn't a copy, the pointer is
171.     the same as that passed to bfd_make_section.  *}
172.
173.  const char *name;
174.
175.  {* A unique sequence number.  *}
176.
177.  int id;
178.
179.  {* Which section is it; 0..nth.  *}
180.
181.  int index;
182.
183.  {* The next section in the list belonging to the BFD, or NULL.  *}
184.
185.  struct sec *next;
186.
187.  {* The field flags contains attributes of the section. Some
188.     flags are read in from the object file, and some are
189.     synthesized from other information.  *}
190.
191.  flagword flags;
192.
193.#define SEC_NO_FLAGS   0x000
194.
195.  {* Tells the OS to allocate space for this section when loading.
196.     This is clear for a section containing debug information only.  *}
197.#define SEC_ALLOC      0x001
198.
199.  {* Tells the OS to load the section from the file when loading.
200.     This is clear for a .bss section.  *}
201.#define SEC_LOAD       0x002
202.
203.  {* The section contains data still to be relocated, so there is
204.     some relocation information too.  *}
205.#define SEC_RELOC      0x004
206.
207.#if 0   {* Obsolete ? *}
208.#define SEC_BALIGN     0x008
209.#endif
210.
211.  {* A signal to the OS that the section contains read only data.  *}
212.#define SEC_READONLY   0x010
213.
214.  {* The section contains code only.  *}
215.#define SEC_CODE       0x020
216.
217.  {* The section contains data only.  *}
218.#define SEC_DATA       0x040
219.
220.  {* The section will reside in ROM.  *}
221.#define SEC_ROM        0x080
222.
223.  {* The section contains constructor information. This section
224.     type is used by the linker to create lists of constructors and
225.     destructors used by <<g++>>. When a back end sees a symbol
226.     which should be used in a constructor list, it creates a new
227.     section for the type of name (e.g., <<__CTOR_LIST__>>), attaches
228.     the symbol to it, and builds a relocation. To build the lists
229.     of constructors, all the linker has to do is catenate all the
230.     sections called <<__CTOR_LIST__>> and relocate the data
231.     contained within - exactly the operations it would peform on
232.     standard data.  *}
233.#define SEC_CONSTRUCTOR 0x100
234.
235.  {* The section is a constructor, and should be placed at the
236.     end of the text, data, or bss section(?).  *}
237.#define SEC_CONSTRUCTOR_TEXT 0x1100
238.#define SEC_CONSTRUCTOR_DATA 0x2100
239.#define SEC_CONSTRUCTOR_BSS  0x3100
240.
241.  {* The section has contents - a data section could be
242.     <<SEC_ALLOC>> | <<SEC_HAS_CONTENTS>>; a debug section could be
243.     <<SEC_HAS_CONTENTS>>  *}
244.#define SEC_HAS_CONTENTS 0x200
245.
246.  {* An instruction to the linker to not output the section
247.     even if it has information which would normally be written.  *}
248.#define SEC_NEVER_LOAD 0x400
249.
250.  {* The section is a COFF shared library section.  This flag is
251.     only for the linker.  If this type of section appears in
252.     the input file, the linker must copy it to the output file
253.     without changing the vma or size.  FIXME: Although this
254.     was originally intended to be general, it really is COFF
255.     specific (and the flag was renamed to indicate this).  It
256.     might be cleaner to have some more general mechanism to
257.     allow the back end to control what the linker does with
258.     sections.  *}
259.#define SEC_COFF_SHARED_LIBRARY 0x800
260.
261.  {* The section has GOT references.  This flag is only for the
262.     linker, and is currently only used by the elf32-hppa back end.
263.     It will be set if global offset table references were detected
264.     in this section, which indicate to the linker that the section
265.     contains PIC code, and must be handled specially when doing a
266.     static link.  *}
267.#define SEC_HAS_GOT_REF 0x4000
268.
269.  {* The section contains common symbols (symbols may be defined
270.     multiple times, the value of a symbol is the amount of
271.     space it requires, and the largest symbol value is the one
272.     used).  Most targets have exactly one of these (which we
273.     translate to bfd_com_section_ptr), but ECOFF has two.  *}
274.#define SEC_IS_COMMON 0x8000
275.
276.  {* The section contains only debugging information.  For
277.     example, this is set for ELF .debug and .stab sections.
278.     strip tests this flag to see if a section can be
279.     discarded.  *}
280.#define SEC_DEBUGGING 0x10000
281.
282.  {* The contents of this section are held in memory pointed to
283.     by the contents field.  This is checked by bfd_get_section_contents,
284.     and the data is retrieved from memory if appropriate.  *}
285.#define SEC_IN_MEMORY 0x20000
286.
287.  {* The contents of this section are to be excluded by the
288.     linker for executable and shared objects unless those
289.     objects are to be further relocated.  *}
290.#define SEC_EXCLUDE 0x40000
291.
292.  {* The contents of this section are to be sorted by the
293.     based on the address specified in the associated symbol
294.     table.  *}
295.#define SEC_SORT_ENTRIES 0x80000
296.
297.  {* When linking, duplicate sections of the same name should be
298.     discarded, rather than being combined into a single section as
299.     is usually done.  This is similar to how common symbols are
300.     handled.  See SEC_LINK_DUPLICATES below.  *}
301.#define SEC_LINK_ONCE 0x100000
302.
303.  {* If SEC_LINK_ONCE is set, this bitfield describes how the linker
304.     should handle duplicate sections.  *}
305.#define SEC_LINK_DUPLICATES 0x600000
306.
307.  {* This value for SEC_LINK_DUPLICATES means that duplicate
308.     sections with the same name should simply be discarded.  *}
309.#define SEC_LINK_DUPLICATES_DISCARD 0x0
310.
311.  {* This value for SEC_LINK_DUPLICATES means that the linker
312.     should warn if there are any duplicate sections, although
313.     it should still only link one copy.  *}
314.#define SEC_LINK_DUPLICATES_ONE_ONLY 0x200000
315.
316.  {* This value for SEC_LINK_DUPLICATES means that the linker
317.     should warn if any duplicate sections are a different size.  *}
318.#define SEC_LINK_DUPLICATES_SAME_SIZE 0x400000
319.
320.  {* This value for SEC_LINK_DUPLICATES means that the linker
321.     should warn if any duplicate sections contain different
322.     contents.  *}
323.#define SEC_LINK_DUPLICATES_SAME_CONTENTS 0x600000
324.
325.  {* This section was created by the linker as part of dynamic
326.     relocation or other arcane processing.  It is skipped when
327.     going through the first-pass output, trusting that someone
328.     else up the line will take care of it later.  *}
329.#define SEC_LINKER_CREATED 0x800000
330.
331.  {* This section should not be subject to garbage collection.  *}
332.#define SEC_KEEP 0x1000000
333.
334.  {* This section contains "short" data, and should be placed
335.     "near" the GP.  *}
336.#define SEC_SMALL_DATA 0x2000000
337.
338.  {* This section contains data which may be shared with other
339.     executables or shared objects.  *}
340.#define SEC_SHARED 0x4000000
341.
342.  {* When a section with this flag is being linked, then if the size of
343.     the input section is less than a page, it should not cross a page
344.     boundary.  If the size of the input section is one page or more, it
345.     should be aligned on a page boundary.  *}
346.#define SEC_BLOCK 0x8000000
347.
348.  {* Conditionally link this section; do not link if there are no
349.     references found to any symbol in the section.  *}
350.#define SEC_CLINK 0x10000000
351.
352.  {*  End of section flags.  *}
353.
354.  {* Some internal packed boolean fields.  *}
355.
356.  {* See the vma field.  *}
357.  unsigned int user_set_vma : 1;
358.
359.  {* Whether relocations have been processed.  *}
360.  unsigned int reloc_done : 1;
361.
362.  {* A mark flag used by some of the linker backends.  *}
363.  unsigned int linker_mark : 1;
364.
365.  {* A mark flag used by some linker backends for garbage collection.  *}
366.  unsigned int gc_mark : 1;
367.
368.  {* Used by the ELF code to mark sections which have been allocated to segments.  *}
369.  unsigned int segment_mark : 1;
370.
371.  {* End of internal packed boolean fields.  *}
372.
373.  {*  The virtual memory address of the section - where it will be
374.      at run time.  The symbols are relocated against this.  The
375.      user_set_vma flag is maintained by bfd; if it's not set, the
376.      backend can assign addresses (for example, in <<a.out>>, where
377.      the default address for <<.data>> is dependent on the specific
378.      target and various flags).  *}
379.
380.  bfd_vma vma;
381.
382.  {*  The load address of the section - where it would be in a
383.      rom image; really only used for writing section header
384.      information. *}
385.
386.  bfd_vma lma;
387.
388.  {* The size of the section in octets, as it will be output.
389.     Contains a value even if the section has no contents (e.g., the
390.     size of <<.bss>>).  This will be filled in after relocation.  *}
391.
392.  bfd_size_type _cooked_size;
393.
394.  {* The original size on disk of the section, in octets.  Normally this
395.     value is the same as the size, but if some relaxing has
396.     been done, then this value will be bigger.  *}
397.
398.  bfd_size_type _raw_size;
399.
400.  {* If this section is going to be output, then this value is the
401.     offset in *bytes* into the output section of the first byte in the
402.     input section (byte ==> smallest addressable unit on the
403.     target).  In most cases, if this was going to start at the
404.     100th octet (8-bit quantity) in the output section, this value
405.     would be 100.  However, if the target byte size is 16 bits
406.     (bfd_octets_per_byte is "2"), this value would be 50.  *}
407.
408.  bfd_vma output_offset;
409.
410.  {* The output section through which to map on output.  *}
411.
412.  struct sec *output_section;
413.
414.  {* The alignment requirement of the section, as an exponent of 2 -
415.     e.g., 3 aligns to 2^3 (or 8).  *}
416.
417.  unsigned int alignment_power;
418.
419.  {* If an input section, a pointer to a vector of relocation
420.     records for the data in this section.  *}
421.
422.  struct reloc_cache_entry *relocation;
423.
424.  {* If an output section, a pointer to a vector of pointers to
425.     relocation records for the data in this section.  *}
426.
427.  struct reloc_cache_entry **orelocation;
428.
429.  {* The number of relocation records in one of the above  *}
430.
431.  unsigned reloc_count;
432.
433.  {* Information below is back end specific - and not always used
434.     or updated.  *}
435.
436.  {* File position of section data.  *}
437.
438.  file_ptr filepos;
439.
440.  {* File position of relocation info.  *}
441.
442.  file_ptr rel_filepos;
443.
444.  {* File position of line data.  *}
445.
446.  file_ptr line_filepos;
447.
448.  {* Pointer to data for applications.  *}
449.
450.  PTR userdata;
451.
452.  {* If the SEC_IN_MEMORY flag is set, this points to the actual
453.     contents.  *}
454.  unsigned char *contents;
455.
456.  {* Attached line number information.  *}
457.
458.  alent *lineno;
459.
460.  {* Number of line number records.  *}
461.
462.  unsigned int lineno_count;
463.
464.  {* Optional information about a COMDAT entry; NULL if not COMDAT.  *}
465.
466.  struct bfd_comdat_info *comdat;
467.
468.  {* Points to the kept section if this section is a link-once section,
469.     and is discarded.  *}
470.  struct sec *kept_section;
471.
472.  {* When a section is being output, this value changes as more
473.     linenumbers are written out.  *}
474.
475.  file_ptr moving_line_filepos;
476.
477.  {* What the section number is in the target world.  *}
478.
479.  int target_index;
480.
481.  PTR used_by_bfd;
482.
483.  {* If this is a constructor section then here is a list of the
484.     relocations created to relocate items within it.  *}
485.
486.  struct relent_chain *constructor_chain;
487.
488.  {* The BFD which owns the section.  *}
489.
490.  bfd *owner;
491.
492.  {* A symbol which points at this section only *}
493.  struct symbol_cache_entry *symbol;
494.  struct symbol_cache_entry **symbol_ptr_ptr;
495.
496.  struct bfd_link_order *link_order_head;
497.  struct bfd_link_order *link_order_tail;
498.} asection ;
499.
500.{* These sections are global, and are managed by BFD.  The application
501.   and target back end are not permitted to change the values in
502.   these sections.  New code should use the section_ptr macros rather
503.   than referring directly to the const sections.  The const sections
504.   may eventually vanish.  *}
505.#define BFD_ABS_SECTION_NAME "*ABS*"
506.#define BFD_UND_SECTION_NAME "*UND*"
507.#define BFD_COM_SECTION_NAME "*COM*"
508.#define BFD_IND_SECTION_NAME "*IND*"
509.
510.{* the absolute section *}
511.extern const asection bfd_abs_section;
512.#define bfd_abs_section_ptr ((asection *) &bfd_abs_section)
513.#define bfd_is_abs_section(sec) ((sec) == bfd_abs_section_ptr)
514.{* Pointer to the undefined section *}
515.extern const asection bfd_und_section;
516.#define bfd_und_section_ptr ((asection *) &bfd_und_section)
517.#define bfd_is_und_section(sec) ((sec) == bfd_und_section_ptr)
518.{* Pointer to the common section *}
519.extern const asection bfd_com_section;
520.#define bfd_com_section_ptr ((asection *) &bfd_com_section)
521.{* Pointer to the indirect section *}
522.extern const asection bfd_ind_section;
523.#define bfd_ind_section_ptr ((asection *) &bfd_ind_section)
524.#define bfd_is_ind_section(sec) ((sec) == bfd_ind_section_ptr)
525.
526.extern const struct symbol_cache_entry * const bfd_abs_symbol;
527.extern const struct symbol_cache_entry * const bfd_com_symbol;
528.extern const struct symbol_cache_entry * const bfd_und_symbol;
529.extern const struct symbol_cache_entry * const bfd_ind_symbol;
530.#define bfd_get_section_size_before_reloc(section) \
531.     ((section)->reloc_done ? (abort (), (bfd_size_type) 1) \
532.                            : (section)->_raw_size)
533.#define bfd_get_section_size_after_reloc(section) \
534.     ((section)->reloc_done ? (section)->_cooked_size \
535.                            : (abort (), (bfd_size_type) 1))
536*/
537
538/* We use a macro to initialize the static asymbol structures because
539   traditional C does not permit us to initialize a union member while
540   gcc warns if we don't initialize it.  */
541 /* the_bfd, name, value, attr, section [, udata] */
542#ifdef __STDC__
543#define GLOBAL_SYM_INIT(NAME, SECTION) \
544  { 0, NAME, 0, BSF_SECTION_SYM, (asection *) SECTION, { 0 }}
545#else
546#define GLOBAL_SYM_INIT(NAME, SECTION) \
547  { 0, NAME, 0, BSF_SECTION_SYM, (asection *) SECTION }
548#endif
549
550/* These symbols are global, not specific to any BFD.  Therefore, anything
551   that tries to change them is broken, and should be repaired.  */
552
553static const asymbol global_syms[] =
554{
555  GLOBAL_SYM_INIT (BFD_COM_SECTION_NAME, &bfd_com_section),
556  GLOBAL_SYM_INIT (BFD_UND_SECTION_NAME, &bfd_und_section),
557  GLOBAL_SYM_INIT (BFD_ABS_SECTION_NAME, &bfd_abs_section),
558  GLOBAL_SYM_INIT (BFD_IND_SECTION_NAME, &bfd_ind_section)
559};
560
561#define STD_SECTION(SEC, FLAGS, SYM, NAME, IDX)				\
562  const asymbol * const SYM = (asymbol *) &global_syms[IDX]; 		\
563  const asection SEC = 							\
564    /* name, id,  index, next, flags, user_set_vma, reloc_done,      */	\
565    { NAME,  IDX, 0,     NULL, FLAGS, 0,            0,			\
566									\
567    /* linker_mark, gc_mark, segment_mark, vma, lma, _cooked_size,   */	\
568       0,           1,       0,            0,   0,   0,            	\
569									\
570    /* _raw_size, output_offset, output_section,    alignment_power, */ \
571       0,         0,           (struct sec *) &SEC, 0,			\
572									\
573    /* relocation, orelocation, reloc_count, filepos, rel_filepos,   */	\
574       NULL,       NULL,        0,           0,       0,		\
575									\
576    /* line_filepos, userdata, contents, lineno, lineno_count,       */	\
577       0,            NULL,     NULL,     NULL,   0,			\
578									\
579    /* comdat, kept_section, moving_line_filepos, target_index,      */	\
580       NULL,   NULL,         0,                   0,			\
581									\
582    /* used_by_bfd, constructor_chain, owner,                        */	\
583       NULL,        NULL,              NULL,				\
584									\
585    /* symbol,                                                       */	\
586       (struct symbol_cache_entry *) &global_syms[IDX],			\
587									\
588    /* symbol_ptr_ptr,                                               */	\
589       (struct symbol_cache_entry **) &SYM,				\
590									\
591    /* link_order_head, link_order_tail                              */	\
592       NULL,            NULL						\
593    }
594
595STD_SECTION (bfd_com_section, SEC_IS_COMMON, bfd_com_symbol,
596	     BFD_COM_SECTION_NAME, 0);
597STD_SECTION (bfd_und_section, 0, bfd_und_symbol, BFD_UND_SECTION_NAME, 1);
598STD_SECTION (bfd_abs_section, 0, bfd_abs_symbol, BFD_ABS_SECTION_NAME, 2);
599STD_SECTION (bfd_ind_section, 0, bfd_ind_symbol, BFD_IND_SECTION_NAME, 3);
600#undef STD_SECTION
601
602/*
603DOCDD
604INODE
605section prototypes,  , typedef asection, Sections
606SUBSECTION
607	Section prototypes
608
609These are the functions exported by the section handling part of BFD.
610*/
611
612/*
613FUNCTION
614	bfd_get_section_by_name
615
616SYNOPSIS
617	asection *bfd_get_section_by_name(bfd *abfd, const char *name);
618
619DESCRIPTION
620	Run through @var{abfd} and return the one of the
621	<<asection>>s whose name matches @var{name}, otherwise <<NULL>>.
622	@xref{Sections}, for more information.
623
624	This should only be used in special cases; the normal way to process
625	all sections of a given name is to use <<bfd_map_over_sections>> and
626	<<strcmp>> on the name (or better yet, base it on the section flags
627	or something else) for each section.
628*/
629
630asection *
631bfd_get_section_by_name (abfd, name)
632     bfd *abfd;
633     const char *name;
634{
635  asection *sect;
636
637  for (sect = abfd->sections; sect != NULL; sect = sect->next)
638    if (!strcmp (sect->name, name))
639      return sect;
640  return NULL;
641}
642
643/*
644FUNCTION
645	bfd_get_unique_section_name
646
647SYNOPSIS
648	char *bfd_get_unique_section_name(bfd *abfd,
649					  const char *templat,
650					  int *count);
651
652DESCRIPTION
653	Invent a section name that is unique in @var{abfd} by tacking
654	a dot and a digit suffix onto the original @var{templat}.  If
655	@var{count} is non-NULL, then it specifies the first number
656	tried as a suffix to generate a unique name.  The value
657	pointed to by @var{count} will be incremented in this case.
658*/
659
660char *
661bfd_get_unique_section_name (abfd, templat, count)
662     bfd *abfd;
663     const char *templat;
664     int *count;
665{
666  int num;
667  unsigned int len;
668  char *sname;
669
670  len = strlen (templat);
671  sname = bfd_malloc (len + 8);
672  if (sname == NULL)
673    return NULL;
674  strcpy (sname, templat);
675  num = 1;
676  if (count != NULL)
677    num = *count;
678
679  do
680    {
681      /* If we have a million sections, something is badly wrong.  */
682      if (num > 999999)
683	abort ();
684      sprintf (sname + len, ".%d", num++);
685    }
686  while (bfd_get_section_by_name (abfd, sname) != NULL);
687
688  if (count != NULL)
689    *count = num;
690  return sname;
691}
692
693/*
694FUNCTION
695	bfd_make_section_old_way
696
697SYNOPSIS
698	asection *bfd_make_section_old_way(bfd *abfd, const char *name);
699
700DESCRIPTION
701	Create a new empty section called @var{name}
702	and attach it to the end of the chain of sections for the
703	BFD @var{abfd}. An attempt to create a section with a name which
704	is already in use returns its pointer without changing the
705	section chain.
706
707	It has the funny name since this is the way it used to be
708	before it was rewritten....
709
710	Possible errors are:
711	o <<bfd_error_invalid_operation>> -
712	If output has already started for this BFD.
713	o <<bfd_error_no_memory>> -
714	If memory allocation fails.
715
716*/
717
718asection *
719bfd_make_section_old_way (abfd, name)
720     bfd *abfd;
721     const char *name;
722{
723  asection *sec = bfd_get_section_by_name (abfd, name);
724  if (sec == (asection *) NULL)
725    {
726      sec = bfd_make_section (abfd, name);
727    }
728  return sec;
729}
730
731/*
732FUNCTION
733	bfd_make_section_anyway
734
735SYNOPSIS
736	asection *bfd_make_section_anyway(bfd *abfd, const char *name);
737
738DESCRIPTION
739   Create a new empty section called @var{name} and attach it to the end of
740   the chain of sections for @var{abfd}.  Create a new section even if there
741   is already a section with that name.
742
743   Return <<NULL>> and set <<bfd_error>> on error; possible errors are:
744   o <<bfd_error_invalid_operation>> - If output has already started for @var{abfd}.
745   o <<bfd_error_no_memory>> - If memory allocation fails.
746*/
747
748sec_ptr
749bfd_make_section_anyway (abfd, name)
750     bfd *abfd;
751     const char *name;
752{
753  static int section_id = 0x10;  /* id 0 to 3 used by STD_SECTION.  */
754  asection *newsect;
755  asection **prev = &abfd->sections;
756  asection *sect = abfd->sections;
757
758  if (abfd->output_has_begun)
759    {
760      bfd_set_error (bfd_error_invalid_operation);
761      return NULL;
762    }
763
764  while (sect)
765    {
766      prev = &sect->next;
767      sect = sect->next;
768    }
769
770  newsect = (asection *) bfd_zalloc (abfd, sizeof (asection));
771  if (newsect == NULL)
772    return NULL;
773
774  newsect->name = name;
775  newsect->id = section_id++;
776  newsect->index = abfd->section_count++;
777  newsect->flags = SEC_NO_FLAGS;
778
779  newsect->userdata = NULL;
780  newsect->contents = NULL;
781  newsect->next = (asection *) NULL;
782  newsect->relocation = (arelent *) NULL;
783  newsect->reloc_count = 0;
784  newsect->line_filepos = 0;
785  newsect->owner = abfd;
786  newsect->comdat = NULL;
787  newsect->kept_section = NULL;
788
789  /* Create a symbol whos only job is to point to this section. This is
790     useful for things like relocs which are relative to the base of a
791     section.  */
792  newsect->symbol = bfd_make_empty_symbol (abfd);
793  if (newsect->symbol == NULL)
794    {
795      bfd_release (abfd, newsect);
796      return NULL;
797    }
798  newsect->symbol->name = name;
799  newsect->symbol->value = 0;
800  newsect->symbol->section = newsect;
801  newsect->symbol->flags = BSF_SECTION_SYM;
802
803  newsect->symbol_ptr_ptr = &newsect->symbol;
804
805  if (BFD_SEND (abfd, _new_section_hook, (abfd, newsect)) != true)
806    {
807      bfd_release (abfd, newsect);
808      return NULL;
809    }
810
811  *prev = newsect;
812  return newsect;
813}
814
815/*
816FUNCTION
817	bfd_make_section
818
819SYNOPSIS
820	asection *bfd_make_section(bfd *, const char *name);
821
822DESCRIPTION
823   Like <<bfd_make_section_anyway>>, but return <<NULL>> (without calling
824   bfd_set_error ()) without changing the section chain if there is already a
825   section named @var{name}.  If there is an error, return <<NULL>> and set
826   <<bfd_error>>.
827*/
828
829asection *
830bfd_make_section (abfd, name)
831     bfd *abfd;
832     const char *name;
833{
834  asection *sect = abfd->sections;
835
836  if (strcmp (name, BFD_ABS_SECTION_NAME) == 0)
837    {
838      return bfd_abs_section_ptr;
839    }
840  if (strcmp (name, BFD_COM_SECTION_NAME) == 0)
841    {
842      return bfd_com_section_ptr;
843    }
844  if (strcmp (name, BFD_UND_SECTION_NAME) == 0)
845    {
846      return bfd_und_section_ptr;
847    }
848
849  if (strcmp (name, BFD_IND_SECTION_NAME) == 0)
850    {
851      return bfd_ind_section_ptr;
852    }
853
854  while (sect)
855    {
856      if (!strcmp (sect->name, name))
857	return NULL;
858      sect = sect->next;
859    }
860
861  /* The name is not already used; go ahead and make a new section.  */
862  return bfd_make_section_anyway (abfd, name);
863}
864
865/*
866FUNCTION
867	bfd_set_section_flags
868
869SYNOPSIS
870	boolean bfd_set_section_flags(bfd *abfd, asection *sec, flagword flags);
871
872DESCRIPTION
873	Set the attributes of the section @var{sec} in the BFD
874	@var{abfd} to the value @var{flags}. Return <<true>> on success,
875	<<false>> on error. Possible error returns are:
876
877	o <<bfd_error_invalid_operation>> -
878	The section cannot have one or more of the attributes
879	requested. For example, a .bss section in <<a.out>> may not
880	have the <<SEC_HAS_CONTENTS>> field set.
881
882*/
883
884/*ARGSUSED*/
885boolean
886bfd_set_section_flags (abfd, section, flags)
887     bfd *abfd ATTRIBUTE_UNUSED;
888     sec_ptr section;
889     flagword flags;
890{
891#if 0
892  /* If you try to copy a text section from an input file (where it
893     has the SEC_CODE flag set) to an output file, this loses big if
894     the bfd_applicable_section_flags (abfd) doesn't have the SEC_CODE
895     set - which it doesn't, at least not for a.out.  FIXME */
896
897  if ((flags & bfd_applicable_section_flags (abfd)) != flags)
898    {
899      bfd_set_error (bfd_error_invalid_operation);
900      return false;
901    }
902#endif
903
904  section->flags = flags;
905  return true;
906}
907
908/*
909FUNCTION
910	bfd_map_over_sections
911
912SYNOPSIS
913	void bfd_map_over_sections(bfd *abfd,
914				   void (*func) (bfd *abfd,
915						asection *sect,
916						PTR obj),
917				   PTR obj);
918
919DESCRIPTION
920	Call the provided function @var{func} for each section
921	attached to the BFD @var{abfd}, passing @var{obj} as an
922	argument. The function will be called as if by
923
924|	func(abfd, the_section, obj);
925
926	This is the prefered method for iterating over sections; an
927	alternative would be to use a loop:
928
929|	   section *p;
930|	   for (p = abfd->sections; p != NULL; p = p->next)
931|	      func(abfd, p, ...)
932
933*/
934
935/*VARARGS2*/
936void
937bfd_map_over_sections (abfd, operation, user_storage)
938     bfd *abfd;
939     void (*operation) PARAMS ((bfd * abfd, asection * sect, PTR obj));
940     PTR user_storage;
941{
942  asection *sect;
943  unsigned int i = 0;
944
945  for (sect = abfd->sections; sect != NULL; i++, sect = sect->next)
946    (*operation) (abfd, sect, user_storage);
947
948  if (i != abfd->section_count)	/* Debugging */
949    abort ();
950}
951
952/*
953FUNCTION
954	bfd_set_section_size
955
956SYNOPSIS
957	boolean bfd_set_section_size(bfd *abfd, asection *sec, bfd_size_type val);
958
959DESCRIPTION
960	Set @var{sec} to the size @var{val}. If the operation is
961	ok, then <<true>> is returned, else <<false>>.
962
963	Possible error returns:
964	o <<bfd_error_invalid_operation>> -
965	Writing has started to the BFD, so setting the size is invalid.
966
967*/
968
969boolean
970bfd_set_section_size (abfd, ptr, val)
971     bfd *abfd;
972     sec_ptr ptr;
973     bfd_size_type val;
974{
975  /* Once you've started writing to any section you cannot create or change
976     the size of any others.  */
977
978  if (abfd->output_has_begun)
979    {
980      bfd_set_error (bfd_error_invalid_operation);
981      return false;
982    }
983
984  ptr->_cooked_size = val;
985  ptr->_raw_size = val;
986
987  return true;
988}
989
990/*
991FUNCTION
992	bfd_set_section_contents
993
994SYNOPSIS
995	boolean bfd_set_section_contents
996         (bfd *abfd,
997         asection *section,
998         PTR data,
999         file_ptr offset,
1000         bfd_size_type count);
1001
1002DESCRIPTION
1003	Sets the contents of the section @var{section} in BFD
1004	@var{abfd} to the data starting in memory at @var{data}. The
1005	data is written to the output section starting at offset
1006	@var{offset} for @var{count} octets.
1007
1008	Normally <<true>> is returned, else <<false>>. Possible error
1009	returns are:
1010	o <<bfd_error_no_contents>> -
1011	The output section does not have the <<SEC_HAS_CONTENTS>>
1012	attribute, so nothing can be written to it.
1013	o and some more too
1014
1015	This routine is front end to the back end function
1016	<<_bfd_set_section_contents>>.
1017
1018*/
1019
1020#define bfd_get_section_size_now(abfd,sec) \
1021(sec->reloc_done \
1022 ? bfd_get_section_size_after_reloc (sec) \
1023 : bfd_get_section_size_before_reloc (sec))
1024
1025boolean
1026bfd_set_section_contents (abfd, section, location, offset, count)
1027     bfd *abfd;
1028     sec_ptr section;
1029     PTR location;
1030     file_ptr offset;
1031     bfd_size_type count;
1032{
1033  bfd_size_type sz;
1034
1035  if (!(bfd_get_section_flags (abfd, section) & SEC_HAS_CONTENTS))
1036    {
1037      bfd_set_error (bfd_error_no_contents);
1038      return (false);
1039    }
1040
1041  if (offset < 0)
1042    {
1043    bad_val:
1044      bfd_set_error (bfd_error_bad_value);
1045      return false;
1046    }
1047  sz = bfd_get_section_size_now (abfd, section);
1048  if ((bfd_size_type) offset > sz
1049      || count > sz
1050      || offset + count > sz)
1051    goto bad_val;
1052
1053  switch (abfd->direction)
1054    {
1055    case read_direction:
1056    case no_direction:
1057      bfd_set_error (bfd_error_invalid_operation);
1058      return false;
1059
1060    case write_direction:
1061      break;
1062
1063    case both_direction:
1064      /* File is opened for update. `output_has_begun' some time ago when
1065	   the file was created.  Do not recompute sections sizes or alignments
1066	   in _bfd_set_section_content.  */
1067      abfd->output_has_begun = true;
1068      break;
1069    }
1070
1071  /* Record a copy of the data in memory if desired.  */
1072  if (section->contents
1073      && location != section->contents + offset)
1074    memcpy (section->contents + offset, location, count);
1075
1076  if (BFD_SEND (abfd, _bfd_set_section_contents,
1077		(abfd, section, location, offset, count)))
1078    {
1079      abfd->output_has_begun = true;
1080      return true;
1081    }
1082
1083  return false;
1084}
1085
1086/*
1087FUNCTION
1088	bfd_get_section_contents
1089
1090SYNOPSIS
1091	boolean bfd_get_section_contents
1092        (bfd *abfd, asection *section, PTR location,
1093         file_ptr offset, bfd_size_type count);
1094
1095DESCRIPTION
1096	Read data from @var{section} in BFD @var{abfd}
1097	into memory starting at @var{location}. The data is read at an
1098	offset of @var{offset} from the start of the input section,
1099	and is read for @var{count} bytes.
1100
1101	If the contents of a constructor with the <<SEC_CONSTRUCTOR>>
1102	flag set are requested or if the section does not have the
1103	<<SEC_HAS_CONTENTS>> flag set, then the @var{location} is filled
1104	with zeroes. If no errors occur, <<true>> is returned, else
1105	<<false>>.
1106
1107*/
1108boolean
1109bfd_get_section_contents (abfd, section, location, offset, count)
1110     bfd *abfd;
1111     sec_ptr section;
1112     PTR location;
1113     file_ptr offset;
1114     bfd_size_type count;
1115{
1116  bfd_size_type sz;
1117
1118  if (section->flags & SEC_CONSTRUCTOR)
1119    {
1120      memset (location, 0, (unsigned) count);
1121      return true;
1122    }
1123
1124  if (offset < 0)
1125    {
1126    bad_val:
1127      bfd_set_error (bfd_error_bad_value);
1128      return false;
1129    }
1130  /* Even if reloc_done is true, this function reads unrelocated
1131     contents, so we want the raw size.  */
1132  sz = section->_raw_size;
1133  if ((bfd_size_type) offset > sz || count > sz || offset + count > sz)
1134    goto bad_val;
1135
1136  if (count == 0)
1137    /* Don't bother.  */
1138    return true;
1139
1140  if ((section->flags & SEC_HAS_CONTENTS) == 0)
1141    {
1142      memset (location, 0, (unsigned) count);
1143      return true;
1144    }
1145
1146  if ((section->flags & SEC_IN_MEMORY) != 0)
1147    {
1148      memcpy (location, section->contents + offset, (size_t) count);
1149      return true;
1150    }
1151
1152  return BFD_SEND (abfd, _bfd_get_section_contents,
1153		   (abfd, section, location, offset, count));
1154}
1155
1156/*
1157FUNCTION
1158	bfd_copy_private_section_data
1159
1160SYNOPSIS
1161	boolean bfd_copy_private_section_data(bfd *ibfd, asection *isec, bfd *obfd, asection *osec);
1162
1163DESCRIPTION
1164	Copy private section information from @var{isec} in the BFD
1165	@var{ibfd} to the section @var{osec} in the BFD @var{obfd}.
1166	Return <<true>> on success, <<false>> on error.  Possible error
1167	returns are:
1168
1169	o <<bfd_error_no_memory>> -
1170	Not enough memory exists to create private data for @var{osec}.
1171
1172.#define bfd_copy_private_section_data(ibfd, isection, obfd, osection) \
1173.     BFD_SEND (obfd, _bfd_copy_private_section_data, \
1174.		(ibfd, isection, obfd, osection))
1175*/
1176
1177/*
1178FUNCTION
1179	_bfd_strip_section_from_output
1180
1181SYNOPSIS
1182	void _bfd_strip_section_from_output
1183	(struct bfd_link_info *info, asection *section);
1184
1185DESCRIPTION
1186	Remove @var{section} from the output.  If the output section
1187	becomes empty, remove it from the output bfd.  @var{info} may
1188	be NULL; if it is not, it is used to decide whether the output
1189	section is empty.
1190*/
1191void
1192_bfd_strip_section_from_output (info, s)
1193     struct bfd_link_info *info;
1194     asection *s;
1195{
1196  asection **spp, *os;
1197  struct bfd_link_order *p, *pp;
1198  boolean keep_os;
1199
1200  /* Excise the input section from the link order.
1201
1202     FIXME: For all calls that I can see to this function, the link
1203     orders have not yet been set up.  So why are we checking them? --
1204     Ian */
1205  os = s->output_section;
1206  for (p = os->link_order_head, pp = NULL; p != NULL; pp = p, p = p->next)
1207    if (p->type == bfd_indirect_link_order
1208	&& p->u.indirect.section == s)
1209      {
1210	if (pp)
1211	  pp->next = p->next;
1212	else
1213	  os->link_order_head = p->next;
1214	if (!p->next)
1215	  os->link_order_tail = pp;
1216	break;
1217      }
1218
1219  keep_os = os->link_order_head != NULL;
1220
1221  if (! keep_os && info != NULL)
1222    {
1223      bfd *abfd;
1224      for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
1225	{
1226	  asection *is;
1227	  for (is = abfd->sections; is != NULL; is = is->next)
1228	    {
1229	      if (is != s && is->output_section == os)
1230		break;
1231	    }
1232	  if (is != NULL)
1233	    break;
1234	}
1235      if (abfd != NULL)
1236	keep_os = true;
1237    }
1238
1239  /* If the output section is empty, remove it too.  Careful about sections
1240     that have been discarded in the link script -- they are mapped to
1241     bfd_abs_section, which has no owner.  */
1242  if (!keep_os && os->owner != NULL)
1243    {
1244      for (spp = &os->owner->sections; *spp; spp = &(*spp)->next)
1245	if (*spp == os)
1246	  {
1247	    *spp = os->next;
1248	    os->owner->section_count--;
1249	    break;
1250	  }
1251    }
1252}
1253