elf64-hppa.c 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046
  1. /* Support for HPPA 64-bit ELF
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libbfd.h"
  19. #include "elf-bfd.h"
  20. #include "elf/hppa.h"
  21. #include "libhppa.h"
  22. #include "elf64-hppa.h"
  23. #include "libiberty.h"
  24. #define ARCH_SIZE 64
  25. #define PLT_ENTRY_SIZE 0x10
  26. #define DLT_ENTRY_SIZE 0x8
  27. #define OPD_ENTRY_SIZE 0x20
  28. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/pa20_64/dld.sl"
  29. /* The stub is supposed to load the target address and target's DP
  30. value out of the PLT, then do an external branch to the target
  31. address.
  32. LDD PLTOFF(%r27),%r1
  33. BVE (%r1)
  34. LDD PLTOFF+8(%r27),%r27
  35. Note that we must use the LDD with a 14 bit displacement, not the one
  36. with a 5 bit displacement. */
  37. static char plt_stub[] = {0x53, 0x61, 0x00, 0x00, 0xe8, 0x20, 0xd0, 0x00,
  38. 0x53, 0x7b, 0x00, 0x00 };
  39. struct elf64_hppa_link_hash_entry
  40. {
  41. struct elf_link_hash_entry eh;
  42. /* Offsets for this symbol in various linker sections. */
  43. bfd_vma dlt_offset;
  44. bfd_vma plt_offset;
  45. bfd_vma opd_offset;
  46. bfd_vma stub_offset;
  47. /* The index of the (possibly local) symbol in the input bfd and its
  48. associated BFD. Needed so that we can have relocs against local
  49. symbols in shared libraries. */
  50. long sym_indx;
  51. bfd *owner;
  52. /* Dynamic symbols may need to have two different values. One for
  53. the dynamic symbol table, one for the normal symbol table.
  54. In such cases we store the symbol's real value and section
  55. index here so we can restore the real value before we write
  56. the normal symbol table. */
  57. bfd_vma st_value;
  58. int st_shndx;
  59. /* Used to count non-got, non-plt relocations for delayed sizing
  60. of relocation sections. */
  61. struct elf64_hppa_dyn_reloc_entry
  62. {
  63. /* Next relocation in the chain. */
  64. struct elf64_hppa_dyn_reloc_entry *next;
  65. /* The type of the relocation. */
  66. int type;
  67. /* The input section of the relocation. */
  68. asection *sec;
  69. /* Number of relocs copied in this section. */
  70. bfd_size_type count;
  71. /* The index of the section symbol for the input section of
  72. the relocation. Only needed when building shared libraries. */
  73. int sec_symndx;
  74. /* The offset within the input section of the relocation. */
  75. bfd_vma offset;
  76. /* The addend for the relocation. */
  77. bfd_vma addend;
  78. } *reloc_entries;
  79. /* Nonzero if this symbol needs an entry in one of the linker
  80. sections. */
  81. unsigned want_dlt;
  82. unsigned want_plt;
  83. unsigned want_opd;
  84. unsigned want_stub;
  85. };
  86. struct elf64_hppa_link_hash_table
  87. {
  88. struct elf_link_hash_table root;
  89. /* Shortcuts to get to the various linker defined sections. */
  90. asection *dlt_sec;
  91. asection *dlt_rel_sec;
  92. asection *opd_sec;
  93. asection *opd_rel_sec;
  94. asection *other_rel_sec;
  95. /* Offset of __gp within .plt section. When the PLT gets large we want
  96. to slide __gp into the PLT section so that we can continue to use
  97. single DP relative instructions to load values out of the PLT. */
  98. bfd_vma gp_offset;
  99. /* Note this is not strictly correct. We should create a stub section for
  100. each input section with calls. The stub section should be placed before
  101. the section with the call. */
  102. asection *stub_sec;
  103. bfd_vma text_segment_base;
  104. bfd_vma data_segment_base;
  105. /* We build tables to map from an input section back to its
  106. symbol index. This is the BFD for which we currently have
  107. a map. */
  108. bfd *section_syms_bfd;
  109. /* Array of symbol numbers for each input section attached to the
  110. current BFD. */
  111. int *section_syms;
  112. };
  113. #define hppa_link_hash_table(p) \
  114. ((is_elf_hash_table ((p)->hash) \
  115. && elf_hash_table_id (elf_hash_table (p)) == HPPA64_ELF_DATA) \
  116. ? (struct elf64_hppa_link_hash_table *) (p)->hash : NULL)
  117. #define hppa_elf_hash_entry(ent) \
  118. ((struct elf64_hppa_link_hash_entry *)(ent))
  119. #define eh_name(eh) \
  120. (eh ? eh->root.root.string : "<undef>")
  121. typedef struct bfd_hash_entry *(*new_hash_entry_func)
  122. (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
  123. static struct bfd_link_hash_table *elf64_hppa_hash_table_create
  124. (bfd *abfd);
  125. /* This must follow the definitions of the various derived linker
  126. hash tables and shared functions. */
  127. #include "elf-hppa.h"
  128. static bool elf64_hppa_object_p
  129. (bfd *);
  130. static bool elf64_hppa_create_dynamic_sections
  131. (bfd *, struct bfd_link_info *);
  132. static bool elf64_hppa_adjust_dynamic_symbol
  133. (struct bfd_link_info *, struct elf_link_hash_entry *);
  134. static bool elf64_hppa_mark_milli_and_exported_functions
  135. (struct elf_link_hash_entry *, void *);
  136. static bool elf64_hppa_size_dynamic_sections
  137. (bfd *, struct bfd_link_info *);
  138. static int elf64_hppa_link_output_symbol_hook
  139. (struct bfd_link_info *, const char *, Elf_Internal_Sym *,
  140. asection *, struct elf_link_hash_entry *);
  141. static bool elf64_hppa_finish_dynamic_symbol
  142. (bfd *, struct bfd_link_info *,
  143. struct elf_link_hash_entry *, Elf_Internal_Sym *);
  144. static bool elf64_hppa_finish_dynamic_sections
  145. (bfd *, struct bfd_link_info *);
  146. static bool elf64_hppa_check_relocs
  147. (bfd *, struct bfd_link_info *,
  148. asection *, const Elf_Internal_Rela *);
  149. static bool elf64_hppa_dynamic_symbol_p
  150. (struct elf_link_hash_entry *, struct bfd_link_info *);
  151. static bool elf64_hppa_mark_exported_functions
  152. (struct elf_link_hash_entry *, void *);
  153. static bool elf64_hppa_finalize_opd
  154. (struct elf_link_hash_entry *, void *);
  155. static bool elf64_hppa_finalize_dlt
  156. (struct elf_link_hash_entry *, void *);
  157. static bool allocate_global_data_dlt
  158. (struct elf_link_hash_entry *, void *);
  159. static bool allocate_global_data_plt
  160. (struct elf_link_hash_entry *, void *);
  161. static bool allocate_global_data_stub
  162. (struct elf_link_hash_entry *, void *);
  163. static bool allocate_global_data_opd
  164. (struct elf_link_hash_entry *, void *);
  165. static bool get_reloc_section
  166. (bfd *, struct elf64_hppa_link_hash_table *, asection *);
  167. static bool count_dyn_reloc
  168. (bfd *, struct elf64_hppa_link_hash_entry *,
  169. int, asection *, int, bfd_vma, bfd_vma);
  170. static bool allocate_dynrel_entries
  171. (struct elf_link_hash_entry *, void *);
  172. static bool elf64_hppa_finalize_dynreloc
  173. (struct elf_link_hash_entry *, void *);
  174. static bool get_opd
  175. (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
  176. static bool get_plt
  177. (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
  178. static bool get_dlt
  179. (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
  180. static bool get_stub
  181. (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
  182. static int elf64_hppa_elf_get_symbol_type
  183. (Elf_Internal_Sym *, int);
  184. /* Initialize an entry in the link hash table. */
  185. static struct bfd_hash_entry *
  186. hppa64_link_hash_newfunc (struct bfd_hash_entry *entry,
  187. struct bfd_hash_table *table,
  188. const char *string)
  189. {
  190. /* Allocate the structure if it has not already been allocated by a
  191. subclass. */
  192. if (entry == NULL)
  193. {
  194. entry = bfd_hash_allocate (table,
  195. sizeof (struct elf64_hppa_link_hash_entry));
  196. if (entry == NULL)
  197. return entry;
  198. }
  199. /* Call the allocation method of the superclass. */
  200. entry = _bfd_elf_link_hash_newfunc (entry, table, string);
  201. if (entry != NULL)
  202. {
  203. struct elf64_hppa_link_hash_entry *hh;
  204. /* Initialize our local data. All zeros. */
  205. hh = hppa_elf_hash_entry (entry);
  206. memset (&hh->dlt_offset, 0,
  207. (sizeof (struct elf64_hppa_link_hash_entry)
  208. - offsetof (struct elf64_hppa_link_hash_entry, dlt_offset)));
  209. }
  210. return entry;
  211. }
  212. /* Create the derived linker hash table. The PA64 ELF port uses this
  213. derived hash table to keep information specific to the PA ElF
  214. linker (without using static variables). */
  215. static struct bfd_link_hash_table*
  216. elf64_hppa_hash_table_create (bfd *abfd)
  217. {
  218. struct elf64_hppa_link_hash_table *htab;
  219. size_t amt = sizeof (*htab);
  220. htab = bfd_zmalloc (amt);
  221. if (htab == NULL)
  222. return NULL;
  223. if (!_bfd_elf_link_hash_table_init (&htab->root, abfd,
  224. hppa64_link_hash_newfunc,
  225. sizeof (struct elf64_hppa_link_hash_entry),
  226. HPPA64_ELF_DATA))
  227. {
  228. free (htab);
  229. return NULL;
  230. }
  231. htab->root.dt_pltgot_required = true;
  232. htab->text_segment_base = (bfd_vma) -1;
  233. htab->data_segment_base = (bfd_vma) -1;
  234. return &htab->root.root;
  235. }
  236. /* Return nonzero if ABFD represents a PA2.0 ELF64 file.
  237. Additionally we set the default architecture and machine. */
  238. static bool
  239. elf64_hppa_object_p (bfd *abfd)
  240. {
  241. Elf_Internal_Ehdr * i_ehdrp;
  242. unsigned int flags;
  243. i_ehdrp = elf_elfheader (abfd);
  244. if (strcmp (bfd_get_target (abfd), "elf64-hppa-linux") == 0)
  245. {
  246. /* GCC on hppa-linux produces binaries with OSABI=GNU,
  247. but the kernel produces corefiles with OSABI=SysV. */
  248. if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
  249. && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
  250. return false;
  251. }
  252. else
  253. {
  254. /* HPUX produces binaries with OSABI=HPUX,
  255. but the kernel produces corefiles with OSABI=SysV. */
  256. if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX
  257. && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
  258. return false;
  259. }
  260. flags = i_ehdrp->e_flags;
  261. switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
  262. {
  263. case EFA_PARISC_1_0:
  264. return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
  265. case EFA_PARISC_1_1:
  266. return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
  267. case EFA_PARISC_2_0:
  268. if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
  269. return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
  270. else
  271. return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
  272. case EFA_PARISC_2_0 | EF_PARISC_WIDE:
  273. return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
  274. }
  275. /* Don't be fussy. */
  276. return true;
  277. }
  278. /* Given section type (hdr->sh_type), return a boolean indicating
  279. whether or not the section is an elf64-hppa specific section. */
  280. static bool
  281. elf64_hppa_section_from_shdr (bfd *abfd,
  282. Elf_Internal_Shdr *hdr,
  283. const char *name,
  284. int shindex)
  285. {
  286. switch (hdr->sh_type)
  287. {
  288. case SHT_PARISC_EXT:
  289. if (strcmp (name, ".PARISC.archext") != 0)
  290. return false;
  291. break;
  292. case SHT_PARISC_UNWIND:
  293. if (strcmp (name, ".PARISC.unwind") != 0)
  294. return false;
  295. break;
  296. case SHT_PARISC_DOC:
  297. case SHT_PARISC_ANNOT:
  298. default:
  299. return false;
  300. }
  301. if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
  302. return false;
  303. return ((hdr->sh_flags & SHF_PARISC_SHORT) == 0
  304. || bfd_set_section_flags (hdr->bfd_section,
  305. hdr->bfd_section->flags | SEC_SMALL_DATA));
  306. }
  307. /* SEC is a section containing relocs for an input BFD when linking; return
  308. a suitable section for holding relocs in the output BFD for a link. */
  309. static bool
  310. get_reloc_section (bfd *abfd,
  311. struct elf64_hppa_link_hash_table *hppa_info,
  312. asection *sec)
  313. {
  314. const char *srel_name;
  315. asection *srel;
  316. bfd *dynobj;
  317. srel_name = (bfd_elf_string_from_elf_section
  318. (abfd, elf_elfheader(abfd)->e_shstrndx,
  319. _bfd_elf_single_rel_hdr(sec)->sh_name));
  320. if (srel_name == NULL)
  321. return false;
  322. dynobj = hppa_info->root.dynobj;
  323. if (!dynobj)
  324. hppa_info->root.dynobj = dynobj = abfd;
  325. srel = bfd_get_linker_section (dynobj, srel_name);
  326. if (srel == NULL)
  327. {
  328. srel = bfd_make_section_anyway_with_flags (dynobj, srel_name,
  329. (SEC_ALLOC
  330. | SEC_LOAD
  331. | SEC_HAS_CONTENTS
  332. | SEC_IN_MEMORY
  333. | SEC_LINKER_CREATED
  334. | SEC_READONLY));
  335. if (srel == NULL
  336. || !bfd_set_section_alignment (srel, 3))
  337. return false;
  338. }
  339. hppa_info->other_rel_sec = srel;
  340. return true;
  341. }
  342. /* Add a new entry to the list of dynamic relocations against DYN_H.
  343. We use this to keep a record of all the FPTR relocations against a
  344. particular symbol so that we can create FPTR relocations in the
  345. output file. */
  346. static bool
  347. count_dyn_reloc (bfd *abfd,
  348. struct elf64_hppa_link_hash_entry *hh,
  349. int type,
  350. asection *sec,
  351. int sec_symndx,
  352. bfd_vma offset,
  353. bfd_vma addend)
  354. {
  355. struct elf64_hppa_dyn_reloc_entry *rent;
  356. rent = (struct elf64_hppa_dyn_reloc_entry *)
  357. bfd_alloc (abfd, (bfd_size_type) sizeof (*rent));
  358. if (!rent)
  359. return false;
  360. rent->next = hh->reloc_entries;
  361. rent->type = type;
  362. rent->sec = sec;
  363. rent->sec_symndx = sec_symndx;
  364. rent->offset = offset;
  365. rent->addend = addend;
  366. hh->reloc_entries = rent;
  367. return true;
  368. }
  369. /* Return a pointer to the local DLT, PLT and OPD reference counts
  370. for ABFD. Returns NULL if the storage allocation fails. */
  371. static bfd_signed_vma *
  372. hppa64_elf_local_refcounts (bfd *abfd)
  373. {
  374. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  375. bfd_signed_vma *local_refcounts;
  376. local_refcounts = elf_local_got_refcounts (abfd);
  377. if (local_refcounts == NULL)
  378. {
  379. bfd_size_type size;
  380. /* Allocate space for local DLT, PLT and OPD reference
  381. counts. Done this way to save polluting elf_obj_tdata
  382. with another target specific pointer. */
  383. size = symtab_hdr->sh_info;
  384. size *= 3 * sizeof (bfd_signed_vma);
  385. local_refcounts = bfd_zalloc (abfd, size);
  386. elf_local_got_refcounts (abfd) = local_refcounts;
  387. }
  388. return local_refcounts;
  389. }
  390. /* Scan the RELOCS and record the type of dynamic entries that each
  391. referenced symbol needs. */
  392. static bool
  393. elf64_hppa_check_relocs (bfd *abfd,
  394. struct bfd_link_info *info,
  395. asection *sec,
  396. const Elf_Internal_Rela *relocs)
  397. {
  398. struct elf64_hppa_link_hash_table *hppa_info;
  399. const Elf_Internal_Rela *relend;
  400. Elf_Internal_Shdr *symtab_hdr;
  401. const Elf_Internal_Rela *rel;
  402. unsigned int sec_symndx;
  403. if (bfd_link_relocatable (info))
  404. return true;
  405. /* If this is the first dynamic object found in the link, create
  406. the special sections required for dynamic linking. */
  407. if (! elf_hash_table (info)->dynamic_sections_created)
  408. {
  409. if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
  410. return false;
  411. }
  412. hppa_info = hppa_link_hash_table (info);
  413. if (hppa_info == NULL)
  414. return false;
  415. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  416. /* If necessary, build a new table holding section symbols indices
  417. for this BFD. */
  418. if (bfd_link_pic (info) && hppa_info->section_syms_bfd != abfd)
  419. {
  420. unsigned long i;
  421. unsigned int highest_shndx;
  422. Elf_Internal_Sym *local_syms = NULL;
  423. Elf_Internal_Sym *isym, *isymend;
  424. bfd_size_type amt;
  425. /* We're done with the old cache of section index to section symbol
  426. index information. Free it.
  427. ?!? Note we leak the last section_syms array. Presumably we
  428. could free it in one of the later routines in this file. */
  429. free (hppa_info->section_syms);
  430. /* Read this BFD's local symbols. */
  431. if (symtab_hdr->sh_info != 0)
  432. {
  433. local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
  434. if (local_syms == NULL)
  435. local_syms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  436. symtab_hdr->sh_info, 0,
  437. NULL, NULL, NULL);
  438. if (local_syms == NULL)
  439. return false;
  440. }
  441. /* Record the highest section index referenced by the local symbols. */
  442. highest_shndx = 0;
  443. isymend = local_syms + symtab_hdr->sh_info;
  444. for (isym = local_syms; isym < isymend; isym++)
  445. {
  446. if (isym->st_shndx > highest_shndx
  447. && isym->st_shndx < SHN_LORESERVE)
  448. highest_shndx = isym->st_shndx;
  449. }
  450. /* Allocate an array to hold the section index to section symbol index
  451. mapping. Bump by one since we start counting at zero. */
  452. highest_shndx++;
  453. amt = highest_shndx;
  454. amt *= sizeof (int);
  455. hppa_info->section_syms = (int *) bfd_malloc (amt);
  456. /* Now walk the local symbols again. If we find a section symbol,
  457. record the index of the symbol into the section_syms array. */
  458. for (i = 0, isym = local_syms; isym < isymend; i++, isym++)
  459. {
  460. if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
  461. hppa_info->section_syms[isym->st_shndx] = i;
  462. }
  463. /* We are finished with the local symbols. */
  464. if (local_syms != NULL
  465. && symtab_hdr->contents != (unsigned char *) local_syms)
  466. {
  467. if (! info->keep_memory)
  468. free (local_syms);
  469. else
  470. {
  471. /* Cache the symbols for elf_link_input_bfd. */
  472. symtab_hdr->contents = (unsigned char *) local_syms;
  473. }
  474. }
  475. /* Record which BFD we built the section_syms mapping for. */
  476. hppa_info->section_syms_bfd = abfd;
  477. }
  478. /* Record the symbol index for this input section. We may need it for
  479. relocations when building shared libraries. When not building shared
  480. libraries this value is never really used, but assign it to zero to
  481. prevent out of bounds memory accesses in other routines. */
  482. if (bfd_link_pic (info))
  483. {
  484. sec_symndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  485. /* If we did not find a section symbol for this section, then
  486. something went terribly wrong above. */
  487. if (sec_symndx == SHN_BAD)
  488. return false;
  489. if (sec_symndx < SHN_LORESERVE)
  490. sec_symndx = hppa_info->section_syms[sec_symndx];
  491. else
  492. sec_symndx = 0;
  493. }
  494. else
  495. sec_symndx = 0;
  496. relend = relocs + sec->reloc_count;
  497. for (rel = relocs; rel < relend; ++rel)
  498. {
  499. enum
  500. {
  501. NEED_DLT = 1,
  502. NEED_PLT = 2,
  503. NEED_STUB = 4,
  504. NEED_OPD = 8,
  505. NEED_DYNREL = 16,
  506. };
  507. unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
  508. struct elf64_hppa_link_hash_entry *hh;
  509. int need_entry;
  510. bool maybe_dynamic;
  511. int dynrel_type = R_PARISC_NONE;
  512. static reloc_howto_type *howto;
  513. if (r_symndx >= symtab_hdr->sh_info)
  514. {
  515. /* We're dealing with a global symbol -- find its hash entry
  516. and mark it as being referenced. */
  517. long indx = r_symndx - symtab_hdr->sh_info;
  518. hh = hppa_elf_hash_entry (elf_sym_hashes (abfd)[indx]);
  519. while (hh->eh.root.type == bfd_link_hash_indirect
  520. || hh->eh.root.type == bfd_link_hash_warning)
  521. hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
  522. /* PR15323, ref flags aren't set for references in the same
  523. object. */
  524. hh->eh.ref_regular = 1;
  525. }
  526. else
  527. hh = NULL;
  528. /* We can only get preliminary data on whether a symbol is
  529. locally or externally defined, as not all of the input files
  530. have yet been processed. Do something with what we know, as
  531. this may help reduce memory usage and processing time later. */
  532. maybe_dynamic = false;
  533. if (hh && ((bfd_link_pic (info)
  534. && (!info->symbolic
  535. || info->unresolved_syms_in_shared_libs == RM_IGNORE))
  536. || !hh->eh.def_regular
  537. || hh->eh.root.type == bfd_link_hash_defweak))
  538. maybe_dynamic = true;
  539. howto = elf_hppa_howto_table + ELF64_R_TYPE (rel->r_info);
  540. need_entry = 0;
  541. switch (howto->type)
  542. {
  543. /* These are simple indirect references to symbols through the
  544. DLT. We need to create a DLT entry for any symbols which
  545. appears in a DLTIND relocation. */
  546. case R_PARISC_DLTIND21L:
  547. case R_PARISC_DLTIND14R:
  548. case R_PARISC_DLTIND14F:
  549. case R_PARISC_DLTIND14WR:
  550. case R_PARISC_DLTIND14DR:
  551. need_entry = NEED_DLT;
  552. break;
  553. /* ?!? These need a DLT entry. But I have no idea what to do with
  554. the "link time TP value. */
  555. case R_PARISC_LTOFF_TP21L:
  556. case R_PARISC_LTOFF_TP14R:
  557. case R_PARISC_LTOFF_TP14F:
  558. case R_PARISC_LTOFF_TP64:
  559. case R_PARISC_LTOFF_TP14WR:
  560. case R_PARISC_LTOFF_TP14DR:
  561. case R_PARISC_LTOFF_TP16F:
  562. case R_PARISC_LTOFF_TP16WF:
  563. case R_PARISC_LTOFF_TP16DF:
  564. need_entry = NEED_DLT;
  565. break;
  566. /* These are function calls. Depending on their precise target we
  567. may need to make a stub for them. The stub uses the PLT, so we
  568. need to create PLT entries for these symbols too. */
  569. case R_PARISC_PCREL12F:
  570. case R_PARISC_PCREL17F:
  571. case R_PARISC_PCREL22F:
  572. case R_PARISC_PCREL32:
  573. case R_PARISC_PCREL64:
  574. case R_PARISC_PCREL21L:
  575. case R_PARISC_PCREL17R:
  576. case R_PARISC_PCREL17C:
  577. case R_PARISC_PCREL14R:
  578. case R_PARISC_PCREL14F:
  579. case R_PARISC_PCREL22C:
  580. case R_PARISC_PCREL14WR:
  581. case R_PARISC_PCREL14DR:
  582. case R_PARISC_PCREL16F:
  583. case R_PARISC_PCREL16WF:
  584. case R_PARISC_PCREL16DF:
  585. /* Function calls might need to go through the .plt, and
  586. might need a long branch stub. */
  587. if (hh != NULL && hh->eh.type != STT_PARISC_MILLI)
  588. need_entry = (NEED_PLT | NEED_STUB);
  589. else
  590. need_entry = 0;
  591. break;
  592. case R_PARISC_PLTOFF21L:
  593. case R_PARISC_PLTOFF14R:
  594. case R_PARISC_PLTOFF14F:
  595. case R_PARISC_PLTOFF14WR:
  596. case R_PARISC_PLTOFF14DR:
  597. case R_PARISC_PLTOFF16F:
  598. case R_PARISC_PLTOFF16WF:
  599. case R_PARISC_PLTOFF16DF:
  600. need_entry = (NEED_PLT);
  601. break;
  602. case R_PARISC_DIR64:
  603. if (bfd_link_pic (info) || maybe_dynamic)
  604. need_entry = (NEED_DYNREL);
  605. dynrel_type = R_PARISC_DIR64;
  606. break;
  607. /* This is an indirect reference through the DLT to get the address
  608. of a OPD descriptor. Thus we need to make a DLT entry that points
  609. to an OPD entry. */
  610. case R_PARISC_LTOFF_FPTR21L:
  611. case R_PARISC_LTOFF_FPTR14R:
  612. case R_PARISC_LTOFF_FPTR14WR:
  613. case R_PARISC_LTOFF_FPTR14DR:
  614. case R_PARISC_LTOFF_FPTR32:
  615. case R_PARISC_LTOFF_FPTR64:
  616. case R_PARISC_LTOFF_FPTR16F:
  617. case R_PARISC_LTOFF_FPTR16WF:
  618. case R_PARISC_LTOFF_FPTR16DF:
  619. if (bfd_link_pic (info) || maybe_dynamic)
  620. need_entry = (NEED_DLT | NEED_OPD | NEED_PLT);
  621. else
  622. need_entry = (NEED_DLT | NEED_OPD | NEED_PLT);
  623. dynrel_type = R_PARISC_FPTR64;
  624. break;
  625. /* This is a simple OPD entry. */
  626. case R_PARISC_FPTR64:
  627. if (bfd_link_pic (info) || maybe_dynamic)
  628. need_entry = (NEED_OPD | NEED_PLT | NEED_DYNREL);
  629. else
  630. need_entry = (NEED_OPD | NEED_PLT);
  631. dynrel_type = R_PARISC_FPTR64;
  632. break;
  633. /* Add more cases as needed. */
  634. }
  635. if (!need_entry)
  636. continue;
  637. if (hh)
  638. {
  639. /* Stash away enough information to be able to find this symbol
  640. regardless of whether or not it is local or global. */
  641. hh->owner = abfd;
  642. hh->sym_indx = r_symndx;
  643. }
  644. /* Create what's needed. */
  645. if (need_entry & NEED_DLT)
  646. {
  647. /* Allocate space for a DLT entry, as well as a dynamic
  648. relocation for this entry. */
  649. if (! hppa_info->dlt_sec
  650. && ! get_dlt (abfd, info, hppa_info))
  651. goto err_out;
  652. if (hh != NULL)
  653. {
  654. hh->want_dlt = 1;
  655. hh->eh.got.refcount += 1;
  656. }
  657. else
  658. {
  659. bfd_signed_vma *local_dlt_refcounts;
  660. /* This is a DLT entry for a local symbol. */
  661. local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
  662. if (local_dlt_refcounts == NULL)
  663. return false;
  664. local_dlt_refcounts[r_symndx] += 1;
  665. }
  666. }
  667. if (need_entry & NEED_PLT)
  668. {
  669. if (! hppa_info->root.splt
  670. && ! get_plt (abfd, info, hppa_info))
  671. goto err_out;
  672. if (hh != NULL)
  673. {
  674. hh->want_plt = 1;
  675. hh->eh.needs_plt = 1;
  676. hh->eh.plt.refcount += 1;
  677. }
  678. else
  679. {
  680. bfd_signed_vma *local_dlt_refcounts;
  681. bfd_signed_vma *local_plt_refcounts;
  682. /* This is a PLT entry for a local symbol. */
  683. local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
  684. if (local_dlt_refcounts == NULL)
  685. return false;
  686. local_plt_refcounts = local_dlt_refcounts + symtab_hdr->sh_info;
  687. local_plt_refcounts[r_symndx] += 1;
  688. }
  689. }
  690. if (need_entry & NEED_STUB)
  691. {
  692. if (! hppa_info->stub_sec
  693. && ! get_stub (abfd, info, hppa_info))
  694. goto err_out;
  695. if (hh)
  696. hh->want_stub = 1;
  697. }
  698. if (need_entry & NEED_OPD)
  699. {
  700. if (! hppa_info->opd_sec
  701. && ! get_opd (abfd, info, hppa_info))
  702. goto err_out;
  703. /* FPTRs are not allocated by the dynamic linker for PA64,
  704. though it is possible that will change in the future. */
  705. if (hh != NULL)
  706. hh->want_opd = 1;
  707. else
  708. {
  709. bfd_signed_vma *local_dlt_refcounts;
  710. bfd_signed_vma *local_opd_refcounts;
  711. /* This is a OPD for a local symbol. */
  712. local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
  713. if (local_dlt_refcounts == NULL)
  714. return false;
  715. local_opd_refcounts = (local_dlt_refcounts
  716. + 2 * symtab_hdr->sh_info);
  717. local_opd_refcounts[r_symndx] += 1;
  718. }
  719. }
  720. /* Add a new dynamic relocation to the chain of dynamic
  721. relocations for this symbol. */
  722. if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
  723. {
  724. if (! hppa_info->other_rel_sec
  725. && ! get_reloc_section (abfd, hppa_info, sec))
  726. goto err_out;
  727. /* Count dynamic relocations against global symbols. */
  728. if (hh != NULL
  729. && !count_dyn_reloc (abfd, hh, dynrel_type, sec,
  730. sec_symndx, rel->r_offset, rel->r_addend))
  731. goto err_out;
  732. /* If we are building a shared library and we just recorded
  733. a dynamic R_PARISC_FPTR64 relocation, then make sure the
  734. section symbol for this section ends up in the dynamic
  735. symbol table. */
  736. if (bfd_link_pic (info) && dynrel_type == R_PARISC_FPTR64
  737. && ! (bfd_elf_link_record_local_dynamic_symbol
  738. (info, abfd, sec_symndx)))
  739. return false;
  740. }
  741. }
  742. return true;
  743. err_out:
  744. return false;
  745. }
  746. struct elf64_hppa_allocate_data
  747. {
  748. struct bfd_link_info *info;
  749. bfd_size_type ofs;
  750. };
  751. /* Should we do dynamic things to this symbol? */
  752. static bool
  753. elf64_hppa_dynamic_symbol_p (struct elf_link_hash_entry *eh,
  754. struct bfd_link_info *info)
  755. {
  756. /* ??? What, if anything, needs to happen wrt STV_PROTECTED symbols
  757. and relocations that retrieve a function descriptor? Assume the
  758. worst for now. */
  759. if (_bfd_elf_dynamic_symbol_p (eh, info, 1))
  760. {
  761. /* ??? Why is this here and not elsewhere is_local_label_name. */
  762. if (eh->root.root.string[0] == '$' && eh->root.root.string[1] == '$')
  763. return false;
  764. return true;
  765. }
  766. else
  767. return false;
  768. }
  769. /* Mark all functions exported by this file so that we can later allocate
  770. entries in .opd for them. */
  771. static bool
  772. elf64_hppa_mark_exported_functions (struct elf_link_hash_entry *eh, void *data)
  773. {
  774. struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
  775. struct bfd_link_info *info = (struct bfd_link_info *)data;
  776. struct elf64_hppa_link_hash_table *hppa_info;
  777. hppa_info = hppa_link_hash_table (info);
  778. if (hppa_info == NULL)
  779. return false;
  780. if (eh
  781. && (eh->root.type == bfd_link_hash_defined
  782. || eh->root.type == bfd_link_hash_defweak)
  783. && eh->root.u.def.section->output_section != NULL
  784. && eh->type == STT_FUNC)
  785. {
  786. if (! hppa_info->opd_sec
  787. && ! get_opd (hppa_info->root.dynobj, info, hppa_info))
  788. return false;
  789. hh->want_opd = 1;
  790. /* Put a flag here for output_symbol_hook. */
  791. hh->st_shndx = -1;
  792. eh->needs_plt = 1;
  793. }
  794. return true;
  795. }
  796. /* Allocate space for a DLT entry. */
  797. static bool
  798. allocate_global_data_dlt (struct elf_link_hash_entry *eh, void *data)
  799. {
  800. struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
  801. struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
  802. if (hh->want_dlt)
  803. {
  804. if (bfd_link_pic (x->info))
  805. {
  806. /* Possibly add the symbol to the local dynamic symbol
  807. table since we might need to create a dynamic relocation
  808. against it. */
  809. if (eh->dynindx == -1 && eh->type != STT_PARISC_MILLI)
  810. {
  811. bfd *owner = eh->root.u.def.section->owner;
  812. if (! (bfd_elf_link_record_local_dynamic_symbol
  813. (x->info, owner, hh->sym_indx)))
  814. return false;
  815. }
  816. }
  817. hh->dlt_offset = x->ofs;
  818. x->ofs += DLT_ENTRY_SIZE;
  819. }
  820. return true;
  821. }
  822. /* Allocate space for a DLT.PLT entry. */
  823. static bool
  824. allocate_global_data_plt (struct elf_link_hash_entry *eh, void *data)
  825. {
  826. struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
  827. struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *) data;
  828. if (hh->want_plt
  829. && elf64_hppa_dynamic_symbol_p (eh, x->info)
  830. && !((eh->root.type == bfd_link_hash_defined
  831. || eh->root.type == bfd_link_hash_defweak)
  832. && eh->root.u.def.section->output_section != NULL))
  833. {
  834. hh->plt_offset = x->ofs;
  835. x->ofs += PLT_ENTRY_SIZE;
  836. if (hh->plt_offset < 0x2000)
  837. {
  838. struct elf64_hppa_link_hash_table *hppa_info;
  839. hppa_info = hppa_link_hash_table (x->info);
  840. if (hppa_info == NULL)
  841. return false;
  842. hppa_info->gp_offset = hh->plt_offset;
  843. }
  844. }
  845. else
  846. hh->want_plt = 0;
  847. return true;
  848. }
  849. /* Allocate space for a STUB entry. */
  850. static bool
  851. allocate_global_data_stub (struct elf_link_hash_entry *eh, void *data)
  852. {
  853. struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
  854. struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
  855. if (hh->want_stub
  856. && elf64_hppa_dynamic_symbol_p (eh, x->info)
  857. && !((eh->root.type == bfd_link_hash_defined
  858. || eh->root.type == bfd_link_hash_defweak)
  859. && eh->root.u.def.section->output_section != NULL))
  860. {
  861. hh->stub_offset = x->ofs;
  862. x->ofs += sizeof (plt_stub);
  863. }
  864. else
  865. hh->want_stub = 0;
  866. return true;
  867. }
  868. /* Allocate space for a FPTR entry. */
  869. static bool
  870. allocate_global_data_opd (struct elf_link_hash_entry *eh, void *data)
  871. {
  872. struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
  873. struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
  874. if (hh && hh->want_opd)
  875. {
  876. /* We never need an opd entry for a symbol which is not
  877. defined by this output file. */
  878. if (hh && (hh->eh.root.type == bfd_link_hash_undefined
  879. || hh->eh.root.type == bfd_link_hash_undefweak
  880. || hh->eh.root.u.def.section->output_section == NULL))
  881. hh->want_opd = 0;
  882. /* If we are creating a shared library, took the address of a local
  883. function or might export this function from this object file, then
  884. we have to create an opd descriptor. */
  885. else if (bfd_link_pic (x->info)
  886. || hh == NULL
  887. || (hh->eh.dynindx == -1 && hh->eh.type != STT_PARISC_MILLI)
  888. || (hh->eh.root.type == bfd_link_hash_defined
  889. || hh->eh.root.type == bfd_link_hash_defweak))
  890. {
  891. /* If we are creating a shared library, then we will have to
  892. create a runtime relocation for the symbol to properly
  893. initialize the .opd entry. Make sure the symbol gets
  894. added to the dynamic symbol table. */
  895. if (bfd_link_pic (x->info)
  896. && (hh == NULL || (hh->eh.dynindx == -1)))
  897. {
  898. bfd *owner;
  899. /* PR 6511: Default to using the dynamic symbol table. */
  900. owner = (hh->owner ? hh->owner: eh->root.u.def.section->owner);
  901. if (!bfd_elf_link_record_local_dynamic_symbol
  902. (x->info, owner, hh->sym_indx))
  903. return false;
  904. }
  905. /* This may not be necessary or desirable anymore now that
  906. we have some support for dealing with section symbols
  907. in dynamic relocs. But name munging does make the result
  908. much easier to debug. ie, the EPLT reloc will reference
  909. a symbol like .foobar, instead of .text + offset. */
  910. if (bfd_link_pic (x->info) && eh)
  911. {
  912. char *new_name;
  913. struct elf_link_hash_entry *nh;
  914. new_name = concat (".", eh->root.root.string, NULL);
  915. nh = elf_link_hash_lookup (elf_hash_table (x->info),
  916. new_name, true, true, true);
  917. free (new_name);
  918. nh->root.type = eh->root.type;
  919. nh->root.u.def.value = eh->root.u.def.value;
  920. nh->root.u.def.section = eh->root.u.def.section;
  921. if (! bfd_elf_link_record_dynamic_symbol (x->info, nh))
  922. return false;
  923. }
  924. hh->opd_offset = x->ofs;
  925. x->ofs += OPD_ENTRY_SIZE;
  926. }
  927. /* Otherwise we do not need an opd entry. */
  928. else
  929. hh->want_opd = 0;
  930. }
  931. return true;
  932. }
  933. /* HP requires the EI_OSABI field to be filled in. The assignment to
  934. EI_ABIVERSION may not be strictly necessary. */
  935. static bool
  936. elf64_hppa_init_file_header (bfd *abfd, struct bfd_link_info *info)
  937. {
  938. Elf_Internal_Ehdr *i_ehdrp;
  939. if (!_bfd_elf_init_file_header (abfd, info))
  940. return false;
  941. i_ehdrp = elf_elfheader (abfd);
  942. i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
  943. i_ehdrp->e_ident[EI_ABIVERSION] = 1;
  944. return true;
  945. }
  946. /* Create function descriptor section (.opd). This section is called .opd
  947. because it contains "official procedure descriptors". The "official"
  948. refers to the fact that these descriptors are used when taking the address
  949. of a procedure, thus ensuring a unique address for each procedure. */
  950. static bool
  951. get_opd (bfd *abfd,
  952. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  953. struct elf64_hppa_link_hash_table *hppa_info)
  954. {
  955. asection *opd;
  956. bfd *dynobj;
  957. opd = hppa_info->opd_sec;
  958. if (!opd)
  959. {
  960. dynobj = hppa_info->root.dynobj;
  961. if (!dynobj)
  962. hppa_info->root.dynobj = dynobj = abfd;
  963. opd = bfd_make_section_anyway_with_flags (dynobj, ".opd",
  964. (SEC_ALLOC
  965. | SEC_LOAD
  966. | SEC_HAS_CONTENTS
  967. | SEC_IN_MEMORY
  968. | SEC_LINKER_CREATED));
  969. if (!opd
  970. || !bfd_set_section_alignment (opd, 3))
  971. {
  972. BFD_ASSERT (0);
  973. return false;
  974. }
  975. hppa_info->opd_sec = opd;
  976. }
  977. return true;
  978. }
  979. /* Create the PLT section. */
  980. static bool
  981. get_plt (bfd *abfd,
  982. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  983. struct elf64_hppa_link_hash_table *hppa_info)
  984. {
  985. asection *plt;
  986. bfd *dynobj;
  987. plt = hppa_info->root.splt;
  988. if (!plt)
  989. {
  990. dynobj = hppa_info->root.dynobj;
  991. if (!dynobj)
  992. hppa_info->root.dynobj = dynobj = abfd;
  993. plt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
  994. (SEC_ALLOC
  995. | SEC_LOAD
  996. | SEC_HAS_CONTENTS
  997. | SEC_IN_MEMORY
  998. | SEC_LINKER_CREATED));
  999. if (!plt
  1000. || !bfd_set_section_alignment (plt, 3))
  1001. {
  1002. BFD_ASSERT (0);
  1003. return false;
  1004. }
  1005. hppa_info->root.splt = plt;
  1006. }
  1007. return true;
  1008. }
  1009. /* Create the DLT section. */
  1010. static bool
  1011. get_dlt (bfd *abfd,
  1012. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1013. struct elf64_hppa_link_hash_table *hppa_info)
  1014. {
  1015. asection *dlt;
  1016. bfd *dynobj;
  1017. dlt = hppa_info->dlt_sec;
  1018. if (!dlt)
  1019. {
  1020. dynobj = hppa_info->root.dynobj;
  1021. if (!dynobj)
  1022. hppa_info->root.dynobj = dynobj = abfd;
  1023. dlt = bfd_make_section_anyway_with_flags (dynobj, ".dlt",
  1024. (SEC_ALLOC
  1025. | SEC_LOAD
  1026. | SEC_HAS_CONTENTS
  1027. | SEC_IN_MEMORY
  1028. | SEC_LINKER_CREATED));
  1029. if (!dlt
  1030. || !bfd_set_section_alignment (dlt, 3))
  1031. {
  1032. BFD_ASSERT (0);
  1033. return false;
  1034. }
  1035. hppa_info->dlt_sec = dlt;
  1036. }
  1037. return true;
  1038. }
  1039. /* Create the stubs section. */
  1040. static bool
  1041. get_stub (bfd *abfd,
  1042. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1043. struct elf64_hppa_link_hash_table *hppa_info)
  1044. {
  1045. asection *stub;
  1046. bfd *dynobj;
  1047. stub = hppa_info->stub_sec;
  1048. if (!stub)
  1049. {
  1050. dynobj = hppa_info->root.dynobj;
  1051. if (!dynobj)
  1052. hppa_info->root.dynobj = dynobj = abfd;
  1053. stub = bfd_make_section_anyway_with_flags (dynobj, ".stub",
  1054. (SEC_ALLOC | SEC_LOAD
  1055. | SEC_HAS_CONTENTS
  1056. | SEC_IN_MEMORY
  1057. | SEC_READONLY
  1058. | SEC_LINKER_CREATED));
  1059. if (!stub
  1060. || !bfd_set_section_alignment (stub, 3))
  1061. {
  1062. BFD_ASSERT (0);
  1063. return false;
  1064. }
  1065. hppa_info->stub_sec = stub;
  1066. }
  1067. return true;
  1068. }
  1069. /* Create sections necessary for dynamic linking. This is only a rough
  1070. cut and will likely change as we learn more about the somewhat
  1071. unusual dynamic linking scheme HP uses.
  1072. .stub:
  1073. Contains code to implement cross-space calls. The first time one
  1074. of the stubs is used it will call into the dynamic linker, later
  1075. calls will go straight to the target.
  1076. The only stub we support right now looks like
  1077. ldd OFFSET(%dp),%r1
  1078. bve %r0(%r1)
  1079. ldd OFFSET+8(%dp),%dp
  1080. Other stubs may be needed in the future. We may want the remove
  1081. the break/nop instruction. It is only used right now to keep the
  1082. offset of a .plt entry and a .stub entry in sync.
  1083. .dlt:
  1084. This is what most people call the .got. HP used a different name.
  1085. Losers.
  1086. .rela.dlt:
  1087. Relocations for the DLT.
  1088. .plt:
  1089. Function pointers as address,gp pairs.
  1090. .rela.plt:
  1091. Should contain dynamic IPLT (and EPLT?) relocations.
  1092. .opd:
  1093. FPTRS
  1094. .rela.opd:
  1095. EPLT relocations for symbols exported from shared libraries. */
  1096. static bool
  1097. elf64_hppa_create_dynamic_sections (bfd *abfd,
  1098. struct bfd_link_info *info)
  1099. {
  1100. asection *s;
  1101. struct elf64_hppa_link_hash_table *hppa_info;
  1102. hppa_info = hppa_link_hash_table (info);
  1103. if (hppa_info == NULL)
  1104. return false;
  1105. if (! get_stub (abfd, info, hppa_info))
  1106. return false;
  1107. if (! get_dlt (abfd, info, hppa_info))
  1108. return false;
  1109. if (! get_plt (abfd, info, hppa_info))
  1110. return false;
  1111. if (! get_opd (abfd, info, hppa_info))
  1112. return false;
  1113. s = bfd_make_section_anyway_with_flags (abfd, ".rela.dlt",
  1114. (SEC_ALLOC | SEC_LOAD
  1115. | SEC_HAS_CONTENTS
  1116. | SEC_IN_MEMORY
  1117. | SEC_READONLY
  1118. | SEC_LINKER_CREATED));
  1119. if (s == NULL
  1120. || !bfd_set_section_alignment (s, 3))
  1121. return false;
  1122. hppa_info->dlt_rel_sec = s;
  1123. s = bfd_make_section_anyway_with_flags (abfd, ".rela.plt",
  1124. (SEC_ALLOC | SEC_LOAD
  1125. | SEC_HAS_CONTENTS
  1126. | SEC_IN_MEMORY
  1127. | SEC_READONLY
  1128. | SEC_LINKER_CREATED));
  1129. if (s == NULL
  1130. || !bfd_set_section_alignment (s, 3))
  1131. return false;
  1132. hppa_info->root.srelplt = s;
  1133. s = bfd_make_section_anyway_with_flags (abfd, ".rela.data",
  1134. (SEC_ALLOC | SEC_LOAD
  1135. | SEC_HAS_CONTENTS
  1136. | SEC_IN_MEMORY
  1137. | SEC_READONLY
  1138. | SEC_LINKER_CREATED));
  1139. if (s == NULL
  1140. || !bfd_set_section_alignment (s, 3))
  1141. return false;
  1142. hppa_info->other_rel_sec = s;
  1143. s = bfd_make_section_anyway_with_flags (abfd, ".rela.opd",
  1144. (SEC_ALLOC | SEC_LOAD
  1145. | SEC_HAS_CONTENTS
  1146. | SEC_IN_MEMORY
  1147. | SEC_READONLY
  1148. | SEC_LINKER_CREATED));
  1149. if (s == NULL
  1150. || !bfd_set_section_alignment (s, 3))
  1151. return false;
  1152. hppa_info->opd_rel_sec = s;
  1153. return true;
  1154. }
  1155. /* Allocate dynamic relocations for those symbols that turned out
  1156. to be dynamic. */
  1157. static bool
  1158. allocate_dynrel_entries (struct elf_link_hash_entry *eh, void *data)
  1159. {
  1160. struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
  1161. struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
  1162. struct elf64_hppa_link_hash_table *hppa_info;
  1163. struct elf64_hppa_dyn_reloc_entry *rent;
  1164. bool dynamic_symbol, shared;
  1165. hppa_info = hppa_link_hash_table (x->info);
  1166. if (hppa_info == NULL)
  1167. return false;
  1168. dynamic_symbol = elf64_hppa_dynamic_symbol_p (eh, x->info);
  1169. shared = bfd_link_pic (x->info);
  1170. /* We may need to allocate relocations for a non-dynamic symbol
  1171. when creating a shared library. */
  1172. if (!dynamic_symbol && !shared)
  1173. return true;
  1174. /* Take care of the normal data relocations. */
  1175. for (rent = hh->reloc_entries; rent; rent = rent->next)
  1176. {
  1177. /* Allocate one iff we are building a shared library, the relocation
  1178. isn't a R_PARISC_FPTR64, or we don't want an opd entry. */
  1179. if (!shared && rent->type == R_PARISC_FPTR64 && hh->want_opd)
  1180. continue;
  1181. hppa_info->other_rel_sec->size += sizeof (Elf64_External_Rela);
  1182. /* Make sure this symbol gets into the dynamic symbol table if it is
  1183. not already recorded. ?!? This should not be in the loop since
  1184. the symbol need only be added once. */
  1185. if (eh->dynindx == -1 && eh->type != STT_PARISC_MILLI)
  1186. if (!bfd_elf_link_record_local_dynamic_symbol
  1187. (x->info, rent->sec->owner, hh->sym_indx))
  1188. return false;
  1189. }
  1190. /* Take care of the GOT and PLT relocations. */
  1191. if ((dynamic_symbol || shared) && hh->want_dlt)
  1192. hppa_info->dlt_rel_sec->size += sizeof (Elf64_External_Rela);
  1193. /* If we are building a shared library, then every symbol that has an
  1194. opd entry will need an EPLT relocation to relocate the symbol's address
  1195. and __gp value based on the runtime load address. */
  1196. if (shared && hh->want_opd)
  1197. hppa_info->opd_rel_sec->size += sizeof (Elf64_External_Rela);
  1198. if (hh->want_plt && dynamic_symbol)
  1199. {
  1200. bfd_size_type t = 0;
  1201. /* Dynamic symbols get one IPLT relocation. Local symbols in
  1202. shared libraries get two REL relocations. Local symbols in
  1203. main applications get nothing. */
  1204. if (dynamic_symbol)
  1205. t = sizeof (Elf64_External_Rela);
  1206. else if (shared)
  1207. t = 2 * sizeof (Elf64_External_Rela);
  1208. hppa_info->root.srelplt->size += t;
  1209. }
  1210. return true;
  1211. }
  1212. /* Adjust a symbol defined by a dynamic object and referenced by a
  1213. regular object. */
  1214. static bool
  1215. elf64_hppa_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1216. struct elf_link_hash_entry *eh)
  1217. {
  1218. /* ??? Undefined symbols with PLT entries should be re-defined
  1219. to be the PLT entry. */
  1220. /* If this is a weak symbol, and there is a real definition, the
  1221. processor independent code will have arranged for us to see the
  1222. real definition first, and we can just use the same value. */
  1223. if (eh->is_weakalias)
  1224. {
  1225. struct elf_link_hash_entry *def = weakdef (eh);
  1226. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  1227. eh->root.u.def.section = def->root.u.def.section;
  1228. eh->root.u.def.value = def->root.u.def.value;
  1229. return true;
  1230. }
  1231. /* If this is a reference to a symbol defined by a dynamic object which
  1232. is not a function, we might allocate the symbol in our .dynbss section
  1233. and allocate a COPY dynamic relocation.
  1234. But PA64 code is canonically PIC, so as a rule we can avoid this sort
  1235. of hackery. */
  1236. return true;
  1237. }
  1238. /* This function is called via elf_link_hash_traverse to mark millicode
  1239. symbols with a dynindx of -1 and to remove the string table reference
  1240. from the dynamic symbol table. If the symbol is not a millicode symbol,
  1241. elf64_hppa_mark_exported_functions is called. */
  1242. static bool
  1243. elf64_hppa_mark_milli_and_exported_functions (struct elf_link_hash_entry *eh,
  1244. void *data)
  1245. {
  1246. struct bfd_link_info *info = (struct bfd_link_info *) data;
  1247. if (eh->type == STT_PARISC_MILLI)
  1248. {
  1249. if (eh->dynindx != -1)
  1250. {
  1251. eh->dynindx = -1;
  1252. _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
  1253. eh->dynstr_index);
  1254. }
  1255. return true;
  1256. }
  1257. return elf64_hppa_mark_exported_functions (eh, data);
  1258. }
  1259. /* Set the final sizes of the dynamic sections and allocate memory for
  1260. the contents of our special sections. */
  1261. static bool
  1262. elf64_hppa_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
  1263. {
  1264. struct elf64_hppa_link_hash_table *hppa_info;
  1265. struct elf64_hppa_allocate_data data;
  1266. bfd *dynobj;
  1267. bfd *ibfd;
  1268. asection *sec;
  1269. bool relocs;
  1270. hppa_info = hppa_link_hash_table (info);
  1271. if (hppa_info == NULL)
  1272. return false;
  1273. dynobj = hppa_info->root.dynobj;
  1274. BFD_ASSERT (dynobj != NULL);
  1275. /* Mark each function this program exports so that we will allocate
  1276. space in the .opd section for each function's FPTR. If we are
  1277. creating dynamic sections, change the dynamic index of millicode
  1278. symbols to -1 and remove them from the string table for .dynstr.
  1279. We have to traverse the main linker hash table since we have to
  1280. find functions which may not have been mentioned in any relocs. */
  1281. elf_link_hash_traverse (&hppa_info->root,
  1282. (hppa_info->root.dynamic_sections_created
  1283. ? elf64_hppa_mark_milli_and_exported_functions
  1284. : elf64_hppa_mark_exported_functions),
  1285. info);
  1286. if (hppa_info->root.dynamic_sections_created)
  1287. {
  1288. /* Set the contents of the .interp section to the interpreter. */
  1289. if (bfd_link_executable (info) && !info->nointerp)
  1290. {
  1291. sec = bfd_get_linker_section (dynobj, ".interp");
  1292. BFD_ASSERT (sec != NULL);
  1293. sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
  1294. sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  1295. }
  1296. }
  1297. else
  1298. {
  1299. /* We may have created entries in the .rela.got section.
  1300. However, if we are not creating the dynamic sections, we will
  1301. not actually use these entries. Reset the size of .rela.dlt,
  1302. which will cause it to get stripped from the output file
  1303. below. */
  1304. sec = hppa_info->dlt_rel_sec;
  1305. if (sec != NULL)
  1306. sec->size = 0;
  1307. }
  1308. /* Set up DLT, PLT and OPD offsets for local syms, and space for local
  1309. dynamic relocs. */
  1310. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  1311. {
  1312. bfd_signed_vma *local_dlt;
  1313. bfd_signed_vma *end_local_dlt;
  1314. bfd_signed_vma *local_plt;
  1315. bfd_signed_vma *end_local_plt;
  1316. bfd_signed_vma *local_opd;
  1317. bfd_signed_vma *end_local_opd;
  1318. bfd_size_type locsymcount;
  1319. Elf_Internal_Shdr *symtab_hdr;
  1320. asection *srel;
  1321. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
  1322. continue;
  1323. for (sec = ibfd->sections; sec != NULL; sec = sec->next)
  1324. {
  1325. struct elf64_hppa_dyn_reloc_entry *hdh_p;
  1326. for (hdh_p = ((struct elf64_hppa_dyn_reloc_entry *)
  1327. elf_section_data (sec)->local_dynrel);
  1328. hdh_p != NULL;
  1329. hdh_p = hdh_p->next)
  1330. {
  1331. if (!bfd_is_abs_section (hdh_p->sec)
  1332. && bfd_is_abs_section (hdh_p->sec->output_section))
  1333. {
  1334. /* Input section has been discarded, either because
  1335. it is a copy of a linkonce section or due to
  1336. linker script /DISCARD/, so we'll be discarding
  1337. the relocs too. */
  1338. }
  1339. else if (hdh_p->count != 0)
  1340. {
  1341. srel = elf_section_data (hdh_p->sec)->sreloc;
  1342. srel->size += hdh_p->count * sizeof (Elf64_External_Rela);
  1343. if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
  1344. info->flags |= DF_TEXTREL;
  1345. }
  1346. }
  1347. }
  1348. local_dlt = elf_local_got_refcounts (ibfd);
  1349. if (!local_dlt)
  1350. continue;
  1351. symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
  1352. locsymcount = symtab_hdr->sh_info;
  1353. end_local_dlt = local_dlt + locsymcount;
  1354. sec = hppa_info->dlt_sec;
  1355. srel = hppa_info->dlt_rel_sec;
  1356. for (; local_dlt < end_local_dlt; ++local_dlt)
  1357. {
  1358. if (*local_dlt > 0)
  1359. {
  1360. *local_dlt = sec->size;
  1361. sec->size += DLT_ENTRY_SIZE;
  1362. if (bfd_link_pic (info))
  1363. {
  1364. srel->size += sizeof (Elf64_External_Rela);
  1365. }
  1366. }
  1367. else
  1368. *local_dlt = (bfd_vma) -1;
  1369. }
  1370. local_plt = end_local_dlt;
  1371. end_local_plt = local_plt + locsymcount;
  1372. if (! hppa_info->root.dynamic_sections_created)
  1373. {
  1374. /* Won't be used, but be safe. */
  1375. for (; local_plt < end_local_plt; ++local_plt)
  1376. *local_plt = (bfd_vma) -1;
  1377. }
  1378. else
  1379. {
  1380. sec = hppa_info->root.splt;
  1381. srel = hppa_info->root.srelplt;
  1382. for (; local_plt < end_local_plt; ++local_plt)
  1383. {
  1384. if (*local_plt > 0)
  1385. {
  1386. *local_plt = sec->size;
  1387. sec->size += PLT_ENTRY_SIZE;
  1388. if (bfd_link_pic (info))
  1389. srel->size += sizeof (Elf64_External_Rela);
  1390. }
  1391. else
  1392. *local_plt = (bfd_vma) -1;
  1393. }
  1394. }
  1395. local_opd = end_local_plt;
  1396. end_local_opd = local_opd + locsymcount;
  1397. if (! hppa_info->root.dynamic_sections_created)
  1398. {
  1399. /* Won't be used, but be safe. */
  1400. for (; local_opd < end_local_opd; ++local_opd)
  1401. *local_opd = (bfd_vma) -1;
  1402. }
  1403. else
  1404. {
  1405. sec = hppa_info->opd_sec;
  1406. srel = hppa_info->opd_rel_sec;
  1407. for (; local_opd < end_local_opd; ++local_opd)
  1408. {
  1409. if (*local_opd > 0)
  1410. {
  1411. *local_opd = sec->size;
  1412. sec->size += OPD_ENTRY_SIZE;
  1413. if (bfd_link_pic (info))
  1414. srel->size += sizeof (Elf64_External_Rela);
  1415. }
  1416. else
  1417. *local_opd = (bfd_vma) -1;
  1418. }
  1419. }
  1420. }
  1421. /* Allocate the GOT entries. */
  1422. data.info = info;
  1423. if (hppa_info->dlt_sec)
  1424. {
  1425. data.ofs = hppa_info->dlt_sec->size;
  1426. elf_link_hash_traverse (&hppa_info->root,
  1427. allocate_global_data_dlt, &data);
  1428. hppa_info->dlt_sec->size = data.ofs;
  1429. }
  1430. if (hppa_info->root.splt)
  1431. {
  1432. data.ofs = hppa_info->root.splt->size;
  1433. elf_link_hash_traverse (&hppa_info->root,
  1434. allocate_global_data_plt, &data);
  1435. hppa_info->root.splt->size = data.ofs;
  1436. }
  1437. if (hppa_info->stub_sec)
  1438. {
  1439. data.ofs = 0x0;
  1440. elf_link_hash_traverse (&hppa_info->root,
  1441. allocate_global_data_stub, &data);
  1442. hppa_info->stub_sec->size = data.ofs;
  1443. }
  1444. /* Allocate space for entries in the .opd section. */
  1445. if (hppa_info->opd_sec)
  1446. {
  1447. data.ofs = hppa_info->opd_sec->size;
  1448. elf_link_hash_traverse (&hppa_info->root,
  1449. allocate_global_data_opd, &data);
  1450. hppa_info->opd_sec->size = data.ofs;
  1451. }
  1452. /* Now allocate space for dynamic relocations, if necessary. */
  1453. if (hppa_info->root.dynamic_sections_created)
  1454. elf_link_hash_traverse (&hppa_info->root,
  1455. allocate_dynrel_entries, &data);
  1456. /* The sizes of all the sections are set. Allocate memory for them. */
  1457. relocs = false;
  1458. for (sec = dynobj->sections; sec != NULL; sec = sec->next)
  1459. {
  1460. const char *name;
  1461. if ((sec->flags & SEC_LINKER_CREATED) == 0)
  1462. continue;
  1463. /* It's OK to base decisions on the section name, because none
  1464. of the dynobj section names depend upon the input files. */
  1465. name = bfd_section_name (sec);
  1466. if (strcmp (name, ".plt") == 0)
  1467. {
  1468. /* Remember whether there is a PLT. */
  1469. ;
  1470. }
  1471. else if (strcmp (name, ".opd") == 0
  1472. || startswith (name, ".dlt")
  1473. || strcmp (name, ".stub") == 0
  1474. || strcmp (name, ".got") == 0)
  1475. {
  1476. /* Strip this section if we don't need it; see the comment below. */
  1477. }
  1478. else if (startswith (name, ".rela"))
  1479. {
  1480. if (sec->size != 0)
  1481. {
  1482. /* Remember whether there are any reloc sections other
  1483. than .rela.plt. */
  1484. if (strcmp (name, ".rela.plt") != 0)
  1485. relocs = true;
  1486. /* We use the reloc_count field as a counter if we need
  1487. to copy relocs into the output file. */
  1488. sec->reloc_count = 0;
  1489. }
  1490. }
  1491. else
  1492. {
  1493. /* It's not one of our sections, so don't allocate space. */
  1494. continue;
  1495. }
  1496. if (sec->size == 0)
  1497. {
  1498. /* If we don't need this section, strip it from the
  1499. output file. This is mostly to handle .rela.bss and
  1500. .rela.plt. We must create both sections in
  1501. create_dynamic_sections, because they must be created
  1502. before the linker maps input sections to output
  1503. sections. The linker does that before
  1504. adjust_dynamic_symbol is called, and it is that
  1505. function which decides whether anything needs to go
  1506. into these sections. */
  1507. sec->flags |= SEC_EXCLUDE;
  1508. continue;
  1509. }
  1510. if ((sec->flags & SEC_HAS_CONTENTS) == 0)
  1511. continue;
  1512. /* Allocate memory for the section contents if it has not
  1513. been allocated already. We use bfd_zalloc here in case
  1514. unused entries are not reclaimed before the section's
  1515. contents are written out. This should not happen, but this
  1516. way if it does, we get a R_PARISC_NONE reloc instead of
  1517. garbage. */
  1518. if (sec->contents == NULL)
  1519. {
  1520. sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
  1521. if (sec->contents == NULL)
  1522. return false;
  1523. }
  1524. }
  1525. if (hppa_info->root.dynamic_sections_created)
  1526. {
  1527. /* Always create a DT_PLTGOT. It actually has nothing to do with
  1528. the PLT, it is how we communicate the __gp value of a load
  1529. module to the dynamic linker. */
  1530. #define add_dynamic_entry(TAG, VAL) \
  1531. _bfd_elf_add_dynamic_entry (info, TAG, VAL)
  1532. if (!add_dynamic_entry (DT_HP_DLD_FLAGS, 0))
  1533. return false;
  1534. /* Add some entries to the .dynamic section. We fill in the
  1535. values later, in elf64_hppa_finish_dynamic_sections, but we
  1536. must add the entries now so that we get the correct size for
  1537. the .dynamic section. The DT_DEBUG entry is filled in by the
  1538. dynamic linker and used by the debugger. */
  1539. if (! bfd_link_pic (info))
  1540. {
  1541. if (!add_dynamic_entry (DT_HP_DLD_HOOK, 0)
  1542. || !add_dynamic_entry (DT_HP_LOAD_MAP, 0))
  1543. return false;
  1544. }
  1545. /* Force DT_FLAGS to always be set.
  1546. Required by HPUX 11.00 patch PHSS_26559. */
  1547. if (!add_dynamic_entry (DT_FLAGS, (info)->flags))
  1548. return false;
  1549. }
  1550. #undef add_dynamic_entry
  1551. return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
  1552. }
  1553. /* Called after we have output the symbol into the dynamic symbol
  1554. table, but before we output the symbol into the normal symbol
  1555. table.
  1556. For some symbols we had to change their address when outputting
  1557. the dynamic symbol table. We undo that change here so that
  1558. the symbols have their expected value in the normal symbol
  1559. table. Ick. */
  1560. static int
  1561. elf64_hppa_link_output_symbol_hook (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1562. const char *name,
  1563. Elf_Internal_Sym *sym,
  1564. asection *input_sec ATTRIBUTE_UNUSED,
  1565. struct elf_link_hash_entry *eh)
  1566. {
  1567. struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
  1568. /* We may be called with the file symbol or section symbols.
  1569. They never need munging, so it is safe to ignore them. */
  1570. if (!name || !eh)
  1571. return 1;
  1572. /* Function symbols for which we created .opd entries *may* have been
  1573. munged by finish_dynamic_symbol and have to be un-munged here.
  1574. Note that finish_dynamic_symbol sometimes turns dynamic symbols
  1575. into non-dynamic ones, so we initialize st_shndx to -1 in
  1576. mark_exported_functions and check to see if it was overwritten
  1577. here instead of just checking eh->dynindx. */
  1578. if (hh->want_opd && hh->st_shndx != -1)
  1579. {
  1580. /* Restore the saved value and section index. */
  1581. sym->st_value = hh->st_value;
  1582. sym->st_shndx = hh->st_shndx;
  1583. }
  1584. return 1;
  1585. }
  1586. /* Finish up dynamic symbol handling. We set the contents of various
  1587. dynamic sections here. */
  1588. static bool
  1589. elf64_hppa_finish_dynamic_symbol (bfd *output_bfd,
  1590. struct bfd_link_info *info,
  1591. struct elf_link_hash_entry *eh,
  1592. Elf_Internal_Sym *sym)
  1593. {
  1594. struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
  1595. asection *stub, *splt, *sopd, *spltrel;
  1596. struct elf64_hppa_link_hash_table *hppa_info;
  1597. hppa_info = hppa_link_hash_table (info);
  1598. if (hppa_info == NULL)
  1599. return false;
  1600. stub = hppa_info->stub_sec;
  1601. splt = hppa_info->root.splt;
  1602. sopd = hppa_info->opd_sec;
  1603. spltrel = hppa_info->root.srelplt;
  1604. /* Incredible. It is actually necessary to NOT use the symbol's real
  1605. value when building the dynamic symbol table for a shared library.
  1606. At least for symbols that refer to functions.
  1607. We will store a new value and section index into the symbol long
  1608. enough to output it into the dynamic symbol table, then we restore
  1609. the original values (in elf64_hppa_link_output_symbol_hook). */
  1610. if (hh->want_opd)
  1611. {
  1612. BFD_ASSERT (sopd != NULL);
  1613. /* Save away the original value and section index so that we
  1614. can restore them later. */
  1615. hh->st_value = sym->st_value;
  1616. hh->st_shndx = sym->st_shndx;
  1617. /* For the dynamic symbol table entry, we want the value to be
  1618. address of this symbol's entry within the .opd section. */
  1619. sym->st_value = (hh->opd_offset
  1620. + sopd->output_offset
  1621. + sopd->output_section->vma);
  1622. sym->st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
  1623. sopd->output_section);
  1624. }
  1625. /* Initialize a .plt entry if requested. */
  1626. if (hh->want_plt
  1627. && elf64_hppa_dynamic_symbol_p (eh, info))
  1628. {
  1629. bfd_vma value;
  1630. Elf_Internal_Rela rel;
  1631. bfd_byte *loc;
  1632. BFD_ASSERT (splt != NULL && spltrel != NULL);
  1633. /* We do not actually care about the value in the PLT entry
  1634. if we are creating a shared library and the symbol is
  1635. still undefined, we create a dynamic relocation to fill
  1636. in the correct value. */
  1637. if (bfd_link_pic (info) && eh->root.type == bfd_link_hash_undefined)
  1638. value = 0;
  1639. else
  1640. value = (eh->root.u.def.value + eh->root.u.def.section->vma);
  1641. /* Fill in the entry in the procedure linkage table.
  1642. The format of a plt entry is
  1643. <funcaddr> <__gp>.
  1644. plt_offset is the offset within the PLT section at which to
  1645. install the PLT entry.
  1646. We are modifying the in-memory PLT contents here, so we do not add
  1647. in the output_offset of the PLT section. */
  1648. bfd_put_64 (splt->owner, value, splt->contents + hh->plt_offset);
  1649. value = _bfd_get_gp_value (info->output_bfd);
  1650. bfd_put_64 (splt->owner, value, splt->contents + hh->plt_offset + 0x8);
  1651. /* Create a dynamic IPLT relocation for this entry.
  1652. We are creating a relocation in the output file's PLT section,
  1653. which is included within the DLT secton. So we do need to include
  1654. the PLT's output_offset in the computation of the relocation's
  1655. address. */
  1656. rel.r_offset = (hh->plt_offset + splt->output_offset
  1657. + splt->output_section->vma);
  1658. rel.r_info = ELF64_R_INFO (hh->eh.dynindx, R_PARISC_IPLT);
  1659. rel.r_addend = 0;
  1660. loc = spltrel->contents;
  1661. loc += spltrel->reloc_count++ * sizeof (Elf64_External_Rela);
  1662. bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
  1663. }
  1664. /* Initialize an external call stub entry if requested. */
  1665. if (hh->want_stub
  1666. && elf64_hppa_dynamic_symbol_p (eh, info))
  1667. {
  1668. bfd_vma value;
  1669. int insn;
  1670. unsigned int max_offset;
  1671. BFD_ASSERT (stub != NULL);
  1672. /* Install the generic stub template.
  1673. We are modifying the contents of the stub section, so we do not
  1674. need to include the stub section's output_offset here. */
  1675. memcpy (stub->contents + hh->stub_offset, plt_stub, sizeof (plt_stub));
  1676. /* Fix up the first ldd instruction.
  1677. We are modifying the contents of the STUB section in memory,
  1678. so we do not need to include its output offset in this computation.
  1679. Note the plt_offset value is the value of the PLT entry relative to
  1680. the start of the PLT section. These instructions will reference
  1681. data relative to the value of __gp, which may not necessarily have
  1682. the same address as the start of the PLT section.
  1683. gp_offset contains the offset of __gp within the PLT section. */
  1684. value = hh->plt_offset - hppa_info->gp_offset;
  1685. insn = bfd_get_32 (stub->owner, stub->contents + hh->stub_offset);
  1686. if (output_bfd->arch_info->mach >= 25)
  1687. {
  1688. /* Wide mode allows 16 bit offsets. */
  1689. max_offset = 32768;
  1690. insn &= ~ 0xfff1;
  1691. insn |= re_assemble_16 ((int) value);
  1692. }
  1693. else
  1694. {
  1695. max_offset = 8192;
  1696. insn &= ~ 0x3ff1;
  1697. insn |= re_assemble_14 ((int) value);
  1698. }
  1699. if ((value & 7) || value + max_offset >= 2*max_offset - 8)
  1700. {
  1701. _bfd_error_handler
  1702. /* xgettext:c-format */
  1703. (_("stub entry for %s cannot load .plt, dp offset = %" PRId64),
  1704. hh->eh.root.root.string, (int64_t) value);
  1705. return false;
  1706. }
  1707. bfd_put_32 (stub->owner, (bfd_vma) insn,
  1708. stub->contents + hh->stub_offset);
  1709. /* Fix up the second ldd instruction. */
  1710. value += 8;
  1711. insn = bfd_get_32 (stub->owner, stub->contents + hh->stub_offset + 8);
  1712. if (output_bfd->arch_info->mach >= 25)
  1713. {
  1714. insn &= ~ 0xfff1;
  1715. insn |= re_assemble_16 ((int) value);
  1716. }
  1717. else
  1718. {
  1719. insn &= ~ 0x3ff1;
  1720. insn |= re_assemble_14 ((int) value);
  1721. }
  1722. bfd_put_32 (stub->owner, (bfd_vma) insn,
  1723. stub->contents + hh->stub_offset + 8);
  1724. }
  1725. return true;
  1726. }
  1727. /* The .opd section contains FPTRs for each function this file
  1728. exports. Initialize the FPTR entries. */
  1729. static bool
  1730. elf64_hppa_finalize_opd (struct elf_link_hash_entry *eh, void *data)
  1731. {
  1732. struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
  1733. struct bfd_link_info *info = (struct bfd_link_info *)data;
  1734. struct elf64_hppa_link_hash_table *hppa_info;
  1735. asection *sopd;
  1736. asection *sopdrel;
  1737. hppa_info = hppa_link_hash_table (info);
  1738. if (hppa_info == NULL)
  1739. return false;
  1740. sopd = hppa_info->opd_sec;
  1741. sopdrel = hppa_info->opd_rel_sec;
  1742. if (hh->want_opd)
  1743. {
  1744. bfd_vma value;
  1745. /* The first two words of an .opd entry are zero.
  1746. We are modifying the contents of the OPD section in memory, so we
  1747. do not need to include its output offset in this computation. */
  1748. memset (sopd->contents + hh->opd_offset, 0, 16);
  1749. value = (eh->root.u.def.value
  1750. + eh->root.u.def.section->output_section->vma
  1751. + eh->root.u.def.section->output_offset);
  1752. /* The next word is the address of the function. */
  1753. bfd_put_64 (sopd->owner, value, sopd->contents + hh->opd_offset + 16);
  1754. /* The last word is our local __gp value. */
  1755. value = _bfd_get_gp_value (info->output_bfd);
  1756. bfd_put_64 (sopd->owner, value, sopd->contents + hh->opd_offset + 24);
  1757. }
  1758. /* If we are generating a shared library, we must generate EPLT relocations
  1759. for each entry in the .opd, even for static functions (they may have
  1760. had their address taken). */
  1761. if (bfd_link_pic (info) && hh->want_opd)
  1762. {
  1763. Elf_Internal_Rela rel;
  1764. bfd_byte *loc;
  1765. int dynindx;
  1766. /* We may need to do a relocation against a local symbol, in
  1767. which case we have to look up it's dynamic symbol index off
  1768. the local symbol hash table. */
  1769. if (eh->dynindx != -1)
  1770. dynindx = eh->dynindx;
  1771. else
  1772. dynindx
  1773. = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
  1774. hh->sym_indx);
  1775. /* The offset of this relocation is the absolute address of the
  1776. .opd entry for this symbol. */
  1777. rel.r_offset = (hh->opd_offset + sopd->output_offset
  1778. + sopd->output_section->vma);
  1779. /* If H is non-null, then we have an external symbol.
  1780. It is imperative that we use a different dynamic symbol for the
  1781. EPLT relocation if the symbol has global scope.
  1782. In the dynamic symbol table, the function symbol will have a value
  1783. which is address of the function's .opd entry.
  1784. Thus, we can not use that dynamic symbol for the EPLT relocation
  1785. (if we did, the data in the .opd would reference itself rather
  1786. than the actual address of the function). Instead we have to use
  1787. a new dynamic symbol which has the same value as the original global
  1788. function symbol.
  1789. We prefix the original symbol with a "." and use the new symbol in
  1790. the EPLT relocation. This new symbol has already been recorded in
  1791. the symbol table, we just have to look it up and use it.
  1792. We do not have such problems with static functions because we do
  1793. not make their addresses in the dynamic symbol table point to
  1794. the .opd entry. Ultimately this should be safe since a static
  1795. function can not be directly referenced outside of its shared
  1796. library.
  1797. We do have to play similar games for FPTR relocations in shared
  1798. libraries, including those for static symbols. See the FPTR
  1799. handling in elf64_hppa_finalize_dynreloc. */
  1800. if (eh)
  1801. {
  1802. char *new_name;
  1803. struct elf_link_hash_entry *nh;
  1804. new_name = concat (".", eh->root.root.string, NULL);
  1805. nh = elf_link_hash_lookup (elf_hash_table (info),
  1806. new_name, true, true, false);
  1807. /* All we really want from the new symbol is its dynamic
  1808. symbol index. */
  1809. if (nh)
  1810. dynindx = nh->dynindx;
  1811. free (new_name);
  1812. }
  1813. rel.r_addend = 0;
  1814. rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_EPLT);
  1815. loc = sopdrel->contents;
  1816. loc += sopdrel->reloc_count++ * sizeof (Elf64_External_Rela);
  1817. bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
  1818. }
  1819. return true;
  1820. }
  1821. /* The .dlt section contains addresses for items referenced through the
  1822. dlt. Note that we can have a DLTIND relocation for a local symbol, thus
  1823. we can not depend on finish_dynamic_symbol to initialize the .dlt. */
  1824. static bool
  1825. elf64_hppa_finalize_dlt (struct elf_link_hash_entry *eh, void *data)
  1826. {
  1827. struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
  1828. struct bfd_link_info *info = (struct bfd_link_info *)data;
  1829. struct elf64_hppa_link_hash_table *hppa_info;
  1830. asection *sdlt, *sdltrel;
  1831. hppa_info = hppa_link_hash_table (info);
  1832. if (hppa_info == NULL)
  1833. return false;
  1834. sdlt = hppa_info->dlt_sec;
  1835. sdltrel = hppa_info->dlt_rel_sec;
  1836. /* H/DYN_H may refer to a local variable and we know it's
  1837. address, so there is no need to create a relocation. Just install
  1838. the proper value into the DLT, note this shortcut can not be
  1839. skipped when building a shared library. */
  1840. if (! bfd_link_pic (info) && hh && hh->want_dlt)
  1841. {
  1842. bfd_vma value;
  1843. /* If we had an LTOFF_FPTR style relocation we want the DLT entry
  1844. to point to the FPTR entry in the .opd section.
  1845. We include the OPD's output offset in this computation as
  1846. we are referring to an absolute address in the resulting
  1847. object file. */
  1848. if (hh->want_opd)
  1849. {
  1850. value = (hh->opd_offset
  1851. + hppa_info->opd_sec->output_offset
  1852. + hppa_info->opd_sec->output_section->vma);
  1853. }
  1854. else if ((eh->root.type == bfd_link_hash_defined
  1855. || eh->root.type == bfd_link_hash_defweak)
  1856. && eh->root.u.def.section)
  1857. {
  1858. value = eh->root.u.def.value + eh->root.u.def.section->output_offset;
  1859. if (eh->root.u.def.section->output_section)
  1860. value += eh->root.u.def.section->output_section->vma;
  1861. else
  1862. value += eh->root.u.def.section->vma;
  1863. }
  1864. else
  1865. /* We have an undefined function reference. */
  1866. value = 0;
  1867. /* We do not need to include the output offset of the DLT section
  1868. here because we are modifying the in-memory contents. */
  1869. bfd_put_64 (sdlt->owner, value, sdlt->contents + hh->dlt_offset);
  1870. }
  1871. /* Create a relocation for the DLT entry associated with this symbol.
  1872. When building a shared library the symbol does not have to be dynamic. */
  1873. if (hh->want_dlt
  1874. && (elf64_hppa_dynamic_symbol_p (eh, info) || bfd_link_pic (info)))
  1875. {
  1876. Elf_Internal_Rela rel;
  1877. bfd_byte *loc;
  1878. int dynindx;
  1879. /* We may need to do a relocation against a local symbol, in
  1880. which case we have to look up it's dynamic symbol index off
  1881. the local symbol hash table. */
  1882. if (eh && eh->dynindx != -1)
  1883. dynindx = eh->dynindx;
  1884. else
  1885. dynindx
  1886. = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
  1887. hh->sym_indx);
  1888. /* Create a dynamic relocation for this entry. Do include the output
  1889. offset of the DLT entry since we need an absolute address in the
  1890. resulting object file. */
  1891. rel.r_offset = (hh->dlt_offset + sdlt->output_offset
  1892. + sdlt->output_section->vma);
  1893. if (eh && eh->type == STT_FUNC)
  1894. rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_FPTR64);
  1895. else
  1896. rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_DIR64);
  1897. rel.r_addend = 0;
  1898. loc = sdltrel->contents;
  1899. loc += sdltrel->reloc_count++ * sizeof (Elf64_External_Rela);
  1900. bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
  1901. }
  1902. return true;
  1903. }
  1904. /* Finalize the dynamic relocations. Specifically the FPTR relocations
  1905. for dynamic functions used to initialize static data. */
  1906. static bool
  1907. elf64_hppa_finalize_dynreloc (struct elf_link_hash_entry *eh,
  1908. void *data)
  1909. {
  1910. struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
  1911. struct bfd_link_info *info = (struct bfd_link_info *)data;
  1912. struct elf64_hppa_link_hash_table *hppa_info;
  1913. int dynamic_symbol;
  1914. dynamic_symbol = elf64_hppa_dynamic_symbol_p (eh, info);
  1915. if (!dynamic_symbol && !bfd_link_pic (info))
  1916. return true;
  1917. if (hh->reloc_entries)
  1918. {
  1919. struct elf64_hppa_dyn_reloc_entry *rent;
  1920. int dynindx;
  1921. hppa_info = hppa_link_hash_table (info);
  1922. if (hppa_info == NULL)
  1923. return false;
  1924. /* We may need to do a relocation against a local symbol, in
  1925. which case we have to look up it's dynamic symbol index off
  1926. the local symbol hash table. */
  1927. if (eh->dynindx != -1)
  1928. dynindx = eh->dynindx;
  1929. else
  1930. dynindx
  1931. = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
  1932. hh->sym_indx);
  1933. for (rent = hh->reloc_entries; rent; rent = rent->next)
  1934. {
  1935. Elf_Internal_Rela rel;
  1936. bfd_byte *loc;
  1937. /* Allocate one iff we are building a shared library, the relocation
  1938. isn't a R_PARISC_FPTR64, or we don't want an opd entry. */
  1939. if (!bfd_link_pic (info)
  1940. && rent->type == R_PARISC_FPTR64 && hh->want_opd)
  1941. continue;
  1942. /* Create a dynamic relocation for this entry.
  1943. We need the output offset for the reloc's section because
  1944. we are creating an absolute address in the resulting object
  1945. file. */
  1946. rel.r_offset = (rent->offset + rent->sec->output_offset
  1947. + rent->sec->output_section->vma);
  1948. /* An FPTR64 relocation implies that we took the address of
  1949. a function and that the function has an entry in the .opd
  1950. section. We want the FPTR64 relocation to reference the
  1951. entry in .opd.
  1952. We could munge the symbol value in the dynamic symbol table
  1953. (in fact we already do for functions with global scope) to point
  1954. to the .opd entry. Then we could use that dynamic symbol in
  1955. this relocation.
  1956. Or we could do something sensible, not munge the symbol's
  1957. address and instead just use a different symbol to reference
  1958. the .opd entry. At least that seems sensible until you
  1959. realize there's no local dynamic symbols we can use for that
  1960. purpose. Thus the hair in the check_relocs routine.
  1961. We use a section symbol recorded by check_relocs as the
  1962. base symbol for the relocation. The addend is the difference
  1963. between the section symbol and the address of the .opd entry. */
  1964. if (bfd_link_pic (info)
  1965. && rent->type == R_PARISC_FPTR64 && hh->want_opd)
  1966. {
  1967. bfd_vma value, value2;
  1968. /* First compute the address of the opd entry for this symbol. */
  1969. value = (hh->opd_offset
  1970. + hppa_info->opd_sec->output_section->vma
  1971. + hppa_info->opd_sec->output_offset);
  1972. /* Compute the value of the start of the section with
  1973. the relocation. */
  1974. value2 = (rent->sec->output_section->vma
  1975. + rent->sec->output_offset);
  1976. /* Compute the difference between the start of the section
  1977. with the relocation and the opd entry. */
  1978. value -= value2;
  1979. /* The result becomes the addend of the relocation. */
  1980. rel.r_addend = value;
  1981. /* The section symbol becomes the symbol for the dynamic
  1982. relocation. */
  1983. dynindx
  1984. = _bfd_elf_link_lookup_local_dynindx (info,
  1985. rent->sec->owner,
  1986. rent->sec_symndx);
  1987. }
  1988. else
  1989. rel.r_addend = rent->addend;
  1990. rel.r_info = ELF64_R_INFO (dynindx, rent->type);
  1991. loc = hppa_info->other_rel_sec->contents;
  1992. loc += (hppa_info->other_rel_sec->reloc_count++
  1993. * sizeof (Elf64_External_Rela));
  1994. bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
  1995. }
  1996. }
  1997. return true;
  1998. }
  1999. /* Used to decide how to sort relocs in an optimal manner for the
  2000. dynamic linker, before writing them out. */
  2001. static enum elf_reloc_type_class
  2002. elf64_hppa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2003. const asection *rel_sec ATTRIBUTE_UNUSED,
  2004. const Elf_Internal_Rela *rela)
  2005. {
  2006. if (ELF64_R_SYM (rela->r_info) == STN_UNDEF)
  2007. return reloc_class_relative;
  2008. switch ((int) ELF64_R_TYPE (rela->r_info))
  2009. {
  2010. case R_PARISC_IPLT:
  2011. return reloc_class_plt;
  2012. case R_PARISC_COPY:
  2013. return reloc_class_copy;
  2014. default:
  2015. return reloc_class_normal;
  2016. }
  2017. }
  2018. /* Finish up the dynamic sections. */
  2019. static bool
  2020. elf64_hppa_finish_dynamic_sections (bfd *output_bfd,
  2021. struct bfd_link_info *info)
  2022. {
  2023. bfd *dynobj;
  2024. asection *sdyn;
  2025. struct elf64_hppa_link_hash_table *hppa_info;
  2026. hppa_info = hppa_link_hash_table (info);
  2027. if (hppa_info == NULL)
  2028. return false;
  2029. /* Finalize the contents of the .opd section. */
  2030. elf_link_hash_traverse (elf_hash_table (info),
  2031. elf64_hppa_finalize_opd,
  2032. info);
  2033. elf_link_hash_traverse (elf_hash_table (info),
  2034. elf64_hppa_finalize_dynreloc,
  2035. info);
  2036. /* Finalize the contents of the .dlt section. */
  2037. dynobj = elf_hash_table (info)->dynobj;
  2038. /* Finalize the contents of the .dlt section. */
  2039. elf_link_hash_traverse (elf_hash_table (info),
  2040. elf64_hppa_finalize_dlt,
  2041. info);
  2042. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  2043. if (elf_hash_table (info)->dynamic_sections_created)
  2044. {
  2045. Elf64_External_Dyn *dyncon, *dynconend;
  2046. BFD_ASSERT (sdyn != NULL);
  2047. dyncon = (Elf64_External_Dyn *) sdyn->contents;
  2048. dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
  2049. for (; dyncon < dynconend; dyncon++)
  2050. {
  2051. Elf_Internal_Dyn dyn;
  2052. asection *s;
  2053. bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
  2054. switch (dyn.d_tag)
  2055. {
  2056. default:
  2057. break;
  2058. case DT_HP_LOAD_MAP:
  2059. /* Compute the absolute address of 16byte scratchpad area
  2060. for the dynamic linker.
  2061. By convention the linker script will allocate the scratchpad
  2062. area at the start of the .data section. So all we have to
  2063. to is find the start of the .data section. */
  2064. s = bfd_get_section_by_name (output_bfd, ".data");
  2065. if (!s)
  2066. return false;
  2067. dyn.d_un.d_ptr = s->vma;
  2068. bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
  2069. break;
  2070. case DT_PLTGOT:
  2071. /* HP's use PLTGOT to set the GOT register. */
  2072. dyn.d_un.d_ptr = _bfd_get_gp_value (output_bfd);
  2073. bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
  2074. break;
  2075. case DT_JMPREL:
  2076. s = hppa_info->root.srelplt;
  2077. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  2078. bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
  2079. break;
  2080. case DT_PLTRELSZ:
  2081. s = hppa_info->root.srelplt;
  2082. dyn.d_un.d_val = s->size;
  2083. bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
  2084. break;
  2085. case DT_RELA:
  2086. s = hppa_info->other_rel_sec;
  2087. if (! s || ! s->size)
  2088. s = hppa_info->dlt_rel_sec;
  2089. if (! s || ! s->size)
  2090. s = hppa_info->opd_rel_sec;
  2091. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  2092. bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
  2093. break;
  2094. case DT_RELASZ:
  2095. s = hppa_info->other_rel_sec;
  2096. dyn.d_un.d_val = s->size;
  2097. s = hppa_info->dlt_rel_sec;
  2098. dyn.d_un.d_val += s->size;
  2099. s = hppa_info->opd_rel_sec;
  2100. dyn.d_un.d_val += s->size;
  2101. /* There is some question about whether or not the size of
  2102. the PLT relocs should be included here. HP's tools do
  2103. it, so we'll emulate them. */
  2104. s = hppa_info->root.srelplt;
  2105. dyn.d_un.d_val += s->size;
  2106. bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
  2107. break;
  2108. }
  2109. }
  2110. }
  2111. return true;
  2112. }
  2113. /* Support for core dump NOTE sections. */
  2114. static bool
  2115. elf64_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  2116. {
  2117. int offset;
  2118. size_t size;
  2119. switch (note->descsz)
  2120. {
  2121. default:
  2122. return false;
  2123. case 760: /* Linux/hppa */
  2124. /* pr_cursig */
  2125. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  2126. /* pr_pid */
  2127. elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
  2128. /* pr_reg */
  2129. offset = 112;
  2130. size = 640;
  2131. break;
  2132. }
  2133. /* Make a ".reg/999" section. */
  2134. return _bfd_elfcore_make_pseudosection (abfd, ".reg",
  2135. size, note->descpos + offset);
  2136. }
  2137. static bool
  2138. elf64_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  2139. {
  2140. char * command;
  2141. int n;
  2142. switch (note->descsz)
  2143. {
  2144. default:
  2145. return false;
  2146. case 136: /* Linux/hppa elf_prpsinfo. */
  2147. elf_tdata (abfd)->core->program
  2148. = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
  2149. elf_tdata (abfd)->core->command
  2150. = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
  2151. }
  2152. /* Note that for some reason, a spurious space is tacked
  2153. onto the end of the args in some (at least one anyway)
  2154. implementations, so strip it off if it exists. */
  2155. command = elf_tdata (abfd)->core->command;
  2156. n = strlen (command);
  2157. if (0 < n && command[n - 1] == ' ')
  2158. command[n - 1] = '\0';
  2159. return true;
  2160. }
  2161. /* Return the number of additional phdrs we will need.
  2162. The generic ELF code only creates PT_PHDRs for executables. The HP
  2163. dynamic linker requires PT_PHDRs for dynamic libraries too.
  2164. This routine indicates that the backend needs one additional program
  2165. header for that case.
  2166. Note we do not have access to the link info structure here, so we have
  2167. to guess whether or not we are building a shared library based on the
  2168. existence of a .interp section. */
  2169. static int
  2170. elf64_hppa_additional_program_headers (bfd *abfd,
  2171. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  2172. {
  2173. asection *s;
  2174. /* If we are creating a shared library, then we have to create a
  2175. PT_PHDR segment. HP's dynamic linker chokes without it. */
  2176. s = bfd_get_section_by_name (abfd, ".interp");
  2177. if (! s)
  2178. return 1;
  2179. return 0;
  2180. }
  2181. static bool
  2182. elf64_hppa_allow_non_load_phdr (bfd *abfd ATTRIBUTE_UNUSED,
  2183. const Elf_Internal_Phdr *phdr ATTRIBUTE_UNUSED,
  2184. unsigned int count ATTRIBUTE_UNUSED)
  2185. {
  2186. return true;
  2187. }
  2188. /* Allocate and initialize any program headers required by this
  2189. specific backend.
  2190. The generic ELF code only creates PT_PHDRs for executables. The HP
  2191. dynamic linker requires PT_PHDRs for dynamic libraries too.
  2192. This allocates the PT_PHDR and initializes it in a manner suitable
  2193. for the HP linker.
  2194. Note we do not have access to the link info structure here, so we have
  2195. to guess whether or not we are building a shared library based on the
  2196. existence of a .interp section. */
  2197. static bool
  2198. elf64_hppa_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
  2199. {
  2200. struct elf_segment_map *m;
  2201. m = elf_seg_map (abfd);
  2202. if (info != NULL && !info->user_phdrs && m != NULL && m->p_type != PT_PHDR)
  2203. {
  2204. m = ((struct elf_segment_map *)
  2205. bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
  2206. if (m == NULL)
  2207. return false;
  2208. m->p_type = PT_PHDR;
  2209. m->p_flags = PF_R | PF_X;
  2210. m->p_flags_valid = 1;
  2211. m->p_paddr_valid = 1;
  2212. m->includes_phdrs = 1;
  2213. m->next = elf_seg_map (abfd);
  2214. elf_seg_map (abfd) = m;
  2215. }
  2216. for (m = elf_seg_map (abfd) ; m != NULL; m = m->next)
  2217. if (m->p_type == PT_LOAD)
  2218. {
  2219. unsigned int i;
  2220. for (i = 0; i < m->count; i++)
  2221. {
  2222. /* The code "hint" is not really a hint. It is a requirement
  2223. for certain versions of the HP dynamic linker. Worse yet,
  2224. it must be set even if the shared library does not have
  2225. any code in its "text" segment (thus the check for .hash
  2226. to catch this situation). */
  2227. if (m->sections[i]->flags & SEC_CODE
  2228. || (strcmp (m->sections[i]->name, ".hash") == 0))
  2229. m->p_flags |= (PF_X | PF_HP_CODE);
  2230. }
  2231. }
  2232. return true;
  2233. }
  2234. /* Called when writing out an object file to decide the type of a
  2235. symbol. */
  2236. static int
  2237. elf64_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym,
  2238. int type)
  2239. {
  2240. if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
  2241. return STT_PARISC_MILLI;
  2242. else
  2243. return type;
  2244. }
  2245. /* Support HP specific sections for core files. */
  2246. static bool
  2247. elf64_hppa_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int sec_index,
  2248. const char *typename)
  2249. {
  2250. if (hdr->p_type == PT_HP_CORE_KERNEL)
  2251. {
  2252. asection *sect;
  2253. if (!_bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename))
  2254. return false;
  2255. sect = bfd_make_section_anyway (abfd, ".kernel");
  2256. if (sect == NULL)
  2257. return false;
  2258. sect->size = hdr->p_filesz;
  2259. sect->filepos = hdr->p_offset;
  2260. sect->flags = SEC_HAS_CONTENTS | SEC_READONLY;
  2261. return true;
  2262. }
  2263. if (hdr->p_type == PT_HP_CORE_PROC)
  2264. {
  2265. int sig;
  2266. if (bfd_seek (abfd, hdr->p_offset, SEEK_SET) != 0)
  2267. return false;
  2268. if (bfd_bread (&sig, 4, abfd) != 4)
  2269. return false;
  2270. elf_tdata (abfd)->core->signal = sig;
  2271. if (!_bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename))
  2272. return false;
  2273. /* GDB uses the ".reg" section to read register contents. */
  2274. return _bfd_elfcore_make_pseudosection (abfd, ".reg", hdr->p_filesz,
  2275. hdr->p_offset);
  2276. }
  2277. if (hdr->p_type == PT_HP_CORE_LOADABLE
  2278. || hdr->p_type == PT_HP_CORE_STACK
  2279. || hdr->p_type == PT_HP_CORE_MMF)
  2280. hdr->p_type = PT_LOAD;
  2281. return _bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename);
  2282. }
  2283. /* Hook called by the linker routine which adds symbols from an object
  2284. file. HP's libraries define symbols with HP specific section
  2285. indices, which we have to handle. */
  2286. static bool
  2287. elf_hppa_add_symbol_hook (bfd *abfd,
  2288. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2289. Elf_Internal_Sym *sym,
  2290. const char **namep ATTRIBUTE_UNUSED,
  2291. flagword *flagsp ATTRIBUTE_UNUSED,
  2292. asection **secp,
  2293. bfd_vma *valp)
  2294. {
  2295. unsigned int sec_index = sym->st_shndx;
  2296. switch (sec_index)
  2297. {
  2298. case SHN_PARISC_ANSI_COMMON:
  2299. *secp = bfd_make_section_old_way (abfd, ".PARISC.ansi.common");
  2300. (*secp)->flags |= SEC_IS_COMMON;
  2301. *valp = sym->st_size;
  2302. break;
  2303. case SHN_PARISC_HUGE_COMMON:
  2304. *secp = bfd_make_section_old_way (abfd, ".PARISC.huge.common");
  2305. (*secp)->flags |= SEC_IS_COMMON;
  2306. *valp = sym->st_size;
  2307. break;
  2308. }
  2309. return true;
  2310. }
  2311. static bool
  2312. elf_hppa_unmark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
  2313. void *data)
  2314. {
  2315. struct bfd_link_info *info = data;
  2316. /* If we are not creating a shared library, and this symbol is
  2317. referenced by a shared library but is not defined anywhere, then
  2318. the generic code will warn that it is undefined.
  2319. This behavior is undesirable on HPs since the standard shared
  2320. libraries contain references to undefined symbols.
  2321. So we twiddle the flags associated with such symbols so that they
  2322. will not trigger the warning. ?!? FIXME. This is horribly fragile.
  2323. Ultimately we should have better controls over the generic ELF BFD
  2324. linker code. */
  2325. if (! bfd_link_relocatable (info)
  2326. && info->unresolved_syms_in_shared_libs != RM_IGNORE
  2327. && h->root.type == bfd_link_hash_undefined
  2328. && h->ref_dynamic
  2329. && !h->ref_regular)
  2330. {
  2331. h->ref_dynamic = 0;
  2332. h->pointer_equality_needed = 1;
  2333. }
  2334. return true;
  2335. }
  2336. static bool
  2337. elf_hppa_remark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
  2338. void *data)
  2339. {
  2340. struct bfd_link_info *info = data;
  2341. /* If we are not creating a shared library, and this symbol is
  2342. referenced by a shared library but is not defined anywhere, then
  2343. the generic code will warn that it is undefined.
  2344. This behavior is undesirable on HPs since the standard shared
  2345. libraries contain references to undefined symbols.
  2346. So we twiddle the flags associated with such symbols so that they
  2347. will not trigger the warning. ?!? FIXME. This is horribly fragile.
  2348. Ultimately we should have better controls over the generic ELF BFD
  2349. linker code. */
  2350. if (! bfd_link_relocatable (info)
  2351. && info->unresolved_syms_in_shared_libs != RM_IGNORE
  2352. && h->root.type == bfd_link_hash_undefined
  2353. && !h->ref_dynamic
  2354. && !h->ref_regular
  2355. && h->pointer_equality_needed)
  2356. {
  2357. h->ref_dynamic = 1;
  2358. h->pointer_equality_needed = 0;
  2359. }
  2360. return true;
  2361. }
  2362. static bool
  2363. elf_hppa_is_dynamic_loader_symbol (const char *name)
  2364. {
  2365. return (! strcmp (name, "__CPU_REVISION")
  2366. || ! strcmp (name, "__CPU_KEYBITS_1")
  2367. || ! strcmp (name, "__SYSTEM_ID_D")
  2368. || ! strcmp (name, "__FPU_MODEL")
  2369. || ! strcmp (name, "__FPU_REVISION")
  2370. || ! strcmp (name, "__ARGC")
  2371. || ! strcmp (name, "__ARGV")
  2372. || ! strcmp (name, "__ENVP")
  2373. || ! strcmp (name, "__TLS_SIZE_D")
  2374. || ! strcmp (name, "__LOAD_INFO")
  2375. || ! strcmp (name, "__systab"));
  2376. }
  2377. /* Record the lowest address for the data and text segments. */
  2378. static void
  2379. elf_hppa_record_segment_addrs (bfd *abfd,
  2380. asection *section,
  2381. void *data)
  2382. {
  2383. struct elf64_hppa_link_hash_table *hppa_info = data;
  2384. if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
  2385. {
  2386. bfd_vma value;
  2387. Elf_Internal_Phdr *p;
  2388. p = _bfd_elf_find_segment_containing_section (abfd, section->output_section);
  2389. BFD_ASSERT (p != NULL);
  2390. value = p->p_vaddr;
  2391. if (section->flags & SEC_READONLY)
  2392. {
  2393. if (value < hppa_info->text_segment_base)
  2394. hppa_info->text_segment_base = value;
  2395. }
  2396. else
  2397. {
  2398. if (value < hppa_info->data_segment_base)
  2399. hppa_info->data_segment_base = value;
  2400. }
  2401. }
  2402. }
  2403. /* Called after we have seen all the input files/sections, but before
  2404. final symbol resolution and section placement has been determined.
  2405. We use this hook to (possibly) provide a value for __gp, then we
  2406. fall back to the generic ELF final link routine. */
  2407. static bool
  2408. elf_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
  2409. {
  2410. struct stat buf;
  2411. struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
  2412. if (hppa_info == NULL)
  2413. return false;
  2414. if (! bfd_link_relocatable (info))
  2415. {
  2416. struct elf_link_hash_entry *gp;
  2417. bfd_vma gp_val;
  2418. /* The linker script defines a value for __gp iff it was referenced
  2419. by one of the objects being linked. First try to find the symbol
  2420. in the hash table. If that fails, just compute the value __gp
  2421. should have had. */
  2422. gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
  2423. false, false);
  2424. if (gp)
  2425. {
  2426. /* Adjust the value of __gp as we may want to slide it into the
  2427. .plt section so that the stubs can access PLT entries without
  2428. using an addil sequence. */
  2429. gp->root.u.def.value += hppa_info->gp_offset;
  2430. gp_val = (gp->root.u.def.section->output_section->vma
  2431. + gp->root.u.def.section->output_offset
  2432. + gp->root.u.def.value);
  2433. }
  2434. else
  2435. {
  2436. asection *sec;
  2437. /* First look for a .plt section. If found, then __gp is the
  2438. address of the .plt + gp_offset.
  2439. If no .plt is found, then look for .dlt, .opd and .data (in
  2440. that order) and set __gp to the base address of whichever
  2441. section is found first. */
  2442. sec = hppa_info->root.splt;
  2443. if (sec && ! (sec->flags & SEC_EXCLUDE))
  2444. gp_val = (sec->output_offset
  2445. + sec->output_section->vma
  2446. + hppa_info->gp_offset);
  2447. else
  2448. {
  2449. sec = hppa_info->dlt_sec;
  2450. if (!sec || (sec->flags & SEC_EXCLUDE))
  2451. sec = hppa_info->opd_sec;
  2452. if (!sec || (sec->flags & SEC_EXCLUDE))
  2453. sec = bfd_get_section_by_name (abfd, ".data");
  2454. if (!sec || (sec->flags & SEC_EXCLUDE))
  2455. gp_val = 0;
  2456. else
  2457. gp_val = sec->output_offset + sec->output_section->vma;
  2458. }
  2459. }
  2460. /* Install whatever value we found/computed for __gp. */
  2461. _bfd_set_gp_value (abfd, gp_val);
  2462. }
  2463. /* We need to know the base of the text and data segments so that we
  2464. can perform SEGREL relocations. We will record the base addresses
  2465. when we encounter the first SEGREL relocation. */
  2466. hppa_info->text_segment_base = (bfd_vma)-1;
  2467. hppa_info->data_segment_base = (bfd_vma)-1;
  2468. /* HP's shared libraries have references to symbols that are not
  2469. defined anywhere. The generic ELF BFD linker code will complain
  2470. about such symbols.
  2471. So we detect the losing case and arrange for the flags on the symbol
  2472. to indicate that it was never referenced. This keeps the generic
  2473. ELF BFD link code happy and appears to not create any secondary
  2474. problems. Ultimately we need a way to control the behavior of the
  2475. generic ELF BFD link code better. */
  2476. elf_link_hash_traverse (elf_hash_table (info),
  2477. elf_hppa_unmark_useless_dynamic_symbols,
  2478. info);
  2479. /* Invoke the regular ELF backend linker to do all the work. */
  2480. if (!bfd_elf_final_link (abfd, info))
  2481. return false;
  2482. elf_link_hash_traverse (elf_hash_table (info),
  2483. elf_hppa_remark_useless_dynamic_symbols,
  2484. info);
  2485. /* If we're producing a final executable, sort the contents of the
  2486. unwind section. */
  2487. if (bfd_link_relocatable (info))
  2488. return true;
  2489. /* Do not attempt to sort non-regular files. This is here
  2490. especially for configure scripts and kernel builds which run
  2491. tests with "ld [...] -o /dev/null". */
  2492. if (stat (bfd_get_filename (abfd), &buf) != 0
  2493. || !S_ISREG(buf.st_mode))
  2494. return true;
  2495. return elf_hppa_sort_unwind (abfd);
  2496. }
  2497. /* Relocate the given INSN. VALUE should be the actual value we want
  2498. to insert into the instruction, ie by this point we should not be
  2499. concerned with computing an offset relative to the DLT, PC, etc.
  2500. Instead this routine is meant to handle the bit manipulations needed
  2501. to insert the relocation into the given instruction. */
  2502. static int
  2503. elf_hppa_relocate_insn (int insn, int sym_value, unsigned int r_type)
  2504. {
  2505. switch (r_type)
  2506. {
  2507. /* This is any 22 bit branch. In PA2.0 syntax it corresponds to
  2508. the "B" instruction. */
  2509. case R_PARISC_PCREL22F:
  2510. case R_PARISC_PCREL22C:
  2511. return (insn & ~0x3ff1ffd) | re_assemble_22 (sym_value);
  2512. /* This is any 12 bit branch. */
  2513. case R_PARISC_PCREL12F:
  2514. return (insn & ~0x1ffd) | re_assemble_12 (sym_value);
  2515. /* This is any 17 bit branch. In PA2.0 syntax it also corresponds
  2516. to the "B" instruction as well as BE. */
  2517. case R_PARISC_PCREL17F:
  2518. case R_PARISC_DIR17F:
  2519. case R_PARISC_DIR17R:
  2520. case R_PARISC_PCREL17C:
  2521. case R_PARISC_PCREL17R:
  2522. return (insn & ~0x1f1ffd) | re_assemble_17 (sym_value);
  2523. /* ADDIL or LDIL instructions. */
  2524. case R_PARISC_DLTREL21L:
  2525. case R_PARISC_DLTIND21L:
  2526. case R_PARISC_LTOFF_FPTR21L:
  2527. case R_PARISC_PCREL21L:
  2528. case R_PARISC_LTOFF_TP21L:
  2529. case R_PARISC_DPREL21L:
  2530. case R_PARISC_PLTOFF21L:
  2531. case R_PARISC_DIR21L:
  2532. return (insn & ~0x1fffff) | re_assemble_21 (sym_value);
  2533. /* LDO and integer loads/stores with 14 bit displacements. */
  2534. case R_PARISC_DLTREL14R:
  2535. case R_PARISC_DLTREL14F:
  2536. case R_PARISC_DLTIND14R:
  2537. case R_PARISC_DLTIND14F:
  2538. case R_PARISC_LTOFF_FPTR14R:
  2539. case R_PARISC_PCREL14R:
  2540. case R_PARISC_PCREL14F:
  2541. case R_PARISC_LTOFF_TP14R:
  2542. case R_PARISC_LTOFF_TP14F:
  2543. case R_PARISC_DPREL14R:
  2544. case R_PARISC_DPREL14F:
  2545. case R_PARISC_PLTOFF14R:
  2546. case R_PARISC_PLTOFF14F:
  2547. case R_PARISC_DIR14R:
  2548. case R_PARISC_DIR14F:
  2549. return (insn & ~0x3fff) | low_sign_unext (sym_value, 14);
  2550. /* PA2.0W LDO and integer loads/stores with 16 bit displacements. */
  2551. case R_PARISC_LTOFF_FPTR16F:
  2552. case R_PARISC_PCREL16F:
  2553. case R_PARISC_LTOFF_TP16F:
  2554. case R_PARISC_GPREL16F:
  2555. case R_PARISC_PLTOFF16F:
  2556. case R_PARISC_DIR16F:
  2557. case R_PARISC_LTOFF16F:
  2558. return (insn & ~0xffff) | re_assemble_16 (sym_value);
  2559. /* Doubleword loads and stores with a 14 bit displacement. */
  2560. case R_PARISC_DLTREL14DR:
  2561. case R_PARISC_DLTIND14DR:
  2562. case R_PARISC_LTOFF_FPTR14DR:
  2563. case R_PARISC_LTOFF_FPTR16DF:
  2564. case R_PARISC_PCREL14DR:
  2565. case R_PARISC_PCREL16DF:
  2566. case R_PARISC_LTOFF_TP14DR:
  2567. case R_PARISC_LTOFF_TP16DF:
  2568. case R_PARISC_DPREL14DR:
  2569. case R_PARISC_GPREL16DF:
  2570. case R_PARISC_PLTOFF14DR:
  2571. case R_PARISC_PLTOFF16DF:
  2572. case R_PARISC_DIR14DR:
  2573. case R_PARISC_DIR16DF:
  2574. case R_PARISC_LTOFF16DF:
  2575. return (insn & ~0x3ff1) | (((sym_value & 0x2000) >> 13)
  2576. | ((sym_value & 0x1ff8) << 1));
  2577. /* Floating point single word load/store instructions. */
  2578. case R_PARISC_DLTREL14WR:
  2579. case R_PARISC_DLTIND14WR:
  2580. case R_PARISC_LTOFF_FPTR14WR:
  2581. case R_PARISC_LTOFF_FPTR16WF:
  2582. case R_PARISC_PCREL14WR:
  2583. case R_PARISC_PCREL16WF:
  2584. case R_PARISC_LTOFF_TP14WR:
  2585. case R_PARISC_LTOFF_TP16WF:
  2586. case R_PARISC_DPREL14WR:
  2587. case R_PARISC_GPREL16WF:
  2588. case R_PARISC_PLTOFF14WR:
  2589. case R_PARISC_PLTOFF16WF:
  2590. case R_PARISC_DIR16WF:
  2591. case R_PARISC_DIR14WR:
  2592. case R_PARISC_LTOFF16WF:
  2593. return (insn & ~0x3ff9) | (((sym_value & 0x2000) >> 13)
  2594. | ((sym_value & 0x1ffc) << 1));
  2595. default:
  2596. return insn;
  2597. }
  2598. }
  2599. /* Compute the value for a relocation (REL) during a final link stage,
  2600. then insert the value into the proper location in CONTENTS.
  2601. VALUE is a tentative value for the relocation and may be overridden
  2602. and modified here based on the specific relocation to be performed.
  2603. For example we do conversions for PC-relative branches in this routine
  2604. or redirection of calls to external routines to stubs.
  2605. The work of actually applying the relocation is left to a helper
  2606. routine in an attempt to reduce the complexity and size of this
  2607. function. */
  2608. static bfd_reloc_status_type
  2609. elf_hppa_final_link_relocate (Elf_Internal_Rela *rel,
  2610. bfd *input_bfd,
  2611. bfd *output_bfd,
  2612. asection *input_section,
  2613. bfd_byte *contents,
  2614. bfd_vma value,
  2615. struct bfd_link_info *info,
  2616. asection *sym_sec,
  2617. struct elf_link_hash_entry *eh)
  2618. {
  2619. struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
  2620. struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
  2621. bfd_vma *local_offsets;
  2622. Elf_Internal_Shdr *symtab_hdr;
  2623. int insn;
  2624. bfd_vma max_branch_offset = 0;
  2625. bfd_vma offset = rel->r_offset;
  2626. bfd_signed_vma addend = rel->r_addend;
  2627. reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
  2628. unsigned int r_symndx = ELF_R_SYM (rel->r_info);
  2629. unsigned int r_type = howto->type;
  2630. bfd_byte *hit_data = contents + offset;
  2631. if (hppa_info == NULL)
  2632. return bfd_reloc_notsupported;
  2633. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2634. local_offsets = elf_local_got_offsets (input_bfd);
  2635. insn = bfd_get_32 (input_bfd, hit_data);
  2636. switch (r_type)
  2637. {
  2638. case R_PARISC_NONE:
  2639. break;
  2640. /* Basic function call support.
  2641. Note for a call to a function defined in another dynamic library
  2642. we want to redirect the call to a stub. */
  2643. /* PC relative relocs without an implicit offset. */
  2644. case R_PARISC_PCREL21L:
  2645. case R_PARISC_PCREL14R:
  2646. case R_PARISC_PCREL14F:
  2647. case R_PARISC_PCREL14WR:
  2648. case R_PARISC_PCREL14DR:
  2649. case R_PARISC_PCREL16F:
  2650. case R_PARISC_PCREL16WF:
  2651. case R_PARISC_PCREL16DF:
  2652. {
  2653. /* If this is a call to a function defined in another dynamic
  2654. library, then redirect the call to the local stub for this
  2655. function. */
  2656. if (sym_sec == NULL || sym_sec->output_section == NULL)
  2657. value = (hh->stub_offset + hppa_info->stub_sec->output_offset
  2658. + hppa_info->stub_sec->output_section->vma);
  2659. /* Turn VALUE into a proper PC relative address. */
  2660. value -= (offset + input_section->output_offset
  2661. + input_section->output_section->vma);
  2662. /* Adjust for any field selectors. */
  2663. if (r_type == R_PARISC_PCREL21L)
  2664. value = hppa_field_adjust (value, -8 + addend, e_lsel);
  2665. else if (r_type == R_PARISC_PCREL14F
  2666. || r_type == R_PARISC_PCREL16F
  2667. || r_type == R_PARISC_PCREL16WF
  2668. || r_type == R_PARISC_PCREL16DF)
  2669. value = hppa_field_adjust (value, -8 + addend, e_fsel);
  2670. else
  2671. value = hppa_field_adjust (value, -8 + addend, e_rsel);
  2672. /* Apply the relocation to the given instruction. */
  2673. insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
  2674. break;
  2675. }
  2676. case R_PARISC_PCREL12F:
  2677. case R_PARISC_PCREL22F:
  2678. case R_PARISC_PCREL17F:
  2679. case R_PARISC_PCREL22C:
  2680. case R_PARISC_PCREL17C:
  2681. case R_PARISC_PCREL17R:
  2682. {
  2683. /* If this is a call to a function defined in another dynamic
  2684. library, then redirect the call to the local stub for this
  2685. function. */
  2686. if (sym_sec == NULL || sym_sec->output_section == NULL)
  2687. value = (hh->stub_offset + hppa_info->stub_sec->output_offset
  2688. + hppa_info->stub_sec->output_section->vma);
  2689. /* Turn VALUE into a proper PC relative address. */
  2690. value -= (offset + input_section->output_offset
  2691. + input_section->output_section->vma);
  2692. addend -= 8;
  2693. if (r_type == (unsigned int) R_PARISC_PCREL22F)
  2694. max_branch_offset = (1 << (22-1)) << 2;
  2695. else if (r_type == (unsigned int) R_PARISC_PCREL17F)
  2696. max_branch_offset = (1 << (17-1)) << 2;
  2697. else if (r_type == (unsigned int) R_PARISC_PCREL12F)
  2698. max_branch_offset = (1 << (12-1)) << 2;
  2699. /* Make sure we can reach the branch target. */
  2700. if (max_branch_offset != 0
  2701. && value + addend + max_branch_offset >= 2*max_branch_offset)
  2702. {
  2703. _bfd_error_handler
  2704. /* xgettext:c-format */
  2705. (_("%pB(%pA+%#" PRIx64 "): cannot reach %s"),
  2706. input_bfd,
  2707. input_section,
  2708. (uint64_t) offset,
  2709. eh ? eh->root.root.string : "unknown");
  2710. bfd_set_error (bfd_error_bad_value);
  2711. return bfd_reloc_overflow;
  2712. }
  2713. /* Adjust for any field selectors. */
  2714. if (r_type == R_PARISC_PCREL17R)
  2715. value = hppa_field_adjust (value, addend, e_rsel);
  2716. else
  2717. value = hppa_field_adjust (value, addend, e_fsel);
  2718. /* All branches are implicitly shifted by 2 places. */
  2719. value >>= 2;
  2720. /* Apply the relocation to the given instruction. */
  2721. insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
  2722. break;
  2723. }
  2724. /* Indirect references to data through the DLT. */
  2725. case R_PARISC_DLTIND14R:
  2726. case R_PARISC_DLTIND14F:
  2727. case R_PARISC_DLTIND14DR:
  2728. case R_PARISC_DLTIND14WR:
  2729. case R_PARISC_DLTIND21L:
  2730. case R_PARISC_LTOFF_FPTR14R:
  2731. case R_PARISC_LTOFF_FPTR14DR:
  2732. case R_PARISC_LTOFF_FPTR14WR:
  2733. case R_PARISC_LTOFF_FPTR21L:
  2734. case R_PARISC_LTOFF_FPTR16F:
  2735. case R_PARISC_LTOFF_FPTR16WF:
  2736. case R_PARISC_LTOFF_FPTR16DF:
  2737. case R_PARISC_LTOFF_TP21L:
  2738. case R_PARISC_LTOFF_TP14R:
  2739. case R_PARISC_LTOFF_TP14F:
  2740. case R_PARISC_LTOFF_TP14WR:
  2741. case R_PARISC_LTOFF_TP14DR:
  2742. case R_PARISC_LTOFF_TP16F:
  2743. case R_PARISC_LTOFF_TP16WF:
  2744. case R_PARISC_LTOFF_TP16DF:
  2745. case R_PARISC_LTOFF16F:
  2746. case R_PARISC_LTOFF16WF:
  2747. case R_PARISC_LTOFF16DF:
  2748. {
  2749. bfd_vma off;
  2750. /* If this relocation was against a local symbol, then we still
  2751. have not set up the DLT entry (it's not convenient to do so
  2752. in the "finalize_dlt" routine because it is difficult to get
  2753. to the local symbol's value).
  2754. So, if this is a local symbol (h == NULL), then we need to
  2755. fill in its DLT entry.
  2756. Similarly we may still need to set up an entry in .opd for
  2757. a local function which had its address taken. */
  2758. if (hh == NULL)
  2759. {
  2760. bfd_vma *local_opd_offsets, *local_dlt_offsets;
  2761. if (local_offsets == NULL)
  2762. abort ();
  2763. /* Now do .opd creation if needed. */
  2764. if (r_type == R_PARISC_LTOFF_FPTR14R
  2765. || r_type == R_PARISC_LTOFF_FPTR14DR
  2766. || r_type == R_PARISC_LTOFF_FPTR14WR
  2767. || r_type == R_PARISC_LTOFF_FPTR21L
  2768. || r_type == R_PARISC_LTOFF_FPTR16F
  2769. || r_type == R_PARISC_LTOFF_FPTR16WF
  2770. || r_type == R_PARISC_LTOFF_FPTR16DF)
  2771. {
  2772. local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
  2773. off = local_opd_offsets[r_symndx];
  2774. /* The last bit records whether we've already initialised
  2775. this local .opd entry. */
  2776. if ((off & 1) != 0)
  2777. {
  2778. BFD_ASSERT (off != (bfd_vma) -1);
  2779. off &= ~1;
  2780. }
  2781. else
  2782. {
  2783. local_opd_offsets[r_symndx] |= 1;
  2784. /* The first two words of an .opd entry are zero. */
  2785. memset (hppa_info->opd_sec->contents + off, 0, 16);
  2786. /* The next word is the address of the function. */
  2787. bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
  2788. (hppa_info->opd_sec->contents + off + 16));
  2789. /* The last word is our local __gp value. */
  2790. value = _bfd_get_gp_value (info->output_bfd);
  2791. bfd_put_64 (hppa_info->opd_sec->owner, value,
  2792. (hppa_info->opd_sec->contents + off + 24));
  2793. }
  2794. /* The DLT value is the address of the .opd entry. */
  2795. value = (off
  2796. + hppa_info->opd_sec->output_offset
  2797. + hppa_info->opd_sec->output_section->vma);
  2798. addend = 0;
  2799. }
  2800. local_dlt_offsets = local_offsets;
  2801. off = local_dlt_offsets[r_symndx];
  2802. if ((off & 1) != 0)
  2803. {
  2804. BFD_ASSERT (off != (bfd_vma) -1);
  2805. off &= ~1;
  2806. }
  2807. else
  2808. {
  2809. local_dlt_offsets[r_symndx] |= 1;
  2810. bfd_put_64 (hppa_info->dlt_sec->owner,
  2811. value + addend,
  2812. hppa_info->dlt_sec->contents + off);
  2813. }
  2814. }
  2815. else
  2816. off = hh->dlt_offset;
  2817. /* We want the value of the DLT offset for this symbol, not
  2818. the symbol's actual address. Note that __gp may not point
  2819. to the start of the DLT, so we have to compute the absolute
  2820. address, then subtract out the value of __gp. */
  2821. value = (off
  2822. + hppa_info->dlt_sec->output_offset
  2823. + hppa_info->dlt_sec->output_section->vma);
  2824. value -= _bfd_get_gp_value (output_bfd);
  2825. /* All DLTIND relocations are basically the same at this point,
  2826. except that we need different field selectors for the 21bit
  2827. version vs the 14bit versions. */
  2828. if (r_type == R_PARISC_DLTIND21L
  2829. || r_type == R_PARISC_LTOFF_FPTR21L
  2830. || r_type == R_PARISC_LTOFF_TP21L)
  2831. value = hppa_field_adjust (value, 0, e_lsel);
  2832. else if (r_type == R_PARISC_DLTIND14F
  2833. || r_type == R_PARISC_LTOFF_FPTR16F
  2834. || r_type == R_PARISC_LTOFF_FPTR16WF
  2835. || r_type == R_PARISC_LTOFF_FPTR16DF
  2836. || r_type == R_PARISC_LTOFF16F
  2837. || r_type == R_PARISC_LTOFF16DF
  2838. || r_type == R_PARISC_LTOFF16WF
  2839. || r_type == R_PARISC_LTOFF_TP16F
  2840. || r_type == R_PARISC_LTOFF_TP16WF
  2841. || r_type == R_PARISC_LTOFF_TP16DF)
  2842. value = hppa_field_adjust (value, 0, e_fsel);
  2843. else
  2844. value = hppa_field_adjust (value, 0, e_rsel);
  2845. insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
  2846. break;
  2847. }
  2848. case R_PARISC_DLTREL14R:
  2849. case R_PARISC_DLTREL14F:
  2850. case R_PARISC_DLTREL14DR:
  2851. case R_PARISC_DLTREL14WR:
  2852. case R_PARISC_DLTREL21L:
  2853. case R_PARISC_DPREL21L:
  2854. case R_PARISC_DPREL14WR:
  2855. case R_PARISC_DPREL14DR:
  2856. case R_PARISC_DPREL14R:
  2857. case R_PARISC_DPREL14F:
  2858. case R_PARISC_GPREL16F:
  2859. case R_PARISC_GPREL16WF:
  2860. case R_PARISC_GPREL16DF:
  2861. {
  2862. /* Subtract out the global pointer value to make value a DLT
  2863. relative address. */
  2864. value -= _bfd_get_gp_value (output_bfd);
  2865. /* All DLTREL relocations are basically the same at this point,
  2866. except that we need different field selectors for the 21bit
  2867. version vs the 14bit versions. */
  2868. if (r_type == R_PARISC_DLTREL21L
  2869. || r_type == R_PARISC_DPREL21L)
  2870. value = hppa_field_adjust (value, addend, e_lrsel);
  2871. else if (r_type == R_PARISC_DLTREL14F
  2872. || r_type == R_PARISC_DPREL14F
  2873. || r_type == R_PARISC_GPREL16F
  2874. || r_type == R_PARISC_GPREL16WF
  2875. || r_type == R_PARISC_GPREL16DF)
  2876. value = hppa_field_adjust (value, addend, e_fsel);
  2877. else
  2878. value = hppa_field_adjust (value, addend, e_rrsel);
  2879. insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
  2880. break;
  2881. }
  2882. case R_PARISC_DIR21L:
  2883. case R_PARISC_DIR17R:
  2884. case R_PARISC_DIR17F:
  2885. case R_PARISC_DIR14R:
  2886. case R_PARISC_DIR14F:
  2887. case R_PARISC_DIR14WR:
  2888. case R_PARISC_DIR14DR:
  2889. case R_PARISC_DIR16F:
  2890. case R_PARISC_DIR16WF:
  2891. case R_PARISC_DIR16DF:
  2892. {
  2893. /* All DIR relocations are basically the same at this point,
  2894. except that branch offsets need to be divided by four, and
  2895. we need different field selectors. Note that we don't
  2896. redirect absolute calls to local stubs. */
  2897. if (r_type == R_PARISC_DIR21L)
  2898. value = hppa_field_adjust (value, addend, e_lrsel);
  2899. else if (r_type == R_PARISC_DIR17F
  2900. || r_type == R_PARISC_DIR16F
  2901. || r_type == R_PARISC_DIR16WF
  2902. || r_type == R_PARISC_DIR16DF
  2903. || r_type == R_PARISC_DIR14F)
  2904. value = hppa_field_adjust (value, addend, e_fsel);
  2905. else
  2906. value = hppa_field_adjust (value, addend, e_rrsel);
  2907. if (r_type == R_PARISC_DIR17R || r_type == R_PARISC_DIR17F)
  2908. /* All branches are implicitly shifted by 2 places. */
  2909. value >>= 2;
  2910. insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
  2911. break;
  2912. }
  2913. case R_PARISC_PLTOFF21L:
  2914. case R_PARISC_PLTOFF14R:
  2915. case R_PARISC_PLTOFF14F:
  2916. case R_PARISC_PLTOFF14WR:
  2917. case R_PARISC_PLTOFF14DR:
  2918. case R_PARISC_PLTOFF16F:
  2919. case R_PARISC_PLTOFF16WF:
  2920. case R_PARISC_PLTOFF16DF:
  2921. {
  2922. /* We want the value of the PLT offset for this symbol, not
  2923. the symbol's actual address. Note that __gp may not point
  2924. to the start of the DLT, so we have to compute the absolute
  2925. address, then subtract out the value of __gp. */
  2926. value = (hh->plt_offset
  2927. + hppa_info->root.splt->output_offset
  2928. + hppa_info->root.splt->output_section->vma);
  2929. value -= _bfd_get_gp_value (output_bfd);
  2930. /* All PLTOFF relocations are basically the same at this point,
  2931. except that we need different field selectors for the 21bit
  2932. version vs the 14bit versions. */
  2933. if (r_type == R_PARISC_PLTOFF21L)
  2934. value = hppa_field_adjust (value, addend, e_lrsel);
  2935. else if (r_type == R_PARISC_PLTOFF14F
  2936. || r_type == R_PARISC_PLTOFF16F
  2937. || r_type == R_PARISC_PLTOFF16WF
  2938. || r_type == R_PARISC_PLTOFF16DF)
  2939. value = hppa_field_adjust (value, addend, e_fsel);
  2940. else
  2941. value = hppa_field_adjust (value, addend, e_rrsel);
  2942. insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
  2943. break;
  2944. }
  2945. case R_PARISC_LTOFF_FPTR32:
  2946. {
  2947. /* FIXME: There used to be code here to create the FPTR itself if
  2948. the relocation was against a local symbol. But the code could
  2949. never have worked. If the assert below is ever triggered then
  2950. the code will need to be reinstated and fixed so that it does
  2951. what is needed. */
  2952. BFD_ASSERT (hh != NULL);
  2953. /* We want the value of the DLT offset for this symbol, not
  2954. the symbol's actual address. Note that __gp may not point
  2955. to the start of the DLT, so we have to compute the absolute
  2956. address, then subtract out the value of __gp. */
  2957. value = (hh->dlt_offset
  2958. + hppa_info->dlt_sec->output_offset
  2959. + hppa_info->dlt_sec->output_section->vma);
  2960. value -= _bfd_get_gp_value (output_bfd);
  2961. bfd_put_32 (input_bfd, value, hit_data);
  2962. return bfd_reloc_ok;
  2963. }
  2964. case R_PARISC_LTOFF_FPTR64:
  2965. case R_PARISC_LTOFF_TP64:
  2966. {
  2967. /* We may still need to create the FPTR itself if it was for
  2968. a local symbol. */
  2969. if (eh == NULL && r_type == R_PARISC_LTOFF_FPTR64)
  2970. {
  2971. /* The first two words of an .opd entry are zero. */
  2972. memset (hppa_info->opd_sec->contents + hh->opd_offset, 0, 16);
  2973. /* The next word is the address of the function. */
  2974. bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
  2975. (hppa_info->opd_sec->contents
  2976. + hh->opd_offset + 16));
  2977. /* The last word is our local __gp value. */
  2978. value = _bfd_get_gp_value (info->output_bfd);
  2979. bfd_put_64 (hppa_info->opd_sec->owner, value,
  2980. hppa_info->opd_sec->contents + hh->opd_offset + 24);
  2981. /* The DLT value is the address of the .opd entry. */
  2982. value = (hh->opd_offset
  2983. + hppa_info->opd_sec->output_offset
  2984. + hppa_info->opd_sec->output_section->vma);
  2985. bfd_put_64 (hppa_info->dlt_sec->owner,
  2986. value,
  2987. hppa_info->dlt_sec->contents + hh->dlt_offset);
  2988. }
  2989. /* We want the value of the DLT offset for this symbol, not
  2990. the symbol's actual address. Note that __gp may not point
  2991. to the start of the DLT, so we have to compute the absolute
  2992. address, then subtract out the value of __gp. */
  2993. value = (hh->dlt_offset
  2994. + hppa_info->dlt_sec->output_offset
  2995. + hppa_info->dlt_sec->output_section->vma);
  2996. value -= _bfd_get_gp_value (output_bfd);
  2997. bfd_put_64 (input_bfd, value, hit_data);
  2998. return bfd_reloc_ok;
  2999. }
  3000. case R_PARISC_DIR32:
  3001. bfd_put_32 (input_bfd, value + addend, hit_data);
  3002. return bfd_reloc_ok;
  3003. case R_PARISC_DIR64:
  3004. bfd_put_64 (input_bfd, value + addend, hit_data);
  3005. return bfd_reloc_ok;
  3006. case R_PARISC_GPREL64:
  3007. /* Subtract out the global pointer value to make value a DLT
  3008. relative address. */
  3009. value -= _bfd_get_gp_value (output_bfd);
  3010. bfd_put_64 (input_bfd, value + addend, hit_data);
  3011. return bfd_reloc_ok;
  3012. case R_PARISC_LTOFF64:
  3013. /* We want the value of the DLT offset for this symbol, not
  3014. the symbol's actual address. Note that __gp may not point
  3015. to the start of the DLT, so we have to compute the absolute
  3016. address, then subtract out the value of __gp. */
  3017. value = (hh->dlt_offset
  3018. + hppa_info->dlt_sec->output_offset
  3019. + hppa_info->dlt_sec->output_section->vma);
  3020. value -= _bfd_get_gp_value (output_bfd);
  3021. bfd_put_64 (input_bfd, value + addend, hit_data);
  3022. return bfd_reloc_ok;
  3023. case R_PARISC_PCREL32:
  3024. {
  3025. /* If this is a call to a function defined in another dynamic
  3026. library, then redirect the call to the local stub for this
  3027. function. */
  3028. if (sym_sec == NULL || sym_sec->output_section == NULL)
  3029. value = (hh->stub_offset + hppa_info->stub_sec->output_offset
  3030. + hppa_info->stub_sec->output_section->vma);
  3031. /* Turn VALUE into a proper PC relative address. */
  3032. value -= (offset + input_section->output_offset
  3033. + input_section->output_section->vma);
  3034. value += addend;
  3035. value -= 8;
  3036. bfd_put_32 (input_bfd, value, hit_data);
  3037. return bfd_reloc_ok;
  3038. }
  3039. case R_PARISC_PCREL64:
  3040. {
  3041. /* If this is a call to a function defined in another dynamic
  3042. library, then redirect the call to the local stub for this
  3043. function. */
  3044. if (sym_sec == NULL || sym_sec->output_section == NULL)
  3045. value = (hh->stub_offset + hppa_info->stub_sec->output_offset
  3046. + hppa_info->stub_sec->output_section->vma);
  3047. /* Turn VALUE into a proper PC relative address. */
  3048. value -= (offset + input_section->output_offset
  3049. + input_section->output_section->vma);
  3050. value += addend;
  3051. value -= 8;
  3052. bfd_put_64 (input_bfd, value, hit_data);
  3053. return bfd_reloc_ok;
  3054. }
  3055. case R_PARISC_FPTR64:
  3056. {
  3057. bfd_vma off;
  3058. /* We may still need to create the FPTR itself if it was for
  3059. a local symbol. */
  3060. if (hh == NULL)
  3061. {
  3062. bfd_vma *local_opd_offsets;
  3063. if (local_offsets == NULL)
  3064. abort ();
  3065. local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
  3066. off = local_opd_offsets[r_symndx];
  3067. /* The last bit records whether we've already initialised
  3068. this local .opd entry. */
  3069. if ((off & 1) != 0)
  3070. {
  3071. BFD_ASSERT (off != (bfd_vma) -1);
  3072. off &= ~1;
  3073. }
  3074. else
  3075. {
  3076. /* The first two words of an .opd entry are zero. */
  3077. memset (hppa_info->opd_sec->contents + off, 0, 16);
  3078. /* The next word is the address of the function. */
  3079. bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
  3080. (hppa_info->opd_sec->contents + off + 16));
  3081. /* The last word is our local __gp value. */
  3082. value = _bfd_get_gp_value (info->output_bfd);
  3083. bfd_put_64 (hppa_info->opd_sec->owner, value,
  3084. hppa_info->opd_sec->contents + off + 24);
  3085. }
  3086. }
  3087. else
  3088. off = hh->opd_offset;
  3089. if (hh == NULL || hh->want_opd)
  3090. /* We want the value of the OPD offset for this symbol. */
  3091. value = (off
  3092. + hppa_info->opd_sec->output_offset
  3093. + hppa_info->opd_sec->output_section->vma);
  3094. else
  3095. /* We want the address of the symbol. */
  3096. value += addend;
  3097. bfd_put_64 (input_bfd, value, hit_data);
  3098. return bfd_reloc_ok;
  3099. }
  3100. case R_PARISC_SECREL32:
  3101. if (sym_sec && sym_sec->output_section)
  3102. value -= sym_sec->output_section->vma;
  3103. bfd_put_32 (input_bfd, value + addend, hit_data);
  3104. return bfd_reloc_ok;
  3105. case R_PARISC_SEGREL32:
  3106. case R_PARISC_SEGREL64:
  3107. {
  3108. /* If this is the first SEGREL relocation, then initialize
  3109. the segment base values. */
  3110. if (hppa_info->text_segment_base == (bfd_vma) -1)
  3111. bfd_map_over_sections (output_bfd, elf_hppa_record_segment_addrs,
  3112. hppa_info);
  3113. /* VALUE holds the absolute address. We want to include the
  3114. addend, then turn it into a segment relative address.
  3115. The segment is derived from SYM_SEC. We assume that there are
  3116. only two segments of note in the resulting executable/shlib.
  3117. A readonly segment (.text) and a readwrite segment (.data). */
  3118. value += addend;
  3119. if (sym_sec->flags & SEC_CODE)
  3120. value -= hppa_info->text_segment_base;
  3121. else
  3122. value -= hppa_info->data_segment_base;
  3123. if (r_type == R_PARISC_SEGREL32)
  3124. bfd_put_32 (input_bfd, value, hit_data);
  3125. else
  3126. bfd_put_64 (input_bfd, value, hit_data);
  3127. return bfd_reloc_ok;
  3128. }
  3129. /* Something we don't know how to handle. */
  3130. default:
  3131. return bfd_reloc_notsupported;
  3132. }
  3133. /* Update the instruction word. */
  3134. bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
  3135. return bfd_reloc_ok;
  3136. }
  3137. /* Relocate an HPPA ELF section. */
  3138. static int
  3139. elf64_hppa_relocate_section (bfd *output_bfd,
  3140. struct bfd_link_info *info,
  3141. bfd *input_bfd,
  3142. asection *input_section,
  3143. bfd_byte *contents,
  3144. Elf_Internal_Rela *relocs,
  3145. Elf_Internal_Sym *local_syms,
  3146. asection **local_sections)
  3147. {
  3148. Elf_Internal_Shdr *symtab_hdr;
  3149. Elf_Internal_Rela *rel;
  3150. Elf_Internal_Rela *relend;
  3151. struct elf64_hppa_link_hash_table *hppa_info;
  3152. hppa_info = hppa_link_hash_table (info);
  3153. if (hppa_info == NULL)
  3154. return false;
  3155. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  3156. rel = relocs;
  3157. relend = relocs + input_section->reloc_count;
  3158. for (; rel < relend; rel++)
  3159. {
  3160. int r_type;
  3161. reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
  3162. unsigned long r_symndx;
  3163. struct elf_link_hash_entry *eh;
  3164. Elf_Internal_Sym *sym;
  3165. asection *sym_sec;
  3166. bfd_vma relocation;
  3167. bfd_reloc_status_type r;
  3168. r_type = ELF_R_TYPE (rel->r_info);
  3169. if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
  3170. {
  3171. bfd_set_error (bfd_error_bad_value);
  3172. return false;
  3173. }
  3174. if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
  3175. || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
  3176. continue;
  3177. /* This is a final link. */
  3178. r_symndx = ELF_R_SYM (rel->r_info);
  3179. eh = NULL;
  3180. sym = NULL;
  3181. sym_sec = NULL;
  3182. if (r_symndx < symtab_hdr->sh_info)
  3183. {
  3184. /* This is a local symbol, hh defaults to NULL. */
  3185. sym = local_syms + r_symndx;
  3186. sym_sec = local_sections[r_symndx];
  3187. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rel);
  3188. }
  3189. else
  3190. {
  3191. /* This is not a local symbol. */
  3192. struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
  3193. /* It seems this can happen with erroneous or unsupported
  3194. input (mixing a.out and elf in an archive, for example.) */
  3195. if (sym_hashes == NULL)
  3196. return false;
  3197. eh = sym_hashes[r_symndx - symtab_hdr->sh_info];
  3198. if (info->wrap_hash != NULL
  3199. && (input_section->flags & SEC_DEBUGGING) != 0)
  3200. eh = ((struct elf_link_hash_entry *)
  3201. unwrap_hash_lookup (info, input_bfd, &eh->root));
  3202. while (eh->root.type == bfd_link_hash_indirect
  3203. || eh->root.type == bfd_link_hash_warning)
  3204. eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
  3205. relocation = 0;
  3206. if (eh->root.type == bfd_link_hash_defined
  3207. || eh->root.type == bfd_link_hash_defweak)
  3208. {
  3209. sym_sec = eh->root.u.def.section;
  3210. if (sym_sec != NULL
  3211. && sym_sec->output_section != NULL)
  3212. relocation = (eh->root.u.def.value
  3213. + sym_sec->output_section->vma
  3214. + sym_sec->output_offset);
  3215. }
  3216. else if (eh->root.type == bfd_link_hash_undefweak)
  3217. ;
  3218. else if (info->unresolved_syms_in_objects == RM_IGNORE
  3219. && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT)
  3220. ;
  3221. else if (!bfd_link_relocatable (info)
  3222. && elf_hppa_is_dynamic_loader_symbol (eh->root.root.string))
  3223. continue;
  3224. else if (!bfd_link_relocatable (info))
  3225. {
  3226. bool err;
  3227. err = (info->unresolved_syms_in_objects == RM_DIAGNOSE
  3228. && !info->warn_unresolved_syms)
  3229. || ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT;
  3230. info->callbacks->undefined_symbol
  3231. (info, eh->root.root.string, input_bfd,
  3232. input_section, rel->r_offset, err);
  3233. }
  3234. if (!bfd_link_relocatable (info)
  3235. && relocation == 0
  3236. && eh->root.type != bfd_link_hash_defined
  3237. && eh->root.type != bfd_link_hash_defweak
  3238. && eh->root.type != bfd_link_hash_undefweak)
  3239. {
  3240. if (info->unresolved_syms_in_objects == RM_IGNORE
  3241. && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT
  3242. && eh->type == STT_PARISC_MILLI)
  3243. info->callbacks->undefined_symbol
  3244. (info, eh_name (eh), input_bfd,
  3245. input_section, rel->r_offset, false);
  3246. }
  3247. }
  3248. if (sym_sec != NULL && discarded_section (sym_sec))
  3249. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  3250. rel, 1, relend, howto, 0, contents);
  3251. if (bfd_link_relocatable (info))
  3252. continue;
  3253. r = elf_hppa_final_link_relocate (rel, input_bfd, output_bfd,
  3254. input_section, contents,
  3255. relocation, info, sym_sec,
  3256. eh);
  3257. if (r != bfd_reloc_ok)
  3258. {
  3259. switch (r)
  3260. {
  3261. default:
  3262. abort ();
  3263. case bfd_reloc_overflow:
  3264. {
  3265. const char *sym_name;
  3266. if (eh != NULL)
  3267. sym_name = NULL;
  3268. else
  3269. {
  3270. sym_name = bfd_elf_string_from_elf_section (input_bfd,
  3271. symtab_hdr->sh_link,
  3272. sym->st_name);
  3273. if (sym_name == NULL)
  3274. return false;
  3275. if (*sym_name == '\0')
  3276. sym_name = bfd_section_name (sym_sec);
  3277. }
  3278. (*info->callbacks->reloc_overflow)
  3279. (info, (eh ? &eh->root : NULL), sym_name, howto->name,
  3280. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  3281. }
  3282. break;
  3283. }
  3284. }
  3285. }
  3286. return true;
  3287. }
  3288. static const struct bfd_elf_special_section elf64_hppa_special_sections[] =
  3289. {
  3290. { STRING_COMMA_LEN (".tbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_HP_TLS },
  3291. { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  3292. { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  3293. { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
  3294. { STRING_COMMA_LEN (".dlt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
  3295. { STRING_COMMA_LEN (".sdata"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
  3296. { STRING_COMMA_LEN (".sbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
  3297. { NULL, 0, 0, 0, 0 }
  3298. };
  3299. /* The hash bucket size is the standard one, namely 4. */
  3300. const struct elf_size_info hppa64_elf_size_info =
  3301. {
  3302. sizeof (Elf64_External_Ehdr),
  3303. sizeof (Elf64_External_Phdr),
  3304. sizeof (Elf64_External_Shdr),
  3305. sizeof (Elf64_External_Rel),
  3306. sizeof (Elf64_External_Rela),
  3307. sizeof (Elf64_External_Sym),
  3308. sizeof (Elf64_External_Dyn),
  3309. sizeof (Elf_External_Note),
  3310. 4,
  3311. 1,
  3312. 64, 3,
  3313. ELFCLASS64, EV_CURRENT,
  3314. bfd_elf64_write_out_phdrs,
  3315. bfd_elf64_write_shdrs_and_ehdr,
  3316. bfd_elf64_checksum_contents,
  3317. bfd_elf64_write_relocs,
  3318. bfd_elf64_swap_symbol_in,
  3319. bfd_elf64_swap_symbol_out,
  3320. bfd_elf64_slurp_reloc_table,
  3321. bfd_elf64_slurp_symbol_table,
  3322. bfd_elf64_swap_dyn_in,
  3323. bfd_elf64_swap_dyn_out,
  3324. bfd_elf64_swap_reloc_in,
  3325. bfd_elf64_swap_reloc_out,
  3326. bfd_elf64_swap_reloca_in,
  3327. bfd_elf64_swap_reloca_out
  3328. };
  3329. #define TARGET_BIG_SYM hppa_elf64_vec
  3330. #define TARGET_BIG_NAME "elf64-hppa"
  3331. #define ELF_ARCH bfd_arch_hppa
  3332. #define ELF_TARGET_ID HPPA64_ELF_DATA
  3333. #define ELF_MACHINE_CODE EM_PARISC
  3334. /* This is not strictly correct. The maximum page size for PA2.0 is
  3335. 64M. But everything still uses 4k. */
  3336. #define ELF_MAXPAGESIZE 0x1000
  3337. #define ELF_OSABI ELFOSABI_HPUX
  3338. #define bfd_elf64_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
  3339. #define bfd_elf64_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
  3340. #define bfd_elf64_bfd_is_local_label_name elf_hppa_is_local_label_name
  3341. #define elf_info_to_howto elf_hppa_info_to_howto
  3342. #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
  3343. #define elf_backend_section_from_shdr elf64_hppa_section_from_shdr
  3344. #define elf_backend_object_p elf64_hppa_object_p
  3345. #define elf_backend_final_write_processing \
  3346. elf_hppa_final_write_processing
  3347. #define elf_backend_fake_sections elf_hppa_fake_sections
  3348. #define elf_backend_add_symbol_hook elf_hppa_add_symbol_hook
  3349. #define elf_backend_relocate_section elf_hppa_relocate_section
  3350. #define bfd_elf64_bfd_final_link elf_hppa_final_link
  3351. #define elf_backend_create_dynamic_sections \
  3352. elf64_hppa_create_dynamic_sections
  3353. #define elf_backend_init_file_header elf64_hppa_init_file_header
  3354. #define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
  3355. #define elf_backend_adjust_dynamic_symbol \
  3356. elf64_hppa_adjust_dynamic_symbol
  3357. #define elf_backend_size_dynamic_sections \
  3358. elf64_hppa_size_dynamic_sections
  3359. #define elf_backend_finish_dynamic_symbol \
  3360. elf64_hppa_finish_dynamic_symbol
  3361. #define elf_backend_finish_dynamic_sections \
  3362. elf64_hppa_finish_dynamic_sections
  3363. #define elf_backend_grok_prstatus elf64_hppa_grok_prstatus
  3364. #define elf_backend_grok_psinfo elf64_hppa_grok_psinfo
  3365. /* Stuff for the BFD linker: */
  3366. #define bfd_elf64_bfd_link_hash_table_create \
  3367. elf64_hppa_hash_table_create
  3368. #define elf_backend_check_relocs \
  3369. elf64_hppa_check_relocs
  3370. #define elf_backend_size_info \
  3371. hppa64_elf_size_info
  3372. #define elf_backend_additional_program_headers \
  3373. elf64_hppa_additional_program_headers
  3374. #define elf_backend_modify_segment_map \
  3375. elf64_hppa_modify_segment_map
  3376. #define elf_backend_allow_non_load_phdr \
  3377. elf64_hppa_allow_non_load_phdr
  3378. #define elf_backend_link_output_symbol_hook \
  3379. elf64_hppa_link_output_symbol_hook
  3380. #define elf_backend_want_got_plt 0
  3381. #define elf_backend_plt_readonly 0
  3382. #define elf_backend_want_plt_sym 0
  3383. #define elf_backend_got_header_size 0
  3384. #define elf_backend_type_change_ok true
  3385. #define elf_backend_get_symbol_type elf64_hppa_elf_get_symbol_type
  3386. #define elf_backend_reloc_type_class elf64_hppa_reloc_type_class
  3387. #define elf_backend_rela_normal 1
  3388. #define elf_backend_special_sections elf64_hppa_special_sections
  3389. #define elf_backend_action_discarded elf_hppa_action_discarded
  3390. #define elf_backend_section_from_phdr elf64_hppa_section_from_phdr
  3391. #define elf64_bed elf64_hppa_hpux_bed
  3392. #include "elf64-target.h"
  3393. #undef TARGET_BIG_SYM
  3394. #define TARGET_BIG_SYM hppa_elf64_linux_vec
  3395. #undef TARGET_BIG_NAME
  3396. #define TARGET_BIG_NAME "elf64-hppa-linux"
  3397. #undef ELF_OSABI
  3398. #define ELF_OSABI ELFOSABI_GNU
  3399. #undef elf64_bed
  3400. #define elf64_bed elf64_hppa_linux_bed
  3401. #undef elf_backend_special_sections
  3402. #define elf_backend_special_sections (elf64_hppa_special_sections + 1)
  3403. #include "elf64-target.h"