elfnn-ia64.c 138 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116
  1. /* IA-64 support for 64-bit ELF
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "elf-bfd.h"
  21. #include "opcode/ia64.h"
  22. #include "elf/ia64.h"
  23. #include "objalloc.h"
  24. #include "hashtab.h"
  25. #include "elfxx-ia64.h"
  26. #define ARCH_SIZE NN
  27. #if ARCH_SIZE == 64
  28. #define LOG_SECTION_ALIGN 3
  29. #endif
  30. #if ARCH_SIZE == 32
  31. #define LOG_SECTION_ALIGN 2
  32. #endif
  33. #define is_ia64_elf(bfd) \
  34. (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
  35. && elf_object_id (bfd) == IA64_ELF_DATA)
  36. typedef struct bfd_hash_entry *(*new_hash_entry_func)
  37. (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
  38. /* In dynamically (linker-) created sections, we generally need to keep track
  39. of the place a symbol or expression got allocated to. This is done via hash
  40. tables that store entries of the following type. */
  41. struct elfNN_ia64_dyn_sym_info
  42. {
  43. /* The addend for which this entry is relevant. */
  44. bfd_vma addend;
  45. bfd_vma got_offset;
  46. bfd_vma fptr_offset;
  47. bfd_vma pltoff_offset;
  48. bfd_vma plt_offset;
  49. bfd_vma plt2_offset;
  50. bfd_vma tprel_offset;
  51. bfd_vma dtpmod_offset;
  52. bfd_vma dtprel_offset;
  53. /* The symbol table entry, if any, that this was derived from. */
  54. struct elf_link_hash_entry *h;
  55. /* Used to count non-got, non-plt relocations for delayed sizing
  56. of relocation sections. */
  57. struct elfNN_ia64_dyn_reloc_entry
  58. {
  59. struct elfNN_ia64_dyn_reloc_entry *next;
  60. asection *srel;
  61. int type;
  62. int count;
  63. /* Is this reloc against readonly section? */
  64. bool reltext;
  65. } *reloc_entries;
  66. /* TRUE when the section contents have been updated. */
  67. unsigned got_done : 1;
  68. unsigned fptr_done : 1;
  69. unsigned pltoff_done : 1;
  70. unsigned tprel_done : 1;
  71. unsigned dtpmod_done : 1;
  72. unsigned dtprel_done : 1;
  73. /* TRUE for the different kinds of linker data we want created. */
  74. unsigned want_got : 1;
  75. unsigned want_gotx : 1;
  76. unsigned want_fptr : 1;
  77. unsigned want_ltoff_fptr : 1;
  78. unsigned want_plt : 1;
  79. unsigned want_plt2 : 1;
  80. unsigned want_pltoff : 1;
  81. unsigned want_tprel : 1;
  82. unsigned want_dtpmod : 1;
  83. unsigned want_dtprel : 1;
  84. };
  85. struct elfNN_ia64_local_hash_entry
  86. {
  87. int id;
  88. unsigned int r_sym;
  89. /* The number of elements in elfNN_ia64_dyn_sym_info array. */
  90. unsigned int count;
  91. /* The number of sorted elements in elfNN_ia64_dyn_sym_info array. */
  92. unsigned int sorted_count;
  93. /* The size of elfNN_ia64_dyn_sym_info array. */
  94. unsigned int size;
  95. /* The array of elfNN_ia64_dyn_sym_info. */
  96. struct elfNN_ia64_dyn_sym_info *info;
  97. /* TRUE if this hash entry's addends was translated for
  98. SHF_MERGE optimization. */
  99. unsigned sec_merge_done : 1;
  100. };
  101. struct elfNN_ia64_link_hash_entry
  102. {
  103. struct elf_link_hash_entry root;
  104. /* The number of elements in elfNN_ia64_dyn_sym_info array. */
  105. unsigned int count;
  106. /* The number of sorted elements in elfNN_ia64_dyn_sym_info array. */
  107. unsigned int sorted_count;
  108. /* The size of elfNN_ia64_dyn_sym_info array. */
  109. unsigned int size;
  110. /* The array of elfNN_ia64_dyn_sym_info. */
  111. struct elfNN_ia64_dyn_sym_info *info;
  112. };
  113. struct elfNN_ia64_link_hash_table
  114. {
  115. /* The main hash table. */
  116. struct elf_link_hash_table root;
  117. asection *fptr_sec; /* Function descriptor table (or NULL). */
  118. asection *rel_fptr_sec; /* Dynamic relocation section for same. */
  119. asection *pltoff_sec; /* Private descriptors for plt (or NULL). */
  120. asection *rel_pltoff_sec; /* Dynamic relocation section for same. */
  121. bfd_size_type minplt_entries; /* Number of minplt entries. */
  122. unsigned self_dtpmod_done : 1;/* Has self DTPMOD entry been finished? */
  123. bfd_vma self_dtpmod_offset; /* .got offset to self DTPMOD entry. */
  124. /* There are maybe R_IA64_GPREL22 relocations, including those
  125. optimized from R_IA64_LTOFF22X, against non-SHF_IA_64_SHORT
  126. sections. We need to record those sections so that we can choose
  127. a proper GP to cover all R_IA64_GPREL22 relocations. */
  128. asection *max_short_sec; /* Maximum short output section. */
  129. bfd_vma max_short_offset; /* Maximum short offset. */
  130. asection *min_short_sec; /* Minimum short output section. */
  131. bfd_vma min_short_offset; /* Minimum short offset. */
  132. htab_t loc_hash_table;
  133. void *loc_hash_memory;
  134. };
  135. struct elfNN_ia64_allocate_data
  136. {
  137. struct bfd_link_info *info;
  138. bfd_size_type ofs;
  139. bool only_got;
  140. };
  141. #define elfNN_ia64_hash_table(p) \
  142. ((is_elf_hash_table ((p)->hash) \
  143. && elf_hash_table_id (elf_hash_table (p)) == IA64_ELF_DATA) \
  144. ? (struct elfNN_ia64_link_hash_table *) (p)->hash : NULL)
  145. static struct elfNN_ia64_dyn_sym_info * get_dyn_sym_info
  146. (struct elfNN_ia64_link_hash_table *ia64_info,
  147. struct elf_link_hash_entry *h,
  148. bfd *abfd, const Elf_Internal_Rela *rel, bool create);
  149. static bool elfNN_ia64_dynamic_symbol_p
  150. (struct elf_link_hash_entry *h, struct bfd_link_info *info, int);
  151. static bool elfNN_ia64_choose_gp
  152. (bfd *abfd, struct bfd_link_info *info, bool final);
  153. static void elfNN_ia64_dyn_sym_traverse
  154. (struct elfNN_ia64_link_hash_table *ia64_info,
  155. bool (*func) (struct elfNN_ia64_dyn_sym_info *, void *),
  156. void * info);
  157. static bool allocate_global_data_got
  158. (struct elfNN_ia64_dyn_sym_info *dyn_i, void * data);
  159. static bool allocate_global_fptr_got
  160. (struct elfNN_ia64_dyn_sym_info *dyn_i, void * data);
  161. static bool allocate_local_got
  162. (struct elfNN_ia64_dyn_sym_info *dyn_i, void * data);
  163. static bool elfNN_ia64_hpux_vec
  164. (const bfd_target *vec);
  165. static bool allocate_dynrel_entries
  166. (struct elfNN_ia64_dyn_sym_info *dyn_i, void * data);
  167. static asection *get_pltoff
  168. (bfd *abfd, struct bfd_link_info *info,
  169. struct elfNN_ia64_link_hash_table *ia64_info);
  170. /* ia64-specific relocation. */
  171. /* Given a ELF reloc, return the matching HOWTO structure. */
  172. static bool
  173. elfNN_ia64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
  174. arelent *bfd_reloc,
  175. Elf_Internal_Rela *elf_reloc)
  176. {
  177. unsigned int r_type = ELF32_R_TYPE (elf_reloc->r_info);
  178. bfd_reloc->howto = ia64_elf_lookup_howto (r_type);
  179. if (bfd_reloc->howto == NULL)
  180. {
  181. /* xgettext:c-format */
  182. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  183. abfd, r_type);
  184. bfd_set_error (bfd_error_bad_value);
  185. return false;
  186. }
  187. return true;
  188. }
  189. #define PLT_HEADER_SIZE (3 * 16)
  190. #define PLT_MIN_ENTRY_SIZE (1 * 16)
  191. #define PLT_FULL_ENTRY_SIZE (2 * 16)
  192. #define PLT_RESERVED_WORDS 3
  193. static const bfd_byte plt_header[PLT_HEADER_SIZE] =
  194. {
  195. 0x0b, 0x10, 0x00, 0x1c, 0x00, 0x21, /* [MMI] mov r2=r14;; */
  196. 0xe0, 0x00, 0x08, 0x00, 0x48, 0x00, /* addl r14=0,r2 */
  197. 0x00, 0x00, 0x04, 0x00, /* nop.i 0x0;; */
  198. 0x0b, 0x80, 0x20, 0x1c, 0x18, 0x14, /* [MMI] ld8 r16=[r14],8;; */
  199. 0x10, 0x41, 0x38, 0x30, 0x28, 0x00, /* ld8 r17=[r14],8 */
  200. 0x00, 0x00, 0x04, 0x00, /* nop.i 0x0;; */
  201. 0x11, 0x08, 0x00, 0x1c, 0x18, 0x10, /* [MIB] ld8 r1=[r14] */
  202. 0x60, 0x88, 0x04, 0x80, 0x03, 0x00, /* mov b6=r17 */
  203. 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
  204. };
  205. static const bfd_byte plt_min_entry[PLT_MIN_ENTRY_SIZE] =
  206. {
  207. 0x11, 0x78, 0x00, 0x00, 0x00, 0x24, /* [MIB] mov r15=0 */
  208. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, /* nop.i 0x0 */
  209. 0x00, 0x00, 0x00, 0x40 /* br.few 0 <PLT0>;; */
  210. };
  211. static const bfd_byte plt_full_entry[PLT_FULL_ENTRY_SIZE] =
  212. {
  213. 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
  214. 0x00, 0x41, 0x3c, 0x70, 0x29, 0xc0, /* ld8.acq r16=[r15],8*/
  215. 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
  216. 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
  217. 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
  218. 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
  219. };
  220. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
  221. static const bfd_byte oor_brl[16] =
  222. {
  223. 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
  224. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* brl.sptk.few tgt;; */
  225. 0x00, 0x00, 0x00, 0xc0
  226. };
  227. static const bfd_byte oor_ip[48] =
  228. {
  229. 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MLX] nop.m 0 */
  230. 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, /* movl r15=0 */
  231. 0x01, 0x00, 0x00, 0x60,
  232. 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MII] nop.m 0 */
  233. 0x00, 0x01, 0x00, 0x60, 0x00, 0x00, /* mov r16=ip;; */
  234. 0xf2, 0x80, 0x00, 0x80, /* add r16=r15,r16;; */
  235. 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, /* [MIB] nop.m 0 */
  236. 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
  237. 0x60, 0x00, 0x80, 0x00 /* br b6;; */
  238. };
  239. static size_t oor_branch_size = sizeof (oor_brl);
  240. void
  241. bfd_elfNN_ia64_after_parse (int itanium)
  242. {
  243. oor_branch_size = itanium ? sizeof (oor_ip) : sizeof (oor_brl);
  244. }
  245. /* Rename some of the generic section flags to better document how they
  246. are used here. */
  247. #define skip_relax_pass_0 sec_flg0
  248. #define skip_relax_pass_1 sec_flg1
  249. /* These functions do relaxation for IA-64 ELF. */
  250. static void
  251. elfNN_ia64_update_short_info (asection *sec, bfd_vma offset,
  252. struct elfNN_ia64_link_hash_table *ia64_info)
  253. {
  254. /* Skip ABS and SHF_IA_64_SHORT sections. */
  255. if (sec == bfd_abs_section_ptr
  256. || (sec->flags & SEC_SMALL_DATA) != 0)
  257. return;
  258. if (!ia64_info->min_short_sec)
  259. {
  260. ia64_info->max_short_sec = sec;
  261. ia64_info->max_short_offset = offset;
  262. ia64_info->min_short_sec = sec;
  263. ia64_info->min_short_offset = offset;
  264. }
  265. else if (sec == ia64_info->max_short_sec
  266. && offset > ia64_info->max_short_offset)
  267. ia64_info->max_short_offset = offset;
  268. else if (sec == ia64_info->min_short_sec
  269. && offset < ia64_info->min_short_offset)
  270. ia64_info->min_short_offset = offset;
  271. else if (sec->output_section->vma
  272. > ia64_info->max_short_sec->vma)
  273. {
  274. ia64_info->max_short_sec = sec;
  275. ia64_info->max_short_offset = offset;
  276. }
  277. else if (sec->output_section->vma
  278. < ia64_info->min_short_sec->vma)
  279. {
  280. ia64_info->min_short_sec = sec;
  281. ia64_info->min_short_offset = offset;
  282. }
  283. }
  284. static bool
  285. elfNN_ia64_relax_section (bfd *abfd, asection *sec,
  286. struct bfd_link_info *link_info,
  287. bool *again)
  288. {
  289. struct one_fixup
  290. {
  291. struct one_fixup *next;
  292. asection *tsec;
  293. bfd_vma toff;
  294. bfd_vma trampoff;
  295. };
  296. Elf_Internal_Shdr *symtab_hdr;
  297. Elf_Internal_Rela *internal_relocs;
  298. Elf_Internal_Rela *irel, *irelend;
  299. bfd_byte *contents;
  300. Elf_Internal_Sym *isymbuf = NULL;
  301. struct elfNN_ia64_link_hash_table *ia64_info;
  302. struct one_fixup *fixups = NULL;
  303. bool changed_contents = false;
  304. bool changed_relocs = false;
  305. bool changed_got = false;
  306. bool skip_relax_pass_0 = true;
  307. bool skip_relax_pass_1 = true;
  308. bfd_vma gp = 0;
  309. /* Assume we're not going to change any sizes, and we'll only need
  310. one pass. */
  311. *again = false;
  312. if (bfd_link_relocatable (link_info))
  313. (*link_info->callbacks->einfo)
  314. (_("%P%F: --relax and -r may not be used together\n"));
  315. /* Don't even try to relax for non-ELF outputs. */
  316. if (!is_elf_hash_table (link_info->hash))
  317. return false;
  318. /* Nothing to do if there are no relocations or there is no need for
  319. the current pass. */
  320. if ((sec->flags & SEC_RELOC) == 0
  321. || sec->reloc_count == 0
  322. || (link_info->relax_pass == 0 && sec->skip_relax_pass_0)
  323. || (link_info->relax_pass == 1 && sec->skip_relax_pass_1))
  324. return true;
  325. ia64_info = elfNN_ia64_hash_table (link_info);
  326. if (ia64_info == NULL)
  327. return false;
  328. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  329. /* Load the relocations for this section. */
  330. internal_relocs = (_bfd_elf_link_read_relocs
  331. (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
  332. link_info->keep_memory));
  333. if (internal_relocs == NULL)
  334. return false;
  335. irelend = internal_relocs + sec->reloc_count;
  336. /* Get the section contents. */
  337. if (elf_section_data (sec)->this_hdr.contents != NULL)
  338. contents = elf_section_data (sec)->this_hdr.contents;
  339. else
  340. {
  341. if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  342. goto error_return;
  343. }
  344. for (irel = internal_relocs; irel < irelend; irel++)
  345. {
  346. unsigned long r_type = ELFNN_R_TYPE (irel->r_info);
  347. bfd_vma symaddr, reladdr, trampoff, toff, roff;
  348. asection *tsec;
  349. struct one_fixup *f;
  350. bfd_size_type amt;
  351. bool is_branch;
  352. struct elfNN_ia64_dyn_sym_info *dyn_i;
  353. char symtype;
  354. switch (r_type)
  355. {
  356. case R_IA64_PCREL21B:
  357. case R_IA64_PCREL21BI:
  358. case R_IA64_PCREL21M:
  359. case R_IA64_PCREL21F:
  360. /* In pass 1, all br relaxations are done. We can skip it. */
  361. if (link_info->relax_pass == 1)
  362. continue;
  363. skip_relax_pass_0 = false;
  364. is_branch = true;
  365. break;
  366. case R_IA64_PCREL60B:
  367. /* We can't optimize brl to br in pass 0 since br relaxations
  368. will increase the code size. Defer it to pass 1. */
  369. if (link_info->relax_pass == 0)
  370. {
  371. skip_relax_pass_1 = false;
  372. continue;
  373. }
  374. is_branch = true;
  375. break;
  376. case R_IA64_GPREL22:
  377. /* Update max_short_sec/min_short_sec. */
  378. case R_IA64_LTOFF22X:
  379. case R_IA64_LDXMOV:
  380. /* We can't relax ldx/mov in pass 0 since br relaxations will
  381. increase the code size. Defer it to pass 1. */
  382. if (link_info->relax_pass == 0)
  383. {
  384. skip_relax_pass_1 = false;
  385. continue;
  386. }
  387. is_branch = false;
  388. break;
  389. default:
  390. continue;
  391. }
  392. /* Get the value of the symbol referred to by the reloc. */
  393. if (ELFNN_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  394. {
  395. /* A local symbol. */
  396. Elf_Internal_Sym *isym;
  397. /* Read this BFD's local symbols. */
  398. if (isymbuf == NULL)
  399. {
  400. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  401. if (isymbuf == NULL)
  402. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  403. symtab_hdr->sh_info, 0,
  404. NULL, NULL, NULL);
  405. if (isymbuf == 0)
  406. goto error_return;
  407. }
  408. isym = isymbuf + ELFNN_R_SYM (irel->r_info);
  409. if (isym->st_shndx == SHN_UNDEF)
  410. continue; /* We can't do anything with undefined symbols. */
  411. else if (isym->st_shndx == SHN_ABS)
  412. tsec = bfd_abs_section_ptr;
  413. else if (isym->st_shndx == SHN_COMMON)
  414. tsec = bfd_com_section_ptr;
  415. else if (isym->st_shndx == SHN_IA_64_ANSI_COMMON)
  416. tsec = bfd_com_section_ptr;
  417. else
  418. tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  419. toff = isym->st_value;
  420. dyn_i = get_dyn_sym_info (ia64_info, NULL, abfd, irel, false);
  421. symtype = ELF_ST_TYPE (isym->st_info);
  422. }
  423. else
  424. {
  425. unsigned long indx;
  426. struct elf_link_hash_entry *h;
  427. indx = ELFNN_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  428. h = elf_sym_hashes (abfd)[indx];
  429. BFD_ASSERT (h != NULL);
  430. while (h->root.type == bfd_link_hash_indirect
  431. || h->root.type == bfd_link_hash_warning)
  432. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  433. dyn_i = get_dyn_sym_info (ia64_info, h, abfd, irel, false);
  434. /* For branches to dynamic symbols, we're interested instead
  435. in a branch to the PLT entry. */
  436. if (is_branch && dyn_i && dyn_i->want_plt2)
  437. {
  438. /* Internal branches shouldn't be sent to the PLT.
  439. Leave this for now and we'll give an error later. */
  440. if (r_type != R_IA64_PCREL21B)
  441. continue;
  442. tsec = ia64_info->root.splt;
  443. toff = dyn_i->plt2_offset;
  444. BFD_ASSERT (irel->r_addend == 0);
  445. }
  446. /* Can't do anything else with dynamic symbols. */
  447. else if (elfNN_ia64_dynamic_symbol_p (h, link_info, r_type))
  448. continue;
  449. else
  450. {
  451. /* We can't do anything with undefined symbols. */
  452. if (h->root.type == bfd_link_hash_undefined
  453. || h->root.type == bfd_link_hash_undefweak)
  454. continue;
  455. tsec = h->root.u.def.section;
  456. toff = h->root.u.def.value;
  457. }
  458. symtype = h->type;
  459. }
  460. if (tsec->sec_info_type == SEC_INFO_TYPE_MERGE)
  461. {
  462. /* At this stage in linking, no SEC_MERGE symbol has been
  463. adjusted, so all references to such symbols need to be
  464. passed through _bfd_merged_section_offset. (Later, in
  465. relocate_section, all SEC_MERGE symbols *except* for
  466. section symbols have been adjusted.)
  467. gas may reduce relocations against symbols in SEC_MERGE
  468. sections to a relocation against the section symbol when
  469. the original addend was zero. When the reloc is against
  470. a section symbol we should include the addend in the
  471. offset passed to _bfd_merged_section_offset, since the
  472. location of interest is the original symbol. On the
  473. other hand, an access to "sym+addend" where "sym" is not
  474. a section symbol should not include the addend; Such an
  475. access is presumed to be an offset from "sym"; The
  476. location of interest is just "sym". */
  477. if (symtype == STT_SECTION)
  478. toff += irel->r_addend;
  479. toff = _bfd_merged_section_offset (abfd, &tsec,
  480. elf_section_data (tsec)->sec_info,
  481. toff);
  482. if (symtype != STT_SECTION)
  483. toff += irel->r_addend;
  484. }
  485. else
  486. toff += irel->r_addend;
  487. symaddr = tsec->output_section->vma + tsec->output_offset + toff;
  488. roff = irel->r_offset;
  489. if (is_branch)
  490. {
  491. bfd_signed_vma offset;
  492. reladdr = (sec->output_section->vma
  493. + sec->output_offset
  494. + roff) & (bfd_vma) -4;
  495. /* The .plt section is aligned at 32byte and the .text section
  496. is aligned at 64byte. The .text section is right after the
  497. .plt section. After the first relaxation pass, linker may
  498. increase the gap between the .plt and .text sections up
  499. to 32byte. We assume linker will always insert 32byte
  500. between the .plt and .text sections after the first
  501. relaxation pass. */
  502. if (tsec == ia64_info->root.splt)
  503. offset = -0x1000000 + 32;
  504. else
  505. offset = -0x1000000;
  506. /* If the branch is in range, no need to do anything. */
  507. if ((bfd_signed_vma) (symaddr - reladdr) >= offset
  508. && (bfd_signed_vma) (symaddr - reladdr) <= 0x0FFFFF0)
  509. {
  510. /* If the 60-bit branch is in 21-bit range, optimize it. */
  511. if (r_type == R_IA64_PCREL60B)
  512. {
  513. ia64_elf_relax_brl (contents, roff);
  514. irel->r_info
  515. = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
  516. R_IA64_PCREL21B);
  517. /* If the original relocation offset points to slot
  518. 1, change it to slot 2. */
  519. if ((irel->r_offset & 3) == 1)
  520. irel->r_offset += 1;
  521. changed_contents = true;
  522. changed_relocs = true;
  523. }
  524. continue;
  525. }
  526. else if (r_type == R_IA64_PCREL60B)
  527. continue;
  528. else if (ia64_elf_relax_br (contents, roff))
  529. {
  530. irel->r_info
  531. = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
  532. R_IA64_PCREL60B);
  533. /* Make the relocation offset point to slot 1. */
  534. irel->r_offset = (irel->r_offset & ~((bfd_vma) 0x3)) + 1;
  535. changed_contents = true;
  536. changed_relocs = true;
  537. continue;
  538. }
  539. /* We can't put a trampoline in a .init/.fini section. Issue
  540. an error. */
  541. if (strcmp (sec->output_section->name, ".init") == 0
  542. || strcmp (sec->output_section->name, ".fini") == 0)
  543. {
  544. _bfd_error_handler
  545. /* xgettext:c-format */
  546. (_("%pB: can't relax br at %#" PRIx64 " in section `%pA';"
  547. " please use brl or indirect branch"),
  548. sec->owner, (uint64_t) roff, sec);
  549. bfd_set_error (bfd_error_bad_value);
  550. goto error_return;
  551. }
  552. /* If the branch and target are in the same section, you've
  553. got one honking big section and we can't help you unless
  554. you are branching backwards. You'll get an error message
  555. later. */
  556. if (tsec == sec && toff > roff)
  557. continue;
  558. /* Look for an existing fixup to this address. */
  559. for (f = fixups; f ; f = f->next)
  560. if (f->tsec == tsec && f->toff == toff)
  561. break;
  562. if (f == NULL)
  563. {
  564. /* Two alternatives: If it's a branch to a PLT entry, we can
  565. make a copy of the FULL_PLT entry. Otherwise, we'll have
  566. to use a `brl' insn to get where we're going. */
  567. size_t size;
  568. if (tsec == ia64_info->root.splt)
  569. size = sizeof (plt_full_entry);
  570. else
  571. size = oor_branch_size;
  572. /* Resize the current section to make room for the new branch. */
  573. trampoff = (sec->size + 15) & (bfd_vma) -16;
  574. /* If trampoline is out of range, there is nothing we
  575. can do. */
  576. offset = trampoff - (roff & (bfd_vma) -4);
  577. if (offset < -0x1000000 || offset > 0x0FFFFF0)
  578. continue;
  579. amt = trampoff + size;
  580. contents = (bfd_byte *) bfd_realloc (contents, amt);
  581. if (contents == NULL)
  582. goto error_return;
  583. sec->size = amt;
  584. if (tsec == ia64_info->root.splt)
  585. {
  586. memcpy (contents + trampoff, plt_full_entry, size);
  587. /* Hijack the old relocation for use as the PLTOFF reloc. */
  588. irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
  589. R_IA64_PLTOFF22);
  590. irel->r_offset = trampoff;
  591. }
  592. else
  593. {
  594. if (size == sizeof (oor_ip))
  595. {
  596. memcpy (contents + trampoff, oor_ip, size);
  597. irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
  598. R_IA64_PCREL64I);
  599. irel->r_addend -= 16;
  600. irel->r_offset = trampoff + 2;
  601. }
  602. else
  603. {
  604. memcpy (contents + trampoff, oor_brl, size);
  605. irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
  606. R_IA64_PCREL60B);
  607. irel->r_offset = trampoff + 2;
  608. }
  609. }
  610. /* Record the fixup so we don't do it again this section. */
  611. f = (struct one_fixup *)
  612. bfd_malloc ((bfd_size_type) sizeof (*f));
  613. f->next = fixups;
  614. f->tsec = tsec;
  615. f->toff = toff;
  616. f->trampoff = trampoff;
  617. fixups = f;
  618. }
  619. else
  620. {
  621. /* If trampoline is out of range, there is nothing we
  622. can do. */
  623. offset = f->trampoff - (roff & (bfd_vma) -4);
  624. if (offset < -0x1000000 || offset > 0x0FFFFF0)
  625. continue;
  626. /* Nop out the reloc, since we're finalizing things here. */
  627. irel->r_info = ELFNN_R_INFO (0, R_IA64_NONE);
  628. }
  629. /* Fix up the existing branch to hit the trampoline. */
  630. if (ia64_elf_install_value (contents + roff, offset, r_type)
  631. != bfd_reloc_ok)
  632. goto error_return;
  633. changed_contents = true;
  634. changed_relocs = true;
  635. }
  636. else
  637. {
  638. /* Fetch the gp. */
  639. if (gp == 0)
  640. {
  641. bfd *obfd = sec->output_section->owner;
  642. gp = _bfd_get_gp_value (obfd);
  643. if (gp == 0)
  644. {
  645. if (!elfNN_ia64_choose_gp (obfd, link_info, false))
  646. goto error_return;
  647. gp = _bfd_get_gp_value (obfd);
  648. }
  649. }
  650. /* If the data is out of range, do nothing. */
  651. if ((bfd_signed_vma) (symaddr - gp) >= 0x200000
  652. ||(bfd_signed_vma) (symaddr - gp) < -0x200000)
  653. continue;
  654. if (r_type == R_IA64_GPREL22)
  655. elfNN_ia64_update_short_info (tsec->output_section,
  656. tsec->output_offset + toff,
  657. ia64_info);
  658. else if (r_type == R_IA64_LTOFF22X)
  659. {
  660. irel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (irel->r_info),
  661. R_IA64_GPREL22);
  662. changed_relocs = true;
  663. if (dyn_i->want_gotx)
  664. {
  665. dyn_i->want_gotx = 0;
  666. changed_got |= !dyn_i->want_got;
  667. }
  668. elfNN_ia64_update_short_info (tsec->output_section,
  669. tsec->output_offset + toff,
  670. ia64_info);
  671. }
  672. else
  673. {
  674. ia64_elf_relax_ldxmov (contents, roff);
  675. irel->r_info = ELFNN_R_INFO (0, R_IA64_NONE);
  676. changed_contents = true;
  677. changed_relocs = true;
  678. }
  679. }
  680. }
  681. /* ??? If we created fixups, this may push the code segment large
  682. enough that the data segment moves, which will change the GP.
  683. Reset the GP so that we re-calculate next round. We need to
  684. do this at the _beginning_ of the next round; now will not do. */
  685. /* Clean up and go home. */
  686. while (fixups)
  687. {
  688. struct one_fixup *f = fixups;
  689. fixups = fixups->next;
  690. free (f);
  691. }
  692. if (isymbuf != NULL
  693. && symtab_hdr->contents != (unsigned char *) isymbuf)
  694. {
  695. if (! link_info->keep_memory)
  696. free (isymbuf);
  697. else
  698. {
  699. /* Cache the symbols for elf_link_input_bfd. */
  700. symtab_hdr->contents = (unsigned char *) isymbuf;
  701. }
  702. }
  703. if (contents != NULL
  704. && elf_section_data (sec)->this_hdr.contents != contents)
  705. {
  706. if (!changed_contents && !link_info->keep_memory)
  707. free (contents);
  708. else
  709. {
  710. /* Cache the section contents for elf_link_input_bfd. */
  711. elf_section_data (sec)->this_hdr.contents = contents;
  712. }
  713. }
  714. if (elf_section_data (sec)->relocs != internal_relocs)
  715. {
  716. if (!changed_relocs)
  717. free (internal_relocs);
  718. else
  719. elf_section_data (sec)->relocs = internal_relocs;
  720. }
  721. if (changed_got)
  722. {
  723. struct elfNN_ia64_allocate_data data;
  724. data.info = link_info;
  725. data.ofs = 0;
  726. ia64_info->self_dtpmod_offset = (bfd_vma) -1;
  727. elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
  728. elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
  729. elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
  730. ia64_info->root.sgot->size = data.ofs;
  731. if (ia64_info->root.dynamic_sections_created
  732. && ia64_info->root.srelgot != NULL)
  733. {
  734. /* Resize .rela.got. */
  735. ia64_info->root.srelgot->size = 0;
  736. if (bfd_link_pic (link_info)
  737. && ia64_info->self_dtpmod_offset != (bfd_vma) -1)
  738. ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
  739. data.only_got = true;
  740. elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries,
  741. &data);
  742. }
  743. }
  744. if (link_info->relax_pass == 0)
  745. {
  746. /* Pass 0 is only needed to relax br. */
  747. sec->skip_relax_pass_0 = skip_relax_pass_0;
  748. sec->skip_relax_pass_1 = skip_relax_pass_1;
  749. }
  750. *again = changed_contents || changed_relocs;
  751. return true;
  752. error_return:
  753. if ((unsigned char *) isymbuf != symtab_hdr->contents)
  754. free (isymbuf);
  755. if (elf_section_data (sec)->this_hdr.contents != contents)
  756. free (contents);
  757. if (elf_section_data (sec)->relocs != internal_relocs)
  758. free (internal_relocs);
  759. return false;
  760. }
  761. #undef skip_relax_pass_0
  762. #undef skip_relax_pass_1
  763. /* Return TRUE if NAME is an unwind table section name. */
  764. static inline bool
  765. is_unwind_section_name (bfd *abfd, const char *name)
  766. {
  767. if (elfNN_ia64_hpux_vec (abfd->xvec)
  768. && !strcmp (name, ELF_STRING_ia64_unwind_hdr))
  769. return false;
  770. return ((startswith (name, ELF_STRING_ia64_unwind)
  771. && ! startswith (name, ELF_STRING_ia64_unwind_info))
  772. || startswith (name, ELF_STRING_ia64_unwind_once));
  773. }
  774. /* Handle an IA-64 specific section when reading an object file. This
  775. is called when bfd_section_from_shdr finds a section with an unknown
  776. type. */
  777. static bool
  778. elfNN_ia64_section_from_shdr (bfd *abfd,
  779. Elf_Internal_Shdr *hdr,
  780. const char *name,
  781. int shindex)
  782. {
  783. /* There ought to be a place to keep ELF backend specific flags, but
  784. at the moment there isn't one. We just keep track of the
  785. sections by their name, instead. Fortunately, the ABI gives
  786. suggested names for all the MIPS specific sections, so we will
  787. probably get away with this. */
  788. switch (hdr->sh_type)
  789. {
  790. case SHT_IA_64_UNWIND:
  791. case SHT_IA_64_HP_OPT_ANOT:
  792. break;
  793. case SHT_IA_64_EXT:
  794. if (strcmp (name, ELF_STRING_ia64_archext) != 0)
  795. return false;
  796. break;
  797. default:
  798. return false;
  799. }
  800. if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
  801. return false;
  802. return true;
  803. }
  804. /* Convert IA-64 specific section flags to bfd internal section flags. */
  805. /* ??? There is no bfd internal flag equivalent to the SHF_IA_64_NORECOV
  806. flag. */
  807. static bool
  808. elfNN_ia64_section_flags (const Elf_Internal_Shdr *hdr)
  809. {
  810. if (hdr->sh_flags & SHF_IA_64_SHORT)
  811. hdr->bfd_section->flags |= SEC_SMALL_DATA;
  812. return true;
  813. }
  814. /* Set the correct type for an IA-64 ELF section. We do this by the
  815. section name, which is a hack, but ought to work. */
  816. static bool
  817. elfNN_ia64_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr,
  818. asection *sec)
  819. {
  820. const char *name;
  821. name = bfd_section_name (sec);
  822. if (is_unwind_section_name (abfd, name))
  823. {
  824. /* We don't have the sections numbered at this point, so sh_info
  825. is set later, in elfNN_ia64_final_write_processing. */
  826. hdr->sh_type = SHT_IA_64_UNWIND;
  827. hdr->sh_flags |= SHF_LINK_ORDER;
  828. }
  829. else if (strcmp (name, ELF_STRING_ia64_archext) == 0)
  830. hdr->sh_type = SHT_IA_64_EXT;
  831. else if (strcmp (name, ".HP.opt_annot") == 0)
  832. hdr->sh_type = SHT_IA_64_HP_OPT_ANOT;
  833. else if (strcmp (name, ".reloc") == 0)
  834. /* This is an ugly, but unfortunately necessary hack that is
  835. needed when producing EFI binaries on IA-64. It tells
  836. elf.c:elf_fake_sections() not to consider ".reloc" as a section
  837. containing ELF relocation info. We need this hack in order to
  838. be able to generate ELF binaries that can be translated into
  839. EFI applications (which are essentially COFF objects). Those
  840. files contain a COFF ".reloc" section inside an ELFNN object,
  841. which would normally cause BFD to segfault because it would
  842. attempt to interpret this section as containing relocation
  843. entries for section "oc". With this hack enabled, ".reloc"
  844. will be treated as a normal data section, which will avoid the
  845. segfault. However, you won't be able to create an ELFNN binary
  846. with a section named "oc" that needs relocations, but that's
  847. the kind of ugly side-effects you get when detecting section
  848. types based on their names... In practice, this limitation is
  849. unlikely to bite. */
  850. hdr->sh_type = SHT_PROGBITS;
  851. if (sec->flags & SEC_SMALL_DATA)
  852. hdr->sh_flags |= SHF_IA_64_SHORT;
  853. /* Some HP linkers look for the SHF_IA_64_HP_TLS flag instead of SHF_TLS. */
  854. if (elfNN_ia64_hpux_vec (abfd->xvec) && (sec->flags & SHF_TLS))
  855. hdr->sh_flags |= SHF_IA_64_HP_TLS;
  856. return true;
  857. }
  858. /* The final processing done just before writing out an IA-64 ELF
  859. object file. */
  860. static bool
  861. elfNN_ia64_final_write_processing (bfd *abfd)
  862. {
  863. Elf_Internal_Shdr *hdr;
  864. asection *s;
  865. for (s = abfd->sections; s; s = s->next)
  866. {
  867. hdr = &elf_section_data (s)->this_hdr;
  868. switch (hdr->sh_type)
  869. {
  870. case SHT_IA_64_UNWIND:
  871. /* The IA-64 processor-specific ABI requires setting sh_link
  872. to the unwind section, whereas HP-UX requires sh_info to
  873. do so. For maximum compatibility, we'll set both for
  874. now... */
  875. hdr->sh_info = hdr->sh_link;
  876. break;
  877. }
  878. }
  879. if (! elf_flags_init (abfd))
  880. {
  881. unsigned long flags = 0;
  882. if (abfd->xvec->byteorder == BFD_ENDIAN_BIG)
  883. flags |= EF_IA_64_BE;
  884. if (bfd_get_mach (abfd) == bfd_mach_ia64_elf64)
  885. flags |= EF_IA_64_ABI64;
  886. elf_elfheader(abfd)->e_flags = flags;
  887. elf_flags_init (abfd) = true;
  888. }
  889. return _bfd_elf_final_write_processing (abfd);
  890. }
  891. /* Hook called by the linker routine which adds symbols from an object
  892. file. We use it to put .comm items in .sbss, and not .bss. */
  893. static bool
  894. elfNN_ia64_add_symbol_hook (bfd *abfd,
  895. struct bfd_link_info *info,
  896. Elf_Internal_Sym *sym,
  897. const char **namep ATTRIBUTE_UNUSED,
  898. flagword *flagsp ATTRIBUTE_UNUSED,
  899. asection **secp,
  900. bfd_vma *valp)
  901. {
  902. if (sym->st_shndx == SHN_COMMON
  903. && !bfd_link_relocatable (info)
  904. && sym->st_size <= elf_gp_size (abfd))
  905. {
  906. /* Common symbols less than or equal to -G nn bytes are
  907. automatically put into .sbss. */
  908. asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
  909. if (scomm == NULL)
  910. {
  911. scomm = bfd_make_section_with_flags (abfd, ".scommon",
  912. (SEC_ALLOC
  913. | SEC_IS_COMMON
  914. | SEC_SMALL_DATA
  915. | SEC_LINKER_CREATED));
  916. if (scomm == NULL)
  917. return false;
  918. }
  919. *secp = scomm;
  920. *valp = sym->st_size;
  921. }
  922. return true;
  923. }
  924. /* Return the number of additional phdrs we will need. */
  925. static int
  926. elfNN_ia64_additional_program_headers (bfd *abfd,
  927. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  928. {
  929. asection *s;
  930. int ret = 0;
  931. /* See if we need a PT_IA_64_ARCHEXT segment. */
  932. s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
  933. if (s && (s->flags & SEC_LOAD))
  934. ++ret;
  935. /* Count how many PT_IA_64_UNWIND segments we need. */
  936. for (s = abfd->sections; s; s = s->next)
  937. if (is_unwind_section_name (abfd, s->name) && (s->flags & SEC_LOAD))
  938. ++ret;
  939. return ret;
  940. }
  941. static bool
  942. elfNN_ia64_modify_segment_map (bfd *abfd,
  943. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  944. {
  945. struct elf_segment_map *m, **pm;
  946. Elf_Internal_Shdr *hdr;
  947. asection *s;
  948. /* If we need a PT_IA_64_ARCHEXT segment, it must come before
  949. all PT_LOAD segments. */
  950. s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_archext);
  951. if (s && (s->flags & SEC_LOAD))
  952. {
  953. for (m = elf_seg_map (abfd); m != NULL; m = m->next)
  954. if (m->p_type == PT_IA_64_ARCHEXT)
  955. break;
  956. if (m == NULL)
  957. {
  958. m = ((struct elf_segment_map *)
  959. bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
  960. if (m == NULL)
  961. return false;
  962. m->p_type = PT_IA_64_ARCHEXT;
  963. m->count = 1;
  964. m->sections[0] = s;
  965. /* We want to put it after the PHDR and INTERP segments. */
  966. pm = &elf_seg_map (abfd);
  967. while (*pm != NULL
  968. && ((*pm)->p_type == PT_PHDR
  969. || (*pm)->p_type == PT_INTERP))
  970. pm = &(*pm)->next;
  971. m->next = *pm;
  972. *pm = m;
  973. }
  974. }
  975. /* Install PT_IA_64_UNWIND segments, if needed. */
  976. for (s = abfd->sections; s; s = s->next)
  977. {
  978. hdr = &elf_section_data (s)->this_hdr;
  979. if (hdr->sh_type != SHT_IA_64_UNWIND)
  980. continue;
  981. if (s && (s->flags & SEC_LOAD))
  982. {
  983. for (m = elf_seg_map (abfd); m != NULL; m = m->next)
  984. if (m->p_type == PT_IA_64_UNWIND)
  985. {
  986. int i;
  987. /* Look through all sections in the unwind segment
  988. for a match since there may be multiple sections
  989. to a segment. */
  990. for (i = m->count - 1; i >= 0; --i)
  991. if (m->sections[i] == s)
  992. break;
  993. if (i >= 0)
  994. break;
  995. }
  996. if (m == NULL)
  997. {
  998. m = ((struct elf_segment_map *)
  999. bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
  1000. if (m == NULL)
  1001. return false;
  1002. m->p_type = PT_IA_64_UNWIND;
  1003. m->count = 1;
  1004. m->sections[0] = s;
  1005. m->next = NULL;
  1006. /* We want to put it last. */
  1007. pm = &elf_seg_map (abfd);
  1008. while (*pm != NULL)
  1009. pm = &(*pm)->next;
  1010. *pm = m;
  1011. }
  1012. }
  1013. }
  1014. return true;
  1015. }
  1016. /* Turn on PF_IA_64_NORECOV if needed. This involves traversing all of
  1017. the input sections for each output section in the segment and testing
  1018. for SHF_IA_64_NORECOV on each. */
  1019. static bool
  1020. elfNN_ia64_modify_headers (bfd *abfd, struct bfd_link_info *info)
  1021. {
  1022. struct elf_obj_tdata *tdata = elf_tdata (abfd);
  1023. struct elf_segment_map *m;
  1024. Elf_Internal_Phdr *p;
  1025. for (p = tdata->phdr, m = elf_seg_map (abfd); m != NULL; m = m->next, p++)
  1026. if (m->p_type == PT_LOAD)
  1027. {
  1028. int i;
  1029. for (i = m->count - 1; i >= 0; --i)
  1030. {
  1031. struct bfd_link_order *order = m->sections[i]->map_head.link_order;
  1032. while (order != NULL)
  1033. {
  1034. if (order->type == bfd_indirect_link_order)
  1035. {
  1036. asection *is = order->u.indirect.section;
  1037. bfd_vma flags = elf_section_data(is)->this_hdr.sh_flags;
  1038. if (flags & SHF_IA_64_NORECOV)
  1039. {
  1040. p->p_flags |= PF_IA_64_NORECOV;
  1041. goto found;
  1042. }
  1043. }
  1044. order = order->next;
  1045. }
  1046. }
  1047. found:;
  1048. }
  1049. return _bfd_elf_modify_headers (abfd, info);
  1050. }
  1051. /* According to the Tahoe assembler spec, all labels starting with a
  1052. '.' are local. */
  1053. static bool
  1054. elfNN_ia64_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
  1055. const char *name)
  1056. {
  1057. return name[0] == '.';
  1058. }
  1059. /* Should we do dynamic things to this symbol? */
  1060. static bool
  1061. elfNN_ia64_dynamic_symbol_p (struct elf_link_hash_entry *h,
  1062. struct bfd_link_info *info, int r_type)
  1063. {
  1064. bool ignore_protected
  1065. = ((r_type & 0xf8) == 0x40 /* FPTR relocs */
  1066. || (r_type & 0xf8) == 0x50); /* LTOFF_FPTR relocs */
  1067. return _bfd_elf_dynamic_symbol_p (h, info, ignore_protected);
  1068. }
  1069. static struct bfd_hash_entry*
  1070. elfNN_ia64_new_elf_hash_entry (struct bfd_hash_entry *entry,
  1071. struct bfd_hash_table *table,
  1072. const char *string)
  1073. {
  1074. struct elfNN_ia64_link_hash_entry *ret;
  1075. ret = (struct elfNN_ia64_link_hash_entry *) entry;
  1076. /* Allocate the structure if it has not already been allocated by a
  1077. subclass. */
  1078. if (!ret)
  1079. ret = bfd_hash_allocate (table, sizeof (*ret));
  1080. if (!ret)
  1081. return 0;
  1082. /* Call the allocation method of the superclass. */
  1083. ret = ((struct elfNN_ia64_link_hash_entry *)
  1084. _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  1085. table, string));
  1086. ret->info = NULL;
  1087. ret->count = 0;
  1088. ret->sorted_count = 0;
  1089. ret->size = 0;
  1090. return (struct bfd_hash_entry *) ret;
  1091. }
  1092. static void
  1093. elfNN_ia64_hash_copy_indirect (struct bfd_link_info *info,
  1094. struct elf_link_hash_entry *xdir,
  1095. struct elf_link_hash_entry *xind)
  1096. {
  1097. struct elfNN_ia64_link_hash_entry *dir, *ind;
  1098. dir = (struct elfNN_ia64_link_hash_entry *) xdir;
  1099. ind = (struct elfNN_ia64_link_hash_entry *) xind;
  1100. /* Copy down any references that we may have already seen to the
  1101. symbol which just became indirect. */
  1102. if (dir->root.versioned != versioned_hidden)
  1103. dir->root.ref_dynamic |= ind->root.ref_dynamic;
  1104. dir->root.ref_regular |= ind->root.ref_regular;
  1105. dir->root.ref_regular_nonweak |= ind->root.ref_regular_nonweak;
  1106. dir->root.needs_plt |= ind->root.needs_plt;
  1107. if (ind->root.root.type != bfd_link_hash_indirect)
  1108. return;
  1109. /* Copy over the got and plt data. This would have been done
  1110. by check_relocs. */
  1111. if (ind->info != NULL)
  1112. {
  1113. struct elfNN_ia64_dyn_sym_info *dyn_i;
  1114. unsigned int count;
  1115. free (dir->info);
  1116. dir->info = ind->info;
  1117. dir->count = ind->count;
  1118. dir->sorted_count = ind->sorted_count;
  1119. dir->size = ind->size;
  1120. ind->info = NULL;
  1121. ind->count = 0;
  1122. ind->sorted_count = 0;
  1123. ind->size = 0;
  1124. /* Fix up the dyn_sym_info pointers to the global symbol. */
  1125. for (count = dir->count, dyn_i = dir->info;
  1126. count != 0;
  1127. count--, dyn_i++)
  1128. dyn_i->h = &dir->root;
  1129. }
  1130. /* Copy over the dynindx. */
  1131. if (ind->root.dynindx != -1)
  1132. {
  1133. if (dir->root.dynindx != -1)
  1134. _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
  1135. dir->root.dynstr_index);
  1136. dir->root.dynindx = ind->root.dynindx;
  1137. dir->root.dynstr_index = ind->root.dynstr_index;
  1138. ind->root.dynindx = -1;
  1139. ind->root.dynstr_index = 0;
  1140. }
  1141. }
  1142. static void
  1143. elfNN_ia64_hash_hide_symbol (struct bfd_link_info *info,
  1144. struct elf_link_hash_entry *xh,
  1145. bool force_local)
  1146. {
  1147. struct elfNN_ia64_link_hash_entry *h;
  1148. struct elfNN_ia64_dyn_sym_info *dyn_i;
  1149. unsigned int count;
  1150. h = (struct elfNN_ia64_link_hash_entry *)xh;
  1151. _bfd_elf_link_hash_hide_symbol (info, &h->root, force_local);
  1152. for (count = h->count, dyn_i = h->info;
  1153. count != 0;
  1154. count--, dyn_i++)
  1155. {
  1156. dyn_i->want_plt2 = 0;
  1157. dyn_i->want_plt = 0;
  1158. }
  1159. }
  1160. /* Compute a hash of a local hash entry. */
  1161. static hashval_t
  1162. elfNN_ia64_local_htab_hash (const void *ptr)
  1163. {
  1164. struct elfNN_ia64_local_hash_entry *entry
  1165. = (struct elfNN_ia64_local_hash_entry *) ptr;
  1166. return ELF_LOCAL_SYMBOL_HASH (entry->id, entry->r_sym);
  1167. }
  1168. /* Compare local hash entries. */
  1169. static int
  1170. elfNN_ia64_local_htab_eq (const void *ptr1, const void *ptr2)
  1171. {
  1172. struct elfNN_ia64_local_hash_entry *entry1
  1173. = (struct elfNN_ia64_local_hash_entry *) ptr1;
  1174. struct elfNN_ia64_local_hash_entry *entry2
  1175. = (struct elfNN_ia64_local_hash_entry *) ptr2;
  1176. return entry1->id == entry2->id && entry1->r_sym == entry2->r_sym;
  1177. }
  1178. /* Free the global elfNN_ia64_dyn_sym_info array. */
  1179. static bool
  1180. elfNN_ia64_global_dyn_info_free (struct elf_link_hash_entry *xentry,
  1181. void *unused ATTRIBUTE_UNUSED)
  1182. {
  1183. struct elfNN_ia64_link_hash_entry *entry
  1184. = (struct elfNN_ia64_link_hash_entry *) xentry;
  1185. free (entry->info);
  1186. entry->info = NULL;
  1187. entry->count = 0;
  1188. entry->sorted_count = 0;
  1189. entry->size = 0;
  1190. return true;
  1191. }
  1192. /* Free the local elfNN_ia64_dyn_sym_info array. */
  1193. static int
  1194. elfNN_ia64_local_dyn_info_free (void **slot,
  1195. void * unused ATTRIBUTE_UNUSED)
  1196. {
  1197. struct elfNN_ia64_local_hash_entry *entry
  1198. = (struct elfNN_ia64_local_hash_entry *) *slot;
  1199. free (entry->info);
  1200. entry->info = NULL;
  1201. entry->count = 0;
  1202. entry->sorted_count = 0;
  1203. entry->size = 0;
  1204. return true;
  1205. }
  1206. /* Destroy IA-64 linker hash table. */
  1207. static void
  1208. elfNN_ia64_link_hash_table_free (bfd *obfd)
  1209. {
  1210. struct elfNN_ia64_link_hash_table *ia64_info
  1211. = (struct elfNN_ia64_link_hash_table *) obfd->link.hash;
  1212. if (ia64_info->loc_hash_table)
  1213. {
  1214. htab_traverse (ia64_info->loc_hash_table,
  1215. elfNN_ia64_local_dyn_info_free, NULL);
  1216. htab_delete (ia64_info->loc_hash_table);
  1217. }
  1218. if (ia64_info->loc_hash_memory)
  1219. objalloc_free ((struct objalloc *) ia64_info->loc_hash_memory);
  1220. elf_link_hash_traverse (&ia64_info->root,
  1221. elfNN_ia64_global_dyn_info_free, NULL);
  1222. _bfd_elf_link_hash_table_free (obfd);
  1223. }
  1224. /* Create the derived linker hash table. The IA-64 ELF port uses this
  1225. derived hash table to keep information specific to the IA-64 ElF
  1226. linker (without using static variables). */
  1227. static struct bfd_link_hash_table *
  1228. elfNN_ia64_hash_table_create (bfd *abfd)
  1229. {
  1230. struct elfNN_ia64_link_hash_table *ret;
  1231. ret = bfd_zmalloc ((bfd_size_type) sizeof (*ret));
  1232. if (!ret)
  1233. return NULL;
  1234. if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
  1235. elfNN_ia64_new_elf_hash_entry,
  1236. sizeof (struct elfNN_ia64_link_hash_entry),
  1237. IA64_ELF_DATA))
  1238. {
  1239. free (ret);
  1240. return NULL;
  1241. }
  1242. ret->loc_hash_table = htab_try_create (1024, elfNN_ia64_local_htab_hash,
  1243. elfNN_ia64_local_htab_eq, NULL);
  1244. ret->loc_hash_memory = objalloc_create ();
  1245. if (!ret->loc_hash_table || !ret->loc_hash_memory)
  1246. {
  1247. elfNN_ia64_link_hash_table_free (abfd);
  1248. return NULL;
  1249. }
  1250. ret->root.root.hash_table_free = elfNN_ia64_link_hash_table_free;
  1251. ret->root.dt_pltgot_required = true;
  1252. return &ret->root.root;
  1253. }
  1254. /* Traverse both local and global hash tables. */
  1255. struct elfNN_ia64_dyn_sym_traverse_data
  1256. {
  1257. bool (*func) (struct elfNN_ia64_dyn_sym_info *, void *);
  1258. void * data;
  1259. };
  1260. static bool
  1261. elfNN_ia64_global_dyn_sym_thunk (struct elf_link_hash_entry *xentry,
  1262. void * xdata)
  1263. {
  1264. struct elfNN_ia64_link_hash_entry *entry
  1265. = (struct elfNN_ia64_link_hash_entry *) xentry;
  1266. struct elfNN_ia64_dyn_sym_traverse_data *data
  1267. = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
  1268. struct elfNN_ia64_dyn_sym_info *dyn_i;
  1269. unsigned int count;
  1270. for (count = entry->count, dyn_i = entry->info;
  1271. count != 0;
  1272. count--, dyn_i++)
  1273. if (! (*data->func) (dyn_i, data->data))
  1274. return false;
  1275. return true;
  1276. }
  1277. static int
  1278. elfNN_ia64_local_dyn_sym_thunk (void **slot, void * xdata)
  1279. {
  1280. struct elfNN_ia64_local_hash_entry *entry
  1281. = (struct elfNN_ia64_local_hash_entry *) *slot;
  1282. struct elfNN_ia64_dyn_sym_traverse_data *data
  1283. = (struct elfNN_ia64_dyn_sym_traverse_data *) xdata;
  1284. struct elfNN_ia64_dyn_sym_info *dyn_i;
  1285. unsigned int count;
  1286. for (count = entry->count, dyn_i = entry->info;
  1287. count != 0;
  1288. count--, dyn_i++)
  1289. if (! (*data->func) (dyn_i, data->data))
  1290. return false;
  1291. return true;
  1292. }
  1293. static void
  1294. elfNN_ia64_dyn_sym_traverse (struct elfNN_ia64_link_hash_table *ia64_info,
  1295. bool (*func) (struct elfNN_ia64_dyn_sym_info *,
  1296. void *),
  1297. void * data)
  1298. {
  1299. struct elfNN_ia64_dyn_sym_traverse_data xdata;
  1300. xdata.func = func;
  1301. xdata.data = data;
  1302. elf_link_hash_traverse (&ia64_info->root,
  1303. elfNN_ia64_global_dyn_sym_thunk, &xdata);
  1304. htab_traverse (ia64_info->loc_hash_table,
  1305. elfNN_ia64_local_dyn_sym_thunk, &xdata);
  1306. }
  1307. static bool
  1308. elfNN_ia64_create_dynamic_sections (bfd *abfd,
  1309. struct bfd_link_info *info)
  1310. {
  1311. struct elfNN_ia64_link_hash_table *ia64_info;
  1312. asection *s;
  1313. if (! _bfd_elf_create_dynamic_sections (abfd, info))
  1314. return false;
  1315. ia64_info = elfNN_ia64_hash_table (info);
  1316. if (ia64_info == NULL)
  1317. return false;
  1318. {
  1319. flagword flags = bfd_section_flags (ia64_info->root.sgot);
  1320. bfd_set_section_flags (ia64_info->root.sgot, SEC_SMALL_DATA | flags);
  1321. /* The .got section is always aligned at 8 bytes. */
  1322. if (!bfd_set_section_alignment (ia64_info->root.sgot, 3))
  1323. return false;
  1324. }
  1325. if (!get_pltoff (abfd, info, ia64_info))
  1326. return false;
  1327. s = bfd_make_section_anyway_with_flags (abfd, ".rela.IA_64.pltoff",
  1328. (SEC_ALLOC | SEC_LOAD
  1329. | SEC_HAS_CONTENTS
  1330. | SEC_IN_MEMORY
  1331. | SEC_LINKER_CREATED
  1332. | SEC_READONLY));
  1333. if (s == NULL
  1334. || !bfd_set_section_alignment (s, LOG_SECTION_ALIGN))
  1335. return false;
  1336. ia64_info->rel_pltoff_sec = s;
  1337. return true;
  1338. }
  1339. /* Find and/or create a hash entry for local symbol. */
  1340. static struct elfNN_ia64_local_hash_entry *
  1341. get_local_sym_hash (struct elfNN_ia64_link_hash_table *ia64_info,
  1342. bfd *abfd, const Elf_Internal_Rela *rel,
  1343. bool create)
  1344. {
  1345. struct elfNN_ia64_local_hash_entry e, *ret;
  1346. asection *sec = abfd->sections;
  1347. hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
  1348. ELFNN_R_SYM (rel->r_info));
  1349. void **slot;
  1350. e.id = sec->id;
  1351. e.r_sym = ELFNN_R_SYM (rel->r_info);
  1352. slot = htab_find_slot_with_hash (ia64_info->loc_hash_table, &e, h,
  1353. create ? INSERT : NO_INSERT);
  1354. if (!slot)
  1355. return NULL;
  1356. if (*slot)
  1357. return (struct elfNN_ia64_local_hash_entry *) *slot;
  1358. ret = (struct elfNN_ia64_local_hash_entry *)
  1359. objalloc_alloc ((struct objalloc *) ia64_info->loc_hash_memory,
  1360. sizeof (struct elfNN_ia64_local_hash_entry));
  1361. if (ret)
  1362. {
  1363. memset (ret, 0, sizeof (*ret));
  1364. ret->id = sec->id;
  1365. ret->r_sym = ELFNN_R_SYM (rel->r_info);
  1366. *slot = ret;
  1367. }
  1368. return ret;
  1369. }
  1370. /* Used to sort elfNN_ia64_dyn_sym_info array. */
  1371. static int
  1372. addend_compare (const void *xp, const void *yp)
  1373. {
  1374. const struct elfNN_ia64_dyn_sym_info *x
  1375. = (const struct elfNN_ia64_dyn_sym_info *) xp;
  1376. const struct elfNN_ia64_dyn_sym_info *y
  1377. = (const struct elfNN_ia64_dyn_sym_info *) yp;
  1378. return x->addend < y->addend ? -1 : x->addend > y->addend ? 1 : 0;
  1379. }
  1380. /* Sort elfNN_ia64_dyn_sym_info array and remove duplicates. */
  1381. static unsigned int
  1382. sort_dyn_sym_info (struct elfNN_ia64_dyn_sym_info *info,
  1383. unsigned int count)
  1384. {
  1385. bfd_vma curr, prev, got_offset;
  1386. unsigned int i, kept, dupes, diff, dest, src, len;
  1387. qsort (info, count, sizeof (*info), addend_compare);
  1388. /* Find the first duplicate. */
  1389. prev = info [0].addend;
  1390. got_offset = info [0].got_offset;
  1391. for (i = 1; i < count; i++)
  1392. {
  1393. curr = info [i].addend;
  1394. if (curr == prev)
  1395. {
  1396. /* For duplicates, make sure that GOT_OFFSET is valid. */
  1397. if (got_offset == (bfd_vma) -1)
  1398. got_offset = info [i].got_offset;
  1399. break;
  1400. }
  1401. got_offset = info [i].got_offset;
  1402. prev = curr;
  1403. }
  1404. /* We may move a block of elements to here. */
  1405. dest = i++;
  1406. /* Remove duplicates. */
  1407. if (i < count)
  1408. {
  1409. while (i < count)
  1410. {
  1411. /* For duplicates, make sure that the kept one has a valid
  1412. got_offset. */
  1413. kept = dest - 1;
  1414. if (got_offset != (bfd_vma) -1)
  1415. info [kept].got_offset = got_offset;
  1416. curr = info [i].addend;
  1417. got_offset = info [i].got_offset;
  1418. /* Move a block of elements whose first one is different from
  1419. the previous. */
  1420. if (curr == prev)
  1421. {
  1422. for (src = i + 1; src < count; src++)
  1423. {
  1424. if (info [src].addend != curr)
  1425. break;
  1426. /* For duplicates, make sure that GOT_OFFSET is
  1427. valid. */
  1428. if (got_offset == (bfd_vma) -1)
  1429. got_offset = info [src].got_offset;
  1430. }
  1431. /* Make sure that the kept one has a valid got_offset. */
  1432. if (got_offset != (bfd_vma) -1)
  1433. info [kept].got_offset = got_offset;
  1434. }
  1435. else
  1436. src = i;
  1437. if (src >= count)
  1438. break;
  1439. /* Find the next duplicate. SRC will be kept. */
  1440. prev = info [src].addend;
  1441. got_offset = info [src].got_offset;
  1442. for (dupes = src + 1; dupes < count; dupes ++)
  1443. {
  1444. curr = info [dupes].addend;
  1445. if (curr == prev)
  1446. {
  1447. /* Make sure that got_offset is valid. */
  1448. if (got_offset == (bfd_vma) -1)
  1449. got_offset = info [dupes].got_offset;
  1450. /* For duplicates, make sure that the kept one has
  1451. a valid got_offset. */
  1452. if (got_offset != (bfd_vma) -1)
  1453. info [dupes - 1].got_offset = got_offset;
  1454. break;
  1455. }
  1456. got_offset = info [dupes].got_offset;
  1457. prev = curr;
  1458. }
  1459. /* How much to move. */
  1460. len = dupes - src;
  1461. i = dupes + 1;
  1462. if (len == 1 && dupes < count)
  1463. {
  1464. /* If we only move 1 element, we combine it with the next
  1465. one. There must be at least a duplicate. Find the
  1466. next different one. */
  1467. for (diff = dupes + 1, src++; diff < count; diff++, src++)
  1468. {
  1469. if (info [diff].addend != curr)
  1470. break;
  1471. /* Make sure that got_offset is valid. */
  1472. if (got_offset == (bfd_vma) -1)
  1473. got_offset = info [diff].got_offset;
  1474. }
  1475. /* Makre sure that the last duplicated one has an valid
  1476. offset. */
  1477. BFD_ASSERT (curr == prev);
  1478. if (got_offset != (bfd_vma) -1)
  1479. info [diff - 1].got_offset = got_offset;
  1480. if (diff < count)
  1481. {
  1482. /* Find the next duplicate. Track the current valid
  1483. offset. */
  1484. prev = info [diff].addend;
  1485. got_offset = info [diff].got_offset;
  1486. for (dupes = diff + 1; dupes < count; dupes ++)
  1487. {
  1488. curr = info [dupes].addend;
  1489. if (curr == prev)
  1490. {
  1491. /* For duplicates, make sure that GOT_OFFSET
  1492. is valid. */
  1493. if (got_offset == (bfd_vma) -1)
  1494. got_offset = info [dupes].got_offset;
  1495. break;
  1496. }
  1497. got_offset = info [dupes].got_offset;
  1498. prev = curr;
  1499. diff++;
  1500. }
  1501. len = diff - src + 1;
  1502. i = diff + 1;
  1503. }
  1504. }
  1505. memmove (&info [dest], &info [src], len * sizeof (*info));
  1506. dest += len;
  1507. }
  1508. count = dest;
  1509. }
  1510. else
  1511. {
  1512. /* When we get here, either there is no duplicate at all or
  1513. the only duplicate is the last element. */
  1514. if (dest < count)
  1515. {
  1516. /* If the last element is a duplicate, make sure that the
  1517. kept one has a valid got_offset. We also update count. */
  1518. if (got_offset != (bfd_vma) -1)
  1519. info [dest - 1].got_offset = got_offset;
  1520. count = dest;
  1521. }
  1522. }
  1523. return count;
  1524. }
  1525. /* Find and/or create a descriptor for dynamic symbol info. This will
  1526. vary based on global or local symbol, and the addend to the reloc.
  1527. We don't sort when inserting. Also, we sort and eliminate
  1528. duplicates if there is an unsorted section. Typically, this will
  1529. only happen once, because we do all insertions before lookups. We
  1530. then use bsearch to do a lookup. This also allows lookups to be
  1531. fast. So we have fast insertion (O(log N) due to duplicate check),
  1532. fast lookup (O(log N)) and one sort (O(N log N) expected time).
  1533. Previously, all lookups were O(N) because of the use of the linked
  1534. list and also all insertions were O(N) because of the check for
  1535. duplicates. There are some complications here because the array
  1536. size grows occasionally, which may add an O(N) factor, but this
  1537. should be rare. Also, we free the excess array allocation, which
  1538. requires a copy which is O(N), but this only happens once. */
  1539. static struct elfNN_ia64_dyn_sym_info *
  1540. get_dyn_sym_info (struct elfNN_ia64_link_hash_table *ia64_info,
  1541. struct elf_link_hash_entry *h, bfd *abfd,
  1542. const Elf_Internal_Rela *rel, bool create)
  1543. {
  1544. struct elfNN_ia64_dyn_sym_info **info_p, *info, *dyn_i, key;
  1545. unsigned int *count_p, *sorted_count_p, *size_p;
  1546. unsigned int count, sorted_count, size;
  1547. bfd_vma addend = rel ? rel->r_addend : 0;
  1548. bfd_size_type amt;
  1549. if (h)
  1550. {
  1551. struct elfNN_ia64_link_hash_entry *global_h;
  1552. global_h = (struct elfNN_ia64_link_hash_entry *) h;
  1553. info_p = &global_h->info;
  1554. count_p = &global_h->count;
  1555. sorted_count_p = &global_h->sorted_count;
  1556. size_p = &global_h->size;
  1557. }
  1558. else
  1559. {
  1560. struct elfNN_ia64_local_hash_entry *loc_h;
  1561. loc_h = get_local_sym_hash (ia64_info, abfd, rel, create);
  1562. if (!loc_h)
  1563. {
  1564. BFD_ASSERT (!create);
  1565. return NULL;
  1566. }
  1567. info_p = &loc_h->info;
  1568. count_p = &loc_h->count;
  1569. sorted_count_p = &loc_h->sorted_count;
  1570. size_p = &loc_h->size;
  1571. }
  1572. count = *count_p;
  1573. sorted_count = *sorted_count_p;
  1574. size = *size_p;
  1575. info = *info_p;
  1576. if (create)
  1577. {
  1578. /* When we create the array, we don't check for duplicates,
  1579. except in the previously sorted section if one exists, and
  1580. against the last inserted entry. This allows insertions to
  1581. be fast. */
  1582. if (info)
  1583. {
  1584. if (sorted_count)
  1585. {
  1586. /* Try bsearch first on the sorted section. */
  1587. key.addend = addend;
  1588. dyn_i = bsearch (&key, info, sorted_count,
  1589. sizeof (*info), addend_compare);
  1590. if (dyn_i)
  1591. return dyn_i;
  1592. }
  1593. if (count != 0)
  1594. {
  1595. /* Do a quick check for the last inserted entry. */
  1596. dyn_i = info + count - 1;
  1597. if (dyn_i->addend == addend)
  1598. return dyn_i;
  1599. }
  1600. }
  1601. if (size == 0)
  1602. {
  1603. /* It is the very first element. We create the array of size
  1604. 1. */
  1605. size = 1;
  1606. amt = size * sizeof (*info);
  1607. info = bfd_malloc (amt);
  1608. }
  1609. else if (size <= count)
  1610. {
  1611. /* We double the array size every time when we reach the
  1612. size limit. */
  1613. size += size;
  1614. amt = size * sizeof (*info);
  1615. info = bfd_realloc (info, amt);
  1616. }
  1617. else
  1618. goto has_space;
  1619. if (info == NULL)
  1620. return NULL;
  1621. *size_p = size;
  1622. *info_p = info;
  1623. has_space:
  1624. /* Append the new one to the array. */
  1625. dyn_i = info + count;
  1626. memset (dyn_i, 0, sizeof (*dyn_i));
  1627. dyn_i->got_offset = (bfd_vma) -1;
  1628. dyn_i->addend = addend;
  1629. /* We increment count only since the new ones are unsorted and
  1630. may have duplicate. */
  1631. (*count_p)++;
  1632. }
  1633. else
  1634. {
  1635. /* It is a lookup without insertion. Sort array if part of the
  1636. array isn't sorted. */
  1637. if (count != sorted_count)
  1638. {
  1639. count = sort_dyn_sym_info (info, count);
  1640. *count_p = count;
  1641. *sorted_count_p = count;
  1642. }
  1643. /* Free unused memory. */
  1644. if (size != count)
  1645. {
  1646. amt = count * sizeof (*info);
  1647. info = bfd_realloc (info, amt);
  1648. *size_p = count;
  1649. if (info == NULL && count != 0)
  1650. /* realloc should never fail since we are reducing size here,
  1651. but if it does use the old array. */
  1652. info = *info_p;
  1653. else
  1654. *info_p = info;
  1655. }
  1656. if (count == 0)
  1657. dyn_i = NULL;
  1658. else
  1659. {
  1660. key.addend = addend;
  1661. dyn_i = bsearch (&key, info, count, sizeof (*info), addend_compare);
  1662. }
  1663. }
  1664. return dyn_i;
  1665. }
  1666. static asection *
  1667. get_got (bfd *abfd, struct bfd_link_info *info,
  1668. struct elfNN_ia64_link_hash_table *ia64_info)
  1669. {
  1670. asection *got;
  1671. bfd *dynobj;
  1672. got = ia64_info->root.sgot;
  1673. if (!got)
  1674. {
  1675. flagword flags;
  1676. dynobj = ia64_info->root.dynobj;
  1677. if (!dynobj)
  1678. ia64_info->root.dynobj = dynobj = abfd;
  1679. if (!_bfd_elf_create_got_section (dynobj, info))
  1680. return NULL;
  1681. got = ia64_info->root.sgot;
  1682. /* The .got section is always aligned at 8 bytes. */
  1683. if (!bfd_set_section_alignment (got, 3))
  1684. return NULL;
  1685. flags = bfd_section_flags (got);
  1686. if (!bfd_set_section_flags (got, SEC_SMALL_DATA | flags))
  1687. return NULL;
  1688. }
  1689. return got;
  1690. }
  1691. /* Create function descriptor section (.opd). This section is called .opd
  1692. because it contains "official procedure descriptors". The "official"
  1693. refers to the fact that these descriptors are used when taking the address
  1694. of a procedure, thus ensuring a unique address for each procedure. */
  1695. static asection *
  1696. get_fptr (bfd *abfd, struct bfd_link_info *info,
  1697. struct elfNN_ia64_link_hash_table *ia64_info)
  1698. {
  1699. asection *fptr;
  1700. bfd *dynobj;
  1701. fptr = ia64_info->fptr_sec;
  1702. if (!fptr)
  1703. {
  1704. dynobj = ia64_info->root.dynobj;
  1705. if (!dynobj)
  1706. ia64_info->root.dynobj = dynobj = abfd;
  1707. fptr = bfd_make_section_anyway_with_flags (dynobj, ".opd",
  1708. (SEC_ALLOC
  1709. | SEC_LOAD
  1710. | SEC_HAS_CONTENTS
  1711. | SEC_IN_MEMORY
  1712. | (bfd_link_pie (info)
  1713. ? 0 : SEC_READONLY)
  1714. | SEC_LINKER_CREATED));
  1715. if (!fptr
  1716. || !bfd_set_section_alignment (fptr, 4))
  1717. {
  1718. BFD_ASSERT (0);
  1719. return NULL;
  1720. }
  1721. ia64_info->fptr_sec = fptr;
  1722. if (bfd_link_pie (info))
  1723. {
  1724. asection *fptr_rel;
  1725. fptr_rel = bfd_make_section_anyway_with_flags (dynobj, ".rela.opd",
  1726. (SEC_ALLOC | SEC_LOAD
  1727. | SEC_HAS_CONTENTS
  1728. | SEC_IN_MEMORY
  1729. | SEC_LINKER_CREATED
  1730. | SEC_READONLY));
  1731. if (fptr_rel == NULL
  1732. || !bfd_set_section_alignment (fptr_rel, LOG_SECTION_ALIGN))
  1733. {
  1734. BFD_ASSERT (0);
  1735. return NULL;
  1736. }
  1737. ia64_info->rel_fptr_sec = fptr_rel;
  1738. }
  1739. }
  1740. return fptr;
  1741. }
  1742. static asection *
  1743. get_pltoff (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1744. struct elfNN_ia64_link_hash_table *ia64_info)
  1745. {
  1746. asection *pltoff;
  1747. bfd *dynobj;
  1748. pltoff = ia64_info->pltoff_sec;
  1749. if (!pltoff)
  1750. {
  1751. dynobj = ia64_info->root.dynobj;
  1752. if (!dynobj)
  1753. ia64_info->root.dynobj = dynobj = abfd;
  1754. pltoff = bfd_make_section_anyway_with_flags (dynobj,
  1755. ELF_STRING_ia64_pltoff,
  1756. (SEC_ALLOC
  1757. | SEC_LOAD
  1758. | SEC_HAS_CONTENTS
  1759. | SEC_IN_MEMORY
  1760. | SEC_SMALL_DATA
  1761. | SEC_LINKER_CREATED));
  1762. if (!pltoff
  1763. || !bfd_set_section_alignment (pltoff, 4))
  1764. {
  1765. BFD_ASSERT (0);
  1766. return NULL;
  1767. }
  1768. ia64_info->pltoff_sec = pltoff;
  1769. }
  1770. return pltoff;
  1771. }
  1772. static asection *
  1773. get_reloc_section (bfd *abfd,
  1774. struct elfNN_ia64_link_hash_table *ia64_info,
  1775. asection *sec, bool create)
  1776. {
  1777. const char *srel_name;
  1778. asection *srel;
  1779. bfd *dynobj;
  1780. srel_name = (bfd_elf_string_from_elf_section
  1781. (abfd, elf_elfheader(abfd)->e_shstrndx,
  1782. _bfd_elf_single_rel_hdr (sec)->sh_name));
  1783. if (srel_name == NULL)
  1784. return NULL;
  1785. dynobj = ia64_info->root.dynobj;
  1786. if (!dynobj)
  1787. ia64_info->root.dynobj = dynobj = abfd;
  1788. srel = bfd_get_linker_section (dynobj, srel_name);
  1789. if (srel == NULL && create)
  1790. {
  1791. srel = bfd_make_section_anyway_with_flags (dynobj, srel_name,
  1792. (SEC_ALLOC | SEC_LOAD
  1793. | SEC_HAS_CONTENTS
  1794. | SEC_IN_MEMORY
  1795. | SEC_LINKER_CREATED
  1796. | SEC_READONLY));
  1797. if (srel == NULL
  1798. || !bfd_set_section_alignment (srel, LOG_SECTION_ALIGN))
  1799. return NULL;
  1800. }
  1801. return srel;
  1802. }
  1803. static bool
  1804. count_dyn_reloc (bfd *abfd, struct elfNN_ia64_dyn_sym_info *dyn_i,
  1805. asection *srel, int type, bool reltext)
  1806. {
  1807. struct elfNN_ia64_dyn_reloc_entry *rent;
  1808. for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
  1809. if (rent->srel == srel && rent->type == type)
  1810. break;
  1811. if (!rent)
  1812. {
  1813. rent = ((struct elfNN_ia64_dyn_reloc_entry *)
  1814. bfd_alloc (abfd, (bfd_size_type) sizeof (*rent)));
  1815. if (!rent)
  1816. return false;
  1817. rent->next = dyn_i->reloc_entries;
  1818. rent->srel = srel;
  1819. rent->type = type;
  1820. rent->count = 0;
  1821. dyn_i->reloc_entries = rent;
  1822. }
  1823. rent->reltext = reltext;
  1824. rent->count++;
  1825. return true;
  1826. }
  1827. static bool
  1828. elfNN_ia64_check_relocs (bfd *abfd, struct bfd_link_info *info,
  1829. asection *sec,
  1830. const Elf_Internal_Rela *relocs)
  1831. {
  1832. struct elfNN_ia64_link_hash_table *ia64_info;
  1833. const Elf_Internal_Rela *relend;
  1834. Elf_Internal_Shdr *symtab_hdr;
  1835. const Elf_Internal_Rela *rel;
  1836. asection *got, *fptr, *srel, *pltoff;
  1837. enum {
  1838. NEED_GOT = 1,
  1839. NEED_GOTX = 2,
  1840. NEED_FPTR = 4,
  1841. NEED_PLTOFF = 8,
  1842. NEED_MIN_PLT = 16,
  1843. NEED_FULL_PLT = 32,
  1844. NEED_DYNREL = 64,
  1845. NEED_LTOFF_FPTR = 128,
  1846. NEED_TPREL = 256,
  1847. NEED_DTPMOD = 512,
  1848. NEED_DTPREL = 1024
  1849. };
  1850. int need_entry;
  1851. struct elf_link_hash_entry *h;
  1852. unsigned long r_symndx;
  1853. bool maybe_dynamic;
  1854. if (bfd_link_relocatable (info))
  1855. return true;
  1856. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1857. ia64_info = elfNN_ia64_hash_table (info);
  1858. if (ia64_info == NULL)
  1859. return false;
  1860. got = fptr = srel = pltoff = NULL;
  1861. relend = relocs + sec->reloc_count;
  1862. /* We scan relocations first to create dynamic relocation arrays. We
  1863. modified get_dyn_sym_info to allow fast insertion and support fast
  1864. lookup in the next loop. */
  1865. for (rel = relocs; rel < relend; ++rel)
  1866. {
  1867. r_symndx = ELFNN_R_SYM (rel->r_info);
  1868. if (r_symndx >= symtab_hdr->sh_info)
  1869. {
  1870. long indx = r_symndx - symtab_hdr->sh_info;
  1871. h = elf_sym_hashes (abfd)[indx];
  1872. while (h->root.type == bfd_link_hash_indirect
  1873. || h->root.type == bfd_link_hash_warning)
  1874. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1875. }
  1876. else
  1877. h = NULL;
  1878. /* We can only get preliminary data on whether a symbol is
  1879. locally or externally defined, as not all of the input files
  1880. have yet been processed. Do something with what we know, as
  1881. this may help reduce memory usage and processing time later. */
  1882. maybe_dynamic = (h && ((!bfd_link_executable (info)
  1883. && (!SYMBOLIC_BIND (info, h)
  1884. || info->unresolved_syms_in_shared_libs == RM_IGNORE))
  1885. || !h->def_regular
  1886. || h->root.type == bfd_link_hash_defweak));
  1887. need_entry = 0;
  1888. switch (ELFNN_R_TYPE (rel->r_info))
  1889. {
  1890. case R_IA64_TPREL64MSB:
  1891. case R_IA64_TPREL64LSB:
  1892. if (bfd_link_pic (info) || maybe_dynamic)
  1893. need_entry = NEED_DYNREL;
  1894. break;
  1895. case R_IA64_LTOFF_TPREL22:
  1896. need_entry = NEED_TPREL;
  1897. if (bfd_link_pic (info))
  1898. info->flags |= DF_STATIC_TLS;
  1899. break;
  1900. case R_IA64_DTPREL32MSB:
  1901. case R_IA64_DTPREL32LSB:
  1902. case R_IA64_DTPREL64MSB:
  1903. case R_IA64_DTPREL64LSB:
  1904. if (bfd_link_pic (info) || maybe_dynamic)
  1905. need_entry = NEED_DYNREL;
  1906. break;
  1907. case R_IA64_LTOFF_DTPREL22:
  1908. need_entry = NEED_DTPREL;
  1909. break;
  1910. case R_IA64_DTPMOD64MSB:
  1911. case R_IA64_DTPMOD64LSB:
  1912. if (bfd_link_pic (info) || maybe_dynamic)
  1913. need_entry = NEED_DYNREL;
  1914. break;
  1915. case R_IA64_LTOFF_DTPMOD22:
  1916. need_entry = NEED_DTPMOD;
  1917. break;
  1918. case R_IA64_LTOFF_FPTR22:
  1919. case R_IA64_LTOFF_FPTR64I:
  1920. case R_IA64_LTOFF_FPTR32MSB:
  1921. case R_IA64_LTOFF_FPTR32LSB:
  1922. case R_IA64_LTOFF_FPTR64MSB:
  1923. case R_IA64_LTOFF_FPTR64LSB:
  1924. need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
  1925. break;
  1926. case R_IA64_FPTR64I:
  1927. case R_IA64_FPTR32MSB:
  1928. case R_IA64_FPTR32LSB:
  1929. case R_IA64_FPTR64MSB:
  1930. case R_IA64_FPTR64LSB:
  1931. if (bfd_link_pic (info) || h)
  1932. need_entry = NEED_FPTR | NEED_DYNREL;
  1933. else
  1934. need_entry = NEED_FPTR;
  1935. break;
  1936. case R_IA64_LTOFF22:
  1937. case R_IA64_LTOFF64I:
  1938. need_entry = NEED_GOT;
  1939. break;
  1940. case R_IA64_LTOFF22X:
  1941. need_entry = NEED_GOTX;
  1942. break;
  1943. case R_IA64_PLTOFF22:
  1944. case R_IA64_PLTOFF64I:
  1945. case R_IA64_PLTOFF64MSB:
  1946. case R_IA64_PLTOFF64LSB:
  1947. need_entry = NEED_PLTOFF;
  1948. if (h)
  1949. {
  1950. if (maybe_dynamic)
  1951. need_entry |= NEED_MIN_PLT;
  1952. }
  1953. else
  1954. {
  1955. (*info->callbacks->warning)
  1956. (info, _("@pltoff reloc against local symbol"), 0,
  1957. abfd, 0, (bfd_vma) 0);
  1958. }
  1959. break;
  1960. case R_IA64_PCREL21B:
  1961. case R_IA64_PCREL60B:
  1962. /* Depending on where this symbol is defined, we may or may not
  1963. need a full plt entry. Only skip if we know we'll not need
  1964. the entry -- static or symbolic, and the symbol definition
  1965. has already been seen. */
  1966. if (maybe_dynamic && rel->r_addend == 0)
  1967. need_entry = NEED_FULL_PLT;
  1968. break;
  1969. case R_IA64_IMM14:
  1970. case R_IA64_IMM22:
  1971. case R_IA64_IMM64:
  1972. case R_IA64_DIR32MSB:
  1973. case R_IA64_DIR32LSB:
  1974. case R_IA64_DIR64MSB:
  1975. case R_IA64_DIR64LSB:
  1976. /* Shared objects will always need at least a REL relocation. */
  1977. if (bfd_link_pic (info) || maybe_dynamic)
  1978. need_entry = NEED_DYNREL;
  1979. break;
  1980. case R_IA64_IPLTMSB:
  1981. case R_IA64_IPLTLSB:
  1982. /* Shared objects will always need at least a REL relocation. */
  1983. if (bfd_link_pic (info) || maybe_dynamic)
  1984. need_entry = NEED_DYNREL;
  1985. break;
  1986. case R_IA64_PCREL22:
  1987. case R_IA64_PCREL64I:
  1988. case R_IA64_PCREL32MSB:
  1989. case R_IA64_PCREL32LSB:
  1990. case R_IA64_PCREL64MSB:
  1991. case R_IA64_PCREL64LSB:
  1992. if (maybe_dynamic)
  1993. need_entry = NEED_DYNREL;
  1994. break;
  1995. }
  1996. if (!need_entry)
  1997. continue;
  1998. if ((need_entry & NEED_FPTR) != 0
  1999. && rel->r_addend)
  2000. {
  2001. (*info->callbacks->warning)
  2002. (info, _("non-zero addend in @fptr reloc"), 0,
  2003. abfd, 0, (bfd_vma) 0);
  2004. }
  2005. if (get_dyn_sym_info (ia64_info, h, abfd, rel, true) == NULL)
  2006. return false;
  2007. }
  2008. /* Now, we only do lookup without insertion, which is very fast
  2009. with the modified get_dyn_sym_info. */
  2010. for (rel = relocs; rel < relend; ++rel)
  2011. {
  2012. struct elfNN_ia64_dyn_sym_info *dyn_i;
  2013. int dynrel_type = R_IA64_NONE;
  2014. r_symndx = ELFNN_R_SYM (rel->r_info);
  2015. if (r_symndx >= symtab_hdr->sh_info)
  2016. {
  2017. /* We're dealing with a global symbol -- find its hash entry
  2018. and mark it as being referenced. */
  2019. long indx = r_symndx - symtab_hdr->sh_info;
  2020. h = elf_sym_hashes (abfd)[indx];
  2021. while (h->root.type == bfd_link_hash_indirect
  2022. || h->root.type == bfd_link_hash_warning)
  2023. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2024. /* PR15323, ref flags aren't set for references in the same
  2025. object. */
  2026. h->ref_regular = 1;
  2027. }
  2028. else
  2029. h = NULL;
  2030. /* We can only get preliminary data on whether a symbol is
  2031. locally or externally defined, as not all of the input files
  2032. have yet been processed. Do something with what we know, as
  2033. this may help reduce memory usage and processing time later. */
  2034. maybe_dynamic = (h && ((!bfd_link_executable (info)
  2035. && (!SYMBOLIC_BIND (info, h)
  2036. || info->unresolved_syms_in_shared_libs == RM_IGNORE))
  2037. || !h->def_regular
  2038. || h->root.type == bfd_link_hash_defweak));
  2039. need_entry = 0;
  2040. switch (ELFNN_R_TYPE (rel->r_info))
  2041. {
  2042. case R_IA64_TPREL64MSB:
  2043. case R_IA64_TPREL64LSB:
  2044. if (bfd_link_pic (info) || maybe_dynamic)
  2045. need_entry = NEED_DYNREL;
  2046. dynrel_type = R_IA64_TPREL64LSB;
  2047. if (bfd_link_pic (info))
  2048. info->flags |= DF_STATIC_TLS;
  2049. break;
  2050. case R_IA64_LTOFF_TPREL22:
  2051. need_entry = NEED_TPREL;
  2052. if (bfd_link_pic (info))
  2053. info->flags |= DF_STATIC_TLS;
  2054. break;
  2055. case R_IA64_DTPREL32MSB:
  2056. case R_IA64_DTPREL32LSB:
  2057. case R_IA64_DTPREL64MSB:
  2058. case R_IA64_DTPREL64LSB:
  2059. if (bfd_link_pic (info) || maybe_dynamic)
  2060. need_entry = NEED_DYNREL;
  2061. dynrel_type = R_IA64_DTPRELNNLSB;
  2062. break;
  2063. case R_IA64_LTOFF_DTPREL22:
  2064. need_entry = NEED_DTPREL;
  2065. break;
  2066. case R_IA64_DTPMOD64MSB:
  2067. case R_IA64_DTPMOD64LSB:
  2068. if (bfd_link_pic (info) || maybe_dynamic)
  2069. need_entry = NEED_DYNREL;
  2070. dynrel_type = R_IA64_DTPMOD64LSB;
  2071. break;
  2072. case R_IA64_LTOFF_DTPMOD22:
  2073. need_entry = NEED_DTPMOD;
  2074. break;
  2075. case R_IA64_LTOFF_FPTR22:
  2076. case R_IA64_LTOFF_FPTR64I:
  2077. case R_IA64_LTOFF_FPTR32MSB:
  2078. case R_IA64_LTOFF_FPTR32LSB:
  2079. case R_IA64_LTOFF_FPTR64MSB:
  2080. case R_IA64_LTOFF_FPTR64LSB:
  2081. need_entry = NEED_FPTR | NEED_GOT | NEED_LTOFF_FPTR;
  2082. break;
  2083. case R_IA64_FPTR64I:
  2084. case R_IA64_FPTR32MSB:
  2085. case R_IA64_FPTR32LSB:
  2086. case R_IA64_FPTR64MSB:
  2087. case R_IA64_FPTR64LSB:
  2088. if (bfd_link_pic (info) || h)
  2089. need_entry = NEED_FPTR | NEED_DYNREL;
  2090. else
  2091. need_entry = NEED_FPTR;
  2092. dynrel_type = R_IA64_FPTRNNLSB;
  2093. break;
  2094. case R_IA64_LTOFF22:
  2095. case R_IA64_LTOFF64I:
  2096. need_entry = NEED_GOT;
  2097. break;
  2098. case R_IA64_LTOFF22X:
  2099. need_entry = NEED_GOTX;
  2100. break;
  2101. case R_IA64_PLTOFF22:
  2102. case R_IA64_PLTOFF64I:
  2103. case R_IA64_PLTOFF64MSB:
  2104. case R_IA64_PLTOFF64LSB:
  2105. need_entry = NEED_PLTOFF;
  2106. if (h)
  2107. {
  2108. if (maybe_dynamic)
  2109. need_entry |= NEED_MIN_PLT;
  2110. }
  2111. break;
  2112. case R_IA64_PCREL21B:
  2113. case R_IA64_PCREL60B:
  2114. /* Depending on where this symbol is defined, we may or may not
  2115. need a full plt entry. Only skip if we know we'll not need
  2116. the entry -- static or symbolic, and the symbol definition
  2117. has already been seen. */
  2118. if (maybe_dynamic && rel->r_addend == 0)
  2119. need_entry = NEED_FULL_PLT;
  2120. break;
  2121. case R_IA64_IMM14:
  2122. case R_IA64_IMM22:
  2123. case R_IA64_IMM64:
  2124. case R_IA64_DIR32MSB:
  2125. case R_IA64_DIR32LSB:
  2126. case R_IA64_DIR64MSB:
  2127. case R_IA64_DIR64LSB:
  2128. /* Shared objects will always need at least a REL relocation. */
  2129. if (bfd_link_pic (info) || maybe_dynamic)
  2130. need_entry = NEED_DYNREL;
  2131. dynrel_type = R_IA64_DIRNNLSB;
  2132. break;
  2133. case R_IA64_IPLTMSB:
  2134. case R_IA64_IPLTLSB:
  2135. /* Shared objects will always need at least a REL relocation. */
  2136. if (bfd_link_pic (info) || maybe_dynamic)
  2137. need_entry = NEED_DYNREL;
  2138. dynrel_type = R_IA64_IPLTLSB;
  2139. break;
  2140. case R_IA64_PCREL22:
  2141. case R_IA64_PCREL64I:
  2142. case R_IA64_PCREL32MSB:
  2143. case R_IA64_PCREL32LSB:
  2144. case R_IA64_PCREL64MSB:
  2145. case R_IA64_PCREL64LSB:
  2146. if (maybe_dynamic)
  2147. need_entry = NEED_DYNREL;
  2148. dynrel_type = R_IA64_PCRELNNLSB;
  2149. break;
  2150. }
  2151. if (!need_entry)
  2152. continue;
  2153. dyn_i = get_dyn_sym_info (ia64_info, h, abfd, rel, false);
  2154. /* Record whether or not this is a local symbol. */
  2155. dyn_i->h = h;
  2156. /* Create what's needed. */
  2157. if (need_entry & (NEED_GOT | NEED_GOTX | NEED_TPREL
  2158. | NEED_DTPMOD | NEED_DTPREL))
  2159. {
  2160. if (!got)
  2161. {
  2162. got = get_got (abfd, info, ia64_info);
  2163. if (!got)
  2164. return false;
  2165. }
  2166. if (need_entry & NEED_GOT)
  2167. dyn_i->want_got = 1;
  2168. if (need_entry & NEED_GOTX)
  2169. dyn_i->want_gotx = 1;
  2170. if (need_entry & NEED_TPREL)
  2171. dyn_i->want_tprel = 1;
  2172. if (need_entry & NEED_DTPMOD)
  2173. dyn_i->want_dtpmod = 1;
  2174. if (need_entry & NEED_DTPREL)
  2175. dyn_i->want_dtprel = 1;
  2176. }
  2177. if (need_entry & NEED_FPTR)
  2178. {
  2179. if (!fptr)
  2180. {
  2181. fptr = get_fptr (abfd, info, ia64_info);
  2182. if (!fptr)
  2183. return false;
  2184. }
  2185. /* FPTRs for shared libraries are allocated by the dynamic
  2186. linker. Make sure this local symbol will appear in the
  2187. dynamic symbol table. */
  2188. if (!h && bfd_link_pic (info))
  2189. {
  2190. if (! (bfd_elf_link_record_local_dynamic_symbol
  2191. (info, abfd, (long) r_symndx)))
  2192. return false;
  2193. }
  2194. dyn_i->want_fptr = 1;
  2195. }
  2196. if (need_entry & NEED_LTOFF_FPTR)
  2197. dyn_i->want_ltoff_fptr = 1;
  2198. if (need_entry & (NEED_MIN_PLT | NEED_FULL_PLT))
  2199. {
  2200. if (!ia64_info->root.dynobj)
  2201. ia64_info->root.dynobj = abfd;
  2202. h->needs_plt = 1;
  2203. dyn_i->want_plt = 1;
  2204. }
  2205. if (need_entry & NEED_FULL_PLT)
  2206. dyn_i->want_plt2 = 1;
  2207. if (need_entry & NEED_PLTOFF)
  2208. {
  2209. /* This is needed here, in case @pltoff is used in a non-shared
  2210. link. */
  2211. if (!pltoff)
  2212. {
  2213. pltoff = get_pltoff (abfd, info, ia64_info);
  2214. if (!pltoff)
  2215. return false;
  2216. }
  2217. dyn_i->want_pltoff = 1;
  2218. }
  2219. if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
  2220. {
  2221. if (!srel)
  2222. {
  2223. srel = get_reloc_section (abfd, ia64_info, sec, true);
  2224. if (!srel)
  2225. return false;
  2226. }
  2227. if (!count_dyn_reloc (abfd, dyn_i, srel, dynrel_type,
  2228. (sec->flags & SEC_READONLY) != 0))
  2229. return false;
  2230. }
  2231. }
  2232. return true;
  2233. }
  2234. /* For cleanliness, and potentially faster dynamic loading, allocate
  2235. external GOT entries first. */
  2236. static bool
  2237. allocate_global_data_got (struct elfNN_ia64_dyn_sym_info *dyn_i,
  2238. void * data)
  2239. {
  2240. struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
  2241. if ((dyn_i->want_got || dyn_i->want_gotx)
  2242. && ! dyn_i->want_fptr
  2243. && elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0))
  2244. {
  2245. dyn_i->got_offset = x->ofs;
  2246. x->ofs += 8;
  2247. }
  2248. if (dyn_i->want_tprel)
  2249. {
  2250. dyn_i->tprel_offset = x->ofs;
  2251. x->ofs += 8;
  2252. }
  2253. if (dyn_i->want_dtpmod)
  2254. {
  2255. if (elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0))
  2256. {
  2257. dyn_i->dtpmod_offset = x->ofs;
  2258. x->ofs += 8;
  2259. }
  2260. else
  2261. {
  2262. struct elfNN_ia64_link_hash_table *ia64_info;
  2263. ia64_info = elfNN_ia64_hash_table (x->info);
  2264. if (ia64_info == NULL)
  2265. return false;
  2266. if (ia64_info->self_dtpmod_offset == (bfd_vma) -1)
  2267. {
  2268. ia64_info->self_dtpmod_offset = x->ofs;
  2269. x->ofs += 8;
  2270. }
  2271. dyn_i->dtpmod_offset = ia64_info->self_dtpmod_offset;
  2272. }
  2273. }
  2274. if (dyn_i->want_dtprel)
  2275. {
  2276. dyn_i->dtprel_offset = x->ofs;
  2277. x->ofs += 8;
  2278. }
  2279. return true;
  2280. }
  2281. /* Next, allocate all the GOT entries used by LTOFF_FPTR relocs. */
  2282. static bool
  2283. allocate_global_fptr_got (struct elfNN_ia64_dyn_sym_info *dyn_i,
  2284. void * data)
  2285. {
  2286. struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
  2287. if (dyn_i->want_got
  2288. && dyn_i->want_fptr
  2289. && elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, R_IA64_FPTRNNLSB))
  2290. {
  2291. dyn_i->got_offset = x->ofs;
  2292. x->ofs += 8;
  2293. }
  2294. return true;
  2295. }
  2296. /* Lastly, allocate all the GOT entries for local data. */
  2297. static bool
  2298. allocate_local_got (struct elfNN_ia64_dyn_sym_info *dyn_i,
  2299. void * data)
  2300. {
  2301. struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
  2302. if ((dyn_i->want_got || dyn_i->want_gotx)
  2303. && !elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0))
  2304. {
  2305. dyn_i->got_offset = x->ofs;
  2306. x->ofs += 8;
  2307. }
  2308. return true;
  2309. }
  2310. /* Search for the index of a global symbol in it's defining object file. */
  2311. static long
  2312. global_sym_index (struct elf_link_hash_entry *h)
  2313. {
  2314. struct elf_link_hash_entry **p;
  2315. bfd *obj;
  2316. BFD_ASSERT (h->root.type == bfd_link_hash_defined
  2317. || h->root.type == bfd_link_hash_defweak);
  2318. obj = h->root.u.def.section->owner;
  2319. for (p = elf_sym_hashes (obj); *p != h; ++p)
  2320. continue;
  2321. return p - elf_sym_hashes (obj) + elf_tdata (obj)->symtab_hdr.sh_info;
  2322. }
  2323. /* Allocate function descriptors. We can do these for every function
  2324. in a main executable that is not exported. */
  2325. static bool
  2326. allocate_fptr (struct elfNN_ia64_dyn_sym_info *dyn_i, void * data)
  2327. {
  2328. struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
  2329. if (dyn_i->want_fptr)
  2330. {
  2331. struct elf_link_hash_entry *h = dyn_i->h;
  2332. if (h)
  2333. while (h->root.type == bfd_link_hash_indirect
  2334. || h->root.type == bfd_link_hash_warning)
  2335. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2336. if (!bfd_link_executable (x->info)
  2337. && (!h
  2338. || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  2339. || (h->root.type != bfd_link_hash_undefweak
  2340. && h->root.type != bfd_link_hash_undefined)))
  2341. {
  2342. if (h && h->dynindx == -1)
  2343. {
  2344. BFD_ASSERT ((h->root.type == bfd_link_hash_defined)
  2345. || (h->root.type == bfd_link_hash_defweak));
  2346. if (!bfd_elf_link_record_local_dynamic_symbol
  2347. (x->info, h->root.u.def.section->owner,
  2348. global_sym_index (h)))
  2349. return false;
  2350. }
  2351. dyn_i->want_fptr = 0;
  2352. }
  2353. else if (h == NULL || h->dynindx == -1)
  2354. {
  2355. dyn_i->fptr_offset = x->ofs;
  2356. x->ofs += 16;
  2357. }
  2358. else
  2359. dyn_i->want_fptr = 0;
  2360. }
  2361. return true;
  2362. }
  2363. /* Allocate all the minimal PLT entries. */
  2364. static bool
  2365. allocate_plt_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
  2366. void * data)
  2367. {
  2368. struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
  2369. if (dyn_i->want_plt)
  2370. {
  2371. struct elf_link_hash_entry *h = dyn_i->h;
  2372. if (h)
  2373. while (h->root.type == bfd_link_hash_indirect
  2374. || h->root.type == bfd_link_hash_warning)
  2375. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2376. /* ??? Versioned symbols seem to lose NEEDS_PLT. */
  2377. if (elfNN_ia64_dynamic_symbol_p (h, x->info, 0))
  2378. {
  2379. bfd_size_type offset = x->ofs;
  2380. if (offset == 0)
  2381. offset = PLT_HEADER_SIZE;
  2382. dyn_i->plt_offset = offset;
  2383. x->ofs = offset + PLT_MIN_ENTRY_SIZE;
  2384. dyn_i->want_pltoff = 1;
  2385. }
  2386. else
  2387. {
  2388. dyn_i->want_plt = 0;
  2389. dyn_i->want_plt2 = 0;
  2390. }
  2391. }
  2392. return true;
  2393. }
  2394. /* Allocate all the full PLT entries. */
  2395. static bool
  2396. allocate_plt2_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
  2397. void * data)
  2398. {
  2399. struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
  2400. if (dyn_i->want_plt2)
  2401. {
  2402. struct elf_link_hash_entry *h = dyn_i->h;
  2403. bfd_size_type ofs = x->ofs;
  2404. dyn_i->plt2_offset = ofs;
  2405. x->ofs = ofs + PLT_FULL_ENTRY_SIZE;
  2406. while (h->root.type == bfd_link_hash_indirect
  2407. || h->root.type == bfd_link_hash_warning)
  2408. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2409. dyn_i->h->plt.offset = ofs;
  2410. }
  2411. return true;
  2412. }
  2413. /* Allocate all the PLTOFF entries requested by relocations and
  2414. plt entries. We can't share space with allocated FPTR entries,
  2415. because the latter are not necessarily addressable by the GP.
  2416. ??? Relaxation might be able to determine that they are. */
  2417. static bool
  2418. allocate_pltoff_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
  2419. void * data)
  2420. {
  2421. struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
  2422. if (dyn_i->want_pltoff)
  2423. {
  2424. dyn_i->pltoff_offset = x->ofs;
  2425. x->ofs += 16;
  2426. }
  2427. return true;
  2428. }
  2429. /* Allocate dynamic relocations for those symbols that turned out
  2430. to be dynamic. */
  2431. static bool
  2432. allocate_dynrel_entries (struct elfNN_ia64_dyn_sym_info *dyn_i,
  2433. void * data)
  2434. {
  2435. struct elfNN_ia64_allocate_data *x = (struct elfNN_ia64_allocate_data *)data;
  2436. struct elfNN_ia64_link_hash_table *ia64_info;
  2437. struct elfNN_ia64_dyn_reloc_entry *rent;
  2438. bool dynamic_symbol, shared, resolved_zero;
  2439. ia64_info = elfNN_ia64_hash_table (x->info);
  2440. if (ia64_info == NULL)
  2441. return false;
  2442. /* Note that this can't be used in relation to FPTR relocs below. */
  2443. dynamic_symbol = elfNN_ia64_dynamic_symbol_p (dyn_i->h, x->info, 0);
  2444. shared = bfd_link_pic (x->info);
  2445. resolved_zero = (dyn_i->h
  2446. && ELF_ST_VISIBILITY (dyn_i->h->other)
  2447. && dyn_i->h->root.type == bfd_link_hash_undefweak);
  2448. /* Take care of the GOT and PLT relocations. */
  2449. if ((!resolved_zero
  2450. && (dynamic_symbol || shared)
  2451. && (dyn_i->want_got || dyn_i->want_gotx))
  2452. || (dyn_i->want_ltoff_fptr
  2453. && dyn_i->h
  2454. && dyn_i->h->dynindx != -1))
  2455. {
  2456. if (!dyn_i->want_ltoff_fptr
  2457. || !bfd_link_pie (x->info)
  2458. || dyn_i->h == NULL
  2459. || dyn_i->h->root.type != bfd_link_hash_undefweak)
  2460. ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
  2461. }
  2462. if ((dynamic_symbol || shared) && dyn_i->want_tprel)
  2463. ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
  2464. if (dynamic_symbol && dyn_i->want_dtpmod)
  2465. ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
  2466. if (dynamic_symbol && dyn_i->want_dtprel)
  2467. ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
  2468. if (x->only_got)
  2469. return true;
  2470. if (ia64_info->rel_fptr_sec && dyn_i->want_fptr)
  2471. {
  2472. if (dyn_i->h == NULL || dyn_i->h->root.type != bfd_link_hash_undefweak)
  2473. ia64_info->rel_fptr_sec->size += sizeof (ElfNN_External_Rela);
  2474. }
  2475. if (!resolved_zero && dyn_i->want_pltoff)
  2476. {
  2477. bfd_size_type t = 0;
  2478. /* Dynamic symbols get one IPLT relocation. Local symbols in
  2479. shared libraries get two REL relocations. Local symbols in
  2480. main applications get nothing. */
  2481. if (dynamic_symbol)
  2482. t = sizeof (ElfNN_External_Rela);
  2483. else if (shared)
  2484. t = 2 * sizeof (ElfNN_External_Rela);
  2485. ia64_info->rel_pltoff_sec->size += t;
  2486. }
  2487. /* Take care of the normal data relocations. */
  2488. for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
  2489. {
  2490. int count = rent->count;
  2491. switch (rent->type)
  2492. {
  2493. case R_IA64_FPTR32LSB:
  2494. case R_IA64_FPTR64LSB:
  2495. /* Allocate one iff !want_fptr and not PIE, which by this point
  2496. will be true only if we're actually allocating one statically
  2497. in the main executable. Position independent executables
  2498. need a relative reloc. */
  2499. if (dyn_i->want_fptr && !bfd_link_pie (x->info))
  2500. continue;
  2501. break;
  2502. case R_IA64_PCREL32LSB:
  2503. case R_IA64_PCREL64LSB:
  2504. if (!dynamic_symbol)
  2505. continue;
  2506. break;
  2507. case R_IA64_DIR32LSB:
  2508. case R_IA64_DIR64LSB:
  2509. if (!dynamic_symbol && !shared)
  2510. continue;
  2511. break;
  2512. case R_IA64_IPLTLSB:
  2513. if (!dynamic_symbol && !shared)
  2514. continue;
  2515. /* Use two REL relocations for IPLT relocations
  2516. against local symbols. */
  2517. if (!dynamic_symbol)
  2518. count *= 2;
  2519. break;
  2520. case R_IA64_DTPREL32LSB:
  2521. case R_IA64_TPREL64LSB:
  2522. case R_IA64_DTPREL64LSB:
  2523. case R_IA64_DTPMOD64LSB:
  2524. break;
  2525. default:
  2526. abort ();
  2527. }
  2528. if (rent->reltext)
  2529. x->info->flags |= DF_TEXTREL;
  2530. rent->srel->size += sizeof (ElfNN_External_Rela) * count;
  2531. }
  2532. return true;
  2533. }
  2534. static bool
  2535. elfNN_ia64_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2536. struct elf_link_hash_entry *h)
  2537. {
  2538. /* ??? Undefined symbols with PLT entries should be re-defined
  2539. to be the PLT entry. */
  2540. /* If this is a weak symbol, and there is a real definition, the
  2541. processor independent code will have arranged for us to see the
  2542. real definition first, and we can just use the same value. */
  2543. if (h->is_weakalias)
  2544. {
  2545. struct elf_link_hash_entry *def = weakdef (h);
  2546. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  2547. h->root.u.def.section = def->root.u.def.section;
  2548. h->root.u.def.value = def->root.u.def.value;
  2549. return true;
  2550. }
  2551. /* If this is a reference to a symbol defined by a dynamic object which
  2552. is not a function, we might allocate the symbol in our .dynbss section
  2553. and allocate a COPY dynamic relocation.
  2554. But IA-64 code is canonically PIC, so as a rule we can avoid this sort
  2555. of hackery. */
  2556. return true;
  2557. }
  2558. static bool
  2559. elfNN_ia64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  2560. struct bfd_link_info *info)
  2561. {
  2562. struct elfNN_ia64_allocate_data data;
  2563. struct elfNN_ia64_link_hash_table *ia64_info;
  2564. asection *sec;
  2565. bfd *dynobj;
  2566. ia64_info = elfNN_ia64_hash_table (info);
  2567. if (ia64_info == NULL)
  2568. return false;
  2569. dynobj = ia64_info->root.dynobj;
  2570. ia64_info->self_dtpmod_offset = (bfd_vma) -1;
  2571. BFD_ASSERT(dynobj != NULL);
  2572. data.info = info;
  2573. /* Set the contents of the .interp section to the interpreter. */
  2574. if (ia64_info->root.dynamic_sections_created
  2575. && bfd_link_executable (info) && !info->nointerp)
  2576. {
  2577. sec = bfd_get_linker_section (dynobj, ".interp");
  2578. BFD_ASSERT (sec != NULL);
  2579. sec->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
  2580. sec->size = strlen (ELF_DYNAMIC_INTERPRETER) + 1;
  2581. }
  2582. /* Allocate the GOT entries. */
  2583. if (ia64_info->root.sgot)
  2584. {
  2585. data.ofs = 0;
  2586. elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_data_got, &data);
  2587. elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_global_fptr_got, &data);
  2588. elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_local_got, &data);
  2589. ia64_info->root.sgot->size = data.ofs;
  2590. }
  2591. /* Allocate the FPTR entries. */
  2592. if (ia64_info->fptr_sec)
  2593. {
  2594. data.ofs = 0;
  2595. elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_fptr, &data);
  2596. ia64_info->fptr_sec->size = data.ofs;
  2597. }
  2598. /* Now that we've seen all of the input files, we can decide which
  2599. symbols need plt entries. Allocate the minimal PLT entries first.
  2600. We do this even though dynamic_sections_created may be FALSE, because
  2601. this has the side-effect of clearing want_plt and want_plt2. */
  2602. data.ofs = 0;
  2603. elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt_entries, &data);
  2604. ia64_info->minplt_entries = 0;
  2605. if (data.ofs)
  2606. {
  2607. ia64_info->minplt_entries
  2608. = (data.ofs - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
  2609. }
  2610. /* Align the pointer for the plt2 entries. */
  2611. data.ofs = (data.ofs + 31) & (bfd_vma) -32;
  2612. elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_plt2_entries, &data);
  2613. if (data.ofs != 0 || ia64_info->root.dynamic_sections_created)
  2614. {
  2615. /* FIXME: we always reserve the memory for dynamic linker even if
  2616. there are no PLT entries since dynamic linker may assume the
  2617. reserved memory always exists. */
  2618. BFD_ASSERT (ia64_info->root.dynamic_sections_created);
  2619. ia64_info->root.splt->size = data.ofs;
  2620. /* If we've got a .plt, we need some extra memory for the dynamic
  2621. linker. We stuff these in .got.plt. */
  2622. ia64_info->root.sgotplt->size = 8 * PLT_RESERVED_WORDS;
  2623. }
  2624. /* Allocate the PLTOFF entries. */
  2625. if (ia64_info->pltoff_sec)
  2626. {
  2627. data.ofs = 0;
  2628. elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_pltoff_entries, &data);
  2629. ia64_info->pltoff_sec->size = data.ofs;
  2630. }
  2631. if (ia64_info->root.dynamic_sections_created)
  2632. {
  2633. /* Allocate space for the dynamic relocations that turned out to be
  2634. required. */
  2635. if (bfd_link_pic (info) && ia64_info->self_dtpmod_offset != (bfd_vma) -1)
  2636. ia64_info->root.srelgot->size += sizeof (ElfNN_External_Rela);
  2637. data.only_got = false;
  2638. elfNN_ia64_dyn_sym_traverse (ia64_info, allocate_dynrel_entries, &data);
  2639. }
  2640. /* We have now determined the sizes of the various dynamic sections.
  2641. Allocate memory for them. */
  2642. for (sec = dynobj->sections; sec != NULL; sec = sec->next)
  2643. {
  2644. bool strip;
  2645. if (!(sec->flags & SEC_LINKER_CREATED))
  2646. continue;
  2647. /* If we don't need this section, strip it from the output file.
  2648. There were several sections primarily related to dynamic
  2649. linking that must be create before the linker maps input
  2650. sections to output sections. The linker does that before
  2651. bfd_elf_size_dynamic_sections is called, and it is that
  2652. function which decides whether anything needs to go into
  2653. these sections. */
  2654. strip = (sec->size == 0);
  2655. if (sec == ia64_info->root.sgot)
  2656. strip = false;
  2657. else if (sec == ia64_info->root.srelgot)
  2658. {
  2659. if (strip)
  2660. ia64_info->root.srelgot = NULL;
  2661. else
  2662. /* We use the reloc_count field as a counter if we need to
  2663. copy relocs into the output file. */
  2664. sec->reloc_count = 0;
  2665. }
  2666. else if (sec == ia64_info->fptr_sec)
  2667. {
  2668. if (strip)
  2669. ia64_info->fptr_sec = NULL;
  2670. }
  2671. else if (sec == ia64_info->rel_fptr_sec)
  2672. {
  2673. if (strip)
  2674. ia64_info->rel_fptr_sec = NULL;
  2675. else
  2676. /* We use the reloc_count field as a counter if we need to
  2677. copy relocs into the output file. */
  2678. sec->reloc_count = 0;
  2679. }
  2680. else if (sec == ia64_info->root.splt)
  2681. {
  2682. if (strip)
  2683. ia64_info->root.splt = NULL;
  2684. }
  2685. else if (sec == ia64_info->pltoff_sec)
  2686. {
  2687. if (strip)
  2688. ia64_info->pltoff_sec = NULL;
  2689. }
  2690. else if (sec == ia64_info->rel_pltoff_sec)
  2691. {
  2692. if (strip)
  2693. ia64_info->rel_pltoff_sec = NULL;
  2694. else
  2695. {
  2696. ia64_info->root.dt_jmprel_required = true;
  2697. /* We use the reloc_count field as a counter if we need to
  2698. copy relocs into the output file. */
  2699. sec->reloc_count = 0;
  2700. }
  2701. }
  2702. else
  2703. {
  2704. const char *name;
  2705. /* It's OK to base decisions on the section name, because none
  2706. of the dynobj section names depend upon the input files. */
  2707. name = bfd_section_name (sec);
  2708. if (strcmp (name, ".got.plt") == 0)
  2709. strip = false;
  2710. else if (startswith (name, ".rel"))
  2711. {
  2712. if (!strip)
  2713. {
  2714. /* We use the reloc_count field as a counter if we need to
  2715. copy relocs into the output file. */
  2716. sec->reloc_count = 0;
  2717. }
  2718. }
  2719. else
  2720. continue;
  2721. }
  2722. if (strip)
  2723. sec->flags |= SEC_EXCLUDE;
  2724. else
  2725. {
  2726. /* Allocate memory for the section contents. */
  2727. sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
  2728. if (sec->contents == NULL && sec->size != 0)
  2729. return false;
  2730. }
  2731. }
  2732. if (ia64_info->root.dynamic_sections_created)
  2733. {
  2734. /* Add some entries to the .dynamic section. We fill in the values
  2735. later (in finish_dynamic_sections) but we must add the entries now
  2736. so that we get the correct size for the .dynamic section. */
  2737. #define add_dynamic_entry(TAG, VAL) \
  2738. _bfd_elf_add_dynamic_entry (info, TAG, VAL)
  2739. if (!_bfd_elf_add_dynamic_tags (output_bfd, info, true))
  2740. return false;
  2741. if (!add_dynamic_entry (DT_IA_64_PLT_RESERVE, 0))
  2742. return false;
  2743. }
  2744. /* ??? Perhaps force __gp local. */
  2745. return true;
  2746. }
  2747. static void
  2748. elfNN_ia64_install_dyn_reloc (bfd *abfd, struct bfd_link_info *info,
  2749. asection *sec, asection *srel,
  2750. bfd_vma offset, unsigned int type,
  2751. long dynindx, bfd_vma addend)
  2752. {
  2753. Elf_Internal_Rela outrel;
  2754. bfd_byte *loc;
  2755. BFD_ASSERT (dynindx != -1);
  2756. outrel.r_info = ELFNN_R_INFO (dynindx, type);
  2757. outrel.r_addend = addend;
  2758. outrel.r_offset = _bfd_elf_section_offset (abfd, info, sec, offset);
  2759. if (outrel.r_offset >= (bfd_vma) -2)
  2760. {
  2761. /* Run for the hills. We shouldn't be outputting a relocation
  2762. for this. So do what everyone else does and output a no-op. */
  2763. outrel.r_info = ELFNN_R_INFO (0, R_IA64_NONE);
  2764. outrel.r_addend = 0;
  2765. outrel.r_offset = 0;
  2766. }
  2767. else
  2768. outrel.r_offset += sec->output_section->vma + sec->output_offset;
  2769. loc = srel->contents;
  2770. loc += srel->reloc_count++ * sizeof (ElfNN_External_Rela);
  2771. bfd_elfNN_swap_reloca_out (abfd, &outrel, loc);
  2772. BFD_ASSERT (sizeof (ElfNN_External_Rela) * srel->reloc_count <= srel->size);
  2773. }
  2774. /* Store an entry for target address TARGET_ADDR in the linkage table
  2775. and return the gp-relative address of the linkage table entry. */
  2776. static bfd_vma
  2777. set_got_entry (bfd *abfd, struct bfd_link_info *info,
  2778. struct elfNN_ia64_dyn_sym_info *dyn_i,
  2779. long dynindx, bfd_vma addend, bfd_vma value,
  2780. unsigned int dyn_r_type)
  2781. {
  2782. struct elfNN_ia64_link_hash_table *ia64_info;
  2783. asection *got_sec;
  2784. bool done;
  2785. bfd_vma got_offset;
  2786. ia64_info = elfNN_ia64_hash_table (info);
  2787. if (ia64_info == NULL)
  2788. return 0;
  2789. got_sec = ia64_info->root.sgot;
  2790. switch (dyn_r_type)
  2791. {
  2792. case R_IA64_TPREL64LSB:
  2793. done = dyn_i->tprel_done;
  2794. dyn_i->tprel_done = true;
  2795. got_offset = dyn_i->tprel_offset;
  2796. break;
  2797. case R_IA64_DTPMOD64LSB:
  2798. if (dyn_i->dtpmod_offset != ia64_info->self_dtpmod_offset)
  2799. {
  2800. done = dyn_i->dtpmod_done;
  2801. dyn_i->dtpmod_done = true;
  2802. }
  2803. else
  2804. {
  2805. done = ia64_info->self_dtpmod_done;
  2806. ia64_info->self_dtpmod_done = true;
  2807. dynindx = 0;
  2808. }
  2809. got_offset = dyn_i->dtpmod_offset;
  2810. break;
  2811. case R_IA64_DTPREL32LSB:
  2812. case R_IA64_DTPREL64LSB:
  2813. done = dyn_i->dtprel_done;
  2814. dyn_i->dtprel_done = true;
  2815. got_offset = dyn_i->dtprel_offset;
  2816. break;
  2817. default:
  2818. done = dyn_i->got_done;
  2819. dyn_i->got_done = true;
  2820. got_offset = dyn_i->got_offset;
  2821. break;
  2822. }
  2823. BFD_ASSERT ((got_offset & 7) == 0);
  2824. if (! done)
  2825. {
  2826. /* Store the target address in the linkage table entry. */
  2827. bfd_put_64 (abfd, value, got_sec->contents + got_offset);
  2828. /* Install a dynamic relocation if needed. */
  2829. if (((bfd_link_pic (info)
  2830. && (!dyn_i->h
  2831. || ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
  2832. || dyn_i->h->root.type != bfd_link_hash_undefweak)
  2833. && dyn_r_type != R_IA64_DTPREL32LSB
  2834. && dyn_r_type != R_IA64_DTPREL64LSB)
  2835. || elfNN_ia64_dynamic_symbol_p (dyn_i->h, info, dyn_r_type)
  2836. || (dynindx != -1
  2837. && (dyn_r_type == R_IA64_FPTR32LSB
  2838. || dyn_r_type == R_IA64_FPTR64LSB)))
  2839. && (!dyn_i->want_ltoff_fptr
  2840. || !bfd_link_pie (info)
  2841. || !dyn_i->h
  2842. || dyn_i->h->root.type != bfd_link_hash_undefweak))
  2843. {
  2844. if (dynindx == -1
  2845. && dyn_r_type != R_IA64_TPREL64LSB
  2846. && dyn_r_type != R_IA64_DTPMOD64LSB
  2847. && dyn_r_type != R_IA64_DTPREL32LSB
  2848. && dyn_r_type != R_IA64_DTPREL64LSB)
  2849. {
  2850. dyn_r_type = R_IA64_RELNNLSB;
  2851. dynindx = 0;
  2852. addend = value;
  2853. }
  2854. if (bfd_big_endian (abfd))
  2855. {
  2856. switch (dyn_r_type)
  2857. {
  2858. case R_IA64_REL32LSB:
  2859. dyn_r_type = R_IA64_REL32MSB;
  2860. break;
  2861. case R_IA64_DIR32LSB:
  2862. dyn_r_type = R_IA64_DIR32MSB;
  2863. break;
  2864. case R_IA64_FPTR32LSB:
  2865. dyn_r_type = R_IA64_FPTR32MSB;
  2866. break;
  2867. case R_IA64_DTPREL32LSB:
  2868. dyn_r_type = R_IA64_DTPREL32MSB;
  2869. break;
  2870. case R_IA64_REL64LSB:
  2871. dyn_r_type = R_IA64_REL64MSB;
  2872. break;
  2873. case R_IA64_DIR64LSB:
  2874. dyn_r_type = R_IA64_DIR64MSB;
  2875. break;
  2876. case R_IA64_FPTR64LSB:
  2877. dyn_r_type = R_IA64_FPTR64MSB;
  2878. break;
  2879. case R_IA64_TPREL64LSB:
  2880. dyn_r_type = R_IA64_TPREL64MSB;
  2881. break;
  2882. case R_IA64_DTPMOD64LSB:
  2883. dyn_r_type = R_IA64_DTPMOD64MSB;
  2884. break;
  2885. case R_IA64_DTPREL64LSB:
  2886. dyn_r_type = R_IA64_DTPREL64MSB;
  2887. break;
  2888. default:
  2889. BFD_ASSERT (false);
  2890. break;
  2891. }
  2892. }
  2893. elfNN_ia64_install_dyn_reloc (abfd, NULL, got_sec,
  2894. ia64_info->root.srelgot,
  2895. got_offset, dyn_r_type,
  2896. dynindx, addend);
  2897. }
  2898. }
  2899. /* Return the address of the linkage table entry. */
  2900. value = (got_sec->output_section->vma
  2901. + got_sec->output_offset
  2902. + got_offset);
  2903. return value;
  2904. }
  2905. /* Fill in a function descriptor consisting of the function's code
  2906. address and its global pointer. Return the descriptor's address. */
  2907. static bfd_vma
  2908. set_fptr_entry (bfd *abfd, struct bfd_link_info *info,
  2909. struct elfNN_ia64_dyn_sym_info *dyn_i,
  2910. bfd_vma value)
  2911. {
  2912. struct elfNN_ia64_link_hash_table *ia64_info;
  2913. asection *fptr_sec;
  2914. ia64_info = elfNN_ia64_hash_table (info);
  2915. if (ia64_info == NULL)
  2916. return 0;
  2917. fptr_sec = ia64_info->fptr_sec;
  2918. if (!dyn_i->fptr_done)
  2919. {
  2920. dyn_i->fptr_done = 1;
  2921. /* Fill in the function descriptor. */
  2922. bfd_put_64 (abfd, value, fptr_sec->contents + dyn_i->fptr_offset);
  2923. bfd_put_64 (abfd, _bfd_get_gp_value (abfd),
  2924. fptr_sec->contents + dyn_i->fptr_offset + 8);
  2925. if (ia64_info->rel_fptr_sec)
  2926. {
  2927. Elf_Internal_Rela outrel;
  2928. bfd_byte *loc;
  2929. if (bfd_little_endian (abfd))
  2930. outrel.r_info = ELFNN_R_INFO (0, R_IA64_IPLTLSB);
  2931. else
  2932. outrel.r_info = ELFNN_R_INFO (0, R_IA64_IPLTMSB);
  2933. outrel.r_addend = value;
  2934. outrel.r_offset = (fptr_sec->output_section->vma
  2935. + fptr_sec->output_offset
  2936. + dyn_i->fptr_offset);
  2937. loc = ia64_info->rel_fptr_sec->contents;
  2938. loc += ia64_info->rel_fptr_sec->reloc_count++
  2939. * sizeof (ElfNN_External_Rela);
  2940. bfd_elfNN_swap_reloca_out (abfd, &outrel, loc);
  2941. }
  2942. }
  2943. /* Return the descriptor's address. */
  2944. value = (fptr_sec->output_section->vma
  2945. + fptr_sec->output_offset
  2946. + dyn_i->fptr_offset);
  2947. return value;
  2948. }
  2949. /* Fill in a PLTOFF entry consisting of the function's code address
  2950. and its global pointer. Return the descriptor's address. */
  2951. static bfd_vma
  2952. set_pltoff_entry (bfd *abfd, struct bfd_link_info *info,
  2953. struct elfNN_ia64_dyn_sym_info *dyn_i,
  2954. bfd_vma value, bool is_plt)
  2955. {
  2956. struct elfNN_ia64_link_hash_table *ia64_info;
  2957. asection *pltoff_sec;
  2958. ia64_info = elfNN_ia64_hash_table (info);
  2959. if (ia64_info == NULL)
  2960. return 0;
  2961. pltoff_sec = ia64_info->pltoff_sec;
  2962. /* Don't do anything if this symbol uses a real PLT entry. In
  2963. that case, we'll fill this in during finish_dynamic_symbol. */
  2964. if ((! dyn_i->want_plt || is_plt)
  2965. && !dyn_i->pltoff_done)
  2966. {
  2967. bfd_vma gp = _bfd_get_gp_value (abfd);
  2968. /* Fill in the function descriptor. */
  2969. bfd_put_64 (abfd, value, pltoff_sec->contents + dyn_i->pltoff_offset);
  2970. bfd_put_64 (abfd, gp, pltoff_sec->contents + dyn_i->pltoff_offset + 8);
  2971. /* Install dynamic relocations if needed. */
  2972. if (!is_plt
  2973. && bfd_link_pic (info)
  2974. && (!dyn_i->h
  2975. || ELF_ST_VISIBILITY (dyn_i->h->other) == STV_DEFAULT
  2976. || dyn_i->h->root.type != bfd_link_hash_undefweak))
  2977. {
  2978. unsigned int dyn_r_type;
  2979. if (bfd_big_endian (abfd))
  2980. dyn_r_type = R_IA64_RELNNMSB;
  2981. else
  2982. dyn_r_type = R_IA64_RELNNLSB;
  2983. elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
  2984. ia64_info->rel_pltoff_sec,
  2985. dyn_i->pltoff_offset,
  2986. dyn_r_type, 0, value);
  2987. elfNN_ia64_install_dyn_reloc (abfd, NULL, pltoff_sec,
  2988. ia64_info->rel_pltoff_sec,
  2989. dyn_i->pltoff_offset + ARCH_SIZE / 8,
  2990. dyn_r_type, 0, gp);
  2991. }
  2992. dyn_i->pltoff_done = 1;
  2993. }
  2994. /* Return the descriptor's address. */
  2995. value = (pltoff_sec->output_section->vma
  2996. + pltoff_sec->output_offset
  2997. + dyn_i->pltoff_offset);
  2998. return value;
  2999. }
  3000. /* Return the base VMA address which should be subtracted from real addresses
  3001. when resolving @tprel() relocation.
  3002. Main program TLS (whose template starts at PT_TLS p_vaddr)
  3003. is assigned offset round(2 * size of pointer, PT_TLS p_align). */
  3004. static bfd_vma
  3005. elfNN_ia64_tprel_base (struct bfd_link_info *info)
  3006. {
  3007. asection *tls_sec = elf_hash_table (info)->tls_sec;
  3008. return tls_sec->vma - align_power ((bfd_vma) ARCH_SIZE / 4,
  3009. tls_sec->alignment_power);
  3010. }
  3011. /* Return the base VMA address which should be subtracted from real addresses
  3012. when resolving @dtprel() relocation.
  3013. This is PT_TLS segment p_vaddr. */
  3014. static bfd_vma
  3015. elfNN_ia64_dtprel_base (struct bfd_link_info *info)
  3016. {
  3017. return elf_hash_table (info)->tls_sec->vma;
  3018. }
  3019. /* Called through qsort to sort the .IA_64.unwind section during a
  3020. non-relocatable link. Set elfNN_ia64_unwind_entry_compare_bfd
  3021. to the output bfd so we can do proper endianness frobbing. */
  3022. static bfd *elfNN_ia64_unwind_entry_compare_bfd;
  3023. static int
  3024. elfNN_ia64_unwind_entry_compare (const void * a, const void * b)
  3025. {
  3026. bfd_vma av, bv;
  3027. av = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, a);
  3028. bv = bfd_get_64 (elfNN_ia64_unwind_entry_compare_bfd, b);
  3029. return (av < bv ? -1 : av > bv ? 1 : 0);
  3030. }
  3031. /* Make sure we've got ourselves a nice fat __gp value. */
  3032. static bool
  3033. elfNN_ia64_choose_gp (bfd *abfd, struct bfd_link_info *info, bool final)
  3034. {
  3035. bfd_vma min_vma = (bfd_vma) -1, max_vma = 0;
  3036. bfd_vma min_short_vma = min_vma, max_short_vma = 0;
  3037. struct elf_link_hash_entry *gp;
  3038. bfd_vma gp_val;
  3039. asection *os;
  3040. struct elfNN_ia64_link_hash_table *ia64_info;
  3041. ia64_info = elfNN_ia64_hash_table (info);
  3042. if (ia64_info == NULL)
  3043. return false;
  3044. /* Find the min and max vma of all sections marked short. Also collect
  3045. min and max vma of any type, for use in selecting a nice gp. */
  3046. for (os = abfd->sections; os ; os = os->next)
  3047. {
  3048. bfd_vma lo, hi;
  3049. if ((os->flags & SEC_ALLOC) == 0)
  3050. continue;
  3051. lo = os->vma;
  3052. /* When this function is called from elfNN_ia64_final_link
  3053. the correct value to use is os->size. When called from
  3054. elfNN_ia64_relax_section we are in the middle of section
  3055. sizing; some sections will already have os->size set, others
  3056. will have os->size zero and os->rawsize the previous size. */
  3057. hi = os->vma + (!final && os->rawsize ? os->rawsize : os->size);
  3058. if (hi < lo)
  3059. hi = (bfd_vma) -1;
  3060. if (min_vma > lo)
  3061. min_vma = lo;
  3062. if (max_vma < hi)
  3063. max_vma = hi;
  3064. if (os->flags & SEC_SMALL_DATA)
  3065. {
  3066. if (min_short_vma > lo)
  3067. min_short_vma = lo;
  3068. if (max_short_vma < hi)
  3069. max_short_vma = hi;
  3070. }
  3071. }
  3072. if (ia64_info->min_short_sec)
  3073. {
  3074. if (min_short_vma
  3075. > (ia64_info->min_short_sec->vma
  3076. + ia64_info->min_short_offset))
  3077. min_short_vma = (ia64_info->min_short_sec->vma
  3078. + ia64_info->min_short_offset);
  3079. if (max_short_vma
  3080. < (ia64_info->max_short_sec->vma
  3081. + ia64_info->max_short_offset))
  3082. max_short_vma = (ia64_info->max_short_sec->vma
  3083. + ia64_info->max_short_offset);
  3084. }
  3085. /* See if the user wants to force a value. */
  3086. gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
  3087. false, false);
  3088. if (gp
  3089. && (gp->root.type == bfd_link_hash_defined
  3090. || gp->root.type == bfd_link_hash_defweak))
  3091. {
  3092. asection *gp_sec = gp->root.u.def.section;
  3093. gp_val = (gp->root.u.def.value
  3094. + gp_sec->output_section->vma
  3095. + gp_sec->output_offset);
  3096. }
  3097. else
  3098. {
  3099. /* Pick a sensible value. */
  3100. if (ia64_info->min_short_sec)
  3101. {
  3102. bfd_vma short_range = max_short_vma - min_short_vma;
  3103. /* If min_short_sec is set, pick one in the middle bewteen
  3104. min_short_vma and max_short_vma. */
  3105. if (short_range >= 0x400000)
  3106. goto overflow;
  3107. gp_val = min_short_vma + short_range / 2;
  3108. }
  3109. else
  3110. {
  3111. asection *got_sec = ia64_info->root.sgot;
  3112. /* Start with just the address of the .got. */
  3113. if (got_sec)
  3114. gp_val = got_sec->output_section->vma;
  3115. else if (max_short_vma != 0)
  3116. gp_val = min_short_vma;
  3117. else if (max_vma - min_vma < 0x200000)
  3118. gp_val = min_vma;
  3119. else
  3120. gp_val = max_vma - 0x200000 + 8;
  3121. }
  3122. /* If it is possible to address the entire image, but we
  3123. don't with the choice above, adjust. */
  3124. if (max_vma - min_vma < 0x400000
  3125. && (max_vma - gp_val >= 0x200000
  3126. || gp_val - min_vma > 0x200000))
  3127. gp_val = min_vma + 0x200000;
  3128. else if (max_short_vma != 0)
  3129. {
  3130. /* If we don't cover all the short data, adjust. */
  3131. if (max_short_vma - gp_val >= 0x200000)
  3132. gp_val = min_short_vma + 0x200000;
  3133. /* If we're addressing stuff past the end, adjust back. */
  3134. if (gp_val > max_vma)
  3135. gp_val = max_vma - 0x200000 + 8;
  3136. }
  3137. }
  3138. /* Validate whether all SHF_IA_64_SHORT sections are within
  3139. range of the chosen GP. */
  3140. if (max_short_vma != 0)
  3141. {
  3142. if (max_short_vma - min_short_vma >= 0x400000)
  3143. {
  3144. overflow:
  3145. _bfd_error_handler
  3146. /* xgettext:c-format */
  3147. (_("%pB: short data segment overflowed (%#" PRIx64 " >= 0x400000)"),
  3148. abfd, (uint64_t) (max_short_vma - min_short_vma));
  3149. return false;
  3150. }
  3151. else if ((gp_val > min_short_vma
  3152. && gp_val - min_short_vma > 0x200000)
  3153. || (gp_val < max_short_vma
  3154. && max_short_vma - gp_val >= 0x200000))
  3155. {
  3156. _bfd_error_handler
  3157. (_("%pB: __gp does not cover short data segment"), abfd);
  3158. return false;
  3159. }
  3160. }
  3161. _bfd_set_gp_value (abfd, gp_val);
  3162. return true;
  3163. }
  3164. static bool
  3165. elfNN_ia64_final_link (bfd *abfd, struct bfd_link_info *info)
  3166. {
  3167. struct elfNN_ia64_link_hash_table *ia64_info;
  3168. asection *unwind_output_sec;
  3169. ia64_info = elfNN_ia64_hash_table (info);
  3170. if (ia64_info == NULL)
  3171. return false;
  3172. /* Make sure we've got ourselves a nice fat __gp value. */
  3173. if (!bfd_link_relocatable (info))
  3174. {
  3175. bfd_vma gp_val;
  3176. struct elf_link_hash_entry *gp;
  3177. /* We assume after gp is set, section size will only decrease. We
  3178. need to adjust gp for it. */
  3179. _bfd_set_gp_value (abfd, 0);
  3180. if (! elfNN_ia64_choose_gp (abfd, info, true))
  3181. return false;
  3182. gp_val = _bfd_get_gp_value (abfd);
  3183. gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
  3184. false, false);
  3185. if (gp)
  3186. {
  3187. gp->root.type = bfd_link_hash_defined;
  3188. gp->root.u.def.value = gp_val;
  3189. gp->root.u.def.section = bfd_abs_section_ptr;
  3190. }
  3191. }
  3192. /* If we're producing a final executable, we need to sort the contents
  3193. of the .IA_64.unwind section. Force this section to be relocated
  3194. into memory rather than written immediately to the output file. */
  3195. unwind_output_sec = NULL;
  3196. if (!bfd_link_relocatable (info))
  3197. {
  3198. asection *s = bfd_get_section_by_name (abfd, ELF_STRING_ia64_unwind);
  3199. if (s)
  3200. {
  3201. unwind_output_sec = s->output_section;
  3202. unwind_output_sec->contents
  3203. = bfd_malloc (unwind_output_sec->size);
  3204. if (unwind_output_sec->contents == NULL)
  3205. return false;
  3206. }
  3207. }
  3208. /* Invoke the regular ELF backend linker to do all the work. */
  3209. if (!bfd_elf_final_link (abfd, info))
  3210. return false;
  3211. if (unwind_output_sec)
  3212. {
  3213. elfNN_ia64_unwind_entry_compare_bfd = abfd;
  3214. qsort (unwind_output_sec->contents,
  3215. (size_t) (unwind_output_sec->size / 24),
  3216. 24,
  3217. elfNN_ia64_unwind_entry_compare);
  3218. if (! bfd_set_section_contents (abfd, unwind_output_sec,
  3219. unwind_output_sec->contents, (bfd_vma) 0,
  3220. unwind_output_sec->size))
  3221. return false;
  3222. }
  3223. return true;
  3224. }
  3225. static int
  3226. elfNN_ia64_relocate_section (bfd *output_bfd,
  3227. struct bfd_link_info *info,
  3228. bfd *input_bfd,
  3229. asection *input_section,
  3230. bfd_byte *contents,
  3231. Elf_Internal_Rela *relocs,
  3232. Elf_Internal_Sym *local_syms,
  3233. asection **local_sections)
  3234. {
  3235. struct elfNN_ia64_link_hash_table *ia64_info;
  3236. Elf_Internal_Shdr *symtab_hdr;
  3237. Elf_Internal_Rela *rel;
  3238. Elf_Internal_Rela *relend;
  3239. asection *srel;
  3240. bool ret_val = true; /* for non-fatal errors */
  3241. bfd_vma gp_val;
  3242. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  3243. ia64_info = elfNN_ia64_hash_table (info);
  3244. if (ia64_info == NULL)
  3245. return false;
  3246. /* Infect various flags from the input section to the output section. */
  3247. if (bfd_link_relocatable (info))
  3248. {
  3249. bfd_vma flags;
  3250. flags = elf_section_data(input_section)->this_hdr.sh_flags;
  3251. flags &= SHF_IA_64_NORECOV;
  3252. elf_section_data(input_section->output_section)
  3253. ->this_hdr.sh_flags |= flags;
  3254. }
  3255. gp_val = _bfd_get_gp_value (output_bfd);
  3256. srel = get_reloc_section (input_bfd, ia64_info, input_section, false);
  3257. rel = relocs;
  3258. relend = relocs + input_section->reloc_count;
  3259. for (; rel < relend; ++rel)
  3260. {
  3261. struct elf_link_hash_entry *h;
  3262. struct elfNN_ia64_dyn_sym_info *dyn_i;
  3263. bfd_reloc_status_type r;
  3264. reloc_howto_type *howto;
  3265. unsigned long r_symndx;
  3266. Elf_Internal_Sym *sym;
  3267. unsigned int r_type;
  3268. bfd_vma value;
  3269. asection *sym_sec;
  3270. bfd_byte *hit_addr;
  3271. bool dynamic_symbol_p;
  3272. bool undef_weak_ref;
  3273. r_type = ELFNN_R_TYPE (rel->r_info);
  3274. if (r_type > R_IA64_MAX_RELOC_CODE)
  3275. {
  3276. /* xgettext:c-format */
  3277. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  3278. input_bfd, (int) r_type);
  3279. bfd_set_error (bfd_error_bad_value);
  3280. ret_val = false;
  3281. continue;
  3282. }
  3283. howto = ia64_elf_lookup_howto (r_type);
  3284. if (howto == NULL)
  3285. {
  3286. ret_val = false;
  3287. continue;
  3288. }
  3289. r_symndx = ELFNN_R_SYM (rel->r_info);
  3290. h = NULL;
  3291. sym = NULL;
  3292. sym_sec = NULL;
  3293. undef_weak_ref = false;
  3294. if (r_symndx < symtab_hdr->sh_info)
  3295. {
  3296. /* Reloc against local symbol. */
  3297. asection *msec;
  3298. sym = local_syms + r_symndx;
  3299. sym_sec = local_sections[r_symndx];
  3300. msec = sym_sec;
  3301. value = _bfd_elf_rela_local_sym (output_bfd, sym, &msec, rel);
  3302. if (!bfd_link_relocatable (info)
  3303. && (sym_sec->flags & SEC_MERGE) != 0
  3304. && ELF_ST_TYPE (sym->st_info) == STT_SECTION
  3305. && sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE)
  3306. {
  3307. struct elfNN_ia64_local_hash_entry *loc_h;
  3308. loc_h = get_local_sym_hash (ia64_info, input_bfd, rel, false);
  3309. if (loc_h && ! loc_h->sec_merge_done)
  3310. {
  3311. struct elfNN_ia64_dyn_sym_info *dynent;
  3312. unsigned int count;
  3313. for (count = loc_h->count, dynent = loc_h->info;
  3314. count != 0;
  3315. count--, dynent++)
  3316. {
  3317. msec = sym_sec;
  3318. dynent->addend =
  3319. _bfd_merged_section_offset (output_bfd, &msec,
  3320. elf_section_data (msec)->
  3321. sec_info,
  3322. sym->st_value
  3323. + dynent->addend);
  3324. dynent->addend -= sym->st_value;
  3325. dynent->addend += msec->output_section->vma
  3326. + msec->output_offset
  3327. - sym_sec->output_section->vma
  3328. - sym_sec->output_offset;
  3329. }
  3330. /* We may have introduced duplicated entries. We need
  3331. to remove them properly. */
  3332. count = sort_dyn_sym_info (loc_h->info, loc_h->count);
  3333. if (count != loc_h->count)
  3334. {
  3335. loc_h->count = count;
  3336. loc_h->sorted_count = count;
  3337. }
  3338. loc_h->sec_merge_done = 1;
  3339. }
  3340. }
  3341. }
  3342. else
  3343. {
  3344. bool unresolved_reloc;
  3345. bool warned, ignored;
  3346. struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
  3347. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  3348. r_symndx, symtab_hdr, sym_hashes,
  3349. h, sym_sec, value,
  3350. unresolved_reloc, warned, ignored);
  3351. if (h->root.type == bfd_link_hash_undefweak)
  3352. undef_weak_ref = true;
  3353. else if (warned || (ignored && bfd_link_executable (info)))
  3354. continue;
  3355. }
  3356. if (sym_sec != NULL && discarded_section (sym_sec))
  3357. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  3358. rel, 1, relend, howto, 0, contents);
  3359. if (bfd_link_relocatable (info))
  3360. continue;
  3361. hit_addr = contents + rel->r_offset;
  3362. value += rel->r_addend;
  3363. dynamic_symbol_p = elfNN_ia64_dynamic_symbol_p (h, info, r_type);
  3364. switch (r_type)
  3365. {
  3366. case R_IA64_NONE:
  3367. case R_IA64_LDXMOV:
  3368. continue;
  3369. case R_IA64_IMM14:
  3370. case R_IA64_IMM22:
  3371. case R_IA64_IMM64:
  3372. case R_IA64_DIR32MSB:
  3373. case R_IA64_DIR32LSB:
  3374. case R_IA64_DIR64MSB:
  3375. case R_IA64_DIR64LSB:
  3376. /* Install a dynamic relocation for this reloc. */
  3377. if ((dynamic_symbol_p || bfd_link_pic (info))
  3378. && r_symndx != STN_UNDEF
  3379. && (input_section->flags & SEC_ALLOC) != 0)
  3380. {
  3381. unsigned int dyn_r_type;
  3382. long dynindx;
  3383. bfd_vma addend;
  3384. BFD_ASSERT (srel != NULL);
  3385. switch (r_type)
  3386. {
  3387. case R_IA64_IMM14:
  3388. case R_IA64_IMM22:
  3389. case R_IA64_IMM64:
  3390. /* ??? People shouldn't be doing non-pic code in
  3391. shared libraries nor dynamic executables. */
  3392. _bfd_error_handler
  3393. /* xgettext:c-format */
  3394. (_("%pB: non-pic code with imm relocation against dynamic symbol `%s'"),
  3395. input_bfd,
  3396. h ? h->root.root.string
  3397. : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
  3398. sym_sec));
  3399. ret_val = false;
  3400. continue;
  3401. default:
  3402. break;
  3403. }
  3404. /* If we don't need dynamic symbol lookup, find a
  3405. matching RELATIVE relocation. */
  3406. dyn_r_type = r_type;
  3407. if (dynamic_symbol_p)
  3408. {
  3409. dynindx = h->dynindx;
  3410. addend = rel->r_addend;
  3411. value = 0;
  3412. }
  3413. else
  3414. {
  3415. switch (r_type)
  3416. {
  3417. case R_IA64_DIR32MSB:
  3418. dyn_r_type = R_IA64_REL32MSB;
  3419. break;
  3420. case R_IA64_DIR32LSB:
  3421. dyn_r_type = R_IA64_REL32LSB;
  3422. break;
  3423. case R_IA64_DIR64MSB:
  3424. dyn_r_type = R_IA64_REL64MSB;
  3425. break;
  3426. case R_IA64_DIR64LSB:
  3427. dyn_r_type = R_IA64_REL64LSB;
  3428. break;
  3429. default:
  3430. break;
  3431. }
  3432. dynindx = 0;
  3433. addend = value;
  3434. }
  3435. elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
  3436. srel, rel->r_offset, dyn_r_type,
  3437. dynindx, addend);
  3438. }
  3439. /* Fall through. */
  3440. case R_IA64_LTV32MSB:
  3441. case R_IA64_LTV32LSB:
  3442. case R_IA64_LTV64MSB:
  3443. case R_IA64_LTV64LSB:
  3444. r = ia64_elf_install_value (hit_addr, value, r_type);
  3445. break;
  3446. case R_IA64_GPREL22:
  3447. case R_IA64_GPREL64I:
  3448. case R_IA64_GPREL32MSB:
  3449. case R_IA64_GPREL32LSB:
  3450. case R_IA64_GPREL64MSB:
  3451. case R_IA64_GPREL64LSB:
  3452. if (dynamic_symbol_p)
  3453. {
  3454. _bfd_error_handler
  3455. /* xgettext:c-format */
  3456. (_("%pB: @gprel relocation against dynamic symbol %s"),
  3457. input_bfd,
  3458. h ? h->root.root.string
  3459. : bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
  3460. sym_sec));
  3461. ret_val = false;
  3462. continue;
  3463. }
  3464. value -= gp_val;
  3465. r = ia64_elf_install_value (hit_addr, value, r_type);
  3466. break;
  3467. case R_IA64_LTOFF22:
  3468. case R_IA64_LTOFF22X:
  3469. case R_IA64_LTOFF64I:
  3470. dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
  3471. value = set_got_entry (input_bfd, info, dyn_i, (h ? h->dynindx : -1),
  3472. rel->r_addend, value, R_IA64_DIRNNLSB);
  3473. value -= gp_val;
  3474. r = ia64_elf_install_value (hit_addr, value, r_type);
  3475. break;
  3476. case R_IA64_PLTOFF22:
  3477. case R_IA64_PLTOFF64I:
  3478. case R_IA64_PLTOFF64MSB:
  3479. case R_IA64_PLTOFF64LSB:
  3480. dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
  3481. value = set_pltoff_entry (output_bfd, info, dyn_i, value, false);
  3482. value -= gp_val;
  3483. r = ia64_elf_install_value (hit_addr, value, r_type);
  3484. break;
  3485. case R_IA64_FPTR64I:
  3486. case R_IA64_FPTR32MSB:
  3487. case R_IA64_FPTR32LSB:
  3488. case R_IA64_FPTR64MSB:
  3489. case R_IA64_FPTR64LSB:
  3490. dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
  3491. if (dyn_i->want_fptr)
  3492. {
  3493. if (!undef_weak_ref)
  3494. value = set_fptr_entry (output_bfd, info, dyn_i, value);
  3495. }
  3496. if (!dyn_i->want_fptr || bfd_link_pie (info))
  3497. {
  3498. long dynindx;
  3499. unsigned int dyn_r_type = r_type;
  3500. bfd_vma addend = rel->r_addend;
  3501. /* Otherwise, we expect the dynamic linker to create
  3502. the entry. */
  3503. if (dyn_i->want_fptr)
  3504. {
  3505. if (r_type == R_IA64_FPTR64I)
  3506. {
  3507. /* We can't represent this without a dynamic symbol.
  3508. Adjust the relocation to be against an output
  3509. section symbol, which are always present in the
  3510. dynamic symbol table. */
  3511. /* ??? People shouldn't be doing non-pic code in
  3512. shared libraries. Hork. */
  3513. _bfd_error_handler
  3514. (_("%pB: linking non-pic code in a position independent executable"),
  3515. input_bfd);
  3516. ret_val = false;
  3517. continue;
  3518. }
  3519. dynindx = 0;
  3520. addend = value;
  3521. dyn_r_type = r_type + R_IA64_RELNNLSB - R_IA64_FPTRNNLSB;
  3522. }
  3523. else if (h)
  3524. {
  3525. if (h->dynindx != -1)
  3526. dynindx = h->dynindx;
  3527. else
  3528. dynindx = (_bfd_elf_link_lookup_local_dynindx
  3529. (info, h->root.u.def.section->owner,
  3530. global_sym_index (h)));
  3531. value = 0;
  3532. }
  3533. else
  3534. {
  3535. dynindx = (_bfd_elf_link_lookup_local_dynindx
  3536. (info, input_bfd, (long) r_symndx));
  3537. value = 0;
  3538. }
  3539. elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
  3540. srel, rel->r_offset, dyn_r_type,
  3541. dynindx, addend);
  3542. }
  3543. r = ia64_elf_install_value (hit_addr, value, r_type);
  3544. break;
  3545. case R_IA64_LTOFF_FPTR22:
  3546. case R_IA64_LTOFF_FPTR64I:
  3547. case R_IA64_LTOFF_FPTR32MSB:
  3548. case R_IA64_LTOFF_FPTR32LSB:
  3549. case R_IA64_LTOFF_FPTR64MSB:
  3550. case R_IA64_LTOFF_FPTR64LSB:
  3551. {
  3552. long dynindx;
  3553. dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
  3554. if (dyn_i->want_fptr)
  3555. {
  3556. BFD_ASSERT (h == NULL || h->dynindx == -1);
  3557. if (!undef_weak_ref)
  3558. value = set_fptr_entry (output_bfd, info, dyn_i, value);
  3559. dynindx = -1;
  3560. }
  3561. else
  3562. {
  3563. /* Otherwise, we expect the dynamic linker to create
  3564. the entry. */
  3565. if (h)
  3566. {
  3567. if (h->dynindx != -1)
  3568. dynindx = h->dynindx;
  3569. else
  3570. dynindx = (_bfd_elf_link_lookup_local_dynindx
  3571. (info, h->root.u.def.section->owner,
  3572. global_sym_index (h)));
  3573. }
  3574. else
  3575. dynindx = (_bfd_elf_link_lookup_local_dynindx
  3576. (info, input_bfd, (long) r_symndx));
  3577. value = 0;
  3578. }
  3579. value = set_got_entry (output_bfd, info, dyn_i, dynindx,
  3580. rel->r_addend, value, R_IA64_FPTRNNLSB);
  3581. value -= gp_val;
  3582. r = ia64_elf_install_value (hit_addr, value, r_type);
  3583. }
  3584. break;
  3585. case R_IA64_PCREL32MSB:
  3586. case R_IA64_PCREL32LSB:
  3587. case R_IA64_PCREL64MSB:
  3588. case R_IA64_PCREL64LSB:
  3589. /* Install a dynamic relocation for this reloc. */
  3590. if (dynamic_symbol_p && r_symndx != STN_UNDEF)
  3591. {
  3592. BFD_ASSERT (srel != NULL);
  3593. elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
  3594. srel, rel->r_offset, r_type,
  3595. h->dynindx, rel->r_addend);
  3596. }
  3597. goto finish_pcrel;
  3598. case R_IA64_PCREL21B:
  3599. case R_IA64_PCREL60B:
  3600. /* We should have created a PLT entry for any dynamic symbol. */
  3601. dyn_i = NULL;
  3602. if (h)
  3603. dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
  3604. if (dyn_i && dyn_i->want_plt2)
  3605. {
  3606. /* Should have caught this earlier. */
  3607. BFD_ASSERT (rel->r_addend == 0);
  3608. value = (ia64_info->root.splt->output_section->vma
  3609. + ia64_info->root.splt->output_offset
  3610. + dyn_i->plt2_offset);
  3611. }
  3612. else
  3613. {
  3614. /* Since there's no PLT entry, Validate that this is
  3615. locally defined. */
  3616. BFD_ASSERT (undef_weak_ref || sym_sec->output_section != NULL);
  3617. /* If the symbol is undef_weak, we shouldn't be trying
  3618. to call it. There's every chance that we'd wind up
  3619. with an out-of-range fixup here. Don't bother setting
  3620. any value at all. */
  3621. if (undef_weak_ref)
  3622. continue;
  3623. }
  3624. goto finish_pcrel;
  3625. case R_IA64_PCREL21BI:
  3626. case R_IA64_PCREL21F:
  3627. case R_IA64_PCREL21M:
  3628. case R_IA64_PCREL22:
  3629. case R_IA64_PCREL64I:
  3630. /* The PCREL21BI reloc is specifically not intended for use with
  3631. dynamic relocs. PCREL21F and PCREL21M are used for speculation
  3632. fixup code, and thus probably ought not be dynamic. The
  3633. PCREL22 and PCREL64I relocs aren't emitted as dynamic relocs. */
  3634. if (dynamic_symbol_p)
  3635. {
  3636. const char *msg;
  3637. if (r_type == R_IA64_PCREL21BI)
  3638. /* xgettext:c-format */
  3639. msg = _("%pB: @internal branch to dynamic symbol %s");
  3640. else if (r_type == R_IA64_PCREL21F || r_type == R_IA64_PCREL21M)
  3641. /* xgettext:c-format */
  3642. msg = _("%pB: speculation fixup to dynamic symbol %s");
  3643. else
  3644. /* xgettext:c-format */
  3645. msg = _("%pB: @pcrel relocation against dynamic symbol %s");
  3646. _bfd_error_handler (msg, input_bfd,
  3647. h ? h->root.root.string
  3648. : bfd_elf_sym_name (input_bfd,
  3649. symtab_hdr,
  3650. sym,
  3651. sym_sec));
  3652. ret_val = false;
  3653. continue;
  3654. }
  3655. goto finish_pcrel;
  3656. finish_pcrel:
  3657. /* Make pc-relative. */
  3658. value -= (input_section->output_section->vma
  3659. + input_section->output_offset
  3660. + rel->r_offset) & ~ (bfd_vma) 0x3;
  3661. r = ia64_elf_install_value (hit_addr, value, r_type);
  3662. break;
  3663. case R_IA64_SEGREL32MSB:
  3664. case R_IA64_SEGREL32LSB:
  3665. case R_IA64_SEGREL64MSB:
  3666. case R_IA64_SEGREL64LSB:
  3667. {
  3668. /* Find the segment that contains the output_section. */
  3669. Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section
  3670. (output_bfd, input_section->output_section);
  3671. if (p == NULL)
  3672. {
  3673. r = bfd_reloc_notsupported;
  3674. }
  3675. else
  3676. {
  3677. /* The VMA of the segment is the vaddr of the associated
  3678. program header. */
  3679. if (value > p->p_vaddr)
  3680. value -= p->p_vaddr;
  3681. else
  3682. value = 0;
  3683. r = ia64_elf_install_value (hit_addr, value, r_type);
  3684. }
  3685. break;
  3686. }
  3687. case R_IA64_SECREL32MSB:
  3688. case R_IA64_SECREL32LSB:
  3689. case R_IA64_SECREL64MSB:
  3690. case R_IA64_SECREL64LSB:
  3691. /* Make output-section relative to section where the symbol
  3692. is defined. PR 475 */
  3693. if (sym_sec)
  3694. value -= sym_sec->output_section->vma;
  3695. r = ia64_elf_install_value (hit_addr, value, r_type);
  3696. break;
  3697. case R_IA64_IPLTMSB:
  3698. case R_IA64_IPLTLSB:
  3699. /* Install a dynamic relocation for this reloc. */
  3700. if ((dynamic_symbol_p || bfd_link_pic (info))
  3701. && (input_section->flags & SEC_ALLOC) != 0)
  3702. {
  3703. BFD_ASSERT (srel != NULL);
  3704. /* If we don't need dynamic symbol lookup, install two
  3705. RELATIVE relocations. */
  3706. if (!dynamic_symbol_p)
  3707. {
  3708. unsigned int dyn_r_type;
  3709. if (r_type == R_IA64_IPLTMSB)
  3710. dyn_r_type = R_IA64_REL64MSB;
  3711. else
  3712. dyn_r_type = R_IA64_REL64LSB;
  3713. elfNN_ia64_install_dyn_reloc (output_bfd, info,
  3714. input_section,
  3715. srel, rel->r_offset,
  3716. dyn_r_type, 0, value);
  3717. elfNN_ia64_install_dyn_reloc (output_bfd, info,
  3718. input_section,
  3719. srel, rel->r_offset + 8,
  3720. dyn_r_type, 0, gp_val);
  3721. }
  3722. else
  3723. elfNN_ia64_install_dyn_reloc (output_bfd, info, input_section,
  3724. srel, rel->r_offset, r_type,
  3725. h->dynindx, rel->r_addend);
  3726. }
  3727. if (r_type == R_IA64_IPLTMSB)
  3728. r_type = R_IA64_DIR64MSB;
  3729. else
  3730. r_type = R_IA64_DIR64LSB;
  3731. ia64_elf_install_value (hit_addr, value, r_type);
  3732. r = ia64_elf_install_value (hit_addr + 8, gp_val, r_type);
  3733. break;
  3734. case R_IA64_TPREL14:
  3735. case R_IA64_TPREL22:
  3736. case R_IA64_TPREL64I:
  3737. if (elf_hash_table (info)->tls_sec == NULL)
  3738. goto missing_tls_sec;
  3739. value -= elfNN_ia64_tprel_base (info);
  3740. r = ia64_elf_install_value (hit_addr, value, r_type);
  3741. break;
  3742. case R_IA64_DTPREL14:
  3743. case R_IA64_DTPREL22:
  3744. case R_IA64_DTPREL64I:
  3745. case R_IA64_DTPREL32LSB:
  3746. case R_IA64_DTPREL32MSB:
  3747. case R_IA64_DTPREL64LSB:
  3748. case R_IA64_DTPREL64MSB:
  3749. if (elf_hash_table (info)->tls_sec == NULL)
  3750. goto missing_tls_sec;
  3751. value -= elfNN_ia64_dtprel_base (info);
  3752. r = ia64_elf_install_value (hit_addr, value, r_type);
  3753. break;
  3754. case R_IA64_LTOFF_TPREL22:
  3755. case R_IA64_LTOFF_DTPMOD22:
  3756. case R_IA64_LTOFF_DTPREL22:
  3757. {
  3758. int got_r_type;
  3759. long dynindx = h ? h->dynindx : -1;
  3760. bfd_vma r_addend = rel->r_addend;
  3761. switch (r_type)
  3762. {
  3763. default:
  3764. case R_IA64_LTOFF_TPREL22:
  3765. if (!dynamic_symbol_p)
  3766. {
  3767. if (elf_hash_table (info)->tls_sec == NULL)
  3768. goto missing_tls_sec;
  3769. if (!bfd_link_pic (info))
  3770. value -= elfNN_ia64_tprel_base (info);
  3771. else
  3772. {
  3773. r_addend += value - elfNN_ia64_dtprel_base (info);
  3774. dynindx = 0;
  3775. }
  3776. }
  3777. got_r_type = R_IA64_TPREL64LSB;
  3778. break;
  3779. case R_IA64_LTOFF_DTPMOD22:
  3780. if (!dynamic_symbol_p && !bfd_link_pic (info))
  3781. value = 1;
  3782. got_r_type = R_IA64_DTPMOD64LSB;
  3783. break;
  3784. case R_IA64_LTOFF_DTPREL22:
  3785. if (!dynamic_symbol_p)
  3786. {
  3787. if (elf_hash_table (info)->tls_sec == NULL)
  3788. goto missing_tls_sec;
  3789. value -= elfNN_ia64_dtprel_base (info);
  3790. }
  3791. got_r_type = R_IA64_DTPRELNNLSB;
  3792. break;
  3793. }
  3794. dyn_i = get_dyn_sym_info (ia64_info, h, input_bfd, rel, false);
  3795. value = set_got_entry (input_bfd, info, dyn_i, dynindx, r_addend,
  3796. value, got_r_type);
  3797. value -= gp_val;
  3798. r = ia64_elf_install_value (hit_addr, value, r_type);
  3799. }
  3800. break;
  3801. default:
  3802. r = bfd_reloc_notsupported;
  3803. break;
  3804. }
  3805. switch (r)
  3806. {
  3807. case bfd_reloc_ok:
  3808. break;
  3809. case bfd_reloc_undefined:
  3810. /* This can happen for global table relative relocs if
  3811. __gp is undefined. This is a panic situation so we
  3812. don't try to continue. */
  3813. (*info->callbacks->undefined_symbol)
  3814. (info, "__gp", input_bfd, input_section, rel->r_offset, 1);
  3815. return false;
  3816. case bfd_reloc_notsupported:
  3817. {
  3818. const char *name;
  3819. if (h)
  3820. name = h->root.root.string;
  3821. else
  3822. name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
  3823. sym_sec);
  3824. (*info->callbacks->warning) (info, _("unsupported reloc"),
  3825. name, input_bfd,
  3826. input_section, rel->r_offset);
  3827. ret_val = false;
  3828. }
  3829. break;
  3830. case bfd_reloc_dangerous:
  3831. case bfd_reloc_outofrange:
  3832. case bfd_reloc_overflow:
  3833. default:
  3834. missing_tls_sec:
  3835. {
  3836. const char *name;
  3837. if (h)
  3838. name = h->root.root.string;
  3839. else
  3840. name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
  3841. sym_sec);
  3842. switch (r_type)
  3843. {
  3844. case R_IA64_TPREL14:
  3845. case R_IA64_TPREL22:
  3846. case R_IA64_TPREL64I:
  3847. case R_IA64_DTPREL14:
  3848. case R_IA64_DTPREL22:
  3849. case R_IA64_DTPREL64I:
  3850. case R_IA64_DTPREL32LSB:
  3851. case R_IA64_DTPREL32MSB:
  3852. case R_IA64_DTPREL64LSB:
  3853. case R_IA64_DTPREL64MSB:
  3854. case R_IA64_LTOFF_TPREL22:
  3855. case R_IA64_LTOFF_DTPMOD22:
  3856. case R_IA64_LTOFF_DTPREL22:
  3857. _bfd_error_handler
  3858. /* xgettext:c-format */
  3859. (_("%pB: missing TLS section for relocation %s against `%s'"
  3860. " at %#" PRIx64 " in section `%pA'."),
  3861. input_bfd, howto->name, name,
  3862. (uint64_t) rel->r_offset, input_section);
  3863. break;
  3864. case R_IA64_PCREL21B:
  3865. case R_IA64_PCREL21BI:
  3866. case R_IA64_PCREL21M:
  3867. case R_IA64_PCREL21F:
  3868. if (is_elf_hash_table (info->hash))
  3869. {
  3870. /* Relaxtion is always performed for ELF output.
  3871. Overflow failures for those relocations mean
  3872. that the section is too big to relax. */
  3873. _bfd_error_handler
  3874. /* xgettext:c-format */
  3875. (_("%pB: Can't relax br (%s) to `%s' at %#" PRIx64
  3876. " in section `%pA' with size %#" PRIx64
  3877. " (> 0x1000000)."),
  3878. input_bfd, howto->name, name, (uint64_t) rel->r_offset,
  3879. input_section, (uint64_t) input_section->size);
  3880. break;
  3881. }
  3882. /* Fall through. */
  3883. default:
  3884. (*info->callbacks->reloc_overflow) (info,
  3885. &h->root,
  3886. name,
  3887. howto->name,
  3888. (bfd_vma) 0,
  3889. input_bfd,
  3890. input_section,
  3891. rel->r_offset);
  3892. break;
  3893. }
  3894. ret_val = false;
  3895. }
  3896. break;
  3897. }
  3898. }
  3899. return ret_val;
  3900. }
  3901. static bool
  3902. elfNN_ia64_finish_dynamic_symbol (bfd *output_bfd,
  3903. struct bfd_link_info *info,
  3904. struct elf_link_hash_entry *h,
  3905. Elf_Internal_Sym *sym)
  3906. {
  3907. struct elfNN_ia64_link_hash_table *ia64_info;
  3908. struct elfNN_ia64_dyn_sym_info *dyn_i;
  3909. ia64_info = elfNN_ia64_hash_table (info);
  3910. if (ia64_info == NULL)
  3911. return false;
  3912. dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false);
  3913. /* Fill in the PLT data, if required. */
  3914. if (dyn_i && dyn_i->want_plt)
  3915. {
  3916. Elf_Internal_Rela outrel;
  3917. bfd_byte *loc;
  3918. asection *plt_sec;
  3919. bfd_vma plt_addr, pltoff_addr, gp_val, plt_index;
  3920. gp_val = _bfd_get_gp_value (output_bfd);
  3921. /* Initialize the minimal PLT entry. */
  3922. plt_index = (dyn_i->plt_offset - PLT_HEADER_SIZE) / PLT_MIN_ENTRY_SIZE;
  3923. plt_sec = ia64_info->root.splt;
  3924. loc = plt_sec->contents + dyn_i->plt_offset;
  3925. memcpy (loc, plt_min_entry, PLT_MIN_ENTRY_SIZE);
  3926. ia64_elf_install_value (loc, plt_index, R_IA64_IMM22);
  3927. ia64_elf_install_value (loc+2, -dyn_i->plt_offset, R_IA64_PCREL21B);
  3928. plt_addr = (plt_sec->output_section->vma
  3929. + plt_sec->output_offset
  3930. + dyn_i->plt_offset);
  3931. pltoff_addr = set_pltoff_entry (output_bfd, info, dyn_i, plt_addr, true);
  3932. /* Initialize the FULL PLT entry, if needed. */
  3933. if (dyn_i->want_plt2)
  3934. {
  3935. loc = plt_sec->contents + dyn_i->plt2_offset;
  3936. memcpy (loc, plt_full_entry, PLT_FULL_ENTRY_SIZE);
  3937. ia64_elf_install_value (loc, pltoff_addr - gp_val, R_IA64_IMM22);
  3938. /* Mark the symbol as undefined, rather than as defined in the
  3939. plt section. Leave the value alone. */
  3940. /* ??? We didn't redefine it in adjust_dynamic_symbol in the
  3941. first place. But perhaps elflink.c did some for us. */
  3942. if (!h->def_regular)
  3943. sym->st_shndx = SHN_UNDEF;
  3944. }
  3945. /* Create the dynamic relocation. */
  3946. outrel.r_offset = pltoff_addr;
  3947. if (bfd_little_endian (output_bfd))
  3948. outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTLSB);
  3949. else
  3950. outrel.r_info = ELFNN_R_INFO (h->dynindx, R_IA64_IPLTMSB);
  3951. outrel.r_addend = 0;
  3952. /* This is fun. In the .IA_64.pltoff section, we've got entries
  3953. that correspond both to real PLT entries, and those that
  3954. happened to resolve to local symbols but need to be created
  3955. to satisfy @pltoff relocations. The .rela.IA_64.pltoff
  3956. relocations for the real PLT should come at the end of the
  3957. section, so that they can be indexed by plt entry at runtime.
  3958. We emitted all of the relocations for the non-PLT @pltoff
  3959. entries during relocate_section. So we can consider the
  3960. existing sec->reloc_count to be the base of the array of
  3961. PLT relocations. */
  3962. loc = ia64_info->rel_pltoff_sec->contents;
  3963. loc += ((ia64_info->rel_pltoff_sec->reloc_count + plt_index)
  3964. * sizeof (ElfNN_External_Rela));
  3965. bfd_elfNN_swap_reloca_out (output_bfd, &outrel, loc);
  3966. }
  3967. /* Mark some specially defined symbols as absolute. */
  3968. if (h == ia64_info->root.hdynamic
  3969. || h == ia64_info->root.hgot
  3970. || h == ia64_info->root.hplt)
  3971. sym->st_shndx = SHN_ABS;
  3972. return true;
  3973. }
  3974. static bool
  3975. elfNN_ia64_finish_dynamic_sections (bfd *abfd,
  3976. struct bfd_link_info *info)
  3977. {
  3978. struct elfNN_ia64_link_hash_table *ia64_info;
  3979. bfd *dynobj;
  3980. ia64_info = elfNN_ia64_hash_table (info);
  3981. if (ia64_info == NULL)
  3982. return false;
  3983. dynobj = ia64_info->root.dynobj;
  3984. if (ia64_info->root.dynamic_sections_created)
  3985. {
  3986. ElfNN_External_Dyn *dyncon, *dynconend;
  3987. asection *sdyn, *sgotplt;
  3988. bfd_vma gp_val;
  3989. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  3990. sgotplt = ia64_info->root.sgotplt;
  3991. BFD_ASSERT (sdyn != NULL);
  3992. dyncon = (ElfNN_External_Dyn *) sdyn->contents;
  3993. dynconend = (ElfNN_External_Dyn *) (sdyn->contents + sdyn->size);
  3994. gp_val = _bfd_get_gp_value (abfd);
  3995. for (; dyncon < dynconend; dyncon++)
  3996. {
  3997. Elf_Internal_Dyn dyn;
  3998. bfd_elfNN_swap_dyn_in (dynobj, dyncon, &dyn);
  3999. switch (dyn.d_tag)
  4000. {
  4001. case DT_PLTGOT:
  4002. dyn.d_un.d_ptr = gp_val;
  4003. break;
  4004. case DT_PLTRELSZ:
  4005. dyn.d_un.d_val = (ia64_info->minplt_entries
  4006. * sizeof (ElfNN_External_Rela));
  4007. break;
  4008. case DT_JMPREL:
  4009. /* See the comment above in finish_dynamic_symbol. */
  4010. dyn.d_un.d_ptr = (ia64_info->rel_pltoff_sec->output_section->vma
  4011. + ia64_info->rel_pltoff_sec->output_offset
  4012. + (ia64_info->rel_pltoff_sec->reloc_count
  4013. * sizeof (ElfNN_External_Rela)));
  4014. break;
  4015. case DT_IA_64_PLT_RESERVE:
  4016. dyn.d_un.d_ptr = (sgotplt->output_section->vma
  4017. + sgotplt->output_offset);
  4018. break;
  4019. }
  4020. bfd_elfNN_swap_dyn_out (abfd, &dyn, dyncon);
  4021. }
  4022. /* Initialize the PLT0 entry. */
  4023. if (ia64_info->root.splt)
  4024. {
  4025. bfd_byte *loc = ia64_info->root.splt->contents;
  4026. bfd_vma pltres;
  4027. memcpy (loc, plt_header, PLT_HEADER_SIZE);
  4028. pltres = (sgotplt->output_section->vma
  4029. + sgotplt->output_offset
  4030. - gp_val);
  4031. ia64_elf_install_value (loc+1, pltres, R_IA64_GPREL22);
  4032. }
  4033. }
  4034. return true;
  4035. }
  4036. /* ELF file flag handling: */
  4037. /* Function to keep IA-64 specific file flags. */
  4038. static bool
  4039. elfNN_ia64_set_private_flags (bfd *abfd, flagword flags)
  4040. {
  4041. BFD_ASSERT (!elf_flags_init (abfd)
  4042. || elf_elfheader (abfd)->e_flags == flags);
  4043. elf_elfheader (abfd)->e_flags = flags;
  4044. elf_flags_init (abfd) = true;
  4045. return true;
  4046. }
  4047. /* Merge backend specific data from an object file to the output
  4048. object file when linking. */
  4049. static bool
  4050. elfNN_ia64_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  4051. {
  4052. bfd *obfd = info->output_bfd;
  4053. flagword out_flags;
  4054. flagword in_flags;
  4055. bool ok = true;
  4056. /* FIXME: What should be checked when linking shared libraries? */
  4057. if ((ibfd->flags & DYNAMIC) != 0)
  4058. return true;
  4059. if (!is_ia64_elf (ibfd) || !is_ia64_elf (obfd))
  4060. return true;
  4061. in_flags = elf_elfheader (ibfd)->e_flags;
  4062. out_flags = elf_elfheader (obfd)->e_flags;
  4063. if (! elf_flags_init (obfd))
  4064. {
  4065. elf_flags_init (obfd) = true;
  4066. elf_elfheader (obfd)->e_flags = in_flags;
  4067. if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
  4068. && bfd_get_arch_info (obfd)->the_default)
  4069. {
  4070. return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
  4071. bfd_get_mach (ibfd));
  4072. }
  4073. return true;
  4074. }
  4075. /* Check flag compatibility. */
  4076. if (in_flags == out_flags)
  4077. return true;
  4078. /* Output has EF_IA_64_REDUCEDFP set only if all inputs have it set. */
  4079. if (!(in_flags & EF_IA_64_REDUCEDFP) && (out_flags & EF_IA_64_REDUCEDFP))
  4080. elf_elfheader (obfd)->e_flags &= ~EF_IA_64_REDUCEDFP;
  4081. if ((in_flags & EF_IA_64_TRAPNIL) != (out_flags & EF_IA_64_TRAPNIL))
  4082. {
  4083. _bfd_error_handler
  4084. (_("%pB: linking trap-on-NULL-dereference with non-trapping files"),
  4085. ibfd);
  4086. bfd_set_error (bfd_error_bad_value);
  4087. ok = false;
  4088. }
  4089. if ((in_flags & EF_IA_64_BE) != (out_flags & EF_IA_64_BE))
  4090. {
  4091. _bfd_error_handler
  4092. (_("%pB: linking big-endian files with little-endian files"),
  4093. ibfd);
  4094. bfd_set_error (bfd_error_bad_value);
  4095. ok = false;
  4096. }
  4097. if ((in_flags & EF_IA_64_ABI64) != (out_flags & EF_IA_64_ABI64))
  4098. {
  4099. _bfd_error_handler
  4100. (_("%pB: linking 64-bit files with 32-bit files"),
  4101. ibfd);
  4102. bfd_set_error (bfd_error_bad_value);
  4103. ok = false;
  4104. }
  4105. if ((in_flags & EF_IA_64_CONS_GP) != (out_flags & EF_IA_64_CONS_GP))
  4106. {
  4107. _bfd_error_handler
  4108. (_("%pB: linking constant-gp files with non-constant-gp files"),
  4109. ibfd);
  4110. bfd_set_error (bfd_error_bad_value);
  4111. ok = false;
  4112. }
  4113. if ((in_flags & EF_IA_64_NOFUNCDESC_CONS_GP)
  4114. != (out_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
  4115. {
  4116. _bfd_error_handler
  4117. (_("%pB: linking auto-pic files with non-auto-pic files"),
  4118. ibfd);
  4119. bfd_set_error (bfd_error_bad_value);
  4120. ok = false;
  4121. }
  4122. return ok;
  4123. }
  4124. static bool
  4125. elfNN_ia64_print_private_bfd_data (bfd *abfd, void * ptr)
  4126. {
  4127. FILE *file = (FILE *) ptr;
  4128. flagword flags = elf_elfheader (abfd)->e_flags;
  4129. BFD_ASSERT (abfd != NULL && ptr != NULL);
  4130. fprintf (file, "private flags = %s%s%s%s%s%s%s%s\n",
  4131. (flags & EF_IA_64_TRAPNIL) ? "TRAPNIL, " : "",
  4132. (flags & EF_IA_64_EXT) ? "EXT, " : "",
  4133. (flags & EF_IA_64_BE) ? "BE, " : "LE, ",
  4134. (flags & EF_IA_64_REDUCEDFP) ? "REDUCEDFP, " : "",
  4135. (flags & EF_IA_64_CONS_GP) ? "CONS_GP, " : "",
  4136. (flags & EF_IA_64_NOFUNCDESC_CONS_GP) ? "NOFUNCDESC_CONS_GP, " : "",
  4137. (flags & EF_IA_64_ABSOLUTE) ? "ABSOLUTE, " : "",
  4138. (flags & EF_IA_64_ABI64) ? "ABI64" : "ABI32");
  4139. _bfd_elf_print_private_bfd_data (abfd, ptr);
  4140. return true;
  4141. }
  4142. static enum elf_reloc_type_class
  4143. elfNN_ia64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  4144. const asection *rel_sec ATTRIBUTE_UNUSED,
  4145. const Elf_Internal_Rela *rela)
  4146. {
  4147. switch ((int) ELFNN_R_TYPE (rela->r_info))
  4148. {
  4149. case R_IA64_REL32MSB:
  4150. case R_IA64_REL32LSB:
  4151. case R_IA64_REL64MSB:
  4152. case R_IA64_REL64LSB:
  4153. return reloc_class_relative;
  4154. case R_IA64_IPLTMSB:
  4155. case R_IA64_IPLTLSB:
  4156. return reloc_class_plt;
  4157. case R_IA64_COPY:
  4158. return reloc_class_copy;
  4159. default:
  4160. return reloc_class_normal;
  4161. }
  4162. }
  4163. static const struct bfd_elf_special_section elfNN_ia64_special_sections[] =
  4164. {
  4165. { STRING_COMMA_LEN (".sbss"), -1, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
  4166. { STRING_COMMA_LEN (".sdata"), -1, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_IA_64_SHORT },
  4167. { NULL, 0, 0, 0, 0 }
  4168. };
  4169. static bool
  4170. elfNN_ia64_object_p (bfd *abfd)
  4171. {
  4172. asection *sec;
  4173. asection *group, *unwi, *unw;
  4174. flagword flags;
  4175. const char *name;
  4176. char *unwi_name, *unw_name;
  4177. size_t amt;
  4178. if (abfd->flags & DYNAMIC)
  4179. return true;
  4180. /* Flags for fake group section. */
  4181. flags = (SEC_LINKER_CREATED | SEC_GROUP | SEC_LINK_ONCE
  4182. | SEC_EXCLUDE);
  4183. /* We add a fake section group for each .gnu.linkonce.t.* section,
  4184. which isn't in a section group, and its unwind sections. */
  4185. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  4186. {
  4187. if (elf_sec_group (sec) == NULL
  4188. && ((sec->flags & (SEC_LINK_ONCE | SEC_CODE | SEC_GROUP))
  4189. == (SEC_LINK_ONCE | SEC_CODE))
  4190. && startswith (sec->name, ".gnu.linkonce.t."))
  4191. {
  4192. name = sec->name + 16;
  4193. amt = strlen (name) + sizeof (".gnu.linkonce.ia64unwi.");
  4194. unwi_name = bfd_alloc (abfd, amt);
  4195. if (!unwi_name)
  4196. return false;
  4197. strcpy (stpcpy (unwi_name, ".gnu.linkonce.ia64unwi."), name);
  4198. unwi = bfd_get_section_by_name (abfd, unwi_name);
  4199. amt = strlen (name) + sizeof (".gnu.linkonce.ia64unw.");
  4200. unw_name = bfd_alloc (abfd, amt);
  4201. if (!unw_name)
  4202. return false;
  4203. strcpy (stpcpy (unw_name, ".gnu.linkonce.ia64unw."), name);
  4204. unw = bfd_get_section_by_name (abfd, unw_name);
  4205. /* We need to create a fake group section for it and its
  4206. unwind sections. */
  4207. group = bfd_make_section_anyway_with_flags (abfd, name,
  4208. flags);
  4209. if (group == NULL)
  4210. return false;
  4211. /* Move the fake group section to the beginning. */
  4212. bfd_section_list_remove (abfd, group);
  4213. bfd_section_list_prepend (abfd, group);
  4214. elf_next_in_group (group) = sec;
  4215. elf_group_name (sec) = name;
  4216. elf_next_in_group (sec) = sec;
  4217. elf_sec_group (sec) = group;
  4218. if (unwi)
  4219. {
  4220. elf_group_name (unwi) = name;
  4221. elf_next_in_group (unwi) = sec;
  4222. elf_next_in_group (sec) = unwi;
  4223. elf_sec_group (unwi) = group;
  4224. }
  4225. if (unw)
  4226. {
  4227. elf_group_name (unw) = name;
  4228. if (unwi)
  4229. {
  4230. elf_next_in_group (unw) = elf_next_in_group (unwi);
  4231. elf_next_in_group (unwi) = unw;
  4232. }
  4233. else
  4234. {
  4235. elf_next_in_group (unw) = sec;
  4236. elf_next_in_group (sec) = unw;
  4237. }
  4238. elf_sec_group (unw) = group;
  4239. }
  4240. /* Fake SHT_GROUP section header. */
  4241. elf_section_data (group)->this_hdr.bfd_section = group;
  4242. elf_section_data (group)->this_hdr.sh_type = SHT_GROUP;
  4243. }
  4244. }
  4245. return true;
  4246. }
  4247. static bool
  4248. elfNN_ia64_hpux_vec (const bfd_target *vec)
  4249. {
  4250. extern const bfd_target ia64_elfNN_hpux_be_vec;
  4251. return (vec == &ia64_elfNN_hpux_be_vec);
  4252. }
  4253. static bool
  4254. elfNN_hpux_init_file_header (bfd *abfd, struct bfd_link_info *info)
  4255. {
  4256. Elf_Internal_Ehdr *i_ehdrp;
  4257. if (!_bfd_elf_init_file_header (abfd, info))
  4258. return false;
  4259. i_ehdrp = elf_elfheader (abfd);
  4260. i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
  4261. i_ehdrp->e_ident[EI_ABIVERSION] = 1;
  4262. return true;
  4263. }
  4264. static bool
  4265. elfNN_hpux_backend_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
  4266. asection *sec, int *retval)
  4267. {
  4268. if (bfd_is_com_section (sec))
  4269. {
  4270. *retval = SHN_IA_64_ANSI_COMMON;
  4271. return true;
  4272. }
  4273. return false;
  4274. }
  4275. static void
  4276. elfNN_hpux_backend_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
  4277. asymbol *asym)
  4278. {
  4279. elf_symbol_type *elfsym = (elf_symbol_type *) asym;
  4280. switch (elfsym->internal_elf_sym.st_shndx)
  4281. {
  4282. case SHN_IA_64_ANSI_COMMON:
  4283. asym->section = bfd_com_section_ptr;
  4284. asym->value = elfsym->internal_elf_sym.st_size;
  4285. asym->flags &= ~BSF_GLOBAL;
  4286. break;
  4287. }
  4288. }
  4289. static void
  4290. ignore_errors (const char *fmt ATTRIBUTE_UNUSED, ...)
  4291. {
  4292. }
  4293. #define TARGET_LITTLE_SYM ia64_elfNN_le_vec
  4294. #define TARGET_LITTLE_NAME "elfNN-ia64-little"
  4295. #define TARGET_BIG_SYM ia64_elfNN_be_vec
  4296. #define TARGET_BIG_NAME "elfNN-ia64-big"
  4297. #define ELF_ARCH bfd_arch_ia64
  4298. #define ELF_TARGET_ID IA64_ELF_DATA
  4299. #define ELF_MACHINE_CODE EM_IA_64
  4300. #define ELF_MACHINE_ALT1 1999 /* EAS2.3 */
  4301. #define ELF_MACHINE_ALT2 1998 /* EAS2.2 */
  4302. #define ELF_MAXPAGESIZE 0x10000 /* 64KB */
  4303. #define ELF_COMMONPAGESIZE 0x4000 /* 16KB */
  4304. #define elf_backend_section_from_shdr \
  4305. elfNN_ia64_section_from_shdr
  4306. #define elf_backend_section_flags \
  4307. elfNN_ia64_section_flags
  4308. #define elf_backend_fake_sections \
  4309. elfNN_ia64_fake_sections
  4310. #define elf_backend_final_write_processing \
  4311. elfNN_ia64_final_write_processing
  4312. #define elf_backend_add_symbol_hook \
  4313. elfNN_ia64_add_symbol_hook
  4314. #define elf_backend_additional_program_headers \
  4315. elfNN_ia64_additional_program_headers
  4316. #define elf_backend_modify_segment_map \
  4317. elfNN_ia64_modify_segment_map
  4318. #define elf_backend_modify_headers \
  4319. elfNN_ia64_modify_headers
  4320. #define elf_info_to_howto \
  4321. elfNN_ia64_info_to_howto
  4322. #define bfd_elfNN_bfd_reloc_type_lookup \
  4323. ia64_elf_reloc_type_lookup
  4324. #define bfd_elfNN_bfd_reloc_name_lookup \
  4325. ia64_elf_reloc_name_lookup
  4326. #define bfd_elfNN_bfd_is_local_label_name \
  4327. elfNN_ia64_is_local_label_name
  4328. #define bfd_elfNN_bfd_relax_section \
  4329. elfNN_ia64_relax_section
  4330. #define elf_backend_object_p \
  4331. elfNN_ia64_object_p
  4332. /* Stuff for the BFD linker: */
  4333. #define bfd_elfNN_bfd_link_hash_table_create \
  4334. elfNN_ia64_hash_table_create
  4335. #define elf_backend_create_dynamic_sections \
  4336. elfNN_ia64_create_dynamic_sections
  4337. #define elf_backend_check_relocs \
  4338. elfNN_ia64_check_relocs
  4339. #define elf_backend_adjust_dynamic_symbol \
  4340. elfNN_ia64_adjust_dynamic_symbol
  4341. #define elf_backend_size_dynamic_sections \
  4342. elfNN_ia64_size_dynamic_sections
  4343. #define elf_backend_omit_section_dynsym \
  4344. _bfd_elf_omit_section_dynsym_all
  4345. #define elf_backend_relocate_section \
  4346. elfNN_ia64_relocate_section
  4347. #define elf_backend_finish_dynamic_symbol \
  4348. elfNN_ia64_finish_dynamic_symbol
  4349. #define elf_backend_finish_dynamic_sections \
  4350. elfNN_ia64_finish_dynamic_sections
  4351. #define bfd_elfNN_bfd_final_link \
  4352. elfNN_ia64_final_link
  4353. #define bfd_elfNN_bfd_merge_private_bfd_data \
  4354. elfNN_ia64_merge_private_bfd_data
  4355. #define bfd_elfNN_bfd_set_private_flags \
  4356. elfNN_ia64_set_private_flags
  4357. #define bfd_elfNN_bfd_print_private_bfd_data \
  4358. elfNN_ia64_print_private_bfd_data
  4359. #define elf_backend_plt_readonly 1
  4360. #define elf_backend_can_gc_sections 1
  4361. #define elf_backend_want_plt_sym 0
  4362. #define elf_backend_plt_alignment 5
  4363. #define elf_backend_got_header_size 0
  4364. #define elf_backend_want_got_plt 1
  4365. #define elf_backend_may_use_rel_p 1
  4366. #define elf_backend_may_use_rela_p 1
  4367. #define elf_backend_default_use_rela_p 1
  4368. #define elf_backend_want_dynbss 0
  4369. #define elf_backend_copy_indirect_symbol elfNN_ia64_hash_copy_indirect
  4370. #define elf_backend_hide_symbol elfNN_ia64_hash_hide_symbol
  4371. #define elf_backend_fixup_symbol _bfd_elf_link_hash_fixup_symbol
  4372. #define elf_backend_reloc_type_class elfNN_ia64_reloc_type_class
  4373. #define elf_backend_rela_normal 1
  4374. #define elf_backend_dtrel_excludes_plt 1
  4375. #define elf_backend_special_sections elfNN_ia64_special_sections
  4376. #define elf_backend_default_execstack 0
  4377. /* FIXME: PR 290: The Intel C compiler generates SHT_IA_64_UNWIND with
  4378. SHF_LINK_ORDER. But it doesn't set the sh_link or sh_info fields.
  4379. We don't want to flood users with so many error messages. We turn
  4380. off the warning for now. It will be turned on later when the Intel
  4381. compiler is fixed. */
  4382. #define elf_backend_link_order_error_handler ignore_errors
  4383. #include "elfNN-target.h"
  4384. /* HPUX-specific vectors. */
  4385. #undef TARGET_LITTLE_SYM
  4386. #undef TARGET_LITTLE_NAME
  4387. #undef TARGET_BIG_SYM
  4388. #define TARGET_BIG_SYM ia64_elfNN_hpux_be_vec
  4389. #undef TARGET_BIG_NAME
  4390. #define TARGET_BIG_NAME "elfNN-ia64-hpux-big"
  4391. /* These are HP-UX specific functions. */
  4392. #undef elf_backend_init_file_header
  4393. #define elf_backend_init_file_header elfNN_hpux_init_file_header
  4394. #undef elf_backend_section_from_bfd_section
  4395. #define elf_backend_section_from_bfd_section elfNN_hpux_backend_section_from_bfd_section
  4396. #undef elf_backend_symbol_processing
  4397. #define elf_backend_symbol_processing elfNN_hpux_backend_symbol_processing
  4398. #undef elf_backend_want_p_paddr_set_to_zero
  4399. #define elf_backend_want_p_paddr_set_to_zero 1
  4400. #undef ELF_COMMONPAGESIZE
  4401. #undef ELF_OSABI
  4402. #define ELF_OSABI ELFOSABI_HPUX
  4403. #undef elfNN_bed
  4404. #define elfNN_bed elfNN_ia64_hpux_bed
  4405. #include "elfNN-target.h"