arm.cc 413 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920
  1. // arm.cc -- arm target support for gold.
  2. // Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
  4. // by Ian Lance Taylor <iant@google.com>.
  5. // This file also contains borrowed and adapted code from
  6. // bfd/elf32-arm.c.
  7. // This file is part of gold.
  8. // This program is free software; you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation; either version 3 of the License, or
  11. // (at your option) any later version.
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  19. // MA 02110-1301, USA.
  20. #include "gold.h"
  21. #include <cstring>
  22. #include <limits>
  23. #include <cstdio>
  24. #include <string>
  25. #include <algorithm>
  26. #include <map>
  27. #include <utility>
  28. #include <set>
  29. #include "elfcpp.h"
  30. #include "parameters.h"
  31. #include "reloc.h"
  32. #include "arm.h"
  33. #include "object.h"
  34. #include "symtab.h"
  35. #include "layout.h"
  36. #include "output.h"
  37. #include "copy-relocs.h"
  38. #include "target.h"
  39. #include "target-reloc.h"
  40. #include "target-select.h"
  41. #include "tls.h"
  42. #include "defstd.h"
  43. #include "gc.h"
  44. #include "attributes.h"
  45. #include "arm-reloc-property.h"
  46. #include "nacl.h"
  47. namespace
  48. {
  49. using namespace gold;
  50. template<bool big_endian>
  51. class Output_data_plt_arm;
  52. template<bool big_endian>
  53. class Output_data_plt_arm_standard;
  54. template<bool big_endian>
  55. class Stub_table;
  56. template<bool big_endian>
  57. class Arm_input_section;
  58. class Arm_exidx_cantunwind;
  59. class Arm_exidx_merged_section;
  60. class Arm_exidx_fixup;
  61. template<bool big_endian>
  62. class Arm_output_section;
  63. class Arm_exidx_input_section;
  64. template<bool big_endian>
  65. class Arm_relobj;
  66. template<bool big_endian>
  67. class Arm_relocate_functions;
  68. template<bool big_endian>
  69. class Arm_output_data_got;
  70. template<bool big_endian>
  71. class Target_arm;
  72. // For convenience.
  73. typedef elfcpp::Elf_types<32>::Elf_Addr Arm_address;
  74. // Maximum branch offsets for ARM, THUMB and THUMB2.
  75. const int32_t ARM_MAX_FWD_BRANCH_OFFSET = ((((1 << 23) - 1) << 2) + 8);
  76. const int32_t ARM_MAX_BWD_BRANCH_OFFSET = ((-((1 << 23) << 2)) + 8);
  77. const int32_t THM_MAX_FWD_BRANCH_OFFSET = ((1 << 22) -2 + 4);
  78. const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
  79. const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
  80. const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
  81. // Thread Control Block size.
  82. const size_t ARM_TCB_SIZE = 8;
  83. // The arm target class.
  84. //
  85. // This is a very simple port of gold for ARM-EABI. It is intended for
  86. // supporting Android only for the time being.
  87. //
  88. // TODOs:
  89. // - Implement all static relocation types documented in arm-reloc.def.
  90. // - Make PLTs more flexible for different architecture features like
  91. // Thumb-2 and BE8.
  92. // There are probably a lot more.
  93. // Ideally we would like to avoid using global variables but this is used
  94. // very in many places and sometimes in loops. If we use a function
  95. // returning a static instance of Arm_reloc_property_table, it will be very
  96. // slow in an threaded environment since the static instance needs to be
  97. // locked. The pointer is below initialized in the
  98. // Target::do_select_as_default_target() hook so that we do not spend time
  99. // building the table if we are not linking ARM objects.
  100. //
  101. // An alternative is to to process the information in arm-reloc.def in
  102. // compilation time and generate a representation of it in PODs only. That
  103. // way we can avoid initialization when the linker starts.
  104. Arm_reloc_property_table* arm_reloc_property_table = NULL;
  105. // Instruction template class. This class is similar to the insn_sequence
  106. // struct in bfd/elf32-arm.c.
  107. class Insn_template
  108. {
  109. public:
  110. // Types of instruction templates.
  111. enum Type
  112. {
  113. THUMB16_TYPE = 1,
  114. // THUMB16_SPECIAL_TYPE is used by sub-classes of Stub for instruction
  115. // templates with class-specific semantics. Currently this is used
  116. // only by the Cortex_a8_stub class for handling condition codes in
  117. // conditional branches.
  118. THUMB16_SPECIAL_TYPE,
  119. THUMB32_TYPE,
  120. ARM_TYPE,
  121. DATA_TYPE
  122. };
  123. // Factory methods to create instruction templates in different formats.
  124. static const Insn_template
  125. thumb16_insn(uint32_t data)
  126. { return Insn_template(data, THUMB16_TYPE, elfcpp::R_ARM_NONE, 0); }
  127. // A Thumb conditional branch, in which the proper condition is inserted
  128. // when we build the stub.
  129. static const Insn_template
  130. thumb16_bcond_insn(uint32_t data)
  131. { return Insn_template(data, THUMB16_SPECIAL_TYPE, elfcpp::R_ARM_NONE, 1); }
  132. static const Insn_template
  133. thumb32_insn(uint32_t data)
  134. { return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_NONE, 0); }
  135. static const Insn_template
  136. thumb32_b_insn(uint32_t data, int reloc_addend)
  137. {
  138. return Insn_template(data, THUMB32_TYPE, elfcpp::R_ARM_THM_JUMP24,
  139. reloc_addend);
  140. }
  141. static const Insn_template
  142. arm_insn(uint32_t data)
  143. { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_NONE, 0); }
  144. static const Insn_template
  145. arm_rel_insn(unsigned data, int reloc_addend)
  146. { return Insn_template(data, ARM_TYPE, elfcpp::R_ARM_JUMP24, reloc_addend); }
  147. static const Insn_template
  148. data_word(unsigned data, unsigned int r_type, int reloc_addend)
  149. { return Insn_template(data, DATA_TYPE, r_type, reloc_addend); }
  150. // Accessors. This class is used for read-only objects so no modifiers
  151. // are provided.
  152. uint32_t
  153. data() const
  154. { return this->data_; }
  155. // Return the instruction sequence type of this.
  156. Type
  157. type() const
  158. { return this->type_; }
  159. // Return the ARM relocation type of this.
  160. unsigned int
  161. r_type() const
  162. { return this->r_type_; }
  163. int32_t
  164. reloc_addend() const
  165. { return this->reloc_addend_; }
  166. // Return size of instruction template in bytes.
  167. size_t
  168. size() const;
  169. // Return byte-alignment of instruction template.
  170. unsigned
  171. alignment() const;
  172. private:
  173. // We make the constructor private to ensure that only the factory
  174. // methods are used.
  175. inline
  176. Insn_template(unsigned data, Type type, unsigned int r_type, int reloc_addend)
  177. : data_(data), type_(type), r_type_(r_type), reloc_addend_(reloc_addend)
  178. { }
  179. // Instruction specific data. This is used to store information like
  180. // some of the instruction bits.
  181. uint32_t data_;
  182. // Instruction template type.
  183. Type type_;
  184. // Relocation type if there is a relocation or R_ARM_NONE otherwise.
  185. unsigned int r_type_;
  186. // Relocation addend.
  187. int32_t reloc_addend_;
  188. };
  189. // Macro for generating code to stub types. One entry per long/short
  190. // branch stub
  191. #define DEF_STUBS \
  192. DEF_STUB(long_branch_any_any) \
  193. DEF_STUB(long_branch_v4t_arm_thumb) \
  194. DEF_STUB(long_branch_thumb_only) \
  195. DEF_STUB(long_branch_v4t_thumb_thumb) \
  196. DEF_STUB(long_branch_v4t_thumb_arm) \
  197. DEF_STUB(short_branch_v4t_thumb_arm) \
  198. DEF_STUB(long_branch_any_arm_pic) \
  199. DEF_STUB(long_branch_any_thumb_pic) \
  200. DEF_STUB(long_branch_v4t_thumb_thumb_pic) \
  201. DEF_STUB(long_branch_v4t_arm_thumb_pic) \
  202. DEF_STUB(long_branch_v4t_thumb_arm_pic) \
  203. DEF_STUB(long_branch_thumb_only_pic) \
  204. DEF_STUB(a8_veneer_b_cond) \
  205. DEF_STUB(a8_veneer_b) \
  206. DEF_STUB(a8_veneer_bl) \
  207. DEF_STUB(a8_veneer_blx) \
  208. DEF_STUB(v4_veneer_bx)
  209. // Stub types.
  210. #define DEF_STUB(x) arm_stub_##x,
  211. typedef enum
  212. {
  213. arm_stub_none,
  214. DEF_STUBS
  215. // First reloc stub type.
  216. arm_stub_reloc_first = arm_stub_long_branch_any_any,
  217. // Last reloc stub type.
  218. arm_stub_reloc_last = arm_stub_long_branch_thumb_only_pic,
  219. // First Cortex-A8 stub type.
  220. arm_stub_cortex_a8_first = arm_stub_a8_veneer_b_cond,
  221. // Last Cortex-A8 stub type.
  222. arm_stub_cortex_a8_last = arm_stub_a8_veneer_blx,
  223. // Last stub type.
  224. arm_stub_type_last = arm_stub_v4_veneer_bx
  225. } Stub_type;
  226. #undef DEF_STUB
  227. // Stub template class. Templates are meant to be read-only objects.
  228. // A stub template for a stub type contains all read-only attributes
  229. // common to all stubs of the same type.
  230. class Stub_template
  231. {
  232. public:
  233. Stub_template(Stub_type, const Insn_template*, size_t);
  234. ~Stub_template()
  235. { }
  236. // Return stub type.
  237. Stub_type
  238. type() const
  239. { return this->type_; }
  240. // Return an array of instruction templates.
  241. const Insn_template*
  242. insns() const
  243. { return this->insns_; }
  244. // Return size of template in number of instructions.
  245. size_t
  246. insn_count() const
  247. { return this->insn_count_; }
  248. // Return size of template in bytes.
  249. size_t
  250. size() const
  251. { return this->size_; }
  252. // Return alignment of the stub template.
  253. unsigned
  254. alignment() const
  255. { return this->alignment_; }
  256. // Return whether entry point is in thumb mode.
  257. bool
  258. entry_in_thumb_mode() const
  259. { return this->entry_in_thumb_mode_; }
  260. // Return number of relocations in this template.
  261. size_t
  262. reloc_count() const
  263. { return this->relocs_.size(); }
  264. // Return index of the I-th instruction with relocation.
  265. size_t
  266. reloc_insn_index(size_t i) const
  267. {
  268. gold_assert(i < this->relocs_.size());
  269. return this->relocs_[i].first;
  270. }
  271. // Return the offset of the I-th instruction with relocation from the
  272. // beginning of the stub.
  273. section_size_type
  274. reloc_offset(size_t i) const
  275. {
  276. gold_assert(i < this->relocs_.size());
  277. return this->relocs_[i].second;
  278. }
  279. private:
  280. // This contains information about an instruction template with a relocation
  281. // and its offset from start of stub.
  282. typedef std::pair<size_t, section_size_type> Reloc;
  283. // A Stub_template may not be copied. We want to share templates as much
  284. // as possible.
  285. Stub_template(const Stub_template&);
  286. Stub_template& operator=(const Stub_template&);
  287. // Stub type.
  288. Stub_type type_;
  289. // Points to an array of Insn_templates.
  290. const Insn_template* insns_;
  291. // Number of Insn_templates in insns_[].
  292. size_t insn_count_;
  293. // Size of templated instructions in bytes.
  294. size_t size_;
  295. // Alignment of templated instructions.
  296. unsigned alignment_;
  297. // Flag to indicate if entry is in thumb mode.
  298. bool entry_in_thumb_mode_;
  299. // A table of reloc instruction indices and offsets. We can find these by
  300. // looking at the instruction templates but we pre-compute and then stash
  301. // them here for speed.
  302. std::vector<Reloc> relocs_;
  303. };
  304. //
  305. // A class for code stubs. This is a base class for different type of
  306. // stubs used in the ARM target.
  307. //
  308. class Stub
  309. {
  310. private:
  311. static const section_offset_type invalid_offset =
  312. static_cast<section_offset_type>(-1);
  313. public:
  314. Stub(const Stub_template* stub_template)
  315. : stub_template_(stub_template), offset_(invalid_offset)
  316. { }
  317. virtual
  318. ~Stub()
  319. { }
  320. // Return the stub template.
  321. const Stub_template*
  322. stub_template() const
  323. { return this->stub_template_; }
  324. // Return offset of code stub from beginning of its containing stub table.
  325. section_offset_type
  326. offset() const
  327. {
  328. gold_assert(this->offset_ != invalid_offset);
  329. return this->offset_;
  330. }
  331. // Set offset of code stub from beginning of its containing stub table.
  332. void
  333. set_offset(section_offset_type offset)
  334. { this->offset_ = offset; }
  335. // Return the relocation target address of the i-th relocation in the
  336. // stub. This must be defined in a child class.
  337. Arm_address
  338. reloc_target(size_t i)
  339. { return this->do_reloc_target(i); }
  340. // Write a stub at output VIEW. BIG_ENDIAN select how a stub is written.
  341. void
  342. write(unsigned char* view, section_size_type view_size, bool big_endian)
  343. { this->do_write(view, view_size, big_endian); }
  344. // Return the instruction for THUMB16_SPECIAL_TYPE instruction template
  345. // for the i-th instruction.
  346. uint16_t
  347. thumb16_special(size_t i)
  348. { return this->do_thumb16_special(i); }
  349. protected:
  350. // This must be defined in the child class.
  351. virtual Arm_address
  352. do_reloc_target(size_t) = 0;
  353. // This may be overridden in the child class.
  354. virtual void
  355. do_write(unsigned char* view, section_size_type view_size, bool big_endian)
  356. {
  357. if (big_endian)
  358. this->do_fixed_endian_write<true>(view, view_size);
  359. else
  360. this->do_fixed_endian_write<false>(view, view_size);
  361. }
  362. // This must be overridden if a child class uses the THUMB16_SPECIAL_TYPE
  363. // instruction template.
  364. virtual uint16_t
  365. do_thumb16_special(size_t)
  366. { gold_unreachable(); }
  367. private:
  368. // A template to implement do_write.
  369. template<bool big_endian>
  370. void inline
  371. do_fixed_endian_write(unsigned char*, section_size_type);
  372. // Its template.
  373. const Stub_template* stub_template_;
  374. // Offset within the section of containing this stub.
  375. section_offset_type offset_;
  376. };
  377. // Reloc stub class. These are stubs we use to fix up relocation because
  378. // of limited branch ranges.
  379. class Reloc_stub : public Stub
  380. {
  381. public:
  382. static const unsigned int invalid_index = static_cast<unsigned int>(-1);
  383. // We assume we never jump to this address.
  384. static const Arm_address invalid_address = static_cast<Arm_address>(-1);
  385. // Return destination address.
  386. Arm_address
  387. destination_address() const
  388. {
  389. gold_assert(this->destination_address_ != this->invalid_address);
  390. return this->destination_address_;
  391. }
  392. // Set destination address.
  393. void
  394. set_destination_address(Arm_address address)
  395. {
  396. gold_assert(address != this->invalid_address);
  397. this->destination_address_ = address;
  398. }
  399. // Reset destination address.
  400. void
  401. reset_destination_address()
  402. { this->destination_address_ = this->invalid_address; }
  403. // Determine stub type for a branch of a relocation of R_TYPE going
  404. // from BRANCH_ADDRESS to BRANCH_TARGET. If TARGET_IS_THUMB is set,
  405. // the branch target is a thumb instruction. TARGET is used for look
  406. // up ARM-specific linker settings.
  407. static Stub_type
  408. stub_type_for_reloc(unsigned int r_type, Arm_address branch_address,
  409. Arm_address branch_target, bool target_is_thumb);
  410. // Reloc_stub key. A key is logically a triplet of a stub type, a symbol
  411. // and an addend. Since we treat global and local symbol differently, we
  412. // use a Symbol object for a global symbol and a object-index pair for
  413. // a local symbol.
  414. class Key
  415. {
  416. public:
  417. // If SYMBOL is not null, this is a global symbol, we ignore RELOBJ and
  418. // R_SYM. Otherwise, this is a local symbol and RELOBJ must non-NULL
  419. // and R_SYM must not be invalid_index.
  420. Key(Stub_type stub_type, const Symbol* symbol, const Relobj* relobj,
  421. unsigned int r_sym, int32_t addend)
  422. : stub_type_(stub_type), addend_(addend)
  423. {
  424. if (symbol != NULL)
  425. {
  426. this->r_sym_ = Reloc_stub::invalid_index;
  427. this->u_.symbol = symbol;
  428. }
  429. else
  430. {
  431. gold_assert(relobj != NULL && r_sym != invalid_index);
  432. this->r_sym_ = r_sym;
  433. this->u_.relobj = relobj;
  434. }
  435. }
  436. ~Key()
  437. { }
  438. // Accessors: Keys are meant to be read-only object so no modifiers are
  439. // provided.
  440. // Return stub type.
  441. Stub_type
  442. stub_type() const
  443. { return this->stub_type_; }
  444. // Return the local symbol index or invalid_index.
  445. unsigned int
  446. r_sym() const
  447. { return this->r_sym_; }
  448. // Return the symbol if there is one.
  449. const Symbol*
  450. symbol() const
  451. { return this->r_sym_ == invalid_index ? this->u_.symbol : NULL; }
  452. // Return the relobj if there is one.
  453. const Relobj*
  454. relobj() const
  455. { return this->r_sym_ != invalid_index ? this->u_.relobj : NULL; }
  456. // Whether this equals to another key k.
  457. bool
  458. eq(const Key& k) const
  459. {
  460. return ((this->stub_type_ == k.stub_type_)
  461. && (this->r_sym_ == k.r_sym_)
  462. && ((this->r_sym_ != Reloc_stub::invalid_index)
  463. ? (this->u_.relobj == k.u_.relobj)
  464. : (this->u_.symbol == k.u_.symbol))
  465. && (this->addend_ == k.addend_));
  466. }
  467. // Return a hash value.
  468. size_t
  469. hash_value() const
  470. {
  471. return (this->stub_type_
  472. ^ this->r_sym_
  473. ^ gold::string_hash<char>(
  474. (this->r_sym_ != Reloc_stub::invalid_index)
  475. ? this->u_.relobj->name().c_str()
  476. : this->u_.symbol->name())
  477. ^ this->addend_);
  478. }
  479. // Functors for STL associative containers.
  480. struct hash
  481. {
  482. size_t
  483. operator()(const Key& k) const
  484. { return k.hash_value(); }
  485. };
  486. struct equal_to
  487. {
  488. bool
  489. operator()(const Key& k1, const Key& k2) const
  490. { return k1.eq(k2); }
  491. };
  492. // Name of key. This is mainly for debugging.
  493. std::string
  494. name() const;
  495. private:
  496. // Stub type.
  497. Stub_type stub_type_;
  498. // If this is a local symbol, this is the index in the defining object.
  499. // Otherwise, it is invalid_index for a global symbol.
  500. unsigned int r_sym_;
  501. // If r_sym_ is an invalid index, this points to a global symbol.
  502. // Otherwise, it points to a relobj. We used the unsized and target
  503. // independent Symbol and Relobj classes instead of Sized_symbol<32> and
  504. // Arm_relobj, in order to avoid making the stub class a template
  505. // as most of the stub machinery is endianness-neutral. However, it
  506. // may require a bit of casting done by users of this class.
  507. union
  508. {
  509. const Symbol* symbol;
  510. const Relobj* relobj;
  511. } u_;
  512. // Addend associated with a reloc.
  513. int32_t addend_;
  514. };
  515. protected:
  516. // Reloc_stubs are created via a stub factory. So these are protected.
  517. Reloc_stub(const Stub_template* stub_template)
  518. : Stub(stub_template), destination_address_(invalid_address)
  519. { }
  520. ~Reloc_stub()
  521. { }
  522. friend class Stub_factory;
  523. // Return the relocation target address of the i-th relocation in the
  524. // stub.
  525. Arm_address
  526. do_reloc_target(size_t i)
  527. {
  528. // All reloc stub have only one relocation.
  529. gold_assert(i == 0);
  530. return this->destination_address_;
  531. }
  532. private:
  533. // Address of destination.
  534. Arm_address destination_address_;
  535. };
  536. // Cortex-A8 stub class. We need a Cortex-A8 stub to redirect any 32-bit
  537. // THUMB branch that meets the following conditions:
  538. //
  539. // 1. The branch straddles across a page boundary. i.e. lower 12-bit of
  540. // branch address is 0xffe.
  541. // 2. The branch target address is in the same page as the first word of the
  542. // branch.
  543. // 3. The branch follows a 32-bit instruction which is not a branch.
  544. //
  545. // To do the fix up, we need to store the address of the branch instruction
  546. // and its target at least. We also need to store the original branch
  547. // instruction bits for the condition code in a conditional branch. The
  548. // condition code is used in a special instruction template. We also want
  549. // to identify input sections needing Cortex-A8 workaround quickly. We store
  550. // extra information about object and section index of the code section
  551. // containing a branch being fixed up. The information is used to mark
  552. // the code section when we finalize the Cortex-A8 stubs.
  553. //
  554. class Cortex_a8_stub : public Stub
  555. {
  556. public:
  557. ~Cortex_a8_stub()
  558. { }
  559. // Return the object of the code section containing the branch being fixed
  560. // up.
  561. Relobj*
  562. relobj() const
  563. { return this->relobj_; }
  564. // Return the section index of the code section containing the branch being
  565. // fixed up.
  566. unsigned int
  567. shndx() const
  568. { return this->shndx_; }
  569. // Return the source address of stub. This is the address of the original
  570. // branch instruction. LSB is 1 always set to indicate that it is a THUMB
  571. // instruction.
  572. Arm_address
  573. source_address() const
  574. { return this->source_address_; }
  575. // Return the destination address of the stub. This is the branch taken
  576. // address of the original branch instruction. LSB is 1 if it is a THUMB
  577. // instruction address.
  578. Arm_address
  579. destination_address() const
  580. { return this->destination_address_; }
  581. // Return the instruction being fixed up.
  582. uint32_t
  583. original_insn() const
  584. { return this->original_insn_; }
  585. protected:
  586. // Cortex_a8_stubs are created via a stub factory. So these are protected.
  587. Cortex_a8_stub(const Stub_template* stub_template, Relobj* relobj,
  588. unsigned int shndx, Arm_address source_address,
  589. Arm_address destination_address, uint32_t original_insn)
  590. : Stub(stub_template), relobj_(relobj), shndx_(shndx),
  591. source_address_(source_address | 1U),
  592. destination_address_(destination_address),
  593. original_insn_(original_insn)
  594. { }
  595. friend class Stub_factory;
  596. // Return the relocation target address of the i-th relocation in the
  597. // stub.
  598. Arm_address
  599. do_reloc_target(size_t i)
  600. {
  601. if (this->stub_template()->type() == arm_stub_a8_veneer_b_cond)
  602. {
  603. // The conditional branch veneer has two relocations.
  604. gold_assert(i < 2);
  605. return i == 0 ? this->source_address_ + 4 : this->destination_address_;
  606. }
  607. else
  608. {
  609. // All other Cortex-A8 stubs have only one relocation.
  610. gold_assert(i == 0);
  611. return this->destination_address_;
  612. }
  613. }
  614. // Return an instruction for the THUMB16_SPECIAL_TYPE instruction template.
  615. uint16_t
  616. do_thumb16_special(size_t);
  617. private:
  618. // Object of the code section containing the branch being fixed up.
  619. Relobj* relobj_;
  620. // Section index of the code section containing the branch begin fixed up.
  621. unsigned int shndx_;
  622. // Source address of original branch.
  623. Arm_address source_address_;
  624. // Destination address of the original branch.
  625. Arm_address destination_address_;
  626. // Original branch instruction. This is needed for copying the condition
  627. // code from a condition branch to its stub.
  628. uint32_t original_insn_;
  629. };
  630. // ARMv4 BX Rx branch relocation stub class.
  631. class Arm_v4bx_stub : public Stub
  632. {
  633. public:
  634. ~Arm_v4bx_stub()
  635. { }
  636. // Return the associated register.
  637. uint32_t
  638. reg() const
  639. { return this->reg_; }
  640. protected:
  641. // Arm V4BX stubs are created via a stub factory. So these are protected.
  642. Arm_v4bx_stub(const Stub_template* stub_template, const uint32_t reg)
  643. : Stub(stub_template), reg_(reg)
  644. { }
  645. friend class Stub_factory;
  646. // Return the relocation target address of the i-th relocation in the
  647. // stub.
  648. Arm_address
  649. do_reloc_target(size_t)
  650. { gold_unreachable(); }
  651. // This may be overridden in the child class.
  652. virtual void
  653. do_write(unsigned char* view, section_size_type view_size, bool big_endian)
  654. {
  655. if (big_endian)
  656. this->do_fixed_endian_v4bx_write<true>(view, view_size);
  657. else
  658. this->do_fixed_endian_v4bx_write<false>(view, view_size);
  659. }
  660. private:
  661. // A template to implement do_write.
  662. template<bool big_endian>
  663. void inline
  664. do_fixed_endian_v4bx_write(unsigned char* view, section_size_type)
  665. {
  666. const Insn_template* insns = this->stub_template()->insns();
  667. elfcpp::Swap<32, big_endian>::writeval(view,
  668. (insns[0].data()
  669. + (this->reg_ << 16)));
  670. view += insns[0].size();
  671. elfcpp::Swap<32, big_endian>::writeval(view,
  672. (insns[1].data() + this->reg_));
  673. view += insns[1].size();
  674. elfcpp::Swap<32, big_endian>::writeval(view,
  675. (insns[2].data() + this->reg_));
  676. }
  677. // A register index (r0-r14), which is associated with the stub.
  678. uint32_t reg_;
  679. };
  680. // Stub factory class.
  681. class Stub_factory
  682. {
  683. public:
  684. // Return the unique instance of this class.
  685. static const Stub_factory&
  686. get_instance()
  687. {
  688. static Stub_factory singleton;
  689. return singleton;
  690. }
  691. // Make a relocation stub.
  692. Reloc_stub*
  693. make_reloc_stub(Stub_type stub_type) const
  694. {
  695. gold_assert(stub_type >= arm_stub_reloc_first
  696. && stub_type <= arm_stub_reloc_last);
  697. return new Reloc_stub(this->stub_templates_[stub_type]);
  698. }
  699. // Make a Cortex-A8 stub.
  700. Cortex_a8_stub*
  701. make_cortex_a8_stub(Stub_type stub_type, Relobj* relobj, unsigned int shndx,
  702. Arm_address source, Arm_address destination,
  703. uint32_t original_insn) const
  704. {
  705. gold_assert(stub_type >= arm_stub_cortex_a8_first
  706. && stub_type <= arm_stub_cortex_a8_last);
  707. return new Cortex_a8_stub(this->stub_templates_[stub_type], relobj, shndx,
  708. source, destination, original_insn);
  709. }
  710. // Make an ARM V4BX relocation stub.
  711. // This method creates a stub from the arm_stub_v4_veneer_bx template only.
  712. Arm_v4bx_stub*
  713. make_arm_v4bx_stub(uint32_t reg) const
  714. {
  715. gold_assert(reg < 0xf);
  716. return new Arm_v4bx_stub(this->stub_templates_[arm_stub_v4_veneer_bx],
  717. reg);
  718. }
  719. private:
  720. // Constructor and destructor are protected since we only return a single
  721. // instance created in Stub_factory::get_instance().
  722. Stub_factory();
  723. // A Stub_factory may not be copied since it is a singleton.
  724. Stub_factory(const Stub_factory&);
  725. Stub_factory& operator=(Stub_factory&);
  726. // Stub templates. These are initialized in the constructor.
  727. const Stub_template* stub_templates_[arm_stub_type_last+1];
  728. };
  729. // A class to hold stubs for the ARM target.
  730. template<bool big_endian>
  731. class Stub_table : public Output_data
  732. {
  733. public:
  734. Stub_table(Arm_input_section<big_endian>* owner)
  735. : Output_data(), owner_(owner), reloc_stubs_(), reloc_stubs_size_(0),
  736. reloc_stubs_addralign_(1), cortex_a8_stubs_(), arm_v4bx_stubs_(0xf),
  737. prev_data_size_(0), prev_addralign_(1)
  738. { }
  739. ~Stub_table()
  740. { }
  741. // Owner of this stub table.
  742. Arm_input_section<big_endian>*
  743. owner() const
  744. { return this->owner_; }
  745. // Whether this stub table is empty.
  746. bool
  747. empty() const
  748. {
  749. return (this->reloc_stubs_.empty()
  750. && this->cortex_a8_stubs_.empty()
  751. && this->arm_v4bx_stubs_.empty());
  752. }
  753. // Return the current data size.
  754. off_t
  755. current_data_size() const
  756. { return this->current_data_size_for_child(); }
  757. // Add a STUB using KEY. The caller is responsible for avoiding addition
  758. // if a STUB with the same key has already been added.
  759. void
  760. add_reloc_stub(Reloc_stub* stub, const Reloc_stub::Key& key)
  761. {
  762. const Stub_template* stub_template = stub->stub_template();
  763. gold_assert(stub_template->type() == key.stub_type());
  764. this->reloc_stubs_[key] = stub;
  765. // Assign stub offset early. We can do this because we never remove
  766. // reloc stubs and they are in the beginning of the stub table.
  767. uint64_t align = stub_template->alignment();
  768. this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_, align);
  769. stub->set_offset(this->reloc_stubs_size_);
  770. this->reloc_stubs_size_ += stub_template->size();
  771. this->reloc_stubs_addralign_ =
  772. std::max(this->reloc_stubs_addralign_, align);
  773. }
  774. // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
  775. // The caller is responsible for avoiding addition if a STUB with the same
  776. // address has already been added.
  777. void
  778. add_cortex_a8_stub(Arm_address address, Cortex_a8_stub* stub)
  779. {
  780. std::pair<Arm_address, Cortex_a8_stub*> value(address, stub);
  781. this->cortex_a8_stubs_.insert(value);
  782. }
  783. // Add an ARM V4BX relocation stub. A register index will be retrieved
  784. // from the stub.
  785. void
  786. add_arm_v4bx_stub(Arm_v4bx_stub* stub)
  787. {
  788. gold_assert(stub != NULL && this->arm_v4bx_stubs_[stub->reg()] == NULL);
  789. this->arm_v4bx_stubs_[stub->reg()] = stub;
  790. }
  791. // Remove all Cortex-A8 stubs.
  792. void
  793. remove_all_cortex_a8_stubs();
  794. // Look up a relocation stub using KEY. Return NULL if there is none.
  795. Reloc_stub*
  796. find_reloc_stub(const Reloc_stub::Key& key) const
  797. {
  798. typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.find(key);
  799. return (p != this->reloc_stubs_.end()) ? p->second : NULL;
  800. }
  801. // Look up an arm v4bx relocation stub using the register index.
  802. // Return NULL if there is none.
  803. Arm_v4bx_stub*
  804. find_arm_v4bx_stub(const uint32_t reg) const
  805. {
  806. gold_assert(reg < 0xf);
  807. return this->arm_v4bx_stubs_[reg];
  808. }
  809. // Relocate stubs in this stub table.
  810. void
  811. relocate_stubs(const Relocate_info<32, big_endian>*,
  812. Target_arm<big_endian>*, Output_section*,
  813. unsigned char*, Arm_address, section_size_type);
  814. // Update data size and alignment at the end of a relaxation pass. Return
  815. // true if either data size or alignment is different from that of the
  816. // previous relaxation pass.
  817. bool
  818. update_data_size_and_addralign();
  819. // Finalize stubs. Set the offsets of all stubs and mark input sections
  820. // needing the Cortex-A8 workaround.
  821. void
  822. finalize_stubs();
  823. // Apply Cortex-A8 workaround to an address range.
  824. void
  825. apply_cortex_a8_workaround_to_address_range(Target_arm<big_endian>*,
  826. unsigned char*, Arm_address,
  827. section_size_type);
  828. protected:
  829. // Write out section contents.
  830. void
  831. do_write(Output_file*);
  832. // Return the required alignment.
  833. uint64_t
  834. do_addralign() const
  835. { return this->prev_addralign_; }
  836. // Reset address and file offset.
  837. void
  838. do_reset_address_and_file_offset()
  839. { this->set_current_data_size_for_child(this->prev_data_size_); }
  840. // Set final data size.
  841. void
  842. set_final_data_size()
  843. { this->set_data_size(this->current_data_size()); }
  844. private:
  845. // Relocate one stub.
  846. void
  847. relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
  848. Target_arm<big_endian>*, Output_section*,
  849. unsigned char*, Arm_address, section_size_type);
  850. // Unordered map of relocation stubs.
  851. typedef
  852. Unordered_map<Reloc_stub::Key, Reloc_stub*, Reloc_stub::Key::hash,
  853. Reloc_stub::Key::equal_to>
  854. Reloc_stub_map;
  855. // List of Cortex-A8 stubs ordered by addresses of branches being
  856. // fixed up in output.
  857. typedef std::map<Arm_address, Cortex_a8_stub*> Cortex_a8_stub_list;
  858. // List of Arm V4BX relocation stubs ordered by associated registers.
  859. typedef std::vector<Arm_v4bx_stub*> Arm_v4bx_stub_list;
  860. // Owner of this stub table.
  861. Arm_input_section<big_endian>* owner_;
  862. // The relocation stubs.
  863. Reloc_stub_map reloc_stubs_;
  864. // Size of reloc stubs.
  865. off_t reloc_stubs_size_;
  866. // Maximum address alignment of reloc stubs.
  867. uint64_t reloc_stubs_addralign_;
  868. // The cortex_a8_stubs.
  869. Cortex_a8_stub_list cortex_a8_stubs_;
  870. // The Arm V4BX relocation stubs.
  871. Arm_v4bx_stub_list arm_v4bx_stubs_;
  872. // data size of this in the previous pass.
  873. off_t prev_data_size_;
  874. // address alignment of this in the previous pass.
  875. uint64_t prev_addralign_;
  876. };
  877. // Arm_exidx_cantunwind class. This represents an EXIDX_CANTUNWIND entry
  878. // we add to the end of an EXIDX input section that goes into the output.
  879. class Arm_exidx_cantunwind : public Output_section_data
  880. {
  881. public:
  882. Arm_exidx_cantunwind(Relobj* relobj, unsigned int shndx)
  883. : Output_section_data(8, 4, true), relobj_(relobj), shndx_(shndx)
  884. { }
  885. // Return the object containing the section pointed by this.
  886. Relobj*
  887. relobj() const
  888. { return this->relobj_; }
  889. // Return the section index of the section pointed by this.
  890. unsigned int
  891. shndx() const
  892. { return this->shndx_; }
  893. protected:
  894. void
  895. do_write(Output_file* of)
  896. {
  897. if (parameters->target().is_big_endian())
  898. this->do_fixed_endian_write<true>(of);
  899. else
  900. this->do_fixed_endian_write<false>(of);
  901. }
  902. // Write to a map file.
  903. void
  904. do_print_to_mapfile(Mapfile* mapfile) const
  905. { mapfile->print_output_data(this, _("** ARM cantunwind")); }
  906. private:
  907. // Implement do_write for a given endianness.
  908. template<bool big_endian>
  909. void inline
  910. do_fixed_endian_write(Output_file*);
  911. // The object containing the section pointed by this.
  912. Relobj* relobj_;
  913. // The section index of the section pointed by this.
  914. unsigned int shndx_;
  915. };
  916. // During EXIDX coverage fix-up, we compact an EXIDX section. The
  917. // Offset map is used to map input section offset within the EXIDX section
  918. // to the output offset from the start of this EXIDX section.
  919. typedef std::map<section_offset_type, section_offset_type>
  920. Arm_exidx_section_offset_map;
  921. // Arm_exidx_merged_section class. This represents an EXIDX input section
  922. // with some of its entries merged.
  923. class Arm_exidx_merged_section : public Output_relaxed_input_section
  924. {
  925. public:
  926. // Constructor for Arm_exidx_merged_section.
  927. // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
  928. // SECTION_OFFSET_MAP points to a section offset map describing how
  929. // parts of the input section are mapped to output. DELETED_BYTES is
  930. // the number of bytes deleted from the EXIDX input section.
  931. Arm_exidx_merged_section(
  932. const Arm_exidx_input_section& exidx_input_section,
  933. const Arm_exidx_section_offset_map& section_offset_map,
  934. uint32_t deleted_bytes);
  935. // Build output contents.
  936. void
  937. build_contents(const unsigned char*, section_size_type);
  938. // Return the original EXIDX input section.
  939. const Arm_exidx_input_section&
  940. exidx_input_section() const
  941. { return this->exidx_input_section_; }
  942. // Return the section offset map.
  943. const Arm_exidx_section_offset_map&
  944. section_offset_map() const
  945. { return this->section_offset_map_; }
  946. protected:
  947. // Write merged section into file OF.
  948. void
  949. do_write(Output_file* of);
  950. bool
  951. do_output_offset(const Relobj*, unsigned int, section_offset_type,
  952. section_offset_type*) const;
  953. private:
  954. // Original EXIDX input section.
  955. const Arm_exidx_input_section& exidx_input_section_;
  956. // Section offset map.
  957. const Arm_exidx_section_offset_map& section_offset_map_;
  958. // Merged section contents. We need to keep build the merged section
  959. // and save it here to avoid accessing the original EXIDX section when
  960. // we cannot lock the sections' object.
  961. unsigned char* section_contents_;
  962. };
  963. // A class to wrap an ordinary input section containing executable code.
  964. template<bool big_endian>
  965. class Arm_input_section : public Output_relaxed_input_section
  966. {
  967. public:
  968. Arm_input_section(Relobj* relobj, unsigned int shndx)
  969. : Output_relaxed_input_section(relobj, shndx, 1),
  970. original_addralign_(1), original_size_(0), stub_table_(NULL),
  971. original_contents_(NULL)
  972. { }
  973. ~Arm_input_section()
  974. { delete[] this->original_contents_; }
  975. // Initialize.
  976. void
  977. init();
  978. // Whether this is a stub table owner.
  979. bool
  980. is_stub_table_owner() const
  981. { return this->stub_table_ != NULL && this->stub_table_->owner() == this; }
  982. // Return the stub table.
  983. Stub_table<big_endian>*
  984. stub_table() const
  985. { return this->stub_table_; }
  986. // Set the stub_table.
  987. void
  988. set_stub_table(Stub_table<big_endian>* stub_table)
  989. { this->stub_table_ = stub_table; }
  990. // Downcast a base pointer to an Arm_input_section pointer. This is
  991. // not type-safe but we only use Arm_input_section not the base class.
  992. static Arm_input_section<big_endian>*
  993. as_arm_input_section(Output_relaxed_input_section* poris)
  994. { return static_cast<Arm_input_section<big_endian>*>(poris); }
  995. // Return the original size of the section.
  996. uint32_t
  997. original_size() const
  998. { return this->original_size_; }
  999. protected:
  1000. // Write data to output file.
  1001. void
  1002. do_write(Output_file*);
  1003. // Return required alignment of this.
  1004. uint64_t
  1005. do_addralign() const
  1006. {
  1007. if (this->is_stub_table_owner())
  1008. return std::max(this->stub_table_->addralign(),
  1009. static_cast<uint64_t>(this->original_addralign_));
  1010. else
  1011. return this->original_addralign_;
  1012. }
  1013. // Finalize data size.
  1014. void
  1015. set_final_data_size();
  1016. // Reset address and file offset.
  1017. void
  1018. do_reset_address_and_file_offset();
  1019. // Output offset.
  1020. bool
  1021. do_output_offset(const Relobj* object, unsigned int shndx,
  1022. section_offset_type offset,
  1023. section_offset_type* poutput) const
  1024. {
  1025. if ((object == this->relobj())
  1026. && (shndx == this->shndx())
  1027. && (offset >= 0)
  1028. && (offset <=
  1029. convert_types<section_offset_type, uint32_t>(this->original_size_)))
  1030. {
  1031. *poutput = offset;
  1032. return true;
  1033. }
  1034. else
  1035. return false;
  1036. }
  1037. private:
  1038. // Copying is not allowed.
  1039. Arm_input_section(const Arm_input_section&);
  1040. Arm_input_section& operator=(const Arm_input_section&);
  1041. // Address alignment of the original input section.
  1042. uint32_t original_addralign_;
  1043. // Section size of the original input section.
  1044. uint32_t original_size_;
  1045. // Stub table.
  1046. Stub_table<big_endian>* stub_table_;
  1047. // Original section contents. We have to make a copy here since the file
  1048. // containing the original section may not be locked when we need to access
  1049. // the contents.
  1050. unsigned char* original_contents_;
  1051. };
  1052. // Arm_exidx_fixup class. This is used to define a number of methods
  1053. // and keep states for fixing up EXIDX coverage.
  1054. class Arm_exidx_fixup
  1055. {
  1056. public:
  1057. Arm_exidx_fixup(Output_section* exidx_output_section,
  1058. bool merge_exidx_entries = true)
  1059. : exidx_output_section_(exidx_output_section), last_unwind_type_(UT_NONE),
  1060. last_inlined_entry_(0), last_input_section_(NULL),
  1061. section_offset_map_(NULL), first_output_text_section_(NULL),
  1062. merge_exidx_entries_(merge_exidx_entries)
  1063. { }
  1064. ~Arm_exidx_fixup()
  1065. { delete this->section_offset_map_; }
  1066. // Process an EXIDX section for entry merging. SECTION_CONTENTS points
  1067. // to the EXIDX contents and SECTION_SIZE is the size of the contents. Return
  1068. // number of bytes to be deleted in output. If parts of the input EXIDX
  1069. // section are merged a heap allocated Arm_exidx_section_offset_map is store
  1070. // in the located PSECTION_OFFSET_MAP. The caller owns the map and is
  1071. // responsible for releasing it.
  1072. template<bool big_endian>
  1073. uint32_t
  1074. process_exidx_section(const Arm_exidx_input_section* exidx_input_section,
  1075. const unsigned char* section_contents,
  1076. section_size_type section_size,
  1077. Arm_exidx_section_offset_map** psection_offset_map);
  1078. // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
  1079. // input section, if there is not one already.
  1080. void
  1081. add_exidx_cantunwind_as_needed();
  1082. // Return the output section for the text section which is linked to the
  1083. // first exidx input in output.
  1084. Output_section*
  1085. first_output_text_section() const
  1086. { return this->first_output_text_section_; }
  1087. private:
  1088. // Copying is not allowed.
  1089. Arm_exidx_fixup(const Arm_exidx_fixup&);
  1090. Arm_exidx_fixup& operator=(const Arm_exidx_fixup&);
  1091. // Type of EXIDX unwind entry.
  1092. enum Unwind_type
  1093. {
  1094. // No type.
  1095. UT_NONE,
  1096. // EXIDX_CANTUNWIND.
  1097. UT_EXIDX_CANTUNWIND,
  1098. // Inlined entry.
  1099. UT_INLINED_ENTRY,
  1100. // Normal entry.
  1101. UT_NORMAL_ENTRY,
  1102. };
  1103. // Process an EXIDX entry. We only care about the second word of the
  1104. // entry. Return true if the entry can be deleted.
  1105. bool
  1106. process_exidx_entry(uint32_t second_word);
  1107. // Update the current section offset map during EXIDX section fix-up.
  1108. // If there is no map, create one. INPUT_OFFSET is the offset of a
  1109. // reference point, DELETED_BYTES is the number of deleted by in the
  1110. // section so far. If DELETE_ENTRY is true, the reference point and
  1111. // all offsets after the previous reference point are discarded.
  1112. void
  1113. update_offset_map(section_offset_type input_offset,
  1114. section_size_type deleted_bytes, bool delete_entry);
  1115. // EXIDX output section.
  1116. Output_section* exidx_output_section_;
  1117. // Unwind type of the last EXIDX entry processed.
  1118. Unwind_type last_unwind_type_;
  1119. // Last seen inlined EXIDX entry.
  1120. uint32_t last_inlined_entry_;
  1121. // Last processed EXIDX input section.
  1122. const Arm_exidx_input_section* last_input_section_;
  1123. // Section offset map created in process_exidx_section.
  1124. Arm_exidx_section_offset_map* section_offset_map_;
  1125. // Output section for the text section which is linked to the first exidx
  1126. // input in output.
  1127. Output_section* first_output_text_section_;
  1128. bool merge_exidx_entries_;
  1129. };
  1130. // Arm output section class. This is defined mainly to add a number of
  1131. // stub generation methods.
  1132. template<bool big_endian>
  1133. class Arm_output_section : public Output_section
  1134. {
  1135. public:
  1136. typedef std::vector<std::pair<Relobj*, unsigned int> > Text_section_list;
  1137. // We need to force SHF_LINK_ORDER in a SHT_ARM_EXIDX section.
  1138. Arm_output_section(const char* name, elfcpp::Elf_Word type,
  1139. elfcpp::Elf_Xword flags)
  1140. : Output_section(name, type,
  1141. (type == elfcpp::SHT_ARM_EXIDX
  1142. ? flags | elfcpp::SHF_LINK_ORDER
  1143. : flags))
  1144. {
  1145. if (type == elfcpp::SHT_ARM_EXIDX)
  1146. this->set_always_keeps_input_sections();
  1147. }
  1148. ~Arm_output_section()
  1149. { }
  1150. // Group input sections for stub generation.
  1151. void
  1152. group_sections(section_size_type, bool, Target_arm<big_endian>*, const Task*);
  1153. // Downcast a base pointer to an Arm_output_section pointer. This is
  1154. // not type-safe but we only use Arm_output_section not the base class.
  1155. static Arm_output_section<big_endian>*
  1156. as_arm_output_section(Output_section* os)
  1157. { return static_cast<Arm_output_section<big_endian>*>(os); }
  1158. // Append all input text sections in this into LIST.
  1159. void
  1160. append_text_sections_to_list(Text_section_list* list);
  1161. // Fix EXIDX coverage of this EXIDX output section. SORTED_TEXT_SECTION
  1162. // is a list of text input sections sorted in ascending order of their
  1163. // output addresses.
  1164. void
  1165. fix_exidx_coverage(Layout* layout,
  1166. const Text_section_list& sorted_text_section,
  1167. Symbol_table* symtab,
  1168. bool merge_exidx_entries,
  1169. const Task* task);
  1170. // Link an EXIDX section into its corresponding text section.
  1171. void
  1172. set_exidx_section_link();
  1173. private:
  1174. // For convenience.
  1175. typedef Output_section::Input_section Input_section;
  1176. typedef Output_section::Input_section_list Input_section_list;
  1177. // Create a stub group.
  1178. void create_stub_group(Input_section_list::const_iterator,
  1179. Input_section_list::const_iterator,
  1180. Input_section_list::const_iterator,
  1181. Target_arm<big_endian>*,
  1182. std::vector<Output_relaxed_input_section*>*,
  1183. const Task* task);
  1184. };
  1185. // Arm_exidx_input_section class. This represents an EXIDX input section.
  1186. class Arm_exidx_input_section
  1187. {
  1188. public:
  1189. static const section_offset_type invalid_offset =
  1190. static_cast<section_offset_type>(-1);
  1191. Arm_exidx_input_section(Relobj* relobj, unsigned int shndx,
  1192. unsigned int link, uint32_t size,
  1193. uint32_t addralign, uint32_t text_size)
  1194. : relobj_(relobj), shndx_(shndx), link_(link), size_(size),
  1195. addralign_(addralign), text_size_(text_size), has_errors_(false)
  1196. { }
  1197. ~Arm_exidx_input_section()
  1198. { }
  1199. // Accessors: This is a read-only class.
  1200. // Return the object containing this EXIDX input section.
  1201. Relobj*
  1202. relobj() const
  1203. { return this->relobj_; }
  1204. // Return the section index of this EXIDX input section.
  1205. unsigned int
  1206. shndx() const
  1207. { return this->shndx_; }
  1208. // Return the section index of linked text section in the same object.
  1209. unsigned int
  1210. link() const
  1211. { return this->link_; }
  1212. // Return size of the EXIDX input section.
  1213. uint32_t
  1214. size() const
  1215. { return this->size_; }
  1216. // Return address alignment of EXIDX input section.
  1217. uint32_t
  1218. addralign() const
  1219. { return this->addralign_; }
  1220. // Return size of the associated text input section.
  1221. uint32_t
  1222. text_size() const
  1223. { return this->text_size_; }
  1224. // Whether there are any errors in the EXIDX input section.
  1225. bool
  1226. has_errors() const
  1227. { return this->has_errors_; }
  1228. // Set has-errors flag.
  1229. void
  1230. set_has_errors()
  1231. { this->has_errors_ = true; }
  1232. private:
  1233. // Object containing this.
  1234. Relobj* relobj_;
  1235. // Section index of this.
  1236. unsigned int shndx_;
  1237. // text section linked to this in the same object.
  1238. unsigned int link_;
  1239. // Size of this. For ARM 32-bit is sufficient.
  1240. uint32_t size_;
  1241. // Address alignment of this. For ARM 32-bit is sufficient.
  1242. uint32_t addralign_;
  1243. // Size of associated text section.
  1244. uint32_t text_size_;
  1245. // Whether this has any errors.
  1246. bool has_errors_;
  1247. };
  1248. // Arm_relobj class.
  1249. template<bool big_endian>
  1250. class Arm_relobj : public Sized_relobj_file<32, big_endian>
  1251. {
  1252. public:
  1253. static const Arm_address invalid_address = static_cast<Arm_address>(-1);
  1254. Arm_relobj(const std::string& name, Input_file* input_file, off_t offset,
  1255. const typename elfcpp::Ehdr<32, big_endian>& ehdr)
  1256. : Sized_relobj_file<32, big_endian>(name, input_file, offset, ehdr),
  1257. stub_tables_(), local_symbol_is_thumb_function_(),
  1258. attributes_section_data_(NULL), mapping_symbols_info_(),
  1259. section_has_cortex_a8_workaround_(NULL), exidx_section_map_(),
  1260. output_local_symbol_count_needs_update_(false),
  1261. merge_flags_and_attributes_(true)
  1262. { }
  1263. ~Arm_relobj()
  1264. { delete this->attributes_section_data_; }
  1265. // Return the stub table of the SHNDX-th section if there is one.
  1266. Stub_table<big_endian>*
  1267. stub_table(unsigned int shndx) const
  1268. {
  1269. gold_assert(shndx < this->stub_tables_.size());
  1270. return this->stub_tables_[shndx];
  1271. }
  1272. // Set STUB_TABLE to be the stub_table of the SHNDX-th section.
  1273. void
  1274. set_stub_table(unsigned int shndx, Stub_table<big_endian>* stub_table)
  1275. {
  1276. gold_assert(shndx < this->stub_tables_.size());
  1277. this->stub_tables_[shndx] = stub_table;
  1278. }
  1279. // Whether a local symbol is a THUMB function. R_SYM is the symbol table
  1280. // index. This is only valid after do_count_local_symbol is called.
  1281. bool
  1282. local_symbol_is_thumb_function(unsigned int r_sym) const
  1283. {
  1284. gold_assert(r_sym < this->local_symbol_is_thumb_function_.size());
  1285. return this->local_symbol_is_thumb_function_[r_sym];
  1286. }
  1287. // Scan all relocation sections for stub generation.
  1288. void
  1289. scan_sections_for_stubs(Target_arm<big_endian>*, const Symbol_table*,
  1290. const Layout*);
  1291. // Convert regular input section with index SHNDX to a relaxed section.
  1292. void
  1293. convert_input_section_to_relaxed_section(unsigned shndx)
  1294. {
  1295. // The stubs have relocations and we need to process them after writing
  1296. // out the stubs. So relocation now must follow section write.
  1297. this->set_section_offset(shndx, -1ULL);
  1298. this->set_relocs_must_follow_section_writes();
  1299. }
  1300. // Downcast a base pointer to an Arm_relobj pointer. This is
  1301. // not type-safe but we only use Arm_relobj not the base class.
  1302. static Arm_relobj<big_endian>*
  1303. as_arm_relobj(Relobj* relobj)
  1304. { return static_cast<Arm_relobj<big_endian>*>(relobj); }
  1305. // Processor-specific flags in ELF file header. This is valid only after
  1306. // reading symbols.
  1307. elfcpp::Elf_Word
  1308. processor_specific_flags() const
  1309. { return this->processor_specific_flags_; }
  1310. // Attribute section data This is the contents of the .ARM.attribute section
  1311. // if there is one.
  1312. const Attributes_section_data*
  1313. attributes_section_data() const
  1314. { return this->attributes_section_data_; }
  1315. // Mapping symbol location.
  1316. typedef std::pair<unsigned int, Arm_address> Mapping_symbol_position;
  1317. // Functor for STL container.
  1318. struct Mapping_symbol_position_less
  1319. {
  1320. bool
  1321. operator()(const Mapping_symbol_position& p1,
  1322. const Mapping_symbol_position& p2) const
  1323. {
  1324. return (p1.first < p2.first
  1325. || (p1.first == p2.first && p1.second < p2.second));
  1326. }
  1327. };
  1328. // We only care about the first character of a mapping symbol, so
  1329. // we only store that instead of the whole symbol name.
  1330. typedef std::map<Mapping_symbol_position, char,
  1331. Mapping_symbol_position_less> Mapping_symbols_info;
  1332. // Whether a section contains any Cortex-A8 workaround.
  1333. bool
  1334. section_has_cortex_a8_workaround(unsigned int shndx) const
  1335. {
  1336. return (this->section_has_cortex_a8_workaround_ != NULL
  1337. && (*this->section_has_cortex_a8_workaround_)[shndx]);
  1338. }
  1339. // Mark a section that has Cortex-A8 workaround.
  1340. void
  1341. mark_section_for_cortex_a8_workaround(unsigned int shndx)
  1342. {
  1343. if (this->section_has_cortex_a8_workaround_ == NULL)
  1344. this->section_has_cortex_a8_workaround_ =
  1345. new std::vector<bool>(this->shnum(), false);
  1346. (*this->section_has_cortex_a8_workaround_)[shndx] = true;
  1347. }
  1348. // Return the EXIDX section of an text section with index SHNDX or NULL
  1349. // if the text section has no associated EXIDX section.
  1350. const Arm_exidx_input_section*
  1351. exidx_input_section_by_link(unsigned int shndx) const
  1352. {
  1353. Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
  1354. return ((p != this->exidx_section_map_.end()
  1355. && p->second->link() == shndx)
  1356. ? p->second
  1357. : NULL);
  1358. }
  1359. // Return the EXIDX section with index SHNDX or NULL if there is none.
  1360. const Arm_exidx_input_section*
  1361. exidx_input_section_by_shndx(unsigned shndx) const
  1362. {
  1363. Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
  1364. return ((p != this->exidx_section_map_.end()
  1365. && p->second->shndx() == shndx)
  1366. ? p->second
  1367. : NULL);
  1368. }
  1369. // Whether output local symbol count needs updating.
  1370. bool
  1371. output_local_symbol_count_needs_update() const
  1372. { return this->output_local_symbol_count_needs_update_; }
  1373. // Set output_local_symbol_count_needs_update flag to be true.
  1374. void
  1375. set_output_local_symbol_count_needs_update()
  1376. { this->output_local_symbol_count_needs_update_ = true; }
  1377. // Update output local symbol count at the end of relaxation.
  1378. void
  1379. update_output_local_symbol_count();
  1380. // Whether we want to merge processor-specific flags and attributes.
  1381. bool
  1382. merge_flags_and_attributes() const
  1383. { return this->merge_flags_and_attributes_; }
  1384. // Export list of EXIDX section indices.
  1385. void
  1386. get_exidx_shndx_list(std::vector<unsigned int>* list) const
  1387. {
  1388. list->clear();
  1389. for (Exidx_section_map::const_iterator p = this->exidx_section_map_.begin();
  1390. p != this->exidx_section_map_.end();
  1391. ++p)
  1392. {
  1393. if (p->second->shndx() == p->first)
  1394. list->push_back(p->first);
  1395. }
  1396. // Sort list to make result independent of implementation of map.
  1397. std::sort(list->begin(), list->end());
  1398. }
  1399. protected:
  1400. // Post constructor setup.
  1401. void
  1402. do_setup()
  1403. {
  1404. // Call parent's setup method.
  1405. Sized_relobj_file<32, big_endian>::do_setup();
  1406. // Initialize look-up tables.
  1407. Stub_table_list empty_stub_table_list(this->shnum(), NULL);
  1408. this->stub_tables_.swap(empty_stub_table_list);
  1409. }
  1410. // Count the local symbols.
  1411. void
  1412. do_count_local_symbols(Stringpool_template<char>*,
  1413. Stringpool_template<char>*);
  1414. void
  1415. do_relocate_sections(
  1416. const Symbol_table* symtab, const Layout* layout,
  1417. const unsigned char* pshdrs, Output_file* of,
  1418. typename Sized_relobj_file<32, big_endian>::Views* pivews);
  1419. // Read the symbol information.
  1420. void
  1421. do_read_symbols(Read_symbols_data* sd);
  1422. // Process relocs for garbage collection.
  1423. void
  1424. do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
  1425. private:
  1426. // Whether a section needs to be scanned for relocation stubs.
  1427. bool
  1428. section_needs_reloc_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
  1429. const Relobj::Output_sections&,
  1430. const Symbol_table*, const unsigned char*);
  1431. // Whether a section is a scannable text section.
  1432. bool
  1433. section_is_scannable(const elfcpp::Shdr<32, big_endian>&, unsigned int,
  1434. const Output_section*, const Symbol_table*);
  1435. // Whether a section needs to be scanned for the Cortex-A8 erratum.
  1436. bool
  1437. section_needs_cortex_a8_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
  1438. unsigned int, Output_section*,
  1439. const Symbol_table*);
  1440. // Scan a section for the Cortex-A8 erratum.
  1441. void
  1442. scan_section_for_cortex_a8_erratum(const elfcpp::Shdr<32, big_endian>&,
  1443. unsigned int, Output_section*,
  1444. Target_arm<big_endian>*);
  1445. // Find the linked text section of an EXIDX section by looking at the
  1446. // first relocation of the EXIDX section. PSHDR points to the section
  1447. // headers of a relocation section and PSYMS points to the local symbols.
  1448. // PSHNDX points to a location storing the text section index if found.
  1449. // Return whether we can find the linked section.
  1450. bool
  1451. find_linked_text_section(const unsigned char* pshdr,
  1452. const unsigned char* psyms, unsigned int* pshndx);
  1453. //
  1454. // Make a new Arm_exidx_input_section object for EXIDX section with
  1455. // index SHNDX and section header SHDR. TEXT_SHNDX is the section
  1456. // index of the linked text section.
  1457. void
  1458. make_exidx_input_section(unsigned int shndx,
  1459. const elfcpp::Shdr<32, big_endian>& shdr,
  1460. unsigned int text_shndx,
  1461. const elfcpp::Shdr<32, big_endian>& text_shdr);
  1462. // Return the output address of either a plain input section or a
  1463. // relaxed input section. SHNDX is the section index.
  1464. Arm_address
  1465. simple_input_section_output_address(unsigned int, Output_section*);
  1466. typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
  1467. typedef Unordered_map<unsigned int, const Arm_exidx_input_section*>
  1468. Exidx_section_map;
  1469. // List of stub tables.
  1470. Stub_table_list stub_tables_;
  1471. // Bit vector to tell if a local symbol is a thumb function or not.
  1472. // This is only valid after do_count_local_symbol is called.
  1473. std::vector<bool> local_symbol_is_thumb_function_;
  1474. // processor-specific flags in ELF file header.
  1475. elfcpp::Elf_Word processor_specific_flags_;
  1476. // Object attributes if there is an .ARM.attributes section or NULL.
  1477. Attributes_section_data* attributes_section_data_;
  1478. // Mapping symbols information.
  1479. Mapping_symbols_info mapping_symbols_info_;
  1480. // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
  1481. std::vector<bool>* section_has_cortex_a8_workaround_;
  1482. // Map a text section to its associated .ARM.exidx section, if there is one.
  1483. Exidx_section_map exidx_section_map_;
  1484. // Whether output local symbol count needs updating.
  1485. bool output_local_symbol_count_needs_update_;
  1486. // Whether we merge processor flags and attributes of this object to
  1487. // output.
  1488. bool merge_flags_and_attributes_;
  1489. };
  1490. // Arm_dynobj class.
  1491. template<bool big_endian>
  1492. class Arm_dynobj : public Sized_dynobj<32, big_endian>
  1493. {
  1494. public:
  1495. Arm_dynobj(const std::string& name, Input_file* input_file, off_t offset,
  1496. const elfcpp::Ehdr<32, big_endian>& ehdr)
  1497. : Sized_dynobj<32, big_endian>(name, input_file, offset, ehdr),
  1498. processor_specific_flags_(0), attributes_section_data_(NULL)
  1499. { }
  1500. ~Arm_dynobj()
  1501. { delete this->attributes_section_data_; }
  1502. // Downcast a base pointer to an Arm_relobj pointer. This is
  1503. // not type-safe but we only use Arm_relobj not the base class.
  1504. static Arm_dynobj<big_endian>*
  1505. as_arm_dynobj(Dynobj* dynobj)
  1506. { return static_cast<Arm_dynobj<big_endian>*>(dynobj); }
  1507. // Processor-specific flags in ELF file header. This is valid only after
  1508. // reading symbols.
  1509. elfcpp::Elf_Word
  1510. processor_specific_flags() const
  1511. { return this->processor_specific_flags_; }
  1512. // Attributes section data.
  1513. const Attributes_section_data*
  1514. attributes_section_data() const
  1515. { return this->attributes_section_data_; }
  1516. protected:
  1517. // Read the symbol information.
  1518. void
  1519. do_read_symbols(Read_symbols_data* sd);
  1520. private:
  1521. // processor-specific flags in ELF file header.
  1522. elfcpp::Elf_Word processor_specific_flags_;
  1523. // Object attributes if there is an .ARM.attributes section or NULL.
  1524. Attributes_section_data* attributes_section_data_;
  1525. };
  1526. // Functor to read reloc addends during stub generation.
  1527. template<int sh_type, bool big_endian>
  1528. struct Stub_addend_reader
  1529. {
  1530. // Return the addend for a relocation of a particular type. Depending
  1531. // on whether this is a REL or RELA relocation, read the addend from a
  1532. // view or from a Reloc object.
  1533. elfcpp::Elf_types<32>::Elf_Swxword
  1534. operator()(
  1535. unsigned int /* r_type */,
  1536. const unsigned char* /* view */,
  1537. const typename Reloc_types<sh_type,
  1538. 32, big_endian>::Reloc& /* reloc */) const;
  1539. };
  1540. // Specialized Stub_addend_reader for SHT_REL type relocation sections.
  1541. template<bool big_endian>
  1542. struct Stub_addend_reader<elfcpp::SHT_REL, big_endian>
  1543. {
  1544. elfcpp::Elf_types<32>::Elf_Swxword
  1545. operator()(
  1546. unsigned int,
  1547. const unsigned char*,
  1548. const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const;
  1549. };
  1550. // Specialized Stub_addend_reader for RELA type relocation sections.
  1551. // We currently do not handle RELA type relocation sections but it is trivial
  1552. // to implement the addend reader. This is provided for completeness and to
  1553. // make it easier to add support for RELA relocation sections in the future.
  1554. template<bool big_endian>
  1555. struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
  1556. {
  1557. elfcpp::Elf_types<32>::Elf_Swxword
  1558. operator()(
  1559. unsigned int,
  1560. const unsigned char*,
  1561. const typename Reloc_types<elfcpp::SHT_RELA, 32,
  1562. big_endian>::Reloc& reloc) const
  1563. { return reloc.get_r_addend(); }
  1564. };
  1565. // Cortex_a8_reloc class. We keep record of relocation that may need
  1566. // the Cortex-A8 erratum workaround.
  1567. class Cortex_a8_reloc
  1568. {
  1569. public:
  1570. Cortex_a8_reloc(Reloc_stub* reloc_stub, unsigned r_type,
  1571. Arm_address destination)
  1572. : reloc_stub_(reloc_stub), r_type_(r_type), destination_(destination)
  1573. { }
  1574. ~Cortex_a8_reloc()
  1575. { }
  1576. // Accessors: This is a read-only class.
  1577. // Return the relocation stub associated with this relocation if there is
  1578. // one.
  1579. const Reloc_stub*
  1580. reloc_stub() const
  1581. { return this->reloc_stub_; }
  1582. // Return the relocation type.
  1583. unsigned int
  1584. r_type() const
  1585. { return this->r_type_; }
  1586. // Return the destination address of the relocation. LSB stores the THUMB
  1587. // bit.
  1588. Arm_address
  1589. destination() const
  1590. { return this->destination_; }
  1591. private:
  1592. // Associated relocation stub if there is one, or NULL.
  1593. const Reloc_stub* reloc_stub_;
  1594. // Relocation type.
  1595. unsigned int r_type_;
  1596. // Destination address of this relocation. LSB is used to distinguish
  1597. // ARM/THUMB mode.
  1598. Arm_address destination_;
  1599. };
  1600. // Arm_output_data_got class. We derive this from Output_data_got to add
  1601. // extra methods to handle TLS relocations in a static link.
  1602. template<bool big_endian>
  1603. class Arm_output_data_got : public Output_data_got<32, big_endian>
  1604. {
  1605. public:
  1606. Arm_output_data_got(Symbol_table* symtab, Layout* layout)
  1607. : Output_data_got<32, big_endian>(), symbol_table_(symtab), layout_(layout)
  1608. { }
  1609. // Add a static entry for the GOT entry at OFFSET. GSYM is a global
  1610. // symbol and R_TYPE is the code of a dynamic relocation that needs to be
  1611. // applied in a static link.
  1612. void
  1613. add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
  1614. { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
  1615. // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object
  1616. // defining a local symbol with INDEX. R_TYPE is the code of a dynamic
  1617. // relocation that needs to be applied in a static link.
  1618. void
  1619. add_static_reloc(unsigned int got_offset, unsigned int r_type,
  1620. Sized_relobj_file<32, big_endian>* relobj,
  1621. unsigned int index)
  1622. {
  1623. this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
  1624. index));
  1625. }
  1626. // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
  1627. // The first one is initialized to be 1, which is the module index for
  1628. // the main executable and the second one 0. A reloc of the type
  1629. // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
  1630. // be applied by gold. GSYM is a global symbol.
  1631. void
  1632. add_tls_gd32_with_static_reloc(unsigned int got_type, Symbol* gsym);
  1633. // Same as the above but for a local symbol in OBJECT with INDEX.
  1634. void
  1635. add_tls_gd32_with_static_reloc(unsigned int got_type,
  1636. Sized_relobj_file<32, big_endian>* object,
  1637. unsigned int index);
  1638. protected:
  1639. // Write out the GOT table.
  1640. void
  1641. do_write(Output_file*);
  1642. private:
  1643. // This class represent dynamic relocations that need to be applied by
  1644. // gold because we are using TLS relocations in a static link.
  1645. class Static_reloc
  1646. {
  1647. public:
  1648. Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
  1649. : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
  1650. { this->u_.global.symbol = gsym; }
  1651. Static_reloc(unsigned int got_offset, unsigned int r_type,
  1652. Sized_relobj_file<32, big_endian>* relobj, unsigned int index)
  1653. : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
  1654. {
  1655. this->u_.local.relobj = relobj;
  1656. this->u_.local.index = index;
  1657. }
  1658. // Return the GOT offset.
  1659. unsigned int
  1660. got_offset() const
  1661. { return this->got_offset_; }
  1662. // Relocation type.
  1663. unsigned int
  1664. r_type() const
  1665. { return this->r_type_; }
  1666. // Whether the symbol is global or not.
  1667. bool
  1668. symbol_is_global() const
  1669. { return this->symbol_is_global_; }
  1670. // For a relocation against a global symbol, the global symbol.
  1671. Symbol*
  1672. symbol() const
  1673. {
  1674. gold_assert(this->symbol_is_global_);
  1675. return this->u_.global.symbol;
  1676. }
  1677. // For a relocation against a local symbol, the defining object.
  1678. Sized_relobj_file<32, big_endian>*
  1679. relobj() const
  1680. {
  1681. gold_assert(!this->symbol_is_global_);
  1682. return this->u_.local.relobj;
  1683. }
  1684. // For a relocation against a local symbol, the local symbol index.
  1685. unsigned int
  1686. index() const
  1687. {
  1688. gold_assert(!this->symbol_is_global_);
  1689. return this->u_.local.index;
  1690. }
  1691. private:
  1692. // GOT offset of the entry to which this relocation is applied.
  1693. unsigned int got_offset_;
  1694. // Type of relocation.
  1695. unsigned int r_type_;
  1696. // Whether this relocation is against a global symbol.
  1697. bool symbol_is_global_;
  1698. // A global or local symbol.
  1699. union
  1700. {
  1701. struct
  1702. {
  1703. // For a global symbol, the symbol itself.
  1704. Symbol* symbol;
  1705. } global;
  1706. struct
  1707. {
  1708. // For a local symbol, the object defining object.
  1709. Sized_relobj_file<32, big_endian>* relobj;
  1710. // For a local symbol, the symbol index.
  1711. unsigned int index;
  1712. } local;
  1713. } u_;
  1714. };
  1715. // Symbol table of the output object.
  1716. Symbol_table* symbol_table_;
  1717. // Layout of the output object.
  1718. Layout* layout_;
  1719. // Static relocs to be applied to the GOT.
  1720. std::vector<Static_reloc> static_relocs_;
  1721. };
  1722. // The ARM target has many relocation types with odd-sizes or noncontiguous
  1723. // bits. The default handling of relocatable relocation cannot process these
  1724. // relocations. So we have to extend the default code.
  1725. template<bool big_endian, int sh_type, typename Classify_reloc>
  1726. class Arm_scan_relocatable_relocs :
  1727. public Default_scan_relocatable_relocs<sh_type, Classify_reloc>
  1728. {
  1729. public:
  1730. // Return the strategy to use for a local symbol which is a section
  1731. // symbol, given the relocation type.
  1732. inline Relocatable_relocs::Reloc_strategy
  1733. local_section_strategy(unsigned int r_type, Relobj*)
  1734. {
  1735. if (sh_type == elfcpp::SHT_RELA)
  1736. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
  1737. else
  1738. {
  1739. if (r_type == elfcpp::R_ARM_TARGET1
  1740. || r_type == elfcpp::R_ARM_TARGET2)
  1741. {
  1742. const Target_arm<big_endian>* arm_target =
  1743. Target_arm<big_endian>::default_target();
  1744. r_type = arm_target->get_real_reloc_type(r_type);
  1745. }
  1746. switch(r_type)
  1747. {
  1748. // Relocations that write nothing. These exclude R_ARM_TARGET1
  1749. // and R_ARM_TARGET2.
  1750. case elfcpp::R_ARM_NONE:
  1751. case elfcpp::R_ARM_V4BX:
  1752. case elfcpp::R_ARM_TLS_GOTDESC:
  1753. case elfcpp::R_ARM_TLS_CALL:
  1754. case elfcpp::R_ARM_TLS_DESCSEQ:
  1755. case elfcpp::R_ARM_THM_TLS_CALL:
  1756. case elfcpp::R_ARM_GOTRELAX:
  1757. case elfcpp::R_ARM_GNU_VTENTRY:
  1758. case elfcpp::R_ARM_GNU_VTINHERIT:
  1759. case elfcpp::R_ARM_THM_TLS_DESCSEQ16:
  1760. case elfcpp::R_ARM_THM_TLS_DESCSEQ32:
  1761. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
  1762. // These should have been converted to something else above.
  1763. case elfcpp::R_ARM_TARGET1:
  1764. case elfcpp::R_ARM_TARGET2:
  1765. gold_unreachable();
  1766. // Relocations that write full 32 bits and
  1767. // have alignment of 1.
  1768. case elfcpp::R_ARM_ABS32:
  1769. case elfcpp::R_ARM_REL32:
  1770. case elfcpp::R_ARM_SBREL32:
  1771. case elfcpp::R_ARM_GOTOFF32:
  1772. case elfcpp::R_ARM_BASE_PREL:
  1773. case elfcpp::R_ARM_GOT_BREL:
  1774. case elfcpp::R_ARM_BASE_ABS:
  1775. case elfcpp::R_ARM_ABS32_NOI:
  1776. case elfcpp::R_ARM_REL32_NOI:
  1777. case elfcpp::R_ARM_PLT32_ABS:
  1778. case elfcpp::R_ARM_GOT_ABS:
  1779. case elfcpp::R_ARM_GOT_PREL:
  1780. case elfcpp::R_ARM_TLS_GD32:
  1781. case elfcpp::R_ARM_TLS_LDM32:
  1782. case elfcpp::R_ARM_TLS_LDO32:
  1783. case elfcpp::R_ARM_TLS_IE32:
  1784. case elfcpp::R_ARM_TLS_LE32:
  1785. return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED;
  1786. default:
  1787. // For all other static relocations, return RELOC_SPECIAL.
  1788. return Relocatable_relocs::RELOC_SPECIAL;
  1789. }
  1790. }
  1791. }
  1792. };
  1793. template<bool big_endian>
  1794. class Target_arm : public Sized_target<32, big_endian>
  1795. {
  1796. public:
  1797. typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
  1798. Reloc_section;
  1799. // When were are relocating a stub, we pass this as the relocation number.
  1800. static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
  1801. Target_arm(const Target::Target_info* info = &arm_info)
  1802. : Sized_target<32, big_endian>(info),
  1803. got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
  1804. rel_dyn_(NULL), rel_irelative_(NULL), copy_relocs_(elfcpp::R_ARM_COPY),
  1805. got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
  1806. stub_tables_(), stub_factory_(Stub_factory::get_instance()),
  1807. should_force_pic_veneer_(false),
  1808. arm_input_section_map_(), attributes_section_data_(NULL),
  1809. fix_cortex_a8_(false), cortex_a8_relocs_info_()
  1810. { }
  1811. // Whether we force PCI branch veneers.
  1812. bool
  1813. should_force_pic_veneer() const
  1814. { return this->should_force_pic_veneer_; }
  1815. // Set PIC veneer flag.
  1816. void
  1817. set_should_force_pic_veneer(bool value)
  1818. { this->should_force_pic_veneer_ = value; }
  1819. // Whether we use THUMB-2 instructions.
  1820. bool
  1821. using_thumb2() const
  1822. {
  1823. Object_attribute* attr =
  1824. this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
  1825. int arch = attr->int_value();
  1826. return arch == elfcpp::TAG_CPU_ARCH_V6T2 || arch >= elfcpp::TAG_CPU_ARCH_V7;
  1827. }
  1828. // Whether we use THUMB/THUMB-2 instructions only.
  1829. bool
  1830. using_thumb_only() const
  1831. {
  1832. Object_attribute* attr =
  1833. this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
  1834. if (attr->int_value() == elfcpp::TAG_CPU_ARCH_V6_M
  1835. || attr->int_value() == elfcpp::TAG_CPU_ARCH_V6S_M)
  1836. return true;
  1837. if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
  1838. && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
  1839. return false;
  1840. attr = this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
  1841. return attr->int_value() == 'M';
  1842. }
  1843. // Whether we have an NOP instruction. If not, use mov r0, r0 instead.
  1844. bool
  1845. may_use_arm_nop() const
  1846. {
  1847. Object_attribute* attr =
  1848. this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
  1849. int arch = attr->int_value();
  1850. return (arch == elfcpp::TAG_CPU_ARCH_V6T2
  1851. || arch == elfcpp::TAG_CPU_ARCH_V6K
  1852. || arch == elfcpp::TAG_CPU_ARCH_V7
  1853. || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
  1854. }
  1855. // Whether we have THUMB-2 NOP.W instruction.
  1856. bool
  1857. may_use_thumb2_nop() const
  1858. {
  1859. Object_attribute* attr =
  1860. this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
  1861. int arch = attr->int_value();
  1862. return (arch == elfcpp::TAG_CPU_ARCH_V6T2
  1863. || arch == elfcpp::TAG_CPU_ARCH_V7
  1864. || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
  1865. }
  1866. // Whether we have v4T interworking instructions available.
  1867. bool
  1868. may_use_v4t_interworking() const
  1869. {
  1870. Object_attribute* attr =
  1871. this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
  1872. int arch = attr->int_value();
  1873. return (arch != elfcpp::TAG_CPU_ARCH_PRE_V4
  1874. && arch != elfcpp::TAG_CPU_ARCH_V4);
  1875. }
  1876. // Whether we have v5T interworking instructions available.
  1877. bool
  1878. may_use_v5t_interworking() const
  1879. {
  1880. Object_attribute* attr =
  1881. this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
  1882. int arch = attr->int_value();
  1883. if (parameters->options().fix_arm1176())
  1884. return (arch == elfcpp::TAG_CPU_ARCH_V6T2
  1885. || arch == elfcpp::TAG_CPU_ARCH_V7
  1886. || arch == elfcpp::TAG_CPU_ARCH_V6_M
  1887. || arch == elfcpp::TAG_CPU_ARCH_V6S_M
  1888. || arch == elfcpp::TAG_CPU_ARCH_V7E_M);
  1889. else
  1890. return (arch != elfcpp::TAG_CPU_ARCH_PRE_V4
  1891. && arch != elfcpp::TAG_CPU_ARCH_V4
  1892. && arch != elfcpp::TAG_CPU_ARCH_V4T);
  1893. }
  1894. // Process the relocations to determine unreferenced sections for
  1895. // garbage collection.
  1896. void
  1897. gc_process_relocs(Symbol_table* symtab,
  1898. Layout* layout,
  1899. Sized_relobj_file<32, big_endian>* object,
  1900. unsigned int data_shndx,
  1901. unsigned int sh_type,
  1902. const unsigned char* prelocs,
  1903. size_t reloc_count,
  1904. Output_section* output_section,
  1905. bool needs_special_offset_handling,
  1906. size_t local_symbol_count,
  1907. const unsigned char* plocal_symbols);
  1908. // Scan the relocations to look for symbol adjustments.
  1909. void
  1910. scan_relocs(Symbol_table* symtab,
  1911. Layout* layout,
  1912. Sized_relobj_file<32, big_endian>* object,
  1913. unsigned int data_shndx,
  1914. unsigned int sh_type,
  1915. const unsigned char* prelocs,
  1916. size_t reloc_count,
  1917. Output_section* output_section,
  1918. bool needs_special_offset_handling,
  1919. size_t local_symbol_count,
  1920. const unsigned char* plocal_symbols);
  1921. // Finalize the sections.
  1922. void
  1923. do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
  1924. // Return the value to use for a dynamic symbol which requires special
  1925. // treatment.
  1926. uint64_t
  1927. do_dynsym_value(const Symbol*) const;
  1928. // Return the plt address for globals. Since we have irelative plt entries,
  1929. // address calculation is not as straightforward as plt_address + plt_offset.
  1930. uint64_t
  1931. do_plt_address_for_global(const Symbol* gsym) const
  1932. { return this->plt_section()->address_for_global(gsym); }
  1933. // Return the plt address for locals. Since we have irelative plt entries,
  1934. // address calculation is not as straightforward as plt_address + plt_offset.
  1935. uint64_t
  1936. do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
  1937. { return this->plt_section()->address_for_local(relobj, symndx); }
  1938. // Relocate a section.
  1939. void
  1940. relocate_section(const Relocate_info<32, big_endian>*,
  1941. unsigned int sh_type,
  1942. const unsigned char* prelocs,
  1943. size_t reloc_count,
  1944. Output_section* output_section,
  1945. bool needs_special_offset_handling,
  1946. unsigned char* view,
  1947. Arm_address view_address,
  1948. section_size_type view_size,
  1949. const Reloc_symbol_changes*);
  1950. // Scan the relocs during a relocatable link.
  1951. void
  1952. scan_relocatable_relocs(Symbol_table* symtab,
  1953. Layout* layout,
  1954. Sized_relobj_file<32, big_endian>* object,
  1955. unsigned int data_shndx,
  1956. unsigned int sh_type,
  1957. const unsigned char* prelocs,
  1958. size_t reloc_count,
  1959. Output_section* output_section,
  1960. bool needs_special_offset_handling,
  1961. size_t local_symbol_count,
  1962. const unsigned char* plocal_symbols,
  1963. Relocatable_relocs*);
  1964. // Emit relocations for a section.
  1965. void
  1966. relocate_relocs(const Relocate_info<32, big_endian>*,
  1967. unsigned int sh_type,
  1968. const unsigned char* prelocs,
  1969. size_t reloc_count,
  1970. Output_section* output_section,
  1971. typename elfcpp::Elf_types<32>::Elf_Off
  1972. offset_in_output_section,
  1973. const Relocatable_relocs*,
  1974. unsigned char* view,
  1975. Arm_address view_address,
  1976. section_size_type view_size,
  1977. unsigned char* reloc_view,
  1978. section_size_type reloc_view_size);
  1979. // Perform target-specific processing in a relocatable link. This is
  1980. // only used if we use the relocation strategy RELOC_SPECIAL.
  1981. void
  1982. relocate_special_relocatable(const Relocate_info<32, big_endian>* relinfo,
  1983. unsigned int sh_type,
  1984. const unsigned char* preloc_in,
  1985. size_t relnum,
  1986. Output_section* output_section,
  1987. typename elfcpp::Elf_types<32>::Elf_Off
  1988. offset_in_output_section,
  1989. unsigned char* view,
  1990. typename elfcpp::Elf_types<32>::Elf_Addr
  1991. view_address,
  1992. section_size_type view_size,
  1993. unsigned char* preloc_out);
  1994. // Return whether SYM is defined by the ABI.
  1995. bool
  1996. do_is_defined_by_abi(const Symbol* sym) const
  1997. { return strcmp(sym->name(), "__tls_get_addr") == 0; }
  1998. // Return whether there is a GOT section.
  1999. bool
  2000. has_got_section() const
  2001. { return this->got_ != NULL; }
  2002. // Return the size of the GOT section.
  2003. section_size_type
  2004. got_size() const
  2005. {
  2006. gold_assert(this->got_ != NULL);
  2007. return this->got_->data_size();
  2008. }
  2009. // Return the number of entries in the GOT.
  2010. unsigned int
  2011. got_entry_count() const
  2012. {
  2013. if (!this->has_got_section())
  2014. return 0;
  2015. return this->got_size() / 4;
  2016. }
  2017. // Return the number of entries in the PLT.
  2018. unsigned int
  2019. plt_entry_count() const;
  2020. // Return the offset of the first non-reserved PLT entry.
  2021. unsigned int
  2022. first_plt_entry_offset() const;
  2023. // Return the size of each PLT entry.
  2024. unsigned int
  2025. plt_entry_size() const;
  2026. // Get the section to use for IRELATIVE relocations, create it if necessary.
  2027. Reloc_section*
  2028. rel_irelative_section(Layout*);
  2029. // Map platform-specific reloc types
  2030. static unsigned int
  2031. get_real_reloc_type(unsigned int r_type);
  2032. //
  2033. // Methods to support stub-generations.
  2034. //
  2035. // Return the stub factory
  2036. const Stub_factory&
  2037. stub_factory() const
  2038. { return this->stub_factory_; }
  2039. // Make a new Arm_input_section object.
  2040. Arm_input_section<big_endian>*
  2041. new_arm_input_section(Relobj*, unsigned int);
  2042. // Find the Arm_input_section object corresponding to the SHNDX-th input
  2043. // section of RELOBJ.
  2044. Arm_input_section<big_endian>*
  2045. find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
  2046. // Make a new Stub_table
  2047. Stub_table<big_endian>*
  2048. new_stub_table(Arm_input_section<big_endian>*);
  2049. // Scan a section for stub generation.
  2050. void
  2051. scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
  2052. const unsigned char*, size_t, Output_section*,
  2053. bool, const unsigned char*, Arm_address,
  2054. section_size_type);
  2055. // Relocate a stub.
  2056. void
  2057. relocate_stub(Stub*, const Relocate_info<32, big_endian>*,
  2058. Output_section*, unsigned char*, Arm_address,
  2059. section_size_type);
  2060. // Get the default ARM target.
  2061. static Target_arm<big_endian>*
  2062. default_target()
  2063. {
  2064. gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
  2065. && parameters->target().is_big_endian() == big_endian);
  2066. return static_cast<Target_arm<big_endian>*>(
  2067. parameters->sized_target<32, big_endian>());
  2068. }
  2069. // Whether NAME belongs to a mapping symbol.
  2070. static bool
  2071. is_mapping_symbol_name(const char* name)
  2072. {
  2073. return (name
  2074. && name[0] == '$'
  2075. && (name[1] == 'a' || name[1] == 't' || name[1] == 'd')
  2076. && (name[2] == '\0' || name[2] == '.'));
  2077. }
  2078. // Whether we work around the Cortex-A8 erratum.
  2079. bool
  2080. fix_cortex_a8() const
  2081. { return this->fix_cortex_a8_; }
  2082. // Whether we merge exidx entries in debuginfo.
  2083. bool
  2084. merge_exidx_entries() const
  2085. { return parameters->options().merge_exidx_entries(); }
  2086. // Whether we fix R_ARM_V4BX relocation.
  2087. // 0 - do not fix
  2088. // 1 - replace with MOV instruction (armv4 target)
  2089. // 2 - make interworking veneer (>= armv4t targets only)
  2090. General_options::Fix_v4bx
  2091. fix_v4bx() const
  2092. { return parameters->options().fix_v4bx(); }
  2093. // Scan a span of THUMB code section for Cortex-A8 erratum.
  2094. void
  2095. scan_span_for_cortex_a8_erratum(Arm_relobj<big_endian>*, unsigned int,
  2096. section_size_type, section_size_type,
  2097. const unsigned char*, Arm_address);
  2098. // Apply Cortex-A8 workaround to a branch.
  2099. void
  2100. apply_cortex_a8_workaround(const Cortex_a8_stub*, Arm_address,
  2101. unsigned char*, Arm_address);
  2102. protected:
  2103. // Make the PLT-generator object.
  2104. Output_data_plt_arm<big_endian>*
  2105. make_data_plt(Layout* layout,
  2106. Arm_output_data_got<big_endian>* got,
  2107. Output_data_space* got_plt,
  2108. Output_data_space* got_irelative)
  2109. { return this->do_make_data_plt(layout, got, got_plt, got_irelative); }
  2110. // Make an ELF object.
  2111. Object*
  2112. do_make_elf_object(const std::string&, Input_file*, off_t,
  2113. const elfcpp::Ehdr<32, big_endian>& ehdr);
  2114. Object*
  2115. do_make_elf_object(const std::string&, Input_file*, off_t,
  2116. const elfcpp::Ehdr<32, !big_endian>&)
  2117. { gold_unreachable(); }
  2118. Object*
  2119. do_make_elf_object(const std::string&, Input_file*, off_t,
  2120. const elfcpp::Ehdr<64, false>&)
  2121. { gold_unreachable(); }
  2122. Object*
  2123. do_make_elf_object(const std::string&, Input_file*, off_t,
  2124. const elfcpp::Ehdr<64, true>&)
  2125. { gold_unreachable(); }
  2126. // Make an output section.
  2127. Output_section*
  2128. do_make_output_section(const char* name, elfcpp::Elf_Word type,
  2129. elfcpp::Elf_Xword flags)
  2130. { return new Arm_output_section<big_endian>(name, type, flags); }
  2131. void
  2132. do_adjust_elf_header(unsigned char* view, int len);
  2133. // We only need to generate stubs, and hence perform relaxation if we are
  2134. // not doing relocatable linking.
  2135. bool
  2136. do_may_relax() const
  2137. { return !parameters->options().relocatable(); }
  2138. bool
  2139. do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
  2140. // Determine whether an object attribute tag takes an integer, a
  2141. // string or both.
  2142. int
  2143. do_attribute_arg_type(int tag) const;
  2144. // Reorder tags during output.
  2145. int
  2146. do_attributes_order(int num) const;
  2147. // This is called when the target is selected as the default.
  2148. void
  2149. do_select_as_default_target()
  2150. {
  2151. // No locking is required since there should only be one default target.
  2152. // We cannot have both the big-endian and little-endian ARM targets
  2153. // as the default.
  2154. gold_assert(arm_reloc_property_table == NULL);
  2155. arm_reloc_property_table = new Arm_reloc_property_table();
  2156. }
  2157. // Virtual function which is set to return true by a target if
  2158. // it can use relocation types to determine if a function's
  2159. // pointer is taken.
  2160. virtual bool
  2161. do_can_check_for_function_pointers() const
  2162. { return true; }
  2163. // Whether a section called SECTION_NAME may have function pointers to
  2164. // sections not eligible for safe ICF folding.
  2165. virtual bool
  2166. do_section_may_have_icf_unsafe_pointers(const char* section_name) const
  2167. {
  2168. return (!is_prefix_of(".ARM.exidx", section_name)
  2169. && !is_prefix_of(".ARM.extab", section_name)
  2170. && Target::do_section_may_have_icf_unsafe_pointers(section_name));
  2171. }
  2172. virtual void
  2173. do_define_standard_symbols(Symbol_table*, Layout*);
  2174. virtual Output_data_plt_arm<big_endian>*
  2175. do_make_data_plt(Layout* layout,
  2176. Arm_output_data_got<big_endian>* got,
  2177. Output_data_space* got_plt,
  2178. Output_data_space* got_irelative)
  2179. {
  2180. gold_assert(got_plt != NULL && got_irelative != NULL);
  2181. return new Output_data_plt_arm_standard<big_endian>(
  2182. layout, got, got_plt, got_irelative);
  2183. }
  2184. private:
  2185. // The class which scans relocations.
  2186. class Scan
  2187. {
  2188. public:
  2189. Scan()
  2190. : issued_non_pic_error_(false)
  2191. { }
  2192. static inline int
  2193. get_reference_flags(unsigned int r_type);
  2194. inline void
  2195. local(Symbol_table* symtab, Layout* layout, Target_arm* target,
  2196. Sized_relobj_file<32, big_endian>* object,
  2197. unsigned int data_shndx,
  2198. Output_section* output_section,
  2199. const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
  2200. const elfcpp::Sym<32, big_endian>& lsym,
  2201. bool is_discarded);
  2202. inline void
  2203. global(Symbol_table* symtab, Layout* layout, Target_arm* target,
  2204. Sized_relobj_file<32, big_endian>* object,
  2205. unsigned int data_shndx,
  2206. Output_section* output_section,
  2207. const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
  2208. Symbol* gsym);
  2209. inline bool
  2210. local_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
  2211. Sized_relobj_file<32, big_endian>* ,
  2212. unsigned int ,
  2213. Output_section* ,
  2214. const elfcpp::Rel<32, big_endian>& ,
  2215. unsigned int ,
  2216. const elfcpp::Sym<32, big_endian>&);
  2217. inline bool
  2218. global_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
  2219. Sized_relobj_file<32, big_endian>* ,
  2220. unsigned int ,
  2221. Output_section* ,
  2222. const elfcpp::Rel<32, big_endian>& ,
  2223. unsigned int , Symbol*);
  2224. private:
  2225. static void
  2226. unsupported_reloc_local(Sized_relobj_file<32, big_endian>*,
  2227. unsigned int r_type);
  2228. static void
  2229. unsupported_reloc_global(Sized_relobj_file<32, big_endian>*,
  2230. unsigned int r_type, Symbol*);
  2231. void
  2232. check_non_pic(Relobj*, unsigned int r_type);
  2233. // Almost identical to Symbol::needs_plt_entry except that it also
  2234. // handles STT_ARM_TFUNC.
  2235. static bool
  2236. symbol_needs_plt_entry(const Symbol* sym)
  2237. {
  2238. // An undefined symbol from an executable does not need a PLT entry.
  2239. if (sym->is_undefined() && !parameters->options().shared())
  2240. return false;
  2241. if (sym->type() == elfcpp::STT_GNU_IFUNC)
  2242. return true;
  2243. return (!parameters->doing_static_link()
  2244. && (sym->type() == elfcpp::STT_FUNC
  2245. || sym->type() == elfcpp::STT_ARM_TFUNC)
  2246. && (sym->is_from_dynobj()
  2247. || sym->is_undefined()
  2248. || sym->is_preemptible()));
  2249. }
  2250. inline bool
  2251. possible_function_pointer_reloc(unsigned int r_type);
  2252. // Whether a plt entry is needed for ifunc.
  2253. bool
  2254. reloc_needs_plt_for_ifunc(Sized_relobj_file<32, big_endian>*,
  2255. unsigned int r_type);
  2256. // Whether we have issued an error about a non-PIC compilation.
  2257. bool issued_non_pic_error_;
  2258. };
  2259. // The class which implements relocation.
  2260. class Relocate
  2261. {
  2262. public:
  2263. Relocate()
  2264. { }
  2265. ~Relocate()
  2266. { }
  2267. // Return whether the static relocation needs to be applied.
  2268. inline bool
  2269. should_apply_static_reloc(const Sized_symbol<32>* gsym,
  2270. unsigned int r_type,
  2271. bool is_32bit,
  2272. Output_section* output_section);
  2273. // Do a relocation. Return false if the caller should not issue
  2274. // any warnings about this relocation.
  2275. inline bool
  2276. relocate(const Relocate_info<32, big_endian>*, Target_arm*,
  2277. Output_section*, size_t relnum,
  2278. const elfcpp::Rel<32, big_endian>&,
  2279. unsigned int r_type, const Sized_symbol<32>*,
  2280. const Symbol_value<32>*,
  2281. unsigned char*, Arm_address,
  2282. section_size_type);
  2283. // Return whether we want to pass flag NON_PIC_REF for this
  2284. // reloc. This means the relocation type accesses a symbol not via
  2285. // GOT or PLT.
  2286. static inline bool
  2287. reloc_is_non_pic(unsigned int r_type)
  2288. {
  2289. switch (r_type)
  2290. {
  2291. // These relocation types reference GOT or PLT entries explicitly.
  2292. case elfcpp::R_ARM_GOT_BREL:
  2293. case elfcpp::R_ARM_GOT_ABS:
  2294. case elfcpp::R_ARM_GOT_PREL:
  2295. case elfcpp::R_ARM_GOT_BREL12:
  2296. case elfcpp::R_ARM_PLT32_ABS:
  2297. case elfcpp::R_ARM_TLS_GD32:
  2298. case elfcpp::R_ARM_TLS_LDM32:
  2299. case elfcpp::R_ARM_TLS_IE32:
  2300. case elfcpp::R_ARM_TLS_IE12GP:
  2301. // These relocate types may use PLT entries.
  2302. case elfcpp::R_ARM_CALL:
  2303. case elfcpp::R_ARM_THM_CALL:
  2304. case elfcpp::R_ARM_JUMP24:
  2305. case elfcpp::R_ARM_THM_JUMP24:
  2306. case elfcpp::R_ARM_THM_JUMP19:
  2307. case elfcpp::R_ARM_PLT32:
  2308. case elfcpp::R_ARM_THM_XPC22:
  2309. case elfcpp::R_ARM_PREL31:
  2310. case elfcpp::R_ARM_SBREL31:
  2311. return false;
  2312. default:
  2313. return true;
  2314. }
  2315. }
  2316. private:
  2317. // Do a TLS relocation.
  2318. inline typename Arm_relocate_functions<big_endian>::Status
  2319. relocate_tls(const Relocate_info<32, big_endian>*, Target_arm<big_endian>*,
  2320. size_t, const elfcpp::Rel<32, big_endian>&, unsigned int,
  2321. const Sized_symbol<32>*, const Symbol_value<32>*,
  2322. unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
  2323. section_size_type);
  2324. };
  2325. // A class which returns the size required for a relocation type,
  2326. // used while scanning relocs during a relocatable link.
  2327. class Relocatable_size_for_reloc
  2328. {
  2329. public:
  2330. unsigned int
  2331. get_size_for_reloc(unsigned int, Relobj*);
  2332. };
  2333. // Adjust TLS relocation type based on the options and whether this
  2334. // is a local symbol.
  2335. static tls::Tls_optimization
  2336. optimize_tls_reloc(bool is_final, int r_type);
  2337. // Get the GOT section, creating it if necessary.
  2338. Arm_output_data_got<big_endian>*
  2339. got_section(Symbol_table*, Layout*);
  2340. // Get the GOT PLT section.
  2341. Output_data_space*
  2342. got_plt_section() const
  2343. {
  2344. gold_assert(this->got_plt_ != NULL);
  2345. return this->got_plt_;
  2346. }
  2347. // Create the PLT section.
  2348. void
  2349. make_plt_section(Symbol_table* symtab, Layout* layout);
  2350. // Create a PLT entry for a global symbol.
  2351. void
  2352. make_plt_entry(Symbol_table*, Layout*, Symbol*);
  2353. // Create a PLT entry for a local STT_GNU_IFUNC symbol.
  2354. void
  2355. make_local_ifunc_plt_entry(Symbol_table*, Layout*,
  2356. Sized_relobj_file<32, big_endian>* relobj,
  2357. unsigned int local_sym_index);
  2358. // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
  2359. void
  2360. define_tls_base_symbol(Symbol_table*, Layout*);
  2361. // Create a GOT entry for the TLS module index.
  2362. unsigned int
  2363. got_mod_index_entry(Symbol_table* symtab, Layout* layout,
  2364. Sized_relobj_file<32, big_endian>* object);
  2365. // Get the PLT section.
  2366. const Output_data_plt_arm<big_endian>*
  2367. plt_section() const
  2368. {
  2369. gold_assert(this->plt_ != NULL);
  2370. return this->plt_;
  2371. }
  2372. // Get the dynamic reloc section, creating it if necessary.
  2373. Reloc_section*
  2374. rel_dyn_section(Layout*);
  2375. // Get the section to use for TLS_DESC relocations.
  2376. Reloc_section*
  2377. rel_tls_desc_section(Layout*) const;
  2378. // Return true if the symbol may need a COPY relocation.
  2379. // References from an executable object to non-function symbols
  2380. // defined in a dynamic object may need a COPY relocation.
  2381. bool
  2382. may_need_copy_reloc(Symbol* gsym)
  2383. {
  2384. return (gsym->type() != elfcpp::STT_ARM_TFUNC
  2385. && gsym->may_need_copy_reloc());
  2386. }
  2387. // Add a potential copy relocation.
  2388. void
  2389. copy_reloc(Symbol_table* symtab, Layout* layout,
  2390. Sized_relobj_file<32, big_endian>* object,
  2391. unsigned int shndx, Output_section* output_section,
  2392. Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
  2393. {
  2394. this->copy_relocs_.copy_reloc(symtab, layout,
  2395. symtab->get_sized_symbol<32>(sym),
  2396. object, shndx, output_section, reloc,
  2397. this->rel_dyn_section(layout));
  2398. }
  2399. // Whether two EABI versions are compatible.
  2400. static bool
  2401. are_eabi_versions_compatible(elfcpp::Elf_Word v1, elfcpp::Elf_Word v2);
  2402. // Merge processor-specific flags from input object and those in the ELF
  2403. // header of the output.
  2404. void
  2405. merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
  2406. // Get the secondary compatible architecture.
  2407. static int
  2408. get_secondary_compatible_arch(const Attributes_section_data*);
  2409. // Set the secondary compatible architecture.
  2410. static void
  2411. set_secondary_compatible_arch(Attributes_section_data*, int);
  2412. static int
  2413. tag_cpu_arch_combine(const char*, int, int*, int, int);
  2414. // Helper to print AEABI enum tag value.
  2415. static std::string
  2416. aeabi_enum_name(unsigned int);
  2417. // Return string value for TAG_CPU_name.
  2418. static std::string
  2419. tag_cpu_name_value(unsigned int);
  2420. // Query attributes object to see if integer divide instructions may be
  2421. // present in an object.
  2422. static bool
  2423. attributes_accept_div(int arch, int profile,
  2424. const Object_attribute* div_attr);
  2425. // Query attributes object to see if integer divide instructions are
  2426. // forbidden to be in the object. This is not the inverse of
  2427. // attributes_accept_div.
  2428. static bool
  2429. attributes_forbid_div(const Object_attribute* div_attr);
  2430. // Merge object attributes from input object and those in the output.
  2431. void
  2432. merge_object_attributes(const char*, const Attributes_section_data*);
  2433. // Helper to get an AEABI object attribute
  2434. Object_attribute*
  2435. get_aeabi_object_attribute(int tag) const
  2436. {
  2437. Attributes_section_data* pasd = this->attributes_section_data_;
  2438. gold_assert(pasd != NULL);
  2439. Object_attribute* attr =
  2440. pasd->get_attribute(Object_attribute::OBJ_ATTR_PROC, tag);
  2441. gold_assert(attr != NULL);
  2442. return attr;
  2443. }
  2444. //
  2445. // Methods to support stub-generations.
  2446. //
  2447. // Group input sections for stub generation.
  2448. void
  2449. group_sections(Layout*, section_size_type, bool, const Task*);
  2450. // Scan a relocation for stub generation.
  2451. void
  2452. scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
  2453. const Sized_symbol<32>*, unsigned int,
  2454. const Symbol_value<32>*,
  2455. elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
  2456. // Scan a relocation section for stub.
  2457. template<int sh_type>
  2458. void
  2459. scan_reloc_section_for_stubs(
  2460. const Relocate_info<32, big_endian>* relinfo,
  2461. const unsigned char* prelocs,
  2462. size_t reloc_count,
  2463. Output_section* output_section,
  2464. bool needs_special_offset_handling,
  2465. const unsigned char* view,
  2466. elfcpp::Elf_types<32>::Elf_Addr view_address,
  2467. section_size_type);
  2468. // Fix .ARM.exidx section coverage.
  2469. void
  2470. fix_exidx_coverage(Layout*, const Input_objects*,
  2471. Arm_output_section<big_endian>*, Symbol_table*,
  2472. const Task*);
  2473. // Functors for STL set.
  2474. struct output_section_address_less_than
  2475. {
  2476. bool
  2477. operator()(const Output_section* s1, const Output_section* s2) const
  2478. { return s1->address() < s2->address(); }
  2479. };
  2480. // Information about this specific target which we pass to the
  2481. // general Target structure.
  2482. static const Target::Target_info arm_info;
  2483. // The types of GOT entries needed for this platform.
  2484. // These values are exposed to the ABI in an incremental link.
  2485. // Do not renumber existing values without changing the version
  2486. // number of the .gnu_incremental_inputs section.
  2487. enum Got_type
  2488. {
  2489. GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
  2490. GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
  2491. GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
  2492. GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
  2493. GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
  2494. };
  2495. typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
  2496. // Map input section to Arm_input_section.
  2497. typedef Unordered_map<Section_id,
  2498. Arm_input_section<big_endian>*,
  2499. Section_id_hash>
  2500. Arm_input_section_map;
  2501. // Map output addresses to relocs for Cortex-A8 erratum.
  2502. typedef Unordered_map<Arm_address, const Cortex_a8_reloc*>
  2503. Cortex_a8_relocs_info;
  2504. // The GOT section.
  2505. Arm_output_data_got<big_endian>* got_;
  2506. // The PLT section.
  2507. Output_data_plt_arm<big_endian>* plt_;
  2508. // The GOT PLT section.
  2509. Output_data_space* got_plt_;
  2510. // The GOT section for IRELATIVE relocations.
  2511. Output_data_space* got_irelative_;
  2512. // The dynamic reloc section.
  2513. Reloc_section* rel_dyn_;
  2514. // The section to use for IRELATIVE relocs.
  2515. Reloc_section* rel_irelative_;
  2516. // Relocs saved to avoid a COPY reloc.
  2517. Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
  2518. // Offset of the GOT entry for the TLS module index.
  2519. unsigned int got_mod_index_offset_;
  2520. // True if the _TLS_MODULE_BASE_ symbol has been defined.
  2521. bool tls_base_symbol_defined_;
  2522. // Vector of Stub_tables created.
  2523. Stub_table_list stub_tables_;
  2524. // Stub factory.
  2525. const Stub_factory &stub_factory_;
  2526. // Whether we force PIC branch veneers.
  2527. bool should_force_pic_veneer_;
  2528. // Map for locating Arm_input_sections.
  2529. Arm_input_section_map arm_input_section_map_;
  2530. // Attributes section data in output.
  2531. Attributes_section_data* attributes_section_data_;
  2532. // Whether we want to fix code for Cortex-A8 erratum.
  2533. bool fix_cortex_a8_;
  2534. // Map addresses to relocs for Cortex-A8 erratum.
  2535. Cortex_a8_relocs_info cortex_a8_relocs_info_;
  2536. };
  2537. template<bool big_endian>
  2538. const Target::Target_info Target_arm<big_endian>::arm_info =
  2539. {
  2540. 32, // size
  2541. big_endian, // is_big_endian
  2542. elfcpp::EM_ARM, // machine_code
  2543. false, // has_make_symbol
  2544. false, // has_resolve
  2545. false, // has_code_fill
  2546. true, // is_default_stack_executable
  2547. false, // can_icf_inline_merge_sections
  2548. '\0', // wrap_char
  2549. "/usr/lib/libc.so.1", // dynamic_linker
  2550. 0x8000, // default_text_segment_address
  2551. 0x1000, // abi_pagesize (overridable by -z max-page-size)
  2552. 0x1000, // common_pagesize (overridable by -z common-page-size)
  2553. false, // isolate_execinstr
  2554. 0, // rosegment_gap
  2555. elfcpp::SHN_UNDEF, // small_common_shndx
  2556. elfcpp::SHN_UNDEF, // large_common_shndx
  2557. 0, // small_common_section_flags
  2558. 0, // large_common_section_flags
  2559. ".ARM.attributes", // attributes_section
  2560. "aeabi", // attributes_vendor
  2561. "_start" // entry_symbol_name
  2562. };
  2563. // Arm relocate functions class
  2564. //
  2565. template<bool big_endian>
  2566. class Arm_relocate_functions : public Relocate_functions<32, big_endian>
  2567. {
  2568. public:
  2569. typedef enum
  2570. {
  2571. STATUS_OKAY, // No error during relocation.
  2572. STATUS_OVERFLOW, // Relocation overflow.
  2573. STATUS_BAD_RELOC // Relocation cannot be applied.
  2574. } Status;
  2575. private:
  2576. typedef Relocate_functions<32, big_endian> Base;
  2577. typedef Arm_relocate_functions<big_endian> This;
  2578. // Encoding of imm16 argument for movt and movw ARM instructions
  2579. // from ARM ARM:
  2580. //
  2581. // imm16 := imm4 | imm12
  2582. //
  2583. // f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0
  2584. // +-------+---------------+-------+-------+-----------------------+
  2585. // | | |imm4 | |imm12 |
  2586. // +-------+---------------+-------+-------+-----------------------+
  2587. // Extract the relocation addend from VAL based on the ARM
  2588. // instruction encoding described above.
  2589. static inline typename elfcpp::Swap<32, big_endian>::Valtype
  2590. extract_arm_movw_movt_addend(
  2591. typename elfcpp::Swap<32, big_endian>::Valtype val)
  2592. {
  2593. // According to the Elf ABI for ARM Architecture the immediate
  2594. // field is sign-extended to form the addend.
  2595. return Bits<16>::sign_extend32(((val >> 4) & 0xf000) | (val & 0xfff));
  2596. }
  2597. // Insert X into VAL based on the ARM instruction encoding described
  2598. // above.
  2599. static inline typename elfcpp::Swap<32, big_endian>::Valtype
  2600. insert_val_arm_movw_movt(
  2601. typename elfcpp::Swap<32, big_endian>::Valtype val,
  2602. typename elfcpp::Swap<32, big_endian>::Valtype x)
  2603. {
  2604. val &= 0xfff0f000;
  2605. val |= x & 0x0fff;
  2606. val |= (x & 0xf000) << 4;
  2607. return val;
  2608. }
  2609. // Encoding of imm16 argument for movt and movw Thumb2 instructions
  2610. // from ARM ARM:
  2611. //
  2612. // imm16 := imm4 | i | imm3 | imm8
  2613. //
  2614. // f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0
  2615. // +---------+-+-----------+-------++-+-----+-------+---------------+
  2616. // | |i| |imm4 || |imm3 | |imm8 |
  2617. // +---------+-+-----------+-------++-+-----+-------+---------------+
  2618. // Extract the relocation addend from VAL based on the Thumb2
  2619. // instruction encoding described above.
  2620. static inline typename elfcpp::Swap<32, big_endian>::Valtype
  2621. extract_thumb_movw_movt_addend(
  2622. typename elfcpp::Swap<32, big_endian>::Valtype val)
  2623. {
  2624. // According to the Elf ABI for ARM Architecture the immediate
  2625. // field is sign-extended to form the addend.
  2626. return Bits<16>::sign_extend32(((val >> 4) & 0xf000)
  2627. | ((val >> 15) & 0x0800)
  2628. | ((val >> 4) & 0x0700)
  2629. | (val & 0x00ff));
  2630. }
  2631. // Insert X into VAL based on the Thumb2 instruction encoding
  2632. // described above.
  2633. static inline typename elfcpp::Swap<32, big_endian>::Valtype
  2634. insert_val_thumb_movw_movt(
  2635. typename elfcpp::Swap<32, big_endian>::Valtype val,
  2636. typename elfcpp::Swap<32, big_endian>::Valtype x)
  2637. {
  2638. val &= 0xfbf08f00;
  2639. val |= (x & 0xf000) << 4;
  2640. val |= (x & 0x0800) << 15;
  2641. val |= (x & 0x0700) << 4;
  2642. val |= (x & 0x00ff);
  2643. return val;
  2644. }
  2645. // Calculate the smallest constant Kn for the specified residual.
  2646. // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
  2647. static uint32_t
  2648. calc_grp_kn(typename elfcpp::Swap<32, big_endian>::Valtype residual)
  2649. {
  2650. int32_t msb;
  2651. if (residual == 0)
  2652. return 0;
  2653. // Determine the most significant bit in the residual and
  2654. // align the resulting value to a 2-bit boundary.
  2655. for (msb = 30; (msb >= 0) && !(residual & (3 << msb)); msb -= 2)
  2656. ;
  2657. // The desired shift is now (msb - 6), or zero, whichever
  2658. // is the greater.
  2659. return (((msb - 6) < 0) ? 0 : (msb - 6));
  2660. }
  2661. // Calculate the final residual for the specified group index.
  2662. // If the passed group index is less than zero, the method will return
  2663. // the value of the specified residual without any change.
  2664. // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
  2665. static typename elfcpp::Swap<32, big_endian>::Valtype
  2666. calc_grp_residual(typename elfcpp::Swap<32, big_endian>::Valtype residual,
  2667. const int group)
  2668. {
  2669. for (int n = 0; n <= group; n++)
  2670. {
  2671. // Calculate which part of the value to mask.
  2672. uint32_t shift = calc_grp_kn(residual);
  2673. // Calculate the residual for the next time around.
  2674. residual &= ~(residual & (0xff << shift));
  2675. }
  2676. return residual;
  2677. }
  2678. // Calculate the value of Gn for the specified group index.
  2679. // We return it in the form of an encoded constant-and-rotation.
  2680. // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
  2681. static typename elfcpp::Swap<32, big_endian>::Valtype
  2682. calc_grp_gn(typename elfcpp::Swap<32, big_endian>::Valtype residual,
  2683. const int group)
  2684. {
  2685. typename elfcpp::Swap<32, big_endian>::Valtype gn = 0;
  2686. uint32_t shift = 0;
  2687. for (int n = 0; n <= group; n++)
  2688. {
  2689. // Calculate which part of the value to mask.
  2690. shift = calc_grp_kn(residual);
  2691. // Calculate Gn in 32-bit as well as encoded constant-and-rotation form.
  2692. gn = residual & (0xff << shift);
  2693. // Calculate the residual for the next time around.
  2694. residual &= ~gn;
  2695. }
  2696. // Return Gn in the form of an encoded constant-and-rotation.
  2697. return ((gn >> shift) | ((gn <= 0xff ? 0 : (32 - shift) / 2) << 8));
  2698. }
  2699. public:
  2700. // Handle ARM long branches.
  2701. static typename This::Status
  2702. arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
  2703. unsigned char*, const Sized_symbol<32>*,
  2704. const Arm_relobj<big_endian>*, unsigned int,
  2705. const Symbol_value<32>*, Arm_address, Arm_address, bool);
  2706. // Handle THUMB long branches.
  2707. static typename This::Status
  2708. thumb_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
  2709. unsigned char*, const Sized_symbol<32>*,
  2710. const Arm_relobj<big_endian>*, unsigned int,
  2711. const Symbol_value<32>*, Arm_address, Arm_address, bool);
  2712. // Return the branch offset of a 32-bit THUMB branch.
  2713. static inline int32_t
  2714. thumb32_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
  2715. {
  2716. // We use the Thumb-2 encoding (backwards compatible with Thumb-1)
  2717. // involving the J1 and J2 bits.
  2718. uint32_t s = (upper_insn & (1U << 10)) >> 10;
  2719. uint32_t upper = upper_insn & 0x3ffU;
  2720. uint32_t lower = lower_insn & 0x7ffU;
  2721. uint32_t j1 = (lower_insn & (1U << 13)) >> 13;
  2722. uint32_t j2 = (lower_insn & (1U << 11)) >> 11;
  2723. uint32_t i1 = j1 ^ s ? 0 : 1;
  2724. uint32_t i2 = j2 ^ s ? 0 : 1;
  2725. return Bits<25>::sign_extend32((s << 24) | (i1 << 23) | (i2 << 22)
  2726. | (upper << 12) | (lower << 1));
  2727. }
  2728. // Insert OFFSET to a 32-bit THUMB branch and return the upper instruction.
  2729. // UPPER_INSN is the original upper instruction of the branch. Caller is
  2730. // responsible for overflow checking and BLX offset adjustment.
  2731. static inline uint16_t
  2732. thumb32_branch_upper(uint16_t upper_insn, int32_t offset)
  2733. {
  2734. uint32_t s = offset < 0 ? 1 : 0;
  2735. uint32_t bits = static_cast<uint32_t>(offset);
  2736. return (upper_insn & ~0x7ffU) | ((bits >> 12) & 0x3ffU) | (s << 10);
  2737. }
  2738. // Insert OFFSET to a 32-bit THUMB branch and return the lower instruction.
  2739. // LOWER_INSN is the original lower instruction of the branch. Caller is
  2740. // responsible for overflow checking and BLX offset adjustment.
  2741. static inline uint16_t
  2742. thumb32_branch_lower(uint16_t lower_insn, int32_t offset)
  2743. {
  2744. uint32_t s = offset < 0 ? 1 : 0;
  2745. uint32_t bits = static_cast<uint32_t>(offset);
  2746. return ((lower_insn & ~0x2fffU)
  2747. | ((((bits >> 23) & 1) ^ !s) << 13)
  2748. | ((((bits >> 22) & 1) ^ !s) << 11)
  2749. | ((bits >> 1) & 0x7ffU));
  2750. }
  2751. // Return the branch offset of a 32-bit THUMB conditional branch.
  2752. static inline int32_t
  2753. thumb32_cond_branch_offset(uint16_t upper_insn, uint16_t lower_insn)
  2754. {
  2755. uint32_t s = (upper_insn & 0x0400U) >> 10;
  2756. uint32_t j1 = (lower_insn & 0x2000U) >> 13;
  2757. uint32_t j2 = (lower_insn & 0x0800U) >> 11;
  2758. uint32_t lower = (lower_insn & 0x07ffU);
  2759. uint32_t upper = (s << 8) | (j2 << 7) | (j1 << 6) | (upper_insn & 0x003fU);
  2760. return Bits<21>::sign_extend32((upper << 12) | (lower << 1));
  2761. }
  2762. // Insert OFFSET to a 32-bit THUMB conditional branch and return the upper
  2763. // instruction. UPPER_INSN is the original upper instruction of the branch.
  2764. // Caller is responsible for overflow checking.
  2765. static inline uint16_t
  2766. thumb32_cond_branch_upper(uint16_t upper_insn, int32_t offset)
  2767. {
  2768. uint32_t s = offset < 0 ? 1 : 0;
  2769. uint32_t bits = static_cast<uint32_t>(offset);
  2770. return (upper_insn & 0xfbc0U) | (s << 10) | ((bits & 0x0003f000U) >> 12);
  2771. }
  2772. // Insert OFFSET to a 32-bit THUMB conditional branch and return the lower
  2773. // instruction. LOWER_INSN is the original lower instruction of the branch.
  2774. // The caller is responsible for overflow checking.
  2775. static inline uint16_t
  2776. thumb32_cond_branch_lower(uint16_t lower_insn, int32_t offset)
  2777. {
  2778. uint32_t bits = static_cast<uint32_t>(offset);
  2779. uint32_t j2 = (bits & 0x00080000U) >> 19;
  2780. uint32_t j1 = (bits & 0x00040000U) >> 18;
  2781. uint32_t lo = (bits & 0x00000ffeU) >> 1;
  2782. return (lower_insn & 0xd000U) | (j1 << 13) | (j2 << 11) | lo;
  2783. }
  2784. // R_ARM_ABS8: S + A
  2785. static inline typename This::Status
  2786. abs8(unsigned char* view,
  2787. const Sized_relobj_file<32, big_endian>* object,
  2788. const Symbol_value<32>* psymval)
  2789. {
  2790. typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
  2791. Valtype* wv = reinterpret_cast<Valtype*>(view);
  2792. Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
  2793. int32_t addend = Bits<8>::sign_extend32(val);
  2794. Arm_address x = psymval->value(object, addend);
  2795. val = Bits<32>::bit_select32(val, x, 0xffU);
  2796. elfcpp::Swap<8, big_endian>::writeval(wv, val);
  2797. // R_ARM_ABS8 permits signed or unsigned results.
  2798. return (Bits<8>::has_signed_unsigned_overflow32(x)
  2799. ? This::STATUS_OVERFLOW
  2800. : This::STATUS_OKAY);
  2801. }
  2802. // R_ARM_THM_ABS5: S + A
  2803. static inline typename This::Status
  2804. thm_abs5(unsigned char* view,
  2805. const Sized_relobj_file<32, big_endian>* object,
  2806. const Symbol_value<32>* psymval)
  2807. {
  2808. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
  2809. typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
  2810. Valtype* wv = reinterpret_cast<Valtype*>(view);
  2811. Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
  2812. Reltype addend = (val & 0x7e0U) >> 6;
  2813. Reltype x = psymval->value(object, addend);
  2814. val = Bits<32>::bit_select32(val, x << 6, 0x7e0U);
  2815. elfcpp::Swap<16, big_endian>::writeval(wv, val);
  2816. return (Bits<5>::has_overflow32(x)
  2817. ? This::STATUS_OVERFLOW
  2818. : This::STATUS_OKAY);
  2819. }
  2820. // R_ARM_ABS12: S + A
  2821. static inline typename This::Status
  2822. abs12(unsigned char* view,
  2823. const Sized_relobj_file<32, big_endian>* object,
  2824. const Symbol_value<32>* psymval)
  2825. {
  2826. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  2827. typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
  2828. Valtype* wv = reinterpret_cast<Valtype*>(view);
  2829. Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
  2830. Reltype addend = val & 0x0fffU;
  2831. Reltype x = psymval->value(object, addend);
  2832. val = Bits<32>::bit_select32(val, x, 0x0fffU);
  2833. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  2834. return (Bits<12>::has_overflow32(x)
  2835. ? This::STATUS_OVERFLOW
  2836. : This::STATUS_OKAY);
  2837. }
  2838. // R_ARM_ABS16: S + A
  2839. static inline typename This::Status
  2840. abs16(unsigned char* view,
  2841. const Sized_relobj_file<32, big_endian>* object,
  2842. const Symbol_value<32>* psymval)
  2843. {
  2844. typedef typename elfcpp::Swap_unaligned<16, big_endian>::Valtype Valtype;
  2845. Valtype val = elfcpp::Swap_unaligned<16, big_endian>::readval(view);
  2846. int32_t addend = Bits<16>::sign_extend32(val);
  2847. Arm_address x = psymval->value(object, addend);
  2848. val = Bits<32>::bit_select32(val, x, 0xffffU);
  2849. elfcpp::Swap_unaligned<16, big_endian>::writeval(view, val);
  2850. // R_ARM_ABS16 permits signed or unsigned results.
  2851. return (Bits<16>::has_signed_unsigned_overflow32(x)
  2852. ? This::STATUS_OVERFLOW
  2853. : This::STATUS_OKAY);
  2854. }
  2855. // R_ARM_ABS32: (S + A) | T
  2856. static inline typename This::Status
  2857. abs32(unsigned char* view,
  2858. const Sized_relobj_file<32, big_endian>* object,
  2859. const Symbol_value<32>* psymval,
  2860. Arm_address thumb_bit)
  2861. {
  2862. typedef typename elfcpp::Swap_unaligned<32, big_endian>::Valtype Valtype;
  2863. Valtype addend = elfcpp::Swap_unaligned<32, big_endian>::readval(view);
  2864. Valtype x = psymval->value(object, addend) | thumb_bit;
  2865. elfcpp::Swap_unaligned<32, big_endian>::writeval(view, x);
  2866. return This::STATUS_OKAY;
  2867. }
  2868. // R_ARM_REL32: (S + A) | T - P
  2869. static inline typename This::Status
  2870. rel32(unsigned char* view,
  2871. const Sized_relobj_file<32, big_endian>* object,
  2872. const Symbol_value<32>* psymval,
  2873. Arm_address address,
  2874. Arm_address thumb_bit)
  2875. {
  2876. typedef typename elfcpp::Swap_unaligned<32, big_endian>::Valtype Valtype;
  2877. Valtype addend = elfcpp::Swap_unaligned<32, big_endian>::readval(view);
  2878. Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
  2879. elfcpp::Swap_unaligned<32, big_endian>::writeval(view, x);
  2880. return This::STATUS_OKAY;
  2881. }
  2882. // R_ARM_THM_JUMP24: (S + A) | T - P
  2883. static typename This::Status
  2884. thm_jump19(unsigned char* view, const Arm_relobj<big_endian>* object,
  2885. const Symbol_value<32>* psymval, Arm_address address,
  2886. Arm_address thumb_bit);
  2887. // R_ARM_THM_JUMP6: S + A – P
  2888. static inline typename This::Status
  2889. thm_jump6(unsigned char* view,
  2890. const Sized_relobj_file<32, big_endian>* object,
  2891. const Symbol_value<32>* psymval,
  2892. Arm_address address)
  2893. {
  2894. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
  2895. typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
  2896. Valtype* wv = reinterpret_cast<Valtype*>(view);
  2897. Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
  2898. // bit[9]:bit[7:3]:’0’ (mask: 0x02f8)
  2899. Reltype addend = (((val & 0x0200) >> 3) | ((val & 0x00f8) >> 2));
  2900. Reltype x = (psymval->value(object, addend) - address);
  2901. val = (val & 0xfd07) | ((x & 0x0040) << 3) | ((val & 0x003e) << 2);
  2902. elfcpp::Swap<16, big_endian>::writeval(wv, val);
  2903. // CZB does only forward jumps.
  2904. return ((x > 0x007e)
  2905. ? This::STATUS_OVERFLOW
  2906. : This::STATUS_OKAY);
  2907. }
  2908. // R_ARM_THM_JUMP8: S + A – P
  2909. static inline typename This::Status
  2910. thm_jump8(unsigned char* view,
  2911. const Sized_relobj_file<32, big_endian>* object,
  2912. const Symbol_value<32>* psymval,
  2913. Arm_address address)
  2914. {
  2915. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
  2916. Valtype* wv = reinterpret_cast<Valtype*>(view);
  2917. Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
  2918. int32_t addend = Bits<8>::sign_extend32((val & 0x00ff) << 1);
  2919. int32_t x = (psymval->value(object, addend) - address);
  2920. elfcpp::Swap<16, big_endian>::writeval(wv, ((val & 0xff00)
  2921. | ((x & 0x01fe) >> 1)));
  2922. // We do a 9-bit overflow check because x is right-shifted by 1 bit.
  2923. return (Bits<9>::has_overflow32(x)
  2924. ? This::STATUS_OVERFLOW
  2925. : This::STATUS_OKAY);
  2926. }
  2927. // R_ARM_THM_JUMP11: S + A – P
  2928. static inline typename This::Status
  2929. thm_jump11(unsigned char* view,
  2930. const Sized_relobj_file<32, big_endian>* object,
  2931. const Symbol_value<32>* psymval,
  2932. Arm_address address)
  2933. {
  2934. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
  2935. Valtype* wv = reinterpret_cast<Valtype*>(view);
  2936. Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
  2937. int32_t addend = Bits<11>::sign_extend32((val & 0x07ff) << 1);
  2938. int32_t x = (psymval->value(object, addend) - address);
  2939. elfcpp::Swap<16, big_endian>::writeval(wv, ((val & 0xf800)
  2940. | ((x & 0x0ffe) >> 1)));
  2941. // We do a 12-bit overflow check because x is right-shifted by 1 bit.
  2942. return (Bits<12>::has_overflow32(x)
  2943. ? This::STATUS_OVERFLOW
  2944. : This::STATUS_OKAY);
  2945. }
  2946. // R_ARM_BASE_PREL: B(S) + A - P
  2947. static inline typename This::Status
  2948. base_prel(unsigned char* view,
  2949. Arm_address origin,
  2950. Arm_address address)
  2951. {
  2952. Base::rel32(view, origin - address);
  2953. return STATUS_OKAY;
  2954. }
  2955. // R_ARM_BASE_ABS: B(S) + A
  2956. static inline typename This::Status
  2957. base_abs(unsigned char* view,
  2958. Arm_address origin)
  2959. {
  2960. Base::rel32(view, origin);
  2961. return STATUS_OKAY;
  2962. }
  2963. // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
  2964. static inline typename This::Status
  2965. got_brel(unsigned char* view,
  2966. typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
  2967. {
  2968. Base::rel32(view, got_offset);
  2969. return This::STATUS_OKAY;
  2970. }
  2971. // R_ARM_GOT_PREL: GOT(S) + A - P
  2972. static inline typename This::Status
  2973. got_prel(unsigned char* view,
  2974. Arm_address got_entry,
  2975. Arm_address address)
  2976. {
  2977. Base::rel32(view, got_entry - address);
  2978. return This::STATUS_OKAY;
  2979. }
  2980. // R_ARM_PREL: (S + A) | T - P
  2981. static inline typename This::Status
  2982. prel31(unsigned char* view,
  2983. const Sized_relobj_file<32, big_endian>* object,
  2984. const Symbol_value<32>* psymval,
  2985. Arm_address address,
  2986. Arm_address thumb_bit)
  2987. {
  2988. typedef typename elfcpp::Swap_unaligned<32, big_endian>::Valtype Valtype;
  2989. Valtype val = elfcpp::Swap_unaligned<32, big_endian>::readval(view);
  2990. Valtype addend = Bits<31>::sign_extend32(val);
  2991. Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
  2992. val = Bits<32>::bit_select32(val, x, 0x7fffffffU);
  2993. elfcpp::Swap_unaligned<32, big_endian>::writeval(view, val);
  2994. return (Bits<31>::has_overflow32(x)
  2995. ? This::STATUS_OVERFLOW
  2996. : This::STATUS_OKAY);
  2997. }
  2998. // R_ARM_MOVW_ABS_NC: (S + A) | T (relative address base is )
  2999. // R_ARM_MOVW_PREL_NC: (S + A) | T - P
  3000. // R_ARM_MOVW_BREL_NC: ((S + A) | T) - B(S)
  3001. // R_ARM_MOVW_BREL: ((S + A) | T) - B(S)
  3002. static inline typename This::Status
  3003. movw(unsigned char* view,
  3004. const Sized_relobj_file<32, big_endian>* object,
  3005. const Symbol_value<32>* psymval,
  3006. Arm_address relative_address_base,
  3007. Arm_address thumb_bit,
  3008. bool check_overflow)
  3009. {
  3010. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  3011. Valtype* wv = reinterpret_cast<Valtype*>(view);
  3012. Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
  3013. Valtype addend = This::extract_arm_movw_movt_addend(val);
  3014. Valtype x = ((psymval->value(object, addend) | thumb_bit)
  3015. - relative_address_base);
  3016. val = This::insert_val_arm_movw_movt(val, x);
  3017. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3018. return ((check_overflow && Bits<16>::has_overflow32(x))
  3019. ? This::STATUS_OVERFLOW
  3020. : This::STATUS_OKAY);
  3021. }
  3022. // R_ARM_MOVT_ABS: S + A (relative address base is 0)
  3023. // R_ARM_MOVT_PREL: S + A - P
  3024. // R_ARM_MOVT_BREL: S + A - B(S)
  3025. static inline typename This::Status
  3026. movt(unsigned char* view,
  3027. const Sized_relobj_file<32, big_endian>* object,
  3028. const Symbol_value<32>* psymval,
  3029. Arm_address relative_address_base)
  3030. {
  3031. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  3032. Valtype* wv = reinterpret_cast<Valtype*>(view);
  3033. Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
  3034. Valtype addend = This::extract_arm_movw_movt_addend(val);
  3035. Valtype x = (psymval->value(object, addend) - relative_address_base) >> 16;
  3036. val = This::insert_val_arm_movw_movt(val, x);
  3037. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3038. // FIXME: IHI0044D says that we should check for overflow.
  3039. return This::STATUS_OKAY;
  3040. }
  3041. // R_ARM_THM_MOVW_ABS_NC: S + A | T (relative_address_base is 0)
  3042. // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
  3043. // R_ARM_THM_MOVW_BREL_NC: ((S + A) | T) - B(S)
  3044. // R_ARM_THM_MOVW_BREL: ((S + A) | T) - B(S)
  3045. static inline typename This::Status
  3046. thm_movw(unsigned char* view,
  3047. const Sized_relobj_file<32, big_endian>* object,
  3048. const Symbol_value<32>* psymval,
  3049. Arm_address relative_address_base,
  3050. Arm_address thumb_bit,
  3051. bool check_overflow)
  3052. {
  3053. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
  3054. typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
  3055. Valtype* wv = reinterpret_cast<Valtype*>(view);
  3056. Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
  3057. | elfcpp::Swap<16, big_endian>::readval(wv + 1);
  3058. Reltype addend = This::extract_thumb_movw_movt_addend(val);
  3059. Reltype x =
  3060. (psymval->value(object, addend) | thumb_bit) - relative_address_base;
  3061. val = This::insert_val_thumb_movw_movt(val, x);
  3062. elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
  3063. elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
  3064. return ((check_overflow && Bits<16>::has_overflow32(x))
  3065. ? This::STATUS_OVERFLOW
  3066. : This::STATUS_OKAY);
  3067. }
  3068. // R_ARM_THM_MOVT_ABS: S + A (relative address base is 0)
  3069. // R_ARM_THM_MOVT_PREL: S + A - P
  3070. // R_ARM_THM_MOVT_BREL: S + A - B(S)
  3071. static inline typename This::Status
  3072. thm_movt(unsigned char* view,
  3073. const Sized_relobj_file<32, big_endian>* object,
  3074. const Symbol_value<32>* psymval,
  3075. Arm_address relative_address_base)
  3076. {
  3077. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
  3078. typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
  3079. Valtype* wv = reinterpret_cast<Valtype*>(view);
  3080. Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
  3081. | elfcpp::Swap<16, big_endian>::readval(wv + 1);
  3082. Reltype addend = This::extract_thumb_movw_movt_addend(val);
  3083. Reltype x = (psymval->value(object, addend) - relative_address_base) >> 16;
  3084. val = This::insert_val_thumb_movw_movt(val, x);
  3085. elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
  3086. elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
  3087. return This::STATUS_OKAY;
  3088. }
  3089. // R_ARM_THM_ALU_PREL_11_0: ((S + A) | T) - Pa (Thumb32)
  3090. static inline typename This::Status
  3091. thm_alu11(unsigned char* view,
  3092. const Sized_relobj_file<32, big_endian>* object,
  3093. const Symbol_value<32>* psymval,
  3094. Arm_address address,
  3095. Arm_address thumb_bit)
  3096. {
  3097. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
  3098. typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
  3099. Valtype* wv = reinterpret_cast<Valtype*>(view);
  3100. Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
  3101. | elfcpp::Swap<16, big_endian>::readval(wv + 1);
  3102. // f e d c b|a|9|8 7 6 5|4|3 2 1 0||f|e d c|b a 9 8|7 6 5 4 3 2 1 0
  3103. // -----------------------------------------------------------------------
  3104. // ADD{S} 1 1 1 1 0|i|0|1 0 0 0|S|1 1 0 1||0|imm3 |Rd |imm8
  3105. // ADDW 1 1 1 1 0|i|1|0 0 0 0|0|1 1 0 1||0|imm3 |Rd |imm8
  3106. // ADR[+] 1 1 1 1 0|i|1|0 0 0 0|0|1 1 1 1||0|imm3 |Rd |imm8
  3107. // SUB{S} 1 1 1 1 0|i|0|1 1 0 1|S|1 1 0 1||0|imm3 |Rd |imm8
  3108. // SUBW 1 1 1 1 0|i|1|0 1 0 1|0|1 1 0 1||0|imm3 |Rd |imm8
  3109. // ADR[-] 1 1 1 1 0|i|1|0 1 0 1|0|1 1 1 1||0|imm3 |Rd |imm8
  3110. // Determine a sign for the addend.
  3111. const int sign = ((insn & 0xf8ef0000) == 0xf0ad0000
  3112. || (insn & 0xf8ef0000) == 0xf0af0000) ? -1 : 1;
  3113. // Thumb2 addend encoding:
  3114. // imm12 := i | imm3 | imm8
  3115. int32_t addend = (insn & 0xff)
  3116. | ((insn & 0x00007000) >> 4)
  3117. | ((insn & 0x04000000) >> 15);
  3118. // Apply a sign to the added.
  3119. addend *= sign;
  3120. int32_t x = (psymval->value(object, addend) | thumb_bit)
  3121. - (address & 0xfffffffc);
  3122. Reltype val = abs(x);
  3123. // Mask out the value and a distinct part of the ADD/SUB opcode
  3124. // (bits 7:5 of opword).
  3125. insn = (insn & 0xfb0f8f00)
  3126. | (val & 0xff)
  3127. | ((val & 0x700) << 4)
  3128. | ((val & 0x800) << 15);
  3129. // Set the opcode according to whether the value to go in the
  3130. // place is negative.
  3131. if (x < 0)
  3132. insn |= 0x00a00000;
  3133. elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
  3134. elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
  3135. return ((val > 0xfff) ?
  3136. This::STATUS_OVERFLOW : This::STATUS_OKAY);
  3137. }
  3138. // R_ARM_THM_PC8: S + A - Pa (Thumb)
  3139. static inline typename This::Status
  3140. thm_pc8(unsigned char* view,
  3141. const Sized_relobj_file<32, big_endian>* object,
  3142. const Symbol_value<32>* psymval,
  3143. Arm_address address)
  3144. {
  3145. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
  3146. typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
  3147. Valtype* wv = reinterpret_cast<Valtype*>(view);
  3148. Valtype insn = elfcpp::Swap<16, big_endian>::readval(wv);
  3149. Reltype addend = ((insn & 0x00ff) << 2);
  3150. int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
  3151. Reltype val = abs(x);
  3152. insn = (insn & 0xff00) | ((val & 0x03fc) >> 2);
  3153. elfcpp::Swap<16, big_endian>::writeval(wv, insn);
  3154. return ((val > 0x03fc)
  3155. ? This::STATUS_OVERFLOW
  3156. : This::STATUS_OKAY);
  3157. }
  3158. // R_ARM_THM_PC12: S + A - Pa (Thumb32)
  3159. static inline typename This::Status
  3160. thm_pc12(unsigned char* view,
  3161. const Sized_relobj_file<32, big_endian>* object,
  3162. const Symbol_value<32>* psymval,
  3163. Arm_address address)
  3164. {
  3165. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
  3166. typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
  3167. Valtype* wv = reinterpret_cast<Valtype*>(view);
  3168. Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
  3169. | elfcpp::Swap<16, big_endian>::readval(wv + 1);
  3170. // Determine a sign for the addend (positive if the U bit is 1).
  3171. const int sign = (insn & 0x00800000) ? 1 : -1;
  3172. int32_t addend = (insn & 0xfff);
  3173. // Apply a sign to the added.
  3174. addend *= sign;
  3175. int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
  3176. Reltype val = abs(x);
  3177. // Mask out and apply the value and the U bit.
  3178. insn = (insn & 0xff7ff000) | (val & 0xfff);
  3179. // Set the U bit according to whether the value to go in the
  3180. // place is positive.
  3181. if (x >= 0)
  3182. insn |= 0x00800000;
  3183. elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
  3184. elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
  3185. return ((val > 0xfff) ?
  3186. This::STATUS_OVERFLOW : This::STATUS_OKAY);
  3187. }
  3188. // R_ARM_V4BX
  3189. static inline typename This::Status
  3190. v4bx(const Relocate_info<32, big_endian>* relinfo,
  3191. unsigned char* view,
  3192. const Arm_relobj<big_endian>* object,
  3193. const Arm_address address,
  3194. const bool is_interworking)
  3195. {
  3196. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  3197. Valtype* wv = reinterpret_cast<Valtype*>(view);
  3198. Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
  3199. // Ensure that we have a BX instruction.
  3200. gold_assert((val & 0x0ffffff0) == 0x012fff10);
  3201. const uint32_t reg = (val & 0xf);
  3202. if (is_interworking && reg != 0xf)
  3203. {
  3204. Stub_table<big_endian>* stub_table =
  3205. object->stub_table(relinfo->data_shndx);
  3206. gold_assert(stub_table != NULL);
  3207. Arm_v4bx_stub* stub = stub_table->find_arm_v4bx_stub(reg);
  3208. gold_assert(stub != NULL);
  3209. int32_t veneer_address =
  3210. stub_table->address() + stub->offset() - 8 - address;
  3211. gold_assert((veneer_address <= ARM_MAX_FWD_BRANCH_OFFSET)
  3212. && (veneer_address >= ARM_MAX_BWD_BRANCH_OFFSET));
  3213. // Replace with a branch to veneer (B <addr>)
  3214. val = (val & 0xf0000000) | 0x0a000000
  3215. | ((veneer_address >> 2) & 0x00ffffff);
  3216. }
  3217. else
  3218. {
  3219. // Preserve Rm (lowest four bits) and the condition code
  3220. // (highest four bits). Other bits encode MOV PC,Rm.
  3221. val = (val & 0xf000000f) | 0x01a0f000;
  3222. }
  3223. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3224. return This::STATUS_OKAY;
  3225. }
  3226. // R_ARM_ALU_PC_G0_NC: ((S + A) | T) - P
  3227. // R_ARM_ALU_PC_G0: ((S + A) | T) - P
  3228. // R_ARM_ALU_PC_G1_NC: ((S + A) | T) - P
  3229. // R_ARM_ALU_PC_G1: ((S + A) | T) - P
  3230. // R_ARM_ALU_PC_G2: ((S + A) | T) - P
  3231. // R_ARM_ALU_SB_G0_NC: ((S + A) | T) - B(S)
  3232. // R_ARM_ALU_SB_G0: ((S + A) | T) - B(S)
  3233. // R_ARM_ALU_SB_G1_NC: ((S + A) | T) - B(S)
  3234. // R_ARM_ALU_SB_G1: ((S + A) | T) - B(S)
  3235. // R_ARM_ALU_SB_G2: ((S + A) | T) - B(S)
  3236. static inline typename This::Status
  3237. arm_grp_alu(unsigned char* view,
  3238. const Sized_relobj_file<32, big_endian>* object,
  3239. const Symbol_value<32>* psymval,
  3240. const int group,
  3241. Arm_address address,
  3242. Arm_address thumb_bit,
  3243. bool check_overflow)
  3244. {
  3245. gold_assert(group >= 0 && group < 3);
  3246. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  3247. Valtype* wv = reinterpret_cast<Valtype*>(view);
  3248. Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
  3249. // ALU group relocations are allowed only for the ADD/SUB instructions.
  3250. // (0x00800000 - ADD, 0x00400000 - SUB)
  3251. const Valtype opcode = insn & 0x01e00000;
  3252. if (opcode != 0x00800000 && opcode != 0x00400000)
  3253. return This::STATUS_BAD_RELOC;
  3254. // Determine a sign for the addend.
  3255. const int sign = (opcode == 0x00800000) ? 1 : -1;
  3256. // shifter = rotate_imm * 2
  3257. const uint32_t shifter = (insn & 0xf00) >> 7;
  3258. // Initial addend value.
  3259. int32_t addend = insn & 0xff;
  3260. // Rotate addend right by shifter.
  3261. addend = (addend >> shifter) | (addend << (32 - shifter));
  3262. // Apply a sign to the added.
  3263. addend *= sign;
  3264. int32_t x = ((psymval->value(object, addend) | thumb_bit) - address);
  3265. Valtype gn = Arm_relocate_functions::calc_grp_gn(abs(x), group);
  3266. // Check for overflow if required
  3267. if (check_overflow
  3268. && (Arm_relocate_functions::calc_grp_residual(abs(x), group) != 0))
  3269. return This::STATUS_OVERFLOW;
  3270. // Mask out the value and the ADD/SUB part of the opcode; take care
  3271. // not to destroy the S bit.
  3272. insn &= 0xff1ff000;
  3273. // Set the opcode according to whether the value to go in the
  3274. // place is negative.
  3275. insn |= ((x < 0) ? 0x00400000 : 0x00800000);
  3276. // Encode the offset (encoded Gn).
  3277. insn |= gn;
  3278. elfcpp::Swap<32, big_endian>::writeval(wv, insn);
  3279. return This::STATUS_OKAY;
  3280. }
  3281. // R_ARM_LDR_PC_G0: S + A - P
  3282. // R_ARM_LDR_PC_G1: S + A - P
  3283. // R_ARM_LDR_PC_G2: S + A - P
  3284. // R_ARM_LDR_SB_G0: S + A - B(S)
  3285. // R_ARM_LDR_SB_G1: S + A - B(S)
  3286. // R_ARM_LDR_SB_G2: S + A - B(S)
  3287. static inline typename This::Status
  3288. arm_grp_ldr(unsigned char* view,
  3289. const Sized_relobj_file<32, big_endian>* object,
  3290. const Symbol_value<32>* psymval,
  3291. const int group,
  3292. Arm_address address)
  3293. {
  3294. gold_assert(group >= 0 && group < 3);
  3295. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  3296. Valtype* wv = reinterpret_cast<Valtype*>(view);
  3297. Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
  3298. const int sign = (insn & 0x00800000) ? 1 : -1;
  3299. int32_t addend = (insn & 0xfff) * sign;
  3300. int32_t x = (psymval->value(object, addend) - address);
  3301. // Calculate the relevant G(n-1) value to obtain this stage residual.
  3302. Valtype residual =
  3303. Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
  3304. if (residual >= 0x1000)
  3305. return This::STATUS_OVERFLOW;
  3306. // Mask out the value and U bit.
  3307. insn &= 0xff7ff000;
  3308. // Set the U bit for non-negative values.
  3309. if (x >= 0)
  3310. insn |= 0x00800000;
  3311. insn |= residual;
  3312. elfcpp::Swap<32, big_endian>::writeval(wv, insn);
  3313. return This::STATUS_OKAY;
  3314. }
  3315. // R_ARM_LDRS_PC_G0: S + A - P
  3316. // R_ARM_LDRS_PC_G1: S + A - P
  3317. // R_ARM_LDRS_PC_G2: S + A - P
  3318. // R_ARM_LDRS_SB_G0: S + A - B(S)
  3319. // R_ARM_LDRS_SB_G1: S + A - B(S)
  3320. // R_ARM_LDRS_SB_G2: S + A - B(S)
  3321. static inline typename This::Status
  3322. arm_grp_ldrs(unsigned char* view,
  3323. const Sized_relobj_file<32, big_endian>* object,
  3324. const Symbol_value<32>* psymval,
  3325. const int group,
  3326. Arm_address address)
  3327. {
  3328. gold_assert(group >= 0 && group < 3);
  3329. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  3330. Valtype* wv = reinterpret_cast<Valtype*>(view);
  3331. Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
  3332. const int sign = (insn & 0x00800000) ? 1 : -1;
  3333. int32_t addend = (((insn & 0xf00) >> 4) + (insn & 0xf)) * sign;
  3334. int32_t x = (psymval->value(object, addend) - address);
  3335. // Calculate the relevant G(n-1) value to obtain this stage residual.
  3336. Valtype residual =
  3337. Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
  3338. if (residual >= 0x100)
  3339. return This::STATUS_OVERFLOW;
  3340. // Mask out the value and U bit.
  3341. insn &= 0xff7ff0f0;
  3342. // Set the U bit for non-negative values.
  3343. if (x >= 0)
  3344. insn |= 0x00800000;
  3345. insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
  3346. elfcpp::Swap<32, big_endian>::writeval(wv, insn);
  3347. return This::STATUS_OKAY;
  3348. }
  3349. // R_ARM_LDC_PC_G0: S + A - P
  3350. // R_ARM_LDC_PC_G1: S + A - P
  3351. // R_ARM_LDC_PC_G2: S + A - P
  3352. // R_ARM_LDC_SB_G0: S + A - B(S)
  3353. // R_ARM_LDC_SB_G1: S + A - B(S)
  3354. // R_ARM_LDC_SB_G2: S + A - B(S)
  3355. static inline typename This::Status
  3356. arm_grp_ldc(unsigned char* view,
  3357. const Sized_relobj_file<32, big_endian>* object,
  3358. const Symbol_value<32>* psymval,
  3359. const int group,
  3360. Arm_address address)
  3361. {
  3362. gold_assert(group >= 0 && group < 3);
  3363. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  3364. Valtype* wv = reinterpret_cast<Valtype*>(view);
  3365. Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
  3366. const int sign = (insn & 0x00800000) ? 1 : -1;
  3367. int32_t addend = ((insn & 0xff) << 2) * sign;
  3368. int32_t x = (psymval->value(object, addend) - address);
  3369. // Calculate the relevant G(n-1) value to obtain this stage residual.
  3370. Valtype residual =
  3371. Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
  3372. if ((residual & 0x3) != 0 || residual >= 0x400)
  3373. return This::STATUS_OVERFLOW;
  3374. // Mask out the value and U bit.
  3375. insn &= 0xff7fff00;
  3376. // Set the U bit for non-negative values.
  3377. if (x >= 0)
  3378. insn |= 0x00800000;
  3379. insn |= (residual >> 2);
  3380. elfcpp::Swap<32, big_endian>::writeval(wv, insn);
  3381. return This::STATUS_OKAY;
  3382. }
  3383. };
  3384. // Relocate ARM long branches. This handles relocation types
  3385. // R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
  3386. // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
  3387. // undefined and we do not use PLT in this relocation. In such a case,
  3388. // the branch is converted into an NOP.
  3389. template<bool big_endian>
  3390. typename Arm_relocate_functions<big_endian>::Status
  3391. Arm_relocate_functions<big_endian>::arm_branch_common(
  3392. unsigned int r_type,
  3393. const Relocate_info<32, big_endian>* relinfo,
  3394. unsigned char* view,
  3395. const Sized_symbol<32>* gsym,
  3396. const Arm_relobj<big_endian>* object,
  3397. unsigned int r_sym,
  3398. const Symbol_value<32>* psymval,
  3399. Arm_address address,
  3400. Arm_address thumb_bit,
  3401. bool is_weakly_undefined_without_plt)
  3402. {
  3403. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  3404. Valtype* wv = reinterpret_cast<Valtype*>(view);
  3405. Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
  3406. bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
  3407. && ((val & 0x0f000000UL) == 0x0a000000UL);
  3408. bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
  3409. bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
  3410. && ((val & 0x0f000000UL) == 0x0b000000UL);
  3411. bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
  3412. bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
  3413. // Check that the instruction is valid.
  3414. if (r_type == elfcpp::R_ARM_CALL)
  3415. {
  3416. if (!insn_is_uncond_bl && !insn_is_blx)
  3417. return This::STATUS_BAD_RELOC;
  3418. }
  3419. else if (r_type == elfcpp::R_ARM_JUMP24)
  3420. {
  3421. if (!insn_is_b && !insn_is_cond_bl)
  3422. return This::STATUS_BAD_RELOC;
  3423. }
  3424. else if (r_type == elfcpp::R_ARM_PLT32)
  3425. {
  3426. if (!insn_is_any_branch)
  3427. return This::STATUS_BAD_RELOC;
  3428. }
  3429. else if (r_type == elfcpp::R_ARM_XPC25)
  3430. {
  3431. // FIXME: AAELF document IH0044C does not say much about it other
  3432. // than it being obsolete.
  3433. if (!insn_is_any_branch)
  3434. return This::STATUS_BAD_RELOC;
  3435. }
  3436. else
  3437. gold_unreachable();
  3438. // A branch to an undefined weak symbol is turned into a jump to
  3439. // the next instruction unless a PLT entry will be created.
  3440. // Do the same for local undefined symbols.
  3441. // The jump to the next instruction is optimized as a NOP depending
  3442. // on the architecture.
  3443. const Target_arm<big_endian>* arm_target =
  3444. Target_arm<big_endian>::default_target();
  3445. if (is_weakly_undefined_without_plt)
  3446. {
  3447. gold_assert(!parameters->options().relocatable());
  3448. Valtype cond = val & 0xf0000000U;
  3449. if (arm_target->may_use_arm_nop())
  3450. val = cond | 0x0320f000;
  3451. else
  3452. val = cond | 0x01a00000; // Using pre-UAL nop: mov r0, r0.
  3453. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3454. return This::STATUS_OKAY;
  3455. }
  3456. Valtype addend = Bits<26>::sign_extend32(val << 2);
  3457. Valtype branch_target = psymval->value(object, addend);
  3458. int32_t branch_offset = branch_target - address;
  3459. // We need a stub if the branch offset is too large or if we need
  3460. // to switch mode.
  3461. bool may_use_blx = arm_target->may_use_v5t_interworking();
  3462. Reloc_stub* stub = NULL;
  3463. if (!parameters->options().relocatable()
  3464. && (Bits<26>::has_overflow32(branch_offset)
  3465. || ((thumb_bit != 0)
  3466. && !(may_use_blx && r_type == elfcpp::R_ARM_CALL))))
  3467. {
  3468. Valtype unadjusted_branch_target = psymval->value(object, 0);
  3469. Stub_type stub_type =
  3470. Reloc_stub::stub_type_for_reloc(r_type, address,
  3471. unadjusted_branch_target,
  3472. (thumb_bit != 0));
  3473. if (stub_type != arm_stub_none)
  3474. {
  3475. Stub_table<big_endian>* stub_table =
  3476. object->stub_table(relinfo->data_shndx);
  3477. gold_assert(stub_table != NULL);
  3478. Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
  3479. stub = stub_table->find_reloc_stub(stub_key);
  3480. gold_assert(stub != NULL);
  3481. thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
  3482. branch_target = stub_table->address() + stub->offset() + addend;
  3483. branch_offset = branch_target - address;
  3484. gold_assert(!Bits<26>::has_overflow32(branch_offset));
  3485. }
  3486. }
  3487. // At this point, if we still need to switch mode, the instruction
  3488. // must either be a BLX or a BL that can be converted to a BLX.
  3489. if (thumb_bit != 0)
  3490. {
  3491. // Turn BL to BLX.
  3492. gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
  3493. val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
  3494. }
  3495. val = Bits<32>::bit_select32(val, (branch_offset >> 2), 0xffffffUL);
  3496. elfcpp::Swap<32, big_endian>::writeval(wv, val);
  3497. return (Bits<26>::has_overflow32(branch_offset)
  3498. ? This::STATUS_OVERFLOW
  3499. : This::STATUS_OKAY);
  3500. }
  3501. // Relocate THUMB long branches. This handles relocation types
  3502. // R_ARM_THM_CALL, R_ARM_THM_JUMP24 and R_ARM_THM_XPC22.
  3503. // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
  3504. // undefined and we do not use PLT in this relocation. In such a case,
  3505. // the branch is converted into an NOP.
  3506. template<bool big_endian>
  3507. typename Arm_relocate_functions<big_endian>::Status
  3508. Arm_relocate_functions<big_endian>::thumb_branch_common(
  3509. unsigned int r_type,
  3510. const Relocate_info<32, big_endian>* relinfo,
  3511. unsigned char* view,
  3512. const Sized_symbol<32>* gsym,
  3513. const Arm_relobj<big_endian>* object,
  3514. unsigned int r_sym,
  3515. const Symbol_value<32>* psymval,
  3516. Arm_address address,
  3517. Arm_address thumb_bit,
  3518. bool is_weakly_undefined_without_plt)
  3519. {
  3520. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
  3521. Valtype* wv = reinterpret_cast<Valtype*>(view);
  3522. uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
  3523. uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
  3524. // FIXME: These tests are too loose and do not take THUMB/THUMB-2 difference
  3525. // into account.
  3526. bool is_bl_insn = (lower_insn & 0x1000U) == 0x1000U;
  3527. bool is_blx_insn = (lower_insn & 0x1000U) == 0x0000U;
  3528. // Check that the instruction is valid.
  3529. if (r_type == elfcpp::R_ARM_THM_CALL)
  3530. {
  3531. if (!is_bl_insn && !is_blx_insn)
  3532. return This::STATUS_BAD_RELOC;
  3533. }
  3534. else if (r_type == elfcpp::R_ARM_THM_JUMP24)
  3535. {
  3536. // This cannot be a BLX.
  3537. if (!is_bl_insn)
  3538. return This::STATUS_BAD_RELOC;
  3539. }
  3540. else if (r_type == elfcpp::R_ARM_THM_XPC22)
  3541. {
  3542. // Check for Thumb to Thumb call.
  3543. if (!is_blx_insn)
  3544. return This::STATUS_BAD_RELOC;
  3545. if (thumb_bit != 0)
  3546. {
  3547. gold_warning(_("%s: Thumb BLX instruction targets "
  3548. "thumb function '%s'."),
  3549. object->name().c_str(),
  3550. (gsym ? gsym->name() : "(local)"));
  3551. // Convert BLX to BL.
  3552. lower_insn |= 0x1000U;
  3553. }
  3554. }
  3555. else
  3556. gold_unreachable();
  3557. // A branch to an undefined weak symbol is turned into a jump to
  3558. // the next instruction unless a PLT entry will be created.
  3559. // The jump to the next instruction is optimized as a NOP.W for
  3560. // Thumb-2 enabled architectures.
  3561. const Target_arm<big_endian>* arm_target =
  3562. Target_arm<big_endian>::default_target();
  3563. if (is_weakly_undefined_without_plt)
  3564. {
  3565. gold_assert(!parameters->options().relocatable());
  3566. if (arm_target->may_use_thumb2_nop())
  3567. {
  3568. elfcpp::Swap<16, big_endian>::writeval(wv, 0xf3af);
  3569. elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0x8000);
  3570. }
  3571. else
  3572. {
  3573. elfcpp::Swap<16, big_endian>::writeval(wv, 0xe000);
  3574. elfcpp::Swap<16, big_endian>::writeval(wv + 1, 0xbf00);
  3575. }
  3576. return This::STATUS_OKAY;
  3577. }
  3578. int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
  3579. Arm_address branch_target = psymval->value(object, addend);
  3580. // For BLX, bit 1 of target address comes from bit 1 of base address.
  3581. bool may_use_blx = arm_target->may_use_v5t_interworking();
  3582. if (thumb_bit == 0 && may_use_blx)
  3583. branch_target = Bits<32>::bit_select32(branch_target, address, 0x2);
  3584. int32_t branch_offset = branch_target - address;
  3585. // We need a stub if the branch offset is too large or if we need
  3586. // to switch mode.
  3587. bool thumb2 = arm_target->using_thumb2();
  3588. if (!parameters->options().relocatable()
  3589. && ((!thumb2 && Bits<23>::has_overflow32(branch_offset))
  3590. || (thumb2 && Bits<25>::has_overflow32(branch_offset))
  3591. || ((thumb_bit == 0)
  3592. && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
  3593. || r_type == elfcpp::R_ARM_THM_JUMP24))))
  3594. {
  3595. Arm_address unadjusted_branch_target = psymval->value(object, 0);
  3596. Stub_type stub_type =
  3597. Reloc_stub::stub_type_for_reloc(r_type, address,
  3598. unadjusted_branch_target,
  3599. (thumb_bit != 0));
  3600. if (stub_type != arm_stub_none)
  3601. {
  3602. Stub_table<big_endian>* stub_table =
  3603. object->stub_table(relinfo->data_shndx);
  3604. gold_assert(stub_table != NULL);
  3605. Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
  3606. Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
  3607. gold_assert(stub != NULL);
  3608. thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
  3609. branch_target = stub_table->address() + stub->offset() + addend;
  3610. if (thumb_bit == 0 && may_use_blx)
  3611. branch_target = Bits<32>::bit_select32(branch_target, address, 0x2);
  3612. branch_offset = branch_target - address;
  3613. }
  3614. }
  3615. // At this point, if we still need to switch mode, the instruction
  3616. // must either be a BLX or a BL that can be converted to a BLX.
  3617. if (thumb_bit == 0)
  3618. {
  3619. gold_assert(may_use_blx
  3620. && (r_type == elfcpp::R_ARM_THM_CALL
  3621. || r_type == elfcpp::R_ARM_THM_XPC22));
  3622. // Make sure this is a BLX.
  3623. lower_insn &= ~0x1000U;
  3624. }
  3625. else
  3626. {
  3627. // Make sure this is a BL.
  3628. lower_insn |= 0x1000U;
  3629. }
  3630. // For a BLX instruction, make sure that the relocation is rounded up
  3631. // to a word boundary. This follows the semantics of the instruction
  3632. // which specifies that bit 1 of the target address will come from bit
  3633. // 1 of the base address.
  3634. if ((lower_insn & 0x5000U) == 0x4000U)
  3635. gold_assert((branch_offset & 3) == 0);
  3636. // Put BRANCH_OFFSET back into the insn. Assumes two's complement.
  3637. // We use the Thumb-2 encoding, which is safe even if dealing with
  3638. // a Thumb-1 instruction by virtue of our overflow check above. */
  3639. upper_insn = This::thumb32_branch_upper(upper_insn, branch_offset);
  3640. lower_insn = This::thumb32_branch_lower(lower_insn, branch_offset);
  3641. elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
  3642. elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
  3643. gold_assert(!Bits<25>::has_overflow32(branch_offset));
  3644. return ((thumb2
  3645. ? Bits<25>::has_overflow32(branch_offset)
  3646. : Bits<23>::has_overflow32(branch_offset))
  3647. ? This::STATUS_OVERFLOW
  3648. : This::STATUS_OKAY);
  3649. }
  3650. // Relocate THUMB-2 long conditional branches.
  3651. // If IS_WEAK_UNDEFINED_WITH_PLT is true. The target symbol is weakly
  3652. // undefined and we do not use PLT in this relocation. In such a case,
  3653. // the branch is converted into an NOP.
  3654. template<bool big_endian>
  3655. typename Arm_relocate_functions<big_endian>::Status
  3656. Arm_relocate_functions<big_endian>::thm_jump19(
  3657. unsigned char* view,
  3658. const Arm_relobj<big_endian>* object,
  3659. const Symbol_value<32>* psymval,
  3660. Arm_address address,
  3661. Arm_address thumb_bit)
  3662. {
  3663. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
  3664. Valtype* wv = reinterpret_cast<Valtype*>(view);
  3665. uint32_t upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
  3666. uint32_t lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
  3667. int32_t addend = This::thumb32_cond_branch_offset(upper_insn, lower_insn);
  3668. Arm_address branch_target = psymval->value(object, addend);
  3669. int32_t branch_offset = branch_target - address;
  3670. // ??? Should handle interworking? GCC might someday try to
  3671. // use this for tail calls.
  3672. // FIXME: We do support thumb entry to PLT yet.
  3673. if (thumb_bit == 0)
  3674. {
  3675. gold_error(_("conditional branch to PLT in THUMB-2 not supported yet."));
  3676. return This::STATUS_BAD_RELOC;
  3677. }
  3678. // Put RELOCATION back into the insn.
  3679. upper_insn = This::thumb32_cond_branch_upper(upper_insn, branch_offset);
  3680. lower_insn = This::thumb32_cond_branch_lower(lower_insn, branch_offset);
  3681. // Put the relocated value back in the object file:
  3682. elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
  3683. elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
  3684. return (Bits<21>::has_overflow32(branch_offset)
  3685. ? This::STATUS_OVERFLOW
  3686. : This::STATUS_OKAY);
  3687. }
  3688. // Get the GOT section, creating it if necessary.
  3689. template<bool big_endian>
  3690. Arm_output_data_got<big_endian>*
  3691. Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
  3692. {
  3693. if (this->got_ == NULL)
  3694. {
  3695. gold_assert(symtab != NULL && layout != NULL);
  3696. // When using -z now, we can treat .got as a relro section.
  3697. // Without -z now, it is modified after program startup by lazy
  3698. // PLT relocations.
  3699. bool is_got_relro = parameters->options().now();
  3700. Output_section_order got_order = (is_got_relro
  3701. ? ORDER_RELRO_LAST
  3702. : ORDER_DATA);
  3703. // Unlike some targets (.e.g x86), ARM does not use separate .got and
  3704. // .got.plt sections in output. The output .got section contains both
  3705. // PLT and non-PLT GOT entries.
  3706. this->got_ = new Arm_output_data_got<big_endian>(symtab, layout);
  3707. layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
  3708. (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
  3709. this->got_, got_order, is_got_relro);
  3710. // The old GNU linker creates a .got.plt section. We just
  3711. // create another set of data in the .got section. Note that we
  3712. // always create a PLT if we create a GOT, although the PLT
  3713. // might be empty.
  3714. this->got_plt_ = new Output_data_space(4, "** GOT PLT");
  3715. layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
  3716. (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
  3717. this->got_plt_, got_order, is_got_relro);
  3718. // The first three entries are reserved.
  3719. this->got_plt_->set_current_data_size(3 * 4);
  3720. // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
  3721. symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
  3722. Symbol_table::PREDEFINED,
  3723. this->got_plt_,
  3724. 0, 0, elfcpp::STT_OBJECT,
  3725. elfcpp::STB_LOCAL,
  3726. elfcpp::STV_HIDDEN, 0,
  3727. false, false);
  3728. // If there are any IRELATIVE relocations, they get GOT entries
  3729. // in .got.plt after the jump slot entries.
  3730. this->got_irelative_ = new Output_data_space(4, "** GOT IRELATIVE PLT");
  3731. layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
  3732. (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
  3733. this->got_irelative_,
  3734. got_order, is_got_relro);
  3735. }
  3736. return this->got_;
  3737. }
  3738. // Get the dynamic reloc section, creating it if necessary.
  3739. template<bool big_endian>
  3740. typename Target_arm<big_endian>::Reloc_section*
  3741. Target_arm<big_endian>::rel_dyn_section(Layout* layout)
  3742. {
  3743. if (this->rel_dyn_ == NULL)
  3744. {
  3745. gold_assert(layout != NULL);
  3746. // Create both relocation sections in the same place, so as to ensure
  3747. // their relative order in the output section.
  3748. this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
  3749. this->rel_irelative_ = new Reloc_section(false);
  3750. layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
  3751. elfcpp::SHF_ALLOC, this->rel_dyn_,
  3752. ORDER_DYNAMIC_RELOCS, false);
  3753. layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
  3754. elfcpp::SHF_ALLOC, this->rel_irelative_,
  3755. ORDER_DYNAMIC_RELOCS, false);
  3756. }
  3757. return this->rel_dyn_;
  3758. }
  3759. // Get the section to use for IRELATIVE relocs, creating it if necessary. These
  3760. // go in .rela.dyn, but only after all other dynamic relocations. They need to
  3761. // follow the other dynamic relocations so that they can refer to global
  3762. // variables initialized by those relocs.
  3763. template<bool big_endian>
  3764. typename Target_arm<big_endian>::Reloc_section*
  3765. Target_arm<big_endian>::rel_irelative_section(Layout* layout)
  3766. {
  3767. if (this->rel_irelative_ == NULL)
  3768. {
  3769. // Delegate the creation to rel_dyn_section so as to ensure their order in
  3770. // the output section.
  3771. this->rel_dyn_section(layout);
  3772. gold_assert(this->rel_irelative_ != NULL
  3773. && (this->rel_dyn_->output_section()
  3774. == this->rel_irelative_->output_section()));
  3775. }
  3776. return this->rel_irelative_;
  3777. }
  3778. // Insn_template methods.
  3779. // Return byte size of an instruction template.
  3780. size_t
  3781. Insn_template::size() const
  3782. {
  3783. switch (this->type())
  3784. {
  3785. case THUMB16_TYPE:
  3786. case THUMB16_SPECIAL_TYPE:
  3787. return 2;
  3788. case ARM_TYPE:
  3789. case THUMB32_TYPE:
  3790. case DATA_TYPE:
  3791. return 4;
  3792. default:
  3793. gold_unreachable();
  3794. }
  3795. }
  3796. // Return alignment of an instruction template.
  3797. unsigned
  3798. Insn_template::alignment() const
  3799. {
  3800. switch (this->type())
  3801. {
  3802. case THUMB16_TYPE:
  3803. case THUMB16_SPECIAL_TYPE:
  3804. case THUMB32_TYPE:
  3805. return 2;
  3806. case ARM_TYPE:
  3807. case DATA_TYPE:
  3808. return 4;
  3809. default:
  3810. gold_unreachable();
  3811. }
  3812. }
  3813. // Stub_template methods.
  3814. Stub_template::Stub_template(
  3815. Stub_type type, const Insn_template* insns,
  3816. size_t insn_count)
  3817. : type_(type), insns_(insns), insn_count_(insn_count), alignment_(1),
  3818. entry_in_thumb_mode_(false), relocs_()
  3819. {
  3820. off_t offset = 0;
  3821. // Compute byte size and alignment of stub template.
  3822. for (size_t i = 0; i < insn_count; i++)
  3823. {
  3824. unsigned insn_alignment = insns[i].alignment();
  3825. size_t insn_size = insns[i].size();
  3826. gold_assert((offset & (insn_alignment - 1)) == 0);
  3827. this->alignment_ = std::max(this->alignment_, insn_alignment);
  3828. switch (insns[i].type())
  3829. {
  3830. case Insn_template::THUMB16_TYPE:
  3831. case Insn_template::THUMB16_SPECIAL_TYPE:
  3832. if (i == 0)
  3833. this->entry_in_thumb_mode_ = true;
  3834. break;
  3835. case Insn_template::THUMB32_TYPE:
  3836. if (insns[i].r_type() != elfcpp::R_ARM_NONE)
  3837. this->relocs_.push_back(Reloc(i, offset));
  3838. if (i == 0)
  3839. this->entry_in_thumb_mode_ = true;
  3840. break;
  3841. case Insn_template::ARM_TYPE:
  3842. // Handle cases where the target is encoded within the
  3843. // instruction.
  3844. if (insns[i].r_type() == elfcpp::R_ARM_JUMP24)
  3845. this->relocs_.push_back(Reloc(i, offset));
  3846. break;
  3847. case Insn_template::DATA_TYPE:
  3848. // Entry point cannot be data.
  3849. gold_assert(i != 0);
  3850. this->relocs_.push_back(Reloc(i, offset));
  3851. break;
  3852. default:
  3853. gold_unreachable();
  3854. }
  3855. offset += insn_size;
  3856. }
  3857. this->size_ = offset;
  3858. }
  3859. // Stub methods.
  3860. // Template to implement do_write for a specific target endianness.
  3861. template<bool big_endian>
  3862. void inline
  3863. Stub::do_fixed_endian_write(unsigned char* view, section_size_type view_size)
  3864. {
  3865. const Stub_template* stub_template = this->stub_template();
  3866. const Insn_template* insns = stub_template->insns();
  3867. // FIXME: We do not handle BE8 encoding yet.
  3868. unsigned char* pov = view;
  3869. for (size_t i = 0; i < stub_template->insn_count(); i++)
  3870. {
  3871. switch (insns[i].type())
  3872. {
  3873. case Insn_template::THUMB16_TYPE:
  3874. elfcpp::Swap<16, big_endian>::writeval(pov, insns[i].data() & 0xffff);
  3875. break;
  3876. case Insn_template::THUMB16_SPECIAL_TYPE:
  3877. elfcpp::Swap<16, big_endian>::writeval(
  3878. pov,
  3879. this->thumb16_special(i));
  3880. break;
  3881. case Insn_template::THUMB32_TYPE:
  3882. {
  3883. uint32_t hi = (insns[i].data() >> 16) & 0xffff;
  3884. uint32_t lo = insns[i].data() & 0xffff;
  3885. elfcpp::Swap<16, big_endian>::writeval(pov, hi);
  3886. elfcpp::Swap<16, big_endian>::writeval(pov + 2, lo);
  3887. }
  3888. break;
  3889. case Insn_template::ARM_TYPE:
  3890. case Insn_template::DATA_TYPE:
  3891. elfcpp::Swap<32, big_endian>::writeval(pov, insns[i].data());
  3892. break;
  3893. default:
  3894. gold_unreachable();
  3895. }
  3896. pov += insns[i].size();
  3897. }
  3898. gold_assert(static_cast<section_size_type>(pov - view) == view_size);
  3899. }
  3900. // Reloc_stub::Key methods.
  3901. // Dump a Key as a string for debugging.
  3902. std::string
  3903. Reloc_stub::Key::name() const
  3904. {
  3905. if (this->r_sym_ == invalid_index)
  3906. {
  3907. // Global symbol key name
  3908. // <stub-type>:<symbol name>:<addend>.
  3909. const std::string sym_name = this->u_.symbol->name();
  3910. // We need to print two hex number and two colons. So just add 100 bytes
  3911. // to the symbol name size.
  3912. size_t len = sym_name.size() + 100;
  3913. char* buffer = new char[len];
  3914. int c = snprintf(buffer, len, "%d:%s:%x", this->stub_type_,
  3915. sym_name.c_str(), this->addend_);
  3916. gold_assert(c > 0 && c < static_cast<int>(len));
  3917. delete[] buffer;
  3918. return std::string(buffer);
  3919. }
  3920. else
  3921. {
  3922. // local symbol key name
  3923. // <stub-type>:<object>:<r_sym>:<addend>.
  3924. const size_t len = 200;
  3925. char buffer[len];
  3926. int c = snprintf(buffer, len, "%d:%p:%u:%x", this->stub_type_,
  3927. this->u_.relobj, this->r_sym_, this->addend_);
  3928. gold_assert(c > 0 && c < static_cast<int>(len));
  3929. return std::string(buffer);
  3930. }
  3931. }
  3932. // Reloc_stub methods.
  3933. // Determine the type of stub needed, if any, for a relocation of R_TYPE at
  3934. // LOCATION to DESTINATION.
  3935. // This code is based on the arm_type_of_stub function in
  3936. // bfd/elf32-arm.c. We have changed the interface a little to keep the Stub
  3937. // class simple.
  3938. Stub_type
  3939. Reloc_stub::stub_type_for_reloc(
  3940. unsigned int r_type,
  3941. Arm_address location,
  3942. Arm_address destination,
  3943. bool target_is_thumb)
  3944. {
  3945. Stub_type stub_type = arm_stub_none;
  3946. // This is a bit ugly but we want to avoid using a templated class for
  3947. // big and little endianities.
  3948. bool may_use_blx;
  3949. bool should_force_pic_veneer = parameters->options().pic_veneer();
  3950. bool thumb2;
  3951. bool thumb_only;
  3952. if (parameters->target().is_big_endian())
  3953. {
  3954. const Target_arm<true>* big_endian_target =
  3955. Target_arm<true>::default_target();
  3956. may_use_blx = big_endian_target->may_use_v5t_interworking();
  3957. should_force_pic_veneer |= big_endian_target->should_force_pic_veneer();
  3958. thumb2 = big_endian_target->using_thumb2();
  3959. thumb_only = big_endian_target->using_thumb_only();
  3960. }
  3961. else
  3962. {
  3963. const Target_arm<false>* little_endian_target =
  3964. Target_arm<false>::default_target();
  3965. may_use_blx = little_endian_target->may_use_v5t_interworking();
  3966. should_force_pic_veneer |=
  3967. little_endian_target->should_force_pic_veneer();
  3968. thumb2 = little_endian_target->using_thumb2();
  3969. thumb_only = little_endian_target->using_thumb_only();
  3970. }
  3971. int64_t branch_offset;
  3972. bool output_is_position_independent =
  3973. parameters->options().output_is_position_independent();
  3974. if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
  3975. {
  3976. // For THUMB BLX instruction, bit 1 of target comes from bit 1 of the
  3977. // base address (instruction address + 4).
  3978. if ((r_type == elfcpp::R_ARM_THM_CALL) && may_use_blx && !target_is_thumb)
  3979. destination = Bits<32>::bit_select32(destination, location, 0x2);
  3980. branch_offset = static_cast<int64_t>(destination) - location;
  3981. // Handle cases where:
  3982. // - this call goes too far (different Thumb/Thumb2 max
  3983. // distance)
  3984. // - it's a Thumb->Arm call and blx is not available, or it's a
  3985. // Thumb->Arm branch (not bl). A stub is needed in this case.
  3986. if ((!thumb2
  3987. && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
  3988. || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
  3989. || (thumb2
  3990. && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
  3991. || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
  3992. || ((!target_is_thumb)
  3993. && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
  3994. || (r_type == elfcpp::R_ARM_THM_JUMP24))))
  3995. {
  3996. if (target_is_thumb)
  3997. {
  3998. // Thumb to thumb.
  3999. if (!thumb_only)
  4000. {
  4001. stub_type = (output_is_position_independent
  4002. || should_force_pic_veneer)
  4003. // PIC stubs.
  4004. ? ((may_use_blx
  4005. && (r_type == elfcpp::R_ARM_THM_CALL))
  4006. // V5T and above. Stub starts with ARM code, so
  4007. // we must be able to switch mode before
  4008. // reaching it, which is only possible for 'bl'
  4009. // (ie R_ARM_THM_CALL relocation).
  4010. ? arm_stub_long_branch_any_thumb_pic
  4011. // On V4T, use Thumb code only.
  4012. : arm_stub_long_branch_v4t_thumb_thumb_pic)
  4013. // non-PIC stubs.
  4014. : ((may_use_blx
  4015. && (r_type == elfcpp::R_ARM_THM_CALL))
  4016. ? arm_stub_long_branch_any_any // V5T and above.
  4017. : arm_stub_long_branch_v4t_thumb_thumb); // V4T.
  4018. }
  4019. else
  4020. {
  4021. stub_type = (output_is_position_independent
  4022. || should_force_pic_veneer)
  4023. ? arm_stub_long_branch_thumb_only_pic // PIC stub.
  4024. : arm_stub_long_branch_thumb_only; // non-PIC stub.
  4025. }
  4026. }
  4027. else
  4028. {
  4029. // Thumb to arm.
  4030. // FIXME: We should check that the input section is from an
  4031. // object that has interwork enabled.
  4032. stub_type = (output_is_position_independent
  4033. || should_force_pic_veneer)
  4034. // PIC stubs.
  4035. ? ((may_use_blx
  4036. && (r_type == elfcpp::R_ARM_THM_CALL))
  4037. ? arm_stub_long_branch_any_arm_pic // V5T and above.
  4038. : arm_stub_long_branch_v4t_thumb_arm_pic) // V4T.
  4039. // non-PIC stubs.
  4040. : ((may_use_blx
  4041. && (r_type == elfcpp::R_ARM_THM_CALL))
  4042. ? arm_stub_long_branch_any_any // V5T and above.
  4043. : arm_stub_long_branch_v4t_thumb_arm); // V4T.
  4044. // Handle v4t short branches.
  4045. if ((stub_type == arm_stub_long_branch_v4t_thumb_arm)
  4046. && (branch_offset <= THM_MAX_FWD_BRANCH_OFFSET)
  4047. && (branch_offset >= THM_MAX_BWD_BRANCH_OFFSET))
  4048. stub_type = arm_stub_short_branch_v4t_thumb_arm;
  4049. }
  4050. }
  4051. }
  4052. else if (r_type == elfcpp::R_ARM_CALL
  4053. || r_type == elfcpp::R_ARM_JUMP24
  4054. || r_type == elfcpp::R_ARM_PLT32)
  4055. {
  4056. branch_offset = static_cast<int64_t>(destination) - location;
  4057. if (target_is_thumb)
  4058. {
  4059. // Arm to thumb.
  4060. // FIXME: We should check that the input section is from an
  4061. // object that has interwork enabled.
  4062. // We have an extra 2-bytes reach because of
  4063. // the mode change (bit 24 (H) of BLX encoding).
  4064. if (branch_offset > (ARM_MAX_FWD_BRANCH_OFFSET + 2)
  4065. || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
  4066. || ((r_type == elfcpp::R_ARM_CALL) && !may_use_blx)
  4067. || (r_type == elfcpp::R_ARM_JUMP24)
  4068. || (r_type == elfcpp::R_ARM_PLT32))
  4069. {
  4070. stub_type = (output_is_position_independent
  4071. || should_force_pic_veneer)
  4072. // PIC stubs.
  4073. ? (may_use_blx
  4074. ? arm_stub_long_branch_any_thumb_pic// V5T and above.
  4075. : arm_stub_long_branch_v4t_arm_thumb_pic) // V4T stub.
  4076. // non-PIC stubs.
  4077. : (may_use_blx
  4078. ? arm_stub_long_branch_any_any // V5T and above.
  4079. : arm_stub_long_branch_v4t_arm_thumb); // V4T.
  4080. }
  4081. }
  4082. else
  4083. {
  4084. // Arm to arm.
  4085. if (branch_offset > ARM_MAX_FWD_BRANCH_OFFSET
  4086. || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET))
  4087. {
  4088. stub_type = (output_is_position_independent
  4089. || should_force_pic_veneer)
  4090. ? arm_stub_long_branch_any_arm_pic // PIC stubs.
  4091. : arm_stub_long_branch_any_any; /// non-PIC.
  4092. }
  4093. }
  4094. }
  4095. return stub_type;
  4096. }
  4097. // Cortex_a8_stub methods.
  4098. // Return the instruction for a THUMB16_SPECIAL_TYPE instruction template.
  4099. // I is the position of the instruction template in the stub template.
  4100. uint16_t
  4101. Cortex_a8_stub::do_thumb16_special(size_t i)
  4102. {
  4103. // The only use of this is to copy condition code from a conditional
  4104. // branch being worked around to the corresponding conditional branch in
  4105. // to the stub.
  4106. gold_assert(this->stub_template()->type() == arm_stub_a8_veneer_b_cond
  4107. && i == 0);
  4108. uint16_t data = this->stub_template()->insns()[i].data();
  4109. gold_assert((data & 0xff00U) == 0xd000U);
  4110. data |= ((this->original_insn_ >> 22) & 0xf) << 8;
  4111. return data;
  4112. }
  4113. // Stub_factory methods.
  4114. Stub_factory::Stub_factory()
  4115. {
  4116. // The instruction template sequences are declared as static
  4117. // objects and initialized first time the constructor runs.
  4118. // Arm/Thumb -> Arm/Thumb long branch stub. On V5T and above, use blx
  4119. // to reach the stub if necessary.
  4120. static const Insn_template elf32_arm_stub_long_branch_any_any[] =
  4121. {
  4122. Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
  4123. Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
  4124. // dcd R_ARM_ABS32(X)
  4125. };
  4126. // V4T Arm -> Thumb long branch stub. Used on V4T where blx is not
  4127. // available.
  4128. static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb[] =
  4129. {
  4130. Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
  4131. Insn_template::arm_insn(0xe12fff1c), // bx ip
  4132. Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
  4133. // dcd R_ARM_ABS32(X)
  4134. };
  4135. // Thumb -> Thumb long branch stub. Used on M-profile architectures.
  4136. static const Insn_template elf32_arm_stub_long_branch_thumb_only[] =
  4137. {
  4138. Insn_template::thumb16_insn(0xb401), // push {r0}
  4139. Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
  4140. Insn_template::thumb16_insn(0x4684), // mov ip, r0
  4141. Insn_template::thumb16_insn(0xbc01), // pop {r0}
  4142. Insn_template::thumb16_insn(0x4760), // bx ip
  4143. Insn_template::thumb16_insn(0xbf00), // nop
  4144. Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
  4145. // dcd R_ARM_ABS32(X)
  4146. };
  4147. // V4T Thumb -> Thumb long branch stub. Using the stack is not
  4148. // allowed.
  4149. static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb[] =
  4150. {
  4151. Insn_template::thumb16_insn(0x4778), // bx pc
  4152. Insn_template::thumb16_insn(0x46c0), // nop
  4153. Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
  4154. Insn_template::arm_insn(0xe12fff1c), // bx ip
  4155. Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
  4156. // dcd R_ARM_ABS32(X)
  4157. };
  4158. // V4T Thumb -> ARM long branch stub. Used on V4T where blx is not
  4159. // available.
  4160. static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm[] =
  4161. {
  4162. Insn_template::thumb16_insn(0x4778), // bx pc
  4163. Insn_template::thumb16_insn(0x46c0), // nop
  4164. Insn_template::arm_insn(0xe51ff004), // ldr pc, [pc, #-4]
  4165. Insn_template::data_word(0, elfcpp::R_ARM_ABS32, 0),
  4166. // dcd R_ARM_ABS32(X)
  4167. };
  4168. // V4T Thumb -> ARM short branch stub. Shorter variant of the above
  4169. // one, when the destination is close enough.
  4170. static const Insn_template elf32_arm_stub_short_branch_v4t_thumb_arm[] =
  4171. {
  4172. Insn_template::thumb16_insn(0x4778), // bx pc
  4173. Insn_template::thumb16_insn(0x46c0), // nop
  4174. Insn_template::arm_rel_insn(0xea000000, -8), // b (X-8)
  4175. };
  4176. // ARM/Thumb -> ARM long branch stub, PIC. On V5T and above, use
  4177. // blx to reach the stub if necessary.
  4178. static const Insn_template elf32_arm_stub_long_branch_any_arm_pic[] =
  4179. {
  4180. Insn_template::arm_insn(0xe59fc000), // ldr r12, [pc]
  4181. Insn_template::arm_insn(0xe08ff00c), // add pc, pc, ip
  4182. Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
  4183. // dcd R_ARM_REL32(X-4)
  4184. };
  4185. // ARM/Thumb -> Thumb long branch stub, PIC. On V5T and above, use
  4186. // blx to reach the stub if necessary. We can not add into pc;
  4187. // it is not guaranteed to mode switch (different in ARMv6 and
  4188. // ARMv7).
  4189. static const Insn_template elf32_arm_stub_long_branch_any_thumb_pic[] =
  4190. {
  4191. Insn_template::arm_insn(0xe59fc004), // ldr r12, [pc, #4]
  4192. Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
  4193. Insn_template::arm_insn(0xe12fff1c), // bx ip
  4194. Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
  4195. // dcd R_ARM_REL32(X)
  4196. };
  4197. // V4T ARM -> ARM long branch stub, PIC.
  4198. static const Insn_template elf32_arm_stub_long_branch_v4t_arm_thumb_pic[] =
  4199. {
  4200. Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
  4201. Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
  4202. Insn_template::arm_insn(0xe12fff1c), // bx ip
  4203. Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
  4204. // dcd R_ARM_REL32(X)
  4205. };
  4206. // V4T Thumb -> ARM long branch stub, PIC.
  4207. static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_arm_pic[] =
  4208. {
  4209. Insn_template::thumb16_insn(0x4778), // bx pc
  4210. Insn_template::thumb16_insn(0x46c0), // nop
  4211. Insn_template::arm_insn(0xe59fc000), // ldr ip, [pc, #0]
  4212. Insn_template::arm_insn(0xe08cf00f), // add pc, ip, pc
  4213. Insn_template::data_word(0, elfcpp::R_ARM_REL32, -4),
  4214. // dcd R_ARM_REL32(X)
  4215. };
  4216. // Thumb -> Thumb long branch stub, PIC. Used on M-profile
  4217. // architectures.
  4218. static const Insn_template elf32_arm_stub_long_branch_thumb_only_pic[] =
  4219. {
  4220. Insn_template::thumb16_insn(0xb401), // push {r0}
  4221. Insn_template::thumb16_insn(0x4802), // ldr r0, [pc, #8]
  4222. Insn_template::thumb16_insn(0x46fc), // mov ip, pc
  4223. Insn_template::thumb16_insn(0x4484), // add ip, r0
  4224. Insn_template::thumb16_insn(0xbc01), // pop {r0}
  4225. Insn_template::thumb16_insn(0x4760), // bx ip
  4226. Insn_template::data_word(0, elfcpp::R_ARM_REL32, 4),
  4227. // dcd R_ARM_REL32(X)
  4228. };
  4229. // V4T Thumb -> Thumb long branch stub, PIC. Using the stack is not
  4230. // allowed.
  4231. static const Insn_template elf32_arm_stub_long_branch_v4t_thumb_thumb_pic[] =
  4232. {
  4233. Insn_template::thumb16_insn(0x4778), // bx pc
  4234. Insn_template::thumb16_insn(0x46c0), // nop
  4235. Insn_template::arm_insn(0xe59fc004), // ldr ip, [pc, #4]
  4236. Insn_template::arm_insn(0xe08fc00c), // add ip, pc, ip
  4237. Insn_template::arm_insn(0xe12fff1c), // bx ip
  4238. Insn_template::data_word(0, elfcpp::R_ARM_REL32, 0),
  4239. // dcd R_ARM_REL32(X)
  4240. };
  4241. // Cortex-A8 erratum-workaround stubs.
  4242. // Stub used for conditional branches (which may be beyond +/-1MB away,
  4243. // so we can't use a conditional branch to reach this stub).
  4244. // original code:
  4245. //
  4246. // b<cond> X
  4247. // after:
  4248. //
  4249. static const Insn_template elf32_arm_stub_a8_veneer_b_cond[] =
  4250. {
  4251. Insn_template::thumb16_bcond_insn(0xd001), // b<cond>.n true
  4252. Insn_template::thumb32_b_insn(0xf000b800, -4), // b.w after
  4253. Insn_template::thumb32_b_insn(0xf000b800, -4) // true:
  4254. // b.w X
  4255. };
  4256. // Stub used for b.w and bl.w instructions.
  4257. static const Insn_template elf32_arm_stub_a8_veneer_b[] =
  4258. {
  4259. Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
  4260. };
  4261. static const Insn_template elf32_arm_stub_a8_veneer_bl[] =
  4262. {
  4263. Insn_template::thumb32_b_insn(0xf000b800, -4) // b.w dest
  4264. };
  4265. // Stub used for Thumb-2 blx.w instructions. We modified the original blx.w
  4266. // instruction (which switches to ARM mode) to point to this stub. Jump to
  4267. // the real destination using an ARM-mode branch.
  4268. static const Insn_template elf32_arm_stub_a8_veneer_blx[] =
  4269. {
  4270. Insn_template::arm_rel_insn(0xea000000, -8) // b dest
  4271. };
  4272. // Stub used to provide an interworking for R_ARM_V4BX relocation
  4273. // (bx r[n] instruction).
  4274. static const Insn_template elf32_arm_stub_v4_veneer_bx[] =
  4275. {
  4276. Insn_template::arm_insn(0xe3100001), // tst r<n>, #1
  4277. Insn_template::arm_insn(0x01a0f000), // moveq pc, r<n>
  4278. Insn_template::arm_insn(0xe12fff10) // bx r<n>
  4279. };
  4280. // Fill in the stub template look-up table. Stub templates are constructed
  4281. // per instance of Stub_factory for fast look-up without locking
  4282. // in a thread-enabled environment.
  4283. this->stub_templates_[arm_stub_none] =
  4284. new Stub_template(arm_stub_none, NULL, 0);
  4285. #define DEF_STUB(x) \
  4286. do \
  4287. { \
  4288. size_t array_size \
  4289. = sizeof(elf32_arm_stub_##x) / sizeof(elf32_arm_stub_##x[0]); \
  4290. Stub_type type = arm_stub_##x; \
  4291. this->stub_templates_[type] = \
  4292. new Stub_template(type, elf32_arm_stub_##x, array_size); \
  4293. } \
  4294. while (0);
  4295. DEF_STUBS
  4296. #undef DEF_STUB
  4297. }
  4298. // Stub_table methods.
  4299. // Remove all Cortex-A8 stub.
  4300. template<bool big_endian>
  4301. void
  4302. Stub_table<big_endian>::remove_all_cortex_a8_stubs()
  4303. {
  4304. for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
  4305. p != this->cortex_a8_stubs_.end();
  4306. ++p)
  4307. delete p->second;
  4308. this->cortex_a8_stubs_.clear();
  4309. }
  4310. // Relocate one stub. This is a helper for Stub_table::relocate_stubs().
  4311. template<bool big_endian>
  4312. void
  4313. Stub_table<big_endian>::relocate_stub(
  4314. Stub* stub,
  4315. const Relocate_info<32, big_endian>* relinfo,
  4316. Target_arm<big_endian>* arm_target,
  4317. Output_section* output_section,
  4318. unsigned char* view,
  4319. Arm_address address,
  4320. section_size_type view_size)
  4321. {
  4322. const Stub_template* stub_template = stub->stub_template();
  4323. if (stub_template->reloc_count() != 0)
  4324. {
  4325. // Adjust view to cover the stub only.
  4326. section_size_type offset = stub->offset();
  4327. section_size_type stub_size = stub_template->size();
  4328. gold_assert(offset + stub_size <= view_size);
  4329. arm_target->relocate_stub(stub, relinfo, output_section, view + offset,
  4330. address + offset, stub_size);
  4331. }
  4332. }
  4333. // Relocate all stubs in this stub table.
  4334. template<bool big_endian>
  4335. void
  4336. Stub_table<big_endian>::relocate_stubs(
  4337. const Relocate_info<32, big_endian>* relinfo,
  4338. Target_arm<big_endian>* arm_target,
  4339. Output_section* output_section,
  4340. unsigned char* view,
  4341. Arm_address address,
  4342. section_size_type view_size)
  4343. {
  4344. // If we are passed a view bigger than the stub table's. we need to
  4345. // adjust the view.
  4346. gold_assert(address == this->address()
  4347. && (view_size
  4348. == static_cast<section_size_type>(this->data_size())));
  4349. // Relocate all relocation stubs.
  4350. for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
  4351. p != this->reloc_stubs_.end();
  4352. ++p)
  4353. this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
  4354. address, view_size);
  4355. // Relocate all Cortex-A8 stubs.
  4356. for (Cortex_a8_stub_list::iterator p = this->cortex_a8_stubs_.begin();
  4357. p != this->cortex_a8_stubs_.end();
  4358. ++p)
  4359. this->relocate_stub(p->second, relinfo, arm_target, output_section, view,
  4360. address, view_size);
  4361. // Relocate all ARM V4BX stubs.
  4362. for (Arm_v4bx_stub_list::iterator p = this->arm_v4bx_stubs_.begin();
  4363. p != this->arm_v4bx_stubs_.end();
  4364. ++p)
  4365. {
  4366. if (*p != NULL)
  4367. this->relocate_stub(*p, relinfo, arm_target, output_section, view,
  4368. address, view_size);
  4369. }
  4370. }
  4371. // Write out the stubs to file.
  4372. template<bool big_endian>
  4373. void
  4374. Stub_table<big_endian>::do_write(Output_file* of)
  4375. {
  4376. off_t offset = this->offset();
  4377. const section_size_type oview_size =
  4378. convert_to_section_size_type(this->data_size());
  4379. unsigned char* const oview = of->get_output_view(offset, oview_size);
  4380. // Write relocation stubs.
  4381. for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
  4382. p != this->reloc_stubs_.end();
  4383. ++p)
  4384. {
  4385. Reloc_stub* stub = p->second;
  4386. Arm_address address = this->address() + stub->offset();
  4387. gold_assert(address
  4388. == align_address(address,
  4389. stub->stub_template()->alignment()));
  4390. stub->write(oview + stub->offset(), stub->stub_template()->size(),
  4391. big_endian);
  4392. }
  4393. // Write Cortex-A8 stubs.
  4394. for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
  4395. p != this->cortex_a8_stubs_.end();
  4396. ++p)
  4397. {
  4398. Cortex_a8_stub* stub = p->second;
  4399. Arm_address address = this->address() + stub->offset();
  4400. gold_assert(address
  4401. == align_address(address,
  4402. stub->stub_template()->alignment()));
  4403. stub->write(oview + stub->offset(), stub->stub_template()->size(),
  4404. big_endian);
  4405. }
  4406. // Write ARM V4BX relocation stubs.
  4407. for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
  4408. p != this->arm_v4bx_stubs_.end();
  4409. ++p)
  4410. {
  4411. if (*p == NULL)
  4412. continue;
  4413. Arm_address address = this->address() + (*p)->offset();
  4414. gold_assert(address
  4415. == align_address(address,
  4416. (*p)->stub_template()->alignment()));
  4417. (*p)->write(oview + (*p)->offset(), (*p)->stub_template()->size(),
  4418. big_endian);
  4419. }
  4420. of->write_output_view(this->offset(), oview_size, oview);
  4421. }
  4422. // Update the data size and address alignment of the stub table at the end
  4423. // of a relaxation pass. Return true if either the data size or the
  4424. // alignment changed in this relaxation pass.
  4425. template<bool big_endian>
  4426. bool
  4427. Stub_table<big_endian>::update_data_size_and_addralign()
  4428. {
  4429. // Go over all stubs in table to compute data size and address alignment.
  4430. off_t size = this->reloc_stubs_size_;
  4431. unsigned addralign = this->reloc_stubs_addralign_;
  4432. for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
  4433. p != this->cortex_a8_stubs_.end();
  4434. ++p)
  4435. {
  4436. const Stub_template* stub_template = p->second->stub_template();
  4437. addralign = std::max(addralign, stub_template->alignment());
  4438. size = (align_address(size, stub_template->alignment())
  4439. + stub_template->size());
  4440. }
  4441. for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
  4442. p != this->arm_v4bx_stubs_.end();
  4443. ++p)
  4444. {
  4445. if (*p == NULL)
  4446. continue;
  4447. const Stub_template* stub_template = (*p)->stub_template();
  4448. addralign = std::max(addralign, stub_template->alignment());
  4449. size = (align_address(size, stub_template->alignment())
  4450. + stub_template->size());
  4451. }
  4452. // Check if either data size or alignment changed in this pass.
  4453. // Update prev_data_size_ and prev_addralign_. These will be used
  4454. // as the current data size and address alignment for the next pass.
  4455. bool changed = size != this->prev_data_size_;
  4456. this->prev_data_size_ = size;
  4457. if (addralign != this->prev_addralign_)
  4458. changed = true;
  4459. this->prev_addralign_ = addralign;
  4460. return changed;
  4461. }
  4462. // Finalize the stubs. This sets the offsets of the stubs within the stub
  4463. // table. It also marks all input sections needing Cortex-A8 workaround.
  4464. template<bool big_endian>
  4465. void
  4466. Stub_table<big_endian>::finalize_stubs()
  4467. {
  4468. off_t off = this->reloc_stubs_size_;
  4469. for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
  4470. p != this->cortex_a8_stubs_.end();
  4471. ++p)
  4472. {
  4473. Cortex_a8_stub* stub = p->second;
  4474. const Stub_template* stub_template = stub->stub_template();
  4475. uint64_t stub_addralign = stub_template->alignment();
  4476. off = align_address(off, stub_addralign);
  4477. stub->set_offset(off);
  4478. off += stub_template->size();
  4479. // Mark input section so that we can determine later if a code section
  4480. // needs the Cortex-A8 workaround quickly.
  4481. Arm_relobj<big_endian>* arm_relobj =
  4482. Arm_relobj<big_endian>::as_arm_relobj(stub->relobj());
  4483. arm_relobj->mark_section_for_cortex_a8_workaround(stub->shndx());
  4484. }
  4485. for (Arm_v4bx_stub_list::const_iterator p = this->arm_v4bx_stubs_.begin();
  4486. p != this->arm_v4bx_stubs_.end();
  4487. ++p)
  4488. {
  4489. if (*p == NULL)
  4490. continue;
  4491. const Stub_template* stub_template = (*p)->stub_template();
  4492. uint64_t stub_addralign = stub_template->alignment();
  4493. off = align_address(off, stub_addralign);
  4494. (*p)->set_offset(off);
  4495. off += stub_template->size();
  4496. }
  4497. gold_assert(off <= this->prev_data_size_);
  4498. }
  4499. // Apply Cortex-A8 workaround to an address range between VIEW_ADDRESS
  4500. // and VIEW_ADDRESS + VIEW_SIZE - 1. VIEW points to the mapped address
  4501. // of the address range seen by the linker.
  4502. template<bool big_endian>
  4503. void
  4504. Stub_table<big_endian>::apply_cortex_a8_workaround_to_address_range(
  4505. Target_arm<big_endian>* arm_target,
  4506. unsigned char* view,
  4507. Arm_address view_address,
  4508. section_size_type view_size)
  4509. {
  4510. // Cortex-A8 stubs are sorted by addresses of branches being fixed up.
  4511. for (Cortex_a8_stub_list::const_iterator p =
  4512. this->cortex_a8_stubs_.lower_bound(view_address);
  4513. ((p != this->cortex_a8_stubs_.end())
  4514. && (p->first < (view_address + view_size)));
  4515. ++p)
  4516. {
  4517. // We do not store the THUMB bit in the LSB of either the branch address
  4518. // or the stub offset. There is no need to strip the LSB.
  4519. Arm_address branch_address = p->first;
  4520. const Cortex_a8_stub* stub = p->second;
  4521. Arm_address stub_address = this->address() + stub->offset();
  4522. // Offset of the branch instruction relative to this view.
  4523. section_size_type offset =
  4524. convert_to_section_size_type(branch_address - view_address);
  4525. gold_assert((offset + 4) <= view_size);
  4526. arm_target->apply_cortex_a8_workaround(stub, stub_address,
  4527. view + offset, branch_address);
  4528. }
  4529. }
  4530. // Arm_input_section methods.
  4531. // Initialize an Arm_input_section.
  4532. template<bool big_endian>
  4533. void
  4534. Arm_input_section<big_endian>::init()
  4535. {
  4536. Relobj* relobj = this->relobj();
  4537. unsigned int shndx = this->shndx();
  4538. // We have to cache original size, alignment and contents to avoid locking
  4539. // the original file.
  4540. this->original_addralign_ =
  4541. convert_types<uint32_t, uint64_t>(relobj->section_addralign(shndx));
  4542. // This is not efficient but we expect only a small number of relaxed
  4543. // input sections for stubs.
  4544. section_size_type section_size;
  4545. const unsigned char* section_contents =
  4546. relobj->section_contents(shndx, &section_size, false);
  4547. this->original_size_ =
  4548. convert_types<uint32_t, uint64_t>(relobj->section_size(shndx));
  4549. gold_assert(this->original_contents_ == NULL);
  4550. this->original_contents_ = new unsigned char[section_size];
  4551. memcpy(this->original_contents_, section_contents, section_size);
  4552. // We want to make this look like the original input section after
  4553. // output sections are finalized.
  4554. Output_section* os = relobj->output_section(shndx);
  4555. off_t offset = relobj->output_section_offset(shndx);
  4556. gold_assert(os != NULL && !relobj->is_output_section_offset_invalid(shndx));
  4557. this->set_address(os->address() + offset);
  4558. this->set_file_offset(os->offset() + offset);
  4559. this->set_current_data_size(this->original_size_);
  4560. this->finalize_data_size();
  4561. }
  4562. template<bool big_endian>
  4563. void
  4564. Arm_input_section<big_endian>::do_write(Output_file* of)
  4565. {
  4566. // We have to write out the original section content.
  4567. gold_assert(this->original_contents_ != NULL);
  4568. of->write(this->offset(), this->original_contents_,
  4569. this->original_size_);
  4570. // If this owns a stub table and it is not empty, write it.
  4571. if (this->is_stub_table_owner() && !this->stub_table_->empty())
  4572. this->stub_table_->write(of);
  4573. }
  4574. // Finalize data size.
  4575. template<bool big_endian>
  4576. void
  4577. Arm_input_section<big_endian>::set_final_data_size()
  4578. {
  4579. off_t off = convert_types<off_t, uint64_t>(this->original_size_);
  4580. if (this->is_stub_table_owner())
  4581. {
  4582. this->stub_table_->finalize_data_size();
  4583. off = align_address(off, this->stub_table_->addralign());
  4584. off += this->stub_table_->data_size();
  4585. }
  4586. this->set_data_size(off);
  4587. }
  4588. // Reset address and file offset.
  4589. template<bool big_endian>
  4590. void
  4591. Arm_input_section<big_endian>::do_reset_address_and_file_offset()
  4592. {
  4593. // Size of the original input section contents.
  4594. off_t off = convert_types<off_t, uint64_t>(this->original_size_);
  4595. // If this is a stub table owner, account for the stub table size.
  4596. if (this->is_stub_table_owner())
  4597. {
  4598. Stub_table<big_endian>* stub_table = this->stub_table_;
  4599. // Reset the stub table's address and file offset. The
  4600. // current data size for child will be updated after that.
  4601. stub_table_->reset_address_and_file_offset();
  4602. off = align_address(off, stub_table_->addralign());
  4603. off += stub_table->current_data_size();
  4604. }
  4605. this->set_current_data_size(off);
  4606. }
  4607. // Arm_exidx_cantunwind methods.
  4608. // Write this to Output file OF for a fixed endianness.
  4609. template<bool big_endian>
  4610. void
  4611. Arm_exidx_cantunwind::do_fixed_endian_write(Output_file* of)
  4612. {
  4613. off_t offset = this->offset();
  4614. const section_size_type oview_size = 8;
  4615. unsigned char* const oview = of->get_output_view(offset, oview_size);
  4616. Output_section* os = this->relobj_->output_section(this->shndx_);
  4617. gold_assert(os != NULL);
  4618. Arm_relobj<big_endian>* arm_relobj =
  4619. Arm_relobj<big_endian>::as_arm_relobj(this->relobj_);
  4620. Arm_address output_offset =
  4621. arm_relobj->get_output_section_offset(this->shndx_);
  4622. Arm_address section_start;
  4623. section_size_type section_size;
  4624. // Find out the end of the text section referred by this.
  4625. if (output_offset != Arm_relobj<big_endian>::invalid_address)
  4626. {
  4627. section_start = os->address() + output_offset;
  4628. const Arm_exidx_input_section* exidx_input_section =
  4629. arm_relobj->exidx_input_section_by_link(this->shndx_);
  4630. gold_assert(exidx_input_section != NULL);
  4631. section_size =
  4632. convert_to_section_size_type(exidx_input_section->text_size());
  4633. }
  4634. else
  4635. {
  4636. // Currently this only happens for a relaxed section.
  4637. const Output_relaxed_input_section* poris =
  4638. os->find_relaxed_input_section(this->relobj_, this->shndx_);
  4639. gold_assert(poris != NULL);
  4640. section_start = poris->address();
  4641. section_size = convert_to_section_size_type(poris->data_size());
  4642. }
  4643. // We always append this to the end of an EXIDX section.
  4644. Arm_address output_address = section_start + section_size;
  4645. // Write out the entry. The first word either points to the beginning
  4646. // or after the end of a text section. The second word is the special
  4647. // EXIDX_CANTUNWIND value.
  4648. uint32_t prel31_offset = output_address - this->address();
  4649. if (Bits<31>::has_overflow32(offset))
  4650. gold_error(_("PREL31 overflow in EXIDX_CANTUNWIND entry"));
  4651. elfcpp::Swap_unaligned<32, big_endian>::writeval(oview,
  4652. prel31_offset & 0x7fffffffU);
  4653. elfcpp::Swap_unaligned<32, big_endian>::writeval(oview + 4,
  4654. elfcpp::EXIDX_CANTUNWIND);
  4655. of->write_output_view(this->offset(), oview_size, oview);
  4656. }
  4657. // Arm_exidx_merged_section methods.
  4658. // Constructor for Arm_exidx_merged_section.
  4659. // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
  4660. // SECTION_OFFSET_MAP points to a section offset map describing how
  4661. // parts of the input section are mapped to output. DELETED_BYTES is
  4662. // the number of bytes deleted from the EXIDX input section.
  4663. Arm_exidx_merged_section::Arm_exidx_merged_section(
  4664. const Arm_exidx_input_section& exidx_input_section,
  4665. const Arm_exidx_section_offset_map& section_offset_map,
  4666. uint32_t deleted_bytes)
  4667. : Output_relaxed_input_section(exidx_input_section.relobj(),
  4668. exidx_input_section.shndx(),
  4669. exidx_input_section.addralign()),
  4670. exidx_input_section_(exidx_input_section),
  4671. section_offset_map_(section_offset_map)
  4672. {
  4673. // If we retain or discard the whole EXIDX input section, we would
  4674. // not be here.
  4675. gold_assert(deleted_bytes != 0
  4676. && deleted_bytes != this->exidx_input_section_.size());
  4677. // Fix size here so that we do not need to implement set_final_data_size.
  4678. uint32_t size = exidx_input_section.size() - deleted_bytes;
  4679. this->set_data_size(size);
  4680. this->fix_data_size();
  4681. // Allocate buffer for section contents and build contents.
  4682. this->section_contents_ = new unsigned char[size];
  4683. }
  4684. // Build the contents of a merged EXIDX output section.
  4685. void
  4686. Arm_exidx_merged_section::build_contents(
  4687. const unsigned char* original_contents,
  4688. section_size_type original_size)
  4689. {
  4690. // Go over spans of input offsets and write only those that are not
  4691. // discarded.
  4692. section_offset_type in_start = 0;
  4693. section_offset_type out_start = 0;
  4694. section_offset_type in_max =
  4695. convert_types<section_offset_type>(original_size);
  4696. section_offset_type out_max =
  4697. convert_types<section_offset_type>(this->data_size());
  4698. for (Arm_exidx_section_offset_map::const_iterator p =
  4699. this->section_offset_map_.begin();
  4700. p != this->section_offset_map_.end();
  4701. ++p)
  4702. {
  4703. section_offset_type in_end = p->first;
  4704. gold_assert(in_end >= in_start);
  4705. section_offset_type out_end = p->second;
  4706. size_t in_chunk_size = convert_types<size_t>(in_end - in_start + 1);
  4707. if (out_end != -1)
  4708. {
  4709. size_t out_chunk_size =
  4710. convert_types<size_t>(out_end - out_start + 1);
  4711. gold_assert(out_chunk_size == in_chunk_size
  4712. && in_end < in_max && out_end < out_max);
  4713. memcpy(this->section_contents_ + out_start,
  4714. original_contents + in_start,
  4715. out_chunk_size);
  4716. out_start += out_chunk_size;
  4717. }
  4718. in_start += in_chunk_size;
  4719. }
  4720. }
  4721. // Given an input OBJECT, an input section index SHNDX within that
  4722. // object, and an OFFSET relative to the start of that input
  4723. // section, return whether or not the corresponding offset within
  4724. // the output section is known. If this function returns true, it
  4725. // sets *POUTPUT to the output offset. The value -1 indicates that
  4726. // this input offset is being discarded.
  4727. bool
  4728. Arm_exidx_merged_section::do_output_offset(
  4729. const Relobj* relobj,
  4730. unsigned int shndx,
  4731. section_offset_type offset,
  4732. section_offset_type* poutput) const
  4733. {
  4734. // We only handle offsets for the original EXIDX input section.
  4735. if (relobj != this->exidx_input_section_.relobj()
  4736. || shndx != this->exidx_input_section_.shndx())
  4737. return false;
  4738. section_offset_type section_size =
  4739. convert_types<section_offset_type>(this->exidx_input_section_.size());
  4740. if (offset < 0 || offset >= section_size)
  4741. // Input offset is out of valid range.
  4742. *poutput = -1;
  4743. else
  4744. {
  4745. // We need to look up the section offset map to determine the output
  4746. // offset. Find the reference point in map that is first offset
  4747. // bigger than or equal to this offset.
  4748. Arm_exidx_section_offset_map::const_iterator p =
  4749. this->section_offset_map_.lower_bound(offset);
  4750. // The section offset maps are build such that this should not happen if
  4751. // input offset is in the valid range.
  4752. gold_assert(p != this->section_offset_map_.end());
  4753. // We need to check if this is dropped.
  4754. section_offset_type ref = p->first;
  4755. section_offset_type mapped_ref = p->second;
  4756. if (mapped_ref != Arm_exidx_input_section::invalid_offset)
  4757. // Offset is present in output.
  4758. *poutput = mapped_ref + (offset - ref);
  4759. else
  4760. // Offset is discarded owing to EXIDX entry merging.
  4761. *poutput = -1;
  4762. }
  4763. return true;
  4764. }
  4765. // Write this to output file OF.
  4766. void
  4767. Arm_exidx_merged_section::do_write(Output_file* of)
  4768. {
  4769. off_t offset = this->offset();
  4770. const section_size_type oview_size = this->data_size();
  4771. unsigned char* const oview = of->get_output_view(offset, oview_size);
  4772. Output_section* os = this->relobj()->output_section(this->shndx());
  4773. gold_assert(os != NULL);
  4774. memcpy(oview, this->section_contents_, oview_size);
  4775. of->write_output_view(this->offset(), oview_size, oview);
  4776. }
  4777. // Arm_exidx_fixup methods.
  4778. // Append an EXIDX_CANTUNWIND in the current output section if the last entry
  4779. // is not an EXIDX_CANTUNWIND entry already. The new EXIDX_CANTUNWIND entry
  4780. // points to the end of the last seen EXIDX section.
  4781. void
  4782. Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
  4783. {
  4784. if (this->last_unwind_type_ != UT_EXIDX_CANTUNWIND
  4785. && this->last_input_section_ != NULL)
  4786. {
  4787. Relobj* relobj = this->last_input_section_->relobj();
  4788. unsigned int text_shndx = this->last_input_section_->link();
  4789. Arm_exidx_cantunwind* cantunwind =
  4790. new Arm_exidx_cantunwind(relobj, text_shndx);
  4791. this->exidx_output_section_->add_output_section_data(cantunwind);
  4792. this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
  4793. }
  4794. }
  4795. // Process an EXIDX section entry in input. Return whether this entry
  4796. // can be deleted in the output. SECOND_WORD in the second word of the
  4797. // EXIDX entry.
  4798. bool
  4799. Arm_exidx_fixup::process_exidx_entry(uint32_t second_word)
  4800. {
  4801. bool delete_entry;
  4802. if (second_word == elfcpp::EXIDX_CANTUNWIND)
  4803. {
  4804. // Merge if previous entry is also an EXIDX_CANTUNWIND.
  4805. delete_entry = this->last_unwind_type_ == UT_EXIDX_CANTUNWIND;
  4806. this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
  4807. }
  4808. else if ((second_word & 0x80000000) != 0)
  4809. {
  4810. // Inlined unwinding data. Merge if equal to previous.
  4811. delete_entry = (merge_exidx_entries_
  4812. && this->last_unwind_type_ == UT_INLINED_ENTRY
  4813. && this->last_inlined_entry_ == second_word);
  4814. this->last_unwind_type_ = UT_INLINED_ENTRY;
  4815. this->last_inlined_entry_ = second_word;
  4816. }
  4817. else
  4818. {
  4819. // Normal table entry. In theory we could merge these too,
  4820. // but duplicate entries are likely to be much less common.
  4821. delete_entry = false;
  4822. this->last_unwind_type_ = UT_NORMAL_ENTRY;
  4823. }
  4824. return delete_entry;
  4825. }
  4826. // Update the current section offset map during EXIDX section fix-up.
  4827. // If there is no map, create one. INPUT_OFFSET is the offset of a
  4828. // reference point, DELETED_BYTES is the number of deleted by in the
  4829. // section so far. If DELETE_ENTRY is true, the reference point and
  4830. // all offsets after the previous reference point are discarded.
  4831. void
  4832. Arm_exidx_fixup::update_offset_map(
  4833. section_offset_type input_offset,
  4834. section_size_type deleted_bytes,
  4835. bool delete_entry)
  4836. {
  4837. if (this->section_offset_map_ == NULL)
  4838. this->section_offset_map_ = new Arm_exidx_section_offset_map();
  4839. section_offset_type output_offset;
  4840. if (delete_entry)
  4841. output_offset = Arm_exidx_input_section::invalid_offset;
  4842. else
  4843. output_offset = input_offset - deleted_bytes;
  4844. (*this->section_offset_map_)[input_offset] = output_offset;
  4845. }
  4846. // Process EXIDX_INPUT_SECTION for EXIDX entry merging. Return the number of
  4847. // bytes deleted. SECTION_CONTENTS points to the contents of the EXIDX
  4848. // section and SECTION_SIZE is the number of bytes pointed by SECTION_CONTENTS.
  4849. // If some entries are merged, also store a pointer to a newly created
  4850. // Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP. The caller
  4851. // owns the map and is responsible for releasing it after use.
  4852. template<bool big_endian>
  4853. uint32_t
  4854. Arm_exidx_fixup::process_exidx_section(
  4855. const Arm_exidx_input_section* exidx_input_section,
  4856. const unsigned char* section_contents,
  4857. section_size_type section_size,
  4858. Arm_exidx_section_offset_map** psection_offset_map)
  4859. {
  4860. Relobj* relobj = exidx_input_section->relobj();
  4861. unsigned shndx = exidx_input_section->shndx();
  4862. if ((section_size % 8) != 0)
  4863. {
  4864. // Something is wrong with this section. Better not touch it.
  4865. gold_error(_("uneven .ARM.exidx section size in %s section %u"),
  4866. relobj->name().c_str(), shndx);
  4867. this->last_input_section_ = exidx_input_section;
  4868. this->last_unwind_type_ = UT_NONE;
  4869. return 0;
  4870. }
  4871. uint32_t deleted_bytes = 0;
  4872. bool prev_delete_entry = false;
  4873. gold_assert(this->section_offset_map_ == NULL);
  4874. for (section_size_type i = 0; i < section_size; i += 8)
  4875. {
  4876. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  4877. const Valtype* wv =
  4878. reinterpret_cast<const Valtype*>(section_contents + i + 4);
  4879. uint32_t second_word = elfcpp::Swap<32, big_endian>::readval(wv);
  4880. bool delete_entry = this->process_exidx_entry(second_word);
  4881. // Entry deletion causes changes in output offsets. We use a std::map
  4882. // to record these. And entry (x, y) means input offset x
  4883. // is mapped to output offset y. If y is invalid_offset, then x is
  4884. // dropped in the output. Because of the way std::map::lower_bound
  4885. // works, we record the last offset in a region w.r.t to keeping or
  4886. // dropping. If there is no entry (x0, y0) for an input offset x0,
  4887. // the output offset y0 of it is determined by the output offset y1 of
  4888. // the smallest input offset x1 > x0 that there is an (x1, y1) entry
  4889. // in the map. If y1 is not -1, then y0 = y1 + x0 - x1. Otherwise, y1
  4890. // y0 is also -1.
  4891. if (delete_entry != prev_delete_entry && i != 0)
  4892. this->update_offset_map(i - 1, deleted_bytes, prev_delete_entry);
  4893. // Update total deleted bytes for this entry.
  4894. if (delete_entry)
  4895. deleted_bytes += 8;
  4896. prev_delete_entry = delete_entry;
  4897. }
  4898. // If section offset map is not NULL, make an entry for the end of
  4899. // section.
  4900. if (this->section_offset_map_ != NULL)
  4901. update_offset_map(section_size - 1, deleted_bytes, prev_delete_entry);
  4902. *psection_offset_map = this->section_offset_map_;
  4903. this->section_offset_map_ = NULL;
  4904. this->last_input_section_ = exidx_input_section;
  4905. // Set the first output text section so that we can link the EXIDX output
  4906. // section to it. Ignore any EXIDX input section that is completely merged.
  4907. if (this->first_output_text_section_ == NULL
  4908. && deleted_bytes != section_size)
  4909. {
  4910. unsigned int link = exidx_input_section->link();
  4911. Output_section* os = relobj->output_section(link);
  4912. gold_assert(os != NULL);
  4913. this->first_output_text_section_ = os;
  4914. }
  4915. return deleted_bytes;
  4916. }
  4917. // Arm_output_section methods.
  4918. // Create a stub group for input sections from BEGIN to END. OWNER
  4919. // points to the input section to be the owner a new stub table.
  4920. template<bool big_endian>
  4921. void
  4922. Arm_output_section<big_endian>::create_stub_group(
  4923. Input_section_list::const_iterator begin,
  4924. Input_section_list::const_iterator end,
  4925. Input_section_list::const_iterator owner,
  4926. Target_arm<big_endian>* target,
  4927. std::vector<Output_relaxed_input_section*>* new_relaxed_sections,
  4928. const Task* task)
  4929. {
  4930. // We use a different kind of relaxed section in an EXIDX section.
  4931. // The static casting from Output_relaxed_input_section to
  4932. // Arm_input_section is invalid in an EXIDX section. We are okay
  4933. // because we should not be calling this for an EXIDX section.
  4934. gold_assert(this->type() != elfcpp::SHT_ARM_EXIDX);
  4935. // Currently we convert ordinary input sections into relaxed sections only
  4936. // at this point but we may want to support creating relaxed input section
  4937. // very early. So we check here to see if owner is already a relaxed
  4938. // section.
  4939. Arm_input_section<big_endian>* arm_input_section;
  4940. if (owner->is_relaxed_input_section())
  4941. {
  4942. arm_input_section =
  4943. Arm_input_section<big_endian>::as_arm_input_section(
  4944. owner->relaxed_input_section());
  4945. }
  4946. else
  4947. {
  4948. gold_assert(owner->is_input_section());
  4949. // Create a new relaxed input section. We need to lock the original
  4950. // file.
  4951. Task_lock_obj<Object> tl(task, owner->relobj());
  4952. arm_input_section =
  4953. target->new_arm_input_section(owner->relobj(), owner->shndx());
  4954. new_relaxed_sections->push_back(arm_input_section);
  4955. }
  4956. // Create a stub table.
  4957. Stub_table<big_endian>* stub_table =
  4958. target->new_stub_table(arm_input_section);
  4959. arm_input_section->set_stub_table(stub_table);
  4960. Input_section_list::const_iterator p = begin;
  4961. Input_section_list::const_iterator prev_p;
  4962. // Look for input sections or relaxed input sections in [begin ... end].
  4963. do
  4964. {
  4965. if (p->is_input_section() || p->is_relaxed_input_section())
  4966. {
  4967. // The stub table information for input sections live
  4968. // in their objects.
  4969. Arm_relobj<big_endian>* arm_relobj =
  4970. Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
  4971. arm_relobj->set_stub_table(p->shndx(), stub_table);
  4972. }
  4973. prev_p = p++;
  4974. }
  4975. while (prev_p != end);
  4976. }
  4977. // Group input sections for stub generation. GROUP_SIZE is roughly the limit
  4978. // of stub groups. We grow a stub group by adding input section until the
  4979. // size is just below GROUP_SIZE. The last input section will be converted
  4980. // into a stub table. If STUB_ALWAYS_AFTER_BRANCH is false, we also add
  4981. // input section after the stub table, effectively double the group size.
  4982. //
  4983. // This is similar to the group_sections() function in elf32-arm.c but is
  4984. // implemented differently.
  4985. template<bool big_endian>
  4986. void
  4987. Arm_output_section<big_endian>::group_sections(
  4988. section_size_type group_size,
  4989. bool stubs_always_after_branch,
  4990. Target_arm<big_endian>* target,
  4991. const Task* task)
  4992. {
  4993. // States for grouping.
  4994. typedef enum
  4995. {
  4996. // No group is being built.
  4997. NO_GROUP,
  4998. // A group is being built but the stub table is not found yet.
  4999. // We keep group a stub group until the size is just under GROUP_SIZE.
  5000. // The last input section in the group will be used as the stub table.
  5001. FINDING_STUB_SECTION,
  5002. // A group is being built and we have already found a stub table.
  5003. // We enter this state to grow a stub group by adding input section
  5004. // after the stub table. This effectively doubles the group size.
  5005. HAS_STUB_SECTION
  5006. } State;
  5007. // Any newly created relaxed sections are stored here.
  5008. std::vector<Output_relaxed_input_section*> new_relaxed_sections;
  5009. State state = NO_GROUP;
  5010. section_size_type off = 0;
  5011. section_size_type group_begin_offset = 0;
  5012. section_size_type group_end_offset = 0;
  5013. section_size_type stub_table_end_offset = 0;
  5014. Input_section_list::const_iterator group_begin =
  5015. this->input_sections().end();
  5016. Input_section_list::const_iterator stub_table =
  5017. this->input_sections().end();
  5018. Input_section_list::const_iterator group_end = this->input_sections().end();
  5019. for (Input_section_list::const_iterator p = this->input_sections().begin();
  5020. p != this->input_sections().end();
  5021. ++p)
  5022. {
  5023. section_size_type section_begin_offset =
  5024. align_address(off, p->addralign());
  5025. section_size_type section_end_offset =
  5026. section_begin_offset + p->data_size();
  5027. // Check to see if we should group the previously seen sections.
  5028. switch (state)
  5029. {
  5030. case NO_GROUP:
  5031. break;
  5032. case FINDING_STUB_SECTION:
  5033. // Adding this section makes the group larger than GROUP_SIZE.
  5034. if (section_end_offset - group_begin_offset >= group_size)
  5035. {
  5036. if (stubs_always_after_branch)
  5037. {
  5038. gold_assert(group_end != this->input_sections().end());
  5039. this->create_stub_group(group_begin, group_end, group_end,
  5040. target, &new_relaxed_sections,
  5041. task);
  5042. state = NO_GROUP;
  5043. }
  5044. else
  5045. {
  5046. // But wait, there's more! Input sections up to
  5047. // stub_group_size bytes after the stub table can be
  5048. // handled by it too.
  5049. state = HAS_STUB_SECTION;
  5050. stub_table = group_end;
  5051. stub_table_end_offset = group_end_offset;
  5052. }
  5053. }
  5054. break;
  5055. case HAS_STUB_SECTION:
  5056. // Adding this section makes the post stub-section group larger
  5057. // than GROUP_SIZE.
  5058. if (section_end_offset - stub_table_end_offset >= group_size)
  5059. {
  5060. gold_assert(group_end != this->input_sections().end());
  5061. this->create_stub_group(group_begin, group_end, stub_table,
  5062. target, &new_relaxed_sections, task);
  5063. state = NO_GROUP;
  5064. }
  5065. break;
  5066. default:
  5067. gold_unreachable();
  5068. }
  5069. // If we see an input section and currently there is no group, start
  5070. // a new one. Skip any empty sections. We look at the data size
  5071. // instead of calling p->relobj()->section_size() to avoid locking.
  5072. if ((p->is_input_section() || p->is_relaxed_input_section())
  5073. && (p->data_size() != 0))
  5074. {
  5075. if (state == NO_GROUP)
  5076. {
  5077. state = FINDING_STUB_SECTION;
  5078. group_begin = p;
  5079. group_begin_offset = section_begin_offset;
  5080. }
  5081. // Keep track of the last input section seen.
  5082. group_end = p;
  5083. group_end_offset = section_end_offset;
  5084. }
  5085. off = section_end_offset;
  5086. }
  5087. // Create a stub group for any ungrouped sections.
  5088. if (state == FINDING_STUB_SECTION || state == HAS_STUB_SECTION)
  5089. {
  5090. gold_assert(group_end != this->input_sections().end());
  5091. this->create_stub_group(group_begin, group_end,
  5092. (state == FINDING_STUB_SECTION
  5093. ? group_end
  5094. : stub_table),
  5095. target, &new_relaxed_sections, task);
  5096. }
  5097. // Convert input section into relaxed input section in a batch.
  5098. if (!new_relaxed_sections.empty())
  5099. this->convert_input_sections_to_relaxed_sections(new_relaxed_sections);
  5100. // Update the section offsets
  5101. for (size_t i = 0; i < new_relaxed_sections.size(); ++i)
  5102. {
  5103. Arm_relobj<big_endian>* arm_relobj =
  5104. Arm_relobj<big_endian>::as_arm_relobj(
  5105. new_relaxed_sections[i]->relobj());
  5106. unsigned int shndx = new_relaxed_sections[i]->shndx();
  5107. // Tell Arm_relobj that this input section is converted.
  5108. arm_relobj->convert_input_section_to_relaxed_section(shndx);
  5109. }
  5110. }
  5111. // Append non empty text sections in this to LIST in ascending
  5112. // order of their position in this.
  5113. template<bool big_endian>
  5114. void
  5115. Arm_output_section<big_endian>::append_text_sections_to_list(
  5116. Text_section_list* list)
  5117. {
  5118. gold_assert((this->flags() & elfcpp::SHF_ALLOC) != 0);
  5119. for (Input_section_list::const_iterator p = this->input_sections().begin();
  5120. p != this->input_sections().end();
  5121. ++p)
  5122. {
  5123. // We only care about plain or relaxed input sections. We also
  5124. // ignore any merged sections.
  5125. if (p->is_input_section() || p->is_relaxed_input_section())
  5126. list->push_back(Text_section_list::value_type(p->relobj(),
  5127. p->shndx()));
  5128. }
  5129. }
  5130. template<bool big_endian>
  5131. void
  5132. Arm_output_section<big_endian>::fix_exidx_coverage(
  5133. Layout* layout,
  5134. const Text_section_list& sorted_text_sections,
  5135. Symbol_table* symtab,
  5136. bool merge_exidx_entries,
  5137. const Task* task)
  5138. {
  5139. // We should only do this for the EXIDX output section.
  5140. gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
  5141. // We don't want the relaxation loop to undo these changes, so we discard
  5142. // the current saved states and take another one after the fix-up.
  5143. this->discard_states();
  5144. // Remove all input sections.
  5145. uint64_t address = this->address();
  5146. typedef std::list<Output_section::Input_section> Input_section_list;
  5147. Input_section_list input_sections;
  5148. this->reset_address_and_file_offset();
  5149. this->get_input_sections(address, std::string(""), &input_sections);
  5150. if (!this->input_sections().empty())
  5151. gold_error(_("Found non-EXIDX input sections in EXIDX output section"));
  5152. // Go through all the known input sections and record them.
  5153. typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
  5154. typedef Unordered_map<Section_id, const Output_section::Input_section*,
  5155. Section_id_hash> Text_to_exidx_map;
  5156. Text_to_exidx_map text_to_exidx_map;
  5157. for (Input_section_list::const_iterator p = input_sections.begin();
  5158. p != input_sections.end();
  5159. ++p)
  5160. {
  5161. // This should never happen. At this point, we should only see
  5162. // plain EXIDX input sections.
  5163. gold_assert(!p->is_relaxed_input_section());
  5164. text_to_exidx_map[Section_id(p->relobj(), p->shndx())] = &(*p);
  5165. }
  5166. Arm_exidx_fixup exidx_fixup(this, merge_exidx_entries);
  5167. // Go over the sorted text sections.
  5168. typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
  5169. Section_id_set processed_input_sections;
  5170. for (Text_section_list::const_iterator p = sorted_text_sections.begin();
  5171. p != sorted_text_sections.end();
  5172. ++p)
  5173. {
  5174. Relobj* relobj = p->first;
  5175. unsigned int shndx = p->second;
  5176. Arm_relobj<big_endian>* arm_relobj =
  5177. Arm_relobj<big_endian>::as_arm_relobj(relobj);
  5178. const Arm_exidx_input_section* exidx_input_section =
  5179. arm_relobj->exidx_input_section_by_link(shndx);
  5180. // If this text section has no EXIDX section or if the EXIDX section
  5181. // has errors, force an EXIDX_CANTUNWIND entry pointing to the end
  5182. // of the last seen EXIDX section.
  5183. if (exidx_input_section == NULL || exidx_input_section->has_errors())
  5184. {
  5185. exidx_fixup.add_exidx_cantunwind_as_needed();
  5186. continue;
  5187. }
  5188. Relobj* exidx_relobj = exidx_input_section->relobj();
  5189. unsigned int exidx_shndx = exidx_input_section->shndx();
  5190. Section_id sid(exidx_relobj, exidx_shndx);
  5191. Text_to_exidx_map::const_iterator iter = text_to_exidx_map.find(sid);
  5192. if (iter == text_to_exidx_map.end())
  5193. {
  5194. // This is odd. We have not seen this EXIDX input section before.
  5195. // We cannot do fix-up. If we saw a SECTIONS clause in a script,
  5196. // issue a warning instead. We assume the user knows what he
  5197. // or she is doing. Otherwise, this is an error.
  5198. if (layout->script_options()->saw_sections_clause())
  5199. gold_warning(_("unwinding may not work because EXIDX input section"
  5200. " %u of %s is not in EXIDX output section"),
  5201. exidx_shndx, exidx_relobj->name().c_str());
  5202. else
  5203. gold_error(_("unwinding may not work because EXIDX input section"
  5204. " %u of %s is not in EXIDX output section"),
  5205. exidx_shndx, exidx_relobj->name().c_str());
  5206. exidx_fixup.add_exidx_cantunwind_as_needed();
  5207. continue;
  5208. }
  5209. // We need to access the contents of the EXIDX section, lock the
  5210. // object here.
  5211. Task_lock_obj<Object> tl(task, exidx_relobj);
  5212. section_size_type exidx_size;
  5213. const unsigned char* exidx_contents =
  5214. exidx_relobj->section_contents(exidx_shndx, &exidx_size, false);
  5215. // Fix up coverage and append input section to output data list.
  5216. Arm_exidx_section_offset_map* section_offset_map = NULL;
  5217. uint32_t deleted_bytes =
  5218. exidx_fixup.process_exidx_section<big_endian>(exidx_input_section,
  5219. exidx_contents,
  5220. exidx_size,
  5221. &section_offset_map);
  5222. if (deleted_bytes == exidx_input_section->size())
  5223. {
  5224. // The whole EXIDX section got merged. Remove it from output.
  5225. gold_assert(section_offset_map == NULL);
  5226. exidx_relobj->set_output_section(exidx_shndx, NULL);
  5227. // All local symbols defined in this input section will be dropped.
  5228. // We need to adjust output local symbol count.
  5229. arm_relobj->set_output_local_symbol_count_needs_update();
  5230. }
  5231. else if (deleted_bytes > 0)
  5232. {
  5233. // Some entries are merged. We need to convert this EXIDX input
  5234. // section into a relaxed section.
  5235. gold_assert(section_offset_map != NULL);
  5236. Arm_exidx_merged_section* merged_section =
  5237. new Arm_exidx_merged_section(*exidx_input_section,
  5238. *section_offset_map, deleted_bytes);
  5239. merged_section->build_contents(exidx_contents, exidx_size);
  5240. const std::string secname = exidx_relobj->section_name(exidx_shndx);
  5241. this->add_relaxed_input_section(layout, merged_section, secname);
  5242. arm_relobj->convert_input_section_to_relaxed_section(exidx_shndx);
  5243. // All local symbols defined in discarded portions of this input
  5244. // section will be dropped. We need to adjust output local symbol
  5245. // count.
  5246. arm_relobj->set_output_local_symbol_count_needs_update();
  5247. }
  5248. else
  5249. {
  5250. // Just add back the EXIDX input section.
  5251. gold_assert(section_offset_map == NULL);
  5252. const Output_section::Input_section* pis = iter->second;
  5253. gold_assert(pis->is_input_section());
  5254. this->add_script_input_section(*pis);
  5255. }
  5256. processed_input_sections.insert(Section_id(exidx_relobj, exidx_shndx));
  5257. }
  5258. // Insert an EXIDX_CANTUNWIND entry at the end of output if necessary.
  5259. exidx_fixup.add_exidx_cantunwind_as_needed();
  5260. // Remove any known EXIDX input sections that are not processed.
  5261. for (Input_section_list::const_iterator p = input_sections.begin();
  5262. p != input_sections.end();
  5263. ++p)
  5264. {
  5265. if (processed_input_sections.find(Section_id(p->relobj(), p->shndx()))
  5266. == processed_input_sections.end())
  5267. {
  5268. // We discard a known EXIDX section because its linked
  5269. // text section has been folded by ICF. We also discard an
  5270. // EXIDX section with error, the output does not matter in this
  5271. // case. We do this to avoid triggering asserts.
  5272. Arm_relobj<big_endian>* arm_relobj =
  5273. Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
  5274. const Arm_exidx_input_section* exidx_input_section =
  5275. arm_relobj->exidx_input_section_by_shndx(p->shndx());
  5276. gold_assert(exidx_input_section != NULL);
  5277. if (!exidx_input_section->has_errors())
  5278. {
  5279. unsigned int text_shndx = exidx_input_section->link();
  5280. gold_assert(symtab->is_section_folded(p->relobj(), text_shndx));
  5281. }
  5282. // Remove this from link. We also need to recount the
  5283. // local symbols.
  5284. p->relobj()->set_output_section(p->shndx(), NULL);
  5285. arm_relobj->set_output_local_symbol_count_needs_update();
  5286. }
  5287. }
  5288. // Link exidx output section to the first seen output section and
  5289. // set correct entry size.
  5290. this->set_link_section(exidx_fixup.first_output_text_section());
  5291. this->set_entsize(8);
  5292. // Make changes permanent.
  5293. this->save_states();
  5294. this->set_section_offsets_need_adjustment();
  5295. }
  5296. // Link EXIDX output sections to text output sections.
  5297. template<bool big_endian>
  5298. void
  5299. Arm_output_section<big_endian>::set_exidx_section_link()
  5300. {
  5301. gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
  5302. if (!this->input_sections().empty())
  5303. {
  5304. Input_section_list::const_iterator p = this->input_sections().begin();
  5305. Arm_relobj<big_endian>* arm_relobj =
  5306. Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
  5307. unsigned exidx_shndx = p->shndx();
  5308. const Arm_exidx_input_section* exidx_input_section =
  5309. arm_relobj->exidx_input_section_by_shndx(exidx_shndx);
  5310. gold_assert(exidx_input_section != NULL);
  5311. unsigned int text_shndx = exidx_input_section->link();
  5312. Output_section* os = arm_relobj->output_section(text_shndx);
  5313. this->set_link_section(os);
  5314. }
  5315. }
  5316. // Arm_relobj methods.
  5317. // Determine if an input section is scannable for stub processing. SHDR is
  5318. // the header of the section and SHNDX is the section index. OS is the output
  5319. // section for the input section and SYMTAB is the global symbol table used to
  5320. // look up ICF information.
  5321. template<bool big_endian>
  5322. bool
  5323. Arm_relobj<big_endian>::section_is_scannable(
  5324. const elfcpp::Shdr<32, big_endian>& shdr,
  5325. unsigned int shndx,
  5326. const Output_section* os,
  5327. const Symbol_table* symtab)
  5328. {
  5329. // Skip any empty sections, unallocated sections or sections whose
  5330. // type are not SHT_PROGBITS.
  5331. if (shdr.get_sh_size() == 0
  5332. || (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
  5333. || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
  5334. return false;
  5335. // Skip any discarded or ICF'ed sections.
  5336. if (os == NULL || symtab->is_section_folded(this, shndx))
  5337. return false;
  5338. // If this requires special offset handling, check to see if it is
  5339. // a relaxed section. If this is not, then it is a merged section that
  5340. // we cannot handle.
  5341. if (this->is_output_section_offset_invalid(shndx))
  5342. {
  5343. const Output_relaxed_input_section* poris =
  5344. os->find_relaxed_input_section(this, shndx);
  5345. if (poris == NULL)
  5346. return false;
  5347. }
  5348. return true;
  5349. }
  5350. // Determine if we want to scan the SHNDX-th section for relocation stubs.
  5351. // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
  5352. template<bool big_endian>
  5353. bool
  5354. Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
  5355. const elfcpp::Shdr<32, big_endian>& shdr,
  5356. const Relobj::Output_sections& out_sections,
  5357. const Symbol_table* symtab,
  5358. const unsigned char* pshdrs)
  5359. {
  5360. unsigned int sh_type = shdr.get_sh_type();
  5361. if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
  5362. return false;
  5363. // Ignore empty section.
  5364. off_t sh_size = shdr.get_sh_size();
  5365. if (sh_size == 0)
  5366. return false;
  5367. // Ignore reloc section with unexpected symbol table. The
  5368. // error will be reported in the final link.
  5369. if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
  5370. return false;
  5371. unsigned int reloc_size;
  5372. if (sh_type == elfcpp::SHT_REL)
  5373. reloc_size = elfcpp::Elf_sizes<32>::rel_size;
  5374. else
  5375. reloc_size = elfcpp::Elf_sizes<32>::rela_size;
  5376. // Ignore reloc section with unexpected entsize or uneven size.
  5377. // The error will be reported in the final link.
  5378. if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
  5379. return false;
  5380. // Ignore reloc section with bad info. This error will be
  5381. // reported in the final link.
  5382. unsigned int index = this->adjust_shndx(shdr.get_sh_info());
  5383. if (index >= this->shnum())
  5384. return false;
  5385. const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
  5386. const elfcpp::Shdr<32, big_endian> text_shdr(pshdrs + index * shdr_size);
  5387. return this->section_is_scannable(text_shdr, index,
  5388. out_sections[index], symtab);
  5389. }
  5390. // Return the output address of either a plain input section or a relaxed
  5391. // input section. SHNDX is the section index. We define and use this
  5392. // instead of calling Output_section::output_address because that is slow
  5393. // for large output.
  5394. template<bool big_endian>
  5395. Arm_address
  5396. Arm_relobj<big_endian>::simple_input_section_output_address(
  5397. unsigned int shndx,
  5398. Output_section* os)
  5399. {
  5400. if (this->is_output_section_offset_invalid(shndx))
  5401. {
  5402. const Output_relaxed_input_section* poris =
  5403. os->find_relaxed_input_section(this, shndx);
  5404. // We do not handle merged sections here.
  5405. gold_assert(poris != NULL);
  5406. return poris->address();
  5407. }
  5408. else
  5409. return os->address() + this->get_output_section_offset(shndx);
  5410. }
  5411. // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
  5412. // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
  5413. template<bool big_endian>
  5414. bool
  5415. Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
  5416. const elfcpp::Shdr<32, big_endian>& shdr,
  5417. unsigned int shndx,
  5418. Output_section* os,
  5419. const Symbol_table* symtab)
  5420. {
  5421. if (!this->section_is_scannable(shdr, shndx, os, symtab))
  5422. return false;
  5423. // If the section does not cross any 4K-boundaries, it does not need to
  5424. // be scanned.
  5425. Arm_address address = this->simple_input_section_output_address(shndx, os);
  5426. if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
  5427. return false;
  5428. return true;
  5429. }
  5430. // Scan a section for Cortex-A8 workaround.
  5431. template<bool big_endian>
  5432. void
  5433. Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
  5434. const elfcpp::Shdr<32, big_endian>& shdr,
  5435. unsigned int shndx,
  5436. Output_section* os,
  5437. Target_arm<big_endian>* arm_target)
  5438. {
  5439. // Look for the first mapping symbol in this section. It should be
  5440. // at (shndx, 0).
  5441. Mapping_symbol_position section_start(shndx, 0);
  5442. typename Mapping_symbols_info::const_iterator p =
  5443. this->mapping_symbols_info_.lower_bound(section_start);
  5444. // There are no mapping symbols for this section. Treat it as a data-only
  5445. // section.
  5446. if (p == this->mapping_symbols_info_.end() || p->first.first != shndx)
  5447. return;
  5448. Arm_address output_address =
  5449. this->simple_input_section_output_address(shndx, os);
  5450. // Get the section contents.
  5451. section_size_type input_view_size = 0;
  5452. const unsigned char* input_view =
  5453. this->section_contents(shndx, &input_view_size, false);
  5454. // We need to go through the mapping symbols to determine what to
  5455. // scan. There are two reasons. First, we should look at THUMB code and
  5456. // THUMB code only. Second, we only want to look at the 4K-page boundary
  5457. // to speed up the scanning.
  5458. while (p != this->mapping_symbols_info_.end()
  5459. && p->first.first == shndx)
  5460. {
  5461. typename Mapping_symbols_info::const_iterator next =
  5462. this->mapping_symbols_info_.upper_bound(p->first);
  5463. // Only scan part of a section with THUMB code.
  5464. if (p->second == 't')
  5465. {
  5466. // Determine the end of this range.
  5467. section_size_type span_start =
  5468. convert_to_section_size_type(p->first.second);
  5469. section_size_type span_end;
  5470. if (next != this->mapping_symbols_info_.end()
  5471. && next->first.first == shndx)
  5472. span_end = convert_to_section_size_type(next->first.second);
  5473. else
  5474. span_end = convert_to_section_size_type(shdr.get_sh_size());
  5475. if (((span_start + output_address) & ~0xfffUL)
  5476. != ((span_end + output_address - 1) & ~0xfffUL))
  5477. {
  5478. arm_target->scan_span_for_cortex_a8_erratum(this, shndx,
  5479. span_start, span_end,
  5480. input_view,
  5481. output_address);
  5482. }
  5483. }
  5484. p = next;
  5485. }
  5486. }
  5487. // Scan relocations for stub generation.
  5488. template<bool big_endian>
  5489. void
  5490. Arm_relobj<big_endian>::scan_sections_for_stubs(
  5491. Target_arm<big_endian>* arm_target,
  5492. const Symbol_table* symtab,
  5493. const Layout* layout)
  5494. {
  5495. unsigned int shnum = this->shnum();
  5496. const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
  5497. // Read the section headers.
  5498. const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
  5499. shnum * shdr_size,
  5500. true, true);
  5501. // To speed up processing, we set up hash tables for fast lookup of
  5502. // input offsets to output addresses.
  5503. this->initialize_input_to_output_maps();
  5504. const Relobj::Output_sections& out_sections(this->output_sections());
  5505. Relocate_info<32, big_endian> relinfo;
  5506. relinfo.symtab = symtab;
  5507. relinfo.layout = layout;
  5508. relinfo.object = this;
  5509. // Do relocation stubs scanning.
  5510. const unsigned char* p = pshdrs + shdr_size;
  5511. for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
  5512. {
  5513. const elfcpp::Shdr<32, big_endian> shdr(p);
  5514. if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
  5515. pshdrs))
  5516. {
  5517. unsigned int index = this->adjust_shndx(shdr.get_sh_info());
  5518. Arm_address output_offset = this->get_output_section_offset(index);
  5519. Arm_address output_address;
  5520. if (output_offset != invalid_address)
  5521. output_address = out_sections[index]->address() + output_offset;
  5522. else
  5523. {
  5524. // Currently this only happens for a relaxed section.
  5525. const Output_relaxed_input_section* poris =
  5526. out_sections[index]->find_relaxed_input_section(this, index);
  5527. gold_assert(poris != NULL);
  5528. output_address = poris->address();
  5529. }
  5530. // Get the relocations.
  5531. const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
  5532. shdr.get_sh_size(),
  5533. true, false);
  5534. // Get the section contents. This does work for the case in which
  5535. // we modify the contents of an input section. We need to pass the
  5536. // output view under such circumstances.
  5537. section_size_type input_view_size = 0;
  5538. const unsigned char* input_view =
  5539. this->section_contents(index, &input_view_size, false);
  5540. relinfo.reloc_shndx = i;
  5541. relinfo.data_shndx = index;
  5542. unsigned int sh_type = shdr.get_sh_type();
  5543. unsigned int reloc_size;
  5544. if (sh_type == elfcpp::SHT_REL)
  5545. reloc_size = elfcpp::Elf_sizes<32>::rel_size;
  5546. else
  5547. reloc_size = elfcpp::Elf_sizes<32>::rela_size;
  5548. Output_section* os = out_sections[index];
  5549. arm_target->scan_section_for_stubs(&relinfo, sh_type, prelocs,
  5550. shdr.get_sh_size() / reloc_size,
  5551. os,
  5552. output_offset == invalid_address,
  5553. input_view, output_address,
  5554. input_view_size);
  5555. }
  5556. }
  5557. // Do Cortex-A8 erratum stubs scanning. This has to be done for a section
  5558. // after its relocation section, if there is one, is processed for
  5559. // relocation stubs. Merging this loop with the one above would have been
  5560. // complicated since we would have had to make sure that relocation stub
  5561. // scanning is done first.
  5562. if (arm_target->fix_cortex_a8())
  5563. {
  5564. const unsigned char* p = pshdrs + shdr_size;
  5565. for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
  5566. {
  5567. const elfcpp::Shdr<32, big_endian> shdr(p);
  5568. if (this->section_needs_cortex_a8_stub_scanning(shdr, i,
  5569. out_sections[i],
  5570. symtab))
  5571. this->scan_section_for_cortex_a8_erratum(shdr, i, out_sections[i],
  5572. arm_target);
  5573. }
  5574. }
  5575. // After we've done the relocations, we release the hash tables,
  5576. // since we no longer need them.
  5577. this->free_input_to_output_maps();
  5578. }
  5579. // Count the local symbols. The ARM backend needs to know if a symbol
  5580. // is a THUMB function or not. For global symbols, it is easy because
  5581. // the Symbol object keeps the ELF symbol type. For local symbol it is
  5582. // harder because we cannot access this information. So we override the
  5583. // do_count_local_symbol in parent and scan local symbols to mark
  5584. // THUMB functions. This is not the most efficient way but I do not want to
  5585. // slow down other ports by calling a per symbol target hook inside
  5586. // Sized_relobj_file<size, big_endian>::do_count_local_symbols.
  5587. template<bool big_endian>
  5588. void
  5589. Arm_relobj<big_endian>::do_count_local_symbols(
  5590. Stringpool_template<char>* pool,
  5591. Stringpool_template<char>* dynpool)
  5592. {
  5593. // We need to fix-up the values of any local symbols whose type are
  5594. // STT_ARM_TFUNC.
  5595. // Ask parent to count the local symbols.
  5596. Sized_relobj_file<32, big_endian>::do_count_local_symbols(pool, dynpool);
  5597. const unsigned int loccount = this->local_symbol_count();
  5598. if (loccount == 0)
  5599. return;
  5600. // Initialize the thumb function bit-vector.
  5601. std::vector<bool> empty_vector(loccount, false);
  5602. this->local_symbol_is_thumb_function_.swap(empty_vector);
  5603. // Read the symbol table section header.
  5604. const unsigned int symtab_shndx = this->symtab_shndx();
  5605. elfcpp::Shdr<32, big_endian>
  5606. symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
  5607. gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
  5608. // Read the local symbols.
  5609. const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
  5610. gold_assert(loccount == symtabshdr.get_sh_info());
  5611. off_t locsize = loccount * sym_size;
  5612. const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
  5613. locsize, true, true);
  5614. // For mapping symbol processing, we need to read the symbol names.
  5615. unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
  5616. if (strtab_shndx >= this->shnum())
  5617. {
  5618. this->error(_("invalid symbol table name index: %u"), strtab_shndx);
  5619. return;
  5620. }
  5621. elfcpp::Shdr<32, big_endian>
  5622. strtabshdr(this, this->elf_file()->section_header(strtab_shndx));
  5623. if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
  5624. {
  5625. this->error(_("symbol table name section has wrong type: %u"),
  5626. static_cast<unsigned int>(strtabshdr.get_sh_type()));
  5627. return;
  5628. }
  5629. const char* pnames =
  5630. reinterpret_cast<const char*>(this->get_view(strtabshdr.get_sh_offset(),
  5631. strtabshdr.get_sh_size(),
  5632. false, false));
  5633. // Loop over the local symbols and mark any local symbols pointing
  5634. // to THUMB functions.
  5635. // Skip the first dummy symbol.
  5636. psyms += sym_size;
  5637. typename Sized_relobj_file<32, big_endian>::Local_values* plocal_values =
  5638. this->local_values();
  5639. for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
  5640. {
  5641. elfcpp::Sym<32, big_endian> sym(psyms);
  5642. elfcpp::STT st_type = sym.get_st_type();
  5643. Symbol_value<32>& lv((*plocal_values)[i]);
  5644. Arm_address input_value = lv.input_value();
  5645. // Check to see if this is a mapping symbol.
  5646. const char* sym_name = pnames + sym.get_st_name();
  5647. if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
  5648. {
  5649. bool is_ordinary;
  5650. unsigned int input_shndx =
  5651. this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
  5652. gold_assert(is_ordinary);
  5653. // Strip of LSB in case this is a THUMB symbol.
  5654. Mapping_symbol_position msp(input_shndx, input_value & ~1U);
  5655. this->mapping_symbols_info_[msp] = sym_name[1];
  5656. }
  5657. if (st_type == elfcpp::STT_ARM_TFUNC
  5658. || (st_type == elfcpp::STT_FUNC && ((input_value & 1) != 0)))
  5659. {
  5660. // This is a THUMB function. Mark this and canonicalize the
  5661. // symbol value by setting LSB.
  5662. this->local_symbol_is_thumb_function_[i] = true;
  5663. if ((input_value & 1) == 0)
  5664. lv.set_input_value(input_value | 1);
  5665. }
  5666. }
  5667. }
  5668. // Relocate sections.
  5669. template<bool big_endian>
  5670. void
  5671. Arm_relobj<big_endian>::do_relocate_sections(
  5672. const Symbol_table* symtab,
  5673. const Layout* layout,
  5674. const unsigned char* pshdrs,
  5675. Output_file* of,
  5676. typename Sized_relobj_file<32, big_endian>::Views* pviews)
  5677. {
  5678. // Call parent to relocate sections.
  5679. Sized_relobj_file<32, big_endian>::do_relocate_sections(symtab, layout,
  5680. pshdrs, of, pviews);
  5681. // We do not generate stubs if doing a relocatable link.
  5682. if (parameters->options().relocatable())
  5683. return;
  5684. // Relocate stub tables.
  5685. unsigned int shnum = this->shnum();
  5686. Target_arm<big_endian>* arm_target =
  5687. Target_arm<big_endian>::default_target();
  5688. Relocate_info<32, big_endian> relinfo;
  5689. relinfo.symtab = symtab;
  5690. relinfo.layout = layout;
  5691. relinfo.object = this;
  5692. for (unsigned int i = 1; i < shnum; ++i)
  5693. {
  5694. Arm_input_section<big_endian>* arm_input_section =
  5695. arm_target->find_arm_input_section(this, i);
  5696. if (arm_input_section != NULL
  5697. && arm_input_section->is_stub_table_owner()
  5698. && !arm_input_section->stub_table()->empty())
  5699. {
  5700. // We cannot discard a section if it owns a stub table.
  5701. Output_section* os = this->output_section(i);
  5702. gold_assert(os != NULL);
  5703. relinfo.reloc_shndx = elfcpp::SHN_UNDEF;
  5704. relinfo.reloc_shdr = NULL;
  5705. relinfo.data_shndx = i;
  5706. relinfo.data_shdr = pshdrs + i * elfcpp::Elf_sizes<32>::shdr_size;
  5707. gold_assert((*pviews)[i].view != NULL);
  5708. // We are passed the output section view. Adjust it to cover the
  5709. // stub table only.
  5710. Stub_table<big_endian>* stub_table = arm_input_section->stub_table();
  5711. gold_assert((stub_table->address() >= (*pviews)[i].address)
  5712. && ((stub_table->address() + stub_table->data_size())
  5713. <= (*pviews)[i].address + (*pviews)[i].view_size));
  5714. off_t offset = stub_table->address() - (*pviews)[i].address;
  5715. unsigned char* view = (*pviews)[i].view + offset;
  5716. Arm_address address = stub_table->address();
  5717. section_size_type view_size = stub_table->data_size();
  5718. stub_table->relocate_stubs(&relinfo, arm_target, os, view, address,
  5719. view_size);
  5720. }
  5721. // Apply Cortex A8 workaround if applicable.
  5722. if (this->section_has_cortex_a8_workaround(i))
  5723. {
  5724. unsigned char* view = (*pviews)[i].view;
  5725. Arm_address view_address = (*pviews)[i].address;
  5726. section_size_type view_size = (*pviews)[i].view_size;
  5727. Stub_table<big_endian>* stub_table = this->stub_tables_[i];
  5728. // Adjust view to cover section.
  5729. Output_section* os = this->output_section(i);
  5730. gold_assert(os != NULL);
  5731. Arm_address section_address =
  5732. this->simple_input_section_output_address(i, os);
  5733. uint64_t section_size = this->section_size(i);
  5734. gold_assert(section_address >= view_address
  5735. && ((section_address + section_size)
  5736. <= (view_address + view_size)));
  5737. unsigned char* section_view = view + (section_address - view_address);
  5738. // Apply the Cortex-A8 workaround to the output address range
  5739. // corresponding to this input section.
  5740. stub_table->apply_cortex_a8_workaround_to_address_range(
  5741. arm_target,
  5742. section_view,
  5743. section_address,
  5744. section_size);
  5745. }
  5746. }
  5747. }
  5748. // Find the linked text section of an EXIDX section by looking at the first
  5749. // relocation. 4.4.1 of the EHABI specifications says that an EXIDX section
  5750. // must be linked to its associated code section via the sh_link field of
  5751. // its section header. However, some tools are broken and the link is not
  5752. // always set. LD just drops such an EXIDX section silently, causing the
  5753. // associated code not unwindabled. Here we try a little bit harder to
  5754. // discover the linked code section.
  5755. //
  5756. // PSHDR points to the section header of a relocation section of an EXIDX
  5757. // section. If we can find a linked text section, return true and
  5758. // store the text section index in the location PSHNDX. Otherwise
  5759. // return false.
  5760. template<bool big_endian>
  5761. bool
  5762. Arm_relobj<big_endian>::find_linked_text_section(
  5763. const unsigned char* pshdr,
  5764. const unsigned char* psyms,
  5765. unsigned int* pshndx)
  5766. {
  5767. elfcpp::Shdr<32, big_endian> shdr(pshdr);
  5768. // If there is no relocation, we cannot find the linked text section.
  5769. size_t reloc_size;
  5770. if (shdr.get_sh_type() == elfcpp::SHT_REL)
  5771. reloc_size = elfcpp::Elf_sizes<32>::rel_size;
  5772. else
  5773. reloc_size = elfcpp::Elf_sizes<32>::rela_size;
  5774. size_t reloc_count = shdr.get_sh_size() / reloc_size;
  5775. // Get the relocations.
  5776. const unsigned char* prelocs =
  5777. this->get_view(shdr.get_sh_offset(), shdr.get_sh_size(), true, false);
  5778. // Find the REL31 relocation for the first word of the first EXIDX entry.
  5779. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  5780. {
  5781. Arm_address r_offset;
  5782. typename elfcpp::Elf_types<32>::Elf_WXword r_info;
  5783. if (shdr.get_sh_type() == elfcpp::SHT_REL)
  5784. {
  5785. typename elfcpp::Rel<32, big_endian> reloc(prelocs);
  5786. r_info = reloc.get_r_info();
  5787. r_offset = reloc.get_r_offset();
  5788. }
  5789. else
  5790. {
  5791. typename elfcpp::Rela<32, big_endian> reloc(prelocs);
  5792. r_info = reloc.get_r_info();
  5793. r_offset = reloc.get_r_offset();
  5794. }
  5795. unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
  5796. if (r_type != elfcpp::R_ARM_PREL31 && r_type != elfcpp::R_ARM_SBREL31)
  5797. continue;
  5798. unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
  5799. if (r_sym == 0
  5800. || r_sym >= this->local_symbol_count()
  5801. || r_offset != 0)
  5802. continue;
  5803. // This is the relocation for the first word of the first EXIDX entry.
  5804. // We expect to see a local section symbol.
  5805. const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
  5806. elfcpp::Sym<32, big_endian> sym(psyms + r_sym * sym_size);
  5807. if (sym.get_st_type() == elfcpp::STT_SECTION)
  5808. {
  5809. bool is_ordinary;
  5810. *pshndx =
  5811. this->adjust_sym_shndx(r_sym, sym.get_st_shndx(), &is_ordinary);
  5812. gold_assert(is_ordinary);
  5813. return true;
  5814. }
  5815. else
  5816. return false;
  5817. }
  5818. return false;
  5819. }
  5820. // Make an EXIDX input section object for an EXIDX section whose index is
  5821. // SHNDX. SHDR is the section header of the EXIDX section and TEXT_SHNDX
  5822. // is the section index of the linked text section.
  5823. template<bool big_endian>
  5824. void
  5825. Arm_relobj<big_endian>::make_exidx_input_section(
  5826. unsigned int shndx,
  5827. const elfcpp::Shdr<32, big_endian>& shdr,
  5828. unsigned int text_shndx,
  5829. const elfcpp::Shdr<32, big_endian>& text_shdr)
  5830. {
  5831. // Create an Arm_exidx_input_section object for this EXIDX section.
  5832. Arm_exidx_input_section* exidx_input_section =
  5833. new Arm_exidx_input_section(this, shndx, text_shndx, shdr.get_sh_size(),
  5834. shdr.get_sh_addralign(),
  5835. text_shdr.get_sh_size());
  5836. gold_assert(this->exidx_section_map_[shndx] == NULL);
  5837. this->exidx_section_map_[shndx] = exidx_input_section;
  5838. if (text_shndx == elfcpp::SHN_UNDEF || text_shndx >= this->shnum())
  5839. {
  5840. gold_error(_("EXIDX section %s(%u) links to invalid section %u in %s"),
  5841. this->section_name(shndx).c_str(), shndx, text_shndx,
  5842. this->name().c_str());
  5843. exidx_input_section->set_has_errors();
  5844. }
  5845. else if (this->exidx_section_map_[text_shndx] != NULL)
  5846. {
  5847. unsigned other_exidx_shndx =
  5848. this->exidx_section_map_[text_shndx]->shndx();
  5849. gold_error(_("EXIDX sections %s(%u) and %s(%u) both link to text section"
  5850. "%s(%u) in %s"),
  5851. this->section_name(shndx).c_str(), shndx,
  5852. this->section_name(other_exidx_shndx).c_str(),
  5853. other_exidx_shndx, this->section_name(text_shndx).c_str(),
  5854. text_shndx, this->name().c_str());
  5855. exidx_input_section->set_has_errors();
  5856. }
  5857. else
  5858. this->exidx_section_map_[text_shndx] = exidx_input_section;
  5859. // Check section flags of text section.
  5860. if ((text_shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
  5861. {
  5862. gold_error(_("EXIDX section %s(%u) links to non-allocated section %s(%u) "
  5863. " in %s"),
  5864. this->section_name(shndx).c_str(), shndx,
  5865. this->section_name(text_shndx).c_str(), text_shndx,
  5866. this->name().c_str());
  5867. exidx_input_section->set_has_errors();
  5868. }
  5869. else if ((text_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) == 0)
  5870. // I would like to make this an error but currently ld just ignores
  5871. // this.
  5872. gold_warning(_("EXIDX section %s(%u) links to non-executable section "
  5873. "%s(%u) in %s"),
  5874. this->section_name(shndx).c_str(), shndx,
  5875. this->section_name(text_shndx).c_str(), text_shndx,
  5876. this->name().c_str());
  5877. }
  5878. // Read the symbol information.
  5879. template<bool big_endian>
  5880. void
  5881. Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
  5882. {
  5883. // Call parent class to read symbol information.
  5884. this->base_read_symbols(sd);
  5885. // If this input file is a binary file, it has no processor
  5886. // specific flags and attributes section.
  5887. Input_file::Format format = this->input_file()->format();
  5888. if (format != Input_file::FORMAT_ELF)
  5889. {
  5890. gold_assert(format == Input_file::FORMAT_BINARY);
  5891. this->merge_flags_and_attributes_ = false;
  5892. return;
  5893. }
  5894. // Read processor-specific flags in ELF file header.
  5895. const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
  5896. elfcpp::Elf_sizes<32>::ehdr_size,
  5897. true, false);
  5898. elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
  5899. this->processor_specific_flags_ = ehdr.get_e_flags();
  5900. // Go over the section headers and look for .ARM.attributes and .ARM.exidx
  5901. // sections.
  5902. std::vector<unsigned int> deferred_exidx_sections;
  5903. const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
  5904. const unsigned char* pshdrs = sd->section_headers->data();
  5905. const unsigned char* ps = pshdrs + shdr_size;
  5906. bool must_merge_flags_and_attributes = false;
  5907. for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
  5908. {
  5909. elfcpp::Shdr<32, big_endian> shdr(ps);
  5910. // Sometimes an object has no contents except the section name string
  5911. // table and an empty symbol table with the undefined symbol. We
  5912. // don't want to merge processor-specific flags from such an object.
  5913. if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
  5914. {
  5915. // Symbol table is not empty.
  5916. const elfcpp::Elf_types<32>::Elf_WXword sym_size =
  5917. elfcpp::Elf_sizes<32>::sym_size;
  5918. if (shdr.get_sh_size() > sym_size)
  5919. must_merge_flags_and_attributes = true;
  5920. }
  5921. else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
  5922. // If this is neither an empty symbol table nor a string table,
  5923. // be conservative.
  5924. must_merge_flags_and_attributes = true;
  5925. if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
  5926. {
  5927. gold_assert(this->attributes_section_data_ == NULL);
  5928. section_offset_type section_offset = shdr.get_sh_offset();
  5929. section_size_type section_size =
  5930. convert_to_section_size_type(shdr.get_sh_size());
  5931. const unsigned char* view =
  5932. this->get_view(section_offset, section_size, true, false);
  5933. this->attributes_section_data_ =
  5934. new Attributes_section_data(view, section_size);
  5935. }
  5936. else if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
  5937. {
  5938. unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
  5939. if (text_shndx == elfcpp::SHN_UNDEF)
  5940. deferred_exidx_sections.push_back(i);
  5941. else
  5942. {
  5943. elfcpp::Shdr<32, big_endian> text_shdr(pshdrs
  5944. + text_shndx * shdr_size);
  5945. this->make_exidx_input_section(i, shdr, text_shndx, text_shdr);
  5946. }
  5947. // EHABI 4.4.1 requires that SHF_LINK_ORDER flag to be set.
  5948. if ((shdr.get_sh_flags() & elfcpp::SHF_LINK_ORDER) == 0)
  5949. gold_warning(_("SHF_LINK_ORDER not set in EXIDX section %s of %s"),
  5950. this->section_name(i).c_str(), this->name().c_str());
  5951. }
  5952. }
  5953. // This is rare.
  5954. if (!must_merge_flags_and_attributes)
  5955. {
  5956. gold_assert(deferred_exidx_sections.empty());
  5957. this->merge_flags_and_attributes_ = false;
  5958. return;
  5959. }
  5960. // Some tools are broken and they do not set the link of EXIDX sections.
  5961. // We look at the first relocation to figure out the linked sections.
  5962. if (!deferred_exidx_sections.empty())
  5963. {
  5964. // We need to go over the section headers again to find the mapping
  5965. // from sections being relocated to their relocation sections. This is
  5966. // a bit inefficient as we could do that in the loop above. However,
  5967. // we do not expect any deferred EXIDX sections normally. So we do not
  5968. // want to slow down the most common path.
  5969. typedef Unordered_map<unsigned int, unsigned int> Reloc_map;
  5970. Reloc_map reloc_map;
  5971. ps = pshdrs + shdr_size;
  5972. for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
  5973. {
  5974. elfcpp::Shdr<32, big_endian> shdr(ps);
  5975. elfcpp::Elf_Word sh_type = shdr.get_sh_type();
  5976. if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
  5977. {
  5978. unsigned int info_shndx = this->adjust_shndx(shdr.get_sh_info());
  5979. if (info_shndx >= this->shnum())
  5980. gold_error(_("relocation section %u has invalid info %u"),
  5981. i, info_shndx);
  5982. Reloc_map::value_type value(info_shndx, i);
  5983. std::pair<Reloc_map::iterator, bool> result =
  5984. reloc_map.insert(value);
  5985. if (!result.second)
  5986. gold_error(_("section %u has multiple relocation sections "
  5987. "%u and %u"),
  5988. info_shndx, i, reloc_map[info_shndx]);
  5989. }
  5990. }
  5991. // Read the symbol table section header.
  5992. const unsigned int symtab_shndx = this->symtab_shndx();
  5993. elfcpp::Shdr<32, big_endian>
  5994. symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
  5995. gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
  5996. // Read the local symbols.
  5997. const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
  5998. const unsigned int loccount = this->local_symbol_count();
  5999. gold_assert(loccount == symtabshdr.get_sh_info());
  6000. off_t locsize = loccount * sym_size;
  6001. const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
  6002. locsize, true, true);
  6003. // Process the deferred EXIDX sections.
  6004. for (unsigned int i = 0; i < deferred_exidx_sections.size(); ++i)
  6005. {
  6006. unsigned int shndx = deferred_exidx_sections[i];
  6007. elfcpp::Shdr<32, big_endian> shdr(pshdrs + shndx * shdr_size);
  6008. unsigned int text_shndx = elfcpp::SHN_UNDEF;
  6009. Reloc_map::const_iterator it = reloc_map.find(shndx);
  6010. if (it != reloc_map.end())
  6011. find_linked_text_section(pshdrs + it->second * shdr_size,
  6012. psyms, &text_shndx);
  6013. elfcpp::Shdr<32, big_endian> text_shdr(pshdrs
  6014. + text_shndx * shdr_size);
  6015. this->make_exidx_input_section(shndx, shdr, text_shndx, text_shdr);
  6016. }
  6017. }
  6018. }
  6019. // Process relocations for garbage collection. The ARM target uses .ARM.exidx
  6020. // sections for unwinding. These sections are referenced implicitly by
  6021. // text sections linked in the section headers. If we ignore these implicit
  6022. // references, the .ARM.exidx sections and any .ARM.extab sections they use
  6023. // will be garbage-collected incorrectly. Hence we override the same function
  6024. // in the base class to handle these implicit references.
  6025. template<bool big_endian>
  6026. void
  6027. Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
  6028. Layout* layout,
  6029. Read_relocs_data* rd)
  6030. {
  6031. // First, call base class method to process relocations in this object.
  6032. Sized_relobj_file<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
  6033. // If --gc-sections is not specified, there is nothing more to do.
  6034. // This happens when --icf is used but --gc-sections is not.
  6035. if (!parameters->options().gc_sections())
  6036. return;
  6037. unsigned int shnum = this->shnum();
  6038. const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
  6039. const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
  6040. shnum * shdr_size,
  6041. true, true);
  6042. // Scan section headers for sections of type SHT_ARM_EXIDX. Add references
  6043. // to these from the linked text sections.
  6044. const unsigned char* ps = pshdrs + shdr_size;
  6045. for (unsigned int i = 1; i < shnum; ++i, ps += shdr_size)
  6046. {
  6047. elfcpp::Shdr<32, big_endian> shdr(ps);
  6048. if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
  6049. {
  6050. // Found an .ARM.exidx section, add it to the set of reachable
  6051. // sections from its linked text section.
  6052. unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
  6053. symtab->gc()->add_reference(this, text_shndx, this, i);
  6054. }
  6055. }
  6056. }
  6057. // Update output local symbol count. Owing to EXIDX entry merging, some local
  6058. // symbols will be removed in output. Adjust output local symbol count
  6059. // accordingly. We can only changed the static output local symbol count. It
  6060. // is too late to change the dynamic symbols.
  6061. template<bool big_endian>
  6062. void
  6063. Arm_relobj<big_endian>::update_output_local_symbol_count()
  6064. {
  6065. // Caller should check that this needs updating. We want caller checking
  6066. // because output_local_symbol_count_needs_update() is most likely inlined.
  6067. gold_assert(this->output_local_symbol_count_needs_update_);
  6068. gold_assert(this->symtab_shndx() != -1U);
  6069. if (this->symtab_shndx() == 0)
  6070. {
  6071. // This object has no symbols. Weird but legal.
  6072. return;
  6073. }
  6074. // Read the symbol table section header.
  6075. const unsigned int symtab_shndx = this->symtab_shndx();
  6076. elfcpp::Shdr<32, big_endian>
  6077. symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
  6078. gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
  6079. // Read the local symbols.
  6080. const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
  6081. const unsigned int loccount = this->local_symbol_count();
  6082. gold_assert(loccount == symtabshdr.get_sh_info());
  6083. off_t locsize = loccount * sym_size;
  6084. const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
  6085. locsize, true, true);
  6086. // Loop over the local symbols.
  6087. typedef typename Sized_relobj_file<32, big_endian>::Output_sections
  6088. Output_sections;
  6089. const Output_sections& out_sections(this->output_sections());
  6090. unsigned int shnum = this->shnum();
  6091. unsigned int count = 0;
  6092. // Skip the first, dummy, symbol.
  6093. psyms += sym_size;
  6094. for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
  6095. {
  6096. elfcpp::Sym<32, big_endian> sym(psyms);
  6097. Symbol_value<32>& lv((*this->local_values())[i]);
  6098. // This local symbol was already discarded by do_count_local_symbols.
  6099. if (lv.is_output_symtab_index_set() && !lv.has_output_symtab_entry())
  6100. continue;
  6101. bool is_ordinary;
  6102. unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
  6103. &is_ordinary);
  6104. if (shndx < shnum)
  6105. {
  6106. Output_section* os = out_sections[shndx];
  6107. // This local symbol no longer has an output section. Discard it.
  6108. if (os == NULL)
  6109. {
  6110. lv.set_no_output_symtab_entry();
  6111. continue;
  6112. }
  6113. // Currently we only discard parts of EXIDX input sections.
  6114. // We explicitly check for a merged EXIDX input section to avoid
  6115. // calling Output_section_data::output_offset unless necessary.
  6116. if ((this->get_output_section_offset(shndx) == invalid_address)
  6117. && (this->exidx_input_section_by_shndx(shndx) != NULL))
  6118. {
  6119. section_offset_type output_offset =
  6120. os->output_offset(this, shndx, lv.input_value());
  6121. if (output_offset == -1)
  6122. {
  6123. // This symbol is defined in a part of an EXIDX input section
  6124. // that is discarded due to entry merging.
  6125. lv.set_no_output_symtab_entry();
  6126. continue;
  6127. }
  6128. }
  6129. }
  6130. ++count;
  6131. }
  6132. this->set_output_local_symbol_count(count);
  6133. this->output_local_symbol_count_needs_update_ = false;
  6134. }
  6135. // Arm_dynobj methods.
  6136. // Read the symbol information.
  6137. template<bool big_endian>
  6138. void
  6139. Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
  6140. {
  6141. // Call parent class to read symbol information.
  6142. this->base_read_symbols(sd);
  6143. // Read processor-specific flags in ELF file header.
  6144. const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
  6145. elfcpp::Elf_sizes<32>::ehdr_size,
  6146. true, false);
  6147. elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
  6148. this->processor_specific_flags_ = ehdr.get_e_flags();
  6149. // Read the attributes section if there is one.
  6150. // We read from the end because gas seems to put it near the end of
  6151. // the section headers.
  6152. const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
  6153. const unsigned char* ps =
  6154. sd->section_headers->data() + shdr_size * (this->shnum() - 1);
  6155. for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
  6156. {
  6157. elfcpp::Shdr<32, big_endian> shdr(ps);
  6158. if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
  6159. {
  6160. section_offset_type section_offset = shdr.get_sh_offset();
  6161. section_size_type section_size =
  6162. convert_to_section_size_type(shdr.get_sh_size());
  6163. const unsigned char* view =
  6164. this->get_view(section_offset, section_size, true, false);
  6165. this->attributes_section_data_ =
  6166. new Attributes_section_data(view, section_size);
  6167. break;
  6168. }
  6169. }
  6170. }
  6171. // Stub_addend_reader methods.
  6172. // Read the addend of a REL relocation of type R_TYPE at VIEW.
  6173. template<bool big_endian>
  6174. elfcpp::Elf_types<32>::Elf_Swxword
  6175. Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
  6176. unsigned int r_type,
  6177. const unsigned char* view,
  6178. const typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc&) const
  6179. {
  6180. typedef class Arm_relocate_functions<big_endian> RelocFuncs;
  6181. switch (r_type)
  6182. {
  6183. case elfcpp::R_ARM_CALL:
  6184. case elfcpp::R_ARM_JUMP24:
  6185. case elfcpp::R_ARM_PLT32:
  6186. {
  6187. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  6188. const Valtype* wv = reinterpret_cast<const Valtype*>(view);
  6189. Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
  6190. return Bits<26>::sign_extend32(val << 2);
  6191. }
  6192. case elfcpp::R_ARM_THM_CALL:
  6193. case elfcpp::R_ARM_THM_JUMP24:
  6194. case elfcpp::R_ARM_THM_XPC22:
  6195. {
  6196. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
  6197. const Valtype* wv = reinterpret_cast<const Valtype*>(view);
  6198. Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
  6199. Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
  6200. return RelocFuncs::thumb32_branch_offset(upper_insn, lower_insn);
  6201. }
  6202. case elfcpp::R_ARM_THM_JUMP19:
  6203. {
  6204. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
  6205. const Valtype* wv = reinterpret_cast<const Valtype*>(view);
  6206. Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
  6207. Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
  6208. return RelocFuncs::thumb32_cond_branch_offset(upper_insn, lower_insn);
  6209. }
  6210. default:
  6211. gold_unreachable();
  6212. }
  6213. }
  6214. // Arm_output_data_got methods.
  6215. // Add a GOT pair for R_ARM_TLS_GD32. The creates a pair of GOT entries.
  6216. // The first one is initialized to be 1, which is the module index for
  6217. // the main executable and the second one 0. A reloc of the type
  6218. // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
  6219. // be applied by gold. GSYM is a global symbol.
  6220. //
  6221. template<bool big_endian>
  6222. void
  6223. Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
  6224. unsigned int got_type,
  6225. Symbol* gsym)
  6226. {
  6227. if (gsym->has_got_offset(got_type))
  6228. return;
  6229. // We are doing a static link. Just mark it as belong to module 1,
  6230. // the executable.
  6231. unsigned int got_offset = this->add_constant(1);
  6232. gsym->set_got_offset(got_type, got_offset);
  6233. got_offset = this->add_constant(0);
  6234. this->static_relocs_.push_back(Static_reloc(got_offset,
  6235. elfcpp::R_ARM_TLS_DTPOFF32,
  6236. gsym));
  6237. }
  6238. // Same as the above but for a local symbol.
  6239. template<bool big_endian>
  6240. void
  6241. Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
  6242. unsigned int got_type,
  6243. Sized_relobj_file<32, big_endian>* object,
  6244. unsigned int index)
  6245. {
  6246. if (object->local_has_got_offset(index, got_type))
  6247. return;
  6248. // We are doing a static link. Just mark it as belong to module 1,
  6249. // the executable.
  6250. unsigned int got_offset = this->add_constant(1);
  6251. object->set_local_got_offset(index, got_type, got_offset);
  6252. got_offset = this->add_constant(0);
  6253. this->static_relocs_.push_back(Static_reloc(got_offset,
  6254. elfcpp::R_ARM_TLS_DTPOFF32,
  6255. object, index));
  6256. }
  6257. template<bool big_endian>
  6258. void
  6259. Arm_output_data_got<big_endian>::do_write(Output_file* of)
  6260. {
  6261. // Call parent to write out GOT.
  6262. Output_data_got<32, big_endian>::do_write(of);
  6263. // We are done if there is no fix up.
  6264. if (this->static_relocs_.empty())
  6265. return;
  6266. gold_assert(parameters->doing_static_link());
  6267. const off_t offset = this->offset();
  6268. const section_size_type oview_size =
  6269. convert_to_section_size_type(this->data_size());
  6270. unsigned char* const oview = of->get_output_view(offset, oview_size);
  6271. Output_segment* tls_segment = this->layout_->tls_segment();
  6272. gold_assert(tls_segment != NULL);
  6273. // The thread pointer $tp points to the TCB, which is followed by the
  6274. // TLS. So we need to adjust $tp relative addressing by this amount.
  6275. Arm_address aligned_tcb_size =
  6276. align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
  6277. for (size_t i = 0; i < this->static_relocs_.size(); ++i)
  6278. {
  6279. Static_reloc& reloc(this->static_relocs_[i]);
  6280. Arm_address value;
  6281. if (!reloc.symbol_is_global())
  6282. {
  6283. Sized_relobj_file<32, big_endian>* object = reloc.relobj();
  6284. const Symbol_value<32>* psymval =
  6285. reloc.relobj()->local_symbol(reloc.index());
  6286. // We are doing static linking. Issue an error and skip this
  6287. // relocation if the symbol is undefined or in a discarded_section.
  6288. bool is_ordinary;
  6289. unsigned int shndx = psymval->input_shndx(&is_ordinary);
  6290. if ((shndx == elfcpp::SHN_UNDEF)
  6291. || (is_ordinary
  6292. && shndx != elfcpp::SHN_UNDEF
  6293. && !object->is_section_included(shndx)
  6294. && !this->symbol_table_->is_section_folded(object, shndx)))
  6295. {
  6296. gold_error(_("undefined or discarded local symbol %u from "
  6297. " object %s in GOT"),
  6298. reloc.index(), reloc.relobj()->name().c_str());
  6299. continue;
  6300. }
  6301. value = psymval->value(object, 0);
  6302. }
  6303. else
  6304. {
  6305. const Symbol* gsym = reloc.symbol();
  6306. gold_assert(gsym != NULL);
  6307. if (gsym->is_forwarder())
  6308. gsym = this->symbol_table_->resolve_forwards(gsym);
  6309. // We are doing static linking. Issue an error and skip this
  6310. // relocation if the symbol is undefined or in a discarded_section
  6311. // unless it is a weakly_undefined symbol.
  6312. if ((gsym->is_defined_in_discarded_section()
  6313. || gsym->is_undefined())
  6314. && !gsym->is_weak_undefined())
  6315. {
  6316. gold_error(_("undefined or discarded symbol %s in GOT"),
  6317. gsym->name());
  6318. continue;
  6319. }
  6320. if (!gsym->is_weak_undefined())
  6321. {
  6322. const Sized_symbol<32>* sym =
  6323. static_cast<const Sized_symbol<32>*>(gsym);
  6324. value = sym->value();
  6325. }
  6326. else
  6327. value = 0;
  6328. }
  6329. unsigned got_offset = reloc.got_offset();
  6330. gold_assert(got_offset < oview_size);
  6331. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  6332. Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
  6333. Valtype x;
  6334. switch (reloc.r_type())
  6335. {
  6336. case elfcpp::R_ARM_TLS_DTPOFF32:
  6337. x = value;
  6338. break;
  6339. case elfcpp::R_ARM_TLS_TPOFF32:
  6340. x = value + aligned_tcb_size;
  6341. break;
  6342. default:
  6343. gold_unreachable();
  6344. }
  6345. elfcpp::Swap<32, big_endian>::writeval(wv, x);
  6346. }
  6347. of->write_output_view(offset, oview_size, oview);
  6348. }
  6349. // A class to handle the PLT data.
  6350. // This is an abstract base class that handles most of the linker details
  6351. // but does not know the actual contents of PLT entries. The derived
  6352. // classes below fill in those details.
  6353. template<bool big_endian>
  6354. class Output_data_plt_arm : public Output_section_data
  6355. {
  6356. public:
  6357. // Unlike aarch64, which records symbol value in "addend" field of relocations
  6358. // and could be done at the same time an IRelative reloc is created for the
  6359. // symbol, arm puts the symbol value into "GOT" table, which, however, is
  6360. // issued later in Output_data_plt_arm::do_write(). So we have a struct here
  6361. // to keep necessary symbol information for later use in do_write. We usually
  6362. // have only a very limited number of ifuncs, so the extra data required here
  6363. // is also limited.
  6364. struct IRelative_data
  6365. {
  6366. IRelative_data(Sized_symbol<32>* sized_symbol)
  6367. : symbol_is_global_(true)
  6368. {
  6369. u_.global = sized_symbol;
  6370. }
  6371. IRelative_data(Sized_relobj_file<32, big_endian>* relobj,
  6372. unsigned int index)
  6373. : symbol_is_global_(false)
  6374. {
  6375. u_.local.relobj = relobj;
  6376. u_.local.index = index;
  6377. }
  6378. union
  6379. {
  6380. Sized_symbol<32>* global;
  6381. struct
  6382. {
  6383. Sized_relobj_file<32, big_endian>* relobj;
  6384. unsigned int index;
  6385. } local;
  6386. } u_;
  6387. bool symbol_is_global_;
  6388. };
  6389. typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
  6390. Reloc_section;
  6391. Output_data_plt_arm(Layout* layout, uint64_t addralign,
  6392. Arm_output_data_got<big_endian>* got,
  6393. Output_data_space* got_plt,
  6394. Output_data_space* got_irelative);
  6395. // Add an entry to the PLT.
  6396. void
  6397. add_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym);
  6398. // Add the relocation for a plt entry.
  6399. void
  6400. add_relocation(Symbol_table* symtab, Layout* layout,
  6401. Symbol* gsym, unsigned int got_offset);
  6402. // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
  6403. unsigned int
  6404. add_local_ifunc_entry(Symbol_table* symtab, Layout*,
  6405. Sized_relobj_file<32, big_endian>* relobj,
  6406. unsigned int local_sym_index);
  6407. // Return the .rel.plt section data.
  6408. const Reloc_section*
  6409. rel_plt() const
  6410. { return this->rel_; }
  6411. // Return the PLT relocation container for IRELATIVE.
  6412. Reloc_section*
  6413. rel_irelative(Symbol_table*, Layout*);
  6414. // Return the number of PLT entries.
  6415. unsigned int
  6416. entry_count() const
  6417. { return this->count_ + this->irelative_count_; }
  6418. // Return the offset of the first non-reserved PLT entry.
  6419. unsigned int
  6420. first_plt_entry_offset() const
  6421. { return this->do_first_plt_entry_offset(); }
  6422. // Return the size of a PLT entry.
  6423. unsigned int
  6424. get_plt_entry_size() const
  6425. { return this->do_get_plt_entry_size(); }
  6426. // Return the PLT address for globals.
  6427. uint32_t
  6428. address_for_global(const Symbol*) const;
  6429. // Return the PLT address for locals.
  6430. uint32_t
  6431. address_for_local(const Relobj*, unsigned int symndx) const;
  6432. protected:
  6433. // Fill in the first PLT entry.
  6434. void
  6435. fill_first_plt_entry(unsigned char* pov,
  6436. Arm_address got_address,
  6437. Arm_address plt_address)
  6438. { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
  6439. void
  6440. fill_plt_entry(unsigned char* pov,
  6441. Arm_address got_address,
  6442. Arm_address plt_address,
  6443. unsigned int got_offset,
  6444. unsigned int plt_offset)
  6445. { do_fill_plt_entry(pov, got_address, plt_address, got_offset, plt_offset); }
  6446. virtual unsigned int
  6447. do_first_plt_entry_offset() const = 0;
  6448. virtual unsigned int
  6449. do_get_plt_entry_size() const = 0;
  6450. virtual void
  6451. do_fill_first_plt_entry(unsigned char* pov,
  6452. Arm_address got_address,
  6453. Arm_address plt_address) = 0;
  6454. virtual void
  6455. do_fill_plt_entry(unsigned char* pov,
  6456. Arm_address got_address,
  6457. Arm_address plt_address,
  6458. unsigned int got_offset,
  6459. unsigned int plt_offset) = 0;
  6460. void
  6461. do_adjust_output_section(Output_section* os);
  6462. // Write to a map file.
  6463. void
  6464. do_print_to_mapfile(Mapfile* mapfile) const
  6465. { mapfile->print_output_data(this, _("** PLT")); }
  6466. private:
  6467. // Set the final size.
  6468. void
  6469. set_final_data_size()
  6470. {
  6471. this->set_data_size(this->first_plt_entry_offset()
  6472. + ((this->count_ + this->irelative_count_)
  6473. * this->get_plt_entry_size()));
  6474. }
  6475. // Write out the PLT data.
  6476. void
  6477. do_write(Output_file*);
  6478. // Record irelative symbol data.
  6479. void insert_irelative_data(const IRelative_data& idata)
  6480. { irelative_data_vec_.push_back(idata); }
  6481. // The reloc section.
  6482. Reloc_section* rel_;
  6483. // The IRELATIVE relocs, if necessary. These must follow the
  6484. // regular PLT relocations.
  6485. Reloc_section* irelative_rel_;
  6486. // The .got section.
  6487. Arm_output_data_got<big_endian>* got_;
  6488. // The .got.plt section.
  6489. Output_data_space* got_plt_;
  6490. // The part of the .got.plt section used for IRELATIVE relocs.
  6491. Output_data_space* got_irelative_;
  6492. // The number of PLT entries.
  6493. unsigned int count_;
  6494. // Number of PLT entries with R_ARM_IRELATIVE relocs. These
  6495. // follow the regular PLT entries.
  6496. unsigned int irelative_count_;
  6497. // Vector for irelative data.
  6498. typedef std::vector<IRelative_data> IRelative_data_vec;
  6499. IRelative_data_vec irelative_data_vec_;
  6500. };
  6501. // Create the PLT section. The ordinary .got section is an argument,
  6502. // since we need to refer to the start. We also create our own .got
  6503. // section just for PLT entries.
  6504. template<bool big_endian>
  6505. Output_data_plt_arm<big_endian>::Output_data_plt_arm(
  6506. Layout* layout, uint64_t addralign,
  6507. Arm_output_data_got<big_endian>* got,
  6508. Output_data_space* got_plt,
  6509. Output_data_space* got_irelative)
  6510. : Output_section_data(addralign), irelative_rel_(NULL),
  6511. got_(got), got_plt_(got_plt), got_irelative_(got_irelative),
  6512. count_(0), irelative_count_(0)
  6513. {
  6514. this->rel_ = new Reloc_section(false);
  6515. layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
  6516. elfcpp::SHF_ALLOC, this->rel_,
  6517. ORDER_DYNAMIC_PLT_RELOCS, false);
  6518. }
  6519. template<bool big_endian>
  6520. void
  6521. Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
  6522. {
  6523. os->set_entsize(0);
  6524. }
  6525. // Add an entry to the PLT.
  6526. template<bool big_endian>
  6527. void
  6528. Output_data_plt_arm<big_endian>::add_entry(Symbol_table* symtab,
  6529. Layout* layout,
  6530. Symbol* gsym)
  6531. {
  6532. gold_assert(!gsym->has_plt_offset());
  6533. unsigned int* entry_count;
  6534. Output_section_data_build* got;
  6535. // We have 2 different types of plt entry here, normal and ifunc.
  6536. // For normal plt, the offset begins with first_plt_entry_offset(20), and the
  6537. // 1st entry offset would be 20, the second 32, third 44 ... etc.
  6538. // For ifunc plt, the offset begins with 0. So the first offset would 0,
  6539. // second 12, third 24 ... etc.
  6540. // IFunc plt entries *always* come after *normal* plt entries.
  6541. // Notice, when computing the plt address of a certain symbol, "plt_address +
  6542. // plt_offset" is no longer correct. Use target->plt_address_for_global() or
  6543. // target->plt_address_for_local() instead.
  6544. int begin_offset = 0;
  6545. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  6546. && gsym->can_use_relative_reloc(false))
  6547. {
  6548. entry_count = &this->irelative_count_;
  6549. got = this->got_irelative_;
  6550. // For irelative plt entries, offset is relative to the end of normal plt
  6551. // entries, so it starts from 0.
  6552. begin_offset = 0;
  6553. // Record symbol information.
  6554. this->insert_irelative_data(
  6555. IRelative_data(symtab->get_sized_symbol<32>(gsym)));
  6556. }
  6557. else
  6558. {
  6559. entry_count = &this->count_;
  6560. got = this->got_plt_;
  6561. // Note that for normal plt entries, when setting the PLT offset we skip
  6562. // the initial reserved PLT entry.
  6563. begin_offset = this->first_plt_entry_offset();
  6564. }
  6565. gsym->set_plt_offset(begin_offset
  6566. + (*entry_count) * this->get_plt_entry_size());
  6567. ++(*entry_count);
  6568. section_offset_type got_offset = got->current_data_size();
  6569. // Every PLT entry needs a GOT entry which points back to the PLT
  6570. // entry (this will be changed by the dynamic linker, normally
  6571. // lazily when the function is called).
  6572. got->set_current_data_size(got_offset + 4);
  6573. // Every PLT entry needs a reloc.
  6574. this->add_relocation(symtab, layout, gsym, got_offset);
  6575. // Note that we don't need to save the symbol. The contents of the
  6576. // PLT are independent of which symbols are used. The symbols only
  6577. // appear in the relocations.
  6578. }
  6579. // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
  6580. // the PLT offset.
  6581. template<bool big_endian>
  6582. unsigned int
  6583. Output_data_plt_arm<big_endian>::add_local_ifunc_entry(
  6584. Symbol_table* symtab,
  6585. Layout* layout,
  6586. Sized_relobj_file<32, big_endian>* relobj,
  6587. unsigned int local_sym_index)
  6588. {
  6589. this->insert_irelative_data(IRelative_data(relobj, local_sym_index));
  6590. // Notice, when computingthe plt entry address, "plt_address + plt_offset" is
  6591. // no longer correct. Use target->plt_address_for_local() instead.
  6592. unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
  6593. ++this->irelative_count_;
  6594. section_offset_type got_offset = this->got_irelative_->current_data_size();
  6595. // Every PLT entry needs a GOT entry which points back to the PLT
  6596. // entry.
  6597. this->got_irelative_->set_current_data_size(got_offset + 4);
  6598. // Every PLT entry needs a reloc.
  6599. Reloc_section* rel = this->rel_irelative(symtab, layout);
  6600. rel->add_symbolless_local_addend(relobj, local_sym_index,
  6601. elfcpp::R_ARM_IRELATIVE,
  6602. this->got_irelative_, got_offset);
  6603. return plt_offset;
  6604. }
  6605. // Add the relocation for a PLT entry.
  6606. template<bool big_endian>
  6607. void
  6608. Output_data_plt_arm<big_endian>::add_relocation(
  6609. Symbol_table* symtab, Layout* layout, Symbol* gsym, unsigned int got_offset)
  6610. {
  6611. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  6612. && gsym->can_use_relative_reloc(false))
  6613. {
  6614. Reloc_section* rel = this->rel_irelative(symtab, layout);
  6615. rel->add_symbolless_global_addend(gsym, elfcpp::R_ARM_IRELATIVE,
  6616. this->got_irelative_, got_offset);
  6617. }
  6618. else
  6619. {
  6620. gsym->set_needs_dynsym_entry();
  6621. this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
  6622. got_offset);
  6623. }
  6624. }
  6625. // Create the irelative relocation data.
  6626. template<bool big_endian>
  6627. typename Output_data_plt_arm<big_endian>::Reloc_section*
  6628. Output_data_plt_arm<big_endian>::rel_irelative(Symbol_table* symtab,
  6629. Layout* layout)
  6630. {
  6631. if (this->irelative_rel_ == NULL)
  6632. {
  6633. // Since irelative relocations goes into 'rel.dyn', we delegate the
  6634. // creation of irelative_rel_ to where rel_dyn section gets created.
  6635. Target_arm<big_endian>* arm_target =
  6636. Target_arm<big_endian>::default_target();
  6637. this->irelative_rel_ = arm_target->rel_irelative_section(layout);
  6638. // Make sure we have a place for the TLSDESC relocations, in
  6639. // case we see any later on.
  6640. // this->rel_tlsdesc(layout);
  6641. if (parameters->doing_static_link())
  6642. {
  6643. // A statically linked executable will only have a .rel.plt section to
  6644. // hold R_ARM_IRELATIVE relocs for STT_GNU_IFUNC symbols. The library
  6645. // will use these symbols to locate the IRELATIVE relocs at program
  6646. // startup time.
  6647. symtab->define_in_output_data("__rel_iplt_start", NULL,
  6648. Symbol_table::PREDEFINED,
  6649. this->irelative_rel_, 0, 0,
  6650. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  6651. elfcpp::STV_HIDDEN, 0, false, true);
  6652. symtab->define_in_output_data("__rel_iplt_end", NULL,
  6653. Symbol_table::PREDEFINED,
  6654. this->irelative_rel_, 0, 0,
  6655. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  6656. elfcpp::STV_HIDDEN, 0, true, true);
  6657. }
  6658. }
  6659. return this->irelative_rel_;
  6660. }
  6661. // Return the PLT address for a global symbol.
  6662. template<bool big_endian>
  6663. uint32_t
  6664. Output_data_plt_arm<big_endian>::address_for_global(const Symbol* gsym) const
  6665. {
  6666. uint64_t begin_offset = 0;
  6667. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  6668. && gsym->can_use_relative_reloc(false))
  6669. {
  6670. begin_offset = (this->first_plt_entry_offset() +
  6671. this->count_ * this->get_plt_entry_size());
  6672. }
  6673. return this->address() + begin_offset + gsym->plt_offset();
  6674. }
  6675. // Return the PLT address for a local symbol. These are always
  6676. // IRELATIVE relocs.
  6677. template<bool big_endian>
  6678. uint32_t
  6679. Output_data_plt_arm<big_endian>::address_for_local(
  6680. const Relobj* object,
  6681. unsigned int r_sym) const
  6682. {
  6683. return (this->address()
  6684. + this->first_plt_entry_offset()
  6685. + this->count_ * this->get_plt_entry_size()
  6686. + object->local_plt_offset(r_sym));
  6687. }
  6688. template<bool big_endian>
  6689. class Output_data_plt_arm_standard : public Output_data_plt_arm<big_endian>
  6690. {
  6691. public:
  6692. Output_data_plt_arm_standard(Layout* layout,
  6693. Arm_output_data_got<big_endian>* got,
  6694. Output_data_space* got_plt,
  6695. Output_data_space* got_irelative)
  6696. : Output_data_plt_arm<big_endian>(layout, 4, got, got_plt, got_irelative)
  6697. { }
  6698. protected:
  6699. // Return the offset of the first non-reserved PLT entry.
  6700. virtual unsigned int
  6701. do_first_plt_entry_offset() const
  6702. { return sizeof(first_plt_entry); }
  6703. // Return the size of a PLT entry.
  6704. virtual unsigned int
  6705. do_get_plt_entry_size() const
  6706. { return sizeof(plt_entry); }
  6707. virtual void
  6708. do_fill_first_plt_entry(unsigned char* pov,
  6709. Arm_address got_address,
  6710. Arm_address plt_address);
  6711. virtual void
  6712. do_fill_plt_entry(unsigned char* pov,
  6713. Arm_address got_address,
  6714. Arm_address plt_address,
  6715. unsigned int got_offset,
  6716. unsigned int plt_offset);
  6717. private:
  6718. // Template for the first PLT entry.
  6719. static const uint32_t first_plt_entry[5];
  6720. // Template for subsequent PLT entries.
  6721. static const uint32_t plt_entry[3];
  6722. };
  6723. // ARM PLTs.
  6724. // FIXME: This is not very flexible. Right now this has only been tested
  6725. // on armv5te. If we are to support additional architecture features like
  6726. // Thumb-2 or BE8, we need to make this more flexible like GNU ld.
  6727. // The first entry in the PLT.
  6728. template<bool big_endian>
  6729. const uint32_t Output_data_plt_arm_standard<big_endian>::first_plt_entry[5] =
  6730. {
  6731. 0xe52de004, // str lr, [sp, #-4]!
  6732. 0xe59fe004, // ldr lr, [pc, #4]
  6733. 0xe08fe00e, // add lr, pc, lr
  6734. 0xe5bef008, // ldr pc, [lr, #8]!
  6735. 0x00000000, // &GOT[0] - .
  6736. };
  6737. template<bool big_endian>
  6738. void
  6739. Output_data_plt_arm_standard<big_endian>::do_fill_first_plt_entry(
  6740. unsigned char* pov,
  6741. Arm_address got_address,
  6742. Arm_address plt_address)
  6743. {
  6744. // Write first PLT entry. All but the last word are constants.
  6745. const size_t num_first_plt_words = (sizeof(first_plt_entry)
  6746. / sizeof(plt_entry[0]));
  6747. for (size_t i = 0; i < num_first_plt_words - 1; i++)
  6748. elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
  6749. // Last word in first PLT entry is &GOT[0] - .
  6750. elfcpp::Swap<32, big_endian>::writeval(pov + 16,
  6751. got_address - (plt_address + 16));
  6752. }
  6753. // Subsequent entries in the PLT.
  6754. template<bool big_endian>
  6755. const uint32_t Output_data_plt_arm_standard<big_endian>::plt_entry[3] =
  6756. {
  6757. 0xe28fc600, // add ip, pc, #0xNN00000
  6758. 0xe28cca00, // add ip, ip, #0xNN000
  6759. 0xe5bcf000, // ldr pc, [ip, #0xNNN]!
  6760. };
  6761. template<bool big_endian>
  6762. void
  6763. Output_data_plt_arm_standard<big_endian>::do_fill_plt_entry(
  6764. unsigned char* pov,
  6765. Arm_address got_address,
  6766. Arm_address plt_address,
  6767. unsigned int got_offset,
  6768. unsigned int plt_offset)
  6769. {
  6770. int32_t offset = ((got_address + got_offset)
  6771. - (plt_address + plt_offset + 8));
  6772. gold_assert(offset >= 0 && offset < 0x0fffffff);
  6773. uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
  6774. elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
  6775. uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
  6776. elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
  6777. uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
  6778. elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
  6779. }
  6780. // Write out the PLT. This uses the hand-coded instructions above,
  6781. // and adjusts them as needed. This is all specified by the arm ELF
  6782. // Processor Supplement.
  6783. template<bool big_endian>
  6784. void
  6785. Output_data_plt_arm<big_endian>::do_write(Output_file* of)
  6786. {
  6787. const off_t offset = this->offset();
  6788. const section_size_type oview_size =
  6789. convert_to_section_size_type(this->data_size());
  6790. unsigned char* const oview = of->get_output_view(offset, oview_size);
  6791. const off_t got_file_offset = this->got_plt_->offset();
  6792. gold_assert(got_file_offset + this->got_plt_->data_size()
  6793. == this->got_irelative_->offset());
  6794. const section_size_type got_size =
  6795. convert_to_section_size_type(this->got_plt_->data_size()
  6796. + this->got_irelative_->data_size());
  6797. unsigned char* const got_view = of->get_output_view(got_file_offset,
  6798. got_size);
  6799. unsigned char* pov = oview;
  6800. Arm_address plt_address = this->address();
  6801. Arm_address got_address = this->got_plt_->address();
  6802. // Write first PLT entry.
  6803. this->fill_first_plt_entry(pov, got_address, plt_address);
  6804. pov += this->first_plt_entry_offset();
  6805. unsigned char* got_pov = got_view;
  6806. memset(got_pov, 0, 12);
  6807. got_pov += 12;
  6808. unsigned int plt_offset = this->first_plt_entry_offset();
  6809. unsigned int got_offset = 12;
  6810. const unsigned int count = this->count_ + this->irelative_count_;
  6811. gold_assert(this->irelative_count_ == this->irelative_data_vec_.size());
  6812. for (unsigned int i = 0;
  6813. i < count;
  6814. ++i,
  6815. pov += this->get_plt_entry_size(),
  6816. got_pov += 4,
  6817. plt_offset += this->get_plt_entry_size(),
  6818. got_offset += 4)
  6819. {
  6820. // Set and adjust the PLT entry itself.
  6821. this->fill_plt_entry(pov, got_address, plt_address,
  6822. got_offset, plt_offset);
  6823. Arm_address value;
  6824. if (i < this->count_)
  6825. {
  6826. // For non-irelative got entries, the value is the beginning of plt.
  6827. value = plt_address;
  6828. }
  6829. else
  6830. {
  6831. // For irelative got entries, the value is the (global/local) symbol
  6832. // address.
  6833. const IRelative_data& idata =
  6834. this->irelative_data_vec_[i - this->count_];
  6835. if (idata.symbol_is_global_)
  6836. {
  6837. // Set the entry in the GOT for irelative symbols. The content is
  6838. // the address of the ifunc, not the address of plt start.
  6839. const Sized_symbol<32>* sized_symbol = idata.u_.global;
  6840. gold_assert(sized_symbol->type() == elfcpp::STT_GNU_IFUNC);
  6841. value = sized_symbol->value();
  6842. }
  6843. else
  6844. {
  6845. value = idata.u_.local.relobj->local_symbol_value(
  6846. idata.u_.local.index, 0);
  6847. }
  6848. }
  6849. elfcpp::Swap<32, big_endian>::writeval(got_pov, value);
  6850. }
  6851. gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
  6852. gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
  6853. of->write_output_view(offset, oview_size, oview);
  6854. of->write_output_view(got_file_offset, got_size, got_view);
  6855. }
  6856. // Create a PLT entry for a global symbol.
  6857. template<bool big_endian>
  6858. void
  6859. Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
  6860. Symbol* gsym)
  6861. {
  6862. if (gsym->has_plt_offset())
  6863. return;
  6864. if (this->plt_ == NULL)
  6865. this->make_plt_section(symtab, layout);
  6866. this->plt_->add_entry(symtab, layout, gsym);
  6867. }
  6868. // Create the PLT section.
  6869. template<bool big_endian>
  6870. void
  6871. Target_arm<big_endian>::make_plt_section(
  6872. Symbol_table* symtab, Layout* layout)
  6873. {
  6874. if (this->plt_ == NULL)
  6875. {
  6876. // Create the GOT section first.
  6877. this->got_section(symtab, layout);
  6878. // GOT for irelatives is create along with got.plt.
  6879. gold_assert(this->got_ != NULL
  6880. && this->got_plt_ != NULL
  6881. && this->got_irelative_ != NULL);
  6882. this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
  6883. this->got_irelative_);
  6884. layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
  6885. (elfcpp::SHF_ALLOC
  6886. | elfcpp::SHF_EXECINSTR),
  6887. this->plt_, ORDER_PLT, false);
  6888. symtab->define_in_output_data("$a", NULL,
  6889. Symbol_table::PREDEFINED,
  6890. this->plt_,
  6891. 0, 0, elfcpp::STT_NOTYPE,
  6892. elfcpp::STB_LOCAL,
  6893. elfcpp::STV_DEFAULT, 0,
  6894. false, false);
  6895. }
  6896. }
  6897. // Make a PLT entry for a local STT_GNU_IFUNC symbol.
  6898. template<bool big_endian>
  6899. void
  6900. Target_arm<big_endian>::make_local_ifunc_plt_entry(
  6901. Symbol_table* symtab, Layout* layout,
  6902. Sized_relobj_file<32, big_endian>* relobj,
  6903. unsigned int local_sym_index)
  6904. {
  6905. if (relobj->local_has_plt_offset(local_sym_index))
  6906. return;
  6907. if (this->plt_ == NULL)
  6908. this->make_plt_section(symtab, layout);
  6909. unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
  6910. relobj,
  6911. local_sym_index);
  6912. relobj->set_local_plt_offset(local_sym_index, plt_offset);
  6913. }
  6914. // Return the number of entries in the PLT.
  6915. template<bool big_endian>
  6916. unsigned int
  6917. Target_arm<big_endian>::plt_entry_count() const
  6918. {
  6919. if (this->plt_ == NULL)
  6920. return 0;
  6921. return this->plt_->entry_count();
  6922. }
  6923. // Return the offset of the first non-reserved PLT entry.
  6924. template<bool big_endian>
  6925. unsigned int
  6926. Target_arm<big_endian>::first_plt_entry_offset() const
  6927. {
  6928. return this->plt_->first_plt_entry_offset();
  6929. }
  6930. // Return the size of each PLT entry.
  6931. template<bool big_endian>
  6932. unsigned int
  6933. Target_arm<big_endian>::plt_entry_size() const
  6934. {
  6935. return this->plt_->get_plt_entry_size();
  6936. }
  6937. // Get the section to use for TLS_DESC relocations.
  6938. template<bool big_endian>
  6939. typename Target_arm<big_endian>::Reloc_section*
  6940. Target_arm<big_endian>::rel_tls_desc_section(Layout* layout) const
  6941. {
  6942. return this->plt_section()->rel_tls_desc(layout);
  6943. }
  6944. // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
  6945. template<bool big_endian>
  6946. void
  6947. Target_arm<big_endian>::define_tls_base_symbol(
  6948. Symbol_table* symtab,
  6949. Layout* layout)
  6950. {
  6951. if (this->tls_base_symbol_defined_)
  6952. return;
  6953. Output_segment* tls_segment = layout->tls_segment();
  6954. if (tls_segment != NULL)
  6955. {
  6956. bool is_exec = parameters->options().output_is_executable();
  6957. symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
  6958. Symbol_table::PREDEFINED,
  6959. tls_segment, 0, 0,
  6960. elfcpp::STT_TLS,
  6961. elfcpp::STB_LOCAL,
  6962. elfcpp::STV_HIDDEN, 0,
  6963. (is_exec
  6964. ? Symbol::SEGMENT_END
  6965. : Symbol::SEGMENT_START),
  6966. true);
  6967. }
  6968. this->tls_base_symbol_defined_ = true;
  6969. }
  6970. // Create a GOT entry for the TLS module index.
  6971. template<bool big_endian>
  6972. unsigned int
  6973. Target_arm<big_endian>::got_mod_index_entry(
  6974. Symbol_table* symtab,
  6975. Layout* layout,
  6976. Sized_relobj_file<32, big_endian>* object)
  6977. {
  6978. if (this->got_mod_index_offset_ == -1U)
  6979. {
  6980. gold_assert(symtab != NULL && layout != NULL && object != NULL);
  6981. Arm_output_data_got<big_endian>* got = this->got_section(symtab, layout);
  6982. unsigned int got_offset;
  6983. if (!parameters->doing_static_link())
  6984. {
  6985. got_offset = got->add_constant(0);
  6986. Reloc_section* rel_dyn = this->rel_dyn_section(layout);
  6987. rel_dyn->add_local(object, 0, elfcpp::R_ARM_TLS_DTPMOD32, got,
  6988. got_offset);
  6989. }
  6990. else
  6991. {
  6992. // We are doing a static link. Just mark it as belong to module 1,
  6993. // the executable.
  6994. got_offset = got->add_constant(1);
  6995. }
  6996. got->add_constant(0);
  6997. this->got_mod_index_offset_ = got_offset;
  6998. }
  6999. return this->got_mod_index_offset_;
  7000. }
  7001. // Optimize the TLS relocation type based on what we know about the
  7002. // symbol. IS_FINAL is true if the final address of this symbol is
  7003. // known at link time.
  7004. template<bool big_endian>
  7005. tls::Tls_optimization
  7006. Target_arm<big_endian>::optimize_tls_reloc(bool, int)
  7007. {
  7008. // FIXME: Currently we do not do any TLS optimization.
  7009. return tls::TLSOPT_NONE;
  7010. }
  7011. // Get the Reference_flags for a particular relocation.
  7012. template<bool big_endian>
  7013. int
  7014. Target_arm<big_endian>::Scan::get_reference_flags(unsigned int r_type)
  7015. {
  7016. switch (r_type)
  7017. {
  7018. case elfcpp::R_ARM_NONE:
  7019. case elfcpp::R_ARM_V4BX:
  7020. case elfcpp::R_ARM_GNU_VTENTRY:
  7021. case elfcpp::R_ARM_GNU_VTINHERIT:
  7022. // No symbol reference.
  7023. return 0;
  7024. case elfcpp::R_ARM_ABS32:
  7025. case elfcpp::R_ARM_ABS16:
  7026. case elfcpp::R_ARM_ABS12:
  7027. case elfcpp::R_ARM_THM_ABS5:
  7028. case elfcpp::R_ARM_ABS8:
  7029. case elfcpp::R_ARM_BASE_ABS:
  7030. case elfcpp::R_ARM_MOVW_ABS_NC:
  7031. case elfcpp::R_ARM_MOVT_ABS:
  7032. case elfcpp::R_ARM_THM_MOVW_ABS_NC:
  7033. case elfcpp::R_ARM_THM_MOVT_ABS:
  7034. case elfcpp::R_ARM_ABS32_NOI:
  7035. return Symbol::ABSOLUTE_REF;
  7036. case elfcpp::R_ARM_REL32:
  7037. case elfcpp::R_ARM_LDR_PC_G0:
  7038. case elfcpp::R_ARM_SBREL32:
  7039. case elfcpp::R_ARM_THM_PC8:
  7040. case elfcpp::R_ARM_BASE_PREL:
  7041. case elfcpp::R_ARM_MOVW_PREL_NC:
  7042. case elfcpp::R_ARM_MOVT_PREL:
  7043. case elfcpp::R_ARM_THM_MOVW_PREL_NC:
  7044. case elfcpp::R_ARM_THM_MOVT_PREL:
  7045. case elfcpp::R_ARM_THM_ALU_PREL_11_0:
  7046. case elfcpp::R_ARM_THM_PC12:
  7047. case elfcpp::R_ARM_REL32_NOI:
  7048. case elfcpp::R_ARM_ALU_PC_G0_NC:
  7049. case elfcpp::R_ARM_ALU_PC_G0:
  7050. case elfcpp::R_ARM_ALU_PC_G1_NC:
  7051. case elfcpp::R_ARM_ALU_PC_G1:
  7052. case elfcpp::R_ARM_ALU_PC_G2:
  7053. case elfcpp::R_ARM_LDR_PC_G1:
  7054. case elfcpp::R_ARM_LDR_PC_G2:
  7055. case elfcpp::R_ARM_LDRS_PC_G0:
  7056. case elfcpp::R_ARM_LDRS_PC_G1:
  7057. case elfcpp::R_ARM_LDRS_PC_G2:
  7058. case elfcpp::R_ARM_LDC_PC_G0:
  7059. case elfcpp::R_ARM_LDC_PC_G1:
  7060. case elfcpp::R_ARM_LDC_PC_G2:
  7061. case elfcpp::R_ARM_ALU_SB_G0_NC:
  7062. case elfcpp::R_ARM_ALU_SB_G0:
  7063. case elfcpp::R_ARM_ALU_SB_G1_NC:
  7064. case elfcpp::R_ARM_ALU_SB_G1:
  7065. case elfcpp::R_ARM_ALU_SB_G2:
  7066. case elfcpp::R_ARM_LDR_SB_G0:
  7067. case elfcpp::R_ARM_LDR_SB_G1:
  7068. case elfcpp::R_ARM_LDR_SB_G2:
  7069. case elfcpp::R_ARM_LDRS_SB_G0:
  7070. case elfcpp::R_ARM_LDRS_SB_G1:
  7071. case elfcpp::R_ARM_LDRS_SB_G2:
  7072. case elfcpp::R_ARM_LDC_SB_G0:
  7073. case elfcpp::R_ARM_LDC_SB_G1:
  7074. case elfcpp::R_ARM_LDC_SB_G2:
  7075. case elfcpp::R_ARM_MOVW_BREL_NC:
  7076. case elfcpp::R_ARM_MOVT_BREL:
  7077. case elfcpp::R_ARM_MOVW_BREL:
  7078. case elfcpp::R_ARM_THM_MOVW_BREL_NC:
  7079. case elfcpp::R_ARM_THM_MOVT_BREL:
  7080. case elfcpp::R_ARM_THM_MOVW_BREL:
  7081. case elfcpp::R_ARM_GOTOFF32:
  7082. case elfcpp::R_ARM_GOTOFF12:
  7083. case elfcpp::R_ARM_SBREL31:
  7084. return Symbol::RELATIVE_REF;
  7085. case elfcpp::R_ARM_PLT32:
  7086. case elfcpp::R_ARM_CALL:
  7087. case elfcpp::R_ARM_JUMP24:
  7088. case elfcpp::R_ARM_THM_CALL:
  7089. case elfcpp::R_ARM_THM_JUMP24:
  7090. case elfcpp::R_ARM_THM_JUMP19:
  7091. case elfcpp::R_ARM_THM_JUMP6:
  7092. case elfcpp::R_ARM_THM_JUMP11:
  7093. case elfcpp::R_ARM_THM_JUMP8:
  7094. // R_ARM_PREL31 is not used to relocate call/jump instructions but
  7095. // in unwind tables. It may point to functions via PLTs.
  7096. // So we treat it like call/jump relocations above.
  7097. case elfcpp::R_ARM_PREL31:
  7098. return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
  7099. case elfcpp::R_ARM_GOT_BREL:
  7100. case elfcpp::R_ARM_GOT_ABS:
  7101. case elfcpp::R_ARM_GOT_PREL:
  7102. // Absolute in GOT.
  7103. return Symbol::ABSOLUTE_REF;
  7104. case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
  7105. case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
  7106. case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
  7107. case elfcpp::R_ARM_TLS_IE32: // Initial-exec
  7108. case elfcpp::R_ARM_TLS_LE32: // Local-exec
  7109. return Symbol::TLS_REF;
  7110. case elfcpp::R_ARM_TARGET1:
  7111. case elfcpp::R_ARM_TARGET2:
  7112. case elfcpp::R_ARM_COPY:
  7113. case elfcpp::R_ARM_GLOB_DAT:
  7114. case elfcpp::R_ARM_JUMP_SLOT:
  7115. case elfcpp::R_ARM_RELATIVE:
  7116. case elfcpp::R_ARM_PC24:
  7117. case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
  7118. case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
  7119. case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
  7120. default:
  7121. // Not expected. We will give an error later.
  7122. return 0;
  7123. }
  7124. }
  7125. // Report an unsupported relocation against a local symbol.
  7126. template<bool big_endian>
  7127. void
  7128. Target_arm<big_endian>::Scan::unsupported_reloc_local(
  7129. Sized_relobj_file<32, big_endian>* object,
  7130. unsigned int r_type)
  7131. {
  7132. gold_error(_("%s: unsupported reloc %u against local symbol"),
  7133. object->name().c_str(), r_type);
  7134. }
  7135. // We are about to emit a dynamic relocation of type R_TYPE. If the
  7136. // dynamic linker does not support it, issue an error. The GNU linker
  7137. // only issues a non-PIC error for an allocated read-only section.
  7138. // Here we know the section is allocated, but we don't know that it is
  7139. // read-only. But we check for all the relocation types which the
  7140. // glibc dynamic linker supports, so it seems appropriate to issue an
  7141. // error even if the section is not read-only.
  7142. template<bool big_endian>
  7143. void
  7144. Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
  7145. unsigned int r_type)
  7146. {
  7147. switch (r_type)
  7148. {
  7149. // These are the relocation types supported by glibc for ARM.
  7150. case elfcpp::R_ARM_RELATIVE:
  7151. case elfcpp::R_ARM_COPY:
  7152. case elfcpp::R_ARM_GLOB_DAT:
  7153. case elfcpp::R_ARM_JUMP_SLOT:
  7154. case elfcpp::R_ARM_ABS32:
  7155. case elfcpp::R_ARM_ABS32_NOI:
  7156. case elfcpp::R_ARM_IRELATIVE:
  7157. case elfcpp::R_ARM_PC24:
  7158. // FIXME: The following 3 types are not supported by Android's dynamic
  7159. // linker.
  7160. case elfcpp::R_ARM_TLS_DTPMOD32:
  7161. case elfcpp::R_ARM_TLS_DTPOFF32:
  7162. case elfcpp::R_ARM_TLS_TPOFF32:
  7163. return;
  7164. default:
  7165. {
  7166. // This prevents us from issuing more than one error per reloc
  7167. // section. But we can still wind up issuing more than one
  7168. // error per object file.
  7169. if (this->issued_non_pic_error_)
  7170. return;
  7171. const Arm_reloc_property* reloc_property =
  7172. arm_reloc_property_table->get_reloc_property(r_type);
  7173. gold_assert(reloc_property != NULL);
  7174. object->error(_("requires unsupported dynamic reloc %s; "
  7175. "recompile with -fPIC"),
  7176. reloc_property->name().c_str());
  7177. this->issued_non_pic_error_ = true;
  7178. return;
  7179. }
  7180. case elfcpp::R_ARM_NONE:
  7181. gold_unreachable();
  7182. }
  7183. }
  7184. // Return whether we need to make a PLT entry for a relocation of the
  7185. // given type against a STT_GNU_IFUNC symbol.
  7186. template<bool big_endian>
  7187. bool
  7188. Target_arm<big_endian>::Scan::reloc_needs_plt_for_ifunc(
  7189. Sized_relobj_file<32, big_endian>* object,
  7190. unsigned int r_type)
  7191. {
  7192. int flags = Scan::get_reference_flags(r_type);
  7193. if (flags & Symbol::TLS_REF)
  7194. {
  7195. gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
  7196. object->name().c_str(), r_type);
  7197. return false;
  7198. }
  7199. return flags != 0;
  7200. }
  7201. // Scan a relocation for a local symbol.
  7202. // FIXME: This only handles a subset of relocation types used by Android
  7203. // on ARM v5te devices.
  7204. template<bool big_endian>
  7205. inline void
  7206. Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
  7207. Layout* layout,
  7208. Target_arm* target,
  7209. Sized_relobj_file<32, big_endian>* object,
  7210. unsigned int data_shndx,
  7211. Output_section* output_section,
  7212. const elfcpp::Rel<32, big_endian>& reloc,
  7213. unsigned int r_type,
  7214. const elfcpp::Sym<32, big_endian>& lsym,
  7215. bool is_discarded)
  7216. {
  7217. if (is_discarded)
  7218. return;
  7219. r_type = get_real_reloc_type(r_type);
  7220. // A local STT_GNU_IFUNC symbol may require a PLT entry.
  7221. bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
  7222. if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
  7223. {
  7224. unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  7225. target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
  7226. }
  7227. switch (r_type)
  7228. {
  7229. case elfcpp::R_ARM_NONE:
  7230. case elfcpp::R_ARM_V4BX:
  7231. case elfcpp::R_ARM_GNU_VTENTRY:
  7232. case elfcpp::R_ARM_GNU_VTINHERIT:
  7233. break;
  7234. case elfcpp::R_ARM_ABS32:
  7235. case elfcpp::R_ARM_ABS32_NOI:
  7236. // If building a shared library (or a position-independent
  7237. // executable), we need to create a dynamic relocation for
  7238. // this location. The relocation applied at link time will
  7239. // apply the link-time value, so we flag the location with
  7240. // an R_ARM_RELATIVE relocation so the dynamic loader can
  7241. // relocate it easily.
  7242. if (parameters->options().output_is_position_independent())
  7243. {
  7244. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  7245. unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  7246. // If we are to add more other reloc types than R_ARM_ABS32,
  7247. // we need to add check_non_pic(object, r_type) here.
  7248. rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
  7249. output_section, data_shndx,
  7250. reloc.get_r_offset(), is_ifunc);
  7251. }
  7252. break;
  7253. case elfcpp::R_ARM_ABS16:
  7254. case elfcpp::R_ARM_ABS12:
  7255. case elfcpp::R_ARM_THM_ABS5:
  7256. case elfcpp::R_ARM_ABS8:
  7257. case elfcpp::R_ARM_BASE_ABS:
  7258. case elfcpp::R_ARM_MOVW_ABS_NC:
  7259. case elfcpp::R_ARM_MOVT_ABS:
  7260. case elfcpp::R_ARM_THM_MOVW_ABS_NC:
  7261. case elfcpp::R_ARM_THM_MOVT_ABS:
  7262. // If building a shared library (or a position-independent
  7263. // executable), we need to create a dynamic relocation for
  7264. // this location. Because the addend needs to remain in the
  7265. // data section, we need to be careful not to apply this
  7266. // relocation statically.
  7267. if (parameters->options().output_is_position_independent())
  7268. {
  7269. check_non_pic(object, r_type);
  7270. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  7271. unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  7272. if (lsym.get_st_type() != elfcpp::STT_SECTION)
  7273. rel_dyn->add_local(object, r_sym, r_type, output_section,
  7274. data_shndx, reloc.get_r_offset());
  7275. else
  7276. {
  7277. gold_assert(lsym.get_st_value() == 0);
  7278. unsigned int shndx = lsym.get_st_shndx();
  7279. bool is_ordinary;
  7280. shndx = object->adjust_sym_shndx(r_sym, shndx,
  7281. &is_ordinary);
  7282. if (!is_ordinary)
  7283. object->error(_("section symbol %u has bad shndx %u"),
  7284. r_sym, shndx);
  7285. else
  7286. rel_dyn->add_local_section(object, shndx,
  7287. r_type, output_section,
  7288. data_shndx, reloc.get_r_offset());
  7289. }
  7290. }
  7291. break;
  7292. case elfcpp::R_ARM_REL32:
  7293. case elfcpp::R_ARM_LDR_PC_G0:
  7294. case elfcpp::R_ARM_SBREL32:
  7295. case elfcpp::R_ARM_THM_CALL:
  7296. case elfcpp::R_ARM_THM_PC8:
  7297. case elfcpp::R_ARM_BASE_PREL:
  7298. case elfcpp::R_ARM_PLT32:
  7299. case elfcpp::R_ARM_CALL:
  7300. case elfcpp::R_ARM_JUMP24:
  7301. case elfcpp::R_ARM_THM_JUMP24:
  7302. case elfcpp::R_ARM_SBREL31:
  7303. case elfcpp::R_ARM_PREL31:
  7304. case elfcpp::R_ARM_MOVW_PREL_NC:
  7305. case elfcpp::R_ARM_MOVT_PREL:
  7306. case elfcpp::R_ARM_THM_MOVW_PREL_NC:
  7307. case elfcpp::R_ARM_THM_MOVT_PREL:
  7308. case elfcpp::R_ARM_THM_JUMP19:
  7309. case elfcpp::R_ARM_THM_JUMP6:
  7310. case elfcpp::R_ARM_THM_ALU_PREL_11_0:
  7311. case elfcpp::R_ARM_THM_PC12:
  7312. case elfcpp::R_ARM_REL32_NOI:
  7313. case elfcpp::R_ARM_ALU_PC_G0_NC:
  7314. case elfcpp::R_ARM_ALU_PC_G0:
  7315. case elfcpp::R_ARM_ALU_PC_G1_NC:
  7316. case elfcpp::R_ARM_ALU_PC_G1:
  7317. case elfcpp::R_ARM_ALU_PC_G2:
  7318. case elfcpp::R_ARM_LDR_PC_G1:
  7319. case elfcpp::R_ARM_LDR_PC_G2:
  7320. case elfcpp::R_ARM_LDRS_PC_G0:
  7321. case elfcpp::R_ARM_LDRS_PC_G1:
  7322. case elfcpp::R_ARM_LDRS_PC_G2:
  7323. case elfcpp::R_ARM_LDC_PC_G0:
  7324. case elfcpp::R_ARM_LDC_PC_G1:
  7325. case elfcpp::R_ARM_LDC_PC_G2:
  7326. case elfcpp::R_ARM_ALU_SB_G0_NC:
  7327. case elfcpp::R_ARM_ALU_SB_G0:
  7328. case elfcpp::R_ARM_ALU_SB_G1_NC:
  7329. case elfcpp::R_ARM_ALU_SB_G1:
  7330. case elfcpp::R_ARM_ALU_SB_G2:
  7331. case elfcpp::R_ARM_LDR_SB_G0:
  7332. case elfcpp::R_ARM_LDR_SB_G1:
  7333. case elfcpp::R_ARM_LDR_SB_G2:
  7334. case elfcpp::R_ARM_LDRS_SB_G0:
  7335. case elfcpp::R_ARM_LDRS_SB_G1:
  7336. case elfcpp::R_ARM_LDRS_SB_G2:
  7337. case elfcpp::R_ARM_LDC_SB_G0:
  7338. case elfcpp::R_ARM_LDC_SB_G1:
  7339. case elfcpp::R_ARM_LDC_SB_G2:
  7340. case elfcpp::R_ARM_MOVW_BREL_NC:
  7341. case elfcpp::R_ARM_MOVT_BREL:
  7342. case elfcpp::R_ARM_MOVW_BREL:
  7343. case elfcpp::R_ARM_THM_MOVW_BREL_NC:
  7344. case elfcpp::R_ARM_THM_MOVT_BREL:
  7345. case elfcpp::R_ARM_THM_MOVW_BREL:
  7346. case elfcpp::R_ARM_THM_JUMP11:
  7347. case elfcpp::R_ARM_THM_JUMP8:
  7348. // We don't need to do anything for a relative addressing relocation
  7349. // against a local symbol if it does not reference the GOT.
  7350. break;
  7351. case elfcpp::R_ARM_GOTOFF32:
  7352. case elfcpp::R_ARM_GOTOFF12:
  7353. // We need a GOT section:
  7354. target->got_section(symtab, layout);
  7355. break;
  7356. case elfcpp::R_ARM_GOT_BREL:
  7357. case elfcpp::R_ARM_GOT_PREL:
  7358. {
  7359. // The symbol requires a GOT entry.
  7360. Arm_output_data_got<big_endian>* got =
  7361. target->got_section(symtab, layout);
  7362. unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  7363. if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
  7364. {
  7365. // If we are generating a shared object, we need to add a
  7366. // dynamic RELATIVE relocation for this symbol's GOT entry.
  7367. if (parameters->options().output_is_position_independent())
  7368. {
  7369. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  7370. unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  7371. rel_dyn->add_local_relative(
  7372. object, r_sym, elfcpp::R_ARM_RELATIVE, got,
  7373. object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
  7374. }
  7375. }
  7376. }
  7377. break;
  7378. case elfcpp::R_ARM_TARGET1:
  7379. case elfcpp::R_ARM_TARGET2:
  7380. // This should have been mapped to another type already.
  7381. // Fall through.
  7382. case elfcpp::R_ARM_COPY:
  7383. case elfcpp::R_ARM_GLOB_DAT:
  7384. case elfcpp::R_ARM_JUMP_SLOT:
  7385. case elfcpp::R_ARM_RELATIVE:
  7386. // These are relocations which should only be seen by the
  7387. // dynamic linker, and should never be seen here.
  7388. gold_error(_("%s: unexpected reloc %u in object file"),
  7389. object->name().c_str(), r_type);
  7390. break;
  7391. // These are initial TLS relocs, which are expected when
  7392. // linking.
  7393. case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
  7394. case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
  7395. case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
  7396. case elfcpp::R_ARM_TLS_IE32: // Initial-exec
  7397. case elfcpp::R_ARM_TLS_LE32: // Local-exec
  7398. {
  7399. bool output_is_shared = parameters->options().shared();
  7400. const tls::Tls_optimization optimized_type
  7401. = Target_arm<big_endian>::optimize_tls_reloc(!output_is_shared,
  7402. r_type);
  7403. switch (r_type)
  7404. {
  7405. case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
  7406. if (optimized_type == tls::TLSOPT_NONE)
  7407. {
  7408. // Create a pair of GOT entries for the module index and
  7409. // dtv-relative offset.
  7410. Arm_output_data_got<big_endian>* got
  7411. = target->got_section(symtab, layout);
  7412. unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  7413. unsigned int shndx = lsym.get_st_shndx();
  7414. bool is_ordinary;
  7415. shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  7416. if (!is_ordinary)
  7417. {
  7418. object->error(_("local symbol %u has bad shndx %u"),
  7419. r_sym, shndx);
  7420. break;
  7421. }
  7422. if (!parameters->doing_static_link())
  7423. got->add_local_pair_with_rel(object, r_sym, shndx,
  7424. GOT_TYPE_TLS_PAIR,
  7425. target->rel_dyn_section(layout),
  7426. elfcpp::R_ARM_TLS_DTPMOD32);
  7427. else
  7428. got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR,
  7429. object, r_sym);
  7430. }
  7431. else
  7432. // FIXME: TLS optimization not supported yet.
  7433. gold_unreachable();
  7434. break;
  7435. case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
  7436. if (optimized_type == tls::TLSOPT_NONE)
  7437. {
  7438. // Create a GOT entry for the module index.
  7439. target->got_mod_index_entry(symtab, layout, object);
  7440. }
  7441. else
  7442. // FIXME: TLS optimization not supported yet.
  7443. gold_unreachable();
  7444. break;
  7445. case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
  7446. break;
  7447. case elfcpp::R_ARM_TLS_IE32: // Initial-exec
  7448. layout->set_has_static_tls();
  7449. if (optimized_type == tls::TLSOPT_NONE)
  7450. {
  7451. // Create a GOT entry for the tp-relative offset.
  7452. Arm_output_data_got<big_endian>* got
  7453. = target->got_section(symtab, layout);
  7454. unsigned int r_sym =
  7455. elfcpp::elf_r_sym<32>(reloc.get_r_info());
  7456. if (!parameters->doing_static_link())
  7457. got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
  7458. target->rel_dyn_section(layout),
  7459. elfcpp::R_ARM_TLS_TPOFF32);
  7460. else if (!object->local_has_got_offset(r_sym,
  7461. GOT_TYPE_TLS_OFFSET))
  7462. {
  7463. got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
  7464. unsigned int got_offset =
  7465. object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
  7466. got->add_static_reloc(got_offset,
  7467. elfcpp::R_ARM_TLS_TPOFF32, object,
  7468. r_sym);
  7469. }
  7470. }
  7471. else
  7472. // FIXME: TLS optimization not supported yet.
  7473. gold_unreachable();
  7474. break;
  7475. case elfcpp::R_ARM_TLS_LE32: // Local-exec
  7476. layout->set_has_static_tls();
  7477. if (output_is_shared)
  7478. {
  7479. // We need to create a dynamic relocation.
  7480. gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
  7481. unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  7482. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  7483. rel_dyn->add_local(object, r_sym, elfcpp::R_ARM_TLS_TPOFF32,
  7484. output_section, data_shndx,
  7485. reloc.get_r_offset());
  7486. }
  7487. break;
  7488. default:
  7489. gold_unreachable();
  7490. }
  7491. }
  7492. break;
  7493. case elfcpp::R_ARM_PC24:
  7494. case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
  7495. case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
  7496. case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
  7497. default:
  7498. unsupported_reloc_local(object, r_type);
  7499. break;
  7500. }
  7501. }
  7502. // Report an unsupported relocation against a global symbol.
  7503. template<bool big_endian>
  7504. void
  7505. Target_arm<big_endian>::Scan::unsupported_reloc_global(
  7506. Sized_relobj_file<32, big_endian>* object,
  7507. unsigned int r_type,
  7508. Symbol* gsym)
  7509. {
  7510. gold_error(_("%s: unsupported reloc %u against global symbol %s"),
  7511. object->name().c_str(), r_type, gsym->demangled_name().c_str());
  7512. }
  7513. template<bool big_endian>
  7514. inline bool
  7515. Target_arm<big_endian>::Scan::possible_function_pointer_reloc(
  7516. unsigned int r_type)
  7517. {
  7518. switch (r_type)
  7519. {
  7520. case elfcpp::R_ARM_PC24:
  7521. case elfcpp::R_ARM_THM_CALL:
  7522. case elfcpp::R_ARM_PLT32:
  7523. case elfcpp::R_ARM_CALL:
  7524. case elfcpp::R_ARM_JUMP24:
  7525. case elfcpp::R_ARM_THM_JUMP24:
  7526. case elfcpp::R_ARM_SBREL31:
  7527. case elfcpp::R_ARM_PREL31:
  7528. case elfcpp::R_ARM_THM_JUMP19:
  7529. case elfcpp::R_ARM_THM_JUMP6:
  7530. case elfcpp::R_ARM_THM_JUMP11:
  7531. case elfcpp::R_ARM_THM_JUMP8:
  7532. // All the relocations above are branches except SBREL31 and PREL31.
  7533. return false;
  7534. default:
  7535. // Be conservative and assume this is a function pointer.
  7536. return true;
  7537. }
  7538. }
  7539. template<bool big_endian>
  7540. inline bool
  7541. Target_arm<big_endian>::Scan::local_reloc_may_be_function_pointer(
  7542. Symbol_table*,
  7543. Layout*,
  7544. Target_arm<big_endian>* target,
  7545. Sized_relobj_file<32, big_endian>*,
  7546. unsigned int,
  7547. Output_section*,
  7548. const elfcpp::Rel<32, big_endian>&,
  7549. unsigned int r_type,
  7550. const elfcpp::Sym<32, big_endian>&)
  7551. {
  7552. r_type = target->get_real_reloc_type(r_type);
  7553. return possible_function_pointer_reloc(r_type);
  7554. }
  7555. template<bool big_endian>
  7556. inline bool
  7557. Target_arm<big_endian>::Scan::global_reloc_may_be_function_pointer(
  7558. Symbol_table*,
  7559. Layout*,
  7560. Target_arm<big_endian>* target,
  7561. Sized_relobj_file<32, big_endian>*,
  7562. unsigned int,
  7563. Output_section*,
  7564. const elfcpp::Rel<32, big_endian>&,
  7565. unsigned int r_type,
  7566. Symbol* gsym)
  7567. {
  7568. // GOT is not a function.
  7569. if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
  7570. return false;
  7571. r_type = target->get_real_reloc_type(r_type);
  7572. return possible_function_pointer_reloc(r_type);
  7573. }
  7574. // Scan a relocation for a global symbol.
  7575. template<bool big_endian>
  7576. inline void
  7577. Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
  7578. Layout* layout,
  7579. Target_arm* target,
  7580. Sized_relobj_file<32, big_endian>* object,
  7581. unsigned int data_shndx,
  7582. Output_section* output_section,
  7583. const elfcpp::Rel<32, big_endian>& reloc,
  7584. unsigned int r_type,
  7585. Symbol* gsym)
  7586. {
  7587. // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
  7588. // section. We check here to avoid creating a dynamic reloc against
  7589. // _GLOBAL_OFFSET_TABLE_.
  7590. if (!target->has_got_section()
  7591. && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
  7592. target->got_section(symtab, layout);
  7593. // A STT_GNU_IFUNC symbol may require a PLT entry.
  7594. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  7595. && this->reloc_needs_plt_for_ifunc(object, r_type))
  7596. target->make_plt_entry(symtab, layout, gsym);
  7597. r_type = get_real_reloc_type(r_type);
  7598. switch (r_type)
  7599. {
  7600. case elfcpp::R_ARM_NONE:
  7601. case elfcpp::R_ARM_V4BX:
  7602. case elfcpp::R_ARM_GNU_VTENTRY:
  7603. case elfcpp::R_ARM_GNU_VTINHERIT:
  7604. break;
  7605. case elfcpp::R_ARM_ABS32:
  7606. case elfcpp::R_ARM_ABS16:
  7607. case elfcpp::R_ARM_ABS12:
  7608. case elfcpp::R_ARM_THM_ABS5:
  7609. case elfcpp::R_ARM_ABS8:
  7610. case elfcpp::R_ARM_BASE_ABS:
  7611. case elfcpp::R_ARM_MOVW_ABS_NC:
  7612. case elfcpp::R_ARM_MOVT_ABS:
  7613. case elfcpp::R_ARM_THM_MOVW_ABS_NC:
  7614. case elfcpp::R_ARM_THM_MOVT_ABS:
  7615. case elfcpp::R_ARM_ABS32_NOI:
  7616. // Absolute addressing relocations.
  7617. {
  7618. // Make a PLT entry if necessary.
  7619. if (this->symbol_needs_plt_entry(gsym))
  7620. {
  7621. target->make_plt_entry(symtab, layout, gsym);
  7622. // Since this is not a PC-relative relocation, we may be
  7623. // taking the address of a function. In that case we need to
  7624. // set the entry in the dynamic symbol table to the address of
  7625. // the PLT entry.
  7626. if (gsym->is_from_dynobj() && !parameters->options().shared())
  7627. gsym->set_needs_dynsym_value();
  7628. }
  7629. // Make a dynamic relocation if necessary.
  7630. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
  7631. {
  7632. if (!parameters->options().output_is_position_independent()
  7633. && gsym->may_need_copy_reloc())
  7634. {
  7635. target->copy_reloc(symtab, layout, object,
  7636. data_shndx, output_section, gsym, reloc);
  7637. }
  7638. else if ((r_type == elfcpp::R_ARM_ABS32
  7639. || r_type == elfcpp::R_ARM_ABS32_NOI)
  7640. && gsym->type() == elfcpp::STT_GNU_IFUNC
  7641. && gsym->can_use_relative_reloc(false)
  7642. && !gsym->is_from_dynobj()
  7643. && !gsym->is_undefined()
  7644. && !gsym->is_preemptible())
  7645. {
  7646. // Use an IRELATIVE reloc for a locally defined STT_GNU_IFUNC
  7647. // symbol. This makes a function address in a PIE executable
  7648. // match the address in a shared library that it links against.
  7649. Reloc_section* rel_irelative =
  7650. target->rel_irelative_section(layout);
  7651. unsigned int r_type = elfcpp::R_ARM_IRELATIVE;
  7652. rel_irelative->add_symbolless_global_addend(
  7653. gsym, r_type, output_section, object,
  7654. data_shndx, reloc.get_r_offset());
  7655. }
  7656. else if ((r_type == elfcpp::R_ARM_ABS32
  7657. || r_type == elfcpp::R_ARM_ABS32_NOI)
  7658. && gsym->can_use_relative_reloc(false))
  7659. {
  7660. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  7661. rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
  7662. output_section, object,
  7663. data_shndx, reloc.get_r_offset());
  7664. }
  7665. else
  7666. {
  7667. check_non_pic(object, r_type);
  7668. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  7669. rel_dyn->add_global(gsym, r_type, output_section, object,
  7670. data_shndx, reloc.get_r_offset());
  7671. }
  7672. }
  7673. }
  7674. break;
  7675. case elfcpp::R_ARM_GOTOFF32:
  7676. case elfcpp::R_ARM_GOTOFF12:
  7677. // We need a GOT section.
  7678. target->got_section(symtab, layout);
  7679. break;
  7680. case elfcpp::R_ARM_REL32:
  7681. case elfcpp::R_ARM_LDR_PC_G0:
  7682. case elfcpp::R_ARM_SBREL32:
  7683. case elfcpp::R_ARM_THM_PC8:
  7684. case elfcpp::R_ARM_BASE_PREL:
  7685. case elfcpp::R_ARM_MOVW_PREL_NC:
  7686. case elfcpp::R_ARM_MOVT_PREL:
  7687. case elfcpp::R_ARM_THM_MOVW_PREL_NC:
  7688. case elfcpp::R_ARM_THM_MOVT_PREL:
  7689. case elfcpp::R_ARM_THM_ALU_PREL_11_0:
  7690. case elfcpp::R_ARM_THM_PC12:
  7691. case elfcpp::R_ARM_REL32_NOI:
  7692. case elfcpp::R_ARM_ALU_PC_G0_NC:
  7693. case elfcpp::R_ARM_ALU_PC_G0:
  7694. case elfcpp::R_ARM_ALU_PC_G1_NC:
  7695. case elfcpp::R_ARM_ALU_PC_G1:
  7696. case elfcpp::R_ARM_ALU_PC_G2:
  7697. case elfcpp::R_ARM_LDR_PC_G1:
  7698. case elfcpp::R_ARM_LDR_PC_G2:
  7699. case elfcpp::R_ARM_LDRS_PC_G0:
  7700. case elfcpp::R_ARM_LDRS_PC_G1:
  7701. case elfcpp::R_ARM_LDRS_PC_G2:
  7702. case elfcpp::R_ARM_LDC_PC_G0:
  7703. case elfcpp::R_ARM_LDC_PC_G1:
  7704. case elfcpp::R_ARM_LDC_PC_G2:
  7705. case elfcpp::R_ARM_ALU_SB_G0_NC:
  7706. case elfcpp::R_ARM_ALU_SB_G0:
  7707. case elfcpp::R_ARM_ALU_SB_G1_NC:
  7708. case elfcpp::R_ARM_ALU_SB_G1:
  7709. case elfcpp::R_ARM_ALU_SB_G2:
  7710. case elfcpp::R_ARM_LDR_SB_G0:
  7711. case elfcpp::R_ARM_LDR_SB_G1:
  7712. case elfcpp::R_ARM_LDR_SB_G2:
  7713. case elfcpp::R_ARM_LDRS_SB_G0:
  7714. case elfcpp::R_ARM_LDRS_SB_G1:
  7715. case elfcpp::R_ARM_LDRS_SB_G2:
  7716. case elfcpp::R_ARM_LDC_SB_G0:
  7717. case elfcpp::R_ARM_LDC_SB_G1:
  7718. case elfcpp::R_ARM_LDC_SB_G2:
  7719. case elfcpp::R_ARM_MOVW_BREL_NC:
  7720. case elfcpp::R_ARM_MOVT_BREL:
  7721. case elfcpp::R_ARM_MOVW_BREL:
  7722. case elfcpp::R_ARM_THM_MOVW_BREL_NC:
  7723. case elfcpp::R_ARM_THM_MOVT_BREL:
  7724. case elfcpp::R_ARM_THM_MOVW_BREL:
  7725. // Relative addressing relocations.
  7726. {
  7727. // Make a dynamic relocation if necessary.
  7728. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
  7729. {
  7730. if (parameters->options().output_is_executable()
  7731. && target->may_need_copy_reloc(gsym))
  7732. {
  7733. target->copy_reloc(symtab, layout, object,
  7734. data_shndx, output_section, gsym, reloc);
  7735. }
  7736. else
  7737. {
  7738. check_non_pic(object, r_type);
  7739. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  7740. rel_dyn->add_global(gsym, r_type, output_section, object,
  7741. data_shndx, reloc.get_r_offset());
  7742. }
  7743. }
  7744. }
  7745. break;
  7746. case elfcpp::R_ARM_THM_CALL:
  7747. case elfcpp::R_ARM_PLT32:
  7748. case elfcpp::R_ARM_CALL:
  7749. case elfcpp::R_ARM_JUMP24:
  7750. case elfcpp::R_ARM_THM_JUMP24:
  7751. case elfcpp::R_ARM_SBREL31:
  7752. case elfcpp::R_ARM_PREL31:
  7753. case elfcpp::R_ARM_THM_JUMP19:
  7754. case elfcpp::R_ARM_THM_JUMP6:
  7755. case elfcpp::R_ARM_THM_JUMP11:
  7756. case elfcpp::R_ARM_THM_JUMP8:
  7757. // All the relocation above are branches except for the PREL31 ones.
  7758. // A PREL31 relocation can point to a personality function in a shared
  7759. // library. In that case we want to use a PLT because we want to
  7760. // call the personality routine and the dynamic linkers we care about
  7761. // do not support dynamic PREL31 relocations. An REL31 relocation may
  7762. // point to a function whose unwinding behaviour is being described but
  7763. // we will not mistakenly generate a PLT for that because we should use
  7764. // a local section symbol.
  7765. // If the symbol is fully resolved, this is just a relative
  7766. // local reloc. Otherwise we need a PLT entry.
  7767. if (gsym->final_value_is_known())
  7768. break;
  7769. // If building a shared library, we can also skip the PLT entry
  7770. // if the symbol is defined in the output file and is protected
  7771. // or hidden.
  7772. if (gsym->is_defined()
  7773. && !gsym->is_from_dynobj()
  7774. && !gsym->is_preemptible())
  7775. break;
  7776. target->make_plt_entry(symtab, layout, gsym);
  7777. break;
  7778. case elfcpp::R_ARM_GOT_BREL:
  7779. case elfcpp::R_ARM_GOT_ABS:
  7780. case elfcpp::R_ARM_GOT_PREL:
  7781. {
  7782. // The symbol requires a GOT entry.
  7783. Arm_output_data_got<big_endian>* got =
  7784. target->got_section(symtab, layout);
  7785. if (gsym->final_value_is_known())
  7786. {
  7787. // For a STT_GNU_IFUNC symbol we want the PLT address.
  7788. if (gsym->type() == elfcpp::STT_GNU_IFUNC)
  7789. got->add_global_plt(gsym, GOT_TYPE_STANDARD);
  7790. else
  7791. got->add_global(gsym, GOT_TYPE_STANDARD);
  7792. }
  7793. else
  7794. {
  7795. // If this symbol is not fully resolved, we need to add a
  7796. // GOT entry with a dynamic relocation.
  7797. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  7798. if (gsym->is_from_dynobj()
  7799. || gsym->is_undefined()
  7800. || gsym->is_preemptible()
  7801. || (gsym->visibility() == elfcpp::STV_PROTECTED
  7802. && parameters->options().shared())
  7803. || (gsym->type() == elfcpp::STT_GNU_IFUNC
  7804. && parameters->options().output_is_position_independent()))
  7805. got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
  7806. rel_dyn, elfcpp::R_ARM_GLOB_DAT);
  7807. else
  7808. {
  7809. // For a STT_GNU_IFUNC symbol we want to write the PLT
  7810. // offset into the GOT, so that function pointer
  7811. // comparisons work correctly.
  7812. bool is_new;
  7813. if (gsym->type() != elfcpp::STT_GNU_IFUNC)
  7814. is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
  7815. else
  7816. {
  7817. is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
  7818. // Tell the dynamic linker to use the PLT address
  7819. // when resolving relocations.
  7820. if (gsym->is_from_dynobj()
  7821. && !parameters->options().shared())
  7822. gsym->set_needs_dynsym_value();
  7823. }
  7824. if (is_new)
  7825. rel_dyn->add_global_relative(
  7826. gsym, elfcpp::R_ARM_RELATIVE, got,
  7827. gsym->got_offset(GOT_TYPE_STANDARD));
  7828. }
  7829. }
  7830. }
  7831. break;
  7832. case elfcpp::R_ARM_TARGET1:
  7833. case elfcpp::R_ARM_TARGET2:
  7834. // These should have been mapped to other types already.
  7835. // Fall through.
  7836. case elfcpp::R_ARM_COPY:
  7837. case elfcpp::R_ARM_GLOB_DAT:
  7838. case elfcpp::R_ARM_JUMP_SLOT:
  7839. case elfcpp::R_ARM_RELATIVE:
  7840. // These are relocations which should only be seen by the
  7841. // dynamic linker, and should never be seen here.
  7842. gold_error(_("%s: unexpected reloc %u in object file"),
  7843. object->name().c_str(), r_type);
  7844. break;
  7845. // These are initial tls relocs, which are expected when
  7846. // linking.
  7847. case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
  7848. case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
  7849. case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
  7850. case elfcpp::R_ARM_TLS_IE32: // Initial-exec
  7851. case elfcpp::R_ARM_TLS_LE32: // Local-exec
  7852. {
  7853. const bool is_final = gsym->final_value_is_known();
  7854. const tls::Tls_optimization optimized_type
  7855. = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
  7856. switch (r_type)
  7857. {
  7858. case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
  7859. if (optimized_type == tls::TLSOPT_NONE)
  7860. {
  7861. // Create a pair of GOT entries for the module index and
  7862. // dtv-relative offset.
  7863. Arm_output_data_got<big_endian>* got
  7864. = target->got_section(symtab, layout);
  7865. if (!parameters->doing_static_link())
  7866. got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
  7867. target->rel_dyn_section(layout),
  7868. elfcpp::R_ARM_TLS_DTPMOD32,
  7869. elfcpp::R_ARM_TLS_DTPOFF32);
  7870. else
  7871. got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR, gsym);
  7872. }
  7873. else
  7874. // FIXME: TLS optimization not supported yet.
  7875. gold_unreachable();
  7876. break;
  7877. case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
  7878. if (optimized_type == tls::TLSOPT_NONE)
  7879. {
  7880. // Create a GOT entry for the module index.
  7881. target->got_mod_index_entry(symtab, layout, object);
  7882. }
  7883. else
  7884. // FIXME: TLS optimization not supported yet.
  7885. gold_unreachable();
  7886. break;
  7887. case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
  7888. break;
  7889. case elfcpp::R_ARM_TLS_IE32: // Initial-exec
  7890. layout->set_has_static_tls();
  7891. if (optimized_type == tls::TLSOPT_NONE)
  7892. {
  7893. // Create a GOT entry for the tp-relative offset.
  7894. Arm_output_data_got<big_endian>* got
  7895. = target->got_section(symtab, layout);
  7896. if (!parameters->doing_static_link())
  7897. got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
  7898. target->rel_dyn_section(layout),
  7899. elfcpp::R_ARM_TLS_TPOFF32);
  7900. else if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
  7901. {
  7902. got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
  7903. unsigned int got_offset =
  7904. gsym->got_offset(GOT_TYPE_TLS_OFFSET);
  7905. got->add_static_reloc(got_offset,
  7906. elfcpp::R_ARM_TLS_TPOFF32, gsym);
  7907. }
  7908. }
  7909. else
  7910. // FIXME: TLS optimization not supported yet.
  7911. gold_unreachable();
  7912. break;
  7913. case elfcpp::R_ARM_TLS_LE32: // Local-exec
  7914. layout->set_has_static_tls();
  7915. if (parameters->options().shared())
  7916. {
  7917. // We need to create a dynamic relocation.
  7918. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  7919. rel_dyn->add_global(gsym, elfcpp::R_ARM_TLS_TPOFF32,
  7920. output_section, object,
  7921. data_shndx, reloc.get_r_offset());
  7922. }
  7923. break;
  7924. default:
  7925. gold_unreachable();
  7926. }
  7927. }
  7928. break;
  7929. case elfcpp::R_ARM_PC24:
  7930. case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
  7931. case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
  7932. case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
  7933. default:
  7934. unsupported_reloc_global(object, r_type, gsym);
  7935. break;
  7936. }
  7937. }
  7938. // Process relocations for gc.
  7939. template<bool big_endian>
  7940. void
  7941. Target_arm<big_endian>::gc_process_relocs(
  7942. Symbol_table* symtab,
  7943. Layout* layout,
  7944. Sized_relobj_file<32, big_endian>* object,
  7945. unsigned int data_shndx,
  7946. unsigned int,
  7947. const unsigned char* prelocs,
  7948. size_t reloc_count,
  7949. Output_section* output_section,
  7950. bool needs_special_offset_handling,
  7951. size_t local_symbol_count,
  7952. const unsigned char* plocal_symbols)
  7953. {
  7954. typedef Target_arm<big_endian> Arm;
  7955. typedef typename Target_arm<big_endian>::Scan Scan;
  7956. gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan,
  7957. typename Target_arm::Relocatable_size_for_reloc>(
  7958. symtab,
  7959. layout,
  7960. this,
  7961. object,
  7962. data_shndx,
  7963. prelocs,
  7964. reloc_count,
  7965. output_section,
  7966. needs_special_offset_handling,
  7967. local_symbol_count,
  7968. plocal_symbols);
  7969. }
  7970. // Scan relocations for a section.
  7971. template<bool big_endian>
  7972. void
  7973. Target_arm<big_endian>::scan_relocs(Symbol_table* symtab,
  7974. Layout* layout,
  7975. Sized_relobj_file<32, big_endian>* object,
  7976. unsigned int data_shndx,
  7977. unsigned int sh_type,
  7978. const unsigned char* prelocs,
  7979. size_t reloc_count,
  7980. Output_section* output_section,
  7981. bool needs_special_offset_handling,
  7982. size_t local_symbol_count,
  7983. const unsigned char* plocal_symbols)
  7984. {
  7985. typedef typename Target_arm<big_endian>::Scan Scan;
  7986. if (sh_type == elfcpp::SHT_RELA)
  7987. {
  7988. gold_error(_("%s: unsupported RELA reloc section"),
  7989. object->name().c_str());
  7990. return;
  7991. }
  7992. gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
  7993. symtab,
  7994. layout,
  7995. this,
  7996. object,
  7997. data_shndx,
  7998. prelocs,
  7999. reloc_count,
  8000. output_section,
  8001. needs_special_offset_handling,
  8002. local_symbol_count,
  8003. plocal_symbols);
  8004. }
  8005. // Finalize the sections.
  8006. template<bool big_endian>
  8007. void
  8008. Target_arm<big_endian>::do_finalize_sections(
  8009. Layout* layout,
  8010. const Input_objects* input_objects,
  8011. Symbol_table*)
  8012. {
  8013. bool merged_any_attributes = false;
  8014. // Merge processor-specific flags.
  8015. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  8016. p != input_objects->relobj_end();
  8017. ++p)
  8018. {
  8019. Arm_relobj<big_endian>* arm_relobj =
  8020. Arm_relobj<big_endian>::as_arm_relobj(*p);
  8021. if (arm_relobj->merge_flags_and_attributes())
  8022. {
  8023. this->merge_processor_specific_flags(
  8024. arm_relobj->name(),
  8025. arm_relobj->processor_specific_flags());
  8026. this->merge_object_attributes(arm_relobj->name().c_str(),
  8027. arm_relobj->attributes_section_data());
  8028. merged_any_attributes = true;
  8029. }
  8030. }
  8031. for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
  8032. p != input_objects->dynobj_end();
  8033. ++p)
  8034. {
  8035. Arm_dynobj<big_endian>* arm_dynobj =
  8036. Arm_dynobj<big_endian>::as_arm_dynobj(*p);
  8037. this->merge_processor_specific_flags(
  8038. arm_dynobj->name(),
  8039. arm_dynobj->processor_specific_flags());
  8040. this->merge_object_attributes(arm_dynobj->name().c_str(),
  8041. arm_dynobj->attributes_section_data());
  8042. merged_any_attributes = true;
  8043. }
  8044. // Create an empty uninitialized attribute section if we still don't have it
  8045. // at this moment. This happens if there is no attributes sections in all
  8046. // inputs.
  8047. if (this->attributes_section_data_ == NULL)
  8048. this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
  8049. const Object_attribute* cpu_arch_attr =
  8050. this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
  8051. // Check if we need to use Cortex-A8 workaround.
  8052. if (parameters->options().user_set_fix_cortex_a8())
  8053. this->fix_cortex_a8_ = parameters->options().fix_cortex_a8();
  8054. else
  8055. {
  8056. // If neither --fix-cortex-a8 nor --no-fix-cortex-a8 is used, turn on
  8057. // Cortex-A8 erratum workaround for ARMv7-A or ARMv7 with unknown
  8058. // profile.
  8059. const Object_attribute* cpu_arch_profile_attr =
  8060. this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile);
  8061. this->fix_cortex_a8_ =
  8062. (cpu_arch_attr->int_value() == elfcpp::TAG_CPU_ARCH_V7
  8063. && (cpu_arch_profile_attr->int_value() == 'A'
  8064. || cpu_arch_profile_attr->int_value() == 0));
  8065. }
  8066. // Check if we can use V4BX interworking.
  8067. // The V4BX interworking stub contains BX instruction,
  8068. // which is not specified for some profiles.
  8069. if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
  8070. && !this->may_use_v4t_interworking())
  8071. gold_error(_("unable to provide V4BX reloc interworking fix up; "
  8072. "the target profile does not support BX instruction"));
  8073. // Fill in some more dynamic tags.
  8074. const Reloc_section* rel_plt = (this->plt_ == NULL
  8075. ? NULL
  8076. : this->plt_->rel_plt());
  8077. layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
  8078. this->rel_dyn_, true, false);
  8079. // Emit any relocs we saved in an attempt to avoid generating COPY
  8080. // relocs.
  8081. if (this->copy_relocs_.any_saved_relocs())
  8082. this->copy_relocs_.emit(this->rel_dyn_section(layout));
  8083. // Handle the .ARM.exidx section.
  8084. Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
  8085. if (!parameters->options().relocatable())
  8086. {
  8087. if (exidx_section != NULL
  8088. && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
  8089. {
  8090. // For the ARM target, we need to add a PT_ARM_EXIDX segment for
  8091. // the .ARM.exidx section.
  8092. if (!layout->script_options()->saw_phdrs_clause())
  8093. {
  8094. gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0,
  8095. 0)
  8096. == NULL);
  8097. Output_segment* exidx_segment =
  8098. layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
  8099. exidx_segment->add_output_section_to_nonload(exidx_section,
  8100. elfcpp::PF_R);
  8101. }
  8102. }
  8103. }
  8104. // Create an .ARM.attributes section if we have merged any attributes
  8105. // from inputs.
  8106. if (merged_any_attributes)
  8107. {
  8108. Output_attributes_section_data* attributes_section =
  8109. new Output_attributes_section_data(*this->attributes_section_data_);
  8110. layout->add_output_section_data(".ARM.attributes",
  8111. elfcpp::SHT_ARM_ATTRIBUTES, 0,
  8112. attributes_section, ORDER_INVALID,
  8113. false);
  8114. }
  8115. // Fix up links in section EXIDX headers.
  8116. for (Layout::Section_list::const_iterator p = layout->section_list().begin();
  8117. p != layout->section_list().end();
  8118. ++p)
  8119. if ((*p)->type() == elfcpp::SHT_ARM_EXIDX)
  8120. {
  8121. Arm_output_section<big_endian>* os =
  8122. Arm_output_section<big_endian>::as_arm_output_section(*p);
  8123. os->set_exidx_section_link();
  8124. }
  8125. }
  8126. // Return whether a direct absolute static relocation needs to be applied.
  8127. // In cases where Scan::local() or Scan::global() has created
  8128. // a dynamic relocation other than R_ARM_RELATIVE, the addend
  8129. // of the relocation is carried in the data, and we must not
  8130. // apply the static relocation.
  8131. template<bool big_endian>
  8132. inline bool
  8133. Target_arm<big_endian>::Relocate::should_apply_static_reloc(
  8134. const Sized_symbol<32>* gsym,
  8135. unsigned int r_type,
  8136. bool is_32bit,
  8137. Output_section* output_section)
  8138. {
  8139. // If the output section is not allocated, then we didn't call
  8140. // scan_relocs, we didn't create a dynamic reloc, and we must apply
  8141. // the reloc here.
  8142. if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
  8143. return true;
  8144. int ref_flags = Scan::get_reference_flags(r_type);
  8145. // For local symbols, we will have created a non-RELATIVE dynamic
  8146. // relocation only if (a) the output is position independent,
  8147. // (b) the relocation is absolute (not pc- or segment-relative), and
  8148. // (c) the relocation is not 32 bits wide.
  8149. if (gsym == NULL)
  8150. return !(parameters->options().output_is_position_independent()
  8151. && (ref_flags & Symbol::ABSOLUTE_REF)
  8152. && !is_32bit);
  8153. // For global symbols, we use the same helper routines used in the
  8154. // scan pass. If we did not create a dynamic relocation, or if we
  8155. // created a RELATIVE dynamic relocation, we should apply the static
  8156. // relocation.
  8157. bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
  8158. bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
  8159. && gsym->can_use_relative_reloc(ref_flags
  8160. & Symbol::FUNCTION_CALL);
  8161. return !has_dyn || is_rel;
  8162. }
  8163. // Perform a relocation.
  8164. template<bool big_endian>
  8165. inline bool
  8166. Target_arm<big_endian>::Relocate::relocate(
  8167. const Relocate_info<32, big_endian>* relinfo,
  8168. Target_arm* target,
  8169. Output_section* output_section,
  8170. size_t relnum,
  8171. const elfcpp::Rel<32, big_endian>& rel,
  8172. unsigned int r_type,
  8173. const Sized_symbol<32>* gsym,
  8174. const Symbol_value<32>* psymval,
  8175. unsigned char* view,
  8176. Arm_address address,
  8177. section_size_type view_size)
  8178. {
  8179. if (view == NULL)
  8180. return true;
  8181. typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
  8182. r_type = get_real_reloc_type(r_type);
  8183. const Arm_reloc_property* reloc_property =
  8184. arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
  8185. if (reloc_property == NULL)
  8186. {
  8187. std::string reloc_name =
  8188. arm_reloc_property_table->reloc_name_in_error_message(r_type);
  8189. gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
  8190. _("cannot relocate %s in object file"),
  8191. reloc_name.c_str());
  8192. return true;
  8193. }
  8194. const Arm_relobj<big_endian>* object =
  8195. Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
  8196. // If the final branch target of a relocation is THUMB instruction, this
  8197. // is 1. Otherwise it is 0.
  8198. Arm_address thumb_bit = 0;
  8199. Symbol_value<32> symval;
  8200. bool is_weakly_undefined_without_plt = false;
  8201. bool have_got_offset = false;
  8202. unsigned int got_offset = 0;
  8203. // If the relocation uses the GOT entry of a symbol instead of the symbol
  8204. // itself, we don't care about whether the symbol is defined or what kind
  8205. // of symbol it is.
  8206. if (reloc_property->uses_got_entry())
  8207. {
  8208. // Get the GOT offset.
  8209. // The GOT pointer points to the end of the GOT section.
  8210. // We need to subtract the size of the GOT section to get
  8211. // the actual offset to use in the relocation.
  8212. // TODO: We should move GOT offset computing code in TLS relocations
  8213. // to here.
  8214. switch (r_type)
  8215. {
  8216. case elfcpp::R_ARM_GOT_BREL:
  8217. case elfcpp::R_ARM_GOT_PREL:
  8218. if (gsym != NULL)
  8219. {
  8220. gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
  8221. got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
  8222. - target->got_size());
  8223. }
  8224. else
  8225. {
  8226. unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
  8227. gold_assert(object->local_has_got_offset(r_sym,
  8228. GOT_TYPE_STANDARD));
  8229. got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
  8230. - target->got_size());
  8231. }
  8232. have_got_offset = true;
  8233. break;
  8234. default:
  8235. break;
  8236. }
  8237. }
  8238. else if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
  8239. {
  8240. if (gsym != NULL)
  8241. {
  8242. // This is a global symbol. Determine if we use PLT and if the
  8243. // final target is THUMB.
  8244. if (gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
  8245. {
  8246. // This uses a PLT, change the symbol value.
  8247. symval.set_output_value(target->plt_address_for_global(gsym));
  8248. psymval = &symval;
  8249. }
  8250. else if (gsym->is_weak_undefined())
  8251. {
  8252. // This is a weakly undefined symbol and we do not use PLT
  8253. // for this relocation. A branch targeting this symbol will
  8254. // be converted into an NOP.
  8255. is_weakly_undefined_without_plt = true;
  8256. }
  8257. else if (gsym->is_undefined() && reloc_property->uses_symbol())
  8258. {
  8259. // This relocation uses the symbol value but the symbol is
  8260. // undefined. Exit early and have the caller reporting an
  8261. // error.
  8262. return true;
  8263. }
  8264. else
  8265. {
  8266. // Set thumb bit if symbol:
  8267. // -Has type STT_ARM_TFUNC or
  8268. // -Has type STT_FUNC, is defined and with LSB in value set.
  8269. thumb_bit =
  8270. (((gsym->type() == elfcpp::STT_ARM_TFUNC)
  8271. || (gsym->type() == elfcpp::STT_FUNC
  8272. && !gsym->is_undefined()
  8273. && ((psymval->value(object, 0) & 1) != 0)))
  8274. ? 1
  8275. : 0);
  8276. }
  8277. }
  8278. else
  8279. {
  8280. // This is a local symbol. Determine if the final target is THUMB.
  8281. // We saved this information when all the local symbols were read.
  8282. elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
  8283. unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
  8284. thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
  8285. if (psymval->is_ifunc_symbol() && object->local_has_plt_offset(r_sym))
  8286. {
  8287. symval.set_output_value(
  8288. target->plt_address_for_local(object, r_sym));
  8289. psymval = &symval;
  8290. }
  8291. }
  8292. }
  8293. else
  8294. {
  8295. // This is a fake relocation synthesized for a stub. It does not have
  8296. // a real symbol. We just look at the LSB of the symbol value to
  8297. // determine if the target is THUMB or not.
  8298. thumb_bit = ((psymval->value(object, 0) & 1) != 0);
  8299. }
  8300. // Strip LSB if this points to a THUMB target.
  8301. if (thumb_bit != 0
  8302. && reloc_property->uses_thumb_bit()
  8303. && ((psymval->value(object, 0) & 1) != 0))
  8304. {
  8305. Arm_address stripped_value =
  8306. psymval->value(object, 0) & ~static_cast<Arm_address>(1);
  8307. symval.set_output_value(stripped_value);
  8308. psymval = &symval;
  8309. }
  8310. // To look up relocation stubs, we need to pass the symbol table index of
  8311. // a local symbol.
  8312. unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
  8313. // Get the addressing origin of the output segment defining the
  8314. // symbol gsym if needed (AAELF 4.6.1.2 Relocation types).
  8315. Arm_address sym_origin = 0;
  8316. if (reloc_property->uses_symbol_base())
  8317. {
  8318. if (r_type == elfcpp::R_ARM_BASE_ABS && gsym == NULL)
  8319. // R_ARM_BASE_ABS with the NULL symbol will give the
  8320. // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
  8321. // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
  8322. sym_origin = target->got_plt_section()->address();
  8323. else if (gsym == NULL)
  8324. sym_origin = 0;
  8325. else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
  8326. sym_origin = gsym->output_segment()->vaddr();
  8327. else if (gsym->source() == Symbol::IN_OUTPUT_DATA)
  8328. sym_origin = gsym->output_data()->address();
  8329. // TODO: Assumes the segment base to be zero for the global symbols
  8330. // till the proper support for the segment-base-relative addressing
  8331. // will be implemented. This is consistent with GNU ld.
  8332. }
  8333. // For relative addressing relocation, find out the relative address base.
  8334. Arm_address relative_address_base = 0;
  8335. switch(reloc_property->relative_address_base())
  8336. {
  8337. case Arm_reloc_property::RAB_NONE:
  8338. // Relocations with relative address bases RAB_TLS and RAB_tp are
  8339. // handled by relocate_tls. So we do not need to do anything here.
  8340. case Arm_reloc_property::RAB_TLS:
  8341. case Arm_reloc_property::RAB_tp:
  8342. break;
  8343. case Arm_reloc_property::RAB_B_S:
  8344. relative_address_base = sym_origin;
  8345. break;
  8346. case Arm_reloc_property::RAB_GOT_ORG:
  8347. relative_address_base = target->got_plt_section()->address();
  8348. break;
  8349. case Arm_reloc_property::RAB_P:
  8350. relative_address_base = address;
  8351. break;
  8352. case Arm_reloc_property::RAB_Pa:
  8353. relative_address_base = address & 0xfffffffcU;
  8354. break;
  8355. default:
  8356. gold_unreachable();
  8357. }
  8358. typename Arm_relocate_functions::Status reloc_status =
  8359. Arm_relocate_functions::STATUS_OKAY;
  8360. bool check_overflow = reloc_property->checks_overflow();
  8361. switch (r_type)
  8362. {
  8363. case elfcpp::R_ARM_NONE:
  8364. break;
  8365. case elfcpp::R_ARM_ABS8:
  8366. if (should_apply_static_reloc(gsym, r_type, false, output_section))
  8367. reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
  8368. break;
  8369. case elfcpp::R_ARM_ABS12:
  8370. if (should_apply_static_reloc(gsym, r_type, false, output_section))
  8371. reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
  8372. break;
  8373. case elfcpp::R_ARM_ABS16:
  8374. if (should_apply_static_reloc(gsym, r_type, false, output_section))
  8375. reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
  8376. break;
  8377. case elfcpp::R_ARM_ABS32:
  8378. if (should_apply_static_reloc(gsym, r_type, true, output_section))
  8379. reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
  8380. thumb_bit);
  8381. break;
  8382. case elfcpp::R_ARM_ABS32_NOI:
  8383. if (should_apply_static_reloc(gsym, r_type, true, output_section))
  8384. // No thumb bit for this relocation: (S + A)
  8385. reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
  8386. 0);
  8387. break;
  8388. case elfcpp::R_ARM_MOVW_ABS_NC:
  8389. if (should_apply_static_reloc(gsym, r_type, false, output_section))
  8390. reloc_status = Arm_relocate_functions::movw(view, object, psymval,
  8391. 0, thumb_bit,
  8392. check_overflow);
  8393. break;
  8394. case elfcpp::R_ARM_MOVT_ABS:
  8395. if (should_apply_static_reloc(gsym, r_type, false, output_section))
  8396. reloc_status = Arm_relocate_functions::movt(view, object, psymval, 0);
  8397. break;
  8398. case elfcpp::R_ARM_THM_MOVW_ABS_NC:
  8399. if (should_apply_static_reloc(gsym, r_type, false, output_section))
  8400. reloc_status = Arm_relocate_functions::thm_movw(view, object, psymval,
  8401. 0, thumb_bit, false);
  8402. break;
  8403. case elfcpp::R_ARM_THM_MOVT_ABS:
  8404. if (should_apply_static_reloc(gsym, r_type, false, output_section))
  8405. reloc_status = Arm_relocate_functions::thm_movt(view, object,
  8406. psymval, 0);
  8407. break;
  8408. case elfcpp::R_ARM_MOVW_PREL_NC:
  8409. case elfcpp::R_ARM_MOVW_BREL_NC:
  8410. case elfcpp::R_ARM_MOVW_BREL:
  8411. reloc_status =
  8412. Arm_relocate_functions::movw(view, object, psymval,
  8413. relative_address_base, thumb_bit,
  8414. check_overflow);
  8415. break;
  8416. case elfcpp::R_ARM_MOVT_PREL:
  8417. case elfcpp::R_ARM_MOVT_BREL:
  8418. reloc_status =
  8419. Arm_relocate_functions::movt(view, object, psymval,
  8420. relative_address_base);
  8421. break;
  8422. case elfcpp::R_ARM_THM_MOVW_PREL_NC:
  8423. case elfcpp::R_ARM_THM_MOVW_BREL_NC:
  8424. case elfcpp::R_ARM_THM_MOVW_BREL:
  8425. reloc_status =
  8426. Arm_relocate_functions::thm_movw(view, object, psymval,
  8427. relative_address_base,
  8428. thumb_bit, check_overflow);
  8429. break;
  8430. case elfcpp::R_ARM_THM_MOVT_PREL:
  8431. case elfcpp::R_ARM_THM_MOVT_BREL:
  8432. reloc_status =
  8433. Arm_relocate_functions::thm_movt(view, object, psymval,
  8434. relative_address_base);
  8435. break;
  8436. case elfcpp::R_ARM_REL32:
  8437. reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
  8438. address, thumb_bit);
  8439. break;
  8440. case elfcpp::R_ARM_THM_ABS5:
  8441. if (should_apply_static_reloc(gsym, r_type, false, output_section))
  8442. reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
  8443. break;
  8444. // Thumb long branches.
  8445. case elfcpp::R_ARM_THM_CALL:
  8446. case elfcpp::R_ARM_THM_XPC22:
  8447. case elfcpp::R_ARM_THM_JUMP24:
  8448. reloc_status =
  8449. Arm_relocate_functions::thumb_branch_common(
  8450. r_type, relinfo, view, gsym, object, r_sym, psymval, address,
  8451. thumb_bit, is_weakly_undefined_without_plt);
  8452. break;
  8453. case elfcpp::R_ARM_GOTOFF32:
  8454. {
  8455. Arm_address got_origin;
  8456. got_origin = target->got_plt_section()->address();
  8457. reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
  8458. got_origin, thumb_bit);
  8459. }
  8460. break;
  8461. case elfcpp::R_ARM_BASE_PREL:
  8462. gold_assert(gsym != NULL);
  8463. reloc_status =
  8464. Arm_relocate_functions::base_prel(view, sym_origin, address);
  8465. break;
  8466. case elfcpp::R_ARM_BASE_ABS:
  8467. if (should_apply_static_reloc(gsym, r_type, false, output_section))
  8468. reloc_status = Arm_relocate_functions::base_abs(view, sym_origin);
  8469. break;
  8470. case elfcpp::R_ARM_GOT_BREL:
  8471. gold_assert(have_got_offset);
  8472. reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
  8473. break;
  8474. case elfcpp::R_ARM_GOT_PREL:
  8475. gold_assert(have_got_offset);
  8476. // Get the address origin for GOT PLT, which is allocated right
  8477. // after the GOT section, to calculate an absolute address of
  8478. // the symbol GOT entry (got_origin + got_offset).
  8479. Arm_address got_origin;
  8480. got_origin = target->got_plt_section()->address();
  8481. reloc_status = Arm_relocate_functions::got_prel(view,
  8482. got_origin + got_offset,
  8483. address);
  8484. break;
  8485. case elfcpp::R_ARM_PLT32:
  8486. case elfcpp::R_ARM_CALL:
  8487. case elfcpp::R_ARM_JUMP24:
  8488. case elfcpp::R_ARM_XPC25:
  8489. gold_assert(gsym == NULL
  8490. || gsym->has_plt_offset()
  8491. || gsym->final_value_is_known()
  8492. || (gsym->is_defined()
  8493. && !gsym->is_from_dynobj()
  8494. && !gsym->is_preemptible()));
  8495. reloc_status =
  8496. Arm_relocate_functions::arm_branch_common(
  8497. r_type, relinfo, view, gsym, object, r_sym, psymval, address,
  8498. thumb_bit, is_weakly_undefined_without_plt);
  8499. break;
  8500. case elfcpp::R_ARM_THM_JUMP19:
  8501. reloc_status =
  8502. Arm_relocate_functions::thm_jump19(view, object, psymval, address,
  8503. thumb_bit);
  8504. break;
  8505. case elfcpp::R_ARM_THM_JUMP6:
  8506. reloc_status =
  8507. Arm_relocate_functions::thm_jump6(view, object, psymval, address);
  8508. break;
  8509. case elfcpp::R_ARM_THM_JUMP8:
  8510. reloc_status =
  8511. Arm_relocate_functions::thm_jump8(view, object, psymval, address);
  8512. break;
  8513. case elfcpp::R_ARM_THM_JUMP11:
  8514. reloc_status =
  8515. Arm_relocate_functions::thm_jump11(view, object, psymval, address);
  8516. break;
  8517. case elfcpp::R_ARM_PREL31:
  8518. reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
  8519. address, thumb_bit);
  8520. break;
  8521. case elfcpp::R_ARM_V4BX:
  8522. if (target->fix_v4bx() > General_options::FIX_V4BX_NONE)
  8523. {
  8524. const bool is_v4bx_interworking =
  8525. (target->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING);
  8526. reloc_status =
  8527. Arm_relocate_functions::v4bx(relinfo, view, object, address,
  8528. is_v4bx_interworking);
  8529. }
  8530. break;
  8531. case elfcpp::R_ARM_THM_PC8:
  8532. reloc_status =
  8533. Arm_relocate_functions::thm_pc8(view, object, psymval, address);
  8534. break;
  8535. case elfcpp::R_ARM_THM_PC12:
  8536. reloc_status =
  8537. Arm_relocate_functions::thm_pc12(view, object, psymval, address);
  8538. break;
  8539. case elfcpp::R_ARM_THM_ALU_PREL_11_0:
  8540. reloc_status =
  8541. Arm_relocate_functions::thm_alu11(view, object, psymval, address,
  8542. thumb_bit);
  8543. break;
  8544. case elfcpp::R_ARM_ALU_PC_G0_NC:
  8545. case elfcpp::R_ARM_ALU_PC_G0:
  8546. case elfcpp::R_ARM_ALU_PC_G1_NC:
  8547. case elfcpp::R_ARM_ALU_PC_G1:
  8548. case elfcpp::R_ARM_ALU_PC_G2:
  8549. case elfcpp::R_ARM_ALU_SB_G0_NC:
  8550. case elfcpp::R_ARM_ALU_SB_G0:
  8551. case elfcpp::R_ARM_ALU_SB_G1_NC:
  8552. case elfcpp::R_ARM_ALU_SB_G1:
  8553. case elfcpp::R_ARM_ALU_SB_G2:
  8554. reloc_status =
  8555. Arm_relocate_functions::arm_grp_alu(view, object, psymval,
  8556. reloc_property->group_index(),
  8557. relative_address_base,
  8558. thumb_bit, check_overflow);
  8559. break;
  8560. case elfcpp::R_ARM_LDR_PC_G0:
  8561. case elfcpp::R_ARM_LDR_PC_G1:
  8562. case elfcpp::R_ARM_LDR_PC_G2:
  8563. case elfcpp::R_ARM_LDR_SB_G0:
  8564. case elfcpp::R_ARM_LDR_SB_G1:
  8565. case elfcpp::R_ARM_LDR_SB_G2:
  8566. reloc_status =
  8567. Arm_relocate_functions::arm_grp_ldr(view, object, psymval,
  8568. reloc_property->group_index(),
  8569. relative_address_base);
  8570. break;
  8571. case elfcpp::R_ARM_LDRS_PC_G0:
  8572. case elfcpp::R_ARM_LDRS_PC_G1:
  8573. case elfcpp::R_ARM_LDRS_PC_G2:
  8574. case elfcpp::R_ARM_LDRS_SB_G0:
  8575. case elfcpp::R_ARM_LDRS_SB_G1:
  8576. case elfcpp::R_ARM_LDRS_SB_G2:
  8577. reloc_status =
  8578. Arm_relocate_functions::arm_grp_ldrs(view, object, psymval,
  8579. reloc_property->group_index(),
  8580. relative_address_base);
  8581. break;
  8582. case elfcpp::R_ARM_LDC_PC_G0:
  8583. case elfcpp::R_ARM_LDC_PC_G1:
  8584. case elfcpp::R_ARM_LDC_PC_G2:
  8585. case elfcpp::R_ARM_LDC_SB_G0:
  8586. case elfcpp::R_ARM_LDC_SB_G1:
  8587. case elfcpp::R_ARM_LDC_SB_G2:
  8588. reloc_status =
  8589. Arm_relocate_functions::arm_grp_ldc(view, object, psymval,
  8590. reloc_property->group_index(),
  8591. relative_address_base);
  8592. break;
  8593. // These are initial tls relocs, which are expected when
  8594. // linking.
  8595. case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
  8596. case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
  8597. case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
  8598. case elfcpp::R_ARM_TLS_IE32: // Initial-exec
  8599. case elfcpp::R_ARM_TLS_LE32: // Local-exec
  8600. reloc_status =
  8601. this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
  8602. view, address, view_size);
  8603. break;
  8604. // The known and unknown unsupported and/or deprecated relocations.
  8605. case elfcpp::R_ARM_PC24:
  8606. case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
  8607. case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
  8608. case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
  8609. default:
  8610. // Just silently leave the method. We should get an appropriate error
  8611. // message in the scan methods.
  8612. break;
  8613. }
  8614. // Report any errors.
  8615. switch (reloc_status)
  8616. {
  8617. case Arm_relocate_functions::STATUS_OKAY:
  8618. break;
  8619. case Arm_relocate_functions::STATUS_OVERFLOW:
  8620. gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
  8621. _("relocation overflow in %s"),
  8622. reloc_property->name().c_str());
  8623. break;
  8624. case Arm_relocate_functions::STATUS_BAD_RELOC:
  8625. gold_error_at_location(
  8626. relinfo,
  8627. relnum,
  8628. rel.get_r_offset(),
  8629. _("unexpected opcode while processing relocation %s"),
  8630. reloc_property->name().c_str());
  8631. break;
  8632. default:
  8633. gold_unreachable();
  8634. }
  8635. return true;
  8636. }
  8637. // Perform a TLS relocation.
  8638. template<bool big_endian>
  8639. inline typename Arm_relocate_functions<big_endian>::Status
  8640. Target_arm<big_endian>::Relocate::relocate_tls(
  8641. const Relocate_info<32, big_endian>* relinfo,
  8642. Target_arm<big_endian>* target,
  8643. size_t relnum,
  8644. const elfcpp::Rel<32, big_endian>& rel,
  8645. unsigned int r_type,
  8646. const Sized_symbol<32>* gsym,
  8647. const Symbol_value<32>* psymval,
  8648. unsigned char* view,
  8649. elfcpp::Elf_types<32>::Elf_Addr address,
  8650. section_size_type /*view_size*/ )
  8651. {
  8652. typedef Arm_relocate_functions<big_endian> ArmRelocFuncs;
  8653. typedef Relocate_functions<32, big_endian> RelocFuncs;
  8654. Output_segment* tls_segment = relinfo->layout->tls_segment();
  8655. const Sized_relobj_file<32, big_endian>* object = relinfo->object;
  8656. elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
  8657. const bool is_final = (gsym == NULL
  8658. ? !parameters->options().shared()
  8659. : gsym->final_value_is_known());
  8660. const tls::Tls_optimization optimized_type
  8661. = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
  8662. switch (r_type)
  8663. {
  8664. case elfcpp::R_ARM_TLS_GD32: // Global-dynamic
  8665. {
  8666. unsigned int got_type = GOT_TYPE_TLS_PAIR;
  8667. unsigned int got_offset;
  8668. if (gsym != NULL)
  8669. {
  8670. gold_assert(gsym->has_got_offset(got_type));
  8671. got_offset = gsym->got_offset(got_type) - target->got_size();
  8672. }
  8673. else
  8674. {
  8675. unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
  8676. gold_assert(object->local_has_got_offset(r_sym, got_type));
  8677. got_offset = (object->local_got_offset(r_sym, got_type)
  8678. - target->got_size());
  8679. }
  8680. if (optimized_type == tls::TLSOPT_NONE)
  8681. {
  8682. Arm_address got_entry =
  8683. target->got_plt_section()->address() + got_offset;
  8684. // Relocate the field with the PC relative offset of the pair of
  8685. // GOT entries.
  8686. RelocFuncs::pcrel32_unaligned(view, got_entry, address);
  8687. return ArmRelocFuncs::STATUS_OKAY;
  8688. }
  8689. }
  8690. break;
  8691. case elfcpp::R_ARM_TLS_LDM32: // Local-dynamic
  8692. if (optimized_type == tls::TLSOPT_NONE)
  8693. {
  8694. // Relocate the field with the offset of the GOT entry for
  8695. // the module index.
  8696. unsigned int got_offset;
  8697. got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
  8698. - target->got_size());
  8699. Arm_address got_entry =
  8700. target->got_plt_section()->address() + got_offset;
  8701. // Relocate the field with the PC relative offset of the pair of
  8702. // GOT entries.
  8703. RelocFuncs::pcrel32_unaligned(view, got_entry, address);
  8704. return ArmRelocFuncs::STATUS_OKAY;
  8705. }
  8706. break;
  8707. case elfcpp::R_ARM_TLS_LDO32: // Alternate local-dynamic
  8708. RelocFuncs::rel32_unaligned(view, value);
  8709. return ArmRelocFuncs::STATUS_OKAY;
  8710. case elfcpp::R_ARM_TLS_IE32: // Initial-exec
  8711. if (optimized_type == tls::TLSOPT_NONE)
  8712. {
  8713. // Relocate the field with the offset of the GOT entry for
  8714. // the tp-relative offset of the symbol.
  8715. unsigned int got_type = GOT_TYPE_TLS_OFFSET;
  8716. unsigned int got_offset;
  8717. if (gsym != NULL)
  8718. {
  8719. gold_assert(gsym->has_got_offset(got_type));
  8720. got_offset = gsym->got_offset(got_type);
  8721. }
  8722. else
  8723. {
  8724. unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
  8725. gold_assert(object->local_has_got_offset(r_sym, got_type));
  8726. got_offset = object->local_got_offset(r_sym, got_type);
  8727. }
  8728. // All GOT offsets are relative to the end of the GOT.
  8729. got_offset -= target->got_size();
  8730. Arm_address got_entry =
  8731. target->got_plt_section()->address() + got_offset;
  8732. // Relocate the field with the PC relative offset of the GOT entry.
  8733. RelocFuncs::pcrel32_unaligned(view, got_entry, address);
  8734. return ArmRelocFuncs::STATUS_OKAY;
  8735. }
  8736. break;
  8737. case elfcpp::R_ARM_TLS_LE32: // Local-exec
  8738. // If we're creating a shared library, a dynamic relocation will
  8739. // have been created for this location, so do not apply it now.
  8740. if (!parameters->options().shared())
  8741. {
  8742. gold_assert(tls_segment != NULL);
  8743. // $tp points to the TCB, which is followed by the TLS, so we
  8744. // need to add TCB size to the offset.
  8745. Arm_address aligned_tcb_size =
  8746. align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
  8747. RelocFuncs::rel32_unaligned(view, value + aligned_tcb_size);
  8748. }
  8749. return ArmRelocFuncs::STATUS_OKAY;
  8750. default:
  8751. gold_unreachable();
  8752. }
  8753. gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
  8754. _("unsupported reloc %u"),
  8755. r_type);
  8756. return ArmRelocFuncs::STATUS_BAD_RELOC;
  8757. }
  8758. // Relocate section data.
  8759. template<bool big_endian>
  8760. void
  8761. Target_arm<big_endian>::relocate_section(
  8762. const Relocate_info<32, big_endian>* relinfo,
  8763. unsigned int sh_type,
  8764. const unsigned char* prelocs,
  8765. size_t reloc_count,
  8766. Output_section* output_section,
  8767. bool needs_special_offset_handling,
  8768. unsigned char* view,
  8769. Arm_address address,
  8770. section_size_type view_size,
  8771. const Reloc_symbol_changes* reloc_symbol_changes)
  8772. {
  8773. typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
  8774. gold_assert(sh_type == elfcpp::SHT_REL);
  8775. // See if we are relocating a relaxed input section. If so, the view
  8776. // covers the whole output section and we need to adjust accordingly.
  8777. if (needs_special_offset_handling)
  8778. {
  8779. const Output_relaxed_input_section* poris =
  8780. output_section->find_relaxed_input_section(relinfo->object,
  8781. relinfo->data_shndx);
  8782. if (poris != NULL)
  8783. {
  8784. Arm_address section_address = poris->address();
  8785. section_size_type section_size = poris->data_size();
  8786. gold_assert((section_address >= address)
  8787. && ((section_address + section_size)
  8788. <= (address + view_size)));
  8789. off_t offset = section_address - address;
  8790. view += offset;
  8791. address += offset;
  8792. view_size = section_size;
  8793. }
  8794. }
  8795. gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
  8796. Arm_relocate, gold::Default_comdat_behavior>(
  8797. relinfo,
  8798. this,
  8799. prelocs,
  8800. reloc_count,
  8801. output_section,
  8802. needs_special_offset_handling,
  8803. view,
  8804. address,
  8805. view_size,
  8806. reloc_symbol_changes);
  8807. }
  8808. // Return the size of a relocation while scanning during a relocatable
  8809. // link.
  8810. template<bool big_endian>
  8811. unsigned int
  8812. Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
  8813. unsigned int r_type,
  8814. Relobj* object)
  8815. {
  8816. r_type = get_real_reloc_type(r_type);
  8817. const Arm_reloc_property* arp =
  8818. arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
  8819. if (arp != NULL)
  8820. return arp->size();
  8821. else
  8822. {
  8823. std::string reloc_name =
  8824. arm_reloc_property_table->reloc_name_in_error_message(r_type);
  8825. gold_error(_("%s: unexpected %s in object file"),
  8826. object->name().c_str(), reloc_name.c_str());
  8827. return 0;
  8828. }
  8829. }
  8830. // Scan the relocs during a relocatable link.
  8831. template<bool big_endian>
  8832. void
  8833. Target_arm<big_endian>::scan_relocatable_relocs(
  8834. Symbol_table* symtab,
  8835. Layout* layout,
  8836. Sized_relobj_file<32, big_endian>* object,
  8837. unsigned int data_shndx,
  8838. unsigned int sh_type,
  8839. const unsigned char* prelocs,
  8840. size_t reloc_count,
  8841. Output_section* output_section,
  8842. bool needs_special_offset_handling,
  8843. size_t local_symbol_count,
  8844. const unsigned char* plocal_symbols,
  8845. Relocatable_relocs* rr)
  8846. {
  8847. gold_assert(sh_type == elfcpp::SHT_REL);
  8848. typedef Arm_scan_relocatable_relocs<big_endian, elfcpp::SHT_REL,
  8849. Relocatable_size_for_reloc> Scan_relocatable_relocs;
  8850. gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
  8851. Scan_relocatable_relocs>(
  8852. symtab,
  8853. layout,
  8854. object,
  8855. data_shndx,
  8856. prelocs,
  8857. reloc_count,
  8858. output_section,
  8859. needs_special_offset_handling,
  8860. local_symbol_count,
  8861. plocal_symbols,
  8862. rr);
  8863. }
  8864. // Emit relocations for a section.
  8865. template<bool big_endian>
  8866. void
  8867. Target_arm<big_endian>::relocate_relocs(
  8868. const Relocate_info<32, big_endian>* relinfo,
  8869. unsigned int sh_type,
  8870. const unsigned char* prelocs,
  8871. size_t reloc_count,
  8872. Output_section* output_section,
  8873. typename elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,
  8874. const Relocatable_relocs* rr,
  8875. unsigned char* view,
  8876. Arm_address view_address,
  8877. section_size_type view_size,
  8878. unsigned char* reloc_view,
  8879. section_size_type reloc_view_size)
  8880. {
  8881. gold_assert(sh_type == elfcpp::SHT_REL);
  8882. gold::relocate_relocs<32, big_endian, elfcpp::SHT_REL>(
  8883. relinfo,
  8884. prelocs,
  8885. reloc_count,
  8886. output_section,
  8887. offset_in_output_section,
  8888. rr,
  8889. view,
  8890. view_address,
  8891. view_size,
  8892. reloc_view,
  8893. reloc_view_size);
  8894. }
  8895. // Perform target-specific processing in a relocatable link. This is
  8896. // only used if we use the relocation strategy RELOC_SPECIAL.
  8897. template<bool big_endian>
  8898. void
  8899. Target_arm<big_endian>::relocate_special_relocatable(
  8900. const Relocate_info<32, big_endian>* relinfo,
  8901. unsigned int sh_type,
  8902. const unsigned char* preloc_in,
  8903. size_t relnum,
  8904. Output_section* output_section,
  8905. typename elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,
  8906. unsigned char* view,
  8907. elfcpp::Elf_types<32>::Elf_Addr view_address,
  8908. section_size_type,
  8909. unsigned char* preloc_out)
  8910. {
  8911. // We can only handle REL type relocation sections.
  8912. gold_assert(sh_type == elfcpp::SHT_REL);
  8913. typedef typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc Reltype;
  8914. typedef typename Reloc_types<elfcpp::SHT_REL, 32, big_endian>::Reloc_write
  8915. Reltype_write;
  8916. const Arm_address invalid_address = static_cast<Arm_address>(0) - 1;
  8917. const Arm_relobj<big_endian>* object =
  8918. Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
  8919. const unsigned int local_count = object->local_symbol_count();
  8920. Reltype reloc(preloc_in);
  8921. Reltype_write reloc_write(preloc_out);
  8922. elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
  8923. const unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
  8924. const unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
  8925. const Arm_reloc_property* arp =
  8926. arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
  8927. gold_assert(arp != NULL);
  8928. // Get the new symbol index.
  8929. // We only use RELOC_SPECIAL strategy in local relocations.
  8930. gold_assert(r_sym < local_count);
  8931. // We are adjusting a section symbol. We need to find
  8932. // the symbol table index of the section symbol for
  8933. // the output section corresponding to input section
  8934. // in which this symbol is defined.
  8935. bool is_ordinary;
  8936. unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
  8937. gold_assert(is_ordinary);
  8938. Output_section* os = object->output_section(shndx);
  8939. gold_assert(os != NULL);
  8940. gold_assert(os->needs_symtab_index());
  8941. unsigned int new_symndx = os->symtab_index();
  8942. // Get the new offset--the location in the output section where
  8943. // this relocation should be applied.
  8944. Arm_address offset = reloc.get_r_offset();
  8945. Arm_address new_offset;
  8946. if (offset_in_output_section != invalid_address)
  8947. new_offset = offset + offset_in_output_section;
  8948. else
  8949. {
  8950. section_offset_type sot_offset =
  8951. convert_types<section_offset_type, Arm_address>(offset);
  8952. section_offset_type new_sot_offset =
  8953. output_section->output_offset(object, relinfo->data_shndx,
  8954. sot_offset);
  8955. gold_assert(new_sot_offset != -1);
  8956. new_offset = new_sot_offset;
  8957. }
  8958. // In an object file, r_offset is an offset within the section.
  8959. // In an executable or dynamic object, generated by
  8960. // --emit-relocs, r_offset is an absolute address.
  8961. if (!parameters->options().relocatable())
  8962. {
  8963. new_offset += view_address;
  8964. if (offset_in_output_section != invalid_address)
  8965. new_offset -= offset_in_output_section;
  8966. }
  8967. reloc_write.put_r_offset(new_offset);
  8968. reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
  8969. // Handle the reloc addend.
  8970. // The relocation uses a section symbol in the input file.
  8971. // We are adjusting it to use a section symbol in the output
  8972. // file. The input section symbol refers to some address in
  8973. // the input section. We need the relocation in the output
  8974. // file to refer to that same address. This adjustment to
  8975. // the addend is the same calculation we use for a simple
  8976. // absolute relocation for the input section symbol.
  8977. const Symbol_value<32>* psymval = object->local_symbol(r_sym);
  8978. // Handle THUMB bit.
  8979. Symbol_value<32> symval;
  8980. Arm_address thumb_bit =
  8981. object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
  8982. if (thumb_bit != 0
  8983. && arp->uses_thumb_bit()
  8984. && ((psymval->value(object, 0) & 1) != 0))
  8985. {
  8986. Arm_address stripped_value =
  8987. psymval->value(object, 0) & ~static_cast<Arm_address>(1);
  8988. symval.set_output_value(stripped_value);
  8989. psymval = &symval;
  8990. }
  8991. unsigned char* paddend = view + offset;
  8992. typename Arm_relocate_functions<big_endian>::Status reloc_status =
  8993. Arm_relocate_functions<big_endian>::STATUS_OKAY;
  8994. switch (r_type)
  8995. {
  8996. case elfcpp::R_ARM_ABS8:
  8997. reloc_status = Arm_relocate_functions<big_endian>::abs8(paddend, object,
  8998. psymval);
  8999. break;
  9000. case elfcpp::R_ARM_ABS12:
  9001. reloc_status = Arm_relocate_functions<big_endian>::abs12(paddend, object,
  9002. psymval);
  9003. break;
  9004. case elfcpp::R_ARM_ABS16:
  9005. reloc_status = Arm_relocate_functions<big_endian>::abs16(paddend, object,
  9006. psymval);
  9007. break;
  9008. case elfcpp::R_ARM_THM_ABS5:
  9009. reloc_status = Arm_relocate_functions<big_endian>::thm_abs5(paddend,
  9010. object,
  9011. psymval);
  9012. break;
  9013. case elfcpp::R_ARM_MOVW_ABS_NC:
  9014. case elfcpp::R_ARM_MOVW_PREL_NC:
  9015. case elfcpp::R_ARM_MOVW_BREL_NC:
  9016. case elfcpp::R_ARM_MOVW_BREL:
  9017. reloc_status = Arm_relocate_functions<big_endian>::movw(
  9018. paddend, object, psymval, 0, thumb_bit, arp->checks_overflow());
  9019. break;
  9020. case elfcpp::R_ARM_THM_MOVW_ABS_NC:
  9021. case elfcpp::R_ARM_THM_MOVW_PREL_NC:
  9022. case elfcpp::R_ARM_THM_MOVW_BREL_NC:
  9023. case elfcpp::R_ARM_THM_MOVW_BREL:
  9024. reloc_status = Arm_relocate_functions<big_endian>::thm_movw(
  9025. paddend, object, psymval, 0, thumb_bit, arp->checks_overflow());
  9026. break;
  9027. case elfcpp::R_ARM_THM_CALL:
  9028. case elfcpp::R_ARM_THM_XPC22:
  9029. case elfcpp::R_ARM_THM_JUMP24:
  9030. reloc_status =
  9031. Arm_relocate_functions<big_endian>::thumb_branch_common(
  9032. r_type, relinfo, paddend, NULL, object, 0, psymval, 0, thumb_bit,
  9033. false);
  9034. break;
  9035. case elfcpp::R_ARM_PLT32:
  9036. case elfcpp::R_ARM_CALL:
  9037. case elfcpp::R_ARM_JUMP24:
  9038. case elfcpp::R_ARM_XPC25:
  9039. reloc_status =
  9040. Arm_relocate_functions<big_endian>::arm_branch_common(
  9041. r_type, relinfo, paddend, NULL, object, 0, psymval, 0, thumb_bit,
  9042. false);
  9043. break;
  9044. case elfcpp::R_ARM_THM_JUMP19:
  9045. reloc_status =
  9046. Arm_relocate_functions<big_endian>::thm_jump19(paddend, object,
  9047. psymval, 0, thumb_bit);
  9048. break;
  9049. case elfcpp::R_ARM_THM_JUMP6:
  9050. reloc_status =
  9051. Arm_relocate_functions<big_endian>::thm_jump6(paddend, object, psymval,
  9052. 0);
  9053. break;
  9054. case elfcpp::R_ARM_THM_JUMP8:
  9055. reloc_status =
  9056. Arm_relocate_functions<big_endian>::thm_jump8(paddend, object, psymval,
  9057. 0);
  9058. break;
  9059. case elfcpp::R_ARM_THM_JUMP11:
  9060. reloc_status =
  9061. Arm_relocate_functions<big_endian>::thm_jump11(paddend, object, psymval,
  9062. 0);
  9063. break;
  9064. case elfcpp::R_ARM_PREL31:
  9065. reloc_status =
  9066. Arm_relocate_functions<big_endian>::prel31(paddend, object, psymval, 0,
  9067. thumb_bit);
  9068. break;
  9069. case elfcpp::R_ARM_THM_PC8:
  9070. reloc_status =
  9071. Arm_relocate_functions<big_endian>::thm_pc8(paddend, object, psymval,
  9072. 0);
  9073. break;
  9074. case elfcpp::R_ARM_THM_PC12:
  9075. reloc_status =
  9076. Arm_relocate_functions<big_endian>::thm_pc12(paddend, object, psymval,
  9077. 0);
  9078. break;
  9079. case elfcpp::R_ARM_THM_ALU_PREL_11_0:
  9080. reloc_status =
  9081. Arm_relocate_functions<big_endian>::thm_alu11(paddend, object, psymval,
  9082. 0, thumb_bit);
  9083. break;
  9084. // These relocation truncate relocation results so we cannot handle them
  9085. // in a relocatable link.
  9086. case elfcpp::R_ARM_MOVT_ABS:
  9087. case elfcpp::R_ARM_THM_MOVT_ABS:
  9088. case elfcpp::R_ARM_MOVT_PREL:
  9089. case elfcpp::R_ARM_MOVT_BREL:
  9090. case elfcpp::R_ARM_THM_MOVT_PREL:
  9091. case elfcpp::R_ARM_THM_MOVT_BREL:
  9092. case elfcpp::R_ARM_ALU_PC_G0_NC:
  9093. case elfcpp::R_ARM_ALU_PC_G0:
  9094. case elfcpp::R_ARM_ALU_PC_G1_NC:
  9095. case elfcpp::R_ARM_ALU_PC_G1:
  9096. case elfcpp::R_ARM_ALU_PC_G2:
  9097. case elfcpp::R_ARM_ALU_SB_G0_NC:
  9098. case elfcpp::R_ARM_ALU_SB_G0:
  9099. case elfcpp::R_ARM_ALU_SB_G1_NC:
  9100. case elfcpp::R_ARM_ALU_SB_G1:
  9101. case elfcpp::R_ARM_ALU_SB_G2:
  9102. case elfcpp::R_ARM_LDR_PC_G0:
  9103. case elfcpp::R_ARM_LDR_PC_G1:
  9104. case elfcpp::R_ARM_LDR_PC_G2:
  9105. case elfcpp::R_ARM_LDR_SB_G0:
  9106. case elfcpp::R_ARM_LDR_SB_G1:
  9107. case elfcpp::R_ARM_LDR_SB_G2:
  9108. case elfcpp::R_ARM_LDRS_PC_G0:
  9109. case elfcpp::R_ARM_LDRS_PC_G1:
  9110. case elfcpp::R_ARM_LDRS_PC_G2:
  9111. case elfcpp::R_ARM_LDRS_SB_G0:
  9112. case elfcpp::R_ARM_LDRS_SB_G1:
  9113. case elfcpp::R_ARM_LDRS_SB_G2:
  9114. case elfcpp::R_ARM_LDC_PC_G0:
  9115. case elfcpp::R_ARM_LDC_PC_G1:
  9116. case elfcpp::R_ARM_LDC_PC_G2:
  9117. case elfcpp::R_ARM_LDC_SB_G0:
  9118. case elfcpp::R_ARM_LDC_SB_G1:
  9119. case elfcpp::R_ARM_LDC_SB_G2:
  9120. gold_error(_("cannot handle %s in a relocatable link"),
  9121. arp->name().c_str());
  9122. break;
  9123. default:
  9124. gold_unreachable();
  9125. }
  9126. // Report any errors.
  9127. switch (reloc_status)
  9128. {
  9129. case Arm_relocate_functions<big_endian>::STATUS_OKAY:
  9130. break;
  9131. case Arm_relocate_functions<big_endian>::STATUS_OVERFLOW:
  9132. gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
  9133. _("relocation overflow in %s"),
  9134. arp->name().c_str());
  9135. break;
  9136. case Arm_relocate_functions<big_endian>::STATUS_BAD_RELOC:
  9137. gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
  9138. _("unexpected opcode while processing relocation %s"),
  9139. arp->name().c_str());
  9140. break;
  9141. default:
  9142. gold_unreachable();
  9143. }
  9144. }
  9145. // Return the value to use for a dynamic symbol which requires special
  9146. // treatment. This is how we support equality comparisons of function
  9147. // pointers across shared library boundaries, as described in the
  9148. // processor specific ABI supplement.
  9149. template<bool big_endian>
  9150. uint64_t
  9151. Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
  9152. {
  9153. gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
  9154. return this->plt_address_for_global(gsym);
  9155. }
  9156. // Map platform-specific relocs to real relocs
  9157. //
  9158. template<bool big_endian>
  9159. unsigned int
  9160. Target_arm<big_endian>::get_real_reloc_type(unsigned int r_type)
  9161. {
  9162. switch (r_type)
  9163. {
  9164. case elfcpp::R_ARM_TARGET1:
  9165. // This is either R_ARM_ABS32 or R_ARM_REL32;
  9166. return elfcpp::R_ARM_ABS32;
  9167. case elfcpp::R_ARM_TARGET2:
  9168. // This can be any reloc type but usually is R_ARM_GOT_PREL
  9169. return elfcpp::R_ARM_GOT_PREL;
  9170. default:
  9171. return r_type;
  9172. }
  9173. }
  9174. // Whether if two EABI versions V1 and V2 are compatible.
  9175. template<bool big_endian>
  9176. bool
  9177. Target_arm<big_endian>::are_eabi_versions_compatible(
  9178. elfcpp::Elf_Word v1,
  9179. elfcpp::Elf_Word v2)
  9180. {
  9181. // v4 and v5 are the same spec before and after it was released,
  9182. // so allow mixing them.
  9183. if ((v1 == elfcpp::EF_ARM_EABI_UNKNOWN || v2 == elfcpp::EF_ARM_EABI_UNKNOWN)
  9184. || (v1 == elfcpp::EF_ARM_EABI_VER4 && v2 == elfcpp::EF_ARM_EABI_VER5)
  9185. || (v1 == elfcpp::EF_ARM_EABI_VER5 && v2 == elfcpp::EF_ARM_EABI_VER4))
  9186. return true;
  9187. return v1 == v2;
  9188. }
  9189. // Combine FLAGS from an input object called NAME and the processor-specific
  9190. // flags in the ELF header of the output. Much of this is adapted from the
  9191. // processor-specific flags merging code in elf32_arm_merge_private_bfd_data
  9192. // in bfd/elf32-arm.c.
  9193. template<bool big_endian>
  9194. void
  9195. Target_arm<big_endian>::merge_processor_specific_flags(
  9196. const std::string& name,
  9197. elfcpp::Elf_Word flags)
  9198. {
  9199. if (this->are_processor_specific_flags_set())
  9200. {
  9201. elfcpp::Elf_Word out_flags = this->processor_specific_flags();
  9202. // Nothing to merge if flags equal to those in output.
  9203. if (flags == out_flags)
  9204. return;
  9205. // Complain about various flag mismatches.
  9206. elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
  9207. elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
  9208. if (!this->are_eabi_versions_compatible(version1, version2)
  9209. && parameters->options().warn_mismatch())
  9210. gold_error(_("Source object %s has EABI version %d but output has "
  9211. "EABI version %d."),
  9212. name.c_str(),
  9213. (flags & elfcpp::EF_ARM_EABIMASK) >> 24,
  9214. (out_flags & elfcpp::EF_ARM_EABIMASK) >> 24);
  9215. }
  9216. else
  9217. {
  9218. // If the input is the default architecture and had the default
  9219. // flags then do not bother setting the flags for the output
  9220. // architecture, instead allow future merges to do this. If no
  9221. // future merges ever set these flags then they will retain their
  9222. // uninitialised values, which surprise surprise, correspond
  9223. // to the default values.
  9224. if (flags == 0)
  9225. return;
  9226. // This is the first time, just copy the flags.
  9227. // We only copy the EABI version for now.
  9228. this->set_processor_specific_flags(flags & elfcpp::EF_ARM_EABIMASK);
  9229. }
  9230. }
  9231. // Adjust ELF file header.
  9232. template<bool big_endian>
  9233. void
  9234. Target_arm<big_endian>::do_adjust_elf_header(
  9235. unsigned char* view,
  9236. int len)
  9237. {
  9238. gold_assert(len == elfcpp::Elf_sizes<32>::ehdr_size);
  9239. elfcpp::Ehdr<32, big_endian> ehdr(view);
  9240. elfcpp::Elf_Word flags = this->processor_specific_flags();
  9241. unsigned char e_ident[elfcpp::EI_NIDENT];
  9242. memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
  9243. if (elfcpp::arm_eabi_version(flags)
  9244. == elfcpp::EF_ARM_EABI_UNKNOWN)
  9245. e_ident[elfcpp::EI_OSABI] = elfcpp::ELFOSABI_ARM;
  9246. else
  9247. e_ident[elfcpp::EI_OSABI] = 0;
  9248. e_ident[elfcpp::EI_ABIVERSION] = 0;
  9249. // FIXME: Do EF_ARM_BE8 adjustment.
  9250. // If we're working in EABI_VER5, set the hard/soft float ABI flags
  9251. // as appropriate.
  9252. if (elfcpp::arm_eabi_version(flags) == elfcpp::EF_ARM_EABI_VER5)
  9253. {
  9254. elfcpp::Elf_Half type = ehdr.get_e_type();
  9255. if (type == elfcpp::ET_EXEC || type == elfcpp::ET_DYN)
  9256. {
  9257. Object_attribute* attr = this->get_aeabi_object_attribute(elfcpp::Tag_ABI_VFP_args);
  9258. if (attr->int_value() == elfcpp::AEABI_VFP_args_vfp)
  9259. flags |= elfcpp::EF_ARM_ABI_FLOAT_HARD;
  9260. else
  9261. flags |= elfcpp::EF_ARM_ABI_FLOAT_SOFT;
  9262. this->set_processor_specific_flags(flags);
  9263. }
  9264. }
  9265. elfcpp::Ehdr_write<32, big_endian> oehdr(view);
  9266. oehdr.put_e_ident(e_ident);
  9267. oehdr.put_e_flags(this->processor_specific_flags());
  9268. }
  9269. // do_make_elf_object to override the same function in the base class.
  9270. // We need to use a target-specific sub-class of
  9271. // Sized_relobj_file<32, big_endian> to store ARM specific information.
  9272. // Hence we need to have our own ELF object creation.
  9273. template<bool big_endian>
  9274. Object*
  9275. Target_arm<big_endian>::do_make_elf_object(
  9276. const std::string& name,
  9277. Input_file* input_file,
  9278. off_t offset, const elfcpp::Ehdr<32, big_endian>& ehdr)
  9279. {
  9280. int et = ehdr.get_e_type();
  9281. // ET_EXEC files are valid input for --just-symbols/-R,
  9282. // and we treat them as relocatable objects.
  9283. if (et == elfcpp::ET_REL
  9284. || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
  9285. {
  9286. Arm_relobj<big_endian>* obj =
  9287. new Arm_relobj<big_endian>(name, input_file, offset, ehdr);
  9288. obj->setup();
  9289. return obj;
  9290. }
  9291. else if (et == elfcpp::ET_DYN)
  9292. {
  9293. Sized_dynobj<32, big_endian>* obj =
  9294. new Arm_dynobj<big_endian>(name, input_file, offset, ehdr);
  9295. obj->setup();
  9296. return obj;
  9297. }
  9298. else
  9299. {
  9300. gold_error(_("%s: unsupported ELF file type %d"),
  9301. name.c_str(), et);
  9302. return NULL;
  9303. }
  9304. }
  9305. // Read the architecture from the Tag_also_compatible_with attribute, if any.
  9306. // Returns -1 if no architecture could be read.
  9307. // This is adapted from get_secondary_compatible_arch() in bfd/elf32-arm.c.
  9308. template<bool big_endian>
  9309. int
  9310. Target_arm<big_endian>::get_secondary_compatible_arch(
  9311. const Attributes_section_data* pasd)
  9312. {
  9313. const Object_attribute* known_attributes =
  9314. pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
  9315. // Note: the tag and its argument below are uleb128 values, though
  9316. // currently-defined values fit in one byte for each.
  9317. const std::string& sv =
  9318. known_attributes[elfcpp::Tag_also_compatible_with].string_value();
  9319. if (sv.size() == 2
  9320. && sv.data()[0] == elfcpp::Tag_CPU_arch
  9321. && (sv.data()[1] & 128) != 128)
  9322. return sv.data()[1];
  9323. // This tag is "safely ignorable", so don't complain if it looks funny.
  9324. return -1;
  9325. }
  9326. // Set, or unset, the architecture of the Tag_also_compatible_with attribute.
  9327. // The tag is removed if ARCH is -1.
  9328. // This is adapted from set_secondary_compatible_arch() in bfd/elf32-arm.c.
  9329. template<bool big_endian>
  9330. void
  9331. Target_arm<big_endian>::set_secondary_compatible_arch(
  9332. Attributes_section_data* pasd,
  9333. int arch)
  9334. {
  9335. Object_attribute* known_attributes =
  9336. pasd->known_attributes(Object_attribute::OBJ_ATTR_PROC);
  9337. if (arch == -1)
  9338. {
  9339. known_attributes[elfcpp::Tag_also_compatible_with].set_string_value("");
  9340. return;
  9341. }
  9342. // Note: the tag and its argument below are uleb128 values, though
  9343. // currently-defined values fit in one byte for each.
  9344. char sv[3];
  9345. sv[0] = elfcpp::Tag_CPU_arch;
  9346. gold_assert(arch != 0);
  9347. sv[1] = arch;
  9348. sv[2] = '\0';
  9349. known_attributes[elfcpp::Tag_also_compatible_with].set_string_value(sv);
  9350. }
  9351. // Combine two values for Tag_CPU_arch, taking secondary compatibility tags
  9352. // into account.
  9353. // This is adapted from tag_cpu_arch_combine() in bfd/elf32-arm.c.
  9354. template<bool big_endian>
  9355. int
  9356. Target_arm<big_endian>::tag_cpu_arch_combine(
  9357. const char* name,
  9358. int oldtag,
  9359. int* secondary_compat_out,
  9360. int newtag,
  9361. int secondary_compat)
  9362. {
  9363. #define T(X) elfcpp::TAG_CPU_ARCH_##X
  9364. static const int v6t2[] =
  9365. {
  9366. T(V6T2), // PRE_V4.
  9367. T(V6T2), // V4.
  9368. T(V6T2), // V4T.
  9369. T(V6T2), // V5T.
  9370. T(V6T2), // V5TE.
  9371. T(V6T2), // V5TEJ.
  9372. T(V6T2), // V6.
  9373. T(V7), // V6KZ.
  9374. T(V6T2) // V6T2.
  9375. };
  9376. static const int v6k[] =
  9377. {
  9378. T(V6K), // PRE_V4.
  9379. T(V6K), // V4.
  9380. T(V6K), // V4T.
  9381. T(V6K), // V5T.
  9382. T(V6K), // V5TE.
  9383. T(V6K), // V5TEJ.
  9384. T(V6K), // V6.
  9385. T(V6KZ), // V6KZ.
  9386. T(V7), // V6T2.
  9387. T(V6K) // V6K.
  9388. };
  9389. static const int v7[] =
  9390. {
  9391. T(V7), // PRE_V4.
  9392. T(V7), // V4.
  9393. T(V7), // V4T.
  9394. T(V7), // V5T.
  9395. T(V7), // V5TE.
  9396. T(V7), // V5TEJ.
  9397. T(V7), // V6.
  9398. T(V7), // V6KZ.
  9399. T(V7), // V6T2.
  9400. T(V7), // V6K.
  9401. T(V7) // V7.
  9402. };
  9403. static const int v6_m[] =
  9404. {
  9405. -1, // PRE_V4.
  9406. -1, // V4.
  9407. T(V6K), // V4T.
  9408. T(V6K), // V5T.
  9409. T(V6K), // V5TE.
  9410. T(V6K), // V5TEJ.
  9411. T(V6K), // V6.
  9412. T(V6KZ), // V6KZ.
  9413. T(V7), // V6T2.
  9414. T(V6K), // V6K.
  9415. T(V7), // V7.
  9416. T(V6_M) // V6_M.
  9417. };
  9418. static const int v6s_m[] =
  9419. {
  9420. -1, // PRE_V4.
  9421. -1, // V4.
  9422. T(V6K), // V4T.
  9423. T(V6K), // V5T.
  9424. T(V6K), // V5TE.
  9425. T(V6K), // V5TEJ.
  9426. T(V6K), // V6.
  9427. T(V6KZ), // V6KZ.
  9428. T(V7), // V6T2.
  9429. T(V6K), // V6K.
  9430. T(V7), // V7.
  9431. T(V6S_M), // V6_M.
  9432. T(V6S_M) // V6S_M.
  9433. };
  9434. static const int v7e_m[] =
  9435. {
  9436. -1, // PRE_V4.
  9437. -1, // V4.
  9438. T(V7E_M), // V4T.
  9439. T(V7E_M), // V5T.
  9440. T(V7E_M), // V5TE.
  9441. T(V7E_M), // V5TEJ.
  9442. T(V7E_M), // V6.
  9443. T(V7E_M), // V6KZ.
  9444. T(V7E_M), // V6T2.
  9445. T(V7E_M), // V6K.
  9446. T(V7E_M), // V7.
  9447. T(V7E_M), // V6_M.
  9448. T(V7E_M), // V6S_M.
  9449. T(V7E_M) // V7E_M.
  9450. };
  9451. static const int v8[] =
  9452. {
  9453. T(V8), // PRE_V4.
  9454. T(V8), // V4.
  9455. T(V8), // V4T.
  9456. T(V8), // V5T.
  9457. T(V8), // V5TE.
  9458. T(V8), // V5TEJ.
  9459. T(V8), // V6.
  9460. T(V8), // V6KZ.
  9461. T(V8), // V6T2.
  9462. T(V8), // V6K.
  9463. T(V8), // V7.
  9464. T(V8), // V6_M.
  9465. T(V8), // V6S_M.
  9466. T(V8), // V7E_M.
  9467. T(V8) // V8.
  9468. };
  9469. static const int v4t_plus_v6_m[] =
  9470. {
  9471. -1, // PRE_V4.
  9472. -1, // V4.
  9473. T(V4T), // V4T.
  9474. T(V5T), // V5T.
  9475. T(V5TE), // V5TE.
  9476. T(V5TEJ), // V5TEJ.
  9477. T(V6), // V6.
  9478. T(V6KZ), // V6KZ.
  9479. T(V6T2), // V6T2.
  9480. T(V6K), // V6K.
  9481. T(V7), // V7.
  9482. T(V6_M), // V6_M.
  9483. T(V6S_M), // V6S_M.
  9484. T(V7E_M), // V7E_M.
  9485. T(V8), // V8.
  9486. T(V4T_PLUS_V6_M) // V4T plus V6_M.
  9487. };
  9488. static const int* comb[] =
  9489. {
  9490. v6t2,
  9491. v6k,
  9492. v7,
  9493. v6_m,
  9494. v6s_m,
  9495. v7e_m,
  9496. v8,
  9497. // Pseudo-architecture.
  9498. v4t_plus_v6_m
  9499. };
  9500. // Check we've not got a higher architecture than we know about.
  9501. if (oldtag > elfcpp::MAX_TAG_CPU_ARCH || newtag > elfcpp::MAX_TAG_CPU_ARCH)
  9502. {
  9503. gold_error(_("%s: unknown CPU architecture"), name);
  9504. return -1;
  9505. }
  9506. // Override old tag if we have a Tag_also_compatible_with on the output.
  9507. if ((oldtag == T(V6_M) && *secondary_compat_out == T(V4T))
  9508. || (oldtag == T(V4T) && *secondary_compat_out == T(V6_M)))
  9509. oldtag = T(V4T_PLUS_V6_M);
  9510. // And override the new tag if we have a Tag_also_compatible_with on the
  9511. // input.
  9512. if ((newtag == T(V6_M) && secondary_compat == T(V4T))
  9513. || (newtag == T(V4T) && secondary_compat == T(V6_M)))
  9514. newtag = T(V4T_PLUS_V6_M);
  9515. // Architectures before V6KZ add features monotonically.
  9516. int tagh = std::max(oldtag, newtag);
  9517. if (tagh <= elfcpp::TAG_CPU_ARCH_V6KZ)
  9518. return tagh;
  9519. int tagl = std::min(oldtag, newtag);
  9520. int result = comb[tagh - T(V6T2)][tagl];
  9521. // Use Tag_CPU_arch == V4T and Tag_also_compatible_with (Tag_CPU_arch V6_M)
  9522. // as the canonical version.
  9523. if (result == T(V4T_PLUS_V6_M))
  9524. {
  9525. result = T(V4T);
  9526. *secondary_compat_out = T(V6_M);
  9527. }
  9528. else
  9529. *secondary_compat_out = -1;
  9530. if (result == -1)
  9531. {
  9532. gold_error(_("%s: conflicting CPU architectures %d/%d"),
  9533. name, oldtag, newtag);
  9534. return -1;
  9535. }
  9536. return result;
  9537. #undef T
  9538. }
  9539. // Helper to print AEABI enum tag value.
  9540. template<bool big_endian>
  9541. std::string
  9542. Target_arm<big_endian>::aeabi_enum_name(unsigned int value)
  9543. {
  9544. static const char* aeabi_enum_names[] =
  9545. { "", "variable-size", "32-bit", "" };
  9546. const size_t aeabi_enum_names_size =
  9547. sizeof(aeabi_enum_names) / sizeof(aeabi_enum_names[0]);
  9548. if (value < aeabi_enum_names_size)
  9549. return std::string(aeabi_enum_names[value]);
  9550. else
  9551. {
  9552. char buffer[100];
  9553. sprintf(buffer, "<unknown value %u>", value);
  9554. return std::string(buffer);
  9555. }
  9556. }
  9557. // Return the string value to store in TAG_CPU_name.
  9558. template<bool big_endian>
  9559. std::string
  9560. Target_arm<big_endian>::tag_cpu_name_value(unsigned int value)
  9561. {
  9562. static const char* name_table[] = {
  9563. // These aren't real CPU names, but we can't guess
  9564. // that from the architecture version alone.
  9565. "Pre v4",
  9566. "ARM v4",
  9567. "ARM v4T",
  9568. "ARM v5T",
  9569. "ARM v5TE",
  9570. "ARM v5TEJ",
  9571. "ARM v6",
  9572. "ARM v6KZ",
  9573. "ARM v6T2",
  9574. "ARM v6K",
  9575. "ARM v7",
  9576. "ARM v6-M",
  9577. "ARM v6S-M",
  9578. "ARM v7E-M",
  9579. "ARM v8"
  9580. };
  9581. const size_t name_table_size = sizeof(name_table) / sizeof(name_table[0]);
  9582. if (value < name_table_size)
  9583. return std::string(name_table[value]);
  9584. else
  9585. {
  9586. char buffer[100];
  9587. sprintf(buffer, "<unknown CPU value %u>", value);
  9588. return std::string(buffer);
  9589. }
  9590. }
  9591. // Query attributes object to see if integer divide instructions may be
  9592. // present in an object.
  9593. template<bool big_endian>
  9594. bool
  9595. Target_arm<big_endian>::attributes_accept_div(int arch, int profile,
  9596. const Object_attribute* div_attr)
  9597. {
  9598. switch (div_attr->int_value())
  9599. {
  9600. case 0:
  9601. // Integer divide allowed if instruction contained in
  9602. // archetecture.
  9603. if (arch == elfcpp::TAG_CPU_ARCH_V7 && (profile == 'R' || profile == 'M'))
  9604. return true;
  9605. else if (arch >= elfcpp::TAG_CPU_ARCH_V7E_M)
  9606. return true;
  9607. else
  9608. return false;
  9609. case 1:
  9610. // Integer divide explicitly prohibited.
  9611. return false;
  9612. default:
  9613. // Unrecognised case - treat as allowing divide everywhere.
  9614. case 2:
  9615. // Integer divide allowed in ARM state.
  9616. return true;
  9617. }
  9618. }
  9619. // Query attributes object to see if integer divide instructions are
  9620. // forbidden to be in the object. This is not the inverse of
  9621. // attributes_accept_div.
  9622. template<bool big_endian>
  9623. bool
  9624. Target_arm<big_endian>::attributes_forbid_div(const Object_attribute* div_attr)
  9625. {
  9626. return div_attr->int_value() == 1;
  9627. }
  9628. // Merge object attributes from input file called NAME with those of the
  9629. // output. The input object attributes are in the object pointed by PASD.
  9630. template<bool big_endian>
  9631. void
  9632. Target_arm<big_endian>::merge_object_attributes(
  9633. const char* name,
  9634. const Attributes_section_data* pasd)
  9635. {
  9636. // Return if there is no attributes section data.
  9637. if (pasd == NULL)
  9638. return;
  9639. // If output has no object attributes, just copy.
  9640. const int vendor = Object_attribute::OBJ_ATTR_PROC;
  9641. if (this->attributes_section_data_ == NULL)
  9642. {
  9643. this->attributes_section_data_ = new Attributes_section_data(*pasd);
  9644. Object_attribute* out_attr =
  9645. this->attributes_section_data_->known_attributes(vendor);
  9646. // We do not output objects with Tag_MPextension_use_legacy - we move
  9647. // the attribute's value to Tag_MPextension_use. */
  9648. if (out_attr[elfcpp::Tag_MPextension_use_legacy].int_value() != 0)
  9649. {
  9650. if (out_attr[elfcpp::Tag_MPextension_use].int_value() != 0
  9651. && out_attr[elfcpp::Tag_MPextension_use_legacy].int_value()
  9652. != out_attr[elfcpp::Tag_MPextension_use].int_value())
  9653. {
  9654. gold_error(_("%s has both the current and legacy "
  9655. "Tag_MPextension_use attributes"),
  9656. name);
  9657. }
  9658. out_attr[elfcpp::Tag_MPextension_use] =
  9659. out_attr[elfcpp::Tag_MPextension_use_legacy];
  9660. out_attr[elfcpp::Tag_MPextension_use_legacy].set_type(0);
  9661. out_attr[elfcpp::Tag_MPextension_use_legacy].set_int_value(0);
  9662. }
  9663. return;
  9664. }
  9665. const Object_attribute* in_attr = pasd->known_attributes(vendor);
  9666. Object_attribute* out_attr =
  9667. this->attributes_section_data_->known_attributes(vendor);
  9668. // This needs to happen before Tag_ABI_FP_number_model is merged. */
  9669. if (in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
  9670. != out_attr[elfcpp::Tag_ABI_VFP_args].int_value())
  9671. {
  9672. // Ignore mismatches if the object doesn't use floating point. */
  9673. if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value()
  9674. == elfcpp::AEABI_FP_number_model_none
  9675. || (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value()
  9676. != elfcpp::AEABI_FP_number_model_none
  9677. && out_attr[elfcpp::Tag_ABI_VFP_args].int_value()
  9678. == elfcpp::AEABI_VFP_args_compatible))
  9679. out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
  9680. in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
  9681. else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value()
  9682. != elfcpp::AEABI_FP_number_model_none
  9683. && in_attr[elfcpp::Tag_ABI_VFP_args].int_value()
  9684. != elfcpp::AEABI_VFP_args_compatible
  9685. && parameters->options().warn_mismatch())
  9686. gold_error(_("%s uses VFP register arguments, output does not"),
  9687. name);
  9688. }
  9689. for (int i = 4; i < Vendor_object_attributes::NUM_KNOWN_ATTRIBUTES; ++i)
  9690. {
  9691. // Merge this attribute with existing attributes.
  9692. switch (i)
  9693. {
  9694. case elfcpp::Tag_CPU_raw_name:
  9695. case elfcpp::Tag_CPU_name:
  9696. // These are merged after Tag_CPU_arch.
  9697. break;
  9698. case elfcpp::Tag_ABI_optimization_goals:
  9699. case elfcpp::Tag_ABI_FP_optimization_goals:
  9700. // Use the first value seen.
  9701. break;
  9702. case elfcpp::Tag_CPU_arch:
  9703. {
  9704. unsigned int saved_out_attr = out_attr->int_value();
  9705. // Merge Tag_CPU_arch and Tag_also_compatible_with.
  9706. int secondary_compat =
  9707. this->get_secondary_compatible_arch(pasd);
  9708. int secondary_compat_out =
  9709. this->get_secondary_compatible_arch(
  9710. this->attributes_section_data_);
  9711. out_attr[i].set_int_value(
  9712. tag_cpu_arch_combine(name, out_attr[i].int_value(),
  9713. &secondary_compat_out,
  9714. in_attr[i].int_value(),
  9715. secondary_compat));
  9716. this->set_secondary_compatible_arch(this->attributes_section_data_,
  9717. secondary_compat_out);
  9718. // Merge Tag_CPU_name and Tag_CPU_raw_name.
  9719. if (out_attr[i].int_value() == saved_out_attr)
  9720. ; // Leave the names alone.
  9721. else if (out_attr[i].int_value() == in_attr[i].int_value())
  9722. {
  9723. // The output architecture has been changed to match the
  9724. // input architecture. Use the input names.
  9725. out_attr[elfcpp::Tag_CPU_name].set_string_value(
  9726. in_attr[elfcpp::Tag_CPU_name].string_value());
  9727. out_attr[elfcpp::Tag_CPU_raw_name].set_string_value(
  9728. in_attr[elfcpp::Tag_CPU_raw_name].string_value());
  9729. }
  9730. else
  9731. {
  9732. out_attr[elfcpp::Tag_CPU_name].set_string_value("");
  9733. out_attr[elfcpp::Tag_CPU_raw_name].set_string_value("");
  9734. }
  9735. // If we still don't have a value for Tag_CPU_name,
  9736. // make one up now. Tag_CPU_raw_name remains blank.
  9737. if (out_attr[elfcpp::Tag_CPU_name].string_value() == "")
  9738. {
  9739. const std::string cpu_name =
  9740. this->tag_cpu_name_value(out_attr[i].int_value());
  9741. // FIXME: If we see an unknown CPU, this will be set
  9742. // to "<unknown CPU n>", where n is the attribute value.
  9743. // This is different from BFD, which leaves the name alone.
  9744. out_attr[elfcpp::Tag_CPU_name].set_string_value(cpu_name);
  9745. }
  9746. }
  9747. break;
  9748. case elfcpp::Tag_ARM_ISA_use:
  9749. case elfcpp::Tag_THUMB_ISA_use:
  9750. case elfcpp::Tag_WMMX_arch:
  9751. case elfcpp::Tag_Advanced_SIMD_arch:
  9752. // ??? Do Advanced_SIMD (NEON) and WMMX conflict?
  9753. case elfcpp::Tag_ABI_FP_rounding:
  9754. case elfcpp::Tag_ABI_FP_exceptions:
  9755. case elfcpp::Tag_ABI_FP_user_exceptions:
  9756. case elfcpp::Tag_ABI_FP_number_model:
  9757. case elfcpp::Tag_VFP_HP_extension:
  9758. case elfcpp::Tag_CPU_unaligned_access:
  9759. case elfcpp::Tag_T2EE_use:
  9760. case elfcpp::Tag_Virtualization_use:
  9761. case elfcpp::Tag_MPextension_use:
  9762. // Use the largest value specified.
  9763. if (in_attr[i].int_value() > out_attr[i].int_value())
  9764. out_attr[i].set_int_value(in_attr[i].int_value());
  9765. break;
  9766. case elfcpp::Tag_ABI_align8_preserved:
  9767. case elfcpp::Tag_ABI_PCS_RO_data:
  9768. // Use the smallest value specified.
  9769. if (in_attr[i].int_value() < out_attr[i].int_value())
  9770. out_attr[i].set_int_value(in_attr[i].int_value());
  9771. break;
  9772. case elfcpp::Tag_ABI_align8_needed:
  9773. if ((in_attr[i].int_value() > 0 || out_attr[i].int_value() > 0)
  9774. && (in_attr[elfcpp::Tag_ABI_align8_preserved].int_value() == 0
  9775. || (out_attr[elfcpp::Tag_ABI_align8_preserved].int_value()
  9776. == 0)))
  9777. {
  9778. // This error message should be enabled once all non-conforming
  9779. // binaries in the toolchain have had the attributes set
  9780. // properly.
  9781. // gold_error(_("output 8-byte data alignment conflicts with %s"),
  9782. // name);
  9783. }
  9784. // Fall through.
  9785. case elfcpp::Tag_ABI_FP_denormal:
  9786. case elfcpp::Tag_ABI_PCS_GOT_use:
  9787. {
  9788. // These tags have 0 = don't care, 1 = strong requirement,
  9789. // 2 = weak requirement.
  9790. static const int order_021[3] = {0, 2, 1};
  9791. // Use the "greatest" from the sequence 0, 2, 1, or the largest
  9792. // value if greater than 2 (for future-proofing).
  9793. if ((in_attr[i].int_value() > 2
  9794. && in_attr[i].int_value() > out_attr[i].int_value())
  9795. || (in_attr[i].int_value() <= 2
  9796. && out_attr[i].int_value() <= 2
  9797. && (order_021[in_attr[i].int_value()]
  9798. > order_021[out_attr[i].int_value()])))
  9799. out_attr[i].set_int_value(in_attr[i].int_value());
  9800. }
  9801. break;
  9802. case elfcpp::Tag_CPU_arch_profile:
  9803. if (out_attr[i].int_value() != in_attr[i].int_value())
  9804. {
  9805. // 0 will merge with anything.
  9806. // 'A' and 'S' merge to 'A'.
  9807. // 'R' and 'S' merge to 'R'.
  9808. // 'M' and 'A|R|S' is an error.
  9809. if (out_attr[i].int_value() == 0
  9810. || (out_attr[i].int_value() == 'S'
  9811. && (in_attr[i].int_value() == 'A'
  9812. || in_attr[i].int_value() == 'R')))
  9813. out_attr[i].set_int_value(in_attr[i].int_value());
  9814. else if (in_attr[i].int_value() == 0
  9815. || (in_attr[i].int_value() == 'S'
  9816. && (out_attr[i].int_value() == 'A'
  9817. || out_attr[i].int_value() == 'R')))
  9818. ; // Do nothing.
  9819. else if (parameters->options().warn_mismatch())
  9820. {
  9821. gold_error
  9822. (_("conflicting architecture profiles %c/%c"),
  9823. in_attr[i].int_value() ? in_attr[i].int_value() : '0',
  9824. out_attr[i].int_value() ? out_attr[i].int_value() : '0');
  9825. }
  9826. }
  9827. break;
  9828. case elfcpp::Tag_VFP_arch:
  9829. {
  9830. static const struct
  9831. {
  9832. int ver;
  9833. int regs;
  9834. } vfp_versions[7] =
  9835. {
  9836. {0, 0},
  9837. {1, 16},
  9838. {2, 16},
  9839. {3, 32},
  9840. {3, 16},
  9841. {4, 32},
  9842. {4, 16}
  9843. };
  9844. // Values greater than 6 aren't defined, so just pick the
  9845. // biggest.
  9846. if (in_attr[i].int_value() > 6
  9847. && in_attr[i].int_value() > out_attr[i].int_value())
  9848. {
  9849. *out_attr = *in_attr;
  9850. break;
  9851. }
  9852. // The output uses the superset of input features
  9853. // (ISA version) and registers.
  9854. int ver = std::max(vfp_versions[in_attr[i].int_value()].ver,
  9855. vfp_versions[out_attr[i].int_value()].ver);
  9856. int regs = std::max(vfp_versions[in_attr[i].int_value()].regs,
  9857. vfp_versions[out_attr[i].int_value()].regs);
  9858. // This assumes all possible supersets are also a valid
  9859. // options.
  9860. int newval;
  9861. for (newval = 6; newval > 0; newval--)
  9862. {
  9863. if (regs == vfp_versions[newval].regs
  9864. && ver == vfp_versions[newval].ver)
  9865. break;
  9866. }
  9867. out_attr[i].set_int_value(newval);
  9868. }
  9869. break;
  9870. case elfcpp::Tag_PCS_config:
  9871. if (out_attr[i].int_value() == 0)
  9872. out_attr[i].set_int_value(in_attr[i].int_value());
  9873. else if (in_attr[i].int_value() != 0
  9874. && out_attr[i].int_value() != 0
  9875. && parameters->options().warn_mismatch())
  9876. {
  9877. // It's sometimes ok to mix different configs, so this is only
  9878. // a warning.
  9879. gold_warning(_("%s: conflicting platform configuration"), name);
  9880. }
  9881. break;
  9882. case elfcpp::Tag_ABI_PCS_R9_use:
  9883. if (in_attr[i].int_value() != out_attr[i].int_value()
  9884. && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
  9885. && in_attr[i].int_value() != elfcpp::AEABI_R9_unused
  9886. && parameters->options().warn_mismatch())
  9887. {
  9888. gold_error(_("%s: conflicting use of R9"), name);
  9889. }
  9890. if (out_attr[i].int_value() == elfcpp::AEABI_R9_unused)
  9891. out_attr[i].set_int_value(in_attr[i].int_value());
  9892. break;
  9893. case elfcpp::Tag_ABI_PCS_RW_data:
  9894. if (in_attr[i].int_value() == elfcpp::AEABI_PCS_RW_data_SBrel
  9895. && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
  9896. != elfcpp::AEABI_R9_SB)
  9897. && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
  9898. != elfcpp::AEABI_R9_unused)
  9899. && parameters->options().warn_mismatch())
  9900. {
  9901. gold_error(_("%s: SB relative addressing conflicts with use "
  9902. "of R9"),
  9903. name);
  9904. }
  9905. // Use the smallest value specified.
  9906. if (in_attr[i].int_value() < out_attr[i].int_value())
  9907. out_attr[i].set_int_value(in_attr[i].int_value());
  9908. break;
  9909. case elfcpp::Tag_ABI_PCS_wchar_t:
  9910. if (out_attr[i].int_value()
  9911. && in_attr[i].int_value()
  9912. && out_attr[i].int_value() != in_attr[i].int_value()
  9913. && parameters->options().warn_mismatch()
  9914. && parameters->options().wchar_size_warning())
  9915. {
  9916. gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
  9917. "use %u-byte wchar_t; use of wchar_t values "
  9918. "across objects may fail"),
  9919. name, in_attr[i].int_value(),
  9920. out_attr[i].int_value());
  9921. }
  9922. else if (in_attr[i].int_value() && !out_attr[i].int_value())
  9923. out_attr[i].set_int_value(in_attr[i].int_value());
  9924. break;
  9925. case elfcpp::Tag_ABI_enum_size:
  9926. if (in_attr[i].int_value() != elfcpp::AEABI_enum_unused)
  9927. {
  9928. if (out_attr[i].int_value() == elfcpp::AEABI_enum_unused
  9929. || out_attr[i].int_value() == elfcpp::AEABI_enum_forced_wide)
  9930. {
  9931. // The existing object is compatible with anything.
  9932. // Use whatever requirements the new object has.
  9933. out_attr[i].set_int_value(in_attr[i].int_value());
  9934. }
  9935. else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
  9936. && out_attr[i].int_value() != in_attr[i].int_value()
  9937. && parameters->options().warn_mismatch()
  9938. && parameters->options().enum_size_warning())
  9939. {
  9940. unsigned int in_value = in_attr[i].int_value();
  9941. unsigned int out_value = out_attr[i].int_value();
  9942. gold_warning(_("%s uses %s enums yet the output is to use "
  9943. "%s enums; use of enum values across objects "
  9944. "may fail"),
  9945. name,
  9946. this->aeabi_enum_name(in_value).c_str(),
  9947. this->aeabi_enum_name(out_value).c_str());
  9948. }
  9949. }
  9950. break;
  9951. case elfcpp::Tag_ABI_VFP_args:
  9952. // Already done.
  9953. break;
  9954. case elfcpp::Tag_ABI_WMMX_args:
  9955. if (in_attr[i].int_value() != out_attr[i].int_value()
  9956. && parameters->options().warn_mismatch())
  9957. {
  9958. gold_error(_("%s uses iWMMXt register arguments, output does "
  9959. "not"),
  9960. name);
  9961. }
  9962. break;
  9963. case Object_attribute::Tag_compatibility:
  9964. // Merged in target-independent code.
  9965. break;
  9966. case elfcpp::Tag_ABI_HardFP_use:
  9967. // 1 (SP) and 2 (DP) conflict, so combine to 3 (SP & DP).
  9968. if ((in_attr[i].int_value() == 1 && out_attr[i].int_value() == 2)
  9969. || (in_attr[i].int_value() == 2 && out_attr[i].int_value() == 1))
  9970. out_attr[i].set_int_value(3);
  9971. else if (in_attr[i].int_value() > out_attr[i].int_value())
  9972. out_attr[i].set_int_value(in_attr[i].int_value());
  9973. break;
  9974. case elfcpp::Tag_ABI_FP_16bit_format:
  9975. if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
  9976. {
  9977. if (in_attr[i].int_value() != out_attr[i].int_value()
  9978. && parameters->options().warn_mismatch())
  9979. gold_error(_("fp16 format mismatch between %s and output"),
  9980. name);
  9981. }
  9982. if (in_attr[i].int_value() != 0)
  9983. out_attr[i].set_int_value(in_attr[i].int_value());
  9984. break;
  9985. case elfcpp::Tag_DIV_use:
  9986. {
  9987. // A value of zero on input means that the divide
  9988. // instruction may be used if available in the base
  9989. // architecture as specified via Tag_CPU_arch and
  9990. // Tag_CPU_arch_profile. A value of 1 means that the user
  9991. // did not want divide instructions. A value of 2
  9992. // explicitly means that divide instructions were allowed
  9993. // in ARM and Thumb state.
  9994. int arch = this->
  9995. get_aeabi_object_attribute(elfcpp::Tag_CPU_arch)->
  9996. int_value();
  9997. int profile = this->
  9998. get_aeabi_object_attribute(elfcpp::Tag_CPU_arch_profile)->
  9999. int_value();
  10000. if (in_attr[i].int_value() == out_attr[i].int_value())
  10001. {
  10002. // Do nothing.
  10003. }
  10004. else if (attributes_forbid_div(&in_attr[i])
  10005. && !attributes_accept_div(arch, profile, &out_attr[i]))
  10006. out_attr[i].set_int_value(1);
  10007. else if (attributes_forbid_div(&out_attr[i])
  10008. && attributes_accept_div(arch, profile, &in_attr[i]))
  10009. out_attr[i].set_int_value(in_attr[i].int_value());
  10010. else if (in_attr[i].int_value() == 2)
  10011. out_attr[i].set_int_value(in_attr[i].int_value());
  10012. }
  10013. break;
  10014. case elfcpp::Tag_MPextension_use_legacy:
  10015. // We don't output objects with Tag_MPextension_use_legacy - we
  10016. // move the value to Tag_MPextension_use.
  10017. if (in_attr[i].int_value() != 0
  10018. && in_attr[elfcpp::Tag_MPextension_use].int_value() != 0)
  10019. {
  10020. if (in_attr[elfcpp::Tag_MPextension_use].int_value()
  10021. != in_attr[i].int_value())
  10022. {
  10023. gold_error(_("%s has has both the current and legacy "
  10024. "Tag_MPextension_use attributes"),
  10025. name);
  10026. }
  10027. }
  10028. if (in_attr[i].int_value()
  10029. > out_attr[elfcpp::Tag_MPextension_use].int_value())
  10030. out_attr[elfcpp::Tag_MPextension_use] = in_attr[i];
  10031. break;
  10032. case elfcpp::Tag_nodefaults:
  10033. // This tag is set if it exists, but the value is unused (and is
  10034. // typically zero). We don't actually need to do anything here -
  10035. // the merge happens automatically when the type flags are merged
  10036. // below.
  10037. break;
  10038. case elfcpp::Tag_also_compatible_with:
  10039. // Already done in Tag_CPU_arch.
  10040. break;
  10041. case elfcpp::Tag_conformance:
  10042. // Keep the attribute if it matches. Throw it away otherwise.
  10043. // No attribute means no claim to conform.
  10044. if (in_attr[i].string_value() != out_attr[i].string_value())
  10045. out_attr[i].set_string_value("");
  10046. break;
  10047. default:
  10048. {
  10049. const char* err_object = NULL;
  10050. // The "known_obj_attributes" table does contain some undefined
  10051. // attributes. Ensure that there are unused.
  10052. if (out_attr[i].int_value() != 0
  10053. || out_attr[i].string_value() != "")
  10054. err_object = "output";
  10055. else if (in_attr[i].int_value() != 0
  10056. || in_attr[i].string_value() != "")
  10057. err_object = name;
  10058. if (err_object != NULL
  10059. && parameters->options().warn_mismatch())
  10060. {
  10061. // Attribute numbers >=64 (mod 128) can be safely ignored.
  10062. if ((i & 127) < 64)
  10063. gold_error(_("%s: unknown mandatory EABI object attribute "
  10064. "%d"),
  10065. err_object, i);
  10066. else
  10067. gold_warning(_("%s: unknown EABI object attribute %d"),
  10068. err_object, i);
  10069. }
  10070. // Only pass on attributes that match in both inputs.
  10071. if (!in_attr[i].matches(out_attr[i]))
  10072. {
  10073. out_attr[i].set_int_value(0);
  10074. out_attr[i].set_string_value("");
  10075. }
  10076. }
  10077. }
  10078. // If out_attr was copied from in_attr then it won't have a type yet.
  10079. if (in_attr[i].type() && !out_attr[i].type())
  10080. out_attr[i].set_type(in_attr[i].type());
  10081. }
  10082. // Merge Tag_compatibility attributes and any common GNU ones.
  10083. this->attributes_section_data_->merge(name, pasd);
  10084. // Check for any attributes not known on ARM.
  10085. typedef Vendor_object_attributes::Other_attributes Other_attributes;
  10086. const Other_attributes* in_other_attributes = pasd->other_attributes(vendor);
  10087. Other_attributes::const_iterator in_iter = in_other_attributes->begin();
  10088. Other_attributes* out_other_attributes =
  10089. this->attributes_section_data_->other_attributes(vendor);
  10090. Other_attributes::iterator out_iter = out_other_attributes->begin();
  10091. while (in_iter != in_other_attributes->end()
  10092. || out_iter != out_other_attributes->end())
  10093. {
  10094. const char* err_object = NULL;
  10095. int err_tag = 0;
  10096. // The tags for each list are in numerical order.
  10097. // If the tags are equal, then merge.
  10098. if (out_iter != out_other_attributes->end()
  10099. && (in_iter == in_other_attributes->end()
  10100. || in_iter->first > out_iter->first))
  10101. {
  10102. // This attribute only exists in output. We can't merge, and we
  10103. // don't know what the tag means, so delete it.
  10104. err_object = "output";
  10105. err_tag = out_iter->first;
  10106. int saved_tag = out_iter->first;
  10107. delete out_iter->second;
  10108. out_other_attributes->erase(out_iter);
  10109. out_iter = out_other_attributes->upper_bound(saved_tag);
  10110. }
  10111. else if (in_iter != in_other_attributes->end()
  10112. && (out_iter != out_other_attributes->end()
  10113. || in_iter->first < out_iter->first))
  10114. {
  10115. // This attribute only exists in input. We can't merge, and we
  10116. // don't know what the tag means, so ignore it.
  10117. err_object = name;
  10118. err_tag = in_iter->first;
  10119. ++in_iter;
  10120. }
  10121. else // The tags are equal.
  10122. {
  10123. // As present, all attributes in the list are unknown, and
  10124. // therefore can't be merged meaningfully.
  10125. err_object = "output";
  10126. err_tag = out_iter->first;
  10127. // Only pass on attributes that match in both inputs.
  10128. if (!in_iter->second->matches(*(out_iter->second)))
  10129. {
  10130. // No match. Delete the attribute.
  10131. int saved_tag = out_iter->first;
  10132. delete out_iter->second;
  10133. out_other_attributes->erase(out_iter);
  10134. out_iter = out_other_attributes->upper_bound(saved_tag);
  10135. }
  10136. else
  10137. {
  10138. // Matched. Keep the attribute and move to the next.
  10139. ++out_iter;
  10140. ++in_iter;
  10141. }
  10142. }
  10143. if (err_object && parameters->options().warn_mismatch())
  10144. {
  10145. // Attribute numbers >=64 (mod 128) can be safely ignored. */
  10146. if ((err_tag & 127) < 64)
  10147. {
  10148. gold_error(_("%s: unknown mandatory EABI object attribute %d"),
  10149. err_object, err_tag);
  10150. }
  10151. else
  10152. {
  10153. gold_warning(_("%s: unknown EABI object attribute %d"),
  10154. err_object, err_tag);
  10155. }
  10156. }
  10157. }
  10158. }
  10159. // Stub-generation methods for Target_arm.
  10160. // Make a new Arm_input_section object.
  10161. template<bool big_endian>
  10162. Arm_input_section<big_endian>*
  10163. Target_arm<big_endian>::new_arm_input_section(
  10164. Relobj* relobj,
  10165. unsigned int shndx)
  10166. {
  10167. Section_id sid(relobj, shndx);
  10168. Arm_input_section<big_endian>* arm_input_section =
  10169. new Arm_input_section<big_endian>(relobj, shndx);
  10170. arm_input_section->init();
  10171. // Register new Arm_input_section in map for look-up.
  10172. std::pair<typename Arm_input_section_map::iterator, bool> ins =
  10173. this->arm_input_section_map_.insert(std::make_pair(sid, arm_input_section));
  10174. // Make sure that it we have not created another Arm_input_section
  10175. // for this input section already.
  10176. gold_assert(ins.second);
  10177. return arm_input_section;
  10178. }
  10179. // Find the Arm_input_section object corresponding to the SHNDX-th input
  10180. // section of RELOBJ.
  10181. template<bool big_endian>
  10182. Arm_input_section<big_endian>*
  10183. Target_arm<big_endian>::find_arm_input_section(
  10184. Relobj* relobj,
  10185. unsigned int shndx) const
  10186. {
  10187. Section_id sid(relobj, shndx);
  10188. typename Arm_input_section_map::const_iterator p =
  10189. this->arm_input_section_map_.find(sid);
  10190. return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
  10191. }
  10192. // Make a new stub table.
  10193. template<bool big_endian>
  10194. Stub_table<big_endian>*
  10195. Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
  10196. {
  10197. Stub_table<big_endian>* stub_table =
  10198. new Stub_table<big_endian>(owner);
  10199. this->stub_tables_.push_back(stub_table);
  10200. stub_table->set_address(owner->address() + owner->data_size());
  10201. stub_table->set_file_offset(owner->offset() + owner->data_size());
  10202. stub_table->finalize_data_size();
  10203. return stub_table;
  10204. }
  10205. // Scan a relocation for stub generation.
  10206. template<bool big_endian>
  10207. void
  10208. Target_arm<big_endian>::scan_reloc_for_stub(
  10209. const Relocate_info<32, big_endian>* relinfo,
  10210. unsigned int r_type,
  10211. const Sized_symbol<32>* gsym,
  10212. unsigned int r_sym,
  10213. const Symbol_value<32>* psymval,
  10214. elfcpp::Elf_types<32>::Elf_Swxword addend,
  10215. Arm_address address)
  10216. {
  10217. const Arm_relobj<big_endian>* arm_relobj =
  10218. Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
  10219. bool target_is_thumb;
  10220. Symbol_value<32> symval;
  10221. if (gsym != NULL)
  10222. {
  10223. // This is a global symbol. Determine if we use PLT and if the
  10224. // final target is THUMB.
  10225. if (gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
  10226. {
  10227. // This uses a PLT, change the symbol value.
  10228. symval.set_output_value(this->plt_address_for_global(gsym));
  10229. psymval = &symval;
  10230. target_is_thumb = false;
  10231. }
  10232. else if (gsym->is_undefined())
  10233. // There is no need to generate a stub symbol is undefined.
  10234. return;
  10235. else
  10236. {
  10237. target_is_thumb =
  10238. ((gsym->type() == elfcpp::STT_ARM_TFUNC)
  10239. || (gsym->type() == elfcpp::STT_FUNC
  10240. && !gsym->is_undefined()
  10241. && ((psymval->value(arm_relobj, 0) & 1) != 0)));
  10242. }
  10243. }
  10244. else
  10245. {
  10246. // This is a local symbol. Determine if the final target is THUMB.
  10247. target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
  10248. }
  10249. // Strip LSB if this points to a THUMB target.
  10250. const Arm_reloc_property* reloc_property =
  10251. arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
  10252. gold_assert(reloc_property != NULL);
  10253. if (target_is_thumb
  10254. && reloc_property->uses_thumb_bit()
  10255. && ((psymval->value(arm_relobj, 0) & 1) != 0))
  10256. {
  10257. Arm_address stripped_value =
  10258. psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
  10259. symval.set_output_value(stripped_value);
  10260. psymval = &symval;
  10261. }
  10262. // Get the symbol value.
  10263. Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
  10264. // Owing to pipelining, the PC relative branches below actually skip
  10265. // two instructions when the branch offset is 0.
  10266. Arm_address destination;
  10267. switch (r_type)
  10268. {
  10269. case elfcpp::R_ARM_CALL:
  10270. case elfcpp::R_ARM_JUMP24:
  10271. case elfcpp::R_ARM_PLT32:
  10272. // ARM branches.
  10273. destination = value + addend + 8;
  10274. break;
  10275. case elfcpp::R_ARM_THM_CALL:
  10276. case elfcpp::R_ARM_THM_XPC22:
  10277. case elfcpp::R_ARM_THM_JUMP24:
  10278. case elfcpp::R_ARM_THM_JUMP19:
  10279. // THUMB branches.
  10280. destination = value + addend + 4;
  10281. break;
  10282. default:
  10283. gold_unreachable();
  10284. }
  10285. Reloc_stub* stub = NULL;
  10286. Stub_type stub_type =
  10287. Reloc_stub::stub_type_for_reloc(r_type, address, destination,
  10288. target_is_thumb);
  10289. if (stub_type != arm_stub_none)
  10290. {
  10291. // Try looking up an existing stub from a stub table.
  10292. Stub_table<big_endian>* stub_table =
  10293. arm_relobj->stub_table(relinfo->data_shndx);
  10294. gold_assert(stub_table != NULL);
  10295. // Locate stub by destination.
  10296. Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
  10297. // Create a stub if there is not one already
  10298. stub = stub_table->find_reloc_stub(stub_key);
  10299. if (stub == NULL)
  10300. {
  10301. // create a new stub and add it to stub table.
  10302. stub = this->stub_factory().make_reloc_stub(stub_type);
  10303. stub_table->add_reloc_stub(stub, stub_key);
  10304. }
  10305. // Record the destination address.
  10306. stub->set_destination_address(destination
  10307. | (target_is_thumb ? 1 : 0));
  10308. }
  10309. // For Cortex-A8, we need to record a relocation at 4K page boundary.
  10310. if (this->fix_cortex_a8_
  10311. && (r_type == elfcpp::R_ARM_THM_JUMP24
  10312. || r_type == elfcpp::R_ARM_THM_JUMP19
  10313. || r_type == elfcpp::R_ARM_THM_CALL
  10314. || r_type == elfcpp::R_ARM_THM_XPC22)
  10315. && (address & 0xfffU) == 0xffeU)
  10316. {
  10317. // Found a candidate. Note we haven't checked the destination is
  10318. // within 4K here: if we do so (and don't create a record) we can't
  10319. // tell that a branch should have been relocated when scanning later.
  10320. this->cortex_a8_relocs_info_[address] =
  10321. new Cortex_a8_reloc(stub, r_type,
  10322. destination | (target_is_thumb ? 1 : 0));
  10323. }
  10324. }
  10325. // This function scans a relocation sections for stub generation.
  10326. // The template parameter Relocate must be a class type which provides
  10327. // a single function, relocate(), which implements the machine
  10328. // specific part of a relocation.
  10329. // BIG_ENDIAN is the endianness of the data. SH_TYPE is the section type:
  10330. // SHT_REL or SHT_RELA.
  10331. // PRELOCS points to the relocation data. RELOC_COUNT is the number
  10332. // of relocs. OUTPUT_SECTION is the output section.
  10333. // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
  10334. // mapped to output offsets.
  10335. // VIEW is the section data, VIEW_ADDRESS is its memory address, and
  10336. // VIEW_SIZE is the size. These refer to the input section, unless
  10337. // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
  10338. // the output section.
  10339. template<bool big_endian>
  10340. template<int sh_type>
  10341. void inline
  10342. Target_arm<big_endian>::scan_reloc_section_for_stubs(
  10343. const Relocate_info<32, big_endian>* relinfo,
  10344. const unsigned char* prelocs,
  10345. size_t reloc_count,
  10346. Output_section* output_section,
  10347. bool needs_special_offset_handling,
  10348. const unsigned char* view,
  10349. elfcpp::Elf_types<32>::Elf_Addr view_address,
  10350. section_size_type)
  10351. {
  10352. typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
  10353. const int reloc_size =
  10354. Reloc_types<sh_type, 32, big_endian>::reloc_size;
  10355. Arm_relobj<big_endian>* arm_object =
  10356. Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
  10357. unsigned int local_count = arm_object->local_symbol_count();
  10358. gold::Default_comdat_behavior default_comdat_behavior;
  10359. Comdat_behavior comdat_behavior = CB_UNDETERMINED;
  10360. for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
  10361. {
  10362. Reltype reloc(prelocs);
  10363. typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
  10364. unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
  10365. unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
  10366. r_type = this->get_real_reloc_type(r_type);
  10367. // Only a few relocation types need stubs.
  10368. if ((r_type != elfcpp::R_ARM_CALL)
  10369. && (r_type != elfcpp::R_ARM_JUMP24)
  10370. && (r_type != elfcpp::R_ARM_PLT32)
  10371. && (r_type != elfcpp::R_ARM_THM_CALL)
  10372. && (r_type != elfcpp::R_ARM_THM_XPC22)
  10373. && (r_type != elfcpp::R_ARM_THM_JUMP24)
  10374. && (r_type != elfcpp::R_ARM_THM_JUMP19)
  10375. && (r_type != elfcpp::R_ARM_V4BX))
  10376. continue;
  10377. section_offset_type offset =
  10378. convert_to_section_size_type(reloc.get_r_offset());
  10379. if (needs_special_offset_handling)
  10380. {
  10381. offset = output_section->output_offset(relinfo->object,
  10382. relinfo->data_shndx,
  10383. offset);
  10384. if (offset == -1)
  10385. continue;
  10386. }
  10387. // Create a v4bx stub if --fix-v4bx-interworking is used.
  10388. if (r_type == elfcpp::R_ARM_V4BX)
  10389. {
  10390. if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING)
  10391. {
  10392. // Get the BX instruction.
  10393. typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
  10394. const Valtype* wv =
  10395. reinterpret_cast<const Valtype*>(view + offset);
  10396. elfcpp::Elf_types<32>::Elf_Swxword insn =
  10397. elfcpp::Swap<32, big_endian>::readval(wv);
  10398. const uint32_t reg = (insn & 0xf);
  10399. if (reg < 0xf)
  10400. {
  10401. // Try looking up an existing stub from a stub table.
  10402. Stub_table<big_endian>* stub_table =
  10403. arm_object->stub_table(relinfo->data_shndx);
  10404. gold_assert(stub_table != NULL);
  10405. if (stub_table->find_arm_v4bx_stub(reg) == NULL)
  10406. {
  10407. // create a new stub and add it to stub table.
  10408. Arm_v4bx_stub* stub =
  10409. this->stub_factory().make_arm_v4bx_stub(reg);
  10410. gold_assert(stub != NULL);
  10411. stub_table->add_arm_v4bx_stub(stub);
  10412. }
  10413. }
  10414. }
  10415. continue;
  10416. }
  10417. // Get the addend.
  10418. Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
  10419. elfcpp::Elf_types<32>::Elf_Swxword addend =
  10420. stub_addend_reader(r_type, view + offset, reloc);
  10421. const Sized_symbol<32>* sym;
  10422. Symbol_value<32> symval;
  10423. const Symbol_value<32> *psymval;
  10424. bool is_defined_in_discarded_section;
  10425. unsigned int shndx;
  10426. if (r_sym < local_count)
  10427. {
  10428. sym = NULL;
  10429. psymval = arm_object->local_symbol(r_sym);
  10430. // If the local symbol belongs to a section we are discarding,
  10431. // and that section is a debug section, try to find the
  10432. // corresponding kept section and map this symbol to its
  10433. // counterpart in the kept section. The symbol must not
  10434. // correspond to a section we are folding.
  10435. bool is_ordinary;
  10436. shndx = psymval->input_shndx(&is_ordinary);
  10437. is_defined_in_discarded_section =
  10438. (is_ordinary
  10439. && shndx != elfcpp::SHN_UNDEF
  10440. && !arm_object->is_section_included(shndx)
  10441. && !relinfo->symtab->is_section_folded(arm_object, shndx));
  10442. // We need to compute the would-be final value of this local
  10443. // symbol.
  10444. if (!is_defined_in_discarded_section)
  10445. {
  10446. typedef Sized_relobj_file<32, big_endian> ObjType;
  10447. typename ObjType::Compute_final_local_value_status status =
  10448. arm_object->compute_final_local_value(r_sym, psymval, &symval,
  10449. relinfo->symtab);
  10450. if (status == ObjType::CFLV_OK)
  10451. {
  10452. // Currently we cannot handle a branch to a target in
  10453. // a merged section. If this is the case, issue an error
  10454. // and also free the merge symbol value.
  10455. if (!symval.has_output_value())
  10456. {
  10457. const std::string& section_name =
  10458. arm_object->section_name(shndx);
  10459. arm_object->error(_("cannot handle branch to local %u "
  10460. "in a merged section %s"),
  10461. r_sym, section_name.c_str());
  10462. }
  10463. psymval = &symval;
  10464. }
  10465. else
  10466. {
  10467. // We cannot determine the final value.
  10468. continue;
  10469. }
  10470. }
  10471. }
  10472. else
  10473. {
  10474. const Symbol* gsym;
  10475. gsym = arm_object->global_symbol(r_sym);
  10476. gold_assert(gsym != NULL);
  10477. if (gsym->is_forwarder())
  10478. gsym = relinfo->symtab->resolve_forwards(gsym);
  10479. sym = static_cast<const Sized_symbol<32>*>(gsym);
  10480. if (sym->has_symtab_index() && sym->symtab_index() != -1U)
  10481. symval.set_output_symtab_index(sym->symtab_index());
  10482. else
  10483. symval.set_no_output_symtab_entry();
  10484. // We need to compute the would-be final value of this global
  10485. // symbol.
  10486. const Symbol_table* symtab = relinfo->symtab;
  10487. const Sized_symbol<32>* sized_symbol =
  10488. symtab->get_sized_symbol<32>(gsym);
  10489. Symbol_table::Compute_final_value_status status;
  10490. Arm_address value =
  10491. symtab->compute_final_value<32>(sized_symbol, &status);
  10492. // Skip this if the symbol has not output section.
  10493. if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
  10494. continue;
  10495. symval.set_output_value(value);
  10496. if (gsym->type() == elfcpp::STT_TLS)
  10497. symval.set_is_tls_symbol();
  10498. else if (gsym->type() == elfcpp::STT_GNU_IFUNC)
  10499. symval.set_is_ifunc_symbol();
  10500. psymval = &symval;
  10501. is_defined_in_discarded_section =
  10502. (gsym->is_defined_in_discarded_section()
  10503. && gsym->is_undefined());
  10504. shndx = 0;
  10505. }
  10506. Symbol_value<32> symval2;
  10507. if (is_defined_in_discarded_section)
  10508. {
  10509. if (comdat_behavior == CB_UNDETERMINED)
  10510. {
  10511. std::string name = arm_object->section_name(relinfo->data_shndx);
  10512. comdat_behavior = default_comdat_behavior.get(name.c_str());
  10513. }
  10514. if (comdat_behavior == CB_PRETEND)
  10515. {
  10516. // FIXME: This case does not work for global symbols.
  10517. // We have no place to store the original section index.
  10518. // Fortunately this does not matter for comdat sections,
  10519. // only for sections explicitly discarded by a linker
  10520. // script.
  10521. bool found;
  10522. typename elfcpp::Elf_types<32>::Elf_Addr value =
  10523. arm_object->map_to_kept_section(shndx, &found);
  10524. if (found)
  10525. symval2.set_output_value(value + psymval->input_value());
  10526. else
  10527. symval2.set_output_value(0);
  10528. }
  10529. else
  10530. {
  10531. if (comdat_behavior == CB_WARNING)
  10532. gold_warning_at_location(relinfo, i, offset,
  10533. _("relocation refers to discarded "
  10534. "section"));
  10535. symval2.set_output_value(0);
  10536. }
  10537. symval2.set_no_output_symtab_entry();
  10538. psymval = &symval2;
  10539. }
  10540. // If symbol is a section symbol, we don't know the actual type of
  10541. // destination. Give up.
  10542. if (psymval->is_section_symbol())
  10543. continue;
  10544. this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
  10545. addend, view_address + offset);
  10546. }
  10547. }
  10548. // Scan an input section for stub generation.
  10549. template<bool big_endian>
  10550. void
  10551. Target_arm<big_endian>::scan_section_for_stubs(
  10552. const Relocate_info<32, big_endian>* relinfo,
  10553. unsigned int sh_type,
  10554. const unsigned char* prelocs,
  10555. size_t reloc_count,
  10556. Output_section* output_section,
  10557. bool needs_special_offset_handling,
  10558. const unsigned char* view,
  10559. Arm_address view_address,
  10560. section_size_type view_size)
  10561. {
  10562. if (sh_type == elfcpp::SHT_REL)
  10563. this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
  10564. relinfo,
  10565. prelocs,
  10566. reloc_count,
  10567. output_section,
  10568. needs_special_offset_handling,
  10569. view,
  10570. view_address,
  10571. view_size);
  10572. else if (sh_type == elfcpp::SHT_RELA)
  10573. // We do not support RELA type relocations yet. This is provided for
  10574. // completeness.
  10575. this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
  10576. relinfo,
  10577. prelocs,
  10578. reloc_count,
  10579. output_section,
  10580. needs_special_offset_handling,
  10581. view,
  10582. view_address,
  10583. view_size);
  10584. else
  10585. gold_unreachable();
  10586. }
  10587. // Group input sections for stub generation.
  10588. //
  10589. // We group input sections in an output section so that the total size,
  10590. // including any padding space due to alignment is smaller than GROUP_SIZE
  10591. // unless the only input section in group is bigger than GROUP_SIZE already.
  10592. // Then an ARM stub table is created to follow the last input section
  10593. // in group. For each group an ARM stub table is created an is placed
  10594. // after the last group. If STUB_ALWAYS_AFTER_BRANCH is false, we further
  10595. // extend the group after the stub table.
  10596. template<bool big_endian>
  10597. void
  10598. Target_arm<big_endian>::group_sections(
  10599. Layout* layout,
  10600. section_size_type group_size,
  10601. bool stubs_always_after_branch,
  10602. const Task* task)
  10603. {
  10604. // Group input sections and insert stub table
  10605. Layout::Section_list section_list;
  10606. layout->get_executable_sections(&section_list);
  10607. for (Layout::Section_list::const_iterator p = section_list.begin();
  10608. p != section_list.end();
  10609. ++p)
  10610. {
  10611. Arm_output_section<big_endian>* output_section =
  10612. Arm_output_section<big_endian>::as_arm_output_section(*p);
  10613. output_section->group_sections(group_size, stubs_always_after_branch,
  10614. this, task);
  10615. }
  10616. }
  10617. // Relaxation hook. This is where we do stub generation.
  10618. template<bool big_endian>
  10619. bool
  10620. Target_arm<big_endian>::do_relax(
  10621. int pass,
  10622. const Input_objects* input_objects,
  10623. Symbol_table* symtab,
  10624. Layout* layout,
  10625. const Task* task)
  10626. {
  10627. // No need to generate stubs if this is a relocatable link.
  10628. gold_assert(!parameters->options().relocatable());
  10629. // If this is the first pass, we need to group input sections into
  10630. // stub groups.
  10631. bool done_exidx_fixup = false;
  10632. typedef typename Stub_table_list::iterator Stub_table_iterator;
  10633. if (pass == 1)
  10634. {
  10635. // Determine the stub group size. The group size is the absolute
  10636. // value of the parameter --stub-group-size. If --stub-group-size
  10637. // is passed a negative value, we restrict stubs to be always after
  10638. // the stubbed branches.
  10639. int32_t stub_group_size_param =
  10640. parameters->options().stub_group_size();
  10641. bool stubs_always_after_branch = stub_group_size_param < 0;
  10642. section_size_type stub_group_size = abs(stub_group_size_param);
  10643. if (stub_group_size == 1)
  10644. {
  10645. // Default value.
  10646. // Thumb branch range is +-4MB has to be used as the default
  10647. // maximum size (a given section can contain both ARM and Thumb
  10648. // code, so the worst case has to be taken into account). If we are
  10649. // fixing cortex-a8 errata, the branch range has to be even smaller,
  10650. // since wide conditional branch has a range of +-1MB only.
  10651. //
  10652. // This value is 48K less than that, which allows for 4096
  10653. // 12-byte stubs. If we exceed that, then we will fail to link.
  10654. // The user will have to relink with an explicit group size
  10655. // option.
  10656. stub_group_size = 4145152;
  10657. }
  10658. // The Cortex-A8 erratum fix depends on stubs not being in the same 4K
  10659. // page as the first half of a 32-bit branch straddling two 4K pages.
  10660. // This is a crude way of enforcing that. In addition, long conditional
  10661. // branches of THUMB-2 have a range of +-1M. If we are fixing cortex-A8
  10662. // erratum, limit the group size to (1M - 12k) to avoid unreachable
  10663. // cortex-A8 stubs from long conditional branches.
  10664. if (this->fix_cortex_a8_)
  10665. {
  10666. stubs_always_after_branch = true;
  10667. const section_size_type cortex_a8_group_size = 1024 * (1024 - 12);
  10668. stub_group_size = std::max(stub_group_size, cortex_a8_group_size);
  10669. }
  10670. group_sections(layout, stub_group_size, stubs_always_after_branch, task);
  10671. // Also fix .ARM.exidx section coverage.
  10672. Arm_output_section<big_endian>* exidx_output_section = NULL;
  10673. for (Layout::Section_list::const_iterator p =
  10674. layout->section_list().begin();
  10675. p != layout->section_list().end();
  10676. ++p)
  10677. if ((*p)->type() == elfcpp::SHT_ARM_EXIDX)
  10678. {
  10679. if (exidx_output_section == NULL)
  10680. exidx_output_section =
  10681. Arm_output_section<big_endian>::as_arm_output_section(*p);
  10682. else
  10683. // We cannot handle this now.
  10684. gold_error(_("multiple SHT_ARM_EXIDX sections %s and %s in a "
  10685. "non-relocatable link"),
  10686. exidx_output_section->name(),
  10687. (*p)->name());
  10688. }
  10689. if (exidx_output_section != NULL)
  10690. {
  10691. this->fix_exidx_coverage(layout, input_objects, exidx_output_section,
  10692. symtab, task);
  10693. done_exidx_fixup = true;
  10694. }
  10695. }
  10696. else
  10697. {
  10698. // If this is not the first pass, addresses and file offsets have
  10699. // been reset at this point, set them here.
  10700. for (Stub_table_iterator sp = this->stub_tables_.begin();
  10701. sp != this->stub_tables_.end();
  10702. ++sp)
  10703. {
  10704. Arm_input_section<big_endian>* owner = (*sp)->owner();
  10705. off_t off = align_address(owner->original_size(),
  10706. (*sp)->addralign());
  10707. (*sp)->set_address_and_file_offset(owner->address() + off,
  10708. owner->offset() + off);
  10709. }
  10710. }
  10711. // The Cortex-A8 stubs are sensitive to layout of code sections. At the
  10712. // beginning of each relaxation pass, just blow away all the stubs.
  10713. // Alternatively, we could selectively remove only the stubs and reloc
  10714. // information for code sections that have moved since the last pass.
  10715. // That would require more book-keeping.
  10716. if (this->fix_cortex_a8_)
  10717. {
  10718. // Clear all Cortex-A8 reloc information.
  10719. for (typename Cortex_a8_relocs_info::const_iterator p =
  10720. this->cortex_a8_relocs_info_.begin();
  10721. p != this->cortex_a8_relocs_info_.end();
  10722. ++p)
  10723. delete p->second;
  10724. this->cortex_a8_relocs_info_.clear();
  10725. // Remove all Cortex-A8 stubs.
  10726. for (Stub_table_iterator sp = this->stub_tables_.begin();
  10727. sp != this->stub_tables_.end();
  10728. ++sp)
  10729. (*sp)->remove_all_cortex_a8_stubs();
  10730. }
  10731. // Scan relocs for relocation stubs
  10732. for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
  10733. op != input_objects->relobj_end();
  10734. ++op)
  10735. {
  10736. Arm_relobj<big_endian>* arm_relobj =
  10737. Arm_relobj<big_endian>::as_arm_relobj(*op);
  10738. // Lock the object so we can read from it. This is only called
  10739. // single-threaded from Layout::finalize, so it is OK to lock.
  10740. Task_lock_obj<Object> tl(task, arm_relobj);
  10741. arm_relobj->scan_sections_for_stubs(this, symtab, layout);
  10742. }
  10743. // Check all stub tables to see if any of them have their data sizes
  10744. // or addresses alignments changed. These are the only things that
  10745. // matter.
  10746. bool any_stub_table_changed = false;
  10747. Unordered_set<const Output_section*> sections_needing_adjustment;
  10748. for (Stub_table_iterator sp = this->stub_tables_.begin();
  10749. (sp != this->stub_tables_.end()) && !any_stub_table_changed;
  10750. ++sp)
  10751. {
  10752. if ((*sp)->update_data_size_and_addralign())
  10753. {
  10754. // Update data size of stub table owner.
  10755. Arm_input_section<big_endian>* owner = (*sp)->owner();
  10756. uint64_t address = owner->address();
  10757. off_t offset = owner->offset();
  10758. owner->reset_address_and_file_offset();
  10759. owner->set_address_and_file_offset(address, offset);
  10760. sections_needing_adjustment.insert(owner->output_section());
  10761. any_stub_table_changed = true;
  10762. }
  10763. }
  10764. // Output_section_data::output_section() returns a const pointer but we
  10765. // need to update output sections, so we record all output sections needing
  10766. // update above and scan the sections here to find out what sections need
  10767. // to be updated.
  10768. for (Layout::Section_list::const_iterator p = layout->section_list().begin();
  10769. p != layout->section_list().end();
  10770. ++p)
  10771. {
  10772. if (sections_needing_adjustment.find(*p)
  10773. != sections_needing_adjustment.end())
  10774. (*p)->set_section_offsets_need_adjustment();
  10775. }
  10776. // Stop relaxation if no EXIDX fix-up and no stub table change.
  10777. bool continue_relaxation = done_exidx_fixup || any_stub_table_changed;
  10778. // Finalize the stubs in the last relaxation pass.
  10779. if (!continue_relaxation)
  10780. {
  10781. for (Stub_table_iterator sp = this->stub_tables_.begin();
  10782. (sp != this->stub_tables_.end()) && !any_stub_table_changed;
  10783. ++sp)
  10784. (*sp)->finalize_stubs();
  10785. // Update output local symbol counts of objects if necessary.
  10786. for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
  10787. op != input_objects->relobj_end();
  10788. ++op)
  10789. {
  10790. Arm_relobj<big_endian>* arm_relobj =
  10791. Arm_relobj<big_endian>::as_arm_relobj(*op);
  10792. // Update output local symbol counts. We need to discard local
  10793. // symbols defined in parts of input sections that are discarded by
  10794. // relaxation.
  10795. if (arm_relobj->output_local_symbol_count_needs_update())
  10796. {
  10797. // We need to lock the object's file to update it.
  10798. Task_lock_obj<Object> tl(task, arm_relobj);
  10799. arm_relobj->update_output_local_symbol_count();
  10800. }
  10801. }
  10802. }
  10803. return continue_relaxation;
  10804. }
  10805. // Relocate a stub.
  10806. template<bool big_endian>
  10807. void
  10808. Target_arm<big_endian>::relocate_stub(
  10809. Stub* stub,
  10810. const Relocate_info<32, big_endian>* relinfo,
  10811. Output_section* output_section,
  10812. unsigned char* view,
  10813. Arm_address address,
  10814. section_size_type view_size)
  10815. {
  10816. Relocate relocate;
  10817. const Stub_template* stub_template = stub->stub_template();
  10818. for (size_t i = 0; i < stub_template->reloc_count(); i++)
  10819. {
  10820. size_t reloc_insn_index = stub_template->reloc_insn_index(i);
  10821. const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
  10822. unsigned int r_type = insn->r_type();
  10823. section_size_type reloc_offset = stub_template->reloc_offset(i);
  10824. section_size_type reloc_size = insn->size();
  10825. gold_assert(reloc_offset + reloc_size <= view_size);
  10826. // This is the address of the stub destination.
  10827. Arm_address target = stub->reloc_target(i) + insn->reloc_addend();
  10828. Symbol_value<32> symval;
  10829. symval.set_output_value(target);
  10830. // Synthesize a fake reloc just in case. We don't have a symbol so
  10831. // we use 0.
  10832. unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
  10833. memset(reloc_buffer, 0, sizeof(reloc_buffer));
  10834. elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
  10835. reloc_write.put_r_offset(reloc_offset);
  10836. reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
  10837. elfcpp::Rel<32, big_endian> rel(reloc_buffer);
  10838. relocate.relocate(relinfo, this, output_section,
  10839. this->fake_relnum_for_stubs, rel, r_type,
  10840. NULL, &symval, view + reloc_offset,
  10841. address + reloc_offset, reloc_size);
  10842. }
  10843. }
  10844. // Determine whether an object attribute tag takes an integer, a
  10845. // string or both.
  10846. template<bool big_endian>
  10847. int
  10848. Target_arm<big_endian>::do_attribute_arg_type(int tag) const
  10849. {
  10850. if (tag == Object_attribute::Tag_compatibility)
  10851. return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
  10852. | Object_attribute::ATTR_TYPE_FLAG_STR_VAL);
  10853. else if (tag == elfcpp::Tag_nodefaults)
  10854. return (Object_attribute::ATTR_TYPE_FLAG_INT_VAL
  10855. | Object_attribute::ATTR_TYPE_FLAG_NO_DEFAULT);
  10856. else if (tag == elfcpp::Tag_CPU_raw_name || tag == elfcpp::Tag_CPU_name)
  10857. return Object_attribute::ATTR_TYPE_FLAG_STR_VAL;
  10858. else if (tag < 32)
  10859. return Object_attribute::ATTR_TYPE_FLAG_INT_VAL;
  10860. else
  10861. return ((tag & 1) != 0
  10862. ? Object_attribute::ATTR_TYPE_FLAG_STR_VAL
  10863. : Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
  10864. }
  10865. // Reorder attributes.
  10866. //
  10867. // The ABI defines that Tag_conformance should be emitted first, and that
  10868. // Tag_nodefaults should be second (if either is defined). This sets those
  10869. // two positions, and bumps up the position of all the remaining tags to
  10870. // compensate.
  10871. template<bool big_endian>
  10872. int
  10873. Target_arm<big_endian>::do_attributes_order(int num) const
  10874. {
  10875. // Reorder the known object attributes in output. We want to move
  10876. // Tag_conformance to position 4 and Tag_conformance to position 5
  10877. // and shift everything between 4 .. Tag_conformance - 1 to make room.
  10878. if (num == 4)
  10879. return elfcpp::Tag_conformance;
  10880. if (num == 5)
  10881. return elfcpp::Tag_nodefaults;
  10882. if ((num - 2) < elfcpp::Tag_nodefaults)
  10883. return num - 2;
  10884. if ((num - 1) < elfcpp::Tag_conformance)
  10885. return num - 1;
  10886. return num;
  10887. }
  10888. // Scan a span of THUMB code for Cortex-A8 erratum.
  10889. template<bool big_endian>
  10890. void
  10891. Target_arm<big_endian>::scan_span_for_cortex_a8_erratum(
  10892. Arm_relobj<big_endian>* arm_relobj,
  10893. unsigned int shndx,
  10894. section_size_type span_start,
  10895. section_size_type span_end,
  10896. const unsigned char* view,
  10897. Arm_address address)
  10898. {
  10899. // Scan for 32-bit Thumb-2 branches which span two 4K regions, where:
  10900. //
  10901. // The opcode is BLX.W, BL.W, B.W, Bcc.W
  10902. // The branch target is in the same 4KB region as the
  10903. // first half of the branch.
  10904. // The instruction before the branch is a 32-bit
  10905. // length non-branch instruction.
  10906. section_size_type i = span_start;
  10907. bool last_was_32bit = false;
  10908. bool last_was_branch = false;
  10909. while (i < span_end)
  10910. {
  10911. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
  10912. const Valtype* wv = reinterpret_cast<const Valtype*>(view + i);
  10913. uint32_t insn = elfcpp::Swap<16, big_endian>::readval(wv);
  10914. bool is_blx = false, is_b = false;
  10915. bool is_bl = false, is_bcc = false;
  10916. bool insn_32bit = (insn & 0xe000) == 0xe000 && (insn & 0x1800) != 0x0000;
  10917. if (insn_32bit)
  10918. {
  10919. // Load the rest of the insn (in manual-friendly order).
  10920. insn = (insn << 16) | elfcpp::Swap<16, big_endian>::readval(wv + 1);
  10921. // Encoding T4: B<c>.W.
  10922. is_b = (insn & 0xf800d000U) == 0xf0009000U;
  10923. // Encoding T1: BL<c>.W.
  10924. is_bl = (insn & 0xf800d000U) == 0xf000d000U;
  10925. // Encoding T2: BLX<c>.W.
  10926. is_blx = (insn & 0xf800d000U) == 0xf000c000U;
  10927. // Encoding T3: B<c>.W (not permitted in IT block).
  10928. is_bcc = ((insn & 0xf800d000U) == 0xf0008000U
  10929. && (insn & 0x07f00000U) != 0x03800000U);
  10930. }
  10931. bool is_32bit_branch = is_b || is_bl || is_blx || is_bcc;
  10932. // If this instruction is a 32-bit THUMB branch that crosses a 4K
  10933. // page boundary and it follows 32-bit non-branch instruction,
  10934. // we need to work around.
  10935. if (is_32bit_branch
  10936. && ((address + i) & 0xfffU) == 0xffeU
  10937. && last_was_32bit
  10938. && !last_was_branch)
  10939. {
  10940. // Check to see if there is a relocation stub for this branch.
  10941. bool force_target_arm = false;
  10942. bool force_target_thumb = false;
  10943. const Cortex_a8_reloc* cortex_a8_reloc = NULL;
  10944. Cortex_a8_relocs_info::const_iterator p =
  10945. this->cortex_a8_relocs_info_.find(address + i);
  10946. if (p != this->cortex_a8_relocs_info_.end())
  10947. {
  10948. cortex_a8_reloc = p->second;
  10949. bool target_is_thumb = (cortex_a8_reloc->destination() & 1) != 0;
  10950. if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
  10951. && !target_is_thumb)
  10952. force_target_arm = true;
  10953. else if (cortex_a8_reloc->r_type() == elfcpp::R_ARM_THM_CALL
  10954. && target_is_thumb)
  10955. force_target_thumb = true;
  10956. }
  10957. off_t offset;
  10958. Stub_type stub_type = arm_stub_none;
  10959. // Check if we have an offending branch instruction.
  10960. uint16_t upper_insn = (insn >> 16) & 0xffffU;
  10961. uint16_t lower_insn = insn & 0xffffU;
  10962. typedef class Arm_relocate_functions<big_endian> RelocFuncs;
  10963. if (cortex_a8_reloc != NULL
  10964. && cortex_a8_reloc->reloc_stub() != NULL)
  10965. // We've already made a stub for this instruction, e.g.
  10966. // it's a long branch or a Thumb->ARM stub. Assume that
  10967. // stub will suffice to work around the A8 erratum (see
  10968. // setting of always_after_branch above).
  10969. ;
  10970. else if (is_bcc)
  10971. {
  10972. offset = RelocFuncs::thumb32_cond_branch_offset(upper_insn,
  10973. lower_insn);
  10974. stub_type = arm_stub_a8_veneer_b_cond;
  10975. }
  10976. else if (is_b || is_bl || is_blx)
  10977. {
  10978. offset = RelocFuncs::thumb32_branch_offset(upper_insn,
  10979. lower_insn);
  10980. if (is_blx)
  10981. offset &= ~3;
  10982. stub_type = (is_blx
  10983. ? arm_stub_a8_veneer_blx
  10984. : (is_bl
  10985. ? arm_stub_a8_veneer_bl
  10986. : arm_stub_a8_veneer_b));
  10987. }
  10988. if (stub_type != arm_stub_none)
  10989. {
  10990. Arm_address pc_for_insn = address + i + 4;
  10991. // The original instruction is a BL, but the target is
  10992. // an ARM instruction. If we were not making a stub,
  10993. // the BL would have been converted to a BLX. Use the
  10994. // BLX stub instead in that case.
  10995. if (this->may_use_v5t_interworking() && force_target_arm
  10996. && stub_type == arm_stub_a8_veneer_bl)
  10997. {
  10998. stub_type = arm_stub_a8_veneer_blx;
  10999. is_blx = true;
  11000. is_bl = false;
  11001. }
  11002. // Conversely, if the original instruction was
  11003. // BLX but the target is Thumb mode, use the BL stub.
  11004. else if (force_target_thumb
  11005. && stub_type == arm_stub_a8_veneer_blx)
  11006. {
  11007. stub_type = arm_stub_a8_veneer_bl;
  11008. is_blx = false;
  11009. is_bl = true;
  11010. }
  11011. if (is_blx)
  11012. pc_for_insn &= ~3;
  11013. // If we found a relocation, use the proper destination,
  11014. // not the offset in the (unrelocated) instruction.
  11015. // Note this is always done if we switched the stub type above.
  11016. if (cortex_a8_reloc != NULL)
  11017. offset = (off_t) (cortex_a8_reloc->destination() - pc_for_insn);
  11018. Arm_address target = (pc_for_insn + offset) | (is_blx ? 0 : 1);
  11019. // Add a new stub if destination address in in the same page.
  11020. if (((address + i) & ~0xfffU) == (target & ~0xfffU))
  11021. {
  11022. Cortex_a8_stub* stub =
  11023. this->stub_factory_.make_cortex_a8_stub(stub_type,
  11024. arm_relobj, shndx,
  11025. address + i,
  11026. target, insn);
  11027. Stub_table<big_endian>* stub_table =
  11028. arm_relobj->stub_table(shndx);
  11029. gold_assert(stub_table != NULL);
  11030. stub_table->add_cortex_a8_stub(address + i, stub);
  11031. }
  11032. }
  11033. }
  11034. i += insn_32bit ? 4 : 2;
  11035. last_was_32bit = insn_32bit;
  11036. last_was_branch = is_32bit_branch;
  11037. }
  11038. }
  11039. // Apply the Cortex-A8 workaround.
  11040. template<bool big_endian>
  11041. void
  11042. Target_arm<big_endian>::apply_cortex_a8_workaround(
  11043. const Cortex_a8_stub* stub,
  11044. Arm_address stub_address,
  11045. unsigned char* insn_view,
  11046. Arm_address insn_address)
  11047. {
  11048. typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
  11049. Valtype* wv = reinterpret_cast<Valtype*>(insn_view);
  11050. Valtype upper_insn = elfcpp::Swap<16, big_endian>::readval(wv);
  11051. Valtype lower_insn = elfcpp::Swap<16, big_endian>::readval(wv + 1);
  11052. off_t branch_offset = stub_address - (insn_address + 4);
  11053. typedef class Arm_relocate_functions<big_endian> RelocFuncs;
  11054. switch (stub->stub_template()->type())
  11055. {
  11056. case arm_stub_a8_veneer_b_cond:
  11057. // For a conditional branch, we re-write it to be an unconditional
  11058. // branch to the stub. We use the THUMB-2 encoding here.
  11059. upper_insn = 0xf000U;
  11060. lower_insn = 0xb800U;
  11061. // Fall through
  11062. case arm_stub_a8_veneer_b:
  11063. case arm_stub_a8_veneer_bl:
  11064. case arm_stub_a8_veneer_blx:
  11065. if ((lower_insn & 0x5000U) == 0x4000U)
  11066. // For a BLX instruction, make sure that the relocation is
  11067. // rounded up to a word boundary. This follows the semantics of
  11068. // the instruction which specifies that bit 1 of the target
  11069. // address will come from bit 1 of the base address.
  11070. branch_offset = (branch_offset + 2) & ~3;
  11071. // Put BRANCH_OFFSET back into the insn.
  11072. gold_assert(!Bits<25>::has_overflow32(branch_offset));
  11073. upper_insn = RelocFuncs::thumb32_branch_upper(upper_insn, branch_offset);
  11074. lower_insn = RelocFuncs::thumb32_branch_lower(lower_insn, branch_offset);
  11075. break;
  11076. default:
  11077. gold_unreachable();
  11078. }
  11079. // Put the relocated value back in the object file:
  11080. elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
  11081. elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
  11082. }
  11083. // Target selector for ARM. Note this is never instantiated directly.
  11084. // It's only used in Target_selector_arm_nacl, below.
  11085. template<bool big_endian>
  11086. class Target_selector_arm : public Target_selector
  11087. {
  11088. public:
  11089. Target_selector_arm()
  11090. : Target_selector(elfcpp::EM_ARM, 32, big_endian,
  11091. (big_endian ? "elf32-bigarm" : "elf32-littlearm"),
  11092. (big_endian ? "armelfb" : "armelf"))
  11093. { }
  11094. Target*
  11095. do_instantiate_target()
  11096. { return new Target_arm<big_endian>(); }
  11097. };
  11098. // Fix .ARM.exidx section coverage.
  11099. template<bool big_endian>
  11100. void
  11101. Target_arm<big_endian>::fix_exidx_coverage(
  11102. Layout* layout,
  11103. const Input_objects* input_objects,
  11104. Arm_output_section<big_endian>* exidx_section,
  11105. Symbol_table* symtab,
  11106. const Task* task)
  11107. {
  11108. // We need to look at all the input sections in output in ascending
  11109. // order of of output address. We do that by building a sorted list
  11110. // of output sections by addresses. Then we looks at the output sections
  11111. // in order. The input sections in an output section are already sorted
  11112. // by addresses within the output section.
  11113. typedef std::set<Output_section*, output_section_address_less_than>
  11114. Sorted_output_section_list;
  11115. Sorted_output_section_list sorted_output_sections;
  11116. // Find out all the output sections of input sections pointed by
  11117. // EXIDX input sections.
  11118. for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
  11119. p != input_objects->relobj_end();
  11120. ++p)
  11121. {
  11122. Arm_relobj<big_endian>* arm_relobj =
  11123. Arm_relobj<big_endian>::as_arm_relobj(*p);
  11124. std::vector<unsigned int> shndx_list;
  11125. arm_relobj->get_exidx_shndx_list(&shndx_list);
  11126. for (size_t i = 0; i < shndx_list.size(); ++i)
  11127. {
  11128. const Arm_exidx_input_section* exidx_input_section =
  11129. arm_relobj->exidx_input_section_by_shndx(shndx_list[i]);
  11130. gold_assert(exidx_input_section != NULL);
  11131. if (!exidx_input_section->has_errors())
  11132. {
  11133. unsigned int text_shndx = exidx_input_section->link();
  11134. Output_section* os = arm_relobj->output_section(text_shndx);
  11135. if (os != NULL && (os->flags() & elfcpp::SHF_ALLOC) != 0)
  11136. sorted_output_sections.insert(os);
  11137. }
  11138. }
  11139. }
  11140. // Go over the output sections in ascending order of output addresses.
  11141. typedef typename Arm_output_section<big_endian>::Text_section_list
  11142. Text_section_list;
  11143. Text_section_list sorted_text_sections;
  11144. for (typename Sorted_output_section_list::iterator p =
  11145. sorted_output_sections.begin();
  11146. p != sorted_output_sections.end();
  11147. ++p)
  11148. {
  11149. Arm_output_section<big_endian>* arm_output_section =
  11150. Arm_output_section<big_endian>::as_arm_output_section(*p);
  11151. arm_output_section->append_text_sections_to_list(&sorted_text_sections);
  11152. }
  11153. exidx_section->fix_exidx_coverage(layout, sorted_text_sections, symtab,
  11154. merge_exidx_entries(), task);
  11155. }
  11156. template<bool big_endian>
  11157. void
  11158. Target_arm<big_endian>::do_define_standard_symbols(
  11159. Symbol_table* symtab,
  11160. Layout* layout)
  11161. {
  11162. // Handle the .ARM.exidx section.
  11163. Output_section* exidx_section = layout->find_output_section(".ARM.exidx");
  11164. if (exidx_section != NULL)
  11165. {
  11166. // Create __exidx_start and __exidx_end symbols.
  11167. symtab->define_in_output_data("__exidx_start",
  11168. NULL, // version
  11169. Symbol_table::PREDEFINED,
  11170. exidx_section,
  11171. 0, // value
  11172. 0, // symsize
  11173. elfcpp::STT_NOTYPE,
  11174. elfcpp::STB_GLOBAL,
  11175. elfcpp::STV_HIDDEN,
  11176. 0, // nonvis
  11177. false, // offset_is_from_end
  11178. true); // only_if_ref
  11179. symtab->define_in_output_data("__exidx_end",
  11180. NULL, // version
  11181. Symbol_table::PREDEFINED,
  11182. exidx_section,
  11183. 0, // value
  11184. 0, // symsize
  11185. elfcpp::STT_NOTYPE,
  11186. elfcpp::STB_GLOBAL,
  11187. elfcpp::STV_HIDDEN,
  11188. 0, // nonvis
  11189. true, // offset_is_from_end
  11190. true); // only_if_ref
  11191. }
  11192. else
  11193. {
  11194. // Define __exidx_start and __exidx_end even when .ARM.exidx
  11195. // section is missing to match ld's behaviour.
  11196. symtab->define_as_constant("__exidx_start", NULL,
  11197. Symbol_table::PREDEFINED,
  11198. 0, 0, elfcpp::STT_OBJECT,
  11199. elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
  11200. true, false);
  11201. symtab->define_as_constant("__exidx_end", NULL,
  11202. Symbol_table::PREDEFINED,
  11203. 0, 0, elfcpp::STT_OBJECT,
  11204. elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN, 0,
  11205. true, false);
  11206. }
  11207. }
  11208. // NaCl variant. It uses different PLT contents.
  11209. template<bool big_endian>
  11210. class Output_data_plt_arm_nacl;
  11211. template<bool big_endian>
  11212. class Target_arm_nacl : public Target_arm<big_endian>
  11213. {
  11214. public:
  11215. Target_arm_nacl()
  11216. : Target_arm<big_endian>(&arm_nacl_info)
  11217. { }
  11218. protected:
  11219. virtual Output_data_plt_arm<big_endian>*
  11220. do_make_data_plt(
  11221. Layout* layout,
  11222. Arm_output_data_got<big_endian>* got,
  11223. Output_data_space* got_plt,
  11224. Output_data_space* got_irelative)
  11225. { return new Output_data_plt_arm_nacl<big_endian>(
  11226. layout, got, got_plt, got_irelative); }
  11227. private:
  11228. static const Target::Target_info arm_nacl_info;
  11229. };
  11230. template<bool big_endian>
  11231. const Target::Target_info Target_arm_nacl<big_endian>::arm_nacl_info =
  11232. {
  11233. 32, // size
  11234. big_endian, // is_big_endian
  11235. elfcpp::EM_ARM, // machine_code
  11236. false, // has_make_symbol
  11237. false, // has_resolve
  11238. false, // has_code_fill
  11239. true, // is_default_stack_executable
  11240. false, // can_icf_inline_merge_sections
  11241. '\0', // wrap_char
  11242. "/lib/ld-nacl-arm.so.1", // dynamic_linker
  11243. 0x20000, // default_text_segment_address
  11244. 0x10000, // abi_pagesize (overridable by -z max-page-size)
  11245. 0x10000, // common_pagesize (overridable by -z common-page-size)
  11246. true, // isolate_execinstr
  11247. 0x10000000, // rosegment_gap
  11248. elfcpp::SHN_UNDEF, // small_common_shndx
  11249. elfcpp::SHN_UNDEF, // large_common_shndx
  11250. 0, // small_common_section_flags
  11251. 0, // large_common_section_flags
  11252. ".ARM.attributes", // attributes_section
  11253. "aeabi", // attributes_vendor
  11254. "_start" // entry_symbol_name
  11255. };
  11256. template<bool big_endian>
  11257. class Output_data_plt_arm_nacl : public Output_data_plt_arm<big_endian>
  11258. {
  11259. public:
  11260. Output_data_plt_arm_nacl(
  11261. Layout* layout,
  11262. Arm_output_data_got<big_endian>* got,
  11263. Output_data_space* got_plt,
  11264. Output_data_space* got_irelative)
  11265. : Output_data_plt_arm<big_endian>(layout, 16, got, got_plt, got_irelative)
  11266. { }
  11267. protected:
  11268. // Return the offset of the first non-reserved PLT entry.
  11269. virtual unsigned int
  11270. do_first_plt_entry_offset() const
  11271. { return sizeof(first_plt_entry); }
  11272. // Return the size of a PLT entry.
  11273. virtual unsigned int
  11274. do_get_plt_entry_size() const
  11275. { return sizeof(plt_entry); }
  11276. virtual void
  11277. do_fill_first_plt_entry(unsigned char* pov,
  11278. Arm_address got_address,
  11279. Arm_address plt_address);
  11280. virtual void
  11281. do_fill_plt_entry(unsigned char* pov,
  11282. Arm_address got_address,
  11283. Arm_address plt_address,
  11284. unsigned int got_offset,
  11285. unsigned int plt_offset);
  11286. private:
  11287. inline uint32_t arm_movw_immediate(uint32_t value)
  11288. {
  11289. return (value & 0x00000fff) | ((value & 0x0000f000) << 4);
  11290. }
  11291. inline uint32_t arm_movt_immediate(uint32_t value)
  11292. {
  11293. return ((value & 0x0fff0000) >> 16) | ((value & 0xf0000000) >> 12);
  11294. }
  11295. // Template for the first PLT entry.
  11296. static const uint32_t first_plt_entry[16];
  11297. // Template for subsequent PLT entries.
  11298. static const uint32_t plt_entry[4];
  11299. };
  11300. // The first entry in the PLT.
  11301. template<bool big_endian>
  11302. const uint32_t Output_data_plt_arm_nacl<big_endian>::first_plt_entry[16] =
  11303. {
  11304. // First bundle:
  11305. 0xe300c000, // movw ip, #:lower16:&GOT[2]-.+8
  11306. 0xe340c000, // movt ip, #:upper16:&GOT[2]-.+8
  11307. 0xe08cc00f, // add ip, ip, pc
  11308. 0xe52dc008, // str ip, [sp, #-8]!
  11309. // Second bundle:
  11310. 0xe3ccc103, // bic ip, ip, #0xc0000000
  11311. 0xe59cc000, // ldr ip, [ip]
  11312. 0xe3ccc13f, // bic ip, ip, #0xc000000f
  11313. 0xe12fff1c, // bx ip
  11314. // Third bundle:
  11315. 0xe320f000, // nop
  11316. 0xe320f000, // nop
  11317. 0xe320f000, // nop
  11318. // .Lplt_tail:
  11319. 0xe50dc004, // str ip, [sp, #-4]
  11320. // Fourth bundle:
  11321. 0xe3ccc103, // bic ip, ip, #0xc0000000
  11322. 0xe59cc000, // ldr ip, [ip]
  11323. 0xe3ccc13f, // bic ip, ip, #0xc000000f
  11324. 0xe12fff1c, // bx ip
  11325. };
  11326. template<bool big_endian>
  11327. void
  11328. Output_data_plt_arm_nacl<big_endian>::do_fill_first_plt_entry(
  11329. unsigned char* pov,
  11330. Arm_address got_address,
  11331. Arm_address plt_address)
  11332. {
  11333. // Write first PLT entry. All but first two words are constants.
  11334. const size_t num_first_plt_words = (sizeof(first_plt_entry)
  11335. / sizeof(first_plt_entry[0]));
  11336. int32_t got_displacement = got_address + 8 - (plt_address + 16);
  11337. elfcpp::Swap<32, big_endian>::writeval
  11338. (pov + 0, first_plt_entry[0] | arm_movw_immediate (got_displacement));
  11339. elfcpp::Swap<32, big_endian>::writeval
  11340. (pov + 4, first_plt_entry[1] | arm_movt_immediate (got_displacement));
  11341. for (size_t i = 2; i < num_first_plt_words; ++i)
  11342. elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
  11343. }
  11344. // Subsequent entries in the PLT.
  11345. template<bool big_endian>
  11346. const uint32_t Output_data_plt_arm_nacl<big_endian>::plt_entry[4] =
  11347. {
  11348. 0xe300c000, // movw ip, #:lower16:&GOT[n]-.+8
  11349. 0xe340c000, // movt ip, #:upper16:&GOT[n]-.+8
  11350. 0xe08cc00f, // add ip, ip, pc
  11351. 0xea000000, // b .Lplt_tail
  11352. };
  11353. template<bool big_endian>
  11354. void
  11355. Output_data_plt_arm_nacl<big_endian>::do_fill_plt_entry(
  11356. unsigned char* pov,
  11357. Arm_address got_address,
  11358. Arm_address plt_address,
  11359. unsigned int got_offset,
  11360. unsigned int plt_offset)
  11361. {
  11362. // Calculate the displacement between the PLT slot and the
  11363. // common tail that's part of the special initial PLT slot.
  11364. int32_t tail_displacement = (plt_address + (11 * sizeof(uint32_t))
  11365. - (plt_address + plt_offset
  11366. + sizeof(plt_entry) + sizeof(uint32_t)));
  11367. gold_assert((tail_displacement & 3) == 0);
  11368. tail_displacement >>= 2;
  11369. gold_assert ((tail_displacement & 0xff000000) == 0
  11370. || (-tail_displacement & 0xff000000) == 0);
  11371. // Calculate the displacement between the PLT slot and the entry
  11372. // in the GOT. The offset accounts for the value produced by
  11373. // adding to pc in the penultimate instruction of the PLT stub.
  11374. const int32_t got_displacement = (got_address + got_offset
  11375. - (plt_address + sizeof(plt_entry)));
  11376. elfcpp::Swap<32, big_endian>::writeval
  11377. (pov + 0, plt_entry[0] | arm_movw_immediate (got_displacement));
  11378. elfcpp::Swap<32, big_endian>::writeval
  11379. (pov + 4, plt_entry[1] | arm_movt_immediate (got_displacement));
  11380. elfcpp::Swap<32, big_endian>::writeval
  11381. (pov + 8, plt_entry[2]);
  11382. elfcpp::Swap<32, big_endian>::writeval
  11383. (pov + 12, plt_entry[3] | (tail_displacement & 0x00ffffff));
  11384. }
  11385. // Target selectors.
  11386. template<bool big_endian>
  11387. class Target_selector_arm_nacl
  11388. : public Target_selector_nacl<Target_selector_arm<big_endian>,
  11389. Target_arm_nacl<big_endian> >
  11390. {
  11391. public:
  11392. Target_selector_arm_nacl()
  11393. : Target_selector_nacl<Target_selector_arm<big_endian>,
  11394. Target_arm_nacl<big_endian> >(
  11395. "arm",
  11396. big_endian ? "elf32-bigarm-nacl" : "elf32-littlearm-nacl",
  11397. big_endian ? "armelfb_nacl" : "armelf_nacl")
  11398. { }
  11399. };
  11400. Target_selector_arm_nacl<false> target_selector_arm;
  11401. Target_selector_arm_nacl<true> target_selector_armbe;
  11402. } // End anonymous namespace.