1 /* Object file "section" support for the BFD library.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 Sections are supported in BFD in @code{section.c}.
25 The raw data contained within a BFD is maintained through the section
26 abstraction. A single BFD may have any number of sections, and keeps
27 hold of them by pointing to the first, each one points to the next in
34 * section prototypes::
37 @node Section Input, Section Output,,Sections
38 @comment node-name, next, previous, up
39 @subsection Section Input
40 When a BFD is opened for reading, the section structures are created
41 and attatched to the BFD.
43 Each section has a name which describes the section in the outside
44 world - for example, @code{a.out} would contain at least three
45 sections, called @code{.text}, @code{.data} and @code{.bss}.
47 Sometimes a BFD will contain more than the 'natural' number of
48 sections. A back end may attatch other sections containing constructor
49 data, or an application may add a section (using bfd_make_section) to
50 the sections attatched to an already open BFD. For example, the linker
51 creates a supernumary section @code{COMMON} for each input file's BFD
52 to hold information about common storage.
54 The raw data is not necessarily read in at the same time as the
55 section descriptor is created. Some targets may leave the data in
56 place until a @code{bfd_get_section_contents} call is made. Other back
57 ends may read in all the data at once - For example; an S-record file
58 has to be read once to determine the size of the data. An IEEE-695
59 file doesn't contain raw data in sections, but data and relocation
60 expressions intermixed, so the data area has to be parsed to get out
61 the data and relocations.
63 @node Section Output,typedef asection,Section Input,Sections
64 @subsection Section Output
65 To write a new object style BFD, the various sections to be written
66 have to be created. They are attatched to the BFD in the same way as
67 input sections, data is written to the sections using
68 @code{bfd_set_section_contents}.
70 The linker uses the fields @code{output_section} and
71 @code{output_offset} to create an output file.
73 The data to be written comes from input sections attatched to the
74 output sections. The output section structure can be considered a
75 filter for the input section, the output section determines the vma of
76 the output data and the name, but the input section determines the
77 offset into the output section of the data to be written.
79 Eg to create a section "O", starting at 0x100, 0x123 long, containing two
80 subsections, "A" at offset 0x0 (ie at vma 0x100) and "B" at offset
81 0x20 (ie at vma 0x120) the structures would look like:
88 output_section -----------> section name "O"
90 section name "B" | size 0x123
93 output_section --------|
106 @node typedef asection,section prototypes,Section Output,Sections
107 @subsection typedef asection
111 The shape of a section struct:
115 $typedef struct sec {
117 The name of the section, the name isn't a copy, the pointer is
118 the same as that passed to bfd_make_section.
122 The next section in the list belonging to the BFD, or NULL.
126 The field flags contains attributes of the section. Some of these
127 flags are read in from the object file, and some are synthesized from
133 $#define SEC_NO_FLAGS 0x000
135 Tells the OS to allocate space for this section when loaded.
136 This would clear for a section containing debug information only.
138 $#define SEC_ALLOC 0x001
140 Tells the OS to load the section from the file when loading.
141 This would be clear for a .bss section
143 $#define SEC_LOAD 0x002
145 The section contains data still to be relocated, so there will be some
146 relocation information too.
148 $#define SEC_RELOC 0x004
152 $#define SEC_BALIGN 0x008
154 A signal to the OS that the section contains read only data.
156 $#define SEC_READONLY 0x010
158 The section contains code only.
160 $#define SEC_CODE 0x020
162 The section contains data only.
164 $#define SEC_DATA 0x040
166 The section will reside in ROM.
168 $#define SEC_ROM 0x080
170 The section contains constructor information. This section type is
171 used by the linker to create lists of constructors and destructors
172 used by @code{g++}. When a back end sees a symbol which should be used
173 in a constructor list, it creates a new section for the type of name
174 (eg @code{__CTOR_LIST__}), attatches the symbol to it and builds a
175 relocation. To build the lists of constructors, all the linker has to
176 to is catenate all the sections called @code{__CTOR_LIST__} and
177 relocte the data contained within - exactly the operations it would
178 peform on standard data.
180 $#define SEC_CONSTRUCTOR 0x100
182 The section is a constuctor, and should be placed at the end of the ..
184 $#define SEC_CONSTRUCTOR_TEXT 0x1100
186 $#define SEC_CONSTRUCTOR_DATA 0x2100
188 $#define SEC_CONSTRUCTOR_BSS 0x3100
191 The section has contents - a bss section could be
192 @code{SEC_ALLOC} | @code{SEC_HAS_CONTENTS}, a debug section could be
193 @code{SEC_HAS_CONTENTS}
195 $#define SEC_HAS_CONTENTS 0x200
197 An instruction to the linker not to output sections containing
198 this flag even if they have information which would normally be written.
200 $#define SEC_NEVER_LOAD 0x400
203 The base address of the section in the address space of the target.
207 The size of the section in bytes of the loaded section. This contains
208 a value even if the section has no contents (eg, the size of @code{.bss}).
210 $ bfd_size_type size;
212 If this section is going to be output, then this value is the
213 offset into the output section of the first byte in the input
214 section. Eg, if this was going to start at the 100th byte in the
215 output section, this value would be 100.
217 $ bfd_vma output_offset;
219 The output section through which to map on output.
221 $ struct sec *output_section;
223 The alignment requirement of the section, as an exponent - eg 3
226 $ unsigned int alignment_power;
228 If an input section, a pointer to a vector of relocation records for
229 the data in this section.
231 $ struct reloc_cache_entry *relocation;
233 If an output section, a pointer to a vector of pointers to
234 relocation records for the data in this section.
236 $ struct reloc_cache_entry **orelocation;
238 The number of relocation records in one of the above
240 $ unsigned reloc_count;
242 Which section is it 0..nth
246 Information below is back end specific - and not always used or
249 File position of section data
252 File position of relocation info
254 $ file_ptr rel_filepos;
256 File position of line data
258 $ file_ptr line_filepos;
260 Pointer to data for applications
264 $ struct lang_output_section *otheruserdata;
266 Attached line number information
269 Number of line number records
271 $ unsigned int lineno_count;
273 When a section is being output, this value changes as more
274 linenumbers are written out
276 $ file_ptr moving_line_filepos;
278 what the section number is in the target world
280 $ unsigned int target_index;
284 If this is a constructor section then here is a list of the
285 relocations created to relocate items within it.
287 $ struct relent_chain *constructor_chain;
289 The BFD which owns the section.
300 @node section prototypes,Section,typedef section,Sections
301 @subsection section prototypes
304 /*proto* bfd_get_section_by_name
305 Runs through the provided @var{abfd} and returns the @code{asection}
306 who's name matches that provided, otherwise NULL. @xref{Sections}, for more information.
308 *; PROTO(asection *, bfd_get_section_by_name,
309 (bfd *abfd, CONST char *name));
312 DEFUN(bfd_get_section_by_name,(abfd, name),
318 for (sect = abfd->sections; sect != NULL; sect = sect->next)
319 if (!strcmp (sect->name, name)) return sect;
324 /*proto* bfd_make_section
325 This function creates a new empty section called @var{name} and attatches it
326 to the end of the chain of sections for the BFD supplied. An attempt to
327 create a section with a name which is already in use, returns the old
328 section by that name instead.
332 @item invalid_operation
333 If output has already started for this BFD.
335 If obstack alloc fails.
338 *; PROTO(asection *, bfd_make_section, (bfd *, CONST char *name));
344 DEFUN(bfd_make_section,(abfd, name),
349 asection ** prev = &abfd->sections;
350 asection * sect = abfd->sections;
352 if (abfd->output_has_begun) {
353 bfd_error = invalid_operation;
358 if (!strcmp(sect->name, name)) return sect;
363 newsect = (asection *) bfd_zalloc(abfd, sizeof (asection));
364 if (newsect == NULL) {
365 bfd_error = no_memory;
369 newsect->name = name;
370 newsect->index = abfd->section_count++;
371 newsect->flags = SEC_NO_FLAGS;
373 newsect->userdata = 0;
374 newsect->next = (asection *)NULL;
375 newsect->relocation = (arelent *)NULL;
376 newsect->reloc_count = 0;
377 newsect->line_filepos =0;
378 newsect->owner = abfd;
379 if (BFD_SEND (abfd, _new_section_hook, (abfd, newsect)) != true) {
389 /*proto* bfd_set_section_flags
390 Attempts to set the attributes of the section named in the BFD
391 supplied to the value. Returns true on success, false on error.
392 Possible error returns are:
394 @item invalid operation
395 The section cannot have one or more of the attributes requested. For
396 example, a .bss section in @code{a.out} may not have the
397 @code{SEC_HAS_CONTENTS} field set.
400 *; PROTO(boolean, bfd_set_section_flags,
401 (bfd *, asection *, flagword));
405 DEFUN(bfd_set_section_flags,(abfd, section, flags),
410 if ((flags & bfd_applicable_section_flags (abfd)) != flags) {
411 bfd_error = invalid_operation;
415 section->flags = flags;
420 /*proto* bfd_map_over_sections
421 Calls the provided function @var{func} for each section attatched to
422 the BFD @var{abfd}, passing @var{obj} as an argument. The function
423 will be called as if by
426 func(abfd, the_section, obj);
430 *; PROTO(void, bfd_map_over_sections,
431 (bfd *abfd, void (*func)(), PTR obj));
433 This is the prefered method for iterating over sections, an
434 alternative would be to use a loop:
438 for (p = abfd->sections; p != NULL; p = p->next)
445 DEFUN(bfd_map_over_sections,(abfd, operation, user_storage),
447 void (*operation)() AND
453 for (sect = abfd->sections; sect != NULL; i++, sect = sect->next)
454 (*operation) (abfd, sect, user_storage);
456 if (i != abfd->section_count) /* Debugging */
461 /*proto* bfd_set_section_size
462 Sets @var{section} to the size @var{val}. If the operation is ok, then
463 @code{true} is returned, else @code{false}.
465 Possible error returns:
467 @item invalid_operation
468 Writing has started to the BFD, so setting the size is invalid
471 *; PROTO(boolean, bfd_set_section_size,
472 (bfd *, asection *, bfd_size_type val));
476 DEFUN(bfd_set_section_size,(abfd, ptr, val),
481 /* Once you've started writing to any section you cannot create or change
482 the size of any others. */
484 if (abfd->output_has_begun) {
485 bfd_error = invalid_operation;
494 /*proto* bfd_set_section_contents
495 Sets the contents of the section @var{section} in BFD @var{abfd} to
496 the data starting in memory at @var{data}. The data is written to the
497 output section starting at offset @var{offset} for @var{count} bytes.
499 Normally @code{true} is returned, else @code{false}. Possible error
503 The output section does not have the @code{SEC_HAS_CONTENTS}
504 attribute, so nothing can be written to it.
505 @item and some more too
507 This routine is front end to the back end function @code{_bfd_set_section_contents}.
509 *; PROTO(boolean, bfd_set_section_contents,
514 bfd_size_type count));
519 DEFUN(bfd_set_section_contents,(abfd, section, location, offset, count),
526 if (!(bfd_get_section_flags(abfd, section) & SEC_HAS_CONTENTS))
528 bfd_error = no_contents;
532 if (BFD_SEND (abfd, _bfd_set_section_contents,
533 (abfd, section, location, offset, count)))
535 abfd->output_has_begun = true;
542 /*proto* bfd_get_section_contents
543 This function reads data from @var{section} in BFD @var{abfd} into
544 memory starting at @var{location}. The data is read at an offset of
545 @var{offset} from the start of the input section, and is read for
548 If the contents of a constuctor with the @code{SEC_CONSTUCTOR} flag
549 set are requested, then the @var{location} is filled with zeroes.
551 If no errors occur, @code{true} is returned, else @code{false}.
558 *; PROTO(boolean, bfd_get_section_contents,
559 (bfd *abfd, asection *section, PTR location,
560 file_ptr offset, bfd_size_type count));
565 DEFUN(bfd_get_section_contents,(abfd, section, location, offset, count),
572 if (section->flags & SEC_CONSTRUCTOR)
574 memset(location, 0, (unsigned)count);
579 return (BFD_SEND (abfd, _bfd_get_section_contents,
580 (abfd, section, location, offset, count)));