elf32-hppa.c 128 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507
  1. /* BFD back-end for HP PA-RISC ELF files.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. Original code by
  4. Center for Software Science
  5. Department of Computer Science
  6. University of Utah
  7. Largely rewritten by Alan Modra <alan@linuxcare.com.au>
  8. Naming cleanup by Carlos O'Donell <carlos@systemhalted.org>
  9. TLS support written by Randolph Chung <tausq@debian.org>
  10. This file is part of BFD, the Binary File Descriptor library.
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 3 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  22. MA 02110-1301, USA. */
  23. #include "sysdep.h"
  24. #include "bfd.h"
  25. #include "libbfd.h"
  26. #include "elf-bfd.h"
  27. #include "elf/hppa.h"
  28. #include "libhppa.h"
  29. #include "elf32-hppa.h"
  30. #define ARCH_SIZE 32
  31. #include "elf32-hppa.h"
  32. #include "elf-hppa.h"
  33. /* In order to gain some understanding of code in this file without
  34. knowing all the intricate details of the linker, note the
  35. following:
  36. Functions named elf32_hppa_* are called by external routines, other
  37. functions are only called locally. elf32_hppa_* functions appear
  38. in this file more or less in the order in which they are called
  39. from external routines. eg. elf32_hppa_check_relocs is called
  40. early in the link process, elf32_hppa_finish_dynamic_sections is
  41. one of the last functions. */
  42. /* We use two hash tables to hold information for linking PA ELF objects.
  43. The first is the elf32_hppa_link_hash_table which is derived
  44. from the standard ELF linker hash table. We use this as a place to
  45. attach other hash tables and static information.
  46. The second is the stub hash table which is derived from the
  47. base BFD hash table. The stub hash table holds the information
  48. necessary to build the linker stubs during a link.
  49. There are a number of different stubs generated by the linker.
  50. Long branch stub:
  51. : ldil LR'X,%r1
  52. : be,n RR'X(%sr4,%r1)
  53. PIC long branch stub:
  54. : b,l .+8,%r1
  55. : addil LR'X - ($PIC_pcrel$0 - 4),%r1
  56. : be,n RR'X - ($PIC_pcrel$0 - 8)(%sr4,%r1)
  57. Import stub to call shared library routine from normal object file
  58. (single sub-space version)
  59. : addil LR'lt_ptr+ltoff,%dp ; get PLT address
  60. : ldo RR'lt_ptr+ltoff(%r1),%r22 ;
  61. : ldw 0(%r22),%r21 ; get procedure entry point
  62. : bv %r0(%r21)
  63. : ldw 4(%r22),%r19 ; get new dlt value.
  64. Import stub to call shared library routine from shared library
  65. (single sub-space version)
  66. : addil LR'ltoff,%r19 ; get PLT address
  67. : ldo RR'ltoff(%r1),%r22
  68. : ldw 0(%r22),%r21 ; get procedure entry point
  69. : bv %r0(%r21)
  70. : ldw 4(%r22),%r19 ; get new dlt value.
  71. Import stub to call shared library routine from normal object file
  72. (multiple sub-space support)
  73. : addil LR'lt_ptr+ltoff,%dp ; get PLT address
  74. : ldo RR'lt_ptr+ltoff(%r1),%r22 ;
  75. : ldw 0(%r22),%r21 ; get procedure entry point
  76. : ldsid (%r21),%r1 ; get target sid
  77. : ldw 4(%r22),%r19 ; get new dlt value.
  78. : mtsp %r1,%sr0
  79. : be 0(%sr0,%r21) ; branch to target
  80. : stw %rp,-24(%sp) ; save rp
  81. Import stub to call shared library routine from shared library
  82. (multiple sub-space support)
  83. : addil LR'ltoff,%r19 ; get PLT address
  84. : ldo RR'ltoff(%r1),%r22
  85. : ldw 0(%r22),%r21 ; get procedure entry point
  86. : ldsid (%r21),%r1 ; get target sid
  87. : ldw 4(%r22),%r19 ; get new dlt value.
  88. : mtsp %r1,%sr0
  89. : be 0(%sr0,%r21) ; branch to target
  90. : stw %rp,-24(%sp) ; save rp
  91. Export stub to return from shared lib routine (multiple sub-space support)
  92. One of these is created for each exported procedure in a shared
  93. library (and stored in the shared lib). Shared lib routines are
  94. called via the first instruction in the export stub so that we can
  95. do an inter-space return. Not required for single sub-space.
  96. : bl,n X,%rp ; trap the return
  97. : nop
  98. : ldw -24(%sp),%rp ; restore the original rp
  99. : ldsid (%rp),%r1
  100. : mtsp %r1,%sr0
  101. : be,n 0(%sr0,%rp) ; inter-space return. */
  102. /* Variable names follow a coding style.
  103. Please follow this (Apps Hungarian) style:
  104. Structure/Variable Prefix
  105. elf_link_hash_table "etab"
  106. elf_link_hash_entry "eh"
  107. elf32_hppa_link_hash_table "htab"
  108. elf32_hppa_link_hash_entry "hh"
  109. bfd_hash_table "btab"
  110. bfd_hash_entry "bh"
  111. bfd_hash_table containing stubs "bstab"
  112. elf32_hppa_stub_hash_entry "hsh"
  113. Always remember to use GNU Coding Style. */
  114. #define PLT_ENTRY_SIZE 8
  115. #define GOT_ENTRY_SIZE 4
  116. #define LONG_BRANCH_STUB_SIZE 8
  117. #define LONG_BRANCH_SHARED_STUB_SIZE 12
  118. #define IMPORT_STUB_SIZE 20
  119. #define IMPORT_SHARED_STUB_SIZE 32
  120. #define EXPORT_STUB_SIZE 24
  121. #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
  122. static const bfd_byte plt_stub[] =
  123. {
  124. 0x0e, 0x80, 0x10, 0x95, /* 1: ldw 0(%r20),%r21 */
  125. 0xea, 0xa0, 0xc0, 0x00, /* bv %r0(%r21) */
  126. 0x0e, 0x88, 0x10, 0x95, /* ldw 4(%r20),%r21 */
  127. #define PLT_STUB_ENTRY (3*4)
  128. 0xea, 0x9f, 0x1f, 0xdd, /* b,l 1b,%r20 */
  129. 0xd6, 0x80, 0x1c, 0x1e, /* depi 0,31,2,%r20 */
  130. 0x00, 0xc0, 0xff, 0xee, /* 9: .word fixup_func */
  131. 0xde, 0xad, 0xbe, 0xef /* .word fixup_ltp */
  132. };
  133. /* Section name for stubs is the associated section name plus this
  134. string. */
  135. #define STUB_SUFFIX ".stub"
  136. /* We don't need to copy certain PC- or GP-relative dynamic relocs
  137. into a shared object's dynamic section. All the relocs of the
  138. limited class we are interested in, are absolute. */
  139. #ifndef RELATIVE_DYNRELOCS
  140. #define RELATIVE_DYNRELOCS 0
  141. #define IS_ABSOLUTE_RELOC(r_type) 1
  142. #define pc_dynrelocs(hh) 0
  143. #endif
  144. /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
  145. copying dynamic variables from a shared lib into an app's dynbss
  146. section, and instead use a dynamic relocation to point into the
  147. shared lib. */
  148. #define ELIMINATE_COPY_RELOCS 1
  149. enum elf32_hppa_stub_type
  150. {
  151. hppa_stub_long_branch,
  152. hppa_stub_long_branch_shared,
  153. hppa_stub_import,
  154. hppa_stub_import_shared,
  155. hppa_stub_export,
  156. hppa_stub_none
  157. };
  158. struct elf32_hppa_stub_hash_entry
  159. {
  160. /* Base hash table entry structure. */
  161. struct bfd_hash_entry bh_root;
  162. /* The stub section. */
  163. asection *stub_sec;
  164. /* Offset within stub_sec of the beginning of this stub. */
  165. bfd_vma stub_offset;
  166. /* Given the symbol's value and its section we can determine its final
  167. value when building the stubs (so the stub knows where to jump. */
  168. bfd_vma target_value;
  169. asection *target_section;
  170. enum elf32_hppa_stub_type stub_type;
  171. /* The symbol table entry, if any, that this was derived from. */
  172. struct elf32_hppa_link_hash_entry *hh;
  173. /* Where this stub is being called from, or, in the case of combined
  174. stub sections, the first input section in the group. */
  175. asection *id_sec;
  176. };
  177. enum _tls_type
  178. {
  179. GOT_UNKNOWN = 0,
  180. GOT_NORMAL = 1,
  181. GOT_TLS_GD = 2,
  182. GOT_TLS_LDM = 4,
  183. GOT_TLS_IE = 8
  184. };
  185. struct elf32_hppa_link_hash_entry
  186. {
  187. struct elf_link_hash_entry eh;
  188. /* A pointer to the most recently used stub hash entry against this
  189. symbol. */
  190. struct elf32_hppa_stub_hash_entry *hsh_cache;
  191. ENUM_BITFIELD (_tls_type) tls_type : 8;
  192. /* Set if this symbol is used by a plabel reloc. */
  193. unsigned int plabel:1;
  194. };
  195. struct elf32_hppa_link_hash_table
  196. {
  197. /* The main hash table. */
  198. struct elf_link_hash_table etab;
  199. /* The stub hash table. */
  200. struct bfd_hash_table bstab;
  201. /* Linker stub bfd. */
  202. bfd *stub_bfd;
  203. /* Linker call-backs. */
  204. asection * (*add_stub_section) (const char *, asection *);
  205. void (*layout_sections_again) (void);
  206. /* Array to keep track of which stub sections have been created, and
  207. information on stub grouping. */
  208. struct map_stub
  209. {
  210. /* This is the section to which stubs in the group will be
  211. attached. */
  212. asection *link_sec;
  213. /* The stub section. */
  214. asection *stub_sec;
  215. } *stub_group;
  216. /* Assorted information used by elf32_hppa_size_stubs. */
  217. unsigned int bfd_count;
  218. unsigned int top_index;
  219. asection **input_list;
  220. Elf_Internal_Sym **all_local_syms;
  221. /* Used during a final link to store the base of the text and data
  222. segments so that we can perform SEGREL relocations. */
  223. bfd_vma text_segment_base;
  224. bfd_vma data_segment_base;
  225. /* Whether we support multiple sub-spaces for shared libs. */
  226. unsigned int multi_subspace:1;
  227. /* Flags set when various size branches are detected. Used to
  228. select suitable defaults for the stub group size. */
  229. unsigned int has_12bit_branch:1;
  230. unsigned int has_17bit_branch:1;
  231. unsigned int has_22bit_branch:1;
  232. /* Set if we need a .plt stub to support lazy dynamic linking. */
  233. unsigned int need_plt_stub:1;
  234. /* Data for LDM relocations. */
  235. union
  236. {
  237. bfd_signed_vma refcount;
  238. bfd_vma offset;
  239. } tls_ldm_got;
  240. };
  241. /* Various hash macros and functions. */
  242. #define hppa_link_hash_table(p) \
  243. ((is_elf_hash_table ((p)->hash) \
  244. && elf_hash_table_id (elf_hash_table (p)) == HPPA32_ELF_DATA) \
  245. ? (struct elf32_hppa_link_hash_table *) (p)->hash : NULL)
  246. #define hppa_elf_hash_entry(ent) \
  247. ((struct elf32_hppa_link_hash_entry *)(ent))
  248. #define hppa_stub_hash_entry(ent) \
  249. ((struct elf32_hppa_stub_hash_entry *)(ent))
  250. #define hppa_stub_hash_lookup(table, string, create, copy) \
  251. ((struct elf32_hppa_stub_hash_entry *) \
  252. bfd_hash_lookup ((table), (string), (create), (copy)))
  253. #define hppa_elf_local_got_tls_type(abfd) \
  254. ((char *)(elf_local_got_offsets (abfd) + (elf_tdata (abfd)->symtab_hdr.sh_info * 2)))
  255. #define hh_name(hh) \
  256. (hh ? hh->eh.root.root.string : "<undef>")
  257. #define eh_name(eh) \
  258. (eh ? eh->root.root.string : "<undef>")
  259. /* Assorted hash table functions. */
  260. /* Initialize an entry in the stub hash table. */
  261. static struct bfd_hash_entry *
  262. stub_hash_newfunc (struct bfd_hash_entry *entry,
  263. struct bfd_hash_table *table,
  264. const char *string)
  265. {
  266. /* Allocate the structure if it has not already been allocated by a
  267. subclass. */
  268. if (entry == NULL)
  269. {
  270. entry = bfd_hash_allocate (table,
  271. sizeof (struct elf32_hppa_stub_hash_entry));
  272. if (entry == NULL)
  273. return entry;
  274. }
  275. /* Call the allocation method of the superclass. */
  276. entry = bfd_hash_newfunc (entry, table, string);
  277. if (entry != NULL)
  278. {
  279. struct elf32_hppa_stub_hash_entry *hsh;
  280. /* Initialize the local fields. */
  281. hsh = hppa_stub_hash_entry (entry);
  282. hsh->stub_sec = NULL;
  283. hsh->stub_offset = 0;
  284. hsh->target_value = 0;
  285. hsh->target_section = NULL;
  286. hsh->stub_type = hppa_stub_long_branch;
  287. hsh->hh = NULL;
  288. hsh->id_sec = NULL;
  289. }
  290. return entry;
  291. }
  292. /* Initialize an entry in the link hash table. */
  293. static struct bfd_hash_entry *
  294. hppa_link_hash_newfunc (struct bfd_hash_entry *entry,
  295. struct bfd_hash_table *table,
  296. const char *string)
  297. {
  298. /* Allocate the structure if it has not already been allocated by a
  299. subclass. */
  300. if (entry == NULL)
  301. {
  302. entry = bfd_hash_allocate (table,
  303. sizeof (struct elf32_hppa_link_hash_entry));
  304. if (entry == NULL)
  305. return entry;
  306. }
  307. /* Call the allocation method of the superclass. */
  308. entry = _bfd_elf_link_hash_newfunc (entry, table, string);
  309. if (entry != NULL)
  310. {
  311. struct elf32_hppa_link_hash_entry *hh;
  312. /* Initialize the local fields. */
  313. hh = hppa_elf_hash_entry (entry);
  314. hh->hsh_cache = NULL;
  315. hh->plabel = 0;
  316. hh->tls_type = GOT_UNKNOWN;
  317. }
  318. return entry;
  319. }
  320. /* Free the derived linker hash table. */
  321. static void
  322. elf32_hppa_link_hash_table_free (bfd *obfd)
  323. {
  324. struct elf32_hppa_link_hash_table *htab
  325. = (struct elf32_hppa_link_hash_table *) obfd->link.hash;
  326. bfd_hash_table_free (&htab->bstab);
  327. _bfd_elf_link_hash_table_free (obfd);
  328. }
  329. /* Create the derived linker hash table. The PA ELF port uses the derived
  330. hash table to keep information specific to the PA ELF linker (without
  331. using static variables). */
  332. static struct bfd_link_hash_table *
  333. elf32_hppa_link_hash_table_create (bfd *abfd)
  334. {
  335. struct elf32_hppa_link_hash_table *htab;
  336. size_t amt = sizeof (*htab);
  337. htab = bfd_zmalloc (amt);
  338. if (htab == NULL)
  339. return NULL;
  340. if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd, hppa_link_hash_newfunc,
  341. sizeof (struct elf32_hppa_link_hash_entry),
  342. HPPA32_ELF_DATA))
  343. {
  344. free (htab);
  345. return NULL;
  346. }
  347. /* Init the stub hash table too. */
  348. if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
  349. sizeof (struct elf32_hppa_stub_hash_entry)))
  350. {
  351. _bfd_elf_link_hash_table_free (abfd);
  352. return NULL;
  353. }
  354. htab->etab.root.hash_table_free = elf32_hppa_link_hash_table_free;
  355. htab->etab.dt_pltgot_required = true;
  356. htab->text_segment_base = (bfd_vma) -1;
  357. htab->data_segment_base = (bfd_vma) -1;
  358. return &htab->etab.root;
  359. }
  360. /* Initialize the linker stubs BFD so that we can use it for linker
  361. created dynamic sections. */
  362. void
  363. elf32_hppa_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
  364. {
  365. struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
  366. elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS32;
  367. htab->etab.dynobj = abfd;
  368. }
  369. /* Build a name for an entry in the stub hash table. */
  370. static char *
  371. hppa_stub_name (const asection *input_section,
  372. const asection *sym_sec,
  373. const struct elf32_hppa_link_hash_entry *hh,
  374. const Elf_Internal_Rela *rela)
  375. {
  376. char *stub_name;
  377. bfd_size_type len;
  378. if (hh)
  379. {
  380. len = 8 + 1 + strlen (hh_name (hh)) + 1 + 8 + 1;
  381. stub_name = bfd_malloc (len);
  382. if (stub_name != NULL)
  383. sprintf (stub_name, "%08x_%s+%x",
  384. input_section->id & 0xffffffff,
  385. hh_name (hh),
  386. (int) rela->r_addend & 0xffffffff);
  387. }
  388. else
  389. {
  390. len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
  391. stub_name = bfd_malloc (len);
  392. if (stub_name != NULL)
  393. sprintf (stub_name, "%08x_%x:%x+%x",
  394. input_section->id & 0xffffffff,
  395. sym_sec->id & 0xffffffff,
  396. (int) ELF32_R_SYM (rela->r_info) & 0xffffffff,
  397. (int) rela->r_addend & 0xffffffff);
  398. }
  399. return stub_name;
  400. }
  401. /* Look up an entry in the stub hash. Stub entries are cached because
  402. creating the stub name takes a bit of time. */
  403. static struct elf32_hppa_stub_hash_entry *
  404. hppa_get_stub_entry (const asection *input_section,
  405. const asection *sym_sec,
  406. struct elf32_hppa_link_hash_entry *hh,
  407. const Elf_Internal_Rela *rela,
  408. struct elf32_hppa_link_hash_table *htab)
  409. {
  410. struct elf32_hppa_stub_hash_entry *hsh_entry;
  411. const asection *id_sec;
  412. /* If this input section is part of a group of sections sharing one
  413. stub section, then use the id of the first section in the group.
  414. Stub names need to include a section id, as there may well be
  415. more than one stub used to reach say, printf, and we need to
  416. distinguish between them. */
  417. id_sec = htab->stub_group[input_section->id].link_sec;
  418. if (id_sec == NULL)
  419. return NULL;
  420. if (hh != NULL && hh->hsh_cache != NULL
  421. && hh->hsh_cache->hh == hh
  422. && hh->hsh_cache->id_sec == id_sec)
  423. {
  424. hsh_entry = hh->hsh_cache;
  425. }
  426. else
  427. {
  428. char *stub_name;
  429. stub_name = hppa_stub_name (id_sec, sym_sec, hh, rela);
  430. if (stub_name == NULL)
  431. return NULL;
  432. hsh_entry = hppa_stub_hash_lookup (&htab->bstab,
  433. stub_name, false, false);
  434. if (hh != NULL)
  435. hh->hsh_cache = hsh_entry;
  436. free (stub_name);
  437. }
  438. return hsh_entry;
  439. }
  440. /* Add a new stub entry to the stub hash. Not all fields of the new
  441. stub entry are initialised. */
  442. static struct elf32_hppa_stub_hash_entry *
  443. hppa_add_stub (const char *stub_name,
  444. asection *section,
  445. struct elf32_hppa_link_hash_table *htab)
  446. {
  447. asection *link_sec;
  448. asection *stub_sec;
  449. struct elf32_hppa_stub_hash_entry *hsh;
  450. link_sec = htab->stub_group[section->id].link_sec;
  451. stub_sec = htab->stub_group[section->id].stub_sec;
  452. if (stub_sec == NULL)
  453. {
  454. stub_sec = htab->stub_group[link_sec->id].stub_sec;
  455. if (stub_sec == NULL)
  456. {
  457. size_t namelen;
  458. bfd_size_type len;
  459. char *s_name;
  460. namelen = strlen (link_sec->name);
  461. len = namelen + sizeof (STUB_SUFFIX);
  462. s_name = bfd_alloc (htab->stub_bfd, len);
  463. if (s_name == NULL)
  464. return NULL;
  465. memcpy (s_name, link_sec->name, namelen);
  466. memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
  467. stub_sec = (*htab->add_stub_section) (s_name, link_sec);
  468. if (stub_sec == NULL)
  469. return NULL;
  470. htab->stub_group[link_sec->id].stub_sec = stub_sec;
  471. }
  472. htab->stub_group[section->id].stub_sec = stub_sec;
  473. }
  474. /* Enter this entry into the linker stub hash table. */
  475. hsh = hppa_stub_hash_lookup (&htab->bstab, stub_name,
  476. true, false);
  477. if (hsh == NULL)
  478. {
  479. /* xgettext:c-format */
  480. _bfd_error_handler (_("%pB: cannot create stub entry %s"),
  481. section->owner, stub_name);
  482. return NULL;
  483. }
  484. hsh->stub_sec = stub_sec;
  485. hsh->stub_offset = 0;
  486. hsh->id_sec = link_sec;
  487. return hsh;
  488. }
  489. /* Determine the type of stub needed, if any, for a call. */
  490. static enum elf32_hppa_stub_type
  491. hppa_type_of_stub (asection *input_sec,
  492. const Elf_Internal_Rela *rela,
  493. struct elf32_hppa_link_hash_entry *hh,
  494. bfd_vma destination,
  495. struct bfd_link_info *info)
  496. {
  497. bfd_vma location;
  498. bfd_vma branch_offset;
  499. bfd_vma max_branch_offset;
  500. unsigned int r_type;
  501. if (hh != NULL
  502. && hh->eh.plt.offset != (bfd_vma) -1
  503. && hh->eh.dynindx != -1
  504. && !hh->plabel
  505. && (bfd_link_pic (info)
  506. || !hh->eh.def_regular
  507. || hh->eh.root.type == bfd_link_hash_defweak))
  508. {
  509. /* We need an import stub. Decide between hppa_stub_import
  510. and hppa_stub_import_shared later. */
  511. return hppa_stub_import;
  512. }
  513. if (destination == (bfd_vma) -1)
  514. return hppa_stub_none;
  515. /* Determine where the call point is. */
  516. location = (input_sec->output_offset
  517. + input_sec->output_section->vma
  518. + rela->r_offset);
  519. branch_offset = destination - location - 8;
  520. r_type = ELF32_R_TYPE (rela->r_info);
  521. /* Determine if a long branch stub is needed. parisc branch offsets
  522. are relative to the second instruction past the branch, ie. +8
  523. bytes on from the branch instruction location. The offset is
  524. signed and counts in units of 4 bytes. */
  525. if (r_type == (unsigned int) R_PARISC_PCREL17F)
  526. max_branch_offset = (1 << (17 - 1)) << 2;
  527. else if (r_type == (unsigned int) R_PARISC_PCREL12F)
  528. max_branch_offset = (1 << (12 - 1)) << 2;
  529. else /* R_PARISC_PCREL22F. */
  530. max_branch_offset = (1 << (22 - 1)) << 2;
  531. if (branch_offset + max_branch_offset >= 2*max_branch_offset)
  532. return hppa_stub_long_branch;
  533. return hppa_stub_none;
  534. }
  535. /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
  536. IN_ARG contains the link info pointer. */
  537. #define LDIL_R1 0x20200000 /* ldil LR'XXX,%r1 */
  538. #define BE_SR4_R1 0xe0202002 /* be,n RR'XXX(%sr4,%r1) */
  539. #define BL_R1 0xe8200000 /* b,l .+8,%r1 */
  540. #define ADDIL_R1 0x28200000 /* addil LR'XXX,%r1,%r1 */
  541. #define DEPI_R1 0xd4201c1e /* depi 0,31,2,%r1 */
  542. #define ADDIL_DP 0x2b600000 /* addil LR'XXX,%dp,%r1 */
  543. #define LDW_R1_R21 0x48350000 /* ldw RR'XXX(%sr0,%r1),%r21 */
  544. #define BV_R0_R21 0xeaa0c000 /* bv %r0(%r21) */
  545. #define LDW_R1_R19 0x48330000 /* ldw RR'XXX(%sr0,%r1),%r19 */
  546. #define ADDIL_R19 0x2a600000 /* addil LR'XXX,%r19,%r1 */
  547. #define LDW_R1_DP 0x483b0000 /* ldw RR'XXX(%sr0,%r1),%dp */
  548. #define LDO_R1_R22 0x34360000 /* ldo RR'XXX(%r1),%r22 */
  549. #define LDW_R22_R21 0x0ec01095 /* ldw 0(%r22),%r21 */
  550. #define LDW_R22_R19 0x0ec81093 /* ldw 4(%r22),%r19 */
  551. #define LDSID_R21_R1 0x02a010a1 /* ldsid (%sr0,%r21),%r1 */
  552. #define MTSP_R1 0x00011820 /* mtsp %r1,%sr0 */
  553. #define BE_SR0_R21 0xe2a00000 /* be 0(%sr0,%r21) */
  554. #define STW_RP 0x6bc23fd1 /* stw %rp,-24(%sr0,%sp) */
  555. #define BL22_RP 0xe800a002 /* b,l,n XXX,%rp */
  556. #define BL_RP 0xe8400002 /* b,l,n XXX,%rp */
  557. #define NOP 0x08000240 /* nop */
  558. #define LDW_RP 0x4bc23fd1 /* ldw -24(%sr0,%sp),%rp */
  559. #define LDSID_RP_R1 0x004010a1 /* ldsid (%sr0,%rp),%r1 */
  560. #define BE_SR0_RP 0xe0400002 /* be,n 0(%sr0,%rp) */
  561. #ifndef R19_STUBS
  562. #define R19_STUBS 1
  563. #endif
  564. #if R19_STUBS
  565. #define LDW_R1_DLT LDW_R1_R19
  566. #else
  567. #define LDW_R1_DLT LDW_R1_DP
  568. #endif
  569. static bool
  570. hppa_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
  571. {
  572. struct elf32_hppa_stub_hash_entry *hsh;
  573. struct bfd_link_info *info;
  574. struct elf32_hppa_link_hash_table *htab;
  575. asection *stub_sec;
  576. bfd *stub_bfd;
  577. bfd_byte *loc;
  578. bfd_vma sym_value;
  579. bfd_vma insn;
  580. bfd_vma off;
  581. int val;
  582. int size;
  583. /* Massage our args to the form they really have. */
  584. hsh = hppa_stub_hash_entry (bh);
  585. info = (struct bfd_link_info *)in_arg;
  586. htab = hppa_link_hash_table (info);
  587. if (htab == NULL)
  588. return false;
  589. stub_sec = hsh->stub_sec;
  590. /* Make a note of the offset within the stubs for this entry. */
  591. hsh->stub_offset = stub_sec->size;
  592. loc = stub_sec->contents + hsh->stub_offset;
  593. stub_bfd = stub_sec->owner;
  594. switch (hsh->stub_type)
  595. {
  596. case hppa_stub_long_branch:
  597. /* Fail if the target section could not be assigned to an output
  598. section. The user should fix his linker script. */
  599. if (hsh->target_section->output_section == NULL
  600. && info->non_contiguous_regions)
  601. info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output "
  602. "section. Retry without "
  603. "--enable-non-contiguous-regions.\n"),
  604. hsh->target_section);
  605. /* Create the long branch. A long branch is formed with "ldil"
  606. loading the upper bits of the target address into a register,
  607. then branching with "be" which adds in the lower bits.
  608. The "be" has its delay slot nullified. */
  609. sym_value = (hsh->target_value
  610. + hsh->target_section->output_offset
  611. + hsh->target_section->output_section->vma);
  612. val = hppa_field_adjust (sym_value, 0, e_lrsel);
  613. insn = hppa_rebuild_insn ((int) LDIL_R1, val, 21);
  614. bfd_put_32 (stub_bfd, insn, loc);
  615. val = hppa_field_adjust (sym_value, 0, e_rrsel) >> 2;
  616. insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
  617. bfd_put_32 (stub_bfd, insn, loc + 4);
  618. size = LONG_BRANCH_STUB_SIZE;
  619. break;
  620. case hppa_stub_long_branch_shared:
  621. /* Fail if the target section could not be assigned to an output
  622. section. The user should fix his linker script. */
  623. if (hsh->target_section->output_section == NULL
  624. && info->non_contiguous_regions)
  625. info->callbacks->einfo (_("%F%P: Could not assign %pA to an output "
  626. "section. Retry without "
  627. "--enable-non-contiguous-regions.\n"),
  628. hsh->target_section);
  629. /* Branches are relative. This is where we are going to. */
  630. sym_value = (hsh->target_value
  631. + hsh->target_section->output_offset
  632. + hsh->target_section->output_section->vma);
  633. /* And this is where we are coming from, more or less. */
  634. sym_value -= (hsh->stub_offset
  635. + stub_sec->output_offset
  636. + stub_sec->output_section->vma);
  637. bfd_put_32 (stub_bfd, (bfd_vma) BL_R1, loc);
  638. val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_lrsel);
  639. insn = hppa_rebuild_insn ((int) ADDIL_R1, val, 21);
  640. bfd_put_32 (stub_bfd, insn, loc + 4);
  641. val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_rrsel) >> 2;
  642. insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
  643. bfd_put_32 (stub_bfd, insn, loc + 8);
  644. size = LONG_BRANCH_SHARED_STUB_SIZE;
  645. break;
  646. case hppa_stub_import:
  647. case hppa_stub_import_shared:
  648. off = hsh->hh->eh.plt.offset;
  649. if (off >= (bfd_vma) -2)
  650. abort ();
  651. off &= ~ (bfd_vma) 1;
  652. sym_value = (off
  653. + htab->etab.splt->output_offset
  654. + htab->etab.splt->output_section->vma
  655. - elf_gp (htab->etab.splt->output_section->owner));
  656. insn = ADDIL_DP;
  657. #if R19_STUBS
  658. if (hsh->stub_type == hppa_stub_import_shared)
  659. insn = ADDIL_R19;
  660. #endif
  661. /* Load function descriptor address into register %r22. It is
  662. sometimes needed for lazy binding. */
  663. val = hppa_field_adjust (sym_value, 0, e_lrsel),
  664. insn = hppa_rebuild_insn ((int) insn, val, 21);
  665. bfd_put_32 (stub_bfd, insn, loc);
  666. val = hppa_field_adjust (sym_value, 0, e_rrsel);
  667. insn = hppa_rebuild_insn ((int) LDO_R1_R22, val, 14);
  668. bfd_put_32 (stub_bfd, insn, loc + 4);
  669. bfd_put_32 (stub_bfd, (bfd_vma) LDW_R22_R21, loc + 8);
  670. if (htab->multi_subspace)
  671. {
  672. bfd_put_32 (stub_bfd, (bfd_vma) LDSID_R21_R1, loc + 12);
  673. bfd_put_32 (stub_bfd, (bfd_vma) LDW_R22_R19, loc + 16);
  674. bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1, loc + 20);
  675. bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_R21, loc + 24);
  676. bfd_put_32 (stub_bfd, (bfd_vma) STW_RP, loc + 28);
  677. size = IMPORT_SHARED_STUB_SIZE;
  678. }
  679. else
  680. {
  681. bfd_put_32 (stub_bfd, (bfd_vma) BV_R0_R21, loc + 12);
  682. bfd_put_32 (stub_bfd, (bfd_vma) LDW_R22_R19, loc + 16);
  683. size = IMPORT_STUB_SIZE;
  684. }
  685. break;
  686. case hppa_stub_export:
  687. /* Fail if the target section could not be assigned to an output
  688. section. The user should fix his linker script. */
  689. if (hsh->target_section->output_section == NULL
  690. && info->non_contiguous_regions)
  691. info->callbacks->einfo (_("%F%P: Could not assign %pA to an output "
  692. "section. Retry without "
  693. "--enable-non-contiguous-regions.\n"),
  694. hsh->target_section);
  695. /* Branches are relative. This is where we are going to. */
  696. sym_value = (hsh->target_value
  697. + hsh->target_section->output_offset
  698. + hsh->target_section->output_section->vma);
  699. /* And this is where we are coming from. */
  700. sym_value -= (hsh->stub_offset
  701. + stub_sec->output_offset
  702. + stub_sec->output_section->vma);
  703. if (sym_value - 8 + (1 << (17 + 1)) >= (1 << (17 + 2))
  704. && (!htab->has_22bit_branch
  705. || sym_value - 8 + (1 << (22 + 1)) >= (1 << (22 + 2))))
  706. {
  707. _bfd_error_handler
  708. /* xgettext:c-format */
  709. (_("%pB(%pA+%#" PRIx64 "): "
  710. "cannot reach %s, recompile with -ffunction-sections"),
  711. hsh->target_section->owner,
  712. stub_sec,
  713. (uint64_t) hsh->stub_offset,
  714. hsh->bh_root.string);
  715. bfd_set_error (bfd_error_bad_value);
  716. return false;
  717. }
  718. val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_fsel) >> 2;
  719. if (!htab->has_22bit_branch)
  720. insn = hppa_rebuild_insn ((int) BL_RP, val, 17);
  721. else
  722. insn = hppa_rebuild_insn ((int) BL22_RP, val, 22);
  723. bfd_put_32 (stub_bfd, insn, loc);
  724. bfd_put_32 (stub_bfd, (bfd_vma) NOP, loc + 4);
  725. bfd_put_32 (stub_bfd, (bfd_vma) LDW_RP, loc + 8);
  726. bfd_put_32 (stub_bfd, (bfd_vma) LDSID_RP_R1, loc + 12);
  727. bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1, loc + 16);
  728. bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_RP, loc + 20);
  729. /* Point the function symbol at the stub. */
  730. hsh->hh->eh.root.u.def.section = stub_sec;
  731. hsh->hh->eh.root.u.def.value = stub_sec->size;
  732. size = EXPORT_STUB_SIZE;
  733. break;
  734. default:
  735. BFD_FAIL ();
  736. return false;
  737. }
  738. stub_sec->size += size;
  739. return true;
  740. }
  741. #undef LDIL_R1
  742. #undef BE_SR4_R1
  743. #undef BL_R1
  744. #undef ADDIL_R1
  745. #undef DEPI_R1
  746. #undef LDW_R1_R21
  747. #undef LDW_R1_DLT
  748. #undef LDW_R1_R19
  749. #undef ADDIL_R19
  750. #undef LDW_R1_DP
  751. #undef LDSID_R21_R1
  752. #undef MTSP_R1
  753. #undef BE_SR0_R21
  754. #undef STW_RP
  755. #undef BV_R0_R21
  756. #undef BL_RP
  757. #undef NOP
  758. #undef LDW_RP
  759. #undef LDSID_RP_R1
  760. #undef BE_SR0_RP
  761. /* As above, but don't actually build the stub. Just bump offset so
  762. we know stub section sizes. */
  763. static bool
  764. hppa_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
  765. {
  766. struct elf32_hppa_stub_hash_entry *hsh;
  767. struct elf32_hppa_link_hash_table *htab;
  768. int size;
  769. /* Massage our args to the form they really have. */
  770. hsh = hppa_stub_hash_entry (bh);
  771. htab = in_arg;
  772. if (hsh->stub_type == hppa_stub_long_branch)
  773. size = LONG_BRANCH_STUB_SIZE;
  774. else if (hsh->stub_type == hppa_stub_long_branch_shared)
  775. size = LONG_BRANCH_SHARED_STUB_SIZE;
  776. else if (hsh->stub_type == hppa_stub_export)
  777. size = EXPORT_STUB_SIZE;
  778. else /* hppa_stub_import or hppa_stub_import_shared. */
  779. {
  780. if (htab->multi_subspace)
  781. size = IMPORT_SHARED_STUB_SIZE;
  782. else
  783. size = IMPORT_STUB_SIZE;
  784. }
  785. hsh->stub_sec->size += size;
  786. return true;
  787. }
  788. /* Return nonzero if ABFD represents an HPPA ELF32 file.
  789. Additionally we set the default architecture and machine. */
  790. static bool
  791. elf32_hppa_object_p (bfd *abfd)
  792. {
  793. Elf_Internal_Ehdr * i_ehdrp;
  794. unsigned int flags;
  795. i_ehdrp = elf_elfheader (abfd);
  796. if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
  797. {
  798. /* GCC on hppa-linux produces binaries with OSABI=GNU,
  799. but the kernel produces corefiles with OSABI=SysV. */
  800. if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU &&
  801. i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
  802. return false;
  803. }
  804. else if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0)
  805. {
  806. /* GCC on hppa-netbsd produces binaries with OSABI=NetBSD,
  807. but the kernel produces corefiles with OSABI=SysV. */
  808. if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NETBSD &&
  809. i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
  810. return false;
  811. }
  812. else
  813. {
  814. if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX)
  815. return false;
  816. }
  817. flags = i_ehdrp->e_flags;
  818. switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
  819. {
  820. case EFA_PARISC_1_0:
  821. return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
  822. case EFA_PARISC_1_1:
  823. return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
  824. case EFA_PARISC_2_0:
  825. return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
  826. case EFA_PARISC_2_0 | EF_PARISC_WIDE:
  827. return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
  828. }
  829. return true;
  830. }
  831. /* Create the .plt and .got sections, and set up our hash table
  832. short-cuts to various dynamic sections. */
  833. static bool
  834. elf32_hppa_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  835. {
  836. struct elf32_hppa_link_hash_table *htab;
  837. struct elf_link_hash_entry *eh;
  838. /* Don't try to create the .plt and .got twice. */
  839. htab = hppa_link_hash_table (info);
  840. if (htab == NULL)
  841. return false;
  842. if (htab->etab.splt != NULL)
  843. return true;
  844. /* Call the generic code to do most of the work. */
  845. if (! _bfd_elf_create_dynamic_sections (abfd, info))
  846. return false;
  847. /* hppa-linux needs _GLOBAL_OFFSET_TABLE_ to be visible from the main
  848. application, because __canonicalize_funcptr_for_compare needs it. */
  849. eh = elf_hash_table (info)->hgot;
  850. eh->forced_local = 0;
  851. eh->other = STV_DEFAULT;
  852. return bfd_elf_link_record_dynamic_symbol (info, eh);
  853. }
  854. /* Copy the extra info we tack onto an elf_link_hash_entry. */
  855. static void
  856. elf32_hppa_copy_indirect_symbol (struct bfd_link_info *info,
  857. struct elf_link_hash_entry *eh_dir,
  858. struct elf_link_hash_entry *eh_ind)
  859. {
  860. struct elf32_hppa_link_hash_entry *hh_dir, *hh_ind;
  861. hh_dir = hppa_elf_hash_entry (eh_dir);
  862. hh_ind = hppa_elf_hash_entry (eh_ind);
  863. if (eh_ind->root.type == bfd_link_hash_indirect)
  864. {
  865. hh_dir->plabel |= hh_ind->plabel;
  866. hh_dir->tls_type |= hh_ind->tls_type;
  867. hh_ind->tls_type = GOT_UNKNOWN;
  868. }
  869. _bfd_elf_link_hash_copy_indirect (info, eh_dir, eh_ind);
  870. }
  871. static int
  872. elf32_hppa_optimized_tls_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  873. int r_type, int is_local ATTRIBUTE_UNUSED)
  874. {
  875. /* For now we don't support linker optimizations. */
  876. return r_type;
  877. }
  878. /* Return a pointer to the local GOT, PLT and TLS reference counts
  879. for ABFD. Returns NULL if the storage allocation fails. */
  880. static bfd_signed_vma *
  881. hppa32_elf_local_refcounts (bfd *abfd)
  882. {
  883. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  884. bfd_signed_vma *local_refcounts;
  885. local_refcounts = elf_local_got_refcounts (abfd);
  886. if (local_refcounts == NULL)
  887. {
  888. bfd_size_type size;
  889. /* Allocate space for local GOT and PLT reference
  890. counts. Done this way to save polluting elf_obj_tdata
  891. with another target specific pointer. */
  892. size = symtab_hdr->sh_info;
  893. size *= 2 * sizeof (bfd_signed_vma);
  894. /* Add in space to store the local GOT TLS types. */
  895. size += symtab_hdr->sh_info;
  896. local_refcounts = bfd_zalloc (abfd, size);
  897. if (local_refcounts == NULL)
  898. return NULL;
  899. elf_local_got_refcounts (abfd) = local_refcounts;
  900. memset (hppa_elf_local_got_tls_type (abfd), GOT_UNKNOWN,
  901. symtab_hdr->sh_info);
  902. }
  903. return local_refcounts;
  904. }
  905. /* Look through the relocs for a section during the first phase, and
  906. calculate needed space in the global offset table, procedure linkage
  907. table, and dynamic reloc sections. At this point we haven't
  908. necessarily read all the input files. */
  909. static bool
  910. elf32_hppa_check_relocs (bfd *abfd,
  911. struct bfd_link_info *info,
  912. asection *sec,
  913. const Elf_Internal_Rela *relocs)
  914. {
  915. Elf_Internal_Shdr *symtab_hdr;
  916. struct elf_link_hash_entry **eh_syms;
  917. const Elf_Internal_Rela *rela;
  918. const Elf_Internal_Rela *rela_end;
  919. struct elf32_hppa_link_hash_table *htab;
  920. asection *sreloc;
  921. if (bfd_link_relocatable (info))
  922. return true;
  923. htab = hppa_link_hash_table (info);
  924. if (htab == NULL)
  925. return false;
  926. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  927. eh_syms = elf_sym_hashes (abfd);
  928. sreloc = NULL;
  929. rela_end = relocs + sec->reloc_count;
  930. for (rela = relocs; rela < rela_end; rela++)
  931. {
  932. enum {
  933. NEED_GOT = 1,
  934. NEED_PLT = 2,
  935. NEED_DYNREL = 4,
  936. PLT_PLABEL = 8
  937. };
  938. unsigned int r_symndx, r_type;
  939. struct elf32_hppa_link_hash_entry *hh;
  940. int need_entry = 0;
  941. r_symndx = ELF32_R_SYM (rela->r_info);
  942. if (r_symndx < symtab_hdr->sh_info)
  943. hh = NULL;
  944. else
  945. {
  946. hh = hppa_elf_hash_entry (eh_syms[r_symndx - symtab_hdr->sh_info]);
  947. while (hh->eh.root.type == bfd_link_hash_indirect
  948. || hh->eh.root.type == bfd_link_hash_warning)
  949. hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
  950. }
  951. r_type = ELF32_R_TYPE (rela->r_info);
  952. r_type = elf32_hppa_optimized_tls_reloc (info, r_type, hh == NULL);
  953. switch (r_type)
  954. {
  955. case R_PARISC_DLTIND14F:
  956. case R_PARISC_DLTIND14R:
  957. case R_PARISC_DLTIND21L:
  958. /* This symbol requires a global offset table entry. */
  959. need_entry = NEED_GOT;
  960. break;
  961. case R_PARISC_PLABEL14R: /* "Official" procedure labels. */
  962. case R_PARISC_PLABEL21L:
  963. case R_PARISC_PLABEL32:
  964. /* If the addend is non-zero, we break badly. */
  965. if (rela->r_addend != 0)
  966. abort ();
  967. /* If we are creating a shared library, then we need to
  968. create a PLT entry for all PLABELs, because PLABELs with
  969. local symbols may be passed via a pointer to another
  970. object. Additionally, output a dynamic relocation
  971. pointing to the PLT entry.
  972. For executables, the original 32-bit ABI allowed two
  973. different styles of PLABELs (function pointers): For
  974. global functions, the PLABEL word points into the .plt
  975. two bytes past a (function address, gp) pair, and for
  976. local functions the PLABEL points directly at the
  977. function. The magic +2 for the first type allows us to
  978. differentiate between the two. As you can imagine, this
  979. is a real pain when it comes to generating code to call
  980. functions indirectly or to compare function pointers.
  981. We avoid the mess by always pointing a PLABEL into the
  982. .plt, even for local functions. */
  983. need_entry = PLT_PLABEL | NEED_PLT;
  984. if (bfd_link_pic (info))
  985. need_entry |= NEED_DYNREL;
  986. break;
  987. case R_PARISC_PCREL12F:
  988. htab->has_12bit_branch = 1;
  989. goto branch_common;
  990. case R_PARISC_PCREL17C:
  991. case R_PARISC_PCREL17F:
  992. htab->has_17bit_branch = 1;
  993. goto branch_common;
  994. case R_PARISC_PCREL22F:
  995. htab->has_22bit_branch = 1;
  996. branch_common:
  997. /* Function calls might need to go through the .plt, and
  998. might require long branch stubs. */
  999. if (hh == NULL)
  1000. {
  1001. /* We know local syms won't need a .plt entry, and if
  1002. they need a long branch stub we can't guarantee that
  1003. we can reach the stub. So just flag an error later
  1004. if we're doing a shared link and find we need a long
  1005. branch stub. */
  1006. continue;
  1007. }
  1008. else
  1009. {
  1010. /* Global symbols will need a .plt entry if they remain
  1011. global, and in most cases won't need a long branch
  1012. stub. Unfortunately, we have to cater for the case
  1013. where a symbol is forced local by versioning, or due
  1014. to symbolic linking, and we lose the .plt entry. */
  1015. need_entry = NEED_PLT;
  1016. if (hh->eh.type == STT_PARISC_MILLI)
  1017. need_entry = 0;
  1018. }
  1019. break;
  1020. case R_PARISC_SEGBASE: /* Used to set segment base. */
  1021. case R_PARISC_SEGREL32: /* Relative reloc, used for unwind. */
  1022. case R_PARISC_PCREL14F: /* PC relative load/store. */
  1023. case R_PARISC_PCREL14R:
  1024. case R_PARISC_PCREL17R: /* External branches. */
  1025. case R_PARISC_PCREL21L: /* As above, and for load/store too. */
  1026. case R_PARISC_PCREL32:
  1027. /* We don't need to propagate the relocation if linking a
  1028. shared object since these are section relative. */
  1029. continue;
  1030. case R_PARISC_DPREL14F: /* Used for gp rel data load/store. */
  1031. case R_PARISC_DPREL14R:
  1032. case R_PARISC_DPREL21L:
  1033. if (bfd_link_pic (info))
  1034. {
  1035. _bfd_error_handler
  1036. /* xgettext:c-format */
  1037. (_("%pB: relocation %s can not be used when making a shared object; recompile with -fPIC"),
  1038. abfd,
  1039. elf_hppa_howto_table[r_type].name);
  1040. bfd_set_error (bfd_error_bad_value);
  1041. return false;
  1042. }
  1043. /* Fall through. */
  1044. case R_PARISC_DIR17F: /* Used for external branches. */
  1045. case R_PARISC_DIR17R:
  1046. case R_PARISC_DIR14F: /* Used for load/store from absolute locn. */
  1047. case R_PARISC_DIR14R:
  1048. case R_PARISC_DIR21L: /* As above, and for ext branches too. */
  1049. case R_PARISC_DIR32: /* .word relocs. */
  1050. /* We may want to output a dynamic relocation later. */
  1051. need_entry = NEED_DYNREL;
  1052. break;
  1053. /* This relocation describes the C++ object vtable hierarchy.
  1054. Reconstruct it for later use during GC. */
  1055. case R_PARISC_GNU_VTINHERIT:
  1056. if (!bfd_elf_gc_record_vtinherit (abfd, sec, &hh->eh, rela->r_offset))
  1057. return false;
  1058. continue;
  1059. /* This relocation describes which C++ vtable entries are actually
  1060. used. Record for later use during GC. */
  1061. case R_PARISC_GNU_VTENTRY:
  1062. if (!bfd_elf_gc_record_vtentry (abfd, sec, &hh->eh, rela->r_addend))
  1063. return false;
  1064. continue;
  1065. case R_PARISC_TLS_GD21L:
  1066. case R_PARISC_TLS_GD14R:
  1067. case R_PARISC_TLS_LDM21L:
  1068. case R_PARISC_TLS_LDM14R:
  1069. need_entry = NEED_GOT;
  1070. break;
  1071. case R_PARISC_TLS_IE21L:
  1072. case R_PARISC_TLS_IE14R:
  1073. if (bfd_link_dll (info))
  1074. info->flags |= DF_STATIC_TLS;
  1075. need_entry = NEED_GOT;
  1076. break;
  1077. default:
  1078. continue;
  1079. }
  1080. /* Now carry out our orders. */
  1081. if (need_entry & NEED_GOT)
  1082. {
  1083. int tls_type = GOT_NORMAL;
  1084. switch (r_type)
  1085. {
  1086. default:
  1087. break;
  1088. case R_PARISC_TLS_GD21L:
  1089. case R_PARISC_TLS_GD14R:
  1090. tls_type = GOT_TLS_GD;
  1091. break;
  1092. case R_PARISC_TLS_LDM21L:
  1093. case R_PARISC_TLS_LDM14R:
  1094. tls_type = GOT_TLS_LDM;
  1095. break;
  1096. case R_PARISC_TLS_IE21L:
  1097. case R_PARISC_TLS_IE14R:
  1098. tls_type = GOT_TLS_IE;
  1099. break;
  1100. }
  1101. /* Allocate space for a GOT entry, as well as a dynamic
  1102. relocation for this entry. */
  1103. if (htab->etab.sgot == NULL)
  1104. {
  1105. if (!elf32_hppa_create_dynamic_sections (htab->etab.dynobj, info))
  1106. return false;
  1107. }
  1108. if (hh != NULL)
  1109. {
  1110. if (tls_type == GOT_TLS_LDM)
  1111. htab->tls_ldm_got.refcount += 1;
  1112. else
  1113. hh->eh.got.refcount += 1;
  1114. hh->tls_type |= tls_type;
  1115. }
  1116. else
  1117. {
  1118. bfd_signed_vma *local_got_refcounts;
  1119. /* This is a global offset table entry for a local symbol. */
  1120. local_got_refcounts = hppa32_elf_local_refcounts (abfd);
  1121. if (local_got_refcounts == NULL)
  1122. return false;
  1123. if (tls_type == GOT_TLS_LDM)
  1124. htab->tls_ldm_got.refcount += 1;
  1125. else
  1126. local_got_refcounts[r_symndx] += 1;
  1127. hppa_elf_local_got_tls_type (abfd) [r_symndx] |= tls_type;
  1128. }
  1129. }
  1130. if (need_entry & NEED_PLT)
  1131. {
  1132. /* If we are creating a shared library, and this is a reloc
  1133. against a weak symbol or a global symbol in a dynamic
  1134. object, then we will be creating an import stub and a
  1135. .plt entry for the symbol. Similarly, on a normal link
  1136. to symbols defined in a dynamic object we'll need the
  1137. import stub and a .plt entry. We don't know yet whether
  1138. the symbol is defined or not, so make an entry anyway and
  1139. clean up later in adjust_dynamic_symbol. */
  1140. if ((sec->flags & SEC_ALLOC) != 0)
  1141. {
  1142. if (hh != NULL)
  1143. {
  1144. hh->eh.needs_plt = 1;
  1145. hh->eh.plt.refcount += 1;
  1146. /* If this .plt entry is for a plabel, mark it so
  1147. that adjust_dynamic_symbol will keep the entry
  1148. even if it appears to be local. */
  1149. if (need_entry & PLT_PLABEL)
  1150. hh->plabel = 1;
  1151. }
  1152. else if (need_entry & PLT_PLABEL)
  1153. {
  1154. bfd_signed_vma *local_got_refcounts;
  1155. bfd_signed_vma *local_plt_refcounts;
  1156. local_got_refcounts = hppa32_elf_local_refcounts (abfd);
  1157. if (local_got_refcounts == NULL)
  1158. return false;
  1159. local_plt_refcounts = (local_got_refcounts
  1160. + symtab_hdr->sh_info);
  1161. local_plt_refcounts[r_symndx] += 1;
  1162. }
  1163. }
  1164. }
  1165. if ((need_entry & NEED_DYNREL) != 0
  1166. && (sec->flags & SEC_ALLOC) != 0)
  1167. {
  1168. /* Flag this symbol as having a non-got, non-plt reference
  1169. so that we generate copy relocs if it turns out to be
  1170. dynamic. */
  1171. if (hh != NULL)
  1172. hh->eh.non_got_ref = 1;
  1173. /* If we are creating a shared library then we need to copy
  1174. the reloc into the shared library. However, if we are
  1175. linking with -Bsymbolic, we need only copy absolute
  1176. relocs or relocs against symbols that are not defined in
  1177. an object we are including in the link. PC- or DP- or
  1178. DLT-relative relocs against any local sym or global sym
  1179. with DEF_REGULAR set, can be discarded. At this point we
  1180. have not seen all the input files, so it is possible that
  1181. DEF_REGULAR is not set now but will be set later (it is
  1182. never cleared). We account for that possibility below by
  1183. storing information in the dyn_relocs field of the
  1184. hash table entry.
  1185. A similar situation to the -Bsymbolic case occurs when
  1186. creating shared libraries and symbol visibility changes
  1187. render the symbol local.
  1188. As it turns out, all the relocs we will be creating here
  1189. are absolute, so we cannot remove them on -Bsymbolic
  1190. links or visibility changes anyway. A STUB_REL reloc
  1191. is absolute too, as in that case it is the reloc in the
  1192. stub we will be creating, rather than copying the PCREL
  1193. reloc in the branch.
  1194. If on the other hand, we are creating an executable, we
  1195. may need to keep relocations for symbols satisfied by a
  1196. dynamic library if we manage to avoid copy relocs for the
  1197. symbol. */
  1198. if ((bfd_link_pic (info)
  1199. && (IS_ABSOLUTE_RELOC (r_type)
  1200. || (hh != NULL
  1201. && (!SYMBOLIC_BIND (info, &hh->eh)
  1202. || hh->eh.root.type == bfd_link_hash_defweak
  1203. || !hh->eh.def_regular))))
  1204. || (ELIMINATE_COPY_RELOCS
  1205. && !bfd_link_pic (info)
  1206. && hh != NULL
  1207. && (hh->eh.root.type == bfd_link_hash_defweak
  1208. || !hh->eh.def_regular)))
  1209. {
  1210. struct elf_dyn_relocs *hdh_p;
  1211. struct elf_dyn_relocs **hdh_head;
  1212. /* Create a reloc section in dynobj and make room for
  1213. this reloc. */
  1214. if (sreloc == NULL)
  1215. {
  1216. sreloc = _bfd_elf_make_dynamic_reloc_section
  1217. (sec, htab->etab.dynobj, 2, abfd, /*rela?*/ true);
  1218. if (sreloc == NULL)
  1219. {
  1220. bfd_set_error (bfd_error_bad_value);
  1221. return false;
  1222. }
  1223. }
  1224. /* If this is a global symbol, we count the number of
  1225. relocations we need for this symbol. */
  1226. if (hh != NULL)
  1227. {
  1228. hdh_head = &hh->eh.dyn_relocs;
  1229. }
  1230. else
  1231. {
  1232. /* Track dynamic relocs needed for local syms too.
  1233. We really need local syms available to do this
  1234. easily. Oh well. */
  1235. asection *sr;
  1236. void *vpp;
  1237. Elf_Internal_Sym *isym;
  1238. isym = bfd_sym_from_r_symndx (&htab->etab.sym_cache,
  1239. abfd, r_symndx);
  1240. if (isym == NULL)
  1241. return false;
  1242. sr = bfd_section_from_elf_index (abfd, isym->st_shndx);
  1243. if (sr == NULL)
  1244. sr = sec;
  1245. vpp = &elf_section_data (sr)->local_dynrel;
  1246. hdh_head = (struct elf_dyn_relocs **) vpp;
  1247. }
  1248. hdh_p = *hdh_head;
  1249. if (hdh_p == NULL || hdh_p->sec != sec)
  1250. {
  1251. hdh_p = bfd_alloc (htab->etab.dynobj, sizeof *hdh_p);
  1252. if (hdh_p == NULL)
  1253. return false;
  1254. hdh_p->next = *hdh_head;
  1255. *hdh_head = hdh_p;
  1256. hdh_p->sec = sec;
  1257. hdh_p->count = 0;
  1258. #if RELATIVE_DYNRELOCS
  1259. hdh_p->pc_count = 0;
  1260. #endif
  1261. }
  1262. hdh_p->count += 1;
  1263. #if RELATIVE_DYNRELOCS
  1264. if (!IS_ABSOLUTE_RELOC (rtype))
  1265. hdh_p->pc_count += 1;
  1266. #endif
  1267. }
  1268. }
  1269. }
  1270. return true;
  1271. }
  1272. /* Return the section that should be marked against garbage collection
  1273. for a given relocation. */
  1274. static asection *
  1275. elf32_hppa_gc_mark_hook (asection *sec,
  1276. struct bfd_link_info *info,
  1277. Elf_Internal_Rela *rela,
  1278. struct elf_link_hash_entry *hh,
  1279. Elf_Internal_Sym *sym)
  1280. {
  1281. if (hh != NULL)
  1282. switch ((unsigned int) ELF32_R_TYPE (rela->r_info))
  1283. {
  1284. case R_PARISC_GNU_VTINHERIT:
  1285. case R_PARISC_GNU_VTENTRY:
  1286. return NULL;
  1287. }
  1288. return _bfd_elf_gc_mark_hook (sec, info, rela, hh, sym);
  1289. }
  1290. /* Support for core dump NOTE sections. */
  1291. static bool
  1292. elf32_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  1293. {
  1294. int offset;
  1295. size_t size;
  1296. switch (note->descsz)
  1297. {
  1298. default:
  1299. return false;
  1300. case 396: /* Linux/hppa */
  1301. /* pr_cursig */
  1302. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  1303. /* pr_pid */
  1304. elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
  1305. /* pr_reg */
  1306. offset = 72;
  1307. size = 320;
  1308. break;
  1309. }
  1310. /* Make a ".reg/999" section. */
  1311. return _bfd_elfcore_make_pseudosection (abfd, ".reg",
  1312. size, note->descpos + offset);
  1313. }
  1314. static bool
  1315. elf32_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  1316. {
  1317. switch (note->descsz)
  1318. {
  1319. default:
  1320. return false;
  1321. case 124: /* Linux/hppa elf_prpsinfo. */
  1322. elf_tdata (abfd)->core->program
  1323. = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
  1324. elf_tdata (abfd)->core->command
  1325. = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
  1326. }
  1327. /* Note that for some reason, a spurious space is tacked
  1328. onto the end of the args in some (at least one anyway)
  1329. implementations, so strip it off if it exists. */
  1330. {
  1331. char *command = elf_tdata (abfd)->core->command;
  1332. int n = strlen (command);
  1333. if (0 < n && command[n - 1] == ' ')
  1334. command[n - 1] = '\0';
  1335. }
  1336. return true;
  1337. }
  1338. /* Our own version of hide_symbol, so that we can keep plt entries for
  1339. plabels. */
  1340. static void
  1341. elf32_hppa_hide_symbol (struct bfd_link_info *info,
  1342. struct elf_link_hash_entry *eh,
  1343. bool force_local)
  1344. {
  1345. if (force_local)
  1346. {
  1347. eh->forced_local = 1;
  1348. if (eh->dynindx != -1)
  1349. {
  1350. eh->dynindx = -1;
  1351. _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
  1352. eh->dynstr_index);
  1353. }
  1354. /* PR 16082: Remove version information from hidden symbol. */
  1355. eh->verinfo.verdef = NULL;
  1356. eh->verinfo.vertree = NULL;
  1357. }
  1358. /* STT_GNU_IFUNC symbol must go through PLT. */
  1359. if (! hppa_elf_hash_entry (eh)->plabel
  1360. && eh->type != STT_GNU_IFUNC)
  1361. {
  1362. eh->needs_plt = 0;
  1363. eh->plt = elf_hash_table (info)->init_plt_offset;
  1364. }
  1365. }
  1366. /* Return true if we have dynamic relocs against H or any of its weak
  1367. aliases, that apply to read-only sections. Cannot be used after
  1368. size_dynamic_sections. */
  1369. static bool
  1370. alias_readonly_dynrelocs (struct elf_link_hash_entry *eh)
  1371. {
  1372. struct elf32_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
  1373. do
  1374. {
  1375. if (_bfd_elf_readonly_dynrelocs (&hh->eh))
  1376. return true;
  1377. hh = hppa_elf_hash_entry (hh->eh.u.alias);
  1378. } while (hh != NULL && &hh->eh != eh);
  1379. return false;
  1380. }
  1381. /* Adjust a symbol defined by a dynamic object and referenced by a
  1382. regular object. The current definition is in some section of the
  1383. dynamic object, but we're not including those sections. We have to
  1384. change the definition to something the rest of the link can
  1385. understand. */
  1386. static bool
  1387. elf32_hppa_adjust_dynamic_symbol (struct bfd_link_info *info,
  1388. struct elf_link_hash_entry *eh)
  1389. {
  1390. struct elf32_hppa_link_hash_table *htab;
  1391. asection *sec, *srel;
  1392. /* If this is a function, put it in the procedure linkage table. We
  1393. will fill in the contents of the procedure linkage table later. */
  1394. if (eh->type == STT_FUNC
  1395. || eh->needs_plt)
  1396. {
  1397. bool local = (SYMBOL_CALLS_LOCAL (info, eh)
  1398. || UNDEFWEAK_NO_DYNAMIC_RELOC (info, eh));
  1399. /* Discard dyn_relocs when non-pic if we've decided that a
  1400. function symbol is local. */
  1401. if (!bfd_link_pic (info) && local)
  1402. eh->dyn_relocs = NULL;
  1403. /* If the symbol is used by a plabel, we must allocate a PLT slot.
  1404. The refcounts are not reliable when it has been hidden since
  1405. hide_symbol can be called before the plabel flag is set. */
  1406. if (hppa_elf_hash_entry (eh)->plabel)
  1407. eh->plt.refcount = 1;
  1408. /* Note that unlike some other backends, the refcount is not
  1409. incremented for a non-call (and non-plabel) function reference. */
  1410. else if (eh->plt.refcount <= 0
  1411. || local)
  1412. {
  1413. /* The .plt entry is not needed when:
  1414. a) Garbage collection has removed all references to the
  1415. symbol, or
  1416. b) We know for certain the symbol is defined in this
  1417. object, and it's not a weak definition, nor is the symbol
  1418. used by a plabel relocation. Either this object is the
  1419. application or we are doing a shared symbolic link. */
  1420. eh->plt.offset = (bfd_vma) -1;
  1421. eh->needs_plt = 0;
  1422. }
  1423. /* Unlike other targets, elf32-hppa.c does not define a function
  1424. symbol in a non-pic executable on PLT stub code, so we don't
  1425. have a local definition in that case. ie. dyn_relocs can't
  1426. be discarded. */
  1427. /* Function symbols can't have copy relocs. */
  1428. return true;
  1429. }
  1430. else
  1431. eh->plt.offset = (bfd_vma) -1;
  1432. htab = hppa_link_hash_table (info);
  1433. if (htab == NULL)
  1434. return false;
  1435. /* If this is a weak symbol, and there is a real definition, the
  1436. processor independent code will have arranged for us to see the
  1437. real definition first, and we can just use the same value. */
  1438. if (eh->is_weakalias)
  1439. {
  1440. struct elf_link_hash_entry *def = weakdef (eh);
  1441. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  1442. eh->root.u.def.section = def->root.u.def.section;
  1443. eh->root.u.def.value = def->root.u.def.value;
  1444. if (def->root.u.def.section == htab->etab.sdynbss
  1445. || def->root.u.def.section == htab->etab.sdynrelro)
  1446. eh->dyn_relocs = NULL;
  1447. return true;
  1448. }
  1449. /* This is a reference to a symbol defined by a dynamic object which
  1450. is not a function. */
  1451. /* If we are creating a shared library, we must presume that the
  1452. only references to the symbol are via the global offset table.
  1453. For such cases we need not do anything here; the relocations will
  1454. be handled correctly by relocate_section. */
  1455. if (bfd_link_pic (info))
  1456. return true;
  1457. /* If there are no references to this symbol that do not use the
  1458. GOT, we don't need to generate a copy reloc. */
  1459. if (!eh->non_got_ref)
  1460. return true;
  1461. /* If -z nocopyreloc was given, we won't generate them either. */
  1462. if (info->nocopyreloc)
  1463. return true;
  1464. /* If we don't find any dynamic relocs in read-only sections, then
  1465. we'll be keeping the dynamic relocs and avoiding the copy reloc. */
  1466. if (ELIMINATE_COPY_RELOCS
  1467. && !alias_readonly_dynrelocs (eh))
  1468. return true;
  1469. /* We must allocate the symbol in our .dynbss section, which will
  1470. become part of the .bss section of the executable. There will be
  1471. an entry for this symbol in the .dynsym section. The dynamic
  1472. object will contain position independent code, so all references
  1473. from the dynamic object to this symbol will go through the global
  1474. offset table. The dynamic linker will use the .dynsym entry to
  1475. determine the address it must put in the global offset table, so
  1476. both the dynamic object and the regular object will refer to the
  1477. same memory location for the variable. */
  1478. if ((eh->root.u.def.section->flags & SEC_READONLY) != 0)
  1479. {
  1480. sec = htab->etab.sdynrelro;
  1481. srel = htab->etab.sreldynrelro;
  1482. }
  1483. else
  1484. {
  1485. sec = htab->etab.sdynbss;
  1486. srel = htab->etab.srelbss;
  1487. }
  1488. if ((eh->root.u.def.section->flags & SEC_ALLOC) != 0 && eh->size != 0)
  1489. {
  1490. /* We must generate a COPY reloc to tell the dynamic linker to
  1491. copy the initial value out of the dynamic object and into the
  1492. runtime process image. */
  1493. srel->size += sizeof (Elf32_External_Rela);
  1494. eh->needs_copy = 1;
  1495. }
  1496. /* We no longer want dyn_relocs. */
  1497. eh->dyn_relocs = NULL;
  1498. return _bfd_elf_adjust_dynamic_copy (info, eh, sec);
  1499. }
  1500. /* If EH is undefined, make it dynamic if that makes sense. */
  1501. static bool
  1502. ensure_undef_dynamic (struct bfd_link_info *info,
  1503. struct elf_link_hash_entry *eh)
  1504. {
  1505. struct elf_link_hash_table *htab = elf_hash_table (info);
  1506. if (htab->dynamic_sections_created
  1507. && (eh->root.type == bfd_link_hash_undefweak
  1508. || eh->root.type == bfd_link_hash_undefined)
  1509. && eh->dynindx == -1
  1510. && !eh->forced_local
  1511. && eh->type != STT_PARISC_MILLI
  1512. && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, eh)
  1513. && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT)
  1514. return bfd_elf_link_record_dynamic_symbol (info, eh);
  1515. return true;
  1516. }
  1517. /* Allocate space in the .plt for entries that won't have relocations.
  1518. ie. plabel entries. */
  1519. static bool
  1520. allocate_plt_static (struct elf_link_hash_entry *eh, void *inf)
  1521. {
  1522. struct bfd_link_info *info;
  1523. struct elf32_hppa_link_hash_table *htab;
  1524. struct elf32_hppa_link_hash_entry *hh;
  1525. asection *sec;
  1526. if (eh->root.type == bfd_link_hash_indirect)
  1527. return true;
  1528. info = (struct bfd_link_info *) inf;
  1529. hh = hppa_elf_hash_entry (eh);
  1530. htab = hppa_link_hash_table (info);
  1531. if (htab == NULL)
  1532. return false;
  1533. if (htab->etab.dynamic_sections_created
  1534. && eh->plt.refcount > 0)
  1535. {
  1536. if (!ensure_undef_dynamic (info, eh))
  1537. return false;
  1538. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), eh))
  1539. {
  1540. /* Allocate these later. From this point on, h->plabel
  1541. means that the plt entry is only used by a plabel.
  1542. We'll be using a normal plt entry for this symbol, so
  1543. clear the plabel indicator. */
  1544. hh->plabel = 0;
  1545. }
  1546. else if (hh->plabel)
  1547. {
  1548. /* Make an entry in the .plt section for plabel references
  1549. that won't have a .plt entry for other reasons. */
  1550. sec = htab->etab.splt;
  1551. eh->plt.offset = sec->size;
  1552. sec->size += PLT_ENTRY_SIZE;
  1553. if (bfd_link_pic (info))
  1554. htab->etab.srelplt->size += sizeof (Elf32_External_Rela);
  1555. }
  1556. else
  1557. {
  1558. /* No .plt entry needed. */
  1559. eh->plt.offset = (bfd_vma) -1;
  1560. eh->needs_plt = 0;
  1561. }
  1562. }
  1563. else
  1564. {
  1565. eh->plt.offset = (bfd_vma) -1;
  1566. eh->needs_plt = 0;
  1567. }
  1568. return true;
  1569. }
  1570. /* Calculate size of GOT entries for symbol given its TLS_TYPE. */
  1571. static inline unsigned int
  1572. got_entries_needed (int tls_type)
  1573. {
  1574. unsigned int need = 0;
  1575. if ((tls_type & GOT_NORMAL) != 0)
  1576. need += GOT_ENTRY_SIZE;
  1577. if ((tls_type & GOT_TLS_GD) != 0)
  1578. need += GOT_ENTRY_SIZE * 2;
  1579. if ((tls_type & GOT_TLS_IE) != 0)
  1580. need += GOT_ENTRY_SIZE;
  1581. return need;
  1582. }
  1583. /* Calculate size of relocs needed for symbol given its TLS_TYPE and
  1584. NEEDed GOT entries. TPREL_KNOWN says a TPREL offset can be
  1585. calculated at link time. DTPREL_KNOWN says the same for a DTPREL
  1586. offset. */
  1587. static inline unsigned int
  1588. got_relocs_needed (int tls_type, unsigned int need,
  1589. bool dtprel_known, bool tprel_known)
  1590. {
  1591. /* All the entries we allocated need relocs.
  1592. Except for GD and IE with local symbols. */
  1593. if ((tls_type & GOT_TLS_GD) != 0 && dtprel_known)
  1594. need -= GOT_ENTRY_SIZE;
  1595. if ((tls_type & GOT_TLS_IE) != 0 && tprel_known)
  1596. need -= GOT_ENTRY_SIZE;
  1597. return need * sizeof (Elf32_External_Rela) / GOT_ENTRY_SIZE;
  1598. }
  1599. /* Allocate space in .plt, .got and associated reloc sections for
  1600. global syms. */
  1601. static bool
  1602. allocate_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
  1603. {
  1604. struct bfd_link_info *info;
  1605. struct elf32_hppa_link_hash_table *htab;
  1606. asection *sec;
  1607. struct elf32_hppa_link_hash_entry *hh;
  1608. struct elf_dyn_relocs *hdh_p;
  1609. if (eh->root.type == bfd_link_hash_indirect)
  1610. return true;
  1611. info = inf;
  1612. htab = hppa_link_hash_table (info);
  1613. if (htab == NULL)
  1614. return false;
  1615. hh = hppa_elf_hash_entry (eh);
  1616. if (htab->etab.dynamic_sections_created
  1617. && eh->plt.offset != (bfd_vma) -1
  1618. && !hh->plabel
  1619. && eh->plt.refcount > 0)
  1620. {
  1621. /* Make an entry in the .plt section. */
  1622. sec = htab->etab.splt;
  1623. eh->plt.offset = sec->size;
  1624. sec->size += PLT_ENTRY_SIZE;
  1625. /* We also need to make an entry in the .rela.plt section. */
  1626. htab->etab.srelplt->size += sizeof (Elf32_External_Rela);
  1627. htab->need_plt_stub = 1;
  1628. }
  1629. if (eh->got.refcount > 0)
  1630. {
  1631. unsigned int need;
  1632. if (!ensure_undef_dynamic (info, eh))
  1633. return false;
  1634. sec = htab->etab.sgot;
  1635. eh->got.offset = sec->size;
  1636. need = got_entries_needed (hh->tls_type);
  1637. sec->size += need;
  1638. if (htab->etab.dynamic_sections_created
  1639. && (bfd_link_dll (info)
  1640. || (bfd_link_pic (info) && (hh->tls_type & GOT_NORMAL) != 0)
  1641. || (eh->dynindx != -1
  1642. && !SYMBOL_REFERENCES_LOCAL (info, eh)))
  1643. && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, eh))
  1644. {
  1645. bool local = SYMBOL_REFERENCES_LOCAL (info, eh);
  1646. htab->etab.srelgot->size
  1647. += got_relocs_needed (hh->tls_type, need, local,
  1648. local && bfd_link_executable (info));
  1649. }
  1650. }
  1651. else
  1652. eh->got.offset = (bfd_vma) -1;
  1653. /* If no dynamic sections we can't have dynamic relocs. */
  1654. if (!htab->etab.dynamic_sections_created)
  1655. eh->dyn_relocs = NULL;
  1656. /* Discard relocs on undefined syms with non-default visibility. */
  1657. else if ((eh->root.type == bfd_link_hash_undefined
  1658. && ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT)
  1659. || UNDEFWEAK_NO_DYNAMIC_RELOC (info, eh))
  1660. eh->dyn_relocs = NULL;
  1661. if (eh->dyn_relocs == NULL)
  1662. return true;
  1663. /* If this is a -Bsymbolic shared link, then we need to discard all
  1664. space allocated for dynamic pc-relative relocs against symbols
  1665. defined in a regular object. For the normal shared case, discard
  1666. space for relocs that have become local due to symbol visibility
  1667. changes. */
  1668. if (bfd_link_pic (info))
  1669. {
  1670. #if RELATIVE_DYNRELOCS
  1671. if (SYMBOL_CALLS_LOCAL (info, eh))
  1672. {
  1673. struct elf_dyn_relocs **hdh_pp;
  1674. for (hdh_pp = &eh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
  1675. {
  1676. hdh_p->count -= hdh_p->pc_count;
  1677. hdh_p->pc_count = 0;
  1678. if (hdh_p->count == 0)
  1679. *hdh_pp = hdh_p->next;
  1680. else
  1681. hdh_pp = &hdh_p->next;
  1682. }
  1683. }
  1684. #endif
  1685. if (eh->dyn_relocs != NULL)
  1686. {
  1687. if (!ensure_undef_dynamic (info, eh))
  1688. return false;
  1689. }
  1690. }
  1691. else if (ELIMINATE_COPY_RELOCS)
  1692. {
  1693. /* For the non-shared case, discard space for relocs against
  1694. symbols which turn out to need copy relocs or are not
  1695. dynamic. */
  1696. if (eh->dynamic_adjusted
  1697. && !eh->def_regular
  1698. && !ELF_COMMON_DEF_P (eh))
  1699. {
  1700. if (!ensure_undef_dynamic (info, eh))
  1701. return false;
  1702. if (eh->dynindx == -1)
  1703. eh->dyn_relocs = NULL;
  1704. }
  1705. else
  1706. eh->dyn_relocs = NULL;
  1707. }
  1708. /* Finally, allocate space. */
  1709. for (hdh_p = eh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->next)
  1710. {
  1711. asection *sreloc = elf_section_data (hdh_p->sec)->sreloc;
  1712. sreloc->size += hdh_p->count * sizeof (Elf32_External_Rela);
  1713. }
  1714. return true;
  1715. }
  1716. /* This function is called via elf_link_hash_traverse to force
  1717. millicode symbols local so they do not end up as globals in the
  1718. dynamic symbol table. We ought to be able to do this in
  1719. adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
  1720. for all dynamic symbols. Arguably, this is a bug in
  1721. elf_adjust_dynamic_symbol. */
  1722. static bool
  1723. clobber_millicode_symbols (struct elf_link_hash_entry *eh,
  1724. void *info)
  1725. {
  1726. if (eh->type == STT_PARISC_MILLI
  1727. && !eh->forced_local)
  1728. elf32_hppa_hide_symbol ((struct bfd_link_info *) info, eh, true);
  1729. return true;
  1730. }
  1731. /* Set the sizes of the dynamic sections. */
  1732. static bool
  1733. elf32_hppa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  1734. struct bfd_link_info *info)
  1735. {
  1736. struct elf32_hppa_link_hash_table *htab;
  1737. bfd *dynobj;
  1738. bfd *ibfd;
  1739. asection *sec;
  1740. bool relocs;
  1741. htab = hppa_link_hash_table (info);
  1742. if (htab == NULL)
  1743. return false;
  1744. dynobj = htab->etab.dynobj;
  1745. if (dynobj == NULL)
  1746. abort ();
  1747. if (htab->etab.dynamic_sections_created)
  1748. {
  1749. /* Set the contents of the .interp section to the interpreter. */
  1750. if (bfd_link_executable (info) && !info->nointerp)
  1751. {
  1752. sec = bfd_get_linker_section (dynobj, ".interp");
  1753. if (sec == NULL)
  1754. abort ();
  1755. sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
  1756. sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  1757. }
  1758. /* Force millicode symbols local. */
  1759. elf_link_hash_traverse (&htab->etab,
  1760. clobber_millicode_symbols,
  1761. info);
  1762. }
  1763. /* Set up .got and .plt offsets for local syms, and space for local
  1764. dynamic relocs. */
  1765. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  1766. {
  1767. bfd_signed_vma *local_got;
  1768. bfd_signed_vma *end_local_got;
  1769. bfd_signed_vma *local_plt;
  1770. bfd_signed_vma *end_local_plt;
  1771. bfd_size_type locsymcount;
  1772. Elf_Internal_Shdr *symtab_hdr;
  1773. asection *srel;
  1774. char *local_tls_type;
  1775. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
  1776. continue;
  1777. for (sec = ibfd->sections; sec != NULL; sec = sec->next)
  1778. {
  1779. struct elf_dyn_relocs *hdh_p;
  1780. for (hdh_p = ((struct elf_dyn_relocs *)
  1781. elf_section_data (sec)->local_dynrel);
  1782. hdh_p != NULL;
  1783. hdh_p = hdh_p->next)
  1784. {
  1785. if (!bfd_is_abs_section (hdh_p->sec)
  1786. && bfd_is_abs_section (hdh_p->sec->output_section))
  1787. {
  1788. /* Input section has been discarded, either because
  1789. it is a copy of a linkonce section or due to
  1790. linker script /DISCARD/, so we'll be discarding
  1791. the relocs too. */
  1792. }
  1793. else if (hdh_p->count != 0)
  1794. {
  1795. srel = elf_section_data (hdh_p->sec)->sreloc;
  1796. srel->size += hdh_p->count * sizeof (Elf32_External_Rela);
  1797. if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
  1798. info->flags |= DF_TEXTREL;
  1799. }
  1800. }
  1801. }
  1802. local_got = elf_local_got_refcounts (ibfd);
  1803. if (!local_got)
  1804. continue;
  1805. symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
  1806. locsymcount = symtab_hdr->sh_info;
  1807. end_local_got = local_got + locsymcount;
  1808. local_tls_type = hppa_elf_local_got_tls_type (ibfd);
  1809. sec = htab->etab.sgot;
  1810. srel = htab->etab.srelgot;
  1811. for (; local_got < end_local_got; ++local_got)
  1812. {
  1813. if (*local_got > 0)
  1814. {
  1815. unsigned int need;
  1816. *local_got = sec->size;
  1817. need = got_entries_needed (*local_tls_type);
  1818. sec->size += need;
  1819. if (bfd_link_dll (info)
  1820. || (bfd_link_pic (info)
  1821. && (*local_tls_type & GOT_NORMAL) != 0))
  1822. htab->etab.srelgot->size
  1823. += got_relocs_needed (*local_tls_type, need, true,
  1824. bfd_link_executable (info));
  1825. }
  1826. else
  1827. *local_got = (bfd_vma) -1;
  1828. ++local_tls_type;
  1829. }
  1830. local_plt = end_local_got;
  1831. end_local_plt = local_plt + locsymcount;
  1832. if (! htab->etab.dynamic_sections_created)
  1833. {
  1834. /* Won't be used, but be safe. */
  1835. for (; local_plt < end_local_plt; ++local_plt)
  1836. *local_plt = (bfd_vma) -1;
  1837. }
  1838. else
  1839. {
  1840. sec = htab->etab.splt;
  1841. srel = htab->etab.srelplt;
  1842. for (; local_plt < end_local_plt; ++local_plt)
  1843. {
  1844. if (*local_plt > 0)
  1845. {
  1846. *local_plt = sec->size;
  1847. sec->size += PLT_ENTRY_SIZE;
  1848. if (bfd_link_pic (info))
  1849. srel->size += sizeof (Elf32_External_Rela);
  1850. }
  1851. else
  1852. *local_plt = (bfd_vma) -1;
  1853. }
  1854. }
  1855. }
  1856. if (htab->tls_ldm_got.refcount > 0)
  1857. {
  1858. /* Allocate 2 got entries and 1 dynamic reloc for
  1859. R_PARISC_TLS_DTPMOD32 relocs. */
  1860. htab->tls_ldm_got.offset = htab->etab.sgot->size;
  1861. htab->etab.sgot->size += (GOT_ENTRY_SIZE * 2);
  1862. htab->etab.srelgot->size += sizeof (Elf32_External_Rela);
  1863. }
  1864. else
  1865. htab->tls_ldm_got.offset = -1;
  1866. /* Do all the .plt entries without relocs first. The dynamic linker
  1867. uses the last .plt reloc to find the end of the .plt (and hence
  1868. the start of the .got) for lazy linking. */
  1869. elf_link_hash_traverse (&htab->etab, allocate_plt_static, info);
  1870. /* Allocate global sym .plt and .got entries, and space for global
  1871. sym dynamic relocs. */
  1872. elf_link_hash_traverse (&htab->etab, allocate_dynrelocs, info);
  1873. /* The check_relocs and adjust_dynamic_symbol entry points have
  1874. determined the sizes of the various dynamic sections. Allocate
  1875. memory for them. */
  1876. relocs = false;
  1877. for (sec = dynobj->sections; sec != NULL; sec = sec->next)
  1878. {
  1879. if ((sec->flags & SEC_LINKER_CREATED) == 0)
  1880. continue;
  1881. if (sec == htab->etab.splt)
  1882. {
  1883. if (htab->need_plt_stub)
  1884. {
  1885. /* Make space for the plt stub at the end of the .plt
  1886. section. We want this stub right at the end, up
  1887. against the .got section. */
  1888. int gotalign = bfd_section_alignment (htab->etab.sgot);
  1889. int pltalign = bfd_section_alignment (sec);
  1890. int align = gotalign > 3 ? gotalign : 3;
  1891. bfd_size_type mask;
  1892. if (align > pltalign)
  1893. bfd_set_section_alignment (sec, align);
  1894. mask = ((bfd_size_type) 1 << gotalign) - 1;
  1895. sec->size = (sec->size + sizeof (plt_stub) + mask) & ~mask;
  1896. }
  1897. }
  1898. else if (sec == htab->etab.sgot
  1899. || sec == htab->etab.sdynbss
  1900. || sec == htab->etab.sdynrelro)
  1901. ;
  1902. else if (startswith (bfd_section_name (sec), ".rela"))
  1903. {
  1904. if (sec->size != 0)
  1905. {
  1906. /* Remember whether there are any reloc sections other
  1907. than .rela.plt. */
  1908. if (sec != htab->etab.srelplt)
  1909. relocs = true;
  1910. /* We use the reloc_count field as a counter if we need
  1911. to copy relocs into the output file. */
  1912. sec->reloc_count = 0;
  1913. }
  1914. }
  1915. else
  1916. {
  1917. /* It's not one of our sections, so don't allocate space. */
  1918. continue;
  1919. }
  1920. if (sec->size == 0)
  1921. {
  1922. /* If we don't need this section, strip it from the
  1923. output file. This is mostly to handle .rela.bss and
  1924. .rela.plt. We must create both sections in
  1925. create_dynamic_sections, because they must be created
  1926. before the linker maps input sections to output
  1927. sections. The linker does that before
  1928. adjust_dynamic_symbol is called, and it is that
  1929. function which decides whether anything needs to go
  1930. into these sections. */
  1931. sec->flags |= SEC_EXCLUDE;
  1932. continue;
  1933. }
  1934. if ((sec->flags & SEC_HAS_CONTENTS) == 0)
  1935. continue;
  1936. /* Allocate memory for the section contents. Zero it, because
  1937. we may not fill in all the reloc sections. */
  1938. sec->contents = bfd_zalloc (dynobj, sec->size);
  1939. if (sec->contents == NULL)
  1940. return false;
  1941. }
  1942. return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
  1943. }
  1944. /* External entry points for sizing and building linker stubs. */
  1945. /* Set up various things so that we can make a list of input sections
  1946. for each output section included in the link. Returns -1 on error,
  1947. 0 when no stubs will be needed, and 1 on success. */
  1948. int
  1949. elf32_hppa_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
  1950. {
  1951. bfd *input_bfd;
  1952. unsigned int bfd_count;
  1953. unsigned int top_id, top_index;
  1954. asection *section;
  1955. asection **input_list, **list;
  1956. size_t amt;
  1957. struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
  1958. if (htab == NULL)
  1959. return -1;
  1960. /* Count the number of input BFDs and find the top input section id. */
  1961. for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
  1962. input_bfd != NULL;
  1963. input_bfd = input_bfd->link.next)
  1964. {
  1965. bfd_count += 1;
  1966. for (section = input_bfd->sections;
  1967. section != NULL;
  1968. section = section->next)
  1969. {
  1970. if (top_id < section->id)
  1971. top_id = section->id;
  1972. }
  1973. }
  1974. htab->bfd_count = bfd_count;
  1975. amt = sizeof (struct map_stub) * (top_id + 1);
  1976. htab->stub_group = bfd_zmalloc (amt);
  1977. if (htab->stub_group == NULL)
  1978. return -1;
  1979. /* We can't use output_bfd->section_count here to find the top output
  1980. section index as some sections may have been removed, and
  1981. strip_excluded_output_sections doesn't renumber the indices. */
  1982. for (section = output_bfd->sections, top_index = 0;
  1983. section != NULL;
  1984. section = section->next)
  1985. {
  1986. if (top_index < section->index)
  1987. top_index = section->index;
  1988. }
  1989. htab->top_index = top_index;
  1990. amt = sizeof (asection *) * (top_index + 1);
  1991. input_list = bfd_malloc (amt);
  1992. htab->input_list = input_list;
  1993. if (input_list == NULL)
  1994. return -1;
  1995. /* For sections we aren't interested in, mark their entries with a
  1996. value we can check later. */
  1997. list = input_list + top_index;
  1998. do
  1999. *list = bfd_abs_section_ptr;
  2000. while (list-- != input_list);
  2001. for (section = output_bfd->sections;
  2002. section != NULL;
  2003. section = section->next)
  2004. {
  2005. if ((section->flags & SEC_CODE) != 0)
  2006. input_list[section->index] = NULL;
  2007. }
  2008. return 1;
  2009. }
  2010. /* The linker repeatedly calls this function for each input section,
  2011. in the order that input sections are linked into output sections.
  2012. Build lists of input sections to determine groupings between which
  2013. we may insert linker stubs. */
  2014. void
  2015. elf32_hppa_next_input_section (struct bfd_link_info *info, asection *isec)
  2016. {
  2017. struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
  2018. if (htab == NULL)
  2019. return;
  2020. if (isec->output_section->index <= htab->top_index)
  2021. {
  2022. asection **list = htab->input_list + isec->output_section->index;
  2023. if (*list != bfd_abs_section_ptr)
  2024. {
  2025. /* Steal the link_sec pointer for our list. */
  2026. #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
  2027. /* This happens to make the list in reverse order,
  2028. which is what we want. */
  2029. PREV_SEC (isec) = *list;
  2030. *list = isec;
  2031. }
  2032. }
  2033. }
  2034. /* See whether we can group stub sections together. Grouping stub
  2035. sections may result in fewer stubs. More importantly, we need to
  2036. put all .init* and .fini* stubs at the beginning of the .init or
  2037. .fini output sections respectively, because glibc splits the
  2038. _init and _fini functions into multiple parts. Putting a stub in
  2039. the middle of a function is not a good idea. */
  2040. static void
  2041. group_sections (struct elf32_hppa_link_hash_table *htab,
  2042. bfd_size_type stub_group_size,
  2043. bool stubs_always_before_branch)
  2044. {
  2045. asection **list = htab->input_list + htab->top_index;
  2046. do
  2047. {
  2048. asection *tail = *list;
  2049. if (tail == bfd_abs_section_ptr)
  2050. continue;
  2051. while (tail != NULL)
  2052. {
  2053. asection *curr;
  2054. asection *prev;
  2055. bfd_size_type total;
  2056. bool big_sec;
  2057. curr = tail;
  2058. total = tail->size;
  2059. big_sec = total >= stub_group_size;
  2060. while ((prev = PREV_SEC (curr)) != NULL
  2061. && ((total += curr->output_offset - prev->output_offset)
  2062. < stub_group_size))
  2063. curr = prev;
  2064. /* OK, the size from the start of CURR to the end is less
  2065. than 240000 bytes and thus can be handled by one stub
  2066. section. (or the tail section is itself larger than
  2067. 240000 bytes, in which case we may be toast.)
  2068. We should really be keeping track of the total size of
  2069. stubs added here, as stubs contribute to the final output
  2070. section size. That's a little tricky, and this way will
  2071. only break if stubs added total more than 22144 bytes, or
  2072. 2768 long branch stubs. It seems unlikely for more than
  2073. 2768 different functions to be called, especially from
  2074. code only 240000 bytes long. This limit used to be
  2075. 250000, but c++ code tends to generate lots of little
  2076. functions, and sometimes violated the assumption. */
  2077. do
  2078. {
  2079. prev = PREV_SEC (tail);
  2080. /* Set up this stub group. */
  2081. htab->stub_group[tail->id].link_sec = curr;
  2082. }
  2083. while (tail != curr && (tail = prev) != NULL);
  2084. /* But wait, there's more! Input sections up to 240000
  2085. bytes before the stub section can be handled by it too.
  2086. Don't do this if we have a really large section after the
  2087. stubs, as adding more stubs increases the chance that
  2088. branches may not reach into the stub section. */
  2089. if (!stubs_always_before_branch && !big_sec)
  2090. {
  2091. total = 0;
  2092. while (prev != NULL
  2093. && ((total += tail->output_offset - prev->output_offset)
  2094. < stub_group_size))
  2095. {
  2096. tail = prev;
  2097. prev = PREV_SEC (tail);
  2098. htab->stub_group[tail->id].link_sec = curr;
  2099. }
  2100. }
  2101. tail = prev;
  2102. }
  2103. }
  2104. while (list-- != htab->input_list);
  2105. free (htab->input_list);
  2106. #undef PREV_SEC
  2107. }
  2108. /* Read in all local syms for all input bfds, and create hash entries
  2109. for export stubs if we are building a multi-subspace shared lib.
  2110. Returns -1 on error, 1 if export stubs created, 0 otherwise. */
  2111. static int
  2112. get_local_syms (bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *info)
  2113. {
  2114. unsigned int bfd_indx;
  2115. Elf_Internal_Sym *local_syms, **all_local_syms;
  2116. int stub_changed = 0;
  2117. struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
  2118. if (htab == NULL)
  2119. return -1;
  2120. /* We want to read in symbol extension records only once. To do this
  2121. we need to read in the local symbols in parallel and save them for
  2122. later use; so hold pointers to the local symbols in an array. */
  2123. size_t amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
  2124. all_local_syms = bfd_zmalloc (amt);
  2125. htab->all_local_syms = all_local_syms;
  2126. if (all_local_syms == NULL)
  2127. return -1;
  2128. /* Walk over all the input BFDs, swapping in local symbols.
  2129. If we are creating a shared library, create hash entries for the
  2130. export stubs. */
  2131. for (bfd_indx = 0;
  2132. input_bfd != NULL;
  2133. input_bfd = input_bfd->link.next, bfd_indx++)
  2134. {
  2135. Elf_Internal_Shdr *symtab_hdr;
  2136. /* We'll need the symbol table in a second. */
  2137. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2138. if (symtab_hdr->sh_info == 0)
  2139. continue;
  2140. /* We need an array of the local symbols attached to the input bfd. */
  2141. local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
  2142. if (local_syms == NULL)
  2143. {
  2144. local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
  2145. symtab_hdr->sh_info, 0,
  2146. NULL, NULL, NULL);
  2147. /* Cache them for elf_link_input_bfd. */
  2148. symtab_hdr->contents = (unsigned char *) local_syms;
  2149. }
  2150. if (local_syms == NULL)
  2151. return -1;
  2152. all_local_syms[bfd_indx] = local_syms;
  2153. if (bfd_link_pic (info) && htab->multi_subspace)
  2154. {
  2155. struct elf_link_hash_entry **eh_syms;
  2156. struct elf_link_hash_entry **eh_symend;
  2157. unsigned int symcount;
  2158. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  2159. - symtab_hdr->sh_info);
  2160. eh_syms = (struct elf_link_hash_entry **) elf_sym_hashes (input_bfd);
  2161. eh_symend = (struct elf_link_hash_entry **) (eh_syms + symcount);
  2162. /* Look through the global syms for functions; We need to
  2163. build export stubs for all globally visible functions. */
  2164. for (; eh_syms < eh_symend; eh_syms++)
  2165. {
  2166. struct elf32_hppa_link_hash_entry *hh;
  2167. hh = hppa_elf_hash_entry (*eh_syms);
  2168. while (hh->eh.root.type == bfd_link_hash_indirect
  2169. || hh->eh.root.type == bfd_link_hash_warning)
  2170. hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
  2171. /* At this point in the link, undefined syms have been
  2172. resolved, so we need to check that the symbol was
  2173. defined in this BFD. */
  2174. if ((hh->eh.root.type == bfd_link_hash_defined
  2175. || hh->eh.root.type == bfd_link_hash_defweak)
  2176. && hh->eh.type == STT_FUNC
  2177. && hh->eh.root.u.def.section->output_section != NULL
  2178. && (hh->eh.root.u.def.section->output_section->owner
  2179. == output_bfd)
  2180. && hh->eh.root.u.def.section->owner == input_bfd
  2181. && hh->eh.def_regular
  2182. && !hh->eh.forced_local
  2183. && ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT)
  2184. {
  2185. asection *sec;
  2186. const char *stub_name;
  2187. struct elf32_hppa_stub_hash_entry *hsh;
  2188. sec = hh->eh.root.u.def.section;
  2189. stub_name = hh_name (hh);
  2190. hsh = hppa_stub_hash_lookup (&htab->bstab,
  2191. stub_name,
  2192. false, false);
  2193. if (hsh == NULL)
  2194. {
  2195. hsh = hppa_add_stub (stub_name, sec, htab);
  2196. if (!hsh)
  2197. return -1;
  2198. hsh->target_value = hh->eh.root.u.def.value;
  2199. hsh->target_section = hh->eh.root.u.def.section;
  2200. hsh->stub_type = hppa_stub_export;
  2201. hsh->hh = hh;
  2202. stub_changed = 1;
  2203. }
  2204. else
  2205. {
  2206. /* xgettext:c-format */
  2207. _bfd_error_handler (_("%pB: duplicate export stub %s"),
  2208. input_bfd, stub_name);
  2209. }
  2210. }
  2211. }
  2212. }
  2213. }
  2214. return stub_changed;
  2215. }
  2216. /* Determine and set the size of the stub section for a final link.
  2217. The basic idea here is to examine all the relocations looking for
  2218. PC-relative calls to a target that is unreachable with a "bl"
  2219. instruction. */
  2220. bool
  2221. elf32_hppa_size_stubs
  2222. (bfd *output_bfd, bfd *stub_bfd, struct bfd_link_info *info,
  2223. bool multi_subspace, bfd_signed_vma group_size,
  2224. asection * (*add_stub_section) (const char *, asection *),
  2225. void (*layout_sections_again) (void))
  2226. {
  2227. bfd_size_type stub_group_size;
  2228. bool stubs_always_before_branch;
  2229. bool stub_changed;
  2230. struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
  2231. if (htab == NULL)
  2232. return false;
  2233. /* Stash our params away. */
  2234. htab->stub_bfd = stub_bfd;
  2235. htab->multi_subspace = multi_subspace;
  2236. htab->add_stub_section = add_stub_section;
  2237. htab->layout_sections_again = layout_sections_again;
  2238. stubs_always_before_branch = group_size < 0;
  2239. if (group_size < 0)
  2240. stub_group_size = -group_size;
  2241. else
  2242. stub_group_size = group_size;
  2243. if (stub_group_size == 1)
  2244. {
  2245. /* Default values. */
  2246. if (stubs_always_before_branch)
  2247. {
  2248. stub_group_size = 7680000;
  2249. if (htab->has_17bit_branch || htab->multi_subspace)
  2250. stub_group_size = 240000;
  2251. if (htab->has_12bit_branch)
  2252. stub_group_size = 7500;
  2253. }
  2254. else
  2255. {
  2256. stub_group_size = 6971392;
  2257. if (htab->has_17bit_branch || htab->multi_subspace)
  2258. stub_group_size = 217856;
  2259. if (htab->has_12bit_branch)
  2260. stub_group_size = 6808;
  2261. }
  2262. }
  2263. group_sections (htab, stub_group_size, stubs_always_before_branch);
  2264. switch (get_local_syms (output_bfd, info->input_bfds, info))
  2265. {
  2266. default:
  2267. if (htab->all_local_syms)
  2268. goto error_ret_free_local;
  2269. return false;
  2270. case 0:
  2271. stub_changed = false;
  2272. break;
  2273. case 1:
  2274. stub_changed = true;
  2275. break;
  2276. }
  2277. while (1)
  2278. {
  2279. bfd *input_bfd;
  2280. unsigned int bfd_indx;
  2281. asection *stub_sec;
  2282. for (input_bfd = info->input_bfds, bfd_indx = 0;
  2283. input_bfd != NULL;
  2284. input_bfd = input_bfd->link.next, bfd_indx++)
  2285. {
  2286. Elf_Internal_Shdr *symtab_hdr;
  2287. asection *section;
  2288. Elf_Internal_Sym *local_syms;
  2289. /* We'll need the symbol table in a second. */
  2290. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2291. if (symtab_hdr->sh_info == 0)
  2292. continue;
  2293. local_syms = htab->all_local_syms[bfd_indx];
  2294. /* Walk over each section attached to the input bfd. */
  2295. for (section = input_bfd->sections;
  2296. section != NULL;
  2297. section = section->next)
  2298. {
  2299. Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
  2300. /* If there aren't any relocs, then there's nothing more
  2301. to do. */
  2302. if ((section->flags & SEC_RELOC) == 0
  2303. || (section->flags & SEC_ALLOC) == 0
  2304. || (section->flags & SEC_LOAD) == 0
  2305. || (section->flags & SEC_CODE) == 0
  2306. || section->reloc_count == 0)
  2307. continue;
  2308. /* If this section is a link-once section that will be
  2309. discarded, then don't create any stubs. */
  2310. if (section->output_section == NULL
  2311. || section->output_section->owner != output_bfd)
  2312. continue;
  2313. /* Get the relocs. */
  2314. internal_relocs
  2315. = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
  2316. info->keep_memory);
  2317. if (internal_relocs == NULL)
  2318. goto error_ret_free_local;
  2319. /* Now examine each relocation. */
  2320. irela = internal_relocs;
  2321. irelaend = irela + section->reloc_count;
  2322. for (; irela < irelaend; irela++)
  2323. {
  2324. unsigned int r_type, r_indx;
  2325. enum elf32_hppa_stub_type stub_type;
  2326. struct elf32_hppa_stub_hash_entry *hsh;
  2327. asection *sym_sec;
  2328. bfd_vma sym_value;
  2329. bfd_vma destination;
  2330. struct elf32_hppa_link_hash_entry *hh;
  2331. char *stub_name;
  2332. const asection *id_sec;
  2333. r_type = ELF32_R_TYPE (irela->r_info);
  2334. r_indx = ELF32_R_SYM (irela->r_info);
  2335. if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
  2336. {
  2337. bfd_set_error (bfd_error_bad_value);
  2338. error_ret_free_internal:
  2339. if (elf_section_data (section)->relocs == NULL)
  2340. free (internal_relocs);
  2341. goto error_ret_free_local;
  2342. }
  2343. /* Only look for stubs on call instructions. */
  2344. if (r_type != (unsigned int) R_PARISC_PCREL12F
  2345. && r_type != (unsigned int) R_PARISC_PCREL17F
  2346. && r_type != (unsigned int) R_PARISC_PCREL22F)
  2347. continue;
  2348. /* Now determine the call target, its name, value,
  2349. section. */
  2350. sym_sec = NULL;
  2351. sym_value = 0;
  2352. destination = -1;
  2353. hh = NULL;
  2354. if (r_indx < symtab_hdr->sh_info)
  2355. {
  2356. /* It's a local symbol. */
  2357. Elf_Internal_Sym *sym;
  2358. Elf_Internal_Shdr *hdr;
  2359. unsigned int shndx;
  2360. sym = local_syms + r_indx;
  2361. if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
  2362. sym_value = sym->st_value;
  2363. shndx = sym->st_shndx;
  2364. if (shndx < elf_numsections (input_bfd))
  2365. {
  2366. hdr = elf_elfsections (input_bfd)[shndx];
  2367. sym_sec = hdr->bfd_section;
  2368. destination = (sym_value + irela->r_addend
  2369. + sym_sec->output_offset
  2370. + sym_sec->output_section->vma);
  2371. }
  2372. }
  2373. else
  2374. {
  2375. /* It's an external symbol. */
  2376. int e_indx;
  2377. e_indx = r_indx - symtab_hdr->sh_info;
  2378. hh = hppa_elf_hash_entry (elf_sym_hashes (input_bfd)[e_indx]);
  2379. while (hh->eh.root.type == bfd_link_hash_indirect
  2380. || hh->eh.root.type == bfd_link_hash_warning)
  2381. hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
  2382. if (hh->eh.root.type == bfd_link_hash_defined
  2383. || hh->eh.root.type == bfd_link_hash_defweak)
  2384. {
  2385. sym_sec = hh->eh.root.u.def.section;
  2386. sym_value = hh->eh.root.u.def.value;
  2387. if (sym_sec->output_section != NULL)
  2388. destination = (sym_value + irela->r_addend
  2389. + sym_sec->output_offset
  2390. + sym_sec->output_section->vma);
  2391. }
  2392. else if (hh->eh.root.type == bfd_link_hash_undefweak)
  2393. {
  2394. if (! bfd_link_pic (info))
  2395. continue;
  2396. }
  2397. else if (hh->eh.root.type == bfd_link_hash_undefined)
  2398. {
  2399. if (! (info->unresolved_syms_in_objects == RM_IGNORE
  2400. && (ELF_ST_VISIBILITY (hh->eh.other)
  2401. == STV_DEFAULT)
  2402. && hh->eh.type != STT_PARISC_MILLI))
  2403. continue;
  2404. }
  2405. else
  2406. {
  2407. bfd_set_error (bfd_error_bad_value);
  2408. goto error_ret_free_internal;
  2409. }
  2410. }
  2411. /* Determine what (if any) linker stub is needed. */
  2412. stub_type = hppa_type_of_stub (section, irela, hh,
  2413. destination, info);
  2414. if (stub_type == hppa_stub_none)
  2415. continue;
  2416. /* Support for grouping stub sections. */
  2417. id_sec = htab->stub_group[section->id].link_sec;
  2418. /* Get the name of this stub. */
  2419. stub_name = hppa_stub_name (id_sec, sym_sec, hh, irela);
  2420. if (!stub_name)
  2421. goto error_ret_free_internal;
  2422. hsh = hppa_stub_hash_lookup (&htab->bstab,
  2423. stub_name,
  2424. false, false);
  2425. if (hsh != NULL)
  2426. {
  2427. /* The proper stub has already been created. */
  2428. free (stub_name);
  2429. continue;
  2430. }
  2431. hsh = hppa_add_stub (stub_name, section, htab);
  2432. if (hsh == NULL)
  2433. {
  2434. free (stub_name);
  2435. goto error_ret_free_internal;
  2436. }
  2437. hsh->target_value = sym_value;
  2438. hsh->target_section = sym_sec;
  2439. hsh->stub_type = stub_type;
  2440. if (bfd_link_pic (info))
  2441. {
  2442. if (stub_type == hppa_stub_import)
  2443. hsh->stub_type = hppa_stub_import_shared;
  2444. else if (stub_type == hppa_stub_long_branch)
  2445. hsh->stub_type = hppa_stub_long_branch_shared;
  2446. }
  2447. hsh->hh = hh;
  2448. stub_changed = true;
  2449. }
  2450. /* We're done with the internal relocs, free them. */
  2451. if (elf_section_data (section)->relocs == NULL)
  2452. free (internal_relocs);
  2453. }
  2454. }
  2455. if (!stub_changed)
  2456. break;
  2457. /* OK, we've added some stubs. Find out the new size of the
  2458. stub sections. */
  2459. for (stub_sec = htab->stub_bfd->sections;
  2460. stub_sec != NULL;
  2461. stub_sec = stub_sec->next)
  2462. if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
  2463. stub_sec->size = 0;
  2464. bfd_hash_traverse (&htab->bstab, hppa_size_one_stub, htab);
  2465. /* Ask the linker to do its stuff. */
  2466. (*htab->layout_sections_again) ();
  2467. stub_changed = false;
  2468. }
  2469. free (htab->all_local_syms);
  2470. return true;
  2471. error_ret_free_local:
  2472. free (htab->all_local_syms);
  2473. return false;
  2474. }
  2475. /* For a final link, this function is called after we have sized the
  2476. stubs to provide a value for __gp. */
  2477. bool
  2478. elf32_hppa_set_gp (bfd *abfd, struct bfd_link_info *info)
  2479. {
  2480. struct bfd_link_hash_entry *h;
  2481. asection *sec = NULL;
  2482. bfd_vma gp_val = 0;
  2483. h = bfd_link_hash_lookup (info->hash, "$global$", false, false, false);
  2484. if (h != NULL
  2485. && (h->type == bfd_link_hash_defined
  2486. || h->type == bfd_link_hash_defweak))
  2487. {
  2488. gp_val = h->u.def.value;
  2489. sec = h->u.def.section;
  2490. }
  2491. else
  2492. {
  2493. asection *splt = bfd_get_section_by_name (abfd, ".plt");
  2494. asection *sgot = bfd_get_section_by_name (abfd, ".got");
  2495. /* Choose to point our LTP at, in this order, one of .plt, .got,
  2496. or .data, if these sections exist. In the case of choosing
  2497. .plt try to make the LTP ideal for addressing anywhere in the
  2498. .plt or .got with a 14 bit signed offset. Typically, the end
  2499. of the .plt is the start of the .got, so choose .plt + 0x2000
  2500. if either the .plt or .got is larger than 0x2000. If both
  2501. the .plt and .got are smaller than 0x2000, choose the end of
  2502. the .plt section. */
  2503. sec = strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0
  2504. ? NULL : splt;
  2505. if (sec != NULL)
  2506. {
  2507. gp_val = sec->size;
  2508. if (gp_val > 0x2000 || (sgot && sgot->size > 0x2000))
  2509. {
  2510. gp_val = 0x2000;
  2511. }
  2512. }
  2513. else
  2514. {
  2515. sec = sgot;
  2516. if (sec != NULL)
  2517. {
  2518. if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") != 0)
  2519. {
  2520. /* We know we don't have a .plt. If .got is large,
  2521. offset our LTP. */
  2522. if (sec->size > 0x2000)
  2523. gp_val = 0x2000;
  2524. }
  2525. }
  2526. else
  2527. {
  2528. /* No .plt or .got. Who cares what the LTP is? */
  2529. sec = bfd_get_section_by_name (abfd, ".data");
  2530. }
  2531. }
  2532. if (h != NULL)
  2533. {
  2534. h->type = bfd_link_hash_defined;
  2535. h->u.def.value = gp_val;
  2536. if (sec != NULL)
  2537. h->u.def.section = sec;
  2538. else
  2539. h->u.def.section = bfd_abs_section_ptr;
  2540. }
  2541. }
  2542. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  2543. {
  2544. if (sec != NULL && sec->output_section != NULL)
  2545. gp_val += sec->output_section->vma + sec->output_offset;
  2546. elf_gp (abfd) = gp_val;
  2547. }
  2548. return true;
  2549. }
  2550. /* Build all the stubs associated with the current output file. The
  2551. stubs are kept in a hash table attached to the main linker hash
  2552. table. We also set up the .plt entries for statically linked PIC
  2553. functions here. This function is called via hppaelf_finish in the
  2554. linker. */
  2555. bool
  2556. elf32_hppa_build_stubs (struct bfd_link_info *info)
  2557. {
  2558. asection *stub_sec;
  2559. struct bfd_hash_table *table;
  2560. struct elf32_hppa_link_hash_table *htab;
  2561. htab = hppa_link_hash_table (info);
  2562. if (htab == NULL)
  2563. return false;
  2564. for (stub_sec = htab->stub_bfd->sections;
  2565. stub_sec != NULL;
  2566. stub_sec = stub_sec->next)
  2567. if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
  2568. && stub_sec->size != 0)
  2569. {
  2570. /* Allocate memory to hold the linker stubs. */
  2571. stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
  2572. if (stub_sec->contents == NULL)
  2573. return false;
  2574. stub_sec->size = 0;
  2575. }
  2576. /* Build the stubs as directed by the stub hash table. */
  2577. table = &htab->bstab;
  2578. bfd_hash_traverse (table, hppa_build_one_stub, info);
  2579. return true;
  2580. }
  2581. /* Return the base vma address which should be subtracted from the real
  2582. address when resolving a dtpoff relocation.
  2583. This is PT_TLS segment p_vaddr. */
  2584. static bfd_vma
  2585. dtpoff_base (struct bfd_link_info *info)
  2586. {
  2587. /* If tls_sec is NULL, we should have signalled an error already. */
  2588. if (elf_hash_table (info)->tls_sec == NULL)
  2589. return 0;
  2590. return elf_hash_table (info)->tls_sec->vma;
  2591. }
  2592. /* Return the relocation value for R_PARISC_TLS_TPOFF*.. */
  2593. static bfd_vma
  2594. tpoff (struct bfd_link_info *info, bfd_vma address)
  2595. {
  2596. struct elf_link_hash_table *htab = elf_hash_table (info);
  2597. /* If tls_sec is NULL, we should have signalled an error already. */
  2598. if (htab->tls_sec == NULL)
  2599. return 0;
  2600. /* hppa TLS ABI is variant I and static TLS block start just after
  2601. tcbhead structure which has 2 pointer fields. */
  2602. return (address - htab->tls_sec->vma
  2603. + align_power ((bfd_vma) 8, htab->tls_sec->alignment_power));
  2604. }
  2605. /* Perform a final link. */
  2606. static bool
  2607. elf32_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
  2608. {
  2609. struct stat buf;
  2610. /* Invoke the regular ELF linker to do all the work. */
  2611. if (!bfd_elf_final_link (abfd, info))
  2612. return false;
  2613. /* If we're producing a final executable, sort the contents of the
  2614. unwind section. */
  2615. if (bfd_link_relocatable (info))
  2616. return true;
  2617. /* Do not attempt to sort non-regular files. This is here
  2618. especially for configure scripts and kernel builds which run
  2619. tests with "ld [...] -o /dev/null". */
  2620. if (stat (bfd_get_filename (abfd), &buf) != 0
  2621. || !S_ISREG(buf.st_mode))
  2622. return true;
  2623. return elf_hppa_sort_unwind (abfd);
  2624. }
  2625. /* Record the lowest address for the data and text segments. */
  2626. static void
  2627. hppa_record_segment_addr (bfd *abfd, asection *section, void *data)
  2628. {
  2629. struct elf32_hppa_link_hash_table *htab;
  2630. htab = (struct elf32_hppa_link_hash_table*) data;
  2631. if (htab == NULL)
  2632. return;
  2633. if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
  2634. {
  2635. bfd_vma value;
  2636. Elf_Internal_Phdr *p;
  2637. p = _bfd_elf_find_segment_containing_section (abfd, section->output_section);
  2638. BFD_ASSERT (p != NULL);
  2639. value = p->p_vaddr;
  2640. if ((section->flags & SEC_READONLY) != 0)
  2641. {
  2642. if (value < htab->text_segment_base)
  2643. htab->text_segment_base = value;
  2644. }
  2645. else
  2646. {
  2647. if (value < htab->data_segment_base)
  2648. htab->data_segment_base = value;
  2649. }
  2650. }
  2651. }
  2652. /* Perform a relocation as part of a final link. */
  2653. static bfd_reloc_status_type
  2654. final_link_relocate (asection *input_section,
  2655. bfd_byte *contents,
  2656. const Elf_Internal_Rela *rela,
  2657. bfd_vma value,
  2658. struct elf32_hppa_link_hash_table *htab,
  2659. asection *sym_sec,
  2660. struct elf32_hppa_link_hash_entry *hh,
  2661. struct bfd_link_info *info)
  2662. {
  2663. unsigned int insn;
  2664. unsigned int r_type = ELF32_R_TYPE (rela->r_info);
  2665. unsigned int orig_r_type = r_type;
  2666. reloc_howto_type *howto = elf_hppa_howto_table + r_type;
  2667. int r_format = howto->bitsize;
  2668. enum hppa_reloc_field_selector_type_alt r_field;
  2669. bfd *input_bfd = input_section->owner;
  2670. bfd_vma offset = rela->r_offset;
  2671. bfd_vma max_branch_offset = 0;
  2672. bfd_byte *hit_data = contents + offset;
  2673. bfd_signed_vma addend = rela->r_addend;
  2674. bfd_vma location;
  2675. struct elf32_hppa_stub_hash_entry *hsh = NULL;
  2676. int val;
  2677. if (r_type == R_PARISC_NONE)
  2678. return bfd_reloc_ok;
  2679. insn = bfd_get_32 (input_bfd, hit_data);
  2680. /* Find out where we are and where we're going. */
  2681. location = (offset +
  2682. input_section->output_offset +
  2683. input_section->output_section->vma);
  2684. /* If we are not building a shared library, convert DLTIND relocs to
  2685. DPREL relocs. */
  2686. if (!bfd_link_pic (info))
  2687. {
  2688. switch (r_type)
  2689. {
  2690. case R_PARISC_DLTIND21L:
  2691. case R_PARISC_TLS_GD21L:
  2692. case R_PARISC_TLS_LDM21L:
  2693. case R_PARISC_TLS_IE21L:
  2694. r_type = R_PARISC_DPREL21L;
  2695. break;
  2696. case R_PARISC_DLTIND14R:
  2697. case R_PARISC_TLS_GD14R:
  2698. case R_PARISC_TLS_LDM14R:
  2699. case R_PARISC_TLS_IE14R:
  2700. r_type = R_PARISC_DPREL14R;
  2701. break;
  2702. case R_PARISC_DLTIND14F:
  2703. r_type = R_PARISC_DPREL14F;
  2704. break;
  2705. }
  2706. }
  2707. switch (r_type)
  2708. {
  2709. case R_PARISC_PCREL12F:
  2710. case R_PARISC_PCREL17F:
  2711. case R_PARISC_PCREL22F:
  2712. /* If this call should go via the plt, find the import stub in
  2713. the stub hash. */
  2714. if (sym_sec == NULL
  2715. || sym_sec->output_section == NULL
  2716. || (hh != NULL
  2717. && hh->eh.plt.offset != (bfd_vma) -1
  2718. && hh->eh.dynindx != -1
  2719. && !hh->plabel
  2720. && (bfd_link_pic (info)
  2721. || !hh->eh.def_regular
  2722. || hh->eh.root.type == bfd_link_hash_defweak)))
  2723. {
  2724. hsh = hppa_get_stub_entry (input_section, sym_sec,
  2725. hh, rela, htab);
  2726. if (hsh != NULL)
  2727. {
  2728. value = (hsh->stub_offset
  2729. + hsh->stub_sec->output_offset
  2730. + hsh->stub_sec->output_section->vma);
  2731. addend = 0;
  2732. }
  2733. else if (sym_sec == NULL && hh != NULL
  2734. && hh->eh.root.type == bfd_link_hash_undefweak)
  2735. {
  2736. /* It's OK if undefined weak. Calls to undefined weak
  2737. symbols behave as if the "called" function
  2738. immediately returns. We can thus call to a weak
  2739. function without first checking whether the function
  2740. is defined. */
  2741. value = location;
  2742. addend = 8;
  2743. }
  2744. else
  2745. return bfd_reloc_undefined;
  2746. }
  2747. /* Fall thru. */
  2748. case R_PARISC_PCREL21L:
  2749. case R_PARISC_PCREL17C:
  2750. case R_PARISC_PCREL17R:
  2751. case R_PARISC_PCREL14R:
  2752. case R_PARISC_PCREL14F:
  2753. case R_PARISC_PCREL32:
  2754. /* Make it a pc relative offset. */
  2755. value -= location;
  2756. addend -= 8;
  2757. break;
  2758. case R_PARISC_DPREL21L:
  2759. case R_PARISC_DPREL14R:
  2760. case R_PARISC_DPREL14F:
  2761. /* Convert instructions that use the linkage table pointer (r19) to
  2762. instructions that use the global data pointer (dp). This is the
  2763. most efficient way of using PIC code in an incomplete executable,
  2764. but the user must follow the standard runtime conventions for
  2765. accessing data for this to work. */
  2766. if (orig_r_type != r_type)
  2767. {
  2768. if (r_type == R_PARISC_DPREL21L)
  2769. {
  2770. /* GCC sometimes uses a register other than r19 for the
  2771. operation, so we must convert any addil instruction
  2772. that uses this relocation. */
  2773. if ((insn & 0xfc000000) == OP_ADDIL << 26)
  2774. insn = ADDIL_DP;
  2775. else
  2776. /* We must have a ldil instruction. It's too hard to find
  2777. and convert the associated add instruction, so issue an
  2778. error. */
  2779. _bfd_error_handler
  2780. /* xgettext:c-format */
  2781. (_("%pB(%pA+%#" PRIx64 "): %s fixup for insn %#x "
  2782. "is not supported in a non-shared link"),
  2783. input_bfd,
  2784. input_section,
  2785. (uint64_t) offset,
  2786. howto->name,
  2787. insn);
  2788. }
  2789. else if (r_type == R_PARISC_DPREL14F)
  2790. {
  2791. /* This must be a format 1 load/store. Change the base
  2792. register to dp. */
  2793. insn = (insn & 0xfc1ffff) | (27 << 21);
  2794. }
  2795. }
  2796. /* For all the DP relative relocations, we need to examine the symbol's
  2797. section. If it has no section or if it's a code section, then
  2798. "data pointer relative" makes no sense. In that case we don't
  2799. adjust the "value", and for 21 bit addil instructions, we change the
  2800. source addend register from %dp to %r0. This situation commonly
  2801. arises for undefined weak symbols and when a variable's "constness"
  2802. is declared differently from the way the variable is defined. For
  2803. instance: "extern int foo" with foo defined as "const int foo". */
  2804. if (sym_sec == NULL || (sym_sec->flags & SEC_CODE) != 0)
  2805. {
  2806. if ((insn & ((0x3fu << 26) | (0x1f << 21)))
  2807. == ((OP_ADDIL << 26) | (27 << 21)))
  2808. {
  2809. insn &= ~ (0x1f << 21);
  2810. }
  2811. /* Now try to make things easy for the dynamic linker. */
  2812. break;
  2813. }
  2814. /* Fall thru. */
  2815. case R_PARISC_DLTIND21L:
  2816. case R_PARISC_DLTIND14R:
  2817. case R_PARISC_DLTIND14F:
  2818. case R_PARISC_TLS_GD21L:
  2819. case R_PARISC_TLS_LDM21L:
  2820. case R_PARISC_TLS_IE21L:
  2821. case R_PARISC_TLS_GD14R:
  2822. case R_PARISC_TLS_LDM14R:
  2823. case R_PARISC_TLS_IE14R:
  2824. value -= elf_gp (input_section->output_section->owner);
  2825. break;
  2826. case R_PARISC_SEGREL32:
  2827. if ((sym_sec->flags & SEC_CODE) != 0)
  2828. value -= htab->text_segment_base;
  2829. else
  2830. value -= htab->data_segment_base;
  2831. break;
  2832. default:
  2833. break;
  2834. }
  2835. switch (r_type)
  2836. {
  2837. case R_PARISC_DIR32:
  2838. case R_PARISC_DIR14F:
  2839. case R_PARISC_DIR17F:
  2840. case R_PARISC_PCREL17C:
  2841. case R_PARISC_PCREL14F:
  2842. case R_PARISC_PCREL32:
  2843. case R_PARISC_DPREL14F:
  2844. case R_PARISC_PLABEL32:
  2845. case R_PARISC_DLTIND14F:
  2846. case R_PARISC_SEGBASE:
  2847. case R_PARISC_SEGREL32:
  2848. case R_PARISC_TLS_DTPMOD32:
  2849. case R_PARISC_TLS_DTPOFF32:
  2850. case R_PARISC_TLS_TPREL32:
  2851. r_field = e_fsel;
  2852. break;
  2853. case R_PARISC_DLTIND21L:
  2854. case R_PARISC_PCREL21L:
  2855. case R_PARISC_PLABEL21L:
  2856. r_field = e_lsel;
  2857. break;
  2858. case R_PARISC_DIR21L:
  2859. case R_PARISC_DPREL21L:
  2860. case R_PARISC_TLS_GD21L:
  2861. case R_PARISC_TLS_LDM21L:
  2862. case R_PARISC_TLS_LDO21L:
  2863. case R_PARISC_TLS_IE21L:
  2864. case R_PARISC_TLS_LE21L:
  2865. r_field = e_lrsel;
  2866. break;
  2867. case R_PARISC_PCREL17R:
  2868. case R_PARISC_PCREL14R:
  2869. case R_PARISC_PLABEL14R:
  2870. case R_PARISC_DLTIND14R:
  2871. r_field = e_rsel;
  2872. break;
  2873. case R_PARISC_DIR17R:
  2874. case R_PARISC_DIR14R:
  2875. case R_PARISC_DPREL14R:
  2876. case R_PARISC_TLS_GD14R:
  2877. case R_PARISC_TLS_LDM14R:
  2878. case R_PARISC_TLS_LDO14R:
  2879. case R_PARISC_TLS_IE14R:
  2880. case R_PARISC_TLS_LE14R:
  2881. r_field = e_rrsel;
  2882. break;
  2883. case R_PARISC_PCREL12F:
  2884. case R_PARISC_PCREL17F:
  2885. case R_PARISC_PCREL22F:
  2886. r_field = e_fsel;
  2887. if (r_type == (unsigned int) R_PARISC_PCREL17F)
  2888. {
  2889. max_branch_offset = (1 << (17-1)) << 2;
  2890. }
  2891. else if (r_type == (unsigned int) R_PARISC_PCREL12F)
  2892. {
  2893. max_branch_offset = (1 << (12-1)) << 2;
  2894. }
  2895. else
  2896. {
  2897. max_branch_offset = (1 << (22-1)) << 2;
  2898. }
  2899. /* sym_sec is NULL on undefined weak syms or when shared on
  2900. undefined syms. We've already checked for a stub for the
  2901. shared undefined case. */
  2902. if (sym_sec == NULL)
  2903. break;
  2904. /* If the branch is out of reach, then redirect the
  2905. call to the local stub for this function. */
  2906. if (value + addend + max_branch_offset >= 2*max_branch_offset)
  2907. {
  2908. hsh = hppa_get_stub_entry (input_section, sym_sec,
  2909. hh, rela, htab);
  2910. if (hsh == NULL)
  2911. return bfd_reloc_undefined;
  2912. /* Munge up the value and addend so that we call the stub
  2913. rather than the procedure directly. */
  2914. value = (hsh->stub_offset
  2915. + hsh->stub_sec->output_offset
  2916. + hsh->stub_sec->output_section->vma
  2917. - location);
  2918. addend = -8;
  2919. }
  2920. break;
  2921. /* Something we don't know how to handle. */
  2922. default:
  2923. return bfd_reloc_notsupported;
  2924. }
  2925. /* Make sure we can reach the stub. */
  2926. if (max_branch_offset != 0
  2927. && value + addend + max_branch_offset >= 2*max_branch_offset)
  2928. {
  2929. _bfd_error_handler
  2930. /* xgettext:c-format */
  2931. (_("%pB(%pA+%#" PRIx64 "): cannot reach %s, "
  2932. "recompile with -ffunction-sections"),
  2933. input_bfd,
  2934. input_section,
  2935. (uint64_t) offset,
  2936. hsh->bh_root.string);
  2937. bfd_set_error (bfd_error_bad_value);
  2938. return bfd_reloc_notsupported;
  2939. }
  2940. val = hppa_field_adjust (value, addend, r_field);
  2941. switch (r_type)
  2942. {
  2943. case R_PARISC_PCREL12F:
  2944. case R_PARISC_PCREL17C:
  2945. case R_PARISC_PCREL17F:
  2946. case R_PARISC_PCREL17R:
  2947. case R_PARISC_PCREL22F:
  2948. case R_PARISC_DIR17F:
  2949. case R_PARISC_DIR17R:
  2950. /* This is a branch. Divide the offset by four.
  2951. Note that we need to decide whether it's a branch or
  2952. otherwise by inspecting the reloc. Inspecting insn won't
  2953. work as insn might be from a .word directive. */
  2954. val >>= 2;
  2955. break;
  2956. default:
  2957. break;
  2958. }
  2959. insn = hppa_rebuild_insn (insn, val, r_format);
  2960. /* Update the instruction word. */
  2961. bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
  2962. return bfd_reloc_ok;
  2963. }
  2964. /* Relocate an HPPA ELF section. */
  2965. static int
  2966. elf32_hppa_relocate_section (bfd *output_bfd,
  2967. struct bfd_link_info *info,
  2968. bfd *input_bfd,
  2969. asection *input_section,
  2970. bfd_byte *contents,
  2971. Elf_Internal_Rela *relocs,
  2972. Elf_Internal_Sym *local_syms,
  2973. asection **local_sections)
  2974. {
  2975. bfd_vma *local_got_offsets;
  2976. struct elf32_hppa_link_hash_table *htab;
  2977. Elf_Internal_Shdr *symtab_hdr;
  2978. Elf_Internal_Rela *rela;
  2979. Elf_Internal_Rela *relend;
  2980. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2981. htab = hppa_link_hash_table (info);
  2982. if (htab == NULL)
  2983. return false;
  2984. local_got_offsets = elf_local_got_offsets (input_bfd);
  2985. rela = relocs;
  2986. relend = relocs + input_section->reloc_count;
  2987. for (; rela < relend; rela++)
  2988. {
  2989. unsigned int r_type;
  2990. reloc_howto_type *howto;
  2991. unsigned int r_symndx;
  2992. struct elf32_hppa_link_hash_entry *hh;
  2993. Elf_Internal_Sym *sym;
  2994. asection *sym_sec;
  2995. bfd_vma relocation;
  2996. bfd_reloc_status_type rstatus;
  2997. const char *sym_name;
  2998. bool plabel;
  2999. bool warned_undef;
  3000. r_type = ELF32_R_TYPE (rela->r_info);
  3001. if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
  3002. {
  3003. bfd_set_error (bfd_error_bad_value);
  3004. return false;
  3005. }
  3006. if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
  3007. || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
  3008. continue;
  3009. r_symndx = ELF32_R_SYM (rela->r_info);
  3010. hh = NULL;
  3011. sym = NULL;
  3012. sym_sec = NULL;
  3013. warned_undef = false;
  3014. if (r_symndx < symtab_hdr->sh_info)
  3015. {
  3016. /* This is a local symbol, h defaults to NULL. */
  3017. sym = local_syms + r_symndx;
  3018. sym_sec = local_sections[r_symndx];
  3019. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rela);
  3020. }
  3021. else
  3022. {
  3023. struct elf_link_hash_entry *eh;
  3024. bool unresolved_reloc, ignored;
  3025. struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
  3026. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rela,
  3027. r_symndx, symtab_hdr, sym_hashes,
  3028. eh, sym_sec, relocation,
  3029. unresolved_reloc, warned_undef,
  3030. ignored);
  3031. if (!bfd_link_relocatable (info)
  3032. && relocation == 0
  3033. && eh->root.type != bfd_link_hash_defined
  3034. && eh->root.type != bfd_link_hash_defweak
  3035. && eh->root.type != bfd_link_hash_undefweak)
  3036. {
  3037. if (info->unresolved_syms_in_objects == RM_IGNORE
  3038. && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT
  3039. && eh->type == STT_PARISC_MILLI)
  3040. {
  3041. (*info->callbacks->undefined_symbol)
  3042. (info, eh_name (eh), input_bfd,
  3043. input_section, rela->r_offset, false);
  3044. warned_undef = true;
  3045. }
  3046. }
  3047. hh = hppa_elf_hash_entry (eh);
  3048. }
  3049. if (sym_sec != NULL && discarded_section (sym_sec))
  3050. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  3051. rela, 1, relend,
  3052. elf_hppa_howto_table + r_type, 0,
  3053. contents);
  3054. if (bfd_link_relocatable (info))
  3055. continue;
  3056. /* Do any required modifications to the relocation value, and
  3057. determine what types of dynamic info we need to output, if
  3058. any. */
  3059. plabel = 0;
  3060. switch (r_type)
  3061. {
  3062. case R_PARISC_DLTIND14F:
  3063. case R_PARISC_DLTIND14R:
  3064. case R_PARISC_DLTIND21L:
  3065. {
  3066. bfd_vma off;
  3067. bool do_got = false;
  3068. bool reloc = bfd_link_pic (info);
  3069. /* Relocation is to the entry for this symbol in the
  3070. global offset table. */
  3071. if (hh != NULL)
  3072. {
  3073. bool dyn;
  3074. off = hh->eh.got.offset;
  3075. dyn = htab->etab.dynamic_sections_created;
  3076. reloc = (!UNDEFWEAK_NO_DYNAMIC_RELOC (info, &hh->eh)
  3077. && (reloc
  3078. || (hh->eh.dynindx != -1
  3079. && !SYMBOL_REFERENCES_LOCAL (info, &hh->eh))));
  3080. if (!reloc
  3081. || !WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  3082. bfd_link_pic (info),
  3083. &hh->eh))
  3084. {
  3085. /* If we aren't going to call finish_dynamic_symbol,
  3086. then we need to handle initialisation of the .got
  3087. entry and create needed relocs here. Since the
  3088. offset must always be a multiple of 4, we use the
  3089. least significant bit to record whether we have
  3090. initialised it already. */
  3091. if ((off & 1) != 0)
  3092. off &= ~1;
  3093. else
  3094. {
  3095. hh->eh.got.offset |= 1;
  3096. do_got = true;
  3097. }
  3098. }
  3099. }
  3100. else
  3101. {
  3102. /* Local symbol case. */
  3103. if (local_got_offsets == NULL)
  3104. abort ();
  3105. off = local_got_offsets[r_symndx];
  3106. /* The offset must always be a multiple of 4. We use
  3107. the least significant bit to record whether we have
  3108. already generated the necessary reloc. */
  3109. if ((off & 1) != 0)
  3110. off &= ~1;
  3111. else
  3112. {
  3113. local_got_offsets[r_symndx] |= 1;
  3114. do_got = true;
  3115. }
  3116. }
  3117. if (do_got)
  3118. {
  3119. if (reloc)
  3120. {
  3121. /* Output a dynamic relocation for this GOT entry.
  3122. In this case it is relative to the base of the
  3123. object because the symbol index is zero. */
  3124. Elf_Internal_Rela outrel;
  3125. bfd_byte *loc;
  3126. asection *sec = htab->etab.srelgot;
  3127. outrel.r_offset = (off
  3128. + htab->etab.sgot->output_offset
  3129. + htab->etab.sgot->output_section->vma);
  3130. outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
  3131. outrel.r_addend = relocation;
  3132. loc = sec->contents;
  3133. loc += sec->reloc_count++ * sizeof (Elf32_External_Rela);
  3134. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  3135. }
  3136. else
  3137. bfd_put_32 (output_bfd, relocation,
  3138. htab->etab.sgot->contents + off);
  3139. }
  3140. if (off >= (bfd_vma) -2)
  3141. abort ();
  3142. /* Add the base of the GOT to the relocation value. */
  3143. relocation = (off
  3144. + htab->etab.sgot->output_offset
  3145. + htab->etab.sgot->output_section->vma);
  3146. }
  3147. break;
  3148. case R_PARISC_SEGREL32:
  3149. /* If this is the first SEGREL relocation, then initialize
  3150. the segment base values. */
  3151. if (htab->text_segment_base == (bfd_vma) -1)
  3152. bfd_map_over_sections (output_bfd, hppa_record_segment_addr, htab);
  3153. break;
  3154. case R_PARISC_PLABEL14R:
  3155. case R_PARISC_PLABEL21L:
  3156. case R_PARISC_PLABEL32:
  3157. if (htab->etab.dynamic_sections_created)
  3158. {
  3159. bfd_vma off;
  3160. bool do_plt = 0;
  3161. /* If we have a global symbol with a PLT slot, then
  3162. redirect this relocation to it. */
  3163. if (hh != NULL)
  3164. {
  3165. off = hh->eh.plt.offset;
  3166. if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (1,
  3167. bfd_link_pic (info),
  3168. &hh->eh))
  3169. {
  3170. /* In a non-shared link, adjust_dynamic_symbol
  3171. isn't called for symbols forced local. We
  3172. need to write out the plt entry here. */
  3173. if ((off & 1) != 0)
  3174. off &= ~1;
  3175. else
  3176. {
  3177. hh->eh.plt.offset |= 1;
  3178. do_plt = 1;
  3179. }
  3180. }
  3181. }
  3182. else
  3183. {
  3184. bfd_vma *local_plt_offsets;
  3185. if (local_got_offsets == NULL)
  3186. abort ();
  3187. local_plt_offsets = local_got_offsets + symtab_hdr->sh_info;
  3188. off = local_plt_offsets[r_symndx];
  3189. /* As for the local .got entry case, we use the last
  3190. bit to record whether we've already initialised
  3191. this local .plt entry. */
  3192. if ((off & 1) != 0)
  3193. off &= ~1;
  3194. else
  3195. {
  3196. local_plt_offsets[r_symndx] |= 1;
  3197. do_plt = 1;
  3198. }
  3199. }
  3200. if (do_plt)
  3201. {
  3202. if (bfd_link_pic (info))
  3203. {
  3204. /* Output a dynamic IPLT relocation for this
  3205. PLT entry. */
  3206. Elf_Internal_Rela outrel;
  3207. bfd_byte *loc;
  3208. asection *s = htab->etab.srelplt;
  3209. outrel.r_offset = (off
  3210. + htab->etab.splt->output_offset
  3211. + htab->etab.splt->output_section->vma);
  3212. outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
  3213. outrel.r_addend = relocation;
  3214. loc = s->contents;
  3215. loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
  3216. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  3217. }
  3218. else
  3219. {
  3220. bfd_put_32 (output_bfd,
  3221. relocation,
  3222. htab->etab.splt->contents + off);
  3223. bfd_put_32 (output_bfd,
  3224. elf_gp (htab->etab.splt->output_section->owner),
  3225. htab->etab.splt->contents + off + 4);
  3226. }
  3227. }
  3228. if (off >= (bfd_vma) -2)
  3229. abort ();
  3230. /* PLABELs contain function pointers. Relocation is to
  3231. the entry for the function in the .plt. The magic +2
  3232. offset signals to $$dyncall that the function pointer
  3233. is in the .plt and thus has a gp pointer too.
  3234. Exception: Undefined PLABELs should have a value of
  3235. zero. */
  3236. if (hh == NULL
  3237. || (hh->eh.root.type != bfd_link_hash_undefweak
  3238. && hh->eh.root.type != bfd_link_hash_undefined))
  3239. {
  3240. relocation = (off
  3241. + htab->etab.splt->output_offset
  3242. + htab->etab.splt->output_section->vma
  3243. + 2);
  3244. }
  3245. plabel = 1;
  3246. }
  3247. /* Fall through. */
  3248. case R_PARISC_DIR17F:
  3249. case R_PARISC_DIR17R:
  3250. case R_PARISC_DIR14F:
  3251. case R_PARISC_DIR14R:
  3252. case R_PARISC_DIR21L:
  3253. case R_PARISC_DPREL14F:
  3254. case R_PARISC_DPREL14R:
  3255. case R_PARISC_DPREL21L:
  3256. case R_PARISC_DIR32:
  3257. if ((input_section->flags & SEC_ALLOC) == 0)
  3258. break;
  3259. if (bfd_link_pic (info)
  3260. ? ((hh == NULL
  3261. || hh->eh.dyn_relocs != NULL)
  3262. && ((hh != NULL && pc_dynrelocs (hh))
  3263. || IS_ABSOLUTE_RELOC (r_type)))
  3264. : (hh != NULL
  3265. && hh->eh.dyn_relocs != NULL))
  3266. {
  3267. Elf_Internal_Rela outrel;
  3268. bool skip;
  3269. asection *sreloc;
  3270. bfd_byte *loc;
  3271. /* When generating a shared object, these relocations
  3272. are copied into the output file to be resolved at run
  3273. time. */
  3274. outrel.r_addend = rela->r_addend;
  3275. outrel.r_offset =
  3276. _bfd_elf_section_offset (output_bfd, info, input_section,
  3277. rela->r_offset);
  3278. skip = (outrel.r_offset == (bfd_vma) -1
  3279. || outrel.r_offset == (bfd_vma) -2);
  3280. outrel.r_offset += (input_section->output_offset
  3281. + input_section->output_section->vma);
  3282. if (skip)
  3283. {
  3284. memset (&outrel, 0, sizeof (outrel));
  3285. }
  3286. else if (hh != NULL
  3287. && hh->eh.dynindx != -1
  3288. && (plabel
  3289. || !IS_ABSOLUTE_RELOC (r_type)
  3290. || !bfd_link_pic (info)
  3291. || !SYMBOLIC_BIND (info, &hh->eh)
  3292. || !hh->eh.def_regular))
  3293. {
  3294. outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
  3295. }
  3296. else /* It's a local symbol, or one marked to become local. */
  3297. {
  3298. int indx = 0;
  3299. /* Add the absolute offset of the symbol. */
  3300. outrel.r_addend += relocation;
  3301. /* Global plabels need to be processed by the
  3302. dynamic linker so that functions have at most one
  3303. fptr. For this reason, we need to differentiate
  3304. between global and local plabels, which we do by
  3305. providing the function symbol for a global plabel
  3306. reloc, and no symbol for local plabels. */
  3307. if (! plabel
  3308. && sym_sec != NULL
  3309. && sym_sec->output_section != NULL
  3310. && ! bfd_is_abs_section (sym_sec))
  3311. {
  3312. asection *osec;
  3313. osec = sym_sec->output_section;
  3314. indx = elf_section_data (osec)->dynindx;
  3315. if (indx == 0)
  3316. {
  3317. osec = htab->etab.text_index_section;
  3318. indx = elf_section_data (osec)->dynindx;
  3319. }
  3320. BFD_ASSERT (indx != 0);
  3321. /* We are turning this relocation into one
  3322. against a section symbol, so subtract out the
  3323. output section's address but not the offset
  3324. of the input section in the output section. */
  3325. outrel.r_addend -= osec->vma;
  3326. }
  3327. outrel.r_info = ELF32_R_INFO (indx, r_type);
  3328. }
  3329. sreloc = elf_section_data (input_section)->sreloc;
  3330. if (sreloc == NULL)
  3331. abort ();
  3332. loc = sreloc->contents;
  3333. loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
  3334. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  3335. }
  3336. break;
  3337. case R_PARISC_TLS_LDM21L:
  3338. case R_PARISC_TLS_LDM14R:
  3339. {
  3340. bfd_vma off;
  3341. off = htab->tls_ldm_got.offset;
  3342. if (off & 1)
  3343. off &= ~1;
  3344. else
  3345. {
  3346. Elf_Internal_Rela outrel;
  3347. bfd_byte *loc;
  3348. outrel.r_offset = (off
  3349. + htab->etab.sgot->output_section->vma
  3350. + htab->etab.sgot->output_offset);
  3351. outrel.r_addend = 0;
  3352. outrel.r_info = ELF32_R_INFO (0, R_PARISC_TLS_DTPMOD32);
  3353. loc = htab->etab.srelgot->contents;
  3354. loc += htab->etab.srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
  3355. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  3356. htab->tls_ldm_got.offset |= 1;
  3357. }
  3358. /* Add the base of the GOT to the relocation value. */
  3359. relocation = (off
  3360. + htab->etab.sgot->output_offset
  3361. + htab->etab.sgot->output_section->vma);
  3362. break;
  3363. }
  3364. case R_PARISC_TLS_LDO21L:
  3365. case R_PARISC_TLS_LDO14R:
  3366. relocation -= dtpoff_base (info);
  3367. break;
  3368. case R_PARISC_TLS_GD21L:
  3369. case R_PARISC_TLS_GD14R:
  3370. case R_PARISC_TLS_IE21L:
  3371. case R_PARISC_TLS_IE14R:
  3372. {
  3373. bfd_vma off;
  3374. int indx;
  3375. char tls_type;
  3376. indx = 0;
  3377. if (hh != NULL)
  3378. {
  3379. if (!htab->etab.dynamic_sections_created
  3380. || hh->eh.dynindx == -1
  3381. || SYMBOL_REFERENCES_LOCAL (info, &hh->eh)
  3382. || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &hh->eh))
  3383. /* This is actually a static link, or it is a
  3384. -Bsymbolic link and the symbol is defined
  3385. locally, or the symbol was forced to be local
  3386. because of a version file. */
  3387. ;
  3388. else
  3389. indx = hh->eh.dynindx;
  3390. off = hh->eh.got.offset;
  3391. tls_type = hh->tls_type;
  3392. }
  3393. else
  3394. {
  3395. off = local_got_offsets[r_symndx];
  3396. tls_type = hppa_elf_local_got_tls_type (input_bfd)[r_symndx];
  3397. }
  3398. if (tls_type == GOT_UNKNOWN)
  3399. abort ();
  3400. if ((off & 1) != 0)
  3401. off &= ~1;
  3402. else
  3403. {
  3404. bool need_relocs = false;
  3405. Elf_Internal_Rela outrel;
  3406. bfd_byte *loc = NULL;
  3407. int cur_off = off;
  3408. /* The GOT entries have not been initialized yet. Do it
  3409. now, and emit any relocations. If both an IE GOT and a
  3410. GD GOT are necessary, we emit the GD first. */
  3411. if (indx != 0
  3412. || (bfd_link_dll (info)
  3413. && (hh == NULL
  3414. || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &hh->eh))))
  3415. {
  3416. need_relocs = true;
  3417. loc = htab->etab.srelgot->contents;
  3418. loc += (htab->etab.srelgot->reloc_count
  3419. * sizeof (Elf32_External_Rela));
  3420. }
  3421. if (tls_type & GOT_TLS_GD)
  3422. {
  3423. if (need_relocs)
  3424. {
  3425. outrel.r_offset
  3426. = (cur_off
  3427. + htab->etab.sgot->output_section->vma
  3428. + htab->etab.sgot->output_offset);
  3429. outrel.r_info
  3430. = ELF32_R_INFO (indx, R_PARISC_TLS_DTPMOD32);
  3431. outrel.r_addend = 0;
  3432. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  3433. htab->etab.srelgot->reloc_count++;
  3434. loc += sizeof (Elf32_External_Rela);
  3435. bfd_put_32 (output_bfd, 0,
  3436. htab->etab.sgot->contents + cur_off);
  3437. }
  3438. else
  3439. /* If we are not emitting relocations for a
  3440. general dynamic reference, then we must be in a
  3441. static link or an executable link with the
  3442. symbol binding locally. Mark it as belonging
  3443. to module 1, the executable. */
  3444. bfd_put_32 (output_bfd, 1,
  3445. htab->etab.sgot->contents + cur_off);
  3446. if (indx != 0)
  3447. {
  3448. outrel.r_info
  3449. = ELF32_R_INFO (indx, R_PARISC_TLS_DTPOFF32);
  3450. outrel.r_offset += 4;
  3451. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  3452. htab->etab.srelgot->reloc_count++;
  3453. loc += sizeof (Elf32_External_Rela);
  3454. bfd_put_32 (output_bfd, 0,
  3455. htab->etab.sgot->contents + cur_off + 4);
  3456. }
  3457. else
  3458. bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
  3459. htab->etab.sgot->contents + cur_off + 4);
  3460. cur_off += 8;
  3461. }
  3462. if (tls_type & GOT_TLS_IE)
  3463. {
  3464. if (need_relocs
  3465. && !(bfd_link_executable (info)
  3466. && SYMBOL_REFERENCES_LOCAL (info, &hh->eh)))
  3467. {
  3468. outrel.r_offset
  3469. = (cur_off
  3470. + htab->etab.sgot->output_section->vma
  3471. + htab->etab.sgot->output_offset);
  3472. outrel.r_info = ELF32_R_INFO (indx,
  3473. R_PARISC_TLS_TPREL32);
  3474. if (indx == 0)
  3475. outrel.r_addend = relocation - dtpoff_base (info);
  3476. else
  3477. outrel.r_addend = 0;
  3478. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  3479. htab->etab.srelgot->reloc_count++;
  3480. loc += sizeof (Elf32_External_Rela);
  3481. }
  3482. else
  3483. bfd_put_32 (output_bfd, tpoff (info, relocation),
  3484. htab->etab.sgot->contents + cur_off);
  3485. cur_off += 4;
  3486. }
  3487. if (hh != NULL)
  3488. hh->eh.got.offset |= 1;
  3489. else
  3490. local_got_offsets[r_symndx] |= 1;
  3491. }
  3492. if ((tls_type & GOT_NORMAL) != 0
  3493. && (tls_type & (GOT_TLS_GD | GOT_TLS_LDM | GOT_TLS_IE)) != 0)
  3494. {
  3495. if (hh != NULL)
  3496. _bfd_error_handler (_("%s has both normal and TLS relocs"),
  3497. hh_name (hh));
  3498. else
  3499. {
  3500. Elf_Internal_Sym *isym
  3501. = bfd_sym_from_r_symndx (&htab->etab.sym_cache,
  3502. input_bfd, r_symndx);
  3503. if (isym == NULL)
  3504. return false;
  3505. sym_name
  3506. = bfd_elf_string_from_elf_section (input_bfd,
  3507. symtab_hdr->sh_link,
  3508. isym->st_name);
  3509. if (sym_name == NULL)
  3510. return false;
  3511. if (*sym_name == '\0')
  3512. sym_name = bfd_section_name (sym_sec);
  3513. _bfd_error_handler
  3514. (_("%pB:%s has both normal and TLS relocs"),
  3515. input_bfd, sym_name);
  3516. }
  3517. bfd_set_error (bfd_error_bad_value);
  3518. return false;
  3519. }
  3520. if ((tls_type & GOT_TLS_GD)
  3521. && r_type != R_PARISC_TLS_GD21L
  3522. && r_type != R_PARISC_TLS_GD14R)
  3523. off += 2 * GOT_ENTRY_SIZE;
  3524. /* Add the base of the GOT to the relocation value. */
  3525. relocation = (off
  3526. + htab->etab.sgot->output_offset
  3527. + htab->etab.sgot->output_section->vma);
  3528. break;
  3529. }
  3530. case R_PARISC_TLS_LE21L:
  3531. case R_PARISC_TLS_LE14R:
  3532. {
  3533. relocation = tpoff (info, relocation);
  3534. break;
  3535. }
  3536. break;
  3537. default:
  3538. break;
  3539. }
  3540. rstatus = final_link_relocate (input_section, contents, rela, relocation,
  3541. htab, sym_sec, hh, info);
  3542. if (rstatus == bfd_reloc_ok)
  3543. continue;
  3544. if (hh != NULL)
  3545. sym_name = hh_name (hh);
  3546. else
  3547. {
  3548. sym_name = bfd_elf_string_from_elf_section (input_bfd,
  3549. symtab_hdr->sh_link,
  3550. sym->st_name);
  3551. if (sym_name == NULL)
  3552. return false;
  3553. if (*sym_name == '\0')
  3554. sym_name = bfd_section_name (sym_sec);
  3555. }
  3556. howto = elf_hppa_howto_table + r_type;
  3557. if (rstatus == bfd_reloc_undefined || rstatus == bfd_reloc_notsupported)
  3558. {
  3559. if (rstatus == bfd_reloc_notsupported || !warned_undef)
  3560. {
  3561. _bfd_error_handler
  3562. /* xgettext:c-format */
  3563. (_("%pB(%pA+%#" PRIx64 "): cannot handle %s for %s"),
  3564. input_bfd,
  3565. input_section,
  3566. (uint64_t) rela->r_offset,
  3567. howto->name,
  3568. sym_name);
  3569. bfd_set_error (bfd_error_bad_value);
  3570. return false;
  3571. }
  3572. }
  3573. else
  3574. (*info->callbacks->reloc_overflow)
  3575. (info, (hh ? &hh->eh.root : NULL), sym_name, howto->name,
  3576. (bfd_vma) 0, input_bfd, input_section, rela->r_offset);
  3577. }
  3578. return true;
  3579. }
  3580. /* Finish up dynamic symbol handling. We set the contents of various
  3581. dynamic sections here. */
  3582. static bool
  3583. elf32_hppa_finish_dynamic_symbol (bfd *output_bfd,
  3584. struct bfd_link_info *info,
  3585. struct elf_link_hash_entry *eh,
  3586. Elf_Internal_Sym *sym)
  3587. {
  3588. struct elf32_hppa_link_hash_table *htab;
  3589. Elf_Internal_Rela rela;
  3590. bfd_byte *loc;
  3591. htab = hppa_link_hash_table (info);
  3592. if (htab == NULL)
  3593. return false;
  3594. if (eh->plt.offset != (bfd_vma) -1)
  3595. {
  3596. bfd_vma value;
  3597. if (eh->plt.offset & 1)
  3598. abort ();
  3599. /* This symbol has an entry in the procedure linkage table. Set
  3600. it up.
  3601. The format of a plt entry is
  3602. <funcaddr>
  3603. <__gp>
  3604. */
  3605. value = 0;
  3606. if (eh->root.type == bfd_link_hash_defined
  3607. || eh->root.type == bfd_link_hash_defweak)
  3608. {
  3609. value = eh->root.u.def.value;
  3610. if (eh->root.u.def.section->output_section != NULL)
  3611. value += (eh->root.u.def.section->output_offset
  3612. + eh->root.u.def.section->output_section->vma);
  3613. }
  3614. /* Create a dynamic IPLT relocation for this entry. */
  3615. rela.r_offset = (eh->plt.offset
  3616. + htab->etab.splt->output_offset
  3617. + htab->etab.splt->output_section->vma);
  3618. if (eh->dynindx != -1)
  3619. {
  3620. rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_IPLT);
  3621. rela.r_addend = 0;
  3622. }
  3623. else
  3624. {
  3625. /* This symbol has been marked to become local, and is
  3626. used by a plabel so must be kept in the .plt. */
  3627. rela.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
  3628. rela.r_addend = value;
  3629. }
  3630. loc = htab->etab.srelplt->contents;
  3631. loc += htab->etab.srelplt->reloc_count++ * sizeof (Elf32_External_Rela);
  3632. bfd_elf32_swap_reloca_out (htab->etab.splt->output_section->owner, &rela, loc);
  3633. if (!eh->def_regular)
  3634. {
  3635. /* Mark the symbol as undefined, rather than as defined in
  3636. the .plt section. Leave the value alone. */
  3637. sym->st_shndx = SHN_UNDEF;
  3638. }
  3639. }
  3640. if (eh->got.offset != (bfd_vma) -1
  3641. && (hppa_elf_hash_entry (eh)->tls_type & GOT_NORMAL) != 0
  3642. && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, eh))
  3643. {
  3644. bool is_dyn = (eh->dynindx != -1
  3645. && !SYMBOL_REFERENCES_LOCAL (info, eh));
  3646. if (is_dyn || bfd_link_pic (info))
  3647. {
  3648. /* This symbol has an entry in the global offset table. Set
  3649. it up. */
  3650. rela.r_offset = ((eh->got.offset &~ (bfd_vma) 1)
  3651. + htab->etab.sgot->output_offset
  3652. + htab->etab.sgot->output_section->vma);
  3653. /* If this is a -Bsymbolic link and the symbol is defined
  3654. locally or was forced to be local because of a version
  3655. file, we just want to emit a RELATIVE reloc. The entry
  3656. in the global offset table will already have been
  3657. initialized in the relocate_section function. */
  3658. if (!is_dyn)
  3659. {
  3660. rela.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
  3661. rela.r_addend = (eh->root.u.def.value
  3662. + eh->root.u.def.section->output_offset
  3663. + eh->root.u.def.section->output_section->vma);
  3664. }
  3665. else
  3666. {
  3667. if ((eh->got.offset & 1) != 0)
  3668. abort ();
  3669. bfd_put_32 (output_bfd, 0,
  3670. htab->etab.sgot->contents + (eh->got.offset & ~1));
  3671. rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_DIR32);
  3672. rela.r_addend = 0;
  3673. }
  3674. loc = htab->etab.srelgot->contents;
  3675. loc += (htab->etab.srelgot->reloc_count++
  3676. * sizeof (Elf32_External_Rela));
  3677. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  3678. }
  3679. }
  3680. if (eh->needs_copy)
  3681. {
  3682. asection *sec;
  3683. /* This symbol needs a copy reloc. Set it up. */
  3684. if (! (eh->dynindx != -1
  3685. && (eh->root.type == bfd_link_hash_defined
  3686. || eh->root.type == bfd_link_hash_defweak)))
  3687. abort ();
  3688. rela.r_offset = (eh->root.u.def.value
  3689. + eh->root.u.def.section->output_offset
  3690. + eh->root.u.def.section->output_section->vma);
  3691. rela.r_addend = 0;
  3692. rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_COPY);
  3693. if (eh->root.u.def.section == htab->etab.sdynrelro)
  3694. sec = htab->etab.sreldynrelro;
  3695. else
  3696. sec = htab->etab.srelbss;
  3697. loc = sec->contents + sec->reloc_count++ * sizeof (Elf32_External_Rela);
  3698. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  3699. }
  3700. /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
  3701. if (eh == htab->etab.hdynamic || eh == htab->etab.hgot)
  3702. {
  3703. sym->st_shndx = SHN_ABS;
  3704. }
  3705. return true;
  3706. }
  3707. /* Used to decide how to sort relocs in an optimal manner for the
  3708. dynamic linker, before writing them out. */
  3709. static enum elf_reloc_type_class
  3710. elf32_hppa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  3711. const asection *rel_sec ATTRIBUTE_UNUSED,
  3712. const Elf_Internal_Rela *rela)
  3713. {
  3714. /* Handle TLS relocs first; we don't want them to be marked
  3715. relative by the "if (ELF32_R_SYM (rela->r_info) == STN_UNDEF)"
  3716. check below. */
  3717. switch ((int) ELF32_R_TYPE (rela->r_info))
  3718. {
  3719. case R_PARISC_TLS_DTPMOD32:
  3720. case R_PARISC_TLS_DTPOFF32:
  3721. case R_PARISC_TLS_TPREL32:
  3722. return reloc_class_normal;
  3723. }
  3724. if (ELF32_R_SYM (rela->r_info) == STN_UNDEF)
  3725. return reloc_class_relative;
  3726. switch ((int) ELF32_R_TYPE (rela->r_info))
  3727. {
  3728. case R_PARISC_IPLT:
  3729. return reloc_class_plt;
  3730. case R_PARISC_COPY:
  3731. return reloc_class_copy;
  3732. default:
  3733. return reloc_class_normal;
  3734. }
  3735. }
  3736. /* Finish up the dynamic sections. */
  3737. static bool
  3738. elf32_hppa_finish_dynamic_sections (bfd *output_bfd,
  3739. struct bfd_link_info *info)
  3740. {
  3741. bfd *dynobj;
  3742. struct elf32_hppa_link_hash_table *htab;
  3743. asection *sdyn;
  3744. asection * sgot;
  3745. htab = hppa_link_hash_table (info);
  3746. if (htab == NULL)
  3747. return false;
  3748. dynobj = htab->etab.dynobj;
  3749. sgot = htab->etab.sgot;
  3750. /* A broken linker script might have discarded the dynamic sections.
  3751. Catch this here so that we do not seg-fault later on. */
  3752. if (sgot != NULL && bfd_is_abs_section (sgot->output_section))
  3753. return false;
  3754. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  3755. if (htab->etab.dynamic_sections_created)
  3756. {
  3757. Elf32_External_Dyn *dyncon, *dynconend;
  3758. if (sdyn == NULL)
  3759. abort ();
  3760. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  3761. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  3762. for (; dyncon < dynconend; dyncon++)
  3763. {
  3764. Elf_Internal_Dyn dyn;
  3765. asection *s;
  3766. bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  3767. switch (dyn.d_tag)
  3768. {
  3769. default:
  3770. continue;
  3771. case DT_PLTGOT:
  3772. /* Use PLTGOT to set the GOT register. */
  3773. dyn.d_un.d_ptr = elf_gp (output_bfd);
  3774. break;
  3775. case DT_JMPREL:
  3776. s = htab->etab.srelplt;
  3777. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  3778. break;
  3779. case DT_PLTRELSZ:
  3780. s = htab->etab.srelplt;
  3781. dyn.d_un.d_val = s->size;
  3782. break;
  3783. }
  3784. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  3785. }
  3786. }
  3787. if (sgot != NULL && sgot->size != 0)
  3788. {
  3789. /* Fill in the first entry in the global offset table.
  3790. We use it to point to our dynamic section, if we have one. */
  3791. bfd_put_32 (output_bfd,
  3792. sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
  3793. sgot->contents);
  3794. /* The second entry is reserved for use by the dynamic linker. */
  3795. memset (sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
  3796. /* Set .got entry size. */
  3797. elf_section_data (sgot->output_section)
  3798. ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
  3799. }
  3800. if (htab->etab.splt != NULL && htab->etab.splt->size != 0)
  3801. {
  3802. /* Set plt entry size to 0 instead of PLT_ENTRY_SIZE, since we add the
  3803. plt stubs and as such the section does not hold a table of fixed-size
  3804. entries. */
  3805. elf_section_data (htab->etab.splt->output_section)->this_hdr.sh_entsize = 0;
  3806. if (htab->need_plt_stub)
  3807. {
  3808. /* Set up the .plt stub. */
  3809. memcpy (htab->etab.splt->contents
  3810. + htab->etab.splt->size - sizeof (plt_stub),
  3811. plt_stub, sizeof (plt_stub));
  3812. if ((htab->etab.splt->output_offset
  3813. + htab->etab.splt->output_section->vma
  3814. + htab->etab.splt->size)
  3815. != (sgot->output_offset
  3816. + sgot->output_section->vma))
  3817. {
  3818. _bfd_error_handler
  3819. (_(".got section not immediately after .plt section"));
  3820. return false;
  3821. }
  3822. }
  3823. }
  3824. return true;
  3825. }
  3826. /* Called when writing out an object file to decide the type of a
  3827. symbol. */
  3828. static int
  3829. elf32_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
  3830. {
  3831. if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
  3832. return STT_PARISC_MILLI;
  3833. else
  3834. return type;
  3835. }
  3836. /* Misc BFD support code. */
  3837. #define bfd_elf32_bfd_is_local_label_name elf_hppa_is_local_label_name
  3838. #define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
  3839. #define bfd_elf32_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
  3840. #define elf_info_to_howto elf_hppa_info_to_howto
  3841. #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
  3842. /* Stuff for the BFD linker. */
  3843. #define bfd_elf32_bfd_final_link elf32_hppa_final_link
  3844. #define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
  3845. #define elf_backend_adjust_dynamic_symbol elf32_hppa_adjust_dynamic_symbol
  3846. #define elf_backend_copy_indirect_symbol elf32_hppa_copy_indirect_symbol
  3847. #define elf_backend_check_relocs elf32_hppa_check_relocs
  3848. #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
  3849. #define elf_backend_create_dynamic_sections elf32_hppa_create_dynamic_sections
  3850. #define elf_backend_fake_sections elf_hppa_fake_sections
  3851. #define elf_backend_relocate_section elf32_hppa_relocate_section
  3852. #define elf_backend_hide_symbol elf32_hppa_hide_symbol
  3853. #define elf_backend_finish_dynamic_symbol elf32_hppa_finish_dynamic_symbol
  3854. #define elf_backend_finish_dynamic_sections elf32_hppa_finish_dynamic_sections
  3855. #define elf_backend_size_dynamic_sections elf32_hppa_size_dynamic_sections
  3856. #define elf_backend_init_index_section _bfd_elf_init_1_index_section
  3857. #define elf_backend_gc_mark_hook elf32_hppa_gc_mark_hook
  3858. #define elf_backend_grok_prstatus elf32_hppa_grok_prstatus
  3859. #define elf_backend_grok_psinfo elf32_hppa_grok_psinfo
  3860. #define elf_backend_object_p elf32_hppa_object_p
  3861. #define elf_backend_final_write_processing elf_hppa_final_write_processing
  3862. #define elf_backend_get_symbol_type elf32_hppa_elf_get_symbol_type
  3863. #define elf_backend_reloc_type_class elf32_hppa_reloc_type_class
  3864. #define elf_backend_action_discarded elf_hppa_action_discarded
  3865. #define elf_backend_can_gc_sections 1
  3866. #define elf_backend_can_refcount 1
  3867. #define elf_backend_plt_alignment 2
  3868. #define elf_backend_want_got_plt 0
  3869. #define elf_backend_plt_readonly 0
  3870. #define elf_backend_want_plt_sym 0
  3871. #define elf_backend_got_header_size 8
  3872. #define elf_backend_want_dynrelro 1
  3873. #define elf_backend_rela_normal 1
  3874. #define elf_backend_dtrel_excludes_plt 1
  3875. #define elf_backend_no_page_alias 1
  3876. #define TARGET_BIG_SYM hppa_elf32_vec
  3877. #define TARGET_BIG_NAME "elf32-hppa"
  3878. #define ELF_ARCH bfd_arch_hppa
  3879. #define ELF_TARGET_ID HPPA32_ELF_DATA
  3880. #define ELF_MACHINE_CODE EM_PARISC
  3881. #define ELF_MAXPAGESIZE 0x1000
  3882. #define ELF_OSABI ELFOSABI_HPUX
  3883. #define elf32_bed elf32_hppa_hpux_bed
  3884. #include "elf32-target.h"
  3885. #undef TARGET_BIG_SYM
  3886. #define TARGET_BIG_SYM hppa_elf32_linux_vec
  3887. #undef TARGET_BIG_NAME
  3888. #define TARGET_BIG_NAME "elf32-hppa-linux"
  3889. #undef ELF_OSABI
  3890. #define ELF_OSABI ELFOSABI_GNU
  3891. #undef elf32_bed
  3892. #define elf32_bed elf32_hppa_linux_bed
  3893. #include "elf32-target.h"
  3894. #undef TARGET_BIG_SYM
  3895. #define TARGET_BIG_SYM hppa_elf32_nbsd_vec
  3896. #undef TARGET_BIG_NAME
  3897. #define TARGET_BIG_NAME "elf32-hppa-netbsd"
  3898. #undef ELF_OSABI
  3899. #define ELF_OSABI ELFOSABI_NETBSD
  3900. #undef elf32_bed
  3901. #define elf32_bed elf32_hppa_netbsd_bed
  3902. #include "elf32-target.h"