bfd.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822
  1. /* Generic BFD library interface and support routines.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. Written by Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /*
  18. 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_unknown = 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. . {* Track current file position (or current buffer offset for
  69. . in-memory BFDs). When a file is closed by the caching routines,
  70. . BFD retains state information on the file here. *}
  71. . ufile_ptr where;
  72. .
  73. . {* File modified time, if mtime_set is TRUE. *}
  74. . long mtime;
  75. .
  76. . {* A unique identifier of the BFD *}
  77. . unsigned int id;
  78. .
  79. . {* Format_specific flags. *}
  80. . flagword flags;
  81. .
  82. . {* Values that may appear in the flags field of a BFD. These also
  83. . appear in the object_flags field of the bfd_target structure, where
  84. . they indicate the set of flags used by that backend (not all flags
  85. . are meaningful for all object file formats) (FIXME: at the moment,
  86. . the object_flags values have mostly just been copied from backend
  87. . to another, and are not necessarily correct). *}
  88. .
  89. .#define BFD_NO_FLAGS 0x0
  90. .
  91. . {* BFD contains relocation entries. *}
  92. .#define HAS_RELOC 0x1
  93. .
  94. . {* BFD is directly executable. *}
  95. .#define EXEC_P 0x2
  96. .
  97. . {* BFD has line number information (basically used for F_LNNO in a
  98. . COFF header). *}
  99. .#define HAS_LINENO 0x4
  100. .
  101. . {* BFD has debugging information. *}
  102. .#define HAS_DEBUG 0x08
  103. .
  104. . {* BFD has symbols. *}
  105. .#define HAS_SYMS 0x10
  106. .
  107. . {* BFD has local symbols (basically used for F_LSYMS in a COFF
  108. . header). *}
  109. .#define HAS_LOCALS 0x20
  110. .
  111. . {* BFD is a dynamic object. *}
  112. .#define DYNAMIC 0x40
  113. .
  114. . {* Text section is write protected (if D_PAGED is not set, this is
  115. . like an a.out NMAGIC file) (the linker sets this by default, but
  116. . clears it for -r or -N). *}
  117. .#define WP_TEXT 0x80
  118. .
  119. . {* BFD is dynamically paged (this is like an a.out ZMAGIC file) (the
  120. . linker sets this by default, but clears it for -r or -n or -N). *}
  121. .#define D_PAGED 0x100
  122. .
  123. . {* BFD is relaxable (this means that bfd_relax_section may be able to
  124. . do something) (sometimes bfd_relax_section can do something even if
  125. . this is not set). *}
  126. .#define BFD_IS_RELAXABLE 0x200
  127. .
  128. . {* This may be set before writing out a BFD to request using a
  129. . traditional format. For example, this is used to request that when
  130. . writing out an a.out object the symbols not be hashed to eliminate
  131. . duplicates. *}
  132. .#define BFD_TRADITIONAL_FORMAT 0x400
  133. .
  134. . {* This flag indicates that the BFD contents are actually cached
  135. . in memory. If this is set, iostream points to a bfd_in_memory
  136. . struct. *}
  137. .#define BFD_IN_MEMORY 0x800
  138. .
  139. . {* This BFD has been created by the linker and doesn't correspond
  140. . to any input file. *}
  141. .#define BFD_LINKER_CREATED 0x1000
  142. .
  143. . {* This may be set before writing out a BFD to request that it
  144. . be written using values for UIDs, GIDs, timestamps, etc. that
  145. . will be consistent from run to run. *}
  146. .#define BFD_DETERMINISTIC_OUTPUT 0x2000
  147. .
  148. . {* Compress sections in this BFD. *}
  149. .#define BFD_COMPRESS 0x4000
  150. .
  151. . {* Decompress sections in this BFD. *}
  152. .#define BFD_DECOMPRESS 0x8000
  153. .
  154. . {* BFD is a dummy, for plugins. *}
  155. .#define BFD_PLUGIN 0x10000
  156. .
  157. . {* Compress sections in this BFD with SHF_COMPRESSED from gABI. *}
  158. .#define BFD_COMPRESS_GABI 0x20000
  159. .
  160. . {* Convert ELF common symbol type to STT_COMMON or STT_OBJECT in this
  161. . BFD. *}
  162. .#define BFD_CONVERT_ELF_COMMON 0x40000
  163. .
  164. . {* Use the ELF STT_COMMON type in this BFD. *}
  165. .#define BFD_USE_ELF_STT_COMMON 0x80000
  166. .
  167. . {* Put pathnames into archives (non-POSIX). *}
  168. .#define BFD_ARCHIVE_FULL_PATH 0x100000
  169. .
  170. . {* Flags bits to be saved in bfd_preserve_save. *}
  171. .#define BFD_FLAGS_SAVED \
  172. . (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \
  173. . | BFD_PLUGIN | BFD_COMPRESS_GABI | BFD_CONVERT_ELF_COMMON \
  174. . | BFD_USE_ELF_STT_COMMON)
  175. .
  176. . {* Flags bits which are for BFD use only. *}
  177. .#define BFD_FLAGS_FOR_BFD_USE_MASK \
  178. . (BFD_IN_MEMORY | BFD_COMPRESS | BFD_DECOMPRESS | BFD_LINKER_CREATED \
  179. . | BFD_PLUGIN | BFD_TRADITIONAL_FORMAT | BFD_DETERMINISTIC_OUTPUT \
  180. . | BFD_COMPRESS_GABI | BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON)
  181. .
  182. . {* The format which belongs to the BFD. (object, core, etc.) *}
  183. . ENUM_BITFIELD (bfd_format) format : 3;
  184. .
  185. . {* The direction with which the BFD was opened. *}
  186. . ENUM_BITFIELD (bfd_direction) direction : 2;
  187. .
  188. . {* Is the file descriptor being cached? That is, can it be closed as
  189. . needed, and re-opened when accessed later? *}
  190. . unsigned int cacheable : 1;
  191. .
  192. . {* Marks whether there was a default target specified when the
  193. . BFD was opened. This is used to select which matching algorithm
  194. . to use to choose the back end. *}
  195. . unsigned int target_defaulted : 1;
  196. .
  197. . {* ... and here: (``once'' means at least once). *}
  198. . unsigned int opened_once : 1;
  199. .
  200. . {* Set if we have a locally maintained mtime value, rather than
  201. . getting it from the file each time. *}
  202. . unsigned int mtime_set : 1;
  203. .
  204. . {* Flag set if symbols from this BFD should not be exported. *}
  205. . unsigned int no_export : 1;
  206. .
  207. . {* Remember when output has begun, to stop strange things
  208. . from happening. *}
  209. . unsigned int output_has_begun : 1;
  210. .
  211. . {* Have archive map. *}
  212. . unsigned int has_armap : 1;
  213. .
  214. . {* Set if this is a thin archive. *}
  215. . unsigned int is_thin_archive : 1;
  216. .
  217. . {* Set if this archive should not cache element positions. *}
  218. . unsigned int no_element_cache : 1;
  219. .
  220. . {* Set if only required symbols should be added in the link hash table for
  221. . this object. Used by VMS linkers. *}
  222. . unsigned int selective_search : 1;
  223. .
  224. . {* Set if this is the linker output BFD. *}
  225. . unsigned int is_linker_output : 1;
  226. .
  227. . {* Set if this is the linker input BFD. *}
  228. . unsigned int is_linker_input : 1;
  229. .
  230. . {* If this is an input for a compiler plug-in library. *}
  231. . ENUM_BITFIELD (bfd_plugin_format) plugin_format : 2;
  232. .
  233. . {* Set if this is a plugin output file. *}
  234. . unsigned int lto_output : 1;
  235. .
  236. . {* Set if this is a slim LTO object not loaded with a compiler plugin. *}
  237. . unsigned int lto_slim_object : 1;
  238. .
  239. . {* Do not attempt to modify this file. Set when detecting errors
  240. . that BFD is not prepared to handle for objcopy/strip. *}
  241. . unsigned int read_only : 1;
  242. .
  243. . {* Set to dummy BFD created when claimed by a compiler plug-in
  244. . library. *}
  245. . bfd *plugin_dummy_bfd;
  246. .
  247. . {* The offset of this bfd in the file, typically 0 if it is not
  248. . contained in an archive. *}
  249. . ufile_ptr origin;
  250. .
  251. . {* The origin in the archive of the proxy entry. This will
  252. . normally be the same as origin, except for thin archives,
  253. . when it will contain the current offset of the proxy in the
  254. . thin archive rather than the offset of the bfd in its actual
  255. . container. *}
  256. . ufile_ptr proxy_origin;
  257. .
  258. . {* A hash table for section names. *}
  259. . struct bfd_hash_table section_htab;
  260. .
  261. . {* Pointer to linked list of sections. *}
  262. . struct bfd_section *sections;
  263. .
  264. . {* The last section on the section list. *}
  265. . struct bfd_section *section_last;
  266. .
  267. . {* The number of sections. *}
  268. . unsigned int section_count;
  269. .
  270. . {* The archive plugin file descriptor. *}
  271. . int archive_plugin_fd;
  272. .
  273. . {* The number of opens on the archive plugin file descriptor. *}
  274. . unsigned int archive_plugin_fd_open_count;
  275. .
  276. . {* A field used by _bfd_generic_link_add_archive_symbols. This will
  277. . be used only for archive elements. *}
  278. . int archive_pass;
  279. .
  280. . {* The total size of memory from bfd_alloc. *}
  281. . bfd_size_type alloc_size;
  282. .
  283. . {* Stuff only useful for object files:
  284. . The start address. *}
  285. . bfd_vma start_address;
  286. .
  287. . {* Symbol table for output BFD (with symcount entries).
  288. . Also used by the linker to cache input BFD symbols. *}
  289. . struct bfd_symbol **outsymbols;
  290. .
  291. . {* Used for input and output. *}
  292. . unsigned int symcount;
  293. .
  294. . {* Used for slurped dynamic symbol tables. *}
  295. . unsigned int dynsymcount;
  296. .
  297. . {* Pointer to structure which contains architecture information. *}
  298. . const struct bfd_arch_info *arch_info;
  299. .
  300. . {* Cached length of file for bfd_get_size. 0 until bfd_get_size is
  301. . called, 1 if stat returns an error or the file size is too large to
  302. . return in ufile_ptr. Both 0 and 1 should be treated as "unknown". *}
  303. . ufile_ptr size;
  304. .
  305. . {* Stuff only useful for archives. *}
  306. . void *arelt_data;
  307. . struct bfd *my_archive; {* The containing archive BFD. *}
  308. . struct bfd *archive_next; {* The next BFD in the archive. *}
  309. . struct bfd *archive_head; {* The first BFD in the archive. *}
  310. . struct bfd *nested_archives; {* List of nested archive in a flattened
  311. . thin archive. *}
  312. .
  313. . union {
  314. . {* For input BFDs, a chain of BFDs involved in a link. *}
  315. . struct bfd *next;
  316. . {* For output BFD, the linker hash table. *}
  317. . struct bfd_link_hash_table *hash;
  318. . } link;
  319. .
  320. . {* Used by the back end to hold private data. *}
  321. . union
  322. . {
  323. . struct aout_data_struct *aout_data;
  324. . struct artdata *aout_ar_data;
  325. . struct coff_tdata *coff_obj_data;
  326. . struct pe_tdata *pe_obj_data;
  327. . struct xcoff_tdata *xcoff_obj_data;
  328. . struct ecoff_tdata *ecoff_obj_data;
  329. . struct srec_data_struct *srec_data;
  330. . struct verilog_data_struct *verilog_data;
  331. . struct ihex_data_struct *ihex_data;
  332. . struct tekhex_data_struct *tekhex_data;
  333. . struct elf_obj_tdata *elf_obj_data;
  334. . struct mmo_data_struct *mmo_data;
  335. . struct sun_core_struct *sun_core_data;
  336. . struct sco5_core_struct *sco5_core_data;
  337. . struct trad_core_struct *trad_core_data;
  338. . struct som_data_struct *som_data;
  339. . struct hpux_core_struct *hpux_core_data;
  340. . struct hppabsd_core_struct *hppabsd_core_data;
  341. . struct sgi_core_struct *sgi_core_data;
  342. . struct lynx_core_struct *lynx_core_data;
  343. . struct osf_core_struct *osf_core_data;
  344. . struct cisco_core_struct *cisco_core_data;
  345. . struct versados_data_struct *versados_data;
  346. . struct netbsd_core_struct *netbsd_core_data;
  347. . struct mach_o_data_struct *mach_o_data;
  348. . struct mach_o_fat_data_struct *mach_o_fat_data;
  349. . struct plugin_data_struct *plugin_data;
  350. . struct bfd_pef_data_struct *pef_data;
  351. . struct bfd_pef_xlib_data_struct *pef_xlib_data;
  352. . struct bfd_sym_data_struct *sym_data;
  353. . void *any;
  354. . }
  355. . tdata;
  356. .
  357. . {* Used by the application to hold private data. *}
  358. . void *usrdata;
  359. .
  360. . {* Where all the allocated stuff under this BFD goes. This is a
  361. . struct objalloc *, but we use void * to avoid requiring the inclusion
  362. . of objalloc.h. *}
  363. . void *memory;
  364. .
  365. . {* For input BFDs, the build ID, if the object has one. *}
  366. . const struct bfd_build_id *build_id;
  367. .};
  368. .
  369. .static inline const char *
  370. .bfd_get_filename (const bfd *abfd)
  371. .{
  372. . return abfd->filename;
  373. .}
  374. .
  375. .static inline bool
  376. .bfd_get_cacheable (const bfd *abfd)
  377. .{
  378. . return abfd->cacheable;
  379. .}
  380. .
  381. .static inline enum bfd_format
  382. .bfd_get_format (const bfd *abfd)
  383. .{
  384. . return abfd->format;
  385. .}
  386. .
  387. .static inline flagword
  388. .bfd_get_file_flags (const bfd *abfd)
  389. .{
  390. . return abfd->flags;
  391. .}
  392. .
  393. .static inline bfd_vma
  394. .bfd_get_start_address (const bfd *abfd)
  395. .{
  396. . return abfd->start_address;
  397. .}
  398. .
  399. .static inline unsigned int
  400. .bfd_get_symcount (const bfd *abfd)
  401. .{
  402. . return abfd->symcount;
  403. .}
  404. .
  405. .static inline unsigned int
  406. .bfd_get_dynamic_symcount (const bfd *abfd)
  407. .{
  408. . return abfd->dynsymcount;
  409. .}
  410. .
  411. .static inline struct bfd_symbol **
  412. .bfd_get_outsymbols (const bfd *abfd)
  413. .{
  414. . return abfd->outsymbols;
  415. .}
  416. .
  417. .static inline unsigned int
  418. .bfd_count_sections (const bfd *abfd)
  419. .{
  420. . return abfd->section_count;
  421. .}
  422. .
  423. .static inline bool
  424. .bfd_has_map (const bfd *abfd)
  425. .{
  426. . return abfd->has_armap;
  427. .}
  428. .
  429. .static inline bool
  430. .bfd_is_thin_archive (const bfd *abfd)
  431. .{
  432. . return abfd->is_thin_archive;
  433. .}
  434. .
  435. .static inline void *
  436. .bfd_usrdata (const bfd *abfd)
  437. .{
  438. . return abfd->usrdata;
  439. .}
  440. .
  441. .{* See note beside bfd_set_section_userdata. *}
  442. .static inline bool
  443. .bfd_set_cacheable (bfd * abfd, bool val)
  444. .{
  445. . abfd->cacheable = val;
  446. . return true;
  447. .}
  448. .
  449. .static inline void
  450. .bfd_set_thin_archive (bfd *abfd, bool val)
  451. .{
  452. . abfd->is_thin_archive = val;
  453. .}
  454. .
  455. .static inline void
  456. .bfd_set_usrdata (bfd *abfd, void *val)
  457. .{
  458. . abfd->usrdata = val;
  459. .}
  460. .
  461. .static inline asection *
  462. .bfd_asymbol_section (const asymbol *sy)
  463. .{
  464. . return sy->section;
  465. .}
  466. .
  467. .static inline bfd_vma
  468. .bfd_asymbol_value (const asymbol *sy)
  469. .{
  470. . return sy->section->vma + sy->value;
  471. .}
  472. .
  473. .static inline const char *
  474. .bfd_asymbol_name (const asymbol *sy)
  475. .{
  476. . return sy->name;
  477. .}
  478. .
  479. .static inline struct bfd *
  480. .bfd_asymbol_bfd (const asymbol *sy)
  481. .{
  482. . return sy->the_bfd;
  483. .}
  484. .
  485. .static inline void
  486. .bfd_set_asymbol_name (asymbol *sy, const char *name)
  487. .{
  488. . sy->name = name;
  489. .}
  490. .
  491. .static inline bfd_size_type
  492. .bfd_get_section_limit_octets (const bfd *abfd, const asection *sec)
  493. .{
  494. . if (abfd->direction != write_direction && sec->rawsize != 0)
  495. . return sec->rawsize;
  496. . return sec->size;
  497. .}
  498. .
  499. .{* Find the address one past the end of SEC. *}
  500. .static inline bfd_size_type
  501. .bfd_get_section_limit (const bfd *abfd, const asection *sec)
  502. .{
  503. . return (bfd_get_section_limit_octets (abfd, sec)
  504. . / bfd_octets_per_byte (abfd, sec));
  505. .}
  506. .
  507. .{* Functions to handle insertion and deletion of a bfd's sections. These
  508. . only handle the list pointers, ie. do not adjust section_count,
  509. . target_index etc. *}
  510. .static inline void
  511. .bfd_section_list_remove (bfd *abfd, asection *s)
  512. .{
  513. . asection *next = s->next;
  514. . asection *prev = s->prev;
  515. . if (prev)
  516. . prev->next = next;
  517. . else
  518. . abfd->sections = next;
  519. . if (next)
  520. . next->prev = prev;
  521. . else
  522. . abfd->section_last = prev;
  523. .}
  524. .
  525. .static inline void
  526. .bfd_section_list_append (bfd *abfd, asection *s)
  527. .{
  528. . s->next = 0;
  529. . if (abfd->section_last)
  530. . {
  531. . s->prev = abfd->section_last;
  532. . abfd->section_last->next = s;
  533. . }
  534. . else
  535. . {
  536. . s->prev = 0;
  537. . abfd->sections = s;
  538. . }
  539. . abfd->section_last = s;
  540. .}
  541. .
  542. .static inline void
  543. .bfd_section_list_prepend (bfd *abfd, asection *s)
  544. .{
  545. . s->prev = 0;
  546. . if (abfd->sections)
  547. . {
  548. . s->next = abfd->sections;
  549. . abfd->sections->prev = s;
  550. . }
  551. . else
  552. . {
  553. . s->next = 0;
  554. . abfd->section_last = s;
  555. . }
  556. . abfd->sections = s;
  557. .}
  558. .
  559. .static inline void
  560. .bfd_section_list_insert_after (bfd *abfd, asection *a, asection *s)
  561. .{
  562. . asection *next = a->next;
  563. . s->next = next;
  564. . s->prev = a;
  565. . a->next = s;
  566. . if (next)
  567. . next->prev = s;
  568. . else
  569. . abfd->section_last = s;
  570. .}
  571. .
  572. .static inline void
  573. .bfd_section_list_insert_before (bfd *abfd, asection *b, asection *s)
  574. .{
  575. . asection *prev = b->prev;
  576. . s->prev = prev;
  577. . s->next = b;
  578. . b->prev = s;
  579. . if (prev)
  580. . prev->next = s;
  581. . else
  582. . abfd->sections = s;
  583. .}
  584. .
  585. .static inline bool
  586. .bfd_section_removed_from_list (const bfd *abfd, const asection *s)
  587. .{
  588. . return s->next ? s->next->prev != s : abfd->section_last != s;
  589. .}
  590. .
  591. */
  592. #include "sysdep.h"
  593. #include <stdarg.h>
  594. #include "bfd.h"
  595. #include "bfdver.h"
  596. #include "libiberty.h"
  597. #include "demangle.h"
  598. #include "safe-ctype.h"
  599. #include "bfdlink.h"
  600. #include "libbfd.h"
  601. #include "coff/internal.h"
  602. #include "coff/sym.h"
  603. #include "libcoff.h"
  604. #include "libecoff.h"
  605. #undef obj_symbols
  606. #include "elf-bfd.h"
  607. #ifndef EXIT_FAILURE
  608. #define EXIT_FAILURE 1
  609. #endif
  610. /* provide storage for subsystem, stack and heap data which may have been
  611. passed in on the command line. Ld puts this data into a bfd_link_info
  612. struct which ultimately gets passed in to the bfd. When it arrives, copy
  613. it to the following struct so that the data will be available in coffcode.h
  614. where it is needed. The typedef's used are defined in bfd.h */
  615. /*
  616. INODE
  617. Error reporting, Miscellaneous, typedef bfd, BFD front end
  618. SECTION
  619. Error reporting
  620. Most BFD functions return nonzero on success (check their
  621. individual documentation for precise semantics). On an error,
  622. they call <<bfd_set_error>> to set an error condition that callers
  623. can check by calling <<bfd_get_error>>.
  624. If that returns <<bfd_error_system_call>>, then check
  625. <<errno>>.
  626. The easiest way to report a BFD error to the user is to
  627. use <<bfd_perror>>.
  628. SUBSECTION
  629. Type <<bfd_error_type>>
  630. The values returned by <<bfd_get_error>> are defined by the
  631. enumerated type <<bfd_error_type>>.
  632. CODE_FRAGMENT
  633. .
  634. .typedef enum bfd_error
  635. .{
  636. . bfd_error_no_error = 0,
  637. . bfd_error_system_call,
  638. . bfd_error_invalid_target,
  639. . bfd_error_wrong_format,
  640. . bfd_error_wrong_object_format,
  641. . bfd_error_invalid_operation,
  642. . bfd_error_no_memory,
  643. . bfd_error_no_symbols,
  644. . bfd_error_no_armap,
  645. . bfd_error_no_more_archived_files,
  646. . bfd_error_malformed_archive,
  647. . bfd_error_missing_dso,
  648. . bfd_error_file_not_recognized,
  649. . bfd_error_file_ambiguously_recognized,
  650. . bfd_error_no_contents,
  651. . bfd_error_nonrepresentable_section,
  652. . bfd_error_no_debug_section,
  653. . bfd_error_bad_value,
  654. . bfd_error_file_truncated,
  655. . bfd_error_file_too_big,
  656. . bfd_error_sorry,
  657. . bfd_error_on_input,
  658. . bfd_error_invalid_error_code
  659. .}
  660. .bfd_error_type;
  661. .
  662. */
  663. static bfd_error_type bfd_error = bfd_error_no_error;
  664. static bfd *input_bfd = NULL;
  665. static bfd_error_type input_error = bfd_error_no_error;
  666. const char *const bfd_errmsgs[] =
  667. {
  668. N_("no error"),
  669. N_("system call error"),
  670. N_("invalid bfd target"),
  671. N_("file in wrong format"),
  672. N_("archive object file in wrong format"),
  673. N_("invalid operation"),
  674. N_("memory exhausted"),
  675. N_("no symbols"),
  676. N_("archive has no index; run ranlib to add one"),
  677. N_("no more archived files"),
  678. N_("malformed archive"),
  679. N_("DSO missing from command line"),
  680. N_("file format not recognized"),
  681. N_("file format is ambiguous"),
  682. N_("section has no contents"),
  683. N_("nonrepresentable section on output"),
  684. N_("symbol needs debug section which does not exist"),
  685. N_("bad value"),
  686. N_("file truncated"),
  687. N_("file too big"),
  688. N_("sorry, cannot handle this file"),
  689. N_("error reading %s: %s"),
  690. N_("#<invalid error code>")
  691. };
  692. /*
  693. FUNCTION
  694. bfd_get_error
  695. SYNOPSIS
  696. bfd_error_type bfd_get_error (void);
  697. DESCRIPTION
  698. Return the current BFD error condition.
  699. */
  700. bfd_error_type
  701. bfd_get_error (void)
  702. {
  703. return bfd_error;
  704. }
  705. /*
  706. FUNCTION
  707. bfd_set_error
  708. SYNOPSIS
  709. void bfd_set_error (bfd_error_type error_tag);
  710. DESCRIPTION
  711. Set the BFD error condition to be @var{error_tag}.
  712. @var{error_tag} must not be bfd_error_on_input. Use
  713. bfd_set_input_error for input errors instead.
  714. */
  715. void
  716. bfd_set_error (bfd_error_type error_tag)
  717. {
  718. bfd_error = error_tag;
  719. if (bfd_error >= bfd_error_on_input)
  720. abort ();
  721. }
  722. /*
  723. FUNCTION
  724. bfd_set_input_error
  725. SYNOPSIS
  726. void bfd_set_input_error (bfd *input, bfd_error_type error_tag);
  727. DESCRIPTION
  728. Set the BFD error condition to be bfd_error_on_input.
  729. @var{input} is the input bfd where the error occurred, and
  730. @var{error_tag} the bfd_error_type error.
  731. */
  732. void
  733. bfd_set_input_error (bfd *input, bfd_error_type error_tag)
  734. {
  735. /* This is an error that occurred during bfd_close when writing an
  736. archive, but on one of the input files. */
  737. bfd_error = bfd_error_on_input;
  738. input_bfd = input;
  739. input_error = error_tag;
  740. if (input_error >= bfd_error_on_input)
  741. abort ();
  742. }
  743. /*
  744. FUNCTION
  745. bfd_errmsg
  746. SYNOPSIS
  747. const char *bfd_errmsg (bfd_error_type error_tag);
  748. DESCRIPTION
  749. Return a string describing the error @var{error_tag}, or
  750. the system error if @var{error_tag} is <<bfd_error_system_call>>.
  751. */
  752. const char *
  753. bfd_errmsg (bfd_error_type error_tag)
  754. {
  755. #ifndef errno
  756. extern int errno;
  757. #endif
  758. if (error_tag == bfd_error_on_input)
  759. {
  760. char *buf;
  761. const char *msg = bfd_errmsg (input_error);
  762. if (asprintf (&buf, _(bfd_errmsgs [error_tag]),
  763. bfd_get_filename (input_bfd), msg) != -1)
  764. return buf;
  765. /* Ick, what to do on out of memory? */
  766. return msg;
  767. }
  768. if (error_tag == bfd_error_system_call)
  769. return xstrerror (errno);
  770. if (error_tag > bfd_error_invalid_error_code)
  771. error_tag = bfd_error_invalid_error_code; /* sanity check */
  772. return _(bfd_errmsgs [error_tag]);
  773. }
  774. /*
  775. FUNCTION
  776. bfd_perror
  777. SYNOPSIS
  778. void bfd_perror (const char *message);
  779. DESCRIPTION
  780. Print to the standard error stream a string describing the
  781. last BFD error that occurred, or the last system error if
  782. the last BFD error was a system call failure. If @var{message}
  783. is non-NULL and non-empty, the error string printed is preceded
  784. by @var{message}, a colon, and a space. It is followed by a newline.
  785. */
  786. void
  787. bfd_perror (const char *message)
  788. {
  789. fflush (stdout);
  790. if (message == NULL || *message == '\0')
  791. fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ()));
  792. else
  793. fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ()));
  794. fflush (stderr);
  795. }
  796. /*
  797. SUBSECTION
  798. BFD error handler
  799. Some BFD functions want to print messages describing the
  800. problem. They call a BFD error handler function. This
  801. function may be overridden by the program.
  802. The BFD error handler acts like vprintf.
  803. CODE_FRAGMENT
  804. .
  805. .typedef void (*bfd_error_handler_type) (const char *, va_list);
  806. .
  807. */
  808. /* The program name used when printing BFD error messages. */
  809. static const char *_bfd_error_program_name;
  810. /* Support for positional parameters. */
  811. union _bfd_doprnt_args
  812. {
  813. int i;
  814. long l;
  815. long long ll;
  816. double d;
  817. long double ld;
  818. void *p;
  819. enum
  820. {
  821. Bad,
  822. Int,
  823. Long,
  824. LongLong,
  825. Double,
  826. LongDouble,
  827. Ptr
  828. } type;
  829. };
  830. /* This macro and _bfd_doprnt taken from libiberty _doprnt.c, tidied a
  831. little and extended to handle '%pA', '%pB' and positional parameters. */
  832. #define PRINT_TYPE(TYPE, FIELD) \
  833. do \
  834. { \
  835. TYPE value = (TYPE) args[arg_no].FIELD; \
  836. result = fprintf (stream, specifier, value); \
  837. } while (0)
  838. static int
  839. _bfd_doprnt (FILE *stream, const char *format, union _bfd_doprnt_args *args)
  840. {
  841. const char *ptr = format;
  842. char specifier[128];
  843. int total_printed = 0;
  844. unsigned int arg_count = 0;
  845. while (*ptr != '\0')
  846. {
  847. int result;
  848. if (*ptr != '%')
  849. {
  850. /* While we have regular characters, print them. */
  851. char *end = strchr (ptr, '%');
  852. if (end != NULL)
  853. result = fprintf (stream, "%.*s", (int) (end - ptr), ptr);
  854. else
  855. result = fprintf (stream, "%s", ptr);
  856. ptr += result;
  857. }
  858. else if (ptr[1] == '%')
  859. {
  860. fputc ('%', stream);
  861. result = 1;
  862. ptr += 2;
  863. }
  864. else
  865. {
  866. /* We have a format specifier! */
  867. char *sptr = specifier;
  868. int wide_width = 0, short_width = 0;
  869. unsigned int arg_no;
  870. /* Copy the % and move forward. */
  871. *sptr++ = *ptr++;
  872. /* Check for a positional parameter. */
  873. arg_no = -1u;
  874. if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
  875. {
  876. arg_no = *ptr - '1';
  877. ptr += 2;
  878. }
  879. /* Move past flags. */
  880. while (strchr ("-+ #0'I", *ptr))
  881. *sptr++ = *ptr++;
  882. if (*ptr == '*')
  883. {
  884. int value;
  885. unsigned int arg_index;
  886. ptr++;
  887. arg_index = arg_count;
  888. if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
  889. {
  890. arg_index = *ptr - '1';
  891. ptr += 2;
  892. }
  893. value = abs (args[arg_index].i);
  894. arg_count++;
  895. sptr += sprintf (sptr, "%d", value);
  896. }
  897. else
  898. /* Handle explicit numeric value. */
  899. while (ISDIGIT (*ptr))
  900. *sptr++ = *ptr++;
  901. /* Precision. */
  902. if (*ptr == '.')
  903. {
  904. /* Copy and go past the period. */
  905. *sptr++ = *ptr++;
  906. if (*ptr == '*')
  907. {
  908. int value;
  909. unsigned int arg_index;
  910. ptr++;
  911. arg_index = arg_count;
  912. if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
  913. {
  914. arg_index = *ptr - '1';
  915. ptr += 2;
  916. }
  917. value = abs (args[arg_index].i);
  918. arg_count++;
  919. sptr += sprintf (sptr, "%d", value);
  920. }
  921. else
  922. /* Handle explicit numeric value. */
  923. while (ISDIGIT (*ptr))
  924. *sptr++ = *ptr++;
  925. }
  926. while (strchr ("hlL", *ptr))
  927. {
  928. switch (*ptr)
  929. {
  930. case 'h':
  931. short_width = 1;
  932. break;
  933. case 'l':
  934. wide_width++;
  935. break;
  936. case 'L':
  937. wide_width = 2;
  938. break;
  939. default:
  940. abort();
  941. }
  942. *sptr++ = *ptr++;
  943. }
  944. /* Copy the type specifier, and NULL terminate. */
  945. *sptr++ = *ptr++;
  946. *sptr = '\0';
  947. if ((int) arg_no < 0)
  948. arg_no = arg_count;
  949. switch (ptr[-1])
  950. {
  951. case 'd':
  952. case 'i':
  953. case 'o':
  954. case 'u':
  955. case 'x':
  956. case 'X':
  957. case 'c':
  958. {
  959. /* Short values are promoted to int, so just copy it
  960. as an int and trust the C library printf to cast it
  961. to the right width. */
  962. if (short_width)
  963. PRINT_TYPE (int, i);
  964. else
  965. {
  966. switch (wide_width)
  967. {
  968. case 0:
  969. PRINT_TYPE (int, i);
  970. break;
  971. case 1:
  972. PRINT_TYPE (long, l);
  973. break;
  974. case 2:
  975. default:
  976. #if defined (__MSVCRT__)
  977. sptr[-3] = 'I';
  978. sptr[-2] = '6';
  979. sptr[-1] = '4';
  980. *sptr++ = ptr[-1];
  981. *sptr = '\0';
  982. #endif
  983. PRINT_TYPE (long long, ll);
  984. break;
  985. }
  986. }
  987. }
  988. break;
  989. case 'f':
  990. case 'e':
  991. case 'E':
  992. case 'g':
  993. case 'G':
  994. {
  995. if (wide_width == 0)
  996. PRINT_TYPE (double, d);
  997. else
  998. PRINT_TYPE (long double, ld);
  999. }
  1000. break;
  1001. case 's':
  1002. PRINT_TYPE (char *, p);
  1003. break;
  1004. case 'p':
  1005. if (*ptr == 'A')
  1006. {
  1007. asection *sec;
  1008. bfd *abfd;
  1009. const char *group = NULL;
  1010. struct coff_comdat_info *ci;
  1011. ptr++;
  1012. sec = (asection *) args[arg_no].p;
  1013. if (sec == NULL)
  1014. /* Invoking %pA with a null section pointer is an
  1015. internal error. */
  1016. abort ();
  1017. abfd = sec->owner;
  1018. if (abfd != NULL
  1019. && bfd_get_flavour (abfd) == bfd_target_elf_flavour
  1020. && elf_next_in_group (sec) != NULL
  1021. && (sec->flags & SEC_GROUP) == 0)
  1022. group = elf_group_name (sec);
  1023. else if (abfd != NULL
  1024. && bfd_get_flavour (abfd) == bfd_target_coff_flavour
  1025. && (ci = bfd_coff_get_comdat_section (sec->owner,
  1026. sec)) != NULL)
  1027. group = ci->name;
  1028. if (group != NULL)
  1029. result = fprintf (stream, "%s[%s]", sec->name, group);
  1030. else
  1031. result = fprintf (stream, "%s", sec->name);
  1032. }
  1033. else if (*ptr == 'B')
  1034. {
  1035. bfd *abfd;
  1036. ptr++;
  1037. abfd = (bfd *) args[arg_no].p;
  1038. if (abfd == NULL)
  1039. /* Invoking %pB with a null bfd pointer is an
  1040. internal error. */
  1041. abort ();
  1042. else if (abfd->my_archive
  1043. && !bfd_is_thin_archive (abfd->my_archive))
  1044. result = fprintf (stream, "%s(%s)",
  1045. bfd_get_filename (abfd->my_archive),
  1046. bfd_get_filename (abfd));
  1047. else
  1048. result = fprintf (stream, "%s", bfd_get_filename (abfd));
  1049. }
  1050. else
  1051. PRINT_TYPE (void *, p);
  1052. break;
  1053. default:
  1054. abort();
  1055. }
  1056. arg_count++;
  1057. }
  1058. if (result == -1)
  1059. return -1;
  1060. total_printed += result;
  1061. }
  1062. return total_printed;
  1063. }
  1064. /* First pass over FORMAT to gather ARGS. Returns number of args. */
  1065. static unsigned int
  1066. _bfd_doprnt_scan (const char *format, union _bfd_doprnt_args *args)
  1067. {
  1068. const char *ptr = format;
  1069. unsigned int arg_count = 0;
  1070. while (*ptr != '\0')
  1071. {
  1072. if (*ptr != '%')
  1073. {
  1074. ptr = strchr (ptr, '%');
  1075. if (ptr == NULL)
  1076. break;
  1077. }
  1078. else if (ptr[1] == '%')
  1079. ptr += 2;
  1080. else
  1081. {
  1082. int wide_width = 0, short_width = 0;
  1083. unsigned int arg_no;
  1084. int arg_type;
  1085. ptr++;
  1086. /* Check for a positional parameter. */
  1087. arg_no = -1u;
  1088. if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
  1089. {
  1090. arg_no = *ptr - '1';
  1091. ptr += 2;
  1092. }
  1093. /* Move past flags. */
  1094. while (strchr ("-+ #0'I", *ptr))
  1095. ptr++;
  1096. if (*ptr == '*')
  1097. {
  1098. unsigned int arg_index;
  1099. ptr++;
  1100. arg_index = arg_count;
  1101. if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
  1102. {
  1103. arg_index = *ptr - '1';
  1104. ptr += 2;
  1105. }
  1106. if (arg_index >= 9)
  1107. abort ();
  1108. args[arg_index].type = Int;
  1109. arg_count++;
  1110. }
  1111. else
  1112. /* Handle explicit numeric value. */
  1113. while (ISDIGIT (*ptr))
  1114. ptr++;
  1115. /* Precision. */
  1116. if (*ptr == '.')
  1117. {
  1118. ptr++;
  1119. if (*ptr == '*')
  1120. {
  1121. unsigned int arg_index;
  1122. ptr++;
  1123. arg_index = arg_count;
  1124. if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
  1125. {
  1126. arg_index = *ptr - '1';
  1127. ptr += 2;
  1128. }
  1129. if (arg_index >= 9)
  1130. abort ();
  1131. args[arg_index].type = Int;
  1132. arg_count++;
  1133. }
  1134. else
  1135. /* Handle explicit numeric value. */
  1136. while (ISDIGIT (*ptr))
  1137. ptr++;
  1138. }
  1139. while (strchr ("hlL", *ptr))
  1140. {
  1141. switch (*ptr)
  1142. {
  1143. case 'h':
  1144. short_width = 1;
  1145. break;
  1146. case 'l':
  1147. wide_width++;
  1148. break;
  1149. case 'L':
  1150. wide_width = 2;
  1151. break;
  1152. default:
  1153. abort();
  1154. }
  1155. ptr++;
  1156. }
  1157. ptr++;
  1158. if ((int) arg_no < 0)
  1159. arg_no = arg_count;
  1160. arg_type = Bad;
  1161. switch (ptr[-1])
  1162. {
  1163. case 'd':
  1164. case 'i':
  1165. case 'o':
  1166. case 'u':
  1167. case 'x':
  1168. case 'X':
  1169. case 'c':
  1170. {
  1171. if (short_width)
  1172. arg_type = Int;
  1173. else
  1174. {
  1175. switch (wide_width)
  1176. {
  1177. case 0:
  1178. arg_type = Int;
  1179. break;
  1180. case 1:
  1181. arg_type = Long;
  1182. break;
  1183. case 2:
  1184. default:
  1185. arg_type = LongLong;
  1186. break;
  1187. }
  1188. }
  1189. }
  1190. break;
  1191. case 'f':
  1192. case 'e':
  1193. case 'E':
  1194. case 'g':
  1195. case 'G':
  1196. {
  1197. if (wide_width == 0)
  1198. arg_type = Double;
  1199. else
  1200. arg_type = LongDouble;
  1201. }
  1202. break;
  1203. case 's':
  1204. arg_type = Ptr;
  1205. break;
  1206. case 'p':
  1207. if (*ptr == 'A' || *ptr == 'B')
  1208. ptr++;
  1209. arg_type = Ptr;
  1210. break;
  1211. default:
  1212. abort();
  1213. }
  1214. if (arg_no >= 9)
  1215. abort ();
  1216. args[arg_no].type = arg_type;
  1217. arg_count++;
  1218. }
  1219. }
  1220. return arg_count;
  1221. }
  1222. static void
  1223. error_handler_internal (const char *fmt, va_list ap)
  1224. {
  1225. unsigned int i, arg_count;
  1226. union _bfd_doprnt_args args[9];
  1227. for (i = 0; i < sizeof (args) / sizeof (args[0]); i++)
  1228. args[i].type = Bad;
  1229. arg_count = _bfd_doprnt_scan (fmt, args);
  1230. for (i = 0; i < arg_count; i++)
  1231. {
  1232. switch (args[i].type)
  1233. {
  1234. case Int:
  1235. args[i].i = va_arg (ap, int);
  1236. break;
  1237. case Long:
  1238. args[i].l = va_arg (ap, long);
  1239. break;
  1240. case LongLong:
  1241. args[i].ll = va_arg (ap, long long);
  1242. break;
  1243. case Double:
  1244. args[i].d = va_arg (ap, double);
  1245. break;
  1246. case LongDouble:
  1247. args[i].ld = va_arg (ap, long double);
  1248. break;
  1249. case Ptr:
  1250. args[i].p = va_arg (ap, void *);
  1251. break;
  1252. default:
  1253. abort ();
  1254. }
  1255. }
  1256. /* PR 4992: Don't interrupt output being sent to stdout. */
  1257. fflush (stdout);
  1258. if (_bfd_error_program_name != NULL)
  1259. fprintf (stderr, "%s: ", _bfd_error_program_name);
  1260. else
  1261. fprintf (stderr, "BFD: ");
  1262. _bfd_doprnt (stderr, fmt, args);
  1263. /* On AIX, putc is implemented as a macro that triggers a -Wunused-value
  1264. warning, so use the fputc function to avoid it. */
  1265. fputc ('\n', stderr);
  1266. fflush (stderr);
  1267. }
  1268. /* This is a function pointer to the routine which should handle BFD
  1269. error messages. It is called when a BFD routine encounters an
  1270. error for which it wants to print a message. Going through a
  1271. function pointer permits a program linked against BFD to intercept
  1272. the messages and deal with them itself. */
  1273. static bfd_error_handler_type _bfd_error_internal = error_handler_internal;
  1274. /*
  1275. FUNCTION
  1276. _bfd_error_handler
  1277. SYNOPSIS
  1278. void _bfd_error_handler (const char *fmt, ...) ATTRIBUTE_PRINTF_1;
  1279. DESCRIPTION
  1280. This is the default routine to handle BFD error messages.
  1281. Like fprintf (stderr, ...), but also handles some extra format
  1282. specifiers.
  1283. %pA section name from section. For group components, prints
  1284. group name too.
  1285. %pB file name from bfd. For archive components, prints
  1286. archive too.
  1287. Beware: Only supports a maximum of 9 format arguments.
  1288. */
  1289. void
  1290. _bfd_error_handler (const char *fmt, ...)
  1291. {
  1292. va_list ap;
  1293. va_start (ap, fmt);
  1294. _bfd_error_internal (fmt, ap);
  1295. va_end (ap);
  1296. }
  1297. /*
  1298. FUNCTION
  1299. bfd_set_error_handler
  1300. SYNOPSIS
  1301. bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type);
  1302. DESCRIPTION
  1303. Set the BFD error handler function. Returns the previous
  1304. function.
  1305. */
  1306. bfd_error_handler_type
  1307. bfd_set_error_handler (bfd_error_handler_type pnew)
  1308. {
  1309. bfd_error_handler_type pold;
  1310. pold = _bfd_error_internal;
  1311. _bfd_error_internal = pnew;
  1312. return pold;
  1313. }
  1314. /*
  1315. FUNCTION
  1316. bfd_set_error_program_name
  1317. SYNOPSIS
  1318. void bfd_set_error_program_name (const char *);
  1319. DESCRIPTION
  1320. Set the program name to use when printing a BFD error. This
  1321. is printed before the error message followed by a colon and
  1322. space. The string must not be changed after it is passed to
  1323. this function.
  1324. */
  1325. void
  1326. bfd_set_error_program_name (const char *name)
  1327. {
  1328. _bfd_error_program_name = name;
  1329. }
  1330. /*
  1331. SUBSECTION
  1332. BFD assert handler
  1333. If BFD finds an internal inconsistency, the bfd assert
  1334. handler is called with information on the BFD version, BFD
  1335. source file and line. If this happens, most programs linked
  1336. against BFD are expected to want to exit with an error, or mark
  1337. the current BFD operation as failed, so it is recommended to
  1338. override the default handler, which just calls
  1339. _bfd_error_handler and continues.
  1340. CODE_FRAGMENT
  1341. .
  1342. .typedef void (*bfd_assert_handler_type) (const char *bfd_formatmsg,
  1343. . const char *bfd_version,
  1344. . const char *bfd_file,
  1345. . int bfd_line);
  1346. .
  1347. */
  1348. /* Note the use of bfd_ prefix on the parameter names above: we want to
  1349. show which one is the message and which is the version by naming the
  1350. parameters, but avoid polluting the program-using-bfd namespace as
  1351. the typedef is visible in the exported headers that the program
  1352. includes. Below, it's just for consistency. */
  1353. static void
  1354. _bfd_default_assert_handler (const char *bfd_formatmsg,
  1355. const char *bfd_version,
  1356. const char *bfd_file,
  1357. int bfd_line)
  1358. {
  1359. _bfd_error_handler (bfd_formatmsg, bfd_version, bfd_file, bfd_line);
  1360. }
  1361. /* Similar to _bfd_error_handler, a program can decide to exit on an
  1362. internal BFD error. We use a non-variadic type to simplify passing
  1363. on parameters to other functions, e.g. _bfd_error_handler. */
  1364. static bfd_assert_handler_type _bfd_assert_handler = _bfd_default_assert_handler;
  1365. /*
  1366. FUNCTION
  1367. bfd_set_assert_handler
  1368. SYNOPSIS
  1369. bfd_assert_handler_type bfd_set_assert_handler (bfd_assert_handler_type);
  1370. DESCRIPTION
  1371. Set the BFD assert handler function. Returns the previous
  1372. function.
  1373. */
  1374. bfd_assert_handler_type
  1375. bfd_set_assert_handler (bfd_assert_handler_type pnew)
  1376. {
  1377. bfd_assert_handler_type pold;
  1378. pold = _bfd_assert_handler;
  1379. _bfd_assert_handler = pnew;
  1380. return pold;
  1381. }
  1382. /*
  1383. INODE
  1384. Miscellaneous, Memory Usage, Error reporting, BFD front end
  1385. SECTION
  1386. Miscellaneous
  1387. SUBSECTION
  1388. Miscellaneous functions
  1389. */
  1390. /*
  1391. FUNCTION
  1392. bfd_get_reloc_upper_bound
  1393. SYNOPSIS
  1394. long bfd_get_reloc_upper_bound (bfd *abfd, asection *sect);
  1395. DESCRIPTION
  1396. Return the number of bytes required to store the
  1397. relocation information associated with section @var{sect}
  1398. attached to bfd @var{abfd}. If an error occurs, return -1.
  1399. */
  1400. long
  1401. bfd_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
  1402. {
  1403. if (abfd->format != bfd_object)
  1404. {
  1405. bfd_set_error (bfd_error_invalid_operation);
  1406. return -1;
  1407. }
  1408. return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
  1409. }
  1410. /*
  1411. FUNCTION
  1412. bfd_canonicalize_reloc
  1413. SYNOPSIS
  1414. long bfd_canonicalize_reloc
  1415. (bfd *abfd, asection *sec, arelent **loc, asymbol **syms);
  1416. DESCRIPTION
  1417. Call the back end associated with the open BFD
  1418. @var{abfd} and translate the external form of the relocation
  1419. information attached to @var{sec} into the internal canonical
  1420. form. Place the table into memory at @var{loc}, which has
  1421. been preallocated, usually by a call to
  1422. <<bfd_get_reloc_upper_bound>>. Returns the number of relocs, or
  1423. -1 on error.
  1424. The @var{syms} table is also needed for horrible internal magic
  1425. reasons.
  1426. */
  1427. long
  1428. bfd_canonicalize_reloc (bfd *abfd,
  1429. sec_ptr asect,
  1430. arelent **location,
  1431. asymbol **symbols)
  1432. {
  1433. if (abfd->format != bfd_object)
  1434. {
  1435. bfd_set_error (bfd_error_invalid_operation);
  1436. return -1;
  1437. }
  1438. return BFD_SEND (abfd, _bfd_canonicalize_reloc,
  1439. (abfd, asect, location, symbols));
  1440. }
  1441. /*
  1442. FUNCTION
  1443. bfd_set_reloc
  1444. SYNOPSIS
  1445. void bfd_set_reloc
  1446. (bfd *abfd, asection *sec, arelent **rel, unsigned int count);
  1447. DESCRIPTION
  1448. Set the relocation pointer and count within
  1449. section @var{sec} to the values @var{rel} and @var{count}.
  1450. The argument @var{abfd} is ignored.
  1451. .#define bfd_set_reloc(abfd, asect, location, count) \
  1452. . BFD_SEND (abfd, _bfd_set_reloc, (abfd, asect, location, count))
  1453. */
  1454. /*
  1455. FUNCTION
  1456. bfd_set_file_flags
  1457. SYNOPSIS
  1458. bool bfd_set_file_flags (bfd *abfd, flagword flags);
  1459. DESCRIPTION
  1460. Set the flag word in the BFD @var{abfd} to the value @var{flags}.
  1461. Possible errors are:
  1462. o <<bfd_error_wrong_format>> - The target bfd was not of object format.
  1463. o <<bfd_error_invalid_operation>> - The target bfd was open for reading.
  1464. o <<bfd_error_invalid_operation>> -
  1465. The flag word contained a bit which was not applicable to the
  1466. type of file. E.g., an attempt was made to set the <<D_PAGED>> bit
  1467. on a BFD format which does not support demand paging.
  1468. */
  1469. bool
  1470. bfd_set_file_flags (bfd *abfd, flagword flags)
  1471. {
  1472. if (abfd->format != bfd_object)
  1473. {
  1474. bfd_set_error (bfd_error_wrong_format);
  1475. return false;
  1476. }
  1477. if (bfd_read_p (abfd))
  1478. {
  1479. bfd_set_error (bfd_error_invalid_operation);
  1480. return false;
  1481. }
  1482. abfd->flags = flags;
  1483. if ((flags & bfd_applicable_file_flags (abfd)) != flags)
  1484. {
  1485. bfd_set_error (bfd_error_invalid_operation);
  1486. return false;
  1487. }
  1488. return true;
  1489. }
  1490. void
  1491. bfd_assert (const char *file, int line)
  1492. {
  1493. /* xgettext:c-format */
  1494. (*_bfd_assert_handler) (_("BFD %s assertion fail %s:%d"),
  1495. BFD_VERSION_STRING, file, line);
  1496. }
  1497. /* A more or less friendly abort message. In libbfd.h abort is
  1498. defined to call this function. */
  1499. void
  1500. _bfd_abort (const char *file, int line, const char *fn)
  1501. {
  1502. if (fn != NULL)
  1503. _bfd_error_handler
  1504. /* xgettext:c-format */
  1505. (_("BFD %s internal error, aborting at %s:%d in %s\n"),
  1506. BFD_VERSION_STRING, file, line, fn);
  1507. else
  1508. _bfd_error_handler
  1509. /* xgettext:c-format */
  1510. (_("BFD %s internal error, aborting at %s:%d\n"),
  1511. BFD_VERSION_STRING, file, line);
  1512. _bfd_error_handler (_("Please report this bug.\n"));
  1513. _exit (EXIT_FAILURE);
  1514. }
  1515. /*
  1516. FUNCTION
  1517. bfd_get_arch_size
  1518. SYNOPSIS
  1519. int bfd_get_arch_size (bfd *abfd);
  1520. DESCRIPTION
  1521. Returns the normalized architecture address size, in bits, as
  1522. determined by the object file's format. By normalized, we mean
  1523. either 32 or 64. For ELF, this information is included in the
  1524. header. Use bfd_arch_bits_per_address for number of bits in
  1525. the architecture address.
  1526. RETURNS
  1527. Returns the arch size in bits if known, <<-1>> otherwise.
  1528. */
  1529. int
  1530. bfd_get_arch_size (bfd *abfd)
  1531. {
  1532. if (abfd->xvec->flavour == bfd_target_elf_flavour)
  1533. return get_elf_backend_data (abfd)->s->arch_size;
  1534. return bfd_arch_bits_per_address (abfd) > 32 ? 64 : 32;
  1535. }
  1536. /*
  1537. FUNCTION
  1538. bfd_get_sign_extend_vma
  1539. SYNOPSIS
  1540. int bfd_get_sign_extend_vma (bfd *abfd);
  1541. DESCRIPTION
  1542. Indicates if the target architecture "naturally" sign extends
  1543. an address. Some architectures implicitly sign extend address
  1544. values when they are converted to types larger than the size
  1545. of an address. For instance, bfd_get_start_address() will
  1546. return an address sign extended to fill a bfd_vma when this is
  1547. the case.
  1548. RETURNS
  1549. Returns <<1>> if the target architecture is known to sign
  1550. extend addresses, <<0>> if the target architecture is known to
  1551. not sign extend addresses, and <<-1>> otherwise.
  1552. */
  1553. int
  1554. bfd_get_sign_extend_vma (bfd *abfd)
  1555. {
  1556. const char *name;
  1557. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  1558. return get_elf_backend_data (abfd)->sign_extend_vma;
  1559. name = bfd_get_target (abfd);
  1560. /* Return a proper value for DJGPP & PE COFF.
  1561. This function is required for DWARF2 support, but there is
  1562. no place to store this information in the COFF back end.
  1563. Should enough other COFF targets add support for DWARF2,
  1564. a place will have to be found. Until then, this hack will do. */
  1565. if (startswith (name, "coff-go32")
  1566. || strcmp (name, "pe-i386") == 0
  1567. || strcmp (name, "pei-i386") == 0
  1568. || strcmp (name, "pe-x86-64") == 0
  1569. || strcmp (name, "pei-x86-64") == 0
  1570. || strcmp (name, "pei-aarch64-little") == 0
  1571. || strcmp (name, "pe-arm-wince-little") == 0
  1572. || strcmp (name, "pei-arm-wince-little") == 0
  1573. || strcmp (name, "aixcoff-rs6000") == 0
  1574. || strcmp (name, "aix5coff64-rs6000") == 0)
  1575. return 1;
  1576. if (startswith (name, "mach-o"))
  1577. return 0;
  1578. bfd_set_error (bfd_error_wrong_format);
  1579. return -1;
  1580. }
  1581. /*
  1582. FUNCTION
  1583. bfd_set_start_address
  1584. SYNOPSIS
  1585. bool bfd_set_start_address (bfd *abfd, bfd_vma vma);
  1586. DESCRIPTION
  1587. Make @var{vma} the entry point of output BFD @var{abfd}.
  1588. RETURNS
  1589. Returns <<TRUE>> on success, <<FALSE>> otherwise.
  1590. */
  1591. bool
  1592. bfd_set_start_address (bfd *abfd, bfd_vma vma)
  1593. {
  1594. abfd->start_address = vma;
  1595. return true;
  1596. }
  1597. /*
  1598. FUNCTION
  1599. bfd_get_gp_size
  1600. SYNOPSIS
  1601. unsigned int bfd_get_gp_size (bfd *abfd);
  1602. DESCRIPTION
  1603. Return the maximum size of objects to be optimized using the GP
  1604. register under MIPS ECOFF. This is typically set by the <<-G>>
  1605. argument to the compiler, assembler or linker.
  1606. */
  1607. unsigned int
  1608. bfd_get_gp_size (bfd *abfd)
  1609. {
  1610. if (abfd->format == bfd_object)
  1611. {
  1612. if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
  1613. return ecoff_data (abfd)->gp_size;
  1614. else if (abfd->xvec->flavour == bfd_target_elf_flavour)
  1615. return elf_gp_size (abfd);
  1616. }
  1617. return 0;
  1618. }
  1619. /*
  1620. FUNCTION
  1621. bfd_set_gp_size
  1622. SYNOPSIS
  1623. void bfd_set_gp_size (bfd *abfd, unsigned int i);
  1624. DESCRIPTION
  1625. Set the maximum size of objects to be optimized using the GP
  1626. register under ECOFF or MIPS ELF. This is typically set by
  1627. the <<-G>> argument to the compiler, assembler or linker.
  1628. */
  1629. void
  1630. bfd_set_gp_size (bfd *abfd, unsigned int i)
  1631. {
  1632. /* Don't try to set GP size on an archive or core file! */
  1633. if (abfd->format != bfd_object)
  1634. return;
  1635. if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
  1636. ecoff_data (abfd)->gp_size = i;
  1637. else if (abfd->xvec->flavour == bfd_target_elf_flavour)
  1638. elf_gp_size (abfd) = i;
  1639. }
  1640. /* Get the GP value. This is an internal function used by some of the
  1641. relocation special_function routines on targets which support a GP
  1642. register. */
  1643. bfd_vma
  1644. _bfd_get_gp_value (bfd *abfd)
  1645. {
  1646. if (! abfd)
  1647. return 0;
  1648. if (abfd->format != bfd_object)
  1649. return 0;
  1650. if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
  1651. return ecoff_data (abfd)->gp;
  1652. else if (abfd->xvec->flavour == bfd_target_elf_flavour)
  1653. return elf_gp (abfd);
  1654. return 0;
  1655. }
  1656. /* Set the GP value. */
  1657. void
  1658. _bfd_set_gp_value (bfd *abfd, bfd_vma v)
  1659. {
  1660. if (! abfd)
  1661. abort ();
  1662. if (abfd->format != bfd_object)
  1663. return;
  1664. if (abfd->xvec->flavour == bfd_target_ecoff_flavour)
  1665. ecoff_data (abfd)->gp = v;
  1666. else if (abfd->xvec->flavour == bfd_target_elf_flavour)
  1667. elf_gp (abfd) = v;
  1668. }
  1669. /*
  1670. FUNCTION
  1671. bfd_set_gp_value
  1672. SYNOPSIS
  1673. void bfd_set_gp_value (bfd *abfd, bfd_vma v);
  1674. DESCRIPTION
  1675. Allow external access to the fucntion to set the GP value.
  1676. This is specifically added for gdb-compile support.
  1677. */
  1678. void
  1679. bfd_set_gp_value (bfd *abfd, bfd_vma v)
  1680. {
  1681. _bfd_set_gp_value (abfd, v);
  1682. }
  1683. /*
  1684. FUNCTION
  1685. bfd_scan_vma
  1686. SYNOPSIS
  1687. bfd_vma bfd_scan_vma (const char *string, const char **end, int base);
  1688. DESCRIPTION
  1689. Convert, like <<strtoul>>, a numerical expression
  1690. @var{string} into a <<bfd_vma>> integer, and return that integer.
  1691. (Though without as many bells and whistles as <<strtoul>>.)
  1692. The expression is assumed to be unsigned (i.e., positive).
  1693. If given a @var{base}, it is used as the base for conversion.
  1694. A base of 0 causes the function to interpret the string
  1695. in hex if a leading "0x" or "0X" is found, otherwise
  1696. in octal if a leading zero is found, otherwise in decimal.
  1697. If the value would overflow, the maximum <<bfd_vma>> value is
  1698. returned.
  1699. */
  1700. bfd_vma
  1701. bfd_scan_vma (const char *string, const char **end, int base)
  1702. {
  1703. bfd_vma value;
  1704. bfd_vma cutoff;
  1705. unsigned int cutlim;
  1706. int overflow;
  1707. /* Let the host do it if possible. */
  1708. if (sizeof (bfd_vma) <= sizeof (unsigned long))
  1709. return strtoul (string, (char **) end, base);
  1710. if (sizeof (bfd_vma) <= sizeof (unsigned long long))
  1711. return strtoull (string, (char **) end, base);
  1712. if (base == 0)
  1713. {
  1714. if (string[0] == '0')
  1715. {
  1716. if ((string[1] == 'x') || (string[1] == 'X'))
  1717. base = 16;
  1718. else
  1719. base = 8;
  1720. }
  1721. }
  1722. if ((base < 2) || (base > 36))
  1723. base = 10;
  1724. if (base == 16
  1725. && string[0] == '0'
  1726. && (string[1] == 'x' || string[1] == 'X')
  1727. && ISXDIGIT (string[2]))
  1728. {
  1729. string += 2;
  1730. }
  1731. cutoff = (~ (bfd_vma) 0) / (bfd_vma) base;
  1732. cutlim = (~ (bfd_vma) 0) % (bfd_vma) base;
  1733. value = 0;
  1734. overflow = 0;
  1735. while (1)
  1736. {
  1737. unsigned int digit;
  1738. digit = *string;
  1739. if (ISDIGIT (digit))
  1740. digit = digit - '0';
  1741. else if (ISALPHA (digit))
  1742. digit = TOUPPER (digit) - 'A' + 10;
  1743. else
  1744. break;
  1745. if (digit >= (unsigned int) base)
  1746. break;
  1747. if (value > cutoff || (value == cutoff && digit > cutlim))
  1748. overflow = 1;
  1749. value = value * base + digit;
  1750. ++string;
  1751. }
  1752. if (overflow)
  1753. value = ~ (bfd_vma) 0;
  1754. if (end != NULL)
  1755. *end = string;
  1756. return value;
  1757. }
  1758. /*
  1759. FUNCTION
  1760. bfd_copy_private_header_data
  1761. SYNOPSIS
  1762. bool bfd_copy_private_header_data (bfd *ibfd, bfd *obfd);
  1763. DESCRIPTION
  1764. Copy private BFD header information from the BFD @var{ibfd} to the
  1765. the BFD @var{obfd}. This copies information that may require
  1766. sections to exist, but does not require symbol tables. Return
  1767. <<true>> on success, <<false>> on error.
  1768. Possible error returns are:
  1769. o <<bfd_error_no_memory>> -
  1770. Not enough memory exists to create private data for @var{obfd}.
  1771. .#define bfd_copy_private_header_data(ibfd, obfd) \
  1772. . BFD_SEND (obfd, _bfd_copy_private_header_data, \
  1773. . (ibfd, obfd))
  1774. */
  1775. /*
  1776. FUNCTION
  1777. bfd_copy_private_bfd_data
  1778. SYNOPSIS
  1779. bool bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd);
  1780. DESCRIPTION
  1781. Copy private BFD information from the BFD @var{ibfd} to the
  1782. the BFD @var{obfd}. Return <<TRUE>> on success, <<FALSE>> on error.
  1783. Possible error returns are:
  1784. o <<bfd_error_no_memory>> -
  1785. Not enough memory exists to create private data for @var{obfd}.
  1786. .#define bfd_copy_private_bfd_data(ibfd, obfd) \
  1787. . BFD_SEND (obfd, _bfd_copy_private_bfd_data, \
  1788. . (ibfd, obfd))
  1789. */
  1790. /*
  1791. FUNCTION
  1792. bfd_set_private_flags
  1793. SYNOPSIS
  1794. bool bfd_set_private_flags (bfd *abfd, flagword flags);
  1795. DESCRIPTION
  1796. Set private BFD flag information in the BFD @var{abfd}.
  1797. Return <<TRUE>> on success, <<FALSE>> on error. Possible error
  1798. returns are:
  1799. o <<bfd_error_no_memory>> -
  1800. Not enough memory exists to create private data for @var{obfd}.
  1801. .#define bfd_set_private_flags(abfd, flags) \
  1802. . BFD_SEND (abfd, _bfd_set_private_flags, (abfd, flags))
  1803. */
  1804. /*
  1805. FUNCTION
  1806. Other functions
  1807. DESCRIPTION
  1808. The following functions exist but have not yet been documented.
  1809. .#define bfd_sizeof_headers(abfd, info) \
  1810. . BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, info))
  1811. .
  1812. .#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \
  1813. . BFD_SEND (abfd, _bfd_find_nearest_line, \
  1814. . (abfd, syms, sec, off, file, func, line, NULL))
  1815. .
  1816. .#define bfd_find_nearest_line_discriminator(abfd, sec, syms, off, file, func, \
  1817. . line, disc) \
  1818. . BFD_SEND (abfd, _bfd_find_nearest_line, \
  1819. . (abfd, syms, sec, off, file, func, line, disc))
  1820. .
  1821. .#define bfd_find_line(abfd, syms, sym, file, line) \
  1822. . BFD_SEND (abfd, _bfd_find_line, \
  1823. . (abfd, syms, sym, file, line))
  1824. .
  1825. .#define bfd_find_inliner_info(abfd, file, func, line) \
  1826. . BFD_SEND (abfd, _bfd_find_inliner_info, \
  1827. . (abfd, file, func, line))
  1828. .
  1829. .#define bfd_debug_info_start(abfd) \
  1830. . BFD_SEND (abfd, _bfd_debug_info_start, (abfd))
  1831. .
  1832. .#define bfd_debug_info_end(abfd) \
  1833. . BFD_SEND (abfd, _bfd_debug_info_end, (abfd))
  1834. .
  1835. .#define bfd_debug_info_accumulate(abfd, section) \
  1836. . BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section))
  1837. .
  1838. .#define bfd_stat_arch_elt(abfd, stat) \
  1839. . BFD_SEND (abfd->my_archive ? abfd->my_archive : abfd, \
  1840. . _bfd_stat_arch_elt, (abfd, stat))
  1841. .
  1842. .#define bfd_update_armap_timestamp(abfd) \
  1843. . BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd))
  1844. .
  1845. .#define bfd_set_arch_mach(abfd, arch, mach)\
  1846. . BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach))
  1847. .
  1848. .#define bfd_relax_section(abfd, section, link_info, again) \
  1849. . BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again))
  1850. .
  1851. .#define bfd_gc_sections(abfd, link_info) \
  1852. . BFD_SEND (abfd, _bfd_gc_sections, (abfd, link_info))
  1853. .
  1854. .#define bfd_lookup_section_flags(link_info, flag_info, section) \
  1855. . BFD_SEND (abfd, _bfd_lookup_section_flags, (link_info, flag_info, section))
  1856. .
  1857. .#define bfd_merge_sections(abfd, link_info) \
  1858. . BFD_SEND (abfd, _bfd_merge_sections, (abfd, link_info))
  1859. .
  1860. .#define bfd_is_group_section(abfd, sec) \
  1861. . BFD_SEND (abfd, _bfd_is_group_section, (abfd, sec))
  1862. .
  1863. .#define bfd_group_name(abfd, sec) \
  1864. . BFD_SEND (abfd, _bfd_group_name, (abfd, sec))
  1865. .
  1866. .#define bfd_discard_group(abfd, sec) \
  1867. . BFD_SEND (abfd, _bfd_discard_group, (abfd, sec))
  1868. .
  1869. .#define bfd_link_hash_table_create(abfd) \
  1870. . BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd))
  1871. .
  1872. .#define bfd_link_add_symbols(abfd, info) \
  1873. . BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info))
  1874. .
  1875. .#define bfd_link_just_syms(abfd, sec, info) \
  1876. . BFD_SEND (abfd, _bfd_link_just_syms, (sec, info))
  1877. .
  1878. .#define bfd_final_link(abfd, info) \
  1879. . BFD_SEND (abfd, _bfd_final_link, (abfd, info))
  1880. .
  1881. .#define bfd_free_cached_info(abfd) \
  1882. . BFD_SEND (abfd, _bfd_free_cached_info, (abfd))
  1883. .
  1884. .#define bfd_get_dynamic_symtab_upper_bound(abfd) \
  1885. . BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd))
  1886. .
  1887. .#define bfd_print_private_bfd_data(abfd, file)\
  1888. . BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file))
  1889. .
  1890. .#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \
  1891. . BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols))
  1892. .
  1893. .#define bfd_get_synthetic_symtab(abfd, count, syms, dyncount, dynsyms, ret) \
  1894. . BFD_SEND (abfd, _bfd_get_synthetic_symtab, (abfd, count, syms, \
  1895. . dyncount, dynsyms, ret))
  1896. .
  1897. .#define bfd_get_dynamic_reloc_upper_bound(abfd) \
  1898. . BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd))
  1899. .
  1900. .#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \
  1901. . BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms))
  1902. .
  1903. .extern bfd_byte *bfd_get_relocated_section_contents
  1904. . (bfd *, struct bfd_link_info *, struct bfd_link_order *, bfd_byte *,
  1905. . bool, asymbol **);
  1906. .
  1907. */
  1908. bfd_byte *
  1909. bfd_get_relocated_section_contents (bfd *abfd,
  1910. struct bfd_link_info *link_info,
  1911. struct bfd_link_order *link_order,
  1912. bfd_byte *data,
  1913. bool relocatable,
  1914. asymbol **symbols)
  1915. {
  1916. bfd *abfd2;
  1917. bfd_byte *(*fn) (bfd *, struct bfd_link_info *, struct bfd_link_order *,
  1918. bfd_byte *, bool, asymbol **);
  1919. if (link_order->type == bfd_indirect_link_order)
  1920. {
  1921. abfd2 = link_order->u.indirect.section->owner;
  1922. if (abfd2 == NULL)
  1923. abfd2 = abfd;
  1924. }
  1925. else
  1926. abfd2 = abfd;
  1927. fn = abfd2->xvec->_bfd_get_relocated_section_contents;
  1928. return (*fn) (abfd, link_info, link_order, data, relocatable, symbols);
  1929. }
  1930. /* Record information about an ELF program header. */
  1931. bool
  1932. bfd_record_phdr (bfd *abfd,
  1933. unsigned long type,
  1934. bool flags_valid,
  1935. flagword flags,
  1936. bool at_valid,
  1937. bfd_vma at, /* Bytes. */
  1938. bool includes_filehdr,
  1939. bool includes_phdrs,
  1940. unsigned int count,
  1941. asection **secs)
  1942. {
  1943. struct elf_segment_map *m, **pm;
  1944. size_t amt;
  1945. unsigned int opb = bfd_octets_per_byte (abfd, NULL);
  1946. if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  1947. return true;
  1948. amt = sizeof (struct elf_segment_map);
  1949. amt += ((bfd_size_type) count - 1) * sizeof (asection *);
  1950. m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
  1951. if (m == NULL)
  1952. return false;
  1953. m->p_type = type;
  1954. m->p_flags = flags;
  1955. m->p_paddr = at * opb;
  1956. m->p_flags_valid = flags_valid;
  1957. m->p_paddr_valid = at_valid;
  1958. m->includes_filehdr = includes_filehdr;
  1959. m->includes_phdrs = includes_phdrs;
  1960. m->count = count;
  1961. if (count > 0)
  1962. memcpy (m->sections, secs, count * sizeof (asection *));
  1963. for (pm = &elf_seg_map (abfd); *pm != NULL; pm = &(*pm)->next)
  1964. ;
  1965. *pm = m;
  1966. return true;
  1967. }
  1968. #ifdef BFD64
  1969. /* Return true iff this target is 32-bit. */
  1970. static bool
  1971. is32bit (bfd *abfd)
  1972. {
  1973. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  1974. {
  1975. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  1976. return bed->s->elfclass == ELFCLASS32;
  1977. }
  1978. /* For non-ELF targets, use architecture information. */
  1979. return bfd_arch_bits_per_address (abfd) <= 32;
  1980. }
  1981. #endif
  1982. /* bfd_sprintf_vma and bfd_fprintf_vma display an address in the
  1983. target's address size. */
  1984. void
  1985. bfd_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value)
  1986. {
  1987. #ifdef BFD64
  1988. if (is32bit (abfd))
  1989. {
  1990. sprintf (buf, "%08lx", (unsigned long) value & 0xffffffff);
  1991. return;
  1992. }
  1993. #endif
  1994. sprintf_vma (buf, value);
  1995. }
  1996. void
  1997. bfd_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value)
  1998. {
  1999. #ifdef BFD64
  2000. if (is32bit (abfd))
  2001. {
  2002. fprintf ((FILE *) stream, "%08lx", (unsigned long) value & 0xffffffff);
  2003. return;
  2004. }
  2005. #endif
  2006. fprintf_vma ((FILE *) stream, value);
  2007. }
  2008. /*
  2009. FUNCTION
  2010. bfd_alt_mach_code
  2011. SYNOPSIS
  2012. bool bfd_alt_mach_code (bfd *abfd, int alternative);
  2013. DESCRIPTION
  2014. When more than one machine code number is available for the
  2015. same machine type, this function can be used to switch between
  2016. the preferred one (alternative == 0) and any others. Currently,
  2017. only ELF supports this feature, with up to two alternate
  2018. machine codes.
  2019. */
  2020. bool
  2021. bfd_alt_mach_code (bfd *abfd, int alternative)
  2022. {
  2023. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  2024. {
  2025. int code;
  2026. switch (alternative)
  2027. {
  2028. case 0:
  2029. code = get_elf_backend_data (abfd)->elf_machine_code;
  2030. break;
  2031. case 1:
  2032. code = get_elf_backend_data (abfd)->elf_machine_alt1;
  2033. if (code == 0)
  2034. return false;
  2035. break;
  2036. case 2:
  2037. code = get_elf_backend_data (abfd)->elf_machine_alt2;
  2038. if (code == 0)
  2039. return false;
  2040. break;
  2041. default:
  2042. return false;
  2043. }
  2044. elf_elfheader (abfd)->e_machine = code;
  2045. return true;
  2046. }
  2047. return false;
  2048. }
  2049. /*
  2050. FUNCTION
  2051. bfd_emul_get_maxpagesize
  2052. SYNOPSIS
  2053. bfd_vma bfd_emul_get_maxpagesize (const char *);
  2054. DESCRIPTION
  2055. Returns the maximum page size, in bytes, as determined by
  2056. emulation.
  2057. RETURNS
  2058. Returns the maximum page size in bytes for ELF, 0 otherwise.
  2059. */
  2060. bfd_vma
  2061. bfd_emul_get_maxpagesize (const char *emul)
  2062. {
  2063. const bfd_target *target;
  2064. target = bfd_find_target (emul, NULL);
  2065. if (target != NULL
  2066. && target->flavour == bfd_target_elf_flavour)
  2067. return xvec_get_elf_backend_data (target)->maxpagesize;
  2068. return 0;
  2069. }
  2070. /*
  2071. FUNCTION
  2072. bfd_emul_get_commonpagesize
  2073. SYNOPSIS
  2074. bfd_vma bfd_emul_get_commonpagesize (const char *);
  2075. DESCRIPTION
  2076. Returns the common page size, in bytes, as determined by
  2077. emulation.
  2078. RETURNS
  2079. Returns the common page size in bytes for ELF, 0 otherwise.
  2080. */
  2081. bfd_vma
  2082. bfd_emul_get_commonpagesize (const char *emul)
  2083. {
  2084. const bfd_target *target;
  2085. target = bfd_find_target (emul, NULL);
  2086. if (target != NULL
  2087. && target->flavour == bfd_target_elf_flavour)
  2088. {
  2089. const struct elf_backend_data *bed;
  2090. bed = xvec_get_elf_backend_data (target);
  2091. return bed->commonpagesize;
  2092. }
  2093. return 0;
  2094. }
  2095. /*
  2096. FUNCTION
  2097. bfd_demangle
  2098. SYNOPSIS
  2099. char *bfd_demangle (bfd *, const char *, int);
  2100. DESCRIPTION
  2101. Wrapper around cplus_demangle. Strips leading underscores and
  2102. other such chars that would otherwise confuse the demangler.
  2103. If passed a g++ v3 ABI mangled name, returns a buffer allocated
  2104. with malloc holding the demangled name. Returns NULL otherwise
  2105. and on memory alloc failure.
  2106. */
  2107. char *
  2108. bfd_demangle (bfd *abfd, const char *name, int options)
  2109. {
  2110. char *res, *alloc;
  2111. const char *pre, *suf;
  2112. size_t pre_len;
  2113. bool skip_lead;
  2114. skip_lead = (abfd != NULL
  2115. && *name != '\0'
  2116. && bfd_get_symbol_leading_char (abfd) == *name);
  2117. if (skip_lead)
  2118. ++name;
  2119. /* This is a hack for better error reporting on XCOFF, PowerPC64-ELF
  2120. or the MS PE format. These formats have a number of leading '.'s
  2121. on at least some symbols, so we remove all dots to avoid
  2122. confusing the demangler. */
  2123. pre = name;
  2124. while (*name == '.' || *name == '$')
  2125. ++name;
  2126. pre_len = name - pre;
  2127. /* Strip off @plt and suchlike too. */
  2128. alloc = NULL;
  2129. suf = strchr (name, '@');
  2130. if (suf != NULL)
  2131. {
  2132. alloc = (char *) bfd_malloc (suf - name + 1);
  2133. if (alloc == NULL)
  2134. return NULL;
  2135. memcpy (alloc, name, suf - name);
  2136. alloc[suf - name] = '\0';
  2137. name = alloc;
  2138. }
  2139. res = cplus_demangle (name, options);
  2140. free (alloc);
  2141. if (res == NULL)
  2142. {
  2143. if (skip_lead)
  2144. {
  2145. size_t len = strlen (pre) + 1;
  2146. alloc = (char *) bfd_malloc (len);
  2147. if (alloc == NULL)
  2148. return NULL;
  2149. memcpy (alloc, pre, len);
  2150. return alloc;
  2151. }
  2152. return NULL;
  2153. }
  2154. /* Put back any prefix or suffix. */
  2155. if (pre_len != 0 || suf != NULL)
  2156. {
  2157. size_t len;
  2158. size_t suf_len;
  2159. char *final;
  2160. len = strlen (res);
  2161. if (suf == NULL)
  2162. suf = res + len;
  2163. suf_len = strlen (suf) + 1;
  2164. final = (char *) bfd_malloc (pre_len + len + suf_len);
  2165. if (final != NULL)
  2166. {
  2167. memcpy (final, pre, pre_len);
  2168. memcpy (final + pre_len, res, len);
  2169. memcpy (final + pre_len + len, suf, suf_len);
  2170. }
  2171. free (res);
  2172. res = final;
  2173. }
  2174. return res;
  2175. }
  2176. /*
  2177. FUNCTION
  2178. bfd_update_compression_header
  2179. SYNOPSIS
  2180. void bfd_update_compression_header
  2181. (bfd *abfd, bfd_byte *contents, asection *sec);
  2182. DESCRIPTION
  2183. Set the compression header at CONTENTS of SEC in ABFD and update
  2184. elf_section_flags for compression.
  2185. */
  2186. void
  2187. bfd_update_compression_header (bfd *abfd, bfd_byte *contents,
  2188. asection *sec)
  2189. {
  2190. if ((abfd->flags & BFD_COMPRESS) == 0)
  2191. abort ();
  2192. switch (bfd_get_flavour (abfd))
  2193. {
  2194. case bfd_target_elf_flavour:
  2195. if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
  2196. {
  2197. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  2198. struct bfd_elf_section_data * esd = elf_section_data (sec);
  2199. /* Set the SHF_COMPRESSED bit. */
  2200. elf_section_flags (sec) |= SHF_COMPRESSED;
  2201. if (bed->s->elfclass == ELFCLASS32)
  2202. {
  2203. Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
  2204. bfd_put_32 (abfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
  2205. bfd_put_32 (abfd, sec->size, &echdr->ch_size);
  2206. bfd_put_32 (abfd, 1u << sec->alignment_power,
  2207. &echdr->ch_addralign);
  2208. /* bfd_log2 (alignof (Elf32_Chdr)) */
  2209. bfd_set_section_alignment (sec, 2);
  2210. esd->this_hdr.sh_addralign = 4;
  2211. }
  2212. else
  2213. {
  2214. Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
  2215. bfd_put_32 (abfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
  2216. bfd_put_32 (abfd, 0, &echdr->ch_reserved);
  2217. bfd_put_64 (abfd, sec->size, &echdr->ch_size);
  2218. bfd_put_64 (abfd, UINT64_C (1) << sec->alignment_power,
  2219. &echdr->ch_addralign);
  2220. /* bfd_log2 (alignof (Elf64_Chdr)) */
  2221. bfd_set_section_alignment (sec, 3);
  2222. esd->this_hdr.sh_addralign = 8;
  2223. }
  2224. break;
  2225. }
  2226. /* Clear the SHF_COMPRESSED bit. */
  2227. elf_section_flags (sec) &= ~SHF_COMPRESSED;
  2228. /* Fall through. */
  2229. default:
  2230. /* Write the zlib header. It should be "ZLIB" followed by
  2231. the uncompressed section size, 8 bytes in big-endian
  2232. order. */
  2233. memcpy (contents, "ZLIB", 4);
  2234. bfd_putb64 (sec->size, contents + 4);
  2235. /* No way to keep the original alignment, just use 1 always. */
  2236. bfd_set_section_alignment (sec, 0);
  2237. break;
  2238. }
  2239. }
  2240. /*
  2241. FUNCTION
  2242. bfd_check_compression_header
  2243. SYNOPSIS
  2244. bool bfd_check_compression_header
  2245. (bfd *abfd, bfd_byte *contents, asection *sec,
  2246. bfd_size_type *uncompressed_size,
  2247. unsigned int *uncompressed_alignment_power);
  2248. DESCRIPTION
  2249. Check the compression header at CONTENTS of SEC in ABFD and
  2250. store the uncompressed size in UNCOMPRESSED_SIZE and the
  2251. uncompressed data alignment in UNCOMPRESSED_ALIGNMENT_POWER
  2252. if the compression header is valid.
  2253. RETURNS
  2254. Return TRUE if the compression header is valid.
  2255. */
  2256. bool
  2257. bfd_check_compression_header (bfd *abfd, bfd_byte *contents,
  2258. asection *sec,
  2259. bfd_size_type *uncompressed_size,
  2260. unsigned int *uncompressed_alignment_power)
  2261. {
  2262. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  2263. && (elf_section_flags (sec) & SHF_COMPRESSED) != 0)
  2264. {
  2265. Elf_Internal_Chdr chdr;
  2266. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  2267. if (bed->s->elfclass == ELFCLASS32)
  2268. {
  2269. Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
  2270. chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
  2271. chdr.ch_size = bfd_get_32 (abfd, &echdr->ch_size);
  2272. chdr.ch_addralign = bfd_get_32 (abfd, &echdr->ch_addralign);
  2273. }
  2274. else
  2275. {
  2276. Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
  2277. chdr.ch_type = bfd_get_32 (abfd, &echdr->ch_type);
  2278. chdr.ch_size = bfd_get_64 (abfd, &echdr->ch_size);
  2279. chdr.ch_addralign = bfd_get_64 (abfd, &echdr->ch_addralign);
  2280. }
  2281. if (chdr.ch_type == ELFCOMPRESS_ZLIB
  2282. && chdr.ch_addralign == (chdr.ch_addralign & -chdr.ch_addralign))
  2283. {
  2284. *uncompressed_size = chdr.ch_size;
  2285. *uncompressed_alignment_power = bfd_log2 (chdr.ch_addralign);
  2286. return true;
  2287. }
  2288. }
  2289. return false;
  2290. }
  2291. /*
  2292. FUNCTION
  2293. bfd_get_compression_header_size
  2294. SYNOPSIS
  2295. int bfd_get_compression_header_size (bfd *abfd, asection *sec);
  2296. DESCRIPTION
  2297. Return the size of the compression header of SEC in ABFD.
  2298. RETURNS
  2299. Return the size of the compression header in bytes.
  2300. */
  2301. int
  2302. bfd_get_compression_header_size (bfd *abfd, asection *sec)
  2303. {
  2304. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  2305. {
  2306. if (sec == NULL)
  2307. {
  2308. if (!(abfd->flags & BFD_COMPRESS_GABI))
  2309. return 0;
  2310. }
  2311. else if (!(elf_section_flags (sec) & SHF_COMPRESSED))
  2312. return 0;
  2313. if (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS32)
  2314. return sizeof (Elf32_External_Chdr);
  2315. else
  2316. return sizeof (Elf64_External_Chdr);
  2317. }
  2318. return 0;
  2319. }
  2320. /*
  2321. FUNCTION
  2322. bfd_convert_section_size
  2323. SYNOPSIS
  2324. bfd_size_type bfd_convert_section_size
  2325. (bfd *ibfd, asection *isec, bfd *obfd, bfd_size_type size);
  2326. DESCRIPTION
  2327. Convert the size @var{size} of the section @var{isec} in input
  2328. BFD @var{ibfd} to the section size in output BFD @var{obfd}.
  2329. */
  2330. bfd_size_type
  2331. bfd_convert_section_size (bfd *ibfd, sec_ptr isec, bfd *obfd,
  2332. bfd_size_type size)
  2333. {
  2334. bfd_size_type hdr_size;
  2335. /* Do nothing if either input or output aren't ELF. */
  2336. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  2337. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  2338. return size;
  2339. /* Do nothing if ELF classes of input and output are the same. */
  2340. if (get_elf_backend_data (ibfd)->s->elfclass
  2341. == get_elf_backend_data (obfd)->s->elfclass)
  2342. return size;
  2343. /* Convert GNU property size. */
  2344. if (startswith (isec->name, NOTE_GNU_PROPERTY_SECTION_NAME))
  2345. return _bfd_elf_convert_gnu_property_size (ibfd, obfd);
  2346. /* Do nothing if input file will be decompressed. */
  2347. if ((ibfd->flags & BFD_DECOMPRESS))
  2348. return size;
  2349. /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
  2350. hdr_size = bfd_get_compression_header_size (ibfd, isec);
  2351. if (hdr_size == 0)
  2352. return size;
  2353. /* Adjust the size of the output SHF_COMPRESSED section. */
  2354. if (hdr_size == sizeof (Elf32_External_Chdr))
  2355. return (size - sizeof (Elf32_External_Chdr)
  2356. + sizeof (Elf64_External_Chdr));
  2357. else
  2358. return (size - sizeof (Elf64_External_Chdr)
  2359. + sizeof (Elf32_External_Chdr));
  2360. }
  2361. /*
  2362. FUNCTION
  2363. bfd_convert_section_contents
  2364. SYNOPSIS
  2365. bool bfd_convert_section_contents
  2366. (bfd *ibfd, asection *isec, bfd *obfd,
  2367. bfd_byte **ptr, bfd_size_type *ptr_size);
  2368. DESCRIPTION
  2369. Convert the contents, stored in @var{*ptr}, of the section
  2370. @var{isec} in input BFD @var{ibfd} to output BFD @var{obfd}
  2371. if needed. The original buffer pointed to by @var{*ptr} may
  2372. be freed and @var{*ptr} is returned with memory malloc'd by this
  2373. function, and the new size written to @var{ptr_size}.
  2374. */
  2375. bool
  2376. bfd_convert_section_contents (bfd *ibfd, sec_ptr isec, bfd *obfd,
  2377. bfd_byte **ptr, bfd_size_type *ptr_size)
  2378. {
  2379. bfd_byte *contents;
  2380. bfd_size_type ihdr_size, ohdr_size, size;
  2381. Elf_Internal_Chdr chdr;
  2382. bool use_memmove;
  2383. /* Do nothing if either input or output aren't ELF. */
  2384. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  2385. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  2386. return true;
  2387. /* Do nothing if ELF classes of input and output are the same. */
  2388. if (get_elf_backend_data (ibfd)->s->elfclass
  2389. == get_elf_backend_data (obfd)->s->elfclass)
  2390. return true;
  2391. /* Convert GNU properties. */
  2392. if (startswith (isec->name, NOTE_GNU_PROPERTY_SECTION_NAME))
  2393. return _bfd_elf_convert_gnu_properties (ibfd, isec, obfd, ptr,
  2394. ptr_size);
  2395. /* Do nothing if input file will be decompressed. */
  2396. if ((ibfd->flags & BFD_DECOMPRESS))
  2397. return true;
  2398. /* Do nothing if the input section isn't a SHF_COMPRESSED section. */
  2399. ihdr_size = bfd_get_compression_header_size (ibfd, isec);
  2400. if (ihdr_size == 0)
  2401. return true;
  2402. /* PR 25221. Check for corrupt input sections. */
  2403. if (ihdr_size > bfd_get_section_limit (ibfd, isec))
  2404. /* FIXME: Issue a warning about a corrupt
  2405. compression header size field ? */
  2406. return false;
  2407. contents = *ptr;
  2408. /* Convert the contents of the input SHF_COMPRESSED section to
  2409. output. Get the input compression header and the size of the
  2410. output compression header. */
  2411. if (ihdr_size == sizeof (Elf32_External_Chdr))
  2412. {
  2413. Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
  2414. chdr.ch_type = bfd_get_32 (ibfd, &echdr->ch_type);
  2415. chdr.ch_size = bfd_get_32 (ibfd, &echdr->ch_size);
  2416. chdr.ch_addralign = bfd_get_32 (ibfd, &echdr->ch_addralign);
  2417. ohdr_size = sizeof (Elf64_External_Chdr);
  2418. use_memmove = false;
  2419. }
  2420. else if (ihdr_size != sizeof (Elf64_External_Chdr))
  2421. {
  2422. /* FIXME: Issue a warning about a corrupt
  2423. compression header size field ? */
  2424. return false;
  2425. }
  2426. else
  2427. {
  2428. Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
  2429. chdr.ch_type = bfd_get_32 (ibfd, &echdr->ch_type);
  2430. chdr.ch_size = bfd_get_64 (ibfd, &echdr->ch_size);
  2431. chdr.ch_addralign = bfd_get_64 (ibfd, &echdr->ch_addralign);
  2432. ohdr_size = sizeof (Elf32_External_Chdr);
  2433. use_memmove = true;
  2434. }
  2435. size = bfd_section_size (isec) - ihdr_size + ohdr_size;
  2436. if (!use_memmove)
  2437. {
  2438. contents = (bfd_byte *) bfd_malloc (size);
  2439. if (contents == NULL)
  2440. return false;
  2441. }
  2442. /* Write out the output compression header. */
  2443. if (ohdr_size == sizeof (Elf32_External_Chdr))
  2444. {
  2445. Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) contents;
  2446. bfd_put_32 (obfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
  2447. bfd_put_32 (obfd, chdr.ch_size, &echdr->ch_size);
  2448. bfd_put_32 (obfd, chdr.ch_addralign, &echdr->ch_addralign);
  2449. }
  2450. else
  2451. {
  2452. Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) contents;
  2453. bfd_put_32 (obfd, ELFCOMPRESS_ZLIB, &echdr->ch_type);
  2454. bfd_put_32 (obfd, 0, &echdr->ch_reserved);
  2455. bfd_put_64 (obfd, chdr.ch_size, &echdr->ch_size);
  2456. bfd_put_64 (obfd, chdr.ch_addralign, &echdr->ch_addralign);
  2457. }
  2458. /* Copy the compressed contents. */
  2459. if (use_memmove)
  2460. memmove (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
  2461. else
  2462. {
  2463. memcpy (contents + ohdr_size, *ptr + ihdr_size, size - ohdr_size);
  2464. free (*ptr);
  2465. *ptr = contents;
  2466. }
  2467. *ptr_size = size;
  2468. return true;
  2469. }
  2470. /* Get the linker information. */
  2471. struct bfd_link_info *
  2472. _bfd_get_link_info (bfd *abfd)
  2473. {
  2474. if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  2475. return NULL;
  2476. return elf_link_info (abfd);
  2477. }