linker.c 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574
  1. /* linker.c -- BFD linker routines
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. Written by Steve Chamberlain and Ian Lance Taylor, 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. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "bfdlink.h"
  21. #include "genlink.h"
  22. /*
  23. SECTION
  24. Linker Functions
  25. @cindex Linker
  26. The linker uses three special entry points in the BFD target
  27. vector. It is not necessary to write special routines for
  28. these entry points when creating a new BFD back end, since
  29. generic versions are provided. However, writing them can
  30. speed up linking and make it use significantly less runtime
  31. memory.
  32. The first routine creates a hash table used by the other
  33. routines. The second routine adds the symbols from an object
  34. file to the hash table. The third routine takes all the
  35. object files and links them together to create the output
  36. file. These routines are designed so that the linker proper
  37. does not need to know anything about the symbols in the object
  38. files that it is linking. The linker merely arranges the
  39. sections as directed by the linker script and lets BFD handle
  40. the details of symbols and relocs.
  41. The second routine and third routines are passed a pointer to
  42. a <<struct bfd_link_info>> structure (defined in
  43. <<bfdlink.h>>) which holds information relevant to the link,
  44. including the linker hash table (which was created by the
  45. first routine) and a set of callback functions to the linker
  46. proper.
  47. The generic linker routines are in <<linker.c>>, and use the
  48. header file <<genlink.h>>. As of this writing, the only back
  49. ends which have implemented versions of these routines are
  50. a.out (in <<aoutx.h>>) and ECOFF (in <<ecoff.c>>). The a.out
  51. routines are used as examples throughout this section.
  52. @menu
  53. @* Creating a Linker Hash Table::
  54. @* Adding Symbols to the Hash Table::
  55. @* Performing the Final Link::
  56. @end menu
  57. INODE
  58. Creating a Linker Hash Table, Adding Symbols to the Hash Table, Linker Functions, Linker Functions
  59. SUBSECTION
  60. Creating a linker hash table
  61. @cindex _bfd_link_hash_table_create in target vector
  62. @cindex target vector (_bfd_link_hash_table_create)
  63. The linker routines must create a hash table, which must be
  64. derived from <<struct bfd_link_hash_table>> described in
  65. <<bfdlink.c>>. @xref{Hash Tables}, for information on how to
  66. create a derived hash table. This entry point is called using
  67. the target vector of the linker output file.
  68. The <<_bfd_link_hash_table_create>> entry point must allocate
  69. and initialize an instance of the desired hash table. If the
  70. back end does not require any additional information to be
  71. stored with the entries in the hash table, the entry point may
  72. simply create a <<struct bfd_link_hash_table>>. Most likely,
  73. however, some additional information will be needed.
  74. For example, with each entry in the hash table the a.out
  75. linker keeps the index the symbol has in the final output file
  76. (this index number is used so that when doing a relocatable
  77. link the symbol index used in the output file can be quickly
  78. filled in when copying over a reloc). The a.out linker code
  79. defines the required structures and functions for a hash table
  80. derived from <<struct bfd_link_hash_table>>. The a.out linker
  81. hash table is created by the function
  82. <<NAME(aout,link_hash_table_create)>>; it simply allocates
  83. space for the hash table, initializes it, and returns a
  84. pointer to it.
  85. When writing the linker routines for a new back end, you will
  86. generally not know exactly which fields will be required until
  87. you have finished. You should simply create a new hash table
  88. which defines no additional fields, and then simply add fields
  89. as they become necessary.
  90. INODE
  91. Adding Symbols to the Hash Table, Performing the Final Link, Creating a Linker Hash Table, Linker Functions
  92. SUBSECTION
  93. Adding symbols to the hash table
  94. @cindex _bfd_link_add_symbols in target vector
  95. @cindex target vector (_bfd_link_add_symbols)
  96. The linker proper will call the <<_bfd_link_add_symbols>>
  97. entry point for each object file or archive which is to be
  98. linked (typically these are the files named on the command
  99. line, but some may also come from the linker script). The
  100. entry point is responsible for examining the file. For an
  101. object file, BFD must add any relevant symbol information to
  102. the hash table. For an archive, BFD must determine which
  103. elements of the archive should be used and adding them to the
  104. link.
  105. The a.out version of this entry point is
  106. <<NAME(aout,link_add_symbols)>>.
  107. @menu
  108. @* Differing file formats::
  109. @* Adding symbols from an object file::
  110. @* Adding symbols from an archive::
  111. @end menu
  112. INODE
  113. Differing file formats, Adding symbols from an object file, Adding Symbols to the Hash Table, Adding Symbols to the Hash Table
  114. SUBSUBSECTION
  115. Differing file formats
  116. Normally all the files involved in a link will be of the same
  117. format, but it is also possible to link together different
  118. format object files, and the back end must support that. The
  119. <<_bfd_link_add_symbols>> entry point is called via the target
  120. vector of the file to be added. This has an important
  121. consequence: the function may not assume that the hash table
  122. is the type created by the corresponding
  123. <<_bfd_link_hash_table_create>> vector. All the
  124. <<_bfd_link_add_symbols>> function can assume about the hash
  125. table is that it is derived from <<struct
  126. bfd_link_hash_table>>.
  127. Sometimes the <<_bfd_link_add_symbols>> function must store
  128. some information in the hash table entry to be used by the
  129. <<_bfd_final_link>> function. In such a case the output bfd
  130. xvec must be checked to make sure that the hash table was
  131. created by an object file of the same format.
  132. The <<_bfd_final_link>> routine must be prepared to handle a
  133. hash entry without any extra information added by the
  134. <<_bfd_link_add_symbols>> function. A hash entry without
  135. extra information will also occur when the linker script
  136. directs the linker to create a symbol. Note that, regardless
  137. of how a hash table entry is added, all the fields will be
  138. initialized to some sort of null value by the hash table entry
  139. initialization function.
  140. See <<ecoff_link_add_externals>> for an example of how to
  141. check the output bfd before saving information (in this
  142. case, the ECOFF external symbol debugging information) in a
  143. hash table entry.
  144. INODE
  145. Adding symbols from an object file, Adding symbols from an archive, Differing file formats, Adding Symbols to the Hash Table
  146. SUBSUBSECTION
  147. Adding symbols from an object file
  148. When the <<_bfd_link_add_symbols>> routine is passed an object
  149. file, it must add all externally visible symbols in that
  150. object file to the hash table. The actual work of adding the
  151. symbol to the hash table is normally handled by the function
  152. <<_bfd_generic_link_add_one_symbol>>. The
  153. <<_bfd_link_add_symbols>> routine is responsible for reading
  154. all the symbols from the object file and passing the correct
  155. information to <<_bfd_generic_link_add_one_symbol>>.
  156. The <<_bfd_link_add_symbols>> routine should not use
  157. <<bfd_canonicalize_symtab>> to read the symbols. The point of
  158. providing this routine is to avoid the overhead of converting
  159. the symbols into generic <<asymbol>> structures.
  160. @findex _bfd_generic_link_add_one_symbol
  161. <<_bfd_generic_link_add_one_symbol>> handles the details of
  162. combining common symbols, warning about multiple definitions,
  163. and so forth. It takes arguments which describe the symbol to
  164. add, notably symbol flags, a section, and an offset. The
  165. symbol flags include such things as <<BSF_WEAK>> or
  166. <<BSF_INDIRECT>>. The section is a section in the object
  167. file, or something like <<bfd_und_section_ptr>> for an undefined
  168. symbol or <<bfd_com_section_ptr>> for a common symbol.
  169. If the <<_bfd_final_link>> routine is also going to need to
  170. read the symbol information, the <<_bfd_link_add_symbols>>
  171. routine should save it somewhere attached to the object file
  172. BFD. However, the information should only be saved if the
  173. <<keep_memory>> field of the <<info>> argument is TRUE, so
  174. that the <<-no-keep-memory>> linker switch is effective.
  175. The a.out function which adds symbols from an object file is
  176. <<aout_link_add_object_symbols>>, and most of the interesting
  177. work is in <<aout_link_add_symbols>>. The latter saves
  178. pointers to the hash tables entries created by
  179. <<_bfd_generic_link_add_one_symbol>> indexed by symbol number,
  180. so that the <<_bfd_final_link>> routine does not have to call
  181. the hash table lookup routine to locate the entry.
  182. INODE
  183. Adding symbols from an archive, , Adding symbols from an object file, Adding Symbols to the Hash Table
  184. SUBSUBSECTION
  185. Adding symbols from an archive
  186. When the <<_bfd_link_add_symbols>> routine is passed an
  187. archive, it must look through the symbols defined by the
  188. archive and decide which elements of the archive should be
  189. included in the link. For each such element it must call the
  190. <<add_archive_element>> linker callback, and it must add the
  191. symbols from the object file to the linker hash table. (The
  192. callback may in fact indicate that a replacement BFD should be
  193. used, in which case the symbols from that BFD should be added
  194. to the linker hash table instead.)
  195. @findex _bfd_generic_link_add_archive_symbols
  196. In most cases the work of looking through the symbols in the
  197. archive should be done by the
  198. <<_bfd_generic_link_add_archive_symbols>> function.
  199. <<_bfd_generic_link_add_archive_symbols>> is passed a function
  200. to call to make the final decision about adding an archive
  201. element to the link and to do the actual work of adding the
  202. symbols to the linker hash table. If the element is to
  203. be included, the <<add_archive_element>> linker callback
  204. routine must be called with the element as an argument, and
  205. the element's symbols must be added to the linker hash table
  206. just as though the element had itself been passed to the
  207. <<_bfd_link_add_symbols>> function.
  208. When the a.out <<_bfd_link_add_symbols>> function receives an
  209. archive, it calls <<_bfd_generic_link_add_archive_symbols>>
  210. passing <<aout_link_check_archive_element>> as the function
  211. argument. <<aout_link_check_archive_element>> calls
  212. <<aout_link_check_ar_symbols>>. If the latter decides to add
  213. the element (an element is only added if it provides a real,
  214. non-common, definition for a previously undefined or common
  215. symbol) it calls the <<add_archive_element>> callback and then
  216. <<aout_link_check_archive_element>> calls
  217. <<aout_link_add_symbols>> to actually add the symbols to the
  218. linker hash table - possibly those of a substitute BFD, if the
  219. <<add_archive_element>> callback avails itself of that option.
  220. The ECOFF back end is unusual in that it does not normally
  221. call <<_bfd_generic_link_add_archive_symbols>>, because ECOFF
  222. archives already contain a hash table of symbols. The ECOFF
  223. back end searches the archive itself to avoid the overhead of
  224. creating a new hash table.
  225. INODE
  226. Performing the Final Link, , Adding Symbols to the Hash Table, Linker Functions
  227. SUBSECTION
  228. Performing the final link
  229. @cindex _bfd_link_final_link in target vector
  230. @cindex target vector (_bfd_final_link)
  231. When all the input files have been processed, the linker calls
  232. the <<_bfd_final_link>> entry point of the output BFD. This
  233. routine is responsible for producing the final output file,
  234. which has several aspects. It must relocate the contents of
  235. the input sections and copy the data into the output sections.
  236. It must build an output symbol table including any local
  237. symbols from the input files and the global symbols from the
  238. hash table. When producing relocatable output, it must
  239. modify the input relocs and write them into the output file.
  240. There may also be object format dependent work to be done.
  241. The linker will also call the <<write_object_contents>> entry
  242. point when the BFD is closed. The two entry points must work
  243. together in order to produce the correct output file.
  244. The details of how this works are inevitably dependent upon
  245. the specific object file format. The a.out
  246. <<_bfd_final_link>> routine is <<NAME(aout,final_link)>>.
  247. @menu
  248. @* Information provided by the linker::
  249. @* Relocating the section contents::
  250. @* Writing the symbol table::
  251. @end menu
  252. INODE
  253. Information provided by the linker, Relocating the section contents, Performing the Final Link, Performing the Final Link
  254. SUBSUBSECTION
  255. Information provided by the linker
  256. Before the linker calls the <<_bfd_final_link>> entry point,
  257. it sets up some data structures for the function to use.
  258. The <<input_bfds>> field of the <<bfd_link_info>> structure
  259. will point to a list of all the input files included in the
  260. link. These files are linked through the <<link.next>> field
  261. of the <<bfd>> structure.
  262. Each section in the output file will have a list of
  263. <<link_order>> structures attached to the <<map_head.link_order>>
  264. field (the <<link_order>> structure is defined in
  265. <<bfdlink.h>>). These structures describe how to create the
  266. contents of the output section in terms of the contents of
  267. various input sections, fill constants, and, eventually, other
  268. types of information. They also describe relocs that must be
  269. created by the BFD backend, but do not correspond to any input
  270. file; this is used to support -Ur, which builds constructors
  271. while generating a relocatable object file.
  272. INODE
  273. Relocating the section contents, Writing the symbol table, Information provided by the linker, Performing the Final Link
  274. SUBSUBSECTION
  275. Relocating the section contents
  276. The <<_bfd_final_link>> function should look through the
  277. <<link_order>> structures attached to each section of the
  278. output file. Each <<link_order>> structure should either be
  279. handled specially, or it should be passed to the function
  280. <<_bfd_default_link_order>> which will do the right thing
  281. (<<_bfd_default_link_order>> is defined in <<linker.c>>).
  282. For efficiency, a <<link_order>> of type
  283. <<bfd_indirect_link_order>> whose associated section belongs
  284. to a BFD of the same format as the output BFD must be handled
  285. specially. This type of <<link_order>> describes part of an
  286. output section in terms of a section belonging to one of the
  287. input files. The <<_bfd_final_link>> function should read the
  288. contents of the section and any associated relocs, apply the
  289. relocs to the section contents, and write out the modified
  290. section contents. If performing a relocatable link, the
  291. relocs themselves must also be modified and written out.
  292. @findex _bfd_relocate_contents
  293. @findex _bfd_final_link_relocate
  294. The functions <<_bfd_relocate_contents>> and
  295. <<_bfd_final_link_relocate>> provide some general support for
  296. performing the actual relocations, notably overflow checking.
  297. Their arguments include information about the symbol the
  298. relocation is against and a <<reloc_howto_type>> argument
  299. which describes the relocation to perform. These functions
  300. are defined in <<reloc.c>>.
  301. The a.out function which handles reading, relocating, and
  302. writing section contents is <<aout_link_input_section>>. The
  303. actual relocation is done in <<aout_link_input_section_std>>
  304. and <<aout_link_input_section_ext>>.
  305. INODE
  306. Writing the symbol table, , Relocating the section contents, Performing the Final Link
  307. SUBSUBSECTION
  308. Writing the symbol table
  309. The <<_bfd_final_link>> function must gather all the symbols
  310. in the input files and write them out. It must also write out
  311. all the symbols in the global hash table. This must be
  312. controlled by the <<strip>> and <<discard>> fields of the
  313. <<bfd_link_info>> structure.
  314. The local symbols of the input files will not have been
  315. entered into the linker hash table. The <<_bfd_final_link>>
  316. routine must consider each input file and include the symbols
  317. in the output file. It may be convenient to do this when
  318. looking through the <<link_order>> structures, or it may be
  319. done by stepping through the <<input_bfds>> list.
  320. The <<_bfd_final_link>> routine must also traverse the global
  321. hash table to gather all the externally visible symbols. It
  322. is possible that most of the externally visible symbols may be
  323. written out when considering the symbols of each input file,
  324. but it is still necessary to traverse the hash table since the
  325. linker script may have defined some symbols that are not in
  326. any of the input files.
  327. The <<strip>> field of the <<bfd_link_info>> structure
  328. controls which symbols are written out. The possible values
  329. are listed in <<bfdlink.h>>. If the value is <<strip_some>>,
  330. then the <<keep_hash>> field of the <<bfd_link_info>>
  331. structure is a hash table of symbols to keep; each symbol
  332. should be looked up in this hash table, and only symbols which
  333. are present should be included in the output file.
  334. If the <<strip>> field of the <<bfd_link_info>> structure
  335. permits local symbols to be written out, the <<discard>> field
  336. is used to further controls which local symbols are included
  337. in the output file. If the value is <<discard_l>>, then all
  338. local symbols which begin with a certain prefix are discarded;
  339. this is controlled by the <<bfd_is_local_label_name>> entry point.
  340. The a.out backend handles symbols by calling
  341. <<aout_link_write_symbols>> on each input BFD and then
  342. traversing the global hash table with the function
  343. <<aout_link_write_other_symbol>>. It builds a string table
  344. while writing out the symbols, which is written to the output
  345. file at the end of <<NAME(aout,final_link)>>.
  346. */
  347. static bool generic_link_add_object_symbols
  348. (bfd *, struct bfd_link_info *);
  349. static bool generic_link_check_archive_element
  350. (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
  351. bool *);
  352. static bool generic_link_add_symbol_list
  353. (bfd *, struct bfd_link_info *, bfd_size_type count, asymbol **);
  354. static bool generic_add_output_symbol
  355. (bfd *, size_t *psymalloc, asymbol *);
  356. static bool default_data_link_order
  357. (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *);
  358. static bool default_indirect_link_order
  359. (bfd *, struct bfd_link_info *, asection *, struct bfd_link_order *,
  360. bool);
  361. /* The link hash table structure is defined in bfdlink.h. It provides
  362. a base hash table which the backend specific hash tables are built
  363. upon. */
  364. /* Routine to create an entry in the link hash table. */
  365. struct bfd_hash_entry *
  366. _bfd_link_hash_newfunc (struct bfd_hash_entry *entry,
  367. struct bfd_hash_table *table,
  368. const char *string)
  369. {
  370. /* Allocate the structure if it has not already been allocated by a
  371. subclass. */
  372. if (entry == NULL)
  373. {
  374. entry = (struct bfd_hash_entry *)
  375. bfd_hash_allocate (table, sizeof (struct bfd_link_hash_entry));
  376. if (entry == NULL)
  377. return entry;
  378. }
  379. /* Call the allocation method of the superclass. */
  380. entry = bfd_hash_newfunc (entry, table, string);
  381. if (entry)
  382. {
  383. struct bfd_link_hash_entry *h = (struct bfd_link_hash_entry *) entry;
  384. /* Initialize the local fields. */
  385. memset ((char *) &h->root + sizeof (h->root), 0,
  386. sizeof (*h) - sizeof (h->root));
  387. }
  388. return entry;
  389. }
  390. /* Initialize a link hash table. The BFD argument is the one
  391. responsible for creating this table. */
  392. bool
  393. _bfd_link_hash_table_init
  394. (struct bfd_link_hash_table *table,
  395. bfd *abfd ATTRIBUTE_UNUSED,
  396. struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
  397. struct bfd_hash_table *,
  398. const char *),
  399. unsigned int entsize)
  400. {
  401. bool ret;
  402. BFD_ASSERT (!abfd->is_linker_output && !abfd->link.hash);
  403. table->undefs = NULL;
  404. table->undefs_tail = NULL;
  405. table->type = bfd_link_generic_hash_table;
  406. ret = bfd_hash_table_init (&table->table, newfunc, entsize);
  407. if (ret)
  408. {
  409. /* Arrange for destruction of this hash table on closing ABFD. */
  410. table->hash_table_free = _bfd_generic_link_hash_table_free;
  411. abfd->link.hash = table;
  412. abfd->is_linker_output = true;
  413. }
  414. return ret;
  415. }
  416. /* Look up a symbol in a link hash table. If follow is TRUE, we
  417. follow bfd_link_hash_indirect and bfd_link_hash_warning links to
  418. the real symbol.
  419. .{* Return TRUE if the symbol described by a linker hash entry H
  420. . is going to be absolute. Linker-script defined symbols can be
  421. . converted from absolute to section-relative ones late in the
  422. . link. Use this macro to correctly determine whether the symbol
  423. . will actually end up absolute in output. *}
  424. .#define bfd_is_abs_symbol(H) \
  425. . (((H)->type == bfd_link_hash_defined \
  426. . || (H)->type == bfd_link_hash_defweak) \
  427. . && bfd_is_abs_section ((H)->u.def.section) \
  428. . && !(H)->rel_from_abs)
  429. .
  430. */
  431. struct bfd_link_hash_entry *
  432. bfd_link_hash_lookup (struct bfd_link_hash_table *table,
  433. const char *string,
  434. bool create,
  435. bool copy,
  436. bool follow)
  437. {
  438. struct bfd_link_hash_entry *ret;
  439. if (table == NULL || string == NULL)
  440. return NULL;
  441. ret = ((struct bfd_link_hash_entry *)
  442. bfd_hash_lookup (&table->table, string, create, copy));
  443. if (follow && ret != NULL)
  444. {
  445. while (ret->type == bfd_link_hash_indirect
  446. || ret->type == bfd_link_hash_warning)
  447. ret = ret->u.i.link;
  448. }
  449. return ret;
  450. }
  451. /* Look up a symbol in the main linker hash table if the symbol might
  452. be wrapped. This should only be used for references to an
  453. undefined symbol, not for definitions of a symbol. */
  454. struct bfd_link_hash_entry *
  455. bfd_wrapped_link_hash_lookup (bfd *abfd,
  456. struct bfd_link_info *info,
  457. const char *string,
  458. bool create,
  459. bool copy,
  460. bool follow)
  461. {
  462. size_t amt;
  463. if (info->wrap_hash != NULL)
  464. {
  465. const char *l;
  466. char prefix = '\0';
  467. l = string;
  468. if (*l == bfd_get_symbol_leading_char (abfd) || *l == info->wrap_char)
  469. {
  470. prefix = *l;
  471. ++l;
  472. }
  473. #undef WRAP
  474. #define WRAP "__wrap_"
  475. if (bfd_hash_lookup (info->wrap_hash, l, false, false) != NULL)
  476. {
  477. char *n;
  478. struct bfd_link_hash_entry *h;
  479. /* This symbol is being wrapped. We want to replace all
  480. references to SYM with references to __wrap_SYM. */
  481. amt = strlen (l) + sizeof WRAP + 1;
  482. n = (char *) bfd_malloc (amt);
  483. if (n == NULL)
  484. return NULL;
  485. n[0] = prefix;
  486. n[1] = '\0';
  487. strcat (n, WRAP);
  488. strcat (n, l);
  489. h = bfd_link_hash_lookup (info->hash, n, create, true, follow);
  490. free (n);
  491. return h;
  492. }
  493. #undef REAL
  494. #define REAL "__real_"
  495. if (*l == '_'
  496. && startswith (l, REAL)
  497. && bfd_hash_lookup (info->wrap_hash, l + sizeof REAL - 1,
  498. false, false) != NULL)
  499. {
  500. char *n;
  501. struct bfd_link_hash_entry *h;
  502. /* This is a reference to __real_SYM, where SYM is being
  503. wrapped. We want to replace all references to __real_SYM
  504. with references to SYM. */
  505. amt = strlen (l + sizeof REAL - 1) + 2;
  506. n = (char *) bfd_malloc (amt);
  507. if (n == NULL)
  508. return NULL;
  509. n[0] = prefix;
  510. n[1] = '\0';
  511. strcat (n, l + sizeof REAL - 1);
  512. h = bfd_link_hash_lookup (info->hash, n, create, true, follow);
  513. free (n);
  514. return h;
  515. }
  516. #undef REAL
  517. }
  518. return bfd_link_hash_lookup (info->hash, string, create, copy, follow);
  519. }
  520. /* If H is a wrapped symbol, ie. the symbol name starts with "__wrap_"
  521. and the remainder is found in wrap_hash, return the real symbol. */
  522. struct bfd_link_hash_entry *
  523. unwrap_hash_lookup (struct bfd_link_info *info,
  524. bfd *input_bfd,
  525. struct bfd_link_hash_entry *h)
  526. {
  527. const char *l = h->root.string;
  528. if (*l == bfd_get_symbol_leading_char (input_bfd)
  529. || *l == info->wrap_char)
  530. ++l;
  531. if (startswith (l, WRAP))
  532. {
  533. l += sizeof WRAP - 1;
  534. if (bfd_hash_lookup (info->wrap_hash, l, false, false) != NULL)
  535. {
  536. char save = 0;
  537. if (l - (sizeof WRAP - 1) != h->root.string)
  538. {
  539. --l;
  540. save = *l;
  541. *(char *) l = *h->root.string;
  542. }
  543. h = bfd_link_hash_lookup (info->hash, l, false, false, false);
  544. if (save)
  545. *(char *) l = save;
  546. }
  547. }
  548. return h;
  549. }
  550. #undef WRAP
  551. /* Traverse a generic link hash table. Differs from bfd_hash_traverse
  552. in the treatment of warning symbols. When warning symbols are
  553. created they replace the real symbol, so you don't get to see the
  554. real symbol in a bfd_hash_traverse. This traversal calls func with
  555. the real symbol. */
  556. void
  557. bfd_link_hash_traverse
  558. (struct bfd_link_hash_table *htab,
  559. bool (*func) (struct bfd_link_hash_entry *, void *),
  560. void *info)
  561. {
  562. unsigned int i;
  563. htab->table.frozen = 1;
  564. for (i = 0; i < htab->table.size; i++)
  565. {
  566. struct bfd_link_hash_entry *p;
  567. p = (struct bfd_link_hash_entry *) htab->table.table[i];
  568. for (; p != NULL; p = (struct bfd_link_hash_entry *) p->root.next)
  569. if (!(*func) (p->type == bfd_link_hash_warning ? p->u.i.link : p, info))
  570. goto out;
  571. }
  572. out:
  573. htab->table.frozen = 0;
  574. }
  575. /* Add a symbol to the linker hash table undefs list. */
  576. void
  577. bfd_link_add_undef (struct bfd_link_hash_table *table,
  578. struct bfd_link_hash_entry *h)
  579. {
  580. BFD_ASSERT (h->u.undef.next == NULL);
  581. if (table->undefs_tail != NULL)
  582. table->undefs_tail->u.undef.next = h;
  583. if (table->undefs == NULL)
  584. table->undefs = h;
  585. table->undefs_tail = h;
  586. }
  587. /* The undefs list was designed so that in normal use we don't need to
  588. remove entries. However, if symbols on the list are changed from
  589. bfd_link_hash_undefined to either bfd_link_hash_undefweak or
  590. bfd_link_hash_new for some reason, then they must be removed from the
  591. list. Failure to do so might result in the linker attempting to add
  592. the symbol to the list again at a later stage. */
  593. void
  594. bfd_link_repair_undef_list (struct bfd_link_hash_table *table)
  595. {
  596. struct bfd_link_hash_entry **pun;
  597. pun = &table->undefs;
  598. while (*pun != NULL)
  599. {
  600. struct bfd_link_hash_entry *h = *pun;
  601. if (h->type == bfd_link_hash_new
  602. || h->type == bfd_link_hash_undefweak)
  603. {
  604. *pun = h->u.undef.next;
  605. h->u.undef.next = NULL;
  606. if (h == table->undefs_tail)
  607. {
  608. if (pun == &table->undefs)
  609. table->undefs_tail = NULL;
  610. else
  611. /* pun points at an u.undef.next field. Go back to
  612. the start of the link_hash_entry. */
  613. table->undefs_tail = (struct bfd_link_hash_entry *)
  614. ((char *) pun - ((char *) &h->u.undef.next - (char *) h));
  615. break;
  616. }
  617. }
  618. else
  619. pun = &h->u.undef.next;
  620. }
  621. }
  622. /* Routine to create an entry in a generic link hash table. */
  623. struct bfd_hash_entry *
  624. _bfd_generic_link_hash_newfunc (struct bfd_hash_entry *entry,
  625. struct bfd_hash_table *table,
  626. const char *string)
  627. {
  628. /* Allocate the structure if it has not already been allocated by a
  629. subclass. */
  630. if (entry == NULL)
  631. {
  632. entry = (struct bfd_hash_entry *)
  633. bfd_hash_allocate (table, sizeof (struct generic_link_hash_entry));
  634. if (entry == NULL)
  635. return entry;
  636. }
  637. /* Call the allocation method of the superclass. */
  638. entry = _bfd_link_hash_newfunc (entry, table, string);
  639. if (entry)
  640. {
  641. struct generic_link_hash_entry *ret;
  642. /* Set local fields. */
  643. ret = (struct generic_link_hash_entry *) entry;
  644. ret->written = false;
  645. ret->sym = NULL;
  646. }
  647. return entry;
  648. }
  649. /* Create a generic link hash table. */
  650. struct bfd_link_hash_table *
  651. _bfd_generic_link_hash_table_create (bfd *abfd)
  652. {
  653. struct generic_link_hash_table *ret;
  654. size_t amt = sizeof (struct generic_link_hash_table);
  655. ret = (struct generic_link_hash_table *) bfd_malloc (amt);
  656. if (ret == NULL)
  657. return NULL;
  658. if (! _bfd_link_hash_table_init (&ret->root, abfd,
  659. _bfd_generic_link_hash_newfunc,
  660. sizeof (struct generic_link_hash_entry)))
  661. {
  662. free (ret);
  663. return NULL;
  664. }
  665. return &ret->root;
  666. }
  667. void
  668. _bfd_generic_link_hash_table_free (bfd *obfd)
  669. {
  670. struct generic_link_hash_table *ret;
  671. BFD_ASSERT (obfd->is_linker_output && obfd->link.hash);
  672. ret = (struct generic_link_hash_table *) obfd->link.hash;
  673. bfd_hash_table_free (&ret->root.table);
  674. free (ret);
  675. obfd->link.hash = NULL;
  676. obfd->is_linker_output = false;
  677. }
  678. /* Grab the symbols for an object file when doing a generic link. We
  679. store the symbols in the outsymbols field. We need to keep them
  680. around for the entire link to ensure that we only read them once.
  681. If we read them multiple times, we might wind up with relocs and
  682. the hash table pointing to different instances of the symbol
  683. structure. */
  684. bool
  685. bfd_generic_link_read_symbols (bfd *abfd)
  686. {
  687. if (bfd_get_outsymbols (abfd) == NULL)
  688. {
  689. long symsize;
  690. long symcount;
  691. symsize = bfd_get_symtab_upper_bound (abfd);
  692. if (symsize < 0)
  693. return false;
  694. abfd->outsymbols = bfd_alloc (abfd, symsize);
  695. if (bfd_get_outsymbols (abfd) == NULL && symsize != 0)
  696. return false;
  697. symcount = bfd_canonicalize_symtab (abfd, bfd_get_outsymbols (abfd));
  698. if (symcount < 0)
  699. return false;
  700. abfd->symcount = symcount;
  701. }
  702. return true;
  703. }
  704. /* Indicate that we are only retrieving symbol values from this
  705. section. We want the symbols to act as though the values in the
  706. file are absolute. */
  707. void
  708. _bfd_generic_link_just_syms (asection *sec,
  709. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  710. {
  711. sec->sec_info_type = SEC_INFO_TYPE_JUST_SYMS;
  712. sec->output_section = bfd_abs_section_ptr;
  713. sec->output_offset = sec->vma;
  714. }
  715. /* Copy the symbol type and other attributes for a linker script
  716. assignment from HSRC to HDEST.
  717. The default implementation does nothing. */
  718. void
  719. _bfd_generic_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED,
  720. struct bfd_link_hash_entry *hdest ATTRIBUTE_UNUSED,
  721. struct bfd_link_hash_entry *hsrc ATTRIBUTE_UNUSED)
  722. {
  723. }
  724. /* Generic function to add symbols from an object file to the
  725. global hash table. */
  726. bool
  727. _bfd_generic_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
  728. {
  729. bool ret;
  730. switch (bfd_get_format (abfd))
  731. {
  732. case bfd_object:
  733. ret = generic_link_add_object_symbols (abfd, info);
  734. break;
  735. case bfd_archive:
  736. ret = (_bfd_generic_link_add_archive_symbols
  737. (abfd, info, generic_link_check_archive_element));
  738. break;
  739. default:
  740. bfd_set_error (bfd_error_wrong_format);
  741. ret = false;
  742. }
  743. return ret;
  744. }
  745. /* Add symbols from an object file to the global hash table. */
  746. static bool
  747. generic_link_add_object_symbols (bfd *abfd,
  748. struct bfd_link_info *info)
  749. {
  750. bfd_size_type symcount;
  751. struct bfd_symbol **outsyms;
  752. if (!bfd_generic_link_read_symbols (abfd))
  753. return false;
  754. symcount = _bfd_generic_link_get_symcount (abfd);
  755. outsyms = _bfd_generic_link_get_symbols (abfd);
  756. return generic_link_add_symbol_list (abfd, info, symcount, outsyms);
  757. }
  758. /* Generic function to add symbols from an archive file to the global
  759. hash file. This function presumes that the archive symbol table
  760. has already been read in (this is normally done by the
  761. bfd_check_format entry point). It looks through the archive symbol
  762. table for symbols that are undefined or common in the linker global
  763. symbol hash table. When one is found, the CHECKFN argument is used
  764. to see if an object file should be included. This allows targets
  765. to customize common symbol behaviour. CHECKFN should set *PNEEDED
  766. to TRUE if the object file should be included, and must also call
  767. the bfd_link_info add_archive_element callback function and handle
  768. adding the symbols to the global hash table. CHECKFN must notice
  769. if the callback indicates a substitute BFD, and arrange to add
  770. those symbols instead if it does so. CHECKFN should only return
  771. FALSE if some sort of error occurs. */
  772. bool
  773. _bfd_generic_link_add_archive_symbols
  774. (bfd *abfd,
  775. struct bfd_link_info *info,
  776. bool (*checkfn) (bfd *, struct bfd_link_info *,
  777. struct bfd_link_hash_entry *, const char *, bool *))
  778. {
  779. bool loop;
  780. bfd_size_type amt;
  781. unsigned char *included;
  782. if (! bfd_has_map (abfd))
  783. {
  784. /* An empty archive is a special case. */
  785. if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
  786. return true;
  787. bfd_set_error (bfd_error_no_armap);
  788. return false;
  789. }
  790. amt = bfd_ardata (abfd)->symdef_count;
  791. if (amt == 0)
  792. return true;
  793. amt *= sizeof (*included);
  794. included = (unsigned char *) bfd_zmalloc (amt);
  795. if (included == NULL)
  796. return false;
  797. do
  798. {
  799. carsym *arsyms;
  800. carsym *arsym_end;
  801. carsym *arsym;
  802. unsigned int indx;
  803. file_ptr last_ar_offset = -1;
  804. bool needed = false;
  805. bfd *element = NULL;
  806. loop = false;
  807. arsyms = bfd_ardata (abfd)->symdefs;
  808. arsym_end = arsyms + bfd_ardata (abfd)->symdef_count;
  809. for (arsym = arsyms, indx = 0; arsym < arsym_end; arsym++, indx++)
  810. {
  811. struct bfd_link_hash_entry *h;
  812. struct bfd_link_hash_entry *undefs_tail;
  813. if (included[indx])
  814. continue;
  815. if (needed && arsym->file_offset == last_ar_offset)
  816. {
  817. included[indx] = 1;
  818. continue;
  819. }
  820. if (arsym->name == NULL)
  821. goto error_return;
  822. h = bfd_link_hash_lookup (info->hash, arsym->name,
  823. false, false, true);
  824. if (h == NULL
  825. && info->pei386_auto_import
  826. && startswith (arsym->name, "__imp_"))
  827. h = bfd_link_hash_lookup (info->hash, arsym->name + 6,
  828. false, false, true);
  829. if (h == NULL)
  830. continue;
  831. if (h->type != bfd_link_hash_undefined
  832. && h->type != bfd_link_hash_common)
  833. {
  834. if (h->type != bfd_link_hash_undefweak)
  835. /* Symbol must be defined. Don't check it again. */
  836. included[indx] = 1;
  837. continue;
  838. }
  839. if (last_ar_offset != arsym->file_offset)
  840. {
  841. last_ar_offset = arsym->file_offset;
  842. element = _bfd_get_elt_at_filepos (abfd, last_ar_offset,
  843. info);
  844. if (element == NULL
  845. || !bfd_check_format (element, bfd_object))
  846. goto error_return;
  847. }
  848. undefs_tail = info->hash->undefs_tail;
  849. /* CHECKFN will see if this element should be included, and
  850. go ahead and include it if appropriate. */
  851. if (! (*checkfn) (element, info, h, arsym->name, &needed))
  852. goto error_return;
  853. if (needed)
  854. {
  855. unsigned int mark;
  856. /* Look backward to mark all symbols from this object file
  857. which we have already seen in this pass. */
  858. mark = indx;
  859. do
  860. {
  861. included[mark] = 1;
  862. if (mark == 0)
  863. break;
  864. --mark;
  865. }
  866. while (arsyms[mark].file_offset == last_ar_offset);
  867. if (undefs_tail != info->hash->undefs_tail)
  868. loop = true;
  869. }
  870. }
  871. } while (loop);
  872. free (included);
  873. return true;
  874. error_return:
  875. free (included);
  876. return false;
  877. }
  878. /* See if we should include an archive element. */
  879. static bool
  880. generic_link_check_archive_element (bfd *abfd,
  881. struct bfd_link_info *info,
  882. struct bfd_link_hash_entry *h,
  883. const char *name ATTRIBUTE_UNUSED,
  884. bool *pneeded)
  885. {
  886. asymbol **pp, **ppend;
  887. *pneeded = false;
  888. if (!bfd_generic_link_read_symbols (abfd))
  889. return false;
  890. pp = _bfd_generic_link_get_symbols (abfd);
  891. ppend = pp + _bfd_generic_link_get_symcount (abfd);
  892. for (; pp < ppend; pp++)
  893. {
  894. asymbol *p;
  895. p = *pp;
  896. /* We are only interested in globally visible symbols. */
  897. if (! bfd_is_com_section (p->section)
  898. && (p->flags & (BSF_GLOBAL | BSF_INDIRECT | BSF_WEAK)) == 0)
  899. continue;
  900. /* We are only interested if we know something about this
  901. symbol, and it is undefined or common. An undefined weak
  902. symbol (type bfd_link_hash_undefweak) is not considered to be
  903. a reference when pulling files out of an archive. See the
  904. SVR4 ABI, p. 4-27. */
  905. h = bfd_link_hash_lookup (info->hash, bfd_asymbol_name (p), false,
  906. false, true);
  907. if (h == NULL
  908. || (h->type != bfd_link_hash_undefined
  909. && h->type != bfd_link_hash_common))
  910. continue;
  911. /* P is a symbol we are looking for. */
  912. if (! bfd_is_com_section (p->section)
  913. || (h->type == bfd_link_hash_undefined
  914. && h->u.undef.abfd == NULL))
  915. {
  916. /* P is not a common symbol, or an undefined reference was
  917. created from outside BFD such as from a linker -u option.
  918. This object file defines the symbol, so pull it in. */
  919. *pneeded = true;
  920. if (!(*info->callbacks
  921. ->add_archive_element) (info, abfd, bfd_asymbol_name (p),
  922. &abfd))
  923. return false;
  924. /* Potentially, the add_archive_element hook may have set a
  925. substitute BFD for us. */
  926. return bfd_link_add_symbols (abfd, info);
  927. }
  928. /* P is a common symbol. */
  929. if (h->type == bfd_link_hash_undefined)
  930. {
  931. bfd *symbfd;
  932. bfd_vma size;
  933. unsigned int power;
  934. /* Turn the symbol into a common symbol but do not link in
  935. the object file. This is how a.out works. Object
  936. formats that require different semantics must implement
  937. this function differently. This symbol is already on the
  938. undefs list. We add the section to a common section
  939. attached to symbfd to ensure that it is in a BFD which
  940. will be linked in. */
  941. symbfd = h->u.undef.abfd;
  942. h->type = bfd_link_hash_common;
  943. h->u.c.p = (struct bfd_link_hash_common_entry *)
  944. bfd_hash_allocate (&info->hash->table,
  945. sizeof (struct bfd_link_hash_common_entry));
  946. if (h->u.c.p == NULL)
  947. return false;
  948. size = bfd_asymbol_value (p);
  949. h->u.c.size = size;
  950. power = bfd_log2 (size);
  951. if (power > 4)
  952. power = 4;
  953. h->u.c.p->alignment_power = power;
  954. if (p->section == bfd_com_section_ptr)
  955. h->u.c.p->section = bfd_make_section_old_way (symbfd, "COMMON");
  956. else
  957. h->u.c.p->section = bfd_make_section_old_way (symbfd,
  958. p->section->name);
  959. h->u.c.p->section->flags |= SEC_ALLOC;
  960. }
  961. else
  962. {
  963. /* Adjust the size of the common symbol if necessary. This
  964. is how a.out works. Object formats that require
  965. different semantics must implement this function
  966. differently. */
  967. if (bfd_asymbol_value (p) > h->u.c.size)
  968. h->u.c.size = bfd_asymbol_value (p);
  969. }
  970. }
  971. /* This archive element is not needed. */
  972. return true;
  973. }
  974. /* Add the symbols from an object file to the global hash table. ABFD
  975. is the object file. INFO is the linker information. SYMBOL_COUNT
  976. is the number of symbols. SYMBOLS is the list of symbols. */
  977. static bool
  978. generic_link_add_symbol_list (bfd *abfd,
  979. struct bfd_link_info *info,
  980. bfd_size_type symbol_count,
  981. asymbol **symbols)
  982. {
  983. asymbol **pp, **ppend;
  984. pp = symbols;
  985. ppend = symbols + symbol_count;
  986. for (; pp < ppend; pp++)
  987. {
  988. asymbol *p;
  989. p = *pp;
  990. if ((p->flags & (BSF_INDIRECT
  991. | BSF_WARNING
  992. | BSF_GLOBAL
  993. | BSF_CONSTRUCTOR
  994. | BSF_WEAK)) != 0
  995. || bfd_is_und_section (bfd_asymbol_section (p))
  996. || bfd_is_com_section (bfd_asymbol_section (p))
  997. || bfd_is_ind_section (bfd_asymbol_section (p)))
  998. {
  999. const char *name;
  1000. const char *string;
  1001. struct generic_link_hash_entry *h;
  1002. struct bfd_link_hash_entry *bh;
  1003. string = name = bfd_asymbol_name (p);
  1004. if (((p->flags & BSF_INDIRECT) != 0
  1005. || bfd_is_ind_section (p->section))
  1006. && pp + 1 < ppend)
  1007. {
  1008. pp++;
  1009. string = bfd_asymbol_name (*pp);
  1010. }
  1011. else if ((p->flags & BSF_WARNING) != 0
  1012. && pp + 1 < ppend)
  1013. {
  1014. /* The name of P is actually the warning string, and the
  1015. next symbol is the one to warn about. */
  1016. pp++;
  1017. name = bfd_asymbol_name (*pp);
  1018. }
  1019. bh = NULL;
  1020. if (! (_bfd_generic_link_add_one_symbol
  1021. (info, abfd, name, p->flags, bfd_asymbol_section (p),
  1022. p->value, string, false, false, &bh)))
  1023. return false;
  1024. h = (struct generic_link_hash_entry *) bh;
  1025. /* If this is a constructor symbol, and the linker didn't do
  1026. anything with it, then we want to just pass the symbol
  1027. through to the output file. This will happen when
  1028. linking with -r. */
  1029. if ((p->flags & BSF_CONSTRUCTOR) != 0
  1030. && (h == NULL || h->root.type == bfd_link_hash_new))
  1031. {
  1032. p->udata.p = NULL;
  1033. continue;
  1034. }
  1035. /* Save the BFD symbol so that we don't lose any backend
  1036. specific information that may be attached to it. We only
  1037. want this one if it gives more information than the
  1038. existing one; we don't want to replace a defined symbol
  1039. with an undefined one. This routine may be called with a
  1040. hash table other than the generic hash table, so we only
  1041. do this if we are certain that the hash table is a
  1042. generic one. */
  1043. if (info->output_bfd->xvec == abfd->xvec)
  1044. {
  1045. if (h->sym == NULL
  1046. || (! bfd_is_und_section (bfd_asymbol_section (p))
  1047. && (! bfd_is_com_section (bfd_asymbol_section (p))
  1048. || bfd_is_und_section (bfd_asymbol_section (h->sym)))))
  1049. {
  1050. h->sym = p;
  1051. /* BSF_OLD_COMMON is a hack to support COFF reloc
  1052. reading, and it should go away when the COFF
  1053. linker is switched to the new version. */
  1054. if (bfd_is_com_section (bfd_asymbol_section (p)))
  1055. p->flags |= BSF_OLD_COMMON;
  1056. }
  1057. }
  1058. /* Store a back pointer from the symbol to the hash
  1059. table entry for the benefit of relaxation code until
  1060. it gets rewritten to not use asymbol structures.
  1061. Setting this is also used to check whether these
  1062. symbols were set up by the generic linker. */
  1063. p->udata.p = h;
  1064. }
  1065. }
  1066. return true;
  1067. }
  1068. /* We use a state table to deal with adding symbols from an object
  1069. file. The first index into the state table describes the symbol
  1070. from the object file. The second index into the state table is the
  1071. type of the symbol in the hash table. */
  1072. /* The symbol from the object file is turned into one of these row
  1073. values. */
  1074. enum link_row
  1075. {
  1076. UNDEF_ROW, /* Undefined. */
  1077. UNDEFW_ROW, /* Weak undefined. */
  1078. DEF_ROW, /* Defined. */
  1079. DEFW_ROW, /* Weak defined. */
  1080. COMMON_ROW, /* Common. */
  1081. INDR_ROW, /* Indirect. */
  1082. WARN_ROW, /* Warning. */
  1083. SET_ROW /* Member of set. */
  1084. };
  1085. /* apparently needed for Hitachi 3050R(HI-UX/WE2)? */
  1086. #undef FAIL
  1087. /* The actions to take in the state table. */
  1088. enum link_action
  1089. {
  1090. FAIL, /* Abort. */
  1091. UND, /* Mark symbol undefined. */
  1092. WEAK, /* Mark symbol weak undefined. */
  1093. DEF, /* Mark symbol defined. */
  1094. DEFW, /* Mark symbol weak defined. */
  1095. COM, /* Mark symbol common. */
  1096. REF, /* Mark defined symbol referenced. */
  1097. CREF, /* Possibly warn about common reference to defined symbol. */
  1098. CDEF, /* Define existing common symbol. */
  1099. NOACT, /* No action. */
  1100. BIG, /* Mark symbol common using largest size. */
  1101. MDEF, /* Multiple definition error. */
  1102. MIND, /* Multiple indirect symbols. */
  1103. IND, /* Make indirect symbol. */
  1104. CIND, /* Make indirect symbol from existing common symbol. */
  1105. SET, /* Add value to set. */
  1106. MWARN, /* Make warning symbol. */
  1107. WARN, /* Warn if referenced, else MWARN. */
  1108. CYCLE, /* Repeat with symbol pointed to. */
  1109. REFC, /* Mark indirect symbol referenced and then CYCLE. */
  1110. WARNC /* Issue warning and then CYCLE. */
  1111. };
  1112. /* The state table itself. The first index is a link_row and the
  1113. second index is a bfd_link_hash_type. */
  1114. static const enum link_action link_action[8][8] =
  1115. {
  1116. /* current\prev new undef undefw def defw com indr warn */
  1117. /* UNDEF_ROW */ {UND, NOACT, UND, REF, REF, NOACT, REFC, WARNC },
  1118. /* UNDEFW_ROW */ {WEAK, NOACT, NOACT, REF, REF, NOACT, REFC, WARNC },
  1119. /* DEF_ROW */ {DEF, DEF, DEF, MDEF, DEF, CDEF, MIND, CYCLE },
  1120. /* DEFW_ROW */ {DEFW, DEFW, DEFW, NOACT, NOACT, NOACT, NOACT, CYCLE },
  1121. /* COMMON_ROW */ {COM, COM, COM, CREF, COM, BIG, REFC, WARNC },
  1122. /* INDR_ROW */ {IND, IND, IND, MDEF, IND, CIND, MIND, CYCLE },
  1123. /* WARN_ROW */ {MWARN, WARN, WARN, WARN, WARN, WARN, WARN, NOACT },
  1124. /* SET_ROW */ {SET, SET, SET, SET, SET, SET, CYCLE, CYCLE }
  1125. };
  1126. /* Most of the entries in the LINK_ACTION table are straightforward,
  1127. but a few are somewhat subtle.
  1128. A reference to an indirect symbol (UNDEF_ROW/indr or
  1129. UNDEFW_ROW/indr) is counted as a reference both to the indirect
  1130. symbol and to the symbol the indirect symbol points to.
  1131. A reference to a warning symbol (UNDEF_ROW/warn or UNDEFW_ROW/warn)
  1132. causes the warning to be issued.
  1133. A common definition of an indirect symbol (COMMON_ROW/indr) is
  1134. treated as a multiple definition error. Likewise for an indirect
  1135. definition of a common symbol (INDR_ROW/com).
  1136. An indirect definition of a warning (INDR_ROW/warn) does not cause
  1137. the warning to be issued.
  1138. If a warning is created for an indirect symbol (WARN_ROW/indr) no
  1139. warning is created for the symbol the indirect symbol points to.
  1140. Adding an entry to a set does not count as a reference to a set,
  1141. and no warning is issued (SET_ROW/warn). */
  1142. /* Return the BFD in which a hash entry has been defined, if known. */
  1143. static bfd *
  1144. hash_entry_bfd (struct bfd_link_hash_entry *h)
  1145. {
  1146. while (h->type == bfd_link_hash_warning)
  1147. h = h->u.i.link;
  1148. switch (h->type)
  1149. {
  1150. default:
  1151. return NULL;
  1152. case bfd_link_hash_undefined:
  1153. case bfd_link_hash_undefweak:
  1154. return h->u.undef.abfd;
  1155. case bfd_link_hash_defined:
  1156. case bfd_link_hash_defweak:
  1157. return h->u.def.section->owner;
  1158. case bfd_link_hash_common:
  1159. return h->u.c.p->section->owner;
  1160. }
  1161. /*NOTREACHED*/
  1162. }
  1163. /* Add a symbol to the global hash table.
  1164. ABFD is the BFD the symbol comes from.
  1165. NAME is the name of the symbol.
  1166. FLAGS is the BSF_* bits associated with the symbol.
  1167. SECTION is the section in which the symbol is defined; this may be
  1168. bfd_und_section_ptr or bfd_com_section_ptr.
  1169. VALUE is the value of the symbol, relative to the section.
  1170. STRING is used for either an indirect symbol, in which case it is
  1171. the name of the symbol to indirect to, or a warning symbol, in
  1172. which case it is the warning string.
  1173. COPY is TRUE if NAME or STRING must be copied into locally
  1174. allocated memory if they need to be saved.
  1175. COLLECT is TRUE if we should automatically collect gcc constructor
  1176. or destructor names as collect2 does.
  1177. HASHP, if not NULL, is a place to store the created hash table
  1178. entry; if *HASHP is not NULL, the caller has already looked up
  1179. the hash table entry, and stored it in *HASHP. */
  1180. bool
  1181. _bfd_generic_link_add_one_symbol (struct bfd_link_info *info,
  1182. bfd *abfd,
  1183. const char *name,
  1184. flagword flags,
  1185. asection *section,
  1186. bfd_vma value,
  1187. const char *string,
  1188. bool copy,
  1189. bool collect,
  1190. struct bfd_link_hash_entry **hashp)
  1191. {
  1192. enum link_row row;
  1193. struct bfd_link_hash_entry *h;
  1194. struct bfd_link_hash_entry *inh = NULL;
  1195. bool cycle;
  1196. BFD_ASSERT (section != NULL);
  1197. if (bfd_is_ind_section (section)
  1198. || (flags & BSF_INDIRECT) != 0)
  1199. {
  1200. row = INDR_ROW;
  1201. /* Create the indirect symbol here. This is for the benefit of
  1202. the plugin "notice" function.
  1203. STRING is the name of the symbol we want to indirect to. */
  1204. inh = bfd_wrapped_link_hash_lookup (abfd, info, string, true,
  1205. copy, false);
  1206. if (inh == NULL)
  1207. return false;
  1208. }
  1209. else if ((flags & BSF_WARNING) != 0)
  1210. row = WARN_ROW;
  1211. else if ((flags & BSF_CONSTRUCTOR) != 0)
  1212. row = SET_ROW;
  1213. else if (bfd_is_und_section (section))
  1214. {
  1215. if ((flags & BSF_WEAK) != 0)
  1216. row = UNDEFW_ROW;
  1217. else
  1218. row = UNDEF_ROW;
  1219. }
  1220. else if ((flags & BSF_WEAK) != 0)
  1221. row = DEFW_ROW;
  1222. else if (bfd_is_com_section (section))
  1223. {
  1224. row = COMMON_ROW;
  1225. if (!bfd_link_relocatable (info)
  1226. && name != NULL
  1227. && name[0] == '_'
  1228. && name[1] == '_'
  1229. && strcmp (name + (name[2] == '_'), "__gnu_lto_slim") == 0)
  1230. _bfd_error_handler
  1231. (_("%pB: plugin needed to handle lto object"), abfd);
  1232. }
  1233. else
  1234. row = DEF_ROW;
  1235. if (hashp != NULL && *hashp != NULL)
  1236. h = *hashp;
  1237. else
  1238. {
  1239. if (row == UNDEF_ROW || row == UNDEFW_ROW)
  1240. h = bfd_wrapped_link_hash_lookup (abfd, info, name, true, copy, false);
  1241. else
  1242. h = bfd_link_hash_lookup (info->hash, name, true, copy, false);
  1243. if (h == NULL)
  1244. {
  1245. if (hashp != NULL)
  1246. *hashp = NULL;
  1247. return false;
  1248. }
  1249. }
  1250. if (info->notice_all
  1251. || (info->notice_hash != NULL
  1252. && bfd_hash_lookup (info->notice_hash, name, false, false) != NULL))
  1253. {
  1254. if (! (*info->callbacks->notice) (info, h, inh,
  1255. abfd, section, value, flags))
  1256. return false;
  1257. }
  1258. if (hashp != NULL)
  1259. *hashp = h;
  1260. do
  1261. {
  1262. enum link_action action;
  1263. int prev;
  1264. prev = h->type;
  1265. /* Treat symbols defined by early linker script pass as undefined. */
  1266. if (h->ldscript_def)
  1267. prev = bfd_link_hash_undefined;
  1268. cycle = false;
  1269. action = link_action[(int) row][prev];
  1270. switch (action)
  1271. {
  1272. case FAIL:
  1273. abort ();
  1274. case NOACT:
  1275. /* Do nothing. */
  1276. break;
  1277. case UND:
  1278. /* Make a new undefined symbol. */
  1279. h->type = bfd_link_hash_undefined;
  1280. h->u.undef.abfd = abfd;
  1281. bfd_link_add_undef (info->hash, h);
  1282. break;
  1283. case WEAK:
  1284. /* Make a new weak undefined symbol. */
  1285. h->type = bfd_link_hash_undefweak;
  1286. h->u.undef.abfd = abfd;
  1287. break;
  1288. case CDEF:
  1289. /* We have found a definition for a symbol which was
  1290. previously common. */
  1291. BFD_ASSERT (h->type == bfd_link_hash_common);
  1292. (*info->callbacks->multiple_common) (info, h, abfd,
  1293. bfd_link_hash_defined, 0);
  1294. /* Fall through. */
  1295. case DEF:
  1296. case DEFW:
  1297. {
  1298. enum bfd_link_hash_type oldtype;
  1299. /* Define a symbol. */
  1300. oldtype = h->type;
  1301. if (action == DEFW)
  1302. h->type = bfd_link_hash_defweak;
  1303. else
  1304. h->type = bfd_link_hash_defined;
  1305. h->u.def.section = section;
  1306. h->u.def.value = value;
  1307. h->linker_def = 0;
  1308. h->ldscript_def = 0;
  1309. /* If we have been asked to, we act like collect2 and
  1310. identify all functions that might be global
  1311. constructors and destructors and pass them up in a
  1312. callback. We only do this for certain object file
  1313. types, since many object file types can handle this
  1314. automatically. */
  1315. if (collect && name[0] == '_')
  1316. {
  1317. const char *s;
  1318. /* A constructor or destructor name starts like this:
  1319. _+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
  1320. the second are the same character (we accept any
  1321. character there, in case a new object file format
  1322. comes along with even worse naming restrictions). */
  1323. #define CONS_PREFIX "GLOBAL_"
  1324. #define CONS_PREFIX_LEN (sizeof CONS_PREFIX - 1)
  1325. s = name + 1;
  1326. while (*s == '_')
  1327. ++s;
  1328. if (s[0] == 'G' && startswith (s, CONS_PREFIX))
  1329. {
  1330. char c;
  1331. c = s[CONS_PREFIX_LEN + 1];
  1332. if ((c == 'I' || c == 'D')
  1333. && s[CONS_PREFIX_LEN] == s[CONS_PREFIX_LEN + 2])
  1334. {
  1335. /* If this is a definition of a symbol which
  1336. was previously weakly defined, we are in
  1337. trouble. We have already added a
  1338. constructor entry for the weak defined
  1339. symbol, and now we are trying to add one
  1340. for the new symbol. Fortunately, this case
  1341. should never arise in practice. */
  1342. if (oldtype == bfd_link_hash_defweak)
  1343. abort ();
  1344. (*info->callbacks->constructor) (info, c == 'I',
  1345. h->root.string, abfd,
  1346. section, value);
  1347. }
  1348. }
  1349. }
  1350. }
  1351. break;
  1352. case COM:
  1353. /* We have found a common definition for a symbol. */
  1354. if (h->type == bfd_link_hash_new)
  1355. bfd_link_add_undef (info->hash, h);
  1356. h->type = bfd_link_hash_common;
  1357. h->u.c.p = (struct bfd_link_hash_common_entry *)
  1358. bfd_hash_allocate (&info->hash->table,
  1359. sizeof (struct bfd_link_hash_common_entry));
  1360. if (h->u.c.p == NULL)
  1361. return false;
  1362. h->u.c.size = value;
  1363. /* Select a default alignment based on the size. This may
  1364. be overridden by the caller. */
  1365. {
  1366. unsigned int power;
  1367. power = bfd_log2 (value);
  1368. if (power > 4)
  1369. power = 4;
  1370. h->u.c.p->alignment_power = power;
  1371. }
  1372. /* The section of a common symbol is only used if the common
  1373. symbol is actually allocated. It basically provides a
  1374. hook for the linker script to decide which output section
  1375. the common symbols should be put in. In most cases, the
  1376. section of a common symbol will be bfd_com_section_ptr,
  1377. the code here will choose a common symbol section named
  1378. "COMMON", and the linker script will contain *(COMMON) in
  1379. the appropriate place. A few targets use separate common
  1380. sections for small symbols, and they require special
  1381. handling. */
  1382. if (section == bfd_com_section_ptr)
  1383. {
  1384. h->u.c.p->section = bfd_make_section_old_way (abfd, "COMMON");
  1385. h->u.c.p->section->flags |= SEC_ALLOC;
  1386. }
  1387. else if (section->owner != abfd)
  1388. {
  1389. h->u.c.p->section = bfd_make_section_old_way (abfd,
  1390. section->name);
  1391. h->u.c.p->section->flags |= SEC_ALLOC;
  1392. }
  1393. else
  1394. h->u.c.p->section = section;
  1395. h->linker_def = 0;
  1396. h->ldscript_def = 0;
  1397. break;
  1398. case REF:
  1399. /* A reference to a defined symbol. */
  1400. if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
  1401. h->u.undef.next = h;
  1402. break;
  1403. case BIG:
  1404. /* We have found a common definition for a symbol which
  1405. already had a common definition. Use the maximum of the
  1406. two sizes, and use the section required by the larger symbol. */
  1407. BFD_ASSERT (h->type == bfd_link_hash_common);
  1408. (*info->callbacks->multiple_common) (info, h, abfd,
  1409. bfd_link_hash_common, value);
  1410. if (value > h->u.c.size)
  1411. {
  1412. unsigned int power;
  1413. h->u.c.size = value;
  1414. /* Select a default alignment based on the size. This may
  1415. be overridden by the caller. */
  1416. power = bfd_log2 (value);
  1417. if (power > 4)
  1418. power = 4;
  1419. h->u.c.p->alignment_power = power;
  1420. /* Some systems have special treatment for small commons,
  1421. hence we want to select the section used by the larger
  1422. symbol. This makes sure the symbol does not go in a
  1423. small common section if it is now too large. */
  1424. if (section == bfd_com_section_ptr)
  1425. {
  1426. h->u.c.p->section
  1427. = bfd_make_section_old_way (abfd, "COMMON");
  1428. h->u.c.p->section->flags |= SEC_ALLOC;
  1429. }
  1430. else if (section->owner != abfd)
  1431. {
  1432. h->u.c.p->section
  1433. = bfd_make_section_old_way (abfd, section->name);
  1434. h->u.c.p->section->flags |= SEC_ALLOC;
  1435. }
  1436. else
  1437. h->u.c.p->section = section;
  1438. }
  1439. break;
  1440. case CREF:
  1441. /* We have found a common definition for a symbol which
  1442. was already defined. */
  1443. (*info->callbacks->multiple_common) (info, h, abfd,
  1444. bfd_link_hash_common, value);
  1445. break;
  1446. case MIND:
  1447. /* Multiple indirect symbols. This is OK if they both point
  1448. to the same symbol. */
  1449. if (h->u.i.link->type == bfd_link_hash_defweak)
  1450. {
  1451. /* It is also OK to redefine a symbol that indirects to
  1452. a weak definition. So for sym@ver -> sym@@ver where
  1453. sym@@ver is weak and we have a new strong sym@ver,
  1454. redefine sym@@ver. Of course if there exists
  1455. sym -> sym@@ver then this also redefines sym. */
  1456. h = h->u.i.link;
  1457. cycle = true;
  1458. break;
  1459. }
  1460. if (string != NULL && strcmp (h->u.i.link->root.string, string) == 0)
  1461. break;
  1462. /* Fall through. */
  1463. case MDEF:
  1464. /* Handle a multiple definition. */
  1465. (*info->callbacks->multiple_definition) (info, h,
  1466. abfd, section, value);
  1467. break;
  1468. case CIND:
  1469. /* Create an indirect symbol from an existing common symbol. */
  1470. BFD_ASSERT (h->type == bfd_link_hash_common);
  1471. (*info->callbacks->multiple_common) (info, h, abfd,
  1472. bfd_link_hash_indirect, 0);
  1473. /* Fall through. */
  1474. case IND:
  1475. if (inh->type == bfd_link_hash_indirect
  1476. && inh->u.i.link == h)
  1477. {
  1478. _bfd_error_handler
  1479. /* xgettext:c-format */
  1480. (_("%pB: indirect symbol `%s' to `%s' is a loop"),
  1481. abfd, name, string);
  1482. bfd_set_error (bfd_error_invalid_operation);
  1483. return false;
  1484. }
  1485. if (inh->type == bfd_link_hash_new)
  1486. {
  1487. inh->type = bfd_link_hash_undefined;
  1488. inh->u.undef.abfd = abfd;
  1489. bfd_link_add_undef (info->hash, inh);
  1490. }
  1491. /* If the indirect symbol has been referenced, we need to
  1492. push the reference down to the symbol we are referencing. */
  1493. if (h->type != bfd_link_hash_new)
  1494. {
  1495. /* ??? If inh->type == bfd_link_hash_undefweak this
  1496. converts inh to bfd_link_hash_undefined. */
  1497. row = UNDEF_ROW;
  1498. cycle = true;
  1499. }
  1500. h->type = bfd_link_hash_indirect;
  1501. h->u.i.link = inh;
  1502. /* Not setting h = h->u.i.link here means that when cycle is
  1503. set above we'll always go to REFC, and then cycle again
  1504. to the indirected symbol. This means that any successful
  1505. change of an existing symbol to indirect counts as a
  1506. reference. ??? That may not be correct when the existing
  1507. symbol was defweak. */
  1508. break;
  1509. case SET:
  1510. /* Add an entry to a set. */
  1511. (*info->callbacks->add_to_set) (info, h, BFD_RELOC_CTOR,
  1512. abfd, section, value);
  1513. break;
  1514. case WARNC:
  1515. /* Issue a warning and cycle, except when the reference is
  1516. in LTO IR. */
  1517. if (h->u.i.warning != NULL
  1518. && (abfd->flags & BFD_PLUGIN) == 0)
  1519. {
  1520. (*info->callbacks->warning) (info, h->u.i.warning,
  1521. h->root.string, abfd, NULL, 0);
  1522. /* Only issue a warning once. */
  1523. h->u.i.warning = NULL;
  1524. }
  1525. /* Fall through. */
  1526. case CYCLE:
  1527. /* Try again with the referenced symbol. */
  1528. h = h->u.i.link;
  1529. cycle = true;
  1530. break;
  1531. case REFC:
  1532. /* A reference to an indirect symbol. */
  1533. if (h->u.undef.next == NULL && info->hash->undefs_tail != h)
  1534. h->u.undef.next = h;
  1535. h = h->u.i.link;
  1536. cycle = true;
  1537. break;
  1538. case WARN:
  1539. /* Warn if this symbol has been referenced already from non-IR,
  1540. otherwise add a warning. */
  1541. if ((!info->lto_plugin_active
  1542. && (h->u.undef.next != NULL || info->hash->undefs_tail == h))
  1543. || h->non_ir_ref_regular
  1544. || h->non_ir_ref_dynamic)
  1545. {
  1546. (*info->callbacks->warning) (info, string, h->root.string,
  1547. hash_entry_bfd (h), NULL, 0);
  1548. break;
  1549. }
  1550. /* Fall through. */
  1551. case MWARN:
  1552. /* Make a warning symbol. */
  1553. {
  1554. struct bfd_link_hash_entry *sub;
  1555. /* STRING is the warning to give. */
  1556. sub = ((struct bfd_link_hash_entry *)
  1557. ((*info->hash->table.newfunc)
  1558. (NULL, &info->hash->table, h->root.string)));
  1559. if (sub == NULL)
  1560. return false;
  1561. *sub = *h;
  1562. sub->type = bfd_link_hash_warning;
  1563. sub->u.i.link = h;
  1564. if (! copy)
  1565. sub->u.i.warning = string;
  1566. else
  1567. {
  1568. char *w;
  1569. size_t len = strlen (string) + 1;
  1570. w = (char *) bfd_hash_allocate (&info->hash->table, len);
  1571. if (w == NULL)
  1572. return false;
  1573. memcpy (w, string, len);
  1574. sub->u.i.warning = w;
  1575. }
  1576. bfd_hash_replace (&info->hash->table,
  1577. (struct bfd_hash_entry *) h,
  1578. (struct bfd_hash_entry *) sub);
  1579. if (hashp != NULL)
  1580. *hashp = sub;
  1581. }
  1582. break;
  1583. }
  1584. }
  1585. while (cycle);
  1586. return true;
  1587. }
  1588. /* Generic final link routine. */
  1589. bool
  1590. _bfd_generic_final_link (bfd *abfd, struct bfd_link_info *info)
  1591. {
  1592. bfd *sub;
  1593. asection *o;
  1594. struct bfd_link_order *p;
  1595. size_t outsymalloc;
  1596. struct generic_write_global_symbol_info wginfo;
  1597. abfd->outsymbols = NULL;
  1598. abfd->symcount = 0;
  1599. outsymalloc = 0;
  1600. /* Mark all sections which will be included in the output file. */
  1601. for (o = abfd->sections; o != NULL; o = o->next)
  1602. for (p = o->map_head.link_order; p != NULL; p = p->next)
  1603. if (p->type == bfd_indirect_link_order)
  1604. p->u.indirect.section->linker_mark = true;
  1605. /* Build the output symbol table. */
  1606. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  1607. if (! _bfd_generic_link_output_symbols (abfd, sub, info, &outsymalloc))
  1608. return false;
  1609. /* Accumulate the global symbols. */
  1610. wginfo.info = info;
  1611. wginfo.output_bfd = abfd;
  1612. wginfo.psymalloc = &outsymalloc;
  1613. _bfd_generic_link_hash_traverse (_bfd_generic_hash_table (info),
  1614. _bfd_generic_link_write_global_symbol,
  1615. &wginfo);
  1616. /* Make sure we have a trailing NULL pointer on OUTSYMBOLS. We
  1617. shouldn't really need one, since we have SYMCOUNT, but some old
  1618. code still expects one. */
  1619. if (! generic_add_output_symbol (abfd, &outsymalloc, NULL))
  1620. return false;
  1621. if (bfd_link_relocatable (info))
  1622. {
  1623. /* Allocate space for the output relocs for each section. */
  1624. for (o = abfd->sections; o != NULL; o = o->next)
  1625. {
  1626. o->reloc_count = 0;
  1627. for (p = o->map_head.link_order; p != NULL; p = p->next)
  1628. {
  1629. if (p->type == bfd_section_reloc_link_order
  1630. || p->type == bfd_symbol_reloc_link_order)
  1631. ++o->reloc_count;
  1632. else if (p->type == bfd_indirect_link_order)
  1633. {
  1634. asection *input_section;
  1635. bfd *input_bfd;
  1636. long relsize;
  1637. arelent **relocs;
  1638. asymbol **symbols;
  1639. long reloc_count;
  1640. input_section = p->u.indirect.section;
  1641. input_bfd = input_section->owner;
  1642. relsize = bfd_get_reloc_upper_bound (input_bfd,
  1643. input_section);
  1644. if (relsize < 0)
  1645. return false;
  1646. relocs = (arelent **) bfd_malloc (relsize);
  1647. if (!relocs && relsize != 0)
  1648. return false;
  1649. symbols = _bfd_generic_link_get_symbols (input_bfd);
  1650. reloc_count = bfd_canonicalize_reloc (input_bfd,
  1651. input_section,
  1652. relocs,
  1653. symbols);
  1654. free (relocs);
  1655. if (reloc_count < 0)
  1656. return false;
  1657. BFD_ASSERT ((unsigned long) reloc_count
  1658. == input_section->reloc_count);
  1659. o->reloc_count += reloc_count;
  1660. }
  1661. }
  1662. if (o->reloc_count > 0)
  1663. {
  1664. bfd_size_type amt;
  1665. amt = o->reloc_count;
  1666. amt *= sizeof (arelent *);
  1667. o->orelocation = (struct reloc_cache_entry **) bfd_alloc (abfd, amt);
  1668. if (!o->orelocation)
  1669. return false;
  1670. o->flags |= SEC_RELOC;
  1671. /* Reset the count so that it can be used as an index
  1672. when putting in the output relocs. */
  1673. o->reloc_count = 0;
  1674. }
  1675. }
  1676. }
  1677. /* Handle all the link order information for the sections. */
  1678. for (o = abfd->sections; o != NULL; o = o->next)
  1679. {
  1680. for (p = o->map_head.link_order; p != NULL; p = p->next)
  1681. {
  1682. switch (p->type)
  1683. {
  1684. case bfd_section_reloc_link_order:
  1685. case bfd_symbol_reloc_link_order:
  1686. if (! _bfd_generic_reloc_link_order (abfd, info, o, p))
  1687. return false;
  1688. break;
  1689. case bfd_indirect_link_order:
  1690. if (! default_indirect_link_order (abfd, info, o, p, true))
  1691. return false;
  1692. break;
  1693. default:
  1694. if (! _bfd_default_link_order (abfd, info, o, p))
  1695. return false;
  1696. break;
  1697. }
  1698. }
  1699. }
  1700. return true;
  1701. }
  1702. /* Add an output symbol to the output BFD. */
  1703. static bool
  1704. generic_add_output_symbol (bfd *output_bfd, size_t *psymalloc, asymbol *sym)
  1705. {
  1706. if (bfd_get_symcount (output_bfd) >= *psymalloc)
  1707. {
  1708. asymbol **newsyms;
  1709. bfd_size_type amt;
  1710. if (*psymalloc == 0)
  1711. *psymalloc = 124;
  1712. else
  1713. *psymalloc *= 2;
  1714. amt = *psymalloc;
  1715. amt *= sizeof (asymbol *);
  1716. newsyms = (asymbol **) bfd_realloc (bfd_get_outsymbols (output_bfd), amt);
  1717. if (newsyms == NULL)
  1718. return false;
  1719. output_bfd->outsymbols = newsyms;
  1720. }
  1721. output_bfd->outsymbols[output_bfd->symcount] = sym;
  1722. if (sym != NULL)
  1723. ++output_bfd->symcount;
  1724. return true;
  1725. }
  1726. /* Handle the symbols for an input BFD. */
  1727. bool
  1728. _bfd_generic_link_output_symbols (bfd *output_bfd,
  1729. bfd *input_bfd,
  1730. struct bfd_link_info *info,
  1731. size_t *psymalloc)
  1732. {
  1733. asymbol **sym_ptr;
  1734. asymbol **sym_end;
  1735. if (!bfd_generic_link_read_symbols (input_bfd))
  1736. return false;
  1737. /* Create a filename symbol if we are supposed to. */
  1738. if (info->create_object_symbols_section != NULL)
  1739. {
  1740. asection *sec;
  1741. for (sec = input_bfd->sections; sec != NULL; sec = sec->next)
  1742. {
  1743. if (sec->output_section == info->create_object_symbols_section)
  1744. {
  1745. asymbol *newsym;
  1746. newsym = bfd_make_empty_symbol (input_bfd);
  1747. if (!newsym)
  1748. return false;
  1749. newsym->name = bfd_get_filename (input_bfd);
  1750. newsym->value = 0;
  1751. newsym->flags = BSF_LOCAL | BSF_FILE;
  1752. newsym->section = sec;
  1753. if (! generic_add_output_symbol (output_bfd, psymalloc,
  1754. newsym))
  1755. return false;
  1756. break;
  1757. }
  1758. }
  1759. }
  1760. /* Adjust the values of the globally visible symbols, and write out
  1761. local symbols. */
  1762. sym_ptr = _bfd_generic_link_get_symbols (input_bfd);
  1763. sym_end = sym_ptr + _bfd_generic_link_get_symcount (input_bfd);
  1764. for (; sym_ptr < sym_end; sym_ptr++)
  1765. {
  1766. asymbol *sym;
  1767. struct generic_link_hash_entry *h;
  1768. bool output;
  1769. h = NULL;
  1770. sym = *sym_ptr;
  1771. if ((sym->flags & (BSF_INDIRECT
  1772. | BSF_WARNING
  1773. | BSF_GLOBAL
  1774. | BSF_CONSTRUCTOR
  1775. | BSF_WEAK)) != 0
  1776. || bfd_is_und_section (bfd_asymbol_section (sym))
  1777. || bfd_is_com_section (bfd_asymbol_section (sym))
  1778. || bfd_is_ind_section (bfd_asymbol_section (sym)))
  1779. {
  1780. if (sym->udata.p != NULL)
  1781. h = (struct generic_link_hash_entry *) sym->udata.p;
  1782. else if ((sym->flags & BSF_CONSTRUCTOR) != 0)
  1783. {
  1784. /* This case normally means that the main linker code
  1785. deliberately ignored this constructor symbol. We
  1786. should just pass it through. This will screw up if
  1787. the constructor symbol is from a different,
  1788. non-generic, object file format, but the case will
  1789. only arise when linking with -r, which will probably
  1790. fail anyhow, since there will be no way to represent
  1791. the relocs in the output format being used. */
  1792. h = NULL;
  1793. }
  1794. else if (bfd_is_und_section (bfd_asymbol_section (sym)))
  1795. h = ((struct generic_link_hash_entry *)
  1796. bfd_wrapped_link_hash_lookup (output_bfd, info,
  1797. bfd_asymbol_name (sym),
  1798. false, false, true));
  1799. else
  1800. h = _bfd_generic_link_hash_lookup (_bfd_generic_hash_table (info),
  1801. bfd_asymbol_name (sym),
  1802. false, false, true);
  1803. if (h != NULL)
  1804. {
  1805. /* Force all references to this symbol to point to
  1806. the same area in memory. It is possible that
  1807. this routine will be called with a hash table
  1808. other than a generic hash table, so we double
  1809. check that. */
  1810. if (info->output_bfd->xvec == input_bfd->xvec)
  1811. {
  1812. if (h->sym != NULL)
  1813. *sym_ptr = sym = h->sym;
  1814. }
  1815. switch (h->root.type)
  1816. {
  1817. default:
  1818. case bfd_link_hash_new:
  1819. abort ();
  1820. case bfd_link_hash_undefined:
  1821. break;
  1822. case bfd_link_hash_undefweak:
  1823. sym->flags |= BSF_WEAK;
  1824. break;
  1825. case bfd_link_hash_indirect:
  1826. h = (struct generic_link_hash_entry *) h->root.u.i.link;
  1827. /* fall through */
  1828. case bfd_link_hash_defined:
  1829. sym->flags |= BSF_GLOBAL;
  1830. sym->flags &=~ (BSF_WEAK | BSF_CONSTRUCTOR);
  1831. sym->value = h->root.u.def.value;
  1832. sym->section = h->root.u.def.section;
  1833. break;
  1834. case bfd_link_hash_defweak:
  1835. sym->flags |= BSF_WEAK;
  1836. sym->flags &=~ BSF_CONSTRUCTOR;
  1837. sym->value = h->root.u.def.value;
  1838. sym->section = h->root.u.def.section;
  1839. break;
  1840. case bfd_link_hash_common:
  1841. sym->value = h->root.u.c.size;
  1842. sym->flags |= BSF_GLOBAL;
  1843. if (! bfd_is_com_section (sym->section))
  1844. {
  1845. BFD_ASSERT (bfd_is_und_section (sym->section));
  1846. sym->section = bfd_com_section_ptr;
  1847. }
  1848. /* We do not set the section of the symbol to
  1849. h->root.u.c.p->section. That value was saved so
  1850. that we would know where to allocate the symbol
  1851. if it was defined. In this case the type is
  1852. still bfd_link_hash_common, so we did not define
  1853. it, so we do not want to use that section. */
  1854. break;
  1855. }
  1856. }
  1857. }
  1858. if ((sym->flags & BSF_KEEP) == 0
  1859. && (info->strip == strip_all
  1860. || (info->strip == strip_some
  1861. && bfd_hash_lookup (info->keep_hash, bfd_asymbol_name (sym),
  1862. false, false) == NULL)))
  1863. output = false;
  1864. else if ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0)
  1865. {
  1866. /* If this symbol is marked as occurring now, rather
  1867. than at the end, output it now. This is used for
  1868. COFF C_EXT FCN symbols. FIXME: There must be a
  1869. better way. */
  1870. if (bfd_asymbol_bfd (sym) == input_bfd
  1871. && (sym->flags & BSF_NOT_AT_END) != 0)
  1872. output = true;
  1873. else
  1874. output = false;
  1875. }
  1876. else if ((sym->flags & BSF_KEEP) != 0)
  1877. output = true;
  1878. else if (bfd_is_ind_section (sym->section))
  1879. output = false;
  1880. else if ((sym->flags & BSF_DEBUGGING) != 0)
  1881. {
  1882. if (info->strip == strip_none)
  1883. output = true;
  1884. else
  1885. output = false;
  1886. }
  1887. else if (bfd_is_und_section (sym->section)
  1888. || bfd_is_com_section (sym->section))
  1889. output = false;
  1890. else if ((sym->flags & BSF_LOCAL) != 0)
  1891. {
  1892. if ((sym->flags & BSF_WARNING) != 0)
  1893. output = false;
  1894. else
  1895. {
  1896. switch (info->discard)
  1897. {
  1898. default:
  1899. case discard_all:
  1900. output = false;
  1901. break;
  1902. case discard_sec_merge:
  1903. output = true;
  1904. if (bfd_link_relocatable (info)
  1905. || ! (sym->section->flags & SEC_MERGE))
  1906. break;
  1907. /* FALLTHROUGH */
  1908. case discard_l:
  1909. if (bfd_is_local_label (input_bfd, sym))
  1910. output = false;
  1911. else
  1912. output = true;
  1913. break;
  1914. case discard_none:
  1915. output = true;
  1916. break;
  1917. }
  1918. }
  1919. }
  1920. else if ((sym->flags & BSF_CONSTRUCTOR))
  1921. {
  1922. if (info->strip != strip_all)
  1923. output = true;
  1924. else
  1925. output = false;
  1926. }
  1927. else if (sym->flags == 0
  1928. && (sym->section->owner->flags & BFD_PLUGIN) != 0)
  1929. /* LTO doesn't set symbol information. We get here with the
  1930. generic linker for a symbol that was "common" but no longer
  1931. needs to be global. */
  1932. output = false;
  1933. else
  1934. abort ();
  1935. /* If this symbol is in a section which is not being included
  1936. in the output file, then we don't want to output the
  1937. symbol. */
  1938. if (!bfd_is_abs_section (sym->section)
  1939. && bfd_section_removed_from_list (output_bfd,
  1940. sym->section->output_section))
  1941. output = false;
  1942. if (output)
  1943. {
  1944. if (! generic_add_output_symbol (output_bfd, psymalloc, sym))
  1945. return false;
  1946. if (h != NULL)
  1947. h->written = true;
  1948. }
  1949. }
  1950. return true;
  1951. }
  1952. /* Set the section and value of a generic BFD symbol based on a linker
  1953. hash table entry. */
  1954. static void
  1955. set_symbol_from_hash (asymbol *sym, struct bfd_link_hash_entry *h)
  1956. {
  1957. switch (h->type)
  1958. {
  1959. default:
  1960. abort ();
  1961. break;
  1962. case bfd_link_hash_new:
  1963. /* This can happen when a constructor symbol is seen but we are
  1964. not building constructors. */
  1965. if (sym->section != NULL)
  1966. {
  1967. BFD_ASSERT ((sym->flags & BSF_CONSTRUCTOR) != 0);
  1968. }
  1969. else
  1970. {
  1971. sym->flags |= BSF_CONSTRUCTOR;
  1972. sym->section = bfd_abs_section_ptr;
  1973. sym->value = 0;
  1974. }
  1975. break;
  1976. case bfd_link_hash_undefined:
  1977. sym->section = bfd_und_section_ptr;
  1978. sym->value = 0;
  1979. break;
  1980. case bfd_link_hash_undefweak:
  1981. sym->section = bfd_und_section_ptr;
  1982. sym->value = 0;
  1983. sym->flags |= BSF_WEAK;
  1984. break;
  1985. case bfd_link_hash_defined:
  1986. sym->section = h->u.def.section;
  1987. sym->value = h->u.def.value;
  1988. break;
  1989. case bfd_link_hash_defweak:
  1990. sym->flags |= BSF_WEAK;
  1991. sym->section = h->u.def.section;
  1992. sym->value = h->u.def.value;
  1993. break;
  1994. case bfd_link_hash_common:
  1995. sym->value = h->u.c.size;
  1996. if (sym->section == NULL)
  1997. sym->section = bfd_com_section_ptr;
  1998. else if (! bfd_is_com_section (sym->section))
  1999. {
  2000. BFD_ASSERT (bfd_is_und_section (sym->section));
  2001. sym->section = bfd_com_section_ptr;
  2002. }
  2003. /* Do not set the section; see _bfd_generic_link_output_symbols. */
  2004. break;
  2005. case bfd_link_hash_indirect:
  2006. case bfd_link_hash_warning:
  2007. /* FIXME: What should we do here? */
  2008. break;
  2009. }
  2010. }
  2011. /* Write out a global symbol, if it hasn't already been written out.
  2012. This is called for each symbol in the hash table. */
  2013. bool
  2014. _bfd_generic_link_write_global_symbol (struct generic_link_hash_entry *h,
  2015. void *data)
  2016. {
  2017. struct generic_write_global_symbol_info *wginfo =
  2018. (struct generic_write_global_symbol_info *) data;
  2019. asymbol *sym;
  2020. if (h->written)
  2021. return true;
  2022. h->written = true;
  2023. if (wginfo->info->strip == strip_all
  2024. || (wginfo->info->strip == strip_some
  2025. && bfd_hash_lookup (wginfo->info->keep_hash, h->root.root.string,
  2026. false, false) == NULL))
  2027. return true;
  2028. if (h->sym != NULL)
  2029. sym = h->sym;
  2030. else
  2031. {
  2032. sym = bfd_make_empty_symbol (wginfo->output_bfd);
  2033. if (!sym)
  2034. return false;
  2035. sym->name = h->root.root.string;
  2036. sym->flags = 0;
  2037. }
  2038. set_symbol_from_hash (sym, &h->root);
  2039. sym->flags |= BSF_GLOBAL;
  2040. if (! generic_add_output_symbol (wginfo->output_bfd, wginfo->psymalloc,
  2041. sym))
  2042. {
  2043. /* FIXME: No way to return failure. */
  2044. abort ();
  2045. }
  2046. return true;
  2047. }
  2048. /* Create a relocation. */
  2049. bool
  2050. _bfd_generic_reloc_link_order (bfd *abfd,
  2051. struct bfd_link_info *info,
  2052. asection *sec,
  2053. struct bfd_link_order *link_order)
  2054. {
  2055. arelent *r;
  2056. if (! bfd_link_relocatable (info))
  2057. abort ();
  2058. if (sec->orelocation == NULL)
  2059. abort ();
  2060. r = (arelent *) bfd_alloc (abfd, sizeof (arelent));
  2061. if (r == NULL)
  2062. return false;
  2063. r->address = link_order->offset;
  2064. r->howto = bfd_reloc_type_lookup (abfd, link_order->u.reloc.p->reloc);
  2065. if (r->howto == 0)
  2066. {
  2067. bfd_set_error (bfd_error_bad_value);
  2068. return false;
  2069. }
  2070. /* Get the symbol to use for the relocation. */
  2071. if (link_order->type == bfd_section_reloc_link_order)
  2072. r->sym_ptr_ptr = link_order->u.reloc.p->u.section->symbol_ptr_ptr;
  2073. else
  2074. {
  2075. struct generic_link_hash_entry *h;
  2076. h = ((struct generic_link_hash_entry *)
  2077. bfd_wrapped_link_hash_lookup (abfd, info,
  2078. link_order->u.reloc.p->u.name,
  2079. false, false, true));
  2080. if (h == NULL
  2081. || ! h->written)
  2082. {
  2083. (*info->callbacks->unattached_reloc)
  2084. (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
  2085. bfd_set_error (bfd_error_bad_value);
  2086. return false;
  2087. }
  2088. r->sym_ptr_ptr = &h->sym;
  2089. }
  2090. /* If this is an inplace reloc, write the addend to the object file.
  2091. Otherwise, store it in the reloc addend. */
  2092. if (! r->howto->partial_inplace)
  2093. r->addend = link_order->u.reloc.p->addend;
  2094. else
  2095. {
  2096. bfd_size_type size;
  2097. bfd_reloc_status_type rstat;
  2098. bfd_byte *buf;
  2099. bool ok;
  2100. file_ptr loc;
  2101. size = bfd_get_reloc_size (r->howto);
  2102. buf = (bfd_byte *) bfd_zmalloc (size);
  2103. if (buf == NULL && size != 0)
  2104. return false;
  2105. rstat = _bfd_relocate_contents (r->howto, abfd,
  2106. (bfd_vma) link_order->u.reloc.p->addend,
  2107. buf);
  2108. switch (rstat)
  2109. {
  2110. case bfd_reloc_ok:
  2111. break;
  2112. default:
  2113. case bfd_reloc_outofrange:
  2114. abort ();
  2115. case bfd_reloc_overflow:
  2116. (*info->callbacks->reloc_overflow)
  2117. (info, NULL,
  2118. (link_order->type == bfd_section_reloc_link_order
  2119. ? bfd_section_name (link_order->u.reloc.p->u.section)
  2120. : link_order->u.reloc.p->u.name),
  2121. r->howto->name, link_order->u.reloc.p->addend,
  2122. NULL, NULL, 0);
  2123. break;
  2124. }
  2125. loc = link_order->offset * bfd_octets_per_byte (abfd, sec);
  2126. ok = bfd_set_section_contents (abfd, sec, buf, loc, size);
  2127. free (buf);
  2128. if (! ok)
  2129. return false;
  2130. r->addend = 0;
  2131. }
  2132. sec->orelocation[sec->reloc_count] = r;
  2133. ++sec->reloc_count;
  2134. return true;
  2135. }
  2136. /* Allocate a new link_order for a section. */
  2137. struct bfd_link_order *
  2138. bfd_new_link_order (bfd *abfd, asection *section)
  2139. {
  2140. size_t amt = sizeof (struct bfd_link_order);
  2141. struct bfd_link_order *new_lo;
  2142. new_lo = (struct bfd_link_order *) bfd_zalloc (abfd, amt);
  2143. if (!new_lo)
  2144. return NULL;
  2145. new_lo->type = bfd_undefined_link_order;
  2146. if (section->map_tail.link_order != NULL)
  2147. section->map_tail.link_order->next = new_lo;
  2148. else
  2149. section->map_head.link_order = new_lo;
  2150. section->map_tail.link_order = new_lo;
  2151. return new_lo;
  2152. }
  2153. /* Default link order processing routine. Note that we can not handle
  2154. the reloc_link_order types here, since they depend upon the details
  2155. of how the particular backends generates relocs. */
  2156. bool
  2157. _bfd_default_link_order (bfd *abfd,
  2158. struct bfd_link_info *info,
  2159. asection *sec,
  2160. struct bfd_link_order *link_order)
  2161. {
  2162. switch (link_order->type)
  2163. {
  2164. case bfd_undefined_link_order:
  2165. case bfd_section_reloc_link_order:
  2166. case bfd_symbol_reloc_link_order:
  2167. default:
  2168. abort ();
  2169. case bfd_indirect_link_order:
  2170. return default_indirect_link_order (abfd, info, sec, link_order,
  2171. false);
  2172. case bfd_data_link_order:
  2173. return default_data_link_order (abfd, info, sec, link_order);
  2174. }
  2175. }
  2176. /* Default routine to handle a bfd_data_link_order. */
  2177. static bool
  2178. default_data_link_order (bfd *abfd,
  2179. struct bfd_link_info *info,
  2180. asection *sec,
  2181. struct bfd_link_order *link_order)
  2182. {
  2183. bfd_size_type size;
  2184. size_t fill_size;
  2185. bfd_byte *fill;
  2186. file_ptr loc;
  2187. bool result;
  2188. BFD_ASSERT ((sec->flags & SEC_HAS_CONTENTS) != 0);
  2189. size = link_order->size;
  2190. if (size == 0)
  2191. return true;
  2192. fill = link_order->u.data.contents;
  2193. fill_size = link_order->u.data.size;
  2194. if (fill_size == 0)
  2195. {
  2196. fill = abfd->arch_info->fill (size, info->big_endian,
  2197. (sec->flags & SEC_CODE) != 0);
  2198. if (fill == NULL)
  2199. return false;
  2200. }
  2201. else if (fill_size < size)
  2202. {
  2203. bfd_byte *p;
  2204. fill = (bfd_byte *) bfd_malloc (size);
  2205. if (fill == NULL)
  2206. return false;
  2207. p = fill;
  2208. if (fill_size == 1)
  2209. memset (p, (int) link_order->u.data.contents[0], (size_t) size);
  2210. else
  2211. {
  2212. do
  2213. {
  2214. memcpy (p, link_order->u.data.contents, fill_size);
  2215. p += fill_size;
  2216. size -= fill_size;
  2217. }
  2218. while (size >= fill_size);
  2219. if (size != 0)
  2220. memcpy (p, link_order->u.data.contents, (size_t) size);
  2221. size = link_order->size;
  2222. }
  2223. }
  2224. loc = link_order->offset * bfd_octets_per_byte (abfd, sec);
  2225. result = bfd_set_section_contents (abfd, sec, fill, loc, size);
  2226. if (fill != link_order->u.data.contents)
  2227. free (fill);
  2228. return result;
  2229. }
  2230. /* Default routine to handle a bfd_indirect_link_order. */
  2231. static bool
  2232. default_indirect_link_order (bfd *output_bfd,
  2233. struct bfd_link_info *info,
  2234. asection *output_section,
  2235. struct bfd_link_order *link_order,
  2236. bool generic_linker)
  2237. {
  2238. asection *input_section;
  2239. bfd *input_bfd;
  2240. bfd_byte *contents = NULL;
  2241. bfd_byte *new_contents;
  2242. bfd_size_type sec_size;
  2243. file_ptr loc;
  2244. BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
  2245. input_section = link_order->u.indirect.section;
  2246. input_bfd = input_section->owner;
  2247. if (input_section->size == 0)
  2248. return true;
  2249. BFD_ASSERT (input_section->output_section == output_section);
  2250. BFD_ASSERT (input_section->output_offset == link_order->offset);
  2251. BFD_ASSERT (input_section->size == link_order->size);
  2252. if (bfd_link_relocatable (info)
  2253. && input_section->reloc_count > 0
  2254. && output_section->orelocation == NULL)
  2255. {
  2256. /* Space has not been allocated for the output relocations.
  2257. This can happen when we are called by a specific backend
  2258. because somebody is attempting to link together different
  2259. types of object files. Handling this case correctly is
  2260. difficult, and sometimes impossible. */
  2261. _bfd_error_handler
  2262. /* xgettext:c-format */
  2263. (_("attempt to do relocatable link with %s input and %s output"),
  2264. bfd_get_target (input_bfd), bfd_get_target (output_bfd));
  2265. bfd_set_error (bfd_error_wrong_format);
  2266. return false;
  2267. }
  2268. if (! generic_linker)
  2269. {
  2270. asymbol **sympp;
  2271. asymbol **symppend;
  2272. /* Get the canonical symbols. The generic linker will always
  2273. have retrieved them by this point, but we are being called by
  2274. a specific linker, presumably because we are linking
  2275. different types of object files together. */
  2276. if (!bfd_generic_link_read_symbols (input_bfd))
  2277. return false;
  2278. /* Since we have been called by a specific linker, rather than
  2279. the generic linker, the values of the symbols will not be
  2280. right. They will be the values as seen in the input file,
  2281. not the values of the final link. We need to fix them up
  2282. before we can relocate the section. */
  2283. sympp = _bfd_generic_link_get_symbols (input_bfd);
  2284. symppend = sympp + _bfd_generic_link_get_symcount (input_bfd);
  2285. for (; sympp < symppend; sympp++)
  2286. {
  2287. asymbol *sym;
  2288. struct bfd_link_hash_entry *h;
  2289. sym = *sympp;
  2290. if ((sym->flags & (BSF_INDIRECT
  2291. | BSF_WARNING
  2292. | BSF_GLOBAL
  2293. | BSF_CONSTRUCTOR
  2294. | BSF_WEAK)) != 0
  2295. || bfd_is_und_section (bfd_asymbol_section (sym))
  2296. || bfd_is_com_section (bfd_asymbol_section (sym))
  2297. || bfd_is_ind_section (bfd_asymbol_section (sym)))
  2298. {
  2299. /* sym->udata may have been set by
  2300. generic_link_add_symbol_list. */
  2301. if (sym->udata.p != NULL)
  2302. h = (struct bfd_link_hash_entry *) sym->udata.p;
  2303. else if (bfd_is_und_section (bfd_asymbol_section (sym)))
  2304. h = bfd_wrapped_link_hash_lookup (output_bfd, info,
  2305. bfd_asymbol_name (sym),
  2306. false, false, true);
  2307. else
  2308. h = bfd_link_hash_lookup (info->hash,
  2309. bfd_asymbol_name (sym),
  2310. false, false, true);
  2311. if (h != NULL)
  2312. set_symbol_from_hash (sym, h);
  2313. }
  2314. }
  2315. }
  2316. if ((output_section->flags & (SEC_GROUP | SEC_LINKER_CREATED)) == SEC_GROUP
  2317. && input_section->size != 0)
  2318. {
  2319. /* Group section contents are set by bfd_elf_set_group_contents. */
  2320. if (!output_bfd->output_has_begun)
  2321. {
  2322. /* FIXME: This hack ensures bfd_elf_set_group_contents is called. */
  2323. if (!bfd_set_section_contents (output_bfd, output_section, "", 0, 1))
  2324. goto error_return;
  2325. }
  2326. new_contents = output_section->contents;
  2327. BFD_ASSERT (new_contents != NULL);
  2328. BFD_ASSERT (input_section->output_offset == 0);
  2329. }
  2330. else
  2331. {
  2332. /* Get and relocate the section contents. */
  2333. sec_size = (input_section->rawsize > input_section->size
  2334. ? input_section->rawsize
  2335. : input_section->size);
  2336. contents = (bfd_byte *) bfd_malloc (sec_size);
  2337. if (contents == NULL && sec_size != 0)
  2338. goto error_return;
  2339. new_contents = (bfd_get_relocated_section_contents
  2340. (output_bfd, info, link_order, contents,
  2341. bfd_link_relocatable (info),
  2342. _bfd_generic_link_get_symbols (input_bfd)));
  2343. if (!new_contents)
  2344. goto error_return;
  2345. }
  2346. /* Output the section contents. */
  2347. loc = (input_section->output_offset
  2348. * bfd_octets_per_byte (output_bfd, output_section));
  2349. if (! bfd_set_section_contents (output_bfd, output_section,
  2350. new_contents, loc, input_section->size))
  2351. goto error_return;
  2352. free (contents);
  2353. return true;
  2354. error_return:
  2355. free (contents);
  2356. return false;
  2357. }
  2358. /* A little routine to count the number of relocs in a link_order
  2359. list. */
  2360. unsigned int
  2361. _bfd_count_link_order_relocs (struct bfd_link_order *link_order)
  2362. {
  2363. register unsigned int c;
  2364. register struct bfd_link_order *l;
  2365. c = 0;
  2366. for (l = link_order; l != NULL; l = l->next)
  2367. {
  2368. if (l->type == bfd_section_reloc_link_order
  2369. || l->type == bfd_symbol_reloc_link_order)
  2370. ++c;
  2371. }
  2372. return c;
  2373. }
  2374. /*
  2375. FUNCTION
  2376. bfd_link_split_section
  2377. SYNOPSIS
  2378. bool bfd_link_split_section (bfd *abfd, asection *sec);
  2379. DESCRIPTION
  2380. Return nonzero if @var{sec} should be split during a
  2381. reloceatable or final link.
  2382. .#define bfd_link_split_section(abfd, sec) \
  2383. . BFD_SEND (abfd, _bfd_link_split_section, (abfd, sec))
  2384. .
  2385. */
  2386. bool
  2387. _bfd_generic_link_split_section (bfd *abfd ATTRIBUTE_UNUSED,
  2388. asection *sec ATTRIBUTE_UNUSED)
  2389. {
  2390. return false;
  2391. }
  2392. /*
  2393. FUNCTION
  2394. bfd_section_already_linked
  2395. SYNOPSIS
  2396. bool bfd_section_already_linked (bfd *abfd,
  2397. asection *sec,
  2398. struct bfd_link_info *info);
  2399. DESCRIPTION
  2400. Check if @var{data} has been already linked during a reloceatable
  2401. or final link. Return TRUE if it has.
  2402. .#define bfd_section_already_linked(abfd, sec, info) \
  2403. . BFD_SEND (abfd, _section_already_linked, (abfd, sec, info))
  2404. .
  2405. */
  2406. /* Sections marked with the SEC_LINK_ONCE flag should only be linked
  2407. once into the output. This routine checks each section, and
  2408. arrange to discard it if a section of the same name has already
  2409. been linked. This code assumes that all relevant sections have the
  2410. SEC_LINK_ONCE flag set; that is, it does not depend solely upon the
  2411. section name. bfd_section_already_linked is called via
  2412. bfd_map_over_sections. */
  2413. /* The hash table. */
  2414. static struct bfd_hash_table _bfd_section_already_linked_table;
  2415. /* Support routines for the hash table used by section_already_linked,
  2416. initialize the table, traverse, lookup, fill in an entry and remove
  2417. the table. */
  2418. void
  2419. bfd_section_already_linked_table_traverse
  2420. (bool (*func) (struct bfd_section_already_linked_hash_entry *, void *),
  2421. void *info)
  2422. {
  2423. bfd_hash_traverse (&_bfd_section_already_linked_table,
  2424. (bool (*) (struct bfd_hash_entry *, void *)) func,
  2425. info);
  2426. }
  2427. struct bfd_section_already_linked_hash_entry *
  2428. bfd_section_already_linked_table_lookup (const char *name)
  2429. {
  2430. return ((struct bfd_section_already_linked_hash_entry *)
  2431. bfd_hash_lookup (&_bfd_section_already_linked_table, name,
  2432. true, false));
  2433. }
  2434. bool
  2435. bfd_section_already_linked_table_insert
  2436. (struct bfd_section_already_linked_hash_entry *already_linked_list,
  2437. asection *sec)
  2438. {
  2439. struct bfd_section_already_linked *l;
  2440. /* Allocate the memory from the same obstack as the hash table is
  2441. kept in. */
  2442. l = (struct bfd_section_already_linked *)
  2443. bfd_hash_allocate (&_bfd_section_already_linked_table, sizeof *l);
  2444. if (l == NULL)
  2445. return false;
  2446. l->sec = sec;
  2447. l->next = already_linked_list->entry;
  2448. already_linked_list->entry = l;
  2449. return true;
  2450. }
  2451. static struct bfd_hash_entry *
  2452. already_linked_newfunc (struct bfd_hash_entry *entry ATTRIBUTE_UNUSED,
  2453. struct bfd_hash_table *table,
  2454. const char *string ATTRIBUTE_UNUSED)
  2455. {
  2456. struct bfd_section_already_linked_hash_entry *ret =
  2457. (struct bfd_section_already_linked_hash_entry *)
  2458. bfd_hash_allocate (table, sizeof *ret);
  2459. if (ret == NULL)
  2460. return NULL;
  2461. ret->entry = NULL;
  2462. return &ret->root;
  2463. }
  2464. bool
  2465. bfd_section_already_linked_table_init (void)
  2466. {
  2467. return bfd_hash_table_init_n (&_bfd_section_already_linked_table,
  2468. already_linked_newfunc,
  2469. sizeof (struct bfd_section_already_linked_hash_entry),
  2470. 42);
  2471. }
  2472. void
  2473. bfd_section_already_linked_table_free (void)
  2474. {
  2475. bfd_hash_table_free (&_bfd_section_already_linked_table);
  2476. }
  2477. /* Report warnings as appropriate for duplicate section SEC.
  2478. Return FALSE if we decide to keep SEC after all. */
  2479. bool
  2480. _bfd_handle_already_linked (asection *sec,
  2481. struct bfd_section_already_linked *l,
  2482. struct bfd_link_info *info)
  2483. {
  2484. switch (sec->flags & SEC_LINK_DUPLICATES)
  2485. {
  2486. default:
  2487. abort ();
  2488. case SEC_LINK_DUPLICATES_DISCARD:
  2489. /* If we found an LTO IR match for this comdat group on
  2490. the first pass, replace it with the LTO output on the
  2491. second pass. We can't simply choose real object
  2492. files over IR because the first pass may contain a
  2493. mix of LTO and normal objects and we must keep the
  2494. first match, be it IR or real. */
  2495. if (sec->owner->lto_output
  2496. && (l->sec->owner->flags & BFD_PLUGIN) != 0)
  2497. {
  2498. l->sec = sec;
  2499. return false;
  2500. }
  2501. break;
  2502. case SEC_LINK_DUPLICATES_ONE_ONLY:
  2503. info->callbacks->einfo
  2504. /* xgettext:c-format */
  2505. (_("%pB: ignoring duplicate section `%pA'\n"),
  2506. sec->owner, sec);
  2507. break;
  2508. case SEC_LINK_DUPLICATES_SAME_SIZE:
  2509. if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
  2510. ;
  2511. else if (sec->size != l->sec->size)
  2512. info->callbacks->einfo
  2513. /* xgettext:c-format */
  2514. (_("%pB: duplicate section `%pA' has different size\n"),
  2515. sec->owner, sec);
  2516. break;
  2517. case SEC_LINK_DUPLICATES_SAME_CONTENTS:
  2518. if ((l->sec->owner->flags & BFD_PLUGIN) != 0)
  2519. ;
  2520. else if (sec->size != l->sec->size)
  2521. info->callbacks->einfo
  2522. /* xgettext:c-format */
  2523. (_("%pB: duplicate section `%pA' has different size\n"),
  2524. sec->owner, sec);
  2525. else if (sec->size != 0)
  2526. {
  2527. bfd_byte *sec_contents, *l_sec_contents = NULL;
  2528. if (!bfd_malloc_and_get_section (sec->owner, sec, &sec_contents))
  2529. info->callbacks->einfo
  2530. /* xgettext:c-format */
  2531. (_("%pB: could not read contents of section `%pA'\n"),
  2532. sec->owner, sec);
  2533. else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec,
  2534. &l_sec_contents))
  2535. info->callbacks->einfo
  2536. /* xgettext:c-format */
  2537. (_("%pB: could not read contents of section `%pA'\n"),
  2538. l->sec->owner, l->sec);
  2539. else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0)
  2540. info->callbacks->einfo
  2541. /* xgettext:c-format */
  2542. (_("%pB: duplicate section `%pA' has different contents\n"),
  2543. sec->owner, sec);
  2544. free (sec_contents);
  2545. free (l_sec_contents);
  2546. }
  2547. break;
  2548. }
  2549. /* Set the output_section field so that lang_add_section
  2550. does not create a lang_input_section structure for this
  2551. section. Since there might be a symbol in the section
  2552. being discarded, we must retain a pointer to the section
  2553. which we are really going to use. */
  2554. sec->output_section = bfd_abs_section_ptr;
  2555. sec->kept_section = l->sec;
  2556. return true;
  2557. }
  2558. /* This is used on non-ELF inputs. */
  2559. bool
  2560. _bfd_generic_section_already_linked (bfd *abfd ATTRIBUTE_UNUSED,
  2561. asection *sec,
  2562. struct bfd_link_info *info)
  2563. {
  2564. const char *name;
  2565. struct bfd_section_already_linked *l;
  2566. struct bfd_section_already_linked_hash_entry *already_linked_list;
  2567. if ((sec->flags & SEC_LINK_ONCE) == 0)
  2568. return false;
  2569. /* The generic linker doesn't handle section groups. */
  2570. if ((sec->flags & SEC_GROUP) != 0)
  2571. return false;
  2572. /* FIXME: When doing a relocatable link, we may have trouble
  2573. copying relocations in other sections that refer to local symbols
  2574. in the section being discarded. Those relocations will have to
  2575. be converted somehow; as of this writing I'm not sure that any of
  2576. the backends handle that correctly.
  2577. It is tempting to instead not discard link once sections when
  2578. doing a relocatable link (technically, they should be discarded
  2579. whenever we are building constructors). However, that fails,
  2580. because the linker winds up combining all the link once sections
  2581. into a single large link once section, which defeats the purpose
  2582. of having link once sections in the first place. */
  2583. name = bfd_section_name (sec);
  2584. already_linked_list = bfd_section_already_linked_table_lookup (name);
  2585. l = already_linked_list->entry;
  2586. if (l != NULL)
  2587. {
  2588. /* The section has already been linked. See if we should
  2589. issue a warning. */
  2590. return _bfd_handle_already_linked (sec, l, info);
  2591. }
  2592. /* This is the first section with this name. Record it. */
  2593. if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
  2594. info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
  2595. return false;
  2596. }
  2597. /* Choose a neighbouring section to S in OBFD that will be output, or
  2598. the absolute section if ADDR is out of bounds of the neighbours. */
  2599. asection *
  2600. _bfd_nearby_section (bfd *obfd, asection *s, bfd_vma addr)
  2601. {
  2602. asection *next, *prev, *best;
  2603. /* Find preceding kept section. */
  2604. for (prev = s->prev; prev != NULL; prev = prev->prev)
  2605. if ((prev->flags & SEC_EXCLUDE) == 0
  2606. && !bfd_section_removed_from_list (obfd, prev))
  2607. break;
  2608. /* Find following kept section. Start at prev->next because
  2609. other sections may have been added after S was removed. */
  2610. if (s->prev != NULL)
  2611. next = s->prev->next;
  2612. else
  2613. next = s->owner->sections;
  2614. for (; next != NULL; next = next->next)
  2615. if ((next->flags & SEC_EXCLUDE) == 0
  2616. && !bfd_section_removed_from_list (obfd, next))
  2617. break;
  2618. /* Choose better of two sections, based on flags. The idea
  2619. is to choose a section that will be in the same segment
  2620. as S would have been if it was kept. */
  2621. best = next;
  2622. if (prev == NULL)
  2623. {
  2624. if (next == NULL)
  2625. best = bfd_abs_section_ptr;
  2626. }
  2627. else if (next == NULL)
  2628. best = prev;
  2629. else if (((prev->flags ^ next->flags)
  2630. & (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_LOAD)) != 0)
  2631. {
  2632. if (((next->flags ^ s->flags)
  2633. & (SEC_ALLOC | SEC_THREAD_LOCAL)) != 0
  2634. /* We prefer to choose a loaded section. Section S
  2635. doesn't have SEC_LOAD set (it being excluded, that
  2636. part of the flag processing didn't happen) so we
  2637. can't compare that flag to those of NEXT and PREV. */
  2638. || ((prev->flags & SEC_LOAD) != 0
  2639. && (next->flags & SEC_LOAD) == 0))
  2640. best = prev;
  2641. }
  2642. else if (((prev->flags ^ next->flags) & SEC_READONLY) != 0)
  2643. {
  2644. if (((next->flags ^ s->flags) & SEC_READONLY) != 0)
  2645. best = prev;
  2646. }
  2647. else if (((prev->flags ^ next->flags) & SEC_CODE) != 0)
  2648. {
  2649. if (((next->flags ^ s->flags) & SEC_CODE) != 0)
  2650. best = prev;
  2651. }
  2652. else
  2653. {
  2654. /* Flags we care about are the same. Prefer the following
  2655. section if that will result in a positive valued sym. */
  2656. if (addr < next->vma)
  2657. best = prev;
  2658. }
  2659. return best;
  2660. }
  2661. /* Convert symbols in excluded output sections to use a kept section. */
  2662. static bool
  2663. fix_syms (struct bfd_link_hash_entry *h, void *data)
  2664. {
  2665. bfd *obfd = (bfd *) data;
  2666. if (h->type == bfd_link_hash_defined
  2667. || h->type == bfd_link_hash_defweak)
  2668. {
  2669. asection *s = h->u.def.section;
  2670. if (s != NULL
  2671. && s->output_section != NULL
  2672. && (s->output_section->flags & SEC_EXCLUDE) != 0
  2673. && bfd_section_removed_from_list (obfd, s->output_section))
  2674. {
  2675. asection *op;
  2676. h->u.def.value += s->output_offset + s->output_section->vma;
  2677. op = _bfd_nearby_section (obfd, s->output_section, h->u.def.value);
  2678. h->u.def.value -= op->vma;
  2679. h->u.def.section = op;
  2680. }
  2681. }
  2682. return true;
  2683. }
  2684. void
  2685. _bfd_fix_excluded_sec_syms (bfd *obfd, struct bfd_link_info *info)
  2686. {
  2687. bfd_link_hash_traverse (info->hash, fix_syms, obfd);
  2688. }
  2689. /*
  2690. FUNCTION
  2691. bfd_generic_define_common_symbol
  2692. SYNOPSIS
  2693. bool bfd_generic_define_common_symbol
  2694. (bfd *output_bfd, struct bfd_link_info *info,
  2695. struct bfd_link_hash_entry *h);
  2696. DESCRIPTION
  2697. Convert common symbol @var{h} into a defined symbol.
  2698. Return TRUE on success and FALSE on failure.
  2699. .#define bfd_define_common_symbol(output_bfd, info, h) \
  2700. . BFD_SEND (output_bfd, _bfd_define_common_symbol, (output_bfd, info, h))
  2701. .
  2702. */
  2703. bool
  2704. bfd_generic_define_common_symbol (bfd *output_bfd,
  2705. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2706. struct bfd_link_hash_entry *h)
  2707. {
  2708. unsigned int power_of_two;
  2709. bfd_vma alignment, size;
  2710. asection *section;
  2711. BFD_ASSERT (h != NULL && h->type == bfd_link_hash_common);
  2712. size = h->u.c.size;
  2713. power_of_two = h->u.c.p->alignment_power;
  2714. section = h->u.c.p->section;
  2715. /* Increase the size of the section to align the common symbol.
  2716. The alignment must be a power of two. But if the section does
  2717. not have any alignment requirement then do not increase the
  2718. alignment unnecessarily. */
  2719. if (power_of_two)
  2720. alignment = bfd_octets_per_byte (output_bfd, section) << power_of_two;
  2721. else
  2722. alignment = 1;
  2723. BFD_ASSERT (alignment != 0 && (alignment & -alignment) == alignment);
  2724. section->size += alignment - 1;
  2725. section->size &= -alignment;
  2726. /* Adjust the section's overall alignment if necessary. */
  2727. if (power_of_two > section->alignment_power)
  2728. section->alignment_power = power_of_two;
  2729. /* Change the symbol from common to defined. */
  2730. h->type = bfd_link_hash_defined;
  2731. h->u.def.section = section;
  2732. h->u.def.value = section->size;
  2733. /* Increase the size of the section. */
  2734. section->size += size;
  2735. /* Make sure the section is allocated in memory, and make sure that
  2736. it is no longer a common section. */
  2737. section->flags |= SEC_ALLOC;
  2738. section->flags &= ~(SEC_IS_COMMON | SEC_HAS_CONTENTS);
  2739. return true;
  2740. }
  2741. /*
  2742. FUNCTION
  2743. _bfd_generic_link_hide_symbol
  2744. SYNOPSIS
  2745. void _bfd_generic_link_hide_symbol
  2746. (bfd *output_bfd, struct bfd_link_info *info,
  2747. struct bfd_link_hash_entry *h);
  2748. DESCRIPTION
  2749. Hide symbol @var{h}.
  2750. This is an internal function. It should not be called from
  2751. outside the BFD library.
  2752. .#define bfd_link_hide_symbol(output_bfd, info, h) \
  2753. . BFD_SEND (output_bfd, _bfd_link_hide_symbol, (output_bfd, info, h))
  2754. .
  2755. */
  2756. void
  2757. _bfd_generic_link_hide_symbol (bfd *output_bfd ATTRIBUTE_UNUSED,
  2758. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2759. struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED)
  2760. {
  2761. }
  2762. /*
  2763. FUNCTION
  2764. bfd_generic_define_start_stop
  2765. SYNOPSIS
  2766. struct bfd_link_hash_entry *bfd_generic_define_start_stop
  2767. (struct bfd_link_info *info,
  2768. const char *symbol, asection *sec);
  2769. DESCRIPTION
  2770. Define a __start, __stop, .startof. or .sizeof. symbol.
  2771. Return the symbol or NULL if no such undefined symbol exists.
  2772. .#define bfd_define_start_stop(output_bfd, info, symbol, sec) \
  2773. . BFD_SEND (output_bfd, _bfd_define_start_stop, (info, symbol, sec))
  2774. .
  2775. */
  2776. struct bfd_link_hash_entry *
  2777. bfd_generic_define_start_stop (struct bfd_link_info *info,
  2778. const char *symbol, asection *sec)
  2779. {
  2780. struct bfd_link_hash_entry *h;
  2781. h = bfd_link_hash_lookup (info->hash, symbol, false, false, true);
  2782. if (h != NULL
  2783. && !h->ldscript_def
  2784. && (h->type == bfd_link_hash_undefined
  2785. || h->type == bfd_link_hash_undefweak))
  2786. {
  2787. h->type = bfd_link_hash_defined;
  2788. h->u.def.section = sec;
  2789. h->u.def.value = 0;
  2790. return h;
  2791. }
  2792. return NULL;
  2793. }
  2794. /*
  2795. FUNCTION
  2796. bfd_find_version_for_sym
  2797. SYNOPSIS
  2798. struct bfd_elf_version_tree * bfd_find_version_for_sym
  2799. (struct bfd_elf_version_tree *verdefs,
  2800. const char *sym_name, bool *hide);
  2801. DESCRIPTION
  2802. Search an elf version script tree for symbol versioning
  2803. info and export / don't-export status for a given symbol.
  2804. Return non-NULL on success and NULL on failure; also sets
  2805. the output @samp{hide} boolean parameter.
  2806. */
  2807. struct bfd_elf_version_tree *
  2808. bfd_find_version_for_sym (struct bfd_elf_version_tree *verdefs,
  2809. const char *sym_name,
  2810. bool *hide)
  2811. {
  2812. struct bfd_elf_version_tree *t;
  2813. struct bfd_elf_version_tree *local_ver, *global_ver, *exist_ver;
  2814. struct bfd_elf_version_tree *star_local_ver, *star_global_ver;
  2815. local_ver = NULL;
  2816. global_ver = NULL;
  2817. star_local_ver = NULL;
  2818. star_global_ver = NULL;
  2819. exist_ver = NULL;
  2820. for (t = verdefs; t != NULL; t = t->next)
  2821. {
  2822. if (t->globals.list != NULL)
  2823. {
  2824. struct bfd_elf_version_expr *d = NULL;
  2825. while ((d = (*t->match) (&t->globals, d, sym_name)) != NULL)
  2826. {
  2827. if (d->literal || strcmp (d->pattern, "*") != 0)
  2828. global_ver = t;
  2829. else
  2830. star_global_ver = t;
  2831. if (d->symver)
  2832. exist_ver = t;
  2833. d->script = 1;
  2834. /* If the match is a wildcard pattern, keep looking for
  2835. a more explicit, perhaps even local, match. */
  2836. if (d->literal)
  2837. break;
  2838. }
  2839. if (d != NULL)
  2840. break;
  2841. }
  2842. if (t->locals.list != NULL)
  2843. {
  2844. struct bfd_elf_version_expr *d = NULL;
  2845. while ((d = (*t->match) (&t->locals, d, sym_name)) != NULL)
  2846. {
  2847. if (d->literal || strcmp (d->pattern, "*") != 0)
  2848. local_ver = t;
  2849. else
  2850. star_local_ver = t;
  2851. /* If the match is a wildcard pattern, keep looking for
  2852. a more explicit, perhaps even global, match. */
  2853. if (d->literal)
  2854. {
  2855. /* An exact match overrides a global wildcard. */
  2856. global_ver = NULL;
  2857. star_global_ver = NULL;
  2858. break;
  2859. }
  2860. }
  2861. if (d != NULL)
  2862. break;
  2863. }
  2864. }
  2865. if (global_ver == NULL && local_ver == NULL)
  2866. global_ver = star_global_ver;
  2867. if (global_ver != NULL)
  2868. {
  2869. /* If we already have a versioned symbol that matches the
  2870. node for this symbol, then we don't want to create a
  2871. duplicate from the unversioned symbol. Instead hide the
  2872. unversioned symbol. */
  2873. *hide = exist_ver == global_ver;
  2874. return global_ver;
  2875. }
  2876. if (local_ver == NULL)
  2877. local_ver = star_local_ver;
  2878. if (local_ver != NULL)
  2879. {
  2880. *hide = true;
  2881. return local_ver;
  2882. }
  2883. return NULL;
  2884. }
  2885. /*
  2886. FUNCTION
  2887. bfd_hide_sym_by_version
  2888. SYNOPSIS
  2889. bool bfd_hide_sym_by_version
  2890. (struct bfd_elf_version_tree *verdefs, const char *sym_name);
  2891. DESCRIPTION
  2892. Search an elf version script tree for symbol versioning
  2893. info for a given symbol. Return TRUE if the symbol is hidden.
  2894. */
  2895. bool
  2896. bfd_hide_sym_by_version (struct bfd_elf_version_tree *verdefs,
  2897. const char *sym_name)
  2898. {
  2899. bool hidden = false;
  2900. bfd_find_version_for_sym (verdefs, sym_name, &hidden);
  2901. return hidden;
  2902. }
  2903. /*
  2904. FUNCTION
  2905. bfd_link_check_relocs
  2906. SYNOPSIS
  2907. bool bfd_link_check_relocs
  2908. (bfd *abfd, struct bfd_link_info *info);
  2909. DESCRIPTION
  2910. Checks the relocs in ABFD for validity.
  2911. Does not execute the relocs.
  2912. Return TRUE if everything is OK, FALSE otherwise.
  2913. This is the external entry point to this code.
  2914. */
  2915. bool
  2916. bfd_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
  2917. {
  2918. return BFD_SEND (abfd, _bfd_link_check_relocs, (abfd, info));
  2919. }
  2920. /*
  2921. FUNCTION
  2922. _bfd_generic_link_check_relocs
  2923. SYNOPSIS
  2924. bool _bfd_generic_link_check_relocs
  2925. (bfd *abfd, struct bfd_link_info *info);
  2926. DESCRIPTION
  2927. Stub function for targets that do not implement reloc checking.
  2928. Return TRUE.
  2929. This is an internal function. It should not be called from
  2930. outside the BFD library.
  2931. */
  2932. bool
  2933. _bfd_generic_link_check_relocs (bfd *abfd ATTRIBUTE_UNUSED,
  2934. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  2935. {
  2936. return true;
  2937. }
  2938. /*
  2939. FUNCTION
  2940. bfd_merge_private_bfd_data
  2941. SYNOPSIS
  2942. bool bfd_merge_private_bfd_data
  2943. (bfd *ibfd, struct bfd_link_info *info);
  2944. DESCRIPTION
  2945. Merge private BFD information from the BFD @var{ibfd} to the
  2946. the output file BFD when linking. Return <<TRUE>> on success,
  2947. <<FALSE>> on error. Possible error returns are:
  2948. o <<bfd_error_no_memory>> -
  2949. Not enough memory exists to create private data for @var{obfd}.
  2950. .#define bfd_merge_private_bfd_data(ibfd, info) \
  2951. . BFD_SEND ((info)->output_bfd, _bfd_merge_private_bfd_data, \
  2952. . (ibfd, info))
  2953. */
  2954. /*
  2955. INTERNAL_FUNCTION
  2956. _bfd_generic_verify_endian_match
  2957. SYNOPSIS
  2958. bool _bfd_generic_verify_endian_match
  2959. (bfd *ibfd, struct bfd_link_info *info);
  2960. DESCRIPTION
  2961. Can be used from / for bfd_merge_private_bfd_data to check that
  2962. endianness matches between input and output file. Returns
  2963. TRUE for a match, otherwise returns FALSE and emits an error.
  2964. */
  2965. bool
  2966. _bfd_generic_verify_endian_match (bfd *ibfd, struct bfd_link_info *info)
  2967. {
  2968. bfd *obfd = info->output_bfd;
  2969. if (ibfd->xvec->byteorder != obfd->xvec->byteorder
  2970. && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
  2971. && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
  2972. {
  2973. if (bfd_big_endian (ibfd))
  2974. _bfd_error_handler (_("%pB: compiled for a big endian system "
  2975. "and target is little endian"), ibfd);
  2976. else
  2977. _bfd_error_handler (_("%pB: compiled for a little endian system "
  2978. "and target is big endian"), ibfd);
  2979. bfd_set_error (bfd_error_wrong_format);
  2980. return false;
  2981. }
  2982. return true;
  2983. }
  2984. int
  2985. _bfd_nolink_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
  2986. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  2987. {
  2988. return 0;
  2989. }
  2990. bool
  2991. _bfd_nolink_bfd_relax_section (bfd *abfd,
  2992. asection *section ATTRIBUTE_UNUSED,
  2993. struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
  2994. bool *again ATTRIBUTE_UNUSED)
  2995. {
  2996. return _bfd_bool_bfd_false_error (abfd);
  2997. }
  2998. bfd_byte *
  2999. _bfd_nolink_bfd_get_relocated_section_contents
  3000. (bfd *abfd,
  3001. struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
  3002. struct bfd_link_order *link_order ATTRIBUTE_UNUSED,
  3003. bfd_byte *data ATTRIBUTE_UNUSED,
  3004. bool relocatable ATTRIBUTE_UNUSED,
  3005. asymbol **symbols ATTRIBUTE_UNUSED)
  3006. {
  3007. return (bfd_byte *) _bfd_ptr_bfd_null_error (abfd);
  3008. }
  3009. bool
  3010. _bfd_nolink_bfd_lookup_section_flags
  3011. (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  3012. struct flag_info *flaginfo ATTRIBUTE_UNUSED,
  3013. asection *section)
  3014. {
  3015. return _bfd_bool_bfd_false_error (section->owner);
  3016. }
  3017. bool
  3018. _bfd_nolink_bfd_is_group_section (bfd *abfd,
  3019. const asection *sec ATTRIBUTE_UNUSED)
  3020. {
  3021. return _bfd_bool_bfd_false_error (abfd);
  3022. }
  3023. const char *
  3024. _bfd_nolink_bfd_group_name (bfd *abfd,
  3025. const asection *sec ATTRIBUTE_UNUSED)
  3026. {
  3027. return _bfd_ptr_bfd_null_error (abfd);
  3028. }
  3029. bool
  3030. _bfd_nolink_bfd_discard_group (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
  3031. {
  3032. return _bfd_bool_bfd_false_error (abfd);
  3033. }
  3034. struct bfd_link_hash_table *
  3035. _bfd_nolink_bfd_link_hash_table_create (bfd *abfd)
  3036. {
  3037. return (struct bfd_link_hash_table *) _bfd_ptr_bfd_null_error (abfd);
  3038. }
  3039. void
  3040. _bfd_nolink_bfd_link_just_syms (asection *sec ATTRIBUTE_UNUSED,
  3041. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  3042. {
  3043. }
  3044. void
  3045. _bfd_nolink_bfd_copy_link_hash_symbol_type
  3046. (bfd *abfd ATTRIBUTE_UNUSED,
  3047. struct bfd_link_hash_entry *from ATTRIBUTE_UNUSED,
  3048. struct bfd_link_hash_entry *to ATTRIBUTE_UNUSED)
  3049. {
  3050. }
  3051. bool
  3052. _bfd_nolink_bfd_link_split_section (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
  3053. {
  3054. return _bfd_bool_bfd_false_error (abfd);
  3055. }
  3056. bool
  3057. _bfd_nolink_section_already_linked (bfd *abfd,
  3058. asection *sec ATTRIBUTE_UNUSED,
  3059. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  3060. {
  3061. return _bfd_bool_bfd_false_error (abfd);
  3062. }
  3063. bool
  3064. _bfd_nolink_bfd_define_common_symbol
  3065. (bfd *abfd,
  3066. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  3067. struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED)
  3068. {
  3069. return _bfd_bool_bfd_false_error (abfd);
  3070. }
  3071. struct bfd_link_hash_entry *
  3072. _bfd_nolink_bfd_define_start_stop (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  3073. const char *name ATTRIBUTE_UNUSED,
  3074. asection *sec)
  3075. {
  3076. return (struct bfd_link_hash_entry *) _bfd_ptr_bfd_null_error (sec->owner);
  3077. }
  3078. /* Return false if linker should avoid caching relocation infomation
  3079. and symbol tables of input files in memory. */
  3080. bool
  3081. _bfd_link_keep_memory (struct bfd_link_info * info)
  3082. {
  3083. bfd *abfd;
  3084. bfd_size_type size;
  3085. if (!info->keep_memory)
  3086. return false;
  3087. if (info->max_cache_size == (bfd_size_type) -1)
  3088. return true;
  3089. abfd = info->input_bfds;
  3090. size = info->cache_size;
  3091. do
  3092. {
  3093. if (size >= info->max_cache_size)
  3094. {
  3095. /* Over the limit. Reduce the memory usage. */
  3096. info->keep_memory = false;
  3097. return false;
  3098. }
  3099. if (!abfd)
  3100. break;
  3101. size += abfd->alloc_size;
  3102. abfd = abfd->link.next;
  3103. }
  3104. while (1);
  3105. return true;
  3106. }