]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/section.c
2.41 Release sources
[thirdparty/binutils-gdb.git] / bfd / section.c
CommitLineData
252b5132 1/* Object file "section" support for the BFD library.
d87bef3a 2 Copyright (C) 1990-2023 Free Software Foundation, Inc.
252b5132
RH
3 Written by Cygnus Support.
4
cd123cb7
NC
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
252b5132
RH
21
22/*
23SECTION
24 Sections
25
26 The raw data contained within a BFD is maintained through the
27 section abstraction. A single BFD may have any number of
28 sections. It keeps hold of them by pointing to the first;
29 each one points to the next in the list.
30
31 Sections are supported in BFD in <<section.c>>.
32
33@menu
34@* Section Input::
35@* Section Output::
36@* typedef asection::
37@* section prototypes::
38@end menu
39
40INODE
41Section Input, Section Output, Sections, Sections
42SUBSECTION
43 Section input
44
45 When a BFD is opened for reading, the section structures are
46 created and attached to the BFD.
47
48 Each section has a name which describes the section in the
49 outside world---for example, <<a.out>> would contain at least
50 three sections, called <<.text>>, <<.data>> and <<.bss>>.
51
52 Names need not be unique; for example a COFF file may have several
53 sections named <<.data>>.
54
55 Sometimes a BFD will contain more than the ``natural'' number of
56 sections. A back end may attach other sections containing
57 constructor data, or an application may add a section (using
58 <<bfd_make_section>>) to the sections attached to an already open
59 BFD. For example, the linker creates an extra section
60 <<COMMON>> for each input file's BFD to hold information about
61 common storage.
62
63 The raw data is not necessarily read in when
64 the section descriptor is created. Some targets may leave the
65 data in place until a <<bfd_get_section_contents>> call is
66 made. Other back ends may read in all the data at once. For
67 example, an S-record file has to be read once to determine the
fdef3943 68 size of the data.
252b5132
RH
69
70INODE
71Section Output, typedef asection, Section Input, Sections
72
73SUBSECTION
74 Section output
75
76 To write a new object style BFD, the various sections to be
77 written have to be created. They are attached to the BFD in
78 the same way as input sections; data is written to the
79 sections using <<bfd_set_section_contents>>.
80
81 Any program that creates or combines sections (e.g., the assembler
82 and linker) must use the <<asection>> fields <<output_section>> and
83 <<output_offset>> to indicate the file sections to which each
84 section must be written. (If the section is being created from
85 scratch, <<output_section>> should probably point to the section
86 itself and <<output_offset>> should probably be zero.)
87
88 The data to be written comes from input sections attached
89 (via <<output_section>> pointers) to
90 the output sections. The output section structure can be
91 considered a filter for the input section: the output section
92 determines the vma of the output data and the name, but the
93 input section determines the offset into the output section of
94 the data to be written.
95
96 E.g., to create a section "O", starting at 0x100, 0x123 long,
97 containing two subsections, "A" at offset 0x0 (i.e., at vma
98 0x100) and "B" at offset 0x20 (i.e., at vma 0x120) the <<asection>>
99 structures would look like:
100
101| section name "A"
102| output_offset 0x00
103| size 0x20
104| output_section -----------> section name "O"
105| | vma 0x100
106| section name "B" | size 0x123
107| output_offset 0x20 |
108| size 0x103 |
109| output_section --------|
110
252b5132
RH
111SUBSECTION
112 Link orders
113
114 The data within a section is stored in a @dfn{link_order}.
115 These are much like the fixups in <<gas>>. The link_order
116 abstraction allows a section to grow and shrink within itself.
117
118 A link_order knows how big it is, and which is the next
119 link_order and where the raw data for it is; it also points to
120 a list of relocations which apply to it.
121
122 The link_order is used by the linker to perform relaxing on
123 final code. The compiler creates code which is as big as
124 necessary to make it work without relaxing, and the user can
125 select whether to relax. Sometimes relaxing takes a lot of
126 time. The linker runs around the relocations to see if any
127 are attached to data which can be shrunk, if so it does it on
128 a link_order by link_order basis.
129
130*/
131
252b5132 132#include "sysdep.h"
3db64b00 133#include "bfd.h"
252b5132
RH
134#include "libbfd.h"
135#include "bfdlink.h"
136
137/*
138DOCDD
139INODE
717d4bd6 140 typedef asection, section prototypes, Section Output, Sections
252b5132
RH
141SUBSECTION
142 typedef asection
143
144 Here is the section structure:
145
717d4bd6
AM
146EXTERNAL
147.{* Linenumber stuff. *}
148.typedef struct lineno_cache_entry
149.{
150. unsigned int line_number; {* Linenumber from start of function. *}
151. union
152. {
153. struct bfd_symbol *sym; {* Function name. *}
154. bfd_vma offset; {* Offset into section. *}
155. } u;
156.}
157.alent;
252b5132 158.
717d4bd6
AM
159
160CODE_FRAGMENT
198beae2 161.typedef struct bfd_section
252b5132 162.{
52b219b5
AM
163. {* The name of the section; the name isn't a copy, the pointer is
164. the same as that passed to bfd_make_section. *}
52b219b5
AM
165. const char *name;
166.
57594b6a
AM
167. {* The next section in the list belonging to the BFD, or NULL. *}
168. struct bfd_section *next;
169.
170. {* The previous section in the list belonging to the BFD, or NULL. *}
171. struct bfd_section *prev;
172.
52b219b5 173. {* A unique sequence number. *}
7292b3ac 174. unsigned int id;
252b5132 175.
a8c4d40b
L
176. {* A unique section number which can be used by assembler to
177. distinguish different sections with the same section name. *}
178. unsigned int section_id;
179.
dbb410c3 180. {* Which section in the bfd; 0..n-1 as sections are created in a bfd. *}
7292b3ac 181. unsigned int index;
252b5132 182.
52b219b5
AM
183. {* The field flags contains attributes of the section. Some
184. flags are read in from the object file, and some are
185. synthesized from other information. *}
52b219b5 186. flagword flags;
252b5132 187.
07d6d2b8 188.#define SEC_NO_FLAGS 0x0
252b5132 189.
52b219b5
AM
190. {* Tells the OS to allocate space for this section when loading.
191. This is clear for a section containing debug information only. *}
07d6d2b8 192.#define SEC_ALLOC 0x1
252b5132 193.
52b219b5
AM
194. {* Tells the OS to load the section from the file when loading.
195. This is clear for a .bss section. *}
07d6d2b8 196.#define SEC_LOAD 0x2
252b5132 197.
52b219b5
AM
198. {* The section contains data still to be relocated, so there is
199. some relocation information too. *}
07d6d2b8 200.#define SEC_RELOC 0x4
252b5132 201.
52b219b5 202. {* A signal to the OS that the section contains read only data. *}
07d6d2b8 203.#define SEC_READONLY 0x8
252b5132 204.
52b219b5 205. {* The section contains code only. *}
07d6d2b8 206.#define SEC_CODE 0x10
252b5132 207.
52b219b5 208. {* The section contains data only. *}
07d6d2b8 209.#define SEC_DATA 0x20
252b5132 210.
52b219b5 211. {* The section will reside in ROM. *}
07d6d2b8 212.#define SEC_ROM 0x40
252b5132 213.
52b219b5
AM
214. {* The section contains constructor information. This section
215. type is used by the linker to create lists of constructors and
216. destructors used by <<g++>>. When a back end sees a symbol
217. which should be used in a constructor list, it creates a new
218. section for the type of name (e.g., <<__CTOR_LIST__>>), attaches
219. the symbol to it, and builds a relocation. To build the lists
220. of constructors, all the linker has to do is catenate all the
221. sections called <<__CTOR_LIST__>> and relocate the data
222. contained within - exactly the operations it would peform on
223. standard data. *}
07d6d2b8 224.#define SEC_CONSTRUCTOR 0x80
252b5132 225.
52b219b5
AM
226. {* The section has contents - a data section could be
227. <<SEC_ALLOC>> | <<SEC_HAS_CONTENTS>>; a debug section could be
228. <<SEC_HAS_CONTENTS>> *}
07d6d2b8 229.#define SEC_HAS_CONTENTS 0x100
252b5132 230.
52b219b5
AM
231. {* An instruction to the linker to not output the section
232. even if it has information which would normally be written. *}
07d6d2b8 233.#define SEC_NEVER_LOAD 0x200
252b5132 234.
13ae64f3 235. {* The section contains thread local data. *}
07d6d2b8 236.#define SEC_THREAD_LOCAL 0x400
13ae64f3 237.
8772de11
MR
238. {* The section's size is fixed. Generic linker code will not
239. recalculate it and it is up to whoever has set this flag to
240. get the size right. *}
241.#define SEC_FIXED_SIZE 0x800
242.
52b219b5
AM
243. {* The section contains common symbols (symbols may be defined
244. multiple times, the value of a symbol is the amount of
245. space it requires, and the largest symbol value is the one
246. used). Most targets have exactly one of these (which we
247. translate to bfd_com_section_ptr), but ECOFF has two. *}
07d6d2b8 248.#define SEC_IS_COMMON 0x1000
252b5132 249.
52b219b5
AM
250. {* The section contains only debugging information. For
251. example, this is set for ELF .debug and .stab sections.
252. strip tests this flag to see if a section can be
253. discarded. *}
07d6d2b8 254.#define SEC_DEBUGGING 0x2000
252b5132 255.
52b219b5
AM
256. {* The contents of this section are held in memory pointed to
257. by the contents field. This is checked by bfd_get_section_contents,
258. and the data is retrieved from memory if appropriate. *}
07d6d2b8 259.#define SEC_IN_MEMORY 0x4000
252b5132 260.
52b219b5
AM
261. {* The contents of this section are to be excluded by the
262. linker for executable and shared objects unless those
263. objects are to be further relocated. *}
07d6d2b8 264.#define SEC_EXCLUDE 0x8000
252b5132 265.
dbb410c3
AM
266. {* The contents of this section are to be sorted based on the sum of
267. the symbol and addend values specified by the associated relocation
268. entries. Entries without associated relocation entries will be
269. appended to the end of the section in an unspecified order. *}
07d6d2b8 270.#define SEC_SORT_ENTRIES 0x10000
252b5132 271.
52b219b5
AM
272. {* When linking, duplicate sections of the same name should be
273. discarded, rather than being combined into a single section as
274. is usually done. This is similar to how common symbols are
275. handled. See SEC_LINK_DUPLICATES below. *}
07d6d2b8 276.#define SEC_LINK_ONCE 0x20000
252b5132 277.
52b219b5
AM
278. {* If SEC_LINK_ONCE is set, this bitfield describes how the linker
279. should handle duplicate sections. *}
07d6d2b8 280.#define SEC_LINK_DUPLICATES 0xc0000
252b5132 281.
52b219b5
AM
282. {* This value for SEC_LINK_DUPLICATES means that duplicate
283. sections with the same name should simply be discarded. *}
07d6d2b8 284.#define SEC_LINK_DUPLICATES_DISCARD 0x0
252b5132 285.
52b219b5
AM
286. {* This value for SEC_LINK_DUPLICATES means that the linker
287. should warn if there are any duplicate sections, although
288. it should still only link one copy. *}
07d6d2b8 289.#define SEC_LINK_DUPLICATES_ONE_ONLY 0x40000
252b5132 290.
52b219b5
AM
291. {* This value for SEC_LINK_DUPLICATES means that the linker
292. should warn if any duplicate sections are a different size. *}
f856272b 293.#define SEC_LINK_DUPLICATES_SAME_SIZE 0x80000
252b5132 294.
52b219b5
AM
295. {* This value for SEC_LINK_DUPLICATES means that the linker
296. should warn if any duplicate sections contain different
297. contents. *}
ebe372c1
L
298.#define SEC_LINK_DUPLICATES_SAME_CONTENTS \
299. (SEC_LINK_DUPLICATES_ONE_ONLY | SEC_LINK_DUPLICATES_SAME_SIZE)
252b5132 300.
52b219b5
AM
301. {* This section was created by the linker as part of dynamic
302. relocation or other arcane processing. It is skipped when
303. going through the first-pass output, trusting that someone
304. else up the line will take care of it later. *}
07d6d2b8 305.#define SEC_LINKER_CREATED 0x100000
252b5132 306.
a8c4d40b 307. {* This section contains a section ID to distinguish different
ef4627fa 308. sections with the same section name. *}
a8c4d40b
L
309.#define SEC_ASSEMBLER_SECTION_ID 0x100000
310.
a14a5de3
AM
311. {* This section should not be subject to garbage collection.
312. Also set to inform the linker that this section should not be
313. listed in the link map as discarded. *}
07d6d2b8 314.#define SEC_KEEP 0x200000
252b5132 315.
52b219b5
AM
316. {* This section contains "short" data, and should be placed
317. "near" the GP. *}
07d6d2b8 318.#define SEC_SMALL_DATA 0x400000
34cbe64e 319.
2dd439c5
L
320. {* Attempt to merge identical entities in the section.
321. Entity size is given in the entsize field. *}
07d6d2b8 322.#define SEC_MERGE 0x800000
2dd439c5
L
323.
324. {* If given with SEC_MERGE, entities to merge are zero terminated
325. strings where entsize specifies character size instead of fixed
326. size entries. *}
07d6d2b8 327.#define SEC_STRINGS 0x1000000
2dd439c5 328.
dbb410c3 329. {* This section contains data about section groups. *}
07d6d2b8 330.#define SEC_GROUP 0x2000000
ebe372c1
L
331.
332. {* The section is a COFF shared library section. This flag is
333. only for the linker. If this type of section appears in
334. the input file, the linker must copy it to the output file
335. without changing the vma or size. FIXME: Although this
336. was originally intended to be general, it really is COFF
337. specific (and the flag was renamed to indicate this). It
338. might be cleaner to have some more general mechanism to
339. allow the back end to control what the linker does with
340. sections. *}
07d6d2b8 341.#define SEC_COFF_SHARED_LIBRARY 0x4000000
ebe372c1 342.
310fd250
L
343. {* This input section should be copied to output in reverse order
344. as an array of pointers. This is for ELF linker internal use
345. only. *}
07d6d2b8 346.#define SEC_ELF_REVERSE_COPY 0x4000000
310fd250 347.
ebe372c1
L
348. {* This section contains data which may be shared with other
349. executables or shared objects. This is for COFF only. *}
07d6d2b8 350.#define SEC_COFF_SHARED 0x8000000
ebe372c1 351.
da16cc96
AM
352. {* Indicate that section has the purecode flag set. *}
353.#define SEC_ELF_PURECODE 0x8000000
0ce398f1 354.
ebe372c1
L
355. {* When a section with this flag is being linked, then if the size of
356. the input section is less than a page, it should not cross a page
357. boundary. If the size of the input section is one page or more,
358. it should be aligned on a page boundary. This is for TI
359. TMS320C54X only. *}
07d6d2b8 360.#define SEC_TIC54X_BLOCK 0x10000000
ebe372c1
L
361.
362. {* Conditionally link this section; do not link if there are no
363. references found to any symbol in the section. This is for TI
364. TMS320C54X only. *}
07d6d2b8 365.#define SEC_TIC54X_CLINK 0x20000000
dbb410c3 366.
9361e630 367. {* This section contains vliw code. This is for Toshiba MeP only. *}
07d6d2b8 368.#define SEC_MEP_VLIW 0x20000000
9361e630 369.
61826503
CE
370. {* All symbols, sizes and relocations in this section are octets
371. instead of bytes. Required for DWARF debug sections as DWARF
372. information is organized in octets, not bytes. *}
373.#define SEC_ELF_OCTETS 0x40000000
374.
156621f3
KT
375. {* Indicate that section has the no read flag set. This happens
376. when memory read flag isn't set. *}
07d6d2b8 377.#define SEC_COFF_NOREAD 0x40000000
156621f3 378.
52b219b5 379. {* End of section flags. *}
252b5132 380.
52b219b5 381. {* Some internal packed boolean fields. *}
252b5132 382.
52b219b5
AM
383. {* See the vma field. *}
384. unsigned int user_set_vma : 1;
252b5132 385.
52b219b5
AM
386. {* A mark flag used by some of the linker backends. *}
387. unsigned int linker_mark : 1;
252b5132 388.
d1778b88 389. {* Another mark flag used by some of the linker backends. Set for
08da05b0 390. output sections that have an input section. *}
d1778b88
AM
391. unsigned int linker_has_input : 1;
392.
9d0a14d3 393. {* Mark flag used by some linker backends for garbage collection. *}
52b219b5 394. unsigned int gc_mark : 1;
252b5132 395.
4a114e3e
L
396. {* Section compression status. *}
397. unsigned int compress_status : 2;
398.#define COMPRESS_SECTION_NONE 0
399.#define COMPRESS_SECTION_DONE 1
2cac01e3
FS
400.#define DECOMPRESS_SECTION_ZLIB 2
401.#define DECOMPRESS_SECTION_ZSTD 3
4a114e3e 402.
68bfbfcc
AM
403. {* The following flags are used by the ELF linker. *}
404.
405. {* Mark sections which have been allocated to segments. *}
bc67d8a6
NC
406. unsigned int segment_mark : 1;
407.
68bfbfcc
AM
408. {* Type of sec_info information. *}
409. unsigned int sec_info_type:3;
dbaa2011
AM
410.#define SEC_INFO_TYPE_NONE 0
411.#define SEC_INFO_TYPE_STABS 1
412.#define SEC_INFO_TYPE_MERGE 2
413.#define SEC_INFO_TYPE_EH_FRAME 3
414.#define SEC_INFO_TYPE_JUST_SYMS 4
5446cbdf 415.#define SEC_INFO_TYPE_TARGET 5
2f0c68f2 416.#define SEC_INFO_TYPE_EH_FRAME_ENTRY 6
cf0e0a0b 417.#define SEC_INFO_TYPE_SFRAME 7
68bfbfcc
AM
418.
419. {* Nonzero if this section uses RELA relocations, rather than REL. *}
420. unsigned int use_rela_p:1;
421.
4c52953f
AM
422. {* Bits used by various backends. The generic code doesn't touch
423. these fields. *}
68bfbfcc 424.
b0dddeec
AM
425. unsigned int sec_flg0:1;
426. unsigned int sec_flg1:1;
427. unsigned int sec_flg2:1;
428. unsigned int sec_flg3:1;
429. unsigned int sec_flg4:1;
430. unsigned int sec_flg5:1;
68bfbfcc 431.
52b219b5 432. {* End of internal packed boolean fields. *}
252b5132 433.
52b219b5
AM
434. {* The virtual memory address of the section - where it will be
435. at run time. The symbols are relocated against this. The
436. user_set_vma flag is maintained by bfd; if it's not set, the
437. backend can assign addresses (for example, in <<a.out>>, where
438. the default address for <<.data>> is dependent on the specific
439. target and various flags). *}
52b219b5 440. bfd_vma vma;
252b5132 441.
52b219b5
AM
442. {* The load address of the section - where it would be in a
443. rom image; really only used for writing section header
b5f79c76 444. information. *}
52b219b5 445. bfd_vma lma;
252b5132 446.
37b01f6a 447. {* The size of the section in *octets*, as it will be output.
52b219b5 448. Contains a value even if the section has no contents (e.g., the
eea6121a
AM
449. size of <<.bss>>). *}
450. bfd_size_type size;
451.
1a23a9e6 452. {* For input sections, the original size on disk of the section, in
73c5c7a8
BW
453. octets. This field should be set for any section whose size is
454. changed by linker relaxation. It is required for sections where
455. the linker relaxation scheme doesn't cache altered section and
456. reloc contents (stabs, eh_frame, SEC_MERGE, some coff relaxing
457. targets), and thus the original size needs to be kept to read the
458. section multiple times. For output sections, rawsize holds the
459. section size calculated on a previous linker relaxation pass. *}
eea6121a 460. bfd_size_type rawsize;
252b5132 461.
4a114e3e
L
462. {* The compressed size of the section in octets. *}
463. bfd_size_type compressed_size;
464.
52b219b5
AM
465. {* If this section is going to be output, then this value is the
466. offset in *bytes* into the output section of the first byte in the
467. input section (byte ==> smallest addressable unit on the
468. target). In most cases, if this was going to start at the
469. 100th octet (8-bit quantity) in the output section, this value
470. would be 100. However, if the target byte size is 16 bits
471. (bfd_octets_per_byte is "2"), this value would be 50. *}
52b219b5 472. bfd_vma output_offset;
252b5132 473.
52b219b5 474. {* The output section through which to map on output. *}
198beae2 475. struct bfd_section *output_section;
252b5132 476.
52b219b5
AM
477. {* If an input section, a pointer to a vector of relocation
478. records for the data in this section. *}
52b219b5 479. struct reloc_cache_entry *relocation;
252b5132 480.
52b219b5
AM
481. {* If an output section, a pointer to a vector of pointers to
482. relocation records for the data in this section. *}
52b219b5 483. struct reloc_cache_entry **orelocation;
252b5132 484.
b5f79c76 485. {* The number of relocation records in one of the above. *}
52b219b5 486. unsigned reloc_count;
252b5132 487.
57594b6a
AM
488. {* The alignment requirement of the section, as an exponent of 2 -
489. e.g., 3 aligns to 2^3 (or 8). *}
490. unsigned int alignment_power;
491.
52b219b5
AM
492. {* Information below is back end specific - and not always used
493. or updated. *}
252b5132 494.
52b219b5 495. {* File position of section data. *}
52b219b5 496. file_ptr filepos;
252b5132 497.
52b219b5 498. {* File position of relocation info. *}
52b219b5 499. file_ptr rel_filepos;
252b5132 500.
52b219b5 501. {* File position of line data. *}
52b219b5 502. file_ptr line_filepos;
252b5132 503.
52b219b5 504. {* Pointer to data for applications. *}
c58b9523 505. void *userdata;
252b5132 506.
52b219b5
AM
507. {* If the SEC_IN_MEMORY flag is set, this points to the actual
508. contents. *}
717d4bd6 509. bfd_byte *contents;
252b5132 510.
52b219b5 511. {* Attached line number information. *}
52b219b5 512. alent *lineno;
252b5132 513.
52b219b5 514. {* Number of line number records. *}
52b219b5 515. unsigned int lineno_count;
252b5132 516.
2dd439c5 517. {* Entity size for merging purposes. *}
2dd439c5
L
518. unsigned int entsize;
519.
f97b9cb8
L
520. {* Points to the kept section if this section is a link-once section,
521. and is discarded. *}
198beae2 522. struct bfd_section *kept_section;
f97b9cb8 523.
52b219b5
AM
524. {* When a section is being output, this value changes as more
525. linenumbers are written out. *}
52b219b5 526. file_ptr moving_line_filepos;
252b5132 527.
52b219b5 528. {* What the section number is in the target world. *}
52b219b5 529. int target_index;
252b5132 530.
c58b9523 531. void *used_by_bfd;
252b5132 532.
52b219b5
AM
533. {* If this is a constructor section then here is a list of the
534. relocations created to relocate items within it. *}
52b219b5 535. struct relent_chain *constructor_chain;
252b5132 536.
52b219b5 537. {* The BFD which owns the section. *}
52b219b5 538. bfd *owner;
252b5132 539.
b5f79c76 540. {* A symbol which points at this section only. *}
fc0a2244
AC
541. struct bfd_symbol *symbol;
542. struct bfd_symbol **symbol_ptr_ptr;
252b5132 543.
8423293d
AM
544. {* Early in the link process, map_head and map_tail are used to build
545. a list of input sections attached to an output section. Later,
546. output sections use these fields for a list of bfd_link_order
b7d07216
L
547. structs. The linked_to_symbol_name field is for ELF assembler
548. internal use. *}
8423293d
AM
549. union {
550. struct bfd_link_order *link_order;
551. struct bfd_section *s;
b7d07216 552. const char *linked_to_symbol_name;
8423293d 553. } map_head, map_tail;
cf53a97b
AM
554.
555. {* Points to the output section this section is already assigned to,
556. if any. This is used when support for non-contiguous memory
557. regions is enabled. *}
558. struct bfd_section *already_assigned;
559.
560. {* Explicitly specified section type, if non-zero. *}
561. unsigned int type;
abf874aa 562.
b5f79c76 563.} asection;
252b5132 564.
717d4bd6
AM
565
566EXTERNAL
a48931cc
AM
567.static inline const char *
568.bfd_section_name (const asection *sec)
569.{
570. return sec->name;
571.}
572.
573.static inline bfd_size_type
574.bfd_section_size (const asection *sec)
575.{
576. return sec->size;
577.}
578.
579.static inline bfd_vma
580.bfd_section_vma (const asection *sec)
581.{
582. return sec->vma;
583.}
584.
585.static inline bfd_vma
586.bfd_section_lma (const asection *sec)
587.{
588. return sec->lma;
589.}
590.
591.static inline unsigned int
592.bfd_section_alignment (const asection *sec)
593.{
594. return sec->alignment_power;
595.}
596.
597.static inline flagword
598.bfd_section_flags (const asection *sec)
599.{
600. return sec->flags;
601.}
602.
603.static inline void *
604.bfd_section_userdata (const asection *sec)
605.{
606. return sec->userdata;
607.}
0a1b45a2 608.static inline bool
a48931cc
AM
609.bfd_is_com_section (const asection *sec)
610.{
611. return (sec->flags & SEC_IS_COMMON) != 0;
612.}
613.
27b829ee
NC
614.{* Note: the following are provided as inline functions rather than macros
615. because not all callers use the return value. A macro implementation
616. would use a comma expression, eg: "((ptr)->foo = val, TRUE)" and some
617. compilers will complain about comma expressions that have no effect. *}
0a1b45a2 618.static inline bool
fd361982 619.bfd_set_section_userdata (asection *sec, void *val)
27b829ee 620.{
fd361982 621. sec->userdata = val;
0a1b45a2 622. return true;
27b829ee
NC
623.}
624.
0a1b45a2 625.static inline bool
fd361982 626.bfd_set_section_vma (asection *sec, bfd_vma val)
27b829ee 627.{
fd361982 628. sec->vma = sec->lma = val;
0a1b45a2
AM
629. sec->user_set_vma = true;
630. return true;
27b829ee
NC
631.}
632.
0a1b45a2 633.static inline bool
fd361982 634.bfd_set_section_lma (asection *sec, bfd_vma val)
27b829ee 635.{
fd361982 636. sec->lma = val;
0a1b45a2 637. return true;
fd361982
AM
638.}
639.
0a1b45a2 640.static inline bool
fd361982
AM
641.bfd_set_section_alignment (asection *sec, unsigned int val)
642.{
578a7392
AM
643. if (val >= sizeof (bfd_vma) * 8 - 1)
644. return false;
fd361982 645. sec->alignment_power = val;
0a1b45a2 646. return true;
27b829ee
NC
647.}
648.
52b219b5
AM
649.{* These sections are global, and are managed by BFD. The application
650. and target back end are not permitted to change the values in
45dfa85a 651. these sections. *}
45a466b5 652.extern asection _bfd_std_section[4];
45dfa85a 653.
252b5132
RH
654.#define BFD_ABS_SECTION_NAME "*ABS*"
655.#define BFD_UND_SECTION_NAME "*UND*"
656.#define BFD_COM_SECTION_NAME "*COM*"
657.#define BFD_IND_SECTION_NAME "*IND*"
658.
b5f79c76 659.{* Pointer to the common section. *}
45a466b5 660.#define bfd_com_section_ptr (&_bfd_std_section[0])
45dfa85a 661.{* Pointer to the undefined section. *}
45a466b5 662.#define bfd_und_section_ptr (&_bfd_std_section[1])
45dfa85a 663.{* Pointer to the absolute section. *}
45a466b5 664.#define bfd_abs_section_ptr (&_bfd_std_section[2])
b5f79c76 665.{* Pointer to the indirect section. *}
45a466b5 666.#define bfd_ind_section_ptr (&_bfd_std_section[3])
45dfa85a 667.
0a1b45a2 668.static inline bool
a48931cc
AM
669.bfd_is_und_section (const asection *sec)
670.{
671. return sec == bfd_und_section_ptr;
672.}
673.
0a1b45a2 674.static inline bool
a48931cc
AM
675.bfd_is_abs_section (const asection *sec)
676.{
677. return sec == bfd_abs_section_ptr;
678.}
679.
0a1b45a2 680.static inline bool
a48931cc
AM
681.bfd_is_ind_section (const asection *sec)
682.{
683. return sec == bfd_ind_section_ptr;
684.}
685.
0a1b45a2 686.static inline bool
a48931cc
AM
687.bfd_is_const_section (const asection *sec)
688.{
18f97353
AM
689. return (sec >= _bfd_std_section
690. && sec < _bfd_std_section + (sizeof (_bfd_std_section)
691. / sizeof (_bfd_std_section[0])));
a48931cc
AM
692.}
693.
694.{* Return TRUE if input section SEC has been discarded. *}
0a1b45a2 695.static inline bool
a48931cc
AM
696.discarded_section (const asection *sec)
697.{
698. return (!bfd_is_abs_section (sec)
699. && bfd_is_abs_section (sec->output_section)
700. && sec->sec_info_type != SEC_INFO_TYPE_MERGE
701. && sec->sec_info_type != SEC_INFO_TYPE_JUST_SYMS);
702.}
9e7b37b3 703.
821e6ff6 704.#define BFD_FAKE_SECTION(SEC, SYM, NAME, IDX, FLAGS) \
57594b6a
AM
705. {* name, next, prev, id, section_id, index, flags, user_set_vma, *} \
706. { NAME, NULL, NULL, IDX, 0, 0, FLAGS, 0, \
a4d8e49b 707. \
48e81d7f 708. {* linker_mark, linker_has_input, gc_mark, decompress_status, *} \
b0dddeec 709. 0, 0, 1, 0, \
a4d8e49b 710. \
48e81d7f 711. {* segment_mark, sec_info_type, use_rela_p, *} \
4a114e3e 712. 0, 0, 0, \
a4d8e49b 713. \
48e81d7f 714. {* sec_flg0, sec_flg1, sec_flg2, sec_flg3, sec_flg4, sec_flg5, *} \
b0dddeec 715. 0, 0, 0, 0, 0, 0, \
a4d8e49b 716. \
57594b6a 717. {* vma, lma, size, rawsize, compressed_size, *} \
9751574e 718. 0, 0, 0, 0, 0, \
a4d8e49b 719. \
57594b6a
AM
720. {* output_offset, output_section, relocation, orelocation, *} \
721. 0, &SEC, NULL, NULL, \
a4d8e49b 722. \
57594b6a
AM
723. {* reloc_count, alignment_power, filepos, rel_filepos, *} \
724. 0, 0, 0, 0, \
a4d8e49b 725. \
48e81d7f 726. {* line_filepos, userdata, contents, lineno, lineno_count, *} \
a4d8e49b
L
727. 0, NULL, NULL, NULL, 0, \
728. \
48e81d7f
AM
729. {* entsize, kept_section, moving_line_filepos, *} \
730. 0, NULL, 0, \
a4d8e49b 731. \
48e81d7f 732. {* target_index, used_by_bfd, constructor_chain, owner, *} \
a4d8e49b
L
733. 0, NULL, NULL, NULL, \
734. \
b209b5a6
AM
735. {* symbol, symbol_ptr_ptr, *} \
736. (struct bfd_symbol *) SYM, &SEC.symbol, \
a4d8e49b 737. \
c212f39d 738. {* map_head, map_tail, already_assigned, type *} \
cf53a97b 739. { NULL }, { NULL }, NULL, 0 \
abf874aa 740. \
a4d8e49b
L
741. }
742.
7eacd66b
AM
743.{* We use a macro to initialize the static asymbol structures because
744. traditional C does not permit us to initialize a union member while
745. gcc warns if we don't initialize it.
746. the_bfd, name, value, attr, section [, udata] *}
747.#ifdef __STDC__
748.#define GLOBAL_SYM_INIT(NAME, SECTION) \
749. { 0, NAME, 0, BSF_SECTION_SYM, SECTION, { 0 }}
750.#else
751.#define GLOBAL_SYM_INIT(NAME, SECTION) \
752. { 0, NAME, 0, BSF_SECTION_SYM, SECTION }
753.#endif
754.
252b5132
RH
755*/
756
757/* These symbols are global, not specific to any BFD. Therefore, anything
758 that tries to change them is broken, and should be repaired. */
22bc497d 759
252b5132
RH
760static const asymbol global_syms[] =
761{
45dfa85a
AM
762 GLOBAL_SYM_INIT (BFD_COM_SECTION_NAME, bfd_com_section_ptr),
763 GLOBAL_SYM_INIT (BFD_UND_SECTION_NAME, bfd_und_section_ptr),
764 GLOBAL_SYM_INIT (BFD_ABS_SECTION_NAME, bfd_abs_section_ptr),
765 GLOBAL_SYM_INIT (BFD_IND_SECTION_NAME, bfd_ind_section_ptr)
252b5132
RH
766};
767
45dfa85a 768#define STD_SECTION(NAME, IDX, FLAGS) \
821e6ff6 769 BFD_FAKE_SECTION(_bfd_std_section[IDX], &global_syms[IDX], NAME, IDX, FLAGS)
252b5132 770
45a466b5 771asection _bfd_std_section[] = {
45dfa85a
AM
772 STD_SECTION (BFD_COM_SECTION_NAME, 0, SEC_IS_COMMON),
773 STD_SECTION (BFD_UND_SECTION_NAME, 1, 0),
774 STD_SECTION (BFD_ABS_SECTION_NAME, 2, 0),
775 STD_SECTION (BFD_IND_SECTION_NAME, 3, 0)
776};
252b5132
RH
777#undef STD_SECTION
778
73e87d70
AM
779/* Initialize an entry in the section hash table. */
780
781struct bfd_hash_entry *
c58b9523
AM
782bfd_section_hash_newfunc (struct bfd_hash_entry *entry,
783 struct bfd_hash_table *table,
784 const char *string)
73e87d70
AM
785{
786 /* Allocate the structure if it has not already been allocated by a
787 subclass. */
788 if (entry == NULL)
789 {
d45913a0
DA
790 entry = (struct bfd_hash_entry *)
791 bfd_hash_allocate (table, sizeof (struct section_hash_entry));
73e87d70
AM
792 if (entry == NULL)
793 return entry;
794 }
795
796 /* Call the allocation method of the superclass. */
797 entry = bfd_hash_newfunc (entry, table, string);
798 if (entry != NULL)
c58b9523
AM
799 memset (&((struct section_hash_entry *) entry)->section, 0,
800 sizeof (asection));
73e87d70
AM
801
802 return entry;
803}
804
805#define section_hash_lookup(table, string, create, copy) \
806 ((struct section_hash_entry *) \
807 bfd_hash_lookup ((table), (string), (create), (copy)))
808
f592407e
AM
809/* Create a symbol whose only job is to point to this section. This
810 is useful for things like relocs which are relative to the base
811 of a section. */
73e87d70 812
0a1b45a2 813bool
f592407e 814_bfd_generic_new_section_hook (bfd *abfd, asection *newsect)
73e87d70 815{
73e87d70
AM
816 newsect->symbol = bfd_make_empty_symbol (abfd);
817 if (newsect->symbol == NULL)
0a1b45a2 818 return false;
73e87d70
AM
819
820 newsect->symbol->name = newsect->name;
821 newsect->symbol->value = 0;
822 newsect->symbol->section = newsect;
823 newsect->symbol->flags = BSF_SECTION_SYM;
824
825 newsect->symbol_ptr_ptr = &newsect->symbol;
0a1b45a2 826 return true;
f592407e
AM
827}
828
7cf7fcc8 829unsigned int _bfd_section_id = 0x10; /* id 0 to 3 used by STD_SECTION. */
6f20ed8a 830
f592407e
AM
831/* Initializes a new section. NEWSECT->NAME is already set. */
832
833static asection *
834bfd_section_init (bfd *abfd, asection *newsect)
835{
7cf7fcc8 836 newsect->id = _bfd_section_id;
f592407e
AM
837 newsect->index = abfd->section_count;
838 newsect->owner = abfd;
73e87d70
AM
839
840 if (! BFD_SEND (abfd, _new_section_hook, (abfd, newsect)))
841 return NULL;
842
7cf7fcc8 843 _bfd_section_id++;
73e87d70 844 abfd->section_count++;
5daa8fe7 845 bfd_section_list_append (abfd, newsect);
73e87d70
AM
846 return newsect;
847}
848
252b5132
RH
849/*
850DOCDD
851INODE
852section prototypes, , typedef asection, Sections
853SUBSECTION
854 Section prototypes
855
856These are the functions exported by the section handling part of BFD.
857*/
858
9e7b37b3
AM
859/*
860FUNCTION
861 bfd_section_list_clear
862
863SYNOPSIS
864 void bfd_section_list_clear (bfd *);
865
866DESCRIPTION
867 Clears the section list, and also resets the section count and
868 hash table entries.
869*/
870
871void
c58b9523 872bfd_section_list_clear (bfd *abfd)
9e7b37b3
AM
873{
874 abfd->sections = NULL;
5daa8fe7 875 abfd->section_last = NULL;
9e7b37b3 876 abfd->section_count = 0;
c58b9523 877 memset (abfd->section_htab.table, 0,
9e7b37b3 878 abfd->section_htab.size * sizeof (struct bfd_hash_entry *));
c9ba0c87 879 abfd->section_htab.count = 0;
9e7b37b3
AM
880}
881
252b5132
RH
882/*
883FUNCTION
884 bfd_get_section_by_name
885
886SYNOPSIS
c58b9523 887 asection *bfd_get_section_by_name (bfd *abfd, const char *name);
252b5132
RH
888
889DESCRIPTION
90061c33
AM
890 Return the most recently created section attached to @var{abfd}
891 named @var{name}. Return NULL if no such section exists.
252b5132
RH
892*/
893
894asection *
c58b9523 895bfd_get_section_by_name (bfd *abfd, const char *name)
252b5132 896{
73e87d70
AM
897 struct section_hash_entry *sh;
898
427e4066
AB
899 if (name == NULL)
900 return NULL;
901
0a1b45a2 902 sh = section_hash_lookup (&abfd->section_htab, name, false, false);
73e87d70
AM
903 if (sh != NULL)
904 return &sh->section;
252b5132 905
252b5132
RH
906 return NULL;
907}
908
90061c33
AM
909/*
910FUNCTION
911 bfd_get_next_section_by_name
912
913SYNOPSIS
199af150 914 asection *bfd_get_next_section_by_name (bfd *ibfd, asection *sec);
90061c33
AM
915
916DESCRIPTION
917 Given @var{sec} is a section returned by @code{bfd_get_section_by_name},
918 return the next most recently created section attached to the same
199af150
AM
919 BFD with the same name, or if no such section exists in the same BFD and
920 IBFD is non-NULL, the next section with the same name in any input
921 BFD following IBFD. Return NULL on finding no section.
90061c33
AM
922*/
923
924asection *
199af150 925bfd_get_next_section_by_name (bfd *ibfd, asection *sec)
90061c33
AM
926{
927 struct section_hash_entry *sh;
928 const char *name;
929 unsigned long hash;
930
931 sh = ((struct section_hash_entry *)
932 ((char *) sec - offsetof (struct section_hash_entry, section)));
933
934 hash = sh->root.hash;
935 name = sec->name;
936 for (sh = (struct section_hash_entry *) sh->root.next;
937 sh != NULL;
938 sh = (struct section_hash_entry *) sh->root.next)
939 if (sh->root.hash == hash
940 && strcmp (sh->root.string, name) == 0)
941 return &sh->section;
942
199af150
AM
943 if (ibfd != NULL)
944 {
945 while ((ibfd = ibfd->link.next) != NULL)
946 {
947 asection *s = bfd_get_section_by_name (ibfd, name);
948 if (s != NULL)
949 return s;
950 }
951 }
952
90061c33
AM
953 return NULL;
954}
955
3d4d4302
AM
956/*
957FUNCTION
958 bfd_get_linker_section
959
960SYNOPSIS
961 asection *bfd_get_linker_section (bfd *abfd, const char *name);
962
963DESCRIPTION
964 Return the linker created section attached to @var{abfd}
965 named @var{name}. Return NULL if no such section exists.
966*/
967
968asection *
969bfd_get_linker_section (bfd *abfd, const char *name)
970{
971 asection *sec = bfd_get_section_by_name (abfd, name);
972
973 while (sec != NULL && (sec->flags & SEC_LINKER_CREATED) == 0)
199af150 974 sec = bfd_get_next_section_by_name (NULL, sec);
3d4d4302
AM
975 return sec;
976}
977
fafe6678
L
978/*
979FUNCTION
980 bfd_get_section_by_name_if
981
982SYNOPSIS
983 asection *bfd_get_section_by_name_if
984 (bfd *abfd,
985 const char *name,
0a1b45a2 986 bool (*func) (bfd *abfd, asection *sect, void *obj),
fafe6678
L
987 void *obj);
988
989DESCRIPTION
990 Call the provided function @var{func} for each section
991 attached to the BFD @var{abfd} whose name matches @var{name},
992 passing @var{obj} as an argument. The function will be called
993 as if by
994
995| func (abfd, the_section, obj);
996
997 It returns the first section for which @var{func} returns true,
998 otherwise <<NULL>>.
999
1000*/
1001
1002asection *
1003bfd_get_section_by_name_if (bfd *abfd, const char *name,
0a1b45a2 1004 bool (*operation) (bfd *, asection *, void *),
fafe6678
L
1005 void *user_storage)
1006{
1007 struct section_hash_entry *sh;
1008 unsigned long hash;
1009
427e4066
AB
1010 if (name == NULL)
1011 return NULL;
1012
0a1b45a2 1013 sh = section_hash_lookup (&abfd->section_htab, name, false, false);
fafe6678
L
1014 if (sh == NULL)
1015 return NULL;
1016
1017 hash = sh->root.hash;
2fb9328d
AM
1018 for (; sh != NULL; sh = (struct section_hash_entry *) sh->root.next)
1019 if (sh->root.hash == hash
1020 && strcmp (sh->root.string, name) == 0
1021 && (*operation) (abfd, &sh->section, user_storage))
1022 return &sh->section;
fafe6678
L
1023
1024 return NULL;
1025}
1026
1bd91689
AM
1027/*
1028FUNCTION
1029 bfd_get_unique_section_name
1030
1031SYNOPSIS
c58b9523
AM
1032 char *bfd_get_unique_section_name
1033 (bfd *abfd, const char *templat, int *count);
1bd91689
AM
1034
1035DESCRIPTION
1036 Invent a section name that is unique in @var{abfd} by tacking
77cb06e9
AM
1037 a dot and a digit suffix onto the original @var{templat}. If
1038 @var{count} is non-NULL, then it specifies the first number
1039 tried as a suffix to generate a unique name. The value
1040 pointed to by @var{count} will be incremented in this case.
1bd91689
AM
1041*/
1042
1043char *
c58b9523 1044bfd_get_unique_section_name (bfd *abfd, const char *templat, int *count)
1bd91689
AM
1045{
1046 int num;
1047 unsigned int len;
1048 char *sname;
1049
a966dba9 1050 len = strlen (templat);
a50b1753 1051 sname = (char *) bfd_malloc (len + 8);
b3ea3584
AM
1052 if (sname == NULL)
1053 return NULL;
d4c88bbb 1054 memcpy (sname, templat, len);
1bd91689
AM
1055 num = 1;
1056 if (count != NULL)
1057 num = *count;
1058
1059 do
1060 {
1061 /* If we have a million sections, something is badly wrong. */
1062 if (num > 999999)
1063 abort ();
77cb06e9 1064 sprintf (sname + len, ".%d", num++);
1bd91689 1065 }
0a1b45a2 1066 while (section_hash_lookup (&abfd->section_htab, sname, false, false));
1bd91689
AM
1067
1068 if (count != NULL)
1069 *count = num;
1070 return sname;
1071}
1072
252b5132
RH
1073/*
1074FUNCTION
1075 bfd_make_section_old_way
1076
1077SYNOPSIS
c58b9523 1078 asection *bfd_make_section_old_way (bfd *abfd, const char *name);
252b5132
RH
1079
1080DESCRIPTION
1081 Create a new empty section called @var{name}
1082 and attach it to the end of the chain of sections for the
1083 BFD @var{abfd}. An attempt to create a section with a name which
1084 is already in use returns its pointer without changing the
1085 section chain.
1086
1087 It has the funny name since this is the way it used to be
1088 before it was rewritten....
1089
1090 Possible errors are:
1091 o <<bfd_error_invalid_operation>> -
1092 If output has already started for this BFD.
1093 o <<bfd_error_no_memory>> -
1094 If memory allocation fails.
1095
1096*/
1097
252b5132 1098asection *
c58b9523 1099bfd_make_section_old_way (bfd *abfd, const char *name)
252b5132 1100{
73e87d70
AM
1101 asection *newsect;
1102
1103 if (abfd->output_has_begun)
1104 {
1105 bfd_set_error (bfd_error_invalid_operation);
1106 return NULL;
1107 }
1108
1109 if (strcmp (name, BFD_ABS_SECTION_NAME) == 0)
f592407e
AM
1110 newsect = bfd_abs_section_ptr;
1111 else if (strcmp (name, BFD_COM_SECTION_NAME) == 0)
1112 newsect = bfd_com_section_ptr;
1113 else if (strcmp (name, BFD_UND_SECTION_NAME) == 0)
1114 newsect = bfd_und_section_ptr;
1115 else if (strcmp (name, BFD_IND_SECTION_NAME) == 0)
1116 newsect = bfd_ind_section_ptr;
1117 else
1118 {
1119 struct section_hash_entry *sh;
73e87d70 1120
0a1b45a2 1121 sh = section_hash_lookup (&abfd->section_htab, name, true, false);
f592407e
AM
1122 if (sh == NULL)
1123 return NULL;
73e87d70 1124
f592407e
AM
1125 newsect = &sh->section;
1126 if (newsect->name != NULL)
1127 {
1128 /* Section already exists. */
1129 return newsect;
1130 }
73e87d70 1131
f592407e
AM
1132 newsect->name = name;
1133 return bfd_section_init (abfd, newsect);
252b5132 1134 }
73e87d70 1135
f592407e
AM
1136 /* Call new_section_hook when "creating" the standard abs, com, und
1137 and ind sections to tack on format specific section data.
1138 Also, create a proper section symbol. */
1139 if (! BFD_SEND (abfd, _new_section_hook, (abfd, newsect)))
1140 return NULL;
1141 return newsect;
252b5132
RH
1142}
1143
1144/*
1145FUNCTION
3496cb2a 1146 bfd_make_section_anyway_with_flags
252b5132
RH
1147
1148SYNOPSIS
3496cb2a
L
1149 asection *bfd_make_section_anyway_with_flags
1150 (bfd *abfd, const char *name, flagword flags);
252b5132
RH
1151
1152DESCRIPTION
1153 Create a new empty section called @var{name} and attach it to the end of
1154 the chain of sections for @var{abfd}. Create a new section even if there
3496cb2a
L
1155 is already a section with that name. Also set the attributes of the
1156 new section to the value @var{flags}.
252b5132
RH
1157
1158 Return <<NULL>> and set <<bfd_error>> on error; possible errors are:
1159 o <<bfd_error_invalid_operation>> - If output has already started for @var{abfd}.
1160 o <<bfd_error_no_memory>> - If memory allocation fails.
1161*/
1162
1163sec_ptr
3496cb2a
L
1164bfd_make_section_anyway_with_flags (bfd *abfd, const char *name,
1165 flagword flags)
252b5132 1166{
73e87d70 1167 struct section_hash_entry *sh;
252b5132 1168 asection *newsect;
252b5132
RH
1169
1170 if (abfd->output_has_begun)
1171 {
1172 bfd_set_error (bfd_error_invalid_operation);
1173 return NULL;
1174 }
1175
0a1b45a2 1176 sh = section_hash_lookup (&abfd->section_htab, name, true, false);
73e87d70 1177 if (sh == NULL)
252b5132
RH
1178 return NULL;
1179
73e87d70
AM
1180 newsect = &sh->section;
1181 if (newsect->name != NULL)
4d7ce4dd 1182 {
72adc230
AM
1183 /* We are making a section of the same name. Put it in the
1184 section hash table. Even though we can't find it directly by a
1185 hash lookup, we'll be able to find the section by traversing
1186 sh->root.next quicker than looking at all the bfd sections. */
1187 struct section_hash_entry *new_sh;
1188 new_sh = (struct section_hash_entry *)
1189 bfd_section_hash_newfunc (NULL, &abfd->section_htab, name);
1190 if (new_sh == NULL)
73e87d70 1191 return NULL;
72adc230 1192
73499ab8 1193 new_sh->root = sh->root;
72adc230
AM
1194 sh->root.next = &new_sh->root;
1195 newsect = &new_sh->section;
252b5132
RH
1196 }
1197
3496cb2a 1198 newsect->flags = flags;
73e87d70
AM
1199 newsect->name = name;
1200 return bfd_section_init (abfd, newsect);
252b5132
RH
1201}
1202
1203/*
1204FUNCTION
3496cb2a 1205 bfd_make_section_anyway
252b5132
RH
1206
1207SYNOPSIS
3496cb2a
L
1208 asection *bfd_make_section_anyway (bfd *abfd, const char *name);
1209
1210DESCRIPTION
1211 Create a new empty section called @var{name} and attach it to the end of
1212 the chain of sections for @var{abfd}. Create a new section even if there
1213 is already a section with that name.
1214
1215 Return <<NULL>> and set <<bfd_error>> on error; possible errors are:
1216 o <<bfd_error_invalid_operation>> - If output has already started for @var{abfd}.
1217 o <<bfd_error_no_memory>> - If memory allocation fails.
1218*/
1219
1220sec_ptr
1221bfd_make_section_anyway (bfd *abfd, const char *name)
1222{
1223 return bfd_make_section_anyway_with_flags (abfd, name, 0);
1224}
1225
1226/*
1227FUNCTION
1228 bfd_make_section_with_flags
1229
1230SYNOPSIS
1231 asection *bfd_make_section_with_flags
1232 (bfd *, const char *name, flagword flags);
252b5132
RH
1233
1234DESCRIPTION
1235 Like <<bfd_make_section_anyway>>, but return <<NULL>> (without calling
1236 bfd_set_error ()) without changing the section chain if there is already a
3496cb2a
L
1237 section named @var{name}. Also set the attributes of the new section to
1238 the value @var{flags}. If there is an error, return <<NULL>> and set
252b5132
RH
1239 <<bfd_error>>.
1240*/
1241
1242asection *
3496cb2a
L
1243bfd_make_section_with_flags (bfd *abfd, const char *name,
1244 flagword flags)
252b5132 1245{
73e87d70
AM
1246 struct section_hash_entry *sh;
1247 asection *newsect;
252b5132 1248
8bdf0be1 1249 if (abfd == NULL || name == NULL || abfd->output_has_begun)
252b5132 1250 {
73e87d70
AM
1251 bfd_set_error (bfd_error_invalid_operation);
1252 return NULL;
252b5132
RH
1253 }
1254
73e87d70
AM
1255 if (strcmp (name, BFD_ABS_SECTION_NAME) == 0
1256 || strcmp (name, BFD_COM_SECTION_NAME) == 0
1257 || strcmp (name, BFD_UND_SECTION_NAME) == 0
1258 || strcmp (name, BFD_IND_SECTION_NAME) == 0)
1259 return NULL;
252b5132 1260
0a1b45a2 1261 sh = section_hash_lookup (&abfd->section_htab, name, true, false);
73e87d70
AM
1262 if (sh == NULL)
1263 return NULL;
1264
1265 newsect = &sh->section;
1266 if (newsect->name != NULL)
252b5132 1267 {
73e87d70 1268 /* Section already exists. */
003d627e 1269 return NULL;
252b5132
RH
1270 }
1271
73e87d70 1272 newsect->name = name;
3496cb2a 1273 newsect->flags = flags;
73e87d70 1274 return bfd_section_init (abfd, newsect);
252b5132
RH
1275}
1276
3496cb2a
L
1277/*
1278FUNCTION
1279 bfd_make_section
1280
1281SYNOPSIS
1282 asection *bfd_make_section (bfd *, const char *name);
1283
1284DESCRIPTION
1285 Like <<bfd_make_section_anyway>>, but return <<NULL>> (without calling
1286 bfd_set_error ()) without changing the section chain if there is already a
1287 section named @var{name}. If there is an error, return <<NULL>> and set
1288 <<bfd_error>>.
1289*/
1290
1291asection *
1292bfd_make_section (bfd *abfd, const char *name)
1293{
1294 return bfd_make_section_with_flags (abfd, name, 0);
1295}
1296
252b5132
RH
1297/*
1298FUNCTION
1299 bfd_set_section_flags
1300
1301SYNOPSIS
0a1b45a2 1302 bool bfd_set_section_flags (asection *sec, flagword flags);
252b5132
RH
1303
1304DESCRIPTION
fd361982
AM
1305 Set the attributes of the section @var{sec} to the value @var{flags}.
1306 Return <<TRUE>> on success, <<FALSE>> on error. Possible error
1307 returns are:
252b5132
RH
1308
1309 o <<bfd_error_invalid_operation>> -
1310 The section cannot have one or more of the attributes
1311 requested. For example, a .bss section in <<a.out>> may not
1312 have the <<SEC_HAS_CONTENTS>> field set.
1313
1314*/
1315
0a1b45a2 1316bool
fd361982 1317bfd_set_section_flags (asection *section, flagword flags)
252b5132 1318{
252b5132 1319 section->flags = flags;
0a1b45a2 1320 return true;
252b5132
RH
1321}
1322
4e011fb5
AM
1323/*
1324FUNCTION
1325 bfd_rename_section
1326
1327SYNOPSIS
1328 void bfd_rename_section
fd361982 1329 (asection *sec, const char *newname);
4e011fb5
AM
1330
1331DESCRIPTION
fd361982 1332 Rename section @var{sec} to @var{newname}.
4e011fb5
AM
1333*/
1334
1335void
fd361982 1336bfd_rename_section (asection *sec, const char *newname)
4e011fb5
AM
1337{
1338 struct section_hash_entry *sh;
1339
1340 sh = (struct section_hash_entry *)
1341 ((char *) sec - offsetof (struct section_hash_entry, section));
1342 sh->section.name = newname;
fd361982 1343 bfd_hash_rename (&sec->owner->section_htab, newname, &sh->root);
4e011fb5
AM
1344}
1345
252b5132
RH
1346/*
1347FUNCTION
1348 bfd_map_over_sections
1349
1350SYNOPSIS
c58b9523
AM
1351 void bfd_map_over_sections
1352 (bfd *abfd,
1353 void (*func) (bfd *abfd, asection *sect, void *obj),
1354 void *obj);
252b5132
RH
1355
1356DESCRIPTION
1357 Call the provided function @var{func} for each section
1358 attached to the BFD @var{abfd}, passing @var{obj} as an
1359 argument. The function will be called as if by
1360
c58b9523 1361| func (abfd, the_section, obj);
252b5132 1362
7dee875e 1363 This is the preferred method for iterating over sections; an
252b5132
RH
1364 alternative would be to use a loop:
1365
c06fc9eb 1366| asection *p;
252b5132 1367| for (p = abfd->sections; p != NULL; p = p->next)
c58b9523 1368| func (abfd, p, ...)
252b5132 1369
252b5132
RH
1370*/
1371
252b5132 1372void
c58b9523
AM
1373bfd_map_over_sections (bfd *abfd,
1374 void (*operation) (bfd *, asection *, void *),
1375 void *user_storage)
252b5132
RH
1376{
1377 asection *sect;
1378 unsigned int i = 0;
1379
1380 for (sect = abfd->sections; sect != NULL; i++, sect = sect->next)
1381 (*operation) (abfd, sect, user_storage);
1382
1383 if (i != abfd->section_count) /* Debugging */
1384 abort ();
1385}
1386
bc87dd2e
L
1387/*
1388FUNCTION
1389 bfd_sections_find_if
1390
1391SYNOPSIS
1392 asection *bfd_sections_find_if
1393 (bfd *abfd,
0a1b45a2 1394 bool (*operation) (bfd *abfd, asection *sect, void *obj),
bc87dd2e
L
1395 void *obj);
1396
1397DESCRIPTION
f4eae89c 1398 Call the provided function @var{operation} for each section
bc87dd2e
L
1399 attached to the BFD @var{abfd}, passing @var{obj} as an
1400 argument. The function will be called as if by
1401
f4eae89c 1402| operation (abfd, the_section, obj);
bc87dd2e 1403
f4eae89c 1404 It returns the first section for which @var{operation} returns true.
bc87dd2e
L
1405
1406*/
1407
1408asection *
1409bfd_sections_find_if (bfd *abfd,
0a1b45a2 1410 bool (*operation) (bfd *, asection *, void *),
bc87dd2e
L
1411 void *user_storage)
1412{
1413 asection *sect;
1414
1415 for (sect = abfd->sections; sect != NULL; sect = sect->next)
1416 if ((*operation) (abfd, sect, user_storage))
1417 break;
1418
1419 return sect;
1420}
1421
252b5132
RH
1422/*
1423FUNCTION
1424 bfd_set_section_size
1425
1426SYNOPSIS
0a1b45a2 1427 bool bfd_set_section_size (asection *sec, bfd_size_type val);
252b5132
RH
1428
1429DESCRIPTION
1430 Set @var{sec} to the size @var{val}. If the operation is
b34976b6 1431 ok, then <<TRUE>> is returned, else <<FALSE>>.
252b5132
RH
1432
1433 Possible error returns:
1434 o <<bfd_error_invalid_operation>> -
1435 Writing has started to the BFD, so setting the size is invalid.
1436
1437*/
1438
0a1b45a2 1439bool
fd361982 1440bfd_set_section_size (asection *sec, bfd_size_type val)
252b5132
RH
1441{
1442 /* Once you've started writing to any section you cannot create or change
7b82c249 1443 the size of any others. */
252b5132 1444
fd361982 1445 if (sec->owner == NULL || sec->owner->output_has_begun)
252b5132
RH
1446 {
1447 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 1448 return false;
252b5132
RH
1449 }
1450
fd361982 1451 sec->size = val;
0a1b45a2 1452 return true;
252b5132
RH
1453}
1454
1455/*
1456FUNCTION
1457 bfd_set_section_contents
1458
1459SYNOPSIS
0a1b45a2 1460 bool bfd_set_section_contents
85302095
AC
1461 (bfd *abfd, asection *section, const void *data,
1462 file_ptr offset, bfd_size_type count);
252b5132 1463
252b5132
RH
1464DESCRIPTION
1465 Sets the contents of the section @var{section} in BFD
c8d3f932
MW
1466 @var{abfd} to the data starting in memory at @var{location}.
1467 The data is written to the output section starting at offset
9a968f43 1468 @var{offset} for @var{count} octets.
252b5132 1469
c8d3f932
MW
1470 Normally <<TRUE>> is returned, but <<FALSE>> is returned if
1471 there was an error. Possible error returns are:
252b5132
RH
1472 o <<bfd_error_no_contents>> -
1473 The output section does not have the <<SEC_HAS_CONTENTS>>
1474 attribute, so nothing can be written to it.
c8d3f932
MW
1475 o <<bfd_error_bad_value>> -
1476 The section is unable to contain all of the data.
1477 o <<bfd_error_invalid_operation>> -
1478 The BFD is not writeable.
1479 o and some more too.
252b5132
RH
1480
1481 This routine is front end to the back end function
1482 <<_bfd_set_section_contents>>.
1483
252b5132
RH
1484*/
1485
0a1b45a2 1486bool
c58b9523
AM
1487bfd_set_section_contents (bfd *abfd,
1488 sec_ptr section,
85302095 1489 const void *location,
c58b9523
AM
1490 file_ptr offset,
1491 bfd_size_type count)
252b5132
RH
1492{
1493 bfd_size_type sz;
1494
fd361982 1495 if (!(bfd_section_flags (section) & SEC_HAS_CONTENTS))
252b5132
RH
1496 {
1497 bfd_set_error (bfd_error_no_contents);
0a1b45a2 1498 return false;
252b5132
RH
1499 }
1500
eea6121a 1501 sz = section->size;
dc810e39 1502 if ((bfd_size_type) offset > sz
6db658c5 1503 || count > sz - offset
dc810e39 1504 || count != (size_t) count)
252b5132 1505 {
252b5132 1506 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1507 return false;
252b5132 1508 }
252b5132 1509
26ae6d5e 1510 if (!bfd_write_p (abfd))
252b5132 1511 {
252b5132 1512 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 1513 return false;
252b5132
RH
1514 }
1515
9a951beb
RH
1516 /* Record a copy of the data in memory if desired. */
1517 if (section->contents
c58b9523 1518 && location != section->contents + offset)
dc810e39 1519 memcpy (section->contents + offset, location, (size_t) count);
9a951beb 1520
252b5132
RH
1521 if (BFD_SEND (abfd, _bfd_set_section_contents,
1522 (abfd, section, location, offset, count)))
1523 {
0a1b45a2
AM
1524 abfd->output_has_begun = true;
1525 return true;
252b5132
RH
1526 }
1527
0a1b45a2 1528 return false;
252b5132
RH
1529}
1530
1531/*
1532FUNCTION
1533 bfd_get_section_contents
1534
1535SYNOPSIS
0a1b45a2 1536 bool bfd_get_section_contents
c58b9523
AM
1537 (bfd *abfd, asection *section, void *location, file_ptr offset,
1538 bfd_size_type count);
252b5132
RH
1539
1540DESCRIPTION
1541 Read data from @var{section} in BFD @var{abfd}
1542 into memory starting at @var{location}. The data is read at an
1543 offset of @var{offset} from the start of the input section,
1544 and is read for @var{count} bytes.
1545
1546 If the contents of a constructor with the <<SEC_CONSTRUCTOR>>
1547 flag set are requested or if the section does not have the
1548 <<SEC_HAS_CONTENTS>> flag set, then the @var{location} is filled
b34976b6
AM
1549 with zeroes. If no errors occur, <<TRUE>> is returned, else
1550 <<FALSE>>.
252b5132 1551
252b5132 1552*/
0a1b45a2 1553bool
c58b9523
AM
1554bfd_get_section_contents (bfd *abfd,
1555 sec_ptr section,
1556 void *location,
1557 file_ptr offset,
1558 bfd_size_type count)
252b5132
RH
1559{
1560 bfd_size_type sz;
1561
1562 if (section->flags & SEC_CONSTRUCTOR)
1563 {
dc810e39 1564 memset (location, 0, (size_t) count);
0a1b45a2 1565 return true;
252b5132
RH
1566 }
1567
20d8836e 1568 sz = bfd_get_section_limit_octets (abfd, section);
dc810e39 1569 if ((bfd_size_type) offset > sz
6db658c5 1570 || count > sz - offset
dc810e39 1571 || count != (size_t) count)
252b5132 1572 {
252b5132 1573 bfd_set_error (bfd_error_bad_value);
0a1b45a2 1574 return false;
252b5132 1575 }
252b5132
RH
1576
1577 if (count == 0)
1578 /* Don't bother. */
0a1b45a2 1579 return true;
252b5132
RH
1580
1581 if ((section->flags & SEC_HAS_CONTENTS) == 0)
1582 {
dc810e39 1583 memset (location, 0, (size_t) count);
0a1b45a2 1584 return true;
252b5132
RH
1585 }
1586
1587 if ((section->flags & SEC_IN_MEMORY) != 0)
1588 {
ea882e87
NC
1589 if (section->contents == NULL)
1590 {
1591 /* This can happen because of errors earlier on in the linking process.
1592 We do not want to seg-fault here, so clear the flag and return an
1593 error code. */
1594 section->flags &= ~ SEC_IN_MEMORY;
1595 bfd_set_error (bfd_error_invalid_operation);
0a1b45a2 1596 return false;
ea882e87 1597 }
68ffbac6 1598
ddda4adc 1599 memmove (location, section->contents + offset, (size_t) count);
0a1b45a2 1600 return true;
252b5132
RH
1601 }
1602
1603 return BFD_SEND (abfd, _bfd_get_section_contents,
1604 (abfd, section, location, offset, count));
1605}
1606
eea6121a
AM
1607/*
1608FUNCTION
1609 bfd_malloc_and_get_section
1610
1611SYNOPSIS
0a1b45a2 1612 bool bfd_malloc_and_get_section
eea6121a
AM
1613 (bfd *abfd, asection *section, bfd_byte **buf);
1614
1615DESCRIPTION
1616 Read all data from @var{section} in BFD @var{abfd}
1617 into a buffer, *@var{buf}, malloc'd by this function.
ea7672c1
AM
1618 Return @code{true} on success, @code{false} on failure in which
1619 case *@var{buf} will be NULL.
eea6121a
AM
1620*/
1621
0a1b45a2 1622bool
eea6121a
AM
1623bfd_malloc_and_get_section (bfd *abfd, sec_ptr sec, bfd_byte **buf)
1624{
4a114e3e
L
1625 *buf = NULL;
1626 return bfd_get_full_section_contents (abfd, sec, buf);
eea6121a 1627}
252b5132
RH
1628/*
1629FUNCTION
1630 bfd_copy_private_section_data
1631
1632SYNOPSIS
0a1b45a2 1633 bool bfd_copy_private_section_data
c58b9523 1634 (bfd *ibfd, asection *isec, bfd *obfd, asection *osec);
252b5132
RH
1635
1636DESCRIPTION
1637 Copy private section information from @var{isec} in the BFD
1638 @var{ibfd} to the section @var{osec} in the BFD @var{obfd}.
b34976b6 1639 Return <<TRUE>> on success, <<FALSE>> on error. Possible error
252b5132
RH
1640 returns are:
1641
1642 o <<bfd_error_no_memory>> -
1643 Not enough memory exists to create private data for @var{osec}.
1644
1645.#define bfd_copy_private_section_data(ibfd, isection, obfd, osection) \
07d6d2b8
AM
1646. BFD_SEND (obfd, _bfd_copy_private_section_data, \
1647. (ibfd, isection, obfd, osection))
252b5132
RH
1648*/
1649
72adc230
AM
1650/*
1651FUNCTION
1652 bfd_generic_is_group_section
1653
1654SYNOPSIS
0a1b45a2 1655 bool bfd_generic_is_group_section (bfd *, const asection *sec);
72adc230
AM
1656
1657DESCRIPTION
1658 Returns TRUE if @var{sec} is a member of a group.
1659*/
1660
0a1b45a2 1661bool
72adc230
AM
1662bfd_generic_is_group_section (bfd *abfd ATTRIBUTE_UNUSED,
1663 const asection *sec ATTRIBUTE_UNUSED)
1664{
0a1b45a2 1665 return false;
72adc230
AM
1666}
1667
cb7f4b29
AM
1668/*
1669FUNCTION
1670 bfd_generic_group_name
1671
1672SYNOPSIS
1673 const char *bfd_generic_group_name (bfd *, const asection *sec);
1674
1675DESCRIPTION
1676 Returns group name if @var{sec} is a member of a group.
1677*/
1678
1679const char *
1680bfd_generic_group_name (bfd *abfd ATTRIBUTE_UNUSED,
1681 const asection *sec ATTRIBUTE_UNUSED)
1682{
1683 return NULL;
1684}
1685
b885599b
AM
1686/*
1687FUNCTION
e61463e1 1688 bfd_generic_discard_group
b885599b
AM
1689
1690SYNOPSIS
0a1b45a2 1691 bool bfd_generic_discard_group (bfd *abfd, asection *group);
b885599b
AM
1692
1693DESCRIPTION
1694 Remove all members of @var{group} from the output.
1695*/
1696
0a1b45a2 1697bool
c58b9523
AM
1698bfd_generic_discard_group (bfd *abfd ATTRIBUTE_UNUSED,
1699 asection *group ATTRIBUTE_UNUSED)
b885599b 1700{
0a1b45a2 1701 return true;
b885599b 1702}
d00dd7dc 1703
0a1b45a2 1704bool
d00dd7dc
AM
1705_bfd_nowrite_set_section_contents (bfd *abfd,
1706 sec_ptr section ATTRIBUTE_UNUSED,
1707 const void *location ATTRIBUTE_UNUSED,
1708 file_ptr offset ATTRIBUTE_UNUSED,
1709 bfd_size_type count ATTRIBUTE_UNUSED)
1710{
1711 return _bfd_bool_bfd_false_error (abfd);
1712}
f7502dfe
AM
1713
1714/*
1715INTERNAL_FUNCTION
1716 _bfd_section_size_insane
1717
1718SYNOPSIS
1719 bool _bfd_section_size_insane (bfd *abfd, asection *sec);
1720
1721DESCRIPTION
1722 Returns true if the given section has a size that indicates
1723 it cannot be read from file. Return false if the size is OK
1724 *or* this function can't say one way or the other.
1725
1726*/
1727
1728bool
1729_bfd_section_size_insane (bfd *abfd, asection *sec)
1730{
1731 bfd_size_type size = bfd_get_section_limit_octets (abfd, sec);
1732 if (size == 0)
1733 return false;
1734
1735 if ((bfd_section_flags (sec) & SEC_IN_MEMORY) != 0
1736 /* PR 24753: Linker created sections can be larger than
1737 the file size, eg if they are being used to hold stubs. */
1738 || (bfd_section_flags (sec) & SEC_LINKER_CREATED) != 0
1739 /* PR 24753: Sections which have no content should also be
1740 excluded as they contain no size on disk. */
1741 || (bfd_section_flags (sec) & SEC_HAS_CONTENTS) == 0
1742 /* The MMO file format supports its own special compression
1743 technique, but it uses COMPRESS_SECTION_NONE when loading
1744 a section's contents. */
1745 || bfd_get_flavour (abfd) == bfd_target_mmo_flavour)
1746 return false;
1747
1748 ufile_ptr filesize = bfd_get_file_size (abfd);
1749 if (filesize == 0)
1750 return false;
1751
1752 if (sec->compress_status == DECOMPRESS_SECTION_ZSTD
1753 || sec->compress_status == DECOMPRESS_SECTION_ZLIB)
1754 {
1755 /* PR26946, PR28834: Sanity check compress header uncompressed
1756 size against the original file size, and check that the
1757 compressed section can be read from file. We choose an
1758 arbitrary uncompressed size of 10x the file size, rather than
1759 a compress ratio. The reason being that compiling
1760 "int aaa..a;" with "a" repeated enough times can result in
1761 compression ratios without limit for .debug_str, whereas such
1762 a file will usually also have the enormous symbol
1763 uncompressed in .symtab. */
1764 if (size / 10 > filesize)
1765 {
1766 bfd_set_error (bfd_error_bad_value);
1767 return true;
1768 }
1769 size = sec->compressed_size;
1770 }
1771
1772 if ((ufile_ptr) sec->filepos > filesize || size > filesize - sec->filepos)
1773 {
1774 bfd_set_error (bfd_error_file_truncated);
1775 return true;
1776 }
1777 return false;
1778}