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