section.c 49 KB

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