bfd.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273
  1. /* Generic BFD library interface and support routines.
  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. INODE
  19. typedef bfd, Error reporting, BFD front end, BFD front end
  20. SECTION
  21. <<typedef bfd>>
  22. A BFD has type <<bfd>>; objects of this type are the
  23. cornerstone of any application using BFD. Using BFD
  24. consists of making references though the BFD and to data in the BFD.
  25. Here is the structure that defines the type <<bfd>>. It
  26. contains the major data about the file and pointers
  27. to the rest of the data.
  28. CODE_FRAGMENT
  29. .
  30. .enum bfd_direction
  31. . {
  32. . no_direction = 0,
  33. . read_direction = 1,
  34. . write_direction = 2,
  35. . both_direction = 3
  36. . };
  37. .
  38. .enum bfd_plugin_format
  39. . {
  40. . bfd_plugin_uknown = 0,
  41. . bfd_plugin_yes = 1,
  42. . bfd_plugin_no = 2
  43. . };
  44. .
  45. .struct bfd_build_id
  46. . {
  47. . bfd_size_type size;
  48. . bfd_byte data[1];
  49. . };
  50. .
  51. .struct bfd
  52. .{
  53. . {* The filename the application opened the BFD with. *}
  54. . const char *filename;
  55. .
  56. . {* A pointer to the target jump table. *}
  57. . const struct bfd_target *xvec;
  58. .
  59. . {* The IOSTREAM, and corresponding IO vector that provide access
  60. . to the file backing the BFD. *}
  61. . void *iostream;
  62. . const struct bfd_iovec *iovec;
  63. .
  64. . {* The caching routines use these to maintain a
  65. . least-recently-used list of BFDs. *}
  66. . struct bfd *lru_prev, *lru_next;
  67. .
  68. . {* When a file is closed by the caching routines, BFD retains
  69. . state information on the file here... *}
  70. . ufile_ptr where;
  71. .
  72. . {* File modified time, if mtime_set is TRUE. *}
  73. . long mtime;
  74. .
  75. . {* A unique identifier of the BFD *}
  76. . unsigned int id;
  77. .
  78. . {* The format which belongs to the BFD. (object, core, etc.) *}
  79. . ENUM_BITFIELD (bfd_format) format : 3;
  80. .
  81. . {* The direction with which the BFD was opened. *}
  82. . ENUM_BITFIELD (bfd_direction) direction : 2;
  83. .
  84. . {* Format_specific flags. *}
  85. . flagword flags : 18;
  86. .
  87. . {* Values that may appear in the flags field of a BFD. These also
  88. . appear in the object_flags field of the bfd_target structure, where
  89. . they indicate the set of flags used by that backend (not all flags
  90. . are meaningful for all object file formats) (FIXME: at the moment,
  91. . the object_flags values have mostly just been copied from backend
  92. . to another, and are not necessarily correct). *}
  93. .
  94. .#define BFD_NO_FLAGS 0x00
  95. .
  96. . {* BFD contains relocation entries. *}
  97. .#define HAS_RELOC 0x01
  98. .
  99. . {* BFD is directly executable. *}
  100. .#define EXEC_P 0x02
  101. .
  102. . {* BFD has line number information (basically used for F_LNNO in a
  103. . COFF header). *}
  104. .#define HAS_LINENO 0x04
  105. .
  106. . {* BFD has debugging information. *}
  107. .#define HAS_DEBUG 0x08
  108. .
  109. . {* BFD has symbols. *}
  110. .#define HAS_SYMS 0x10
  111. .
  112. . {* BFD has local symbols (basically used for F_LSYMS in a COFF
  113. . header). *}
  114. .#define HAS_LOCALS 0x20
  115. .
  116. . {* BFD is a dynamic object. *}
  117. .#define DYNAMIC 0x40
  118. .
  119. . {* Text section is write protected (if D_PAGED is not set, this is
  120. . like an a.out NMAGIC file) (the linker sets this by default, but
  121. . clears it for -r or -N). *}
  122. .#define WP_TEXT 0x80
  123. .
  124. . {* BFD is dynamically paged (this is like an a.out ZMAGIC file) (the
  125. . linker sets this by default, but clears it for -r or -n or -N). *}
  126. .#define D_PAGED 0x100
  127. .
  128. . {* BFD is relaxable (this means that bfd_relax_section may be able to
  129. . do something) (sometimes bfd_relax_section can do something even if
  130. . this is not set). *}
  131. .#define BFD_IS_RELAXABLE 0x200
  132. .
  133. . {* This may be set before writing out a BFD to request using a
  134. . traditional format. For example, this is used to request that when
  135. . writing out an a.out object the symbols not be hashed to eliminate
  136. . duplicates. *}
  137. .#define BFD_TRADITIONAL_FORMAT 0x400
  138. .
  139. . {* This flag indicates that the BFD contents are actually cached
  140. . in memory. If this is set, iostream points to a bfd_in_memory
  141. . struct. *}
  142. .#define BFD_IN_MEMORY 0x800
  143. .
  144. . {* This BFD has been created by the linker and doesn't correspond
  145. . to any input file. *}
  146. .#define BFD_LINKER_CREATED 0x1000
  147. .
  148. . {* This may be set before writing out a BFD to request that it
  149. . be written using values for UIDs, GIDs, timestamps, etc. that
  150. . will be consistent from run to run. *}
  151. .#define BFD_DETERMINISTIC_OUTPUT 0x2000
  152. .
  153. . {* Compress sections in this BFD. *}
  154. .#define BFD_COMPRESS 0x4000
  155. .
  156. . {* Decompress sections in this BFD. *}
  157. .#define BFD_DECOMPRESS 0x8000
  158. .
  159. . {* BFD is a dummy, for plugins. *}
  160. .#define BFD_PLUGIN 0x10000
  161. .
  162. . {* Compress sections in this BFD with SHF_COMPRESSED from gABI. *}
  163. .#define BFD_COMPRESS_GABI 0x20000
  164. .
  165. . {* Flags bits to be saved in bfd_preserve_save. *}
  166. .#define BFD_FLAGS_SAVED \
  167. . (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_PLUGIN \
  168. . | BFD_COMPRESS_GABI)
  169. .
  170. . {* Flags bits which are for BFD use only. *}
  171. .#define BFD_FLAGS_FOR_BFD_USE_MASK \
  172. . (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \
  173. . | BFD_PLUGIN | BFD_TRADITIONAL_FORMAT | BFD_DETERMINISTIC_OUTPUT \
  174. . | BFD_COMPRESS_GABI)
  175. .
  176. . {* Is the file descriptor being cached? That is, can it be closed as
  177. . needed, and re-opened when accessed later? *}
  178. . unsigned int cacheable : 1;
  179. .
  180. . {* Marks whether there was a default target specified when the
  181. . BFD was opened. This is used to select which matching algorithm
  182. . to use to choose the back end. *}
  183. . unsigned int target_defaulted : 1;
  184. .
  185. . {* ... and here: (``once'' means at least once). *}
  186. . unsigned int opened_once : 1;
  187. .
  188. . {* Set if we have a locally maintained mtime value, rather than
  189. . getting it from the file each time. *}
  190. . unsigned int mtime_set : 1;
  191. .
  192. . {* Flag set if symbols from this BFD should not be exported. *}
  193. . unsigned int no_export : 1;
  194. .
  195. . {* Remember when output has begun, to stop strange things
  196. . from happening. *}
  197. . unsigned int output_has_begun : 1;
  198. .
  199. . {* Have archive map. *}
  200. . unsigned int has_armap : 1;
  201. .
  202. . {* Set if this is a thin archive. *}
  203. . unsigned int is_thin_archive : 1;
  204. .
  205. . {* Set if only required symbols should be added in the link hash table for
  206. . this object. Used by VMS linkers. *}
  207. . unsigned int selective_search : 1;
  208. .
  209. . {* Set if this is the linker output BFD. *}
  210. . unsigned int is_linker_output : 1;
  211. .
  212. . {* Set if this is the linker input BFD. *}
  213. . unsigned int is_linker_input : 1;
  214. .
  215. . {* If this is an input for a compiler plug-in library. *}
  216. . ENUM_BITFIELD (bfd_plugin_format) plugin_format : 2;
  217. .
  218. . {* Set if this is a plugin output file. *}
  219. . unsigned int lto_output : 1;
  220. .
  221. . {* Set to dummy BFD created when claimed by a compiler plug-in
  222. . library. *}
  223. . bfd *plugin_dummy_bfd;
  224. .
  225. . {* Currently my_archive is tested before adding origin to
  226. . anything. I believe that this can become always an add of
  227. . origin, with origin set to 0 for non archive files. *}
  228. . ufile_ptr origin;
  229. .
  230. . {* The origin in the archive of the proxy entry. This will
  231. . normally be the same as origin, except for thin archives,
  232. . when it will contain the current offset of the proxy in the
  233. . thin archive rather than the offset of the bfd in its actual
  234. . container. *}
  235. . ufile_ptr proxy_origin;
  236. .
  237. . {* A hash table for section names. *}
  238. . struct bfd_hash_table section_htab;
  239. .
  240. . {* Pointer to linked list of sections. *}
  241. . struct bfd_section *sections;
  242. .
  243. . {* The last section on the section list. *}
  244. . struct bfd_section *section_last;
  245. .
  246. . {* The number of sections. *}
  247. . unsigned int section_count;
  248. .
  249. . {* A field used by _bfd_generic_link_add_archive_symbols. This will
  250. . be used only for archive elements. *}
  251. . int archive_pass;
  252. .
  253. . {* Stuff only useful for object files:
  254. . The start address. *}
  255. . bfd_vma start_address;
  256. .
  257. . {* Symbol table for output BFD (with symcount entries).
  258. . Also used by the linker to cache input BFD symbols. *}
  259. . struct bfd_symbol **outsymbols;
  260. .
  261. . {* Used for input and output. *}
  262. . unsigned int symcount;
  263. .
  264. . {* Used for slurped dynamic symbol tables. *}
  265. . unsigned int dynsymcount;
  266. .
  267. . {* Pointer to structure which contains architecture information. *}
  268. . const struct bfd_arch_info *arch_info;
  269. .
  270. . {* Stuff only useful for archives. *}
  271. . void *arelt_data;
  272. . struct bfd *my_archive; {* The containing archive BFD. *}
  273. . struct bfd *archive_next; {* The next BFD in the archive. *}
  274. . struct bfd *archive_head; {* The first BFD in the archive. *}
  275. . struct bfd *nested_archives; {* List of nested archive in a flattened
  276. . thin archive. *}
  277. .
  278. . union {
  279. . {* For input BFDs, a chain of BFDs involved in a link. *}
  280. . struct bfd *next;
  281. . {* For output BFD, the linker hash table. *}
  282. . struct bfd_link_hash_table *hash;
  283. . } link;
  284. .
  285. . {* Used by the back end to hold private data. *}
  286. . union
  287. . {
  288. . struct aout_data_struct *aout_data;
  289. . struct artdata *aout_ar_data;
  290. . struct _oasys_data *oasys_obj_data;
  291. . struct _oasys_ar_data *oasys_ar_data;
  292. . struct coff_tdata *coff_obj_data;
  293. . struct pe_tdata *pe_obj_data;
  294. . struct xcoff_tdata *xcoff_obj_data;
  295. . struct ecoff_tdata *ecoff_obj_data;
  296. . struct ieee_data_struct *ieee_data;
  297. . struct ieee_ar_data_struct *ieee_ar_data;
  298. . struct srec_data_struct *srec_data;
  299. . struct verilog_data_struct *verilog_data;
  300. . struct ihex_data_struct *ihex_data;
  301. . struct tekhex_data_struct *tekhex_data;
  302. . struct elf_obj_tdata *elf_obj_data;
  303. . struct nlm_obj_tdata *nlm_obj_data;
  304. . struct bout_data_struct *bout_data;
  305. . struct mmo_data_struct *mmo_data;
  306. . struct sun_core_struct *sun_core_data;
  307. . struct sco5_core_struct *sco5_core_data;
  308. . struct trad_core_struct *trad_core_data;
  309. . struct som_data_struct *som_data;
  310. . struct hpux_core_struct *hpux_core_data;
  311. . struct hppabsd_core_struct *hppabsd_core_data;
  312. . struct sgi_core_struct *sgi_core_data;
  313. . struct lynx_core_struct *lynx_core_data;
  314. . struct osf_core_struct *osf_core_data;
  315. . struct cisco_core_struct *cisco_core_data;
  316. . struct versados_data_struct *versados_data;
  317. . struct netbsd_core_struct *netbsd_core_data;
  318. . struct mach_o_data_struct *mach_o_data;
  319. . struct mach_o_fat_data_struct *mach_o_fat_data;
  320. . struct plugin_data_struct *plugin_data;
  321. . struct bfd_pef_data_struct *pef_data;
  322. . struct bfd_pef_xlib_data_struct *pef_xlib_data;
  323. . struct bfd_sym_data_struct *sym_data;
  324. . void *any;
  325. . }
  326. . tdata;
  327. .
  328. . {* Used by the application to hold private data. *}
  329. . void *usrdata;
  330. .
  331. . {* Where all the allocated stuff under this BFD goes. This is a
  332. . struct objalloc *, but we use void * to avoid requiring the inclusion
  333. . of objalloc.h. *}
  334. . void *memory;
  335. .
  336. . {* For input BFDs, the build ID, if the object has one. *}
  337. . const struct bfd_build_id *build_id;
  338. .};
  339. .
  340. .{* See note beside bfd_set_section_userdata. *}
  341. .static inline bfd_boolean
  342. .bfd_set_cacheable (bfd * abfd, bfd_boolean val)
  343. .{
  344. . abfd->cacheable = val;
  345. . return TRUE;
  346. .}
  347. .
  348. */
  349. #include "sysdep.h"
  350. #include <stdarg.h>
  351. #include "bfd.h"
  352. #include "bfdver.h"
  353. #include "libiberty.h"
  354. #include "demangle.h"
  355. #include "safe-ctype.h"
  356. #include "bfdlink.h"
  357. #include "libbfd.h"
  358. #include "coff/internal.h"
  359. #include "coff/sym.h"
  360. #include "libcoff.h"
  361. #include "libecoff.h"
  362. #undef obj_symbols
  363. #include "elf-bfd.h"
  364. #ifndef EXIT_FAILURE
  365. #define EXIT_FAILURE 1
  366. #endif
  367. /* provide storage for subsystem, stack and heap data which may have been
  368. passed in on the command line. Ld puts this data into a bfd_link_info
  369. struct which ultimately gets passed in to the bfd. When it arrives, copy
  370. it to the following struct so that the data will be available in coffcode.h
  371. where it is needed. The typedef's used are defined in bfd.h */
  372. /*
  373. INODE
  374. Error reporting, Miscellaneous, typedef bfd, BFD front end
  375. SECTION
  376. Error reporting
  377. Most BFD functions return nonzero on success (check their
  378. individual documentation for precise semantics). On an error,
  379. they call <<bfd_set_error>> to set an error condition that callers
  380. can check by calling <<bfd_get_error>>.
  381. If that returns <<bfd_error_system_call>>, then check
  382. <<errno>>.
  383. The easiest way to report a BFD error to the user is to
  384. use <<bfd_perror>>.
  385. SUBSECTION
  386. Type <<bfd_error_type>>
  387. The values returned by <<bfd_get_error>> are defined by the
  388. enumerated type <<bfd_error_type>>.
  389. CODE_FRAGMENT
  390. .
  391. .typedef enum bfd_error
  392. .{
  393. . bfd_error_no_error = 0,
  394. . bfd_error_system_call,
  395. . bfd_error_invalid_target,
  396. . bfd_error_wrong_format,
  397. . bfd_error_wrong_object_format,
  398. . bfd_error_invalid_operation,
  399. . bfd_error_no_memory,
  400. . bfd_error_no_symbols,
  401. . bfd_error_no_armap,
  402. . bfd_error_no_more_archived_files,
  403. . bfd_error_malformed_archive,
  404. . bfd_error_missing_dso,
  405. . bfd_error_file_not_recognized,
  406. . bfd_error_file_ambiguously_recognized,
  407. . bfd_error_no_contents,
  408. . bfd_error_nonrepresentable_section,
  409. . bfd_error_no_debug_section,
  410. . bfd_error_bad_value,
  411. . bfd_error_file_truncated,
  412. . bfd_error_file_too_big,
  413. . bfd_error_on_input,
  414. . bfd_error_invalid_error_code
  415. .}
  416. .bfd_error_type;
  417. .
  418. */
  419. static bfd_error_type bfd_error = bfd_error_no_error;
  420. static bfd *input_bfd = NULL;
  421. static bfd_error_type input_error = bfd_error_no_error;
  422. const char *const bfd_errmsgs[] =
  423. {
  424. N_("No error"),
  425. N_("System call error"),
  426. N_("Invalid bfd target"),
  427. N_("File in wrong format"),
  428. N_("Archive object file in wrong format"),
  429. N_("Invalid operation"),
  430. N_("Memory exhausted"),
  431. N_("No symbols"),
  432. N_("Archive has no index; run ranlib to add one"),
  433. N_("No more archived files"),
  434. N_("Malformed archive"),
  435. N_("DSO missing from command line"),
  436. N_("File format not recognized"),
  437. N_("File format is ambiguous"),
  438. N_("Section has no contents"),
  439. N_("Nonrepresentable section on output"),
  440. N_("Symbol needs debug section which does not exist"),
  441. N_("Bad value"),
  442. N_("File truncated"),
  443. N_("File too big"),
  444. N_("Error reading %s: %s"),
  445. N_("#<Invalid error code>")
  446. };
  447. /*
  448. FUNCTION
  449. bfd_get_error
  450. SYNOPSIS
  451. bfd_error_type bfd_get_error (void);
  452. DESCRIPTION
  453. Return the current BFD error condition.
  454. */
  455. bfd_error_type
  456. bfd_get_error (void)
  457. {
  458. return bfd_error;
  459. }
  460. /*
  461. FUNCTION
  462. bfd_set_error
  463. SYNOPSIS
  464. void bfd_set_error (bfd_error_type error_tag, ...);
  465. DESCRIPTION
  466. Set the BFD error condition to be @var{error_tag}.
  467. If @var{error_tag} is bfd_error_on_input, then this function
  468. takes two more parameters, the input bfd where the error
  469. occurred, and the bfd_error_type error.
  470. */
  471. void
  472. bfd_set_error (bfd_error_type error_tag, ...)
  473. {
  474. bfd_error = error_tag;
  475. if (error_tag == bfd_error_on_input)
  476. {
  477. /* This is an error that occurred during bfd_close when
  478. writing an archive, but on one of the input files. */
  479. va_list ap;
  480. va_start (ap, error_tag);
  481. input_bfd = va_arg (ap, bfd *);
  482. input_error = (bfd_error_type) va_arg (ap, int);
  483. if (input_error >= bfd_error_on_input)
  484. abort ();
  485. va_end (ap);
  486. }
  487. }
  488. /*
  489. FUNCTION
  490. bfd_errmsg
  491. SYNOPSIS
  492. const char *bfd_errmsg (bfd_error_type error_tag);
  493. DESCRIPTION
  494. Return a string describing the error @var{error_tag}, or
  495. the system error if @var{error_tag} is <<bfd_error_system_call>>.
  496. */
  497. const char *
  498. bfd_errmsg (bfd_error_type error_tag)
  499. {
  500. #ifndef errno
  501. extern int errno;
  502. #endif
  503. if (error_tag == bfd_error_on_input)
  504. {
  505. char *buf;
  506. const char *msg = bfd_errmsg (input_error);
  507. if (asprintf (&buf, _(bfd_errmsgs [error_tag]), input_bfd->filename, msg)
  508. != -1)
  509. return buf;
  510. /* Ick, what to do on out of memory? */
  511. return msg;
  512. }
  513. if (error_tag == bfd_error_system_call)
  514. return xstrerror (errno);
  515. if (error_tag > bfd_error_invalid_error_code)
  516. error_tag = bfd_error_invalid_error_code; /* sanity check */
  517. return _(bfd_errmsgs [error_tag]);
  518. }
  519. /*
  520. FUNCTION
  521. bfd_perror
  522. SYNOPSIS
  523. void bfd_perror (const char *message);
  524. DESCRIPTION
  525. Print to the standard error stream a string describing the
  526. last BFD error that occurred, or the last system error if
  527. the last BFD error was a system call failure. If @var{message}
  528. is non-NULL and non-empty, the error string printed is preceded
  529. by @var{message}, a colon, and a space. It is followed by a newline.
  530. */
  531. void
  532. bfd_perror (const char *message)
  533. {
  534. fflush (stdout);
  535. if (message == NULL || *message == '\0')
  536. fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ()));
  537. else
  538. fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ()));
  539. fflush (stderr);
  540. }
  541. /*
  542. SUBSECTION
  543. BFD error handler
  544. Some BFD functions want to print messages describing the
  545. problem. They call a BFD error handler function. This
  546. function may be overridden by the program.
  547. The BFD error handler acts like printf.
  548. CODE_FRAGMENT
  549. .
  550. .typedef void (*bfd_error_handler_type) (const char *, ...);
  551. .
  552. */
  553. /* The program name used when printing BFD error messages. */
  554. static const char *_bfd_error_program_name;
  555. /* This is the default routine to handle BFD error messages.
  556. Like fprintf (stderr, ...), but also handles some extra format specifiers.
  557. %A section name from section. For group components, print group name too.
  558. %B file name from bfd. For archive components, prints archive too.
  559. Note - because these two extra format specifiers require special handling
  560. they are scanned for and processed in this function, before calling
  561. vfprintf. This means that the *arguments* for these format specifiers
  562. must be the first ones in the variable argument list, regardless of where
  563. the specifiers appear in the format string. Thus for example calling
  564. this function with a format string of:
  565. "blah %s blah %A blah %d blah %B"
  566. would involve passing the arguments as:
  567. "blah %s blah %A blah %d blah %B",
  568. asection_for_the_%A,
  569. bfd_for_the_%B,
  570. string_for_the_%s,
  571. integer_for_the_%d);
  572. */
  573. void
  574. _bfd_default_error_handler (const char *fmt, ...)
  575. {
  576. va_list ap;
  577. char *bufp;
  578. const char *new_fmt, *p;
  579. size_t avail = 1000;
  580. char buf[1000];
  581. /* PR 4992: Don't interrupt output being sent to stdout. */
  582. fflush (stdout);
  583. if (_bfd_error_program_name != NULL)
  584. fprintf (stderr, "%s: ", _bfd_error_program_name);
  585. else
  586. fprintf (stderr, "BFD: ");
  587. va_start (ap, fmt);
  588. new_fmt = fmt;
  589. bufp = buf;
  590. /* Reserve enough space for the existing format string. */
  591. avail -= strlen (fmt) + 1;
  592. if (avail > 1000)
  593. _exit (EXIT_FAILURE);
  594. p = fmt;
  595. while (1)
  596. {
  597. char *q;
  598. size_t len, extra, trim;
  599. p = strchr (p, '%');
  600. if (p == NULL || p[1] == '\0')
  601. {
  602. if (new_fmt == buf)
  603. {
  604. len = strlen (fmt);
  605. memcpy (bufp, fmt, len + 1);
  606. }
  607. break;
  608. }
  609. if (p[1] == 'A' || p[1] == 'B')
  610. {
  611. len = p - fmt;
  612. memcpy (bufp, fmt, len);
  613. bufp += len;
  614. fmt = p + 2;
  615. new_fmt = buf;
  616. /* If we run out of space, tough, you lose your ridiculously
  617. long file or section name. It's not safe to try to alloc
  618. memory here; We might be printing an out of memory message. */
  619. if (avail == 0)
  620. {
  621. *bufp++ = '*';
  622. *bufp++ = '*';
  623. *bufp = '\0';
  624. }
  625. else
  626. {
  627. if (p[1] == 'B')
  628. {
  629. bfd *abfd = va_arg (ap, bfd *);
  630. if (abfd == NULL)
  631. /* Invoking %B with a null bfd pointer is an internal error. */
  632. abort ();
  633. else if (abfd->my_archive)
  634. snprintf (bufp, avail, "%s(%s)",
  635. abfd->my_archive->filename, abfd->filename);
  636. else
  637. snprintf (bufp, avail, "%s", abfd->filename);
  638. }
  639. else
  640. {
  641. asection *sec = va_arg (ap, asection *);
  642. bfd *abfd;
  643. const char *group = NULL;
  644. struct coff_comdat_info *ci;
  645. if (sec == NULL)
  646. /* Invoking %A with a null section pointer is an internal error. */
  647. abort ();
  648. abfd = sec->owner;
  649. if (abfd != NULL
  650. && bfd_get_flavour (abfd) == bfd_target_elf_flavour
  651. && elf_next_in_group (sec) != NULL
  652. && (sec->flags & SEC_GROUP) == 0)
  653. group = elf_group_name (sec);
  654. else if (abfd != NULL
  655. && bfd_get_flavour (abfd) == bfd_target_coff_flavour
  656. && (ci = bfd_coff_get_comdat_section (sec->owner,
  657. sec)) != NULL)
  658. group = ci->name;
  659. if (group != NULL)
  660. snprintf (bufp, avail, "%s[%s]", sec->name, group);
  661. else
  662. snprintf (bufp, avail, "%s", sec->name);
  663. }
  664. len = strlen (bufp);
  665. avail = avail - len + 2;
  666. /* We need to replace any '%' we printed by "%%".
  667. First count how many. */
  668. q = bufp;
  669. bufp += len;
  670. extra = 0;
  671. while ((q = strchr (q, '%')) != NULL)
  672. {
  673. ++q;
  674. ++extra;
  675. }
  676. /* If there isn't room, trim off the end of the string. */
  677. q = bufp;
  678. bufp += extra;
  679. if (extra > avail)
  680. {
  681. trim = extra - avail;
  682. bufp -= trim;
  683. do
  684. {
  685. if (*--q == '%')
  686. --extra;
  687. }
  688. while (--trim != 0);
  689. *q = '\0';
  690. avail = extra;
  691. }
  692. avail -= extra;
  693. /* Now double all '%' chars, shuffling the string as we go. */
  694. while (extra != 0)
  695. {
  696. while ((q[extra] = *q) != '%')
  697. --q;
  698. q[--extra] = '%';
  699. --q;
  700. }
  701. }
  702. }
  703. p = p + 2;
  704. }
  705. vfprintf (stderr, new_fmt, ap);
  706. va_end (ap);
  707. /* On AIX, putc is implemented as a macro that triggers a -Wunused-value
  708. warning, so use the fputc function to avoid it. */
  709. fputc ('\n', stderr);
  710. fflush (stderr);
  711. }
  712. /* This is a function pointer to the routine which should handle BFD
  713. error messages. It is called when a BFD routine encounters an
  714. error for which it wants to print a message. Going through a
  715. function pointer permits a program linked against BFD to intercept
  716. the messages and deal with them itself. */
  717. bfd_error_handler_type _bfd_error_handler = _bfd_default_error_handler;
  718. /*
  719. FUNCTION
  720. bfd_set_error_handler
  721. SYNOPSIS
  722. bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
  723. DESCRIPTION
  724. Set the BFD error handler function. Returns the previous
  725. function.
  726. */
  727. bfd_error_handler_type
  728. bfd_set_error_handler (bfd_error_handler_type pnew)
  729. {
  730. bfd_error_handler_type pold;
  731. pold = _bfd_error_handler;
  732. _bfd_error_handler = pnew;
  733. return pold;
  734. }
  735. /*
  736. FUNCTION
  737. bfd_set_error_program_name
  738. SYNOPSIS
  739. void bfd_set_error_program_name (const char *);
  740. DESCRIPTION
  741. Set the program name to use when printing a BFD error. This
  742. is printed before the error message followed by a colon and
  743. space. The string must not be changed after it is passed to
  744. this function.
  745. */
  746. void
  747. bfd_set_error_program_name (const char *name)
  748. {
  749. _bfd_error_program_name = name;
  750. }
  751. /*
  752. FUNCTION
  753. bfd_get_error_handler
  754. SYNOPSIS
  755. bfd_error_handler_type bfd_get_error_handler (void);
  756. DESCRIPTION
  757. Return the BFD error handler function.
  758. */
  759. bfd_error_handler_type
  760. bfd_get_error_handler (void)
  761. {
  762. return _bfd_error_handler;
  763. }
  764. /*
  765. SUBSECTION
  766. BFD assert handler
  767. If BFD finds an internal inconsistency, the bfd assert
  768. handler is called with information on the BFD version, BFD
  769. source file and line. If this happens, most programs linked
  770. against BFD are expected to want to exit with an error, or mark
  771. the current BFD operation as failed, so it is recommended to
  772. override the default handler, which just calls
  773. _bfd_error_handler and continues.
  774. CODE_FRAGMENT
  775. .
  776. .typedef void (*bfd_assert_handler_type) (const char *bfd_formatmsg,
  777. . const char *bfd_version,
  778. . const char *bfd_file,
  779. . int bfd_line);
  780. .
  781. */
  782. /* Note the use of bfd_ prefix on the parameter names above: we want to
  783. show which one is the message and which is the version by naming the
  784. parameters, but avoid polluting the program-using-bfd namespace as
  785. the typedef is visible in the exported headers that the program
  786. includes. Below, it's just for consistency. */
  787. static void
  788. _bfd_default_assert_handler (const char *bfd_formatmsg,
  789. const char *bfd_version,
  790. const char *bfd_file,
  791. int bfd_line)
  792. {
  793. (*_bfd_error_handler) (bfd_formatmsg, bfd_version, bfd_file, bfd_line);
  794. }
  795. /* Similar to _bfd_error_handler, a program can decide to exit on an
  796. internal BFD error. We use a non-variadic type to simplify passing
  797. on parameters to other functions, e.g. _bfd_error_handler. */
  798. bfd_assert_handler_type _bfd_assert_handler = _bfd_default_assert_handler;
  799. /*
  800. FUNCTION
  801. bfd_set_assert_handler
  802. SYNOPSIS
  803. bfd_assert_handler_type bfd_set_assert_handler (bfd_assert_handler_type);
  804. DESCRIPTION
  805. Set the BFD assert handler function. Returns the previous
  806. function.
  807. */
  808. bfd_assert_handler_type
  809. bfd_set_assert_handler (bfd_assert_handler_type pnew)
  810. {
  811. bfd_assert_handler_type pold;
  812. pold = _bfd_assert_handler;
  813. _bfd_assert_handler = pnew;
  814. return pold;
  815. }
  816. /*
  817. FUNCTION
  818. bfd_get_assert_handler
  819. SYNOPSIS
  820. bfd_assert_handler_type bfd_get_assert_handler (void);
  821. DESCRIPTION
  822. Return the BFD assert handler function.
  823. */
  824. bfd_assert_handler_type
  825. bfd_get_assert_handler (void)
  826. {
  827. return _bfd_assert_handler;
  828. }
  829. /*
  830. INODE
  831. Miscellaneous, Memory Usage, Error reporting, BFD front end
  832. SECTION
  833. Miscellaneous
  834. SUBSECTION
  835. Miscellaneous functions
  836. */
  837. /*
  838. FUNCTION
  839. bfd_get_reloc_upper_bound
  840. SYNOPSIS
  841. long bfd_get_reloc_upper_bound (bfd *abfd, asection *sect);
  842. DESCRIPTION
  843. Return the number of bytes required to store the
  844. relocation information associated with section @var{sect}
  845. attached to bfd @var{abfd}. If an error occurs, return -1.
  846. */
  847. long
  848. bfd_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
  849. {
  850. if (abfd->format != bfd_object)
  851. {
  852. bfd_set_error (bfd_error_invalid_operation);
  853. return -1;
  854. }
  855. return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
  856. }
  857. /*
  858. FUNCTION
  859. bfd_canonicalize_reloc
  860. SYNOPSIS
  861. long bfd_canonicalize_reloc
  862. (bfd *abfd, asection *sec, arelent **loc, asymbol **syms);
  863. DESCRIPTION
  864. Call the back end associated with the open BFD
  865. @var{abfd} and translate the external form of the relocation
  866. information attached to @var{sec} into the internal canonical
  867. form. Place the table into memory at @var{loc}, which has
  868. been preallocated, usually by a call to
  869. <<bfd_get_reloc_upper_bound>>. Returns the number of relocs, or
  870. -1 on error.
  871. The @var{syms} table is also needed for horrible internal magic
  872. reasons.
  873. */
  874. long
  875. bfd_canonicalize_reloc (bfd *abfd,
  876. sec_ptr asect,
  877. arelent **location,
  878. asymbol **symbols)
  879. {
  880. if (abfd->format != bfd_object)
  881. {
  882. bfd_set_error (bfd_error_invalid_operation);
  883. return -1;
  884. }
  885. return BFD_SEND (abfd, _bfd_canonicalize_reloc,
  886. (abfd, asect, location, symbols));
  887. }
  888. /*
  889. FUNCTION
  890. bfd_set_reloc
  891. SYNOPSIS
  892. void bfd_set_reloc
  893. (bfd *abfd, asection *sec, arelent **rel, unsigned int count);
  894. DESCRIPTION
  895. Set the relocation pointer and count within
  896. section @var{sec} to the values @var{rel} and @var{count}.
  897. The argument @var{abfd} is ignored.
  898. */
  899. void
  900. bfd_set_reloc (bfd *ignore_abfd ATTRIBUTE_UNUSED,
  901. sec_ptr asect,
  902. arelent **location,
  903. unsigned int count)
  904. {
  905. asect->orelocation = location;
  906. asect->reloc_count = count;
  907. }
  908. /*
  909. FUNCTION
  910. bfd_set_file_flags
  911. SYNOPSIS
  912. bfd_boolean bfd_set_file_flags (bfd *abfd, flagword flags);
  913. DESCRIPTION
  914. Set the flag word in the BFD @var{abfd} to the value @var{flags}.
  915. Possible errors are:
  916. o <<bfd_error_wrong_format>> - The target bfd was not of object format.
  917. o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
  918. o <<bfd_error_invalid_operation>> -
  919. The flag word contained a bit which was not applicable to the
  920. type of file. E.g., an attempt was made to set the <<D_PAGED>> bit
  921. on a BFD format which does not support demand paging.
  922. */
  923. bfd_boolean
  924. bfd_set_file_flags (bfd *abfd, flagword flags)
  925. {
  926. if (abfd->format != bfd_object)
  927. {
  928. bfd_set_error (bfd_error_wrong_format);
  929. return FALSE;
  930. }
  931. if (bfd_read_p (abfd))
  932. {
  933. bfd_set_error (bfd_error_invalid_operation);
  934. return FALSE;
  935. }
  936. bfd_get_file_flags (abfd) = flags;
  937. if ((flags & bfd_applicable_file_flags (abfd)) != flags)
  938. {
  939. bfd_set_error (bfd_error_invalid_operation);
  940. return FALSE;
  941. }
  942. return TRUE;
  943. }
  944. void
  945. bfd_assert (const char *file, int line)
  946. {
  947. (*_bfd_assert_handler) (_("BFD %s assertion fail %s:%d"),
  948. BFD_VERSION_STRING, file, line);
  949. }
  950. /* A more or less friendly abort message. In libbfd.h abort is
  951. defined to call this function. */
  952. void
  953. _bfd_abort (const char *file, int line, const char *fn)
  954. {
  955. if (fn != NULL)
  956. (*_bfd_error_handler)
  957. (_("BFD %s internal error, aborting at %s:%d in %s\n"),
  958. BFD_VERSION_STRING, file, line, fn);
  959. else
  960. (*_bfd_error_handler)
  961. (_("BFD %s internal error, aborting at %s:%d\n"),
  962. BFD_VERSION_STRING, file, line);
  963. (*_bfd_error_handler) (_("Please report this bug.\n"));
  964. _exit (EXIT_FAILURE);
  965. }
  966. /*
  967. FUNCTION
  968. bfd_get_arch_size
  969. SYNOPSIS
  970. int bfd_get_arch_size (bfd *abfd);
  971. DESCRIPTION
  972. Returns the normalized architecture address size, in bits, as
  973. determined by the object file's format. By normalized, we mean
  974. either 32 or 64. For ELF, this information is included in the
  975. header. Use bfd_arch_bits_per_address for number of bits in
  976. the architecture address.
  977. RETURNS
  978. Returns the arch size in bits if known, <<-1>> otherwise.
  979. */
  980. int
  981. bfd_get_arch_size (bfd *abfd)
  982. {
  983. if (abfd->xvec->flavour == bfd_target_elf_flavour)
  984. return get_elf_backend_data (abfd)->s->arch_size;
  985. return bfd_arch_bits_per_address (abfd) > 32 ? 64 : 32;
  986. }
  987. /*
  988. FUNCTION
  989. bfd_get_sign_extend_vma
  990. SYNOPSIS
  991. int bfd_get_sign_extend_vma (bfd *abfd);
  992. DESCRIPTION
  993. Indicates if the target architecture "naturally" sign extends
  994. an address. Some architectures implicitly sign extend address
  995. values when they are converted to types larger than the size
  996. of an address. For instance, bfd_get_start_address() will
  997. return an address sign extended to fill a bfd_vma when this is
  998. the case.
  999. RETURNS
  1000. Returns <<1>> if the target architecture is known to sign
  1001. extend addresses, <<0>> if the target architecture is known to
  1002. not sign extend addresses, and <<-1>> otherwise.
  1003. */
  1004. int
  1005. bfd_get_sign_extend_vma (bfd *abfd)
  1006. {
  1007. char *name;
  1008. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  1009. return get_elf_backend_data (abfd)->sign_extend_vma;
  1010. name = bfd_get_target (abfd);
  1011. /* Return a proper value for DJGPP & PE COFF.
  1012. This function is required for DWARF2 support, but there is
  1013. no place to store this information in the COFF back end.
  1014. Should enough other COFF targets add support for DWARF2,
  1015. a place will have to be found. Until then, this hack will do. */
  1016. if (CONST_STRNEQ (name, "coff-go32")
  1017. || strcmp (name, "pe-i386") == 0
  1018. || strcmp (name, "pei-i386") == 0
  1019. || strcmp (name, "pe-x86-64") == 0
  1020. || strcmp (name, "pei-x86-64") == 0
  1021. || strcmp (name, "pe-arm-wince-little") == 0
  1022. || strcmp (name, "pei-arm-wince-little") == 0
  1023. || strcmp (name, "aixcoff-rs6000") == 0)
  1024. return 1;
  1025. if (CONST_STRNEQ (name, "mach-o"))
  1026. return 0;
  1027. bfd_set_error (bfd_error_wrong_format);
  1028. return -1;
  1029. }
  1030. /*
  1031. FUNCTION
  1032. bfd_set_start_address
  1033. SYNOPSIS
  1034. bfd_boolean bfd_set_start_address (bfd *abfd, bfd_vma vma);
  1035. DESCRIPTION
  1036. Make @var{vma} the entry point of output BFD @var{abfd}.
  1037. RETURNS
  1038. Returns <<TRUE>> on success, <<FALSE>> otherwise.
  1039. */
  1040. bfd_boolean
  1041. bfd_set_start_address (bfd *abfd, bfd_vma vma)
  1042. {
  1043. abfd->start_address = vma;
  1044. return TRUE;
  1045. }
  1046. /*
  1047. FUNCTION
  1048. bfd_get_gp_size
  1049. SYNOPSIS
  1050. unsigned int bfd_get_gp_size (bfd *abfd);
  1051. DESCRIPTION
  1052. Return the maximum size of objects to be optimized using the GP
  1053. register under MIPS ECOFF. This is typically set by the <<-G>>
  1054. argument to the compiler, assembler or linker.
  1055. */
  1056. unsigned int
  1057. bfd_get_gp_size (bfd *abfd)
  1058. {
  1059. if (abfd->format == bfd_object)
  1060. {
  1061. if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
  1062. return ecoff_data (abfd)->gp_size;
  1063. else if (abfd->xvec->flavour == bfd_target_elf_flavour)
  1064. return elf_gp_size (abfd);
  1065. }
  1066. return 0;
  1067. }
  1068. /*
  1069. FUNCTION
  1070. bfd_set_gp_size
  1071. SYNOPSIS
  1072. void bfd_set_gp_size (bfd *abfd, unsigned int i);
  1073. DESCRIPTION
  1074. Set the maximum size of objects to be optimized using the GP
  1075. register under ECOFF or MIPS ELF. This is typically set by
  1076. the <<-G>> argument to the compiler, assembler or linker.
  1077. */
  1078. void
  1079. bfd_set_gp_size (bfd *abfd, unsigned int i)
  1080. {
  1081. /* Don't try to set GP size on an archive or core file! */
  1082. if (abfd->format != bfd_object)
  1083. return;
  1084. if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
  1085. ecoff_data (abfd)->gp_size = i;
  1086. else if (abfd->xvec->flavour == bfd_target_elf_flavour)
  1087. elf_gp_size (abfd) = i;
  1088. }
  1089. /* Get the GP value. This is an internal function used by some of the
  1090. relocation special_function routines on targets which support a GP
  1091. register. */
  1092. bfd_vma
  1093. _bfd_get_gp_value (bfd *abfd)
  1094. {
  1095. if (! abfd)
  1096. return 0;
  1097. if (abfd->format != bfd_object)
  1098. return 0;
  1099. if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
  1100. return ecoff_data (abfd)->gp;
  1101. else if (abfd->xvec->flavour == bfd_target_elf_flavour)
  1102. return elf_gp (abfd);
  1103. return 0;
  1104. }
  1105. /* Set the GP value. */
  1106. void
  1107. _bfd_set_gp_value (bfd *abfd, bfd_vma v)
  1108. {
  1109. if (! abfd)
  1110. abort ();
  1111. if (abfd->format != bfd_object)
  1112. return;
  1113. if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
  1114. ecoff_data (abfd)->gp = v;
  1115. else if (abfd->xvec->flavour == bfd_target_elf_flavour)
  1116. elf_gp (abfd) = v;
  1117. }
  1118. /*
  1119. FUNCTION
  1120. bfd_scan_vma
  1121. SYNOPSIS
  1122. bfd_vma bfd_scan_vma (const char *string, const char **end, int base);
  1123. DESCRIPTION
  1124. Convert, like <<strtoul>>, a numerical expression
  1125. @var{string} into a <<bfd_vma>> integer, and return that integer.
  1126. (Though without as many bells and whistles as <<strtoul>>.)
  1127. The expression is assumed to be unsigned (i.e., positive).
  1128. If given a @var{base}, it is used as the base for conversion.
  1129. A base of 0 causes the function to interpret the string
  1130. in hex if a leading "0x" or "0X" is found, otherwise
  1131. in octal if a leading zero is found, otherwise in decimal.
  1132. If the value would overflow, the maximum <<bfd_vma>> value is
  1133. returned.
  1134. */
  1135. bfd_vma
  1136. bfd_scan_vma (const char *string, const char **end, int base)
  1137. {
  1138. bfd_vma value;
  1139. bfd_vma cutoff;
  1140. unsigned int cutlim;
  1141. int overflow;
  1142. /* Let the host do it if possible. */
  1143. if (sizeof (bfd_vma) <= sizeof (unsigned long))
  1144. return strtoul (string, (char **) end, base);
  1145. #ifdef HAVE_STRTOULL
  1146. if (sizeof (bfd_vma) <= sizeof (unsigned long long))
  1147. return strtoull (string, (char **) end, base);
  1148. #endif
  1149. if (base == 0)
  1150. {
  1151. if (string[0] == '0')
  1152. {
  1153. if ((string[1] == 'x') || (string[1] == 'X'))
  1154. base = 16;
  1155. else
  1156. base = 8;
  1157. }
  1158. }
  1159. if ((base < 2) || (base > 36))
  1160. base = 10;
  1161. if (base == 16
  1162. && string[0] == '0'
  1163. && (string[1] == 'x' || string[1] == 'X')
  1164. && ISXDIGIT (string[2]))
  1165. {
  1166. string += 2;
  1167. }
  1168. cutoff = (~ (bfd_vma) 0) / (bfd_vma) base;
  1169. cutlim = (~ (bfd_vma) 0) % (bfd_vma) base;
  1170. value = 0;
  1171. overflow = 0;
  1172. while (1)
  1173. {
  1174. unsigned int digit;
  1175. digit = *string;
  1176. if (ISDIGIT (digit))
  1177. digit = digit - '0';
  1178. else if (ISALPHA (digit))
  1179. digit = TOUPPER (digit) - 'A' + 10;
  1180. else
  1181. break;
  1182. if (digit >= (unsigned int) base)
  1183. break;
  1184. if (value > cutoff || (value == cutoff && digit > cutlim))
  1185. overflow = 1;
  1186. value = value * base + digit;
  1187. ++string;
  1188. }
  1189. if (overflow)
  1190. value = ~ (bfd_vma) 0;
  1191. if (end != NULL)
  1192. *end = string;
  1193. return value;
  1194. }
  1195. /*
  1196. FUNCTION
  1197. bfd_copy_private_header_data
  1198. SYNOPSIS
  1199. bfd_boolean bfd_copy_private_header_data (bfd *ibfd, bfd *obfd);
  1200. DESCRIPTION
  1201. Copy private BFD header information from the BFD @var{ibfd} to the
  1202. the BFD @var{obfd}. This copies information that may require
  1203. sections to exist, but does not require symbol tables. Return
  1204. <<true>> on success, <<false>> on error.
  1205. Possible error returns are:
  1206. o <<bfd_error_no_memory>> -
  1207. Not enough memory exists to create private data for @var{obfd}.
  1208. .#define bfd_copy_private_header_data(ibfd, obfd) \
  1209. . BFD_SEND (obfd, _bfd_copy_private_header_data, \
  1210. . (ibfd, obfd))
  1211. */
  1212. /*
  1213. FUNCTION
  1214. bfd_copy_private_bfd_data
  1215. SYNOPSIS
  1216. bfd_boolean bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd);
  1217. DESCRIPTION
  1218. Copy private BFD information from the BFD @var{ibfd} to the
  1219. the BFD @var{obfd}. Return <<TRUE>> on success, <<FALSE>> on error.
  1220. Possible error returns are:
  1221. o <<bfd_error_no_memory>> -
  1222. Not enough memory exists to create private data for @var{obfd}.
  1223. .#define bfd_copy_private_bfd_data(ibfd, obfd) \
  1224. . BFD_SEND (obfd, _bfd_copy_private_bfd_data, \
  1225. . (ibfd, obfd))
  1226. */
  1227. /*
  1228. FUNCTION
  1229. bfd_merge_private_bfd_data
  1230. SYNOPSIS
  1231. bfd_boolean bfd_merge_private_bfd_data (bfd *ibfd, bfd *obfd);
  1232. DESCRIPTION
  1233. Merge private BFD information from the BFD @var{ibfd} to the
  1234. the output file BFD @var{obfd} when linking. Return <<TRUE>>
  1235. on success, <<FALSE>> on error. Possible error returns are:
  1236. o <<bfd_error_no_memory>> -
  1237. Not enough memory exists to create private data for @var{obfd}.
  1238. .#define bfd_merge_private_bfd_data(ibfd, obfd) \
  1239. . BFD_SEND (obfd, _bfd_merge_private_bfd_data, \
  1240. . (ibfd, obfd))
  1241. */
  1242. /*
  1243. FUNCTION
  1244. bfd_set_private_flags
  1245. SYNOPSIS
  1246. bfd_boolean bfd_set_private_flags (bfd *abfd, flagword flags);
  1247. DESCRIPTION
  1248. Set private BFD flag information in the BFD @var{abfd}.
  1249. Return <<TRUE>> on success, <<FALSE>> on error. Possible error
  1250. returns are:
  1251. o <<bfd_error_no_memory>> -
  1252. Not enough memory exists to create private data for @var{obfd}.
  1253. .#define bfd_set_private_flags(abfd, flags) \
  1254. . BFD_SEND (abfd, _bfd_set_private_flags, (abfd, flags))
  1255. */
  1256. /*
  1257. FUNCTION
  1258. Other functions
  1259. DESCRIPTION
  1260. The following functions exist but have not yet been documented.
  1261. .#define bfd_sizeof_headers(abfd, info) \
  1262. . BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, info))
  1263. .
  1264. .#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
  1265. . BFD_SEND (abfd, _bfd_find_nearest_line, \
  1266. . (abfd, syms, sec, off, file, func, line, NULL))
  1267. .
  1268. .#define bfd_find_nearest_line_discriminator(abfd, sec, syms, off, file, func, \
  1269. . line, disc) \
  1270. . BFD_SEND (abfd, _bfd_find_nearest_line, \
  1271. . (abfd, syms, sec, off, file, func, line, disc))
  1272. .
  1273. .#define bfd_find_line(abfd, syms, sym, file, line) \
  1274. . BFD_SEND (abfd, _bfd_find_line, \
  1275. . (abfd, syms, sym, file, line))
  1276. .
  1277. .#define bfd_find_inliner_info(abfd, file, func, line) \
  1278. . BFD_SEND (abfd, _bfd_find_inliner_info, \
  1279. . (abfd, file, func, line))
  1280. .
  1281. .#define bfd_debug_info_start(abfd) \
  1282. . BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
  1283. .
  1284. .#define bfd_debug_info_end(abfd) \
  1285. . BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
  1286. .
  1287. .#define bfd_debug_info_accumulate(abfd, section) \
  1288. . BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
  1289. .
  1290. .#define bfd_stat_arch_elt(abfd, stat) \
  1291. . BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat))
  1292. .
  1293. .#define bfd_update_armap_timestamp(abfd) \
  1294. . BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
  1295. .
  1296. .#define bfd_set_arch_mach(abfd, arch, mach)\
  1297. . BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
  1298. .
  1299. .#define bfd_relax_section(abfd, section, link_info, again) \
  1300. . BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
  1301. .
  1302. .#define bfd_gc_sections(abfd, link_info) \
  1303. . BFD_SEND (abfd, _bfd_gc_sections, (abfd, link_info))
  1304. .
  1305. .#define bfd_lookup_section_flags(link_info, flag_info, section) \
  1306. . BFD_SEND (abfd, _bfd_lookup_section_flags, (link_info, flag_info, section))
  1307. .
  1308. .#define bfd_merge_sections(abfd, link_info) \
  1309. . BFD_SEND (abfd, _bfd_merge_sections, (abfd, link_info))
  1310. .
  1311. .#define bfd_is_group_section(abfd, sec) \
  1312. . BFD_SEND (abfd, _bfd_is_group_section, (abfd, sec))
  1313. .
  1314. .#define bfd_discard_group(abfd, sec) \
  1315. . BFD_SEND (abfd, _bfd_discard_group, (abfd, sec))
  1316. .
  1317. .#define bfd_link_hash_table_create(abfd) \
  1318. . BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
  1319. .
  1320. .#define bfd_link_add_symbols(abfd, info) \
  1321. . BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
  1322. .
  1323. .#define bfd_link_just_syms(abfd, sec, info) \
  1324. . BFD_SEND (abfd, _bfd_link_just_syms, (sec, info))
  1325. .
  1326. .#define bfd_final_link(abfd, info) \
  1327. . BFD_SEND (abfd, _bfd_final_link, (abfd, info))
  1328. .
  1329. .#define bfd_free_cached_info(abfd) \
  1330. . BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
  1331. .
  1332. .#define bfd_get_dynamic_symtab_upper_bound(abfd) \
  1333. . BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
  1334. .
  1335. .#define bfd_print_private_bfd_data(abfd, file)\
  1336. . BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
  1337. .
  1338. .#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
  1339. . BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
  1340. .
  1341. .#define bfd_get_synthetic_symtab(abfd, count, syms, dyncount, dynsyms, ret) \
  1342. . BFD_SEND (abfd, _bfd_get_synthetic_symtab, (abfd, count, syms, \
  1343. . dyncount, dynsyms, ret))
  1344. .
  1345. .#define bfd_get_dynamic_reloc_upper_bound(abfd) \
  1346. . BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
  1347. .
  1348. .#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
  1349. . BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
  1350. .
  1351. .extern bfd_byte *bfd_get_relocated_section_contents
  1352. . (bfd *, struct bfd_link_info *, struct bfd_link_order *, bfd_byte *,
  1353. . bfd_boolean, asymbol **);
  1354. .
  1355. */
  1356. bfd_byte *
  1357. bfd_get_relocated_section_contents (bfd *abfd,
  1358. struct bfd_link_info *link_info,
  1359. struct bfd_link_order *link_order,
  1360. bfd_byte *data,
  1361. bfd_boolean relocatable,
  1362. asymbol **symbols)
  1363. {
  1364. bfd *abfd2;
  1365. bfd_byte *(*fn) (bfd *, struct bfd_link_info *, struct bfd_link_order *,
  1366. bfd_byte *, bfd_boolean, asymbol **);
  1367. if (link_order->type == bfd_indirect_link_order)
  1368. {
  1369. abfd2 = link_order->u.indirect.section->owner;
  1370. if (abfd2 == NULL)
  1371. abfd2 = abfd;
  1372. }
  1373. else
  1374. abfd2 = abfd;
  1375. fn = abfd2->xvec->_bfd_get_relocated_section_contents;
  1376. return (*fn) (abfd, link_info, link_order, data, relocatable, symbols);
  1377. }
  1378. /* Record information about an ELF program header. */
  1379. bfd_boolean
  1380. bfd_record_phdr (bfd *abfd,
  1381. unsigned long type,
  1382. bfd_boolean flags_valid,
  1383. flagword flags,
  1384. bfd_boolean at_valid,
  1385. bfd_vma at,
  1386. bfd_boolean includes_filehdr,
  1387. bfd_boolean includes_phdrs,
  1388. unsigned int count,
  1389. asection **secs)
  1390. {
  1391. struct elf_segment_map *m, **pm;
  1392. bfd_size_type amt;
  1393. if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  1394. return TRUE;
  1395. amt = sizeof (struct elf_segment_map);
  1396. amt += ((bfd_size_type) count - 1) * sizeof (asection *);
  1397. m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
  1398. if (m == NULL)
  1399. return FALSE;
  1400. m->p_type = type;
  1401. m->p_flags = flags;
  1402. m->p_paddr = at;
  1403. m->p_flags_valid = flags_valid;
  1404. m->p_paddr_valid = at_valid;
  1405. m->includes_filehdr = includes_filehdr;
  1406. m->includes_phdrs = includes_phdrs;
  1407. m->count = count;
  1408. if (count > 0)
  1409. memcpy (m->sections, secs, count * sizeof (asection *));
  1410. for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
  1411. ;
  1412. *pm = m;
  1413. return TRUE;
  1414. }
  1415. #ifdef BFD64
  1416. /* Return true iff this target is 32-bit. */
  1417. static bfd_boolean
  1418. is32bit (bfd *abfd)
  1419. {
  1420. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  1421. {
  1422. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  1423. return bed->s->elfclass == ELFCLASS32;
  1424. }
  1425. /* For non-ELF targets, use architecture information. */
  1426. return bfd_arch_bits_per_address (abfd) <= 32;
  1427. }
  1428. #endif
  1429. /* bfd_sprintf_vma and bfd_fprintf_vma display an address in the
  1430. target's address size. */
  1431. void
  1432. bfd_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
  1433. {
  1434. #ifdef BFD64
  1435. if (is32bit (abfd))
  1436. {
  1437. sprintf (buf, "%08lx", (unsigned long) value & 0xffffffff);
  1438. return;
  1439. }
  1440. #endif
  1441. sprintf_vma (buf, value);
  1442. }
  1443. void
  1444. bfd_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
  1445. {
  1446. #ifdef BFD64
  1447. if (is32bit (abfd))
  1448. {
  1449. fprintf ((FILE *) stream, "%08lx", (unsigned long) value & 0xffffffff);
  1450. return;
  1451. }
  1452. #endif
  1453. fprintf_vma ((FILE *) stream, value);
  1454. }
  1455. /*
  1456. FUNCTION
  1457. bfd_alt_mach_code
  1458. SYNOPSIS
  1459. bfd_boolean bfd_alt_mach_code (bfd *abfd, int alternative);
  1460. DESCRIPTION
  1461. When more than one machine code number is available for the
  1462. same machine type, this function can be used to switch between
  1463. the preferred one (alternative == 0) and any others. Currently,
  1464. only ELF supports this feature, with up to two alternate
  1465. machine codes.
  1466. */
  1467. bfd_boolean
  1468. bfd_alt_mach_code (bfd *abfd, int alternative)
  1469. {
  1470. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  1471. {
  1472. int code;
  1473. switch (alternative)
  1474. {
  1475. case 0:
  1476. code = get_elf_backend_data (abfd)->elf_machine_code;
  1477. break;
  1478. case 1:
  1479. code = get_elf_backend_data (abfd)->elf_machine_alt1;
  1480. if (code == 0)
  1481. return FALSE;
  1482. break;
  1483. case 2:
  1484. code = get_elf_backend_data (abfd)->elf_machine_alt2;
  1485. if (code == 0)
  1486. return FALSE;
  1487. break;
  1488. default:
  1489. return FALSE;
  1490. }
  1491. elf_elfheader (abfd)->e_machine = code;
  1492. return TRUE;
  1493. }
  1494. return FALSE;
  1495. }
  1496. /*
  1497. FUNCTION
  1498. bfd_emul_get_maxpagesize
  1499. SYNOPSIS
  1500. bfd_vma bfd_emul_get_maxpagesize (const char *);
  1501. DESCRIPTION
  1502. Returns the maximum page size, in bytes, as determined by
  1503. emulation.
  1504. RETURNS
  1505. Returns the maximum page size in bytes for ELF, 0 otherwise.
  1506. */
  1507. bfd_vma
  1508. bfd_emul_get_maxpagesize (const char *emul)
  1509. {
  1510. const bfd_target *target;
  1511. target = bfd_find_target (emul, NULL);
  1512. if (target != NULL
  1513. && target->flavour == bfd_target_elf_flavour)
  1514. return xvec_get_elf_backend_data (target)->maxpagesize;
  1515. return 0;
  1516. }
  1517. static void
  1518. bfd_elf_set_pagesize (const bfd_target *target, bfd_vma size,
  1519. int offset, const bfd_target *orig_target)
  1520. {
  1521. if (target->flavour == bfd_target_elf_flavour)
  1522. {
  1523. const struct elf_backend_data *bed;
  1524. bed = xvec_get_elf_backend_data (target);
  1525. *((bfd_vma *) ((char *) bed + offset)) = size;
  1526. }
  1527. if (target->alternative_target
  1528. && target->alternative_target != orig_target)
  1529. bfd_elf_set_pagesize (target->alternative_target, size, offset,
  1530. orig_target);
  1531. }
  1532. /*
  1533. FUNCTION
  1534. bfd_emul_set_maxpagesize
  1535. SYNOPSIS
  1536. void bfd_emul_set_maxpagesize (const char *, bfd_vma);
  1537. DESCRIPTION
  1538. For ELF, set the maximum page size for the emulation. It is
  1539. a no-op for other formats.
  1540. */
  1541. void
  1542. bfd_emul_set_maxpagesize (const char *emul, bfd_vma size)
  1543. {
  1544. const bfd_target *target;
  1545. target = bfd_find_target (emul, NULL);
  1546. if (target)
  1547. bfd_elf_set_pagesize (target, size,
  1548. offsetof (struct elf_backend_data,
  1549. maxpagesize), target);
  1550. }
  1551. /*
  1552. FUNCTION
  1553. bfd_emul_get_commonpagesize
  1554. SYNOPSIS
  1555. bfd_vma bfd_emul_get_commonpagesize (const char *);
  1556. DESCRIPTION
  1557. Returns the common page size, in bytes, as determined by
  1558. emulation.
  1559. RETURNS
  1560. Returns the common page size in bytes for ELF, 0 otherwise.
  1561. */
  1562. bfd_vma
  1563. bfd_emul_get_commonpagesize (const char *emul)
  1564. {
  1565. const bfd_target *target;
  1566. target = bfd_find_target (emul, NULL);
  1567. if (target != NULL
  1568. && target->flavour == bfd_target_elf_flavour)
  1569. return xvec_get_elf_backend_data (target)->commonpagesize;
  1570. return 0;
  1571. }
  1572. /*
  1573. FUNCTION
  1574. bfd_emul_set_commonpagesize
  1575. SYNOPSIS
  1576. void bfd_emul_set_commonpagesize (const char *, bfd_vma);
  1577. DESCRIPTION
  1578. For ELF, set the common page size for the emulation. It is
  1579. a no-op for other formats.
  1580. */
  1581. void
  1582. bfd_emul_set_commonpagesize (const char *emul, bfd_vma size)
  1583. {
  1584. const bfd_target *target;
  1585. target = bfd_find_target (emul, NULL);
  1586. if (target)
  1587. bfd_elf_set_pagesize (target, size,
  1588. offsetof (struct elf_backend_data,
  1589. commonpagesize), target);
  1590. }
  1591. /*
  1592. FUNCTION
  1593. bfd_demangle
  1594. SYNOPSIS
  1595. char *bfd_demangle (bfd *, const char *, int);
  1596. DESCRIPTION
  1597. Wrapper around cplus_demangle. Strips leading underscores and
  1598. other such chars that would otherwise confuse the demangler.
  1599. If passed a g++ v3 ABI mangled name, returns a buffer allocated
  1600. with malloc holding the demangled name. Returns NULL otherwise
  1601. and on memory alloc failure.
  1602. */
  1603. char *
  1604. bfd_demangle (bfd *abfd, const char *name, int options)
  1605. {
  1606. char *res, *alloc;
  1607. const char *pre, *suf;
  1608. size_t pre_len;
  1609. bfd_boolean skip_lead;
  1610. skip_lead = (abfd != NULL
  1611. && *name != '\0'
  1612. && bfd_get_symbol_leading_char (abfd) == *name);
  1613. if (skip_lead)
  1614. ++name;
  1615. /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
  1616. or the MS PE format. These formats have a number of leading '.'s
  1617. on at least some symbols, so we remove all dots to avoid
  1618. confusing the demangler. */
  1619. pre = name;
  1620. while (*name == '.' || *name == '$')
  1621. ++name;
  1622. pre_len = name - pre;
  1623. /* Strip off @plt and suchlike too. */
  1624. alloc = NULL;
  1625. suf = strchr (name, '@');
  1626. if (suf != NULL)
  1627. {
  1628. alloc = (char *) bfd_malloc (suf - name + 1);
  1629. if (alloc == NULL)
  1630. return NULL;
  1631. memcpy (alloc, name, suf - name);
  1632. alloc[suf - name] = '\0';
  1633. name = alloc;
  1634. }
  1635. res = cplus_demangle (name, options);
  1636. if (alloc != NULL)
  1637. free (alloc);
  1638. if (res == NULL)
  1639. {
  1640. if (skip_lead)
  1641. {
  1642. size_t len = strlen (pre) + 1;
  1643. alloc = (char *) bfd_malloc (len);
  1644. if (alloc == NULL)
  1645. return NULL;
  1646. memcpy (alloc, pre, len);
  1647. return alloc;
  1648. }
  1649. return NULL;
  1650. }
  1651. /* Put back any prefix or suffix. */
  1652. if (pre_len != 0 || suf != NULL)
  1653. {
  1654. size_t len;
  1655. size_t suf_len;
  1656. char *final;
  1657. len = strlen (res);
  1658. if (suf == NULL)
  1659. suf = res + len;
  1660. suf_len = strlen (suf) + 1;
  1661. final = (char *) bfd_malloc (pre_len + len + suf_len);
  1662. if (final != NULL)
  1663. {
  1664. memcpy (final, pre, pre_len);
  1665. memcpy (final + pre_len, res, len);
  1666. memcpy (final + pre_len + len, suf, suf_len);
  1667. }
  1668. free (res);
  1669. res = final;
  1670. }
  1671. return res;
  1672. }
  1673. /*
  1674. FUNCTION
  1675. bfd_update_compression_header
  1676. SYNOPSIS
  1677. void bfd_update_compression_header
  1678. (bfd *abfd, bfd_byte *contents, asection *sec);
  1679. DESCRIPTION
  1680. Set the compression header at CONTENTS of SEC in ABFD and update
  1681. elf_section_flags for compression.
  1682. */
  1683. void
  1684. bfd_update_compression_header (bfd *abfd, bfd_byte *contents,
  1685. asection *sec)
  1686. {
  1687. if ((abfd->flags & BFD_COMPRESS) != 0)
  1688. {
  1689. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  1690. {
  1691. if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
  1692. {
  1693. const struct elf_backend_data *bed
  1694. = get_elf_backend_data (abfd);
  1695. /* Set the SHF_COMPRESSED bit. */
  1696. elf_section_flags (sec) |= SHF_COMPRESSED;
  1697. if (bed->s->elfclass == ELFCLASS32)
  1698. {
  1699. Elf32_External_Chdr *echdr
  1700. = (Elf32_External_Chdr *) contents;
  1701. bfd_put_32 (abfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
  1702. bfd_put_32 (abfd, sec->size, &echdr->ch_size);
  1703. bfd_put_32 (abfd, 1 << sec->alignment_power,
  1704. &echdr->ch_addralign);
  1705. }
  1706. else
  1707. {
  1708. Elf64_External_Chdr *echdr
  1709. = (Elf64_External_Chdr *) contents;
  1710. bfd_put_32 (abfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
  1711. bfd_put_32 (abfd, 0, &echdr->ch_reserved);
  1712. bfd_put_64 (abfd, sec->size, &echdr->ch_size);
  1713. bfd_put_64 (abfd, 1 << sec->alignment_power,
  1714. &echdr->ch_addralign);
  1715. }
  1716. }
  1717. else
  1718. {
  1719. /* Clear the SHF_COMPRESSED bit. */
  1720. elf_section_flags (sec) &= ~SHF_COMPRESSED;
  1721. /* Write the zlib header. It should be "ZLIB" followed by
  1722. the uncompressed section size, 8 bytes in big-endian
  1723. order. */
  1724. memcpy (contents, "ZLIB", 4);
  1725. bfd_putb64 (sec->size, contents + 4);
  1726. }
  1727. }
  1728. }
  1729. else
  1730. abort ();
  1731. }
  1732. /*
  1733. FUNCTION
  1734. bfd_check_compression_header
  1735. SYNOPSIS
  1736. bfd_boolean bfd_check_compression_header
  1737. (bfd *abfd, bfd_byte *contents, asection *sec,
  1738. bfd_size_type *uncompressed_size);
  1739. DESCRIPTION
  1740. Check the compression header at CONTENTS of SEC in ABFD and
  1741. store the uncompressed size in UNCOMPRESSED_SIZE if the
  1742. compression header is valid.
  1743. RETURNS
  1744. Return TRUE if the compression header is valid.
  1745. */
  1746. bfd_boolean
  1747. bfd_check_compression_header (bfd *abfd, bfd_byte *contents,
  1748. asection *sec,
  1749. bfd_size_type *uncompressed_size)
  1750. {
  1751. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  1752. && (elf_section_flags (sec) & SHF_COMPRESSED) != 0)
  1753. {
  1754. Elf_Internal_Chdr chdr;
  1755. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  1756. if (bed->s->elfclass == ELFCLASS32)
  1757. {
  1758. Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
  1759. chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
  1760. chdr.ch_size = bfd_get_32 (abfd, &echdr->ch_size);
  1761. chdr.ch_addralign = bfd_get_32 (abfd, &echdr->ch_addralign);
  1762. }
  1763. else
  1764. {
  1765. Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
  1766. chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
  1767. chdr.ch_size = bfd_get_64 (abfd, &echdr->ch_size);
  1768. chdr.ch_addralign = bfd_get_64 (abfd, &echdr->ch_addralign);
  1769. }
  1770. if (chdr.ch_type == ELFCOMPRESS_ZLIB
  1771. && chdr.ch_addralign == 1U << sec->alignment_power)
  1772. {
  1773. *uncompressed_size = chdr.ch_size;
  1774. return TRUE;
  1775. }
  1776. }
  1777. return FALSE;
  1778. }
  1779. /*
  1780. FUNCTION
  1781. bfd_get_compression_header_size
  1782. SYNOPSIS
  1783. int bfd_get_compression_header_size (bfd *abfd, asection *sec);
  1784. DESCRIPTION
  1785. Return the size of the compression header of SEC in ABFD.
  1786. RETURNS
  1787. Return the size of the compression header in bytes.
  1788. */
  1789. int
  1790. bfd_get_compression_header_size (bfd *abfd, asection *sec)
  1791. {
  1792. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  1793. {
  1794. if (sec == NULL)
  1795. {
  1796. if (!(abfd->flags & BFD_COMPRESS_GABI))
  1797. return 0;
  1798. }
  1799. else if (!(elf_section_flags (sec) & SHF_COMPRESSED))
  1800. return 0;
  1801. if (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS32)
  1802. return sizeof (Elf32_External_Chdr);
  1803. else
  1804. return sizeof (Elf64_External_Chdr);
  1805. }
  1806. return 0;
  1807. }
  1808. /*
  1809. FUNCTION
  1810. bfd_convert_section_size
  1811. SYNOPSIS
  1812. bfd_size_type bfd_convert_section_size
  1813. (bfd *ibfd, asection *isec, bfd *obfd, bfd_size_type size);
  1814. DESCRIPTION
  1815. Convert the size @var{size} of the section @var{isec} in input
  1816. BFD @var{ibfd} to the section size in output BFD @var{obfd}.
  1817. */
  1818. bfd_size_type
  1819. bfd_convert_section_size (bfd *ibfd, sec_ptr isec, bfd *obfd,
  1820. bfd_size_type size)
  1821. {
  1822. bfd_size_type hdr_size;
  1823. /* Do nothing if input file will be decompressed. */
  1824. if ((ibfd->flags & BFD_DECOMPRESS))
  1825. return size;
  1826. /* Do nothing if either input or output aren't ELF. */
  1827. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  1828. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  1829. return size;
  1830. /* Do nothing if ELF classes of input and output are the same. */
  1831. if (get_elf_backend_data (ibfd)->s->elfclass
  1832. == get_elf_backend_data (obfd)->s->elfclass)
  1833. return size;
  1834. /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
  1835. hdr_size = bfd_get_compression_header_size (ibfd, isec);
  1836. if (hdr_size == 0)
  1837. return size;
  1838. /* Adjust the size of the output SHF_COMPRESSED section. */
  1839. if (hdr_size == sizeof (Elf32_External_Chdr))
  1840. return (size - sizeof (Elf32_External_Chdr)
  1841. + sizeof (Elf64_External_Chdr));
  1842. else
  1843. return (size - sizeof (Elf64_External_Chdr)
  1844. + sizeof (Elf32_External_Chdr));
  1845. }
  1846. /*
  1847. FUNCTION
  1848. bfd_convert_section_contents
  1849. SYNOPSIS
  1850. bfd_boolean bfd_convert_section_contents
  1851. (bfd *ibfd, asection *isec, bfd *obfd,
  1852. bfd_byte **ptr, bfd_size_type *ptr_size);
  1853. DESCRIPTION
  1854. Convert the contents, stored in @var{*ptr}, of the section
  1855. @var{isec} in input BFD @var{ibfd} to output BFD @var{obfd}
  1856. if needed. The original buffer pointed to by @var{*ptr} may
  1857. be freed and @var{*ptr} is returned with memory malloc'd by this
  1858. function, and the new size written to @var{ptr_size}.
  1859. */
  1860. bfd_boolean
  1861. bfd_convert_section_contents (bfd *ibfd, sec_ptr isec, bfd *obfd,
  1862. bfd_byte **ptr, bfd_size_type *ptr_size)
  1863. {
  1864. bfd_byte *contents;
  1865. bfd_size_type ihdr_size, ohdr_size, size;
  1866. Elf_Internal_Chdr chdr;
  1867. bfd_boolean use_memmove;
  1868. /* Do nothing if input file will be decompressed. */
  1869. if ((ibfd->flags & BFD_DECOMPRESS))
  1870. return TRUE;
  1871. /* Do nothing if either input or output aren't ELF. */
  1872. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  1873. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  1874. return TRUE;
  1875. /* Do nothing if ELF classes of input and output are the same. */
  1876. if (get_elf_backend_data (ibfd)->s->elfclass
  1877. == get_elf_backend_data (obfd)->s->elfclass)
  1878. return TRUE;
  1879. /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
  1880. ihdr_size = bfd_get_compression_header_size (ibfd, isec);
  1881. if (ihdr_size == 0)
  1882. return TRUE;
  1883. contents = *ptr;
  1884. /* Convert the contents of the input SHF_COMPRESSED section to
  1885. output. Get the input compression header and the size of the
  1886. output compression header. */
  1887. if (ihdr_size == sizeof (Elf32_External_Chdr))
  1888. {
  1889. Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
  1890. chdr.ch_type = bfd_get_32 (ibfd, &echdr->ch_type);
  1891. chdr.ch_size = bfd_get_32 (ibfd, &echdr->ch_size);
  1892. chdr.ch_addralign = bfd_get_32 (ibfd, &echdr->ch_addralign);
  1893. ohdr_size = sizeof (Elf64_External_Chdr);
  1894. use_memmove = FALSE;
  1895. }
  1896. else
  1897. {
  1898. Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
  1899. chdr.ch_type = bfd_get_32 (ibfd, &echdr->ch_type);
  1900. chdr.ch_size = bfd_get_64 (ibfd, &echdr->ch_size);
  1901. chdr.ch_addralign = bfd_get_64 (ibfd, &echdr->ch_addralign);
  1902. ohdr_size = sizeof (Elf32_External_Chdr);
  1903. use_memmove = TRUE;
  1904. }
  1905. size = bfd_get_section_size (isec) - ihdr_size + ohdr_size;
  1906. if (!use_memmove)
  1907. {
  1908. contents = (bfd_byte *) bfd_malloc (size);
  1909. if (contents == NULL)
  1910. return FALSE;
  1911. }
  1912. /* Write out the output compression header. */
  1913. if (ohdr_size == sizeof (Elf32_External_Chdr))
  1914. {
  1915. Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
  1916. bfd_put_32 (obfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
  1917. bfd_put_32 (obfd, chdr.ch_size, &echdr->ch_size);
  1918. bfd_put_32 (obfd, chdr.ch_addralign, &echdr->ch_addralign);
  1919. }
  1920. else
  1921. {
  1922. Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
  1923. bfd_put_32 (obfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
  1924. bfd_put_32 (obfd, 0, &echdr->ch_reserved);
  1925. bfd_put_64 (obfd, chdr.ch_size, &echdr->ch_size);
  1926. bfd_put_64 (obfd, chdr.ch_addralign, &echdr->ch_addralign);
  1927. }
  1928. /* Copy the compressed contents. */
  1929. if (use_memmove)
  1930. memmove (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
  1931. else
  1932. {
  1933. memcpy (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
  1934. free (*ptr);
  1935. *ptr = contents;
  1936. }
  1937. *ptr_size = size;
  1938. return TRUE;
  1939. }