daily update
[deliverable/binutils-gdb.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, 2002
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 . const char *name;
174 .
175 . {* A unique sequence number. *}
176 . int id;
177 .
178 . {* Which section in the bfd; 0..n-1 as sections are created in a bfd. *}
179 . int index;
180 .
181 . {* The next section in the list belonging to the BFD, or NULL. *}
182 . struct sec *next;
183 .
184 . {* The field flags contains attributes of the section. Some
185 . flags are read in from the object file, and some are
186 . synthesized from other information. *}
187 . flagword flags;
188 .
189 .#define SEC_NO_FLAGS 0x000
190 .
191 . {* Tells the OS to allocate space for this section when loading.
192 . This is clear for a section containing debug information only. *}
193 .#define SEC_ALLOC 0x001
194 .
195 . {* Tells the OS to load the section from the file when loading.
196 . This is clear for a .bss section. *}
197 .#define SEC_LOAD 0x002
198 .
199 . {* The section contains data still to be relocated, so there is
200 . some relocation information too. *}
201 .#define SEC_RELOC 0x004
202 .
203 . {* ELF reserves 4 processor specific bits and 8 operating system
204 . specific bits in sh_flags; at present we can get away with just
205 . one in communicating between the assembler and BFD, but this
206 . isn't a good long-term solution. *}
207 .#define SEC_ARCH_BIT_0 0x008
208 .
209 . {* A signal to the OS that the section contains read only data. *}
210 .#define SEC_READONLY 0x010
211 .
212 . {* The section contains code only. *}
213 .#define SEC_CODE 0x020
214 .
215 . {* The section contains data only. *}
216 .#define SEC_DATA 0x040
217 .
218 . {* The section will reside in ROM. *}
219 .#define SEC_ROM 0x080
220 .
221 . {* The section contains constructor information. This section
222 . type is used by the linker to create lists of constructors and
223 . destructors used by <<g++>>. When a back end sees a symbol
224 . which should be used in a constructor list, it creates a new
225 . section for the type of name (e.g., <<__CTOR_LIST__>>), attaches
226 . the symbol to it, and builds a relocation. To build the lists
227 . of constructors, all the linker has to do is catenate all the
228 . sections called <<__CTOR_LIST__>> and relocate the data
229 . contained within - exactly the operations it would peform on
230 . standard data. *}
231 .#define SEC_CONSTRUCTOR 0x100
232 .
233 . {* The section is a constructor, and should be placed at the
234 . end of the text, data, or bss section(?). *}
235 .#define SEC_CONSTRUCTOR_TEXT 0x1100
236 .#define SEC_CONSTRUCTOR_DATA 0x2100
237 .#define SEC_CONSTRUCTOR_BSS 0x3100
238 .
239 . {* The section has contents - a data section could be
240 . <<SEC_ALLOC>> | <<SEC_HAS_CONTENTS>>; a debug section could be
241 . <<SEC_HAS_CONTENTS>> *}
242 .#define SEC_HAS_CONTENTS 0x200
243 .
244 . {* An instruction to the linker to not output the section
245 . even if it has information which would normally be written. *}
246 .#define SEC_NEVER_LOAD 0x400
247 .
248 . {* The section is a COFF shared library section. This flag is
249 . only for the linker. If this type of section appears in
250 . the input file, the linker must copy it to the output file
251 . without changing the vma or size. FIXME: Although this
252 . was originally intended to be general, it really is COFF
253 . specific (and the flag was renamed to indicate this). It
254 . might be cleaner to have some more general mechanism to
255 . allow the back end to control what the linker does with
256 . sections. *}
257 .#define SEC_COFF_SHARED_LIBRARY 0x800
258 .
259 . {* The section has GOT references. This flag is only for the
260 . linker, and is currently only used by the elf32-hppa back end.
261 . It will be set if global offset table references were detected
262 . in this section, which indicate to the linker that the section
263 . contains PIC code, and must be handled specially when doing a
264 . static link. *}
265 .#define SEC_HAS_GOT_REF 0x4000
266 .
267 . {* The section contains common symbols (symbols may be defined
268 . multiple times, the value of a symbol is the amount of
269 . space it requires, and the largest symbol value is the one
270 . used). Most targets have exactly one of these (which we
271 . translate to bfd_com_section_ptr), but ECOFF has two. *}
272 .#define SEC_IS_COMMON 0x8000
273 .
274 . {* The section contains only debugging information. For
275 . example, this is set for ELF .debug and .stab sections.
276 . strip tests this flag to see if a section can be
277 . discarded. *}
278 .#define SEC_DEBUGGING 0x10000
279 .
280 . {* The contents of this section are held in memory pointed to
281 . by the contents field. This is checked by bfd_get_section_contents,
282 . and the data is retrieved from memory if appropriate. *}
283 .#define SEC_IN_MEMORY 0x20000
284 .
285 . {* The contents of this section are to be excluded by the
286 . linker for executable and shared objects unless those
287 . objects are to be further relocated. *}
288 .#define SEC_EXCLUDE 0x40000
289 .
290 . {* The contents of this section are to be sorted based on the sum of
291 . the symbol and addend values specified by the associated relocation
292 . entries. Entries without associated relocation entries will be
293 . appended to the end of the section in an unspecified order. *}
294 .#define SEC_SORT_ENTRIES 0x80000
295 .
296 . {* When linking, duplicate sections of the same name should be
297 . discarded, rather than being combined into a single section as
298 . is usually done. This is similar to how common symbols are
299 . handled. See SEC_LINK_DUPLICATES below. *}
300 .#define SEC_LINK_ONCE 0x100000
301 .
302 . {* If SEC_LINK_ONCE is set, this bitfield describes how the linker
303 . should handle duplicate sections. *}
304 .#define SEC_LINK_DUPLICATES 0x600000
305 .
306 . {* This value for SEC_LINK_DUPLICATES means that duplicate
307 . sections with the same name should simply be discarded. *}
308 .#define SEC_LINK_DUPLICATES_DISCARD 0x0
309 .
310 . {* This value for SEC_LINK_DUPLICATES means that the linker
311 . should warn if there are any duplicate sections, although
312 . it should still only link one copy. *}
313 .#define SEC_LINK_DUPLICATES_ONE_ONLY 0x200000
314 .
315 . {* This value for SEC_LINK_DUPLICATES means that the linker
316 . should warn if any duplicate sections are a different size. *}
317 .#define SEC_LINK_DUPLICATES_SAME_SIZE 0x400000
318 .
319 . {* This value for SEC_LINK_DUPLICATES means that the linker
320 . should warn if any duplicate sections contain different
321 . contents. *}
322 .#define SEC_LINK_DUPLICATES_SAME_CONTENTS 0x600000
323 .
324 . {* This section was created by the linker as part of dynamic
325 . relocation or other arcane processing. It is skipped when
326 . going through the first-pass output, trusting that someone
327 . else up the line will take care of it later. *}
328 .#define SEC_LINKER_CREATED 0x800000
329 .
330 . {* This section should not be subject to garbage collection. *}
331 .#define SEC_KEEP 0x1000000
332 .
333 . {* This section contains "short" data, and should be placed
334 . "near" the GP. *}
335 .#define SEC_SMALL_DATA 0x2000000
336 .
337 . {* This section contains data which may be shared with other
338 . executables or shared objects. *}
339 .#define SEC_SHARED 0x4000000
340 .
341 . {* When a section with this flag is being linked, then if the size of
342 . the input section is less than a page, it should not cross a page
343 . boundary. If the size of the input section is one page or more, it
344 . should be aligned on a page boundary. *}
345 .#define SEC_BLOCK 0x8000000
346 .
347 . {* Conditionally link this section; do not link if there are no
348 . references found to any symbol in the section. *}
349 .#define SEC_CLINK 0x10000000
350 .
351 . {* Attempt to merge identical entities in the section.
352 . Entity size is given in the entsize field. *}
353 .#define SEC_MERGE 0x20000000
354 .
355 . {* If given with SEC_MERGE, entities to merge are zero terminated
356 . strings where entsize specifies character size instead of fixed
357 . size entries. *}
358 .#define SEC_STRINGS 0x40000000
359 .
360 . {* This section contains data about section groups. *}
361 .#define SEC_GROUP 0x80000000
362 .
363 . {* End of section flags. *}
364 .
365 . {* Some internal packed boolean fields. *}
366 .
367 . {* See the vma field. *}
368 . unsigned int user_set_vma : 1;
369 .
370 . {* Whether relocations have been processed. *}
371 . unsigned int reloc_done : 1;
372 .
373 . {* A mark flag used by some of the linker backends. *}
374 . unsigned int linker_mark : 1;
375 .
376 . {* Another mark flag used by some of the linker backends. Set for
377 . output sections that have an input section. *}
378 . unsigned int linker_has_input : 1;
379 .
380 . {* A mark flag used by some linker backends for garbage collection. *}
381 . unsigned int gc_mark : 1;
382 .
383 . {* Used by the ELF code to mark sections which have been allocated
384 . to segments. *}
385 . unsigned int segment_mark : 1;
386 .
387 . {* End of internal packed boolean fields. *}
388 .
389 . {* The virtual memory address of the section - where it will be
390 . at run time. The symbols are relocated against this. The
391 . user_set_vma flag is maintained by bfd; if it's not set, the
392 . backend can assign addresses (for example, in <<a.out>>, where
393 . the default address for <<.data>> is dependent on the specific
394 . target and various flags). *}
395 . bfd_vma vma;
396 .
397 . {* The load address of the section - where it would be in a
398 . rom image; really only used for writing section header
399 . information. *}
400 . bfd_vma lma;
401 .
402 . {* The size of the section in octets, as it will be output.
403 . Contains a value even if the section has no contents (e.g., the
404 . size of <<.bss>>). This will be filled in after relocation. *}
405 . bfd_size_type _cooked_size;
406 .
407 . {* The original size on disk of the section, in octets. Normally this
408 . value is the same as the size, but if some relaxing has
409 . been done, then this value will be bigger. *}
410 . bfd_size_type _raw_size;
411 .
412 . {* If this section is going to be output, then this value is the
413 . offset in *bytes* into the output section of the first byte in the
414 . input section (byte ==> smallest addressable unit on the
415 . target). In most cases, if this was going to start at the
416 . 100th octet (8-bit quantity) in the output section, this value
417 . would be 100. However, if the target byte size is 16 bits
418 . (bfd_octets_per_byte is "2"), this value would be 50. *}
419 . bfd_vma output_offset;
420 .
421 . {* The output section through which to map on output. *}
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 . unsigned int alignment_power;
427 .
428 . {* If an input section, a pointer to a vector of relocation
429 . records for the data in this section. *}
430 . struct reloc_cache_entry *relocation;
431 .
432 . {* If an output section, a pointer to a vector of pointers to
433 . relocation records for the data in this section. *}
434 . struct reloc_cache_entry **orelocation;
435 .
436 . {* The number of relocation records in one of the above. *}
437 . unsigned reloc_count;
438 .
439 . {* Information below is back end specific - and not always used
440 . or updated. *}
441 .
442 . {* File position of section data. *}
443 . file_ptr filepos;
444 .
445 . {* File position of relocation info. *}
446 . file_ptr rel_filepos;
447 .
448 . {* File position of line data. *}
449 . file_ptr line_filepos;
450 .
451 . {* Pointer to data for applications. *}
452 . PTR userdata;
453 .
454 . {* If the SEC_IN_MEMORY flag is set, this points to the actual
455 . contents. *}
456 . unsigned char *contents;
457 .
458 . {* Attached line number information. *}
459 . alent *lineno;
460 .
461 . {* Number of line number records. *}
462 . unsigned int lineno_count;
463 .
464 . {* Entity size for merging purposes. *}
465 . unsigned int entsize;
466 .
467 . {* Optional information about a COMDAT entry; NULL if not COMDAT. *}
468 . struct bfd_comdat_info *comdat;
469 .
470 . {* When a section is being output, this value changes as more
471 . linenumbers are written out. *}
472 . file_ptr moving_line_filepos;
473 .
474 . {* What the section number is in the target world. *}
475 . int target_index;
476 .
477 . PTR used_by_bfd;
478 .
479 . {* If this is a constructor section then here is a list of the
480 . relocations created to relocate items within it. *}
481 . struct relent_chain *constructor_chain;
482 .
483 . {* The BFD which owns the section. *}
484 . bfd *owner;
485 .
486 . {* A symbol which points at this section only. *}
487 . struct symbol_cache_entry *symbol;
488 . struct symbol_cache_entry **symbol_ptr_ptr;
489 .
490 . struct bfd_link_order *link_order_head;
491 . struct bfd_link_order *link_order_tail;
492 .} asection;
493 .
494 .{* These sections are global, and are managed by BFD. The application
495 . and target back end are not permitted to change the values in
496 . these sections. New code should use the section_ptr macros rather
497 . than referring directly to the const sections. The const sections
498 . may eventually vanish. *}
499 .#define BFD_ABS_SECTION_NAME "*ABS*"
500 .#define BFD_UND_SECTION_NAME "*UND*"
501 .#define BFD_COM_SECTION_NAME "*COM*"
502 .#define BFD_IND_SECTION_NAME "*IND*"
503 .
504 .{* The absolute section. *}
505 .extern const asection bfd_abs_section;
506 .#define bfd_abs_section_ptr ((asection *) &bfd_abs_section)
507 .#define bfd_is_abs_section(sec) ((sec) == bfd_abs_section_ptr)
508 .{* Pointer to the undefined section. *}
509 .extern const asection bfd_und_section;
510 .#define bfd_und_section_ptr ((asection *) &bfd_und_section)
511 .#define bfd_is_und_section(sec) ((sec) == bfd_und_section_ptr)
512 .{* Pointer to the common section. *}
513 .extern const asection bfd_com_section;
514 .#define bfd_com_section_ptr ((asection *) &bfd_com_section)
515 .{* Pointer to the indirect section. *}
516 .extern const asection bfd_ind_section;
517 .#define bfd_ind_section_ptr ((asection *) &bfd_ind_section)
518 .#define bfd_is_ind_section(sec) ((sec) == bfd_ind_section_ptr)
519 .
520 .#define bfd_is_const_section(SEC) \
521 . ( ((SEC) == bfd_abs_section_ptr) \
522 . || ((SEC) == bfd_und_section_ptr) \
523 . || ((SEC) == bfd_com_section_ptr) \
524 . || ((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 .{* Macros to handle insertion and deletion of a bfd's sections. These
538 . only handle the list pointers, ie. do not adjust section_count,
539 . target_index etc. *}
540 .#define bfd_section_list_remove(ABFD, PS) \
541 . do \
542 . { \
543 . asection **_ps = PS; \
544 . asection *_s = *_ps; \
545 . *_ps = _s->next; \
546 . if (_s->next == NULL) \
547 . (ABFD)->section_tail = _ps; \
548 . } \
549 . while (0)
550 .#define bfd_section_list_insert(ABFD, PS, S) \
551 . do \
552 . { \
553 . asection **_ps = PS; \
554 . asection *_s = S; \
555 . _s->next = *_ps; \
556 . *_ps = _s; \
557 . if (_s->next == NULL) \
558 . (ABFD)->section_tail = &_s->next; \
559 . } \
560 . while (0)
561 .
562 */
563
564 /* We use a macro to initialize the static asymbol structures because
565 traditional C does not permit us to initialize a union member while
566 gcc warns if we don't initialize it. */
567 /* the_bfd, name, value, attr, section [, udata] */
568 #ifdef __STDC__
569 #define GLOBAL_SYM_INIT(NAME, SECTION) \
570 { 0, NAME, 0, BSF_SECTION_SYM, (asection *) SECTION, { 0 }}
571 #else
572 #define GLOBAL_SYM_INIT(NAME, SECTION) \
573 { 0, NAME, 0, BSF_SECTION_SYM, (asection *) SECTION }
574 #endif
575
576 /* These symbols are global, not specific to any BFD. Therefore, anything
577 that tries to change them is broken, and should be repaired. */
578
579 static const asymbol global_syms[] =
580 {
581 GLOBAL_SYM_INIT (BFD_COM_SECTION_NAME, &bfd_com_section),
582 GLOBAL_SYM_INIT (BFD_UND_SECTION_NAME, &bfd_und_section),
583 GLOBAL_SYM_INIT (BFD_ABS_SECTION_NAME, &bfd_abs_section),
584 GLOBAL_SYM_INIT (BFD_IND_SECTION_NAME, &bfd_ind_section)
585 };
586
587 #define STD_SECTION(SEC, FLAGS, SYM, NAME, IDX) \
588 const asymbol * const SYM = (asymbol *) &global_syms[IDX]; \
589 const asection SEC = \
590 /* name, id, index, next, flags, user_set_vma, reloc_done, */ \
591 { NAME, IDX, 0, NULL, FLAGS, 0, 0, \
592 \
593 /* linker_mark, linker_has_input, gc_mark, segment_mark, */ \
594 0, 0, 1, 0, \
595 \
596 /* vma, lma, _cooked_size, _raw_size, */ \
597 0, 0, 0, 0, \
598 \
599 /* output_offset, output_section, alignment_power, */ \
600 0, (struct sec *) &SEC, 0, \
601 \
602 /* relocation, orelocation, reloc_count, filepos, rel_filepos, */ \
603 NULL, NULL, 0, 0, 0, \
604 \
605 /* line_filepos, userdata, contents, lineno, lineno_count, */ \
606 0, NULL, NULL, NULL, 0, \
607 \
608 /* entsize, comdat, moving_line_filepos, */ \
609 0, NULL, 0, \
610 \
611 /* target_index, used_by_bfd, constructor_chain, owner, */ \
612 0, NULL, NULL, NULL, \
613 \
614 /* symbol, */ \
615 (struct symbol_cache_entry *) &global_syms[IDX], \
616 \
617 /* symbol_ptr_ptr, */ \
618 (struct symbol_cache_entry **) &SYM, \
619 \
620 /* link_order_head, link_order_tail */ \
621 NULL, NULL \
622 }
623
624 STD_SECTION (bfd_com_section, SEC_IS_COMMON, bfd_com_symbol,
625 BFD_COM_SECTION_NAME, 0);
626 STD_SECTION (bfd_und_section, 0, bfd_und_symbol, BFD_UND_SECTION_NAME, 1);
627 STD_SECTION (bfd_abs_section, 0, bfd_abs_symbol, BFD_ABS_SECTION_NAME, 2);
628 STD_SECTION (bfd_ind_section, 0, bfd_ind_symbol, BFD_IND_SECTION_NAME, 3);
629 #undef STD_SECTION
630
631 struct section_hash_entry
632 {
633 struct bfd_hash_entry root;
634 asection section;
635 };
636
637 /* Initialize an entry in the section hash table. */
638
639 struct bfd_hash_entry *
640 bfd_section_hash_newfunc (entry, table, string)
641 struct bfd_hash_entry *entry;
642 struct bfd_hash_table *table;
643 const char *string;
644 {
645 /* Allocate the structure if it has not already been allocated by a
646 subclass. */
647 if (entry == NULL)
648 {
649 entry = bfd_hash_allocate (table, sizeof (struct section_hash_entry));
650 if (entry == NULL)
651 return entry;
652 }
653
654 /* Call the allocation method of the superclass. */
655 entry = bfd_hash_newfunc (entry, table, string);
656 if (entry != NULL)
657 {
658 memset ((PTR) &((struct section_hash_entry *) entry)->section,
659 0, sizeof (asection));
660 }
661
662 return entry;
663 }
664
665 #define section_hash_lookup(table, string, create, copy) \
666 ((struct section_hash_entry *) \
667 bfd_hash_lookup ((table), (string), (create), (copy)))
668
669 /* Initializes a new section. NEWSECT->NAME is already set. */
670
671 static asection *bfd_section_init PARAMS ((bfd *, asection *));
672
673 static asection *
674 bfd_section_init (abfd, newsect)
675 bfd *abfd;
676 asection *newsect;
677 {
678 static int section_id = 0x10; /* id 0 to 3 used by STD_SECTION. */
679
680 newsect->id = section_id;
681 newsect->index = abfd->section_count;
682 newsect->owner = abfd;
683
684 /* Create a symbol whose only job is to point to this section. This
685 is useful for things like relocs which are relative to the base
686 of a section. */
687 newsect->symbol = bfd_make_empty_symbol (abfd);
688 if (newsect->symbol == NULL)
689 return NULL;
690
691 newsect->symbol->name = newsect->name;
692 newsect->symbol->value = 0;
693 newsect->symbol->section = newsect;
694 newsect->symbol->flags = BSF_SECTION_SYM;
695
696 newsect->symbol_ptr_ptr = &newsect->symbol;
697
698 if (! BFD_SEND (abfd, _new_section_hook, (abfd, newsect)))
699 return NULL;
700
701 section_id++;
702 abfd->section_count++;
703 *abfd->section_tail = newsect;
704 abfd->section_tail = &newsect->next;
705 return newsect;
706 }
707
708 /*
709 DOCDD
710 INODE
711 section prototypes, , typedef asection, Sections
712 SUBSECTION
713 Section prototypes
714
715 These are the functions exported by the section handling part of BFD.
716 */
717
718 /*
719 FUNCTION
720 bfd_section_list_clear
721
722 SYNOPSIS
723 void bfd_section_list_clear (bfd *);
724
725 DESCRIPTION
726 Clears the section list, and also resets the section count and
727 hash table entries.
728 */
729
730 void
731 bfd_section_list_clear (abfd)
732 bfd *abfd;
733 {
734 abfd->sections = NULL;
735 abfd->section_tail = &abfd->sections;
736 abfd->section_count = 0;
737 memset ((PTR) abfd->section_htab.table, 0,
738 abfd->section_htab.size * sizeof (struct bfd_hash_entry *));
739 }
740
741 /*
742 FUNCTION
743 bfd_get_section_by_name
744
745 SYNOPSIS
746 asection *bfd_get_section_by_name(bfd *abfd, const char *name);
747
748 DESCRIPTION
749 Run through @var{abfd} and return the one of the
750 <<asection>>s whose name matches @var{name}, otherwise <<NULL>>.
751 @xref{Sections}, for more information.
752
753 This should only be used in special cases; the normal way to process
754 all sections of a given name is to use <<bfd_map_over_sections>> and
755 <<strcmp>> on the name (or better yet, base it on the section flags
756 or something else) for each section.
757 */
758
759 asection *
760 bfd_get_section_by_name (abfd, name)
761 bfd *abfd;
762 const char *name;
763 {
764 struct section_hash_entry *sh;
765
766 sh = section_hash_lookup (&abfd->section_htab, name, false, false);
767 if (sh != NULL)
768 return &sh->section;
769
770 return NULL;
771 }
772
773 /*
774 FUNCTION
775 bfd_get_unique_section_name
776
777 SYNOPSIS
778 char *bfd_get_unique_section_name(bfd *abfd,
779 const char *templat,
780 int *count);
781
782 DESCRIPTION
783 Invent a section name that is unique in @var{abfd} by tacking
784 a dot and a digit suffix onto the original @var{templat}. If
785 @var{count} is non-NULL, then it specifies the first number
786 tried as a suffix to generate a unique name. The value
787 pointed to by @var{count} will be incremented in this case.
788 */
789
790 char *
791 bfd_get_unique_section_name (abfd, templat, count)
792 bfd *abfd;
793 const char *templat;
794 int *count;
795 {
796 int num;
797 unsigned int len;
798 char *sname;
799
800 len = strlen (templat);
801 sname = bfd_malloc ((bfd_size_type) len + 8);
802 if (sname == NULL)
803 return NULL;
804 strcpy (sname, templat);
805 num = 1;
806 if (count != NULL)
807 num = *count;
808
809 do
810 {
811 /* If we have a million sections, something is badly wrong. */
812 if (num > 999999)
813 abort ();
814 sprintf (sname + len, ".%d", num++);
815 }
816 while (section_hash_lookup (&abfd->section_htab, sname, false, false));
817
818 if (count != NULL)
819 *count = num;
820 return sname;
821 }
822
823 /*
824 FUNCTION
825 bfd_make_section_old_way
826
827 SYNOPSIS
828 asection *bfd_make_section_old_way(bfd *abfd, const char *name);
829
830 DESCRIPTION
831 Create a new empty section called @var{name}
832 and attach it to the end of the chain of sections for the
833 BFD @var{abfd}. An attempt to create a section with a name which
834 is already in use returns its pointer without changing the
835 section chain.
836
837 It has the funny name since this is the way it used to be
838 before it was rewritten....
839
840 Possible errors are:
841 o <<bfd_error_invalid_operation>> -
842 If output has already started for this BFD.
843 o <<bfd_error_no_memory>> -
844 If memory allocation fails.
845
846 */
847
848 asection *
849 bfd_make_section_old_way (abfd, name)
850 bfd *abfd;
851 const char *name;
852 {
853 struct section_hash_entry *sh;
854 asection *newsect;
855
856 if (abfd->output_has_begun)
857 {
858 bfd_set_error (bfd_error_invalid_operation);
859 return NULL;
860 }
861
862 if (strcmp (name, BFD_ABS_SECTION_NAME) == 0)
863 return bfd_abs_section_ptr;
864
865 if (strcmp (name, BFD_COM_SECTION_NAME) == 0)
866 return bfd_com_section_ptr;
867
868 if (strcmp (name, BFD_UND_SECTION_NAME) == 0)
869 return bfd_und_section_ptr;
870
871 if (strcmp (name, BFD_IND_SECTION_NAME) == 0)
872 return bfd_ind_section_ptr;
873
874 sh = section_hash_lookup (&abfd->section_htab, name, true, false);
875 if (sh == NULL)
876 return NULL;
877
878 newsect = &sh->section;
879 if (newsect->name != NULL)
880 {
881 /* Section already exists. */
882 return newsect;
883 }
884
885 newsect->name = name;
886 return bfd_section_init (abfd, newsect);
887 }
888
889 /*
890 FUNCTION
891 bfd_make_section_anyway
892
893 SYNOPSIS
894 asection *bfd_make_section_anyway(bfd *abfd, const char *name);
895
896 DESCRIPTION
897 Create a new empty section called @var{name} and attach it to the end of
898 the chain of sections for @var{abfd}. Create a new section even if there
899 is already a section with that name.
900
901 Return <<NULL>> and set <<bfd_error>> on error; possible errors are:
902 o <<bfd_error_invalid_operation>> - If output has already started for @var{abfd}.
903 o <<bfd_error_no_memory>> - If memory allocation fails.
904 */
905
906 sec_ptr
907 bfd_make_section_anyway (abfd, name)
908 bfd *abfd;
909 const char *name;
910 {
911 struct section_hash_entry *sh;
912 asection *newsect;
913
914 if (abfd->output_has_begun)
915 {
916 bfd_set_error (bfd_error_invalid_operation);
917 return NULL;
918 }
919
920 sh = section_hash_lookup (&abfd->section_htab, name, true, false);
921 if (sh == NULL)
922 return NULL;
923
924 newsect = &sh->section;
925 if (newsect->name != NULL)
926 {
927 /* We are making a section of the same name. It can't go in
928 section_htab without generating a unique section name and
929 that would be pointless; We don't need to traverse the
930 hash table. */
931 newsect = (asection *) bfd_zalloc (abfd, sizeof (asection));
932 if (newsect == NULL)
933 return NULL;
934 }
935
936 newsect->name = name;
937 return bfd_section_init (abfd, newsect);
938 }
939
940 /*
941 FUNCTION
942 bfd_make_section
943
944 SYNOPSIS
945 asection *bfd_make_section(bfd *, const char *name);
946
947 DESCRIPTION
948 Like <<bfd_make_section_anyway>>, but return <<NULL>> (without calling
949 bfd_set_error ()) without changing the section chain if there is already a
950 section named @var{name}. If there is an error, return <<NULL>> and set
951 <<bfd_error>>.
952 */
953
954 asection *
955 bfd_make_section (abfd, name)
956 bfd *abfd;
957 const char *name;
958 {
959 struct section_hash_entry *sh;
960 asection *newsect;
961
962 if (abfd->output_has_begun)
963 {
964 bfd_set_error (bfd_error_invalid_operation);
965 return NULL;
966 }
967
968 if (strcmp (name, BFD_ABS_SECTION_NAME) == 0
969 || strcmp (name, BFD_COM_SECTION_NAME) == 0
970 || strcmp (name, BFD_UND_SECTION_NAME) == 0
971 || strcmp (name, BFD_IND_SECTION_NAME) == 0)
972 return NULL;
973
974 sh = section_hash_lookup (&abfd->section_htab, name, true, false);
975 if (sh == NULL)
976 return NULL;
977
978 newsect = &sh->section;
979 if (newsect->name != NULL)
980 {
981 /* Section already exists. */
982 return newsect;
983 }
984
985 newsect->name = name;
986 return bfd_section_init (abfd, newsect);
987 }
988
989 /*
990 FUNCTION
991 bfd_set_section_flags
992
993 SYNOPSIS
994 boolean bfd_set_section_flags(bfd *abfd, asection *sec, flagword flags);
995
996 DESCRIPTION
997 Set the attributes of the section @var{sec} in the BFD
998 @var{abfd} to the value @var{flags}. Return <<true>> on success,
999 <<false>> on error. Possible error returns are:
1000
1001 o <<bfd_error_invalid_operation>> -
1002 The section cannot have one or more of the attributes
1003 requested. For example, a .bss section in <<a.out>> may not
1004 have the <<SEC_HAS_CONTENTS>> field set.
1005
1006 */
1007
1008 /*ARGSUSED*/
1009 boolean
1010 bfd_set_section_flags (abfd, section, flags)
1011 bfd *abfd ATTRIBUTE_UNUSED;
1012 sec_ptr section;
1013 flagword flags;
1014 {
1015 #if 0
1016 /* If you try to copy a text section from an input file (where it
1017 has the SEC_CODE flag set) to an output file, this loses big if
1018 the bfd_applicable_section_flags (abfd) doesn't have the SEC_CODE
1019 set - which it doesn't, at least not for a.out. FIXME */
1020
1021 if ((flags & bfd_applicable_section_flags (abfd)) != flags)
1022 {
1023 bfd_set_error (bfd_error_invalid_operation);
1024 return false;
1025 }
1026 #endif
1027
1028 section->flags = flags;
1029 return true;
1030 }
1031
1032 /*
1033 FUNCTION
1034 bfd_map_over_sections
1035
1036 SYNOPSIS
1037 void bfd_map_over_sections(bfd *abfd,
1038 void (*func) (bfd *abfd,
1039 asection *sect,
1040 PTR obj),
1041 PTR obj);
1042
1043 DESCRIPTION
1044 Call the provided function @var{func} for each section
1045 attached to the BFD @var{abfd}, passing @var{obj} as an
1046 argument. The function will be called as if by
1047
1048 | func(abfd, the_section, obj);
1049
1050 This is the prefered method for iterating over sections; an
1051 alternative would be to use a loop:
1052
1053 | section *p;
1054 | for (p = abfd->sections; p != NULL; p = p->next)
1055 | func(abfd, p, ...)
1056
1057 */
1058
1059 /*VARARGS2*/
1060 void
1061 bfd_map_over_sections (abfd, operation, user_storage)
1062 bfd *abfd;
1063 void (*operation) PARAMS ((bfd * abfd, asection * sect, PTR obj));
1064 PTR user_storage;
1065 {
1066 asection *sect;
1067 unsigned int i = 0;
1068
1069 for (sect = abfd->sections; sect != NULL; i++, sect = sect->next)
1070 (*operation) (abfd, sect, user_storage);
1071
1072 if (i != abfd->section_count) /* Debugging */
1073 abort ();
1074 }
1075
1076 /*
1077 FUNCTION
1078 bfd_set_section_size
1079
1080 SYNOPSIS
1081 boolean bfd_set_section_size(bfd *abfd, asection *sec, bfd_size_type val);
1082
1083 DESCRIPTION
1084 Set @var{sec} to the size @var{val}. If the operation is
1085 ok, then <<true>> is returned, else <<false>>.
1086
1087 Possible error returns:
1088 o <<bfd_error_invalid_operation>> -
1089 Writing has started to the BFD, so setting the size is invalid.
1090
1091 */
1092
1093 boolean
1094 bfd_set_section_size (abfd, ptr, val)
1095 bfd *abfd;
1096 sec_ptr ptr;
1097 bfd_size_type val;
1098 {
1099 /* Once you've started writing to any section you cannot create or change
1100 the size of any others. */
1101
1102 if (abfd->output_has_begun)
1103 {
1104 bfd_set_error (bfd_error_invalid_operation);
1105 return false;
1106 }
1107
1108 ptr->_cooked_size = val;
1109 ptr->_raw_size = val;
1110
1111 return true;
1112 }
1113
1114 /*
1115 FUNCTION
1116 bfd_set_section_contents
1117
1118 SYNOPSIS
1119 boolean bfd_set_section_contents (bfd *abfd, asection *section,
1120 PTR data, file_ptr offset,
1121 bfd_size_type count);
1122
1123 DESCRIPTION
1124 Sets the contents of the section @var{section} in BFD
1125 @var{abfd} to the data starting in memory at @var{data}. The
1126 data is written to the output section starting at offset
1127 @var{offset} for @var{count} octets.
1128
1129 Normally <<true>> is returned, else <<false>>. Possible error
1130 returns are:
1131 o <<bfd_error_no_contents>> -
1132 The output section does not have the <<SEC_HAS_CONTENTS>>
1133 attribute, so nothing can be written to it.
1134 o and some more too
1135
1136 This routine is front end to the back end function
1137 <<_bfd_set_section_contents>>.
1138
1139 */
1140
1141 #define bfd_get_section_size_now(abfd,sec) \
1142 (sec->reloc_done \
1143 ? bfd_get_section_size_after_reloc (sec) \
1144 : bfd_get_section_size_before_reloc (sec))
1145
1146 boolean
1147 bfd_set_section_contents (abfd, section, location, offset, count)
1148 bfd *abfd;
1149 sec_ptr section;
1150 PTR location;
1151 file_ptr offset;
1152 bfd_size_type count;
1153 {
1154 bfd_size_type sz;
1155
1156 if (!(bfd_get_section_flags (abfd, section) & SEC_HAS_CONTENTS))
1157 {
1158 bfd_set_error (bfd_error_no_contents);
1159 return (false);
1160 }
1161
1162 sz = bfd_get_section_size_now (abfd, section);
1163 if ((bfd_size_type) offset > sz
1164 || count > sz
1165 || offset + count > sz
1166 || count != (size_t) count)
1167 {
1168 bfd_set_error (bfd_error_bad_value);
1169 return false;
1170 }
1171
1172 switch (abfd->direction)
1173 {
1174 case read_direction:
1175 case no_direction:
1176 bfd_set_error (bfd_error_invalid_operation);
1177 return false;
1178
1179 case write_direction:
1180 break;
1181
1182 case both_direction:
1183 /* File is opened for update. `output_has_begun' some time ago when
1184 the file was created. Do not recompute sections sizes or alignments
1185 in _bfd_set_section_content. */
1186 abfd->output_has_begun = true;
1187 break;
1188 }
1189
1190 /* Record a copy of the data in memory if desired. */
1191 if (section->contents
1192 && location != section->contents + offset)
1193 memcpy (section->contents + offset, location, (size_t) count);
1194
1195 if (BFD_SEND (abfd, _bfd_set_section_contents,
1196 (abfd, section, location, offset, count)))
1197 {
1198 abfd->output_has_begun = true;
1199 return true;
1200 }
1201
1202 return false;
1203 }
1204
1205 /*
1206 FUNCTION
1207 bfd_get_section_contents
1208
1209 SYNOPSIS
1210 boolean bfd_get_section_contents (bfd *abfd, asection *section,
1211 PTR location, file_ptr offset,
1212 bfd_size_type count);
1213
1214 DESCRIPTION
1215 Read data from @var{section} in BFD @var{abfd}
1216 into memory starting at @var{location}. The data is read at an
1217 offset of @var{offset} from the start of the input section,
1218 and is read for @var{count} bytes.
1219
1220 If the contents of a constructor with the <<SEC_CONSTRUCTOR>>
1221 flag set are requested or if the section does not have the
1222 <<SEC_HAS_CONTENTS>> flag set, then the @var{location} is filled
1223 with zeroes. If no errors occur, <<true>> is returned, else
1224 <<false>>.
1225
1226 */
1227 boolean
1228 bfd_get_section_contents (abfd, section, location, offset, count)
1229 bfd *abfd;
1230 sec_ptr section;
1231 PTR location;
1232 file_ptr offset;
1233 bfd_size_type count;
1234 {
1235 bfd_size_type sz;
1236
1237 if (section->flags & SEC_CONSTRUCTOR)
1238 {
1239 memset (location, 0, (size_t) count);
1240 return true;
1241 }
1242
1243 /* Even if reloc_done is true, this function reads unrelocated
1244 contents, so we want the raw size. */
1245 sz = section->_raw_size;
1246 if ((bfd_size_type) offset > sz
1247 || count > sz
1248 || offset + count > sz
1249 || count != (size_t) count)
1250 {
1251 bfd_set_error (bfd_error_bad_value);
1252 return false;
1253 }
1254
1255 if (count == 0)
1256 /* Don't bother. */
1257 return true;
1258
1259 if ((section->flags & SEC_HAS_CONTENTS) == 0)
1260 {
1261 memset (location, 0, (size_t) count);
1262 return true;
1263 }
1264
1265 if ((section->flags & SEC_IN_MEMORY) != 0)
1266 {
1267 memcpy (location, section->contents + offset, (size_t) count);
1268 return true;
1269 }
1270
1271 return BFD_SEND (abfd, _bfd_get_section_contents,
1272 (abfd, section, location, offset, count));
1273 }
1274
1275 /*
1276 FUNCTION
1277 bfd_copy_private_section_data
1278
1279 SYNOPSIS
1280 boolean bfd_copy_private_section_data (bfd *ibfd, asection *isec,
1281 bfd *obfd, asection *osec);
1282
1283 DESCRIPTION
1284 Copy private section information from @var{isec} in the BFD
1285 @var{ibfd} to the section @var{osec} in the BFD @var{obfd}.
1286 Return <<true>> on success, <<false>> on error. Possible error
1287 returns are:
1288
1289 o <<bfd_error_no_memory>> -
1290 Not enough memory exists to create private data for @var{osec}.
1291
1292 .#define bfd_copy_private_section_data(ibfd, isection, obfd, osection) \
1293 . BFD_SEND (obfd, _bfd_copy_private_section_data, \
1294 . (ibfd, isection, obfd, osection))
1295 */
1296
1297 /*
1298 FUNCTION
1299 _bfd_strip_section_from_output
1300
1301 SYNOPSIS
1302 void _bfd_strip_section_from_output
1303 (struct bfd_link_info *info, asection *section);
1304
1305 DESCRIPTION
1306 Remove @var{section} from the output. If the output section
1307 becomes empty, remove it from the output bfd. @var{info} may
1308 be NULL; if it is not, it is used to decide whether the output
1309 section is empty.
1310 */
1311 void
1312 _bfd_strip_section_from_output (info, s)
1313 struct bfd_link_info *info;
1314 asection *s;
1315 {
1316 asection **spp, *os;
1317 struct bfd_link_order *p, *pp;
1318 boolean keep_os;
1319
1320 /* Excise the input section from the link order.
1321
1322 FIXME: For all calls that I can see to this function, the link
1323 orders have not yet been set up. So why are we checking them? --
1324 Ian */
1325 os = s->output_section;
1326
1327 /* Handle a section that wasn't output. */
1328 if (os == NULL)
1329 return;
1330
1331 for (p = os->link_order_head, pp = NULL; p != NULL; pp = p, p = p->next)
1332 if (p->type == bfd_indirect_link_order
1333 && p->u.indirect.section == s)
1334 {
1335 if (pp)
1336 pp->next = p->next;
1337 else
1338 os->link_order_head = p->next;
1339 if (!p->next)
1340 os->link_order_tail = pp;
1341 break;
1342 }
1343
1344 keep_os = os->link_order_head != NULL;
1345
1346 if (! keep_os && info != NULL)
1347 {
1348 bfd *abfd;
1349 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next)
1350 {
1351 asection *is;
1352 for (is = abfd->sections; is != NULL; is = is->next)
1353 {
1354 if (is != s && is->output_section == os
1355 && (is->flags & SEC_EXCLUDE) == 0)
1356 break;
1357 }
1358 if (is != NULL)
1359 break;
1360 }
1361 if (abfd != NULL)
1362 keep_os = true;
1363 }
1364
1365 /* If the output section is empty, remove it too. Careful about sections
1366 that have been discarded in the link script -- they are mapped to
1367 bfd_abs_section, which has no owner. */
1368 if (!keep_os && os->owner != NULL)
1369 {
1370 for (spp = &os->owner->sections; *spp; spp = &(*spp)->next)
1371 if (*spp == os)
1372 {
1373 bfd_section_list_remove (os->owner, spp);
1374 os->owner->section_count--;
1375 break;
1376 }
1377 }
1378
1379 s->flags |= SEC_EXCLUDE;
1380 }
This page took 0.05813 seconds and 4 git commands to generate.