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