2001-03-22 Philip Blundell <philb@gnu.org>
[binutils.git] / bfd / section.c
blob13e10d30f67536aff91177671dbe9f41d30cc2f2
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.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 SECTION
24 Sections
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.
31 Sections are supported in BFD in <<section.c>>.
33 @menu
34 @* Section Input::
35 @* Section Output::
36 @* typedef asection::
37 @* section prototypes::
38 @end menu
40 INODE
41 Section Input, Section Output, Sections, Sections
42 SUBSECTION
43 Section input
45 When a BFD is opened for reading, the section structures are
46 created and attached to the BFD.
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>>.
52 Names need not be unique; for example a COFF file may have several
53 sections named <<.data>>.
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.
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.
73 INODE
74 Section Output, typedef asection, Section Input, Sections
76 SUBSECTION
77 Section output
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>>.
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.)
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.
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:
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 --------|
114 SUBSECTION
115 Link orders
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.
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.
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.
135 #include "bfd.h"
136 #include "sysdep.h"
137 #include "libbfd.h"
138 #include "bfdlink.h"
141 DOCDD
142 INODE
143 typedef asection, section prototypes, Section Output, Sections
144 SUBSECTION
145 typedef asection
147 Here is the section structure:
149 CODE_FRAGMENT
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. *}
156 .struct bfd_comdat_info
158 . {* The name of the symbol associated with a comdat section. *}
159 . const char *name;
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;
168 .typedef struct sec
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. *}
173 . const char *name;
175 . {* A unique sequence number. *}
177 . int id;
179 . {* Which section is it; 0..nth. *}
181 . int index;
183 . {* The next section in the list belonging to the BFD, or NULL. *}
185 . struct sec *next;
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. *}
191 . flagword flags;
193 .#define SEC_NO_FLAGS 0x000
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
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
203 . {* The section contains data still to be relocated, so there is
204 . some relocation information too. *}
205 .#define SEC_RELOC 0x004
207 .#if 0 {* Obsolete ? *}
208 .#define SEC_BALIGN 0x008
209 .#endif
211 . {* A signal to the OS that the section contains read only data. *}
212 .#define SEC_READONLY 0x010
214 . {* The section contains code only. *}
215 .#define SEC_CODE 0x020
217 . {* The section contains data only. *}
218 .#define SEC_DATA 0x040
220 . {* The section will reside in ROM. *}
221 .#define SEC_ROM 0x080
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
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
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
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
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
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
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
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
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
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
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
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
303 . {* If SEC_LINK_ONCE is set, this bitfield describes how the linker
304 . should handle duplicate sections. *}
305 .#define SEC_LINK_DUPLICATES 0x600000
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
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
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
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
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
331 . {* This section should not be subject to garbage collection. *}
332 .#define SEC_KEEP 0x1000000
334 . {* This section contains "short" data, and should be placed
335 . "near" the GP. *}
336 .#define SEC_SMALL_DATA 0x2000000
338 . {* This section contains data which may be shared with other
339 . executables or shared objects. *}
340 .#define SEC_SHARED 0x4000000
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
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
352 . {* End of section flags. *}
354 . {* Some internal packed boolean fields. *}
356 . {* See the vma field. *}
357 . unsigned int user_set_vma : 1;
359 . {* Whether relocations have been processed. *}
360 . unsigned int reloc_done : 1;
362 . {* A mark flag used by some of the linker backends. *}
363 . unsigned int linker_mark : 1;
365 . {* A mark flag used by some linker backends for garbage collection. *}
366 . unsigned int gc_mark : 1;
368 . {* Used by the ELF code to mark sections which have been allocated to segments. *}
369 . unsigned int segment_mark : 1;
371 . {* End of internal packed boolean fields. *}
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). *}
380 . bfd_vma vma;
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. *}
386 . bfd_vma lma;
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. *}
392 . bfd_size_type _cooked_size;
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. *}
398 . bfd_size_type _raw_size;
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. *}
408 . bfd_vma output_offset;
410 . {* The output section through which to map on output. *}
412 . struct sec *output_section;
414 . {* The alignment requirement of the section, as an exponent of 2 -
415 . e.g., 3 aligns to 2^3 (or 8). *}
417 . unsigned int alignment_power;
419 . {* If an input section, a pointer to a vector of relocation
420 . records for the data in this section. *}
422 . struct reloc_cache_entry *relocation;
424 . {* If an output section, a pointer to a vector of pointers to
425 . relocation records for the data in this section. *}
427 . struct reloc_cache_entry **orelocation;
429 . {* The number of relocation records in one of the above *}
431 . unsigned reloc_count;
433 . {* Information below is back end specific - and not always used
434 . or updated. *}
436 . {* File position of section data. *}
438 . file_ptr filepos;
440 . {* File position of relocation info. *}
442 . file_ptr rel_filepos;
444 . {* File position of line data. *}
446 . file_ptr line_filepos;
448 . {* Pointer to data for applications. *}
450 . PTR userdata;
452 . {* If the SEC_IN_MEMORY flag is set, this points to the actual
453 . contents. *}
454 . unsigned char *contents;
456 . {* Attached line number information. *}
458 . alent *lineno;
460 . {* Number of line number records. *}
462 . unsigned int lineno_count;
464 . {* Optional information about a COMDAT entry; NULL if not COMDAT. *}
466 . struct bfd_comdat_info *comdat;
468 . {* Points to the kept section if this section is a link-once section,
469 . and is discarded. *}
470 . struct sec *kept_section;
472 . {* When a section is being output, this value changes as more
473 . linenumbers are written out. *}
475 . file_ptr moving_line_filepos;
477 . {* What the section number is in the target world. *}
479 . int target_index;
481 . PTR used_by_bfd;
483 . {* If this is a constructor section then here is a list of the
484 . relocations created to relocate items within it. *}
486 . struct relent_chain *constructor_chain;
488 . {* The BFD which owns the section. *}
490 . bfd *owner;
492 . {* A symbol which points at this section only *}
493 . struct symbol_cache_entry *symbol;
494 . struct symbol_cache_entry **symbol_ptr_ptr;
496 . struct bfd_link_order *link_order_head;
497 . struct bfd_link_order *link_order_tail;
498 .} asection ;
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*"
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)
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))
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
550 /* These symbols are global, not specific to any BFD. Therefore, anything
551 that tries to change them is broken, and should be repaired. */
553 static const asymbol global_syms[] =
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)
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, \
567 /* linker_mark, gc_mark, segment_mark, vma, lma, _cooked_size, */ \
568 0, 1, 0, 0, 0, 0, \
570 /* _raw_size, output_offset, output_section, alignment_power, */ \
571 0, 0, (struct sec *) &SEC, 0, \
573 /* relocation, orelocation, reloc_count, filepos, rel_filepos, */ \
574 NULL, NULL, 0, 0, 0, \
576 /* line_filepos, userdata, contents, lineno, lineno_count, */ \
577 0, NULL, NULL, NULL, 0, \
579 /* comdat, kept_section, moving_line_filepos, target_index, */ \
580 NULL, NULL, 0, 0, \
582 /* used_by_bfd, constructor_chain, owner, */ \
583 NULL, NULL, NULL, \
585 /* symbol, */ \
586 (struct symbol_cache_entry *) &global_syms[IDX], \
588 /* symbol_ptr_ptr, */ \
589 (struct symbol_cache_entry **) &SYM, \
591 /* link_order_head, link_order_tail */ \
592 NULL, NULL \
595 STD_SECTION (bfd_com_section, SEC_IS_COMMON, bfd_com_symbol,
596 BFD_COM_SECTION_NAME, 0);
597 STD_SECTION (bfd_und_section, 0, bfd_und_symbol, BFD_UND_SECTION_NAME, 1);
598 STD_SECTION (bfd_abs_section, 0, bfd_abs_symbol, BFD_ABS_SECTION_NAME, 2);
599 STD_SECTION (bfd_ind_section, 0, bfd_ind_symbol, BFD_IND_SECTION_NAME, 3);
600 #undef STD_SECTION
603 DOCDD
604 INODE
605 section prototypes, , typedef asection, Sections
606 SUBSECTION
607 Section prototypes
609 These are the functions exported by the section handling part of BFD.
613 FUNCTION
614 bfd_get_section_by_name
616 SYNOPSIS
617 asection *bfd_get_section_by_name(bfd *abfd, const char *name);
619 DESCRIPTION
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.
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.
630 asection *
631 bfd_get_section_by_name (abfd, name)
632 bfd *abfd;
633 const char *name;
635 asection *sect;
637 for (sect = abfd->sections; sect != NULL; sect = sect->next)
638 if (!strcmp (sect->name, name))
639 return sect;
640 return NULL;
644 FUNCTION
645 bfd_get_unique_section_name
647 SYNOPSIS
648 char *bfd_get_unique_section_name(bfd *abfd,
649 const char *templat,
650 int *count);
652 DESCRIPTION
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.
660 char *
661 bfd_get_unique_section_name (abfd, templat, count)
662 bfd *abfd;
663 const char *templat;
664 int *count;
666 int num;
667 unsigned int len;
668 char *sname;
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;
681 /* If we have a million sections, something is badly wrong. */
682 if (num > 999999)
683 abort ();
684 sprintf (sname + len, ".%d", num++);
686 while (bfd_get_section_by_name (abfd, sname) != NULL);
688 if (count != NULL)
689 *count = num;
690 return sname;
694 FUNCTION
695 bfd_make_section_old_way
697 SYNOPSIS
698 asection *bfd_make_section_old_way(bfd *abfd, const char *name);
700 DESCRIPTION
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.
707 It has the funny name since this is the way it used to be
708 before it was rewritten....
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.
718 asection *
719 bfd_make_section_old_way (abfd, name)
720 bfd *abfd;
721 const char *name;
723 asection *sec = bfd_get_section_by_name (abfd, name);
724 if (sec == (asection *) NULL)
726 sec = bfd_make_section (abfd, name);
728 return sec;
732 FUNCTION
733 bfd_make_section_anyway
735 SYNOPSIS
736 asection *bfd_make_section_anyway(bfd *abfd, const char *name);
738 DESCRIPTION
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.
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.
748 sec_ptr
749 bfd_make_section_anyway (abfd, name)
750 bfd *abfd;
751 const char *name;
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;
758 if (abfd->output_has_begun)
760 bfd_set_error (bfd_error_invalid_operation);
761 return NULL;
764 while (sect)
766 prev = &sect->next;
767 sect = sect->next;
770 newsect = (asection *) bfd_zalloc (abfd, sizeof (asection));
771 if (newsect == NULL)
772 return NULL;
774 newsect->name = name;
775 newsect->id = section_id++;
776 newsect->index = abfd->section_count++;
777 newsect->flags = SEC_NO_FLAGS;
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;
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)
795 bfd_release (abfd, newsect);
796 return NULL;
798 newsect->symbol->name = name;
799 newsect->symbol->value = 0;
800 newsect->symbol->section = newsect;
801 newsect->symbol->flags = BSF_SECTION_SYM;
803 newsect->symbol_ptr_ptr = &newsect->symbol;
805 if (BFD_SEND (abfd, _new_section_hook, (abfd, newsect)) != true)
807 bfd_release (abfd, newsect);
808 return NULL;
811 *prev = newsect;
812 return newsect;
816 FUNCTION
817 bfd_make_section
819 SYNOPSIS
820 asection *bfd_make_section(bfd *, const char *name);
822 DESCRIPTION
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>>.
829 asection *
830 bfd_make_section (abfd, name)
831 bfd *abfd;
832 const char *name;
834 asection *sect = abfd->sections;
836 if (strcmp (name, BFD_ABS_SECTION_NAME) == 0)
838 return bfd_abs_section_ptr;
840 if (strcmp (name, BFD_COM_SECTION_NAME) == 0)
842 return bfd_com_section_ptr;
844 if (strcmp (name, BFD_UND_SECTION_NAME) == 0)
846 return bfd_und_section_ptr;
849 if (strcmp (name, BFD_IND_SECTION_NAME) == 0)
851 return bfd_ind_section_ptr;
854 while (sect)
856 if (!strcmp (sect->name, name))
857 return NULL;
858 sect = sect->next;
861 /* The name is not already used; go ahead and make a new section. */
862 return bfd_make_section_anyway (abfd, name);
866 FUNCTION
867 bfd_set_section_flags
869 SYNOPSIS
870 boolean bfd_set_section_flags(bfd *abfd, asection *sec, flagword flags);
872 DESCRIPTION
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:
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.
884 /*ARGSUSED*/
885 boolean
886 bfd_set_section_flags (abfd, section, flags)
887 bfd *abfd ATTRIBUTE_UNUSED;
888 sec_ptr section;
889 flagword flags;
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 */
897 if ((flags & bfd_applicable_section_flags (abfd)) != flags)
899 bfd_set_error (bfd_error_invalid_operation);
900 return false;
902 #endif
904 section->flags = flags;
905 return true;
909 FUNCTION
910 bfd_map_over_sections
912 SYNOPSIS
913 void bfd_map_over_sections(bfd *abfd,
914 void (*func) (bfd *abfd,
915 asection *sect,
916 PTR obj),
917 PTR obj);
919 DESCRIPTION
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
924 | func(abfd, the_section, obj);
926 This is the prefered method for iterating over sections; an
927 alternative would be to use a loop:
929 | section *p;
930 | for (p = abfd->sections; p != NULL; p = p->next)
931 | func(abfd, p, ...)
935 /*VARARGS2*/
936 void
937 bfd_map_over_sections (abfd, operation, user_storage)
938 bfd *abfd;
939 void (*operation) PARAMS ((bfd * abfd, asection * sect, PTR obj));
940 PTR user_storage;
942 asection *sect;
943 unsigned int i = 0;
945 for (sect = abfd->sections; sect != NULL; i++, sect = sect->next)
946 (*operation) (abfd, sect, user_storage);
948 if (i != abfd->section_count) /* Debugging */
949 abort ();
953 FUNCTION
954 bfd_set_section_size
956 SYNOPSIS
957 boolean bfd_set_section_size(bfd *abfd, asection *sec, bfd_size_type val);
959 DESCRIPTION
960 Set @var{sec} to the size @var{val}. If the operation is
961 ok, then <<true>> is returned, else <<false>>.
963 Possible error returns:
964 o <<bfd_error_invalid_operation>> -
965 Writing has started to the BFD, so setting the size is invalid.
969 boolean
970 bfd_set_section_size (abfd, ptr, val)
971 bfd *abfd;
972 sec_ptr ptr;
973 bfd_size_type val;
975 /* Once you've started writing to any section you cannot create or change
976 the size of any others. */
978 if (abfd->output_has_begun)
980 bfd_set_error (bfd_error_invalid_operation);
981 return false;
984 ptr->_cooked_size = val;
985 ptr->_raw_size = val;
987 return true;
991 FUNCTION
992 bfd_set_section_contents
994 SYNOPSIS
995 boolean bfd_set_section_contents
996 (bfd *abfd,
997 asection *section,
998 PTR data,
999 file_ptr offset,
1000 bfd_size_type count);
1002 DESCRIPTION
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.
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
1015 This routine is front end to the back end function
1016 <<_bfd_set_section_contents>>.
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))
1025 boolean
1026 bfd_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;
1033 bfd_size_type sz;
1035 if (!(bfd_get_section_flags (abfd, section) & SEC_HAS_CONTENTS))
1037 bfd_set_error (bfd_error_no_contents);
1038 return (false);
1041 if (offset < 0)
1043 bad_val:
1044 bfd_set_error (bfd_error_bad_value);
1045 return false;
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;
1053 switch (abfd->direction)
1055 case read_direction:
1056 case no_direction:
1057 bfd_set_error (bfd_error_invalid_operation);
1058 return false;
1060 case write_direction:
1061 break;
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;
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);
1076 if (BFD_SEND (abfd, _bfd_set_section_contents,
1077 (abfd, section, location, offset, count)))
1079 abfd->output_has_begun = true;
1080 return true;
1083 return false;
1087 FUNCTION
1088 bfd_get_section_contents
1090 SYNOPSIS
1091 boolean bfd_get_section_contents
1092 (bfd *abfd, asection *section, PTR location,
1093 file_ptr offset, bfd_size_type count);
1095 DESCRIPTION
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.
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>>.
1108 boolean
1109 bfd_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;
1116 bfd_size_type sz;
1118 if (section->flags & SEC_CONSTRUCTOR)
1120 memset (location, 0, (unsigned) count);
1121 return true;
1124 if (offset < 0)
1126 bad_val:
1127 bfd_set_error (bfd_error_bad_value);
1128 return false;
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;
1136 if (count == 0)
1137 /* Don't bother. */
1138 return true;
1140 if ((section->flags & SEC_HAS_CONTENTS) == 0)
1142 memset (location, 0, (unsigned) count);
1143 return true;
1146 if ((section->flags & SEC_IN_MEMORY) != 0)
1148 memcpy (location, section->contents + offset, (size_t) count);
1149 return true;
1152 return BFD_SEND (abfd, _bfd_get_section_contents,
1153 (abfd, section, location, offset, count));
1157 FUNCTION
1158 bfd_copy_private_section_data
1160 SYNOPSIS
1161 boolean bfd_copy_private_section_data(bfd *ibfd, asection *isec, bfd *obfd, asection *osec);
1163 DESCRIPTION
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:
1169 o <<bfd_error_no_memory>> -
1170 Not enough memory exists to create private data for @var{osec}.
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))
1178 FUNCTION
1179 _bfd_strip_section_from_output
1181 SYNOPSIS
1182 void _bfd_strip_section_from_output
1183 (struct bfd_link_info *info, asection *section);
1185 DESCRIPTION
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.
1191 void
1192 _bfd_strip_section_from_output (info, s)
1193 struct bfd_link_info *info;
1194 asection *s;
1196 asection **spp, *os;
1197 struct bfd_link_order *p, *pp;
1198 boolean keep_os;
1200 /* Excise the input section from the link order.
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)
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;
1219 keep_os = os->link_order_head != NULL;
1221 if (! keep_os && info != NULL)
1223 bfd *abfd;
1224 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
1226 asection *is;
1227 for (is = abfd->sections; is != NULL; is = is->next)
1229 if (is != s && is->output_section == os)
1230 break;
1232 if (is != NULL)
1233 break;
1235 if (abfd != NULL)
1236 keep_os = true;
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)
1244 for (spp = &os->owner->sections; *spp; spp = &(*spp)->next)
1245 if (*spp == os)
1247 *spp = os->next;
1248 os->owner->section_count--;
1249 break;