daily update
[deliverable/binutils-gdb.git] / bfd / section.c
CommitLineData
252b5132 1/* Object file "section" support for the BFD library.
7898deda 2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
9e7b37b3 3 2000, 2001, 2002
252b5132
RH
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
6
7This file is part of BFD, the Binary File Descriptor library.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
23/*
24SECTION
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
41INODE
42Section Input, Section Output, Sections, Sections
43SUBSECTION
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
74INODE
75Section Output, typedef asection, Section Input, Sections
76
77SUBSECTION
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
252b5132
RH
115SUBSECTION
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
252b5132
RH
136#include "bfd.h"
137#include "sysdep.h"
138#include "libbfd.h"
139#include "bfdlink.h"
140
141/*
142DOCDD
143INODE
144typedef asection, section prototypes, Section Output, Sections
145SUBSECTION
146 typedef asection
147
148 Here is the section structure:
149
150CODE_FRAGMENT
151.
52b219b5
AM
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. *}
022a5af4
ILT
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;
022a5af4
ILT
167.};
168.
252b5132
RH
169.typedef struct sec
170.{
52b219b5
AM
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. *}
52b219b5
AM
173. const char *name;
174.
175. {* A unique sequence number. *}
52b219b5 176. int id;
252b5132 177.
dbb410c3 178. {* Which section in the bfd; 0..n-1 as sections are created in a bfd. *}
52b219b5 179. int index;
252b5132 180.
52b219b5 181. {* The next section in the list belonging to the BFD, or NULL. *}
52b219b5 182. struct sec *next;
252b5132 183.
52b219b5
AM
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. *}
52b219b5 187. flagword flags;
252b5132
RH
188.
189.#define SEC_NO_FLAGS 0x000
190.
52b219b5
AM
191. {* Tells the OS to allocate space for this section when loading.
192. This is clear for a section containing debug information only. *}
252b5132
RH
193.#define SEC_ALLOC 0x001
194.
52b219b5
AM
195. {* Tells the OS to load the section from the file when loading.
196. This is clear for a .bss section. *}
252b5132
RH
197.#define SEC_LOAD 0x002
198.
52b219b5
AM
199. {* The section contains data still to be relocated, so there is
200. some relocation information too. *}
252b5132
RH
201.#define SEC_RELOC 0x004
202.
65db3b0d
RH
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
252b5132 208.
52b219b5 209. {* A signal to the OS that the section contains read only data. *}
252b5132
RH
210.#define SEC_READONLY 0x010
211.
52b219b5 212. {* The section contains code only. *}
252b5132
RH
213.#define SEC_CODE 0x020
214.
52b219b5 215. {* The section contains data only. *}
252b5132
RH
216.#define SEC_DATA 0x040
217.
52b219b5 218. {* The section will reside in ROM. *}
252b5132
RH
219.#define SEC_ROM 0x080
220.
52b219b5
AM
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. *}
252b5132
RH
231.#define SEC_CONSTRUCTOR 0x100
232.
52b219b5
AM
233. {* The section is a constructor, and should be placed at the
234. end of the text, data, or bss section(?). *}
252b5132
RH
235.#define SEC_CONSTRUCTOR_TEXT 0x1100
236.#define SEC_CONSTRUCTOR_DATA 0x2100
237.#define SEC_CONSTRUCTOR_BSS 0x3100
238.
52b219b5
AM
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>> *}
252b5132
RH
242.#define SEC_HAS_CONTENTS 0x200
243.
52b219b5
AM
244. {* An instruction to the linker to not output the section
245. even if it has information which would normally be written. *}
252b5132
RH
246.#define SEC_NEVER_LOAD 0x400
247.
52b219b5
AM
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. *}
252b5132
RH
257.#define SEC_COFF_SHARED_LIBRARY 0x800
258.
1bd91689
AM
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.
52b219b5
AM
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. *}
252b5132
RH
272.#define SEC_IS_COMMON 0x8000
273.
52b219b5
AM
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. *}
252b5132
RH
278.#define SEC_DEBUGGING 0x10000
279.
52b219b5
AM
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. *}
252b5132
RH
283.#define SEC_IN_MEMORY 0x20000
284.
52b219b5
AM
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. *}
252b5132
RH
288.#define SEC_EXCLUDE 0x40000
289.
dbb410c3
AM
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. *}
252b5132
RH
294.#define SEC_SORT_ENTRIES 0x80000
295.
52b219b5
AM
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. *}
252b5132
RH
300.#define SEC_LINK_ONCE 0x100000
301.
52b219b5
AM
302. {* If SEC_LINK_ONCE is set, this bitfield describes how the linker
303. should handle duplicate sections. *}
252b5132
RH
304.#define SEC_LINK_DUPLICATES 0x600000
305.
52b219b5
AM
306. {* This value for SEC_LINK_DUPLICATES means that duplicate
307. sections with the same name should simply be discarded. *}
252b5132
RH
308.#define SEC_LINK_DUPLICATES_DISCARD 0x0
309.
52b219b5
AM
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. *}
252b5132
RH
313.#define SEC_LINK_DUPLICATES_ONE_ONLY 0x200000
314.
52b219b5
AM
315. {* This value for SEC_LINK_DUPLICATES means that the linker
316. should warn if any duplicate sections are a different size. *}
252b5132
RH
317.#define SEC_LINK_DUPLICATES_SAME_SIZE 0x400000
318.
52b219b5
AM
319. {* This value for SEC_LINK_DUPLICATES means that the linker
320. should warn if any duplicate sections contain different
321. contents. *}
252b5132
RH
322.#define SEC_LINK_DUPLICATES_SAME_CONTENTS 0x600000
323.
52b219b5
AM
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. *}
252b5132
RH
328.#define SEC_LINKER_CREATED 0x800000
329.
52b219b5 330. {* This section should not be subject to garbage collection. *}
252b5132
RH
331.#define SEC_KEEP 0x1000000
332.
52b219b5
AM
333. {* This section contains "short" data, and should be placed
334. "near" the GP. *}
851edbaf 335.#define SEC_SMALL_DATA 0x2000000
0c3ff40b 336.
52b219b5
AM
337. {* This section contains data which may be shared with other
338. executables or shared objects. *}
bd826630
ILT
339.#define SEC_SHARED 0x4000000
340.
52b219b5
AM
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. *}
34cbe64e
TW
345.#define SEC_BLOCK 0x8000000
346.
52b219b5
AM
347. {* Conditionally link this section; do not link if there are no
348. references found to any symbol in the section. *}
34cbe64e
TW
349.#define SEC_CLINK 0x10000000
350.
2dd439c5
L
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.
dbb410c3
AM
360. {* This section contains data about section groups. *}
361.#define SEC_GROUP 0x80000000
362.
52b219b5 363. {* End of section flags. *}
252b5132 364.
52b219b5 365. {* Some internal packed boolean fields. *}
252b5132 366.
52b219b5
AM
367. {* See the vma field. *}
368. unsigned int user_set_vma : 1;
252b5132 369.
52b219b5
AM
370. {* Whether relocations have been processed. *}
371. unsigned int reloc_done : 1;
252b5132 372.
52b219b5
AM
373. {* A mark flag used by some of the linker backends. *}
374. unsigned int linker_mark : 1;
252b5132 375.
d1778b88 376. {* Another mark flag used by some of the linker backends. Set for
08da05b0 377. output sections that have an input section. *}
d1778b88
AM
378. unsigned int linker_has_input : 1;
379.
52b219b5
AM
380. {* A mark flag used by some linker backends for garbage collection. *}
381. unsigned int gc_mark : 1;
252b5132 382.
dbb410c3
AM
383. {* Used by the ELF code to mark sections which have been allocated
384. to segments. *}
bc67d8a6
NC
385. unsigned int segment_mark : 1;
386.
52b219b5 387. {* End of internal packed boolean fields. *}
252b5132 388.
52b219b5
AM
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). *}
52b219b5 395. bfd_vma vma;
252b5132 396.
52b219b5
AM
397. {* The load address of the section - where it would be in a
398. rom image; really only used for writing section header
b5f79c76 399. information. *}
52b219b5 400. bfd_vma lma;
252b5132 401.
52b219b5
AM
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. *}
52b219b5 405. bfd_size_type _cooked_size;
252b5132 406.
52b219b5
AM
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. *}
52b219b5 410. bfd_size_type _raw_size;
252b5132 411.
52b219b5
AM
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. *}
52b219b5 419. bfd_vma output_offset;
252b5132 420.
52b219b5 421. {* The output section through which to map on output. *}
52b219b5 422. struct sec *output_section;
252b5132 423.
52b219b5
AM
424. {* The alignment requirement of the section, as an exponent of 2 -
425. e.g., 3 aligns to 2^3 (or 8). *}
52b219b5 426. unsigned int alignment_power;
252b5132 427.
52b219b5
AM
428. {* If an input section, a pointer to a vector of relocation
429. records for the data in this section. *}
52b219b5 430. struct reloc_cache_entry *relocation;
252b5132 431.
52b219b5
AM
432. {* If an output section, a pointer to a vector of pointers to
433. relocation records for the data in this section. *}
52b219b5 434. struct reloc_cache_entry **orelocation;
252b5132 435.
b5f79c76 436. {* The number of relocation records in one of the above. *}
52b219b5 437. unsigned reloc_count;
252b5132 438.
52b219b5
AM
439. {* Information below is back end specific - and not always used
440. or updated. *}
252b5132 441.
52b219b5 442. {* File position of section data. *}
52b219b5 443. file_ptr filepos;
252b5132 444.
52b219b5 445. {* File position of relocation info. *}
52b219b5 446. file_ptr rel_filepos;
252b5132 447.
52b219b5 448. {* File position of line data. *}
52b219b5 449. file_ptr line_filepos;
252b5132 450.
52b219b5 451. {* Pointer to data for applications. *}
52b219b5 452. PTR userdata;
252b5132 453.
52b219b5
AM
454. {* If the SEC_IN_MEMORY flag is set, this points to the actual
455. contents. *}
456. unsigned char *contents;
252b5132 457.
52b219b5 458. {* Attached line number information. *}
52b219b5 459. alent *lineno;
252b5132 460.
52b219b5 461. {* Number of line number records. *}
52b219b5 462. unsigned int lineno_count;
252b5132 463.
2dd439c5 464. {* Entity size for merging purposes. *}
2dd439c5
L
465. unsigned int entsize;
466.
52b219b5 467. {* Optional information about a COMDAT entry; NULL if not COMDAT. *}
52b219b5 468. struct bfd_comdat_info *comdat;
022a5af4 469.
52b219b5
AM
470. {* When a section is being output, this value changes as more
471. linenumbers are written out. *}
52b219b5 472. file_ptr moving_line_filepos;
252b5132 473.
52b219b5 474. {* What the section number is in the target world. *}
52b219b5 475. int target_index;
252b5132 476.
52b219b5 477. PTR used_by_bfd;
252b5132 478.
52b219b5
AM
479. {* If this is a constructor section then here is a list of the
480. relocations created to relocate items within it. *}
52b219b5 481. struct relent_chain *constructor_chain;
252b5132 482.
52b219b5 483. {* The BFD which owns the section. *}
52b219b5 484. bfd *owner;
252b5132 485.
b5f79c76 486. {* A symbol which points at this section only. *}
52b219b5
AM
487. struct symbol_cache_entry *symbol;
488. struct symbol_cache_entry **symbol_ptr_ptr;
252b5132 489.
52b219b5
AM
490. struct bfd_link_order *link_order_head;
491. struct bfd_link_order *link_order_tail;
b5f79c76 492.} asection;
252b5132 493.
52b219b5
AM
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. *}
252b5132
RH
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.
b5f79c76 504.{* The absolute section. *}
252b5132
RH
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)
b5f79c76 508.{* Pointer to the undefined section. *}
252b5132
RH
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)
b5f79c76 512.{* Pointer to the common section. *}
252b5132
RH
513.extern const asection bfd_com_section;
514.#define bfd_com_section_ptr ((asection *) &bfd_com_section)
b5f79c76 515.{* Pointer to the indirect section. *}
252b5132
RH
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.
84c254c6
NC
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.
252b5132
RH
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) \
f6af82bd
AM
531. ((section)->reloc_done ? (abort (), (bfd_size_type) 1) \
532. : (section)->_raw_size)
252b5132 533.#define bfd_get_section_size_after_reloc(section) \
f6af82bd
AM
534. ((section)->reloc_done ? (section)->_cooked_size \
535. : (abort (), (bfd_size_type) 1))
9e7b37b3
AM
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.
252b5132
RH
562*/
563
22bc497d
ILT
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
252b5132
RH
576/* These symbols are global, not specific to any BFD. Therefore, anything
577 that tries to change them is broken, and should be repaired. */
22bc497d 578
252b5132
RH
579static const asymbol global_syms[] =
580{
22bc497d
ILT
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)
252b5132
RH
585};
586
bc67d8a6
NC
587#define STD_SECTION(SEC, FLAGS, SYM, NAME, IDX) \
588 const asymbol * const SYM = (asymbol *) &global_syms[IDX]; \
589 const asection SEC = \
3df7b4e2
AM
590 /* name, id, index, next, flags, user_set_vma, reloc_done, */ \
591 { NAME, IDX, 0, NULL, FLAGS, 0, 0, \
52b219b5 592 \
d1778b88
AM
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, \
52b219b5 598 \
d1778b88
AM
599 /* output_offset, output_section, alignment_power, */ \
600 0, (struct sec *) &SEC, 0, \
52b219b5
AM
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 \
862517b6
AM
608 /* entsize, comdat, moving_line_filepos, */ \
609 0, NULL, 0, \
52b219b5 610 \
767e4b0d
JJ
611 /* target_index, used_by_bfd, constructor_chain, owner, */ \
612 0, NULL, NULL, NULL, \
52b219b5
AM
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 \
022a5af4 622 }
252b5132
RH
623
624STD_SECTION (bfd_com_section, SEC_IS_COMMON, bfd_com_symbol,
625 BFD_COM_SECTION_NAME, 0);
626STD_SECTION (bfd_und_section, 0, bfd_und_symbol, BFD_UND_SECTION_NAME, 1);
627STD_SECTION (bfd_abs_section, 0, bfd_abs_symbol, BFD_ABS_SECTION_NAME, 2);
628STD_SECTION (bfd_ind_section, 0, bfd_ind_symbol, BFD_IND_SECTION_NAME, 3);
629#undef STD_SECTION
630
73e87d70
AM
631struct 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
639struct bfd_hash_entry *
640bfd_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
671static asection *bfd_section_init PARAMS ((bfd *, asection *));
672
673static asection *
674bfd_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;
73e87d70 682 newsect->owner = abfd;
73e87d70
AM
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
252b5132
RH
708/*
709DOCDD
710INODE
711section prototypes, , typedef asection, Sections
712SUBSECTION
713 Section prototypes
714
715These are the functions exported by the section handling part of BFD.
716*/
717
9e7b37b3
AM
718/*
719FUNCTION
720 bfd_section_list_clear
721
722SYNOPSIS
723 void bfd_section_list_clear (bfd *);
724
725DESCRIPTION
726 Clears the section list, and also resets the section count and
727 hash table entries.
728*/
729
730void
731bfd_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
252b5132
RH
741/*
742FUNCTION
743 bfd_get_section_by_name
744
745SYNOPSIS
52b219b5 746 asection *bfd_get_section_by_name(bfd *abfd, const char *name);
252b5132
RH
747
748DESCRIPTION
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
759asection *
760bfd_get_section_by_name (abfd, name)
761 bfd *abfd;
52b219b5 762 const char *name;
252b5132 763{
73e87d70
AM
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;
252b5132 769
252b5132
RH
770 return NULL;
771}
772
1bd91689
AM
773/*
774FUNCTION
775 bfd_get_unique_section_name
776
777SYNOPSIS
778 char *bfd_get_unique_section_name(bfd *abfd,
a966dba9 779 const char *templat,
1bd91689
AM
780 int *count);
781
782DESCRIPTION
783 Invent a section name that is unique in @var{abfd} by tacking
77cb06e9
AM
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.
1bd91689
AM
788*/
789
790char *
a966dba9 791bfd_get_unique_section_name (abfd, templat, count)
1bd91689 792 bfd *abfd;
a966dba9 793 const char *templat;
1bd91689
AM
794 int *count;
795{
796 int num;
797 unsigned int len;
798 char *sname;
799
a966dba9 800 len = strlen (templat);
dc810e39 801 sname = bfd_malloc ((bfd_size_type) len + 8);
b3ea3584
AM
802 if (sname == NULL)
803 return NULL;
a966dba9 804 strcpy (sname, templat);
1bd91689
AM
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 ();
77cb06e9 814 sprintf (sname + len, ".%d", num++);
1bd91689 815 }
73e87d70 816 while (section_hash_lookup (&abfd->section_htab, sname, false, false));
1bd91689
AM
817
818 if (count != NULL)
819 *count = num;
820 return sname;
821}
822
252b5132
RH
823/*
824FUNCTION
825 bfd_make_section_old_way
826
827SYNOPSIS
52b219b5 828 asection *bfd_make_section_old_way(bfd *abfd, const char *name);
252b5132
RH
829
830DESCRIPTION
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
252b5132
RH
848asection *
849bfd_make_section_old_way (abfd, name)
850 bfd *abfd;
52b219b5 851 const char *name;
252b5132 852{
73e87d70
AM
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)
252b5132 880 {
73e87d70
AM
881 /* Section already exists. */
882 return newsect;
252b5132 883 }
73e87d70
AM
884
885 newsect->name = name;
886 return bfd_section_init (abfd, newsect);
252b5132
RH
887}
888
889/*
890FUNCTION
891 bfd_make_section_anyway
892
893SYNOPSIS
52b219b5 894 asection *bfd_make_section_anyway(bfd *abfd, const char *name);
252b5132
RH
895
896DESCRIPTION
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
906sec_ptr
907bfd_make_section_anyway (abfd, name)
908 bfd *abfd;
52b219b5 909 const char *name;
252b5132 910{
73e87d70 911 struct section_hash_entry *sh;
252b5132 912 asection *newsect;
252b5132
RH
913
914 if (abfd->output_has_begun)
915 {
916 bfd_set_error (bfd_error_invalid_operation);
917 return NULL;
918 }
919
73e87d70
AM
920 sh = section_hash_lookup (&abfd->section_htab, name, true, false);
921 if (sh == NULL)
252b5132
RH
922 return NULL;
923
73e87d70
AM
924 newsect = &sh->section;
925 if (newsect->name != NULL)
4d7ce4dd 926 {
73e87d70
AM
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;
252b5132
RH
934 }
935
73e87d70
AM
936 newsect->name = name;
937 return bfd_section_init (abfd, newsect);
252b5132
RH
938}
939
940/*
941FUNCTION
942 bfd_make_section
943
944SYNOPSIS
52b219b5 945 asection *bfd_make_section(bfd *, const char *name);
252b5132
RH
946
947DESCRIPTION
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
954asection *
955bfd_make_section (abfd, name)
956 bfd *abfd;
52b219b5 957 const char *name;
252b5132 958{
73e87d70
AM
959 struct section_hash_entry *sh;
960 asection *newsect;
252b5132 961
73e87d70 962 if (abfd->output_has_begun)
252b5132 963 {
73e87d70
AM
964 bfd_set_error (bfd_error_invalid_operation);
965 return NULL;
252b5132
RH
966 }
967
73e87d70
AM
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;
252b5132 973
73e87d70
AM
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)
252b5132 980 {
73e87d70
AM
981 /* Section already exists. */
982 return newsect;
252b5132
RH
983 }
984
73e87d70
AM
985 newsect->name = name;
986 return bfd_section_init (abfd, newsect);
252b5132
RH
987}
988
252b5132
RH
989/*
990FUNCTION
991 bfd_set_section_flags
992
993SYNOPSIS
994 boolean bfd_set_section_flags(bfd *abfd, asection *sec, flagword flags);
995
996DESCRIPTION
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*/
1009boolean
1010bfd_set_section_flags (abfd, section, flags)
7442e600 1011 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
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
252b5132
RH
1032/*
1033FUNCTION
1034 bfd_map_over_sections
1035
1036SYNOPSIS
1037 void bfd_map_over_sections(bfd *abfd,
7b82c249 1038 void (*func) (bfd *abfd,
252b5132
RH
1039 asection *sect,
1040 PTR obj),
1041 PTR obj);
1042
1043DESCRIPTION
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
252b5132
RH
1057*/
1058
1059/*VARARGS2*/
1060void
1061bfd_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
252b5132
RH
1076/*
1077FUNCTION
1078 bfd_set_section_size
1079
1080SYNOPSIS
1081 boolean bfd_set_section_size(bfd *abfd, asection *sec, bfd_size_type val);
1082
1083DESCRIPTION
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
1093boolean
1094bfd_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
7b82c249 1100 the size of any others. */
252b5132
RH
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/*
1115FUNCTION
1116 bfd_set_section_contents
1117
1118SYNOPSIS
dc810e39
AM
1119 boolean bfd_set_section_contents (bfd *abfd, asection *section,
1120 PTR data, file_ptr offset,
1121 bfd_size_type count);
252b5132 1122
252b5132
RH
1123DESCRIPTION
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
9a968f43 1127 @var{offset} for @var{count} octets.
252b5132 1128
252b5132
RH
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
252b5132
RH
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
1146boolean
1147bfd_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
dc810e39
AM
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)
252b5132 1167 {
252b5132
RH
1168 bfd_set_error (bfd_error_bad_value);
1169 return false;
1170 }
252b5132
RH
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
9a951beb
RH
1190 /* Record a copy of the data in memory if desired. */
1191 if (section->contents
1192 && location != section->contents + offset)
dc810e39 1193 memcpy (section->contents + offset, location, (size_t) count);
9a951beb 1194
252b5132
RH
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/*
1206FUNCTION
1207 bfd_get_section_contents
1208
1209SYNOPSIS
dc810e39
AM
1210 boolean bfd_get_section_contents (bfd *abfd, asection *section,
1211 PTR location, file_ptr offset,
1212 bfd_size_type count);
252b5132
RH
1213
1214DESCRIPTION
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
252b5132
RH
1226*/
1227boolean
1228bfd_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 {
dc810e39 1239 memset (location, 0, (size_t) count);
252b5132
RH
1240 return true;
1241 }
1242
dc810e39
AM
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)
252b5132 1250 {
252b5132
RH
1251 bfd_set_error (bfd_error_bad_value);
1252 return false;
1253 }
252b5132
RH
1254
1255 if (count == 0)
1256 /* Don't bother. */
1257 return true;
1258
1259 if ((section->flags & SEC_HAS_CONTENTS) == 0)
1260 {
dc810e39 1261 memset (location, 0, (size_t) count);
252b5132
RH
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/*
1276FUNCTION
1277 bfd_copy_private_section_data
1278
1279SYNOPSIS
dc810e39
AM
1280 boolean bfd_copy_private_section_data (bfd *ibfd, asection *isec,
1281 bfd *obfd, asection *osec);
252b5132
RH
1282
1283DESCRIPTION
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/*
1298FUNCTION
1299 _bfd_strip_section_from_output
1300
1301SYNOPSIS
1302 void _bfd_strip_section_from_output
7f8d5fc9 1303 (struct bfd_link_info *info, asection *section);
252b5132
RH
1304
1305DESCRIPTION
7f8d5fc9
ILT
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.
252b5132
RH
1310*/
1311void
7f8d5fc9
ILT
1312_bfd_strip_section_from_output (info, s)
1313 struct bfd_link_info *info;
252b5132
RH
1314 asection *s;
1315{
1316 asection **spp, *os;
1317 struct bfd_link_order *p, *pp;
7f8d5fc9 1318 boolean keep_os;
252b5132 1319
7f8d5fc9
ILT
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 */
252b5132 1325 os = s->output_section;
2f484710
HPN
1326
1327 /* Handle a section that wasn't output. */
1328 if (os == NULL)
1329 return;
1330
252b5132
RH
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 {
252b5132
RH
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;
9d7428a9
RH
1341 break;
1342 }
252b5132 1343
7f8d5fc9
ILT
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 {
25263aad
JJ
1354 if (is != s && is->output_section == os
1355 && (is->flags & SEC_EXCLUDE) == 0)
7f8d5fc9
ILT
1356 break;
1357 }
1358 if (is != NULL)
1359 break;
1360 }
1361 if (abfd != NULL)
1362 keep_os = true;
1363 }
1364
0bde07d4 1365 /* If the output section is empty, remove it too. Careful about sections
52b219b5 1366 that have been discarded in the link script -- they are mapped to
0bde07d4 1367 bfd_abs_section, which has no owner. */
7f8d5fc9 1368 if (!keep_os && os->owner != NULL)
9d7428a9
RH
1369 {
1370 for (spp = &os->owner->sections; *spp; spp = &(*spp)->next)
1371 if (*spp == os)
252b5132 1372 {
9e7b37b3 1373 bfd_section_list_remove (os->owner, spp);
9d7428a9
RH
1374 os->owner->section_count--;
1375 break;
252b5132 1376 }
9d7428a9 1377 }
25263aad
JJ
1378
1379 s->flags |= SEC_EXCLUDE;
252b5132 1380}
This page took 0.193157 seconds and 4 git commands to generate.