elf32-nds32.c 413 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978
  1. /* NDS32-specific support for 32-bit ELF.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. Contributed by Andes Technology Corporation.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  16. 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "bfdlink.h"
  20. #include "libbfd.h"
  21. #include "elf-bfd.h"
  22. #include "libiberty.h"
  23. #include "elf/nds32.h"
  24. #include "opcode/nds32.h"
  25. #include "elf32-nds32.h"
  26. #include "opcode/cgen.h"
  27. #include "../opcodes/nds32-opc.h"
  28. /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
  29. #define OCTETS_PER_BYTE(ABFD, SEC) 1
  30. /* Relocation HOWTO functions. */
  31. static bfd_reloc_status_type nds32_elf_ignore_reloc
  32. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  33. static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
  34. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  35. static bfd_reloc_status_type nds32_elf_hi20_reloc
  36. (bfd *, arelent *, asymbol *, void *,
  37. asection *, bfd *, char **);
  38. static bfd_reloc_status_type nds32_elf_lo12_reloc
  39. (bfd *, arelent *, asymbol *, void *,
  40. asection *, bfd *, char **);
  41. static bfd_reloc_status_type nds32_elf_generic_reloc
  42. (bfd *, arelent *, asymbol *, void *,
  43. asection *, bfd *, char **);
  44. static bfd_reloc_status_type nds32_elf_sda15_reloc
  45. (bfd *, arelent *, asymbol *, void *,
  46. asection *, bfd *, char **);
  47. /* Helper functions for HOWTO. */
  48. static bfd_reloc_status_type nds32_elf_do_9_pcrel_reloc
  49. (bfd *, reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
  50. asection *, bfd_vma, bfd_vma);
  51. /* Nds32 helper functions. */
  52. static bfd_vma calculate_memory_address
  53. (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
  54. static int nds32_get_section_contents (bfd *, asection *,
  55. bfd_byte **, bool);
  56. static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
  57. Elf_Internal_Sym **);
  58. static bool nds32_relax_fp_as_gp
  59. (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
  60. Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
  61. Elf_Internal_Sym *isymbuf);
  62. static bool nds32_fag_remove_unused_fpbase
  63. (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
  64. Elf_Internal_Rela *irelend);
  65. enum
  66. {
  67. MACH_V1 = bfd_mach_n1h,
  68. MACH_V2 = bfd_mach_n1h_v2,
  69. MACH_V3 = bfd_mach_n1h_v3,
  70. MACH_V3M = bfd_mach_n1h_v3m
  71. };
  72. #define MIN(a, b) ((a) > (b) ? (b) : (a))
  73. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  74. /* The name of the dynamic interpreter. This is put in the .interp
  75. section. */
  76. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
  77. #define NDS32_GUARD_SEC_P(flags) ((flags) & SEC_ALLOC \
  78. && (flags) & SEC_LOAD \
  79. && (flags) & SEC_READONLY)
  80. /* The nop opcode we use. */
  81. #define NDS32_NOP32 0x40000009
  82. #define NDS32_NOP16 0x9200
  83. /* The size in bytes of an entry in the procedure linkage table. */
  84. #define PLT_ENTRY_SIZE 24
  85. #define PLT_HEADER_SIZE 24
  86. /* The first entry in a procedure linkage table are reserved,
  87. and the initial contents are unimportant (we zero them out).
  88. Subsequent entries look like this. */
  89. #define PLT0_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(.got+4) */
  90. #define PLT0_ENTRY_WORD1 0x58f78000 /* ori r15, r25, LO12(.got+4) */
  91. #define PLT0_ENTRY_WORD2 0x05178000 /* lwi r17, [r15+0] */
  92. #define PLT0_ENTRY_WORD3 0x04f78001 /* lwi r15, [r15+4] */
  93. #define PLT0_ENTRY_WORD4 0x4a003c00 /* jr r15 */
  94. /* $ta is change to $r15 (from $r25). */
  95. #define PLT0_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[1]@GOT) */
  96. #define PLT0_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[1]@GOT) */
  97. #define PLT0_PIC_ENTRY_WORD2 0x40f7f400 /* add r15, gp, r15 */
  98. #define PLT0_PIC_ENTRY_WORD3 0x05178000 /* lwi r17, [r15+0] */
  99. #define PLT0_PIC_ENTRY_WORD4 0x04f78001 /* lwi r15, [r15+4] */
  100. #define PLT0_PIC_ENTRY_WORD5 0x4a003c00 /* jr r15 */
  101. #define PLT_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(&got[n+3]) */
  102. #define PLT_ENTRY_WORD1 0x04f78000 /* lwi r15, r15, LO12(&got[n+3]) */
  103. #define PLT_ENTRY_WORD2 0x4a003c00 /* jr r15 */
  104. #define PLT_ENTRY_WORD3 0x45000000 /* movi r16, sizeof(RELA) * n */
  105. #define PLT_ENTRY_WORD4 0x48000000 /* j .plt0. */
  106. #define PLT_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[n+3]@GOT) */
  107. #define PLT_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[n+3]@GOT) */
  108. #define PLT_PIC_ENTRY_WORD2 0x38febc02 /* lw r15, [gp+r15] */
  109. #define PLT_PIC_ENTRY_WORD3 0x4a003c00 /* jr r15 */
  110. #define PLT_PIC_ENTRY_WORD4 0x45000000 /* movi r16, sizeof(RELA) * n */
  111. #define PLT_PIC_ENTRY_WORD5 0x48000000 /* j .plt0 */
  112. /* These are macros used to get the relocation accurate value. */
  113. #define ACCURATE_8BIT_S1 (0x100)
  114. #define ACCURATE_U9BIT_S1 (0x400)
  115. #define ACCURATE_12BIT_S1 (0x2000)
  116. #define ACCURATE_14BIT_S1 (0x4000)
  117. #define ACCURATE_19BIT (0x40000)
  118. /* These are macros used to get the relocation conservative value. */
  119. #define CONSERVATIVE_8BIT_S1 (0x100 - 4)
  120. #define CONSERVATIVE_14BIT_S1 (0x4000 - 4)
  121. #define CONSERVATIVE_16BIT_S1 (0x10000 - 4)
  122. #define CONSERVATIVE_24BIT_S1 (0x1000000 - 4)
  123. /* These must be more conservative because the address may be in
  124. different segment. */
  125. #define CONSERVATIVE_15BIT (0x4000 - 0x1000)
  126. #define CONSERVATIVE_15BIT_S1 (0x8000 - 0x1000)
  127. #define CONSERVATIVE_15BIT_S2 (0x10000 - 0x1000)
  128. #define CONSERVATIVE_19BIT (0x40000 - 0x1000)
  129. #define CONSERVATIVE_20BIT (0x80000 - 0x1000)
  130. /* Size of small data/bss sections, used to calculate SDA_BASE. */
  131. static long got_size = 0;
  132. static int is_SDA_BASE_set = 0;
  133. /* Convert ELF-VER in eflags to string for debugging purpose. */
  134. static const char *const nds32_elfver_strtab[] =
  135. {
  136. "ELF-1.2",
  137. "ELF-1.3",
  138. "ELF-1.4",
  139. };
  140. /* The nds32 linker needs to keep track of the number of relocs that it
  141. decides to copy in check_relocs for each symbol. This is so that
  142. it can discard PC relative relocs if it doesn't need them when
  143. linking with -Bsymbolic. We store the information in a field
  144. extending the regular ELF linker hash table. */
  145. /* This structure keeps track of the number of PC relative relocs we
  146. have copied for a given symbol. */
  147. struct elf_nds32_pcrel_relocs_copied
  148. {
  149. /* Next section. */
  150. struct elf_nds32_pcrel_relocs_copied *next;
  151. /* A section in dynobj. */
  152. asection *section;
  153. /* Number of relocs copied in this section. */
  154. bfd_size_type count;
  155. };
  156. enum elf_nds32_tls_type
  157. {
  158. GOT_UNKNOWN = (0),
  159. GOT_NORMAL = (1 << 0),
  160. GOT_TLS_LE = (1 << 1),
  161. GOT_TLS_IE = (1 << 2),
  162. GOT_TLS_IEGP = (1 << 3),
  163. GOT_TLS_LD = (1 << 4),
  164. GOT_TLS_GD = (1 << 5),
  165. GOT_TLS_DESC = (1 << 6),
  166. };
  167. /* Nds32 ELF linker hash entry. */
  168. struct elf_nds32_link_hash_entry
  169. {
  170. struct elf_link_hash_entry root;
  171. /* For checking relocation type. */
  172. enum elf_nds32_tls_type tls_type;
  173. int offset_to_gp;
  174. };
  175. /* Get the nds32 ELF linker hash table from a link_info structure. */
  176. #define FP_BASE_NAME "_FP_BASE_"
  177. static int check_start_export_sym = 0;
  178. /* The offset for executable tls relaxation. */
  179. #define TP_OFFSET 0x0
  180. typedef struct
  181. {
  182. int min_id;
  183. int max_id;
  184. int count;
  185. int bias;
  186. int init;
  187. } elf32_nds32_relax_group_t;
  188. struct elf_nds32_obj_tdata
  189. {
  190. struct elf_obj_tdata root;
  191. /* tls_type for each local got entry. */
  192. char *local_got_tls_type;
  193. /* GOTPLT entries for TLS descriptors. */
  194. bfd_vma *local_tlsdesc_gotent;
  195. /* for R_NDS32_RELAX_GROUP handling. */
  196. elf32_nds32_relax_group_t relax_group;
  197. unsigned int hdr_size;
  198. int* offset_to_gp;
  199. };
  200. #define elf_nds32_tdata(bfd) \
  201. ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
  202. #define elf32_nds32_local_got_tls_type(bfd) \
  203. (elf_nds32_tdata (bfd)->local_got_tls_type)
  204. #define elf32_nds32_local_gp_offset(bfd) \
  205. (elf_nds32_tdata (bfd)->offset_to_gp)
  206. #define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
  207. #define elf32_nds32_relax_group_ptr(bfd) \
  208. &(elf_nds32_tdata (bfd)->relax_group)
  209. static bool
  210. nds32_elf_mkobject (bfd *abfd)
  211. {
  212. return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
  213. NDS32_ELF_DATA);
  214. }
  215. /* Relocations used for relocation. */
  216. /* Define HOWTO2 (for relocation) and HOWTO3 (for relaxation) to
  217. initialize array nds32_elf_howto_table in any order. The benefit
  218. is that we can add any new relocations with any numbers and don't
  219. need to fill the gap by lots of EMPTY_HOWTO. */
  220. #define HOWTO2(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
  221. [C] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
  222. static reloc_howto_type nds32_elf_howto_table[] =
  223. {
  224. /* This reloc does nothing. */
  225. HOWTO2 (R_NDS32_NONE, /* type */
  226. 0, /* rightshift */
  227. 2, /* size (0 = byte, 1 = short, 2 = long) */
  228. 32, /* bitsize */
  229. false, /* pc_relative */
  230. 0, /* bitpos */
  231. complain_overflow_bitfield,/* complain_on_overflow */
  232. bfd_elf_generic_reloc, /* special_function */
  233. "R_NDS32_NONE", /* name */
  234. false, /* partial_inplace */
  235. 0, /* src_mask */
  236. 0, /* dst_mask */
  237. false), /* pcrel_offset */
  238. /* A 16 bit absolute relocation. */
  239. HOWTO2 (R_NDS32_16, /* type */
  240. 0, /* rightshift */
  241. 1, /* size (0 = byte, 1 = short, 2 = long) */
  242. 16, /* bitsize */
  243. false, /* pc_relative */
  244. 0, /* bitpos */
  245. complain_overflow_bitfield,/* complain_on_overflow */
  246. nds32_elf_generic_reloc,/* special_function */
  247. "R_NDS32_16", /* name */
  248. false, /* partial_inplace */
  249. 0xffff, /* src_mask */
  250. 0xffff, /* dst_mask */
  251. false), /* pcrel_offset */
  252. /* A 32 bit absolute relocation. */
  253. HOWTO2 (R_NDS32_32, /* type */
  254. 0, /* rightshift */
  255. 2, /* size (0 = byte, 1 = short, 2 = long) */
  256. 32, /* bitsize */
  257. false, /* pc_relative */
  258. 0, /* bitpos */
  259. complain_overflow_bitfield,/* complain_on_overflow */
  260. nds32_elf_generic_reloc,/* special_function */
  261. "R_NDS32_32", /* name */
  262. false, /* partial_inplace */
  263. 0xffffffff, /* src_mask */
  264. 0xffffffff, /* dst_mask */
  265. false), /* pcrel_offset */
  266. /* A 20 bit address. */
  267. HOWTO2 (R_NDS32_20, /* type */
  268. 0, /* rightshift */
  269. 2, /* size (0 = byte, 1 = short, 2 = long) */
  270. 20, /* bitsize */
  271. false, /* pc_relative */
  272. 0, /* bitpos */
  273. complain_overflow_unsigned,/* complain_on_overflow */
  274. nds32_elf_generic_reloc,/* special_function */
  275. "R_NDS32_20", /* name */
  276. false, /* partial_inplace */
  277. 0xfffff, /* src_mask */
  278. 0xfffff, /* dst_mask */
  279. false), /* pcrel_offset */
  280. /* An PC Relative 9-bit relocation, shifted by 2.
  281. This reloc is complicated because relocations are relative to pc & -4.
  282. i.e. branches in the right insn slot use the address of the left insn
  283. slot for pc. */
  284. /* It's not clear whether this should have partial_inplace set or not.
  285. Branch relaxing in the assembler can store the addend in the insn,
  286. and if bfd_install_relocation gets called the addend may get added
  287. again. */
  288. HOWTO2 (R_NDS32_9_PCREL, /* type */
  289. 1, /* rightshift */
  290. 1, /* size (0 = byte, 1 = short, 2 = long) */
  291. 8, /* bitsize */
  292. true, /* pc_relative */
  293. 0, /* bitpos */
  294. complain_overflow_signed,/* complain_on_overflow */
  295. nds32_elf_9_pcrel_reloc,/* special_function */
  296. "R_NDS32_9_PCREL", /* name */
  297. false, /* partial_inplace */
  298. 0xff, /* src_mask */
  299. 0xff, /* dst_mask */
  300. true), /* pcrel_offset */
  301. /* A relative 15 bit relocation, right shifted by 1. */
  302. HOWTO2 (R_NDS32_15_PCREL, /* type */
  303. 1, /* rightshift */
  304. 2, /* size (0 = byte, 1 = short, 2 = long) */
  305. 14, /* bitsize */
  306. true, /* pc_relative */
  307. 0, /* bitpos */
  308. complain_overflow_signed,/* complain_on_overflow */
  309. bfd_elf_generic_reloc, /* special_function */
  310. "R_NDS32_15_PCREL", /* name */
  311. false, /* partial_inplace */
  312. 0x3fff, /* src_mask */
  313. 0x3fff, /* dst_mask */
  314. true), /* pcrel_offset */
  315. /* A relative 17 bit relocation, right shifted by 1. */
  316. HOWTO2 (R_NDS32_17_PCREL, /* type */
  317. 1, /* rightshift */
  318. 2, /* size (0 = byte, 1 = short, 2 = long) */
  319. 16, /* bitsize */
  320. true, /* pc_relative */
  321. 0, /* bitpos */
  322. complain_overflow_signed,/* complain_on_overflow */
  323. bfd_elf_generic_reloc, /* special_function */
  324. "R_NDS32_17_PCREL", /* name */
  325. false, /* partial_inplace */
  326. 0xffff, /* src_mask */
  327. 0xffff, /* dst_mask */
  328. true), /* pcrel_offset */
  329. /* A relative 25 bit relocation, right shifted by 1. */
  330. /* It's not clear whether this should have partial_inplace set or not.
  331. Branch relaxing in the assembler can store the addend in the insn,
  332. and if bfd_install_relocation gets called the addend may get added
  333. again. */
  334. HOWTO2 (R_NDS32_25_PCREL, /* type */
  335. 1, /* rightshift */
  336. 2, /* size (0 = byte, 1 = short, 2 = long) */
  337. 24, /* bitsize */
  338. true, /* pc_relative */
  339. 0, /* bitpos */
  340. complain_overflow_signed,/* complain_on_overflow */
  341. bfd_elf_generic_reloc, /* special_function */
  342. "R_NDS32_25_PCREL", /* name */
  343. false, /* partial_inplace */
  344. 0xffffff, /* src_mask */
  345. 0xffffff, /* dst_mask */
  346. true), /* pcrel_offset */
  347. /* High 20 bits of address when lower 12 is or'd in. */
  348. HOWTO2 (R_NDS32_HI20, /* type */
  349. 12, /* rightshift */
  350. 2, /* size (0 = byte, 1 = short, 2 = long) */
  351. 20, /* bitsize */
  352. false, /* pc_relative */
  353. 0, /* bitpos */
  354. complain_overflow_dont,/* complain_on_overflow */
  355. nds32_elf_hi20_reloc, /* special_function */
  356. "R_NDS32_HI20", /* name */
  357. false, /* partial_inplace */
  358. 0x000fffff, /* src_mask */
  359. 0x000fffff, /* dst_mask */
  360. false), /* pcrel_offset */
  361. /* Lower 12 bits of address. */
  362. HOWTO2 (R_NDS32_LO12S3, /* type */
  363. 3, /* rightshift */
  364. 2, /* size (0 = byte, 1 = short, 2 = long) */
  365. 9, /* bitsize */
  366. false, /* pc_relative */
  367. 0, /* bitpos */
  368. complain_overflow_dont,/* complain_on_overflow */
  369. nds32_elf_lo12_reloc, /* special_function */
  370. "R_NDS32_LO12S3", /* name */
  371. false, /* partial_inplace */
  372. 0x000001ff, /* src_mask */
  373. 0x000001ff, /* dst_mask */
  374. false), /* pcrel_offset */
  375. /* Lower 12 bits of address. */
  376. HOWTO2 (R_NDS32_LO12S2, /* type */
  377. 2, /* rightshift */
  378. 2, /* size (0 = byte, 1 = short, 2 = long) */
  379. 10, /* bitsize */
  380. false, /* pc_relative */
  381. 0, /* bitpos */
  382. complain_overflow_dont,/* complain_on_overflow */
  383. nds32_elf_lo12_reloc, /* special_function */
  384. "R_NDS32_LO12S2", /* name */
  385. false, /* partial_inplace */
  386. 0x000003ff, /* src_mask */
  387. 0x000003ff, /* dst_mask */
  388. false), /* pcrel_offset */
  389. /* Lower 12 bits of address. */
  390. HOWTO2 (R_NDS32_LO12S1, /* type */
  391. 1, /* rightshift */
  392. 2, /* size (0 = byte, 1 = short, 2 = long) */
  393. 11, /* bitsize */
  394. false, /* pc_relative */
  395. 0, /* bitpos */
  396. complain_overflow_dont,/* complain_on_overflow */
  397. nds32_elf_lo12_reloc, /* special_function */
  398. "R_NDS32_LO12S1", /* name */
  399. false, /* partial_inplace */
  400. 0x000007ff, /* src_mask */
  401. 0x000007ff, /* dst_mask */
  402. false), /* pcrel_offset */
  403. /* Lower 12 bits of address. */
  404. HOWTO2 (R_NDS32_LO12S0, /* type */
  405. 0, /* rightshift */
  406. 2, /* size (0 = byte, 1 = short, 2 = long) */
  407. 12, /* bitsize */
  408. false, /* pc_relative */
  409. 0, /* bitpos */
  410. complain_overflow_dont,/* complain_on_overflow */
  411. nds32_elf_lo12_reloc, /* special_function */
  412. "R_NDS32_LO12S0", /* name */
  413. false, /* partial_inplace */
  414. 0x00000fff, /* src_mask */
  415. 0x00000fff, /* dst_mask */
  416. false), /* pcrel_offset */
  417. /* Small data area 15 bits offset. */
  418. HOWTO2 (R_NDS32_SDA15S3, /* type */
  419. 3, /* rightshift */
  420. 2, /* size (0 = byte, 1 = short, 2 = long) */
  421. 15, /* bitsize */
  422. false, /* pc_relative */
  423. 0, /* bitpos */
  424. complain_overflow_signed,/* complain_on_overflow */
  425. nds32_elf_sda15_reloc, /* special_function */
  426. "R_NDS32_SDA15S3", /* name */
  427. false, /* partial_inplace */
  428. 0x00007fff, /* src_mask */
  429. 0x00007fff, /* dst_mask */
  430. false), /* pcrel_offset */
  431. /* Small data area 15 bits offset. */
  432. HOWTO2 (R_NDS32_SDA15S2, /* type */
  433. 2, /* rightshift */
  434. 2, /* size (0 = byte, 1 = short, 2 = long) */
  435. 15, /* bitsize */
  436. false, /* pc_relative */
  437. 0, /* bitpos */
  438. complain_overflow_signed,/* complain_on_overflow */
  439. nds32_elf_sda15_reloc, /* special_function */
  440. "R_NDS32_SDA15S2", /* name */
  441. false, /* partial_inplace */
  442. 0x00007fff, /* src_mask */
  443. 0x00007fff, /* dst_mask */
  444. false), /* pcrel_offset */
  445. /* Small data area 15 bits offset. */
  446. HOWTO2 (R_NDS32_SDA15S1, /* type */
  447. 1, /* rightshift */
  448. 2, /* size (0 = byte, 1 = short, 2 = long) */
  449. 15, /* bitsize */
  450. false, /* pc_relative */
  451. 0, /* bitpos */
  452. complain_overflow_signed,/* complain_on_overflow */
  453. nds32_elf_sda15_reloc, /* special_function */
  454. "R_NDS32_SDA15S1", /* name */
  455. false, /* partial_inplace */
  456. 0x00007fff, /* src_mask */
  457. 0x00007fff, /* dst_mask */
  458. false), /* pcrel_offset */
  459. /* Small data area 15 bits offset. */
  460. HOWTO2 (R_NDS32_SDA15S0, /* type */
  461. 0, /* rightshift */
  462. 2, /* size (0 = byte, 1 = short, 2 = long) */
  463. 15, /* bitsize */
  464. false, /* pc_relative */
  465. 0, /* bitpos */
  466. complain_overflow_signed,/* complain_on_overflow */
  467. nds32_elf_sda15_reloc, /* special_function */
  468. "R_NDS32_SDA15S0", /* name */
  469. false, /* partial_inplace */
  470. 0x00007fff, /* src_mask */
  471. 0x00007fff, /* dst_mask */
  472. false), /* pcrel_offset */
  473. /* GNU extension to record C++ vtable hierarchy */
  474. HOWTO2 (R_NDS32_GNU_VTINHERIT,/* type */
  475. 0, /* rightshift */
  476. 2, /* size (0 = byte, 1 = short, 2 = long) */
  477. 0, /* bitsize */
  478. false, /* pc_relative */
  479. 0, /* bitpos */
  480. complain_overflow_dont,/* complain_on_overflow */
  481. NULL, /* special_function */
  482. "R_NDS32_GNU_VTINHERIT",/* name */
  483. false, /* partial_inplace */
  484. 0, /* src_mask */
  485. 0, /* dst_mask */
  486. false), /* pcrel_offset */
  487. /* GNU extension to record C++ vtable member usage */
  488. HOWTO2 (R_NDS32_GNU_VTENTRY, /* type */
  489. 0, /* rightshift */
  490. 2, /* size (0 = byte, 1 = short, 2 = long) */
  491. 0, /* bitsize */
  492. false, /* pc_relative */
  493. 0, /* bitpos */
  494. complain_overflow_dont,/* complain_on_overflow */
  495. _bfd_elf_rel_vtable_reloc_fn,/* special_function */
  496. "R_NDS32_GNU_VTENTRY", /* name */
  497. false, /* partial_inplace */
  498. 0, /* src_mask */
  499. 0, /* dst_mask */
  500. false), /* pcrel_offset */
  501. /* A 16 bit absolute relocation. */
  502. HOWTO2 (R_NDS32_16_RELA, /* type */
  503. 0, /* rightshift */
  504. 1, /* size (0 = byte, 1 = short, 2 = long) */
  505. 16, /* bitsize */
  506. false, /* pc_relative */
  507. 0, /* bitpos */
  508. complain_overflow_bitfield,/* complain_on_overflow */
  509. bfd_elf_generic_reloc, /* special_function */
  510. "R_NDS32_16_RELA", /* name */
  511. false, /* partial_inplace */
  512. 0xffff, /* src_mask */
  513. 0xffff, /* dst_mask */
  514. false), /* pcrel_offset */
  515. /* A 32 bit absolute relocation. */
  516. HOWTO2 (R_NDS32_32_RELA, /* type */
  517. 0, /* rightshift */
  518. 2, /* size (0 = byte, 1 = short, 2 = long) */
  519. 32, /* bitsize */
  520. false, /* pc_relative */
  521. 0, /* bitpos */
  522. complain_overflow_bitfield,/* complain_on_overflow */
  523. bfd_elf_generic_reloc, /* special_function */
  524. "R_NDS32_32_RELA", /* name */
  525. false, /* partial_inplace */
  526. 0xffffffff, /* src_mask */
  527. 0xffffffff, /* dst_mask */
  528. false), /* pcrel_offset */
  529. /* A 20 bit address. */
  530. HOWTO2 (R_NDS32_20_RELA, /* type */
  531. 0, /* rightshift */
  532. 2, /* size (0 = byte, 1 = short, 2 = long) */
  533. 20, /* bitsize */
  534. false, /* pc_relative */
  535. 0, /* bitpos */
  536. complain_overflow_signed,/* complain_on_overflow */
  537. bfd_elf_generic_reloc, /* special_function */
  538. "R_NDS32_20_RELA", /* name */
  539. false, /* partial_inplace */
  540. 0xfffff, /* src_mask */
  541. 0xfffff, /* dst_mask */
  542. false), /* pcrel_offset */
  543. HOWTO2 (R_NDS32_9_PCREL_RELA, /* type */
  544. 1, /* rightshift */
  545. 1, /* size (0 = byte, 1 = short, 2 = long) */
  546. 8, /* bitsize */
  547. true, /* pc_relative */
  548. 0, /* bitpos */
  549. complain_overflow_signed,/* complain_on_overflow */
  550. bfd_elf_generic_reloc, /* special_function */
  551. "R_NDS32_9_PCREL_RELA",/* name */
  552. false, /* partial_inplace */
  553. 0xff, /* src_mask */
  554. 0xff, /* dst_mask */
  555. true), /* pcrel_offset */
  556. /* A relative 15 bit relocation, right shifted by 1. */
  557. HOWTO2 (R_NDS32_15_PCREL_RELA,/* type */
  558. 1, /* rightshift */
  559. 2, /* size (0 = byte, 1 = short, 2 = long) */
  560. 14, /* bitsize */
  561. true, /* pc_relative */
  562. 0, /* bitpos */
  563. complain_overflow_signed,/* complain_on_overflow */
  564. bfd_elf_generic_reloc, /* special_function */
  565. "R_NDS32_15_PCREL_RELA",/* name */
  566. false, /* partial_inplace */
  567. 0x3fff, /* src_mask */
  568. 0x3fff, /* dst_mask */
  569. true), /* pcrel_offset */
  570. /* A relative 17 bit relocation, right shifted by 1. */
  571. HOWTO2 (R_NDS32_17_PCREL_RELA,/* type */
  572. 1, /* rightshift */
  573. 2, /* size (0 = byte, 1 = short, 2 = long) */
  574. 16, /* bitsize */
  575. true, /* pc_relative */
  576. 0, /* bitpos */
  577. complain_overflow_signed,/* complain_on_overflow */
  578. bfd_elf_generic_reloc, /* special_function */
  579. "R_NDS32_17_PCREL_RELA",/* name */
  580. false, /* partial_inplace */
  581. 0xffff, /* src_mask */
  582. 0xffff, /* dst_mask */
  583. true), /* pcrel_offset */
  584. /* A relative 25 bit relocation, right shifted by 2. */
  585. HOWTO2 (R_NDS32_25_PCREL_RELA,/* type */
  586. 1, /* rightshift */
  587. 2, /* size (0 = byte, 1 = short, 2 = long) */
  588. 24, /* bitsize */
  589. true, /* pc_relative */
  590. 0, /* bitpos */
  591. complain_overflow_signed,/* complain_on_overflow */
  592. bfd_elf_generic_reloc, /* special_function */
  593. "R_NDS32_25_PCREL_RELA",/* name */
  594. false, /* partial_inplace */
  595. 0xffffff, /* src_mask */
  596. 0xffffff, /* dst_mask */
  597. true), /* pcrel_offset */
  598. /* High 20 bits of address when lower 16 is or'd in. */
  599. HOWTO2 (R_NDS32_HI20_RELA, /* type */
  600. 12, /* rightshift */
  601. 2, /* size (0 = byte, 1 = short, 2 = long) */
  602. 20, /* bitsize */
  603. false, /* pc_relative */
  604. 0, /* bitpos */
  605. complain_overflow_dont,/* complain_on_overflow */
  606. bfd_elf_generic_reloc, /* special_function */
  607. "R_NDS32_HI20_RELA", /* name */
  608. false, /* partial_inplace */
  609. 0x000fffff, /* src_mask */
  610. 0x000fffff, /* dst_mask */
  611. false), /* pcrel_offset */
  612. /* Lower 12 bits of address. */
  613. HOWTO2 (R_NDS32_LO12S3_RELA, /* type */
  614. 3, /* rightshift */
  615. 2, /* size (0 = byte, 1 = short, 2 = long) */
  616. 9, /* bitsize */
  617. false, /* pc_relative */
  618. 0, /* bitpos */
  619. complain_overflow_dont,/* complain_on_overflow */
  620. bfd_elf_generic_reloc, /* special_function */
  621. "R_NDS32_LO12S3_RELA", /* name */
  622. false, /* partial_inplace */
  623. 0x000001ff, /* src_mask */
  624. 0x000001ff, /* dst_mask */
  625. false), /* pcrel_offset */
  626. /* Lower 12 bits of address. */
  627. HOWTO2 (R_NDS32_LO12S2_RELA, /* type */
  628. 2, /* rightshift */
  629. 2, /* size (0 = byte, 1 = short, 2 = long) */
  630. 10, /* bitsize */
  631. false, /* pc_relative */
  632. 0, /* bitpos */
  633. complain_overflow_dont,/* complain_on_overflow */
  634. bfd_elf_generic_reloc, /* special_function */
  635. "R_NDS32_LO12S2_RELA", /* name */
  636. false, /* partial_inplace */
  637. 0x000003ff, /* src_mask */
  638. 0x000003ff, /* dst_mask */
  639. false), /* pcrel_offset */
  640. /* Lower 12 bits of address. */
  641. HOWTO2 (R_NDS32_LO12S1_RELA, /* type */
  642. 1, /* rightshift */
  643. 2, /* size (0 = byte, 1 = short, 2 = long) */
  644. 11, /* bitsize */
  645. false, /* pc_relative */
  646. 0, /* bitpos */
  647. complain_overflow_dont,/* complain_on_overflow */
  648. bfd_elf_generic_reloc, /* special_function */
  649. "R_NDS32_LO12S1_RELA", /* name */
  650. false, /* partial_inplace */
  651. 0x000007ff, /* src_mask */
  652. 0x000007ff, /* dst_mask */
  653. false), /* pcrel_offset */
  654. /* Lower 12 bits of address. */
  655. HOWTO2 (R_NDS32_LO12S0_RELA, /* type */
  656. 0, /* rightshift */
  657. 2, /* size (0 = byte, 1 = short, 2 = long) */
  658. 12, /* bitsize */
  659. false, /* pc_relative */
  660. 0, /* bitpos */
  661. complain_overflow_dont,/* complain_on_overflow */
  662. bfd_elf_generic_reloc, /* special_function */
  663. "R_NDS32_LO12S0_RELA", /* name */
  664. false, /* partial_inplace */
  665. 0x00000fff, /* src_mask */
  666. 0x00000fff, /* dst_mask */
  667. false), /* pcrel_offset */
  668. /* Small data area 15 bits offset. */
  669. HOWTO2 (R_NDS32_SDA15S3_RELA, /* type */
  670. 3, /* rightshift */
  671. 2, /* size (0 = byte, 1 = short, 2 = long) */
  672. 15, /* bitsize */
  673. false, /* pc_relative */
  674. 0, /* bitpos */
  675. complain_overflow_signed,/* complain_on_overflow */
  676. bfd_elf_generic_reloc, /* special_function */
  677. "R_NDS32_SDA15S3_RELA",/* name */
  678. false, /* partial_inplace */
  679. 0x00007fff, /* src_mask */
  680. 0x00007fff, /* dst_mask */
  681. false), /* pcrel_offset */
  682. /* Small data area 15 bits offset. */
  683. HOWTO2 (R_NDS32_SDA15S2_RELA, /* type */
  684. 2, /* rightshift */
  685. 2, /* size (0 = byte, 1 = short, 2 = long) */
  686. 15, /* bitsize */
  687. false, /* pc_relative */
  688. 0, /* bitpos */
  689. complain_overflow_signed,/* complain_on_overflow */
  690. bfd_elf_generic_reloc, /* special_function */
  691. "R_NDS32_SDA15S2_RELA",/* name */
  692. false, /* partial_inplace */
  693. 0x00007fff, /* src_mask */
  694. 0x00007fff, /* dst_mask */
  695. false), /* pcrel_offset */
  696. HOWTO2 (R_NDS32_SDA15S1_RELA, /* type */
  697. 1, /* rightshift */
  698. 2, /* size (0 = byte, 1 = short, 2 = long) */
  699. 15, /* bitsize */
  700. false, /* pc_relative */
  701. 0, /* bitpos */
  702. complain_overflow_signed,/* complain_on_overflow */
  703. bfd_elf_generic_reloc, /* special_function */
  704. "R_NDS32_SDA15S1_RELA",/* name */
  705. false, /* partial_inplace */
  706. 0x00007fff, /* src_mask */
  707. 0x00007fff, /* dst_mask */
  708. false), /* pcrel_offset */
  709. HOWTO2 (R_NDS32_SDA15S0_RELA, /* type */
  710. 0, /* rightshift */
  711. 2, /* size (0 = byte, 1 = short, 2 = long) */
  712. 15, /* bitsize */
  713. false, /* pc_relative */
  714. 0, /* bitpos */
  715. complain_overflow_signed,/* complain_on_overflow */
  716. bfd_elf_generic_reloc, /* special_function */
  717. "R_NDS32_SDA15S0_RELA",/* name */
  718. false, /* partial_inplace */
  719. 0x00007fff, /* src_mask */
  720. 0x00007fff, /* dst_mask */
  721. false), /* pcrel_offset */
  722. /* GNU extension to record C++ vtable hierarchy */
  723. HOWTO2 (R_NDS32_RELA_GNU_VTINHERIT,/* type */
  724. 0, /* rightshift */
  725. 2, /* size (0 = byte, 1 = short, 2 = long) */
  726. 0, /* bitsize */
  727. false, /* pc_relative */
  728. 0, /* bitpos */
  729. complain_overflow_dont,/* complain_on_overflow */
  730. NULL, /* special_function */
  731. "R_NDS32_RELA_GNU_VTINHERIT",/* name */
  732. false, /* partial_inplace */
  733. 0, /* src_mask */
  734. 0, /* dst_mask */
  735. false), /* pcrel_offset */
  736. /* GNU extension to record C++ vtable member usage */
  737. HOWTO2 (R_NDS32_RELA_GNU_VTENTRY,/* type */
  738. 0, /* rightshift */
  739. 2, /* size (0 = byte, 1 = short, 2 = long) */
  740. 0, /* bitsize */
  741. false, /* pc_relative */
  742. 0, /* bitpos */
  743. complain_overflow_dont,/* complain_on_overflow */
  744. _bfd_elf_rel_vtable_reloc_fn,/* special_function */
  745. "R_NDS32_RELA_GNU_VTENTRY",/* name */
  746. false, /* partial_inplace */
  747. 0, /* src_mask */
  748. 0, /* dst_mask */
  749. false), /* pcrel_offset */
  750. /* Like R_NDS32_20, but referring to the GOT table entry for
  751. the symbol. */
  752. HOWTO2 (R_NDS32_GOT20, /* type */
  753. 0, /* rightshift */
  754. 2, /* size (0 = byte, 1 = short, 2 = long) */
  755. 20, /* bitsize */
  756. false, /* pc_relative */
  757. 0, /* bitpos */
  758. complain_overflow_signed,/* complain_on_overflow */
  759. bfd_elf_generic_reloc, /* special_function */
  760. "R_NDS32_GOT20", /* name */
  761. false, /* partial_inplace */
  762. 0xfffff, /* src_mask */
  763. 0xfffff, /* dst_mask */
  764. false), /* pcrel_offset */
  765. /* Like R_NDS32_PCREL, but referring to the procedure linkage table
  766. entry for the symbol. */
  767. HOWTO2 (R_NDS32_25_PLTREL, /* type */
  768. 1, /* rightshift */
  769. 2, /* size (0 = byte, 1 = short, 2 = long) */
  770. 24, /* bitsize */
  771. true, /* pc_relative */
  772. 0, /* bitpos */
  773. complain_overflow_signed,/* complain_on_overflow */
  774. bfd_elf_generic_reloc, /* special_function */
  775. "R_NDS32_25_PLTREL", /* name */
  776. false, /* partial_inplace */
  777. 0xffffff, /* src_mask */
  778. 0xffffff, /* dst_mask */
  779. true), /* pcrel_offset */
  780. /* This is used only by the dynamic linker. The symbol should exist
  781. both in the object being run and in some shared library. The
  782. dynamic linker copies the data addressed by the symbol from the
  783. shared library into the object, because the object being
  784. run has to have the data at some particular address. */
  785. HOWTO2 (R_NDS32_COPY, /* type */
  786. 0, /* rightshift */
  787. 2, /* size (0 = byte, 1 = short, 2 = long) */
  788. 32, /* bitsize */
  789. false, /* pc_relative */
  790. 0, /* bitpos */
  791. complain_overflow_bitfield,/* complain_on_overflow */
  792. bfd_elf_generic_reloc, /* special_function */
  793. "R_NDS32_COPY", /* name */
  794. false, /* partial_inplace */
  795. 0xffffffff, /* src_mask */
  796. 0xffffffff, /* dst_mask */
  797. false), /* pcrel_offset */
  798. /* Like R_NDS32_20, but used when setting global offset table
  799. entries. */
  800. HOWTO2 (R_NDS32_GLOB_DAT, /* type */
  801. 0, /* rightshift */
  802. 2, /* size (0 = byte, 1 = short, 2 = long) */
  803. 32, /* bitsize */
  804. false, /* pc_relative */
  805. 0, /* bitpos */
  806. complain_overflow_bitfield,/* complain_on_overflow */
  807. bfd_elf_generic_reloc, /* special_function */
  808. "R_NDS32_GLOB_DAT", /* name */
  809. false, /* partial_inplace */
  810. 0xffffffff, /* src_mask */
  811. 0xffffffff, /* dst_mask */
  812. false), /* pcrel_offset */
  813. /* Marks a procedure linkage table entry for a symbol. */
  814. HOWTO2 (R_NDS32_JMP_SLOT, /* type */
  815. 0, /* rightshift */
  816. 2, /* size (0 = byte, 1 = short, 2 = long) */
  817. 32, /* bitsize */
  818. false, /* pc_relative */
  819. 0, /* bitpos */
  820. complain_overflow_bitfield,/* complain_on_overflow */
  821. bfd_elf_generic_reloc, /* special_function */
  822. "R_NDS32_JMP_SLOT", /* name */
  823. false, /* partial_inplace */
  824. 0xffffffff, /* src_mask */
  825. 0xffffffff, /* dst_mask */
  826. false), /* pcrel_offset */
  827. /* Used only by the dynamic linker. When the object is run, this
  828. longword is set to the load address of the object, plus the
  829. addend. */
  830. HOWTO2 (R_NDS32_RELATIVE, /* type */
  831. 0, /* rightshift */
  832. 2, /* size (0 = byte, 1 = short, 2 = long) */
  833. 32, /* bitsize */
  834. false, /* pc_relative */
  835. 0, /* bitpos */
  836. complain_overflow_bitfield,/* complain_on_overflow */
  837. bfd_elf_generic_reloc, /* special_function */
  838. "R_NDS32_RELATIVE", /* name */
  839. false, /* partial_inplace */
  840. 0xffffffff, /* src_mask */
  841. 0xffffffff, /* dst_mask */
  842. false), /* pcrel_offset */
  843. HOWTO2 (R_NDS32_GOTOFF, /* type */
  844. 0, /* rightshift */
  845. 2, /* size (0 = byte, 1 = short, 2 = long) */
  846. 20, /* bitsize */
  847. false, /* pc_relative */
  848. 0, /* bitpos */
  849. complain_overflow_signed,/* complain_on_overflow */
  850. bfd_elf_generic_reloc, /* special_function */
  851. "R_NDS32_GOTOFF", /* name */
  852. false, /* partial_inplace */
  853. 0xfffff, /* src_mask */
  854. 0xfffff, /* dst_mask */
  855. false), /* pcrel_offset */
  856. /* An PC Relative 20-bit relocation used when setting PIC offset
  857. table register. */
  858. HOWTO2 (R_NDS32_GOTPC20, /* type */
  859. 0, /* rightshift */
  860. 2, /* size (0 = byte, 1 = short, 2 = long) */
  861. 20, /* bitsize */
  862. true, /* pc_relative */
  863. 0, /* bitpos */
  864. complain_overflow_signed,/* complain_on_overflow */
  865. bfd_elf_generic_reloc, /* special_function */
  866. "R_NDS32_GOTPC20", /* name */
  867. false, /* partial_inplace */
  868. 0xfffff, /* src_mask */
  869. 0xfffff, /* dst_mask */
  870. true), /* pcrel_offset */
  871. /* Like R_NDS32_HI20, but referring to the GOT table entry for
  872. the symbol. */
  873. HOWTO2 (R_NDS32_GOT_HI20, /* type */
  874. 12, /* rightshift */
  875. 2, /* size (0 = byte, 1 = short, 2 = long) */
  876. 20, /* bitsize */
  877. false, /* pc_relative */
  878. 0, /* bitpos */
  879. complain_overflow_dont,/* complain_on_overflow */
  880. bfd_elf_generic_reloc, /* special_function */
  881. "R_NDS32_GOT_HI20", /* name */
  882. false, /* partial_inplace */
  883. 0x000fffff, /* src_mask */
  884. 0x000fffff, /* dst_mask */
  885. false), /* pcrel_offset */
  886. HOWTO2 (R_NDS32_GOT_LO12, /* type */
  887. 0, /* rightshift */
  888. 2, /* size (0 = byte, 1 = short, 2 = long) */
  889. 12, /* bitsize */
  890. false, /* pc_relative */
  891. 0, /* bitpos */
  892. complain_overflow_dont,/* complain_on_overflow */
  893. bfd_elf_generic_reloc, /* special_function */
  894. "R_NDS32_GOT_LO12", /* name */
  895. false, /* partial_inplace */
  896. 0x00000fff, /* src_mask */
  897. 0x00000fff, /* dst_mask */
  898. false), /* pcrel_offset */
  899. /* An PC Relative relocation used when setting PIC offset table register.
  900. Like R_NDS32_HI20, but referring to the GOT table entry for
  901. the symbol. */
  902. HOWTO2 (R_NDS32_GOTPC_HI20, /* type */
  903. 12, /* rightshift */
  904. 2, /* size (0 = byte, 1 = short, 2 = long) */
  905. 20, /* bitsize */
  906. false, /* pc_relative */
  907. 0, /* bitpos */
  908. complain_overflow_dont,/* complain_on_overflow */
  909. bfd_elf_generic_reloc, /* special_function */
  910. "R_NDS32_GOTPC_HI20", /* name */
  911. false, /* partial_inplace */
  912. 0x000fffff, /* src_mask */
  913. 0x000fffff, /* dst_mask */
  914. true), /* pcrel_offset */
  915. HOWTO2 (R_NDS32_GOTPC_LO12, /* type */
  916. 0, /* rightshift */
  917. 2, /* size (0 = byte, 1 = short, 2 = long) */
  918. 12, /* bitsize */
  919. false, /* pc_relative */
  920. 0, /* bitpos */
  921. complain_overflow_dont,/* complain_on_overflow */
  922. bfd_elf_generic_reloc, /* special_function */
  923. "R_NDS32_GOTPC_LO12", /* name */
  924. false, /* partial_inplace */
  925. 0x00000fff, /* src_mask */
  926. 0x00000fff, /* dst_mask */
  927. true), /* pcrel_offset */
  928. HOWTO2 (R_NDS32_GOTOFF_HI20, /* type */
  929. 12, /* rightshift */
  930. 2, /* size (0 = byte, 1 = short, 2 = long) */
  931. 20, /* bitsize */
  932. false, /* pc_relative */
  933. 0, /* bitpos */
  934. complain_overflow_dont,/* complain_on_overflow */
  935. bfd_elf_generic_reloc, /* special_function */
  936. "R_NDS32_GOTOFF_HI20", /* name */
  937. false, /* partial_inplace */
  938. 0x000fffff, /* src_mask */
  939. 0x000fffff, /* dst_mask */
  940. false), /* pcrel_offset */
  941. HOWTO2 (R_NDS32_GOTOFF_LO12, /* type */
  942. 0, /* rightshift */
  943. 2, /* size (0 = byte, 1 = short, 2 = long) */
  944. 12, /* bitsize */
  945. false, /* pc_relative */
  946. 0, /* bitpos */
  947. complain_overflow_dont,/* complain_on_overflow */
  948. bfd_elf_generic_reloc, /* special_function */
  949. "R_NDS32_GOTOFF_LO12", /* name */
  950. false, /* partial_inplace */
  951. 0x00000fff, /* src_mask */
  952. 0x00000fff, /* dst_mask */
  953. false), /* pcrel_offset */
  954. /* Alignment hint for relaxable instruction. This is used with
  955. R_NDS32_LABEL as a pair. Relax this instruction from 4 bytes to 2
  956. in order to make next label aligned on word boundary. */
  957. HOWTO2 (R_NDS32_INSN16, /* type */
  958. 0, /* rightshift */
  959. 2, /* size (0 = byte, 1 = short, 2 = long) */
  960. 32, /* bitsize */
  961. false, /* pc_relative */
  962. 0, /* bitpos */
  963. complain_overflow_dont,/* complain_on_overflow */
  964. nds32_elf_ignore_reloc,/* special_function */
  965. "R_NDS32_INSN16", /* name */
  966. false, /* partial_inplace */
  967. 0x00000fff, /* src_mask */
  968. 0x00000fff, /* dst_mask */
  969. false), /* pcrel_offset */
  970. /* Alignment hint for label. */
  971. HOWTO2 (R_NDS32_LABEL, /* type */
  972. 0, /* rightshift */
  973. 2, /* size (0 = byte, 1 = short, 2 = long) */
  974. 32, /* bitsize */
  975. false, /* pc_relative */
  976. 0, /* bitpos */
  977. complain_overflow_dont,/* complain_on_overflow */
  978. nds32_elf_ignore_reloc,/* special_function */
  979. "R_NDS32_LABEL", /* name */
  980. false, /* partial_inplace */
  981. 0xffffffff, /* src_mask */
  982. 0xffffffff, /* dst_mask */
  983. false), /* pcrel_offset */
  984. /* Relax hint for unconditional call sequence */
  985. HOWTO2 (R_NDS32_LONGCALL1, /* type */
  986. 0, /* rightshift */
  987. 2, /* size (0 = byte, 1 = short, 2 = long) */
  988. 32, /* bitsize */
  989. false, /* pc_relative */
  990. 0, /* bitpos */
  991. complain_overflow_dont,/* complain_on_overflow */
  992. nds32_elf_ignore_reloc,/* special_function */
  993. "R_NDS32_LONGCALL1", /* name */
  994. false, /* partial_inplace */
  995. 0xffffffff, /* src_mask */
  996. 0xffffffff, /* dst_mask */
  997. false), /* pcrel_offset */
  998. /* Relax hint for conditional call sequence. */
  999. HOWTO2 (R_NDS32_LONGCALL2, /* type */
  1000. 0, /* rightshift */
  1001. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1002. 32, /* bitsize */
  1003. false, /* pc_relative */
  1004. 0, /* bitpos */
  1005. complain_overflow_dont,/* complain_on_overflow */
  1006. nds32_elf_ignore_reloc,/* special_function */
  1007. "R_NDS32_LONGCALL2", /* name */
  1008. false, /* partial_inplace */
  1009. 0xffffffff, /* src_mask */
  1010. 0xffffffff, /* dst_mask */
  1011. false), /* pcrel_offset */
  1012. /* Relax hint for conditional call sequence. */
  1013. HOWTO2 (R_NDS32_LONGCALL3, /* type */
  1014. 0, /* rightshift */
  1015. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1016. 32, /* bitsize */
  1017. false, /* pc_relative */
  1018. 0, /* bitpos */
  1019. complain_overflow_dont,/* complain_on_overflow */
  1020. nds32_elf_ignore_reloc,/* special_function */
  1021. "R_NDS32_LONGCALL3", /* name */
  1022. false, /* partial_inplace */
  1023. 0xffffffff, /* src_mask */
  1024. 0xffffffff, /* dst_mask */
  1025. false), /* pcrel_offset */
  1026. /* Relax hint for unconditional branch sequence. */
  1027. HOWTO2 (R_NDS32_LONGJUMP1, /* type */
  1028. 0, /* rightshift */
  1029. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1030. 32, /* bitsize */
  1031. false, /* pc_relative */
  1032. 0, /* bitpos */
  1033. complain_overflow_dont,/* complain_on_overflow */
  1034. nds32_elf_ignore_reloc,/* special_function */
  1035. "R_NDS32_LONGJUMP1", /* name */
  1036. false, /* partial_inplace */
  1037. 0xffffffff, /* src_mask */
  1038. 0xffffffff, /* dst_mask */
  1039. false), /* pcrel_offset */
  1040. /* Relax hint for conditional branch sequence. */
  1041. HOWTO2 (R_NDS32_LONGJUMP2, /* type */
  1042. 0, /* rightshift */
  1043. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1044. 32, /* bitsize */
  1045. false, /* pc_relative */
  1046. 0, /* bitpos */
  1047. complain_overflow_dont,/* complain_on_overflow */
  1048. nds32_elf_ignore_reloc,/* special_function */
  1049. "R_NDS32_LONGJUMP2", /* name */
  1050. false, /* partial_inplace */
  1051. 0xffffffff, /* src_mask */
  1052. 0xffffffff, /* dst_mask */
  1053. false), /* pcrel_offset */
  1054. /* Relax hint for conditional branch sequence. */
  1055. HOWTO2 (R_NDS32_LONGJUMP3, /* type */
  1056. 0, /* rightshift */
  1057. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1058. 32, /* bitsize */
  1059. false, /* pc_relative */
  1060. 0, /* bitpos */
  1061. complain_overflow_dont,/* complain_on_overflow */
  1062. nds32_elf_ignore_reloc,/* special_function */
  1063. "R_NDS32_LONGJUMP3", /* name */
  1064. false, /* partial_inplace */
  1065. 0xffffffff, /* src_mask */
  1066. 0xffffffff, /* dst_mask */
  1067. false), /* pcrel_offset */
  1068. /* Relax hint for load/store sequence. */
  1069. HOWTO2 (R_NDS32_LOADSTORE, /* type */
  1070. 0, /* rightshift */
  1071. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1072. 32, /* bitsize */
  1073. false, /* pc_relative */
  1074. 0, /* bitpos */
  1075. complain_overflow_dont,/* complain_on_overflow */
  1076. nds32_elf_ignore_reloc,/* special_function */
  1077. "R_NDS32_LOADSTORE", /* name */
  1078. false, /* partial_inplace */
  1079. 0xffffffff, /* src_mask */
  1080. 0xffffffff, /* dst_mask */
  1081. false), /* pcrel_offset */
  1082. /* Relax hint for load/store sequence. */
  1083. HOWTO2 (R_NDS32_9_FIXED_RELA, /* type */
  1084. 0, /* rightshift */
  1085. 1, /* size (0 = byte, 1 = short, 2 = long) */
  1086. 16, /* bitsize */
  1087. false, /* pc_relative */
  1088. 0, /* bitpos */
  1089. complain_overflow_dont,/* complain_on_overflow */
  1090. nds32_elf_ignore_reloc,/* special_function */
  1091. "R_NDS32_9_FIXED_RELA",/* name */
  1092. false, /* partial_inplace */
  1093. 0x000000ff, /* src_mask */
  1094. 0x000000ff, /* dst_mask */
  1095. false), /* pcrel_offset */
  1096. /* Relax hint for load/store sequence. */
  1097. HOWTO2 (R_NDS32_15_FIXED_RELA,/* type */
  1098. 0, /* rightshift */
  1099. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1100. 32, /* bitsize */
  1101. false, /* pc_relative */
  1102. 0, /* bitpos */
  1103. complain_overflow_dont,/* complain_on_overflow */
  1104. nds32_elf_ignore_reloc,/* special_function */
  1105. "R_NDS32_15_FIXED_RELA",/* name */
  1106. false, /* partial_inplace */
  1107. 0x00003fff, /* src_mask */
  1108. 0x00003fff, /* dst_mask */
  1109. false), /* pcrel_offset */
  1110. /* Relax hint for load/store sequence. */
  1111. HOWTO2 (R_NDS32_17_FIXED_RELA,/* type */
  1112. 0, /* rightshift */
  1113. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1114. 32, /* bitsize */
  1115. false, /* pc_relative */
  1116. 0, /* bitpos */
  1117. complain_overflow_dont,/* complain_on_overflow */
  1118. nds32_elf_ignore_reloc,/* special_function */
  1119. "R_NDS32_17_FIXED_RELA",/* name */
  1120. false, /* partial_inplace */
  1121. 0x0000ffff, /* src_mask */
  1122. 0x0000ffff, /* dst_mask */
  1123. false), /* pcrel_offset */
  1124. /* Relax hint for load/store sequence. */
  1125. HOWTO2 (R_NDS32_25_FIXED_RELA,/* type */
  1126. 0, /* rightshift */
  1127. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1128. 32, /* bitsize */
  1129. false, /* pc_relative */
  1130. 0, /* bitpos */
  1131. complain_overflow_dont,/* complain_on_overflow */
  1132. nds32_elf_ignore_reloc,/* special_function */
  1133. "R_NDS32_25_FIXED_RELA",/* name */
  1134. false, /* partial_inplace */
  1135. 0x00ffffff, /* src_mask */
  1136. 0x00ffffff, /* dst_mask */
  1137. false), /* pcrel_offset */
  1138. /* High 20 bits of PLT symbol offset relative to PC. */
  1139. HOWTO2 (R_NDS32_PLTREL_HI20, /* type */
  1140. 12, /* rightshift */
  1141. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1142. 20, /* bitsize */
  1143. false, /* pc_relative */
  1144. 0, /* bitpos */
  1145. complain_overflow_dont,/* complain_on_overflow */
  1146. bfd_elf_generic_reloc, /* special_function */
  1147. "R_NDS32_PLTREL_HI20", /* name */
  1148. false, /* partial_inplace */
  1149. 0x000fffff, /* src_mask */
  1150. 0x000fffff, /* dst_mask */
  1151. false), /* pcrel_offset */
  1152. /* Low 12 bits of PLT symbol offset relative to PC. */
  1153. HOWTO2 (R_NDS32_PLTREL_LO12, /* type */
  1154. 0, /* rightshift */
  1155. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1156. 12, /* bitsize */
  1157. false, /* pc_relative */
  1158. 0, /* bitpos */
  1159. complain_overflow_dont,/* complain_on_overflow */
  1160. bfd_elf_generic_reloc, /* special_function */
  1161. "R_NDS32_PLTREL_LO12", /* name */
  1162. false, /* partial_inplace */
  1163. 0x00000fff, /* src_mask */
  1164. 0x00000fff, /* dst_mask */
  1165. false), /* pcrel_offset */
  1166. /* High 20 bits of PLT symbol offset relative to GOT (GP). */
  1167. HOWTO2 (R_NDS32_PLT_GOTREL_HI20, /* type */
  1168. 12, /* rightshift */
  1169. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1170. 20, /* bitsize */
  1171. false, /* pc_relative */
  1172. 0, /* bitpos */
  1173. complain_overflow_dont,/* complain_on_overflow */
  1174. bfd_elf_generic_reloc, /* special_function */
  1175. "R_NDS32_PLT_GOTREL_HI20",/* name */
  1176. false, /* partial_inplace */
  1177. 0x000fffff, /* src_mask */
  1178. 0x000fffff, /* dst_mask */
  1179. false), /* pcrel_offset */
  1180. /* Low 12 bits of PLT symbol offset relative to GOT (GP). */
  1181. HOWTO2 (R_NDS32_PLT_GOTREL_LO12,/* type */
  1182. 0, /* rightshift */
  1183. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1184. 12, /* bitsize */
  1185. false, /* pc_relative */
  1186. 0, /* bitpos */
  1187. complain_overflow_dont,/* complain_on_overflow */
  1188. bfd_elf_generic_reloc, /* special_function */
  1189. "R_NDS32_PLT_GOTREL_LO12",/* name */
  1190. false, /* partial_inplace */
  1191. 0x00000fff, /* src_mask */
  1192. 0x00000fff, /* dst_mask */
  1193. false), /* pcrel_offset */
  1194. /* Small data area 12 bits offset. */
  1195. HOWTO2 (R_NDS32_SDA12S2_DP_RELA,/* type */
  1196. 2, /* rightshift */
  1197. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1198. 12, /* bitsize */
  1199. false, /* pc_relative */
  1200. 0, /* bitpos */
  1201. complain_overflow_signed,/* complain_on_overflow */
  1202. bfd_elf_generic_reloc, /* special_function */
  1203. "R_NDS32_SDA12S2_DP_RELA",/* name */
  1204. false, /* partial_inplace */
  1205. 0x00000fff, /* src_mask */
  1206. 0x00000fff, /* dst_mask */
  1207. false), /* pcrel_offset */
  1208. /* Small data area 12 bits offset. */
  1209. HOWTO2 (R_NDS32_SDA12S2_SP_RELA,/* type */
  1210. 2, /* rightshift */
  1211. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1212. 12, /* bitsize */
  1213. false, /* pc_relative */
  1214. 0, /* bitpos */
  1215. complain_overflow_signed,/* complain_on_overflow */
  1216. bfd_elf_generic_reloc, /* special_function */
  1217. "R_NDS32_SDA12S2_SP_RELA",/* name */
  1218. false, /* partial_inplace */
  1219. 0x00000fff, /* src_mask */
  1220. 0x00000fff, /* dst_mask */
  1221. false), /* pcrel_offset */
  1222. /* Lower 12 bits of address. */
  1223. HOWTO2 (R_NDS32_LO12S2_DP_RELA, /* type */
  1224. 2, /* rightshift */
  1225. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1226. 10, /* bitsize */
  1227. false, /* pc_relative */
  1228. 0, /* bitpos */
  1229. complain_overflow_dont,/* complain_on_overflow */
  1230. bfd_elf_generic_reloc, /* special_function */
  1231. "R_NDS32_LO12S2_DP_RELA",/* name */
  1232. false, /* partial_inplace */
  1233. 0x000003ff, /* src_mask */
  1234. 0x000003ff, /* dst_mask */
  1235. false), /* pcrel_offset */
  1236. /* Lower 12 bits of address. */
  1237. HOWTO2 (R_NDS32_LO12S2_SP_RELA,/* type */
  1238. 2, /* rightshift */
  1239. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1240. 10, /* bitsize */
  1241. false, /* pc_relative */
  1242. 0, /* bitpos */
  1243. complain_overflow_dont,/* complain_on_overflow */
  1244. bfd_elf_generic_reloc, /* special_function */
  1245. "R_NDS32_LO12S2_SP_RELA",/* name */
  1246. false, /* partial_inplace */
  1247. 0x000003ff, /* src_mask */
  1248. 0x000003ff, /* dst_mask */
  1249. false), /* pcrel_offset */
  1250. /* Lower 12 bits of address. Special identity for or case. */
  1251. HOWTO2 (R_NDS32_LO12S0_ORI_RELA,/* type */
  1252. 0, /* rightshift */
  1253. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1254. 12, /* bitsize */
  1255. false, /* pc_relative */
  1256. 0, /* bitpos */
  1257. complain_overflow_dont,/* complain_on_overflow */
  1258. bfd_elf_generic_reloc, /* special_function */
  1259. "R_NDS32_LO12S0_ORI_RELA",/* name */
  1260. false, /* partial_inplace */
  1261. 0x00000fff, /* src_mask */
  1262. 0x00000fff, /* dst_mask */
  1263. false), /* pcrel_offset */
  1264. /* Small data area 19 bits offset. */
  1265. HOWTO2 (R_NDS32_SDA16S3_RELA, /* type */
  1266. 3, /* rightshift */
  1267. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1268. 16, /* bitsize */
  1269. false, /* pc_relative */
  1270. 0, /* bitpos */
  1271. complain_overflow_signed,/* complain_on_overflow */
  1272. bfd_elf_generic_reloc, /* special_function */
  1273. "R_NDS32_SDA16S3_RELA",/* name */
  1274. false, /* partial_inplace */
  1275. 0x0000ffff, /* src_mask */
  1276. 0x0000ffff, /* dst_mask */
  1277. false), /* pcrel_offset */
  1278. /* Small data area 15 bits offset. */
  1279. HOWTO2 (R_NDS32_SDA17S2_RELA, /* type */
  1280. 2, /* rightshift */
  1281. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1282. 17, /* bitsize */
  1283. false, /* pc_relative */
  1284. 0, /* bitpos */
  1285. complain_overflow_signed,/* complain_on_overflow */
  1286. bfd_elf_generic_reloc, /* special_function */
  1287. "R_NDS32_SDA17S2_RELA",/* name */
  1288. false, /* partial_inplace */
  1289. 0x0001ffff, /* src_mask */
  1290. 0x0001ffff, /* dst_mask */
  1291. false), /* pcrel_offset */
  1292. HOWTO2 (R_NDS32_SDA18S1_RELA, /* type */
  1293. 1, /* rightshift */
  1294. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1295. 18, /* bitsize */
  1296. false, /* pc_relative */
  1297. 0, /* bitpos */
  1298. complain_overflow_signed,/* complain_on_overflow */
  1299. bfd_elf_generic_reloc, /* special_function */
  1300. "R_NDS32_SDA18S1_RELA",/* name */
  1301. false, /* partial_inplace */
  1302. 0x0003ffff, /* src_mask */
  1303. 0x0003ffff, /* dst_mask */
  1304. false), /* pcrel_offset */
  1305. HOWTO2 (R_NDS32_SDA19S0_RELA, /* type */
  1306. 0, /* rightshift */
  1307. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1308. 19, /* bitsize */
  1309. false, /* pc_relative */
  1310. 0, /* bitpos */
  1311. complain_overflow_signed,/* complain_on_overflow */
  1312. bfd_elf_generic_reloc, /* special_function */
  1313. "R_NDS32_SDA19S0_RELA",/* name */
  1314. false, /* partial_inplace */
  1315. 0x0007ffff, /* src_mask */
  1316. 0x0007ffff, /* dst_mask */
  1317. false), /* pcrel_offset */
  1318. HOWTO2 (R_NDS32_DWARF2_OP1_RELA,/* type */
  1319. 0, /* rightshift */
  1320. 0, /* size (0 = byte, 1 = short, 2 = long) */
  1321. 8, /* bitsize */
  1322. false, /* pc_relative */
  1323. 0, /* bitpos */
  1324. complain_overflow_dont,/* complain_on_overflow */
  1325. nds32_elf_ignore_reloc,/* special_function */
  1326. "R_NDS32_DWARF2_OP1_RELA",/* name */
  1327. false, /* partial_inplace */
  1328. 0xff, /* src_mask */
  1329. 0xff, /* dst_mask */
  1330. false), /* pcrel_offset */
  1331. HOWTO2 (R_NDS32_DWARF2_OP2_RELA,/* type */
  1332. 0, /* rightshift */
  1333. 1, /* size (0 = byte, 1 = short, 2 = long) */
  1334. 16, /* bitsize */
  1335. false, /* pc_relative */
  1336. 0, /* bitpos */
  1337. complain_overflow_dont,/* complain_on_overflow */
  1338. nds32_elf_ignore_reloc,/* special_function */
  1339. "R_NDS32_DWARF2_OP2_RELA",/* name */
  1340. false, /* partial_inplace */
  1341. 0xffff, /* src_mask */
  1342. 0xffff, /* dst_mask */
  1343. false), /* pcrel_offset */
  1344. HOWTO2 (R_NDS32_DWARF2_LEB_RELA,/* type */
  1345. 0, /* rightshift */
  1346. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1347. 32, /* bitsize */
  1348. false, /* pc_relative */
  1349. 0, /* bitpos */
  1350. complain_overflow_dont,/* complain_on_overflow */
  1351. nds32_elf_ignore_reloc,/* special_function */
  1352. "R_NDS32_DWARF2_LEB_RELA",/* name */
  1353. false, /* partial_inplace */
  1354. 0xffffffff, /* src_mask */
  1355. 0xffffffff, /* dst_mask */
  1356. false), /* pcrel_offset */
  1357. HOWTO2 (R_NDS32_UPDATE_TA_RELA,/* type */
  1358. 0, /* rightshift */
  1359. 1, /* size (0 = byte, 1 = short, 2 = long) */
  1360. 16, /* bitsize */
  1361. false, /* pc_relative */
  1362. 0, /* bitpos */
  1363. complain_overflow_dont,/* complain_on_overflow */
  1364. nds32_elf_ignore_reloc,/* special_function */
  1365. "R_NDS32_UPDATE_TA_RELA",/* name */
  1366. false, /* partial_inplace */
  1367. 0xffff, /* src_mask */
  1368. 0xffff, /* dst_mask */
  1369. false), /* pcrel_offset */
  1370. /* Like R_NDS32_PCREL, but referring to the procedure linkage table
  1371. entry for the symbol. */
  1372. HOWTO2 (R_NDS32_9_PLTREL, /* type */
  1373. 1, /* rightshift */
  1374. 1, /* size (0 = byte, 1 = short, 2 = long) */
  1375. 8, /* bitsize */
  1376. true, /* pc_relative */
  1377. 0, /* bitpos */
  1378. complain_overflow_signed,/* complain_on_overflow */
  1379. bfd_elf_generic_reloc, /* special_function */
  1380. "R_NDS32_9_PLTREL", /* name */
  1381. false, /* partial_inplace */
  1382. 0xff, /* src_mask */
  1383. 0xff, /* dst_mask */
  1384. true), /* pcrel_offset */
  1385. /* Low 20 bits of PLT symbol offset relative to GOT (GP). */
  1386. HOWTO2 (R_NDS32_PLT_GOTREL_LO20,/* type */
  1387. 0, /* rightshift */
  1388. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1389. 20, /* bitsize */
  1390. false, /* pc_relative */
  1391. 0, /* bitpos */
  1392. complain_overflow_dont,/* complain_on_overflow */
  1393. bfd_elf_generic_reloc, /* special_function */
  1394. "R_NDS32_PLT_GOTREL_LO20",/* name */
  1395. false, /* partial_inplace */
  1396. 0x000fffff, /* src_mask */
  1397. 0x000fffff, /* dst_mask */
  1398. false), /* pcrel_offset */
  1399. /* low 15 bits of PLT symbol offset relative to GOT (GP) */
  1400. HOWTO2 (R_NDS32_PLT_GOTREL_LO15,/* type */
  1401. 0, /* rightshift */
  1402. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1403. 15, /* bitsize */
  1404. false, /* pc_relative */
  1405. 0, /* bitpos */
  1406. complain_overflow_dont,/* complain_on_overflow */
  1407. bfd_elf_generic_reloc, /* special_function */
  1408. "R_NDS32_PLT_GOTREL_LO15",/* name */
  1409. false, /* partial_inplace */
  1410. 0x00007fff, /* src_mask */
  1411. 0x00007fff, /* dst_mask */
  1412. false), /* pcrel_offset */
  1413. /* Low 19 bits of PLT symbol offset relative to GOT (GP). */
  1414. HOWTO2 (R_NDS32_PLT_GOTREL_LO19,/* type */
  1415. 0, /* rightshift */
  1416. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1417. 19, /* bitsize */
  1418. false, /* pc_relative */
  1419. 0, /* bitpos */
  1420. complain_overflow_dont,/* complain_on_overflow */
  1421. bfd_elf_generic_reloc, /* special_function */
  1422. "R_NDS32_PLT_GOTREL_LO19",/* name */
  1423. false, /* partial_inplace */
  1424. 0x0007ffff, /* src_mask */
  1425. 0x0007ffff, /* dst_mask */
  1426. false), /* pcrel_offset */
  1427. HOWTO2 (R_NDS32_GOT_LO15, /* type */
  1428. 0, /* rightshift */
  1429. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1430. 15, /* bitsize */
  1431. false, /* pc_relative */
  1432. 0, /* bitpos */
  1433. complain_overflow_dont,/* complain_on_overflow */
  1434. bfd_elf_generic_reloc, /* special_function */
  1435. "R_NDS32_GOT_LO15", /* name */
  1436. false, /* partial_inplace */
  1437. 0x00007fff, /* src_mask */
  1438. 0x00007fff, /* dst_mask */
  1439. false), /* pcrel_offset */
  1440. HOWTO2 (R_NDS32_GOT_LO19, /* type */
  1441. 0, /* rightshift */
  1442. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1443. 19, /* bitsize */
  1444. false, /* pc_relative */
  1445. 0, /* bitpos */
  1446. complain_overflow_dont,/* complain_on_overflow */
  1447. bfd_elf_generic_reloc, /* special_function */
  1448. "R_NDS32_GOT_LO19", /* name */
  1449. false, /* partial_inplace */
  1450. 0x0007ffff, /* src_mask */
  1451. 0x0007ffff, /* dst_mask */
  1452. false), /* pcrel_offset */
  1453. HOWTO2 (R_NDS32_GOTOFF_LO15, /* type */
  1454. 0, /* rightshift */
  1455. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1456. 15, /* bitsize */
  1457. false, /* pc_relative */
  1458. 0, /* bitpos */
  1459. complain_overflow_dont,/* complain_on_overflow */
  1460. bfd_elf_generic_reloc, /* special_function */
  1461. "R_NDS32_GOTOFF_LO15", /* name */
  1462. false, /* partial_inplace */
  1463. 0x00007fff, /* src_mask */
  1464. 0x00007fff, /* dst_mask */
  1465. false), /* pcrel_offset */
  1466. HOWTO2 (R_NDS32_GOTOFF_LO19, /* type */
  1467. 0, /* rightshift */
  1468. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1469. 19, /* bitsize */
  1470. false, /* pc_relative */
  1471. 0, /* bitpos */
  1472. complain_overflow_dont,/* complain_on_overflow */
  1473. bfd_elf_generic_reloc, /* special_function */
  1474. "R_NDS32_GOTOFF_LO19", /* name */
  1475. false, /* partial_inplace */
  1476. 0x0007ffff, /* src_mask */
  1477. 0x0007ffff, /* dst_mask */
  1478. false), /* pcrel_offset */
  1479. /* GOT 15 bits offset. */
  1480. HOWTO2 (R_NDS32_GOT15S2_RELA, /* type */
  1481. 2, /* rightshift */
  1482. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1483. 15, /* bitsize */
  1484. false, /* pc_relative */
  1485. 0, /* bitpos */
  1486. complain_overflow_signed,/* complain_on_overflow */
  1487. bfd_elf_generic_reloc, /* special_function */
  1488. "R_NDS32_GOT15S2_RELA",/* name */
  1489. false, /* partial_inplace */
  1490. 0x00007fff, /* src_mask */
  1491. 0x00007fff, /* dst_mask */
  1492. false), /* pcrel_offset */
  1493. /* GOT 17 bits offset. */
  1494. HOWTO2 (R_NDS32_GOT17S2_RELA, /* type */
  1495. 2, /* rightshift */
  1496. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1497. 17, /* bitsize */
  1498. false, /* pc_relative */
  1499. 0, /* bitpos */
  1500. complain_overflow_signed,/* complain_on_overflow */
  1501. bfd_elf_generic_reloc, /* special_function */
  1502. "R_NDS32_GOT17S2_RELA",/* name */
  1503. false, /* partial_inplace */
  1504. 0x0001ffff, /* src_mask */
  1505. 0x0001ffff, /* dst_mask */
  1506. false), /* pcrel_offset */
  1507. /* A 5 bit address. */
  1508. HOWTO2 (R_NDS32_5_RELA, /* type */
  1509. 0, /* rightshift */
  1510. 1, /* size (0 = byte, 1 = short, 2 = long) */
  1511. 5, /* bitsize */
  1512. false, /* pc_relative */
  1513. 0, /* bitpos */
  1514. complain_overflow_signed,/* complain_on_overflow */
  1515. bfd_elf_generic_reloc, /* special_function */
  1516. "R_NDS32_5_RELA", /* name */
  1517. false, /* partial_inplace */
  1518. 0x1f, /* src_mask */
  1519. 0x1f, /* dst_mask */
  1520. false), /* pcrel_offset */
  1521. HOWTO2 (R_NDS32_10_UPCREL_RELA,/* type */
  1522. 1, /* rightshift */
  1523. 1, /* size (0 = byte, 1 = short, 2 = long) */
  1524. 9, /* bitsize */
  1525. true, /* pc_relative */
  1526. 0, /* bitpos */
  1527. complain_overflow_unsigned,/* complain_on_overflow */
  1528. bfd_elf_generic_reloc, /* special_function */
  1529. "R_NDS32_10_UPCREL_RELA",/* name */
  1530. false, /* partial_inplace */
  1531. 0x1ff, /* src_mask */
  1532. 0x1ff, /* dst_mask */
  1533. true), /* pcrel_offset */
  1534. HOWTO2 (R_NDS32_SDA_FP7U2_RELA,/* type */
  1535. 2, /* rightshift */
  1536. 1, /* size (0 = byte, 1 = short, 2 = long) */
  1537. 7, /* bitsize */
  1538. false, /* pc_relative */
  1539. 0, /* bitpos */
  1540. complain_overflow_unsigned,/* complain_on_overflow */
  1541. bfd_elf_generic_reloc, /* special_function */
  1542. "R_NDS32_SDA_FP7U2_RELA",/* name */
  1543. false, /* partial_inplace */
  1544. 0x0000007f, /* src_mask */
  1545. 0x0000007f, /* dst_mask */
  1546. false), /* pcrel_offset */
  1547. HOWTO2 (R_NDS32_WORD_9_PCREL_RELA,/* type */
  1548. 1, /* rightshift */
  1549. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1550. 8, /* bitsize */
  1551. true, /* pc_relative */
  1552. 0, /* bitpos */
  1553. complain_overflow_signed,/* complain_on_overflow */
  1554. bfd_elf_generic_reloc, /* special_function */
  1555. "R_NDS32_WORD_9_PCREL_RELA",/* name */
  1556. false, /* partial_inplace */
  1557. 0xff, /* src_mask */
  1558. 0xff, /* dst_mask */
  1559. true), /* pcrel_offset */
  1560. HOWTO2 (R_NDS32_25_ABS_RELA, /* type */
  1561. 1, /* rightshift */
  1562. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1563. 24, /* bitsize */
  1564. false, /* pc_relative */
  1565. 0, /* bitpos */
  1566. complain_overflow_dont,/* complain_on_overflow */
  1567. bfd_elf_generic_reloc, /* special_function */
  1568. "R_NDS32_25_ABS_RELA", /* name */
  1569. false, /* partial_inplace */
  1570. 0xffffff, /* src_mask */
  1571. 0xffffff, /* dst_mask */
  1572. false), /* pcrel_offset */
  1573. /* A relative 17 bit relocation for ifc, right shifted by 1. */
  1574. HOWTO2 (R_NDS32_17IFC_PCREL_RELA,/* type */
  1575. 1, /* rightshift */
  1576. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1577. 16, /* bitsize */
  1578. true, /* pc_relative */
  1579. 0, /* bitpos */
  1580. complain_overflow_signed,/* complain_on_overflow */
  1581. bfd_elf_generic_reloc, /* special_function */
  1582. "R_NDS32_17IFC_PCREL_RELA",/* name */
  1583. false, /* partial_inplace */
  1584. 0xffff, /* src_mask */
  1585. 0xffff, /* dst_mask */
  1586. true), /* pcrel_offset */
  1587. /* A relative unsigned 10 bit relocation for ifc, right shifted by 1. */
  1588. HOWTO2 (R_NDS32_10IFCU_PCREL_RELA,/* type */
  1589. 1, /* rightshift */
  1590. 1, /* size (0 = byte, 1 = short, 2 = long) */
  1591. 9, /* bitsize */
  1592. true, /* pc_relative */
  1593. 0, /* bitpos */
  1594. complain_overflow_unsigned,/* complain_on_overflow */
  1595. bfd_elf_generic_reloc, /* special_function */
  1596. "R_NDS32_10IFCU_PCREL_RELA",/* name */
  1597. false, /* partial_inplace */
  1598. 0x1ff, /* src_mask */
  1599. 0x1ff, /* dst_mask */
  1600. true), /* pcrel_offset */
  1601. /* Like R_NDS32_HI20, but referring to the TLS LE entry for the symbol. */
  1602. HOWTO2 (R_NDS32_TLS_LE_HI20, /* type */
  1603. 12, /* rightshift */
  1604. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1605. 20, /* bitsize */
  1606. false, /* pc_relative */
  1607. 0, /* bitpos */
  1608. complain_overflow_dont,/* complain_on_overflow */
  1609. bfd_elf_generic_reloc, /* special_function */
  1610. "R_NDS32_TLS_LE_HI20", /* name */
  1611. false, /* partial_inplace */
  1612. 0x000fffff, /* src_mask */
  1613. 0x000fffff, /* dst_mask */
  1614. false), /* pcrel_offset */
  1615. HOWTO2 (R_NDS32_TLS_LE_LO12, /* type */
  1616. 0, /* rightshift */
  1617. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1618. 12, /* bitsize */
  1619. false, /* pc_relative */
  1620. 0, /* bitpos */
  1621. complain_overflow_dont,/* complain_on_overflow */
  1622. bfd_elf_generic_reloc, /* special_function */
  1623. "R_NDS32_TLS_LE_LO12", /* name */
  1624. false, /* partial_inplace */
  1625. 0x00000fff, /* src_mask */
  1626. 0x00000fff, /* dst_mask */
  1627. false), /* pcrel_offset */
  1628. /* Like R_NDS32_HI20, but referring to the TLS IE entry for the symbol. */
  1629. HOWTO2 (R_NDS32_TLS_IE_HI20, /* type */
  1630. 12, /* rightshift */
  1631. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1632. 20, /* bitsize */
  1633. false, /* pc_relative */
  1634. 0, /* bitpos */
  1635. complain_overflow_dont,/* complain_on_overflow */
  1636. bfd_elf_generic_reloc, /* special_function */
  1637. "R_NDS32_TLS_IE_HI20", /* name */
  1638. false, /* partial_inplace */
  1639. 0x000fffff, /* src_mask */
  1640. 0x000fffff, /* dst_mask */
  1641. false), /* pcrel_offset */
  1642. HOWTO2 (R_NDS32_TLS_IE_LO12S2,/* type */
  1643. 2, /* rightshift */
  1644. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1645. 10, /* bitsize */
  1646. false, /* pc_relative */
  1647. 0, /* bitpos */
  1648. complain_overflow_dont,/* complain_on_overflow */
  1649. bfd_elf_generic_reloc, /* special_function */
  1650. "R_NDS32_TLS_IE_LO12S2",/* name */
  1651. false, /* partial_inplace */
  1652. 0x000003ff, /* src_mask */
  1653. 0x000003ff, /* dst_mask */
  1654. false), /* pcrel_offset */
  1655. /* TLS LE TP offset relocation */
  1656. HOWTO2 (R_NDS32_TLS_TPOFF, /* type */
  1657. 0, /* rightshift */
  1658. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1659. 32, /* bitsize */
  1660. false, /* pc_relative */
  1661. 0, /* bitpos */
  1662. complain_overflow_bitfield,/* complain_on_overflow */
  1663. bfd_elf_generic_reloc, /* special_function */
  1664. "R_NDS32_TLS_TPOFF", /* name */
  1665. false, /* partial_inplace */
  1666. 0xffffffff, /* src_mask */
  1667. 0xffffffff, /* dst_mask */
  1668. false), /* pcrel_offset */
  1669. /* A 20 bit address. */
  1670. HOWTO2 (R_NDS32_TLS_LE_20, /* type */
  1671. 0, /* rightshift */
  1672. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1673. 20, /* bitsize */
  1674. false, /* pc_relative */
  1675. 0, /* bitpos */
  1676. complain_overflow_signed,/* complain_on_overflow */
  1677. bfd_elf_generic_reloc, /* special_function */
  1678. "R_NDS32_TLS_LE_20", /* name */
  1679. false, /* partial_inplace */
  1680. 0xfffff, /* src_mask */
  1681. 0xfffff, /* dst_mask */
  1682. false), /* pcrel_offset */
  1683. HOWTO2 (R_NDS32_TLS_LE_15S0, /* type */
  1684. 0, /* rightshift */
  1685. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1686. 15, /* bitsize */
  1687. false, /* pc_relative */
  1688. 0, /* bitpos */
  1689. complain_overflow_signed,/* complain_on_overflow */
  1690. bfd_elf_generic_reloc, /* special_function */
  1691. "R_NDS32_TLS_LE_15S0", /* name */
  1692. false, /* partial_inplace */
  1693. 0x7fff, /* src_mask */
  1694. 0x7fff, /* dst_mask */
  1695. false), /* pcrel_offset */
  1696. HOWTO2 (R_NDS32_TLS_LE_15S1, /* type */
  1697. 1, /* rightshift */
  1698. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1699. 15, /* bitsize */
  1700. false, /* pc_relative */
  1701. 0, /* bitpos */
  1702. complain_overflow_signed,/* complain_on_overflow */
  1703. bfd_elf_generic_reloc, /* special_function */
  1704. "R_NDS32_TLS_LE_15S1", /* name */
  1705. false, /* partial_inplace */
  1706. 0x7fff, /* src_mask */
  1707. 0x7fff, /* dst_mask */
  1708. false), /* pcrel_offset */
  1709. HOWTO2 (R_NDS32_TLS_LE_15S2, /* type */
  1710. 2, /* rightshift */
  1711. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1712. 15, /* bitsize */
  1713. false, /* pc_relative */
  1714. 0, /* bitpos */
  1715. complain_overflow_signed,/* complain_on_overflow */
  1716. bfd_elf_generic_reloc, /* special_function */
  1717. "R_NDS32_TLS_LE_15S2", /* name */
  1718. false, /* partial_inplace */
  1719. 0x7fff, /* src_mask */
  1720. 0x7fff, /* dst_mask */
  1721. false), /* pcrel_offset */
  1722. /* Relax hint for unconditional call sequence */
  1723. HOWTO2 (R_NDS32_LONGCALL4, /* type */
  1724. 0, /* rightshift */
  1725. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1726. 32, /* bitsize */
  1727. false, /* pc_relative */
  1728. 0, /* bitpos */
  1729. complain_overflow_dont,/* complain_on_overflow */
  1730. nds32_elf_ignore_reloc,/* special_function */
  1731. "R_NDS32_LONGCALL4", /* name */
  1732. false, /* partial_inplace */
  1733. 0xffffffff, /* src_mask */
  1734. 0xffffffff, /* dst_mask */
  1735. false), /* pcrel_offset */
  1736. /* Relax hint for conditional call sequence. */
  1737. HOWTO2 (R_NDS32_LONGCALL5, /* type */
  1738. 0, /* rightshift */
  1739. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1740. 32, /* bitsize */
  1741. false, /* pc_relative */
  1742. 0, /* bitpos */
  1743. complain_overflow_dont,/* complain_on_overflow */
  1744. nds32_elf_ignore_reloc,/* special_function */
  1745. "R_NDS32_LONGCALL5", /* name */
  1746. false, /* partial_inplace */
  1747. 0xffffffff, /* src_mask */
  1748. 0xffffffff, /* dst_mask */
  1749. false), /* pcrel_offset */
  1750. /* Relax hint for conditional call sequence. */
  1751. HOWTO2 (R_NDS32_LONGCALL6, /* type */
  1752. 0, /* rightshift */
  1753. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1754. 32, /* bitsize */
  1755. false, /* pc_relative */
  1756. 0, /* bitpos */
  1757. complain_overflow_dont,/* complain_on_overflow */
  1758. nds32_elf_ignore_reloc,/* special_function */
  1759. "R_NDS32_LONGCALL6", /* name */
  1760. false, /* partial_inplace */
  1761. 0xffffffff, /* src_mask */
  1762. 0xffffffff, /* dst_mask */
  1763. false), /* pcrel_offset */
  1764. /* Relax hint for unconditional branch sequence. */
  1765. HOWTO2 (R_NDS32_LONGJUMP4, /* type */
  1766. 0, /* rightshift */
  1767. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1768. 32, /* bitsize */
  1769. false, /* pc_relative */
  1770. 0, /* bitpos */
  1771. complain_overflow_dont,/* complain_on_overflow */
  1772. nds32_elf_ignore_reloc,/* special_function */
  1773. "R_NDS32_LONGJUMP4", /* name */
  1774. false, /* partial_inplace */
  1775. 0xffffffff, /* src_mask */
  1776. 0xffffffff, /* dst_mask */
  1777. false), /* pcrel_offset */
  1778. /* Relax hint for conditional branch sequence. */
  1779. HOWTO2 (R_NDS32_LONGJUMP5, /* type */
  1780. 0, /* rightshift */
  1781. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1782. 32, /* bitsize */
  1783. false, /* pc_relative */
  1784. 0, /* bitpos */
  1785. complain_overflow_dont,/* complain_on_overflow */
  1786. nds32_elf_ignore_reloc,/* special_function */
  1787. "R_NDS32_LONGJUMP5", /* name */
  1788. false, /* partial_inplace */
  1789. 0xffffffff, /* src_mask */
  1790. 0xffffffff, /* dst_mask */
  1791. false), /* pcrel_offset */
  1792. /* Relax hint for conditional branch sequence. */
  1793. HOWTO2 (R_NDS32_LONGJUMP6, /* type */
  1794. 0, /* rightshift */
  1795. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1796. 32, /* bitsize */
  1797. false, /* pc_relative */
  1798. 0, /* bitpos */
  1799. complain_overflow_dont,/* complain_on_overflow */
  1800. nds32_elf_ignore_reloc,/* special_function */
  1801. "R_NDS32_LONGJUMP6", /* name */
  1802. false, /* partial_inplace */
  1803. 0xffffffff, /* src_mask */
  1804. 0xffffffff, /* dst_mask */
  1805. false), /* pcrel_offset */
  1806. /* Relax hint for conditional branch sequence. */
  1807. HOWTO2 (R_NDS32_LONGJUMP7, /* type */
  1808. 0, /* rightshift */
  1809. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1810. 32, /* bitsize */
  1811. false, /* pc_relative */
  1812. 0, /* bitpos */
  1813. complain_overflow_dont,/* complain_on_overflow */
  1814. nds32_elf_ignore_reloc,/* special_function */
  1815. "R_NDS32_LONGJUMP7", /* name */
  1816. false, /* partial_inplace */
  1817. 0xffffffff, /* src_mask */
  1818. 0xffffffff, /* dst_mask */
  1819. false), /* pcrel_offset */
  1820. EMPTY_HOWTO (114),
  1821. HOWTO2 (R_NDS32_TLS_IE_LO12, /* type */
  1822. 0, /* rightshift */
  1823. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1824. 12, /* bitsize */
  1825. false, /* pc_relative */
  1826. 0, /* bitpos */
  1827. complain_overflow_dont,/* complain_on_overflow */
  1828. bfd_elf_generic_reloc, /* special_function */
  1829. "R_NDS32_TLS_IE_LO12", /* name */
  1830. false, /* partial_inplace */
  1831. 0x00000fff, /* src_mask */
  1832. 0x00000fff, /* dst_mask */
  1833. false), /* pcrel_offset */
  1834. /* Like R_NDS32_HI20, but referring to the TLS IE (PIE)
  1835. entry for the symbol. */
  1836. HOWTO2 (R_NDS32_TLS_IEGP_HI20,/* type */
  1837. 12, /* rightshift */
  1838. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1839. 20, /* bitsize */
  1840. false, /* pc_relative */
  1841. 0, /* bitpos */
  1842. complain_overflow_dont,/* complain_on_overflow */
  1843. bfd_elf_generic_reloc, /* special_function */
  1844. "R_NDS32_TLS_IEGP_HI20",/* name */
  1845. false, /* partial_inplace */
  1846. 0x000fffff, /* src_mask */
  1847. 0x000fffff, /* dst_mask */
  1848. false), /* pcrel_offset */
  1849. HOWTO2 (R_NDS32_TLS_IEGP_LO12,/* type */
  1850. 0, /* rightshift */
  1851. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1852. 12, /* bitsize */
  1853. false, /* pc_relative */
  1854. 0, /* bitpos */
  1855. complain_overflow_dont,/* complain_on_overflow */
  1856. bfd_elf_generic_reloc, /* special_function */
  1857. "R_NDS32_TLS_IEGP_LO12",/* name */
  1858. false, /* partial_inplace */
  1859. 0x00000fff, /* src_mask */
  1860. 0x00000fff, /* dst_mask */
  1861. false), /* pcrel_offset */
  1862. HOWTO2 (R_NDS32_TLS_IEGP_LO12S2,/* type */
  1863. 2, /* rightshift */
  1864. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1865. 10, /* bitsize */
  1866. false, /* pc_relative */
  1867. 0, /* bitpos */
  1868. complain_overflow_dont,/* complain_on_overflow */
  1869. bfd_elf_generic_reloc, /* special_function */
  1870. "R_NDS32_TLS_IEGP_LO12S2",/* name */
  1871. false, /* partial_inplace */
  1872. 0x000003ff, /* src_mask */
  1873. 0x000003ff, /* dst_mask */
  1874. false), /* pcrel_offset */
  1875. /* TLS description relocation */
  1876. HOWTO2 (R_NDS32_TLS_DESC, /* type */
  1877. 12, /* rightshift */
  1878. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1879. 20, /* bitsize */
  1880. false, /* pc_relative */
  1881. 0, /* bitpos */
  1882. complain_overflow_dont,/* complain_on_overflow */
  1883. nds32_elf_hi20_reloc, /* special_function */
  1884. "R_NDS32_TLS_DESC_HI20",/* name */
  1885. false, /* partial_inplace */
  1886. 0x000fffff, /* src_mask */
  1887. 0x000fffff, /* dst_mask */
  1888. false), /* pcrel_offset */
  1889. /* TLS GD/LD description offset high part. */
  1890. HOWTO2 (R_NDS32_TLS_DESC_HI20,/* type */
  1891. 12, /* rightshift */
  1892. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1893. 20, /* bitsize */
  1894. false, /* pc_relative */
  1895. 0, /* bitpos */
  1896. complain_overflow_dont,/* complain_on_overflow */
  1897. nds32_elf_hi20_reloc, /* special_function */
  1898. "R_NDS32_TLS_DESC_HI20",/* name */
  1899. false, /* partial_inplace */
  1900. 0x000fffff, /* src_mask */
  1901. 0x000fffff, /* dst_mask */
  1902. false), /* pcrel_offset */
  1903. /* TLS GD/LD description offset low part. */
  1904. HOWTO2 (R_NDS32_TLS_DESC_LO12,/* type */
  1905. 0, /* rightshift */
  1906. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1907. 12, /* bitsize */
  1908. false, /* pc_relative */
  1909. 0, /* bitpos */
  1910. complain_overflow_dont,/* complain_on_overflow */
  1911. nds32_elf_lo12_reloc, /* special_function */
  1912. "R_NDS32_TLS_DESC_LO12",/* name */
  1913. false, /* partial_inplace */
  1914. 0x00000fff, /* src_mask */
  1915. 0x00000fff, /* dst_mask */
  1916. false), /* pcrel_offset */
  1917. /* TLS GD/LD description offset set (movi). */
  1918. HOWTO2 (R_NDS32_TLS_DESC_20, /* type */
  1919. 0, /* rightshift */
  1920. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1921. 20, /* bitsize */
  1922. false, /* pc_relative */
  1923. 0, /* bitpos */
  1924. complain_overflow_signed,/* complain_on_overflow */
  1925. bfd_elf_generic_reloc, /* special_function */
  1926. "R_NDS32_TLS_DESC_20", /* name */
  1927. false, /* partial_inplace */
  1928. 0x000fffff, /* src_mask */
  1929. 0x000fffff, /* dst_mask */
  1930. false), /* pcrel_offset */
  1931. /* TLS GD/LD description offset set (lwi.gp). */
  1932. HOWTO2 (R_NDS32_TLS_DESC_SDA17S2,/* type */
  1933. 2, /* rightshift */
  1934. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1935. 17, /* bitsize */
  1936. false, /* pc_relative */
  1937. 0, /* bitpos */
  1938. complain_overflow_signed,/* complain_on_overflow */
  1939. bfd_elf_generic_reloc, /* special_function */
  1940. "R_NDS32_TLS_DESC_SDA17S2",/* name */
  1941. false, /* partial_inplace */
  1942. 0x0001ffff, /* src_mask */
  1943. 0x0001ffff, /* dst_mask */
  1944. false), /* pcrel_offset */
  1945. };
  1946. /* Relocations used for relaxation. */
  1947. #define HOWTO3(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
  1948. [C-R_NDS32_RELAX_ENTRY] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
  1949. static reloc_howto_type nds32_elf_relax_howto_table[] = {
  1950. HOWTO3 (R_NDS32_RELAX_ENTRY, /* type */
  1951. 0, /* rightshift */
  1952. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1953. 32, /* bitsize */
  1954. false, /* pc_relative */
  1955. 0, /* bitpos */
  1956. complain_overflow_dont,/* complain_on_overflow */
  1957. nds32_elf_ignore_reloc,/* special_function */
  1958. "R_NDS32_RELAX_ENTRY", /* name */
  1959. false, /* partial_inplace */
  1960. 0xffffffff, /* src_mask */
  1961. 0xffffffff, /* dst_mask */
  1962. false), /* pcrel_offset */
  1963. HOWTO3 (R_NDS32_GOT_SUFF, /* type */
  1964. 0, /* rightshift */
  1965. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1966. 32, /* bitsize */
  1967. false, /* pc_relative */
  1968. 0, /* bitpos */
  1969. complain_overflow_dont,/* complain_on_overflow */
  1970. nds32_elf_ignore_reloc,/* special_function */
  1971. "R_NDS32_GOT_SUFF", /* name */
  1972. false, /* partial_inplace */
  1973. 0xffffffff, /* src_mask */
  1974. 0xffffffff, /* dst_mask */
  1975. false), /* pcrel_offset */
  1976. HOWTO3 (R_NDS32_GOTOFF_SUFF, /* type */
  1977. 0, /* rightshift */
  1978. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1979. 32, /* bitsize */
  1980. false, /* pc_relative */
  1981. 0, /* bitpos */
  1982. complain_overflow_bitfield,/* complain_on_overflow */
  1983. nds32_elf_ignore_reloc,/* special_function */
  1984. "R_NDS32_GOTOFF_SUFF", /* name */
  1985. false, /* partial_inplace */
  1986. 0xffffffff, /* src_mask */
  1987. 0xffffffff, /* dst_mask */
  1988. false), /* pcrel_offset */
  1989. HOWTO3 (R_NDS32_PLT_GOT_SUFF, /* type */
  1990. 0, /* rightshift */
  1991. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1992. 32, /* bitsize */
  1993. false, /* pc_relative */
  1994. 0, /* bitpos */
  1995. complain_overflow_dont,/* complain_on_overflow */
  1996. nds32_elf_ignore_reloc,/* special_function */
  1997. "R_NDS32_PLT_GOT_SUFF",/* name */
  1998. false, /* partial_inplace */
  1999. 0xffffffff, /* src_mask */
  2000. 0xffffffff, /* dst_mask */
  2001. false), /* pcrel_offset */
  2002. HOWTO3 (R_NDS32_MULCALL_SUFF, /* type */
  2003. 0, /* rightshift */
  2004. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2005. 32, /* bitsize */
  2006. false, /* pc_relative */
  2007. 0, /* bitpos */
  2008. complain_overflow_dont,/* complain_on_overflow */
  2009. nds32_elf_ignore_reloc,/* special_function */
  2010. "R_NDS32_MULCALL_SUFF",/* name */
  2011. false, /* partial_inplace */
  2012. 0xffffffff, /* src_mask */
  2013. 0xffffffff, /* dst_mask */
  2014. false), /* pcrel_offset */
  2015. HOWTO3 (R_NDS32_PTR, /* type */
  2016. 0, /* rightshift */
  2017. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2018. 32, /* bitsize */
  2019. false, /* pc_relative */
  2020. 0, /* bitpos */
  2021. complain_overflow_dont,/* complain_on_overflow */
  2022. nds32_elf_ignore_reloc,/* special_function */
  2023. "R_NDS32_PTR", /* name */
  2024. false, /* partial_inplace */
  2025. 0xffffffff, /* src_mask */
  2026. 0xffffffff, /* dst_mask */
  2027. false), /* pcrel_offset */
  2028. HOWTO3 (R_NDS32_PTR_COUNT, /* type */
  2029. 0, /* rightshift */
  2030. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2031. 32, /* bitsize */
  2032. false, /* pc_relative */
  2033. 0, /* bitpos */
  2034. complain_overflow_dont,/* complain_on_overflow */
  2035. nds32_elf_ignore_reloc,/* special_function */
  2036. "R_NDS32_PTR_COUNT", /* name */
  2037. false, /* partial_inplace */
  2038. 0xffffffff, /* src_mask */
  2039. 0xffffffff, /* dst_mask */
  2040. false), /* pcrel_offset */
  2041. HOWTO3 (R_NDS32_PTR_RESOLVED, /* type */
  2042. 0, /* rightshift */
  2043. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2044. 32, /* bitsize */
  2045. false, /* pc_relative */
  2046. 0, /* bitpos */
  2047. complain_overflow_dont,/* complain_on_overflow */
  2048. nds32_elf_ignore_reloc,/* special_function */
  2049. "R_NDS32_PTR_RESOLVED",/* name */
  2050. false, /* partial_inplace */
  2051. 0xffffffff, /* src_mask */
  2052. 0xffffffff, /* dst_mask */
  2053. false), /* pcrel_offset */
  2054. HOWTO3 (R_NDS32_PLTBLOCK, /* type */
  2055. 0, /* rightshift */
  2056. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2057. 32, /* bitsize */
  2058. false, /* pc_relative */
  2059. 0, /* bitpos */
  2060. complain_overflow_dont,/* complain_on_overflow */
  2061. nds32_elf_ignore_reloc,/* special_function */
  2062. "R_NDS32_PLTBLOCK", /* name */
  2063. false, /* partial_inplace */
  2064. 0xffffffff, /* src_mask */
  2065. 0xffffffff, /* dst_mask */
  2066. false), /* pcrel_offset */
  2067. HOWTO3 (R_NDS32_RELAX_REGION_BEGIN,/* type */
  2068. 0, /* rightshift */
  2069. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2070. 32, /* bitsize */
  2071. false, /* pc_relative */
  2072. 0, /* bitpos */
  2073. complain_overflow_dont,/* complain_on_overflow */
  2074. nds32_elf_ignore_reloc,/* special_function */
  2075. "R_NDS32_RELAX_REGION_BEGIN",/* name */
  2076. false, /* partial_inplace */
  2077. 0xffffffff, /* src_mask */
  2078. 0xffffffff, /* dst_mask */
  2079. false), /* pcrel_offset */
  2080. HOWTO3 (R_NDS32_RELAX_REGION_END,/* type */
  2081. 0, /* rightshift */
  2082. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2083. 32, /* bitsize */
  2084. false, /* pc_relative */
  2085. 0, /* bitpos */
  2086. complain_overflow_dont,/* complain_on_overflow */
  2087. nds32_elf_ignore_reloc,/* special_function */
  2088. "R_NDS32_RELAX_REGION_END",/* name */
  2089. false, /* partial_inplace */
  2090. 0xffffffff, /* src_mask */
  2091. 0xffffffff, /* dst_mask */
  2092. false), /* pcrel_offset */
  2093. HOWTO3 (R_NDS32_MINUEND, /* type */
  2094. 0, /* rightshift */
  2095. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2096. 32, /* bitsize */
  2097. false, /* pc_relative */
  2098. 0, /* bitpos */
  2099. complain_overflow_dont,/* complain_on_overflow */
  2100. nds32_elf_ignore_reloc,/* special_function */
  2101. "R_NDS32_MINUEND", /* name */
  2102. false, /* partial_inplace */
  2103. 0xffffffff, /* src_mask */
  2104. 0xffffffff, /* dst_mask */
  2105. false), /* pcrel_offset */
  2106. HOWTO3 (R_NDS32_SUBTRAHEND, /* type */
  2107. 0, /* rightshift */
  2108. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2109. 32, /* bitsize */
  2110. false, /* pc_relative */
  2111. 0, /* bitpos */
  2112. complain_overflow_dont,/* complain_on_overflow */
  2113. nds32_elf_ignore_reloc,/* special_function */
  2114. "R_NDS32_SUBTRAHEND", /* name */
  2115. false, /* partial_inplace */
  2116. 0xffffffff, /* src_mask */
  2117. 0xffffffff, /* dst_mask */
  2118. false), /* pcrel_offset */
  2119. HOWTO3 (R_NDS32_DIFF8, /* type */
  2120. 0, /* rightshift */
  2121. 0, /* size (0 = byte, 1 = short, 2 = long) */
  2122. 8, /* bitsize */
  2123. false, /* pc_relative */
  2124. 0, /* bitpos */
  2125. complain_overflow_dont,/* complain_on_overflow */
  2126. nds32_elf_ignore_reloc,/* special_function */
  2127. "R_NDS32_DIFF8", /* name */
  2128. false, /* partial_inplace */
  2129. 0x000000ff, /* src_mask */
  2130. 0x000000ff, /* dst_mask */
  2131. false), /* pcrel_offset */
  2132. HOWTO3 (R_NDS32_DIFF16, /* type */
  2133. 0, /* rightshift */
  2134. 1, /* size (0 = byte, 1 = short, 2 = long) */
  2135. 16, /* bitsize */
  2136. false, /* pc_relative */
  2137. 0, /* bitpos */
  2138. complain_overflow_dont,/* complain_on_overflow */
  2139. nds32_elf_ignore_reloc,/* special_function */
  2140. "R_NDS32_DIFF16", /* name */
  2141. false, /* partial_inplace */
  2142. 0x0000ffff, /* src_mask */
  2143. 0x0000ffff, /* dst_mask */
  2144. false), /* pcrel_offset */
  2145. HOWTO3 (R_NDS32_DIFF32, /* type */
  2146. 0, /* rightshift */
  2147. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2148. 32, /* bitsize */
  2149. false, /* pc_relative */
  2150. 0, /* bitpos */
  2151. complain_overflow_dont,/* complain_on_overflow */
  2152. nds32_elf_ignore_reloc,/* special_function */
  2153. "R_NDS32_DIFF32", /* name */
  2154. false, /* partial_inplace */
  2155. 0xffffffff, /* src_mask */
  2156. 0xffffffff, /* dst_mask */
  2157. false), /* pcrel_offset */
  2158. HOWTO3 (R_NDS32_DIFF_ULEB128, /* type */
  2159. 0, /* rightshift */
  2160. 0, /* size (0 = byte, 1 = short, 2 = long) */
  2161. 0, /* bitsize */
  2162. false, /* pc_relative */
  2163. 0, /* bitpos */
  2164. complain_overflow_dont,/* complain_on_overflow */
  2165. nds32_elf_ignore_reloc,/* special_function */
  2166. "R_NDS32_DIFF_ULEB128",/* name */
  2167. false, /* partial_inplace */
  2168. 0xffffffff, /* src_mask */
  2169. 0xffffffff, /* dst_mask */
  2170. false), /* pcrel_offset */
  2171. HOWTO3 (R_NDS32_DATA, /* type */
  2172. 0, /* rightshift */
  2173. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2174. 32, /* bitsize */
  2175. false, /* pc_relative */
  2176. 0, /* bitpos */
  2177. complain_overflow_dont,/* complain_on_overflow */
  2178. nds32_elf_ignore_reloc,/* special_function */
  2179. "R_NDS32_DATA", /* name */
  2180. false, /* partial_inplace */
  2181. 0xffffffff, /* src_mask */
  2182. 0xffffffff, /* dst_mask */
  2183. false), /* pcrel_offset */
  2184. HOWTO3 (R_NDS32_TRAN, /* type */
  2185. 0, /* rightshift */
  2186. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2187. 32, /* bitsize */
  2188. false, /* pc_relative */
  2189. 0, /* bitpos */
  2190. complain_overflow_dont,/* complain_on_overflow */
  2191. nds32_elf_ignore_reloc,/* special_function */
  2192. "R_NDS32_TRAN", /* name */
  2193. false, /* partial_inplace */
  2194. 0xffffffff, /* src_mask */
  2195. 0xffffffff, /* dst_mask */
  2196. false), /* pcrel_offset */
  2197. HOWTO3 (R_NDS32_TLS_LE_ADD, /* type */
  2198. 0, /* rightshift */
  2199. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2200. 32, /* bitsize */
  2201. false, /* pc_relative */
  2202. 0, /* bitpos */
  2203. complain_overflow_dont,/* complain_on_overflow */
  2204. nds32_elf_ignore_reloc,/* special_function */
  2205. "R_NDS32_TLS_LE_ADD", /* name */
  2206. false, /* partial_inplace */
  2207. 0xffffffff, /* src_mask */
  2208. 0xffffffff, /* dst_mask */
  2209. false), /* pcrel_offset */
  2210. HOWTO3 (R_NDS32_TLS_LE_LS, /* type */
  2211. 0, /* rightshift */
  2212. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2213. 32, /* bitsize */
  2214. false, /* pc_relative */
  2215. 0, /* bitpos */
  2216. complain_overflow_dont,/* complain_on_overflow */
  2217. nds32_elf_ignore_reloc,/* special_function */
  2218. "R_NDS32_TLS_LE_LS", /* name */
  2219. false, /* partial_inplace */
  2220. 0xffffffff, /* src_mask */
  2221. 0xffffffff, /* dst_mask */
  2222. false), /* pcrel_offset */
  2223. HOWTO3 (R_NDS32_EMPTY, /* type */
  2224. 0, /* rightshift */
  2225. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2226. 32, /* bitsize */
  2227. false, /* pc_relative */
  2228. 0, /* bitpos */
  2229. complain_overflow_dont,/* complain_on_overflow */
  2230. nds32_elf_ignore_reloc,/* special_function */
  2231. "R_NDS32_EMPTY", /* name */
  2232. false, /* partial_inplace */
  2233. 0xffffffff, /* src_mask */
  2234. 0xffffffff, /* dst_mask */
  2235. false), /* pcrel_offset */
  2236. /* TLS GD/LD description address base addition. */
  2237. HOWTO3 (R_NDS32_TLS_DESC_ADD, /* type */
  2238. 0, /* rightshift */
  2239. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2240. 32, /* bitsize */
  2241. false, /* pc_relative */
  2242. 0, /* bitpos */
  2243. complain_overflow_dont,/* complain_on_overflow */
  2244. nds32_elf_ignore_reloc,/* special_function */
  2245. "R_NDS32_TLS_DESC_ADD",/* name */
  2246. false, /* partial_inplace */
  2247. 0xffffffff, /* src_mask */
  2248. 0xffffffff, /* dst_mask */
  2249. false), /* pcrel_offset */
  2250. /* TLS GD/LD description function load. */
  2251. HOWTO3 (R_NDS32_TLS_DESC_FUNC,/* type */
  2252. 0, /* rightshift */
  2253. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2254. 32, /* bitsize */
  2255. false, /* pc_relative */
  2256. 0, /* bitpos */
  2257. complain_overflow_dont,/* complain_on_overflow */
  2258. nds32_elf_ignore_reloc,/* special_function */
  2259. "R_NDS32_TLS_DESC_FUNC",/* name */
  2260. false, /* partial_inplace */
  2261. 0xffffffff, /* src_mask */
  2262. 0xffffffff, /* dst_mask */
  2263. false), /* pcrel_offset */
  2264. /* TLS DESC resolve function call. */
  2265. HOWTO3 (R_NDS32_TLS_DESC_CALL,/* type */
  2266. 0, /* rightshift */
  2267. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2268. 32, /* bitsize */
  2269. false, /* pc_relative */
  2270. 0, /* bitpos */
  2271. complain_overflow_dont,/* complain_on_overflow */
  2272. nds32_elf_ignore_reloc,/* special_function */
  2273. "R_NDS32_TLS_DESC_CALL",/* name */
  2274. false, /* partial_inplace */
  2275. 0xffffffff, /* src_mask */
  2276. 0xffffffff, /* dst_mask */
  2277. false), /* pcrel_offset */
  2278. /* TLS DESC variable access. */
  2279. HOWTO3 (R_NDS32_TLS_DESC_MEM, /* type */
  2280. 0, /* rightshift */
  2281. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2282. 32, /* bitsize */
  2283. false, /* pc_relative */
  2284. 0, /* bitpos */
  2285. complain_overflow_dont,/* complain_on_overflow */
  2286. nds32_elf_ignore_reloc,/* special_function */
  2287. "R_NDS32_TLS_DESC_MEM",/* name */
  2288. false, /* partial_inplace */
  2289. 0xffffffff, /* src_mask */
  2290. 0xffffffff, /* dst_mask */
  2291. false), /* pcrel_offset */
  2292. /* TLS GD/LD description mark (@tlsdec). */
  2293. HOWTO3 (R_NDS32_RELAX_REMOVE, /* type */
  2294. 0, /* rightshift */
  2295. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2296. 32, /* bitsize */
  2297. false, /* pc_relative */
  2298. 0, /* bitpos */
  2299. complain_overflow_dont,/* complain_on_overflow */
  2300. nds32_elf_ignore_reloc,/* special_function */
  2301. "R_NDS32_REMOVE", /* name */
  2302. false, /* partial_inplace */
  2303. 0xffffffff, /* src_mask */
  2304. 0xffffffff, /* dst_mask */
  2305. false), /* pcrel_offset */
  2306. /* TLS GD/LD description mark (@tlsdec). */
  2307. HOWTO3 (R_NDS32_RELAX_GROUP, /* type */
  2308. 0, /* rightshift */
  2309. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2310. 32, /* bitsize */
  2311. false, /* pc_relative */
  2312. 0, /* bitpos */
  2313. complain_overflow_dont,/* complain_on_overflow */
  2314. nds32_elf_ignore_reloc,/* special_function */
  2315. "R_NDS32_GROUP", /* name */
  2316. false, /* partial_inplace */
  2317. 0xffffffff, /* src_mask */
  2318. 0xffffffff, /* dst_mask */
  2319. false), /* pcrel_offset */
  2320. HOWTO3 (R_NDS32_TLS_IEGP_LW, /* type */
  2321. 0, /* rightshift */
  2322. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2323. 32, /* bitsize */
  2324. false, /* pc_relative */
  2325. 0, /* bitpos */
  2326. complain_overflow_dont,/* complain_on_overflow */
  2327. nds32_elf_ignore_reloc,/* special_function */
  2328. "R_NDS32_TLS_IEGP_LW", /* name */
  2329. false, /* partial_inplace */
  2330. 0xffffffff, /* src_mask */
  2331. 0xffffffff, /* dst_mask */
  2332. false), /* pcrel_offset */
  2333. /* LA and FLSI relaxation. */
  2334. HOWTO3 (R_NDS32_LSI, /* type */
  2335. 0, /* rightshift */
  2336. 2, /* size (0 = byte, 1 = short, 2 = long) */
  2337. 32, /* bitsize */
  2338. false, /* pc_relative */
  2339. 0, /* bitpos */
  2340. complain_overflow_dont,/* complain_on_overflow */
  2341. nds32_elf_ignore_reloc,/* special_function */
  2342. "R_NDS32_LSI", /* name */
  2343. false, /* partial_inplace */
  2344. 0xffffffff, /* src_mask */
  2345. 0xffffffff, /* dst_mask */
  2346. false),
  2347. };
  2348. static unsigned long dl_tlsdesc_lazy_trampoline[] =
  2349. {
  2350. 0x46200000, /* sethi $r2,#0x0 */
  2351. 0x58210000, /* ori $r2,$r2,#0x0 */
  2352. 0x40217400, /* add $r2,$r2,$gp */
  2353. 0x04210000, /* lwi $r2,[$r2+#0x0] */
  2354. 0x46300000, /* sethi $r3,#0x0 */
  2355. 0x58318000, /* ori $r3,$r3,#0x0 */
  2356. 0x4031f400, /* add $r3,$r3,$gp */
  2357. 0x4a000800, /* jr $r2 */
  2358. };
  2359. static void
  2360. nds32_put_trampoline (void *contents, const unsigned long *template,
  2361. unsigned count)
  2362. {
  2363. unsigned ix;
  2364. for (ix = 0; ix != count; ix++)
  2365. {
  2366. unsigned long insn = template[ix];
  2367. bfd_putb32 (insn, (char *) contents + ix * 4);
  2368. }
  2369. }
  2370. /* nds32_insertion_sort sorts an array with nmemb elements of size size.
  2371. This prototype is the same as qsort (). */
  2372. static void
  2373. nds32_insertion_sort (void *base, size_t nmemb, size_t size,
  2374. int (*compar) (const void *lhs, const void *rhs))
  2375. {
  2376. char *ptr = (char *) base;
  2377. int i, j;
  2378. char tmp[sizeof (Elf_Internal_Rela)];
  2379. BFD_ASSERT (size <= sizeof (tmp));
  2380. /* If i is less than j, i is inserted before j.
  2381. |---- j ----- i --------------|
  2382. \ / \ /
  2383. sorted unsorted
  2384. */
  2385. for (i = 1; i < (int) nmemb; i++)
  2386. {
  2387. for (j = (i - 1); j >= 0; j--)
  2388. if (compar (ptr + i * size, ptr + j * size) >= 0)
  2389. break;
  2390. j++;
  2391. if (i == j)
  2392. continue; /* i is in order. */
  2393. memcpy (tmp, ptr + i * size, size);
  2394. memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
  2395. memcpy (ptr + j * size, tmp, size);
  2396. }
  2397. }
  2398. /* Sort relocation by r_offset.
  2399. We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
  2400. algorithm. Relocations at the same r_offset must keep their order.
  2401. For example, RELAX_ENTRY must be the very first relocation entry.
  2402. Currently, this function implements insertion-sort.
  2403. FIXME: If we already sort them in assembler, why bother sort them
  2404. here again? */
  2405. static int
  2406. compar_reloc (const void *lhs, const void *rhs)
  2407. {
  2408. const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
  2409. const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
  2410. if (l->r_offset > r->r_offset)
  2411. return 1;
  2412. else if (l->r_offset == r->r_offset)
  2413. return 0;
  2414. else
  2415. return -1;
  2416. }
  2417. /* Functions listed below are only used for old relocs.
  2418. nds32_elf_9_pcrel_reloc
  2419. nds32_elf_do_9_pcrel_reloc
  2420. nds32_elf_hi20_reloc
  2421. nds32_elf_relocate_hi20
  2422. nds32_elf_lo12_reloc
  2423. nds32_elf_sda15_reloc
  2424. nds32_elf_generic_reloc. */
  2425. /* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc. */
  2426. static bfd_reloc_status_type
  2427. nds32_elf_9_pcrel_reloc (bfd * abfd,
  2428. arelent * reloc_entry,
  2429. asymbol * symbol,
  2430. void * data,
  2431. asection * input_section,
  2432. bfd * output_bfd,
  2433. char ** error_message ATTRIBUTE_UNUSED)
  2434. {
  2435. /* This part is from bfd_elf_generic_reloc. */
  2436. if (output_bfd != (bfd *) NULL
  2437. && (symbol->flags & BSF_SECTION_SYM) == 0
  2438. && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
  2439. {
  2440. reloc_entry->address += input_section->output_offset;
  2441. return bfd_reloc_ok;
  2442. }
  2443. if (output_bfd != NULL)
  2444. {
  2445. /* FIXME: See bfd_perform_relocation. Is this right? */
  2446. return bfd_reloc_continue;
  2447. }
  2448. return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
  2449. input_section,
  2450. data, reloc_entry->address,
  2451. symbol->section,
  2452. (symbol->value
  2453. + symbol->section->output_section->vma
  2454. + symbol->section->output_offset),
  2455. reloc_entry->addend);
  2456. }
  2457. /* Utility to actually perform an R_NDS32_9_PCREL reloc. */
  2458. #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
  2459. static bfd_reloc_status_type
  2460. nds32_elf_do_9_pcrel_reloc (bfd * abfd,
  2461. reloc_howto_type * howto,
  2462. asection * input_section,
  2463. bfd_byte * data,
  2464. bfd_vma offset,
  2465. asection * symbol_section ATTRIBUTE_UNUSED,
  2466. bfd_vma symbol_value,
  2467. bfd_vma addend)
  2468. {
  2469. bfd_signed_vma relocation;
  2470. unsigned short x;
  2471. bfd_reloc_status_type status;
  2472. /* Sanity check the address (offset in section). */
  2473. if (offset > bfd_get_section_limit (abfd, input_section))
  2474. return bfd_reloc_outofrange;
  2475. relocation = symbol_value + addend;
  2476. /* Make it pc relative. */
  2477. relocation -= (input_section->output_section->vma
  2478. + input_section->output_offset);
  2479. /* These jumps mask off the lower two bits of the current address
  2480. before doing pcrel calculations. */
  2481. relocation -= (offset & -(bfd_vma) 2);
  2482. if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
  2483. status = bfd_reloc_overflow;
  2484. else
  2485. status = bfd_reloc_ok;
  2486. x = bfd_getb16 (data + offset);
  2487. relocation >>= howto->rightshift;
  2488. relocation <<= howto->bitpos;
  2489. x = (x & ~howto->dst_mask)
  2490. | (((x & howto->src_mask) + relocation) & howto->dst_mask);
  2491. bfd_putb16 ((bfd_vma) x, data + offset);
  2492. return status;
  2493. }
  2494. /* Handle the R_NDS32_HI20_[SU]LO relocs.
  2495. HI20_SLO is for the add3 and load/store with displacement instructions.
  2496. HI20 is for the or3 instruction.
  2497. For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
  2498. the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
  2499. we must add one to the high 16 bytes (which will get subtracted off when
  2500. the low 16 bits are added).
  2501. These relocs have to be done in combination with an R_NDS32_LO12 reloc
  2502. because there is a carry from the LO12 to the HI20. Here we just save
  2503. the information we need; we do the actual relocation when we see the LO12.
  2504. This code is copied from the elf32-mips.c. We also support an arbitrary
  2505. number of HI20 relocs to be associated with a single LO12 reloc. The
  2506. assembler sorts the relocs to ensure each HI20 immediately precedes its
  2507. LO12. However if there are multiple copies, the assembler may not find
  2508. the real LO12 so it picks the first one it finds. */
  2509. struct nds32_hi20
  2510. {
  2511. struct nds32_hi20 *next;
  2512. bfd_byte *addr;
  2513. bfd_vma addend;
  2514. };
  2515. static struct nds32_hi20 *nds32_hi20_list;
  2516. static bfd_reloc_status_type
  2517. nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  2518. arelent *reloc_entry,
  2519. asymbol *symbol,
  2520. void *data,
  2521. asection *input_section,
  2522. bfd *output_bfd,
  2523. char **error_message ATTRIBUTE_UNUSED)
  2524. {
  2525. bfd_reloc_status_type ret;
  2526. bfd_vma relocation;
  2527. struct nds32_hi20 *n;
  2528. /* This part is from bfd_elf_generic_reloc.
  2529. If we're relocating, and this an external symbol, we don't want
  2530. to change anything. */
  2531. if (output_bfd != (bfd *) NULL
  2532. && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
  2533. {
  2534. reloc_entry->address += input_section->output_offset;
  2535. return bfd_reloc_ok;
  2536. }
  2537. /* Sanity check the address (offset in section). */
  2538. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  2539. return bfd_reloc_outofrange;
  2540. ret = bfd_reloc_ok;
  2541. if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
  2542. ret = bfd_reloc_undefined;
  2543. if (bfd_is_com_section (symbol->section))
  2544. relocation = 0;
  2545. else
  2546. relocation = symbol->value;
  2547. relocation += symbol->section->output_section->vma;
  2548. relocation += symbol->section->output_offset;
  2549. relocation += reloc_entry->addend;
  2550. /* Save the information, and let LO12 do the actual relocation. */
  2551. n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
  2552. if (n == NULL)
  2553. return bfd_reloc_outofrange;
  2554. n->addr = (bfd_byte *) data + reloc_entry->address;
  2555. n->addend = relocation;
  2556. n->next = nds32_hi20_list;
  2557. nds32_hi20_list = n;
  2558. if (output_bfd != (bfd *) NULL)
  2559. reloc_entry->address += input_section->output_offset;
  2560. return ret;
  2561. }
  2562. /* Handle an NDS32 ELF HI20 reloc. */
  2563. static void
  2564. nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
  2565. int type ATTRIBUTE_UNUSED,
  2566. Elf_Internal_Rela *relhi,
  2567. Elf_Internal_Rela *rello,
  2568. bfd_byte *contents,
  2569. bfd_vma addend)
  2570. {
  2571. unsigned long insn;
  2572. bfd_vma addlo;
  2573. insn = bfd_getb32 (contents + relhi->r_offset);
  2574. addlo = bfd_getb32 (contents + rello->r_offset);
  2575. addlo &= 0xfff;
  2576. addend += ((insn & 0xfffff) << 20) + addlo;
  2577. insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
  2578. bfd_putb32 (insn, contents + relhi->r_offset);
  2579. }
  2580. /* Do an R_NDS32_LO12 relocation. This is a straightforward 12 bit
  2581. inplace relocation; this function exists in order to do the
  2582. R_NDS32_HI20_[SU]LO relocation described above. */
  2583. static bfd_reloc_status_type
  2584. nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
  2585. void *data, asection *input_section, bfd *output_bfd,
  2586. char **error_message)
  2587. {
  2588. /* This part is from bfd_elf_generic_reloc.
  2589. If we're relocating, and this an external symbol, we don't want
  2590. to change anything. */
  2591. if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
  2592. && reloc_entry->addend == 0)
  2593. {
  2594. reloc_entry->address += input_section->output_offset;
  2595. return bfd_reloc_ok;
  2596. }
  2597. if (nds32_hi20_list != NULL)
  2598. {
  2599. struct nds32_hi20 *l;
  2600. l = nds32_hi20_list;
  2601. while (l != NULL)
  2602. {
  2603. unsigned long insn;
  2604. unsigned long val;
  2605. unsigned long vallo;
  2606. struct nds32_hi20 *next;
  2607. /* Do the HI20 relocation. Note that we actually don't need
  2608. to know anything about the LO12 itself, except where to
  2609. find the low 12 bits of the addend needed by the LO12. */
  2610. insn = bfd_getb32 (l->addr);
  2611. vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
  2612. vallo &= 0xfff;
  2613. switch (reloc_entry->howto->type)
  2614. {
  2615. case R_NDS32_LO12S3:
  2616. vallo <<= 3;
  2617. break;
  2618. case R_NDS32_LO12S2:
  2619. vallo <<= 2;
  2620. break;
  2621. case R_NDS32_LO12S1:
  2622. vallo <<= 1;
  2623. break;
  2624. case R_NDS32_LO12S0:
  2625. vallo <<= 0;
  2626. break;
  2627. }
  2628. val = ((insn & 0xfffff) << 12) + vallo;
  2629. val += l->addend;
  2630. insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
  2631. bfd_putb32 ((bfd_vma) insn, l->addr);
  2632. next = l->next;
  2633. free (l);
  2634. l = next;
  2635. }
  2636. nds32_hi20_list = NULL;
  2637. }
  2638. /* Now do the LO12 reloc in the usual way.
  2639. ??? It would be nice to call bfd_elf_generic_reloc here,
  2640. but we have partial_inplace set. bfd_elf_generic_reloc will
  2641. pass the handling back to bfd_install_relocation which will install
  2642. a section relative addend which is wrong. */
  2643. return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
  2644. input_section, output_bfd, error_message);
  2645. }
  2646. /* Do generic partial_inplace relocation.
  2647. This is a local replacement for bfd_elf_generic_reloc. */
  2648. static bfd_reloc_status_type
  2649. nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
  2650. asymbol *symbol, void *data, asection *input_section,
  2651. bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
  2652. {
  2653. bfd_reloc_status_type ret;
  2654. bfd_vma relocation;
  2655. bfd_byte *inplace_address;
  2656. /* This part is from bfd_elf_generic_reloc.
  2657. If we're relocating, and this an external symbol, we don't want
  2658. to change anything. */
  2659. if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
  2660. && reloc_entry->addend == 0)
  2661. {
  2662. reloc_entry->address += input_section->output_offset;
  2663. return bfd_reloc_ok;
  2664. }
  2665. /* Now do the reloc in the usual way.
  2666. ??? It would be nice to call bfd_elf_generic_reloc here,
  2667. but we have partial_inplace set. bfd_elf_generic_reloc will
  2668. pass the handling back to bfd_install_relocation which will install
  2669. a section relative addend which is wrong. */
  2670. /* Sanity check the address (offset in section). */
  2671. if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
  2672. return bfd_reloc_outofrange;
  2673. ret = bfd_reloc_ok;
  2674. if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
  2675. ret = bfd_reloc_undefined;
  2676. if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
  2677. relocation = 0;
  2678. else
  2679. relocation = symbol->value;
  2680. /* Only do this for a final link. */
  2681. if (output_bfd == (bfd *) NULL)
  2682. {
  2683. relocation += symbol->section->output_section->vma;
  2684. relocation += symbol->section->output_offset;
  2685. }
  2686. relocation += reloc_entry->addend;
  2687. switch (reloc_entry->howto->type)
  2688. {
  2689. case R_NDS32_LO12S3:
  2690. relocation >>= 3;
  2691. break;
  2692. case R_NDS32_LO12S2:
  2693. relocation >>= 2;
  2694. break;
  2695. case R_NDS32_LO12S1:
  2696. relocation >>= 1;
  2697. break;
  2698. case R_NDS32_LO12S0:
  2699. default:
  2700. relocation >>= 0;
  2701. break;
  2702. }
  2703. inplace_address = (bfd_byte *) data + reloc_entry->address;
  2704. #define DOIT(x) \
  2705. x = ((x & ~reloc_entry->howto->dst_mask) | \
  2706. (((x & reloc_entry->howto->src_mask) + relocation) & \
  2707. reloc_entry->howto->dst_mask))
  2708. switch (reloc_entry->howto->size)
  2709. {
  2710. case 1:
  2711. {
  2712. short x = bfd_getb16 (inplace_address);
  2713. DOIT (x);
  2714. bfd_putb16 ((bfd_vma) x, inplace_address);
  2715. }
  2716. break;
  2717. case 2:
  2718. {
  2719. unsigned long x = bfd_getb32 (inplace_address);
  2720. DOIT (x);
  2721. bfd_putb32 ((bfd_vma) x, inplace_address);
  2722. }
  2723. break;
  2724. default:
  2725. BFD_ASSERT (0);
  2726. }
  2727. if (output_bfd != (bfd *) NULL)
  2728. reloc_entry->address += input_section->output_offset;
  2729. return ret;
  2730. }
  2731. /* Handle the R_NDS32_SDA15 reloc.
  2732. This reloc is used to compute the address of objects in the small data area
  2733. and to perform loads and stores from that area.
  2734. The lower 15 bits are sign extended and added to the register specified
  2735. in the instruction, which is assumed to point to _SDA_BASE_.
  2736. Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
  2737. the access size, this must be taken care of. */
  2738. static bfd_reloc_status_type
  2739. nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
  2740. asymbol *symbol, void *data ATTRIBUTE_UNUSED,
  2741. asection *input_section, bfd *output_bfd,
  2742. char **error_message ATTRIBUTE_UNUSED)
  2743. {
  2744. /* This part is from bfd_elf_generic_reloc. */
  2745. if (output_bfd != (bfd *) NULL
  2746. && (symbol->flags & BSF_SECTION_SYM) == 0
  2747. && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
  2748. {
  2749. reloc_entry->address += input_section->output_offset;
  2750. return bfd_reloc_ok;
  2751. }
  2752. if (output_bfd != NULL)
  2753. {
  2754. /* FIXME: See bfd_perform_relocation. Is this right? */
  2755. return bfd_reloc_continue;
  2756. }
  2757. /* FIXME: not sure what to do here yet. But then again, the linker
  2758. may never call us. */
  2759. abort ();
  2760. }
  2761. /* nds32_elf_ignore_reloc is the special function for
  2762. relocation types which don't need to be relocated
  2763. like relaxation relocation types.
  2764. This function simply return bfd_reloc_ok when it is
  2765. invoked. */
  2766. static bfd_reloc_status_type
  2767. nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
  2768. asymbol *symbol ATTRIBUTE_UNUSED,
  2769. void *data ATTRIBUTE_UNUSED, asection *input_section,
  2770. bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
  2771. {
  2772. if (output_bfd != NULL)
  2773. reloc_entry->address += input_section->output_offset;
  2774. return bfd_reloc_ok;
  2775. }
  2776. /* Map BFD reloc types to NDS32 ELF reloc types. */
  2777. struct nds32_reloc_map_entry
  2778. {
  2779. bfd_reloc_code_real_type bfd_reloc_val;
  2780. unsigned char elf_reloc_val;
  2781. };
  2782. static const struct nds32_reloc_map_entry nds32_reloc_map[] =
  2783. {
  2784. {BFD_RELOC_NONE, R_NDS32_NONE},
  2785. {BFD_RELOC_16, R_NDS32_16_RELA},
  2786. {BFD_RELOC_32, R_NDS32_32_RELA},
  2787. {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
  2788. {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
  2789. {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
  2790. {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
  2791. {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
  2792. {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
  2793. {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
  2794. {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
  2795. {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
  2796. {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
  2797. {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
  2798. {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
  2799. {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
  2800. {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
  2801. {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
  2802. {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
  2803. {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
  2804. {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
  2805. {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
  2806. {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
  2807. {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
  2808. {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
  2809. {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
  2810. {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
  2811. {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
  2812. {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
  2813. {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
  2814. {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
  2815. {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
  2816. {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
  2817. {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
  2818. {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
  2819. {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
  2820. {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
  2821. {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
  2822. {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
  2823. {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
  2824. {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
  2825. {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
  2826. {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
  2827. {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
  2828. {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
  2829. {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
  2830. {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
  2831. {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
  2832. {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
  2833. {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
  2834. {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
  2835. {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
  2836. {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
  2837. {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
  2838. {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
  2839. {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
  2840. {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
  2841. {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
  2842. {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
  2843. {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
  2844. {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
  2845. {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
  2846. {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
  2847. {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
  2848. {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
  2849. {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
  2850. {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
  2851. {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
  2852. {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
  2853. {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
  2854. {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
  2855. {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
  2856. {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
  2857. {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
  2858. {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
  2859. {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
  2860. {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
  2861. {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
  2862. {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
  2863. {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
  2864. {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
  2865. {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
  2866. {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
  2867. {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
  2868. {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
  2869. {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
  2870. {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
  2871. {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
  2872. {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
  2873. {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
  2874. {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
  2875. {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
  2876. {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
  2877. {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
  2878. {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
  2879. {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
  2880. {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
  2881. {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
  2882. {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
  2883. {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
  2884. {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
  2885. {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
  2886. {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
  2887. {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
  2888. {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
  2889. {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
  2890. {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
  2891. /* Not sure. */
  2892. {BFD_RELOC_NDS32_TPOFF, R_NDS32_TLS_TPOFF},
  2893. /* Missing: BFD_RELOC_NDS32_GOTTPOFF. */
  2894. {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
  2895. {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
  2896. {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
  2897. {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
  2898. {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
  2899. {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
  2900. {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
  2901. {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
  2902. {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
  2903. {BFD_RELOC_NDS32_TLS_IE_LO12, R_NDS32_TLS_IE_LO12},
  2904. {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
  2905. {BFD_RELOC_NDS32_TLS_IEGP_HI20, R_NDS32_TLS_IEGP_HI20},
  2906. {BFD_RELOC_NDS32_TLS_IEGP_LO12, R_NDS32_TLS_IEGP_LO12},
  2907. {BFD_RELOC_NDS32_TLS_IEGP_LO12S2, R_NDS32_TLS_IEGP_LO12S2},
  2908. {BFD_RELOC_NDS32_TLS_IEGP_LW, R_NDS32_TLS_IEGP_LW},
  2909. {BFD_RELOC_NDS32_TLS_DESC, R_NDS32_TLS_DESC},
  2910. {BFD_RELOC_NDS32_TLS_DESC_HI20, R_NDS32_TLS_DESC_HI20},
  2911. {BFD_RELOC_NDS32_TLS_DESC_LO12, R_NDS32_TLS_DESC_LO12},
  2912. {BFD_RELOC_NDS32_TLS_DESC_20, R_NDS32_TLS_DESC_20},
  2913. {BFD_RELOC_NDS32_TLS_DESC_SDA17S2, R_NDS32_TLS_DESC_SDA17S2},
  2914. {BFD_RELOC_NDS32_TLS_DESC_ADD, R_NDS32_TLS_DESC_ADD},
  2915. {BFD_RELOC_NDS32_TLS_DESC_FUNC, R_NDS32_TLS_DESC_FUNC},
  2916. {BFD_RELOC_NDS32_TLS_DESC_CALL, R_NDS32_TLS_DESC_CALL},
  2917. {BFD_RELOC_NDS32_TLS_DESC_MEM, R_NDS32_TLS_DESC_MEM},
  2918. {BFD_RELOC_NDS32_REMOVE, R_NDS32_RELAX_REMOVE},
  2919. {BFD_RELOC_NDS32_GROUP, R_NDS32_RELAX_GROUP},
  2920. {BFD_RELOC_NDS32_LSI, R_NDS32_LSI},
  2921. };
  2922. /* Patch tag. */
  2923. static reloc_howto_type *
  2924. bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  2925. const char *r_name)
  2926. {
  2927. unsigned int i;
  2928. for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
  2929. if (nds32_elf_howto_table[i].name != NULL
  2930. && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
  2931. return &nds32_elf_howto_table[i];
  2932. for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
  2933. if (nds32_elf_relax_howto_table[i].name != NULL
  2934. && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
  2935. return &nds32_elf_relax_howto_table[i];
  2936. return NULL;
  2937. }
  2938. static reloc_howto_type *
  2939. bfd_elf32_bfd_reloc_type_table_lookup (unsigned int code)
  2940. {
  2941. if (code < R_NDS32_RELAX_ENTRY)
  2942. {
  2943. if (code < ARRAY_SIZE (nds32_elf_howto_table))
  2944. return &nds32_elf_howto_table[code];
  2945. }
  2946. else
  2947. {
  2948. if (code - R_NDS32_RELAX_ENTRY < ARRAY_SIZE (nds32_elf_relax_howto_table))
  2949. return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
  2950. }
  2951. return NULL;
  2952. }
  2953. static reloc_howto_type *
  2954. bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  2955. bfd_reloc_code_real_type code)
  2956. {
  2957. unsigned int i;
  2958. for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
  2959. {
  2960. if (nds32_reloc_map[i].bfd_reloc_val == code)
  2961. return bfd_elf32_bfd_reloc_type_table_lookup
  2962. (nds32_reloc_map[i].elf_reloc_val);
  2963. }
  2964. return NULL;
  2965. }
  2966. /* Set the howto pointer for an NDS32 ELF reloc. */
  2967. static bool
  2968. nds32_info_to_howto_rel (bfd *abfd, arelent *cache_ptr,
  2969. Elf_Internal_Rela *dst)
  2970. {
  2971. unsigned int r_type = ELF32_R_TYPE (dst->r_info);
  2972. cache_ptr->howto = NULL;
  2973. if (r_type <= R_NDS32_GNU_VTENTRY)
  2974. cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
  2975. if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
  2976. {
  2977. /* xgettext:c-format */
  2978. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  2979. abfd, r_type);
  2980. bfd_set_error (bfd_error_bad_value);
  2981. return false;
  2982. }
  2983. return true;
  2984. }
  2985. static bool
  2986. nds32_info_to_howto (bfd *abfd, arelent *cache_ptr,
  2987. Elf_Internal_Rela *dst)
  2988. {
  2989. unsigned int r_type = ELF32_R_TYPE (dst->r_info);
  2990. cache_ptr->howto = NULL;
  2991. if (r_type == R_NDS32_NONE
  2992. || r_type > R_NDS32_GNU_VTENTRY)
  2993. cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
  2994. if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
  2995. {
  2996. /* xgettext:c-format */
  2997. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  2998. abfd, r_type);
  2999. bfd_set_error (bfd_error_bad_value);
  3000. return false;
  3001. }
  3002. return true;
  3003. }
  3004. /* Support for core dump NOTE sections.
  3005. Reference to include/linux/elfcore.h in Linux. */
  3006. static bool
  3007. nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  3008. {
  3009. int offset;
  3010. size_t size;
  3011. switch (note->descsz)
  3012. {
  3013. case 0x114:
  3014. /* Linux/NDS32 32-bit, ABI1. */
  3015. /* pr_cursig */
  3016. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  3017. /* pr_pid */
  3018. elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
  3019. /* pr_reg */
  3020. offset = 72;
  3021. size = 200;
  3022. break;
  3023. case 0xfc:
  3024. /* Linux/NDS32 32-bit. */
  3025. /* pr_cursig */
  3026. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  3027. /* pr_pid */
  3028. elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
  3029. /* pr_reg */
  3030. offset = 72;
  3031. size = 176;
  3032. break;
  3033. default:
  3034. return false;
  3035. }
  3036. /* Make a ".reg" section. */
  3037. return _bfd_elfcore_make_pseudosection (abfd, ".reg",
  3038. size, note->descpos + offset);
  3039. }
  3040. static bool
  3041. nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  3042. {
  3043. switch (note->descsz)
  3044. {
  3045. case 124:
  3046. /* Linux/NDS32. */
  3047. /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform. */
  3048. elf_tdata (abfd)->core->program =
  3049. _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
  3050. elf_tdata (abfd)->core->command =
  3051. _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
  3052. break;
  3053. default:
  3054. return false;
  3055. }
  3056. /* Note that for some reason, a spurious space is tacked
  3057. onto the end of the args in some (at least one anyway)
  3058. implementations, so strip it off if it exists. */
  3059. {
  3060. char *command = elf_tdata (abfd)->core->command;
  3061. int n = strlen (command);
  3062. if (0 < n && command[n - 1] == ' ')
  3063. command[n - 1] = '\0';
  3064. }
  3065. return true;
  3066. }
  3067. /* Hook called by the linker routine which adds symbols from an object
  3068. file. We must handle the special NDS32 section numbers here.
  3069. We also keep watching for whether we need to create the sdata special
  3070. linker sections. */
  3071. static bool
  3072. nds32_elf_add_symbol_hook (bfd *abfd,
  3073. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  3074. Elf_Internal_Sym *sym,
  3075. const char **namep ATTRIBUTE_UNUSED,
  3076. flagword *flagsp ATTRIBUTE_UNUSED,
  3077. asection **secp, bfd_vma *valp)
  3078. {
  3079. switch (sym->st_shndx)
  3080. {
  3081. case SHN_COMMON:
  3082. /* Common symbols less than the GP size are automatically
  3083. treated as SHN_MIPS_SCOMMON symbols. */
  3084. if (sym->st_size > elf_gp_size (abfd)
  3085. || ELF_ST_TYPE (sym->st_info) == STT_TLS)
  3086. break;
  3087. /* st_value is the alignment constraint.
  3088. That might be its actual size if it is an array or structure. */
  3089. switch (sym->st_value)
  3090. {
  3091. case 1:
  3092. *secp = bfd_make_section_old_way (abfd, ".scommon_b");
  3093. break;
  3094. case 2:
  3095. *secp = bfd_make_section_old_way (abfd, ".scommon_h");
  3096. break;
  3097. case 4:
  3098. *secp = bfd_make_section_old_way (abfd, ".scommon_w");
  3099. break;
  3100. case 8:
  3101. *secp = bfd_make_section_old_way (abfd, ".scommon_d");
  3102. break;
  3103. default:
  3104. return true;
  3105. }
  3106. (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
  3107. *valp = sym->st_size;
  3108. break;
  3109. }
  3110. return true;
  3111. }
  3112. /* This function can figure out the best location for a base register to access
  3113. data relative to this base register
  3114. INPUT:
  3115. sda_d0: size of first DOUBLE WORD data section
  3116. sda_w0: size of first WORD data section
  3117. sda_h0: size of first HALF WORD data section
  3118. sda_b : size of BYTE data section
  3119. sda_hi: size of second HALF WORD data section
  3120. sda_w1: size of second WORD data section
  3121. sda_d1: size of second DOUBLE WORD data section
  3122. OUTPUT:
  3123. offset (always positive) from the beginning of sda_d0 if OK
  3124. a negative error value if fail
  3125. NOTE:
  3126. these 7 sections have to be located back to back if exist
  3127. a pass in 0 value for non-existing section */
  3128. /* Due to the interpretation of simm15 field of load/store depending on
  3129. data accessing size, the organization of base register relative data shall
  3130. like the following figure
  3131. -------------------------------------------
  3132. | DOUBLE WORD sized data (range +/- 128K)
  3133. -------------------------------------------
  3134. | WORD sized data (range +/- 64K)
  3135. -------------------------------------------
  3136. | HALF WORD sized data (range +/- 32K)
  3137. -------------------------------------------
  3138. | BYTE sized data (range +/- 16K)
  3139. -------------------------------------------
  3140. | HALF WORD sized data (range +/- 32K)
  3141. -------------------------------------------
  3142. | WORD sized data (range +/- 64K)
  3143. -------------------------------------------
  3144. | DOUBLE WORD sized data (range +/- 128K)
  3145. -------------------------------------------
  3146. Its base register shall be set to access these data freely. */
  3147. /* We have to figure out the SDA_BASE value, so that we can adjust the
  3148. symbol value correctly. We look up the symbol _SDA_BASE_ in the output
  3149. BFD. If we can't find it, we're stuck. We cache it in the ELF
  3150. target data. We don't need to adjust the symbol value for an
  3151. external symbol if we are producing relocatable output. */
  3152. static asection *sda_rela_sec = NULL;
  3153. #define SDA_SECTION_NUM 10
  3154. static bfd_reloc_status_type
  3155. nds32_elf_final_sda_base (bfd *output_bfd,
  3156. struct bfd_link_info *info,
  3157. bfd_vma *psb,
  3158. bool add_symbol)
  3159. {
  3160. int relax_fp_as_gp;
  3161. struct elf_nds32_link_hash_table *table;
  3162. struct bfd_link_hash_entry *h, *h2;
  3163. long unsigned int total = 0;
  3164. asection *first = NULL, *final = NULL, *temp;
  3165. bfd_vma sda_base = 0;
  3166. h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, true);
  3167. if (!h || (h->type != bfd_link_hash_defined
  3168. && h->type != bfd_link_hash_defweak))
  3169. {
  3170. /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
  3171. 4 byte-aligned. Therefore, it has to set the first section ".data"
  3172. 4 byte-aligned. */
  3173. static const char sec_name[SDA_SECTION_NUM][10] =
  3174. {
  3175. ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
  3176. ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
  3177. };
  3178. size_t i = 0;
  3179. if (output_bfd->sections == NULL)
  3180. {
  3181. *psb = elf_gp (output_bfd);
  3182. return bfd_reloc_ok;
  3183. }
  3184. /* Get the first and final section. */
  3185. while (i < ARRAY_SIZE (sec_name))
  3186. {
  3187. temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
  3188. if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
  3189. first = temp;
  3190. if (temp && (temp->size != 0 || temp->rawsize != 0))
  3191. final = temp;
  3192. /* Summarize the sections in order to check if joining .bss. */
  3193. if (temp && temp->size != 0)
  3194. total += temp->size;
  3195. else if (temp && temp->rawsize != 0)
  3196. total += temp->rawsize;
  3197. i++;
  3198. }
  3199. /* Check .bss size. */
  3200. temp = bfd_get_section_by_name (output_bfd, ".bss");
  3201. if (temp)
  3202. {
  3203. if (temp->size != 0)
  3204. total += temp->size;
  3205. else if (temp->rawsize != 0)
  3206. total += temp->rawsize;
  3207. if (total < 0x80000)
  3208. {
  3209. if (!first && (temp->size != 0 || temp->rawsize != 0))
  3210. first = temp;
  3211. if ((temp->size != 0 || temp->rawsize != 0))
  3212. final = temp;
  3213. }
  3214. }
  3215. if (first && final)
  3216. {
  3217. /* The middle of data region. */
  3218. sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
  3219. /* Find the section sda_base located. */
  3220. i = 0;
  3221. while (i < ARRAY_SIZE (sec_name))
  3222. {
  3223. final = bfd_get_section_by_name (output_bfd, sec_name[i]);
  3224. if (final && (final->size != 0 || final->rawsize != 0)
  3225. && sda_base >= final->vma)
  3226. {
  3227. first = final;
  3228. i++;
  3229. }
  3230. else
  3231. break;
  3232. }
  3233. }
  3234. else
  3235. {
  3236. /* If there is not any default data section in output bfd, try to find
  3237. the first data section. If no data section be found, just simplily
  3238. choose the first output section. */
  3239. temp = output_bfd->sections;
  3240. while (temp)
  3241. {
  3242. if (temp->flags & SEC_ALLOC
  3243. && (((temp->flags & SEC_DATA)
  3244. && ((temp->flags & SEC_READONLY) == 0))
  3245. || (temp->flags & SEC_LOAD) == 0)
  3246. && (temp->size != 0 || temp->rawsize != 0))
  3247. {
  3248. if (!first)
  3249. first = temp;
  3250. final = temp;
  3251. }
  3252. temp = temp->next;
  3253. }
  3254. /* There is no data or bss section. */
  3255. if (!first || (first->size == 0 && first->rawsize == 0))
  3256. {
  3257. first = output_bfd->sections;
  3258. while (first && first->size == 0 && first->rawsize == 0)
  3259. first = first->next;
  3260. }
  3261. /* There is no concrete section. */
  3262. if (!first)
  3263. {
  3264. *psb = elf_gp (output_bfd);
  3265. return bfd_reloc_ok;
  3266. }
  3267. if (final && (final->vma + final->rawsize - first->vma) <= 0x4000)
  3268. sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
  3269. else
  3270. sda_base = first->vma + 0x2000;
  3271. }
  3272. sda_base -= first->vma;
  3273. sda_base = sda_base & (~7);
  3274. if (!_bfd_generic_link_add_one_symbol
  3275. (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
  3276. (bfd_vma) sda_base, (const char *) NULL, false,
  3277. get_elf_backend_data (output_bfd)->collect, &h))
  3278. return false;
  3279. sda_rela_sec = first;
  3280. }
  3281. /* Set _FP_BASE_ to _SDA_BASE_. */
  3282. table = nds32_elf_hash_table (info);
  3283. relax_fp_as_gp = table->relax_fp_as_gp;
  3284. h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME, false, false, false);
  3285. /* _SDA_BASE_ is difined in linker script. */
  3286. if (!first)
  3287. {
  3288. first = h->u.def.section;
  3289. sda_base = h->u.def.value;
  3290. }
  3291. if (relax_fp_as_gp && h2
  3292. && (h2->type == bfd_link_hash_undefweak
  3293. || h2->type == bfd_link_hash_undefined))
  3294. {
  3295. /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
  3296. And set FP equal to SDA_BASE to do relaxation for
  3297. la $fp, _FP_BASE_. */
  3298. if (!_bfd_generic_link_add_one_symbol
  3299. (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
  3300. first, sda_base, (const char *) NULL,
  3301. false, get_elf_backend_data (output_bfd)->collect, &h2))
  3302. return false;
  3303. }
  3304. if (add_symbol)
  3305. {
  3306. if (h)
  3307. {
  3308. /* Now set gp. */
  3309. elf_gp (output_bfd) = (h->u.def.value
  3310. + h->u.def.section->output_section->vma
  3311. + h->u.def.section->output_offset);
  3312. }
  3313. else
  3314. {
  3315. _bfd_error_handler (_("error: can't find symbol: %s"), "_SDA_BASE_");
  3316. return bfd_reloc_dangerous;
  3317. }
  3318. }
  3319. *psb = h->u.def.value
  3320. + h->u.def.section->output_section->vma
  3321. + h->u.def.section->output_offset;
  3322. return bfd_reloc_ok;
  3323. }
  3324. /* Return size of a PLT entry. */
  3325. #define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
  3326. /* Create an entry in an nds32 ELF linker hash table. */
  3327. static struct bfd_hash_entry *
  3328. nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
  3329. struct bfd_hash_table *table,
  3330. const char *string)
  3331. {
  3332. struct elf_nds32_link_hash_entry *ret;
  3333. ret = (struct elf_nds32_link_hash_entry *) entry;
  3334. /* Allocate the structure if it has not already been allocated by a
  3335. subclass. */
  3336. if (ret == NULL)
  3337. ret = (struct elf_nds32_link_hash_entry *)
  3338. bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
  3339. if (ret == NULL)
  3340. return (struct bfd_hash_entry *) ret;
  3341. /* Call the allocation method of the superclass. */
  3342. ret = (struct elf_nds32_link_hash_entry *)
  3343. _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
  3344. if (ret != NULL)
  3345. {
  3346. struct elf_nds32_link_hash_entry *eh;
  3347. eh = (struct elf_nds32_link_hash_entry *) ret;
  3348. eh->tls_type = GOT_UNKNOWN;
  3349. eh->offset_to_gp = 0;
  3350. }
  3351. return (struct bfd_hash_entry *) ret;
  3352. }
  3353. /* Create an nds32 ELF linker hash table. */
  3354. static struct bfd_link_hash_table *
  3355. nds32_elf_link_hash_table_create (bfd *abfd)
  3356. {
  3357. struct elf_nds32_link_hash_table *ret;
  3358. size_t amt = sizeof (struct elf_nds32_link_hash_table);
  3359. ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
  3360. if (ret == NULL)
  3361. return NULL;
  3362. /* Patch tag. */
  3363. if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
  3364. nds32_elf_link_hash_newfunc,
  3365. sizeof (struct elf_nds32_link_hash_entry),
  3366. NDS32_ELF_DATA))
  3367. {
  3368. free (ret);
  3369. return NULL;
  3370. }
  3371. ret->sym_ld_script = NULL;
  3372. return &ret->root.root;
  3373. }
  3374. /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
  3375. shortcuts to them in our hash table. */
  3376. static bool
  3377. create_got_section (bfd *dynobj, struct bfd_link_info *info)
  3378. {
  3379. struct elf_link_hash_table *ehtab;
  3380. if (!_bfd_elf_create_got_section (dynobj, info))
  3381. return false;
  3382. ehtab = elf_hash_table (info);
  3383. ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
  3384. ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
  3385. if (!ehtab->sgot || !ehtab->sgotplt)
  3386. abort ();
  3387. /* _bfd_elf_create_got_section will create it for us. */
  3388. ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
  3389. if (ehtab->srelgot == NULL
  3390. || !bfd_set_section_flags (ehtab->srelgot,
  3391. (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
  3392. | SEC_IN_MEMORY | SEC_LINKER_CREATED
  3393. | SEC_READONLY))
  3394. || !bfd_set_section_alignment (ehtab->srelgot, 2))
  3395. return false;
  3396. return true;
  3397. }
  3398. /* Create dynamic sections when linking against a dynamic object. */
  3399. static bool
  3400. nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  3401. {
  3402. struct elf_link_hash_table *ehtab;
  3403. struct elf_nds32_link_hash_table *htab;
  3404. flagword flags, pltflags;
  3405. register asection *s;
  3406. const struct elf_backend_data *bed;
  3407. int ptralign = 2; /* 32-bit */
  3408. const char *secname;
  3409. char *relname;
  3410. flagword secflags;
  3411. asection *sec;
  3412. bed = get_elf_backend_data (abfd);
  3413. ehtab = elf_hash_table (info);
  3414. htab = nds32_elf_hash_table (info);
  3415. /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
  3416. .rel[a].bss sections. */
  3417. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  3418. | SEC_LINKER_CREATED);
  3419. pltflags = flags;
  3420. pltflags |= SEC_CODE;
  3421. if (bed->plt_not_loaded)
  3422. pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
  3423. if (bed->plt_readonly)
  3424. pltflags |= SEC_READONLY;
  3425. s = bfd_make_section (abfd, ".plt");
  3426. ehtab->splt = s;
  3427. if (s == NULL
  3428. || !bfd_set_section_flags (s, pltflags)
  3429. || !bfd_set_section_alignment (s, bed->plt_alignment))
  3430. return false;
  3431. if (bed->want_plt_sym)
  3432. {
  3433. /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
  3434. .plt section. */
  3435. struct bfd_link_hash_entry *bh = NULL;
  3436. struct elf_link_hash_entry *h;
  3437. if (!(_bfd_generic_link_add_one_symbol
  3438. (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
  3439. (bfd_vma) 0, (const char *) NULL, false,
  3440. get_elf_backend_data (abfd)->collect, &bh)))
  3441. return false;
  3442. h = (struct elf_link_hash_entry *) bh;
  3443. h->def_regular = 1;
  3444. h->type = STT_OBJECT;
  3445. if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
  3446. return false;
  3447. }
  3448. s = bfd_make_section (abfd,
  3449. bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
  3450. ehtab->srelplt = s;
  3451. if (s == NULL
  3452. || !bfd_set_section_flags (s, flags | SEC_READONLY)
  3453. || !bfd_set_section_alignment (s, ptralign))
  3454. return false;
  3455. if (ehtab->sgot == NULL && !create_got_section (abfd, info))
  3456. return false;
  3457. for (sec = abfd->sections; sec; sec = sec->next)
  3458. {
  3459. secflags = bfd_section_flags (sec);
  3460. if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
  3461. || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
  3462. continue;
  3463. secname = bfd_section_name (sec);
  3464. relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
  3465. strcpy (relname, ".rela");
  3466. strcat (relname, secname);
  3467. if (bfd_get_section_by_name (abfd, secname))
  3468. continue;
  3469. s = bfd_make_section (abfd, relname);
  3470. if (s == NULL
  3471. || !bfd_set_section_flags (s, flags | SEC_READONLY)
  3472. || !bfd_set_section_alignment (s, ptralign))
  3473. return false;
  3474. }
  3475. if (bed->want_dynbss)
  3476. {
  3477. /* The .dynbss section is a place to put symbols which are defined
  3478. by dynamic objects, are referenced by regular objects, and are
  3479. not functions. We must allocate space for them in the process
  3480. image and use a R_*_COPY reloc to tell the dynamic linker to
  3481. initialize them at run time. The linker script puts the .dynbss
  3482. section into the .bss section of the final image. */
  3483. s = bfd_make_section (abfd, ".dynbss");
  3484. htab->root.sdynbss = s;
  3485. if (s == NULL
  3486. || !bfd_set_section_flags (s, SEC_ALLOC | SEC_LINKER_CREATED))
  3487. return false;
  3488. /* The .rel[a].bss section holds copy relocs. This section is not
  3489. normally needed. We need to create it here, though, so that the
  3490. linker will map it to an output section. We can't just create it
  3491. only if we need it, because we will not know whether we need it
  3492. until we have seen all the input files, and the first time the
  3493. main linker code calls BFD after examining all the input files
  3494. (size_dynamic_sections) the input sections have already been
  3495. mapped to the output sections. If the section turns out not to
  3496. be needed, we can discard it later. We will never need this
  3497. section when generating a shared object, since they do not use
  3498. copy relocs. */
  3499. if (!bfd_link_pic (info))
  3500. {
  3501. s = bfd_make_section (abfd, (bed->default_use_rela_p
  3502. ? ".rela.bss" : ".rel.bss"));
  3503. htab->root.srelbss = s;
  3504. if (s == NULL
  3505. || !bfd_set_section_flags (s, flags | SEC_READONLY)
  3506. || !bfd_set_section_alignment (s, ptralign))
  3507. return false;
  3508. }
  3509. }
  3510. return true;
  3511. }
  3512. /* Copy the extra info we tack onto an elf_link_hash_entry. */
  3513. static void
  3514. nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
  3515. struct elf_link_hash_entry *dir,
  3516. struct elf_link_hash_entry *ind)
  3517. {
  3518. struct elf_nds32_link_hash_entry *edir, *eind;
  3519. edir = (struct elf_nds32_link_hash_entry *) dir;
  3520. eind = (struct elf_nds32_link_hash_entry *) ind;
  3521. if (ind->root.type == bfd_link_hash_indirect)
  3522. {
  3523. if (dir->got.refcount <= 0)
  3524. {
  3525. edir->tls_type = eind->tls_type;
  3526. eind->tls_type = GOT_UNKNOWN;
  3527. }
  3528. }
  3529. _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  3530. }
  3531. /* Adjust a symbol defined by a dynamic object and referenced by a
  3532. regular object. The current definition is in some section of the
  3533. dynamic object, but we're not including those sections. We have to
  3534. change the definition to something the rest of the link can
  3535. understand. */
  3536. static bool
  3537. nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
  3538. struct elf_link_hash_entry *h)
  3539. {
  3540. struct elf_nds32_link_hash_table *htab;
  3541. bfd *dynobj;
  3542. asection *s;
  3543. unsigned int power_of_two;
  3544. dynobj = elf_hash_table (info)->dynobj;
  3545. /* Make sure we know what is going on here. */
  3546. BFD_ASSERT (dynobj != NULL
  3547. && (h->needs_plt
  3548. || h->is_weakalias
  3549. || (h->def_dynamic && h->ref_regular && !h->def_regular)));
  3550. /* If this is a function, put it in the procedure linkage table. We
  3551. will fill in the contents of the procedure linkage table later,
  3552. when we know the address of the .got section. */
  3553. if (h->type == STT_FUNC || h->needs_plt)
  3554. {
  3555. if (!bfd_link_pic (info)
  3556. && !h->def_dynamic
  3557. && !h->ref_dynamic
  3558. && h->root.type != bfd_link_hash_undefweak
  3559. && h->root.type != bfd_link_hash_undefined)
  3560. {
  3561. /* This case can occur if we saw a PLT reloc in an input
  3562. file, but the symbol was never referred to by a dynamic
  3563. object. In such a case, we don't actually need to build
  3564. a procedure linkage table, and we can just do a PCREL
  3565. reloc instead. */
  3566. h->plt.offset = (bfd_vma) - 1;
  3567. h->needs_plt = 0;
  3568. }
  3569. return true;
  3570. }
  3571. else
  3572. h->plt.offset = (bfd_vma) - 1;
  3573. /* If this is a weak symbol, and there is a real definition, the
  3574. processor independent code will have arranged for us to see the
  3575. real definition first, and we can just use the same value. */
  3576. if (h->is_weakalias)
  3577. {
  3578. struct elf_link_hash_entry *def = weakdef (h);
  3579. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  3580. h->root.u.def.section = def->root.u.def.section;
  3581. h->root.u.def.value = def->root.u.def.value;
  3582. return true;
  3583. }
  3584. /* This is a reference to a symbol defined by a dynamic object which
  3585. is not a function. */
  3586. /* If we are creating a shared library, we must presume that the
  3587. only references to the symbol are via the global offset table.
  3588. For such cases we need not do anything here; the relocations will
  3589. be handled correctly by relocate_section. */
  3590. if (bfd_link_pic (info))
  3591. return true;
  3592. /* If there are no references to this symbol that do not use the
  3593. GOT, we don't need to generate a copy reloc. */
  3594. if (!h->non_got_ref)
  3595. return true;
  3596. /* If -z nocopyreloc was given, we won't generate them either. */
  3597. if (0 && info->nocopyreloc)
  3598. {
  3599. h->non_got_ref = 0;
  3600. return true;
  3601. }
  3602. /* If we don't find any dynamic relocs in read-only sections, then
  3603. we'll be keeping the dynamic relocs and avoiding the copy reloc. */
  3604. if (!_bfd_elf_readonly_dynrelocs (h))
  3605. {
  3606. h->non_got_ref = 0;
  3607. return true;
  3608. }
  3609. /* We must allocate the symbol in our .dynbss section, which will
  3610. become part of the .bss section of the executable. There will be
  3611. an entry for this symbol in the .dynsym section. The dynamic
  3612. object will contain position independent code, so all references
  3613. from the dynamic object to this symbol will go through the global
  3614. offset table. The dynamic linker will use the .dynsym entry to
  3615. determine the address it must put in the global offset table, so
  3616. both the dynamic object and the regular object will refer to the
  3617. same memory location for the variable. */
  3618. htab = nds32_elf_hash_table (info);
  3619. s = htab->root.sdynbss;
  3620. BFD_ASSERT (s != NULL);
  3621. /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
  3622. to copy the initial value out of the dynamic object and into the
  3623. runtime process image. We need to remember the offset into the
  3624. .rela.bss section we are going to use. */
  3625. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
  3626. {
  3627. asection *srel;
  3628. srel = htab->root.srelbss;
  3629. BFD_ASSERT (srel != NULL);
  3630. srel->size += sizeof (Elf32_External_Rela);
  3631. h->needs_copy = 1;
  3632. }
  3633. /* We need to figure out the alignment required for this symbol. I
  3634. have no idea how ELF linkers handle this. */
  3635. power_of_two = bfd_log2 (h->size);
  3636. if (power_of_two > 3)
  3637. power_of_two = 3;
  3638. /* Apply the required alignment. */
  3639. s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
  3640. if (power_of_two > bfd_section_alignment (s))
  3641. {
  3642. if (!bfd_set_section_alignment (s, power_of_two))
  3643. return false;
  3644. }
  3645. /* Define the symbol as being at this point in the section. */
  3646. h->root.u.def.section = s;
  3647. h->root.u.def.value = s->size;
  3648. /* Increment the section size to make room for the symbol. */
  3649. s->size += h->size;
  3650. return true;
  3651. }
  3652. /* Allocate space in .plt, .got and associated reloc sections for
  3653. dynamic relocs. */
  3654. static bool
  3655. allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
  3656. {
  3657. struct bfd_link_info *info;
  3658. struct elf_link_hash_table *ehtab;
  3659. struct elf_nds32_link_hash_table *htab;
  3660. struct elf_dyn_relocs *p;
  3661. if (h->root.type == bfd_link_hash_indirect)
  3662. return true;
  3663. /* When warning symbols are created, they **replace** the "real"
  3664. entry in the hash table, thus we never get to see the real
  3665. symbol in a hash traversal. So look at it now. */
  3666. if (h->root.type == bfd_link_hash_warning)
  3667. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  3668. info = (struct bfd_link_info *) inf;
  3669. ehtab = elf_hash_table (info);
  3670. htab = nds32_elf_hash_table (info);
  3671. if (htab == NULL)
  3672. return false;
  3673. if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
  3674. && h->plt.refcount > 0
  3675. && !(bfd_link_pie (info) && h->def_regular))
  3676. {
  3677. /* Make sure this symbol is output as a dynamic symbol.
  3678. Undefined weak syms won't yet be marked as dynamic. */
  3679. if (h->dynindx == -1 && !h->forced_local)
  3680. {
  3681. if (!bfd_elf_link_record_dynamic_symbol (info, h))
  3682. return false;
  3683. }
  3684. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
  3685. {
  3686. asection *s = ehtab->splt;
  3687. /* If this is the first .plt entry, make room for the special
  3688. first entry. */
  3689. if (s->size == 0)
  3690. s->size += PLT_ENTRY_SIZE;
  3691. h->plt.offset = s->size;
  3692. /* If this symbol is not defined in a regular file, and we are
  3693. not generating a shared library, then set the symbol to this
  3694. location in the .plt. This is required to make function
  3695. pointers compare as equal between the normal executable and
  3696. the shared library. */
  3697. if (!bfd_link_pic (info) && !h->def_regular)
  3698. {
  3699. h->root.u.def.section = s;
  3700. h->root.u.def.value = h->plt.offset;
  3701. }
  3702. /* Make room for this entry. */
  3703. s->size += PLT_ENTRY_SIZE;
  3704. /* We also need to make an entry in the .got.plt section, which
  3705. will be placed in the .got section by the linker script. */
  3706. ehtab->sgotplt->size += 4;
  3707. /* We also need to make an entry in the .rel.plt section. */
  3708. ehtab->srelplt->size += sizeof (Elf32_External_Rela);
  3709. if (htab->tls_desc_trampoline)
  3710. htab->next_tls_desc_index++;
  3711. }
  3712. else
  3713. {
  3714. h->plt.offset = (bfd_vma) - 1;
  3715. h->needs_plt = 0;
  3716. }
  3717. }
  3718. else
  3719. {
  3720. h->plt.offset = (bfd_vma) - 1;
  3721. h->needs_plt = 0;
  3722. }
  3723. if (h->got.refcount > 0)
  3724. {
  3725. asection *sgot;
  3726. bool dyn;
  3727. int tls_type = elf32_nds32_hash_entry (h)->tls_type;
  3728. /* Make sure this symbol is output as a dynamic symbol.
  3729. Undefined weak syms won't yet be marked as dynamic. */
  3730. if (h->dynindx == -1 && !h->forced_local)
  3731. {
  3732. if (!bfd_elf_link_record_dynamic_symbol (info, h))
  3733. return false;
  3734. }
  3735. sgot = elf_hash_table (info)->sgot;
  3736. h->got.offset = sgot->size;
  3737. if (tls_type == GOT_UNKNOWN)
  3738. abort ();
  3739. /* Non-TLS symbols, and TLS_IE need one GOT slot. */
  3740. if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
  3741. sgot->size += 4;
  3742. else
  3743. {
  3744. /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots. */
  3745. if (tls_type & GOT_TLS_DESC)
  3746. sgot->size += 8;
  3747. }
  3748. dyn = htab->root.dynamic_sections_created;
  3749. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
  3750. {
  3751. if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
  3752. {
  3753. /* TLS_DESC with trampoline needs a relocation slot
  3754. within .rela.plt. */
  3755. htab->num_tls_desc++;
  3756. ehtab->srelplt->size += sizeof (Elf32_External_Rela);
  3757. htab->tls_trampoline = -1;
  3758. }
  3759. else
  3760. {
  3761. /* other relocations, including TLS_DESC without trampoline, need
  3762. a relocation slot within .rela.got. */
  3763. ehtab->srelgot->size += sizeof (Elf32_External_Rela);
  3764. }
  3765. }
  3766. }
  3767. else
  3768. h->got.offset = (bfd_vma)-1;
  3769. if (h->dyn_relocs == NULL)
  3770. return true;
  3771. /* In the shared -Bsymbolic case, discard space allocated for
  3772. dynamic pc-relative relocs against symbols which turn out to be
  3773. defined in regular objects. For the normal shared case, discard
  3774. space for pc-relative relocs that have become local due to symbol
  3775. visibility changes. */
  3776. if (bfd_link_pic (info))
  3777. {
  3778. if (h->def_regular && (h->forced_local || info->symbolic))
  3779. {
  3780. struct elf_dyn_relocs **pp;
  3781. for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
  3782. {
  3783. p->count -= p->pc_count;
  3784. p->pc_count = 0;
  3785. if (p->count == 0)
  3786. *pp = p->next;
  3787. else
  3788. pp = &p->next;
  3789. }
  3790. }
  3791. }
  3792. else
  3793. {
  3794. /* For the non-shared case, discard space for relocs against
  3795. symbols which turn out to need copy relocs or are not dynamic. */
  3796. if (!h->non_got_ref
  3797. && ((h->def_dynamic
  3798. && !h->def_regular)
  3799. || (htab->root.dynamic_sections_created
  3800. && (h->root.type == bfd_link_hash_undefweak
  3801. || h->root.type == bfd_link_hash_undefined))))
  3802. {
  3803. /* Make sure this symbol is output as a dynamic symbol.
  3804. Undefined weak syms won't yet be marked as dynamic. */
  3805. if (h->dynindx == -1 && !h->forced_local)
  3806. {
  3807. if (!bfd_elf_link_record_dynamic_symbol (info, h))
  3808. return false;
  3809. }
  3810. /* If that succeeded, we know we'll be keeping all the
  3811. relocs. */
  3812. if (h->dynindx != -1)
  3813. goto keep;
  3814. }
  3815. h->dyn_relocs = NULL;
  3816. keep:;
  3817. }
  3818. /* Finally, allocate space. */
  3819. for (p = h->dyn_relocs; p != NULL; p = p->next)
  3820. {
  3821. asection *sreloc = elf_section_data (p->sec)->sreloc;
  3822. sreloc->size += p->count * sizeof (Elf32_External_Rela);
  3823. }
  3824. return true;
  3825. }
  3826. /* Add relocation REL to the end of relocation section SRELOC. */
  3827. static void
  3828. elf32_nds32_add_dynreloc (bfd *output_bfd,
  3829. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  3830. asection *sreloc, Elf_Internal_Rela *rel)
  3831. {
  3832. bfd_byte *loc;
  3833. if (sreloc == NULL)
  3834. abort ();
  3835. loc = sreloc->contents;
  3836. loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
  3837. if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
  3838. abort ();
  3839. bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
  3840. }
  3841. /* Set the sizes of the dynamic sections. */
  3842. static bool
  3843. nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  3844. struct bfd_link_info *info)
  3845. {
  3846. struct elf_nds32_link_hash_table *htab;
  3847. bfd *dynobj;
  3848. asection *s;
  3849. bool relocs;
  3850. bfd *ibfd;
  3851. htab = nds32_elf_hash_table (info);
  3852. if (htab == NULL)
  3853. return false;
  3854. dynobj = elf_hash_table (info)->dynobj;
  3855. BFD_ASSERT (dynobj != NULL);
  3856. if (elf_hash_table (info)->dynamic_sections_created)
  3857. {
  3858. /* Set the contents of the .interp section to the interpreter. */
  3859. if (bfd_link_executable (info) && !info->nointerp)
  3860. {
  3861. s = bfd_get_section_by_name (dynobj, ".interp");
  3862. BFD_ASSERT (s != NULL);
  3863. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  3864. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  3865. }
  3866. }
  3867. /* Set up .got offsets for local syms, and space for local dynamic
  3868. relocs. */
  3869. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  3870. {
  3871. bfd_signed_vma *local_got;
  3872. bfd_signed_vma *end_local_got;
  3873. bfd_size_type locsymcount;
  3874. Elf_Internal_Shdr *symtab_hdr;
  3875. asection *sgot;
  3876. char *local_tls_type;
  3877. unsigned long symndx;
  3878. bfd_vma *local_tlsdesc_gotent;
  3879. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
  3880. continue;
  3881. for (s = ibfd->sections; s != NULL; s = s->next)
  3882. {
  3883. struct elf_dyn_relocs *p;
  3884. for (p = ((struct elf_dyn_relocs *)
  3885. elf_section_data (s)->local_dynrel);
  3886. p != NULL; p = p->next)
  3887. {
  3888. if (!bfd_is_abs_section (p->sec)
  3889. && bfd_is_abs_section (p->sec->output_section))
  3890. {
  3891. /* Input section has been discarded, either because
  3892. it is a copy of a linkonce section or due to
  3893. linker script /DISCARD/, so we'll be discarding
  3894. the relocs too. */
  3895. }
  3896. else if (p->count != 0)
  3897. {
  3898. asection *sreloc = elf_section_data (p->sec)->sreloc;
  3899. sreloc->size += p->count * sizeof (Elf32_External_Rela);
  3900. if ((p->sec->output_section->flags & SEC_READONLY) != 0)
  3901. info->flags |= DF_TEXTREL;
  3902. }
  3903. }
  3904. }
  3905. local_got = elf_local_got_refcounts (ibfd);
  3906. if (!local_got)
  3907. continue;
  3908. symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
  3909. locsymcount = symtab_hdr->sh_info;
  3910. end_local_got = local_got + locsymcount;
  3911. sgot = elf_hash_table (info)->sgot;
  3912. local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
  3913. local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
  3914. for (symndx = 0; local_got < end_local_got;
  3915. ++local_got, ++local_tls_type, ++local_tlsdesc_gotent, ++symndx)
  3916. {
  3917. if (*local_got > 0)
  3918. {
  3919. int num_of_got_entry_needed = 0;
  3920. *local_got = sgot->size;
  3921. *local_tlsdesc_gotent = sgot->size;
  3922. /* TLS_NORMAL, and TLS_IE need one slot in .got. */
  3923. if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
  3924. num_of_got_entry_needed = 1;
  3925. /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT. */
  3926. else if (*local_tls_type & GOT_TLS_DESC)
  3927. num_of_got_entry_needed = 2;
  3928. sgot->size += (num_of_got_entry_needed << 2);
  3929. /* non-relax-able TLS_DESCs need a slot in .rela.plt.
  3930. others need a slot in .rela.got. */
  3931. if (*local_tls_type == GOT_TLS_DESC)
  3932. {
  3933. if (bfd_link_pic (info))
  3934. {
  3935. if (htab->tls_desc_trampoline)
  3936. {
  3937. htab->num_tls_desc++;
  3938. htab->root.srelplt->size += sizeof (Elf32_External_Rela);
  3939. htab->tls_trampoline = -1;
  3940. }
  3941. else
  3942. htab->root.srelgot->size += sizeof (Elf32_External_Rela);
  3943. }
  3944. else
  3945. {
  3946. /* TLS_DESC -> TLS_LE */
  3947. }
  3948. }
  3949. else
  3950. {
  3951. htab->root.srelgot->size += sizeof (Elf32_External_Rela);
  3952. }
  3953. }
  3954. else
  3955. {
  3956. *local_got = (bfd_vma) -1;
  3957. *local_tlsdesc_gotent = (bfd_vma) -1;
  3958. }
  3959. }
  3960. }
  3961. /* Allocate global sym .plt and .got entries, and space for global
  3962. sym dynamic relocs. */
  3963. elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
  3964. /* For every jump slot reserved in the sgotplt, reloc_count is
  3965. incremented. However, when we reserve space for TLS descriptors,
  3966. it's not incremented, so in order to compute the space reserved
  3967. for them, it suffices to multiply the reloc count by the jump
  3968. slot size. */
  3969. if (htab->tls_desc_trampoline && htab->root.srelplt)
  3970. htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
  3971. if (htab->tls_trampoline)
  3972. {
  3973. htab->tls_trampoline = htab->root.splt->size;
  3974. /* If we're not using lazy TLS relocations, don't generate the
  3975. PLT and GOT entries they require. */
  3976. if ((info->flags & DF_BIND_NOW))
  3977. htab->root.tlsdesc_plt = 0;
  3978. else
  3979. {
  3980. htab->root.tlsdesc_got = htab->root.sgot->size;
  3981. htab->root.sgot->size += 4;
  3982. htab->root.tlsdesc_plt = htab->root.splt->size;
  3983. htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
  3984. }
  3985. }
  3986. /* We now have determined the sizes of the various dynamic sections.
  3987. Allocate memory for them. */
  3988. /* The check_relocs and adjust_dynamic_symbol entry points have
  3989. determined the sizes of the various dynamic sections. Allocate
  3990. memory for them. */
  3991. relocs = false;
  3992. for (s = dynobj->sections; s != NULL; s = s->next)
  3993. {
  3994. if ((s->flags & SEC_LINKER_CREATED) == 0)
  3995. continue;
  3996. if (s == htab->root.splt)
  3997. {
  3998. /* Strip this section if we don't need it; see the
  3999. comment below. */
  4000. ;
  4001. }
  4002. else if (s == elf_hash_table (info)->sgot)
  4003. {
  4004. got_size += s->size;
  4005. }
  4006. else if (s == elf_hash_table (info)->sgotplt)
  4007. {
  4008. got_size += s->size;
  4009. }
  4010. else if (startswith (bfd_section_name (s), ".rela"))
  4011. {
  4012. if (s->size != 0 && s != elf_hash_table (info)->srelplt)
  4013. relocs = true;
  4014. /* We use the reloc_count field as a counter if we need
  4015. to copy relocs into the output file. */
  4016. s->reloc_count = 0;
  4017. }
  4018. else
  4019. {
  4020. /* It's not one of our sections, so don't allocate space. */
  4021. continue;
  4022. }
  4023. if (s->size == 0)
  4024. {
  4025. /* If we don't need this section, strip it from the
  4026. output file. This is mostly to handle .rela.bss and
  4027. .rela.plt. We must create both sections in
  4028. create_dynamic_sections, because they must be created
  4029. before the linker maps input sections to output
  4030. sections. The linker does that before
  4031. adjust_dynamic_symbol is called, and it is that
  4032. function which decides whether anything needs to go
  4033. into these sections. */
  4034. s->flags |= SEC_EXCLUDE;
  4035. continue;
  4036. }
  4037. /* Allocate memory for the section contents. We use bfd_zalloc
  4038. here in case unused entries are not reclaimed before the
  4039. section's contents are written out. This should not happen,
  4040. but this way if it does, we get a R_NDS32_NONE reloc instead
  4041. of garbage. */
  4042. s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
  4043. if (s->contents == NULL)
  4044. return false;
  4045. }
  4046. return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
  4047. }
  4048. static bfd_reloc_status_type
  4049. nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
  4050. bfd_vma relocation, bfd_byte *location)
  4051. {
  4052. int size;
  4053. bfd_vma x = 0;
  4054. bfd_reloc_status_type flag;
  4055. unsigned int rightshift = howto->rightshift;
  4056. unsigned int bitpos = howto->bitpos;
  4057. if (howto->negate)
  4058. relocation = -relocation;
  4059. /* Get the value we are going to relocate. */
  4060. size = bfd_get_reloc_size (howto);
  4061. switch (size)
  4062. {
  4063. default:
  4064. abort ();
  4065. break;
  4066. case 0:
  4067. return bfd_reloc_ok;
  4068. case 2:
  4069. x = bfd_getb16 (location);
  4070. break;
  4071. case 4:
  4072. x = bfd_getb32 (location);
  4073. break;
  4074. }
  4075. /* Check for overflow. FIXME: We may drop bits during the addition
  4076. which we don't check for. We must either check at every single
  4077. operation, which would be tedious, or we must do the computations
  4078. in a type larger than bfd_vma, which would be inefficient. */
  4079. flag = bfd_reloc_ok;
  4080. if (howto->complain_on_overflow != complain_overflow_dont)
  4081. {
  4082. bfd_vma addrmask, fieldmask, signmask, ss;
  4083. bfd_vma a, b, sum;
  4084. /* Get the values to be added together. For signed and unsigned
  4085. relocations, we assume that all values should be truncated to
  4086. the size of an address. For bitfields, all the bits matter.
  4087. See also bfd_check_overflow. */
  4088. fieldmask = N_ONES (howto->bitsize);
  4089. signmask = ~fieldmask;
  4090. addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
  4091. a = (relocation & addrmask) >> rightshift;
  4092. b = (x & howto->src_mask & addrmask) >> bitpos;
  4093. switch (howto->complain_on_overflow)
  4094. {
  4095. case complain_overflow_signed:
  4096. /* If any sign bits are set, all sign bits must be set.
  4097. That is, A must be a valid negative address after
  4098. shifting. */
  4099. signmask = ~(fieldmask >> 1);
  4100. /* Fall through. */
  4101. case complain_overflow_bitfield:
  4102. /* Much like the signed check, but for a field one bit
  4103. wider. We allow a bitfield to represent numbers in the
  4104. range -2**n to 2**n-1, where n is the number of bits in the
  4105. field. Note that when bfd_vma is 32 bits, a 32-bit reloc
  4106. can't overflow, which is exactly what we want. */
  4107. ss = a & signmask;
  4108. if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
  4109. flag = bfd_reloc_overflow;
  4110. /* We only need this next bit of code if the sign bit of B
  4111. is below the sign bit of A. This would only happen if
  4112. SRC_MASK had fewer bits than BITSIZE. Note that if
  4113. SRC_MASK has more bits than BITSIZE, we can get into
  4114. trouble; we would need to verify that B is in range, as
  4115. we do for A above. */
  4116. ss = ((~howto->src_mask) >> 1) & howto->src_mask;
  4117. ss >>= bitpos;
  4118. /* Set all the bits above the sign bit. */
  4119. b = (b ^ ss) - ss;
  4120. /* Now we can do the addition. */
  4121. sum = a + b;
  4122. /* See if the result has the correct sign. Bits above the
  4123. sign bit are junk now; ignore them. If the sum is
  4124. positive, make sure we did not have all negative inputs;
  4125. if the sum is negative, make sure we did not have all
  4126. positive inputs. The test below looks only at the sign
  4127. bits, and it really just
  4128. SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
  4129. We mask with addrmask here to explicitly allow an address
  4130. wrap-around. The Linux kernel relies on it, and it is
  4131. the only way to write assembler code which can run when
  4132. loaded at a location 0x80000000 away from the location at
  4133. which it is linked. */
  4134. if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
  4135. flag = bfd_reloc_overflow;
  4136. break;
  4137. case complain_overflow_unsigned:
  4138. /* Checking for an unsigned overflow is relatively easy:
  4139. trim the addresses and add, and trim the result as well.
  4140. Overflow is normally indicated when the result does not
  4141. fit in the field. However, we also need to consider the
  4142. case when, e.g., fieldmask is 0x7fffffff or smaller, an
  4143. input is 0x80000000, and bfd_vma is only 32 bits; then we
  4144. will get sum == 0, but there is an overflow, since the
  4145. inputs did not fit in the field. Instead of doing a
  4146. separate test, we can check for this by or-ing in the
  4147. operands when testing for the sum overflowing its final
  4148. field. */
  4149. sum = (a + b) & addrmask;
  4150. if ((a | b | sum) & signmask)
  4151. flag = bfd_reloc_overflow;
  4152. break;
  4153. default:
  4154. abort ();
  4155. }
  4156. }
  4157. /* Put RELOCATION in the right bits. */
  4158. relocation >>= (bfd_vma) rightshift;
  4159. relocation <<= (bfd_vma) bitpos;
  4160. /* Add RELOCATION to the right bits of X. */
  4161. /* FIXME : 090616
  4162. Because the relaxation may generate duplicate relocation at one address,
  4163. an addition to immediate in the instruction may cause the relocation added
  4164. several times.
  4165. This bug should be fixed in assembler, but a check is also needed here. */
  4166. if (howto->partial_inplace)
  4167. x = ((x & ~howto->dst_mask)
  4168. | (((x & howto->src_mask) + relocation) & howto->dst_mask));
  4169. else
  4170. x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
  4171. /* Put the relocated value back in the object file. */
  4172. switch (size)
  4173. {
  4174. default:
  4175. case 0:
  4176. case 1:
  4177. case 8:
  4178. abort ();
  4179. break;
  4180. case 2:
  4181. bfd_putb16 (x, location);
  4182. break;
  4183. case 4:
  4184. bfd_putb32 (x, location);
  4185. break;
  4186. }
  4187. return flag;
  4188. }
  4189. static bfd_reloc_status_type
  4190. nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
  4191. asection *input_section, bfd_byte *contents,
  4192. bfd_vma address, bfd_vma value, bfd_vma addend)
  4193. {
  4194. bfd_vma relocation;
  4195. /* Sanity check the address. */
  4196. if (address > bfd_get_section_limit (input_bfd, input_section))
  4197. return bfd_reloc_outofrange;
  4198. /* This function assumes that we are dealing with a basic relocation
  4199. against a symbol. We want to compute the value of the symbol to
  4200. relocate to. This is just VALUE, the value of the symbol, plus
  4201. ADDEND, any addend associated with the reloc. */
  4202. relocation = value + addend;
  4203. /* If the relocation is PC relative, we want to set RELOCATION to
  4204. the distance between the symbol (currently in RELOCATION) and the
  4205. location we are relocating. If pcrel_offset is FALSE we do not
  4206. need to subtract out the offset of the location within the
  4207. section (which is just ADDRESS). */
  4208. if (howto->pc_relative)
  4209. {
  4210. relocation -= (input_section->output_section->vma
  4211. + input_section->output_offset);
  4212. if (howto->pcrel_offset)
  4213. relocation -= address;
  4214. }
  4215. return nds32_relocate_contents (howto, input_bfd, relocation,
  4216. contents + address);
  4217. }
  4218. static int
  4219. nds32_elf_output_symbol_hook (struct bfd_link_info *info,
  4220. const char *name,
  4221. Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
  4222. asection *input_sec,
  4223. struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
  4224. {
  4225. const char *source;
  4226. FILE *sym_ld_script = NULL;
  4227. struct elf_nds32_link_hash_table *table;
  4228. table = nds32_elf_hash_table (info);
  4229. sym_ld_script = table->sym_ld_script;
  4230. if (!sym_ld_script)
  4231. return true;
  4232. if (!h || !name || *name == '\0')
  4233. return true;
  4234. if (input_sec->flags & SEC_EXCLUDE)
  4235. return true;
  4236. if (!check_start_export_sym)
  4237. {
  4238. fprintf (sym_ld_script, "SECTIONS\n{\n");
  4239. check_start_export_sym = 1;
  4240. }
  4241. if (h->root.type == bfd_link_hash_defined
  4242. || h->root.type == bfd_link_hash_defweak)
  4243. {
  4244. if (!h->root.u.def.section->output_section)
  4245. return true;
  4246. if (bfd_is_const_section (input_sec))
  4247. source = input_sec->name;
  4248. else
  4249. source = bfd_get_filename (input_sec->owner);
  4250. fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
  4251. h->root.root.string,
  4252. (long) (h->root.u.def.value
  4253. + h->root.u.def.section->output_section->vma
  4254. + h->root.u.def.section->output_offset), source);
  4255. }
  4256. return true;
  4257. }
  4258. /* Relocate an NDS32/D ELF section.
  4259. There is some attempt to make this function usable for many architectures,
  4260. both for RELA and REL type relocs, if only to serve as a learning tool.
  4261. The RELOCATE_SECTION function is called by the new ELF backend linker
  4262. to handle the relocations for a section.
  4263. The relocs are always passed as Rela structures; if the section
  4264. actually uses Rel structures, the r_addend field will always be
  4265. zero.
  4266. This function is responsible for adjust the section contents as
  4267. necessary, and (if using Rela relocs and generating a
  4268. relocatable output file) adjusting the reloc addend as
  4269. necessary.
  4270. This function does not have to worry about setting the reloc
  4271. address or the reloc symbol index.
  4272. LOCAL_SYMS is a pointer to the swapped in local symbols.
  4273. LOCAL_SECTIONS is an array giving the section in the input file
  4274. corresponding to the st_shndx field of each local symbol.
  4275. The global hash table entry for the global symbols can be found
  4276. via elf_sym_hashes (input_bfd).
  4277. When generating relocatable output, this function must handle
  4278. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  4279. going to be the section symbol corresponding to the output
  4280. section, which means that the addend must be adjusted
  4281. accordingly. */
  4282. /* Return the base VMA address which should be subtracted from real addresses
  4283. when resolving @dtpoff relocation.
  4284. This is PT_TLS segment p_vaddr. */
  4285. /* Return the relocation value for @tpoff relocation
  4286. if STT_TLS virtual address is ADDRESS. */
  4287. /* Return the relocation value for @gottpoff relocation
  4288. if STT_TLS virtual address is ADDRESS. */
  4289. static bfd_vma
  4290. gottpoff (struct bfd_link_info *info, bfd_vma address)
  4291. {
  4292. bfd_vma tp_base;
  4293. bfd_vma tp_offset;
  4294. /* If tls_sec is NULL, we should have signalled an error already. */
  4295. if (elf_hash_table (info)->tls_sec == NULL)
  4296. return 0;
  4297. tp_base = elf_hash_table (info)->tls_sec->vma;
  4298. tp_offset = address - tp_base;
  4299. return tp_offset;
  4300. }
  4301. static bool
  4302. patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
  4303. {
  4304. /* TLS_GD/TLS_LD model #1
  4305. 46 00 00 00 sethi $r0,#0x0
  4306. 58 00 00 00 ori $r0,$r0,#0x0
  4307. 40 00 74 00 add $r0,$r0,$gp
  4308. 04 10 00 00 lwi $r1,[$r0+#0x0]
  4309. 4b e0 04 01 jral $lp,$r1 */
  4310. /* TLS_GD/TLS_LD model #2
  4311. 46 00 00 00 sethi $r0,#0x0
  4312. 58 00 00 00 ori $r0,$r0,#0x0
  4313. 38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
  4314. 40 00 74 00 add $r0,$r0,$gp
  4315. 4b e0 04 01 jral $lp,$r1 */
  4316. /* TLS_IE model (non-PIC)
  4317. 46 00 00 00 sethi $r0,#0x0
  4318. 04 00 00 00 lwi $r0,[$r0+#0x0]
  4319. 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
  4320. /* TLS_IE model (PIC)
  4321. 46 00 00 00 sethi $r0,#0x0
  4322. 58 00 00 00 ori $r0,$r0,#0x0
  4323. 38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
  4324. 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
  4325. /* TLS_GD_TO_IE model
  4326. 46 00 00 00 sethi $r0,#0x0
  4327. 58 00 00 00 ori $r0,$r0,#0x0
  4328. 40 00 74 00 add $r0,$rM,$gp
  4329. 04 00 00 01 lwi $r0,[$r0+#0x4]
  4330. 40 00 64 00 add $r0,$r0,$r25 */
  4331. bool rz = false;
  4332. typedef struct
  4333. {
  4334. uint32_t opcode;
  4335. uint32_t mask;
  4336. } pat_t;
  4337. uint32_t patch[3] =
  4338. {
  4339. 0x40007400, /* add $r0,$rM,$gp */
  4340. 0x04000001, /* lwi $r0,[$r0+#0x4] */
  4341. 0x40006400, /* add $r0,$r0,$r25 */
  4342. };
  4343. pat_t mode0[3] =
  4344. {
  4345. { 0x40000000, 0xfe0003ff },
  4346. { 0x04000000, 0xfe000000 },
  4347. { 0x4be00001, 0xffff83ff },
  4348. };
  4349. pat_t mode1[3] =
  4350. {
  4351. { 0x38007402, 0xfe007fff },
  4352. { 0x40007400, 0xfe007fff },
  4353. { 0x4be00001, 0xffff83ff },
  4354. };
  4355. unsigned char *p = contents + rel->r_offset;
  4356. uint32_t insn;
  4357. uint32_t regidx = 0;
  4358. insn = bfd_getb32 (p);
  4359. if (INSN_SETHI == (0xfe0fffffu & insn))
  4360. {
  4361. regidx = 0x1f & (insn >> 20);
  4362. p += 4;
  4363. }
  4364. insn = bfd_getb32 (p);
  4365. if (INSN_ORI == (0xfe007fffu & insn))
  4366. {
  4367. regidx = 0x1f & (insn >> 20);
  4368. p += 4;
  4369. }
  4370. if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction. */
  4371. {
  4372. /* already patched? */
  4373. if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
  4374. (patch[1] == bfd_getb32 (p + 4)))
  4375. rz = true;
  4376. }
  4377. else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
  4378. {
  4379. if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
  4380. (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
  4381. {
  4382. bfd_putb32 (patch[0] | (regidx << 15), p + 0);
  4383. bfd_putb32 (patch[1], p + 4);
  4384. bfd_putb32 (patch[2], p + 8);
  4385. rz = true;
  4386. }
  4387. }
  4388. else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
  4389. {
  4390. if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
  4391. (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
  4392. {
  4393. bfd_putb32 (patch[0] | (regidx << 15), p + 0);
  4394. bfd_putb32 (patch[1], p + 4);
  4395. bfd_putb32 (patch[2], p + 8);
  4396. rz = true;
  4397. }
  4398. }
  4399. if (!rz)
  4400. {
  4401. printf ("%s: %s @ 0x%08x\n", __func__, bfd_get_filename (ibfd),
  4402. (int) rel->r_offset);
  4403. BFD_ASSERT(0); /* Unsupported pattern. */
  4404. }
  4405. return rz;
  4406. }
  4407. static enum elf_nds32_tls_type
  4408. get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
  4409. static unsigned int
  4410. ones32 (register unsigned int x)
  4411. {
  4412. /* 32-bit recursive reduction using SWAR...
  4413. but first step is mapping 2-bit values
  4414. into sum of 2 1-bit values in sneaky way. */
  4415. x -= ((x >> 1) & 0x55555555);
  4416. x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
  4417. x = (((x >> 4) + x) & 0x0f0f0f0f);
  4418. x += (x >> 8);
  4419. x += (x >> 16);
  4420. return (x & 0x0000003f);
  4421. }
  4422. #if !HAVE_FLS
  4423. static unsigned int
  4424. fls (register unsigned int x)
  4425. {
  4426. return ffs (x & (-x));
  4427. }
  4428. #endif /* !HAVE_FLS */
  4429. #define nds32_elf_local_tlsdesc_gotent(bfd) \
  4430. (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
  4431. static int
  4432. nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
  4433. struct bfd_link_info * info,
  4434. bfd * input_bfd,
  4435. asection * input_section,
  4436. bfd_byte * contents,
  4437. Elf_Internal_Rela * relocs,
  4438. Elf_Internal_Sym * local_syms,
  4439. asection ** local_sections)
  4440. {
  4441. Elf_Internal_Shdr *symtab_hdr;
  4442. struct elf_link_hash_entry **sym_hashes;
  4443. Elf_Internal_Rela *rel, *relend;
  4444. bool ret = true; /* Assume success. */
  4445. int align = 0;
  4446. bfd_reloc_status_type r;
  4447. const char *errmsg = NULL;
  4448. bfd_vma gp;
  4449. struct elf_link_hash_table *ehtab;
  4450. struct elf_nds32_link_hash_table *htab;
  4451. bfd *dynobj;
  4452. bfd_vma *local_got_offsets;
  4453. asection *sgot, *splt, *sreloc;
  4454. bfd_vma high_address;
  4455. struct elf_nds32_link_hash_table *table;
  4456. int eliminate_gc_relocs;
  4457. bfd_vma fpbase_addr;
  4458. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  4459. sym_hashes = elf_sym_hashes (input_bfd);
  4460. ehtab = elf_hash_table (info);
  4461. htab = nds32_elf_hash_table (info);
  4462. high_address = bfd_get_section_limit (input_bfd, input_section);
  4463. dynobj = htab->root.dynobj;
  4464. local_got_offsets = elf_local_got_offsets (input_bfd);
  4465. sgot = ehtab->sgot;
  4466. splt = ehtab->splt;
  4467. sreloc = NULL;
  4468. rel = relocs;
  4469. relend = relocs + input_section->reloc_count;
  4470. table = nds32_elf_hash_table (info);
  4471. eliminate_gc_relocs = table->eliminate_gc_relocs;
  4472. /* By this time, we can adjust the value of _SDA_BASE_. */
  4473. /* Explain _SDA_BASE_ */
  4474. if ((!bfd_link_relocatable (info)))
  4475. {
  4476. is_SDA_BASE_set = 1;
  4477. r = nds32_elf_final_sda_base (output_bfd, info, &gp, true);
  4478. if (r != bfd_reloc_ok)
  4479. return false;
  4480. }
  4481. /* Do TLS model conversion once at first. */
  4482. nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
  4483. /* Use gp as fp to prevent truncated fit. Because in relaxation time
  4484. the fp value is set as gp, and it has be reverted for instruction
  4485. setting fp. */
  4486. fpbase_addr = elf_gp (output_bfd);
  4487. /* Deal with (dynamic) relocations. */
  4488. for (rel = relocs; rel < relend; rel++)
  4489. {
  4490. enum elf_nds32_reloc_type r_type;
  4491. reloc_howto_type *howto = NULL;
  4492. unsigned long r_symndx;
  4493. struct elf_link_hash_entry *h = NULL;
  4494. Elf_Internal_Sym *sym = NULL;
  4495. asection *sec;
  4496. bfd_vma relocation;
  4497. bfd_vma relocation_sym = 0xdeadbeef;
  4498. Elf_Internal_Rela *lorel;
  4499. bfd_vma off;
  4500. /* We can't modify r_addend here as elf_link_input_bfd has an assert to
  4501. ensure it's zero (we use REL relocs, not RELA). Therefore this
  4502. should be assigning zero to `addend', but for clarity we use
  4503. `r_addend'. */
  4504. bfd_vma addend = rel->r_addend;
  4505. bfd_vma offset = rel->r_offset;
  4506. r_type = ELF32_R_TYPE (rel->r_info);
  4507. if (r_type >= R_NDS32_max)
  4508. {
  4509. /* xgettext:c-format */
  4510. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  4511. input_bfd, r_type);
  4512. bfd_set_error (bfd_error_bad_value);
  4513. ret = false;
  4514. continue;
  4515. }
  4516. if (r_type == R_NDS32_GNU_VTENTRY
  4517. || r_type == R_NDS32_GNU_VTINHERIT
  4518. || r_type == R_NDS32_NONE
  4519. || r_type == R_NDS32_RELA_GNU_VTENTRY
  4520. || r_type == R_NDS32_RELA_GNU_VTINHERIT
  4521. || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
  4522. || r_type == R_NDS32_DATA
  4523. || r_type == R_NDS32_TRAN)
  4524. continue;
  4525. /* If we enter the fp-as-gp region. Resolve the address
  4526. of best fp-base. */
  4527. if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
  4528. && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
  4529. {
  4530. int dist;
  4531. /* Distance to relocation of best fp-base is encoded in R_SYM. */
  4532. dist = rel->r_addend >> 16;
  4533. fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
  4534. local_syms, symtab_hdr);
  4535. }
  4536. else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
  4537. && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
  4538. {
  4539. fpbase_addr = elf_gp (output_bfd);
  4540. }
  4541. /* Skip the relocations used for relaxation. */
  4542. /* We have to update LONGCALL and LONGJUMP
  4543. relocations when generating the relocatable files. */
  4544. if (!bfd_link_relocatable (info)
  4545. && (r_type >= R_NDS32_RELAX_ENTRY
  4546. || (r_type >= R_NDS32_LONGCALL4
  4547. && r_type <= R_NDS32_LONGJUMP7)))
  4548. continue;
  4549. howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
  4550. r_symndx = ELF32_R_SYM (rel->r_info);
  4551. /* This is a final link. */
  4552. sym = NULL;
  4553. sec = NULL;
  4554. h = NULL;
  4555. if (r_symndx < symtab_hdr->sh_info)
  4556. {
  4557. /* Local symbol. */
  4558. sym = local_syms + r_symndx;
  4559. sec = local_sections[r_symndx];
  4560. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  4561. addend = rel->r_addend;
  4562. /* keep symbol location for static TLS_IE GOT entry */
  4563. relocation_sym = relocation;
  4564. if (bfd_link_relocatable (info))
  4565. {
  4566. /* This is a relocatable link. We don't have to change
  4567. anything, unless the reloc is against a section symbol,
  4568. in which case we have to adjust according to where the
  4569. section symbol winds up in the output section. */
  4570. if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  4571. rel->r_addend += sec->output_offset + sym->st_value;
  4572. continue;
  4573. }
  4574. }
  4575. else
  4576. {
  4577. /* External symbol. */
  4578. if (bfd_link_relocatable (info))
  4579. continue;
  4580. bool warned, ignored, unresolved_reloc;
  4581. int symndx = r_symndx - symtab_hdr->sh_info;
  4582. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  4583. r_symndx, symtab_hdr, sym_hashes, h, sec,
  4584. relocation, unresolved_reloc, warned,
  4585. ignored);
  4586. /* keep symbol location for static TLS_IE GOT entry */
  4587. relocation_sym = relocation;
  4588. /* la $fp, _FP_BASE_ is per-function (region).
  4589. Handle it specially. */
  4590. switch ((int) r_type)
  4591. {
  4592. case R_NDS32_HI20_RELA:
  4593. case R_NDS32_LO12S0_RELA:
  4594. if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
  4595. FP_BASE_NAME) == 0)
  4596. {
  4597. if (!bfd_link_pie (info))
  4598. {
  4599. _bfd_error_handler
  4600. ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
  4601. input_bfd);
  4602. }
  4603. relocation = fpbase_addr;
  4604. }
  4605. break;
  4606. case R_NDS32_SDA19S0_RELA:
  4607. case R_NDS32_SDA15S0_RELA:
  4608. case R_NDS32_20_RELA:
  4609. if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
  4610. FP_BASE_NAME) == 0)
  4611. {
  4612. relocation = fpbase_addr;
  4613. break;
  4614. }
  4615. }
  4616. }
  4617. /* Sanity check the address. */
  4618. if (offset > high_address)
  4619. {
  4620. r = bfd_reloc_outofrange;
  4621. goto check_reloc;
  4622. }
  4623. if (r_type >= R_NDS32_RELAX_ENTRY)
  4624. continue;
  4625. switch ((int) r_type)
  4626. {
  4627. case R_NDS32_GOTOFF:
  4628. /* Relocation is relative to the start of the global offset
  4629. table (for ld24 rx, #uimm24), e.g. access at label+addend
  4630. ld24 rx. #label@GOTOFF + addend
  4631. sub rx, r12. */
  4632. case R_NDS32_GOTOFF_HI20:
  4633. case R_NDS32_GOTOFF_LO12:
  4634. case R_NDS32_GOTOFF_LO15:
  4635. case R_NDS32_GOTOFF_LO19:
  4636. BFD_ASSERT (sgot != NULL);
  4637. relocation -= elf_gp (output_bfd);
  4638. break;
  4639. case R_NDS32_9_PLTREL:
  4640. case R_NDS32_25_PLTREL:
  4641. /* Relocation is to the entry for this symbol in the
  4642. procedure linkage table. */
  4643. /* The native assembler will generate a 25_PLTREL reloc
  4644. for a local symbol if you assemble a call from one
  4645. section to another when using -K pic. */
  4646. if (h == NULL)
  4647. break;
  4648. if (h->forced_local)
  4649. break;
  4650. /* We didn't make a PLT entry for this symbol. This
  4651. happens when statically linking PIC code, or when
  4652. using -Bsymbolic. */
  4653. if (h->plt.offset == (bfd_vma) - 1)
  4654. break;
  4655. relocation = (splt->output_section->vma
  4656. + splt->output_offset + h->plt.offset);
  4657. break;
  4658. case R_NDS32_PLT_GOTREL_HI20:
  4659. case R_NDS32_PLT_GOTREL_LO12:
  4660. case R_NDS32_PLT_GOTREL_LO15:
  4661. case R_NDS32_PLT_GOTREL_LO19:
  4662. case R_NDS32_PLT_GOTREL_LO20:
  4663. if (h == NULL
  4664. || h->forced_local
  4665. || h->plt.offset == (bfd_vma) -1
  4666. || (bfd_link_pie (info) && h->def_regular))
  4667. {
  4668. /* Maybe we should find better checking to optimize
  4669. PIE PLT relocations. */
  4670. /* We didn't make a PLT entry for this symbol. This
  4671. happens when statically linking PIC code, or when
  4672. using -Bsymbolic. */
  4673. if (h)
  4674. h->plt.offset = (bfd_vma) -1; /* Cancel PLT trampoline. */
  4675. relocation -= elf_gp (output_bfd);
  4676. break;
  4677. }
  4678. relocation = (splt->output_section->vma
  4679. + splt->output_offset + h->plt.offset);
  4680. relocation -= elf_gp (output_bfd);
  4681. break;
  4682. case R_NDS32_PLTREL_HI20:
  4683. case R_NDS32_PLTREL_LO12:
  4684. /* Relocation is to the entry for this symbol in the
  4685. procedure linkage table. */
  4686. /* The native assembler will generate a 25_PLTREL reloc
  4687. for a local symbol if you assemble a call from one
  4688. section to another when using -K pic. */
  4689. if (h == NULL)
  4690. break;
  4691. if (h->forced_local)
  4692. break;
  4693. if (h->plt.offset == (bfd_vma) - 1)
  4694. /* We didn't make a PLT entry for this symbol. This
  4695. happens when statically linking PIC code, or when
  4696. using -Bsymbolic. */
  4697. break;
  4698. if (splt == NULL)
  4699. break;
  4700. relocation = (splt->output_section->vma
  4701. + splt->output_offset
  4702. + h->plt.offset + 4)
  4703. - (input_section->output_section->vma
  4704. + input_section->output_offset
  4705. + rel->r_offset);
  4706. break;
  4707. case R_NDS32_GOTPC20:
  4708. /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
  4709. ld24 rx,#_GLOBAL_OFFSET_TABLE_ */
  4710. relocation = elf_gp (output_bfd);
  4711. break;
  4712. case R_NDS32_GOTPC_HI20:
  4713. case R_NDS32_GOTPC_LO12:
  4714. /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
  4715. bl .+4
  4716. seth rx,#high(_GLOBAL_OFFSET_TABLE_)
  4717. or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
  4718. or
  4719. bl .+4
  4720. seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
  4721. add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4) */
  4722. relocation = elf_gp (output_bfd);
  4723. relocation -= (input_section->output_section->vma
  4724. + input_section->output_offset + rel->r_offset);
  4725. break;
  4726. case R_NDS32_GOT20:
  4727. /* Fall through. */
  4728. case R_NDS32_GOT_HI20:
  4729. case R_NDS32_GOT_LO12:
  4730. case R_NDS32_GOT_LO15:
  4731. case R_NDS32_GOT_LO19:
  4732. /* Relocation is to the entry for this symbol in the global
  4733. offset table. */
  4734. BFD_ASSERT (sgot != NULL);
  4735. if (h != NULL)
  4736. {
  4737. /* External symbol */
  4738. bool dyn;
  4739. off = h->got.offset;
  4740. BFD_ASSERT (off != (bfd_vma) - 1);
  4741. dyn = htab->root.dynamic_sections_created;
  4742. if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  4743. bfd_link_pic (info),
  4744. h)
  4745. || (bfd_link_pic (info)
  4746. && (info->symbolic
  4747. || h->dynindx == -1
  4748. || h->forced_local) && h->def_regular))
  4749. {
  4750. /* This is actually a static link, or it is a
  4751. -Bsymbolic link and the symbol is defined
  4752. locally, or the symbol was forced to be local
  4753. because of a version file. We must initialize
  4754. this entry in the global offset table. Since the
  4755. offset must always be a multiple of 4, we use the
  4756. least significant bit to record whether we have
  4757. initialized it already.
  4758. When doing a dynamic link, we create a .rela.got
  4759. relocation entry to initialize the value. This
  4760. is done in the finish_dynamic_symbol routine. */
  4761. if ((off & 1) != 0) /* clear LSB */
  4762. off &= ~1;
  4763. else
  4764. {
  4765. bfd_put_32 (output_bfd, relocation, sgot->contents + off);
  4766. h->got.offset |= 1;
  4767. }
  4768. }
  4769. relocation = sgot->output_section->vma + sgot->output_offset + off
  4770. - elf_gp (output_bfd);
  4771. }
  4772. else
  4773. {
  4774. /* Local symbol */
  4775. bfd_byte *loc;
  4776. BFD_ASSERT (local_got_offsets != NULL
  4777. && local_got_offsets[r_symndx] != (bfd_vma) - 1);
  4778. off = local_got_offsets[r_symndx];
  4779. /* The offset must always be a multiple of 4. We use
  4780. the least significant bit to record whether we have
  4781. already processed this entry. */
  4782. if ((off & 1) != 0) /* clear LSB */
  4783. off &= ~1;
  4784. else
  4785. {
  4786. bfd_put_32 (output_bfd, relocation, sgot->contents + off);
  4787. if (bfd_link_pic (info))
  4788. {
  4789. asection *srelgot;
  4790. Elf_Internal_Rela outrel;
  4791. /* We need to generate a R_NDS32_RELATIVE reloc
  4792. for the dynamic linker. */
  4793. srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
  4794. BFD_ASSERT (srelgot != NULL);
  4795. outrel.r_offset = (elf_gp (output_bfd)
  4796. + sgot->output_offset + off);
  4797. outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
  4798. outrel.r_addend = relocation;
  4799. loc = srelgot->contents;
  4800. loc +=
  4801. srelgot->reloc_count * sizeof (Elf32_External_Rela);
  4802. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  4803. ++srelgot->reloc_count;
  4804. }
  4805. local_got_offsets[r_symndx] |= 1;
  4806. }
  4807. relocation = sgot->output_section->vma + sgot->output_offset + off
  4808. - elf_gp (output_bfd);
  4809. }
  4810. break;
  4811. case R_NDS32_16_RELA:
  4812. case R_NDS32_20_RELA:
  4813. case R_NDS32_5_RELA:
  4814. case R_NDS32_32_RELA:
  4815. case R_NDS32_9_PCREL_RELA:
  4816. case R_NDS32_WORD_9_PCREL_RELA:
  4817. case R_NDS32_10_UPCREL_RELA:
  4818. case R_NDS32_15_PCREL_RELA:
  4819. case R_NDS32_17_PCREL_RELA:
  4820. case R_NDS32_25_PCREL_RELA:
  4821. case R_NDS32_HI20_RELA:
  4822. case R_NDS32_LO12S3_RELA:
  4823. case R_NDS32_LO12S2_RELA:
  4824. case R_NDS32_LO12S2_DP_RELA:
  4825. case R_NDS32_LO12S2_SP_RELA:
  4826. case R_NDS32_LO12S1_RELA:
  4827. case R_NDS32_LO12S0_RELA:
  4828. case R_NDS32_LO12S0_ORI_RELA:
  4829. if (bfd_link_pic (info) && r_symndx != 0
  4830. && (input_section->flags & SEC_ALLOC) != 0
  4831. && (eliminate_gc_relocs == 0
  4832. || (sec && (sec->flags & SEC_EXCLUDE) == 0))
  4833. && ((r_type != R_NDS32_9_PCREL_RELA
  4834. && r_type != R_NDS32_WORD_9_PCREL_RELA
  4835. && r_type != R_NDS32_10_UPCREL_RELA
  4836. && r_type != R_NDS32_15_PCREL_RELA
  4837. && r_type != R_NDS32_17_PCREL_RELA
  4838. && r_type != R_NDS32_25_PCREL_RELA
  4839. && !(r_type == R_NDS32_32_RELA
  4840. && strcmp (input_section->name, ".eh_frame") == 0))
  4841. || (h != NULL && h->dynindx != -1
  4842. && (!info->symbolic || !h->def_regular))))
  4843. {
  4844. Elf_Internal_Rela outrel;
  4845. bool skip, relocate;
  4846. bfd_byte *loc;
  4847. /* When generating a shared object, these relocations
  4848. are copied into the output file to be resolved at run
  4849. time. */
  4850. if (sreloc == NULL)
  4851. {
  4852. const char *name;
  4853. name = bfd_elf_string_from_elf_section
  4854. (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
  4855. elf_section_data (input_section)->rela.hdr->sh_name);
  4856. if (name == NULL)
  4857. return false;
  4858. BFD_ASSERT (startswith (name, ".rela")
  4859. && strcmp (bfd_section_name (input_section),
  4860. name + 5) == 0);
  4861. sreloc = bfd_get_section_by_name (dynobj, name);
  4862. BFD_ASSERT (sreloc != NULL);
  4863. }
  4864. skip = false;
  4865. relocate = false;
  4866. outrel.r_offset = _bfd_elf_section_offset (output_bfd,
  4867. info,
  4868. input_section,
  4869. rel->r_offset);
  4870. if (outrel.r_offset == (bfd_vma) - 1)
  4871. skip = true;
  4872. else if (outrel.r_offset == (bfd_vma) - 2)
  4873. skip = true, relocate = true;
  4874. outrel.r_offset += (input_section->output_section->vma
  4875. + input_section->output_offset);
  4876. if (skip)
  4877. memset (&outrel, 0, sizeof outrel);
  4878. else if (r_type == R_NDS32_17_PCREL_RELA
  4879. || r_type == R_NDS32_15_PCREL_RELA
  4880. || r_type == R_NDS32_25_PCREL_RELA)
  4881. {
  4882. BFD_ASSERT (h != NULL && h->dynindx != -1);
  4883. outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
  4884. outrel.r_addend = rel->r_addend;
  4885. }
  4886. else
  4887. {
  4888. /* h->dynindx may be -1 if this symbol was marked to
  4889. become local. */
  4890. if (h == NULL
  4891. || ((info->symbolic || h->dynindx == -1)
  4892. && h->def_regular)
  4893. || (bfd_link_pie (info) && h->def_regular))
  4894. {
  4895. relocate = true;
  4896. outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
  4897. outrel.r_addend = relocation + rel->r_addend;
  4898. if (h)
  4899. {
  4900. h->plt.offset = (bfd_vma) -1; /* cancel PLT trampoline. */
  4901. BFD_ASSERT (sgot != NULL);
  4902. /* If we did not allocate got entry for the symbol,
  4903. we can not fill the nonexistent got entry. */
  4904. if (h->got.offset != (bfd_vma) -1
  4905. && (h->got.offset & 1) == 0)
  4906. {
  4907. bfd_put_32 (output_bfd, outrel.r_addend,
  4908. sgot->contents + h->got.offset);
  4909. }
  4910. }
  4911. }
  4912. else
  4913. {
  4914. if (h->dynindx == -1)
  4915. {
  4916. _bfd_error_handler
  4917. (_("%pB: relocation %s against `%s' can not be used when "
  4918. "making a shared object; recompile with -fPIC"),
  4919. input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
  4920. bfd_set_error (bfd_error_bad_value);
  4921. return false;
  4922. }
  4923. outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
  4924. outrel.r_addend = rel->r_addend;
  4925. }
  4926. }
  4927. loc = sreloc->contents;
  4928. loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
  4929. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  4930. ++sreloc->reloc_count;
  4931. /* If this reloc is against an external symbol, we do
  4932. not want to fiddle with the addend. Otherwise, we
  4933. need to include the symbol value so that it becomes
  4934. an addend for the dynamic reloc. */
  4935. if (!relocate)
  4936. continue;
  4937. }
  4938. break;
  4939. case R_NDS32_25_ABS_RELA:
  4940. if (bfd_link_pic (info))
  4941. {
  4942. _bfd_error_handler
  4943. (_("%pB: warning: %s unsupported in shared mode"),
  4944. input_bfd, "R_NDS32_25_ABS_RELA");
  4945. return false;
  4946. }
  4947. break;
  4948. case R_NDS32_9_PCREL:
  4949. r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
  4950. contents, offset,
  4951. sec, relocation, addend);
  4952. goto check_reloc;
  4953. case R_NDS32_HI20:
  4954. /* We allow an arbitrary number of HI20 relocs before the
  4955. LO12 reloc. This permits gcc to emit the HI and LO relocs
  4956. itself. */
  4957. for (lorel = rel + 1;
  4958. (lorel < relend
  4959. && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
  4960. continue;
  4961. if (lorel < relend
  4962. && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
  4963. || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
  4964. || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
  4965. || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
  4966. {
  4967. nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
  4968. contents, relocation + addend);
  4969. r = bfd_reloc_ok;
  4970. }
  4971. else
  4972. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  4973. contents, offset, relocation,
  4974. addend);
  4975. goto check_reloc;
  4976. case R_NDS32_GOT17S2_RELA:
  4977. case R_NDS32_GOT15S2_RELA:
  4978. BFD_ASSERT (sgot != NULL);
  4979. if (h != NULL)
  4980. {
  4981. bool dyn;
  4982. off = h->got.offset;
  4983. BFD_ASSERT (off != (bfd_vma) - 1);
  4984. dyn = htab->root.dynamic_sections_created;
  4985. if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
  4986. (dyn, bfd_link_pic (info), h)
  4987. || (bfd_link_pic (info)
  4988. && (info->symbolic
  4989. || h->dynindx == -1
  4990. || h->forced_local)
  4991. && h->def_regular))
  4992. {
  4993. /* This is actually a static link, or it is a
  4994. -Bsymbolic link and the symbol is defined
  4995. locally, or the symbol was forced to be local
  4996. because of a version file. We must initialize
  4997. this entry in the global offset table. Since the
  4998. offset must always be a multiple of 4, we use the
  4999. least significant bit to record whether we have
  5000. initialized it already.
  5001. When doing a dynamic link, we create a .rela.got
  5002. relocation entry to initialize the value. This
  5003. is done in the finish_dynamic_symbol routine. */
  5004. if ((off & 1) != 0)
  5005. off &= ~1;
  5006. else
  5007. {
  5008. bfd_put_32 (output_bfd, relocation,
  5009. sgot->contents + off);
  5010. h->got.offset |= 1;
  5011. }
  5012. }
  5013. }
  5014. else
  5015. {
  5016. bfd_byte *loc;
  5017. BFD_ASSERT (local_got_offsets != NULL
  5018. && local_got_offsets[r_symndx] != (bfd_vma) - 1);
  5019. off = local_got_offsets[r_symndx];
  5020. /* The offset must always be a multiple of 4. We use
  5021. the least significant bit to record whether we have
  5022. already processed this entry. */
  5023. if ((off & 1) != 0)
  5024. off &= ~1;
  5025. else
  5026. {
  5027. bfd_put_32 (output_bfd, relocation, sgot->contents + off);
  5028. if (bfd_link_pic (info))
  5029. {
  5030. asection *srelgot;
  5031. Elf_Internal_Rela outrel;
  5032. /* We need to generate a R_NDS32_RELATIVE reloc
  5033. for the dynamic linker. */
  5034. srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
  5035. BFD_ASSERT (srelgot != NULL);
  5036. outrel.r_offset = (elf_gp (output_bfd)
  5037. + sgot->output_offset + off);
  5038. outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
  5039. outrel.r_addend = relocation;
  5040. loc = srelgot->contents;
  5041. loc +=
  5042. srelgot->reloc_count * sizeof (Elf32_External_Rela);
  5043. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  5044. ++srelgot->reloc_count;
  5045. }
  5046. local_got_offsets[r_symndx] |= 1;
  5047. }
  5048. }
  5049. relocation = sgot->output_section->vma + sgot->output_offset + off
  5050. - elf_gp (output_bfd);
  5051. if (relocation & align)
  5052. {
  5053. /* Incorrect alignment. */
  5054. _bfd_error_handler
  5055. (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
  5056. ret = false;
  5057. r = bfd_reloc_dangerous;
  5058. goto check_reloc;
  5059. }
  5060. break;
  5061. case R_NDS32_SDA16S3_RELA:
  5062. case R_NDS32_SDA15S3_RELA:
  5063. case R_NDS32_SDA15S3:
  5064. align = 0x7;
  5065. goto handle_sda;
  5066. case R_NDS32_SDA17S2_RELA:
  5067. case R_NDS32_SDA15S2_RELA:
  5068. case R_NDS32_SDA12S2_SP_RELA:
  5069. case R_NDS32_SDA12S2_DP_RELA:
  5070. case R_NDS32_SDA15S2:
  5071. case R_NDS32_SDA_FP7U2_RELA:
  5072. align = 0x3;
  5073. goto handle_sda;
  5074. case R_NDS32_SDA18S1_RELA:
  5075. case R_NDS32_SDA15S1_RELA:
  5076. case R_NDS32_SDA15S1:
  5077. align = 0x1;
  5078. goto handle_sda;
  5079. case R_NDS32_SDA19S0_RELA:
  5080. case R_NDS32_SDA15S0_RELA:
  5081. case R_NDS32_SDA15S0:
  5082. align = 0x0;
  5083. handle_sda:
  5084. BFD_ASSERT (sec != NULL);
  5085. /* If the symbol is in the abs section, the out_bfd will be null.
  5086. This happens when the relocation has a symbol@GOTOFF. */
  5087. r = nds32_elf_final_sda_base (output_bfd, info, &gp, false);
  5088. if (r != bfd_reloc_ok)
  5089. {
  5090. _bfd_error_handler
  5091. (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
  5092. ret = false;
  5093. goto check_reloc;
  5094. }
  5095. /* At this point `relocation' contains the object's
  5096. address. */
  5097. if (r_type == R_NDS32_SDA_FP7U2_RELA)
  5098. {
  5099. relocation -= fpbase_addr;
  5100. }
  5101. else
  5102. relocation -= gp;
  5103. /* Now it contains the offset from _SDA_BASE_. */
  5104. /* Make sure alignment is correct. */
  5105. if (relocation & align)
  5106. {
  5107. /* Incorrect alignment. */
  5108. _bfd_error_handler
  5109. /* xgettext:c-format */
  5110. (_("%pB(%pA): warning: unaligned small data access"
  5111. " of type %d"),
  5112. input_bfd, input_section, r_type);
  5113. ret = false;
  5114. goto check_reloc;
  5115. }
  5116. break;
  5117. case R_NDS32_17IFC_PCREL_RELA:
  5118. case R_NDS32_10IFCU_PCREL_RELA:
  5119. /* Do nothing. */
  5120. break;
  5121. case R_NDS32_TLS_LE_HI20:
  5122. case R_NDS32_TLS_LE_LO12:
  5123. case R_NDS32_TLS_LE_20:
  5124. case R_NDS32_TLS_LE_15S0:
  5125. case R_NDS32_TLS_LE_15S1:
  5126. case R_NDS32_TLS_LE_15S2:
  5127. /* We do not have garbage collection for got entries.
  5128. Therefore, IE to LE may have one empty entry, and DESC to
  5129. LE may have two. */
  5130. if (elf_hash_table (info)->tls_sec != NULL)
  5131. relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
  5132. break;
  5133. case R_NDS32_TLS_IE_HI20:
  5134. case R_NDS32_TLS_IE_LO12S2:
  5135. case R_NDS32_TLS_DESC_HI20:
  5136. case R_NDS32_TLS_DESC_LO12:
  5137. case R_NDS32_TLS_IE_LO12:
  5138. case R_NDS32_TLS_IEGP_HI20:
  5139. case R_NDS32_TLS_IEGP_LO12:
  5140. case R_NDS32_TLS_IEGP_LO12S2:
  5141. {
  5142. /* Relocation is to the entry for this symbol in the global
  5143. offset table. */
  5144. enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
  5145. asection *srelgot;
  5146. Elf_Internal_Rela outrel;
  5147. bfd_byte *loc;
  5148. int indx = 0;
  5149. eff_tls_type = org_tls_type = get_tls_type (r_type, h);
  5150. BFD_ASSERT (sgot != NULL);
  5151. if (h != NULL)
  5152. {
  5153. bool dyn;
  5154. off = h->got.offset;
  5155. BFD_ASSERT (off != (bfd_vma) -1);
  5156. dyn = htab->root.dynamic_sections_created;
  5157. tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
  5158. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
  5159. && (!bfd_link_pic (info)
  5160. || !SYMBOL_REFERENCES_LOCAL (info, h)))
  5161. indx = h->dynindx;
  5162. }
  5163. else
  5164. {
  5165. BFD_ASSERT (local_got_offsets != NULL
  5166. && local_got_offsets[r_symndx] != (bfd_vma) - 1);
  5167. off = local_got_offsets[r_symndx];
  5168. tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
  5169. }
  5170. relocation = sgot->output_section->vma + sgot->output_offset + off;
  5171. if (1 < ones32 (tls_type))
  5172. {
  5173. eff_tls_type = 1 << (fls (tls_type) - 1);
  5174. /* TLS model shall be handled in nds32_elf_unify_tls_model (). */
  5175. /* TLS model X -> LE is not implement yet!
  5176. workaround here! */
  5177. if (eff_tls_type == GOT_TLS_LE)
  5178. {
  5179. eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
  5180. }
  5181. }
  5182. /* The offset must always be a multiple of 4. We use
  5183. the least significant bit to record whether we have
  5184. already processed this entry. */
  5185. bool need_relocs = false;
  5186. srelgot = ehtab->srelgot;
  5187. if ((bfd_link_pic (info) || indx != 0)
  5188. && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  5189. || h->root.type != bfd_link_hash_undefweak))
  5190. {
  5191. need_relocs = true;
  5192. BFD_ASSERT (srelgot != NULL);
  5193. }
  5194. if (off & 1)
  5195. {
  5196. off &= ~1;
  5197. relocation &= ~1;
  5198. if (eff_tls_type & GOT_TLS_DESC)
  5199. {
  5200. relocation -= elf_gp (output_bfd);
  5201. if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
  5202. {
  5203. /* TLS model shall be converted. */
  5204. BFD_ASSERT(0);
  5205. }
  5206. }
  5207. else if (eff_tls_type & GOT_TLS_IEGP)
  5208. {
  5209. relocation -= elf_gp (output_bfd);
  5210. }
  5211. }
  5212. else
  5213. {
  5214. if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
  5215. {
  5216. /* TLS model workaround shall be applied. */
  5217. BFD_ASSERT(0);
  5218. }
  5219. else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
  5220. {
  5221. if (eff_tls_type & GOT_TLS_IEGP)
  5222. relocation -= elf_gp(output_bfd);
  5223. if (need_relocs)
  5224. {
  5225. if (indx == 0)
  5226. outrel.r_addend = gottpoff (info, relocation_sym);
  5227. else
  5228. outrel.r_addend = 0;
  5229. outrel.r_offset = (sgot->output_section->vma
  5230. + sgot->output_offset + off);
  5231. outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
  5232. elf32_nds32_add_dynreloc (output_bfd, info, srelgot,
  5233. &outrel);
  5234. }
  5235. else
  5236. {
  5237. bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
  5238. sgot->contents + off);
  5239. }
  5240. }
  5241. else if (eff_tls_type & GOT_TLS_DESC)
  5242. {
  5243. relocation -= elf_gp (output_bfd);
  5244. if (need_relocs)
  5245. {
  5246. if (indx == 0)
  5247. outrel.r_addend = gottpoff (info, relocation_sym);
  5248. else
  5249. outrel.r_addend = 0;
  5250. outrel.r_offset = (sgot->output_section->vma
  5251. + sgot->output_offset + off);
  5252. outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
  5253. if (htab->tls_desc_trampoline)
  5254. {
  5255. asection *srelplt;
  5256. srelplt = ehtab->srelplt;
  5257. loc = srelplt->contents;
  5258. loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
  5259. BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
  5260. <= srelplt->contents + srelplt->size);
  5261. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  5262. }
  5263. else
  5264. {
  5265. loc = srelgot->contents;
  5266. loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
  5267. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  5268. ++srelgot->reloc_count;
  5269. }
  5270. }
  5271. else
  5272. {
  5273. /* feed me! */
  5274. bfd_put_32 (output_bfd, 0xdeadbeef,
  5275. sgot->contents + off);
  5276. bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
  5277. sgot->contents + off + 4);
  5278. patch_tls_desc_to_ie (contents, rel, input_bfd);
  5279. BFD_ASSERT(0);
  5280. }
  5281. }
  5282. else
  5283. {
  5284. /* TLS model workaround shall be applied. */
  5285. BFD_ASSERT(0);
  5286. }
  5287. if (h != NULL)
  5288. h->got.offset |= 1;
  5289. else
  5290. local_got_offsets[r_symndx] |= 1;
  5291. }
  5292. }
  5293. break;
  5294. /* DON'T fall through. */
  5295. default:
  5296. /* OLD_NDS32_RELOC. */
  5297. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  5298. contents, offset, relocation, addend);
  5299. goto check_reloc;
  5300. }
  5301. switch ((int) r_type)
  5302. {
  5303. case R_NDS32_20_RELA:
  5304. case R_NDS32_5_RELA:
  5305. case R_NDS32_9_PCREL_RELA:
  5306. case R_NDS32_WORD_9_PCREL_RELA:
  5307. case R_NDS32_10_UPCREL_RELA:
  5308. case R_NDS32_15_PCREL_RELA:
  5309. case R_NDS32_17_PCREL_RELA:
  5310. case R_NDS32_25_PCREL_RELA:
  5311. case R_NDS32_25_ABS_RELA:
  5312. case R_NDS32_HI20_RELA:
  5313. case R_NDS32_LO12S3_RELA:
  5314. case R_NDS32_LO12S2_RELA:
  5315. case R_NDS32_LO12S2_DP_RELA:
  5316. case R_NDS32_LO12S2_SP_RELA:
  5317. case R_NDS32_LO12S1_RELA:
  5318. case R_NDS32_LO12S0_RELA:
  5319. case R_NDS32_LO12S0_ORI_RELA:
  5320. case R_NDS32_SDA16S3_RELA:
  5321. case R_NDS32_SDA17S2_RELA:
  5322. case R_NDS32_SDA18S1_RELA:
  5323. case R_NDS32_SDA19S0_RELA:
  5324. case R_NDS32_SDA15S3_RELA:
  5325. case R_NDS32_SDA15S2_RELA:
  5326. case R_NDS32_SDA12S2_DP_RELA:
  5327. case R_NDS32_SDA12S2_SP_RELA:
  5328. case R_NDS32_SDA15S1_RELA:
  5329. case R_NDS32_SDA15S0_RELA:
  5330. case R_NDS32_SDA_FP7U2_RELA:
  5331. case R_NDS32_9_PLTREL:
  5332. case R_NDS32_25_PLTREL:
  5333. case R_NDS32_GOT20:
  5334. case R_NDS32_GOT_HI20:
  5335. case R_NDS32_GOT_LO12:
  5336. case R_NDS32_GOT_LO15:
  5337. case R_NDS32_GOT_LO19:
  5338. case R_NDS32_GOT15S2_RELA:
  5339. case R_NDS32_GOT17S2_RELA:
  5340. case R_NDS32_GOTPC20:
  5341. case R_NDS32_GOTPC_HI20:
  5342. case R_NDS32_GOTPC_LO12:
  5343. case R_NDS32_GOTOFF:
  5344. case R_NDS32_GOTOFF_HI20:
  5345. case R_NDS32_GOTOFF_LO12:
  5346. case R_NDS32_GOTOFF_LO15:
  5347. case R_NDS32_GOTOFF_LO19:
  5348. case R_NDS32_PLTREL_HI20:
  5349. case R_NDS32_PLTREL_LO12:
  5350. case R_NDS32_PLT_GOTREL_HI20:
  5351. case R_NDS32_PLT_GOTREL_LO12:
  5352. case R_NDS32_PLT_GOTREL_LO15:
  5353. case R_NDS32_PLT_GOTREL_LO19:
  5354. case R_NDS32_PLT_GOTREL_LO20:
  5355. case R_NDS32_17IFC_PCREL_RELA:
  5356. case R_NDS32_10IFCU_PCREL_RELA:
  5357. case R_NDS32_TLS_LE_HI20:
  5358. case R_NDS32_TLS_LE_LO12:
  5359. case R_NDS32_TLS_IE_HI20:
  5360. case R_NDS32_TLS_IE_LO12S2:
  5361. case R_NDS32_TLS_LE_20:
  5362. case R_NDS32_TLS_LE_15S0:
  5363. case R_NDS32_TLS_LE_15S1:
  5364. case R_NDS32_TLS_LE_15S2:
  5365. case R_NDS32_TLS_DESC_HI20:
  5366. case R_NDS32_TLS_DESC_LO12:
  5367. case R_NDS32_TLS_IE_LO12:
  5368. case R_NDS32_TLS_IEGP_HI20:
  5369. case R_NDS32_TLS_IEGP_LO12:
  5370. case R_NDS32_TLS_IEGP_LO12S2:
  5371. /* Instruction related relocs must handle endian properly. */
  5372. /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER. */
  5373. r = nds32_elf_final_link_relocate (howto, input_bfd,
  5374. input_section, contents,
  5375. rel->r_offset, relocation,
  5376. rel->r_addend);
  5377. break;
  5378. default:
  5379. /* All other relocs can use default handler. */
  5380. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  5381. contents, rel->r_offset,
  5382. relocation, rel->r_addend);
  5383. break;
  5384. }
  5385. check_reloc:
  5386. if (r != bfd_reloc_ok)
  5387. {
  5388. /* FIXME: This should be generic enough to go in a utility. */
  5389. const char *name;
  5390. if (h != NULL)
  5391. name = h->root.root.string;
  5392. else
  5393. {
  5394. name = bfd_elf_string_from_elf_section
  5395. (input_bfd, symtab_hdr->sh_link, sym->st_name);
  5396. if (name == NULL || *name == '\0')
  5397. name = bfd_section_name (sec);
  5398. }
  5399. if (errmsg != NULL)
  5400. goto common_error;
  5401. switch (r)
  5402. {
  5403. case bfd_reloc_overflow:
  5404. (*info->callbacks->reloc_overflow)
  5405. (info, (h ? &h->root : NULL), name, howto->name,
  5406. (bfd_vma) 0, input_bfd, input_section, offset);
  5407. break;
  5408. case bfd_reloc_undefined:
  5409. (*info->callbacks->undefined_symbol)
  5410. (info, name, input_bfd, input_section, offset, true);
  5411. break;
  5412. case bfd_reloc_outofrange:
  5413. errmsg = _("internal error: out of range error");
  5414. goto common_error;
  5415. case bfd_reloc_notsupported:
  5416. errmsg = _("internal error: unsupported relocation error");
  5417. goto common_error;
  5418. case bfd_reloc_dangerous:
  5419. errmsg = _("internal error: dangerous error");
  5420. goto common_error;
  5421. default:
  5422. errmsg = _("internal error: unknown error");
  5423. /* Fall through. */
  5424. common_error:
  5425. (*info->callbacks->warning) (info, errmsg, name, input_bfd,
  5426. input_section, offset);
  5427. break;
  5428. }
  5429. }
  5430. }
  5431. /* Resotre header size to avoid overflow load. */
  5432. if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
  5433. symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
  5434. return ret;
  5435. }
  5436. /* Finish up dynamic symbol handling. We set the contents of various
  5437. dynamic sections here. */
  5438. static bool
  5439. nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
  5440. struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
  5441. {
  5442. struct elf_link_hash_table *ehtab;
  5443. struct elf_nds32_link_hash_entry *hent;
  5444. bfd_byte *loc;
  5445. ehtab = elf_hash_table (info);
  5446. hent = (struct elf_nds32_link_hash_entry *) h;
  5447. if (h->plt.offset != (bfd_vma) - 1)
  5448. {
  5449. asection *splt;
  5450. asection *sgot;
  5451. asection *srela;
  5452. bfd_vma plt_index;
  5453. bfd_vma got_offset;
  5454. bfd_vma local_plt_offset;
  5455. Elf_Internal_Rela rela;
  5456. /* This symbol has an entry in the procedure linkage table. Set
  5457. it up. */
  5458. BFD_ASSERT (h->dynindx != -1);
  5459. splt = ehtab->splt;
  5460. sgot = ehtab->sgotplt;
  5461. srela = ehtab->srelplt;
  5462. BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
  5463. /* Get the index in the procedure linkage table which
  5464. corresponds to this symbol. This is the index of this symbol
  5465. in all the symbols for which we are making plt entries. The
  5466. first entry in the procedure linkage table is reserved. */
  5467. plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
  5468. /* Get the offset into the .got table of the entry that
  5469. corresponds to this function. Each .got entry is 4 bytes.
  5470. The first three are reserved. */
  5471. got_offset = (plt_index + 3) * 4;
  5472. /* Fill in the entry in the procedure linkage table. */
  5473. if (!bfd_link_pic (info))
  5474. {
  5475. unsigned long insn;
  5476. insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
  5477. + sgot->output_offset + got_offset) >> 12)
  5478. & 0xfffff);
  5479. bfd_putb32 (insn, splt->contents + h->plt.offset);
  5480. insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
  5481. + sgot->output_offset + got_offset) & 0x0fff)
  5482. >> 2);
  5483. bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
  5484. insn = PLT_ENTRY_WORD2;
  5485. bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
  5486. insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
  5487. bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
  5488. insn = PLT_ENTRY_WORD4
  5489. + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
  5490. bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
  5491. local_plt_offset = 12;
  5492. }
  5493. else
  5494. {
  5495. /* sda_base must be set at this time. */
  5496. unsigned long insn;
  5497. long offset;
  5498. offset = sgot->output_section->vma + sgot->output_offset + got_offset
  5499. - elf_gp (output_bfd);
  5500. insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
  5501. bfd_putb32 (insn, splt->contents + h->plt.offset);
  5502. insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
  5503. bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
  5504. insn = PLT_PIC_ENTRY_WORD2;
  5505. bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
  5506. insn = PLT_PIC_ENTRY_WORD3;
  5507. bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
  5508. insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
  5509. bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
  5510. insn = PLT_PIC_ENTRY_WORD5
  5511. + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
  5512. bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
  5513. local_plt_offset = 16;
  5514. }
  5515. /* Fill in the entry in the global offset table,
  5516. so it will fall through to the next instruction for the first time. */
  5517. bfd_put_32 (output_bfd,
  5518. (splt->output_section->vma + splt->output_offset
  5519. + h->plt.offset + local_plt_offset),
  5520. sgot->contents + got_offset);
  5521. /* Fill in the entry in the .rela.plt section. */
  5522. rela.r_offset = (sgot->output_section->vma
  5523. + sgot->output_offset + got_offset);
  5524. rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
  5525. rela.r_addend = 0;
  5526. loc = srela->contents;
  5527. loc += plt_index * sizeof (Elf32_External_Rela);
  5528. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  5529. if (!h->def_regular)
  5530. {
  5531. /* Mark the symbol as undefined, rather than as defined in
  5532. the .plt section. Leave the value alone. */
  5533. sym->st_shndx = SHN_UNDEF;
  5534. if (!h->ref_regular_nonweak)
  5535. sym->st_value = 0;
  5536. }
  5537. }
  5538. if (h->got.offset != (bfd_vma) - 1
  5539. && hent->tls_type == GOT_NORMAL)
  5540. {
  5541. asection *sgot;
  5542. asection *srelagot;
  5543. Elf_Internal_Rela rela;
  5544. /* This symbol has an entry in the global offset table.
  5545. Set it up. */
  5546. sgot = ehtab->sgot;
  5547. srelagot = ehtab->srelgot;
  5548. BFD_ASSERT (sgot != NULL && srelagot != NULL);
  5549. rela.r_offset = (sgot->output_section->vma
  5550. + sgot->output_offset + (h->got.offset & ~1));
  5551. /* If this is a -Bsymbolic link, and the symbol is defined
  5552. locally, we just want to emit a RELATIVE reloc. Likewise if
  5553. the symbol was forced to be local because of a version file.
  5554. The entry in the global offset table will already have been
  5555. initialized in the relocate_section function. */
  5556. if ((bfd_link_pic (info)
  5557. && (info->symbolic || h->dynindx == -1 || h->forced_local)
  5558. && h->def_regular)
  5559. || (bfd_link_pie (info) && h->def_regular))
  5560. {
  5561. rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
  5562. rela.r_addend = (h->root.u.def.value
  5563. + h->root.u.def.section->output_section->vma
  5564. + h->root.u.def.section->output_offset);
  5565. if ((h->got.offset & 1) == 0)
  5566. {
  5567. bfd_put_32 (output_bfd, rela.r_addend,
  5568. sgot->contents + h->got.offset);
  5569. }
  5570. }
  5571. else
  5572. {
  5573. BFD_ASSERT ((h->got.offset & 1) == 0);
  5574. bfd_put_32 (output_bfd, (bfd_vma) 0,
  5575. sgot->contents + h->got.offset);
  5576. rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
  5577. rela.r_addend = 0;
  5578. }
  5579. loc = srelagot->contents;
  5580. loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
  5581. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  5582. ++srelagot->reloc_count;
  5583. BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
  5584. }
  5585. if (h->needs_copy)
  5586. {
  5587. asection *s;
  5588. Elf_Internal_Rela rela;
  5589. /* This symbols needs a copy reloc. Set it up. */
  5590. BFD_ASSERT (h->dynindx != -1
  5591. && (h->root.type == bfd_link_hash_defined
  5592. || h->root.type == bfd_link_hash_defweak));
  5593. s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
  5594. BFD_ASSERT (s != NULL);
  5595. rela.r_offset = (h->root.u.def.value
  5596. + h->root.u.def.section->output_section->vma
  5597. + h->root.u.def.section->output_offset);
  5598. rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
  5599. rela.r_addend = 0;
  5600. loc = s->contents;
  5601. loc += s->reloc_count * sizeof (Elf32_External_Rela);
  5602. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  5603. ++s->reloc_count;
  5604. }
  5605. /* Mark some specially defined symbols as absolute. */
  5606. if (strcmp (h->root.root.string, "_DYNAMIC") == 0
  5607. || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
  5608. sym->st_shndx = SHN_ABS;
  5609. return true;
  5610. }
  5611. /* Finish up the dynamic sections. */
  5612. static bool
  5613. nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
  5614. {
  5615. bfd *dynobj;
  5616. asection *sdyn;
  5617. asection *sgotplt;
  5618. struct elf_link_hash_table *ehtab;
  5619. struct elf_nds32_link_hash_table *htab;
  5620. ehtab = elf_hash_table (info);
  5621. htab = nds32_elf_hash_table (info);
  5622. if (htab == NULL)
  5623. return false;
  5624. dynobj = elf_hash_table (info)->dynobj;
  5625. sgotplt = ehtab->sgotplt;
  5626. /* A broken linker script might have discarded the dynamic sections.
  5627. Catch this here so that we do not seg-fault later on. */
  5628. if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
  5629. return false;
  5630. sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
  5631. if (elf_hash_table (info)->dynamic_sections_created)
  5632. {
  5633. asection *splt;
  5634. Elf32_External_Dyn *dyncon, *dynconend;
  5635. BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
  5636. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  5637. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  5638. for (; dyncon < dynconend; dyncon++)
  5639. {
  5640. Elf_Internal_Dyn dyn;
  5641. asection *s;
  5642. bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  5643. switch (dyn.d_tag)
  5644. {
  5645. default:
  5646. break;
  5647. case DT_PLTGOT:
  5648. /* name = ".got"; */
  5649. s = ehtab->sgot->output_section;
  5650. goto get_vma;
  5651. case DT_JMPREL:
  5652. s = ehtab->srelplt->output_section;
  5653. get_vma:
  5654. BFD_ASSERT (s != NULL);
  5655. dyn.d_un.d_ptr = s->vma;
  5656. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5657. break;
  5658. case DT_PLTRELSZ:
  5659. s = ehtab->srelplt->output_section;
  5660. BFD_ASSERT (s != NULL);
  5661. dyn.d_un.d_val = s->size;
  5662. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5663. break;
  5664. case DT_RELASZ:
  5665. /* My reading of the SVR4 ABI indicates that the
  5666. procedure linkage table relocs (DT_JMPREL) should be
  5667. included in the overall relocs (DT_RELA). This is
  5668. what Solaris does. However, UnixWare can not handle
  5669. that case. Therefore, we override the DT_RELASZ entry
  5670. here to make it not include the JMPREL relocs. Since
  5671. the linker script arranges for .rela.plt to follow all
  5672. other relocation sections, we don't have to worry
  5673. about changing the DT_RELA entry. */
  5674. if (ehtab->srelplt != NULL)
  5675. {
  5676. s = ehtab->srelplt->output_section;
  5677. dyn.d_un.d_val -= s->size;
  5678. }
  5679. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5680. break;
  5681. case DT_TLSDESC_PLT:
  5682. s = htab->root.splt;
  5683. dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
  5684. + htab->root.tlsdesc_plt);
  5685. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5686. break;
  5687. case DT_TLSDESC_GOT:
  5688. s = htab->root.sgot;
  5689. dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
  5690. + htab->root.tlsdesc_got);
  5691. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  5692. break;
  5693. }
  5694. }
  5695. /* Fill in the first entry in the procedure linkage table. */
  5696. splt = ehtab->splt;
  5697. if (splt && splt->size > 0)
  5698. {
  5699. if (bfd_link_pic (info))
  5700. {
  5701. unsigned long insn;
  5702. long offset;
  5703. offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
  5704. - elf_gp (output_bfd);
  5705. insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
  5706. bfd_putb32 (insn, splt->contents);
  5707. /* here has a typo? */
  5708. insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
  5709. bfd_putb32 (insn, splt->contents + 4);
  5710. insn = PLT0_PIC_ENTRY_WORD2;
  5711. bfd_putb32 (insn, splt->contents + 8);
  5712. insn = PLT0_PIC_ENTRY_WORD3;
  5713. bfd_putb32 (insn, splt->contents + 12);
  5714. insn = PLT0_PIC_ENTRY_WORD4;
  5715. bfd_putb32 (insn, splt->contents + 16);
  5716. insn = PLT0_PIC_ENTRY_WORD5;
  5717. bfd_putb32 (insn, splt->contents + 20);
  5718. }
  5719. else
  5720. {
  5721. unsigned long insn;
  5722. unsigned long addr;
  5723. /* addr = .got + 4 */
  5724. addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
  5725. insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
  5726. bfd_putb32 (insn, splt->contents);
  5727. insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
  5728. bfd_putb32 (insn, splt->contents + 4);
  5729. insn = PLT0_ENTRY_WORD2;
  5730. bfd_putb32 (insn, splt->contents + 8);
  5731. insn = PLT0_ENTRY_WORD3;
  5732. bfd_putb32 (insn, splt->contents + 12);
  5733. insn = PLT0_ENTRY_WORD4;
  5734. bfd_putb32 (insn, splt->contents + 16);
  5735. }
  5736. elf_section_data (splt->output_section)->this_hdr.sh_entsize =
  5737. PLT_ENTRY_SIZE;
  5738. }
  5739. if (htab->root.tlsdesc_plt)
  5740. {
  5741. /* Calculate addresses. */
  5742. asection *sgot = sgot = ehtab->sgot;
  5743. bfd_vma pltgot = sgotplt->output_section->vma
  5744. + sgotplt->output_offset;
  5745. bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
  5746. + htab->root.tlsdesc_got;
  5747. /* Get GP offset. */
  5748. pltgot -= elf_gp (output_bfd) - 4; /* PLTGOT[1] */
  5749. tlsdesc_got -= elf_gp (output_bfd);
  5750. /* Do relocation. */
  5751. dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
  5752. dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
  5753. dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
  5754. dl_tlsdesc_lazy_trampoline[5] += 0xfff & pltgot;
  5755. /* Insert .plt. */
  5756. nds32_put_trampoline (splt->contents + htab->root.tlsdesc_plt,
  5757. dl_tlsdesc_lazy_trampoline,
  5758. ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
  5759. }
  5760. }
  5761. /* Fill in the first three entries in the global offset table. */
  5762. if (sgotplt && sgotplt->size > 0)
  5763. {
  5764. if (sdyn == NULL)
  5765. bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
  5766. else
  5767. bfd_put_32 (output_bfd,
  5768. sdyn->output_section->vma + sdyn->output_offset,
  5769. sgotplt->contents);
  5770. bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
  5771. bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
  5772. elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
  5773. }
  5774. return true;
  5775. }
  5776. /* Set the right machine number. */
  5777. static bool
  5778. nds32_elf_object_p (bfd *abfd)
  5779. {
  5780. static unsigned int cur_arch = 0;
  5781. if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
  5782. {
  5783. /* E_N1_ARCH is a wild card, so it is set only when no others exist. */
  5784. cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
  5785. }
  5786. switch (cur_arch)
  5787. {
  5788. default:
  5789. case E_N1_ARCH:
  5790. bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
  5791. break;
  5792. case E_N1H_ARCH:
  5793. bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
  5794. break;
  5795. case E_NDS_ARCH_STAR_V2_0:
  5796. bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
  5797. break;
  5798. case E_NDS_ARCH_STAR_V3_0:
  5799. bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
  5800. break;
  5801. case E_NDS_ARCH_STAR_V3_M:
  5802. bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
  5803. break;
  5804. }
  5805. return true;
  5806. }
  5807. /* Store the machine number in the flags field. */
  5808. static bool
  5809. nds32_elf_final_write_processing (bfd *abfd)
  5810. {
  5811. unsigned long val;
  5812. static unsigned int cur_mach = 0;
  5813. if (bfd_mach_n1 != bfd_get_mach (abfd))
  5814. {
  5815. cur_mach = bfd_get_mach (abfd);
  5816. }
  5817. switch (cur_mach)
  5818. {
  5819. case bfd_mach_n1:
  5820. /* Only happen when object is empty, since the case is abandon. */
  5821. val = E_N1_ARCH;
  5822. val |= E_NDS_ABI_AABI;
  5823. val |= E_NDS32_ELF_VER_1_4;
  5824. break;
  5825. case bfd_mach_n1h:
  5826. val = E_N1H_ARCH;
  5827. break;
  5828. case bfd_mach_n1h_v2:
  5829. val = E_NDS_ARCH_STAR_V2_0;
  5830. break;
  5831. case bfd_mach_n1h_v3:
  5832. val = E_NDS_ARCH_STAR_V3_0;
  5833. break;
  5834. case bfd_mach_n1h_v3m:
  5835. val = E_NDS_ARCH_STAR_V3_M;
  5836. break;
  5837. default:
  5838. val = 0;
  5839. break;
  5840. }
  5841. elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
  5842. elf_elfheader (abfd)->e_flags |= val;
  5843. return _bfd_elf_final_write_processing (abfd);
  5844. }
  5845. /* Function to keep NDS32 specific file flags. */
  5846. static bool
  5847. nds32_elf_set_private_flags (bfd *abfd, flagword flags)
  5848. {
  5849. BFD_ASSERT (!elf_flags_init (abfd)
  5850. || elf_elfheader (abfd)->e_flags == flags);
  5851. elf_elfheader (abfd)->e_flags = flags;
  5852. elf_flags_init (abfd) = true;
  5853. return true;
  5854. }
  5855. static unsigned int
  5856. convert_e_flags (unsigned int e_flags, unsigned int arch)
  5857. {
  5858. if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
  5859. {
  5860. /* From 0.9 to 1.0. */
  5861. e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
  5862. /* Invert E_NDS32_HAS_NO_MAC_INST. */
  5863. e_flags ^= E_NDS32_HAS_NO_MAC_INST;
  5864. if (arch == E_NDS_ARCH_STAR_V1_0)
  5865. {
  5866. /* Done. */
  5867. return e_flags;
  5868. }
  5869. }
  5870. /* From 1.0 to 2.0. */
  5871. e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
  5872. /* Clear E_NDS32_HAS_MFUSR_PC_INST. */
  5873. e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
  5874. /* Invert E_NDS32_HAS_NO_MAC_INST. */
  5875. e_flags ^= E_NDS32_HAS_NO_MAC_INST;
  5876. return e_flags;
  5877. }
  5878. static bool
  5879. nds32_check_vec_size (bfd *ibfd)
  5880. {
  5881. static unsigned int nds32_vec_size = 0;
  5882. asection *sec_t = NULL;
  5883. bfd_byte *contents = NULL;
  5884. sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
  5885. if (sec_t && sec_t->size >= 4)
  5886. {
  5887. /* Get vec_size in file. */
  5888. unsigned int flag_t;
  5889. nds32_get_section_contents (ibfd, sec_t, &contents, true);
  5890. flag_t = bfd_get_32 (ibfd, contents);
  5891. /* The value could only be 4 or 16. */
  5892. if (!nds32_vec_size)
  5893. /* Set if not set yet. */
  5894. nds32_vec_size = (flag_t & 0x3);
  5895. else if (nds32_vec_size != (flag_t & 0x3))
  5896. {
  5897. _bfd_error_handler
  5898. /* xgettext:c-format */
  5899. (_("%pB: ISR vector size mismatch"
  5900. " with previous modules, previous %u-byte, current %u-byte"),
  5901. ibfd,
  5902. nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
  5903. (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
  5904. return false;
  5905. }
  5906. else
  5907. /* Only keep the first vec_size section. */
  5908. sec_t->flags |= SEC_EXCLUDE;
  5909. }
  5910. return true;
  5911. }
  5912. /* Merge backend specific data from an object file to the output
  5913. object file when linking. */
  5914. static bool
  5915. nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  5916. {
  5917. bfd *obfd = info->output_bfd;
  5918. flagword out_flags;
  5919. flagword in_flags;
  5920. flagword out_16regs;
  5921. flagword in_no_mac;
  5922. flagword out_no_mac;
  5923. flagword in_16regs;
  5924. flagword out_version;
  5925. flagword in_version;
  5926. flagword out_fpu_config;
  5927. flagword in_fpu_config;
  5928. /* FIXME: What should be checked when linking shared libraries? */
  5929. if ((ibfd->flags & DYNAMIC) != 0)
  5930. return true;
  5931. /* TODO: Revise to use object-attributes instead. */
  5932. if (!nds32_check_vec_size (ibfd))
  5933. return false;
  5934. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  5935. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  5936. return true;
  5937. if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
  5938. {
  5939. _bfd_error_handler
  5940. (_("%pB: warning: endian mismatch with previous modules"), ibfd);
  5941. bfd_set_error (bfd_error_bad_value);
  5942. return false;
  5943. }
  5944. /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
  5945. treat as generic one without checking and merging. */
  5946. if (elf_elfheader (ibfd)->e_flags)
  5947. {
  5948. in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
  5949. if (in_version == E_NDS32_ELF_VER_1_2)
  5950. {
  5951. _bfd_error_handler
  5952. (_("%pB: warning: older version of object file encountered, "
  5953. "please recompile with current tool chain"), ibfd);
  5954. }
  5955. /* We may need to merge V1 and V2 arch object files to V2. */
  5956. if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
  5957. != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
  5958. {
  5959. /* Need to convert version. */
  5960. if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
  5961. == E_NDS_ARCH_STAR_RESERVED)
  5962. {
  5963. elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
  5964. }
  5965. else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
  5966. == E_NDS_ARCH_STAR_V3_M
  5967. && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
  5968. == E_NDS_ARCH_STAR_V3_0)
  5969. {
  5970. elf_elfheader (ibfd)->e_flags =
  5971. (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
  5972. | E_NDS_ARCH_STAR_V3_0;
  5973. }
  5974. else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
  5975. == E_NDS_ARCH_STAR_V0_9
  5976. || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
  5977. > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
  5978. {
  5979. elf_elfheader (obfd)->e_flags =
  5980. convert_e_flags (elf_elfheader (obfd)->e_flags,
  5981. (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
  5982. }
  5983. else
  5984. {
  5985. elf_elfheader (ibfd)->e_flags =
  5986. convert_e_flags (elf_elfheader (ibfd)->e_flags,
  5987. (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
  5988. }
  5989. }
  5990. /* Extract some flags. */
  5991. in_flags = elf_elfheader (ibfd)->e_flags
  5992. & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
  5993. | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
  5994. /* The following flags need special treatment. */
  5995. in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
  5996. in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
  5997. in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
  5998. /* Extract some flags. */
  5999. out_flags = elf_elfheader (obfd)->e_flags
  6000. & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
  6001. | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
  6002. /* The following flags need special treatment. */
  6003. out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
  6004. out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
  6005. out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
  6006. out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
  6007. if (!elf_flags_init (obfd))
  6008. {
  6009. /* If the input is the default architecture then do not
  6010. bother setting the flags for the output architecture,
  6011. instead allow future merges to do this. If no future
  6012. merges ever set these flags then they will retain their
  6013. unitialised values, which surprise surprise, correspond
  6014. to the default values. */
  6015. if (bfd_get_arch_info (ibfd)->the_default)
  6016. return true;
  6017. elf_flags_init (obfd) = true;
  6018. elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
  6019. if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
  6020. && bfd_get_arch_info (obfd)->the_default)
  6021. {
  6022. return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
  6023. bfd_get_mach (ibfd));
  6024. }
  6025. return true;
  6026. }
  6027. /* Check flag compatibility. */
  6028. if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
  6029. {
  6030. _bfd_error_handler
  6031. (_("%pB: error: ABI mismatch with previous modules"), ibfd);
  6032. bfd_set_error (bfd_error_bad_value);
  6033. return false;
  6034. }
  6035. if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
  6036. {
  6037. if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
  6038. {
  6039. _bfd_error_handler
  6040. (_("%pB: error: instruction set mismatch with previous modules"),
  6041. ibfd);
  6042. bfd_set_error (bfd_error_bad_value);
  6043. return false;
  6044. }
  6045. }
  6046. /* When linking with V1.2 and V1.3 objects together the output is V1.2.
  6047. and perf ext1 and DIV are mergerd to perf ext1. */
  6048. if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
  6049. {
  6050. elf_elfheader (obfd)->e_flags =
  6051. (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
  6052. | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
  6053. | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
  6054. ? E_NDS32_HAS_EXT_INST : 0)
  6055. | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
  6056. ? E_NDS32_HAS_EXT_INST : 0)
  6057. | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
  6058. | ((in_version > out_version) ? out_version : in_version);
  6059. }
  6060. else
  6061. {
  6062. if (in_version != out_version)
  6063. _bfd_error_handler
  6064. /* xgettext:c-format */
  6065. (_("%pB: warning: incompatible elf-versions %s and %s"),
  6066. ibfd, nds32_elfver_strtab[out_version],
  6067. nds32_elfver_strtab[in_version]);
  6068. elf_elfheader (obfd)->e_flags = in_flags | out_flags
  6069. | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
  6070. | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
  6071. | (in_version > out_version ? out_version : in_version);
  6072. }
  6073. }
  6074. return true;
  6075. }
  6076. /* Display the flags field. */
  6077. static bool
  6078. nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
  6079. {
  6080. FILE *file = (FILE *) ptr;
  6081. BFD_ASSERT (abfd != NULL && ptr != NULL);
  6082. _bfd_elf_print_private_bfd_data (abfd, ptr);
  6083. fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
  6084. switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
  6085. {
  6086. default:
  6087. case E_N1_ARCH:
  6088. fprintf (file, _(": n1 instructions"));
  6089. break;
  6090. case E_N1H_ARCH:
  6091. fprintf (file, _(": n1h instructions"));
  6092. break;
  6093. }
  6094. fputc ('\n', file);
  6095. return true;
  6096. }
  6097. static unsigned int
  6098. nds32_elf_action_discarded (asection *sec)
  6099. {
  6100. if (startswith (sec->name, ".gcc_except_table"))
  6101. return 0;
  6102. return _bfd_elf_default_action_discarded (sec);
  6103. }
  6104. static asection *
  6105. nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
  6106. Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
  6107. Elf_Internal_Sym *sym)
  6108. {
  6109. if (h != NULL)
  6110. switch (ELF32_R_TYPE (rel->r_info))
  6111. {
  6112. case R_NDS32_GNU_VTINHERIT:
  6113. case R_NDS32_GNU_VTENTRY:
  6114. case R_NDS32_RELA_GNU_VTINHERIT:
  6115. case R_NDS32_RELA_GNU_VTENTRY:
  6116. return NULL;
  6117. }
  6118. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  6119. }
  6120. static enum elf_nds32_tls_type
  6121. get_tls_type (enum elf_nds32_reloc_type r_type,
  6122. struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
  6123. {
  6124. enum elf_nds32_tls_type tls_type;
  6125. switch (r_type)
  6126. {
  6127. case R_NDS32_TLS_LE_HI20:
  6128. case R_NDS32_TLS_LE_LO12:
  6129. tls_type = GOT_TLS_LE;
  6130. break;
  6131. case R_NDS32_TLS_IE_HI20:
  6132. case R_NDS32_TLS_IE_LO12S2:
  6133. case R_NDS32_TLS_IE_LO12:
  6134. tls_type = GOT_TLS_IE;
  6135. break;
  6136. case R_NDS32_TLS_IEGP_HI20:
  6137. case R_NDS32_TLS_IEGP_LO12:
  6138. case R_NDS32_TLS_IEGP_LO12S2:
  6139. tls_type = GOT_TLS_IEGP;
  6140. break;
  6141. case R_NDS32_TLS_DESC_HI20:
  6142. case R_NDS32_TLS_DESC_LO12:
  6143. case R_NDS32_TLS_DESC_ADD:
  6144. case R_NDS32_TLS_DESC_FUNC:
  6145. case R_NDS32_TLS_DESC_CALL:
  6146. tls_type = GOT_TLS_DESC;
  6147. break;
  6148. default:
  6149. tls_type = GOT_NORMAL;
  6150. break;
  6151. }
  6152. return tls_type;
  6153. }
  6154. /* Ensure that we have allocated bookkeeping structures for ABFD's local
  6155. symbols. */
  6156. static bool
  6157. elf32_nds32_allocate_local_sym_info (bfd *abfd)
  6158. {
  6159. if (elf_local_got_refcounts (abfd) == NULL)
  6160. {
  6161. bfd_size_type num_syms;
  6162. bfd_size_type size;
  6163. char *data;
  6164. num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
  6165. /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
  6166. gp_offset. The details can refer to struct elf_nds32_obj_tdata. */
  6167. size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
  6168. + sizeof (bfd_vma) + sizeof (int)
  6169. + sizeof (bool) + sizeof (bfd_vma));
  6170. data = bfd_zalloc (abfd, size);
  6171. if (data == NULL)
  6172. return false;
  6173. elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
  6174. data += num_syms * sizeof (bfd_signed_vma);
  6175. elf32_nds32_local_got_tls_type (abfd) = (char *) data;
  6176. data += num_syms * sizeof (char);
  6177. elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
  6178. data += num_syms * sizeof (bfd_vma);
  6179. elf32_nds32_local_gp_offset (abfd) = (int *) data;
  6180. data += num_syms * sizeof (int);
  6181. }
  6182. return true;
  6183. }
  6184. /* Look through the relocs for a section during the first phase.
  6185. Since we don't do .gots or .plts, we just need to consider the
  6186. virtual table relocs for gc. */
  6187. static bool
  6188. nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
  6189. asection *sec, const Elf_Internal_Rela *relocs)
  6190. {
  6191. Elf_Internal_Shdr *symtab_hdr;
  6192. struct elf_link_hash_entry **sym_hashes;
  6193. const Elf_Internal_Rela *rel;
  6194. const Elf_Internal_Rela *rel_end;
  6195. struct elf_link_hash_table *ehtab;
  6196. struct elf_nds32_link_hash_table *htab;
  6197. bfd *dynobj;
  6198. asection *sreloc = NULL;
  6199. /* No need for relocation if relocatable already. */
  6200. if (bfd_link_relocatable (info))
  6201. {
  6202. elf32_nds32_check_relax_group (abfd, sec);
  6203. return true;
  6204. }
  6205. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  6206. sym_hashes = elf_sym_hashes (abfd);
  6207. ehtab = elf_hash_table (info);
  6208. htab = nds32_elf_hash_table (info);
  6209. dynobj = htab->root.dynobj;
  6210. rel_end = relocs + sec->reloc_count;
  6211. for (rel = relocs; rel < rel_end; rel++)
  6212. {
  6213. enum elf_nds32_reloc_type r_type;
  6214. struct elf_link_hash_entry *h;
  6215. unsigned long r_symndx;
  6216. enum elf_nds32_tls_type tls_type, old_tls_type;
  6217. r_symndx = ELF32_R_SYM (rel->r_info);
  6218. r_type = ELF32_R_TYPE (rel->r_info);
  6219. if (r_symndx < symtab_hdr->sh_info)
  6220. h = NULL;
  6221. else
  6222. {
  6223. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  6224. while (h->root.type == bfd_link_hash_indirect
  6225. || h->root.type == bfd_link_hash_warning)
  6226. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  6227. }
  6228. /* Create .got section if necessary.
  6229. Some relocs require a global offset table. We create
  6230. got section here, since these relocation need a got section
  6231. and if it is not created yet. */
  6232. if (ehtab->sgot == NULL)
  6233. {
  6234. switch (r_type)
  6235. {
  6236. case R_NDS32_GOT_HI20:
  6237. case R_NDS32_GOT_LO12:
  6238. case R_NDS32_GOT_LO15:
  6239. case R_NDS32_GOT_LO19:
  6240. case R_NDS32_GOT17S2_RELA:
  6241. case R_NDS32_GOT15S2_RELA:
  6242. case R_NDS32_GOTOFF:
  6243. case R_NDS32_GOTOFF_HI20:
  6244. case R_NDS32_GOTOFF_LO12:
  6245. case R_NDS32_GOTOFF_LO15:
  6246. case R_NDS32_GOTOFF_LO19:
  6247. case R_NDS32_GOTPC20:
  6248. case R_NDS32_GOTPC_HI20:
  6249. case R_NDS32_GOTPC_LO12:
  6250. case R_NDS32_GOT20:
  6251. case R_NDS32_TLS_IE_HI20:
  6252. case R_NDS32_TLS_IE_LO12:
  6253. case R_NDS32_TLS_IE_LO12S2:
  6254. case R_NDS32_TLS_IEGP_HI20:
  6255. case R_NDS32_TLS_IEGP_LO12:
  6256. case R_NDS32_TLS_IEGP_LO12S2:
  6257. case R_NDS32_TLS_DESC_HI20:
  6258. case R_NDS32_TLS_DESC_LO12:
  6259. if (dynobj == NULL)
  6260. htab->root.dynobj = dynobj = abfd;
  6261. if (!create_got_section (dynobj, info))
  6262. return false;
  6263. break;
  6264. default:
  6265. break;
  6266. }
  6267. }
  6268. /* Check relocation type. */
  6269. switch ((int) r_type)
  6270. {
  6271. case R_NDS32_GOT_HI20:
  6272. case R_NDS32_GOT_LO12:
  6273. case R_NDS32_GOT_LO15:
  6274. case R_NDS32_GOT_LO19:
  6275. case R_NDS32_GOT20:
  6276. case R_NDS32_TLS_LE_HI20:
  6277. case R_NDS32_TLS_LE_LO12:
  6278. case R_NDS32_TLS_IE_HI20:
  6279. case R_NDS32_TLS_IE_LO12:
  6280. case R_NDS32_TLS_IE_LO12S2:
  6281. case R_NDS32_TLS_IEGP_HI20:
  6282. case R_NDS32_TLS_IEGP_LO12:
  6283. case R_NDS32_TLS_IEGP_LO12S2:
  6284. case R_NDS32_TLS_DESC_HI20:
  6285. case R_NDS32_TLS_DESC_LO12:
  6286. tls_type = get_tls_type (r_type, h);
  6287. if (h)
  6288. {
  6289. if (tls_type != GOT_TLS_LE)
  6290. h->got.refcount += 1;
  6291. old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
  6292. }
  6293. else
  6294. {
  6295. /* This is a global offset table entry for a local symbol. */
  6296. if (!elf32_nds32_allocate_local_sym_info (abfd))
  6297. return false;
  6298. BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
  6299. if (tls_type != GOT_TLS_LE)
  6300. elf_local_got_refcounts (abfd)[r_symndx] += 1;
  6301. old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
  6302. }
  6303. /* We would already have issued an error message if there
  6304. is a TLS/non-TLS mismatch, based on the symbol
  6305. type. So just combine any TLS types needed. */
  6306. if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
  6307. && tls_type != GOT_NORMAL)
  6308. tls_type |= old_tls_type;
  6309. /* DESC to IE/IEGP if link to executable. */
  6310. if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
  6311. && (bfd_link_executable (info)))
  6312. tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
  6313. if (old_tls_type != tls_type)
  6314. {
  6315. if (h != NULL)
  6316. elf32_nds32_hash_entry (h)->tls_type = tls_type;
  6317. else
  6318. elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
  6319. }
  6320. break;
  6321. case R_NDS32_9_PLTREL:
  6322. case R_NDS32_25_PLTREL:
  6323. case R_NDS32_PLTREL_HI20:
  6324. case R_NDS32_PLTREL_LO12:
  6325. case R_NDS32_PLT_GOTREL_HI20:
  6326. case R_NDS32_PLT_GOTREL_LO12:
  6327. case R_NDS32_PLT_GOTREL_LO15:
  6328. case R_NDS32_PLT_GOTREL_LO19:
  6329. case R_NDS32_PLT_GOTREL_LO20:
  6330. /* This symbol requires a procedure linkage table entry. We
  6331. actually build the entry in adjust_dynamic_symbol,
  6332. because this might be a case of linking PIC code without
  6333. linking in any dynamic objects, in which case we don't
  6334. need to generate a procedure linkage table after all. */
  6335. /* If this is a local symbol, we resolve it directly without
  6336. creating a procedure linkage table entry. */
  6337. if (h == NULL)
  6338. continue;
  6339. if (h->forced_local
  6340. || (bfd_link_pie (info) && h->def_regular))
  6341. break;
  6342. elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
  6343. h->needs_plt = 1;
  6344. h->plt.refcount += 1;
  6345. break;
  6346. case R_NDS32_16_RELA:
  6347. case R_NDS32_20_RELA:
  6348. case R_NDS32_5_RELA:
  6349. case R_NDS32_32_RELA:
  6350. case R_NDS32_HI20_RELA:
  6351. case R_NDS32_LO12S3_RELA:
  6352. case R_NDS32_LO12S2_RELA:
  6353. case R_NDS32_LO12S2_DP_RELA:
  6354. case R_NDS32_LO12S2_SP_RELA:
  6355. case R_NDS32_LO12S1_RELA:
  6356. case R_NDS32_LO12S0_RELA:
  6357. case R_NDS32_LO12S0_ORI_RELA:
  6358. case R_NDS32_SDA16S3_RELA:
  6359. case R_NDS32_SDA17S2_RELA:
  6360. case R_NDS32_SDA18S1_RELA:
  6361. case R_NDS32_SDA19S0_RELA:
  6362. case R_NDS32_SDA15S3_RELA:
  6363. case R_NDS32_SDA15S2_RELA:
  6364. case R_NDS32_SDA12S2_DP_RELA:
  6365. case R_NDS32_SDA12S2_SP_RELA:
  6366. case R_NDS32_SDA15S1_RELA:
  6367. case R_NDS32_SDA15S0_RELA:
  6368. case R_NDS32_SDA_FP7U2_RELA:
  6369. case R_NDS32_15_PCREL_RELA:
  6370. case R_NDS32_17_PCREL_RELA:
  6371. case R_NDS32_25_PCREL_RELA:
  6372. if (h != NULL && !bfd_link_pic (info))
  6373. {
  6374. h->non_got_ref = 1;
  6375. h->plt.refcount += 1;
  6376. }
  6377. /* If we are creating a shared library, and this is a reloc against
  6378. a global symbol, or a non PC relative reloc against a local
  6379. symbol, then we need to copy the reloc into the shared library.
  6380. However, if we are linking with -Bsymbolic, we do not need to
  6381. copy a reloc against a global symbol which is defined in an
  6382. object we are including in the link (i.e., DEF_REGULAR is set).
  6383. At this point we have not seen all the input files, so it is
  6384. possible that DEF_REGULAR is not set now but will be set later
  6385. (it is never cleared). We account for that possibility below by
  6386. storing information in the dyn_relocs field of the hash table
  6387. entry. A similar situation occurs when creating shared libraries
  6388. and symbol visibility changes render the symbol local.
  6389. If on the other hand, we are creating an executable, we may need
  6390. to keep relocations for symbols satisfied by a dynamic library
  6391. if we manage to avoid copy relocs for the symbol. */
  6392. if ((bfd_link_pic (info)
  6393. && (sec->flags & SEC_ALLOC) != 0
  6394. && ((r_type != R_NDS32_25_PCREL_RELA
  6395. && r_type != R_NDS32_15_PCREL_RELA
  6396. && r_type != R_NDS32_17_PCREL_RELA
  6397. && !(r_type == R_NDS32_32_RELA
  6398. && strcmp (sec->name, ".eh_frame") == 0))
  6399. || (h != NULL
  6400. && (!info->symbolic
  6401. || h->root.type == bfd_link_hash_defweak
  6402. || !h->def_regular))))
  6403. || (!bfd_link_pic (info)
  6404. && (sec->flags & SEC_ALLOC) != 0
  6405. && h != NULL
  6406. && (h->root.type == bfd_link_hash_defweak
  6407. || !h->def_regular)))
  6408. {
  6409. struct elf_dyn_relocs *p;
  6410. struct elf_dyn_relocs **head;
  6411. if (dynobj == NULL)
  6412. htab->root.dynobj = dynobj = abfd;
  6413. /* When creating a shared object, we must copy these
  6414. relocs into the output file. We create a reloc
  6415. section in dynobj and make room for the reloc. */
  6416. if (sreloc == NULL)
  6417. {
  6418. const char *name;
  6419. name = bfd_elf_string_from_elf_section
  6420. (abfd, elf_elfheader (abfd)->e_shstrndx,
  6421. elf_section_data (sec)->rela.hdr->sh_name);
  6422. if (name == NULL)
  6423. return false;
  6424. BFD_ASSERT (startswith (name, ".rela")
  6425. && strcmp (bfd_section_name (sec),
  6426. name + 5) == 0);
  6427. sreloc = bfd_get_section_by_name (dynobj, name);
  6428. if (sreloc == NULL)
  6429. {
  6430. flagword flags;
  6431. sreloc = bfd_make_section (dynobj, name);
  6432. flags = (SEC_HAS_CONTENTS | SEC_READONLY
  6433. | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  6434. if ((sec->flags & SEC_ALLOC) != 0)
  6435. flags |= SEC_ALLOC | SEC_LOAD;
  6436. if (sreloc == NULL
  6437. || !bfd_set_section_flags (sreloc, flags)
  6438. || !bfd_set_section_alignment (sreloc, 2))
  6439. return false;
  6440. elf_section_type (sreloc) = SHT_RELA;
  6441. }
  6442. elf_section_data (sec)->sreloc = sreloc;
  6443. }
  6444. /* If this is a global symbol, we count the number of
  6445. relocations we need for this symbol. */
  6446. if (h != NULL)
  6447. head = &h->dyn_relocs;
  6448. else
  6449. {
  6450. asection *s;
  6451. void *vpp;
  6452. Elf_Internal_Sym *isym;
  6453. isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
  6454. abfd, r_symndx);
  6455. if (isym == NULL)
  6456. return false;
  6457. /* Track dynamic relocs needed for local syms too. */
  6458. s = bfd_section_from_elf_index (abfd, isym->st_shndx);
  6459. if (s == NULL)
  6460. return false;
  6461. vpp = &elf_section_data (s)->local_dynrel;
  6462. head = (struct elf_dyn_relocs **) vpp;
  6463. }
  6464. p = *head;
  6465. if (p == NULL || p->sec != sec)
  6466. {
  6467. size_t amt = sizeof (*p);
  6468. p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
  6469. if (p == NULL)
  6470. return false;
  6471. p->next = *head;
  6472. *head = p;
  6473. p->sec = sec;
  6474. p->count = 0;
  6475. p->pc_count = 0;
  6476. }
  6477. p->count += 1;
  6478. /* Since eh_frame is readonly, R_NDS32_32_RELA
  6479. reloc for eh_frame will cause shared library has
  6480. TEXTREL entry in the dynamic section. This lead glibc
  6481. testsuites to failure (bug-13092) and cause kernel fail
  6482. (bug-11819). I think the best solution is to replace
  6483. absolute reloc with pc relative reloc in the eh_frame.
  6484. To do that, we need to support the following issues:
  6485. === For GCC ===
  6486. * gcc/config/nds32/nds32.h: Define
  6487. ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
  6488. and DW_EH_PE_sdata4 into DWARF exception header when
  6489. option have '-fpic'.
  6490. === For binutils ===
  6491. * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
  6492. * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
  6493. may break our nds DIFF mechanism, therefore, we
  6494. must disable all linker relaxations to ensure
  6495. correctness.
  6496. * gas/config/tc-nds32.c (nds32_apply_fix): Replace
  6497. R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
  6498. do the necessary modification.
  6499. Unfortunately, it still have some problems for nds32
  6500. to support pc relative reloc in the eh_frame. So I use
  6501. another solution to fix this issue.
  6502. However, I find that ld always emit TEXTREL marker for
  6503. R_NDS32_NONE relocs in rel.dyn. These none relocs are
  6504. correspond to R_NDS32_32_RELA for .eh_frame section.
  6505. It means that we always reserve redundant entries of rel.dyn
  6506. for these relocs which actually do nothing in dynamic linker.
  6507. Therefore, we regard these relocs as pc relative relocs
  6508. here and increase the pc_count. */
  6509. if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
  6510. || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
  6511. || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
  6512. || (r_type == R_NDS32_32_RELA
  6513. && strcmp (sec->name, ".eh_frame") == 0))
  6514. p->pc_count += 1;
  6515. }
  6516. break;
  6517. /* This relocation describes the C++ object vtable hierarchy.
  6518. Reconstruct it for later use during GC. */
  6519. case R_NDS32_RELA_GNU_VTINHERIT:
  6520. case R_NDS32_GNU_VTINHERIT:
  6521. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  6522. return false;
  6523. break;
  6524. /* This relocation describes which C++ vtable entries are actually
  6525. used. Record for later use during GC. */
  6526. case R_NDS32_GNU_VTENTRY:
  6527. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
  6528. return false;
  6529. break;
  6530. case R_NDS32_RELA_GNU_VTENTRY:
  6531. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  6532. return false;
  6533. break;
  6534. }
  6535. }
  6536. return true;
  6537. }
  6538. /* Write VAL in uleb128 format to P, returning a pointer to the
  6539. following byte.
  6540. This code is copied from elf-attr.c. */
  6541. static bfd_byte *
  6542. write_uleb128 (bfd_byte *p, unsigned int val)
  6543. {
  6544. bfd_byte c;
  6545. do
  6546. {
  6547. c = val & 0x7f;
  6548. val >>= 7;
  6549. if (val)
  6550. c |= 0x80;
  6551. *(p++) = c;
  6552. }
  6553. while (val);
  6554. return p;
  6555. }
  6556. static bfd_signed_vma
  6557. calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
  6558. Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
  6559. {
  6560. bfd_signed_vma foff;
  6561. bfd_vma symval, addend;
  6562. asection *sym_sec;
  6563. /* Get the value of the symbol referred to by the reloc. */
  6564. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  6565. {
  6566. Elf_Internal_Sym *isym;
  6567. /* A local symbol. */
  6568. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  6569. if (isym->st_shndx == SHN_UNDEF)
  6570. sym_sec = bfd_und_section_ptr;
  6571. else if (isym->st_shndx == SHN_ABS)
  6572. sym_sec = bfd_abs_section_ptr;
  6573. else if (isym->st_shndx == SHN_COMMON)
  6574. sym_sec = bfd_com_section_ptr;
  6575. else
  6576. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  6577. symval = isym->st_value + sym_sec->output_section->vma
  6578. + sym_sec->output_offset;
  6579. }
  6580. else
  6581. {
  6582. unsigned long indx;
  6583. struct elf_link_hash_entry *h;
  6584. /* An external symbol. */
  6585. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  6586. h = elf_sym_hashes (abfd)[indx];
  6587. BFD_ASSERT (h != NULL);
  6588. if (h->root.type != bfd_link_hash_defined
  6589. && h->root.type != bfd_link_hash_defweak)
  6590. /* This appears to be a reference to an undefined
  6591. symbol. Just ignore it--it will be caught by the
  6592. regular reloc processing. */
  6593. return 0;
  6594. if (h->root.u.def.section->flags & SEC_MERGE)
  6595. {
  6596. sym_sec = h->root.u.def.section;
  6597. symval = _bfd_merged_section_offset (abfd, &sym_sec,
  6598. elf_section_data (sym_sec)->sec_info,
  6599. h->root.u.def.value);
  6600. symval = symval + sym_sec->output_section->vma
  6601. + sym_sec->output_offset;
  6602. }
  6603. else
  6604. symval = (h->root.u.def.value
  6605. + h->root.u.def.section->output_section->vma
  6606. + h->root.u.def.section->output_offset);
  6607. }
  6608. addend = irel->r_addend;
  6609. foff = (symval + addend
  6610. - (irel->r_offset + sec->output_section->vma + sec->output_offset));
  6611. return foff;
  6612. }
  6613. /* Convert a 32-bit instruction to 16-bit one.
  6614. INSN is the input 32-bit instruction, INSN16 is the output 16-bit
  6615. instruction. If INSN_TYPE is not NULL, it the CGEN instruction
  6616. type of INSN16. Return 1 if successful. */
  6617. static int
  6618. nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
  6619. int *pinsn_type)
  6620. {
  6621. uint16_t insn16 = 0;
  6622. int insn_type = 0;
  6623. unsigned long mach = bfd_get_mach (abfd);
  6624. if (N32_SH5 (insn) != 0)
  6625. return 0;
  6626. switch (N32_SUB5 (insn))
  6627. {
  6628. case N32_ALU1_ADD_SLLI:
  6629. case N32_ALU1_ADD_SRLI:
  6630. if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
  6631. {
  6632. insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
  6633. N32_RB5 (insn));
  6634. insn_type = NDS32_INSN_ADD333;
  6635. }
  6636. else if (N32_IS_RT4 (insn))
  6637. {
  6638. if (N32_RT5 (insn) == N32_RA5 (insn))
  6639. insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
  6640. else if (N32_RT5 (insn) == N32_RB5 (insn))
  6641. insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
  6642. insn_type = NDS32_INSN_ADD45;
  6643. }
  6644. break;
  6645. case N32_ALU1_SUB_SLLI:
  6646. case N32_ALU1_SUB_SRLI:
  6647. if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
  6648. {
  6649. insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
  6650. N32_RB5 (insn));
  6651. insn_type = NDS32_INSN_SUB333;
  6652. }
  6653. else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
  6654. {
  6655. insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
  6656. insn_type = NDS32_INSN_SUB45;
  6657. }
  6658. break;
  6659. case N32_ALU1_AND_SLLI:
  6660. case N32_ALU1_AND_SRLI:
  6661. /* and $rt, $rt, $rb -> and33 for v3, v3m. */
  6662. if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
  6663. && N32_IS_RB3 (insn))
  6664. {
  6665. if (N32_RT5 (insn) == N32_RA5 (insn))
  6666. insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
  6667. else if (N32_RT5 (insn) == N32_RB5 (insn))
  6668. insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
  6669. if (insn16)
  6670. insn_type = NDS32_INSN_AND33;
  6671. }
  6672. break;
  6673. case N32_ALU1_XOR_SLLI:
  6674. case N32_ALU1_XOR_SRLI:
  6675. /* xor $rt, $rt, $rb -> xor33 for v3, v3m. */
  6676. if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
  6677. && N32_IS_RB3 (insn))
  6678. {
  6679. if (N32_RT5 (insn) == N32_RA5 (insn))
  6680. insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
  6681. else if (N32_RT5 (insn) == N32_RB5 (insn))
  6682. insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
  6683. if (insn16)
  6684. insn_type = NDS32_INSN_XOR33;
  6685. }
  6686. break;
  6687. case N32_ALU1_OR_SLLI:
  6688. case N32_ALU1_OR_SRLI:
  6689. /* or $rt, $rt, $rb -> or33 for v3, v3m. */
  6690. if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
  6691. && N32_IS_RB3 (insn))
  6692. {
  6693. if (N32_RT5 (insn) == N32_RA5 (insn))
  6694. insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
  6695. else if (N32_RT5 (insn) == N32_RB5 (insn))
  6696. insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
  6697. if (insn16)
  6698. insn_type = NDS32_INSN_OR33;
  6699. }
  6700. break;
  6701. case N32_ALU1_NOR:
  6702. /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
  6703. if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
  6704. && N32_RA5 (insn) == N32_RB5 (insn))
  6705. {
  6706. insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
  6707. insn_type = NDS32_INSN_NOT33;
  6708. }
  6709. break;
  6710. case N32_ALU1_SRAI:
  6711. if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
  6712. {
  6713. insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
  6714. insn_type = NDS32_INSN_SRAI45;
  6715. }
  6716. break;
  6717. case N32_ALU1_SRLI:
  6718. if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
  6719. {
  6720. insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
  6721. insn_type = NDS32_INSN_SRLI45;
  6722. }
  6723. break;
  6724. case N32_ALU1_SLLI:
  6725. if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
  6726. {
  6727. insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
  6728. N32_UB5 (insn));
  6729. insn_type = NDS32_INSN_SLLI333;
  6730. }
  6731. break;
  6732. case N32_ALU1_ZEH:
  6733. if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
  6734. {
  6735. insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
  6736. insn_type = NDS32_INSN_ZEH33;
  6737. }
  6738. break;
  6739. case N32_ALU1_SEB:
  6740. if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
  6741. {
  6742. insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
  6743. insn_type = NDS32_INSN_SEB33;
  6744. }
  6745. break;
  6746. case N32_ALU1_SEH:
  6747. if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
  6748. {
  6749. insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
  6750. insn_type = NDS32_INSN_SEH33;
  6751. }
  6752. break;
  6753. case N32_ALU1_SLT:
  6754. if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
  6755. {
  6756. /* Implicit r15. */
  6757. insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
  6758. insn_type = NDS32_INSN_SLT45;
  6759. }
  6760. break;
  6761. case N32_ALU1_SLTS:
  6762. if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
  6763. {
  6764. /* Implicit r15. */
  6765. insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
  6766. insn_type = NDS32_INSN_SLTS45;
  6767. }
  6768. break;
  6769. }
  6770. if ((insn16 & 0x8000) == 0)
  6771. return 0;
  6772. if (pinsn16)
  6773. *pinsn16 = insn16;
  6774. if (pinsn_type)
  6775. *pinsn_type = insn_type;
  6776. return 1;
  6777. }
  6778. static int
  6779. nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
  6780. int *pinsn_type)
  6781. {
  6782. uint16_t insn16 = 0;
  6783. int insn_type;
  6784. unsigned long mach = bfd_get_mach (abfd);
  6785. /* TODO: bset, bclr, btgl, btst. */
  6786. if (__GF (insn, 6, 4) != 0)
  6787. return 0;
  6788. switch (N32_IMMU (insn, 6))
  6789. {
  6790. case N32_ALU2_MUL:
  6791. if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
  6792. && N32_IS_RB3 (insn))
  6793. {
  6794. if (N32_RT5 (insn) == N32_RA5 (insn))
  6795. insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
  6796. else if (N32_RT5 (insn) == N32_RB5 (insn))
  6797. insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
  6798. if (insn16)
  6799. insn_type = NDS32_INSN_MUL33;
  6800. }
  6801. }
  6802. if ((insn16 & 0x8000) == 0)
  6803. return 0;
  6804. if (pinsn16)
  6805. *pinsn16 = insn16;
  6806. if (pinsn_type)
  6807. *pinsn_type = insn_type;
  6808. return 1;
  6809. }
  6810. int
  6811. nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
  6812. int *pinsn_type)
  6813. {
  6814. int op6;
  6815. uint16_t insn16 = 0;
  6816. int insn_type = 0;
  6817. unsigned long mach = bfd_get_mach (abfd);
  6818. /* Decode 32-bit instruction. */
  6819. if (insn & 0x80000000)
  6820. {
  6821. /* Not 32-bit insn. */
  6822. return 0;
  6823. }
  6824. op6 = N32_OP6 (insn);
  6825. /* Convert it to 16-bit instruction. */
  6826. switch (op6)
  6827. {
  6828. case N32_OP6_MOVI:
  6829. if (IS_WITHIN_S (N32_IMM20S (insn), 5))
  6830. {
  6831. insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
  6832. insn_type = NDS32_INSN_MOVI55;
  6833. }
  6834. else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
  6835. && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
  6836. {
  6837. insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
  6838. N32_IMM20S (insn) - 16);
  6839. insn_type = NDS32_INSN_MOVPI45;
  6840. }
  6841. break;
  6842. case N32_OP6_ADDI:
  6843. if (N32_IMM15S (insn) == 0)
  6844. {
  6845. /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
  6846. because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
  6847. if (mach <= MACH_V2
  6848. || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
  6849. {
  6850. insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
  6851. insn_type = NDS32_INSN_MOV55;
  6852. }
  6853. }
  6854. else if (N32_IMM15S (insn) > 0)
  6855. {
  6856. if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
  6857. {
  6858. insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
  6859. N32_IMM15S (insn));
  6860. insn_type = NDS32_INSN_ADDI333;
  6861. }
  6862. else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
  6863. && N32_IMM15S (insn) < 32)
  6864. {
  6865. insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
  6866. insn_type = NDS32_INSN_ADDI45;
  6867. }
  6868. else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
  6869. && N32_RT5 (insn) == N32_RA5 (insn)
  6870. && N32_IMM15S (insn) < 512)
  6871. {
  6872. insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
  6873. insn_type = NDS32_INSN_ADDI10_SP;
  6874. }
  6875. else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
  6876. && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
  6877. && (N32_IMM15S (insn) % 4 == 0))
  6878. {
  6879. insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
  6880. N32_IMM15S (insn) >> 2);
  6881. insn_type = NDS32_INSN_ADDRI36_SP;
  6882. }
  6883. }
  6884. else
  6885. {
  6886. /* Less than 0. */
  6887. if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
  6888. {
  6889. insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
  6890. 0 - N32_IMM15S (insn));
  6891. insn_type = NDS32_INSN_SUBI333;
  6892. }
  6893. else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
  6894. && N32_IMM15S (insn) > -32)
  6895. {
  6896. insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
  6897. 0 - N32_IMM15S (insn));
  6898. insn_type = NDS32_INSN_SUBI45;
  6899. }
  6900. else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
  6901. && N32_RT5 (insn) == N32_RA5 (insn)
  6902. && N32_IMM15S (insn) >= -512)
  6903. {
  6904. insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
  6905. insn_type = NDS32_INSN_ADDI10_SP;
  6906. }
  6907. }
  6908. break;
  6909. case N32_OP6_ORI:
  6910. if (N32_IMM15S (insn) == 0)
  6911. {
  6912. /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
  6913. because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
  6914. if (mach <= MACH_V2
  6915. || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
  6916. {
  6917. insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
  6918. insn_type = NDS32_INSN_MOV55;
  6919. }
  6920. }
  6921. break;
  6922. case N32_OP6_SUBRI:
  6923. if (mach >= MACH_V3 && N32_IS_RT3 (insn)
  6924. && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
  6925. {
  6926. insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
  6927. insn_type = NDS32_INSN_NEG33;
  6928. }
  6929. break;
  6930. case N32_OP6_ANDI:
  6931. if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
  6932. {
  6933. if (N32_IMM15U (insn) == 1)
  6934. {
  6935. insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
  6936. insn_type = NDS32_INSN_XLSB33;
  6937. }
  6938. else if (N32_IMM15U (insn) == 0x7ff)
  6939. {
  6940. insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
  6941. insn_type = NDS32_INSN_X11B33;
  6942. }
  6943. else if (N32_IMM15U (insn) == 0xff)
  6944. {
  6945. insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
  6946. insn_type = NDS32_INSN_ZEB33;
  6947. }
  6948. else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
  6949. && N32_IMM15U (insn) < 256)
  6950. {
  6951. int imm15u = N32_IMM15U (insn);
  6952. if (__builtin_popcount (imm15u) == 1)
  6953. {
  6954. /* BMSKI33 */
  6955. int imm3u = __builtin_ctz (imm15u);
  6956. insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
  6957. insn_type = NDS32_INSN_BMSKI33;
  6958. }
  6959. else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
  6960. {
  6961. /* FEXTI33 */
  6962. int imm3u = __builtin_ctz (imm15u + 1) - 1;
  6963. insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
  6964. insn_type = NDS32_INSN_FEXTI33;
  6965. }
  6966. }
  6967. }
  6968. break;
  6969. case N32_OP6_SLTI:
  6970. if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
  6971. && IS_WITHIN_U (N32_IMM15S (insn), 5))
  6972. {
  6973. insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
  6974. insn_type = NDS32_INSN_SLTI45;
  6975. }
  6976. break;
  6977. case N32_OP6_SLTSI:
  6978. if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
  6979. && IS_WITHIN_U (N32_IMM15S (insn), 5))
  6980. {
  6981. insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
  6982. insn_type = NDS32_INSN_SLTSI45;
  6983. }
  6984. break;
  6985. case N32_OP6_LWI:
  6986. if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
  6987. {
  6988. insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
  6989. insn_type = NDS32_INSN_LWI450;
  6990. }
  6991. else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
  6992. && IS_WITHIN_U (N32_IMM15S (insn), 3))
  6993. {
  6994. insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
  6995. N32_IMM15S (insn));
  6996. insn_type = NDS32_INSN_LWI333;
  6997. }
  6998. else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
  6999. && IS_WITHIN_U (N32_IMM15S (insn), 7))
  7000. {
  7001. insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
  7002. insn_type = NDS32_INSN_LWI37;
  7003. }
  7004. else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
  7005. && IS_WITHIN_U (N32_IMM15S (insn), 7))
  7006. {
  7007. insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
  7008. insn_type = NDS32_INSN_LWI37_SP;
  7009. }
  7010. else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
  7011. && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
  7012. {
  7013. insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
  7014. N32_IMM15S (insn) + 32);
  7015. insn_type = NDS32_INSN_LWI45_FE;
  7016. }
  7017. break;
  7018. case N32_OP6_SWI:
  7019. if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
  7020. {
  7021. insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
  7022. insn_type = NDS32_INSN_SWI450;
  7023. }
  7024. else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
  7025. && IS_WITHIN_U (N32_IMM15S (insn), 3))
  7026. {
  7027. insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
  7028. N32_IMM15S (insn));
  7029. insn_type = NDS32_INSN_SWI333;
  7030. }
  7031. else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
  7032. && IS_WITHIN_U (N32_IMM15S (insn), 7))
  7033. {
  7034. insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
  7035. insn_type = NDS32_INSN_SWI37;
  7036. }
  7037. else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
  7038. && IS_WITHIN_U (N32_IMM15S (insn), 7))
  7039. {
  7040. insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
  7041. insn_type = NDS32_INSN_SWI37_SP;
  7042. }
  7043. break;
  7044. case N32_OP6_LWI_BI:
  7045. if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
  7046. && IS_WITHIN_U (N32_IMM15S (insn), 3))
  7047. {
  7048. insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
  7049. N32_IMM15S (insn));
  7050. insn_type = NDS32_INSN_LWI333_BI;
  7051. }
  7052. break;
  7053. case N32_OP6_SWI_BI:
  7054. if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
  7055. && IS_WITHIN_U (N32_IMM15S (insn), 3))
  7056. {
  7057. insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
  7058. N32_IMM15S (insn));
  7059. insn_type = NDS32_INSN_SWI333_BI;
  7060. }
  7061. break;
  7062. case N32_OP6_LHI:
  7063. if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
  7064. && IS_WITHIN_U (N32_IMM15S (insn), 3))
  7065. {
  7066. insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
  7067. N32_IMM15S (insn));
  7068. insn_type = NDS32_INSN_LHI333;
  7069. }
  7070. break;
  7071. case N32_OP6_SHI:
  7072. if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
  7073. && IS_WITHIN_U (N32_IMM15S (insn), 3))
  7074. {
  7075. insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
  7076. N32_IMM15S (insn));
  7077. insn_type = NDS32_INSN_SHI333;
  7078. }
  7079. break;
  7080. case N32_OP6_LBI:
  7081. if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
  7082. && IS_WITHIN_U (N32_IMM15S (insn), 3))
  7083. {
  7084. insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
  7085. N32_IMM15S (insn));
  7086. insn_type = NDS32_INSN_LBI333;
  7087. }
  7088. break;
  7089. case N32_OP6_SBI:
  7090. if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
  7091. && IS_WITHIN_U (N32_IMM15S (insn), 3))
  7092. {
  7093. insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
  7094. N32_IMM15S (insn));
  7095. insn_type = NDS32_INSN_SBI333;
  7096. }
  7097. break;
  7098. case N32_OP6_ALU1:
  7099. return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
  7100. case N32_OP6_ALU2:
  7101. return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
  7102. case N32_OP6_BR1:
  7103. if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
  7104. goto done;
  7105. if ((insn & N32_BIT (14)) == 0)
  7106. {
  7107. /* N32_BR1_BEQ */
  7108. if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
  7109. && N32_RT5 (insn) != REG_R5)
  7110. insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
  7111. else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
  7112. && N32_RA5 (insn) != REG_R5)
  7113. insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
  7114. insn_type = NDS32_INSN_BEQS38;
  7115. break;
  7116. }
  7117. else
  7118. {
  7119. /* N32_BR1_BNE */
  7120. if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
  7121. && N32_RT5 (insn) != REG_R5)
  7122. insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
  7123. else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
  7124. && N32_RA5 (insn) != REG_R5)
  7125. insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
  7126. insn_type = NDS32_INSN_BNES38;
  7127. break;
  7128. }
  7129. break;
  7130. case N32_OP6_BR2:
  7131. switch (N32_BR2_SUB (insn))
  7132. {
  7133. case N32_BR2_BEQZ:
  7134. if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
  7135. {
  7136. insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
  7137. insn_type = NDS32_INSN_BEQZ38;
  7138. }
  7139. else if (N32_RT5 (insn) == REG_R15
  7140. && IS_WITHIN_S (N32_IMM16S (insn), 8))
  7141. {
  7142. insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
  7143. insn_type = NDS32_INSN_BEQZS8;
  7144. }
  7145. break;
  7146. case N32_BR2_BNEZ:
  7147. if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
  7148. {
  7149. insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
  7150. insn_type = NDS32_INSN_BNEZ38;
  7151. }
  7152. else if (N32_RT5 (insn) == REG_R15
  7153. && IS_WITHIN_S (N32_IMM16S (insn), 8))
  7154. {
  7155. insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
  7156. insn_type = NDS32_INSN_BNEZS8;
  7157. }
  7158. break;
  7159. case N32_BR2_SOP0:
  7160. if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
  7161. {
  7162. insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
  7163. insn_type = NDS32_INSN_IFCALL9;
  7164. }
  7165. break;
  7166. }
  7167. break;
  7168. case N32_OP6_JI:
  7169. if ((insn & N32_BIT (24)) == 0)
  7170. {
  7171. /* N32_JI_J */
  7172. if (IS_WITHIN_S (N32_IMM24S (insn), 8))
  7173. {
  7174. insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
  7175. insn_type = NDS32_INSN_J8;
  7176. }
  7177. }
  7178. break;
  7179. case N32_OP6_JREG:
  7180. if (__GF (insn, 8, 2) != 0)
  7181. goto done;
  7182. switch (N32_IMMU (insn, 5))
  7183. {
  7184. case N32_JREG_JR:
  7185. if (N32_JREG_HINT (insn) == 0)
  7186. {
  7187. /* jr */
  7188. insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
  7189. insn_type = NDS32_INSN_JR5;
  7190. }
  7191. else if (N32_JREG_HINT (insn) == 1)
  7192. {
  7193. /* ret */
  7194. insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
  7195. insn_type = NDS32_INSN_RET5;
  7196. }
  7197. else if (N32_JREG_HINT (insn) == 3)
  7198. {
  7199. /* ifret = mov55 $sp, $sp */
  7200. insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
  7201. insn_type = NDS32_INSN_IFRET;
  7202. }
  7203. break;
  7204. case N32_JREG_JRAL:
  7205. /* It's convertible when return rt5 is $lp and address
  7206. translation is kept. */
  7207. if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
  7208. {
  7209. insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
  7210. insn_type = NDS32_INSN_JRAL5;
  7211. }
  7212. break;
  7213. }
  7214. break;
  7215. case N32_OP6_MISC:
  7216. if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
  7217. {
  7218. /* For v3, swid above 31 are used for ex9.it. */
  7219. insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
  7220. insn_type = NDS32_INSN_BREAK16;
  7221. }
  7222. break;
  7223. default:
  7224. /* This instruction has no 16-bit variant. */
  7225. goto done;
  7226. }
  7227. done:
  7228. /* Bit-15 of insn16 should be set for a valid instruction. */
  7229. if ((insn16 & 0x8000) == 0)
  7230. return 0;
  7231. if (pinsn16)
  7232. *pinsn16 = insn16;
  7233. if (pinsn_type)
  7234. *pinsn_type = insn_type;
  7235. return 1;
  7236. }
  7237. static int
  7238. special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
  7239. Elf_Internal_Rela *reloc)
  7240. {
  7241. uint16_t insn16 = 0;
  7242. if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
  7243. || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
  7244. return 0;
  7245. if (!N32_IS_RT3 (insn))
  7246. return 0;
  7247. switch (N32_OP6 (insn))
  7248. {
  7249. case N32_OP6_LWI:
  7250. if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
  7251. insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
  7252. break;
  7253. case N32_OP6_SWI:
  7254. if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
  7255. insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
  7256. break;
  7257. case N32_OP6_HWGP:
  7258. if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
  7259. break;
  7260. if (__GF (insn, 17, 3) == 6)
  7261. insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
  7262. else if (__GF (insn, 17, 3) == 7)
  7263. insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
  7264. break;
  7265. }
  7266. if ((insn16 & 0x8000) == 0)
  7267. return 0;
  7268. *pinsn16 = insn16;
  7269. return 1;
  7270. }
  7271. /* Convert a 16-bit instruction to 32-bit one.
  7272. INSN16 it the input and PINSN it the point to output.
  7273. Return non-zero on successful. Otherwise 0 is returned. */
  7274. int
  7275. nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
  7276. {
  7277. uint32_t insn = 0xffffffff;
  7278. unsigned long mach = bfd_get_mach (abfd);
  7279. /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
  7280. switch (__GF (insn16, 9, 6))
  7281. {
  7282. case 0x4: /* add45 */
  7283. insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
  7284. N16_RA5 (insn16));
  7285. goto done;
  7286. case 0x5: /* sub45 */
  7287. insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
  7288. N16_RA5 (insn16));
  7289. goto done;
  7290. case 0x6: /* addi45 */
  7291. insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
  7292. N16_IMM5U (insn16));
  7293. goto done;
  7294. case 0x7: /* subi45 */
  7295. insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
  7296. -N16_IMM5U (insn16));
  7297. goto done;
  7298. case 0x8: /* srai45 */
  7299. insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
  7300. N16_IMM5U (insn16));
  7301. goto done;
  7302. case 0x9: /* srli45 */
  7303. insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
  7304. N16_IMM5U (insn16));
  7305. goto done;
  7306. case 0xa: /* slli333 */
  7307. insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
  7308. N16_IMM3U (insn16));
  7309. goto done;
  7310. case 0xc: /* add333 */
  7311. insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
  7312. N16_RB3 (insn16));
  7313. goto done;
  7314. case 0xd: /* sub333 */
  7315. insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
  7316. N16_RB3 (insn16));
  7317. goto done;
  7318. case 0xe: /* addi333 */
  7319. insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
  7320. N16_IMM3U (insn16));
  7321. goto done;
  7322. case 0xf: /* subi333 */
  7323. insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
  7324. -N16_IMM3U (insn16));
  7325. goto done;
  7326. case 0x10: /* lwi333 */
  7327. insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
  7328. N16_IMM3U (insn16));
  7329. goto done;
  7330. case 0x12: /* lhi333 */
  7331. insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
  7332. N16_IMM3U (insn16));
  7333. goto done;
  7334. case 0x13: /* lbi333 */
  7335. insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
  7336. N16_IMM3U (insn16));
  7337. goto done;
  7338. case 0x11: /* lwi333.bi */
  7339. insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
  7340. N16_IMM3U (insn16));
  7341. goto done;
  7342. case 0x14: /* swi333 */
  7343. insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
  7344. N16_IMM3U (insn16));
  7345. goto done;
  7346. case 0x16: /* shi333 */
  7347. insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
  7348. N16_IMM3U (insn16));
  7349. goto done;
  7350. case 0x17: /* sbi333 */
  7351. insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
  7352. N16_IMM3U (insn16));
  7353. goto done;
  7354. case 0x15: /* swi333.bi */
  7355. insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
  7356. N16_IMM3U (insn16));
  7357. goto done;
  7358. case 0x18: /* addri36.sp */
  7359. insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
  7360. N16_IMM6U (insn16) << 2);
  7361. goto done;
  7362. case 0x19: /* lwi45.fe */
  7363. insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
  7364. (N16_IMM5U (insn16) - 32));
  7365. goto done;
  7366. case 0x1a: /* lwi450 */
  7367. insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
  7368. goto done;
  7369. case 0x1b: /* swi450 */
  7370. insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
  7371. goto done;
  7372. /* These are r15 implied instructions. */
  7373. case 0x30: /* slts45 */
  7374. insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
  7375. goto done;
  7376. case 0x31: /* slt45 */
  7377. insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
  7378. goto done;
  7379. case 0x32: /* sltsi45 */
  7380. insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
  7381. goto done;
  7382. case 0x33: /* slti45 */
  7383. insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
  7384. goto done;
  7385. case 0x34: /* beqzs8, bnezs8 */
  7386. if (insn16 & N32_BIT (8))
  7387. insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
  7388. else
  7389. insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
  7390. goto done;
  7391. case 0x35: /* break16, ex9.it */
  7392. /* Only consider range of v3 break16. */
  7393. insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
  7394. goto done;
  7395. case 0x3c: /* ifcall9 */
  7396. insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
  7397. goto done;
  7398. case 0x3d: /* movpi45 */
  7399. insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
  7400. goto done;
  7401. case 0x3f: /* MISC33 */
  7402. switch (insn16 & 0x7)
  7403. {
  7404. case 2: /* neg33 */
  7405. insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
  7406. break;
  7407. case 3: /* not33 */
  7408. insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
  7409. N16_RA3 (insn16));
  7410. break;
  7411. case 4: /* mul33 */
  7412. insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
  7413. N16_RA3 (insn16));
  7414. break;
  7415. case 5: /* xor33 */
  7416. insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
  7417. N16_RA3 (insn16));
  7418. break;
  7419. case 6: /* and33 */
  7420. insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
  7421. N16_RA3 (insn16));
  7422. break;
  7423. case 7: /* or33 */
  7424. insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
  7425. N16_RA3 (insn16));
  7426. break;
  7427. }
  7428. goto done;
  7429. case 0xb:
  7430. switch (insn16 & 0x7)
  7431. {
  7432. case 0: /* zeb33 */
  7433. insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
  7434. break;
  7435. case 1: /* zeh33 */
  7436. insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
  7437. break;
  7438. case 2: /* seb33 */
  7439. insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
  7440. break;
  7441. case 3: /* seh33 */
  7442. insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
  7443. break;
  7444. case 4: /* xlsb33 */
  7445. insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
  7446. break;
  7447. case 5: /* x11b33 */
  7448. insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
  7449. break;
  7450. case 6: /* bmski33 */
  7451. insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
  7452. 1 << __GF (insn16, 3, 3));
  7453. break;
  7454. case 7: /* fexti33 */
  7455. insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
  7456. (1 << (__GF (insn16, 3, 3) + 1)) - 1);
  7457. break;
  7458. }
  7459. goto done;
  7460. }
  7461. switch (__GF (insn16, 10, 5))
  7462. {
  7463. case 0x0: /* mov55 or ifret16 */
  7464. if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
  7465. && N16_RT5 (insn16) == N16_RA5 (insn16))
  7466. insn = N32_JREG (JR, 0, 0, 0, 3);
  7467. else
  7468. insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
  7469. goto done;
  7470. case 0x1: /* movi55 */
  7471. insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
  7472. goto done;
  7473. case 0x1b: /* addi10s (V2) */
  7474. insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
  7475. goto done;
  7476. }
  7477. switch (__GF (insn16, 11, 4))
  7478. {
  7479. case 0x7: /* lwi37.fp/swi37.fp */
  7480. if (insn16 & N32_BIT (7)) /* swi37.fp */
  7481. insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
  7482. else /* lwi37.fp */
  7483. insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
  7484. goto done;
  7485. case 0x8: /* beqz38 */
  7486. insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
  7487. goto done;
  7488. case 0x9: /* bnez38 */
  7489. insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
  7490. goto done;
  7491. case 0xa: /* beqs38/j8, implied r5 */
  7492. if (N16_RT38 (insn16) == 5)
  7493. insn = N32_JI (J, N16_IMM8S (insn16));
  7494. else
  7495. insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
  7496. goto done;
  7497. case 0xb: /* bnes38 and others. */
  7498. if (N16_RT38 (insn16) == 5)
  7499. {
  7500. switch (__GF (insn16, 5, 3))
  7501. {
  7502. case 0: /* jr5 */
  7503. insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
  7504. break;
  7505. case 4: /* ret5 */
  7506. insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
  7507. break;
  7508. case 1: /* jral5 */
  7509. insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
  7510. break;
  7511. case 2: /* ex9.it imm5 */
  7512. /* ex9.it had no 32-bit variantl. */
  7513. break;
  7514. case 5: /* add5.pc */
  7515. /* add5.pc had no 32-bit variantl. */
  7516. break;
  7517. }
  7518. }
  7519. else /* bnes38 */
  7520. insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
  7521. goto done;
  7522. case 0xe: /* lwi37/swi37 */
  7523. if (insn16 & (1 << 7)) /* swi37.sp */
  7524. insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
  7525. else /* lwi37.sp */
  7526. insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
  7527. goto done;
  7528. }
  7529. done:
  7530. if (insn & 0x80000000)
  7531. return 0;
  7532. if (pinsn)
  7533. *pinsn = insn;
  7534. return 1;
  7535. }
  7536. static bool
  7537. is_sda_access_insn (unsigned long insn)
  7538. {
  7539. switch (N32_OP6 (insn))
  7540. {
  7541. case N32_OP6_LWI:
  7542. case N32_OP6_LHI:
  7543. case N32_OP6_LHSI:
  7544. case N32_OP6_LBI:
  7545. case N32_OP6_LBSI:
  7546. case N32_OP6_SWI:
  7547. case N32_OP6_SHI:
  7548. case N32_OP6_SBI:
  7549. case N32_OP6_LWC:
  7550. case N32_OP6_LDC:
  7551. case N32_OP6_SWC:
  7552. case N32_OP6_SDC:
  7553. return true;
  7554. default:
  7555. ;
  7556. }
  7557. return false;
  7558. }
  7559. static unsigned long
  7560. turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
  7561. {
  7562. uint32_t oinsn = 0;
  7563. switch (type)
  7564. {
  7565. case R_NDS32_GOT_LO12:
  7566. case R_NDS32_GOTOFF_LO12:
  7567. case R_NDS32_PLTREL_LO12:
  7568. case R_NDS32_PLT_GOTREL_LO12:
  7569. case R_NDS32_LO12S0_RELA:
  7570. switch (N32_OP6 (insn))
  7571. {
  7572. case N32_OP6_LBI:
  7573. /* lbi.gp */
  7574. oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
  7575. break;
  7576. case N32_OP6_LBSI:
  7577. /* lbsi.gp */
  7578. oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
  7579. break;
  7580. case N32_OP6_SBI:
  7581. /* sbi.gp */
  7582. oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
  7583. break;
  7584. case N32_OP6_ORI:
  7585. /* addi.gp */
  7586. oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
  7587. break;
  7588. }
  7589. break;
  7590. case R_NDS32_LO12S1_RELA:
  7591. switch (N32_OP6 (insn))
  7592. {
  7593. case N32_OP6_LHI:
  7594. /* lhi.gp */
  7595. oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
  7596. break;
  7597. case N32_OP6_LHSI:
  7598. /* lhsi.gp */
  7599. oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
  7600. break;
  7601. case N32_OP6_SHI:
  7602. /* shi.gp */
  7603. oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
  7604. break;
  7605. }
  7606. break;
  7607. case R_NDS32_LO12S2_RELA:
  7608. switch (N32_OP6 (insn))
  7609. {
  7610. case N32_OP6_LWI:
  7611. /* lwi.gp */
  7612. oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
  7613. break;
  7614. case N32_OP6_SWI:
  7615. /* swi.gp */
  7616. oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
  7617. break;
  7618. }
  7619. break;
  7620. case R_NDS32_LO12S2_DP_RELA:
  7621. case R_NDS32_LO12S2_SP_RELA:
  7622. oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
  7623. break;
  7624. }
  7625. if (oinsn)
  7626. *pinsn = oinsn;
  7627. return oinsn != 0;
  7628. }
  7629. /* Linker hasn't found the correct merge section for non-section symbol
  7630. in relax time, this work is left to the function elf_link_input_bfd().
  7631. So for non-section symbol, _bfd_merged_section_offset is also needed
  7632. to find the correct symbol address. */
  7633. static bfd_vma
  7634. nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
  7635. asection **psec, Elf_Internal_Rela *rel)
  7636. {
  7637. asection *sec = *psec;
  7638. bfd_vma relocation;
  7639. relocation = (sec->output_section->vma
  7640. + sec->output_offset + sym->st_value);
  7641. if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
  7642. {
  7643. if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  7644. rel->r_addend =
  7645. _bfd_merged_section_offset (abfd, psec,
  7646. elf_section_data (sec)->sec_info,
  7647. sym->st_value + rel->r_addend);
  7648. else
  7649. rel->r_addend =
  7650. _bfd_merged_section_offset (abfd, psec,
  7651. elf_section_data (sec)->sec_info,
  7652. sym->st_value) + rel->r_addend;
  7653. if (sec != *psec)
  7654. {
  7655. /* If we have changed the section, and our original section is
  7656. marked with SEC_EXCLUDE, it means that the original
  7657. SEC_MERGE section has been completely subsumed in some
  7658. other SEC_MERGE section. In this case, we need to leave
  7659. some info around for --emit-relocs. */
  7660. if ((sec->flags & SEC_EXCLUDE) != 0)
  7661. sec->kept_section = *psec;
  7662. sec = *psec;
  7663. }
  7664. rel->r_addend -= relocation;
  7665. rel->r_addend += sec->output_section->vma + sec->output_offset;
  7666. }
  7667. return relocation;
  7668. }
  7669. static bfd_vma
  7670. calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
  7671. Elf_Internal_Sym *isymbuf,
  7672. Elf_Internal_Shdr *symtab_hdr)
  7673. {
  7674. bfd_signed_vma foff;
  7675. bfd_vma symval, addend;
  7676. Elf_Internal_Rela irel_fn;
  7677. Elf_Internal_Sym *isym;
  7678. asection *sym_sec;
  7679. /* Get the value of the symbol referred to by the reloc. */
  7680. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  7681. {
  7682. /* A local symbol. */
  7683. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  7684. if (isym->st_shndx == SHN_UNDEF)
  7685. sym_sec = bfd_und_section_ptr;
  7686. else if (isym->st_shndx == SHN_ABS)
  7687. sym_sec = bfd_abs_section_ptr;
  7688. else if (isym->st_shndx == SHN_COMMON)
  7689. sym_sec = bfd_com_section_ptr;
  7690. else
  7691. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  7692. memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
  7693. symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
  7694. addend = irel_fn.r_addend;
  7695. }
  7696. else
  7697. {
  7698. unsigned long indx;
  7699. struct elf_link_hash_entry *h;
  7700. /* An external symbol. */
  7701. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  7702. h = elf_sym_hashes (abfd)[indx];
  7703. BFD_ASSERT (h != NULL);
  7704. while (h->root.type == bfd_link_hash_indirect
  7705. || h->root.type == bfd_link_hash_warning)
  7706. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  7707. if (h->root.type != bfd_link_hash_defined
  7708. && h->root.type != bfd_link_hash_defweak)
  7709. /* This appears to be a reference to an undefined
  7710. symbol. Just ignore it--it will be caught by the
  7711. regular reloc processing. */
  7712. return 0;
  7713. if (h->root.u.def.section->flags & SEC_MERGE)
  7714. {
  7715. sym_sec = h->root.u.def.section;
  7716. symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
  7717. (sym_sec)->sec_info, h->root.u.def.value);
  7718. symval = symval + sym_sec->output_section->vma
  7719. + sym_sec->output_offset;
  7720. }
  7721. else
  7722. symval = (h->root.u.def.value
  7723. + h->root.u.def.section->output_section->vma
  7724. + h->root.u.def.section->output_offset);
  7725. addend = irel->r_addend;
  7726. }
  7727. foff = symval + addend;
  7728. return foff;
  7729. }
  7730. static int
  7731. is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
  7732. asection *sec, Elf_Internal_Rela *rel)
  7733. {
  7734. bfd_byte *contents;
  7735. unsigned short insn16;
  7736. if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
  7737. return false;
  7738. contents = elf_section_data (sec)->this_hdr.contents;
  7739. insn16 = bfd_getb16 (contents + rel->r_offset);
  7740. if (insn16 == NDS32_NOP16)
  7741. return true;
  7742. return false;
  7743. }
  7744. /* It checks whether the instruction could be converted to
  7745. 16-bit form and returns the converted one.
  7746. `internal_relocs' is supposed to be sorted. */
  7747. static int
  7748. is_convert_32_to_16 (bfd *abfd, asection *sec,
  7749. Elf_Internal_Rela *reloc,
  7750. Elf_Internal_Rela *internal_relocs,
  7751. Elf_Internal_Rela *irelend,
  7752. uint16_t *insn16)
  7753. {
  7754. #define NORMAL_32_TO_16 (1 << 0)
  7755. #define SPECIAL_32_TO_16 (1 << 1)
  7756. bfd_byte *contents = NULL;
  7757. bfd_signed_vma off;
  7758. bfd_vma mem_addr;
  7759. uint32_t insn = 0;
  7760. Elf_Internal_Rela *pc_rel;
  7761. Elf_Internal_Shdr *symtab_hdr;
  7762. Elf_Internal_Sym *isymbuf = NULL;
  7763. int convert_type;
  7764. bfd_vma offset;
  7765. if (reloc->r_offset + 4 > sec->size)
  7766. return false;
  7767. offset = reloc->r_offset;
  7768. if (!nds32_get_section_contents (abfd, sec, &contents, true))
  7769. return false;
  7770. insn = bfd_getb32 (contents + offset);
  7771. if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
  7772. convert_type = NORMAL_32_TO_16;
  7773. else if (special_convert_32_to_16 (insn, insn16, reloc))
  7774. convert_type = SPECIAL_32_TO_16;
  7775. else
  7776. return false;
  7777. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  7778. if (!nds32_get_local_syms (abfd, sec, &isymbuf))
  7779. return false;
  7780. /* Find the first relocation of the same relocation-type,
  7781. so we iteratie them forward. */
  7782. pc_rel = reloc;
  7783. while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
  7784. pc_rel--;
  7785. for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
  7786. {
  7787. if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
  7788. || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
  7789. || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
  7790. || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
  7791. {
  7792. off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
  7793. if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
  7794. || off == 0)
  7795. return false;
  7796. break;
  7797. }
  7798. else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
  7799. {
  7800. /* movi => movi55 */
  7801. mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
  7802. symtab_hdr);
  7803. /* mem_addr is unsigned, but the value should
  7804. be between [-16, 15]. */
  7805. if ((mem_addr + 0x10) >> 5)
  7806. return false;
  7807. break;
  7808. }
  7809. else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
  7810. || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
  7811. {
  7812. /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
  7813. because it can be relaxed to addi for TLS_LE_ADD. */
  7814. return false;
  7815. }
  7816. else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
  7817. || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
  7818. && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
  7819. && convert_type == SPECIAL_32_TO_16)
  7820. {
  7821. /* fp-as-gp
  7822. We've selected a best fp-base for this access, so we can
  7823. always resolve it anyway. Do nothing. */
  7824. break;
  7825. }
  7826. else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
  7827. && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
  7828. || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
  7829. && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
  7830. || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
  7831. && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
  7832. {
  7833. /* Prevent unresolved addi instruction translate
  7834. to addi45 or addi333. */
  7835. return false;
  7836. }
  7837. else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
  7838. {
  7839. off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
  7840. if (off >= ACCURATE_U9BIT_S1 || off <= 0)
  7841. return false;
  7842. break;
  7843. }
  7844. }
  7845. return true;
  7846. }
  7847. static void
  7848. nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
  7849. Elf_Internal_Rela *reloc,
  7850. Elf_Internal_Rela *internal_relocs,
  7851. Elf_Internal_Rela *irelend,
  7852. unsigned short insn16)
  7853. {
  7854. Elf_Internal_Rela *pc_rel;
  7855. bfd_vma offset;
  7856. offset = reloc->r_offset;
  7857. bfd_putb16 (insn16, contents + offset);
  7858. /* Find the first relocation of the same relocation-type,
  7859. so we iteratie them forward. */
  7860. pc_rel = reloc;
  7861. while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
  7862. pc_rel--;
  7863. for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
  7864. {
  7865. if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
  7866. || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
  7867. || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
  7868. {
  7869. pc_rel->r_info =
  7870. ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
  7871. }
  7872. else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
  7873. pc_rel->r_info =
  7874. ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
  7875. else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
  7876. pc_rel->r_info =
  7877. ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
  7878. else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
  7879. || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
  7880. pc_rel->r_info =
  7881. ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
  7882. else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
  7883. pc_rel->r_info =
  7884. ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
  7885. }
  7886. }
  7887. /* Find a relocation of type specified by `reloc_type'
  7888. of the same r_offset with reloc.
  7889. If not found, return irelend.
  7890. Assuming relocations are sorted by r_offset,
  7891. we find the relocation from `reloc' backward untill relocs,
  7892. or find it from `reloc' forward untill irelend. */
  7893. static Elf_Internal_Rela *
  7894. find_relocs_at_address (Elf_Internal_Rela *reloc,
  7895. Elf_Internal_Rela *relocs,
  7896. Elf_Internal_Rela *irelend,
  7897. enum elf_nds32_reloc_type reloc_type)
  7898. {
  7899. Elf_Internal_Rela *rel_t;
  7900. /* Find backward. */
  7901. for (rel_t = reloc;
  7902. rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
  7903. rel_t--)
  7904. if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
  7905. return rel_t;
  7906. /* We didn't find it backward. Try find it forward. */
  7907. for (rel_t = reloc;
  7908. rel_t < irelend && rel_t->r_offset == reloc->r_offset;
  7909. rel_t++)
  7910. if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
  7911. return rel_t;
  7912. return irelend;
  7913. }
  7914. /* Find a relocation of specified type and offset.
  7915. `reloc' is just a refence point to find a relocation at specified offset.
  7916. If not found, return irelend.
  7917. Assuming relocations are sorted by r_offset,
  7918. we find the relocation from `reloc' backward untill relocs,
  7919. or find it from `reloc' forward untill irelend. */
  7920. static Elf_Internal_Rela *
  7921. find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
  7922. Elf_Internal_Rela *relocs,
  7923. Elf_Internal_Rela *irelend,
  7924. enum elf_nds32_reloc_type reloc_type,
  7925. bfd_vma offset_p)
  7926. {
  7927. Elf_Internal_Rela *rel_t = NULL;
  7928. /* First, we try to find a relocation of offset `offset_p',
  7929. and then we use find_relocs_at_address to find specific type. */
  7930. if (reloc->r_offset > offset_p)
  7931. {
  7932. /* Find backward. */
  7933. for (rel_t = reloc;
  7934. rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
  7935. /* Do nothing. */;
  7936. }
  7937. else if (reloc->r_offset < offset_p)
  7938. {
  7939. /* Find forward. */
  7940. for (rel_t = reloc;
  7941. rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
  7942. /* Do nothing. */;
  7943. }
  7944. else
  7945. rel_t = reloc;
  7946. /* Not found? */
  7947. if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
  7948. return irelend;
  7949. return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
  7950. }
  7951. typedef struct nds32_elf_blank nds32_elf_blank_t;
  7952. struct nds32_elf_blank
  7953. {
  7954. /* Where the blank begins. */
  7955. bfd_vma offset;
  7956. /* The size of the blank. */
  7957. bfd_vma size;
  7958. /* The accumulative size before this blank. */
  7959. bfd_vma total_size;
  7960. nds32_elf_blank_t *next;
  7961. nds32_elf_blank_t *prev;
  7962. };
  7963. static nds32_elf_blank_t *blank_free_list = NULL;
  7964. static nds32_elf_blank_t *
  7965. create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
  7966. {
  7967. nds32_elf_blank_t *blank_t;
  7968. if (blank_free_list)
  7969. {
  7970. blank_t = blank_free_list;
  7971. blank_free_list = blank_free_list->next;
  7972. }
  7973. else
  7974. blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
  7975. if (blank_t == NULL)
  7976. return NULL;
  7977. blank_t->offset = offset_p;
  7978. blank_t->size = size_p;
  7979. blank_t->total_size = 0;
  7980. blank_t->next = NULL;
  7981. blank_t->prev = NULL;
  7982. return blank_t;
  7983. }
  7984. static void
  7985. remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
  7986. {
  7987. if (blank_free_list)
  7988. {
  7989. blank_free_list->prev = blank_p;
  7990. blank_p->next = blank_free_list;
  7991. }
  7992. else
  7993. blank_p->next = NULL;
  7994. blank_p->prev = NULL;
  7995. blank_free_list = blank_p;
  7996. }
  7997. static void
  7998. clean_nds32_elf_blank (void)
  7999. {
  8000. nds32_elf_blank_t *blank_t;
  8001. while (blank_free_list)
  8002. {
  8003. blank_t = blank_free_list;
  8004. blank_free_list = blank_free_list->next;
  8005. free (blank_t);
  8006. }
  8007. }
  8008. static nds32_elf_blank_t *
  8009. search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
  8010. {
  8011. nds32_elf_blank_t *blank_t;
  8012. if (!blank_p)
  8013. return NULL;
  8014. blank_t = blank_p;
  8015. while (blank_t && addr < blank_t->offset)
  8016. blank_t = blank_t->prev;
  8017. while (blank_t && blank_t->next && addr >= blank_t->next->offset)
  8018. blank_t = blank_t->next;
  8019. return blank_t;
  8020. }
  8021. static bfd_vma
  8022. get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
  8023. int overwrite)
  8024. {
  8025. nds32_elf_blank_t *blank_t;
  8026. blank_t = search_nds32_elf_blank (*blank_p, addr);
  8027. if (!blank_t)
  8028. return 0;
  8029. if (overwrite)
  8030. *blank_p = blank_t;
  8031. if (addr < blank_t->offset + blank_t->size)
  8032. return blank_t->total_size + (addr - blank_t->offset);
  8033. else
  8034. return blank_t->total_size + blank_t->size;
  8035. }
  8036. static bool
  8037. insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
  8038. {
  8039. nds32_elf_blank_t *blank_t, *blank_t2;
  8040. if (!*blank_p)
  8041. {
  8042. *blank_p = create_nds32_elf_blank (addr, len);
  8043. return *blank_p != NULL;
  8044. }
  8045. blank_t = search_nds32_elf_blank (*blank_p, addr);
  8046. if (blank_t == NULL)
  8047. {
  8048. blank_t = create_nds32_elf_blank (addr, len);
  8049. if (!blank_t)
  8050. return false;
  8051. while ((*blank_p)->prev != NULL)
  8052. *blank_p = (*blank_p)->prev;
  8053. blank_t->next = *blank_p;
  8054. (*blank_p)->prev = blank_t;
  8055. (*blank_p) = blank_t;
  8056. return true;
  8057. }
  8058. if (addr < blank_t->offset + blank_t->size)
  8059. {
  8060. /* Extend the origin blank. */
  8061. if (addr + len > blank_t->offset + blank_t->size)
  8062. blank_t->size = addr + len - blank_t->offset;
  8063. }
  8064. else
  8065. {
  8066. blank_t2 = create_nds32_elf_blank (addr, len);
  8067. if (!blank_t2)
  8068. return false;
  8069. if (blank_t->next)
  8070. {
  8071. blank_t->next->prev = blank_t2;
  8072. blank_t2->next = blank_t->next;
  8073. }
  8074. blank_t2->prev = blank_t;
  8075. blank_t->next = blank_t2;
  8076. *blank_p = blank_t2;
  8077. }
  8078. return true;
  8079. }
  8080. static bool
  8081. insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
  8082. bfd_vma len)
  8083. {
  8084. nds32_elf_blank_t *blank_t;
  8085. if (!insert_nds32_elf_blank (blank_p, addr, len))
  8086. return false;
  8087. blank_t = *blank_p;
  8088. if (!blank_t->prev)
  8089. {
  8090. blank_t->total_size = 0;
  8091. blank_t = blank_t->next;
  8092. }
  8093. while (blank_t)
  8094. {
  8095. blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
  8096. blank_t = blank_t->next;
  8097. }
  8098. return true;
  8099. }
  8100. static void
  8101. calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
  8102. {
  8103. nds32_elf_blank_t *blank_t;
  8104. bfd_vma total_size = 0;
  8105. if (!blank_p)
  8106. return;
  8107. blank_t = blank_p;
  8108. while (blank_t->prev)
  8109. blank_t = blank_t->prev;
  8110. while (blank_t)
  8111. {
  8112. blank_t->total_size = total_size;
  8113. total_size += blank_t->size;
  8114. blank_t = blank_t->next;
  8115. }
  8116. }
  8117. static bool
  8118. nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
  8119. nds32_elf_blank_t *blank_p)
  8120. {
  8121. Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
  8122. Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
  8123. Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
  8124. unsigned int sec_shndx; /* The section the be relaxed. */
  8125. bfd_byte *contents; /* Contents data of iterating section. */
  8126. Elf_Internal_Rela *internal_relocs;
  8127. Elf_Internal_Rela *irel;
  8128. Elf_Internal_Rela *irelend;
  8129. struct elf_link_hash_entry **sym_hashes;
  8130. struct elf_link_hash_entry **end_hashes;
  8131. unsigned int symcount;
  8132. asection *sect;
  8133. nds32_elf_blank_t *blank_t;
  8134. nds32_elf_blank_t *blank_t2;
  8135. nds32_elf_blank_t *blank_head;
  8136. blank_head = blank_t = blank_p;
  8137. while (blank_head->prev != NULL)
  8138. blank_head = blank_head->prev;
  8139. while (blank_t->next != NULL)
  8140. blank_t = blank_t->next;
  8141. if (blank_t->offset + blank_t->size <= sec->size)
  8142. {
  8143. blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
  8144. blank_t->next->prev = blank_t;
  8145. }
  8146. if (blank_head->offset > 0)
  8147. {
  8148. blank_head->prev = create_nds32_elf_blank (0, 0);
  8149. blank_head->prev->next = blank_head;
  8150. blank_head = blank_head->prev;
  8151. }
  8152. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  8153. /* The deletion must stop at the next ALIGN reloc for an alignment
  8154. power larger than the number of bytes we are deleting. */
  8155. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  8156. if (!nds32_get_local_syms (abfd, sec, &isym))
  8157. return false;
  8158. if (isym == NULL)
  8159. {
  8160. isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  8161. symtab_hdr->sh_info, 0, NULL, NULL, NULL);
  8162. symtab_hdr->contents = (bfd_byte *) isym;
  8163. }
  8164. if (isym == NULL || symtab_hdr->sh_info == 0)
  8165. return false;
  8166. blank_t = blank_head;
  8167. calc_nds32_blank_total (blank_head);
  8168. for (sect = abfd->sections; sect != NULL; sect = sect->next)
  8169. {
  8170. /* Adjust all the relocs. */
  8171. /* Relocations MUST be kept in memory, because relaxation adjust them. */
  8172. internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
  8173. true /* keep_memory */);
  8174. irelend = internal_relocs + sect->reloc_count;
  8175. blank_t = blank_head;
  8176. blank_t2 = blank_head;
  8177. if (!(sect->flags & SEC_RELOC))
  8178. continue;
  8179. contents = NULL;
  8180. nds32_get_section_contents (abfd, sect, &contents, true);
  8181. for (irel = internal_relocs; irel < irelend; irel++)
  8182. {
  8183. bfd_vma raddr;
  8184. if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
  8185. && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
  8186. && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
  8187. {
  8188. unsigned long val = 0;
  8189. unsigned long mask;
  8190. long before, between;
  8191. long offset = 0;
  8192. switch (ELF32_R_TYPE (irel->r_info))
  8193. {
  8194. case R_NDS32_DIFF8:
  8195. offset = bfd_get_8 (abfd, contents + irel->r_offset);
  8196. break;
  8197. case R_NDS32_DIFF16:
  8198. offset = bfd_get_16 (abfd, contents + irel->r_offset);
  8199. break;
  8200. case R_NDS32_DIFF32:
  8201. val = bfd_get_32 (abfd, contents + irel->r_offset);
  8202. /* Get the signed bit and mask for the high part. The
  8203. gcc will alarm when right shift 32-bit since the
  8204. type size of long may be 32-bit. */
  8205. mask = 0 - (val >> 31);
  8206. if (mask)
  8207. offset = (val | (mask - 0xffffffff));
  8208. else
  8209. offset = val;
  8210. break;
  8211. default:
  8212. BFD_ASSERT (0);
  8213. }
  8214. /* DIFF value
  8215. 0 |encoded in location|
  8216. |------------|-------------------|---------
  8217. sym+off(addend)
  8218. -- before ---| *****************
  8219. --------------------- between ---|
  8220. We only care how much data are relax between DIFF,
  8221. marked as ***. */
  8222. before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
  8223. between = get_nds32_elf_blank_total (&blank_t,
  8224. irel->r_addend + offset, 0);
  8225. if (between == before)
  8226. goto done_adjust_diff;
  8227. switch (ELF32_R_TYPE (irel->r_info))
  8228. {
  8229. case R_NDS32_DIFF8:
  8230. bfd_put_8 (abfd, offset - (between - before),
  8231. contents + irel->r_offset);
  8232. break;
  8233. case R_NDS32_DIFF16:
  8234. bfd_put_16 (abfd, offset - (between - before),
  8235. contents + irel->r_offset);
  8236. break;
  8237. case R_NDS32_DIFF32:
  8238. bfd_put_32 (abfd, offset - (between - before),
  8239. contents + irel->r_offset);
  8240. break;
  8241. }
  8242. }
  8243. else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
  8244. && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
  8245. {
  8246. bfd_vma val = 0;
  8247. unsigned int len = 0;
  8248. unsigned long before, between;
  8249. bfd_byte *endp, *p;
  8250. val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
  8251. &len);
  8252. before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
  8253. between = get_nds32_elf_blank_total (&blank_t,
  8254. irel->r_addend + val, 0);
  8255. if (between == before)
  8256. goto done_adjust_diff;
  8257. p = contents + irel->r_offset;
  8258. endp = p + len -1;
  8259. memset (p, 0x80, len);
  8260. *(endp) = 0;
  8261. p = write_uleb128 (p, val - (between - before)) - 1;
  8262. if (p < endp)
  8263. *p |= 0x80;
  8264. }
  8265. done_adjust_diff:
  8266. if (sec == sect)
  8267. {
  8268. raddr = irel->r_offset;
  8269. irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
  8270. irel->r_offset, 1);
  8271. if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
  8272. continue;
  8273. if (blank_t2 && blank_t2->next
  8274. && (blank_t2->offset > raddr
  8275. || blank_t2->next->offset <= raddr))
  8276. _bfd_error_handler
  8277. (_("%pB: error: search_nds32_elf_blank reports wrong node"),
  8278. abfd);
  8279. /* Mark reloc in deleted portion as NONE.
  8280. For some relocs like R_NDS32_LABEL that doesn't modify the
  8281. content in the section. R_NDS32_LABEL doesn't belong to the
  8282. instruction in the section, so we should preserve it. */
  8283. if (raddr >= blank_t2->offset
  8284. && raddr < blank_t2->offset + blank_t2->size
  8285. && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
  8286. && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
  8287. && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
  8288. && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
  8289. && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
  8290. && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
  8291. {
  8292. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  8293. R_NDS32_NONE);
  8294. continue;
  8295. }
  8296. }
  8297. if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
  8298. || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
  8299. || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
  8300. continue;
  8301. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
  8302. && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
  8303. && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
  8304. {
  8305. if (irel->r_addend <= sec->size)
  8306. irel->r_addend -=
  8307. get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
  8308. }
  8309. }
  8310. }
  8311. /* Adjust the local symbols defined in this section. */
  8312. blank_t = blank_head;
  8313. for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
  8314. {
  8315. if (isym->st_shndx == sec_shndx)
  8316. {
  8317. if (isym->st_value <= sec->size)
  8318. {
  8319. bfd_vma ahead;
  8320. bfd_vma orig_addr = isym->st_value;
  8321. ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
  8322. isym->st_value -= ahead;
  8323. /* Adjust function size. */
  8324. if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
  8325. && isym->st_size > 0)
  8326. isym->st_size -=
  8327. get_nds32_elf_blank_total
  8328. (&blank_t, orig_addr + isym->st_size, 0) - ahead;
  8329. }
  8330. }
  8331. }
  8332. /* Now adjust the global symbols defined in this section. */
  8333. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  8334. - symtab_hdr->sh_info);
  8335. sym_hashes = elf_sym_hashes (abfd);
  8336. end_hashes = sym_hashes + symcount;
  8337. blank_t = blank_head;
  8338. for (; sym_hashes < end_hashes; sym_hashes++)
  8339. {
  8340. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  8341. if ((sym_hash->root.type == bfd_link_hash_defined
  8342. || sym_hash->root.type == bfd_link_hash_defweak)
  8343. && sym_hash->root.u.def.section == sec)
  8344. {
  8345. if (sym_hash->root.u.def.value <= sec->size)
  8346. {
  8347. bfd_vma ahead;
  8348. bfd_vma orig_addr = sym_hash->root.u.def.value;
  8349. ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
  8350. sym_hash->root.u.def.value -= ahead;
  8351. /* Adjust function size. */
  8352. if (sym_hash->type == STT_FUNC)
  8353. sym_hash->size -=
  8354. get_nds32_elf_blank_total
  8355. (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
  8356. }
  8357. }
  8358. }
  8359. contents = elf_section_data (sec)->this_hdr.contents;
  8360. blank_t = blank_head;
  8361. while (blank_t->next)
  8362. {
  8363. /* Actually delete the bytes. */
  8364. /* If current blank is the last blank overlap with current section,
  8365. go to finish process. */
  8366. if (sec->size <= (blank_t->next->offset))
  8367. break;
  8368. memmove (contents + blank_t->offset - blank_t->total_size,
  8369. contents + blank_t->offset + blank_t->size,
  8370. blank_t->next->offset - (blank_t->offset + blank_t->size));
  8371. blank_t = blank_t->next;
  8372. }
  8373. if (sec->size > (blank_t->offset + blank_t->size))
  8374. {
  8375. /* There are remaining code between blank and section boundary.
  8376. Move the remaining code to appropriate location. */
  8377. memmove (contents + blank_t->offset - blank_t->total_size,
  8378. contents + blank_t->offset + blank_t->size,
  8379. sec->size - (blank_t->offset + blank_t->size));
  8380. sec->size -= blank_t->total_size + blank_t->size;
  8381. }
  8382. else
  8383. /* This blank is not entirely included in the section,
  8384. reduce the section size by only part of the blank size. */
  8385. sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
  8386. while (blank_head)
  8387. {
  8388. blank_t = blank_head;
  8389. blank_head = blank_head->next;
  8390. remove_nds32_elf_blank (blank_t);
  8391. }
  8392. return true;
  8393. }
  8394. /* Get the contents of a section. */
  8395. static int
  8396. nds32_get_section_contents (bfd *abfd, asection *sec,
  8397. bfd_byte **contents_p, bool cache)
  8398. {
  8399. /* Get the section contents. */
  8400. if (elf_section_data (sec)->this_hdr.contents != NULL)
  8401. *contents_p = elf_section_data (sec)->this_hdr.contents;
  8402. else
  8403. {
  8404. if (!bfd_get_full_section_contents (abfd, sec, contents_p))
  8405. return false;
  8406. if (cache)
  8407. elf_section_data (sec)->this_hdr.contents = *contents_p;
  8408. }
  8409. return true;
  8410. }
  8411. /* Get the contents of the internal symbol of abfd. */
  8412. static int
  8413. nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
  8414. Elf_Internal_Sym **isymbuf_p)
  8415. {
  8416. Elf_Internal_Shdr *symtab_hdr;
  8417. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  8418. /* Read this BFD's local symbols if we haven't done so already. */
  8419. if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
  8420. {
  8421. *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
  8422. if (*isymbuf_p == NULL)
  8423. {
  8424. *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  8425. symtab_hdr->sh_info, 0,
  8426. NULL, NULL, NULL);
  8427. if (*isymbuf_p == NULL)
  8428. return false;
  8429. }
  8430. }
  8431. symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
  8432. return true;
  8433. }
  8434. /* Range of small data. */
  8435. static bfd_vma sdata_range[2][2];
  8436. static bfd_vma const sdata_init_range[2] =
  8437. { ACCURATE_12BIT_S1, ACCURATE_19BIT };
  8438. static int
  8439. nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
  8440. bfd_byte *contents, bfd_vma addr)
  8441. {
  8442. unsigned long insn = bfd_getb32 (contents + addr);
  8443. if (insn & 0x80000000)
  8444. return 2;
  8445. return 4;
  8446. }
  8447. /* Set the gp relax range. We have to measure the safe range
  8448. to do gp relaxation. */
  8449. static void
  8450. relax_range_measurement (bfd *abfd, struct bfd_link_info *link_info)
  8451. {
  8452. asection *sec_f, *sec_b;
  8453. /* For upper bound. */
  8454. bfd_vma maxpgsz;
  8455. bfd_vma align;
  8456. static int decide_relax_range = 0;
  8457. int i;
  8458. int range_number = ARRAY_SIZE (sdata_init_range);
  8459. if (decide_relax_range)
  8460. return;
  8461. decide_relax_range = 1;
  8462. if (sda_rela_sec == NULL)
  8463. {
  8464. /* Since there is no data sections, we assume the range is page size. */
  8465. for (i = 0; i < range_number; i++)
  8466. {
  8467. sdata_range[i][0] = sdata_init_range[i] - 0x1000;
  8468. sdata_range[i][1] = sdata_init_range[i] - 0x1000;
  8469. }
  8470. return;
  8471. }
  8472. /* Get the biggest alignment power after the gp located section. */
  8473. sec_f = sda_rela_sec->output_section;
  8474. sec_b = sec_f->next;
  8475. align = 0;
  8476. while (sec_b != NULL)
  8477. {
  8478. if ((unsigned)(1 << sec_b->alignment_power) > align)
  8479. align = (1 << sec_b->alignment_power);
  8480. sec_b = sec_b->next;
  8481. }
  8482. if (link_info != NULL)
  8483. maxpgsz = link_info->maxpagesize;
  8484. else
  8485. maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
  8486. /* I guess we can not determine the section before
  8487. gp located section, so we assume the align is max page size. */
  8488. for (i = 0; i < range_number; i++)
  8489. {
  8490. sdata_range[i][1] = sdata_init_range[i] - align;
  8491. BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
  8492. sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
  8493. BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
  8494. }
  8495. }
  8496. /* These are macros used to check flags encoded in r_addend.
  8497. They are only used by nds32_elf_relax_section (). */
  8498. #define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
  8499. #define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
  8500. #define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
  8501. #define IS_16BIT_ON(addend) ((addend) & 0x20000000)
  8502. static const char * unrecognized_reloc_msg =
  8503. /* xgettext:c-format */
  8504. N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
  8505. /* Relax LONGCALL1 relocation for nds32_elf_relax_section. */
  8506. static bool
  8507. nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
  8508. Elf_Internal_Rela *internal_relocs, int *insn_len,
  8509. bfd_byte *contents, Elf_Internal_Sym *isymbuf,
  8510. Elf_Internal_Shdr *symtab_hdr)
  8511. {
  8512. /* There are 3 variations for LONGCALL1
  8513. case 4-4-2; 16-bit on, optimize off or optimize for space
  8514. sethi ta, hi20(symbol) ; LONGCALL1/HI20
  8515. ori ta, ta, lo12(symbol) ; LO12S0
  8516. jral5 ta ;
  8517. case 4-4-4; 16-bit off, optimize don't care
  8518. sethi ta, hi20(symbol) ; LONGCALL1/HI20
  8519. ori ta, ta, lo12(symbol) ; LO12S0
  8520. jral ta ;
  8521. case 4-4-4; 16-bit on, optimize for speed
  8522. sethi ta, hi20(symbol) ; LONGCALL1/HI20
  8523. ori ta, ta, lo12(symbol) ; LO12S0
  8524. jral ta ;
  8525. Check code for -mlong-calls output. */
  8526. /* Get the reloc for the address from which the register is
  8527. being loaded. This reloc will tell us which function is
  8528. actually being called. */
  8529. bfd_vma laddr;
  8530. int seq_len; /* Original length of instruction sequence. */
  8531. uint32_t insn;
  8532. Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
  8533. bfd_signed_vma foff;
  8534. uint16_t insn16;
  8535. irelend = internal_relocs + sec->reloc_count;
  8536. seq_len = GET_SEQ_LEN (irel->r_addend);
  8537. laddr = irel->r_offset;
  8538. *insn_len = seq_len;
  8539. hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  8540. R_NDS32_HI20_RELA, laddr);
  8541. lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  8542. R_NDS32_LO12S0_ORI_RELA,
  8543. laddr + 4);
  8544. if (hi_irelfn == irelend || lo_irelfn == irelend)
  8545. {
  8546. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
  8547. (uint64_t) irel->r_offset);
  8548. return false;
  8549. }
  8550. /* Get the value of the symbol referred to by the reloc. */
  8551. foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
  8552. /* This condition only happened when symbol is undefined. */
  8553. if (foff == 0
  8554. || foff < -CONSERVATIVE_24BIT_S1
  8555. || foff >= CONSERVATIVE_24BIT_S1)
  8556. return false;
  8557. /* Relax to: jal symbol; 25_PCREL. */
  8558. /* For simplicity of coding, we are going to modify the section
  8559. contents, the section relocs, and the BFD symbol table. We
  8560. must tell the rest of the code not to free up this
  8561. information. It would be possible to instead create a table
  8562. of changes which have to be made, as is done in coff-mips.c;
  8563. that would be more work, but would require less memory when
  8564. the linker is run. */
  8565. /* Replace the long call with a jal. */
  8566. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
  8567. R_NDS32_25_PCREL_RELA);
  8568. irel->r_addend = hi_irelfn->r_addend;
  8569. /* We don't resolve this here but resolve it in relocate_section. */
  8570. insn = INSN_JAL;
  8571. bfd_putb32 (insn, contents + irel->r_offset);
  8572. hi_irelfn->r_info =
  8573. ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
  8574. lo_irelfn->r_info =
  8575. ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
  8576. *insn_len = 4;
  8577. if (seq_len & 0x2)
  8578. {
  8579. insn16 = NDS32_NOP16;
  8580. bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
  8581. lo_irelfn->r_info =
  8582. ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
  8583. lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
  8584. *insn_len += 2;
  8585. }
  8586. return true;
  8587. }
  8588. #define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
  8589. /* Relax LONGCALL2 relocation for nds32_elf_relax_section. */
  8590. static bool
  8591. nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
  8592. Elf_Internal_Rela *internal_relocs, int *insn_len,
  8593. bfd_byte *contents, Elf_Internal_Sym *isymbuf,
  8594. Elf_Internal_Shdr *symtab_hdr)
  8595. {
  8596. /* bltz rt, .L1 ; LONGCALL2
  8597. jal symbol ; 25_PCREL
  8598. .L1: */
  8599. /* Get the reloc for the address from which the register is
  8600. being loaded. This reloc will tell us which function is
  8601. actually being called. */
  8602. bfd_vma laddr;
  8603. uint32_t insn;
  8604. Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
  8605. bfd_signed_vma foff;
  8606. irelend = internal_relocs + sec->reloc_count;
  8607. laddr = irel->r_offset;
  8608. i1_irelfn =
  8609. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  8610. R_NDS32_25_PCREL_RELA, laddr + 4);
  8611. if (i1_irelfn == irelend)
  8612. {
  8613. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
  8614. (uint64_t) irel->r_offset);
  8615. return false;
  8616. }
  8617. insn = bfd_getb32 (contents + laddr);
  8618. /* Get the value of the symbol referred to by the reloc. */
  8619. foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
  8620. if (foff == 0
  8621. || foff < -CONSERVATIVE_16BIT_S1
  8622. || foff >= CONSERVATIVE_16BIT_S1)
  8623. return false;
  8624. /* Relax to bgezal rt, label ; 17_PCREL
  8625. or bltzal rt, label ; 17_PCREL */
  8626. /* Convert to complimentary conditional call. */
  8627. insn = CONVERT_CONDITION_CALL (insn);
  8628. /* For simplicity of coding, we are going to modify the section
  8629. contents, the section relocs, and the BFD symbol table. We
  8630. must tell the rest of the code not to free up this
  8631. information. It would be possible to instead create a table
  8632. of changes which have to be made, as is done in coff-mips.c;
  8633. that would be more work, but would require less memory when
  8634. the linker is run. */
  8635. /* Clean unnessary relocations. */
  8636. i1_irelfn->r_info =
  8637. ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
  8638. cond_irelfn =
  8639. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  8640. R_NDS32_17_PCREL_RELA, laddr);
  8641. if (cond_irelfn != irelend)
  8642. cond_irelfn->r_info =
  8643. ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
  8644. /* Replace the long call with a bgezal. */
  8645. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
  8646. R_NDS32_17_PCREL_RELA);
  8647. irel->r_addend = i1_irelfn->r_addend;
  8648. bfd_putb32 (insn, contents + irel->r_offset);
  8649. *insn_len = 4;
  8650. return true;
  8651. }
  8652. /* Relax LONGCALL3 relocation for nds32_elf_relax_section. */
  8653. static bool
  8654. nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
  8655. Elf_Internal_Rela *internal_relocs, int *insn_len,
  8656. bfd_byte *contents, Elf_Internal_Sym *isymbuf,
  8657. Elf_Internal_Shdr *symtab_hdr)
  8658. {
  8659. /* There are 3 variations for LONGCALL3
  8660. case 4-4-4-2; 16-bit on, optimize off or optimize for space
  8661. bltz rt, $1 ; LONGCALL3
  8662. sethi ta, hi20(symbol) ; HI20
  8663. ori ta, ta, lo12(symbol) ; LO12S0
  8664. jral5 ta ;
  8665. $1
  8666. case 4-4-4-4; 16-bit off, optimize don't care
  8667. bltz rt, $1 ; LONGCALL3
  8668. sethi ta, hi20(symbol) ; HI20
  8669. ori ta, ta, lo12(symbol) ; LO12S0
  8670. jral ta ;
  8671. $1
  8672. case 4-4-4-4; 16-bit on, optimize for speed
  8673. bltz rt, $1 ; LONGCALL3
  8674. sethi ta, hi20(symbol) ; HI20
  8675. ori ta, ta, lo12(symbol) ; LO12S0
  8676. jral ta ;
  8677. $1 */
  8678. /* Get the reloc for the address from which the register is
  8679. being loaded. This reloc will tell us which function is
  8680. actually being called. */
  8681. bfd_vma laddr;
  8682. int seq_len; /* Original length of instruction sequence. */
  8683. uint32_t insn;
  8684. Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
  8685. bfd_signed_vma foff;
  8686. uint16_t insn16;
  8687. irelend = internal_relocs + sec->reloc_count;
  8688. seq_len = GET_SEQ_LEN (irel->r_addend);
  8689. laddr = irel->r_offset;
  8690. *insn_len = seq_len;
  8691. hi_irelfn =
  8692. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  8693. R_NDS32_HI20_RELA, laddr + 4);
  8694. lo_irelfn =
  8695. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  8696. R_NDS32_LO12S0_ORI_RELA, laddr + 8);
  8697. if (hi_irelfn == irelend || lo_irelfn == irelend)
  8698. {
  8699. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
  8700. (uint64_t) irel->r_offset);
  8701. return false;
  8702. }
  8703. /* Get the value of the symbol referred to by the reloc. */
  8704. foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
  8705. if (foff == 0
  8706. || foff < -CONSERVATIVE_24BIT_S1
  8707. || foff >= CONSERVATIVE_24BIT_S1)
  8708. return false;
  8709. insn = bfd_getb32 (contents + laddr);
  8710. if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
  8711. {
  8712. /* Relax to bgezal rt, label ; 17_PCREL
  8713. or bltzal rt, label ; 17_PCREL */
  8714. /* Convert to complimentary conditional call. */
  8715. insn = CONVERT_CONDITION_CALL (insn);
  8716. bfd_putb32 (insn, contents + irel->r_offset);
  8717. *insn_len = 4;
  8718. irel->r_info =
  8719. ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
  8720. hi_irelfn->r_info =
  8721. ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
  8722. lo_irelfn->r_info =
  8723. ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
  8724. cond_irelfn =
  8725. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  8726. R_NDS32_17_PCREL_RELA, laddr);
  8727. if (cond_irelfn != irelend)
  8728. {
  8729. cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
  8730. R_NDS32_17_PCREL_RELA);
  8731. cond_irelfn->r_addend = hi_irelfn->r_addend;
  8732. }
  8733. if (seq_len & 0x2)
  8734. {
  8735. insn16 = NDS32_NOP16;
  8736. bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
  8737. hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
  8738. R_NDS32_INSN16);
  8739. hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
  8740. insn_len += 2;
  8741. }
  8742. }
  8743. else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
  8744. {
  8745. /* Relax to the following instruction sequence
  8746. bltz rt, $1 ; LONGCALL2
  8747. jal symbol ; 25_PCREL
  8748. $1 */
  8749. *insn_len = 8;
  8750. insn = INSN_JAL;
  8751. bfd_putb32 (insn, contents + hi_irelfn->r_offset);
  8752. hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
  8753. R_NDS32_25_PCREL_RELA);
  8754. irel->r_info =
  8755. ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
  8756. lo_irelfn->r_info =
  8757. ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
  8758. if (seq_len & 0x2)
  8759. {
  8760. insn16 = NDS32_NOP16;
  8761. bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
  8762. lo_irelfn->r_info =
  8763. ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
  8764. lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
  8765. insn_len += 2;
  8766. }
  8767. }
  8768. return true;
  8769. }
  8770. /* Relax LONGJUMP1 relocation for nds32_elf_relax_section. */
  8771. static bool
  8772. nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
  8773. Elf_Internal_Rela *internal_relocs, int *insn_len,
  8774. bfd_byte *contents, Elf_Internal_Sym *isymbuf,
  8775. Elf_Internal_Shdr *symtab_hdr)
  8776. {
  8777. /* There are 3 variations for LONGJUMP1
  8778. case 4-4-2; 16-bit bit on, optimize off or optimize for space
  8779. sethi ta, hi20(symbol) ; LONGJUMP1/HI20
  8780. ori ta, ta, lo12(symbol) ; LO12S0
  8781. jr5 ta ;
  8782. case 4-4-4; 16-bit off, optimize don't care
  8783. sethi ta, hi20(symbol) ; LONGJUMP1/HI20
  8784. ori ta, ta, lo12(symbol) ; LO12S0
  8785. jr ta ;
  8786. case 4-4-4; 16-bit on, optimize for speed
  8787. sethi ta, hi20(symbol) ; LONGJUMP1/HI20
  8788. ori ta, ta, lo12(symbol) ; LO12S0
  8789. jr ta ; */
  8790. /* Get the reloc for the address from which the register is
  8791. being loaded. This reloc will tell us which function is
  8792. actually being called. */
  8793. bfd_vma laddr;
  8794. int seq_len; /* Original length of instruction sequence. */
  8795. int insn16_on; /* 16-bit on/off. */
  8796. uint32_t insn;
  8797. Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
  8798. bfd_signed_vma foff;
  8799. uint16_t insn16;
  8800. unsigned long reloc;
  8801. irelend = internal_relocs + sec->reloc_count;
  8802. seq_len = GET_SEQ_LEN (irel->r_addend);
  8803. laddr = irel->r_offset;
  8804. *insn_len = seq_len;
  8805. insn16_on = IS_16BIT_ON (irel->r_addend);
  8806. hi_irelfn =
  8807. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  8808. R_NDS32_HI20_RELA, laddr);
  8809. lo_irelfn =
  8810. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  8811. R_NDS32_LO12S0_ORI_RELA, laddr + 4);
  8812. if (hi_irelfn == irelend || lo_irelfn == irelend)
  8813. {
  8814. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
  8815. (uint64_t) irel->r_offset);
  8816. return false;
  8817. }
  8818. /* Get the value of the symbol referred to by the reloc. */
  8819. foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
  8820. if (foff == 0
  8821. || foff >= CONSERVATIVE_24BIT_S1
  8822. || foff < -CONSERVATIVE_24BIT_S1)
  8823. return false;
  8824. if (insn16_on
  8825. && foff >= -ACCURATE_8BIT_S1
  8826. && foff < ACCURATE_8BIT_S1
  8827. && (seq_len & 0x2))
  8828. {
  8829. /* j8 label */
  8830. /* 16-bit on, but not optimized for speed. */
  8831. reloc = R_NDS32_9_PCREL_RELA;
  8832. insn16 = INSN_J8;
  8833. bfd_putb16 (insn16, contents + irel->r_offset);
  8834. *insn_len = 2;
  8835. irel->r_info =
  8836. ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
  8837. }
  8838. else
  8839. {
  8840. /* j label */
  8841. reloc = R_NDS32_25_PCREL_RELA;
  8842. insn = INSN_J;
  8843. bfd_putb32 (insn, contents + irel->r_offset);
  8844. *insn_len = 4;
  8845. irel->r_info =
  8846. ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
  8847. irel->r_addend = 0;
  8848. }
  8849. hi_irelfn->r_info =
  8850. ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
  8851. lo_irelfn->r_info =
  8852. ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
  8853. if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
  8854. {
  8855. insn16 = NDS32_NOP16;
  8856. bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
  8857. lo_irelfn->r_info =
  8858. ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
  8859. R_NDS32_INSN16);
  8860. lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
  8861. *insn_len += 2;
  8862. }
  8863. return true;
  8864. }
  8865. /* Revert condition branch. This function does not check if the input
  8866. instruction is condition branch or not. */
  8867. static void
  8868. nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
  8869. uint16_t *re_insn16, uint32_t *re_insn)
  8870. {
  8871. uint32_t comp_insn = 0;
  8872. uint16_t comp_insn16 = 0;
  8873. if (insn)
  8874. {
  8875. if (N32_OP6 (insn) == N32_OP6_BR1)
  8876. {
  8877. /* beqs label. */
  8878. comp_insn = (insn ^ 0x4000) & 0xffffc000;
  8879. if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
  8880. {
  8881. /* Insn can be contracted to 16-bit implied r5. */
  8882. comp_insn16 =
  8883. (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
  8884. comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
  8885. }
  8886. }
  8887. else if (N32_OP6 (insn) == N32_OP6_BR3)
  8888. {
  8889. /* bnec $ta, imm11, label. */
  8890. comp_insn = (insn ^ 0x80000) & 0xffffff00;
  8891. }
  8892. else
  8893. {
  8894. comp_insn = (insn ^ 0x10000) & 0xffffc000;
  8895. if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
  8896. || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
  8897. {
  8898. if (N32_IS_RT3 (insn))
  8899. {
  8900. /* Insn can be contracted to 16-bit. */
  8901. comp_insn16 =
  8902. (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
  8903. comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
  8904. }
  8905. else if (N32_RT5 (insn) == REG_R15)
  8906. {
  8907. /* Insn can be contracted to 16-bit. */
  8908. comp_insn16 =
  8909. (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
  8910. }
  8911. }
  8912. }
  8913. }
  8914. else
  8915. {
  8916. switch ((insn16 & 0xf000) >> 12)
  8917. {
  8918. case 0xc:
  8919. /* beqz38 or bnez38 */
  8920. comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
  8921. comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
  8922. comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
  8923. break;
  8924. case 0xd:
  8925. /* beqs38 or bnes38 */
  8926. comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
  8927. comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
  8928. comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
  8929. | (REG_R5 << 15);
  8930. break;
  8931. case 0xe:
  8932. /* beqzS8 or bnezS8 */
  8933. comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
  8934. comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
  8935. comp_insn |= REG_R15 << 20;
  8936. break;
  8937. default:
  8938. break;
  8939. }
  8940. }
  8941. if (comp_insn && re_insn)
  8942. *re_insn = comp_insn;
  8943. if (comp_insn16 && re_insn16)
  8944. *re_insn16 = comp_insn16;
  8945. }
  8946. /* Relax LONGJUMP2 relocation for nds32_elf_relax_section. */
  8947. static bool
  8948. nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
  8949. Elf_Internal_Rela *internal_relocs, int *insn_len,
  8950. bfd_byte *contents, Elf_Internal_Sym *isymbuf,
  8951. Elf_Internal_Shdr *symtab_hdr)
  8952. {
  8953. /* There are 3 variations for LONGJUMP2
  8954. case 2-4; 1st insn convertible, 16-bit on,
  8955. optimize off or optimize for space
  8956. bnes38 rt, ra, $1 ; LONGJUMP2
  8957. j label ; 25_PCREL
  8958. $1:
  8959. case 4-4; 1st insn not convertible
  8960. bne rt, ra, $1 ; LONGJUMP2
  8961. j label ; 25_PCREL
  8962. $1:
  8963. case 4-4; 1st insn convertible, 16-bit on, optimize for speed
  8964. bne rt, ra, $1 ; LONGJUMP2
  8965. j label ; 25_PCREL
  8966. $1: */
  8967. /* Get the reloc for the address from which the register is
  8968. being loaded. This reloc will tell us which function is
  8969. actually being called. */
  8970. bfd_vma laddr;
  8971. int seq_len; /* Original length of instruction sequence. */
  8972. Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
  8973. int first_size;
  8974. unsigned int i;
  8975. bfd_signed_vma foff;
  8976. uint32_t insn, re_insn = 0;
  8977. uint16_t insn16, re_insn16 = 0;
  8978. unsigned long reloc, cond_reloc;
  8979. enum elf_nds32_reloc_type checked_types[] =
  8980. { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
  8981. irelend = internal_relocs + sec->reloc_count;
  8982. seq_len = GET_SEQ_LEN (irel->r_addend);
  8983. laddr = irel->r_offset;
  8984. *insn_len = seq_len;
  8985. first_size = (seq_len == 6) ? 2 : 4;
  8986. i2_irelfn =
  8987. find_relocs_at_address_addr (irel, internal_relocs,
  8988. irelend, R_NDS32_25_PCREL_RELA,
  8989. laddr + first_size);
  8990. for (i = 0; i < ARRAY_SIZE (checked_types); i++)
  8991. {
  8992. cond_irelfn =
  8993. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  8994. checked_types[i], laddr);
  8995. if (cond_irelfn != irelend)
  8996. break;
  8997. }
  8998. if (i2_irelfn == irelend || cond_irelfn == irelend)
  8999. {
  9000. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
  9001. (uint64_t) irel->r_offset);
  9002. return false;
  9003. }
  9004. /* Get the value of the symbol referred to by the reloc. */
  9005. foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
  9006. if (foff == 0
  9007. || foff < -CONSERVATIVE_16BIT_S1
  9008. || foff >= CONSERVATIVE_16BIT_S1)
  9009. return false;
  9010. /* Get the all corresponding instructions. */
  9011. if (first_size == 4)
  9012. {
  9013. insn = bfd_getb32 (contents + laddr);
  9014. nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
  9015. }
  9016. else
  9017. {
  9018. insn16 = bfd_getb16 (contents + laddr);
  9019. nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
  9020. }
  9021. if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
  9022. && foff < ACCURATE_8BIT_S1 - first_size)
  9023. {
  9024. if (first_size == 4)
  9025. {
  9026. /* Don't convert it to 16-bit now, keep this as relaxable for
  9027. ``label reloc; INSN16''. */
  9028. /* Save comp_insn32 to buffer. */
  9029. bfd_putb32 (re_insn, contents + irel->r_offset);
  9030. *insn_len = 4;
  9031. reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
  9032. R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
  9033. cond_reloc = R_NDS32_INSN16;
  9034. }
  9035. else
  9036. {
  9037. bfd_putb16 (re_insn16, contents + irel->r_offset);
  9038. *insn_len = 2;
  9039. reloc = R_NDS32_9_PCREL_RELA;
  9040. cond_reloc = R_NDS32_NONE;
  9041. }
  9042. }
  9043. else if (N32_OP6 (re_insn) == N32_OP6_BR1
  9044. && (foff >= -(ACCURATE_14BIT_S1 - first_size)
  9045. && foff < ACCURATE_14BIT_S1 - first_size))
  9046. {
  9047. /* beqs label ; 15_PCREL */
  9048. bfd_putb32 (re_insn, contents + irel->r_offset);
  9049. *insn_len = 4;
  9050. reloc = R_NDS32_15_PCREL_RELA;
  9051. cond_reloc = R_NDS32_NONE;
  9052. }
  9053. else if (N32_OP6 (re_insn) == N32_OP6_BR2
  9054. && foff >= -CONSERVATIVE_16BIT_S1
  9055. && foff < CONSERVATIVE_16BIT_S1)
  9056. {
  9057. /* beqz label ; 17_PCREL */
  9058. bfd_putb32 (re_insn, contents + irel->r_offset);
  9059. *insn_len = 4;
  9060. reloc = R_NDS32_17_PCREL_RELA;
  9061. cond_reloc = R_NDS32_NONE;
  9062. }
  9063. else
  9064. return false;
  9065. /* Set all relocations. */
  9066. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
  9067. irel->r_addend = i2_irelfn->r_addend;
  9068. cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
  9069. cond_reloc);
  9070. cond_irelfn->r_addend = 0;
  9071. if ((seq_len ^ *insn_len ) & 0x2)
  9072. {
  9073. insn16 = NDS32_NOP16;
  9074. bfd_putb16 (insn16, contents + irel->r_offset + 4);
  9075. i2_irelfn->r_offset = 4;
  9076. i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
  9077. R_NDS32_INSN16);
  9078. i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
  9079. *insn_len += 2;
  9080. }
  9081. else
  9082. i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
  9083. R_NDS32_NONE);
  9084. return true;
  9085. }
  9086. /* Relax LONGJUMP3 relocation for nds32_elf_relax_section. */
  9087. static bool
  9088. nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
  9089. Elf_Internal_Rela *internal_relocs, int *insn_len,
  9090. bfd_byte *contents, Elf_Internal_Sym *isymbuf,
  9091. Elf_Internal_Shdr *symtab_hdr)
  9092. {
  9093. /* There are 5 variations for LONGJUMP3
  9094. case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
  9095. optimize off or optimize for space
  9096. bnes38 rt, ra, $1 ; LONGJUMP3
  9097. sethi ta, hi20(symbol) ; HI20
  9098. ori ta, ta, lo12(symbol) ; LO12S0
  9099. jr5 ta ;
  9100. $1: ;
  9101. case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
  9102. bnes38 rt, ra, $1 ; LONGJUMP3
  9103. sethi ta, hi20(symbol) ; HI20
  9104. ori ta, ta, lo12(symbol) ; LO12S0
  9105. jr5 ta ;
  9106. $1: ; LABEL
  9107. case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
  9108. optimize off or optimize for space
  9109. bne rt, ra, $1 ; LONGJUMP3
  9110. sethi ta, hi20(symbol) ; HI20
  9111. ori ta, ta, lo12(symbol) ; LO12S0
  9112. jr5 ta ;
  9113. $1: ;
  9114. case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
  9115. 16-bit off if no INSN16
  9116. bne rt, ra, $1 ; LONGJUMP3
  9117. sethi ta, hi20(symbol) ; HI20
  9118. ori ta, ta, lo12(symbol) ; LO12S0
  9119. jr ta ;
  9120. $1: ;
  9121. case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
  9122. 16-bit off if no INSN16
  9123. bne rt, ra, $1 ; LONGJUMP3
  9124. sethi ta, hi20(symbol) ; HI20
  9125. ori ta, ta, lo12(symbol) ; LO12S0
  9126. jr ta ;
  9127. $1: ; LABEL */
  9128. /* Get the reloc for the address from which the register is
  9129. being loaded. This reloc will tell us which function is
  9130. actually being called. */
  9131. enum elf_nds32_reloc_type checked_types[] =
  9132. { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
  9133. int reloc_off = 0, cond_removed = 0, convertible;
  9134. bfd_vma laddr;
  9135. int seq_len; /* Original length of instruction sequence. */
  9136. Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
  9137. int first_size;
  9138. unsigned int i;
  9139. bfd_signed_vma foff;
  9140. uint32_t insn, re_insn = 0;
  9141. uint16_t insn16, re_insn16 = 0;
  9142. unsigned long reloc, cond_reloc;
  9143. irelend = internal_relocs + sec->reloc_count;
  9144. seq_len = GET_SEQ_LEN (irel->r_addend);
  9145. laddr = irel->r_offset;
  9146. *insn_len = seq_len;
  9147. convertible = IS_1ST_CONVERT (irel->r_addend);
  9148. if (convertible)
  9149. first_size = 2;
  9150. else
  9151. first_size = 4;
  9152. /* Get all needed relocations. */
  9153. hi_irelfn =
  9154. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9155. R_NDS32_HI20_RELA, laddr + first_size);
  9156. lo_irelfn =
  9157. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9158. R_NDS32_LO12S0_ORI_RELA,
  9159. laddr + first_size + 4);
  9160. for (i = 0; i < ARRAY_SIZE (checked_types); i++)
  9161. {
  9162. cond_irelfn =
  9163. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9164. checked_types[i], laddr);
  9165. if (cond_irelfn != irelend)
  9166. break;
  9167. }
  9168. if (hi_irelfn == irelend
  9169. || lo_irelfn == irelend
  9170. || cond_irelfn == irelend)
  9171. {
  9172. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
  9173. (uint64_t) irel->r_offset);
  9174. return false;
  9175. }
  9176. /* Get the value of the symbol referred to by the reloc. */
  9177. foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
  9178. if (foff == 0
  9179. || foff < -CONSERVATIVE_24BIT_S1
  9180. || foff >= CONSERVATIVE_24BIT_S1)
  9181. return false;
  9182. /* Get the all corresponding instructions. */
  9183. if (first_size == 4)
  9184. {
  9185. insn = bfd_getb32 (contents + laddr);
  9186. nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
  9187. }
  9188. else
  9189. {
  9190. insn16 = bfd_getb16 (contents + laddr);
  9191. nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
  9192. }
  9193. /* For simplicity of coding, we are going to modify the section
  9194. contents, the section relocs, and the BFD symbol table. We
  9195. must tell the rest of the code not to free up this
  9196. information. It would be possible to instead create a table
  9197. of changes which have to be made, as is done in coff-mips.c;
  9198. that would be more work, but would require less memory when
  9199. the linker is run. */
  9200. if (re_insn16
  9201. && foff >= -ACCURATE_8BIT_S1 - first_size
  9202. && foff < ACCURATE_8BIT_S1 - first_size)
  9203. {
  9204. if (!(seq_len & 0x2))
  9205. {
  9206. /* Don't convert it to 16-bit now, keep this as relaxable
  9207. for ``label reloc; INSN1a''6. */
  9208. /* Save comp_insn32 to buffer. */
  9209. bfd_putb32 (re_insn, contents + irel->r_offset);
  9210. *insn_len = 4;
  9211. reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
  9212. R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
  9213. cond_reloc = R_NDS32_INSN16;
  9214. }
  9215. else
  9216. {
  9217. /* Not optimize for speed; convert sequence to 16-bit. */
  9218. /* Save comp_insn16 to buffer. */
  9219. bfd_putb16 (re_insn16, contents + irel->r_offset);
  9220. *insn_len = 2;
  9221. reloc = R_NDS32_9_PCREL_RELA;
  9222. cond_reloc = R_NDS32_NONE;
  9223. }
  9224. cond_removed = 1;
  9225. }
  9226. else if (N32_OP6 (re_insn) == N32_OP6_BR1
  9227. && (foff >= -(ACCURATE_14BIT_S1 - first_size)
  9228. && foff < ACCURATE_14BIT_S1 - first_size))
  9229. {
  9230. /* beqs label ; 15_PCREL */
  9231. bfd_putb32 (re_insn, contents + irel->r_offset);
  9232. *insn_len = 4;
  9233. reloc = R_NDS32_15_PCREL_RELA;
  9234. cond_reloc = R_NDS32_NONE;
  9235. cond_removed = 1;
  9236. }
  9237. else if (N32_OP6 (re_insn) == N32_OP6_BR2
  9238. && foff >= -CONSERVATIVE_16BIT_S1
  9239. && foff < CONSERVATIVE_16BIT_S1)
  9240. {
  9241. /* beqz label ; 17_PCREL */
  9242. bfd_putb32 (re_insn, contents + irel->r_offset);
  9243. *insn_len = 4;
  9244. reloc = R_NDS32_17_PCREL_RELA;
  9245. cond_reloc = R_NDS32_NONE;
  9246. cond_removed = 1;
  9247. }
  9248. else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
  9249. && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
  9250. {
  9251. /* Relax to one of the following 3 variations
  9252. case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
  9253. for space
  9254. bnes38 rt, $1 ; LONGJUMP2
  9255. j label ; 25_PCREL
  9256. $1
  9257. case 4-4; 1st insn not convertible, others don't care
  9258. bne rt, ra, $1 ; LONGJUMP2
  9259. j label ; 25_PCREL
  9260. $1
  9261. case 4-4; 1st insn convertible, 16-bit on, optimize for speed
  9262. bne rt, ra, $1 ; LONGJUMP2
  9263. j label ; 25_PCREL
  9264. $1 */
  9265. /* Offset for first instruction. */
  9266. /* Use j label as second instruction. */
  9267. *insn_len = 4 + first_size;
  9268. insn = INSN_J;
  9269. bfd_putb32 (insn, contents + hi_irelfn->r_offset);
  9270. reloc = R_NDS32_LONGJUMP2;
  9271. cond_reloc = R_NDS32_25_PLTREL;
  9272. }
  9273. else
  9274. return false;
  9275. if (cond_removed == 1)
  9276. {
  9277. /* Set all relocations. */
  9278. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
  9279. irel->r_addend = hi_irelfn->r_addend;
  9280. cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
  9281. cond_reloc);
  9282. cond_irelfn->r_addend = 0;
  9283. hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
  9284. R_NDS32_NONE);
  9285. }
  9286. else
  9287. {
  9288. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
  9289. irel->r_addend = irel->r_addend;
  9290. hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
  9291. cond_reloc);
  9292. }
  9293. if ((seq_len ^ *insn_len ) & 0x2)
  9294. {
  9295. insn16 = NDS32_NOP16;
  9296. bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
  9297. lo_irelfn->r_offset = *insn_len;
  9298. lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
  9299. R_NDS32_INSN16);
  9300. lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
  9301. *insn_len += 2;
  9302. }
  9303. else
  9304. lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
  9305. R_NDS32_NONE);
  9306. return true;
  9307. }
  9308. /* Relax LONGCALL4 relocation for nds32_elf_relax_section. */
  9309. static bool
  9310. nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
  9311. Elf_Internal_Rela *internal_relocs, int *insn_len,
  9312. bfd_byte *contents, Elf_Internal_Sym *isymbuf,
  9313. Elf_Internal_Shdr *symtab_hdr)
  9314. {
  9315. /* The pattern for LONGCALL4. Support for function cse.
  9316. sethi ta, hi20(symbol) ; LONGCALL4/HI20
  9317. ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
  9318. jral ta ; PTR_RES/EMPTY/INSN16 */
  9319. bfd_vma laddr;
  9320. uint32_t insn;
  9321. Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
  9322. Elf_Internal_Rela *irelend;
  9323. bfd_signed_vma foff;
  9324. irelend = internal_relocs + sec->reloc_count;
  9325. laddr = irel->r_offset;
  9326. /* Get the reloc for the address from which the register is
  9327. being loaded. This reloc will tell us which function is
  9328. actually being called. */
  9329. hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9330. R_NDS32_HI20_RELA, laddr);
  9331. if (hi_irel == irelend)
  9332. {
  9333. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
  9334. (uint64_t) irel->r_offset);
  9335. return false;
  9336. }
  9337. /* Get the value of the symbol referred to by the reloc. */
  9338. foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
  9339. /* This condition only happened when symbol is undefined. */
  9340. if (foff == 0
  9341. || foff < -CONSERVATIVE_24BIT_S1
  9342. || foff >= CONSERVATIVE_24BIT_S1)
  9343. return false;
  9344. /* Relax to: jal symbol; 25_PCREL. */
  9345. /* For simplicity of coding, we are going to modify the section
  9346. contents, the section relocs, and the BFD symbol table. We
  9347. must tell the rest of the code not to free up this
  9348. information. It would be possible to instead create a table
  9349. of changes which have to be made, as is done in coff-mips.c;
  9350. that would be more work, but would require less memory when
  9351. the linker is run. */
  9352. ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9353. R_NDS32_PTR_RESOLVED, irel->r_addend);
  9354. em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9355. R_NDS32_EMPTY, irel->r_addend);
  9356. if (ptr_irel == irelend || em_irel == irelend)
  9357. {
  9358. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
  9359. (uint64_t) irel->r_offset);
  9360. return false;
  9361. }
  9362. /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
  9363. insn = bfd_getb32 (contents + irel->r_addend);
  9364. if (insn & 0x80000000)
  9365. return false;
  9366. /* Replace the long call with a jal. */
  9367. em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
  9368. R_NDS32_25_PCREL_RELA);
  9369. ptr_irel->r_addend = 1;
  9370. /* We don't resolve this here but resolve it in relocate_section. */
  9371. insn = INSN_JAL;
  9372. bfd_putb32 (insn, contents + em_irel->r_offset);
  9373. irel->r_info =
  9374. ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
  9375. /* If there is function cse, HI20 can not remove now. */
  9376. call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9377. R_NDS32_LONGCALL4, laddr);
  9378. if (call_irel == irelend)
  9379. {
  9380. *insn_len = 0;
  9381. hi_irel->r_info =
  9382. ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
  9383. }
  9384. insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9385. R_NDS32_INSN16, irel->r_addend);
  9386. if (insn_irel != irelend)
  9387. insn_irel->r_info =
  9388. ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
  9389. return true;
  9390. }
  9391. /* Relax LONGCALL5 relocation for nds32_elf_relax_section. */
  9392. static bool
  9393. nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
  9394. Elf_Internal_Rela *internal_relocs, int *insn_len,
  9395. bfd_byte *contents, Elf_Internal_Sym *isymbuf,
  9396. Elf_Internal_Shdr *symtab_hdr)
  9397. {
  9398. /* The pattern for LONGCALL5.
  9399. bltz rt, .L1 ; LONGCALL5/17_PCREL
  9400. jal symbol ; 25_PCREL
  9401. .L1: */
  9402. bfd_vma laddr;
  9403. uint32_t insn;
  9404. Elf_Internal_Rela *cond_irel, *irelend;
  9405. bfd_signed_vma foff;
  9406. irelend = internal_relocs + sec->reloc_count;
  9407. laddr = irel->r_offset;
  9408. insn = bfd_getb32 (contents + laddr);
  9409. /* Get the reloc for the address from which the register is
  9410. being loaded. This reloc will tell us which function is
  9411. actually being called. */
  9412. cond_irel =
  9413. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9414. R_NDS32_25_PCREL_RELA, irel->r_addend);
  9415. if (cond_irel == irelend)
  9416. {
  9417. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
  9418. (uint64_t) irel->r_offset);
  9419. return false;
  9420. }
  9421. /* Get the value of the symbol referred to by the reloc. */
  9422. foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
  9423. if (foff == 0
  9424. || foff < -CONSERVATIVE_16BIT_S1
  9425. || foff >= CONSERVATIVE_16BIT_S1)
  9426. return false;
  9427. /* Relax to bgezal rt, label ; 17_PCREL
  9428. or bltzal rt, label ; 17_PCREL. */
  9429. /* Convert to complimentary conditional call. */
  9430. insn = CONVERT_CONDITION_CALL (insn);
  9431. /* For simplicity of coding, we are going to modify the section
  9432. contents, the section relocs, and the BFD symbol table. We
  9433. must tell the rest of the code not to free up this
  9434. information. It would be possible to instead create a table
  9435. of changes which have to be made, as is done in coff-mips.c;
  9436. that would be more work, but would require less memory when
  9437. the linker is run. */
  9438. /* Modify relocation and contents. */
  9439. cond_irel->r_info =
  9440. ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
  9441. /* Replace the long call with a bgezal. */
  9442. bfd_putb32 (insn, contents + cond_irel->r_offset);
  9443. *insn_len = 0;
  9444. /* Clean unnessary relocations. */
  9445. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
  9446. cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9447. R_NDS32_17_PCREL_RELA, laddr);
  9448. cond_irel->r_info =
  9449. ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
  9450. return true;
  9451. }
  9452. /* Relax LONGCALL6 relocation for nds32_elf_relax_section. */
  9453. static bool
  9454. nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
  9455. Elf_Internal_Rela *internal_relocs, int *insn_len,
  9456. bfd_byte *contents, Elf_Internal_Sym *isymbuf,
  9457. Elf_Internal_Shdr *symtab_hdr)
  9458. {
  9459. /* The pattern for LONGCALL6.
  9460. bltz rt, .L1 ; LONGCALL6/17_PCREL
  9461. sethi ta, hi20(symbol) ; HI20/PTR
  9462. ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
  9463. jral ta ; PTR_RES/EMPTY/INSN16
  9464. .L1 */
  9465. bfd_vma laddr;
  9466. uint32_t insn;
  9467. Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
  9468. bfd_signed_vma foff;
  9469. irelend = internal_relocs + sec->reloc_count;
  9470. laddr = irel->r_offset;
  9471. /* Get the reloc for the address from which the register is
  9472. being loaded. This reloc will tell us which function is
  9473. actually being called. */
  9474. em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9475. R_NDS32_EMPTY, irel->r_addend);
  9476. if (em_irel == irelend)
  9477. {
  9478. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
  9479. (uint64_t) irel->r_offset);
  9480. return false;
  9481. }
  9482. /* Get the value of the symbol referred to by the reloc. */
  9483. foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
  9484. if (foff == 0
  9485. || foff < -CONSERVATIVE_24BIT_S1
  9486. || foff >= CONSERVATIVE_24BIT_S1)
  9487. return false;
  9488. /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
  9489. insn = bfd_getb32 (contents + irel->r_addend);
  9490. if (insn & 0x80000000)
  9491. return false;
  9492. insn = bfd_getb32 (contents + laddr);
  9493. if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
  9494. {
  9495. /* Relax to bgezal rt, label ; 17_PCREL
  9496. or bltzal rt, label ; 17_PCREL. */
  9497. /* Convert to complimentary conditional call. */
  9498. *insn_len = 0;
  9499. insn = CONVERT_CONDITION_CALL (insn);
  9500. bfd_putb32 (insn, contents + em_irel->r_offset);
  9501. em_irel->r_info =
  9502. ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
  9503. /* Set resolved relocation. */
  9504. cond_irel =
  9505. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9506. R_NDS32_PTR_RESOLVED, irel->r_addend);
  9507. if (cond_irel == irelend)
  9508. {
  9509. _bfd_error_handler (unrecognized_reloc_msg, abfd,
  9510. "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
  9511. return false;
  9512. }
  9513. cond_irel->r_addend = 1;
  9514. /* Clear relocations. */
  9515. irel->r_info =
  9516. ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
  9517. cond_irel =
  9518. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9519. R_NDS32_17_PCREL_RELA, laddr);
  9520. if (cond_irel != irelend)
  9521. cond_irel->r_info =
  9522. ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
  9523. cond_irel =
  9524. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9525. R_NDS32_INSN16, irel->r_addend);
  9526. if (cond_irel != irelend)
  9527. cond_irel->r_info =
  9528. ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
  9529. }
  9530. else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
  9531. {
  9532. /* Relax to the following instruction sequence
  9533. bltz rt, .L1 ; LONGCALL2/17_PCREL
  9534. jal symbol ; 25_PCREL/PTR_RES
  9535. .L1 */
  9536. *insn_len = 4;
  9537. /* Convert instruction. */
  9538. insn = INSN_JAL;
  9539. bfd_putb32 (insn, contents + em_irel->r_offset);
  9540. /* Convert relocations. */
  9541. em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
  9542. R_NDS32_25_PCREL_RELA);
  9543. irel->r_info =
  9544. ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
  9545. /* Set resolved relocation. */
  9546. cond_irel =
  9547. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9548. R_NDS32_PTR_RESOLVED, irel->r_addend);
  9549. if (cond_irel == irelend)
  9550. {
  9551. _bfd_error_handler (unrecognized_reloc_msg, abfd,
  9552. "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
  9553. return false;
  9554. }
  9555. cond_irel->r_addend = 1;
  9556. cond_irel =
  9557. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9558. R_NDS32_INSN16, irel->r_addend);
  9559. if (cond_irel != irelend)
  9560. cond_irel->r_info =
  9561. ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
  9562. }
  9563. return true;
  9564. }
  9565. /* Relax LONGJUMP4 relocation for nds32_elf_relax_section. */
  9566. static bool
  9567. nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
  9568. Elf_Internal_Rela *internal_relocs, int *insn_len,
  9569. bfd_byte *contents, Elf_Internal_Sym *isymbuf,
  9570. Elf_Internal_Shdr *symtab_hdr)
  9571. {
  9572. /* The pattern for LONGJUMP4.
  9573. sethi ta, hi20(symbol) ; LONGJUMP4/HI20
  9574. ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
  9575. jr ta ; PTR_RES/INSN16/EMPTY */
  9576. bfd_vma laddr;
  9577. int seq_len; /* Original length of instruction sequence. */
  9578. uint32_t insn;
  9579. Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
  9580. bfd_signed_vma foff;
  9581. irelend = internal_relocs + sec->reloc_count;
  9582. seq_len = GET_SEQ_LEN (irel->r_addend);
  9583. laddr = irel->r_offset;
  9584. *insn_len = seq_len;
  9585. /* Get the reloc for the address from which the register is
  9586. being loaded. This reloc will tell us which function is
  9587. actually being called. */
  9588. hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9589. R_NDS32_HI20_RELA, laddr);
  9590. if (hi_irel == irelend)
  9591. {
  9592. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
  9593. (uint64_t) irel->r_offset);
  9594. return false;
  9595. }
  9596. /* Get the value of the symbol referred to by the reloc. */
  9597. foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
  9598. if (foff == 0
  9599. || foff >= CONSERVATIVE_24BIT_S1
  9600. || foff < -CONSERVATIVE_24BIT_S1)
  9601. return false;
  9602. /* Convert it to "j label", it may be converted to j8 in the final
  9603. pass of relaxation. Therefore, we do not consider this currently. */
  9604. ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9605. R_NDS32_PTR_RESOLVED, irel->r_addend);
  9606. em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9607. R_NDS32_EMPTY, irel->r_addend);
  9608. if (ptr_irel == irelend || em_irel == irelend)
  9609. {
  9610. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
  9611. (uint64_t) irel->r_offset);
  9612. return false;
  9613. }
  9614. em_irel->r_info =
  9615. ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
  9616. ptr_irel->r_addend = 1;
  9617. /* Write instruction. */
  9618. insn = INSN_J;
  9619. bfd_putb32 (insn, contents + em_irel->r_offset);
  9620. /* Clear relocations. */
  9621. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
  9622. /* If there is function cse, HI20 can not remove now. */
  9623. call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9624. R_NDS32_LONGJUMP4, laddr);
  9625. if (call_irel == irelend)
  9626. {
  9627. *insn_len = 0;
  9628. hi_irel->r_info =
  9629. ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
  9630. }
  9631. return true;
  9632. }
  9633. /* Relax LONGJUMP5 relocation for nds32_elf_relax_section. */
  9634. static bool
  9635. nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
  9636. Elf_Internal_Rela *internal_relocs, int *insn_len,
  9637. int *seq_len, bfd_byte *contents,
  9638. Elf_Internal_Sym *isymbuf,
  9639. Elf_Internal_Shdr *symtab_hdr)
  9640. {
  9641. /* There are 2 variations for LONGJUMP5
  9642. case 2-4; 1st insn convertible, 16-bit on.
  9643. bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
  9644. j label ; 25_PCREL/INSN16
  9645. $1:
  9646. case 4-4; 1st insn not convertible
  9647. bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
  9648. j label ; 25_PCREL/INSN16
  9649. .L1: */
  9650. bfd_vma laddr;
  9651. Elf_Internal_Rela *cond_irel, *irelend;
  9652. unsigned int i;
  9653. bfd_signed_vma foff;
  9654. uint32_t insn, re_insn = 0;
  9655. uint16_t insn16, re_insn16 = 0;
  9656. unsigned long reloc;
  9657. enum elf_nds32_reloc_type checked_types[] =
  9658. { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
  9659. R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
  9660. irelend = internal_relocs + sec->reloc_count;
  9661. laddr = irel->r_offset;
  9662. /* Get the reloc for the address from which the register is
  9663. being loaded. This reloc will tell us which function is
  9664. actually being called. */
  9665. cond_irel =
  9666. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9667. R_NDS32_25_PCREL_RELA, irel->r_addend);
  9668. if (cond_irel == irelend)
  9669. {
  9670. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
  9671. (uint64_t) irel->r_offset);
  9672. return false;
  9673. }
  9674. /* Get the value of the symbol referred to by the reloc. */
  9675. foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
  9676. if (foff == 0
  9677. || foff < -CONSERVATIVE_16BIT_S1
  9678. || foff >= CONSERVATIVE_16BIT_S1)
  9679. return false;
  9680. /* Get the all corresponding instructions. */
  9681. insn = bfd_getb32 (contents + laddr);
  9682. /* Check instruction size. */
  9683. if (insn & 0x80000000)
  9684. {
  9685. *seq_len = 0;
  9686. insn16 = insn >> 16;
  9687. nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
  9688. }
  9689. else
  9690. nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
  9691. if (N32_OP6 (re_insn) == N32_OP6_BR1
  9692. && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
  9693. {
  9694. /* beqs label ; 15_PCREL. */
  9695. bfd_putb32 (re_insn, contents + cond_irel->r_offset);
  9696. reloc = R_NDS32_15_PCREL_RELA;
  9697. }
  9698. else if (N32_OP6 (re_insn) == N32_OP6_BR2
  9699. && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
  9700. {
  9701. /* beqz label ; 17_PCREL. */
  9702. bfd_putb32 (re_insn, contents + cond_irel->r_offset);
  9703. reloc = R_NDS32_17_PCREL_RELA;
  9704. }
  9705. else if ( N32_OP6 (re_insn) == N32_OP6_BR3
  9706. && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
  9707. {
  9708. /* beqc label ; 9_PCREL. */
  9709. bfd_putb32 (re_insn, contents + cond_irel->r_offset);
  9710. reloc = R_NDS32_WORD_9_PCREL_RELA;
  9711. }
  9712. else
  9713. return false;
  9714. /* Set all relocations. */
  9715. cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
  9716. /* Clean relocations. */
  9717. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
  9718. for (i = 0; i < ARRAY_SIZE (checked_types); i++)
  9719. {
  9720. cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9721. checked_types[i], laddr);
  9722. if (cond_irel != irelend)
  9723. {
  9724. if (*seq_len == 0
  9725. && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
  9726. {
  9727. /* If the branch instruction is 2 byte, it cannot remove
  9728. directly. Only convert it to nop16 and remove it after
  9729. checking alignment issue. */
  9730. insn16 = NDS32_NOP16;
  9731. bfd_putb16 (insn16, contents + laddr);
  9732. cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
  9733. }
  9734. else
  9735. cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
  9736. R_NDS32_NONE);
  9737. }
  9738. }
  9739. *insn_len = 0;
  9740. return true;
  9741. }
  9742. /* Relax LONGJUMP6 relocation for nds32_elf_relax_section. */
  9743. static bool
  9744. nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
  9745. Elf_Internal_Rela *internal_relocs, int *insn_len,
  9746. int *seq_len, bfd_byte *contents,
  9747. Elf_Internal_Sym *isymbuf,
  9748. Elf_Internal_Shdr *symtab_hdr)
  9749. {
  9750. /* There are 5 variations for LONGJUMP6
  9751. case : 2-4-4-4; 1st insn convertible, 16-bit on.
  9752. bnes38 rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
  9753. sethi ta, hi20(symbol) ; HI20/PTR
  9754. ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
  9755. jr ta ; PTR_RES/INSN16/EMPTY
  9756. .L1:
  9757. case : 4-4-4-4; 1st insn not convertible, 16-bit on.
  9758. bne rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
  9759. sethi ta, hi20(symbol) ; HI20/PTR
  9760. ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
  9761. jr ta ; PTR_RES/INSN16/EMPTY
  9762. .L1: */
  9763. enum elf_nds32_reloc_type checked_types[] =
  9764. { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
  9765. R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
  9766. int reloc_off = 0, cond_removed = 0;
  9767. bfd_vma laddr;
  9768. Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
  9769. unsigned int i;
  9770. bfd_signed_vma foff;
  9771. uint32_t insn, re_insn = 0;
  9772. uint16_t insn16, re_insn16 = 0;
  9773. unsigned long reloc;
  9774. irelend = internal_relocs + sec->reloc_count;
  9775. laddr = irel->r_offset;
  9776. /* Get the reloc for the address from which the register is
  9777. being loaded. This reloc will tell us which function is
  9778. actually being called. */
  9779. em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9780. R_NDS32_EMPTY, irel->r_addend);
  9781. if (em_irel == irelend)
  9782. {
  9783. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
  9784. (uint64_t) irel->r_offset);
  9785. return false;
  9786. }
  9787. /* Get the value of the symbol referred to by the reloc. */
  9788. foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
  9789. if (foff == 0
  9790. || foff < -CONSERVATIVE_24BIT_S1
  9791. || foff >= CONSERVATIVE_24BIT_S1)
  9792. return false;
  9793. insn = bfd_getb32 (contents + laddr);
  9794. /* Check instruction size. */
  9795. if (insn & 0x80000000)
  9796. {
  9797. *seq_len = 0;
  9798. insn16 = insn >> 16;
  9799. nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
  9800. }
  9801. else
  9802. nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
  9803. /* For simplicity of coding, we are going to modify the section
  9804. contents, the section relocs, and the BFD symbol table. We
  9805. must tell the rest of the code not to free up this
  9806. information. It would be possible to instead create a table
  9807. of changes which have to be made, as is done in coff-mips.c;
  9808. that would be more work, but would require less memory when
  9809. the linker is run. */
  9810. if (N32_OP6 (re_insn) == N32_OP6_BR1
  9811. && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
  9812. {
  9813. /* beqs label ; 15_PCREL. */
  9814. bfd_putb32 (re_insn, contents + em_irel->r_offset);
  9815. reloc = R_NDS32_15_PCREL_RELA;
  9816. cond_removed = 1;
  9817. }
  9818. else if (N32_OP6 (re_insn) == N32_OP6_BR2
  9819. && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
  9820. {
  9821. /* beqz label ; 17_PCREL. */
  9822. bfd_putb32 (re_insn, contents + em_irel->r_offset);
  9823. reloc = R_NDS32_17_PCREL_RELA;
  9824. cond_removed = 1;
  9825. }
  9826. else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
  9827. && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
  9828. {
  9829. /* Relax to one of the following 2 variations
  9830. case 2-4; 1st insn convertible, 16-bit on.
  9831. bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
  9832. j label ; 25_PCREL/INSN16
  9833. $1:
  9834. case 4-4; 1st insn not convertible
  9835. bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
  9836. j label ; 25_PCREL/INSN16
  9837. .L1: */
  9838. /* Use j label as second instruction. */
  9839. insn = INSN_J;
  9840. reloc = R_NDS32_25_PCREL_RELA;
  9841. bfd_putb32 (insn, contents + em_irel->r_offset);
  9842. }
  9843. else
  9844. return false;
  9845. /* Set all relocations. */
  9846. em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
  9847. cond_irel =
  9848. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9849. R_NDS32_PTR_RESOLVED, em_irel->r_offset);
  9850. cond_irel->r_addend = 1;
  9851. /* Use INSN16 of first branch instruction to distinguish if keeping
  9852. INSN16 of final instruction or not. */
  9853. insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9854. R_NDS32_INSN16, irel->r_offset);
  9855. if (insn_irel == irelend)
  9856. {
  9857. /* Clean the final INSN16. */
  9858. insn_irel =
  9859. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9860. R_NDS32_INSN16, em_irel->r_offset);
  9861. insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
  9862. R_NDS32_NONE);
  9863. }
  9864. if (cond_removed == 1)
  9865. {
  9866. *insn_len = 0;
  9867. /* Clear relocations. */
  9868. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
  9869. for (i = 0; i < ARRAY_SIZE (checked_types); i++)
  9870. {
  9871. cond_irel =
  9872. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9873. checked_types[i], laddr);
  9874. if (cond_irel != irelend)
  9875. {
  9876. if (*seq_len == 0
  9877. && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
  9878. {
  9879. /* If the branch instruction is 2 byte, it cannot remove
  9880. directly. Only convert it to nop16 and remove it after
  9881. checking alignment issue. */
  9882. insn16 = NDS32_NOP16;
  9883. bfd_putb16 (insn16, contents + laddr);
  9884. cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
  9885. }
  9886. else
  9887. cond_irel->r_info =
  9888. ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
  9889. }
  9890. }
  9891. }
  9892. else
  9893. {
  9894. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  9895. R_NDS32_LONGJUMP5);
  9896. }
  9897. return true;
  9898. }
  9899. /* Relax LONGJUMP7 relocation for nds32_elf_relax_section. */
  9900. static bool
  9901. nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
  9902. Elf_Internal_Rela *internal_relocs, int *insn_len,
  9903. int *seq_len, bfd_byte *contents,
  9904. Elf_Internal_Sym *isymbuf,
  9905. Elf_Internal_Shdr *symtab_hdr)
  9906. {
  9907. /* There are 2 variations for LONGJUMP5
  9908. case 2-4; 1st insn convertible, 16-bit on.
  9909. movi55 ta, imm11 ; LONGJUMP7/INSN16
  9910. beq rt, ta, label ; 15_PCREL
  9911. case 4-4; 1st insn not convertible
  9912. movi55 ta, imm11 ; LONGJUMP7/INSN16
  9913. beq rt, ta, label ; 15_PCREL */
  9914. bfd_vma laddr;
  9915. Elf_Internal_Rela *cond_irel, *irelend, *insn_irel;
  9916. bfd_signed_vma foff;
  9917. uint32_t insn, re_insn = 0;
  9918. uint16_t insn16;
  9919. uint32_t imm11;
  9920. irelend = internal_relocs + sec->reloc_count;
  9921. laddr = irel->r_offset;
  9922. /* Get the reloc for the address from which the register is
  9923. being loaded. This reloc will tell us which function is
  9924. actually being called. */
  9925. cond_irel =
  9926. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9927. R_NDS32_15_PCREL_RELA, irel->r_addend);
  9928. if (cond_irel == irelend)
  9929. {
  9930. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
  9931. (uint64_t) irel->r_offset);
  9932. return false;
  9933. }
  9934. /* Get the value of the symbol referred to by the reloc. */
  9935. foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
  9936. if (foff == 0
  9937. || foff < -CONSERVATIVE_8BIT_S1
  9938. || foff >= CONSERVATIVE_8BIT_S1)
  9939. return false;
  9940. /* Get the first instruction for its size. */
  9941. insn = bfd_getb32 (contents + laddr);
  9942. if (insn & 0x80000000)
  9943. {
  9944. *seq_len = 0;
  9945. /* Get the immediate from movi55. */
  9946. imm11 = N16_IMM5S (insn >> 16);
  9947. }
  9948. else
  9949. {
  9950. /* Get the immediate from movi. */
  9951. imm11 = N32_IMM20S (insn);
  9952. }
  9953. /* Get the branch instruction. */
  9954. insn = bfd_getb32 (contents + irel->r_addend);
  9955. /* Convert instruction to BR3. */
  9956. if ((insn >> 14) & 0x1)
  9957. re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
  9958. else
  9959. re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
  9960. bfd_putb32 (re_insn, contents + cond_irel->r_offset);
  9961. /* Set all relocations. */
  9962. cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
  9963. R_NDS32_WORD_9_PCREL_RELA);
  9964. /* Clean relocations. */
  9965. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
  9966. insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  9967. R_NDS32_INSN16, irel->r_offset);
  9968. if (insn_irel != irelend)
  9969. {
  9970. if (*seq_len == 0)
  9971. {
  9972. /* If the first insntruction is 16bit, convert it to nop16. */
  9973. insn16 = NDS32_NOP16;
  9974. bfd_putb16 (insn16, contents + laddr);
  9975. insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
  9976. }
  9977. else
  9978. cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
  9979. R_NDS32_NONE);
  9980. }
  9981. *insn_len = 0;
  9982. return true;
  9983. }
  9984. /* We figure out and reassign the best gp value in nds32_elf_final_sda_base
  9985. for each relax round. But the gp may changed dramatically and then cause
  9986. the truncated to fit errors for the the converted gp instructions.
  9987. Therefore, we must reserve the minimum but safe enough size to prevent it. */
  9988. static bool
  9989. nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
  9990. Elf_Internal_Rela *irel, bool *again,
  9991. bool init,
  9992. struct elf_nds32_link_hash_table *table,
  9993. Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
  9994. {
  9995. int offset_to_gp;
  9996. static bool sec_pass = false;
  9997. static asection *first_sec = NULL, *sym_sec;
  9998. /* Record the number of instructions which may be removed. */
  9999. static int count = 0, record_count;
  10000. Elf_Internal_Sym *isym;
  10001. struct elf_link_hash_entry *h = NULL;
  10002. int indx;
  10003. unsigned long r_symndx;
  10004. bfd *abfd = sec->owner;
  10005. static bfd_vma record_sda = 0;
  10006. int sda_offset = 0;
  10007. /* Force doing relaxation when hyper-relax is high. */
  10008. if (table->hyper_relax == 2)
  10009. return true;
  10010. /* Do not relax the load/store patterns for the first
  10011. relax round. */
  10012. if (init)
  10013. {
  10014. if (!first_sec)
  10015. first_sec = sec;
  10016. else if (first_sec == sec)
  10017. {
  10018. record_count = count;
  10019. count = 0;
  10020. sec_pass = true;
  10021. }
  10022. if (!sec_pass)
  10023. *again = true;
  10024. return true;
  10025. }
  10026. /* Generally, _SDA_BASE_ is fixed or smaller. But the large
  10027. DATA_SEGMENT_ALIGN size in the linker script may make it
  10028. get even bigger. */
  10029. if (record_sda == 0)
  10030. record_sda = local_sda;
  10031. else if (local_sda > record_sda)
  10032. sda_offset = local_sda - record_sda;
  10033. /* Assume the instruction will be removed in the best case. */
  10034. count++;
  10035. /* We record the offset to gp for each symbol, and then check
  10036. if it is changed dramatically after relaxing.
  10037. (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
  10038. (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx]. */
  10039. r_symndx = ELF32_R_SYM (irel->r_info);
  10040. if (r_symndx >= symtab_hdr->sh_info)
  10041. {
  10042. /* Global symbols. */
  10043. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  10044. h = elf_sym_hashes (abfd)[indx];
  10045. sym_sec = h->root.u.def.section;
  10046. if (NDS32_GUARD_SEC_P (sym_sec->flags)
  10047. || bfd_is_abs_section (sym_sec))
  10048. {
  10049. /* Forbid doing relaxation when hyper-relax is low. */
  10050. if (table->hyper_relax == 0)
  10051. return false;
  10052. offset_to_gp = *access_addr - local_sda;
  10053. if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
  10054. elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
  10055. else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
  10056. < abs (offset_to_gp) - sda_offset)
  10057. {
  10058. /* This may cause the error, so we reserve the
  10059. safe enough size for relaxing. */
  10060. if (*access_addr >= local_sda)
  10061. *access_addr += (record_count * 4);
  10062. else
  10063. *access_addr -= (record_count * 4);
  10064. }
  10065. return sec_pass;
  10066. }
  10067. }
  10068. else
  10069. {
  10070. /* Local symbols. */
  10071. if (!elf32_nds32_allocate_local_sym_info (abfd))
  10072. return false;
  10073. isym = isymbuf + r_symndx;
  10074. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  10075. if (NDS32_GUARD_SEC_P (sym_sec->flags))
  10076. {
  10077. /* Forbid doing relaxation when hyper-relax is low. */
  10078. if (table->hyper_relax == 0)
  10079. return false;
  10080. offset_to_gp = *access_addr - local_sda;
  10081. if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
  10082. elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
  10083. else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
  10084. < abs (offset_to_gp) - sda_offset)
  10085. {
  10086. /* This may cause the error, so we reserve the
  10087. safe enough size for relaxing. */
  10088. if (*access_addr >= local_sda)
  10089. *access_addr += (record_count * 4);
  10090. else
  10091. *access_addr -= (record_count * 4);
  10092. }
  10093. return sec_pass;
  10094. }
  10095. }
  10096. return true;
  10097. }
  10098. #define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
  10099. /* Relax LOADSTORE relocation for nds32_elf_relax_section. */
  10100. static bool
  10101. nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
  10102. asection *sec, Elf_Internal_Rela *irel,
  10103. Elf_Internal_Rela *internal_relocs, int *insn_len,
  10104. bfd_byte *contents, Elf_Internal_Sym *isymbuf,
  10105. Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
  10106. struct elf_nds32_link_hash_table *table)
  10107. {
  10108. int eliminate_sethi = 0, range_type;
  10109. unsigned int i;
  10110. bfd_vma local_sda, laddr;
  10111. int seq_len; /* Original length of instruction sequence. */
  10112. uint32_t insn;
  10113. Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
  10114. bfd_vma access_addr = 0;
  10115. bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
  10116. struct elf_link_hash_entry *h = NULL;
  10117. int indx;
  10118. enum elf_nds32_reloc_type checked_types[] =
  10119. { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
  10120. R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
  10121. R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
  10122. R_NDS32_TLS_LE_HI20
  10123. };
  10124. irelend = internal_relocs + sec->reloc_count;
  10125. seq_len = GET_SEQ_LEN (irel->r_addend);
  10126. laddr = irel->r_offset;
  10127. *insn_len = seq_len;
  10128. /* Get the high part relocation. */
  10129. for (i = 0; i < ARRAY_SIZE (checked_types); i++)
  10130. {
  10131. hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
  10132. checked_types[i], laddr);
  10133. if (hi_irelfn != irelend)
  10134. break;
  10135. }
  10136. if (hi_irelfn == irelend)
  10137. {
  10138. /* Not R_NDS32_HI20_RELA. */
  10139. if (i != 0)
  10140. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
  10141. (uint64_t) irel->r_offset);
  10142. return false;
  10143. }
  10144. range_type = GET_LOADSTORE_RANGE (irel->r_addend);
  10145. nds32_elf_final_sda_base (sec->output_section->owner,
  10146. link_info, &local_sda, false);
  10147. switch (ELF32_R_TYPE (hi_irelfn->r_info))
  10148. {
  10149. case R_NDS32_HI20_RELA:
  10150. insn = bfd_getb32 (contents + laddr);
  10151. access_addr =
  10152. calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
  10153. if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
  10154. {
  10155. indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
  10156. h = elf_sym_hashes (abfd)[indx];
  10157. }
  10158. /* Try movi. */
  10159. if (range_type == NDS32_LOADSTORE_IMM
  10160. && access_addr < CONSERVATIVE_20BIT
  10161. && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
  10162. {
  10163. eliminate_sethi = 1;
  10164. break;
  10165. }
  10166. if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
  10167. {
  10168. eliminate_sethi = 1;
  10169. break;
  10170. }
  10171. else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
  10172. NULL, false, table, isymbuf, symtab_hdr))
  10173. return false;
  10174. if (!load_store_relax)
  10175. return false;
  10176. /* Case for set gp register. */
  10177. if (N32_RT5 (insn) == REG_GP)
  10178. return false;
  10179. if (range_type == NDS32_LOADSTORE_FLOAT_S
  10180. || range_type == NDS32_LOADSTORE_FLOAT_D)
  10181. {
  10182. range_l = sdata_range[0][0];
  10183. range_h = sdata_range[0][1];
  10184. }
  10185. else
  10186. {
  10187. range_l = sdata_range[1][0];
  10188. range_h = sdata_range[1][1];
  10189. }
  10190. break;
  10191. default:
  10192. return false;
  10193. }
  10194. /* Delete sethi instruction. */
  10195. if (eliminate_sethi == 1
  10196. || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
  10197. || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
  10198. {
  10199. hi_irelfn->r_info =
  10200. ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
  10201. irel->r_info =
  10202. ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
  10203. *insn_len = 0;
  10204. return true;
  10205. }
  10206. return false;
  10207. }
  10208. /* Relax LO12 relocation for nds32_elf_relax_section. */
  10209. static void
  10210. nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
  10211. asection *sec, Elf_Internal_Rela *irel,
  10212. Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
  10213. Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
  10214. struct elf_nds32_link_hash_table *table)
  10215. {
  10216. uint32_t insn;
  10217. bfd_vma local_sda, laddr;
  10218. unsigned long reloc;
  10219. bfd_vma access_addr;
  10220. bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
  10221. Elf_Internal_Rela *irelfn = NULL, *irelend;
  10222. struct elf_link_hash_entry *h = NULL;
  10223. int indx;
  10224. /* For SDA base relative relaxation. */
  10225. nds32_elf_final_sda_base (sec->output_section->owner, link_info,
  10226. &local_sda, false);
  10227. irelend = internal_relocs + sec->reloc_count;
  10228. laddr = irel->r_offset;
  10229. insn = bfd_getb32 (contents + laddr);
  10230. if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
  10231. return;
  10232. access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
  10233. if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
  10234. {
  10235. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  10236. h = elf_sym_hashes (abfd)[indx];
  10237. }
  10238. /* Try movi. */
  10239. if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
  10240. && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
  10241. {
  10242. reloc = R_NDS32_20_RELA;
  10243. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
  10244. insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
  10245. bfd_putb32 (insn, contents + laddr);
  10246. }
  10247. else
  10248. {
  10249. if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
  10250. {
  10251. /* Fall through. */
  10252. }
  10253. else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
  10254. false, table, isymbuf, symtab_hdr))
  10255. return;
  10256. range_l = sdata_range[1][0];
  10257. range_h = sdata_range[1][1];
  10258. switch (ELF32_R_TYPE (irel->r_info))
  10259. {
  10260. case R_NDS32_LO12S0_RELA:
  10261. reloc = R_NDS32_SDA19S0_RELA;
  10262. break;
  10263. case R_NDS32_LO12S1_RELA:
  10264. reloc = R_NDS32_SDA18S1_RELA;
  10265. break;
  10266. case R_NDS32_LO12S2_RELA:
  10267. reloc = R_NDS32_SDA17S2_RELA;
  10268. break;
  10269. case R_NDS32_LO12S2_DP_RELA:
  10270. range_l = sdata_range[0][0];
  10271. range_h = sdata_range[0][1];
  10272. reloc = R_NDS32_SDA12S2_DP_RELA;
  10273. break;
  10274. case R_NDS32_LO12S2_SP_RELA:
  10275. range_l = sdata_range[0][0];
  10276. range_h = sdata_range[0][1];
  10277. reloc = R_NDS32_SDA12S2_SP_RELA;
  10278. break;
  10279. default:
  10280. return;
  10281. }
  10282. /* There are range_h and range_l because linker has to promise
  10283. all sections move cross one page together. */
  10284. if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
  10285. || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
  10286. || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
  10287. {
  10288. if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
  10289. {
  10290. /* Maybe we should add R_NDS32_INSN16 reloc type here
  10291. or manually do some optimization. sethi can't be
  10292. eliminated when updating $gp so the relative ori
  10293. needs to be preserved. */
  10294. return;
  10295. }
  10296. if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
  10297. &insn))
  10298. return;
  10299. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
  10300. bfd_putb32 (insn, contents + laddr);
  10301. irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
  10302. R_NDS32_INSN16);
  10303. /* SDA17 must keep INSN16 for converting fp_as_gp. */
  10304. if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
  10305. irelfn->r_info =
  10306. ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
  10307. }
  10308. }
  10309. return;
  10310. }
  10311. /* Relax PTR relocation for nds32_elf_relax_section. */
  10312. static bool
  10313. nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
  10314. Elf_Internal_Rela *internal_relocs, int *insn_len,
  10315. int *seq_len, bfd_byte *contents)
  10316. {
  10317. Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
  10318. irelend = internal_relocs + sec->reloc_count;
  10319. re_irel =
  10320. find_relocs_at_address_addr (irel, internal_relocs, irelend,
  10321. R_NDS32_PTR_RESOLVED, irel->r_addend);
  10322. if (re_irel == irelend)
  10323. {
  10324. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
  10325. (uint64_t) irel->r_offset);
  10326. return false;
  10327. }
  10328. if (re_irel->r_addend != 1)
  10329. return false;
  10330. /* Pointed target is relaxed and no longer needs this void *,
  10331. change the type to NONE. */
  10332. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
  10333. /* Find PTR_COUNT to decide remove it or not. If PTR_COUNT does
  10334. not exist, it means only count 1 and remove it directly. */
  10335. /* TODO: I hope we can obsolate R_NDS32_COUNT in the future. */
  10336. count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
  10337. R_NDS32_PTR_COUNT);
  10338. ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
  10339. R_NDS32_PTR);
  10340. if (count_irel != irelend)
  10341. {
  10342. if (--count_irel->r_addend > 0)
  10343. return false;
  10344. }
  10345. if (ptr_irel != irelend)
  10346. return false;
  10347. /* If the PTR_COUNT is already 0, remove current instruction. */
  10348. *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
  10349. *insn_len = 0;
  10350. return true;
  10351. }
  10352. /* Relax LWC relocation for nds32_elf_relax_section. */
  10353. static void
  10354. nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
  10355. asection *sec, Elf_Internal_Rela *irel,
  10356. Elf_Internal_Rela *internal_relocs,
  10357. bfd_byte *contents, Elf_Internal_Sym *isymbuf,
  10358. Elf_Internal_Shdr *symtab_hdr, bool *again)
  10359. {
  10360. /* Pattern:
  10361. sethi ra, hi20(symbol) ; HI20/LOADSTORE
  10362. ori ra, ra, lo12(symbol) ; LO12S0/PTR/PTR/.../INSN16
  10363. flsi fsa, [ra + offset1] ; LSI/PTR_RESOLVED/INSN16
  10364. flsi fsb, [ra + offset2] ; LSI/PTR_RESOLVED/INSN16
  10365. ... */
  10366. uint32_t insn;
  10367. bfd_vma local_sda, laddr;
  10368. unsigned long reloc;
  10369. bfd_vma access_addr, flsi_offset;
  10370. bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
  10371. Elf_Internal_Rela *irelend, *re_irel;
  10372. unsigned int opcode;
  10373. irelend = internal_relocs + sec->reloc_count;
  10374. laddr = irel->r_offset;
  10375. insn = bfd_getb32 (contents + laddr);
  10376. if ((insn & 0x80000000) || !is_sda_access_insn (insn))
  10377. return;
  10378. /* Can not do relaxation for bi format. */
  10379. if ((insn & 0x1000))
  10380. return;
  10381. /* Only deal with flsi, fssi, fldi, fsdi, so far. */
  10382. opcode = N32_OP6 (insn);
  10383. if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
  10384. reloc = R_NDS32_SDA12S2_SP_RELA;
  10385. else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
  10386. reloc = R_NDS32_SDA12S2_DP_RELA;
  10387. else
  10388. return;
  10389. re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
  10390. R_NDS32_PTR_RESOLVED);
  10391. if (re_irel == irelend)
  10392. {
  10393. _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
  10394. (uint64_t) irel->r_offset);
  10395. return;
  10396. }
  10397. /* For SDA base relative relaxation. */
  10398. nds32_elf_final_sda_base (sec->output_section->owner, link_info,
  10399. &local_sda, false);
  10400. access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
  10401. flsi_offset = (insn & 0xfff) << 2;
  10402. access_addr += flsi_offset;
  10403. range_l = sdata_range[0][0];
  10404. range_h = sdata_range[0][1];
  10405. if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
  10406. || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
  10407. {
  10408. /* Turn flsi instruction into sda access format. */
  10409. insn = (insn & 0x7ff07000) | (REG_GP << 15);
  10410. /* Add relocation type to flsi. */
  10411. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
  10412. irel->r_addend += flsi_offset;
  10413. bfd_putb32 (insn, contents + re_irel->r_offset);
  10414. re_irel->r_addend |= 1;
  10415. *again = true;
  10416. }
  10417. }
  10418. static bool
  10419. nds32_relax_adjust_label (bfd *abfd, asection *sec,
  10420. Elf_Internal_Rela *internal_relocs,
  10421. bfd_byte *contents,
  10422. nds32_elf_blank_t **relax_blank_list,
  10423. int optimize, int opt_size)
  10424. {
  10425. /* This code block is used to adjust 4-byte alignment by relax a pair
  10426. of instruction a time.
  10427. It recognizes three types of relocations.
  10428. 1. R_NDS32_LABEL - a alignment.
  10429. 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
  10430. 3. is_16bit_NOP () - remove a 16-bit instruction. */
  10431. /* TODO: It seems currently implementation only support 4-byte alignment.
  10432. We should handle any-alignment. */
  10433. Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
  10434. Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
  10435. Elf_Internal_Rela rel_temp;
  10436. Elf_Internal_Rela *irelend;
  10437. bfd_vma address;
  10438. uint16_t insn16;
  10439. /* Checking for branch relaxation relies on the relocations to
  10440. be sorted on 'r_offset'. This is not guaranteed so we must sort. */
  10441. nds32_insertion_sort (internal_relocs, sec->reloc_count,
  10442. sizeof (Elf_Internal_Rela), compar_reloc);
  10443. irelend = internal_relocs + sec->reloc_count;
  10444. /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
  10445. /* FIXME: Can we generate the right order in assembler?
  10446. So we don't have to swapping them here. */
  10447. for (label_rel = internal_relocs, insn_rel = internal_relocs;
  10448. label_rel < irelend; label_rel++)
  10449. {
  10450. if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
  10451. continue;
  10452. /* Find the first reloc has the same offset with label_rel. */
  10453. while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
  10454. insn_rel++;
  10455. for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
  10456. insn_rel++)
  10457. /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
  10458. address. */
  10459. if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
  10460. break;
  10461. if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
  10462. && insn_rel < label_rel)
  10463. {
  10464. /* Swap the two reloc if the R_NDS32_INSN16 is
  10465. before R_NDS32_LABEL. */
  10466. memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
  10467. memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
  10468. memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
  10469. }
  10470. }
  10471. label_rel = NULL;
  10472. insn_rel = NULL;
  10473. /* If there were a sequence of R_NDS32_LABEL end up with .align 2
  10474. or higher, remove other R_NDS32_LABEL with lower alignment.
  10475. If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
  10476. then the R_NDS32_LABEL sequence is broke. */
  10477. for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
  10478. {
  10479. if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
  10480. {
  10481. if (label_rel == NULL)
  10482. {
  10483. if (tmp_rel->r_addend < 2)
  10484. label_rel = tmp_rel;
  10485. continue;
  10486. }
  10487. else if (tmp_rel->r_addend > 1)
  10488. {
  10489. /* Remove all LABEL relocation from label_rel to tmp_rel
  10490. including relocations with same offset as tmp_rel. */
  10491. for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
  10492. {
  10493. if (tmp2_rel->r_offset == tmp_rel->r_offset)
  10494. break;
  10495. if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
  10496. && tmp2_rel->r_addend < 2)
  10497. tmp2_rel->r_info =
  10498. ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
  10499. R_NDS32_NONE);
  10500. }
  10501. label_rel = NULL;
  10502. }
  10503. }
  10504. else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
  10505. {
  10506. /* A new INSN16 which can be converted, so clear label_rel. */
  10507. if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
  10508. irelend, &insn16)
  10509. || is_16bit_NOP (abfd, sec, tmp_rel))
  10510. label_rel = NULL;
  10511. }
  10512. }
  10513. label_rel = NULL;
  10514. insn_rel = NULL;
  10515. /* Optimized for speed and nothing has not been relaxed.
  10516. It's time to align labels.
  10517. We may convert a 16-bit instruction right before a label to
  10518. 32-bit, in order to align the label if necessary
  10519. all reloc entries has been sorted by r_offset. */
  10520. for (irel = internal_relocs;
  10521. irel < irelend && irel->r_offset < sec->size; irel++)
  10522. {
  10523. if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
  10524. && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
  10525. continue;
  10526. if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
  10527. {
  10528. /* A new INSN16 found, resize the old one. */
  10529. if (is_convert_32_to_16
  10530. (abfd, sec, irel, internal_relocs, irelend, &insn16)
  10531. || is_16bit_NOP (abfd, sec, irel))
  10532. {
  10533. if (insn_rel)
  10534. {
  10535. /* Previous INSN16 reloc exists, reduce its
  10536. size to 16-bit. */
  10537. if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
  10538. irelend, &insn16))
  10539. {
  10540. nds32_elf_write_16 (abfd, contents, insn_rel,
  10541. internal_relocs, irelend, insn16);
  10542. if (!insert_nds32_elf_blank_recalc_total
  10543. (relax_blank_list, insn_rel->r_offset + 2, 2))
  10544. return false;
  10545. }
  10546. else if (is_16bit_NOP (abfd, sec, insn_rel))
  10547. {
  10548. if (!insert_nds32_elf_blank_recalc_total
  10549. (relax_blank_list, insn_rel->r_offset, 2))
  10550. return false;
  10551. }
  10552. insn_rel->r_info =
  10553. ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
  10554. }
  10555. /* Save the new one for later use. */
  10556. insn_rel = irel;
  10557. }
  10558. else
  10559. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  10560. R_NDS32_NONE);
  10561. }
  10562. else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
  10563. {
  10564. /* Search for label. */
  10565. int force_relax = 0;
  10566. /* Label on 16-bit instruction or optimization
  10567. needless, just reset this reloc. */
  10568. insn16 = bfd_getb16 (contents + irel->r_offset);
  10569. if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
  10570. {
  10571. irel->r_info =
  10572. ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
  10573. continue;
  10574. }
  10575. address =
  10576. irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
  10577. irel->r_offset, 1);
  10578. if (!insn_rel)
  10579. {
  10580. /* Check if there is case which can not be aligned. */
  10581. if (irel->r_addend == 2 && address & 0x2)
  10582. return false;
  10583. continue;
  10584. }
  10585. /* Try to align this label. */
  10586. if ((irel->r_addend & 0x1f) < 2)
  10587. {
  10588. /* Check if there is a INSN16 at the same address.
  10589. Label_rel always seats before insn_rel after
  10590. our sort. */
  10591. /* Search for INSN16 at LABEL location. If INSN16 is at
  10592. same location and this LABEL alignment is lower than 2,
  10593. the INSN16 can be converted to 2-byte. */
  10594. for (tmp_rel = irel;
  10595. tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
  10596. tmp_rel++)
  10597. {
  10598. if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
  10599. && (is_convert_32_to_16
  10600. (abfd, sec, tmp_rel, internal_relocs,
  10601. irelend, &insn16)
  10602. || is_16bit_NOP (abfd, sec, tmp_rel)))
  10603. {
  10604. force_relax = 1;
  10605. break;
  10606. }
  10607. }
  10608. }
  10609. if (force_relax || irel->r_addend == 1 || address & 0x2)
  10610. {
  10611. /* Label not aligned. */
  10612. /* Previous reloc exists, reduce its size to 16-bit. */
  10613. if (is_convert_32_to_16 (abfd, sec, insn_rel,
  10614. internal_relocs, irelend, &insn16))
  10615. {
  10616. nds32_elf_write_16 (abfd, contents, insn_rel,
  10617. internal_relocs, irelend, insn16);
  10618. if (!insert_nds32_elf_blank_recalc_total
  10619. (relax_blank_list, insn_rel->r_offset + 2, 2))
  10620. return false;
  10621. }
  10622. else if (is_16bit_NOP (abfd, sec, insn_rel))
  10623. {
  10624. if (!insert_nds32_elf_blank_recalc_total
  10625. (relax_blank_list, insn_rel->r_offset, 2))
  10626. return false;
  10627. }
  10628. }
  10629. /* INSN16 reloc is used. */
  10630. insn_rel = NULL;
  10631. }
  10632. }
  10633. address =
  10634. sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
  10635. if (insn_rel && (address & 0x2 || opt_size))
  10636. {
  10637. if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
  10638. irelend, &insn16))
  10639. {
  10640. nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
  10641. irelend, insn16);
  10642. if (!insert_nds32_elf_blank_recalc_total
  10643. (relax_blank_list, insn_rel->r_offset + 2, 2))
  10644. return false;
  10645. insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
  10646. R_NDS32_NONE);
  10647. }
  10648. else if (is_16bit_NOP (abfd, sec, insn_rel))
  10649. {
  10650. if (!insert_nds32_elf_blank_recalc_total
  10651. (relax_blank_list, insn_rel->r_offset, 2))
  10652. return false;
  10653. insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
  10654. R_NDS32_NONE);
  10655. }
  10656. }
  10657. insn_rel = NULL;
  10658. return true;
  10659. }
  10660. static bool
  10661. nds32_elf_relax_section (bfd *abfd, asection *sec,
  10662. struct bfd_link_info *link_info, bool *again)
  10663. {
  10664. nds32_elf_blank_t *relax_blank_list = NULL;
  10665. Elf_Internal_Shdr *symtab_hdr;
  10666. Elf_Internal_Rela *internal_relocs;
  10667. Elf_Internal_Rela *irel;
  10668. Elf_Internal_Rela *irelend;
  10669. Elf_Internal_Sym *isymbuf = NULL;
  10670. bfd_byte *contents = NULL;
  10671. bool result = true;
  10672. int optimize = 0;
  10673. int opt_size = 0;
  10674. uint32_t insn;
  10675. uint16_t insn16;
  10676. /* Target dependnet option. */
  10677. struct elf_nds32_link_hash_table *table;
  10678. int load_store_relax;
  10679. relax_blank_list = NULL;
  10680. *again = false;
  10681. /* Nothing to do for
  10682. * relocatable link or
  10683. * non-relocatable section or
  10684. * non-code section or
  10685. * empty content or
  10686. * no reloc entry. */
  10687. if (bfd_link_relocatable (link_info)
  10688. || (sec->flags & SEC_RELOC) == 0
  10689. || (sec->flags & SEC_EXCLUDE) != 0
  10690. || (sec->flags & SEC_CODE) == 0
  10691. || sec->size == 0
  10692. || sec->reloc_count == 0)
  10693. return true;
  10694. /* 09.12.11 Workaround. */
  10695. /* We have to adjust align for R_NDS32_LABEL if needed.
  10696. The adjust approach only can fix 2-byte align once. */
  10697. if (sec->alignment_power > 2)
  10698. return true;
  10699. /* Do TLS model conversion once at first. */
  10700. nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
  10701. /* The optimization type to do. */
  10702. table = nds32_elf_hash_table (link_info);
  10703. /* Save the first section for abs symbol relaxation.
  10704. This is used for checking gp relaxation in the
  10705. nds32_elf_relax_loadstore and nds32_elf_relax_lo12. */
  10706. nds32_elf_relax_guard (NULL, 0, sec, NULL, again, true,
  10707. table, NULL, NULL);
  10708. /* The begining of general relaxation. */
  10709. if (is_SDA_BASE_set == 0)
  10710. {
  10711. bfd_vma gp;
  10712. is_SDA_BASE_set = 1;
  10713. nds32_elf_final_sda_base (sec->output_section->owner, link_info,
  10714. &gp, false);
  10715. relax_range_measurement (abfd, link_info);
  10716. }
  10717. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  10718. /* Relocations MUST be kept in memory, because relaxation adjust them. */
  10719. internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
  10720. true /* keep_memory */);
  10721. if (internal_relocs == NULL)
  10722. goto error_return;
  10723. irelend = internal_relocs + sec->reloc_count;
  10724. irel = find_relocs_at_address (internal_relocs, internal_relocs,
  10725. irelend, R_NDS32_RELAX_ENTRY);
  10726. if (irel == irelend)
  10727. return true;
  10728. if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
  10729. {
  10730. if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
  10731. return true;
  10732. if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
  10733. optimize = 1;
  10734. if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
  10735. opt_size = 1;
  10736. }
  10737. load_store_relax = table->load_store_relax;
  10738. /* Get symbol table and section content. */
  10739. contents = NULL;
  10740. if (!nds32_get_section_contents (abfd, sec, &contents, true)
  10741. || !nds32_get_local_syms (abfd, sec, &isymbuf))
  10742. goto error_return;
  10743. /* Do relax loop only when finalize is not done.
  10744. Take care of relaxable relocs except INSN16. */
  10745. for (irel = internal_relocs; irel < irelend; irel++)
  10746. {
  10747. int seq_len; /* Original length of instruction sequence. */
  10748. int insn_len = 0; /* Final length of instruction sequence. */
  10749. bool removed;
  10750. insn = 0;
  10751. if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
  10752. && (irel->r_addend & 0x1f) >= 2)
  10753. optimize = 1;
  10754. /* Relocation Types
  10755. R_NDS32_LONGCALL1 53
  10756. R_NDS32_LONGCALL2 54
  10757. R_NDS32_LONGCALL3 55
  10758. R_NDS32_LONGJUMP1 56
  10759. R_NDS32_LONGJUMP2 57
  10760. R_NDS32_LONGJUMP3 58
  10761. R_NDS32_LOADSTORE 59 */
  10762. if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
  10763. && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
  10764. seq_len = GET_SEQ_LEN (irel->r_addend);
  10765. /* Relocation Types
  10766. R_NDS32_LONGCALL4 107
  10767. R_NDS32_LONGCALL5 108
  10768. R_NDS32_LONGCALL6 109
  10769. R_NDS32_LONGJUMP4 110
  10770. R_NDS32_LONGJUMP5 111
  10771. R_NDS32_LONGJUMP6 112
  10772. R_NDS32_LONGJUMP7 113 */
  10773. else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
  10774. && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
  10775. seq_len = 4;
  10776. /* Relocation Types
  10777. R_NDS32_LO12S0_RELA 30
  10778. R_NDS32_LO12S1_RELA 29
  10779. R_NDS32_LO12S2_RELA 28
  10780. R_NDS32_LO12S2_SP_RELA 71
  10781. R_NDS32_LO12S2_DP_RELA 70
  10782. R_NDS32_GOT_LO12 46
  10783. R_NDS32_GOTOFF_LO12 50
  10784. R_NDS32_PLTREL_LO12 65
  10785. R_NDS32_PLT_GOTREL_LO12 67
  10786. R_NDS32_17IFC_PCREL_RELA 96
  10787. R_NDS32_GOT_SUFF 193
  10788. R_NDS32_GOTOFF_SUFF 194
  10789. R_NDS32_PLT_GOT_SUFF 195
  10790. R_NDS32_MULCALL_SUFF 196
  10791. R_NDS32_PTR 197 */
  10792. else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
  10793. && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
  10794. || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
  10795. || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
  10796. || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
  10797. || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
  10798. || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
  10799. || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
  10800. || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
  10801. || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
  10802. && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
  10803. || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
  10804. || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
  10805. || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
  10806. || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
  10807. || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
  10808. seq_len = 0;
  10809. else
  10810. continue;
  10811. insn_len = seq_len;
  10812. removed = false;
  10813. switch (ELF32_R_TYPE (irel->r_info))
  10814. {
  10815. case R_NDS32_LONGCALL1:
  10816. removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
  10817. &insn_len, contents, isymbuf,
  10818. symtab_hdr);
  10819. break;
  10820. case R_NDS32_LONGCALL2:
  10821. removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
  10822. &insn_len, contents, isymbuf,
  10823. symtab_hdr);
  10824. break;
  10825. case R_NDS32_LONGCALL3:
  10826. removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
  10827. &insn_len, contents, isymbuf,
  10828. symtab_hdr);
  10829. break;
  10830. case R_NDS32_LONGJUMP1:
  10831. removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
  10832. &insn_len, contents, isymbuf,
  10833. symtab_hdr);
  10834. break;
  10835. case R_NDS32_LONGJUMP2:
  10836. removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
  10837. &insn_len, contents, isymbuf,
  10838. symtab_hdr);
  10839. break;
  10840. case R_NDS32_LONGJUMP3:
  10841. removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
  10842. &insn_len, contents, isymbuf,
  10843. symtab_hdr);
  10844. break;
  10845. case R_NDS32_LONGCALL4:
  10846. removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
  10847. &insn_len, contents, isymbuf,
  10848. symtab_hdr);
  10849. break;
  10850. case R_NDS32_LONGCALL5:
  10851. removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
  10852. &insn_len, contents, isymbuf,
  10853. symtab_hdr);
  10854. break;
  10855. case R_NDS32_LONGCALL6:
  10856. removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
  10857. &insn_len, contents, isymbuf,
  10858. symtab_hdr);
  10859. break;
  10860. case R_NDS32_LONGJUMP4:
  10861. removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
  10862. &insn_len, contents, isymbuf,
  10863. symtab_hdr);
  10864. break;
  10865. case R_NDS32_LONGJUMP5:
  10866. removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
  10867. &insn_len, &seq_len, contents,
  10868. isymbuf, symtab_hdr);
  10869. break;
  10870. case R_NDS32_LONGJUMP6:
  10871. removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
  10872. &insn_len, &seq_len, contents,
  10873. isymbuf, symtab_hdr);
  10874. break;
  10875. case R_NDS32_LONGJUMP7:
  10876. removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
  10877. &insn_len, &seq_len, contents,
  10878. isymbuf, symtab_hdr);
  10879. break;
  10880. case R_NDS32_LOADSTORE:
  10881. removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
  10882. internal_relocs, &insn_len,
  10883. contents, isymbuf, symtab_hdr,
  10884. load_store_relax, table);
  10885. break;
  10886. case R_NDS32_LO12S0_RELA:
  10887. case R_NDS32_LO12S1_RELA:
  10888. case R_NDS32_LO12S2_RELA:
  10889. case R_NDS32_LO12S2_DP_RELA:
  10890. case R_NDS32_LO12S2_SP_RELA:
  10891. /* Relax for low part. */
  10892. nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
  10893. contents, isymbuf, symtab_hdr, table);
  10894. /* It is impossible to delete blank, so just continue. */
  10895. continue;
  10896. case R_NDS32_PTR:
  10897. removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
  10898. &insn_len, &seq_len, contents);
  10899. break;
  10900. case R_NDS32_LSI:
  10901. nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
  10902. contents, isymbuf, symtab_hdr, again);
  10903. continue;
  10904. case R_NDS32_GOT_LO12:
  10905. case R_NDS32_GOTOFF_LO12:
  10906. case R_NDS32_PLTREL_LO12:
  10907. case R_NDS32_PLT_GOTREL_LO12:
  10908. case R_NDS32_GOTPC_LO12:
  10909. case R_NDS32_TLS_LE_LO12:
  10910. case R_NDS32_TLS_LE_ADD:
  10911. case R_NDS32_TLS_LE_LS:
  10912. case R_NDS32_PLT_GOT_SUFF:
  10913. case R_NDS32_GOT_SUFF:
  10914. case R_NDS32_GOTOFF_SUFF:
  10915. continue;
  10916. default:
  10917. continue;
  10918. }
  10919. if (removed && seq_len - insn_len > 0)
  10920. {
  10921. if (!insert_nds32_elf_blank
  10922. (&relax_blank_list, irel->r_offset + insn_len,
  10923. seq_len - insn_len))
  10924. goto error_return;
  10925. *again = true;
  10926. }
  10927. }
  10928. calc_nds32_blank_total (relax_blank_list);
  10929. if (table->relax_fp_as_gp)
  10930. {
  10931. if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
  10932. irelend, isymbuf))
  10933. goto error_return;
  10934. if (!*again)
  10935. {
  10936. if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
  10937. irelend))
  10938. goto error_return;
  10939. }
  10940. }
  10941. if (!*again)
  10942. {
  10943. if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
  10944. &relax_blank_list, optimize, opt_size))
  10945. goto error_return;
  10946. }
  10947. /* It doesn't matter optimize_for_space_no_align anymore.
  10948. If object file is assembled with flag '-Os',
  10949. the we don't adjust jump-destination on 4-byte boundary. */
  10950. if (relax_blank_list)
  10951. {
  10952. nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
  10953. relax_blank_list = NULL;
  10954. }
  10955. if (!*again)
  10956. {
  10957. /* Closing the section, so we don't relax it anymore. */
  10958. bfd_vma sec_size_align;
  10959. Elf_Internal_Rela *tmp_rel;
  10960. /* Pad to alignment boundary. Only handle current section alignment. */
  10961. sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
  10962. & ((-1U) << sec->alignment_power);
  10963. if ((sec_size_align - sec->size) & 0x2)
  10964. {
  10965. insn16 = NDS32_NOP16;
  10966. bfd_putb16 (insn16, contents + sec->size);
  10967. sec->size += 2;
  10968. }
  10969. while (sec_size_align != sec->size)
  10970. {
  10971. insn = NDS32_NOP32;
  10972. bfd_putb32 (insn, contents + sec->size);
  10973. sec->size += 4;
  10974. }
  10975. tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
  10976. irelend, R_NDS32_RELAX_ENTRY);
  10977. if (tmp_rel != irelend)
  10978. tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
  10979. clean_nds32_elf_blank ();
  10980. }
  10981. finish:
  10982. if (elf_section_data (sec)->relocs != internal_relocs)
  10983. free (internal_relocs);
  10984. if (elf_section_data (sec)->this_hdr.contents != contents)
  10985. free (contents);
  10986. if (symtab_hdr->contents != (bfd_byte *) isymbuf)
  10987. free (isymbuf);
  10988. return result;
  10989. error_return:
  10990. result = false;
  10991. goto finish;
  10992. }
  10993. static struct bfd_elf_special_section const nds32_elf_special_sections[] =
  10994. {
  10995. {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
  10996. {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
  10997. {NULL, 0, 0, 0, 0}
  10998. };
  10999. static bool
  11000. nds32_elf_section_flags (const Elf_Internal_Shdr *hdr)
  11001. {
  11002. const char *name = hdr->bfd_section->name;
  11003. if (startswith (name, ".sbss")
  11004. || startswith (name, ".sdata"))
  11005. hdr->bfd_section->flags |= SEC_SMALL_DATA;
  11006. return true;
  11007. }
  11008. static bool
  11009. nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
  11010. struct bfd_link_info *info,
  11011. void *finfo ATTRIBUTE_UNUSED,
  11012. int (*func) (void *, const char *,
  11013. Elf_Internal_Sym *,
  11014. asection *,
  11015. struct elf_link_hash_entry *)
  11016. ATTRIBUTE_UNUSED)
  11017. {
  11018. FILE *sym_ld_script = NULL;
  11019. struct elf_nds32_link_hash_table *table;
  11020. table = nds32_elf_hash_table (info);
  11021. sym_ld_script = table->sym_ld_script;
  11022. if (check_start_export_sym)
  11023. fprintf (sym_ld_script, "}\n");
  11024. return true;
  11025. }
  11026. static enum elf_reloc_type_class
  11027. nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  11028. const asection *rel_sec ATTRIBUTE_UNUSED,
  11029. const Elf_Internal_Rela *rela)
  11030. {
  11031. switch ((int) ELF32_R_TYPE (rela->r_info))
  11032. {
  11033. case R_NDS32_RELATIVE:
  11034. return reloc_class_relative;
  11035. case R_NDS32_JMP_SLOT:
  11036. return reloc_class_plt;
  11037. case R_NDS32_COPY:
  11038. return reloc_class_copy;
  11039. default:
  11040. return reloc_class_normal;
  11041. }
  11042. }
  11043. /* Put target dependent option into info hash table. */
  11044. void
  11045. bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
  11046. int relax_fp_as_gp,
  11047. int eliminate_gc_relocs,
  11048. FILE * sym_ld_script,
  11049. int hyper_relax,
  11050. int tls_desc_trampoline,
  11051. int load_store_relax)
  11052. {
  11053. struct elf_nds32_link_hash_table *table;
  11054. table = nds32_elf_hash_table (link_info);
  11055. if (table == NULL)
  11056. return;
  11057. table->relax_fp_as_gp = relax_fp_as_gp;
  11058. table->eliminate_gc_relocs = eliminate_gc_relocs;
  11059. table->sym_ld_script = sym_ld_script;
  11060. table->hyper_relax = hyper_relax;
  11061. table->tls_desc_trampoline = tls_desc_trampoline;
  11062. table ->load_store_relax = load_store_relax;
  11063. }
  11064. /* These functions and data-structures are used for fp-as-gp
  11065. optimization. */
  11066. #define FAG_THRESHOLD 3 /* At least 3 gp-access. */
  11067. /* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
  11068. the read-only section and read-write section. */
  11069. #define FAG_WINDOW (508 - 32)
  11070. /* An nds32_fag represent a gp-relative access.
  11071. We find best fp-base by using a sliding window
  11072. to find a base address which can cover most gp-access. */
  11073. struct nds32_fag
  11074. {
  11075. struct nds32_fag *next; /* NULL-teminated linked list. */
  11076. bfd_vma addr; /* The address of this fag. */
  11077. Elf_Internal_Rela **relas; /* The relocations associated with this fag.
  11078. It is used for applying FP7U2_FLAG. */
  11079. int count; /* How many times this address is referred.
  11080. There should be exactly `count' relocations
  11081. in relas. */
  11082. int relas_capcity; /* The buffer size of relas.
  11083. We use an array instead of linked-list,
  11084. and realloc is used to adjust buffer size. */
  11085. };
  11086. static void
  11087. nds32_fag_init (struct nds32_fag *head)
  11088. {
  11089. memset (head, 0, sizeof (struct nds32_fag));
  11090. }
  11091. static void
  11092. nds32_fag_verify (struct nds32_fag *head)
  11093. {
  11094. struct nds32_fag *iter;
  11095. struct nds32_fag *prev;
  11096. prev = NULL;
  11097. iter = head->next;
  11098. while (iter)
  11099. {
  11100. if (prev && prev->addr >= iter->addr)
  11101. puts ("Bug in fp-as-gp insertion.");
  11102. prev = iter;
  11103. iter = iter->next;
  11104. }
  11105. }
  11106. /* Insert a fag in ascending order.
  11107. If a fag of the same address already exists,
  11108. they are chained by relas array. */
  11109. static void
  11110. nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
  11111. Elf_Internal_Rela * rel)
  11112. {
  11113. struct nds32_fag *iter;
  11114. struct nds32_fag *new_fag;
  11115. const int INIT_RELAS_CAP = 4;
  11116. for (iter = head;
  11117. iter->next && iter->next->addr <= addr;
  11118. iter = iter->next)
  11119. /* Find somewhere to insert. */ ;
  11120. /* `iter' will be equal to `head' if the list is empty. */
  11121. if (iter != head && iter->addr == addr)
  11122. {
  11123. /* The address exists in the list.
  11124. Insert `rel' into relocation list, relas. */
  11125. /* Check whether relas is big enough. */
  11126. if (iter->count >= iter->relas_capcity)
  11127. {
  11128. iter->relas_capcity *= 2;
  11129. iter->relas = bfd_realloc
  11130. (iter->relas, iter->relas_capcity * sizeof (void *));
  11131. }
  11132. iter->relas[iter->count++] = rel;
  11133. return;
  11134. }
  11135. /* This is a new address. Create a fag node for it. */
  11136. new_fag = bfd_malloc (sizeof (struct nds32_fag));
  11137. memset (new_fag, 0, sizeof (*new_fag));
  11138. new_fag->addr = addr;
  11139. new_fag->count = 1;
  11140. new_fag->next = iter->next;
  11141. new_fag->relas_capcity = INIT_RELAS_CAP;
  11142. new_fag->relas = (Elf_Internal_Rela **)
  11143. bfd_malloc (new_fag->relas_capcity * sizeof (void *));
  11144. new_fag->relas[0] = rel;
  11145. iter->next = new_fag;
  11146. nds32_fag_verify (head);
  11147. }
  11148. static void
  11149. nds32_fag_free_list (struct nds32_fag *head)
  11150. {
  11151. struct nds32_fag *iter;
  11152. iter = head->next;
  11153. while (iter)
  11154. {
  11155. struct nds32_fag *tmp = iter;
  11156. iter = iter->next;
  11157. free (tmp->relas);
  11158. tmp->relas = NULL;
  11159. free (tmp);
  11160. }
  11161. }
  11162. /* Find the best fp-base address.
  11163. The relocation associated with that address is returned,
  11164. so we can track the symbol instead of a fixed address.
  11165. When relaxation, the address of an datum may change,
  11166. because a text section is shrinked, so the data section
  11167. moves forward. If the aligments of text and data section
  11168. are different, their distance may change too.
  11169. Therefore, tracking a fixed address is not appriate. */
  11170. static int
  11171. nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
  11172. {
  11173. struct nds32_fag *base; /* First fag in the window. */
  11174. struct nds32_fag *last; /* First fag outside the window. */
  11175. int accu = 0; /* Usage accumulation. */
  11176. struct nds32_fag *best; /* Best fag. */
  11177. int baccu = 0; /* Best accumulation. */
  11178. /* Use first fag for initial, and find the last fag in the window.
  11179. In each iteration, we could simply subtract previous fag
  11180. and accumulate following fags which are inside the window,
  11181. untill we each the end. */
  11182. if (head->next == NULL)
  11183. {
  11184. *bestpp = NULL;
  11185. return 0;
  11186. }
  11187. /* Initialize base. */
  11188. base = head->next;
  11189. best = base;
  11190. for (last = base;
  11191. last && last->addr < base->addr + FAG_WINDOW;
  11192. last = last->next)
  11193. accu += last->count;
  11194. baccu = accu;
  11195. /* Record the best base in each iteration. */
  11196. while (base->next)
  11197. {
  11198. accu -= base->count;
  11199. base = base->next;
  11200. /* Account fags in window. */
  11201. for (/* Nothing. */;
  11202. last && last->addr < base->addr + FAG_WINDOW;
  11203. last = last->next)
  11204. accu += last->count;
  11205. /* A better fp-base? */
  11206. if (accu > baccu)
  11207. {
  11208. best = base;
  11209. baccu = accu;
  11210. }
  11211. }
  11212. if (bestpp)
  11213. *bestpp = best;
  11214. return baccu;
  11215. }
  11216. /* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
  11217. so we can convert it fo fp-relative access later.
  11218. `best_fag' is the best fp-base. Only those inside the window
  11219. of best_fag is applied the flag. */
  11220. static bool
  11221. nds32_fag_mark_relax (struct bfd_link_info *link_info,
  11222. asection *sec, struct nds32_fag *best_fag,
  11223. Elf_Internal_Rela *internal_relocs,
  11224. Elf_Internal_Rela *irelend)
  11225. {
  11226. struct nds32_fag *ifag;
  11227. bfd_vma best_fpbase, gp;
  11228. bfd *output_bfd;
  11229. output_bfd = sec->output_section->owner;
  11230. nds32_elf_final_sda_base (output_bfd, link_info, &gp, false);
  11231. best_fpbase = best_fag->addr;
  11232. if (best_fpbase > gp + sdata_range[1][1]
  11233. || best_fpbase < gp - sdata_range[1][0])
  11234. return false;
  11235. /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
  11236. so we know they can be converted to lwi37.fp. */
  11237. for (ifag = best_fag;
  11238. ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
  11239. {
  11240. int i;
  11241. for (i = 0; i < ifag->count; i++)
  11242. {
  11243. Elf_Internal_Rela *insn16_rel;
  11244. Elf_Internal_Rela *fag_rel;
  11245. fag_rel = ifag->relas[i];
  11246. /* Only if this is within the WINDOWS, FP7U2_FLAG
  11247. is applied. */
  11248. insn16_rel = find_relocs_at_address
  11249. (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
  11250. if (insn16_rel != irelend)
  11251. insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
  11252. }
  11253. }
  11254. return true;
  11255. }
  11256. /* Reset INSN16 to clean fp as gp. */
  11257. static void
  11258. nds32_fag_unmark_relax (struct nds32_fag *fag,
  11259. Elf_Internal_Rela *internal_relocs,
  11260. Elf_Internal_Rela *irelend)
  11261. {
  11262. struct nds32_fag *ifag;
  11263. int i;
  11264. Elf_Internal_Rela *insn16_rel;
  11265. Elf_Internal_Rela *fag_rel;
  11266. for (ifag = fag; ifag; ifag = ifag->next)
  11267. {
  11268. for (i = 0; i < ifag->count; i++)
  11269. {
  11270. fag_rel = ifag->relas[i];
  11271. /* Restore the INSN16 relocation. */
  11272. insn16_rel = find_relocs_at_address
  11273. (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
  11274. if (insn16_rel != irelend)
  11275. insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
  11276. }
  11277. }
  11278. }
  11279. /* This is the main function of fp-as-gp optimization.
  11280. It should be called by relax_section. */
  11281. static bool
  11282. nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
  11283. bfd *abfd, asection *sec,
  11284. Elf_Internal_Rela *internal_relocs,
  11285. Elf_Internal_Rela *irelend,
  11286. Elf_Internal_Sym *isymbuf)
  11287. {
  11288. Elf_Internal_Rela *begin_rel = NULL;
  11289. Elf_Internal_Rela *irel;
  11290. struct nds32_fag fag_head;
  11291. Elf_Internal_Shdr *symtab_hdr;
  11292. bfd_byte *contents;
  11293. bool ifc_inside = false;
  11294. /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
  11295. /* Per-function fp-base selection.
  11296. 1. Create a list for all the gp-relative access.
  11297. 2. Base on those gp-relative address,
  11298. find a fp-base which can cover most access.
  11299. 3. Use the fp-base for fp-as-gp relaxation.
  11300. NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
  11301. we should
  11302. 1. delete the `la $fp, _FP_BASE_' instruction and
  11303. 2. not convert lwi.gp to lwi37.fp.
  11304. To delete the _FP_BASE_ instruction, we simply apply
  11305. R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
  11306. To suppress the conversion, we simply NOT to apply
  11307. R_NDS32_INSN16_FP7U2_FLAG flag. */
  11308. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  11309. contents = NULL;
  11310. if (!nds32_get_section_contents (abfd, sec, &contents, true)
  11311. || !nds32_get_local_syms (abfd, sec, &isymbuf))
  11312. return false;
  11313. /* Check whether it is worth for fp-as-gp optimization,
  11314. i.e., at least 3 gp-load.
  11315. Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
  11316. apply this optimization. */
  11317. for (irel = internal_relocs; irel < irelend; irel++)
  11318. {
  11319. /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
  11320. One we enter the begin of the region, we track all the LW/ST
  11321. instructions, so when we leave the region, we try to find
  11322. the best fp-base address for those LW/ST instructions. */
  11323. if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
  11324. && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
  11325. {
  11326. /* Begin of the region. */
  11327. if (begin_rel)
  11328. /* xgettext:c-format */
  11329. _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
  11330. begin_rel = irel;
  11331. nds32_fag_init (&fag_head);
  11332. ifc_inside = false;
  11333. }
  11334. else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
  11335. && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
  11336. {
  11337. int accu;
  11338. struct nds32_fag *best_fag, *tmp_fag;
  11339. int dist;
  11340. /* End of the region.
  11341. Check whether it is worth to do fp-as-gp. */
  11342. if (begin_rel == NULL)
  11343. {
  11344. /* xgettext:c-format */
  11345. _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
  11346. abfd, sec);
  11347. continue;
  11348. }
  11349. accu = nds32_fag_find_base (&fag_head, &best_fag);
  11350. /* Clean FP7U2_FLAG because they may set ever. */
  11351. tmp_fag = fag_head.next;
  11352. nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
  11353. /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
  11354. if (accu < FAG_THRESHOLD
  11355. || !nds32_fag_mark_relax (link_info, sec, best_fag,
  11356. internal_relocs, irelend))
  11357. {
  11358. /* Not worth to do fp-as-gp. */
  11359. begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
  11360. begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
  11361. irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
  11362. irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
  11363. nds32_fag_free_list (&fag_head);
  11364. begin_rel = NULL;
  11365. continue;
  11366. }
  11367. /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
  11368. so we use it to record the distance to the reloction of best
  11369. fp-base. */
  11370. dist = best_fag->relas[0] - begin_rel;
  11371. BFD_ASSERT (dist > 0 && dist < 0xffffff);
  11372. /* Use high 16 bits of addend to record the _FP_BASE_ matched
  11373. relocation. And get the base value when relocating. */
  11374. begin_rel->r_addend &= (0x1 << 16) - 1;
  11375. begin_rel->r_addend |= dist << 16;
  11376. nds32_fag_free_list (&fag_head);
  11377. begin_rel = NULL;
  11378. }
  11379. if (begin_rel == NULL || ifc_inside)
  11380. /* Skip if we are not in the region of fp-as-gp. */
  11381. continue;
  11382. if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
  11383. || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
  11384. {
  11385. bfd_vma addr;
  11386. uint32_t insn;
  11387. /* A gp-relative access is found. Insert it to the fag-list. */
  11388. /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
  11389. insn = bfd_getb32 (contents + irel->r_offset);
  11390. if (!N32_IS_RT3 (insn))
  11391. continue;
  11392. addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
  11393. nds32_fag_insert (&fag_head, addr, irel);
  11394. }
  11395. else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
  11396. {
  11397. begin_rel = NULL;
  11398. }
  11399. else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
  11400. || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
  11401. {
  11402. /* Suppress fp as gp when encounter ifc. */
  11403. ifc_inside = true;
  11404. }
  11405. }
  11406. return true;
  11407. }
  11408. /* Remove unused `la $fp, _FD_BASE_' instruction. */
  11409. static bool
  11410. nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
  11411. Elf_Internal_Rela *internal_relocs,
  11412. Elf_Internal_Rela *irelend)
  11413. {
  11414. Elf_Internal_Rela *irel;
  11415. Elf_Internal_Shdr *symtab_hdr;
  11416. bfd_byte *contents = NULL;
  11417. nds32_elf_blank_t *relax_blank_list = NULL;
  11418. bool result = true;
  11419. bool unused_region = false;
  11420. /*
  11421. NOTE: Disable fp-as-gp if we encounter ifcall relocations:
  11422. R_NDS32_17IFC_PCREL_RELA
  11423. R_NDS32_10IFCU_PCREL_RELA. */
  11424. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  11425. nds32_get_section_contents (abfd, sec, &contents, true);
  11426. for (irel = internal_relocs; irel < irelend; irel++)
  11427. {
  11428. /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
  11429. we marked to in previous pass.
  11430. DO NOT scan relocations again, since we've alreadly decided it
  11431. and set the flag. */
  11432. const char *syname;
  11433. int syndx;
  11434. uint32_t insn;
  11435. if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
  11436. && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
  11437. unused_region = true;
  11438. else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
  11439. && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
  11440. unused_region = false;
  11441. /* We're not in the region. */
  11442. if (!unused_region)
  11443. continue;
  11444. /* _FP_BASE_ must be a GLOBAL symbol. */
  11445. syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  11446. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  11447. continue;
  11448. /* The symbol name must be _FP_BASE_. */
  11449. syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
  11450. if (strcmp (syname, FP_BASE_NAME) != 0)
  11451. continue;
  11452. if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
  11453. {
  11454. /* addi.gp $fp, -256 */
  11455. insn = bfd_getb32 (contents + irel->r_offset);
  11456. if (insn != INSN_ADDIGP_TO_FP)
  11457. continue;
  11458. }
  11459. else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
  11460. {
  11461. /* addi $fp, $gp, -256 */
  11462. insn = bfd_getb32 (contents + irel->r_offset);
  11463. if (insn != INSN_ADDI_GP_TO_FP)
  11464. continue;
  11465. }
  11466. else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
  11467. {
  11468. /* movi $fp, FP_BASE */
  11469. insn = bfd_getb32 (contents + irel->r_offset);
  11470. if (insn != INSN_MOVI_TO_FP)
  11471. continue;
  11472. }
  11473. else
  11474. continue;
  11475. /* We got here because a FP_BASE instruction is found. */
  11476. if (!insert_nds32_elf_blank_recalc_total
  11477. (&relax_blank_list, irel->r_offset, 4))
  11478. goto error_return;
  11479. }
  11480. finish:
  11481. if (relax_blank_list)
  11482. {
  11483. nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
  11484. relax_blank_list = NULL;
  11485. }
  11486. return result;
  11487. error_return:
  11488. result = false;
  11489. goto finish;
  11490. }
  11491. /* This is a version of bfd_generic_get_relocated_section_contents.
  11492. We need this variety because relaxation will modify the dwarf
  11493. infomation. When there is undefined symbol reference error mesage,
  11494. linker need to dump line number where the symbol be used. However
  11495. the address is be relaxed, it can not get the original dwarf contents.
  11496. The variety only modify function call for reading in the section. */
  11497. static bfd_byte *
  11498. nds32_elf_get_relocated_section_contents (bfd *abfd,
  11499. struct bfd_link_info *link_info,
  11500. struct bfd_link_order *link_order,
  11501. bfd_byte *data,
  11502. bool relocatable,
  11503. asymbol **symbols)
  11504. {
  11505. bfd *input_bfd = link_order->u.indirect.section->owner;
  11506. asection *input_section = link_order->u.indirect.section;
  11507. long reloc_size;
  11508. arelent **reloc_vector;
  11509. long reloc_count;
  11510. reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
  11511. if (reloc_size < 0)
  11512. return NULL;
  11513. /* Read in the section. */
  11514. if (!nds32_get_section_contents (input_bfd, input_section, &data, false))
  11515. return NULL;
  11516. if (reloc_size == 0)
  11517. return data;
  11518. reloc_vector = (arelent **) bfd_malloc (reloc_size);
  11519. if (reloc_vector == NULL)
  11520. return NULL;
  11521. reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
  11522. reloc_vector, symbols);
  11523. if (reloc_count < 0)
  11524. goto error_return;
  11525. if (reloc_count > 0)
  11526. {
  11527. arelent **parent;
  11528. for (parent = reloc_vector; *parent != NULL; parent++)
  11529. {
  11530. char *error_message = NULL;
  11531. asymbol *symbol;
  11532. bfd_reloc_status_type r;
  11533. symbol = *(*parent)->sym_ptr_ptr;
  11534. if (symbol->section && discarded_section (symbol->section))
  11535. {
  11536. bfd_vma off;
  11537. static reloc_howto_type none_howto
  11538. = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
  11539. "unused", false, 0, 0, false);
  11540. off = (*parent)->address * OCTETS_PER_BYTE (input_bfd,
  11541. input_section);
  11542. _bfd_clear_contents ((*parent)->howto, input_bfd,
  11543. input_section, data, off);
  11544. (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  11545. (*parent)->addend = 0;
  11546. (*parent)->howto = &none_howto;
  11547. r = bfd_reloc_ok;
  11548. }
  11549. else
  11550. r = bfd_perform_relocation (input_bfd, *parent, data,
  11551. input_section,
  11552. relocatable ? abfd : NULL,
  11553. &error_message);
  11554. if (relocatable)
  11555. {
  11556. asection *os = input_section->output_section;
  11557. /* A partial link, so keep the relocs. */
  11558. os->orelocation[os->reloc_count] = *parent;
  11559. os->reloc_count++;
  11560. }
  11561. if (r != bfd_reloc_ok)
  11562. {
  11563. switch (r)
  11564. {
  11565. case bfd_reloc_undefined:
  11566. (*link_info->callbacks->undefined_symbol)
  11567. (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
  11568. input_bfd, input_section, (*parent)->address, true);
  11569. break;
  11570. case bfd_reloc_dangerous:
  11571. BFD_ASSERT (error_message != NULL);
  11572. (*link_info->callbacks->reloc_dangerous)
  11573. (link_info, error_message,
  11574. input_bfd, input_section, (*parent)->address);
  11575. break;
  11576. case bfd_reloc_overflow:
  11577. (*link_info->callbacks->reloc_overflow)
  11578. (link_info, NULL,
  11579. bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
  11580. (*parent)->howto->name, (*parent)->addend,
  11581. input_bfd, input_section, (*parent)->address);
  11582. break;
  11583. case bfd_reloc_outofrange:
  11584. /* PR ld/13730:
  11585. This error can result when processing some partially
  11586. complete binaries. Do not abort, but issue an error
  11587. message instead. */
  11588. link_info->callbacks->einfo
  11589. /* xgettext:c-format */
  11590. (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
  11591. abfd, input_section, * parent);
  11592. goto error_return;
  11593. default:
  11594. abort ();
  11595. break;
  11596. }
  11597. }
  11598. }
  11599. }
  11600. free (reloc_vector);
  11601. return data;
  11602. error_return:
  11603. free (reloc_vector);
  11604. return NULL;
  11605. }
  11606. /* Check target symbol. */
  11607. static bool
  11608. nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
  11609. {
  11610. if (!sym || !sym->name || sym->name[0] != '$')
  11611. return false;
  11612. return true;
  11613. }
  11614. /* nds32 find maybe function sym. Ignore target special symbol
  11615. first, and then go the general function. */
  11616. static bfd_size_type
  11617. nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
  11618. bfd_vma *code_off)
  11619. {
  11620. if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
  11621. return 0;
  11622. return _bfd_elf_maybe_function_sym (sym, sec, code_off);
  11623. }
  11624. /* Do TLS model conversion. */
  11625. typedef struct relax_group_list_t
  11626. {
  11627. Elf_Internal_Rela *relo;
  11628. struct relax_group_list_t *next;
  11629. struct relax_group_list_t *next_sibling;
  11630. int id;
  11631. } relax_group_list_t;
  11632. int
  11633. list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
  11634. int
  11635. list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
  11636. void
  11637. dump_chain (relax_group_list_t *pHead);
  11638. int
  11639. list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
  11640. {
  11641. relax_group_list_t *pNext = pHead;
  11642. /* Find place. */
  11643. while (pNext->next)
  11644. {
  11645. if (pNext->next->id > (int) pElem->r_addend)
  11646. break;
  11647. pNext = pNext->next;
  11648. }
  11649. /* Insert node. */
  11650. relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
  11651. if (!pNew)
  11652. return false;
  11653. relax_group_list_t *tmp = pNext->next;
  11654. pNext->next = pNew;
  11655. pNew->id = pElem->r_addend;
  11656. pNew->relo = pElem;
  11657. pNew->next = tmp;
  11658. pNew->next_sibling = NULL;
  11659. return true;
  11660. }
  11661. int
  11662. list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
  11663. {
  11664. relax_group_list_t *pNext = pNode;
  11665. /* Find place. */
  11666. while (pNext->next_sibling)
  11667. {
  11668. pNext = pNext->next_sibling;
  11669. }
  11670. /* Insert node. */
  11671. relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
  11672. if (!pNew)
  11673. return false;
  11674. relax_group_list_t *tmp = pNext->next_sibling;
  11675. pNext->next_sibling = pNew;
  11676. pNew->id = -1;
  11677. pNew->relo = pElem;
  11678. pNew->next = NULL;
  11679. pNew->next_sibling = tmp;
  11680. return true;
  11681. }
  11682. void
  11683. dump_chain (relax_group_list_t *pHead)
  11684. {
  11685. relax_group_list_t *pNext = pHead->next;
  11686. while (pNext)
  11687. {
  11688. printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
  11689. relax_group_list_t *pNextSib = pNext->next_sibling;
  11690. while (pNextSib)
  11691. {
  11692. printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
  11693. pNextSib = pNextSib->next_sibling;
  11694. }
  11695. pNext = pNext->next;
  11696. printf("\n");
  11697. }
  11698. }
  11699. /* Check R_NDS32_RELAX_GROUP of each section.
  11700. There might be multiple sections in one object file. */
  11701. int
  11702. elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
  11703. {
  11704. elf32_nds32_relax_group_t *relax_group_ptr =
  11705. elf32_nds32_relax_group_ptr (abfd);
  11706. int min_id = relax_group_ptr->min_id;
  11707. int max_id = relax_group_ptr->max_id;
  11708. Elf_Internal_Rela *rel;
  11709. Elf_Internal_Rela *relend;
  11710. Elf_Internal_Rela *relocs;
  11711. enum elf_nds32_reloc_type rtype;
  11712. do
  11713. {
  11714. /* Relocations MUST be kept in memory, because relaxation adjust them. */
  11715. relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
  11716. true /* keep_memory */);
  11717. if (relocs == NULL)
  11718. break;
  11719. /* Check R_NDS32_RELAX_GROUP. */
  11720. relend = relocs + asec->reloc_count;
  11721. for (rel = relocs; rel < relend; rel++)
  11722. {
  11723. int id;
  11724. rtype = ELF32_R_TYPE (rel->r_info);
  11725. if (rtype != R_NDS32_RELAX_GROUP)
  11726. continue;
  11727. id = rel->r_addend;
  11728. if (id < min_id)
  11729. min_id = id;
  11730. else if (id > max_id)
  11731. max_id = id;
  11732. }
  11733. }
  11734. while (false);
  11735. if (elf_section_data (asec)->relocs != relocs)
  11736. free (relocs);
  11737. if ((min_id != relax_group_ptr->min_id)
  11738. || (max_id != relax_group_ptr->max_id))
  11739. {
  11740. relax_group_ptr->count = max_id - min_id + 1;
  11741. BFD_ASSERT(min_id <= relax_group_ptr->min_id);
  11742. relax_group_ptr->min_id = min_id;
  11743. BFD_ASSERT(max_id >= relax_group_ptr->max_id);
  11744. relax_group_ptr->max_id = max_id;
  11745. }
  11746. return relax_group_ptr->count;
  11747. }
  11748. /* Reorder RELAX_GROUP ID when command line option '-r' is applied. */
  11749. static struct section_id_list_t *relax_group_section_id_list = NULL;
  11750. struct section_id_list_t *
  11751. elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
  11752. {
  11753. struct section_id_list_t *result = NULL;
  11754. struct section_id_list_t *lst = *lst_ptr;
  11755. if (NULL == lst)
  11756. {
  11757. result = (struct section_id_list_t *) calloc
  11758. (1, sizeof (struct section_id_list_t));
  11759. BFD_ASSERT (result); /* Feed me. */
  11760. result->id = id;
  11761. *lst_ptr = result;
  11762. }
  11763. else
  11764. {
  11765. struct section_id_list_t *cur = lst;
  11766. struct section_id_list_t *prv = NULL;
  11767. struct section_id_list_t *sec = NULL;
  11768. while (cur)
  11769. {
  11770. if (cur->id < id)
  11771. {
  11772. prv = cur;
  11773. cur = cur->next;
  11774. continue;
  11775. }
  11776. if (cur->id > id)
  11777. {
  11778. cur = NULL; /* To insert after prv. */
  11779. sec = cur; /* In case prv == NULL. */
  11780. }
  11781. break;
  11782. }
  11783. if (NULL == cur)
  11784. {
  11785. /* Insert after prv. */
  11786. result = (struct section_id_list_t *) calloc
  11787. (1, sizeof (struct section_id_list_t));
  11788. BFD_ASSERT (result); /* Feed me. */
  11789. result->id = id;
  11790. if (NULL != prv)
  11791. {
  11792. result->next = prv->next;
  11793. prv->next = result;
  11794. }
  11795. else
  11796. {
  11797. *lst_ptr = result;
  11798. result->next = sec;
  11799. }
  11800. }
  11801. }
  11802. return result;
  11803. }
  11804. int
  11805. elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
  11806. {
  11807. static int next_relax_group_bias = 0;
  11808. elf32_nds32_relax_group_t *relax_group_ptr =
  11809. elf32_nds32_relax_group_ptr (abfd);
  11810. bool result = true;
  11811. Elf_Internal_Rela *rel;
  11812. Elf_Internal_Rela *relend;
  11813. Elf_Internal_Rela *relocs = NULL;
  11814. enum elf_nds32_reloc_type rtype;
  11815. struct section_id_list_t *node = NULL;
  11816. int count = 0;
  11817. do
  11818. {
  11819. if (0 == relax_group_ptr->count)
  11820. break;
  11821. /* Check if this section has been handled. */
  11822. node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
  11823. if (NULL == node)
  11824. break; /* Hit, the section id has handled. */
  11825. /* Relocations MUST be kept in memory, because relaxation adjust them. */
  11826. relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
  11827. true /* keep_memory */);
  11828. if (relocs == NULL)
  11829. {
  11830. BFD_ASSERT (0); /* feed me */
  11831. break;
  11832. }
  11833. /* Allocate group id bias for this bfd! */
  11834. if (0 == relax_group_ptr->init)
  11835. {
  11836. relax_group_ptr->bias = next_relax_group_bias;
  11837. next_relax_group_bias += relax_group_ptr->count;
  11838. relax_group_ptr->init = 1;
  11839. }
  11840. /* Reorder relax group groups. */
  11841. relend = relocs + asec->reloc_count;
  11842. for (rel = relocs; rel < relend; rel++)
  11843. {
  11844. rtype = ELF32_R_TYPE(rel->r_info);
  11845. if (rtype != R_NDS32_RELAX_GROUP)
  11846. continue;
  11847. /* Change it. */
  11848. rel->r_addend += relax_group_ptr->bias;
  11849. /* Debugging count. */
  11850. count++;
  11851. }
  11852. }
  11853. while (false);
  11854. if (elf_section_data (asec)->relocs != relocs)
  11855. free (relocs);
  11856. return result;
  11857. }
  11858. int
  11859. nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
  11860. struct bfd_link_info *lnkinfo)
  11861. {
  11862. bool result = true;
  11863. Elf_Internal_Rela *irel;
  11864. Elf_Internal_Rela *irelend;
  11865. Elf_Internal_Rela *internal_relocs;
  11866. unsigned long r_symndx;
  11867. enum elf_nds32_reloc_type r_type;
  11868. Elf_Internal_Sym *local_syms = NULL;
  11869. bfd_byte *contents = NULL;
  11870. relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
  11871. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
  11872. struct elf_link_hash_entry **sym_hashes;
  11873. sym_hashes = elf_sym_hashes (inbfd);
  11874. /* Reorder RELAX_GROUP when command line option '-r' is applied. */
  11875. if (bfd_link_relocatable (lnkinfo))
  11876. {
  11877. elf32_nds32_unify_relax_group (inbfd, insec);
  11878. return result;
  11879. }
  11880. /* Relocations MUST be kept in memory, because relaxation adjust them. */
  11881. internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
  11882. true /* keep_memory */);
  11883. if (internal_relocs == NULL)
  11884. goto error_return;
  11885. irelend = internal_relocs + insec->reloc_count;
  11886. irel = find_relocs_at_address (internal_relocs, internal_relocs,
  11887. irelend, R_NDS32_RELAX_ENTRY);
  11888. if (irel == irelend)
  11889. goto finish;
  11890. /* Chain/remove groups. */
  11891. for (irel = internal_relocs; irel < irelend; irel++)
  11892. {
  11893. r_symndx = ELF32_R_SYM (irel->r_info);
  11894. r_type = ELF32_R_TYPE (irel->r_info);
  11895. if (r_type != R_NDS32_RELAX_GROUP)
  11896. continue;
  11897. /* Remove it. */
  11898. irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
  11899. /* Chain it now. */
  11900. if (!list_insert (&chain, irel))
  11901. goto error_return;
  11902. }
  11903. /* Collect group relocations. */
  11904. /* Presume relocations are sorted. */
  11905. relax_group_list_t *pNext = chain.next;
  11906. while (pNext)
  11907. {
  11908. for (irel = internal_relocs; irel < irelend; irel++)
  11909. {
  11910. if (irel->r_offset == pNext->relo->r_offset)
  11911. {
  11912. /* Ignore Non-TLS relocation types. */
  11913. r_type = ELF32_R_TYPE (irel->r_info);
  11914. if ((R_NDS32_TLS_LE_HI20 > r_type)
  11915. || (R_NDS32_RELAX_ENTRY == r_type))
  11916. continue;
  11917. if (!list_insert_sibling (pNext, irel))
  11918. goto error_return;
  11919. }
  11920. else if (irel->r_offset > pNext->relo->r_offset)
  11921. {
  11922. pNext = pNext->next;
  11923. if (!pNext)
  11924. break;
  11925. bfd_vma current_offset = pNext->relo->r_offset;
  11926. if (irel->r_offset > current_offset)
  11927. irel = internal_relocs; /* restart from head */
  11928. else
  11929. --irel; /* Check current irel again. */
  11930. continue;
  11931. }
  11932. else
  11933. {
  11934. /* This shouldn't be reached. */
  11935. }
  11936. }
  11937. if (pNext)
  11938. pNext = pNext->next;
  11939. }
  11940. #ifdef DUBUG_VERBOSE
  11941. dump_chain(&chain);
  11942. #endif
  11943. /* Get symbol table and section content. */
  11944. if (incontents)
  11945. contents = incontents;
  11946. else if (!nds32_get_section_contents (inbfd, insec, &contents, true)
  11947. || !nds32_get_local_syms (inbfd, insec, &local_syms))
  11948. goto error_return;
  11949. char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
  11950. /* Convert TLS model each group if necessary. */
  11951. pNext = chain.next;
  11952. int cur_grp_id = -1;
  11953. int sethi_rt = -1;
  11954. int add_rt = -1;
  11955. enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
  11956. tls_type = org_tls_type = eff_tls_type = 0;
  11957. while (pNext)
  11958. {
  11959. relax_group_list_t *pNextSig = pNext->next_sibling;
  11960. while (pNextSig)
  11961. {
  11962. struct elf_link_hash_entry *h = NULL;
  11963. irel = pNextSig->relo;
  11964. r_symndx = ELF32_R_SYM(irel->r_info);
  11965. r_type = ELF32_R_TYPE(irel->r_info);
  11966. if (pNext->id != cur_grp_id)
  11967. {
  11968. cur_grp_id = pNext->id;
  11969. org_tls_type = get_tls_type (r_type, NULL);
  11970. if (r_symndx >= symtab_hdr->sh_info)
  11971. {
  11972. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  11973. while (h->root.type == bfd_link_hash_indirect
  11974. || h->root.type == bfd_link_hash_warning)
  11975. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  11976. tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
  11977. }
  11978. else
  11979. {
  11980. tls_type = local_got_tls_type
  11981. ? local_got_tls_type[r_symndx]
  11982. : GOT_NORMAL;
  11983. }
  11984. eff_tls_type = 1 << (fls (tls_type) - 1);
  11985. sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
  11986. }
  11987. if (eff_tls_type != org_tls_type)
  11988. {
  11989. switch (org_tls_type)
  11990. {
  11991. /* DESC to IEGP/IE/LE. */
  11992. case GOT_TLS_DESC:
  11993. switch (eff_tls_type)
  11994. {
  11995. case GOT_TLS_IE:
  11996. switch (r_type)
  11997. {
  11998. case R_NDS32_TLS_DESC_HI20:
  11999. irel->r_info = ELF32_R_INFO(r_symndx,
  12000. R_NDS32_TLS_IE_HI20);
  12001. break;
  12002. case R_NDS32_TLS_DESC_LO12:
  12003. irel->r_info = ELF32_R_INFO(r_symndx,
  12004. R_NDS32_TLS_IE_LO12);
  12005. break;
  12006. case R_NDS32_TLS_DESC_ADD:
  12007. {
  12008. uint32_t insn = bfd_getb32 (contents + irel->r_offset);
  12009. add_rt = N32_RT5 (insn);
  12010. insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
  12011. bfd_putb32 (insn, contents + irel->r_offset);
  12012. irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
  12013. }
  12014. break;
  12015. case R_NDS32_TLS_DESC_FUNC:
  12016. bfd_putb32 (INSN_NOP, contents + irel->r_offset);
  12017. irel->r_info = ELF32_R_INFO(r_symndx,
  12018. R_NDS32_RELAX_REMOVE);
  12019. break;
  12020. case R_NDS32_TLS_DESC_CALL:
  12021. {
  12022. uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
  12023. REG_TP);
  12024. bfd_putb32 (insn, contents + irel->r_offset);
  12025. irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
  12026. }
  12027. break;
  12028. case R_NDS32_LOADSTORE:
  12029. case R_NDS32_PTR:
  12030. case R_NDS32_PTR_RESOLVED:
  12031. case R_NDS32_NONE:
  12032. case R_NDS32_LABEL:
  12033. break;
  12034. default:
  12035. BFD_ASSERT(0);
  12036. break;
  12037. }
  12038. break;
  12039. case GOT_TLS_IEGP:
  12040. switch (r_type)
  12041. {
  12042. case R_NDS32_TLS_DESC_HI20:
  12043. irel->r_info = ELF32_R_INFO(r_symndx,
  12044. R_NDS32_TLS_IEGP_HI20);
  12045. break;
  12046. case R_NDS32_TLS_DESC_LO12:
  12047. irel->r_info = ELF32_R_INFO(r_symndx,
  12048. R_NDS32_TLS_IEGP_LO12);
  12049. break;
  12050. case R_NDS32_TLS_DESC_ADD:
  12051. {
  12052. uint32_t insn = bfd_getb32 (contents + irel->r_offset);
  12053. add_rt = N32_RT5 (insn);
  12054. insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
  12055. bfd_putb32 (insn, contents + irel->r_offset);
  12056. irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
  12057. }
  12058. break;
  12059. case R_NDS32_TLS_DESC_FUNC:
  12060. bfd_putb32 (INSN_NOP, contents + irel->r_offset);
  12061. irel->r_info = ELF32_R_INFO(r_symndx,
  12062. R_NDS32_RELAX_REMOVE);
  12063. break;
  12064. case R_NDS32_TLS_DESC_CALL:
  12065. {
  12066. uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
  12067. REG_TP);
  12068. bfd_putb32 (insn, contents + irel->r_offset);
  12069. irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
  12070. }
  12071. break;
  12072. case R_NDS32_LOADSTORE:
  12073. case R_NDS32_PTR:
  12074. case R_NDS32_PTR_RESOLVED:
  12075. case R_NDS32_NONE:
  12076. case R_NDS32_LABEL:
  12077. break;
  12078. default:
  12079. BFD_ASSERT(0);
  12080. break;
  12081. }
  12082. break;
  12083. case GOT_TLS_LE:
  12084. switch (r_type)
  12085. {
  12086. case R_NDS32_TLS_DESC_HI20:
  12087. irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
  12088. break;
  12089. case R_NDS32_TLS_DESC_LO12:
  12090. irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
  12091. break;
  12092. case R_NDS32_TLS_DESC_ADD:
  12093. {
  12094. uint32_t insn = bfd_getb32 (contents + irel->r_offset);
  12095. add_rt = N32_RT5 (insn);
  12096. insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
  12097. bfd_putb32 (insn, contents + irel->r_offset);
  12098. irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
  12099. }
  12100. break;
  12101. case R_NDS32_TLS_DESC_FUNC:
  12102. bfd_putb32 (INSN_NOP, contents + irel->r_offset);
  12103. irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
  12104. break;
  12105. case R_NDS32_TLS_DESC_CALL:
  12106. bfd_putb32 (INSN_NOP, contents + irel->r_offset);
  12107. irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
  12108. break;
  12109. case R_NDS32_LOADSTORE:
  12110. case R_NDS32_PTR:
  12111. case R_NDS32_PTR_RESOLVED:
  12112. case R_NDS32_NONE:
  12113. case R_NDS32_LABEL:
  12114. break;
  12115. default:
  12116. BFD_ASSERT(0);
  12117. break;
  12118. }
  12119. break;
  12120. default:
  12121. break;
  12122. }
  12123. break;
  12124. /* IEGP to IE/LE. */
  12125. case GOT_TLS_IEGP:
  12126. switch (eff_tls_type)
  12127. {
  12128. case GOT_TLS_IE:
  12129. switch (r_type)
  12130. {
  12131. case R_NDS32_TLS_IEGP_HI20:
  12132. irel->r_info = ELF32_R_INFO(r_symndx,
  12133. R_NDS32_TLS_IE_HI20);
  12134. break;
  12135. case R_NDS32_TLS_IEGP_LO12:
  12136. irel->r_info = ELF32_R_INFO(r_symndx,
  12137. R_NDS32_TLS_IE_LO12);
  12138. break;
  12139. case R_NDS32_PTR_RESOLVED:
  12140. {
  12141. uint32_t insn = bfd_getb32 (contents + irel->r_offset);
  12142. add_rt = N32_RT5 (insn);
  12143. insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
  12144. bfd_putb32 (insn, contents + irel->r_offset);
  12145. }
  12146. break;
  12147. case R_NDS32_TLS_IEGP_LW:
  12148. break;
  12149. case R_NDS32_LOADSTORE:
  12150. case R_NDS32_PTR:
  12151. case R_NDS32_NONE:
  12152. case R_NDS32_LABEL:
  12153. break;
  12154. default:
  12155. BFD_ASSERT(0);
  12156. break;
  12157. }
  12158. break;
  12159. case GOT_TLS_LE:
  12160. switch (r_type)
  12161. {
  12162. case R_NDS32_TLS_IEGP_HI20:
  12163. irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
  12164. break;
  12165. case R_NDS32_TLS_IEGP_LO12:
  12166. irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
  12167. break;
  12168. case R_NDS32_TLS_IEGP_LW:
  12169. bfd_putb32 (INSN_NOP, contents + irel->r_offset);
  12170. irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
  12171. break;
  12172. case R_NDS32_LOADSTORE:
  12173. case R_NDS32_PTR:
  12174. case R_NDS32_NONE:
  12175. case R_NDS32_LABEL:
  12176. case R_NDS32_PTR_RESOLVED:
  12177. break;
  12178. default:
  12179. BFD_ASSERT(0);
  12180. break;
  12181. }
  12182. break;
  12183. default:
  12184. break;
  12185. }
  12186. break;
  12187. /* IE to LE. */
  12188. case GOT_TLS_IE:
  12189. switch (eff_tls_type)
  12190. {
  12191. case GOT_TLS_LE:
  12192. switch (r_type)
  12193. {
  12194. case R_NDS32_TLS_IE_HI20:
  12195. irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
  12196. break;
  12197. case R_NDS32_TLS_IE_LO12S2:
  12198. {
  12199. uint32_t insn = bfd_getb32 (contents + irel->r_offset);
  12200. add_rt = N32_RT5 (insn);
  12201. insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
  12202. bfd_putb32 (insn, contents + irel->r_offset);
  12203. irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
  12204. }
  12205. break;
  12206. case R_NDS32_LOADSTORE:
  12207. case R_NDS32_PTR:
  12208. case R_NDS32_NONE:
  12209. case R_NDS32_LABEL:
  12210. break;
  12211. default:
  12212. BFD_ASSERT(0);
  12213. break;
  12214. }
  12215. break;
  12216. default:
  12217. break;
  12218. }
  12219. break;
  12220. default:
  12221. break;
  12222. }
  12223. }
  12224. pNextSig = pNextSig->next_sibling;
  12225. }
  12226. #if 1
  12227. pNext = pNext->next;
  12228. #else
  12229. while (pNext)
  12230. {
  12231. if (pNext->id != cur_grp_id)
  12232. break;
  12233. pNext = pNext->next;
  12234. }
  12235. #endif
  12236. }
  12237. finish:
  12238. if (incontents)
  12239. contents = NULL;
  12240. if (elf_section_data (insec)->relocs != internal_relocs)
  12241. free (internal_relocs);
  12242. if (elf_section_data (insec)->this_hdr.contents != contents)
  12243. free (contents);
  12244. if (symtab_hdr->contents != (bfd_byte *) local_syms)
  12245. free (local_syms);
  12246. if (chain.next)
  12247. {
  12248. pNext = chain.next;
  12249. relax_group_list_t *pDel;
  12250. while (pNext)
  12251. {
  12252. pDel = pNext;
  12253. pNext = pNext->next;
  12254. free (pDel);
  12255. }
  12256. }
  12257. return result;
  12258. error_return:
  12259. result = false;
  12260. goto finish;
  12261. }
  12262. /* End TLS model conversion. */
  12263. #define ELF_ARCH bfd_arch_nds32
  12264. #define ELF_MACHINE_CODE EM_NDS32
  12265. #define ELF_MAXPAGESIZE 0x1000
  12266. #define ELF_TARGET_ID NDS32_ELF_DATA
  12267. #define TARGET_BIG_SYM nds32_elf32_be_vec
  12268. #define TARGET_BIG_NAME "elf32-nds32be"
  12269. #define TARGET_LITTLE_SYM nds32_elf32_le_vec
  12270. #define TARGET_LITTLE_NAME "elf32-nds32le"
  12271. #define elf_info_to_howto nds32_info_to_howto
  12272. #define elf_info_to_howto_rel nds32_info_to_howto_rel
  12273. #define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
  12274. #define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
  12275. #define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
  12276. #define bfd_elf32_bfd_relax_section nds32_elf_relax_section
  12277. #define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
  12278. #define bfd_elf32_mkobject nds32_elf_mkobject
  12279. #define elf_backend_action_discarded nds32_elf_action_discarded
  12280. #define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
  12281. #define elf_backend_check_relocs nds32_elf_check_relocs
  12282. #define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
  12283. #define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
  12284. #define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
  12285. #define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
  12286. #define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
  12287. #define elf_backend_relocate_section nds32_elf_relocate_section
  12288. #define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
  12289. #define elf_backend_grok_prstatus nds32_elf_grok_prstatus
  12290. #define elf_backend_grok_psinfo nds32_elf_grok_psinfo
  12291. #define elf_backend_reloc_type_class nds32_elf_reloc_type_class
  12292. #define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
  12293. #define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
  12294. #define elf_backend_output_arch_syms nds32_elf_output_arch_syms
  12295. #define elf_backend_object_p nds32_elf_object_p
  12296. #define elf_backend_final_write_processing nds32_elf_final_write_processing
  12297. #define elf_backend_special_sections nds32_elf_special_sections
  12298. #define elf_backend_section_flags nds32_elf_section_flags
  12299. #define bfd_elf32_bfd_get_relocated_section_contents \
  12300. nds32_elf_get_relocated_section_contents
  12301. #define bfd_elf32_bfd_is_target_special_symbol nds32_elf_is_target_special_symbol
  12302. #define elf_backend_maybe_function_sym nds32_elf_maybe_function_sym
  12303. #define elf_backend_can_gc_sections 1
  12304. #define elf_backend_can_refcount 1
  12305. #define elf_backend_want_got_plt 1
  12306. #define elf_backend_plt_readonly 1
  12307. #define elf_backend_want_plt_sym 0
  12308. #define elf_backend_got_header_size 12
  12309. #define elf_backend_may_use_rel_p 1
  12310. #define elf_backend_default_use_rela_p 1
  12311. #define elf_backend_may_use_rela_p 1
  12312. #define elf_backend_dtrel_excludes_plt 0
  12313. #include "elf32-target.h"
  12314. #undef ELF_MAXPAGESIZE
  12315. #define ELF_MAXPAGESIZE 0x2000
  12316. #undef TARGET_BIG_SYM
  12317. #define TARGET_BIG_SYM nds32_elf32_linux_be_vec
  12318. #undef TARGET_BIG_NAME
  12319. #define TARGET_BIG_NAME "elf32-nds32be-linux"
  12320. #undef TARGET_LITTLE_SYM
  12321. #define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
  12322. #undef TARGET_LITTLE_NAME
  12323. #define TARGET_LITTLE_NAME "elf32-nds32le-linux"
  12324. #undef elf32_bed
  12325. #define elf32_bed elf32_nds32_lin_bed
  12326. #include "elf32-target.h"