elf32-bfin.c 173 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758
  1. /* ADI Blackfin BFD support for 32-bit ELF.
  2. Copyright (C) 2005-2015 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libbfd.h"
  19. #include "elf-bfd.h"
  20. #include "elf/bfin.h"
  21. #include "dwarf2.h"
  22. #include "hashtab.h"
  23. /* FUNCTION : bfin_pltpc_reloc
  24. ABSTRACT : TODO : figure out how to handle pltpc relocs. */
  25. static bfd_reloc_status_type
  26. bfin_pltpc_reloc (
  27. bfd *abfd ATTRIBUTE_UNUSED,
  28. arelent *reloc_entry ATTRIBUTE_UNUSED,
  29. asymbol *symbol ATTRIBUTE_UNUSED,
  30. void * data ATTRIBUTE_UNUSED,
  31. asection *input_section ATTRIBUTE_UNUSED,
  32. bfd *output_bfd ATTRIBUTE_UNUSED,
  33. char **error_message ATTRIBUTE_UNUSED)
  34. {
  35. bfd_reloc_status_type flag = bfd_reloc_ok;
  36. return flag;
  37. }
  38. static bfd_reloc_status_type
  39. bfin_pcrel24_reloc (bfd *abfd,
  40. arelent *reloc_entry,
  41. asymbol *symbol,
  42. void * data,
  43. asection *input_section,
  44. bfd *output_bfd,
  45. char **error_message ATTRIBUTE_UNUSED)
  46. {
  47. bfd_vma relocation;
  48. bfd_size_type addr = reloc_entry->address;
  49. bfd_vma output_base = 0;
  50. reloc_howto_type *howto = reloc_entry->howto;
  51. asection *output_section;
  52. bfd_boolean relocatable = (output_bfd != NULL);
  53. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  54. return bfd_reloc_outofrange;
  55. if (bfd_is_und_section (symbol->section)
  56. && (symbol->flags & BSF_WEAK) == 0
  57. && !relocatable)
  58. return bfd_reloc_undefined;
  59. if (bfd_is_com_section (symbol->section))
  60. relocation = 0;
  61. else
  62. relocation = symbol->value;
  63. output_section = symbol->section->output_section;
  64. if (relocatable)
  65. output_base = 0;
  66. else
  67. output_base = output_section->vma;
  68. if (!relocatable || !strcmp (symbol->name, symbol->section->name))
  69. relocation += output_base + symbol->section->output_offset;
  70. if (!relocatable && !strcmp (symbol->name, symbol->section->name))
  71. relocation += reloc_entry->addend;
  72. relocation -= input_section->output_section->vma + input_section->output_offset;
  73. relocation -= reloc_entry->address;
  74. if (howto->complain_on_overflow != complain_overflow_dont)
  75. {
  76. bfd_reloc_status_type status;
  77. status = bfd_check_overflow (howto->complain_on_overflow,
  78. howto->bitsize,
  79. howto->rightshift,
  80. bfd_arch_bits_per_address(abfd),
  81. relocation);
  82. if (status != bfd_reloc_ok)
  83. return status;
  84. }
  85. /* if rightshift is 1 and the number odd, return error. */
  86. if (howto->rightshift && (relocation & 0x01))
  87. {
  88. (*_bfd_error_handler) (_("relocation should be even number"));
  89. return bfd_reloc_overflow;
  90. }
  91. relocation >>= (bfd_vma) howto->rightshift;
  92. /* Shift everything up to where it's going to be used. */
  93. relocation <<= (bfd_vma) howto->bitpos;
  94. if (relocatable)
  95. {
  96. reloc_entry->address += input_section->output_offset;
  97. reloc_entry->addend += symbol->section->output_offset;
  98. }
  99. {
  100. short x;
  101. /* We are getting reloc_entry->address 2 byte off from
  102. the start of instruction. Assuming absolute postion
  103. of the reloc data. But, following code had been written assuming
  104. reloc address is starting at begining of instruction.
  105. To compensate that I have increased the value of
  106. relocation by 1 (effectively 2) and used the addr -2 instead of addr. */
  107. relocation += 1;
  108. x = bfd_get_16 (abfd, (bfd_byte *) data + addr - 2);
  109. x = (x & 0xff00) | ((relocation >> 16) & 0xff);
  110. bfd_put_16 (abfd, x, (unsigned char *) data + addr - 2);
  111. x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
  112. x = relocation & 0xFFFF;
  113. bfd_put_16 (abfd, x, (unsigned char *) data + addr );
  114. }
  115. return bfd_reloc_ok;
  116. }
  117. static bfd_reloc_status_type
  118. bfin_imm16_reloc (bfd *abfd,
  119. arelent *reloc_entry,
  120. asymbol *symbol,
  121. void * data,
  122. asection *input_section,
  123. bfd *output_bfd,
  124. char **error_message ATTRIBUTE_UNUSED)
  125. {
  126. bfd_vma relocation, x;
  127. bfd_size_type reloc_addr = reloc_entry->address;
  128. bfd_vma output_base = 0;
  129. reloc_howto_type *howto = reloc_entry->howto;
  130. asection *output_section;
  131. bfd_boolean relocatable = (output_bfd != NULL);
  132. /* Is the address of the relocation really within the section? */
  133. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  134. return bfd_reloc_outofrange;
  135. if (bfd_is_und_section (symbol->section)
  136. && (symbol->flags & BSF_WEAK) == 0
  137. && !relocatable)
  138. return bfd_reloc_undefined;
  139. output_section = symbol->section->output_section;
  140. relocation = symbol->value;
  141. /* Convert input-section-relative symbol value to absolute. */
  142. if (relocatable)
  143. output_base = 0;
  144. else
  145. output_base = output_section->vma;
  146. if (!relocatable || !strcmp (symbol->name, symbol->section->name))
  147. relocation += output_base + symbol->section->output_offset;
  148. /* Add in supplied addend. */
  149. relocation += reloc_entry->addend;
  150. if (relocatable)
  151. {
  152. reloc_entry->address += input_section->output_offset;
  153. reloc_entry->addend += symbol->section->output_offset;
  154. }
  155. else
  156. {
  157. reloc_entry->addend = 0;
  158. }
  159. if (howto->complain_on_overflow != complain_overflow_dont)
  160. {
  161. bfd_reloc_status_type flag;
  162. flag = bfd_check_overflow (howto->complain_on_overflow,
  163. howto->bitsize,
  164. howto->rightshift,
  165. bfd_arch_bits_per_address(abfd),
  166. relocation);
  167. if (flag != bfd_reloc_ok)
  168. return flag;
  169. }
  170. /* Here the variable relocation holds the final address of the
  171. symbol we are relocating against, plus any addend. */
  172. relocation >>= (bfd_vma) howto->rightshift;
  173. x = relocation;
  174. bfd_put_16 (abfd, x, (unsigned char *) data + reloc_addr);
  175. return bfd_reloc_ok;
  176. }
  177. static bfd_reloc_status_type
  178. bfin_byte4_reloc (bfd *abfd,
  179. arelent *reloc_entry,
  180. asymbol *symbol,
  181. void * data,
  182. asection *input_section,
  183. bfd *output_bfd,
  184. char **error_message ATTRIBUTE_UNUSED)
  185. {
  186. bfd_vma relocation, x;
  187. bfd_size_type addr = reloc_entry->address;
  188. bfd_vma output_base = 0;
  189. asection *output_section;
  190. bfd_boolean relocatable = (output_bfd != NULL);
  191. /* Is the address of the relocation really within the section? */
  192. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  193. return bfd_reloc_outofrange;
  194. if (bfd_is_und_section (symbol->section)
  195. && (symbol->flags & BSF_WEAK) == 0
  196. && !relocatable)
  197. return bfd_reloc_undefined;
  198. output_section = symbol->section->output_section;
  199. relocation = symbol->value;
  200. /* Convert input-section-relative symbol value to absolute. */
  201. if (relocatable)
  202. output_base = 0;
  203. else
  204. output_base = output_section->vma;
  205. if ((symbol->name
  206. && symbol->section->name
  207. && !strcmp (symbol->name, symbol->section->name))
  208. || !relocatable)
  209. {
  210. relocation += output_base + symbol->section->output_offset;
  211. }
  212. relocation += reloc_entry->addend;
  213. if (relocatable)
  214. {
  215. /* This output will be relocatable ... like ld -r. */
  216. reloc_entry->address += input_section->output_offset;
  217. reloc_entry->addend += symbol->section->output_offset;
  218. }
  219. else
  220. {
  221. reloc_entry->addend = 0;
  222. }
  223. /* Here the variable relocation holds the final address of the
  224. symbol we are relocating against, plus any addend. */
  225. x = relocation & 0xFFFF0000;
  226. x >>=16;
  227. bfd_put_16 (abfd, x, (unsigned char *) data + addr + 2);
  228. x = relocation & 0x0000FFFF;
  229. bfd_put_16 (abfd, x, (unsigned char *) data + addr);
  230. return bfd_reloc_ok;
  231. }
  232. /* bfin_bfd_reloc handles the blackfin arithmetic relocations.
  233. Use this instead of bfd_perform_relocation. */
  234. static bfd_reloc_status_type
  235. bfin_bfd_reloc (bfd *abfd,
  236. arelent *reloc_entry,
  237. asymbol *symbol,
  238. void * data,
  239. asection *input_section,
  240. bfd *output_bfd,
  241. char **error_message ATTRIBUTE_UNUSED)
  242. {
  243. bfd_vma relocation;
  244. bfd_size_type addr = reloc_entry->address;
  245. bfd_vma output_base = 0;
  246. reloc_howto_type *howto = reloc_entry->howto;
  247. asection *output_section;
  248. bfd_boolean relocatable = (output_bfd != NULL);
  249. /* Is the address of the relocation really within the section? */
  250. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  251. return bfd_reloc_outofrange;
  252. if (bfd_is_und_section (symbol->section)
  253. && (symbol->flags & BSF_WEAK) == 0
  254. && !relocatable)
  255. return bfd_reloc_undefined;
  256. /* Get symbol value. (Common symbols are special.) */
  257. if (bfd_is_com_section (symbol->section))
  258. relocation = 0;
  259. else
  260. relocation = symbol->value;
  261. output_section = symbol->section->output_section;
  262. /* Convert input-section-relative symbol value to absolute. */
  263. if (relocatable)
  264. output_base = 0;
  265. else
  266. output_base = output_section->vma;
  267. if (!relocatable || !strcmp (symbol->name, symbol->section->name))
  268. relocation += output_base + symbol->section->output_offset;
  269. if (!relocatable && !strcmp (symbol->name, symbol->section->name))
  270. {
  271. /* Add in supplied addend. */
  272. relocation += reloc_entry->addend;
  273. }
  274. /* Here the variable relocation holds the final address of the
  275. symbol we are relocating against, plus any addend. */
  276. if (howto->pc_relative == TRUE)
  277. {
  278. relocation -= input_section->output_section->vma + input_section->output_offset;
  279. if (howto->pcrel_offset == TRUE)
  280. relocation -= reloc_entry->address;
  281. }
  282. if (relocatable)
  283. {
  284. reloc_entry->address += input_section->output_offset;
  285. reloc_entry->addend += symbol->section->output_offset;
  286. }
  287. if (howto->complain_on_overflow != complain_overflow_dont)
  288. {
  289. bfd_reloc_status_type status;
  290. status = bfd_check_overflow (howto->complain_on_overflow,
  291. howto->bitsize,
  292. howto->rightshift,
  293. bfd_arch_bits_per_address(abfd),
  294. relocation);
  295. if (status != bfd_reloc_ok)
  296. return status;
  297. }
  298. /* If rightshift is 1 and the number odd, return error. */
  299. if (howto->rightshift && (relocation & 0x01))
  300. {
  301. (*_bfd_error_handler) (_("relocation should be even number"));
  302. return bfd_reloc_overflow;
  303. }
  304. relocation >>= (bfd_vma) howto->rightshift;
  305. /* Shift everything up to where it's going to be used. */
  306. relocation <<= (bfd_vma) howto->bitpos;
  307. #define DOIT(x) \
  308. x = ( (x & ~howto->dst_mask) | (relocation & howto->dst_mask))
  309. /* handle 8 and 16 bit relocations here. */
  310. switch (howto->size)
  311. {
  312. case 0:
  313. {
  314. char x = bfd_get_8 (abfd, (char *) data + addr);
  315. DOIT (x);
  316. bfd_put_8 (abfd, x, (unsigned char *) data + addr);
  317. }
  318. break;
  319. case 1:
  320. {
  321. unsigned short x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
  322. DOIT (x);
  323. bfd_put_16 (abfd, (bfd_vma) x, (unsigned char *) data + addr);
  324. }
  325. break;
  326. default:
  327. return bfd_reloc_other;
  328. }
  329. return bfd_reloc_ok;
  330. }
  331. /* HOWTO Table for blackfin.
  332. Blackfin relocations are fairly complicated.
  333. Some of the salient features are
  334. a. Even numbered offsets. A number of (not all) relocations are
  335. even numbered. This means that the rightmost bit is not stored.
  336. Needs to right shift by 1 and check to see if value is not odd
  337. b. A relocation can be an expression. An expression takes on
  338. a variety of relocations arranged in a stack.
  339. As a result, we cannot use the standard generic function as special
  340. function. We will have our own, which is very similar to the standard
  341. generic function except that it understands how to get the value from
  342. the relocation stack. . */
  343. #define BFIN_RELOC_MIN 0
  344. #define BFIN_RELOC_MAX 0x21
  345. #define BFIN_GNUEXT_RELOC_MIN 0x40
  346. #define BFIN_GNUEXT_RELOC_MAX 0x43
  347. #define BFIN_ARELOC_MIN 0xE0
  348. #define BFIN_ARELOC_MAX 0xF3
  349. static reloc_howto_type bfin_howto_table [] =
  350. {
  351. /* This reloc does nothing. . */
  352. HOWTO (R_BFIN_UNUSED0, /* type. */
  353. 0, /* rightshift. */
  354. 3, /* size (0 = byte, 1 = short, 2 = long). */
  355. 0, /* bitsize. */
  356. FALSE, /* pc_relative. */
  357. 0, /* bitpos. */
  358. complain_overflow_dont, /* complain_on_overflow. */
  359. bfd_elf_generic_reloc, /* special_function. */
  360. "R_BFIN_UNUSED0", /* name. */
  361. FALSE, /* partial_inplace. */
  362. 0, /* src_mask. */
  363. 0, /* dst_mask. */
  364. FALSE), /* pcrel_offset. */
  365. HOWTO (R_BFIN_PCREL5M2, /* type. */
  366. 1, /* rightshift. */
  367. 1, /* size (0 = byte, 1 = short, 2 = long).. */
  368. 4, /* bitsize. */
  369. TRUE, /* pc_relative. */
  370. 0, /* bitpos. */
  371. complain_overflow_unsigned, /* complain_on_overflow. */
  372. bfin_bfd_reloc, /* special_function. */
  373. "R_BFIN_PCREL5M2", /* name. */
  374. FALSE, /* partial_inplace. */
  375. 0, /* src_mask. */
  376. 0x0000000F, /* dst_mask. */
  377. FALSE), /* pcrel_offset. */
  378. HOWTO (R_BFIN_UNUSED1, /* type. */
  379. 0, /* rightshift. */
  380. 3, /* size (0 = byte, 1 = short, 2 = long). */
  381. 0, /* bitsize. */
  382. FALSE, /* pc_relative. */
  383. 0, /* bitpos. */
  384. complain_overflow_dont, /* complain_on_overflow. */
  385. bfd_elf_generic_reloc, /* special_function. */
  386. "R_BFIN_UNUSED1", /* name. */
  387. FALSE, /* partial_inplace. */
  388. 0, /* src_mask. */
  389. 0, /* dst_mask. */
  390. FALSE), /* pcrel_offset. */
  391. HOWTO (R_BFIN_PCREL10, /* type. */
  392. 1, /* rightshift. */
  393. 1, /* size (0 = byte, 1 = short, 2 = long). */
  394. 10, /* bitsize. */
  395. TRUE, /* pc_relative. */
  396. 0, /* bitpos. */
  397. complain_overflow_signed, /* complain_on_overflow. */
  398. bfin_bfd_reloc, /* special_function. */
  399. "R_BFIN_PCREL10", /* name. */
  400. FALSE, /* partial_inplace. */
  401. 0, /* src_mask. */
  402. 0x000003FF, /* dst_mask. */
  403. TRUE), /* pcrel_offset. */
  404. HOWTO (R_BFIN_PCREL12_JUMP, /* type. */
  405. 1, /* rightshift. */
  406. /* the offset is actually 13 bit
  407. aligned on a word boundary so
  408. only 12 bits have to be used.
  409. Right shift the rightmost bit.. */
  410. 1, /* size (0 = byte, 1 = short, 2 = long). */
  411. 12, /* bitsize. */
  412. TRUE, /* pc_relative. */
  413. 0, /* bitpos. */
  414. complain_overflow_signed, /* complain_on_overflow. */
  415. bfin_bfd_reloc, /* special_function. */
  416. "R_BFIN_PCREL12_JUMP", /* name. */
  417. FALSE, /* partial_inplace. */
  418. 0, /* src_mask. */
  419. 0x0FFF, /* dst_mask. */
  420. TRUE), /* pcrel_offset. */
  421. HOWTO (R_BFIN_RIMM16, /* type. */
  422. 0, /* rightshift. */
  423. 1, /* size (0 = byte, 1 = short, 2 = long). */
  424. 16, /* bitsize. */
  425. FALSE, /* pc_relative. */
  426. 0, /* bitpos. */
  427. complain_overflow_signed, /* complain_on_overflow. */
  428. bfin_imm16_reloc, /* special_function. */
  429. "R_BFIN_RIMM16", /* name. */
  430. FALSE, /* partial_inplace. */
  431. 0, /* src_mask. */
  432. 0x0000FFFF, /* dst_mask. */
  433. TRUE), /* pcrel_offset. */
  434. HOWTO (R_BFIN_LUIMM16, /* type. */
  435. 0, /* rightshift. */
  436. 1, /* size (0 = byte, 1 = short, 2 = long). */
  437. 16, /* bitsize. */
  438. FALSE, /* pc_relative. */
  439. 0, /* bitpos. */
  440. complain_overflow_dont, /* complain_on_overflow. */
  441. bfin_imm16_reloc, /* special_function. */
  442. "R_BFIN_LUIMM16", /* name. */
  443. FALSE, /* partial_inplace. */
  444. 0, /* src_mask. */
  445. 0x0000FFFF, /* dst_mask. */
  446. TRUE), /* pcrel_offset. */
  447. HOWTO (R_BFIN_HUIMM16, /* type. */
  448. 16, /* rightshift. */
  449. 1, /* size (0 = byte, 1 = short, 2 = long). */
  450. 16, /* bitsize. */
  451. FALSE, /* pc_relative. */
  452. 0, /* bitpos. */
  453. complain_overflow_unsigned, /* complain_on_overflow. */
  454. bfin_imm16_reloc, /* special_function. */
  455. "R_BFIN_HUIMM16", /* name. */
  456. FALSE, /* partial_inplace. */
  457. 0, /* src_mask. */
  458. 0x0000FFFF, /* dst_mask. */
  459. TRUE), /* pcrel_offset. */
  460. HOWTO (R_BFIN_PCREL12_JUMP_S, /* type. */
  461. 1, /* rightshift. */
  462. 1, /* size (0 = byte, 1 = short, 2 = long). */
  463. 12, /* bitsize. */
  464. TRUE, /* pc_relative. */
  465. 0, /* bitpos. */
  466. complain_overflow_signed, /* complain_on_overflow. */
  467. bfin_bfd_reloc, /* special_function. */
  468. "R_BFIN_PCREL12_JUMP_S", /* name. */
  469. FALSE, /* partial_inplace. */
  470. 0, /* src_mask. */
  471. 0x00000FFF, /* dst_mask. */
  472. TRUE), /* pcrel_offset. */
  473. HOWTO (R_BFIN_PCREL24_JUMP_X, /* type. */
  474. 1, /* rightshift. */
  475. 2, /* size (0 = byte, 1 = short, 2 = long). */
  476. 24, /* bitsize. */
  477. TRUE, /* pc_relative. */
  478. 0, /* bitpos. */
  479. complain_overflow_signed, /* complain_on_overflow. */
  480. bfin_pcrel24_reloc, /* special_function. */
  481. "R_BFIN_PCREL24_JUMP_X", /* name. */
  482. FALSE, /* partial_inplace. */
  483. 0, /* src_mask. */
  484. 0x00FFFFFF, /* dst_mask. */
  485. TRUE), /* pcrel_offset. */
  486. HOWTO (R_BFIN_PCREL24, /* type. */
  487. 1, /* rightshift. */
  488. 2, /* size (0 = byte, 1 = short, 2 = long). */
  489. 24, /* bitsize. */
  490. TRUE, /* pc_relative. */
  491. 0, /* bitpos. */
  492. complain_overflow_signed, /* complain_on_overflow. */
  493. bfin_pcrel24_reloc, /* special_function. */
  494. "R_BFIN_PCREL24", /* name. */
  495. FALSE, /* partial_inplace. */
  496. 0, /* src_mask. */
  497. 0x00FFFFFF, /* dst_mask. */
  498. TRUE), /* pcrel_offset. */
  499. HOWTO (R_BFIN_UNUSEDB, /* type. */
  500. 0, /* rightshift. */
  501. 3, /* size (0 = byte, 1 = short, 2 = long). */
  502. 0, /* bitsize. */
  503. FALSE, /* pc_relative. */
  504. 0, /* bitpos. */
  505. complain_overflow_dont, /* complain_on_overflow. */
  506. bfd_elf_generic_reloc, /* special_function. */
  507. "R_BFIN_UNUSEDB", /* name. */
  508. FALSE, /* partial_inplace. */
  509. 0, /* src_mask. */
  510. 0, /* dst_mask. */
  511. FALSE), /* pcrel_offset. */
  512. HOWTO (R_BFIN_UNUSEDC, /* type. */
  513. 0, /* rightshift. */
  514. 3, /* size (0 = byte, 1 = short, 2 = long). */
  515. 0, /* bitsize. */
  516. FALSE, /* pc_relative. */
  517. 0, /* bitpos. */
  518. complain_overflow_dont, /* complain_on_overflow. */
  519. bfd_elf_generic_reloc, /* special_function. */
  520. "R_BFIN_UNUSEDC", /* name. */
  521. FALSE, /* partial_inplace. */
  522. 0, /* src_mask. */
  523. 0, /* dst_mask. */
  524. FALSE), /* pcrel_offset. */
  525. HOWTO (R_BFIN_PCREL24_JUMP_L, /* type. */
  526. 1, /* rightshift. */
  527. 2, /* size (0 = byte, 1 = short, 2 = long). */
  528. 24, /* bitsize. */
  529. TRUE, /* pc_relative. */
  530. 0, /* bitpos. */
  531. complain_overflow_signed, /* complain_on_overflow. */
  532. bfin_pcrel24_reloc, /* special_function. */
  533. "R_BFIN_PCREL24_JUMP_L", /* name. */
  534. FALSE, /* partial_inplace. */
  535. 0, /* src_mask. */
  536. 0x00FFFFFF, /* dst_mask. */
  537. TRUE), /* pcrel_offset. */
  538. HOWTO (R_BFIN_PCREL24_CALL_X, /* type. */
  539. 1, /* rightshift. */
  540. 2, /* size (0 = byte, 1 = short, 2 = long). */
  541. 24, /* bitsize. */
  542. TRUE, /* pc_relative. */
  543. 0, /* bitpos. */
  544. complain_overflow_signed, /* complain_on_overflow. */
  545. bfin_pcrel24_reloc, /* special_function. */
  546. "R_BFIN_PCREL24_CALL_X", /* name. */
  547. FALSE, /* partial_inplace. */
  548. 0, /* src_mask. */
  549. 0x00FFFFFF, /* dst_mask. */
  550. TRUE), /* pcrel_offset. */
  551. HOWTO (R_BFIN_VAR_EQ_SYMB, /* type. */
  552. 0, /* rightshift. */
  553. 2, /* size (0 = byte, 1 = short, 2 = long). */
  554. 32, /* bitsize. */
  555. FALSE, /* pc_relative. */
  556. 0, /* bitpos. */
  557. complain_overflow_bitfield, /* complain_on_overflow. */
  558. bfin_bfd_reloc, /* special_function. */
  559. "R_BFIN_VAR_EQ_SYMB", /* name. */
  560. FALSE, /* partial_inplace. */
  561. 0, /* src_mask. */
  562. 0, /* dst_mask. */
  563. FALSE), /* pcrel_offset. */
  564. HOWTO (R_BFIN_BYTE_DATA, /* type. */
  565. 0, /* rightshift. */
  566. 0, /* size (0 = byte, 1 = short, 2 = long). */
  567. 8, /* bitsize. */
  568. FALSE, /* pc_relative. */
  569. 0, /* bitpos. */
  570. complain_overflow_unsigned, /* complain_on_overflow. */
  571. bfin_bfd_reloc, /* special_function. */
  572. "R_BFIN_BYTE_DATA", /* name. */
  573. FALSE, /* partial_inplace. */
  574. 0, /* src_mask. */
  575. 0xFF, /* dst_mask. */
  576. TRUE), /* pcrel_offset. */
  577. HOWTO (R_BFIN_BYTE2_DATA, /* type. */
  578. 0, /* rightshift. */
  579. 1, /* size (0 = byte, 1 = short, 2 = long). */
  580. 16, /* bitsize. */
  581. FALSE, /* pc_relative. */
  582. 0, /* bitpos. */
  583. complain_overflow_signed, /* complain_on_overflow. */
  584. bfin_bfd_reloc, /* special_function. */
  585. "R_BFIN_BYTE2_DATA", /* name. */
  586. FALSE, /* partial_inplace. */
  587. 0, /* src_mask. */
  588. 0xFFFF, /* dst_mask. */
  589. TRUE), /* pcrel_offset. */
  590. HOWTO (R_BFIN_BYTE4_DATA, /* type. */
  591. 0, /* rightshift. */
  592. 2, /* size (0 = byte, 1 = short, 2 = long). */
  593. 32, /* bitsize. */
  594. FALSE, /* pc_relative. */
  595. 0, /* bitpos. */
  596. complain_overflow_unsigned, /* complain_on_overflow. */
  597. bfin_byte4_reloc, /* special_function. */
  598. "R_BFIN_BYTE4_DATA", /* name. */
  599. FALSE, /* partial_inplace. */
  600. 0, /* src_mask. */
  601. 0xFFFFFFFF, /* dst_mask. */
  602. TRUE), /* pcrel_offset. */
  603. HOWTO (R_BFIN_PCREL11, /* type. */
  604. 1, /* rightshift. */
  605. 1, /* size (0 = byte, 1 = short, 2 = long). */
  606. 10, /* bitsize. */
  607. TRUE, /* pc_relative. */
  608. 0, /* bitpos. */
  609. complain_overflow_unsigned, /* complain_on_overflow. */
  610. bfin_bfd_reloc, /* special_function. */
  611. "R_BFIN_PCREL11", /* name. */
  612. FALSE, /* partial_inplace. */
  613. 0, /* src_mask. */
  614. 0x000003FF, /* dst_mask. */
  615. FALSE), /* pcrel_offset. */
  616. /* A 18-bit signed operand with the GOT offset for the address of
  617. the symbol. */
  618. HOWTO (R_BFIN_GOT17M4, /* type */
  619. 2, /* rightshift */
  620. 1, /* size (0 = byte, 1 = short, 2 = long) */
  621. 16, /* bitsize */
  622. FALSE, /* pc_relative */
  623. 0, /* bitpos */
  624. complain_overflow_signed, /* complain_on_overflow */
  625. bfd_elf_generic_reloc, /* special_function */
  626. "R_BFIN_GOT17M4", /* name */
  627. FALSE, /* partial_inplace */
  628. 0xffff, /* src_mask */
  629. 0xffff, /* dst_mask */
  630. FALSE), /* pcrel_offset */
  631. /* The upper 16 bits of the GOT offset for the address of the
  632. symbol. */
  633. HOWTO (R_BFIN_GOTHI, /* type */
  634. 0, /* rightshift */
  635. 1, /* size (0 = byte, 1 = short, 2 = long) */
  636. 16, /* bitsize */
  637. FALSE, /* pc_relative */
  638. 0, /* bitpos */
  639. complain_overflow_dont, /* complain_on_overflow */
  640. bfd_elf_generic_reloc, /* special_function */
  641. "R_BFIN_GOTHI", /* name */
  642. FALSE, /* partial_inplace */
  643. 0xffff, /* src_mask */
  644. 0xffff, /* dst_mask */
  645. FALSE), /* pcrel_offset */
  646. /* The lower 16 bits of the GOT offset for the address of the
  647. symbol. */
  648. HOWTO (R_BFIN_GOTLO, /* type */
  649. 0, /* rightshift */
  650. 1, /* size (0 = byte, 1 = short, 2 = long) */
  651. 16, /* bitsize */
  652. FALSE, /* pc_relative */
  653. 0, /* bitpos */
  654. complain_overflow_dont, /* complain_on_overflow */
  655. bfd_elf_generic_reloc, /* special_function */
  656. "R_BFIN_GOTLO", /* name */
  657. FALSE, /* partial_inplace */
  658. 0xffff, /* src_mask */
  659. 0xffff, /* dst_mask */
  660. FALSE), /* pcrel_offset */
  661. /* The 32-bit address of the canonical descriptor of a function. */
  662. HOWTO (R_BFIN_FUNCDESC, /* type */
  663. 0, /* rightshift */
  664. 2, /* size (0 = byte, 1 = short, 2 = long) */
  665. 32, /* bitsize */
  666. FALSE, /* pc_relative */
  667. 0, /* bitpos */
  668. complain_overflow_bitfield, /* complain_on_overflow */
  669. bfd_elf_generic_reloc, /* special_function */
  670. "R_BFIN_FUNCDESC", /* name */
  671. FALSE, /* partial_inplace */
  672. 0xffffffff, /* src_mask */
  673. 0xffffffff, /* dst_mask */
  674. FALSE), /* pcrel_offset */
  675. /* A 12-bit signed operand with the GOT offset for the address of
  676. canonical descriptor of a function. */
  677. HOWTO (R_BFIN_FUNCDESC_GOT17M4, /* type */
  678. 2, /* rightshift */
  679. 1, /* size (0 = byte, 1 = short, 2 = long) */
  680. 16, /* bitsize */
  681. FALSE, /* pc_relative */
  682. 0, /* bitpos */
  683. complain_overflow_signed, /* complain_on_overflow */
  684. bfd_elf_generic_reloc, /* special_function */
  685. "R_BFIN_FUNCDESC_GOT17M4", /* name */
  686. FALSE, /* partial_inplace */
  687. 0xffff, /* src_mask */
  688. 0xffff, /* dst_mask */
  689. FALSE), /* pcrel_offset */
  690. /* The upper 16 bits of the GOT offset for the address of the
  691. canonical descriptor of a function. */
  692. HOWTO (R_BFIN_FUNCDESC_GOTHI, /* type */
  693. 0, /* rightshift */
  694. 1, /* size (0 = byte, 1 = short, 2 = long) */
  695. 16, /* bitsize */
  696. FALSE, /* pc_relative */
  697. 0, /* bitpos */
  698. complain_overflow_dont, /* complain_on_overflow */
  699. bfd_elf_generic_reloc, /* special_function */
  700. "R_BFIN_FUNCDESC_GOTHI", /* name */
  701. FALSE, /* partial_inplace */
  702. 0xffff, /* src_mask */
  703. 0xffff, /* dst_mask */
  704. FALSE), /* pcrel_offset */
  705. /* The lower 16 bits of the GOT offset for the address of the
  706. canonical descriptor of a function. */
  707. HOWTO (R_BFIN_FUNCDESC_GOTLO, /* type */
  708. 0, /* rightshift */
  709. 1, /* size (0 = byte, 1 = short, 2 = long) */
  710. 16, /* bitsize */
  711. FALSE, /* pc_relative */
  712. 0, /* bitpos */
  713. complain_overflow_dont, /* complain_on_overflow */
  714. bfd_elf_generic_reloc, /* special_function */
  715. "R_BFIN_FUNCDESC_GOTLO", /* name */
  716. FALSE, /* partial_inplace */
  717. 0xffff, /* src_mask */
  718. 0xffff, /* dst_mask */
  719. FALSE), /* pcrel_offset */
  720. /* The 32-bit address of the canonical descriptor of a function. */
  721. HOWTO (R_BFIN_FUNCDESC_VALUE, /* type */
  722. 0, /* rightshift */
  723. 2, /* size (0 = byte, 1 = short, 2 = long) */
  724. 64, /* bitsize */
  725. FALSE, /* pc_relative */
  726. 0, /* bitpos */
  727. complain_overflow_bitfield, /* complain_on_overflow */
  728. bfd_elf_generic_reloc, /* special_function */
  729. "R_BFIN_FUNCDESC_VALUE", /* name */
  730. FALSE, /* partial_inplace */
  731. 0xffffffff, /* src_mask */
  732. 0xffffffff, /* dst_mask */
  733. FALSE), /* pcrel_offset */
  734. /* A 12-bit signed operand with the GOT offset for the address of
  735. canonical descriptor of a function. */
  736. HOWTO (R_BFIN_FUNCDESC_GOTOFF17M4, /* type */
  737. 2, /* rightshift */
  738. 1, /* size (0 = byte, 1 = short, 2 = long) */
  739. 16, /* bitsize */
  740. FALSE, /* pc_relative */
  741. 0, /* bitpos */
  742. complain_overflow_signed, /* complain_on_overflow */
  743. bfd_elf_generic_reloc, /* special_function */
  744. "R_BFIN_FUNCDESC_GOTOFF17M4", /* name */
  745. FALSE, /* partial_inplace */
  746. 0xffff, /* src_mask */
  747. 0xffff, /* dst_mask */
  748. FALSE), /* pcrel_offset */
  749. /* The upper 16 bits of the GOT offset for the address of the
  750. canonical descriptor of a function. */
  751. HOWTO (R_BFIN_FUNCDESC_GOTOFFHI, /* type */
  752. 0, /* rightshift */
  753. 1, /* size (0 = byte, 1 = short, 2 = long) */
  754. 16, /* bitsize */
  755. FALSE, /* pc_relative */
  756. 0, /* bitpos */
  757. complain_overflow_dont, /* complain_on_overflow */
  758. bfd_elf_generic_reloc, /* special_function */
  759. "R_BFIN_FUNCDESC_GOTOFFHI", /* name */
  760. FALSE, /* partial_inplace */
  761. 0xffff, /* src_mask */
  762. 0xffff, /* dst_mask */
  763. FALSE), /* pcrel_offset */
  764. /* The lower 16 bits of the GOT offset for the address of the
  765. canonical descriptor of a function. */
  766. HOWTO (R_BFIN_FUNCDESC_GOTOFFLO, /* type */
  767. 0, /* rightshift */
  768. 1, /* size (0 = byte, 1 = short, 2 = long) */
  769. 16, /* bitsize */
  770. FALSE, /* pc_relative */
  771. 0, /* bitpos */
  772. complain_overflow_dont, /* complain_on_overflow */
  773. bfd_elf_generic_reloc, /* special_function */
  774. "R_BFIN_FUNCDESC_GOTOFFLO", /* name */
  775. FALSE, /* partial_inplace */
  776. 0xffff, /* src_mask */
  777. 0xffff, /* dst_mask */
  778. FALSE), /* pcrel_offset */
  779. /* A 12-bit signed operand with the GOT offset for the address of
  780. the symbol. */
  781. HOWTO (R_BFIN_GOTOFF17M4, /* type */
  782. 2, /* rightshift */
  783. 1, /* size (0 = byte, 1 = short, 2 = long) */
  784. 16, /* bitsize */
  785. FALSE, /* pc_relative */
  786. 0, /* bitpos */
  787. complain_overflow_signed, /* complain_on_overflow */
  788. bfd_elf_generic_reloc, /* special_function */
  789. "R_BFIN_GOTOFF17M4", /* name */
  790. FALSE, /* partial_inplace */
  791. 0xffff, /* src_mask */
  792. 0xffff, /* dst_mask */
  793. FALSE), /* pcrel_offset */
  794. /* The upper 16 bits of the GOT offset for the address of the
  795. symbol. */
  796. HOWTO (R_BFIN_GOTOFFHI, /* type */
  797. 0, /* rightshift */
  798. 1, /* size (0 = byte, 1 = short, 2 = long) */
  799. 16, /* bitsize */
  800. FALSE, /* pc_relative */
  801. 0, /* bitpos */
  802. complain_overflow_dont, /* complain_on_overflow */
  803. bfd_elf_generic_reloc, /* special_function */
  804. "R_BFIN_GOTOFFHI", /* name */
  805. FALSE, /* partial_inplace */
  806. 0xffff, /* src_mask */
  807. 0xffff, /* dst_mask */
  808. FALSE), /* pcrel_offset */
  809. /* The lower 16 bits of the GOT offset for the address of the
  810. symbol. */
  811. HOWTO (R_BFIN_GOTOFFLO, /* type */
  812. 0, /* rightshift */
  813. 1, /* size (0 = byte, 1 = short, 2 = long) */
  814. 16, /* bitsize */
  815. FALSE, /* pc_relative */
  816. 0, /* bitpos */
  817. complain_overflow_dont, /* complain_on_overflow */
  818. bfd_elf_generic_reloc, /* special_function */
  819. "R_BFIN_GOTOFFLO", /* name */
  820. FALSE, /* partial_inplace */
  821. 0xffff, /* src_mask */
  822. 0xffff, /* dst_mask */
  823. FALSE), /* pcrel_offset */
  824. };
  825. static reloc_howto_type bfin_gnuext_howto_table [] =
  826. {
  827. HOWTO (R_BFIN_PLTPC, /* type. */
  828. 0, /* rightshift. */
  829. 1, /* size (0 = byte, 1 = short, 2 = long). */
  830. 16, /* bitsize. */
  831. FALSE, /* pc_relative. */
  832. 0, /* bitpos. */
  833. complain_overflow_bitfield, /* complain_on_overflow. */
  834. bfin_pltpc_reloc, /* special_function. */
  835. "R_BFIN_PLTPC", /* name. */
  836. FALSE, /* partial_inplace. */
  837. 0xffff, /* src_mask. */
  838. 0xffff, /* dst_mask. */
  839. FALSE), /* pcrel_offset. */
  840. HOWTO (R_BFIN_GOT, /* type. */
  841. 0, /* rightshift. */
  842. 1, /* size (0 = byte, 1 = short, 2 = long). */
  843. 16, /* bitsize. */
  844. FALSE, /* pc_relative. */
  845. 0, /* bitpos. */
  846. complain_overflow_bitfield, /* complain_on_overflow. */
  847. bfd_elf_generic_reloc, /* special_function. */
  848. "R_BFIN_GOT", /* name. */
  849. FALSE, /* partial_inplace. */
  850. 0x7fff, /* src_mask. */
  851. 0x7fff, /* dst_mask. */
  852. FALSE), /* pcrel_offset. */
  853. /* GNU extension to record C++ vtable hierarchy. */
  854. HOWTO (R_BFIN_GNU_VTINHERIT, /* type. */
  855. 0, /* rightshift. */
  856. 2, /* size (0 = byte, 1 = short, 2 = long). */
  857. 0, /* bitsize. */
  858. FALSE, /* pc_relative. */
  859. 0, /* bitpos. */
  860. complain_overflow_dont, /* complain_on_overflow. */
  861. NULL, /* special_function. */
  862. "R_BFIN_GNU_VTINHERIT", /* name. */
  863. FALSE, /* partial_inplace. */
  864. 0, /* src_mask. */
  865. 0, /* dst_mask. */
  866. FALSE), /* pcrel_offset. */
  867. /* GNU extension to record C++ vtable member usage. */
  868. HOWTO (R_BFIN_GNU_VTENTRY, /* type. */
  869. 0, /* rightshift. */
  870. 2, /* size (0 = byte, 1 = short, 2 = long). */
  871. 0, /* bitsize. */
  872. FALSE, /* pc_relative. */
  873. 0, /* bitpos. */
  874. complain_overflow_dont, /* complain_on_overflow. */
  875. _bfd_elf_rel_vtable_reloc_fn, /* special_function. */
  876. "R_BFIN_GNU_VTENTRY", /* name. */
  877. FALSE, /* partial_inplace. */
  878. 0, /* src_mask. */
  879. 0, /* dst_mask. */
  880. FALSE) /* pcrel_offset. */
  881. };
  882. struct bfin_reloc_map
  883. {
  884. bfd_reloc_code_real_type bfd_reloc_val;
  885. unsigned int bfin_reloc_val;
  886. };
  887. static const struct bfin_reloc_map bfin_reloc_map [] =
  888. {
  889. { BFD_RELOC_NONE, R_BFIN_UNUSED0 },
  890. { BFD_RELOC_BFIN_5_PCREL, R_BFIN_PCREL5M2 },
  891. { BFD_RELOC_NONE, R_BFIN_UNUSED1 },
  892. { BFD_RELOC_BFIN_10_PCREL, R_BFIN_PCREL10 },
  893. { BFD_RELOC_BFIN_12_PCREL_JUMP, R_BFIN_PCREL12_JUMP },
  894. { BFD_RELOC_BFIN_16_IMM, R_BFIN_RIMM16 },
  895. { BFD_RELOC_BFIN_16_LOW, R_BFIN_LUIMM16 },
  896. { BFD_RELOC_BFIN_16_HIGH, R_BFIN_HUIMM16 },
  897. { BFD_RELOC_BFIN_12_PCREL_JUMP_S, R_BFIN_PCREL12_JUMP_S },
  898. { BFD_RELOC_24_PCREL, R_BFIN_PCREL24 },
  899. { BFD_RELOC_24_PCREL, R_BFIN_PCREL24 },
  900. { BFD_RELOC_BFIN_24_PCREL_JUMP_L, R_BFIN_PCREL24_JUMP_L },
  901. { BFD_RELOC_NONE, R_BFIN_UNUSEDB },
  902. { BFD_RELOC_NONE, R_BFIN_UNUSEDC },
  903. { BFD_RELOC_BFIN_24_PCREL_CALL_X, R_BFIN_PCREL24_CALL_X },
  904. { BFD_RELOC_8, R_BFIN_BYTE_DATA },
  905. { BFD_RELOC_16, R_BFIN_BYTE2_DATA },
  906. { BFD_RELOC_32, R_BFIN_BYTE4_DATA },
  907. { BFD_RELOC_BFIN_11_PCREL, R_BFIN_PCREL11 },
  908. { BFD_RELOC_BFIN_GOT, R_BFIN_GOT },
  909. { BFD_RELOC_BFIN_PLTPC, R_BFIN_PLTPC },
  910. { BFD_RELOC_BFIN_GOT17M4, R_BFIN_GOT17M4 },
  911. { BFD_RELOC_BFIN_GOTHI, R_BFIN_GOTHI },
  912. { BFD_RELOC_BFIN_GOTLO, R_BFIN_GOTLO },
  913. { BFD_RELOC_BFIN_FUNCDESC, R_BFIN_FUNCDESC },
  914. { BFD_RELOC_BFIN_FUNCDESC_GOT17M4, R_BFIN_FUNCDESC_GOT17M4 },
  915. { BFD_RELOC_BFIN_FUNCDESC_GOTHI, R_BFIN_FUNCDESC_GOTHI },
  916. { BFD_RELOC_BFIN_FUNCDESC_GOTLO, R_BFIN_FUNCDESC_GOTLO },
  917. { BFD_RELOC_BFIN_FUNCDESC_VALUE, R_BFIN_FUNCDESC_VALUE },
  918. { BFD_RELOC_BFIN_FUNCDESC_GOTOFF17M4, R_BFIN_FUNCDESC_GOTOFF17M4 },
  919. { BFD_RELOC_BFIN_FUNCDESC_GOTOFFHI, R_BFIN_FUNCDESC_GOTOFFHI },
  920. { BFD_RELOC_BFIN_FUNCDESC_GOTOFFLO, R_BFIN_FUNCDESC_GOTOFFLO },
  921. { BFD_RELOC_BFIN_GOTOFF17M4, R_BFIN_GOTOFF17M4 },
  922. { BFD_RELOC_BFIN_GOTOFFHI, R_BFIN_GOTOFFHI },
  923. { BFD_RELOC_BFIN_GOTOFFLO, R_BFIN_GOTOFFLO },
  924. { BFD_RELOC_VTABLE_INHERIT, R_BFIN_GNU_VTINHERIT },
  925. { BFD_RELOC_VTABLE_ENTRY, R_BFIN_GNU_VTENTRY },
  926. };
  927. static void
  928. bfin_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
  929. arelent *cache_ptr,
  930. Elf_Internal_Rela *dst)
  931. {
  932. unsigned int r_type;
  933. r_type = ELF32_R_TYPE (dst->r_info);
  934. if (r_type <= BFIN_RELOC_MAX)
  935. cache_ptr->howto = &bfin_howto_table [r_type];
  936. else if (r_type >= BFIN_GNUEXT_RELOC_MIN && r_type <= BFIN_GNUEXT_RELOC_MAX)
  937. cache_ptr->howto = &bfin_gnuext_howto_table [r_type - BFIN_GNUEXT_RELOC_MIN];
  938. else
  939. cache_ptr->howto = (reloc_howto_type *) NULL;
  940. }
  941. /* Given a BFD reloc type, return the howto. */
  942. static reloc_howto_type *
  943. bfin_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
  944. bfd_reloc_code_real_type code)
  945. {
  946. unsigned int i;
  947. unsigned int r_type = (unsigned int) -1;
  948. for (i = sizeof (bfin_reloc_map) / sizeof (bfin_reloc_map[0]); i--;)
  949. if (bfin_reloc_map[i].bfd_reloc_val == code)
  950. r_type = bfin_reloc_map[i].bfin_reloc_val;
  951. if (r_type <= BFIN_RELOC_MAX)
  952. return &bfin_howto_table [r_type];
  953. else if (r_type >= BFIN_GNUEXT_RELOC_MIN && r_type <= BFIN_GNUEXT_RELOC_MAX)
  954. return &bfin_gnuext_howto_table [r_type - BFIN_GNUEXT_RELOC_MIN];
  955. return (reloc_howto_type *) NULL;
  956. }
  957. static reloc_howto_type *
  958. bfin_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  959. const char *r_name)
  960. {
  961. unsigned int i;
  962. for (i = 0;
  963. i < (sizeof (bfin_howto_table)
  964. / sizeof (bfin_howto_table[0]));
  965. i++)
  966. if (bfin_howto_table[i].name != NULL
  967. && strcasecmp (bfin_howto_table[i].name, r_name) == 0)
  968. return &bfin_howto_table[i];
  969. for (i = 0;
  970. i < (sizeof (bfin_gnuext_howto_table)
  971. / sizeof (bfin_gnuext_howto_table[0]));
  972. i++)
  973. if (bfin_gnuext_howto_table[i].name != NULL
  974. && strcasecmp (bfin_gnuext_howto_table[i].name, r_name) == 0)
  975. return &bfin_gnuext_howto_table[i];
  976. return NULL;
  977. }
  978. /* Given a bfin relocation type, return the howto. */
  979. static reloc_howto_type *
  980. bfin_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
  981. unsigned int r_type)
  982. {
  983. if (r_type <= BFIN_RELOC_MAX)
  984. return &bfin_howto_table [r_type];
  985. else if (r_type >= BFIN_GNUEXT_RELOC_MIN && r_type <= BFIN_GNUEXT_RELOC_MAX)
  986. return &bfin_gnuext_howto_table [r_type - BFIN_GNUEXT_RELOC_MIN];
  987. return (reloc_howto_type *) NULL;
  988. }
  989. /* Set by ld emulation if --code-in-l1. */
  990. bfd_boolean elf32_bfin_code_in_l1 = 0;
  991. /* Set by ld emulation if --data-in-l1. */
  992. bfd_boolean elf32_bfin_data_in_l1 = 0;
  993. static void
  994. elf32_bfin_final_write_processing (bfd *abfd,
  995. bfd_boolean linker ATTRIBUTE_UNUSED)
  996. {
  997. if (elf32_bfin_code_in_l1)
  998. elf_elfheader (abfd)->e_flags |= EF_BFIN_CODE_IN_L1;
  999. if (elf32_bfin_data_in_l1)
  1000. elf_elfheader (abfd)->e_flags |= EF_BFIN_DATA_IN_L1;
  1001. }
  1002. /* Return TRUE if the name is a local label.
  1003. bfin local labels begin with L$. */
  1004. static bfd_boolean
  1005. bfin_is_local_label_name (bfd *abfd, const char *label)
  1006. {
  1007. if (label[0] == 'L' && label[1] == '$' )
  1008. return TRUE;
  1009. return _bfd_elf_is_local_label_name (abfd, label);
  1010. }
  1011. /* Look through the relocs for a section during the first phase, and
  1012. allocate space in the global offset table or procedure linkage
  1013. table. */
  1014. static bfd_boolean
  1015. bfin_check_relocs (bfd * abfd,
  1016. struct bfd_link_info *info,
  1017. asection *sec,
  1018. const Elf_Internal_Rela *relocs)
  1019. {
  1020. bfd *dynobj;
  1021. Elf_Internal_Shdr *symtab_hdr;
  1022. struct elf_link_hash_entry **sym_hashes;
  1023. bfd_signed_vma *local_got_refcounts;
  1024. const Elf_Internal_Rela *rel;
  1025. const Elf_Internal_Rela *rel_end;
  1026. asection *sgot;
  1027. asection *srelgot;
  1028. if (bfd_link_relocatable (info))
  1029. return TRUE;
  1030. dynobj = elf_hash_table (info)->dynobj;
  1031. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1032. sym_hashes = elf_sym_hashes (abfd);
  1033. local_got_refcounts = elf_local_got_refcounts (abfd);
  1034. sgot = NULL;
  1035. srelgot = NULL;
  1036. rel_end = relocs + sec->reloc_count;
  1037. for (rel = relocs; rel < rel_end; rel++)
  1038. {
  1039. unsigned long r_symndx;
  1040. struct elf_link_hash_entry *h;
  1041. r_symndx = ELF32_R_SYM (rel->r_info);
  1042. if (r_symndx < symtab_hdr->sh_info)
  1043. h = NULL;
  1044. else
  1045. {
  1046. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1047. /* PR15323, ref flags aren't set for references in the same
  1048. object. */
  1049. h->root.non_ir_ref = 1;
  1050. }
  1051. switch (ELF32_R_TYPE (rel->r_info))
  1052. {
  1053. /* This relocation describes the C++ object vtable hierarchy.
  1054. Reconstruct it for later use during GC. */
  1055. case R_BFIN_GNU_VTINHERIT:
  1056. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  1057. return FALSE;
  1058. break;
  1059. /* This relocation describes which C++ vtable entries
  1060. are actually used. Record for later use during GC. */
  1061. case R_BFIN_GNU_VTENTRY:
  1062. BFD_ASSERT (h != NULL);
  1063. if (h != NULL
  1064. && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  1065. return FALSE;
  1066. break;
  1067. case R_BFIN_GOT:
  1068. if (h != NULL
  1069. && strcmp (h->root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
  1070. break;
  1071. /* Fall through. */
  1072. if (dynobj == NULL)
  1073. {
  1074. /* Create the .got section. */
  1075. elf_hash_table (info)->dynobj = dynobj = abfd;
  1076. if (!_bfd_elf_create_got_section (dynobj, info))
  1077. return FALSE;
  1078. }
  1079. if (sgot == NULL)
  1080. {
  1081. sgot = bfd_get_linker_section (dynobj, ".got");
  1082. BFD_ASSERT (sgot != NULL);
  1083. }
  1084. if (srelgot == NULL && (h != NULL || bfd_link_pic (info)))
  1085. {
  1086. srelgot = bfd_get_linker_section (dynobj, ".rela.got");
  1087. if (srelgot == NULL)
  1088. {
  1089. flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
  1090. | SEC_IN_MEMORY | SEC_LINKER_CREATED
  1091. | SEC_READONLY);
  1092. srelgot = bfd_make_section_anyway_with_flags (dynobj,
  1093. ".rela.got",
  1094. flags);
  1095. if (srelgot == NULL
  1096. || !bfd_set_section_alignment (dynobj, srelgot, 2))
  1097. return FALSE;
  1098. }
  1099. }
  1100. if (h != NULL)
  1101. {
  1102. if (h->got.refcount == 0)
  1103. {
  1104. /* Make sure this symbol is output as a dynamic symbol. */
  1105. if (h->dynindx == -1 && !h->forced_local)
  1106. {
  1107. if (!bfd_elf_link_record_dynamic_symbol (info, h))
  1108. return FALSE;
  1109. }
  1110. /* Allocate space in the .got section. */
  1111. sgot->size += 4;
  1112. /* Allocate relocation space. */
  1113. srelgot->size += sizeof (Elf32_External_Rela);
  1114. }
  1115. h->got.refcount++;
  1116. }
  1117. else
  1118. {
  1119. /* This is a global offset table entry for a local symbol. */
  1120. if (local_got_refcounts == NULL)
  1121. {
  1122. bfd_size_type size;
  1123. size = symtab_hdr->sh_info;
  1124. size *= sizeof (bfd_signed_vma);
  1125. local_got_refcounts = ((bfd_signed_vma *)
  1126. bfd_zalloc (abfd, size));
  1127. if (local_got_refcounts == NULL)
  1128. return FALSE;
  1129. elf_local_got_refcounts (abfd) = local_got_refcounts;
  1130. }
  1131. if (local_got_refcounts[r_symndx] == 0)
  1132. {
  1133. sgot->size += 4;
  1134. if (bfd_link_pic (info))
  1135. {
  1136. /* If we are generating a shared object, we need to
  1137. output a R_68K_RELATIVE reloc so that the dynamic
  1138. linker can adjust this GOT entry. */
  1139. srelgot->size += sizeof (Elf32_External_Rela);
  1140. }
  1141. }
  1142. local_got_refcounts[r_symndx]++;
  1143. }
  1144. break;
  1145. default:
  1146. break;
  1147. }
  1148. }
  1149. return TRUE;
  1150. }
  1151. static enum elf_reloc_type_class
  1152. elf32_bfin_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1153. const asection *rel_sec ATTRIBUTE_UNUSED,
  1154. const Elf_Internal_Rela * rela)
  1155. {
  1156. switch ((int) ELF32_R_TYPE (rela->r_info))
  1157. {
  1158. default:
  1159. return reloc_class_normal;
  1160. }
  1161. }
  1162. static bfd_reloc_status_type
  1163. bfin_final_link_relocate (Elf_Internal_Rela *rel, reloc_howto_type *howto,
  1164. bfd *input_bfd, asection *input_section,
  1165. bfd_byte *contents, bfd_vma address,
  1166. bfd_vma value, bfd_vma addend)
  1167. {
  1168. int r_type = ELF32_R_TYPE (rel->r_info);
  1169. if (r_type == R_BFIN_PCREL24 || r_type == R_BFIN_PCREL24_JUMP_L)
  1170. {
  1171. bfd_reloc_status_type r = bfd_reloc_ok;
  1172. bfd_vma x;
  1173. if (address > bfd_get_section_limit (input_bfd, input_section))
  1174. return bfd_reloc_outofrange;
  1175. value += addend;
  1176. /* Perform usual pc-relative correction. */
  1177. value -= input_section->output_section->vma + input_section->output_offset;
  1178. value -= address;
  1179. /* We are getting reloc_entry->address 2 byte off from
  1180. the start of instruction. Assuming absolute postion
  1181. of the reloc data. But, following code had been written assuming
  1182. reloc address is starting at begining of instruction.
  1183. To compensate that I have increased the value of
  1184. relocation by 1 (effectively 2) and used the addr -2 instead of addr. */
  1185. value += 2;
  1186. address -= 2;
  1187. if ((value & 0xFF000000) != 0
  1188. && (value & 0xFF000000) != 0xFF000000)
  1189. r = bfd_reloc_overflow;
  1190. value >>= 1;
  1191. x = bfd_get_16 (input_bfd, contents + address);
  1192. x = (x & 0xff00) | ((value >> 16) & 0xff);
  1193. bfd_put_16 (input_bfd, x, contents + address);
  1194. x = bfd_get_16 (input_bfd, contents + address + 2);
  1195. x = value & 0xFFFF;
  1196. bfd_put_16 (input_bfd, x, contents + address + 2);
  1197. return r;
  1198. }
  1199. return _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
  1200. rel->r_offset, value, addend);
  1201. }
  1202. static bfd_boolean
  1203. bfin_relocate_section (bfd * output_bfd,
  1204. struct bfd_link_info *info,
  1205. bfd * input_bfd,
  1206. asection * input_section,
  1207. bfd_byte * contents,
  1208. Elf_Internal_Rela * relocs,
  1209. Elf_Internal_Sym * local_syms,
  1210. asection ** local_sections)
  1211. {
  1212. bfd *dynobj;
  1213. Elf_Internal_Shdr *symtab_hdr;
  1214. struct elf_link_hash_entry **sym_hashes;
  1215. bfd_vma *local_got_offsets;
  1216. asection *sgot;
  1217. Elf_Internal_Rela *rel;
  1218. Elf_Internal_Rela *relend;
  1219. int i = 0;
  1220. dynobj = elf_hash_table (info)->dynobj;
  1221. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  1222. sym_hashes = elf_sym_hashes (input_bfd);
  1223. local_got_offsets = elf_local_got_offsets (input_bfd);
  1224. sgot = NULL;
  1225. rel = relocs;
  1226. relend = relocs + input_section->reloc_count;
  1227. for (; rel < relend; rel++, i++)
  1228. {
  1229. int r_type;
  1230. reloc_howto_type *howto;
  1231. unsigned long r_symndx;
  1232. struct elf_link_hash_entry *h;
  1233. Elf_Internal_Sym *sym;
  1234. asection *sec;
  1235. bfd_vma relocation = 0;
  1236. bfd_boolean unresolved_reloc;
  1237. bfd_reloc_status_type r;
  1238. bfd_vma address;
  1239. r_type = ELF32_R_TYPE (rel->r_info);
  1240. if (r_type < 0 || r_type >= 243)
  1241. {
  1242. bfd_set_error (bfd_error_bad_value);
  1243. return FALSE;
  1244. }
  1245. if (r_type == R_BFIN_GNU_VTENTRY
  1246. || r_type == R_BFIN_GNU_VTINHERIT)
  1247. continue;
  1248. howto = bfin_reloc_type_lookup (input_bfd, r_type);
  1249. if (howto == NULL)
  1250. {
  1251. bfd_set_error (bfd_error_bad_value);
  1252. return FALSE;
  1253. }
  1254. r_symndx = ELF32_R_SYM (rel->r_info);
  1255. h = NULL;
  1256. sym = NULL;
  1257. sec = NULL;
  1258. unresolved_reloc = FALSE;
  1259. if (r_symndx < symtab_hdr->sh_info)
  1260. {
  1261. sym = local_syms + r_symndx;
  1262. sec = local_sections[r_symndx];
  1263. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  1264. }
  1265. else
  1266. {
  1267. bfd_boolean warned, ignored;
  1268. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  1269. r_symndx, symtab_hdr, sym_hashes,
  1270. h, sec, relocation,
  1271. unresolved_reloc, warned, ignored);
  1272. }
  1273. if (sec != NULL && discarded_section (sec))
  1274. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  1275. rel, 1, relend, howto, 0, contents);
  1276. if (bfd_link_relocatable (info))
  1277. continue;
  1278. address = rel->r_offset;
  1279. /* Then, process normally. */
  1280. switch (r_type)
  1281. {
  1282. case R_BFIN_GNU_VTINHERIT:
  1283. case R_BFIN_GNU_VTENTRY:
  1284. return bfd_reloc_ok;
  1285. case R_BFIN_GOT:
  1286. /* Relocation is to the address of the entry for this symbol
  1287. in the global offset table. */
  1288. if (h != NULL
  1289. && strcmp (h->root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
  1290. goto do_default;
  1291. /* Fall through. */
  1292. /* Relocation is the offset of the entry for this symbol in
  1293. the global offset table. */
  1294. {
  1295. bfd_vma off;
  1296. if (dynobj == NULL)
  1297. {
  1298. /* Create the .got section. */
  1299. elf_hash_table (info)->dynobj = dynobj = output_bfd;
  1300. if (!_bfd_elf_create_got_section (dynobj, info))
  1301. return FALSE;
  1302. }
  1303. if (sgot == NULL)
  1304. {
  1305. sgot = bfd_get_linker_section (dynobj, ".got");
  1306. BFD_ASSERT (sgot != NULL);
  1307. }
  1308. if (h != NULL)
  1309. {
  1310. bfd_boolean dyn;
  1311. off = h->got.offset;
  1312. BFD_ASSERT (off != (bfd_vma) - 1);
  1313. dyn = elf_hash_table (info)->dynamic_sections_created;
  1314. if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  1315. bfd_link_pic (info),
  1316. h)
  1317. || (bfd_link_pic (info)
  1318. && (info->symbolic
  1319. || h->dynindx == -1
  1320. || h->forced_local)
  1321. && h->def_regular))
  1322. {
  1323. /* This is actually a static link, or it is a
  1324. -Bsymbolic link and the symbol is defined
  1325. locally, or the symbol was forced to be local
  1326. because of a version file.. We must initialize
  1327. this entry in the global offset table. Since
  1328. the offset must always be a multiple of 4, we
  1329. use the least significant bit to record whether
  1330. we have initialized it already.
  1331. When doing a dynamic link, we create a .rela.got
  1332. relocation entry to initialize the value. This
  1333. is done in the finish_dynamic_symbol routine. */
  1334. if ((off & 1) != 0)
  1335. off &= ~1;
  1336. else
  1337. {
  1338. bfd_put_32 (output_bfd, relocation,
  1339. sgot->contents + off);
  1340. h->got.offset |= 1;
  1341. }
  1342. }
  1343. else
  1344. unresolved_reloc = FALSE;
  1345. }
  1346. else
  1347. {
  1348. BFD_ASSERT (local_got_offsets != NULL);
  1349. off = local_got_offsets[r_symndx];
  1350. BFD_ASSERT (off != (bfd_vma) - 1);
  1351. /* The offset must always be a multiple of 4. We use
  1352. the least significant bit to record whether we have
  1353. already generated the necessary reloc. */
  1354. if ((off & 1) != 0)
  1355. off &= ~1;
  1356. else
  1357. {
  1358. bfd_put_32 (output_bfd, relocation, sgot->contents + off);
  1359. if (bfd_link_pic (info))
  1360. {
  1361. asection *s;
  1362. Elf_Internal_Rela outrel;
  1363. bfd_byte *loc;
  1364. s = bfd_get_linker_section (dynobj, ".rela.got");
  1365. BFD_ASSERT (s != NULL);
  1366. outrel.r_offset = (sgot->output_section->vma
  1367. + sgot->output_offset + off);
  1368. outrel.r_info =
  1369. ELF32_R_INFO (0, R_BFIN_PCREL24);
  1370. outrel.r_addend = relocation;
  1371. loc = s->contents;
  1372. loc +=
  1373. s->reloc_count++ * sizeof (Elf32_External_Rela);
  1374. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  1375. }
  1376. local_got_offsets[r_symndx] |= 1;
  1377. }
  1378. }
  1379. relocation = sgot->output_offset + off;
  1380. rel->r_addend = 0;
  1381. /* bfin : preg = [preg + 17bitdiv4offset] relocation is div by 4. */
  1382. relocation /= 4;
  1383. }
  1384. goto do_default;
  1385. default:
  1386. do_default:
  1387. r = bfin_final_link_relocate (rel, howto, input_bfd, input_section,
  1388. contents, address,
  1389. relocation, rel->r_addend);
  1390. break;
  1391. }
  1392. /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
  1393. because such sections are not SEC_ALLOC and thus ld.so will
  1394. not process them. */
  1395. if (unresolved_reloc
  1396. && !((input_section->flags & SEC_DEBUGGING) != 0 && h->def_dynamic)
  1397. && _bfd_elf_section_offset (output_bfd, info, input_section,
  1398. rel->r_offset) != (bfd_vma) -1)
  1399. {
  1400. (*_bfd_error_handler)
  1401. (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
  1402. input_bfd,
  1403. input_section, (long) rel->r_offset, h->root.root.string);
  1404. return FALSE;
  1405. }
  1406. if (r != bfd_reloc_ok)
  1407. {
  1408. const char *name;
  1409. if (h != NULL)
  1410. name = h->root.root.string;
  1411. else
  1412. {
  1413. name = bfd_elf_string_from_elf_section (input_bfd,
  1414. symtab_hdr->sh_link,
  1415. sym->st_name);
  1416. if (name == NULL)
  1417. return FALSE;
  1418. if (*name == '\0')
  1419. name = bfd_section_name (input_bfd, sec);
  1420. }
  1421. if (r == bfd_reloc_overflow)
  1422. {
  1423. if (!(info->callbacks->reloc_overflow
  1424. (info, (h ? &h->root : NULL), name, howto->name,
  1425. (bfd_vma) 0, input_bfd, input_section, rel->r_offset)))
  1426. return FALSE;
  1427. }
  1428. else
  1429. {
  1430. (*_bfd_error_handler)
  1431. (_("%B(%A+0x%lx): reloc against `%s': error %d"),
  1432. input_bfd, input_section,
  1433. (long) rel->r_offset, name, (int) r);
  1434. return FALSE;
  1435. }
  1436. }
  1437. }
  1438. return TRUE;
  1439. }
  1440. static asection *
  1441. bfin_gc_mark_hook (asection * sec,
  1442. struct bfd_link_info *info,
  1443. Elf_Internal_Rela * rel,
  1444. struct elf_link_hash_entry *h,
  1445. Elf_Internal_Sym * sym)
  1446. {
  1447. if (h != NULL)
  1448. switch (ELF32_R_TYPE (rel->r_info))
  1449. {
  1450. case R_BFIN_GNU_VTINHERIT:
  1451. case R_BFIN_GNU_VTENTRY:
  1452. return NULL;
  1453. }
  1454. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  1455. }
  1456. /* Update the got entry reference counts for the section being removed. */
  1457. static bfd_boolean
  1458. bfin_gc_sweep_hook (bfd * abfd,
  1459. struct bfd_link_info *info,
  1460. asection * sec,
  1461. const Elf_Internal_Rela * relocs)
  1462. {
  1463. Elf_Internal_Shdr *symtab_hdr;
  1464. struct elf_link_hash_entry **sym_hashes;
  1465. bfd_signed_vma *local_got_refcounts;
  1466. const Elf_Internal_Rela *rel, *relend;
  1467. bfd *dynobj;
  1468. asection *sgot;
  1469. asection *srelgot;
  1470. dynobj = elf_hash_table (info)->dynobj;
  1471. if (dynobj == NULL)
  1472. return TRUE;
  1473. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1474. sym_hashes = elf_sym_hashes (abfd);
  1475. local_got_refcounts = elf_local_got_refcounts (abfd);
  1476. sgot = bfd_get_linker_section (dynobj, ".got");
  1477. srelgot = bfd_get_linker_section (dynobj, ".rela.got");
  1478. relend = relocs + sec->reloc_count;
  1479. for (rel = relocs; rel < relend; rel++)
  1480. {
  1481. unsigned long r_symndx;
  1482. struct elf_link_hash_entry *h;
  1483. switch (ELF32_R_TYPE (rel->r_info))
  1484. {
  1485. case R_BFIN_GOT:
  1486. r_symndx = ELF32_R_SYM (rel->r_info);
  1487. if (r_symndx >= symtab_hdr->sh_info)
  1488. {
  1489. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1490. if (h->got.refcount > 0)
  1491. {
  1492. --h->got.refcount;
  1493. if (h->got.refcount == 0)
  1494. {
  1495. /* We don't need the .got entry any more. */
  1496. sgot->size -= 4;
  1497. srelgot->size -= sizeof (Elf32_External_Rela);
  1498. }
  1499. }
  1500. }
  1501. else if (local_got_refcounts != NULL)
  1502. {
  1503. if (local_got_refcounts[r_symndx] > 0)
  1504. {
  1505. --local_got_refcounts[r_symndx];
  1506. if (local_got_refcounts[r_symndx] == 0)
  1507. {
  1508. /* We don't need the .got entry any more. */
  1509. sgot->size -= 4;
  1510. if (bfd_link_pic (info))
  1511. srelgot->size -= sizeof (Elf32_External_Rela);
  1512. }
  1513. }
  1514. }
  1515. break;
  1516. default:
  1517. break;
  1518. }
  1519. }
  1520. return TRUE;
  1521. }
  1522. extern const bfd_target bfin_elf32_fdpic_vec;
  1523. #define IS_FDPIC(bfd) ((bfd)->xvec == &bfin_elf32_fdpic_vec)
  1524. /* An extension of the elf hash table data structure,
  1525. containing some additional Blackfin-specific data. */
  1526. struct bfinfdpic_elf_link_hash_table
  1527. {
  1528. struct elf_link_hash_table elf;
  1529. /* A pointer to the .got section. */
  1530. asection *sgot;
  1531. /* A pointer to the .rel.got section. */
  1532. asection *sgotrel;
  1533. /* A pointer to the .rofixup section. */
  1534. asection *sgotfixup;
  1535. /* A pointer to the .plt section. */
  1536. asection *splt;
  1537. /* A pointer to the .rel.plt section. */
  1538. asection *spltrel;
  1539. /* GOT base offset. */
  1540. bfd_vma got0;
  1541. /* Location of the first non-lazy PLT entry, i.e., the number of
  1542. bytes taken by lazy PLT entries. */
  1543. bfd_vma plt0;
  1544. /* A hash table holding information about which symbols were
  1545. referenced with which PIC-related relocations. */
  1546. struct htab *relocs_info;
  1547. /* Summary reloc information collected by
  1548. _bfinfdpic_count_got_plt_entries. */
  1549. struct _bfinfdpic_dynamic_got_info *g;
  1550. };
  1551. /* Get the Blackfin ELF linker hash table from a link_info structure. */
  1552. #define bfinfdpic_hash_table(info) \
  1553. (elf_hash_table_id ((struct elf_link_hash_table *) ((info)->hash)) \
  1554. == BFIN_ELF_DATA ? ((struct bfinfdpic_elf_link_hash_table *) ((info)->hash)) : NULL)
  1555. #define bfinfdpic_got_section(info) \
  1556. (bfinfdpic_hash_table (info)->sgot)
  1557. #define bfinfdpic_gotrel_section(info) \
  1558. (bfinfdpic_hash_table (info)->sgotrel)
  1559. #define bfinfdpic_gotfixup_section(info) \
  1560. (bfinfdpic_hash_table (info)->sgotfixup)
  1561. #define bfinfdpic_plt_section(info) \
  1562. (bfinfdpic_hash_table (info)->splt)
  1563. #define bfinfdpic_pltrel_section(info) \
  1564. (bfinfdpic_hash_table (info)->spltrel)
  1565. #define bfinfdpic_relocs_info(info) \
  1566. (bfinfdpic_hash_table (info)->relocs_info)
  1567. #define bfinfdpic_got_initial_offset(info) \
  1568. (bfinfdpic_hash_table (info)->got0)
  1569. #define bfinfdpic_plt_initial_offset(info) \
  1570. (bfinfdpic_hash_table (info)->plt0)
  1571. #define bfinfdpic_dynamic_got_plt_info(info) \
  1572. (bfinfdpic_hash_table (info)->g)
  1573. /* The name of the dynamic interpreter. This is put in the .interp
  1574. section. */
  1575. #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
  1576. #define DEFAULT_STACK_SIZE 0x20000
  1577. /* This structure is used to collect the number of entries present in
  1578. each addressable range of the got. */
  1579. struct _bfinfdpic_dynamic_got_info
  1580. {
  1581. /* Several bits of information about the current link. */
  1582. struct bfd_link_info *info;
  1583. /* Total size needed for GOT entries within the 18- or 32-bit
  1584. ranges. */
  1585. bfd_vma got17m4, gothilo;
  1586. /* Total size needed for function descriptor entries within the 18-
  1587. or 32-bit ranges. */
  1588. bfd_vma fd17m4, fdhilo;
  1589. /* Total size needed function descriptor entries referenced in PLT
  1590. entries, that would be profitable to place in offsets close to
  1591. the PIC register. */
  1592. bfd_vma fdplt;
  1593. /* Total size needed by lazy PLT entries. */
  1594. bfd_vma lzplt;
  1595. /* Number of relocations carried over from input object files. */
  1596. unsigned long relocs;
  1597. /* Number of fixups introduced by relocations in input object files. */
  1598. unsigned long fixups;
  1599. };
  1600. /* Create a Blackfin ELF linker hash table. */
  1601. static struct bfd_link_hash_table *
  1602. bfinfdpic_elf_link_hash_table_create (bfd *abfd)
  1603. {
  1604. struct bfinfdpic_elf_link_hash_table *ret;
  1605. bfd_size_type amt = sizeof (struct bfinfdpic_elf_link_hash_table);
  1606. ret = bfd_zmalloc (amt);
  1607. if (ret == NULL)
  1608. return NULL;
  1609. if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
  1610. _bfd_elf_link_hash_newfunc,
  1611. sizeof (struct elf_link_hash_entry),
  1612. BFIN_ELF_DATA))
  1613. {
  1614. free (ret);
  1615. return NULL;
  1616. }
  1617. return &ret->elf.root;
  1618. }
  1619. /* Decide whether a reference to a symbol can be resolved locally or
  1620. not. If the symbol is protected, we want the local address, but
  1621. its function descriptor must be assigned by the dynamic linker. */
  1622. #define BFINFDPIC_SYM_LOCAL(INFO, H) \
  1623. (_bfd_elf_symbol_refs_local_p ((H), (INFO), 1) \
  1624. || ! elf_hash_table (INFO)->dynamic_sections_created)
  1625. #define BFINFDPIC_FUNCDESC_LOCAL(INFO, H) \
  1626. ((H)->dynindx == -1 || ! elf_hash_table (INFO)->dynamic_sections_created)
  1627. /* This structure collects information on what kind of GOT, PLT or
  1628. function descriptors are required by relocations that reference a
  1629. certain symbol. */
  1630. struct bfinfdpic_relocs_info
  1631. {
  1632. /* The index of the symbol, as stored in the relocation r_info, if
  1633. we have a local symbol; -1 otherwise. */
  1634. long symndx;
  1635. union
  1636. {
  1637. /* The input bfd in which the symbol is defined, if it's a local
  1638. symbol. */
  1639. bfd *abfd;
  1640. /* If symndx == -1, the hash table entry corresponding to a global
  1641. symbol (even if it turns out to bind locally, in which case it
  1642. should ideally be replaced with section's symndx + addend). */
  1643. struct elf_link_hash_entry *h;
  1644. } d;
  1645. /* The addend of the relocation that references the symbol. */
  1646. bfd_vma addend;
  1647. /* The fields above are used to identify an entry. The fields below
  1648. contain information on how an entry is used and, later on, which
  1649. locations it was assigned. */
  1650. /* The following 2 fields record whether the symbol+addend above was
  1651. ever referenced with a GOT relocation. The 17M4 suffix indicates a
  1652. GOT17M4 relocation; hilo is used for GOTLO/GOTHI pairs. */
  1653. unsigned got17m4;
  1654. unsigned gothilo;
  1655. /* Whether a FUNCDESC relocation references symbol+addend. */
  1656. unsigned fd;
  1657. /* Whether a FUNCDESC_GOT relocation references symbol+addend. */
  1658. unsigned fdgot17m4;
  1659. unsigned fdgothilo;
  1660. /* Whether a FUNCDESC_GOTOFF relocation references symbol+addend. */
  1661. unsigned fdgoff17m4;
  1662. unsigned fdgoffhilo;
  1663. /* Whether symbol+addend is referenced with GOTOFF17M4, GOTOFFLO or
  1664. GOTOFFHI relocations. The addend doesn't really matter, since we
  1665. envision that this will only be used to check whether the symbol
  1666. is mapped to the same segment as the got. */
  1667. unsigned gotoff;
  1668. /* Whether symbol+addend is referenced by a LABEL24 relocation. */
  1669. unsigned call;
  1670. /* Whether symbol+addend is referenced by a 32 or FUNCDESC_VALUE
  1671. relocation. */
  1672. unsigned sym;
  1673. /* Whether we need a PLT entry for a symbol. Should be implied by
  1674. something like:
  1675. (call && symndx == -1 && ! BFINFDPIC_SYM_LOCAL (info, d.h)) */
  1676. unsigned plt:1;
  1677. /* Whether a function descriptor should be created in this link unit
  1678. for symbol+addend. Should be implied by something like:
  1679. (plt || fdgotoff17m4 || fdgotofflohi
  1680. || ((fd || fdgot17m4 || fdgothilo)
  1681. && (symndx != -1 || BFINFDPIC_FUNCDESC_LOCAL (info, d.h)))) */
  1682. unsigned privfd:1;
  1683. /* Whether a lazy PLT entry is needed for this symbol+addend.
  1684. Should be implied by something like:
  1685. (privfd && symndx == -1 && ! BFINFDPIC_SYM_LOCAL (info, d.h)
  1686. && ! (info->flags & DF_BIND_NOW)) */
  1687. unsigned lazyplt:1;
  1688. /* Whether we've already emitted GOT relocations and PLT entries as
  1689. needed for this symbol. */
  1690. unsigned done:1;
  1691. /* The number of R_BFIN_BYTE4_DATA, R_BFIN_FUNCDESC and R_BFIN_FUNCDESC_VALUE
  1692. relocations referencing the symbol. */
  1693. unsigned relocs32, relocsfd, relocsfdv;
  1694. /* The number of .rofixups entries and dynamic relocations allocated
  1695. for this symbol, minus any that might have already been used. */
  1696. unsigned fixups, dynrelocs;
  1697. /* The offsets of the GOT entries assigned to symbol+addend, to the
  1698. function descriptor's address, and to a function descriptor,
  1699. respectively. Should be zero if unassigned. The offsets are
  1700. counted from the value that will be assigned to the PIC register,
  1701. not from the beginning of the .got section. */
  1702. bfd_signed_vma got_entry, fdgot_entry, fd_entry;
  1703. /* The offsets of the PLT entries assigned to symbol+addend,
  1704. non-lazy and lazy, respectively. If unassigned, should be
  1705. (bfd_vma)-1. */
  1706. bfd_vma plt_entry, lzplt_entry;
  1707. };
  1708. /* Compute a hash with the key fields of an bfinfdpic_relocs_info entry. */
  1709. static hashval_t
  1710. bfinfdpic_relocs_info_hash (const void *entry_)
  1711. {
  1712. const struct bfinfdpic_relocs_info *entry = entry_;
  1713. return (entry->symndx == -1
  1714. ? (long) entry->d.h->root.root.hash
  1715. : entry->symndx + (long) entry->d.abfd->id * 257) + entry->addend;
  1716. }
  1717. /* Test whether the key fields of two bfinfdpic_relocs_info entries are
  1718. identical. */
  1719. static int
  1720. bfinfdpic_relocs_info_eq (const void *entry1, const void *entry2)
  1721. {
  1722. const struct bfinfdpic_relocs_info *e1 = entry1;
  1723. const struct bfinfdpic_relocs_info *e2 = entry2;
  1724. return e1->symndx == e2->symndx && e1->addend == e2->addend
  1725. && (e1->symndx == -1 ? e1->d.h == e2->d.h : e1->d.abfd == e2->d.abfd);
  1726. }
  1727. /* Find or create an entry in a hash table HT that matches the key
  1728. fields of the given ENTRY. If it's not found, memory for a new
  1729. entry is allocated in ABFD's obstack. */
  1730. static struct bfinfdpic_relocs_info *
  1731. bfinfdpic_relocs_info_find (struct htab *ht,
  1732. bfd *abfd,
  1733. const struct bfinfdpic_relocs_info *entry,
  1734. enum insert_option insert)
  1735. {
  1736. struct bfinfdpic_relocs_info **loc;
  1737. if (!ht)
  1738. return NULL;
  1739. loc = (struct bfinfdpic_relocs_info **) htab_find_slot (ht, entry, insert);
  1740. if (! loc)
  1741. return NULL;
  1742. if (*loc)
  1743. return *loc;
  1744. *loc = bfd_zalloc (abfd, sizeof (**loc));
  1745. if (! *loc)
  1746. return *loc;
  1747. (*loc)->symndx = entry->symndx;
  1748. (*loc)->d = entry->d;
  1749. (*loc)->addend = entry->addend;
  1750. (*loc)->plt_entry = (bfd_vma)-1;
  1751. (*loc)->lzplt_entry = (bfd_vma)-1;
  1752. return *loc;
  1753. }
  1754. /* Obtain the address of the entry in HT associated with H's symbol +
  1755. addend, creating a new entry if none existed. ABFD is only used
  1756. for memory allocation purposes. */
  1757. inline static struct bfinfdpic_relocs_info *
  1758. bfinfdpic_relocs_info_for_global (struct htab *ht,
  1759. bfd *abfd,
  1760. struct elf_link_hash_entry *h,
  1761. bfd_vma addend,
  1762. enum insert_option insert)
  1763. {
  1764. struct bfinfdpic_relocs_info entry;
  1765. entry.symndx = -1;
  1766. entry.d.h = h;
  1767. entry.addend = addend;
  1768. return bfinfdpic_relocs_info_find (ht, abfd, &entry, insert);
  1769. }
  1770. /* Obtain the address of the entry in HT associated with the SYMNDXth
  1771. local symbol of the input bfd ABFD, plus the addend, creating a new
  1772. entry if none existed. */
  1773. inline static struct bfinfdpic_relocs_info *
  1774. bfinfdpic_relocs_info_for_local (struct htab *ht,
  1775. bfd *abfd,
  1776. long symndx,
  1777. bfd_vma addend,
  1778. enum insert_option insert)
  1779. {
  1780. struct bfinfdpic_relocs_info entry;
  1781. entry.symndx = symndx;
  1782. entry.d.abfd = abfd;
  1783. entry.addend = addend;
  1784. return bfinfdpic_relocs_info_find (ht, abfd, &entry, insert);
  1785. }
  1786. /* Merge fields set by check_relocs() of two entries that end up being
  1787. mapped to the same (presumably global) symbol. */
  1788. inline static void
  1789. bfinfdpic_pic_merge_early_relocs_info (struct bfinfdpic_relocs_info *e2,
  1790. struct bfinfdpic_relocs_info const *e1)
  1791. {
  1792. e2->got17m4 |= e1->got17m4;
  1793. e2->gothilo |= e1->gothilo;
  1794. e2->fd |= e1->fd;
  1795. e2->fdgot17m4 |= e1->fdgot17m4;
  1796. e2->fdgothilo |= e1->fdgothilo;
  1797. e2->fdgoff17m4 |= e1->fdgoff17m4;
  1798. e2->fdgoffhilo |= e1->fdgoffhilo;
  1799. e2->gotoff |= e1->gotoff;
  1800. e2->call |= e1->call;
  1801. e2->sym |= e1->sym;
  1802. }
  1803. /* Every block of 65535 lazy PLT entries shares a single call to the
  1804. resolver, inserted in the 32768th lazy PLT entry (i.e., entry #
  1805. 32767, counting from 0). All other lazy PLT entries branch to it
  1806. in a single instruction. */
  1807. #define LZPLT_RESOLVER_EXTRA 10
  1808. #define LZPLT_NORMAL_SIZE 6
  1809. #define LZPLT_ENTRIES 1362
  1810. #define BFINFDPIC_LZPLT_BLOCK_SIZE ((bfd_vma) LZPLT_NORMAL_SIZE * LZPLT_ENTRIES + LZPLT_RESOLVER_EXTRA)
  1811. #define BFINFDPIC_LZPLT_RESOLV_LOC (LZPLT_NORMAL_SIZE * LZPLT_ENTRIES / 2)
  1812. /* Add a dynamic relocation to the SRELOC section. */
  1813. inline static bfd_vma
  1814. _bfinfdpic_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
  1815. int reloc_type, long dynindx, bfd_vma addend,
  1816. struct bfinfdpic_relocs_info *entry)
  1817. {
  1818. Elf_Internal_Rela outrel;
  1819. bfd_vma reloc_offset;
  1820. outrel.r_offset = offset;
  1821. outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
  1822. outrel.r_addend = addend;
  1823. reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rel);
  1824. BFD_ASSERT (reloc_offset < sreloc->size);
  1825. bfd_elf32_swap_reloc_out (output_bfd, &outrel,
  1826. sreloc->contents + reloc_offset);
  1827. sreloc->reloc_count++;
  1828. /* If the entry's index is zero, this relocation was probably to a
  1829. linkonce section that got discarded. We reserved a dynamic
  1830. relocation, but it was for another entry than the one we got at
  1831. the time of emitting the relocation. Unfortunately there's no
  1832. simple way for us to catch this situation, since the relocation
  1833. is cleared right before calling relocate_section, at which point
  1834. we no longer know what the relocation used to point to. */
  1835. if (entry->symndx)
  1836. {
  1837. BFD_ASSERT (entry->dynrelocs > 0);
  1838. entry->dynrelocs--;
  1839. }
  1840. return reloc_offset;
  1841. }
  1842. /* Add a fixup to the ROFIXUP section. */
  1843. static bfd_vma
  1844. _bfinfdpic_add_rofixup (bfd *output_bfd, asection *rofixup, bfd_vma offset,
  1845. struct bfinfdpic_relocs_info *entry)
  1846. {
  1847. bfd_vma fixup_offset;
  1848. if (rofixup->flags & SEC_EXCLUDE)
  1849. return -1;
  1850. fixup_offset = rofixup->reloc_count * 4;
  1851. if (rofixup->contents)
  1852. {
  1853. BFD_ASSERT (fixup_offset < rofixup->size);
  1854. bfd_put_32 (output_bfd, offset, rofixup->contents + fixup_offset);
  1855. }
  1856. rofixup->reloc_count++;
  1857. if (entry && entry->symndx)
  1858. {
  1859. /* See discussion about symndx == 0 in _bfinfdpic_add_dyn_reloc
  1860. above. */
  1861. BFD_ASSERT (entry->fixups > 0);
  1862. entry->fixups--;
  1863. }
  1864. return fixup_offset;
  1865. }
  1866. /* Find the segment number in which OSEC, and output section, is
  1867. located. */
  1868. static unsigned
  1869. _bfinfdpic_osec_to_segment (bfd *output_bfd, asection *osec)
  1870. {
  1871. Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
  1872. return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
  1873. }
  1874. inline static bfd_boolean
  1875. _bfinfdpic_osec_readonly_p (bfd *output_bfd, asection *osec)
  1876. {
  1877. unsigned seg = _bfinfdpic_osec_to_segment (output_bfd, osec);
  1878. return ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W);
  1879. }
  1880. /* Generate relocations for GOT entries, function descriptors, and
  1881. code for PLT and lazy PLT entries. */
  1882. inline static bfd_boolean
  1883. _bfinfdpic_emit_got_relocs_plt_entries (struct bfinfdpic_relocs_info *entry,
  1884. bfd *output_bfd,
  1885. struct bfd_link_info *info,
  1886. asection *sec,
  1887. Elf_Internal_Sym *sym,
  1888. bfd_vma addend)
  1889. {
  1890. bfd_vma fd_lazy_rel_offset = (bfd_vma) -1;
  1891. int dynindx = -1;
  1892. if (entry->done)
  1893. return TRUE;
  1894. entry->done = 1;
  1895. if (entry->got_entry || entry->fdgot_entry || entry->fd_entry)
  1896. {
  1897. /* If the symbol is dynamic, consider it for dynamic
  1898. relocations, otherwise decay to section + offset. */
  1899. if (entry->symndx == -1 && entry->d.h->dynindx != -1)
  1900. dynindx = entry->d.h->dynindx;
  1901. else
  1902. {
  1903. if (sec
  1904. && sec->output_section
  1905. && ! bfd_is_abs_section (sec->output_section)
  1906. && ! bfd_is_und_section (sec->output_section))
  1907. dynindx = elf_section_data (sec->output_section)->dynindx;
  1908. else
  1909. dynindx = 0;
  1910. }
  1911. }
  1912. /* Generate relocation for GOT entry pointing to the symbol. */
  1913. if (entry->got_entry)
  1914. {
  1915. int idx = dynindx;
  1916. bfd_vma ad = addend;
  1917. /* If the symbol is dynamic but binds locally, use
  1918. section+offset. */
  1919. if (sec && (entry->symndx != -1
  1920. || BFINFDPIC_SYM_LOCAL (info, entry->d.h)))
  1921. {
  1922. if (entry->symndx == -1)
  1923. ad += entry->d.h->root.u.def.value;
  1924. else
  1925. ad += sym->st_value;
  1926. ad += sec->output_offset;
  1927. if (sec->output_section && elf_section_data (sec->output_section))
  1928. idx = elf_section_data (sec->output_section)->dynindx;
  1929. else
  1930. idx = 0;
  1931. }
  1932. /* If we're linking an executable at a fixed address, we can
  1933. omit the dynamic relocation as long as the symbol is local to
  1934. this module. */
  1935. if (bfd_link_pde (info)
  1936. && (entry->symndx != -1
  1937. || BFINFDPIC_SYM_LOCAL (info, entry->d.h)))
  1938. {
  1939. if (sec)
  1940. ad += sec->output_section->vma;
  1941. if (entry->symndx != -1
  1942. || entry->d.h->root.type != bfd_link_hash_undefweak)
  1943. _bfinfdpic_add_rofixup (output_bfd,
  1944. bfinfdpic_gotfixup_section (info),
  1945. bfinfdpic_got_section (info)->output_section
  1946. ->vma
  1947. + bfinfdpic_got_section (info)->output_offset
  1948. + bfinfdpic_got_initial_offset (info)
  1949. + entry->got_entry, entry);
  1950. }
  1951. else
  1952. _bfinfdpic_add_dyn_reloc (output_bfd, bfinfdpic_gotrel_section (info),
  1953. _bfd_elf_section_offset
  1954. (output_bfd, info,
  1955. bfinfdpic_got_section (info),
  1956. bfinfdpic_got_initial_offset (info)
  1957. + entry->got_entry)
  1958. + bfinfdpic_got_section (info)
  1959. ->output_section->vma
  1960. + bfinfdpic_got_section (info)->output_offset,
  1961. R_BFIN_BYTE4_DATA, idx, ad, entry);
  1962. bfd_put_32 (output_bfd, ad,
  1963. bfinfdpic_got_section (info)->contents
  1964. + bfinfdpic_got_initial_offset (info)
  1965. + entry->got_entry);
  1966. }
  1967. /* Generate relocation for GOT entry pointing to a canonical
  1968. function descriptor. */
  1969. if (entry->fdgot_entry)
  1970. {
  1971. int reloc, idx;
  1972. bfd_vma ad = 0;
  1973. if (! (entry->symndx == -1
  1974. && entry->d.h->root.type == bfd_link_hash_undefweak
  1975. && BFINFDPIC_SYM_LOCAL (info, entry->d.h)))
  1976. {
  1977. /* If the symbol is dynamic and there may be dynamic symbol
  1978. resolution because we are, or are linked with, a shared
  1979. library, emit a FUNCDESC relocation such that the dynamic
  1980. linker will allocate the function descriptor. If the
  1981. symbol needs a non-local function descriptor but binds
  1982. locally (e.g., its visibility is protected, emit a
  1983. dynamic relocation decayed to section+offset. */
  1984. if (entry->symndx == -1
  1985. && ! BFINFDPIC_FUNCDESC_LOCAL (info, entry->d.h)
  1986. && BFINFDPIC_SYM_LOCAL (info, entry->d.h)
  1987. && !bfd_link_pde (info))
  1988. {
  1989. reloc = R_BFIN_FUNCDESC;
  1990. idx = elf_section_data (entry->d.h->root.u.def.section
  1991. ->output_section)->dynindx;
  1992. ad = entry->d.h->root.u.def.section->output_offset
  1993. + entry->d.h->root.u.def.value;
  1994. }
  1995. else if (entry->symndx == -1
  1996. && ! BFINFDPIC_FUNCDESC_LOCAL (info, entry->d.h))
  1997. {
  1998. reloc = R_BFIN_FUNCDESC;
  1999. idx = dynindx;
  2000. ad = addend;
  2001. if (ad)
  2002. return FALSE;
  2003. }
  2004. else
  2005. {
  2006. /* Otherwise, we know we have a private function descriptor,
  2007. so reference it directly. */
  2008. if (elf_hash_table (info)->dynamic_sections_created)
  2009. BFD_ASSERT (entry->privfd);
  2010. reloc = R_BFIN_BYTE4_DATA;
  2011. idx = elf_section_data (bfinfdpic_got_section (info)
  2012. ->output_section)->dynindx;
  2013. ad = bfinfdpic_got_section (info)->output_offset
  2014. + bfinfdpic_got_initial_offset (info) + entry->fd_entry;
  2015. }
  2016. /* If there is room for dynamic symbol resolution, emit the
  2017. dynamic relocation. However, if we're linking an
  2018. executable at a fixed location, we won't have emitted a
  2019. dynamic symbol entry for the got section, so idx will be
  2020. zero, which means we can and should compute the address
  2021. of the private descriptor ourselves. */
  2022. if (bfd_link_pde (info)
  2023. && (entry->symndx != -1
  2024. || BFINFDPIC_FUNCDESC_LOCAL (info, entry->d.h)))
  2025. {
  2026. ad += bfinfdpic_got_section (info)->output_section->vma;
  2027. _bfinfdpic_add_rofixup (output_bfd,
  2028. bfinfdpic_gotfixup_section (info),
  2029. bfinfdpic_got_section (info)
  2030. ->output_section->vma
  2031. + bfinfdpic_got_section (info)
  2032. ->output_offset
  2033. + bfinfdpic_got_initial_offset (info)
  2034. + entry->fdgot_entry, entry);
  2035. }
  2036. else
  2037. _bfinfdpic_add_dyn_reloc (output_bfd,
  2038. bfinfdpic_gotrel_section (info),
  2039. _bfd_elf_section_offset
  2040. (output_bfd, info,
  2041. bfinfdpic_got_section (info),
  2042. bfinfdpic_got_initial_offset (info)
  2043. + entry->fdgot_entry)
  2044. + bfinfdpic_got_section (info)
  2045. ->output_section->vma
  2046. + bfinfdpic_got_section (info)
  2047. ->output_offset,
  2048. reloc, idx, ad, entry);
  2049. }
  2050. bfd_put_32 (output_bfd, ad,
  2051. bfinfdpic_got_section (info)->contents
  2052. + bfinfdpic_got_initial_offset (info)
  2053. + entry->fdgot_entry);
  2054. }
  2055. /* Generate relocation to fill in a private function descriptor in
  2056. the GOT. */
  2057. if (entry->fd_entry)
  2058. {
  2059. int idx = dynindx;
  2060. bfd_vma ad = addend;
  2061. bfd_vma ofst;
  2062. long lowword, highword;
  2063. /* If the symbol is dynamic but binds locally, use
  2064. section+offset. */
  2065. if (sec && (entry->symndx != -1
  2066. || BFINFDPIC_SYM_LOCAL (info, entry->d.h)))
  2067. {
  2068. if (entry->symndx == -1)
  2069. ad += entry->d.h->root.u.def.value;
  2070. else
  2071. ad += sym->st_value;
  2072. ad += sec->output_offset;
  2073. if (sec->output_section && elf_section_data (sec->output_section))
  2074. idx = elf_section_data (sec->output_section)->dynindx;
  2075. else
  2076. idx = 0;
  2077. }
  2078. /* If we're linking an executable at a fixed address, we can
  2079. omit the dynamic relocation as long as the symbol is local to
  2080. this module. */
  2081. if (bfd_link_pde (info)
  2082. && (entry->symndx != -1 || BFINFDPIC_SYM_LOCAL (info, entry->d.h)))
  2083. {
  2084. if (sec)
  2085. ad += sec->output_section->vma;
  2086. ofst = 0;
  2087. if (entry->symndx != -1
  2088. || entry->d.h->root.type != bfd_link_hash_undefweak)
  2089. {
  2090. _bfinfdpic_add_rofixup (output_bfd,
  2091. bfinfdpic_gotfixup_section (info),
  2092. bfinfdpic_got_section (info)
  2093. ->output_section->vma
  2094. + bfinfdpic_got_section (info)
  2095. ->output_offset
  2096. + bfinfdpic_got_initial_offset (info)
  2097. + entry->fd_entry, entry);
  2098. _bfinfdpic_add_rofixup (output_bfd,
  2099. bfinfdpic_gotfixup_section (info),
  2100. bfinfdpic_got_section (info)
  2101. ->output_section->vma
  2102. + bfinfdpic_got_section (info)
  2103. ->output_offset
  2104. + bfinfdpic_got_initial_offset (info)
  2105. + entry->fd_entry + 4, entry);
  2106. }
  2107. }
  2108. else
  2109. {
  2110. ofst
  2111. = _bfinfdpic_add_dyn_reloc (output_bfd,
  2112. entry->lazyplt
  2113. ? bfinfdpic_pltrel_section (info)
  2114. : bfinfdpic_gotrel_section (info),
  2115. _bfd_elf_section_offset
  2116. (output_bfd, info,
  2117. bfinfdpic_got_section (info),
  2118. bfinfdpic_got_initial_offset (info)
  2119. + entry->fd_entry)
  2120. + bfinfdpic_got_section (info)
  2121. ->output_section->vma
  2122. + bfinfdpic_got_section (info)
  2123. ->output_offset,
  2124. R_BFIN_FUNCDESC_VALUE, idx, ad, entry);
  2125. }
  2126. /* If we've omitted the dynamic relocation, just emit the fixed
  2127. addresses of the symbol and of the local GOT base offset. */
  2128. if (bfd_link_pde (info)
  2129. && sec
  2130. && sec->output_section)
  2131. {
  2132. lowword = ad;
  2133. highword = bfinfdpic_got_section (info)->output_section->vma
  2134. + bfinfdpic_got_section (info)->output_offset
  2135. + bfinfdpic_got_initial_offset (info);
  2136. }
  2137. else if (entry->lazyplt)
  2138. {
  2139. if (ad)
  2140. return FALSE;
  2141. fd_lazy_rel_offset = ofst;
  2142. /* A function descriptor used for lazy or local resolving is
  2143. initialized such that its high word contains the output
  2144. section index in which the PLT entries are located, and
  2145. the low word contains the address of the lazy PLT entry
  2146. entry point, that must be within the memory region
  2147. assigned to that section. */
  2148. lowword = entry->lzplt_entry + 4
  2149. + bfinfdpic_plt_section (info)->output_offset
  2150. + bfinfdpic_plt_section (info)->output_section->vma;
  2151. highword = _bfinfdpic_osec_to_segment
  2152. (output_bfd, bfinfdpic_plt_section (info)->output_section);
  2153. }
  2154. else
  2155. {
  2156. /* A function descriptor for a local function gets the index
  2157. of the section. For a non-local function, it's
  2158. disregarded. */
  2159. lowword = ad;
  2160. if (sec == NULL
  2161. || (entry->symndx == -1 && entry->d.h->dynindx != -1
  2162. && entry->d.h->dynindx == idx))
  2163. highword = 0;
  2164. else
  2165. highword = _bfinfdpic_osec_to_segment
  2166. (output_bfd, sec->output_section);
  2167. }
  2168. bfd_put_32 (output_bfd, lowword,
  2169. bfinfdpic_got_section (info)->contents
  2170. + bfinfdpic_got_initial_offset (info)
  2171. + entry->fd_entry);
  2172. bfd_put_32 (output_bfd, highword,
  2173. bfinfdpic_got_section (info)->contents
  2174. + bfinfdpic_got_initial_offset (info)
  2175. + entry->fd_entry + 4);
  2176. }
  2177. /* Generate code for the PLT entry. */
  2178. if (entry->plt_entry != (bfd_vma) -1)
  2179. {
  2180. bfd_byte *plt_code = bfinfdpic_plt_section (info)->contents
  2181. + entry->plt_entry;
  2182. BFD_ASSERT (entry->fd_entry);
  2183. /* Figure out what kind of PLT entry we need, depending on the
  2184. location of the function descriptor within the GOT. */
  2185. if (entry->fd_entry >= -(1 << (18 - 1))
  2186. && entry->fd_entry + 4 < (1 << (18 - 1)))
  2187. {
  2188. /* P1 = [P3 + fd_entry]; P3 = [P3 + fd_entry + 4] */
  2189. bfd_put_32 (output_bfd,
  2190. 0xe519 | ((entry->fd_entry << 14) & 0xFFFF0000),
  2191. plt_code);
  2192. bfd_put_32 (output_bfd,
  2193. 0xe51b | (((entry->fd_entry + 4) << 14) & 0xFFFF0000),
  2194. plt_code + 4);
  2195. plt_code += 8;
  2196. }
  2197. else
  2198. {
  2199. /* P1.L = fd_entry; P1.H = fd_entry;
  2200. P3 = P3 + P1;
  2201. P1 = [P3];
  2202. P3 = [P3 + 4]; */
  2203. bfd_put_32 (output_bfd,
  2204. 0xe109 | (entry->fd_entry << 16),
  2205. plt_code);
  2206. bfd_put_32 (output_bfd,
  2207. 0xe149 | (entry->fd_entry & 0xFFFF0000),
  2208. plt_code + 4);
  2209. bfd_put_16 (output_bfd, 0x5ad9, plt_code + 8);
  2210. bfd_put_16 (output_bfd, 0x9159, plt_code + 10);
  2211. bfd_put_16 (output_bfd, 0xac5b, plt_code + 12);
  2212. plt_code += 14;
  2213. }
  2214. /* JUMP (P1) */
  2215. bfd_put_16 (output_bfd, 0x0051, plt_code);
  2216. }
  2217. /* Generate code for the lazy PLT entry. */
  2218. if (entry->lzplt_entry != (bfd_vma) -1)
  2219. {
  2220. bfd_byte *lzplt_code = bfinfdpic_plt_section (info)->contents
  2221. + entry->lzplt_entry;
  2222. bfd_vma resolverStub_addr;
  2223. bfd_put_32 (output_bfd, fd_lazy_rel_offset, lzplt_code);
  2224. lzplt_code += 4;
  2225. resolverStub_addr = entry->lzplt_entry / BFINFDPIC_LZPLT_BLOCK_SIZE
  2226. * BFINFDPIC_LZPLT_BLOCK_SIZE + BFINFDPIC_LZPLT_RESOLV_LOC;
  2227. if (resolverStub_addr >= bfinfdpic_plt_initial_offset (info))
  2228. resolverStub_addr = bfinfdpic_plt_initial_offset (info) - LZPLT_NORMAL_SIZE - LZPLT_RESOLVER_EXTRA;
  2229. if (entry->lzplt_entry == resolverStub_addr)
  2230. {
  2231. /* This is a lazy PLT entry that includes a resolver call.
  2232. P2 = [P3];
  2233. R3 = [P3 + 4];
  2234. JUMP (P2); */
  2235. bfd_put_32 (output_bfd,
  2236. 0xa05b915a,
  2237. lzplt_code);
  2238. bfd_put_16 (output_bfd, 0x0052, lzplt_code + 4);
  2239. }
  2240. else
  2241. {
  2242. /* JUMP.S resolverStub */
  2243. bfd_put_16 (output_bfd,
  2244. 0x2000
  2245. | (((resolverStub_addr - entry->lzplt_entry)
  2246. / 2) & (((bfd_vma)1 << 12) - 1)),
  2247. lzplt_code);
  2248. }
  2249. }
  2250. return TRUE;
  2251. }
  2252. /* Relocate an Blackfin ELF section.
  2253. The RELOCATE_SECTION function is called by the new ELF backend linker
  2254. to handle the relocations for a section.
  2255. The relocs are always passed as Rela structures; if the section
  2256. actually uses Rel structures, the r_addend field will always be
  2257. zero.
  2258. This function is responsible for adjusting the section contents as
  2259. necessary, and (if using Rela relocs and generating a relocatable
  2260. output file) adjusting the reloc addend as necessary.
  2261. This function does not have to worry about setting the reloc
  2262. address or the reloc symbol index.
  2263. LOCAL_SYMS is a pointer to the swapped in local symbols.
  2264. LOCAL_SECTIONS is an array giving the section in the input file
  2265. corresponding to the st_shndx field of each local symbol.
  2266. The global hash table entry for the global symbols can be found
  2267. via elf_sym_hashes (input_bfd).
  2268. When generating relocatable output, this function must handle
  2269. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  2270. going to be the section symbol corresponding to the output
  2271. section, which means that the addend must be adjusted
  2272. accordingly. */
  2273. static bfd_boolean
  2274. bfinfdpic_relocate_section (bfd * output_bfd,
  2275. struct bfd_link_info *info,
  2276. bfd * input_bfd,
  2277. asection * input_section,
  2278. bfd_byte * contents,
  2279. Elf_Internal_Rela * relocs,
  2280. Elf_Internal_Sym * local_syms,
  2281. asection ** local_sections)
  2282. {
  2283. Elf_Internal_Shdr *symtab_hdr;
  2284. struct elf_link_hash_entry **sym_hashes;
  2285. Elf_Internal_Rela *rel;
  2286. Elf_Internal_Rela *relend;
  2287. unsigned isec_segment, got_segment, plt_segment,
  2288. check_segment[2];
  2289. int silence_segment_error = !bfd_link_pic (info);
  2290. symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
  2291. sym_hashes = elf_sym_hashes (input_bfd);
  2292. relend = relocs + input_section->reloc_count;
  2293. isec_segment = _bfinfdpic_osec_to_segment (output_bfd,
  2294. input_section->output_section);
  2295. if (IS_FDPIC (output_bfd) && bfinfdpic_got_section (info))
  2296. got_segment = _bfinfdpic_osec_to_segment (output_bfd,
  2297. bfinfdpic_got_section (info)
  2298. ->output_section);
  2299. else
  2300. got_segment = -1;
  2301. if (IS_FDPIC (output_bfd) && elf_hash_table (info)->dynamic_sections_created)
  2302. plt_segment = _bfinfdpic_osec_to_segment (output_bfd,
  2303. bfinfdpic_plt_section (info)
  2304. ->output_section);
  2305. else
  2306. plt_segment = -1;
  2307. for (rel = relocs; rel < relend; rel ++)
  2308. {
  2309. reloc_howto_type *howto;
  2310. unsigned long r_symndx;
  2311. Elf_Internal_Sym *sym;
  2312. asection *sec;
  2313. struct elf_link_hash_entry *h;
  2314. bfd_vma relocation;
  2315. bfd_reloc_status_type r;
  2316. const char * name = NULL;
  2317. int r_type;
  2318. asection *osec;
  2319. struct bfinfdpic_relocs_info *picrel;
  2320. bfd_vma orig_addend = rel->r_addend;
  2321. r_type = ELF32_R_TYPE (rel->r_info);
  2322. if (r_type == R_BFIN_GNU_VTINHERIT
  2323. || r_type == R_BFIN_GNU_VTENTRY)
  2324. continue;
  2325. r_symndx = ELF32_R_SYM (rel->r_info);
  2326. howto = bfin_reloc_type_lookup (input_bfd, r_type);
  2327. if (howto == NULL)
  2328. {
  2329. bfd_set_error (bfd_error_bad_value);
  2330. return FALSE;
  2331. }
  2332. h = NULL;
  2333. sym = NULL;
  2334. sec = NULL;
  2335. if (r_symndx < symtab_hdr->sh_info)
  2336. {
  2337. sym = local_syms + r_symndx;
  2338. osec = sec = local_sections [r_symndx];
  2339. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  2340. name = bfd_elf_string_from_elf_section
  2341. (input_bfd, symtab_hdr->sh_link, sym->st_name);
  2342. name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
  2343. }
  2344. else
  2345. {
  2346. bfd_boolean warned, ignored;
  2347. bfd_boolean unresolved_reloc;
  2348. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  2349. r_symndx, symtab_hdr, sym_hashes,
  2350. h, sec, relocation,
  2351. unresolved_reloc, warned, ignored);
  2352. osec = sec;
  2353. }
  2354. if (sec != NULL && discarded_section (sec))
  2355. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  2356. rel, 1, relend, howto, 0, contents);
  2357. if (bfd_link_relocatable (info))
  2358. continue;
  2359. if (h != NULL
  2360. && (h->root.type == bfd_link_hash_defined
  2361. || h->root.type == bfd_link_hash_defweak)
  2362. && !BFINFDPIC_SYM_LOCAL (info, h))
  2363. {
  2364. osec = sec = NULL;
  2365. relocation = 0;
  2366. }
  2367. switch (r_type)
  2368. {
  2369. case R_BFIN_PCREL24:
  2370. case R_BFIN_PCREL24_JUMP_L:
  2371. case R_BFIN_BYTE4_DATA:
  2372. if (! IS_FDPIC (output_bfd))
  2373. goto non_fdpic;
  2374. case R_BFIN_GOT17M4:
  2375. case R_BFIN_GOTHI:
  2376. case R_BFIN_GOTLO:
  2377. case R_BFIN_FUNCDESC_GOT17M4:
  2378. case R_BFIN_FUNCDESC_GOTHI:
  2379. case R_BFIN_FUNCDESC_GOTLO:
  2380. case R_BFIN_GOTOFF17M4:
  2381. case R_BFIN_GOTOFFHI:
  2382. case R_BFIN_GOTOFFLO:
  2383. case R_BFIN_FUNCDESC_GOTOFF17M4:
  2384. case R_BFIN_FUNCDESC_GOTOFFHI:
  2385. case R_BFIN_FUNCDESC_GOTOFFLO:
  2386. case R_BFIN_FUNCDESC:
  2387. case R_BFIN_FUNCDESC_VALUE:
  2388. if (h != NULL)
  2389. picrel = bfinfdpic_relocs_info_for_global (bfinfdpic_relocs_info
  2390. (info), input_bfd, h,
  2391. orig_addend, INSERT);
  2392. else
  2393. /* In order to find the entry we created before, we must
  2394. use the original addend, not the one that may have been
  2395. modified by _bfd_elf_rela_local_sym(). */
  2396. picrel = bfinfdpic_relocs_info_for_local (bfinfdpic_relocs_info
  2397. (info), input_bfd, r_symndx,
  2398. orig_addend, INSERT);
  2399. if (! picrel)
  2400. return FALSE;
  2401. if (!_bfinfdpic_emit_got_relocs_plt_entries (picrel, output_bfd, info,
  2402. osec, sym,
  2403. rel->r_addend))
  2404. {
  2405. (*_bfd_error_handler)
  2406. (_("%B: relocation at `%A+0x%x' references symbol `%s' with nonzero addend"),
  2407. input_bfd, input_section, rel->r_offset, name);
  2408. return FALSE;
  2409. }
  2410. break;
  2411. default:
  2412. non_fdpic:
  2413. picrel = NULL;
  2414. if (h && ! BFINFDPIC_SYM_LOCAL (info, h)
  2415. && _bfd_elf_section_offset (output_bfd, info, input_section,
  2416. rel->r_offset) != (bfd_vma) -1)
  2417. {
  2418. info->callbacks->warning
  2419. (info, _("relocation references symbol not defined in the module"),
  2420. name, input_bfd, input_section, rel->r_offset);
  2421. return FALSE;
  2422. }
  2423. break;
  2424. }
  2425. switch (r_type)
  2426. {
  2427. case R_BFIN_PCREL24:
  2428. case R_BFIN_PCREL24_JUMP_L:
  2429. check_segment[0] = isec_segment;
  2430. if (! IS_FDPIC (output_bfd))
  2431. check_segment[1] = isec_segment;
  2432. else if (picrel->plt)
  2433. {
  2434. relocation = bfinfdpic_plt_section (info)->output_section->vma
  2435. + bfinfdpic_plt_section (info)->output_offset
  2436. + picrel->plt_entry;
  2437. check_segment[1] = plt_segment;
  2438. }
  2439. /* We don't want to warn on calls to undefined weak symbols,
  2440. as calls to them must be protected by non-NULL tests
  2441. anyway, and unprotected calls would invoke undefined
  2442. behavior. */
  2443. else if (picrel->symndx == -1
  2444. && picrel->d.h->root.type == bfd_link_hash_undefweak)
  2445. check_segment[1] = check_segment[0];
  2446. else
  2447. check_segment[1] = sec
  2448. ? _bfinfdpic_osec_to_segment (output_bfd, sec->output_section)
  2449. : (unsigned)-1;
  2450. break;
  2451. case R_BFIN_GOT17M4:
  2452. case R_BFIN_GOTHI:
  2453. case R_BFIN_GOTLO:
  2454. relocation = picrel->got_entry;
  2455. check_segment[0] = check_segment[1] = got_segment;
  2456. break;
  2457. case R_BFIN_FUNCDESC_GOT17M4:
  2458. case R_BFIN_FUNCDESC_GOTHI:
  2459. case R_BFIN_FUNCDESC_GOTLO:
  2460. relocation = picrel->fdgot_entry;
  2461. check_segment[0] = check_segment[1] = got_segment;
  2462. break;
  2463. case R_BFIN_GOTOFFHI:
  2464. case R_BFIN_GOTOFF17M4:
  2465. case R_BFIN_GOTOFFLO:
  2466. relocation -= bfinfdpic_got_section (info)->output_section->vma
  2467. + bfinfdpic_got_section (info)->output_offset
  2468. + bfinfdpic_got_initial_offset (info);
  2469. check_segment[0] = got_segment;
  2470. check_segment[1] = sec
  2471. ? _bfinfdpic_osec_to_segment (output_bfd, sec->output_section)
  2472. : (unsigned)-1;
  2473. break;
  2474. case R_BFIN_FUNCDESC_GOTOFF17M4:
  2475. case R_BFIN_FUNCDESC_GOTOFFHI:
  2476. case R_BFIN_FUNCDESC_GOTOFFLO:
  2477. relocation = picrel->fd_entry;
  2478. check_segment[0] = check_segment[1] = got_segment;
  2479. break;
  2480. case R_BFIN_FUNCDESC:
  2481. {
  2482. int dynindx;
  2483. bfd_vma addend = rel->r_addend;
  2484. if (! (h && h->root.type == bfd_link_hash_undefweak
  2485. && BFINFDPIC_SYM_LOCAL (info, h)))
  2486. {
  2487. /* If the symbol is dynamic and there may be dynamic
  2488. symbol resolution because we are or are linked with a
  2489. shared library, emit a FUNCDESC relocation such that
  2490. the dynamic linker will allocate the function
  2491. descriptor. If the symbol needs a non-local function
  2492. descriptor but binds locally (e.g., its visibility is
  2493. protected, emit a dynamic relocation decayed to
  2494. section+offset. */
  2495. if (h && ! BFINFDPIC_FUNCDESC_LOCAL (info, h)
  2496. && BFINFDPIC_SYM_LOCAL (info, h)
  2497. && !bfd_link_pde (info))
  2498. {
  2499. dynindx = elf_section_data (h->root.u.def.section
  2500. ->output_section)->dynindx;
  2501. addend += h->root.u.def.section->output_offset
  2502. + h->root.u.def.value;
  2503. }
  2504. else if (h && ! BFINFDPIC_FUNCDESC_LOCAL (info, h))
  2505. {
  2506. if (addend)
  2507. {
  2508. info->callbacks->warning
  2509. (info, _("R_BFIN_FUNCDESC references dynamic symbol with nonzero addend"),
  2510. name, input_bfd, input_section, rel->r_offset);
  2511. return FALSE;
  2512. }
  2513. dynindx = h->dynindx;
  2514. }
  2515. else
  2516. {
  2517. /* Otherwise, we know we have a private function
  2518. descriptor, so reference it directly. */
  2519. BFD_ASSERT (picrel->privfd);
  2520. r_type = R_BFIN_BYTE4_DATA;
  2521. dynindx = elf_section_data (bfinfdpic_got_section (info)
  2522. ->output_section)->dynindx;
  2523. addend = bfinfdpic_got_section (info)->output_offset
  2524. + bfinfdpic_got_initial_offset (info)
  2525. + picrel->fd_entry;
  2526. }
  2527. /* If there is room for dynamic symbol resolution, emit
  2528. the dynamic relocation. However, if we're linking an
  2529. executable at a fixed location, we won't have emitted a
  2530. dynamic symbol entry for the got section, so idx will
  2531. be zero, which means we can and should compute the
  2532. address of the private descriptor ourselves. */
  2533. if (bfd_link_pde (info)
  2534. && (!h || BFINFDPIC_FUNCDESC_LOCAL (info, h)))
  2535. {
  2536. bfd_vma offset;
  2537. addend += bfinfdpic_got_section (info)->output_section->vma;
  2538. if ((bfd_get_section_flags (output_bfd,
  2539. input_section->output_section)
  2540. & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
  2541. {
  2542. if (_bfinfdpic_osec_readonly_p (output_bfd,
  2543. input_section
  2544. ->output_section))
  2545. {
  2546. info->callbacks->warning
  2547. (info,
  2548. _("cannot emit fixups in read-only section"),
  2549. name, input_bfd, input_section, rel->r_offset);
  2550. return FALSE;
  2551. }
  2552. offset = _bfd_elf_section_offset
  2553. (output_bfd, info,
  2554. input_section, rel->r_offset);
  2555. if (offset != (bfd_vma)-1)
  2556. _bfinfdpic_add_rofixup (output_bfd,
  2557. bfinfdpic_gotfixup_section
  2558. (info),
  2559. offset + input_section
  2560. ->output_section->vma
  2561. + input_section->output_offset,
  2562. picrel);
  2563. }
  2564. }
  2565. else if ((bfd_get_section_flags (output_bfd,
  2566. input_section->output_section)
  2567. & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
  2568. {
  2569. bfd_vma offset;
  2570. if (_bfinfdpic_osec_readonly_p (output_bfd,
  2571. input_section
  2572. ->output_section))
  2573. {
  2574. info->callbacks->warning
  2575. (info,
  2576. _("cannot emit dynamic relocations in read-only section"),
  2577. name, input_bfd, input_section, rel->r_offset);
  2578. return FALSE;
  2579. }
  2580. offset = _bfd_elf_section_offset (output_bfd, info,
  2581. input_section, rel->r_offset);
  2582. if (offset != (bfd_vma)-1)
  2583. _bfinfdpic_add_dyn_reloc (output_bfd,
  2584. bfinfdpic_gotrel_section (info),
  2585. offset + input_section
  2586. ->output_section->vma
  2587. + input_section->output_offset,
  2588. r_type,
  2589. dynindx, addend, picrel);
  2590. }
  2591. else
  2592. addend += bfinfdpic_got_section (info)->output_section->vma;
  2593. }
  2594. /* We want the addend in-place because dynamic
  2595. relocations are REL. Setting relocation to it should
  2596. arrange for it to be installed. */
  2597. relocation = addend - rel->r_addend;
  2598. }
  2599. check_segment[0] = check_segment[1] = got_segment;
  2600. break;
  2601. case R_BFIN_BYTE4_DATA:
  2602. if (! IS_FDPIC (output_bfd))
  2603. {
  2604. check_segment[0] = check_segment[1] = -1;
  2605. break;
  2606. }
  2607. /* Fall through. */
  2608. case R_BFIN_FUNCDESC_VALUE:
  2609. {
  2610. int dynindx;
  2611. bfd_vma addend = rel->r_addend;
  2612. bfd_vma offset;
  2613. offset = _bfd_elf_section_offset (output_bfd, info,
  2614. input_section, rel->r_offset);
  2615. /* If the symbol is dynamic but binds locally, use
  2616. section+offset. */
  2617. if (h && ! BFINFDPIC_SYM_LOCAL (info, h))
  2618. {
  2619. if (addend && r_type == R_BFIN_FUNCDESC_VALUE)
  2620. {
  2621. info->callbacks->warning
  2622. (info, _("R_BFIN_FUNCDESC_VALUE references dynamic symbol with nonzero addend"),
  2623. name, input_bfd, input_section, rel->r_offset);
  2624. return FALSE;
  2625. }
  2626. dynindx = h->dynindx;
  2627. }
  2628. else
  2629. {
  2630. if (h)
  2631. addend += h->root.u.def.value;
  2632. else
  2633. addend += sym->st_value;
  2634. if (osec)
  2635. addend += osec->output_offset;
  2636. if (osec && osec->output_section
  2637. && ! bfd_is_abs_section (osec->output_section)
  2638. && ! bfd_is_und_section (osec->output_section))
  2639. dynindx = elf_section_data (osec->output_section)->dynindx;
  2640. else
  2641. dynindx = 0;
  2642. }
  2643. /* If we're linking an executable at a fixed address, we
  2644. can omit the dynamic relocation as long as the symbol
  2645. is defined in the current link unit (which is implied
  2646. by its output section not being NULL). */
  2647. if (bfd_link_pde (info)
  2648. && (!h || BFINFDPIC_SYM_LOCAL (info, h)))
  2649. {
  2650. if (osec)
  2651. addend += osec->output_section->vma;
  2652. if (IS_FDPIC (input_bfd)
  2653. && (bfd_get_section_flags (output_bfd,
  2654. input_section->output_section)
  2655. & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
  2656. {
  2657. if (_bfinfdpic_osec_readonly_p (output_bfd,
  2658. input_section
  2659. ->output_section))
  2660. {
  2661. info->callbacks->warning
  2662. (info,
  2663. _("cannot emit fixups in read-only section"),
  2664. name, input_bfd, input_section, rel->r_offset);
  2665. return FALSE;
  2666. }
  2667. if (!h || h->root.type != bfd_link_hash_undefweak)
  2668. {
  2669. if (offset != (bfd_vma)-1)
  2670. {
  2671. _bfinfdpic_add_rofixup (output_bfd,
  2672. bfinfdpic_gotfixup_section
  2673. (info),
  2674. offset + input_section
  2675. ->output_section->vma
  2676. + input_section->output_offset,
  2677. picrel);
  2678. if (r_type == R_BFIN_FUNCDESC_VALUE)
  2679. _bfinfdpic_add_rofixup
  2680. (output_bfd,
  2681. bfinfdpic_gotfixup_section (info),
  2682. offset + input_section->output_section->vma
  2683. + input_section->output_offset + 4, picrel);
  2684. }
  2685. }
  2686. }
  2687. }
  2688. else
  2689. {
  2690. if ((bfd_get_section_flags (output_bfd,
  2691. input_section->output_section)
  2692. & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
  2693. {
  2694. if (_bfinfdpic_osec_readonly_p (output_bfd,
  2695. input_section
  2696. ->output_section))
  2697. {
  2698. info->callbacks->warning
  2699. (info,
  2700. _("cannot emit dynamic relocations in read-only section"),
  2701. name, input_bfd, input_section, rel->r_offset);
  2702. return FALSE;
  2703. }
  2704. if (offset != (bfd_vma)-1)
  2705. _bfinfdpic_add_dyn_reloc (output_bfd,
  2706. bfinfdpic_gotrel_section (info),
  2707. offset
  2708. + input_section->output_section->vma
  2709. + input_section->output_offset,
  2710. r_type, dynindx, addend, picrel);
  2711. }
  2712. else if (osec)
  2713. addend += osec->output_section->vma;
  2714. /* We want the addend in-place because dynamic
  2715. relocations are REL. Setting relocation to it
  2716. should arrange for it to be installed. */
  2717. relocation = addend - rel->r_addend;
  2718. }
  2719. if (r_type == R_BFIN_FUNCDESC_VALUE)
  2720. {
  2721. /* If we've omitted the dynamic relocation, just emit
  2722. the fixed addresses of the symbol and of the local
  2723. GOT base offset. */
  2724. if (bfd_link_pde (info)
  2725. && (!h || BFINFDPIC_SYM_LOCAL (info, h)))
  2726. bfd_put_32 (output_bfd,
  2727. bfinfdpic_got_section (info)->output_section->vma
  2728. + bfinfdpic_got_section (info)->output_offset
  2729. + bfinfdpic_got_initial_offset (info),
  2730. contents + rel->r_offset + 4);
  2731. else
  2732. /* A function descriptor used for lazy or local
  2733. resolving is initialized such that its high word
  2734. contains the output section index in which the
  2735. PLT entries are located, and the low word
  2736. contains the offset of the lazy PLT entry entry
  2737. point into that section. */
  2738. bfd_put_32 (output_bfd,
  2739. h && ! BFINFDPIC_SYM_LOCAL (info, h)
  2740. ? 0
  2741. : _bfinfdpic_osec_to_segment (output_bfd,
  2742. sec
  2743. ->output_section),
  2744. contents + rel->r_offset + 4);
  2745. }
  2746. }
  2747. check_segment[0] = check_segment[1] = got_segment;
  2748. break;
  2749. default:
  2750. check_segment[0] = isec_segment;
  2751. check_segment[1] = sec
  2752. ? _bfinfdpic_osec_to_segment (output_bfd, sec->output_section)
  2753. : (unsigned)-1;
  2754. break;
  2755. }
  2756. if (check_segment[0] != check_segment[1] && IS_FDPIC (output_bfd))
  2757. {
  2758. #if 1 /* If you take this out, remove the #error from fdpic-static-6.d
  2759. in the ld testsuite. */
  2760. /* This helps catch problems in GCC while we can't do more
  2761. than static linking. The idea is to test whether the
  2762. input file basename is crt0.o only once. */
  2763. if (silence_segment_error == 1)
  2764. silence_segment_error =
  2765. (strlen (input_bfd->filename) == 6
  2766. && filename_cmp (input_bfd->filename, "crt0.o") == 0)
  2767. || (strlen (input_bfd->filename) > 6
  2768. && filename_cmp (input_bfd->filename
  2769. + strlen (input_bfd->filename) - 7,
  2770. "/crt0.o") == 0)
  2771. ? -1 : 0;
  2772. #endif
  2773. if (!silence_segment_error
  2774. /* We don't want duplicate errors for undefined
  2775. symbols. */
  2776. && !(picrel && picrel->symndx == -1
  2777. && picrel->d.h->root.type == bfd_link_hash_undefined))
  2778. info->callbacks->warning
  2779. (info,
  2780. bfd_link_pic (info)
  2781. ? _("relocations between different segments are not supported")
  2782. : _("warning: relocation references a different segment"),
  2783. name, input_bfd, input_section, rel->r_offset);
  2784. if (!silence_segment_error && bfd_link_pic (info))
  2785. return FALSE;
  2786. elf_elfheader (output_bfd)->e_flags |= EF_BFIN_PIC;
  2787. }
  2788. switch (r_type)
  2789. {
  2790. case R_BFIN_GOTOFFHI:
  2791. /* We need the addend to be applied before we shift the
  2792. value right. */
  2793. relocation += rel->r_addend;
  2794. /* Fall through. */
  2795. case R_BFIN_GOTHI:
  2796. case R_BFIN_FUNCDESC_GOTHI:
  2797. case R_BFIN_FUNCDESC_GOTOFFHI:
  2798. relocation >>= 16;
  2799. /* Fall through. */
  2800. case R_BFIN_GOTLO:
  2801. case R_BFIN_FUNCDESC_GOTLO:
  2802. case R_BFIN_GOTOFFLO:
  2803. case R_BFIN_FUNCDESC_GOTOFFLO:
  2804. relocation &= 0xffff;
  2805. break;
  2806. default:
  2807. break;
  2808. }
  2809. switch (r_type)
  2810. {
  2811. case R_BFIN_PCREL24:
  2812. case R_BFIN_PCREL24_JUMP_L:
  2813. if (! IS_FDPIC (output_bfd) || ! picrel->plt)
  2814. break;
  2815. /* Fall through. */
  2816. /* When referencing a GOT entry, a function descriptor or a
  2817. PLT, we don't want the addend to apply to the reference,
  2818. but rather to the referenced symbol. The actual entry
  2819. will have already been created taking the addend into
  2820. account, so cancel it out here. */
  2821. case R_BFIN_GOT17M4:
  2822. case R_BFIN_GOTHI:
  2823. case R_BFIN_GOTLO:
  2824. case R_BFIN_FUNCDESC_GOT17M4:
  2825. case R_BFIN_FUNCDESC_GOTHI:
  2826. case R_BFIN_FUNCDESC_GOTLO:
  2827. case R_BFIN_FUNCDESC_GOTOFF17M4:
  2828. case R_BFIN_FUNCDESC_GOTOFFHI:
  2829. case R_BFIN_FUNCDESC_GOTOFFLO:
  2830. /* Note that we only want GOTOFFHI, not GOTOFFLO or GOTOFF17M4
  2831. here, since we do want to apply the addend to the others.
  2832. Note that we've applied the addend to GOTOFFHI before we
  2833. shifted it right. */
  2834. case R_BFIN_GOTOFFHI:
  2835. relocation -= rel->r_addend;
  2836. break;
  2837. default:
  2838. break;
  2839. }
  2840. r = bfin_final_link_relocate (rel, howto, input_bfd, input_section,
  2841. contents, rel->r_offset,
  2842. relocation, rel->r_addend);
  2843. if (r != bfd_reloc_ok)
  2844. {
  2845. const char * msg = (const char *) NULL;
  2846. switch (r)
  2847. {
  2848. case bfd_reloc_overflow:
  2849. r = info->callbacks->reloc_overflow
  2850. (info, (h ? &h->root : NULL), name, howto->name,
  2851. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  2852. break;
  2853. case bfd_reloc_undefined:
  2854. r = info->callbacks->undefined_symbol
  2855. (info, name, input_bfd, input_section, rel->r_offset, TRUE);
  2856. break;
  2857. case bfd_reloc_outofrange:
  2858. msg = _("internal error: out of range error");
  2859. break;
  2860. case bfd_reloc_notsupported:
  2861. msg = _("internal error: unsupported relocation error");
  2862. break;
  2863. case bfd_reloc_dangerous:
  2864. msg = _("internal error: dangerous relocation");
  2865. break;
  2866. default:
  2867. msg = _("internal error: unknown error");
  2868. break;
  2869. }
  2870. if (msg)
  2871. r = info->callbacks->warning
  2872. (info, msg, name, input_bfd, input_section, rel->r_offset);
  2873. if (! r)
  2874. return FALSE;
  2875. }
  2876. }
  2877. return TRUE;
  2878. }
  2879. /* Update the relocation information for the relocations of the section
  2880. being removed. */
  2881. static bfd_boolean
  2882. bfinfdpic_gc_sweep_hook (bfd *abfd,
  2883. struct bfd_link_info *info,
  2884. asection *sec,
  2885. const Elf_Internal_Rela *relocs)
  2886. {
  2887. Elf_Internal_Shdr *symtab_hdr;
  2888. struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
  2889. const Elf_Internal_Rela *rel;
  2890. const Elf_Internal_Rela *rel_end;
  2891. struct bfinfdpic_relocs_info *picrel;
  2892. BFD_ASSERT (IS_FDPIC (abfd));
  2893. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2894. sym_hashes = elf_sym_hashes (abfd);
  2895. sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
  2896. if (!elf_bad_symtab (abfd))
  2897. sym_hashes_end -= symtab_hdr->sh_info;
  2898. rel_end = relocs + sec->reloc_count;
  2899. for (rel = relocs; rel < rel_end; rel++)
  2900. {
  2901. struct elf_link_hash_entry *h;
  2902. unsigned long r_symndx;
  2903. r_symndx = ELF32_R_SYM (rel->r_info);
  2904. if (r_symndx < symtab_hdr->sh_info)
  2905. h = NULL;
  2906. else
  2907. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  2908. if (h != NULL)
  2909. picrel = bfinfdpic_relocs_info_for_global (bfinfdpic_relocs_info (info),
  2910. abfd, h,
  2911. rel->r_addend, NO_INSERT);
  2912. else
  2913. picrel = bfinfdpic_relocs_info_for_local (bfinfdpic_relocs_info
  2914. (info), abfd, r_symndx,
  2915. rel->r_addend, NO_INSERT);
  2916. if (!picrel)
  2917. return TRUE;
  2918. switch (ELF32_R_TYPE (rel->r_info))
  2919. {
  2920. case R_BFIN_PCREL24:
  2921. case R_BFIN_PCREL24_JUMP_L:
  2922. picrel->call--;
  2923. break;
  2924. case R_BFIN_FUNCDESC_VALUE:
  2925. picrel->relocsfdv--;
  2926. if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
  2927. picrel->relocs32++;
  2928. /* Fall through. */
  2929. case R_BFIN_BYTE4_DATA:
  2930. picrel->sym--;
  2931. if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
  2932. picrel->relocs32--;
  2933. break;
  2934. case R_BFIN_GOT17M4:
  2935. picrel->got17m4--;
  2936. break;
  2937. case R_BFIN_GOTHI:
  2938. case R_BFIN_GOTLO:
  2939. picrel->gothilo--;
  2940. break;
  2941. case R_BFIN_FUNCDESC_GOT17M4:
  2942. picrel->fdgot17m4--;
  2943. break;
  2944. case R_BFIN_FUNCDESC_GOTHI:
  2945. case R_BFIN_FUNCDESC_GOTLO:
  2946. picrel->fdgothilo--;
  2947. break;
  2948. case R_BFIN_GOTOFF17M4:
  2949. case R_BFIN_GOTOFFHI:
  2950. case R_BFIN_GOTOFFLO:
  2951. picrel->gotoff--;
  2952. break;
  2953. case R_BFIN_FUNCDESC_GOTOFF17M4:
  2954. picrel->fdgoff17m4--;
  2955. break;
  2956. case R_BFIN_FUNCDESC_GOTOFFHI:
  2957. case R_BFIN_FUNCDESC_GOTOFFLO:
  2958. picrel->fdgoffhilo--;
  2959. break;
  2960. case R_BFIN_FUNCDESC:
  2961. picrel->fd--;
  2962. picrel->relocsfd--;
  2963. break;
  2964. default:
  2965. break;
  2966. }
  2967. }
  2968. return TRUE;
  2969. }
  2970. /* We need dynamic symbols for every section, since segments can
  2971. relocate independently. */
  2972. static bfd_boolean
  2973. _bfinfdpic_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
  2974. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2975. asection *p)
  2976. {
  2977. switch (elf_section_data (p)->this_hdr.sh_type)
  2978. {
  2979. case SHT_PROGBITS:
  2980. case SHT_NOBITS:
  2981. /* If sh_type is yet undecided, assume it could be
  2982. SHT_PROGBITS/SHT_NOBITS. */
  2983. case SHT_NULL:
  2984. return FALSE;
  2985. /* There shouldn't be section relative relocations
  2986. against any other section. */
  2987. default:
  2988. return TRUE;
  2989. }
  2990. }
  2991. /* Create a .got section, as well as its additional info field. This
  2992. is almost entirely copied from
  2993. elflink.c:_bfd_elf_create_got_section(). */
  2994. static bfd_boolean
  2995. _bfin_create_got_section (bfd *abfd, struct bfd_link_info *info)
  2996. {
  2997. flagword flags, pltflags;
  2998. asection *s;
  2999. struct elf_link_hash_entry *h;
  3000. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  3001. int ptralign;
  3002. /* This function may be called more than once. */
  3003. s = bfd_get_linker_section (abfd, ".got");
  3004. if (s != NULL)
  3005. return TRUE;
  3006. /* Machine specific: although pointers are 32-bits wide, we want the
  3007. GOT to be aligned to a 64-bit boundary, such that function
  3008. descriptors in it can be accessed with 64-bit loads and
  3009. stores. */
  3010. ptralign = 3;
  3011. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  3012. | SEC_LINKER_CREATED);
  3013. pltflags = flags;
  3014. s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
  3015. if (s == NULL
  3016. || !bfd_set_section_alignment (abfd, s, ptralign))
  3017. return FALSE;
  3018. if (bed->want_got_plt)
  3019. {
  3020. s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
  3021. if (s == NULL
  3022. || !bfd_set_section_alignment (abfd, s, ptralign))
  3023. return FALSE;
  3024. }
  3025. if (bed->want_got_sym)
  3026. {
  3027. /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
  3028. (or .got.plt) section. We don't do this in the linker script
  3029. because we don't want to define the symbol if we are not creating
  3030. a global offset table. */
  3031. h = _bfd_elf_define_linkage_sym (abfd, info, s, "__GLOBAL_OFFSET_TABLE_");
  3032. elf_hash_table (info)->hgot = h;
  3033. if (h == NULL)
  3034. return FALSE;
  3035. /* Machine-specific: we want the symbol for executables as
  3036. well. */
  3037. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  3038. return FALSE;
  3039. }
  3040. /* The first bit of the global offset table is the header. */
  3041. s->size += bed->got_header_size;
  3042. /* This is the machine-specific part. Create and initialize section
  3043. data for the got. */
  3044. if (IS_FDPIC (abfd))
  3045. {
  3046. bfinfdpic_got_section (info) = s;
  3047. bfinfdpic_relocs_info (info) = htab_try_create (1,
  3048. bfinfdpic_relocs_info_hash,
  3049. bfinfdpic_relocs_info_eq,
  3050. (htab_del) NULL);
  3051. if (! bfinfdpic_relocs_info (info))
  3052. return FALSE;
  3053. s = bfd_make_section_anyway_with_flags (abfd, ".rel.got",
  3054. (flags | SEC_READONLY));
  3055. if (s == NULL
  3056. || ! bfd_set_section_alignment (abfd, s, 2))
  3057. return FALSE;
  3058. bfinfdpic_gotrel_section (info) = s;
  3059. /* Machine-specific. */
  3060. s = bfd_make_section_anyway_with_flags (abfd, ".rofixup",
  3061. (flags | SEC_READONLY));
  3062. if (s == NULL
  3063. || ! bfd_set_section_alignment (abfd, s, 2))
  3064. return FALSE;
  3065. bfinfdpic_gotfixup_section (info) = s;
  3066. }
  3067. pltflags |= SEC_CODE;
  3068. if (bed->plt_not_loaded)
  3069. pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
  3070. if (bed->plt_readonly)
  3071. pltflags |= SEC_READONLY;
  3072. s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
  3073. if (s == NULL
  3074. || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
  3075. return FALSE;
  3076. /* Blackfin-specific: remember it. */
  3077. bfinfdpic_plt_section (info) = s;
  3078. if (bed->want_plt_sym)
  3079. {
  3080. /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
  3081. .plt section. */
  3082. struct bfd_link_hash_entry *bh = NULL;
  3083. if (! (_bfd_generic_link_add_one_symbol
  3084. (info, abfd, "__PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
  3085. FALSE, get_elf_backend_data (abfd)->collect, &bh)))
  3086. return FALSE;
  3087. h = (struct elf_link_hash_entry *) bh;
  3088. h->def_regular = 1;
  3089. h->type = STT_OBJECT;
  3090. if (! bfd_link_executable (info)
  3091. && ! bfd_elf_link_record_dynamic_symbol (info, h))
  3092. return FALSE;
  3093. }
  3094. /* Blackfin-specific: we want rel relocations for the plt. */
  3095. s = bfd_make_section_anyway_with_flags (abfd, ".rel.plt",
  3096. flags | SEC_READONLY);
  3097. if (s == NULL
  3098. || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  3099. return FALSE;
  3100. /* Blackfin-specific: remember it. */
  3101. bfinfdpic_pltrel_section (info) = s;
  3102. return TRUE;
  3103. }
  3104. /* Make sure the got and plt sections exist, and that our pointers in
  3105. the link hash table point to them. */
  3106. static bfd_boolean
  3107. elf32_bfinfdpic_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  3108. {
  3109. /* This is mostly copied from
  3110. elflink.c:_bfd_elf_create_dynamic_sections(). */
  3111. flagword flags;
  3112. asection *s;
  3113. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  3114. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  3115. | SEC_LINKER_CREATED);
  3116. /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
  3117. .rel[a].bss sections. */
  3118. /* Blackfin-specific: we want to create the GOT in the Blackfin way. */
  3119. if (! _bfin_create_got_section (abfd, info))
  3120. return FALSE;
  3121. /* Blackfin-specific: make sure we created everything we wanted. */
  3122. BFD_ASSERT (bfinfdpic_got_section (info) && bfinfdpic_gotrel_section (info)
  3123. /* && bfinfdpic_gotfixup_section (info) */
  3124. && bfinfdpic_plt_section (info)
  3125. && bfinfdpic_pltrel_section (info));
  3126. if (bed->want_dynbss)
  3127. {
  3128. /* The .dynbss section is a place to put symbols which are defined
  3129. by dynamic objects, are referenced by regular objects, and are
  3130. not functions. We must allocate space for them in the process
  3131. image and use a R_*_COPY reloc to tell the dynamic linker to
  3132. initialize them at run time. The linker script puts the .dynbss
  3133. section into the .bss section of the final image. */
  3134. s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
  3135. SEC_ALLOC | SEC_LINKER_CREATED);
  3136. if (s == NULL)
  3137. return FALSE;
  3138. /* The .rel[a].bss section holds copy relocs. This section is not
  3139. normally needed. We need to create it here, though, so that the
  3140. linker will map it to an output section. We can't just create it
  3141. only if we need it, because we will not know whether we need it
  3142. until we have seen all the input files, and the first time the
  3143. main linker code calls BFD after examining all the input files
  3144. (size_dynamic_sections) the input sections have already been
  3145. mapped to the output sections. If the section turns out not to
  3146. be needed, we can discard it later. We will never need this
  3147. section when generating a shared object, since they do not use
  3148. copy relocs. */
  3149. if (! bfd_link_pic (info))
  3150. {
  3151. s = bfd_make_section_anyway_with_flags (abfd,
  3152. ".rela.bss",
  3153. flags | SEC_READONLY);
  3154. if (s == NULL
  3155. || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
  3156. return FALSE;
  3157. }
  3158. }
  3159. return TRUE;
  3160. }
  3161. /* Compute the total GOT size required by each symbol in each range.
  3162. Symbols may require up to 4 words in the GOT: an entry pointing to
  3163. the symbol, an entry pointing to its function descriptor, and a
  3164. private function descriptors taking two words. */
  3165. static void
  3166. _bfinfdpic_count_nontls_entries (struct bfinfdpic_relocs_info *entry,
  3167. struct _bfinfdpic_dynamic_got_info *dinfo)
  3168. {
  3169. /* Allocate space for a GOT entry pointing to the symbol. */
  3170. if (entry->got17m4)
  3171. dinfo->got17m4 += 4;
  3172. else if (entry->gothilo)
  3173. dinfo->gothilo += 4;
  3174. else
  3175. entry->relocs32--;
  3176. entry->relocs32++;
  3177. /* Allocate space for a GOT entry pointing to the function
  3178. descriptor. */
  3179. if (entry->fdgot17m4)
  3180. dinfo->got17m4 += 4;
  3181. else if (entry->fdgothilo)
  3182. dinfo->gothilo += 4;
  3183. else
  3184. entry->relocsfd--;
  3185. entry->relocsfd++;
  3186. /* Decide whether we need a PLT entry, a function descriptor in the
  3187. GOT, and a lazy PLT entry for this symbol. */
  3188. entry->plt = entry->call
  3189. && entry->symndx == -1 && ! BFINFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
  3190. && elf_hash_table (dinfo->info)->dynamic_sections_created;
  3191. entry->privfd = entry->plt
  3192. || entry->fdgoff17m4 || entry->fdgoffhilo
  3193. || ((entry->fd || entry->fdgot17m4 || entry->fdgothilo)
  3194. && (entry->symndx != -1
  3195. || BFINFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h)));
  3196. entry->lazyplt = entry->privfd
  3197. && entry->symndx == -1 && ! BFINFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
  3198. && ! (dinfo->info->flags & DF_BIND_NOW)
  3199. && elf_hash_table (dinfo->info)->dynamic_sections_created;
  3200. /* Allocate space for a function descriptor. */
  3201. if (entry->fdgoff17m4)
  3202. dinfo->fd17m4 += 8;
  3203. else if (entry->privfd && entry->plt)
  3204. dinfo->fdplt += 8;
  3205. else if (entry->privfd)
  3206. dinfo->fdhilo += 8;
  3207. else
  3208. entry->relocsfdv--;
  3209. entry->relocsfdv++;
  3210. if (entry->lazyplt)
  3211. dinfo->lzplt += LZPLT_NORMAL_SIZE;
  3212. }
  3213. /* Compute the number of dynamic relocations and fixups that a symbol
  3214. requires, and add (or subtract) from the grand and per-symbol
  3215. totals. */
  3216. static void
  3217. _bfinfdpic_count_relocs_fixups (struct bfinfdpic_relocs_info *entry,
  3218. struct _bfinfdpic_dynamic_got_info *dinfo,
  3219. bfd_boolean subtract)
  3220. {
  3221. bfd_vma relocs = 0, fixups = 0;
  3222. if (!bfd_link_pde (dinfo->info))
  3223. relocs = entry->relocs32 + entry->relocsfd + entry->relocsfdv;
  3224. else
  3225. {
  3226. if (entry->symndx != -1 || BFINFDPIC_SYM_LOCAL (dinfo->info, entry->d.h))
  3227. {
  3228. if (entry->symndx != -1
  3229. || entry->d.h->root.type != bfd_link_hash_undefweak)
  3230. fixups += entry->relocs32 + 2 * entry->relocsfdv;
  3231. }
  3232. else
  3233. relocs += entry->relocs32 + entry->relocsfdv;
  3234. if (entry->symndx != -1
  3235. || BFINFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h))
  3236. {
  3237. if (entry->symndx != -1
  3238. || entry->d.h->root.type != bfd_link_hash_undefweak)
  3239. fixups += entry->relocsfd;
  3240. }
  3241. else
  3242. relocs += entry->relocsfd;
  3243. }
  3244. if (subtract)
  3245. {
  3246. relocs = - relocs;
  3247. fixups = - fixups;
  3248. }
  3249. entry->dynrelocs += relocs;
  3250. entry->fixups += fixups;
  3251. dinfo->relocs += relocs;
  3252. dinfo->fixups += fixups;
  3253. }
  3254. /* Compute the total GOT and PLT size required by each symbol in each range. *
  3255. Symbols may require up to 4 words in the GOT: an entry pointing to
  3256. the symbol, an entry pointing to its function descriptor, and a
  3257. private function descriptors taking two words. */
  3258. static int
  3259. _bfinfdpic_count_got_plt_entries (void **entryp, void *dinfo_)
  3260. {
  3261. struct bfinfdpic_relocs_info *entry = *entryp;
  3262. struct _bfinfdpic_dynamic_got_info *dinfo = dinfo_;
  3263. _bfinfdpic_count_nontls_entries (entry, dinfo);
  3264. _bfinfdpic_count_relocs_fixups (entry, dinfo, FALSE);
  3265. return 1;
  3266. }
  3267. /* This structure is used to assign offsets to got entries, function
  3268. descriptors, plt entries and lazy plt entries. */
  3269. struct _bfinfdpic_dynamic_got_plt_info
  3270. {
  3271. /* Summary information collected with _bfinfdpic_count_got_plt_entries. */
  3272. struct _bfinfdpic_dynamic_got_info g;
  3273. /* For each addressable range, we record a MAX (positive) and MIN
  3274. (negative) value. CUR is used to assign got entries, and it's
  3275. incremented from an initial positive value to MAX, then from MIN
  3276. to FDCUR (unless FDCUR wraps around first). FDCUR is used to
  3277. assign function descriptors, and it's decreased from an initial
  3278. non-positive value to MIN, then from MAX down to CUR (unless CUR
  3279. wraps around first). All of MIN, MAX, CUR and FDCUR always point
  3280. to even words. ODD, if non-zero, indicates an odd word to be
  3281. used for the next got entry, otherwise CUR is used and
  3282. incremented by a pair of words, wrapping around when it reaches
  3283. MAX. FDCUR is decremented (and wrapped) before the next function
  3284. descriptor is chosen. FDPLT indicates the number of remaining
  3285. slots that can be used for function descriptors used only by PLT
  3286. entries. */
  3287. struct _bfinfdpic_dynamic_got_alloc_data
  3288. {
  3289. bfd_signed_vma max, cur, odd, fdcur, min;
  3290. bfd_vma fdplt;
  3291. } got17m4, gothilo;
  3292. };
  3293. /* Determine the positive and negative ranges to be used by each
  3294. offset range in the GOT. FDCUR and CUR, that must be aligned to a
  3295. double-word boundary, are the minimum (negative) and maximum
  3296. (positive) GOT offsets already used by previous ranges, except for
  3297. an ODD entry that may have been left behind. GOT and FD indicate
  3298. the size of GOT entries and function descriptors that must be
  3299. placed within the range from -WRAP to WRAP. If there's room left,
  3300. up to FDPLT bytes should be reserved for additional function
  3301. descriptors. */
  3302. inline static bfd_signed_vma
  3303. _bfinfdpic_compute_got_alloc_data (struct _bfinfdpic_dynamic_got_alloc_data *gad,
  3304. bfd_signed_vma fdcur,
  3305. bfd_signed_vma odd,
  3306. bfd_signed_vma cur,
  3307. bfd_vma got,
  3308. bfd_vma fd,
  3309. bfd_vma fdplt,
  3310. bfd_vma wrap)
  3311. {
  3312. bfd_signed_vma wrapmin = -wrap;
  3313. /* Start at the given initial points. */
  3314. gad->fdcur = fdcur;
  3315. gad->cur = cur;
  3316. /* If we had an incoming odd word and we have any got entries that
  3317. are going to use it, consume it, otherwise leave gad->odd at
  3318. zero. We might force gad->odd to zero and return the incoming
  3319. odd such that it is used by the next range, but then GOT entries
  3320. might appear to be out of order and we wouldn't be able to
  3321. shorten the GOT by one word if it turns out to end with an
  3322. unpaired GOT entry. */
  3323. if (odd && got)
  3324. {
  3325. gad->odd = odd;
  3326. got -= 4;
  3327. odd = 0;
  3328. }
  3329. else
  3330. gad->odd = 0;
  3331. /* If we're left with an unpaired GOT entry, compute its location
  3332. such that we can return it. Otherwise, if got doesn't require an
  3333. odd number of words here, either odd was already zero in the
  3334. block above, or it was set to zero because got was non-zero, or
  3335. got was already zero. In the latter case, we want the value of
  3336. odd to carry over to the return statement, so we don't want to
  3337. reset odd unless the condition below is true. */
  3338. if (got & 4)
  3339. {
  3340. odd = cur + got;
  3341. got += 4;
  3342. }
  3343. /* Compute the tentative boundaries of this range. */
  3344. gad->max = cur + got;
  3345. gad->min = fdcur - fd;
  3346. gad->fdplt = 0;
  3347. /* If function descriptors took too much space, wrap some of them
  3348. around. */
  3349. if (gad->min < wrapmin)
  3350. {
  3351. gad->max += wrapmin - gad->min;
  3352. gad->min = wrapmin;
  3353. }
  3354. /* If there is space left and we have function descriptors
  3355. referenced in PLT entries that could take advantage of shorter
  3356. offsets, place them here. */
  3357. else if (fdplt && gad->min > wrapmin)
  3358. {
  3359. bfd_vma fds;
  3360. if ((bfd_vma) (gad->min - wrapmin) < fdplt)
  3361. fds = gad->min - wrapmin;
  3362. else
  3363. fds = fdplt;
  3364. fdplt -= fds;
  3365. gad->min -= fds;
  3366. gad->fdplt += fds;
  3367. }
  3368. /* If GOT entries took too much space, wrap some of them around.
  3369. This may well cause gad->min to become lower than wrapmin. This
  3370. will cause a relocation overflow later on, so we don't have to
  3371. report it here . */
  3372. if ((bfd_vma) gad->max > wrap)
  3373. {
  3374. gad->min -= gad->max - wrap;
  3375. gad->max = wrap;
  3376. }
  3377. /* If there is more space left, try to place some more function
  3378. descriptors for PLT entries. */
  3379. else if (fdplt && (bfd_vma) gad->max < wrap)
  3380. {
  3381. bfd_vma fds;
  3382. if ((bfd_vma) (wrap - gad->max) < fdplt)
  3383. fds = wrap - gad->max;
  3384. else
  3385. fds = fdplt;
  3386. fdplt -= fds;
  3387. gad->max += fds;
  3388. gad->fdplt += fds;
  3389. }
  3390. /* If odd was initially computed as an offset past the wrap point,
  3391. wrap it around. */
  3392. if (odd > gad->max)
  3393. odd = gad->min + odd - gad->max;
  3394. /* _bfinfdpic_get_got_entry() below will always wrap gad->cur if needed
  3395. before returning, so do it here too. This guarantees that,
  3396. should cur and fdcur meet at the wrap point, they'll both be
  3397. equal to min. */
  3398. if (gad->cur == gad->max)
  3399. gad->cur = gad->min;
  3400. return odd;
  3401. }
  3402. /* Compute the location of the next GOT entry, given the allocation
  3403. data for a range. */
  3404. inline static bfd_signed_vma
  3405. _bfinfdpic_get_got_entry (struct _bfinfdpic_dynamic_got_alloc_data *gad)
  3406. {
  3407. bfd_signed_vma ret;
  3408. if (gad->odd)
  3409. {
  3410. /* If there was an odd word left behind, use it. */
  3411. ret = gad->odd;
  3412. gad->odd = 0;
  3413. }
  3414. else
  3415. {
  3416. /* Otherwise, use the word pointed to by cur, reserve the next
  3417. as an odd word, and skip to the next pair of words, possibly
  3418. wrapping around. */
  3419. ret = gad->cur;
  3420. gad->odd = gad->cur + 4;
  3421. gad->cur += 8;
  3422. if (gad->cur == gad->max)
  3423. gad->cur = gad->min;
  3424. }
  3425. return ret;
  3426. }
  3427. /* Compute the location of the next function descriptor entry in the
  3428. GOT, given the allocation data for a range. */
  3429. inline static bfd_signed_vma
  3430. _bfinfdpic_get_fd_entry (struct _bfinfdpic_dynamic_got_alloc_data *gad)
  3431. {
  3432. /* If we're at the bottom, wrap around, and only then allocate the
  3433. next pair of words. */
  3434. if (gad->fdcur == gad->min)
  3435. gad->fdcur = gad->max;
  3436. return gad->fdcur -= 8;
  3437. }
  3438. /* Assign GOT offsets for every GOT entry and function descriptor.
  3439. Doing everything in a single pass is tricky. */
  3440. static int
  3441. _bfinfdpic_assign_got_entries (void **entryp, void *info_)
  3442. {
  3443. struct bfinfdpic_relocs_info *entry = *entryp;
  3444. struct _bfinfdpic_dynamic_got_plt_info *dinfo = info_;
  3445. if (entry->got17m4)
  3446. entry->got_entry = _bfinfdpic_get_got_entry (&dinfo->got17m4);
  3447. else if (entry->gothilo)
  3448. entry->got_entry = _bfinfdpic_get_got_entry (&dinfo->gothilo);
  3449. if (entry->fdgot17m4)
  3450. entry->fdgot_entry = _bfinfdpic_get_got_entry (&dinfo->got17m4);
  3451. else if (entry->fdgothilo)
  3452. entry->fdgot_entry = _bfinfdpic_get_got_entry (&dinfo->gothilo);
  3453. if (entry->fdgoff17m4)
  3454. entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->got17m4);
  3455. else if (entry->plt && dinfo->got17m4.fdplt)
  3456. {
  3457. dinfo->got17m4.fdplt -= 8;
  3458. entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->got17m4);
  3459. }
  3460. else if (entry->plt)
  3461. {
  3462. dinfo->gothilo.fdplt -= 8;
  3463. entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->gothilo);
  3464. }
  3465. else if (entry->privfd)
  3466. entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->gothilo);
  3467. return 1;
  3468. }
  3469. /* Assign GOT offsets to private function descriptors used by PLT
  3470. entries (or referenced by 32-bit offsets), as well as PLT entries
  3471. and lazy PLT entries. */
  3472. static int
  3473. _bfinfdpic_assign_plt_entries (void **entryp, void *info_)
  3474. {
  3475. struct bfinfdpic_relocs_info *entry = *entryp;
  3476. struct _bfinfdpic_dynamic_got_plt_info *dinfo = info_;
  3477. /* If this symbol requires a local function descriptor, allocate
  3478. one. */
  3479. if (entry->privfd && entry->fd_entry == 0)
  3480. {
  3481. if (dinfo->got17m4.fdplt)
  3482. {
  3483. entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->got17m4);
  3484. dinfo->got17m4.fdplt -= 8;
  3485. }
  3486. else
  3487. {
  3488. BFD_ASSERT (dinfo->gothilo.fdplt);
  3489. entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->gothilo);
  3490. dinfo->gothilo.fdplt -= 8;
  3491. }
  3492. }
  3493. if (entry->plt)
  3494. {
  3495. int size;
  3496. /* We use the section's raw size to mark the location of the
  3497. next PLT entry. */
  3498. entry->plt_entry = bfinfdpic_plt_section (dinfo->g.info)->size;
  3499. /* Figure out the length of this PLT entry based on the
  3500. addressing mode we need to reach the function descriptor. */
  3501. BFD_ASSERT (entry->fd_entry);
  3502. if (entry->fd_entry >= -(1 << (18 - 1))
  3503. && entry->fd_entry + 4 < (1 << (18 - 1)))
  3504. size = 10;
  3505. else
  3506. size = 16;
  3507. bfinfdpic_plt_section (dinfo->g.info)->size += size;
  3508. }
  3509. if (entry->lazyplt)
  3510. {
  3511. entry->lzplt_entry = dinfo->g.lzplt;
  3512. dinfo->g.lzplt += LZPLT_NORMAL_SIZE;
  3513. /* If this entry is the one that gets the resolver stub, account
  3514. for the additional instruction. */
  3515. if (entry->lzplt_entry % BFINFDPIC_LZPLT_BLOCK_SIZE
  3516. == BFINFDPIC_LZPLT_RESOLV_LOC)
  3517. dinfo->g.lzplt += LZPLT_RESOLVER_EXTRA;
  3518. }
  3519. return 1;
  3520. }
  3521. /* Cancel out any effects of calling _bfinfdpic_assign_got_entries and
  3522. _bfinfdpic_assign_plt_entries. */
  3523. static int
  3524. _bfinfdpic_reset_got_plt_entries (void **entryp, void *ignore ATTRIBUTE_UNUSED)
  3525. {
  3526. struct bfinfdpic_relocs_info *entry = *entryp;
  3527. entry->got_entry = 0;
  3528. entry->fdgot_entry = 0;
  3529. entry->fd_entry = 0;
  3530. entry->plt_entry = (bfd_vma)-1;
  3531. entry->lzplt_entry = (bfd_vma)-1;
  3532. return 1;
  3533. }
  3534. /* Follow indirect and warning hash entries so that each got entry
  3535. points to the final symbol definition. P must point to a pointer
  3536. to the hash table we're traversing. Since this traversal may
  3537. modify the hash table, we set this pointer to NULL to indicate
  3538. we've made a potentially-destructive change to the hash table, so
  3539. the traversal must be restarted. */
  3540. static int
  3541. _bfinfdpic_resolve_final_relocs_info (void **entryp, void *p)
  3542. {
  3543. struct bfinfdpic_relocs_info *entry = *entryp;
  3544. htab_t *htab = p;
  3545. if (entry->symndx == -1)
  3546. {
  3547. struct elf_link_hash_entry *h = entry->d.h;
  3548. struct bfinfdpic_relocs_info *oentry;
  3549. while (h->root.type == bfd_link_hash_indirect
  3550. || h->root.type == bfd_link_hash_warning)
  3551. h = (struct elf_link_hash_entry *)h->root.u.i.link;
  3552. if (entry->d.h == h)
  3553. return 1;
  3554. oentry = bfinfdpic_relocs_info_for_global (*htab, 0, h, entry->addend,
  3555. NO_INSERT);
  3556. if (oentry)
  3557. {
  3558. /* Merge the two entries. */
  3559. bfinfdpic_pic_merge_early_relocs_info (oentry, entry);
  3560. htab_clear_slot (*htab, entryp);
  3561. return 1;
  3562. }
  3563. entry->d.h = h;
  3564. /* If we can't find this entry with the new bfd hash, re-insert
  3565. it, and get the traversal restarted. */
  3566. if (! htab_find (*htab, entry))
  3567. {
  3568. htab_clear_slot (*htab, entryp);
  3569. entryp = htab_find_slot (*htab, entry, INSERT);
  3570. if (! *entryp)
  3571. *entryp = entry;
  3572. /* Abort the traversal, since the whole table may have
  3573. moved, and leave it up to the parent to restart the
  3574. process. */
  3575. *(htab_t *)p = NULL;
  3576. return 0;
  3577. }
  3578. }
  3579. return 1;
  3580. }
  3581. /* Compute the total size of the GOT, the PLT, the dynamic relocations
  3582. section and the rofixup section. Assign locations for GOT and PLT
  3583. entries. */
  3584. static bfd_boolean
  3585. _bfinfdpic_size_got_plt (bfd *output_bfd,
  3586. struct _bfinfdpic_dynamic_got_plt_info *gpinfop)
  3587. {
  3588. bfd_signed_vma odd;
  3589. bfd_vma limit;
  3590. struct bfd_link_info *info = gpinfop->g.info;
  3591. bfd *dynobj = elf_hash_table (info)->dynobj;
  3592. memcpy (bfinfdpic_dynamic_got_plt_info (info), &gpinfop->g,
  3593. sizeof (gpinfop->g));
  3594. odd = 12;
  3595. /* Compute the total size taken by entries in the 18-bit range,
  3596. to tell how many PLT function descriptors we can bring into it
  3597. without causing it to overflow. */
  3598. limit = odd + gpinfop->g.got17m4 + gpinfop->g.fd17m4;
  3599. if (limit < (bfd_vma)1 << 18)
  3600. limit = ((bfd_vma)1 << 18) - limit;
  3601. else
  3602. limit = 0;
  3603. if (gpinfop->g.fdplt < limit)
  3604. limit = gpinfop->g.fdplt;
  3605. /* Determine the ranges of GOT offsets that we can use for each
  3606. range of addressing modes. */
  3607. odd = _bfinfdpic_compute_got_alloc_data (&gpinfop->got17m4,
  3608. 0,
  3609. odd,
  3610. 16,
  3611. gpinfop->g.got17m4,
  3612. gpinfop->g.fd17m4,
  3613. limit,
  3614. (bfd_vma)1 << (18-1));
  3615. odd = _bfinfdpic_compute_got_alloc_data (&gpinfop->gothilo,
  3616. gpinfop->got17m4.min,
  3617. odd,
  3618. gpinfop->got17m4.max,
  3619. gpinfop->g.gothilo,
  3620. gpinfop->g.fdhilo,
  3621. gpinfop->g.fdplt - gpinfop->got17m4.fdplt,
  3622. (bfd_vma)1 << (32-1));
  3623. /* Now assign (most) GOT offsets. */
  3624. htab_traverse (bfinfdpic_relocs_info (info), _bfinfdpic_assign_got_entries,
  3625. gpinfop);
  3626. bfinfdpic_got_section (info)->size = gpinfop->gothilo.max
  3627. - gpinfop->gothilo.min
  3628. /* If an odd word is the last word of the GOT, we don't need this
  3629. word to be part of the GOT. */
  3630. - (odd + 4 == gpinfop->gothilo.max ? 4 : 0);
  3631. if (bfinfdpic_got_section (info)->size == 0)
  3632. bfinfdpic_got_section (info)->flags |= SEC_EXCLUDE;
  3633. else if (bfinfdpic_got_section (info)->size == 12
  3634. && ! elf_hash_table (info)->dynamic_sections_created)
  3635. {
  3636. bfinfdpic_got_section (info)->flags |= SEC_EXCLUDE;
  3637. bfinfdpic_got_section (info)->size = 0;
  3638. }
  3639. else
  3640. {
  3641. bfinfdpic_got_section (info)->contents =
  3642. (bfd_byte *) bfd_zalloc (dynobj,
  3643. bfinfdpic_got_section (info)->size);
  3644. if (bfinfdpic_got_section (info)->contents == NULL)
  3645. return FALSE;
  3646. }
  3647. if (elf_hash_table (info)->dynamic_sections_created)
  3648. /* Subtract the number of lzplt entries, since those will generate
  3649. relocations in the pltrel section. */
  3650. bfinfdpic_gotrel_section (info)->size =
  3651. (gpinfop->g.relocs - gpinfop->g.lzplt / LZPLT_NORMAL_SIZE)
  3652. * get_elf_backend_data (output_bfd)->s->sizeof_rel;
  3653. else
  3654. BFD_ASSERT (gpinfop->g.relocs == 0);
  3655. if (bfinfdpic_gotrel_section (info)->size == 0)
  3656. bfinfdpic_gotrel_section (info)->flags |= SEC_EXCLUDE;
  3657. else
  3658. {
  3659. bfinfdpic_gotrel_section (info)->contents =
  3660. (bfd_byte *) bfd_zalloc (dynobj,
  3661. bfinfdpic_gotrel_section (info)->size);
  3662. if (bfinfdpic_gotrel_section (info)->contents == NULL)
  3663. return FALSE;
  3664. }
  3665. bfinfdpic_gotfixup_section (info)->size = (gpinfop->g.fixups + 1) * 4;
  3666. if (bfinfdpic_gotfixup_section (info)->size == 0)
  3667. bfinfdpic_gotfixup_section (info)->flags |= SEC_EXCLUDE;
  3668. else
  3669. {
  3670. bfinfdpic_gotfixup_section (info)->contents =
  3671. (bfd_byte *) bfd_zalloc (dynobj,
  3672. bfinfdpic_gotfixup_section (info)->size);
  3673. if (bfinfdpic_gotfixup_section (info)->contents == NULL)
  3674. return FALSE;
  3675. }
  3676. if (elf_hash_table (info)->dynamic_sections_created)
  3677. bfinfdpic_pltrel_section (info)->size =
  3678. gpinfop->g.lzplt / LZPLT_NORMAL_SIZE * get_elf_backend_data (output_bfd)->s->sizeof_rel;
  3679. if (bfinfdpic_pltrel_section (info)->size == 0)
  3680. bfinfdpic_pltrel_section (info)->flags |= SEC_EXCLUDE;
  3681. else
  3682. {
  3683. bfinfdpic_pltrel_section (info)->contents =
  3684. (bfd_byte *) bfd_zalloc (dynobj,
  3685. bfinfdpic_pltrel_section (info)->size);
  3686. if (bfinfdpic_pltrel_section (info)->contents == NULL)
  3687. return FALSE;
  3688. }
  3689. /* Add 4 bytes for every block of at most 65535 lazy PLT entries,
  3690. such that there's room for the additional instruction needed to
  3691. call the resolver. Since _bfinfdpic_assign_got_entries didn't
  3692. account for them, our block size is 4 bytes smaller than the real
  3693. block size. */
  3694. if (elf_hash_table (info)->dynamic_sections_created)
  3695. {
  3696. bfinfdpic_plt_section (info)->size = gpinfop->g.lzplt
  3697. + ((gpinfop->g.lzplt + (BFINFDPIC_LZPLT_BLOCK_SIZE - 4) - LZPLT_NORMAL_SIZE)
  3698. / (BFINFDPIC_LZPLT_BLOCK_SIZE - 4) * LZPLT_RESOLVER_EXTRA);
  3699. }
  3700. /* Reset it, such that _bfinfdpic_assign_plt_entries() can use it to
  3701. actually assign lazy PLT entries addresses. */
  3702. gpinfop->g.lzplt = 0;
  3703. /* Save information that we're going to need to generate GOT and PLT
  3704. entries. */
  3705. bfinfdpic_got_initial_offset (info) = -gpinfop->gothilo.min;
  3706. if (get_elf_backend_data (output_bfd)->want_got_sym)
  3707. elf_hash_table (info)->hgot->root.u.def.value
  3708. = bfinfdpic_got_initial_offset (info);
  3709. if (elf_hash_table (info)->dynamic_sections_created)
  3710. bfinfdpic_plt_initial_offset (info) =
  3711. bfinfdpic_plt_section (info)->size;
  3712. htab_traverse (bfinfdpic_relocs_info (info), _bfinfdpic_assign_plt_entries,
  3713. gpinfop);
  3714. /* Allocate the PLT section contents only after
  3715. _bfinfdpic_assign_plt_entries has a chance to add the size of the
  3716. non-lazy PLT entries. */
  3717. if (bfinfdpic_plt_section (info)->size == 0)
  3718. bfinfdpic_plt_section (info)->flags |= SEC_EXCLUDE;
  3719. else
  3720. {
  3721. bfinfdpic_plt_section (info)->contents =
  3722. (bfd_byte *) bfd_zalloc (dynobj,
  3723. bfinfdpic_plt_section (info)->size);
  3724. if (bfinfdpic_plt_section (info)->contents == NULL)
  3725. return FALSE;
  3726. }
  3727. return TRUE;
  3728. }
  3729. /* Set the sizes of the dynamic sections. */
  3730. static bfd_boolean
  3731. elf32_bfinfdpic_size_dynamic_sections (bfd *output_bfd,
  3732. struct bfd_link_info *info)
  3733. {
  3734. struct elf_link_hash_table *htab;
  3735. bfd *dynobj;
  3736. asection *s;
  3737. struct _bfinfdpic_dynamic_got_plt_info gpinfo;
  3738. htab = elf_hash_table (info);
  3739. dynobj = htab->dynobj;
  3740. BFD_ASSERT (dynobj != NULL);
  3741. if (htab->dynamic_sections_created)
  3742. {
  3743. /* Set the contents of the .interp section to the interpreter. */
  3744. if (bfd_link_executable (info) && !info->nointerp)
  3745. {
  3746. s = bfd_get_linker_section (dynobj, ".interp");
  3747. BFD_ASSERT (s != NULL);
  3748. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  3749. s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
  3750. }
  3751. }
  3752. memset (&gpinfo, 0, sizeof (gpinfo));
  3753. gpinfo.g.info = info;
  3754. for (;;)
  3755. {
  3756. htab_t relocs = bfinfdpic_relocs_info (info);
  3757. htab_traverse (relocs, _bfinfdpic_resolve_final_relocs_info, &relocs);
  3758. if (relocs == bfinfdpic_relocs_info (info))
  3759. break;
  3760. }
  3761. htab_traverse (bfinfdpic_relocs_info (info), _bfinfdpic_count_got_plt_entries,
  3762. &gpinfo.g);
  3763. /* Allocate space to save the summary information, we're going to
  3764. use it if we're doing relaxations. */
  3765. bfinfdpic_dynamic_got_plt_info (info) = bfd_alloc (dynobj, sizeof (gpinfo.g));
  3766. if (!_bfinfdpic_size_got_plt (output_bfd, &gpinfo))
  3767. return FALSE;
  3768. if (elf_hash_table (info)->dynamic_sections_created)
  3769. {
  3770. if (bfinfdpic_got_section (info)->size)
  3771. if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0))
  3772. return FALSE;
  3773. if (bfinfdpic_pltrel_section (info)->size)
  3774. if (!_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
  3775. || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_REL)
  3776. || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
  3777. return FALSE;
  3778. if (bfinfdpic_gotrel_section (info)->size)
  3779. if (!_bfd_elf_add_dynamic_entry (info, DT_REL, 0)
  3780. || !_bfd_elf_add_dynamic_entry (info, DT_RELSZ, 0)
  3781. || !_bfd_elf_add_dynamic_entry (info, DT_RELENT,
  3782. sizeof (Elf32_External_Rel)))
  3783. return FALSE;
  3784. }
  3785. s = bfd_get_linker_section (dynobj, ".dynbss");
  3786. if (s && s->size == 0)
  3787. s->flags |= SEC_EXCLUDE;
  3788. s = bfd_get_linker_section (dynobj, ".rela.bss");
  3789. if (s && s->size == 0)
  3790. s->flags |= SEC_EXCLUDE;
  3791. return TRUE;
  3792. }
  3793. static bfd_boolean
  3794. elf32_bfinfdpic_always_size_sections (bfd *output_bfd,
  3795. struct bfd_link_info *info)
  3796. {
  3797. if (!bfd_link_relocatable (info)
  3798. && !bfd_elf_stack_segment_size (output_bfd, info,
  3799. "__stacksize", DEFAULT_STACK_SIZE))
  3800. return FALSE;
  3801. return TRUE;
  3802. }
  3803. /* Check whether any of the relocations was optimized away, and
  3804. subtract it from the relocation or fixup count. */
  3805. static bfd_boolean
  3806. _bfinfdpic_check_discarded_relocs (bfd *abfd, asection *sec,
  3807. struct bfd_link_info *info,
  3808. bfd_boolean *changed)
  3809. {
  3810. Elf_Internal_Shdr *symtab_hdr;
  3811. struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
  3812. Elf_Internal_Rela *rel, *erel;
  3813. if ((sec->flags & SEC_RELOC) == 0
  3814. || sec->reloc_count == 0)
  3815. return TRUE;
  3816. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  3817. sym_hashes = elf_sym_hashes (abfd);
  3818. sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
  3819. if (!elf_bad_symtab (abfd))
  3820. sym_hashes_end -= symtab_hdr->sh_info;
  3821. rel = elf_section_data (sec)->relocs;
  3822. /* Now examine each relocation. */
  3823. for (erel = rel + sec->reloc_count; rel < erel; rel++)
  3824. {
  3825. struct elf_link_hash_entry *h;
  3826. unsigned long r_symndx;
  3827. struct bfinfdpic_relocs_info *picrel;
  3828. struct _bfinfdpic_dynamic_got_info *dinfo;
  3829. if (ELF32_R_TYPE (rel->r_info) != R_BFIN_BYTE4_DATA
  3830. && ELF32_R_TYPE (rel->r_info) != R_BFIN_FUNCDESC)
  3831. continue;
  3832. if (_bfd_elf_section_offset (sec->output_section->owner,
  3833. info, sec, rel->r_offset)
  3834. != (bfd_vma)-1)
  3835. continue;
  3836. r_symndx = ELF32_R_SYM (rel->r_info);
  3837. if (r_symndx < symtab_hdr->sh_info)
  3838. h = NULL;
  3839. else
  3840. {
  3841. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  3842. while (h->root.type == bfd_link_hash_indirect
  3843. || h->root.type == bfd_link_hash_warning)
  3844. h = (struct elf_link_hash_entry *)h->root.u.i.link;
  3845. }
  3846. if (h != NULL)
  3847. picrel = bfinfdpic_relocs_info_for_global (bfinfdpic_relocs_info (info),
  3848. abfd, h,
  3849. rel->r_addend, NO_INSERT);
  3850. else
  3851. picrel = bfinfdpic_relocs_info_for_local (bfinfdpic_relocs_info (info),
  3852. abfd, r_symndx,
  3853. rel->r_addend, NO_INSERT);
  3854. if (! picrel)
  3855. return FALSE;
  3856. *changed = TRUE;
  3857. dinfo = bfinfdpic_dynamic_got_plt_info (info);
  3858. _bfinfdpic_count_relocs_fixups (picrel, dinfo, TRUE);
  3859. if (ELF32_R_TYPE (rel->r_info) == R_BFIN_BYTE4_DATA)
  3860. picrel->relocs32--;
  3861. else /* we know (ELF32_R_TYPE (rel->r_info) == R_BFIN_FUNCDESC) */
  3862. picrel->relocsfd--;
  3863. _bfinfdpic_count_relocs_fixups (picrel, dinfo, FALSE);
  3864. }
  3865. return TRUE;
  3866. }
  3867. static bfd_boolean
  3868. bfinfdpic_elf_discard_info (bfd *ibfd,
  3869. struct elf_reloc_cookie *cookie ATTRIBUTE_UNUSED,
  3870. struct bfd_link_info *info)
  3871. {
  3872. bfd_boolean changed = FALSE;
  3873. asection *s;
  3874. bfd *obfd = NULL;
  3875. /* Account for relaxation of .eh_frame section. */
  3876. for (s = ibfd->sections; s; s = s->next)
  3877. if (s->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
  3878. {
  3879. if (!_bfinfdpic_check_discarded_relocs (ibfd, s, info, &changed))
  3880. return FALSE;
  3881. obfd = s->output_section->owner;
  3882. }
  3883. if (changed)
  3884. {
  3885. struct _bfinfdpic_dynamic_got_plt_info gpinfo;
  3886. memset (&gpinfo, 0, sizeof (gpinfo));
  3887. memcpy (&gpinfo.g, bfinfdpic_dynamic_got_plt_info (info),
  3888. sizeof (gpinfo.g));
  3889. /* Clear GOT and PLT assignments. */
  3890. htab_traverse (bfinfdpic_relocs_info (info),
  3891. _bfinfdpic_reset_got_plt_entries,
  3892. NULL);
  3893. if (!_bfinfdpic_size_got_plt (obfd, &gpinfo))
  3894. return FALSE;
  3895. }
  3896. return TRUE;
  3897. }
  3898. static bfd_boolean
  3899. elf32_bfinfdpic_finish_dynamic_sections (bfd *output_bfd,
  3900. struct bfd_link_info *info)
  3901. {
  3902. bfd *dynobj;
  3903. asection *sdyn;
  3904. dynobj = elf_hash_table (info)->dynobj;
  3905. if (bfinfdpic_got_section (info))
  3906. {
  3907. BFD_ASSERT (bfinfdpic_gotrel_section (info)->size
  3908. == (bfinfdpic_gotrel_section (info)->reloc_count
  3909. * sizeof (Elf32_External_Rel)));
  3910. if (bfinfdpic_gotfixup_section (info))
  3911. {
  3912. struct elf_link_hash_entry *hgot = elf_hash_table (info)->hgot;
  3913. bfd_vma got_value = hgot->root.u.def.value
  3914. + hgot->root.u.def.section->output_section->vma
  3915. + hgot->root.u.def.section->output_offset;
  3916. _bfinfdpic_add_rofixup (output_bfd, bfinfdpic_gotfixup_section (info),
  3917. got_value, 0);
  3918. if (bfinfdpic_gotfixup_section (info)->size
  3919. != (bfinfdpic_gotfixup_section (info)->reloc_count * 4))
  3920. {
  3921. (*_bfd_error_handler)
  3922. ("LINKER BUG: .rofixup section size mismatch");
  3923. return FALSE;
  3924. }
  3925. }
  3926. }
  3927. if (elf_hash_table (info)->dynamic_sections_created)
  3928. {
  3929. BFD_ASSERT (bfinfdpic_pltrel_section (info)->size
  3930. == (bfinfdpic_pltrel_section (info)->reloc_count
  3931. * sizeof (Elf32_External_Rel)));
  3932. }
  3933. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  3934. if (elf_hash_table (info)->dynamic_sections_created)
  3935. {
  3936. Elf32_External_Dyn * dyncon;
  3937. Elf32_External_Dyn * dynconend;
  3938. BFD_ASSERT (sdyn != NULL);
  3939. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  3940. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  3941. for (; dyncon < dynconend; dyncon++)
  3942. {
  3943. Elf_Internal_Dyn dyn;
  3944. bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  3945. switch (dyn.d_tag)
  3946. {
  3947. default:
  3948. break;
  3949. case DT_PLTGOT:
  3950. dyn.d_un.d_ptr = bfinfdpic_got_section (info)->output_section->vma
  3951. + bfinfdpic_got_section (info)->output_offset
  3952. + bfinfdpic_got_initial_offset (info);
  3953. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  3954. break;
  3955. case DT_JMPREL:
  3956. dyn.d_un.d_ptr = bfinfdpic_pltrel_section (info)
  3957. ->output_section->vma
  3958. + bfinfdpic_pltrel_section (info)->output_offset;
  3959. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  3960. break;
  3961. case DT_PLTRELSZ:
  3962. dyn.d_un.d_val = bfinfdpic_pltrel_section (info)->size;
  3963. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  3964. break;
  3965. }
  3966. }
  3967. }
  3968. return TRUE;
  3969. }
  3970. /* Adjust a symbol defined by a dynamic object and referenced by a
  3971. regular object. */
  3972. static bfd_boolean
  3973. elf32_bfinfdpic_adjust_dynamic_symbol (struct bfd_link_info *info,
  3974. struct elf_link_hash_entry *h)
  3975. {
  3976. bfd * dynobj;
  3977. dynobj = elf_hash_table (info)->dynobj;
  3978. /* Make sure we know what is going on here. */
  3979. BFD_ASSERT (dynobj != NULL
  3980. && (h->u.weakdef != NULL
  3981. || (h->def_dynamic
  3982. && h->ref_regular
  3983. && !h->def_regular)));
  3984. /* If this is a weak symbol, and there is a real definition, the
  3985. processor independent code will have arranged for us to see the
  3986. real definition first, and we can just use the same value. */
  3987. if (h->u.weakdef != NULL)
  3988. {
  3989. BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
  3990. || h->u.weakdef->root.type == bfd_link_hash_defweak);
  3991. h->root.u.def.section = h->u.weakdef->root.u.def.section;
  3992. h->root.u.def.value = h->u.weakdef->root.u.def.value;
  3993. }
  3994. return TRUE;
  3995. }
  3996. /* Perform any actions needed for dynamic symbols. */
  3997. static bfd_boolean
  3998. elf32_bfinfdpic_finish_dynamic_symbol
  3999. (bfd *output_bfd ATTRIBUTE_UNUSED,
  4000. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  4001. struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
  4002. Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
  4003. {
  4004. return TRUE;
  4005. }
  4006. /* Decide whether to attempt to turn absptr or lsda encodings in
  4007. shared libraries into pcrel within the given input section. */
  4008. static bfd_boolean
  4009. bfinfdpic_elf_use_relative_eh_frame
  4010. (bfd *input_bfd ATTRIBUTE_UNUSED,
  4011. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  4012. asection *eh_frame_section ATTRIBUTE_UNUSED)
  4013. {
  4014. /* We can't use PC-relative encodings in FDPIC binaries, in general. */
  4015. return FALSE;
  4016. }
  4017. /* Adjust the contents of an eh_frame_hdr section before they're output. */
  4018. static bfd_byte
  4019. bfinfdpic_elf_encode_eh_address (bfd *abfd,
  4020. struct bfd_link_info *info,
  4021. asection *osec, bfd_vma offset,
  4022. asection *loc_sec, bfd_vma loc_offset,
  4023. bfd_vma *encoded)
  4024. {
  4025. struct elf_link_hash_entry *h;
  4026. h = elf_hash_table (info)->hgot;
  4027. BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
  4028. if (! h || (_bfinfdpic_osec_to_segment (abfd, osec)
  4029. == _bfinfdpic_osec_to_segment (abfd, loc_sec->output_section)))
  4030. return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
  4031. loc_sec, loc_offset, encoded);
  4032. BFD_ASSERT (_bfinfdpic_osec_to_segment (abfd, osec)
  4033. == (_bfinfdpic_osec_to_segment
  4034. (abfd, h->root.u.def.section->output_section)));
  4035. *encoded = osec->vma + offset
  4036. - (h->root.u.def.value
  4037. + h->root.u.def.section->output_section->vma
  4038. + h->root.u.def.section->output_offset);
  4039. return DW_EH_PE_datarel | DW_EH_PE_sdata4;
  4040. }
  4041. /* Look through the relocs for a section during the first phase.
  4042. Besides handling virtual table relocs for gc, we have to deal with
  4043. all sorts of PIC-related relocations. We describe below the
  4044. general plan on how to handle such relocations, even though we only
  4045. collect information at this point, storing them in hash tables for
  4046. perusal of later passes.
  4047. 32 relocations are propagated to the linker output when creating
  4048. position-independent output. LO16 and HI16 relocations are not
  4049. supposed to be encountered in this case.
  4050. LABEL16 should always be resolvable by the linker, since it's only
  4051. used by branches.
  4052. LABEL24, on the other hand, is used by calls. If it turns out that
  4053. the target of a call is a dynamic symbol, a PLT entry must be
  4054. created for it, which triggers the creation of a private function
  4055. descriptor and, unless lazy binding is disabled, a lazy PLT entry.
  4056. GPREL relocations require the referenced symbol to be in the same
  4057. segment as _gp, but this can only be checked later.
  4058. All GOT, GOTOFF and FUNCDESC relocations require a .got section to
  4059. exist. LABEL24 might as well, since it may require a PLT entry,
  4060. that will require a got.
  4061. Non-FUNCDESC GOT relocations require a GOT entry to be created
  4062. regardless of whether the symbol is dynamic. However, since a
  4063. global symbol that turns out to not be exported may have the same
  4064. address of a non-dynamic symbol, we don't assign GOT entries at
  4065. this point, such that we can share them in this case. A relocation
  4066. for the GOT entry always has to be created, be it to offset a
  4067. private symbol by the section load address, be it to get the symbol
  4068. resolved dynamically.
  4069. FUNCDESC GOT relocations require a GOT entry to be created, and
  4070. handled as if a FUNCDESC relocation was applied to the GOT entry in
  4071. an object file.
  4072. FUNCDESC relocations referencing a symbol that turns out to NOT be
  4073. dynamic cause a private function descriptor to be created. The
  4074. FUNCDESC relocation then decays to a 32 relocation that points at
  4075. the private descriptor. If the symbol is dynamic, the FUNCDESC
  4076. relocation is propagated to the linker output, such that the
  4077. dynamic linker creates the canonical descriptor, pointing to the
  4078. dynamically-resolved definition of the function.
  4079. Non-FUNCDESC GOTOFF relocations must always refer to non-dynamic
  4080. symbols that are assigned to the same segment as the GOT, but we
  4081. can only check this later, after we know the complete set of
  4082. symbols defined and/or exported.
  4083. FUNCDESC GOTOFF relocations require a function descriptor to be
  4084. created and, unless lazy binding is disabled or the symbol is not
  4085. dynamic, a lazy PLT entry. Since we can't tell at this point
  4086. whether a symbol is going to be dynamic, we have to decide later
  4087. whether to create a lazy PLT entry or bind the descriptor directly
  4088. to the private function.
  4089. FUNCDESC_VALUE relocations are not supposed to be present in object
  4090. files, but they may very well be simply propagated to the linker
  4091. output, since they have no side effect.
  4092. A function descriptor always requires a FUNCDESC_VALUE relocation.
  4093. Whether it's in .plt.rel or not depends on whether lazy binding is
  4094. enabled and on whether the referenced symbol is dynamic.
  4095. The existence of a lazy PLT requires the resolverStub lazy PLT
  4096. entry to be present.
  4097. As for assignment of GOT, PLT and lazy PLT entries, and private
  4098. descriptors, we might do them all sequentially, but we can do
  4099. better than that. For example, we can place GOT entries and
  4100. private function descriptors referenced using 12-bit operands
  4101. closer to the PIC register value, such that these relocations don't
  4102. overflow. Those that are only referenced with LO16 relocations
  4103. could come next, but we may as well place PLT-required function
  4104. descriptors in the 12-bit range to make them shorter. Symbols
  4105. referenced with LO16/HI16 may come next, but we may place
  4106. additional function descriptors in the 16-bit range if we can
  4107. reliably tell that we've already placed entries that are ever
  4108. referenced with only LO16. PLT entries are therefore generated as
  4109. small as possible, while not introducing relocation overflows in
  4110. GOT or FUNCDESC_GOTOFF relocations. Lazy PLT entries could be
  4111. generated before or after PLT entries, but not intermingled with
  4112. them, such that we can have more lazy PLT entries in range for a
  4113. branch to the resolverStub. The resolverStub should be emitted at
  4114. the most distant location from the first lazy PLT entry such that
  4115. it's still in range for a branch, or closer, if there isn't a need
  4116. for so many lazy PLT entries. Additional lazy PLT entries may be
  4117. emitted after the resolverStub, as long as branches are still in
  4118. range. If the branch goes out of range, longer lazy PLT entries
  4119. are emitted.
  4120. We could further optimize PLT and lazy PLT entries by giving them
  4121. priority in assignment to closer-to-gr17 locations depending on the
  4122. number of occurrences of references to them (assuming a function
  4123. that's called more often is more important for performance, so its
  4124. PLT entry should be faster), or taking hints from the compiler.
  4125. Given infinite time and money... :-) */
  4126. static bfd_boolean
  4127. bfinfdpic_check_relocs (bfd *abfd, struct bfd_link_info *info,
  4128. asection *sec, const Elf_Internal_Rela *relocs)
  4129. {
  4130. Elf_Internal_Shdr *symtab_hdr;
  4131. struct elf_link_hash_entry **sym_hashes;
  4132. const Elf_Internal_Rela *rel;
  4133. const Elf_Internal_Rela *rel_end;
  4134. bfd *dynobj;
  4135. struct bfinfdpic_relocs_info *picrel;
  4136. if (bfd_link_relocatable (info))
  4137. return TRUE;
  4138. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  4139. sym_hashes = elf_sym_hashes (abfd);
  4140. dynobj = elf_hash_table (info)->dynobj;
  4141. rel_end = relocs + sec->reloc_count;
  4142. for (rel = relocs; rel < rel_end; rel++)
  4143. {
  4144. struct elf_link_hash_entry *h;
  4145. unsigned long r_symndx;
  4146. r_symndx = ELF32_R_SYM (rel->r_info);
  4147. if (r_symndx < symtab_hdr->sh_info)
  4148. h = NULL;
  4149. else
  4150. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  4151. switch (ELF32_R_TYPE (rel->r_info))
  4152. {
  4153. case R_BFIN_GOT17M4:
  4154. case R_BFIN_GOTHI:
  4155. case R_BFIN_GOTLO:
  4156. case R_BFIN_FUNCDESC_GOT17M4:
  4157. case R_BFIN_FUNCDESC_GOTHI:
  4158. case R_BFIN_FUNCDESC_GOTLO:
  4159. case R_BFIN_GOTOFF17M4:
  4160. case R_BFIN_GOTOFFHI:
  4161. case R_BFIN_GOTOFFLO:
  4162. case R_BFIN_FUNCDESC_GOTOFF17M4:
  4163. case R_BFIN_FUNCDESC_GOTOFFHI:
  4164. case R_BFIN_FUNCDESC_GOTOFFLO:
  4165. case R_BFIN_FUNCDESC:
  4166. case R_BFIN_FUNCDESC_VALUE:
  4167. if (! IS_FDPIC (abfd))
  4168. goto bad_reloc;
  4169. /* Fall through. */
  4170. case R_BFIN_PCREL24:
  4171. case R_BFIN_PCREL24_JUMP_L:
  4172. case R_BFIN_BYTE4_DATA:
  4173. if (IS_FDPIC (abfd) && ! dynobj)
  4174. {
  4175. elf_hash_table (info)->dynobj = dynobj = abfd;
  4176. if (! _bfin_create_got_section (abfd, info))
  4177. return FALSE;
  4178. }
  4179. if (! IS_FDPIC (abfd))
  4180. {
  4181. picrel = NULL;
  4182. break;
  4183. }
  4184. if (h != NULL)
  4185. {
  4186. if (h->dynindx == -1)
  4187. switch (ELF_ST_VISIBILITY (h->other))
  4188. {
  4189. case STV_INTERNAL:
  4190. case STV_HIDDEN:
  4191. break;
  4192. default:
  4193. bfd_elf_link_record_dynamic_symbol (info, h);
  4194. break;
  4195. }
  4196. picrel
  4197. = bfinfdpic_relocs_info_for_global (bfinfdpic_relocs_info (info),
  4198. abfd, h,
  4199. rel->r_addend, INSERT);
  4200. }
  4201. else
  4202. picrel = bfinfdpic_relocs_info_for_local (bfinfdpic_relocs_info
  4203. (info), abfd, r_symndx,
  4204. rel->r_addend, INSERT);
  4205. if (! picrel)
  4206. return FALSE;
  4207. break;
  4208. default:
  4209. picrel = NULL;
  4210. break;
  4211. }
  4212. switch (ELF32_R_TYPE (rel->r_info))
  4213. {
  4214. case R_BFIN_PCREL24:
  4215. case R_BFIN_PCREL24_JUMP_L:
  4216. if (IS_FDPIC (abfd))
  4217. picrel->call++;
  4218. break;
  4219. case R_BFIN_FUNCDESC_VALUE:
  4220. picrel->relocsfdv++;
  4221. if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
  4222. picrel->relocs32--;
  4223. /* Fall through. */
  4224. case R_BFIN_BYTE4_DATA:
  4225. if (! IS_FDPIC (abfd))
  4226. break;
  4227. picrel->sym++;
  4228. if (bfd_get_section_flags (abfd, sec) & SEC_ALLOC)
  4229. picrel->relocs32++;
  4230. break;
  4231. case R_BFIN_GOT17M4:
  4232. picrel->got17m4++;
  4233. break;
  4234. case R_BFIN_GOTHI:
  4235. case R_BFIN_GOTLO:
  4236. picrel->gothilo++;
  4237. break;
  4238. case R_BFIN_FUNCDESC_GOT17M4:
  4239. picrel->fdgot17m4++;
  4240. break;
  4241. case R_BFIN_FUNCDESC_GOTHI:
  4242. case R_BFIN_FUNCDESC_GOTLO:
  4243. picrel->fdgothilo++;
  4244. break;
  4245. case R_BFIN_GOTOFF17M4:
  4246. case R_BFIN_GOTOFFHI:
  4247. case R_BFIN_GOTOFFLO:
  4248. picrel->gotoff++;
  4249. break;
  4250. case R_BFIN_FUNCDESC_GOTOFF17M4:
  4251. picrel->fdgoff17m4++;
  4252. break;
  4253. case R_BFIN_FUNCDESC_GOTOFFHI:
  4254. case R_BFIN_FUNCDESC_GOTOFFLO:
  4255. picrel->fdgoffhilo++;
  4256. break;
  4257. case R_BFIN_FUNCDESC:
  4258. picrel->fd++;
  4259. picrel->relocsfd++;
  4260. break;
  4261. /* This relocation describes the C++ object vtable hierarchy.
  4262. Reconstruct it for later use during GC. */
  4263. case R_BFIN_GNU_VTINHERIT:
  4264. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  4265. return FALSE;
  4266. break;
  4267. /* This relocation describes which C++ vtable entries are actually
  4268. used. Record for later use during GC. */
  4269. case R_BFIN_GNU_VTENTRY:
  4270. BFD_ASSERT (h != NULL);
  4271. if (h != NULL
  4272. && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  4273. return FALSE;
  4274. break;
  4275. case R_BFIN_HUIMM16:
  4276. case R_BFIN_LUIMM16:
  4277. case R_BFIN_PCREL12_JUMP_S:
  4278. case R_BFIN_PCREL10:
  4279. break;
  4280. default:
  4281. bad_reloc:
  4282. (*_bfd_error_handler)
  4283. (_("%B: unsupported relocation type %i"),
  4284. abfd, ELF32_R_TYPE (rel->r_info));
  4285. return FALSE;
  4286. }
  4287. }
  4288. return TRUE;
  4289. }
  4290. /* Set the right machine number for a Blackfin ELF file. */
  4291. static bfd_boolean
  4292. elf32_bfin_object_p (bfd *abfd)
  4293. {
  4294. bfd_default_set_arch_mach (abfd, bfd_arch_bfin, 0);
  4295. return (((elf_elfheader (abfd)->e_flags & EF_BFIN_FDPIC) != 0)
  4296. == (IS_FDPIC (abfd)));
  4297. }
  4298. static bfd_boolean
  4299. elf32_bfin_set_private_flags (bfd * abfd, flagword flags)
  4300. {
  4301. elf_elfheader (abfd)->e_flags = flags;
  4302. elf_flags_init (abfd) = TRUE;
  4303. return TRUE;
  4304. }
  4305. /* Display the flags field. */
  4306. static bfd_boolean
  4307. elf32_bfin_print_private_bfd_data (bfd * abfd, void * ptr)
  4308. {
  4309. FILE *file = (FILE *) ptr;
  4310. flagword flags;
  4311. BFD_ASSERT (abfd != NULL && ptr != NULL);
  4312. /* Print normal ELF private data. */
  4313. _bfd_elf_print_private_bfd_data (abfd, ptr);
  4314. flags = elf_elfheader (abfd)->e_flags;
  4315. /* xgettext:c-format */
  4316. fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
  4317. if (flags & EF_BFIN_PIC)
  4318. fprintf (file, " -fpic");
  4319. if (flags & EF_BFIN_FDPIC)
  4320. fprintf (file, " -mfdpic");
  4321. fputc ('\n', file);
  4322. return TRUE;
  4323. }
  4324. /* Merge backend specific data from an object file to the output
  4325. object file when linking. */
  4326. static bfd_boolean
  4327. elf32_bfin_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
  4328. {
  4329. flagword old_flags, new_flags;
  4330. bfd_boolean error = FALSE;
  4331. new_flags = elf_elfheader (ibfd)->e_flags;
  4332. old_flags = elf_elfheader (obfd)->e_flags;
  4333. if (new_flags & EF_BFIN_FDPIC)
  4334. new_flags &= ~EF_BFIN_PIC;
  4335. #ifndef DEBUG
  4336. if (0)
  4337. #endif
  4338. (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
  4339. old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
  4340. bfd_get_filename (ibfd));
  4341. if (!elf_flags_init (obfd)) /* First call, no flags set. */
  4342. {
  4343. elf_flags_init (obfd) = TRUE;
  4344. elf_elfheader (obfd)->e_flags = new_flags;
  4345. }
  4346. if (((new_flags & EF_BFIN_FDPIC) == 0) != (! IS_FDPIC (obfd)))
  4347. {
  4348. error = TRUE;
  4349. if (IS_FDPIC (obfd))
  4350. (*_bfd_error_handler)
  4351. (_("%s: cannot link non-fdpic object file into fdpic executable"),
  4352. bfd_get_filename (ibfd));
  4353. else
  4354. (*_bfd_error_handler)
  4355. (_("%s: cannot link fdpic object file into non-fdpic executable"),
  4356. bfd_get_filename (ibfd));
  4357. }
  4358. if (error)
  4359. bfd_set_error (bfd_error_bad_value);
  4360. return !error;
  4361. }
  4362. /* bfin ELF linker hash entry. */
  4363. struct bfin_link_hash_entry
  4364. {
  4365. struct elf_link_hash_entry root;
  4366. /* Number of PC relative relocs copied for this symbol. */
  4367. struct bfin_pcrel_relocs_copied *pcrel_relocs_copied;
  4368. };
  4369. /* bfin ELF linker hash table. */
  4370. struct bfin_link_hash_table
  4371. {
  4372. struct elf_link_hash_table root;
  4373. /* Small local sym cache. */
  4374. struct sym_cache sym_cache;
  4375. };
  4376. #define bfin_hash_entry(ent) ((struct bfin_link_hash_entry *) (ent))
  4377. static struct bfd_hash_entry *
  4378. bfin_link_hash_newfunc (struct bfd_hash_entry *entry,
  4379. struct bfd_hash_table *table, const char *string)
  4380. {
  4381. struct bfd_hash_entry *ret = entry;
  4382. /* Allocate the structure if it has not already been allocated by a
  4383. subclass. */
  4384. if (ret == NULL)
  4385. ret = bfd_hash_allocate (table, sizeof (struct bfin_link_hash_entry));
  4386. if (ret == NULL)
  4387. return ret;
  4388. /* Call the allocation method of the superclass. */
  4389. ret = _bfd_elf_link_hash_newfunc (ret, table, string);
  4390. if (ret != NULL)
  4391. bfin_hash_entry (ret)->pcrel_relocs_copied = NULL;
  4392. return ret;
  4393. }
  4394. /* Create an bfin ELF linker hash table. */
  4395. static struct bfd_link_hash_table *
  4396. bfin_link_hash_table_create (bfd * abfd)
  4397. {
  4398. struct bfin_link_hash_table *ret;
  4399. bfd_size_type amt = sizeof (struct bfin_link_hash_table);
  4400. ret = bfd_zmalloc (amt);
  4401. if (ret == NULL)
  4402. return NULL;
  4403. if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
  4404. bfin_link_hash_newfunc,
  4405. sizeof (struct elf_link_hash_entry),
  4406. BFIN_ELF_DATA))
  4407. {
  4408. free (ret);
  4409. return NULL;
  4410. }
  4411. ret->sym_cache.abfd = NULL;
  4412. return &ret->root.root;
  4413. }
  4414. /* The size in bytes of an entry in the procedure linkage table. */
  4415. /* Finish up the dynamic sections. */
  4416. static bfd_boolean
  4417. bfin_finish_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
  4418. struct bfd_link_info *info)
  4419. {
  4420. bfd *dynobj;
  4421. asection *sdyn;
  4422. dynobj = elf_hash_table (info)->dynobj;
  4423. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  4424. if (elf_hash_table (info)->dynamic_sections_created)
  4425. {
  4426. Elf32_External_Dyn *dyncon, *dynconend;
  4427. BFD_ASSERT (sdyn != NULL);
  4428. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  4429. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  4430. for (; dyncon < dynconend; dyncon++)
  4431. {
  4432. Elf_Internal_Dyn dyn;
  4433. bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  4434. }
  4435. }
  4436. return TRUE;
  4437. }
  4438. /* Finish up dynamic symbol handling. We set the contents of various
  4439. dynamic sections here. */
  4440. static bfd_boolean
  4441. bfin_finish_dynamic_symbol (bfd * output_bfd,
  4442. struct bfd_link_info *info,
  4443. struct elf_link_hash_entry *h,
  4444. Elf_Internal_Sym * sym)
  4445. {
  4446. bfd *dynobj;
  4447. dynobj = elf_hash_table (info)->dynobj;
  4448. if (h->got.offset != (bfd_vma) - 1)
  4449. {
  4450. asection *sgot;
  4451. asection *srela;
  4452. Elf_Internal_Rela rela;
  4453. bfd_byte *loc;
  4454. /* This symbol has an entry in the global offset table.
  4455. Set it up. */
  4456. sgot = bfd_get_linker_section (dynobj, ".got");
  4457. srela = bfd_get_linker_section (dynobj, ".rela.got");
  4458. BFD_ASSERT (sgot != NULL && srela != NULL);
  4459. rela.r_offset = (sgot->output_section->vma
  4460. + sgot->output_offset
  4461. + (h->got.offset & ~(bfd_vma) 1));
  4462. /* If this is a -Bsymbolic link, and the symbol is defined
  4463. locally, we just want to emit a RELATIVE reloc. Likewise if
  4464. the symbol was forced to be local because of a version file.
  4465. The entry in the global offset table will already have been
  4466. initialized in the relocate_section function. */
  4467. if (bfd_link_pic (info)
  4468. && (info->symbolic
  4469. || h->dynindx == -1 || h->forced_local) && h->def_regular)
  4470. {
  4471. (*_bfd_error_handler) (_("*** check this relocation %s"),
  4472. __FUNCTION__);
  4473. rela.r_info = ELF32_R_INFO (0, R_BFIN_PCREL24);
  4474. rela.r_addend = bfd_get_signed_32 (output_bfd,
  4475. (sgot->contents
  4476. +
  4477. (h->got.
  4478. offset & ~(bfd_vma) 1)));
  4479. }
  4480. else
  4481. {
  4482. bfd_put_32 (output_bfd, (bfd_vma) 0,
  4483. sgot->contents + (h->got.offset & ~(bfd_vma) 1));
  4484. rela.r_info = ELF32_R_INFO (h->dynindx, R_BFIN_GOT);
  4485. rela.r_addend = 0;
  4486. }
  4487. loc = srela->contents;
  4488. loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
  4489. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  4490. }
  4491. if (h->needs_copy)
  4492. {
  4493. BFD_ASSERT (0);
  4494. }
  4495. /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
  4496. if (strcmp (h->root.root.string, "__DYNAMIC") == 0
  4497. || h == elf_hash_table (info)->hgot)
  4498. sym->st_shndx = SHN_ABS;
  4499. return TRUE;
  4500. }
  4501. /* Adjust a symbol defined by a dynamic object and referenced by a
  4502. regular object. The current definition is in some section of the
  4503. dynamic object, but we're not including those sections. We have to
  4504. change the definition to something the rest of the link can
  4505. understand. */
  4506. static bfd_boolean
  4507. bfin_adjust_dynamic_symbol (struct bfd_link_info *info,
  4508. struct elf_link_hash_entry *h)
  4509. {
  4510. bfd *dynobj;
  4511. asection *s;
  4512. unsigned int power_of_two;
  4513. dynobj = elf_hash_table (info)->dynobj;
  4514. /* Make sure we know what is going on here. */
  4515. BFD_ASSERT (dynobj != NULL
  4516. && (h->needs_plt
  4517. || h->u.weakdef != NULL
  4518. || (h->def_dynamic && h->ref_regular && !h->def_regular)));
  4519. /* If this is a function, put it in the procedure linkage table. We
  4520. will fill in the contents of the procedure linkage table later,
  4521. when we know the address of the .got section. */
  4522. if (h->type == STT_FUNC || h->needs_plt)
  4523. {
  4524. BFD_ASSERT(0);
  4525. }
  4526. /* If this is a weak symbol, and there is a real definition, the
  4527. processor independent code will have arranged for us to see the
  4528. real definition first, and we can just use the same value. */
  4529. if (h->u.weakdef != NULL)
  4530. {
  4531. BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
  4532. || h->u.weakdef->root.type == bfd_link_hash_defweak);
  4533. h->root.u.def.section = h->u.weakdef->root.u.def.section;
  4534. h->root.u.def.value = h->u.weakdef->root.u.def.value;
  4535. return TRUE;
  4536. }
  4537. /* This is a reference to a symbol defined by a dynamic object which
  4538. is not a function. */
  4539. /* If we are creating a shared library, we must presume that the
  4540. only references to the symbol are via the global offset table.
  4541. For such cases we need not do anything here; the relocations will
  4542. be handled correctly by relocate_section. */
  4543. if (bfd_link_pic (info))
  4544. return TRUE;
  4545. /* We must allocate the symbol in our .dynbss section, which will
  4546. become part of the .bss section of the executable. There will be
  4547. an entry for this symbol in the .dynsym section. The dynamic
  4548. object will contain position independent code, so all references
  4549. from the dynamic object to this symbol will go through the global
  4550. offset table. The dynamic linker will use the .dynsym entry to
  4551. determine the address it must put in the global offset table, so
  4552. both the dynamic object and the regular object will refer to the
  4553. same memory location for the variable. */
  4554. s = bfd_get_linker_section (dynobj, ".dynbss");
  4555. BFD_ASSERT (s != NULL);
  4556. /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
  4557. copy the initial value out of the dynamic object and into the
  4558. runtime process image. We need to remember the offset into the
  4559. .rela.bss section we are going to use. */
  4560. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
  4561. {
  4562. asection *srel;
  4563. srel = bfd_get_linker_section (dynobj, ".rela.bss");
  4564. BFD_ASSERT (srel != NULL);
  4565. srel->size += sizeof (Elf32_External_Rela);
  4566. h->needs_copy = 1;
  4567. }
  4568. /* We need to figure out the alignment required for this symbol. I
  4569. have no idea how ELF linkers handle this. */
  4570. power_of_two = bfd_log2 (h->size);
  4571. if (power_of_two > 3)
  4572. power_of_two = 3;
  4573. /* Apply the required alignment. */
  4574. s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
  4575. if (power_of_two > bfd_get_section_alignment (dynobj, s))
  4576. {
  4577. if (!bfd_set_section_alignment (dynobj, s, power_of_two))
  4578. return FALSE;
  4579. }
  4580. /* Define the symbol as being at this point in the section. */
  4581. h->root.u.def.section = s;
  4582. h->root.u.def.value = s->size;
  4583. /* Increment the section size to make room for the symbol. */
  4584. s->size += h->size;
  4585. return TRUE;
  4586. }
  4587. /* The bfin linker needs to keep track of the number of relocs that it
  4588. decides to copy in check_relocs for each symbol. This is so that it
  4589. can discard PC relative relocs if it doesn't need them when linking
  4590. with -Bsymbolic. We store the information in a field extending the
  4591. regular ELF linker hash table. */
  4592. /* This structure keeps track of the number of PC relative relocs we have
  4593. copied for a given symbol. */
  4594. struct bfin_pcrel_relocs_copied
  4595. {
  4596. /* Next section. */
  4597. struct bfin_pcrel_relocs_copied *next;
  4598. /* A section in dynobj. */
  4599. asection *section;
  4600. /* Number of relocs copied in this section. */
  4601. bfd_size_type count;
  4602. };
  4603. /* This function is called via elf_link_hash_traverse if we are
  4604. creating a shared object. In the -Bsymbolic case it discards the
  4605. space allocated to copy PC relative relocs against symbols which
  4606. are defined in regular objects. For the normal shared case, it
  4607. discards space for pc-relative relocs that have become local due to
  4608. symbol visibility changes. We allocated space for them in the
  4609. check_relocs routine, but we won't fill them in in the
  4610. relocate_section routine.
  4611. We also check whether any of the remaining relocations apply
  4612. against a readonly section, and set the DF_TEXTREL flag in this
  4613. case. */
  4614. static bfd_boolean
  4615. bfin_discard_copies (struct elf_link_hash_entry *h, void * inf)
  4616. {
  4617. struct bfd_link_info *info = (struct bfd_link_info *) inf;
  4618. struct bfin_pcrel_relocs_copied *s;
  4619. if (!h->def_regular || (!info->symbolic && !h->forced_local))
  4620. {
  4621. if ((info->flags & DF_TEXTREL) == 0)
  4622. {
  4623. /* Look for relocations against read-only sections. */
  4624. for (s = bfin_hash_entry (h)->pcrel_relocs_copied;
  4625. s != NULL; s = s->next)
  4626. if ((s->section->flags & SEC_READONLY) != 0)
  4627. {
  4628. info->flags |= DF_TEXTREL;
  4629. break;
  4630. }
  4631. }
  4632. return TRUE;
  4633. }
  4634. for (s = bfin_hash_entry (h)->pcrel_relocs_copied;
  4635. s != NULL; s = s->next)
  4636. s->section->size -= s->count * sizeof (Elf32_External_Rela);
  4637. return TRUE;
  4638. }
  4639. static bfd_boolean
  4640. bfin_size_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
  4641. struct bfd_link_info *info)
  4642. {
  4643. bfd *dynobj;
  4644. asection *s;
  4645. bfd_boolean relocs;
  4646. dynobj = elf_hash_table (info)->dynobj;
  4647. BFD_ASSERT (dynobj != NULL);
  4648. if (elf_hash_table (info)->dynamic_sections_created)
  4649. {
  4650. /* Set the contents of the .interp section to the interpreter. */
  4651. if (bfd_link_executable (info))
  4652. {
  4653. s = bfd_get_linker_section (dynobj, ".interp");
  4654. BFD_ASSERT (s != NULL);
  4655. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  4656. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  4657. }
  4658. }
  4659. else
  4660. {
  4661. /* We may have created entries in the .rela.got section.
  4662. However, if we are not creating the dynamic sections, we will
  4663. not actually use these entries. Reset the size of .rela.got,
  4664. which will cause it to get stripped from the output file
  4665. below. */
  4666. s = bfd_get_linker_section (dynobj, ".rela.got");
  4667. if (s != NULL)
  4668. s->size = 0;
  4669. }
  4670. /* If this is a -Bsymbolic shared link, then we need to discard all
  4671. PC relative relocs against symbols defined in a regular object.
  4672. For the normal shared case we discard the PC relative relocs
  4673. against symbols that have become local due to visibility changes.
  4674. We allocated space for them in the check_relocs routine, but we
  4675. will not fill them in in the relocate_section routine. */
  4676. if (bfd_link_pic (info))
  4677. elf_link_hash_traverse (elf_hash_table (info),
  4678. bfin_discard_copies, info);
  4679. /* The check_relocs and adjust_dynamic_symbol entry points have
  4680. determined the sizes of the various dynamic sections. Allocate
  4681. memory for them. */
  4682. relocs = FALSE;
  4683. for (s = dynobj->sections; s != NULL; s = s->next)
  4684. {
  4685. const char *name;
  4686. bfd_boolean strip;
  4687. if ((s->flags & SEC_LINKER_CREATED) == 0)
  4688. continue;
  4689. /* It's OK to base decisions on the section name, because none
  4690. of the dynobj section names depend upon the input files. */
  4691. name = bfd_get_section_name (dynobj, s);
  4692. strip = FALSE;
  4693. if (CONST_STRNEQ (name, ".rela"))
  4694. {
  4695. if (s->size == 0)
  4696. {
  4697. /* If we don't need this section, strip it from the
  4698. output file. This is mostly to handle .rela.bss and
  4699. .rela.plt. We must create both sections in
  4700. create_dynamic_sections, because they must be created
  4701. before the linker maps input sections to output
  4702. sections. The linker does that before
  4703. adjust_dynamic_symbol is called, and it is that
  4704. function which decides whether anything needs to go
  4705. into these sections. */
  4706. strip = TRUE;
  4707. }
  4708. else
  4709. {
  4710. relocs = TRUE;
  4711. /* We use the reloc_count field as a counter if we need
  4712. to copy relocs into the output file. */
  4713. s->reloc_count = 0;
  4714. }
  4715. }
  4716. else if (! CONST_STRNEQ (name, ".got"))
  4717. {
  4718. /* It's not one of our sections, so don't allocate space. */
  4719. continue;
  4720. }
  4721. if (strip)
  4722. {
  4723. s->flags |= SEC_EXCLUDE;
  4724. continue;
  4725. }
  4726. /* Allocate memory for the section contents. */
  4727. /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
  4728. Unused entries should be reclaimed before the section's contents
  4729. are written out, but at the moment this does not happen. Thus in
  4730. order to prevent writing out garbage, we initialise the section's
  4731. contents to zero. */
  4732. s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
  4733. if (s->contents == NULL && s->size != 0)
  4734. return FALSE;
  4735. }
  4736. if (elf_hash_table (info)->dynamic_sections_created)
  4737. {
  4738. /* Add some entries to the .dynamic section. We fill in the
  4739. values later, in bfin_finish_dynamic_sections, but we
  4740. must add the entries now so that we get the correct size for
  4741. the .dynamic section. The DT_DEBUG entry is filled in by the
  4742. dynamic linker and used by the debugger. */
  4743. #define add_dynamic_entry(TAG, VAL) \
  4744. _bfd_elf_add_dynamic_entry (info, TAG, VAL)
  4745. if (!bfd_link_pic (info))
  4746. {
  4747. if (!add_dynamic_entry (DT_DEBUG, 0))
  4748. return FALSE;
  4749. }
  4750. if (relocs)
  4751. {
  4752. if (!add_dynamic_entry (DT_RELA, 0)
  4753. || !add_dynamic_entry (DT_RELASZ, 0)
  4754. || !add_dynamic_entry (DT_RELAENT,
  4755. sizeof (Elf32_External_Rela)))
  4756. return FALSE;
  4757. }
  4758. if ((info->flags & DF_TEXTREL) != 0)
  4759. {
  4760. if (!add_dynamic_entry (DT_TEXTREL, 0))
  4761. return FALSE;
  4762. }
  4763. }
  4764. #undef add_dynamic_entry
  4765. return TRUE;
  4766. }
  4767. /* Given a .data section and a .emreloc in-memory section, store
  4768. relocation information into the .emreloc section which can be
  4769. used at runtime to relocate the section. This is called by the
  4770. linker when the --embedded-relocs switch is used. This is called
  4771. after the add_symbols entry point has been called for all the
  4772. objects, and before the final_link entry point is called. */
  4773. bfd_boolean
  4774. bfd_bfin_elf32_create_embedded_relocs (bfd *abfd,
  4775. struct bfd_link_info *info,
  4776. asection *datasec,
  4777. asection *relsec,
  4778. char **errmsg)
  4779. {
  4780. Elf_Internal_Shdr *symtab_hdr;
  4781. Elf_Internal_Sym *isymbuf = NULL;
  4782. Elf_Internal_Rela *internal_relocs = NULL;
  4783. Elf_Internal_Rela *irel, *irelend;
  4784. bfd_byte *p;
  4785. bfd_size_type amt;
  4786. BFD_ASSERT (! bfd_link_relocatable (info));
  4787. *errmsg = NULL;
  4788. if (datasec->reloc_count == 0)
  4789. return TRUE;
  4790. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  4791. /* Get a copy of the native relocations. */
  4792. internal_relocs = (_bfd_elf_link_read_relocs
  4793. (abfd, datasec, NULL, (Elf_Internal_Rela *) NULL,
  4794. info->keep_memory));
  4795. if (internal_relocs == NULL)
  4796. goto error_return;
  4797. amt = (bfd_size_type) datasec->reloc_count * 12;
  4798. relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
  4799. if (relsec->contents == NULL)
  4800. goto error_return;
  4801. p = relsec->contents;
  4802. irelend = internal_relocs + datasec->reloc_count;
  4803. for (irel = internal_relocs; irel < irelend; irel++, p += 12)
  4804. {
  4805. asection *targetsec;
  4806. /* We are going to write a four byte longword into the runtime
  4807. reloc section. The longword will be the address in the data
  4808. section which must be relocated. It is followed by the name
  4809. of the target section NUL-padded or truncated to 8
  4810. characters. */
  4811. /* We can only relocate absolute longword relocs at run time. */
  4812. if (ELF32_R_TYPE (irel->r_info) != (int) R_BFIN_BYTE4_DATA)
  4813. {
  4814. *errmsg = _("unsupported reloc type");
  4815. bfd_set_error (bfd_error_bad_value);
  4816. goto error_return;
  4817. }
  4818. /* Get the target section referred to by the reloc. */
  4819. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  4820. {
  4821. /* A local symbol. */
  4822. Elf_Internal_Sym *isym;
  4823. /* Read this BFD's local symbols if we haven't done so already. */
  4824. if (isymbuf == NULL)
  4825. {
  4826. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  4827. if (isymbuf == NULL)
  4828. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  4829. symtab_hdr->sh_info, 0,
  4830. NULL, NULL, NULL);
  4831. if (isymbuf == NULL)
  4832. goto error_return;
  4833. }
  4834. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  4835. targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  4836. }
  4837. else
  4838. {
  4839. unsigned long indx;
  4840. struct elf_link_hash_entry *h;
  4841. /* An external symbol. */
  4842. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  4843. h = elf_sym_hashes (abfd)[indx];
  4844. BFD_ASSERT (h != NULL);
  4845. if (h->root.type == bfd_link_hash_defined
  4846. || h->root.type == bfd_link_hash_defweak)
  4847. targetsec = h->root.u.def.section;
  4848. else
  4849. targetsec = NULL;
  4850. }
  4851. bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
  4852. memset (p + 4, 0, 8);
  4853. if (targetsec != NULL)
  4854. strncpy ((char *) p + 4, targetsec->output_section->name, 8);
  4855. }
  4856. if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
  4857. free (isymbuf);
  4858. if (internal_relocs != NULL
  4859. && elf_section_data (datasec)->relocs != internal_relocs)
  4860. free (internal_relocs);
  4861. return TRUE;
  4862. error_return:
  4863. if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
  4864. free (isymbuf);
  4865. if (internal_relocs != NULL
  4866. && elf_section_data (datasec)->relocs != internal_relocs)
  4867. free (internal_relocs);
  4868. return FALSE;
  4869. }
  4870. struct bfd_elf_special_section const elf32_bfin_special_sections[] =
  4871. {
  4872. { ".l1.text", 8, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
  4873. { ".l1.data", 8, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  4874. { NULL, 0, 0, 0, 0 }
  4875. };
  4876. #define TARGET_LITTLE_SYM bfin_elf32_vec
  4877. #define TARGET_LITTLE_NAME "elf32-bfin"
  4878. #define ELF_ARCH bfd_arch_bfin
  4879. #define ELF_TARGET_ID BFIN_ELF_DATA
  4880. #define ELF_MACHINE_CODE EM_BLACKFIN
  4881. #define ELF_MAXPAGESIZE 0x1000
  4882. #define elf_symbol_leading_char '_'
  4883. #define bfd_elf32_bfd_reloc_type_lookup bfin_bfd_reloc_type_lookup
  4884. #define bfd_elf32_bfd_reloc_name_lookup \
  4885. bfin_bfd_reloc_name_lookup
  4886. #define elf_info_to_howto bfin_info_to_howto
  4887. #define elf_info_to_howto_rel 0
  4888. #define elf_backend_object_p elf32_bfin_object_p
  4889. #define bfd_elf32_bfd_is_local_label_name \
  4890. bfin_is_local_label_name
  4891. #define bfin_hash_table(p) \
  4892. ((struct bfin_link_hash_table *) (p)->hash)
  4893. #define elf_backend_create_dynamic_sections \
  4894. _bfd_elf_create_dynamic_sections
  4895. #define bfd_elf32_bfd_link_hash_table_create \
  4896. bfin_link_hash_table_create
  4897. #define bfd_elf32_bfd_final_link bfd_elf_gc_common_final_link
  4898. #define elf_backend_check_relocs bfin_check_relocs
  4899. #define elf_backend_adjust_dynamic_symbol \
  4900. bfin_adjust_dynamic_symbol
  4901. #define elf_backend_size_dynamic_sections \
  4902. bfin_size_dynamic_sections
  4903. #define elf_backend_relocate_section bfin_relocate_section
  4904. #define elf_backend_finish_dynamic_symbol \
  4905. bfin_finish_dynamic_symbol
  4906. #define elf_backend_finish_dynamic_sections \
  4907. bfin_finish_dynamic_sections
  4908. #define elf_backend_gc_mark_hook bfin_gc_mark_hook
  4909. #define elf_backend_gc_sweep_hook bfin_gc_sweep_hook
  4910. #define bfd_elf32_bfd_merge_private_bfd_data \
  4911. elf32_bfin_merge_private_bfd_data
  4912. #define bfd_elf32_bfd_set_private_flags \
  4913. elf32_bfin_set_private_flags
  4914. #define bfd_elf32_bfd_print_private_bfd_data \
  4915. elf32_bfin_print_private_bfd_data
  4916. #define elf_backend_final_write_processing \
  4917. elf32_bfin_final_write_processing
  4918. #define elf_backend_reloc_type_class elf32_bfin_reloc_type_class
  4919. #define elf_backend_stack_align 8
  4920. #define elf_backend_can_gc_sections 1
  4921. #define elf_backend_special_sections elf32_bfin_special_sections
  4922. #define elf_backend_can_refcount 1
  4923. #define elf_backend_want_got_plt 0
  4924. #define elf_backend_plt_readonly 1
  4925. #define elf_backend_want_plt_sym 0
  4926. #define elf_backend_got_header_size 12
  4927. #define elf_backend_rela_normal 1
  4928. #include "elf32-target.h"
  4929. #undef TARGET_LITTLE_SYM
  4930. #define TARGET_LITTLE_SYM bfin_elf32_fdpic_vec
  4931. #undef TARGET_LITTLE_NAME
  4932. #define TARGET_LITTLE_NAME "elf32-bfinfdpic"
  4933. #undef elf32_bed
  4934. #define elf32_bed elf32_bfinfdpic_bed
  4935. #undef elf_backend_gc_sweep_hook
  4936. #define elf_backend_gc_sweep_hook bfinfdpic_gc_sweep_hook
  4937. #undef elf_backend_got_header_size
  4938. #define elf_backend_got_header_size 0
  4939. #undef elf_backend_relocate_section
  4940. #define elf_backend_relocate_section bfinfdpic_relocate_section
  4941. #undef elf_backend_check_relocs
  4942. #define elf_backend_check_relocs bfinfdpic_check_relocs
  4943. #undef bfd_elf32_bfd_link_hash_table_create
  4944. #define bfd_elf32_bfd_link_hash_table_create \
  4945. bfinfdpic_elf_link_hash_table_create
  4946. #undef elf_backend_always_size_sections
  4947. #define elf_backend_always_size_sections \
  4948. elf32_bfinfdpic_always_size_sections
  4949. #undef elf_backend_create_dynamic_sections
  4950. #define elf_backend_create_dynamic_sections \
  4951. elf32_bfinfdpic_create_dynamic_sections
  4952. #undef elf_backend_adjust_dynamic_symbol
  4953. #define elf_backend_adjust_dynamic_symbol \
  4954. elf32_bfinfdpic_adjust_dynamic_symbol
  4955. #undef elf_backend_size_dynamic_sections
  4956. #define elf_backend_size_dynamic_sections \
  4957. elf32_bfinfdpic_size_dynamic_sections
  4958. #undef elf_backend_finish_dynamic_symbol
  4959. #define elf_backend_finish_dynamic_symbol \
  4960. elf32_bfinfdpic_finish_dynamic_symbol
  4961. #undef elf_backend_finish_dynamic_sections
  4962. #define elf_backend_finish_dynamic_sections \
  4963. elf32_bfinfdpic_finish_dynamic_sections
  4964. #undef elf_backend_discard_info
  4965. #define elf_backend_discard_info \
  4966. bfinfdpic_elf_discard_info
  4967. #undef elf_backend_can_make_relative_eh_frame
  4968. #define elf_backend_can_make_relative_eh_frame \
  4969. bfinfdpic_elf_use_relative_eh_frame
  4970. #undef elf_backend_can_make_lsda_relative_eh_frame
  4971. #define elf_backend_can_make_lsda_relative_eh_frame \
  4972. bfinfdpic_elf_use_relative_eh_frame
  4973. #undef elf_backend_encode_eh_address
  4974. #define elf_backend_encode_eh_address \
  4975. bfinfdpic_elf_encode_eh_address
  4976. #undef elf_backend_may_use_rel_p
  4977. #define elf_backend_may_use_rel_p 1
  4978. #undef elf_backend_may_use_rela_p
  4979. #define elf_backend_may_use_rela_p 1
  4980. /* We use REL for dynamic relocations only. */
  4981. #undef elf_backend_default_use_rela_p
  4982. #define elf_backend_default_use_rela_p 1
  4983. #undef elf_backend_omit_section_dynsym
  4984. #define elf_backend_omit_section_dynsym _bfinfdpic_link_omit_section_dynsym
  4985. #include "elf32-target.h"