ldlang.c 224 KB

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