ldlang.c 265 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727
  1. /* Linker command language support.
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. This file is part of the GNU Binutils.
  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 <limits.h>
  18. #include "bfd.h"
  19. #include "libiberty.h"
  20. #include "filenames.h"
  21. #include "safe-ctype.h"
  22. #include "obstack.h"
  23. #include "bfdlink.h"
  24. #include "ctf-api.h"
  25. #include "ld.h"
  26. #include "ldmain.h"
  27. #include "ldexp.h"
  28. #include "ldlang.h"
  29. #include <ldgram.h>
  30. #include "ldlex.h"
  31. #include "ldmisc.h"
  32. #include "ldctor.h"
  33. #include "ldfile.h"
  34. #include "ldemul.h"
  35. #include "fnmatch.h"
  36. #include "demangle.h"
  37. #include "hashtab.h"
  38. #include "elf-bfd.h"
  39. #if BFD_SUPPORTS_PLUGINS
  40. #include "plugin.h"
  41. #endif /* BFD_SUPPORTS_PLUGINS */
  42. #ifndef offsetof
  43. #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
  44. #endif
  45. /* Convert between addresses in bytes and sizes in octets.
  46. For currently supported targets, octets_per_byte is always a power
  47. of two, so we can use shifts. */
  48. #define TO_ADDR(X) ((X) >> opb_shift)
  49. #define TO_SIZE(X) ((X) << opb_shift)
  50. /* Local variables. */
  51. static struct obstack stat_obstack;
  52. static struct obstack map_obstack;
  53. #define obstack_chunk_alloc xmalloc
  54. #define obstack_chunk_free free
  55. static const char *entry_symbol_default = "start";
  56. static bool map_head_is_link_order = false;
  57. static lang_output_section_statement_type *default_common_section;
  58. static bool map_option_f;
  59. static bfd_vma print_dot;
  60. static lang_input_statement_type *first_file;
  61. static const char *current_target;
  62. static lang_statement_list_type *stat_save[10];
  63. static lang_statement_list_type **stat_save_ptr = &stat_save[0];
  64. static struct unique_sections *unique_section_list;
  65. static struct asneeded_minfo *asneeded_list_head;
  66. static unsigned int opb_shift = 0;
  67. /* Forward declarations. */
  68. static void exp_init_os (etree_type *);
  69. static lang_input_statement_type *lookup_name (const char *);
  70. static void insert_undefined (const char *);
  71. static bool sort_def_symbol (struct bfd_link_hash_entry *, void *);
  72. static void print_statement (lang_statement_union_type *,
  73. lang_output_section_statement_type *);
  74. static void print_statement_list (lang_statement_union_type *,
  75. lang_output_section_statement_type *);
  76. static void print_statements (void);
  77. static void print_input_section (asection *, bool);
  78. static bool lang_one_common (struct bfd_link_hash_entry *, void *);
  79. static void lang_record_phdrs (void);
  80. static void lang_do_version_exports_section (void);
  81. static void lang_finalize_version_expr_head
  82. (struct bfd_elf_version_expr_head *);
  83. static void lang_do_memory_regions (bool);
  84. /* Exported variables. */
  85. const char *output_target;
  86. lang_output_section_statement_type *abs_output_section;
  87. /* Header for list of statements corresponding to any files involved in the
  88. link, either specified from the command-line or added implicitely (eg.
  89. archive member used to resolved undefined symbol, wildcard statement from
  90. linker script, etc.). Next pointer is in next field of a
  91. lang_statement_header_type (reached via header field in a
  92. lang_statement_union). */
  93. lang_statement_list_type statement_list;
  94. lang_statement_list_type lang_os_list;
  95. lang_statement_list_type *stat_ptr = &statement_list;
  96. /* Header for list of statements corresponding to files used in the final
  97. executable. This can be either object file specified on the command-line
  98. or library member resolving an undefined reference. Next pointer is in next
  99. field of a lang_input_statement_type (reached via input_statement field in a
  100. lang_statement_union). */
  101. lang_statement_list_type file_chain = { NULL, NULL };
  102. /* Header for list of statements corresponding to files specified on the
  103. command-line for linking. It thus contains real object files and archive
  104. but not archive members. Next pointer is in next_real_file field of a
  105. lang_input_statement_type statement (reached via input_statement field in a
  106. lang_statement_union). */
  107. lang_statement_list_type input_file_chain;
  108. static const char *current_input_file;
  109. struct bfd_elf_dynamic_list **current_dynamic_list_p;
  110. struct bfd_sym_chain entry_symbol = { NULL, NULL };
  111. const char *entry_section = ".text";
  112. struct lang_input_statement_flags input_flags;
  113. bool entry_from_cmdline;
  114. bool lang_has_input_file = false;
  115. bool had_output_filename = false;
  116. bool lang_float_flag = false;
  117. bool delete_output_file_on_failure = false;
  118. struct lang_phdr *lang_phdr_list;
  119. struct lang_nocrossrefs *nocrossref_list;
  120. struct asneeded_minfo **asneeded_list_tail;
  121. #ifdef ENABLE_LIBCTF
  122. static ctf_dict_t *ctf_output;
  123. #endif
  124. /* Functions that traverse the linker script and might evaluate
  125. DEFINED() need to increment this at the start of the traversal. */
  126. int lang_statement_iteration = 0;
  127. /* Count times through one_lang_size_sections_pass after mark phase. */
  128. static int lang_sizing_iteration = 0;
  129. /* Return TRUE if the PATTERN argument is a wildcard pattern.
  130. Although backslashes are treated specially if a pattern contains
  131. wildcards, we do not consider the mere presence of a backslash to
  132. be enough to cause the pattern to be treated as a wildcard.
  133. That lets us handle DOS filenames more naturally. */
  134. #define wildcardp(pattern) (strpbrk ((pattern), "?*[") != NULL)
  135. #define new_stat(x, y) \
  136. (x##_type *) new_statement (x##_enum, sizeof (x##_type), y)
  137. #define outside_section_address(q) \
  138. ((q)->output_offset + (q)->output_section->vma)
  139. #define outside_symbol_address(q) \
  140. ((q)->value + outside_section_address (q->section))
  141. /* CTF sections smaller than this are not compressed: compression of
  142. dictionaries this small doesn't gain much, and this lets consumers mmap the
  143. sections directly out of the ELF file and use them with no decompression
  144. overhead if they want to. */
  145. #define CTF_COMPRESSION_THRESHOLD 4096
  146. void *
  147. stat_alloc (size_t size)
  148. {
  149. return obstack_alloc (&stat_obstack, size);
  150. }
  151. static int
  152. name_match (const char *pattern, const char *name)
  153. {
  154. if (wildcardp (pattern))
  155. return fnmatch (pattern, name, 0);
  156. return strcmp (pattern, name);
  157. }
  158. static char *
  159. ldirname (const char *name)
  160. {
  161. const char *base = lbasename (name);
  162. char *dirname;
  163. while (base > name && IS_DIR_SEPARATOR (base[-1]))
  164. --base;
  165. if (base == name)
  166. return strdup (".");
  167. dirname = strdup (name);
  168. dirname[base - name] = '\0';
  169. return dirname;
  170. }
  171. /* If PATTERN is of the form archive:file, return a pointer to the
  172. separator. If not, return NULL. */
  173. static char *
  174. archive_path (const char *pattern)
  175. {
  176. char *p = NULL;
  177. if (link_info.path_separator == 0)
  178. return p;
  179. p = strchr (pattern, link_info.path_separator);
  180. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  181. if (p == NULL || link_info.path_separator != ':')
  182. return p;
  183. /* Assume a match on the second char is part of drive specifier,
  184. as in "c:\silly.dos". */
  185. if (p == pattern + 1 && ISALPHA (*pattern))
  186. p = strchr (p + 1, link_info.path_separator);
  187. #endif
  188. return p;
  189. }
  190. /* Given that FILE_SPEC results in a non-NULL SEP result from archive_path,
  191. return whether F matches FILE_SPEC. */
  192. static bool
  193. input_statement_is_archive_path (const char *file_spec, char *sep,
  194. lang_input_statement_type *f)
  195. {
  196. bool match = false;
  197. if ((*(sep + 1) == 0
  198. || name_match (sep + 1, f->filename) == 0)
  199. && ((sep != file_spec)
  200. == (f->the_bfd != NULL && f->the_bfd->my_archive != NULL)))
  201. {
  202. match = true;
  203. if (sep != file_spec)
  204. {
  205. const char *aname = bfd_get_filename (f->the_bfd->my_archive);
  206. *sep = 0;
  207. match = name_match (file_spec, aname) == 0;
  208. *sep = link_info.path_separator;
  209. }
  210. }
  211. return match;
  212. }
  213. static bool
  214. unique_section_p (const asection *sec,
  215. const lang_output_section_statement_type *os)
  216. {
  217. struct unique_sections *unam;
  218. const char *secnam;
  219. if (!link_info.resolve_section_groups
  220. && sec->owner != NULL
  221. && bfd_is_group_section (sec->owner, sec))
  222. return !(os != NULL
  223. && strcmp (os->name, DISCARD_SECTION_NAME) == 0);
  224. secnam = sec->name;
  225. for (unam = unique_section_list; unam; unam = unam->next)
  226. if (name_match (unam->name, secnam) == 0)
  227. return true;
  228. return false;
  229. }
  230. /* Generic traversal routines for finding matching sections. */
  231. /* Return true if FILE matches a pattern in EXCLUDE_LIST, otherwise return
  232. false. */
  233. static bool
  234. walk_wild_file_in_exclude_list (struct name_list *exclude_list,
  235. lang_input_statement_type *file)
  236. {
  237. struct name_list *list_tmp;
  238. for (list_tmp = exclude_list;
  239. list_tmp;
  240. list_tmp = list_tmp->next)
  241. {
  242. char *p = archive_path (list_tmp->name);
  243. if (p != NULL)
  244. {
  245. if (input_statement_is_archive_path (list_tmp->name, p, file))
  246. return true;
  247. }
  248. else if (name_match (list_tmp->name, file->filename) == 0)
  249. return true;
  250. /* FIXME: Perhaps remove the following at some stage? Matching
  251. unadorned archives like this was never documented and has
  252. been superceded by the archive:path syntax. */
  253. else if (file->the_bfd != NULL
  254. && file->the_bfd->my_archive != NULL
  255. && name_match (list_tmp->name,
  256. bfd_get_filename (file->the_bfd->my_archive)) == 0)
  257. return true;
  258. }
  259. return false;
  260. }
  261. /* Try processing a section against a wildcard. This just calls
  262. the callback unless the filename exclusion list is present
  263. and excludes the file. It's hardly ever present so this
  264. function is very fast. */
  265. static void
  266. walk_wild_consider_section (lang_wild_statement_type *ptr,
  267. lang_input_statement_type *file,
  268. asection *s,
  269. struct wildcard_list *sec,
  270. callback_t callback,
  271. void *data)
  272. {
  273. /* Don't process sections from files which were excluded. */
  274. if (walk_wild_file_in_exclude_list (sec->spec.exclude_name_list, file))
  275. return;
  276. (*callback) (ptr, sec, s, file, data);
  277. }
  278. /* Lowest common denominator routine that can handle everything correctly,
  279. but slowly. */
  280. static void
  281. walk_wild_section_general (lang_wild_statement_type *ptr,
  282. lang_input_statement_type *file,
  283. callback_t callback,
  284. void *data)
  285. {
  286. asection *s;
  287. struct wildcard_list *sec;
  288. for (s = file->the_bfd->sections; s != NULL; s = s->next)
  289. {
  290. sec = ptr->section_list;
  291. if (sec == NULL)
  292. (*callback) (ptr, sec, s, file, data);
  293. while (sec != NULL)
  294. {
  295. bool skip = false;
  296. if (sec->spec.name != NULL)
  297. {
  298. const char *sname = bfd_section_name (s);
  299. skip = name_match (sec->spec.name, sname) != 0;
  300. }
  301. if (!skip)
  302. walk_wild_consider_section (ptr, file, s, sec, callback, data);
  303. sec = sec->next;
  304. }
  305. }
  306. }
  307. /* Routines to find a single section given its name. If there's more
  308. than one section with that name, we report that. */
  309. typedef struct
  310. {
  311. asection *found_section;
  312. bool multiple_sections_found;
  313. } section_iterator_callback_data;
  314. static bool
  315. section_iterator_callback (bfd *abfd ATTRIBUTE_UNUSED, asection *s, void *data)
  316. {
  317. section_iterator_callback_data *d = (section_iterator_callback_data *) data;
  318. if (d->found_section != NULL)
  319. {
  320. d->multiple_sections_found = true;
  321. return true;
  322. }
  323. d->found_section = s;
  324. return false;
  325. }
  326. static asection *
  327. find_section (lang_input_statement_type *file,
  328. struct wildcard_list *sec,
  329. bool *multiple_sections_found)
  330. {
  331. section_iterator_callback_data cb_data = { NULL, false };
  332. bfd_get_section_by_name_if (file->the_bfd, sec->spec.name,
  333. section_iterator_callback, &cb_data);
  334. *multiple_sections_found = cb_data.multiple_sections_found;
  335. return cb_data.found_section;
  336. }
  337. /* Code for handling simple wildcards without going through fnmatch,
  338. which can be expensive because of charset translations etc. */
  339. /* A simple wild is a literal string followed by a single '*',
  340. where the literal part is at least 4 characters long. */
  341. static bool
  342. is_simple_wild (const char *name)
  343. {
  344. size_t len = strcspn (name, "*?[");
  345. return len >= 4 && name[len] == '*' && name[len + 1] == '\0';
  346. }
  347. static bool
  348. match_simple_wild (const char *pattern, const char *name)
  349. {
  350. /* The first four characters of the pattern are guaranteed valid
  351. non-wildcard characters. So we can go faster. */
  352. if (pattern[0] != name[0] || pattern[1] != name[1]
  353. || pattern[2] != name[2] || pattern[3] != name[3])
  354. return false;
  355. pattern += 4;
  356. name += 4;
  357. while (*pattern != '*')
  358. if (*name++ != *pattern++)
  359. return false;
  360. return true;
  361. }
  362. /* Return the numerical value of the init_priority attribute from
  363. section name NAME. */
  364. static int
  365. get_init_priority (const asection *sec)
  366. {
  367. const char *name = bfd_section_name (sec);
  368. const char *dot;
  369. /* GCC uses the following section names for the init_priority
  370. attribute with numerical values 101 to 65535 inclusive. A
  371. lower value means a higher priority.
  372. 1: .init_array.NNNNN/.fini_array.NNNNN: Where NNNNN is the
  373. decimal numerical value of the init_priority attribute.
  374. The order of execution in .init_array is forward and
  375. .fini_array is backward.
  376. 2: .ctors.NNNNN/.dtors.NNNNN: Where NNNNN is 65535 minus the
  377. decimal numerical value of the init_priority attribute.
  378. The order of execution in .ctors is backward and .dtors
  379. is forward.
  380. .init_array.NNNNN sections would normally be placed in an output
  381. .init_array section, .fini_array.NNNNN in .fini_array,
  382. .ctors.NNNNN in .ctors, and .dtors.NNNNN in .dtors. This means
  383. we should sort by increasing number (and could just use
  384. SORT_BY_NAME in scripts). However if .ctors.NNNNN sections are
  385. being placed in .init_array (which may also contain
  386. .init_array.NNNNN sections) or .dtors.NNNNN sections are being
  387. placed in .fini_array then we need to extract the init_priority
  388. attribute and sort on that. */
  389. dot = strrchr (name, '.');
  390. if (dot != NULL && ISDIGIT (dot[1]))
  391. {
  392. char *end;
  393. unsigned long init_priority = strtoul (dot + 1, &end, 10);
  394. if (*end == 0)
  395. {
  396. if (dot == name + 6
  397. && (strncmp (name, ".ctors", 6) == 0
  398. || strncmp (name, ".dtors", 6) == 0))
  399. init_priority = 65535 - init_priority;
  400. if (init_priority <= INT_MAX)
  401. return init_priority;
  402. }
  403. }
  404. return -1;
  405. }
  406. /* Compare sections ASEC and BSEC according to SORT. */
  407. static int
  408. compare_section (sort_type sort, asection *asec, asection *bsec)
  409. {
  410. int ret;
  411. int a_priority, b_priority;
  412. switch (sort)
  413. {
  414. default:
  415. abort ();
  416. case by_init_priority:
  417. a_priority = get_init_priority (asec);
  418. b_priority = get_init_priority (bsec);
  419. if (a_priority < 0 || b_priority < 0)
  420. goto sort_by_name;
  421. ret = a_priority - b_priority;
  422. if (ret)
  423. break;
  424. else
  425. goto sort_by_name;
  426. case by_alignment_name:
  427. ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec);
  428. if (ret)
  429. break;
  430. /* Fall through. */
  431. case by_name:
  432. sort_by_name:
  433. ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec));
  434. break;
  435. case by_name_alignment:
  436. ret = strcmp (bfd_section_name (asec), bfd_section_name (bsec));
  437. if (ret)
  438. break;
  439. /* Fall through. */
  440. case by_alignment:
  441. ret = bfd_section_alignment (bsec) - bfd_section_alignment (asec);
  442. break;
  443. }
  444. return ret;
  445. }
  446. /* Build a Binary Search Tree to sort sections, unlike insertion sort
  447. used in wild_sort(). BST is considerably faster if the number of
  448. of sections are large. */
  449. static lang_section_bst_type **
  450. wild_sort_fast (lang_wild_statement_type *wild,
  451. struct wildcard_list *sec,
  452. lang_input_statement_type *file ATTRIBUTE_UNUSED,
  453. asection *section)
  454. {
  455. lang_section_bst_type **tree;
  456. tree = &wild->tree;
  457. if (!wild->filenames_sorted
  458. && (sec == NULL || sec->spec.sorted == none))
  459. {
  460. /* Append at the right end of tree. */
  461. while (*tree)
  462. tree = &((*tree)->right);
  463. return tree;
  464. }
  465. while (*tree)
  466. {
  467. /* Find the correct node to append this section. */
  468. if (compare_section (sec->spec.sorted, section, (*tree)->section) < 0)
  469. tree = &((*tree)->left);
  470. else
  471. tree = &((*tree)->right);
  472. }
  473. return tree;
  474. }
  475. /* Use wild_sort_fast to build a BST to sort sections. */
  476. static void
  477. output_section_callback_fast (lang_wild_statement_type *ptr,
  478. struct wildcard_list *sec,
  479. asection *section,
  480. lang_input_statement_type *file,
  481. void *output)
  482. {
  483. lang_section_bst_type *node;
  484. lang_section_bst_type **tree;
  485. lang_output_section_statement_type *os;
  486. os = (lang_output_section_statement_type *) output;
  487. if (unique_section_p (section, os))
  488. return;
  489. node = (lang_section_bst_type *) xmalloc (sizeof (lang_section_bst_type));
  490. node->left = 0;
  491. node->right = 0;
  492. node->section = section;
  493. node->pattern = ptr->section_list;
  494. tree = wild_sort_fast (ptr, sec, file, section);
  495. if (tree != NULL)
  496. *tree = node;
  497. }
  498. /* Convert a sorted sections' BST back to list form. */
  499. static void
  500. output_section_callback_tree_to_list (lang_wild_statement_type *ptr,
  501. lang_section_bst_type *tree,
  502. void *output)
  503. {
  504. if (tree->left)
  505. output_section_callback_tree_to_list (ptr, tree->left, output);
  506. lang_add_section (&ptr->children, tree->section, tree->pattern, NULL,
  507. (lang_output_section_statement_type *) output);
  508. if (tree->right)
  509. output_section_callback_tree_to_list (ptr, tree->right, output);
  510. free (tree);
  511. }
  512. /* Specialized, optimized routines for handling different kinds of
  513. wildcards */
  514. static void
  515. walk_wild_section_specs1_wild0 (lang_wild_statement_type *ptr,
  516. lang_input_statement_type *file,
  517. callback_t callback,
  518. void *data)
  519. {
  520. /* We can just do a hash lookup for the section with the right name.
  521. But if that lookup discovers more than one section with the name
  522. (should be rare), we fall back to the general algorithm because
  523. we would otherwise have to sort the sections to make sure they
  524. get processed in the bfd's order. */
  525. bool multiple_sections_found;
  526. struct wildcard_list *sec0 = ptr->handler_data[0];
  527. asection *s0 = find_section (file, sec0, &multiple_sections_found);
  528. if (multiple_sections_found)
  529. walk_wild_section_general (ptr, file, callback, data);
  530. else if (s0)
  531. walk_wild_consider_section (ptr, file, s0, sec0, callback, data);
  532. }
  533. static void
  534. walk_wild_section_specs1_wild1 (lang_wild_statement_type *ptr,
  535. lang_input_statement_type *file,
  536. callback_t callback,
  537. void *data)
  538. {
  539. asection *s;
  540. struct wildcard_list *wildsec0 = ptr->handler_data[0];
  541. for (s = file->the_bfd->sections; s != NULL; s = s->next)
  542. {
  543. const char *sname = bfd_section_name (s);
  544. bool skip = !match_simple_wild (wildsec0->spec.name, sname);
  545. if (!skip)
  546. walk_wild_consider_section (ptr, file, s, wildsec0, callback, data);
  547. }
  548. }
  549. static void
  550. walk_wild_section_specs2_wild1 (lang_wild_statement_type *ptr,
  551. lang_input_statement_type *file,
  552. callback_t callback,
  553. void *data)
  554. {
  555. asection *s;
  556. struct wildcard_list *sec0 = ptr->handler_data[0];
  557. struct wildcard_list *wildsec1 = ptr->handler_data[1];
  558. bool multiple_sections_found;
  559. asection *s0 = find_section (file, sec0, &multiple_sections_found);
  560. if (multiple_sections_found)
  561. {
  562. walk_wild_section_general (ptr, file, callback, data);
  563. return;
  564. }
  565. /* Note that if the section was not found, s0 is NULL and
  566. we'll simply never succeed the s == s0 test below. */
  567. for (s = file->the_bfd->sections; s != NULL; s = s->next)
  568. {
  569. /* Recall that in this code path, a section cannot satisfy more
  570. than one spec, so if s == s0 then it cannot match
  571. wildspec1. */
  572. if (s == s0)
  573. walk_wild_consider_section (ptr, file, s, sec0, callback, data);
  574. else
  575. {
  576. const char *sname = bfd_section_name (s);
  577. bool skip = !match_simple_wild (wildsec1->spec.name, sname);
  578. if (!skip)
  579. walk_wild_consider_section (ptr, file, s, wildsec1, callback,
  580. data);
  581. }
  582. }
  583. }
  584. static void
  585. walk_wild_section_specs3_wild2 (lang_wild_statement_type *ptr,
  586. lang_input_statement_type *file,
  587. callback_t callback,
  588. void *data)
  589. {
  590. asection *s;
  591. struct wildcard_list *sec0 = ptr->handler_data[0];
  592. struct wildcard_list *wildsec1 = ptr->handler_data[1];
  593. struct wildcard_list *wildsec2 = ptr->handler_data[2];
  594. bool multiple_sections_found;
  595. asection *s0 = find_section (file, sec0, &multiple_sections_found);
  596. if (multiple_sections_found)
  597. {
  598. walk_wild_section_general (ptr, file, callback, data);
  599. return;
  600. }
  601. for (s = file->the_bfd->sections; s != NULL; s = s->next)
  602. {
  603. if (s == s0)
  604. walk_wild_consider_section (ptr, file, s, sec0, callback, data);
  605. else
  606. {
  607. const char *sname = bfd_section_name (s);
  608. bool skip = !match_simple_wild (wildsec1->spec.name, sname);
  609. if (!skip)
  610. walk_wild_consider_section (ptr, file, s, wildsec1, callback, data);
  611. else
  612. {
  613. skip = !match_simple_wild (wildsec2->spec.name, sname);
  614. if (!skip)
  615. walk_wild_consider_section (ptr, file, s, wildsec2, callback,
  616. data);
  617. }
  618. }
  619. }
  620. }
  621. static void
  622. walk_wild_section_specs4_wild2 (lang_wild_statement_type *ptr,
  623. lang_input_statement_type *file,
  624. callback_t callback,
  625. void *data)
  626. {
  627. asection *s;
  628. struct wildcard_list *sec0 = ptr->handler_data[0];
  629. struct wildcard_list *sec1 = ptr->handler_data[1];
  630. struct wildcard_list *wildsec2 = ptr->handler_data[2];
  631. struct wildcard_list *wildsec3 = ptr->handler_data[3];
  632. bool multiple_sections_found;
  633. asection *s0 = find_section (file, sec0, &multiple_sections_found), *s1;
  634. if (multiple_sections_found)
  635. {
  636. walk_wild_section_general (ptr, file, callback, data);
  637. return;
  638. }
  639. s1 = find_section (file, sec1, &multiple_sections_found);
  640. if (multiple_sections_found)
  641. {
  642. walk_wild_section_general (ptr, file, callback, data);
  643. return;
  644. }
  645. for (s = file->the_bfd->sections; s != NULL; s = s->next)
  646. {
  647. if (s == s0)
  648. walk_wild_consider_section (ptr, file, s, sec0, callback, data);
  649. else
  650. if (s == s1)
  651. walk_wild_consider_section (ptr, file, s, sec1, callback, data);
  652. else
  653. {
  654. const char *sname = bfd_section_name (s);
  655. bool skip = !match_simple_wild (wildsec2->spec.name, sname);
  656. if (!skip)
  657. walk_wild_consider_section (ptr, file, s, wildsec2, callback,
  658. data);
  659. else
  660. {
  661. skip = !match_simple_wild (wildsec3->spec.name, sname);
  662. if (!skip)
  663. walk_wild_consider_section (ptr, file, s, wildsec3,
  664. callback, data);
  665. }
  666. }
  667. }
  668. }
  669. static void
  670. walk_wild_section (lang_wild_statement_type *ptr,
  671. lang_input_statement_type *file,
  672. callback_t callback,
  673. void *data)
  674. {
  675. if (file->flags.just_syms)
  676. return;
  677. (*ptr->walk_wild_section_handler) (ptr, file, callback, data);
  678. }
  679. /* Returns TRUE when name1 is a wildcard spec that might match
  680. something name2 can match. We're conservative: we return FALSE
  681. only if the prefixes of name1 and name2 are different up to the
  682. first wildcard character. */
  683. static bool
  684. wild_spec_can_overlap (const char *name1, const char *name2)
  685. {
  686. size_t prefix1_len = strcspn (name1, "?*[");
  687. size_t prefix2_len = strcspn (name2, "?*[");
  688. size_t min_prefix_len;
  689. /* Note that if there is no wildcard character, then we treat the
  690. terminating 0 as part of the prefix. Thus ".text" won't match
  691. ".text." or ".text.*", for example. */
  692. if (name1[prefix1_len] == '\0')
  693. prefix1_len++;
  694. if (name2[prefix2_len] == '\0')
  695. prefix2_len++;
  696. min_prefix_len = prefix1_len < prefix2_len ? prefix1_len : prefix2_len;
  697. return memcmp (name1, name2, min_prefix_len) == 0;
  698. }
  699. /* Select specialized code to handle various kinds of wildcard
  700. statements. */
  701. static void
  702. analyze_walk_wild_section_handler (lang_wild_statement_type *ptr)
  703. {
  704. int sec_count = 0;
  705. int wild_name_count = 0;
  706. struct wildcard_list *sec;
  707. int signature;
  708. int data_counter;
  709. ptr->walk_wild_section_handler = walk_wild_section_general;
  710. ptr->handler_data[0] = NULL;
  711. ptr->handler_data[1] = NULL;
  712. ptr->handler_data[2] = NULL;
  713. ptr->handler_data[3] = NULL;
  714. ptr->tree = NULL;
  715. /* Count how many wildcard_specs there are, and how many of those
  716. actually use wildcards in the name. Also, bail out if any of the
  717. wildcard names are NULL. (Can this actually happen?
  718. walk_wild_section used to test for it.) And bail out if any
  719. of the wildcards are more complex than a simple string
  720. ending in a single '*'. */
  721. for (sec = ptr->section_list; sec != NULL; sec = sec->next)
  722. {
  723. ++sec_count;
  724. if (sec->spec.name == NULL)
  725. return;
  726. if (wildcardp (sec->spec.name))
  727. {
  728. ++wild_name_count;
  729. if (!is_simple_wild (sec->spec.name))
  730. return;
  731. }
  732. }
  733. /* The zero-spec case would be easy to optimize but it doesn't
  734. happen in practice. Likewise, more than 4 specs doesn't
  735. happen in practice. */
  736. if (sec_count == 0 || sec_count > 4)
  737. return;
  738. /* Check that no two specs can match the same section. */
  739. for (sec = ptr->section_list; sec != NULL; sec = sec->next)
  740. {
  741. struct wildcard_list *sec2;
  742. for (sec2 = sec->next; sec2 != NULL; sec2 = sec2->next)
  743. {
  744. if (wild_spec_can_overlap (sec->spec.name, sec2->spec.name))
  745. return;
  746. }
  747. }
  748. signature = (sec_count << 8) + wild_name_count;
  749. switch (signature)
  750. {
  751. case 0x0100:
  752. ptr->walk_wild_section_handler = walk_wild_section_specs1_wild0;
  753. break;
  754. case 0x0101:
  755. ptr->walk_wild_section_handler = walk_wild_section_specs1_wild1;
  756. break;
  757. case 0x0201:
  758. ptr->walk_wild_section_handler = walk_wild_section_specs2_wild1;
  759. break;
  760. case 0x0302:
  761. ptr->walk_wild_section_handler = walk_wild_section_specs3_wild2;
  762. break;
  763. case 0x0402:
  764. ptr->walk_wild_section_handler = walk_wild_section_specs4_wild2;
  765. break;
  766. default:
  767. return;
  768. }
  769. /* Now fill the data array with pointers to the specs, first the
  770. specs with non-wildcard names, then the specs with wildcard
  771. names. It's OK to process the specs in different order from the
  772. given order, because we've already determined that no section
  773. will match more than one spec. */
  774. data_counter = 0;
  775. for (sec = ptr->section_list; sec != NULL; sec = sec->next)
  776. if (!wildcardp (sec->spec.name))
  777. ptr->handler_data[data_counter++] = sec;
  778. for (sec = ptr->section_list; sec != NULL; sec = sec->next)
  779. if (wildcardp (sec->spec.name))
  780. ptr->handler_data[data_counter++] = sec;
  781. }
  782. /* Handle a wild statement for a single file F. */
  783. static void
  784. walk_wild_file (lang_wild_statement_type *s,
  785. lang_input_statement_type *f,
  786. callback_t callback,
  787. void *data)
  788. {
  789. if (walk_wild_file_in_exclude_list (s->exclude_name_list, f))
  790. return;
  791. if (f->the_bfd == NULL
  792. || !bfd_check_format (f->the_bfd, bfd_archive))
  793. walk_wild_section (s, f, callback, data);
  794. else
  795. {
  796. bfd *member;
  797. /* This is an archive file. We must map each member of the
  798. archive separately. */
  799. member = bfd_openr_next_archived_file (f->the_bfd, NULL);
  800. while (member != NULL)
  801. {
  802. /* When lookup_name is called, it will call the add_symbols
  803. entry point for the archive. For each element of the
  804. archive which is included, BFD will call ldlang_add_file,
  805. which will set the usrdata field of the member to the
  806. lang_input_statement. */
  807. if (bfd_usrdata (member) != NULL)
  808. walk_wild_section (s, bfd_usrdata (member), callback, data);
  809. member = bfd_openr_next_archived_file (f->the_bfd, member);
  810. }
  811. }
  812. }
  813. static void
  814. walk_wild (lang_wild_statement_type *s, callback_t callback, void *data)
  815. {
  816. const char *file_spec = s->filename;
  817. char *p;
  818. if (file_spec == NULL)
  819. {
  820. /* Perform the iteration over all files in the list. */
  821. LANG_FOR_EACH_INPUT_STATEMENT (f)
  822. {
  823. walk_wild_file (s, f, callback, data);
  824. }
  825. }
  826. else if ((p = archive_path (file_spec)) != NULL)
  827. {
  828. LANG_FOR_EACH_INPUT_STATEMENT (f)
  829. {
  830. if (input_statement_is_archive_path (file_spec, p, f))
  831. walk_wild_file (s, f, callback, data);
  832. }
  833. }
  834. else if (wildcardp (file_spec))
  835. {
  836. LANG_FOR_EACH_INPUT_STATEMENT (f)
  837. {
  838. if (fnmatch (file_spec, f->filename, 0) == 0)
  839. walk_wild_file (s, f, callback, data);
  840. }
  841. }
  842. else
  843. {
  844. lang_input_statement_type *f;
  845. /* Perform the iteration over a single file. */
  846. f = lookup_name (file_spec);
  847. if (f)
  848. walk_wild_file (s, f, callback, data);
  849. }
  850. }
  851. /* lang_for_each_statement walks the parse tree and calls the provided
  852. function for each node, except those inside output section statements
  853. with constraint set to -1. */
  854. void
  855. lang_for_each_statement_worker (void (*func) (lang_statement_union_type *),
  856. lang_statement_union_type *s)
  857. {
  858. for (; s != NULL; s = s->header.next)
  859. {
  860. func (s);
  861. switch (s->header.type)
  862. {
  863. case lang_constructors_statement_enum:
  864. lang_for_each_statement_worker (func, constructor_list.head);
  865. break;
  866. case lang_output_section_statement_enum:
  867. if (s->output_section_statement.constraint != -1)
  868. lang_for_each_statement_worker
  869. (func, s->output_section_statement.children.head);
  870. break;
  871. case lang_wild_statement_enum:
  872. lang_for_each_statement_worker (func,
  873. s->wild_statement.children.head);
  874. break;
  875. case lang_group_statement_enum:
  876. lang_for_each_statement_worker (func,
  877. s->group_statement.children.head);
  878. break;
  879. case lang_data_statement_enum:
  880. case lang_reloc_statement_enum:
  881. case lang_object_symbols_statement_enum:
  882. case lang_output_statement_enum:
  883. case lang_target_statement_enum:
  884. case lang_input_section_enum:
  885. case lang_input_statement_enum:
  886. case lang_assignment_statement_enum:
  887. case lang_padding_statement_enum:
  888. case lang_address_statement_enum:
  889. case lang_fill_statement_enum:
  890. case lang_insert_statement_enum:
  891. break;
  892. default:
  893. FAIL ();
  894. break;
  895. }
  896. }
  897. }
  898. void
  899. lang_for_each_statement (void (*func) (lang_statement_union_type *))
  900. {
  901. lang_for_each_statement_worker (func, statement_list.head);
  902. }
  903. /*----------------------------------------------------------------------*/
  904. void
  905. lang_list_init (lang_statement_list_type *list)
  906. {
  907. list->head = NULL;
  908. list->tail = &list->head;
  909. }
  910. static void
  911. lang_statement_append (lang_statement_list_type *list,
  912. void *element,
  913. void *field)
  914. {
  915. *(list->tail) = element;
  916. list->tail = field;
  917. }
  918. void
  919. push_stat_ptr (lang_statement_list_type *new_ptr)
  920. {
  921. if (stat_save_ptr >= stat_save + sizeof (stat_save) / sizeof (stat_save[0]))
  922. abort ();
  923. *stat_save_ptr++ = stat_ptr;
  924. stat_ptr = new_ptr;
  925. }
  926. void
  927. pop_stat_ptr (void)
  928. {
  929. if (stat_save_ptr <= stat_save)
  930. abort ();
  931. stat_ptr = *--stat_save_ptr;
  932. }
  933. /* Build a new statement node for the parse tree. */
  934. static lang_statement_union_type *
  935. new_statement (enum statement_enum type,
  936. size_t size,
  937. lang_statement_list_type *list)
  938. {
  939. lang_statement_union_type *new_stmt;
  940. new_stmt = stat_alloc (size);
  941. new_stmt->header.type = type;
  942. new_stmt->header.next = NULL;
  943. lang_statement_append (list, new_stmt, &new_stmt->header.next);
  944. return new_stmt;
  945. }
  946. /* Build a new input file node for the language. There are several
  947. ways in which we treat an input file, eg, we only look at symbols,
  948. or prefix it with a -l etc.
  949. We can be supplied with requests for input files more than once;
  950. they may, for example be split over several lines like foo.o(.text)
  951. foo.o(.data) etc, so when asked for a file we check that we haven't
  952. got it already so we don't duplicate the bfd. */
  953. static lang_input_statement_type *
  954. new_afile (const char *name,
  955. lang_input_file_enum_type file_type,
  956. const char *target,
  957. const char *from_filename)
  958. {
  959. lang_input_statement_type *p;
  960. lang_has_input_file = true;
  961. p = new_stat (lang_input_statement, stat_ptr);
  962. memset (&p->the_bfd, 0,
  963. sizeof (*p) - offsetof (lang_input_statement_type, the_bfd));
  964. p->extra_search_path = NULL;
  965. p->target = target;
  966. p->flags.dynamic = input_flags.dynamic;
  967. p->flags.add_DT_NEEDED_for_dynamic = input_flags.add_DT_NEEDED_for_dynamic;
  968. p->flags.add_DT_NEEDED_for_regular = input_flags.add_DT_NEEDED_for_regular;
  969. p->flags.whole_archive = input_flags.whole_archive;
  970. p->flags.sysrooted = input_flags.sysrooted;
  971. switch (file_type)
  972. {
  973. case lang_input_file_is_symbols_only_enum:
  974. p->filename = name;
  975. p->local_sym_name = name;
  976. p->flags.real = true;
  977. p->flags.just_syms = true;
  978. break;
  979. case lang_input_file_is_fake_enum:
  980. p->filename = name;
  981. p->local_sym_name = name;
  982. break;
  983. case lang_input_file_is_l_enum:
  984. if (name[0] == ':' && name[1] != '\0')
  985. {
  986. p->filename = name + 1;
  987. p->flags.full_name_provided = true;
  988. }
  989. else
  990. p->filename = name;
  991. p->local_sym_name = concat ("-l", name, (const char *) NULL);
  992. p->flags.maybe_archive = true;
  993. p->flags.real = true;
  994. p->flags.search_dirs = true;
  995. break;
  996. case lang_input_file_is_marker_enum:
  997. p->filename = name;
  998. p->local_sym_name = name;
  999. p->flags.search_dirs = true;
  1000. break;
  1001. case lang_input_file_is_search_file_enum:
  1002. p->filename = name;
  1003. p->local_sym_name = name;
  1004. /* If name is a relative path, search the directory of the current linker
  1005. script first. */
  1006. if (from_filename && !IS_ABSOLUTE_PATH (name))
  1007. p->extra_search_path = ldirname (from_filename);
  1008. p->flags.real = true;
  1009. p->flags.search_dirs = true;
  1010. break;
  1011. case lang_input_file_is_file_enum:
  1012. p->filename = name;
  1013. p->local_sym_name = name;
  1014. p->flags.real = true;
  1015. break;
  1016. default:
  1017. FAIL ();
  1018. }
  1019. lang_statement_append (&input_file_chain, p, &p->next_real_file);
  1020. return p;
  1021. }
  1022. lang_input_statement_type *
  1023. lang_add_input_file (const char *name,
  1024. lang_input_file_enum_type file_type,
  1025. const char *target)
  1026. {
  1027. if (name != NULL
  1028. && (*name == '=' || startswith (name, "$SYSROOT")))
  1029. {
  1030. lang_input_statement_type *ret;
  1031. char *sysrooted_name
  1032. = concat (ld_sysroot,
  1033. name + (*name == '=' ? 1 : strlen ("$SYSROOT")),
  1034. (const char *) NULL);
  1035. /* We've now forcibly prepended the sysroot, making the input
  1036. file independent of the context. Therefore, temporarily
  1037. force a non-sysrooted context for this statement, so it won't
  1038. get the sysroot prepended again when opened. (N.B. if it's a
  1039. script, any child nodes with input files starting with "/"
  1040. will be handled as "sysrooted" as they'll be found to be
  1041. within the sysroot subdirectory.) */
  1042. unsigned int outer_sysrooted = input_flags.sysrooted;
  1043. input_flags.sysrooted = 0;
  1044. ret = new_afile (sysrooted_name, file_type, target, NULL);
  1045. input_flags.sysrooted = outer_sysrooted;
  1046. return ret;
  1047. }
  1048. return new_afile (name, file_type, target, current_input_file);
  1049. }
  1050. struct out_section_hash_entry
  1051. {
  1052. struct bfd_hash_entry root;
  1053. lang_statement_union_type s;
  1054. };
  1055. /* The hash table. */
  1056. static struct bfd_hash_table output_section_statement_table;
  1057. /* Support routines for the hash table used by lang_output_section_find,
  1058. initialize the table, fill in an entry and remove the table. */
  1059. static struct bfd_hash_entry *
  1060. output_section_statement_newfunc (struct bfd_hash_entry *entry,
  1061. struct bfd_hash_table *table,
  1062. const char *string)
  1063. {
  1064. lang_output_section_statement_type **nextp;
  1065. struct out_section_hash_entry *ret;
  1066. if (entry == NULL)
  1067. {
  1068. entry = (struct bfd_hash_entry *) bfd_hash_allocate (table,
  1069. sizeof (*ret));
  1070. if (entry == NULL)
  1071. return entry;
  1072. }
  1073. entry = bfd_hash_newfunc (entry, table, string);
  1074. if (entry == NULL)
  1075. return entry;
  1076. ret = (struct out_section_hash_entry *) entry;
  1077. memset (&ret->s, 0, sizeof (ret->s));
  1078. ret->s.header.type = lang_output_section_statement_enum;
  1079. ret->s.output_section_statement.subsection_alignment = NULL;
  1080. ret->s.output_section_statement.section_alignment = NULL;
  1081. ret->s.output_section_statement.block_value = 1;
  1082. lang_list_init (&ret->s.output_section_statement.children);
  1083. lang_statement_append (stat_ptr, &ret->s, &ret->s.header.next);
  1084. /* For every output section statement added to the list, except the
  1085. first one, lang_os_list.tail points to the "next"
  1086. field of the last element of the list. */
  1087. if (lang_os_list.head != NULL)
  1088. ret->s.output_section_statement.prev
  1089. = ((lang_output_section_statement_type *)
  1090. ((char *) lang_os_list.tail
  1091. - offsetof (lang_output_section_statement_type, next)));
  1092. /* GCC's strict aliasing rules prevent us from just casting the
  1093. address, so we store the pointer in a variable and cast that
  1094. instead. */
  1095. nextp = &ret->s.output_section_statement.next;
  1096. lang_statement_append (&lang_os_list, &ret->s, nextp);
  1097. return &ret->root;
  1098. }
  1099. static void
  1100. output_section_statement_table_init (void)
  1101. {
  1102. if (!bfd_hash_table_init_n (&output_section_statement_table,
  1103. output_section_statement_newfunc,
  1104. sizeof (struct out_section_hash_entry),
  1105. 61))
  1106. einfo (_("%F%P: can not create hash table: %E\n"));
  1107. }
  1108. static void
  1109. output_section_statement_table_free (void)
  1110. {
  1111. bfd_hash_table_free (&output_section_statement_table);
  1112. }
  1113. /* Build enough state so that the parser can build its tree. */
  1114. void
  1115. lang_init (void)
  1116. {
  1117. obstack_begin (&stat_obstack, 1000);
  1118. stat_ptr = &statement_list;
  1119. output_section_statement_table_init ();
  1120. lang_list_init (stat_ptr);
  1121. lang_list_init (&input_file_chain);
  1122. lang_list_init (&lang_os_list);
  1123. lang_list_init (&file_chain);
  1124. first_file = lang_add_input_file (NULL, lang_input_file_is_marker_enum,
  1125. NULL);
  1126. abs_output_section =
  1127. lang_output_section_statement_lookup (BFD_ABS_SECTION_NAME, 0, 1);
  1128. abs_output_section->bfd_section = bfd_abs_section_ptr;
  1129. asneeded_list_head = NULL;
  1130. asneeded_list_tail = &asneeded_list_head;
  1131. }
  1132. void
  1133. lang_finish (void)
  1134. {
  1135. output_section_statement_table_free ();
  1136. }
  1137. /*----------------------------------------------------------------------
  1138. A region is an area of memory declared with the
  1139. MEMORY { name:org=exp, len=exp ... }
  1140. syntax.
  1141. We maintain a list of all the regions here.
  1142. If no regions are specified in the script, then the default is used
  1143. which is created when looked up to be the entire data space.
  1144. If create is true we are creating a region inside a MEMORY block.
  1145. In this case it is probably an error to create a region that has
  1146. already been created. If we are not inside a MEMORY block it is
  1147. dubious to use an undeclared region name (except DEFAULT_MEMORY_REGION)
  1148. and so we issue a warning.
  1149. Each region has at least one name. The first name is either
  1150. DEFAULT_MEMORY_REGION or the name given in the MEMORY block. You can add
  1151. alias names to an existing region within a script with
  1152. REGION_ALIAS (alias, region_name). Each name corresponds to at most one
  1153. region. */
  1154. static lang_memory_region_type *lang_memory_region_list;
  1155. static lang_memory_region_type **lang_memory_region_list_tail
  1156. = &lang_memory_region_list;
  1157. lang_memory_region_type *
  1158. lang_memory_region_lookup (const char *const name, bool create)
  1159. {
  1160. lang_memory_region_name *n;
  1161. lang_memory_region_type *r;
  1162. lang_memory_region_type *new_region;
  1163. /* NAME is NULL for LMA memspecs if no region was specified. */
  1164. if (name == NULL)
  1165. return NULL;
  1166. for (r = lang_memory_region_list; r != NULL; r = r->next)
  1167. for (n = &r->name_list; n != NULL; n = n->next)
  1168. if (strcmp (n->name, name) == 0)
  1169. {
  1170. if (create)
  1171. einfo (_("%P:%pS: warning: redeclaration of memory region `%s'\n"),
  1172. NULL, name);
  1173. return r;
  1174. }
  1175. if (!create && strcmp (name, DEFAULT_MEMORY_REGION))
  1176. einfo (_("%P:%pS: warning: memory region `%s' not declared\n"),
  1177. NULL, name);
  1178. new_region = stat_alloc (sizeof (lang_memory_region_type));
  1179. new_region->name_list.name = xstrdup (name);
  1180. new_region->name_list.next = NULL;
  1181. new_region->next = NULL;
  1182. new_region->origin_exp = NULL;
  1183. new_region->origin = 0;
  1184. new_region->length_exp = NULL;
  1185. new_region->length = ~(bfd_size_type) 0;
  1186. new_region->current = 0;
  1187. new_region->last_os = NULL;
  1188. new_region->flags = 0;
  1189. new_region->not_flags = 0;
  1190. new_region->had_full_message = false;
  1191. *lang_memory_region_list_tail = new_region;
  1192. lang_memory_region_list_tail = &new_region->next;
  1193. return new_region;
  1194. }
  1195. void
  1196. lang_memory_region_alias (const char *alias, const char *region_name)
  1197. {
  1198. lang_memory_region_name *n;
  1199. lang_memory_region_type *r;
  1200. lang_memory_region_type *region;
  1201. /* The default region must be unique. This ensures that it is not necessary
  1202. to iterate through the name list if someone wants the check if a region is
  1203. the default memory region. */
  1204. if (strcmp (region_name, DEFAULT_MEMORY_REGION) == 0
  1205. || strcmp (alias, DEFAULT_MEMORY_REGION) == 0)
  1206. einfo (_("%F%P:%pS: error: alias for default memory region\n"), NULL);
  1207. /* Look for the target region and check if the alias is not already
  1208. in use. */
  1209. region = NULL;
  1210. for (r = lang_memory_region_list; r != NULL; r = r->next)
  1211. for (n = &r->name_list; n != NULL; n = n->next)
  1212. {
  1213. if (region == NULL && strcmp (n->name, region_name) == 0)
  1214. region = r;
  1215. if (strcmp (n->name, alias) == 0)
  1216. einfo (_("%F%P:%pS: error: redefinition of memory region "
  1217. "alias `%s'\n"),
  1218. NULL, alias);
  1219. }
  1220. /* Check if the target region exists. */
  1221. if (region == NULL)
  1222. einfo (_("%F%P:%pS: error: memory region `%s' "
  1223. "for alias `%s' does not exist\n"),
  1224. NULL, region_name, alias);
  1225. /* Add alias to region name list. */
  1226. n = stat_alloc (sizeof (lang_memory_region_name));
  1227. n->name = xstrdup (alias);
  1228. n->next = region->name_list.next;
  1229. region->name_list.next = n;
  1230. }
  1231. static lang_memory_region_type *
  1232. lang_memory_default (asection *section)
  1233. {
  1234. lang_memory_region_type *p;
  1235. flagword sec_flags = section->flags;
  1236. /* Override SEC_DATA to mean a writable section. */
  1237. if ((sec_flags & (SEC_ALLOC | SEC_READONLY | SEC_CODE)) == SEC_ALLOC)
  1238. sec_flags |= SEC_DATA;
  1239. for (p = lang_memory_region_list; p != NULL; p = p->next)
  1240. {
  1241. if ((p->flags & sec_flags) != 0
  1242. && (p->not_flags & sec_flags) == 0)
  1243. {
  1244. return p;
  1245. }
  1246. }
  1247. return lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false);
  1248. }
  1249. /* Get the output section statement directly from the userdata. */
  1250. lang_output_section_statement_type *
  1251. lang_output_section_get (const asection *output_section)
  1252. {
  1253. return bfd_section_userdata (output_section);
  1254. }
  1255. /* Find or create an output_section_statement with the given NAME.
  1256. If CONSTRAINT is non-zero match one with that constraint, otherwise
  1257. match any non-negative constraint. If CREATE is 0 return NULL when
  1258. no match exists. If CREATE is 1, create an output_section_statement
  1259. when no match exists or if CONSTRAINT is SPECIAL. If CREATE is 2,
  1260. always make a new output_section_statement. */
  1261. lang_output_section_statement_type *
  1262. lang_output_section_statement_lookup (const char *name,
  1263. int constraint,
  1264. int create)
  1265. {
  1266. struct out_section_hash_entry *entry;
  1267. entry = ((struct out_section_hash_entry *)
  1268. bfd_hash_lookup (&output_section_statement_table, name,
  1269. create != 0, false));
  1270. if (entry == NULL)
  1271. {
  1272. if (create)
  1273. einfo (_("%F%P: failed creating section `%s': %E\n"), name);
  1274. return NULL;
  1275. }
  1276. if (entry->s.output_section_statement.name != NULL)
  1277. {
  1278. /* We have a section of this name, but it might not have the correct
  1279. constraint. */
  1280. struct out_section_hash_entry *last_ent;
  1281. name = entry->s.output_section_statement.name;
  1282. do
  1283. {
  1284. if (create != 2
  1285. && !(create && constraint == SPECIAL)
  1286. && (constraint == entry->s.output_section_statement.constraint
  1287. || (constraint == 0
  1288. && entry->s.output_section_statement.constraint >= 0)))
  1289. return &entry->s.output_section_statement;
  1290. last_ent = entry;
  1291. entry = (struct out_section_hash_entry *) entry->root.next;
  1292. }
  1293. while (entry != NULL
  1294. && name == entry->s.output_section_statement.name);
  1295. if (!create)
  1296. return NULL;
  1297. entry
  1298. = ((struct out_section_hash_entry *)
  1299. output_section_statement_newfunc (NULL,
  1300. &output_section_statement_table,
  1301. name));
  1302. if (entry == NULL)
  1303. {
  1304. einfo (_("%F%P: failed creating section `%s': %E\n"), name);
  1305. return NULL;
  1306. }
  1307. entry->root = last_ent->root;
  1308. last_ent->root.next = &entry->root;
  1309. }
  1310. entry->s.output_section_statement.name = name;
  1311. entry->s.output_section_statement.constraint = constraint;
  1312. entry->s.output_section_statement.dup_output = (create == 2
  1313. || constraint == SPECIAL);
  1314. return &entry->s.output_section_statement;
  1315. }
  1316. /* Find the next output_section_statement with the same name as OS.
  1317. If CONSTRAINT is non-zero, find one with that constraint otherwise
  1318. match any non-negative constraint. */
  1319. lang_output_section_statement_type *
  1320. next_matching_output_section_statement (lang_output_section_statement_type *os,
  1321. int constraint)
  1322. {
  1323. /* All output_section_statements are actually part of a
  1324. struct out_section_hash_entry. */
  1325. struct out_section_hash_entry *entry = (struct out_section_hash_entry *)
  1326. ((char *) os
  1327. - offsetof (struct out_section_hash_entry, s.output_section_statement));
  1328. const char *name = os->name;
  1329. ASSERT (name == entry->root.string);
  1330. do
  1331. {
  1332. entry = (struct out_section_hash_entry *) entry->root.next;
  1333. if (entry == NULL
  1334. || name != entry->s.output_section_statement.name)
  1335. return NULL;
  1336. }
  1337. while (constraint != entry->s.output_section_statement.constraint
  1338. && (constraint != 0
  1339. || entry->s.output_section_statement.constraint < 0));
  1340. return &entry->s.output_section_statement;
  1341. }
  1342. /* A variant of lang_output_section_find used by place_orphan.
  1343. Returns the output statement that should precede a new output
  1344. statement for SEC. If an exact match is found on certain flags,
  1345. sets *EXACT too. */
  1346. lang_output_section_statement_type *
  1347. lang_output_section_find_by_flags (const asection *sec,
  1348. flagword sec_flags,
  1349. lang_output_section_statement_type **exact,
  1350. lang_match_sec_type_func match_type)
  1351. {
  1352. lang_output_section_statement_type *first, *look, *found;
  1353. flagword look_flags, differ;
  1354. /* We know the first statement on this list is *ABS*. May as well
  1355. skip it. */
  1356. first = (void *) lang_os_list.head;
  1357. first = first->next;
  1358. /* First try for an exact match. */
  1359. found = NULL;
  1360. for (look = first; look; look = look->next)
  1361. {
  1362. look_flags = look->flags;
  1363. if (look->bfd_section != NULL)
  1364. {
  1365. look_flags = look->bfd_section->flags;
  1366. if (match_type && !match_type (link_info.output_bfd,
  1367. look->bfd_section,
  1368. sec->owner, sec))
  1369. continue;
  1370. }
  1371. differ = look_flags ^ sec_flags;
  1372. if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY
  1373. | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
  1374. found = look;
  1375. }
  1376. if (found != NULL)
  1377. {
  1378. if (exact != NULL)
  1379. *exact = found;
  1380. return found;
  1381. }
  1382. if ((sec_flags & SEC_CODE) != 0
  1383. && (sec_flags & SEC_ALLOC) != 0)
  1384. {
  1385. /* Try for a rw code section. */
  1386. for (look = first; look; look = look->next)
  1387. {
  1388. look_flags = look->flags;
  1389. if (look->bfd_section != NULL)
  1390. {
  1391. look_flags = look->bfd_section->flags;
  1392. if (match_type && !match_type (link_info.output_bfd,
  1393. look->bfd_section,
  1394. sec->owner, sec))
  1395. continue;
  1396. }
  1397. differ = look_flags ^ sec_flags;
  1398. if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
  1399. | SEC_CODE | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
  1400. found = look;
  1401. }
  1402. }
  1403. else if ((sec_flags & SEC_READONLY) != 0
  1404. && (sec_flags & SEC_ALLOC) != 0)
  1405. {
  1406. /* .rodata can go after .text, .sdata2 after .rodata. */
  1407. for (look = first; look; look = look->next)
  1408. {
  1409. look_flags = look->flags;
  1410. if (look->bfd_section != NULL)
  1411. {
  1412. look_flags = look->bfd_section->flags;
  1413. if (match_type && !match_type (link_info.output_bfd,
  1414. look->bfd_section,
  1415. sec->owner, sec))
  1416. continue;
  1417. }
  1418. differ = look_flags ^ sec_flags;
  1419. if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
  1420. | SEC_READONLY | SEC_SMALL_DATA))
  1421. || (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
  1422. | SEC_READONLY))
  1423. && !(look_flags & SEC_SMALL_DATA)))
  1424. found = look;
  1425. }
  1426. }
  1427. else if ((sec_flags & SEC_THREAD_LOCAL) != 0
  1428. && (sec_flags & SEC_ALLOC) != 0)
  1429. {
  1430. /* .tdata can go after .data, .tbss after .tdata. Treat .tbss
  1431. as if it were a loaded section, and don't use match_type. */
  1432. bool seen_thread_local = false;
  1433. match_type = NULL;
  1434. for (look = first; look; look = look->next)
  1435. {
  1436. look_flags = look->flags;
  1437. if (look->bfd_section != NULL)
  1438. look_flags = look->bfd_section->flags;
  1439. differ = look_flags ^ (sec_flags | SEC_LOAD | SEC_HAS_CONTENTS);
  1440. if (!(differ & (SEC_THREAD_LOCAL | SEC_ALLOC)))
  1441. {
  1442. /* .tdata and .tbss must be adjacent and in that order. */
  1443. if (!(look_flags & SEC_LOAD)
  1444. && (sec_flags & SEC_LOAD))
  1445. /* ..so if we're at a .tbss section and we're placing
  1446. a .tdata section stop looking and return the
  1447. previous section. */
  1448. break;
  1449. found = look;
  1450. seen_thread_local = true;
  1451. }
  1452. else if (seen_thread_local)
  1453. break;
  1454. else if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD)))
  1455. found = look;
  1456. }
  1457. }
  1458. else if ((sec_flags & SEC_SMALL_DATA) != 0
  1459. && (sec_flags & SEC_ALLOC) != 0)
  1460. {
  1461. /* .sdata goes after .data, .sbss after .sdata. */
  1462. for (look = first; look; look = look->next)
  1463. {
  1464. look_flags = look->flags;
  1465. if (look->bfd_section != NULL)
  1466. {
  1467. look_flags = look->bfd_section->flags;
  1468. if (match_type && !match_type (link_info.output_bfd,
  1469. look->bfd_section,
  1470. sec->owner, sec))
  1471. continue;
  1472. }
  1473. differ = look_flags ^ sec_flags;
  1474. if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
  1475. | SEC_THREAD_LOCAL))
  1476. || ((look_flags & SEC_SMALL_DATA)
  1477. && !(sec_flags & SEC_HAS_CONTENTS)))
  1478. found = look;
  1479. }
  1480. }
  1481. else if ((sec_flags & SEC_HAS_CONTENTS) != 0
  1482. && (sec_flags & SEC_ALLOC) != 0)
  1483. {
  1484. /* .data goes after .rodata. */
  1485. for (look = first; look; look = look->next)
  1486. {
  1487. look_flags = look->flags;
  1488. if (look->bfd_section != NULL)
  1489. {
  1490. look_flags = look->bfd_section->flags;
  1491. if (match_type && !match_type (link_info.output_bfd,
  1492. look->bfd_section,
  1493. sec->owner, sec))
  1494. continue;
  1495. }
  1496. differ = look_flags ^ sec_flags;
  1497. if (!(differ & (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
  1498. | SEC_SMALL_DATA | SEC_THREAD_LOCAL)))
  1499. found = look;
  1500. }
  1501. }
  1502. else if ((sec_flags & SEC_ALLOC) != 0)
  1503. {
  1504. /* .bss goes after any other alloc section. */
  1505. for (look = first; look; look = look->next)
  1506. {
  1507. look_flags = look->flags;
  1508. if (look->bfd_section != NULL)
  1509. {
  1510. look_flags = look->bfd_section->flags;
  1511. if (match_type && !match_type (link_info.output_bfd,
  1512. look->bfd_section,
  1513. sec->owner, sec))
  1514. continue;
  1515. }
  1516. differ = look_flags ^ sec_flags;
  1517. if (!(differ & SEC_ALLOC))
  1518. found = look;
  1519. }
  1520. }
  1521. else
  1522. {
  1523. /* non-alloc go last. */
  1524. for (look = first; look; look = look->next)
  1525. {
  1526. look_flags = look->flags;
  1527. if (look->bfd_section != NULL)
  1528. look_flags = look->bfd_section->flags;
  1529. differ = look_flags ^ sec_flags;
  1530. if (!(differ & SEC_DEBUGGING))
  1531. found = look;
  1532. }
  1533. return found;
  1534. }
  1535. if (found || !match_type)
  1536. return found;
  1537. return lang_output_section_find_by_flags (sec, sec_flags, NULL, NULL);
  1538. }
  1539. /* Find the last output section before given output statement.
  1540. Used by place_orphan. */
  1541. static asection *
  1542. output_prev_sec_find (lang_output_section_statement_type *os)
  1543. {
  1544. lang_output_section_statement_type *lookup;
  1545. for (lookup = os->prev; lookup != NULL; lookup = lookup->prev)
  1546. {
  1547. if (lookup->constraint < 0)
  1548. continue;
  1549. if (lookup->bfd_section != NULL && lookup->bfd_section->owner != NULL)
  1550. return lookup->bfd_section;
  1551. }
  1552. return NULL;
  1553. }
  1554. /* Look for a suitable place for a new output section statement. The
  1555. idea is to skip over anything that might be inside a SECTIONS {}
  1556. statement in a script, before we find another output section
  1557. statement. Assignments to "dot" before an output section statement
  1558. are assumed to belong to it, except in two cases; The first
  1559. assignment to dot, and assignments before non-alloc sections.
  1560. Otherwise we might put an orphan before . = . + SIZEOF_HEADERS or
  1561. similar assignments that set the initial address, or we might
  1562. insert non-alloc note sections among assignments setting end of
  1563. image symbols. */
  1564. static lang_statement_union_type **
  1565. insert_os_after (lang_output_section_statement_type *after)
  1566. {
  1567. lang_statement_union_type **where;
  1568. lang_statement_union_type **assign = NULL;
  1569. bool ignore_first;
  1570. ignore_first = after == (void *) lang_os_list.head;
  1571. for (where = &after->header.next;
  1572. *where != NULL;
  1573. where = &(*where)->header.next)
  1574. {
  1575. switch ((*where)->header.type)
  1576. {
  1577. case lang_assignment_statement_enum:
  1578. if (assign == NULL)
  1579. {
  1580. lang_assignment_statement_type *ass;
  1581. ass = &(*where)->assignment_statement;
  1582. if (ass->exp->type.node_class != etree_assert
  1583. && ass->exp->assign.dst[0] == '.'
  1584. && ass->exp->assign.dst[1] == 0)
  1585. {
  1586. if (!ignore_first)
  1587. assign = where;
  1588. ignore_first = false;
  1589. }
  1590. }
  1591. continue;
  1592. case lang_wild_statement_enum:
  1593. case lang_input_section_enum:
  1594. case lang_object_symbols_statement_enum:
  1595. case lang_fill_statement_enum:
  1596. case lang_data_statement_enum:
  1597. case lang_reloc_statement_enum:
  1598. case lang_padding_statement_enum:
  1599. case lang_constructors_statement_enum:
  1600. assign = NULL;
  1601. ignore_first = false;
  1602. continue;
  1603. case lang_output_section_statement_enum:
  1604. if (assign != NULL)
  1605. {
  1606. asection *s = (*where)->output_section_statement.bfd_section;
  1607. if (s == NULL
  1608. || s->map_head.s == NULL
  1609. || (s->flags & SEC_ALLOC) != 0)
  1610. where = assign;
  1611. }
  1612. break;
  1613. case lang_input_statement_enum:
  1614. case lang_address_statement_enum:
  1615. case lang_target_statement_enum:
  1616. case lang_output_statement_enum:
  1617. case lang_group_statement_enum:
  1618. case lang_insert_statement_enum:
  1619. continue;
  1620. }
  1621. break;
  1622. }
  1623. return where;
  1624. }
  1625. lang_output_section_statement_type *
  1626. lang_insert_orphan (asection *s,
  1627. const char *secname,
  1628. int constraint,
  1629. lang_output_section_statement_type *after,
  1630. struct orphan_save *place,
  1631. etree_type *address,
  1632. lang_statement_list_type *add_child)
  1633. {
  1634. lang_statement_list_type add;
  1635. lang_output_section_statement_type *os;
  1636. lang_output_section_statement_type **os_tail;
  1637. /* If we have found an appropriate place for the output section
  1638. statements for this orphan, add them to our own private list,
  1639. inserting them later into the global statement list. */
  1640. if (after != NULL)
  1641. {
  1642. lang_list_init (&add);
  1643. push_stat_ptr (&add);
  1644. }
  1645. if (bfd_link_relocatable (&link_info)
  1646. || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0)
  1647. address = exp_intop (0);
  1648. os_tail = (lang_output_section_statement_type **) lang_os_list.tail;
  1649. os = lang_enter_output_section_statement (
  1650. secname, address, normal_section, 0, NULL, NULL, NULL, constraint, 0);
  1651. if (add_child == NULL)
  1652. add_child = &os->children;
  1653. lang_add_section (add_child, s, NULL, NULL, os);
  1654. if (after && (s->flags & (SEC_LOAD | SEC_ALLOC)) != 0)
  1655. {
  1656. const char *region = (after->region
  1657. ? after->region->name_list.name
  1658. : DEFAULT_MEMORY_REGION);
  1659. const char *lma_region = (after->lma_region
  1660. ? after->lma_region->name_list.name
  1661. : NULL);
  1662. lang_leave_output_section_statement (NULL, region, after->phdrs,
  1663. lma_region);
  1664. }
  1665. else
  1666. lang_leave_output_section_statement (NULL, DEFAULT_MEMORY_REGION, NULL,
  1667. NULL);
  1668. /* Restore the global list pointer. */
  1669. if (after != NULL)
  1670. pop_stat_ptr ();
  1671. if (after != NULL && os->bfd_section != NULL)
  1672. {
  1673. asection *snew, *as;
  1674. bool place_after = place->stmt == NULL;
  1675. bool insert_after = true;
  1676. snew = os->bfd_section;
  1677. /* Shuffle the bfd section list to make the output file look
  1678. neater. This is really only cosmetic. */
  1679. if (place->section == NULL
  1680. && after != (void *) lang_os_list.head)
  1681. {
  1682. asection *bfd_section = after->bfd_section;
  1683. /* If the output statement hasn't been used to place any input
  1684. sections (and thus doesn't have an output bfd_section),
  1685. look for the closest prior output statement having an
  1686. output section. */
  1687. if (bfd_section == NULL)
  1688. bfd_section = output_prev_sec_find (after);
  1689. if (bfd_section != NULL && bfd_section != snew)
  1690. place->section = &bfd_section->next;
  1691. }
  1692. if (place->section == NULL)
  1693. place->section = &link_info.output_bfd->sections;
  1694. as = *place->section;
  1695. if (!as)
  1696. {
  1697. /* Put the section at the end of the list. */
  1698. /* Unlink the section. */
  1699. bfd_section_list_remove (link_info.output_bfd, snew);
  1700. /* Now tack it back on in the right place. */
  1701. bfd_section_list_append (link_info.output_bfd, snew);
  1702. }
  1703. else if ((bfd_get_flavour (link_info.output_bfd)
  1704. == bfd_target_elf_flavour)
  1705. && (bfd_get_flavour (s->owner)
  1706. == bfd_target_elf_flavour)
  1707. && ((elf_section_type (s) == SHT_NOTE
  1708. && (s->flags & SEC_LOAD) != 0)
  1709. || (elf_section_type (as) == SHT_NOTE
  1710. && (as->flags & SEC_LOAD) != 0)))
  1711. {
  1712. /* Make sure that output note sections are grouped and sorted
  1713. by alignments when inserting a note section or insert a
  1714. section after a note section, */
  1715. asection *sec;
  1716. /* A specific section after which the output note section
  1717. should be placed. */
  1718. asection *after_sec;
  1719. /* True if we need to insert the orphan section after a
  1720. specific section to maintain output note section order. */
  1721. bool after_sec_note = false;
  1722. static asection *first_orphan_note = NULL;
  1723. /* Group and sort output note section by alignments in
  1724. ascending order. */
  1725. after_sec = NULL;
  1726. if (elf_section_type (s) == SHT_NOTE
  1727. && (s->flags & SEC_LOAD) != 0)
  1728. {
  1729. /* Search from the beginning for the last output note
  1730. section with equal or larger alignments. NB: Don't
  1731. place orphan note section after non-note sections. */
  1732. first_orphan_note = NULL;
  1733. for (sec = link_info.output_bfd->sections;
  1734. (sec != NULL
  1735. && !bfd_is_abs_section (sec));
  1736. sec = sec->next)
  1737. if (sec != snew
  1738. && elf_section_type (sec) == SHT_NOTE
  1739. && (sec->flags & SEC_LOAD) != 0)
  1740. {
  1741. if (!first_orphan_note)
  1742. first_orphan_note = sec;
  1743. if (sec->alignment_power >= s->alignment_power)
  1744. after_sec = sec;
  1745. }
  1746. else if (first_orphan_note)
  1747. {
  1748. /* Stop if there is non-note section after the first
  1749. orphan note section. */
  1750. break;
  1751. }
  1752. /* If this will be the first orphan note section, it can
  1753. be placed at the default location. */
  1754. after_sec_note = first_orphan_note != NULL;
  1755. if (after_sec == NULL && after_sec_note)
  1756. {
  1757. /* If all output note sections have smaller
  1758. alignments, place the section before all
  1759. output orphan note sections. */
  1760. after_sec = first_orphan_note;
  1761. insert_after = false;
  1762. }
  1763. }
  1764. else if (first_orphan_note)
  1765. {
  1766. /* Don't place non-note sections in the middle of orphan
  1767. note sections. */
  1768. after_sec_note = true;
  1769. after_sec = as;
  1770. for (sec = as->next;
  1771. (sec != NULL
  1772. && !bfd_is_abs_section (sec));
  1773. sec = sec->next)
  1774. if (elf_section_type (sec) == SHT_NOTE
  1775. && (sec->flags & SEC_LOAD) != 0)
  1776. after_sec = sec;
  1777. }
  1778. if (after_sec_note)
  1779. {
  1780. if (after_sec)
  1781. {
  1782. /* Search forward to insert OS after AFTER_SEC output
  1783. statement. */
  1784. lang_output_section_statement_type *stmt, *next;
  1785. bool found = false;
  1786. for (stmt = after; stmt != NULL; stmt = next)
  1787. {
  1788. next = stmt->next;
  1789. if (insert_after)
  1790. {
  1791. if (stmt->bfd_section == after_sec)
  1792. {
  1793. place_after = true;
  1794. found = true;
  1795. after = stmt;
  1796. break;
  1797. }
  1798. }
  1799. else
  1800. {
  1801. /* If INSERT_AFTER is FALSE, place OS before
  1802. AFTER_SEC output statement. */
  1803. if (next && next->bfd_section == after_sec)
  1804. {
  1805. place_after = true;
  1806. found = true;
  1807. after = stmt;
  1808. break;
  1809. }
  1810. }
  1811. }
  1812. /* Search backward to insert OS after AFTER_SEC output
  1813. statement. */
  1814. if (!found)
  1815. for (stmt = after; stmt != NULL; stmt = stmt->prev)
  1816. {
  1817. if (insert_after)
  1818. {
  1819. if (stmt->bfd_section == after_sec)
  1820. {
  1821. place_after = true;
  1822. after = stmt;
  1823. break;
  1824. }
  1825. }
  1826. else
  1827. {
  1828. /* If INSERT_AFTER is FALSE, place OS before
  1829. AFTER_SEC output statement. */
  1830. if (stmt->next->bfd_section == after_sec)
  1831. {
  1832. place_after = true;
  1833. after = stmt;
  1834. break;
  1835. }
  1836. }
  1837. }
  1838. }
  1839. if (after_sec == NULL
  1840. || (insert_after && after_sec->next != snew)
  1841. || (!insert_after && after_sec->prev != snew))
  1842. {
  1843. /* Unlink the section. */
  1844. bfd_section_list_remove (link_info.output_bfd, snew);
  1845. /* Place SNEW after AFTER_SEC. If AFTER_SEC is NULL,
  1846. prepend SNEW. */
  1847. if (after_sec)
  1848. {
  1849. if (insert_after)
  1850. bfd_section_list_insert_after (link_info.output_bfd,
  1851. after_sec, snew);
  1852. else
  1853. bfd_section_list_insert_before (link_info.output_bfd,
  1854. after_sec, snew);
  1855. }
  1856. else
  1857. bfd_section_list_prepend (link_info.output_bfd, snew);
  1858. }
  1859. }
  1860. else if (as != snew && as->prev != snew)
  1861. {
  1862. /* Unlink the section. */
  1863. bfd_section_list_remove (link_info.output_bfd, snew);
  1864. /* Now tack it back on in the right place. */
  1865. bfd_section_list_insert_before (link_info.output_bfd,
  1866. as, snew);
  1867. }
  1868. }
  1869. else if (as != snew && as->prev != snew)
  1870. {
  1871. /* Unlink the section. */
  1872. bfd_section_list_remove (link_info.output_bfd, snew);
  1873. /* Now tack it back on in the right place. */
  1874. bfd_section_list_insert_before (link_info.output_bfd, as, snew);
  1875. }
  1876. /* Save the end of this list. Further ophans of this type will
  1877. follow the one we've just added. */
  1878. place->section = &snew->next;
  1879. /* The following is non-cosmetic. We try to put the output
  1880. statements in some sort of reasonable order here, because they
  1881. determine the final load addresses of the orphan sections.
  1882. In addition, placing output statements in the wrong order may
  1883. require extra segments. For instance, given a typical
  1884. situation of all read-only sections placed in one segment and
  1885. following that a segment containing all the read-write
  1886. sections, we wouldn't want to place an orphan read/write
  1887. section before or amongst the read-only ones. */
  1888. if (add.head != NULL)
  1889. {
  1890. lang_output_section_statement_type *newly_added_os;
  1891. /* Place OS after AFTER if AFTER_NOTE is TRUE. */
  1892. if (place_after)
  1893. {
  1894. lang_statement_union_type **where = insert_os_after (after);
  1895. *add.tail = *where;
  1896. *where = add.head;
  1897. place->os_tail = &after->next;
  1898. }
  1899. else
  1900. {
  1901. /* Put it after the last orphan statement we added. */
  1902. *add.tail = *place->stmt;
  1903. *place->stmt = add.head;
  1904. }
  1905. /* Fix the global list pointer if we happened to tack our
  1906. new list at the tail. */
  1907. if (*stat_ptr->tail == add.head)
  1908. stat_ptr->tail = add.tail;
  1909. /* Save the end of this list. */
  1910. place->stmt = add.tail;
  1911. /* Do the same for the list of output section statements. */
  1912. newly_added_os = *os_tail;
  1913. *os_tail = NULL;
  1914. newly_added_os->prev = (lang_output_section_statement_type *)
  1915. ((char *) place->os_tail
  1916. - offsetof (lang_output_section_statement_type, next));
  1917. newly_added_os->next = *place->os_tail;
  1918. if (newly_added_os->next != NULL)
  1919. newly_added_os->next->prev = newly_added_os;
  1920. *place->os_tail = newly_added_os;
  1921. place->os_tail = &newly_added_os->next;
  1922. /* Fixing the global list pointer here is a little different.
  1923. We added to the list in lang_enter_output_section_statement,
  1924. trimmed off the new output_section_statment above when
  1925. assigning *os_tail = NULL, but possibly added it back in
  1926. the same place when assigning *place->os_tail. */
  1927. if (*os_tail == NULL)
  1928. lang_os_list.tail = (lang_statement_union_type **) os_tail;
  1929. }
  1930. }
  1931. return os;
  1932. }
  1933. static void
  1934. lang_print_asneeded (void)
  1935. {
  1936. struct asneeded_minfo *m;
  1937. if (asneeded_list_head == NULL)
  1938. return;
  1939. minfo (_("\nAs-needed library included to satisfy reference by file (symbol)\n\n"));
  1940. for (m = asneeded_list_head; m != NULL; m = m->next)
  1941. {
  1942. size_t len;
  1943. minfo ("%s", m->soname);
  1944. len = strlen (m->soname);
  1945. if (len >= 29)
  1946. {
  1947. print_nl ();
  1948. len = 0;
  1949. }
  1950. while (len < 30)
  1951. {
  1952. print_space ();
  1953. ++len;
  1954. }
  1955. if (m->ref != NULL)
  1956. minfo ("%pB ", m->ref);
  1957. minfo ("(%pT)\n", m->name);
  1958. }
  1959. }
  1960. static void
  1961. lang_map_flags (flagword flag)
  1962. {
  1963. if (flag & SEC_ALLOC)
  1964. minfo ("a");
  1965. if (flag & SEC_CODE)
  1966. minfo ("x");
  1967. if (flag & SEC_READONLY)
  1968. minfo ("r");
  1969. if (flag & SEC_DATA)
  1970. minfo ("w");
  1971. if (flag & SEC_LOAD)
  1972. minfo ("l");
  1973. }
  1974. void
  1975. lang_map (void)
  1976. {
  1977. lang_memory_region_type *m;
  1978. bool dis_header_printed = false;
  1979. LANG_FOR_EACH_INPUT_STATEMENT (file)
  1980. {
  1981. asection *s;
  1982. if ((file->the_bfd->flags & (BFD_LINKER_CREATED | DYNAMIC)) != 0
  1983. || file->flags.just_syms)
  1984. continue;
  1985. if (config.print_map_discarded)
  1986. for (s = file->the_bfd->sections; s != NULL; s = s->next)
  1987. if ((s->output_section == NULL
  1988. || s->output_section->owner != link_info.output_bfd)
  1989. && (s->flags & (SEC_LINKER_CREATED | SEC_KEEP)) == 0)
  1990. {
  1991. if (! dis_header_printed)
  1992. {
  1993. fprintf (config.map_file, _("\nDiscarded input sections\n\n"));
  1994. dis_header_printed = true;
  1995. }
  1996. print_input_section (s, true);
  1997. }
  1998. }
  1999. minfo (_("\nMemory Configuration\n\n"));
  2000. fprintf (config.map_file, "%-16s %-18s %-18s %s\n",
  2001. _("Name"), _("Origin"), _("Length"), _("Attributes"));
  2002. for (m = lang_memory_region_list; m != NULL; m = m->next)
  2003. {
  2004. char buf[100];
  2005. int len;
  2006. fprintf (config.map_file, "%-16s ", m->name_list.name);
  2007. sprintf_vma (buf, m->origin);
  2008. minfo ("0x%s ", buf);
  2009. len = strlen (buf);
  2010. while (len < 16)
  2011. {
  2012. print_space ();
  2013. ++len;
  2014. }
  2015. minfo ("0x%V", m->length);
  2016. if (m->flags || m->not_flags)
  2017. {
  2018. #ifndef BFD64
  2019. minfo (" ");
  2020. #endif
  2021. if (m->flags)
  2022. {
  2023. print_space ();
  2024. lang_map_flags (m->flags);
  2025. }
  2026. if (m->not_flags)
  2027. {
  2028. minfo (" !");
  2029. lang_map_flags (m->not_flags);
  2030. }
  2031. }
  2032. print_nl ();
  2033. }
  2034. fprintf (config.map_file, _("\nLinker script and memory map\n\n"));
  2035. if (!link_info.reduce_memory_overheads)
  2036. {
  2037. obstack_begin (&map_obstack, 1000);
  2038. bfd_link_hash_traverse (link_info.hash, sort_def_symbol, 0);
  2039. }
  2040. expld.phase = lang_fixed_phase_enum;
  2041. lang_statement_iteration++;
  2042. print_statements ();
  2043. ldemul_extra_map_file_text (link_info.output_bfd, &link_info,
  2044. config.map_file);
  2045. }
  2046. static bool
  2047. sort_def_symbol (struct bfd_link_hash_entry *hash_entry,
  2048. void *info ATTRIBUTE_UNUSED)
  2049. {
  2050. if ((hash_entry->type == bfd_link_hash_defined
  2051. || hash_entry->type == bfd_link_hash_defweak)
  2052. && hash_entry->u.def.section->owner != link_info.output_bfd
  2053. && hash_entry->u.def.section->owner != NULL)
  2054. {
  2055. input_section_userdata_type *ud;
  2056. struct map_symbol_def *def;
  2057. ud = bfd_section_userdata (hash_entry->u.def.section);
  2058. if (!ud)
  2059. {
  2060. ud = stat_alloc (sizeof (*ud));
  2061. bfd_set_section_userdata (hash_entry->u.def.section, ud);
  2062. ud->map_symbol_def_tail = &ud->map_symbol_def_head;
  2063. ud->map_symbol_def_count = 0;
  2064. }
  2065. else if (!ud->map_symbol_def_tail)
  2066. ud->map_symbol_def_tail = &ud->map_symbol_def_head;
  2067. def = (struct map_symbol_def *) obstack_alloc (&map_obstack, sizeof *def);
  2068. def->entry = hash_entry;
  2069. *(ud->map_symbol_def_tail) = def;
  2070. ud->map_symbol_def_tail = &def->next;
  2071. ud->map_symbol_def_count++;
  2072. }
  2073. return true;
  2074. }
  2075. /* Initialize an output section. */
  2076. static void
  2077. init_os (lang_output_section_statement_type *s, flagword flags)
  2078. {
  2079. if (strcmp (s->name, DISCARD_SECTION_NAME) == 0)
  2080. einfo (_("%F%P: illegal use of `%s' section\n"), DISCARD_SECTION_NAME);
  2081. if (!s->dup_output)
  2082. s->bfd_section = bfd_get_section_by_name (link_info.output_bfd, s->name);
  2083. if (s->bfd_section == NULL)
  2084. s->bfd_section = bfd_make_section_anyway_with_flags (link_info.output_bfd,
  2085. s->name, flags);
  2086. if (s->bfd_section == NULL)
  2087. {
  2088. einfo (_("%F%P: output format %s cannot represent section"
  2089. " called %s: %E\n"),
  2090. link_info.output_bfd->xvec->name, s->name);
  2091. }
  2092. s->bfd_section->output_section = s->bfd_section;
  2093. s->bfd_section->output_offset = 0;
  2094. /* Set the userdata of the output section to the output section
  2095. statement to avoid lookup. */
  2096. bfd_set_section_userdata (s->bfd_section, s);
  2097. /* If there is a base address, make sure that any sections it might
  2098. mention are initialized. */
  2099. if (s->addr_tree != NULL)
  2100. exp_init_os (s->addr_tree);
  2101. if (s->load_base != NULL)
  2102. exp_init_os (s->load_base);
  2103. /* If supplied an alignment, set it. */
  2104. if (s->section_alignment != NULL)
  2105. s->bfd_section->alignment_power = exp_get_power (s->section_alignment,
  2106. "section alignment");
  2107. }
  2108. /* Make sure that all output sections mentioned in an expression are
  2109. initialized. */
  2110. static void
  2111. exp_init_os (etree_type *exp)
  2112. {
  2113. switch (exp->type.node_class)
  2114. {
  2115. case etree_assign:
  2116. case etree_provide:
  2117. case etree_provided:
  2118. exp_init_os (exp->assign.src);
  2119. break;
  2120. case etree_binary:
  2121. exp_init_os (exp->binary.lhs);
  2122. exp_init_os (exp->binary.rhs);
  2123. break;
  2124. case etree_trinary:
  2125. exp_init_os (exp->trinary.cond);
  2126. exp_init_os (exp->trinary.lhs);
  2127. exp_init_os (exp->trinary.rhs);
  2128. break;
  2129. case etree_assert:
  2130. exp_init_os (exp->assert_s.child);
  2131. break;
  2132. case etree_unary:
  2133. exp_init_os (exp->unary.child);
  2134. break;
  2135. case etree_name:
  2136. switch (exp->type.node_code)
  2137. {
  2138. case ADDR:
  2139. case LOADADDR:
  2140. case SIZEOF:
  2141. {
  2142. lang_output_section_statement_type *os;
  2143. os = lang_output_section_find (exp->name.name);
  2144. if (os != NULL && os->bfd_section == NULL)
  2145. init_os (os, 0);
  2146. }
  2147. }
  2148. break;
  2149. default:
  2150. break;
  2151. }
  2152. }
  2153. static void
  2154. section_already_linked (bfd *abfd, asection *sec, void *data)
  2155. {
  2156. lang_input_statement_type *entry = (lang_input_statement_type *) data;
  2157. /* If we are only reading symbols from this object, then we want to
  2158. discard all sections. */
  2159. if (entry->flags.just_syms)
  2160. {
  2161. bfd_link_just_syms (abfd, sec, &link_info);
  2162. return;
  2163. }
  2164. /* Deal with SHF_EXCLUDE ELF sections. */
  2165. if (!bfd_link_relocatable (&link_info)
  2166. && (abfd->flags & BFD_PLUGIN) == 0
  2167. && (sec->flags & (SEC_GROUP | SEC_KEEP | SEC_EXCLUDE)) == SEC_EXCLUDE)
  2168. sec->output_section = bfd_abs_section_ptr;
  2169. if (!(abfd->flags & DYNAMIC))
  2170. bfd_section_already_linked (abfd, sec, &link_info);
  2171. }
  2172. /* Returns true if SECTION is one we know will be discarded based on its
  2173. section flags, otherwise returns false. */
  2174. static bool
  2175. lang_discard_section_p (asection *section)
  2176. {
  2177. bool discard;
  2178. flagword flags = section->flags;
  2179. /* Discard sections marked with SEC_EXCLUDE. */
  2180. discard = (flags & SEC_EXCLUDE) != 0;
  2181. /* Discard the group descriptor sections when we're finally placing the
  2182. sections from within the group. */
  2183. if ((flags & SEC_GROUP) != 0
  2184. && link_info.resolve_section_groups)
  2185. discard = true;
  2186. /* Discard debugging sections if we are stripping debugging
  2187. information. */
  2188. if ((link_info.strip == strip_debugger || link_info.strip == strip_all)
  2189. && (flags & SEC_DEBUGGING) != 0)
  2190. discard = true;
  2191. return discard;
  2192. }
  2193. /* The wild routines.
  2194. These expand statements like *(.text) and foo.o to a list of
  2195. explicit actions, like foo.o(.text), bar.o(.text) and
  2196. foo.o(.text, .data). */
  2197. /* Add SECTION to the output section OUTPUT. Do this by creating a
  2198. lang_input_section statement which is placed at PTR. */
  2199. void
  2200. lang_add_section (lang_statement_list_type *ptr,
  2201. asection *section,
  2202. struct wildcard_list *pattern,
  2203. struct flag_info *sflag_info,
  2204. lang_output_section_statement_type *output)
  2205. {
  2206. flagword flags = section->flags;
  2207. bool discard;
  2208. lang_input_section_type *new_section;
  2209. bfd *abfd = link_info.output_bfd;
  2210. /* Is this section one we know should be discarded? */
  2211. discard = lang_discard_section_p (section);
  2212. /* Discard input sections which are assigned to a section named
  2213. DISCARD_SECTION_NAME. */
  2214. if (strcmp (output->name, DISCARD_SECTION_NAME) == 0)
  2215. discard = true;
  2216. if (discard)
  2217. {
  2218. if (section->output_section == NULL)
  2219. {
  2220. /* This prevents future calls from assigning this section. */
  2221. section->output_section = bfd_abs_section_ptr;
  2222. }
  2223. else if (link_info.non_contiguous_regions_warnings)
  2224. einfo (_("%P:%pS: warning: --enable-non-contiguous-regions makes "
  2225. "section `%pA' from '%pB' match /DISCARD/ clause.\n"),
  2226. NULL, section, section->owner);
  2227. return;
  2228. }
  2229. if (sflag_info)
  2230. {
  2231. bool keep;
  2232. keep = bfd_lookup_section_flags (&link_info, sflag_info, section);
  2233. if (!keep)
  2234. return;
  2235. }
  2236. if (section->output_section != NULL)
  2237. {
  2238. if (!link_info.non_contiguous_regions)
  2239. return;
  2240. /* SECTION has already been handled in a special way
  2241. (eg. LINK_ONCE): skip it. */
  2242. if (bfd_is_abs_section (section->output_section))
  2243. return;
  2244. /* Already assigned to the same output section, do not process
  2245. it again, to avoid creating loops between duplicate sections
  2246. later. */
  2247. if (section->output_section == output->bfd_section)
  2248. return;
  2249. if (link_info.non_contiguous_regions_warnings && output->bfd_section)
  2250. einfo (_("%P:%pS: warning: --enable-non-contiguous-regions may "
  2251. "change behaviour for section `%pA' from '%pB' (assigned to "
  2252. "%pA, but additional match: %pA)\n"),
  2253. NULL, section, section->owner, section->output_section,
  2254. output->bfd_section);
  2255. /* SECTION has already been assigned to an output section, but
  2256. the user allows it to be mapped to another one in case it
  2257. overflows. We'll later update the actual output section in
  2258. size_input_section as appropriate. */
  2259. }
  2260. /* We don't copy the SEC_NEVER_LOAD flag from an input section
  2261. to an output section, because we want to be able to include a
  2262. SEC_NEVER_LOAD section in the middle of an otherwise loaded
  2263. section (I don't know why we want to do this, but we do).
  2264. build_link_order in ldwrite.c handles this case by turning
  2265. the embedded SEC_NEVER_LOAD section into a fill. */
  2266. flags &= ~ SEC_NEVER_LOAD;
  2267. /* If final link, don't copy the SEC_LINK_ONCE flags, they've
  2268. already been processed. One reason to do this is that on pe
  2269. format targets, .text$foo sections go into .text and it's odd
  2270. to see .text with SEC_LINK_ONCE set. */
  2271. if ((flags & (SEC_LINK_ONCE | SEC_GROUP)) == (SEC_LINK_ONCE | SEC_GROUP))
  2272. {
  2273. if (link_info.resolve_section_groups)
  2274. flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC);
  2275. else
  2276. flags &= ~(SEC_LINK_DUPLICATES | SEC_RELOC);
  2277. }
  2278. else if (!bfd_link_relocatable (&link_info))
  2279. flags &= ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC);
  2280. switch (output->sectype)
  2281. {
  2282. case normal_section:
  2283. case overlay_section:
  2284. case first_overlay_section:
  2285. case type_section:
  2286. break;
  2287. case noalloc_section:
  2288. flags &= ~SEC_ALLOC;
  2289. break;
  2290. case typed_readonly_section:
  2291. case readonly_section:
  2292. flags |= SEC_READONLY;
  2293. break;
  2294. case noload_section:
  2295. flags &= ~SEC_LOAD;
  2296. flags |= SEC_NEVER_LOAD;
  2297. /* Unfortunately GNU ld has managed to evolve two different
  2298. meanings to NOLOAD in scripts. ELF gets a .bss style noload,
  2299. alloc, no contents section. All others get a noload, noalloc
  2300. section. */
  2301. if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
  2302. flags &= ~SEC_HAS_CONTENTS;
  2303. else
  2304. flags &= ~SEC_ALLOC;
  2305. break;
  2306. }
  2307. if (output->bfd_section == NULL)
  2308. init_os (output, flags);
  2309. /* If SEC_READONLY is not set in the input section, then clear
  2310. it from the output section. */
  2311. output->bfd_section->flags &= flags | ~SEC_READONLY;
  2312. if (output->bfd_section->linker_has_input)
  2313. {
  2314. /* Only set SEC_READONLY flag on the first input section. */
  2315. flags &= ~ SEC_READONLY;
  2316. /* Keep SEC_MERGE and SEC_STRINGS only if they are the same. */
  2317. if ((output->bfd_section->flags & (SEC_MERGE | SEC_STRINGS))
  2318. != (flags & (SEC_MERGE | SEC_STRINGS))
  2319. || ((flags & SEC_MERGE) != 0
  2320. && output->bfd_section->entsize != section->entsize))
  2321. {
  2322. output->bfd_section->flags &= ~ (SEC_MERGE | SEC_STRINGS);
  2323. flags &= ~ (SEC_MERGE | SEC_STRINGS);
  2324. }
  2325. }
  2326. output->bfd_section->flags |= flags;
  2327. if (!output->bfd_section->linker_has_input)
  2328. {
  2329. output->bfd_section->linker_has_input = 1;
  2330. /* This must happen after flags have been updated. The output
  2331. section may have been created before we saw its first input
  2332. section, eg. for a data statement. */
  2333. bfd_init_private_section_data (section->owner, section,
  2334. link_info.output_bfd,
  2335. output->bfd_section,
  2336. &link_info);
  2337. if ((flags & SEC_MERGE) != 0)
  2338. output->bfd_section->entsize = section->entsize;
  2339. }
  2340. if ((flags & SEC_TIC54X_BLOCK) != 0
  2341. && bfd_get_arch (section->owner) == bfd_arch_tic54x)
  2342. {
  2343. /* FIXME: This value should really be obtained from the bfd... */
  2344. output->block_value = 128;
  2345. }
  2346. /* When a .ctors section is placed in .init_array it must be copied
  2347. in reverse order. Similarly for .dtors. Set that up. */
  2348. if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
  2349. && ((startswith (section->name, ".ctors")
  2350. && strcmp (output->bfd_section->name, ".init_array") == 0)
  2351. || (startswith (section->name, ".dtors")
  2352. && strcmp (output->bfd_section->name, ".fini_array") == 0))
  2353. && (section->name[6] == 0 || section->name[6] == '.'))
  2354. section->flags |= SEC_ELF_REVERSE_COPY;
  2355. if (section->alignment_power > output->bfd_section->alignment_power)
  2356. output->bfd_section->alignment_power = section->alignment_power;
  2357. section->output_section = output->bfd_section;
  2358. if (!map_head_is_link_order)
  2359. {
  2360. asection *s = output->bfd_section->map_tail.s;
  2361. output->bfd_section->map_tail.s = section;
  2362. section->map_head.s = NULL;
  2363. section->map_tail.s = s;
  2364. if (s != NULL)
  2365. s->map_head.s = section;
  2366. else
  2367. output->bfd_section->map_head.s = section;
  2368. }
  2369. /* Add a section reference to the list. */
  2370. new_section = new_stat (lang_input_section, ptr);
  2371. new_section->section = section;
  2372. new_section->pattern = pattern;
  2373. }
  2374. /* Handle wildcard sorting. This returns the lang_input_section which
  2375. should follow the one we are going to create for SECTION and FILE,
  2376. based on the sorting requirements of WILD. It returns NULL if the
  2377. new section should just go at the end of the current list. */
  2378. static lang_statement_union_type *
  2379. wild_sort (lang_wild_statement_type *wild,
  2380. struct wildcard_list *sec,
  2381. lang_input_statement_type *file,
  2382. asection *section)
  2383. {
  2384. lang_statement_union_type *l;
  2385. if (!wild->filenames_sorted
  2386. && (sec == NULL || sec->spec.sorted == none))
  2387. return NULL;
  2388. for (l = wild->children.head; l != NULL; l = l->header.next)
  2389. {
  2390. lang_input_section_type *ls;
  2391. if (l->header.type != lang_input_section_enum)
  2392. continue;
  2393. ls = &l->input_section;
  2394. /* Sorting by filename takes precedence over sorting by section
  2395. name. */
  2396. if (wild->filenames_sorted)
  2397. {
  2398. const char *fn, *ln;
  2399. bool fa, la;
  2400. int i;
  2401. /* The PE support for the .idata section as generated by
  2402. dlltool assumes that files will be sorted by the name of
  2403. the archive and then the name of the file within the
  2404. archive. */
  2405. if (file->the_bfd != NULL
  2406. && file->the_bfd->my_archive != NULL)
  2407. {
  2408. fn = bfd_get_filename (file->the_bfd->my_archive);
  2409. fa = true;
  2410. }
  2411. else
  2412. {
  2413. fn = file->filename;
  2414. fa = false;
  2415. }
  2416. if (ls->section->owner->my_archive != NULL)
  2417. {
  2418. ln = bfd_get_filename (ls->section->owner->my_archive);
  2419. la = true;
  2420. }
  2421. else
  2422. {
  2423. ln = bfd_get_filename (ls->section->owner);
  2424. la = false;
  2425. }
  2426. i = filename_cmp (fn, ln);
  2427. if (i > 0)
  2428. continue;
  2429. else if (i < 0)
  2430. break;
  2431. if (fa || la)
  2432. {
  2433. if (fa)
  2434. fn = file->filename;
  2435. if (la)
  2436. ln = bfd_get_filename (ls->section->owner);
  2437. i = filename_cmp (fn, ln);
  2438. if (i > 0)
  2439. continue;
  2440. else if (i < 0)
  2441. break;
  2442. }
  2443. }
  2444. /* Here either the files are not sorted by name, or we are
  2445. looking at the sections for this file. */
  2446. if (sec != NULL
  2447. && sec->spec.sorted != none
  2448. && sec->spec.sorted != by_none)
  2449. if (compare_section (sec->spec.sorted, section, ls->section) < 0)
  2450. break;
  2451. }
  2452. return l;
  2453. }
  2454. /* Expand a wild statement for a particular FILE. SECTION may be
  2455. NULL, in which case it is a wild card. */
  2456. static void
  2457. output_section_callback (lang_wild_statement_type *ptr,
  2458. struct wildcard_list *sec,
  2459. asection *section,
  2460. lang_input_statement_type *file,
  2461. void *output)
  2462. {
  2463. lang_statement_union_type *before;
  2464. lang_output_section_statement_type *os;
  2465. os = (lang_output_section_statement_type *) output;
  2466. /* Exclude sections that match UNIQUE_SECTION_LIST. */
  2467. if (unique_section_p (section, os))
  2468. return;
  2469. before = wild_sort (ptr, sec, file, section);
  2470. /* Here BEFORE points to the lang_input_section which
  2471. should follow the one we are about to add. If BEFORE
  2472. is NULL, then the section should just go at the end
  2473. of the current list. */
  2474. if (before == NULL)
  2475. lang_add_section (&ptr->children, section, ptr->section_list,
  2476. ptr->section_flag_list, os);
  2477. else
  2478. {
  2479. lang_statement_list_type list;
  2480. lang_statement_union_type **pp;
  2481. lang_list_init (&list);
  2482. lang_add_section (&list, section, ptr->section_list,
  2483. ptr->section_flag_list, os);
  2484. /* If we are discarding the section, LIST.HEAD will
  2485. be NULL. */
  2486. if (list.head != NULL)
  2487. {
  2488. ASSERT (list.head->header.next == NULL);
  2489. for (pp = &ptr->children.head;
  2490. *pp != before;
  2491. pp = &(*pp)->header.next)
  2492. ASSERT (*pp != NULL);
  2493. list.head->header.next = *pp;
  2494. *pp = list.head;
  2495. }
  2496. }
  2497. }
  2498. /* Check if all sections in a wild statement for a particular FILE
  2499. are readonly. */
  2500. static void
  2501. check_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
  2502. struct wildcard_list *sec ATTRIBUTE_UNUSED,
  2503. asection *section,
  2504. lang_input_statement_type *file ATTRIBUTE_UNUSED,
  2505. void *output)
  2506. {
  2507. lang_output_section_statement_type *os;
  2508. os = (lang_output_section_statement_type *) output;
  2509. /* Exclude sections that match UNIQUE_SECTION_LIST. */
  2510. if (unique_section_p (section, os))
  2511. return;
  2512. if (section->output_section == NULL && (section->flags & SEC_READONLY) == 0)
  2513. os->all_input_readonly = false;
  2514. }
  2515. /* This is passed a file name which must have been seen already and
  2516. added to the statement tree. We will see if it has been opened
  2517. already and had its symbols read. If not then we'll read it. */
  2518. static lang_input_statement_type *
  2519. lookup_name (const char *name)
  2520. {
  2521. lang_input_statement_type *search;
  2522. for (search = (void *) input_file_chain.head;
  2523. search != NULL;
  2524. search = search->next_real_file)
  2525. {
  2526. /* Use the local_sym_name as the name of the file that has
  2527. already been loaded as filename might have been transformed
  2528. via the search directory lookup mechanism. */
  2529. const char *filename = search->local_sym_name;
  2530. if (filename != NULL
  2531. && filename_cmp (filename, name) == 0)
  2532. break;
  2533. }
  2534. if (search == NULL)
  2535. {
  2536. /* Arrange to splice the input statement added by new_afile into
  2537. statement_list after the current input_file_chain tail.
  2538. We know input_file_chain is not an empty list, and that
  2539. lookup_name was called via open_input_bfds. Later calls to
  2540. lookup_name should always match an existing input_statement. */
  2541. lang_statement_union_type **tail = stat_ptr->tail;
  2542. lang_statement_union_type **after
  2543. = (void *) ((char *) input_file_chain.tail
  2544. - offsetof (lang_input_statement_type, next_real_file)
  2545. + offsetof (lang_input_statement_type, header.next));
  2546. lang_statement_union_type *rest = *after;
  2547. stat_ptr->tail = after;
  2548. search = new_afile (name, lang_input_file_is_search_file_enum,
  2549. default_target, NULL);
  2550. *stat_ptr->tail = rest;
  2551. if (*tail == NULL)
  2552. stat_ptr->tail = tail;
  2553. }
  2554. /* If we have already added this file, or this file is not real
  2555. don't add this file. */
  2556. if (search->flags.loaded || !search->flags.real)
  2557. return search;
  2558. if (!load_symbols (search, NULL))
  2559. return NULL;
  2560. return search;
  2561. }
  2562. /* Save LIST as a list of libraries whose symbols should not be exported. */
  2563. struct excluded_lib
  2564. {
  2565. char *name;
  2566. struct excluded_lib *next;
  2567. };
  2568. static struct excluded_lib *excluded_libs;
  2569. void
  2570. add_excluded_libs (const char *list)
  2571. {
  2572. const char *p = list, *end;
  2573. while (*p != '\0')
  2574. {
  2575. struct excluded_lib *entry;
  2576. end = strpbrk (p, ",:");
  2577. if (end == NULL)
  2578. end = p + strlen (p);
  2579. entry = (struct excluded_lib *) xmalloc (sizeof (*entry));
  2580. entry->next = excluded_libs;
  2581. entry->name = (char *) xmalloc (end - p + 1);
  2582. memcpy (entry->name, p, end - p);
  2583. entry->name[end - p] = '\0';
  2584. excluded_libs = entry;
  2585. if (*end == '\0')
  2586. break;
  2587. p = end + 1;
  2588. }
  2589. }
  2590. static void
  2591. check_excluded_libs (bfd *abfd)
  2592. {
  2593. struct excluded_lib *lib = excluded_libs;
  2594. while (lib)
  2595. {
  2596. int len = strlen (lib->name);
  2597. const char *filename = lbasename (bfd_get_filename (abfd));
  2598. if (strcmp (lib->name, "ALL") == 0)
  2599. {
  2600. abfd->no_export = true;
  2601. return;
  2602. }
  2603. if (filename_ncmp (lib->name, filename, len) == 0
  2604. && (filename[len] == '\0'
  2605. || (filename[len] == '.' && filename[len + 1] == 'a'
  2606. && filename[len + 2] == '\0')))
  2607. {
  2608. abfd->no_export = true;
  2609. return;
  2610. }
  2611. lib = lib->next;
  2612. }
  2613. }
  2614. /* Get the symbols for an input file. */
  2615. bool
  2616. load_symbols (lang_input_statement_type *entry,
  2617. lang_statement_list_type *place)
  2618. {
  2619. char **matching;
  2620. if (entry->flags.loaded)
  2621. return true;
  2622. ldfile_open_file (entry);
  2623. /* Do not process further if the file was missing. */
  2624. if (entry->flags.missing_file)
  2625. return true;
  2626. if (trace_files || verbose)
  2627. info_msg ("%pI\n", entry);
  2628. if (!bfd_check_format (entry->the_bfd, bfd_archive)
  2629. && !bfd_check_format_matches (entry->the_bfd, bfd_object, &matching))
  2630. {
  2631. bfd_error_type err;
  2632. struct lang_input_statement_flags save_flags;
  2633. extern FILE *yyin;
  2634. err = bfd_get_error ();
  2635. /* See if the emulation has some special knowledge. */
  2636. if (ldemul_unrecognized_file (entry))
  2637. return true;
  2638. if (err == bfd_error_file_ambiguously_recognized)
  2639. {
  2640. char **p;
  2641. einfo (_("%P: %pB: file not recognized: %E;"
  2642. " matching formats:"), entry->the_bfd);
  2643. for (p = matching; *p != NULL; p++)
  2644. einfo (" %s", *p);
  2645. einfo ("%F\n");
  2646. }
  2647. else if (err != bfd_error_file_not_recognized
  2648. || place == NULL)
  2649. einfo (_("%F%P: %pB: file not recognized: %E\n"), entry->the_bfd);
  2650. bfd_close (entry->the_bfd);
  2651. entry->the_bfd = NULL;
  2652. /* Try to interpret the file as a linker script. */
  2653. save_flags = input_flags;
  2654. ldfile_open_command_file (entry->filename);
  2655. push_stat_ptr (place);
  2656. input_flags.add_DT_NEEDED_for_regular
  2657. = entry->flags.add_DT_NEEDED_for_regular;
  2658. input_flags.add_DT_NEEDED_for_dynamic
  2659. = entry->flags.add_DT_NEEDED_for_dynamic;
  2660. input_flags.whole_archive = entry->flags.whole_archive;
  2661. input_flags.dynamic = entry->flags.dynamic;
  2662. ldfile_assumed_script = true;
  2663. parser_input = input_script;
  2664. current_input_file = entry->filename;
  2665. yyparse ();
  2666. current_input_file = NULL;
  2667. ldfile_assumed_script = false;
  2668. /* missing_file is sticky. sysrooted will already have been
  2669. restored when seeing EOF in yyparse, but no harm to restore
  2670. again. */
  2671. save_flags.missing_file |= input_flags.missing_file;
  2672. input_flags = save_flags;
  2673. pop_stat_ptr ();
  2674. fclose (yyin);
  2675. yyin = NULL;
  2676. entry->flags.loaded = true;
  2677. return true;
  2678. }
  2679. if (ldemul_recognized_file (entry))
  2680. return true;
  2681. /* We don't call ldlang_add_file for an archive. Instead, the
  2682. add_symbols entry point will call ldlang_add_file, via the
  2683. add_archive_element callback, for each element of the archive
  2684. which is used. */
  2685. switch (bfd_get_format (entry->the_bfd))
  2686. {
  2687. default:
  2688. break;
  2689. case bfd_object:
  2690. if (!entry->flags.reload)
  2691. ldlang_add_file (entry);
  2692. break;
  2693. case bfd_archive:
  2694. check_excluded_libs (entry->the_bfd);
  2695. bfd_set_usrdata (entry->the_bfd, entry);
  2696. if (entry->flags.whole_archive)
  2697. {
  2698. bfd *member = NULL;
  2699. bool loaded = true;
  2700. for (;;)
  2701. {
  2702. bfd *subsbfd;
  2703. member = bfd_openr_next_archived_file (entry->the_bfd, member);
  2704. if (member == NULL)
  2705. break;
  2706. if (!bfd_check_format (member, bfd_object))
  2707. {
  2708. einfo (_("%F%P: %pB: member %pB in archive is not an object\n"),
  2709. entry->the_bfd, member);
  2710. loaded = false;
  2711. }
  2712. subsbfd = member;
  2713. if (!(*link_info.callbacks
  2714. ->add_archive_element) (&link_info, member,
  2715. "--whole-archive", &subsbfd))
  2716. abort ();
  2717. /* Potentially, the add_archive_element hook may have set a
  2718. substitute BFD for us. */
  2719. if (!bfd_link_add_symbols (subsbfd, &link_info))
  2720. {
  2721. einfo (_("%F%P: %pB: error adding symbols: %E\n"), member);
  2722. loaded = false;
  2723. }
  2724. }
  2725. entry->flags.loaded = loaded;
  2726. return loaded;
  2727. }
  2728. break;
  2729. }
  2730. if (bfd_link_add_symbols (entry->the_bfd, &link_info))
  2731. entry->flags.loaded = true;
  2732. else
  2733. einfo (_("%F%P: %pB: error adding symbols: %E\n"), entry->the_bfd);
  2734. return entry->flags.loaded;
  2735. }
  2736. /* Handle a wild statement. S->FILENAME or S->SECTION_LIST or both
  2737. may be NULL, indicating that it is a wildcard. Separate
  2738. lang_input_section statements are created for each part of the
  2739. expansion; they are added after the wild statement S. OUTPUT is
  2740. the output section. */
  2741. static void
  2742. wild (lang_wild_statement_type *s,
  2743. const char *target ATTRIBUTE_UNUSED,
  2744. lang_output_section_statement_type *output)
  2745. {
  2746. struct wildcard_list *sec;
  2747. if (s->handler_data[0]
  2748. && s->handler_data[0]->spec.sorted == by_name
  2749. && !s->filenames_sorted)
  2750. {
  2751. lang_section_bst_type *tree;
  2752. walk_wild (s, output_section_callback_fast, output);
  2753. tree = s->tree;
  2754. if (tree)
  2755. {
  2756. output_section_callback_tree_to_list (s, tree, output);
  2757. s->tree = NULL;
  2758. }
  2759. }
  2760. else
  2761. walk_wild (s, output_section_callback, output);
  2762. if (default_common_section == NULL)
  2763. for (sec = s->section_list; sec != NULL; sec = sec->next)
  2764. if (sec->spec.name != NULL && strcmp (sec->spec.name, "COMMON") == 0)
  2765. {
  2766. /* Remember the section that common is going to in case we
  2767. later get something which doesn't know where to put it. */
  2768. default_common_section = output;
  2769. break;
  2770. }
  2771. }
  2772. /* Return TRUE iff target is the sought target. */
  2773. static int
  2774. get_target (const bfd_target *target, void *data)
  2775. {
  2776. const char *sought = (const char *) data;
  2777. return strcmp (target->name, sought) == 0;
  2778. }
  2779. /* Like strcpy() but convert to lower case as well. */
  2780. static void
  2781. stricpy (char *dest, const char *src)
  2782. {
  2783. char c;
  2784. while ((c = *src++) != 0)
  2785. *dest++ = TOLOWER (c);
  2786. *dest = 0;
  2787. }
  2788. /* Remove the first occurrence of needle (if any) in haystack
  2789. from haystack. */
  2790. static void
  2791. strcut (char *haystack, const char *needle)
  2792. {
  2793. haystack = strstr (haystack, needle);
  2794. if (haystack)
  2795. {
  2796. char *src;
  2797. for (src = haystack + strlen (needle); *src;)
  2798. *haystack++ = *src++;
  2799. *haystack = 0;
  2800. }
  2801. }
  2802. /* Compare two target format name strings.
  2803. Return a value indicating how "similar" they are. */
  2804. static int
  2805. name_compare (const char *first, const char *second)
  2806. {
  2807. char *copy1;
  2808. char *copy2;
  2809. int result;
  2810. copy1 = (char *) xmalloc (strlen (first) + 1);
  2811. copy2 = (char *) xmalloc (strlen (second) + 1);
  2812. /* Convert the names to lower case. */
  2813. stricpy (copy1, first);
  2814. stricpy (copy2, second);
  2815. /* Remove size and endian strings from the name. */
  2816. strcut (copy1, "big");
  2817. strcut (copy1, "little");
  2818. strcut (copy2, "big");
  2819. strcut (copy2, "little");
  2820. /* Return a value based on how many characters match,
  2821. starting from the beginning. If both strings are
  2822. the same then return 10 * their length. */
  2823. for (result = 0; copy1[result] == copy2[result]; result++)
  2824. if (copy1[result] == 0)
  2825. {
  2826. result *= 10;
  2827. break;
  2828. }
  2829. free (copy1);
  2830. free (copy2);
  2831. return result;
  2832. }
  2833. /* Set by closest_target_match() below. */
  2834. static const bfd_target *winner;
  2835. /* Scan all the valid bfd targets looking for one that has the endianness
  2836. requirement that was specified on the command line, and is the nearest
  2837. match to the original output target. */
  2838. static int
  2839. closest_target_match (const bfd_target *target, void *data)
  2840. {
  2841. const bfd_target *original = (const bfd_target *) data;
  2842. if (command_line.endian == ENDIAN_BIG
  2843. && target->byteorder != BFD_ENDIAN_BIG)
  2844. return 0;
  2845. if (command_line.endian == ENDIAN_LITTLE
  2846. && target->byteorder != BFD_ENDIAN_LITTLE)
  2847. return 0;
  2848. /* Must be the same flavour. */
  2849. if (target->flavour != original->flavour)
  2850. return 0;
  2851. /* Ignore generic big and little endian elf vectors. */
  2852. if (strcmp (target->name, "elf32-big") == 0
  2853. || strcmp (target->name, "elf64-big") == 0
  2854. || strcmp (target->name, "elf32-little") == 0
  2855. || strcmp (target->name, "elf64-little") == 0)
  2856. return 0;
  2857. /* If we have not found a potential winner yet, then record this one. */
  2858. if (winner == NULL)
  2859. {
  2860. winner = target;
  2861. return 0;
  2862. }
  2863. /* Oh dear, we now have two potential candidates for a successful match.
  2864. Compare their names and choose the better one. */
  2865. if (name_compare (target->name, original->name)
  2866. > name_compare (winner->name, original->name))
  2867. winner = target;
  2868. /* Keep on searching until wqe have checked them all. */
  2869. return 0;
  2870. }
  2871. /* Return the BFD target format of the first input file. */
  2872. static const char *
  2873. get_first_input_target (void)
  2874. {
  2875. const char *target = NULL;
  2876. LANG_FOR_EACH_INPUT_STATEMENT (s)
  2877. {
  2878. if (s->header.type == lang_input_statement_enum
  2879. && s->flags.real)
  2880. {
  2881. ldfile_open_file (s);
  2882. if (s->the_bfd != NULL
  2883. && bfd_check_format (s->the_bfd, bfd_object))
  2884. {
  2885. target = bfd_get_target (s->the_bfd);
  2886. if (target != NULL)
  2887. break;
  2888. }
  2889. }
  2890. }
  2891. return target;
  2892. }
  2893. const char *
  2894. lang_get_output_target (void)
  2895. {
  2896. const char *target;
  2897. /* Has the user told us which output format to use? */
  2898. if (output_target != NULL)
  2899. return output_target;
  2900. /* No - has the current target been set to something other than
  2901. the default? */
  2902. if (current_target != default_target && current_target != NULL)
  2903. return current_target;
  2904. /* No - can we determine the format of the first input file? */
  2905. target = get_first_input_target ();
  2906. if (target != NULL)
  2907. return target;
  2908. /* Failed - use the default output target. */
  2909. return default_target;
  2910. }
  2911. /* Open the output file. */
  2912. static void
  2913. open_output (const char *name)
  2914. {
  2915. lang_input_statement_type *f;
  2916. char *out = lrealpath (name);
  2917. for (f = (void *) input_file_chain.head;
  2918. f != NULL;
  2919. f = f->next_real_file)
  2920. if (f->flags.real)
  2921. {
  2922. char *in = lrealpath (f->local_sym_name);
  2923. if (filename_cmp (in, out) == 0)
  2924. einfo (_("%F%P: input file '%s' is the same as output file\n"),
  2925. f->filename);
  2926. free (in);
  2927. }
  2928. free (out);
  2929. output_target = lang_get_output_target ();
  2930. /* Has the user requested a particular endianness on the command
  2931. line? */
  2932. if (command_line.endian != ENDIAN_UNSET)
  2933. {
  2934. /* Get the chosen target. */
  2935. const bfd_target *target
  2936. = bfd_iterate_over_targets (get_target, (void *) output_target);
  2937. /* If the target is not supported, we cannot do anything. */
  2938. if (target != NULL)
  2939. {
  2940. enum bfd_endian desired_endian;
  2941. if (command_line.endian == ENDIAN_BIG)
  2942. desired_endian = BFD_ENDIAN_BIG;
  2943. else
  2944. desired_endian = BFD_ENDIAN_LITTLE;
  2945. /* See if the target has the wrong endianness. This should
  2946. not happen if the linker script has provided big and
  2947. little endian alternatives, but some scrips don't do
  2948. this. */
  2949. if (target->byteorder != desired_endian)
  2950. {
  2951. /* If it does, then see if the target provides
  2952. an alternative with the correct endianness. */
  2953. if (target->alternative_target != NULL
  2954. && (target->alternative_target->byteorder == desired_endian))
  2955. output_target = target->alternative_target->name;
  2956. else
  2957. {
  2958. /* Try to find a target as similar as possible to
  2959. the default target, but which has the desired
  2960. endian characteristic. */
  2961. bfd_iterate_over_targets (closest_target_match,
  2962. (void *) target);
  2963. /* Oh dear - we could not find any targets that
  2964. satisfy our requirements. */
  2965. if (winner == NULL)
  2966. einfo (_("%P: warning: could not find any targets"
  2967. " that match endianness requirement\n"));
  2968. else
  2969. output_target = winner->name;
  2970. }
  2971. }
  2972. }
  2973. }
  2974. link_info.output_bfd = bfd_openw (name, output_target);
  2975. if (link_info.output_bfd == NULL)
  2976. {
  2977. if (bfd_get_error () == bfd_error_invalid_target)
  2978. einfo (_("%F%P: target %s not found\n"), output_target);
  2979. einfo (_("%F%P: cannot open output file %s: %E\n"), name);
  2980. }
  2981. delete_output_file_on_failure = true;
  2982. if (!bfd_set_format (link_info.output_bfd, bfd_object))
  2983. einfo (_("%F%P: %s: can not make object file: %E\n"), name);
  2984. if (!bfd_set_arch_mach (link_info.output_bfd,
  2985. ldfile_output_architecture,
  2986. ldfile_output_machine))
  2987. einfo (_("%F%P: %s: can not set architecture: %E\n"), name);
  2988. link_info.hash = bfd_link_hash_table_create (link_info.output_bfd);
  2989. if (link_info.hash == NULL)
  2990. einfo (_("%F%P: can not create hash table: %E\n"));
  2991. bfd_set_gp_size (link_info.output_bfd, g_switch_value);
  2992. }
  2993. static void
  2994. ldlang_open_output (lang_statement_union_type *statement)
  2995. {
  2996. switch (statement->header.type)
  2997. {
  2998. case lang_output_statement_enum:
  2999. ASSERT (link_info.output_bfd == NULL);
  3000. open_output (statement->output_statement.name);
  3001. ldemul_set_output_arch ();
  3002. if (config.magic_demand_paged
  3003. && !bfd_link_relocatable (&link_info))
  3004. link_info.output_bfd->flags |= D_PAGED;
  3005. else
  3006. link_info.output_bfd->flags &= ~D_PAGED;
  3007. if (config.text_read_only)
  3008. link_info.output_bfd->flags |= WP_TEXT;
  3009. else
  3010. link_info.output_bfd->flags &= ~WP_TEXT;
  3011. if (link_info.traditional_format)
  3012. link_info.output_bfd->flags |= BFD_TRADITIONAL_FORMAT;
  3013. else
  3014. link_info.output_bfd->flags &= ~BFD_TRADITIONAL_FORMAT;
  3015. break;
  3016. case lang_target_statement_enum:
  3017. current_target = statement->target_statement.target;
  3018. break;
  3019. default:
  3020. break;
  3021. }
  3022. }
  3023. static void
  3024. init_opb (asection *s)
  3025. {
  3026. unsigned int x;
  3027. opb_shift = 0;
  3028. if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
  3029. && s != NULL
  3030. && (s->flags & SEC_ELF_OCTETS) != 0)
  3031. return;
  3032. x = bfd_arch_mach_octets_per_byte (ldfile_output_architecture,
  3033. ldfile_output_machine);
  3034. if (x > 1)
  3035. while ((x & 1) == 0)
  3036. {
  3037. x >>= 1;
  3038. ++opb_shift;
  3039. }
  3040. ASSERT (x == 1);
  3041. }
  3042. /* Open all the input files. */
  3043. enum open_bfd_mode
  3044. {
  3045. OPEN_BFD_NORMAL = 0,
  3046. OPEN_BFD_FORCE = 1,
  3047. OPEN_BFD_RESCAN = 2
  3048. };
  3049. #if BFD_SUPPORTS_PLUGINS
  3050. static lang_input_statement_type *plugin_insert = NULL;
  3051. static struct bfd_link_hash_entry *plugin_undefs = NULL;
  3052. #endif
  3053. static void
  3054. open_input_bfds (lang_statement_union_type *s, enum open_bfd_mode mode)
  3055. {
  3056. for (; s != NULL; s = s->header.next)
  3057. {
  3058. switch (s->header.type)
  3059. {
  3060. case lang_constructors_statement_enum:
  3061. open_input_bfds (constructor_list.head, mode);
  3062. break;
  3063. case lang_output_section_statement_enum:
  3064. open_input_bfds (s->output_section_statement.children.head, mode);
  3065. break;
  3066. case lang_wild_statement_enum:
  3067. /* Maybe we should load the file's symbols. */
  3068. if ((mode & OPEN_BFD_RESCAN) == 0
  3069. && s->wild_statement.filename
  3070. && !wildcardp (s->wild_statement.filename)
  3071. && !archive_path (s->wild_statement.filename))
  3072. lookup_name (s->wild_statement.filename);
  3073. open_input_bfds (s->wild_statement.children.head, mode);
  3074. break;
  3075. case lang_group_statement_enum:
  3076. {
  3077. struct bfd_link_hash_entry *undefs;
  3078. #if BFD_SUPPORTS_PLUGINS
  3079. lang_input_statement_type *plugin_insert_save;
  3080. #endif
  3081. /* We must continually search the entries in the group
  3082. until no new symbols are added to the list of undefined
  3083. symbols. */
  3084. do
  3085. {
  3086. #if BFD_SUPPORTS_PLUGINS
  3087. plugin_insert_save = plugin_insert;
  3088. #endif
  3089. undefs = link_info.hash->undefs_tail;
  3090. open_input_bfds (s->group_statement.children.head,
  3091. mode | OPEN_BFD_FORCE);
  3092. }
  3093. while (undefs != link_info.hash->undefs_tail
  3094. #if BFD_SUPPORTS_PLUGINS
  3095. /* Objects inserted by a plugin, which are loaded
  3096. before we hit this loop, may have added new
  3097. undefs. */
  3098. || (plugin_insert != plugin_insert_save && plugin_undefs)
  3099. #endif
  3100. );
  3101. }
  3102. break;
  3103. case lang_target_statement_enum:
  3104. current_target = s->target_statement.target;
  3105. break;
  3106. case lang_input_statement_enum:
  3107. if (s->input_statement.flags.real)
  3108. {
  3109. lang_statement_union_type **os_tail;
  3110. lang_statement_list_type add;
  3111. bfd *abfd;
  3112. s->input_statement.target = current_target;
  3113. /* If we are being called from within a group, and this
  3114. is an archive which has already been searched, then
  3115. force it to be researched unless the whole archive
  3116. has been loaded already. Do the same for a rescan.
  3117. Likewise reload --as-needed shared libs. */
  3118. if (mode != OPEN_BFD_NORMAL
  3119. #if BFD_SUPPORTS_PLUGINS
  3120. && ((mode & OPEN_BFD_RESCAN) == 0
  3121. || plugin_insert == NULL)
  3122. #endif
  3123. && s->input_statement.flags.loaded
  3124. && (abfd = s->input_statement.the_bfd) != NULL
  3125. && ((bfd_get_format (abfd) == bfd_archive
  3126. && !s->input_statement.flags.whole_archive)
  3127. || (bfd_get_format (abfd) == bfd_object
  3128. && ((abfd->flags) & DYNAMIC) != 0
  3129. && s->input_statement.flags.add_DT_NEEDED_for_regular
  3130. && bfd_get_flavour (abfd) == bfd_target_elf_flavour
  3131. && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0)))
  3132. {
  3133. s->input_statement.flags.loaded = false;
  3134. s->input_statement.flags.reload = true;
  3135. }
  3136. os_tail = lang_os_list.tail;
  3137. lang_list_init (&add);
  3138. if (!load_symbols (&s->input_statement, &add))
  3139. config.make_executable = false;
  3140. if (add.head != NULL)
  3141. {
  3142. /* If this was a script with output sections then
  3143. tack any added statements on to the end of the
  3144. list. This avoids having to reorder the output
  3145. section statement list. Very likely the user
  3146. forgot -T, and whatever we do here will not meet
  3147. naive user expectations. */
  3148. if (os_tail != lang_os_list.tail)
  3149. {
  3150. einfo (_("%P: warning: %s contains output sections;"
  3151. " did you forget -T?\n"),
  3152. s->input_statement.filename);
  3153. *stat_ptr->tail = add.head;
  3154. stat_ptr->tail = add.tail;
  3155. }
  3156. else
  3157. {
  3158. *add.tail = s->header.next;
  3159. s->header.next = add.head;
  3160. }
  3161. }
  3162. }
  3163. #if BFD_SUPPORTS_PLUGINS
  3164. /* If we have found the point at which a plugin added new
  3165. files, clear plugin_insert to enable archive rescan. */
  3166. if (&s->input_statement == plugin_insert)
  3167. plugin_insert = NULL;
  3168. #endif
  3169. break;
  3170. case lang_assignment_statement_enum:
  3171. if (s->assignment_statement.exp->type.node_class != etree_assert)
  3172. exp_fold_tree_no_dot (s->assignment_statement.exp);
  3173. break;
  3174. default:
  3175. break;
  3176. }
  3177. }
  3178. /* Exit if any of the files were missing. */
  3179. if (input_flags.missing_file)
  3180. einfo ("%F");
  3181. }
  3182. #ifdef ENABLE_LIBCTF
  3183. /* Emit CTF errors and warnings. fp can be NULL to report errors/warnings
  3184. that happened specifically at CTF open time. */
  3185. static void
  3186. lang_ctf_errs_warnings (ctf_dict_t *fp)
  3187. {
  3188. ctf_next_t *i = NULL;
  3189. char *text;
  3190. int is_warning;
  3191. int err;
  3192. while ((text = ctf_errwarning_next (fp, &i, &is_warning, &err)) != NULL)
  3193. {
  3194. einfo (_("%s: %s\n"), is_warning ? _("CTF warning"): _("CTF error"),
  3195. text);
  3196. free (text);
  3197. }
  3198. if (err != ECTF_NEXT_END)
  3199. {
  3200. einfo (_("CTF error: cannot get CTF errors: `%s'\n"),
  3201. ctf_errmsg (err));
  3202. }
  3203. /* `err' returns errors from the error/warning iterator in particular.
  3204. These never assert. But if we have an fp, that could have recorded
  3205. an assertion failure: assert if it has done so. */
  3206. ASSERT (!fp || ctf_errno (fp) != ECTF_INTERNAL);
  3207. }
  3208. /* Open the CTF sections in the input files with libctf: if any were opened,
  3209. create a fake input file that we'll write the merged CTF data to later
  3210. on. */
  3211. static void
  3212. ldlang_open_ctf (void)
  3213. {
  3214. int any_ctf = 0;
  3215. int err;
  3216. LANG_FOR_EACH_INPUT_STATEMENT (file)
  3217. {
  3218. asection *sect;
  3219. /* Incoming files from the compiler have a single ctf_dict_t in them
  3220. (which is presented to us by the libctf API in a ctf_archive_t
  3221. wrapper): files derived from a previous relocatable link have a CTF
  3222. archive containing possibly many CTF files. */
  3223. if ((file->the_ctf = ctf_bfdopen (file->the_bfd, &err)) == NULL)
  3224. {
  3225. if (err != ECTF_NOCTFDATA)
  3226. {
  3227. lang_ctf_errs_warnings (NULL);
  3228. einfo (_("%P: warning: CTF section in %pB not loaded; "
  3229. "its types will be discarded: %s\n"), file->the_bfd,
  3230. ctf_errmsg (err));
  3231. }
  3232. continue;
  3233. }
  3234. /* Prevent the contents of this section from being written, while
  3235. requiring the section itself to be duplicated in the output, but only
  3236. once. */
  3237. /* This section must exist if ctf_bfdopen() succeeded. */
  3238. sect = bfd_get_section_by_name (file->the_bfd, ".ctf");
  3239. sect->size = 0;
  3240. sect->flags |= SEC_NEVER_LOAD | SEC_HAS_CONTENTS | SEC_LINKER_CREATED;
  3241. if (any_ctf)
  3242. sect->flags |= SEC_EXCLUDE;
  3243. any_ctf = 1;
  3244. }
  3245. if (!any_ctf)
  3246. {
  3247. ctf_output = NULL;
  3248. return;
  3249. }
  3250. if ((ctf_output = ctf_create (&err)) != NULL)
  3251. return;
  3252. einfo (_("%P: warning: CTF output not created: `%s'\n"),
  3253. ctf_errmsg (err));
  3254. LANG_FOR_EACH_INPUT_STATEMENT (errfile)
  3255. ctf_close (errfile->the_ctf);
  3256. }
  3257. /* Merge together CTF sections. After this, only the symtab-dependent
  3258. function and data object sections need adjustment. */
  3259. static void
  3260. lang_merge_ctf (void)
  3261. {
  3262. asection *output_sect;
  3263. int flags = 0;
  3264. if (!ctf_output)
  3265. return;
  3266. output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf");
  3267. /* If the section was discarded, don't waste time merging. */
  3268. if (output_sect == NULL)
  3269. {
  3270. ctf_dict_close (ctf_output);
  3271. ctf_output = NULL;
  3272. LANG_FOR_EACH_INPUT_STATEMENT (file)
  3273. {
  3274. ctf_close (file->the_ctf);
  3275. file->the_ctf = NULL;
  3276. }
  3277. return;
  3278. }
  3279. LANG_FOR_EACH_INPUT_STATEMENT (file)
  3280. {
  3281. if (!file->the_ctf)
  3282. continue;
  3283. /* Takes ownership of file->the_ctf. */
  3284. if (ctf_link_add_ctf (ctf_output, file->the_ctf, file->filename) < 0)
  3285. {
  3286. einfo (_("%P: warning: CTF section in %pB cannot be linked: `%s'\n"),
  3287. file->the_bfd, ctf_errmsg (ctf_errno (ctf_output)));
  3288. ctf_close (file->the_ctf);
  3289. file->the_ctf = NULL;
  3290. continue;
  3291. }
  3292. }
  3293. if (!config.ctf_share_duplicated)
  3294. flags = CTF_LINK_SHARE_UNCONFLICTED;
  3295. else
  3296. flags = CTF_LINK_SHARE_DUPLICATED;
  3297. if (!config.ctf_variables)
  3298. flags |= CTF_LINK_OMIT_VARIABLES_SECTION;
  3299. if (bfd_link_relocatable (&link_info))
  3300. flags |= CTF_LINK_NO_FILTER_REPORTED_SYMS;
  3301. if (ctf_link (ctf_output, flags) < 0)
  3302. {
  3303. lang_ctf_errs_warnings (ctf_output);
  3304. einfo (_("%P: warning: CTF linking failed; "
  3305. "output will have no CTF section: %s\n"),
  3306. ctf_errmsg (ctf_errno (ctf_output)));
  3307. if (output_sect)
  3308. {
  3309. output_sect->size = 0;
  3310. output_sect->flags |= SEC_EXCLUDE;
  3311. }
  3312. }
  3313. /* Output any lingering errors that didn't come from ctf_link. */
  3314. lang_ctf_errs_warnings (ctf_output);
  3315. }
  3316. /* Let the emulation acquire strings from the dynamic strtab to help it optimize
  3317. the CTF, if supported. */
  3318. void
  3319. ldlang_ctf_acquire_strings (struct elf_strtab_hash *dynstrtab)
  3320. {
  3321. ldemul_acquire_strings_for_ctf (ctf_output, dynstrtab);
  3322. }
  3323. /* Inform the emulation about the addition of a new dynamic symbol, in BFD
  3324. internal format. */
  3325. void ldlang_ctf_new_dynsym (int symidx, struct elf_internal_sym *sym)
  3326. {
  3327. ldemul_new_dynsym_for_ctf (ctf_output, symidx, sym);
  3328. }
  3329. /* Write out the CTF section. Called early, if the emulation isn't going to
  3330. need to dedup against the strtab and symtab, then possibly called from the
  3331. target linker code if the dedup has happened. */
  3332. static void
  3333. lang_write_ctf (int late)
  3334. {
  3335. size_t output_size;
  3336. asection *output_sect;
  3337. if (!ctf_output)
  3338. return;
  3339. if (late)
  3340. {
  3341. /* Emit CTF late if this emulation says it can do so. */
  3342. if (ldemul_emit_ctf_early ())
  3343. return;
  3344. }
  3345. else
  3346. {
  3347. if (!ldemul_emit_ctf_early ())
  3348. return;
  3349. }
  3350. /* Inform the emulation that all the symbols that will be received have
  3351. been. */
  3352. ldemul_new_dynsym_for_ctf (ctf_output, 0, NULL);
  3353. /* Emit CTF. */
  3354. output_sect = bfd_get_section_by_name (link_info.output_bfd, ".ctf");
  3355. if (output_sect)
  3356. {
  3357. output_sect->contents = ctf_link_write (ctf_output, &output_size,
  3358. CTF_COMPRESSION_THRESHOLD);
  3359. output_sect->size = output_size;
  3360. output_sect->flags |= SEC_IN_MEMORY | SEC_KEEP;
  3361. lang_ctf_errs_warnings (ctf_output);
  3362. if (!output_sect->contents)
  3363. {
  3364. einfo (_("%P: warning: CTF section emission failed; "
  3365. "output will have no CTF section: %s\n"),
  3366. ctf_errmsg (ctf_errno (ctf_output)));
  3367. output_sect->size = 0;
  3368. output_sect->flags |= SEC_EXCLUDE;
  3369. }
  3370. }
  3371. /* This also closes every CTF input file used in the link. */
  3372. ctf_dict_close (ctf_output);
  3373. ctf_output = NULL;
  3374. LANG_FOR_EACH_INPUT_STATEMENT (file)
  3375. file->the_ctf = NULL;
  3376. }
  3377. /* Write out the CTF section late, if the emulation needs that. */
  3378. void
  3379. ldlang_write_ctf_late (void)
  3380. {
  3381. /* Trigger a "late call", if the emulation needs one. */
  3382. lang_write_ctf (1);
  3383. }
  3384. #else
  3385. static void
  3386. ldlang_open_ctf (void)
  3387. {
  3388. LANG_FOR_EACH_INPUT_STATEMENT (file)
  3389. {
  3390. asection *sect;
  3391. /* If built without CTF, warn and delete all CTF sections from the output.
  3392. (The alternative would be to simply concatenate them, which does not
  3393. yield a valid CTF section.) */
  3394. if ((sect = bfd_get_section_by_name (file->the_bfd, ".ctf")) != NULL)
  3395. {
  3396. einfo (_("%P: warning: CTF section in %pB not linkable: "
  3397. "%P was built without support for CTF\n"), file->the_bfd);
  3398. sect->size = 0;
  3399. sect->flags |= SEC_EXCLUDE;
  3400. }
  3401. }
  3402. }
  3403. static void lang_merge_ctf (void) {}
  3404. void
  3405. ldlang_ctf_acquire_strings (struct elf_strtab_hash *dynstrtab
  3406. ATTRIBUTE_UNUSED) {}
  3407. void
  3408. ldlang_ctf_new_dynsym (int symidx ATTRIBUTE_UNUSED,
  3409. struct elf_internal_sym *sym ATTRIBUTE_UNUSED) {}
  3410. static void lang_write_ctf (int late ATTRIBUTE_UNUSED) {}
  3411. void ldlang_write_ctf_late (void) {}
  3412. #endif
  3413. /* Add the supplied name to the symbol table as an undefined reference.
  3414. This is a two step process as the symbol table doesn't even exist at
  3415. the time the ld command line is processed. First we put the name
  3416. on a list, then, once the output file has been opened, transfer the
  3417. name to the symbol table. */
  3418. typedef struct bfd_sym_chain ldlang_undef_chain_list_type;
  3419. #define ldlang_undef_chain_list_head entry_symbol.next
  3420. void
  3421. ldlang_add_undef (const char *const name, bool cmdline ATTRIBUTE_UNUSED)
  3422. {
  3423. ldlang_undef_chain_list_type *new_undef;
  3424. new_undef = stat_alloc (sizeof (*new_undef));
  3425. new_undef->next = ldlang_undef_chain_list_head;
  3426. ldlang_undef_chain_list_head = new_undef;
  3427. new_undef->name = xstrdup (name);
  3428. if (link_info.output_bfd != NULL)
  3429. insert_undefined (new_undef->name);
  3430. }
  3431. /* Insert NAME as undefined in the symbol table. */
  3432. static void
  3433. insert_undefined (const char *name)
  3434. {
  3435. struct bfd_link_hash_entry *h;
  3436. h = bfd_link_hash_lookup (link_info.hash, name, true, false, true);
  3437. if (h == NULL)
  3438. einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
  3439. if (h->type == bfd_link_hash_new)
  3440. {
  3441. h->type = bfd_link_hash_undefined;
  3442. h->u.undef.abfd = NULL;
  3443. h->non_ir_ref_regular = true;
  3444. bfd_link_add_undef (link_info.hash, h);
  3445. }
  3446. }
  3447. /* Run through the list of undefineds created above and place them
  3448. into the linker hash table as undefined symbols belonging to the
  3449. script file. */
  3450. static void
  3451. lang_place_undefineds (void)
  3452. {
  3453. ldlang_undef_chain_list_type *ptr;
  3454. for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
  3455. insert_undefined (ptr->name);
  3456. }
  3457. /* Mark -u symbols against garbage collection. */
  3458. static void
  3459. lang_mark_undefineds (void)
  3460. {
  3461. ldlang_undef_chain_list_type *ptr;
  3462. if (is_elf_hash_table (link_info.hash))
  3463. for (ptr = ldlang_undef_chain_list_head; ptr != NULL; ptr = ptr->next)
  3464. {
  3465. struct elf_link_hash_entry *h = (struct elf_link_hash_entry *)
  3466. bfd_link_hash_lookup (link_info.hash, ptr->name, false, false, true);
  3467. if (h != NULL)
  3468. h->mark = 1;
  3469. }
  3470. }
  3471. /* Structure used to build the list of symbols that the user has required
  3472. be defined. */
  3473. struct require_defined_symbol
  3474. {
  3475. const char *name;
  3476. struct require_defined_symbol *next;
  3477. };
  3478. /* The list of symbols that the user has required be defined. */
  3479. static struct require_defined_symbol *require_defined_symbol_list;
  3480. /* Add a new symbol NAME to the list of symbols that are required to be
  3481. defined. */
  3482. void
  3483. ldlang_add_require_defined (const char *const name)
  3484. {
  3485. struct require_defined_symbol *ptr;
  3486. ldlang_add_undef (name, true);
  3487. ptr = stat_alloc (sizeof (*ptr));
  3488. ptr->next = require_defined_symbol_list;
  3489. ptr->name = strdup (name);
  3490. require_defined_symbol_list = ptr;
  3491. }
  3492. /* Check that all symbols the user required to be defined, are defined,
  3493. raise an error if we find a symbol that is not defined. */
  3494. static void
  3495. ldlang_check_require_defined_symbols (void)
  3496. {
  3497. struct require_defined_symbol *ptr;
  3498. for (ptr = require_defined_symbol_list; ptr != NULL; ptr = ptr->next)
  3499. {
  3500. struct bfd_link_hash_entry *h;
  3501. h = bfd_link_hash_lookup (link_info.hash, ptr->name,
  3502. false, false, true);
  3503. if (h == NULL
  3504. || (h->type != bfd_link_hash_defined
  3505. && h->type != bfd_link_hash_defweak))
  3506. einfo(_("%X%P: required symbol `%s' not defined\n"), ptr->name);
  3507. }
  3508. }
  3509. /* Check for all readonly or some readwrite sections. */
  3510. static void
  3511. check_input_sections
  3512. (lang_statement_union_type *s,
  3513. lang_output_section_statement_type *output_section_statement)
  3514. {
  3515. for (; s != NULL; s = s->header.next)
  3516. {
  3517. switch (s->header.type)
  3518. {
  3519. case lang_wild_statement_enum:
  3520. walk_wild (&s->wild_statement, check_section_callback,
  3521. output_section_statement);
  3522. if (!output_section_statement->all_input_readonly)
  3523. return;
  3524. break;
  3525. case lang_constructors_statement_enum:
  3526. check_input_sections (constructor_list.head,
  3527. output_section_statement);
  3528. if (!output_section_statement->all_input_readonly)
  3529. return;
  3530. break;
  3531. case lang_group_statement_enum:
  3532. check_input_sections (s->group_statement.children.head,
  3533. output_section_statement);
  3534. if (!output_section_statement->all_input_readonly)
  3535. return;
  3536. break;
  3537. default:
  3538. break;
  3539. }
  3540. }
  3541. }
  3542. /* Update wildcard statements if needed. */
  3543. static void
  3544. update_wild_statements (lang_statement_union_type *s)
  3545. {
  3546. struct wildcard_list *sec;
  3547. switch (sort_section)
  3548. {
  3549. default:
  3550. FAIL ();
  3551. case none:
  3552. break;
  3553. case by_name:
  3554. case by_alignment:
  3555. for (; s != NULL; s = s->header.next)
  3556. {
  3557. switch (s->header.type)
  3558. {
  3559. default:
  3560. break;
  3561. case lang_wild_statement_enum:
  3562. for (sec = s->wild_statement.section_list; sec != NULL;
  3563. sec = sec->next)
  3564. /* Don't sort .init/.fini sections. */
  3565. if (strcmp (sec->spec.name, ".init") != 0
  3566. && strcmp (sec->spec.name, ".fini") != 0)
  3567. switch (sec->spec.sorted)
  3568. {
  3569. case none:
  3570. sec->spec.sorted = sort_section;
  3571. break;
  3572. case by_name:
  3573. if (sort_section == by_alignment)
  3574. sec->spec.sorted = by_name_alignment;
  3575. break;
  3576. case by_alignment:
  3577. if (sort_section == by_name)
  3578. sec->spec.sorted = by_alignment_name;
  3579. break;
  3580. default:
  3581. break;
  3582. }
  3583. break;
  3584. case lang_constructors_statement_enum:
  3585. update_wild_statements (constructor_list.head);
  3586. break;
  3587. case lang_output_section_statement_enum:
  3588. update_wild_statements
  3589. (s->output_section_statement.children.head);
  3590. break;
  3591. case lang_group_statement_enum:
  3592. update_wild_statements (s->group_statement.children.head);
  3593. break;
  3594. }
  3595. }
  3596. break;
  3597. }
  3598. }
  3599. /* Open input files and attach to output sections. */
  3600. static void
  3601. map_input_to_output_sections
  3602. (lang_statement_union_type *s, const char *target,
  3603. lang_output_section_statement_type *os)
  3604. {
  3605. for (; s != NULL; s = s->header.next)
  3606. {
  3607. lang_output_section_statement_type *tos;
  3608. flagword flags;
  3609. unsigned int type = 0;
  3610. switch (s->header.type)
  3611. {
  3612. case lang_wild_statement_enum:
  3613. wild (&s->wild_statement, target, os);
  3614. break;
  3615. case lang_constructors_statement_enum:
  3616. map_input_to_output_sections (constructor_list.head,
  3617. target,
  3618. os);
  3619. break;
  3620. case lang_output_section_statement_enum:
  3621. tos = &s->output_section_statement;
  3622. if (tos->constraint == ONLY_IF_RW
  3623. || tos->constraint == ONLY_IF_RO)
  3624. {
  3625. tos->all_input_readonly = true;
  3626. check_input_sections (tos->children.head, tos);
  3627. if (tos->all_input_readonly != (tos->constraint == ONLY_IF_RO))
  3628. tos->constraint = -1;
  3629. }
  3630. if (tos->constraint >= 0)
  3631. map_input_to_output_sections (tos->children.head,
  3632. target,
  3633. tos);
  3634. break;
  3635. case lang_output_statement_enum:
  3636. break;
  3637. case lang_target_statement_enum:
  3638. target = s->target_statement.target;
  3639. break;
  3640. case lang_group_statement_enum:
  3641. map_input_to_output_sections (s->group_statement.children.head,
  3642. target,
  3643. os);
  3644. break;
  3645. case lang_data_statement_enum:
  3646. /* Make sure that any sections mentioned in the expression
  3647. are initialized. */
  3648. exp_init_os (s->data_statement.exp);
  3649. /* The output section gets CONTENTS, ALLOC and LOAD, but
  3650. these may be overridden by the script. */
  3651. flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD;
  3652. switch (os->sectype)
  3653. {
  3654. case normal_section:
  3655. case overlay_section:
  3656. case first_overlay_section:
  3657. break;
  3658. case noalloc_section:
  3659. flags = SEC_HAS_CONTENTS;
  3660. break;
  3661. case readonly_section:
  3662. flags |= SEC_READONLY;
  3663. break;
  3664. case typed_readonly_section:
  3665. flags |= SEC_READONLY;
  3666. /* Fall through. */
  3667. case type_section:
  3668. if (os->sectype_value->type.node_class == etree_name
  3669. && os->sectype_value->type.node_code == NAME)
  3670. {
  3671. const char *name = os->sectype_value->name.name;
  3672. if (strcmp (name, "SHT_PROGBITS") == 0)
  3673. type = SHT_PROGBITS;
  3674. else if (strcmp (name, "SHT_STRTAB") == 0)
  3675. type = SHT_STRTAB;
  3676. else if (strcmp (name, "SHT_NOTE") == 0)
  3677. type = SHT_NOTE;
  3678. else if (strcmp (name, "SHT_NOBITS") == 0)
  3679. type = SHT_NOBITS;
  3680. else if (strcmp (name, "SHT_INIT_ARRAY") == 0)
  3681. type = SHT_INIT_ARRAY;
  3682. else if (strcmp (name, "SHT_FINI_ARRAY") == 0)
  3683. type = SHT_FINI_ARRAY;
  3684. else if (strcmp (name, "SHT_PREINIT_ARRAY") == 0)
  3685. type = SHT_PREINIT_ARRAY;
  3686. else
  3687. einfo (_ ("%F%P: invalid type for output section `%s'\n"),
  3688. os->name);
  3689. }
  3690. else
  3691. {
  3692. exp_fold_tree_no_dot (os->sectype_value);
  3693. if (expld.result.valid_p)
  3694. type = expld.result.value;
  3695. else
  3696. einfo (_ ("%F%P: invalid type for output section `%s'\n"),
  3697. os->name);
  3698. }
  3699. break;
  3700. case noload_section:
  3701. if (bfd_get_flavour (link_info.output_bfd)
  3702. == bfd_target_elf_flavour)
  3703. flags = SEC_NEVER_LOAD | SEC_ALLOC;
  3704. else
  3705. flags = SEC_NEVER_LOAD | SEC_HAS_CONTENTS;
  3706. break;
  3707. }
  3708. if (os->bfd_section == NULL)
  3709. init_os (os, flags | SEC_READONLY);
  3710. else
  3711. os->bfd_section->flags |= flags;
  3712. os->bfd_section->type = type;
  3713. break;
  3714. case lang_input_section_enum:
  3715. break;
  3716. case lang_fill_statement_enum:
  3717. case lang_object_symbols_statement_enum:
  3718. case lang_reloc_statement_enum:
  3719. case lang_padding_statement_enum:
  3720. case lang_input_statement_enum:
  3721. if (os != NULL && os->bfd_section == NULL)
  3722. init_os (os, 0);
  3723. break;
  3724. case lang_assignment_statement_enum:
  3725. if (os != NULL && os->bfd_section == NULL)
  3726. init_os (os, 0);
  3727. /* Make sure that any sections mentioned in the assignment
  3728. are initialized. */
  3729. exp_init_os (s->assignment_statement.exp);
  3730. break;
  3731. case lang_address_statement_enum:
  3732. /* Mark the specified section with the supplied address.
  3733. If this section was actually a segment marker, then the
  3734. directive is ignored if the linker script explicitly
  3735. processed the segment marker. Originally, the linker
  3736. treated segment directives (like -Ttext on the
  3737. command-line) as section directives. We honor the
  3738. section directive semantics for backwards compatibility;
  3739. linker scripts that do not specifically check for
  3740. SEGMENT_START automatically get the old semantics. */
  3741. if (!s->address_statement.segment
  3742. || !s->address_statement.segment->used)
  3743. {
  3744. const char *name = s->address_statement.section_name;
  3745. /* Create the output section statement here so that
  3746. orphans with a set address will be placed after other
  3747. script sections. If we let the orphan placement code
  3748. place them in amongst other sections then the address
  3749. will affect following script sections, which is
  3750. likely to surprise naive users. */
  3751. tos = lang_output_section_statement_lookup (name, 0, 1);
  3752. tos->addr_tree = s->address_statement.address;
  3753. if (tos->bfd_section == NULL)
  3754. init_os (tos, 0);
  3755. }
  3756. break;
  3757. case lang_insert_statement_enum:
  3758. break;
  3759. }
  3760. }
  3761. }
  3762. /* An insert statement snips out all the linker statements from the
  3763. start of the list and places them after the output section
  3764. statement specified by the insert. This operation is complicated
  3765. by the fact that we keep a doubly linked list of output section
  3766. statements as well as the singly linked list of all statements.
  3767. FIXME someday: Twiddling with the list not only moves statements
  3768. from the user's script but also input and group statements that are
  3769. built from command line object files and --start-group. We only
  3770. get away with this because the list pointers used by file_chain
  3771. and input_file_chain are not reordered, and processing via
  3772. statement_list after this point mostly ignores input statements.
  3773. One exception is the map file, where LOAD and START GROUP/END GROUP
  3774. can end up looking odd. */
  3775. static void
  3776. process_insert_statements (lang_statement_union_type **start)
  3777. {
  3778. lang_statement_union_type **s;
  3779. lang_output_section_statement_type *first_os = NULL;
  3780. lang_output_section_statement_type *last_os = NULL;
  3781. lang_output_section_statement_type *os;
  3782. s = start;
  3783. while (*s != NULL)
  3784. {
  3785. if ((*s)->header.type == lang_output_section_statement_enum)
  3786. {
  3787. /* Keep pointers to the first and last output section
  3788. statement in the sequence we may be about to move. */
  3789. os = &(*s)->output_section_statement;
  3790. ASSERT (last_os == NULL || last_os->next == os);
  3791. last_os = os;
  3792. /* Set constraint negative so that lang_output_section_find
  3793. won't match this output section statement. At this
  3794. stage in linking constraint has values in the range
  3795. [-1, ONLY_IN_RW]. */
  3796. last_os->constraint = -2 - last_os->constraint;
  3797. if (first_os == NULL)
  3798. first_os = last_os;
  3799. }
  3800. else if ((*s)->header.type == lang_group_statement_enum)
  3801. {
  3802. /* A user might put -T between --start-group and
  3803. --end-group. One way this odd construct might arise is
  3804. from a wrapper around ld to change library search
  3805. behaviour. For example:
  3806. #! /bin/sh
  3807. exec real_ld --start-group "$@" --end-group
  3808. This isn't completely unreasonable so go looking inside a
  3809. group statement for insert statements. */
  3810. process_insert_statements (&(*s)->group_statement.children.head);
  3811. }
  3812. else if ((*s)->header.type == lang_insert_statement_enum)
  3813. {
  3814. lang_insert_statement_type *i = &(*s)->insert_statement;
  3815. lang_output_section_statement_type *where;
  3816. lang_statement_union_type **ptr;
  3817. lang_statement_union_type *first;
  3818. if (link_info.non_contiguous_regions)
  3819. {
  3820. einfo (_("warning: INSERT statement in linker script is "
  3821. "incompatible with --enable-non-contiguous-regions.\n"));
  3822. }
  3823. where = lang_output_section_find (i->where);
  3824. if (where != NULL && i->is_before)
  3825. {
  3826. do
  3827. where = where->prev;
  3828. while (where != NULL && where->constraint < 0);
  3829. }
  3830. if (where == NULL)
  3831. {
  3832. einfo (_("%F%P: %s not found for insert\n"), i->where);
  3833. return;
  3834. }
  3835. /* Deal with reordering the output section statement list. */
  3836. if (last_os != NULL)
  3837. {
  3838. asection *first_sec, *last_sec;
  3839. struct lang_output_section_statement_struct **next;
  3840. /* Snip out the output sections we are moving. */
  3841. first_os->prev->next = last_os->next;
  3842. if (last_os->next == NULL)
  3843. {
  3844. next = &first_os->prev->next;
  3845. lang_os_list.tail = (lang_statement_union_type **) next;
  3846. }
  3847. else
  3848. last_os->next->prev = first_os->prev;
  3849. /* Add them in at the new position. */
  3850. last_os->next = where->next;
  3851. if (where->next == NULL)
  3852. {
  3853. next = &last_os->next;
  3854. lang_os_list.tail = (lang_statement_union_type **) next;
  3855. }
  3856. else
  3857. where->next->prev = last_os;
  3858. first_os->prev = where;
  3859. where->next = first_os;
  3860. /* Move the bfd sections in the same way. */
  3861. first_sec = NULL;
  3862. last_sec = NULL;
  3863. for (os = first_os; os != NULL; os = os->next)
  3864. {
  3865. os->constraint = -2 - os->constraint;
  3866. if (os->bfd_section != NULL
  3867. && os->bfd_section->owner != NULL)
  3868. {
  3869. last_sec = os->bfd_section;
  3870. if (first_sec == NULL)
  3871. first_sec = last_sec;
  3872. }
  3873. if (os == last_os)
  3874. break;
  3875. }
  3876. if (last_sec != NULL)
  3877. {
  3878. asection *sec = where->bfd_section;
  3879. if (sec == NULL)
  3880. sec = output_prev_sec_find (where);
  3881. /* The place we want to insert must come after the
  3882. sections we are moving. So if we find no
  3883. section or if the section is the same as our
  3884. last section, then no move is needed. */
  3885. if (sec != NULL && sec != last_sec)
  3886. {
  3887. /* Trim them off. */
  3888. if (first_sec->prev != NULL)
  3889. first_sec->prev->next = last_sec->next;
  3890. else
  3891. link_info.output_bfd->sections = last_sec->next;
  3892. if (last_sec->next != NULL)
  3893. last_sec->next->prev = first_sec->prev;
  3894. else
  3895. link_info.output_bfd->section_last = first_sec->prev;
  3896. /* Add back. */
  3897. last_sec->next = sec->next;
  3898. if (sec->next != NULL)
  3899. sec->next->prev = last_sec;
  3900. else
  3901. link_info.output_bfd->section_last = last_sec;
  3902. first_sec->prev = sec;
  3903. sec->next = first_sec;
  3904. }
  3905. }
  3906. first_os = NULL;
  3907. last_os = NULL;
  3908. }
  3909. ptr = insert_os_after (where);
  3910. /* Snip everything from the start of the list, up to and
  3911. including the insert statement we are currently processing. */
  3912. first = *start;
  3913. *start = (*s)->header.next;
  3914. /* Add them back where they belong, minus the insert. */
  3915. *s = *ptr;
  3916. if (*s == NULL)
  3917. statement_list.tail = s;
  3918. *ptr = first;
  3919. s = start;
  3920. continue;
  3921. }
  3922. s = &(*s)->header.next;
  3923. }
  3924. /* Undo constraint twiddling. */
  3925. for (os = first_os; os != NULL; os = os->next)
  3926. {
  3927. os->constraint = -2 - os->constraint;
  3928. if (os == last_os)
  3929. break;
  3930. }
  3931. }
  3932. /* An output section might have been removed after its statement was
  3933. added. For example, ldemul_before_allocation can remove dynamic
  3934. sections if they turn out to be not needed. Clean them up here. */
  3935. void
  3936. strip_excluded_output_sections (void)
  3937. {
  3938. lang_output_section_statement_type *os;
  3939. /* Run lang_size_sections (if not already done). */
  3940. if (expld.phase != lang_mark_phase_enum)
  3941. {
  3942. expld.phase = lang_mark_phase_enum;
  3943. expld.dataseg.phase = exp_seg_none;
  3944. one_lang_size_sections_pass (NULL, false);
  3945. lang_reset_memory_regions ();
  3946. }
  3947. for (os = (void *) lang_os_list.head;
  3948. os != NULL;
  3949. os = os->next)
  3950. {
  3951. asection *output_section;
  3952. bool exclude;
  3953. if (os->constraint < 0)
  3954. continue;
  3955. output_section = os->bfd_section;
  3956. if (output_section == NULL)
  3957. continue;
  3958. exclude = (output_section->rawsize == 0
  3959. && (output_section->flags & SEC_KEEP) == 0
  3960. && !bfd_section_removed_from_list (link_info.output_bfd,
  3961. output_section));
  3962. /* Some sections have not yet been sized, notably .gnu.version,
  3963. .dynsym, .dynstr and .hash. These all have SEC_LINKER_CREATED
  3964. input sections, so don't drop output sections that have such
  3965. input sections unless they are also marked SEC_EXCLUDE. */
  3966. if (exclude && output_section->map_head.s != NULL)
  3967. {
  3968. asection *s;
  3969. for (s = output_section->map_head.s; s != NULL; s = s->map_head.s)
  3970. if ((s->flags & SEC_EXCLUDE) == 0
  3971. && ((s->flags & SEC_LINKER_CREATED) != 0
  3972. || link_info.emitrelocations))
  3973. {
  3974. exclude = false;
  3975. break;
  3976. }
  3977. }
  3978. if (exclude)
  3979. {
  3980. /* We don't set bfd_section to NULL since bfd_section of the
  3981. removed output section statement may still be used. */
  3982. if (!os->update_dot)
  3983. os->ignored = true;
  3984. output_section->flags |= SEC_EXCLUDE;
  3985. bfd_section_list_remove (link_info.output_bfd, output_section);
  3986. link_info.output_bfd->section_count--;
  3987. }
  3988. }
  3989. }
  3990. /* Called from ldwrite to clear out asection.map_head and
  3991. asection.map_tail for use as link_orders in ldwrite. */
  3992. void
  3993. lang_clear_os_map (void)
  3994. {
  3995. lang_output_section_statement_type *os;
  3996. if (map_head_is_link_order)
  3997. return;
  3998. for (os = (void *) lang_os_list.head;
  3999. os != NULL;
  4000. os = os->next)
  4001. {
  4002. asection *output_section;
  4003. if (os->constraint < 0)
  4004. continue;
  4005. output_section = os->bfd_section;
  4006. if (output_section == NULL)
  4007. continue;
  4008. /* TODO: Don't just junk map_head.s, turn them into link_orders. */
  4009. output_section->map_head.link_order = NULL;
  4010. output_section->map_tail.link_order = NULL;
  4011. }
  4012. /* Stop future calls to lang_add_section from messing with map_head
  4013. and map_tail link_order fields. */
  4014. map_head_is_link_order = true;
  4015. }
  4016. static void
  4017. print_output_section_statement
  4018. (lang_output_section_statement_type *output_section_statement)
  4019. {
  4020. asection *section = output_section_statement->bfd_section;
  4021. int len;
  4022. if (output_section_statement != abs_output_section)
  4023. {
  4024. minfo ("\n%s", output_section_statement->name);
  4025. if (section != NULL)
  4026. {
  4027. print_dot = section->vma;
  4028. len = strlen (output_section_statement->name);
  4029. if (len >= SECTION_NAME_MAP_LENGTH - 1)
  4030. {
  4031. print_nl ();
  4032. len = 0;
  4033. }
  4034. while (len < SECTION_NAME_MAP_LENGTH)
  4035. {
  4036. print_space ();
  4037. ++len;
  4038. }
  4039. minfo ("0x%V %W", section->vma, TO_ADDR (section->size));
  4040. if (section->vma != section->lma)
  4041. minfo (_(" load address 0x%V"), section->lma);
  4042. if (output_section_statement->update_dot_tree != NULL)
  4043. exp_fold_tree (output_section_statement->update_dot_tree,
  4044. bfd_abs_section_ptr, &print_dot);
  4045. }
  4046. print_nl ();
  4047. }
  4048. print_statement_list (output_section_statement->children.head,
  4049. output_section_statement);
  4050. }
  4051. static void
  4052. print_assignment (lang_assignment_statement_type *assignment,
  4053. lang_output_section_statement_type *output_section)
  4054. {
  4055. unsigned int i;
  4056. bool is_dot;
  4057. etree_type *tree;
  4058. asection *osec;
  4059. for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
  4060. print_space ();
  4061. if (assignment->exp->type.node_class == etree_assert)
  4062. {
  4063. is_dot = false;
  4064. tree = assignment->exp->assert_s.child;
  4065. }
  4066. else
  4067. {
  4068. const char *dst = assignment->exp->assign.dst;
  4069. is_dot = (dst[0] == '.' && dst[1] == 0);
  4070. tree = assignment->exp;
  4071. }
  4072. osec = output_section->bfd_section;
  4073. if (osec == NULL)
  4074. osec = bfd_abs_section_ptr;
  4075. if (assignment->exp->type.node_class != etree_provide)
  4076. exp_fold_tree (tree, osec, &print_dot);
  4077. else
  4078. expld.result.valid_p = false;
  4079. if (expld.result.valid_p)
  4080. {
  4081. bfd_vma value;
  4082. if (assignment->exp->type.node_class == etree_assert
  4083. || is_dot
  4084. || expld.assign_name != NULL)
  4085. {
  4086. value = expld.result.value;
  4087. if (expld.result.section != NULL)
  4088. value += expld.result.section->vma;
  4089. minfo ("0x%V", value);
  4090. if (is_dot)
  4091. print_dot = value;
  4092. }
  4093. else
  4094. {
  4095. struct bfd_link_hash_entry *h;
  4096. h = bfd_link_hash_lookup (link_info.hash, assignment->exp->assign.dst,
  4097. false, false, true);
  4098. if (h != NULL
  4099. && (h->type == bfd_link_hash_defined
  4100. || h->type == bfd_link_hash_defweak))
  4101. {
  4102. value = h->u.def.value;
  4103. value += h->u.def.section->output_section->vma;
  4104. value += h->u.def.section->output_offset;
  4105. minfo ("[0x%V]", value);
  4106. }
  4107. else
  4108. minfo ("[unresolved]");
  4109. }
  4110. }
  4111. else
  4112. {
  4113. if (assignment->exp->type.node_class == etree_provide)
  4114. minfo ("[!provide]");
  4115. else
  4116. minfo ("*undef* ");
  4117. #ifdef BFD64
  4118. minfo (" ");
  4119. #endif
  4120. }
  4121. expld.assign_name = NULL;
  4122. minfo (" ");
  4123. exp_print_tree (assignment->exp);
  4124. print_nl ();
  4125. }
  4126. static void
  4127. print_input_statement (lang_input_statement_type *statm)
  4128. {
  4129. if (statm->filename != NULL)
  4130. fprintf (config.map_file, "LOAD %s\n", statm->filename);
  4131. }
  4132. /* Print all symbols defined in a particular section. This is called
  4133. via bfd_link_hash_traverse, or by print_all_symbols. */
  4134. bool
  4135. print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr)
  4136. {
  4137. asection *sec = (asection *) ptr;
  4138. if ((hash_entry->type == bfd_link_hash_defined
  4139. || hash_entry->type == bfd_link_hash_defweak)
  4140. && sec == hash_entry->u.def.section)
  4141. {
  4142. int i;
  4143. for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
  4144. print_space ();
  4145. minfo ("0x%V ",
  4146. (hash_entry->u.def.value
  4147. + hash_entry->u.def.section->output_offset
  4148. + hash_entry->u.def.section->output_section->vma));
  4149. minfo (" %pT\n", hash_entry->root.string);
  4150. }
  4151. return true;
  4152. }
  4153. static int
  4154. hash_entry_addr_cmp (const void *a, const void *b)
  4155. {
  4156. const struct bfd_link_hash_entry *l = *(const struct bfd_link_hash_entry **)a;
  4157. const struct bfd_link_hash_entry *r = *(const struct bfd_link_hash_entry **)b;
  4158. if (l->u.def.value < r->u.def.value)
  4159. return -1;
  4160. else if (l->u.def.value > r->u.def.value)
  4161. return 1;
  4162. else
  4163. return 0;
  4164. }
  4165. static void
  4166. print_all_symbols (asection *sec)
  4167. {
  4168. input_section_userdata_type *ud = bfd_section_userdata (sec);
  4169. struct map_symbol_def *def;
  4170. struct bfd_link_hash_entry **entries;
  4171. unsigned int i;
  4172. if (!ud)
  4173. return;
  4174. *ud->map_symbol_def_tail = 0;
  4175. /* Sort the symbols by address. */
  4176. entries = (struct bfd_link_hash_entry **)
  4177. obstack_alloc (&map_obstack,
  4178. ud->map_symbol_def_count * sizeof (*entries));
  4179. for (i = 0, def = ud->map_symbol_def_head; def; def = def->next, i++)
  4180. entries[i] = def->entry;
  4181. qsort (entries, ud->map_symbol_def_count, sizeof (*entries),
  4182. hash_entry_addr_cmp);
  4183. /* Print the symbols. */
  4184. for (i = 0; i < ud->map_symbol_def_count; i++)
  4185. ldemul_print_symbol (entries[i], sec);
  4186. obstack_free (&map_obstack, entries);
  4187. }
  4188. /* Print information about an input section to the map file. */
  4189. static void
  4190. print_input_section (asection *i, bool is_discarded)
  4191. {
  4192. bfd_size_type size = i->size;
  4193. int len;
  4194. bfd_vma addr;
  4195. init_opb (i);
  4196. print_space ();
  4197. minfo ("%s", i->name);
  4198. len = 1 + strlen (i->name);
  4199. if (len >= SECTION_NAME_MAP_LENGTH - 1)
  4200. {
  4201. print_nl ();
  4202. len = 0;
  4203. }
  4204. while (len < SECTION_NAME_MAP_LENGTH)
  4205. {
  4206. print_space ();
  4207. ++len;
  4208. }
  4209. if (i->output_section != NULL
  4210. && i->output_section->owner == link_info.output_bfd)
  4211. addr = i->output_section->vma + i->output_offset;
  4212. else
  4213. {
  4214. addr = print_dot;
  4215. if (!is_discarded)
  4216. size = 0;
  4217. }
  4218. minfo ("0x%V %W %pB\n", addr, TO_ADDR (size), i->owner);
  4219. if (size != i->rawsize && i->rawsize != 0)
  4220. {
  4221. len = SECTION_NAME_MAP_LENGTH + 3;
  4222. #ifdef BFD64
  4223. len += 16;
  4224. #else
  4225. len += 8;
  4226. #endif
  4227. while (len > 0)
  4228. {
  4229. print_space ();
  4230. --len;
  4231. }
  4232. minfo (_("%W (size before relaxing)\n"), TO_ADDR (i->rawsize));
  4233. }
  4234. if (i->output_section != NULL
  4235. && i->output_section->owner == link_info.output_bfd)
  4236. {
  4237. if (link_info.reduce_memory_overheads)
  4238. bfd_link_hash_traverse (link_info.hash, ldemul_print_symbol, i);
  4239. else
  4240. print_all_symbols (i);
  4241. /* Update print_dot, but make sure that we do not move it
  4242. backwards - this could happen if we have overlays and a
  4243. later overlay is shorter than an earier one. */
  4244. if (addr + TO_ADDR (size) > print_dot)
  4245. print_dot = addr + TO_ADDR (size);
  4246. }
  4247. }
  4248. static void
  4249. print_fill_statement (lang_fill_statement_type *fill)
  4250. {
  4251. size_t size;
  4252. unsigned char *p;
  4253. fputs (" FILL mask 0x", config.map_file);
  4254. for (p = fill->fill->data, size = fill->fill->size; size != 0; p++, size--)
  4255. fprintf (config.map_file, "%02x", *p);
  4256. fputs ("\n", config.map_file);
  4257. }
  4258. static void
  4259. print_data_statement (lang_data_statement_type *data)
  4260. {
  4261. int i;
  4262. bfd_vma addr;
  4263. bfd_size_type size;
  4264. const char *name;
  4265. init_opb (data->output_section);
  4266. for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
  4267. print_space ();
  4268. addr = data->output_offset;
  4269. if (data->output_section != NULL)
  4270. addr += data->output_section->vma;
  4271. switch (data->type)
  4272. {
  4273. default:
  4274. abort ();
  4275. case BYTE:
  4276. size = BYTE_SIZE;
  4277. name = "BYTE";
  4278. break;
  4279. case SHORT:
  4280. size = SHORT_SIZE;
  4281. name = "SHORT";
  4282. break;
  4283. case LONG:
  4284. size = LONG_SIZE;
  4285. name = "LONG";
  4286. break;
  4287. case QUAD:
  4288. size = QUAD_SIZE;
  4289. name = "QUAD";
  4290. break;
  4291. case SQUAD:
  4292. size = QUAD_SIZE;
  4293. name = "SQUAD";
  4294. break;
  4295. }
  4296. if (size < TO_SIZE ((unsigned) 1))
  4297. size = TO_SIZE ((unsigned) 1);
  4298. minfo ("0x%V %W %s 0x%v", addr, TO_ADDR (size), name, data->value);
  4299. if (data->exp->type.node_class != etree_value)
  4300. {
  4301. print_space ();
  4302. exp_print_tree (data->exp);
  4303. }
  4304. print_nl ();
  4305. print_dot = addr + TO_ADDR (size);
  4306. }
  4307. /* Print an address statement. These are generated by options like
  4308. -Ttext. */
  4309. static void
  4310. print_address_statement (lang_address_statement_type *address)
  4311. {
  4312. minfo (_("Address of section %s set to "), address->section_name);
  4313. exp_print_tree (address->address);
  4314. print_nl ();
  4315. }
  4316. /* Print a reloc statement. */
  4317. static void
  4318. print_reloc_statement (lang_reloc_statement_type *reloc)
  4319. {
  4320. int i;
  4321. bfd_vma addr;
  4322. bfd_size_type size;
  4323. init_opb (reloc->output_section);
  4324. for (i = 0; i < SECTION_NAME_MAP_LENGTH; i++)
  4325. print_space ();
  4326. addr = reloc->output_offset;
  4327. if (reloc->output_section != NULL)
  4328. addr += reloc->output_section->vma;
  4329. size = bfd_get_reloc_size (reloc->howto);
  4330. minfo ("0x%V %W RELOC %s ", addr, TO_ADDR (size), reloc->howto->name);
  4331. if (reloc->name != NULL)
  4332. minfo ("%s+", reloc->name);
  4333. else
  4334. minfo ("%s+", reloc->section->name);
  4335. exp_print_tree (reloc->addend_exp);
  4336. print_nl ();
  4337. print_dot = addr + TO_ADDR (size);
  4338. }
  4339. static void
  4340. print_padding_statement (lang_padding_statement_type *s)
  4341. {
  4342. int len;
  4343. bfd_vma addr;
  4344. init_opb (s->output_section);
  4345. minfo (" *fill*");
  4346. len = sizeof " *fill*" - 1;
  4347. while (len < SECTION_NAME_MAP_LENGTH)
  4348. {
  4349. print_space ();
  4350. ++len;
  4351. }
  4352. addr = s->output_offset;
  4353. if (s->output_section != NULL)
  4354. addr += s->output_section->vma;
  4355. minfo ("0x%V %W ", addr, TO_ADDR (s->size));
  4356. if (s->fill->size != 0)
  4357. {
  4358. size_t size;
  4359. unsigned char *p;
  4360. for (p = s->fill->data, size = s->fill->size; size != 0; p++, size--)
  4361. fprintf (config.map_file, "%02x", *p);
  4362. }
  4363. print_nl ();
  4364. print_dot = addr + TO_ADDR (s->size);
  4365. }
  4366. static void
  4367. print_wild_statement (lang_wild_statement_type *w,
  4368. lang_output_section_statement_type *os)
  4369. {
  4370. struct wildcard_list *sec;
  4371. print_space ();
  4372. if (w->exclude_name_list)
  4373. {
  4374. name_list *tmp;
  4375. minfo ("EXCLUDE_FILE(%s", w->exclude_name_list->name);
  4376. for (tmp = w->exclude_name_list->next; tmp; tmp = tmp->next)
  4377. minfo (" %s", tmp->name);
  4378. minfo (") ");
  4379. }
  4380. if (w->filenames_sorted)
  4381. minfo ("SORT_BY_NAME(");
  4382. if (w->filename != NULL)
  4383. minfo ("%s", w->filename);
  4384. else
  4385. minfo ("*");
  4386. if (w->filenames_sorted)
  4387. minfo (")");
  4388. minfo ("(");
  4389. for (sec = w->section_list; sec; sec = sec->next)
  4390. {
  4391. int closing_paren = 0;
  4392. switch (sec->spec.sorted)
  4393. {
  4394. case none:
  4395. break;
  4396. case by_name:
  4397. minfo ("SORT_BY_NAME(");
  4398. closing_paren = 1;
  4399. break;
  4400. case by_alignment:
  4401. minfo ("SORT_BY_ALIGNMENT(");
  4402. closing_paren = 1;
  4403. break;
  4404. case by_name_alignment:
  4405. minfo ("SORT_BY_NAME(SORT_BY_ALIGNMENT(");
  4406. closing_paren = 2;
  4407. break;
  4408. case by_alignment_name:
  4409. minfo ("SORT_BY_ALIGNMENT(SORT_BY_NAME(");
  4410. closing_paren = 2;
  4411. break;
  4412. case by_none:
  4413. minfo ("SORT_NONE(");
  4414. closing_paren = 1;
  4415. break;
  4416. case by_init_priority:
  4417. minfo ("SORT_BY_INIT_PRIORITY(");
  4418. closing_paren = 1;
  4419. break;
  4420. }
  4421. if (sec->spec.exclude_name_list != NULL)
  4422. {
  4423. name_list *tmp;
  4424. minfo ("EXCLUDE_FILE(%s", sec->spec.exclude_name_list->name);
  4425. for (tmp = sec->spec.exclude_name_list->next; tmp; tmp = tmp->next)
  4426. minfo (" %s", tmp->name);
  4427. minfo (") ");
  4428. }
  4429. if (sec->spec.name != NULL)
  4430. minfo ("%s", sec->spec.name);
  4431. else
  4432. minfo ("*");
  4433. for (;closing_paren > 0; closing_paren--)
  4434. minfo (")");
  4435. if (sec->next)
  4436. minfo (" ");
  4437. }
  4438. minfo (")");
  4439. print_nl ();
  4440. print_statement_list (w->children.head, os);
  4441. }
  4442. /* Print a group statement. */
  4443. static void
  4444. print_group (lang_group_statement_type *s,
  4445. lang_output_section_statement_type *os)
  4446. {
  4447. fprintf (config.map_file, "START GROUP\n");
  4448. print_statement_list (s->children.head, os);
  4449. fprintf (config.map_file, "END GROUP\n");
  4450. }
  4451. /* Print the list of statements in S.
  4452. This can be called for any statement type. */
  4453. static void
  4454. print_statement_list (lang_statement_union_type *s,
  4455. lang_output_section_statement_type *os)
  4456. {
  4457. while (s != NULL)
  4458. {
  4459. print_statement (s, os);
  4460. s = s->header.next;
  4461. }
  4462. }
  4463. /* Print the first statement in statement list S.
  4464. This can be called for any statement type. */
  4465. static void
  4466. print_statement (lang_statement_union_type *s,
  4467. lang_output_section_statement_type *os)
  4468. {
  4469. switch (s->header.type)
  4470. {
  4471. default:
  4472. fprintf (config.map_file, _("Fail with %d\n"), s->header.type);
  4473. FAIL ();
  4474. break;
  4475. case lang_constructors_statement_enum:
  4476. if (constructor_list.head != NULL)
  4477. {
  4478. if (constructors_sorted)
  4479. minfo (" SORT (CONSTRUCTORS)\n");
  4480. else
  4481. minfo (" CONSTRUCTORS\n");
  4482. print_statement_list (constructor_list.head, os);
  4483. }
  4484. break;
  4485. case lang_wild_statement_enum:
  4486. print_wild_statement (&s->wild_statement, os);
  4487. break;
  4488. case lang_address_statement_enum:
  4489. print_address_statement (&s->address_statement);
  4490. break;
  4491. case lang_object_symbols_statement_enum:
  4492. minfo (" CREATE_OBJECT_SYMBOLS\n");
  4493. break;
  4494. case lang_fill_statement_enum:
  4495. print_fill_statement (&s->fill_statement);
  4496. break;
  4497. case lang_data_statement_enum:
  4498. print_data_statement (&s->data_statement);
  4499. break;
  4500. case lang_reloc_statement_enum:
  4501. print_reloc_statement (&s->reloc_statement);
  4502. break;
  4503. case lang_input_section_enum:
  4504. print_input_section (s->input_section.section, false);
  4505. break;
  4506. case lang_padding_statement_enum:
  4507. print_padding_statement (&s->padding_statement);
  4508. break;
  4509. case lang_output_section_statement_enum:
  4510. print_output_section_statement (&s->output_section_statement);
  4511. break;
  4512. case lang_assignment_statement_enum:
  4513. print_assignment (&s->assignment_statement, os);
  4514. break;
  4515. case lang_target_statement_enum:
  4516. fprintf (config.map_file, "TARGET(%s)\n", s->target_statement.target);
  4517. break;
  4518. case lang_output_statement_enum:
  4519. minfo ("OUTPUT(%s", s->output_statement.name);
  4520. if (output_target != NULL)
  4521. minfo (" %s", output_target);
  4522. minfo (")\n");
  4523. break;
  4524. case lang_input_statement_enum:
  4525. print_input_statement (&s->input_statement);
  4526. break;
  4527. case lang_group_statement_enum:
  4528. print_group (&s->group_statement, os);
  4529. break;
  4530. case lang_insert_statement_enum:
  4531. minfo ("INSERT %s %s\n",
  4532. s->insert_statement.is_before ? "BEFORE" : "AFTER",
  4533. s->insert_statement.where);
  4534. break;
  4535. }
  4536. }
  4537. static void
  4538. print_statements (void)
  4539. {
  4540. print_statement_list (statement_list.head, abs_output_section);
  4541. }
  4542. /* Print the first N statements in statement list S to STDERR.
  4543. If N == 0, nothing is printed.
  4544. If N < 0, the entire list is printed.
  4545. Intended to be called from GDB. */
  4546. void
  4547. dprint_statement (lang_statement_union_type *s, int n)
  4548. {
  4549. FILE *map_save = config.map_file;
  4550. config.map_file = stderr;
  4551. if (n < 0)
  4552. print_statement_list (s, abs_output_section);
  4553. else
  4554. {
  4555. while (s && --n >= 0)
  4556. {
  4557. print_statement (s, abs_output_section);
  4558. s = s->header.next;
  4559. }
  4560. }
  4561. config.map_file = map_save;
  4562. }
  4563. static void
  4564. insert_pad (lang_statement_union_type **ptr,
  4565. fill_type *fill,
  4566. bfd_size_type alignment_needed,
  4567. asection *output_section,
  4568. bfd_vma dot)
  4569. {
  4570. static fill_type zero_fill;
  4571. lang_statement_union_type *pad = NULL;
  4572. if (ptr != &statement_list.head)
  4573. pad = ((lang_statement_union_type *)
  4574. ((char *) ptr - offsetof (lang_statement_union_type, header.next)));
  4575. if (pad != NULL
  4576. && pad->header.type == lang_padding_statement_enum
  4577. && pad->padding_statement.output_section == output_section)
  4578. {
  4579. /* Use the existing pad statement. */
  4580. }
  4581. else if ((pad = *ptr) != NULL
  4582. && pad->header.type == lang_padding_statement_enum
  4583. && pad->padding_statement.output_section == output_section)
  4584. {
  4585. /* Use the existing pad statement. */
  4586. }
  4587. else
  4588. {
  4589. /* Make a new padding statement, linked into existing chain. */
  4590. pad = stat_alloc (sizeof (lang_padding_statement_type));
  4591. pad->header.next = *ptr;
  4592. *ptr = pad;
  4593. pad->header.type = lang_padding_statement_enum;
  4594. pad->padding_statement.output_section = output_section;
  4595. if (fill == NULL)
  4596. fill = &zero_fill;
  4597. pad->padding_statement.fill = fill;
  4598. }
  4599. pad->padding_statement.output_offset = dot - output_section->vma;
  4600. pad->padding_statement.size = alignment_needed;
  4601. if (!(output_section->flags & SEC_FIXED_SIZE))
  4602. output_section->size = TO_SIZE (dot + TO_ADDR (alignment_needed)
  4603. - output_section->vma);
  4604. }
  4605. /* Work out how much this section will move the dot point. */
  4606. static bfd_vma
  4607. size_input_section
  4608. (lang_statement_union_type **this_ptr,
  4609. lang_output_section_statement_type *output_section_statement,
  4610. fill_type *fill,
  4611. bool *removed,
  4612. bfd_vma dot)
  4613. {
  4614. lang_input_section_type *is = &((*this_ptr)->input_section);
  4615. asection *i = is->section;
  4616. asection *o = output_section_statement->bfd_section;
  4617. *removed = 0;
  4618. if (link_info.non_contiguous_regions)
  4619. {
  4620. /* If the input section I has already been successfully assigned
  4621. to an output section other than O, don't bother with it and
  4622. let the caller remove it from the list. Keep processing in
  4623. case we have already handled O, because the repeated passes
  4624. have reinitialized its size. */
  4625. if (i->already_assigned && i->already_assigned != o)
  4626. {
  4627. *removed = 1;
  4628. return dot;
  4629. }
  4630. }
  4631. if (i->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
  4632. i->output_offset = i->vma - o->vma;
  4633. else if (((i->flags & SEC_EXCLUDE) != 0)
  4634. || output_section_statement->ignored)
  4635. i->output_offset = dot - o->vma;
  4636. else
  4637. {
  4638. bfd_size_type alignment_needed;
  4639. /* Align this section first to the input sections requirement,
  4640. then to the output section's requirement. If this alignment
  4641. is greater than any seen before, then record it too. Perform
  4642. the alignment by inserting a magic 'padding' statement. */
  4643. if (output_section_statement->subsection_alignment != NULL)
  4644. i->alignment_power
  4645. = exp_get_power (output_section_statement->subsection_alignment,
  4646. "subsection alignment");
  4647. if (o->alignment_power < i->alignment_power)
  4648. o->alignment_power = i->alignment_power;
  4649. alignment_needed = align_power (dot, i->alignment_power) - dot;
  4650. if (alignment_needed != 0)
  4651. {
  4652. insert_pad (this_ptr, fill, TO_SIZE (alignment_needed), o, dot);
  4653. dot += alignment_needed;
  4654. }
  4655. if (link_info.non_contiguous_regions)
  4656. {
  4657. /* If I would overflow O, let the caller remove I from the
  4658. list. */
  4659. if (output_section_statement->region)
  4660. {
  4661. bfd_vma end = output_section_statement->region->origin
  4662. + output_section_statement->region->length;
  4663. if (dot + TO_ADDR (i->size) > end)
  4664. {
  4665. if (i->flags & SEC_LINKER_CREATED)
  4666. einfo (_("%F%P: Output section '%s' not large enough for the "
  4667. "linker-created stubs section '%s'.\n"),
  4668. i->output_section->name, i->name);
  4669. if (i->rawsize && i->rawsize != i->size)
  4670. einfo (_("%F%P: Relaxation not supported with "
  4671. "--enable-non-contiguous-regions (section '%s' "
  4672. "would overflow '%s' after it changed size).\n"),
  4673. i->name, i->output_section->name);
  4674. *removed = 1;
  4675. dot = end;
  4676. i->output_section = NULL;
  4677. return dot;
  4678. }
  4679. }
  4680. }
  4681. /* Remember where in the output section this input section goes. */
  4682. i->output_offset = dot - o->vma;
  4683. /* Mark how big the output section must be to contain this now. */
  4684. dot += TO_ADDR (i->size);
  4685. if (!(o->flags & SEC_FIXED_SIZE))
  4686. o->size = TO_SIZE (dot - o->vma);
  4687. if (link_info.non_contiguous_regions)
  4688. {
  4689. /* Record that I was successfully assigned to O, and update
  4690. its actual output section too. */
  4691. i->already_assigned = o;
  4692. i->output_section = o;
  4693. }
  4694. }
  4695. return dot;
  4696. }
  4697. struct check_sec
  4698. {
  4699. asection *sec;
  4700. bool warned;
  4701. };
  4702. static int
  4703. sort_sections_by_lma (const void *arg1, const void *arg2)
  4704. {
  4705. const asection *sec1 = ((const struct check_sec *) arg1)->sec;
  4706. const asection *sec2 = ((const struct check_sec *) arg2)->sec;
  4707. if (sec1->lma < sec2->lma)
  4708. return -1;
  4709. else if (sec1->lma > sec2->lma)
  4710. return 1;
  4711. else if (sec1->id < sec2->id)
  4712. return -1;
  4713. else if (sec1->id > sec2->id)
  4714. return 1;
  4715. return 0;
  4716. }
  4717. static int
  4718. sort_sections_by_vma (const void *arg1, const void *arg2)
  4719. {
  4720. const asection *sec1 = ((const struct check_sec *) arg1)->sec;
  4721. const asection *sec2 = ((const struct check_sec *) arg2)->sec;
  4722. if (sec1->vma < sec2->vma)
  4723. return -1;
  4724. else if (sec1->vma > sec2->vma)
  4725. return 1;
  4726. else if (sec1->id < sec2->id)
  4727. return -1;
  4728. else if (sec1->id > sec2->id)
  4729. return 1;
  4730. return 0;
  4731. }
  4732. #define IS_TBSS(s) \
  4733. ((s->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == SEC_THREAD_LOCAL)
  4734. #define IGNORE_SECTION(s) \
  4735. ((s->flags & SEC_ALLOC) == 0 || IS_TBSS (s))
  4736. /* Check to see if any allocated sections overlap with other allocated
  4737. sections. This can happen if a linker script specifies the output
  4738. section addresses of the two sections. Also check whether any memory
  4739. region has overflowed. */
  4740. static void
  4741. lang_check_section_addresses (void)
  4742. {
  4743. asection *s, *p;
  4744. struct check_sec *sections;
  4745. size_t i, count;
  4746. bfd_vma addr_mask;
  4747. bfd_vma s_start;
  4748. bfd_vma s_end;
  4749. bfd_vma p_start = 0;
  4750. bfd_vma p_end = 0;
  4751. lang_memory_region_type *m;
  4752. bool overlays;
  4753. /* Detect address space overflow on allocated sections. */
  4754. addr_mask = ((bfd_vma) 1 <<
  4755. (bfd_arch_bits_per_address (link_info.output_bfd) - 1)) - 1;
  4756. addr_mask = (addr_mask << 1) + 1;
  4757. for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
  4758. if ((s->flags & SEC_ALLOC) != 0)
  4759. {
  4760. s_end = (s->vma + s->size) & addr_mask;
  4761. if (s_end != 0 && s_end < (s->vma & addr_mask))
  4762. einfo (_("%X%P: section %s VMA wraps around address space\n"),
  4763. s->name);
  4764. else
  4765. {
  4766. s_end = (s->lma + s->size) & addr_mask;
  4767. if (s_end != 0 && s_end < (s->lma & addr_mask))
  4768. einfo (_("%X%P: section %s LMA wraps around address space\n"),
  4769. s->name);
  4770. }
  4771. }
  4772. if (bfd_count_sections (link_info.output_bfd) <= 1)
  4773. return;
  4774. count = bfd_count_sections (link_info.output_bfd);
  4775. sections = XNEWVEC (struct check_sec, count);
  4776. /* Scan all sections in the output list. */
  4777. count = 0;
  4778. for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
  4779. {
  4780. if (IGNORE_SECTION (s)
  4781. || s->size == 0)
  4782. continue;
  4783. sections[count].sec = s;
  4784. sections[count].warned = false;
  4785. count++;
  4786. }
  4787. if (count <= 1)
  4788. {
  4789. free (sections);
  4790. return;
  4791. }
  4792. qsort (sections, count, sizeof (*sections), sort_sections_by_lma);
  4793. /* First check section LMAs. There should be no overlap of LMAs on
  4794. loadable sections, even with overlays. */
  4795. for (p = NULL, i = 0; i < count; i++)
  4796. {
  4797. s = sections[i].sec;
  4798. init_opb (s);
  4799. if ((s->flags & SEC_LOAD) != 0)
  4800. {
  4801. s_start = s->lma;
  4802. s_end = s_start + TO_ADDR (s->size) - 1;
  4803. /* Look for an overlap. We have sorted sections by lma, so
  4804. we know that s_start >= p_start. Besides the obvious
  4805. case of overlap when the current section starts before
  4806. the previous one ends, we also must have overlap if the
  4807. previous section wraps around the address space. */
  4808. if (p != NULL
  4809. && (s_start <= p_end
  4810. || p_end < p_start))
  4811. {
  4812. einfo (_("%X%P: section %s LMA [%V,%V]"
  4813. " overlaps section %s LMA [%V,%V]\n"),
  4814. s->name, s_start, s_end, p->name, p_start, p_end);
  4815. sections[i].warned = true;
  4816. }
  4817. p = s;
  4818. p_start = s_start;
  4819. p_end = s_end;
  4820. }
  4821. }
  4822. /* If any non-zero size allocated section (excluding tbss) starts at
  4823. exactly the same VMA as another such section, then we have
  4824. overlays. Overlays generated by the OVERLAY keyword will have
  4825. this property. It is possible to intentionally generate overlays
  4826. that fail this test, but it would be unusual. */
  4827. qsort (sections, count, sizeof (*sections), sort_sections_by_vma);
  4828. overlays = false;
  4829. p_start = sections[0].sec->vma;
  4830. for (i = 1; i < count; i++)
  4831. {
  4832. s_start = sections[i].sec->vma;
  4833. if (p_start == s_start)
  4834. {
  4835. overlays = true;
  4836. break;
  4837. }
  4838. p_start = s_start;
  4839. }
  4840. /* Now check section VMAs if no overlays were detected. */
  4841. if (!overlays)
  4842. {
  4843. for (p = NULL, i = 0; i < count; i++)
  4844. {
  4845. s = sections[i].sec;
  4846. init_opb (s);
  4847. s_start = s->vma;
  4848. s_end = s_start + TO_ADDR (s->size) - 1;
  4849. if (p != NULL
  4850. && !sections[i].warned
  4851. && (s_start <= p_end
  4852. || p_end < p_start))
  4853. einfo (_("%X%P: section %s VMA [%V,%V]"
  4854. " overlaps section %s VMA [%V,%V]\n"),
  4855. s->name, s_start, s_end, p->name, p_start, p_end);
  4856. p = s;
  4857. p_start = s_start;
  4858. p_end = s_end;
  4859. }
  4860. }
  4861. free (sections);
  4862. /* If any memory region has overflowed, report by how much.
  4863. We do not issue this diagnostic for regions that had sections
  4864. explicitly placed outside their bounds; os_region_check's
  4865. diagnostics are adequate for that case.
  4866. FIXME: It is conceivable that m->current - (m->origin + m->length)
  4867. might overflow a 32-bit integer. There is, alas, no way to print
  4868. a bfd_vma quantity in decimal. */
  4869. for (m = lang_memory_region_list; m; m = m->next)
  4870. if (m->had_full_message)
  4871. {
  4872. unsigned long over = m->current - (m->origin + m->length);
  4873. einfo (ngettext ("%X%P: region `%s' overflowed by %lu byte\n",
  4874. "%X%P: region `%s' overflowed by %lu bytes\n",
  4875. over),
  4876. m->name_list.name, over);
  4877. }
  4878. }
  4879. /* Make sure the new address is within the region. We explicitly permit the
  4880. current address to be at the exact end of the region when the address is
  4881. non-zero, in case the region is at the end of addressable memory and the
  4882. calculation wraps around. */
  4883. static void
  4884. os_region_check (lang_output_section_statement_type *os,
  4885. lang_memory_region_type *region,
  4886. etree_type *tree,
  4887. bfd_vma rbase)
  4888. {
  4889. if ((region->current < region->origin
  4890. || (region->current - region->origin > region->length))
  4891. && ((region->current != region->origin + region->length)
  4892. || rbase == 0))
  4893. {
  4894. if (tree != NULL)
  4895. {
  4896. einfo (_("%X%P: address 0x%v of %pB section `%s'"
  4897. " is not within region `%s'\n"),
  4898. region->current,
  4899. os->bfd_section->owner,
  4900. os->bfd_section->name,
  4901. region->name_list.name);
  4902. }
  4903. else if (!region->had_full_message)
  4904. {
  4905. region->had_full_message = true;
  4906. einfo (_("%X%P: %pB section `%s' will not fit in region `%s'\n"),
  4907. os->bfd_section->owner,
  4908. os->bfd_section->name,
  4909. region->name_list.name);
  4910. }
  4911. }
  4912. }
  4913. static void
  4914. ldlang_check_relro_region (lang_statement_union_type *s)
  4915. {
  4916. seg_align_type *seg = &expld.dataseg;
  4917. if (seg->relro == exp_seg_relro_start)
  4918. {
  4919. if (!seg->relro_start_stat)
  4920. seg->relro_start_stat = s;
  4921. else
  4922. {
  4923. ASSERT (seg->relro_start_stat == s);
  4924. }
  4925. }
  4926. else if (seg->relro == exp_seg_relro_end)
  4927. {
  4928. if (!seg->relro_end_stat)
  4929. seg->relro_end_stat = s;
  4930. else
  4931. {
  4932. ASSERT (seg->relro_end_stat == s);
  4933. }
  4934. }
  4935. }
  4936. /* Set the sizes for all the output sections. */
  4937. static bfd_vma
  4938. lang_size_sections_1
  4939. (lang_statement_union_type **prev,
  4940. lang_output_section_statement_type *output_section_statement,
  4941. fill_type *fill,
  4942. bfd_vma dot,
  4943. bool *relax,
  4944. bool check_regions)
  4945. {
  4946. lang_statement_union_type *s;
  4947. lang_statement_union_type *prev_s = NULL;
  4948. bool removed_prev_s = false;
  4949. /* Size up the sections from their constituent parts. */
  4950. for (s = *prev; s != NULL; prev_s = s, s = s->header.next)
  4951. {
  4952. bool removed = false;
  4953. switch (s->header.type)
  4954. {
  4955. case lang_output_section_statement_enum:
  4956. {
  4957. bfd_vma newdot, after, dotdelta;
  4958. lang_output_section_statement_type *os;
  4959. lang_memory_region_type *r;
  4960. int section_alignment = 0;
  4961. os = &s->output_section_statement;
  4962. init_opb (os->bfd_section);
  4963. if (os->constraint == -1)
  4964. break;
  4965. /* FIXME: We shouldn't need to zero section vmas for ld -r
  4966. here, in lang_insert_orphan, or in the default linker scripts.
  4967. This is covering for coff backend linker bugs. See PR6945. */
  4968. if (os->addr_tree == NULL
  4969. && bfd_link_relocatable (&link_info)
  4970. && (bfd_get_flavour (link_info.output_bfd)
  4971. == bfd_target_coff_flavour))
  4972. os->addr_tree = exp_intop (0);
  4973. if (os->addr_tree != NULL)
  4974. {
  4975. os->processed_vma = false;
  4976. exp_fold_tree (os->addr_tree, bfd_abs_section_ptr, &dot);
  4977. if (expld.result.valid_p)
  4978. {
  4979. dot = expld.result.value;
  4980. if (expld.result.section != NULL)
  4981. dot += expld.result.section->vma;
  4982. }
  4983. else if (expld.phase != lang_mark_phase_enum)
  4984. einfo (_("%F%P:%pS: non constant or forward reference"
  4985. " address expression for section %s\n"),
  4986. os->addr_tree, os->name);
  4987. }
  4988. if (os->bfd_section == NULL)
  4989. /* This section was removed or never actually created. */
  4990. break;
  4991. /* If this is a COFF shared library section, use the size and
  4992. address from the input section. FIXME: This is COFF
  4993. specific; it would be cleaner if there were some other way
  4994. to do this, but nothing simple comes to mind. */
  4995. if (((bfd_get_flavour (link_info.output_bfd)
  4996. == bfd_target_ecoff_flavour)
  4997. || (bfd_get_flavour (link_info.output_bfd)
  4998. == bfd_target_coff_flavour))
  4999. && (os->bfd_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
  5000. {
  5001. asection *input;
  5002. if (os->children.head == NULL
  5003. || os->children.head->header.next != NULL
  5004. || (os->children.head->header.type
  5005. != lang_input_section_enum))
  5006. einfo (_("%X%P: internal error on COFF shared library"
  5007. " section %s\n"), os->name);
  5008. input = os->children.head->input_section.section;
  5009. bfd_set_section_vma (os->bfd_section,
  5010. bfd_section_vma (input));
  5011. if (!(os->bfd_section->flags & SEC_FIXED_SIZE))
  5012. os->bfd_section->size = input->size;
  5013. break;
  5014. }
  5015. newdot = dot;
  5016. dotdelta = 0;
  5017. if (bfd_is_abs_section (os->bfd_section))
  5018. {
  5019. /* No matter what happens, an abs section starts at zero. */
  5020. ASSERT (os->bfd_section->vma == 0);
  5021. }
  5022. else
  5023. {
  5024. if (os->addr_tree == NULL)
  5025. {
  5026. /* No address specified for this section, get one
  5027. from the region specification. */
  5028. if (os->region == NULL
  5029. || ((os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD))
  5030. && os->region->name_list.name[0] == '*'
  5031. && strcmp (os->region->name_list.name,
  5032. DEFAULT_MEMORY_REGION) == 0))
  5033. {
  5034. os->region = lang_memory_default (os->bfd_section);
  5035. }
  5036. /* If a loadable section is using the default memory
  5037. region, and some non default memory regions were
  5038. defined, issue an error message. */
  5039. if (!os->ignored
  5040. && !IGNORE_SECTION (os->bfd_section)
  5041. && !bfd_link_relocatable (&link_info)
  5042. && check_regions
  5043. && strcmp (os->region->name_list.name,
  5044. DEFAULT_MEMORY_REGION) == 0
  5045. && lang_memory_region_list != NULL
  5046. && (strcmp (lang_memory_region_list->name_list.name,
  5047. DEFAULT_MEMORY_REGION) != 0
  5048. || lang_memory_region_list->next != NULL)
  5049. && lang_sizing_iteration == 1)
  5050. {
  5051. /* By default this is an error rather than just a
  5052. warning because if we allocate the section to the
  5053. default memory region we can end up creating an
  5054. excessively large binary, or even seg faulting when
  5055. attempting to perform a negative seek. See
  5056. sources.redhat.com/ml/binutils/2003-04/msg00423.html
  5057. for an example of this. This behaviour can be
  5058. overridden by the using the --no-check-sections
  5059. switch. */
  5060. if (command_line.check_section_addresses)
  5061. einfo (_("%F%P: error: no memory region specified"
  5062. " for loadable section `%s'\n"),
  5063. bfd_section_name (os->bfd_section));
  5064. else
  5065. einfo (_("%P: warning: no memory region specified"
  5066. " for loadable section `%s'\n"),
  5067. bfd_section_name (os->bfd_section));
  5068. }
  5069. newdot = os->region->current;
  5070. section_alignment = os->bfd_section->alignment_power;
  5071. }
  5072. else
  5073. section_alignment = exp_get_power (os->section_alignment,
  5074. "section alignment");
  5075. /* Align to what the section needs. */
  5076. if (section_alignment > 0)
  5077. {
  5078. bfd_vma savedot = newdot;
  5079. bfd_vma diff = 0;
  5080. newdot = align_power (newdot, section_alignment);
  5081. dotdelta = newdot - savedot;
  5082. if (lang_sizing_iteration == 1)
  5083. diff = dotdelta;
  5084. else if (lang_sizing_iteration > 1)
  5085. {
  5086. /* Only report adjustments that would change
  5087. alignment from what we have already reported. */
  5088. diff = newdot - os->bfd_section->vma;
  5089. if (!(diff & (((bfd_vma) 1 << section_alignment) - 1)))
  5090. diff = 0;
  5091. }
  5092. if (diff != 0
  5093. && (config.warn_section_align
  5094. || os->addr_tree != NULL))
  5095. einfo (_("%P: warning: "
  5096. "start of section %s changed by %ld\n"),
  5097. os->name, (long) diff);
  5098. }
  5099. bfd_set_section_vma (os->bfd_section, newdot);
  5100. os->bfd_section->output_offset = 0;
  5101. }
  5102. lang_size_sections_1 (&os->children.head, os,
  5103. os->fill, newdot, relax, check_regions);
  5104. os->processed_vma = true;
  5105. if (bfd_is_abs_section (os->bfd_section) || os->ignored)
  5106. /* Except for some special linker created sections,
  5107. no output section should change from zero size
  5108. after strip_excluded_output_sections. A non-zero
  5109. size on an ignored section indicates that some
  5110. input section was not sized early enough. */
  5111. ASSERT (os->bfd_section->size == 0);
  5112. else
  5113. {
  5114. dot = os->bfd_section->vma;
  5115. /* Put the section within the requested block size, or
  5116. align at the block boundary. */
  5117. after = ((dot
  5118. + TO_ADDR (os->bfd_section->size)
  5119. + os->block_value - 1)
  5120. & - (bfd_vma) os->block_value);
  5121. if (!(os->bfd_section->flags & SEC_FIXED_SIZE))
  5122. os->bfd_section->size = TO_SIZE (after
  5123. - os->bfd_section->vma);
  5124. }
  5125. /* Set section lma. */
  5126. r = os->region;
  5127. if (r == NULL)
  5128. r = lang_memory_region_lookup (DEFAULT_MEMORY_REGION, false);
  5129. if (os->load_base)
  5130. {
  5131. bfd_vma lma = exp_get_abs_int (os->load_base, 0, "load base");
  5132. os->bfd_section->lma = lma;
  5133. }
  5134. else if (os->lma_region != NULL)
  5135. {
  5136. bfd_vma lma = os->lma_region->current;
  5137. if (os->align_lma_with_input)
  5138. lma += dotdelta;
  5139. else
  5140. {
  5141. /* When LMA_REGION is the same as REGION, align the LMA
  5142. as we did for the VMA, possibly including alignment
  5143. from the bfd section. If a different region, then
  5144. only align according to the value in the output
  5145. statement. */
  5146. if (os->lma_region != os->region)
  5147. section_alignment = exp_get_power (os->section_alignment,
  5148. "section alignment");
  5149. if (section_alignment > 0)
  5150. lma = align_power (lma, section_alignment);
  5151. }
  5152. os->bfd_section->lma = lma;
  5153. }
  5154. else if (r->last_os != NULL
  5155. && (os->bfd_section->flags & SEC_ALLOC) != 0)
  5156. {
  5157. bfd_vma lma;
  5158. asection *last;
  5159. last = r->last_os->output_section_statement.bfd_section;
  5160. /* A backwards move of dot should be accompanied by
  5161. an explicit assignment to the section LMA (ie.
  5162. os->load_base set) because backwards moves can
  5163. create overlapping LMAs. */
  5164. if (dot < last->vma
  5165. && os->bfd_section->size != 0
  5166. && dot + TO_ADDR (os->bfd_section->size) <= last->vma)
  5167. {
  5168. /* If dot moved backwards then leave lma equal to
  5169. vma. This is the old default lma, which might
  5170. just happen to work when the backwards move is
  5171. sufficiently large. Nag if this changes anything,
  5172. so people can fix their linker scripts. */
  5173. if (last->vma != last->lma)
  5174. einfo (_("%P: warning: dot moved backwards "
  5175. "before `%s'\n"), os->name);
  5176. }
  5177. else
  5178. {
  5179. /* If this is an overlay, set the current lma to that
  5180. at the end of the previous section. */
  5181. if (os->sectype == overlay_section)
  5182. lma = last->lma + TO_ADDR (last->size);
  5183. /* Otherwise, keep the same lma to vma relationship
  5184. as the previous section. */
  5185. else
  5186. lma = os->bfd_section->vma + last->lma - last->vma;
  5187. if (section_alignment > 0)
  5188. lma = align_power (lma, section_alignment);
  5189. os->bfd_section->lma = lma;
  5190. }
  5191. }
  5192. os->processed_lma = true;
  5193. /* Keep track of normal sections using the default
  5194. lma region. We use this to set the lma for
  5195. following sections. Overlays or other linker
  5196. script assignment to lma might mean that the
  5197. default lma == vma is incorrect.
  5198. To avoid warnings about dot moving backwards when using
  5199. -Ttext, don't start tracking sections until we find one
  5200. of non-zero size or with lma set differently to vma.
  5201. Do this tracking before we short-cut the loop so that we
  5202. track changes for the case where the section size is zero,
  5203. but the lma is set differently to the vma. This is
  5204. important, if an orphan section is placed after an
  5205. otherwise empty output section that has an explicit lma
  5206. set, we want that lma reflected in the orphans lma. */
  5207. if (((!IGNORE_SECTION (os->bfd_section)
  5208. && (os->bfd_section->size != 0
  5209. || (r->last_os == NULL
  5210. && os->bfd_section->vma != os->bfd_section->lma)
  5211. || (r->last_os != NULL
  5212. && dot >= (r->last_os->output_section_statement
  5213. .bfd_section->vma))))
  5214. || os->sectype == first_overlay_section)
  5215. && os->lma_region == NULL
  5216. && !bfd_link_relocatable (&link_info))
  5217. r->last_os = s;
  5218. if (bfd_is_abs_section (os->bfd_section) || os->ignored)
  5219. break;
  5220. /* .tbss sections effectively have zero size. */
  5221. if (!IS_TBSS (os->bfd_section)
  5222. || bfd_link_relocatable (&link_info))
  5223. dotdelta = TO_ADDR (os->bfd_section->size);
  5224. else
  5225. dotdelta = 0;
  5226. dot += dotdelta;
  5227. if (os->update_dot_tree != 0)
  5228. exp_fold_tree (os->update_dot_tree, bfd_abs_section_ptr, &dot);
  5229. /* Update dot in the region ?
  5230. We only do this if the section is going to be allocated,
  5231. since unallocated sections do not contribute to the region's
  5232. overall size in memory. */
  5233. if (os->region != NULL
  5234. && (os->bfd_section->flags & (SEC_ALLOC | SEC_LOAD)))
  5235. {
  5236. os->region->current = dot;
  5237. if (check_regions)
  5238. /* Make sure the new address is within the region. */
  5239. os_region_check (os, os->region, os->addr_tree,
  5240. os->bfd_section->vma);
  5241. if (os->lma_region != NULL && os->lma_region != os->region
  5242. && ((os->bfd_section->flags & SEC_LOAD)
  5243. || os->align_lma_with_input))
  5244. {
  5245. os->lma_region->current = os->bfd_section->lma + dotdelta;
  5246. if (check_regions)
  5247. os_region_check (os, os->lma_region, NULL,
  5248. os->bfd_section->lma);
  5249. }
  5250. }
  5251. }
  5252. break;
  5253. case lang_constructors_statement_enum:
  5254. dot = lang_size_sections_1 (&constructor_list.head,
  5255. output_section_statement,
  5256. fill, dot, relax, check_regions);
  5257. break;
  5258. case lang_data_statement_enum:
  5259. {
  5260. unsigned int size = 0;
  5261. s->data_statement.output_offset =
  5262. dot - output_section_statement->bfd_section->vma;
  5263. s->data_statement.output_section =
  5264. output_section_statement->bfd_section;
  5265. /* We might refer to provided symbols in the expression, and
  5266. need to mark them as needed. */
  5267. exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
  5268. switch (s->data_statement.type)
  5269. {
  5270. default:
  5271. abort ();
  5272. case QUAD:
  5273. case SQUAD:
  5274. size = QUAD_SIZE;
  5275. break;
  5276. case LONG:
  5277. size = LONG_SIZE;
  5278. break;
  5279. case SHORT:
  5280. size = SHORT_SIZE;
  5281. break;
  5282. case BYTE:
  5283. size = BYTE_SIZE;
  5284. break;
  5285. }
  5286. if (size < TO_SIZE ((unsigned) 1))
  5287. size = TO_SIZE ((unsigned) 1);
  5288. dot += TO_ADDR (size);
  5289. if (!(output_section_statement->bfd_section->flags
  5290. & SEC_FIXED_SIZE))
  5291. output_section_statement->bfd_section->size
  5292. = TO_SIZE (dot - output_section_statement->bfd_section->vma);
  5293. }
  5294. break;
  5295. case lang_reloc_statement_enum:
  5296. {
  5297. int size;
  5298. s->reloc_statement.output_offset =
  5299. dot - output_section_statement->bfd_section->vma;
  5300. s->reloc_statement.output_section =
  5301. output_section_statement->bfd_section;
  5302. size = bfd_get_reloc_size (s->reloc_statement.howto);
  5303. dot += TO_ADDR (size);
  5304. if (!(output_section_statement->bfd_section->flags
  5305. & SEC_FIXED_SIZE))
  5306. output_section_statement->bfd_section->size
  5307. = TO_SIZE (dot - output_section_statement->bfd_section->vma);
  5308. }
  5309. break;
  5310. case lang_wild_statement_enum:
  5311. dot = lang_size_sections_1 (&s->wild_statement.children.head,
  5312. output_section_statement,
  5313. fill, dot, relax, check_regions);
  5314. break;
  5315. case lang_object_symbols_statement_enum:
  5316. link_info.create_object_symbols_section
  5317. = output_section_statement->bfd_section;
  5318. output_section_statement->bfd_section->flags |= SEC_KEEP;
  5319. break;
  5320. case lang_output_statement_enum:
  5321. case lang_target_statement_enum:
  5322. break;
  5323. case lang_input_section_enum:
  5324. {
  5325. asection *i;
  5326. i = s->input_section.section;
  5327. if (relax)
  5328. {
  5329. bool again;
  5330. if (!bfd_relax_section (i->owner, i, &link_info, &again))
  5331. einfo (_("%F%P: can't relax section: %E\n"));
  5332. if (again)
  5333. *relax = true;
  5334. }
  5335. dot = size_input_section (prev, output_section_statement,
  5336. fill, &removed, dot);
  5337. }
  5338. break;
  5339. case lang_input_statement_enum:
  5340. break;
  5341. case lang_fill_statement_enum:
  5342. s->fill_statement.output_section =
  5343. output_section_statement->bfd_section;
  5344. fill = s->fill_statement.fill;
  5345. break;
  5346. case lang_assignment_statement_enum:
  5347. {
  5348. bfd_vma newdot = dot;
  5349. etree_type *tree = s->assignment_statement.exp;
  5350. expld.dataseg.relro = exp_seg_relro_none;
  5351. exp_fold_tree (tree,
  5352. output_section_statement->bfd_section,
  5353. &newdot);
  5354. ldlang_check_relro_region (s);
  5355. expld.dataseg.relro = exp_seg_relro_none;
  5356. /* This symbol may be relative to this section. */
  5357. if ((tree->type.node_class == etree_provided
  5358. || tree->type.node_class == etree_assign)
  5359. && (tree->assign.dst [0] != '.'
  5360. || tree->assign.dst [1] != '\0'))
  5361. output_section_statement->update_dot = 1;
  5362. if (!output_section_statement->ignored)
  5363. {
  5364. if (output_section_statement == abs_output_section)
  5365. {
  5366. /* If we don't have an output section, then just adjust
  5367. the default memory address. */
  5368. lang_memory_region_lookup (DEFAULT_MEMORY_REGION,
  5369. false)->current = newdot;
  5370. }
  5371. else if (newdot != dot)
  5372. {
  5373. /* Insert a pad after this statement. We can't
  5374. put the pad before when relaxing, in case the
  5375. assignment references dot. */
  5376. insert_pad (&s->header.next, fill, TO_SIZE (newdot - dot),
  5377. output_section_statement->bfd_section, dot);
  5378. /* Don't neuter the pad below when relaxing. */
  5379. s = s->header.next;
  5380. /* If dot is advanced, this implies that the section
  5381. should have space allocated to it, unless the
  5382. user has explicitly stated that the section
  5383. should not be allocated. */
  5384. if (output_section_statement->sectype != noalloc_section
  5385. && (output_section_statement->sectype != noload_section
  5386. || (bfd_get_flavour (link_info.output_bfd)
  5387. == bfd_target_elf_flavour)))
  5388. output_section_statement->bfd_section->flags |= SEC_ALLOC;
  5389. }
  5390. dot = newdot;
  5391. }
  5392. }
  5393. break;
  5394. case lang_padding_statement_enum:
  5395. /* If this is the first time lang_size_sections is called,
  5396. we won't have any padding statements. If this is the
  5397. second or later passes when relaxing, we should allow
  5398. padding to shrink. If padding is needed on this pass, it
  5399. will be added back in. */
  5400. s->padding_statement.size = 0;
  5401. /* Make sure output_offset is valid. If relaxation shrinks
  5402. the section and this pad isn't needed, it's possible to
  5403. have output_offset larger than the final size of the
  5404. section. bfd_set_section_contents will complain even for
  5405. a pad size of zero. */
  5406. s->padding_statement.output_offset
  5407. = dot - output_section_statement->bfd_section->vma;
  5408. break;
  5409. case lang_group_statement_enum:
  5410. dot = lang_size_sections_1 (&s->group_statement.children.head,
  5411. output_section_statement,
  5412. fill, dot, relax, check_regions);
  5413. break;
  5414. case lang_insert_statement_enum:
  5415. break;
  5416. /* We can only get here when relaxing is turned on. */
  5417. case lang_address_statement_enum:
  5418. break;
  5419. default:
  5420. FAIL ();
  5421. break;
  5422. }
  5423. /* If an input section doesn't fit in the current output
  5424. section, remove it from the list. Handle the case where we
  5425. have to remove an input_section statement here: there is a
  5426. special case to remove the first element of the list. */
  5427. if (link_info.non_contiguous_regions && removed)
  5428. {
  5429. /* If we removed the first element during the previous
  5430. iteration, override the loop assignment of prev_s. */
  5431. if (removed_prev_s)
  5432. prev_s = NULL;
  5433. if (prev_s)
  5434. {
  5435. /* If there was a real previous input section, just skip
  5436. the current one. */
  5437. prev_s->header.next=s->header.next;
  5438. s = prev_s;
  5439. removed_prev_s = false;
  5440. }
  5441. else
  5442. {
  5443. /* Remove the first input section of the list. */
  5444. *prev = s->header.next;
  5445. removed_prev_s = true;
  5446. }
  5447. /* Move to next element, unless we removed the head of the
  5448. list. */
  5449. if (!removed_prev_s)
  5450. prev = &s->header.next;
  5451. }
  5452. else
  5453. {
  5454. prev = &s->header.next;
  5455. removed_prev_s = false;
  5456. }
  5457. }
  5458. return dot;
  5459. }
  5460. /* Callback routine that is used in _bfd_elf_map_sections_to_segments.
  5461. The BFD library has set NEW_SEGMENT to TRUE iff it thinks that
  5462. CURRENT_SECTION and PREVIOUS_SECTION ought to be placed into different
  5463. segments. We are allowed an opportunity to override this decision. */
  5464. bool
  5465. ldlang_override_segment_assignment (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  5466. bfd *abfd ATTRIBUTE_UNUSED,
  5467. asection *current_section,
  5468. asection *previous_section,
  5469. bool new_segment)
  5470. {
  5471. lang_output_section_statement_type *cur;
  5472. lang_output_section_statement_type *prev;
  5473. /* The checks below are only necessary when the BFD library has decided
  5474. that the two sections ought to be placed into the same segment. */
  5475. if (new_segment)
  5476. return true;
  5477. /* Paranoia checks. */
  5478. if (current_section == NULL || previous_section == NULL)
  5479. return new_segment;
  5480. /* If this flag is set, the target never wants code and non-code
  5481. sections comingled in the same segment. */
  5482. if (config.separate_code
  5483. && ((current_section->flags ^ previous_section->flags) & SEC_CODE))
  5484. return true;
  5485. /* Find the memory regions associated with the two sections.
  5486. We call lang_output_section_find() here rather than scanning the list
  5487. of output sections looking for a matching section pointer because if
  5488. we have a large number of sections then a hash lookup is faster. */
  5489. cur = lang_output_section_find (current_section->name);
  5490. prev = lang_output_section_find (previous_section->name);
  5491. /* More paranoia. */
  5492. if (cur == NULL || prev == NULL)
  5493. return new_segment;
  5494. /* If the regions are different then force the sections to live in
  5495. different segments. See the email thread starting at the following
  5496. URL for the reasons why this is necessary:
  5497. http://sourceware.org/ml/binutils/2007-02/msg00216.html */
  5498. return cur->region != prev->region;
  5499. }
  5500. void
  5501. one_lang_size_sections_pass (bool *relax, bool check_regions)
  5502. {
  5503. lang_statement_iteration++;
  5504. if (expld.phase != lang_mark_phase_enum)
  5505. lang_sizing_iteration++;
  5506. lang_size_sections_1 (&statement_list.head, abs_output_section,
  5507. 0, 0, relax, check_regions);
  5508. }
  5509. static bool
  5510. lang_size_segment (void)
  5511. {
  5512. /* If XXX_SEGMENT_ALIGN XXX_SEGMENT_END pair was seen, check whether
  5513. a page could be saved in the data segment. */
  5514. seg_align_type *seg = &expld.dataseg;
  5515. bfd_vma first, last;
  5516. first = -seg->base & (seg->commonpagesize - 1);
  5517. last = seg->end & (seg->commonpagesize - 1);
  5518. if (first && last
  5519. && ((seg->base & ~(seg->commonpagesize - 1))
  5520. != (seg->end & ~(seg->commonpagesize - 1)))
  5521. && first + last <= seg->commonpagesize)
  5522. {
  5523. seg->phase = exp_seg_adjust;
  5524. return true;
  5525. }
  5526. seg->phase = exp_seg_done;
  5527. return false;
  5528. }
  5529. static bfd_vma
  5530. lang_size_relro_segment_1 (void)
  5531. {
  5532. seg_align_type *seg = &expld.dataseg;
  5533. bfd_vma relro_end, desired_end;
  5534. asection *sec;
  5535. /* Compute the expected PT_GNU_RELRO/PT_LOAD segment end. */
  5536. relro_end = (seg->relro_end + seg->relropagesize - 1) & -seg->relropagesize;
  5537. /* Adjust by the offset arg of XXX_SEGMENT_RELRO_END. */
  5538. desired_end = relro_end - seg->relro_offset;
  5539. /* For sections in the relro segment.. */
  5540. for (sec = link_info.output_bfd->section_last; sec; sec = sec->prev)
  5541. if ((sec->flags & SEC_ALLOC) != 0
  5542. && sec->vma >= seg->base
  5543. && sec->vma < seg->relro_end - seg->relro_offset)
  5544. {
  5545. /* Where do we want to put this section so that it ends as
  5546. desired? */
  5547. bfd_vma start, end, bump;
  5548. end = start = sec->vma;
  5549. if (!IS_TBSS (sec))
  5550. end += TO_ADDR (sec->size);
  5551. bump = desired_end - end;
  5552. /* We'd like to increase START by BUMP, but we must heed
  5553. alignment so the increase might be less than optimum. */
  5554. start += bump;
  5555. start &= ~(((bfd_vma) 1 << sec->alignment_power) - 1);
  5556. /* This is now the desired end for the previous section. */
  5557. desired_end = start;
  5558. }
  5559. seg->phase = exp_seg_relro_adjust;
  5560. ASSERT (desired_end >= seg->base);
  5561. seg->base = desired_end;
  5562. return relro_end;
  5563. }
  5564. static bool
  5565. lang_size_relro_segment (bool *relax, bool check_regions)
  5566. {
  5567. bool do_reset = false;
  5568. if (link_info.relro && expld.dataseg.relro_end)
  5569. {
  5570. bfd_vma data_initial_base = expld.dataseg.base;
  5571. bfd_vma data_relro_end = lang_size_relro_segment_1 ();
  5572. lang_reset_memory_regions ();
  5573. one_lang_size_sections_pass (relax, check_regions);
  5574. /* Assignments to dot, or to output section address in a user
  5575. script have increased padding over the original. Revert. */
  5576. if (expld.dataseg.relro_end > data_relro_end)
  5577. {
  5578. expld.dataseg.base = data_initial_base;
  5579. do_reset = true;
  5580. }
  5581. }
  5582. else if (lang_size_segment ())
  5583. do_reset = true;
  5584. return do_reset;
  5585. }
  5586. void
  5587. lang_size_sections (bool *relax, bool check_regions)
  5588. {
  5589. expld.phase = lang_allocating_phase_enum;
  5590. expld.dataseg.phase = exp_seg_none;
  5591. one_lang_size_sections_pass (relax, check_regions);
  5592. if (expld.dataseg.phase != exp_seg_end_seen)
  5593. expld.dataseg.phase = exp_seg_done;
  5594. if (expld.dataseg.phase == exp_seg_end_seen)
  5595. {
  5596. bool do_reset
  5597. = lang_size_relro_segment (relax, check_regions);
  5598. if (do_reset)
  5599. {
  5600. lang_reset_memory_regions ();
  5601. one_lang_size_sections_pass (relax, check_regions);
  5602. }
  5603. if (link_info.relro && expld.dataseg.relro_end)
  5604. {
  5605. link_info.relro_start = expld.dataseg.base;
  5606. link_info.relro_end = expld.dataseg.relro_end;
  5607. }
  5608. }
  5609. }
  5610. static lang_output_section_statement_type *current_section;
  5611. static lang_assignment_statement_type *current_assign;
  5612. static bool prefer_next_section;
  5613. /* Worker function for lang_do_assignments. Recursiveness goes here. */
  5614. static bfd_vma
  5615. lang_do_assignments_1 (lang_statement_union_type *s,
  5616. lang_output_section_statement_type *current_os,
  5617. fill_type *fill,
  5618. bfd_vma dot,
  5619. bool *found_end)
  5620. {
  5621. for (; s != NULL; s = s->header.next)
  5622. {
  5623. switch (s->header.type)
  5624. {
  5625. case lang_constructors_statement_enum:
  5626. dot = lang_do_assignments_1 (constructor_list.head,
  5627. current_os, fill, dot, found_end);
  5628. break;
  5629. case lang_output_section_statement_enum:
  5630. {
  5631. lang_output_section_statement_type *os;
  5632. bfd_vma newdot;
  5633. os = &(s->output_section_statement);
  5634. os->after_end = *found_end;
  5635. init_opb (os->bfd_section);
  5636. newdot = dot;
  5637. if (os->bfd_section != NULL)
  5638. {
  5639. if (!os->ignored && (os->bfd_section->flags & SEC_ALLOC) != 0)
  5640. {
  5641. current_section = os;
  5642. prefer_next_section = false;
  5643. }
  5644. newdot = os->bfd_section->vma;
  5645. }
  5646. newdot = lang_do_assignments_1 (os->children.head,
  5647. os, os->fill, newdot, found_end);
  5648. if (!os->ignored)
  5649. {
  5650. if (os->bfd_section != NULL)
  5651. {
  5652. newdot = os->bfd_section->vma;
  5653. /* .tbss sections effectively have zero size. */
  5654. if (!IS_TBSS (os->bfd_section)
  5655. || bfd_link_relocatable (&link_info))
  5656. newdot += TO_ADDR (os->bfd_section->size);
  5657. if (os->update_dot_tree != NULL)
  5658. exp_fold_tree (os->update_dot_tree,
  5659. bfd_abs_section_ptr, &newdot);
  5660. }
  5661. dot = newdot;
  5662. }
  5663. }
  5664. break;
  5665. case lang_wild_statement_enum:
  5666. dot = lang_do_assignments_1 (s->wild_statement.children.head,
  5667. current_os, fill, dot, found_end);
  5668. break;
  5669. case lang_object_symbols_statement_enum:
  5670. case lang_output_statement_enum:
  5671. case lang_target_statement_enum:
  5672. break;
  5673. case lang_data_statement_enum:
  5674. exp_fold_tree (s->data_statement.exp, bfd_abs_section_ptr, &dot);
  5675. if (expld.result.valid_p)
  5676. {
  5677. s->data_statement.value = expld.result.value;
  5678. if (expld.result.section != NULL)
  5679. s->data_statement.value += expld.result.section->vma;
  5680. }
  5681. else if (expld.phase == lang_final_phase_enum)
  5682. einfo (_("%F%P: invalid data statement\n"));
  5683. {
  5684. unsigned int size;
  5685. switch (s->data_statement.type)
  5686. {
  5687. default:
  5688. abort ();
  5689. case QUAD:
  5690. case SQUAD:
  5691. size = QUAD_SIZE;
  5692. break;
  5693. case LONG:
  5694. size = LONG_SIZE;
  5695. break;
  5696. case SHORT:
  5697. size = SHORT_SIZE;
  5698. break;
  5699. case BYTE:
  5700. size = BYTE_SIZE;
  5701. break;
  5702. }
  5703. if (size < TO_SIZE ((unsigned) 1))
  5704. size = TO_SIZE ((unsigned) 1);
  5705. dot += TO_ADDR (size);
  5706. }
  5707. break;
  5708. case lang_reloc_statement_enum:
  5709. exp_fold_tree (s->reloc_statement.addend_exp,
  5710. bfd_abs_section_ptr, &dot);
  5711. if (expld.result.valid_p)
  5712. s->reloc_statement.addend_value = expld.result.value;
  5713. else if (expld.phase == lang_final_phase_enum)
  5714. einfo (_("%F%P: invalid reloc statement\n"));
  5715. dot += TO_ADDR (bfd_get_reloc_size (s->reloc_statement.howto));
  5716. break;
  5717. case lang_input_section_enum:
  5718. {
  5719. asection *in = s->input_section.section;
  5720. if ((in->flags & SEC_EXCLUDE) == 0)
  5721. dot += TO_ADDR (in->size);
  5722. }
  5723. break;
  5724. case lang_input_statement_enum:
  5725. break;
  5726. case lang_fill_statement_enum:
  5727. fill = s->fill_statement.fill;
  5728. break;
  5729. case lang_assignment_statement_enum:
  5730. current_assign = &s->assignment_statement;
  5731. if (current_assign->exp->type.node_class != etree_assert)
  5732. {
  5733. const char *p = current_assign->exp->assign.dst;
  5734. if (current_os == abs_output_section && p[0] == '.' && p[1] == 0)
  5735. prefer_next_section = true;
  5736. while (*p == '_')
  5737. ++p;
  5738. if (strcmp (p, "end") == 0)
  5739. *found_end = true;
  5740. }
  5741. exp_fold_tree (s->assignment_statement.exp,
  5742. (current_os->bfd_section != NULL
  5743. ? current_os->bfd_section : bfd_und_section_ptr),
  5744. &dot);
  5745. break;
  5746. case lang_padding_statement_enum:
  5747. dot += TO_ADDR (s->padding_statement.size);
  5748. break;
  5749. case lang_group_statement_enum:
  5750. dot = lang_do_assignments_1 (s->group_statement.children.head,
  5751. current_os, fill, dot, found_end);
  5752. break;
  5753. case lang_insert_statement_enum:
  5754. break;
  5755. case lang_address_statement_enum:
  5756. break;
  5757. default:
  5758. FAIL ();
  5759. break;
  5760. }
  5761. }
  5762. return dot;
  5763. }
  5764. void
  5765. lang_do_assignments (lang_phase_type phase)
  5766. {
  5767. bool found_end = false;
  5768. current_section = NULL;
  5769. prefer_next_section = false;
  5770. expld.phase = phase;
  5771. lang_statement_iteration++;
  5772. lang_do_assignments_1 (statement_list.head,
  5773. abs_output_section, NULL, 0, &found_end);
  5774. }
  5775. /* For an assignment statement outside of an output section statement,
  5776. choose the best of neighbouring output sections to use for values
  5777. of "dot". */
  5778. asection *
  5779. section_for_dot (void)
  5780. {
  5781. asection *s;
  5782. /* Assignments belong to the previous output section, unless there
  5783. has been an assignment to "dot", in which case following
  5784. assignments belong to the next output section. (The assumption
  5785. is that an assignment to "dot" is setting up the address for the
  5786. next output section.) Except that past the assignment to "_end"
  5787. we always associate with the previous section. This exception is
  5788. for targets like SH that define an alloc .stack or other
  5789. weirdness after non-alloc sections. */
  5790. if (current_section == NULL || prefer_next_section)
  5791. {
  5792. lang_statement_union_type *stmt;
  5793. lang_output_section_statement_type *os;
  5794. for (stmt = (lang_statement_union_type *) current_assign;
  5795. stmt != NULL;
  5796. stmt = stmt->header.next)
  5797. if (stmt->header.type == lang_output_section_statement_enum)
  5798. break;
  5799. os = &stmt->output_section_statement;
  5800. while (os != NULL
  5801. && !os->after_end
  5802. && (os->bfd_section == NULL
  5803. || (os->bfd_section->flags & SEC_EXCLUDE) != 0
  5804. || bfd_section_removed_from_list (link_info.output_bfd,
  5805. os->bfd_section)))
  5806. os = os->next;
  5807. if (current_section == NULL || os == NULL || !os->after_end)
  5808. {
  5809. if (os != NULL)
  5810. s = os->bfd_section;
  5811. else
  5812. s = link_info.output_bfd->section_last;
  5813. while (s != NULL
  5814. && ((s->flags & SEC_ALLOC) == 0
  5815. || (s->flags & SEC_THREAD_LOCAL) != 0))
  5816. s = s->prev;
  5817. if (s != NULL)
  5818. return s;
  5819. return bfd_abs_section_ptr;
  5820. }
  5821. }
  5822. s = current_section->bfd_section;
  5823. /* The section may have been stripped. */
  5824. while (s != NULL
  5825. && ((s->flags & SEC_EXCLUDE) != 0
  5826. || (s->flags & SEC_ALLOC) == 0
  5827. || (s->flags & SEC_THREAD_LOCAL) != 0
  5828. || bfd_section_removed_from_list (link_info.output_bfd, s)))
  5829. s = s->prev;
  5830. if (s == NULL)
  5831. s = link_info.output_bfd->sections;
  5832. while (s != NULL
  5833. && ((s->flags & SEC_ALLOC) == 0
  5834. || (s->flags & SEC_THREAD_LOCAL) != 0))
  5835. s = s->next;
  5836. if (s != NULL)
  5837. return s;
  5838. return bfd_abs_section_ptr;
  5839. }
  5840. /* Array of __start/__stop/.startof./.sizeof/ symbols. */
  5841. static struct bfd_link_hash_entry **start_stop_syms;
  5842. static size_t start_stop_count = 0;
  5843. static size_t start_stop_alloc = 0;
  5844. /* Give start/stop SYMBOL for SEC a preliminary definition, and add it
  5845. to start_stop_syms. */
  5846. static void
  5847. lang_define_start_stop (const char *symbol, asection *sec)
  5848. {
  5849. struct bfd_link_hash_entry *h;
  5850. h = bfd_define_start_stop (link_info.output_bfd, &link_info, symbol, sec);
  5851. if (h != NULL)
  5852. {
  5853. if (start_stop_count == start_stop_alloc)
  5854. {
  5855. start_stop_alloc = 2 * start_stop_alloc + 10;
  5856. start_stop_syms
  5857. = xrealloc (start_stop_syms,
  5858. start_stop_alloc * sizeof (*start_stop_syms));
  5859. }
  5860. start_stop_syms[start_stop_count++] = h;
  5861. }
  5862. }
  5863. /* Check for input sections whose names match references to
  5864. __start_SECNAME or __stop_SECNAME symbols. Give the symbols
  5865. preliminary definitions. */
  5866. static void
  5867. lang_init_start_stop (void)
  5868. {
  5869. bfd *abfd;
  5870. asection *s;
  5871. char leading_char = bfd_get_symbol_leading_char (link_info.output_bfd);
  5872. for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
  5873. for (s = abfd->sections; s != NULL; s = s->next)
  5874. {
  5875. const char *ps;
  5876. const char *secname = s->name;
  5877. for (ps = secname; *ps != '\0'; ps++)
  5878. if (!ISALNUM ((unsigned char) *ps) && *ps != '_')
  5879. break;
  5880. if (*ps == '\0')
  5881. {
  5882. char *symbol = (char *) xmalloc (10 + strlen (secname));
  5883. symbol[0] = leading_char;
  5884. sprintf (symbol + (leading_char != 0), "__start_%s", secname);
  5885. lang_define_start_stop (symbol, s);
  5886. symbol[1] = leading_char;
  5887. memcpy (symbol + 1 + (leading_char != 0), "__stop", 6);
  5888. lang_define_start_stop (symbol + 1, s);
  5889. free (symbol);
  5890. }
  5891. }
  5892. }
  5893. /* Iterate over start_stop_syms. */
  5894. static void
  5895. foreach_start_stop (void (*func) (struct bfd_link_hash_entry *))
  5896. {
  5897. size_t i;
  5898. for (i = 0; i < start_stop_count; ++i)
  5899. func (start_stop_syms[i]);
  5900. }
  5901. /* __start and __stop symbols are only supposed to be defined by the
  5902. linker for orphan sections, but we now extend that to sections that
  5903. map to an output section of the same name. The symbols were
  5904. defined early for --gc-sections, before we mapped input to output
  5905. sections, so undo those that don't satisfy this rule. */
  5906. static void
  5907. undef_start_stop (struct bfd_link_hash_entry *h)
  5908. {
  5909. if (h->ldscript_def)
  5910. return;
  5911. if (h->u.def.section->output_section == NULL
  5912. || h->u.def.section->output_section->owner != link_info.output_bfd
  5913. || strcmp (h->u.def.section->name,
  5914. h->u.def.section->output_section->name) != 0)
  5915. {
  5916. asection *sec = bfd_get_section_by_name (link_info.output_bfd,
  5917. h->u.def.section->name);
  5918. if (sec != NULL)
  5919. {
  5920. /* When there are more than one input sections with the same
  5921. section name, SECNAME, linker picks the first one to define
  5922. __start_SECNAME and __stop_SECNAME symbols. When the first
  5923. input section is removed by comdat group, we need to check
  5924. if there is still an output section with section name
  5925. SECNAME. */
  5926. asection *i;
  5927. for (i = sec->map_head.s; i != NULL; i = i->map_head.s)
  5928. if (strcmp (h->u.def.section->name, i->name) == 0)
  5929. {
  5930. h->u.def.section = i;
  5931. return;
  5932. }
  5933. }
  5934. h->type = bfd_link_hash_undefined;
  5935. h->u.undef.abfd = NULL;
  5936. if (is_elf_hash_table (link_info.hash))
  5937. {
  5938. const struct elf_backend_data *bed;
  5939. struct elf_link_hash_entry *eh = (struct elf_link_hash_entry *) h;
  5940. unsigned int was_forced = eh->forced_local;
  5941. bed = get_elf_backend_data (link_info.output_bfd);
  5942. (*bed->elf_backend_hide_symbol) (&link_info, eh, true);
  5943. if (!eh->ref_regular_nonweak)
  5944. h->type = bfd_link_hash_undefweak;
  5945. eh->def_regular = 0;
  5946. eh->forced_local = was_forced;
  5947. }
  5948. }
  5949. }
  5950. static void
  5951. lang_undef_start_stop (void)
  5952. {
  5953. foreach_start_stop (undef_start_stop);
  5954. }
  5955. /* Check for output sections whose names match references to
  5956. .startof.SECNAME or .sizeof.SECNAME symbols. Give the symbols
  5957. preliminary definitions. */
  5958. static void
  5959. lang_init_startof_sizeof (void)
  5960. {
  5961. asection *s;
  5962. for (s = link_info.output_bfd->sections; s != NULL; s = s->next)
  5963. {
  5964. const char *secname = s->name;
  5965. char *symbol = (char *) xmalloc (10 + strlen (secname));
  5966. sprintf (symbol, ".startof.%s", secname);
  5967. lang_define_start_stop (symbol, s);
  5968. memcpy (symbol + 1, ".size", 5);
  5969. lang_define_start_stop (symbol + 1, s);
  5970. free (symbol);
  5971. }
  5972. }
  5973. /* Set .startof., .sizeof., __start and __stop symbols final values. */
  5974. static void
  5975. set_start_stop (struct bfd_link_hash_entry *h)
  5976. {
  5977. if (h->ldscript_def
  5978. || h->type != bfd_link_hash_defined)
  5979. return;
  5980. if (h->root.string[0] == '.')
  5981. {
  5982. /* .startof. or .sizeof. symbol.
  5983. .startof. already has final value. */
  5984. if (h->root.string[2] == 'i')
  5985. {
  5986. /* .sizeof. */
  5987. h->u.def.value = TO_ADDR (h->u.def.section->size);
  5988. h->u.def.section = bfd_abs_section_ptr;
  5989. }
  5990. }
  5991. else
  5992. {
  5993. /* __start or __stop symbol. */
  5994. int has_lead = bfd_get_symbol_leading_char (link_info.output_bfd) != 0;
  5995. h->u.def.section = h->u.def.section->output_section;
  5996. if (h->root.string[4 + has_lead] == 'o')
  5997. {
  5998. /* __stop_ */
  5999. h->u.def.value = TO_ADDR (h->u.def.section->size);
  6000. }
  6001. }
  6002. }
  6003. static void
  6004. lang_finalize_start_stop (void)
  6005. {
  6006. foreach_start_stop (set_start_stop);
  6007. }
  6008. static void
  6009. lang_symbol_tweaks (void)
  6010. {
  6011. /* Give initial values for __start and __stop symbols, so that ELF
  6012. gc_sections will keep sections referenced by these symbols. Must
  6013. be done before lang_do_assignments. */
  6014. if (config.build_constructors)
  6015. lang_init_start_stop ();
  6016. /* Make __ehdr_start hidden, and set def_regular even though it is
  6017. likely undefined at this stage. For lang_check_relocs. */
  6018. if (is_elf_hash_table (link_info.hash)
  6019. && !bfd_link_relocatable (&link_info))
  6020. {
  6021. struct elf_link_hash_entry *h = (struct elf_link_hash_entry *)
  6022. bfd_link_hash_lookup (link_info.hash, "__ehdr_start",
  6023. false, false, true);
  6024. /* Only adjust the export class if the symbol was referenced
  6025. and not defined, otherwise leave it alone. */
  6026. if (h != NULL
  6027. && (h->root.type == bfd_link_hash_new
  6028. || h->root.type == bfd_link_hash_undefined
  6029. || h->root.type == bfd_link_hash_undefweak
  6030. || h->root.type == bfd_link_hash_common))
  6031. {
  6032. const struct elf_backend_data *bed;
  6033. bed = get_elf_backend_data (link_info.output_bfd);
  6034. (*bed->elf_backend_hide_symbol) (&link_info, h, true);
  6035. if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
  6036. h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
  6037. h->def_regular = 1;
  6038. h->root.linker_def = 1;
  6039. h->root.rel_from_abs = 1;
  6040. }
  6041. }
  6042. }
  6043. static void
  6044. lang_end (void)
  6045. {
  6046. struct bfd_link_hash_entry *h;
  6047. bool warn;
  6048. if ((bfd_link_relocatable (&link_info) && !link_info.gc_sections)
  6049. || bfd_link_dll (&link_info))
  6050. warn = entry_from_cmdline;
  6051. else
  6052. warn = true;
  6053. /* Force the user to specify a root when generating a relocatable with
  6054. --gc-sections, unless --gc-keep-exported was also given. */
  6055. if (bfd_link_relocatable (&link_info)
  6056. && link_info.gc_sections
  6057. && !link_info.gc_keep_exported)
  6058. {
  6059. struct bfd_sym_chain *sym;
  6060. for (sym = link_info.gc_sym_list; sym != NULL; sym = sym->next)
  6061. {
  6062. h = bfd_link_hash_lookup (link_info.hash, sym->name,
  6063. false, false, false);
  6064. if (h != NULL
  6065. && (h->type == bfd_link_hash_defined
  6066. || h->type == bfd_link_hash_defweak)
  6067. && !bfd_is_const_section (h->u.def.section))
  6068. break;
  6069. }
  6070. if (!sym)
  6071. einfo (_("%F%P: --gc-sections requires a defined symbol root "
  6072. "specified by -e or -u\n"));
  6073. }
  6074. if (entry_symbol.name == NULL)
  6075. {
  6076. /* No entry has been specified. Look for the default entry, but
  6077. don't warn if we don't find it. */
  6078. entry_symbol.name = entry_symbol_default;
  6079. warn = false;
  6080. }
  6081. h = bfd_link_hash_lookup (link_info.hash, entry_symbol.name,
  6082. false, false, true);
  6083. if (h != NULL
  6084. && (h->type == bfd_link_hash_defined
  6085. || h->type == bfd_link_hash_defweak)
  6086. && h->u.def.section->output_section != NULL)
  6087. {
  6088. bfd_vma val;
  6089. val = (h->u.def.value
  6090. + bfd_section_vma (h->u.def.section->output_section)
  6091. + h->u.def.section->output_offset);
  6092. if (!bfd_set_start_address (link_info.output_bfd, val))
  6093. einfo (_("%F%P: %s: can't set start address\n"), entry_symbol.name);
  6094. }
  6095. else
  6096. {
  6097. bfd_vma val;
  6098. const char *send;
  6099. /* We couldn't find the entry symbol. Try parsing it as a
  6100. number. */
  6101. val = bfd_scan_vma (entry_symbol.name, &send, 0);
  6102. if (*send == '\0')
  6103. {
  6104. if (!bfd_set_start_address (link_info.output_bfd, val))
  6105. einfo (_("%F%P: can't set start address\n"));
  6106. }
  6107. /* BZ 2004952: Only use the start of the entry section for executables. */
  6108. else if bfd_link_executable (&link_info)
  6109. {
  6110. asection *ts;
  6111. /* Can't find the entry symbol, and it's not a number. Use
  6112. the first address in the text section. */
  6113. ts = bfd_get_section_by_name (link_info.output_bfd, entry_section);
  6114. if (ts != NULL)
  6115. {
  6116. if (warn)
  6117. einfo (_("%P: warning: cannot find entry symbol %s;"
  6118. " defaulting to %V\n"),
  6119. entry_symbol.name,
  6120. bfd_section_vma (ts));
  6121. if (!bfd_set_start_address (link_info.output_bfd,
  6122. bfd_section_vma (ts)))
  6123. einfo (_("%F%P: can't set start address\n"));
  6124. }
  6125. else
  6126. {
  6127. if (warn)
  6128. einfo (_("%P: warning: cannot find entry symbol %s;"
  6129. " not setting start address\n"),
  6130. entry_symbol.name);
  6131. }
  6132. }
  6133. else
  6134. {
  6135. if (warn)
  6136. einfo (_("%P: warning: cannot find entry symbol %s;"
  6137. " not setting start address\n"),
  6138. entry_symbol.name);
  6139. }
  6140. }
  6141. }
  6142. /* This is a small function used when we want to ignore errors from
  6143. BFD. */
  6144. static void
  6145. ignore_bfd_errors (const char *fmt ATTRIBUTE_UNUSED,
  6146. va_list ap ATTRIBUTE_UNUSED)
  6147. {
  6148. /* Don't do anything. */
  6149. }
  6150. /* Check that the architecture of all the input files is compatible
  6151. with the output file. Also call the backend to let it do any
  6152. other checking that is needed. */
  6153. static void
  6154. lang_check (void)
  6155. {
  6156. lang_input_statement_type *file;
  6157. bfd *input_bfd;
  6158. const bfd_arch_info_type *compatible;
  6159. for (file = (void *) file_chain.head;
  6160. file != NULL;
  6161. file = file->next)
  6162. {
  6163. #if BFD_SUPPORTS_PLUGINS
  6164. /* Don't check format of files claimed by plugin. */
  6165. if (file->flags.claimed)
  6166. continue;
  6167. #endif /* BFD_SUPPORTS_PLUGINS */
  6168. input_bfd = file->the_bfd;
  6169. compatible
  6170. = bfd_arch_get_compatible (input_bfd, link_info.output_bfd,
  6171. command_line.accept_unknown_input_arch);
  6172. /* In general it is not possible to perform a relocatable
  6173. link between differing object formats when the input
  6174. file has relocations, because the relocations in the
  6175. input format may not have equivalent representations in
  6176. the output format (and besides BFD does not translate
  6177. relocs for other link purposes than a final link). */
  6178. if (!file->flags.just_syms
  6179. && (bfd_link_relocatable (&link_info)
  6180. || link_info.emitrelocations)
  6181. && (compatible == NULL
  6182. || (bfd_get_flavour (input_bfd)
  6183. != bfd_get_flavour (link_info.output_bfd)))
  6184. && (bfd_get_file_flags (input_bfd) & HAS_RELOC) != 0)
  6185. {
  6186. einfo (_("%F%P: relocatable linking with relocations from"
  6187. " format %s (%pB) to format %s (%pB) is not supported\n"),
  6188. bfd_get_target (input_bfd), input_bfd,
  6189. bfd_get_target (link_info.output_bfd), link_info.output_bfd);
  6190. /* einfo with %F exits. */
  6191. }
  6192. if (compatible == NULL)
  6193. {
  6194. if (command_line.warn_mismatch)
  6195. einfo (_("%X%P: %s architecture of input file `%pB'"
  6196. " is incompatible with %s output\n"),
  6197. bfd_printable_name (input_bfd), input_bfd,
  6198. bfd_printable_name (link_info.output_bfd));
  6199. }
  6200. /* If the input bfd has no contents, it shouldn't set the
  6201. private data of the output bfd. */
  6202. else if (!file->flags.just_syms
  6203. && ((input_bfd->flags & DYNAMIC) != 0
  6204. || bfd_count_sections (input_bfd) != 0))
  6205. {
  6206. bfd_error_handler_type pfn = NULL;
  6207. /* If we aren't supposed to warn about mismatched input
  6208. files, temporarily set the BFD error handler to a
  6209. function which will do nothing. We still want to call
  6210. bfd_merge_private_bfd_data, since it may set up
  6211. information which is needed in the output file. */
  6212. if (!command_line.warn_mismatch)
  6213. pfn = bfd_set_error_handler (ignore_bfd_errors);
  6214. if (!bfd_merge_private_bfd_data (input_bfd, &link_info))
  6215. {
  6216. if (command_line.warn_mismatch)
  6217. einfo (_("%X%P: failed to merge target specific data"
  6218. " of file %pB\n"), input_bfd);
  6219. }
  6220. if (!command_line.warn_mismatch)
  6221. bfd_set_error_handler (pfn);
  6222. }
  6223. }
  6224. }
  6225. /* Look through all the global common symbols and attach them to the
  6226. correct section. The -sort-common command line switch may be used
  6227. to roughly sort the entries by alignment. */
  6228. static void
  6229. lang_common (void)
  6230. {
  6231. if (link_info.inhibit_common_definition)
  6232. return;
  6233. if (bfd_link_relocatable (&link_info)
  6234. && !command_line.force_common_definition)
  6235. return;
  6236. if (!config.sort_common)
  6237. bfd_link_hash_traverse (link_info.hash, lang_one_common, NULL);
  6238. else
  6239. {
  6240. unsigned int power;
  6241. if (config.sort_common == sort_descending)
  6242. {
  6243. for (power = 4; power > 0; power--)
  6244. bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
  6245. power = 0;
  6246. bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
  6247. }
  6248. else
  6249. {
  6250. for (power = 0; power <= 4; power++)
  6251. bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
  6252. power = (unsigned int) -1;
  6253. bfd_link_hash_traverse (link_info.hash, lang_one_common, &power);
  6254. }
  6255. }
  6256. }
  6257. /* Place one common symbol in the correct section. */
  6258. static bool
  6259. lang_one_common (struct bfd_link_hash_entry *h, void *info)
  6260. {
  6261. unsigned int power_of_two;
  6262. bfd_vma size;
  6263. asection *section;
  6264. if (h->type != bfd_link_hash_common)
  6265. return true;
  6266. size = h->u.c.size;
  6267. power_of_two = h->u.c.p->alignment_power;
  6268. if (config.sort_common == sort_descending
  6269. && power_of_two < *(unsigned int *) info)
  6270. return true;
  6271. else if (config.sort_common == sort_ascending
  6272. && power_of_two > *(unsigned int *) info)
  6273. return true;
  6274. section = h->u.c.p->section;
  6275. if (!bfd_define_common_symbol (link_info.output_bfd, &link_info, h))
  6276. einfo (_("%F%P: could not define common symbol `%pT': %E\n"),
  6277. h->root.string);
  6278. if (config.map_file != NULL)
  6279. {
  6280. static bool header_printed;
  6281. int len;
  6282. char *name;
  6283. char buf[50];
  6284. if (!header_printed)
  6285. {
  6286. minfo (_("\nAllocating common symbols\n"));
  6287. minfo (_("Common symbol size file\n\n"));
  6288. header_printed = true;
  6289. }
  6290. name = bfd_demangle (link_info.output_bfd, h->root.string,
  6291. DMGL_ANSI | DMGL_PARAMS);
  6292. if (name == NULL)
  6293. {
  6294. minfo ("%s", h->root.string);
  6295. len = strlen (h->root.string);
  6296. }
  6297. else
  6298. {
  6299. minfo ("%s", name);
  6300. len = strlen (name);
  6301. free (name);
  6302. }
  6303. if (len >= 19)
  6304. {
  6305. print_nl ();
  6306. len = 0;
  6307. }
  6308. while (len < 20)
  6309. {
  6310. print_space ();
  6311. ++len;
  6312. }
  6313. minfo ("0x");
  6314. if (size <= 0xffffffff)
  6315. sprintf (buf, "%lx", (unsigned long) size);
  6316. else
  6317. sprintf_vma (buf, size);
  6318. minfo ("%s", buf);
  6319. len = strlen (buf);
  6320. while (len < 16)
  6321. {
  6322. print_space ();
  6323. ++len;
  6324. }
  6325. minfo ("%pB\n", section->owner);
  6326. }
  6327. return true;
  6328. }
  6329. /* Handle a single orphan section S, placing the orphan into an appropriate
  6330. output section. The effects of the --orphan-handling command line
  6331. option are handled here. */
  6332. static void
  6333. ldlang_place_orphan (asection *s)
  6334. {
  6335. if (config.orphan_handling == orphan_handling_discard)
  6336. {
  6337. lang_output_section_statement_type *os;
  6338. os = lang_output_section_statement_lookup (DISCARD_SECTION_NAME, 0, 1);
  6339. if (os->addr_tree == NULL
  6340. && (bfd_link_relocatable (&link_info)
  6341. || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0))
  6342. os->addr_tree = exp_intop (0);
  6343. lang_add_section (&os->children, s, NULL, NULL, os);
  6344. }
  6345. else
  6346. {
  6347. lang_output_section_statement_type *os;
  6348. const char *name = s->name;
  6349. int constraint = 0;
  6350. if (config.orphan_handling == orphan_handling_error)
  6351. einfo (_("%X%P: error: unplaced orphan section `%pA' from `%pB'\n"),
  6352. s, s->owner);
  6353. if (config.unique_orphan_sections || unique_section_p (s, NULL))
  6354. constraint = SPECIAL;
  6355. os = ldemul_place_orphan (s, name, constraint);
  6356. if (os == NULL)
  6357. {
  6358. os = lang_output_section_statement_lookup (name, constraint, 1);
  6359. if (os->addr_tree == NULL
  6360. && (bfd_link_relocatable (&link_info)
  6361. || (s->flags & (SEC_LOAD | SEC_ALLOC)) == 0))
  6362. os->addr_tree = exp_intop (0);
  6363. lang_add_section (&os->children, s, NULL, NULL, os);
  6364. }
  6365. if (config.orphan_handling == orphan_handling_warn)
  6366. einfo (_("%P: warning: orphan section `%pA' from `%pB' being "
  6367. "placed in section `%s'\n"),
  6368. s, s->owner, os->name);
  6369. }
  6370. }
  6371. /* Run through the input files and ensure that every input section has
  6372. somewhere to go. If one is found without a destination then create
  6373. an input request and place it into the statement tree. */
  6374. static void
  6375. lang_place_orphans (void)
  6376. {
  6377. LANG_FOR_EACH_INPUT_STATEMENT (file)
  6378. {
  6379. asection *s;
  6380. for (s = file->the_bfd->sections; s != NULL; s = s->next)
  6381. {
  6382. if (s->output_section == NULL)
  6383. {
  6384. /* This section of the file is not attached, root
  6385. around for a sensible place for it to go. */
  6386. if (file->flags.just_syms)
  6387. bfd_link_just_syms (file->the_bfd, s, &link_info);
  6388. else if (lang_discard_section_p (s))
  6389. s->output_section = bfd_abs_section_ptr;
  6390. else if (strcmp (s->name, "COMMON") == 0)
  6391. {
  6392. /* This is a lonely common section which must have
  6393. come from an archive. We attach to the section
  6394. with the wildcard. */
  6395. if (!bfd_link_relocatable (&link_info)
  6396. || command_line.force_common_definition)
  6397. {
  6398. if (default_common_section == NULL)
  6399. default_common_section
  6400. = lang_output_section_statement_lookup (".bss", 0, 1);
  6401. lang_add_section (&default_common_section->children, s,
  6402. NULL, NULL, default_common_section);
  6403. }
  6404. }
  6405. else
  6406. ldlang_place_orphan (s);
  6407. }
  6408. }
  6409. }
  6410. }
  6411. void
  6412. lang_set_flags (lang_memory_region_type *ptr, const char *flags, int invert)
  6413. {
  6414. flagword *ptr_flags;
  6415. ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
  6416. while (*flags)
  6417. {
  6418. switch (*flags)
  6419. {
  6420. /* PR 17900: An exclamation mark in the attributes reverses
  6421. the sense of any of the attributes that follow. */
  6422. case '!':
  6423. invert = !invert;
  6424. ptr_flags = invert ? &ptr->not_flags : &ptr->flags;
  6425. break;
  6426. case 'A': case 'a':
  6427. *ptr_flags |= SEC_ALLOC;
  6428. break;
  6429. case 'R': case 'r':
  6430. *ptr_flags |= SEC_READONLY;
  6431. break;
  6432. case 'W': case 'w':
  6433. *ptr_flags |= SEC_DATA;
  6434. break;
  6435. case 'X': case 'x':
  6436. *ptr_flags |= SEC_CODE;
  6437. break;
  6438. case 'L': case 'l':
  6439. case 'I': case 'i':
  6440. *ptr_flags |= SEC_LOAD;
  6441. break;
  6442. default:
  6443. einfo (_("%F%P: invalid character %c (%d) in flags\n"),
  6444. *flags, *flags);
  6445. break;
  6446. }
  6447. flags++;
  6448. }
  6449. }
  6450. /* Call a function on each real input file. This function will be
  6451. called on an archive, but not on the elements. */
  6452. void
  6453. lang_for_each_input_file (void (*func) (lang_input_statement_type *))
  6454. {
  6455. lang_input_statement_type *f;
  6456. for (f = (void *) input_file_chain.head;
  6457. f != NULL;
  6458. f = f->next_real_file)
  6459. if (f->flags.real)
  6460. func (f);
  6461. }
  6462. /* Call a function on each real file. The function will be called on
  6463. all the elements of an archive which are included in the link, but
  6464. will not be called on the archive file itself. */
  6465. void
  6466. lang_for_each_file (void (*func) (lang_input_statement_type *))
  6467. {
  6468. LANG_FOR_EACH_INPUT_STATEMENT (f)
  6469. {
  6470. if (f->flags.real)
  6471. func (f);
  6472. }
  6473. }
  6474. void
  6475. ldlang_add_file (lang_input_statement_type *entry)
  6476. {
  6477. lang_statement_append (&file_chain, entry, &entry->next);
  6478. /* The BFD linker needs to have a list of all input BFDs involved in
  6479. a link. */
  6480. ASSERT (link_info.input_bfds_tail != &entry->the_bfd->link.next
  6481. && entry->the_bfd->link.next == NULL);
  6482. ASSERT (entry->the_bfd != link_info.output_bfd);
  6483. *link_info.input_bfds_tail = entry->the_bfd;
  6484. link_info.input_bfds_tail = &entry->the_bfd->link.next;
  6485. bfd_set_usrdata (entry->the_bfd, entry);
  6486. bfd_set_gp_size (entry->the_bfd, g_switch_value);
  6487. /* Look through the sections and check for any which should not be
  6488. included in the link. We need to do this now, so that we can
  6489. notice when the backend linker tries to report multiple
  6490. definition errors for symbols which are in sections we aren't
  6491. going to link. FIXME: It might be better to entirely ignore
  6492. symbols which are defined in sections which are going to be
  6493. discarded. This would require modifying the backend linker for
  6494. each backend which might set the SEC_LINK_ONCE flag. If we do
  6495. this, we should probably handle SEC_EXCLUDE in the same way. */
  6496. bfd_map_over_sections (entry->the_bfd, section_already_linked, entry);
  6497. }
  6498. void
  6499. lang_add_output (const char *name, int from_script)
  6500. {
  6501. /* Make -o on command line override OUTPUT in script. */
  6502. if (!had_output_filename || !from_script)
  6503. {
  6504. output_filename = name;
  6505. had_output_filename = true;
  6506. }
  6507. }
  6508. lang_output_section_statement_type *
  6509. lang_enter_output_section_statement (const char *output_section_statement_name,
  6510. etree_type *address_exp,
  6511. enum section_type sectype,
  6512. etree_type *sectype_value,
  6513. etree_type *align,
  6514. etree_type *subalign,
  6515. etree_type *ebase,
  6516. int constraint,
  6517. int align_with_input)
  6518. {
  6519. lang_output_section_statement_type *os;
  6520. os = lang_output_section_statement_lookup (output_section_statement_name,
  6521. constraint, 2);
  6522. current_section = os;
  6523. if (os->addr_tree == NULL)
  6524. {
  6525. os->addr_tree = address_exp;
  6526. }
  6527. os->sectype = sectype;
  6528. if (sectype == type_section || sectype == typed_readonly_section)
  6529. os->sectype_value = sectype_value;
  6530. else if (sectype == noload_section)
  6531. os->flags = SEC_NEVER_LOAD;
  6532. else
  6533. os->flags = SEC_NO_FLAGS;
  6534. os->block_value = 1;
  6535. /* Make next things chain into subchain of this. */
  6536. push_stat_ptr (&os->children);
  6537. os->align_lma_with_input = align_with_input == ALIGN_WITH_INPUT;
  6538. if (os->align_lma_with_input && align != NULL)
  6539. einfo (_("%F%P:%pS: error: align with input and explicit align specified\n"),
  6540. NULL);
  6541. os->subsection_alignment = subalign;
  6542. os->section_alignment = align;
  6543. os->load_base = ebase;
  6544. return os;
  6545. }
  6546. void
  6547. lang_final (void)
  6548. {
  6549. lang_output_statement_type *new_stmt;
  6550. new_stmt = new_stat (lang_output_statement, stat_ptr);
  6551. new_stmt->name = output_filename;
  6552. }
  6553. /* Reset the current counters in the regions. */
  6554. void
  6555. lang_reset_memory_regions (void)
  6556. {
  6557. lang_memory_region_type *p = lang_memory_region_list;
  6558. asection *o;
  6559. lang_output_section_statement_type *os;
  6560. for (p = lang_memory_region_list; p != NULL; p = p->next)
  6561. {
  6562. p->current = p->origin;
  6563. p->last_os = NULL;
  6564. }
  6565. for (os = (void *) lang_os_list.head;
  6566. os != NULL;
  6567. os = os->next)
  6568. {
  6569. os->processed_vma = false;
  6570. os->processed_lma = false;
  6571. }
  6572. for (o = link_info.output_bfd->sections; o != NULL; o = o->next)
  6573. {
  6574. /* Save the last size for possible use by bfd_relax_section. */
  6575. o->rawsize = o->size;
  6576. if (!(o->flags & SEC_FIXED_SIZE))
  6577. o->size = 0;
  6578. }
  6579. }
  6580. /* Worker for lang_gc_sections_1. */
  6581. static void
  6582. gc_section_callback (lang_wild_statement_type *ptr,
  6583. struct wildcard_list *sec ATTRIBUTE_UNUSED,
  6584. asection *section,
  6585. lang_input_statement_type *file ATTRIBUTE_UNUSED,
  6586. void *data ATTRIBUTE_UNUSED)
  6587. {
  6588. /* If the wild pattern was marked KEEP, the member sections
  6589. should be as well. */
  6590. if (ptr->keep_sections)
  6591. section->flags |= SEC_KEEP;
  6592. }
  6593. /* Iterate over sections marking them against GC. */
  6594. static void
  6595. lang_gc_sections_1 (lang_statement_union_type *s)
  6596. {
  6597. for (; s != NULL; s = s->header.next)
  6598. {
  6599. switch (s->header.type)
  6600. {
  6601. case lang_wild_statement_enum:
  6602. walk_wild (&s->wild_statement, gc_section_callback, NULL);
  6603. break;
  6604. case lang_constructors_statement_enum:
  6605. lang_gc_sections_1 (constructor_list.head);
  6606. break;
  6607. case lang_output_section_statement_enum:
  6608. lang_gc_sections_1 (s->output_section_statement.children.head);
  6609. break;
  6610. case lang_group_statement_enum:
  6611. lang_gc_sections_1 (s->group_statement.children.head);
  6612. break;
  6613. default:
  6614. break;
  6615. }
  6616. }
  6617. }
  6618. static void
  6619. lang_gc_sections (void)
  6620. {
  6621. /* Keep all sections so marked in the link script. */
  6622. lang_gc_sections_1 (statement_list.head);
  6623. /* SEC_EXCLUDE is ignored when doing a relocatable link, except in
  6624. the special case of .stabstr debug info. (See bfd/stabs.c)
  6625. Twiddle the flag here, to simplify later linker code. */
  6626. if (bfd_link_relocatable (&link_info))
  6627. {
  6628. LANG_FOR_EACH_INPUT_STATEMENT (f)
  6629. {
  6630. asection *sec;
  6631. #if BFD_SUPPORTS_PLUGINS
  6632. if (f->flags.claimed)
  6633. continue;
  6634. #endif
  6635. for (sec = f->the_bfd->sections; sec != NULL; sec = sec->next)
  6636. if ((sec->flags & SEC_DEBUGGING) == 0
  6637. || strcmp (sec->name, ".stabstr") != 0)
  6638. sec->flags &= ~SEC_EXCLUDE;
  6639. }
  6640. }
  6641. if (link_info.gc_sections)
  6642. bfd_gc_sections (link_info.output_bfd, &link_info);
  6643. }
  6644. /* Worker for lang_find_relro_sections_1. */
  6645. static void
  6646. find_relro_section_callback (lang_wild_statement_type *ptr ATTRIBUTE_UNUSED,
  6647. struct wildcard_list *sec ATTRIBUTE_UNUSED,
  6648. asection *section,
  6649. lang_input_statement_type *file ATTRIBUTE_UNUSED,
  6650. void *data)
  6651. {
  6652. /* Discarded, excluded and ignored sections effectively have zero
  6653. size. */
  6654. if (section->output_section != NULL
  6655. && section->output_section->owner == link_info.output_bfd
  6656. && (section->output_section->flags & SEC_EXCLUDE) == 0
  6657. && !IGNORE_SECTION (section)
  6658. && section->size != 0)
  6659. {
  6660. bool *has_relro_section = (bool *) data;
  6661. *has_relro_section = true;
  6662. }
  6663. }
  6664. /* Iterate over sections for relro sections. */
  6665. static void
  6666. lang_find_relro_sections_1 (lang_statement_union_type *s,
  6667. bool *has_relro_section)
  6668. {
  6669. if (*has_relro_section)
  6670. return;
  6671. for (; s != NULL; s = s->header.next)
  6672. {
  6673. if (s == expld.dataseg.relro_end_stat)
  6674. break;
  6675. switch (s->header.type)
  6676. {
  6677. case lang_wild_statement_enum:
  6678. walk_wild (&s->wild_statement,
  6679. find_relro_section_callback,
  6680. has_relro_section);
  6681. break;
  6682. case lang_constructors_statement_enum:
  6683. lang_find_relro_sections_1 (constructor_list.head,
  6684. has_relro_section);
  6685. break;
  6686. case lang_output_section_statement_enum:
  6687. lang_find_relro_sections_1 (s->output_section_statement.children.head,
  6688. has_relro_section);
  6689. break;
  6690. case lang_group_statement_enum:
  6691. lang_find_relro_sections_1 (s->group_statement.children.head,
  6692. has_relro_section);
  6693. break;
  6694. default:
  6695. break;
  6696. }
  6697. }
  6698. }
  6699. static void
  6700. lang_find_relro_sections (void)
  6701. {
  6702. bool has_relro_section = false;
  6703. /* Check all sections in the link script. */
  6704. lang_find_relro_sections_1 (expld.dataseg.relro_start_stat,
  6705. &has_relro_section);
  6706. if (!has_relro_section)
  6707. link_info.relro = false;
  6708. }
  6709. /* Relax all sections until bfd_relax_section gives up. */
  6710. void
  6711. lang_relax_sections (bool need_layout)
  6712. {
  6713. /* NB: Also enable relaxation to layout sections for DT_RELR. */
  6714. if (RELAXATION_ENABLED || link_info.enable_dt_relr)
  6715. {
  6716. /* We may need more than one relaxation pass. */
  6717. int i = link_info.relax_pass;
  6718. /* The backend can use it to determine the current pass. */
  6719. link_info.relax_pass = 0;
  6720. while (i--)
  6721. {
  6722. /* Keep relaxing until bfd_relax_section gives up. */
  6723. bool relax_again;
  6724. link_info.relax_trip = -1;
  6725. do
  6726. {
  6727. link_info.relax_trip++;
  6728. /* Note: pe-dll.c does something like this also. If you find
  6729. you need to change this code, you probably need to change
  6730. pe-dll.c also. DJ */
  6731. /* Do all the assignments with our current guesses as to
  6732. section sizes. */
  6733. lang_do_assignments (lang_assigning_phase_enum);
  6734. /* We must do this after lang_do_assignments, because it uses
  6735. size. */
  6736. lang_reset_memory_regions ();
  6737. /* Perform another relax pass - this time we know where the
  6738. globals are, so can make a better guess. */
  6739. relax_again = false;
  6740. lang_size_sections (&relax_again, false);
  6741. }
  6742. while (relax_again);
  6743. link_info.relax_pass++;
  6744. }
  6745. need_layout = true;
  6746. }
  6747. if (need_layout)
  6748. {
  6749. /* Final extra sizing to report errors. */
  6750. lang_do_assignments (lang_assigning_phase_enum);
  6751. lang_reset_memory_regions ();
  6752. lang_size_sections (NULL, true);
  6753. }
  6754. }
  6755. #if BFD_SUPPORTS_PLUGINS
  6756. /* Find the insert point for the plugin's replacement files. We
  6757. place them after the first claimed real object file, or if the
  6758. first claimed object is an archive member, after the last real
  6759. object file immediately preceding the archive. In the event
  6760. no objects have been claimed at all, we return the first dummy
  6761. object file on the list as the insert point; that works, but
  6762. the callee must be careful when relinking the file_chain as it
  6763. is not actually on that chain, only the statement_list and the
  6764. input_file list; in that case, the replacement files must be
  6765. inserted at the head of the file_chain. */
  6766. static lang_input_statement_type *
  6767. find_replacements_insert_point (bool *before)
  6768. {
  6769. lang_input_statement_type *claim1, *lastobject;
  6770. lastobject = (void *) input_file_chain.head;
  6771. for (claim1 = (void *) file_chain.head;
  6772. claim1 != NULL;
  6773. claim1 = claim1->next)
  6774. {
  6775. if (claim1->flags.claimed)
  6776. {
  6777. *before = claim1->flags.claim_archive;
  6778. return claim1->flags.claim_archive ? lastobject : claim1;
  6779. }
  6780. /* Update lastobject if this is a real object file. */
  6781. if (claim1->the_bfd != NULL && claim1->the_bfd->my_archive == NULL)
  6782. lastobject = claim1;
  6783. }
  6784. /* No files were claimed by the plugin. Choose the last object
  6785. file found on the list (maybe the first, dummy entry) as the
  6786. insert point. */
  6787. *before = false;
  6788. return lastobject;
  6789. }
  6790. /* Find where to insert ADD, an archive element or shared library
  6791. added during a rescan. */
  6792. static lang_input_statement_type **
  6793. find_rescan_insertion (lang_input_statement_type *add)
  6794. {
  6795. bfd *add_bfd = add->the_bfd;
  6796. lang_input_statement_type *f;
  6797. lang_input_statement_type *last_loaded = NULL;
  6798. lang_input_statement_type *before = NULL;
  6799. lang_input_statement_type **iter = NULL;
  6800. if (add_bfd->my_archive != NULL)
  6801. add_bfd = add_bfd->my_archive;
  6802. /* First look through the input file chain, to find an object file
  6803. before the one we've rescanned. Normal object files always
  6804. appear on both the input file chain and the file chain, so this
  6805. lets us get quickly to somewhere near the correct place on the
  6806. file chain if it is full of archive elements. Archives don't
  6807. appear on the file chain, but if an element has been extracted
  6808. then their input_statement->next points at it. */
  6809. for (f = (void *) input_file_chain.head;
  6810. f != NULL;
  6811. f = f->next_real_file)
  6812. {
  6813. if (f->the_bfd == add_bfd)
  6814. {
  6815. before = last_loaded;
  6816. if (f->next != NULL)
  6817. return &f->next->next;
  6818. }
  6819. if (f->the_bfd != NULL && f->next != NULL)
  6820. last_loaded = f;
  6821. }
  6822. for (iter = before ? &before->next : &file_chain.head->input_statement.next;
  6823. *iter != NULL;
  6824. iter = &(*iter)->next)
  6825. if (!(*iter)->flags.claim_archive
  6826. && (*iter)->the_bfd->my_archive == NULL)
  6827. break;
  6828. return iter;
  6829. }
  6830. /* Insert SRCLIST into DESTLIST after given element by chaining
  6831. on FIELD as the next-pointer. (Counterintuitively does not need
  6832. a pointer to the actual after-node itself, just its chain field.) */
  6833. static void
  6834. lang_list_insert_after (lang_statement_list_type *destlist,
  6835. lang_statement_list_type *srclist,
  6836. lang_statement_union_type **field)
  6837. {
  6838. *(srclist->tail) = *field;
  6839. *field = srclist->head;
  6840. if (destlist->tail == field)
  6841. destlist->tail = srclist->tail;
  6842. }
  6843. /* Detach new nodes added to DESTLIST since the time ORIGLIST
  6844. was taken as a copy of it and leave them in ORIGLIST. */
  6845. static void
  6846. lang_list_remove_tail (lang_statement_list_type *destlist,
  6847. lang_statement_list_type *origlist)
  6848. {
  6849. union lang_statement_union **savetail;
  6850. /* Check that ORIGLIST really is an earlier state of DESTLIST. */
  6851. ASSERT (origlist->head == destlist->head);
  6852. savetail = origlist->tail;
  6853. origlist->head = *(savetail);
  6854. origlist->tail = destlist->tail;
  6855. destlist->tail = savetail;
  6856. *savetail = NULL;
  6857. }
  6858. static lang_statement_union_type **
  6859. find_next_input_statement (lang_statement_union_type **s)
  6860. {
  6861. for ( ; *s; s = &(*s)->header.next)
  6862. {
  6863. lang_statement_union_type **t;
  6864. switch ((*s)->header.type)
  6865. {
  6866. case lang_input_statement_enum:
  6867. return s;
  6868. case lang_wild_statement_enum:
  6869. t = &(*s)->wild_statement.children.head;
  6870. break;
  6871. case lang_group_statement_enum:
  6872. t = &(*s)->group_statement.children.head;
  6873. break;
  6874. case lang_output_section_statement_enum:
  6875. t = &(*s)->output_section_statement.children.head;
  6876. break;
  6877. default:
  6878. continue;
  6879. }
  6880. t = find_next_input_statement (t);
  6881. if (*t)
  6882. return t;
  6883. }
  6884. return s;
  6885. }
  6886. #endif /* BFD_SUPPORTS_PLUGINS */
  6887. /* Add NAME to the list of garbage collection entry points. */
  6888. void
  6889. lang_add_gc_name (const char *name)
  6890. {
  6891. struct bfd_sym_chain *sym;
  6892. if (name == NULL)
  6893. return;
  6894. sym = stat_alloc (sizeof (*sym));
  6895. sym->next = link_info.gc_sym_list;
  6896. sym->name = name;
  6897. link_info.gc_sym_list = sym;
  6898. }
  6899. /* Check relocations. */
  6900. static void
  6901. lang_check_relocs (void)
  6902. {
  6903. if (link_info.check_relocs_after_open_input)
  6904. {
  6905. bfd *abfd;
  6906. for (abfd = link_info.input_bfds;
  6907. abfd != (bfd *) NULL; abfd = abfd->link.next)
  6908. if (!bfd_link_check_relocs (abfd, &link_info))
  6909. {
  6910. /* No object output, fail return. */
  6911. config.make_executable = false;
  6912. /* Note: we do not abort the loop, but rather
  6913. continue the scan in case there are other
  6914. bad relocations to report. */
  6915. }
  6916. }
  6917. }
  6918. /* Look through all output sections looking for places where we can
  6919. propagate forward the lma region. */
  6920. static void
  6921. lang_propagate_lma_regions (void)
  6922. {
  6923. lang_output_section_statement_type *os;
  6924. for (os = (void *) lang_os_list.head;
  6925. os != NULL;
  6926. os = os->next)
  6927. {
  6928. if (os->prev != NULL
  6929. && os->lma_region == NULL
  6930. && os->load_base == NULL
  6931. && os->addr_tree == NULL
  6932. && os->region == os->prev->region)
  6933. os->lma_region = os->prev->lma_region;
  6934. }
  6935. }
  6936. void
  6937. lang_process (void)
  6938. {
  6939. /* Finalize dynamic list. */
  6940. if (link_info.dynamic_list)
  6941. lang_finalize_version_expr_head (&link_info.dynamic_list->head);
  6942. current_target = default_target;
  6943. /* Open the output file. */
  6944. lang_for_each_statement (ldlang_open_output);
  6945. init_opb (NULL);
  6946. ldemul_create_output_section_statements ();
  6947. /* Add to the hash table all undefineds on the command line. */
  6948. lang_place_undefineds ();
  6949. if (!bfd_section_already_linked_table_init ())
  6950. einfo (_("%F%P: can not create hash table: %E\n"));
  6951. /* A first pass through the memory regions ensures that if any region
  6952. references a symbol for its origin or length then this symbol will be
  6953. added to the symbol table. Having these symbols in the symbol table
  6954. means that when we call open_input_bfds PROVIDE statements will
  6955. trigger to provide any needed symbols. The regions origins and
  6956. lengths are not assigned as a result of this call. */
  6957. lang_do_memory_regions (false);
  6958. /* Create a bfd for each input file. */
  6959. current_target = default_target;
  6960. lang_statement_iteration++;
  6961. open_input_bfds (statement_list.head, OPEN_BFD_NORMAL);
  6962. /* Now that open_input_bfds has processed assignments and provide
  6963. statements we can give values to symbolic origin/length now. */
  6964. lang_do_memory_regions (true);
  6965. #if BFD_SUPPORTS_PLUGINS
  6966. if (link_info.lto_plugin_active)
  6967. {
  6968. lang_statement_list_type added;
  6969. lang_statement_list_type files, inputfiles;
  6970. ldemul_before_plugin_all_symbols_read ();
  6971. /* Now all files are read, let the plugin(s) decide if there
  6972. are any more to be added to the link before we call the
  6973. emulation's after_open hook. We create a private list of
  6974. input statements for this purpose, which we will eventually
  6975. insert into the global statement list after the first claimed
  6976. file. */
  6977. added = *stat_ptr;
  6978. /* We need to manipulate all three chains in synchrony. */
  6979. files = file_chain;
  6980. inputfiles = input_file_chain;
  6981. if (plugin_call_all_symbols_read ())
  6982. einfo (_("%F%P: %s: plugin reported error after all symbols read\n"),
  6983. plugin_error_plugin ());
  6984. link_info.lto_all_symbols_read = true;
  6985. /* Open any newly added files, updating the file chains. */
  6986. plugin_undefs = link_info.hash->undefs_tail;
  6987. open_input_bfds (*added.tail, OPEN_BFD_NORMAL);
  6988. if (plugin_undefs == link_info.hash->undefs_tail)
  6989. plugin_undefs = NULL;
  6990. /* Restore the global list pointer now they have all been added. */
  6991. lang_list_remove_tail (stat_ptr, &added);
  6992. /* And detach the fresh ends of the file lists. */
  6993. lang_list_remove_tail (&file_chain, &files);
  6994. lang_list_remove_tail (&input_file_chain, &inputfiles);
  6995. /* Were any new files added? */
  6996. if (added.head != NULL)
  6997. {
  6998. /* If so, we will insert them into the statement list immediately
  6999. after the first input file that was claimed by the plugin,
  7000. unless that file was an archive in which case it is inserted
  7001. immediately before. */
  7002. bool before;
  7003. lang_statement_union_type **prev;
  7004. plugin_insert = find_replacements_insert_point (&before);
  7005. /* If a plugin adds input files without having claimed any, we
  7006. don't really have a good idea where to place them. Just putting
  7007. them at the start or end of the list is liable to leave them
  7008. outside the crtbegin...crtend range. */
  7009. ASSERT (plugin_insert != NULL);
  7010. /* Splice the new statement list into the old one. */
  7011. prev = &plugin_insert->header.next;
  7012. if (before)
  7013. {
  7014. prev = find_next_input_statement (prev);
  7015. if (*prev != (void *) plugin_insert->next_real_file)
  7016. {
  7017. /* We didn't find the expected input statement.
  7018. Fall back to adding after plugin_insert. */
  7019. prev = &plugin_insert->header.next;
  7020. }
  7021. }
  7022. lang_list_insert_after (stat_ptr, &added, prev);
  7023. /* Likewise for the file chains. */
  7024. lang_list_insert_after (&input_file_chain, &inputfiles,
  7025. (void *) &plugin_insert->next_real_file);
  7026. /* We must be careful when relinking file_chain; we may need to
  7027. insert the new files at the head of the list if the insert
  7028. point chosen is the dummy first input file. */
  7029. if (plugin_insert->filename)
  7030. lang_list_insert_after (&file_chain, &files,
  7031. (void *) &plugin_insert->next);
  7032. else
  7033. lang_list_insert_after (&file_chain, &files, &file_chain.head);
  7034. /* Rescan archives in case new undefined symbols have appeared. */
  7035. files = file_chain;
  7036. lang_statement_iteration++;
  7037. open_input_bfds (statement_list.head, OPEN_BFD_RESCAN);
  7038. lang_list_remove_tail (&file_chain, &files);
  7039. while (files.head != NULL)
  7040. {
  7041. lang_input_statement_type **insert;
  7042. lang_input_statement_type **iter, *temp;
  7043. bfd *my_arch;
  7044. insert = find_rescan_insertion (&files.head->input_statement);
  7045. /* All elements from an archive can be added at once. */
  7046. iter = &files.head->input_statement.next;
  7047. my_arch = files.head->input_statement.the_bfd->my_archive;
  7048. if (my_arch != NULL)
  7049. for (; *iter != NULL; iter = &(*iter)->next)
  7050. if ((*iter)->the_bfd->my_archive != my_arch)
  7051. break;
  7052. temp = *insert;
  7053. *insert = &files.head->input_statement;
  7054. files.head = (lang_statement_union_type *) *iter;
  7055. *iter = temp;
  7056. if (my_arch != NULL)
  7057. {
  7058. lang_input_statement_type *parent = bfd_usrdata (my_arch);
  7059. if (parent != NULL)
  7060. parent->next = (lang_input_statement_type *)
  7061. ((char *) iter
  7062. - offsetof (lang_input_statement_type, next));
  7063. }
  7064. }
  7065. }
  7066. }
  7067. #endif /* BFD_SUPPORTS_PLUGINS */
  7068. /* Make sure that nobody has tried to add a symbol to this list
  7069. before now. */
  7070. ASSERT (link_info.gc_sym_list == NULL);
  7071. link_info.gc_sym_list = &entry_symbol;
  7072. if (entry_symbol.name == NULL)
  7073. {
  7074. link_info.gc_sym_list = ldlang_undef_chain_list_head;
  7075. /* entry_symbol is normally initialied by a ENTRY definition in the
  7076. linker script or the -e command line option. But if neither of
  7077. these have been used, the target specific backend may still have
  7078. provided an entry symbol via a call to lang_default_entry().
  7079. Unfortunately this value will not be processed until lang_end()
  7080. is called, long after this function has finished. So detect this
  7081. case here and add the target's entry symbol to the list of starting
  7082. points for garbage collection resolution. */
  7083. lang_add_gc_name (entry_symbol_default);
  7084. }
  7085. lang_add_gc_name (link_info.init_function);
  7086. lang_add_gc_name (link_info.fini_function);
  7087. ldemul_after_open ();
  7088. if (config.map_file != NULL)
  7089. lang_print_asneeded ();
  7090. ldlang_open_ctf ();
  7091. bfd_section_already_linked_table_free ();
  7092. /* Make sure that we're not mixing architectures. We call this
  7093. after all the input files have been opened, but before we do any
  7094. other processing, so that any operations merge_private_bfd_data
  7095. does on the output file will be known during the rest of the
  7096. link. */
  7097. lang_check ();
  7098. /* Handle .exports instead of a version script if we're told to do so. */
  7099. if (command_line.version_exports_section)
  7100. lang_do_version_exports_section ();
  7101. /* Build all sets based on the information gathered from the input
  7102. files. */
  7103. ldctor_build_sets ();
  7104. lang_symbol_tweaks ();
  7105. /* PR 13683: We must rerun the assignments prior to running garbage
  7106. collection in order to make sure that all symbol aliases are resolved. */
  7107. lang_do_assignments (lang_mark_phase_enum);
  7108. expld.phase = lang_first_phase_enum;
  7109. /* Size up the common data. */
  7110. lang_common ();
  7111. /* Remove unreferenced sections if asked to. */
  7112. lang_gc_sections ();
  7113. lang_mark_undefineds ();
  7114. /* Check relocations. */
  7115. lang_check_relocs ();
  7116. ldemul_after_check_relocs ();
  7117. /* Update wild statements. */
  7118. update_wild_statements (statement_list.head);
  7119. /* Run through the contours of the script and attach input sections
  7120. to the correct output sections. */
  7121. lang_statement_iteration++;
  7122. map_input_to_output_sections (statement_list.head, NULL, NULL);
  7123. /* Start at the statement immediately after the special abs_section
  7124. output statement, so that it isn't reordered. */
  7125. process_insert_statements (&lang_os_list.head->header.next);
  7126. ldemul_before_place_orphans ();
  7127. /* Find any sections not attached explicitly and handle them. */
  7128. lang_place_orphans ();
  7129. if (!bfd_link_relocatable (&link_info))
  7130. {
  7131. asection *found;
  7132. /* Merge SEC_MERGE sections. This has to be done after GC of
  7133. sections, so that GCed sections are not merged, but before
  7134. assigning dynamic symbols, since removing whole input sections
  7135. is hard then. */
  7136. bfd_merge_sections (link_info.output_bfd, &link_info);
  7137. /* Look for a text section and set the readonly attribute in it. */
  7138. found = bfd_get_section_by_name (link_info.output_bfd, ".text");
  7139. if (found != NULL)
  7140. {
  7141. if (config.text_read_only)
  7142. found->flags |= SEC_READONLY;
  7143. else
  7144. found->flags &= ~SEC_READONLY;
  7145. }
  7146. }
  7147. /* Merge together CTF sections. After this, only the symtab-dependent
  7148. function and data object sections need adjustment. */
  7149. lang_merge_ctf ();
  7150. /* Emit the CTF, iff the emulation doesn't need to do late emission after
  7151. examining things laid out late, like the strtab. */
  7152. lang_write_ctf (0);
  7153. /* Copy forward lma regions for output sections in same lma region. */
  7154. lang_propagate_lma_regions ();
  7155. /* Defining __start/__stop symbols early for --gc-sections to work
  7156. around a glibc build problem can result in these symbols being
  7157. defined when they should not be. Fix them now. */
  7158. if (config.build_constructors)
  7159. lang_undef_start_stop ();
  7160. /* Define .startof./.sizeof. symbols with preliminary values before
  7161. dynamic symbols are created. */
  7162. if (!bfd_link_relocatable (&link_info))
  7163. lang_init_startof_sizeof ();
  7164. /* Do anything special before sizing sections. This is where ELF
  7165. and other back-ends size dynamic sections. */
  7166. ldemul_before_allocation ();
  7167. /* We must record the program headers before we try to fix the
  7168. section positions, since they will affect SIZEOF_HEADERS. */
  7169. lang_record_phdrs ();
  7170. /* Check relro sections. */
  7171. if (link_info.relro && !bfd_link_relocatable (&link_info))
  7172. lang_find_relro_sections ();
  7173. /* Size up the sections. */
  7174. lang_size_sections (NULL, !RELAXATION_ENABLED);
  7175. /* See if anything special should be done now we know how big
  7176. everything is. This is where relaxation is done. */
  7177. ldemul_after_allocation ();
  7178. /* Fix any __start, __stop, .startof. or .sizeof. symbols. */
  7179. lang_finalize_start_stop ();
  7180. /* Do all the assignments again, to report errors. Assignment
  7181. statements are processed multiple times, updating symbols; In
  7182. open_input_bfds, lang_do_assignments, and lang_size_sections.
  7183. Since lang_relax_sections calls lang_do_assignments, symbols are
  7184. also updated in ldemul_after_allocation. */
  7185. lang_do_assignments (lang_final_phase_enum);
  7186. ldemul_finish ();
  7187. /* Convert absolute symbols to section relative. */
  7188. ldexp_finalize_syms ();
  7189. /* Make sure that the section addresses make sense. */
  7190. if (command_line.check_section_addresses)
  7191. lang_check_section_addresses ();
  7192. /* Check any required symbols are known. */
  7193. ldlang_check_require_defined_symbols ();
  7194. lang_end ();
  7195. }
  7196. /* EXPORTED TO YACC */
  7197. void
  7198. lang_add_wild (struct wildcard_spec *filespec,
  7199. struct wildcard_list *section_list,
  7200. bool keep_sections)
  7201. {
  7202. struct wildcard_list *curr, *next;
  7203. lang_wild_statement_type *new_stmt;
  7204. /* Reverse the list as the parser puts it back to front. */
  7205. for (curr = section_list, section_list = NULL;
  7206. curr != NULL;
  7207. section_list = curr, curr = next)
  7208. {
  7209. next = curr->next;
  7210. curr->next = section_list;
  7211. }
  7212. if (filespec != NULL && filespec->name != NULL)
  7213. {
  7214. if (strcmp (filespec->name, "*") == 0)
  7215. filespec->name = NULL;
  7216. else if (!wildcardp (filespec->name))
  7217. lang_has_input_file = true;
  7218. }
  7219. new_stmt = new_stat (lang_wild_statement, stat_ptr);
  7220. new_stmt->filename = NULL;
  7221. new_stmt->filenames_sorted = false;
  7222. new_stmt->section_flag_list = NULL;
  7223. new_stmt->exclude_name_list = NULL;
  7224. if (filespec != NULL)
  7225. {
  7226. new_stmt->filename = filespec->name;
  7227. new_stmt->filenames_sorted = filespec->sorted == by_name;
  7228. new_stmt->section_flag_list = filespec->section_flag_list;
  7229. new_stmt->exclude_name_list = filespec->exclude_name_list;
  7230. }
  7231. new_stmt->section_list = section_list;
  7232. new_stmt->keep_sections = keep_sections;
  7233. lang_list_init (&new_stmt->children);
  7234. analyze_walk_wild_section_handler (new_stmt);
  7235. }
  7236. void
  7237. lang_section_start (const char *name, etree_type *address,
  7238. const segment_type *segment)
  7239. {
  7240. lang_address_statement_type *ad;
  7241. ad = new_stat (lang_address_statement, stat_ptr);
  7242. ad->section_name = name;
  7243. ad->address = address;
  7244. ad->segment = segment;
  7245. }
  7246. /* Set the start symbol to NAME. CMDLINE is nonzero if this is called
  7247. because of a -e argument on the command line, or zero if this is
  7248. called by ENTRY in a linker script. Command line arguments take
  7249. precedence. */
  7250. void
  7251. lang_add_entry (const char *name, bool cmdline)
  7252. {
  7253. if (entry_symbol.name == NULL
  7254. || cmdline
  7255. || !entry_from_cmdline)
  7256. {
  7257. entry_symbol.name = name;
  7258. entry_from_cmdline = cmdline;
  7259. }
  7260. }
  7261. /* Set the default start symbol to NAME. .em files should use this,
  7262. not lang_add_entry, to override the use of "start" if neither the
  7263. linker script nor the command line specifies an entry point. NAME
  7264. must be permanently allocated. */
  7265. void
  7266. lang_default_entry (const char *name)
  7267. {
  7268. entry_symbol_default = name;
  7269. }
  7270. void
  7271. lang_add_target (const char *name)
  7272. {
  7273. lang_target_statement_type *new_stmt;
  7274. new_stmt = new_stat (lang_target_statement, stat_ptr);
  7275. new_stmt->target = name;
  7276. }
  7277. void
  7278. lang_add_map (const char *name)
  7279. {
  7280. while (*name)
  7281. {
  7282. switch (*name)
  7283. {
  7284. case 'F':
  7285. map_option_f = true;
  7286. break;
  7287. }
  7288. name++;
  7289. }
  7290. }
  7291. void
  7292. lang_add_fill (fill_type *fill)
  7293. {
  7294. lang_fill_statement_type *new_stmt;
  7295. new_stmt = new_stat (lang_fill_statement, stat_ptr);
  7296. new_stmt->fill = fill;
  7297. }
  7298. void
  7299. lang_add_data (int type, union etree_union *exp)
  7300. {
  7301. lang_data_statement_type *new_stmt;
  7302. new_stmt = new_stat (lang_data_statement, stat_ptr);
  7303. new_stmt->exp = exp;
  7304. new_stmt->type = type;
  7305. }
  7306. /* Create a new reloc statement. RELOC is the BFD relocation type to
  7307. generate. HOWTO is the corresponding howto structure (we could
  7308. look this up, but the caller has already done so). SECTION is the
  7309. section to generate a reloc against, or NAME is the name of the
  7310. symbol to generate a reloc against. Exactly one of SECTION and
  7311. NAME must be NULL. ADDEND is an expression for the addend. */
  7312. void
  7313. lang_add_reloc (bfd_reloc_code_real_type reloc,
  7314. reloc_howto_type *howto,
  7315. asection *section,
  7316. const char *name,
  7317. union etree_union *addend)
  7318. {
  7319. lang_reloc_statement_type *p = new_stat (lang_reloc_statement, stat_ptr);
  7320. p->reloc = reloc;
  7321. p->howto = howto;
  7322. p->section = section;
  7323. p->name = name;
  7324. p->addend_exp = addend;
  7325. p->addend_value = 0;
  7326. p->output_section = NULL;
  7327. p->output_offset = 0;
  7328. }
  7329. lang_assignment_statement_type *
  7330. lang_add_assignment (etree_type *exp)
  7331. {
  7332. lang_assignment_statement_type *new_stmt;
  7333. new_stmt = new_stat (lang_assignment_statement, stat_ptr);
  7334. new_stmt->exp = exp;
  7335. return new_stmt;
  7336. }
  7337. void
  7338. lang_add_attribute (enum statement_enum attribute)
  7339. {
  7340. new_statement (attribute, sizeof (lang_statement_header_type), stat_ptr);
  7341. }
  7342. void
  7343. lang_startup (const char *name)
  7344. {
  7345. if (first_file->filename != NULL)
  7346. {
  7347. einfo (_("%F%P: multiple STARTUP files\n"));
  7348. }
  7349. first_file->filename = name;
  7350. first_file->local_sym_name = name;
  7351. first_file->flags.real = true;
  7352. }
  7353. void
  7354. lang_float (bool maybe)
  7355. {
  7356. lang_float_flag = maybe;
  7357. }
  7358. /* Work out the load- and run-time regions from a script statement, and
  7359. store them in *LMA_REGION and *REGION respectively.
  7360. MEMSPEC is the name of the run-time region, or the value of
  7361. DEFAULT_MEMORY_REGION if the statement didn't specify one.
  7362. LMA_MEMSPEC is the name of the load-time region, or null if the
  7363. statement didn't specify one.HAVE_LMA_P is TRUE if the statement
  7364. had an explicit load address.
  7365. It is an error to specify both a load region and a load address. */
  7366. static void
  7367. lang_get_regions (lang_memory_region_type **region,
  7368. lang_memory_region_type **lma_region,
  7369. const char *memspec,
  7370. const char *lma_memspec,
  7371. bool have_lma,
  7372. bool have_vma)
  7373. {
  7374. *lma_region = lang_memory_region_lookup (lma_memspec, false);
  7375. /* If no runtime region or VMA has been specified, but the load region
  7376. has been specified, then use the load region for the runtime region
  7377. as well. */
  7378. if (lma_memspec != NULL
  7379. && !have_vma
  7380. && strcmp (memspec, DEFAULT_MEMORY_REGION) == 0)
  7381. *region = *lma_region;
  7382. else
  7383. *region = lang_memory_region_lookup (memspec, false);
  7384. if (have_lma && lma_memspec != 0)
  7385. einfo (_("%X%P:%pS: section has both a load address and a load region\n"),
  7386. NULL);
  7387. }
  7388. void
  7389. lang_leave_output_section_statement (fill_type *fill, const char *memspec,
  7390. lang_output_section_phdr_list *phdrs,
  7391. const char *lma_memspec)
  7392. {
  7393. lang_get_regions (&current_section->region,
  7394. &current_section->lma_region,
  7395. memspec, lma_memspec,
  7396. current_section->load_base != NULL,
  7397. current_section->addr_tree != NULL);
  7398. current_section->fill = fill;
  7399. current_section->phdrs = phdrs;
  7400. pop_stat_ptr ();
  7401. }
  7402. /* Set the output format type. -oformat overrides scripts. */
  7403. void
  7404. lang_add_output_format (const char *format,
  7405. const char *big,
  7406. const char *little,
  7407. int from_script)
  7408. {
  7409. if (output_target == NULL || !from_script)
  7410. {
  7411. if (command_line.endian == ENDIAN_BIG
  7412. && big != NULL)
  7413. format = big;
  7414. else if (command_line.endian == ENDIAN_LITTLE
  7415. && little != NULL)
  7416. format = little;
  7417. output_target = format;
  7418. }
  7419. }
  7420. void
  7421. lang_add_insert (const char *where, int is_before)
  7422. {
  7423. lang_insert_statement_type *new_stmt;
  7424. new_stmt = new_stat (lang_insert_statement, stat_ptr);
  7425. new_stmt->where = where;
  7426. new_stmt->is_before = is_before;
  7427. saved_script_handle = previous_script_handle;
  7428. }
  7429. /* Enter a group. This creates a new lang_group_statement, and sets
  7430. stat_ptr to build new statements within the group. */
  7431. void
  7432. lang_enter_group (void)
  7433. {
  7434. lang_group_statement_type *g;
  7435. g = new_stat (lang_group_statement, stat_ptr);
  7436. lang_list_init (&g->children);
  7437. push_stat_ptr (&g->children);
  7438. }
  7439. /* Leave a group. This just resets stat_ptr to start writing to the
  7440. regular list of statements again. Note that this will not work if
  7441. groups can occur inside anything else which can adjust stat_ptr,
  7442. but currently they can't. */
  7443. void
  7444. lang_leave_group (void)
  7445. {
  7446. pop_stat_ptr ();
  7447. }
  7448. /* Add a new program header. This is called for each entry in a PHDRS
  7449. command in a linker script. */
  7450. void
  7451. lang_new_phdr (const char *name,
  7452. etree_type *type,
  7453. bool filehdr,
  7454. bool phdrs,
  7455. etree_type *at,
  7456. etree_type *flags)
  7457. {
  7458. struct lang_phdr *n, **pp;
  7459. bool hdrs;
  7460. n = stat_alloc (sizeof (struct lang_phdr));
  7461. n->next = NULL;
  7462. n->name = name;
  7463. n->type = exp_get_vma (type, 0, "program header type");
  7464. n->filehdr = filehdr;
  7465. n->phdrs = phdrs;
  7466. n->at = at;
  7467. n->flags = flags;
  7468. hdrs = n->type == 1 && (phdrs || filehdr);
  7469. for (pp = &lang_phdr_list; *pp != NULL; pp = &(*pp)->next)
  7470. if (hdrs
  7471. && (*pp)->type == 1
  7472. && !((*pp)->filehdr || (*pp)->phdrs))
  7473. {
  7474. einfo (_("%X%P:%pS: PHDRS and FILEHDR are not supported"
  7475. " when prior PT_LOAD headers lack them\n"), NULL);
  7476. hdrs = false;
  7477. }
  7478. *pp = n;
  7479. }
  7480. /* Record the program header information in the output BFD. FIXME: We
  7481. should not be calling an ELF specific function here. */
  7482. static void
  7483. lang_record_phdrs (void)
  7484. {
  7485. unsigned int alc;
  7486. asection **secs;
  7487. lang_output_section_phdr_list *last;
  7488. struct lang_phdr *l;
  7489. lang_output_section_statement_type *os;
  7490. alc = 10;
  7491. secs = (asection **) xmalloc (alc * sizeof (asection *));
  7492. last = NULL;
  7493. for (l = lang_phdr_list; l != NULL; l = l->next)
  7494. {
  7495. unsigned int c;
  7496. flagword flags;
  7497. bfd_vma at;
  7498. c = 0;
  7499. for (os = (void *) lang_os_list.head;
  7500. os != NULL;
  7501. os = os->next)
  7502. {
  7503. lang_output_section_phdr_list *pl;
  7504. if (os->constraint < 0)
  7505. continue;
  7506. pl = os->phdrs;
  7507. if (pl != NULL)
  7508. last = pl;
  7509. else
  7510. {
  7511. if (os->sectype == noload_section
  7512. || os->bfd_section == NULL
  7513. || (os->bfd_section->flags & SEC_ALLOC) == 0)
  7514. continue;
  7515. /* Don't add orphans to PT_INTERP header. */
  7516. if (l->type == 3)
  7517. continue;
  7518. if (last == NULL)
  7519. {
  7520. lang_output_section_statement_type *tmp_os;
  7521. /* If we have not run across a section with a program
  7522. header assigned to it yet, then scan forwards to find
  7523. one. This prevents inconsistencies in the linker's
  7524. behaviour when a script has specified just a single
  7525. header and there are sections in that script which are
  7526. not assigned to it, and which occur before the first
  7527. use of that header. See here for more details:
  7528. http://sourceware.org/ml/binutils/2007-02/msg00291.html */
  7529. for (tmp_os = os; tmp_os; tmp_os = tmp_os->next)
  7530. if (tmp_os->phdrs)
  7531. {
  7532. last = tmp_os->phdrs;
  7533. break;
  7534. }
  7535. if (last == NULL)
  7536. einfo (_("%F%P: no sections assigned to phdrs\n"));
  7537. }
  7538. pl = last;
  7539. }
  7540. if (os->bfd_section == NULL)
  7541. continue;
  7542. for (; pl != NULL; pl = pl->next)
  7543. {
  7544. if (strcmp (pl->name, l->name) == 0)
  7545. {
  7546. if (c >= alc)
  7547. {
  7548. alc *= 2;
  7549. secs = (asection **) xrealloc (secs,
  7550. alc * sizeof (asection *));
  7551. }
  7552. secs[c] = os->bfd_section;
  7553. ++c;
  7554. pl->used = true;
  7555. }
  7556. }
  7557. }
  7558. if (l->flags == NULL)
  7559. flags = 0;
  7560. else
  7561. flags = exp_get_vma (l->flags, 0, "phdr flags");
  7562. if (l->at == NULL)
  7563. at = 0;
  7564. else
  7565. at = exp_get_vma (l->at, 0, "phdr load address");
  7566. if (!bfd_record_phdr (link_info.output_bfd, l->type,
  7567. l->flags != NULL, flags, l->at != NULL,
  7568. at, l->filehdr, l->phdrs, c, secs))
  7569. einfo (_("%F%P: bfd_record_phdr failed: %E\n"));
  7570. }
  7571. free (secs);
  7572. /* Make sure all the phdr assignments succeeded. */
  7573. for (os = (void *) lang_os_list.head;
  7574. os != NULL;
  7575. os = os->next)
  7576. {
  7577. lang_output_section_phdr_list *pl;
  7578. if (os->constraint < 0
  7579. || os->bfd_section == NULL)
  7580. continue;
  7581. for (pl = os->phdrs;
  7582. pl != NULL;
  7583. pl = pl->next)
  7584. if (!pl->used && strcmp (pl->name, "NONE") != 0)
  7585. einfo (_("%X%P: section `%s' assigned to non-existent phdr `%s'\n"),
  7586. os->name, pl->name);
  7587. }
  7588. }
  7589. /* Record a list of sections which may not be cross referenced. */
  7590. void
  7591. lang_add_nocrossref (lang_nocrossref_type *l)
  7592. {
  7593. struct lang_nocrossrefs *n;
  7594. n = (struct lang_nocrossrefs *) xmalloc (sizeof *n);
  7595. n->next = nocrossref_list;
  7596. n->list = l;
  7597. n->onlyfirst = false;
  7598. nocrossref_list = n;
  7599. /* Set notice_all so that we get informed about all symbols. */
  7600. link_info.notice_all = true;
  7601. }
  7602. /* Record a section that cannot be referenced from a list of sections. */
  7603. void
  7604. lang_add_nocrossref_to (lang_nocrossref_type *l)
  7605. {
  7606. lang_add_nocrossref (l);
  7607. nocrossref_list->onlyfirst = true;
  7608. }
  7609. /* Overlay handling. We handle overlays with some static variables. */
  7610. /* The overlay virtual address. */
  7611. static etree_type *overlay_vma;
  7612. /* And subsection alignment. */
  7613. static etree_type *overlay_subalign;
  7614. /* An expression for the maximum section size seen so far. */
  7615. static etree_type *overlay_max;
  7616. /* A list of all the sections in this overlay. */
  7617. struct overlay_list {
  7618. struct overlay_list *next;
  7619. lang_output_section_statement_type *os;
  7620. };
  7621. static struct overlay_list *overlay_list;
  7622. /* Start handling an overlay. */
  7623. void
  7624. lang_enter_overlay (etree_type *vma_expr, etree_type *subalign)
  7625. {
  7626. /* The grammar should prevent nested overlays from occurring. */
  7627. ASSERT (overlay_vma == NULL
  7628. && overlay_subalign == NULL
  7629. && overlay_max == NULL);
  7630. overlay_vma = vma_expr;
  7631. overlay_subalign = subalign;
  7632. }
  7633. /* Start a section in an overlay. We handle this by calling
  7634. lang_enter_output_section_statement with the correct VMA.
  7635. lang_leave_overlay sets up the LMA and memory regions. */
  7636. void
  7637. lang_enter_overlay_section (const char *name)
  7638. {
  7639. struct overlay_list *n;
  7640. etree_type *size;
  7641. lang_enter_output_section_statement (name, overlay_vma, overlay_section,
  7642. 0, 0, overlay_subalign, 0, 0, 0);
  7643. /* If this is the first section, then base the VMA of future
  7644. sections on this one. This will work correctly even if `.' is
  7645. used in the addresses. */
  7646. if (overlay_list == NULL)
  7647. overlay_vma = exp_nameop (ADDR, name);
  7648. /* Remember the section. */
  7649. n = (struct overlay_list *) xmalloc (sizeof *n);
  7650. n->os = current_section;
  7651. n->next = overlay_list;
  7652. overlay_list = n;
  7653. size = exp_nameop (SIZEOF, name);
  7654. /* Arrange to work out the maximum section end address. */
  7655. if (overlay_max == NULL)
  7656. overlay_max = size;
  7657. else
  7658. overlay_max = exp_binop (MAX_K, overlay_max, size);
  7659. }
  7660. /* Finish a section in an overlay. There isn't any special to do
  7661. here. */
  7662. void
  7663. lang_leave_overlay_section (fill_type *fill,
  7664. lang_output_section_phdr_list *phdrs)
  7665. {
  7666. const char *name;
  7667. char *clean, *s2;
  7668. const char *s1;
  7669. char *buf;
  7670. name = current_section->name;
  7671. /* For now, assume that DEFAULT_MEMORY_REGION is the run-time memory
  7672. region and that no load-time region has been specified. It doesn't
  7673. really matter what we say here, since lang_leave_overlay will
  7674. override it. */
  7675. lang_leave_output_section_statement (fill, DEFAULT_MEMORY_REGION, phdrs, 0);
  7676. /* Define the magic symbols. */
  7677. clean = (char *) xmalloc (strlen (name) + 1);
  7678. s2 = clean;
  7679. for (s1 = name; *s1 != '\0'; s1++)
  7680. if (ISALNUM (*s1) || *s1 == '_')
  7681. *s2++ = *s1;
  7682. *s2 = '\0';
  7683. buf = (char *) xmalloc (strlen (clean) + sizeof "__load_start_");
  7684. sprintf (buf, "__load_start_%s", clean);
  7685. lang_add_assignment (exp_provide (buf,
  7686. exp_nameop (LOADADDR, name),
  7687. false));
  7688. buf = (char *) xmalloc (strlen (clean) + sizeof "__load_stop_");
  7689. sprintf (buf, "__load_stop_%s", clean);
  7690. lang_add_assignment (exp_provide (buf,
  7691. exp_binop ('+',
  7692. exp_nameop (LOADADDR, name),
  7693. exp_nameop (SIZEOF, name)),
  7694. false));
  7695. free (clean);
  7696. }
  7697. /* Finish an overlay. If there are any overlay wide settings, this
  7698. looks through all the sections in the overlay and sets them. */
  7699. void
  7700. lang_leave_overlay (etree_type *lma_expr,
  7701. int nocrossrefs,
  7702. fill_type *fill,
  7703. const char *memspec,
  7704. lang_output_section_phdr_list *phdrs,
  7705. const char *lma_memspec)
  7706. {
  7707. lang_memory_region_type *region;
  7708. lang_memory_region_type *lma_region;
  7709. struct overlay_list *l;
  7710. lang_nocrossref_type *nocrossref;
  7711. lang_get_regions (&region, &lma_region,
  7712. memspec, lma_memspec,
  7713. lma_expr != NULL, false);
  7714. nocrossref = NULL;
  7715. /* After setting the size of the last section, set '.' to end of the
  7716. overlay region. */
  7717. if (overlay_list != NULL)
  7718. {
  7719. overlay_list->os->update_dot = 1;
  7720. overlay_list->os->update_dot_tree
  7721. = exp_assign (".", exp_binop ('+', overlay_vma, overlay_max), false);
  7722. }
  7723. l = overlay_list;
  7724. while (l != NULL)
  7725. {
  7726. struct overlay_list *next;
  7727. if (fill != NULL && l->os->fill == NULL)
  7728. l->os->fill = fill;
  7729. l->os->region = region;
  7730. l->os->lma_region = lma_region;
  7731. /* The first section has the load address specified in the
  7732. OVERLAY statement. The rest are worked out from that.
  7733. The base address is not needed (and should be null) if
  7734. an LMA region was specified. */
  7735. if (l->next == 0)
  7736. {
  7737. l->os->load_base = lma_expr;
  7738. l->os->sectype = first_overlay_section;
  7739. }
  7740. if (phdrs != NULL && l->os->phdrs == NULL)
  7741. l->os->phdrs = phdrs;
  7742. if (nocrossrefs)
  7743. {
  7744. lang_nocrossref_type *nc;
  7745. nc = (lang_nocrossref_type *) xmalloc (sizeof *nc);
  7746. nc->name = l->os->name;
  7747. nc->next = nocrossref;
  7748. nocrossref = nc;
  7749. }
  7750. next = l->next;
  7751. free (l);
  7752. l = next;
  7753. }
  7754. if (nocrossref != NULL)
  7755. lang_add_nocrossref (nocrossref);
  7756. overlay_vma = NULL;
  7757. overlay_list = NULL;
  7758. overlay_max = NULL;
  7759. overlay_subalign = NULL;
  7760. }
  7761. /* Version handling. This is only useful for ELF. */
  7762. /* If PREV is NULL, return first version pattern matching particular symbol.
  7763. If PREV is non-NULL, return first version pattern matching particular
  7764. symbol after PREV (previously returned by lang_vers_match). */
  7765. static struct bfd_elf_version_expr *
  7766. lang_vers_match (struct bfd_elf_version_expr_head *head,
  7767. struct bfd_elf_version_expr *prev,
  7768. const char *sym)
  7769. {
  7770. const char *c_sym;
  7771. const char *cxx_sym = sym;
  7772. const char *java_sym = sym;
  7773. struct bfd_elf_version_expr *expr = NULL;
  7774. enum demangling_styles curr_style;
  7775. curr_style = CURRENT_DEMANGLING_STYLE;
  7776. cplus_demangle_set_style (no_demangling);
  7777. c_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_NO_OPTS);
  7778. if (!c_sym)
  7779. c_sym = sym;
  7780. cplus_demangle_set_style (curr_style);
  7781. if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
  7782. {
  7783. cxx_sym = bfd_demangle (link_info.output_bfd, sym,
  7784. DMGL_PARAMS | DMGL_ANSI);
  7785. if (!cxx_sym)
  7786. cxx_sym = sym;
  7787. }
  7788. if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
  7789. {
  7790. java_sym = bfd_demangle (link_info.output_bfd, sym, DMGL_JAVA);
  7791. if (!java_sym)
  7792. java_sym = sym;
  7793. }
  7794. if (head->htab && (prev == NULL || prev->literal))
  7795. {
  7796. struct bfd_elf_version_expr e;
  7797. switch (prev ? prev->mask : 0)
  7798. {
  7799. case 0:
  7800. if (head->mask & BFD_ELF_VERSION_C_TYPE)
  7801. {
  7802. e.pattern = c_sym;
  7803. expr = (struct bfd_elf_version_expr *)
  7804. htab_find ((htab_t) head->htab, &e);
  7805. while (expr && strcmp (expr->pattern, c_sym) == 0)
  7806. if (expr->mask == BFD_ELF_VERSION_C_TYPE)
  7807. goto out_ret;
  7808. else
  7809. expr = expr->next;
  7810. }
  7811. /* Fallthrough */
  7812. case BFD_ELF_VERSION_C_TYPE:
  7813. if (head->mask & BFD_ELF_VERSION_CXX_TYPE)
  7814. {
  7815. e.pattern = cxx_sym;
  7816. expr = (struct bfd_elf_version_expr *)
  7817. htab_find ((htab_t) head->htab, &e);
  7818. while (expr && strcmp (expr->pattern, cxx_sym) == 0)
  7819. if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
  7820. goto out_ret;
  7821. else
  7822. expr = expr->next;
  7823. }
  7824. /* Fallthrough */
  7825. case BFD_ELF_VERSION_CXX_TYPE:
  7826. if (head->mask & BFD_ELF_VERSION_JAVA_TYPE)
  7827. {
  7828. e.pattern = java_sym;
  7829. expr = (struct bfd_elf_version_expr *)
  7830. htab_find ((htab_t) head->htab, &e);
  7831. while (expr && strcmp (expr->pattern, java_sym) == 0)
  7832. if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
  7833. goto out_ret;
  7834. else
  7835. expr = expr->next;
  7836. }
  7837. /* Fallthrough */
  7838. default:
  7839. break;
  7840. }
  7841. }
  7842. /* Finally, try the wildcards. */
  7843. if (prev == NULL || prev->literal)
  7844. expr = head->remaining;
  7845. else
  7846. expr = prev->next;
  7847. for (; expr; expr = expr->next)
  7848. {
  7849. const char *s;
  7850. if (!expr->pattern)
  7851. continue;
  7852. if (expr->pattern[0] == '*' && expr->pattern[1] == '\0')
  7853. break;
  7854. if (expr->mask == BFD_ELF_VERSION_JAVA_TYPE)
  7855. s = java_sym;
  7856. else if (expr->mask == BFD_ELF_VERSION_CXX_TYPE)
  7857. s = cxx_sym;
  7858. else
  7859. s = c_sym;
  7860. if (fnmatch (expr->pattern, s, 0) == 0)
  7861. break;
  7862. }
  7863. out_ret:
  7864. if (c_sym != sym)
  7865. free ((char *) c_sym);
  7866. if (cxx_sym != sym)
  7867. free ((char *) cxx_sym);
  7868. if (java_sym != sym)
  7869. free ((char *) java_sym);
  7870. return expr;
  7871. }
  7872. /* Return NULL if the PATTERN argument is a glob pattern, otherwise,
  7873. return a pointer to the symbol name with any backslash quotes removed. */
  7874. static const char *
  7875. realsymbol (const char *pattern)
  7876. {
  7877. const char *p;
  7878. bool changed = false, backslash = false;
  7879. char *s, *symbol = (char *) xmalloc (strlen (pattern) + 1);
  7880. for (p = pattern, s = symbol; *p != '\0'; ++p)
  7881. {
  7882. /* It is a glob pattern only if there is no preceding
  7883. backslash. */
  7884. if (backslash)
  7885. {
  7886. /* Remove the preceding backslash. */
  7887. *(s - 1) = *p;
  7888. backslash = false;
  7889. changed = true;
  7890. }
  7891. else
  7892. {
  7893. if (*p == '?' || *p == '*' || *p == '[')
  7894. {
  7895. free (symbol);
  7896. return NULL;
  7897. }
  7898. *s++ = *p;
  7899. backslash = *p == '\\';
  7900. }
  7901. }
  7902. if (changed)
  7903. {
  7904. *s = '\0';
  7905. return symbol;
  7906. }
  7907. else
  7908. {
  7909. free (symbol);
  7910. return pattern;
  7911. }
  7912. }
  7913. /* This is called for each variable name or match expression. NEW_NAME is
  7914. the name of the symbol to match, or, if LITERAL_P is FALSE, a glob
  7915. pattern to be matched against symbol names. */
  7916. struct bfd_elf_version_expr *
  7917. lang_new_vers_pattern (struct bfd_elf_version_expr *orig,
  7918. const char *new_name,
  7919. const char *lang,
  7920. bool literal_p)
  7921. {
  7922. struct bfd_elf_version_expr *ret;
  7923. ret = (struct bfd_elf_version_expr *) xmalloc (sizeof *ret);
  7924. ret->next = orig;
  7925. ret->symver = 0;
  7926. ret->script = 0;
  7927. ret->literal = true;
  7928. ret->pattern = literal_p ? new_name : realsymbol (new_name);
  7929. if (ret->pattern == NULL)
  7930. {
  7931. ret->pattern = new_name;
  7932. ret->literal = false;
  7933. }
  7934. if (lang == NULL || strcasecmp (lang, "C") == 0)
  7935. ret->mask = BFD_ELF_VERSION_C_TYPE;
  7936. else if (strcasecmp (lang, "C++") == 0)
  7937. ret->mask = BFD_ELF_VERSION_CXX_TYPE;
  7938. else if (strcasecmp (lang, "Java") == 0)
  7939. ret->mask = BFD_ELF_VERSION_JAVA_TYPE;
  7940. else
  7941. {
  7942. einfo (_("%X%P: unknown language `%s' in version information\n"),
  7943. lang);
  7944. ret->mask = BFD_ELF_VERSION_C_TYPE;
  7945. }
  7946. return ldemul_new_vers_pattern (ret);
  7947. }
  7948. /* This is called for each set of variable names and match
  7949. expressions. */
  7950. struct bfd_elf_version_tree *
  7951. lang_new_vers_node (struct bfd_elf_version_expr *globals,
  7952. struct bfd_elf_version_expr *locals)
  7953. {
  7954. struct bfd_elf_version_tree *ret;
  7955. ret = (struct bfd_elf_version_tree *) xcalloc (1, sizeof *ret);
  7956. ret->globals.list = globals;
  7957. ret->locals.list = locals;
  7958. ret->match = lang_vers_match;
  7959. ret->name_indx = (unsigned int) -1;
  7960. return ret;
  7961. }
  7962. /* This static variable keeps track of version indices. */
  7963. static int version_index;
  7964. static hashval_t
  7965. version_expr_head_hash (const void *p)
  7966. {
  7967. const struct bfd_elf_version_expr *e =
  7968. (const struct bfd_elf_version_expr *) p;
  7969. return htab_hash_string (e->pattern);
  7970. }
  7971. static int
  7972. version_expr_head_eq (const void *p1, const void *p2)
  7973. {
  7974. const struct bfd_elf_version_expr *e1 =
  7975. (const struct bfd_elf_version_expr *) p1;
  7976. const struct bfd_elf_version_expr *e2 =
  7977. (const struct bfd_elf_version_expr *) p2;
  7978. return strcmp (e1->pattern, e2->pattern) == 0;
  7979. }
  7980. static void
  7981. lang_finalize_version_expr_head (struct bfd_elf_version_expr_head *head)
  7982. {
  7983. size_t count = 0;
  7984. struct bfd_elf_version_expr *e, *next;
  7985. struct bfd_elf_version_expr **list_loc, **remaining_loc;
  7986. for (e = head->list; e; e = e->next)
  7987. {
  7988. if (e->literal)
  7989. count++;
  7990. head->mask |= e->mask;
  7991. }
  7992. if (count)
  7993. {
  7994. head->htab = htab_create (count * 2, version_expr_head_hash,
  7995. version_expr_head_eq, NULL);
  7996. list_loc = &head->list;
  7997. remaining_loc = &head->remaining;
  7998. for (e = head->list; e; e = next)
  7999. {
  8000. next = e->next;
  8001. if (!e->literal)
  8002. {
  8003. *remaining_loc = e;
  8004. remaining_loc = &e->next;
  8005. }
  8006. else
  8007. {
  8008. void **loc = htab_find_slot ((htab_t) head->htab, e, INSERT);
  8009. if (*loc)
  8010. {
  8011. struct bfd_elf_version_expr *e1, *last;
  8012. e1 = (struct bfd_elf_version_expr *) *loc;
  8013. last = NULL;
  8014. do
  8015. {
  8016. if (e1->mask == e->mask)
  8017. {
  8018. last = NULL;
  8019. break;
  8020. }
  8021. last = e1;
  8022. e1 = e1->next;
  8023. }
  8024. while (e1 && strcmp (e1->pattern, e->pattern) == 0);
  8025. if (last == NULL)
  8026. {
  8027. /* This is a duplicate. */
  8028. /* FIXME: Memory leak. Sometimes pattern is not
  8029. xmalloced alone, but in larger chunk of memory. */
  8030. /* free (e->pattern); */
  8031. free (e);
  8032. }
  8033. else
  8034. {
  8035. e->next = last->next;
  8036. last->next = e;
  8037. }
  8038. }
  8039. else
  8040. {
  8041. *loc = e;
  8042. *list_loc = e;
  8043. list_loc = &e->next;
  8044. }
  8045. }
  8046. }
  8047. *remaining_loc = NULL;
  8048. *list_loc = head->remaining;
  8049. }
  8050. else
  8051. head->remaining = head->list;
  8052. }
  8053. /* This is called when we know the name and dependencies of the
  8054. version. */
  8055. void
  8056. lang_register_vers_node (const char *name,
  8057. struct bfd_elf_version_tree *version,
  8058. struct bfd_elf_version_deps *deps)
  8059. {
  8060. struct bfd_elf_version_tree *t, **pp;
  8061. struct bfd_elf_version_expr *e1;
  8062. if (name == NULL)
  8063. name = "";
  8064. if (link_info.version_info != NULL
  8065. && (name[0] == '\0' || link_info.version_info->name[0] == '\0'))
  8066. {
  8067. einfo (_("%X%P: anonymous version tag cannot be combined"
  8068. " with other version tags\n"));
  8069. free (version);
  8070. return;
  8071. }
  8072. /* Make sure this node has a unique name. */
  8073. for (t = link_info.version_info; t != NULL; t = t->next)
  8074. if (strcmp (t->name, name) == 0)
  8075. einfo (_("%X%P: duplicate version tag `%s'\n"), name);
  8076. lang_finalize_version_expr_head (&version->globals);
  8077. lang_finalize_version_expr_head (&version->locals);
  8078. /* Check the global and local match names, and make sure there
  8079. aren't any duplicates. */
  8080. for (e1 = version->globals.list; e1 != NULL; e1 = e1->next)
  8081. {
  8082. for (t = link_info.version_info; t != NULL; t = t->next)
  8083. {
  8084. struct bfd_elf_version_expr *e2;
  8085. if (t->locals.htab && e1->literal)
  8086. {
  8087. e2 = (struct bfd_elf_version_expr *)
  8088. htab_find ((htab_t) t->locals.htab, e1);
  8089. while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
  8090. {
  8091. if (e1->mask == e2->mask)
  8092. einfo (_("%X%P: duplicate expression `%s'"
  8093. " in version information\n"), e1->pattern);
  8094. e2 = e2->next;
  8095. }
  8096. }
  8097. else if (!e1->literal)
  8098. for (e2 = t->locals.remaining; e2 != NULL; e2 = e2->next)
  8099. if (strcmp (e1->pattern, e2->pattern) == 0
  8100. && e1->mask == e2->mask)
  8101. einfo (_("%X%P: duplicate expression `%s'"
  8102. " in version information\n"), e1->pattern);
  8103. }
  8104. }
  8105. for (e1 = version->locals.list; e1 != NULL; e1 = e1->next)
  8106. {
  8107. for (t = link_info.version_info; t != NULL; t = t->next)
  8108. {
  8109. struct bfd_elf_version_expr *e2;
  8110. if (t->globals.htab && e1->literal)
  8111. {
  8112. e2 = (struct bfd_elf_version_expr *)
  8113. htab_find ((htab_t) t->globals.htab, e1);
  8114. while (e2 && strcmp (e1->pattern, e2->pattern) == 0)
  8115. {
  8116. if (e1->mask == e2->mask)
  8117. einfo (_("%X%P: duplicate expression `%s'"
  8118. " in version information\n"),
  8119. e1->pattern);
  8120. e2 = e2->next;
  8121. }
  8122. }
  8123. else if (!e1->literal)
  8124. for (e2 = t->globals.remaining; e2 != NULL; e2 = e2->next)
  8125. if (strcmp (e1->pattern, e2->pattern) == 0
  8126. && e1->mask == e2->mask)
  8127. einfo (_("%X%P: duplicate expression `%s'"
  8128. " in version information\n"), e1->pattern);
  8129. }
  8130. }
  8131. version->deps = deps;
  8132. version->name = name;
  8133. if (name[0] != '\0')
  8134. {
  8135. ++version_index;
  8136. version->vernum = version_index;
  8137. }
  8138. else
  8139. version->vernum = 0;
  8140. for (pp = &link_info.version_info; *pp != NULL; pp = &(*pp)->next)
  8141. ;
  8142. *pp = version;
  8143. }
  8144. /* This is called when we see a version dependency. */
  8145. struct bfd_elf_version_deps *
  8146. lang_add_vers_depend (struct bfd_elf_version_deps *list, const char *name)
  8147. {
  8148. struct bfd_elf_version_deps *ret;
  8149. struct bfd_elf_version_tree *t;
  8150. ret = (struct bfd_elf_version_deps *) xmalloc (sizeof *ret);
  8151. ret->next = list;
  8152. for (t = link_info.version_info; t != NULL; t = t->next)
  8153. {
  8154. if (strcmp (t->name, name) == 0)
  8155. {
  8156. ret->version_needed = t;
  8157. return ret;
  8158. }
  8159. }
  8160. einfo (_("%X%P: unable to find version dependency `%s'\n"), name);
  8161. ret->version_needed = NULL;
  8162. return ret;
  8163. }
  8164. static void
  8165. lang_do_version_exports_section (void)
  8166. {
  8167. struct bfd_elf_version_expr *greg = NULL, *lreg;
  8168. LANG_FOR_EACH_INPUT_STATEMENT (is)
  8169. {
  8170. asection *sec = bfd_get_section_by_name (is->the_bfd, ".exports");
  8171. char *contents, *p;
  8172. bfd_size_type len;
  8173. if (sec == NULL)
  8174. continue;
  8175. len = sec->size;
  8176. contents = (char *) xmalloc (len);
  8177. if (!bfd_get_section_contents (is->the_bfd, sec, contents, 0, len))
  8178. einfo (_("%X%P: unable to read .exports section contents\n"), sec);
  8179. p = contents;
  8180. while (p < contents + len)
  8181. {
  8182. greg = lang_new_vers_pattern (greg, p, NULL, false);
  8183. p = strchr (p, '\0') + 1;
  8184. }
  8185. /* Do not free the contents, as we used them creating the regex. */
  8186. /* Do not include this section in the link. */
  8187. sec->flags |= SEC_EXCLUDE | SEC_KEEP;
  8188. }
  8189. lreg = lang_new_vers_pattern (NULL, "*", NULL, false);
  8190. lang_register_vers_node (command_line.version_exports_section,
  8191. lang_new_vers_node (greg, lreg), NULL);
  8192. }
  8193. /* Evaluate LENGTH and ORIGIN parts of MEMORY spec. This is initially
  8194. called with UPDATE_REGIONS_P set to FALSE, in this case no errors are
  8195. thrown, however, references to symbols in the origin and length fields
  8196. will be pushed into the symbol table, this allows PROVIDE statements to
  8197. then provide these symbols. This function is called a second time with
  8198. UPDATE_REGIONS_P set to TRUE, this time the we update the actual region
  8199. data structures, and throw errors if missing symbols are encountered. */
  8200. static void
  8201. lang_do_memory_regions (bool update_regions_p)
  8202. {
  8203. lang_memory_region_type *r = lang_memory_region_list;
  8204. for (; r != NULL; r = r->next)
  8205. {
  8206. if (r->origin_exp)
  8207. {
  8208. exp_fold_tree_no_dot (r->origin_exp);
  8209. if (update_regions_p)
  8210. {
  8211. if (expld.result.valid_p)
  8212. {
  8213. r->origin = expld.result.value;
  8214. r->current = r->origin;
  8215. }
  8216. else
  8217. einfo (_("%P: invalid origin for memory region %s\n"),
  8218. r->name_list.name);
  8219. }
  8220. }
  8221. if (r->length_exp)
  8222. {
  8223. exp_fold_tree_no_dot (r->length_exp);
  8224. if (update_regions_p)
  8225. {
  8226. if (expld.result.valid_p)
  8227. r->length = expld.result.value;
  8228. else
  8229. einfo (_("%P: invalid length for memory region %s\n"),
  8230. r->name_list.name);
  8231. }
  8232. }
  8233. }
  8234. }
  8235. void
  8236. lang_add_unique (const char *name)
  8237. {
  8238. struct unique_sections *ent;
  8239. for (ent = unique_section_list; ent; ent = ent->next)
  8240. if (strcmp (ent->name, name) == 0)
  8241. return;
  8242. ent = (struct unique_sections *) xmalloc (sizeof *ent);
  8243. ent->name = xstrdup (name);
  8244. ent->next = unique_section_list;
  8245. unique_section_list = ent;
  8246. }
  8247. /* Append the list of dynamic symbols to the existing one. */
  8248. void
  8249. lang_append_dynamic_list (struct bfd_elf_dynamic_list **list_p,
  8250. struct bfd_elf_version_expr *dynamic)
  8251. {
  8252. if (*list_p)
  8253. {
  8254. struct bfd_elf_version_expr *tail;
  8255. for (tail = dynamic; tail->next != NULL; tail = tail->next)
  8256. ;
  8257. tail->next = (*list_p)->head.list;
  8258. (*list_p)->head.list = dynamic;
  8259. }
  8260. else
  8261. {
  8262. struct bfd_elf_dynamic_list *d;
  8263. d = (struct bfd_elf_dynamic_list *) xcalloc (1, sizeof *d);
  8264. d->head.list = dynamic;
  8265. d->match = lang_vers_match;
  8266. *list_p = d;
  8267. }
  8268. }
  8269. /* Append the list of C++ typeinfo dynamic symbols to the existing
  8270. one. */
  8271. void
  8272. lang_append_dynamic_list_cpp_typeinfo (void)
  8273. {
  8274. const char *symbols[] =
  8275. {
  8276. "typeinfo name for*",
  8277. "typeinfo for*"
  8278. };
  8279. struct bfd_elf_version_expr *dynamic = NULL;
  8280. unsigned int i;
  8281. for (i = 0; i < ARRAY_SIZE (symbols); i++)
  8282. dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
  8283. false);
  8284. lang_append_dynamic_list (&link_info.dynamic_list, dynamic);
  8285. }
  8286. /* Append the list of C++ operator new and delete dynamic symbols to the
  8287. existing one. */
  8288. void
  8289. lang_append_dynamic_list_cpp_new (void)
  8290. {
  8291. const char *symbols[] =
  8292. {
  8293. "operator new*",
  8294. "operator delete*"
  8295. };
  8296. struct bfd_elf_version_expr *dynamic = NULL;
  8297. unsigned int i;
  8298. for (i = 0; i < ARRAY_SIZE (symbols); i++)
  8299. dynamic = lang_new_vers_pattern (dynamic, symbols [i], "C++",
  8300. false);
  8301. lang_append_dynamic_list (&link_info.dynamic_list, dynamic);
  8302. }
  8303. /* Scan a space and/or comma separated string of features. */
  8304. void
  8305. lang_ld_feature (char *str)
  8306. {
  8307. char *p, *q;
  8308. p = str;
  8309. while (*p)
  8310. {
  8311. char sep;
  8312. while (*p == ',' || ISSPACE (*p))
  8313. ++p;
  8314. if (!*p)
  8315. break;
  8316. q = p + 1;
  8317. while (*q && *q != ',' && !ISSPACE (*q))
  8318. ++q;
  8319. sep = *q;
  8320. *q = 0;
  8321. if (strcasecmp (p, "SANE_EXPR") == 0)
  8322. config.sane_expr = true;
  8323. else
  8324. einfo (_("%X%P: unknown feature `%s'\n"), p);
  8325. *q = sep;
  8326. p = q;
  8327. }
  8328. }
  8329. /* Pretty print memory amount. */
  8330. static void
  8331. lang_print_memory_size (bfd_vma sz)
  8332. {
  8333. if ((sz & 0x3fffffff) == 0)
  8334. printf ("%10" BFD_VMA_FMT "u GB", sz >> 30);
  8335. else if ((sz & 0xfffff) == 0)
  8336. printf ("%10" BFD_VMA_FMT "u MB", sz >> 20);
  8337. else if ((sz & 0x3ff) == 0)
  8338. printf ("%10" BFD_VMA_FMT "u KB", sz >> 10);
  8339. else
  8340. printf (" %10" BFD_VMA_FMT "u B", sz);
  8341. }
  8342. /* Implement --print-memory-usage: disply per region memory usage. */
  8343. void
  8344. lang_print_memory_usage (void)
  8345. {
  8346. lang_memory_region_type *r;
  8347. printf ("Memory region Used Size Region Size %%age Used\n");
  8348. for (r = lang_memory_region_list; r->next != NULL; r = r->next)
  8349. {
  8350. bfd_vma used_length = r->current - r->origin;
  8351. printf ("%16s: ",r->name_list.name);
  8352. lang_print_memory_size (used_length);
  8353. lang_print_memory_size ((bfd_vma) r->length);
  8354. if (r->length != 0)
  8355. {
  8356. double percent = used_length * 100.0 / r->length;
  8357. printf (" %6.2f%%", percent);
  8358. }
  8359. printf ("\n");
  8360. }
  8361. }