section.c 49 KB

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