elflink.c 442 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415
  1. /* ELF linking support for BFD.
  2. Copyright (C) 1995-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "bfdlink.h"
  19. #include "libbfd.h"
  20. #define ARCH_SIZE 0
  21. #include "elf-bfd.h"
  22. #include "safe-ctype.h"
  23. #include "libiberty.h"
  24. #include "objalloc.h"
  25. #if BFD_SUPPORTS_PLUGINS
  26. #include "plugin-api.h"
  27. #include "plugin.h"
  28. #endif
  29. #include <limits.h>
  30. #ifndef CHAR_BIT
  31. #define CHAR_BIT 8
  32. #endif
  33. /* This struct is used to pass information to routines called via
  34. elf_link_hash_traverse which must return failure. */
  35. struct elf_info_failed
  36. {
  37. struct bfd_link_info *info;
  38. bool failed;
  39. };
  40. /* This structure is used to pass information to
  41. _bfd_elf_link_find_version_dependencies. */
  42. struct elf_find_verdep_info
  43. {
  44. /* General link information. */
  45. struct bfd_link_info *info;
  46. /* The number of dependencies. */
  47. unsigned int vers;
  48. /* Whether we had a failure. */
  49. bool failed;
  50. };
  51. static bool _bfd_elf_fix_symbol_flags
  52. (struct elf_link_hash_entry *, struct elf_info_failed *);
  53. asection *
  54. _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
  55. unsigned long r_symndx,
  56. bool discard)
  57. {
  58. if (r_symndx >= cookie->locsymcount
  59. || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
  60. {
  61. struct elf_link_hash_entry *h;
  62. h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
  63. while (h->root.type == bfd_link_hash_indirect
  64. || h->root.type == bfd_link_hash_warning)
  65. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  66. if ((h->root.type == bfd_link_hash_defined
  67. || h->root.type == bfd_link_hash_defweak)
  68. && discarded_section (h->root.u.def.section))
  69. return h->root.u.def.section;
  70. else
  71. return NULL;
  72. }
  73. else
  74. {
  75. /* It's not a relocation against a global symbol,
  76. but it could be a relocation against a local
  77. symbol for a discarded section. */
  78. asection *isec;
  79. Elf_Internal_Sym *isym;
  80. /* Need to: get the symbol; get the section. */
  81. isym = &cookie->locsyms[r_symndx];
  82. isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
  83. if (isec != NULL
  84. && discard ? discarded_section (isec) : 1)
  85. return isec;
  86. }
  87. return NULL;
  88. }
  89. /* Define a symbol in a dynamic linkage section. */
  90. struct elf_link_hash_entry *
  91. _bfd_elf_define_linkage_sym (bfd *abfd,
  92. struct bfd_link_info *info,
  93. asection *sec,
  94. const char *name)
  95. {
  96. struct elf_link_hash_entry *h;
  97. struct bfd_link_hash_entry *bh;
  98. const struct elf_backend_data *bed;
  99. h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false);
  100. if (h != NULL)
  101. {
  102. /* Zap symbol defined in an as-needed lib that wasn't linked.
  103. This is a symptom of a larger problem: Absolute symbols
  104. defined in shared libraries can't be overridden, because we
  105. lose the link to the bfd which is via the symbol section. */
  106. h->root.type = bfd_link_hash_new;
  107. bh = &h->root;
  108. }
  109. else
  110. bh = NULL;
  111. bed = get_elf_backend_data (abfd);
  112. if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
  113. sec, 0, NULL, false, bed->collect,
  114. &bh))
  115. return NULL;
  116. h = (struct elf_link_hash_entry *) bh;
  117. BFD_ASSERT (h != NULL);
  118. h->def_regular = 1;
  119. h->non_elf = 0;
  120. h->root.linker_def = 1;
  121. h->type = STT_OBJECT;
  122. if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
  123. h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
  124. (*bed->elf_backend_hide_symbol) (info, h, true);
  125. return h;
  126. }
  127. bool
  128. _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
  129. {
  130. flagword flags;
  131. asection *s;
  132. struct elf_link_hash_entry *h;
  133. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  134. struct elf_link_hash_table *htab = elf_hash_table (info);
  135. /* This function may be called more than once. */
  136. if (htab->sgot != NULL)
  137. return true;
  138. flags = bed->dynamic_sec_flags;
  139. s = bfd_make_section_anyway_with_flags (abfd,
  140. (bed->rela_plts_and_copies_p
  141. ? ".rela.got" : ".rel.got"),
  142. (bed->dynamic_sec_flags
  143. | SEC_READONLY));
  144. if (s == NULL
  145. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  146. return false;
  147. htab->srelgot = s;
  148. s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
  149. if (s == NULL
  150. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  151. return false;
  152. htab->sgot = s;
  153. if (bed->want_got_plt)
  154. {
  155. s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
  156. if (s == NULL
  157. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  158. return false;
  159. htab->sgotplt = s;
  160. }
  161. /* The first bit of the global offset table is the header. */
  162. s->size += bed->got_header_size;
  163. if (bed->want_got_sym)
  164. {
  165. /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
  166. (or .got.plt) section. We don't do this in the linker script
  167. because we don't want to define the symbol if we are not creating
  168. a global offset table. */
  169. h = _bfd_elf_define_linkage_sym (abfd, info, s,
  170. "_GLOBAL_OFFSET_TABLE_");
  171. elf_hash_table (info)->hgot = h;
  172. if (h == NULL)
  173. return false;
  174. }
  175. return true;
  176. }
  177. /* Create a strtab to hold the dynamic symbol names. */
  178. static bool
  179. _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info)
  180. {
  181. struct elf_link_hash_table *hash_table;
  182. hash_table = elf_hash_table (info);
  183. if (hash_table->dynobj == NULL)
  184. {
  185. /* We may not set dynobj, an input file holding linker created
  186. dynamic sections to abfd, which may be a dynamic object with
  187. its own dynamic sections. We need to find a normal input file
  188. to hold linker created sections if possible. */
  189. if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0)
  190. {
  191. bfd *ibfd;
  192. asection *s;
  193. for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
  194. if ((ibfd->flags
  195. & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
  196. && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
  197. && elf_object_id (ibfd) == elf_hash_table_id (hash_table)
  198. && !((s = ibfd->sections) != NULL
  199. && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS))
  200. {
  201. abfd = ibfd;
  202. break;
  203. }
  204. }
  205. hash_table->dynobj = abfd;
  206. }
  207. if (hash_table->dynstr == NULL)
  208. {
  209. hash_table->dynstr = _bfd_elf_strtab_init ();
  210. if (hash_table->dynstr == NULL)
  211. return false;
  212. }
  213. return true;
  214. }
  215. /* Create some sections which will be filled in with dynamic linking
  216. information. ABFD is an input file which requires dynamic sections
  217. to be created. The dynamic sections take up virtual memory space
  218. when the final executable is run, so we need to create them before
  219. addresses are assigned to the output sections. We work out the
  220. actual contents and size of these sections later. */
  221. bool
  222. _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  223. {
  224. flagword flags;
  225. asection *s;
  226. const struct elf_backend_data *bed;
  227. struct elf_link_hash_entry *h;
  228. if (! is_elf_hash_table (info->hash))
  229. return false;
  230. if (elf_hash_table (info)->dynamic_sections_created)
  231. return true;
  232. if (!_bfd_elf_link_create_dynstrtab (abfd, info))
  233. return false;
  234. abfd = elf_hash_table (info)->dynobj;
  235. bed = get_elf_backend_data (abfd);
  236. flags = bed->dynamic_sec_flags;
  237. /* A dynamically linked executable has a .interp section, but a
  238. shared library does not. */
  239. if (bfd_link_executable (info) && !info->nointerp)
  240. {
  241. s = bfd_make_section_anyway_with_flags (abfd, ".interp",
  242. flags | SEC_READONLY);
  243. if (s == NULL)
  244. return false;
  245. }
  246. /* Create sections to hold version informations. These are removed
  247. if they are not needed. */
  248. s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d",
  249. flags | SEC_READONLY);
  250. if (s == NULL
  251. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  252. return false;
  253. s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version",
  254. flags | SEC_READONLY);
  255. if (s == NULL
  256. || !bfd_set_section_alignment (s, 1))
  257. return false;
  258. s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r",
  259. flags | SEC_READONLY);
  260. if (s == NULL
  261. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  262. return false;
  263. s = bfd_make_section_anyway_with_flags (abfd, ".dynsym",
  264. flags | SEC_READONLY);
  265. if (s == NULL
  266. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  267. return false;
  268. elf_hash_table (info)->dynsym = s;
  269. s = bfd_make_section_anyway_with_flags (abfd, ".dynstr",
  270. flags | SEC_READONLY);
  271. if (s == NULL)
  272. return false;
  273. s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags);
  274. if (s == NULL
  275. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  276. return false;
  277. /* The special symbol _DYNAMIC is always set to the start of the
  278. .dynamic section. We could set _DYNAMIC in a linker script, but we
  279. only want to define it if we are, in fact, creating a .dynamic
  280. section. We don't want to define it if there is no .dynamic
  281. section, since on some ELF platforms the start up code examines it
  282. to decide how to initialize the process. */
  283. h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC");
  284. elf_hash_table (info)->hdynamic = h;
  285. if (h == NULL)
  286. return false;
  287. if (info->emit_hash)
  288. {
  289. s = bfd_make_section_anyway_with_flags (abfd, ".hash",
  290. flags | SEC_READONLY);
  291. if (s == NULL
  292. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  293. return false;
  294. elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
  295. }
  296. if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL)
  297. {
  298. s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash",
  299. flags | SEC_READONLY);
  300. if (s == NULL
  301. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  302. return false;
  303. /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section:
  304. 4 32-bit words followed by variable count of 64-bit words, then
  305. variable count of 32-bit words. */
  306. if (bed->s->arch_size == 64)
  307. elf_section_data (s)->this_hdr.sh_entsize = 0;
  308. else
  309. elf_section_data (s)->this_hdr.sh_entsize = 4;
  310. }
  311. if (info->enable_dt_relr)
  312. {
  313. s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn",
  314. (bed->dynamic_sec_flags
  315. | SEC_READONLY));
  316. if (s == NULL
  317. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  318. return false;
  319. elf_hash_table (info)->srelrdyn = s;
  320. }
  321. /* Let the backend create the rest of the sections. This lets the
  322. backend set the right flags. The backend will normally create
  323. the .got and .plt sections. */
  324. if (bed->elf_backend_create_dynamic_sections == NULL
  325. || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
  326. return false;
  327. elf_hash_table (info)->dynamic_sections_created = true;
  328. return true;
  329. }
  330. /* Create dynamic sections when linking against a dynamic object. */
  331. bool
  332. _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  333. {
  334. flagword flags, pltflags;
  335. struct elf_link_hash_entry *h;
  336. asection *s;
  337. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  338. struct elf_link_hash_table *htab = elf_hash_table (info);
  339. /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
  340. .rel[a].bss sections. */
  341. flags = bed->dynamic_sec_flags;
  342. pltflags = flags;
  343. if (bed->plt_not_loaded)
  344. /* We do not clear SEC_ALLOC here because we still want the OS to
  345. allocate space for the section; it's just that there's nothing
  346. to read in from the object file. */
  347. pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
  348. else
  349. pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD;
  350. if (bed->plt_readonly)
  351. pltflags |= SEC_READONLY;
  352. s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
  353. if (s == NULL
  354. || !bfd_set_section_alignment (s, bed->plt_alignment))
  355. return false;
  356. htab->splt = s;
  357. /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
  358. .plt section. */
  359. if (bed->want_plt_sym)
  360. {
  361. h = _bfd_elf_define_linkage_sym (abfd, info, s,
  362. "_PROCEDURE_LINKAGE_TABLE_");
  363. elf_hash_table (info)->hplt = h;
  364. if (h == NULL)
  365. return false;
  366. }
  367. s = bfd_make_section_anyway_with_flags (abfd,
  368. (bed->rela_plts_and_copies_p
  369. ? ".rela.plt" : ".rel.plt"),
  370. flags | SEC_READONLY);
  371. if (s == NULL
  372. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  373. return false;
  374. htab->srelplt = s;
  375. if (! _bfd_elf_create_got_section (abfd, info))
  376. return false;
  377. if (bed->want_dynbss)
  378. {
  379. /* The .dynbss section is a place to put symbols which are defined
  380. by dynamic objects, are referenced by regular objects, and are
  381. not functions. We must allocate space for them in the process
  382. image and use a R_*_COPY reloc to tell the dynamic linker to
  383. initialize them at run time. The linker script puts the .dynbss
  384. section into the .bss section of the final image. */
  385. s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
  386. SEC_ALLOC | SEC_LINKER_CREATED);
  387. if (s == NULL)
  388. return false;
  389. htab->sdynbss = s;
  390. if (bed->want_dynrelro)
  391. {
  392. /* Similarly, but for symbols that were originally in read-only
  393. sections. This section doesn't really need to have contents,
  394. but make it like other .data.rel.ro sections. */
  395. s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro",
  396. flags);
  397. if (s == NULL)
  398. return false;
  399. htab->sdynrelro = s;
  400. }
  401. /* The .rel[a].bss section holds copy relocs. This section is not
  402. normally needed. We need to create it here, though, so that the
  403. linker will map it to an output section. We can't just create it
  404. only if we need it, because we will not know whether we need it
  405. until we have seen all the input files, and the first time the
  406. main linker code calls BFD after examining all the input files
  407. (size_dynamic_sections) the input sections have already been
  408. mapped to the output sections. If the section turns out not to
  409. be needed, we can discard it later. We will never need this
  410. section when generating a shared object, since they do not use
  411. copy relocs. */
  412. if (bfd_link_executable (info))
  413. {
  414. s = bfd_make_section_anyway_with_flags (abfd,
  415. (bed->rela_plts_and_copies_p
  416. ? ".rela.bss" : ".rel.bss"),
  417. flags | SEC_READONLY);
  418. if (s == NULL
  419. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  420. return false;
  421. htab->srelbss = s;
  422. if (bed->want_dynrelro)
  423. {
  424. s = (bfd_make_section_anyway_with_flags
  425. (abfd, (bed->rela_plts_and_copies_p
  426. ? ".rela.data.rel.ro" : ".rel.data.rel.ro"),
  427. flags | SEC_READONLY));
  428. if (s == NULL
  429. || !bfd_set_section_alignment (s, bed->s->log_file_align))
  430. return false;
  431. htab->sreldynrelro = s;
  432. }
  433. }
  434. }
  435. return true;
  436. }
  437. /* Record a new dynamic symbol. We record the dynamic symbols as we
  438. read the input files, since we need to have a list of all of them
  439. before we can determine the final sizes of the output sections.
  440. Note that we may actually call this function even though we are not
  441. going to output any dynamic symbols; in some cases we know that a
  442. symbol should be in the dynamic symbol table, but only if there is
  443. one. */
  444. bool
  445. bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info,
  446. struct elf_link_hash_entry *h)
  447. {
  448. if (h->dynindx == -1)
  449. {
  450. struct elf_strtab_hash *dynstr;
  451. char *p;
  452. const char *name;
  453. size_t indx;
  454. if (h->root.type == bfd_link_hash_defined
  455. || h->root.type == bfd_link_hash_defweak)
  456. {
  457. /* An IR symbol should not be made dynamic. */
  458. if (h->root.u.def.section != NULL
  459. && h->root.u.def.section->owner != NULL
  460. && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)
  461. return true;
  462. }
  463. /* XXX: The ABI draft says the linker must turn hidden and
  464. internal symbols into STB_LOCAL symbols when producing the
  465. DSO. However, if ld.so honors st_other in the dynamic table,
  466. this would not be necessary. */
  467. switch (ELF_ST_VISIBILITY (h->other))
  468. {
  469. case STV_INTERNAL:
  470. case STV_HIDDEN:
  471. if (h->root.type != bfd_link_hash_undefined
  472. && h->root.type != bfd_link_hash_undefweak)
  473. {
  474. h->forced_local = 1;
  475. if (!elf_hash_table (info)->is_relocatable_executable
  476. || ((h->root.type == bfd_link_hash_defined
  477. || h->root.type == bfd_link_hash_defweak)
  478. && h->root.u.def.section->owner != NULL
  479. && h->root.u.def.section->owner->no_export)
  480. || (h->root.type == bfd_link_hash_common
  481. && h->root.u.c.p->section->owner != NULL
  482. && h->root.u.c.p->section->owner->no_export))
  483. return true;
  484. }
  485. default:
  486. break;
  487. }
  488. h->dynindx = elf_hash_table (info)->dynsymcount;
  489. ++elf_hash_table (info)->dynsymcount;
  490. dynstr = elf_hash_table (info)->dynstr;
  491. if (dynstr == NULL)
  492. {
  493. /* Create a strtab to hold the dynamic symbol names. */
  494. elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
  495. if (dynstr == NULL)
  496. return false;
  497. }
  498. /* We don't put any version information in the dynamic string
  499. table. */
  500. name = h->root.root.string;
  501. p = strchr (name, ELF_VER_CHR);
  502. if (p != NULL)
  503. /* We know that the p points into writable memory. In fact,
  504. there are only a few symbols that have read-only names, being
  505. those like _GLOBAL_OFFSET_TABLE_ that are created specially
  506. by the backends. Most symbols will have names pointing into
  507. an ELF string table read from a file, or to objalloc memory. */
  508. *p = 0;
  509. indx = _bfd_elf_strtab_add (dynstr, name, p != NULL);
  510. if (p != NULL)
  511. *p = ELF_VER_CHR;
  512. if (indx == (size_t) -1)
  513. return false;
  514. h->dynstr_index = indx;
  515. }
  516. return true;
  517. }
  518. /* Mark a symbol dynamic. */
  519. static void
  520. bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info,
  521. struct elf_link_hash_entry *h,
  522. Elf_Internal_Sym *sym)
  523. {
  524. struct bfd_elf_dynamic_list *d = info->dynamic_list;
  525. /* It may be called more than once on the same H. */
  526. if(h->dynamic || bfd_link_relocatable (info))
  527. return;
  528. if ((info->dynamic_data
  529. && (h->type == STT_OBJECT
  530. || h->type == STT_COMMON
  531. || (sym != NULL
  532. && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT
  533. || ELF_ST_TYPE (sym->st_info) == STT_COMMON))))
  534. || (d != NULL
  535. && h->non_elf
  536. && (*d->match) (&d->head, NULL, h->root.root.string)))
  537. {
  538. h->dynamic = 1;
  539. /* NB: If a symbol is made dynamic by --dynamic-list, it has
  540. non-IR reference. */
  541. h->root.non_ir_ref_dynamic = 1;
  542. }
  543. }
  544. /* Record an assignment to a symbol made by a linker script. We need
  545. this in case some dynamic object refers to this symbol. */
  546. bool
  547. bfd_elf_record_link_assignment (bfd *output_bfd,
  548. struct bfd_link_info *info,
  549. const char *name,
  550. bool provide,
  551. bool hidden)
  552. {
  553. struct elf_link_hash_entry *h, *hv;
  554. struct elf_link_hash_table *htab;
  555. const struct elf_backend_data *bed;
  556. if (!is_elf_hash_table (info->hash))
  557. return true;
  558. htab = elf_hash_table (info);
  559. h = elf_link_hash_lookup (htab, name, !provide, true, false);
  560. if (h == NULL)
  561. return provide;
  562. if (h->root.type == bfd_link_hash_warning)
  563. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  564. if (h->versioned == unknown)
  565. {
  566. /* Set versioned if symbol version is unknown. */
  567. char *version = strrchr (name, ELF_VER_CHR);
  568. if (version)
  569. {
  570. if (version > name && version[-1] != ELF_VER_CHR)
  571. h->versioned = versioned_hidden;
  572. else
  573. h->versioned = versioned;
  574. }
  575. }
  576. /* Symbols defined in a linker script but not referenced anywhere
  577. else will have non_elf set. */
  578. if (h->non_elf)
  579. {
  580. bfd_elf_link_mark_dynamic_symbol (info, h, NULL);
  581. h->non_elf = 0;
  582. }
  583. switch (h->root.type)
  584. {
  585. case bfd_link_hash_defined:
  586. case bfd_link_hash_defweak:
  587. case bfd_link_hash_common:
  588. break;
  589. case bfd_link_hash_undefweak:
  590. case bfd_link_hash_undefined:
  591. /* Since we're defining the symbol, don't let it seem to have not
  592. been defined. record_dynamic_symbol and size_dynamic_sections
  593. may depend on this. */
  594. h->root.type = bfd_link_hash_new;
  595. if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root)
  596. bfd_link_repair_undef_list (&htab->root);
  597. break;
  598. case bfd_link_hash_new:
  599. break;
  600. case bfd_link_hash_indirect:
  601. /* We had a versioned symbol in a dynamic library. We make the
  602. the versioned symbol point to this one. */
  603. bed = get_elf_backend_data (output_bfd);
  604. hv = h;
  605. while (hv->root.type == bfd_link_hash_indirect
  606. || hv->root.type == bfd_link_hash_warning)
  607. hv = (struct elf_link_hash_entry *) hv->root.u.i.link;
  608. /* We don't need to update h->root.u since linker will set them
  609. later. */
  610. h->root.type = bfd_link_hash_undefined;
  611. hv->root.type = bfd_link_hash_indirect;
  612. hv->root.u.i.link = (struct bfd_link_hash_entry *) h;
  613. (*bed->elf_backend_copy_indirect_symbol) (info, h, hv);
  614. break;
  615. default:
  616. BFD_FAIL ();
  617. return false;
  618. }
  619. /* If this symbol is being provided by the linker script, and it is
  620. currently defined by a dynamic object, but not by a regular
  621. object, then mark it as undefined so that the generic linker will
  622. force the correct value. */
  623. if (provide
  624. && h->def_dynamic
  625. && !h->def_regular)
  626. h->root.type = bfd_link_hash_undefined;
  627. /* If this symbol is currently defined by a dynamic object, but not
  628. by a regular object, then clear out any version information because
  629. the symbol will not be associated with the dynamic object any
  630. more. */
  631. if (h->def_dynamic && !h->def_regular)
  632. h->verinfo.verdef = NULL;
  633. /* Make sure this symbol is not garbage collected. */
  634. h->mark = 1;
  635. h->def_regular = 1;
  636. if (hidden)
  637. {
  638. bed = get_elf_backend_data (output_bfd);
  639. if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
  640. h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
  641. (*bed->elf_backend_hide_symbol) (info, h, true);
  642. }
  643. /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects
  644. and executables. */
  645. if (!bfd_link_relocatable (info)
  646. && h->dynindx != -1
  647. && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
  648. || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
  649. h->forced_local = 1;
  650. if ((h->def_dynamic
  651. || h->ref_dynamic
  652. || bfd_link_dll (info)
  653. || elf_hash_table (info)->is_relocatable_executable)
  654. && !h->forced_local
  655. && h->dynindx == -1)
  656. {
  657. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  658. return false;
  659. /* If this is a weak defined symbol, and we know a corresponding
  660. real symbol from the same dynamic object, make sure the real
  661. symbol is also made into a dynamic symbol. */
  662. if (h->is_weakalias)
  663. {
  664. struct elf_link_hash_entry *def = weakdef (h);
  665. if (def->dynindx == -1
  666. && !bfd_elf_link_record_dynamic_symbol (info, def))
  667. return false;
  668. }
  669. }
  670. return true;
  671. }
  672. /* Record a new local dynamic symbol. Returns 0 on failure, 1 on
  673. success, and 2 on a failure caused by attempting to record a symbol
  674. in a discarded section, eg. a discarded link-once section symbol. */
  675. int
  676. bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info,
  677. bfd *input_bfd,
  678. long input_indx)
  679. {
  680. size_t amt;
  681. struct elf_link_local_dynamic_entry *entry;
  682. struct elf_link_hash_table *eht;
  683. struct elf_strtab_hash *dynstr;
  684. size_t dynstr_index;
  685. char *name;
  686. Elf_External_Sym_Shndx eshndx;
  687. char esym[sizeof (Elf64_External_Sym)];
  688. if (! is_elf_hash_table (info->hash))
  689. return 0;
  690. /* See if the entry exists already. */
  691. for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
  692. if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
  693. return 1;
  694. amt = sizeof (*entry);
  695. entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt);
  696. if (entry == NULL)
  697. return 0;
  698. /* Go find the symbol, so that we can find it's name. */
  699. if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr,
  700. 1, input_indx, &entry->isym, esym, &eshndx))
  701. {
  702. bfd_release (input_bfd, entry);
  703. return 0;
  704. }
  705. if (entry->isym.st_shndx != SHN_UNDEF
  706. && entry->isym.st_shndx < SHN_LORESERVE)
  707. {
  708. asection *s;
  709. s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx);
  710. if (s == NULL || bfd_is_abs_section (s->output_section))
  711. {
  712. /* We can still bfd_release here as nothing has done another
  713. bfd_alloc. We can't do this later in this function. */
  714. bfd_release (input_bfd, entry);
  715. return 2;
  716. }
  717. }
  718. name = (bfd_elf_string_from_elf_section
  719. (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
  720. entry->isym.st_name));
  721. dynstr = elf_hash_table (info)->dynstr;
  722. if (dynstr == NULL)
  723. {
  724. /* Create a strtab to hold the dynamic symbol names. */
  725. elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init ();
  726. if (dynstr == NULL)
  727. return 0;
  728. }
  729. dynstr_index = _bfd_elf_strtab_add (dynstr, name, false);
  730. if (dynstr_index == (size_t) -1)
  731. return 0;
  732. entry->isym.st_name = dynstr_index;
  733. eht = elf_hash_table (info);
  734. entry->next = eht->dynlocal;
  735. eht->dynlocal = entry;
  736. entry->input_bfd = input_bfd;
  737. entry->input_indx = input_indx;
  738. eht->dynsymcount++;
  739. /* Whatever binding the symbol had before, it's now local. */
  740. entry->isym.st_info
  741. = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
  742. /* The dynindx will be set at the end of size_dynamic_sections. */
  743. return 1;
  744. }
  745. /* Return the dynindex of a local dynamic symbol. */
  746. long
  747. _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info,
  748. bfd *input_bfd,
  749. long input_indx)
  750. {
  751. struct elf_link_local_dynamic_entry *e;
  752. for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
  753. if (e->input_bfd == input_bfd && e->input_indx == input_indx)
  754. return e->dynindx;
  755. return -1;
  756. }
  757. /* This function is used to renumber the dynamic symbols, if some of
  758. them are removed because they are marked as local. This is called
  759. via elf_link_hash_traverse. */
  760. static bool
  761. elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h,
  762. void *data)
  763. {
  764. size_t *count = (size_t *) data;
  765. if (h->forced_local)
  766. return true;
  767. if (h->dynindx != -1)
  768. h->dynindx = ++(*count);
  769. return true;
  770. }
  771. /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with
  772. STB_LOCAL binding. */
  773. static bool
  774. elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h,
  775. void *data)
  776. {
  777. size_t *count = (size_t *) data;
  778. if (!h->forced_local)
  779. return true;
  780. if (h->dynindx != -1)
  781. h->dynindx = ++(*count);
  782. return true;
  783. }
  784. /* Return true if the dynamic symbol for a given section should be
  785. omitted when creating a shared library. */
  786. bool
  787. _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED,
  788. struct bfd_link_info *info,
  789. asection *p)
  790. {
  791. struct elf_link_hash_table *htab;
  792. asection *ip;
  793. switch (elf_section_data (p)->this_hdr.sh_type)
  794. {
  795. case SHT_PROGBITS:
  796. case SHT_NOBITS:
  797. /* If sh_type is yet undecided, assume it could be
  798. SHT_PROGBITS/SHT_NOBITS. */
  799. case SHT_NULL:
  800. htab = elf_hash_table (info);
  801. if (htab->text_index_section != NULL)
  802. return p != htab->text_index_section && p != htab->data_index_section;
  803. return (htab->dynobj != NULL
  804. && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL
  805. && ip->output_section == p);
  806. /* There shouldn't be section relative relocations
  807. against any other section. */
  808. default:
  809. return true;
  810. }
  811. }
  812. bool
  813. _bfd_elf_omit_section_dynsym_all
  814. (bfd *output_bfd ATTRIBUTE_UNUSED,
  815. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  816. asection *p ATTRIBUTE_UNUSED)
  817. {
  818. return true;
  819. }
  820. /* Assign dynsym indices. In a shared library we generate a section
  821. symbol for each output section, which come first. Next come symbols
  822. which have been forced to local binding. Then all of the back-end
  823. allocated local dynamic syms, followed by the rest of the global
  824. symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set.
  825. (This prevents the early call before elf_backend_init_index_section
  826. and strip_excluded_output_sections setting dynindx for sections
  827. that are stripped.) */
  828. static unsigned long
  829. _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
  830. struct bfd_link_info *info,
  831. unsigned long *section_sym_count)
  832. {
  833. unsigned long dynsymcount = 0;
  834. bool do_sec = section_sym_count != NULL;
  835. if (bfd_link_pic (info)
  836. || elf_hash_table (info)->is_relocatable_executable)
  837. {
  838. const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
  839. asection *p;
  840. for (p = output_bfd->sections; p ; p = p->next)
  841. if ((p->flags & SEC_EXCLUDE) == 0
  842. && (p->flags & SEC_ALLOC) != 0
  843. && elf_hash_table (info)->dynamic_relocs
  844. && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p))
  845. {
  846. ++dynsymcount;
  847. if (do_sec)
  848. elf_section_data (p)->dynindx = dynsymcount;
  849. }
  850. else if (do_sec)
  851. elf_section_data (p)->dynindx = 0;
  852. }
  853. if (do_sec)
  854. *section_sym_count = dynsymcount;
  855. elf_link_hash_traverse (elf_hash_table (info),
  856. elf_link_renumber_local_hash_table_dynsyms,
  857. &dynsymcount);
  858. if (elf_hash_table (info)->dynlocal)
  859. {
  860. struct elf_link_local_dynamic_entry *p;
  861. for (p = elf_hash_table (info)->dynlocal; p ; p = p->next)
  862. p->dynindx = ++dynsymcount;
  863. }
  864. elf_hash_table (info)->local_dynsymcount = dynsymcount;
  865. elf_link_hash_traverse (elf_hash_table (info),
  866. elf_link_renumber_hash_table_dynsyms,
  867. &dynsymcount);
  868. /* There is an unused NULL entry at the head of the table which we
  869. must account for in our count even if the table is empty since it
  870. is intended for the mandatory DT_SYMTAB tag (.dynsym section) in
  871. .dynamic section. */
  872. dynsymcount++;
  873. elf_hash_table (info)->dynsymcount = dynsymcount;
  874. return dynsymcount;
  875. }
  876. /* Merge st_other field. */
  877. static void
  878. elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
  879. unsigned int st_other, asection *sec,
  880. bool definition, bool dynamic)
  881. {
  882. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  883. /* If st_other has a processor-specific meaning, specific
  884. code might be needed here. */
  885. if (bed->elf_backend_merge_symbol_attribute)
  886. (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition,
  887. dynamic);
  888. if (!dynamic)
  889. {
  890. unsigned symvis = ELF_ST_VISIBILITY (st_other);
  891. unsigned hvis = ELF_ST_VISIBILITY (h->other);
  892. /* Keep the most constraining visibility. Leave the remainder
  893. of the st_other field to elf_backend_merge_symbol_attribute. */
  894. if (symvis - 1 < hvis - 1)
  895. h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
  896. }
  897. else if (definition
  898. && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT
  899. && (sec->flags & SEC_READONLY) == 0)
  900. h->protected_def = 1;
  901. }
  902. /* This function is called when we want to merge a new symbol with an
  903. existing symbol. It handles the various cases which arise when we
  904. find a definition in a dynamic object, or when there is already a
  905. definition in a dynamic object. The new symbol is described by
  906. NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table
  907. entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK
  908. if the old symbol was weak. We set POLD_ALIGNMENT to the alignment
  909. of an old common symbol. We set OVERRIDE if the old symbol is
  910. overriding a new definition. We set TYPE_CHANGE_OK if it is OK for
  911. the type to change. We set SIZE_CHANGE_OK if it is OK for the size
  912. to change. By OK to change, we mean that we shouldn't warn if the
  913. type or size does change. */
  914. static bool
  915. _bfd_elf_merge_symbol (bfd *abfd,
  916. struct bfd_link_info *info,
  917. const char *name,
  918. Elf_Internal_Sym *sym,
  919. asection **psec,
  920. bfd_vma *pvalue,
  921. struct elf_link_hash_entry **sym_hash,
  922. bfd **poldbfd,
  923. bool *pold_weak,
  924. unsigned int *pold_alignment,
  925. bool *skip,
  926. bfd **override,
  927. bool *type_change_ok,
  928. bool *size_change_ok,
  929. bool *matched)
  930. {
  931. asection *sec, *oldsec;
  932. struct elf_link_hash_entry *h;
  933. struct elf_link_hash_entry *hi;
  934. struct elf_link_hash_entry *flip;
  935. int bind;
  936. bfd *oldbfd;
  937. bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
  938. bool newweak, oldweak, newfunc, oldfunc;
  939. const struct elf_backend_data *bed;
  940. char *new_version;
  941. bool default_sym = *matched;
  942. struct elf_link_hash_table *htab;
  943. *skip = false;
  944. *override = NULL;
  945. sec = *psec;
  946. bind = ELF_ST_BIND (sym->st_info);
  947. if (! bfd_is_und_section (sec))
  948. h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
  949. else
  950. h = ((struct elf_link_hash_entry *)
  951. bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
  952. if (h == NULL)
  953. return false;
  954. *sym_hash = h;
  955. bed = get_elf_backend_data (abfd);
  956. /* NEW_VERSION is the symbol version of the new symbol. */
  957. if (h->versioned != unversioned)
  958. {
  959. /* Symbol version is unknown or versioned. */
  960. new_version = strrchr (name, ELF_VER_CHR);
  961. if (new_version)
  962. {
  963. if (h->versioned == unknown)
  964. {
  965. if (new_version > name && new_version[-1] != ELF_VER_CHR)
  966. h->versioned = versioned_hidden;
  967. else
  968. h->versioned = versioned;
  969. }
  970. new_version += 1;
  971. if (new_version[0] == '\0')
  972. new_version = NULL;
  973. }
  974. else
  975. h->versioned = unversioned;
  976. }
  977. else
  978. new_version = NULL;
  979. /* For merging, we only care about real symbols. But we need to make
  980. sure that indirect symbol dynamic flags are updated. */
  981. hi = h;
  982. while (h->root.type == bfd_link_hash_indirect
  983. || h->root.type == bfd_link_hash_warning)
  984. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  985. if (!*matched)
  986. {
  987. if (hi == h || h->root.type == bfd_link_hash_new)
  988. *matched = true;
  989. else
  990. {
  991. /* OLD_HIDDEN is true if the existing symbol is only visible
  992. to the symbol with the same symbol version. NEW_HIDDEN is
  993. true if the new symbol is only visible to the symbol with
  994. the same symbol version. */
  995. bool old_hidden = h->versioned == versioned_hidden;
  996. bool new_hidden = hi->versioned == versioned_hidden;
  997. if (!old_hidden && !new_hidden)
  998. /* The new symbol matches the existing symbol if both
  999. aren't hidden. */
  1000. *matched = true;
  1001. else
  1002. {
  1003. /* OLD_VERSION is the symbol version of the existing
  1004. symbol. */
  1005. char *old_version;
  1006. if (h->versioned >= versioned)
  1007. old_version = strrchr (h->root.root.string,
  1008. ELF_VER_CHR) + 1;
  1009. else
  1010. old_version = NULL;
  1011. /* The new symbol matches the existing symbol if they
  1012. have the same symbol version. */
  1013. *matched = (old_version == new_version
  1014. || (old_version != NULL
  1015. && new_version != NULL
  1016. && strcmp (old_version, new_version) == 0));
  1017. }
  1018. }
  1019. }
  1020. /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the
  1021. existing symbol. */
  1022. oldbfd = NULL;
  1023. oldsec = NULL;
  1024. switch (h->root.type)
  1025. {
  1026. default:
  1027. break;
  1028. case bfd_link_hash_undefined:
  1029. case bfd_link_hash_undefweak:
  1030. oldbfd = h->root.u.undef.abfd;
  1031. break;
  1032. case bfd_link_hash_defined:
  1033. case bfd_link_hash_defweak:
  1034. oldbfd = h->root.u.def.section->owner;
  1035. oldsec = h->root.u.def.section;
  1036. break;
  1037. case bfd_link_hash_common:
  1038. oldbfd = h->root.u.c.p->section->owner;
  1039. oldsec = h->root.u.c.p->section;
  1040. if (pold_alignment)
  1041. *pold_alignment = h->root.u.c.p->alignment_power;
  1042. break;
  1043. }
  1044. if (poldbfd && *poldbfd == NULL)
  1045. *poldbfd = oldbfd;
  1046. /* Differentiate strong and weak symbols. */
  1047. newweak = bind == STB_WEAK;
  1048. oldweak = (h->root.type == bfd_link_hash_defweak
  1049. || h->root.type == bfd_link_hash_undefweak);
  1050. if (pold_weak)
  1051. *pold_weak = oldweak;
  1052. /* We have to check it for every instance since the first few may be
  1053. references and not all compilers emit symbol type for undefined
  1054. symbols. */
  1055. bfd_elf_link_mark_dynamic_symbol (info, h, sym);
  1056. htab = elf_hash_table (info);
  1057. /* NEWDYN and OLDDYN indicate whether the new or old symbol,
  1058. respectively, is from a dynamic object. */
  1059. newdyn = (abfd->flags & DYNAMIC) != 0;
  1060. /* ref_dynamic_nonweak and dynamic_def flags track actual undefined
  1061. syms and defined syms in dynamic libraries respectively.
  1062. ref_dynamic on the other hand can be set for a symbol defined in
  1063. a dynamic library, and def_dynamic may not be set; When the
  1064. definition in a dynamic lib is overridden by a definition in the
  1065. executable use of the symbol in the dynamic lib becomes a
  1066. reference to the executable symbol. */
  1067. if (newdyn)
  1068. {
  1069. if (bfd_is_und_section (sec))
  1070. {
  1071. if (bind != STB_WEAK)
  1072. {
  1073. h->ref_dynamic_nonweak = 1;
  1074. hi->ref_dynamic_nonweak = 1;
  1075. }
  1076. }
  1077. else
  1078. {
  1079. /* Update the existing symbol only if they match. */
  1080. if (*matched)
  1081. h->dynamic_def = 1;
  1082. hi->dynamic_def = 1;
  1083. }
  1084. }
  1085. /* If we just created the symbol, mark it as being an ELF symbol.
  1086. Other than that, there is nothing to do--there is no merge issue
  1087. with a newly defined symbol--so we just return. */
  1088. if (h->root.type == bfd_link_hash_new)
  1089. {
  1090. h->non_elf = 0;
  1091. return true;
  1092. }
  1093. /* In cases involving weak versioned symbols, we may wind up trying
  1094. to merge a symbol with itself. Catch that here, to avoid the
  1095. confusion that results if we try to override a symbol with
  1096. itself. The additional tests catch cases like
  1097. _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
  1098. dynamic object, which we do want to handle here. */
  1099. if (abfd == oldbfd
  1100. && (newweak || oldweak)
  1101. && ((abfd->flags & DYNAMIC) == 0
  1102. || !h->def_regular))
  1103. return true;
  1104. olddyn = false;
  1105. if (oldbfd != NULL)
  1106. olddyn = (oldbfd->flags & DYNAMIC) != 0;
  1107. else if (oldsec != NULL)
  1108. {
  1109. /* This handles the special SHN_MIPS_{TEXT,DATA} section
  1110. indices used by MIPS ELF. */
  1111. olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0;
  1112. }
  1113. /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */
  1114. if (!htab->handling_dt_needed
  1115. && oldbfd != NULL
  1116. && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN))
  1117. {
  1118. if (newdyn != olddyn)
  1119. {
  1120. /* Handle a case where plugin_notice won't be called and thus
  1121. won't set the non_ir_ref flags on the first pass over
  1122. symbols. */
  1123. h->root.non_ir_ref_dynamic = true;
  1124. hi->root.non_ir_ref_dynamic = true;
  1125. }
  1126. else if ((oldbfd->flags & BFD_PLUGIN) != 0
  1127. && hi->root.type == bfd_link_hash_indirect)
  1128. {
  1129. /* Change indirect symbol from IR to undefined. */
  1130. hi->root.type = bfd_link_hash_undefined;
  1131. hi->root.u.undef.abfd = oldbfd;
  1132. }
  1133. }
  1134. /* NEWDEF and OLDDEF indicate whether the new or old symbol,
  1135. respectively, appear to be a definition rather than reference. */
  1136. newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec);
  1137. olddef = (h->root.type != bfd_link_hash_undefined
  1138. && h->root.type != bfd_link_hash_undefweak
  1139. && h->root.type != bfd_link_hash_common);
  1140. /* NEWFUNC and OLDFUNC indicate whether the new or old symbol,
  1141. respectively, appear to be a function. */
  1142. newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
  1143. && bed->is_function_type (ELF_ST_TYPE (sym->st_info)));
  1144. oldfunc = (h->type != STT_NOTYPE
  1145. && bed->is_function_type (h->type));
  1146. if (!(newfunc && oldfunc)
  1147. && ELF_ST_TYPE (sym->st_info) != h->type
  1148. && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE
  1149. && h->type != STT_NOTYPE
  1150. && (newdef || bfd_is_com_section (sec))
  1151. && (olddef || h->root.type == bfd_link_hash_common))
  1152. {
  1153. /* If creating a default indirect symbol ("foo" or "foo@") from
  1154. a dynamic versioned definition ("foo@@") skip doing so if
  1155. there is an existing regular definition with a different
  1156. type. We don't want, for example, a "time" variable in the
  1157. executable overriding a "time" function in a shared library. */
  1158. if (newdyn
  1159. && !olddyn)
  1160. {
  1161. *skip = true;
  1162. return true;
  1163. }
  1164. /* When adding a symbol from a regular object file after we have
  1165. created indirect symbols, undo the indirection and any
  1166. dynamic state. */
  1167. if (hi != h
  1168. && !newdyn
  1169. && olddyn)
  1170. {
  1171. h = hi;
  1172. (*bed->elf_backend_hide_symbol) (info, h, true);
  1173. h->forced_local = 0;
  1174. h->ref_dynamic = 0;
  1175. h->def_dynamic = 0;
  1176. h->dynamic_def = 0;
  1177. if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
  1178. {
  1179. h->root.type = bfd_link_hash_undefined;
  1180. h->root.u.undef.abfd = abfd;
  1181. }
  1182. else
  1183. {
  1184. h->root.type = bfd_link_hash_new;
  1185. h->root.u.undef.abfd = NULL;
  1186. }
  1187. return true;
  1188. }
  1189. }
  1190. /* Check TLS symbols. We don't check undefined symbols introduced
  1191. by "ld -u" which have no type (and oldbfd NULL), and we don't
  1192. check symbols from plugins because they also have no type. */
  1193. if (oldbfd != NULL
  1194. && (oldbfd->flags & BFD_PLUGIN) == 0
  1195. && (abfd->flags & BFD_PLUGIN) == 0
  1196. && ELF_ST_TYPE (sym->st_info) != h->type
  1197. && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS))
  1198. {
  1199. bfd *ntbfd, *tbfd;
  1200. bool ntdef, tdef;
  1201. asection *ntsec, *tsec;
  1202. if (h->type == STT_TLS)
  1203. {
  1204. ntbfd = abfd;
  1205. ntsec = sec;
  1206. ntdef = newdef;
  1207. tbfd = oldbfd;
  1208. tsec = oldsec;
  1209. tdef = olddef;
  1210. }
  1211. else
  1212. {
  1213. ntbfd = oldbfd;
  1214. ntsec = oldsec;
  1215. ntdef = olddef;
  1216. tbfd = abfd;
  1217. tsec = sec;
  1218. tdef = newdef;
  1219. }
  1220. if (tdef && ntdef)
  1221. _bfd_error_handler
  1222. /* xgettext:c-format */
  1223. (_("%s: TLS definition in %pB section %pA "
  1224. "mismatches non-TLS definition in %pB section %pA"),
  1225. h->root.root.string, tbfd, tsec, ntbfd, ntsec);
  1226. else if (!tdef && !ntdef)
  1227. _bfd_error_handler
  1228. /* xgettext:c-format */
  1229. (_("%s: TLS reference in %pB "
  1230. "mismatches non-TLS reference in %pB"),
  1231. h->root.root.string, tbfd, ntbfd);
  1232. else if (tdef)
  1233. _bfd_error_handler
  1234. /* xgettext:c-format */
  1235. (_("%s: TLS definition in %pB section %pA "
  1236. "mismatches non-TLS reference in %pB"),
  1237. h->root.root.string, tbfd, tsec, ntbfd);
  1238. else
  1239. _bfd_error_handler
  1240. /* xgettext:c-format */
  1241. (_("%s: TLS reference in %pB "
  1242. "mismatches non-TLS definition in %pB section %pA"),
  1243. h->root.root.string, tbfd, ntbfd, ntsec);
  1244. bfd_set_error (bfd_error_bad_value);
  1245. return false;
  1246. }
  1247. /* If the old symbol has non-default visibility, we ignore the new
  1248. definition from a dynamic object. */
  1249. if (newdyn
  1250. && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  1251. && !bfd_is_und_section (sec))
  1252. {
  1253. *skip = true;
  1254. /* Make sure this symbol is dynamic. */
  1255. h->ref_dynamic = 1;
  1256. hi->ref_dynamic = 1;
  1257. /* A protected symbol has external availability. Make sure it is
  1258. recorded as dynamic.
  1259. FIXME: Should we check type and size for protected symbol? */
  1260. if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
  1261. return bfd_elf_link_record_dynamic_symbol (info, h);
  1262. else
  1263. return true;
  1264. }
  1265. else if (!newdyn
  1266. && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT
  1267. && h->def_dynamic)
  1268. {
  1269. /* If the new symbol with non-default visibility comes from a
  1270. relocatable file and the old definition comes from a dynamic
  1271. object, we remove the old definition. */
  1272. if (hi->root.type == bfd_link_hash_indirect)
  1273. {
  1274. /* Handle the case where the old dynamic definition is
  1275. default versioned. We need to copy the symbol info from
  1276. the symbol with default version to the normal one if it
  1277. was referenced before. */
  1278. if (h->ref_regular)
  1279. {
  1280. hi->root.type = h->root.type;
  1281. h->root.type = bfd_link_hash_indirect;
  1282. (*bed->elf_backend_copy_indirect_symbol) (info, hi, h);
  1283. h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
  1284. if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
  1285. {
  1286. /* If the new symbol is hidden or internal, completely undo
  1287. any dynamic link state. */
  1288. (*bed->elf_backend_hide_symbol) (info, h, true);
  1289. h->forced_local = 0;
  1290. h->ref_dynamic = 0;
  1291. }
  1292. else
  1293. h->ref_dynamic = 1;
  1294. h->def_dynamic = 0;
  1295. /* FIXME: Should we check type and size for protected symbol? */
  1296. h->size = 0;
  1297. h->type = 0;
  1298. h = hi;
  1299. }
  1300. else
  1301. h = hi;
  1302. }
  1303. /* If the old symbol was undefined before, then it will still be
  1304. on the undefs list. If the new symbol is undefined or
  1305. common, we can't make it bfd_link_hash_new here, because new
  1306. undefined or common symbols will be added to the undefs list
  1307. by _bfd_generic_link_add_one_symbol. Symbols may not be
  1308. added twice to the undefs list. Also, if the new symbol is
  1309. undefweak then we don't want to lose the strong undef. */
  1310. if (h->root.u.undef.next || info->hash->undefs_tail == &h->root)
  1311. {
  1312. h->root.type = bfd_link_hash_undefined;
  1313. h->root.u.undef.abfd = abfd;
  1314. }
  1315. else
  1316. {
  1317. h->root.type = bfd_link_hash_new;
  1318. h->root.u.undef.abfd = NULL;
  1319. }
  1320. if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED)
  1321. {
  1322. /* If the new symbol is hidden or internal, completely undo
  1323. any dynamic link state. */
  1324. (*bed->elf_backend_hide_symbol) (info, h, true);
  1325. h->forced_local = 0;
  1326. h->ref_dynamic = 0;
  1327. }
  1328. else
  1329. h->ref_dynamic = 1;
  1330. h->def_dynamic = 0;
  1331. /* FIXME: Should we check type and size for protected symbol? */
  1332. h->size = 0;
  1333. h->type = 0;
  1334. return true;
  1335. }
  1336. /* If a new weak symbol definition comes from a regular file and the
  1337. old symbol comes from a dynamic library, we treat the new one as
  1338. strong. Similarly, an old weak symbol definition from a regular
  1339. file is treated as strong when the new symbol comes from a dynamic
  1340. library. Further, an old weak symbol from a dynamic library is
  1341. treated as strong if the new symbol is from a dynamic library.
  1342. This reflects the way glibc's ld.so works.
  1343. Also allow a weak symbol to override a linker script symbol
  1344. defined by an early pass over the script. This is done so the
  1345. linker knows the symbol is defined in an object file, for the
  1346. DEFINED script function.
  1347. Do this before setting *type_change_ok or *size_change_ok so that
  1348. we warn properly when dynamic library symbols are overridden. */
  1349. if (newdef && !newdyn && (olddyn || h->root.ldscript_def))
  1350. newweak = false;
  1351. if (olddef && newdyn)
  1352. oldweak = false;
  1353. /* Allow changes between different types of function symbol. */
  1354. if (newfunc && oldfunc)
  1355. *type_change_ok = true;
  1356. /* It's OK to change the type if either the existing symbol or the
  1357. new symbol is weak. A type change is also OK if the old symbol
  1358. is undefined and the new symbol is defined. */
  1359. if (oldweak
  1360. || newweak
  1361. || (newdef
  1362. && h->root.type == bfd_link_hash_undefined))
  1363. *type_change_ok = true;
  1364. /* It's OK to change the size if either the existing symbol or the
  1365. new symbol is weak, or if the old symbol is undefined. */
  1366. if (*type_change_ok
  1367. || h->root.type == bfd_link_hash_undefined)
  1368. *size_change_ok = true;
  1369. /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
  1370. symbol, respectively, appears to be a common symbol in a dynamic
  1371. object. If a symbol appears in an uninitialized section, and is
  1372. not weak, and is not a function, then it may be a common symbol
  1373. which was resolved when the dynamic object was created. We want
  1374. to treat such symbols specially, because they raise special
  1375. considerations when setting the symbol size: if the symbol
  1376. appears as a common symbol in a regular object, and the size in
  1377. the regular object is larger, we must make sure that we use the
  1378. larger size. This problematic case can always be avoided in C,
  1379. but it must be handled correctly when using Fortran shared
  1380. libraries.
  1381. Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
  1382. likewise for OLDDYNCOMMON and OLDDEF.
  1383. Note that this test is just a heuristic, and that it is quite
  1384. possible to have an uninitialized symbol in a shared object which
  1385. is really a definition, rather than a common symbol. This could
  1386. lead to some minor confusion when the symbol really is a common
  1387. symbol in some regular object. However, I think it will be
  1388. harmless. */
  1389. if (newdyn
  1390. && newdef
  1391. && !newweak
  1392. && (sec->flags & SEC_ALLOC) != 0
  1393. && (sec->flags & SEC_LOAD) == 0
  1394. && sym->st_size > 0
  1395. && !newfunc)
  1396. newdyncommon = true;
  1397. else
  1398. newdyncommon = false;
  1399. if (olddyn
  1400. && olddef
  1401. && h->root.type == bfd_link_hash_defined
  1402. && h->def_dynamic
  1403. && (h->root.u.def.section->flags & SEC_ALLOC) != 0
  1404. && (h->root.u.def.section->flags & SEC_LOAD) == 0
  1405. && h->size > 0
  1406. && !oldfunc)
  1407. olddyncommon = true;
  1408. else
  1409. olddyncommon = false;
  1410. /* We now know everything about the old and new symbols. We ask the
  1411. backend to check if we can merge them. */
  1412. if (bed->merge_symbol != NULL)
  1413. {
  1414. if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec))
  1415. return false;
  1416. sec = *psec;
  1417. }
  1418. /* There are multiple definitions of a normal symbol. Skip the
  1419. default symbol as well as definition from an IR object. */
  1420. if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak
  1421. && !default_sym && h->def_regular
  1422. && !(oldbfd != NULL
  1423. && (oldbfd->flags & BFD_PLUGIN) != 0
  1424. && (abfd->flags & BFD_PLUGIN) == 0))
  1425. {
  1426. /* Handle a multiple definition. */
  1427. (*info->callbacks->multiple_definition) (info, &h->root,
  1428. abfd, sec, *pvalue);
  1429. *skip = true;
  1430. return true;
  1431. }
  1432. /* If both the old and the new symbols look like common symbols in a
  1433. dynamic object, set the size of the symbol to the larger of the
  1434. two. */
  1435. if (olddyncommon
  1436. && newdyncommon
  1437. && sym->st_size != h->size)
  1438. {
  1439. /* Since we think we have two common symbols, issue a multiple
  1440. common warning if desired. Note that we only warn if the
  1441. size is different. If the size is the same, we simply let
  1442. the old symbol override the new one as normally happens with
  1443. symbols defined in dynamic objects. */
  1444. (*info->callbacks->multiple_common) (info, &h->root, abfd,
  1445. bfd_link_hash_common, sym->st_size);
  1446. if (sym->st_size > h->size)
  1447. h->size = sym->st_size;
  1448. *size_change_ok = true;
  1449. }
  1450. /* If we are looking at a dynamic object, and we have found a
  1451. definition, we need to see if the symbol was already defined by
  1452. some other object. If so, we want to use the existing
  1453. definition, and we do not want to report a multiple symbol
  1454. definition error; we do this by clobbering *PSEC to be
  1455. bfd_und_section_ptr.
  1456. We treat a common symbol as a definition if the symbol in the
  1457. shared library is a function, since common symbols always
  1458. represent variables; this can cause confusion in principle, but
  1459. any such confusion would seem to indicate an erroneous program or
  1460. shared library. We also permit a common symbol in a regular
  1461. object to override a weak symbol in a shared object. */
  1462. if (newdyn
  1463. && newdef
  1464. && (olddef
  1465. || (h->root.type == bfd_link_hash_common
  1466. && (newweak || newfunc))))
  1467. {
  1468. *override = abfd;
  1469. newdef = false;
  1470. newdyncommon = false;
  1471. *psec = sec = bfd_und_section_ptr;
  1472. *size_change_ok = true;
  1473. /* If we get here when the old symbol is a common symbol, then
  1474. we are explicitly letting it override a weak symbol or
  1475. function in a dynamic object, and we don't want to warn about
  1476. a type change. If the old symbol is a defined symbol, a type
  1477. change warning may still be appropriate. */
  1478. if (h->root.type == bfd_link_hash_common)
  1479. *type_change_ok = true;
  1480. }
  1481. /* Handle the special case of an old common symbol merging with a
  1482. new symbol which looks like a common symbol in a shared object.
  1483. We change *PSEC and *PVALUE to make the new symbol look like a
  1484. common symbol, and let _bfd_generic_link_add_one_symbol do the
  1485. right thing. */
  1486. if (newdyncommon
  1487. && h->root.type == bfd_link_hash_common)
  1488. {
  1489. *override = oldbfd;
  1490. newdef = false;
  1491. newdyncommon = false;
  1492. *pvalue = sym->st_size;
  1493. *psec = sec = bed->common_section (oldsec);
  1494. *size_change_ok = true;
  1495. }
  1496. /* Skip weak definitions of symbols that are already defined. */
  1497. if (newdef && olddef && newweak)
  1498. {
  1499. /* Don't skip new non-IR weak syms. */
  1500. if (!(oldbfd != NULL
  1501. && (oldbfd->flags & BFD_PLUGIN) != 0
  1502. && (abfd->flags & BFD_PLUGIN) == 0))
  1503. {
  1504. newdef = false;
  1505. *skip = true;
  1506. }
  1507. /* Merge st_other. If the symbol already has a dynamic index,
  1508. but visibility says it should not be visible, turn it into a
  1509. local symbol. */
  1510. elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn);
  1511. if (h->dynindx != -1)
  1512. switch (ELF_ST_VISIBILITY (h->other))
  1513. {
  1514. case STV_INTERNAL:
  1515. case STV_HIDDEN:
  1516. (*bed->elf_backend_hide_symbol) (info, h, true);
  1517. break;
  1518. }
  1519. }
  1520. /* If the old symbol is from a dynamic object, and the new symbol is
  1521. a definition which is not from a dynamic object, then the new
  1522. symbol overrides the old symbol. Symbols from regular files
  1523. always take precedence over symbols from dynamic objects, even if
  1524. they are defined after the dynamic object in the link.
  1525. As above, we again permit a common symbol in a regular object to
  1526. override a definition in a shared object if the shared object
  1527. symbol is a function or is weak. */
  1528. flip = NULL;
  1529. if (!newdyn
  1530. && (newdef
  1531. || (bfd_is_com_section (sec)
  1532. && (oldweak || oldfunc)))
  1533. && olddyn
  1534. && olddef
  1535. && h->def_dynamic)
  1536. {
  1537. /* Change the hash table entry to undefined, and let
  1538. _bfd_generic_link_add_one_symbol do the right thing with the
  1539. new definition. */
  1540. h->root.type = bfd_link_hash_undefined;
  1541. h->root.u.undef.abfd = h->root.u.def.section->owner;
  1542. *size_change_ok = true;
  1543. olddef = false;
  1544. olddyncommon = false;
  1545. /* We again permit a type change when a common symbol may be
  1546. overriding a function. */
  1547. if (bfd_is_com_section (sec))
  1548. {
  1549. if (oldfunc)
  1550. {
  1551. /* If a common symbol overrides a function, make sure
  1552. that it isn't defined dynamically nor has type
  1553. function. */
  1554. h->def_dynamic = 0;
  1555. h->type = STT_NOTYPE;
  1556. }
  1557. *type_change_ok = true;
  1558. }
  1559. if (hi->root.type == bfd_link_hash_indirect)
  1560. flip = hi;
  1561. else
  1562. /* This union may have been set to be non-NULL when this symbol
  1563. was seen in a dynamic object. We must force the union to be
  1564. NULL, so that it is correct for a regular symbol. */
  1565. h->verinfo.vertree = NULL;
  1566. }
  1567. /* Handle the special case of a new common symbol merging with an
  1568. old symbol that looks like it might be a common symbol defined in
  1569. a shared object. Note that we have already handled the case in
  1570. which a new common symbol should simply override the definition
  1571. in the shared library. */
  1572. if (! newdyn
  1573. && bfd_is_com_section (sec)
  1574. && olddyncommon)
  1575. {
  1576. /* It would be best if we could set the hash table entry to a
  1577. common symbol, but we don't know what to use for the section
  1578. or the alignment. */
  1579. (*info->callbacks->multiple_common) (info, &h->root, abfd,
  1580. bfd_link_hash_common, sym->st_size);
  1581. /* If the presumed common symbol in the dynamic object is
  1582. larger, pretend that the new symbol has its size. */
  1583. if (h->size > *pvalue)
  1584. *pvalue = h->size;
  1585. /* We need to remember the alignment required by the symbol
  1586. in the dynamic object. */
  1587. BFD_ASSERT (pold_alignment);
  1588. *pold_alignment = h->root.u.def.section->alignment_power;
  1589. olddef = false;
  1590. olddyncommon = false;
  1591. h->root.type = bfd_link_hash_undefined;
  1592. h->root.u.undef.abfd = h->root.u.def.section->owner;
  1593. *size_change_ok = true;
  1594. *type_change_ok = true;
  1595. if (hi->root.type == bfd_link_hash_indirect)
  1596. flip = hi;
  1597. else
  1598. h->verinfo.vertree = NULL;
  1599. }
  1600. if (flip != NULL)
  1601. {
  1602. /* Handle the case where we had a versioned symbol in a dynamic
  1603. library and now find a definition in a normal object. In this
  1604. case, we make the versioned symbol point to the normal one. */
  1605. flip->root.type = h->root.type;
  1606. flip->root.u.undef.abfd = h->root.u.undef.abfd;
  1607. h->root.type = bfd_link_hash_indirect;
  1608. h->root.u.i.link = (struct bfd_link_hash_entry *) flip;
  1609. (*bed->elf_backend_copy_indirect_symbol) (info, flip, h);
  1610. if (h->def_dynamic)
  1611. {
  1612. h->def_dynamic = 0;
  1613. flip->ref_dynamic = 1;
  1614. }
  1615. }
  1616. return true;
  1617. }
  1618. /* This function is called to create an indirect symbol from the
  1619. default for the symbol with the default version if needed. The
  1620. symbol is described by H, NAME, SYM, SEC, and VALUE. We
  1621. set DYNSYM if the new indirect symbol is dynamic. */
  1622. static bool
  1623. _bfd_elf_add_default_symbol (bfd *abfd,
  1624. struct bfd_link_info *info,
  1625. struct elf_link_hash_entry *h,
  1626. const char *name,
  1627. Elf_Internal_Sym *sym,
  1628. asection *sec,
  1629. bfd_vma value,
  1630. bfd **poldbfd,
  1631. bool *dynsym)
  1632. {
  1633. bool type_change_ok;
  1634. bool size_change_ok;
  1635. bool skip;
  1636. char *shortname;
  1637. struct elf_link_hash_entry *hi;
  1638. struct bfd_link_hash_entry *bh;
  1639. const struct elf_backend_data *bed;
  1640. bool collect;
  1641. bool dynamic;
  1642. bfd *override;
  1643. char *p;
  1644. size_t len, shortlen;
  1645. asection *tmp_sec;
  1646. bool matched;
  1647. if (h->versioned == unversioned || h->versioned == versioned_hidden)
  1648. return true;
  1649. /* If this symbol has a version, and it is the default version, we
  1650. create an indirect symbol from the default name to the fully
  1651. decorated name. This will cause external references which do not
  1652. specify a version to be bound to this version of the symbol. */
  1653. p = strchr (name, ELF_VER_CHR);
  1654. if (h->versioned == unknown)
  1655. {
  1656. if (p == NULL)
  1657. {
  1658. h->versioned = unversioned;
  1659. return true;
  1660. }
  1661. else
  1662. {
  1663. if (p[1] != ELF_VER_CHR)
  1664. {
  1665. h->versioned = versioned_hidden;
  1666. return true;
  1667. }
  1668. else
  1669. h->versioned = versioned;
  1670. }
  1671. }
  1672. else
  1673. {
  1674. /* PR ld/19073: We may see an unversioned definition after the
  1675. default version. */
  1676. if (p == NULL)
  1677. return true;
  1678. }
  1679. bed = get_elf_backend_data (abfd);
  1680. collect = bed->collect;
  1681. dynamic = (abfd->flags & DYNAMIC) != 0;
  1682. shortlen = p - name;
  1683. shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1);
  1684. if (shortname == NULL)
  1685. return false;
  1686. memcpy (shortname, name, shortlen);
  1687. shortname[shortlen] = '\0';
  1688. /* We are going to create a new symbol. Merge it with any existing
  1689. symbol with this name. For the purposes of the merge, act as
  1690. though we were defining the symbol we just defined, although we
  1691. actually going to define an indirect symbol. */
  1692. type_change_ok = false;
  1693. size_change_ok = false;
  1694. matched = true;
  1695. tmp_sec = sec;
  1696. if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
  1697. &hi, poldbfd, NULL, NULL, &skip, &override,
  1698. &type_change_ok, &size_change_ok, &matched))
  1699. return false;
  1700. if (skip)
  1701. goto nondefault;
  1702. if (hi->def_regular || ELF_COMMON_DEF_P (hi))
  1703. {
  1704. /* If the undecorated symbol will have a version added by a
  1705. script different to H, then don't indirect to/from the
  1706. undecorated symbol. This isn't ideal because we may not yet
  1707. have seen symbol versions, if given by a script on the
  1708. command line rather than via --version-script. */
  1709. if (hi->verinfo.vertree == NULL && info->version_info != NULL)
  1710. {
  1711. bool hide;
  1712. hi->verinfo.vertree
  1713. = bfd_find_version_for_sym (info->version_info,
  1714. hi->root.root.string, &hide);
  1715. if (hi->verinfo.vertree != NULL && hide)
  1716. {
  1717. (*bed->elf_backend_hide_symbol) (info, hi, true);
  1718. goto nondefault;
  1719. }
  1720. }
  1721. if (hi->verinfo.vertree != NULL
  1722. && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0)
  1723. goto nondefault;
  1724. }
  1725. if (! override)
  1726. {
  1727. /* Add the default symbol if not performing a relocatable link. */
  1728. if (! bfd_link_relocatable (info))
  1729. {
  1730. bh = &hi->root;
  1731. if (bh->type == bfd_link_hash_defined
  1732. && bh->u.def.section->owner != NULL
  1733. && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0)
  1734. {
  1735. /* Mark the previous definition from IR object as
  1736. undefined so that the generic linker will override
  1737. it. */
  1738. bh->type = bfd_link_hash_undefined;
  1739. bh->u.undef.abfd = bh->u.def.section->owner;
  1740. }
  1741. if (! (_bfd_generic_link_add_one_symbol
  1742. (info, abfd, shortname, BSF_INDIRECT,
  1743. bfd_ind_section_ptr,
  1744. 0, name, false, collect, &bh)))
  1745. return false;
  1746. hi = (struct elf_link_hash_entry *) bh;
  1747. }
  1748. }
  1749. else
  1750. {
  1751. /* In this case the symbol named SHORTNAME is overriding the
  1752. indirect symbol we want to add. We were planning on making
  1753. SHORTNAME an indirect symbol referring to NAME. SHORTNAME
  1754. is the name without a version. NAME is the fully versioned
  1755. name, and it is the default version.
  1756. Overriding means that we already saw a definition for the
  1757. symbol SHORTNAME in a regular object, and it is overriding
  1758. the symbol defined in the dynamic object.
  1759. When this happens, we actually want to change NAME, the
  1760. symbol we just added, to refer to SHORTNAME. This will cause
  1761. references to NAME in the shared object to become references
  1762. to SHORTNAME in the regular object. This is what we expect
  1763. when we override a function in a shared object: that the
  1764. references in the shared object will be mapped to the
  1765. definition in the regular object. */
  1766. while (hi->root.type == bfd_link_hash_indirect
  1767. || hi->root.type == bfd_link_hash_warning)
  1768. hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
  1769. h->root.type = bfd_link_hash_indirect;
  1770. h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
  1771. if (h->def_dynamic)
  1772. {
  1773. h->def_dynamic = 0;
  1774. hi->ref_dynamic = 1;
  1775. if (hi->ref_regular
  1776. || hi->def_regular)
  1777. {
  1778. if (! bfd_elf_link_record_dynamic_symbol (info, hi))
  1779. return false;
  1780. }
  1781. }
  1782. /* Now set HI to H, so that the following code will set the
  1783. other fields correctly. */
  1784. hi = h;
  1785. }
  1786. /* Check if HI is a warning symbol. */
  1787. if (hi->root.type == bfd_link_hash_warning)
  1788. hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
  1789. /* If there is a duplicate definition somewhere, then HI may not
  1790. point to an indirect symbol. We will have reported an error to
  1791. the user in that case. */
  1792. if (hi->root.type == bfd_link_hash_indirect)
  1793. {
  1794. struct elf_link_hash_entry *ht;
  1795. ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
  1796. (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi);
  1797. /* If we first saw a reference to SHORTNAME with non-default
  1798. visibility, merge that visibility to the @@VER symbol. */
  1799. elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic);
  1800. /* A reference to the SHORTNAME symbol from a dynamic library
  1801. will be satisfied by the versioned symbol at runtime. In
  1802. effect, we have a reference to the versioned symbol. */
  1803. ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
  1804. hi->dynamic_def |= ht->dynamic_def;
  1805. /* See if the new flags lead us to realize that the symbol must
  1806. be dynamic. */
  1807. if (! *dynsym)
  1808. {
  1809. if (! dynamic)
  1810. {
  1811. if (! bfd_link_executable (info)
  1812. || hi->def_dynamic
  1813. || hi->ref_dynamic)
  1814. *dynsym = true;
  1815. }
  1816. else
  1817. {
  1818. if (hi->ref_regular)
  1819. *dynsym = true;
  1820. }
  1821. }
  1822. }
  1823. /* We also need to define an indirection from the nondefault version
  1824. of the symbol. */
  1825. nondefault:
  1826. len = strlen (name);
  1827. shortname = (char *) bfd_hash_allocate (&info->hash->table, len);
  1828. if (shortname == NULL)
  1829. return false;
  1830. memcpy (shortname, name, shortlen);
  1831. memcpy (shortname + shortlen, p + 1, len - shortlen);
  1832. /* Once again, merge with any existing symbol. */
  1833. type_change_ok = false;
  1834. size_change_ok = false;
  1835. tmp_sec = sec;
  1836. if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value,
  1837. &hi, poldbfd, NULL, NULL, &skip, &override,
  1838. &type_change_ok, &size_change_ok, &matched))
  1839. return false;
  1840. if (skip)
  1841. {
  1842. if (!dynamic
  1843. && h->root.type == bfd_link_hash_defweak
  1844. && hi->root.type == bfd_link_hash_defined)
  1845. {
  1846. /* We are handling a weak sym@@ver and attempting to define
  1847. a weak sym@ver, but _bfd_elf_merge_symbol said to skip the
  1848. new weak sym@ver because there is already a strong sym@ver.
  1849. However, sym@ver and sym@@ver are really the same symbol.
  1850. The existing strong sym@ver ought to override sym@@ver. */
  1851. h->root.type = bfd_link_hash_defined;
  1852. h->root.u.def.section = hi->root.u.def.section;
  1853. h->root.u.def.value = hi->root.u.def.value;
  1854. hi->root.type = bfd_link_hash_indirect;
  1855. hi->root.u.i.link = &h->root;
  1856. }
  1857. else
  1858. return true;
  1859. }
  1860. else if (override)
  1861. {
  1862. /* Here SHORTNAME is a versioned name, so we don't expect to see
  1863. the type of override we do in the case above unless it is
  1864. overridden by a versioned definition. */
  1865. if (hi->root.type != bfd_link_hash_defined
  1866. && hi->root.type != bfd_link_hash_defweak)
  1867. _bfd_error_handler
  1868. /* xgettext:c-format */
  1869. (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"),
  1870. abfd, shortname);
  1871. return true;
  1872. }
  1873. else
  1874. {
  1875. bh = &hi->root;
  1876. if (! (_bfd_generic_link_add_one_symbol
  1877. (info, abfd, shortname, BSF_INDIRECT,
  1878. bfd_ind_section_ptr, 0, name, false, collect, &bh)))
  1879. return false;
  1880. hi = (struct elf_link_hash_entry *) bh;
  1881. }
  1882. /* If there is a duplicate definition somewhere, then HI may not
  1883. point to an indirect symbol. We will have reported an error
  1884. to the user in that case. */
  1885. if (hi->root.type == bfd_link_hash_indirect)
  1886. {
  1887. (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
  1888. h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak;
  1889. hi->dynamic_def |= h->dynamic_def;
  1890. /* If we first saw a reference to @VER symbol with
  1891. non-default visibility, merge that visibility to the
  1892. @@VER symbol. */
  1893. elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic);
  1894. /* See if the new flags lead us to realize that the symbol
  1895. must be dynamic. */
  1896. if (! *dynsym)
  1897. {
  1898. if (! dynamic)
  1899. {
  1900. if (! bfd_link_executable (info)
  1901. || hi->ref_dynamic)
  1902. *dynsym = true;
  1903. }
  1904. else
  1905. {
  1906. if (hi->ref_regular)
  1907. *dynsym = true;
  1908. }
  1909. }
  1910. }
  1911. return true;
  1912. }
  1913. /* This routine is used to export all defined symbols into the dynamic
  1914. symbol table. It is called via elf_link_hash_traverse. */
  1915. static bool
  1916. _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
  1917. {
  1918. struct elf_info_failed *eif = (struct elf_info_failed *) data;
  1919. /* Ignore indirect symbols. These are added by the versioning code. */
  1920. if (h->root.type == bfd_link_hash_indirect)
  1921. return true;
  1922. /* Ignore this if we won't export it. */
  1923. if (!eif->info->export_dynamic && !h->dynamic)
  1924. return true;
  1925. if (h->dynindx == -1
  1926. && (h->def_regular || h->ref_regular)
  1927. && ! bfd_hide_sym_by_version (eif->info->version_info,
  1928. h->root.root.string))
  1929. {
  1930. if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
  1931. {
  1932. eif->failed = true;
  1933. return false;
  1934. }
  1935. }
  1936. return true;
  1937. }
  1938. /* Return true if GLIBC_ABI_DT_RELR is added to the list of version
  1939. dependencies successfully. GLIBC_ABI_DT_RELR will be put into the
  1940. .gnu.version_r section. */
  1941. static bool
  1942. elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo)
  1943. {
  1944. bfd *glibc_bfd = NULL;
  1945. Elf_Internal_Verneed *t;
  1946. Elf_Internal_Vernaux *a;
  1947. size_t amt;
  1948. const char *relr = "GLIBC_ABI_DT_RELR";
  1949. /* See if we already know about GLIBC_PRIVATE_DT_RELR. */
  1950. for (t = elf_tdata (rinfo->info->output_bfd)->verref;
  1951. t != NULL;
  1952. t = t->vn_nextref)
  1953. {
  1954. const char *soname = bfd_elf_get_dt_soname (t->vn_bfd);
  1955. /* Skip the shared library if it isn't libc.so. */
  1956. if (!soname || !startswith (soname, "libc.so."))
  1957. continue;
  1958. for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
  1959. {
  1960. /* Return if GLIBC_PRIVATE_DT_RELR dependency has been
  1961. added. */
  1962. if (a->vna_nodename == relr
  1963. || strcmp (a->vna_nodename, relr) == 0)
  1964. return true;
  1965. /* Check if libc.so provides GLIBC_2.XX version. */
  1966. if (!glibc_bfd && startswith (a->vna_nodename, "GLIBC_2."))
  1967. glibc_bfd = t->vn_bfd;
  1968. }
  1969. break;
  1970. }
  1971. /* Skip if it isn't linked against glibc. */
  1972. if (glibc_bfd == NULL)
  1973. return true;
  1974. /* This is a new version. Add it to tree we are building. */
  1975. if (t == NULL)
  1976. {
  1977. amt = sizeof *t;
  1978. t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd,
  1979. amt);
  1980. if (t == NULL)
  1981. {
  1982. rinfo->failed = true;
  1983. return false;
  1984. }
  1985. t->vn_bfd = glibc_bfd;
  1986. t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
  1987. elf_tdata (rinfo->info->output_bfd)->verref = t;
  1988. }
  1989. amt = sizeof *a;
  1990. a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
  1991. if (a == NULL)
  1992. {
  1993. rinfo->failed = true;
  1994. return false;
  1995. }
  1996. a->vna_nodename = relr;
  1997. a->vna_flags = 0;
  1998. a->vna_nextptr = t->vn_auxptr;
  1999. a->vna_other = rinfo->vers + 1;
  2000. ++rinfo->vers;
  2001. t->vn_auxptr = a;
  2002. return true;
  2003. }
  2004. /* Look through the symbols which are defined in other shared
  2005. libraries and referenced here. Update the list of version
  2006. dependencies. This will be put into the .gnu.version_r section.
  2007. This function is called via elf_link_hash_traverse. */
  2008. static bool
  2009. _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h,
  2010. void *data)
  2011. {
  2012. struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data;
  2013. Elf_Internal_Verneed *t;
  2014. Elf_Internal_Vernaux *a;
  2015. size_t amt;
  2016. /* We only care about symbols defined in shared objects with version
  2017. information. */
  2018. if (!h->def_dynamic
  2019. || h->def_regular
  2020. || h->dynindx == -1
  2021. || h->verinfo.verdef == NULL
  2022. || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
  2023. & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
  2024. return true;
  2025. /* See if we already know about this version. */
  2026. for (t = elf_tdata (rinfo->info->output_bfd)->verref;
  2027. t != NULL;
  2028. t = t->vn_nextref)
  2029. {
  2030. if (t->vn_bfd != h->verinfo.verdef->vd_bfd)
  2031. continue;
  2032. for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
  2033. if (a->vna_nodename == h->verinfo.verdef->vd_nodename)
  2034. return true;
  2035. break;
  2036. }
  2037. /* This is a new version. Add it to tree we are building. */
  2038. if (t == NULL)
  2039. {
  2040. amt = sizeof *t;
  2041. t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt);
  2042. if (t == NULL)
  2043. {
  2044. rinfo->failed = true;
  2045. return false;
  2046. }
  2047. t->vn_bfd = h->verinfo.verdef->vd_bfd;
  2048. t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref;
  2049. elf_tdata (rinfo->info->output_bfd)->verref = t;
  2050. }
  2051. amt = sizeof *a;
  2052. a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt);
  2053. if (a == NULL)
  2054. {
  2055. rinfo->failed = true;
  2056. return false;
  2057. }
  2058. /* Note that we are copying a string pointer here, and testing it
  2059. above. If bfd_elf_string_from_elf_section is ever changed to
  2060. discard the string data when low in memory, this will have to be
  2061. fixed. */
  2062. a->vna_nodename = h->verinfo.verdef->vd_nodename;
  2063. a->vna_flags = h->verinfo.verdef->vd_flags;
  2064. a->vna_nextptr = t->vn_auxptr;
  2065. h->verinfo.verdef->vd_exp_refno = rinfo->vers;
  2066. ++rinfo->vers;
  2067. a->vna_other = h->verinfo.verdef->vd_exp_refno + 1;
  2068. t->vn_auxptr = a;
  2069. return true;
  2070. }
  2071. /* Return TRUE and set *HIDE to TRUE if the versioned symbol is
  2072. hidden. Set *T_P to NULL if there is no match. */
  2073. static bool
  2074. _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info,
  2075. struct elf_link_hash_entry *h,
  2076. const char *version_p,
  2077. struct bfd_elf_version_tree **t_p,
  2078. bool *hide)
  2079. {
  2080. struct bfd_elf_version_tree *t;
  2081. /* Look for the version. If we find it, it is no longer weak. */
  2082. for (t = info->version_info; t != NULL; t = t->next)
  2083. {
  2084. if (strcmp (t->name, version_p) == 0)
  2085. {
  2086. size_t len;
  2087. char *alc;
  2088. struct bfd_elf_version_expr *d;
  2089. len = version_p - h->root.root.string;
  2090. alc = (char *) bfd_malloc (len);
  2091. if (alc == NULL)
  2092. return false;
  2093. memcpy (alc, h->root.root.string, len - 1);
  2094. alc[len - 1] = '\0';
  2095. if (alc[len - 2] == ELF_VER_CHR)
  2096. alc[len - 2] = '\0';
  2097. h->verinfo.vertree = t;
  2098. t->used = true;
  2099. d = NULL;
  2100. if (t->globals.list != NULL)
  2101. d = (*t->match) (&t->globals, NULL, alc);
  2102. /* See if there is anything to force this symbol to
  2103. local scope. */
  2104. if (d == NULL && t->locals.list != NULL)
  2105. {
  2106. d = (*t->match) (&t->locals, NULL, alc);
  2107. if (d != NULL
  2108. && h->dynindx != -1
  2109. && ! info->export_dynamic)
  2110. *hide = true;
  2111. }
  2112. free (alc);
  2113. break;
  2114. }
  2115. }
  2116. *t_p = t;
  2117. return true;
  2118. }
  2119. /* Return TRUE if the symbol H is hidden by version script. */
  2120. bool
  2121. _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info,
  2122. struct elf_link_hash_entry *h)
  2123. {
  2124. const char *p;
  2125. bool hide = false;
  2126. const struct elf_backend_data *bed
  2127. = get_elf_backend_data (info->output_bfd);
  2128. /* Version script only hides symbols defined in regular objects. */
  2129. if (!h->def_regular && !ELF_COMMON_DEF_P (h))
  2130. return true;
  2131. p = strchr (h->root.root.string, ELF_VER_CHR);
  2132. if (p != NULL && h->verinfo.vertree == NULL)
  2133. {
  2134. struct bfd_elf_version_tree *t;
  2135. ++p;
  2136. if (*p == ELF_VER_CHR)
  2137. ++p;
  2138. if (*p != '\0'
  2139. && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)
  2140. && hide)
  2141. {
  2142. if (hide)
  2143. (*bed->elf_backend_hide_symbol) (info, h, true);
  2144. return true;
  2145. }
  2146. }
  2147. /* If we don't have a version for this symbol, see if we can find
  2148. something. */
  2149. if (h->verinfo.vertree == NULL && info->version_info != NULL)
  2150. {
  2151. h->verinfo.vertree
  2152. = bfd_find_version_for_sym (info->version_info,
  2153. h->root.root.string, &hide);
  2154. if (h->verinfo.vertree != NULL && hide)
  2155. {
  2156. (*bed->elf_backend_hide_symbol) (info, h, true);
  2157. return true;
  2158. }
  2159. }
  2160. return false;
  2161. }
  2162. /* Figure out appropriate versions for all the symbols. We may not
  2163. have the version number script until we have read all of the input
  2164. files, so until that point we don't know which symbols should be
  2165. local. This function is called via elf_link_hash_traverse. */
  2166. static bool
  2167. _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data)
  2168. {
  2169. struct elf_info_failed *sinfo;
  2170. struct bfd_link_info *info;
  2171. const struct elf_backend_data *bed;
  2172. struct elf_info_failed eif;
  2173. char *p;
  2174. bool hide;
  2175. sinfo = (struct elf_info_failed *) data;
  2176. info = sinfo->info;
  2177. /* Fix the symbol flags. */
  2178. eif.failed = false;
  2179. eif.info = info;
  2180. if (! _bfd_elf_fix_symbol_flags (h, &eif))
  2181. {
  2182. if (eif.failed)
  2183. sinfo->failed = true;
  2184. return false;
  2185. }
  2186. bed = get_elf_backend_data (info->output_bfd);
  2187. /* We only need version numbers for symbols defined in regular
  2188. objects. */
  2189. if (!h->def_regular && !ELF_COMMON_DEF_P (h))
  2190. {
  2191. /* Hide symbols defined in discarded input sections. */
  2192. if ((h->root.type == bfd_link_hash_defined
  2193. || h->root.type == bfd_link_hash_defweak)
  2194. && discarded_section (h->root.u.def.section))
  2195. (*bed->elf_backend_hide_symbol) (info, h, true);
  2196. return true;
  2197. }
  2198. hide = false;
  2199. p = strchr (h->root.root.string, ELF_VER_CHR);
  2200. if (p != NULL && h->verinfo.vertree == NULL)
  2201. {
  2202. struct bfd_elf_version_tree *t;
  2203. ++p;
  2204. if (*p == ELF_VER_CHR)
  2205. ++p;
  2206. /* If there is no version string, we can just return out. */
  2207. if (*p == '\0')
  2208. return true;
  2209. if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide))
  2210. {
  2211. sinfo->failed = true;
  2212. return false;
  2213. }
  2214. if (hide)
  2215. (*bed->elf_backend_hide_symbol) (info, h, true);
  2216. /* If we are building an application, we need to create a
  2217. version node for this version. */
  2218. if (t == NULL && bfd_link_executable (info))
  2219. {
  2220. struct bfd_elf_version_tree **pp;
  2221. int version_index;
  2222. /* If we aren't going to export this symbol, we don't need
  2223. to worry about it. */
  2224. if (h->dynindx == -1)
  2225. return true;
  2226. t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd,
  2227. sizeof *t);
  2228. if (t == NULL)
  2229. {
  2230. sinfo->failed = true;
  2231. return false;
  2232. }
  2233. t->name = p;
  2234. t->name_indx = (unsigned int) -1;
  2235. t->used = true;
  2236. version_index = 1;
  2237. /* Don't count anonymous version tag. */
  2238. if (sinfo->info->version_info != NULL
  2239. && sinfo->info->version_info->vernum == 0)
  2240. version_index = 0;
  2241. for (pp = &sinfo->info->version_info;
  2242. *pp != NULL;
  2243. pp = &(*pp)->next)
  2244. ++version_index;
  2245. t->vernum = version_index;
  2246. *pp = t;
  2247. h->verinfo.vertree = t;
  2248. }
  2249. else if (t == NULL)
  2250. {
  2251. /* We could not find the version for a symbol when
  2252. generating a shared archive. Return an error. */
  2253. _bfd_error_handler
  2254. /* xgettext:c-format */
  2255. (_("%pB: version node not found for symbol %s"),
  2256. info->output_bfd, h->root.root.string);
  2257. bfd_set_error (bfd_error_bad_value);
  2258. sinfo->failed = true;
  2259. return false;
  2260. }
  2261. }
  2262. /* If we don't have a version for this symbol, see if we can find
  2263. something. */
  2264. if (!hide
  2265. && h->verinfo.vertree == NULL
  2266. && sinfo->info->version_info != NULL)
  2267. {
  2268. h->verinfo.vertree
  2269. = bfd_find_version_for_sym (sinfo->info->version_info,
  2270. h->root.root.string, &hide);
  2271. if (h->verinfo.vertree != NULL && hide)
  2272. (*bed->elf_backend_hide_symbol) (info, h, true);
  2273. }
  2274. return true;
  2275. }
  2276. /* Read and swap the relocs from the section indicated by SHDR. This
  2277. may be either a REL or a RELA section. The relocations are
  2278. translated into RELA relocations and stored in INTERNAL_RELOCS,
  2279. which should have already been allocated to contain enough space.
  2280. The EXTERNAL_RELOCS are a buffer where the external form of the
  2281. relocations should be stored.
  2282. Returns FALSE if something goes wrong. */
  2283. static bool
  2284. elf_link_read_relocs_from_section (bfd *abfd,
  2285. asection *sec,
  2286. Elf_Internal_Shdr *shdr,
  2287. void *external_relocs,
  2288. Elf_Internal_Rela *internal_relocs)
  2289. {
  2290. const struct elf_backend_data *bed;
  2291. void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
  2292. const bfd_byte *erela;
  2293. const bfd_byte *erelaend;
  2294. Elf_Internal_Rela *irela;
  2295. Elf_Internal_Shdr *symtab_hdr;
  2296. size_t nsyms;
  2297. /* Position ourselves at the start of the section. */
  2298. if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
  2299. return false;
  2300. /* Read the relocations. */
  2301. if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size)
  2302. return false;
  2303. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2304. nsyms = NUM_SHDR_ENTRIES (symtab_hdr);
  2305. bed = get_elf_backend_data (abfd);
  2306. /* Convert the external relocations to the internal format. */
  2307. if (shdr->sh_entsize == bed->s->sizeof_rel)
  2308. swap_in = bed->s->swap_reloc_in;
  2309. else if (shdr->sh_entsize == bed->s->sizeof_rela)
  2310. swap_in = bed->s->swap_reloca_in;
  2311. else
  2312. {
  2313. bfd_set_error (bfd_error_wrong_format);
  2314. return false;
  2315. }
  2316. erela = (const bfd_byte *) external_relocs;
  2317. /* Setting erelaend like this and comparing with <= handles case of
  2318. a fuzzed object with sh_size not a multiple of sh_entsize. */
  2319. erelaend = erela + shdr->sh_size - shdr->sh_entsize;
  2320. irela = internal_relocs;
  2321. while (erela <= erelaend)
  2322. {
  2323. bfd_vma r_symndx;
  2324. (*swap_in) (abfd, erela, irela);
  2325. r_symndx = ELF32_R_SYM (irela->r_info);
  2326. if (bed->s->arch_size == 64)
  2327. r_symndx >>= 24;
  2328. if (nsyms > 0)
  2329. {
  2330. if ((size_t) r_symndx >= nsyms)
  2331. {
  2332. _bfd_error_handler
  2333. /* xgettext:c-format */
  2334. (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)"
  2335. " for offset %#" PRIx64 " in section `%pA'"),
  2336. abfd, (uint64_t) r_symndx, (unsigned long) nsyms,
  2337. (uint64_t) irela->r_offset, sec);
  2338. bfd_set_error (bfd_error_bad_value);
  2339. return false;
  2340. }
  2341. }
  2342. else if (r_symndx != STN_UNDEF)
  2343. {
  2344. _bfd_error_handler
  2345. /* xgettext:c-format */
  2346. (_("%pB: non-zero symbol index (%#" PRIx64 ")"
  2347. " for offset %#" PRIx64 " in section `%pA'"
  2348. " when the object file has no symbol table"),
  2349. abfd, (uint64_t) r_symndx,
  2350. (uint64_t) irela->r_offset, sec);
  2351. bfd_set_error (bfd_error_bad_value);
  2352. return false;
  2353. }
  2354. irela += bed->s->int_rels_per_ext_rel;
  2355. erela += shdr->sh_entsize;
  2356. }
  2357. return true;
  2358. }
  2359. /* Read and swap the relocs for a section O. They may have been
  2360. cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
  2361. not NULL, they are used as buffers to read into. They are known to
  2362. be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
  2363. the return value is allocated using either malloc or bfd_alloc,
  2364. according to the KEEP_MEMORY argument. If O has two relocation
  2365. sections (both REL and RELA relocations), then the REL_HDR
  2366. relocations will appear first in INTERNAL_RELOCS, followed by the
  2367. RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
  2368. update cache_size. */
  2369. Elf_Internal_Rela *
  2370. _bfd_elf_link_info_read_relocs (bfd *abfd,
  2371. struct bfd_link_info *info,
  2372. asection *o,
  2373. void *external_relocs,
  2374. Elf_Internal_Rela *internal_relocs,
  2375. bool keep_memory)
  2376. {
  2377. void *alloc1 = NULL;
  2378. Elf_Internal_Rela *alloc2 = NULL;
  2379. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  2380. struct bfd_elf_section_data *esdo = elf_section_data (o);
  2381. Elf_Internal_Rela *internal_rela_relocs;
  2382. if (esdo->relocs != NULL)
  2383. return esdo->relocs;
  2384. if (o->reloc_count == 0)
  2385. return NULL;
  2386. if (internal_relocs == NULL)
  2387. {
  2388. bfd_size_type size;
  2389. size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
  2390. if (keep_memory)
  2391. {
  2392. internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
  2393. if (info)
  2394. info->cache_size += size;
  2395. }
  2396. else
  2397. internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
  2398. if (internal_relocs == NULL)
  2399. goto error_return;
  2400. }
  2401. if (external_relocs == NULL)
  2402. {
  2403. bfd_size_type size = 0;
  2404. if (esdo->rel.hdr)
  2405. size += esdo->rel.hdr->sh_size;
  2406. if (esdo->rela.hdr)
  2407. size += esdo->rela.hdr->sh_size;
  2408. alloc1 = bfd_malloc (size);
  2409. if (alloc1 == NULL)
  2410. goto error_return;
  2411. external_relocs = alloc1;
  2412. }
  2413. internal_rela_relocs = internal_relocs;
  2414. if (esdo->rel.hdr)
  2415. {
  2416. if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr,
  2417. external_relocs,
  2418. internal_relocs))
  2419. goto error_return;
  2420. external_relocs = (((bfd_byte *) external_relocs)
  2421. + esdo->rel.hdr->sh_size);
  2422. internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr)
  2423. * bed->s->int_rels_per_ext_rel);
  2424. }
  2425. if (esdo->rela.hdr
  2426. && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr,
  2427. external_relocs,
  2428. internal_rela_relocs)))
  2429. goto error_return;
  2430. /* Cache the results for next time, if we can. */
  2431. if (keep_memory)
  2432. esdo->relocs = internal_relocs;
  2433. free (alloc1);
  2434. /* Don't free alloc2, since if it was allocated we are passing it
  2435. back (under the name of internal_relocs). */
  2436. return internal_relocs;
  2437. error_return:
  2438. free (alloc1);
  2439. if (alloc2 != NULL)
  2440. {
  2441. if (keep_memory)
  2442. bfd_release (abfd, alloc2);
  2443. else
  2444. free (alloc2);
  2445. }
  2446. return NULL;
  2447. }
  2448. /* This is similar to _bfd_elf_link_info_read_relocs, except for that
  2449. NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
  2450. struct bfd_link_info. */
  2451. Elf_Internal_Rela *
  2452. _bfd_elf_link_read_relocs (bfd *abfd,
  2453. asection *o,
  2454. void *external_relocs,
  2455. Elf_Internal_Rela *internal_relocs,
  2456. bool keep_memory)
  2457. {
  2458. return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
  2459. internal_relocs, keep_memory);
  2460. }
  2461. /* Compute the size of, and allocate space for, REL_HDR which is the
  2462. section header for a section containing relocations for O. */
  2463. static bool
  2464. _bfd_elf_link_size_reloc_section (bfd *abfd,
  2465. struct bfd_elf_section_reloc_data *reldata)
  2466. {
  2467. Elf_Internal_Shdr *rel_hdr = reldata->hdr;
  2468. /* That allows us to calculate the size of the section. */
  2469. rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count;
  2470. /* The contents field must last into write_object_contents, so we
  2471. allocate it with bfd_alloc rather than malloc. Also since we
  2472. cannot be sure that the contents will actually be filled in,
  2473. we zero the allocated space. */
  2474. rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size);
  2475. if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0)
  2476. return false;
  2477. if (reldata->hashes == NULL && reldata->count)
  2478. {
  2479. struct elf_link_hash_entry **p;
  2480. p = ((struct elf_link_hash_entry **)
  2481. bfd_zmalloc (reldata->count * sizeof (*p)));
  2482. if (p == NULL)
  2483. return false;
  2484. reldata->hashes = p;
  2485. }
  2486. return true;
  2487. }
  2488. /* Copy the relocations indicated by the INTERNAL_RELOCS (which
  2489. originated from the section given by INPUT_REL_HDR) to the
  2490. OUTPUT_BFD. */
  2491. bool
  2492. _bfd_elf_link_output_relocs (bfd *output_bfd,
  2493. asection *input_section,
  2494. Elf_Internal_Shdr *input_rel_hdr,
  2495. Elf_Internal_Rela *internal_relocs,
  2496. struct elf_link_hash_entry **rel_hash
  2497. ATTRIBUTE_UNUSED)
  2498. {
  2499. Elf_Internal_Rela *irela;
  2500. Elf_Internal_Rela *irelaend;
  2501. bfd_byte *erel;
  2502. struct bfd_elf_section_reloc_data *output_reldata;
  2503. asection *output_section;
  2504. const struct elf_backend_data *bed;
  2505. void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
  2506. struct bfd_elf_section_data *esdo;
  2507. output_section = input_section->output_section;
  2508. bed = get_elf_backend_data (output_bfd);
  2509. esdo = elf_section_data (output_section);
  2510. if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize)
  2511. {
  2512. output_reldata = &esdo->rel;
  2513. swap_out = bed->s->swap_reloc_out;
  2514. }
  2515. else if (esdo->rela.hdr
  2516. && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize)
  2517. {
  2518. output_reldata = &esdo->rela;
  2519. swap_out = bed->s->swap_reloca_out;
  2520. }
  2521. else
  2522. {
  2523. _bfd_error_handler
  2524. /* xgettext:c-format */
  2525. (_("%pB: relocation size mismatch in %pB section %pA"),
  2526. output_bfd, input_section->owner, input_section);
  2527. bfd_set_error (bfd_error_wrong_format);
  2528. return false;
  2529. }
  2530. erel = output_reldata->hdr->contents;
  2531. erel += output_reldata->count * input_rel_hdr->sh_entsize;
  2532. irela = internal_relocs;
  2533. irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
  2534. * bed->s->int_rels_per_ext_rel);
  2535. while (irela < irelaend)
  2536. {
  2537. (*swap_out) (output_bfd, irela, erel);
  2538. irela += bed->s->int_rels_per_ext_rel;
  2539. erel += input_rel_hdr->sh_entsize;
  2540. }
  2541. /* Bump the counter, so that we know where to add the next set of
  2542. relocations. */
  2543. output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr);
  2544. return true;
  2545. }
  2546. /* Make weak undefined symbols in PIE dynamic. */
  2547. bool
  2548. _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info,
  2549. struct elf_link_hash_entry *h)
  2550. {
  2551. if (bfd_link_pie (info)
  2552. && h->dynindx == -1
  2553. && h->root.type == bfd_link_hash_undefweak)
  2554. return bfd_elf_link_record_dynamic_symbol (info, h);
  2555. return true;
  2556. }
  2557. /* Fix up the flags for a symbol. This handles various cases which
  2558. can only be fixed after all the input files are seen. This is
  2559. currently called by both adjust_dynamic_symbol and
  2560. assign_sym_version, which is unnecessary but perhaps more robust in
  2561. the face of future changes. */
  2562. static bool
  2563. _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h,
  2564. struct elf_info_failed *eif)
  2565. {
  2566. const struct elf_backend_data *bed;
  2567. /* If this symbol was mentioned in a non-ELF file, try to set
  2568. DEF_REGULAR and REF_REGULAR correctly. This is the only way to
  2569. permit a non-ELF file to correctly refer to a symbol defined in
  2570. an ELF dynamic object. */
  2571. if (h->non_elf)
  2572. {
  2573. while (h->root.type == bfd_link_hash_indirect)
  2574. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2575. if (h->root.type != bfd_link_hash_defined
  2576. && h->root.type != bfd_link_hash_defweak)
  2577. {
  2578. h->ref_regular = 1;
  2579. h->ref_regular_nonweak = 1;
  2580. }
  2581. else
  2582. {
  2583. if (h->root.u.def.section->owner != NULL
  2584. && (bfd_get_flavour (h->root.u.def.section->owner)
  2585. == bfd_target_elf_flavour))
  2586. {
  2587. h->ref_regular = 1;
  2588. h->ref_regular_nonweak = 1;
  2589. }
  2590. else
  2591. h->def_regular = 1;
  2592. }
  2593. if (h->dynindx == -1
  2594. && (h->def_dynamic
  2595. || h->ref_dynamic))
  2596. {
  2597. if (! bfd_elf_link_record_dynamic_symbol (eif->info, h))
  2598. {
  2599. eif->failed = true;
  2600. return false;
  2601. }
  2602. }
  2603. }
  2604. else
  2605. {
  2606. /* Unfortunately, NON_ELF is only correct if the symbol
  2607. was first seen in a non-ELF file. Fortunately, if the symbol
  2608. was first seen in an ELF file, we're probably OK unless the
  2609. symbol was defined in a non-ELF file. Catch that case here.
  2610. FIXME: We're still in trouble if the symbol was first seen in
  2611. a dynamic object, and then later in a non-ELF regular object. */
  2612. if ((h->root.type == bfd_link_hash_defined
  2613. || h->root.type == bfd_link_hash_defweak)
  2614. && !h->def_regular
  2615. && (h->root.u.def.section->owner != NULL
  2616. ? (bfd_get_flavour (h->root.u.def.section->owner)
  2617. != bfd_target_elf_flavour)
  2618. : (bfd_is_abs_section (h->root.u.def.section)
  2619. && !h->def_dynamic)))
  2620. h->def_regular = 1;
  2621. }
  2622. /* Backend specific symbol fixup. */
  2623. bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj);
  2624. if (bed->elf_backend_fixup_symbol
  2625. && !(*bed->elf_backend_fixup_symbol) (eif->info, h))
  2626. return false;
  2627. /* If this is a final link, and the symbol was defined as a common
  2628. symbol in a regular object file, and there was no definition in
  2629. any dynamic object, then the linker will have allocated space for
  2630. the symbol in a common section but the DEF_REGULAR
  2631. flag will not have been set. */
  2632. if (h->root.type == bfd_link_hash_defined
  2633. && !h->def_regular
  2634. && h->ref_regular
  2635. && !h->def_dynamic
  2636. && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0)
  2637. h->def_regular = 1;
  2638. /* Symbols defined in discarded sections shouldn't be dynamic. */
  2639. if (h->root.type == bfd_link_hash_undefined && h->indx == -3)
  2640. (*bed->elf_backend_hide_symbol) (eif->info, h, true);
  2641. /* If a weak undefined symbol has non-default visibility, we also
  2642. hide it from the dynamic linker. */
  2643. else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  2644. && h->root.type == bfd_link_hash_undefweak)
  2645. (*bed->elf_backend_hide_symbol) (eif->info, h, true);
  2646. /* A hidden versioned symbol in executable should be forced local if
  2647. it is is locally defined, not referenced by shared library and not
  2648. exported. */
  2649. else if (bfd_link_executable (eif->info)
  2650. && h->versioned == versioned_hidden
  2651. && !eif->info->export_dynamic
  2652. && !h->dynamic
  2653. && !h->ref_dynamic
  2654. && h->def_regular)
  2655. (*bed->elf_backend_hide_symbol) (eif->info, h, true);
  2656. /* If -Bsymbolic was used (which means to bind references to global
  2657. symbols to the definition within the shared object), and this
  2658. symbol was defined in a regular object, then it actually doesn't
  2659. need a PLT entry. Likewise, if the symbol has non-default
  2660. visibility. If the symbol has hidden or internal visibility, we
  2661. will force it local. */
  2662. else if (h->needs_plt
  2663. && bfd_link_pic (eif->info)
  2664. && is_elf_hash_table (eif->info->hash)
  2665. && (SYMBOLIC_BIND (eif->info, h)
  2666. || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  2667. && h->def_regular)
  2668. {
  2669. bool force_local;
  2670. force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
  2671. || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN);
  2672. (*bed->elf_backend_hide_symbol) (eif->info, h, force_local);
  2673. }
  2674. /* If this is a weak defined symbol in a dynamic object, and we know
  2675. the real definition in the dynamic object, copy interesting flags
  2676. over to the real definition. */
  2677. if (h->is_weakalias)
  2678. {
  2679. struct elf_link_hash_entry *def = weakdef (h);
  2680. /* If the real definition is defined by a regular object file,
  2681. don't do anything special. See the longer description in
  2682. _bfd_elf_adjust_dynamic_symbol, below. If the def is not
  2683. bfd_link_hash_defined as it was when put on the alias list
  2684. then it must have originally been a versioned symbol (for
  2685. which a non-versioned indirect symbol is created) and later
  2686. a definition for the non-versioned symbol is found. In that
  2687. case the indirection is flipped with the versioned symbol
  2688. becoming an indirect pointing at the non-versioned symbol.
  2689. Thus, not an alias any more. */
  2690. if (def->def_regular
  2691. || def->root.type != bfd_link_hash_defined)
  2692. {
  2693. h = def;
  2694. while ((h = h->u.alias) != def)
  2695. h->is_weakalias = 0;
  2696. }
  2697. else
  2698. {
  2699. while (h->root.type == bfd_link_hash_indirect)
  2700. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2701. BFD_ASSERT (h->root.type == bfd_link_hash_defined
  2702. || h->root.type == bfd_link_hash_defweak);
  2703. BFD_ASSERT (def->def_dynamic);
  2704. (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h);
  2705. }
  2706. }
  2707. return true;
  2708. }
  2709. /* Make the backend pick a good value for a dynamic symbol. This is
  2710. called via elf_link_hash_traverse, and also calls itself
  2711. recursively. */
  2712. static bool
  2713. _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data)
  2714. {
  2715. struct elf_info_failed *eif = (struct elf_info_failed *) data;
  2716. struct elf_link_hash_table *htab;
  2717. const struct elf_backend_data *bed;
  2718. if (! is_elf_hash_table (eif->info->hash))
  2719. return false;
  2720. /* Ignore indirect symbols. These are added by the versioning code. */
  2721. if (h->root.type == bfd_link_hash_indirect)
  2722. return true;
  2723. /* Fix the symbol flags. */
  2724. if (! _bfd_elf_fix_symbol_flags (h, eif))
  2725. return false;
  2726. htab = elf_hash_table (eif->info);
  2727. bed = get_elf_backend_data (htab->dynobj);
  2728. if (h->root.type == bfd_link_hash_undefweak)
  2729. {
  2730. if (eif->info->dynamic_undefined_weak == 0)
  2731. (*bed->elf_backend_hide_symbol) (eif->info, h, true);
  2732. else if (eif->info->dynamic_undefined_weak > 0
  2733. && h->ref_regular
  2734. && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  2735. && !bfd_hide_sym_by_version (eif->info->version_info,
  2736. h->root.root.string))
  2737. {
  2738. if (!bfd_elf_link_record_dynamic_symbol (eif->info, h))
  2739. {
  2740. eif->failed = true;
  2741. return false;
  2742. }
  2743. }
  2744. }
  2745. /* If this symbol does not require a PLT entry, and it is not
  2746. defined by a dynamic object, or is not referenced by a regular
  2747. object, ignore it. We do have to handle a weak defined symbol,
  2748. even if no regular object refers to it, if we decided to add it
  2749. to the dynamic symbol table. FIXME: Do we normally need to worry
  2750. about symbols which are defined by one dynamic object and
  2751. referenced by another one? */
  2752. if (!h->needs_plt
  2753. && h->type != STT_GNU_IFUNC
  2754. && (h->def_regular
  2755. || !h->def_dynamic
  2756. || (!h->ref_regular
  2757. && (!h->is_weakalias || weakdef (h)->dynindx == -1))))
  2758. {
  2759. h->plt = elf_hash_table (eif->info)->init_plt_offset;
  2760. return true;
  2761. }
  2762. /* If we've already adjusted this symbol, don't do it again. This
  2763. can happen via a recursive call. */
  2764. if (h->dynamic_adjusted)
  2765. return true;
  2766. /* Don't look at this symbol again. Note that we must set this
  2767. after checking the above conditions, because we may look at a
  2768. symbol once, decide not to do anything, and then get called
  2769. recursively later after REF_REGULAR is set below. */
  2770. h->dynamic_adjusted = 1;
  2771. /* If this is a weak definition, and we know a real definition, and
  2772. the real symbol is not itself defined by a regular object file,
  2773. then get a good value for the real definition. We handle the
  2774. real symbol first, for the convenience of the backend routine.
  2775. Note that there is a confusing case here. If the real definition
  2776. is defined by a regular object file, we don't get the real symbol
  2777. from the dynamic object, but we do get the weak symbol. If the
  2778. processor backend uses a COPY reloc, then if some routine in the
  2779. dynamic object changes the real symbol, we will not see that
  2780. change in the corresponding weak symbol. This is the way other
  2781. ELF linkers work as well, and seems to be a result of the shared
  2782. library model.
  2783. I will clarify this issue. Most SVR4 shared libraries define the
  2784. variable _timezone and define timezone as a weak synonym. The
  2785. tzset call changes _timezone. If you write
  2786. extern int timezone;
  2787. int _timezone = 5;
  2788. int main () { tzset (); printf ("%d %d\n", timezone, _timezone); }
  2789. you might expect that, since timezone is a synonym for _timezone,
  2790. the same number will print both times. However, if the processor
  2791. backend uses a COPY reloc, then actually timezone will be copied
  2792. into your process image, and, since you define _timezone
  2793. yourself, _timezone will not. Thus timezone and _timezone will
  2794. wind up at different memory locations. The tzset call will set
  2795. _timezone, leaving timezone unchanged. */
  2796. if (h->is_weakalias)
  2797. {
  2798. struct elf_link_hash_entry *def = weakdef (h);
  2799. /* If we get to this point, there is an implicit reference to
  2800. the alias by a regular object file via the weak symbol H. */
  2801. def->ref_regular = 1;
  2802. /* Ensure that the backend adjust_dynamic_symbol function sees
  2803. the strong alias before H by recursively calling ourselves. */
  2804. if (!_bfd_elf_adjust_dynamic_symbol (def, eif))
  2805. return false;
  2806. }
  2807. /* If a symbol has no type and no size and does not require a PLT
  2808. entry, then we are probably about to do the wrong thing here: we
  2809. are probably going to create a COPY reloc for an empty object.
  2810. This case can arise when a shared object is built with assembly
  2811. code, and the assembly code fails to set the symbol type. */
  2812. if (h->size == 0
  2813. && h->type == STT_NOTYPE
  2814. && !h->needs_plt)
  2815. _bfd_error_handler
  2816. (_("warning: type and size of dynamic symbol `%s' are not defined"),
  2817. h->root.root.string);
  2818. if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h))
  2819. {
  2820. eif->failed = true;
  2821. return false;
  2822. }
  2823. return true;
  2824. }
  2825. /* Adjust the dynamic symbol, H, for copy in the dynamic bss section,
  2826. DYNBSS. */
  2827. bool
  2828. _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
  2829. struct elf_link_hash_entry *h,
  2830. asection *dynbss)
  2831. {
  2832. unsigned int power_of_two;
  2833. bfd_vma mask;
  2834. asection *sec = h->root.u.def.section;
  2835. /* The section alignment of the definition is the maximum alignment
  2836. requirement of symbols defined in the section. Since we don't
  2837. know the symbol alignment requirement, we start with the
  2838. maximum alignment and check low bits of the symbol address
  2839. for the minimum alignment. */
  2840. power_of_two = bfd_section_alignment (sec);
  2841. mask = ((bfd_vma) 1 << power_of_two) - 1;
  2842. while ((h->root.u.def.value & mask) != 0)
  2843. {
  2844. mask >>= 1;
  2845. --power_of_two;
  2846. }
  2847. if (power_of_two > bfd_section_alignment (dynbss))
  2848. {
  2849. /* Adjust the section alignment if needed. */
  2850. if (!bfd_set_section_alignment (dynbss, power_of_two))
  2851. return false;
  2852. }
  2853. /* We make sure that the symbol will be aligned properly. */
  2854. dynbss->size = BFD_ALIGN (dynbss->size, mask + 1);
  2855. /* Define the symbol as being at this point in DYNBSS. */
  2856. h->root.u.def.section = dynbss;
  2857. h->root.u.def.value = dynbss->size;
  2858. /* Increment the size of DYNBSS to make room for the symbol. */
  2859. dynbss->size += h->size;
  2860. /* No error if extern_protected_data is true. */
  2861. if (h->protected_def
  2862. && (!info->extern_protected_data
  2863. || (info->extern_protected_data < 0
  2864. && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
  2865. info->callbacks->einfo
  2866. (_("%P: copy reloc against protected `%pT' is dangerous\n"),
  2867. h->root.root.string);
  2868. return true;
  2869. }
  2870. /* Adjust all external symbols pointing into SEC_MERGE sections
  2871. to reflect the object merging within the sections. */
  2872. static bool
  2873. _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data)
  2874. {
  2875. asection *sec;
  2876. if ((h->root.type == bfd_link_hash_defined
  2877. || h->root.type == bfd_link_hash_defweak)
  2878. && ((sec = h->root.u.def.section)->flags & SEC_MERGE)
  2879. && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
  2880. {
  2881. bfd *output_bfd = (bfd *) data;
  2882. h->root.u.def.value =
  2883. _bfd_merged_section_offset (output_bfd,
  2884. &h->root.u.def.section,
  2885. elf_section_data (sec)->sec_info,
  2886. h->root.u.def.value);
  2887. }
  2888. return true;
  2889. }
  2890. /* Returns false if the symbol referred to by H should be considered
  2891. to resolve local to the current module, and true if it should be
  2892. considered to bind dynamically. */
  2893. bool
  2894. _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h,
  2895. struct bfd_link_info *info,
  2896. bool not_local_protected)
  2897. {
  2898. bool binding_stays_local_p;
  2899. const struct elf_backend_data *bed;
  2900. struct elf_link_hash_table *hash_table;
  2901. if (h == NULL)
  2902. return false;
  2903. while (h->root.type == bfd_link_hash_indirect
  2904. || h->root.type == bfd_link_hash_warning)
  2905. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2906. /* If it was forced local, then clearly it's not dynamic. */
  2907. if (h->dynindx == -1)
  2908. return false;
  2909. if (h->forced_local)
  2910. return false;
  2911. /* Identify the cases where name binding rules say that a
  2912. visible symbol resolves locally. */
  2913. binding_stays_local_p = (bfd_link_executable (info)
  2914. || SYMBOLIC_BIND (info, h));
  2915. switch (ELF_ST_VISIBILITY (h->other))
  2916. {
  2917. case STV_INTERNAL:
  2918. case STV_HIDDEN:
  2919. return false;
  2920. case STV_PROTECTED:
  2921. hash_table = elf_hash_table (info);
  2922. if (!is_elf_hash_table (&hash_table->root))
  2923. return false;
  2924. bed = get_elf_backend_data (hash_table->dynobj);
  2925. /* Proper resolution for function pointer equality may require
  2926. that these symbols perhaps be resolved dynamically, even though
  2927. we should be resolving them to the current module. */
  2928. if (!not_local_protected || !bed->is_function_type (h->type))
  2929. binding_stays_local_p = true;
  2930. break;
  2931. default:
  2932. break;
  2933. }
  2934. /* If it isn't defined locally, then clearly it's dynamic. */
  2935. if (!h->def_regular && !ELF_COMMON_DEF_P (h))
  2936. return true;
  2937. /* Otherwise, the symbol is dynamic if binding rules don't tell
  2938. us that it remains local. */
  2939. return !binding_stays_local_p;
  2940. }
  2941. /* Return true if the symbol referred to by H should be considered
  2942. to resolve local to the current module, and false otherwise. Differs
  2943. from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of
  2944. undefined symbols. The two functions are virtually identical except
  2945. for the place where dynindx == -1 is tested. If that test is true,
  2946. _bfd_elf_dynamic_symbol_p will say the symbol is local, while
  2947. _bfd_elf_symbol_refs_local_p will say the symbol is local only for
  2948. defined symbols.
  2949. It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as
  2950. !_bfd_elf_symbol_refs_local_p, except that targets differ in their
  2951. treatment of undefined weak symbols. For those that do not make
  2952. undefined weak symbols dynamic, both functions may return false. */
  2953. bool
  2954. _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
  2955. struct bfd_link_info *info,
  2956. bool local_protected)
  2957. {
  2958. const struct elf_backend_data *bed;
  2959. struct elf_link_hash_table *hash_table;
  2960. /* If it's a local sym, of course we resolve locally. */
  2961. if (h == NULL)
  2962. return true;
  2963. /* STV_HIDDEN or STV_INTERNAL ones must be local. */
  2964. if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
  2965. || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
  2966. return true;
  2967. /* Forced local symbols resolve locally. */
  2968. if (h->forced_local)
  2969. return true;
  2970. /* Common symbols that become definitions don't get the DEF_REGULAR
  2971. flag set, so test it first, and don't bail out. */
  2972. if (ELF_COMMON_DEF_P (h))
  2973. /* Do nothing. */;
  2974. /* If we don't have a definition in a regular file, then we can't
  2975. resolve locally. The sym is either undefined or dynamic. */
  2976. else if (!h->def_regular)
  2977. return false;
  2978. /* Non-dynamic symbols resolve locally. */
  2979. if (h->dynindx == -1)
  2980. return true;
  2981. /* At this point, we know the symbol is defined and dynamic. In an
  2982. executable it must resolve locally, likewise when building symbolic
  2983. shared libraries. */
  2984. if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h))
  2985. return true;
  2986. /* Now deal with defined dynamic symbols in shared libraries. Ones
  2987. with default visibility might not resolve locally. */
  2988. if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
  2989. return false;
  2990. hash_table = elf_hash_table (info);
  2991. if (!is_elf_hash_table (&hash_table->root))
  2992. return true;
  2993. /* STV_PROTECTED symbols with indirect external access are local. */
  2994. if (info->indirect_extern_access > 0)
  2995. return true;
  2996. bed = get_elf_backend_data (hash_table->dynobj);
  2997. /* If extern_protected_data is false, STV_PROTECTED non-function
  2998. symbols are local. */
  2999. if ((!info->extern_protected_data
  3000. || (info->extern_protected_data < 0
  3001. && !bed->extern_protected_data))
  3002. && !bed->is_function_type (h->type))
  3003. return true;
  3004. /* Function pointer equality tests may require that STV_PROTECTED
  3005. symbols be treated as dynamic symbols. If the address of a
  3006. function not defined in an executable is set to that function's
  3007. plt entry in the executable, then the address of the function in
  3008. a shared library must also be the plt entry in the executable. */
  3009. return local_protected;
  3010. }
  3011. /* Caches some TLS segment info, and ensures that the TLS segment vma is
  3012. aligned. Returns the first TLS output section. */
  3013. struct bfd_section *
  3014. _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
  3015. {
  3016. struct bfd_section *sec, *tls;
  3017. unsigned int align = 0;
  3018. for (sec = obfd->sections; sec != NULL; sec = sec->next)
  3019. if ((sec->flags & SEC_THREAD_LOCAL) != 0)
  3020. break;
  3021. tls = sec;
  3022. for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next)
  3023. if (sec->alignment_power > align)
  3024. align = sec->alignment_power;
  3025. elf_hash_table (info)->tls_sec = tls;
  3026. /* Ensure the alignment of the first section (usually .tdata) is the largest
  3027. alignment, so that the tls segment starts aligned. */
  3028. if (tls != NULL)
  3029. tls->alignment_power = align;
  3030. return tls;
  3031. }
  3032. /* Return TRUE iff this is a non-common, definition of a non-function symbol. */
  3033. static bool
  3034. is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED,
  3035. Elf_Internal_Sym *sym)
  3036. {
  3037. const struct elf_backend_data *bed;
  3038. /* Local symbols do not count, but target specific ones might. */
  3039. if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
  3040. && ELF_ST_BIND (sym->st_info) < STB_LOOS)
  3041. return false;
  3042. bed = get_elf_backend_data (abfd);
  3043. /* Function symbols do not count. */
  3044. if (bed->is_function_type (ELF_ST_TYPE (sym->st_info)))
  3045. return false;
  3046. /* If the section is undefined, then so is the symbol. */
  3047. if (sym->st_shndx == SHN_UNDEF)
  3048. return false;
  3049. /* If the symbol is defined in the common section, then
  3050. it is a common definition and so does not count. */
  3051. if (bed->common_definition (sym))
  3052. return false;
  3053. /* If the symbol is in a target specific section then we
  3054. must rely upon the backend to tell us what it is. */
  3055. if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
  3056. /* FIXME - this function is not coded yet:
  3057. return _bfd_is_global_symbol_definition (abfd, sym);
  3058. Instead for now assume that the definition is not global,
  3059. Even if this is wrong, at least the linker will behave
  3060. in the same way that it used to do. */
  3061. return false;
  3062. return true;
  3063. }
  3064. /* Search the symbol table of the archive element of the archive ABFD
  3065. whose archive map contains a mention of SYMDEF, and determine if
  3066. the symbol is defined in this element. */
  3067. static bool
  3068. elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
  3069. {
  3070. Elf_Internal_Shdr * hdr;
  3071. size_t symcount;
  3072. size_t extsymcount;
  3073. size_t extsymoff;
  3074. Elf_Internal_Sym *isymbuf;
  3075. Elf_Internal_Sym *isym;
  3076. Elf_Internal_Sym *isymend;
  3077. bool result;
  3078. abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
  3079. if (abfd == NULL)
  3080. return false;
  3081. if (! bfd_check_format (abfd, bfd_object))
  3082. return false;
  3083. /* Select the appropriate symbol table. If we don't know if the
  3084. object file is an IR object, give linker LTO plugin a chance to
  3085. get the correct symbol table. */
  3086. if (abfd->plugin_format == bfd_plugin_yes
  3087. #if BFD_SUPPORTS_PLUGINS
  3088. || (abfd->plugin_format == bfd_plugin_unknown
  3089. && bfd_link_plugin_object_p (abfd))
  3090. #endif
  3091. )
  3092. {
  3093. /* Use the IR symbol table if the object has been claimed by
  3094. plugin. */
  3095. abfd = abfd->plugin_dummy_bfd;
  3096. hdr = &elf_tdata (abfd)->symtab_hdr;
  3097. }
  3098. else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
  3099. hdr = &elf_tdata (abfd)->symtab_hdr;
  3100. else
  3101. hdr = &elf_tdata (abfd)->dynsymtab_hdr;
  3102. symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
  3103. /* The sh_info field of the symtab header tells us where the
  3104. external symbols start. We don't care about the local symbols. */
  3105. if (elf_bad_symtab (abfd))
  3106. {
  3107. extsymcount = symcount;
  3108. extsymoff = 0;
  3109. }
  3110. else
  3111. {
  3112. extsymcount = symcount - hdr->sh_info;
  3113. extsymoff = hdr->sh_info;
  3114. }
  3115. if (extsymcount == 0)
  3116. return false;
  3117. /* Read in the symbol table. */
  3118. isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
  3119. NULL, NULL, NULL);
  3120. if (isymbuf == NULL)
  3121. return false;
  3122. /* Scan the symbol table looking for SYMDEF. */
  3123. result = false;
  3124. for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++)
  3125. {
  3126. const char *name;
  3127. name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
  3128. isym->st_name);
  3129. if (name == NULL)
  3130. break;
  3131. if (strcmp (name, symdef->name) == 0)
  3132. {
  3133. result = is_global_data_symbol_definition (abfd, isym);
  3134. break;
  3135. }
  3136. }
  3137. free (isymbuf);
  3138. return result;
  3139. }
  3140. /* Add an entry to the .dynamic table. */
  3141. bool
  3142. _bfd_elf_add_dynamic_entry (struct bfd_link_info *info,
  3143. bfd_vma tag,
  3144. bfd_vma val)
  3145. {
  3146. struct elf_link_hash_table *hash_table;
  3147. const struct elf_backend_data *bed;
  3148. asection *s;
  3149. bfd_size_type newsize;
  3150. bfd_byte *newcontents;
  3151. Elf_Internal_Dyn dyn;
  3152. hash_table = elf_hash_table (info);
  3153. if (! is_elf_hash_table (&hash_table->root))
  3154. return false;
  3155. if (tag == DT_RELA || tag == DT_REL)
  3156. hash_table->dynamic_relocs = true;
  3157. bed = get_elf_backend_data (hash_table->dynobj);
  3158. s = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
  3159. BFD_ASSERT (s != NULL);
  3160. newsize = s->size + bed->s->sizeof_dyn;
  3161. newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
  3162. if (newcontents == NULL)
  3163. return false;
  3164. dyn.d_tag = tag;
  3165. dyn.d_un.d_val = val;
  3166. bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size);
  3167. s->size = newsize;
  3168. s->contents = newcontents;
  3169. return true;
  3170. }
  3171. /* Strip zero-sized dynamic sections. */
  3172. bool
  3173. _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info)
  3174. {
  3175. struct elf_link_hash_table *hash_table;
  3176. const struct elf_backend_data *bed;
  3177. asection *s, *sdynamic, **pp;
  3178. asection *rela_dyn, *rel_dyn;
  3179. Elf_Internal_Dyn dyn;
  3180. bfd_byte *extdyn, *next;
  3181. void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
  3182. bool strip_zero_sized;
  3183. bool strip_zero_sized_plt;
  3184. if (bfd_link_relocatable (info))
  3185. return true;
  3186. hash_table = elf_hash_table (info);
  3187. if (!is_elf_hash_table (&hash_table->root))
  3188. return false;
  3189. if (!hash_table->dynobj)
  3190. return true;
  3191. sdynamic= bfd_get_linker_section (hash_table->dynobj, ".dynamic");
  3192. if (!sdynamic)
  3193. return true;
  3194. bed = get_elf_backend_data (hash_table->dynobj);
  3195. swap_dyn_in = bed->s->swap_dyn_in;
  3196. strip_zero_sized = false;
  3197. strip_zero_sized_plt = false;
  3198. /* Strip zero-sized dynamic sections. */
  3199. rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn");
  3200. rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn");
  3201. for (pp = &info->output_bfd->sections; (s = *pp) != NULL;)
  3202. if (s->size == 0
  3203. && (s == rela_dyn
  3204. || s == rel_dyn
  3205. || s == hash_table->srelplt->output_section
  3206. || s == hash_table->splt->output_section))
  3207. {
  3208. *pp = s->next;
  3209. info->output_bfd->section_count--;
  3210. strip_zero_sized = true;
  3211. if (s == rela_dyn)
  3212. s = rela_dyn;
  3213. if (s == rel_dyn)
  3214. s = rel_dyn;
  3215. else if (s == hash_table->splt->output_section)
  3216. {
  3217. s = hash_table->splt;
  3218. strip_zero_sized_plt = true;
  3219. }
  3220. else
  3221. s = hash_table->srelplt;
  3222. s->flags |= SEC_EXCLUDE;
  3223. s->output_section = bfd_abs_section_ptr;
  3224. }
  3225. else
  3226. pp = &s->next;
  3227. if (strip_zero_sized_plt && sdynamic->size != 0)
  3228. for (extdyn = sdynamic->contents;
  3229. extdyn < sdynamic->contents + sdynamic->size;
  3230. extdyn = next)
  3231. {
  3232. next = extdyn + bed->s->sizeof_dyn;
  3233. swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
  3234. switch (dyn.d_tag)
  3235. {
  3236. default:
  3237. break;
  3238. case DT_JMPREL:
  3239. case DT_PLTRELSZ:
  3240. case DT_PLTREL:
  3241. /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if
  3242. the procedure linkage table (the .plt section) has been
  3243. removed. */
  3244. memmove (extdyn, next,
  3245. sdynamic->size - (next - sdynamic->contents));
  3246. next = extdyn;
  3247. }
  3248. }
  3249. if (strip_zero_sized)
  3250. {
  3251. /* Regenerate program headers. */
  3252. elf_seg_map (info->output_bfd) = NULL;
  3253. return _bfd_elf_map_sections_to_segments (info->output_bfd, info,
  3254. NULL);
  3255. }
  3256. return true;
  3257. }
  3258. /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error,
  3259. 1 if a DT_NEEDED tag already exists, and 0 on success. */
  3260. int
  3261. bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info)
  3262. {
  3263. struct elf_link_hash_table *hash_table;
  3264. size_t strindex;
  3265. const char *soname;
  3266. if (!_bfd_elf_link_create_dynstrtab (abfd, info))
  3267. return -1;
  3268. hash_table = elf_hash_table (info);
  3269. soname = elf_dt_name (abfd);
  3270. strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false);
  3271. if (strindex == (size_t) -1)
  3272. return -1;
  3273. if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1)
  3274. {
  3275. asection *sdyn;
  3276. const struct elf_backend_data *bed;
  3277. bfd_byte *extdyn;
  3278. bed = get_elf_backend_data (hash_table->dynobj);
  3279. sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic");
  3280. if (sdyn != NULL && sdyn->size != 0)
  3281. for (extdyn = sdyn->contents;
  3282. extdyn < sdyn->contents + sdyn->size;
  3283. extdyn += bed->s->sizeof_dyn)
  3284. {
  3285. Elf_Internal_Dyn dyn;
  3286. bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn);
  3287. if (dyn.d_tag == DT_NEEDED
  3288. && dyn.d_un.d_val == strindex)
  3289. {
  3290. _bfd_elf_strtab_delref (hash_table->dynstr, strindex);
  3291. return 1;
  3292. }
  3293. }
  3294. }
  3295. if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info))
  3296. return -1;
  3297. if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex))
  3298. return -1;
  3299. return 0;
  3300. }
  3301. /* Return true if SONAME is on the needed list between NEEDED and STOP
  3302. (or the end of list if STOP is NULL), and needed by a library that
  3303. will be loaded. */
  3304. static bool
  3305. on_needed_list (const char *soname,
  3306. struct bfd_link_needed_list *needed,
  3307. struct bfd_link_needed_list *stop)
  3308. {
  3309. struct bfd_link_needed_list *look;
  3310. for (look = needed; look != stop; look = look->next)
  3311. if (strcmp (soname, look->name) == 0
  3312. && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0
  3313. /* If needed by a library that itself is not directly
  3314. needed, recursively check whether that library is
  3315. indirectly needed. Since we add DT_NEEDED entries to
  3316. the end of the list, library dependencies appear after
  3317. the library. Therefore search prior to the current
  3318. LOOK, preventing possible infinite recursion. */
  3319. || on_needed_list (elf_dt_name (look->by), needed, look)))
  3320. return true;
  3321. return false;
  3322. }
  3323. /* Sort symbol by value, section, size, and type. */
  3324. static int
  3325. elf_sort_symbol (const void *arg1, const void *arg2)
  3326. {
  3327. const struct elf_link_hash_entry *h1;
  3328. const struct elf_link_hash_entry *h2;
  3329. bfd_signed_vma vdiff;
  3330. int sdiff;
  3331. const char *n1;
  3332. const char *n2;
  3333. h1 = *(const struct elf_link_hash_entry **) arg1;
  3334. h2 = *(const struct elf_link_hash_entry **) arg2;
  3335. vdiff = h1->root.u.def.value - h2->root.u.def.value;
  3336. if (vdiff != 0)
  3337. return vdiff > 0 ? 1 : -1;
  3338. sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
  3339. if (sdiff != 0)
  3340. return sdiff;
  3341. /* Sort so that sized symbols are selected over zero size symbols. */
  3342. vdiff = h1->size - h2->size;
  3343. if (vdiff != 0)
  3344. return vdiff > 0 ? 1 : -1;
  3345. /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */
  3346. if (h1->type != h2->type)
  3347. return h1->type - h2->type;
  3348. /* If symbols are properly sized and typed, and multiple strong
  3349. aliases are not defined in a shared library by the user we
  3350. shouldn't get here. Unfortunately linker script symbols like
  3351. __bss_start sometimes match a user symbol defined at the start of
  3352. .bss without proper size and type. We'd like to preference the
  3353. user symbol over reserved system symbols. Sort on leading
  3354. underscores. */
  3355. n1 = h1->root.root.string;
  3356. n2 = h2->root.root.string;
  3357. while (*n1 == *n2)
  3358. {
  3359. if (*n1 == 0)
  3360. break;
  3361. ++n1;
  3362. ++n2;
  3363. }
  3364. if (*n1 == '_')
  3365. return -1;
  3366. if (*n2 == '_')
  3367. return 1;
  3368. /* Final sort on name selects user symbols like '_u' over reserved
  3369. system symbols like '_Z' and also will avoid qsort instability. */
  3370. return *n1 - *n2;
  3371. }
  3372. /* This function is used to adjust offsets into .dynstr for
  3373. dynamic symbols. This is called via elf_link_hash_traverse. */
  3374. static bool
  3375. elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data)
  3376. {
  3377. struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data;
  3378. if (h->dynindx != -1)
  3379. h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index);
  3380. return true;
  3381. }
  3382. /* Assign string offsets in .dynstr, update all structures referencing
  3383. them. */
  3384. static bool
  3385. elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info)
  3386. {
  3387. struct elf_link_hash_table *hash_table = elf_hash_table (info);
  3388. struct elf_link_local_dynamic_entry *entry;
  3389. struct elf_strtab_hash *dynstr = hash_table->dynstr;
  3390. bfd *dynobj = hash_table->dynobj;
  3391. asection *sdyn;
  3392. bfd_size_type size;
  3393. const struct elf_backend_data *bed;
  3394. bfd_byte *extdyn;
  3395. _bfd_elf_strtab_finalize (dynstr);
  3396. size = _bfd_elf_strtab_size (dynstr);
  3397. /* Allow the linker to examine the dynsymtab now it's fully populated. */
  3398. if (info->callbacks->examine_strtab)
  3399. info->callbacks->examine_strtab (dynstr);
  3400. bed = get_elf_backend_data (dynobj);
  3401. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  3402. BFD_ASSERT (sdyn != NULL);
  3403. /* Update all .dynamic entries referencing .dynstr strings. */
  3404. for (extdyn = sdyn->contents;
  3405. extdyn < PTR_ADD (sdyn->contents, sdyn->size);
  3406. extdyn += bed->s->sizeof_dyn)
  3407. {
  3408. Elf_Internal_Dyn dyn;
  3409. bed->s->swap_dyn_in (dynobj, extdyn, &dyn);
  3410. switch (dyn.d_tag)
  3411. {
  3412. case DT_STRSZ:
  3413. dyn.d_un.d_val = size;
  3414. break;
  3415. case DT_NEEDED:
  3416. case DT_SONAME:
  3417. case DT_RPATH:
  3418. case DT_RUNPATH:
  3419. case DT_FILTER:
  3420. case DT_AUXILIARY:
  3421. case DT_AUDIT:
  3422. case DT_DEPAUDIT:
  3423. dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val);
  3424. break;
  3425. default:
  3426. continue;
  3427. }
  3428. bed->s->swap_dyn_out (dynobj, &dyn, extdyn);
  3429. }
  3430. /* Now update local dynamic symbols. */
  3431. for (entry = hash_table->dynlocal; entry ; entry = entry->next)
  3432. entry->isym.st_name = _bfd_elf_strtab_offset (dynstr,
  3433. entry->isym.st_name);
  3434. /* And the rest of dynamic symbols. */
  3435. elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr);
  3436. /* Adjust version definitions. */
  3437. if (elf_tdata (output_bfd)->cverdefs)
  3438. {
  3439. asection *s;
  3440. bfd_byte *p;
  3441. size_t i;
  3442. Elf_Internal_Verdef def;
  3443. Elf_Internal_Verdaux defaux;
  3444. s = bfd_get_linker_section (dynobj, ".gnu.version_d");
  3445. p = s->contents;
  3446. do
  3447. {
  3448. _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p,
  3449. &def);
  3450. p += sizeof (Elf_External_Verdef);
  3451. if (def.vd_aux != sizeof (Elf_External_Verdef))
  3452. continue;
  3453. for (i = 0; i < def.vd_cnt; ++i)
  3454. {
  3455. _bfd_elf_swap_verdaux_in (output_bfd,
  3456. (Elf_External_Verdaux *) p, &defaux);
  3457. defaux.vda_name = _bfd_elf_strtab_offset (dynstr,
  3458. defaux.vda_name);
  3459. _bfd_elf_swap_verdaux_out (output_bfd,
  3460. &defaux, (Elf_External_Verdaux *) p);
  3461. p += sizeof (Elf_External_Verdaux);
  3462. }
  3463. }
  3464. while (def.vd_next);
  3465. }
  3466. /* Adjust version references. */
  3467. if (elf_tdata (output_bfd)->verref)
  3468. {
  3469. asection *s;
  3470. bfd_byte *p;
  3471. size_t i;
  3472. Elf_Internal_Verneed need;
  3473. Elf_Internal_Vernaux needaux;
  3474. s = bfd_get_linker_section (dynobj, ".gnu.version_r");
  3475. p = s->contents;
  3476. do
  3477. {
  3478. _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p,
  3479. &need);
  3480. need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file);
  3481. _bfd_elf_swap_verneed_out (output_bfd, &need,
  3482. (Elf_External_Verneed *) p);
  3483. p += sizeof (Elf_External_Verneed);
  3484. for (i = 0; i < need.vn_cnt; ++i)
  3485. {
  3486. _bfd_elf_swap_vernaux_in (output_bfd,
  3487. (Elf_External_Vernaux *) p, &needaux);
  3488. needaux.vna_name = _bfd_elf_strtab_offset (dynstr,
  3489. needaux.vna_name);
  3490. _bfd_elf_swap_vernaux_out (output_bfd,
  3491. &needaux,
  3492. (Elf_External_Vernaux *) p);
  3493. p += sizeof (Elf_External_Vernaux);
  3494. }
  3495. }
  3496. while (need.vn_next);
  3497. }
  3498. return true;
  3499. }
  3500. /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
  3501. The default is to only match when the INPUT and OUTPUT are exactly
  3502. the same target. */
  3503. bool
  3504. _bfd_elf_default_relocs_compatible (const bfd_target *input,
  3505. const bfd_target *output)
  3506. {
  3507. return input == output;
  3508. }
  3509. /* Return TRUE iff relocations for INPUT are compatible with OUTPUT.
  3510. This version is used when different targets for the same architecture
  3511. are virtually identical. */
  3512. bool
  3513. _bfd_elf_relocs_compatible (const bfd_target *input,
  3514. const bfd_target *output)
  3515. {
  3516. const struct elf_backend_data *obed, *ibed;
  3517. if (input == output)
  3518. return true;
  3519. ibed = xvec_get_elf_backend_data (input);
  3520. obed = xvec_get_elf_backend_data (output);
  3521. if (ibed->arch != obed->arch)
  3522. return false;
  3523. /* If both backends are using this function, deem them compatible. */
  3524. return ibed->relocs_compatible == obed->relocs_compatible;
  3525. }
  3526. /* Make a special call to the linker "notice" function to tell it that
  3527. we are about to handle an as-needed lib, or have finished
  3528. processing the lib. */
  3529. bool
  3530. _bfd_elf_notice_as_needed (bfd *ibfd,
  3531. struct bfd_link_info *info,
  3532. enum notice_asneeded_action act)
  3533. {
  3534. return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0);
  3535. }
  3536. /* Call ACTION on each relocation in an ELF object file. */
  3537. bool
  3538. _bfd_elf_link_iterate_on_relocs
  3539. (bfd *abfd, struct bfd_link_info *info,
  3540. bool (*action) (bfd *, struct bfd_link_info *, asection *,
  3541. const Elf_Internal_Rela *))
  3542. {
  3543. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  3544. struct elf_link_hash_table *htab = elf_hash_table (info);
  3545. /* If this object is the same format as the output object, and it is
  3546. not a shared library, then let the backend look through the
  3547. relocs.
  3548. This is required to build global offset table entries and to
  3549. arrange for dynamic relocs. It is not required for the
  3550. particular common case of linking non PIC code, even when linking
  3551. against shared libraries, but unfortunately there is no way of
  3552. knowing whether an object file has been compiled PIC or not.
  3553. Looking through the relocs is not particularly time consuming.
  3554. The problem is that we must either (1) keep the relocs in memory,
  3555. which causes the linker to require additional runtime memory or
  3556. (2) read the relocs twice from the input file, which wastes time.
  3557. This would be a good case for using mmap.
  3558. I have no idea how to handle linking PIC code into a file of a
  3559. different format. It probably can't be done. */
  3560. if ((abfd->flags & DYNAMIC) == 0
  3561. && is_elf_hash_table (&htab->root)
  3562. && elf_object_id (abfd) == elf_hash_table_id (htab)
  3563. && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec))
  3564. {
  3565. asection *o;
  3566. for (o = abfd->sections; o != NULL; o = o->next)
  3567. {
  3568. Elf_Internal_Rela *internal_relocs;
  3569. bool ok;
  3570. /* Don't check relocations in excluded sections. Don't do
  3571. anything special with non-loaded, non-alloced sections.
  3572. In particular, any relocs in such sections should not
  3573. affect GOT and PLT reference counting (ie. we don't
  3574. allow them to create GOT or PLT entries), there's no
  3575. possibility or desire to optimize TLS relocs, and
  3576. there's not much point in propagating relocs to shared
  3577. libs that the dynamic linker won't relocate. */
  3578. if ((o->flags & SEC_ALLOC) == 0
  3579. || (o->flags & SEC_RELOC) == 0
  3580. || (o->flags & SEC_EXCLUDE) != 0
  3581. || o->reloc_count == 0
  3582. || ((info->strip == strip_all || info->strip == strip_debugger)
  3583. && (o->flags & SEC_DEBUGGING) != 0)
  3584. || bfd_is_abs_section (o->output_section))
  3585. continue;
  3586. internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
  3587. o, NULL,
  3588. NULL,
  3589. _bfd_link_keep_memory (info));
  3590. if (internal_relocs == NULL)
  3591. return false;
  3592. ok = action (abfd, info, o, internal_relocs);
  3593. if (elf_section_data (o)->relocs != internal_relocs)
  3594. free (internal_relocs);
  3595. if (! ok)
  3596. return false;
  3597. }
  3598. }
  3599. return true;
  3600. }
  3601. /* Check relocations in an ELF object file. This is called after
  3602. all input files have been opened. */
  3603. bool
  3604. _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
  3605. {
  3606. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  3607. if (bed->check_relocs != NULL)
  3608. return _bfd_elf_link_iterate_on_relocs (abfd, info,
  3609. bed->check_relocs);
  3610. return true;
  3611. }
  3612. /* Add symbols from an ELF object file to the linker hash table. */
  3613. static bool
  3614. elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
  3615. {
  3616. Elf_Internal_Ehdr *ehdr;
  3617. Elf_Internal_Shdr *hdr;
  3618. size_t symcount;
  3619. size_t extsymcount;
  3620. size_t extsymoff;
  3621. struct elf_link_hash_entry **sym_hash;
  3622. bool dynamic;
  3623. Elf_External_Versym *extversym = NULL;
  3624. Elf_External_Versym *extversym_end = NULL;
  3625. Elf_External_Versym *ever;
  3626. struct elf_link_hash_entry *weaks;
  3627. struct elf_link_hash_entry **nondeflt_vers = NULL;
  3628. size_t nondeflt_vers_cnt = 0;
  3629. Elf_Internal_Sym *isymbuf = NULL;
  3630. Elf_Internal_Sym *isym;
  3631. Elf_Internal_Sym *isymend;
  3632. const struct elf_backend_data *bed;
  3633. bool add_needed;
  3634. struct elf_link_hash_table *htab;
  3635. void *alloc_mark = NULL;
  3636. struct bfd_hash_entry **old_table = NULL;
  3637. unsigned int old_size = 0;
  3638. unsigned int old_count = 0;
  3639. void *old_tab = NULL;
  3640. void *old_ent;
  3641. struct bfd_link_hash_entry *old_undefs = NULL;
  3642. struct bfd_link_hash_entry *old_undefs_tail = NULL;
  3643. void *old_strtab = NULL;
  3644. size_t tabsize = 0;
  3645. asection *s;
  3646. bool just_syms;
  3647. htab = elf_hash_table (info);
  3648. bed = get_elf_backend_data (abfd);
  3649. if ((abfd->flags & DYNAMIC) == 0)
  3650. dynamic = false;
  3651. else
  3652. {
  3653. dynamic = true;
  3654. /* You can't use -r against a dynamic object. Also, there's no
  3655. hope of using a dynamic object which does not exactly match
  3656. the format of the output file. */
  3657. if (bfd_link_relocatable (info)
  3658. || !is_elf_hash_table (&htab->root)
  3659. || info->output_bfd->xvec != abfd->xvec)
  3660. {
  3661. if (bfd_link_relocatable (info))
  3662. bfd_set_error (bfd_error_invalid_operation);
  3663. else
  3664. bfd_set_error (bfd_error_wrong_format);
  3665. goto error_return;
  3666. }
  3667. }
  3668. ehdr = elf_elfheader (abfd);
  3669. if (info->warn_alternate_em
  3670. && bed->elf_machine_code != ehdr->e_machine
  3671. && ((bed->elf_machine_alt1 != 0
  3672. && ehdr->e_machine == bed->elf_machine_alt1)
  3673. || (bed->elf_machine_alt2 != 0
  3674. && ehdr->e_machine == bed->elf_machine_alt2)))
  3675. _bfd_error_handler
  3676. /* xgettext:c-format */
  3677. (_("alternate ELF machine code found (%d) in %pB, expecting %d"),
  3678. ehdr->e_machine, abfd, bed->elf_machine_code);
  3679. /* As a GNU extension, any input sections which are named
  3680. .gnu.warning.SYMBOL are treated as warning symbols for the given
  3681. symbol. This differs from .gnu.warning sections, which generate
  3682. warnings when they are included in an output file. */
  3683. /* PR 12761: Also generate this warning when building shared libraries. */
  3684. for (s = abfd->sections; s != NULL; s = s->next)
  3685. {
  3686. const char *name;
  3687. name = bfd_section_name (s);
  3688. if (startswith (name, ".gnu.warning."))
  3689. {
  3690. char *msg;
  3691. bfd_size_type sz;
  3692. name += sizeof ".gnu.warning." - 1;
  3693. /* If this is a shared object, then look up the symbol
  3694. in the hash table. If it is there, and it is already
  3695. been defined, then we will not be using the entry
  3696. from this shared object, so we don't need to warn.
  3697. FIXME: If we see the definition in a regular object
  3698. later on, we will warn, but we shouldn't. The only
  3699. fix is to keep track of what warnings we are supposed
  3700. to emit, and then handle them all at the end of the
  3701. link. */
  3702. if (dynamic)
  3703. {
  3704. struct elf_link_hash_entry *h;
  3705. h = elf_link_hash_lookup (htab, name, false, false, true);
  3706. /* FIXME: What about bfd_link_hash_common? */
  3707. if (h != NULL
  3708. && (h->root.type == bfd_link_hash_defined
  3709. || h->root.type == bfd_link_hash_defweak))
  3710. continue;
  3711. }
  3712. sz = s->size;
  3713. msg = (char *) bfd_alloc (abfd, sz + 1);
  3714. if (msg == NULL)
  3715. goto error_return;
  3716. if (! bfd_get_section_contents (abfd, s, msg, 0, sz))
  3717. goto error_return;
  3718. msg[sz] = '\0';
  3719. if (! (_bfd_generic_link_add_one_symbol
  3720. (info, abfd, name, BSF_WARNING, s, 0, msg,
  3721. false, bed->collect, NULL)))
  3722. goto error_return;
  3723. if (bfd_link_executable (info))
  3724. {
  3725. /* Clobber the section size so that the warning does
  3726. not get copied into the output file. */
  3727. s->size = 0;
  3728. /* Also set SEC_EXCLUDE, so that symbols defined in
  3729. the warning section don't get copied to the output. */
  3730. s->flags |= SEC_EXCLUDE;
  3731. }
  3732. }
  3733. }
  3734. just_syms = ((s = abfd->sections) != NULL
  3735. && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS);
  3736. add_needed = true;
  3737. if (! dynamic)
  3738. {
  3739. /* If we are creating a shared library, create all the dynamic
  3740. sections immediately. We need to attach them to something,
  3741. so we attach them to this BFD, provided it is the right
  3742. format and is not from ld --just-symbols. Always create the
  3743. dynamic sections for -E/--dynamic-list. FIXME: If there
  3744. are no input BFD's of the same format as the output, we can't
  3745. make a shared library. */
  3746. if (!just_syms
  3747. && (bfd_link_pic (info)
  3748. || (!bfd_link_relocatable (info)
  3749. && info->nointerp
  3750. && (info->export_dynamic || info->dynamic)))
  3751. && is_elf_hash_table (&htab->root)
  3752. && info->output_bfd->xvec == abfd->xvec
  3753. && !htab->dynamic_sections_created)
  3754. {
  3755. if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
  3756. goto error_return;
  3757. }
  3758. }
  3759. else if (!is_elf_hash_table (&htab->root))
  3760. goto error_return;
  3761. else
  3762. {
  3763. const char *soname = NULL;
  3764. char *audit = NULL;
  3765. struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
  3766. const Elf_Internal_Phdr *phdr;
  3767. struct elf_link_loaded_list *loaded_lib;
  3768. /* ld --just-symbols and dynamic objects don't mix very well.
  3769. ld shouldn't allow it. */
  3770. if (just_syms)
  3771. abort ();
  3772. /* If this dynamic lib was specified on the command line with
  3773. --as-needed in effect, then we don't want to add a DT_NEEDED
  3774. tag unless the lib is actually used. Similary for libs brought
  3775. in by another lib's DT_NEEDED. When --no-add-needed is used
  3776. on a dynamic lib, we don't want to add a DT_NEEDED entry for
  3777. any dynamic library in DT_NEEDED tags in the dynamic lib at
  3778. all. */
  3779. add_needed = (elf_dyn_lib_class (abfd)
  3780. & (DYN_AS_NEEDED | DYN_DT_NEEDED
  3781. | DYN_NO_NEEDED)) == 0;
  3782. s = bfd_get_section_by_name (abfd, ".dynamic");
  3783. if (s != NULL && s->size != 0)
  3784. {
  3785. bfd_byte *dynbuf;
  3786. bfd_byte *extdyn;
  3787. unsigned int elfsec;
  3788. unsigned long shlink;
  3789. if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
  3790. {
  3791. error_free_dyn:
  3792. free (dynbuf);
  3793. goto error_return;
  3794. }
  3795. elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
  3796. if (elfsec == SHN_BAD)
  3797. goto error_free_dyn;
  3798. shlink = elf_elfsections (abfd)[elfsec]->sh_link;
  3799. for (extdyn = dynbuf;
  3800. extdyn <= dynbuf + s->size - bed->s->sizeof_dyn;
  3801. extdyn += bed->s->sizeof_dyn)
  3802. {
  3803. Elf_Internal_Dyn dyn;
  3804. bed->s->swap_dyn_in (abfd, extdyn, &dyn);
  3805. if (dyn.d_tag == DT_SONAME)
  3806. {
  3807. unsigned int tagv = dyn.d_un.d_val;
  3808. soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  3809. if (soname == NULL)
  3810. goto error_free_dyn;
  3811. }
  3812. if (dyn.d_tag == DT_NEEDED)
  3813. {
  3814. struct bfd_link_needed_list *n, **pn;
  3815. char *fnm, *anm;
  3816. unsigned int tagv = dyn.d_un.d_val;
  3817. size_t amt = sizeof (struct bfd_link_needed_list);
  3818. n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
  3819. fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  3820. if (n == NULL || fnm == NULL)
  3821. goto error_free_dyn;
  3822. amt = strlen (fnm) + 1;
  3823. anm = (char *) bfd_alloc (abfd, amt);
  3824. if (anm == NULL)
  3825. goto error_free_dyn;
  3826. memcpy (anm, fnm, amt);
  3827. n->name = anm;
  3828. n->by = abfd;
  3829. n->next = NULL;
  3830. for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next)
  3831. ;
  3832. *pn = n;
  3833. }
  3834. if (dyn.d_tag == DT_RUNPATH)
  3835. {
  3836. struct bfd_link_needed_list *n, **pn;
  3837. char *fnm, *anm;
  3838. unsigned int tagv = dyn.d_un.d_val;
  3839. size_t amt = sizeof (struct bfd_link_needed_list);
  3840. n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
  3841. fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  3842. if (n == NULL || fnm == NULL)
  3843. goto error_free_dyn;
  3844. amt = strlen (fnm) + 1;
  3845. anm = (char *) bfd_alloc (abfd, amt);
  3846. if (anm == NULL)
  3847. goto error_free_dyn;
  3848. memcpy (anm, fnm, amt);
  3849. n->name = anm;
  3850. n->by = abfd;
  3851. n->next = NULL;
  3852. for (pn = & runpath;
  3853. *pn != NULL;
  3854. pn = &(*pn)->next)
  3855. ;
  3856. *pn = n;
  3857. }
  3858. /* Ignore DT_RPATH if we have seen DT_RUNPATH. */
  3859. if (!runpath && dyn.d_tag == DT_RPATH)
  3860. {
  3861. struct bfd_link_needed_list *n, **pn;
  3862. char *fnm, *anm;
  3863. unsigned int tagv = dyn.d_un.d_val;
  3864. size_t amt = sizeof (struct bfd_link_needed_list);
  3865. n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
  3866. fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  3867. if (n == NULL || fnm == NULL)
  3868. goto error_free_dyn;
  3869. amt = strlen (fnm) + 1;
  3870. anm = (char *) bfd_alloc (abfd, amt);
  3871. if (anm == NULL)
  3872. goto error_free_dyn;
  3873. memcpy (anm, fnm, amt);
  3874. n->name = anm;
  3875. n->by = abfd;
  3876. n->next = NULL;
  3877. for (pn = & rpath;
  3878. *pn != NULL;
  3879. pn = &(*pn)->next)
  3880. ;
  3881. *pn = n;
  3882. }
  3883. if (dyn.d_tag == DT_AUDIT)
  3884. {
  3885. unsigned int tagv = dyn.d_un.d_val;
  3886. audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  3887. }
  3888. if (dyn.d_tag == DT_FLAGS_1)
  3889. elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0;
  3890. }
  3891. free (dynbuf);
  3892. }
  3893. /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that
  3894. frees all more recently bfd_alloc'd blocks as well. */
  3895. if (runpath)
  3896. rpath = runpath;
  3897. if (rpath)
  3898. {
  3899. struct bfd_link_needed_list **pn;
  3900. for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next)
  3901. ;
  3902. *pn = rpath;
  3903. }
  3904. /* If we have a PT_GNU_RELRO program header, mark as read-only
  3905. all sections contained fully therein. This makes relro
  3906. shared library sections appear as they will at run-time. */
  3907. phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
  3908. while (phdr-- > elf_tdata (abfd)->phdr)
  3909. if (phdr->p_type == PT_GNU_RELRO)
  3910. {
  3911. for (s = abfd->sections; s != NULL; s = s->next)
  3912. {
  3913. unsigned int opb = bfd_octets_per_byte (abfd, s);
  3914. if ((s->flags & SEC_ALLOC) != 0
  3915. && s->vma * opb >= phdr->p_vaddr
  3916. && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz)
  3917. s->flags |= SEC_READONLY;
  3918. }
  3919. break;
  3920. }
  3921. /* We do not want to include any of the sections in a dynamic
  3922. object in the output file. We hack by simply clobbering the
  3923. list of sections in the BFD. This could be handled more
  3924. cleanly by, say, a new section flag; the existing
  3925. SEC_NEVER_LOAD flag is not the one we want, because that one
  3926. still implies that the section takes up space in the output
  3927. file. */
  3928. bfd_section_list_clear (abfd);
  3929. /* Find the name to use in a DT_NEEDED entry that refers to this
  3930. object. If the object has a DT_SONAME entry, we use it.
  3931. Otherwise, if the generic linker stuck something in
  3932. elf_dt_name, we use that. Otherwise, we just use the file
  3933. name. */
  3934. if (soname == NULL || *soname == '\0')
  3935. {
  3936. soname = elf_dt_name (abfd);
  3937. if (soname == NULL || *soname == '\0')
  3938. soname = bfd_get_filename (abfd);
  3939. }
  3940. /* Save the SONAME because sometimes the linker emulation code
  3941. will need to know it. */
  3942. elf_dt_name (abfd) = soname;
  3943. /* If we have already included this dynamic object in the
  3944. link, just ignore it. There is no reason to include a
  3945. particular dynamic object more than once. */
  3946. for (loaded_lib = htab->dyn_loaded;
  3947. loaded_lib != NULL;
  3948. loaded_lib = loaded_lib->next)
  3949. {
  3950. if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0)
  3951. return true;
  3952. }
  3953. /* Create dynamic sections for backends that require that be done
  3954. before setup_gnu_properties. */
  3955. if (add_needed
  3956. && !_bfd_elf_link_create_dynamic_sections (abfd, info))
  3957. return false;
  3958. /* Save the DT_AUDIT entry for the linker emulation code. */
  3959. elf_dt_audit (abfd) = audit;
  3960. }
  3961. /* If this is a dynamic object, we always link against the .dynsym
  3962. symbol table, not the .symtab symbol table. The dynamic linker
  3963. will only see the .dynsym symbol table, so there is no reason to
  3964. look at .symtab for a dynamic object. */
  3965. if (! dynamic || elf_dynsymtab (abfd) == 0)
  3966. hdr = &elf_tdata (abfd)->symtab_hdr;
  3967. else
  3968. hdr = &elf_tdata (abfd)->dynsymtab_hdr;
  3969. symcount = hdr->sh_size / bed->s->sizeof_sym;
  3970. /* The sh_info field of the symtab header tells us where the
  3971. external symbols start. We don't care about the local symbols at
  3972. this point. */
  3973. if (elf_bad_symtab (abfd))
  3974. {
  3975. extsymcount = symcount;
  3976. extsymoff = 0;
  3977. }
  3978. else
  3979. {
  3980. extsymcount = symcount - hdr->sh_info;
  3981. extsymoff = hdr->sh_info;
  3982. }
  3983. sym_hash = elf_sym_hashes (abfd);
  3984. if (extsymcount != 0)
  3985. {
  3986. isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff,
  3987. NULL, NULL, NULL);
  3988. if (isymbuf == NULL)
  3989. goto error_return;
  3990. if (sym_hash == NULL)
  3991. {
  3992. /* We store a pointer to the hash table entry for each
  3993. external symbol. */
  3994. size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *);
  3995. sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt);
  3996. if (sym_hash == NULL)
  3997. goto error_free_sym;
  3998. elf_sym_hashes (abfd) = sym_hash;
  3999. }
  4000. }
  4001. if (dynamic)
  4002. {
  4003. /* Read in any version definitions. */
  4004. if (!_bfd_elf_slurp_version_tables (abfd,
  4005. info->default_imported_symver))
  4006. goto error_free_sym;
  4007. /* Read in the symbol versions, but don't bother to convert them
  4008. to internal format. */
  4009. if (elf_dynversym (abfd) != 0)
  4010. {
  4011. Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr;
  4012. bfd_size_type amt = versymhdr->sh_size;
  4013. if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0)
  4014. goto error_free_sym;
  4015. extversym = (Elf_External_Versym *)
  4016. _bfd_malloc_and_read (abfd, amt, amt);
  4017. if (extversym == NULL)
  4018. goto error_free_sym;
  4019. extversym_end = extversym + amt / sizeof (*extversym);
  4020. }
  4021. }
  4022. /* If we are loading an as-needed shared lib, save the symbol table
  4023. state before we start adding symbols. If the lib turns out
  4024. to be unneeded, restore the state. */
  4025. if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
  4026. {
  4027. unsigned int i;
  4028. size_t entsize;
  4029. for (entsize = 0, i = 0; i < htab->root.table.size; i++)
  4030. {
  4031. struct bfd_hash_entry *p;
  4032. struct elf_link_hash_entry *h;
  4033. for (p = htab->root.table.table[i]; p != NULL; p = p->next)
  4034. {
  4035. h = (struct elf_link_hash_entry *) p;
  4036. entsize += htab->root.table.entsize;
  4037. if (h->root.type == bfd_link_hash_warning)
  4038. {
  4039. entsize += htab->root.table.entsize;
  4040. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4041. }
  4042. if (h->root.type == bfd_link_hash_common)
  4043. entsize += sizeof (*h->root.u.c.p);
  4044. }
  4045. }
  4046. tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *);
  4047. old_tab = bfd_malloc (tabsize + entsize);
  4048. if (old_tab == NULL)
  4049. goto error_free_vers;
  4050. /* Remember the current objalloc pointer, so that all mem for
  4051. symbols added can later be reclaimed. */
  4052. alloc_mark = bfd_hash_allocate (&htab->root.table, 1);
  4053. if (alloc_mark == NULL)
  4054. goto error_free_vers;
  4055. /* Make a special call to the linker "notice" function to
  4056. tell it that we are about to handle an as-needed lib. */
  4057. if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed))
  4058. goto error_free_vers;
  4059. /* Clone the symbol table. Remember some pointers into the
  4060. symbol table, and dynamic symbol count. */
  4061. old_ent = (char *) old_tab + tabsize;
  4062. memcpy (old_tab, htab->root.table.table, tabsize);
  4063. old_undefs = htab->root.undefs;
  4064. old_undefs_tail = htab->root.undefs_tail;
  4065. old_table = htab->root.table.table;
  4066. old_size = htab->root.table.size;
  4067. old_count = htab->root.table.count;
  4068. old_strtab = NULL;
  4069. if (htab->dynstr != NULL)
  4070. {
  4071. old_strtab = _bfd_elf_strtab_save (htab->dynstr);
  4072. if (old_strtab == NULL)
  4073. goto error_free_vers;
  4074. }
  4075. for (i = 0; i < htab->root.table.size; i++)
  4076. {
  4077. struct bfd_hash_entry *p;
  4078. struct elf_link_hash_entry *h;
  4079. for (p = htab->root.table.table[i]; p != NULL; p = p->next)
  4080. {
  4081. h = (struct elf_link_hash_entry *) p;
  4082. memcpy (old_ent, h, htab->root.table.entsize);
  4083. old_ent = (char *) old_ent + htab->root.table.entsize;
  4084. if (h->root.type == bfd_link_hash_warning)
  4085. {
  4086. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4087. memcpy (old_ent, h, htab->root.table.entsize);
  4088. old_ent = (char *) old_ent + htab->root.table.entsize;
  4089. }
  4090. if (h->root.type == bfd_link_hash_common)
  4091. {
  4092. memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p));
  4093. old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
  4094. }
  4095. }
  4096. }
  4097. }
  4098. weaks = NULL;
  4099. if (extversym == NULL)
  4100. ever = NULL;
  4101. else if (extversym + extsymoff < extversym_end)
  4102. ever = extversym + extsymoff;
  4103. else
  4104. {
  4105. /* xgettext:c-format */
  4106. _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"),
  4107. abfd, (long) extsymoff,
  4108. (long) (extversym_end - extversym) / sizeof (* extversym));
  4109. bfd_set_error (bfd_error_bad_value);
  4110. goto error_free_vers;
  4111. }
  4112. if (!bfd_link_relocatable (info)
  4113. && abfd->lto_slim_object)
  4114. {
  4115. _bfd_error_handler
  4116. (_("%pB: plugin needed to handle lto object"), abfd);
  4117. }
  4118. for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount);
  4119. isym < isymend;
  4120. isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
  4121. {
  4122. int bind;
  4123. bfd_vma value;
  4124. asection *sec, *new_sec;
  4125. flagword flags;
  4126. const char *name;
  4127. struct elf_link_hash_entry *h;
  4128. struct elf_link_hash_entry *hi;
  4129. bool definition;
  4130. bool size_change_ok;
  4131. bool type_change_ok;
  4132. bool new_weak;
  4133. bool old_weak;
  4134. bfd *override;
  4135. bool common;
  4136. bool discarded;
  4137. unsigned int old_alignment;
  4138. unsigned int shindex;
  4139. bfd *old_bfd;
  4140. bool matched;
  4141. override = NULL;
  4142. flags = BSF_NO_FLAGS;
  4143. sec = NULL;
  4144. value = isym->st_value;
  4145. common = bed->common_definition (isym);
  4146. if (common && info->inhibit_common_definition)
  4147. {
  4148. /* Treat common symbol as undefined for --no-define-common. */
  4149. isym->st_shndx = SHN_UNDEF;
  4150. common = false;
  4151. }
  4152. discarded = false;
  4153. bind = ELF_ST_BIND (isym->st_info);
  4154. switch (bind)
  4155. {
  4156. case STB_LOCAL:
  4157. /* This should be impossible, since ELF requires that all
  4158. global symbols follow all local symbols, and that sh_info
  4159. point to the first global symbol. Unfortunately, Irix 5
  4160. screws this up. */
  4161. if (elf_bad_symtab (abfd))
  4162. continue;
  4163. /* If we aren't prepared to handle locals within the globals
  4164. then we'll likely segfault on a NULL symbol hash if the
  4165. symbol is ever referenced in relocations. */
  4166. shindex = elf_elfheader (abfd)->e_shstrndx;
  4167. name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name);
  4168. _bfd_error_handler (_("%pB: %s local symbol at index %lu"
  4169. " (>= sh_info of %lu)"),
  4170. abfd, name, (long) (isym - isymbuf + extsymoff),
  4171. (long) extsymoff);
  4172. /* Dynamic object relocations are not processed by ld, so
  4173. ld won't run into the problem mentioned above. */
  4174. if (dynamic)
  4175. continue;
  4176. bfd_set_error (bfd_error_bad_value);
  4177. goto error_free_vers;
  4178. case STB_GLOBAL:
  4179. if (isym->st_shndx != SHN_UNDEF && !common)
  4180. flags = BSF_GLOBAL;
  4181. break;
  4182. case STB_WEAK:
  4183. flags = BSF_WEAK;
  4184. break;
  4185. case STB_GNU_UNIQUE:
  4186. flags = BSF_GNU_UNIQUE;
  4187. break;
  4188. default:
  4189. /* Leave it up to the processor backend. */
  4190. break;
  4191. }
  4192. if (isym->st_shndx == SHN_UNDEF)
  4193. sec = bfd_und_section_ptr;
  4194. else if (isym->st_shndx == SHN_ABS)
  4195. sec = bfd_abs_section_ptr;
  4196. else if (isym->st_shndx == SHN_COMMON)
  4197. {
  4198. sec = bfd_com_section_ptr;
  4199. /* What ELF calls the size we call the value. What ELF
  4200. calls the value we call the alignment. */
  4201. value = isym->st_size;
  4202. }
  4203. else
  4204. {
  4205. sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  4206. if (sec == NULL)
  4207. sec = bfd_abs_section_ptr;
  4208. else if (discarded_section (sec))
  4209. {
  4210. /* Symbols from discarded section are undefined. We keep
  4211. its visibility. */
  4212. sec = bfd_und_section_ptr;
  4213. discarded = true;
  4214. isym->st_shndx = SHN_UNDEF;
  4215. }
  4216. else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
  4217. value -= sec->vma;
  4218. }
  4219. name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
  4220. isym->st_name);
  4221. if (name == NULL)
  4222. goto error_free_vers;
  4223. if (isym->st_shndx == SHN_COMMON
  4224. && (abfd->flags & BFD_PLUGIN) != 0)
  4225. {
  4226. asection *xc = bfd_get_section_by_name (abfd, "COMMON");
  4227. if (xc == NULL)
  4228. {
  4229. flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
  4230. | SEC_EXCLUDE);
  4231. xc = bfd_make_section_with_flags (abfd, "COMMON", sflags);
  4232. if (xc == NULL)
  4233. goto error_free_vers;
  4234. }
  4235. sec = xc;
  4236. }
  4237. else if (isym->st_shndx == SHN_COMMON
  4238. && ELF_ST_TYPE (isym->st_info) == STT_TLS
  4239. && !bfd_link_relocatable (info))
  4240. {
  4241. asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon");
  4242. if (tcomm == NULL)
  4243. {
  4244. flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON
  4245. | SEC_LINKER_CREATED);
  4246. tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags);
  4247. if (tcomm == NULL)
  4248. goto error_free_vers;
  4249. }
  4250. sec = tcomm;
  4251. }
  4252. else if (bed->elf_add_symbol_hook)
  4253. {
  4254. if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags,
  4255. &sec, &value))
  4256. goto error_free_vers;
  4257. /* The hook function sets the name to NULL if this symbol
  4258. should be skipped for some reason. */
  4259. if (name == NULL)
  4260. continue;
  4261. }
  4262. /* Sanity check that all possibilities were handled. */
  4263. if (sec == NULL)
  4264. abort ();
  4265. /* Silently discard TLS symbols from --just-syms. There's
  4266. no way to combine a static TLS block with a new TLS block
  4267. for this executable. */
  4268. if (ELF_ST_TYPE (isym->st_info) == STT_TLS
  4269. && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
  4270. continue;
  4271. if (bfd_is_und_section (sec)
  4272. || bfd_is_com_section (sec))
  4273. definition = false;
  4274. else
  4275. definition = true;
  4276. size_change_ok = false;
  4277. type_change_ok = bed->type_change_ok;
  4278. old_weak = false;
  4279. matched = false;
  4280. old_alignment = 0;
  4281. old_bfd = NULL;
  4282. new_sec = sec;
  4283. if (is_elf_hash_table (&htab->root))
  4284. {
  4285. Elf_Internal_Versym iver;
  4286. unsigned int vernum = 0;
  4287. bool skip;
  4288. if (ever == NULL)
  4289. {
  4290. if (info->default_imported_symver)
  4291. /* Use the default symbol version created earlier. */
  4292. iver.vs_vers = elf_tdata (abfd)->cverdefs;
  4293. else
  4294. iver.vs_vers = 0;
  4295. }
  4296. else if (ever >= extversym_end)
  4297. {
  4298. /* xgettext:c-format */
  4299. _bfd_error_handler (_("%pB: not enough version information"),
  4300. abfd);
  4301. bfd_set_error (bfd_error_bad_value);
  4302. goto error_free_vers;
  4303. }
  4304. else
  4305. _bfd_elf_swap_versym_in (abfd, ever, &iver);
  4306. vernum = iver.vs_vers & VERSYM_VERSION;
  4307. /* If this is a hidden symbol, or if it is not version
  4308. 1, we append the version name to the symbol name.
  4309. However, we do not modify a non-hidden absolute symbol
  4310. if it is not a function, because it might be the version
  4311. symbol itself. FIXME: What if it isn't? */
  4312. if ((iver.vs_vers & VERSYM_HIDDEN) != 0
  4313. || (vernum > 1
  4314. && (!bfd_is_abs_section (sec)
  4315. || bed->is_function_type (ELF_ST_TYPE (isym->st_info)))))
  4316. {
  4317. const char *verstr;
  4318. size_t namelen, verlen, newlen;
  4319. char *newname, *p;
  4320. if (isym->st_shndx != SHN_UNDEF)
  4321. {
  4322. if (vernum > elf_tdata (abfd)->cverdefs)
  4323. verstr = NULL;
  4324. else if (vernum > 1)
  4325. verstr =
  4326. elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
  4327. else
  4328. verstr = "";
  4329. if (verstr == NULL)
  4330. {
  4331. _bfd_error_handler
  4332. /* xgettext:c-format */
  4333. (_("%pB: %s: invalid version %u (max %d)"),
  4334. abfd, name, vernum,
  4335. elf_tdata (abfd)->cverdefs);
  4336. bfd_set_error (bfd_error_bad_value);
  4337. goto error_free_vers;
  4338. }
  4339. }
  4340. else
  4341. {
  4342. /* We cannot simply test for the number of
  4343. entries in the VERNEED section since the
  4344. numbers for the needed versions do not start
  4345. at 0. */
  4346. Elf_Internal_Verneed *t;
  4347. verstr = NULL;
  4348. for (t = elf_tdata (abfd)->verref;
  4349. t != NULL;
  4350. t = t->vn_nextref)
  4351. {
  4352. Elf_Internal_Vernaux *a;
  4353. for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
  4354. {
  4355. if (a->vna_other == vernum)
  4356. {
  4357. verstr = a->vna_nodename;
  4358. break;
  4359. }
  4360. }
  4361. if (a != NULL)
  4362. break;
  4363. }
  4364. if (verstr == NULL)
  4365. {
  4366. _bfd_error_handler
  4367. /* xgettext:c-format */
  4368. (_("%pB: %s: invalid needed version %d"),
  4369. abfd, name, vernum);
  4370. bfd_set_error (bfd_error_bad_value);
  4371. goto error_free_vers;
  4372. }
  4373. }
  4374. namelen = strlen (name);
  4375. verlen = strlen (verstr);
  4376. newlen = namelen + verlen + 2;
  4377. if ((iver.vs_vers & VERSYM_HIDDEN) == 0
  4378. && isym->st_shndx != SHN_UNDEF)
  4379. ++newlen;
  4380. newname = (char *) bfd_hash_allocate (&htab->root.table, newlen);
  4381. if (newname == NULL)
  4382. goto error_free_vers;
  4383. memcpy (newname, name, namelen);
  4384. p = newname + namelen;
  4385. *p++ = ELF_VER_CHR;
  4386. /* If this is a defined non-hidden version symbol,
  4387. we add another @ to the name. This indicates the
  4388. default version of the symbol. */
  4389. if ((iver.vs_vers & VERSYM_HIDDEN) == 0
  4390. && isym->st_shndx != SHN_UNDEF)
  4391. *p++ = ELF_VER_CHR;
  4392. memcpy (p, verstr, verlen + 1);
  4393. name = newname;
  4394. }
  4395. /* If this symbol has default visibility and the user has
  4396. requested we not re-export it, then mark it as hidden. */
  4397. if (!bfd_is_und_section (sec)
  4398. && !dynamic
  4399. && abfd->no_export
  4400. && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL)
  4401. isym->st_other = (STV_HIDDEN
  4402. | (isym->st_other & ~ELF_ST_VISIBILITY (-1)));
  4403. if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value,
  4404. sym_hash, &old_bfd, &old_weak,
  4405. &old_alignment, &skip, &override,
  4406. &type_change_ok, &size_change_ok,
  4407. &matched))
  4408. goto error_free_vers;
  4409. if (skip)
  4410. continue;
  4411. /* Override a definition only if the new symbol matches the
  4412. existing one. */
  4413. if (override && matched)
  4414. definition = false;
  4415. h = *sym_hash;
  4416. while (h->root.type == bfd_link_hash_indirect
  4417. || h->root.type == bfd_link_hash_warning)
  4418. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4419. if (h->versioned != unversioned
  4420. && elf_tdata (abfd)->verdef != NULL
  4421. && vernum > 1
  4422. && definition)
  4423. h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
  4424. }
  4425. if (! (_bfd_generic_link_add_one_symbol
  4426. (info, override ? override : abfd, name, flags, sec, value,
  4427. NULL, false, bed->collect,
  4428. (struct bfd_link_hash_entry **) sym_hash)))
  4429. goto error_free_vers;
  4430. h = *sym_hash;
  4431. /* We need to make sure that indirect symbol dynamic flags are
  4432. updated. */
  4433. hi = h;
  4434. while (h->root.type == bfd_link_hash_indirect
  4435. || h->root.type == bfd_link_hash_warning)
  4436. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4437. *sym_hash = h;
  4438. /* Setting the index to -3 tells elf_link_output_extsym that
  4439. this symbol is defined in a discarded section. */
  4440. if (discarded && is_elf_hash_table (&htab->root))
  4441. h->indx = -3;
  4442. new_weak = (flags & BSF_WEAK) != 0;
  4443. if (dynamic
  4444. && definition
  4445. && new_weak
  4446. && !bed->is_function_type (ELF_ST_TYPE (isym->st_info))
  4447. && is_elf_hash_table (&htab->root)
  4448. && h->u.alias == NULL)
  4449. {
  4450. /* Keep a list of all weak defined non function symbols from
  4451. a dynamic object, using the alias field. Later in this
  4452. function we will set the alias field to the correct
  4453. value. We only put non-function symbols from dynamic
  4454. objects on this list, because that happens to be the only
  4455. time we need to know the normal symbol corresponding to a
  4456. weak symbol, and the information is time consuming to
  4457. figure out. If the alias field is not already NULL,
  4458. then this symbol was already defined by some previous
  4459. dynamic object, and we will be using that previous
  4460. definition anyhow. */
  4461. h->u.alias = weaks;
  4462. weaks = h;
  4463. }
  4464. /* Set the alignment of a common symbol. */
  4465. if ((common || bfd_is_com_section (sec))
  4466. && h->root.type == bfd_link_hash_common)
  4467. {
  4468. unsigned int align;
  4469. if (common)
  4470. align = bfd_log2 (isym->st_value);
  4471. else
  4472. {
  4473. /* The new symbol is a common symbol in a shared object.
  4474. We need to get the alignment from the section. */
  4475. align = new_sec->alignment_power;
  4476. }
  4477. if (align > old_alignment)
  4478. h->root.u.c.p->alignment_power = align;
  4479. else
  4480. h->root.u.c.p->alignment_power = old_alignment;
  4481. }
  4482. if (is_elf_hash_table (&htab->root))
  4483. {
  4484. /* Set a flag in the hash table entry indicating the type of
  4485. reference or definition we just found. A dynamic symbol
  4486. is one which is referenced or defined by both a regular
  4487. object and a shared object. */
  4488. bool dynsym = false;
  4489. /* Plugin symbols aren't normal. Don't set def/ref flags. */
  4490. if ((abfd->flags & BFD_PLUGIN) != 0)
  4491. {
  4492. /* Except for this flag to track nonweak references. */
  4493. if (!definition
  4494. && bind != STB_WEAK)
  4495. h->ref_ir_nonweak = 1;
  4496. }
  4497. else if (!dynamic)
  4498. {
  4499. if (! definition)
  4500. {
  4501. h->ref_regular = 1;
  4502. if (bind != STB_WEAK)
  4503. h->ref_regular_nonweak = 1;
  4504. }
  4505. else
  4506. {
  4507. h->def_regular = 1;
  4508. if (h->def_dynamic)
  4509. {
  4510. h->def_dynamic = 0;
  4511. h->ref_dynamic = 1;
  4512. }
  4513. }
  4514. }
  4515. else
  4516. {
  4517. if (! definition)
  4518. {
  4519. h->ref_dynamic = 1;
  4520. hi->ref_dynamic = 1;
  4521. }
  4522. else
  4523. {
  4524. h->def_dynamic = 1;
  4525. hi->def_dynamic = 1;
  4526. }
  4527. }
  4528. /* If an indirect symbol has been forced local, don't
  4529. make the real symbol dynamic. */
  4530. if (h != hi && hi->forced_local)
  4531. ;
  4532. else if (!dynamic)
  4533. {
  4534. if (bfd_link_dll (info)
  4535. || h->def_dynamic
  4536. || h->ref_dynamic)
  4537. dynsym = true;
  4538. }
  4539. else
  4540. {
  4541. if (h->def_regular
  4542. || h->ref_regular
  4543. || (h->is_weakalias
  4544. && weakdef (h)->dynindx != -1))
  4545. dynsym = true;
  4546. }
  4547. /* Check to see if we need to add an indirect symbol for
  4548. the default name. */
  4549. if ((definition
  4550. || (!override && h->root.type == bfd_link_hash_common))
  4551. && !(hi != h
  4552. && hi->versioned == versioned_hidden))
  4553. if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym,
  4554. sec, value, &old_bfd, &dynsym))
  4555. goto error_free_vers;
  4556. /* Check the alignment when a common symbol is involved. This
  4557. can change when a common symbol is overridden by a normal
  4558. definition or a common symbol is ignored due to the old
  4559. normal definition. We need to make sure the maximum
  4560. alignment is maintained. */
  4561. if ((old_alignment || common)
  4562. && h->root.type != bfd_link_hash_common)
  4563. {
  4564. unsigned int common_align;
  4565. unsigned int normal_align;
  4566. unsigned int symbol_align;
  4567. bfd *normal_bfd;
  4568. bfd *common_bfd;
  4569. BFD_ASSERT (h->root.type == bfd_link_hash_defined
  4570. || h->root.type == bfd_link_hash_defweak);
  4571. symbol_align = ffs (h->root.u.def.value) - 1;
  4572. if (h->root.u.def.section->owner != NULL
  4573. && (h->root.u.def.section->owner->flags
  4574. & (DYNAMIC | BFD_PLUGIN)) == 0)
  4575. {
  4576. normal_align = h->root.u.def.section->alignment_power;
  4577. if (normal_align > symbol_align)
  4578. normal_align = symbol_align;
  4579. }
  4580. else
  4581. normal_align = symbol_align;
  4582. if (old_alignment)
  4583. {
  4584. common_align = old_alignment;
  4585. common_bfd = old_bfd;
  4586. normal_bfd = abfd;
  4587. }
  4588. else
  4589. {
  4590. common_align = bfd_log2 (isym->st_value);
  4591. common_bfd = abfd;
  4592. normal_bfd = old_bfd;
  4593. }
  4594. if (normal_align < common_align)
  4595. {
  4596. /* PR binutils/2735 */
  4597. if (normal_bfd == NULL)
  4598. _bfd_error_handler
  4599. /* xgettext:c-format */
  4600. (_("warning: alignment %u of common symbol `%s' in %pB is"
  4601. " greater than the alignment (%u) of its section %pA"),
  4602. 1 << common_align, name, common_bfd,
  4603. 1 << normal_align, h->root.u.def.section);
  4604. else
  4605. _bfd_error_handler
  4606. /* xgettext:c-format */
  4607. (_("warning: alignment %u of symbol `%s' in %pB"
  4608. " is smaller than %u in %pB"),
  4609. 1 << normal_align, name, normal_bfd,
  4610. 1 << common_align, common_bfd);
  4611. }
  4612. }
  4613. /* Remember the symbol size if it isn't undefined. */
  4614. if (isym->st_size != 0
  4615. && isym->st_shndx != SHN_UNDEF
  4616. && (definition || h->size == 0))
  4617. {
  4618. if (h->size != 0
  4619. && h->size != isym->st_size
  4620. && ! size_change_ok)
  4621. _bfd_error_handler
  4622. /* xgettext:c-format */
  4623. (_("warning: size of symbol `%s' changed"
  4624. " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"),
  4625. name, (uint64_t) h->size, old_bfd,
  4626. (uint64_t) isym->st_size, abfd);
  4627. h->size = isym->st_size;
  4628. }
  4629. /* If this is a common symbol, then we always want H->SIZE
  4630. to be the size of the common symbol. The code just above
  4631. won't fix the size if a common symbol becomes larger. We
  4632. don't warn about a size change here, because that is
  4633. covered by --warn-common. Allow changes between different
  4634. function types. */
  4635. if (h->root.type == bfd_link_hash_common)
  4636. h->size = h->root.u.c.size;
  4637. if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE
  4638. && ((definition && !new_weak)
  4639. || (old_weak && h->root.type == bfd_link_hash_common)
  4640. || h->type == STT_NOTYPE))
  4641. {
  4642. unsigned int type = ELF_ST_TYPE (isym->st_info);
  4643. /* Turn an IFUNC symbol from a DSO into a normal FUNC
  4644. symbol. */
  4645. if (type == STT_GNU_IFUNC
  4646. && (abfd->flags & DYNAMIC) != 0)
  4647. type = STT_FUNC;
  4648. if (h->type != type)
  4649. {
  4650. if (h->type != STT_NOTYPE && ! type_change_ok)
  4651. /* xgettext:c-format */
  4652. _bfd_error_handler
  4653. (_("warning: type of symbol `%s' changed"
  4654. " from %d to %d in %pB"),
  4655. name, h->type, type, abfd);
  4656. h->type = type;
  4657. }
  4658. }
  4659. /* Merge st_other field. */
  4660. elf_merge_st_other (abfd, h, isym->st_other, sec,
  4661. definition, dynamic);
  4662. /* We don't want to make debug symbol dynamic. */
  4663. if (definition
  4664. && (sec->flags & SEC_DEBUGGING)
  4665. && !bfd_link_relocatable (info))
  4666. dynsym = false;
  4667. /* Nor should we make plugin symbols dynamic. */
  4668. if ((abfd->flags & BFD_PLUGIN) != 0)
  4669. dynsym = false;
  4670. if (definition)
  4671. {
  4672. h->target_internal = isym->st_target_internal;
  4673. h->unique_global = (flags & BSF_GNU_UNIQUE) != 0;
  4674. }
  4675. if (definition && !dynamic)
  4676. {
  4677. char *p = strchr (name, ELF_VER_CHR);
  4678. if (p != NULL && p[1] != ELF_VER_CHR)
  4679. {
  4680. /* Queue non-default versions so that .symver x, x@FOO
  4681. aliases can be checked. */
  4682. if (!nondeflt_vers)
  4683. {
  4684. size_t amt = ((isymend - isym + 1)
  4685. * sizeof (struct elf_link_hash_entry *));
  4686. nondeflt_vers
  4687. = (struct elf_link_hash_entry **) bfd_malloc (amt);
  4688. if (!nondeflt_vers)
  4689. goto error_free_vers;
  4690. }
  4691. nondeflt_vers[nondeflt_vers_cnt++] = h;
  4692. }
  4693. }
  4694. if (dynsym && h->dynindx == -1)
  4695. {
  4696. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  4697. goto error_free_vers;
  4698. if (h->is_weakalias
  4699. && weakdef (h)->dynindx == -1)
  4700. {
  4701. if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h)))
  4702. goto error_free_vers;
  4703. }
  4704. }
  4705. else if (h->dynindx != -1)
  4706. /* If the symbol already has a dynamic index, but
  4707. visibility says it should not be visible, turn it into
  4708. a local symbol. */
  4709. switch (ELF_ST_VISIBILITY (h->other))
  4710. {
  4711. case STV_INTERNAL:
  4712. case STV_HIDDEN:
  4713. (*bed->elf_backend_hide_symbol) (info, h, true);
  4714. dynsym = false;
  4715. break;
  4716. }
  4717. if (!add_needed
  4718. && matched
  4719. && definition
  4720. && h->root.type != bfd_link_hash_indirect
  4721. && ((dynsym
  4722. && h->ref_regular_nonweak)
  4723. || (old_bfd != NULL
  4724. && (old_bfd->flags & BFD_PLUGIN) != 0
  4725. && h->ref_ir_nonweak
  4726. && !info->lto_all_symbols_read)
  4727. || (h->ref_dynamic_nonweak
  4728. && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0
  4729. && !on_needed_list (elf_dt_name (abfd),
  4730. htab->needed, NULL))))
  4731. {
  4732. const char *soname = elf_dt_name (abfd);
  4733. info->callbacks->minfo ("%!", soname, old_bfd,
  4734. h->root.root.string);
  4735. /* A symbol from a library loaded via DT_NEEDED of some
  4736. other library is referenced by a regular object.
  4737. Add a DT_NEEDED entry for it. Issue an error if
  4738. --no-add-needed is used and the reference was not
  4739. a weak one. */
  4740. if (old_bfd != NULL
  4741. && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0)
  4742. {
  4743. _bfd_error_handler
  4744. /* xgettext:c-format */
  4745. (_("%pB: undefined reference to symbol '%s'"),
  4746. old_bfd, name);
  4747. bfd_set_error (bfd_error_missing_dso);
  4748. goto error_free_vers;
  4749. }
  4750. elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class)
  4751. (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED);
  4752. /* Create dynamic sections for backends that require
  4753. that be done before setup_gnu_properties. */
  4754. if (!_bfd_elf_link_create_dynamic_sections (abfd, info))
  4755. return false;
  4756. add_needed = true;
  4757. }
  4758. }
  4759. }
  4760. if (info->lto_plugin_active
  4761. && !bfd_link_relocatable (info)
  4762. && (abfd->flags & BFD_PLUGIN) == 0
  4763. && !just_syms
  4764. && extsymcount)
  4765. {
  4766. int r_sym_shift;
  4767. if (bed->s->arch_size == 32)
  4768. r_sym_shift = 8;
  4769. else
  4770. r_sym_shift = 32;
  4771. /* If linker plugin is enabled, set non_ir_ref_regular on symbols
  4772. referenced in regular objects so that linker plugin will get
  4773. the correct symbol resolution. */
  4774. sym_hash = elf_sym_hashes (abfd);
  4775. for (s = abfd->sections; s != NULL; s = s->next)
  4776. {
  4777. Elf_Internal_Rela *internal_relocs;
  4778. Elf_Internal_Rela *rel, *relend;
  4779. /* Don't check relocations in excluded sections. */
  4780. if ((s->flags & SEC_RELOC) == 0
  4781. || s->reloc_count == 0
  4782. || (s->flags & SEC_EXCLUDE) != 0
  4783. || ((info->strip == strip_all
  4784. || info->strip == strip_debugger)
  4785. && (s->flags & SEC_DEBUGGING) != 0))
  4786. continue;
  4787. internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
  4788. s, NULL,
  4789. NULL,
  4790. _bfd_link_keep_memory (info));
  4791. if (internal_relocs == NULL)
  4792. goto error_free_vers;
  4793. rel = internal_relocs;
  4794. relend = rel + s->reloc_count;
  4795. for ( ; rel < relend; rel++)
  4796. {
  4797. unsigned long r_symndx = rel->r_info >> r_sym_shift;
  4798. struct elf_link_hash_entry *h;
  4799. /* Skip local symbols. */
  4800. if (r_symndx < extsymoff)
  4801. continue;
  4802. h = sym_hash[r_symndx - extsymoff];
  4803. if (h != NULL)
  4804. h->root.non_ir_ref_regular = 1;
  4805. }
  4806. if (elf_section_data (s)->relocs != internal_relocs)
  4807. free (internal_relocs);
  4808. }
  4809. }
  4810. free (extversym);
  4811. extversym = NULL;
  4812. free (isymbuf);
  4813. isymbuf = NULL;
  4814. if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)
  4815. {
  4816. unsigned int i;
  4817. /* Restore the symbol table. */
  4818. old_ent = (char *) old_tab + tabsize;
  4819. memset (elf_sym_hashes (abfd), 0,
  4820. extsymcount * sizeof (struct elf_link_hash_entry *));
  4821. htab->root.table.table = old_table;
  4822. htab->root.table.size = old_size;
  4823. htab->root.table.count = old_count;
  4824. memcpy (htab->root.table.table, old_tab, tabsize);
  4825. htab->root.undefs = old_undefs;
  4826. htab->root.undefs_tail = old_undefs_tail;
  4827. if (htab->dynstr != NULL)
  4828. _bfd_elf_strtab_restore (htab->dynstr, old_strtab);
  4829. free (old_strtab);
  4830. old_strtab = NULL;
  4831. for (i = 0; i < htab->root.table.size; i++)
  4832. {
  4833. struct bfd_hash_entry *p;
  4834. struct elf_link_hash_entry *h;
  4835. unsigned int non_ir_ref_dynamic;
  4836. for (p = htab->root.table.table[i]; p != NULL; p = p->next)
  4837. {
  4838. /* Preserve non_ir_ref_dynamic so that this symbol
  4839. will be exported when the dynamic lib becomes needed
  4840. in the second pass. */
  4841. h = (struct elf_link_hash_entry *) p;
  4842. if (h->root.type == bfd_link_hash_warning)
  4843. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4844. non_ir_ref_dynamic = h->root.non_ir_ref_dynamic;
  4845. h = (struct elf_link_hash_entry *) p;
  4846. memcpy (h, old_ent, htab->root.table.entsize);
  4847. old_ent = (char *) old_ent + htab->root.table.entsize;
  4848. if (h->root.type == bfd_link_hash_warning)
  4849. {
  4850. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  4851. memcpy (h, old_ent, htab->root.table.entsize);
  4852. old_ent = (char *) old_ent + htab->root.table.entsize;
  4853. }
  4854. if (h->root.type == bfd_link_hash_common)
  4855. {
  4856. memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p));
  4857. old_ent = (char *) old_ent + sizeof (*h->root.u.c.p);
  4858. }
  4859. h->root.non_ir_ref_dynamic = non_ir_ref_dynamic;
  4860. }
  4861. }
  4862. /* Make a special call to the linker "notice" function to
  4863. tell it that symbols added for crefs may need to be removed. */
  4864. if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed))
  4865. goto error_free_vers;
  4866. free (old_tab);
  4867. objalloc_free_block ((struct objalloc *) htab->root.table.memory,
  4868. alloc_mark);
  4869. free (nondeflt_vers);
  4870. return true;
  4871. }
  4872. if (old_tab != NULL)
  4873. {
  4874. if (!(*bed->notice_as_needed) (abfd, info, notice_needed))
  4875. goto error_free_vers;
  4876. free (old_tab);
  4877. old_tab = NULL;
  4878. }
  4879. /* Now that all the symbols from this input file are created, if
  4880. not performing a relocatable link, handle .symver foo, foo@BAR
  4881. such that any relocs against foo become foo@BAR. */
  4882. if (!bfd_link_relocatable (info) && nondeflt_vers != NULL)
  4883. {
  4884. size_t cnt, symidx;
  4885. for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt)
  4886. {
  4887. struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi;
  4888. char *shortname, *p;
  4889. size_t amt;
  4890. p = strchr (h->root.root.string, ELF_VER_CHR);
  4891. if (p == NULL
  4892. || (h->root.type != bfd_link_hash_defined
  4893. && h->root.type != bfd_link_hash_defweak))
  4894. continue;
  4895. amt = p - h->root.root.string;
  4896. shortname = (char *) bfd_malloc (amt + 1);
  4897. if (!shortname)
  4898. goto error_free_vers;
  4899. memcpy (shortname, h->root.root.string, amt);
  4900. shortname[amt] = '\0';
  4901. hi = (struct elf_link_hash_entry *)
  4902. bfd_link_hash_lookup (&htab->root, shortname,
  4903. false, false, false);
  4904. if (hi != NULL
  4905. && hi->root.type == h->root.type
  4906. && hi->root.u.def.value == h->root.u.def.value
  4907. && hi->root.u.def.section == h->root.u.def.section)
  4908. {
  4909. (*bed->elf_backend_hide_symbol) (info, hi, true);
  4910. hi->root.type = bfd_link_hash_indirect;
  4911. hi->root.u.i.link = (struct bfd_link_hash_entry *) h;
  4912. (*bed->elf_backend_copy_indirect_symbol) (info, h, hi);
  4913. sym_hash = elf_sym_hashes (abfd);
  4914. if (sym_hash)
  4915. for (symidx = 0; symidx < extsymcount; ++symidx)
  4916. if (sym_hash[symidx] == hi)
  4917. {
  4918. sym_hash[symidx] = h;
  4919. break;
  4920. }
  4921. }
  4922. free (shortname);
  4923. }
  4924. free (nondeflt_vers);
  4925. nondeflt_vers = NULL;
  4926. }
  4927. /* Now set the alias field correctly for all the weak defined
  4928. symbols we found. The only way to do this is to search all the
  4929. symbols. Since we only need the information for non functions in
  4930. dynamic objects, that's the only time we actually put anything on
  4931. the list WEAKS. We need this information so that if a regular
  4932. object refers to a symbol defined weakly in a dynamic object, the
  4933. real symbol in the dynamic object is also put in the dynamic
  4934. symbols; we also must arrange for both symbols to point to the
  4935. same memory location. We could handle the general case of symbol
  4936. aliasing, but a general symbol alias can only be generated in
  4937. assembler code, handling it correctly would be very time
  4938. consuming, and other ELF linkers don't handle general aliasing
  4939. either. */
  4940. if (weaks != NULL)
  4941. {
  4942. struct elf_link_hash_entry **hpp;
  4943. struct elf_link_hash_entry **hppend;
  4944. struct elf_link_hash_entry **sorted_sym_hash;
  4945. struct elf_link_hash_entry *h;
  4946. size_t sym_count, amt;
  4947. /* Since we have to search the whole symbol list for each weak
  4948. defined symbol, search time for N weak defined symbols will be
  4949. O(N^2). Binary search will cut it down to O(NlogN). */
  4950. amt = extsymcount * sizeof (*sorted_sym_hash);
  4951. sorted_sym_hash = bfd_malloc (amt);
  4952. if (sorted_sym_hash == NULL)
  4953. goto error_return;
  4954. sym_hash = sorted_sym_hash;
  4955. hpp = elf_sym_hashes (abfd);
  4956. hppend = hpp + extsymcount;
  4957. sym_count = 0;
  4958. for (; hpp < hppend; hpp++)
  4959. {
  4960. h = *hpp;
  4961. if (h != NULL
  4962. && h->root.type == bfd_link_hash_defined
  4963. && !bed->is_function_type (h->type))
  4964. {
  4965. *sym_hash = h;
  4966. sym_hash++;
  4967. sym_count++;
  4968. }
  4969. }
  4970. qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash),
  4971. elf_sort_symbol);
  4972. while (weaks != NULL)
  4973. {
  4974. struct elf_link_hash_entry *hlook;
  4975. asection *slook;
  4976. bfd_vma vlook;
  4977. size_t i, j, idx = 0;
  4978. hlook = weaks;
  4979. weaks = hlook->u.alias;
  4980. hlook->u.alias = NULL;
  4981. if (hlook->root.type != bfd_link_hash_defined
  4982. && hlook->root.type != bfd_link_hash_defweak)
  4983. continue;
  4984. slook = hlook->root.u.def.section;
  4985. vlook = hlook->root.u.def.value;
  4986. i = 0;
  4987. j = sym_count;
  4988. while (i != j)
  4989. {
  4990. bfd_signed_vma vdiff;
  4991. idx = (i + j) / 2;
  4992. h = sorted_sym_hash[idx];
  4993. vdiff = vlook - h->root.u.def.value;
  4994. if (vdiff < 0)
  4995. j = idx;
  4996. else if (vdiff > 0)
  4997. i = idx + 1;
  4998. else
  4999. {
  5000. int sdiff = slook->id - h->root.u.def.section->id;
  5001. if (sdiff < 0)
  5002. j = idx;
  5003. else if (sdiff > 0)
  5004. i = idx + 1;
  5005. else
  5006. break;
  5007. }
  5008. }
  5009. /* We didn't find a value/section match. */
  5010. if (i == j)
  5011. continue;
  5012. /* With multiple aliases, or when the weak symbol is already
  5013. strongly defined, we have multiple matching symbols and
  5014. the binary search above may land on any of them. Step
  5015. one past the matching symbol(s). */
  5016. while (++idx != j)
  5017. {
  5018. h = sorted_sym_hash[idx];
  5019. if (h->root.u.def.section != slook
  5020. || h->root.u.def.value != vlook)
  5021. break;
  5022. }
  5023. /* Now look back over the aliases. Since we sorted by size
  5024. as well as value and section, we'll choose the one with
  5025. the largest size. */
  5026. while (idx-- != i)
  5027. {
  5028. h = sorted_sym_hash[idx];
  5029. /* Stop if value or section doesn't match. */
  5030. if (h->root.u.def.section != slook
  5031. || h->root.u.def.value != vlook)
  5032. break;
  5033. else if (h != hlook)
  5034. {
  5035. struct elf_link_hash_entry *t;
  5036. hlook->u.alias = h;
  5037. hlook->is_weakalias = 1;
  5038. t = h;
  5039. if (t->u.alias != NULL)
  5040. while (t->u.alias != h)
  5041. t = t->u.alias;
  5042. t->u.alias = hlook;
  5043. /* If the weak definition is in the list of dynamic
  5044. symbols, make sure the real definition is put
  5045. there as well. */
  5046. if (hlook->dynindx != -1 && h->dynindx == -1)
  5047. {
  5048. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  5049. {
  5050. err_free_sym_hash:
  5051. free (sorted_sym_hash);
  5052. goto error_return;
  5053. }
  5054. }
  5055. /* If the real definition is in the list of dynamic
  5056. symbols, make sure the weak definition is put
  5057. there as well. If we don't do this, then the
  5058. dynamic loader might not merge the entries for the
  5059. real definition and the weak definition. */
  5060. if (h->dynindx != -1 && hlook->dynindx == -1)
  5061. {
  5062. if (! bfd_elf_link_record_dynamic_symbol (info, hlook))
  5063. goto err_free_sym_hash;
  5064. }
  5065. break;
  5066. }
  5067. }
  5068. }
  5069. free (sorted_sym_hash);
  5070. }
  5071. if (bed->check_directives
  5072. && !(*bed->check_directives) (abfd, info))
  5073. return false;
  5074. /* If this is a non-traditional link, try to optimize the handling
  5075. of the .stab/.stabstr sections. */
  5076. if (! dynamic
  5077. && ! info->traditional_format
  5078. && is_elf_hash_table (&htab->root)
  5079. && (info->strip != strip_all && info->strip != strip_debugger))
  5080. {
  5081. asection *stabstr;
  5082. stabstr = bfd_get_section_by_name (abfd, ".stabstr");
  5083. if (stabstr != NULL)
  5084. {
  5085. bfd_size_type string_offset = 0;
  5086. asection *stab;
  5087. for (stab = abfd->sections; stab; stab = stab->next)
  5088. if (startswith (stab->name, ".stab")
  5089. && (!stab->name[5] ||
  5090. (stab->name[5] == '.' && ISDIGIT (stab->name[6])))
  5091. && (stab->flags & SEC_MERGE) == 0
  5092. && !bfd_is_abs_section (stab->output_section))
  5093. {
  5094. struct bfd_elf_section_data *secdata;
  5095. secdata = elf_section_data (stab);
  5096. if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab,
  5097. stabstr, &secdata->sec_info,
  5098. &string_offset))
  5099. goto error_return;
  5100. if (secdata->sec_info)
  5101. stab->sec_info_type = SEC_INFO_TYPE_STABS;
  5102. }
  5103. }
  5104. }
  5105. if (dynamic && add_needed)
  5106. {
  5107. /* Add this bfd to the loaded list. */
  5108. struct elf_link_loaded_list *n;
  5109. n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n));
  5110. if (n == NULL)
  5111. goto error_return;
  5112. n->abfd = abfd;
  5113. n->next = htab->dyn_loaded;
  5114. htab->dyn_loaded = n;
  5115. }
  5116. if (dynamic && !add_needed
  5117. && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0)
  5118. elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED;
  5119. return true;
  5120. error_free_vers:
  5121. free (old_tab);
  5122. free (old_strtab);
  5123. free (nondeflt_vers);
  5124. free (extversym);
  5125. error_free_sym:
  5126. free (isymbuf);
  5127. error_return:
  5128. return false;
  5129. }
  5130. /* Return the linker hash table entry of a symbol that might be
  5131. satisfied by an archive symbol. Return -1 on error. */
  5132. struct bfd_link_hash_entry *
  5133. _bfd_elf_archive_symbol_lookup (bfd *abfd,
  5134. struct bfd_link_info *info,
  5135. const char *name)
  5136. {
  5137. struct bfd_link_hash_entry *h;
  5138. char *p, *copy;
  5139. size_t len, first;
  5140. h = bfd_link_hash_lookup (info->hash, name, false, false, true);
  5141. if (h != NULL)
  5142. return h;
  5143. /* If this is a default version (the name contains @@), look up the
  5144. symbol again with only one `@' as well as without the version.
  5145. The effect is that references to the symbol with and without the
  5146. version will be matched by the default symbol in the archive. */
  5147. p = strchr (name, ELF_VER_CHR);
  5148. if (p == NULL || p[1] != ELF_VER_CHR)
  5149. return h;
  5150. /* First check with only one `@'. */
  5151. len = strlen (name);
  5152. copy = (char *) bfd_alloc (abfd, len);
  5153. if (copy == NULL)
  5154. return (struct bfd_link_hash_entry *) -1;
  5155. first = p - name + 1;
  5156. memcpy (copy, name, first);
  5157. memcpy (copy + first, name + first + 1, len - first);
  5158. h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
  5159. if (h == NULL)
  5160. {
  5161. /* We also need to check references to the symbol without the
  5162. version. */
  5163. copy[first - 1] = '\0';
  5164. h = bfd_link_hash_lookup (info->hash, copy, false, false, true);
  5165. }
  5166. bfd_release (abfd, copy);
  5167. return h;
  5168. }
  5169. /* Add symbols from an ELF archive file to the linker hash table. We
  5170. don't use _bfd_generic_link_add_archive_symbols because we need to
  5171. handle versioned symbols.
  5172. Fortunately, ELF archive handling is simpler than that done by
  5173. _bfd_generic_link_add_archive_symbols, which has to allow for a.out
  5174. oddities. In ELF, if we find a symbol in the archive map, and the
  5175. symbol is currently undefined, we know that we must pull in that
  5176. object file.
  5177. Unfortunately, we do have to make multiple passes over the symbol
  5178. table until nothing further is resolved. */
  5179. static bool
  5180. elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
  5181. {
  5182. symindex c;
  5183. unsigned char *included = NULL;
  5184. carsym *symdefs;
  5185. bool loop;
  5186. size_t amt;
  5187. const struct elf_backend_data *bed;
  5188. struct bfd_link_hash_entry * (*archive_symbol_lookup)
  5189. (bfd *, struct bfd_link_info *, const char *);
  5190. if (! bfd_has_map (abfd))
  5191. {
  5192. /* An empty archive is a special case. */
  5193. if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
  5194. return true;
  5195. bfd_set_error (bfd_error_no_armap);
  5196. return false;
  5197. }
  5198. /* Keep track of all symbols we know to be already defined, and all
  5199. files we know to be already included. This is to speed up the
  5200. second and subsequent passes. */
  5201. c = bfd_ardata (abfd)->symdef_count;
  5202. if (c == 0)
  5203. return true;
  5204. amt = c * sizeof (*included);
  5205. included = (unsigned char *) bfd_zmalloc (amt);
  5206. if (included == NULL)
  5207. return false;
  5208. symdefs = bfd_ardata (abfd)->symdefs;
  5209. bed = get_elf_backend_data (abfd);
  5210. archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup;
  5211. do
  5212. {
  5213. file_ptr last;
  5214. symindex i;
  5215. carsym *symdef;
  5216. carsym *symdefend;
  5217. loop = false;
  5218. last = -1;
  5219. symdef = symdefs;
  5220. symdefend = symdef + c;
  5221. for (i = 0; symdef < symdefend; symdef++, i++)
  5222. {
  5223. struct bfd_link_hash_entry *h;
  5224. bfd *element;
  5225. struct bfd_link_hash_entry *undefs_tail;
  5226. symindex mark;
  5227. if (included[i])
  5228. continue;
  5229. if (symdef->file_offset == last)
  5230. {
  5231. included[i] = true;
  5232. continue;
  5233. }
  5234. h = archive_symbol_lookup (abfd, info, symdef->name);
  5235. if (h == (struct bfd_link_hash_entry *) -1)
  5236. goto error_return;
  5237. if (h == NULL)
  5238. continue;
  5239. if (h->type == bfd_link_hash_undefined)
  5240. {
  5241. /* If the archive element has already been loaded then one
  5242. of the symbols defined by that element might have been
  5243. made undefined due to being in a discarded section. */
  5244. if (is_elf_hash_table (info->hash)
  5245. && ((struct elf_link_hash_entry *) h)->indx == -3)
  5246. continue;
  5247. }
  5248. else if (h->type == bfd_link_hash_common)
  5249. {
  5250. /* We currently have a common symbol. The archive map contains
  5251. a reference to this symbol, so we may want to include it. We
  5252. only want to include it however, if this archive element
  5253. contains a definition of the symbol, not just another common
  5254. declaration of it.
  5255. Unfortunately some archivers (including GNU ar) will put
  5256. declarations of common symbols into their archive maps, as
  5257. well as real definitions, so we cannot just go by the archive
  5258. map alone. Instead we must read in the element's symbol
  5259. table and check that to see what kind of symbol definition
  5260. this is. */
  5261. if (! elf_link_is_defined_archive_symbol (abfd, symdef))
  5262. continue;
  5263. }
  5264. else
  5265. {
  5266. if (h->type != bfd_link_hash_undefweak)
  5267. /* Symbol must be defined. Don't check it again. */
  5268. included[i] = true;
  5269. continue;
  5270. }
  5271. /* We need to include this archive member. */
  5272. element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
  5273. info);
  5274. if (element == NULL)
  5275. goto error_return;
  5276. if (! bfd_check_format (element, bfd_object))
  5277. goto error_return;
  5278. undefs_tail = info->hash->undefs_tail;
  5279. if (!(*info->callbacks
  5280. ->add_archive_element) (info, element, symdef->name, &element))
  5281. continue;
  5282. if (!bfd_link_add_symbols (element, info))
  5283. goto error_return;
  5284. /* If there are any new undefined symbols, we need to make
  5285. another pass through the archive in order to see whether
  5286. they can be defined. FIXME: This isn't perfect, because
  5287. common symbols wind up on undefs_tail and because an
  5288. undefined symbol which is defined later on in this pass
  5289. does not require another pass. This isn't a bug, but it
  5290. does make the code less efficient than it could be. */
  5291. if (undefs_tail != info->hash->undefs_tail)
  5292. loop = true;
  5293. /* Look backward to mark all symbols from this object file
  5294. which we have already seen in this pass. */
  5295. mark = i;
  5296. do
  5297. {
  5298. included[mark] = true;
  5299. if (mark == 0)
  5300. break;
  5301. --mark;
  5302. }
  5303. while (symdefs[mark].file_offset == symdef->file_offset);
  5304. /* We mark subsequent symbols from this object file as we go
  5305. on through the loop. */
  5306. last = symdef->file_offset;
  5307. }
  5308. }
  5309. while (loop);
  5310. free (included);
  5311. return true;
  5312. error_return:
  5313. free (included);
  5314. return false;
  5315. }
  5316. /* Given an ELF BFD, add symbols to the global hash table as
  5317. appropriate. */
  5318. bool
  5319. bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
  5320. {
  5321. switch (bfd_get_format (abfd))
  5322. {
  5323. case bfd_object:
  5324. return elf_link_add_object_symbols (abfd, info);
  5325. case bfd_archive:
  5326. return elf_link_add_archive_symbols (abfd, info);
  5327. default:
  5328. bfd_set_error (bfd_error_wrong_format);
  5329. return false;
  5330. }
  5331. }
  5332. struct hash_codes_info
  5333. {
  5334. unsigned long *hashcodes;
  5335. bool error;
  5336. };
  5337. /* This function will be called though elf_link_hash_traverse to store
  5338. all hash value of the exported symbols in an array. */
  5339. static bool
  5340. elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data)
  5341. {
  5342. struct hash_codes_info *inf = (struct hash_codes_info *) data;
  5343. const char *name;
  5344. unsigned long ha;
  5345. char *alc = NULL;
  5346. /* Ignore indirect symbols. These are added by the versioning code. */
  5347. if (h->dynindx == -1)
  5348. return true;
  5349. name = h->root.root.string;
  5350. if (h->versioned >= versioned)
  5351. {
  5352. char *p = strchr (name, ELF_VER_CHR);
  5353. if (p != NULL)
  5354. {
  5355. alc = (char *) bfd_malloc (p - name + 1);
  5356. if (alc == NULL)
  5357. {
  5358. inf->error = true;
  5359. return false;
  5360. }
  5361. memcpy (alc, name, p - name);
  5362. alc[p - name] = '\0';
  5363. name = alc;
  5364. }
  5365. }
  5366. /* Compute the hash value. */
  5367. ha = bfd_elf_hash (name);
  5368. /* Store the found hash value in the array given as the argument. */
  5369. *(inf->hashcodes)++ = ha;
  5370. /* And store it in the struct so that we can put it in the hash table
  5371. later. */
  5372. h->u.elf_hash_value = ha;
  5373. free (alc);
  5374. return true;
  5375. }
  5376. struct collect_gnu_hash_codes
  5377. {
  5378. bfd *output_bfd;
  5379. const struct elf_backend_data *bed;
  5380. unsigned long int nsyms;
  5381. unsigned long int maskbits;
  5382. unsigned long int *hashcodes;
  5383. unsigned long int *hashval;
  5384. unsigned long int *indx;
  5385. unsigned long int *counts;
  5386. bfd_vma *bitmask;
  5387. bfd_byte *contents;
  5388. bfd_size_type xlat;
  5389. long int min_dynindx;
  5390. unsigned long int bucketcount;
  5391. unsigned long int symindx;
  5392. long int local_indx;
  5393. long int shift1, shift2;
  5394. unsigned long int mask;
  5395. bool error;
  5396. };
  5397. /* This function will be called though elf_link_hash_traverse to store
  5398. all hash value of the exported symbols in an array. */
  5399. static bool
  5400. elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data)
  5401. {
  5402. struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
  5403. const char *name;
  5404. unsigned long ha;
  5405. char *alc = NULL;
  5406. /* Ignore indirect symbols. These are added by the versioning code. */
  5407. if (h->dynindx == -1)
  5408. return true;
  5409. /* Ignore also local symbols and undefined symbols. */
  5410. if (! (*s->bed->elf_hash_symbol) (h))
  5411. return true;
  5412. name = h->root.root.string;
  5413. if (h->versioned >= versioned)
  5414. {
  5415. char *p = strchr (name, ELF_VER_CHR);
  5416. if (p != NULL)
  5417. {
  5418. alc = (char *) bfd_malloc (p - name + 1);
  5419. if (alc == NULL)
  5420. {
  5421. s->error = true;
  5422. return false;
  5423. }
  5424. memcpy (alc, name, p - name);
  5425. alc[p - name] = '\0';
  5426. name = alc;
  5427. }
  5428. }
  5429. /* Compute the hash value. */
  5430. ha = bfd_elf_gnu_hash (name);
  5431. /* Store the found hash value in the array for compute_bucket_count,
  5432. and also for .dynsym reordering purposes. */
  5433. s->hashcodes[s->nsyms] = ha;
  5434. s->hashval[h->dynindx] = ha;
  5435. ++s->nsyms;
  5436. if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx)
  5437. s->min_dynindx = h->dynindx;
  5438. free (alc);
  5439. return true;
  5440. }
  5441. /* This function will be called though elf_link_hash_traverse to do
  5442. final dynamic symbol renumbering in case of .gnu.hash.
  5443. If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index
  5444. to the translation table. */
  5445. static bool
  5446. elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data)
  5447. {
  5448. struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data;
  5449. unsigned long int bucket;
  5450. unsigned long int val;
  5451. /* Ignore indirect symbols. */
  5452. if (h->dynindx == -1)
  5453. return true;
  5454. /* Ignore also local symbols and undefined symbols. */
  5455. if (! (*s->bed->elf_hash_symbol) (h))
  5456. {
  5457. if (h->dynindx >= s->min_dynindx)
  5458. {
  5459. if (s->bed->record_xhash_symbol != NULL)
  5460. {
  5461. (*s->bed->record_xhash_symbol) (h, 0);
  5462. s->local_indx++;
  5463. }
  5464. else
  5465. h->dynindx = s->local_indx++;
  5466. }
  5467. return true;
  5468. }
  5469. bucket = s->hashval[h->dynindx] % s->bucketcount;
  5470. val = (s->hashval[h->dynindx] >> s->shift1)
  5471. & ((s->maskbits >> s->shift1) - 1);
  5472. s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask);
  5473. s->bitmask[val]
  5474. |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask);
  5475. val = s->hashval[h->dynindx] & ~(unsigned long int) 1;
  5476. if (s->counts[bucket] == 1)
  5477. /* Last element terminates the chain. */
  5478. val |= 1;
  5479. bfd_put_32 (s->output_bfd, val,
  5480. s->contents + (s->indx[bucket] - s->symindx) * 4);
  5481. --s->counts[bucket];
  5482. if (s->bed->record_xhash_symbol != NULL)
  5483. {
  5484. bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4;
  5485. (*s->bed->record_xhash_symbol) (h, xlat_loc);
  5486. }
  5487. else
  5488. h->dynindx = s->indx[bucket]++;
  5489. return true;
  5490. }
  5491. /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
  5492. bool
  5493. _bfd_elf_hash_symbol (struct elf_link_hash_entry *h)
  5494. {
  5495. return !(h->forced_local
  5496. || h->root.type == bfd_link_hash_undefined
  5497. || h->root.type == bfd_link_hash_undefweak
  5498. || ((h->root.type == bfd_link_hash_defined
  5499. || h->root.type == bfd_link_hash_defweak)
  5500. && h->root.u.def.section->output_section == NULL));
  5501. }
  5502. /* Array used to determine the number of hash table buckets to use
  5503. based on the number of symbols there are. If there are fewer than
  5504. 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
  5505. fewer than 37 we use 17 buckets, and so forth. We never use more
  5506. than 32771 buckets. */
  5507. static const size_t elf_buckets[] =
  5508. {
  5509. 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
  5510. 16411, 32771, 0
  5511. };
  5512. /* Compute bucket count for hashing table. We do not use a static set
  5513. of possible tables sizes anymore. Instead we determine for all
  5514. possible reasonable sizes of the table the outcome (i.e., the
  5515. number of collisions etc) and choose the best solution. The
  5516. weighting functions are not too simple to allow the table to grow
  5517. without bounds. Instead one of the weighting factors is the size.
  5518. Therefore the result is always a good payoff between few collisions
  5519. (= short chain lengths) and table size. */
  5520. static size_t
  5521. compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  5522. unsigned long int *hashcodes ATTRIBUTE_UNUSED,
  5523. unsigned long int nsyms,
  5524. int gnu_hash)
  5525. {
  5526. size_t best_size = 0;
  5527. unsigned long int i;
  5528. /* We have a problem here. The following code to optimize the table
  5529. size requires an integer type with more the 32 bits. If
  5530. BFD_HOST_U_64_BIT is set we know about such a type. */
  5531. #ifdef BFD_HOST_U_64_BIT
  5532. if (info->optimize)
  5533. {
  5534. size_t minsize;
  5535. size_t maxsize;
  5536. BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
  5537. bfd *dynobj = elf_hash_table (info)->dynobj;
  5538. size_t dynsymcount = elf_hash_table (info)->dynsymcount;
  5539. const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
  5540. unsigned long int *counts;
  5541. bfd_size_type amt;
  5542. unsigned int no_improvement_count = 0;
  5543. /* Possible optimization parameters: if we have NSYMS symbols we say
  5544. that the hashing table must at least have NSYMS/4 and at most
  5545. 2*NSYMS buckets. */
  5546. minsize = nsyms / 4;
  5547. if (minsize == 0)
  5548. minsize = 1;
  5549. best_size = maxsize = nsyms * 2;
  5550. if (gnu_hash)
  5551. {
  5552. if (minsize < 2)
  5553. minsize = 2;
  5554. if ((best_size & 31) == 0)
  5555. ++best_size;
  5556. }
  5557. /* Create array where we count the collisions in. We must use bfd_malloc
  5558. since the size could be large. */
  5559. amt = maxsize;
  5560. amt *= sizeof (unsigned long int);
  5561. counts = (unsigned long int *) bfd_malloc (amt);
  5562. if (counts == NULL)
  5563. return 0;
  5564. /* Compute the "optimal" size for the hash table. The criteria is a
  5565. minimal chain length. The minor criteria is (of course) the size
  5566. of the table. */
  5567. for (i = minsize; i < maxsize; ++i)
  5568. {
  5569. /* Walk through the array of hashcodes and count the collisions. */
  5570. BFD_HOST_U_64_BIT max;
  5571. unsigned long int j;
  5572. unsigned long int fact;
  5573. if (gnu_hash && (i & 31) == 0)
  5574. continue;
  5575. memset (counts, '\0', i * sizeof (unsigned long int));
  5576. /* Determine how often each hash bucket is used. */
  5577. for (j = 0; j < nsyms; ++j)
  5578. ++counts[hashcodes[j] % i];
  5579. /* For the weight function we need some information about the
  5580. pagesize on the target. This is information need not be 100%
  5581. accurate. Since this information is not available (so far) we
  5582. define it here to a reasonable default value. If it is crucial
  5583. to have a better value some day simply define this value. */
  5584. # ifndef BFD_TARGET_PAGESIZE
  5585. # define BFD_TARGET_PAGESIZE (4096)
  5586. # endif
  5587. /* We in any case need 2 + DYNSYMCOUNT entries for the size values
  5588. and the chains. */
  5589. max = (2 + dynsymcount) * bed->s->sizeof_hash_entry;
  5590. # if 1
  5591. /* Variant 1: optimize for short chains. We add the squares
  5592. of all the chain lengths (which favors many small chain
  5593. over a few long chains). */
  5594. for (j = 0; j < i; ++j)
  5595. max += counts[j] * counts[j];
  5596. /* This adds penalties for the overall size of the table. */
  5597. fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
  5598. max *= fact * fact;
  5599. # else
  5600. /* Variant 2: Optimize a lot more for small table. Here we
  5601. also add squares of the size but we also add penalties for
  5602. empty slots (the +1 term). */
  5603. for (j = 0; j < i; ++j)
  5604. max += (1 + counts[j]) * (1 + counts[j]);
  5605. /* The overall size of the table is considered, but not as
  5606. strong as in variant 1, where it is squared. */
  5607. fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1;
  5608. max *= fact;
  5609. # endif
  5610. /* Compare with current best results. */
  5611. if (max < best_chlen)
  5612. {
  5613. best_chlen = max;
  5614. best_size = i;
  5615. no_improvement_count = 0;
  5616. }
  5617. /* PR 11843: Avoid futile long searches for the best bucket size
  5618. when there are a large number of symbols. */
  5619. else if (++no_improvement_count == 100)
  5620. break;
  5621. }
  5622. free (counts);
  5623. }
  5624. else
  5625. #endif /* defined (BFD_HOST_U_64_BIT) */
  5626. {
  5627. /* This is the fallback solution if no 64bit type is available or if we
  5628. are not supposed to spend much time on optimizations. We select the
  5629. bucket count using a fixed set of numbers. */
  5630. for (i = 0; elf_buckets[i] != 0; i++)
  5631. {
  5632. best_size = elf_buckets[i];
  5633. if (nsyms < elf_buckets[i + 1])
  5634. break;
  5635. }
  5636. if (gnu_hash && best_size < 2)
  5637. best_size = 2;
  5638. }
  5639. return best_size;
  5640. }
  5641. /* Size any SHT_GROUP section for ld -r. */
  5642. bool
  5643. _bfd_elf_size_group_sections (struct bfd_link_info *info)
  5644. {
  5645. bfd *ibfd;
  5646. asection *s;
  5647. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  5648. if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour
  5649. && (s = ibfd->sections) != NULL
  5650. && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
  5651. && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr))
  5652. return false;
  5653. return true;
  5654. }
  5655. /* Set a default stack segment size. The value in INFO wins. If it
  5656. is unset, LEGACY_SYMBOL's value is used, and if that symbol is
  5657. undefined it is initialized. */
  5658. bool
  5659. bfd_elf_stack_segment_size (bfd *output_bfd,
  5660. struct bfd_link_info *info,
  5661. const char *legacy_symbol,
  5662. bfd_vma default_size)
  5663. {
  5664. struct elf_link_hash_entry *h = NULL;
  5665. /* Look for legacy symbol. */
  5666. if (legacy_symbol)
  5667. h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol,
  5668. false, false, false);
  5669. if (h && (h->root.type == bfd_link_hash_defined
  5670. || h->root.type == bfd_link_hash_defweak)
  5671. && h->def_regular
  5672. && (h->type == STT_NOTYPE || h->type == STT_OBJECT))
  5673. {
  5674. /* The symbol has no type if specified on the command line. */
  5675. h->type = STT_OBJECT;
  5676. if (info->stacksize)
  5677. /* xgettext:c-format */
  5678. _bfd_error_handler (_("%pB: stack size specified and %s set"),
  5679. output_bfd, legacy_symbol);
  5680. else if (h->root.u.def.section != bfd_abs_section_ptr)
  5681. /* xgettext:c-format */
  5682. _bfd_error_handler (_("%pB: %s not absolute"),
  5683. output_bfd, legacy_symbol);
  5684. else
  5685. info->stacksize = h->root.u.def.value;
  5686. }
  5687. if (!info->stacksize)
  5688. /* If the user didn't set a size, or explicitly inhibit the
  5689. size, set it now. */
  5690. info->stacksize = default_size;
  5691. /* Provide the legacy symbol, if it is referenced. */
  5692. if (h && (h->root.type == bfd_link_hash_undefined
  5693. || h->root.type == bfd_link_hash_undefweak))
  5694. {
  5695. struct bfd_link_hash_entry *bh = NULL;
  5696. if (!(_bfd_generic_link_add_one_symbol
  5697. (info, output_bfd, legacy_symbol,
  5698. BSF_GLOBAL, bfd_abs_section_ptr,
  5699. info->stacksize >= 0 ? info->stacksize : 0,
  5700. NULL, false, get_elf_backend_data (output_bfd)->collect, &bh)))
  5701. return false;
  5702. h = (struct elf_link_hash_entry *) bh;
  5703. h->def_regular = 1;
  5704. h->type = STT_OBJECT;
  5705. }
  5706. return true;
  5707. }
  5708. /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */
  5709. struct elf_gc_sweep_symbol_info
  5710. {
  5711. struct bfd_link_info *info;
  5712. void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *,
  5713. bool);
  5714. };
  5715. static bool
  5716. elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data)
  5717. {
  5718. if (!h->mark
  5719. && (((h->root.type == bfd_link_hash_defined
  5720. || h->root.type == bfd_link_hash_defweak)
  5721. && !((h->def_regular || ELF_COMMON_DEF_P (h))
  5722. && h->root.u.def.section->gc_mark))
  5723. || h->root.type == bfd_link_hash_undefined
  5724. || h->root.type == bfd_link_hash_undefweak))
  5725. {
  5726. struct elf_gc_sweep_symbol_info *inf;
  5727. inf = (struct elf_gc_sweep_symbol_info *) data;
  5728. (*inf->hide_symbol) (inf->info, h, true);
  5729. h->def_regular = 0;
  5730. h->ref_regular = 0;
  5731. h->ref_regular_nonweak = 0;
  5732. }
  5733. return true;
  5734. }
  5735. /* Set up the sizes and contents of the ELF dynamic sections. This is
  5736. called by the ELF linker emulation before_allocation routine. We
  5737. must set the sizes of the sections before the linker sets the
  5738. addresses of the various sections. */
  5739. bool
  5740. bfd_elf_size_dynamic_sections (bfd *output_bfd,
  5741. const char *soname,
  5742. const char *rpath,
  5743. const char *filter_shlib,
  5744. const char *audit,
  5745. const char *depaudit,
  5746. const char * const *auxiliary_filters,
  5747. struct bfd_link_info *info,
  5748. asection **sinterpptr)
  5749. {
  5750. bfd *dynobj;
  5751. const struct elf_backend_data *bed;
  5752. *sinterpptr = NULL;
  5753. if (!is_elf_hash_table (info->hash))
  5754. return true;
  5755. /* Any syms created from now on start with -1 in
  5756. got.refcount/offset and plt.refcount/offset. */
  5757. elf_hash_table (info)->init_got_refcount
  5758. = elf_hash_table (info)->init_got_offset;
  5759. elf_hash_table (info)->init_plt_refcount
  5760. = elf_hash_table (info)->init_plt_offset;
  5761. bed = get_elf_backend_data (output_bfd);
  5762. /* The backend may have to create some sections regardless of whether
  5763. we're dynamic or not. */
  5764. if (bed->elf_backend_always_size_sections
  5765. && ! (*bed->elf_backend_always_size_sections) (output_bfd, info))
  5766. return false;
  5767. dynobj = elf_hash_table (info)->dynobj;
  5768. if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
  5769. {
  5770. struct bfd_elf_version_tree *verdefs;
  5771. struct elf_info_failed asvinfo;
  5772. struct bfd_elf_version_tree *t;
  5773. struct bfd_elf_version_expr *d;
  5774. asection *s;
  5775. size_t soname_indx;
  5776. /* If we are supposed to export all symbols into the dynamic symbol
  5777. table (this is not the normal case), then do so. */
  5778. if (info->export_dynamic
  5779. || (bfd_link_executable (info) && info->dynamic))
  5780. {
  5781. struct elf_info_failed eif;
  5782. eif.info = info;
  5783. eif.failed = false;
  5784. elf_link_hash_traverse (elf_hash_table (info),
  5785. _bfd_elf_export_symbol,
  5786. &eif);
  5787. if (eif.failed)
  5788. return false;
  5789. }
  5790. if (soname != NULL)
  5791. {
  5792. soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  5793. soname, true);
  5794. if (soname_indx == (size_t) -1
  5795. || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx))
  5796. return false;
  5797. }
  5798. else
  5799. soname_indx = (size_t) -1;
  5800. /* Make all global versions with definition. */
  5801. for (t = info->version_info; t != NULL; t = t->next)
  5802. for (d = t->globals.list; d != NULL; d = d->next)
  5803. if (!d->symver && d->literal)
  5804. {
  5805. const char *verstr, *name;
  5806. size_t namelen, verlen, newlen;
  5807. char *newname, *p, leading_char;
  5808. struct elf_link_hash_entry *newh;
  5809. leading_char = bfd_get_symbol_leading_char (output_bfd);
  5810. name = d->pattern;
  5811. namelen = strlen (name) + (leading_char != '\0');
  5812. verstr = t->name;
  5813. verlen = strlen (verstr);
  5814. newlen = namelen + verlen + 3;
  5815. newname = (char *) bfd_malloc (newlen);
  5816. if (newname == NULL)
  5817. return false;
  5818. newname[0] = leading_char;
  5819. memcpy (newname + (leading_char != '\0'), name, namelen);
  5820. /* Check the hidden versioned definition. */
  5821. p = newname + namelen;
  5822. *p++ = ELF_VER_CHR;
  5823. memcpy (p, verstr, verlen + 1);
  5824. newh = elf_link_hash_lookup (elf_hash_table (info),
  5825. newname, false, false,
  5826. false);
  5827. if (newh == NULL
  5828. || (newh->root.type != bfd_link_hash_defined
  5829. && newh->root.type != bfd_link_hash_defweak))
  5830. {
  5831. /* Check the default versioned definition. */
  5832. *p++ = ELF_VER_CHR;
  5833. memcpy (p, verstr, verlen + 1);
  5834. newh = elf_link_hash_lookup (elf_hash_table (info),
  5835. newname, false, false,
  5836. false);
  5837. }
  5838. free (newname);
  5839. /* Mark this version if there is a definition and it is
  5840. not defined in a shared object. */
  5841. if (newh != NULL
  5842. && !newh->def_dynamic
  5843. && (newh->root.type == bfd_link_hash_defined
  5844. || newh->root.type == bfd_link_hash_defweak))
  5845. d->symver = 1;
  5846. }
  5847. /* Attach all the symbols to their version information. */
  5848. asvinfo.info = info;
  5849. asvinfo.failed = false;
  5850. elf_link_hash_traverse (elf_hash_table (info),
  5851. _bfd_elf_link_assign_sym_version,
  5852. &asvinfo);
  5853. if (asvinfo.failed)
  5854. return false;
  5855. if (!info->allow_undefined_version)
  5856. {
  5857. /* Check if all global versions have a definition. */
  5858. bool all_defined = true;
  5859. for (t = info->version_info; t != NULL; t = t->next)
  5860. for (d = t->globals.list; d != NULL; d = d->next)
  5861. if (d->literal && !d->symver && !d->script)
  5862. {
  5863. _bfd_error_handler
  5864. (_("%s: undefined version: %s"),
  5865. d->pattern, t->name);
  5866. all_defined = false;
  5867. }
  5868. if (!all_defined)
  5869. {
  5870. bfd_set_error (bfd_error_bad_value);
  5871. return false;
  5872. }
  5873. }
  5874. /* Set up the version definition section. */
  5875. s = bfd_get_linker_section (dynobj, ".gnu.version_d");
  5876. BFD_ASSERT (s != NULL);
  5877. /* We may have created additional version definitions if we are
  5878. just linking a regular application. */
  5879. verdefs = info->version_info;
  5880. /* Skip anonymous version tag. */
  5881. if (verdefs != NULL && verdefs->vernum == 0)
  5882. verdefs = verdefs->next;
  5883. if (verdefs == NULL && !info->create_default_symver)
  5884. s->flags |= SEC_EXCLUDE;
  5885. else
  5886. {
  5887. unsigned int cdefs;
  5888. bfd_size_type size;
  5889. bfd_byte *p;
  5890. Elf_Internal_Verdef def;
  5891. Elf_Internal_Verdaux defaux;
  5892. struct bfd_link_hash_entry *bh;
  5893. struct elf_link_hash_entry *h;
  5894. const char *name;
  5895. cdefs = 0;
  5896. size = 0;
  5897. /* Make space for the base version. */
  5898. size += sizeof (Elf_External_Verdef);
  5899. size += sizeof (Elf_External_Verdaux);
  5900. ++cdefs;
  5901. /* Make space for the default version. */
  5902. if (info->create_default_symver)
  5903. {
  5904. size += sizeof (Elf_External_Verdef);
  5905. ++cdefs;
  5906. }
  5907. for (t = verdefs; t != NULL; t = t->next)
  5908. {
  5909. struct bfd_elf_version_deps *n;
  5910. /* Don't emit base version twice. */
  5911. if (t->vernum == 0)
  5912. continue;
  5913. size += sizeof (Elf_External_Verdef);
  5914. size += sizeof (Elf_External_Verdaux);
  5915. ++cdefs;
  5916. for (n = t->deps; n != NULL; n = n->next)
  5917. size += sizeof (Elf_External_Verdaux);
  5918. }
  5919. s->size = size;
  5920. s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
  5921. if (s->contents == NULL && s->size != 0)
  5922. return false;
  5923. /* Fill in the version definition section. */
  5924. p = s->contents;
  5925. def.vd_version = VER_DEF_CURRENT;
  5926. def.vd_flags = VER_FLG_BASE;
  5927. def.vd_ndx = 1;
  5928. def.vd_cnt = 1;
  5929. if (info->create_default_symver)
  5930. {
  5931. def.vd_aux = 2 * sizeof (Elf_External_Verdef);
  5932. def.vd_next = sizeof (Elf_External_Verdef);
  5933. }
  5934. else
  5935. {
  5936. def.vd_aux = sizeof (Elf_External_Verdef);
  5937. def.vd_next = (sizeof (Elf_External_Verdef)
  5938. + sizeof (Elf_External_Verdaux));
  5939. }
  5940. if (soname_indx != (size_t) -1)
  5941. {
  5942. _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
  5943. soname_indx);
  5944. def.vd_hash = bfd_elf_hash (soname);
  5945. defaux.vda_name = soname_indx;
  5946. name = soname;
  5947. }
  5948. else
  5949. {
  5950. size_t indx;
  5951. name = lbasename (bfd_get_filename (output_bfd));
  5952. def.vd_hash = bfd_elf_hash (name);
  5953. indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  5954. name, false);
  5955. if (indx == (size_t) -1)
  5956. return false;
  5957. defaux.vda_name = indx;
  5958. }
  5959. defaux.vda_next = 0;
  5960. _bfd_elf_swap_verdef_out (output_bfd, &def,
  5961. (Elf_External_Verdef *) p);
  5962. p += sizeof (Elf_External_Verdef);
  5963. if (info->create_default_symver)
  5964. {
  5965. /* Add a symbol representing this version. */
  5966. bh = NULL;
  5967. if (! (_bfd_generic_link_add_one_symbol
  5968. (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr,
  5969. 0, NULL, false,
  5970. get_elf_backend_data (dynobj)->collect, &bh)))
  5971. return false;
  5972. h = (struct elf_link_hash_entry *) bh;
  5973. h->non_elf = 0;
  5974. h->def_regular = 1;
  5975. h->type = STT_OBJECT;
  5976. h->verinfo.vertree = NULL;
  5977. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  5978. return false;
  5979. /* Create a duplicate of the base version with the same
  5980. aux block, but different flags. */
  5981. def.vd_flags = 0;
  5982. def.vd_ndx = 2;
  5983. def.vd_aux = sizeof (Elf_External_Verdef);
  5984. if (verdefs)
  5985. def.vd_next = (sizeof (Elf_External_Verdef)
  5986. + sizeof (Elf_External_Verdaux));
  5987. else
  5988. def.vd_next = 0;
  5989. _bfd_elf_swap_verdef_out (output_bfd, &def,
  5990. (Elf_External_Verdef *) p);
  5991. p += sizeof (Elf_External_Verdef);
  5992. }
  5993. _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
  5994. (Elf_External_Verdaux *) p);
  5995. p += sizeof (Elf_External_Verdaux);
  5996. for (t = verdefs; t != NULL; t = t->next)
  5997. {
  5998. unsigned int cdeps;
  5999. struct bfd_elf_version_deps *n;
  6000. /* Don't emit the base version twice. */
  6001. if (t->vernum == 0)
  6002. continue;
  6003. cdeps = 0;
  6004. for (n = t->deps; n != NULL; n = n->next)
  6005. ++cdeps;
  6006. /* Add a symbol representing this version. */
  6007. bh = NULL;
  6008. if (! (_bfd_generic_link_add_one_symbol
  6009. (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr,
  6010. 0, NULL, false,
  6011. get_elf_backend_data (dynobj)->collect, &bh)))
  6012. return false;
  6013. h = (struct elf_link_hash_entry *) bh;
  6014. h->non_elf = 0;
  6015. h->def_regular = 1;
  6016. h->type = STT_OBJECT;
  6017. h->verinfo.vertree = t;
  6018. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  6019. return false;
  6020. def.vd_version = VER_DEF_CURRENT;
  6021. def.vd_flags = 0;
  6022. if (t->globals.list == NULL
  6023. && t->locals.list == NULL
  6024. && ! t->used)
  6025. def.vd_flags |= VER_FLG_WEAK;
  6026. def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1);
  6027. def.vd_cnt = cdeps + 1;
  6028. def.vd_hash = bfd_elf_hash (t->name);
  6029. def.vd_aux = sizeof (Elf_External_Verdef);
  6030. def.vd_next = 0;
  6031. /* If a basever node is next, it *must* be the last node in
  6032. the chain, otherwise Verdef construction breaks. */
  6033. if (t->next != NULL && t->next->vernum == 0)
  6034. BFD_ASSERT (t->next->next == NULL);
  6035. if (t->next != NULL && t->next->vernum != 0)
  6036. def.vd_next = (sizeof (Elf_External_Verdef)
  6037. + (cdeps + 1) * sizeof (Elf_External_Verdaux));
  6038. _bfd_elf_swap_verdef_out (output_bfd, &def,
  6039. (Elf_External_Verdef *) p);
  6040. p += sizeof (Elf_External_Verdef);
  6041. defaux.vda_name = h->dynstr_index;
  6042. _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
  6043. h->dynstr_index);
  6044. defaux.vda_next = 0;
  6045. if (t->deps != NULL)
  6046. defaux.vda_next = sizeof (Elf_External_Verdaux);
  6047. t->name_indx = defaux.vda_name;
  6048. _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
  6049. (Elf_External_Verdaux *) p);
  6050. p += sizeof (Elf_External_Verdaux);
  6051. for (n = t->deps; n != NULL; n = n->next)
  6052. {
  6053. if (n->version_needed == NULL)
  6054. {
  6055. /* This can happen if there was an error in the
  6056. version script. */
  6057. defaux.vda_name = 0;
  6058. }
  6059. else
  6060. {
  6061. defaux.vda_name = n->version_needed->name_indx;
  6062. _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr,
  6063. defaux.vda_name);
  6064. }
  6065. if (n->next == NULL)
  6066. defaux.vda_next = 0;
  6067. else
  6068. defaux.vda_next = sizeof (Elf_External_Verdaux);
  6069. _bfd_elf_swap_verdaux_out (output_bfd, &defaux,
  6070. (Elf_External_Verdaux *) p);
  6071. p += sizeof (Elf_External_Verdaux);
  6072. }
  6073. }
  6074. elf_tdata (output_bfd)->cverdefs = cdefs;
  6075. }
  6076. }
  6077. if (info->gc_sections && bed->can_gc_sections)
  6078. {
  6079. struct elf_gc_sweep_symbol_info sweep_info;
  6080. /* Remove the symbols that were in the swept sections from the
  6081. dynamic symbol table. */
  6082. sweep_info.info = info;
  6083. sweep_info.hide_symbol = bed->elf_backend_hide_symbol;
  6084. elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol,
  6085. &sweep_info);
  6086. }
  6087. if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
  6088. {
  6089. asection *s;
  6090. struct elf_find_verdep_info sinfo;
  6091. /* Work out the size of the version reference section. */
  6092. s = bfd_get_linker_section (dynobj, ".gnu.version_r");
  6093. BFD_ASSERT (s != NULL);
  6094. sinfo.info = info;
  6095. sinfo.vers = elf_tdata (output_bfd)->cverdefs;
  6096. if (sinfo.vers == 0)
  6097. sinfo.vers = 1;
  6098. sinfo.failed = false;
  6099. elf_link_hash_traverse (elf_hash_table (info),
  6100. _bfd_elf_link_find_version_dependencies,
  6101. &sinfo);
  6102. if (sinfo.failed)
  6103. return false;
  6104. if (info->enable_dt_relr)
  6105. {
  6106. elf_link_add_dt_relr_dependency (&sinfo);
  6107. if (sinfo.failed)
  6108. return false;
  6109. }
  6110. if (elf_tdata (output_bfd)->verref == NULL)
  6111. s->flags |= SEC_EXCLUDE;
  6112. else
  6113. {
  6114. Elf_Internal_Verneed *vn;
  6115. unsigned int size;
  6116. unsigned int crefs;
  6117. bfd_byte *p;
  6118. /* Build the version dependency section. */
  6119. size = 0;
  6120. crefs = 0;
  6121. for (vn = elf_tdata (output_bfd)->verref;
  6122. vn != NULL;
  6123. vn = vn->vn_nextref)
  6124. {
  6125. Elf_Internal_Vernaux *a;
  6126. size += sizeof (Elf_External_Verneed);
  6127. ++crefs;
  6128. for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
  6129. size += sizeof (Elf_External_Vernaux);
  6130. }
  6131. s->size = size;
  6132. s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
  6133. if (s->contents == NULL)
  6134. return false;
  6135. p = s->contents;
  6136. for (vn = elf_tdata (output_bfd)->verref;
  6137. vn != NULL;
  6138. vn = vn->vn_nextref)
  6139. {
  6140. unsigned int caux;
  6141. Elf_Internal_Vernaux *a;
  6142. size_t indx;
  6143. caux = 0;
  6144. for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
  6145. ++caux;
  6146. vn->vn_version = VER_NEED_CURRENT;
  6147. vn->vn_cnt = caux;
  6148. indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  6149. elf_dt_name (vn->vn_bfd) != NULL
  6150. ? elf_dt_name (vn->vn_bfd)
  6151. : lbasename (bfd_get_filename
  6152. (vn->vn_bfd)),
  6153. false);
  6154. if (indx == (size_t) -1)
  6155. return false;
  6156. vn->vn_file = indx;
  6157. vn->vn_aux = sizeof (Elf_External_Verneed);
  6158. if (vn->vn_nextref == NULL)
  6159. vn->vn_next = 0;
  6160. else
  6161. vn->vn_next = (sizeof (Elf_External_Verneed)
  6162. + caux * sizeof (Elf_External_Vernaux));
  6163. _bfd_elf_swap_verneed_out (output_bfd, vn,
  6164. (Elf_External_Verneed *) p);
  6165. p += sizeof (Elf_External_Verneed);
  6166. for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr)
  6167. {
  6168. a->vna_hash = bfd_elf_hash (a->vna_nodename);
  6169. indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  6170. a->vna_nodename, false);
  6171. if (indx == (size_t) -1)
  6172. return false;
  6173. a->vna_name = indx;
  6174. if (a->vna_nextptr == NULL)
  6175. a->vna_next = 0;
  6176. else
  6177. a->vna_next = sizeof (Elf_External_Vernaux);
  6178. _bfd_elf_swap_vernaux_out (output_bfd, a,
  6179. (Elf_External_Vernaux *) p);
  6180. p += sizeof (Elf_External_Vernaux);
  6181. }
  6182. }
  6183. elf_tdata (output_bfd)->cverrefs = crefs;
  6184. }
  6185. }
  6186. if (bfd_link_relocatable (info)
  6187. && !_bfd_elf_size_group_sections (info))
  6188. return false;
  6189. /* Determine any GNU_STACK segment requirements, after the backend
  6190. has had a chance to set a default segment size. */
  6191. if (info->execstack)
  6192. elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X;
  6193. else if (info->noexecstack)
  6194. elf_stack_flags (output_bfd) = PF_R | PF_W;
  6195. else
  6196. {
  6197. bfd *inputobj;
  6198. asection *notesec = NULL;
  6199. int exec = 0;
  6200. for (inputobj = info->input_bfds;
  6201. inputobj;
  6202. inputobj = inputobj->link.next)
  6203. {
  6204. asection *s;
  6205. if (inputobj->flags
  6206. & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED))
  6207. continue;
  6208. s = inputobj->sections;
  6209. if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
  6210. continue;
  6211. s = bfd_get_section_by_name (inputobj, ".note.GNU-stack");
  6212. if (s)
  6213. {
  6214. if (s->flags & SEC_CODE)
  6215. exec = PF_X;
  6216. notesec = s;
  6217. }
  6218. else if (bed->default_execstack)
  6219. exec = PF_X;
  6220. }
  6221. if (notesec || info->stacksize > 0)
  6222. elf_stack_flags (output_bfd) = PF_R | PF_W | exec;
  6223. if (notesec && exec && bfd_link_relocatable (info)
  6224. && notesec->output_section != bfd_abs_section_ptr)
  6225. notesec->output_section->flags |= SEC_CODE;
  6226. }
  6227. if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
  6228. {
  6229. struct elf_info_failed eif;
  6230. struct elf_link_hash_entry *h;
  6231. asection *dynstr;
  6232. asection *s;
  6233. *sinterpptr = bfd_get_linker_section (dynobj, ".interp");
  6234. BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp);
  6235. if (info->symbolic)
  6236. {
  6237. if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0))
  6238. return false;
  6239. info->flags |= DF_SYMBOLIC;
  6240. }
  6241. if (rpath != NULL)
  6242. {
  6243. size_t indx;
  6244. bfd_vma tag;
  6245. indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath,
  6246. true);
  6247. if (indx == (size_t) -1)
  6248. return false;
  6249. tag = info->new_dtags ? DT_RUNPATH : DT_RPATH;
  6250. if (!_bfd_elf_add_dynamic_entry (info, tag, indx))
  6251. return false;
  6252. }
  6253. if (filter_shlib != NULL)
  6254. {
  6255. size_t indx;
  6256. indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  6257. filter_shlib, true);
  6258. if (indx == (size_t) -1
  6259. || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx))
  6260. return false;
  6261. }
  6262. if (auxiliary_filters != NULL)
  6263. {
  6264. const char * const *p;
  6265. for (p = auxiliary_filters; *p != NULL; p++)
  6266. {
  6267. size_t indx;
  6268. indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr,
  6269. *p, true);
  6270. if (indx == (size_t) -1
  6271. || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx))
  6272. return false;
  6273. }
  6274. }
  6275. if (audit != NULL)
  6276. {
  6277. size_t indx;
  6278. indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit,
  6279. true);
  6280. if (indx == (size_t) -1
  6281. || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx))
  6282. return false;
  6283. }
  6284. if (depaudit != NULL)
  6285. {
  6286. size_t indx;
  6287. indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit,
  6288. true);
  6289. if (indx == (size_t) -1
  6290. || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx))
  6291. return false;
  6292. }
  6293. eif.info = info;
  6294. eif.failed = false;
  6295. /* Find all symbols which were defined in a dynamic object and make
  6296. the backend pick a reasonable value for them. */
  6297. elf_link_hash_traverse (elf_hash_table (info),
  6298. _bfd_elf_adjust_dynamic_symbol,
  6299. &eif);
  6300. if (eif.failed)
  6301. return false;
  6302. /* Add some entries to the .dynamic section. We fill in some of the
  6303. values later, in bfd_elf_final_link, but we must add the entries
  6304. now so that we know the final size of the .dynamic section. */
  6305. /* If there are initialization and/or finalization functions to
  6306. call then add the corresponding DT_INIT/DT_FINI entries. */
  6307. h = (info->init_function
  6308. ? elf_link_hash_lookup (elf_hash_table (info),
  6309. info->init_function, false,
  6310. false, false)
  6311. : NULL);
  6312. if (h != NULL
  6313. && (h->ref_regular
  6314. || h->def_regular))
  6315. {
  6316. if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
  6317. return false;
  6318. }
  6319. h = (info->fini_function
  6320. ? elf_link_hash_lookup (elf_hash_table (info),
  6321. info->fini_function, false,
  6322. false, false)
  6323. : NULL);
  6324. if (h != NULL
  6325. && (h->ref_regular
  6326. || h->def_regular))
  6327. {
  6328. if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
  6329. return false;
  6330. }
  6331. s = bfd_get_section_by_name (output_bfd, ".preinit_array");
  6332. if (s != NULL && s->linker_has_input)
  6333. {
  6334. /* DT_PREINIT_ARRAY is not allowed in shared library. */
  6335. if (! bfd_link_executable (info))
  6336. {
  6337. bfd *sub;
  6338. asection *o;
  6339. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  6340. if (bfd_get_flavour (sub) == bfd_target_elf_flavour
  6341. && (o = sub->sections) != NULL
  6342. && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS)
  6343. for (o = sub->sections; o != NULL; o = o->next)
  6344. if (elf_section_data (o)->this_hdr.sh_type
  6345. == SHT_PREINIT_ARRAY)
  6346. {
  6347. _bfd_error_handler
  6348. (_("%pB: .preinit_array section is not allowed in DSO"),
  6349. sub);
  6350. break;
  6351. }
  6352. bfd_set_error (bfd_error_nonrepresentable_section);
  6353. return false;
  6354. }
  6355. if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0)
  6356. || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0))
  6357. return false;
  6358. }
  6359. s = bfd_get_section_by_name (output_bfd, ".init_array");
  6360. if (s != NULL && s->linker_has_input)
  6361. {
  6362. if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0)
  6363. || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0))
  6364. return false;
  6365. }
  6366. s = bfd_get_section_by_name (output_bfd, ".fini_array");
  6367. if (s != NULL && s->linker_has_input)
  6368. {
  6369. if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0)
  6370. || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0))
  6371. return false;
  6372. }
  6373. dynstr = bfd_get_linker_section (dynobj, ".dynstr");
  6374. /* If .dynstr is excluded from the link, we don't want any of
  6375. these tags. Strictly, we should be checking each section
  6376. individually; This quick check covers for the case where
  6377. someone does a /DISCARD/ : { *(*) }. */
  6378. if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr)
  6379. {
  6380. bfd_size_type strsize;
  6381. strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
  6382. if ((info->emit_hash
  6383. && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0))
  6384. || (info->emit_gnu_hash
  6385. && (bed->record_xhash_symbol == NULL
  6386. && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)))
  6387. || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0)
  6388. || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0)
  6389. || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize)
  6390. || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT,
  6391. bed->s->sizeof_sym)
  6392. || (info->gnu_flags_1
  6393. && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1,
  6394. info->gnu_flags_1)))
  6395. return false;
  6396. }
  6397. }
  6398. if (! _bfd_elf_maybe_strip_eh_frame_hdr (info))
  6399. return false;
  6400. /* The backend must work out the sizes of all the other dynamic
  6401. sections. */
  6402. if (dynobj != NULL
  6403. && bed->elf_backend_size_dynamic_sections != NULL
  6404. && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info))
  6405. return false;
  6406. if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created)
  6407. {
  6408. if (elf_tdata (output_bfd)->cverdefs)
  6409. {
  6410. unsigned int crefs = elf_tdata (output_bfd)->cverdefs;
  6411. if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0)
  6412. || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs))
  6413. return false;
  6414. }
  6415. if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS))
  6416. {
  6417. if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags))
  6418. return false;
  6419. }
  6420. else if (info->flags & DF_BIND_NOW)
  6421. {
  6422. if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0))
  6423. return false;
  6424. }
  6425. if (info->flags_1)
  6426. {
  6427. if (bfd_link_executable (info))
  6428. info->flags_1 &= ~ (DF_1_INITFIRST
  6429. | DF_1_NODELETE
  6430. | DF_1_NOOPEN);
  6431. if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1))
  6432. return false;
  6433. }
  6434. if (elf_tdata (output_bfd)->cverrefs)
  6435. {
  6436. unsigned int crefs = elf_tdata (output_bfd)->cverrefs;
  6437. if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0)
  6438. || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs))
  6439. return false;
  6440. }
  6441. if ((elf_tdata (output_bfd)->cverrefs == 0
  6442. && elf_tdata (output_bfd)->cverdefs == 0)
  6443. || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1)
  6444. {
  6445. asection *s;
  6446. s = bfd_get_linker_section (dynobj, ".gnu.version");
  6447. s->flags |= SEC_EXCLUDE;
  6448. }
  6449. }
  6450. return true;
  6451. }
  6452. /* Find the first non-excluded output section. We'll use its
  6453. section symbol for some emitted relocs. */
  6454. void
  6455. _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info)
  6456. {
  6457. asection *s;
  6458. asection *found = NULL;
  6459. for (s = output_bfd->sections; s != NULL; s = s->next)
  6460. if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
  6461. && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
  6462. {
  6463. found = s;
  6464. if ((s->flags & SEC_THREAD_LOCAL) == 0)
  6465. break;
  6466. }
  6467. elf_hash_table (info)->text_index_section = found;
  6468. }
  6469. /* Find two non-excluded output sections, one for code, one for data.
  6470. We'll use their section symbols for some emitted relocs. */
  6471. void
  6472. _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info)
  6473. {
  6474. asection *s;
  6475. asection *found = NULL;
  6476. /* Data first, since setting text_index_section changes
  6477. _bfd_elf_omit_section_dynsym_default. */
  6478. for (s = output_bfd->sections; s != NULL; s = s->next)
  6479. if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
  6480. && !(s->flags & SEC_READONLY)
  6481. && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
  6482. {
  6483. found = s;
  6484. if ((s->flags & SEC_THREAD_LOCAL) == 0)
  6485. break;
  6486. }
  6487. elf_hash_table (info)->data_index_section = found;
  6488. for (s = output_bfd->sections; s != NULL; s = s->next)
  6489. if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC
  6490. && (s->flags & SEC_READONLY)
  6491. && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s))
  6492. {
  6493. found = s;
  6494. break;
  6495. }
  6496. elf_hash_table (info)->text_index_section = found;
  6497. }
  6498. #define GNU_HASH_SECTION_NAME(bed) \
  6499. (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash"
  6500. bool
  6501. bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info)
  6502. {
  6503. const struct elf_backend_data *bed;
  6504. unsigned long section_sym_count;
  6505. bfd_size_type dynsymcount = 0;
  6506. if (!is_elf_hash_table (info->hash))
  6507. return true;
  6508. bed = get_elf_backend_data (output_bfd);
  6509. (*bed->elf_backend_init_index_section) (output_bfd, info);
  6510. /* Assign dynsym indices. In a shared library we generate a section
  6511. symbol for each output section, which come first. Next come all
  6512. of the back-end allocated local dynamic syms, followed by the rest
  6513. of the global symbols.
  6514. This is usually not needed for static binaries, however backends
  6515. can request to always do it, e.g. the MIPS backend uses dynamic
  6516. symbol counts to lay out GOT, which will be produced in the
  6517. presence of GOT relocations even in static binaries (holding fixed
  6518. data in that case, to satisfy those relocations). */
  6519. if (elf_hash_table (info)->dynamic_sections_created
  6520. || bed->always_renumber_dynsyms)
  6521. dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info,
  6522. &section_sym_count);
  6523. if (elf_hash_table (info)->dynamic_sections_created)
  6524. {
  6525. bfd *dynobj;
  6526. asection *s;
  6527. unsigned int dtagcount;
  6528. dynobj = elf_hash_table (info)->dynobj;
  6529. /* Work out the size of the symbol version section. */
  6530. s = bfd_get_linker_section (dynobj, ".gnu.version");
  6531. BFD_ASSERT (s != NULL);
  6532. if ((s->flags & SEC_EXCLUDE) == 0)
  6533. {
  6534. s->size = dynsymcount * sizeof (Elf_External_Versym);
  6535. s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
  6536. if (s->contents == NULL)
  6537. return false;
  6538. if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0))
  6539. return false;
  6540. }
  6541. /* Set the size of the .dynsym and .hash sections. We counted
  6542. the number of dynamic symbols in elf_link_add_object_symbols.
  6543. We will build the contents of .dynsym and .hash when we build
  6544. the final symbol table, because until then we do not know the
  6545. correct value to give the symbols. We built the .dynstr
  6546. section as we went along in elf_link_add_object_symbols. */
  6547. s = elf_hash_table (info)->dynsym;
  6548. BFD_ASSERT (s != NULL);
  6549. s->size = dynsymcount * bed->s->sizeof_sym;
  6550. s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size);
  6551. if (s->contents == NULL)
  6552. return false;
  6553. /* The first entry in .dynsym is a dummy symbol. Clear all the
  6554. section syms, in case we don't output them all. */
  6555. ++section_sym_count;
  6556. memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym);
  6557. elf_hash_table (info)->bucketcount = 0;
  6558. /* Compute the size of the hashing table. As a side effect this
  6559. computes the hash values for all the names we export. */
  6560. if (info->emit_hash)
  6561. {
  6562. unsigned long int *hashcodes;
  6563. struct hash_codes_info hashinf;
  6564. bfd_size_type amt;
  6565. unsigned long int nsyms;
  6566. size_t bucketcount;
  6567. size_t hash_entry_size;
  6568. /* Compute the hash values for all exported symbols. At the same
  6569. time store the values in an array so that we could use them for
  6570. optimizations. */
  6571. amt = dynsymcount * sizeof (unsigned long int);
  6572. hashcodes = (unsigned long int *) bfd_malloc (amt);
  6573. if (hashcodes == NULL)
  6574. return false;
  6575. hashinf.hashcodes = hashcodes;
  6576. hashinf.error = false;
  6577. /* Put all hash values in HASHCODES. */
  6578. elf_link_hash_traverse (elf_hash_table (info),
  6579. elf_collect_hash_codes, &hashinf);
  6580. if (hashinf.error)
  6581. {
  6582. free (hashcodes);
  6583. return false;
  6584. }
  6585. nsyms = hashinf.hashcodes - hashcodes;
  6586. bucketcount
  6587. = compute_bucket_count (info, hashcodes, nsyms, 0);
  6588. free (hashcodes);
  6589. if (bucketcount == 0 && nsyms > 0)
  6590. return false;
  6591. elf_hash_table (info)->bucketcount = bucketcount;
  6592. s = bfd_get_linker_section (dynobj, ".hash");
  6593. BFD_ASSERT (s != NULL);
  6594. hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize;
  6595. s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size);
  6596. s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
  6597. if (s->contents == NULL)
  6598. return false;
  6599. bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents);
  6600. bfd_put (8 * hash_entry_size, output_bfd, dynsymcount,
  6601. s->contents + hash_entry_size);
  6602. }
  6603. if (info->emit_gnu_hash)
  6604. {
  6605. size_t i, cnt;
  6606. unsigned char *contents;
  6607. struct collect_gnu_hash_codes cinfo;
  6608. bfd_size_type amt;
  6609. size_t bucketcount;
  6610. memset (&cinfo, 0, sizeof (cinfo));
  6611. /* Compute the hash values for all exported symbols. At the same
  6612. time store the values in an array so that we could use them for
  6613. optimizations. */
  6614. amt = dynsymcount * 2 * sizeof (unsigned long int);
  6615. cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt);
  6616. if (cinfo.hashcodes == NULL)
  6617. return false;
  6618. cinfo.hashval = cinfo.hashcodes + dynsymcount;
  6619. cinfo.min_dynindx = -1;
  6620. cinfo.output_bfd = output_bfd;
  6621. cinfo.bed = bed;
  6622. /* Put all hash values in HASHCODES. */
  6623. elf_link_hash_traverse (elf_hash_table (info),
  6624. elf_collect_gnu_hash_codes, &cinfo);
  6625. if (cinfo.error)
  6626. {
  6627. free (cinfo.hashcodes);
  6628. return false;
  6629. }
  6630. bucketcount
  6631. = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1);
  6632. if (bucketcount == 0)
  6633. {
  6634. free (cinfo.hashcodes);
  6635. return false;
  6636. }
  6637. s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed));
  6638. BFD_ASSERT (s != NULL);
  6639. if (cinfo.nsyms == 0)
  6640. {
  6641. /* Empty .gnu.hash or .MIPS.xhash section is special. */
  6642. BFD_ASSERT (cinfo.min_dynindx == -1);
  6643. free (cinfo.hashcodes);
  6644. s->size = 5 * 4 + bed->s->arch_size / 8;
  6645. contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
  6646. if (contents == NULL)
  6647. return false;
  6648. s->contents = contents;
  6649. /* 1 empty bucket. */
  6650. bfd_put_32 (output_bfd, 1, contents);
  6651. /* SYMIDX above the special symbol 0. */
  6652. bfd_put_32 (output_bfd, 1, contents + 4);
  6653. /* Just one word for bitmask. */
  6654. bfd_put_32 (output_bfd, 1, contents + 8);
  6655. /* Only hash fn bloom filter. */
  6656. bfd_put_32 (output_bfd, 0, contents + 12);
  6657. /* No hashes are valid - empty bitmask. */
  6658. bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16);
  6659. /* No hashes in the only bucket. */
  6660. bfd_put_32 (output_bfd, 0,
  6661. contents + 16 + bed->s->arch_size / 8);
  6662. }
  6663. else
  6664. {
  6665. unsigned long int maskwords, maskbitslog2, x;
  6666. BFD_ASSERT (cinfo.min_dynindx != -1);
  6667. x = cinfo.nsyms;
  6668. maskbitslog2 = 1;
  6669. while ((x >>= 1) != 0)
  6670. ++maskbitslog2;
  6671. if (maskbitslog2 < 3)
  6672. maskbitslog2 = 5;
  6673. else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms)
  6674. maskbitslog2 = maskbitslog2 + 3;
  6675. else
  6676. maskbitslog2 = maskbitslog2 + 2;
  6677. if (bed->s->arch_size == 64)
  6678. {
  6679. if (maskbitslog2 == 5)
  6680. maskbitslog2 = 6;
  6681. cinfo.shift1 = 6;
  6682. }
  6683. else
  6684. cinfo.shift1 = 5;
  6685. cinfo.mask = (1 << cinfo.shift1) - 1;
  6686. cinfo.shift2 = maskbitslog2;
  6687. cinfo.maskbits = 1 << maskbitslog2;
  6688. maskwords = 1 << (maskbitslog2 - cinfo.shift1);
  6689. amt = bucketcount * sizeof (unsigned long int) * 2;
  6690. amt += maskwords * sizeof (bfd_vma);
  6691. cinfo.bitmask = (bfd_vma *) bfd_malloc (amt);
  6692. if (cinfo.bitmask == NULL)
  6693. {
  6694. free (cinfo.hashcodes);
  6695. return false;
  6696. }
  6697. cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords);
  6698. cinfo.indx = cinfo.counts + bucketcount;
  6699. cinfo.symindx = dynsymcount - cinfo.nsyms;
  6700. memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma));
  6701. /* Determine how often each hash bucket is used. */
  6702. memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0]));
  6703. for (i = 0; i < cinfo.nsyms; ++i)
  6704. ++cinfo.counts[cinfo.hashcodes[i] % bucketcount];
  6705. for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i)
  6706. if (cinfo.counts[i] != 0)
  6707. {
  6708. cinfo.indx[i] = cnt;
  6709. cnt += cinfo.counts[i];
  6710. }
  6711. BFD_ASSERT (cnt == dynsymcount);
  6712. cinfo.bucketcount = bucketcount;
  6713. cinfo.local_indx = cinfo.min_dynindx;
  6714. s->size = (4 + bucketcount + cinfo.nsyms) * 4;
  6715. s->size += cinfo.maskbits / 8;
  6716. if (bed->record_xhash_symbol != NULL)
  6717. s->size += cinfo.nsyms * 4;
  6718. contents = (unsigned char *) bfd_zalloc (output_bfd, s->size);
  6719. if (contents == NULL)
  6720. {
  6721. free (cinfo.bitmask);
  6722. free (cinfo.hashcodes);
  6723. return false;
  6724. }
  6725. s->contents = contents;
  6726. bfd_put_32 (output_bfd, bucketcount, contents);
  6727. bfd_put_32 (output_bfd, cinfo.symindx, contents + 4);
  6728. bfd_put_32 (output_bfd, maskwords, contents + 8);
  6729. bfd_put_32 (output_bfd, cinfo.shift2, contents + 12);
  6730. contents += 16 + cinfo.maskbits / 8;
  6731. for (i = 0; i < bucketcount; ++i)
  6732. {
  6733. if (cinfo.counts[i] == 0)
  6734. bfd_put_32 (output_bfd, 0, contents);
  6735. else
  6736. bfd_put_32 (output_bfd, cinfo.indx[i], contents);
  6737. contents += 4;
  6738. }
  6739. cinfo.contents = contents;
  6740. cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents;
  6741. /* Renumber dynamic symbols, if populating .gnu.hash section.
  6742. If using .MIPS.xhash, populate the translation table. */
  6743. elf_link_hash_traverse (elf_hash_table (info),
  6744. elf_gnu_hash_process_symidx, &cinfo);
  6745. contents = s->contents + 16;
  6746. for (i = 0; i < maskwords; ++i)
  6747. {
  6748. bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i],
  6749. contents);
  6750. contents += bed->s->arch_size / 8;
  6751. }
  6752. free (cinfo.bitmask);
  6753. free (cinfo.hashcodes);
  6754. }
  6755. }
  6756. s = bfd_get_linker_section (dynobj, ".dynstr");
  6757. BFD_ASSERT (s != NULL);
  6758. elf_finalize_dynstr (output_bfd, info);
  6759. s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr);
  6760. for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount)
  6761. if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0))
  6762. return false;
  6763. }
  6764. return true;
  6765. }
  6766. /* Make sure sec_info_type is cleared if sec_info is cleared too. */
  6767. static void
  6768. merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED,
  6769. asection *sec)
  6770. {
  6771. BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE);
  6772. sec->sec_info_type = SEC_INFO_TYPE_NONE;
  6773. }
  6774. /* Finish SHF_MERGE section merging. */
  6775. bool
  6776. _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info)
  6777. {
  6778. bfd *ibfd;
  6779. asection *sec;
  6780. if (!is_elf_hash_table (info->hash))
  6781. return false;
  6782. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  6783. if ((ibfd->flags & DYNAMIC) == 0
  6784. && bfd_get_flavour (ibfd) == bfd_target_elf_flavour
  6785. && (elf_elfheader (ibfd)->e_ident[EI_CLASS]
  6786. == get_elf_backend_data (obfd)->s->elfclass))
  6787. for (sec = ibfd->sections; sec != NULL; sec = sec->next)
  6788. if ((sec->flags & SEC_MERGE) != 0
  6789. && !bfd_is_abs_section (sec->output_section))
  6790. {
  6791. struct bfd_elf_section_data *secdata;
  6792. secdata = elf_section_data (sec);
  6793. if (! _bfd_add_merge_section (obfd,
  6794. &elf_hash_table (info)->merge_info,
  6795. sec, &secdata->sec_info))
  6796. return false;
  6797. else if (secdata->sec_info)
  6798. sec->sec_info_type = SEC_INFO_TYPE_MERGE;
  6799. }
  6800. if (elf_hash_table (info)->merge_info != NULL)
  6801. _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info,
  6802. merge_sections_remove_hook);
  6803. return true;
  6804. }
  6805. /* Create an entry in an ELF linker hash table. */
  6806. struct bfd_hash_entry *
  6807. _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
  6808. struct bfd_hash_table *table,
  6809. const char *string)
  6810. {
  6811. /* Allocate the structure if it has not already been allocated by a
  6812. subclass. */
  6813. if (entry == NULL)
  6814. {
  6815. entry = (struct bfd_hash_entry *)
  6816. bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
  6817. if (entry == NULL)
  6818. return entry;
  6819. }
  6820. /* Call the allocation method of the superclass. */
  6821. entry = _bfd_link_hash_newfunc (entry, table, string);
  6822. if (entry != NULL)
  6823. {
  6824. struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
  6825. struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
  6826. /* Set local fields. */
  6827. ret->indx = -1;
  6828. ret->dynindx = -1;
  6829. ret->got = htab->init_got_refcount;
  6830. ret->plt = htab->init_plt_refcount;
  6831. memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry)
  6832. - offsetof (struct elf_link_hash_entry, size)));
  6833. /* Assume that we have been called by a non-ELF symbol reader.
  6834. This flag is then reset by the code which reads an ELF input
  6835. file. This ensures that a symbol created by a non-ELF symbol
  6836. reader will have the flag set correctly. */
  6837. ret->non_elf = 1;
  6838. }
  6839. return entry;
  6840. }
  6841. /* Copy data from an indirect symbol to its direct symbol, hiding the
  6842. old indirect symbol. Also used for copying flags to a weakdef. */
  6843. void
  6844. _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info,
  6845. struct elf_link_hash_entry *dir,
  6846. struct elf_link_hash_entry *ind)
  6847. {
  6848. struct elf_link_hash_table *htab;
  6849. if (ind->dyn_relocs != NULL)
  6850. {
  6851. if (dir->dyn_relocs != NULL)
  6852. {
  6853. struct elf_dyn_relocs **pp;
  6854. struct elf_dyn_relocs *p;
  6855. /* Add reloc counts against the indirect sym to the direct sym
  6856. list. Merge any entries against the same section. */
  6857. for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
  6858. {
  6859. struct elf_dyn_relocs *q;
  6860. for (q = dir->dyn_relocs; q != NULL; q = q->next)
  6861. if (q->sec == p->sec)
  6862. {
  6863. q->pc_count += p->pc_count;
  6864. q->count += p->count;
  6865. *pp = p->next;
  6866. break;
  6867. }
  6868. if (q == NULL)
  6869. pp = &p->next;
  6870. }
  6871. *pp = dir->dyn_relocs;
  6872. }
  6873. dir->dyn_relocs = ind->dyn_relocs;
  6874. ind->dyn_relocs = NULL;
  6875. }
  6876. /* Copy down any references that we may have already seen to the
  6877. symbol which just became indirect. */
  6878. if (dir->versioned != versioned_hidden)
  6879. dir->ref_dynamic |= ind->ref_dynamic;
  6880. dir->ref_regular |= ind->ref_regular;
  6881. dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
  6882. dir->non_got_ref |= ind->non_got_ref;
  6883. dir->needs_plt |= ind->needs_plt;
  6884. dir->pointer_equality_needed |= ind->pointer_equality_needed;
  6885. if (ind->root.type != bfd_link_hash_indirect)
  6886. return;
  6887. /* Copy over the global and procedure linkage table refcount entries.
  6888. These may have been already set up by a check_relocs routine. */
  6889. htab = elf_hash_table (info);
  6890. if (ind->got.refcount > htab->init_got_refcount.refcount)
  6891. {
  6892. if (dir->got.refcount < 0)
  6893. dir->got.refcount = 0;
  6894. dir->got.refcount += ind->got.refcount;
  6895. ind->got.refcount = htab->init_got_refcount.refcount;
  6896. }
  6897. if (ind->plt.refcount > htab->init_plt_refcount.refcount)
  6898. {
  6899. if (dir->plt.refcount < 0)
  6900. dir->plt.refcount = 0;
  6901. dir->plt.refcount += ind->plt.refcount;
  6902. ind->plt.refcount = htab->init_plt_refcount.refcount;
  6903. }
  6904. if (ind->dynindx != -1)
  6905. {
  6906. if (dir->dynindx != -1)
  6907. _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index);
  6908. dir->dynindx = ind->dynindx;
  6909. dir->dynstr_index = ind->dynstr_index;
  6910. ind->dynindx = -1;
  6911. ind->dynstr_index = 0;
  6912. }
  6913. }
  6914. void
  6915. _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info,
  6916. struct elf_link_hash_entry *h,
  6917. bool force_local)
  6918. {
  6919. /* STT_GNU_IFUNC symbol must go through PLT. */
  6920. if (h->type != STT_GNU_IFUNC)
  6921. {
  6922. h->plt = elf_hash_table (info)->init_plt_offset;
  6923. h->needs_plt = 0;
  6924. }
  6925. if (force_local)
  6926. {
  6927. h->forced_local = 1;
  6928. if (h->dynindx != -1)
  6929. {
  6930. _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
  6931. h->dynstr_index);
  6932. h->dynindx = -1;
  6933. h->dynstr_index = 0;
  6934. }
  6935. }
  6936. }
  6937. /* Hide a symbol. */
  6938. void
  6939. _bfd_elf_link_hide_symbol (bfd *output_bfd,
  6940. struct bfd_link_info *info,
  6941. struct bfd_link_hash_entry *h)
  6942. {
  6943. if (is_elf_hash_table (info->hash))
  6944. {
  6945. const struct elf_backend_data *bed
  6946. = get_elf_backend_data (output_bfd);
  6947. struct elf_link_hash_entry *eh
  6948. = (struct elf_link_hash_entry *) h;
  6949. bed->elf_backend_hide_symbol (info, eh, true);
  6950. eh->def_dynamic = 0;
  6951. eh->ref_dynamic = 0;
  6952. eh->dynamic_def = 0;
  6953. }
  6954. }
  6955. /* Initialize an ELF linker hash table. *TABLE has been zeroed by our
  6956. caller. */
  6957. bool
  6958. _bfd_elf_link_hash_table_init
  6959. (struct elf_link_hash_table *table,
  6960. bfd *abfd,
  6961. struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
  6962. struct bfd_hash_table *,
  6963. const char *),
  6964. unsigned int entsize,
  6965. enum elf_target_id target_id)
  6966. {
  6967. bool ret;
  6968. int can_refcount = get_elf_backend_data (abfd)->can_refcount;
  6969. table->init_got_refcount.refcount = can_refcount - 1;
  6970. table->init_plt_refcount.refcount = can_refcount - 1;
  6971. table->init_got_offset.offset = -(bfd_vma) 1;
  6972. table->init_plt_offset.offset = -(bfd_vma) 1;
  6973. /* The first dynamic symbol is a dummy. */
  6974. table->dynsymcount = 1;
  6975. ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
  6976. table->root.type = bfd_link_elf_hash_table;
  6977. table->hash_table_id = target_id;
  6978. table->target_os = get_elf_backend_data (abfd)->target_os;
  6979. return ret;
  6980. }
  6981. /* Create an ELF linker hash table. */
  6982. struct bfd_link_hash_table *
  6983. _bfd_elf_link_hash_table_create (bfd *abfd)
  6984. {
  6985. struct elf_link_hash_table *ret;
  6986. size_t amt = sizeof (struct elf_link_hash_table);
  6987. ret = (struct elf_link_hash_table *) bfd_zmalloc (amt);
  6988. if (ret == NULL)
  6989. return NULL;
  6990. if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc,
  6991. sizeof (struct elf_link_hash_entry),
  6992. GENERIC_ELF_DATA))
  6993. {
  6994. free (ret);
  6995. return NULL;
  6996. }
  6997. ret->root.hash_table_free = _bfd_elf_link_hash_table_free;
  6998. return &ret->root;
  6999. }
  7000. /* Destroy an ELF linker hash table. */
  7001. void
  7002. _bfd_elf_link_hash_table_free (bfd *obfd)
  7003. {
  7004. struct elf_link_hash_table *htab;
  7005. htab = (struct elf_link_hash_table *) obfd->link.hash;
  7006. if (htab->dynstr != NULL)
  7007. _bfd_elf_strtab_free (htab->dynstr);
  7008. _bfd_merge_sections_free (htab->merge_info);
  7009. _bfd_generic_link_hash_table_free (obfd);
  7010. }
  7011. /* This is a hook for the ELF emulation code in the generic linker to
  7012. tell the backend linker what file name to use for the DT_NEEDED
  7013. entry for a dynamic object. */
  7014. void
  7015. bfd_elf_set_dt_needed_name (bfd *abfd, const char *name)
  7016. {
  7017. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  7018. && bfd_get_format (abfd) == bfd_object)
  7019. elf_dt_name (abfd) = name;
  7020. }
  7021. int
  7022. bfd_elf_get_dyn_lib_class (bfd *abfd)
  7023. {
  7024. int lib_class;
  7025. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  7026. && bfd_get_format (abfd) == bfd_object)
  7027. lib_class = elf_dyn_lib_class (abfd);
  7028. else
  7029. lib_class = 0;
  7030. return lib_class;
  7031. }
  7032. void
  7033. bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class)
  7034. {
  7035. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  7036. && bfd_get_format (abfd) == bfd_object)
  7037. elf_dyn_lib_class (abfd) = lib_class;
  7038. }
  7039. /* Get the list of DT_NEEDED entries for a link. This is a hook for
  7040. the linker ELF emulation code. */
  7041. struct bfd_link_needed_list *
  7042. bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED,
  7043. struct bfd_link_info *info)
  7044. {
  7045. if (! is_elf_hash_table (info->hash))
  7046. return NULL;
  7047. return elf_hash_table (info)->needed;
  7048. }
  7049. /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
  7050. hook for the linker ELF emulation code. */
  7051. struct bfd_link_needed_list *
  7052. bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED,
  7053. struct bfd_link_info *info)
  7054. {
  7055. if (! is_elf_hash_table (info->hash))
  7056. return NULL;
  7057. return elf_hash_table (info)->runpath;
  7058. }
  7059. /* Get the name actually used for a dynamic object for a link. This
  7060. is the SONAME entry if there is one. Otherwise, it is the string
  7061. passed to bfd_elf_set_dt_needed_name, or it is the filename. */
  7062. const char *
  7063. bfd_elf_get_dt_soname (bfd *abfd)
  7064. {
  7065. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  7066. && bfd_get_format (abfd) == bfd_object)
  7067. return elf_dt_name (abfd);
  7068. return NULL;
  7069. }
  7070. /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
  7071. the ELF linker emulation code. */
  7072. bool
  7073. bfd_elf_get_bfd_needed_list (bfd *abfd,
  7074. struct bfd_link_needed_list **pneeded)
  7075. {
  7076. asection *s;
  7077. bfd_byte *dynbuf = NULL;
  7078. unsigned int elfsec;
  7079. unsigned long shlink;
  7080. bfd_byte *extdyn, *extdynend;
  7081. size_t extdynsize;
  7082. void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
  7083. *pneeded = NULL;
  7084. if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
  7085. || bfd_get_format (abfd) != bfd_object)
  7086. return true;
  7087. s = bfd_get_section_by_name (abfd, ".dynamic");
  7088. if (s == NULL || s->size == 0)
  7089. return true;
  7090. if (!bfd_malloc_and_get_section (abfd, s, &dynbuf))
  7091. goto error_return;
  7092. elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
  7093. if (elfsec == SHN_BAD)
  7094. goto error_return;
  7095. shlink = elf_elfsections (abfd)[elfsec]->sh_link;
  7096. extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
  7097. swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
  7098. extdyn = dynbuf;
  7099. extdynend = extdyn + s->size;
  7100. for (; extdyn < extdynend; extdyn += extdynsize)
  7101. {
  7102. Elf_Internal_Dyn dyn;
  7103. (*swap_dyn_in) (abfd, extdyn, &dyn);
  7104. if (dyn.d_tag == DT_NULL)
  7105. break;
  7106. if (dyn.d_tag == DT_NEEDED)
  7107. {
  7108. const char *string;
  7109. struct bfd_link_needed_list *l;
  7110. unsigned int tagv = dyn.d_un.d_val;
  7111. size_t amt;
  7112. string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
  7113. if (string == NULL)
  7114. goto error_return;
  7115. amt = sizeof *l;
  7116. l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
  7117. if (l == NULL)
  7118. goto error_return;
  7119. l->by = abfd;
  7120. l->name = string;
  7121. l->next = *pneeded;
  7122. *pneeded = l;
  7123. }
  7124. }
  7125. free (dynbuf);
  7126. return true;
  7127. error_return:
  7128. free (dynbuf);
  7129. return false;
  7130. }
  7131. struct elf_symbuf_symbol
  7132. {
  7133. unsigned long st_name; /* Symbol name, index in string tbl */
  7134. unsigned char st_info; /* Type and binding attributes */
  7135. unsigned char st_other; /* Visibilty, and target specific */
  7136. };
  7137. struct elf_symbuf_head
  7138. {
  7139. struct elf_symbuf_symbol *ssym;
  7140. size_t count;
  7141. unsigned int st_shndx;
  7142. };
  7143. struct elf_symbol
  7144. {
  7145. union
  7146. {
  7147. Elf_Internal_Sym *isym;
  7148. struct elf_symbuf_symbol *ssym;
  7149. void *p;
  7150. } u;
  7151. const char *name;
  7152. };
  7153. /* Sort references to symbols by ascending section number. */
  7154. static int
  7155. elf_sort_elf_symbol (const void *arg1, const void *arg2)
  7156. {
  7157. const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1;
  7158. const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2;
  7159. if (s1->st_shndx != s2->st_shndx)
  7160. return s1->st_shndx > s2->st_shndx ? 1 : -1;
  7161. /* Final sort by the address of the sym in the symbuf ensures
  7162. a stable sort. */
  7163. if (s1 != s2)
  7164. return s1 > s2 ? 1 : -1;
  7165. return 0;
  7166. }
  7167. static int
  7168. elf_sym_name_compare (const void *arg1, const void *arg2)
  7169. {
  7170. const struct elf_symbol *s1 = (const struct elf_symbol *) arg1;
  7171. const struct elf_symbol *s2 = (const struct elf_symbol *) arg2;
  7172. int ret = strcmp (s1->name, s2->name);
  7173. if (ret != 0)
  7174. return ret;
  7175. if (s1->u.p != s2->u.p)
  7176. return s1->u.p > s2->u.p ? 1 : -1;
  7177. return 0;
  7178. }
  7179. static struct elf_symbuf_head *
  7180. elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf)
  7181. {
  7182. Elf_Internal_Sym **ind, **indbufend, **indbuf;
  7183. struct elf_symbuf_symbol *ssym;
  7184. struct elf_symbuf_head *ssymbuf, *ssymhead;
  7185. size_t i, shndx_count, total_size, amt;
  7186. amt = symcount * sizeof (*indbuf);
  7187. indbuf = (Elf_Internal_Sym **) bfd_malloc (amt);
  7188. if (indbuf == NULL)
  7189. return NULL;
  7190. for (ind = indbuf, i = 0; i < symcount; i++)
  7191. if (isymbuf[i].st_shndx != SHN_UNDEF)
  7192. *ind++ = &isymbuf[i];
  7193. indbufend = ind;
  7194. qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *),
  7195. elf_sort_elf_symbol);
  7196. shndx_count = 0;
  7197. if (indbufend > indbuf)
  7198. for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++)
  7199. if (ind[0]->st_shndx != ind[1]->st_shndx)
  7200. shndx_count++;
  7201. total_size = ((shndx_count + 1) * sizeof (*ssymbuf)
  7202. + (indbufend - indbuf) * sizeof (*ssym));
  7203. ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size);
  7204. if (ssymbuf == NULL)
  7205. {
  7206. free (indbuf);
  7207. return NULL;
  7208. }
  7209. ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1);
  7210. ssymbuf->ssym = NULL;
  7211. ssymbuf->count = shndx_count;
  7212. ssymbuf->st_shndx = 0;
  7213. for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++)
  7214. {
  7215. if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx)
  7216. {
  7217. ssymhead++;
  7218. ssymhead->ssym = ssym;
  7219. ssymhead->count = 0;
  7220. ssymhead->st_shndx = (*ind)->st_shndx;
  7221. }
  7222. ssym->st_name = (*ind)->st_name;
  7223. ssym->st_info = (*ind)->st_info;
  7224. ssym->st_other = (*ind)->st_other;
  7225. ssymhead->count++;
  7226. }
  7227. BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count
  7228. && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf)
  7229. == total_size));
  7230. free (indbuf);
  7231. return ssymbuf;
  7232. }
  7233. /* Check if 2 sections define the same set of local and global
  7234. symbols. */
  7235. static bool
  7236. bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2,
  7237. struct bfd_link_info *info)
  7238. {
  7239. bfd *bfd1, *bfd2;
  7240. const struct elf_backend_data *bed1, *bed2;
  7241. Elf_Internal_Shdr *hdr1, *hdr2;
  7242. size_t symcount1, symcount2;
  7243. Elf_Internal_Sym *isymbuf1, *isymbuf2;
  7244. struct elf_symbuf_head *ssymbuf1, *ssymbuf2;
  7245. Elf_Internal_Sym *isym, *isymend;
  7246. struct elf_symbol *symtable1 = NULL, *symtable2 = NULL;
  7247. size_t count1, count2, sec_count1, sec_count2, i;
  7248. unsigned int shndx1, shndx2;
  7249. bool result;
  7250. bool ignore_section_symbol_p;
  7251. bfd1 = sec1->owner;
  7252. bfd2 = sec2->owner;
  7253. /* Both sections have to be in ELF. */
  7254. if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour
  7255. || bfd_get_flavour (bfd2) != bfd_target_elf_flavour)
  7256. return false;
  7257. if (elf_section_type (sec1) != elf_section_type (sec2))
  7258. return false;
  7259. shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1);
  7260. shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2);
  7261. if (shndx1 == SHN_BAD || shndx2 == SHN_BAD)
  7262. return false;
  7263. bed1 = get_elf_backend_data (bfd1);
  7264. bed2 = get_elf_backend_data (bfd2);
  7265. hdr1 = &elf_tdata (bfd1)->symtab_hdr;
  7266. symcount1 = hdr1->sh_size / bed1->s->sizeof_sym;
  7267. hdr2 = &elf_tdata (bfd2)->symtab_hdr;
  7268. symcount2 = hdr2->sh_size / bed2->s->sizeof_sym;
  7269. if (symcount1 == 0 || symcount2 == 0)
  7270. return false;
  7271. result = false;
  7272. isymbuf1 = NULL;
  7273. isymbuf2 = NULL;
  7274. ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf;
  7275. ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf;
  7276. /* Ignore section symbols only when matching non-debugging sections
  7277. or linkonce section with comdat section. */
  7278. ignore_section_symbol_p
  7279. = ((sec1->flags & SEC_DEBUGGING) == 0
  7280. || ((elf_section_flags (sec1) & SHF_GROUP)
  7281. != (elf_section_flags (sec2) & SHF_GROUP)));
  7282. if (ssymbuf1 == NULL)
  7283. {
  7284. isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0,
  7285. NULL, NULL, NULL);
  7286. if (isymbuf1 == NULL)
  7287. goto done;
  7288. if (info != NULL && !info->reduce_memory_overheads)
  7289. {
  7290. ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1);
  7291. elf_tdata (bfd1)->symbuf = ssymbuf1;
  7292. }
  7293. }
  7294. if (ssymbuf1 == NULL || ssymbuf2 == NULL)
  7295. {
  7296. isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0,
  7297. NULL, NULL, NULL);
  7298. if (isymbuf2 == NULL)
  7299. goto done;
  7300. if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads)
  7301. {
  7302. ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2);
  7303. elf_tdata (bfd2)->symbuf = ssymbuf2;
  7304. }
  7305. }
  7306. if (ssymbuf1 != NULL && ssymbuf2 != NULL)
  7307. {
  7308. /* Optimized faster version. */
  7309. size_t lo, hi, mid;
  7310. struct elf_symbol *symp;
  7311. struct elf_symbuf_symbol *ssym, *ssymend;
  7312. lo = 0;
  7313. hi = ssymbuf1->count;
  7314. ssymbuf1++;
  7315. count1 = 0;
  7316. sec_count1 = 0;
  7317. while (lo < hi)
  7318. {
  7319. mid = (lo + hi) / 2;
  7320. if (shndx1 < ssymbuf1[mid].st_shndx)
  7321. hi = mid;
  7322. else if (shndx1 > ssymbuf1[mid].st_shndx)
  7323. lo = mid + 1;
  7324. else
  7325. {
  7326. count1 = ssymbuf1[mid].count;
  7327. ssymbuf1 += mid;
  7328. break;
  7329. }
  7330. }
  7331. if (ignore_section_symbol_p)
  7332. {
  7333. for (i = 0; i < count1; i++)
  7334. if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION)
  7335. sec_count1++;
  7336. count1 -= sec_count1;
  7337. }
  7338. lo = 0;
  7339. hi = ssymbuf2->count;
  7340. ssymbuf2++;
  7341. count2 = 0;
  7342. sec_count2 = 0;
  7343. while (lo < hi)
  7344. {
  7345. mid = (lo + hi) / 2;
  7346. if (shndx2 < ssymbuf2[mid].st_shndx)
  7347. hi = mid;
  7348. else if (shndx2 > ssymbuf2[mid].st_shndx)
  7349. lo = mid + 1;
  7350. else
  7351. {
  7352. count2 = ssymbuf2[mid].count;
  7353. ssymbuf2 += mid;
  7354. break;
  7355. }
  7356. }
  7357. if (ignore_section_symbol_p)
  7358. {
  7359. for (i = 0; i < count2; i++)
  7360. if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION)
  7361. sec_count2++;
  7362. count2 -= sec_count2;
  7363. }
  7364. if (count1 == 0 || count2 == 0 || count1 != count2)
  7365. goto done;
  7366. symtable1
  7367. = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1));
  7368. symtable2
  7369. = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2));
  7370. if (symtable1 == NULL || symtable2 == NULL)
  7371. goto done;
  7372. symp = symtable1;
  7373. for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1;
  7374. ssym < ssymend; ssym++)
  7375. if (sec_count1 == 0
  7376. || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
  7377. {
  7378. symp->u.ssym = ssym;
  7379. symp->name = bfd_elf_string_from_elf_section (bfd1,
  7380. hdr1->sh_link,
  7381. ssym->st_name);
  7382. symp++;
  7383. }
  7384. symp = symtable2;
  7385. for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2;
  7386. ssym < ssymend; ssym++)
  7387. if (sec_count2 == 0
  7388. || ELF_ST_TYPE (ssym->st_info) != STT_SECTION)
  7389. {
  7390. symp->u.ssym = ssym;
  7391. symp->name = bfd_elf_string_from_elf_section (bfd2,
  7392. hdr2->sh_link,
  7393. ssym->st_name);
  7394. symp++;
  7395. }
  7396. /* Sort symbol by name. */
  7397. qsort (symtable1, count1, sizeof (struct elf_symbol),
  7398. elf_sym_name_compare);
  7399. qsort (symtable2, count1, sizeof (struct elf_symbol),
  7400. elf_sym_name_compare);
  7401. for (i = 0; i < count1; i++)
  7402. /* Two symbols must have the same binding, type and name. */
  7403. if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info
  7404. || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other
  7405. || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
  7406. goto done;
  7407. result = true;
  7408. goto done;
  7409. }
  7410. symtable1 = (struct elf_symbol *)
  7411. bfd_malloc (symcount1 * sizeof (struct elf_symbol));
  7412. symtable2 = (struct elf_symbol *)
  7413. bfd_malloc (symcount2 * sizeof (struct elf_symbol));
  7414. if (symtable1 == NULL || symtable2 == NULL)
  7415. goto done;
  7416. /* Count definitions in the section. */
  7417. count1 = 0;
  7418. for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++)
  7419. if (isym->st_shndx == shndx1
  7420. && (!ignore_section_symbol_p
  7421. || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
  7422. symtable1[count1++].u.isym = isym;
  7423. count2 = 0;
  7424. for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++)
  7425. if (isym->st_shndx == shndx2
  7426. && (!ignore_section_symbol_p
  7427. || ELF_ST_TYPE (isym->st_info) != STT_SECTION))
  7428. symtable2[count2++].u.isym = isym;
  7429. if (count1 == 0 || count2 == 0 || count1 != count2)
  7430. goto done;
  7431. for (i = 0; i < count1; i++)
  7432. symtable1[i].name
  7433. = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link,
  7434. symtable1[i].u.isym->st_name);
  7435. for (i = 0; i < count2; i++)
  7436. symtable2[i].name
  7437. = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link,
  7438. symtable2[i].u.isym->st_name);
  7439. /* Sort symbol by name. */
  7440. qsort (symtable1, count1, sizeof (struct elf_symbol),
  7441. elf_sym_name_compare);
  7442. qsort (symtable2, count1, sizeof (struct elf_symbol),
  7443. elf_sym_name_compare);
  7444. for (i = 0; i < count1; i++)
  7445. /* Two symbols must have the same binding, type and name. */
  7446. if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info
  7447. || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other
  7448. || strcmp (symtable1 [i].name, symtable2 [i].name) != 0)
  7449. goto done;
  7450. result = true;
  7451. done:
  7452. free (symtable1);
  7453. free (symtable2);
  7454. free (isymbuf1);
  7455. free (isymbuf2);
  7456. return result;
  7457. }
  7458. /* Return TRUE if 2 section types are compatible. */
  7459. bool
  7460. _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec,
  7461. bfd *bbfd, const asection *bsec)
  7462. {
  7463. if (asec == NULL
  7464. || bsec == NULL
  7465. || abfd->xvec->flavour != bfd_target_elf_flavour
  7466. || bbfd->xvec->flavour != bfd_target_elf_flavour)
  7467. return true;
  7468. return elf_section_type (asec) == elf_section_type (bsec);
  7469. }
  7470. /* Final phase of ELF linker. */
  7471. /* A structure we use to avoid passing large numbers of arguments. */
  7472. struct elf_final_link_info
  7473. {
  7474. /* General link information. */
  7475. struct bfd_link_info *info;
  7476. /* Output BFD. */
  7477. bfd *output_bfd;
  7478. /* Symbol string table. */
  7479. struct elf_strtab_hash *symstrtab;
  7480. /* .hash section. */
  7481. asection *hash_sec;
  7482. /* symbol version section (.gnu.version). */
  7483. asection *symver_sec;
  7484. /* Buffer large enough to hold contents of any section. */
  7485. bfd_byte *contents;
  7486. /* Buffer large enough to hold external relocs of any section. */
  7487. void *external_relocs;
  7488. /* Buffer large enough to hold internal relocs of any section. */
  7489. Elf_Internal_Rela *internal_relocs;
  7490. /* Buffer large enough to hold external local symbols of any input
  7491. BFD. */
  7492. bfd_byte *external_syms;
  7493. /* And a buffer for symbol section indices. */
  7494. Elf_External_Sym_Shndx *locsym_shndx;
  7495. /* Buffer large enough to hold internal local symbols of any input
  7496. BFD. */
  7497. Elf_Internal_Sym *internal_syms;
  7498. /* Array large enough to hold a symbol index for each local symbol
  7499. of any input BFD. */
  7500. long *indices;
  7501. /* Array large enough to hold a section pointer for each local
  7502. symbol of any input BFD. */
  7503. asection **sections;
  7504. /* Buffer for SHT_SYMTAB_SHNDX section. */
  7505. Elf_External_Sym_Shndx *symshndxbuf;
  7506. /* Number of STT_FILE syms seen. */
  7507. size_t filesym_count;
  7508. /* Local symbol hash table. */
  7509. struct bfd_hash_table local_hash_table;
  7510. };
  7511. struct local_hash_entry
  7512. {
  7513. /* Base hash table entry structure. */
  7514. struct bfd_hash_entry root;
  7515. /* Size of the local symbol name. */
  7516. size_t size;
  7517. /* Number of the duplicated local symbol names. */
  7518. long count;
  7519. };
  7520. /* Create an entry in the local symbol hash table. */
  7521. static struct bfd_hash_entry *
  7522. local_hash_newfunc (struct bfd_hash_entry *entry,
  7523. struct bfd_hash_table *table,
  7524. const char *string)
  7525. {
  7526. /* Allocate the structure if it has not already been allocated by a
  7527. subclass. */
  7528. if (entry == NULL)
  7529. {
  7530. entry = bfd_hash_allocate (table,
  7531. sizeof (struct local_hash_entry));
  7532. if (entry == NULL)
  7533. return entry;
  7534. }
  7535. /* Call the allocation method of the superclass. */
  7536. entry = bfd_hash_newfunc (entry, table, string);
  7537. if (entry != NULL)
  7538. {
  7539. ((struct local_hash_entry *) entry)->count = 0;
  7540. ((struct local_hash_entry *) entry)->size = 0;
  7541. }
  7542. return entry;
  7543. }
  7544. /* This struct is used to pass information to elf_link_output_extsym. */
  7545. struct elf_outext_info
  7546. {
  7547. bool failed;
  7548. bool localsyms;
  7549. bool file_sym_done;
  7550. struct elf_final_link_info *flinfo;
  7551. };
  7552. /* Support for evaluating a complex relocation.
  7553. Complex relocations are generalized, self-describing relocations. The
  7554. implementation of them consists of two parts: complex symbols, and the
  7555. relocations themselves.
  7556. The relocations use a reserved elf-wide relocation type code (R_RELC
  7557. external / BFD_RELOC_RELC internal) and an encoding of relocation field
  7558. information (start bit, end bit, word width, etc) into the addend. This
  7559. information is extracted from CGEN-generated operand tables within gas.
  7560. Complex symbols are mangled symbols (STT_RELC external / BSF_RELC
  7561. internal) representing prefix-notation expressions, including but not
  7562. limited to those sorts of expressions normally encoded as addends in the
  7563. addend field. The symbol mangling format is:
  7564. <node> := <literal>
  7565. | <unary-operator> ':' <node>
  7566. | <binary-operator> ':' <node> ':' <node>
  7567. ;
  7568. <literal> := 's' <digits=N> ':' <N character symbol name>
  7569. | 'S' <digits=N> ':' <N character section name>
  7570. | '#' <hexdigits>
  7571. ;
  7572. <binary-operator> := as in C
  7573. <unary-operator> := as in C, plus "0-" for unambiguous negation. */
  7574. static void
  7575. set_symbol_value (bfd *bfd_with_globals,
  7576. Elf_Internal_Sym *isymbuf,
  7577. size_t locsymcount,
  7578. size_t symidx,
  7579. bfd_vma val)
  7580. {
  7581. struct elf_link_hash_entry **sym_hashes;
  7582. struct elf_link_hash_entry *h;
  7583. size_t extsymoff = locsymcount;
  7584. if (symidx < locsymcount)
  7585. {
  7586. Elf_Internal_Sym *sym;
  7587. sym = isymbuf + symidx;
  7588. if (ELF_ST_BIND (sym->st_info) == STB_LOCAL)
  7589. {
  7590. /* It is a local symbol: move it to the
  7591. "absolute" section and give it a value. */
  7592. sym->st_shndx = SHN_ABS;
  7593. sym->st_value = val;
  7594. return;
  7595. }
  7596. BFD_ASSERT (elf_bad_symtab (bfd_with_globals));
  7597. extsymoff = 0;
  7598. }
  7599. /* It is a global symbol: set its link type
  7600. to "defined" and give it a value. */
  7601. sym_hashes = elf_sym_hashes (bfd_with_globals);
  7602. h = sym_hashes [symidx - extsymoff];
  7603. while (h->root.type == bfd_link_hash_indirect
  7604. || h->root.type == bfd_link_hash_warning)
  7605. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  7606. h->root.type = bfd_link_hash_defined;
  7607. h->root.u.def.value = val;
  7608. h->root.u.def.section = bfd_abs_section_ptr;
  7609. }
  7610. static bool
  7611. resolve_symbol (const char *name,
  7612. bfd *input_bfd,
  7613. struct elf_final_link_info *flinfo,
  7614. bfd_vma *result,
  7615. Elf_Internal_Sym *isymbuf,
  7616. size_t locsymcount)
  7617. {
  7618. Elf_Internal_Sym *sym;
  7619. struct bfd_link_hash_entry *global_entry;
  7620. const char *candidate = NULL;
  7621. Elf_Internal_Shdr *symtab_hdr;
  7622. size_t i;
  7623. symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
  7624. for (i = 0; i < locsymcount; ++ i)
  7625. {
  7626. sym = isymbuf + i;
  7627. if (ELF_ST_BIND (sym->st_info) != STB_LOCAL)
  7628. continue;
  7629. candidate = bfd_elf_string_from_elf_section (input_bfd,
  7630. symtab_hdr->sh_link,
  7631. sym->st_name);
  7632. #ifdef DEBUG
  7633. printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n",
  7634. name, candidate, (unsigned long) sym->st_value);
  7635. #endif
  7636. if (candidate && strcmp (candidate, name) == 0)
  7637. {
  7638. asection *sec = flinfo->sections [i];
  7639. *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0);
  7640. *result += sec->output_offset + sec->output_section->vma;
  7641. #ifdef DEBUG
  7642. printf ("Found symbol with value %8.8lx\n",
  7643. (unsigned long) *result);
  7644. #endif
  7645. return true;
  7646. }
  7647. }
  7648. /* Hmm, haven't found it yet. perhaps it is a global. */
  7649. global_entry = bfd_link_hash_lookup (flinfo->info->hash, name,
  7650. false, false, true);
  7651. if (!global_entry)
  7652. return false;
  7653. if (global_entry->type == bfd_link_hash_defined
  7654. || global_entry->type == bfd_link_hash_defweak)
  7655. {
  7656. *result = (global_entry->u.def.value
  7657. + global_entry->u.def.section->output_section->vma
  7658. + global_entry->u.def.section->output_offset);
  7659. #ifdef DEBUG
  7660. printf ("Found GLOBAL symbol '%s' with value %8.8lx\n",
  7661. global_entry->root.string, (unsigned long) *result);
  7662. #endif
  7663. return true;
  7664. }
  7665. return false;
  7666. }
  7667. /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in
  7668. bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section
  7669. names like "foo.end" which is the end address of section "foo". */
  7670. static bool
  7671. resolve_section (const char *name,
  7672. asection *sections,
  7673. bfd_vma *result,
  7674. bfd * abfd)
  7675. {
  7676. asection *curr;
  7677. unsigned int len;
  7678. for (curr = sections; curr; curr = curr->next)
  7679. if (strcmp (curr->name, name) == 0)
  7680. {
  7681. *result = curr->vma;
  7682. return true;
  7683. }
  7684. /* Hmm. still haven't found it. try pseudo-section names. */
  7685. /* FIXME: This could be coded more efficiently... */
  7686. for (curr = sections; curr; curr = curr->next)
  7687. {
  7688. len = strlen (curr->name);
  7689. if (len > strlen (name))
  7690. continue;
  7691. if (strncmp (curr->name, name, len) == 0)
  7692. {
  7693. if (startswith (name + len, ".end"))
  7694. {
  7695. *result = (curr->vma
  7696. + curr->size / bfd_octets_per_byte (abfd, curr));
  7697. return true;
  7698. }
  7699. /* Insert more pseudo-section names here, if you like. */
  7700. }
  7701. }
  7702. return false;
  7703. }
  7704. static void
  7705. undefined_reference (const char *reftype, const char *name)
  7706. {
  7707. /* xgettext:c-format */
  7708. _bfd_error_handler (_("undefined %s reference in complex symbol: %s"),
  7709. reftype, name);
  7710. bfd_set_error (bfd_error_bad_value);
  7711. }
  7712. static bool
  7713. eval_symbol (bfd_vma *result,
  7714. const char **symp,
  7715. bfd *input_bfd,
  7716. struct elf_final_link_info *flinfo,
  7717. bfd_vma dot,
  7718. Elf_Internal_Sym *isymbuf,
  7719. size_t locsymcount,
  7720. int signed_p)
  7721. {
  7722. size_t len;
  7723. size_t symlen;
  7724. bfd_vma a;
  7725. bfd_vma b;
  7726. char symbuf[4096];
  7727. const char *sym = *symp;
  7728. const char *symend;
  7729. bool symbol_is_section = false;
  7730. len = strlen (sym);
  7731. symend = sym + len;
  7732. if (len < 1 || len > sizeof (symbuf))
  7733. {
  7734. bfd_set_error (bfd_error_invalid_operation);
  7735. return false;
  7736. }
  7737. switch (* sym)
  7738. {
  7739. case '.':
  7740. *result = dot;
  7741. *symp = sym + 1;
  7742. return true;
  7743. case '#':
  7744. ++sym;
  7745. *result = strtoul (sym, (char **) symp, 16);
  7746. return true;
  7747. case 'S':
  7748. symbol_is_section = true;
  7749. /* Fall through. */
  7750. case 's':
  7751. ++sym;
  7752. symlen = strtol (sym, (char **) symp, 10);
  7753. sym = *symp + 1; /* Skip the trailing ':'. */
  7754. if (symend < sym || symlen + 1 > sizeof (symbuf))
  7755. {
  7756. bfd_set_error (bfd_error_invalid_operation);
  7757. return false;
  7758. }
  7759. memcpy (symbuf, sym, symlen);
  7760. symbuf[symlen] = '\0';
  7761. *symp = sym + symlen;
  7762. /* Is it always possible, with complex symbols, that gas "mis-guessed"
  7763. the symbol as a section, or vice-versa. so we're pretty liberal in our
  7764. interpretation here; section means "try section first", not "must be a
  7765. section", and likewise with symbol. */
  7766. if (symbol_is_section)
  7767. {
  7768. if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd)
  7769. && !resolve_symbol (symbuf, input_bfd, flinfo, result,
  7770. isymbuf, locsymcount))
  7771. {
  7772. undefined_reference ("section", symbuf);
  7773. return false;
  7774. }
  7775. }
  7776. else
  7777. {
  7778. if (!resolve_symbol (symbuf, input_bfd, flinfo, result,
  7779. isymbuf, locsymcount)
  7780. && !resolve_section (symbuf, flinfo->output_bfd->sections,
  7781. result, input_bfd))
  7782. {
  7783. undefined_reference ("symbol", symbuf);
  7784. return false;
  7785. }
  7786. }
  7787. return true;
  7788. /* All that remains are operators. */
  7789. #define UNARY_OP(op) \
  7790. if (startswith (sym, #op)) \
  7791. { \
  7792. sym += strlen (#op); \
  7793. if (*sym == ':') \
  7794. ++sym; \
  7795. *symp = sym; \
  7796. if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
  7797. isymbuf, locsymcount, signed_p)) \
  7798. return false; \
  7799. if (signed_p) \
  7800. *result = op ((bfd_signed_vma) a); \
  7801. else \
  7802. *result = op a; \
  7803. return true; \
  7804. }
  7805. #define BINARY_OP_HEAD(op) \
  7806. if (startswith (sym, #op)) \
  7807. { \
  7808. sym += strlen (#op); \
  7809. if (*sym == ':') \
  7810. ++sym; \
  7811. *symp = sym; \
  7812. if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \
  7813. isymbuf, locsymcount, signed_p)) \
  7814. return false; \
  7815. ++*symp; \
  7816. if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \
  7817. isymbuf, locsymcount, signed_p)) \
  7818. return false;
  7819. #define BINARY_OP_TAIL(op) \
  7820. if (signed_p) \
  7821. *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \
  7822. else \
  7823. *result = a op b; \
  7824. return true; \
  7825. }
  7826. #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op)
  7827. default:
  7828. UNARY_OP (0-);
  7829. BINARY_OP_HEAD (<<);
  7830. if (b >= sizeof (a) * CHAR_BIT)
  7831. {
  7832. *result = 0;
  7833. return true;
  7834. }
  7835. signed_p = 0;
  7836. BINARY_OP_TAIL (<<);
  7837. BINARY_OP_HEAD (>>);
  7838. if (b >= sizeof (a) * CHAR_BIT)
  7839. {
  7840. *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0;
  7841. return true;
  7842. }
  7843. BINARY_OP_TAIL (>>);
  7844. BINARY_OP (==);
  7845. BINARY_OP (!=);
  7846. BINARY_OP (<=);
  7847. BINARY_OP (>=);
  7848. BINARY_OP (&&);
  7849. BINARY_OP (||);
  7850. UNARY_OP (~);
  7851. UNARY_OP (!);
  7852. BINARY_OP (*);
  7853. BINARY_OP_HEAD (/);
  7854. if (b == 0)
  7855. {
  7856. _bfd_error_handler (_("division by zero"));
  7857. bfd_set_error (bfd_error_bad_value);
  7858. return false;
  7859. }
  7860. BINARY_OP_TAIL (/);
  7861. BINARY_OP_HEAD (%);
  7862. if (b == 0)
  7863. {
  7864. _bfd_error_handler (_("division by zero"));
  7865. bfd_set_error (bfd_error_bad_value);
  7866. return false;
  7867. }
  7868. BINARY_OP_TAIL (%);
  7869. BINARY_OP (^);
  7870. BINARY_OP (|);
  7871. BINARY_OP (&);
  7872. BINARY_OP (+);
  7873. BINARY_OP (-);
  7874. BINARY_OP (<);
  7875. BINARY_OP (>);
  7876. #undef UNARY_OP
  7877. #undef BINARY_OP
  7878. _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym);
  7879. bfd_set_error (bfd_error_invalid_operation);
  7880. return false;
  7881. }
  7882. }
  7883. static void
  7884. put_value (bfd_vma size,
  7885. unsigned long chunksz,
  7886. bfd *input_bfd,
  7887. bfd_vma x,
  7888. bfd_byte *location)
  7889. {
  7890. location += (size - chunksz);
  7891. for (; size; size -= chunksz, location -= chunksz)
  7892. {
  7893. switch (chunksz)
  7894. {
  7895. case 1:
  7896. bfd_put_8 (input_bfd, x, location);
  7897. x >>= 8;
  7898. break;
  7899. case 2:
  7900. bfd_put_16 (input_bfd, x, location);
  7901. x >>= 16;
  7902. break;
  7903. case 4:
  7904. bfd_put_32 (input_bfd, x, location);
  7905. /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */
  7906. x >>= 16;
  7907. x >>= 16;
  7908. break;
  7909. #ifdef BFD64
  7910. case 8:
  7911. bfd_put_64 (input_bfd, x, location);
  7912. /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */
  7913. x >>= 32;
  7914. x >>= 32;
  7915. break;
  7916. #endif
  7917. default:
  7918. abort ();
  7919. break;
  7920. }
  7921. }
  7922. }
  7923. static bfd_vma
  7924. get_value (bfd_vma size,
  7925. unsigned long chunksz,
  7926. bfd *input_bfd,
  7927. bfd_byte *location)
  7928. {
  7929. int shift;
  7930. bfd_vma x = 0;
  7931. /* Sanity checks. */
  7932. BFD_ASSERT (chunksz <= sizeof (x)
  7933. && size >= chunksz
  7934. && chunksz != 0
  7935. && (size % chunksz) == 0
  7936. && input_bfd != NULL
  7937. && location != NULL);
  7938. if (chunksz == sizeof (x))
  7939. {
  7940. BFD_ASSERT (size == chunksz);
  7941. /* Make sure that we do not perform an undefined shift operation.
  7942. We know that size == chunksz so there will only be one iteration
  7943. of the loop below. */
  7944. shift = 0;
  7945. }
  7946. else
  7947. shift = 8 * chunksz;
  7948. for (; size; size -= chunksz, location += chunksz)
  7949. {
  7950. switch (chunksz)
  7951. {
  7952. case 1:
  7953. x = (x << shift) | bfd_get_8 (input_bfd, location);
  7954. break;
  7955. case 2:
  7956. x = (x << shift) | bfd_get_16 (input_bfd, location);
  7957. break;
  7958. case 4:
  7959. x = (x << shift) | bfd_get_32 (input_bfd, location);
  7960. break;
  7961. #ifdef BFD64
  7962. case 8:
  7963. x = (x << shift) | bfd_get_64 (input_bfd, location);
  7964. break;
  7965. #endif
  7966. default:
  7967. abort ();
  7968. }
  7969. }
  7970. return x;
  7971. }
  7972. static void
  7973. decode_complex_addend (unsigned long *start, /* in bits */
  7974. unsigned long *oplen, /* in bits */
  7975. unsigned long *len, /* in bits */
  7976. unsigned long *wordsz, /* in bytes */
  7977. unsigned long *chunksz, /* in bytes */
  7978. unsigned long *lsb0_p,
  7979. unsigned long *signed_p,
  7980. unsigned long *trunc_p,
  7981. unsigned long encoded)
  7982. {
  7983. * start = encoded & 0x3F;
  7984. * len = (encoded >> 6) & 0x3F;
  7985. * oplen = (encoded >> 12) & 0x3F;
  7986. * wordsz = (encoded >> 18) & 0xF;
  7987. * chunksz = (encoded >> 22) & 0xF;
  7988. * lsb0_p = (encoded >> 27) & 1;
  7989. * signed_p = (encoded >> 28) & 1;
  7990. * trunc_p = (encoded >> 29) & 1;
  7991. }
  7992. bfd_reloc_status_type
  7993. bfd_elf_perform_complex_relocation (bfd *input_bfd,
  7994. asection *input_section,
  7995. bfd_byte *contents,
  7996. Elf_Internal_Rela *rel,
  7997. bfd_vma relocation)
  7998. {
  7999. bfd_vma shift, x, mask;
  8000. unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p;
  8001. bfd_reloc_status_type r;
  8002. bfd_size_type octets;
  8003. /* Perform this reloc, since it is complex.
  8004. (this is not to say that it necessarily refers to a complex
  8005. symbol; merely that it is a self-describing CGEN based reloc.
  8006. i.e. the addend has the complete reloc information (bit start, end,
  8007. word size, etc) encoded within it.). */
  8008. decode_complex_addend (&start, &oplen, &len, &wordsz,
  8009. &chunksz, &lsb0_p, &signed_p,
  8010. &trunc_p, rel->r_addend);
  8011. mask = (((1L << (len - 1)) - 1) << 1) | 1;
  8012. if (lsb0_p)
  8013. shift = (start + 1) - len;
  8014. else
  8015. shift = (8 * wordsz) - (start + len);
  8016. octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section);
  8017. x = get_value (wordsz, chunksz, input_bfd, contents + octets);
  8018. #ifdef DEBUG
  8019. printf ("Doing complex reloc: "
  8020. "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, "
  8021. "chunksz %ld, start %ld, len %ld, oplen %ld\n"
  8022. " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n",
  8023. lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len,
  8024. oplen, (unsigned long) x, (unsigned long) mask,
  8025. (unsigned long) relocation);
  8026. #endif
  8027. r = bfd_reloc_ok;
  8028. if (! trunc_p)
  8029. /* Now do an overflow check. */
  8030. r = bfd_check_overflow ((signed_p
  8031. ? complain_overflow_signed
  8032. : complain_overflow_unsigned),
  8033. len, 0, (8 * wordsz),
  8034. relocation);
  8035. /* Do the deed. */
  8036. x = (x & ~(mask << shift)) | ((relocation & mask) << shift);
  8037. #ifdef DEBUG
  8038. printf (" relocation: %8.8lx\n"
  8039. " shifted mask: %8.8lx\n"
  8040. " shifted/masked reloc: %8.8lx\n"
  8041. " result: %8.8lx\n",
  8042. (unsigned long) relocation, (unsigned long) (mask << shift),
  8043. (unsigned long) ((relocation & mask) << shift), (unsigned long) x);
  8044. #endif
  8045. put_value (wordsz, chunksz, input_bfd, x, contents + octets);
  8046. return r;
  8047. }
  8048. /* Functions to read r_offset from external (target order) reloc
  8049. entry. Faster than bfd_getl32 et al, because we let the compiler
  8050. know the value is aligned. */
  8051. static bfd_vma
  8052. ext32l_r_offset (const void *p)
  8053. {
  8054. union aligned32
  8055. {
  8056. uint32_t v;
  8057. unsigned char c[4];
  8058. };
  8059. const union aligned32 *a
  8060. = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
  8061. uint32_t aval = ( (uint32_t) a->c[0]
  8062. | (uint32_t) a->c[1] << 8
  8063. | (uint32_t) a->c[2] << 16
  8064. | (uint32_t) a->c[3] << 24);
  8065. return aval;
  8066. }
  8067. static bfd_vma
  8068. ext32b_r_offset (const void *p)
  8069. {
  8070. union aligned32
  8071. {
  8072. uint32_t v;
  8073. unsigned char c[4];
  8074. };
  8075. const union aligned32 *a
  8076. = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset;
  8077. uint32_t aval = ( (uint32_t) a->c[0] << 24
  8078. | (uint32_t) a->c[1] << 16
  8079. | (uint32_t) a->c[2] << 8
  8080. | (uint32_t) a->c[3]);
  8081. return aval;
  8082. }
  8083. #ifdef BFD_HOST_64_BIT
  8084. static bfd_vma
  8085. ext64l_r_offset (const void *p)
  8086. {
  8087. union aligned64
  8088. {
  8089. uint64_t v;
  8090. unsigned char c[8];
  8091. };
  8092. const union aligned64 *a
  8093. = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
  8094. uint64_t aval = ( (uint64_t) a->c[0]
  8095. | (uint64_t) a->c[1] << 8
  8096. | (uint64_t) a->c[2] << 16
  8097. | (uint64_t) a->c[3] << 24
  8098. | (uint64_t) a->c[4] << 32
  8099. | (uint64_t) a->c[5] << 40
  8100. | (uint64_t) a->c[6] << 48
  8101. | (uint64_t) a->c[7] << 56);
  8102. return aval;
  8103. }
  8104. static bfd_vma
  8105. ext64b_r_offset (const void *p)
  8106. {
  8107. union aligned64
  8108. {
  8109. uint64_t v;
  8110. unsigned char c[8];
  8111. };
  8112. const union aligned64 *a
  8113. = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset;
  8114. uint64_t aval = ( (uint64_t) a->c[0] << 56
  8115. | (uint64_t) a->c[1] << 48
  8116. | (uint64_t) a->c[2] << 40
  8117. | (uint64_t) a->c[3] << 32
  8118. | (uint64_t) a->c[4] << 24
  8119. | (uint64_t) a->c[5] << 16
  8120. | (uint64_t) a->c[6] << 8
  8121. | (uint64_t) a->c[7]);
  8122. return aval;
  8123. }
  8124. #endif
  8125. /* When performing a relocatable link, the input relocations are
  8126. preserved. But, if they reference global symbols, the indices
  8127. referenced must be updated. Update all the relocations found in
  8128. RELDATA. */
  8129. static bool
  8130. elf_link_adjust_relocs (bfd *abfd,
  8131. asection *sec,
  8132. struct bfd_elf_section_reloc_data *reldata,
  8133. bool sort,
  8134. struct bfd_link_info *info)
  8135. {
  8136. unsigned int i;
  8137. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  8138. bfd_byte *erela;
  8139. void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
  8140. void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
  8141. bfd_vma r_type_mask;
  8142. int r_sym_shift;
  8143. unsigned int count = reldata->count;
  8144. struct elf_link_hash_entry **rel_hash = reldata->hashes;
  8145. if (reldata->hdr->sh_entsize == bed->s->sizeof_rel)
  8146. {
  8147. swap_in = bed->s->swap_reloc_in;
  8148. swap_out = bed->s->swap_reloc_out;
  8149. }
  8150. else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela)
  8151. {
  8152. swap_in = bed->s->swap_reloca_in;
  8153. swap_out = bed->s->swap_reloca_out;
  8154. }
  8155. else
  8156. abort ();
  8157. if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL)
  8158. abort ();
  8159. if (bed->s->arch_size == 32)
  8160. {
  8161. r_type_mask = 0xff;
  8162. r_sym_shift = 8;
  8163. }
  8164. else
  8165. {
  8166. r_type_mask = 0xffffffff;
  8167. r_sym_shift = 32;
  8168. }
  8169. erela = reldata->hdr->contents;
  8170. for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize)
  8171. {
  8172. Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL];
  8173. unsigned int j;
  8174. if (*rel_hash == NULL)
  8175. continue;
  8176. if ((*rel_hash)->indx == -2
  8177. && info->gc_sections
  8178. && ! info->gc_keep_exported)
  8179. {
  8180. /* PR 21524: Let the user know if a symbol was removed by garbage collection. */
  8181. _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"),
  8182. abfd, sec,
  8183. (*rel_hash)->root.root.string);
  8184. _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"),
  8185. abfd, sec);
  8186. bfd_set_error (bfd_error_invalid_operation);
  8187. return false;
  8188. }
  8189. BFD_ASSERT ((*rel_hash)->indx >= 0);
  8190. (*swap_in) (abfd, erela, irela);
  8191. for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
  8192. irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift
  8193. | (irela[j].r_info & r_type_mask));
  8194. (*swap_out) (abfd, irela, erela);
  8195. }
  8196. if (bed->elf_backend_update_relocs)
  8197. (*bed->elf_backend_update_relocs) (sec, reldata);
  8198. if (sort && count != 0)
  8199. {
  8200. bfd_vma (*ext_r_off) (const void *);
  8201. bfd_vma r_off;
  8202. size_t elt_size;
  8203. bfd_byte *base, *end, *p, *loc;
  8204. bfd_byte *buf = NULL;
  8205. if (bed->s->arch_size == 32)
  8206. {
  8207. if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
  8208. ext_r_off = ext32l_r_offset;
  8209. else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
  8210. ext_r_off = ext32b_r_offset;
  8211. else
  8212. abort ();
  8213. }
  8214. else
  8215. {
  8216. #ifdef BFD_HOST_64_BIT
  8217. if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)
  8218. ext_r_off = ext64l_r_offset;
  8219. else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
  8220. ext_r_off = ext64b_r_offset;
  8221. else
  8222. #endif
  8223. abort ();
  8224. }
  8225. /* Must use a stable sort here. A modified insertion sort,
  8226. since the relocs are mostly sorted already. */
  8227. elt_size = reldata->hdr->sh_entsize;
  8228. base = reldata->hdr->contents;
  8229. end = base + count * elt_size;
  8230. if (elt_size > sizeof (Elf64_External_Rela))
  8231. abort ();
  8232. /* Ensure the first element is lowest. This acts as a sentinel,
  8233. speeding the main loop below. */
  8234. r_off = (*ext_r_off) (base);
  8235. for (p = loc = base; (p += elt_size) < end; )
  8236. {
  8237. bfd_vma r_off2 = (*ext_r_off) (p);
  8238. if (r_off > r_off2)
  8239. {
  8240. r_off = r_off2;
  8241. loc = p;
  8242. }
  8243. }
  8244. if (loc != base)
  8245. {
  8246. /* Don't just swap *base and *loc as that changes the order
  8247. of the original base[0] and base[1] if they happen to
  8248. have the same r_offset. */
  8249. bfd_byte onebuf[sizeof (Elf64_External_Rela)];
  8250. memcpy (onebuf, loc, elt_size);
  8251. memmove (base + elt_size, base, loc - base);
  8252. memcpy (base, onebuf, elt_size);
  8253. }
  8254. for (p = base + elt_size; (p += elt_size) < end; )
  8255. {
  8256. /* base to p is sorted, *p is next to insert. */
  8257. r_off = (*ext_r_off) (p);
  8258. /* Search the sorted region for location to insert. */
  8259. loc = p - elt_size;
  8260. while (r_off < (*ext_r_off) (loc))
  8261. loc -= elt_size;
  8262. loc += elt_size;
  8263. if (loc != p)
  8264. {
  8265. /* Chances are there is a run of relocs to insert here,
  8266. from one of more input files. Files are not always
  8267. linked in order due to the way elf_link_input_bfd is
  8268. called. See pr17666. */
  8269. size_t sortlen = p - loc;
  8270. bfd_vma r_off2 = (*ext_r_off) (loc);
  8271. size_t runlen = elt_size;
  8272. size_t buf_size = 96 * 1024;
  8273. while (p + runlen < end
  8274. && (sortlen <= buf_size
  8275. || runlen + elt_size <= buf_size)
  8276. && r_off2 > (*ext_r_off) (p + runlen))
  8277. runlen += elt_size;
  8278. if (buf == NULL)
  8279. {
  8280. buf = bfd_malloc (buf_size);
  8281. if (buf == NULL)
  8282. return false;
  8283. }
  8284. if (runlen < sortlen)
  8285. {
  8286. memcpy (buf, p, runlen);
  8287. memmove (loc + runlen, loc, sortlen);
  8288. memcpy (loc, buf, runlen);
  8289. }
  8290. else
  8291. {
  8292. memcpy (buf, loc, sortlen);
  8293. memmove (loc, p, runlen);
  8294. memcpy (loc + runlen, buf, sortlen);
  8295. }
  8296. p += runlen - elt_size;
  8297. }
  8298. }
  8299. /* Hashes are no longer valid. */
  8300. free (reldata->hashes);
  8301. reldata->hashes = NULL;
  8302. free (buf);
  8303. }
  8304. return true;
  8305. }
  8306. struct elf_link_sort_rela
  8307. {
  8308. union {
  8309. bfd_vma offset;
  8310. bfd_vma sym_mask;
  8311. } u;
  8312. enum elf_reloc_type_class type;
  8313. /* We use this as an array of size int_rels_per_ext_rel. */
  8314. Elf_Internal_Rela rela[1];
  8315. };
  8316. /* qsort stability here and for cmp2 is only an issue if multiple
  8317. dynamic relocations are emitted at the same address. But targets
  8318. that apply a series of dynamic relocations each operating on the
  8319. result of the prior relocation can't use -z combreloc as
  8320. implemented anyway. Such schemes tend to be broken by sorting on
  8321. symbol index. That leaves dynamic NONE relocs as the only other
  8322. case where ld might emit multiple relocs at the same address, and
  8323. those are only emitted due to target bugs. */
  8324. static int
  8325. elf_link_sort_cmp1 (const void *A, const void *B)
  8326. {
  8327. const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
  8328. const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
  8329. int relativea, relativeb;
  8330. relativea = a->type == reloc_class_relative;
  8331. relativeb = b->type == reloc_class_relative;
  8332. if (relativea < relativeb)
  8333. return 1;
  8334. if (relativea > relativeb)
  8335. return -1;
  8336. if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask))
  8337. return -1;
  8338. if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask))
  8339. return 1;
  8340. if (a->rela->r_offset < b->rela->r_offset)
  8341. return -1;
  8342. if (a->rela->r_offset > b->rela->r_offset)
  8343. return 1;
  8344. return 0;
  8345. }
  8346. static int
  8347. elf_link_sort_cmp2 (const void *A, const void *B)
  8348. {
  8349. const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A;
  8350. const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B;
  8351. if (a->type < b->type)
  8352. return -1;
  8353. if (a->type > b->type)
  8354. return 1;
  8355. if (a->u.offset < b->u.offset)
  8356. return -1;
  8357. if (a->u.offset > b->u.offset)
  8358. return 1;
  8359. if (a->rela->r_offset < b->rela->r_offset)
  8360. return -1;
  8361. if (a->rela->r_offset > b->rela->r_offset)
  8362. return 1;
  8363. return 0;
  8364. }
  8365. static size_t
  8366. elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec)
  8367. {
  8368. asection *dynamic_relocs;
  8369. asection *rela_dyn;
  8370. asection *rel_dyn;
  8371. bfd_size_type count, size;
  8372. size_t i, ret, sort_elt, ext_size;
  8373. bfd_byte *sort, *s_non_relative, *p;
  8374. struct elf_link_sort_rela *sq;
  8375. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  8376. int i2e = bed->s->int_rels_per_ext_rel;
  8377. unsigned int opb = bfd_octets_per_byte (abfd, NULL);
  8378. void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *);
  8379. void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
  8380. struct bfd_link_order *lo;
  8381. bfd_vma r_sym_mask;
  8382. bool use_rela;
  8383. /* Find a dynamic reloc section. */
  8384. rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn");
  8385. rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn");
  8386. if (rela_dyn != NULL && rela_dyn->size > 0
  8387. && rel_dyn != NULL && rel_dyn->size > 0)
  8388. {
  8389. bool use_rela_initialised = false;
  8390. /* This is just here to stop gcc from complaining.
  8391. Its initialization checking code is not perfect. */
  8392. use_rela = true;
  8393. /* Both sections are present. Examine the sizes
  8394. of the indirect sections to help us choose. */
  8395. for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next)
  8396. if (lo->type == bfd_indirect_link_order)
  8397. {
  8398. asection *o = lo->u.indirect.section;
  8399. if ((o->size % bed->s->sizeof_rela) == 0)
  8400. {
  8401. if ((o->size % bed->s->sizeof_rel) == 0)
  8402. /* Section size is divisible by both rel and rela sizes.
  8403. It is of no help to us. */
  8404. ;
  8405. else
  8406. {
  8407. /* Section size is only divisible by rela. */
  8408. if (use_rela_initialised && !use_rela)
  8409. {
  8410. _bfd_error_handler (_("%pB: unable to sort relocs - "
  8411. "they are in more than one size"),
  8412. abfd);
  8413. bfd_set_error (bfd_error_invalid_operation);
  8414. return 0;
  8415. }
  8416. else
  8417. {
  8418. use_rela = true;
  8419. use_rela_initialised = true;
  8420. }
  8421. }
  8422. }
  8423. else if ((o->size % bed->s->sizeof_rel) == 0)
  8424. {
  8425. /* Section size is only divisible by rel. */
  8426. if (use_rela_initialised && use_rela)
  8427. {
  8428. _bfd_error_handler (_("%pB: unable to sort relocs - "
  8429. "they are in more than one size"),
  8430. abfd);
  8431. bfd_set_error (bfd_error_invalid_operation);
  8432. return 0;
  8433. }
  8434. else
  8435. {
  8436. use_rela = false;
  8437. use_rela_initialised = true;
  8438. }
  8439. }
  8440. else
  8441. {
  8442. /* The section size is not divisible by either -
  8443. something is wrong. */
  8444. _bfd_error_handler (_("%pB: unable to sort relocs - "
  8445. "they are of an unknown size"), abfd);
  8446. bfd_set_error (bfd_error_invalid_operation);
  8447. return 0;
  8448. }
  8449. }
  8450. for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next)
  8451. if (lo->type == bfd_indirect_link_order)
  8452. {
  8453. asection *o = lo->u.indirect.section;
  8454. if ((o->size % bed->s->sizeof_rela) == 0)
  8455. {
  8456. if ((o->size % bed->s->sizeof_rel) == 0)
  8457. /* Section size is divisible by both rel and rela sizes.
  8458. It is of no help to us. */
  8459. ;
  8460. else
  8461. {
  8462. /* Section size is only divisible by rela. */
  8463. if (use_rela_initialised && !use_rela)
  8464. {
  8465. _bfd_error_handler (_("%pB: unable to sort relocs - "
  8466. "they are in more than one size"),
  8467. abfd);
  8468. bfd_set_error (bfd_error_invalid_operation);
  8469. return 0;
  8470. }
  8471. else
  8472. {
  8473. use_rela = true;
  8474. use_rela_initialised = true;
  8475. }
  8476. }
  8477. }
  8478. else if ((o->size % bed->s->sizeof_rel) == 0)
  8479. {
  8480. /* Section size is only divisible by rel. */
  8481. if (use_rela_initialised && use_rela)
  8482. {
  8483. _bfd_error_handler (_("%pB: unable to sort relocs - "
  8484. "they are in more than one size"),
  8485. abfd);
  8486. bfd_set_error (bfd_error_invalid_operation);
  8487. return 0;
  8488. }
  8489. else
  8490. {
  8491. use_rela = false;
  8492. use_rela_initialised = true;
  8493. }
  8494. }
  8495. else
  8496. {
  8497. /* The section size is not divisible by either -
  8498. something is wrong. */
  8499. _bfd_error_handler (_("%pB: unable to sort relocs - "
  8500. "they are of an unknown size"), abfd);
  8501. bfd_set_error (bfd_error_invalid_operation);
  8502. return 0;
  8503. }
  8504. }
  8505. if (! use_rela_initialised)
  8506. /* Make a guess. */
  8507. use_rela = true;
  8508. }
  8509. else if (rela_dyn != NULL && rela_dyn->size > 0)
  8510. use_rela = true;
  8511. else if (rel_dyn != NULL && rel_dyn->size > 0)
  8512. use_rela = false;
  8513. else
  8514. return 0;
  8515. if (use_rela)
  8516. {
  8517. dynamic_relocs = rela_dyn;
  8518. ext_size = bed->s->sizeof_rela;
  8519. swap_in = bed->s->swap_reloca_in;
  8520. swap_out = bed->s->swap_reloca_out;
  8521. }
  8522. else
  8523. {
  8524. dynamic_relocs = rel_dyn;
  8525. ext_size = bed->s->sizeof_rel;
  8526. swap_in = bed->s->swap_reloc_in;
  8527. swap_out = bed->s->swap_reloc_out;
  8528. }
  8529. size = 0;
  8530. for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
  8531. if (lo->type == bfd_indirect_link_order)
  8532. size += lo->u.indirect.section->size;
  8533. if (size != dynamic_relocs->size)
  8534. return 0;
  8535. sort_elt = (sizeof (struct elf_link_sort_rela)
  8536. + (i2e - 1) * sizeof (Elf_Internal_Rela));
  8537. count = dynamic_relocs->size / ext_size;
  8538. if (count == 0)
  8539. return 0;
  8540. sort = (bfd_byte *) bfd_zmalloc (sort_elt * count);
  8541. if (sort == NULL)
  8542. {
  8543. (*info->callbacks->warning)
  8544. (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0);
  8545. return 0;
  8546. }
  8547. if (bed->s->arch_size == 32)
  8548. r_sym_mask = ~(bfd_vma) 0xff;
  8549. else
  8550. r_sym_mask = ~(bfd_vma) 0xffffffff;
  8551. for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
  8552. if (lo->type == bfd_indirect_link_order)
  8553. {
  8554. bfd_byte *erel, *erelend;
  8555. asection *o = lo->u.indirect.section;
  8556. if (o->contents == NULL && o->size != 0)
  8557. {
  8558. /* This is a reloc section that is being handled as a normal
  8559. section. See bfd_section_from_shdr. We can't combine
  8560. relocs in this case. */
  8561. free (sort);
  8562. return 0;
  8563. }
  8564. erel = o->contents;
  8565. erelend = o->contents + o->size;
  8566. p = sort + o->output_offset * opb / ext_size * sort_elt;
  8567. while (erel < erelend)
  8568. {
  8569. struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
  8570. (*swap_in) (abfd, erel, s->rela);
  8571. s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela);
  8572. s->u.sym_mask = r_sym_mask;
  8573. p += sort_elt;
  8574. erel += ext_size;
  8575. }
  8576. }
  8577. qsort (sort, count, sort_elt, elf_link_sort_cmp1);
  8578. for (i = 0, p = sort; i < count; i++, p += sort_elt)
  8579. {
  8580. struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
  8581. if (s->type != reloc_class_relative)
  8582. break;
  8583. }
  8584. ret = i;
  8585. s_non_relative = p;
  8586. sq = (struct elf_link_sort_rela *) s_non_relative;
  8587. for (; i < count; i++, p += sort_elt)
  8588. {
  8589. struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p;
  8590. if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0)
  8591. sq = sp;
  8592. sp->u.offset = sq->rela->r_offset;
  8593. }
  8594. qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2);
  8595. struct elf_link_hash_table *htab = elf_hash_table (info);
  8596. if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs)
  8597. {
  8598. /* We have plt relocs in .rela.dyn. */
  8599. sq = (struct elf_link_sort_rela *) sort;
  8600. for (i = 0; i < count; i++)
  8601. if (sq[count - i - 1].type != reloc_class_plt)
  8602. break;
  8603. if (i != 0 && htab->srelplt->size == i * ext_size)
  8604. {
  8605. struct bfd_link_order **plo;
  8606. /* Put srelplt link_order last. This is so the output_offset
  8607. set in the next loop is correct for DT_JMPREL. */
  8608. for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; )
  8609. if ((*plo)->type == bfd_indirect_link_order
  8610. && (*plo)->u.indirect.section == htab->srelplt)
  8611. {
  8612. lo = *plo;
  8613. *plo = lo->next;
  8614. }
  8615. else
  8616. plo = &(*plo)->next;
  8617. *plo = lo;
  8618. lo->next = NULL;
  8619. dynamic_relocs->map_tail.link_order = lo;
  8620. }
  8621. }
  8622. p = sort;
  8623. for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next)
  8624. if (lo->type == bfd_indirect_link_order)
  8625. {
  8626. bfd_byte *erel, *erelend;
  8627. asection *o = lo->u.indirect.section;
  8628. erel = o->contents;
  8629. erelend = o->contents + o->size;
  8630. o->output_offset = (p - sort) / sort_elt * ext_size / opb;
  8631. while (erel < erelend)
  8632. {
  8633. struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p;
  8634. (*swap_out) (abfd, s->rela, erel);
  8635. p += sort_elt;
  8636. erel += ext_size;
  8637. }
  8638. }
  8639. free (sort);
  8640. *psec = dynamic_relocs;
  8641. return ret;
  8642. }
  8643. /* Add a symbol to the output symbol string table. */
  8644. static int
  8645. elf_link_output_symstrtab (void *finf,
  8646. const char *name,
  8647. Elf_Internal_Sym *elfsym,
  8648. asection *input_sec,
  8649. struct elf_link_hash_entry *h)
  8650. {
  8651. struct elf_final_link_info *flinfo = finf;
  8652. int (*output_symbol_hook)
  8653. (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
  8654. struct elf_link_hash_entry *);
  8655. struct elf_link_hash_table *hash_table;
  8656. const struct elf_backend_data *bed;
  8657. bfd_size_type strtabsize;
  8658. BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
  8659. bed = get_elf_backend_data (flinfo->output_bfd);
  8660. output_symbol_hook = bed->elf_backend_link_output_symbol_hook;
  8661. if (output_symbol_hook != NULL)
  8662. {
  8663. int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h);
  8664. if (ret != 1)
  8665. return ret;
  8666. }
  8667. if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC)
  8668. elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
  8669. if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE)
  8670. elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique;
  8671. if (name == NULL
  8672. || *name == '\0'
  8673. || (input_sec->flags & SEC_EXCLUDE))
  8674. elfsym->st_name = (unsigned long) -1;
  8675. else
  8676. {
  8677. /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
  8678. to get the final offset for st_name. */
  8679. char *versioned_name = (char *) name;
  8680. if (h != NULL)
  8681. {
  8682. if (h->versioned == versioned && h->def_dynamic)
  8683. {
  8684. /* Keep only one '@' for versioned symbols defined in
  8685. shared objects. */
  8686. char *version = strrchr (name, ELF_VER_CHR);
  8687. char *base_end = strchr (name, ELF_VER_CHR);
  8688. if (version != base_end)
  8689. {
  8690. size_t base_len;
  8691. size_t len = strlen (name);
  8692. versioned_name = bfd_alloc (flinfo->output_bfd, len);
  8693. if (versioned_name == NULL)
  8694. return 0;
  8695. base_len = base_end - name;
  8696. memcpy (versioned_name, name, base_len);
  8697. memcpy (versioned_name + base_len, version,
  8698. len - base_len);
  8699. }
  8700. }
  8701. }
  8702. else if (flinfo->info->unique_symbol
  8703. && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
  8704. {
  8705. struct local_hash_entry *lh;
  8706. size_t count_len;
  8707. size_t base_len;
  8708. char buf[30];
  8709. switch (ELF_ST_TYPE (elfsym->st_info))
  8710. {
  8711. case STT_FILE:
  8712. case STT_SECTION:
  8713. break;
  8714. default:
  8715. lh = (struct local_hash_entry *) bfd_hash_lookup
  8716. (&flinfo->local_hash_table, name, true, false);
  8717. if (lh == NULL)
  8718. return 0;
  8719. /* Always append ".COUNT" to local symbols to avoid
  8720. potential conflicts with local symbol "XXX.COUNT". */
  8721. sprintf (buf, "%lx", lh->count);
  8722. base_len = lh->size;
  8723. if (!base_len)
  8724. {
  8725. base_len = strlen (name);
  8726. lh->size = base_len;
  8727. }
  8728. count_len = strlen (buf);
  8729. versioned_name = bfd_alloc (flinfo->output_bfd,
  8730. base_len + count_len + 2);
  8731. if (versioned_name == NULL)
  8732. return 0;
  8733. memcpy (versioned_name, name, base_len);
  8734. versioned_name[base_len] = '.';
  8735. memcpy (versioned_name + base_len + 1, buf,
  8736. count_len + 1);
  8737. lh->count++;
  8738. break;
  8739. }
  8740. }
  8741. elfsym->st_name
  8742. = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab,
  8743. versioned_name, false);
  8744. if (elfsym->st_name == (unsigned long) -1)
  8745. return 0;
  8746. }
  8747. hash_table = elf_hash_table (flinfo->info);
  8748. strtabsize = hash_table->strtabsize;
  8749. if (strtabsize <= flinfo->output_bfd->symcount)
  8750. {
  8751. strtabsize += strtabsize;
  8752. hash_table->strtabsize = strtabsize;
  8753. strtabsize *= sizeof (*hash_table->strtab);
  8754. hash_table->strtab
  8755. = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab,
  8756. strtabsize);
  8757. if (hash_table->strtab == NULL)
  8758. return 0;
  8759. }
  8760. hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym;
  8761. hash_table->strtab[flinfo->output_bfd->symcount].dest_index
  8762. = flinfo->output_bfd->symcount;
  8763. flinfo->output_bfd->symcount += 1;
  8764. return 1;
  8765. }
  8766. /* Swap symbols out to the symbol table and flush the output symbols to
  8767. the file. */
  8768. static bool
  8769. elf_link_swap_symbols_out (struct elf_final_link_info *flinfo)
  8770. {
  8771. struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info);
  8772. size_t amt;
  8773. size_t i;
  8774. const struct elf_backend_data *bed;
  8775. bfd_byte *symbuf;
  8776. Elf_Internal_Shdr *hdr;
  8777. file_ptr pos;
  8778. bool ret;
  8779. if (flinfo->output_bfd->symcount == 0)
  8780. return true;
  8781. BFD_ASSERT (elf_onesymtab (flinfo->output_bfd));
  8782. bed = get_elf_backend_data (flinfo->output_bfd);
  8783. amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
  8784. symbuf = (bfd_byte *) bfd_malloc (amt);
  8785. if (symbuf == NULL)
  8786. return false;
  8787. if (flinfo->symshndxbuf)
  8788. {
  8789. amt = sizeof (Elf_External_Sym_Shndx);
  8790. amt *= bfd_get_symcount (flinfo->output_bfd);
  8791. flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt);
  8792. if (flinfo->symshndxbuf == NULL)
  8793. {
  8794. free (symbuf);
  8795. return false;
  8796. }
  8797. }
  8798. /* Now swap out the symbols. */
  8799. for (i = 0; i < flinfo->output_bfd->symcount; i++)
  8800. {
  8801. struct elf_sym_strtab *elfsym = &hash_table->strtab[i];
  8802. if (elfsym->sym.st_name == (unsigned long) -1)
  8803. elfsym->sym.st_name = 0;
  8804. else
  8805. elfsym->sym.st_name
  8806. = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab,
  8807. elfsym->sym.st_name);
  8808. /* Inform the linker of the addition of this symbol. */
  8809. if (flinfo->info->callbacks->ctf_new_symbol)
  8810. flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index,
  8811. &elfsym->sym);
  8812. bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym,
  8813. ((bfd_byte *) symbuf
  8814. + (elfsym->dest_index
  8815. * bed->s->sizeof_sym)),
  8816. NPTR_ADD (flinfo->symshndxbuf,
  8817. elfsym->dest_index));
  8818. }
  8819. hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
  8820. pos = hdr->sh_offset + hdr->sh_size;
  8821. amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount;
  8822. if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0
  8823. && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt)
  8824. {
  8825. hdr->sh_size += amt;
  8826. ret = true;
  8827. }
  8828. else
  8829. ret = false;
  8830. free (symbuf);
  8831. free (hash_table->strtab);
  8832. hash_table->strtab = NULL;
  8833. return ret;
  8834. }
  8835. /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */
  8836. static bool
  8837. check_dynsym (bfd *abfd, Elf_Internal_Sym *sym)
  8838. {
  8839. if (sym->st_shndx >= (SHN_LORESERVE & 0xffff)
  8840. && sym->st_shndx < SHN_LORESERVE)
  8841. {
  8842. /* The gABI doesn't support dynamic symbols in output sections
  8843. beyond 64k. */
  8844. _bfd_error_handler
  8845. /* xgettext:c-format */
  8846. (_("%pB: too many sections: %d (>= %d)"),
  8847. abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff);
  8848. bfd_set_error (bfd_error_nonrepresentable_section);
  8849. return false;
  8850. }
  8851. return true;
  8852. }
  8853. /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in
  8854. allowing an unsatisfied unversioned symbol in the DSO to match a
  8855. versioned symbol that would normally require an explicit version.
  8856. We also handle the case that a DSO references a hidden symbol
  8857. which may be satisfied by a versioned symbol in another DSO. */
  8858. static bool
  8859. elf_link_check_versioned_symbol (struct bfd_link_info *info,
  8860. const struct elf_backend_data *bed,
  8861. struct elf_link_hash_entry *h)
  8862. {
  8863. bfd *abfd;
  8864. struct elf_link_loaded_list *loaded;
  8865. if (!is_elf_hash_table (info->hash))
  8866. return false;
  8867. /* Check indirect symbol. */
  8868. while (h->root.type == bfd_link_hash_indirect)
  8869. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  8870. switch (h->root.type)
  8871. {
  8872. default:
  8873. abfd = NULL;
  8874. break;
  8875. case bfd_link_hash_undefined:
  8876. case bfd_link_hash_undefweak:
  8877. abfd = h->root.u.undef.abfd;
  8878. if (abfd == NULL
  8879. || (abfd->flags & DYNAMIC) == 0
  8880. || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0)
  8881. return false;
  8882. break;
  8883. case bfd_link_hash_defined:
  8884. case bfd_link_hash_defweak:
  8885. abfd = h->root.u.def.section->owner;
  8886. break;
  8887. case bfd_link_hash_common:
  8888. abfd = h->root.u.c.p->section->owner;
  8889. break;
  8890. }
  8891. BFD_ASSERT (abfd != NULL);
  8892. for (loaded = elf_hash_table (info)->dyn_loaded;
  8893. loaded != NULL;
  8894. loaded = loaded->next)
  8895. {
  8896. bfd *input;
  8897. Elf_Internal_Shdr *hdr;
  8898. size_t symcount;
  8899. size_t extsymcount;
  8900. size_t extsymoff;
  8901. Elf_Internal_Shdr *versymhdr;
  8902. Elf_Internal_Sym *isym;
  8903. Elf_Internal_Sym *isymend;
  8904. Elf_Internal_Sym *isymbuf;
  8905. Elf_External_Versym *ever;
  8906. Elf_External_Versym *extversym;
  8907. input = loaded->abfd;
  8908. /* We check each DSO for a possible hidden versioned definition. */
  8909. if (input == abfd
  8910. || elf_dynversym (input) == 0)
  8911. continue;
  8912. hdr = &elf_tdata (input)->dynsymtab_hdr;
  8913. symcount = hdr->sh_size / bed->s->sizeof_sym;
  8914. if (elf_bad_symtab (input))
  8915. {
  8916. extsymcount = symcount;
  8917. extsymoff = 0;
  8918. }
  8919. else
  8920. {
  8921. extsymcount = symcount - hdr->sh_info;
  8922. extsymoff = hdr->sh_info;
  8923. }
  8924. if (extsymcount == 0)
  8925. continue;
  8926. isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff,
  8927. NULL, NULL, NULL);
  8928. if (isymbuf == NULL)
  8929. return false;
  8930. /* Read in any version definitions. */
  8931. versymhdr = &elf_tdata (input)->dynversym_hdr;
  8932. if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0
  8933. || (extversym = (Elf_External_Versym *)
  8934. _bfd_malloc_and_read (input, versymhdr->sh_size,
  8935. versymhdr->sh_size)) == NULL)
  8936. {
  8937. free (isymbuf);
  8938. return false;
  8939. }
  8940. ever = extversym + extsymoff;
  8941. isymend = isymbuf + extsymcount;
  8942. for (isym = isymbuf; isym < isymend; isym++, ever++)
  8943. {
  8944. const char *name;
  8945. Elf_Internal_Versym iver;
  8946. unsigned short version_index;
  8947. if (ELF_ST_BIND (isym->st_info) == STB_LOCAL
  8948. || isym->st_shndx == SHN_UNDEF)
  8949. continue;
  8950. name = bfd_elf_string_from_elf_section (input,
  8951. hdr->sh_link,
  8952. isym->st_name);
  8953. if (strcmp (name, h->root.root.string) != 0)
  8954. continue;
  8955. _bfd_elf_swap_versym_in (input, ever, &iver);
  8956. if ((iver.vs_vers & VERSYM_HIDDEN) == 0
  8957. && !(h->def_regular
  8958. && h->forced_local))
  8959. {
  8960. /* If we have a non-hidden versioned sym, then it should
  8961. have provided a definition for the undefined sym unless
  8962. it is defined in a non-shared object and forced local.
  8963. */
  8964. abort ();
  8965. }
  8966. version_index = iver.vs_vers & VERSYM_VERSION;
  8967. if (version_index == 1 || version_index == 2)
  8968. {
  8969. /* This is the base or first version. We can use it. */
  8970. free (extversym);
  8971. free (isymbuf);
  8972. return true;
  8973. }
  8974. }
  8975. free (extversym);
  8976. free (isymbuf);
  8977. }
  8978. return false;
  8979. }
  8980. /* Convert ELF common symbol TYPE. */
  8981. static int
  8982. elf_link_convert_common_type (struct bfd_link_info *info, int type)
  8983. {
  8984. /* Commom symbol can only appear in relocatable link. */
  8985. if (!bfd_link_relocatable (info))
  8986. abort ();
  8987. switch (info->elf_stt_common)
  8988. {
  8989. case unchanged:
  8990. break;
  8991. case elf_stt_common:
  8992. type = STT_COMMON;
  8993. break;
  8994. case no_elf_stt_common:
  8995. type = STT_OBJECT;
  8996. break;
  8997. }
  8998. return type;
  8999. }
  9000. /* Add an external symbol to the symbol table. This is called from
  9001. the hash table traversal routine. When generating a shared object,
  9002. we go through the symbol table twice. The first time we output
  9003. anything that might have been forced to local scope in a version
  9004. script. The second time we output the symbols that are still
  9005. global symbols. */
  9006. static bool
  9007. elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
  9008. {
  9009. struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
  9010. struct elf_outext_info *eoinfo = (struct elf_outext_info *) data;
  9011. struct elf_final_link_info *flinfo = eoinfo->flinfo;
  9012. bool strip;
  9013. Elf_Internal_Sym sym;
  9014. asection *input_sec;
  9015. const struct elf_backend_data *bed;
  9016. long indx;
  9017. int ret;
  9018. unsigned int type;
  9019. if (h->root.type == bfd_link_hash_warning)
  9020. {
  9021. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  9022. if (h->root.type == bfd_link_hash_new)
  9023. return true;
  9024. }
  9025. /* Decide whether to output this symbol in this pass. */
  9026. if (eoinfo->localsyms)
  9027. {
  9028. if (!h->forced_local)
  9029. return true;
  9030. }
  9031. else
  9032. {
  9033. if (h->forced_local)
  9034. return true;
  9035. }
  9036. bed = get_elf_backend_data (flinfo->output_bfd);
  9037. if (h->root.type == bfd_link_hash_undefined)
  9038. {
  9039. /* If we have an undefined symbol reference here then it must have
  9040. come from a shared library that is being linked in. (Undefined
  9041. references in regular files have already been handled unless
  9042. they are in unreferenced sections which are removed by garbage
  9043. collection). */
  9044. bool ignore_undef = false;
  9045. /* Some symbols may be special in that the fact that they're
  9046. undefined can be safely ignored - let backend determine that. */
  9047. if (bed->elf_backend_ignore_undef_symbol)
  9048. ignore_undef = bed->elf_backend_ignore_undef_symbol (h);
  9049. /* If we are reporting errors for this situation then do so now. */
  9050. if (!ignore_undef
  9051. && h->ref_dynamic_nonweak
  9052. && (!h->ref_regular || flinfo->info->gc_sections)
  9053. && !elf_link_check_versioned_symbol (flinfo->info, bed, h)
  9054. && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE)
  9055. {
  9056. flinfo->info->callbacks->undefined_symbol
  9057. (flinfo->info, h->root.root.string,
  9058. h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0,
  9059. flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE
  9060. && !flinfo->info->warn_unresolved_syms);
  9061. }
  9062. /* Strip a global symbol defined in a discarded section. */
  9063. if (h->indx == -3)
  9064. return true;
  9065. }
  9066. /* We should also warn if a forced local symbol is referenced from
  9067. shared libraries. */
  9068. if (bfd_link_executable (flinfo->info)
  9069. && h->forced_local
  9070. && h->ref_dynamic
  9071. && h->def_regular
  9072. && !h->dynamic_def
  9073. && h->ref_dynamic_nonweak
  9074. && !elf_link_check_versioned_symbol (flinfo->info, bed, h))
  9075. {
  9076. bfd *def_bfd;
  9077. const char *msg;
  9078. struct elf_link_hash_entry *hi = h;
  9079. /* Check indirect symbol. */
  9080. while (hi->root.type == bfd_link_hash_indirect)
  9081. hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
  9082. if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)
  9083. /* xgettext:c-format */
  9084. msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO");
  9085. else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
  9086. /* xgettext:c-format */
  9087. msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO");
  9088. else
  9089. /* xgettext:c-format */
  9090. msg = _("%pB: local symbol `%s' in %pB is referenced by DSO");
  9091. def_bfd = flinfo->output_bfd;
  9092. if (hi->root.u.def.section != bfd_abs_section_ptr)
  9093. def_bfd = hi->root.u.def.section->owner;
  9094. _bfd_error_handler (msg, flinfo->output_bfd,
  9095. h->root.root.string, def_bfd);
  9096. bfd_set_error (bfd_error_bad_value);
  9097. eoinfo->failed = true;
  9098. return false;
  9099. }
  9100. /* We don't want to output symbols that have never been mentioned by
  9101. a regular file, or that we have been told to strip. However, if
  9102. h->indx is set to -2, the symbol is used by a reloc and we must
  9103. output it. */
  9104. strip = false;
  9105. if (h->indx == -2)
  9106. ;
  9107. else if ((h->def_dynamic
  9108. || h->ref_dynamic
  9109. || h->root.type == bfd_link_hash_new)
  9110. && !h->def_regular
  9111. && !h->ref_regular)
  9112. strip = true;
  9113. else if (flinfo->info->strip == strip_all)
  9114. strip = true;
  9115. else if (flinfo->info->strip == strip_some
  9116. && bfd_hash_lookup (flinfo->info->keep_hash,
  9117. h->root.root.string, false, false) == NULL)
  9118. strip = true;
  9119. else if ((h->root.type == bfd_link_hash_defined
  9120. || h->root.type == bfd_link_hash_defweak)
  9121. && ((flinfo->info->strip_discarded
  9122. && discarded_section (h->root.u.def.section))
  9123. || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0
  9124. && h->root.u.def.section->owner != NULL
  9125. && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0)))
  9126. strip = true;
  9127. else if ((h->root.type == bfd_link_hash_undefined
  9128. || h->root.type == bfd_link_hash_undefweak)
  9129. && h->root.u.undef.abfd != NULL
  9130. && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
  9131. strip = true;
  9132. type = h->type;
  9133. /* If we're stripping it, and it's not a dynamic symbol, there's
  9134. nothing else to do. However, if it is a forced local symbol or
  9135. an ifunc symbol we need to give the backend finish_dynamic_symbol
  9136. function a chance to make it dynamic. */
  9137. if (strip
  9138. && h->dynindx == -1
  9139. && type != STT_GNU_IFUNC
  9140. && !h->forced_local)
  9141. return true;
  9142. sym.st_value = 0;
  9143. sym.st_size = h->size;
  9144. sym.st_other = h->other;
  9145. switch (h->root.type)
  9146. {
  9147. default:
  9148. case bfd_link_hash_new:
  9149. case bfd_link_hash_warning:
  9150. abort ();
  9151. return false;
  9152. case bfd_link_hash_undefined:
  9153. case bfd_link_hash_undefweak:
  9154. input_sec = bfd_und_section_ptr;
  9155. sym.st_shndx = SHN_UNDEF;
  9156. break;
  9157. case bfd_link_hash_defined:
  9158. case bfd_link_hash_defweak:
  9159. {
  9160. input_sec = h->root.u.def.section;
  9161. if (input_sec->output_section != NULL)
  9162. {
  9163. sym.st_shndx =
  9164. _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
  9165. input_sec->output_section);
  9166. if (sym.st_shndx == SHN_BAD)
  9167. {
  9168. _bfd_error_handler
  9169. /* xgettext:c-format */
  9170. (_("%pB: could not find output section %pA for input section %pA"),
  9171. flinfo->output_bfd, input_sec->output_section, input_sec);
  9172. bfd_set_error (bfd_error_nonrepresentable_section);
  9173. eoinfo->failed = true;
  9174. return false;
  9175. }
  9176. /* ELF symbols in relocatable files are section relative,
  9177. but in nonrelocatable files they are virtual
  9178. addresses. */
  9179. sym.st_value = h->root.u.def.value + input_sec->output_offset;
  9180. if (!bfd_link_relocatable (flinfo->info))
  9181. {
  9182. sym.st_value += input_sec->output_section->vma;
  9183. if (h->type == STT_TLS)
  9184. {
  9185. asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec;
  9186. if (tls_sec != NULL)
  9187. sym.st_value -= tls_sec->vma;
  9188. }
  9189. }
  9190. }
  9191. else
  9192. {
  9193. BFD_ASSERT (input_sec->owner == NULL
  9194. || (input_sec->owner->flags & DYNAMIC) != 0);
  9195. sym.st_shndx = SHN_UNDEF;
  9196. input_sec = bfd_und_section_ptr;
  9197. }
  9198. }
  9199. break;
  9200. case bfd_link_hash_common:
  9201. input_sec = h->root.u.c.p->section;
  9202. sym.st_shndx = bed->common_section_index (input_sec);
  9203. sym.st_value = 1 << h->root.u.c.p->alignment_power;
  9204. break;
  9205. case bfd_link_hash_indirect:
  9206. /* These symbols are created by symbol versioning. They point
  9207. to the decorated version of the name. For example, if the
  9208. symbol foo@@GNU_1.2 is the default, which should be used when
  9209. foo is used with no version, then we add an indirect symbol
  9210. foo which points to foo@@GNU_1.2. We ignore these symbols,
  9211. since the indirected symbol is already in the hash table. */
  9212. return true;
  9213. }
  9214. if (type == STT_COMMON || type == STT_OBJECT)
  9215. switch (h->root.type)
  9216. {
  9217. case bfd_link_hash_common:
  9218. type = elf_link_convert_common_type (flinfo->info, type);
  9219. break;
  9220. case bfd_link_hash_defined:
  9221. case bfd_link_hash_defweak:
  9222. if (bed->common_definition (&sym))
  9223. type = elf_link_convert_common_type (flinfo->info, type);
  9224. else
  9225. type = STT_OBJECT;
  9226. break;
  9227. case bfd_link_hash_undefined:
  9228. case bfd_link_hash_undefweak:
  9229. break;
  9230. default:
  9231. abort ();
  9232. }
  9233. if (h->forced_local)
  9234. {
  9235. sym.st_info = ELF_ST_INFO (STB_LOCAL, type);
  9236. /* Turn off visibility on local symbol. */
  9237. sym.st_other &= ~ELF_ST_VISIBILITY (-1);
  9238. }
  9239. /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */
  9240. else if (h->unique_global && h->def_regular)
  9241. sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type);
  9242. else if (h->root.type == bfd_link_hash_undefweak
  9243. || h->root.type == bfd_link_hash_defweak)
  9244. sym.st_info = ELF_ST_INFO (STB_WEAK, type);
  9245. else
  9246. sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
  9247. sym.st_target_internal = h->target_internal;
  9248. /* Give the processor backend a chance to tweak the symbol value,
  9249. and also to finish up anything that needs to be done for this
  9250. symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for
  9251. forced local syms when non-shared is due to a historical quirk.
  9252. STT_GNU_IFUNC symbol must go through PLT. */
  9253. if ((h->type == STT_GNU_IFUNC
  9254. && h->def_regular
  9255. && !bfd_link_relocatable (flinfo->info))
  9256. || ((h->dynindx != -1
  9257. || h->forced_local)
  9258. && ((bfd_link_pic (flinfo->info)
  9259. && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  9260. || h->root.type != bfd_link_hash_undefweak))
  9261. || !h->forced_local)
  9262. && elf_hash_table (flinfo->info)->dynamic_sections_created))
  9263. {
  9264. if (! ((*bed->elf_backend_finish_dynamic_symbol)
  9265. (flinfo->output_bfd, flinfo->info, h, &sym)))
  9266. {
  9267. eoinfo->failed = true;
  9268. return false;
  9269. }
  9270. }
  9271. /* If we are marking the symbol as undefined, and there are no
  9272. non-weak references to this symbol from a regular object, then
  9273. mark the symbol as weak undefined; if there are non-weak
  9274. references, mark the symbol as strong. We can't do this earlier,
  9275. because it might not be marked as undefined until the
  9276. finish_dynamic_symbol routine gets through with it. */
  9277. if (sym.st_shndx == SHN_UNDEF
  9278. && h->ref_regular
  9279. && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL
  9280. || ELF_ST_BIND (sym.st_info) == STB_WEAK))
  9281. {
  9282. int bindtype;
  9283. type = ELF_ST_TYPE (sym.st_info);
  9284. /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */
  9285. if (type == STT_GNU_IFUNC)
  9286. type = STT_FUNC;
  9287. if (h->ref_regular_nonweak)
  9288. bindtype = STB_GLOBAL;
  9289. else
  9290. bindtype = STB_WEAK;
  9291. sym.st_info = ELF_ST_INFO (bindtype, type);
  9292. }
  9293. /* If this is a symbol defined in a dynamic library, don't use the
  9294. symbol size from the dynamic library. Relinking an executable
  9295. against a new library may introduce gratuitous changes in the
  9296. executable's symbols if we keep the size. */
  9297. if (sym.st_shndx == SHN_UNDEF
  9298. && !h->def_regular
  9299. && h->def_dynamic)
  9300. sym.st_size = 0;
  9301. /* If a non-weak symbol with non-default visibility is not defined
  9302. locally, it is a fatal error. */
  9303. if (!bfd_link_relocatable (flinfo->info)
  9304. && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT
  9305. && ELF_ST_BIND (sym.st_info) != STB_WEAK
  9306. && h->root.type == bfd_link_hash_undefined
  9307. && !h->def_regular)
  9308. {
  9309. const char *msg;
  9310. if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED)
  9311. /* xgettext:c-format */
  9312. msg = _("%pB: protected symbol `%s' isn't defined");
  9313. else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL)
  9314. /* xgettext:c-format */
  9315. msg = _("%pB: internal symbol `%s' isn't defined");
  9316. else
  9317. /* xgettext:c-format */
  9318. msg = _("%pB: hidden symbol `%s' isn't defined");
  9319. _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string);
  9320. bfd_set_error (bfd_error_bad_value);
  9321. eoinfo->failed = true;
  9322. return false;
  9323. }
  9324. /* If this symbol should be put in the .dynsym section, then put it
  9325. there now. We already know the symbol index. We also fill in
  9326. the entry in the .hash section. */
  9327. if (h->dynindx != -1
  9328. && elf_hash_table (flinfo->info)->dynamic_sections_created
  9329. && elf_hash_table (flinfo->info)->dynsym != NULL
  9330. && !discarded_section (elf_hash_table (flinfo->info)->dynsym))
  9331. {
  9332. bfd_byte *esym;
  9333. /* Since there is no version information in the dynamic string,
  9334. if there is no version info in symbol version section, we will
  9335. have a run-time problem if not linking executable, referenced
  9336. by shared library, or not bound locally. */
  9337. if (h->verinfo.verdef == NULL
  9338. && (!bfd_link_executable (flinfo->info)
  9339. || h->ref_dynamic
  9340. || !h->def_regular))
  9341. {
  9342. char *p = strrchr (h->root.root.string, ELF_VER_CHR);
  9343. if (p && p [1] != '\0')
  9344. {
  9345. _bfd_error_handler
  9346. /* xgettext:c-format */
  9347. (_("%pB: no symbol version section for versioned symbol `%s'"),
  9348. flinfo->output_bfd, h->root.root.string);
  9349. eoinfo->failed = true;
  9350. return false;
  9351. }
  9352. }
  9353. sym.st_name = h->dynstr_index;
  9354. esym = (elf_hash_table (flinfo->info)->dynsym->contents
  9355. + h->dynindx * bed->s->sizeof_sym);
  9356. if (!check_dynsym (flinfo->output_bfd, &sym))
  9357. {
  9358. eoinfo->failed = true;
  9359. return false;
  9360. }
  9361. /* Inform the linker of the addition of this symbol. */
  9362. if (flinfo->info->callbacks->ctf_new_dynsym)
  9363. flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym);
  9364. bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0);
  9365. if (flinfo->hash_sec != NULL)
  9366. {
  9367. size_t hash_entry_size;
  9368. bfd_byte *bucketpos;
  9369. bfd_vma chain;
  9370. size_t bucketcount;
  9371. size_t bucket;
  9372. bucketcount = elf_hash_table (flinfo->info)->bucketcount;
  9373. bucket = h->u.elf_hash_value % bucketcount;
  9374. hash_entry_size
  9375. = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize;
  9376. bucketpos = ((bfd_byte *) flinfo->hash_sec->contents
  9377. + (bucket + 2) * hash_entry_size);
  9378. chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos);
  9379. bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx,
  9380. bucketpos);
  9381. bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain,
  9382. ((bfd_byte *) flinfo->hash_sec->contents
  9383. + (bucketcount + 2 + h->dynindx) * hash_entry_size));
  9384. }
  9385. if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL)
  9386. {
  9387. Elf_Internal_Versym iversym;
  9388. Elf_External_Versym *eversym;
  9389. if (!h->def_regular && !ELF_COMMON_DEF_P (h))
  9390. {
  9391. if (h->verinfo.verdef == NULL
  9392. || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd)
  9393. & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED)))
  9394. iversym.vs_vers = 1;
  9395. else
  9396. iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1;
  9397. }
  9398. else
  9399. {
  9400. if (h->verinfo.vertree == NULL)
  9401. iversym.vs_vers = 1;
  9402. else
  9403. iversym.vs_vers = h->verinfo.vertree->vernum + 1;
  9404. if (flinfo->info->create_default_symver)
  9405. iversym.vs_vers++;
  9406. }
  9407. /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is
  9408. defined locally. */
  9409. if (h->versioned == versioned_hidden && h->def_regular)
  9410. iversym.vs_vers |= VERSYM_HIDDEN;
  9411. eversym = (Elf_External_Versym *) flinfo->symver_sec->contents;
  9412. eversym += h->dynindx;
  9413. _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym);
  9414. }
  9415. }
  9416. /* If the symbol is undefined, and we didn't output it to .dynsym,
  9417. strip it from .symtab too. Obviously we can't do this for
  9418. relocatable output or when needed for --emit-relocs. */
  9419. else if (input_sec == bfd_und_section_ptr
  9420. && h->indx != -2
  9421. /* PR 22319 Do not strip global undefined symbols marked as being needed. */
  9422. && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL)
  9423. && !bfd_link_relocatable (flinfo->info))
  9424. return true;
  9425. /* Also strip others that we couldn't earlier due to dynamic symbol
  9426. processing. */
  9427. if (strip)
  9428. return true;
  9429. if ((input_sec->flags & SEC_EXCLUDE) != 0)
  9430. return true;
  9431. /* Output a FILE symbol so that following locals are not associated
  9432. with the wrong input file. We need one for forced local symbols
  9433. if we've seen more than one FILE symbol or when we have exactly
  9434. one FILE symbol but global symbols are present in a file other
  9435. than the one with the FILE symbol. We also need one if linker
  9436. defined symbols are present. In practice these conditions are
  9437. always met, so just emit the FILE symbol unconditionally. */
  9438. if (eoinfo->localsyms
  9439. && !eoinfo->file_sym_done
  9440. && eoinfo->flinfo->filesym_count != 0)
  9441. {
  9442. Elf_Internal_Sym fsym;
  9443. memset (&fsym, 0, sizeof (fsym));
  9444. fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
  9445. fsym.st_shndx = SHN_ABS;
  9446. if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym,
  9447. bfd_und_section_ptr, NULL))
  9448. return false;
  9449. eoinfo->file_sym_done = true;
  9450. }
  9451. indx = bfd_get_symcount (flinfo->output_bfd);
  9452. ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym,
  9453. input_sec, h);
  9454. if (ret == 0)
  9455. {
  9456. eoinfo->failed = true;
  9457. return false;
  9458. }
  9459. else if (ret == 1)
  9460. h->indx = indx;
  9461. else if (h->indx == -2)
  9462. abort();
  9463. return true;
  9464. }
  9465. /* Return TRUE if special handling is done for relocs in SEC against
  9466. symbols defined in discarded sections. */
  9467. static bool
  9468. elf_section_ignore_discarded_relocs (asection *sec)
  9469. {
  9470. const struct elf_backend_data *bed;
  9471. switch (sec->sec_info_type)
  9472. {
  9473. case SEC_INFO_TYPE_STABS:
  9474. case SEC_INFO_TYPE_EH_FRAME:
  9475. case SEC_INFO_TYPE_EH_FRAME_ENTRY:
  9476. return true;
  9477. default:
  9478. break;
  9479. }
  9480. bed = get_elf_backend_data (sec->owner);
  9481. if (bed->elf_backend_ignore_discarded_relocs != NULL
  9482. && (*bed->elf_backend_ignore_discarded_relocs) (sec))
  9483. return true;
  9484. return false;
  9485. }
  9486. /* Return a mask saying how ld should treat relocations in SEC against
  9487. symbols defined in discarded sections. If this function returns
  9488. COMPLAIN set, ld will issue a warning message. If this function
  9489. returns PRETEND set, and the discarded section was link-once and the
  9490. same size as the kept link-once section, ld will pretend that the
  9491. symbol was actually defined in the kept section. Otherwise ld will
  9492. zero the reloc (at least that is the intent, but some cooperation by
  9493. the target dependent code is needed, particularly for REL targets). */
  9494. unsigned int
  9495. _bfd_elf_default_action_discarded (asection *sec)
  9496. {
  9497. if (sec->flags & SEC_DEBUGGING)
  9498. return PRETEND;
  9499. if (strcmp (".eh_frame", sec->name) == 0)
  9500. return 0;
  9501. if (strcmp (".gcc_except_table", sec->name) == 0)
  9502. return 0;
  9503. return COMPLAIN | PRETEND;
  9504. }
  9505. /* Find a match between a section and a member of a section group. */
  9506. static asection *
  9507. match_group_member (asection *sec, asection *group,
  9508. struct bfd_link_info *info)
  9509. {
  9510. asection *first = elf_next_in_group (group);
  9511. asection *s = first;
  9512. while (s != NULL)
  9513. {
  9514. if (bfd_elf_match_symbols_in_sections (s, sec, info))
  9515. return s;
  9516. s = elf_next_in_group (s);
  9517. if (s == first)
  9518. break;
  9519. }
  9520. return NULL;
  9521. }
  9522. /* Check if the kept section of a discarded section SEC can be used
  9523. to replace it. Return the replacement if it is OK. Otherwise return
  9524. NULL. */
  9525. asection *
  9526. _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info)
  9527. {
  9528. asection *kept;
  9529. kept = sec->kept_section;
  9530. if (kept != NULL)
  9531. {
  9532. if ((kept->flags & SEC_GROUP) != 0)
  9533. kept = match_group_member (sec, kept, info);
  9534. if (kept != NULL)
  9535. {
  9536. if ((sec->rawsize != 0 ? sec->rawsize : sec->size)
  9537. != (kept->rawsize != 0 ? kept->rawsize : kept->size))
  9538. kept = NULL;
  9539. else
  9540. {
  9541. /* Get the real kept section. */
  9542. asection *next;
  9543. for (next = kept->kept_section;
  9544. next != NULL;
  9545. next = next->kept_section)
  9546. kept = next;
  9547. }
  9548. }
  9549. sec->kept_section = kept;
  9550. }
  9551. return kept;
  9552. }
  9553. /* Link an input file into the linker output file. This function
  9554. handles all the sections and relocations of the input file at once.
  9555. This is so that we only have to read the local symbols once, and
  9556. don't have to keep them in memory. */
  9557. static bool
  9558. elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
  9559. {
  9560. int (*relocate_section)
  9561. (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  9562. Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
  9563. bfd *output_bfd;
  9564. Elf_Internal_Shdr *symtab_hdr;
  9565. size_t locsymcount;
  9566. size_t extsymoff;
  9567. Elf_Internal_Sym *isymbuf;
  9568. Elf_Internal_Sym *isym;
  9569. Elf_Internal_Sym *isymend;
  9570. long *pindex;
  9571. asection **ppsection;
  9572. asection *o;
  9573. const struct elf_backend_data *bed;
  9574. struct elf_link_hash_entry **sym_hashes;
  9575. bfd_size_type address_size;
  9576. bfd_vma r_type_mask;
  9577. int r_sym_shift;
  9578. bool have_file_sym = false;
  9579. output_bfd = flinfo->output_bfd;
  9580. bed = get_elf_backend_data (output_bfd);
  9581. relocate_section = bed->elf_backend_relocate_section;
  9582. /* If this is a dynamic object, we don't want to do anything here:
  9583. we don't want the local symbols, and we don't want the section
  9584. contents. */
  9585. if ((input_bfd->flags & DYNAMIC) != 0)
  9586. return true;
  9587. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  9588. if (elf_bad_symtab (input_bfd))
  9589. {
  9590. locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
  9591. extsymoff = 0;
  9592. }
  9593. else
  9594. {
  9595. locsymcount = symtab_hdr->sh_info;
  9596. extsymoff = symtab_hdr->sh_info;
  9597. }
  9598. /* Enable GNU OSABI features in the output BFD that are used in the input
  9599. BFD. */
  9600. if (bed->elf_osabi == ELFOSABI_NONE
  9601. || bed->elf_osabi == ELFOSABI_GNU
  9602. || bed->elf_osabi == ELFOSABI_FREEBSD)
  9603. elf_tdata (output_bfd)->has_gnu_osabi
  9604. |= (elf_tdata (input_bfd)->has_gnu_osabi
  9605. & (bfd_link_relocatable (flinfo->info)
  9606. ? -1 : ~elf_gnu_osabi_retain));
  9607. /* Read the local symbols. */
  9608. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  9609. if (isymbuf == NULL && locsymcount != 0)
  9610. {
  9611. isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
  9612. flinfo->internal_syms,
  9613. flinfo->external_syms,
  9614. flinfo->locsym_shndx);
  9615. if (isymbuf == NULL)
  9616. return false;
  9617. }
  9618. /* Find local symbol sections and adjust values of symbols in
  9619. SEC_MERGE sections. Write out those local symbols we know are
  9620. going into the output file. */
  9621. isymend = PTR_ADD (isymbuf, locsymcount);
  9622. for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections;
  9623. isym < isymend;
  9624. isym++, pindex++, ppsection++)
  9625. {
  9626. asection *isec;
  9627. const char *name;
  9628. Elf_Internal_Sym osym;
  9629. long indx;
  9630. int ret;
  9631. *pindex = -1;
  9632. if (elf_bad_symtab (input_bfd))
  9633. {
  9634. if (ELF_ST_BIND (isym->st_info) != STB_LOCAL)
  9635. {
  9636. *ppsection = NULL;
  9637. continue;
  9638. }
  9639. }
  9640. if (isym->st_shndx == SHN_UNDEF)
  9641. isec = bfd_und_section_ptr;
  9642. else if (isym->st_shndx == SHN_ABS)
  9643. isec = bfd_abs_section_ptr;
  9644. else if (isym->st_shndx == SHN_COMMON)
  9645. isec = bfd_com_section_ptr;
  9646. else
  9647. {
  9648. isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
  9649. if (isec == NULL)
  9650. {
  9651. /* Don't attempt to output symbols with st_shnx in the
  9652. reserved range other than SHN_ABS and SHN_COMMON. */
  9653. isec = bfd_und_section_ptr;
  9654. }
  9655. else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE
  9656. && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
  9657. isym->st_value =
  9658. _bfd_merged_section_offset (output_bfd, &isec,
  9659. elf_section_data (isec)->sec_info,
  9660. isym->st_value);
  9661. }
  9662. *ppsection = isec;
  9663. /* Don't output the first, undefined, symbol. In fact, don't
  9664. output any undefined local symbol. */
  9665. if (isec == bfd_und_section_ptr)
  9666. continue;
  9667. if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
  9668. {
  9669. /* We never output section symbols. Instead, we use the
  9670. section symbol of the corresponding section in the output
  9671. file. */
  9672. continue;
  9673. }
  9674. /* If we are stripping all symbols, we don't want to output this
  9675. one. */
  9676. if (flinfo->info->strip == strip_all)
  9677. continue;
  9678. /* If we are discarding all local symbols, we don't want to
  9679. output this one. If we are generating a relocatable output
  9680. file, then some of the local symbols may be required by
  9681. relocs; we output them below as we discover that they are
  9682. needed. */
  9683. if (flinfo->info->discard == discard_all)
  9684. continue;
  9685. /* If this symbol is defined in a section which we are
  9686. discarding, we don't need to keep it. */
  9687. if (isym->st_shndx != SHN_UNDEF
  9688. && isym->st_shndx < SHN_LORESERVE
  9689. && isec->output_section == NULL
  9690. && flinfo->info->non_contiguous_regions
  9691. && flinfo->info->non_contiguous_regions_warnings)
  9692. {
  9693. _bfd_error_handler (_("warning: --enable-non-contiguous-regions "
  9694. "discards section `%s' from '%s'\n"),
  9695. isec->name, bfd_get_filename (isec->owner));
  9696. continue;
  9697. }
  9698. if (isym->st_shndx != SHN_UNDEF
  9699. && isym->st_shndx < SHN_LORESERVE
  9700. && bfd_section_removed_from_list (output_bfd,
  9701. isec->output_section))
  9702. continue;
  9703. /* Get the name of the symbol. */
  9704. name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link,
  9705. isym->st_name);
  9706. if (name == NULL)
  9707. return false;
  9708. /* See if we are discarding symbols with this name. */
  9709. if ((flinfo->info->strip == strip_some
  9710. && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false)
  9711. == NULL))
  9712. || (((flinfo->info->discard == discard_sec_merge
  9713. && (isec->flags & SEC_MERGE)
  9714. && !bfd_link_relocatable (flinfo->info))
  9715. || flinfo->info->discard == discard_l)
  9716. && bfd_is_local_label_name (input_bfd, name)))
  9717. continue;
  9718. if (ELF_ST_TYPE (isym->st_info) == STT_FILE)
  9719. {
  9720. if (input_bfd->lto_output)
  9721. /* -flto puts a temp file name here. This means builds
  9722. are not reproducible. Discard the symbol. */
  9723. continue;
  9724. have_file_sym = true;
  9725. flinfo->filesym_count += 1;
  9726. }
  9727. if (!have_file_sym)
  9728. {
  9729. /* In the absence of debug info, bfd_find_nearest_line uses
  9730. FILE symbols to determine the source file for local
  9731. function symbols. Provide a FILE symbol here if input
  9732. files lack such, so that their symbols won't be
  9733. associated with a previous input file. It's not the
  9734. source file, but the best we can do. */
  9735. const char *filename;
  9736. have_file_sym = true;
  9737. flinfo->filesym_count += 1;
  9738. memset (&osym, 0, sizeof (osym));
  9739. osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
  9740. osym.st_shndx = SHN_ABS;
  9741. if (input_bfd->lto_output)
  9742. filename = NULL;
  9743. else
  9744. filename = lbasename (bfd_get_filename (input_bfd));
  9745. if (!elf_link_output_symstrtab (flinfo, filename, &osym,
  9746. bfd_abs_section_ptr, NULL))
  9747. return false;
  9748. }
  9749. osym = *isym;
  9750. /* Adjust the section index for the output file. */
  9751. osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
  9752. isec->output_section);
  9753. if (osym.st_shndx == SHN_BAD)
  9754. return false;
  9755. /* ELF symbols in relocatable files are section relative, but
  9756. in executable files they are virtual addresses. Note that
  9757. this code assumes that all ELF sections have an associated
  9758. BFD section with a reasonable value for output_offset; below
  9759. we assume that they also have a reasonable value for
  9760. output_section. Any special sections must be set up to meet
  9761. these requirements. */
  9762. osym.st_value += isec->output_offset;
  9763. if (!bfd_link_relocatable (flinfo->info))
  9764. {
  9765. osym.st_value += isec->output_section->vma;
  9766. if (ELF_ST_TYPE (osym.st_info) == STT_TLS)
  9767. {
  9768. /* STT_TLS symbols are relative to PT_TLS segment base. */
  9769. if (elf_hash_table (flinfo->info)->tls_sec != NULL)
  9770. osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma;
  9771. else
  9772. osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info),
  9773. STT_NOTYPE);
  9774. }
  9775. }
  9776. indx = bfd_get_symcount (output_bfd);
  9777. ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL);
  9778. if (ret == 0)
  9779. return false;
  9780. else if (ret == 1)
  9781. *pindex = indx;
  9782. }
  9783. if (bed->s->arch_size == 32)
  9784. {
  9785. r_type_mask = 0xff;
  9786. r_sym_shift = 8;
  9787. address_size = 4;
  9788. }
  9789. else
  9790. {
  9791. r_type_mask = 0xffffffff;
  9792. r_sym_shift = 32;
  9793. address_size = 8;
  9794. }
  9795. /* Relocate the contents of each section. */
  9796. sym_hashes = elf_sym_hashes (input_bfd);
  9797. for (o = input_bfd->sections; o != NULL; o = o->next)
  9798. {
  9799. bfd_byte *contents;
  9800. if (! o->linker_mark)
  9801. {
  9802. /* This section was omitted from the link. */
  9803. continue;
  9804. }
  9805. if (!flinfo->info->resolve_section_groups
  9806. && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP)
  9807. {
  9808. /* Deal with the group signature symbol. */
  9809. struct bfd_elf_section_data *sec_data = elf_section_data (o);
  9810. unsigned long symndx = sec_data->this_hdr.sh_info;
  9811. asection *osec = o->output_section;
  9812. BFD_ASSERT (bfd_link_relocatable (flinfo->info));
  9813. if (symndx >= locsymcount
  9814. || (elf_bad_symtab (input_bfd)
  9815. && flinfo->sections[symndx] == NULL))
  9816. {
  9817. struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff];
  9818. while (h->root.type == bfd_link_hash_indirect
  9819. || h->root.type == bfd_link_hash_warning)
  9820. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  9821. /* Arrange for symbol to be output. */
  9822. h->indx = -2;
  9823. elf_section_data (osec)->this_hdr.sh_info = -2;
  9824. }
  9825. else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION)
  9826. {
  9827. /* We'll use the output section target_index. */
  9828. asection *sec = flinfo->sections[symndx]->output_section;
  9829. elf_section_data (osec)->this_hdr.sh_info = sec->target_index;
  9830. }
  9831. else
  9832. {
  9833. if (flinfo->indices[symndx] == -1)
  9834. {
  9835. /* Otherwise output the local symbol now. */
  9836. Elf_Internal_Sym sym = isymbuf[symndx];
  9837. asection *sec = flinfo->sections[symndx]->output_section;
  9838. const char *name;
  9839. long indx;
  9840. int ret;
  9841. name = bfd_elf_string_from_elf_section (input_bfd,
  9842. symtab_hdr->sh_link,
  9843. sym.st_name);
  9844. if (name == NULL)
  9845. return false;
  9846. sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
  9847. sec);
  9848. if (sym.st_shndx == SHN_BAD)
  9849. return false;
  9850. sym.st_value += o->output_offset;
  9851. indx = bfd_get_symcount (output_bfd);
  9852. ret = elf_link_output_symstrtab (flinfo, name, &sym, o,
  9853. NULL);
  9854. if (ret == 0)
  9855. return false;
  9856. else if (ret == 1)
  9857. flinfo->indices[symndx] = indx;
  9858. else
  9859. abort ();
  9860. }
  9861. elf_section_data (osec)->this_hdr.sh_info
  9862. = flinfo->indices[symndx];
  9863. }
  9864. }
  9865. if ((o->flags & SEC_HAS_CONTENTS) == 0
  9866. || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
  9867. continue;
  9868. if ((o->flags & SEC_LINKER_CREATED) != 0)
  9869. {
  9870. /* Section was created by _bfd_elf_link_create_dynamic_sections
  9871. or somesuch. */
  9872. continue;
  9873. }
  9874. /* Get the contents of the section. They have been cached by a
  9875. relaxation routine. Note that o is a section in an input
  9876. file, so the contents field will not have been set by any of
  9877. the routines which work on output files. */
  9878. if (elf_section_data (o)->this_hdr.contents != NULL)
  9879. {
  9880. contents = elf_section_data (o)->this_hdr.contents;
  9881. if (bed->caches_rawsize
  9882. && o->rawsize != 0
  9883. && o->rawsize < o->size)
  9884. {
  9885. memcpy (flinfo->contents, contents, o->rawsize);
  9886. contents = flinfo->contents;
  9887. }
  9888. }
  9889. else
  9890. {
  9891. contents = flinfo->contents;
  9892. if (! bfd_get_full_section_contents (input_bfd, o, &contents))
  9893. return false;
  9894. }
  9895. if ((o->flags & SEC_RELOC) != 0)
  9896. {
  9897. Elf_Internal_Rela *internal_relocs;
  9898. Elf_Internal_Rela *rel, *relend;
  9899. int action_discarded;
  9900. int ret;
  9901. /* Get the swapped relocs. */
  9902. internal_relocs
  9903. = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
  9904. flinfo->external_relocs,
  9905. flinfo->internal_relocs,
  9906. false);
  9907. if (internal_relocs == NULL
  9908. && o->reloc_count > 0)
  9909. return false;
  9910. action_discarded = -1;
  9911. if (!elf_section_ignore_discarded_relocs (o))
  9912. action_discarded = (*bed->action_discarded) (o);
  9913. /* Run through the relocs evaluating complex reloc symbols and
  9914. looking for relocs against symbols from discarded sections
  9915. or section symbols from removed link-once sections.
  9916. Complain about relocs against discarded sections. Zero
  9917. relocs against removed link-once sections. */
  9918. rel = internal_relocs;
  9919. relend = rel + o->reloc_count;
  9920. for ( ; rel < relend; rel++)
  9921. {
  9922. unsigned long r_symndx = rel->r_info >> r_sym_shift;
  9923. unsigned int s_type;
  9924. asection **ps, *sec;
  9925. struct elf_link_hash_entry *h = NULL;
  9926. const char *sym_name;
  9927. if (r_symndx == STN_UNDEF)
  9928. continue;
  9929. if (r_symndx >= locsymcount
  9930. || (elf_bad_symtab (input_bfd)
  9931. && flinfo->sections[r_symndx] == NULL))
  9932. {
  9933. h = sym_hashes[r_symndx - extsymoff];
  9934. /* Badly formatted input files can contain relocs that
  9935. reference non-existant symbols. Check here so that
  9936. we do not seg fault. */
  9937. if (h == NULL)
  9938. {
  9939. _bfd_error_handler
  9940. /* xgettext:c-format */
  9941. (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA "
  9942. "that references a non-existent global symbol"),
  9943. input_bfd, (uint64_t) rel->r_info, o);
  9944. bfd_set_error (bfd_error_bad_value);
  9945. return false;
  9946. }
  9947. while (h->root.type == bfd_link_hash_indirect
  9948. || h->root.type == bfd_link_hash_warning)
  9949. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  9950. s_type = h->type;
  9951. /* If a plugin symbol is referenced from a non-IR file,
  9952. mark the symbol as undefined. Note that the
  9953. linker may attach linker created dynamic sections
  9954. to the plugin bfd. Symbols defined in linker
  9955. created sections are not plugin symbols. */
  9956. if ((h->root.non_ir_ref_regular
  9957. || h->root.non_ir_ref_dynamic)
  9958. && (h->root.type == bfd_link_hash_defined
  9959. || h->root.type == bfd_link_hash_defweak)
  9960. && (h->root.u.def.section->flags
  9961. & SEC_LINKER_CREATED) == 0
  9962. && h->root.u.def.section->owner != NULL
  9963. && (h->root.u.def.section->owner->flags
  9964. & BFD_PLUGIN) != 0)
  9965. {
  9966. h->root.type = bfd_link_hash_undefined;
  9967. h->root.u.undef.abfd = h->root.u.def.section->owner;
  9968. }
  9969. ps = NULL;
  9970. if (h->root.type == bfd_link_hash_defined
  9971. || h->root.type == bfd_link_hash_defweak)
  9972. ps = &h->root.u.def.section;
  9973. sym_name = h->root.root.string;
  9974. }
  9975. else
  9976. {
  9977. Elf_Internal_Sym *sym = isymbuf + r_symndx;
  9978. s_type = ELF_ST_TYPE (sym->st_info);
  9979. ps = &flinfo->sections[r_symndx];
  9980. sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr,
  9981. sym, *ps);
  9982. }
  9983. if ((s_type == STT_RELC || s_type == STT_SRELC)
  9984. && !bfd_link_relocatable (flinfo->info))
  9985. {
  9986. bfd_vma val;
  9987. bfd_vma dot = (rel->r_offset
  9988. + o->output_offset + o->output_section->vma);
  9989. #ifdef DEBUG
  9990. printf ("Encountered a complex symbol!");
  9991. printf (" (input_bfd %s, section %s, reloc %ld\n",
  9992. bfd_get_filename (input_bfd), o->name,
  9993. (long) (rel - internal_relocs));
  9994. printf (" symbol: idx %8.8lx, name %s\n",
  9995. r_symndx, sym_name);
  9996. printf (" reloc : info %8.8lx, addr %8.8lx\n",
  9997. (unsigned long) rel->r_info,
  9998. (unsigned long) rel->r_offset);
  9999. #endif
  10000. if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot,
  10001. isymbuf, locsymcount, s_type == STT_SRELC))
  10002. return false;
  10003. /* Symbol evaluated OK. Update to absolute value. */
  10004. set_symbol_value (input_bfd, isymbuf, locsymcount,
  10005. r_symndx, val);
  10006. continue;
  10007. }
  10008. if (action_discarded != -1 && ps != NULL)
  10009. {
  10010. /* Complain if the definition comes from a
  10011. discarded section. */
  10012. if ((sec = *ps) != NULL && discarded_section (sec))
  10013. {
  10014. BFD_ASSERT (r_symndx != STN_UNDEF);
  10015. if (action_discarded & COMPLAIN)
  10016. (*flinfo->info->callbacks->einfo)
  10017. /* xgettext:c-format */
  10018. (_("%X`%s' referenced in section `%pA' of %pB: "
  10019. "defined in discarded section `%pA' of %pB\n"),
  10020. sym_name, o, input_bfd, sec, sec->owner);
  10021. /* Try to do the best we can to support buggy old
  10022. versions of gcc. Pretend that the symbol is
  10023. really defined in the kept linkonce section.
  10024. FIXME: This is quite broken. Modifying the
  10025. symbol here means we will be changing all later
  10026. uses of the symbol, not just in this section. */
  10027. if (action_discarded & PRETEND)
  10028. {
  10029. asection *kept;
  10030. kept = _bfd_elf_check_kept_section (sec,
  10031. flinfo->info);
  10032. if (kept != NULL)
  10033. {
  10034. *ps = kept;
  10035. continue;
  10036. }
  10037. }
  10038. }
  10039. }
  10040. }
  10041. /* Relocate the section by invoking a back end routine.
  10042. The back end routine is responsible for adjusting the
  10043. section contents as necessary, and (if using Rela relocs
  10044. and generating a relocatable output file) adjusting the
  10045. reloc addend as necessary.
  10046. The back end routine does not have to worry about setting
  10047. the reloc address or the reloc symbol index.
  10048. The back end routine is given a pointer to the swapped in
  10049. internal symbols, and can access the hash table entries
  10050. for the external symbols via elf_sym_hashes (input_bfd).
  10051. When generating relocatable output, the back end routine
  10052. must handle STB_LOCAL/STT_SECTION symbols specially. The
  10053. output symbol is going to be a section symbol
  10054. corresponding to the output section, which will require
  10055. the addend to be adjusted. */
  10056. ret = (*relocate_section) (output_bfd, flinfo->info,
  10057. input_bfd, o, contents,
  10058. internal_relocs,
  10059. isymbuf,
  10060. flinfo->sections);
  10061. if (!ret)
  10062. return false;
  10063. if (ret == 2
  10064. || bfd_link_relocatable (flinfo->info)
  10065. || flinfo->info->emitrelocations)
  10066. {
  10067. Elf_Internal_Rela *irela;
  10068. Elf_Internal_Rela *irelaend, *irelamid;
  10069. bfd_vma last_offset;
  10070. struct elf_link_hash_entry **rel_hash;
  10071. struct elf_link_hash_entry **rel_hash_list, **rela_hash_list;
  10072. Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr;
  10073. unsigned int next_erel;
  10074. bool rela_normal;
  10075. struct bfd_elf_section_data *esdi, *esdo;
  10076. esdi = elf_section_data (o);
  10077. esdo = elf_section_data (o->output_section);
  10078. rela_normal = false;
  10079. /* Adjust the reloc addresses and symbol indices. */
  10080. irela = internal_relocs;
  10081. irelaend = irela + o->reloc_count;
  10082. rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count);
  10083. /* We start processing the REL relocs, if any. When we reach
  10084. IRELAMID in the loop, we switch to the RELA relocs. */
  10085. irelamid = irela;
  10086. if (esdi->rel.hdr != NULL)
  10087. irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr)
  10088. * bed->s->int_rels_per_ext_rel);
  10089. rel_hash_list = rel_hash;
  10090. rela_hash_list = NULL;
  10091. last_offset = o->output_offset;
  10092. if (!bfd_link_relocatable (flinfo->info))
  10093. last_offset += o->output_section->vma;
  10094. for (next_erel = 0; irela < irelaend; irela++, next_erel++)
  10095. {
  10096. unsigned long r_symndx;
  10097. asection *sec;
  10098. Elf_Internal_Sym sym;
  10099. if (next_erel == bed->s->int_rels_per_ext_rel)
  10100. {
  10101. rel_hash++;
  10102. next_erel = 0;
  10103. }
  10104. if (irela == irelamid)
  10105. {
  10106. rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count);
  10107. rela_hash_list = rel_hash;
  10108. rela_normal = bed->rela_normal;
  10109. }
  10110. irela->r_offset = _bfd_elf_section_offset (output_bfd,
  10111. flinfo->info, o,
  10112. irela->r_offset);
  10113. if (irela->r_offset >= (bfd_vma) -2)
  10114. {
  10115. /* This is a reloc for a deleted entry or somesuch.
  10116. Turn it into an R_*_NONE reloc, at the same
  10117. offset as the last reloc. elf_eh_frame.c and
  10118. bfd_elf_discard_info rely on reloc offsets
  10119. being ordered. */
  10120. irela->r_offset = last_offset;
  10121. irela->r_info = 0;
  10122. irela->r_addend = 0;
  10123. continue;
  10124. }
  10125. irela->r_offset += o->output_offset;
  10126. /* Relocs in an executable have to be virtual addresses. */
  10127. if (!bfd_link_relocatable (flinfo->info))
  10128. irela->r_offset += o->output_section->vma;
  10129. last_offset = irela->r_offset;
  10130. r_symndx = irela->r_info >> r_sym_shift;
  10131. if (r_symndx == STN_UNDEF)
  10132. continue;
  10133. if (r_symndx >= locsymcount
  10134. || (elf_bad_symtab (input_bfd)
  10135. && flinfo->sections[r_symndx] == NULL))
  10136. {
  10137. struct elf_link_hash_entry *rh;
  10138. unsigned long indx;
  10139. /* This is a reloc against a global symbol. We
  10140. have not yet output all the local symbols, so
  10141. we do not know the symbol index of any global
  10142. symbol. We set the rel_hash entry for this
  10143. reloc to point to the global hash table entry
  10144. for this symbol. The symbol index is then
  10145. set at the end of bfd_elf_final_link. */
  10146. indx = r_symndx - extsymoff;
  10147. rh = elf_sym_hashes (input_bfd)[indx];
  10148. while (rh->root.type == bfd_link_hash_indirect
  10149. || rh->root.type == bfd_link_hash_warning)
  10150. rh = (struct elf_link_hash_entry *) rh->root.u.i.link;
  10151. /* Setting the index to -2 tells
  10152. elf_link_output_extsym that this symbol is
  10153. used by a reloc. */
  10154. BFD_ASSERT (rh->indx < 0);
  10155. rh->indx = -2;
  10156. *rel_hash = rh;
  10157. continue;
  10158. }
  10159. /* This is a reloc against a local symbol. */
  10160. *rel_hash = NULL;
  10161. sym = isymbuf[r_symndx];
  10162. sec = flinfo->sections[r_symndx];
  10163. if (ELF_ST_TYPE (sym.st_info) == STT_SECTION)
  10164. {
  10165. /* I suppose the backend ought to fill in the
  10166. section of any STT_SECTION symbol against a
  10167. processor specific section. */
  10168. r_symndx = STN_UNDEF;
  10169. if (bfd_is_abs_section (sec))
  10170. ;
  10171. else if (sec == NULL || sec->owner == NULL)
  10172. {
  10173. bfd_set_error (bfd_error_bad_value);
  10174. return false;
  10175. }
  10176. else
  10177. {
  10178. asection *osec = sec->output_section;
  10179. /* If we have discarded a section, the output
  10180. section will be the absolute section. In
  10181. case of discarded SEC_MERGE sections, use
  10182. the kept section. relocate_section should
  10183. have already handled discarded linkonce
  10184. sections. */
  10185. if (bfd_is_abs_section (osec)
  10186. && sec->kept_section != NULL
  10187. && sec->kept_section->output_section != NULL)
  10188. {
  10189. osec = sec->kept_section->output_section;
  10190. irela->r_addend -= osec->vma;
  10191. }
  10192. if (!bfd_is_abs_section (osec))
  10193. {
  10194. r_symndx = osec->target_index;
  10195. if (r_symndx == STN_UNDEF)
  10196. {
  10197. irela->r_addend += osec->vma;
  10198. osec = _bfd_nearby_section (output_bfd, osec,
  10199. osec->vma);
  10200. irela->r_addend -= osec->vma;
  10201. r_symndx = osec->target_index;
  10202. }
  10203. }
  10204. }
  10205. /* Adjust the addend according to where the
  10206. section winds up in the output section. */
  10207. if (rela_normal)
  10208. irela->r_addend += sec->output_offset;
  10209. }
  10210. else
  10211. {
  10212. if (flinfo->indices[r_symndx] == -1)
  10213. {
  10214. unsigned long shlink;
  10215. const char *name;
  10216. asection *osec;
  10217. long indx;
  10218. if (flinfo->info->strip == strip_all)
  10219. {
  10220. /* You can't do ld -r -s. */
  10221. bfd_set_error (bfd_error_invalid_operation);
  10222. return false;
  10223. }
  10224. /* This symbol was skipped earlier, but
  10225. since it is needed by a reloc, we
  10226. must output it now. */
  10227. shlink = symtab_hdr->sh_link;
  10228. name = (bfd_elf_string_from_elf_section
  10229. (input_bfd, shlink, sym.st_name));
  10230. if (name == NULL)
  10231. return false;
  10232. osec = sec->output_section;
  10233. sym.st_shndx =
  10234. _bfd_elf_section_from_bfd_section (output_bfd,
  10235. osec);
  10236. if (sym.st_shndx == SHN_BAD)
  10237. return false;
  10238. sym.st_value += sec->output_offset;
  10239. if (!bfd_link_relocatable (flinfo->info))
  10240. {
  10241. sym.st_value += osec->vma;
  10242. if (ELF_ST_TYPE (sym.st_info) == STT_TLS)
  10243. {
  10244. struct elf_link_hash_table *htab
  10245. = elf_hash_table (flinfo->info);
  10246. /* STT_TLS symbols are relative to PT_TLS
  10247. segment base. */
  10248. if (htab->tls_sec != NULL)
  10249. sym.st_value -= htab->tls_sec->vma;
  10250. else
  10251. sym.st_info
  10252. = ELF_ST_INFO (ELF_ST_BIND (sym.st_info),
  10253. STT_NOTYPE);
  10254. }
  10255. }
  10256. indx = bfd_get_symcount (output_bfd);
  10257. ret = elf_link_output_symstrtab (flinfo, name,
  10258. &sym, sec,
  10259. NULL);
  10260. if (ret == 0)
  10261. return false;
  10262. else if (ret == 1)
  10263. flinfo->indices[r_symndx] = indx;
  10264. else
  10265. abort ();
  10266. }
  10267. r_symndx = flinfo->indices[r_symndx];
  10268. }
  10269. irela->r_info = ((bfd_vma) r_symndx << r_sym_shift
  10270. | (irela->r_info & r_type_mask));
  10271. }
  10272. /* Swap out the relocs. */
  10273. input_rel_hdr = esdi->rel.hdr;
  10274. if (input_rel_hdr && input_rel_hdr->sh_size != 0)
  10275. {
  10276. if (!bed->elf_backend_emit_relocs (output_bfd, o,
  10277. input_rel_hdr,
  10278. internal_relocs,
  10279. rel_hash_list))
  10280. return false;
  10281. internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr)
  10282. * bed->s->int_rels_per_ext_rel);
  10283. rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr);
  10284. }
  10285. input_rela_hdr = esdi->rela.hdr;
  10286. if (input_rela_hdr && input_rela_hdr->sh_size != 0)
  10287. {
  10288. if (!bed->elf_backend_emit_relocs (output_bfd, o,
  10289. input_rela_hdr,
  10290. internal_relocs,
  10291. rela_hash_list))
  10292. return false;
  10293. }
  10294. }
  10295. }
  10296. /* Write out the modified section contents. */
  10297. if (bed->elf_backend_write_section
  10298. && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o,
  10299. contents))
  10300. {
  10301. /* Section written out. */
  10302. }
  10303. else switch (o->sec_info_type)
  10304. {
  10305. case SEC_INFO_TYPE_STABS:
  10306. if (! (_bfd_write_section_stabs
  10307. (output_bfd,
  10308. &elf_hash_table (flinfo->info)->stab_info,
  10309. o, &elf_section_data (o)->sec_info, contents)))
  10310. return false;
  10311. break;
  10312. case SEC_INFO_TYPE_MERGE:
  10313. if (! _bfd_write_merged_section (output_bfd, o,
  10314. elf_section_data (o)->sec_info))
  10315. return false;
  10316. break;
  10317. case SEC_INFO_TYPE_EH_FRAME:
  10318. {
  10319. if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info,
  10320. o, contents))
  10321. return false;
  10322. }
  10323. break;
  10324. case SEC_INFO_TYPE_EH_FRAME_ENTRY:
  10325. {
  10326. if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
  10327. flinfo->info,
  10328. o, contents))
  10329. return false;
  10330. }
  10331. break;
  10332. default:
  10333. {
  10334. if (! (o->flags & SEC_EXCLUDE))
  10335. {
  10336. file_ptr offset = (file_ptr) o->output_offset;
  10337. bfd_size_type todo = o->size;
  10338. offset *= bfd_octets_per_byte (output_bfd, o);
  10339. if ((o->flags & SEC_ELF_REVERSE_COPY)
  10340. && o->size > address_size)
  10341. {
  10342. /* Reverse-copy input section to output. */
  10343. if ((o->size & (address_size - 1)) != 0
  10344. || (o->reloc_count != 0
  10345. && (o->size * bed->s->int_rels_per_ext_rel
  10346. != o->reloc_count * address_size)))
  10347. {
  10348. _bfd_error_handler
  10349. /* xgettext:c-format */
  10350. (_("error: %pB: size of section %pA is not "
  10351. "multiple of address size"),
  10352. input_bfd, o);
  10353. bfd_set_error (bfd_error_bad_value);
  10354. return false;
  10355. }
  10356. do
  10357. {
  10358. todo -= address_size;
  10359. if (! bfd_set_section_contents (output_bfd,
  10360. o->output_section,
  10361. contents + todo,
  10362. offset,
  10363. address_size))
  10364. return false;
  10365. if (todo == 0)
  10366. break;
  10367. offset += address_size;
  10368. }
  10369. while (1);
  10370. }
  10371. else if (! bfd_set_section_contents (output_bfd,
  10372. o->output_section,
  10373. contents,
  10374. offset, todo))
  10375. return false;
  10376. }
  10377. }
  10378. break;
  10379. }
  10380. }
  10381. return true;
  10382. }
  10383. /* Generate a reloc when linking an ELF file. This is a reloc
  10384. requested by the linker, and does not come from any input file. This
  10385. is used to build constructor and destructor tables when linking
  10386. with -Ur. */
  10387. static bool
  10388. elf_reloc_link_order (bfd *output_bfd,
  10389. struct bfd_link_info *info,
  10390. asection *output_section,
  10391. struct bfd_link_order *link_order)
  10392. {
  10393. reloc_howto_type *howto;
  10394. long indx;
  10395. bfd_vma offset;
  10396. bfd_vma addend;
  10397. struct bfd_elf_section_reloc_data *reldata;
  10398. struct elf_link_hash_entry **rel_hash_ptr;
  10399. Elf_Internal_Shdr *rel_hdr;
  10400. const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
  10401. Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL];
  10402. bfd_byte *erel;
  10403. unsigned int i;
  10404. struct bfd_elf_section_data *esdo = elf_section_data (output_section);
  10405. howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
  10406. if (howto == NULL)
  10407. {
  10408. bfd_set_error (bfd_error_bad_value);
  10409. return false;
  10410. }
  10411. addend = link_order->u.reloc.p->addend;
  10412. if (esdo->rel.hdr)
  10413. reldata = &esdo->rel;
  10414. else if (esdo->rela.hdr)
  10415. reldata = &esdo->rela;
  10416. else
  10417. {
  10418. reldata = NULL;
  10419. BFD_ASSERT (0);
  10420. }
  10421. /* Figure out the symbol index. */
  10422. rel_hash_ptr = reldata->hashes + reldata->count;
  10423. if (link_order->type == bfd_section_reloc_link_order)
  10424. {
  10425. indx = link_order->u.reloc.p->u.section->target_index;
  10426. BFD_ASSERT (indx != 0);
  10427. *rel_hash_ptr = NULL;
  10428. }
  10429. else
  10430. {
  10431. struct elf_link_hash_entry *h;
  10432. /* Treat a reloc against a defined symbol as though it were
  10433. actually against the section. */
  10434. h = ((struct elf_link_hash_entry *)
  10435. bfd_wrapped_link_hash_lookup (output_bfd, info,
  10436. link_order->u.reloc.p->u.name,
  10437. false, false, true));
  10438. if (h != NULL
  10439. && (h->root.type == bfd_link_hash_defined
  10440. || h->root.type == bfd_link_hash_defweak))
  10441. {
  10442. asection *section;
  10443. section = h->root.u.def.section;
  10444. indx = section->output_section->target_index;
  10445. *rel_hash_ptr = NULL;
  10446. /* It seems that we ought to add the symbol value to the
  10447. addend here, but in practice it has already been added
  10448. because it was passed to constructor_callback. */
  10449. addend += section->output_section->vma + section->output_offset;
  10450. }
  10451. else if (h != NULL)
  10452. {
  10453. /* Setting the index to -2 tells elf_link_output_extsym that
  10454. this symbol is used by a reloc. */
  10455. h->indx = -2;
  10456. *rel_hash_ptr = h;
  10457. indx = 0;
  10458. }
  10459. else
  10460. {
  10461. (*info->callbacks->unattached_reloc)
  10462. (info, link_order->u.reloc.p->u.name, NULL, NULL, 0);
  10463. indx = 0;
  10464. }
  10465. }
  10466. /* If this is an inplace reloc, we must write the addend into the
  10467. object file. */
  10468. if (howto->partial_inplace && addend != 0)
  10469. {
  10470. bfd_size_type size;
  10471. bfd_reloc_status_type rstat;
  10472. bfd_byte *buf;
  10473. bool ok;
  10474. const char *sym_name;
  10475. bfd_size_type octets;
  10476. size = (bfd_size_type) bfd_get_reloc_size (howto);
  10477. buf = (bfd_byte *) bfd_zmalloc (size);
  10478. if (buf == NULL && size != 0)
  10479. return false;
  10480. rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf);
  10481. switch (rstat)
  10482. {
  10483. case bfd_reloc_ok:
  10484. break;
  10485. default:
  10486. case bfd_reloc_outofrange:
  10487. abort ();
  10488. case bfd_reloc_overflow:
  10489. if (link_order->type == bfd_section_reloc_link_order)
  10490. sym_name = bfd_section_name (link_order->u.reloc.p->u.section);
  10491. else
  10492. sym_name = link_order->u.reloc.p->u.name;
  10493. (*info->callbacks->reloc_overflow) (info, NULL, sym_name,
  10494. howto->name, addend, NULL, NULL,
  10495. (bfd_vma) 0);
  10496. break;
  10497. }
  10498. octets = link_order->offset * bfd_octets_per_byte (output_bfd,
  10499. output_section);
  10500. ok = bfd_set_section_contents (output_bfd, output_section, buf,
  10501. octets, size);
  10502. free (buf);
  10503. if (! ok)
  10504. return false;
  10505. }
  10506. /* The address of a reloc is relative to the section in a
  10507. relocatable file, and is a virtual address in an executable
  10508. file. */
  10509. offset = link_order->offset;
  10510. if (! bfd_link_relocatable (info))
  10511. offset += output_section->vma;
  10512. for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
  10513. {
  10514. irel[i].r_offset = offset;
  10515. irel[i].r_info = 0;
  10516. irel[i].r_addend = 0;
  10517. }
  10518. if (bed->s->arch_size == 32)
  10519. irel[0].r_info = ELF32_R_INFO (indx, howto->type);
  10520. else
  10521. irel[0].r_info = ELF64_R_INFO (indx, howto->type);
  10522. rel_hdr = reldata->hdr;
  10523. erel = rel_hdr->contents;
  10524. if (rel_hdr->sh_type == SHT_REL)
  10525. {
  10526. erel += reldata->count * bed->s->sizeof_rel;
  10527. (*bed->s->swap_reloc_out) (output_bfd, irel, erel);
  10528. }
  10529. else
  10530. {
  10531. irel[0].r_addend = addend;
  10532. erel += reldata->count * bed->s->sizeof_rela;
  10533. (*bed->s->swap_reloca_out) (output_bfd, irel, erel);
  10534. }
  10535. ++reldata->count;
  10536. return true;
  10537. }
  10538. /* Generate an import library in INFO->implib_bfd from symbols in ABFD.
  10539. Returns TRUE upon success, FALSE otherwise. */
  10540. static bool
  10541. elf_output_implib (bfd *abfd, struct bfd_link_info *info)
  10542. {
  10543. bool ret = false;
  10544. bfd *implib_bfd;
  10545. const struct elf_backend_data *bed;
  10546. flagword flags;
  10547. enum bfd_architecture arch;
  10548. unsigned int mach;
  10549. asymbol **sympp = NULL;
  10550. long symsize;
  10551. long symcount;
  10552. long src_count;
  10553. elf_symbol_type *osymbuf;
  10554. size_t amt;
  10555. implib_bfd = info->out_implib_bfd;
  10556. bed = get_elf_backend_data (abfd);
  10557. if (!bfd_set_format (implib_bfd, bfd_object))
  10558. return false;
  10559. /* Use flag from executable but make it a relocatable object. */
  10560. flags = bfd_get_file_flags (abfd);
  10561. flags &= ~HAS_RELOC;
  10562. if (!bfd_set_start_address (implib_bfd, 0)
  10563. || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P))
  10564. return false;
  10565. /* Copy architecture of output file to import library file. */
  10566. arch = bfd_get_arch (abfd);
  10567. mach = bfd_get_mach (abfd);
  10568. if (!bfd_set_arch_mach (implib_bfd, arch, mach)
  10569. && (abfd->target_defaulted
  10570. || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd)))
  10571. return false;
  10572. /* Get symbol table size. */
  10573. symsize = bfd_get_symtab_upper_bound (abfd);
  10574. if (symsize < 0)
  10575. return false;
  10576. /* Read in the symbol table. */
  10577. sympp = (asymbol **) bfd_malloc (symsize);
  10578. if (sympp == NULL)
  10579. return false;
  10580. symcount = bfd_canonicalize_symtab (abfd, sympp);
  10581. if (symcount < 0)
  10582. goto free_sym_buf;
  10583. /* Allow the BFD backend to copy any private header data it
  10584. understands from the output BFD to the import library BFD. */
  10585. if (! bfd_copy_private_header_data (abfd, implib_bfd))
  10586. goto free_sym_buf;
  10587. /* Filter symbols to appear in the import library. */
  10588. if (bed->elf_backend_filter_implib_symbols)
  10589. symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp,
  10590. symcount);
  10591. else
  10592. symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount);
  10593. if (symcount == 0)
  10594. {
  10595. bfd_set_error (bfd_error_no_symbols);
  10596. _bfd_error_handler (_("%pB: no symbol found for import library"),
  10597. implib_bfd);
  10598. goto free_sym_buf;
  10599. }
  10600. /* Make symbols absolute. */
  10601. amt = symcount * sizeof (*osymbuf);
  10602. osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt);
  10603. if (osymbuf == NULL)
  10604. goto free_sym_buf;
  10605. for (src_count = 0; src_count < symcount; src_count++)
  10606. {
  10607. memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count],
  10608. sizeof (*osymbuf));
  10609. osymbuf[src_count].symbol.section = bfd_abs_section_ptr;
  10610. osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS;
  10611. osymbuf[src_count].symbol.value += sympp[src_count]->section->vma;
  10612. osymbuf[src_count].internal_elf_sym.st_value =
  10613. osymbuf[src_count].symbol.value;
  10614. sympp[src_count] = &osymbuf[src_count].symbol;
  10615. }
  10616. bfd_set_symtab (implib_bfd, sympp, symcount);
  10617. /* Allow the BFD backend to copy any private data it understands
  10618. from the output BFD to the import library BFD. This is done last
  10619. to permit the routine to look at the filtered symbol table. */
  10620. if (! bfd_copy_private_bfd_data (abfd, implib_bfd))
  10621. goto free_sym_buf;
  10622. if (!bfd_close (implib_bfd))
  10623. goto free_sym_buf;
  10624. ret = true;
  10625. free_sym_buf:
  10626. free (sympp);
  10627. return ret;
  10628. }
  10629. static void
  10630. elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo)
  10631. {
  10632. asection *o;
  10633. if (flinfo->symstrtab != NULL)
  10634. _bfd_elf_strtab_free (flinfo->symstrtab);
  10635. free (flinfo->contents);
  10636. free (flinfo->external_relocs);
  10637. free (flinfo->internal_relocs);
  10638. free (flinfo->external_syms);
  10639. free (flinfo->locsym_shndx);
  10640. free (flinfo->internal_syms);
  10641. free (flinfo->indices);
  10642. free (flinfo->sections);
  10643. if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1)
  10644. free (flinfo->symshndxbuf);
  10645. for (o = obfd->sections; o != NULL; o = o->next)
  10646. {
  10647. struct bfd_elf_section_data *esdo = elf_section_data (o);
  10648. free (esdo->rel.hashes);
  10649. free (esdo->rela.hashes);
  10650. }
  10651. }
  10652. /* Do the final step of an ELF link. */
  10653. bool
  10654. bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
  10655. {
  10656. bool dynamic;
  10657. bool emit_relocs;
  10658. bfd *dynobj;
  10659. struct elf_final_link_info flinfo;
  10660. asection *o;
  10661. struct bfd_link_order *p;
  10662. bfd *sub;
  10663. bfd_size_type max_contents_size;
  10664. bfd_size_type max_external_reloc_size;
  10665. bfd_size_type max_internal_reloc_count;
  10666. bfd_size_type max_sym_count;
  10667. bfd_size_type max_sym_shndx_count;
  10668. Elf_Internal_Sym elfsym;
  10669. unsigned int i;
  10670. Elf_Internal_Shdr *symtab_hdr;
  10671. Elf_Internal_Shdr *symtab_shndx_hdr;
  10672. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  10673. struct elf_outext_info eoinfo;
  10674. bool merged;
  10675. size_t relativecount;
  10676. size_t relr_entsize;
  10677. asection *reldyn = 0;
  10678. bfd_size_type amt;
  10679. asection *attr_section = NULL;
  10680. bfd_vma attr_size = 0;
  10681. const char *std_attrs_section;
  10682. struct elf_link_hash_table *htab = elf_hash_table (info);
  10683. bool sections_removed;
  10684. bool ret;
  10685. if (!is_elf_hash_table (&htab->root))
  10686. return false;
  10687. if (bfd_link_pic (info))
  10688. abfd->flags |= DYNAMIC;
  10689. dynamic = htab->dynamic_sections_created;
  10690. dynobj = htab->dynobj;
  10691. emit_relocs = (bfd_link_relocatable (info)
  10692. || info->emitrelocations);
  10693. memset (&flinfo, 0, sizeof (flinfo));
  10694. flinfo.info = info;
  10695. flinfo.output_bfd = abfd;
  10696. flinfo.symstrtab = _bfd_elf_strtab_init ();
  10697. if (flinfo.symstrtab == NULL)
  10698. return false;
  10699. if (! dynamic)
  10700. {
  10701. flinfo.hash_sec = NULL;
  10702. flinfo.symver_sec = NULL;
  10703. }
  10704. else
  10705. {
  10706. flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash");
  10707. /* Note that dynsym_sec can be NULL (on VMS). */
  10708. flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version");
  10709. /* Note that it is OK if symver_sec is NULL. */
  10710. }
  10711. if (info->unique_symbol
  10712. && !bfd_hash_table_init (&flinfo.local_hash_table,
  10713. local_hash_newfunc,
  10714. sizeof (struct local_hash_entry)))
  10715. return false;
  10716. /* The object attributes have been merged. Remove the input
  10717. sections from the link, and set the contents of the output
  10718. section. */
  10719. sections_removed = false;
  10720. std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section;
  10721. for (o = abfd->sections; o != NULL; o = o->next)
  10722. {
  10723. bool remove_section = false;
  10724. if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0)
  10725. || strcmp (o->name, ".gnu.attributes") == 0)
  10726. {
  10727. for (p = o->map_head.link_order; p != NULL; p = p->next)
  10728. {
  10729. asection *input_section;
  10730. if (p->type != bfd_indirect_link_order)
  10731. continue;
  10732. input_section = p->u.indirect.section;
  10733. /* Hack: reset the SEC_HAS_CONTENTS flag so that
  10734. elf_link_input_bfd ignores this section. */
  10735. input_section->flags &= ~SEC_HAS_CONTENTS;
  10736. }
  10737. attr_size = bfd_elf_obj_attr_size (abfd);
  10738. bfd_set_section_size (o, attr_size);
  10739. /* Skip this section later on. */
  10740. o->map_head.link_order = NULL;
  10741. if (attr_size)
  10742. attr_section = o;
  10743. else
  10744. remove_section = true;
  10745. }
  10746. else if ((o->flags & SEC_GROUP) != 0 && o->size == 0)
  10747. {
  10748. /* Remove empty group section from linker output. */
  10749. remove_section = true;
  10750. }
  10751. if (remove_section)
  10752. {
  10753. o->flags |= SEC_EXCLUDE;
  10754. bfd_section_list_remove (abfd, o);
  10755. abfd->section_count--;
  10756. sections_removed = true;
  10757. }
  10758. }
  10759. if (sections_removed)
  10760. _bfd_fix_excluded_sec_syms (abfd, info);
  10761. /* Count up the number of relocations we will output for each output
  10762. section, so that we know the sizes of the reloc sections. We
  10763. also figure out some maximum sizes. */
  10764. max_contents_size = 0;
  10765. max_external_reloc_size = 0;
  10766. max_internal_reloc_count = 0;
  10767. max_sym_count = 0;
  10768. max_sym_shndx_count = 0;
  10769. merged = false;
  10770. for (o = abfd->sections; o != NULL; o = o->next)
  10771. {
  10772. struct bfd_elf_section_data *esdo = elf_section_data (o);
  10773. o->reloc_count = 0;
  10774. for (p = o->map_head.link_order; p != NULL; p = p->next)
  10775. {
  10776. unsigned int reloc_count = 0;
  10777. unsigned int additional_reloc_count = 0;
  10778. struct bfd_elf_section_data *esdi = NULL;
  10779. if (p->type == bfd_section_reloc_link_order
  10780. || p->type == bfd_symbol_reloc_link_order)
  10781. reloc_count = 1;
  10782. else if (p->type == bfd_indirect_link_order)
  10783. {
  10784. asection *sec;
  10785. sec = p->u.indirect.section;
  10786. /* Mark all sections which are to be included in the
  10787. link. This will normally be every section. We need
  10788. to do this so that we can identify any sections which
  10789. the linker has decided to not include. */
  10790. sec->linker_mark = true;
  10791. if (sec->flags & SEC_MERGE)
  10792. merged = true;
  10793. if (sec->rawsize > max_contents_size)
  10794. max_contents_size = sec->rawsize;
  10795. if (sec->size > max_contents_size)
  10796. max_contents_size = sec->size;
  10797. if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour
  10798. && (sec->owner->flags & DYNAMIC) == 0)
  10799. {
  10800. size_t sym_count;
  10801. /* We are interested in just local symbols, not all
  10802. symbols. */
  10803. if (elf_bad_symtab (sec->owner))
  10804. sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size
  10805. / bed->s->sizeof_sym);
  10806. else
  10807. sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info;
  10808. if (sym_count > max_sym_count)
  10809. max_sym_count = sym_count;
  10810. if (sym_count > max_sym_shndx_count
  10811. && elf_symtab_shndx_list (sec->owner) != NULL)
  10812. max_sym_shndx_count = sym_count;
  10813. if (esdo->this_hdr.sh_type == SHT_REL
  10814. || esdo->this_hdr.sh_type == SHT_RELA)
  10815. /* Some backends use reloc_count in relocation sections
  10816. to count particular types of relocs. Of course,
  10817. reloc sections themselves can't have relocations. */
  10818. ;
  10819. else if (emit_relocs)
  10820. {
  10821. reloc_count = sec->reloc_count;
  10822. if (bed->elf_backend_count_additional_relocs)
  10823. {
  10824. int c;
  10825. c = (*bed->elf_backend_count_additional_relocs) (sec);
  10826. additional_reloc_count += c;
  10827. }
  10828. }
  10829. else if (bed->elf_backend_count_relocs)
  10830. reloc_count = (*bed->elf_backend_count_relocs) (info, sec);
  10831. esdi = elf_section_data (sec);
  10832. if ((sec->flags & SEC_RELOC) != 0)
  10833. {
  10834. size_t ext_size = 0;
  10835. if (esdi->rel.hdr != NULL)
  10836. ext_size = esdi->rel.hdr->sh_size;
  10837. if (esdi->rela.hdr != NULL)
  10838. ext_size += esdi->rela.hdr->sh_size;
  10839. if (ext_size > max_external_reloc_size)
  10840. max_external_reloc_size = ext_size;
  10841. if (sec->reloc_count > max_internal_reloc_count)
  10842. max_internal_reloc_count = sec->reloc_count;
  10843. }
  10844. }
  10845. }
  10846. if (reloc_count == 0)
  10847. continue;
  10848. reloc_count += additional_reloc_count;
  10849. o->reloc_count += reloc_count;
  10850. if (p->type == bfd_indirect_link_order && emit_relocs)
  10851. {
  10852. if (esdi->rel.hdr)
  10853. {
  10854. esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr);
  10855. esdo->rel.count += additional_reloc_count;
  10856. }
  10857. if (esdi->rela.hdr)
  10858. {
  10859. esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr);
  10860. esdo->rela.count += additional_reloc_count;
  10861. }
  10862. }
  10863. else
  10864. {
  10865. if (o->use_rela_p)
  10866. esdo->rela.count += reloc_count;
  10867. else
  10868. esdo->rel.count += reloc_count;
  10869. }
  10870. }
  10871. if (o->reloc_count > 0)
  10872. o->flags |= SEC_RELOC;
  10873. else
  10874. {
  10875. /* Explicitly clear the SEC_RELOC flag. The linker tends to
  10876. set it (this is probably a bug) and if it is set
  10877. assign_section_numbers will create a reloc section. */
  10878. o->flags &=~ SEC_RELOC;
  10879. }
  10880. /* If the SEC_ALLOC flag is not set, force the section VMA to
  10881. zero. This is done in elf_fake_sections as well, but forcing
  10882. the VMA to 0 here will ensure that relocs against these
  10883. sections are handled correctly. */
  10884. if ((o->flags & SEC_ALLOC) == 0
  10885. && ! o->user_set_vma)
  10886. o->vma = 0;
  10887. }
  10888. if (! bfd_link_relocatable (info) && merged)
  10889. elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd);
  10890. /* Figure out the file positions for everything but the symbol table
  10891. and the relocs. We set symcount to force assign_section_numbers
  10892. to create a symbol table. */
  10893. abfd->symcount = info->strip != strip_all || emit_relocs;
  10894. BFD_ASSERT (! abfd->output_has_begun);
  10895. if (! _bfd_elf_compute_section_file_positions (abfd, info))
  10896. goto error_return;
  10897. /* Set sizes, and assign file positions for reloc sections. */
  10898. for (o = abfd->sections; o != NULL; o = o->next)
  10899. {
  10900. struct bfd_elf_section_data *esdo = elf_section_data (o);
  10901. if ((o->flags & SEC_RELOC) != 0)
  10902. {
  10903. if (esdo->rel.hdr
  10904. && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel)))
  10905. goto error_return;
  10906. if (esdo->rela.hdr
  10907. && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela)))
  10908. goto error_return;
  10909. }
  10910. /* _bfd_elf_compute_section_file_positions makes temporary use
  10911. of target_index. Reset it. */
  10912. o->target_index = 0;
  10913. /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them
  10914. to count upwards while actually outputting the relocations. */
  10915. esdo->rel.count = 0;
  10916. esdo->rela.count = 0;
  10917. if ((esdo->this_hdr.sh_offset == (file_ptr) -1)
  10918. && !bfd_section_is_ctf (o))
  10919. {
  10920. /* Cache the section contents so that they can be compressed
  10921. later. Use bfd_malloc since it will be freed by
  10922. bfd_compress_section_contents. */
  10923. unsigned char *contents = esdo->this_hdr.contents;
  10924. if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
  10925. abort ();
  10926. contents
  10927. = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
  10928. if (contents == NULL)
  10929. goto error_return;
  10930. esdo->this_hdr.contents = contents;
  10931. }
  10932. }
  10933. /* We have now assigned file positions for all the sections except .symtab,
  10934. .strtab, and non-loaded reloc and compressed debugging sections. We start
  10935. the .symtab section at the current file position, and write directly to it.
  10936. We build the .strtab section in memory. */
  10937. abfd->symcount = 0;
  10938. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  10939. /* sh_name is set in prep_headers. */
  10940. symtab_hdr->sh_type = SHT_SYMTAB;
  10941. /* sh_flags, sh_addr and sh_size all start off zero. */
  10942. symtab_hdr->sh_entsize = bed->s->sizeof_sym;
  10943. /* sh_link is set in assign_section_numbers. */
  10944. /* sh_info is set below. */
  10945. /* sh_offset is set just below. */
  10946. symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
  10947. if (max_sym_count < 20)
  10948. max_sym_count = 20;
  10949. htab->strtabsize = max_sym_count;
  10950. amt = max_sym_count * sizeof (struct elf_sym_strtab);
  10951. htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt);
  10952. if (htab->strtab == NULL)
  10953. goto error_return;
  10954. /* The real buffer will be allocated in elf_link_swap_symbols_out. */
  10955. flinfo.symshndxbuf
  10956. = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)
  10957. ? (Elf_External_Sym_Shndx *) -1 : NULL);
  10958. if (info->strip != strip_all || emit_relocs)
  10959. {
  10960. file_ptr off = elf_next_file_pos (abfd);
  10961. _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true);
  10962. /* Note that at this point elf_next_file_pos (abfd) is
  10963. incorrect. We do not yet know the size of the .symtab section.
  10964. We correct next_file_pos below, after we do know the size. */
  10965. /* Start writing out the symbol table. The first symbol is always a
  10966. dummy symbol. */
  10967. elfsym.st_value = 0;
  10968. elfsym.st_size = 0;
  10969. elfsym.st_info = 0;
  10970. elfsym.st_other = 0;
  10971. elfsym.st_shndx = SHN_UNDEF;
  10972. elfsym.st_target_internal = 0;
  10973. if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym,
  10974. bfd_und_section_ptr, NULL) != 1)
  10975. goto error_return;
  10976. /* Output a symbol for each section if asked or they are used for
  10977. relocs. These symbols usually have no names. We store the
  10978. index of each one in the index field of the section, so that
  10979. we can find it again when outputting relocs. */
  10980. if (bfd_keep_unused_section_symbols (abfd) || emit_relocs)
  10981. {
  10982. bool name_local_sections
  10983. = (bed->elf_backend_name_local_section_symbols
  10984. && bed->elf_backend_name_local_section_symbols (abfd));
  10985. const char *name = NULL;
  10986. elfsym.st_size = 0;
  10987. elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
  10988. elfsym.st_other = 0;
  10989. elfsym.st_value = 0;
  10990. elfsym.st_target_internal = 0;
  10991. for (i = 1; i < elf_numsections (abfd); i++)
  10992. {
  10993. o = bfd_section_from_elf_index (abfd, i);
  10994. if (o != NULL)
  10995. {
  10996. o->target_index = bfd_get_symcount (abfd);
  10997. elfsym.st_shndx = i;
  10998. if (!bfd_link_relocatable (info))
  10999. elfsym.st_value = o->vma;
  11000. if (name_local_sections)
  11001. name = o->name;
  11002. if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o,
  11003. NULL) != 1)
  11004. goto error_return;
  11005. }
  11006. }
  11007. }
  11008. }
  11009. /* On some targets like Irix 5 the symbol split between local and global
  11010. ones recorded in the sh_info field needs to be done between section
  11011. and all other symbols. */
  11012. if (bed->elf_backend_elfsym_local_is_section
  11013. && bed->elf_backend_elfsym_local_is_section (abfd))
  11014. symtab_hdr->sh_info = bfd_get_symcount (abfd);
  11015. /* Allocate some memory to hold information read in from the input
  11016. files. */
  11017. if (max_contents_size != 0)
  11018. {
  11019. flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
  11020. if (flinfo.contents == NULL)
  11021. goto error_return;
  11022. }
  11023. if (max_external_reloc_size != 0)
  11024. {
  11025. flinfo.external_relocs = bfd_malloc (max_external_reloc_size);
  11026. if (flinfo.external_relocs == NULL)
  11027. goto error_return;
  11028. }
  11029. if (max_internal_reloc_count != 0)
  11030. {
  11031. amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela);
  11032. flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt);
  11033. if (flinfo.internal_relocs == NULL)
  11034. goto error_return;
  11035. }
  11036. if (max_sym_count != 0)
  11037. {
  11038. amt = max_sym_count * bed->s->sizeof_sym;
  11039. flinfo.external_syms = (bfd_byte *) bfd_malloc (amt);
  11040. if (flinfo.external_syms == NULL)
  11041. goto error_return;
  11042. amt = max_sym_count * sizeof (Elf_Internal_Sym);
  11043. flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
  11044. if (flinfo.internal_syms == NULL)
  11045. goto error_return;
  11046. amt = max_sym_count * sizeof (long);
  11047. flinfo.indices = (long int *) bfd_malloc (amt);
  11048. if (flinfo.indices == NULL)
  11049. goto error_return;
  11050. amt = max_sym_count * sizeof (asection *);
  11051. flinfo.sections = (asection **) bfd_malloc (amt);
  11052. if (flinfo.sections == NULL)
  11053. goto error_return;
  11054. }
  11055. if (max_sym_shndx_count != 0)
  11056. {
  11057. amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx);
  11058. flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
  11059. if (flinfo.locsym_shndx == NULL)
  11060. goto error_return;
  11061. }
  11062. if (htab->tls_sec)
  11063. {
  11064. bfd_vma base, end = 0; /* Both bytes. */
  11065. asection *sec;
  11066. for (sec = htab->tls_sec;
  11067. sec && (sec->flags & SEC_THREAD_LOCAL);
  11068. sec = sec->next)
  11069. {
  11070. bfd_size_type size = sec->size;
  11071. unsigned int opb = bfd_octets_per_byte (abfd, sec);
  11072. if (size == 0
  11073. && (sec->flags & SEC_HAS_CONTENTS) == 0)
  11074. {
  11075. struct bfd_link_order *ord = sec->map_tail.link_order;
  11076. if (ord != NULL)
  11077. size = ord->offset * opb + ord->size;
  11078. }
  11079. end = sec->vma + size / opb;
  11080. }
  11081. base = htab->tls_sec->vma;
  11082. /* Only align end of TLS section if static TLS doesn't have special
  11083. alignment requirements. */
  11084. if (bed->static_tls_alignment == 1)
  11085. end = align_power (end, htab->tls_sec->alignment_power);
  11086. htab->tls_size = end - base;
  11087. }
  11088. if (!_bfd_elf_fixup_eh_frame_hdr (info))
  11089. return false;
  11090. /* Finish relative relocations here after regular symbol processing
  11091. is finished if DT_RELR is enabled. */
  11092. if (info->enable_dt_relr
  11093. && bed->finish_relative_relocs
  11094. && !bed->finish_relative_relocs (info))
  11095. info->callbacks->einfo
  11096. (_("%F%P: %pB: failed to finish relative relocations\n"), abfd);
  11097. /* Since ELF permits relocations to be against local symbols, we
  11098. must have the local symbols available when we do the relocations.
  11099. Since we would rather only read the local symbols once, and we
  11100. would rather not keep them in memory, we handle all the
  11101. relocations for a single input file at the same time.
  11102. Unfortunately, there is no way to know the total number of local
  11103. symbols until we have seen all of them, and the local symbol
  11104. indices precede the global symbol indices. This means that when
  11105. we are generating relocatable output, and we see a reloc against
  11106. a global symbol, we can not know the symbol index until we have
  11107. finished examining all the local symbols to see which ones we are
  11108. going to output. To deal with this, we keep the relocations in
  11109. memory, and don't output them until the end of the link. This is
  11110. an unfortunate waste of memory, but I don't see a good way around
  11111. it. Fortunately, it only happens when performing a relocatable
  11112. link, which is not the common case. FIXME: If keep_memory is set
  11113. we could write the relocs out and then read them again; I don't
  11114. know how bad the memory loss will be. */
  11115. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  11116. sub->output_has_begun = false;
  11117. for (o = abfd->sections; o != NULL; o = o->next)
  11118. {
  11119. for (p = o->map_head.link_order; p != NULL; p = p->next)
  11120. {
  11121. if (p->type == bfd_indirect_link_order
  11122. && (bfd_get_flavour ((sub = p->u.indirect.section->owner))
  11123. == bfd_target_elf_flavour)
  11124. && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass)
  11125. {
  11126. if (! sub->output_has_begun)
  11127. {
  11128. if (! elf_link_input_bfd (&flinfo, sub))
  11129. goto error_return;
  11130. sub->output_has_begun = true;
  11131. }
  11132. }
  11133. else if (p->type == bfd_section_reloc_link_order
  11134. || p->type == bfd_symbol_reloc_link_order)
  11135. {
  11136. if (! elf_reloc_link_order (abfd, info, o, p))
  11137. goto error_return;
  11138. }
  11139. else
  11140. {
  11141. if (! _bfd_default_link_order (abfd, info, o, p))
  11142. {
  11143. if (p->type == bfd_indirect_link_order
  11144. && (bfd_get_flavour (sub)
  11145. == bfd_target_elf_flavour)
  11146. && (elf_elfheader (sub)->e_ident[EI_CLASS]
  11147. != bed->s->elfclass))
  11148. {
  11149. const char *iclass, *oclass;
  11150. switch (bed->s->elfclass)
  11151. {
  11152. case ELFCLASS64: oclass = "ELFCLASS64"; break;
  11153. case ELFCLASS32: oclass = "ELFCLASS32"; break;
  11154. case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break;
  11155. default: abort ();
  11156. }
  11157. switch (elf_elfheader (sub)->e_ident[EI_CLASS])
  11158. {
  11159. case ELFCLASS64: iclass = "ELFCLASS64"; break;
  11160. case ELFCLASS32: iclass = "ELFCLASS32"; break;
  11161. case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break;
  11162. default: abort ();
  11163. }
  11164. bfd_set_error (bfd_error_wrong_format);
  11165. _bfd_error_handler
  11166. /* xgettext:c-format */
  11167. (_("%pB: file class %s incompatible with %s"),
  11168. sub, iclass, oclass);
  11169. }
  11170. goto error_return;
  11171. }
  11172. }
  11173. }
  11174. }
  11175. /* Free symbol buffer if needed. */
  11176. if (!info->reduce_memory_overheads)
  11177. {
  11178. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  11179. if (bfd_get_flavour (sub) == bfd_target_elf_flavour)
  11180. {
  11181. free (elf_tdata (sub)->symbuf);
  11182. elf_tdata (sub)->symbuf = NULL;
  11183. }
  11184. }
  11185. ret = true;
  11186. /* Output any global symbols that got converted to local in a
  11187. version script or due to symbol visibility. We do this in a
  11188. separate step since ELF requires all local symbols to appear
  11189. prior to any global symbols. FIXME: We should only do this if
  11190. some global symbols were, in fact, converted to become local.
  11191. FIXME: Will this work correctly with the Irix 5 linker? */
  11192. eoinfo.failed = false;
  11193. eoinfo.flinfo = &flinfo;
  11194. eoinfo.localsyms = true;
  11195. eoinfo.file_sym_done = false;
  11196. bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
  11197. if (eoinfo.failed)
  11198. {
  11199. ret = false;
  11200. goto return_local_hash_table;
  11201. }
  11202. /* If backend needs to output some local symbols not present in the hash
  11203. table, do it now. */
  11204. if (bed->elf_backend_output_arch_local_syms
  11205. && (info->strip != strip_all || emit_relocs))
  11206. {
  11207. if (! ((*bed->elf_backend_output_arch_local_syms)
  11208. (abfd, info, &flinfo, elf_link_output_symstrtab)))
  11209. {
  11210. ret = false;
  11211. goto return_local_hash_table;
  11212. }
  11213. }
  11214. /* That wrote out all the local symbols. Finish up the symbol table
  11215. with the global symbols. Even if we want to strip everything we
  11216. can, we still need to deal with those global symbols that got
  11217. converted to local in a version script. */
  11218. /* The sh_info field records the index of the first non local symbol. */
  11219. if (!symtab_hdr->sh_info)
  11220. symtab_hdr->sh_info = bfd_get_symcount (abfd);
  11221. if (dynamic
  11222. && htab->dynsym != NULL
  11223. && htab->dynsym->output_section != bfd_abs_section_ptr)
  11224. {
  11225. Elf_Internal_Sym sym;
  11226. bfd_byte *dynsym = htab->dynsym->contents;
  11227. o = htab->dynsym->output_section;
  11228. elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1;
  11229. /* Write out the section symbols for the output sections. */
  11230. if (bfd_link_pic (info)
  11231. || htab->is_relocatable_executable)
  11232. {
  11233. asection *s;
  11234. sym.st_size = 0;
  11235. sym.st_name = 0;
  11236. sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
  11237. sym.st_other = 0;
  11238. sym.st_target_internal = 0;
  11239. for (s = abfd->sections; s != NULL; s = s->next)
  11240. {
  11241. int indx;
  11242. bfd_byte *dest;
  11243. long dynindx;
  11244. dynindx = elf_section_data (s)->dynindx;
  11245. if (dynindx <= 0)
  11246. continue;
  11247. indx = elf_section_data (s)->this_idx;
  11248. BFD_ASSERT (indx > 0);
  11249. sym.st_shndx = indx;
  11250. if (! check_dynsym (abfd, &sym))
  11251. {
  11252. ret = false;
  11253. goto return_local_hash_table;
  11254. }
  11255. sym.st_value = s->vma;
  11256. dest = dynsym + dynindx * bed->s->sizeof_sym;
  11257. /* Inform the linker of the addition of this symbol. */
  11258. if (info->callbacks->ctf_new_dynsym)
  11259. info->callbacks->ctf_new_dynsym (dynindx, &sym);
  11260. bed->s->swap_symbol_out (abfd, &sym, dest, 0);
  11261. }
  11262. }
  11263. /* Write out the local dynsyms. */
  11264. if (htab->dynlocal)
  11265. {
  11266. struct elf_link_local_dynamic_entry *e;
  11267. for (e = htab->dynlocal; e ; e = e->next)
  11268. {
  11269. asection *s;
  11270. bfd_byte *dest;
  11271. /* Copy the internal symbol and turn off visibility.
  11272. Note that we saved a word of storage and overwrote
  11273. the original st_name with the dynstr_index. */
  11274. sym = e->isym;
  11275. sym.st_other &= ~ELF_ST_VISIBILITY (-1);
  11276. sym.st_shndx = SHN_UNDEF;
  11277. s = bfd_section_from_elf_index (e->input_bfd,
  11278. e->isym.st_shndx);
  11279. if (s != NULL
  11280. && s->output_section != NULL
  11281. && elf_section_data (s->output_section) != NULL)
  11282. {
  11283. sym.st_shndx =
  11284. elf_section_data (s->output_section)->this_idx;
  11285. if (! check_dynsym (abfd, &sym))
  11286. {
  11287. ret = false;
  11288. goto return_local_hash_table;
  11289. }
  11290. sym.st_value = (s->output_section->vma
  11291. + s->output_offset
  11292. + e->isym.st_value);
  11293. }
  11294. /* Inform the linker of the addition of this symbol. */
  11295. if (info->callbacks->ctf_new_dynsym)
  11296. info->callbacks->ctf_new_dynsym (e->dynindx, &sym);
  11297. dest = dynsym + e->dynindx * bed->s->sizeof_sym;
  11298. bed->s->swap_symbol_out (abfd, &sym, dest, 0);
  11299. }
  11300. }
  11301. }
  11302. /* We get the global symbols from the hash table. */
  11303. eoinfo.failed = false;
  11304. eoinfo.localsyms = false;
  11305. eoinfo.flinfo = &flinfo;
  11306. bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
  11307. if (eoinfo.failed)
  11308. {
  11309. ret = false;
  11310. goto return_local_hash_table;
  11311. }
  11312. /* If backend needs to output some symbols not present in the hash
  11313. table, do it now. */
  11314. if (bed->elf_backend_output_arch_syms
  11315. && (info->strip != strip_all || emit_relocs))
  11316. {
  11317. if (! ((*bed->elf_backend_output_arch_syms)
  11318. (abfd, info, &flinfo, elf_link_output_symstrtab)))
  11319. {
  11320. ret = false;
  11321. goto return_local_hash_table;
  11322. }
  11323. }
  11324. /* Finalize the .strtab section. */
  11325. _bfd_elf_strtab_finalize (flinfo.symstrtab);
  11326. /* Swap out the .strtab section. */
  11327. if (!elf_link_swap_symbols_out (&flinfo))
  11328. {
  11329. ret = false;
  11330. goto return_local_hash_table;
  11331. }
  11332. /* Now we know the size of the symtab section. */
  11333. if (bfd_get_symcount (abfd) > 0)
  11334. {
  11335. /* Finish up and write out the symbol string table (.strtab)
  11336. section. */
  11337. Elf_Internal_Shdr *symstrtab_hdr = NULL;
  11338. file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
  11339. if (elf_symtab_shndx_list (abfd))
  11340. {
  11341. symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
  11342. if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0)
  11343. {
  11344. symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
  11345. symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
  11346. symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
  11347. amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
  11348. symtab_shndx_hdr->sh_size = amt;
  11349. off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
  11350. off, true);
  11351. if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
  11352. || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
  11353. {
  11354. ret = false;
  11355. goto return_local_hash_table;
  11356. }
  11357. }
  11358. }
  11359. symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
  11360. /* sh_name was set in prep_headers. */
  11361. symstrtab_hdr->sh_type = SHT_STRTAB;
  11362. symstrtab_hdr->sh_flags = bed->elf_strtab_flags;
  11363. symstrtab_hdr->sh_addr = 0;
  11364. symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab);
  11365. symstrtab_hdr->sh_entsize = 0;
  11366. symstrtab_hdr->sh_link = 0;
  11367. symstrtab_hdr->sh_info = 0;
  11368. /* sh_offset is set just below. */
  11369. symstrtab_hdr->sh_addralign = 1;
  11370. off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr,
  11371. off, true);
  11372. elf_next_file_pos (abfd) = off;
  11373. if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
  11374. || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
  11375. {
  11376. ret = false;
  11377. goto return_local_hash_table;
  11378. }
  11379. }
  11380. if (info->out_implib_bfd && !elf_output_implib (abfd, info))
  11381. {
  11382. _bfd_error_handler (_("%pB: failed to generate import library"),
  11383. info->out_implib_bfd);
  11384. ret = false;
  11385. goto return_local_hash_table;
  11386. }
  11387. /* Adjust the relocs to have the correct symbol indices. */
  11388. for (o = abfd->sections; o != NULL; o = o->next)
  11389. {
  11390. struct bfd_elf_section_data *esdo = elf_section_data (o);
  11391. bool sort;
  11392. if ((o->flags & SEC_RELOC) == 0)
  11393. continue;
  11394. sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
  11395. if (esdo->rel.hdr != NULL
  11396. && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
  11397. {
  11398. ret = false;
  11399. goto return_local_hash_table;
  11400. }
  11401. if (esdo->rela.hdr != NULL
  11402. && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
  11403. {
  11404. ret = false;
  11405. goto return_local_hash_table;
  11406. }
  11407. /* Set the reloc_count field to 0 to prevent write_relocs from
  11408. trying to swap the relocs out itself. */
  11409. o->reloc_count = 0;
  11410. }
  11411. relativecount = 0;
  11412. if (dynamic && info->combreloc && dynobj != NULL)
  11413. relativecount = elf_link_sort_relocs (abfd, info, &reldyn);
  11414. relr_entsize = 0;
  11415. if (htab->srelrdyn != NULL
  11416. && htab->srelrdyn->output_section != NULL
  11417. && htab->srelrdyn->size != 0)
  11418. {
  11419. asection *s = htab->srelrdyn->output_section;
  11420. relr_entsize = elf_section_data (s)->this_hdr.sh_entsize;
  11421. if (relr_entsize == 0)
  11422. {
  11423. relr_entsize = bed->s->arch_size / 8;
  11424. elf_section_data (s)->this_hdr.sh_entsize = relr_entsize;
  11425. }
  11426. }
  11427. /* If we are linking against a dynamic object, or generating a
  11428. shared library, finish up the dynamic linking information. */
  11429. if (dynamic)
  11430. {
  11431. bfd_byte *dyncon, *dynconend;
  11432. /* Fix up .dynamic entries. */
  11433. o = bfd_get_linker_section (dynobj, ".dynamic");
  11434. BFD_ASSERT (o != NULL);
  11435. dyncon = o->contents;
  11436. dynconend = PTR_ADD (o->contents, o->size);
  11437. for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
  11438. {
  11439. Elf_Internal_Dyn dyn;
  11440. const char *name;
  11441. unsigned int type;
  11442. bfd_size_type sh_size;
  11443. bfd_vma sh_addr;
  11444. bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
  11445. switch (dyn.d_tag)
  11446. {
  11447. default:
  11448. continue;
  11449. case DT_NULL:
  11450. if (relativecount != 0)
  11451. {
  11452. switch (elf_section_data (reldyn)->this_hdr.sh_type)
  11453. {
  11454. case SHT_REL: dyn.d_tag = DT_RELCOUNT; break;
  11455. case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break;
  11456. }
  11457. if (dyn.d_tag != DT_NULL
  11458. && dynconend - dyncon >= bed->s->sizeof_dyn)
  11459. {
  11460. dyn.d_un.d_val = relativecount;
  11461. relativecount = 0;
  11462. break;
  11463. }
  11464. relativecount = 0;
  11465. }
  11466. if (relr_entsize != 0)
  11467. {
  11468. if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn)
  11469. {
  11470. asection *s = htab->srelrdyn;
  11471. dyn.d_tag = DT_RELR;
  11472. dyn.d_un.d_ptr
  11473. = s->output_section->vma + s->output_offset;
  11474. bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
  11475. dyncon += bed->s->sizeof_dyn;
  11476. dyn.d_tag = DT_RELRSZ;
  11477. dyn.d_un.d_val = s->size;
  11478. bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
  11479. dyncon += bed->s->sizeof_dyn;
  11480. dyn.d_tag = DT_RELRENT;
  11481. dyn.d_un.d_val = relr_entsize;
  11482. relr_entsize = 0;
  11483. break;
  11484. }
  11485. relr_entsize = 0;
  11486. }
  11487. continue;
  11488. case DT_INIT:
  11489. name = info->init_function;
  11490. goto get_sym;
  11491. case DT_FINI:
  11492. name = info->fini_function;
  11493. get_sym:
  11494. {
  11495. struct elf_link_hash_entry *h;
  11496. h = elf_link_hash_lookup (htab, name, false, false, true);
  11497. if (h != NULL
  11498. && (h->root.type == bfd_link_hash_defined
  11499. || h->root.type == bfd_link_hash_defweak))
  11500. {
  11501. dyn.d_un.d_ptr = h->root.u.def.value;
  11502. o = h->root.u.def.section;
  11503. if (o->output_section != NULL)
  11504. dyn.d_un.d_ptr += (o->output_section->vma
  11505. + o->output_offset);
  11506. else
  11507. {
  11508. /* The symbol is imported from another shared
  11509. library and does not apply to this one. */
  11510. dyn.d_un.d_ptr = 0;
  11511. }
  11512. break;
  11513. }
  11514. }
  11515. continue;
  11516. case DT_PREINIT_ARRAYSZ:
  11517. name = ".preinit_array";
  11518. goto get_out_size;
  11519. case DT_INIT_ARRAYSZ:
  11520. name = ".init_array";
  11521. goto get_out_size;
  11522. case DT_FINI_ARRAYSZ:
  11523. name = ".fini_array";
  11524. get_out_size:
  11525. o = bfd_get_section_by_name (abfd, name);
  11526. if (o == NULL)
  11527. {
  11528. _bfd_error_handler
  11529. (_("could not find section %s"), name);
  11530. goto error_return;
  11531. }
  11532. if (o->size == 0)
  11533. _bfd_error_handler
  11534. (_("warning: %s section has zero size"), name);
  11535. dyn.d_un.d_val = o->size;
  11536. break;
  11537. case DT_PREINIT_ARRAY:
  11538. name = ".preinit_array";
  11539. goto get_out_vma;
  11540. case DT_INIT_ARRAY:
  11541. name = ".init_array";
  11542. goto get_out_vma;
  11543. case DT_FINI_ARRAY:
  11544. name = ".fini_array";
  11545. get_out_vma:
  11546. o = bfd_get_section_by_name (abfd, name);
  11547. goto do_vma;
  11548. case DT_HASH:
  11549. name = ".hash";
  11550. goto get_vma;
  11551. case DT_GNU_HASH:
  11552. name = ".gnu.hash";
  11553. goto get_vma;
  11554. case DT_STRTAB:
  11555. name = ".dynstr";
  11556. goto get_vma;
  11557. case DT_SYMTAB:
  11558. name = ".dynsym";
  11559. goto get_vma;
  11560. case DT_VERDEF:
  11561. name = ".gnu.version_d";
  11562. goto get_vma;
  11563. case DT_VERNEED:
  11564. name = ".gnu.version_r";
  11565. goto get_vma;
  11566. case DT_VERSYM:
  11567. name = ".gnu.version";
  11568. get_vma:
  11569. o = bfd_get_linker_section (dynobj, name);
  11570. do_vma:
  11571. if (o == NULL || bfd_is_abs_section (o->output_section))
  11572. {
  11573. _bfd_error_handler
  11574. (_("could not find section %s"), name);
  11575. goto error_return;
  11576. }
  11577. if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE)
  11578. {
  11579. _bfd_error_handler
  11580. (_("warning: section '%s' is being made into a note"), name);
  11581. bfd_set_error (bfd_error_nonrepresentable_section);
  11582. goto error_return;
  11583. }
  11584. dyn.d_un.d_ptr = o->output_section->vma + o->output_offset;
  11585. break;
  11586. case DT_REL:
  11587. case DT_RELA:
  11588. case DT_RELSZ:
  11589. case DT_RELASZ:
  11590. if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ)
  11591. type = SHT_REL;
  11592. else
  11593. type = SHT_RELA;
  11594. sh_size = 0;
  11595. sh_addr = 0;
  11596. for (i = 1; i < elf_numsections (abfd); i++)
  11597. {
  11598. Elf_Internal_Shdr *hdr;
  11599. hdr = elf_elfsections (abfd)[i];
  11600. if (hdr->sh_type == type
  11601. && (hdr->sh_flags & SHF_ALLOC) != 0)
  11602. {
  11603. sh_size += hdr->sh_size;
  11604. if (sh_addr == 0
  11605. || sh_addr > hdr->sh_addr)
  11606. sh_addr = hdr->sh_addr;
  11607. }
  11608. }
  11609. if (bed->dtrel_excludes_plt && htab->srelplt != NULL)
  11610. {
  11611. unsigned int opb = bfd_octets_per_byte (abfd, o);
  11612. /* Don't count procedure linkage table relocs in the
  11613. overall reloc count. */
  11614. sh_size -= htab->srelplt->size;
  11615. if (sh_size == 0)
  11616. /* If the size is zero, make the address zero too.
  11617. This is to avoid a glibc bug. If the backend
  11618. emits DT_RELA/DT_RELASZ even when DT_RELASZ is
  11619. zero, then we'll put DT_RELA at the end of
  11620. DT_JMPREL. glibc will interpret the end of
  11621. DT_RELA matching the end of DT_JMPREL as the
  11622. case where DT_RELA includes DT_JMPREL, and for
  11623. LD_BIND_NOW will decide that processing DT_RELA
  11624. will process the PLT relocs too. Net result:
  11625. No PLT relocs applied. */
  11626. sh_addr = 0;
  11627. /* If .rela.plt is the first .rela section, exclude
  11628. it from DT_RELA. */
  11629. else if (sh_addr == (htab->srelplt->output_section->vma
  11630. + htab->srelplt->output_offset) * opb)
  11631. sh_addr += htab->srelplt->size;
  11632. }
  11633. if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ)
  11634. dyn.d_un.d_val = sh_size;
  11635. else
  11636. dyn.d_un.d_ptr = sh_addr;
  11637. break;
  11638. }
  11639. bed->s->swap_dyn_out (dynobj, &dyn, dyncon);
  11640. }
  11641. }
  11642. /* If we have created any dynamic sections, then output them. */
  11643. if (dynobj != NULL)
  11644. {
  11645. if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info))
  11646. goto error_return;
  11647. /* Check for DT_TEXTREL (late, in case the backend removes it). */
  11648. if (bfd_link_textrel_check (info)
  11649. && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL
  11650. && o->size != 0)
  11651. {
  11652. bfd_byte *dyncon, *dynconend;
  11653. dyncon = o->contents;
  11654. dynconend = o->contents + o->size;
  11655. for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn)
  11656. {
  11657. Elf_Internal_Dyn dyn;
  11658. bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
  11659. if (dyn.d_tag == DT_TEXTREL)
  11660. {
  11661. if (info->textrel_check == textrel_check_error)
  11662. info->callbacks->einfo
  11663. (_("%P%X: read-only segment has dynamic relocations\n"));
  11664. else if (bfd_link_dll (info))
  11665. info->callbacks->einfo
  11666. (_("%P: warning: creating DT_TEXTREL in a shared object\n"));
  11667. else if (bfd_link_pde (info))
  11668. info->callbacks->einfo
  11669. (_("%P: warning: creating DT_TEXTREL in a PDE\n"));
  11670. else
  11671. info->callbacks->einfo
  11672. (_("%P: warning: creating DT_TEXTREL in a PIE\n"));
  11673. break;
  11674. }
  11675. }
  11676. }
  11677. for (o = dynobj->sections; o != NULL; o = o->next)
  11678. {
  11679. if ((o->flags & SEC_HAS_CONTENTS) == 0
  11680. || o->size == 0
  11681. || o->output_section == bfd_abs_section_ptr)
  11682. continue;
  11683. if ((o->flags & SEC_LINKER_CREATED) == 0)
  11684. {
  11685. /* At this point, we are only interested in sections
  11686. created by _bfd_elf_link_create_dynamic_sections. */
  11687. continue;
  11688. }
  11689. if (htab->stab_info.stabstr == o)
  11690. continue;
  11691. if (htab->eh_info.hdr_sec == o)
  11692. continue;
  11693. if (strcmp (o->name, ".dynstr") != 0)
  11694. {
  11695. bfd_size_type octets = ((file_ptr) o->output_offset
  11696. * bfd_octets_per_byte (abfd, o));
  11697. if (!bfd_set_section_contents (abfd, o->output_section,
  11698. o->contents, octets, o->size))
  11699. goto error_return;
  11700. }
  11701. else
  11702. {
  11703. /* The contents of the .dynstr section are actually in a
  11704. stringtab. */
  11705. file_ptr off;
  11706. off = elf_section_data (o->output_section)->this_hdr.sh_offset;
  11707. if (bfd_seek (abfd, off, SEEK_SET) != 0
  11708. || !_bfd_elf_strtab_emit (abfd, htab->dynstr))
  11709. goto error_return;
  11710. }
  11711. }
  11712. }
  11713. if (!info->resolve_section_groups)
  11714. {
  11715. bool failed = false;
  11716. BFD_ASSERT (bfd_link_relocatable (info));
  11717. bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
  11718. if (failed)
  11719. goto error_return;
  11720. }
  11721. /* If we have optimized stabs strings, output them. */
  11722. if (htab->stab_info.stabstr != NULL)
  11723. {
  11724. if (!_bfd_write_stab_strings (abfd, &htab->stab_info))
  11725. goto error_return;
  11726. }
  11727. if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info))
  11728. goto error_return;
  11729. if (info->callbacks->emit_ctf)
  11730. info->callbacks->emit_ctf ();
  11731. elf_final_link_free (abfd, &flinfo);
  11732. if (attr_section)
  11733. {
  11734. bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
  11735. if (contents == NULL)
  11736. {
  11737. /* Bail out and fail. */
  11738. ret = false;
  11739. goto return_local_hash_table;
  11740. }
  11741. bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
  11742. bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
  11743. free (contents);
  11744. }
  11745. return_local_hash_table:
  11746. if (info->unique_symbol)
  11747. bfd_hash_table_free (&flinfo.local_hash_table);
  11748. return ret;
  11749. error_return:
  11750. elf_final_link_free (abfd, &flinfo);
  11751. ret = false;
  11752. goto return_local_hash_table;
  11753. }
  11754. /* Initialize COOKIE for input bfd ABFD. */
  11755. static bool
  11756. init_reloc_cookie (struct elf_reloc_cookie *cookie,
  11757. struct bfd_link_info *info, bfd *abfd)
  11758. {
  11759. Elf_Internal_Shdr *symtab_hdr;
  11760. const struct elf_backend_data *bed;
  11761. bed = get_elf_backend_data (abfd);
  11762. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  11763. cookie->abfd = abfd;
  11764. cookie->sym_hashes = elf_sym_hashes (abfd);
  11765. cookie->bad_symtab = elf_bad_symtab (abfd);
  11766. if (cookie->bad_symtab)
  11767. {
  11768. cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
  11769. cookie->extsymoff = 0;
  11770. }
  11771. else
  11772. {
  11773. cookie->locsymcount = symtab_hdr->sh_info;
  11774. cookie->extsymoff = symtab_hdr->sh_info;
  11775. }
  11776. if (bed->s->arch_size == 32)
  11777. cookie->r_sym_shift = 8;
  11778. else
  11779. cookie->r_sym_shift = 32;
  11780. cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
  11781. if (cookie->locsyms == NULL && cookie->locsymcount != 0)
  11782. {
  11783. cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  11784. cookie->locsymcount, 0,
  11785. NULL, NULL, NULL);
  11786. if (cookie->locsyms == NULL)
  11787. {
  11788. info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
  11789. return false;
  11790. }
  11791. if (_bfd_link_keep_memory (info) )
  11792. {
  11793. symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
  11794. info->cache_size += (cookie->locsymcount
  11795. * sizeof (Elf_External_Sym_Shndx));
  11796. }
  11797. }
  11798. return true;
  11799. }
  11800. /* Free the memory allocated by init_reloc_cookie, if appropriate. */
  11801. static void
  11802. fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd)
  11803. {
  11804. Elf_Internal_Shdr *symtab_hdr;
  11805. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  11806. if (symtab_hdr->contents != (unsigned char *) cookie->locsyms)
  11807. free (cookie->locsyms);
  11808. }
  11809. /* Initialize the relocation information in COOKIE for input section SEC
  11810. of input bfd ABFD. */
  11811. static bool
  11812. init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
  11813. struct bfd_link_info *info, bfd *abfd,
  11814. asection *sec)
  11815. {
  11816. if (sec->reloc_count == 0)
  11817. {
  11818. cookie->rels = NULL;
  11819. cookie->relend = NULL;
  11820. }
  11821. else
  11822. {
  11823. cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
  11824. NULL, NULL,
  11825. _bfd_link_keep_memory (info));
  11826. if (cookie->rels == NULL)
  11827. return false;
  11828. cookie->rel = cookie->rels;
  11829. cookie->relend = cookie->rels + sec->reloc_count;
  11830. }
  11831. cookie->rel = cookie->rels;
  11832. return true;
  11833. }
  11834. /* Free the memory allocated by init_reloc_cookie_rels,
  11835. if appropriate. */
  11836. static void
  11837. fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
  11838. asection *sec)
  11839. {
  11840. if (elf_section_data (sec)->relocs != cookie->rels)
  11841. free (cookie->rels);
  11842. }
  11843. /* Initialize the whole of COOKIE for input section SEC. */
  11844. static bool
  11845. init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
  11846. struct bfd_link_info *info,
  11847. asection *sec)
  11848. {
  11849. if (!init_reloc_cookie (cookie, info, sec->owner))
  11850. goto error1;
  11851. if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
  11852. goto error2;
  11853. return true;
  11854. error2:
  11855. fini_reloc_cookie (cookie, sec->owner);
  11856. error1:
  11857. return false;
  11858. }
  11859. /* Free the memory allocated by init_reloc_cookie_for_section,
  11860. if appropriate. */
  11861. static void
  11862. fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie,
  11863. asection *sec)
  11864. {
  11865. fini_reloc_cookie_rels (cookie, sec);
  11866. fini_reloc_cookie (cookie, sec->owner);
  11867. }
  11868. /* Garbage collect unused sections. */
  11869. /* Default gc_mark_hook. */
  11870. asection *
  11871. _bfd_elf_gc_mark_hook (asection *sec,
  11872. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  11873. Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
  11874. struct elf_link_hash_entry *h,
  11875. Elf_Internal_Sym *sym)
  11876. {
  11877. if (h != NULL)
  11878. {
  11879. switch (h->root.type)
  11880. {
  11881. case bfd_link_hash_defined:
  11882. case bfd_link_hash_defweak:
  11883. return h->root.u.def.section;
  11884. case bfd_link_hash_common:
  11885. return h->root.u.c.p->section;
  11886. default:
  11887. break;
  11888. }
  11889. }
  11890. else
  11891. return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
  11892. return NULL;
  11893. }
  11894. /* Return the debug definition section. */
  11895. static asection *
  11896. elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED,
  11897. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  11898. Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
  11899. struct elf_link_hash_entry *h,
  11900. Elf_Internal_Sym *sym)
  11901. {
  11902. if (h != NULL)
  11903. {
  11904. /* Return the global debug definition section. */
  11905. if ((h->root.type == bfd_link_hash_defined
  11906. || h->root.type == bfd_link_hash_defweak)
  11907. && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0)
  11908. return h->root.u.def.section;
  11909. }
  11910. else
  11911. {
  11912. /* Return the local debug definition section. */
  11913. asection *isec = bfd_section_from_elf_index (sec->owner,
  11914. sym->st_shndx);
  11915. if ((isec->flags & SEC_DEBUGGING) != 0)
  11916. return isec;
  11917. }
  11918. return NULL;
  11919. }
  11920. /* COOKIE->rel describes a relocation against section SEC, which is
  11921. a section we've decided to keep. Return the section that contains
  11922. the relocation symbol, or NULL if no section contains it. */
  11923. asection *
  11924. _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
  11925. elf_gc_mark_hook_fn gc_mark_hook,
  11926. struct elf_reloc_cookie *cookie,
  11927. bool *start_stop)
  11928. {
  11929. unsigned long r_symndx;
  11930. struct elf_link_hash_entry *h, *hw;
  11931. r_symndx = cookie->rel->r_info >> cookie->r_sym_shift;
  11932. if (r_symndx == STN_UNDEF)
  11933. return NULL;
  11934. if (r_symndx >= cookie->locsymcount
  11935. || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
  11936. {
  11937. bool was_marked;
  11938. h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
  11939. if (h == NULL)
  11940. {
  11941. info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"),
  11942. sec->owner);
  11943. return NULL;
  11944. }
  11945. while (h->root.type == bfd_link_hash_indirect
  11946. || h->root.type == bfd_link_hash_warning)
  11947. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  11948. was_marked = h->mark;
  11949. h->mark = 1;
  11950. /* Keep all aliases of the symbol too. If an object symbol
  11951. needs to be copied into .dynbss then all of its aliases
  11952. should be present as dynamic symbols, not just the one used
  11953. on the copy relocation. */
  11954. hw = h;
  11955. while (hw->is_weakalias)
  11956. {
  11957. hw = hw->u.alias;
  11958. hw->mark = 1;
  11959. }
  11960. if (!was_marked && h->start_stop && !h->root.ldscript_def)
  11961. {
  11962. if (info->start_stop_gc)
  11963. return NULL;
  11964. /* To work around a glibc bug, mark XXX input sections
  11965. when there is a reference to __start_XXX or __stop_XXX
  11966. symbols. */
  11967. else if (start_stop != NULL)
  11968. {
  11969. asection *s = h->u2.start_stop_section;
  11970. *start_stop = true;
  11971. return s;
  11972. }
  11973. }
  11974. return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
  11975. }
  11976. return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
  11977. &cookie->locsyms[r_symndx]);
  11978. }
  11979. /* COOKIE->rel describes a relocation against section SEC, which is
  11980. a section we've decided to keep. Mark the section that contains
  11981. the relocation symbol. */
  11982. bool
  11983. _bfd_elf_gc_mark_reloc (struct bfd_link_info *info,
  11984. asection *sec,
  11985. elf_gc_mark_hook_fn gc_mark_hook,
  11986. struct elf_reloc_cookie *cookie)
  11987. {
  11988. asection *rsec;
  11989. bool start_stop = false;
  11990. rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop);
  11991. while (rsec != NULL)
  11992. {
  11993. if (!rsec->gc_mark)
  11994. {
  11995. if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour
  11996. || (rsec->owner->flags & DYNAMIC) != 0)
  11997. rsec->gc_mark = 1;
  11998. else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook))
  11999. return false;
  12000. }
  12001. if (!start_stop)
  12002. break;
  12003. rsec = bfd_get_next_section_by_name (rsec->owner, rsec);
  12004. }
  12005. return true;
  12006. }
  12007. /* The mark phase of garbage collection. For a given section, mark
  12008. it and any sections in this section's group, and all the sections
  12009. which define symbols to which it refers. */
  12010. bool
  12011. _bfd_elf_gc_mark (struct bfd_link_info *info,
  12012. asection *sec,
  12013. elf_gc_mark_hook_fn gc_mark_hook)
  12014. {
  12015. bool ret;
  12016. asection *group_sec, *eh_frame;
  12017. sec->gc_mark = 1;
  12018. /* Mark all the sections in the group. */
  12019. group_sec = elf_section_data (sec)->next_in_group;
  12020. if (group_sec && !group_sec->gc_mark)
  12021. if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook))
  12022. return false;
  12023. /* Look through the section relocs. */
  12024. ret = true;
  12025. eh_frame = elf_eh_frame_section (sec->owner);
  12026. if ((sec->flags & SEC_RELOC) != 0
  12027. && sec->reloc_count > 0
  12028. && sec != eh_frame)
  12029. {
  12030. struct elf_reloc_cookie cookie;
  12031. if (!init_reloc_cookie_for_section (&cookie, info, sec))
  12032. ret = false;
  12033. else
  12034. {
  12035. for (; cookie.rel < cookie.relend; cookie.rel++)
  12036. if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
  12037. {
  12038. ret = false;
  12039. break;
  12040. }
  12041. fini_reloc_cookie_for_section (&cookie, sec);
  12042. }
  12043. }
  12044. if (ret && eh_frame && elf_fde_list (sec))
  12045. {
  12046. struct elf_reloc_cookie cookie;
  12047. if (!init_reloc_cookie_for_section (&cookie, info, eh_frame))
  12048. ret = false;
  12049. else
  12050. {
  12051. if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame,
  12052. gc_mark_hook, &cookie))
  12053. ret = false;
  12054. fini_reloc_cookie_for_section (&cookie, eh_frame);
  12055. }
  12056. }
  12057. eh_frame = elf_section_eh_frame_entry (sec);
  12058. if (ret && eh_frame && !eh_frame->gc_mark)
  12059. if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
  12060. ret = false;
  12061. return ret;
  12062. }
  12063. /* Scan and mark sections in a special or debug section group. */
  12064. static void
  12065. _bfd_elf_gc_mark_debug_special_section_group (asection *grp)
  12066. {
  12067. /* Point to first section of section group. */
  12068. asection *ssec;
  12069. /* Used to iterate the section group. */
  12070. asection *msec;
  12071. bool is_special_grp = true;
  12072. bool is_debug_grp = true;
  12073. /* First scan to see if group contains any section other than debug
  12074. and special section. */
  12075. ssec = msec = elf_next_in_group (grp);
  12076. do
  12077. {
  12078. if ((msec->flags & SEC_DEBUGGING) == 0)
  12079. is_debug_grp = false;
  12080. if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0)
  12081. is_special_grp = false;
  12082. msec = elf_next_in_group (msec);
  12083. }
  12084. while (msec != ssec);
  12085. /* If this is a pure debug section group or pure special section group,
  12086. keep all sections in this group. */
  12087. if (is_debug_grp || is_special_grp)
  12088. {
  12089. do
  12090. {
  12091. msec->gc_mark = 1;
  12092. msec = elf_next_in_group (msec);
  12093. }
  12094. while (msec != ssec);
  12095. }
  12096. }
  12097. /* Keep debug and special sections. */
  12098. bool
  12099. _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
  12100. elf_gc_mark_hook_fn mark_hook)
  12101. {
  12102. bfd *ibfd;
  12103. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  12104. {
  12105. asection *isec;
  12106. bool some_kept;
  12107. bool debug_frag_seen;
  12108. bool has_kept_debug_info;
  12109. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
  12110. continue;
  12111. isec = ibfd->sections;
  12112. if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
  12113. continue;
  12114. /* Ensure all linker created sections are kept,
  12115. see if any other section is already marked,
  12116. and note if we have any fragmented debug sections. */
  12117. debug_frag_seen = some_kept = has_kept_debug_info = false;
  12118. for (isec = ibfd->sections; isec != NULL; isec = isec->next)
  12119. {
  12120. if ((isec->flags & SEC_LINKER_CREATED) != 0)
  12121. isec->gc_mark = 1;
  12122. else if (isec->gc_mark
  12123. && (isec->flags & SEC_ALLOC) != 0
  12124. && elf_section_type (isec) != SHT_NOTE)
  12125. some_kept = true;
  12126. else
  12127. {
  12128. /* Since all sections, except for backend specific ones,
  12129. have been garbage collected, call mark_hook on this
  12130. section if any of its linked-to sections is marked. */
  12131. asection *linked_to_sec;
  12132. for (linked_to_sec = elf_linked_to_section (isec);
  12133. linked_to_sec != NULL && !linked_to_sec->linker_mark;
  12134. linked_to_sec = elf_linked_to_section (linked_to_sec))
  12135. {
  12136. if (linked_to_sec->gc_mark)
  12137. {
  12138. if (!_bfd_elf_gc_mark (info, isec, mark_hook))
  12139. return false;
  12140. break;
  12141. }
  12142. linked_to_sec->linker_mark = 1;
  12143. }
  12144. for (linked_to_sec = elf_linked_to_section (isec);
  12145. linked_to_sec != NULL && linked_to_sec->linker_mark;
  12146. linked_to_sec = elf_linked_to_section (linked_to_sec))
  12147. linked_to_sec->linker_mark = 0;
  12148. }
  12149. if (!debug_frag_seen
  12150. && (isec->flags & SEC_DEBUGGING)
  12151. && startswith (isec->name, ".debug_line."))
  12152. debug_frag_seen = true;
  12153. else if (strcmp (bfd_section_name (isec),
  12154. "__patchable_function_entries") == 0
  12155. && elf_linked_to_section (isec) == NULL)
  12156. info->callbacks->einfo (_("%F%P: %pB(%pA): error: "
  12157. "need linked-to section "
  12158. "for --gc-sections\n"),
  12159. isec->owner, isec);
  12160. }
  12161. /* If no non-note alloc section in this file will be kept, then
  12162. we can toss out the debug and special sections. */
  12163. if (!some_kept)
  12164. continue;
  12165. /* Keep debug and special sections like .comment when they are
  12166. not part of a group. Also keep section groups that contain
  12167. just debug sections or special sections. NB: Sections with
  12168. linked-to section has been handled above. */
  12169. for (isec = ibfd->sections; isec != NULL; isec = isec->next)
  12170. {
  12171. if ((isec->flags & SEC_GROUP) != 0)
  12172. _bfd_elf_gc_mark_debug_special_section_group (isec);
  12173. else if (((isec->flags & SEC_DEBUGGING) != 0
  12174. || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
  12175. && elf_next_in_group (isec) == NULL
  12176. && elf_linked_to_section (isec) == NULL)
  12177. isec->gc_mark = 1;
  12178. if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0)
  12179. has_kept_debug_info = true;
  12180. }
  12181. /* Look for CODE sections which are going to be discarded,
  12182. and find and discard any fragmented debug sections which
  12183. are associated with that code section. */
  12184. if (debug_frag_seen)
  12185. for (isec = ibfd->sections; isec != NULL; isec = isec->next)
  12186. if ((isec->flags & SEC_CODE) != 0
  12187. && isec->gc_mark == 0)
  12188. {
  12189. unsigned int ilen;
  12190. asection *dsec;
  12191. ilen = strlen (isec->name);
  12192. /* Association is determined by the name of the debug
  12193. section containing the name of the code section as
  12194. a suffix. For example .debug_line.text.foo is a
  12195. debug section associated with .text.foo. */
  12196. for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next)
  12197. {
  12198. unsigned int dlen;
  12199. if (dsec->gc_mark == 0
  12200. || (dsec->flags & SEC_DEBUGGING) == 0)
  12201. continue;
  12202. dlen = strlen (dsec->name);
  12203. if (dlen > ilen
  12204. && strncmp (dsec->name + (dlen - ilen),
  12205. isec->name, ilen) == 0)
  12206. dsec->gc_mark = 0;
  12207. }
  12208. }
  12209. /* Mark debug sections referenced by kept debug sections. */
  12210. if (has_kept_debug_info)
  12211. for (isec = ibfd->sections; isec != NULL; isec = isec->next)
  12212. if (isec->gc_mark
  12213. && (isec->flags & SEC_DEBUGGING) != 0)
  12214. if (!_bfd_elf_gc_mark (info, isec,
  12215. elf_gc_mark_debug_section))
  12216. return false;
  12217. }
  12218. return true;
  12219. }
  12220. static bool
  12221. elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
  12222. {
  12223. bfd *sub;
  12224. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  12225. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  12226. {
  12227. asection *o;
  12228. if (bfd_get_flavour (sub) != bfd_target_elf_flavour
  12229. || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info))
  12230. || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
  12231. continue;
  12232. o = sub->sections;
  12233. if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
  12234. continue;
  12235. for (o = sub->sections; o != NULL; o = o->next)
  12236. {
  12237. /* When any section in a section group is kept, we keep all
  12238. sections in the section group. If the first member of
  12239. the section group is excluded, we will also exclude the
  12240. group section. */
  12241. if (o->flags & SEC_GROUP)
  12242. {
  12243. asection *first = elf_next_in_group (o);
  12244. o->gc_mark = first->gc_mark;
  12245. }
  12246. if (o->gc_mark)
  12247. continue;
  12248. /* Skip sweeping sections already excluded. */
  12249. if (o->flags & SEC_EXCLUDE)
  12250. continue;
  12251. /* Since this is early in the link process, it is simple
  12252. to remove a section from the output. */
  12253. o->flags |= SEC_EXCLUDE;
  12254. if (info->print_gc_sections && o->size != 0)
  12255. /* xgettext:c-format */
  12256. _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"),
  12257. o, sub);
  12258. }
  12259. }
  12260. return true;
  12261. }
  12262. /* Propagate collected vtable information. This is called through
  12263. elf_link_hash_traverse. */
  12264. static bool
  12265. elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
  12266. {
  12267. /* Those that are not vtables. */
  12268. if (h->start_stop
  12269. || h->u2.vtable == NULL
  12270. || h->u2.vtable->parent == NULL)
  12271. return true;
  12272. /* Those vtables that do not have parents, we cannot merge. */
  12273. if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1)
  12274. return true;
  12275. /* If we've already been done, exit. */
  12276. if (h->u2.vtable->used && h->u2.vtable->used[-1])
  12277. return true;
  12278. /* Make sure the parent's table is up to date. */
  12279. elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp);
  12280. if (h->u2.vtable->used == NULL)
  12281. {
  12282. /* None of this table's entries were referenced. Re-use the
  12283. parent's table. */
  12284. h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used;
  12285. h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size;
  12286. }
  12287. else
  12288. {
  12289. size_t n;
  12290. bool *cu, *pu;
  12291. /* Or the parent's entries into ours. */
  12292. cu = h->u2.vtable->used;
  12293. cu[-1] = true;
  12294. pu = h->u2.vtable->parent->u2.vtable->used;
  12295. if (pu != NULL)
  12296. {
  12297. const struct elf_backend_data *bed;
  12298. unsigned int log_file_align;
  12299. bed = get_elf_backend_data (h->root.u.def.section->owner);
  12300. log_file_align = bed->s->log_file_align;
  12301. n = h->u2.vtable->parent->u2.vtable->size >> log_file_align;
  12302. while (n--)
  12303. {
  12304. if (*pu)
  12305. *cu = true;
  12306. pu++;
  12307. cu++;
  12308. }
  12309. }
  12310. }
  12311. return true;
  12312. }
  12313. struct link_info_ok
  12314. {
  12315. struct bfd_link_info *info;
  12316. bool ok;
  12317. };
  12318. static bool
  12319. elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
  12320. void *ptr)
  12321. {
  12322. asection *sec;
  12323. bfd_vma hstart, hend;
  12324. Elf_Internal_Rela *relstart, *relend, *rel;
  12325. const struct elf_backend_data *bed;
  12326. unsigned int log_file_align;
  12327. struct link_info_ok *info = (struct link_info_ok *) ptr;
  12328. /* Take care of both those symbols that do not describe vtables as
  12329. well as those that are not loaded. */
  12330. if (h->start_stop
  12331. || h->u2.vtable == NULL
  12332. || h->u2.vtable->parent == NULL)
  12333. return true;
  12334. BFD_ASSERT (h->root.type == bfd_link_hash_defined
  12335. || h->root.type == bfd_link_hash_defweak);
  12336. sec = h->root.u.def.section;
  12337. hstart = h->root.u.def.value;
  12338. hend = hstart + h->size;
  12339. relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
  12340. sec, NULL, NULL, true);
  12341. if (!relstart)
  12342. return info->ok = false;
  12343. bed = get_elf_backend_data (sec->owner);
  12344. log_file_align = bed->s->log_file_align;
  12345. relend = relstart + sec->reloc_count;
  12346. for (rel = relstart; rel < relend; ++rel)
  12347. if (rel->r_offset >= hstart && rel->r_offset < hend)
  12348. {
  12349. /* If the entry is in use, do nothing. */
  12350. if (h->u2.vtable->used
  12351. && (rel->r_offset - hstart) < h->u2.vtable->size)
  12352. {
  12353. bfd_vma entry = (rel->r_offset - hstart) >> log_file_align;
  12354. if (h->u2.vtable->used[entry])
  12355. continue;
  12356. }
  12357. /* Otherwise, kill it. */
  12358. rel->r_offset = rel->r_info = rel->r_addend = 0;
  12359. }
  12360. return true;
  12361. }
  12362. /* Mark sections containing dynamically referenced symbols. When
  12363. building shared libraries, we must assume that any visible symbol is
  12364. referenced. */
  12365. bool
  12366. bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf)
  12367. {
  12368. struct bfd_link_info *info = (struct bfd_link_info *) inf;
  12369. struct bfd_elf_dynamic_list *d = info->dynamic_list;
  12370. if ((h->root.type == bfd_link_hash_defined
  12371. || h->root.type == bfd_link_hash_defweak)
  12372. && (!h->start_stop
  12373. || h->root.ldscript_def
  12374. || !info->start_stop_gc)
  12375. && ((h->ref_dynamic && !h->forced_local)
  12376. || ((h->def_regular || ELF_COMMON_DEF_P (h))
  12377. && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
  12378. && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
  12379. && (!bfd_link_executable (info)
  12380. || info->gc_keep_exported
  12381. || info->export_dynamic
  12382. || (h->dynamic
  12383. && d != NULL
  12384. && (*d->match) (&d->head, NULL, h->root.root.string)))
  12385. && (h->versioned >= versioned
  12386. || !bfd_hide_sym_by_version (info->version_info,
  12387. h->root.root.string)))))
  12388. h->root.u.def.section->flags |= SEC_KEEP;
  12389. return true;
  12390. }
  12391. /* Keep all sections containing symbols undefined on the command-line,
  12392. and the section containing the entry symbol. */
  12393. void
  12394. _bfd_elf_gc_keep (struct bfd_link_info *info)
  12395. {
  12396. struct bfd_sym_chain *sym;
  12397. for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
  12398. {
  12399. struct elf_link_hash_entry *h;
  12400. h = elf_link_hash_lookup (elf_hash_table (info), sym->name,
  12401. false, false, false);
  12402. if (h != NULL
  12403. && (h->root.type == bfd_link_hash_defined
  12404. || h->root.type == bfd_link_hash_defweak)
  12405. && !bfd_is_const_section (h->root.u.def.section))
  12406. h->root.u.def.section->flags |= SEC_KEEP;
  12407. }
  12408. }
  12409. bool
  12410. bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
  12411. struct bfd_link_info *info)
  12412. {
  12413. bfd *ibfd = info->input_bfds;
  12414. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  12415. {
  12416. asection *sec;
  12417. struct elf_reloc_cookie cookie;
  12418. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
  12419. continue;
  12420. sec = ibfd->sections;
  12421. if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
  12422. continue;
  12423. if (!init_reloc_cookie (&cookie, info, ibfd))
  12424. return false;
  12425. for (sec = ibfd->sections; sec; sec = sec->next)
  12426. {
  12427. if (startswith (bfd_section_name (sec), ".eh_frame_entry")
  12428. && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
  12429. {
  12430. _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
  12431. fini_reloc_cookie_rels (&cookie, sec);
  12432. }
  12433. }
  12434. }
  12435. return true;
  12436. }
  12437. /* Do mark and sweep of unused sections. */
  12438. bool
  12439. bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
  12440. {
  12441. bool ok = true;
  12442. bfd *sub;
  12443. elf_gc_mark_hook_fn gc_mark_hook;
  12444. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  12445. struct elf_link_hash_table *htab;
  12446. struct link_info_ok info_ok;
  12447. if (!bed->can_gc_sections
  12448. || !is_elf_hash_table (info->hash))
  12449. {
  12450. _bfd_error_handler(_("warning: gc-sections option ignored"));
  12451. return true;
  12452. }
  12453. bed->gc_keep (info);
  12454. htab = elf_hash_table (info);
  12455. /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section
  12456. at the .eh_frame section if we can mark the FDEs individually. */
  12457. for (sub = info->input_bfds;
  12458. info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
  12459. sub = sub->link.next)
  12460. {
  12461. asection *sec;
  12462. struct elf_reloc_cookie cookie;
  12463. sec = sub->sections;
  12464. if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
  12465. continue;
  12466. sec = bfd_get_section_by_name (sub, ".eh_frame");
  12467. while (sec && init_reloc_cookie_for_section (&cookie, info, sec))
  12468. {
  12469. _bfd_elf_parse_eh_frame (sub, info, sec, &cookie);
  12470. if (elf_section_data (sec)->sec_info
  12471. && (sec->flags & SEC_LINKER_CREATED) == 0)
  12472. elf_eh_frame_section (sub) = sec;
  12473. fini_reloc_cookie_for_section (&cookie, sec);
  12474. sec = bfd_get_next_section_by_name (NULL, sec);
  12475. }
  12476. }
  12477. /* Apply transitive closure to the vtable entry usage info. */
  12478. elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok);
  12479. if (!ok)
  12480. return false;
  12481. /* Kill the vtable relocations that were not used. */
  12482. info_ok.info = info;
  12483. info_ok.ok = true;
  12484. elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
  12485. if (!info_ok.ok)
  12486. return false;
  12487. /* Mark dynamically referenced symbols. */
  12488. if (htab->dynamic_sections_created || info->gc_keep_exported)
  12489. elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info);
  12490. /* Grovel through relocs to find out who stays ... */
  12491. gc_mark_hook = bed->gc_mark_hook;
  12492. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  12493. {
  12494. asection *o;
  12495. if (bfd_get_flavour (sub) != bfd_target_elf_flavour
  12496. || elf_object_id (sub) != elf_hash_table_id (htab)
  12497. || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
  12498. continue;
  12499. o = sub->sections;
  12500. if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
  12501. continue;
  12502. /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
  12503. Also treat note sections as a root, if the section is not part
  12504. of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as
  12505. well as FINI_ARRAY sections for ld -r. */
  12506. for (o = sub->sections; o != NULL; o = o->next)
  12507. if (!o->gc_mark
  12508. && (o->flags & SEC_EXCLUDE) == 0
  12509. && ((o->flags & SEC_KEEP) != 0
  12510. || (bfd_link_relocatable (info)
  12511. && ((elf_section_data (o)->this_hdr.sh_type
  12512. == SHT_PREINIT_ARRAY)
  12513. || (elf_section_data (o)->this_hdr.sh_type
  12514. == SHT_INIT_ARRAY)
  12515. || (elf_section_data (o)->this_hdr.sh_type
  12516. == SHT_FINI_ARRAY)))
  12517. || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE
  12518. && elf_next_in_group (o) == NULL
  12519. && elf_linked_to_section (o) == NULL)
  12520. || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain)
  12521. && (elf_section_flags (o) & SHF_GNU_RETAIN))))
  12522. {
  12523. if (!_bfd_elf_gc_mark (info, o, gc_mark_hook))
  12524. return false;
  12525. }
  12526. }
  12527. /* Allow the backend to mark additional target specific sections. */
  12528. bed->gc_mark_extra_sections (info, gc_mark_hook);
  12529. /* ... and mark SEC_EXCLUDE for those that go. */
  12530. return elf_gc_sweep (abfd, info);
  12531. }
  12532. /* Called from check_relocs to record the existence of a VTINHERIT reloc. */
  12533. bool
  12534. bfd_elf_gc_record_vtinherit (bfd *abfd,
  12535. asection *sec,
  12536. struct elf_link_hash_entry *h,
  12537. bfd_vma offset)
  12538. {
  12539. struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
  12540. struct elf_link_hash_entry **search, *child;
  12541. size_t extsymcount;
  12542. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  12543. /* The sh_info field of the symtab header tells us where the
  12544. external symbols start. We don't care about the local symbols at
  12545. this point. */
  12546. extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym;
  12547. if (!elf_bad_symtab (abfd))
  12548. extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info;
  12549. sym_hashes = elf_sym_hashes (abfd);
  12550. sym_hashes_end = PTR_ADD (sym_hashes, extsymcount);
  12551. /* Hunt down the child symbol, which is in this section at the same
  12552. offset as the relocation. */
  12553. for (search = sym_hashes; search != sym_hashes_end; ++search)
  12554. {
  12555. if ((child = *search) != NULL
  12556. && (child->root.type == bfd_link_hash_defined
  12557. || child->root.type == bfd_link_hash_defweak)
  12558. && child->root.u.def.section == sec
  12559. && child->root.u.def.value == offset)
  12560. goto win;
  12561. }
  12562. /* xgettext:c-format */
  12563. _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"),
  12564. abfd, sec, (uint64_t) offset);
  12565. bfd_set_error (bfd_error_invalid_operation);
  12566. return false;
  12567. win:
  12568. if (!child->u2.vtable)
  12569. {
  12570. child->u2.vtable = ((struct elf_link_virtual_table_entry *)
  12571. bfd_zalloc (abfd, sizeof (*child->u2.vtable)));
  12572. if (!child->u2.vtable)
  12573. return false;
  12574. }
  12575. if (!h)
  12576. {
  12577. /* This *should* only be the absolute section. It could potentially
  12578. be that someone has defined a non-global vtable though, which
  12579. would be bad. It isn't worth paging in the local symbols to be
  12580. sure though; that case should simply be handled by the assembler. */
  12581. child->u2.vtable->parent = (struct elf_link_hash_entry *) -1;
  12582. }
  12583. else
  12584. child->u2.vtable->parent = h;
  12585. return true;
  12586. }
  12587. /* Called from check_relocs to record the existence of a VTENTRY reloc. */
  12588. bool
  12589. bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec,
  12590. struct elf_link_hash_entry *h,
  12591. bfd_vma addend)
  12592. {
  12593. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  12594. unsigned int log_file_align = bed->s->log_file_align;
  12595. if (!h)
  12596. {
  12597. /* xgettext:c-format */
  12598. _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"),
  12599. abfd, sec);
  12600. bfd_set_error (bfd_error_bad_value);
  12601. return false;
  12602. }
  12603. if (!h->u2.vtable)
  12604. {
  12605. h->u2.vtable = ((struct elf_link_virtual_table_entry *)
  12606. bfd_zalloc (abfd, sizeof (*h->u2.vtable)));
  12607. if (!h->u2.vtable)
  12608. return false;
  12609. }
  12610. if (addend >= h->u2.vtable->size)
  12611. {
  12612. size_t size, bytes, file_align;
  12613. bool *ptr = h->u2.vtable->used;
  12614. /* While the symbol is undefined, we have to be prepared to handle
  12615. a zero size. */
  12616. file_align = 1 << log_file_align;
  12617. if (h->root.type == bfd_link_hash_undefined)
  12618. size = addend + file_align;
  12619. else
  12620. {
  12621. size = h->size;
  12622. if (addend >= size)
  12623. {
  12624. /* Oops! We've got a reference past the defined end of
  12625. the table. This is probably a bug -- shall we warn? */
  12626. size = addend + file_align;
  12627. }
  12628. }
  12629. size = (size + file_align - 1) & -file_align;
  12630. /* Allocate one extra entry for use as a "done" flag for the
  12631. consolidation pass. */
  12632. bytes = ((size >> log_file_align) + 1) * sizeof (bool);
  12633. if (ptr)
  12634. {
  12635. ptr = (bool *) bfd_realloc (ptr - 1, bytes);
  12636. if (ptr != NULL)
  12637. {
  12638. size_t oldbytes;
  12639. oldbytes = (((h->u2.vtable->size >> log_file_align) + 1)
  12640. * sizeof (bool));
  12641. memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes);
  12642. }
  12643. }
  12644. else
  12645. ptr = (bool *) bfd_zmalloc (bytes);
  12646. if (ptr == NULL)
  12647. return false;
  12648. /* And arrange for that done flag to be at index -1. */
  12649. h->u2.vtable->used = ptr + 1;
  12650. h->u2.vtable->size = size;
  12651. }
  12652. h->u2.vtable->used[addend >> log_file_align] = true;
  12653. return true;
  12654. }
  12655. /* Map an ELF section header flag to its corresponding string. */
  12656. typedef struct
  12657. {
  12658. char *flag_name;
  12659. flagword flag_value;
  12660. } elf_flags_to_name_table;
  12661. static const elf_flags_to_name_table elf_flags_to_names [] =
  12662. {
  12663. { "SHF_WRITE", SHF_WRITE },
  12664. { "SHF_ALLOC", SHF_ALLOC },
  12665. { "SHF_EXECINSTR", SHF_EXECINSTR },
  12666. { "SHF_MERGE", SHF_MERGE },
  12667. { "SHF_STRINGS", SHF_STRINGS },
  12668. { "SHF_INFO_LINK", SHF_INFO_LINK},
  12669. { "SHF_LINK_ORDER", SHF_LINK_ORDER},
  12670. { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING},
  12671. { "SHF_GROUP", SHF_GROUP },
  12672. { "SHF_TLS", SHF_TLS },
  12673. { "SHF_MASKOS", SHF_MASKOS },
  12674. { "SHF_EXCLUDE", SHF_EXCLUDE },
  12675. };
  12676. /* Returns TRUE if the section is to be included, otherwise FALSE. */
  12677. bool
  12678. bfd_elf_lookup_section_flags (struct bfd_link_info *info,
  12679. struct flag_info *flaginfo,
  12680. asection *section)
  12681. {
  12682. const bfd_vma sh_flags = elf_section_flags (section);
  12683. if (!flaginfo->flags_initialized)
  12684. {
  12685. bfd *obfd = info->output_bfd;
  12686. const struct elf_backend_data *bed = get_elf_backend_data (obfd);
  12687. struct flag_info_list *tf = flaginfo->flag_list;
  12688. int with_hex = 0;
  12689. int without_hex = 0;
  12690. for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next)
  12691. {
  12692. unsigned i;
  12693. flagword (*lookup) (char *);
  12694. lookup = bed->elf_backend_lookup_section_flags_hook;
  12695. if (lookup != NULL)
  12696. {
  12697. flagword hexval = (*lookup) ((char *) tf->name);
  12698. if (hexval != 0)
  12699. {
  12700. if (tf->with == with_flags)
  12701. with_hex |= hexval;
  12702. else if (tf->with == without_flags)
  12703. without_hex |= hexval;
  12704. tf->valid = true;
  12705. continue;
  12706. }
  12707. }
  12708. for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i)
  12709. {
  12710. if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0)
  12711. {
  12712. if (tf->with == with_flags)
  12713. with_hex |= elf_flags_to_names[i].flag_value;
  12714. else if (tf->with == without_flags)
  12715. without_hex |= elf_flags_to_names[i].flag_value;
  12716. tf->valid = true;
  12717. break;
  12718. }
  12719. }
  12720. if (!tf->valid)
  12721. {
  12722. info->callbacks->einfo
  12723. (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name);
  12724. return false;
  12725. }
  12726. }
  12727. flaginfo->flags_initialized = true;
  12728. flaginfo->only_with_flags |= with_hex;
  12729. flaginfo->not_with_flags |= without_hex;
  12730. }
  12731. if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags)
  12732. return false;
  12733. if ((flaginfo->not_with_flags & sh_flags) != 0)
  12734. return false;
  12735. return true;
  12736. }
  12737. struct alloc_got_off_arg {
  12738. bfd_vma gotoff;
  12739. struct bfd_link_info *info;
  12740. };
  12741. /* We need a special top-level link routine to convert got reference counts
  12742. to real got offsets. */
  12743. static bool
  12744. elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg)
  12745. {
  12746. struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg;
  12747. bfd *obfd = gofarg->info->output_bfd;
  12748. const struct elf_backend_data *bed = get_elf_backend_data (obfd);
  12749. if (h->got.refcount > 0)
  12750. {
  12751. h->got.offset = gofarg->gotoff;
  12752. gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0);
  12753. }
  12754. else
  12755. h->got.offset = (bfd_vma) -1;
  12756. return true;
  12757. }
  12758. /* And an accompanying bit to work out final got entry offsets once
  12759. we're done. Should be called from final_link. */
  12760. bool
  12761. bfd_elf_gc_common_finalize_got_offsets (bfd *abfd,
  12762. struct bfd_link_info *info)
  12763. {
  12764. bfd *i;
  12765. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  12766. bfd_vma gotoff;
  12767. struct alloc_got_off_arg gofarg;
  12768. BFD_ASSERT (abfd == info->output_bfd);
  12769. if (! is_elf_hash_table (info->hash))
  12770. return false;
  12771. /* The GOT offset is relative to the .got section, but the GOT header is
  12772. put into the .got.plt section, if the backend uses it. */
  12773. if (bed->want_got_plt)
  12774. gotoff = 0;
  12775. else
  12776. gotoff = bed->got_header_size;
  12777. /* Do the local .got entries first. */
  12778. for (i = info->input_bfds; i; i = i->link.next)
  12779. {
  12780. bfd_signed_vma *local_got;
  12781. size_t j, locsymcount;
  12782. Elf_Internal_Shdr *symtab_hdr;
  12783. if (bfd_get_flavour (i) != bfd_target_elf_flavour)
  12784. continue;
  12785. local_got = elf_local_got_refcounts (i);
  12786. if (!local_got)
  12787. continue;
  12788. symtab_hdr = &elf_tdata (i)->symtab_hdr;
  12789. if (elf_bad_symtab (i))
  12790. locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym;
  12791. else
  12792. locsymcount = symtab_hdr->sh_info;
  12793. for (j = 0; j < locsymcount; ++j)
  12794. {
  12795. if (local_got[j] > 0)
  12796. {
  12797. local_got[j] = gotoff;
  12798. gotoff += bed->got_elt_size (abfd, info, NULL, i, j);
  12799. }
  12800. else
  12801. local_got[j] = (bfd_vma) -1;
  12802. }
  12803. }
  12804. /* Then the global .got entries. .plt refcounts are handled by
  12805. adjust_dynamic_symbol */
  12806. gofarg.gotoff = gotoff;
  12807. gofarg.info = info;
  12808. elf_link_hash_traverse (elf_hash_table (info),
  12809. elf_gc_allocate_got_offsets,
  12810. &gofarg);
  12811. return true;
  12812. }
  12813. /* Many folk need no more in the way of final link than this, once
  12814. got entry reference counting is enabled. */
  12815. bool
  12816. bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info)
  12817. {
  12818. if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info))
  12819. return false;
  12820. /* Invoke the regular ELF backend linker to do all the work. */
  12821. return bfd_elf_final_link (abfd, info);
  12822. }
  12823. bool
  12824. bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie)
  12825. {
  12826. struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie;
  12827. if (rcookie->bad_symtab)
  12828. rcookie->rel = rcookie->rels;
  12829. for (; rcookie->rel < rcookie->relend; rcookie->rel++)
  12830. {
  12831. unsigned long r_symndx;
  12832. if (! rcookie->bad_symtab)
  12833. if (rcookie->rel->r_offset > offset)
  12834. return false;
  12835. if (rcookie->rel->r_offset != offset)
  12836. continue;
  12837. r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift;
  12838. if (r_symndx == STN_UNDEF)
  12839. return true;
  12840. if (r_symndx >= rcookie->locsymcount
  12841. || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL)
  12842. {
  12843. struct elf_link_hash_entry *h;
  12844. h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff];
  12845. while (h->root.type == bfd_link_hash_indirect
  12846. || h->root.type == bfd_link_hash_warning)
  12847. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  12848. if ((h->root.type == bfd_link_hash_defined
  12849. || h->root.type == bfd_link_hash_defweak)
  12850. && (h->root.u.def.section->owner != rcookie->abfd
  12851. || h->root.u.def.section->kept_section != NULL
  12852. || discarded_section (h->root.u.def.section)))
  12853. return true;
  12854. }
  12855. else
  12856. {
  12857. /* It's not a relocation against a global symbol,
  12858. but it could be a relocation against a local
  12859. symbol for a discarded section. */
  12860. asection *isec;
  12861. Elf_Internal_Sym *isym;
  12862. /* Need to: get the symbol; get the section. */
  12863. isym = &rcookie->locsyms[r_symndx];
  12864. isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx);
  12865. if (isec != NULL
  12866. && (isec->kept_section != NULL
  12867. || discarded_section (isec)))
  12868. return true;
  12869. }
  12870. return false;
  12871. }
  12872. return false;
  12873. }
  12874. /* Discard unneeded references to discarded sections.
  12875. Returns -1 on error, 1 if any section's size was changed, 0 if
  12876. nothing changed. This function assumes that the relocations are in
  12877. sorted order, which is true for all known assemblers. */
  12878. int
  12879. bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
  12880. {
  12881. struct elf_reloc_cookie cookie;
  12882. asection *o;
  12883. bfd *abfd;
  12884. int changed = 0;
  12885. if (info->traditional_format
  12886. || !is_elf_hash_table (info->hash))
  12887. return 0;
  12888. o = bfd_get_section_by_name (output_bfd, ".stab");
  12889. if (o != NULL)
  12890. {
  12891. asection *i;
  12892. for (i = o->map_head.s; i != NULL; i = i->map_head.s)
  12893. {
  12894. if (i->size == 0
  12895. || i->reloc_count == 0
  12896. || i->sec_info_type != SEC_INFO_TYPE_STABS)
  12897. continue;
  12898. abfd = i->owner;
  12899. if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  12900. continue;
  12901. if (!init_reloc_cookie_for_section (&cookie, info, i))
  12902. return -1;
  12903. if (_bfd_discard_section_stabs (abfd, i,
  12904. elf_section_data (i)->sec_info,
  12905. bfd_elf_reloc_symbol_deleted_p,
  12906. &cookie))
  12907. changed = 1;
  12908. fini_reloc_cookie_for_section (&cookie, i);
  12909. }
  12910. }
  12911. o = NULL;
  12912. if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
  12913. o = bfd_get_section_by_name (output_bfd, ".eh_frame");
  12914. if (o != NULL)
  12915. {
  12916. asection *i;
  12917. int eh_changed = 0;
  12918. unsigned int eh_alignment; /* Octets. */
  12919. for (i = o->map_head.s; i != NULL; i = i->map_head.s)
  12920. {
  12921. if (i->size == 0)
  12922. continue;
  12923. abfd = i->owner;
  12924. if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  12925. continue;
  12926. if (!init_reloc_cookie_for_section (&cookie, info, i))
  12927. return -1;
  12928. _bfd_elf_parse_eh_frame (abfd, info, i, &cookie);
  12929. if (_bfd_elf_discard_section_eh_frame (abfd, info, i,
  12930. bfd_elf_reloc_symbol_deleted_p,
  12931. &cookie))
  12932. {
  12933. eh_changed = 1;
  12934. if (i->size != i->rawsize)
  12935. changed = 1;
  12936. }
  12937. fini_reloc_cookie_for_section (&cookie, i);
  12938. }
  12939. eh_alignment = ((1 << o->alignment_power)
  12940. * bfd_octets_per_byte (output_bfd, o));
  12941. /* Skip over zero terminator, and prevent empty sections from
  12942. adding alignment padding at the end. */
  12943. for (i = o->map_tail.s; i != NULL; i = i->map_tail.s)
  12944. if (i->size == 0)
  12945. i->flags |= SEC_EXCLUDE;
  12946. else if (i->size > 4)
  12947. break;
  12948. /* The last non-empty eh_frame section doesn't need padding. */
  12949. if (i != NULL)
  12950. i = i->map_tail.s;
  12951. /* Any prior sections must pad the last FDE out to the output
  12952. section alignment. Otherwise we might have zero padding
  12953. between sections, which would be seen as a terminator. */
  12954. for (; i != NULL; i = i->map_tail.s)
  12955. if (i->size == 4)
  12956. /* All but the last zero terminator should have been removed. */
  12957. BFD_FAIL ();
  12958. else
  12959. {
  12960. bfd_size_type size
  12961. = (i->size + eh_alignment - 1) & -eh_alignment;
  12962. if (i->size != size)
  12963. {
  12964. i->size = size;
  12965. changed = 1;
  12966. eh_changed = 1;
  12967. }
  12968. }
  12969. if (eh_changed)
  12970. elf_link_hash_traverse (elf_hash_table (info),
  12971. _bfd_elf_adjust_eh_frame_global_symbol, NULL);
  12972. }
  12973. for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
  12974. {
  12975. const struct elf_backend_data *bed;
  12976. asection *s;
  12977. if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  12978. continue;
  12979. s = abfd->sections;
  12980. if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
  12981. continue;
  12982. bed = get_elf_backend_data (abfd);
  12983. if (bed->elf_backend_discard_info != NULL)
  12984. {
  12985. if (!init_reloc_cookie (&cookie, info, abfd))
  12986. return -1;
  12987. if ((*bed->elf_backend_discard_info) (abfd, &cookie, info))
  12988. changed = 1;
  12989. fini_reloc_cookie (&cookie, abfd);
  12990. }
  12991. }
  12992. if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
  12993. _bfd_elf_end_eh_frame_parsing (info);
  12994. if (info->eh_frame_hdr_type
  12995. && !bfd_link_relocatable (info)
  12996. && _bfd_elf_discard_section_eh_frame_hdr (info))
  12997. changed = 1;
  12998. return changed;
  12999. }
  13000. bool
  13001. _bfd_elf_section_already_linked (bfd *abfd,
  13002. asection *sec,
  13003. struct bfd_link_info *info)
  13004. {
  13005. flagword flags;
  13006. const char *name, *key;
  13007. struct bfd_section_already_linked *l;
  13008. struct bfd_section_already_linked_hash_entry *already_linked_list;
  13009. if (sec->output_section == bfd_abs_section_ptr)
  13010. return false;
  13011. flags = sec->flags;
  13012. /* Return if it isn't a linkonce section. A comdat group section
  13013. also has SEC_LINK_ONCE set. */
  13014. if ((flags & SEC_LINK_ONCE) == 0)
  13015. return false;
  13016. /* Don't put group member sections on our list of already linked
  13017. sections. They are handled as a group via their group section. */
  13018. if (elf_sec_group (sec) != NULL)
  13019. return false;
  13020. /* For a SHT_GROUP section, use the group signature as the key. */
  13021. name = sec->name;
  13022. if ((flags & SEC_GROUP) != 0
  13023. && elf_next_in_group (sec) != NULL
  13024. && elf_group_name (elf_next_in_group (sec)) != NULL)
  13025. key = elf_group_name (elf_next_in_group (sec));
  13026. else
  13027. {
  13028. /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */
  13029. if (startswith (name, ".gnu.linkonce.")
  13030. && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
  13031. key++;
  13032. else
  13033. /* Must be a user linkonce section that doesn't follow gcc's
  13034. naming convention. In this case we won't be matching
  13035. single member groups. */
  13036. key = name;
  13037. }
  13038. already_linked_list = bfd_section_already_linked_table_lookup (key);
  13039. for (l = already_linked_list->entry; l != NULL; l = l->next)
  13040. {
  13041. /* We may have 2 different types of sections on the list: group
  13042. sections with a signature of <key> (<key> is some string),
  13043. and linkonce sections named .gnu.linkonce.<type>.<key>.
  13044. Match like sections. LTO plugin sections are an exception.
  13045. They are always named .gnu.linkonce.t.<key> and match either
  13046. type of section. */
  13047. if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP)
  13048. && ((flags & SEC_GROUP) != 0
  13049. || strcmp (name, l->sec->name) == 0))
  13050. || (l->sec->owner->flags & BFD_PLUGIN) != 0
  13051. || (sec->owner->flags & BFD_PLUGIN) != 0)
  13052. {
  13053. /* The section has already been linked. See if we should
  13054. issue a warning. */
  13055. if (!_bfd_handle_already_linked (sec, l, info))
  13056. return false;
  13057. if (flags & SEC_GROUP)
  13058. {
  13059. asection *first = elf_next_in_group (sec);
  13060. asection *s = first;
  13061. while (s != NULL)
  13062. {
  13063. s->output_section = bfd_abs_section_ptr;
  13064. /* Record which group discards it. */
  13065. s->kept_section = l->sec;
  13066. s = elf_next_in_group (s);
  13067. /* These lists are circular. */
  13068. if (s == first)
  13069. break;
  13070. }
  13071. }
  13072. return true;
  13073. }
  13074. }
  13075. /* A single member comdat group section may be discarded by a
  13076. linkonce section and vice versa. */
  13077. if ((flags & SEC_GROUP) != 0)
  13078. {
  13079. asection *first = elf_next_in_group (sec);
  13080. if (first != NULL && elf_next_in_group (first) == first)
  13081. /* Check this single member group against linkonce sections. */
  13082. for (l = already_linked_list->entry; l != NULL; l = l->next)
  13083. if ((l->sec->flags & SEC_GROUP) == 0
  13084. && bfd_elf_match_symbols_in_sections (l->sec, first, info))
  13085. {
  13086. first->output_section = bfd_abs_section_ptr;
  13087. first->kept_section = l->sec;
  13088. sec->output_section = bfd_abs_section_ptr;
  13089. break;
  13090. }
  13091. }
  13092. else
  13093. /* Check this linkonce section against single member groups. */
  13094. for (l = already_linked_list->entry; l != NULL; l = l->next)
  13095. if (l->sec->flags & SEC_GROUP)
  13096. {
  13097. asection *first = elf_next_in_group (l->sec);
  13098. if (first != NULL
  13099. && elf_next_in_group (first) == first
  13100. && bfd_elf_match_symbols_in_sections (first, sec, info))
  13101. {
  13102. sec->output_section = bfd_abs_section_ptr;
  13103. sec->kept_section = first;
  13104. break;
  13105. }
  13106. }
  13107. /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F'
  13108. referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4
  13109. specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce'
  13110. prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its
  13111. matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded
  13112. but its `.gnu.linkonce.t.F' is discarded means we chose one-only
  13113. `.gnu.linkonce.t.F' section from a different bfd not requiring any
  13114. `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded.
  13115. The reverse order cannot happen as there is never a bfd with only the
  13116. `.gnu.linkonce.r.F' section. The order of sections in a bfd does not
  13117. matter as here were are looking only for cross-bfd sections. */
  13118. if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r."))
  13119. for (l = already_linked_list->entry; l != NULL; l = l->next)
  13120. if ((l->sec->flags & SEC_GROUP) == 0
  13121. && startswith (l->sec->name, ".gnu.linkonce.t."))
  13122. {
  13123. if (abfd != l->sec->owner)
  13124. sec->output_section = bfd_abs_section_ptr;
  13125. break;
  13126. }
  13127. /* This is the first section with this name. Record it. */
  13128. if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
  13129. info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
  13130. return sec->output_section == bfd_abs_section_ptr;
  13131. }
  13132. bool
  13133. _bfd_elf_common_definition (Elf_Internal_Sym *sym)
  13134. {
  13135. return sym->st_shndx == SHN_COMMON;
  13136. }
  13137. unsigned int
  13138. _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED)
  13139. {
  13140. return SHN_COMMON;
  13141. }
  13142. asection *
  13143. _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED)
  13144. {
  13145. return bfd_com_section_ptr;
  13146. }
  13147. bfd_vma
  13148. _bfd_elf_default_got_elt_size (bfd *abfd,
  13149. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  13150. struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
  13151. bfd *ibfd ATTRIBUTE_UNUSED,
  13152. unsigned long symndx ATTRIBUTE_UNUSED)
  13153. {
  13154. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  13155. return bed->s->arch_size / 8;
  13156. }
  13157. /* Routines to support the creation of dynamic relocs. */
  13158. /* Returns the name of the dynamic reloc section associated with SEC. */
  13159. static const char *
  13160. get_dynamic_reloc_section_name (bfd * abfd,
  13161. asection * sec,
  13162. bool is_rela)
  13163. {
  13164. char *name;
  13165. const char *old_name = bfd_section_name (sec);
  13166. const char *prefix = is_rela ? ".rela" : ".rel";
  13167. if (old_name == NULL)
  13168. return NULL;
  13169. name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1);
  13170. sprintf (name, "%s%s", prefix, old_name);
  13171. return name;
  13172. }
  13173. /* Returns the dynamic reloc section associated with SEC.
  13174. If necessary compute the name of the dynamic reloc section based
  13175. on SEC's name (looked up in ABFD's string table) and the setting
  13176. of IS_RELA. */
  13177. asection *
  13178. _bfd_elf_get_dynamic_reloc_section (bfd *abfd,
  13179. asection *sec,
  13180. bool is_rela)
  13181. {
  13182. asection *reloc_sec = elf_section_data (sec)->sreloc;
  13183. if (reloc_sec == NULL)
  13184. {
  13185. const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
  13186. if (name != NULL)
  13187. {
  13188. reloc_sec = bfd_get_linker_section (abfd, name);
  13189. if (reloc_sec != NULL)
  13190. elf_section_data (sec)->sreloc = reloc_sec;
  13191. }
  13192. }
  13193. return reloc_sec;
  13194. }
  13195. /* Returns the dynamic reloc section associated with SEC. If the
  13196. section does not exist it is created and attached to the DYNOBJ
  13197. bfd and stored in the SRELOC field of SEC's elf_section_data
  13198. structure.
  13199. ALIGNMENT is the alignment for the newly created section and
  13200. IS_RELA defines whether the name should be .rela.<SEC's name>
  13201. or .rel.<SEC's name>. The section name is looked up in the
  13202. string table associated with ABFD. */
  13203. asection *
  13204. _bfd_elf_make_dynamic_reloc_section (asection *sec,
  13205. bfd *dynobj,
  13206. unsigned int alignment,
  13207. bfd *abfd,
  13208. bool is_rela)
  13209. {
  13210. asection * reloc_sec = elf_section_data (sec)->sreloc;
  13211. if (reloc_sec == NULL)
  13212. {
  13213. const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela);
  13214. if (name == NULL)
  13215. return NULL;
  13216. reloc_sec = bfd_get_linker_section (dynobj, name);
  13217. if (reloc_sec == NULL)
  13218. {
  13219. flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY
  13220. | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  13221. if ((sec->flags & SEC_ALLOC) != 0)
  13222. flags |= SEC_ALLOC | SEC_LOAD;
  13223. reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags);
  13224. if (reloc_sec != NULL)
  13225. {
  13226. /* _bfd_elf_get_sec_type_attr chooses a section type by
  13227. name. Override as it may be wrong, eg. for a user
  13228. section named "auto" we'll get ".relauto" which is
  13229. seen to be a .rela section. */
  13230. elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL;
  13231. if (!bfd_set_section_alignment (reloc_sec, alignment))
  13232. reloc_sec = NULL;
  13233. }
  13234. }
  13235. elf_section_data (sec)->sreloc = reloc_sec;
  13236. }
  13237. return reloc_sec;
  13238. }
  13239. /* Copy the ELF symbol type and other attributes for a linker script
  13240. assignment from HSRC to HDEST. Generally this should be treated as
  13241. if we found a strong non-dynamic definition for HDEST (except that
  13242. ld ignores multiple definition errors). */
  13243. void
  13244. _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
  13245. struct bfd_link_hash_entry *hdest,
  13246. struct bfd_link_hash_entry *hsrc)
  13247. {
  13248. struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest;
  13249. struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc;
  13250. Elf_Internal_Sym isym;
  13251. ehdest->type = ehsrc->type;
  13252. ehdest->target_internal = ehsrc->target_internal;
  13253. isym.st_other = ehsrc->other;
  13254. elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false);
  13255. }
  13256. /* Append a RELA relocation REL to section S in BFD. */
  13257. void
  13258. elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
  13259. {
  13260. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  13261. bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
  13262. BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size);
  13263. bed->s->swap_reloca_out (abfd, rel, loc);
  13264. }
  13265. /* Append a REL relocation REL to section S in BFD. */
  13266. void
  13267. elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
  13268. {
  13269. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  13270. bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel);
  13271. BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size);
  13272. bed->s->swap_reloc_out (abfd, rel, loc);
  13273. }
  13274. /* Define __start, __stop, .startof. or .sizeof. symbol. */
  13275. struct bfd_link_hash_entry *
  13276. bfd_elf_define_start_stop (struct bfd_link_info *info,
  13277. const char *symbol, asection *sec)
  13278. {
  13279. struct elf_link_hash_entry *h;
  13280. h = elf_link_hash_lookup (elf_hash_table (info), symbol,
  13281. false, false, true);
  13282. /* NB: Common symbols will be turned into definition later. */
  13283. if (h != NULL
  13284. && !h->root.ldscript_def
  13285. && (h->root.type == bfd_link_hash_undefined
  13286. || h->root.type == bfd_link_hash_undefweak
  13287. || ((h->ref_regular || h->def_dynamic)
  13288. && !h->def_regular
  13289. && h->root.type != bfd_link_hash_common)))
  13290. {
  13291. bool was_dynamic = h->ref_dynamic || h->def_dynamic;
  13292. h->verinfo.verdef = NULL;
  13293. h->root.type = bfd_link_hash_defined;
  13294. h->root.u.def.section = sec;
  13295. h->root.u.def.value = 0;
  13296. h->def_regular = 1;
  13297. h->def_dynamic = 0;
  13298. h->start_stop = 1;
  13299. h->u2.start_stop_section = sec;
  13300. if (symbol[0] == '.')
  13301. {
  13302. /* .startof. and .sizeof. symbols are local. */
  13303. const struct elf_backend_data *bed;
  13304. bed = get_elf_backend_data (info->output_bfd);
  13305. (*bed->elf_backend_hide_symbol) (info, h, true);
  13306. }
  13307. else
  13308. {
  13309. if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
  13310. h->other = ((h->other & ~ELF_ST_VISIBILITY (-1))
  13311. | info->start_stop_visibility);
  13312. if (was_dynamic)
  13313. bfd_elf_link_record_dynamic_symbol (info, h);
  13314. }
  13315. return &h->root;
  13316. }
  13317. return NULL;
  13318. }
  13319. /* Find dynamic relocs for H that apply to read-only sections. */
  13320. asection *
  13321. _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h)
  13322. {
  13323. struct elf_dyn_relocs *p;
  13324. for (p = h->dyn_relocs; p != NULL; p = p->next)
  13325. {
  13326. asection *s = p->sec->output_section;
  13327. if (s != NULL && (s->flags & SEC_READONLY) != 0)
  13328. return p->sec;
  13329. }
  13330. return NULL;
  13331. }
  13332. /* Set DF_TEXTREL if we find any dynamic relocs that apply to
  13333. read-only sections. */
  13334. bool
  13335. _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
  13336. {
  13337. asection *sec;
  13338. if (h->root.type == bfd_link_hash_indirect)
  13339. return true;
  13340. sec = _bfd_elf_readonly_dynrelocs (h);
  13341. if (sec != NULL)
  13342. {
  13343. struct bfd_link_info *info = (struct bfd_link_info *) inf;
  13344. info->flags |= DF_TEXTREL;
  13345. /* xgettext:c-format */
  13346. info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' "
  13347. "in read-only section `%pA'\n"),
  13348. sec->owner, h->root.root.string, sec);
  13349. if (bfd_link_textrel_check (info))
  13350. /* xgettext:c-format */
  13351. info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' "
  13352. "in read-only section `%pA'\n"),
  13353. sec->owner, h->root.root.string, sec);
  13354. /* Not an error, just cut short the traversal. */
  13355. return false;
  13356. }
  13357. return true;
  13358. }
  13359. /* Add dynamic tags. */
  13360. bool
  13361. _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info,
  13362. bool need_dynamic_reloc)
  13363. {
  13364. struct elf_link_hash_table *htab = elf_hash_table (info);
  13365. if (htab->dynamic_sections_created)
  13366. {
  13367. /* Add some entries to the .dynamic section. We fill in the
  13368. values later, in finish_dynamic_sections, but we must add
  13369. the entries now so that we get the correct size for the
  13370. .dynamic section. The DT_DEBUG entry is filled in by the
  13371. dynamic linker and used by the debugger. */
  13372. #define add_dynamic_entry(TAG, VAL) \
  13373. _bfd_elf_add_dynamic_entry (info, TAG, VAL)
  13374. const struct elf_backend_data *bed
  13375. = get_elf_backend_data (output_bfd);
  13376. if (bfd_link_executable (info))
  13377. {
  13378. if (!add_dynamic_entry (DT_DEBUG, 0))
  13379. return false;
  13380. }
  13381. if (htab->dt_pltgot_required || htab->splt->size != 0)
  13382. {
  13383. /* DT_PLTGOT is used by prelink even if there is no PLT
  13384. relocation. */
  13385. if (!add_dynamic_entry (DT_PLTGOT, 0))
  13386. return false;
  13387. }
  13388. if (htab->dt_jmprel_required || htab->srelplt->size != 0)
  13389. {
  13390. if (!add_dynamic_entry (DT_PLTRELSZ, 0)
  13391. || !add_dynamic_entry (DT_PLTREL,
  13392. (bed->rela_plts_and_copies_p
  13393. ? DT_RELA : DT_REL))
  13394. || !add_dynamic_entry (DT_JMPREL, 0))
  13395. return false;
  13396. }
  13397. if (htab->tlsdesc_plt
  13398. && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
  13399. || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
  13400. return false;
  13401. if (need_dynamic_reloc)
  13402. {
  13403. if (bed->rela_plts_and_copies_p)
  13404. {
  13405. if (!add_dynamic_entry (DT_RELA, 0)
  13406. || !add_dynamic_entry (DT_RELASZ, 0)
  13407. || !add_dynamic_entry (DT_RELAENT,
  13408. bed->s->sizeof_rela))
  13409. return false;
  13410. }
  13411. else
  13412. {
  13413. if (!add_dynamic_entry (DT_REL, 0)
  13414. || !add_dynamic_entry (DT_RELSZ, 0)
  13415. || !add_dynamic_entry (DT_RELENT,
  13416. bed->s->sizeof_rel))
  13417. return false;
  13418. }
  13419. /* If any dynamic relocs apply to a read-only section,
  13420. then we need a DT_TEXTREL entry. */
  13421. if ((info->flags & DF_TEXTREL) == 0)
  13422. elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel,
  13423. info);
  13424. if ((info->flags & DF_TEXTREL) != 0)
  13425. {
  13426. if (htab->ifunc_resolvers)
  13427. info->callbacks->einfo
  13428. (_("%P: warning: GNU indirect functions with DT_TEXTREL "
  13429. "may result in a segfault at runtime; recompile with %s\n"),
  13430. bfd_link_dll (info) ? "-fPIC" : "-fPIE");
  13431. if (!add_dynamic_entry (DT_TEXTREL, 0))
  13432. return false;
  13433. }
  13434. }
  13435. }
  13436. #undef add_dynamic_entry
  13437. return true;
  13438. }