elf32-bfin.c 166 KB

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