Initial revision
[deliverable/binutils-gdb.git] / bfd / section.c
CommitLineData
6724ff46
RP
1/* Object file "section" support for the BFD library.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
4a96bc04
SC
21/*
22SECTION
23 Sections
985fca12 24
4a96bc04
SC
25 Sections are supported in BFD in <<section.c>>.
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, and keeps hold of them by pointing to the first,
30 each one points to the next in the list.
985fca12
SC
31
32@menu
33* Section Input::
34* Section Output::
35* typedef asection::
36* section prototypes::
37@end menu
38
4a96bc04
SC
39INODE
40 Section Input, Section Output, Sections, Sections
41
42SUBSECTION
43 Section Input
44
45 When a BFD is opened for reading, the section structures are
46 created and attached to the BFD.
47
48 Each section has a name which describes the section in the
49 outside world - for example, <<a.out>> would contain at least
50 three sections, called <<.text>>, <<.data>> and <<.bss>>.
51
52 Sometimes a BFD will contain more than the 'natural' number of
53 sections. A back end may attach other sections containing
54 constructor data, or an application may add a section (using
55 bfd_make_section) to the sections attached to an already open
56 BFD. For example, the linker creates a supernumary section
57 <<COMMON>> for each input file's BFD to hold information about
58 common storage.
59
60 The raw data is not necessarily read in at the same time as
61 the section descriptor is created. Some targets may leave the
62 data in place until a <<bfd_get_section_contents>> call is
63 made. Other back ends may read in all the data at once - For
64 example; an S-record file has to be read once to determine the
65 size of the data. An IEEE-695 file doesn't contain raw data in
66 sections, but data and relocation expressions intermixed, so
67 the data area has to be parsed to get out the data and
68 relocations.
69
70INODE
71 Section Output, typedef asection, Section Input, Sections
72
73SUBSECTION
74 Section Output
75
76 To write a new object style BFD, the various sections to be
77 written have to be created. They are attached to the BFD in
78 the same way as input sections, data is written to the
79 sections using <<bfd_set_section_contents>>.
80
81 The linker uses the fields <<output_section>> and
82 <<output_offset>> to create an output file.
83
84 The data to be written comes from input sections attached to
85 the output sections. The output section structure can be
86 considered a filter for the input section, the output section
87 determines the vma of the output data and the name, but the
88 input section determines the offset into the output section of
89 the data to be written.
90
91 Eg to create a section "O", starting at 0x100, 0x123 long,
92 containing two subsections, "A" at offset 0x0 (ie at vma
93 0x100) and "B" at offset 0x20 (ie at vma 0x120) the structures
94 would look like:
95
96| section name "A"
97| output_offset 0x00
98| size 0x20
99| output_section -----------> section name "O"
100| | vma 0x100
101| section name "B" | size 0x123
102| output_offset 0x20 |
103| size 0x103 |
104| output_section --------|
105
985fca12
SC
106
107*/
108
109
985fca12 110#include "bfd.h"
cbdc7909 111#include "sysdep.h"
985fca12
SC
112#include "libbfd.h"
113
4a96bc04
SC
114/*
115INODE
116 typedef asection, section prototypes, Section Output, Sections
117SUBSECTION
118 typedef asection
119
120SUBSECTION
121 typedef asection
122
123 The shape of a section struct:
124
125CODE_FRAGMENT
126.
127.typedef struct sec
128.{
129. {* The name of the section, the name isn't a copy, the pointer is
130. the same as that passed to bfd_make_section. *}
131.
132. CONST char *name;
133.
134. {* The next section in the list belonging to the BFD, or NULL. *}
135.
136. struct sec *next;
137.
138. {* The field flags contains attributes of the section. Some of
139. flags are read in from the object file, and some are
140. synthesized from other information. *}
141.
142. flagword flags;
143.
144.#define SEC_NO_FLAGS 0x000
145.
146. {* Tells the OS to allocate space for this section when loaded.
147. This would clear for a section containing debug information
148. only. *}
149.
150.
151.#define SEC_ALLOC 0x001
152. {* Tells the OS to load the section from the file when loading.
153. This would be clear for a .bss section *}
154.
155.#define SEC_LOAD 0x002
156. {* The section contains data still to be relocated, so there will
157. be some relocation information too. *}
158.
159.#define SEC_RELOC 0x004
160.
161. {* Obsolete ? *}
162.
163.#define SEC_BALIGN 0x008
164.
165. {* A signal to the OS that the section contains read only
166. data. *}
167.#define SEC_READONLY 0x010
168.
169. {* The section contains code only. *}
170.
171.#define SEC_CODE 0x020
172.
173. {* The section contains data only. *}
174.
175.#define SEC_DATA 0x040
176.
177. {* The section will reside in ROM. *}
178.
179.#define SEC_ROM 0x080
180.
181. {* The section contains constructor information. This section
182. type is used by the linker to create lists of constructors and
183. destructors used by <<g++>>. When a back end sees a symbol
184. which should be used in a constructor list, it creates a new
185. section for the type of name (eg <<__CTOR_LIST__>>), attaches
186. the symbol to it and builds a relocation. To build the lists
187. of constructors, all the linker has to to is catenate all the
188. sections called <<__CTOR_LIST__>> and relocte the data
189. contained within - exactly the operations it would peform on
190. standard data. *}
191.
192.#define SEC_CONSTRUCTOR 0x100
193.
194. {* The section is a constuctor, and should be placed at the
195. end of the . *}
196.
197.
198.#define SEC_CONSTRUCTOR_TEXT 0x1100
199.
200.#define SEC_CONSTRUCTOR_DATA 0x2100
201.
202.#define SEC_CONSTRUCTOR_BSS 0x3100
203.
204.
205. {* The section has contents - a bss section could be
206. <<SEC_ALLOC>> | <<SEC_HAS_CONTENTS>>, a debug section could be
207. <<SEC_HAS_CONTENTS>> *}
208.
209.#define SEC_HAS_CONTENTS 0x200
210.
211. {* An instruction to the linker not to output sections
212. containing this flag even if they have information which
213. would normally be written. *}
214.
215.#define SEC_NEVER_LOAD 0x400
216.
217. {* The base address of the section in the address space of the
218. target. *}
219.
220. bfd_vma vma;
221.
222. {* The size of the section in bytes of the loaded section. This
223. contains a value even if the section has no contents (eg, the
224. size of <<.bss>>). *}
225.
226. bfd_size_type size;
227.
228. {* If this section is going to be output, then this value is the
229. offset into the output section of the first byte in the input
230. section. Eg, if this was going to start at the 100th byte in
231. the output section, this value would be 100. *}
232.
233. bfd_vma output_offset;
234.
235. {* The output section through which to map on output. *}
236.
237. struct sec *output_section;
238.
239. {* The alignment requirement of the section, as an exponent - eg
240. 3 aligns to 2^3 (or 8) *}
241.
242. unsigned int alignment_power;
243.
244. {* If an input section, a pointer to a vector of relocation
245. records for the data in this section. *}
246.
247. struct reloc_cache_entry *relocation;
248.
249. {* If an output section, a pointer to a vector of pointers to
250. relocation records for the data in this section. *}
251.
252. struct reloc_cache_entry **orelocation;
253.
254. {* The number of relocation records in one of the above *}
255.
256. unsigned reloc_count;
257.
258. {* Which section is it 0.nth *}
259.
260. int index;
261.
262. {* Information below is back end specific - and not always used
263. or updated
264.
265. File position of section data *}
266.
267. file_ptr filepos;
268.
269. {* File position of relocation info *}
270.
271. file_ptr rel_filepos;
272.
273. {* File position of line data *}
274.
275. file_ptr line_filepos;
276.
277. {* Pointer to data for applications *}
278.
279. PTR userdata;
280.
281. struct lang_output_section *otheruserdata;
282.
283. {* Attached line number information *}
284.
285. alent *lineno;
286.
287. {* Number of line number records *}
288.
289. unsigned int lineno_count;
290.
291. {* When a section is being output, this value changes as more
292. linenumbers are written out *}
293.
294. file_ptr moving_line_filepos;
295.
296. {* what the section number is in the target world *}
297.
298. unsigned int target_index;
299.
300. PTR used_by_bfd;
301.
302. {* If this is a constructor section then here is a list of the
303. relocations created to relocate items within it. *}
304.
305. struct relent_chain *constructor_chain;
306.
307. {* The BFD which owns the section. *}
308.
309. bfd *owner;
310.
311.} asection ;
985fca12
SC
312*/
313
4a96bc04
SC
314/*
315INODE
316 section prototypes, , typedef asection, Sections
985fca12 317
4a96bc04
SC
318SUBSECTION
319 section prototypes
985fca12 320
4a96bc04
SC
321These are the functions exported by the section handling part of
322<<libbfd>.
323*/
985fca12 324
4a96bc04
SC
325/*
326FUNCTION
327 bfd_get_section_by_name
985fca12 328
4a96bc04
SC
329SYNOPSIS
330 asection *bfd_get_section_by_name(bfd *abfd, CONST char *name);
985fca12 331
4a96bc04
SC
332DESCRIPTION
333 Runs through the provided @var{abfd} and returns the
334 <<asection>> who's name matches that provided, otherwise NULL.
335 @xref{Sections}, for more information.
985fca12 336
4a96bc04 337*/
985fca12 338
4a96bc04
SC
339asection *
340DEFUN(bfd_get_section_by_name,(abfd, name),
341 bfd *abfd AND
342 CONST char *name)
343{
344 asection *sect;
985fca12 345
4a96bc04
SC
346 for (sect = abfd->sections; sect != NULL; sect = sect->next)
347 if (!strcmp (sect->name, name)) return sect;
348 return NULL;
349}
985fca12 350
985fca12 351
4a96bc04
SC
352/*
353FUNCTION
354 bfd_make_section_old_way
985fca12 355
4a96bc04
SC
356SYNOPSIS
357 asection *bfd_make_section_old_way(bfd *, CONST char *name);
985fca12 358
4a96bc04
SC
359DESCRIPTION
360 This function creates a new empty section called @var{name}
361 and attaches it to the end of the chain of sections for the
362 BFD supplied. An attempt to create a section with a name which
363 is already in use, returns its pointer without changing the
364 section chain.
985fca12 365
4a96bc04
SC
366 It has the funny name since this is the way it used to be
367 before is was rewritten...
985fca12 368
4a96bc04
SC
369 Possible errors are:
370 o invalid_operation
371 If output has already started for this BFD.
372 o no_memory
373 If obstack alloc fails.
985fca12
SC
374
375*/
376
985fca12 377
985fca12 378asection *
4a96bc04 379DEFUN(bfd_make_section_old_way,(abfd, name),
985fca12 380 bfd *abfd AND
4a96bc04 381 CONST char * name)
985fca12 382{
4a96bc04
SC
383 asection *sec = bfd_get_section_by_name(abfd, name);
384 if (sec == (asection *)NULL)
385 {
386 sec = bfd_make_section(abfd, name);
387 }
388 return sec;
985fca12
SC
389}
390
391
4a96bc04
SC
392/*
393FUNCTION
394 bfd_make_section
985fca12 395
4a96bc04
SC
396SYNOPSIS
397 asection * bfd_make_section(bfd *, CONST char *name);
985fca12 398
4a96bc04
SC
399DESCRIPTION
400 This function creates a new empty section called @var{name}
401 and attaches it to the end of the chain of sections for the
402 BFD supplied. An attempt to create a section with a name which
403 is already in use, returns NULL without changing the section
404 chain.
405
406 Possible errors are:
407 o invalid_operation - If output has already started for this BFD.
408 o no_memory - If obstack alloc fails.
985fca12
SC
409*/
410
411
412
413sec_ptr
414DEFUN(bfd_make_section,(abfd, name),
415 bfd *abfd AND
416 CONST char * name)
417{
418 asection *newsect;
419 asection ** prev = &abfd->sections;
420 asection * sect = abfd->sections;
421
422 if (abfd->output_has_begun) {
423 bfd_error = invalid_operation;
424 return NULL;
425 }
426
427 while (sect) {
cbdc7909 428 if (!strcmp(sect->name, name)) return NULL;
985fca12
SC
429 prev = &sect->next;
430 sect = sect->next;
431 }
432
433 newsect = (asection *) bfd_zalloc(abfd, sizeof (asection));
434 if (newsect == NULL) {
435 bfd_error = no_memory;
436 return NULL;
437 }
438
439 newsect->name = name;
440 newsect->index = abfd->section_count++;
441 newsect->flags = SEC_NO_FLAGS;
442
443 newsect->userdata = 0;
444 newsect->next = (asection *)NULL;
445 newsect->relocation = (arelent *)NULL;
446 newsect->reloc_count = 0;
447 newsect->line_filepos =0;
448 newsect->owner = abfd;
449 if (BFD_SEND (abfd, _new_section_hook, (abfd, newsect)) != true) {
450 free (newsect);
451 return NULL;
452 }
453
454 *prev = newsect;
455 return newsect;
456}
457
458
4a96bc04
SC
459/*
460FUNCTION
461 bfd_set_section_flags
462
463SYNOPSIS
464 boolean bfd_set_section_flags(bfd *, asection *, flagword);
465
466DESCRIPTION
467 Attempts to set the attributes of the section named in the BFD
468 supplied to the value. Returns true on success, false on
469 error. Possible error returns are:
470
471 o invalid operation
472 The section cannot have one or more of the attributes
473 requested. For example, a .bss section in <<a.out>> may not
474 have the <<SEC_HAS_CONTENTS>> field set.
985fca12 475
985fca12
SC
476*/
477
478boolean
479DEFUN(bfd_set_section_flags,(abfd, section, flags),
480 bfd *abfd AND
481 sec_ptr section AND
482 flagword flags)
483{
484 if ((flags & bfd_applicable_section_flags (abfd)) != flags) {
485 bfd_error = invalid_operation;
486 return false;
487 }
488
489 section->flags = flags;
490 return true;
491}
492
493
4a96bc04
SC
494/*
495FUNCTION
496 bfd_map_over_sections
497
498SYNOPSIS
499 void bfd_map_over_sections(bfd *abfd, void (*func)(), PTR obj);
985fca12 500
4a96bc04
SC
501DESCRIPTION
502 Calls the provided function @var{func} for each section
503 attached to the BFD @var{abfd}, passing @var{obj} as an
504 argument. The function will be called as if by
985fca12 505
4a96bc04 506| func(abfd, the_section, obj);
985fca12 507
4a96bc04
SC
508 This is the prefered method for iterating over sections, an
509 alternative would be to use a loop:
510
511| section *p;
512| for (p = abfd->sections; p != NULL; p = p->next)
513| func(abfd, p, ...)
985fca12 514
985fca12 515
985fca12
SC
516*/
517
518/*VARARGS2*/
519void
520DEFUN(bfd_map_over_sections,(abfd, operation, user_storage),
521 bfd *abfd AND
522 void (*operation)() AND
523 PTR user_storage)
524{
525 asection *sect;
526 int i = 0;
527
528 for (sect = abfd->sections; sect != NULL; i++, sect = sect->next)
529 (*operation) (abfd, sect, user_storage);
530
531 if (i != abfd->section_count) /* Debugging */
532 abort();
533}
534
535
4a96bc04
SC
536/*
537FUNCTION
538 bfd_set_section_size
539
540SYNOPSIS
541 boolean bfd_set_section_size(bfd *, asection *, bfd_size_type val);
985fca12 542
4a96bc04
SC
543DESCRIPTION
544 Sets @var{section} to the size @var{val}. If the operation is
545 ok, then <<true>> is returned, else <<false>>.
546
547 Possible error returns:
548 o invalid_operation
549 Writing has started to the BFD, so setting the size is invalid
985fca12 550
985fca12
SC
551*/
552
553boolean
554DEFUN(bfd_set_section_size,(abfd, ptr, val),
555 bfd *abfd AND
556 sec_ptr ptr AND
6724ff46 557 bfd_size_type val)
985fca12
SC
558{
559 /* Once you've started writing to any section you cannot create or change
560 the size of any others. */
561
562 if (abfd->output_has_begun) {
563 bfd_error = invalid_operation;
564 return false;
565 }
566
567 ptr->size = val;
568
569 return true;
570}
571
4a96bc04
SC
572/*
573FUNCTION
574 bfd_set_section_contents
575
576SYNOPSIS
577 boolean bfd_set_section_contents
985fca12
SC
578 (bfd *abfd,
579 asection *section,
580 PTR data,
581 file_ptr offset,
4a96bc04
SC
582 bfd_size_type count);
583
584
585DESCRIPTION
586 Sets the contents of the section @var{section} in BFD
587 @var{abfd} to the data starting in memory at @var{data}. The
588 data is written to the output section starting at offset
589 @var{offset} for @var{count} bytes.
590
591 Normally <<true>> is returned, else <<false>>. Possible error
592 returns are:
593 o no_contents
594 The output section does not have the <<SEC_HAS_CONTENTS>>
595 attribute, so nothing can be written to it.
596 o and some more too
597
598 This routine is front end to the back end function
599 <<_bfd_set_section_contents>>.
600
985fca12
SC
601
602*/
603
604boolean
605DEFUN(bfd_set_section_contents,(abfd, section, location, offset, count),
606 bfd *abfd AND
607 sec_ptr section AND
608 PTR location AND
609 file_ptr offset AND
610 bfd_size_type count)
611{
612 if (!(bfd_get_section_flags(abfd, section) & SEC_HAS_CONTENTS))
613 {
614 bfd_error = no_contents;
615 return(false);
616 }
617
618 if (BFD_SEND (abfd, _bfd_set_section_contents,
619 (abfd, section, location, offset, count)))
620 {
621 abfd->output_has_begun = true;
622 return true;
623 }
624
625 return false;
626}
627
4a96bc04
SC
628/*
629FUNCTION
630 bfd_get_section_contents
985fca12 631
4a96bc04
SC
632SYNOPSIS
633 boolean bfd_get_section_contents
634 (bfd *abfd, asection *section, PTR location,
635 file_ptr offset, bfd_size_type count);
985fca12 636
4a96bc04
SC
637DESCRIPTION
638 This function reads data from @var{section} in BFD @var{abfd}
639 into memory starting at @var{location}. The data is read at an
640 offset of @var{offset} from the start of the input section,
641 and is read for @var{count} bytes.
985fca12 642
4a96bc04
SC
643 If the contents of a constuctor with the <<SEC_CONSTUCTOR>>
644 flag set are requested, then the @var{location} is filled with
645 zeroes. If no errors occur, <<true>> is returned, else
646 <<false>>.
985fca12 647
985fca12
SC
648
649
650*/
651boolean
652DEFUN(bfd_get_section_contents,(abfd, section, location, offset, count),
653 bfd *abfd AND
654 sec_ptr section AND
655 PTR location AND
656 file_ptr offset AND
657 bfd_size_type count)
658{
659 if (section->flags & SEC_CONSTRUCTOR)
660 {
661 memset(location, 0, (unsigned)count);
662 return true;
663 }
664 else
665 {
666 return (BFD_SEND (abfd, _bfd_get_section_contents,
667 (abfd, section, location, offset, count)));
668 }
669}
670
This page took 0.093697 seconds and 4 git commands to generate.