elf64-alpha.c 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548
  1. /* Alpha specific support for 64-bit ELF
  2. Copyright (C) 1996-2022 Free Software Foundation, Inc.
  3. Contributed by Richard Henderson <rth@tamu.edu>.
  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. /* We need a published ABI spec for this. Until one comes out, don't
  18. assume this'll remain unchanged forever. */
  19. #include "sysdep.h"
  20. #include "bfd.h"
  21. #include "libbfd.h"
  22. #include "elf-bfd.h"
  23. #include "ecoff-bfd.h"
  24. #include "elf/alpha.h"
  25. #define ALPHAECOFF
  26. #define NO_COFF_RELOCS
  27. #define NO_COFF_SYMBOLS
  28. #define NO_COFF_LINENOS
  29. /* Get the ECOFF swapping routines. Needed for the debug information. */
  30. #include "coff/internal.h"
  31. #include "coff/sym.h"
  32. #include "coff/symconst.h"
  33. #include "coff/ecoff.h"
  34. #include "coff/alpha.h"
  35. #include "aout/ar.h"
  36. #include "libcoff.h"
  37. #include "libecoff.h"
  38. #define ECOFF_64
  39. #include "ecoffswap.h"
  40. /* Instruction data for plt generation and relaxation. */
  41. #define OP_LDA 0x08U
  42. #define OP_LDAH 0x09U
  43. #define OP_LDQ 0x29U
  44. #define OP_BR 0x30U
  45. #define OP_BSR 0x34U
  46. #define INSN_LDA (OP_LDA << 26)
  47. #define INSN_LDAH (OP_LDAH << 26)
  48. #define INSN_LDQ (OP_LDQ << 26)
  49. #define INSN_BR (OP_BR << 26)
  50. #define INSN_ADDQ 0x40000400
  51. #define INSN_RDUNIQ 0x0000009e
  52. #define INSN_SUBQ 0x40000520
  53. #define INSN_S4SUBQ 0x40000560
  54. #define INSN_UNOP 0x2ffe0000
  55. #define INSN_JSR 0x68004000
  56. #define INSN_JMP 0x68000000
  57. #define INSN_JSR_MASK 0xfc00c000
  58. #define INSN_A(I,A) (I | ((unsigned) A << 21))
  59. #define INSN_AB(I,A,B) (INSN_A (I, A) | (B << 16))
  60. #define INSN_ABC(I,A,B,C) (INSN_A (I, A) | (B << 16) | C)
  61. #define INSN_ABO(I,A,B,O) (INSN_A (I, A) | (B << 16) | ((O) & 0xffff))
  62. #define INSN_AD(I,A,D) (INSN_A (I, A) | (((D) >> 2) & 0x1fffff))
  63. /* PLT/GOT Stuff */
  64. /* Set by ld emulation. Putting this into the link_info or hash structure
  65. is simply working too hard. */
  66. #ifdef USE_SECUREPLT
  67. bool elf64_alpha_use_secureplt = true;
  68. #else
  69. bool elf64_alpha_use_secureplt = false;
  70. #endif
  71. #define OLD_PLT_HEADER_SIZE 32
  72. #define OLD_PLT_ENTRY_SIZE 12
  73. #define NEW_PLT_HEADER_SIZE 36
  74. #define NEW_PLT_ENTRY_SIZE 4
  75. #define PLT_HEADER_SIZE \
  76. (elf64_alpha_use_secureplt ? NEW_PLT_HEADER_SIZE : OLD_PLT_HEADER_SIZE)
  77. #define PLT_ENTRY_SIZE \
  78. (elf64_alpha_use_secureplt ? NEW_PLT_ENTRY_SIZE : OLD_PLT_ENTRY_SIZE)
  79. #define MAX_GOT_SIZE (64*1024)
  80. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so"
  81. /* Used to implement multiple .got subsections. */
  82. struct alpha_elf_got_entry
  83. {
  84. struct alpha_elf_got_entry *next;
  85. /* Which .got subsection? */
  86. bfd *gotobj;
  87. /* The addend in effect for this entry. */
  88. bfd_vma addend;
  89. /* The .got offset for this entry. */
  90. int got_offset;
  91. /* The .plt offset for this entry. */
  92. int plt_offset;
  93. /* How many references to this entry? */
  94. int use_count;
  95. /* The relocation type of this entry. */
  96. unsigned char reloc_type;
  97. /* How a LITERAL is used. */
  98. unsigned char flags;
  99. /* Have we initialized the dynamic relocation for this entry? */
  100. unsigned char reloc_done;
  101. /* Have we adjusted this entry for SEC_MERGE? */
  102. unsigned char reloc_xlated;
  103. };
  104. struct alpha_elf_reloc_entry
  105. {
  106. struct alpha_elf_reloc_entry *next;
  107. /* Which .reloc section? */
  108. asection *srel;
  109. /* Which section this relocation is against? */
  110. asection *sec;
  111. /* How many did we find? */
  112. unsigned long count;
  113. /* What kind of relocation? */
  114. unsigned int rtype;
  115. };
  116. struct alpha_elf_link_hash_entry
  117. {
  118. struct elf_link_hash_entry root;
  119. /* External symbol information. */
  120. EXTR esym;
  121. /* Cumulative flags for all the .got entries. */
  122. int flags;
  123. /* Contexts in which a literal was referenced. */
  124. #define ALPHA_ELF_LINK_HASH_LU_ADDR 0x01
  125. #define ALPHA_ELF_LINK_HASH_LU_MEM 0x02
  126. #define ALPHA_ELF_LINK_HASH_LU_BYTE 0x04
  127. #define ALPHA_ELF_LINK_HASH_LU_JSR 0x08
  128. #define ALPHA_ELF_LINK_HASH_LU_TLSGD 0x10
  129. #define ALPHA_ELF_LINK_HASH_LU_TLSLDM 0x20
  130. #define ALPHA_ELF_LINK_HASH_LU_JSRDIRECT 0x40
  131. #define ALPHA_ELF_LINK_HASH_LU_PLT 0x38
  132. #define ALPHA_ELF_LINK_HASH_TLS_IE 0x80
  133. /* Used to implement multiple .got subsections. */
  134. struct alpha_elf_got_entry *got_entries;
  135. /* Used to count non-got, non-plt relocations for delayed sizing
  136. of relocation sections. */
  137. struct alpha_elf_reloc_entry *reloc_entries;
  138. };
  139. /* Alpha ELF linker hash table. */
  140. struct alpha_elf_link_hash_table
  141. {
  142. struct elf_link_hash_table root;
  143. /* The head of a list of .got subsections linked through
  144. alpha_elf_tdata(abfd)->got_link_next. */
  145. bfd *got_list;
  146. /* The most recent relax pass that we've seen. The GOTs
  147. should be regenerated if this doesn't match. */
  148. int relax_trip;
  149. };
  150. /* Look up an entry in a Alpha ELF linker hash table. */
  151. #define alpha_elf_link_hash_lookup(table, string, create, copy, follow) \
  152. ((struct alpha_elf_link_hash_entry *) \
  153. elf_link_hash_lookup (&(table)->root, (string), (create), \
  154. (copy), (follow)))
  155. /* Traverse a Alpha ELF linker hash table. */
  156. #define alpha_elf_link_hash_traverse(table, func, info) \
  157. (elf_link_hash_traverse \
  158. (&(table)->root, \
  159. (bool (*) (struct elf_link_hash_entry *, void *)) (func), \
  160. (info)))
  161. /* Get the Alpha ELF linker hash table from a link_info structure. */
  162. #define alpha_elf_hash_table(p) \
  163. ((is_elf_hash_table ((p)->hash) \
  164. && elf_hash_table_id (elf_hash_table (p)) == ALPHA_ELF_DATA) \
  165. ? (struct alpha_elf_link_hash_table *) (p)->hash : NULL)
  166. /* Get the object's symbols as our own entry type. */
  167. #define alpha_elf_sym_hashes(abfd) \
  168. ((struct alpha_elf_link_hash_entry **)elf_sym_hashes(abfd))
  169. /* Should we do dynamic things to this symbol? This differs from the
  170. generic version in that we never need to consider function pointer
  171. equality wrt PLT entries -- we don't create a PLT entry if a symbol's
  172. address is ever taken. */
  173. static inline bool
  174. alpha_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
  175. struct bfd_link_info *info)
  176. {
  177. return _bfd_elf_dynamic_symbol_p (h, info, 0);
  178. }
  179. /* Create an entry in a Alpha ELF linker hash table. */
  180. static struct bfd_hash_entry *
  181. elf64_alpha_link_hash_newfunc (struct bfd_hash_entry *entry,
  182. struct bfd_hash_table *table,
  183. const char *string)
  184. {
  185. struct alpha_elf_link_hash_entry *ret =
  186. (struct alpha_elf_link_hash_entry *) entry;
  187. /* Allocate the structure if it has not already been allocated by a
  188. subclass. */
  189. if (ret == (struct alpha_elf_link_hash_entry *) NULL)
  190. ret = ((struct alpha_elf_link_hash_entry *)
  191. bfd_hash_allocate (table,
  192. sizeof (struct alpha_elf_link_hash_entry)));
  193. if (ret == (struct alpha_elf_link_hash_entry *) NULL)
  194. return (struct bfd_hash_entry *) ret;
  195. /* Call the allocation method of the superclass. */
  196. ret = ((struct alpha_elf_link_hash_entry *)
  197. _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  198. table, string));
  199. if (ret != (struct alpha_elf_link_hash_entry *) NULL)
  200. {
  201. /* Set local fields. */
  202. memset (&ret->esym, 0, sizeof (EXTR));
  203. /* We use -2 as a marker to indicate that the information has
  204. not been set. -1 means there is no associated ifd. */
  205. ret->esym.ifd = -2;
  206. ret->flags = 0;
  207. ret->got_entries = NULL;
  208. ret->reloc_entries = NULL;
  209. }
  210. return (struct bfd_hash_entry *) ret;
  211. }
  212. /* Create a Alpha ELF linker hash table. */
  213. static struct bfd_link_hash_table *
  214. elf64_alpha_bfd_link_hash_table_create (bfd *abfd)
  215. {
  216. struct alpha_elf_link_hash_table *ret;
  217. size_t amt = sizeof (struct alpha_elf_link_hash_table);
  218. ret = (struct alpha_elf_link_hash_table *) bfd_zmalloc (amt);
  219. if (ret == (struct alpha_elf_link_hash_table *) NULL)
  220. return NULL;
  221. if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
  222. elf64_alpha_link_hash_newfunc,
  223. sizeof (struct alpha_elf_link_hash_entry),
  224. ALPHA_ELF_DATA))
  225. {
  226. free (ret);
  227. return NULL;
  228. }
  229. return &ret->root.root;
  230. }
  231. /* Alpha ELF follows MIPS ELF in using a special find_nearest_line
  232. routine in order to handle the ECOFF debugging information. */
  233. struct alpha_elf_find_line
  234. {
  235. struct ecoff_debug_info d;
  236. struct ecoff_find_line i;
  237. };
  238. /* We have some private fields hanging off of the elf_tdata structure. */
  239. struct alpha_elf_obj_tdata
  240. {
  241. struct elf_obj_tdata root;
  242. /* For every input file, these are the got entries for that object's
  243. local symbols. */
  244. struct alpha_elf_got_entry ** local_got_entries;
  245. /* For every input file, this is the object that owns the got that
  246. this input file uses. */
  247. bfd *gotobj;
  248. /* For every got, this is a linked list through the objects using this got */
  249. bfd *in_got_link_next;
  250. /* For every got, this is a link to the next got subsegment. */
  251. bfd *got_link_next;
  252. /* For every got, this is the section. */
  253. asection *got;
  254. /* For every got, this is it's total number of words. */
  255. int total_got_size;
  256. /* For every got, this is the sum of the number of words required
  257. to hold all of the member object's local got. */
  258. int local_got_size;
  259. /* Used by elf64_alpha_find_nearest_line entry point. */
  260. struct alpha_elf_find_line *find_line_info;
  261. };
  262. #define alpha_elf_tdata(abfd) \
  263. ((struct alpha_elf_obj_tdata *) (abfd)->tdata.any)
  264. #define is_alpha_elf(bfd) \
  265. (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
  266. && elf_tdata (bfd) != NULL \
  267. && elf_object_id (bfd) == ALPHA_ELF_DATA)
  268. static bool
  269. elf64_alpha_mkobject (bfd *abfd)
  270. {
  271. return bfd_elf_allocate_object (abfd, sizeof (struct alpha_elf_obj_tdata),
  272. ALPHA_ELF_DATA);
  273. }
  274. static bool
  275. elf64_alpha_object_p (bfd *abfd)
  276. {
  277. /* Set the right machine number for an Alpha ELF file. */
  278. return bfd_default_set_arch_mach (abfd, bfd_arch_alpha, 0);
  279. }
  280. /* A relocation function which doesn't do anything. */
  281. static bfd_reloc_status_type
  282. elf64_alpha_reloc_nil (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc,
  283. asymbol *sym ATTRIBUTE_UNUSED,
  284. void * data ATTRIBUTE_UNUSED, asection *sec,
  285. bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
  286. {
  287. if (output_bfd)
  288. reloc->address += sec->output_offset;
  289. return bfd_reloc_ok;
  290. }
  291. /* A relocation function used for an unsupported reloc. */
  292. static bfd_reloc_status_type
  293. elf64_alpha_reloc_bad (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc,
  294. asymbol *sym ATTRIBUTE_UNUSED,
  295. void * data ATTRIBUTE_UNUSED, asection *sec,
  296. bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
  297. {
  298. if (output_bfd)
  299. reloc->address += sec->output_offset;
  300. return bfd_reloc_notsupported;
  301. }
  302. /* Do the work of the GPDISP relocation. */
  303. static bfd_reloc_status_type
  304. elf64_alpha_do_reloc_gpdisp (bfd *abfd, bfd_vma gpdisp, bfd_byte *p_ldah,
  305. bfd_byte *p_lda)
  306. {
  307. bfd_reloc_status_type ret = bfd_reloc_ok;
  308. bfd_vma addend;
  309. unsigned long i_ldah, i_lda;
  310. i_ldah = bfd_get_32 (abfd, p_ldah);
  311. i_lda = bfd_get_32 (abfd, p_lda);
  312. /* Complain if the instructions are not correct. */
  313. if (((i_ldah >> 26) & 0x3f) != 0x09
  314. || ((i_lda >> 26) & 0x3f) != 0x08)
  315. ret = bfd_reloc_dangerous;
  316. /* Extract the user-supplied offset, mirroring the sign extensions
  317. that the instructions perform. */
  318. addend = ((i_ldah & 0xffff) << 16) | (i_lda & 0xffff);
  319. addend = (addend ^ 0x80008000) - 0x80008000;
  320. gpdisp += addend;
  321. if ((bfd_signed_vma) gpdisp < -(bfd_signed_vma) 0x80000000
  322. || (bfd_signed_vma) gpdisp >= (bfd_signed_vma) 0x7fff8000)
  323. ret = bfd_reloc_overflow;
  324. /* compensate for the sign extension again. */
  325. i_ldah = ((i_ldah & 0xffff0000)
  326. | (((gpdisp >> 16) + ((gpdisp >> 15) & 1)) & 0xffff));
  327. i_lda = (i_lda & 0xffff0000) | (gpdisp & 0xffff);
  328. bfd_put_32 (abfd, (bfd_vma) i_ldah, p_ldah);
  329. bfd_put_32 (abfd, (bfd_vma) i_lda, p_lda);
  330. return ret;
  331. }
  332. /* The special function for the GPDISP reloc. */
  333. static bfd_reloc_status_type
  334. elf64_alpha_reloc_gpdisp (bfd *abfd, arelent *reloc_entry,
  335. asymbol *sym ATTRIBUTE_UNUSED, void * data,
  336. asection *input_section, bfd *output_bfd,
  337. char **err_msg)
  338. {
  339. bfd_reloc_status_type ret;
  340. bfd_vma gp, relocation;
  341. bfd_vma high_address;
  342. bfd_byte *p_ldah, *p_lda;
  343. /* Don't do anything if we're not doing a final link. */
  344. if (output_bfd)
  345. {
  346. reloc_entry->address += input_section->output_offset;
  347. return bfd_reloc_ok;
  348. }
  349. high_address = bfd_get_section_limit (abfd, input_section);
  350. if (reloc_entry->address > high_address
  351. || reloc_entry->address + reloc_entry->addend > high_address)
  352. return bfd_reloc_outofrange;
  353. /* The gp used in the portion of the output object to which this
  354. input object belongs is cached on the input bfd. */
  355. gp = _bfd_get_gp_value (abfd);
  356. relocation = (input_section->output_section->vma
  357. + input_section->output_offset
  358. + reloc_entry->address);
  359. p_ldah = (bfd_byte *) data + reloc_entry->address;
  360. p_lda = p_ldah + reloc_entry->addend;
  361. ret = elf64_alpha_do_reloc_gpdisp (abfd, gp - relocation, p_ldah, p_lda);
  362. /* Complain if the instructions are not correct. */
  363. if (ret == bfd_reloc_dangerous)
  364. *err_msg = _("GPDISP relocation did not find ldah and lda instructions");
  365. return ret;
  366. }
  367. /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
  368. from smaller values. Start with zero, widen, *then* decrement. */
  369. #define MINUS_ONE (((bfd_vma)0) - 1)
  370. #define SKIP_HOWTO(N) \
  371. HOWTO(N, 0, 0, 0, 0, 0, complain_overflow_dont, elf64_alpha_reloc_bad, 0, 0, 0, 0, 0)
  372. static reloc_howto_type elf64_alpha_howto_table[] =
  373. {
  374. HOWTO (R_ALPHA_NONE, /* type */
  375. 0, /* rightshift */
  376. 3, /* size (0 = byte, 1 = short, 2 = long) */
  377. 0, /* bitsize */
  378. true, /* pc_relative */
  379. 0, /* bitpos */
  380. complain_overflow_dont, /* complain_on_overflow */
  381. elf64_alpha_reloc_nil, /* special_function */
  382. "NONE", /* name */
  383. false, /* partial_inplace */
  384. 0, /* src_mask */
  385. 0, /* dst_mask */
  386. true), /* pcrel_offset */
  387. /* A 32 bit reference to a symbol. */
  388. HOWTO (R_ALPHA_REFLONG, /* type */
  389. 0, /* rightshift */
  390. 2, /* size (0 = byte, 1 = short, 2 = long) */
  391. 32, /* bitsize */
  392. false, /* pc_relative */
  393. 0, /* bitpos */
  394. complain_overflow_bitfield, /* complain_on_overflow */
  395. bfd_elf_generic_reloc, /* special_function */
  396. "REFLONG", /* name */
  397. false, /* partial_inplace */
  398. 0xffffffff, /* src_mask */
  399. 0xffffffff, /* dst_mask */
  400. false), /* pcrel_offset */
  401. /* A 64 bit reference to a symbol. */
  402. HOWTO (R_ALPHA_REFQUAD, /* type */
  403. 0, /* rightshift */
  404. 4, /* size (0 = byte, 1 = short, 2 = long) */
  405. 64, /* bitsize */
  406. false, /* pc_relative */
  407. 0, /* bitpos */
  408. complain_overflow_bitfield, /* complain_on_overflow */
  409. bfd_elf_generic_reloc, /* special_function */
  410. "REFQUAD", /* name */
  411. false, /* partial_inplace */
  412. MINUS_ONE, /* src_mask */
  413. MINUS_ONE, /* dst_mask */
  414. false), /* pcrel_offset */
  415. /* A 32 bit GP relative offset. This is just like REFLONG except
  416. that when the value is used the value of the gp register will be
  417. added in. */
  418. HOWTO (R_ALPHA_GPREL32, /* type */
  419. 0, /* rightshift */
  420. 2, /* size (0 = byte, 1 = short, 2 = long) */
  421. 32, /* bitsize */
  422. false, /* pc_relative */
  423. 0, /* bitpos */
  424. complain_overflow_bitfield, /* complain_on_overflow */
  425. bfd_elf_generic_reloc, /* special_function */
  426. "GPREL32", /* name */
  427. false, /* partial_inplace */
  428. 0xffffffff, /* src_mask */
  429. 0xffffffff, /* dst_mask */
  430. false), /* pcrel_offset */
  431. /* Used for an instruction that refers to memory off the GP register. */
  432. HOWTO (R_ALPHA_LITERAL, /* type */
  433. 0, /* rightshift */
  434. 1, /* size (0 = byte, 1 = short, 2 = long) */
  435. 16, /* bitsize */
  436. false, /* pc_relative */
  437. 0, /* bitpos */
  438. complain_overflow_signed, /* complain_on_overflow */
  439. bfd_elf_generic_reloc, /* special_function */
  440. "ELF_LITERAL", /* name */
  441. false, /* partial_inplace */
  442. 0xffff, /* src_mask */
  443. 0xffff, /* dst_mask */
  444. false), /* pcrel_offset */
  445. /* This reloc only appears immediately following an ELF_LITERAL reloc.
  446. It identifies a use of the literal. The symbol index is special:
  447. 1 means the literal address is in the base register of a memory
  448. format instruction; 2 means the literal address is in the byte
  449. offset register of a byte-manipulation instruction; 3 means the
  450. literal address is in the target register of a jsr instruction.
  451. This does not actually do any relocation. */
  452. HOWTO (R_ALPHA_LITUSE, /* type */
  453. 0, /* rightshift */
  454. 1, /* size (0 = byte, 1 = short, 2 = long) */
  455. 32, /* bitsize */
  456. false, /* pc_relative */
  457. 0, /* bitpos */
  458. complain_overflow_dont, /* complain_on_overflow */
  459. elf64_alpha_reloc_nil, /* special_function */
  460. "LITUSE", /* name */
  461. false, /* partial_inplace */
  462. 0, /* src_mask */
  463. 0, /* dst_mask */
  464. false), /* pcrel_offset */
  465. /* Load the gp register. This is always used for a ldah instruction
  466. which loads the upper 16 bits of the gp register. The symbol
  467. index of the GPDISP instruction is an offset in bytes to the lda
  468. instruction that loads the lower 16 bits. The value to use for
  469. the relocation is the difference between the GP value and the
  470. current location; the load will always be done against a register
  471. holding the current address.
  472. NOTE: Unlike ECOFF, partial in-place relocation is not done. If
  473. any offset is present in the instructions, it is an offset from
  474. the register to the ldah instruction. This lets us avoid any
  475. stupid hackery like inventing a gp value to do partial relocation
  476. against. Also unlike ECOFF, we do the whole relocation off of
  477. the GPDISP rather than a GPDISP_HI16/GPDISP_LO16 pair. An odd,
  478. space consuming bit, that, since all the information was present
  479. in the GPDISP_HI16 reloc. */
  480. HOWTO (R_ALPHA_GPDISP, /* type */
  481. 16, /* rightshift */
  482. 2, /* size (0 = byte, 1 = short, 2 = long) */
  483. 16, /* bitsize */
  484. false, /* pc_relative */
  485. 0, /* bitpos */
  486. complain_overflow_dont, /* complain_on_overflow */
  487. elf64_alpha_reloc_gpdisp, /* special_function */
  488. "GPDISP", /* name */
  489. false, /* partial_inplace */
  490. 0xffff, /* src_mask */
  491. 0xffff, /* dst_mask */
  492. true), /* pcrel_offset */
  493. /* A 21 bit branch. */
  494. HOWTO (R_ALPHA_BRADDR, /* type */
  495. 2, /* rightshift */
  496. 2, /* size (0 = byte, 1 = short, 2 = long) */
  497. 21, /* bitsize */
  498. true, /* pc_relative */
  499. 0, /* bitpos */
  500. complain_overflow_signed, /* complain_on_overflow */
  501. bfd_elf_generic_reloc, /* special_function */
  502. "BRADDR", /* name */
  503. false, /* partial_inplace */
  504. 0x1fffff, /* src_mask */
  505. 0x1fffff, /* dst_mask */
  506. true), /* pcrel_offset */
  507. /* A hint for a jump to a register. */
  508. HOWTO (R_ALPHA_HINT, /* type */
  509. 2, /* rightshift */
  510. 1, /* size (0 = byte, 1 = short, 2 = long) */
  511. 14, /* bitsize */
  512. true, /* pc_relative */
  513. 0, /* bitpos */
  514. complain_overflow_dont, /* complain_on_overflow */
  515. bfd_elf_generic_reloc, /* special_function */
  516. "HINT", /* name */
  517. false, /* partial_inplace */
  518. 0x3fff, /* src_mask */
  519. 0x3fff, /* dst_mask */
  520. true), /* pcrel_offset */
  521. /* 16 bit PC relative offset. */
  522. HOWTO (R_ALPHA_SREL16, /* type */
  523. 0, /* rightshift */
  524. 1, /* size (0 = byte, 1 = short, 2 = long) */
  525. 16, /* bitsize */
  526. true, /* pc_relative */
  527. 0, /* bitpos */
  528. complain_overflow_signed, /* complain_on_overflow */
  529. bfd_elf_generic_reloc, /* special_function */
  530. "SREL16", /* name */
  531. false, /* partial_inplace */
  532. 0xffff, /* src_mask */
  533. 0xffff, /* dst_mask */
  534. true), /* pcrel_offset */
  535. /* 32 bit PC relative offset. */
  536. HOWTO (R_ALPHA_SREL32, /* type */
  537. 0, /* rightshift */
  538. 2, /* size (0 = byte, 1 = short, 2 = long) */
  539. 32, /* bitsize */
  540. true, /* pc_relative */
  541. 0, /* bitpos */
  542. complain_overflow_signed, /* complain_on_overflow */
  543. bfd_elf_generic_reloc, /* special_function */
  544. "SREL32", /* name */
  545. false, /* partial_inplace */
  546. 0xffffffff, /* src_mask */
  547. 0xffffffff, /* dst_mask */
  548. true), /* pcrel_offset */
  549. /* A 64 bit PC relative offset. */
  550. HOWTO (R_ALPHA_SREL64, /* type */
  551. 0, /* rightshift */
  552. 4, /* size (0 = byte, 1 = short, 2 = long) */
  553. 64, /* bitsize */
  554. true, /* pc_relative */
  555. 0, /* bitpos */
  556. complain_overflow_signed, /* complain_on_overflow */
  557. bfd_elf_generic_reloc, /* special_function */
  558. "SREL64", /* name */
  559. false, /* partial_inplace */
  560. MINUS_ONE, /* src_mask */
  561. MINUS_ONE, /* dst_mask */
  562. true), /* pcrel_offset */
  563. /* Skip 12 - 16; deprecated ECOFF relocs. */
  564. SKIP_HOWTO (12),
  565. SKIP_HOWTO (13),
  566. SKIP_HOWTO (14),
  567. SKIP_HOWTO (15),
  568. SKIP_HOWTO (16),
  569. /* The high 16 bits of the displacement from GP to the target. */
  570. HOWTO (R_ALPHA_GPRELHIGH,
  571. 0, /* rightshift */
  572. 1, /* size (0 = byte, 1 = short, 2 = long) */
  573. 16, /* bitsize */
  574. false, /* pc_relative */
  575. 0, /* bitpos */
  576. complain_overflow_signed, /* complain_on_overflow */
  577. bfd_elf_generic_reloc, /* special_function */
  578. "GPRELHIGH", /* name */
  579. false, /* partial_inplace */
  580. 0xffff, /* src_mask */
  581. 0xffff, /* dst_mask */
  582. false), /* pcrel_offset */
  583. /* The low 16 bits of the displacement from GP to the target. */
  584. HOWTO (R_ALPHA_GPRELLOW,
  585. 0, /* rightshift */
  586. 1, /* size (0 = byte, 1 = short, 2 = long) */
  587. 16, /* bitsize */
  588. false, /* pc_relative */
  589. 0, /* bitpos */
  590. complain_overflow_dont, /* complain_on_overflow */
  591. bfd_elf_generic_reloc, /* special_function */
  592. "GPRELLOW", /* name */
  593. false, /* partial_inplace */
  594. 0xffff, /* src_mask */
  595. 0xffff, /* dst_mask */
  596. false), /* pcrel_offset */
  597. /* A 16-bit displacement from the GP to the target. */
  598. HOWTO (R_ALPHA_GPREL16,
  599. 0, /* rightshift */
  600. 1, /* size (0 = byte, 1 = short, 2 = long) */
  601. 16, /* bitsize */
  602. false, /* pc_relative */
  603. 0, /* bitpos */
  604. complain_overflow_signed, /* complain_on_overflow */
  605. bfd_elf_generic_reloc, /* special_function */
  606. "GPREL16", /* name */
  607. false, /* partial_inplace */
  608. 0xffff, /* src_mask */
  609. 0xffff, /* dst_mask */
  610. false), /* pcrel_offset */
  611. /* Skip 20 - 23; deprecated ECOFF relocs. */
  612. SKIP_HOWTO (20),
  613. SKIP_HOWTO (21),
  614. SKIP_HOWTO (22),
  615. SKIP_HOWTO (23),
  616. /* Misc ELF relocations. */
  617. /* A dynamic relocation to copy the target into our .dynbss section. */
  618. /* Not generated, as all Alpha objects use PIC, so it is not needed. It
  619. is present because every other ELF has one, but should not be used
  620. because .dynbss is an ugly thing. */
  621. HOWTO (R_ALPHA_COPY,
  622. 0,
  623. 0,
  624. 0,
  625. false,
  626. 0,
  627. complain_overflow_dont,
  628. bfd_elf_generic_reloc,
  629. "COPY",
  630. false,
  631. 0,
  632. 0,
  633. true),
  634. /* A dynamic relocation for a .got entry. */
  635. HOWTO (R_ALPHA_GLOB_DAT,
  636. 0,
  637. 0,
  638. 0,
  639. false,
  640. 0,
  641. complain_overflow_dont,
  642. bfd_elf_generic_reloc,
  643. "GLOB_DAT",
  644. false,
  645. 0,
  646. 0,
  647. true),
  648. /* A dynamic relocation for a .plt entry. */
  649. HOWTO (R_ALPHA_JMP_SLOT,
  650. 0,
  651. 0,
  652. 0,
  653. false,
  654. 0,
  655. complain_overflow_dont,
  656. bfd_elf_generic_reloc,
  657. "JMP_SLOT",
  658. false,
  659. 0,
  660. 0,
  661. true),
  662. /* A dynamic relocation to add the base of the DSO to a 64-bit field. */
  663. HOWTO (R_ALPHA_RELATIVE,
  664. 0,
  665. 0,
  666. 0,
  667. false,
  668. 0,
  669. complain_overflow_dont,
  670. bfd_elf_generic_reloc,
  671. "RELATIVE",
  672. false,
  673. 0,
  674. 0,
  675. true),
  676. /* A 21 bit branch that adjusts for gp loads. */
  677. HOWTO (R_ALPHA_BRSGP, /* type */
  678. 2, /* rightshift */
  679. 2, /* size (0 = byte, 1 = short, 2 = long) */
  680. 21, /* bitsize */
  681. true, /* pc_relative */
  682. 0, /* bitpos */
  683. complain_overflow_signed, /* complain_on_overflow */
  684. bfd_elf_generic_reloc, /* special_function */
  685. "BRSGP", /* name */
  686. false, /* partial_inplace */
  687. 0x1fffff, /* src_mask */
  688. 0x1fffff, /* dst_mask */
  689. true), /* pcrel_offset */
  690. /* Creates a tls_index for the symbol in the got. */
  691. HOWTO (R_ALPHA_TLSGD, /* type */
  692. 0, /* rightshift */
  693. 1, /* size (0 = byte, 1 = short, 2 = long) */
  694. 16, /* bitsize */
  695. false, /* pc_relative */
  696. 0, /* bitpos */
  697. complain_overflow_signed, /* complain_on_overflow */
  698. bfd_elf_generic_reloc, /* special_function */
  699. "TLSGD", /* name */
  700. false, /* partial_inplace */
  701. 0xffff, /* src_mask */
  702. 0xffff, /* dst_mask */
  703. false), /* pcrel_offset */
  704. /* Creates a tls_index for the (current) module in the got. */
  705. HOWTO (R_ALPHA_TLSLDM, /* type */
  706. 0, /* rightshift */
  707. 1, /* size (0 = byte, 1 = short, 2 = long) */
  708. 16, /* bitsize */
  709. false, /* pc_relative */
  710. 0, /* bitpos */
  711. complain_overflow_signed, /* complain_on_overflow */
  712. bfd_elf_generic_reloc, /* special_function */
  713. "TLSLDM", /* name */
  714. false, /* partial_inplace */
  715. 0xffff, /* src_mask */
  716. 0xffff, /* dst_mask */
  717. false), /* pcrel_offset */
  718. /* A dynamic relocation for a DTP module entry. */
  719. HOWTO (R_ALPHA_DTPMOD64, /* type */
  720. 0, /* rightshift */
  721. 4, /* size (0 = byte, 1 = short, 2 = long) */
  722. 64, /* bitsize */
  723. false, /* pc_relative */
  724. 0, /* bitpos */
  725. complain_overflow_bitfield, /* complain_on_overflow */
  726. bfd_elf_generic_reloc, /* special_function */
  727. "DTPMOD64", /* name */
  728. false, /* partial_inplace */
  729. MINUS_ONE, /* src_mask */
  730. MINUS_ONE, /* dst_mask */
  731. false), /* pcrel_offset */
  732. /* Creates a 64-bit offset in the got for the displacement
  733. from DTP to the target. */
  734. HOWTO (R_ALPHA_GOTDTPREL, /* type */
  735. 0, /* rightshift */
  736. 1, /* size (0 = byte, 1 = short, 2 = long) */
  737. 16, /* bitsize */
  738. false, /* pc_relative */
  739. 0, /* bitpos */
  740. complain_overflow_signed, /* complain_on_overflow */
  741. bfd_elf_generic_reloc, /* special_function */
  742. "GOTDTPREL", /* name */
  743. false, /* partial_inplace */
  744. 0xffff, /* src_mask */
  745. 0xffff, /* dst_mask */
  746. false), /* pcrel_offset */
  747. /* A dynamic relocation for a displacement from DTP to the target. */
  748. HOWTO (R_ALPHA_DTPREL64, /* type */
  749. 0, /* rightshift */
  750. 4, /* size (0 = byte, 1 = short, 2 = long) */
  751. 64, /* bitsize */
  752. false, /* pc_relative */
  753. 0, /* bitpos */
  754. complain_overflow_bitfield, /* complain_on_overflow */
  755. bfd_elf_generic_reloc, /* special_function */
  756. "DTPREL64", /* name */
  757. false, /* partial_inplace */
  758. MINUS_ONE, /* src_mask */
  759. MINUS_ONE, /* dst_mask */
  760. false), /* pcrel_offset */
  761. /* The high 16 bits of the displacement from DTP to the target. */
  762. HOWTO (R_ALPHA_DTPRELHI, /* type */
  763. 0, /* rightshift */
  764. 1, /* size (0 = byte, 1 = short, 2 = long) */
  765. 16, /* bitsize */
  766. false, /* pc_relative */
  767. 0, /* bitpos */
  768. complain_overflow_signed, /* complain_on_overflow */
  769. bfd_elf_generic_reloc, /* special_function */
  770. "DTPRELHI", /* name */
  771. false, /* partial_inplace */
  772. 0xffff, /* src_mask */
  773. 0xffff, /* dst_mask */
  774. false), /* pcrel_offset */
  775. /* The low 16 bits of the displacement from DTP to the target. */
  776. HOWTO (R_ALPHA_DTPRELLO, /* type */
  777. 0, /* rightshift */
  778. 1, /* size (0 = byte, 1 = short, 2 = long) */
  779. 16, /* bitsize */
  780. false, /* pc_relative */
  781. 0, /* bitpos */
  782. complain_overflow_dont, /* complain_on_overflow */
  783. bfd_elf_generic_reloc, /* special_function */
  784. "DTPRELLO", /* name */
  785. false, /* partial_inplace */
  786. 0xffff, /* src_mask */
  787. 0xffff, /* dst_mask */
  788. false), /* pcrel_offset */
  789. /* A 16-bit displacement from DTP to the target. */
  790. HOWTO (R_ALPHA_DTPREL16, /* type */
  791. 0, /* rightshift */
  792. 1, /* size (0 = byte, 1 = short, 2 = long) */
  793. 16, /* bitsize */
  794. false, /* pc_relative */
  795. 0, /* bitpos */
  796. complain_overflow_signed, /* complain_on_overflow */
  797. bfd_elf_generic_reloc, /* special_function */
  798. "DTPREL16", /* name */
  799. false, /* partial_inplace */
  800. 0xffff, /* src_mask */
  801. 0xffff, /* dst_mask */
  802. false), /* pcrel_offset */
  803. /* Creates a 64-bit offset in the got for the displacement
  804. from TP to the target. */
  805. HOWTO (R_ALPHA_GOTTPREL, /* type */
  806. 0, /* rightshift */
  807. 1, /* size (0 = byte, 1 = short, 2 = long) */
  808. 16, /* bitsize */
  809. false, /* pc_relative */
  810. 0, /* bitpos */
  811. complain_overflow_signed, /* complain_on_overflow */
  812. bfd_elf_generic_reloc, /* special_function */
  813. "GOTTPREL", /* name */
  814. false, /* partial_inplace */
  815. 0xffff, /* src_mask */
  816. 0xffff, /* dst_mask */
  817. false), /* pcrel_offset */
  818. /* A dynamic relocation for a displacement from TP to the target. */
  819. HOWTO (R_ALPHA_TPREL64, /* type */
  820. 0, /* rightshift */
  821. 4, /* size (0 = byte, 1 = short, 2 = long) */
  822. 64, /* bitsize */
  823. false, /* pc_relative */
  824. 0, /* bitpos */
  825. complain_overflow_bitfield, /* complain_on_overflow */
  826. bfd_elf_generic_reloc, /* special_function */
  827. "TPREL64", /* name */
  828. false, /* partial_inplace */
  829. MINUS_ONE, /* src_mask */
  830. MINUS_ONE, /* dst_mask */
  831. false), /* pcrel_offset */
  832. /* The high 16 bits of the displacement from TP to the target. */
  833. HOWTO (R_ALPHA_TPRELHI, /* type */
  834. 0, /* rightshift */
  835. 1, /* size (0 = byte, 1 = short, 2 = long) */
  836. 16, /* bitsize */
  837. false, /* pc_relative */
  838. 0, /* bitpos */
  839. complain_overflow_signed, /* complain_on_overflow */
  840. bfd_elf_generic_reloc, /* special_function */
  841. "TPRELHI", /* name */
  842. false, /* partial_inplace */
  843. 0xffff, /* src_mask */
  844. 0xffff, /* dst_mask */
  845. false), /* pcrel_offset */
  846. /* The low 16 bits of the displacement from TP to the target. */
  847. HOWTO (R_ALPHA_TPRELLO, /* type */
  848. 0, /* rightshift */
  849. 1, /* size (0 = byte, 1 = short, 2 = long) */
  850. 16, /* bitsize */
  851. false, /* pc_relative */
  852. 0, /* bitpos */
  853. complain_overflow_dont, /* complain_on_overflow */
  854. bfd_elf_generic_reloc, /* special_function */
  855. "TPRELLO", /* name */
  856. false, /* partial_inplace */
  857. 0xffff, /* src_mask */
  858. 0xffff, /* dst_mask */
  859. false), /* pcrel_offset */
  860. /* A 16-bit displacement from TP to the target. */
  861. HOWTO (R_ALPHA_TPREL16, /* type */
  862. 0, /* rightshift */
  863. 1, /* size (0 = byte, 1 = short, 2 = long) */
  864. 16, /* bitsize */
  865. false, /* pc_relative */
  866. 0, /* bitpos */
  867. complain_overflow_signed, /* complain_on_overflow */
  868. bfd_elf_generic_reloc, /* special_function */
  869. "TPREL16", /* name */
  870. false, /* partial_inplace */
  871. 0xffff, /* src_mask */
  872. 0xffff, /* dst_mask */
  873. false), /* pcrel_offset */
  874. };
  875. /* A mapping from BFD reloc types to Alpha ELF reloc types. */
  876. struct elf_reloc_map
  877. {
  878. bfd_reloc_code_real_type bfd_reloc_val;
  879. int elf_reloc_val;
  880. };
  881. static const struct elf_reloc_map elf64_alpha_reloc_map[] =
  882. {
  883. {BFD_RELOC_NONE, R_ALPHA_NONE},
  884. {BFD_RELOC_32, R_ALPHA_REFLONG},
  885. {BFD_RELOC_64, R_ALPHA_REFQUAD},
  886. {BFD_RELOC_CTOR, R_ALPHA_REFQUAD},
  887. {BFD_RELOC_GPREL32, R_ALPHA_GPREL32},
  888. {BFD_RELOC_ALPHA_ELF_LITERAL, R_ALPHA_LITERAL},
  889. {BFD_RELOC_ALPHA_LITUSE, R_ALPHA_LITUSE},
  890. {BFD_RELOC_ALPHA_GPDISP, R_ALPHA_GPDISP},
  891. {BFD_RELOC_23_PCREL_S2, R_ALPHA_BRADDR},
  892. {BFD_RELOC_ALPHA_HINT, R_ALPHA_HINT},
  893. {BFD_RELOC_16_PCREL, R_ALPHA_SREL16},
  894. {BFD_RELOC_32_PCREL, R_ALPHA_SREL32},
  895. {BFD_RELOC_64_PCREL, R_ALPHA_SREL64},
  896. {BFD_RELOC_ALPHA_GPREL_HI16, R_ALPHA_GPRELHIGH},
  897. {BFD_RELOC_ALPHA_GPREL_LO16, R_ALPHA_GPRELLOW},
  898. {BFD_RELOC_GPREL16, R_ALPHA_GPREL16},
  899. {BFD_RELOC_ALPHA_BRSGP, R_ALPHA_BRSGP},
  900. {BFD_RELOC_ALPHA_TLSGD, R_ALPHA_TLSGD},
  901. {BFD_RELOC_ALPHA_TLSLDM, R_ALPHA_TLSLDM},
  902. {BFD_RELOC_ALPHA_DTPMOD64, R_ALPHA_DTPMOD64},
  903. {BFD_RELOC_ALPHA_GOTDTPREL16, R_ALPHA_GOTDTPREL},
  904. {BFD_RELOC_ALPHA_DTPREL64, R_ALPHA_DTPREL64},
  905. {BFD_RELOC_ALPHA_DTPREL_HI16, R_ALPHA_DTPRELHI},
  906. {BFD_RELOC_ALPHA_DTPREL_LO16, R_ALPHA_DTPRELLO},
  907. {BFD_RELOC_ALPHA_DTPREL16, R_ALPHA_DTPREL16},
  908. {BFD_RELOC_ALPHA_GOTTPREL16, R_ALPHA_GOTTPREL},
  909. {BFD_RELOC_ALPHA_TPREL64, R_ALPHA_TPREL64},
  910. {BFD_RELOC_ALPHA_TPREL_HI16, R_ALPHA_TPRELHI},
  911. {BFD_RELOC_ALPHA_TPREL_LO16, R_ALPHA_TPRELLO},
  912. {BFD_RELOC_ALPHA_TPREL16, R_ALPHA_TPREL16},
  913. };
  914. /* Given a BFD reloc type, return a HOWTO structure. */
  915. static reloc_howto_type *
  916. elf64_alpha_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  917. bfd_reloc_code_real_type code)
  918. {
  919. const struct elf_reloc_map *i, *e;
  920. i = e = elf64_alpha_reloc_map;
  921. e += sizeof (elf64_alpha_reloc_map) / sizeof (struct elf_reloc_map);
  922. for (; i != e; ++i)
  923. {
  924. if (i->bfd_reloc_val == code)
  925. return &elf64_alpha_howto_table[i->elf_reloc_val];
  926. }
  927. return 0;
  928. }
  929. static reloc_howto_type *
  930. elf64_alpha_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  931. const char *r_name)
  932. {
  933. unsigned int i;
  934. for (i = 0;
  935. i < (sizeof (elf64_alpha_howto_table)
  936. / sizeof (elf64_alpha_howto_table[0]));
  937. i++)
  938. if (elf64_alpha_howto_table[i].name != NULL
  939. && strcasecmp (elf64_alpha_howto_table[i].name, r_name) == 0)
  940. return &elf64_alpha_howto_table[i];
  941. return NULL;
  942. }
  943. /* Given an Alpha ELF reloc type, fill in an arelent structure. */
  944. static bool
  945. elf64_alpha_info_to_howto (bfd *abfd, arelent *cache_ptr,
  946. Elf_Internal_Rela *dst)
  947. {
  948. unsigned r_type = ELF64_R_TYPE(dst->r_info);
  949. if (r_type >= R_ALPHA_max)
  950. {
  951. /* xgettext:c-format */
  952. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  953. abfd, r_type);
  954. bfd_set_error (bfd_error_bad_value);
  955. return false;
  956. }
  957. cache_ptr->howto = &elf64_alpha_howto_table[r_type];
  958. return true;
  959. }
  960. /* These two relocations create a two-word entry in the got. */
  961. #define alpha_got_entry_size(r_type) \
  962. (r_type == R_ALPHA_TLSGD || r_type == R_ALPHA_TLSLDM ? 16 : 8)
  963. /* This is PT_TLS segment p_vaddr. */
  964. #define alpha_get_dtprel_base(info) \
  965. (elf_hash_table (info)->tls_sec->vma)
  966. /* Main program TLS (whose template starts at PT_TLS p_vaddr)
  967. is assigned offset round(16, PT_TLS p_align). */
  968. #define alpha_get_tprel_base(info) \
  969. (elf_hash_table (info)->tls_sec->vma \
  970. - align_power ((bfd_vma) 16, \
  971. elf_hash_table (info)->tls_sec->alignment_power))
  972. /* Handle an Alpha specific section when reading an object file. This
  973. is called when bfd_section_from_shdr finds a section with an unknown
  974. type. */
  975. static bool
  976. elf64_alpha_section_from_shdr (bfd *abfd,
  977. Elf_Internal_Shdr *hdr,
  978. const char *name,
  979. int shindex)
  980. {
  981. asection *newsect;
  982. /* There ought to be a place to keep ELF backend specific flags, but
  983. at the moment there isn't one. We just keep track of the
  984. sections by their name, instead. Fortunately, the ABI gives
  985. suggested names for all the MIPS specific sections, so we will
  986. probably get away with this. */
  987. switch (hdr->sh_type)
  988. {
  989. case SHT_ALPHA_DEBUG:
  990. if (strcmp (name, ".mdebug") != 0)
  991. return false;
  992. break;
  993. default:
  994. return false;
  995. }
  996. if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
  997. return false;
  998. newsect = hdr->bfd_section;
  999. if (hdr->sh_type == SHT_ALPHA_DEBUG)
  1000. {
  1001. if (!bfd_set_section_flags (newsect,
  1002. bfd_section_flags (newsect) | SEC_DEBUGGING))
  1003. return false;
  1004. }
  1005. return true;
  1006. }
  1007. /* Convert Alpha specific section flags to bfd internal section flags. */
  1008. static bool
  1009. elf64_alpha_section_flags (const Elf_Internal_Shdr *hdr)
  1010. {
  1011. if (hdr->sh_flags & SHF_ALPHA_GPREL)
  1012. hdr->bfd_section->flags |= SEC_SMALL_DATA;
  1013. return true;
  1014. }
  1015. /* Set the correct type for an Alpha ELF section. We do this by the
  1016. section name, which is a hack, but ought to work. */
  1017. static bool
  1018. elf64_alpha_fake_sections (bfd *abfd, Elf_Internal_Shdr *hdr, asection *sec)
  1019. {
  1020. register const char *name;
  1021. name = bfd_section_name (sec);
  1022. if (strcmp (name, ".mdebug") == 0)
  1023. {
  1024. hdr->sh_type = SHT_ALPHA_DEBUG;
  1025. /* In a shared object on Irix 5.3, the .mdebug section has an
  1026. entsize of 0. FIXME: Does this matter? */
  1027. if ((abfd->flags & DYNAMIC) != 0 )
  1028. hdr->sh_entsize = 0;
  1029. else
  1030. hdr->sh_entsize = 1;
  1031. }
  1032. else if ((sec->flags & SEC_SMALL_DATA)
  1033. || strcmp (name, ".sdata") == 0
  1034. || strcmp (name, ".sbss") == 0
  1035. || strcmp (name, ".lit4") == 0
  1036. || strcmp (name, ".lit8") == 0)
  1037. hdr->sh_flags |= SHF_ALPHA_GPREL;
  1038. return true;
  1039. }
  1040. /* Hook called by the linker routine which adds symbols from an object
  1041. file. We use it to put .comm items in .sbss, and not .bss. */
  1042. static bool
  1043. elf64_alpha_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
  1044. Elf_Internal_Sym *sym,
  1045. const char **namep ATTRIBUTE_UNUSED,
  1046. flagword *flagsp ATTRIBUTE_UNUSED,
  1047. asection **secp, bfd_vma *valp)
  1048. {
  1049. if (sym->st_shndx == SHN_COMMON
  1050. && !bfd_link_relocatable (info)
  1051. && sym->st_size <= elf_gp_size (abfd))
  1052. {
  1053. /* Common symbols less than or equal to -G nn bytes are
  1054. automatically put into .sbss. */
  1055. asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
  1056. if (scomm == NULL)
  1057. {
  1058. scomm = bfd_make_section_with_flags (abfd, ".scommon",
  1059. (SEC_ALLOC
  1060. | SEC_IS_COMMON
  1061. | SEC_SMALL_DATA
  1062. | SEC_LINKER_CREATED));
  1063. if (scomm == NULL)
  1064. return false;
  1065. }
  1066. *secp = scomm;
  1067. *valp = sym->st_size;
  1068. }
  1069. return true;
  1070. }
  1071. /* Create the .got section. */
  1072. static bool
  1073. elf64_alpha_create_got_section (bfd *abfd,
  1074. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  1075. {
  1076. flagword flags;
  1077. asection *s;
  1078. if (! is_alpha_elf (abfd))
  1079. return false;
  1080. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  1081. | SEC_LINKER_CREATED);
  1082. s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
  1083. if (s == NULL
  1084. || !bfd_set_section_alignment (s, 3))
  1085. return false;
  1086. alpha_elf_tdata (abfd)->got = s;
  1087. /* Make sure the object's gotobj is set to itself so that we default
  1088. to every object with its own .got. We'll merge .gots later once
  1089. we've collected each object's info. */
  1090. alpha_elf_tdata (abfd)->gotobj = abfd;
  1091. return true;
  1092. }
  1093. /* Create all the dynamic sections. */
  1094. static bool
  1095. elf64_alpha_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  1096. {
  1097. asection *s;
  1098. flagword flags;
  1099. struct elf_link_hash_entry *h;
  1100. if (! is_alpha_elf (abfd))
  1101. return false;
  1102. /* We need to create .plt, .rela.plt, .got, and .rela.got sections. */
  1103. flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  1104. | SEC_LINKER_CREATED
  1105. | (elf64_alpha_use_secureplt ? SEC_READONLY : 0));
  1106. s = bfd_make_section_anyway_with_flags (abfd, ".plt", flags);
  1107. elf_hash_table (info)->splt = s;
  1108. if (s == NULL || ! bfd_set_section_alignment (s, 4))
  1109. return false;
  1110. /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
  1111. .plt section. */
  1112. h = _bfd_elf_define_linkage_sym (abfd, info, s,
  1113. "_PROCEDURE_LINKAGE_TABLE_");
  1114. elf_hash_table (info)->hplt = h;
  1115. if (h == NULL)
  1116. return false;
  1117. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  1118. | SEC_LINKER_CREATED | SEC_READONLY);
  1119. s = bfd_make_section_anyway_with_flags (abfd, ".rela.plt", flags);
  1120. elf_hash_table (info)->srelplt = s;
  1121. if (s == NULL || ! bfd_set_section_alignment (s, 3))
  1122. return false;
  1123. if (elf64_alpha_use_secureplt)
  1124. {
  1125. flags = SEC_ALLOC | SEC_LINKER_CREATED;
  1126. s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
  1127. elf_hash_table (info)->sgotplt = s;
  1128. if (s == NULL || ! bfd_set_section_alignment (s, 3))
  1129. return false;
  1130. }
  1131. /* We may or may not have created a .got section for this object, but
  1132. we definitely havn't done the rest of the work. */
  1133. if (alpha_elf_tdata(abfd)->gotobj == NULL)
  1134. {
  1135. if (!elf64_alpha_create_got_section (abfd, info))
  1136. return false;
  1137. }
  1138. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  1139. | SEC_LINKER_CREATED | SEC_READONLY);
  1140. s = bfd_make_section_anyway_with_flags (abfd, ".rela.got", flags);
  1141. elf_hash_table (info)->srelgot = s;
  1142. if (s == NULL
  1143. || !bfd_set_section_alignment (s, 3))
  1144. return false;
  1145. /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the
  1146. dynobj's .got section. We don't do this in the linker script
  1147. because we don't want to define the symbol if we are not creating
  1148. a global offset table. */
  1149. h = _bfd_elf_define_linkage_sym (abfd, info, alpha_elf_tdata(abfd)->got,
  1150. "_GLOBAL_OFFSET_TABLE_");
  1151. elf_hash_table (info)->hgot = h;
  1152. if (h == NULL)
  1153. return false;
  1154. return true;
  1155. }
  1156. /* Read ECOFF debugging information from a .mdebug section into a
  1157. ecoff_debug_info structure. */
  1158. static bool
  1159. elf64_alpha_read_ecoff_info (bfd *abfd, asection *section,
  1160. struct ecoff_debug_info *debug)
  1161. {
  1162. HDRR *symhdr;
  1163. const struct ecoff_debug_swap *swap;
  1164. char *ext_hdr = NULL;
  1165. swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  1166. memset (debug, 0, sizeof (*debug));
  1167. ext_hdr = (char *) bfd_malloc (swap->external_hdr_size);
  1168. if (ext_hdr == NULL && swap->external_hdr_size != 0)
  1169. goto error_return;
  1170. if (! bfd_get_section_contents (abfd, section, ext_hdr, (file_ptr) 0,
  1171. swap->external_hdr_size))
  1172. goto error_return;
  1173. symhdr = &debug->symbolic_header;
  1174. (*swap->swap_hdr_in) (abfd, ext_hdr, symhdr);
  1175. /* The symbolic header contains absolute file offsets and sizes to
  1176. read. */
  1177. #define READ(ptr, offset, count, size, type) \
  1178. do \
  1179. { \
  1180. size_t amt; \
  1181. debug->ptr = NULL; \
  1182. if (symhdr->count == 0) \
  1183. break; \
  1184. if (_bfd_mul_overflow (size, symhdr->count, &amt)) \
  1185. { \
  1186. bfd_set_error (bfd_error_file_too_big); \
  1187. goto error_return; \
  1188. } \
  1189. if (bfd_seek (abfd, symhdr->offset, SEEK_SET) != 0) \
  1190. goto error_return; \
  1191. debug->ptr = (type) _bfd_malloc_and_read (abfd, amt, amt); \
  1192. if (debug->ptr == NULL) \
  1193. goto error_return; \
  1194. } while (0)
  1195. READ (line, cbLineOffset, cbLine, sizeof (unsigned char), unsigned char *);
  1196. READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
  1197. READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
  1198. READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
  1199. READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
  1200. READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
  1201. union aux_ext *);
  1202. READ (ss, cbSsOffset, issMax, sizeof (char), char *);
  1203. READ (ssext, cbSsExtOffset, issExtMax, sizeof (char), char *);
  1204. READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
  1205. READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
  1206. READ (external_ext, cbExtOffset, iextMax, swap->external_ext_size, void *);
  1207. #undef READ
  1208. debug->fdr = NULL;
  1209. return true;
  1210. error_return:
  1211. free (ext_hdr);
  1212. free (debug->line);
  1213. free (debug->external_dnr);
  1214. free (debug->external_pdr);
  1215. free (debug->external_sym);
  1216. free (debug->external_opt);
  1217. free (debug->external_aux);
  1218. free (debug->ss);
  1219. free (debug->ssext);
  1220. free (debug->external_fdr);
  1221. free (debug->external_rfd);
  1222. free (debug->external_ext);
  1223. return false;
  1224. }
  1225. /* Alpha ELF local labels start with '$'. */
  1226. static bool
  1227. elf64_alpha_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, const char *name)
  1228. {
  1229. return name[0] == '$';
  1230. }
  1231. static bool
  1232. elf64_alpha_find_nearest_line (bfd *abfd, asymbol **symbols,
  1233. asection *section, bfd_vma offset,
  1234. const char **filename_ptr,
  1235. const char **functionname_ptr,
  1236. unsigned int *line_ptr,
  1237. unsigned int *discriminator_ptr)
  1238. {
  1239. asection *msec;
  1240. if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
  1241. filename_ptr, functionname_ptr,
  1242. line_ptr, discriminator_ptr,
  1243. dwarf_debug_sections,
  1244. &elf_tdata (abfd)->dwarf2_find_line_info)
  1245. == 1)
  1246. return true;
  1247. msec = bfd_get_section_by_name (abfd, ".mdebug");
  1248. if (msec != NULL)
  1249. {
  1250. flagword origflags;
  1251. struct alpha_elf_find_line *fi;
  1252. const struct ecoff_debug_swap * const swap =
  1253. get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  1254. /* If we are called during a link, alpha_elf_final_link may have
  1255. cleared the SEC_HAS_CONTENTS field. We force it back on here
  1256. if appropriate (which it normally will be). */
  1257. origflags = msec->flags;
  1258. if (elf_section_data (msec)->this_hdr.sh_type != SHT_NOBITS)
  1259. msec->flags |= SEC_HAS_CONTENTS;
  1260. fi = alpha_elf_tdata (abfd)->find_line_info;
  1261. if (fi == NULL)
  1262. {
  1263. bfd_size_type external_fdr_size;
  1264. char *fraw_src;
  1265. char *fraw_end;
  1266. struct fdr *fdr_ptr;
  1267. bfd_size_type amt = sizeof (struct alpha_elf_find_line);
  1268. fi = (struct alpha_elf_find_line *) bfd_zalloc (abfd, amt);
  1269. if (fi == NULL)
  1270. {
  1271. msec->flags = origflags;
  1272. return false;
  1273. }
  1274. if (!elf64_alpha_read_ecoff_info (abfd, msec, &fi->d))
  1275. {
  1276. msec->flags = origflags;
  1277. return false;
  1278. }
  1279. /* Swap in the FDR information. */
  1280. amt = fi->d.symbolic_header.ifdMax * sizeof (struct fdr);
  1281. fi->d.fdr = (struct fdr *) bfd_alloc (abfd, amt);
  1282. if (fi->d.fdr == NULL)
  1283. {
  1284. msec->flags = origflags;
  1285. return false;
  1286. }
  1287. external_fdr_size = swap->external_fdr_size;
  1288. fdr_ptr = fi->d.fdr;
  1289. fraw_src = (char *) fi->d.external_fdr;
  1290. fraw_end = (fraw_src
  1291. + fi->d.symbolic_header.ifdMax * external_fdr_size);
  1292. for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
  1293. (*swap->swap_fdr_in) (abfd, fraw_src, fdr_ptr);
  1294. alpha_elf_tdata (abfd)->find_line_info = fi;
  1295. /* Note that we don't bother to ever free this information.
  1296. find_nearest_line is either called all the time, as in
  1297. objdump -l, so the information should be saved, or it is
  1298. rarely called, as in ld error messages, so the memory
  1299. wasted is unimportant. Still, it would probably be a
  1300. good idea for free_cached_info to throw it away. */
  1301. }
  1302. if (_bfd_ecoff_locate_line (abfd, section, offset, &fi->d, swap,
  1303. &fi->i, filename_ptr, functionname_ptr,
  1304. line_ptr))
  1305. {
  1306. msec->flags = origflags;
  1307. return true;
  1308. }
  1309. msec->flags = origflags;
  1310. }
  1311. /* Fall back on the generic ELF find_nearest_line routine. */
  1312. return _bfd_elf_find_nearest_line (abfd, symbols, section, offset,
  1313. filename_ptr, functionname_ptr,
  1314. line_ptr, discriminator_ptr);
  1315. }
  1316. /* Structure used to pass information to alpha_elf_output_extsym. */
  1317. struct extsym_info
  1318. {
  1319. bfd *abfd;
  1320. struct bfd_link_info *info;
  1321. struct ecoff_debug_info *debug;
  1322. const struct ecoff_debug_swap *swap;
  1323. bool failed;
  1324. };
  1325. static bool
  1326. elf64_alpha_output_extsym (struct elf_link_hash_entry *x, void * data)
  1327. {
  1328. struct alpha_elf_link_hash_entry *h = (struct alpha_elf_link_hash_entry *) x;
  1329. struct extsym_info *einfo = (struct extsym_info *) data;
  1330. bool strip;
  1331. asection *sec, *output_section;
  1332. if (h->root.indx == -2)
  1333. strip = false;
  1334. else if ((h->root.def_dynamic
  1335. || h->root.ref_dynamic
  1336. || h->root.root.type == bfd_link_hash_new)
  1337. && !h->root.def_regular
  1338. && !h->root.ref_regular)
  1339. strip = true;
  1340. else if (einfo->info->strip == strip_all
  1341. || (einfo->info->strip == strip_some
  1342. && bfd_hash_lookup (einfo->info->keep_hash,
  1343. h->root.root.root.string,
  1344. false, false) == NULL))
  1345. strip = true;
  1346. else
  1347. strip = false;
  1348. if (strip)
  1349. return true;
  1350. if (h->esym.ifd == -2)
  1351. {
  1352. h->esym.jmptbl = 0;
  1353. h->esym.cobol_main = 0;
  1354. h->esym.weakext = 0;
  1355. h->esym.reserved = 0;
  1356. h->esym.ifd = ifdNil;
  1357. h->esym.asym.value = 0;
  1358. h->esym.asym.st = stGlobal;
  1359. if (h->root.root.type != bfd_link_hash_defined
  1360. && h->root.root.type != bfd_link_hash_defweak)
  1361. h->esym.asym.sc = scAbs;
  1362. else
  1363. {
  1364. const char *name;
  1365. sec = h->root.root.u.def.section;
  1366. output_section = sec->output_section;
  1367. /* When making a shared library and symbol h is the one from
  1368. the another shared library, OUTPUT_SECTION may be null. */
  1369. if (output_section == NULL)
  1370. h->esym.asym.sc = scUndefined;
  1371. else
  1372. {
  1373. name = bfd_section_name (output_section);
  1374. if (strcmp (name, ".text") == 0)
  1375. h->esym.asym.sc = scText;
  1376. else if (strcmp (name, ".data") == 0)
  1377. h->esym.asym.sc = scData;
  1378. else if (strcmp (name, ".sdata") == 0)
  1379. h->esym.asym.sc = scSData;
  1380. else if (strcmp (name, ".rodata") == 0
  1381. || strcmp (name, ".rdata") == 0)
  1382. h->esym.asym.sc = scRData;
  1383. else if (strcmp (name, ".bss") == 0)
  1384. h->esym.asym.sc = scBss;
  1385. else if (strcmp (name, ".sbss") == 0)
  1386. h->esym.asym.sc = scSBss;
  1387. else if (strcmp (name, ".init") == 0)
  1388. h->esym.asym.sc = scInit;
  1389. else if (strcmp (name, ".fini") == 0)
  1390. h->esym.asym.sc = scFini;
  1391. else
  1392. h->esym.asym.sc = scAbs;
  1393. }
  1394. }
  1395. h->esym.asym.reserved = 0;
  1396. h->esym.asym.index = indexNil;
  1397. }
  1398. if (h->root.root.type == bfd_link_hash_common)
  1399. h->esym.asym.value = h->root.root.u.c.size;
  1400. else if (h->root.root.type == bfd_link_hash_defined
  1401. || h->root.root.type == bfd_link_hash_defweak)
  1402. {
  1403. if (h->esym.asym.sc == scCommon)
  1404. h->esym.asym.sc = scBss;
  1405. else if (h->esym.asym.sc == scSCommon)
  1406. h->esym.asym.sc = scSBss;
  1407. sec = h->root.root.u.def.section;
  1408. output_section = sec->output_section;
  1409. if (output_section != NULL)
  1410. h->esym.asym.value = (h->root.root.u.def.value
  1411. + sec->output_offset
  1412. + output_section->vma);
  1413. else
  1414. h->esym.asym.value = 0;
  1415. }
  1416. if (! bfd_ecoff_debug_one_external (einfo->abfd, einfo->debug, einfo->swap,
  1417. h->root.root.root.string,
  1418. &h->esym))
  1419. {
  1420. einfo->failed = true;
  1421. return false;
  1422. }
  1423. return true;
  1424. }
  1425. /* Search for and possibly create a got entry. */
  1426. static struct alpha_elf_got_entry *
  1427. get_got_entry (bfd *abfd, struct alpha_elf_link_hash_entry *h,
  1428. unsigned long r_type, unsigned long r_symndx,
  1429. bfd_vma r_addend)
  1430. {
  1431. struct alpha_elf_got_entry *gotent;
  1432. struct alpha_elf_got_entry **slot;
  1433. if (h)
  1434. slot = &h->got_entries;
  1435. else
  1436. {
  1437. /* This is a local .got entry -- record for merge. */
  1438. struct alpha_elf_got_entry **local_got_entries;
  1439. local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
  1440. if (!local_got_entries)
  1441. {
  1442. bfd_size_type size;
  1443. Elf_Internal_Shdr *symtab_hdr;
  1444. symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
  1445. size = symtab_hdr->sh_info;
  1446. size *= sizeof (struct alpha_elf_got_entry *);
  1447. local_got_entries
  1448. = (struct alpha_elf_got_entry **) bfd_zalloc (abfd, size);
  1449. if (!local_got_entries)
  1450. return NULL;
  1451. alpha_elf_tdata (abfd)->local_got_entries = local_got_entries;
  1452. }
  1453. slot = &local_got_entries[r_symndx];
  1454. }
  1455. for (gotent = *slot; gotent ; gotent = gotent->next)
  1456. if (gotent->gotobj == abfd
  1457. && gotent->reloc_type == r_type
  1458. && gotent->addend == r_addend)
  1459. break;
  1460. if (!gotent)
  1461. {
  1462. int entry_size;
  1463. size_t amt;
  1464. amt = sizeof (struct alpha_elf_got_entry);
  1465. gotent = (struct alpha_elf_got_entry *) bfd_alloc (abfd, amt);
  1466. if (!gotent)
  1467. return NULL;
  1468. gotent->gotobj = abfd;
  1469. gotent->addend = r_addend;
  1470. gotent->got_offset = -1;
  1471. gotent->plt_offset = -1;
  1472. gotent->use_count = 1;
  1473. gotent->reloc_type = r_type;
  1474. gotent->reloc_done = 0;
  1475. gotent->reloc_xlated = 0;
  1476. gotent->next = *slot;
  1477. *slot = gotent;
  1478. entry_size = alpha_got_entry_size (r_type);
  1479. alpha_elf_tdata (abfd)->total_got_size += entry_size;
  1480. if (!h)
  1481. alpha_elf_tdata(abfd)->local_got_size += entry_size;
  1482. }
  1483. else
  1484. gotent->use_count += 1;
  1485. return gotent;
  1486. }
  1487. static bool
  1488. elf64_alpha_want_plt (struct alpha_elf_link_hash_entry *ah)
  1489. {
  1490. return ((ah->root.type == STT_FUNC
  1491. || ah->root.root.type == bfd_link_hash_undefweak
  1492. || ah->root.root.type == bfd_link_hash_undefined)
  1493. && (ah->flags & ALPHA_ELF_LINK_HASH_LU_PLT) != 0
  1494. && (ah->flags & ~ALPHA_ELF_LINK_HASH_LU_PLT) == 0);
  1495. }
  1496. /* Whether to sort relocs output by ld -r or ld --emit-relocs, by r_offset.
  1497. Don't do so for code sections. We want to keep ordering of LITERAL/LITUSE
  1498. as is. On the other hand, elf-eh-frame.c processing requires .eh_frame
  1499. relocs to be sorted. */
  1500. static bool
  1501. elf64_alpha_sort_relocs_p (asection *sec)
  1502. {
  1503. return (sec->flags & SEC_CODE) == 0;
  1504. }
  1505. /* Handle dynamic relocations when doing an Alpha ELF link. */
  1506. static bool
  1507. elf64_alpha_check_relocs (bfd *abfd, struct bfd_link_info *info,
  1508. asection *sec, const Elf_Internal_Rela *relocs)
  1509. {
  1510. bfd *dynobj;
  1511. asection *sreloc;
  1512. Elf_Internal_Shdr *symtab_hdr;
  1513. struct alpha_elf_link_hash_entry **sym_hashes;
  1514. const Elf_Internal_Rela *rel, *relend;
  1515. if (bfd_link_relocatable (info))
  1516. return true;
  1517. BFD_ASSERT (is_alpha_elf (abfd));
  1518. dynobj = elf_hash_table (info)->dynobj;
  1519. if (dynobj == NULL)
  1520. elf_hash_table (info)->dynobj = dynobj = abfd;
  1521. sreloc = NULL;
  1522. symtab_hdr = &elf_symtab_hdr (abfd);
  1523. sym_hashes = alpha_elf_sym_hashes (abfd);
  1524. relend = relocs + sec->reloc_count;
  1525. for (rel = relocs; rel < relend; ++rel)
  1526. {
  1527. enum {
  1528. NEED_GOT = 1,
  1529. NEED_GOT_ENTRY = 2,
  1530. NEED_DYNREL = 4
  1531. };
  1532. unsigned long r_symndx, r_type;
  1533. struct alpha_elf_link_hash_entry *h;
  1534. unsigned int gotent_flags;
  1535. bool maybe_dynamic;
  1536. unsigned int need;
  1537. bfd_vma addend;
  1538. r_symndx = ELF64_R_SYM (rel->r_info);
  1539. if (r_symndx < symtab_hdr->sh_info)
  1540. h = NULL;
  1541. else
  1542. {
  1543. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1544. while (h->root.root.type == bfd_link_hash_indirect
  1545. || h->root.root.type == bfd_link_hash_warning)
  1546. h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
  1547. /* PR15323, ref flags aren't set for references in the same
  1548. object. */
  1549. h->root.ref_regular = 1;
  1550. }
  1551. /* We can only get preliminary data on whether a symbol is
  1552. locally or externally defined, as not all of the input files
  1553. have yet been processed. Do something with what we know, as
  1554. this may help reduce memory usage and processing time later. */
  1555. maybe_dynamic = false;
  1556. if (h && ((bfd_link_pic (info)
  1557. && (!info->symbolic
  1558. || info->unresolved_syms_in_shared_libs == RM_IGNORE))
  1559. || !h->root.def_regular
  1560. || h->root.root.type == bfd_link_hash_defweak))
  1561. maybe_dynamic = true;
  1562. need = 0;
  1563. gotent_flags = 0;
  1564. r_type = ELF64_R_TYPE (rel->r_info);
  1565. addend = rel->r_addend;
  1566. switch (r_type)
  1567. {
  1568. case R_ALPHA_LITERAL:
  1569. need = NEED_GOT | NEED_GOT_ENTRY;
  1570. /* Remember how this literal is used from its LITUSEs.
  1571. This will be important when it comes to decide if we can
  1572. create a .plt entry for a function symbol. */
  1573. while (++rel < relend && ELF64_R_TYPE (rel->r_info) == R_ALPHA_LITUSE)
  1574. if (rel->r_addend >= 1 && rel->r_addend <= 6)
  1575. gotent_flags |= 1 << rel->r_addend;
  1576. --rel;
  1577. /* No LITUSEs -- presumably the address is used somehow. */
  1578. if (gotent_flags == 0)
  1579. gotent_flags = ALPHA_ELF_LINK_HASH_LU_ADDR;
  1580. break;
  1581. case R_ALPHA_GPDISP:
  1582. case R_ALPHA_GPREL16:
  1583. case R_ALPHA_GPREL32:
  1584. case R_ALPHA_GPRELHIGH:
  1585. case R_ALPHA_GPRELLOW:
  1586. case R_ALPHA_BRSGP:
  1587. need = NEED_GOT;
  1588. break;
  1589. case R_ALPHA_REFLONG:
  1590. case R_ALPHA_REFQUAD:
  1591. if (bfd_link_pic (info) || maybe_dynamic)
  1592. need = NEED_DYNREL;
  1593. break;
  1594. case R_ALPHA_TLSLDM:
  1595. /* The symbol for a TLSLDM reloc is ignored. Collapse the
  1596. reloc to the STN_UNDEF (0) symbol so that they all match. */
  1597. r_symndx = STN_UNDEF;
  1598. h = 0;
  1599. maybe_dynamic = false;
  1600. /* FALLTHRU */
  1601. case R_ALPHA_TLSGD:
  1602. case R_ALPHA_GOTDTPREL:
  1603. need = NEED_GOT | NEED_GOT_ENTRY;
  1604. break;
  1605. case R_ALPHA_GOTTPREL:
  1606. need = NEED_GOT | NEED_GOT_ENTRY;
  1607. gotent_flags = ALPHA_ELF_LINK_HASH_TLS_IE;
  1608. if (bfd_link_pic (info))
  1609. info->flags |= DF_STATIC_TLS;
  1610. break;
  1611. case R_ALPHA_TPREL64:
  1612. if (bfd_link_dll (info))
  1613. {
  1614. info->flags |= DF_STATIC_TLS;
  1615. need = NEED_DYNREL;
  1616. }
  1617. else if (maybe_dynamic)
  1618. need = NEED_DYNREL;
  1619. break;
  1620. }
  1621. if (need & NEED_GOT)
  1622. {
  1623. if (alpha_elf_tdata(abfd)->gotobj == NULL)
  1624. {
  1625. if (!elf64_alpha_create_got_section (abfd, info))
  1626. return false;
  1627. }
  1628. }
  1629. if (need & NEED_GOT_ENTRY)
  1630. {
  1631. struct alpha_elf_got_entry *gotent;
  1632. gotent = get_got_entry (abfd, h, r_type, r_symndx, addend);
  1633. if (!gotent)
  1634. return false;
  1635. if (gotent_flags)
  1636. {
  1637. gotent->flags |= gotent_flags;
  1638. if (h)
  1639. {
  1640. gotent_flags |= h->flags;
  1641. h->flags = gotent_flags;
  1642. /* Make a guess as to whether a .plt entry is needed. */
  1643. /* ??? It appears that we won't make it into
  1644. adjust_dynamic_symbol for symbols that remain
  1645. totally undefined. Copying this check here means
  1646. we can create a plt entry for them too. */
  1647. h->root.needs_plt
  1648. = (maybe_dynamic && elf64_alpha_want_plt (h));
  1649. }
  1650. }
  1651. }
  1652. if (need & NEED_DYNREL)
  1653. {
  1654. /* We need to create the section here now whether we eventually
  1655. use it or not so that it gets mapped to an output section by
  1656. the linker. If not used, we'll kill it in size_dynamic_sections. */
  1657. if (sreloc == NULL)
  1658. {
  1659. sreloc = _bfd_elf_make_dynamic_reloc_section
  1660. (sec, dynobj, 3, abfd, /*rela?*/ true);
  1661. if (sreloc == NULL)
  1662. return false;
  1663. }
  1664. if (h)
  1665. {
  1666. /* Since we havn't seen all of the input symbols yet, we
  1667. don't know whether we'll actually need a dynamic relocation
  1668. entry for this reloc. So make a record of it. Once we
  1669. find out if this thing needs dynamic relocation we'll
  1670. expand the relocation sections by the appropriate amount. */
  1671. struct alpha_elf_reloc_entry *rent;
  1672. for (rent = h->reloc_entries; rent; rent = rent->next)
  1673. if (rent->rtype == r_type && rent->srel == sreloc)
  1674. break;
  1675. if (!rent)
  1676. {
  1677. size_t amt = sizeof (struct alpha_elf_reloc_entry);
  1678. rent = (struct alpha_elf_reloc_entry *) bfd_alloc (abfd, amt);
  1679. if (!rent)
  1680. return false;
  1681. rent->srel = sreloc;
  1682. rent->sec = sec;
  1683. rent->rtype = r_type;
  1684. rent->count = 1;
  1685. rent->next = h->reloc_entries;
  1686. h->reloc_entries = rent;
  1687. }
  1688. else
  1689. rent->count++;
  1690. }
  1691. else if (bfd_link_pic (info))
  1692. {
  1693. /* If this is a shared library, and the section is to be
  1694. loaded into memory, we need a RELATIVE reloc. */
  1695. sreloc->size += sizeof (Elf64_External_Rela);
  1696. if (sec->flags & SEC_READONLY)
  1697. {
  1698. info->flags |= DF_TEXTREL;
  1699. info->callbacks->minfo
  1700. (_("%pB: dynamic relocation against `%pT' in "
  1701. "read-only section `%pA'\n"),
  1702. sec->owner, h->root.root.root.string, sec);
  1703. }
  1704. }
  1705. }
  1706. }
  1707. return true;
  1708. }
  1709. /* Return the section that should be marked against GC for a given
  1710. relocation. */
  1711. static asection *
  1712. elf64_alpha_gc_mark_hook (asection *sec, struct bfd_link_info *info,
  1713. Elf_Internal_Rela *rel,
  1714. struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
  1715. {
  1716. /* These relocations don't really reference a symbol. Instead we store
  1717. extra data in their addend slot. Ignore the symbol. */
  1718. switch (ELF64_R_TYPE (rel->r_info))
  1719. {
  1720. case R_ALPHA_LITUSE:
  1721. case R_ALPHA_GPDISP:
  1722. case R_ALPHA_HINT:
  1723. return NULL;
  1724. }
  1725. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  1726. }
  1727. /* Adjust a symbol defined by a dynamic object and referenced by a
  1728. regular object. The current definition is in some section of the
  1729. dynamic object, but we're not including those sections. We have to
  1730. change the definition to something the rest of the link can
  1731. understand. */
  1732. static bool
  1733. elf64_alpha_adjust_dynamic_symbol (struct bfd_link_info *info,
  1734. struct elf_link_hash_entry *h)
  1735. {
  1736. bfd *dynobj;
  1737. asection *s;
  1738. struct alpha_elf_link_hash_entry *ah;
  1739. dynobj = elf_hash_table(info)->dynobj;
  1740. ah = (struct alpha_elf_link_hash_entry *)h;
  1741. /* Now that we've seen all of the input symbols, finalize our decision
  1742. about whether this symbol should get a .plt entry. Irritatingly, it
  1743. is common for folk to leave undefined symbols in shared libraries,
  1744. and they still expect lazy binding; accept undefined symbols in lieu
  1745. of STT_FUNC. */
  1746. if (alpha_elf_dynamic_symbol_p (h, info) && elf64_alpha_want_plt (ah))
  1747. {
  1748. h->needs_plt = true;
  1749. s = elf_hash_table(info)->splt;
  1750. if (!s && !elf64_alpha_create_dynamic_sections (dynobj, info))
  1751. return false;
  1752. /* We need one plt entry per got subsection. Delay allocation of
  1753. the actual plt entries until size_plt_section, called from
  1754. size_dynamic_sections or during relaxation. */
  1755. return true;
  1756. }
  1757. else
  1758. h->needs_plt = false;
  1759. /* If this is a weak symbol, and there is a real definition, the
  1760. processor independent code will have arranged for us to see the
  1761. real definition first, and we can just use the same value. */
  1762. if (h->is_weakalias)
  1763. {
  1764. struct elf_link_hash_entry *def = weakdef (h);
  1765. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  1766. h->root.u.def.section = def->root.u.def.section;
  1767. h->root.u.def.value = def->root.u.def.value;
  1768. return true;
  1769. }
  1770. /* This is a reference to a symbol defined by a dynamic object which
  1771. is not a function. The Alpha, since it uses .got entries for all
  1772. symbols even in regular objects, does not need the hackery of a
  1773. .dynbss section and COPY dynamic relocations. */
  1774. return true;
  1775. }
  1776. /* Record STO_ALPHA_NOPV and STO_ALPHA_STD_GPLOAD. */
  1777. static void
  1778. elf64_alpha_merge_symbol_attribute (struct elf_link_hash_entry *h,
  1779. unsigned int st_other,
  1780. bool definition,
  1781. bool dynamic)
  1782. {
  1783. if (!dynamic && definition)
  1784. h->other = ((h->other & ELF_ST_VISIBILITY (-1))
  1785. | (st_other & ~ELF_ST_VISIBILITY (-1)));
  1786. }
  1787. /* Symbol versioning can create new symbols, and make our old symbols
  1788. indirect to the new ones. Consolidate the got and reloc information
  1789. in these situations. */
  1790. static void
  1791. elf64_alpha_copy_indirect_symbol (struct bfd_link_info *info,
  1792. struct elf_link_hash_entry *dir,
  1793. struct elf_link_hash_entry *ind)
  1794. {
  1795. struct alpha_elf_link_hash_entry *hi
  1796. = (struct alpha_elf_link_hash_entry *) ind;
  1797. struct alpha_elf_link_hash_entry *hs
  1798. = (struct alpha_elf_link_hash_entry *) dir;
  1799. /* Do the merging in the superclass. */
  1800. _bfd_elf_link_hash_copy_indirect(info, dir, ind);
  1801. /* Merge the flags. Whee. */
  1802. hs->flags |= hi->flags;
  1803. /* ??? It's unclear to me what's really supposed to happen when
  1804. "merging" defweak and defined symbols, given that we don't
  1805. actually throw away the defweak. This more-or-less copies
  1806. the logic related to got and plt entries in the superclass. */
  1807. if (ind->root.type != bfd_link_hash_indirect)
  1808. return;
  1809. /* Merge the .got entries. Cannibalize the old symbol's list in
  1810. doing so, since we don't need it anymore. */
  1811. if (hs->got_entries == NULL)
  1812. hs->got_entries = hi->got_entries;
  1813. else
  1814. {
  1815. struct alpha_elf_got_entry *gi, *gs, *gin, *gsh;
  1816. gsh = hs->got_entries;
  1817. for (gi = hi->got_entries; gi ; gi = gin)
  1818. {
  1819. gin = gi->next;
  1820. for (gs = gsh; gs ; gs = gs->next)
  1821. if (gi->gotobj == gs->gotobj
  1822. && gi->reloc_type == gs->reloc_type
  1823. && gi->addend == gs->addend)
  1824. {
  1825. gs->use_count += gi->use_count;
  1826. goto got_found;
  1827. }
  1828. gi->next = hs->got_entries;
  1829. hs->got_entries = gi;
  1830. got_found:;
  1831. }
  1832. }
  1833. hi->got_entries = NULL;
  1834. /* And similar for the reloc entries. */
  1835. if (hs->reloc_entries == NULL)
  1836. hs->reloc_entries = hi->reloc_entries;
  1837. else
  1838. {
  1839. struct alpha_elf_reloc_entry *ri, *rs, *rin, *rsh;
  1840. rsh = hs->reloc_entries;
  1841. for (ri = hi->reloc_entries; ri ; ri = rin)
  1842. {
  1843. rin = ri->next;
  1844. for (rs = rsh; rs ; rs = rs->next)
  1845. if (ri->rtype == rs->rtype && ri->srel == rs->srel)
  1846. {
  1847. rs->count += ri->count;
  1848. goto found_reloc;
  1849. }
  1850. ri->next = hs->reloc_entries;
  1851. hs->reloc_entries = ri;
  1852. found_reloc:;
  1853. }
  1854. }
  1855. hi->reloc_entries = NULL;
  1856. }
  1857. /* Is it possible to merge two object file's .got tables? */
  1858. static bool
  1859. elf64_alpha_can_merge_gots (bfd *a, bfd *b)
  1860. {
  1861. int total = alpha_elf_tdata (a)->total_got_size;
  1862. bfd *bsub;
  1863. /* Trivial quick fallout test. */
  1864. if (total + alpha_elf_tdata (b)->total_got_size <= MAX_GOT_SIZE)
  1865. return true;
  1866. /* By their nature, local .got entries cannot be merged. */
  1867. if ((total += alpha_elf_tdata (b)->local_got_size) > MAX_GOT_SIZE)
  1868. return false;
  1869. /* Failing the common trivial comparison, we must effectively
  1870. perform the merge. Not actually performing the merge means that
  1871. we don't have to store undo information in case we fail. */
  1872. for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
  1873. {
  1874. struct alpha_elf_link_hash_entry **hashes = alpha_elf_sym_hashes (bsub);
  1875. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
  1876. int i, n;
  1877. n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
  1878. for (i = 0; i < n; ++i)
  1879. {
  1880. struct alpha_elf_got_entry *ae, *be;
  1881. struct alpha_elf_link_hash_entry *h;
  1882. h = hashes[i];
  1883. while (h->root.root.type == bfd_link_hash_indirect
  1884. || h->root.root.type == bfd_link_hash_warning)
  1885. h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
  1886. for (be = h->got_entries; be ; be = be->next)
  1887. {
  1888. if (be->use_count == 0)
  1889. continue;
  1890. if (be->gotobj != b)
  1891. continue;
  1892. for (ae = h->got_entries; ae ; ae = ae->next)
  1893. if (ae->gotobj == a
  1894. && ae->reloc_type == be->reloc_type
  1895. && ae->addend == be->addend)
  1896. goto global_found;
  1897. total += alpha_got_entry_size (be->reloc_type);
  1898. if (total > MAX_GOT_SIZE)
  1899. return false;
  1900. global_found:;
  1901. }
  1902. }
  1903. }
  1904. return true;
  1905. }
  1906. /* Actually merge two .got tables. */
  1907. static void
  1908. elf64_alpha_merge_gots (bfd *a, bfd *b)
  1909. {
  1910. int total = alpha_elf_tdata (a)->total_got_size;
  1911. bfd *bsub;
  1912. /* Remember local expansion. */
  1913. {
  1914. int e = alpha_elf_tdata (b)->local_got_size;
  1915. total += e;
  1916. alpha_elf_tdata (a)->local_got_size += e;
  1917. }
  1918. for (bsub = b; bsub ; bsub = alpha_elf_tdata (bsub)->in_got_link_next)
  1919. {
  1920. struct alpha_elf_got_entry **local_got_entries;
  1921. struct alpha_elf_link_hash_entry **hashes;
  1922. Elf_Internal_Shdr *symtab_hdr;
  1923. int i, n;
  1924. /* Let the local .got entries know they are part of a new subsegment. */
  1925. local_got_entries = alpha_elf_tdata (bsub)->local_got_entries;
  1926. if (local_got_entries)
  1927. {
  1928. n = elf_tdata (bsub)->symtab_hdr.sh_info;
  1929. for (i = 0; i < n; ++i)
  1930. {
  1931. struct alpha_elf_got_entry *ent;
  1932. for (ent = local_got_entries[i]; ent; ent = ent->next)
  1933. ent->gotobj = a;
  1934. }
  1935. }
  1936. /* Merge the global .got entries. */
  1937. hashes = alpha_elf_sym_hashes (bsub);
  1938. symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
  1939. n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
  1940. for (i = 0; i < n; ++i)
  1941. {
  1942. struct alpha_elf_got_entry *ae, *be, **pbe, **start;
  1943. struct alpha_elf_link_hash_entry *h;
  1944. h = hashes[i];
  1945. while (h->root.root.type == bfd_link_hash_indirect
  1946. || h->root.root.type == bfd_link_hash_warning)
  1947. h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
  1948. pbe = start = &h->got_entries;
  1949. while ((be = *pbe) != NULL)
  1950. {
  1951. if (be->use_count == 0)
  1952. {
  1953. *pbe = be->next;
  1954. memset (be, 0xa5, sizeof (*be));
  1955. goto kill;
  1956. }
  1957. if (be->gotobj != b)
  1958. goto next;
  1959. for (ae = *start; ae ; ae = ae->next)
  1960. if (ae->gotobj == a
  1961. && ae->reloc_type == be->reloc_type
  1962. && ae->addend == be->addend)
  1963. {
  1964. ae->flags |= be->flags;
  1965. ae->use_count += be->use_count;
  1966. *pbe = be->next;
  1967. memset (be, 0xa5, sizeof (*be));
  1968. goto kill;
  1969. }
  1970. be->gotobj = a;
  1971. total += alpha_got_entry_size (be->reloc_type);
  1972. next:;
  1973. pbe = &be->next;
  1974. kill:;
  1975. }
  1976. }
  1977. alpha_elf_tdata (bsub)->gotobj = a;
  1978. }
  1979. alpha_elf_tdata (a)->total_got_size = total;
  1980. /* Merge the two in_got chains. */
  1981. {
  1982. bfd *next;
  1983. bsub = a;
  1984. while ((next = alpha_elf_tdata (bsub)->in_got_link_next) != NULL)
  1985. bsub = next;
  1986. alpha_elf_tdata (bsub)->in_got_link_next = b;
  1987. }
  1988. }
  1989. /* Calculate the offsets for the got entries. */
  1990. static bool
  1991. elf64_alpha_calc_got_offsets_for_symbol (struct alpha_elf_link_hash_entry *h,
  1992. void * arg ATTRIBUTE_UNUSED)
  1993. {
  1994. struct alpha_elf_got_entry *gotent;
  1995. for (gotent = h->got_entries; gotent; gotent = gotent->next)
  1996. if (gotent->use_count > 0)
  1997. {
  1998. struct alpha_elf_obj_tdata *td;
  1999. bfd_size_type *plge;
  2000. td = alpha_elf_tdata (gotent->gotobj);
  2001. plge = &td->got->size;
  2002. gotent->got_offset = *plge;
  2003. *plge += alpha_got_entry_size (gotent->reloc_type);
  2004. }
  2005. return true;
  2006. }
  2007. static void
  2008. elf64_alpha_calc_got_offsets (struct bfd_link_info *info)
  2009. {
  2010. bfd *i, *got_list;
  2011. struct alpha_elf_link_hash_table * htab;
  2012. htab = alpha_elf_hash_table (info);
  2013. if (htab == NULL)
  2014. return;
  2015. got_list = htab->got_list;
  2016. /* First, zero out the .got sizes, as we may be recalculating the
  2017. .got after optimizing it. */
  2018. for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
  2019. alpha_elf_tdata(i)->got->size = 0;
  2020. /* Next, fill in the offsets for all the global entries. */
  2021. alpha_elf_link_hash_traverse (htab,
  2022. elf64_alpha_calc_got_offsets_for_symbol,
  2023. NULL);
  2024. /* Finally, fill in the offsets for the local entries. */
  2025. for (i = got_list; i ; i = alpha_elf_tdata(i)->got_link_next)
  2026. {
  2027. bfd_size_type got_offset = alpha_elf_tdata(i)->got->size;
  2028. bfd *j;
  2029. for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
  2030. {
  2031. struct alpha_elf_got_entry **local_got_entries, *gotent;
  2032. int k, n;
  2033. local_got_entries = alpha_elf_tdata(j)->local_got_entries;
  2034. if (!local_got_entries)
  2035. continue;
  2036. for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
  2037. for (gotent = local_got_entries[k]; gotent; gotent = gotent->next)
  2038. if (gotent->use_count > 0)
  2039. {
  2040. gotent->got_offset = got_offset;
  2041. got_offset += alpha_got_entry_size (gotent->reloc_type);
  2042. }
  2043. }
  2044. alpha_elf_tdata(i)->got->size = got_offset;
  2045. }
  2046. }
  2047. /* Constructs the gots. */
  2048. static bool
  2049. elf64_alpha_size_got_sections (struct bfd_link_info *info,
  2050. bool may_merge)
  2051. {
  2052. bfd *i, *got_list, *cur_got_obj = NULL;
  2053. struct alpha_elf_link_hash_table * htab;
  2054. htab = alpha_elf_hash_table (info);
  2055. if (htab == NULL)
  2056. return false;
  2057. got_list = htab->got_list;
  2058. /* On the first time through, pretend we have an existing got list
  2059. consisting of all of the input files. */
  2060. if (got_list == NULL)
  2061. {
  2062. for (i = info->input_bfds; i ; i = i->link.next)
  2063. {
  2064. bfd *this_got;
  2065. if (! is_alpha_elf (i))
  2066. continue;
  2067. this_got = alpha_elf_tdata (i)->gotobj;
  2068. if (this_got == NULL)
  2069. continue;
  2070. /* We are assuming no merging has yet occurred. */
  2071. BFD_ASSERT (this_got == i);
  2072. if (alpha_elf_tdata (this_got)->total_got_size > MAX_GOT_SIZE)
  2073. {
  2074. /* Yikes! A single object file has too many entries. */
  2075. _bfd_error_handler
  2076. /* xgettext:c-format */
  2077. (_("%pB: .got subsegment exceeds 64K (size %d)"),
  2078. i, alpha_elf_tdata (this_got)->total_got_size);
  2079. return false;
  2080. }
  2081. if (got_list == NULL)
  2082. got_list = this_got;
  2083. else
  2084. alpha_elf_tdata(cur_got_obj)->got_link_next = this_got;
  2085. cur_got_obj = this_got;
  2086. }
  2087. /* Strange degenerate case of no got references. */
  2088. if (got_list == NULL)
  2089. return true;
  2090. htab->got_list = got_list;
  2091. }
  2092. cur_got_obj = got_list;
  2093. if (cur_got_obj == NULL)
  2094. return false;
  2095. if (may_merge)
  2096. {
  2097. i = alpha_elf_tdata(cur_got_obj)->got_link_next;
  2098. while (i != NULL)
  2099. {
  2100. if (elf64_alpha_can_merge_gots (cur_got_obj, i))
  2101. {
  2102. elf64_alpha_merge_gots (cur_got_obj, i);
  2103. alpha_elf_tdata(i)->got->size = 0;
  2104. i = alpha_elf_tdata(i)->got_link_next;
  2105. alpha_elf_tdata(cur_got_obj)->got_link_next = i;
  2106. }
  2107. else
  2108. {
  2109. cur_got_obj = i;
  2110. i = alpha_elf_tdata(i)->got_link_next;
  2111. }
  2112. }
  2113. }
  2114. /* Once the gots have been merged, fill in the got offsets for
  2115. everything therein. */
  2116. elf64_alpha_calc_got_offsets (info);
  2117. return true;
  2118. }
  2119. static bool
  2120. elf64_alpha_size_plt_section_1 (struct alpha_elf_link_hash_entry *h,
  2121. void * data)
  2122. {
  2123. asection *splt = (asection *) data;
  2124. struct alpha_elf_got_entry *gotent;
  2125. bool saw_one = false;
  2126. /* If we didn't need an entry before, we still don't. */
  2127. if (!h->root.needs_plt)
  2128. return true;
  2129. /* For each LITERAL got entry still in use, allocate a plt entry. */
  2130. for (gotent = h->got_entries; gotent ; gotent = gotent->next)
  2131. if (gotent->reloc_type == R_ALPHA_LITERAL
  2132. && gotent->use_count > 0)
  2133. {
  2134. if (splt->size == 0)
  2135. splt->size = PLT_HEADER_SIZE;
  2136. gotent->plt_offset = splt->size;
  2137. splt->size += PLT_ENTRY_SIZE;
  2138. saw_one = true;
  2139. }
  2140. /* If there weren't any, there's no longer a need for the PLT entry. */
  2141. if (!saw_one)
  2142. h->root.needs_plt = false;
  2143. return true;
  2144. }
  2145. /* Called from relax_section to rebuild the PLT in light of potential changes
  2146. in the function's status. */
  2147. static void
  2148. elf64_alpha_size_plt_section (struct bfd_link_info *info)
  2149. {
  2150. asection *splt, *spltrel, *sgotplt;
  2151. unsigned long entries;
  2152. struct alpha_elf_link_hash_table * htab;
  2153. htab = alpha_elf_hash_table (info);
  2154. if (htab == NULL)
  2155. return;
  2156. splt = elf_hash_table(info)->splt;
  2157. if (splt == NULL)
  2158. return;
  2159. splt->size = 0;
  2160. alpha_elf_link_hash_traverse (htab,
  2161. elf64_alpha_size_plt_section_1, splt);
  2162. /* Every plt entry requires a JMP_SLOT relocation. */
  2163. spltrel = elf_hash_table(info)->srelplt;
  2164. entries = 0;
  2165. if (splt->size)
  2166. {
  2167. if (elf64_alpha_use_secureplt)
  2168. entries = (splt->size - NEW_PLT_HEADER_SIZE) / NEW_PLT_ENTRY_SIZE;
  2169. else
  2170. entries = (splt->size - OLD_PLT_HEADER_SIZE) / OLD_PLT_ENTRY_SIZE;
  2171. }
  2172. spltrel->size = entries * sizeof (Elf64_External_Rela);
  2173. /* When using the secureplt, we need two words somewhere in the data
  2174. segment for the dynamic linker to tell us where to go. This is the
  2175. entire contents of the .got.plt section. */
  2176. if (elf64_alpha_use_secureplt)
  2177. {
  2178. sgotplt = elf_hash_table(info)->sgotplt;
  2179. sgotplt->size = entries ? 16 : 0;
  2180. }
  2181. }
  2182. static bool
  2183. elf64_alpha_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  2184. struct bfd_link_info *info)
  2185. {
  2186. bfd *i;
  2187. struct alpha_elf_link_hash_table * htab;
  2188. if (bfd_link_relocatable (info))
  2189. return true;
  2190. htab = alpha_elf_hash_table (info);
  2191. if (htab == NULL)
  2192. return false;
  2193. if (!elf64_alpha_size_got_sections (info, true))
  2194. return false;
  2195. /* Allocate space for all of the .got subsections. */
  2196. i = htab->got_list;
  2197. for ( ; i ; i = alpha_elf_tdata(i)->got_link_next)
  2198. {
  2199. asection *s = alpha_elf_tdata(i)->got;
  2200. if (s->size > 0)
  2201. {
  2202. s->contents = (bfd_byte *) bfd_zalloc (i, s->size);
  2203. if (s->contents == NULL)
  2204. return false;
  2205. }
  2206. }
  2207. return true;
  2208. }
  2209. /* The number of dynamic relocations required by a static relocation. */
  2210. static int
  2211. alpha_dynamic_entries_for_reloc (int r_type, int dynamic, int shared, int pie)
  2212. {
  2213. switch (r_type)
  2214. {
  2215. /* May appear in GOT entries. */
  2216. case R_ALPHA_TLSGD:
  2217. return (dynamic ? 2 : shared ? 1 : 0);
  2218. case R_ALPHA_TLSLDM:
  2219. return shared;
  2220. case R_ALPHA_LITERAL:
  2221. return dynamic || shared;
  2222. case R_ALPHA_GOTTPREL:
  2223. return dynamic || (shared && !pie);
  2224. case R_ALPHA_GOTDTPREL:
  2225. return dynamic;
  2226. /* May appear in data sections. */
  2227. case R_ALPHA_REFLONG:
  2228. case R_ALPHA_REFQUAD:
  2229. return dynamic || shared;
  2230. case R_ALPHA_TPREL64:
  2231. return dynamic || (shared && !pie);
  2232. /* Everything else is illegal. We'll issue an error during
  2233. relocate_section. */
  2234. default:
  2235. return 0;
  2236. }
  2237. }
  2238. /* Work out the sizes of the dynamic relocation entries. */
  2239. static bool
  2240. elf64_alpha_calc_dynrel_sizes (struct alpha_elf_link_hash_entry *h,
  2241. struct bfd_link_info *info)
  2242. {
  2243. bool dynamic;
  2244. struct alpha_elf_reloc_entry *relent;
  2245. unsigned long entries;
  2246. /* If the symbol was defined as a common symbol in a regular object
  2247. file, and there was no definition in any dynamic object, then the
  2248. linker will have allocated space for the symbol in a common
  2249. section but the ELF_LINK_HASH_DEF_REGULAR flag will not have been
  2250. set. This is done for dynamic symbols in
  2251. elf_adjust_dynamic_symbol but this is not done for non-dynamic
  2252. symbols, somehow. */
  2253. if (!h->root.def_regular
  2254. && h->root.ref_regular
  2255. && !h->root.def_dynamic
  2256. && (h->root.root.type == bfd_link_hash_defined
  2257. || h->root.root.type == bfd_link_hash_defweak)
  2258. && !(h->root.root.u.def.section->owner->flags & DYNAMIC))
  2259. h->root.def_regular = 1;
  2260. /* If the symbol is dynamic, we'll need all the relocations in their
  2261. natural form. If this is a shared object, and it has been forced
  2262. local, we'll need the same number of RELATIVE relocations. */
  2263. dynamic = alpha_elf_dynamic_symbol_p (&h->root, info);
  2264. /* If the symbol is a hidden undefined weak, then we never have any
  2265. relocations. Avoid the loop which may want to add RELATIVE relocs
  2266. based on bfd_link_pic (info). */
  2267. if (h->root.root.type == bfd_link_hash_undefweak && !dynamic)
  2268. return true;
  2269. for (relent = h->reloc_entries; relent; relent = relent->next)
  2270. {
  2271. entries = alpha_dynamic_entries_for_reloc (relent->rtype, dynamic,
  2272. bfd_link_pic (info),
  2273. bfd_link_pie (info));
  2274. if (entries)
  2275. {
  2276. asection *sec = relent->sec;
  2277. relent->srel->size +=
  2278. entries * sizeof (Elf64_External_Rela) * relent->count;
  2279. if ((sec->flags & SEC_READONLY) != 0)
  2280. {
  2281. info->flags |= DT_TEXTREL;
  2282. info->callbacks->minfo
  2283. (_("%pB: dynamic relocation against `%pT' in "
  2284. "read-only section `%pA'\n"),
  2285. sec->owner, h->root.root.root.string, sec);
  2286. }
  2287. }
  2288. }
  2289. return true;
  2290. }
  2291. /* Subroutine of elf64_alpha_size_rela_got_section for doing the
  2292. global symbols. */
  2293. static bool
  2294. elf64_alpha_size_rela_got_1 (struct alpha_elf_link_hash_entry *h,
  2295. struct bfd_link_info *info)
  2296. {
  2297. bool dynamic;
  2298. struct alpha_elf_got_entry *gotent;
  2299. unsigned long entries;
  2300. /* If we're using a plt for this symbol, then all of its relocations
  2301. for its got entries go into .rela.plt. */
  2302. if (h->root.needs_plt)
  2303. return true;
  2304. /* If the symbol is dynamic, we'll need all the relocations in their
  2305. natural form. If this is a shared object, and it has been forced
  2306. local, we'll need the same number of RELATIVE relocations. */
  2307. dynamic = alpha_elf_dynamic_symbol_p (&h->root, info);
  2308. /* If the symbol is a hidden undefined weak, then we never have any
  2309. relocations. Avoid the loop which may want to add RELATIVE relocs
  2310. based on bfd_link_pic (info). */
  2311. if (h->root.root.type == bfd_link_hash_undefweak && !dynamic)
  2312. return true;
  2313. entries = 0;
  2314. for (gotent = h->got_entries; gotent ; gotent = gotent->next)
  2315. if (gotent->use_count > 0)
  2316. entries += alpha_dynamic_entries_for_reloc (gotent->reloc_type, dynamic,
  2317. bfd_link_pic (info),
  2318. bfd_link_pie (info));
  2319. if (entries > 0)
  2320. {
  2321. asection *srel = elf_hash_table(info)->srelgot;
  2322. BFD_ASSERT (srel != NULL);
  2323. srel->size += sizeof (Elf64_External_Rela) * entries;
  2324. }
  2325. return true;
  2326. }
  2327. /* Set the sizes of the dynamic relocation sections. */
  2328. static void
  2329. elf64_alpha_size_rela_got_section (struct bfd_link_info *info)
  2330. {
  2331. unsigned long entries;
  2332. bfd *i;
  2333. asection *srel;
  2334. struct alpha_elf_link_hash_table * htab;
  2335. htab = alpha_elf_hash_table (info);
  2336. if (htab == NULL)
  2337. return;
  2338. /* Shared libraries often require RELATIVE relocs, and some relocs
  2339. require attention for the main application as well. */
  2340. entries = 0;
  2341. for (i = htab->got_list;
  2342. i ; i = alpha_elf_tdata(i)->got_link_next)
  2343. {
  2344. bfd *j;
  2345. for (j = i; j ; j = alpha_elf_tdata(j)->in_got_link_next)
  2346. {
  2347. struct alpha_elf_got_entry **local_got_entries, *gotent;
  2348. int k, n;
  2349. local_got_entries = alpha_elf_tdata(j)->local_got_entries;
  2350. if (!local_got_entries)
  2351. continue;
  2352. for (k = 0, n = elf_tdata(j)->symtab_hdr.sh_info; k < n; ++k)
  2353. for (gotent = local_got_entries[k];
  2354. gotent ; gotent = gotent->next)
  2355. if (gotent->use_count > 0)
  2356. entries += (alpha_dynamic_entries_for_reloc
  2357. (gotent->reloc_type, 0, bfd_link_pic (info),
  2358. bfd_link_pie (info)));
  2359. }
  2360. }
  2361. srel = elf_hash_table(info)->srelgot;
  2362. if (!srel)
  2363. {
  2364. BFD_ASSERT (entries == 0);
  2365. return;
  2366. }
  2367. srel->size = sizeof (Elf64_External_Rela) * entries;
  2368. /* Now do the non-local symbols. */
  2369. alpha_elf_link_hash_traverse (htab,
  2370. elf64_alpha_size_rela_got_1, info);
  2371. }
  2372. /* Set the sizes of the dynamic sections. */
  2373. static bool
  2374. elf64_alpha_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  2375. struct bfd_link_info *info)
  2376. {
  2377. bfd *dynobj;
  2378. asection *s;
  2379. bool relplt, relocs;
  2380. struct alpha_elf_link_hash_table * htab;
  2381. htab = alpha_elf_hash_table (info);
  2382. if (htab == NULL)
  2383. return false;
  2384. dynobj = elf_hash_table(info)->dynobj;
  2385. BFD_ASSERT(dynobj != NULL);
  2386. if (elf_hash_table (info)->dynamic_sections_created)
  2387. {
  2388. /* Set the contents of the .interp section to the interpreter. */
  2389. if (bfd_link_executable (info) && !info->nointerp)
  2390. {
  2391. s = bfd_get_linker_section (dynobj, ".interp");
  2392. BFD_ASSERT (s != NULL);
  2393. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  2394. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  2395. }
  2396. /* Now that we've seen all of the input files, we can decide which
  2397. symbols need dynamic relocation entries and which don't. We've
  2398. collected information in check_relocs that we can now apply to
  2399. size the dynamic relocation sections. */
  2400. alpha_elf_link_hash_traverse (htab,
  2401. elf64_alpha_calc_dynrel_sizes, info);
  2402. elf64_alpha_size_rela_got_section (info);
  2403. elf64_alpha_size_plt_section (info);
  2404. }
  2405. /* else we're not dynamic and by definition we don't need such things. */
  2406. /* The check_relocs and adjust_dynamic_symbol entry points have
  2407. determined the sizes of the various dynamic sections. Allocate
  2408. memory for them. */
  2409. relplt = false;
  2410. relocs = false;
  2411. for (s = dynobj->sections; s != NULL; s = s->next)
  2412. {
  2413. const char *name;
  2414. if (!(s->flags & SEC_LINKER_CREATED))
  2415. continue;
  2416. /* It's OK to base decisions on the section name, because none
  2417. of the dynobj section names depend upon the input files. */
  2418. name = bfd_section_name (s);
  2419. if (startswith (name, ".rela"))
  2420. {
  2421. if (s->size != 0)
  2422. {
  2423. if (strcmp (name, ".rela.plt") == 0)
  2424. relplt = true;
  2425. else
  2426. relocs = true;
  2427. /* We use the reloc_count field as a counter if we need
  2428. to copy relocs into the output file. */
  2429. s->reloc_count = 0;
  2430. }
  2431. }
  2432. else if (! startswith (name, ".got")
  2433. && strcmp (name, ".plt") != 0
  2434. && strcmp (name, ".dynbss") != 0)
  2435. {
  2436. /* It's not one of our dynamic sections, so don't allocate space. */
  2437. continue;
  2438. }
  2439. if (s->size == 0)
  2440. {
  2441. /* If we don't need this section, strip it from the output file.
  2442. This is to handle .rela.bss and .rela.plt. We must create it
  2443. in create_dynamic_sections, because it must be created before
  2444. the linker maps input sections to output sections. The
  2445. linker does that before adjust_dynamic_symbol is called, and
  2446. it is that function which decides whether anything needs to
  2447. go into these sections. */
  2448. if (!startswith (name, ".got"))
  2449. s->flags |= SEC_EXCLUDE;
  2450. }
  2451. else if ((s->flags & SEC_HAS_CONTENTS) != 0)
  2452. {
  2453. /* Allocate memory for the section contents. */
  2454. s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
  2455. if (s->contents == NULL)
  2456. return false;
  2457. }
  2458. }
  2459. if (elf_hash_table (info)->dynamic_sections_created)
  2460. {
  2461. /* Add some entries to the .dynamic section. We fill in the
  2462. values later, in elf64_alpha_finish_dynamic_sections, but we
  2463. must add the entries now so that we get the correct size for
  2464. the .dynamic section. The DT_DEBUG entry is filled in by the
  2465. dynamic linker and used by the debugger. */
  2466. #define add_dynamic_entry(TAG, VAL) \
  2467. _bfd_elf_add_dynamic_entry (info, TAG, VAL)
  2468. if (!_bfd_elf_add_dynamic_tags (output_bfd, info,
  2469. relocs || relplt))
  2470. return false;
  2471. if (relplt
  2472. && elf64_alpha_use_secureplt
  2473. && !add_dynamic_entry (DT_ALPHA_PLTRO, 1))
  2474. return false;
  2475. }
  2476. #undef add_dynamic_entry
  2477. return true;
  2478. }
  2479. /* These functions do relaxation for Alpha ELF.
  2480. Currently I'm only handling what I can do with existing compiler
  2481. and assembler support, which means no instructions are removed,
  2482. though some may be nopped. At this time GCC does not emit enough
  2483. information to do all of the relaxing that is possible. It will
  2484. take some not small amount of work for that to happen.
  2485. There are a couple of interesting papers that I once read on this
  2486. subject, that I cannot find references to at the moment, that
  2487. related to Alpha in particular. They are by David Wall, then of
  2488. DEC WRL. */
  2489. struct alpha_relax_info
  2490. {
  2491. bfd *abfd;
  2492. asection *sec;
  2493. bfd_byte *contents;
  2494. Elf_Internal_Shdr *symtab_hdr;
  2495. Elf_Internal_Rela *relocs, *relend;
  2496. struct bfd_link_info *link_info;
  2497. bfd_vma gp;
  2498. bfd *gotobj;
  2499. asection *tsec;
  2500. struct alpha_elf_link_hash_entry *h;
  2501. struct alpha_elf_got_entry **first_gotent;
  2502. struct alpha_elf_got_entry *gotent;
  2503. bool changed_contents;
  2504. bool changed_relocs;
  2505. unsigned char other;
  2506. };
  2507. static Elf_Internal_Rela *
  2508. elf64_alpha_find_reloc_at_ofs (Elf_Internal_Rela *rel,
  2509. Elf_Internal_Rela *relend,
  2510. bfd_vma offset, int type)
  2511. {
  2512. while (rel < relend)
  2513. {
  2514. if (rel->r_offset == offset
  2515. && ELF64_R_TYPE (rel->r_info) == (unsigned int) type)
  2516. return rel;
  2517. ++rel;
  2518. }
  2519. return NULL;
  2520. }
  2521. static bool
  2522. elf64_alpha_relax_got_load (struct alpha_relax_info *info, bfd_vma symval,
  2523. Elf_Internal_Rela *irel, unsigned long r_type)
  2524. {
  2525. unsigned int insn;
  2526. bfd_signed_vma disp;
  2527. /* Get the instruction. */
  2528. insn = bfd_get_32 (info->abfd, info->contents + irel->r_offset);
  2529. if (insn >> 26 != OP_LDQ)
  2530. {
  2531. reloc_howto_type *howto = elf64_alpha_howto_table + r_type;
  2532. _bfd_error_handler
  2533. /* xgettext:c-format */
  2534. (_("%pB: %pA+%#" PRIx64 ": warning: "
  2535. "%s relocation against unexpected insn"),
  2536. info->abfd, info->sec, (uint64_t) irel->r_offset, howto->name);
  2537. return true;
  2538. }
  2539. /* Can't relax dynamic symbols. */
  2540. if (info->h != NULL
  2541. && alpha_elf_dynamic_symbol_p (&info->h->root, info->link_info))
  2542. return true;
  2543. /* Can't use local-exec relocations in shared libraries. */
  2544. if (r_type == R_ALPHA_GOTTPREL
  2545. && bfd_link_dll (info->link_info))
  2546. return true;
  2547. if (r_type == R_ALPHA_LITERAL)
  2548. {
  2549. /* Look for nice constant addresses. This includes the not-uncommon
  2550. special case of 0 for undefweak symbols. */
  2551. if ((info->h && info->h->root.root.type == bfd_link_hash_undefweak)
  2552. || (!bfd_link_pic (info->link_info)
  2553. && (symval >= (bfd_vma)-0x8000 || symval < 0x8000)))
  2554. {
  2555. disp = 0;
  2556. insn = (OP_LDA << 26) | (insn & (31 << 21)) | (31 << 16);
  2557. insn |= (symval & 0xffff);
  2558. r_type = R_ALPHA_NONE;
  2559. }
  2560. else
  2561. {
  2562. /* We may only create GPREL relocs during the second pass. */
  2563. if (info->link_info->relax_pass == 0)
  2564. return true;
  2565. disp = symval - info->gp;
  2566. insn = (OP_LDA << 26) | (insn & 0x03ff0000);
  2567. r_type = R_ALPHA_GPREL16;
  2568. }
  2569. }
  2570. else
  2571. {
  2572. bfd_vma dtp_base, tp_base;
  2573. BFD_ASSERT (elf_hash_table (info->link_info)->tls_sec != NULL);
  2574. dtp_base = alpha_get_dtprel_base (info->link_info);
  2575. tp_base = alpha_get_tprel_base (info->link_info);
  2576. disp = symval - (r_type == R_ALPHA_GOTDTPREL ? dtp_base : tp_base);
  2577. insn = (OP_LDA << 26) | (insn & (31 << 21)) | (31 << 16);
  2578. switch (r_type)
  2579. {
  2580. case R_ALPHA_GOTDTPREL:
  2581. r_type = R_ALPHA_DTPREL16;
  2582. break;
  2583. case R_ALPHA_GOTTPREL:
  2584. r_type = R_ALPHA_TPREL16;
  2585. break;
  2586. default:
  2587. BFD_ASSERT (0);
  2588. return false;
  2589. }
  2590. }
  2591. if (disp < -0x8000 || disp >= 0x8000)
  2592. return true;
  2593. bfd_put_32 (info->abfd, (bfd_vma) insn, info->contents + irel->r_offset);
  2594. info->changed_contents = true;
  2595. /* Reduce the use count on this got entry by one, possibly
  2596. eliminating it. */
  2597. if (--info->gotent->use_count == 0)
  2598. {
  2599. int sz = alpha_got_entry_size (r_type);
  2600. alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
  2601. if (!info->h)
  2602. alpha_elf_tdata (info->gotobj)->local_got_size -= sz;
  2603. }
  2604. /* Smash the existing GOT relocation for its 16-bit immediate pair. */
  2605. irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info), r_type);
  2606. info->changed_relocs = true;
  2607. /* ??? Search forward through this basic block looking for insns
  2608. that use the target register. Stop after an insn modifying the
  2609. register is seen, or after a branch or call.
  2610. Any such memory load insn may be substituted by a load directly
  2611. off the GP. This allows the memory load insn to be issued before
  2612. the calculated GP register would otherwise be ready.
  2613. Any such jsr insn can be replaced by a bsr if it is in range.
  2614. This would mean that we'd have to _add_ relocations, the pain of
  2615. which gives one pause. */
  2616. return true;
  2617. }
  2618. static bfd_vma
  2619. elf64_alpha_relax_opt_call (struct alpha_relax_info *info, bfd_vma symval)
  2620. {
  2621. /* If the function has the same gp, and we can identify that the
  2622. function does not use its function pointer, we can eliminate the
  2623. address load. */
  2624. /* If the symbol is marked NOPV, we are being told the function never
  2625. needs its procedure value. */
  2626. if ((info->other & STO_ALPHA_STD_GPLOAD) == STO_ALPHA_NOPV)
  2627. return symval;
  2628. /* If the symbol is marked STD_GP, we are being told the function does
  2629. a normal ldgp in the first two words. */
  2630. else if ((info->other & STO_ALPHA_STD_GPLOAD) == STO_ALPHA_STD_GPLOAD)
  2631. ;
  2632. /* Otherwise, we may be able to identify a GP load in the first two
  2633. words, which we can then skip. */
  2634. else
  2635. {
  2636. Elf_Internal_Rela *tsec_relocs, *tsec_relend, *tsec_free, *gpdisp;
  2637. bfd_vma ofs;
  2638. /* Load the relocations from the section that the target symbol is in. */
  2639. if (info->sec == info->tsec)
  2640. {
  2641. tsec_relocs = info->relocs;
  2642. tsec_relend = info->relend;
  2643. tsec_free = NULL;
  2644. }
  2645. else
  2646. {
  2647. tsec_relocs = (_bfd_elf_link_read_relocs
  2648. (info->abfd, info->tsec, NULL,
  2649. (Elf_Internal_Rela *) NULL,
  2650. info->link_info->keep_memory));
  2651. if (tsec_relocs == NULL)
  2652. return 0;
  2653. tsec_relend = tsec_relocs + info->tsec->reloc_count;
  2654. tsec_free = (elf_section_data (info->tsec)->relocs == tsec_relocs
  2655. ? NULL
  2656. : tsec_relocs);
  2657. }
  2658. /* Recover the symbol's offset within the section. */
  2659. ofs = (symval - info->tsec->output_section->vma
  2660. - info->tsec->output_offset);
  2661. /* Look for a GPDISP reloc. */
  2662. gpdisp = (elf64_alpha_find_reloc_at_ofs
  2663. (tsec_relocs, tsec_relend, ofs, R_ALPHA_GPDISP));
  2664. if (!gpdisp || gpdisp->r_addend != 4)
  2665. {
  2666. free (tsec_free);
  2667. return 0;
  2668. }
  2669. free (tsec_free);
  2670. }
  2671. /* We've now determined that we can skip an initial gp load. Verify
  2672. that the call and the target use the same gp. */
  2673. if (info->link_info->output_bfd->xvec != info->tsec->owner->xvec
  2674. || info->gotobj != alpha_elf_tdata (info->tsec->owner)->gotobj)
  2675. return 0;
  2676. return symval + 8;
  2677. }
  2678. static bool
  2679. elf64_alpha_relax_with_lituse (struct alpha_relax_info *info,
  2680. bfd_vma symval, Elf_Internal_Rela *irel)
  2681. {
  2682. Elf_Internal_Rela *urel, *erel, *irelend = info->relend;
  2683. int flags;
  2684. bfd_signed_vma disp;
  2685. bool fits16;
  2686. bool fits32;
  2687. bool lit_reused = false;
  2688. bool all_optimized = true;
  2689. bool changed_contents;
  2690. bool changed_relocs;
  2691. bfd_byte *contents = info->contents;
  2692. bfd *abfd = info->abfd;
  2693. bfd_vma sec_output_vma;
  2694. unsigned int lit_insn;
  2695. int relax_pass;
  2696. lit_insn = bfd_get_32 (abfd, contents + irel->r_offset);
  2697. if (lit_insn >> 26 != OP_LDQ)
  2698. {
  2699. _bfd_error_handler
  2700. /* xgettext:c-format */
  2701. (_("%pB: %pA+%#" PRIx64 ": warning: "
  2702. "%s relocation against unexpected insn"),
  2703. abfd, info->sec, (uint64_t) irel->r_offset, "LITERAL");
  2704. return true;
  2705. }
  2706. /* Can't relax dynamic symbols. */
  2707. if (info->h != NULL
  2708. && alpha_elf_dynamic_symbol_p (&info->h->root, info->link_info))
  2709. return true;
  2710. changed_contents = info->changed_contents;
  2711. changed_relocs = info->changed_relocs;
  2712. sec_output_vma = info->sec->output_section->vma + info->sec->output_offset;
  2713. relax_pass = info->link_info->relax_pass;
  2714. /* Summarize how this particular LITERAL is used. */
  2715. for (erel = irel+1, flags = 0; erel < irelend; ++erel)
  2716. {
  2717. if (ELF64_R_TYPE (erel->r_info) != R_ALPHA_LITUSE)
  2718. break;
  2719. if (erel->r_addend <= 6)
  2720. flags |= 1 << erel->r_addend;
  2721. }
  2722. /* A little preparation for the loop... */
  2723. disp = symval - info->gp;
  2724. for (urel = irel+1; urel < erel; ++urel)
  2725. {
  2726. bfd_vma urel_r_offset = urel->r_offset;
  2727. unsigned int insn;
  2728. int insn_disp;
  2729. bfd_signed_vma xdisp;
  2730. Elf_Internal_Rela nrel;
  2731. insn = bfd_get_32 (abfd, contents + urel_r_offset);
  2732. switch (urel->r_addend)
  2733. {
  2734. case LITUSE_ALPHA_ADDR:
  2735. default:
  2736. /* This type is really just a placeholder to note that all
  2737. uses cannot be optimized, but to still allow some. */
  2738. all_optimized = false;
  2739. break;
  2740. case LITUSE_ALPHA_BASE:
  2741. /* We may only create GPREL relocs during the second pass. */
  2742. if (relax_pass == 0)
  2743. {
  2744. all_optimized = false;
  2745. break;
  2746. }
  2747. /* We can always optimize 16-bit displacements. */
  2748. /* Extract the displacement from the instruction, sign-extending
  2749. it if necessary, then test whether it is within 16 or 32 bits
  2750. displacement from GP. */
  2751. insn_disp = ((insn & 0xffff) ^ 0x8000) - 0x8000;
  2752. xdisp = disp + insn_disp;
  2753. fits16 = (xdisp >= - (bfd_signed_vma) 0x8000 && xdisp < 0x8000);
  2754. fits32 = (xdisp >= - (bfd_signed_vma) 0x80000000
  2755. && xdisp < 0x7fff8000);
  2756. if (fits16)
  2757. {
  2758. /* Take the op code and dest from this insn, take the base
  2759. register from the literal insn. Leave the offset alone. */
  2760. insn = (insn & 0xffe0ffff) | (lit_insn & 0x001f0000);
  2761. bfd_put_32 (abfd, (bfd_vma) insn, contents + urel_r_offset);
  2762. changed_contents = true;
  2763. nrel = *urel;
  2764. nrel.r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
  2765. R_ALPHA_GPREL16);
  2766. nrel.r_addend = irel->r_addend;
  2767. /* As we adjust, move the reloc to the end so that we don't
  2768. break the LITERAL+LITUSE chain. */
  2769. if (urel < --erel)
  2770. *urel-- = *erel;
  2771. *erel = nrel;
  2772. changed_relocs = true;
  2773. }
  2774. /* If all mem+byte, we can optimize 32-bit mem displacements. */
  2775. else if (fits32 && !(flags & ~6))
  2776. {
  2777. /* FIXME: sanity check that lit insn Ra is mem insn Rb. */
  2778. irel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
  2779. R_ALPHA_GPRELHIGH);
  2780. lit_insn = (OP_LDAH << 26) | (lit_insn & 0x03ff0000);
  2781. bfd_put_32 (abfd, (bfd_vma) lit_insn, contents + irel->r_offset);
  2782. lit_reused = true;
  2783. changed_contents = true;
  2784. /* Since all relocs must be optimized, don't bother swapping
  2785. this relocation to the end. */
  2786. urel->r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
  2787. R_ALPHA_GPRELLOW);
  2788. urel->r_addend = irel->r_addend;
  2789. changed_relocs = true;
  2790. }
  2791. else
  2792. all_optimized = false;
  2793. break;
  2794. case LITUSE_ALPHA_BYTOFF:
  2795. /* We can always optimize byte instructions. */
  2796. /* FIXME: sanity check the insn for byte op. Check that the
  2797. literal dest reg is indeed Rb in the byte insn. */
  2798. insn &= ~ (unsigned) 0x001ff000;
  2799. insn |= ((symval & 7) << 13) | 0x1000;
  2800. bfd_put_32 (abfd, (bfd_vma) insn, contents + urel_r_offset);
  2801. changed_contents = true;
  2802. nrel = *urel;
  2803. nrel.r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
  2804. nrel.r_addend = 0;
  2805. /* As we adjust, move the reloc to the end so that we don't
  2806. break the LITERAL+LITUSE chain. */
  2807. if (urel < --erel)
  2808. *urel-- = *erel;
  2809. *erel = nrel;
  2810. changed_relocs = true;
  2811. break;
  2812. case LITUSE_ALPHA_JSR:
  2813. case LITUSE_ALPHA_TLSGD:
  2814. case LITUSE_ALPHA_TLSLDM:
  2815. case LITUSE_ALPHA_JSRDIRECT:
  2816. {
  2817. bfd_vma optdest, org;
  2818. bfd_signed_vma odisp;
  2819. /* For undefined weak symbols, we're mostly interested in getting
  2820. rid of the got entry whenever possible, so optimize this to a
  2821. use of the zero register. */
  2822. if (info->h && info->h->root.root.type == bfd_link_hash_undefweak)
  2823. {
  2824. insn |= 31 << 16;
  2825. bfd_put_32 (abfd, (bfd_vma) insn, contents + urel_r_offset);
  2826. changed_contents = true;
  2827. break;
  2828. }
  2829. /* If not zero, place to jump without needing pv. */
  2830. optdest = elf64_alpha_relax_opt_call (info, symval);
  2831. org = sec_output_vma + urel_r_offset + 4;
  2832. odisp = (optdest ? optdest : symval) - org;
  2833. if (odisp >= -0x400000 && odisp < 0x400000)
  2834. {
  2835. Elf_Internal_Rela *xrel;
  2836. /* Preserve branch prediction call stack when possible. */
  2837. if ((insn & INSN_JSR_MASK) == INSN_JSR)
  2838. insn = (OP_BSR << 26) | (insn & 0x03e00000);
  2839. else
  2840. insn = (OP_BR << 26) | (insn & 0x03e00000);
  2841. bfd_put_32 (abfd, (bfd_vma) insn, contents + urel_r_offset);
  2842. changed_contents = true;
  2843. nrel = *urel;
  2844. nrel.r_info = ELF64_R_INFO (ELF64_R_SYM (irel->r_info),
  2845. R_ALPHA_BRADDR);
  2846. nrel.r_addend = irel->r_addend;
  2847. if (optdest)
  2848. nrel.r_addend += optdest - symval;
  2849. else
  2850. all_optimized = false;
  2851. /* Kill any HINT reloc that might exist for this insn. */
  2852. xrel = (elf64_alpha_find_reloc_at_ofs
  2853. (info->relocs, info->relend, urel_r_offset,
  2854. R_ALPHA_HINT));
  2855. if (xrel)
  2856. xrel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
  2857. /* As we adjust, move the reloc to the end so that we don't
  2858. break the LITERAL+LITUSE chain. */
  2859. if (urel < --erel)
  2860. *urel-- = *erel;
  2861. *erel = nrel;
  2862. info->changed_relocs = true;
  2863. }
  2864. else
  2865. all_optimized = false;
  2866. /* Even if the target is not in range for a direct branch,
  2867. if we share a GP, we can eliminate the gp reload. */
  2868. if (optdest)
  2869. {
  2870. Elf_Internal_Rela *gpdisp
  2871. = (elf64_alpha_find_reloc_at_ofs
  2872. (info->relocs, irelend, urel_r_offset + 4,
  2873. R_ALPHA_GPDISP));
  2874. if (gpdisp)
  2875. {
  2876. bfd_byte *p_ldah = contents + gpdisp->r_offset;
  2877. bfd_byte *p_lda = p_ldah + gpdisp->r_addend;
  2878. unsigned int ldah = bfd_get_32 (abfd, p_ldah);
  2879. unsigned int lda = bfd_get_32 (abfd, p_lda);
  2880. /* Verify that the instruction is "ldah $29,0($26)".
  2881. Consider a function that ends in a noreturn call,
  2882. and that the next function begins with an ldgp,
  2883. and that by accident there is no padding between.
  2884. In that case the insn would use $27 as the base. */
  2885. if (ldah == 0x27ba0000 && lda == 0x23bd0000)
  2886. {
  2887. bfd_put_32 (abfd, (bfd_vma) INSN_UNOP, p_ldah);
  2888. bfd_put_32 (abfd, (bfd_vma) INSN_UNOP, p_lda);
  2889. gpdisp->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
  2890. changed_contents = true;
  2891. changed_relocs = true;
  2892. }
  2893. }
  2894. }
  2895. }
  2896. break;
  2897. }
  2898. }
  2899. /* If we reused the literal instruction, we must have optimized all. */
  2900. BFD_ASSERT(!lit_reused || all_optimized);
  2901. /* If all cases were optimized, we can reduce the use count on this
  2902. got entry by one, possibly eliminating it. */
  2903. if (all_optimized)
  2904. {
  2905. if (--info->gotent->use_count == 0)
  2906. {
  2907. int sz = alpha_got_entry_size (R_ALPHA_LITERAL);
  2908. alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
  2909. if (!info->h)
  2910. alpha_elf_tdata (info->gotobj)->local_got_size -= sz;
  2911. }
  2912. /* If the literal instruction is no longer needed (it may have been
  2913. reused. We can eliminate it. */
  2914. /* ??? For now, I don't want to deal with compacting the section,
  2915. so just nop it out. */
  2916. if (!lit_reused)
  2917. {
  2918. irel->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
  2919. changed_relocs = true;
  2920. bfd_put_32 (abfd, (bfd_vma) INSN_UNOP, contents + irel->r_offset);
  2921. changed_contents = true;
  2922. }
  2923. }
  2924. info->changed_contents = changed_contents;
  2925. info->changed_relocs = changed_relocs;
  2926. if (all_optimized || relax_pass == 0)
  2927. return true;
  2928. return elf64_alpha_relax_got_load (info, symval, irel, R_ALPHA_LITERAL);
  2929. }
  2930. static bool
  2931. elf64_alpha_relax_tls_get_addr (struct alpha_relax_info *info, bfd_vma symval,
  2932. Elf_Internal_Rela *irel, bool is_gd)
  2933. {
  2934. bfd_byte *pos[5];
  2935. unsigned int insn, tlsgd_reg;
  2936. Elf_Internal_Rela *gpdisp, *hint;
  2937. bool dynamic, use_gottprel;
  2938. unsigned long new_symndx;
  2939. dynamic = (info->h != NULL
  2940. && alpha_elf_dynamic_symbol_p (&info->h->root, info->link_info));
  2941. /* If a TLS symbol is accessed using IE at least once, there is no point
  2942. to use dynamic model for it. */
  2943. if (is_gd && info->h && (info->h->flags & ALPHA_ELF_LINK_HASH_TLS_IE))
  2944. ;
  2945. /* If the symbol is local, and we've already committed to DF_STATIC_TLS,
  2946. then we might as well relax to IE. */
  2947. else if (bfd_link_pic (info->link_info) && !dynamic
  2948. && (info->link_info->flags & DF_STATIC_TLS))
  2949. ;
  2950. /* Otherwise we must be building an executable to do anything. */
  2951. else if (bfd_link_pic (info->link_info))
  2952. return true;
  2953. /* The TLSGD/TLSLDM relocation must be followed by a LITERAL and
  2954. the matching LITUSE_TLS relocations. */
  2955. if (irel + 2 >= info->relend)
  2956. return true;
  2957. if (ELF64_R_TYPE (irel[1].r_info) != R_ALPHA_LITERAL
  2958. || ELF64_R_TYPE (irel[2].r_info) != R_ALPHA_LITUSE
  2959. || irel[2].r_addend != (is_gd ? LITUSE_ALPHA_TLSGD : LITUSE_ALPHA_TLSLDM))
  2960. return true;
  2961. /* There must be a GPDISP relocation positioned immediately after the
  2962. LITUSE relocation. */
  2963. gpdisp = elf64_alpha_find_reloc_at_ofs (info->relocs, info->relend,
  2964. irel[2].r_offset + 4, R_ALPHA_GPDISP);
  2965. if (!gpdisp)
  2966. return true;
  2967. pos[0] = info->contents + irel[0].r_offset;
  2968. pos[1] = info->contents + irel[1].r_offset;
  2969. pos[2] = info->contents + irel[2].r_offset;
  2970. pos[3] = info->contents + gpdisp->r_offset;
  2971. pos[4] = pos[3] + gpdisp->r_addend;
  2972. /* Beware of the compiler hoisting part of the sequence out a loop
  2973. and adjusting the destination register for the TLSGD insn. If this
  2974. happens, there will be a move into $16 before the JSR insn, so only
  2975. transformations of the first insn pair should use this register. */
  2976. tlsgd_reg = bfd_get_32 (info->abfd, pos[0]);
  2977. tlsgd_reg = (tlsgd_reg >> 21) & 31;
  2978. /* Generally, the positions are not allowed to be out of order, lest the
  2979. modified insn sequence have different register lifetimes. We can make
  2980. an exception when pos 1 is adjacent to pos 0. */
  2981. if (pos[1] + 4 == pos[0])
  2982. {
  2983. bfd_byte *tmp = pos[0];
  2984. pos[0] = pos[1];
  2985. pos[1] = tmp;
  2986. }
  2987. if (pos[1] >= pos[2] || pos[2] >= pos[3])
  2988. return true;
  2989. /* Reduce the use count on the LITERAL relocation. Do this before we
  2990. smash the symndx when we adjust the relocations below. */
  2991. {
  2992. struct alpha_elf_got_entry *lit_gotent;
  2993. struct alpha_elf_link_hash_entry *lit_h;
  2994. unsigned long indx;
  2995. BFD_ASSERT (ELF64_R_SYM (irel[1].r_info) >= info->symtab_hdr->sh_info);
  2996. indx = ELF64_R_SYM (irel[1].r_info) - info->symtab_hdr->sh_info;
  2997. lit_h = alpha_elf_sym_hashes (info->abfd)[indx];
  2998. while (lit_h->root.root.type == bfd_link_hash_indirect
  2999. || lit_h->root.root.type == bfd_link_hash_warning)
  3000. lit_h = (struct alpha_elf_link_hash_entry *) lit_h->root.root.u.i.link;
  3001. for (lit_gotent = lit_h->got_entries; lit_gotent ;
  3002. lit_gotent = lit_gotent->next)
  3003. if (lit_gotent->gotobj == info->gotobj
  3004. && lit_gotent->reloc_type == R_ALPHA_LITERAL
  3005. && lit_gotent->addend == irel[1].r_addend)
  3006. break;
  3007. BFD_ASSERT (lit_gotent);
  3008. if (--lit_gotent->use_count == 0)
  3009. {
  3010. int sz = alpha_got_entry_size (R_ALPHA_LITERAL);
  3011. alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
  3012. }
  3013. }
  3014. /* Change
  3015. lda $16,x($gp) !tlsgd!1
  3016. ldq $27,__tls_get_addr($gp) !literal!1
  3017. jsr $26,($27),__tls_get_addr !lituse_tlsgd!1
  3018. ldah $29,0($26) !gpdisp!2
  3019. lda $29,0($29) !gpdisp!2
  3020. to
  3021. ldq $16,x($gp) !gottprel
  3022. unop
  3023. call_pal rduniq
  3024. addq $16,$0,$0
  3025. unop
  3026. or the first pair to
  3027. lda $16,x($gp) !tprel
  3028. unop
  3029. or
  3030. ldah $16,x($gp) !tprelhi
  3031. lda $16,x($16) !tprello
  3032. as appropriate. */
  3033. use_gottprel = false;
  3034. new_symndx = is_gd ? ELF64_R_SYM (irel->r_info) : STN_UNDEF;
  3035. /* Some compilers warn about a Boolean-looking expression being
  3036. used in a switch. The explicit cast silences them. */
  3037. switch ((int) (!dynamic && !bfd_link_pic (info->link_info)))
  3038. {
  3039. case 1:
  3040. {
  3041. bfd_vma tp_base;
  3042. bfd_signed_vma disp;
  3043. BFD_ASSERT (elf_hash_table (info->link_info)->tls_sec != NULL);
  3044. tp_base = alpha_get_tprel_base (info->link_info);
  3045. disp = symval - tp_base;
  3046. if (disp >= -0x8000 && disp < 0x8000)
  3047. {
  3048. insn = (OP_LDA << 26) | (tlsgd_reg << 21) | (31 << 16);
  3049. bfd_put_32 (info->abfd, (bfd_vma) insn, pos[0]);
  3050. bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos[1]);
  3051. irel[0].r_offset = pos[0] - info->contents;
  3052. irel[0].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_TPREL16);
  3053. irel[1].r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
  3054. break;
  3055. }
  3056. else if (disp >= -(bfd_signed_vma) 0x80000000
  3057. && disp < (bfd_signed_vma) 0x7fff8000
  3058. && pos[0] + 4 == pos[1])
  3059. {
  3060. insn = (OP_LDAH << 26) | (tlsgd_reg << 21) | (31 << 16);
  3061. bfd_put_32 (info->abfd, (bfd_vma) insn, pos[0]);
  3062. insn = (OP_LDA << 26) | (tlsgd_reg << 21) | (tlsgd_reg << 16);
  3063. bfd_put_32 (info->abfd, (bfd_vma) insn, pos[1]);
  3064. irel[0].r_offset = pos[0] - info->contents;
  3065. irel[0].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_TPRELHI);
  3066. irel[1].r_offset = pos[1] - info->contents;
  3067. irel[1].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_TPRELLO);
  3068. break;
  3069. }
  3070. }
  3071. /* FALLTHRU */
  3072. default:
  3073. use_gottprel = true;
  3074. insn = (OP_LDQ << 26) | (tlsgd_reg << 21) | (29 << 16);
  3075. bfd_put_32 (info->abfd, (bfd_vma) insn, pos[0]);
  3076. bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos[1]);
  3077. irel[0].r_offset = pos[0] - info->contents;
  3078. irel[0].r_info = ELF64_R_INFO (new_symndx, R_ALPHA_GOTTPREL);
  3079. irel[1].r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
  3080. break;
  3081. }
  3082. bfd_put_32 (info->abfd, (bfd_vma) INSN_RDUNIQ, pos[2]);
  3083. insn = INSN_ADDQ | (16 << 21) | (0 << 16) | (0 << 0);
  3084. bfd_put_32 (info->abfd, (bfd_vma) insn, pos[3]);
  3085. bfd_put_32 (info->abfd, (bfd_vma) INSN_UNOP, pos[4]);
  3086. irel[2].r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
  3087. gpdisp->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
  3088. hint = elf64_alpha_find_reloc_at_ofs (info->relocs, info->relend,
  3089. irel[2].r_offset, R_ALPHA_HINT);
  3090. if (hint)
  3091. hint->r_info = ELF64_R_INFO (0, R_ALPHA_NONE);
  3092. info->changed_contents = true;
  3093. info->changed_relocs = true;
  3094. /* Reduce the use count on the TLSGD/TLSLDM relocation. */
  3095. if (--info->gotent->use_count == 0)
  3096. {
  3097. int sz = alpha_got_entry_size (info->gotent->reloc_type);
  3098. alpha_elf_tdata (info->gotobj)->total_got_size -= sz;
  3099. if (!info->h)
  3100. alpha_elf_tdata (info->gotobj)->local_got_size -= sz;
  3101. }
  3102. /* If we've switched to a GOTTPREL relocation, increment the reference
  3103. count on that got entry. */
  3104. if (use_gottprel)
  3105. {
  3106. struct alpha_elf_got_entry *tprel_gotent;
  3107. for (tprel_gotent = *info->first_gotent; tprel_gotent ;
  3108. tprel_gotent = tprel_gotent->next)
  3109. if (tprel_gotent->gotobj == info->gotobj
  3110. && tprel_gotent->reloc_type == R_ALPHA_GOTTPREL
  3111. && tprel_gotent->addend == irel->r_addend)
  3112. break;
  3113. if (tprel_gotent)
  3114. tprel_gotent->use_count++;
  3115. else
  3116. {
  3117. if (info->gotent->use_count == 0)
  3118. tprel_gotent = info->gotent;
  3119. else
  3120. {
  3121. tprel_gotent = (struct alpha_elf_got_entry *)
  3122. bfd_alloc (info->abfd, sizeof (struct alpha_elf_got_entry));
  3123. if (!tprel_gotent)
  3124. return false;
  3125. tprel_gotent->next = *info->first_gotent;
  3126. *info->first_gotent = tprel_gotent;
  3127. tprel_gotent->gotobj = info->gotobj;
  3128. tprel_gotent->addend = irel->r_addend;
  3129. tprel_gotent->got_offset = -1;
  3130. tprel_gotent->reloc_done = 0;
  3131. tprel_gotent->reloc_xlated = 0;
  3132. }
  3133. tprel_gotent->use_count = 1;
  3134. tprel_gotent->reloc_type = R_ALPHA_GOTTPREL;
  3135. }
  3136. }
  3137. return true;
  3138. }
  3139. static bool
  3140. elf64_alpha_relax_section (bfd *abfd, asection *sec,
  3141. struct bfd_link_info *link_info, bool *again)
  3142. {
  3143. Elf_Internal_Shdr *symtab_hdr;
  3144. Elf_Internal_Rela *internal_relocs;
  3145. Elf_Internal_Rela *irel, *irelend;
  3146. Elf_Internal_Sym *isymbuf = NULL;
  3147. struct alpha_elf_got_entry **local_got_entries;
  3148. struct alpha_relax_info info;
  3149. struct alpha_elf_link_hash_table * htab;
  3150. int relax_pass;
  3151. htab = alpha_elf_hash_table (link_info);
  3152. if (htab == NULL)
  3153. return false;
  3154. /* There's nothing to change, yet. */
  3155. *again = false;
  3156. if (bfd_link_relocatable (link_info)
  3157. || ((sec->flags & (SEC_CODE | SEC_RELOC | SEC_ALLOC))
  3158. != (SEC_CODE | SEC_RELOC | SEC_ALLOC))
  3159. || sec->reloc_count == 0)
  3160. return true;
  3161. BFD_ASSERT (is_alpha_elf (abfd));
  3162. relax_pass = link_info->relax_pass;
  3163. /* Make sure our GOT and PLT tables are up-to-date. */
  3164. if (htab->relax_trip != link_info->relax_trip)
  3165. {
  3166. htab->relax_trip = link_info->relax_trip;
  3167. /* This should never fail after the initial round, since the only error
  3168. is GOT overflow, and relaxation only shrinks the table. However, we
  3169. may only merge got sections during the first pass. If we merge
  3170. sections after we've created GPREL relocs, the GP for the merged
  3171. section backs up which may put the relocs out of range. */
  3172. if (!elf64_alpha_size_got_sections (link_info, relax_pass == 0))
  3173. abort ();
  3174. if (elf_hash_table (link_info)->dynamic_sections_created)
  3175. {
  3176. elf64_alpha_size_plt_section (link_info);
  3177. elf64_alpha_size_rela_got_section (link_info);
  3178. }
  3179. }
  3180. symtab_hdr = &elf_symtab_hdr (abfd);
  3181. local_got_entries = alpha_elf_tdata(abfd)->local_got_entries;
  3182. /* Load the relocations for this section. */
  3183. internal_relocs = (_bfd_elf_link_read_relocs
  3184. (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
  3185. link_info->keep_memory));
  3186. if (internal_relocs == NULL)
  3187. return false;
  3188. memset(&info, 0, sizeof (info));
  3189. info.abfd = abfd;
  3190. info.sec = sec;
  3191. info.link_info = link_info;
  3192. info.symtab_hdr = symtab_hdr;
  3193. info.relocs = internal_relocs;
  3194. info.relend = irelend = internal_relocs + sec->reloc_count;
  3195. /* Find the GP for this object. Do not store the result back via
  3196. _bfd_set_gp_value, since this could change again before final. */
  3197. info.gotobj = alpha_elf_tdata (abfd)->gotobj;
  3198. if (info.gotobj)
  3199. {
  3200. asection *sgot = alpha_elf_tdata (info.gotobj)->got;
  3201. info.gp = (sgot->output_section->vma
  3202. + sgot->output_offset
  3203. + 0x8000);
  3204. }
  3205. /* Get the section contents. */
  3206. if (elf_section_data (sec)->this_hdr.contents != NULL)
  3207. info.contents = elf_section_data (sec)->this_hdr.contents;
  3208. else
  3209. {
  3210. if (!bfd_malloc_and_get_section (abfd, sec, &info.contents))
  3211. goto error_return;
  3212. }
  3213. for (irel = internal_relocs; irel < irelend; irel++)
  3214. {
  3215. bfd_vma symval;
  3216. struct alpha_elf_got_entry *gotent;
  3217. unsigned long r_type = ELF64_R_TYPE (irel->r_info);
  3218. unsigned long r_symndx = ELF64_R_SYM (irel->r_info);
  3219. /* Early exit for unhandled or unrelaxable relocations. */
  3220. if (r_type != R_ALPHA_LITERAL)
  3221. {
  3222. /* We complete everything except LITERAL in the first pass. */
  3223. if (relax_pass != 0)
  3224. continue;
  3225. if (r_type == R_ALPHA_TLSLDM)
  3226. {
  3227. /* The symbol for a TLSLDM reloc is ignored. Collapse the
  3228. reloc to the STN_UNDEF (0) symbol so that they all match. */
  3229. r_symndx = STN_UNDEF;
  3230. }
  3231. else if (r_type != R_ALPHA_GOTDTPREL
  3232. && r_type != R_ALPHA_GOTTPREL
  3233. && r_type != R_ALPHA_TLSGD)
  3234. continue;
  3235. }
  3236. /* Get the value of the symbol referred to by the reloc. */
  3237. if (r_symndx < symtab_hdr->sh_info)
  3238. {
  3239. /* A local symbol. */
  3240. Elf_Internal_Sym *isym;
  3241. /* Read this BFD's local symbols. */
  3242. if (isymbuf == NULL)
  3243. {
  3244. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  3245. if (isymbuf == NULL)
  3246. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  3247. symtab_hdr->sh_info, 0,
  3248. NULL, NULL, NULL);
  3249. if (isymbuf == NULL)
  3250. goto error_return;
  3251. }
  3252. isym = isymbuf + r_symndx;
  3253. /* Given the symbol for a TLSLDM reloc is ignored, this also
  3254. means forcing the symbol value to the tp base. */
  3255. if (r_type == R_ALPHA_TLSLDM)
  3256. {
  3257. info.tsec = bfd_abs_section_ptr;
  3258. symval = alpha_get_tprel_base (info.link_info);
  3259. }
  3260. else
  3261. {
  3262. symval = isym->st_value;
  3263. if (isym->st_shndx == SHN_UNDEF)
  3264. continue;
  3265. else if (isym->st_shndx == SHN_ABS)
  3266. info.tsec = bfd_abs_section_ptr;
  3267. else if (isym->st_shndx == SHN_COMMON)
  3268. info.tsec = bfd_com_section_ptr;
  3269. else
  3270. info.tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  3271. }
  3272. info.h = NULL;
  3273. info.other = isym->st_other;
  3274. if (local_got_entries)
  3275. info.first_gotent = &local_got_entries[r_symndx];
  3276. else
  3277. {
  3278. info.first_gotent = &info.gotent;
  3279. info.gotent = NULL;
  3280. }
  3281. }
  3282. else
  3283. {
  3284. unsigned long indx;
  3285. struct alpha_elf_link_hash_entry *h;
  3286. indx = r_symndx - symtab_hdr->sh_info;
  3287. h = alpha_elf_sym_hashes (abfd)[indx];
  3288. BFD_ASSERT (h != NULL);
  3289. while (h->root.root.type == bfd_link_hash_indirect
  3290. || h->root.root.type == bfd_link_hash_warning)
  3291. h = (struct alpha_elf_link_hash_entry *)h->root.root.u.i.link;
  3292. /* If the symbol is undefined, we can't do anything with it. */
  3293. if (h->root.root.type == bfd_link_hash_undefined)
  3294. continue;
  3295. /* If the symbol isn't defined in the current module,
  3296. again we can't do anything. */
  3297. if (h->root.root.type == bfd_link_hash_undefweak)
  3298. {
  3299. info.tsec = bfd_abs_section_ptr;
  3300. symval = 0;
  3301. }
  3302. else if (!h->root.def_regular)
  3303. {
  3304. /* Except for TLSGD relocs, which can sometimes be
  3305. relaxed to GOTTPREL relocs. */
  3306. if (r_type != R_ALPHA_TLSGD)
  3307. continue;
  3308. info.tsec = bfd_abs_section_ptr;
  3309. symval = 0;
  3310. }
  3311. else
  3312. {
  3313. info.tsec = h->root.root.u.def.section;
  3314. symval = h->root.root.u.def.value;
  3315. }
  3316. info.h = h;
  3317. info.other = h->root.other;
  3318. info.first_gotent = &h->got_entries;
  3319. }
  3320. /* Search for the got entry to be used by this relocation. */
  3321. for (gotent = *info.first_gotent; gotent ; gotent = gotent->next)
  3322. if (gotent->gotobj == info.gotobj
  3323. && gotent->reloc_type == r_type
  3324. && gotent->addend == irel->r_addend)
  3325. break;
  3326. info.gotent = gotent;
  3327. symval += info.tsec->output_section->vma + info.tsec->output_offset;
  3328. symval += irel->r_addend;
  3329. switch (r_type)
  3330. {
  3331. case R_ALPHA_LITERAL:
  3332. BFD_ASSERT(info.gotent != NULL);
  3333. /* If there exist LITUSE relocations immediately following, this
  3334. opens up all sorts of interesting optimizations, because we
  3335. now know every location that this address load is used. */
  3336. if (irel+1 < irelend
  3337. && ELF64_R_TYPE (irel[1].r_info) == R_ALPHA_LITUSE)
  3338. {
  3339. if (!elf64_alpha_relax_with_lituse (&info, symval, irel))
  3340. goto error_return;
  3341. }
  3342. else
  3343. {
  3344. if (!elf64_alpha_relax_got_load (&info, symval, irel, r_type))
  3345. goto error_return;
  3346. }
  3347. break;
  3348. case R_ALPHA_GOTDTPREL:
  3349. case R_ALPHA_GOTTPREL:
  3350. BFD_ASSERT(info.gotent != NULL);
  3351. if (!elf64_alpha_relax_got_load (&info, symval, irel, r_type))
  3352. goto error_return;
  3353. break;
  3354. case R_ALPHA_TLSGD:
  3355. case R_ALPHA_TLSLDM:
  3356. BFD_ASSERT(info.gotent != NULL);
  3357. if (!elf64_alpha_relax_tls_get_addr (&info, symval, irel,
  3358. r_type == R_ALPHA_TLSGD))
  3359. goto error_return;
  3360. break;
  3361. }
  3362. }
  3363. if (isymbuf != NULL
  3364. && symtab_hdr->contents != (unsigned char *) isymbuf)
  3365. {
  3366. if (!link_info->keep_memory)
  3367. free (isymbuf);
  3368. else
  3369. {
  3370. /* Cache the symbols for elf_link_input_bfd. */
  3371. symtab_hdr->contents = (unsigned char *) isymbuf;
  3372. }
  3373. }
  3374. if (info.contents != NULL
  3375. && elf_section_data (sec)->this_hdr.contents != info.contents)
  3376. {
  3377. if (!info.changed_contents && !link_info->keep_memory)
  3378. free (info.contents);
  3379. else
  3380. {
  3381. /* Cache the section contents for elf_link_input_bfd. */
  3382. elf_section_data (sec)->this_hdr.contents = info.contents;
  3383. }
  3384. }
  3385. if (elf_section_data (sec)->relocs != internal_relocs)
  3386. {
  3387. if (!info.changed_relocs)
  3388. free (internal_relocs);
  3389. else
  3390. elf_section_data (sec)->relocs = internal_relocs;
  3391. }
  3392. *again = info.changed_contents || info.changed_relocs;
  3393. return true;
  3394. error_return:
  3395. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  3396. free (isymbuf);
  3397. if (elf_section_data (sec)->this_hdr.contents != info.contents)
  3398. free (info.contents);
  3399. if (elf_section_data (sec)->relocs != internal_relocs)
  3400. free (internal_relocs);
  3401. return false;
  3402. }
  3403. /* Emit a dynamic relocation for (DYNINDX, RTYPE, ADDEND) at (SEC, OFFSET)
  3404. into the next available slot in SREL. */
  3405. static void
  3406. elf64_alpha_emit_dynrel (bfd *abfd, struct bfd_link_info *info,
  3407. asection *sec, asection *srel, bfd_vma offset,
  3408. long dynindx, long rtype, bfd_vma addend)
  3409. {
  3410. Elf_Internal_Rela outrel;
  3411. bfd_byte *loc;
  3412. BFD_ASSERT (srel != NULL);
  3413. outrel.r_info = ELF64_R_INFO (dynindx, rtype);
  3414. outrel.r_addend = addend;
  3415. offset = _bfd_elf_section_offset (abfd, info, sec, offset);
  3416. if ((offset | 1) != (bfd_vma) -1)
  3417. outrel.r_offset = sec->output_section->vma + sec->output_offset + offset;
  3418. else
  3419. memset (&outrel, 0, sizeof (outrel));
  3420. loc = srel->contents;
  3421. loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
  3422. bfd_elf64_swap_reloca_out (abfd, &outrel, loc);
  3423. BFD_ASSERT (sizeof (Elf64_External_Rela) * srel->reloc_count <= srel->size);
  3424. }
  3425. /* Relocate an Alpha ELF section for a relocatable link.
  3426. We don't have to change anything unless the reloc is against a section
  3427. symbol, in which case we have to adjust according to where the section
  3428. symbol winds up in the output section. */
  3429. static int
  3430. elf64_alpha_relocate_section_r (bfd *output_bfd ATTRIBUTE_UNUSED,
  3431. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  3432. bfd *input_bfd, asection *input_section,
  3433. bfd_byte *contents ATTRIBUTE_UNUSED,
  3434. Elf_Internal_Rela *relocs,
  3435. Elf_Internal_Sym *local_syms,
  3436. asection **local_sections)
  3437. {
  3438. unsigned long symtab_hdr_sh_info;
  3439. Elf_Internal_Rela *rel;
  3440. Elf_Internal_Rela *relend;
  3441. struct elf_link_hash_entry **sym_hashes;
  3442. bool ret_val = true;
  3443. symtab_hdr_sh_info = elf_symtab_hdr (input_bfd).sh_info;
  3444. sym_hashes = elf_sym_hashes (input_bfd);
  3445. relend = relocs + input_section->reloc_count;
  3446. for (rel = relocs; rel < relend; rel++)
  3447. {
  3448. unsigned long r_symndx;
  3449. Elf_Internal_Sym *sym;
  3450. asection *sec;
  3451. unsigned long r_type;
  3452. r_type = ELF64_R_TYPE (rel->r_info);
  3453. if (r_type >= R_ALPHA_max)
  3454. {
  3455. _bfd_error_handler
  3456. /* xgettext:c-format */
  3457. (_("%pB: unsupported relocation type %#x"),
  3458. input_bfd, (int) r_type);
  3459. bfd_set_error (bfd_error_bad_value);
  3460. ret_val = false;
  3461. continue;
  3462. }
  3463. /* The symbol associated with GPDISP and LITUSE is
  3464. immaterial. Only the addend is significant. */
  3465. if (r_type == R_ALPHA_GPDISP || r_type == R_ALPHA_LITUSE)
  3466. continue;
  3467. r_symndx = ELF64_R_SYM (rel->r_info);
  3468. if (r_symndx < symtab_hdr_sh_info)
  3469. {
  3470. sym = local_syms + r_symndx;
  3471. sec = local_sections[r_symndx];
  3472. }
  3473. else
  3474. {
  3475. struct elf_link_hash_entry *h;
  3476. h = sym_hashes[r_symndx - symtab_hdr_sh_info];
  3477. while (h->root.type == bfd_link_hash_indirect
  3478. || h->root.type == bfd_link_hash_warning)
  3479. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  3480. if (h->root.type != bfd_link_hash_defined
  3481. && h->root.type != bfd_link_hash_defweak)
  3482. continue;
  3483. sym = NULL;
  3484. sec = h->root.u.def.section;
  3485. }
  3486. if (sec != NULL && discarded_section (sec))
  3487. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  3488. rel, 1, relend,
  3489. elf64_alpha_howto_table + r_type, 0,
  3490. contents);
  3491. if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  3492. rel->r_addend += sec->output_offset;
  3493. }
  3494. return ret_val;
  3495. }
  3496. /* Relocate an Alpha ELF section. */
  3497. static int
  3498. elf64_alpha_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
  3499. bfd *input_bfd, asection *input_section,
  3500. bfd_byte *contents, Elf_Internal_Rela *relocs,
  3501. Elf_Internal_Sym *local_syms,
  3502. asection **local_sections)
  3503. {
  3504. Elf_Internal_Shdr *symtab_hdr;
  3505. Elf_Internal_Rela *rel;
  3506. Elf_Internal_Rela *relend;
  3507. asection *sgot, *srel, *srelgot;
  3508. bfd *dynobj, *gotobj;
  3509. bfd_vma gp, tp_base, dtp_base;
  3510. struct alpha_elf_got_entry **local_got_entries;
  3511. bool ret_val;
  3512. BFD_ASSERT (is_alpha_elf (input_bfd));
  3513. /* Handle relocatable links with a smaller loop. */
  3514. if (bfd_link_relocatable (info))
  3515. return elf64_alpha_relocate_section_r (output_bfd, info, input_bfd,
  3516. input_section, contents, relocs,
  3517. local_syms, local_sections);
  3518. /* This is a final link. */
  3519. ret_val = true;
  3520. symtab_hdr = &elf_symtab_hdr (input_bfd);
  3521. dynobj = elf_hash_table (info)->dynobj;
  3522. srelgot = elf_hash_table (info)->srelgot;
  3523. if (input_section->flags & SEC_ALLOC)
  3524. {
  3525. const char *section_name;
  3526. section_name = (bfd_elf_string_from_elf_section
  3527. (input_bfd, elf_elfheader(input_bfd)->e_shstrndx,
  3528. _bfd_elf_single_rel_hdr (input_section)->sh_name));
  3529. BFD_ASSERT(section_name != NULL);
  3530. srel = bfd_get_linker_section (dynobj, section_name);
  3531. }
  3532. else
  3533. srel = NULL;
  3534. /* Find the gp value for this input bfd. */
  3535. gotobj = alpha_elf_tdata (input_bfd)->gotobj;
  3536. if (gotobj)
  3537. {
  3538. sgot = alpha_elf_tdata (gotobj)->got;
  3539. gp = _bfd_get_gp_value (gotobj);
  3540. if (gp == 0)
  3541. {
  3542. gp = (sgot->output_section->vma
  3543. + sgot->output_offset
  3544. + 0x8000);
  3545. _bfd_set_gp_value (gotobj, gp);
  3546. }
  3547. }
  3548. else
  3549. {
  3550. sgot = NULL;
  3551. gp = 0;
  3552. }
  3553. local_got_entries = alpha_elf_tdata(input_bfd)->local_got_entries;
  3554. if (elf_hash_table (info)->tls_sec != NULL)
  3555. {
  3556. dtp_base = alpha_get_dtprel_base (info);
  3557. tp_base = alpha_get_tprel_base (info);
  3558. }
  3559. else
  3560. dtp_base = tp_base = 0;
  3561. relend = relocs + input_section->reloc_count;
  3562. for (rel = relocs; rel < relend; rel++)
  3563. {
  3564. struct alpha_elf_link_hash_entry *h = NULL;
  3565. struct alpha_elf_got_entry *gotent;
  3566. bfd_reloc_status_type r;
  3567. reloc_howto_type *howto;
  3568. unsigned long r_symndx;
  3569. Elf_Internal_Sym *sym = NULL;
  3570. asection *sec = NULL;
  3571. bfd_vma value;
  3572. bfd_vma addend;
  3573. bool dynamic_symbol_p;
  3574. bool unresolved_reloc = false;
  3575. bool undef_weak_ref = false;
  3576. unsigned long r_type;
  3577. r_type = ELF64_R_TYPE(rel->r_info);
  3578. if (r_type >= R_ALPHA_max)
  3579. {
  3580. _bfd_error_handler
  3581. /* xgettext:c-format */
  3582. (_("%pB: unsupported relocation type %#x"),
  3583. input_bfd, (int) r_type);
  3584. bfd_set_error (bfd_error_bad_value);
  3585. ret_val = false;
  3586. continue;
  3587. }
  3588. howto = elf64_alpha_howto_table + r_type;
  3589. r_symndx = ELF64_R_SYM(rel->r_info);
  3590. /* The symbol for a TLSLDM reloc is ignored. Collapse the
  3591. reloc to the STN_UNDEF (0) symbol so that they all match. */
  3592. if (r_type == R_ALPHA_TLSLDM)
  3593. r_symndx = STN_UNDEF;
  3594. if (r_symndx < symtab_hdr->sh_info)
  3595. {
  3596. asection *msec;
  3597. sym = local_syms + r_symndx;
  3598. sec = local_sections[r_symndx];
  3599. msec = sec;
  3600. value = _bfd_elf_rela_local_sym (output_bfd, sym, &msec, rel);
  3601. /* If this is a tp-relative relocation against sym STN_UNDEF (0),
  3602. this is hackery from relax_section. Force the value to
  3603. be the tls module base. */
  3604. if (r_symndx == STN_UNDEF
  3605. && (r_type == R_ALPHA_TLSLDM
  3606. || r_type == R_ALPHA_GOTTPREL
  3607. || r_type == R_ALPHA_TPREL64
  3608. || r_type == R_ALPHA_TPRELHI
  3609. || r_type == R_ALPHA_TPRELLO
  3610. || r_type == R_ALPHA_TPREL16))
  3611. value = dtp_base;
  3612. if (local_got_entries)
  3613. gotent = local_got_entries[r_symndx];
  3614. else
  3615. gotent = NULL;
  3616. /* Need to adjust local GOT entries' addends for SEC_MERGE
  3617. unless it has been done already. */
  3618. if ((sec->flags & SEC_MERGE)
  3619. && ELF_ST_TYPE (sym->st_info) == STT_SECTION
  3620. && sec->sec_info_type == SEC_INFO_TYPE_MERGE
  3621. && gotent
  3622. && !gotent->reloc_xlated)
  3623. {
  3624. struct alpha_elf_got_entry *ent;
  3625. for (ent = gotent; ent; ent = ent->next)
  3626. {
  3627. ent->reloc_xlated = 1;
  3628. if (ent->use_count == 0)
  3629. continue;
  3630. msec = sec;
  3631. ent->addend =
  3632. _bfd_merged_section_offset (output_bfd, &msec,
  3633. elf_section_data (sec)->
  3634. sec_info,
  3635. sym->st_value + ent->addend);
  3636. ent->addend -= sym->st_value;
  3637. ent->addend += msec->output_section->vma
  3638. + msec->output_offset
  3639. - sec->output_section->vma
  3640. - sec->output_offset;
  3641. }
  3642. }
  3643. dynamic_symbol_p = false;
  3644. }
  3645. else
  3646. {
  3647. bool warned, ignored;
  3648. struct elf_link_hash_entry *hh;
  3649. struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
  3650. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  3651. r_symndx, symtab_hdr, sym_hashes,
  3652. hh, sec, value,
  3653. unresolved_reloc, warned, ignored);
  3654. if (warned)
  3655. continue;
  3656. if (value == 0
  3657. && ! unresolved_reloc
  3658. && hh->root.type == bfd_link_hash_undefweak)
  3659. undef_weak_ref = true;
  3660. h = (struct alpha_elf_link_hash_entry *) hh;
  3661. dynamic_symbol_p = alpha_elf_dynamic_symbol_p (&h->root, info);
  3662. gotent = h->got_entries;
  3663. }
  3664. if (sec != NULL && discarded_section (sec))
  3665. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  3666. rel, 1, relend, howto, 0, contents);
  3667. addend = rel->r_addend;
  3668. value += addend;
  3669. /* Search for the proper got entry. */
  3670. for (; gotent ; gotent = gotent->next)
  3671. if (gotent->gotobj == gotobj
  3672. && gotent->reloc_type == r_type
  3673. && gotent->addend == addend)
  3674. break;
  3675. switch (r_type)
  3676. {
  3677. case R_ALPHA_GPDISP:
  3678. {
  3679. bfd_byte *p_ldah, *p_lda;
  3680. BFD_ASSERT(gp != 0);
  3681. value = (input_section->output_section->vma
  3682. + input_section->output_offset
  3683. + rel->r_offset);
  3684. p_ldah = contents + rel->r_offset;
  3685. p_lda = p_ldah + rel->r_addend;
  3686. r = elf64_alpha_do_reloc_gpdisp (input_bfd, gp - value,
  3687. p_ldah, p_lda);
  3688. }
  3689. break;
  3690. case R_ALPHA_LITERAL:
  3691. BFD_ASSERT(sgot != NULL);
  3692. BFD_ASSERT(gp != 0);
  3693. BFD_ASSERT(gotent != NULL);
  3694. BFD_ASSERT(gotent->use_count >= 1);
  3695. if (!gotent->reloc_done)
  3696. {
  3697. gotent->reloc_done = 1;
  3698. bfd_put_64 (output_bfd, value,
  3699. sgot->contents + gotent->got_offset);
  3700. /* If the symbol has been forced local, output a
  3701. RELATIVE reloc, otherwise it will be handled in
  3702. finish_dynamic_symbol. */
  3703. if (bfd_link_pic (info)
  3704. && !dynamic_symbol_p
  3705. && !undef_weak_ref)
  3706. elf64_alpha_emit_dynrel (output_bfd, info, sgot, srelgot,
  3707. gotent->got_offset, 0,
  3708. R_ALPHA_RELATIVE, value);
  3709. }
  3710. value = (sgot->output_section->vma
  3711. + sgot->output_offset
  3712. + gotent->got_offset);
  3713. value -= gp;
  3714. goto default_reloc;
  3715. case R_ALPHA_GPREL32:
  3716. case R_ALPHA_GPREL16:
  3717. case R_ALPHA_GPRELLOW:
  3718. if (dynamic_symbol_p)
  3719. {
  3720. _bfd_error_handler
  3721. /* xgettext:c-format */
  3722. (_("%pB: gp-relative relocation against dynamic symbol %s"),
  3723. input_bfd, h->root.root.root.string);
  3724. ret_val = false;
  3725. }
  3726. BFD_ASSERT(gp != 0);
  3727. value -= gp;
  3728. goto default_reloc;
  3729. case R_ALPHA_GPRELHIGH:
  3730. if (dynamic_symbol_p)
  3731. {
  3732. _bfd_error_handler
  3733. /* xgettext:c-format */
  3734. (_("%pB: gp-relative relocation against dynamic symbol %s"),
  3735. input_bfd, h->root.root.root.string);
  3736. ret_val = false;
  3737. }
  3738. BFD_ASSERT(gp != 0);
  3739. value -= gp;
  3740. value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
  3741. goto default_reloc;
  3742. case R_ALPHA_HINT:
  3743. /* A call to a dynamic symbol is definitely out of range of
  3744. the 16-bit displacement. Don't bother writing anything. */
  3745. if (dynamic_symbol_p)
  3746. {
  3747. r = bfd_reloc_ok;
  3748. break;
  3749. }
  3750. /* The regular PC-relative stuff measures from the start of
  3751. the instruction rather than the end. */
  3752. value -= 4;
  3753. goto default_reloc;
  3754. case R_ALPHA_BRADDR:
  3755. if (dynamic_symbol_p)
  3756. {
  3757. _bfd_error_handler
  3758. /* xgettext:c-format */
  3759. (_("%pB: pc-relative relocation against dynamic symbol %s"),
  3760. input_bfd, h->root.root.root.string);
  3761. ret_val = false;
  3762. }
  3763. /* The regular PC-relative stuff measures from the start of
  3764. the instruction rather than the end. */
  3765. value -= 4;
  3766. goto default_reloc;
  3767. case R_ALPHA_BRSGP:
  3768. {
  3769. int other;
  3770. const char *name;
  3771. /* The regular PC-relative stuff measures from the start of
  3772. the instruction rather than the end. */
  3773. value -= 4;
  3774. /* The source and destination gp must be the same. Note that
  3775. the source will always have an assigned gp, since we forced
  3776. one in check_relocs, but that the destination may not, as
  3777. it might not have had any relocations at all. Also take
  3778. care not to crash if H is an undefined symbol. */
  3779. if (h != NULL && sec != NULL
  3780. && alpha_elf_tdata (sec->owner)->gotobj
  3781. && gotobj != alpha_elf_tdata (sec->owner)->gotobj)
  3782. {
  3783. _bfd_error_handler
  3784. /* xgettext:c-format */
  3785. (_("%pB: change in gp: BRSGP %s"),
  3786. input_bfd, h->root.root.root.string);
  3787. ret_val = false;
  3788. }
  3789. /* The symbol should be marked either NOPV or STD_GPLOAD. */
  3790. if (h != NULL)
  3791. other = h->root.other;
  3792. else
  3793. other = sym->st_other;
  3794. switch (other & STO_ALPHA_STD_GPLOAD)
  3795. {
  3796. case STO_ALPHA_NOPV:
  3797. break;
  3798. case STO_ALPHA_STD_GPLOAD:
  3799. value += 8;
  3800. break;
  3801. default:
  3802. if (h != NULL)
  3803. name = h->root.root.root.string;
  3804. else
  3805. {
  3806. name = (bfd_elf_string_from_elf_section
  3807. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  3808. if (name == NULL)
  3809. name = _("<unknown>");
  3810. else if (name[0] == 0)
  3811. name = bfd_section_name (sec);
  3812. }
  3813. _bfd_error_handler
  3814. /* xgettext:c-format */
  3815. (_("%pB: !samegp reloc against symbol without .prologue: %s"),
  3816. input_bfd, name);
  3817. ret_val = false;
  3818. break;
  3819. }
  3820. goto default_reloc;
  3821. }
  3822. case R_ALPHA_REFLONG:
  3823. case R_ALPHA_REFQUAD:
  3824. case R_ALPHA_DTPREL64:
  3825. case R_ALPHA_TPREL64:
  3826. {
  3827. long dynindx, dyntype = r_type;
  3828. bfd_vma dynaddend;
  3829. /* Careful here to remember RELATIVE relocations for global
  3830. variables for symbolic shared objects. */
  3831. if (dynamic_symbol_p)
  3832. {
  3833. BFD_ASSERT(h->root.dynindx != -1);
  3834. dynindx = h->root.dynindx;
  3835. dynaddend = addend;
  3836. addend = 0, value = 0;
  3837. }
  3838. else if (r_type == R_ALPHA_DTPREL64)
  3839. {
  3840. BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
  3841. value -= dtp_base;
  3842. goto default_reloc;
  3843. }
  3844. else if (r_type == R_ALPHA_TPREL64)
  3845. {
  3846. BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
  3847. if (!bfd_link_dll (info))
  3848. {
  3849. value -= tp_base;
  3850. goto default_reloc;
  3851. }
  3852. dynindx = 0;
  3853. dynaddend = value - dtp_base;
  3854. }
  3855. else if (bfd_link_pic (info)
  3856. && r_symndx != STN_UNDEF
  3857. && (input_section->flags & SEC_ALLOC)
  3858. && !undef_weak_ref
  3859. && !(unresolved_reloc
  3860. && (_bfd_elf_section_offset (output_bfd, info,
  3861. input_section,
  3862. rel->r_offset)
  3863. == (bfd_vma) -1)))
  3864. {
  3865. if (r_type == R_ALPHA_REFLONG)
  3866. {
  3867. _bfd_error_handler
  3868. /* xgettext:c-format */
  3869. (_("%pB: unhandled dynamic relocation against %s"),
  3870. input_bfd,
  3871. h->root.root.root.string);
  3872. ret_val = false;
  3873. }
  3874. dynindx = 0;
  3875. dyntype = R_ALPHA_RELATIVE;
  3876. dynaddend = value;
  3877. }
  3878. else
  3879. goto default_reloc;
  3880. if (input_section->flags & SEC_ALLOC)
  3881. elf64_alpha_emit_dynrel (output_bfd, info, input_section,
  3882. srel, rel->r_offset, dynindx,
  3883. dyntype, dynaddend);
  3884. }
  3885. goto default_reloc;
  3886. case R_ALPHA_SREL16:
  3887. case R_ALPHA_SREL32:
  3888. case R_ALPHA_SREL64:
  3889. if (dynamic_symbol_p)
  3890. {
  3891. _bfd_error_handler
  3892. /* xgettext:c-format */
  3893. (_("%pB: pc-relative relocation against dynamic symbol %s"),
  3894. input_bfd, h->root.root.root.string);
  3895. ret_val = false;
  3896. }
  3897. else if (bfd_link_pic (info)
  3898. && undef_weak_ref)
  3899. {
  3900. _bfd_error_handler
  3901. /* xgettext:c-format */
  3902. (_("%pB: pc-relative relocation against undefined weak symbol %s"),
  3903. input_bfd, h->root.root.root.string);
  3904. ret_val = false;
  3905. }
  3906. /* ??? .eh_frame references to discarded sections will be smashed
  3907. to relocations against SHN_UNDEF. The .eh_frame format allows
  3908. NULL to be encoded as 0 in any format, so this works here. */
  3909. if (r_symndx == STN_UNDEF
  3910. || (unresolved_reloc
  3911. && _bfd_elf_section_offset (output_bfd, info,
  3912. input_section,
  3913. rel->r_offset) == (bfd_vma) -1))
  3914. howto = (elf64_alpha_howto_table
  3915. + (r_type - R_ALPHA_SREL32 + R_ALPHA_REFLONG));
  3916. goto default_reloc;
  3917. case R_ALPHA_TLSLDM:
  3918. /* Ignore the symbol for the relocation. The result is always
  3919. the current module. */
  3920. dynamic_symbol_p = 0;
  3921. /* FALLTHRU */
  3922. case R_ALPHA_TLSGD:
  3923. if (!gotent->reloc_done)
  3924. {
  3925. gotent->reloc_done = 1;
  3926. /* Note that the module index for the main program is 1. */
  3927. bfd_put_64 (output_bfd,
  3928. !bfd_link_pic (info) && !dynamic_symbol_p,
  3929. sgot->contents + gotent->got_offset);
  3930. /* If the symbol has been forced local, output a
  3931. DTPMOD64 reloc, otherwise it will be handled in
  3932. finish_dynamic_symbol. */
  3933. if (bfd_link_pic (info) && !dynamic_symbol_p)
  3934. elf64_alpha_emit_dynrel (output_bfd, info, sgot, srelgot,
  3935. gotent->got_offset, 0,
  3936. R_ALPHA_DTPMOD64, 0);
  3937. if (dynamic_symbol_p || r_type == R_ALPHA_TLSLDM)
  3938. value = 0;
  3939. else
  3940. {
  3941. BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
  3942. value -= dtp_base;
  3943. }
  3944. bfd_put_64 (output_bfd, value,
  3945. sgot->contents + gotent->got_offset + 8);
  3946. }
  3947. value = (sgot->output_section->vma
  3948. + sgot->output_offset
  3949. + gotent->got_offset);
  3950. value -= gp;
  3951. goto default_reloc;
  3952. case R_ALPHA_DTPRELHI:
  3953. case R_ALPHA_DTPRELLO:
  3954. case R_ALPHA_DTPREL16:
  3955. if (dynamic_symbol_p)
  3956. {
  3957. _bfd_error_handler
  3958. /* xgettext:c-format */
  3959. (_("%pB: dtp-relative relocation against dynamic symbol %s"),
  3960. input_bfd, h->root.root.root.string);
  3961. ret_val = false;
  3962. }
  3963. BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
  3964. value -= dtp_base;
  3965. if (r_type == R_ALPHA_DTPRELHI)
  3966. value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
  3967. goto default_reloc;
  3968. case R_ALPHA_TPRELHI:
  3969. case R_ALPHA_TPRELLO:
  3970. case R_ALPHA_TPREL16:
  3971. if (bfd_link_dll (info))
  3972. {
  3973. _bfd_error_handler
  3974. /* xgettext:c-format */
  3975. (_("%pB: TLS local exec code cannot be linked into shared objects"),
  3976. input_bfd);
  3977. ret_val = false;
  3978. }
  3979. else if (dynamic_symbol_p)
  3980. {
  3981. _bfd_error_handler
  3982. /* xgettext:c-format */
  3983. (_("%pB: tp-relative relocation against dynamic symbol %s"),
  3984. input_bfd, h->root.root.root.string);
  3985. ret_val = false;
  3986. }
  3987. BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
  3988. value -= tp_base;
  3989. if (r_type == R_ALPHA_TPRELHI)
  3990. value = ((bfd_signed_vma) value >> 16) + ((value >> 15) & 1);
  3991. goto default_reloc;
  3992. case R_ALPHA_GOTDTPREL:
  3993. case R_ALPHA_GOTTPREL:
  3994. BFD_ASSERT(sgot != NULL);
  3995. BFD_ASSERT(gp != 0);
  3996. BFD_ASSERT(gotent != NULL);
  3997. BFD_ASSERT(gotent->use_count >= 1);
  3998. if (!gotent->reloc_done)
  3999. {
  4000. gotent->reloc_done = 1;
  4001. if (dynamic_symbol_p)
  4002. value = 0;
  4003. else
  4004. {
  4005. BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
  4006. if (r_type == R_ALPHA_GOTDTPREL)
  4007. value -= dtp_base;
  4008. else if (bfd_link_executable (info))
  4009. value -= tp_base;
  4010. else
  4011. {
  4012. elf64_alpha_emit_dynrel (output_bfd, info, sgot, srelgot,
  4013. gotent->got_offset, 0,
  4014. R_ALPHA_TPREL64,
  4015. value - dtp_base);
  4016. value = 0;
  4017. }
  4018. }
  4019. bfd_put_64 (output_bfd, value,
  4020. sgot->contents + gotent->got_offset);
  4021. }
  4022. value = (sgot->output_section->vma
  4023. + sgot->output_offset
  4024. + gotent->got_offset);
  4025. value -= gp;
  4026. goto default_reloc;
  4027. default:
  4028. default_reloc:
  4029. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  4030. contents, rel->r_offset, value, 0);
  4031. break;
  4032. }
  4033. switch (r)
  4034. {
  4035. case bfd_reloc_ok:
  4036. break;
  4037. case bfd_reloc_overflow:
  4038. {
  4039. const char *name;
  4040. /* Don't warn if the overflow is due to pc relative reloc
  4041. against discarded section. Section optimization code should
  4042. handle it. */
  4043. if (r_symndx < symtab_hdr->sh_info
  4044. && sec != NULL && howto->pc_relative
  4045. && discarded_section (sec))
  4046. break;
  4047. if (h != NULL)
  4048. name = NULL;
  4049. else
  4050. {
  4051. name = (bfd_elf_string_from_elf_section
  4052. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  4053. if (name == NULL)
  4054. return false;
  4055. if (*name == '\0')
  4056. name = bfd_section_name (sec);
  4057. }
  4058. (*info->callbacks->reloc_overflow)
  4059. (info, (h ? &h->root.root : NULL), name, howto->name,
  4060. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  4061. }
  4062. break;
  4063. default:
  4064. case bfd_reloc_outofrange:
  4065. abort ();
  4066. }
  4067. }
  4068. return ret_val;
  4069. }
  4070. /* Finish up dynamic symbol handling. We set the contents of various
  4071. dynamic sections here. */
  4072. static bool
  4073. elf64_alpha_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
  4074. struct elf_link_hash_entry *h,
  4075. Elf_Internal_Sym *sym)
  4076. {
  4077. struct alpha_elf_link_hash_entry *ah = (struct alpha_elf_link_hash_entry *)h;
  4078. if (h->needs_plt)
  4079. {
  4080. /* Fill in the .plt entry for this symbol. */
  4081. asection *splt, *sgot, *srel;
  4082. Elf_Internal_Rela outrel;
  4083. bfd_byte *loc;
  4084. bfd_vma got_addr, plt_addr;
  4085. bfd_vma plt_index;
  4086. struct alpha_elf_got_entry *gotent;
  4087. BFD_ASSERT (h->dynindx != -1);
  4088. splt = elf_hash_table (info)->splt;
  4089. BFD_ASSERT (splt != NULL);
  4090. srel = elf_hash_table (info)->srelplt;
  4091. BFD_ASSERT (srel != NULL);
  4092. for (gotent = ah->got_entries; gotent ; gotent = gotent->next)
  4093. if (gotent->reloc_type == R_ALPHA_LITERAL
  4094. && gotent->use_count > 0)
  4095. {
  4096. unsigned int insn;
  4097. int disp;
  4098. sgot = alpha_elf_tdata (gotent->gotobj)->got;
  4099. BFD_ASSERT (sgot != NULL);
  4100. BFD_ASSERT (gotent->got_offset != -1);
  4101. BFD_ASSERT (gotent->plt_offset != -1);
  4102. got_addr = (sgot->output_section->vma
  4103. + sgot->output_offset
  4104. + gotent->got_offset);
  4105. plt_addr = (splt->output_section->vma
  4106. + splt->output_offset
  4107. + gotent->plt_offset);
  4108. plt_index = (gotent->plt_offset-PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
  4109. /* Fill in the entry in the procedure linkage table. */
  4110. if (elf64_alpha_use_secureplt)
  4111. {
  4112. disp = (PLT_HEADER_SIZE - 4) - (gotent->plt_offset + 4);
  4113. insn = INSN_AD (INSN_BR, 31, disp);
  4114. bfd_put_32 (output_bfd, insn,
  4115. splt->contents + gotent->plt_offset);
  4116. plt_index = ((gotent->plt_offset - NEW_PLT_HEADER_SIZE)
  4117. / NEW_PLT_ENTRY_SIZE);
  4118. }
  4119. else
  4120. {
  4121. disp = -(gotent->plt_offset + 4);
  4122. insn = INSN_AD (INSN_BR, 28, disp);
  4123. bfd_put_32 (output_bfd, insn,
  4124. splt->contents + gotent->plt_offset);
  4125. bfd_put_32 (output_bfd, INSN_UNOP,
  4126. splt->contents + gotent->plt_offset + 4);
  4127. bfd_put_32 (output_bfd, INSN_UNOP,
  4128. splt->contents + gotent->plt_offset + 8);
  4129. plt_index = ((gotent->plt_offset - OLD_PLT_HEADER_SIZE)
  4130. / OLD_PLT_ENTRY_SIZE);
  4131. }
  4132. /* Fill in the entry in the .rela.plt section. */
  4133. outrel.r_offset = got_addr;
  4134. outrel.r_info = ELF64_R_INFO(h->dynindx, R_ALPHA_JMP_SLOT);
  4135. outrel.r_addend = 0;
  4136. loc = srel->contents + plt_index * sizeof (Elf64_External_Rela);
  4137. bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
  4138. /* Fill in the entry in the .got. */
  4139. bfd_put_64 (output_bfd, plt_addr,
  4140. sgot->contents + gotent->got_offset);
  4141. }
  4142. }
  4143. else if (alpha_elf_dynamic_symbol_p (h, info))
  4144. {
  4145. /* Fill in the dynamic relocations for this symbol's .got entries. */
  4146. asection *srel;
  4147. struct alpha_elf_got_entry *gotent;
  4148. srel = elf_hash_table (info)->srelgot;
  4149. BFD_ASSERT (srel != NULL);
  4150. for (gotent = ((struct alpha_elf_link_hash_entry *) h)->got_entries;
  4151. gotent != NULL;
  4152. gotent = gotent->next)
  4153. {
  4154. asection *sgot;
  4155. long r_type;
  4156. if (gotent->use_count == 0)
  4157. continue;
  4158. sgot = alpha_elf_tdata (gotent->gotobj)->got;
  4159. r_type = gotent->reloc_type;
  4160. switch (r_type)
  4161. {
  4162. case R_ALPHA_LITERAL:
  4163. r_type = R_ALPHA_GLOB_DAT;
  4164. break;
  4165. case R_ALPHA_TLSGD:
  4166. r_type = R_ALPHA_DTPMOD64;
  4167. break;
  4168. case R_ALPHA_GOTDTPREL:
  4169. r_type = R_ALPHA_DTPREL64;
  4170. break;
  4171. case R_ALPHA_GOTTPREL:
  4172. r_type = R_ALPHA_TPREL64;
  4173. break;
  4174. case R_ALPHA_TLSLDM:
  4175. default:
  4176. abort ();
  4177. }
  4178. elf64_alpha_emit_dynrel (output_bfd, info, sgot, srel,
  4179. gotent->got_offset, h->dynindx,
  4180. r_type, gotent->addend);
  4181. if (gotent->reloc_type == R_ALPHA_TLSGD)
  4182. elf64_alpha_emit_dynrel (output_bfd, info, sgot, srel,
  4183. gotent->got_offset + 8, h->dynindx,
  4184. R_ALPHA_DTPREL64, gotent->addend);
  4185. }
  4186. }
  4187. /* Mark some specially defined symbols as absolute. */
  4188. if (h == elf_hash_table (info)->hdynamic
  4189. || h == elf_hash_table (info)->hgot
  4190. || h == elf_hash_table (info)->hplt)
  4191. sym->st_shndx = SHN_ABS;
  4192. return true;
  4193. }
  4194. /* Finish up the dynamic sections. */
  4195. static bool
  4196. elf64_alpha_finish_dynamic_sections (bfd *output_bfd,
  4197. struct bfd_link_info *info)
  4198. {
  4199. bfd *dynobj;
  4200. asection *sdyn;
  4201. dynobj = elf_hash_table (info)->dynobj;
  4202. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  4203. if (elf_hash_table (info)->dynamic_sections_created)
  4204. {
  4205. asection *splt, *sgotplt, *srelaplt;
  4206. Elf64_External_Dyn *dyncon, *dynconend;
  4207. bfd_vma plt_vma, gotplt_vma;
  4208. splt = elf_hash_table (info)->splt;
  4209. srelaplt = elf_hash_table (info)->srelplt;
  4210. BFD_ASSERT (splt != NULL && sdyn != NULL);
  4211. plt_vma = splt->output_section->vma + splt->output_offset;
  4212. gotplt_vma = 0;
  4213. if (elf64_alpha_use_secureplt)
  4214. {
  4215. sgotplt = elf_hash_table (info)->sgotplt;
  4216. BFD_ASSERT (sgotplt != NULL);
  4217. if (sgotplt->size > 0)
  4218. gotplt_vma = sgotplt->output_section->vma + sgotplt->output_offset;
  4219. }
  4220. dyncon = (Elf64_External_Dyn *) sdyn->contents;
  4221. dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
  4222. for (; dyncon < dynconend; dyncon++)
  4223. {
  4224. Elf_Internal_Dyn dyn;
  4225. bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
  4226. switch (dyn.d_tag)
  4227. {
  4228. case DT_PLTGOT:
  4229. dyn.d_un.d_ptr
  4230. = elf64_alpha_use_secureplt ? gotplt_vma : plt_vma;
  4231. break;
  4232. case DT_PLTRELSZ:
  4233. dyn.d_un.d_val = srelaplt ? srelaplt->size : 0;
  4234. break;
  4235. case DT_JMPREL:
  4236. dyn.d_un.d_ptr = srelaplt ? (srelaplt->output_section->vma
  4237. + srelaplt->output_offset) : 0;
  4238. break;
  4239. }
  4240. bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
  4241. }
  4242. /* Initialize the plt header. */
  4243. if (splt->size > 0)
  4244. {
  4245. unsigned int insn;
  4246. int ofs;
  4247. if (elf64_alpha_use_secureplt)
  4248. {
  4249. ofs = gotplt_vma - (plt_vma + PLT_HEADER_SIZE);
  4250. insn = INSN_ABC (INSN_SUBQ, 27, 28, 25);
  4251. bfd_put_32 (output_bfd, insn, splt->contents);
  4252. insn = INSN_ABO (INSN_LDAH, 28, 28, (ofs + 0x8000) >> 16);
  4253. bfd_put_32 (output_bfd, insn, splt->contents + 4);
  4254. insn = INSN_ABC (INSN_S4SUBQ, 25, 25, 25);
  4255. bfd_put_32 (output_bfd, insn, splt->contents + 8);
  4256. insn = INSN_ABO (INSN_LDA, 28, 28, ofs);
  4257. bfd_put_32 (output_bfd, insn, splt->contents + 12);
  4258. insn = INSN_ABO (INSN_LDQ, 27, 28, 0);
  4259. bfd_put_32 (output_bfd, insn, splt->contents + 16);
  4260. insn = INSN_ABC (INSN_ADDQ, 25, 25, 25);
  4261. bfd_put_32 (output_bfd, insn, splt->contents + 20);
  4262. insn = INSN_ABO (INSN_LDQ, 28, 28, 8);
  4263. bfd_put_32 (output_bfd, insn, splt->contents + 24);
  4264. insn = INSN_AB (INSN_JMP, 31, 27);
  4265. bfd_put_32 (output_bfd, insn, splt->contents + 28);
  4266. insn = INSN_AD (INSN_BR, 28, -PLT_HEADER_SIZE);
  4267. bfd_put_32 (output_bfd, insn, splt->contents + 32);
  4268. }
  4269. else
  4270. {
  4271. insn = INSN_AD (INSN_BR, 27, 0); /* br $27, .+4 */
  4272. bfd_put_32 (output_bfd, insn, splt->contents);
  4273. insn = INSN_ABO (INSN_LDQ, 27, 27, 12);
  4274. bfd_put_32 (output_bfd, insn, splt->contents + 4);
  4275. insn = INSN_UNOP;
  4276. bfd_put_32 (output_bfd, insn, splt->contents + 8);
  4277. insn = INSN_AB (INSN_JMP, 27, 27);
  4278. bfd_put_32 (output_bfd, insn, splt->contents + 12);
  4279. /* The next two words will be filled in by ld.so. */
  4280. bfd_put_64 (output_bfd, 0, splt->contents + 16);
  4281. bfd_put_64 (output_bfd, 0, splt->contents + 24);
  4282. }
  4283. elf_section_data (splt->output_section)->this_hdr.sh_entsize = 0;
  4284. }
  4285. }
  4286. return true;
  4287. }
  4288. /* We need to use a special link routine to handle the .mdebug section.
  4289. We need to merge all instances of these sections together, not write
  4290. them all out sequentially. */
  4291. static bool
  4292. elf64_alpha_final_link (bfd *abfd, struct bfd_link_info *info)
  4293. {
  4294. asection *o;
  4295. struct bfd_link_order *p;
  4296. asection *mdebug_sec;
  4297. struct ecoff_debug_info debug;
  4298. const struct ecoff_debug_swap *swap
  4299. = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
  4300. HDRR *symhdr = &debug.symbolic_header;
  4301. void * mdebug_handle = NULL;
  4302. struct alpha_elf_link_hash_table * htab;
  4303. htab = alpha_elf_hash_table (info);
  4304. if (htab == NULL)
  4305. return false;
  4306. /* Go through the sections and collect the mdebug information. */
  4307. mdebug_sec = NULL;
  4308. for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  4309. {
  4310. if (strcmp (o->name, ".mdebug") == 0)
  4311. {
  4312. struct extsym_info einfo;
  4313. /* We have found the .mdebug section in the output file.
  4314. Look through all the link_orders comprising it and merge
  4315. the information together. */
  4316. symhdr->magic = swap->sym_magic;
  4317. /* FIXME: What should the version stamp be? */
  4318. symhdr->vstamp = 0;
  4319. symhdr->ilineMax = 0;
  4320. symhdr->cbLine = 0;
  4321. symhdr->idnMax = 0;
  4322. symhdr->ipdMax = 0;
  4323. symhdr->isymMax = 0;
  4324. symhdr->ioptMax = 0;
  4325. symhdr->iauxMax = 0;
  4326. symhdr->issMax = 0;
  4327. symhdr->issExtMax = 0;
  4328. symhdr->ifdMax = 0;
  4329. symhdr->crfd = 0;
  4330. symhdr->iextMax = 0;
  4331. /* We accumulate the debugging information itself in the
  4332. debug_info structure. */
  4333. debug.line = NULL;
  4334. debug.external_dnr = NULL;
  4335. debug.external_pdr = NULL;
  4336. debug.external_sym = NULL;
  4337. debug.external_opt = NULL;
  4338. debug.external_aux = NULL;
  4339. debug.ss = NULL;
  4340. debug.ssext = debug.ssext_end = NULL;
  4341. debug.external_fdr = NULL;
  4342. debug.external_rfd = NULL;
  4343. debug.external_ext = debug.external_ext_end = NULL;
  4344. mdebug_handle = bfd_ecoff_debug_init (abfd, &debug, swap, info);
  4345. if (mdebug_handle == NULL)
  4346. return false;
  4347. if (1)
  4348. {
  4349. asection *s;
  4350. EXTR esym;
  4351. bfd_vma last = 0;
  4352. unsigned int i;
  4353. static const char * const name[] =
  4354. {
  4355. ".text", ".init", ".fini", ".data",
  4356. ".rodata", ".sdata", ".sbss", ".bss"
  4357. };
  4358. static const int sc[] = { scText, scInit, scFini, scData,
  4359. scRData, scSData, scSBss, scBss };
  4360. esym.jmptbl = 0;
  4361. esym.cobol_main = 0;
  4362. esym.weakext = 0;
  4363. esym.reserved = 0;
  4364. esym.ifd = ifdNil;
  4365. esym.asym.iss = issNil;
  4366. esym.asym.st = stLocal;
  4367. esym.asym.reserved = 0;
  4368. esym.asym.index = indexNil;
  4369. for (i = 0; i < 8; i++)
  4370. {
  4371. esym.asym.sc = sc[i];
  4372. s = bfd_get_section_by_name (abfd, name[i]);
  4373. if (s != NULL)
  4374. {
  4375. esym.asym.value = s->vma;
  4376. last = s->vma + s->size;
  4377. }
  4378. else
  4379. esym.asym.value = last;
  4380. if (! bfd_ecoff_debug_one_external (abfd, &debug, swap,
  4381. name[i], &esym))
  4382. return false;
  4383. }
  4384. }
  4385. for (p = o->map_head.link_order;
  4386. p != (struct bfd_link_order *) NULL;
  4387. p = p->next)
  4388. {
  4389. asection *input_section;
  4390. bfd *input_bfd;
  4391. const struct ecoff_debug_swap *input_swap;
  4392. struct ecoff_debug_info input_debug;
  4393. char *eraw_src;
  4394. char *eraw_end;
  4395. if (p->type != bfd_indirect_link_order)
  4396. {
  4397. if (p->type == bfd_data_link_order)
  4398. continue;
  4399. abort ();
  4400. }
  4401. input_section = p->u.indirect.section;
  4402. input_bfd = input_section->owner;
  4403. if (! is_alpha_elf (input_bfd))
  4404. /* I don't know what a non ALPHA ELF bfd would be
  4405. doing with a .mdebug section, but I don't really
  4406. want to deal with it. */
  4407. continue;
  4408. input_swap = (get_elf_backend_data (input_bfd)
  4409. ->elf_backend_ecoff_debug_swap);
  4410. BFD_ASSERT (p->size == input_section->size);
  4411. /* The ECOFF linking code expects that we have already
  4412. read in the debugging information and set up an
  4413. ecoff_debug_info structure, so we do that now. */
  4414. if (!elf64_alpha_read_ecoff_info (input_bfd, input_section,
  4415. &input_debug))
  4416. return false;
  4417. if (! (bfd_ecoff_debug_accumulate
  4418. (mdebug_handle, abfd, &debug, swap, input_bfd,
  4419. &input_debug, input_swap, info)))
  4420. return false;
  4421. /* Loop through the external symbols. For each one with
  4422. interesting information, try to find the symbol in
  4423. the linker global hash table and save the information
  4424. for the output external symbols. */
  4425. eraw_src = (char *) input_debug.external_ext;
  4426. eraw_end = (eraw_src
  4427. + (input_debug.symbolic_header.iextMax
  4428. * input_swap->external_ext_size));
  4429. for (;
  4430. eraw_src < eraw_end;
  4431. eraw_src += input_swap->external_ext_size)
  4432. {
  4433. EXTR ext;
  4434. const char *name;
  4435. struct alpha_elf_link_hash_entry *h;
  4436. (*input_swap->swap_ext_in) (input_bfd, eraw_src, &ext);
  4437. if (ext.asym.sc == scNil
  4438. || ext.asym.sc == scUndefined
  4439. || ext.asym.sc == scSUndefined)
  4440. continue;
  4441. name = input_debug.ssext + ext.asym.iss;
  4442. h = alpha_elf_link_hash_lookup (htab, name, false, false, true);
  4443. if (h == NULL || h->esym.ifd != -2)
  4444. continue;
  4445. if (ext.ifd != -1)
  4446. {
  4447. BFD_ASSERT (ext.ifd
  4448. < input_debug.symbolic_header.ifdMax);
  4449. ext.ifd = input_debug.ifdmap[ext.ifd];
  4450. }
  4451. h->esym = ext;
  4452. }
  4453. /* Free up the information we just read. */
  4454. free (input_debug.line);
  4455. free (input_debug.external_dnr);
  4456. free (input_debug.external_pdr);
  4457. free (input_debug.external_sym);
  4458. free (input_debug.external_opt);
  4459. free (input_debug.external_aux);
  4460. free (input_debug.ss);
  4461. free (input_debug.ssext);
  4462. free (input_debug.external_fdr);
  4463. free (input_debug.external_rfd);
  4464. free (input_debug.external_ext);
  4465. /* Hack: reset the SEC_HAS_CONTENTS flag so that
  4466. elf_link_input_bfd ignores this section. */
  4467. input_section->flags &=~ SEC_HAS_CONTENTS;
  4468. }
  4469. /* Build the external symbol information. */
  4470. einfo.abfd = abfd;
  4471. einfo.info = info;
  4472. einfo.debug = &debug;
  4473. einfo.swap = swap;
  4474. einfo.failed = false;
  4475. elf_link_hash_traverse (elf_hash_table (info),
  4476. elf64_alpha_output_extsym,
  4477. &einfo);
  4478. if (einfo.failed)
  4479. return false;
  4480. /* Set the size of the .mdebug section. */
  4481. o->size = bfd_ecoff_debug_size (abfd, &debug, swap);
  4482. /* Skip this section later on (I don't think this currently
  4483. matters, but someday it might). */
  4484. o->map_head.link_order = (struct bfd_link_order *) NULL;
  4485. mdebug_sec = o;
  4486. }
  4487. }
  4488. /* Invoke the regular ELF backend linker to do all the work. */
  4489. if (! bfd_elf_final_link (abfd, info))
  4490. return false;
  4491. /* Now write out the computed sections. */
  4492. /* The .got subsections... */
  4493. {
  4494. bfd *i, *dynobj = elf_hash_table(info)->dynobj;
  4495. for (i = htab->got_list;
  4496. i != NULL;
  4497. i = alpha_elf_tdata(i)->got_link_next)
  4498. {
  4499. asection *sgot;
  4500. /* elf_bfd_final_link already did everything in dynobj. */
  4501. if (i == dynobj)
  4502. continue;
  4503. sgot = alpha_elf_tdata(i)->got;
  4504. if (! bfd_set_section_contents (abfd, sgot->output_section,
  4505. sgot->contents,
  4506. (file_ptr) sgot->output_offset,
  4507. sgot->size))
  4508. return false;
  4509. }
  4510. }
  4511. if (mdebug_sec != (asection *) NULL)
  4512. {
  4513. BFD_ASSERT (abfd->output_has_begun);
  4514. if (! bfd_ecoff_write_accumulated_debug (mdebug_handle, abfd, &debug,
  4515. swap, info,
  4516. mdebug_sec->filepos))
  4517. return false;
  4518. bfd_ecoff_debug_free (mdebug_handle, abfd, &debug, swap, info);
  4519. }
  4520. return true;
  4521. }
  4522. static enum elf_reloc_type_class
  4523. elf64_alpha_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  4524. const asection *rel_sec ATTRIBUTE_UNUSED,
  4525. const Elf_Internal_Rela *rela)
  4526. {
  4527. switch ((int) ELF64_R_TYPE (rela->r_info))
  4528. {
  4529. case R_ALPHA_RELATIVE:
  4530. return reloc_class_relative;
  4531. case R_ALPHA_JMP_SLOT:
  4532. return reloc_class_plt;
  4533. case R_ALPHA_COPY:
  4534. return reloc_class_copy;
  4535. default:
  4536. return reloc_class_normal;
  4537. }
  4538. }
  4539. static const struct bfd_elf_special_section elf64_alpha_special_sections[] =
  4540. {
  4541. { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_ALPHA_GPREL },
  4542. { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_ALPHA_GPREL },
  4543. { NULL, 0, 0, 0, 0 }
  4544. };
  4545. /* ECOFF swapping routines. These are used when dealing with the
  4546. .mdebug section, which is in the ECOFF debugging format. Copied
  4547. from elf32-mips.c. */
  4548. static const struct ecoff_debug_swap
  4549. elf64_alpha_ecoff_debug_swap =
  4550. {
  4551. /* Symbol table magic number. */
  4552. magicSym2,
  4553. /* Alignment of debugging information. E.g., 4. */
  4554. 8,
  4555. /* Sizes of external symbolic information. */
  4556. sizeof (struct hdr_ext),
  4557. sizeof (struct dnr_ext),
  4558. sizeof (struct pdr_ext),
  4559. sizeof (struct sym_ext),
  4560. sizeof (struct opt_ext),
  4561. sizeof (struct fdr_ext),
  4562. sizeof (struct rfd_ext),
  4563. sizeof (struct ext_ext),
  4564. /* Functions to swap in external symbolic data. */
  4565. ecoff_swap_hdr_in,
  4566. ecoff_swap_dnr_in,
  4567. ecoff_swap_pdr_in,
  4568. ecoff_swap_sym_in,
  4569. ecoff_swap_opt_in,
  4570. ecoff_swap_fdr_in,
  4571. ecoff_swap_rfd_in,
  4572. ecoff_swap_ext_in,
  4573. _bfd_ecoff_swap_tir_in,
  4574. _bfd_ecoff_swap_rndx_in,
  4575. /* Functions to swap out external symbolic data. */
  4576. ecoff_swap_hdr_out,
  4577. ecoff_swap_dnr_out,
  4578. ecoff_swap_pdr_out,
  4579. ecoff_swap_sym_out,
  4580. ecoff_swap_opt_out,
  4581. ecoff_swap_fdr_out,
  4582. ecoff_swap_rfd_out,
  4583. ecoff_swap_ext_out,
  4584. _bfd_ecoff_swap_tir_out,
  4585. _bfd_ecoff_swap_rndx_out,
  4586. /* Function to read in symbolic data. */
  4587. elf64_alpha_read_ecoff_info
  4588. };
  4589. /* Use a non-standard hash bucket size of 8. */
  4590. static const struct elf_size_info alpha_elf_size_info =
  4591. {
  4592. sizeof (Elf64_External_Ehdr),
  4593. sizeof (Elf64_External_Phdr),
  4594. sizeof (Elf64_External_Shdr),
  4595. sizeof (Elf64_External_Rel),
  4596. sizeof (Elf64_External_Rela),
  4597. sizeof (Elf64_External_Sym),
  4598. sizeof (Elf64_External_Dyn),
  4599. sizeof (Elf_External_Note),
  4600. 8,
  4601. 1,
  4602. 64, 3,
  4603. ELFCLASS64, EV_CURRENT,
  4604. bfd_elf64_write_out_phdrs,
  4605. bfd_elf64_write_shdrs_and_ehdr,
  4606. bfd_elf64_checksum_contents,
  4607. bfd_elf64_write_relocs,
  4608. bfd_elf64_swap_symbol_in,
  4609. bfd_elf64_swap_symbol_out,
  4610. bfd_elf64_slurp_reloc_table,
  4611. bfd_elf64_slurp_symbol_table,
  4612. bfd_elf64_swap_dyn_in,
  4613. bfd_elf64_swap_dyn_out,
  4614. bfd_elf64_swap_reloc_in,
  4615. bfd_elf64_swap_reloc_out,
  4616. bfd_elf64_swap_reloca_in,
  4617. bfd_elf64_swap_reloca_out
  4618. };
  4619. #define TARGET_LITTLE_SYM alpha_elf64_vec
  4620. #define TARGET_LITTLE_NAME "elf64-alpha"
  4621. #define ELF_ARCH bfd_arch_alpha
  4622. #define ELF_TARGET_ID ALPHA_ELF_DATA
  4623. #define ELF_MACHINE_CODE EM_ALPHA
  4624. #define ELF_MAXPAGESIZE 0x10000
  4625. #define ELF_COMMONPAGESIZE 0x2000
  4626. #define bfd_elf64_bfd_link_hash_table_create \
  4627. elf64_alpha_bfd_link_hash_table_create
  4628. #define bfd_elf64_bfd_reloc_type_lookup \
  4629. elf64_alpha_bfd_reloc_type_lookup
  4630. #define bfd_elf64_bfd_reloc_name_lookup \
  4631. elf64_alpha_bfd_reloc_name_lookup
  4632. #define elf_info_to_howto \
  4633. elf64_alpha_info_to_howto
  4634. #define bfd_elf64_mkobject \
  4635. elf64_alpha_mkobject
  4636. #define elf_backend_object_p \
  4637. elf64_alpha_object_p
  4638. #define elf_backend_section_from_shdr \
  4639. elf64_alpha_section_from_shdr
  4640. #define elf_backend_section_flags \
  4641. elf64_alpha_section_flags
  4642. #define elf_backend_fake_sections \
  4643. elf64_alpha_fake_sections
  4644. #define bfd_elf64_bfd_is_local_label_name \
  4645. elf64_alpha_is_local_label_name
  4646. #define bfd_elf64_find_nearest_line \
  4647. elf64_alpha_find_nearest_line
  4648. #define bfd_elf64_bfd_relax_section \
  4649. elf64_alpha_relax_section
  4650. #define elf_backend_add_symbol_hook \
  4651. elf64_alpha_add_symbol_hook
  4652. #define elf_backend_relocs_compatible \
  4653. _bfd_elf_relocs_compatible
  4654. #define elf_backend_sort_relocs_p \
  4655. elf64_alpha_sort_relocs_p
  4656. #define elf_backend_check_relocs \
  4657. elf64_alpha_check_relocs
  4658. #define elf_backend_create_dynamic_sections \
  4659. elf64_alpha_create_dynamic_sections
  4660. #define elf_backend_adjust_dynamic_symbol \
  4661. elf64_alpha_adjust_dynamic_symbol
  4662. #define elf_backend_merge_symbol_attribute \
  4663. elf64_alpha_merge_symbol_attribute
  4664. #define elf_backend_copy_indirect_symbol \
  4665. elf64_alpha_copy_indirect_symbol
  4666. #define elf_backend_always_size_sections \
  4667. elf64_alpha_always_size_sections
  4668. #define elf_backend_size_dynamic_sections \
  4669. elf64_alpha_size_dynamic_sections
  4670. #define elf_backend_omit_section_dynsym \
  4671. _bfd_elf_omit_section_dynsym_all
  4672. #define elf_backend_relocate_section \
  4673. elf64_alpha_relocate_section
  4674. #define elf_backend_finish_dynamic_symbol \
  4675. elf64_alpha_finish_dynamic_symbol
  4676. #define elf_backend_finish_dynamic_sections \
  4677. elf64_alpha_finish_dynamic_sections
  4678. #define bfd_elf64_bfd_final_link \
  4679. elf64_alpha_final_link
  4680. #define elf_backend_reloc_type_class \
  4681. elf64_alpha_reloc_type_class
  4682. #define elf_backend_can_gc_sections 1
  4683. #define elf_backend_gc_mark_hook elf64_alpha_gc_mark_hook
  4684. #define elf_backend_ecoff_debug_swap \
  4685. &elf64_alpha_ecoff_debug_swap
  4686. #define elf_backend_size_info \
  4687. alpha_elf_size_info
  4688. #define elf_backend_special_sections \
  4689. elf64_alpha_special_sections
  4690. #define elf_backend_strip_zero_sized_dynamic_sections \
  4691. _bfd_elf_strip_zero_sized_dynamic_sections
  4692. /* A few constants that determine how the .plt section is set up. */
  4693. #define elf_backend_want_got_plt 0
  4694. #define elf_backend_plt_readonly 0
  4695. #define elf_backend_want_plt_sym 1
  4696. #define elf_backend_got_header_size 0
  4697. #define elf_backend_dtrel_excludes_plt 1
  4698. #include "elf64-target.h"
  4699. /* FreeBSD support. */
  4700. #undef TARGET_LITTLE_SYM
  4701. #define TARGET_LITTLE_SYM alpha_elf64_fbsd_vec
  4702. #undef TARGET_LITTLE_NAME
  4703. #define TARGET_LITTLE_NAME "elf64-alpha-freebsd"
  4704. #undef ELF_OSABI
  4705. #define ELF_OSABI ELFOSABI_FREEBSD
  4706. /* The kernel recognizes executables as valid only if they carry a
  4707. "FreeBSD" label in the ELF header. So we put this label on all
  4708. executables and (for simplicity) also all other object files. */
  4709. static bool
  4710. elf64_alpha_fbsd_init_file_header (bfd *abfd, struct bfd_link_info *info)
  4711. {
  4712. Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */
  4713. if (!_bfd_elf_init_file_header (abfd, info))
  4714. return false;
  4715. i_ehdrp = elf_elfheader (abfd);
  4716. /* Put an ABI label supported by FreeBSD >= 4.1. */
  4717. i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
  4718. #ifdef OLD_FREEBSD_ABI_LABEL
  4719. /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */
  4720. memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8);
  4721. #endif
  4722. return true;
  4723. }
  4724. #undef elf_backend_init_file_header
  4725. #define elf_backend_init_file_header \
  4726. elf64_alpha_fbsd_init_file_header
  4727. #undef elf64_bed
  4728. #define elf64_bed elf64_alpha_fbsd_bed
  4729. #include "elf64-target.h"