coffcode.h 175 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099
  1. /* Support for the generic parts of most COFF variants, for BFD.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. Written by Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /* Most of this hacked by Steve Chamberlain,
  18. sac@cygnus.com. */
  19. /*
  20. SECTION
  21. coff backends
  22. BFD supports a number of different flavours of coff format.
  23. The major differences between formats are the sizes and
  24. alignments of fields in structures on disk, and the occasional
  25. extra field.
  26. Coff in all its varieties is implemented with a few common
  27. files and a number of implementation specific files. For
  28. example, the i386 coff format is implemented in the file
  29. @file{coff-i386.c}. This file @code{#include}s
  30. @file{coff/i386.h} which defines the external structure of the
  31. coff format for the i386, and @file{coff/internal.h} which
  32. defines the internal structure. @file{coff-i386.c} also
  33. defines the relocations used by the i386 coff format
  34. @xref{Relocations}.
  35. SUBSECTION
  36. Porting to a new version of coff
  37. The recommended method is to select from the existing
  38. implementations the version of coff which is most like the one
  39. you want to use. For example, we'll say that i386 coff is
  40. the one you select, and that your coff flavour is called foo.
  41. Copy @file{i386coff.c} to @file{foocoff.c}, copy
  42. @file{../include/coff/i386.h} to @file{../include/coff/foo.h},
  43. and add the lines to @file{targets.c} and @file{Makefile.in}
  44. so that your new back end is used. Alter the shapes of the
  45. structures in @file{../include/coff/foo.h} so that they match
  46. what you need. You will probably also have to add
  47. @code{#ifdef}s to the code in @file{coff/internal.h} and
  48. @file{coffcode.h} if your version of coff is too wild.
  49. You can verify that your new BFD backend works quite simply by
  50. building @file{objdump} from the @file{binutils} directory,
  51. and making sure that its version of what's going on and your
  52. host system's idea (assuming it has the pretty standard coff
  53. dump utility, usually called @code{att-dump} or just
  54. @code{dump}) are the same. Then clean up your code, and send
  55. what you've done to Cygnus. Then your stuff will be in the
  56. next release, and you won't have to keep integrating it.
  57. SUBSECTION
  58. How the coff backend works
  59. SUBSUBSECTION
  60. File layout
  61. The Coff backend is split into generic routines that are
  62. applicable to any Coff target and routines that are specific
  63. to a particular target. The target-specific routines are
  64. further split into ones which are basically the same for all
  65. Coff targets except that they use the external symbol format
  66. or use different values for certain constants.
  67. The generic routines are in @file{coffgen.c}. These routines
  68. work for any Coff target. They use some hooks into the target
  69. specific code; the hooks are in a @code{bfd_coff_backend_data}
  70. structure, one of which exists for each target.
  71. The essentially similar target-specific routines are in
  72. @file{coffcode.h}. This header file includes executable C code.
  73. The various Coff targets first include the appropriate Coff
  74. header file, make any special defines that are needed, and
  75. then include @file{coffcode.h}.
  76. Some of the Coff targets then also have additional routines in
  77. the target source file itself.
  78. SUBSUBSECTION
  79. Coff long section names
  80. In the standard Coff object format, section names are limited to
  81. the eight bytes available in the @code{s_name} field of the
  82. @code{SCNHDR} section header structure. The format requires the
  83. field to be NUL-padded, but not necessarily NUL-terminated, so
  84. the longest section names permitted are a full eight characters.
  85. The Microsoft PE variants of the Coff object file format add
  86. an extension to support the use of long section names. This
  87. extension is defined in section 4 of the Microsoft PE/COFF
  88. specification (rev 8.1). If a section name is too long to fit
  89. into the section header's @code{s_name} field, it is instead
  90. placed into the string table, and the @code{s_name} field is
  91. filled with a slash ("/") followed by the ASCII decimal
  92. representation of the offset of the full name relative to the
  93. string table base.
  94. Note that this implies that the extension can only be used in object
  95. files, as executables do not contain a string table. The standard
  96. specifies that long section names from objects emitted into executable
  97. images are to be truncated.
  98. However, as a GNU extension, BFD can generate executable images
  99. that contain a string table and long section names. This
  100. would appear to be technically valid, as the standard only says
  101. that Coff debugging information is deprecated, not forbidden,
  102. and in practice it works, although some tools that parse PE files
  103. expecting the MS standard format may become confused; @file{PEview} is
  104. one known example.
  105. The functionality is supported in BFD by code implemented under
  106. the control of the macro @code{COFF_LONG_SECTION_NAMES}. If not
  107. defined, the format does not support long section names in any way.
  108. If defined, it is used to initialise a flag,
  109. @code{_bfd_coff_long_section_names}, and a hook function pointer,
  110. @code{_bfd_coff_set_long_section_names}, in the Coff backend data
  111. structure. The flag controls the generation of long section names
  112. in output BFDs at runtime; if it is false, as it will be by default
  113. when generating an executable image, long section names are truncated;
  114. if true, the long section names extension is employed. The hook
  115. points to a function that allows the value of the flag to be altered
  116. at runtime, on formats that support long section names at all; on
  117. other formats it points to a stub that returns an error indication.
  118. With input BFDs, the flag is set according to whether any long section
  119. names are detected while reading the section headers. For a completely
  120. new BFD, the flag is set to the default for the target format. This
  121. information can be used by a client of the BFD library when deciding
  122. what output format to generate, and means that a BFD that is opened
  123. for read and subsequently converted to a writeable BFD and modified
  124. in-place will retain whatever format it had on input.
  125. If @code{COFF_LONG_SECTION_NAMES} is simply defined (blank), or is
  126. defined to the value "1", then long section names are enabled by
  127. default; if it is defined to the value zero, they are disabled by
  128. default (but still accepted in input BFDs). The header @file{coffcode.h}
  129. defines a macro, @code{COFF_DEFAULT_LONG_SECTION_NAMES}, which is
  130. used in the backends to initialise the backend data structure fields
  131. appropriately; see the comments for further detail.
  132. SUBSUBSECTION
  133. Bit twiddling
  134. Each flavour of coff supported in BFD has its own header file
  135. describing the external layout of the structures. There is also
  136. an internal description of the coff layout, in
  137. @file{coff/internal.h}. A major function of the
  138. coff backend is swapping the bytes and twiddling the bits to
  139. translate the external form of the structures into the normal
  140. internal form. This is all performed in the
  141. @code{bfd_swap}_@i{thing}_@i{direction} routines. Some
  142. elements are different sizes between different versions of
  143. coff; it is the duty of the coff version specific include file
  144. to override the definitions of various packing routines in
  145. @file{coffcode.h}. E.g., the size of line number entry in coff is
  146. sometimes 16 bits, and sometimes 32 bits. @code{#define}ing
  147. @code{PUT_LNSZ_LNNO} and @code{GET_LNSZ_LNNO} will select the
  148. correct one. No doubt, some day someone will find a version of
  149. coff which has a varying field size not catered to at the
  150. moment. To port BFD, that person will have to add more @code{#defines}.
  151. Three of the bit twiddling routines are exported to
  152. @code{gdb}; @code{coff_swap_aux_in}, @code{coff_swap_sym_in}
  153. and @code{coff_swap_lineno_in}. @code{GDB} reads the symbol
  154. table on its own, but uses BFD to fix things up. More of the
  155. bit twiddlers are exported for @code{gas};
  156. @code{coff_swap_aux_out}, @code{coff_swap_sym_out},
  157. @code{coff_swap_lineno_out}, @code{coff_swap_reloc_out},
  158. @code{coff_swap_filehdr_out}, @code{coff_swap_aouthdr_out},
  159. @code{coff_swap_scnhdr_out}. @code{Gas} currently keeps track
  160. of all the symbol table and reloc drudgery itself, thereby
  161. saving the internal BFD overhead, but uses BFD to swap things
  162. on the way out, making cross ports much safer. Doing so also
  163. allows BFD (and thus the linker) to use the same header files
  164. as @code{gas}, which makes one avenue to disaster disappear.
  165. SUBSUBSECTION
  166. Symbol reading
  167. The simple canonical form for symbols used by BFD is not rich
  168. enough to keep all the information available in a coff symbol
  169. table. The back end gets around this problem by keeping the original
  170. symbol table around, "behind the scenes".
  171. When a symbol table is requested (through a call to
  172. @code{bfd_canonicalize_symtab}), a request gets through to
  173. @code{coff_get_normalized_symtab}. This reads the symbol table from
  174. the coff file and swaps all the structures inside into the
  175. internal form. It also fixes up all the pointers in the table
  176. (represented in the file by offsets from the first symbol in
  177. the table) into physical pointers to elements in the new
  178. internal table. This involves some work since the meanings of
  179. fields change depending upon context: a field that is a
  180. pointer to another structure in the symbol table at one moment
  181. may be the size in bytes of a structure at the next. Another
  182. pass is made over the table. All symbols which mark file names
  183. (<<C_FILE>> symbols) are modified so that the internal
  184. string points to the value in the auxent (the real filename)
  185. rather than the normal text associated with the symbol
  186. (@code{".file"}).
  187. At this time the symbol names are moved around. Coff stores
  188. all symbols less than nine characters long physically
  189. within the symbol table; longer strings are kept at the end of
  190. the file in the string table. This pass moves all strings
  191. into memory and replaces them with pointers to the strings.
  192. The symbol table is massaged once again, this time to create
  193. the canonical table used by the BFD application. Each symbol
  194. is inspected in turn, and a decision made (using the
  195. @code{sclass} field) about the various flags to set in the
  196. @code{asymbol}. @xref{Symbols}. The generated canonical table
  197. shares strings with the hidden internal symbol table.
  198. Any linenumbers are read from the coff file too, and attached
  199. to the symbols which own the functions the linenumbers belong to.
  200. SUBSUBSECTION
  201. Symbol writing
  202. Writing a symbol to a coff file which didn't come from a coff
  203. file will lose any debugging information. The @code{asymbol}
  204. structure remembers the BFD from which the symbol was taken, and on
  205. output the back end makes sure that the same destination target as
  206. source target is present.
  207. When the symbols have come from a coff file then all the
  208. debugging information is preserved.
  209. Symbol tables are provided for writing to the back end in a
  210. vector of pointers to pointers. This allows applications like
  211. the linker to accumulate and output large symbol tables
  212. without having to do too much byte copying.
  213. This function runs through the provided symbol table and
  214. patches each symbol marked as a file place holder
  215. (@code{C_FILE}) to point to the next file place holder in the
  216. list. It also marks each @code{offset} field in the list with
  217. the offset from the first symbol of the current symbol.
  218. Another function of this procedure is to turn the canonical
  219. value form of BFD into the form used by coff. Internally, BFD
  220. expects symbol values to be offsets from a section base; so a
  221. symbol physically at 0x120, but in a section starting at
  222. 0x100, would have the value 0x20. Coff expects symbols to
  223. contain their final value, so symbols have their values
  224. changed at this point to reflect their sum with their owning
  225. section. This transformation uses the
  226. <<output_section>> field of the @code{asymbol}'s
  227. @code{asection} @xref{Sections}.
  228. o <<coff_mangle_symbols>>
  229. This routine runs though the provided symbol table and uses
  230. the offsets generated by the previous pass and the pointers
  231. generated when the symbol table was read in to create the
  232. structured hierarchy required by coff. It changes each pointer
  233. to a symbol into the index into the symbol table of the asymbol.
  234. o <<coff_write_symbols>>
  235. This routine runs through the symbol table and patches up the
  236. symbols from their internal form into the coff way, calls the
  237. bit twiddlers, and writes out the table to the file.
  238. */
  239. /*
  240. INTERNAL_DEFINITION
  241. coff_symbol_type
  242. DESCRIPTION
  243. The hidden information for an <<asymbol>> is described in a
  244. <<combined_entry_type>>:
  245. CODE_FRAGMENT
  246. .
  247. .typedef struct coff_ptr_struct
  248. .{
  249. . {* Remembers the offset from the first symbol in the file for
  250. . this symbol. Generated by coff_renumber_symbols. *}
  251. . unsigned int offset;
  252. .
  253. . {* Should the value of this symbol be renumbered. Used for
  254. . XCOFF C_BSTAT symbols. Set by coff_slurp_symbol_table. *}
  255. . unsigned int fix_value : 1;
  256. .
  257. . {* Should the tag field of this symbol be renumbered.
  258. . Created by coff_pointerize_aux. *}
  259. . unsigned int fix_tag : 1;
  260. .
  261. . {* Should the endidx field of this symbol be renumbered.
  262. . Created by coff_pointerize_aux. *}
  263. . unsigned int fix_end : 1;
  264. .
  265. . {* Should the x_csect.x_scnlen field be renumbered.
  266. . Created by coff_pointerize_aux. *}
  267. . unsigned int fix_scnlen : 1;
  268. .
  269. . {* Fix up an XCOFF C_BINCL/C_EINCL symbol. The value is the
  270. . index into the line number entries. Set by coff_slurp_symbol_table. *}
  271. . unsigned int fix_line : 1;
  272. .
  273. . {* The container for the symbol structure as read and translated
  274. . from the file. *}
  275. . union
  276. . {
  277. . union internal_auxent auxent;
  278. . struct internal_syment syment;
  279. . } u;
  280. .
  281. . {* Selector for the union above. *}
  282. . bool is_sym;
  283. .
  284. . {* An extra pointer which can used by format based on COFF (like XCOFF)
  285. . to provide extra information to their backend. *}
  286. . void *extrap;
  287. .} combined_entry_type;
  288. .
  289. .
  290. .{* Each canonical asymbol really looks like this: *}
  291. .
  292. .typedef struct coff_symbol_struct
  293. .{
  294. . {* The actual symbol which the rest of BFD works with *}
  295. . asymbol symbol;
  296. .
  297. . {* A pointer to the hidden information for this symbol *}
  298. . combined_entry_type *native;
  299. .
  300. . {* A pointer to the linenumber information for this symbol *}
  301. . struct lineno_cache_entry *lineno;
  302. .
  303. . {* Have the line numbers been relocated yet ? *}
  304. . bool done_lineno;
  305. .} coff_symbol_type;
  306. */
  307. #include "libiberty.h"
  308. #ifdef COFF_WITH_PE
  309. #include "peicode.h"
  310. #else
  311. #include "coffswap.h"
  312. #endif
  313. #define STRING_SIZE_SIZE 4
  314. #define DOT_DEBUG ".debug"
  315. #define DOT_ZDEBUG ".zdebug"
  316. #define GNU_LINKONCE_WI ".gnu.linkonce.wi."
  317. #define GNU_LINKONCE_WT ".gnu.linkonce.wt."
  318. #define DOT_RELOC ".reloc"
  319. #if defined(COFF_WITH_PE) || defined(COFF_GO32_EXE) || defined(COFF_GO32)
  320. # define COFF_WITH_EXTENDED_RELOC_COUNTER
  321. #endif
  322. #if defined (COFF_LONG_SECTION_NAMES)
  323. /* Needed to expand the inputs to BLANKOR1TOODD. */
  324. #define COFFLONGSECTIONCATHELPER(x,y) x ## y
  325. /* If the input macro Y is blank or '1', return an odd number; if it is
  326. '0', return an even number. Result undefined in all other cases. */
  327. #define BLANKOR1TOODD(y) COFFLONGSECTIONCATHELPER(1,y)
  328. /* Defined to numerical 0 or 1 according to whether generation of long
  329. section names is disabled or enabled by default. */
  330. #define COFF_ENABLE_LONG_SECTION_NAMES (BLANKOR1TOODD(COFF_LONG_SECTION_NAMES) & 1)
  331. /* Where long section names are supported, we allow them to be enabled
  332. and disabled at runtime, so select an appropriate hook function for
  333. _bfd_coff_set_long_section_names. */
  334. #define COFF_LONG_SECTION_NAMES_SETTER bfd_coff_set_long_section_names_allowed
  335. #else /* !defined (COFF_LONG_SECTION_NAMES) */
  336. /* If long section names are not supported, this stub disallows any
  337. attempt to enable them at run-time. */
  338. #define COFF_LONG_SECTION_NAMES_SETTER bfd_coff_set_long_section_names_disallowed
  339. #endif /* defined (COFF_LONG_SECTION_NAMES) */
  340. /* Define a macro that can be used to initialise both the fields relating
  341. to long section names in the backend data struct simultaneously. */
  342. #if COFF_ENABLE_LONG_SECTION_NAMES
  343. #define COFF_DEFAULT_LONG_SECTION_NAMES (true), COFF_LONG_SECTION_NAMES_SETTER
  344. #else /* !COFF_ENABLE_LONG_SECTION_NAMES */
  345. #define COFF_DEFAULT_LONG_SECTION_NAMES (false), COFF_LONG_SECTION_NAMES_SETTER
  346. #endif /* COFF_ENABLE_LONG_SECTION_NAMES */
  347. #if defined (COFF_LONG_SECTION_NAMES)
  348. static bool bfd_coff_set_long_section_names_allowed
  349. (bfd *, int);
  350. #else /* !defined (COFF_LONG_SECTION_NAMES) */
  351. static bool bfd_coff_set_long_section_names_disallowed
  352. (bfd *, int);
  353. #endif /* defined (COFF_LONG_SECTION_NAMES) */
  354. static long sec_to_styp_flags
  355. (const char *, flagword);
  356. static bool styp_to_sec_flags
  357. (bfd *, void *, const char *, asection *, flagword *);
  358. static bool coff_bad_format_hook
  359. (bfd *, void *);
  360. static void coff_set_custom_section_alignment
  361. (bfd *, asection *, const struct coff_section_alignment_entry *,
  362. const unsigned int);
  363. static bool coff_new_section_hook
  364. (bfd *, asection *);
  365. static bool coff_set_arch_mach_hook
  366. (bfd *, void *);
  367. static bool coff_write_relocs
  368. (bfd *, int);
  369. static bool coff_set_flags
  370. (bfd *, unsigned int *, unsigned short *);
  371. static bool coff_set_arch_mach
  372. (bfd *, enum bfd_architecture, unsigned long) ATTRIBUTE_UNUSED;
  373. static bool coff_compute_section_file_positions
  374. (bfd *);
  375. static bool coff_write_object_contents
  376. (bfd *) ATTRIBUTE_UNUSED;
  377. static bool coff_set_section_contents
  378. (bfd *, asection *, const void *, file_ptr, bfd_size_type);
  379. static bool coff_slurp_line_table
  380. (bfd *, asection *);
  381. static bool coff_slurp_symbol_table
  382. (bfd *);
  383. static enum coff_symbol_classification coff_classify_symbol
  384. (bfd *, struct internal_syment *);
  385. static bool coff_slurp_reloc_table
  386. (bfd *, asection *, asymbol **);
  387. static long coff_canonicalize_reloc
  388. (bfd *, asection *, arelent **, asymbol **);
  389. #ifndef coff_mkobject_hook
  390. static void * coff_mkobject_hook
  391. (bfd *, void *, void *);
  392. #endif
  393. #ifdef COFF_WITH_PE
  394. static flagword handle_COMDAT
  395. (bfd *, flagword, void *, const char *, asection *);
  396. #endif
  397. #ifdef TICOFF
  398. static bool ticoff0_bad_format_hook
  399. (bfd *, void * );
  400. static bool ticoff1_bad_format_hook
  401. (bfd *, void * );
  402. #endif
  403. /* void warning(); */
  404. #if defined (COFF_LONG_SECTION_NAMES)
  405. static bool
  406. bfd_coff_set_long_section_names_allowed (bfd *abfd, int enable)
  407. {
  408. coff_backend_info (abfd)->_bfd_coff_long_section_names = enable;
  409. return true;
  410. }
  411. #else /* !defined (COFF_LONG_SECTION_NAMES) */
  412. static bool
  413. bfd_coff_set_long_section_names_disallowed (bfd *abfd, int enable)
  414. {
  415. (void) abfd;
  416. (void) enable;
  417. return false;
  418. }
  419. #endif /* defined (COFF_LONG_SECTION_NAMES) */
  420. /* Return a word with STYP_* (scnhdr.s_flags) flags set to represent
  421. the incoming SEC_* flags. The inverse of this function is
  422. styp_to_sec_flags(). NOTE: If you add to/change this routine, you
  423. should probably mirror the changes in styp_to_sec_flags(). */
  424. #ifndef COFF_WITH_PE
  425. /* Macros for setting debugging flags. */
  426. #ifdef STYP_DEBUG
  427. #define STYP_XCOFF_DEBUG STYP_DEBUG
  428. #else
  429. #define STYP_XCOFF_DEBUG STYP_INFO
  430. #endif
  431. #ifdef COFF_ALIGN_IN_S_FLAGS
  432. #define STYP_DEBUG_INFO STYP_DSECT
  433. #else
  434. #define STYP_DEBUG_INFO STYP_INFO
  435. #endif
  436. static long
  437. sec_to_styp_flags (const char *sec_name, flagword sec_flags)
  438. {
  439. long styp_flags = 0;
  440. if (!strcmp (sec_name, _TEXT))
  441. {
  442. styp_flags = STYP_TEXT;
  443. }
  444. else if (!strcmp (sec_name, _DATA))
  445. {
  446. styp_flags = STYP_DATA;
  447. }
  448. else if (!strcmp (sec_name, _BSS))
  449. {
  450. styp_flags = STYP_BSS;
  451. #ifdef _COMMENT
  452. }
  453. else if (!strcmp (sec_name, _COMMENT))
  454. {
  455. styp_flags = STYP_INFO;
  456. #endif /* _COMMENT */
  457. #ifdef _LIB
  458. }
  459. else if (!strcmp (sec_name, _LIB))
  460. {
  461. styp_flags = STYP_LIB;
  462. #endif /* _LIB */
  463. #ifdef _LIT
  464. }
  465. else if (!strcmp (sec_name, _LIT))
  466. {
  467. styp_flags = STYP_LIT;
  468. #endif /* _LIT */
  469. }
  470. else if (startswith (sec_name, DOT_DEBUG)
  471. || startswith (sec_name, DOT_ZDEBUG))
  472. {
  473. /* Handle the XCOFF debug section and DWARF2 debug sections. */
  474. if (!sec_name[6])
  475. styp_flags = STYP_XCOFF_DEBUG;
  476. else
  477. styp_flags = STYP_DEBUG_INFO;
  478. }
  479. else if (startswith (sec_name, ".stab"))
  480. {
  481. styp_flags = STYP_DEBUG_INFO;
  482. }
  483. #ifdef COFF_LONG_SECTION_NAMES
  484. else if (startswith (sec_name, GNU_LINKONCE_WI)
  485. || startswith (sec_name, GNU_LINKONCE_WT))
  486. {
  487. styp_flags = STYP_DEBUG_INFO;
  488. }
  489. #endif
  490. #ifdef RS6000COFF_C
  491. else if (!strcmp (sec_name, _TDATA))
  492. {
  493. styp_flags = STYP_TDATA;
  494. }
  495. else if (!strcmp (sec_name, _TBSS))
  496. {
  497. styp_flags = STYP_TBSS;
  498. }
  499. else if (!strcmp (sec_name, _PAD))
  500. {
  501. styp_flags = STYP_PAD;
  502. }
  503. else if (!strcmp (sec_name, _LOADER))
  504. {
  505. styp_flags = STYP_LOADER;
  506. }
  507. else if (!strcmp (sec_name, _EXCEPT))
  508. {
  509. styp_flags = STYP_EXCEPT;
  510. }
  511. else if (!strcmp (sec_name, _TYPCHK))
  512. {
  513. styp_flags = STYP_TYPCHK;
  514. }
  515. else if (sec_flags & SEC_DEBUGGING)
  516. {
  517. int i;
  518. for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
  519. if (!strcmp (sec_name, xcoff_dwsect_names[i].xcoff_name))
  520. {
  521. styp_flags = STYP_DWARF | xcoff_dwsect_names[i].flag;
  522. break;
  523. }
  524. }
  525. #endif
  526. /* Try and figure out what it should be */
  527. else if (sec_flags & SEC_CODE)
  528. {
  529. styp_flags = STYP_TEXT;
  530. }
  531. else if (sec_flags & SEC_DATA)
  532. {
  533. styp_flags = STYP_DATA;
  534. }
  535. else if (sec_flags & SEC_READONLY)
  536. {
  537. #ifdef STYP_LIT /* 29k readonly text/data section */
  538. styp_flags = STYP_LIT;
  539. #else
  540. styp_flags = STYP_TEXT;
  541. #endif /* STYP_LIT */
  542. }
  543. else if (sec_flags & SEC_LOAD)
  544. {
  545. styp_flags = STYP_TEXT;
  546. }
  547. else if (sec_flags & SEC_ALLOC)
  548. {
  549. styp_flags = STYP_BSS;
  550. }
  551. #ifdef STYP_CLINK
  552. if (sec_flags & SEC_TIC54X_CLINK)
  553. styp_flags |= STYP_CLINK;
  554. #endif
  555. #ifdef STYP_BLOCK
  556. if (sec_flags & SEC_TIC54X_BLOCK)
  557. styp_flags |= STYP_BLOCK;
  558. #endif
  559. #ifdef STYP_NOLOAD
  560. if ((sec_flags & (SEC_NEVER_LOAD | SEC_COFF_SHARED_LIBRARY)) != 0)
  561. styp_flags |= STYP_NOLOAD;
  562. #endif
  563. return styp_flags;
  564. }
  565. #else /* COFF_WITH_PE */
  566. /* The PE version; see above for the general comments. The non-PE
  567. case seems to be more guessing, and breaks PE format; specifically,
  568. .rdata is readonly, but it sure ain't text. Really, all this
  569. should be set up properly in gas (or whatever assembler is in use),
  570. and honor whatever objcopy/strip, etc. sent us as input. */
  571. static long
  572. sec_to_styp_flags (const char *sec_name, flagword sec_flags)
  573. {
  574. long styp_flags = 0;
  575. bool is_dbg = false;
  576. if (startswith (sec_name, DOT_DEBUG)
  577. || startswith (sec_name, DOT_ZDEBUG)
  578. #ifdef COFF_LONG_SECTION_NAMES
  579. || startswith (sec_name, GNU_LINKONCE_WI)
  580. || startswith (sec_name, GNU_LINKONCE_WT)
  581. #endif
  582. || startswith (sec_name, ".stab"))
  583. is_dbg = true;
  584. /* caution: there are at least three groups of symbols that have
  585. very similar bits and meanings: IMAGE_SCN*, SEC_*, and STYP_*.
  586. SEC_* are the BFD internal flags, used for generic BFD
  587. information. STYP_* are the COFF section flags which appear in
  588. COFF files. IMAGE_SCN_* are the PE section flags which appear in
  589. PE files. The STYP_* flags and the IMAGE_SCN_* flags overlap,
  590. but there are more IMAGE_SCN_* flags. */
  591. /* FIXME: There is no gas syntax to specify the debug section flag. */
  592. if (is_dbg)
  593. {
  594. sec_flags &= (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
  595. | SEC_LINK_DUPLICATES_SAME_CONTENTS
  596. | SEC_LINK_DUPLICATES_SAME_SIZE);
  597. sec_flags |= SEC_DEBUGGING | SEC_READONLY;
  598. }
  599. /* skip LOAD */
  600. /* READONLY later */
  601. /* skip RELOC */
  602. if ((sec_flags & SEC_CODE) != 0)
  603. styp_flags |= IMAGE_SCN_CNT_CODE;
  604. if ((sec_flags & (SEC_DATA | SEC_DEBUGGING)) != 0)
  605. styp_flags |= IMAGE_SCN_CNT_INITIALIZED_DATA;
  606. if ((sec_flags & SEC_ALLOC) != 0 && (sec_flags & SEC_LOAD) == 0)
  607. styp_flags |= IMAGE_SCN_CNT_UNINITIALIZED_DATA; /* ==STYP_BSS */
  608. /* skip ROM */
  609. /* skip constRUCTOR */
  610. /* skip CONTENTS */
  611. #ifndef COFF_IMAGE_WITH_PE
  612. /* I don't think any of the IMAGE_SCN_LNK_* flags set below should be set
  613. when the output is PE. Only object files should have them, for the linker
  614. to consume. */
  615. if ((sec_flags & SEC_IS_COMMON) != 0)
  616. styp_flags |= IMAGE_SCN_LNK_COMDAT;
  617. #endif
  618. if ((sec_flags & SEC_DEBUGGING) != 0)
  619. styp_flags |= IMAGE_SCN_MEM_DISCARDABLE;
  620. if ((sec_flags & (SEC_EXCLUDE | SEC_NEVER_LOAD)) != 0 && !is_dbg)
  621. #ifdef COFF_IMAGE_WITH_PE
  622. styp_flags |= IMAGE_SCN_MEM_DISCARDABLE;
  623. #else
  624. styp_flags |= IMAGE_SCN_LNK_REMOVE;
  625. #endif
  626. /* skip IN_MEMORY */
  627. /* skip SORT */
  628. #ifndef COFF_IMAGE_WITH_PE
  629. if (sec_flags & SEC_LINK_ONCE)
  630. styp_flags |= IMAGE_SCN_LNK_COMDAT;
  631. if ((sec_flags
  632. & (SEC_LINK_DUPLICATES_DISCARD | SEC_LINK_DUPLICATES_SAME_CONTENTS
  633. | SEC_LINK_DUPLICATES_SAME_SIZE)) != 0)
  634. styp_flags |= IMAGE_SCN_LNK_COMDAT;
  635. #endif
  636. /* skip LINKER_CREATED */
  637. if ((sec_flags & SEC_COFF_NOREAD) == 0)
  638. styp_flags |= IMAGE_SCN_MEM_READ; /* Invert NOREAD for read. */
  639. if ((sec_flags & SEC_READONLY) == 0)
  640. styp_flags |= IMAGE_SCN_MEM_WRITE; /* Invert READONLY for write. */
  641. if (sec_flags & SEC_CODE)
  642. styp_flags |= IMAGE_SCN_MEM_EXECUTE; /* CODE->EXECUTE. */
  643. if (sec_flags & SEC_COFF_SHARED)
  644. styp_flags |= IMAGE_SCN_MEM_SHARED; /* Shared remains meaningful. */
  645. return styp_flags;
  646. }
  647. #endif /* COFF_WITH_PE */
  648. /* Return a word with SEC_* flags set to represent the incoming STYP_*
  649. flags (from scnhdr.s_flags). The inverse of this function is
  650. sec_to_styp_flags(). NOTE: If you add to/change this routine, you
  651. should probably mirror the changes in sec_to_styp_flags(). */
  652. #ifndef COFF_WITH_PE
  653. static bool
  654. styp_to_sec_flags (bfd *abfd,
  655. void * hdr,
  656. const char *name,
  657. asection *section ATTRIBUTE_UNUSED,
  658. flagword *flags_ptr)
  659. {
  660. struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
  661. unsigned long styp_flags = internal_s->s_flags;
  662. flagword sec_flags = 0;
  663. #ifdef STYP_BLOCK
  664. if (styp_flags & STYP_BLOCK)
  665. sec_flags |= SEC_TIC54X_BLOCK;
  666. #endif
  667. #ifdef STYP_CLINK
  668. if (styp_flags & STYP_CLINK)
  669. sec_flags |= SEC_TIC54X_CLINK;
  670. #endif
  671. #ifdef STYP_NOLOAD
  672. if (styp_flags & STYP_NOLOAD)
  673. sec_flags |= SEC_NEVER_LOAD;
  674. #endif /* STYP_NOLOAD */
  675. /* For 386 COFF, at least, an unloadable text or data section is
  676. actually a shared library section. */
  677. if (styp_flags & STYP_TEXT)
  678. {
  679. if (sec_flags & SEC_NEVER_LOAD)
  680. sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
  681. else
  682. sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
  683. }
  684. else if (styp_flags & STYP_DATA)
  685. {
  686. if (sec_flags & SEC_NEVER_LOAD)
  687. sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
  688. else
  689. sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
  690. }
  691. else if (styp_flags & STYP_BSS)
  692. {
  693. #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
  694. if (sec_flags & SEC_NEVER_LOAD)
  695. sec_flags |= SEC_ALLOC | SEC_COFF_SHARED_LIBRARY;
  696. else
  697. #endif
  698. sec_flags |= SEC_ALLOC;
  699. }
  700. else if (styp_flags & STYP_INFO)
  701. {
  702. /* We mark these as SEC_DEBUGGING, but only if COFF_PAGE_SIZE is
  703. defined. coff_compute_section_file_positions uses
  704. COFF_PAGE_SIZE to ensure that the low order bits of the
  705. section VMA and the file offset match. If we don't know
  706. COFF_PAGE_SIZE, we can't ensure the correct correspondence,
  707. and demand page loading of the file will fail. */
  708. #if defined (COFF_PAGE_SIZE) && !defined (COFF_ALIGN_IN_S_FLAGS)
  709. sec_flags |= SEC_DEBUGGING;
  710. #endif
  711. }
  712. else if (styp_flags & STYP_PAD)
  713. sec_flags = 0;
  714. #ifdef RS6000COFF_C
  715. else if (styp_flags & STYP_TDATA)
  716. {
  717. if (sec_flags & SEC_NEVER_LOAD)
  718. sec_flags |= SEC_DATA | SEC_THREAD_LOCAL | SEC_COFF_SHARED_LIBRARY;
  719. else
  720. sec_flags |= SEC_DATA | SEC_THREAD_LOCAL | SEC_LOAD | SEC_ALLOC;
  721. }
  722. else if (styp_flags & STYP_TBSS)
  723. {
  724. #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
  725. if (sec_flags & SEC_NEVER_LOAD)
  726. sec_flags |= SEC_ALLOC | SEC_THREAD_LOCAL | SEC_COFF_SHARED_LIBRARY;
  727. else
  728. #endif
  729. sec_flags |= SEC_ALLOC | SEC_THREAD_LOCAL;
  730. }
  731. else if (styp_flags & STYP_EXCEPT)
  732. sec_flags |= SEC_LOAD;
  733. else if (styp_flags & STYP_LOADER)
  734. sec_flags |= SEC_LOAD;
  735. else if (styp_flags & STYP_TYPCHK)
  736. sec_flags |= SEC_LOAD;
  737. else if (styp_flags & STYP_DWARF)
  738. sec_flags |= SEC_DEBUGGING;
  739. #endif
  740. else if (strcmp (name, _TEXT) == 0)
  741. {
  742. if (sec_flags & SEC_NEVER_LOAD)
  743. sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
  744. else
  745. sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
  746. }
  747. else if (strcmp (name, _DATA) == 0)
  748. {
  749. if (sec_flags & SEC_NEVER_LOAD)
  750. sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
  751. else
  752. sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
  753. }
  754. else if (strcmp (name, _BSS) == 0)
  755. {
  756. #ifdef BSS_NOLOAD_IS_SHARED_LIBRARY
  757. if (sec_flags & SEC_NEVER_LOAD)
  758. sec_flags |= SEC_ALLOC | SEC_COFF_SHARED_LIBRARY;
  759. else
  760. #endif
  761. sec_flags |= SEC_ALLOC;
  762. }
  763. else if (startswith (name, DOT_DEBUG)
  764. || startswith (name, DOT_ZDEBUG)
  765. #ifdef _COMMENT
  766. || strcmp (name, _COMMENT) == 0
  767. #endif
  768. #ifdef COFF_LONG_SECTION_NAMES
  769. || startswith (name, GNU_LINKONCE_WI)
  770. || startswith (name, GNU_LINKONCE_WT)
  771. #endif
  772. || startswith (name, ".stab"))
  773. {
  774. #ifdef COFF_PAGE_SIZE
  775. sec_flags |= SEC_DEBUGGING;
  776. #endif
  777. }
  778. #ifdef _LIB
  779. else if (strcmp (name, _LIB) == 0)
  780. ;
  781. #endif
  782. #ifdef _LIT
  783. else if (strcmp (name, _LIT) == 0)
  784. sec_flags = SEC_LOAD | SEC_ALLOC | SEC_READONLY;
  785. #endif
  786. else
  787. sec_flags |= SEC_ALLOC | SEC_LOAD;
  788. #ifdef STYP_LIT /* A29k readonly text/data section type. */
  789. if ((styp_flags & STYP_LIT) == STYP_LIT)
  790. sec_flags = (SEC_LOAD | SEC_ALLOC | SEC_READONLY);
  791. #endif /* STYP_LIT */
  792. #ifdef STYP_OTHER_LOAD /* Other loaded sections. */
  793. if (styp_flags & STYP_OTHER_LOAD)
  794. sec_flags = (SEC_LOAD | SEC_ALLOC);
  795. #endif /* STYP_SDATA */
  796. if ((bfd_applicable_section_flags (abfd) & SEC_SMALL_DATA) != 0
  797. && (startswith (name, ".sbss")
  798. || startswith (name, ".sdata")))
  799. sec_flags |= SEC_SMALL_DATA;
  800. #if defined (COFF_LONG_SECTION_NAMES) && defined (COFF_SUPPORT_GNU_LINKONCE)
  801. /* As a GNU extension, if the name begins with .gnu.linkonce, we
  802. only link a single copy of the section. This is used to support
  803. g++. g++ will emit each template expansion in its own section.
  804. The symbols will be defined as weak, so that multiple definitions
  805. are permitted. The GNU linker extension is to actually discard
  806. all but one of the sections. */
  807. if (startswith (name, ".gnu.linkonce"))
  808. sec_flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
  809. #endif
  810. if (flags_ptr == NULL)
  811. return false;
  812. * flags_ptr = sec_flags;
  813. return true;
  814. }
  815. #else /* COFF_WITH_PE */
  816. static flagword
  817. handle_COMDAT (bfd * abfd,
  818. flagword sec_flags,
  819. void * hdr,
  820. const char *name,
  821. asection *section)
  822. {
  823. struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
  824. bfd_byte *esymstart, *esym, *esymend;
  825. int seen_state = 0;
  826. char *target_name = NULL;
  827. sec_flags |= SEC_LINK_ONCE;
  828. /* Unfortunately, the PE format stores essential information in
  829. the symbol table, of all places. We need to extract that
  830. information now, so that objdump and the linker will know how
  831. to handle the section without worrying about the symbols. We
  832. can't call slurp_symtab, because the linker doesn't want the
  833. swapped symbols. */
  834. /* COMDAT sections are special. The first symbol is the section
  835. symbol, which tells what kind of COMDAT section it is. The
  836. second symbol is the "comdat symbol" - the one with the
  837. unique name. GNU uses the section symbol for the unique
  838. name; MS uses ".text" for every comdat section. Sigh. - DJ */
  839. /* This is not mirrored in sec_to_styp_flags(), but there
  840. doesn't seem to be a need to, either, and it would at best be
  841. rather messy. */
  842. if (! _bfd_coff_get_external_symbols (abfd))
  843. return sec_flags;
  844. esymstart = esym = (bfd_byte *) obj_coff_external_syms (abfd);
  845. esymend = esym + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
  846. while (esym < esymend)
  847. {
  848. struct internal_syment isym;
  849. char buf[SYMNMLEN + 1];
  850. const char *symname;
  851. bfd_coff_swap_sym_in (abfd, esym, & isym);
  852. BFD_ASSERT (sizeof (internal_s->s_name) <= SYMNMLEN);
  853. if (isym.n_scnum == section->target_index)
  854. {
  855. /* According to the MSVC documentation, the first
  856. TWO entries with the section # are both of
  857. interest to us. The first one is the "section
  858. symbol" (section name). The second is the comdat
  859. symbol name. Here, we've found the first
  860. qualifying entry; we distinguish it from the
  861. second with a state flag.
  862. In the case of gas-generated (at least until that
  863. is fixed) .o files, it isn't necessarily the
  864. second one. It may be some other later symbol.
  865. Since gas also doesn't follow MS conventions and
  866. emits the section similar to .text$<name>, where
  867. <something> is the name we're looking for, we
  868. distinguish the two as follows:
  869. If the section name is simply a section name (no
  870. $) we presume it's MS-generated, and look at
  871. precisely the second symbol for the comdat name.
  872. If the section name has a $, we assume it's
  873. gas-generated, and look for <something> (whatever
  874. follows the $) as the comdat symbol. */
  875. /* All 3 branches use this. */
  876. symname = _bfd_coff_internal_syment_name (abfd, &isym, buf);
  877. /* PR 17512 file: 078-11867-0.004 */
  878. if (symname == NULL)
  879. {
  880. _bfd_error_handler (_("%pB: unable to load COMDAT section name"),
  881. abfd);
  882. break;
  883. }
  884. switch (seen_state)
  885. {
  886. case 0:
  887. {
  888. /* The first time we've seen the symbol. */
  889. union internal_auxent aux;
  890. /* If it isn't the stuff we're expecting, die;
  891. The MS documentation is vague, but it
  892. appears that the second entry serves BOTH
  893. as the comdat symbol and the defining
  894. symbol record (either C_STAT or C_EXT,
  895. possibly with an aux entry with debug
  896. information if it's a function.) It
  897. appears the only way to find the second one
  898. is to count. (On Intel, they appear to be
  899. adjacent, but on Alpha, they have been
  900. found separated.)
  901. Here, we think we've found the first one,
  902. but there's some checking we can do to be
  903. sure. */
  904. if (! ((isym.n_sclass == C_STAT
  905. || isym.n_sclass == C_EXT)
  906. && BTYPE (isym.n_type) == T_NULL
  907. && isym.n_value == 0))
  908. {
  909. /* Malformed input files can trigger this test.
  910. cf PR 21781. */
  911. _bfd_error_handler (_("%pB: error: unexpected symbol '%s' in COMDAT section"),
  912. abfd, symname);
  913. goto breakloop;
  914. }
  915. /* FIXME LATER: MSVC generates section names
  916. like .text for comdats. Gas generates
  917. names like .text$foo__Fv (in the case of a
  918. function). See comment above for more. */
  919. if (isym.n_sclass == C_STAT && strcmp (name, symname) != 0)
  920. /* xgettext:c-format */
  921. _bfd_error_handler (_("%pB: warning: COMDAT symbol '%s'"
  922. " does not match section name '%s'"),
  923. abfd, symname, name);
  924. seen_state = 1;
  925. /* PR 17512: file: e2cfe54f. */
  926. if (esym + bfd_coff_symesz (abfd) >= esymend)
  927. {
  928. /* xgettext:c-format */
  929. _bfd_error_handler (_("%pB: warning: no symbol for"
  930. " section '%s' found"),
  931. abfd, symname);
  932. break;
  933. }
  934. /* This is the section symbol. */
  935. bfd_coff_swap_aux_in (abfd, (esym + bfd_coff_symesz (abfd)),
  936. isym.n_type, isym.n_sclass,
  937. 0, isym.n_numaux, & aux);
  938. target_name = strchr (name, '$');
  939. if (target_name != NULL)
  940. {
  941. /* Gas mode. */
  942. seen_state = 2;
  943. /* Skip the `$'. */
  944. target_name += 1;
  945. }
  946. /* FIXME: Microsoft uses NODUPLICATES and
  947. ASSOCIATIVE, but gnu uses ANY and
  948. SAME_SIZE. Unfortunately, gnu doesn't do
  949. the comdat symbols right. So, until we can
  950. fix it to do the right thing, we are
  951. temporarily disabling comdats for the MS
  952. types (they're used in DLLs and C++, but we
  953. don't support *their* C++ libraries anyway
  954. - DJ. */
  955. /* Cygwin does not follow the MS style, and
  956. uses ANY and SAME_SIZE where NODUPLICATES
  957. and ASSOCIATIVE should be used. For
  958. Interix, we just do the right thing up
  959. front. */
  960. switch (aux.x_scn.x_comdat)
  961. {
  962. case IMAGE_COMDAT_SELECT_NODUPLICATES:
  963. #ifdef STRICT_PE_FORMAT
  964. sec_flags |= SEC_LINK_DUPLICATES_ONE_ONLY;
  965. #else
  966. sec_flags &= ~SEC_LINK_ONCE;
  967. #endif
  968. break;
  969. case IMAGE_COMDAT_SELECT_ANY:
  970. sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
  971. break;
  972. case IMAGE_COMDAT_SELECT_SAME_SIZE:
  973. sec_flags |= SEC_LINK_DUPLICATES_SAME_SIZE;
  974. break;
  975. case IMAGE_COMDAT_SELECT_EXACT_MATCH:
  976. /* Not yet fully implemented ??? */
  977. sec_flags |= SEC_LINK_DUPLICATES_SAME_CONTENTS;
  978. break;
  979. /* debug$S gets this case; other
  980. implications ??? */
  981. /* There may be no symbol... we'll search
  982. the whole table... Is this the right
  983. place to play this game? Or should we do
  984. it when reading it in. */
  985. case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
  986. #ifdef STRICT_PE_FORMAT
  987. /* FIXME: This is not currently implemented. */
  988. sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
  989. #else
  990. sec_flags &= ~SEC_LINK_ONCE;
  991. #endif
  992. break;
  993. default: /* 0 means "no symbol" */
  994. /* debug$F gets this case; other
  995. implications ??? */
  996. sec_flags |= SEC_LINK_DUPLICATES_DISCARD;
  997. break;
  998. }
  999. }
  1000. break;
  1001. case 2:
  1002. /* Gas mode: the first matching on partial name. */
  1003. #ifndef TARGET_UNDERSCORE
  1004. #define TARGET_UNDERSCORE 0
  1005. #endif
  1006. /* Is this the name we're looking for ? */
  1007. if (strcmp (target_name,
  1008. symname + (TARGET_UNDERSCORE ? 1 : 0)) != 0)
  1009. {
  1010. /* Not the name we're looking for */
  1011. esym += (isym.n_numaux + 1) * bfd_coff_symesz (abfd);
  1012. continue;
  1013. }
  1014. /* Fall through. */
  1015. case 1:
  1016. /* MSVC mode: the lexically second symbol (or
  1017. drop through from the above). */
  1018. {
  1019. char *newname;
  1020. size_t amt;
  1021. /* This must the second symbol with the
  1022. section #. It is the actual symbol name.
  1023. Intel puts the two adjacent, but Alpha (at
  1024. least) spreads them out. */
  1025. amt = sizeof (struct coff_comdat_info);
  1026. coff_section_data (abfd, section)->comdat
  1027. = (struct coff_comdat_info *) bfd_alloc (abfd, amt);
  1028. if (coff_section_data (abfd, section)->comdat == NULL)
  1029. abort ();
  1030. coff_section_data (abfd, section)->comdat->symbol =
  1031. (esym - esymstart) / bfd_coff_symesz (abfd);
  1032. amt = strlen (symname) + 1;
  1033. newname = (char *) bfd_alloc (abfd, amt);
  1034. if (newname == NULL)
  1035. abort ();
  1036. strcpy (newname, symname);
  1037. coff_section_data (abfd, section)->comdat->name
  1038. = newname;
  1039. }
  1040. goto breakloop;
  1041. }
  1042. }
  1043. esym += (isym.n_numaux + 1) * bfd_coff_symesz (abfd);
  1044. }
  1045. breakloop:
  1046. return sec_flags;
  1047. }
  1048. /* The PE version; see above for the general comments.
  1049. Since to set the SEC_LINK_ONCE and associated flags, we have to
  1050. look at the symbol table anyway, we return the symbol table index
  1051. of the symbol being used as the COMDAT symbol. This is admittedly
  1052. ugly, but there's really nowhere else that we have access to the
  1053. required information. FIXME: Is the COMDAT symbol index used for
  1054. any purpose other than objdump? */
  1055. static bool
  1056. styp_to_sec_flags (bfd *abfd,
  1057. void * hdr,
  1058. const char *name,
  1059. asection *section,
  1060. flagword *flags_ptr)
  1061. {
  1062. struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
  1063. unsigned long styp_flags = internal_s->s_flags;
  1064. flagword sec_flags;
  1065. bool result = true;
  1066. bool is_dbg = false;
  1067. if (startswith (name, DOT_DEBUG)
  1068. || startswith (name, DOT_ZDEBUG)
  1069. #ifdef COFF_LONG_SECTION_NAMES
  1070. || startswith (name, GNU_LINKONCE_WI)
  1071. || startswith (name, GNU_LINKONCE_WT)
  1072. /* FIXME: These definitions ought to be in a header file. */
  1073. #define GNU_DEBUGLINK ".gnu_debuglink"
  1074. #define GNU_DEBUGALTLINK ".gnu_debugaltlink"
  1075. || startswith (name, GNU_DEBUGLINK)
  1076. || startswith (name, GNU_DEBUGALTLINK)
  1077. #endif
  1078. || startswith (name, ".stab"))
  1079. is_dbg = true;
  1080. /* Assume read only unless IMAGE_SCN_MEM_WRITE is specified. */
  1081. sec_flags = SEC_READONLY;
  1082. /* If section disallows read, then set the NOREAD flag. */
  1083. if ((styp_flags & IMAGE_SCN_MEM_READ) == 0)
  1084. sec_flags |= SEC_COFF_NOREAD;
  1085. /* Process each flag bit in styp_flags in turn. */
  1086. while (styp_flags)
  1087. {
  1088. unsigned long flag = styp_flags & - styp_flags;
  1089. char * unhandled = NULL;
  1090. styp_flags &= ~ flag;
  1091. /* We infer from the distinct read/write/execute bits the settings
  1092. of some of the bfd flags; the actual values, should we need them,
  1093. are also in pei_section_data (abfd, section)->pe_flags. */
  1094. switch (flag)
  1095. {
  1096. case STYP_DSECT:
  1097. unhandled = "STYP_DSECT";
  1098. break;
  1099. case STYP_GROUP:
  1100. unhandled = "STYP_GROUP";
  1101. break;
  1102. case STYP_COPY:
  1103. unhandled = "STYP_COPY";
  1104. break;
  1105. case STYP_OVER:
  1106. unhandled = "STYP_OVER";
  1107. break;
  1108. #ifdef SEC_NEVER_LOAD
  1109. case STYP_NOLOAD:
  1110. sec_flags |= SEC_NEVER_LOAD;
  1111. break;
  1112. #endif
  1113. case IMAGE_SCN_MEM_READ:
  1114. sec_flags &= ~SEC_COFF_NOREAD;
  1115. break;
  1116. case IMAGE_SCN_TYPE_NO_PAD:
  1117. /* Skip. */
  1118. break;
  1119. case IMAGE_SCN_LNK_OTHER:
  1120. unhandled = "IMAGE_SCN_LNK_OTHER";
  1121. break;
  1122. case IMAGE_SCN_MEM_NOT_CACHED:
  1123. unhandled = "IMAGE_SCN_MEM_NOT_CACHED";
  1124. break;
  1125. case IMAGE_SCN_MEM_NOT_PAGED:
  1126. /* Generate a warning message rather using the 'unhandled'
  1127. variable as this will allow some .sys files generate by
  1128. other toolchains to be processed. See bugzilla issue 196. */
  1129. /* xgettext:c-format */
  1130. _bfd_error_handler (_("%pB: warning: ignoring section flag"
  1131. " %s in section %s"),
  1132. abfd, "IMAGE_SCN_MEM_NOT_PAGED", name);
  1133. break;
  1134. case IMAGE_SCN_MEM_EXECUTE:
  1135. sec_flags |= SEC_CODE;
  1136. break;
  1137. case IMAGE_SCN_MEM_WRITE:
  1138. sec_flags &= ~ SEC_READONLY;
  1139. break;
  1140. case IMAGE_SCN_MEM_DISCARDABLE:
  1141. /* The MS PE spec says that debug sections are DISCARDABLE,
  1142. but the presence of a DISCARDABLE flag does not necessarily
  1143. mean that a given section contains debug information. Thus
  1144. we only set the SEC_DEBUGGING flag on sections that we
  1145. recognise as containing debug information. */
  1146. if (is_dbg
  1147. #ifdef _COMMENT
  1148. || strcmp (name, _COMMENT) == 0
  1149. #endif
  1150. )
  1151. {
  1152. sec_flags |= SEC_DEBUGGING | SEC_READONLY;
  1153. }
  1154. break;
  1155. case IMAGE_SCN_MEM_SHARED:
  1156. sec_flags |= SEC_COFF_SHARED;
  1157. break;
  1158. case IMAGE_SCN_LNK_REMOVE:
  1159. if (!is_dbg)
  1160. sec_flags |= SEC_EXCLUDE;
  1161. break;
  1162. case IMAGE_SCN_CNT_CODE:
  1163. sec_flags |= SEC_CODE | SEC_ALLOC | SEC_LOAD;
  1164. break;
  1165. case IMAGE_SCN_CNT_INITIALIZED_DATA:
  1166. if (is_dbg)
  1167. sec_flags |= SEC_DEBUGGING;
  1168. else
  1169. sec_flags |= SEC_DATA | SEC_ALLOC | SEC_LOAD;
  1170. break;
  1171. case IMAGE_SCN_CNT_UNINITIALIZED_DATA:
  1172. sec_flags |= SEC_ALLOC;
  1173. break;
  1174. case IMAGE_SCN_LNK_INFO:
  1175. /* We mark these as SEC_DEBUGGING, but only if COFF_PAGE_SIZE is
  1176. defined. coff_compute_section_file_positions uses
  1177. COFF_PAGE_SIZE to ensure that the low order bits of the
  1178. section VMA and the file offset match. If we don't know
  1179. COFF_PAGE_SIZE, we can't ensure the correct correspondence,
  1180. and demand page loading of the file will fail. */
  1181. #ifdef COFF_PAGE_SIZE
  1182. sec_flags |= SEC_DEBUGGING;
  1183. #endif
  1184. break;
  1185. case IMAGE_SCN_LNK_COMDAT:
  1186. /* COMDAT gets very special treatment. */
  1187. sec_flags = handle_COMDAT (abfd, sec_flags, hdr, name, section);
  1188. break;
  1189. default:
  1190. /* Silently ignore for now. */
  1191. break;
  1192. }
  1193. /* If the section flag was not handled, report it here. */
  1194. if (unhandled != NULL)
  1195. {
  1196. _bfd_error_handler
  1197. /* xgettext:c-format */
  1198. (_("%pB (%s): section flag %s (%#lx) ignored"),
  1199. abfd, name, unhandled, flag);
  1200. result = false;
  1201. }
  1202. }
  1203. if ((bfd_applicable_section_flags (abfd) & SEC_SMALL_DATA) != 0
  1204. && (startswith (name, ".sbss")
  1205. || startswith (name, ".sdata")))
  1206. sec_flags |= SEC_SMALL_DATA;
  1207. #if defined (COFF_LONG_SECTION_NAMES) && defined (COFF_SUPPORT_GNU_LINKONCE)
  1208. /* As a GNU extension, if the name begins with .gnu.linkonce, we
  1209. only link a single copy of the section. This is used to support
  1210. g++. g++ will emit each template expansion in its own section.
  1211. The symbols will be defined as weak, so that multiple definitions
  1212. are permitted. The GNU linker extension is to actually discard
  1213. all but one of the sections. */
  1214. if (startswith (name, ".gnu.linkonce"))
  1215. sec_flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
  1216. #endif
  1217. if (flags_ptr)
  1218. * flags_ptr = sec_flags;
  1219. return result;
  1220. }
  1221. #endif /* COFF_WITH_PE */
  1222. #define get_index(symbol) ((symbol)->udata.i)
  1223. /*
  1224. INTERNAL_DEFINITION
  1225. bfd_coff_backend_data
  1226. CODE_FRAGMENT
  1227. .{* COFF symbol classifications. *}
  1228. .
  1229. .enum coff_symbol_classification
  1230. .{
  1231. . {* Global symbol. *}
  1232. . COFF_SYMBOL_GLOBAL,
  1233. . {* Common symbol. *}
  1234. . COFF_SYMBOL_COMMON,
  1235. . {* Undefined symbol. *}
  1236. . COFF_SYMBOL_UNDEFINED,
  1237. . {* Local symbol. *}
  1238. . COFF_SYMBOL_LOCAL,
  1239. . {* PE section symbol. *}
  1240. . COFF_SYMBOL_PE_SECTION
  1241. .};
  1242. .
  1243. .typedef asection * (*coff_gc_mark_hook_fn)
  1244. . (asection *, struct bfd_link_info *, struct internal_reloc *,
  1245. . struct coff_link_hash_entry *, struct internal_syment *);
  1246. .
  1247. Special entry points for gdb to swap in coff symbol table parts:
  1248. .typedef struct
  1249. .{
  1250. . void (*_bfd_coff_swap_aux_in)
  1251. . (bfd *, void *, int, int, int, int, void *);
  1252. .
  1253. . void (*_bfd_coff_swap_sym_in)
  1254. . (bfd *, void *, void *);
  1255. .
  1256. . void (*_bfd_coff_swap_lineno_in)
  1257. . (bfd *, void *, void *);
  1258. .
  1259. . unsigned int (*_bfd_coff_swap_aux_out)
  1260. . (bfd *, void *, int, int, int, int, void *);
  1261. .
  1262. . unsigned int (*_bfd_coff_swap_sym_out)
  1263. . (bfd *, void *, void *);
  1264. .
  1265. . unsigned int (*_bfd_coff_swap_lineno_out)
  1266. . (bfd *, void *, void *);
  1267. .
  1268. . unsigned int (*_bfd_coff_swap_reloc_out)
  1269. . (bfd *, void *, void *);
  1270. .
  1271. . unsigned int (*_bfd_coff_swap_filehdr_out)
  1272. . (bfd *, void *, void *);
  1273. .
  1274. . unsigned int (*_bfd_coff_swap_aouthdr_out)
  1275. . (bfd *, void *, void *);
  1276. .
  1277. . unsigned int (*_bfd_coff_swap_scnhdr_out)
  1278. . (bfd *, void *, void *);
  1279. .
  1280. . unsigned int _bfd_filhsz;
  1281. . unsigned int _bfd_aoutsz;
  1282. . unsigned int _bfd_scnhsz;
  1283. . unsigned int _bfd_symesz;
  1284. . unsigned int _bfd_auxesz;
  1285. . unsigned int _bfd_relsz;
  1286. . unsigned int _bfd_linesz;
  1287. . unsigned int _bfd_filnmlen;
  1288. . bool _bfd_coff_long_filenames;
  1289. .
  1290. . bool _bfd_coff_long_section_names;
  1291. . bool (*_bfd_coff_set_long_section_names)
  1292. . (bfd *, int);
  1293. .
  1294. . unsigned int _bfd_coff_default_section_alignment_power;
  1295. . bool _bfd_coff_force_symnames_in_strings;
  1296. . unsigned int _bfd_coff_debug_string_prefix_length;
  1297. . unsigned int _bfd_coff_max_nscns;
  1298. .
  1299. . void (*_bfd_coff_swap_filehdr_in)
  1300. . (bfd *, void *, void *);
  1301. .
  1302. . void (*_bfd_coff_swap_aouthdr_in)
  1303. . (bfd *, void *, void *);
  1304. .
  1305. . void (*_bfd_coff_swap_scnhdr_in)
  1306. . (bfd *, void *, void *);
  1307. .
  1308. . void (*_bfd_coff_swap_reloc_in)
  1309. . (bfd *abfd, void *, void *);
  1310. .
  1311. . bool (*_bfd_coff_bad_format_hook)
  1312. . (bfd *, void *);
  1313. .
  1314. . bool (*_bfd_coff_set_arch_mach_hook)
  1315. . (bfd *, void *);
  1316. .
  1317. . void * (*_bfd_coff_mkobject_hook)
  1318. . (bfd *, void *, void *);
  1319. .
  1320. . bool (*_bfd_styp_to_sec_flags_hook)
  1321. . (bfd *, void *, const char *, asection *, flagword *);
  1322. .
  1323. . void (*_bfd_set_alignment_hook)
  1324. . (bfd *, asection *, void *);
  1325. .
  1326. . bool (*_bfd_coff_slurp_symbol_table)
  1327. . (bfd *);
  1328. .
  1329. . bool (*_bfd_coff_symname_in_debug)
  1330. . (bfd *, struct internal_syment *);
  1331. .
  1332. . bool (*_bfd_coff_pointerize_aux_hook)
  1333. . (bfd *, combined_entry_type *, combined_entry_type *,
  1334. . unsigned int, combined_entry_type *);
  1335. .
  1336. . bool (*_bfd_coff_print_aux)
  1337. . (bfd *, FILE *, combined_entry_type *, combined_entry_type *,
  1338. . combined_entry_type *, unsigned int);
  1339. .
  1340. . void (*_bfd_coff_reloc16_extra_cases)
  1341. . (bfd *, struct bfd_link_info *, struct bfd_link_order *, arelent *,
  1342. . bfd_byte *, unsigned int *, unsigned int *);
  1343. .
  1344. . int (*_bfd_coff_reloc16_estimate)
  1345. . (bfd *, asection *, arelent *, unsigned int,
  1346. . struct bfd_link_info *);
  1347. .
  1348. . enum coff_symbol_classification (*_bfd_coff_classify_symbol)
  1349. . (bfd *, struct internal_syment *);
  1350. .
  1351. . bool (*_bfd_coff_compute_section_file_positions)
  1352. . (bfd *);
  1353. .
  1354. . bool (*_bfd_coff_start_final_link)
  1355. . (bfd *, struct bfd_link_info *);
  1356. .
  1357. . bool (*_bfd_coff_relocate_section)
  1358. . (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  1359. . struct internal_reloc *, struct internal_syment *, asection **);
  1360. .
  1361. . reloc_howto_type *(*_bfd_coff_rtype_to_howto)
  1362. . (bfd *, asection *, struct internal_reloc *,
  1363. . struct coff_link_hash_entry *, struct internal_syment *, bfd_vma *);
  1364. .
  1365. . bool (*_bfd_coff_adjust_symndx)
  1366. . (bfd *, struct bfd_link_info *, bfd *, asection *,
  1367. . struct internal_reloc *, bool *);
  1368. .
  1369. . bool (*_bfd_coff_link_add_one_symbol)
  1370. . (struct bfd_link_info *, bfd *, const char *, flagword,
  1371. . asection *, bfd_vma, const char *, bool, bool,
  1372. . struct bfd_link_hash_entry **);
  1373. .
  1374. . bool (*_bfd_coff_link_output_has_begun)
  1375. . (bfd *, struct coff_final_link_info *);
  1376. .
  1377. . bool (*_bfd_coff_final_link_postscript)
  1378. . (bfd *, struct coff_final_link_info *);
  1379. .
  1380. . bool (*_bfd_coff_print_pdata)
  1381. . (bfd *, void *);
  1382. .
  1383. .} bfd_coff_backend_data;
  1384. .
  1385. .#define coff_backend_info(abfd) \
  1386. . ((bfd_coff_backend_data *) (abfd)->xvec->backend_data)
  1387. .
  1388. .#define bfd_coff_swap_aux_in(a,e,t,c,ind,num,i) \
  1389. . ((coff_backend_info (a)->_bfd_coff_swap_aux_in) (a,e,t,c,ind,num,i))
  1390. .
  1391. .#define bfd_coff_swap_sym_in(a,e,i) \
  1392. . ((coff_backend_info (a)->_bfd_coff_swap_sym_in) (a,e,i))
  1393. .
  1394. .#define bfd_coff_swap_lineno_in(a,e,i) \
  1395. . ((coff_backend_info ( a)->_bfd_coff_swap_lineno_in) (a,e,i))
  1396. .
  1397. .#define bfd_coff_swap_reloc_out(abfd, i, o) \
  1398. . ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_out) (abfd, i, o))
  1399. .
  1400. .#define bfd_coff_swap_lineno_out(abfd, i, o) \
  1401. . ((coff_backend_info (abfd)->_bfd_coff_swap_lineno_out) (abfd, i, o))
  1402. .
  1403. .#define bfd_coff_swap_aux_out(a,i,t,c,ind,num,o) \
  1404. . ((coff_backend_info (a)->_bfd_coff_swap_aux_out) (a,i,t,c,ind,num,o))
  1405. .
  1406. .#define bfd_coff_swap_sym_out(abfd, i,o) \
  1407. . ((coff_backend_info (abfd)->_bfd_coff_swap_sym_out) (abfd, i, o))
  1408. .
  1409. .#define bfd_coff_swap_scnhdr_out(abfd, i,o) \
  1410. . ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_out) (abfd, i, o))
  1411. .
  1412. .#define bfd_coff_swap_filehdr_out(abfd, i,o) \
  1413. . ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_out) (abfd, i, o))
  1414. .
  1415. .#define bfd_coff_swap_aouthdr_out(abfd, i,o) \
  1416. . ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_out) (abfd, i, o))
  1417. .
  1418. .#define bfd_coff_filhsz(abfd) (coff_backend_info (abfd)->_bfd_filhsz)
  1419. .#define bfd_coff_aoutsz(abfd) (coff_backend_info (abfd)->_bfd_aoutsz)
  1420. .#define bfd_coff_scnhsz(abfd) (coff_backend_info (abfd)->_bfd_scnhsz)
  1421. .#define bfd_coff_symesz(abfd) (coff_backend_info (abfd)->_bfd_symesz)
  1422. .#define bfd_coff_auxesz(abfd) (coff_backend_info (abfd)->_bfd_auxesz)
  1423. .#define bfd_coff_relsz(abfd) (coff_backend_info (abfd)->_bfd_relsz)
  1424. .#define bfd_coff_linesz(abfd) (coff_backend_info (abfd)->_bfd_linesz)
  1425. .#define bfd_coff_filnmlen(abfd) (coff_backend_info (abfd)->_bfd_filnmlen)
  1426. .#define bfd_coff_long_filenames(abfd) \
  1427. . (coff_backend_info (abfd)->_bfd_coff_long_filenames)
  1428. .#define bfd_coff_long_section_names(abfd) \
  1429. . (coff_backend_info (abfd)->_bfd_coff_long_section_names)
  1430. .#define bfd_coff_set_long_section_names(abfd, enable) \
  1431. . ((coff_backend_info (abfd)->_bfd_coff_set_long_section_names) (abfd, enable))
  1432. .#define bfd_coff_default_section_alignment_power(abfd) \
  1433. . (coff_backend_info (abfd)->_bfd_coff_default_section_alignment_power)
  1434. .#define bfd_coff_max_nscns(abfd) \
  1435. . (coff_backend_info (abfd)->_bfd_coff_max_nscns)
  1436. .
  1437. .#define bfd_coff_swap_filehdr_in(abfd, i,o) \
  1438. . ((coff_backend_info (abfd)->_bfd_coff_swap_filehdr_in) (abfd, i, o))
  1439. .
  1440. .#define bfd_coff_swap_aouthdr_in(abfd, i,o) \
  1441. . ((coff_backend_info (abfd)->_bfd_coff_swap_aouthdr_in) (abfd, i, o))
  1442. .
  1443. .#define bfd_coff_swap_scnhdr_in(abfd, i,o) \
  1444. . ((coff_backend_info (abfd)->_bfd_coff_swap_scnhdr_in) (abfd, i, o))
  1445. .
  1446. .#define bfd_coff_swap_reloc_in(abfd, i, o) \
  1447. . ((coff_backend_info (abfd)->_bfd_coff_swap_reloc_in) (abfd, i, o))
  1448. .
  1449. .#define bfd_coff_bad_format_hook(abfd, filehdr) \
  1450. . ((coff_backend_info (abfd)->_bfd_coff_bad_format_hook) (abfd, filehdr))
  1451. .
  1452. .#define bfd_coff_set_arch_mach_hook(abfd, filehdr)\
  1453. . ((coff_backend_info (abfd)->_bfd_coff_set_arch_mach_hook) (abfd, filehdr))
  1454. .#define bfd_coff_mkobject_hook(abfd, filehdr, aouthdr)\
  1455. . ((coff_backend_info (abfd)->_bfd_coff_mkobject_hook)\
  1456. . (abfd, filehdr, aouthdr))
  1457. .
  1458. .#define bfd_coff_styp_to_sec_flags_hook(abfd, scnhdr, name, section, flags_ptr)\
  1459. . ((coff_backend_info (abfd)->_bfd_styp_to_sec_flags_hook)\
  1460. . (abfd, scnhdr, name, section, flags_ptr))
  1461. .
  1462. .#define bfd_coff_set_alignment_hook(abfd, sec, scnhdr)\
  1463. . ((coff_backend_info (abfd)->_bfd_set_alignment_hook) (abfd, sec, scnhdr))
  1464. .
  1465. .#define bfd_coff_slurp_symbol_table(abfd)\
  1466. . ((coff_backend_info (abfd)->_bfd_coff_slurp_symbol_table) (abfd))
  1467. .
  1468. .#define bfd_coff_symname_in_debug(abfd, sym)\
  1469. . ((coff_backend_info (abfd)->_bfd_coff_symname_in_debug) (abfd, sym))
  1470. .
  1471. .#define bfd_coff_force_symnames_in_strings(abfd)\
  1472. . (coff_backend_info (abfd)->_bfd_coff_force_symnames_in_strings)
  1473. .
  1474. .#define bfd_coff_debug_string_prefix_length(abfd)\
  1475. . (coff_backend_info (abfd)->_bfd_coff_debug_string_prefix_length)
  1476. .
  1477. .#define bfd_coff_print_aux(abfd, file, base, symbol, aux, indaux)\
  1478. . ((coff_backend_info (abfd)->_bfd_coff_print_aux)\
  1479. . (abfd, file, base, symbol, aux, indaux))
  1480. .
  1481. .#define bfd_coff_reloc16_extra_cases(abfd, link_info, link_order,\
  1482. . reloc, data, src_ptr, dst_ptr)\
  1483. . ((coff_backend_info (abfd)->_bfd_coff_reloc16_extra_cases)\
  1484. . (abfd, link_info, link_order, reloc, data, src_ptr, dst_ptr))
  1485. .
  1486. .#define bfd_coff_reloc16_estimate(abfd, section, reloc, shrink, link_info)\
  1487. . ((coff_backend_info (abfd)->_bfd_coff_reloc16_estimate)\
  1488. . (abfd, section, reloc, shrink, link_info))
  1489. .
  1490. .#define bfd_coff_classify_symbol(abfd, sym)\
  1491. . ((coff_backend_info (abfd)->_bfd_coff_classify_symbol)\
  1492. . (abfd, sym))
  1493. .
  1494. .#define bfd_coff_compute_section_file_positions(abfd)\
  1495. . ((coff_backend_info (abfd)->_bfd_coff_compute_section_file_positions)\
  1496. . (abfd))
  1497. .
  1498. .#define bfd_coff_start_final_link(obfd, info)\
  1499. . ((coff_backend_info (obfd)->_bfd_coff_start_final_link)\
  1500. . (obfd, info))
  1501. .#define bfd_coff_relocate_section(obfd,info,ibfd,o,con,rel,isyms,secs)\
  1502. . ((coff_backend_info (ibfd)->_bfd_coff_relocate_section)\
  1503. . (obfd, info, ibfd, o, con, rel, isyms, secs))
  1504. .#define bfd_coff_rtype_to_howto(abfd, sec, rel, h, sym, addendp)\
  1505. . ((coff_backend_info (abfd)->_bfd_coff_rtype_to_howto)\
  1506. . (abfd, sec, rel, h, sym, addendp))
  1507. .#define bfd_coff_adjust_symndx(obfd, info, ibfd, sec, rel, adjustedp)\
  1508. . ((coff_backend_info (abfd)->_bfd_coff_adjust_symndx)\
  1509. . (obfd, info, ibfd, sec, rel, adjustedp))
  1510. .#define bfd_coff_link_add_one_symbol(info, abfd, name, flags, section,\
  1511. . value, string, cp, coll, hashp)\
  1512. . ((coff_backend_info (abfd)->_bfd_coff_link_add_one_symbol)\
  1513. . (info, abfd, name, flags, section, value, string, cp, coll, hashp))
  1514. .
  1515. .#define bfd_coff_link_output_has_begun(a,p) \
  1516. . ((coff_backend_info (a)->_bfd_coff_link_output_has_begun) (a, p))
  1517. .#define bfd_coff_final_link_postscript(a,p) \
  1518. . ((coff_backend_info (a)->_bfd_coff_final_link_postscript) (a, p))
  1519. .
  1520. .#define bfd_coff_have_print_pdata(a) \
  1521. . (coff_backend_info (a)->_bfd_coff_print_pdata)
  1522. .#define bfd_coff_print_pdata(a,p) \
  1523. . ((coff_backend_info (a)->_bfd_coff_print_pdata) (a, p))
  1524. .
  1525. .{* Macro: Returns true if the bfd is a PE executable as opposed to a
  1526. . PE object file. *}
  1527. .#define bfd_pei_p(abfd) \
  1528. . (startswith ((abfd)->xvec->name, "pei-"))
  1529. */
  1530. /* See whether the magic number matches. */
  1531. static bool
  1532. coff_bad_format_hook (bfd * abfd ATTRIBUTE_UNUSED, void * filehdr)
  1533. {
  1534. struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  1535. if (BADMAG (*internal_f))
  1536. return false;
  1537. return true;
  1538. }
  1539. #ifdef TICOFF
  1540. static bool
  1541. ticoff0_bad_format_hook (bfd *abfd ATTRIBUTE_UNUSED, void * filehdr)
  1542. {
  1543. struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  1544. if (COFF0_BADMAG (*internal_f))
  1545. return false;
  1546. return true;
  1547. }
  1548. #endif
  1549. #ifdef TICOFF
  1550. static bool
  1551. ticoff1_bad_format_hook (bfd *abfd ATTRIBUTE_UNUSED, void * filehdr)
  1552. {
  1553. struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  1554. if (COFF1_BADMAG (*internal_f))
  1555. return false;
  1556. return true;
  1557. }
  1558. #endif
  1559. /* Check whether this section uses an alignment other than the
  1560. default. */
  1561. static void
  1562. coff_set_custom_section_alignment (bfd *abfd ATTRIBUTE_UNUSED,
  1563. asection *section,
  1564. const struct coff_section_alignment_entry *alignment_table,
  1565. const unsigned int table_size)
  1566. {
  1567. const unsigned int default_alignment = COFF_DEFAULT_SECTION_ALIGNMENT_POWER;
  1568. unsigned int i;
  1569. for (i = 0; i < table_size; ++i)
  1570. {
  1571. const char *secname = bfd_section_name (section);
  1572. if (alignment_table[i].comparison_length == (unsigned int) -1
  1573. ? strcmp (alignment_table[i].name, secname) == 0
  1574. : strncmp (alignment_table[i].name, secname,
  1575. alignment_table[i].comparison_length) == 0)
  1576. break;
  1577. }
  1578. if (i >= table_size)
  1579. return;
  1580. if (alignment_table[i].default_alignment_min != COFF_ALIGNMENT_FIELD_EMPTY
  1581. && default_alignment < alignment_table[i].default_alignment_min)
  1582. return;
  1583. if (alignment_table[i].default_alignment_max != COFF_ALIGNMENT_FIELD_EMPTY
  1584. #if COFF_DEFAULT_SECTION_ALIGNMENT_POWER != 0
  1585. && default_alignment > alignment_table[i].default_alignment_max
  1586. #endif
  1587. )
  1588. return;
  1589. section->alignment_power = alignment_table[i].alignment_power;
  1590. }
  1591. /* Custom section alignment records. */
  1592. static const struct coff_section_alignment_entry
  1593. coff_section_alignment_table[] =
  1594. {
  1595. #ifdef COFF_SECTION_ALIGNMENT_ENTRIES
  1596. COFF_SECTION_ALIGNMENT_ENTRIES,
  1597. #endif
  1598. /* There must not be any gaps between .stabstr sections. */
  1599. { COFF_SECTION_NAME_PARTIAL_MATCH (".stabstr"),
  1600. 1, COFF_ALIGNMENT_FIELD_EMPTY, 0 },
  1601. /* The .stab section must be aligned to 2**2 at most, to avoid gaps. */
  1602. { COFF_SECTION_NAME_PARTIAL_MATCH (".stab"),
  1603. 3, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
  1604. /* Similarly for the .ctors and .dtors sections. */
  1605. { COFF_SECTION_NAME_EXACT_MATCH (".ctors"),
  1606. 3, COFF_ALIGNMENT_FIELD_EMPTY, 2 },
  1607. { COFF_SECTION_NAME_EXACT_MATCH (".dtors"),
  1608. 3, COFF_ALIGNMENT_FIELD_EMPTY, 2 }
  1609. };
  1610. static const unsigned int coff_section_alignment_table_size =
  1611. sizeof coff_section_alignment_table / sizeof coff_section_alignment_table[0];
  1612. /* Initialize a section structure with information peculiar to this
  1613. particular implementation of COFF. */
  1614. static bool
  1615. coff_new_section_hook (bfd * abfd, asection * section)
  1616. {
  1617. combined_entry_type *native;
  1618. size_t amt;
  1619. unsigned char sclass = C_STAT;
  1620. section->alignment_power = COFF_DEFAULT_SECTION_ALIGNMENT_POWER;
  1621. #ifdef RS6000COFF_C
  1622. if (bfd_xcoff_text_align_power (abfd) != 0
  1623. && strcmp (bfd_section_name (section), ".text") == 0)
  1624. section->alignment_power = bfd_xcoff_text_align_power (abfd);
  1625. else if (bfd_xcoff_data_align_power (abfd) != 0
  1626. && strcmp (bfd_section_name (section), ".data") == 0)
  1627. section->alignment_power = bfd_xcoff_data_align_power (abfd);
  1628. else
  1629. {
  1630. int i;
  1631. for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
  1632. if (strcmp (bfd_section_name (section),
  1633. xcoff_dwsect_names[i].xcoff_name) == 0)
  1634. {
  1635. section->alignment_power = 0;
  1636. sclass = C_DWARF;
  1637. break;
  1638. }
  1639. }
  1640. #endif
  1641. /* Set up the section symbol. */
  1642. if (!_bfd_generic_new_section_hook (abfd, section))
  1643. return false;
  1644. /* Allocate aux records for section symbols, to store size and
  1645. related info.
  1646. @@ The 10 is a guess at a plausible maximum number of aux entries
  1647. (but shouldn't be a constant). */
  1648. amt = sizeof (combined_entry_type) * 10;
  1649. native = (combined_entry_type *) bfd_zalloc (abfd, amt);
  1650. if (native == NULL)
  1651. return false;
  1652. /* We don't need to set up n_name, n_value, or n_scnum in the native
  1653. symbol information, since they'll be overridden by the BFD symbol
  1654. anyhow. However, we do need to set the type and storage class,
  1655. in case this symbol winds up getting written out. The value 0
  1656. for n_numaux is already correct. */
  1657. native->is_sym = true;
  1658. native->u.syment.n_type = T_NULL;
  1659. native->u.syment.n_sclass = sclass;
  1660. coffsymbol (section->symbol)->native = native;
  1661. coff_set_custom_section_alignment (abfd, section,
  1662. coff_section_alignment_table,
  1663. coff_section_alignment_table_size);
  1664. return true;
  1665. }
  1666. #ifdef COFF_ALIGN_IN_SECTION_HEADER
  1667. /* Set the alignment of a BFD section. */
  1668. static void
  1669. coff_set_alignment_hook (bfd * abfd ATTRIBUTE_UNUSED,
  1670. asection * section,
  1671. void * scnhdr)
  1672. {
  1673. struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
  1674. unsigned int i;
  1675. #ifdef COFF_DECODE_ALIGNMENT
  1676. i = COFF_DECODE_ALIGNMENT(hdr->s_flags);
  1677. #endif
  1678. section->alignment_power = i;
  1679. #ifdef coff_set_section_load_page
  1680. coff_set_section_load_page (section, hdr->s_page);
  1681. #endif
  1682. }
  1683. #else /* ! COFF_ALIGN_IN_SECTION_HEADER */
  1684. #ifdef COFF_WITH_PE
  1685. static void
  1686. coff_set_alignment_hook (bfd * abfd ATTRIBUTE_UNUSED,
  1687. asection * section,
  1688. void * scnhdr)
  1689. {
  1690. struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
  1691. size_t amt;
  1692. unsigned int alignment_power_const
  1693. = hdr->s_flags & IMAGE_SCN_ALIGN_POWER_BIT_MASK;
  1694. switch (alignment_power_const)
  1695. {
  1696. case IMAGE_SCN_ALIGN_8192BYTES:
  1697. case IMAGE_SCN_ALIGN_4096BYTES:
  1698. case IMAGE_SCN_ALIGN_2048BYTES:
  1699. case IMAGE_SCN_ALIGN_1024BYTES:
  1700. case IMAGE_SCN_ALIGN_512BYTES:
  1701. case IMAGE_SCN_ALIGN_256BYTES:
  1702. case IMAGE_SCN_ALIGN_128BYTES:
  1703. case IMAGE_SCN_ALIGN_64BYTES:
  1704. case IMAGE_SCN_ALIGN_32BYTES:
  1705. case IMAGE_SCN_ALIGN_16BYTES:
  1706. case IMAGE_SCN_ALIGN_8BYTES:
  1707. case IMAGE_SCN_ALIGN_4BYTES:
  1708. case IMAGE_SCN_ALIGN_2BYTES:
  1709. case IMAGE_SCN_ALIGN_1BYTES:
  1710. section->alignment_power
  1711. = IMAGE_SCN_ALIGN_POWER_NUM (alignment_power_const);
  1712. break;
  1713. default:
  1714. break;
  1715. }
  1716. /* In a PE image file, the s_paddr field holds the virtual size of a
  1717. section, while the s_size field holds the raw size. We also keep
  1718. the original section flag value, since not every bit can be
  1719. mapped onto a generic BFD section bit. */
  1720. if (coff_section_data (abfd, section) == NULL)
  1721. {
  1722. amt = sizeof (struct coff_section_tdata);
  1723. section->used_by_bfd = bfd_zalloc (abfd, amt);
  1724. if (section->used_by_bfd == NULL)
  1725. /* FIXME: Return error. */
  1726. abort ();
  1727. }
  1728. if (pei_section_data (abfd, section) == NULL)
  1729. {
  1730. amt = sizeof (struct pei_section_tdata);
  1731. coff_section_data (abfd, section)->tdata = bfd_zalloc (abfd, amt);
  1732. if (coff_section_data (abfd, section)->tdata == NULL)
  1733. /* FIXME: Return error. */
  1734. abort ();
  1735. }
  1736. pei_section_data (abfd, section)->virt_size = hdr->s_paddr;
  1737. pei_section_data (abfd, section)->pe_flags = hdr->s_flags;
  1738. section->lma = hdr->s_vaddr;
  1739. /* Check for extended relocs. */
  1740. if (hdr->s_flags & IMAGE_SCN_LNK_NRELOC_OVFL)
  1741. {
  1742. struct external_reloc dst;
  1743. struct internal_reloc n;
  1744. file_ptr oldpos = bfd_tell (abfd);
  1745. bfd_size_type relsz = bfd_coff_relsz (abfd);
  1746. if (bfd_seek (abfd, (file_ptr) hdr->s_relptr, 0) != 0)
  1747. return;
  1748. if (bfd_bread (& dst, relsz, abfd) != relsz)
  1749. return;
  1750. bfd_coff_swap_reloc_in (abfd, &dst, &n);
  1751. if (bfd_seek (abfd, oldpos, 0) != 0)
  1752. return;
  1753. if (n.r_vaddr < 0x10000)
  1754. {
  1755. _bfd_error_handler (_("%pB: overflow reloc count too small"), abfd);
  1756. bfd_set_error (bfd_error_bad_value);
  1757. return;
  1758. }
  1759. section->reloc_count = hdr->s_nreloc = n.r_vaddr - 1;
  1760. section->rel_filepos += relsz;
  1761. }
  1762. else if (hdr->s_nreloc == 0xffff)
  1763. _bfd_error_handler
  1764. (_("%pB: warning: claims to have 0xffff relocs, without overflow"),
  1765. abfd);
  1766. }
  1767. #undef ALIGN_SET
  1768. #undef ELIFALIGN_SET
  1769. #else /* ! COFF_WITH_PE */
  1770. #ifdef RS6000COFF_C
  1771. /* We grossly abuse this function to handle XCOFF overflow headers.
  1772. When we see one, we correct the reloc and line number counts in the
  1773. real header, and remove the section we just created. */
  1774. static void
  1775. coff_set_alignment_hook (bfd *abfd, asection *section, void * scnhdr)
  1776. {
  1777. struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
  1778. asection *real_sec;
  1779. if ((hdr->s_flags & STYP_OVRFLO) == 0)
  1780. return;
  1781. real_sec = coff_section_from_bfd_index (abfd, (int) hdr->s_nreloc);
  1782. if (real_sec == NULL)
  1783. return;
  1784. real_sec->reloc_count = hdr->s_paddr;
  1785. real_sec->lineno_count = hdr->s_vaddr;
  1786. if (!bfd_section_removed_from_list (abfd, section))
  1787. {
  1788. bfd_section_list_remove (abfd, section);
  1789. --abfd->section_count;
  1790. }
  1791. }
  1792. #else /* ! RS6000COFF_C */
  1793. #if defined (COFF_GO32_EXE) || defined (COFF_GO32)
  1794. static void
  1795. coff_set_alignment_hook (bfd * abfd, asection * section, void * scnhdr)
  1796. {
  1797. struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
  1798. /* Check for extended relocs. */
  1799. if (hdr->s_flags & IMAGE_SCN_LNK_NRELOC_OVFL)
  1800. {
  1801. struct external_reloc dst;
  1802. struct internal_reloc n;
  1803. const file_ptr oldpos = bfd_tell (abfd);
  1804. const bfd_size_type relsz = bfd_coff_relsz (abfd);
  1805. if (bfd_seek (abfd, (file_ptr) hdr->s_relptr, 0) != 0)
  1806. return;
  1807. if (bfd_bread (& dst, relsz, abfd) != relsz)
  1808. return;
  1809. bfd_coff_swap_reloc_in (abfd, &dst, &n);
  1810. if (bfd_seek (abfd, oldpos, 0) != 0)
  1811. return;
  1812. section->reloc_count = hdr->s_nreloc = n.r_vaddr - 1;
  1813. section->rel_filepos += relsz;
  1814. }
  1815. else if (hdr->s_nreloc == 0xffff)
  1816. _bfd_error_handler
  1817. (_("%pB: warning: claims to have 0xffff relocs, without overflow"),
  1818. abfd);
  1819. }
  1820. #else /* ! COFF_GO32_EXE && ! COFF_GO32 */
  1821. static void
  1822. coff_set_alignment_hook (bfd *abfd ATTRIBUTE_UNUSED,
  1823. asection *section ATTRIBUTE_UNUSED,
  1824. void *scnhdr ATTRIBUTE_UNUSED)
  1825. {
  1826. }
  1827. #endif /* ! COFF_GO32_EXE && ! COFF_GO32 */
  1828. #endif /* ! RS6000COFF_C */
  1829. #endif /* ! COFF_WITH_PE */
  1830. #endif /* ! COFF_ALIGN_IN_SECTION_HEADER */
  1831. #ifndef coff_mkobject
  1832. static bool
  1833. coff_mkobject (bfd * abfd)
  1834. {
  1835. coff_data_type *coff;
  1836. size_t amt = sizeof (coff_data_type);
  1837. abfd->tdata.coff_obj_data = bfd_zalloc (abfd, amt);
  1838. if (abfd->tdata.coff_obj_data == NULL)
  1839. return false;
  1840. coff = coff_data (abfd);
  1841. coff->symbols = NULL;
  1842. coff->conversion_table = NULL;
  1843. coff->raw_syments = NULL;
  1844. coff->relocbase = 0;
  1845. coff->local_toc_sym_map = 0;
  1846. /* make_abs_section(abfd);*/
  1847. return true;
  1848. }
  1849. #endif
  1850. /* Create the COFF backend specific information. */
  1851. #ifndef coff_mkobject_hook
  1852. static void *
  1853. coff_mkobject_hook (bfd * abfd,
  1854. void * filehdr,
  1855. void * aouthdr ATTRIBUTE_UNUSED)
  1856. {
  1857. struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  1858. coff_data_type *coff;
  1859. if (! coff_mkobject (abfd))
  1860. return NULL;
  1861. coff = coff_data (abfd);
  1862. coff->sym_filepos = internal_f->f_symptr;
  1863. /* These members communicate important constants about the symbol
  1864. table to GDB's symbol-reading code. These `constants'
  1865. unfortunately vary among coff implementations... */
  1866. coff->local_n_btmask = N_BTMASK;
  1867. coff->local_n_btshft = N_BTSHFT;
  1868. coff->local_n_tmask = N_TMASK;
  1869. coff->local_n_tshift = N_TSHIFT;
  1870. coff->local_symesz = bfd_coff_symesz (abfd);
  1871. coff->local_auxesz = bfd_coff_auxesz (abfd);
  1872. coff->local_linesz = bfd_coff_linesz (abfd);
  1873. coff->timestamp = internal_f->f_timdat;
  1874. obj_raw_syment_count (abfd) =
  1875. obj_conv_table_size (abfd) =
  1876. internal_f->f_nsyms;
  1877. #ifdef RS6000COFF_C
  1878. if ((internal_f->f_flags & F_SHROBJ) != 0)
  1879. abfd->flags |= DYNAMIC;
  1880. if (aouthdr != NULL && internal_f->f_opthdr >= bfd_coff_aoutsz (abfd))
  1881. {
  1882. struct internal_aouthdr *internal_a =
  1883. (struct internal_aouthdr *) aouthdr;
  1884. struct xcoff_tdata *xcoff;
  1885. xcoff = xcoff_data (abfd);
  1886. # ifdef U803XTOCMAGIC
  1887. xcoff->xcoff64 = internal_f->f_magic == U803XTOCMAGIC;
  1888. # else
  1889. xcoff->xcoff64 = 0;
  1890. # endif
  1891. xcoff->full_aouthdr = true;
  1892. xcoff->toc = internal_a->o_toc;
  1893. xcoff->sntoc = internal_a->o_sntoc;
  1894. xcoff->snentry = internal_a->o_snentry;
  1895. bfd_xcoff_text_align_power (abfd) = internal_a->o_algntext;
  1896. bfd_xcoff_data_align_power (abfd) = internal_a->o_algndata;
  1897. xcoff->modtype = internal_a->o_modtype;
  1898. xcoff->cputype = internal_a->o_cputype;
  1899. xcoff->maxdata = internal_a->o_maxdata;
  1900. xcoff->maxstack = internal_a->o_maxstack;
  1901. }
  1902. #endif
  1903. #ifdef ARM
  1904. /* Set the flags field from the COFF header read in. */
  1905. if (! _bfd_coff_arm_set_private_flags (abfd, internal_f->f_flags))
  1906. coff->flags = 0;
  1907. #endif
  1908. #ifdef COFF_WITH_PE
  1909. /* FIXME: I'm not sure this is ever executed, since peicode.h
  1910. defines coff_mkobject_hook. */
  1911. if ((internal_f->f_flags & IMAGE_FILE_DEBUG_STRIPPED) == 0)
  1912. abfd->flags |= HAS_DEBUG;
  1913. #endif
  1914. return coff;
  1915. }
  1916. #endif
  1917. /* Determine the machine architecture and type. FIXME: This is target
  1918. dependent because the magic numbers are defined in the target
  1919. dependent header files. But there is no particular need for this.
  1920. If the magic numbers were moved to a separate file, this function
  1921. would be target independent and would also be much more successful
  1922. at linking together COFF files for different architectures. */
  1923. static bool
  1924. coff_set_arch_mach_hook (bfd *abfd, void * filehdr)
  1925. {
  1926. unsigned long machine;
  1927. enum bfd_architecture arch;
  1928. struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  1929. /* Zero selects the default machine for an arch. */
  1930. machine = 0;
  1931. switch (internal_f->f_magic)
  1932. {
  1933. #ifdef I386MAGIC
  1934. case I386MAGIC:
  1935. case I386PTXMAGIC:
  1936. case I386AIXMAGIC: /* Danbury PS/2 AIX C Compiler. */
  1937. case LYNXCOFFMAGIC:
  1938. case I386_APPLE_MAGIC:
  1939. case I386_FREEBSD_MAGIC:
  1940. case I386_LINUX_MAGIC:
  1941. case I386_NETBSD_MAGIC:
  1942. arch = bfd_arch_i386;
  1943. break;
  1944. #endif
  1945. #ifdef AMD64MAGIC
  1946. case AMD64MAGIC:
  1947. case AMD64_APPLE_MAGIC:
  1948. case AMD64_FREEBSD_MAGIC:
  1949. case AMD64_LINUX_MAGIC:
  1950. case AMD64_NETBSD_MAGIC:
  1951. arch = bfd_arch_i386;
  1952. machine = bfd_mach_x86_64;
  1953. break;
  1954. #endif
  1955. #ifdef IA64MAGIC
  1956. case IA64MAGIC:
  1957. arch = bfd_arch_ia64;
  1958. break;
  1959. #endif
  1960. #ifdef ARMMAGIC
  1961. case ARMMAGIC:
  1962. case ARMPEMAGIC:
  1963. case THUMBPEMAGIC:
  1964. arch = bfd_arch_arm;
  1965. machine = bfd_arm_get_mach_from_notes (abfd, ARM_NOTE_SECTION);
  1966. if (machine == bfd_mach_arm_unknown)
  1967. {
  1968. switch (internal_f->f_flags & F_ARM_ARCHITECTURE_MASK)
  1969. {
  1970. case F_ARM_2: machine = bfd_mach_arm_2; break;
  1971. case F_ARM_2a: machine = bfd_mach_arm_2a; break;
  1972. case F_ARM_3: machine = bfd_mach_arm_3; break;
  1973. default:
  1974. case F_ARM_3M: machine = bfd_mach_arm_3M; break;
  1975. case F_ARM_4: machine = bfd_mach_arm_4; break;
  1976. case F_ARM_4T: machine = bfd_mach_arm_4T; break;
  1977. /* The COFF header does not have enough bits available
  1978. to cover all the different ARM architectures. So
  1979. we interpret F_ARM_5, the highest flag value to mean
  1980. "the highest ARM architecture known to BFD" which is
  1981. currently the XScale. */
  1982. case F_ARM_5: machine = bfd_mach_arm_XScale; break;
  1983. }
  1984. }
  1985. break;
  1986. #endif
  1987. #ifdef AARCH64MAGIC
  1988. case AARCH64MAGIC:
  1989. arch = bfd_arch_aarch64;
  1990. machine = internal_f->f_flags & F_AARCH64_ARCHITECTURE_MASK;
  1991. break;
  1992. #endif
  1993. #ifdef Z80MAGIC
  1994. case Z80MAGIC:
  1995. arch = bfd_arch_z80;
  1996. switch (internal_f->f_flags & F_MACHMASK)
  1997. {
  1998. case bfd_mach_z80strict << 12:
  1999. case bfd_mach_z80 << 12:
  2000. case bfd_mach_z80n << 12:
  2001. case bfd_mach_z80full << 12:
  2002. case bfd_mach_r800 << 12:
  2003. case bfd_mach_gbz80 << 12:
  2004. case bfd_mach_z180 << 12:
  2005. case bfd_mach_ez80_z80 << 12:
  2006. case bfd_mach_ez80_adl << 12:
  2007. machine = ((unsigned)internal_f->f_flags & F_MACHMASK) >> 12;
  2008. break;
  2009. default:
  2010. return false;
  2011. }
  2012. break;
  2013. #endif
  2014. #ifdef Z8KMAGIC
  2015. case Z8KMAGIC:
  2016. arch = bfd_arch_z8k;
  2017. switch (internal_f->f_flags & F_MACHMASK)
  2018. {
  2019. case F_Z8001:
  2020. machine = bfd_mach_z8001;
  2021. break;
  2022. case F_Z8002:
  2023. machine = bfd_mach_z8002;
  2024. break;
  2025. default:
  2026. return false;
  2027. }
  2028. break;
  2029. #endif
  2030. #ifdef RS6000COFF_C
  2031. #ifdef XCOFF64
  2032. case U64_TOCMAGIC:
  2033. case U803XTOCMAGIC:
  2034. #else
  2035. case U802ROMAGIC:
  2036. case U802WRMAGIC:
  2037. case U802TOCMAGIC:
  2038. #endif
  2039. {
  2040. int cputype;
  2041. if (xcoff_data (abfd)->cputype != -1)
  2042. cputype = xcoff_data (abfd)->cputype & 0xff;
  2043. else
  2044. {
  2045. /* We did not get a value from the a.out header. If the
  2046. file has not been stripped, we may be able to get the
  2047. architecture information from the first symbol, if it
  2048. is a .file symbol. */
  2049. if (obj_raw_syment_count (abfd) == 0)
  2050. cputype = 0;
  2051. else
  2052. {
  2053. bfd_byte *buf;
  2054. struct internal_syment sym;
  2055. bfd_size_type amt = bfd_coff_symesz (abfd);
  2056. if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
  2057. return false;
  2058. buf = _bfd_malloc_and_read (abfd, amt, amt);
  2059. if (buf == NULL)
  2060. return false;
  2061. bfd_coff_swap_sym_in (abfd, buf, & sym);
  2062. if (sym.n_sclass == C_FILE)
  2063. cputype = sym.n_type & 0xff;
  2064. else
  2065. cputype = 0;
  2066. free (buf);
  2067. }
  2068. }
  2069. /* FIXME: We don't handle all cases here. */
  2070. switch (cputype)
  2071. {
  2072. default:
  2073. case 0:
  2074. arch = bfd_xcoff_architecture (abfd);
  2075. machine = bfd_xcoff_machine (abfd);
  2076. break;
  2077. case 1:
  2078. arch = bfd_arch_powerpc;
  2079. machine = bfd_mach_ppc_601;
  2080. break;
  2081. case 2: /* 64 bit PowerPC */
  2082. arch = bfd_arch_powerpc;
  2083. machine = bfd_mach_ppc_620;
  2084. break;
  2085. case 3:
  2086. arch = bfd_arch_powerpc;
  2087. machine = bfd_mach_ppc;
  2088. break;
  2089. case 4:
  2090. arch = bfd_arch_rs6000;
  2091. machine = bfd_mach_rs6k;
  2092. break;
  2093. }
  2094. }
  2095. break;
  2096. #endif
  2097. #ifdef SH_ARCH_MAGIC_BIG
  2098. case SH_ARCH_MAGIC_BIG:
  2099. case SH_ARCH_MAGIC_LITTLE:
  2100. #ifdef COFF_WITH_PE
  2101. case SH_ARCH_MAGIC_WINCE:
  2102. #endif
  2103. arch = bfd_arch_sh;
  2104. break;
  2105. #endif
  2106. #ifdef MIPS_ARCH_MAGIC_WINCE
  2107. case MIPS_ARCH_MAGIC_WINCE:
  2108. arch = bfd_arch_mips;
  2109. break;
  2110. #endif
  2111. #ifdef SPARCMAGIC
  2112. case SPARCMAGIC:
  2113. #ifdef LYNXCOFFMAGIC
  2114. case LYNXCOFFMAGIC:
  2115. #endif
  2116. arch = bfd_arch_sparc;
  2117. break;
  2118. #endif
  2119. #ifdef TIC30MAGIC
  2120. case TIC30MAGIC:
  2121. arch = bfd_arch_tic30;
  2122. break;
  2123. #endif
  2124. #ifdef TICOFF0MAGIC
  2125. #ifdef TICOFF_TARGET_ARCH
  2126. /* This TI COFF section should be used by all new TI COFF v0 targets. */
  2127. case TICOFF0MAGIC:
  2128. arch = TICOFF_TARGET_ARCH;
  2129. machine = TICOFF_TARGET_MACHINE_GET (internal_f->f_flags);
  2130. break;
  2131. #endif
  2132. #endif
  2133. #ifdef TICOFF1MAGIC
  2134. /* This TI COFF section should be used by all new TI COFF v1/2 targets. */
  2135. /* TI COFF1 and COFF2 use the target_id field to specify which arch. */
  2136. case TICOFF1MAGIC:
  2137. case TICOFF2MAGIC:
  2138. switch (internal_f->f_target_id)
  2139. {
  2140. #ifdef TI_TARGET_ID
  2141. case TI_TARGET_ID:
  2142. arch = TICOFF_TARGET_ARCH;
  2143. machine = TICOFF_TARGET_MACHINE_GET (internal_f->f_flags);
  2144. break;
  2145. #endif
  2146. default:
  2147. arch = bfd_arch_obscure;
  2148. _bfd_error_handler
  2149. (_("unrecognized TI COFF target id '0x%x'"),
  2150. internal_f->f_target_id);
  2151. break;
  2152. }
  2153. break;
  2154. #endif
  2155. #ifdef MCOREMAGIC
  2156. case MCOREMAGIC:
  2157. arch = bfd_arch_mcore;
  2158. break;
  2159. #endif
  2160. default: /* Unreadable input file type. */
  2161. arch = bfd_arch_obscure;
  2162. break;
  2163. }
  2164. bfd_default_set_arch_mach (abfd, arch, machine);
  2165. return true;
  2166. }
  2167. static bool
  2168. symname_in_debug_hook (bfd *abfd ATTRIBUTE_UNUSED,
  2169. struct internal_syment *sym ATTRIBUTE_UNUSED)
  2170. {
  2171. #ifdef SYMNAME_IN_DEBUG
  2172. return SYMNAME_IN_DEBUG (sym) != 0;
  2173. #else
  2174. return false;
  2175. #endif
  2176. }
  2177. #ifdef RS6000COFF_C
  2178. #ifdef XCOFF64
  2179. #define FORCE_SYMNAMES_IN_STRINGS
  2180. #endif
  2181. /* Handle the csect auxent of a C_EXT, C_AIX_WEAKEXT or C_HIDEXT symbol. */
  2182. static bool
  2183. coff_pointerize_aux_hook (bfd *abfd ATTRIBUTE_UNUSED,
  2184. combined_entry_type *table_base,
  2185. combined_entry_type *symbol,
  2186. unsigned int indaux,
  2187. combined_entry_type *aux)
  2188. {
  2189. BFD_ASSERT (symbol->is_sym);
  2190. int n_sclass = symbol->u.syment.n_sclass;
  2191. if (CSECT_SYM_P (n_sclass)
  2192. && indaux + 1 == symbol->u.syment.n_numaux)
  2193. {
  2194. BFD_ASSERT (! aux->is_sym);
  2195. if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) == XTY_LD)
  2196. {
  2197. aux->u.auxent.x_csect.x_scnlen.p =
  2198. table_base + aux->u.auxent.x_csect.x_scnlen.l;
  2199. aux->fix_scnlen = 1;
  2200. }
  2201. /* Return TRUE to indicate that the caller should not do any
  2202. further work on this auxent. */
  2203. return true;
  2204. }
  2205. /* Return FALSE to indicate that this auxent should be handled by
  2206. the caller. */
  2207. return false;
  2208. }
  2209. #else
  2210. #define coff_pointerize_aux_hook 0
  2211. #endif /* ! RS6000COFF_C */
  2212. /* Print an aux entry. This returns TRUE if it has printed it. */
  2213. static bool
  2214. coff_print_aux (bfd *abfd ATTRIBUTE_UNUSED,
  2215. FILE *file ATTRIBUTE_UNUSED,
  2216. combined_entry_type *table_base ATTRIBUTE_UNUSED,
  2217. combined_entry_type *symbol ATTRIBUTE_UNUSED,
  2218. combined_entry_type *aux ATTRIBUTE_UNUSED,
  2219. unsigned int indaux ATTRIBUTE_UNUSED)
  2220. {
  2221. BFD_ASSERT (symbol->is_sym);
  2222. BFD_ASSERT (! aux->is_sym);
  2223. #ifdef RS6000COFF_C
  2224. if (CSECT_SYM_P (symbol->u.syment.n_sclass)
  2225. && indaux + 1 == symbol->u.syment.n_numaux)
  2226. {
  2227. /* This is a csect entry. */
  2228. fprintf (file, "AUX ");
  2229. if (SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp) != XTY_LD)
  2230. {
  2231. BFD_ASSERT (! aux->fix_scnlen);
  2232. fprintf (file, "val %5" BFD_VMA_FMT "d",
  2233. aux->u.auxent.x_csect.x_scnlen.l);
  2234. }
  2235. else
  2236. {
  2237. fprintf (file, "indx ");
  2238. if (! aux->fix_scnlen)
  2239. fprintf (file, "%4" BFD_VMA_FMT "d",
  2240. aux->u.auxent.x_csect.x_scnlen.l);
  2241. else
  2242. fprintf (file, "%4ld",
  2243. (long) (aux->u.auxent.x_csect.x_scnlen.p - table_base));
  2244. }
  2245. fprintf (file,
  2246. " prmhsh %ld snhsh %u typ %d algn %d clss %u stb %ld snstb %u",
  2247. aux->u.auxent.x_csect.x_parmhash,
  2248. (unsigned int) aux->u.auxent.x_csect.x_snhash,
  2249. SMTYP_SMTYP (aux->u.auxent.x_csect.x_smtyp),
  2250. SMTYP_ALIGN (aux->u.auxent.x_csect.x_smtyp),
  2251. (unsigned int) aux->u.auxent.x_csect.x_smclas,
  2252. aux->u.auxent.x_csect.x_stab,
  2253. (unsigned int) aux->u.auxent.x_csect.x_snstab);
  2254. return true;
  2255. }
  2256. #endif
  2257. /* Return FALSE to indicate that no special action was taken. */
  2258. return false;
  2259. }
  2260. /*
  2261. SUBSUBSECTION
  2262. Writing relocations
  2263. To write relocations, the back end steps though the
  2264. canonical relocation table and create an
  2265. @code{internal_reloc}. The symbol index to use is removed from
  2266. the @code{offset} field in the symbol table supplied. The
  2267. address comes directly from the sum of the section base
  2268. address and the relocation offset; the type is dug directly
  2269. from the howto field. Then the @code{internal_reloc} is
  2270. swapped into the shape of an @code{external_reloc} and written
  2271. out to disk.
  2272. */
  2273. #ifdef TARG_AUX
  2274. /* AUX's ld wants relocations to be sorted. */
  2275. static int
  2276. compare_arelent_ptr (const void * x, const void * y)
  2277. {
  2278. const arelent **a = (const arelent **) x;
  2279. const arelent **b = (const arelent **) y;
  2280. bfd_size_type aadr = (*a)->address;
  2281. bfd_size_type badr = (*b)->address;
  2282. return (aadr < badr ? -1 : badr < aadr ? 1 : 0);
  2283. }
  2284. #endif /* TARG_AUX */
  2285. static bool
  2286. coff_write_relocs (bfd * abfd, int first_undef)
  2287. {
  2288. asection *s;
  2289. for (s = abfd->sections; s != NULL; s = s->next)
  2290. {
  2291. unsigned int i;
  2292. struct external_reloc dst;
  2293. arelent **p;
  2294. #ifndef TARG_AUX
  2295. p = s->orelocation;
  2296. #else
  2297. {
  2298. /* Sort relocations before we write them out. */
  2299. bfd_size_type amt;
  2300. amt = s->reloc_count;
  2301. amt *= sizeof (arelent *);
  2302. p = bfd_malloc (amt);
  2303. if (p == NULL)
  2304. {
  2305. if (s->reloc_count > 0)
  2306. return false;
  2307. }
  2308. else
  2309. {
  2310. memcpy (p, s->orelocation, (size_t) amt);
  2311. qsort (p, s->reloc_count, sizeof (arelent *), compare_arelent_ptr);
  2312. }
  2313. }
  2314. #endif
  2315. if (bfd_seek (abfd, s->rel_filepos, SEEK_SET) != 0)
  2316. return false;
  2317. #ifdef COFF_WITH_EXTENDED_RELOC_COUNTER
  2318. if ((obj_pe (abfd) || obj_go32 (abfd)) && s->reloc_count >= 0xffff)
  2319. {
  2320. /* Encode real count here as first reloc. */
  2321. struct internal_reloc n;
  2322. memset (& n, 0, sizeof (n));
  2323. /* Add one to count *this* reloc (grr). */
  2324. n.r_vaddr = s->reloc_count + 1;
  2325. coff_swap_reloc_out (abfd, &n, &dst);
  2326. if (bfd_bwrite (& dst, (bfd_size_type) bfd_coff_relsz (abfd),
  2327. abfd) != bfd_coff_relsz (abfd))
  2328. return false;
  2329. }
  2330. #endif
  2331. for (i = 0; i < s->reloc_count; i++)
  2332. {
  2333. struct internal_reloc n;
  2334. arelent *q = p[i];
  2335. memset (& n, 0, sizeof (n));
  2336. /* Now we've renumbered the symbols we know where the
  2337. undefined symbols live in the table. Check the reloc
  2338. entries for symbols who's output bfd isn't the right one.
  2339. This is because the symbol was undefined (which means
  2340. that all the pointers are never made to point to the same
  2341. place). This is a bad thing,'cause the symbols attached
  2342. to the output bfd are indexed, so that the relocation
  2343. entries know which symbol index they point to. So we
  2344. have to look up the output symbol here. */
  2345. if (q->sym_ptr_ptr[0] != NULL && q->sym_ptr_ptr[0]->the_bfd != abfd)
  2346. {
  2347. int j;
  2348. const char *sname = q->sym_ptr_ptr[0]->name;
  2349. asymbol **outsyms = abfd->outsymbols;
  2350. for (j = first_undef; outsyms[j]; j++)
  2351. {
  2352. const char *intable = outsyms[j]->name;
  2353. if (strcmp (intable, sname) == 0)
  2354. {
  2355. /* Got a hit, so repoint the reloc. */
  2356. q->sym_ptr_ptr = outsyms + j;
  2357. break;
  2358. }
  2359. }
  2360. }
  2361. n.r_vaddr = q->address + s->vma;
  2362. #ifdef R_IHCONST
  2363. /* The 29k const/consth reloc pair is a real kludge. The consth
  2364. part doesn't have a symbol; it has an offset. So rebuilt
  2365. that here. */
  2366. if (q->howto->type == R_IHCONST)
  2367. n.r_symndx = q->addend;
  2368. else
  2369. #endif
  2370. if (q->sym_ptr_ptr && q->sym_ptr_ptr[0] != NULL)
  2371. {
  2372. #ifdef SECTION_RELATIVE_ABSOLUTE_SYMBOL_P
  2373. if (SECTION_RELATIVE_ABSOLUTE_SYMBOL_P (q, s))
  2374. #else
  2375. if ((*q->sym_ptr_ptr)->section == bfd_abs_section_ptr
  2376. && ((*q->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0)
  2377. #endif
  2378. /* This is a relocation relative to the absolute symbol. */
  2379. n.r_symndx = -1;
  2380. else
  2381. {
  2382. n.r_symndx = get_index ((*(q->sym_ptr_ptr)));
  2383. /* Check to see if the symbol reloc points to a symbol
  2384. we don't have in our symbol table. */
  2385. if (n.r_symndx > obj_conv_table_size (abfd))
  2386. {
  2387. bfd_set_error (bfd_error_bad_value);
  2388. /* xgettext:c-format */
  2389. _bfd_error_handler (_("%pB: reloc against a non-existent"
  2390. " symbol index: %ld"),
  2391. abfd, n.r_symndx);
  2392. return false;
  2393. }
  2394. }
  2395. }
  2396. #ifdef SWAP_OUT_RELOC_OFFSET
  2397. n.r_offset = q->addend;
  2398. #endif
  2399. #ifdef SELECT_RELOC
  2400. /* Work out reloc type from what is required. */
  2401. SELECT_RELOC (n, q->howto);
  2402. #else
  2403. n.r_type = q->howto->type;
  2404. #endif
  2405. coff_swap_reloc_out (abfd, &n, &dst);
  2406. if (bfd_bwrite (& dst, (bfd_size_type) bfd_coff_relsz (abfd),
  2407. abfd) != bfd_coff_relsz (abfd))
  2408. return false;
  2409. }
  2410. #ifdef TARG_AUX
  2411. free (p);
  2412. #endif
  2413. }
  2414. return true;
  2415. }
  2416. /* Set flags and magic number of a coff file from architecture and machine
  2417. type. Result is TRUE if we can represent the arch&type, FALSE if not. */
  2418. static bool
  2419. coff_set_flags (bfd * abfd,
  2420. unsigned int *magicp ATTRIBUTE_UNUSED,
  2421. unsigned short *flagsp ATTRIBUTE_UNUSED)
  2422. {
  2423. switch (bfd_get_arch (abfd))
  2424. {
  2425. #ifdef Z80MAGIC
  2426. case bfd_arch_z80:
  2427. *magicp = Z80MAGIC;
  2428. switch (bfd_get_mach (abfd))
  2429. {
  2430. case bfd_mach_z80strict:
  2431. case bfd_mach_z80:
  2432. case bfd_mach_z80n:
  2433. case bfd_mach_z80full:
  2434. case bfd_mach_r800:
  2435. case bfd_mach_gbz80:
  2436. case bfd_mach_z180:
  2437. case bfd_mach_ez80_z80:
  2438. case bfd_mach_ez80_adl:
  2439. *flagsp = bfd_get_mach (abfd) << 12;
  2440. break;
  2441. default:
  2442. return false;
  2443. }
  2444. return true;
  2445. #endif
  2446. #ifdef Z8KMAGIC
  2447. case bfd_arch_z8k:
  2448. *magicp = Z8KMAGIC;
  2449. switch (bfd_get_mach (abfd))
  2450. {
  2451. case bfd_mach_z8001: *flagsp = F_Z8001; break;
  2452. case bfd_mach_z8002: *flagsp = F_Z8002; break;
  2453. default: return false;
  2454. }
  2455. return true;
  2456. #endif
  2457. #ifdef TIC30MAGIC
  2458. case bfd_arch_tic30:
  2459. *magicp = TIC30MAGIC;
  2460. return true;
  2461. #endif
  2462. #ifdef TICOFF_DEFAULT_MAGIC
  2463. case TICOFF_TARGET_ARCH:
  2464. /* If there's no indication of which version we want, use the default. */
  2465. if (!abfd->xvec )
  2466. *magicp = TICOFF_DEFAULT_MAGIC;
  2467. else
  2468. {
  2469. /* We may want to output in a different COFF version. */
  2470. switch (abfd->xvec->name[4])
  2471. {
  2472. case '0':
  2473. *magicp = TICOFF0MAGIC;
  2474. break;
  2475. case '1':
  2476. *magicp = TICOFF1MAGIC;
  2477. break;
  2478. case '2':
  2479. *magicp = TICOFF2MAGIC;
  2480. break;
  2481. default:
  2482. return false;
  2483. }
  2484. }
  2485. TICOFF_TARGET_MACHINE_SET (flagsp, bfd_get_mach (abfd));
  2486. return true;
  2487. #endif
  2488. #ifdef AARCH64MAGIC
  2489. case bfd_arch_aarch64:
  2490. * magicp = AARCH64MAGIC;
  2491. return true;
  2492. #endif
  2493. #ifdef ARMMAGIC
  2494. case bfd_arch_arm:
  2495. #ifdef ARM_WINCE
  2496. * magicp = ARMPEMAGIC;
  2497. #else
  2498. * magicp = ARMMAGIC;
  2499. #endif
  2500. * flagsp = 0;
  2501. if (APCS_SET (abfd))
  2502. {
  2503. if (APCS_26_FLAG (abfd))
  2504. * flagsp |= F_APCS26;
  2505. if (APCS_FLOAT_FLAG (abfd))
  2506. * flagsp |= F_APCS_FLOAT;
  2507. if (PIC_FLAG (abfd))
  2508. * flagsp |= F_PIC;
  2509. }
  2510. if (INTERWORK_SET (abfd) && INTERWORK_FLAG (abfd))
  2511. * flagsp |= F_INTERWORK;
  2512. switch (bfd_get_mach (abfd))
  2513. {
  2514. case bfd_mach_arm_2: * flagsp |= F_ARM_2; break;
  2515. case bfd_mach_arm_2a: * flagsp |= F_ARM_2a; break;
  2516. case bfd_mach_arm_3: * flagsp |= F_ARM_3; break;
  2517. case bfd_mach_arm_3M: * flagsp |= F_ARM_3M; break;
  2518. case bfd_mach_arm_4: * flagsp |= F_ARM_4; break;
  2519. case bfd_mach_arm_4T: * flagsp |= F_ARM_4T; break;
  2520. case bfd_mach_arm_5: * flagsp |= F_ARM_5; break;
  2521. /* FIXME: we do not have F_ARM vaues greater than F_ARM_5.
  2522. See also the comment in coff_set_arch_mach_hook(). */
  2523. case bfd_mach_arm_5T: * flagsp |= F_ARM_5; break;
  2524. case bfd_mach_arm_5TE: * flagsp |= F_ARM_5; break;
  2525. case bfd_mach_arm_XScale: * flagsp |= F_ARM_5; break;
  2526. }
  2527. return true;
  2528. #endif
  2529. #if defined(I386MAGIC) || defined(AMD64MAGIC)
  2530. case bfd_arch_i386:
  2531. #if defined(I386MAGIC)
  2532. *magicp = I386MAGIC;
  2533. #endif
  2534. #if defined LYNXOS
  2535. /* Just overwrite the usual value if we're doing Lynx. */
  2536. *magicp = LYNXCOFFMAGIC;
  2537. #endif
  2538. #if defined AMD64MAGIC
  2539. *magicp = AMD64MAGIC;
  2540. #endif
  2541. return true;
  2542. #endif
  2543. #ifdef IA64MAGIC
  2544. case bfd_arch_ia64:
  2545. *magicp = IA64MAGIC;
  2546. return true;
  2547. #endif
  2548. #ifdef SH_ARCH_MAGIC_BIG
  2549. case bfd_arch_sh:
  2550. #ifdef COFF_IMAGE_WITH_PE
  2551. *magicp = SH_ARCH_MAGIC_WINCE;
  2552. #else
  2553. if (bfd_big_endian (abfd))
  2554. *magicp = SH_ARCH_MAGIC_BIG;
  2555. else
  2556. *magicp = SH_ARCH_MAGIC_LITTLE;
  2557. #endif
  2558. return true;
  2559. #endif
  2560. #ifdef MIPS_ARCH_MAGIC_WINCE
  2561. case bfd_arch_mips:
  2562. *magicp = MIPS_ARCH_MAGIC_WINCE;
  2563. return true;
  2564. #endif
  2565. #ifdef SPARCMAGIC
  2566. case bfd_arch_sparc:
  2567. *magicp = SPARCMAGIC;
  2568. #ifdef LYNXOS
  2569. /* Just overwrite the usual value if we're doing Lynx. */
  2570. *magicp = LYNXCOFFMAGIC;
  2571. #endif
  2572. return true;
  2573. #endif
  2574. #ifdef RS6000COFF_C
  2575. case bfd_arch_rs6000:
  2576. case bfd_arch_powerpc:
  2577. BFD_ASSERT (bfd_get_flavour (abfd) == bfd_target_xcoff_flavour);
  2578. *magicp = bfd_xcoff_magic_number (abfd);
  2579. return true;
  2580. #endif
  2581. #ifdef MCOREMAGIC
  2582. case bfd_arch_mcore:
  2583. * magicp = MCOREMAGIC;
  2584. return true;
  2585. #endif
  2586. default: /* Unknown architecture. */
  2587. break;
  2588. }
  2589. return false;
  2590. }
  2591. static bool
  2592. coff_set_arch_mach (bfd * abfd,
  2593. enum bfd_architecture arch,
  2594. unsigned long machine)
  2595. {
  2596. unsigned dummy1;
  2597. unsigned short dummy2;
  2598. if (! bfd_default_set_arch_mach (abfd, arch, machine))
  2599. return false;
  2600. if (arch != bfd_arch_unknown
  2601. && ! coff_set_flags (abfd, &dummy1, &dummy2))
  2602. return false; /* We can't represent this type. */
  2603. return true; /* We're easy... */
  2604. }
  2605. #ifdef COFF_IMAGE_WITH_PE
  2606. /* This is used to sort sections by VMA, as required by PE image
  2607. files. */
  2608. static int
  2609. sort_by_secaddr (const void * arg1, const void * arg2)
  2610. {
  2611. const asection *a = *(const asection **) arg1;
  2612. const asection *b = *(const asection **) arg2;
  2613. if (a->vma < b->vma)
  2614. return -1;
  2615. else if (a->vma > b->vma)
  2616. return 1;
  2617. return 0;
  2618. }
  2619. #endif /* COFF_IMAGE_WITH_PE */
  2620. /* Calculate the file position for each section. */
  2621. #define ALIGN_SECTIONS_IN_FILE
  2622. #ifdef TICOFF
  2623. #undef ALIGN_SECTIONS_IN_FILE
  2624. #endif
  2625. static bool
  2626. coff_compute_section_file_positions (bfd * abfd)
  2627. {
  2628. asection *current;
  2629. file_ptr sofar = bfd_coff_filhsz (abfd);
  2630. bool align_adjust;
  2631. unsigned int target_index;
  2632. #ifdef ALIGN_SECTIONS_IN_FILE
  2633. asection *previous = NULL;
  2634. file_ptr old_sofar;
  2635. #endif
  2636. #ifdef COFF_IMAGE_WITH_PE
  2637. int page_size;
  2638. if (coff_data (abfd)->link_info
  2639. || (pe_data (abfd) && pe_data (abfd)->pe_opthdr.FileAlignment))
  2640. {
  2641. page_size = pe_data (abfd)->pe_opthdr.FileAlignment;
  2642. /* If no file alignment has been set, default to one.
  2643. This repairs 'ld -r' for arm-wince-pe target. */
  2644. if (page_size == 0)
  2645. page_size = 1;
  2646. /* PR 17512: file: 0ac816d3. */
  2647. if (page_size < 0)
  2648. {
  2649. bfd_set_error (bfd_error_file_too_big);
  2650. _bfd_error_handler
  2651. /* xgettext:c-format */
  2652. (_("%pB: page size is too large (0x%x)"), abfd, page_size);
  2653. return false;
  2654. }
  2655. }
  2656. else
  2657. page_size = PE_DEF_FILE_ALIGNMENT;
  2658. #else
  2659. #ifdef COFF_PAGE_SIZE
  2660. int page_size = COFF_PAGE_SIZE;
  2661. #endif
  2662. #endif
  2663. #ifdef RS6000COFF_C
  2664. /* On XCOFF, if we have symbols, set up the .debug section. */
  2665. if (bfd_get_symcount (abfd) > 0)
  2666. {
  2667. bfd_size_type sz;
  2668. bfd_size_type i, symcount;
  2669. asymbol **symp;
  2670. sz = 0;
  2671. symcount = bfd_get_symcount (abfd);
  2672. for (symp = abfd->outsymbols, i = 0; i < symcount; symp++, i++)
  2673. {
  2674. coff_symbol_type *cf;
  2675. cf = coff_symbol_from (*symp);
  2676. if (cf != NULL
  2677. && cf->native != NULL
  2678. && cf->native->is_sym
  2679. && SYMNAME_IN_DEBUG (&cf->native->u.syment))
  2680. {
  2681. size_t len;
  2682. len = strlen (bfd_asymbol_name (*symp));
  2683. if (len > SYMNMLEN || bfd_coff_force_symnames_in_strings (abfd))
  2684. sz += len + 1 + bfd_coff_debug_string_prefix_length (abfd);
  2685. }
  2686. }
  2687. if (sz > 0)
  2688. {
  2689. asection *dsec;
  2690. dsec = bfd_make_section_old_way (abfd, DOT_DEBUG);
  2691. if (dsec == NULL)
  2692. abort ();
  2693. dsec->size = sz;
  2694. dsec->flags |= SEC_HAS_CONTENTS;
  2695. }
  2696. }
  2697. #endif
  2698. if (bfd_get_start_address (abfd))
  2699. /* A start address may have been added to the original file. In this
  2700. case it will need an optional header to record it. */
  2701. abfd->flags |= EXEC_P;
  2702. if (abfd->flags & EXEC_P)
  2703. sofar += bfd_coff_aoutsz (abfd);
  2704. #ifdef RS6000COFF_C
  2705. else if (xcoff_data (abfd)->full_aouthdr)
  2706. sofar += bfd_coff_aoutsz (abfd);
  2707. else
  2708. sofar += SMALL_AOUTSZ;
  2709. #endif
  2710. sofar += abfd->section_count * bfd_coff_scnhsz (abfd);
  2711. #ifdef RS6000COFF_C
  2712. /* XCOFF handles overflows in the reloc and line number count fields
  2713. by allocating a new section header to hold the correct counts. */
  2714. for (current = abfd->sections; current != NULL; current = current->next)
  2715. if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
  2716. sofar += bfd_coff_scnhsz (abfd);
  2717. #endif
  2718. #ifdef COFF_IMAGE_WITH_PE
  2719. {
  2720. /* PE requires the sections to be in memory order when listed in
  2721. the section headers. It also does not like empty loadable
  2722. sections. The sections apparently do not have to be in the
  2723. right order in the image file itself, but we do need to get the
  2724. target_index values right. */
  2725. unsigned int count;
  2726. asection **section_list;
  2727. unsigned int i;
  2728. bfd_size_type amt;
  2729. #ifdef COFF_PAGE_SIZE
  2730. /* Clear D_PAGED if section alignment is smaller than
  2731. COFF_PAGE_SIZE. */
  2732. if (pe_data (abfd)->pe_opthdr.SectionAlignment < COFF_PAGE_SIZE)
  2733. abfd->flags &= ~D_PAGED;
  2734. #endif
  2735. count = 0;
  2736. for (current = abfd->sections; current != NULL; current = current->next)
  2737. ++count;
  2738. /* We allocate an extra cell to simplify the final loop. */
  2739. amt = sizeof (struct asection *) * (count + 1);
  2740. section_list = (asection **) bfd_malloc (amt);
  2741. if (section_list == NULL)
  2742. return false;
  2743. i = 0;
  2744. for (current = abfd->sections; current != NULL; current = current->next)
  2745. {
  2746. section_list[i] = current;
  2747. ++i;
  2748. }
  2749. section_list[i] = NULL;
  2750. qsort (section_list, count, sizeof (asection *), sort_by_secaddr);
  2751. /* Rethread the linked list into sorted order; at the same time,
  2752. assign target_index values. */
  2753. target_index = 1;
  2754. abfd->sections = NULL;
  2755. abfd->section_last = NULL;
  2756. for (i = 0; i < count; i++)
  2757. {
  2758. current = section_list[i];
  2759. bfd_section_list_append (abfd, current);
  2760. /* Later, if the section has zero size, we'll be throwing it
  2761. away, so we don't want to number it now. Note that having
  2762. a zero size and having real contents are different
  2763. concepts: .bss has no contents, but (usually) non-zero
  2764. size. */
  2765. if (current->size == 0)
  2766. {
  2767. /* Discard. However, it still might have (valid) symbols
  2768. in it, so arbitrarily set it to section 1 (indexing is
  2769. 1-based here; usually .text). __end__ and other
  2770. contents of .endsection really have this happen.
  2771. FIXME: This seems somewhat dubious. */
  2772. current->target_index = 1;
  2773. }
  2774. else
  2775. current->target_index = target_index++;
  2776. }
  2777. free (section_list);
  2778. }
  2779. #else /* ! COFF_IMAGE_WITH_PE */
  2780. {
  2781. /* Set the target_index field. */
  2782. target_index = 1;
  2783. for (current = abfd->sections; current != NULL; current = current->next)
  2784. current->target_index = target_index++;
  2785. }
  2786. #endif /* ! COFF_IMAGE_WITH_PE */
  2787. if (target_index >= bfd_coff_max_nscns (abfd))
  2788. {
  2789. bfd_set_error (bfd_error_file_too_big);
  2790. _bfd_error_handler
  2791. /* xgettext:c-format */
  2792. (_("%pB: too many sections (%d)"), abfd, target_index);
  2793. return false;
  2794. }
  2795. align_adjust = false;
  2796. for (current = abfd->sections;
  2797. current != NULL;
  2798. current = current->next)
  2799. {
  2800. #ifdef COFF_IMAGE_WITH_PE
  2801. /* With PE we have to pad each section to be a multiple of its
  2802. page size too, and remember both sizes. */
  2803. if (coff_section_data (abfd, current) == NULL)
  2804. {
  2805. size_t amt = sizeof (struct coff_section_tdata);
  2806. current->used_by_bfd = bfd_zalloc (abfd, amt);
  2807. if (current->used_by_bfd == NULL)
  2808. return false;
  2809. }
  2810. if (pei_section_data (abfd, current) == NULL)
  2811. {
  2812. size_t amt = sizeof (struct pei_section_tdata);
  2813. coff_section_data (abfd, current)->tdata = bfd_zalloc (abfd, amt);
  2814. if (coff_section_data (abfd, current)->tdata == NULL)
  2815. return false;
  2816. }
  2817. if (pei_section_data (abfd, current)->virt_size == 0)
  2818. pei_section_data (abfd, current)->virt_size = current->size;
  2819. #endif
  2820. /* Only deal with sections which have contents. */
  2821. if (!(current->flags & SEC_HAS_CONTENTS))
  2822. continue;
  2823. current->rawsize = current->size;
  2824. #ifdef COFF_IMAGE_WITH_PE
  2825. /* Make sure we skip empty sections in a PE image. */
  2826. if (current->size == 0)
  2827. continue;
  2828. #endif
  2829. /* Align the sections in the file to the same boundary on
  2830. which they are aligned in virtual memory. */
  2831. #ifdef ALIGN_SECTIONS_IN_FILE
  2832. if ((abfd->flags & EXEC_P) != 0)
  2833. {
  2834. /* Make sure this section is aligned on the right boundary - by
  2835. padding the previous section up if necessary. */
  2836. old_sofar = sofar;
  2837. sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
  2838. #ifdef RS6000COFF_C
  2839. /* Make sure the file offset and the vma of .text/.data are at the
  2840. same page offset, so that the file can be mmap'ed without being
  2841. relocated. Failing that, AIX is able to load and execute the
  2842. program, but it will be silently relocated (possible as
  2843. executables are PIE). But the relocation is slightly costly and
  2844. complexify the use of addr2line or gdb. So better to avoid it,
  2845. like does the native linker. Usually gnu ld makes sure that
  2846. the vma of .text is the file offset so this issue shouldn't
  2847. appear unless you are stripping such an executable.
  2848. AIX loader checks the text section alignment of (vma - filepos),
  2849. and the native linker doesn't try to align the text sections.
  2850. For example:
  2851. 0 .text 000054cc 10000128 10000128 00000128 2**5
  2852. CONTENTS, ALLOC, LOAD, CODE
  2853. Don't perform the above tweak if the previous one is .tdata,
  2854. as it will increase the memory allocated for every threads
  2855. created and not just improve performances with gdb.
  2856. */
  2857. if ((!strcmp (current->name, _TEXT)
  2858. || !strcmp (current->name, _DATA))
  2859. && (previous == NULL || strcmp(previous->name, _TDATA)))
  2860. {
  2861. bfd_vma align = 4096;
  2862. bfd_vma sofar_off = sofar % align;
  2863. bfd_vma vma_off = current->vma % align;
  2864. if (vma_off > sofar_off)
  2865. sofar += vma_off - sofar_off;
  2866. else if (vma_off < sofar_off)
  2867. sofar += align + vma_off - sofar_off;
  2868. }
  2869. #endif
  2870. if (previous != NULL)
  2871. previous->size += sofar - old_sofar;
  2872. }
  2873. #endif
  2874. /* In demand paged files the low order bits of the file offset
  2875. must match the low order bits of the virtual address. */
  2876. #ifdef COFF_PAGE_SIZE
  2877. if ((abfd->flags & D_PAGED) != 0
  2878. && (current->flags & SEC_ALLOC) != 0)
  2879. sofar += (current->vma - (bfd_vma) sofar) % page_size;
  2880. #endif
  2881. current->filepos = sofar;
  2882. #ifdef COFF_IMAGE_WITH_PE
  2883. /* Set the padded size. */
  2884. current->size = (current->size + page_size - 1) & -page_size;
  2885. #endif
  2886. sofar += current->size;
  2887. #ifdef ALIGN_SECTIONS_IN_FILE
  2888. /* Make sure that this section is of the right size too. */
  2889. if ((abfd->flags & EXEC_P) == 0)
  2890. {
  2891. bfd_size_type old_size;
  2892. old_size = current->size;
  2893. current->size = BFD_ALIGN (current->size,
  2894. 1 << current->alignment_power);
  2895. align_adjust = current->size != old_size;
  2896. sofar += current->size - old_size;
  2897. }
  2898. else
  2899. {
  2900. old_sofar = sofar;
  2901. sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
  2902. align_adjust = sofar != old_sofar;
  2903. current->size += sofar - old_sofar;
  2904. }
  2905. #endif
  2906. #ifdef COFF_IMAGE_WITH_PE
  2907. /* For PE we need to make sure we pad out to the aligned
  2908. size, in case the caller only writes out data to the
  2909. unaligned size. */
  2910. if (pei_section_data (abfd, current)->virt_size < current->size)
  2911. align_adjust = true;
  2912. #endif
  2913. #ifdef _LIB
  2914. /* Force .lib sections to start at zero. The vma is then
  2915. incremented in coff_set_section_contents. This is right for
  2916. SVR3.2. */
  2917. if (strcmp (current->name, _LIB) == 0)
  2918. bfd_set_section_vma (current, 0);
  2919. #endif
  2920. #ifdef ALIGN_SECTIONS_IN_FILE
  2921. previous = current;
  2922. #endif
  2923. }
  2924. /* It is now safe to write to the output file. If we needed an
  2925. alignment adjustment for the last section, then make sure that
  2926. there is a byte at offset sofar. If there are no symbols and no
  2927. relocs, then nothing follows the last section. If we don't force
  2928. the last byte out, then the file may appear to be truncated. */
  2929. if (align_adjust)
  2930. {
  2931. bfd_byte b;
  2932. b = 0;
  2933. if (bfd_seek (abfd, sofar - 1, SEEK_SET) != 0
  2934. || bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
  2935. return false;
  2936. }
  2937. /* Make sure the relocations are aligned. We don't need to make
  2938. sure that this byte exists, because it will only matter if there
  2939. really are relocs. */
  2940. sofar = BFD_ALIGN (sofar, 1 << COFF_DEFAULT_SECTION_ALIGNMENT_POWER);
  2941. obj_relocbase (abfd) = sofar;
  2942. abfd->output_has_begun = true;
  2943. return true;
  2944. }
  2945. #ifdef COFF_IMAGE_WITH_PE
  2946. static bool
  2947. coff_read_word (bfd *abfd, unsigned int *value, unsigned int *pelength)
  2948. {
  2949. unsigned char b[2];
  2950. int status;
  2951. status = bfd_bread (b, (bfd_size_type) 2, abfd);
  2952. if (status < 1)
  2953. {
  2954. *value = 0;
  2955. return false;
  2956. }
  2957. if (status == 1)
  2958. *value = (unsigned int) b[0];
  2959. else
  2960. *value = (unsigned int) (b[0] + (b[1] << 8));
  2961. *pelength += status;
  2962. return true;
  2963. }
  2964. static unsigned int
  2965. coff_compute_checksum (bfd *abfd, unsigned int *pelength)
  2966. {
  2967. bool more_data;
  2968. file_ptr filepos;
  2969. unsigned int value;
  2970. unsigned int total;
  2971. total = 0;
  2972. *pelength = 0;
  2973. filepos = (file_ptr) 0;
  2974. do
  2975. {
  2976. if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
  2977. return 0;
  2978. more_data = coff_read_word (abfd, &value, pelength);
  2979. total += value;
  2980. total = 0xffff & (total + (total >> 0x10));
  2981. filepos += 2;
  2982. }
  2983. while (more_data);
  2984. return (0xffff & (total + (total >> 0x10)));
  2985. }
  2986. static bool
  2987. coff_apply_checksum (bfd *abfd)
  2988. {
  2989. unsigned int computed;
  2990. unsigned int checksum = 0;
  2991. unsigned int peheader;
  2992. unsigned int pelength;
  2993. if (bfd_seek (abfd, 0x3c, SEEK_SET) != 0)
  2994. return false;
  2995. if (!coff_read_word (abfd, &peheader, &pelength))
  2996. return false;
  2997. if (bfd_seek (abfd, peheader + 0x58, SEEK_SET) != 0)
  2998. return false;
  2999. checksum = 0;
  3000. bfd_bwrite (&checksum, (bfd_size_type) 4, abfd);
  3001. if (bfd_seek (abfd, peheader, SEEK_SET) != 0)
  3002. return false;
  3003. computed = coff_compute_checksum (abfd, &pelength);
  3004. checksum = computed + pelength;
  3005. if (bfd_seek (abfd, peheader + 0x58, SEEK_SET) != 0)
  3006. return false;
  3007. bfd_bwrite (&checksum, (bfd_size_type) 4, abfd);
  3008. return true;
  3009. }
  3010. #endif /* COFF_IMAGE_WITH_PE */
  3011. static bool
  3012. coff_write_object_contents (bfd * abfd)
  3013. {
  3014. asection *current;
  3015. bool hasrelocs = false;
  3016. bool haslinno = false;
  3017. #ifdef COFF_IMAGE_WITH_PE
  3018. bool hasdebug = false;
  3019. #endif
  3020. file_ptr scn_base;
  3021. file_ptr reloc_base;
  3022. file_ptr lineno_base;
  3023. file_ptr sym_base;
  3024. unsigned long reloc_size = 0, reloc_count = 0;
  3025. unsigned long lnno_size = 0;
  3026. bool long_section_names;
  3027. asection *text_sec = NULL;
  3028. asection *data_sec = NULL;
  3029. asection *bss_sec = NULL;
  3030. #ifdef RS6000COFF_C
  3031. asection *tdata_sec = NULL;
  3032. asection *tbss_sec = NULL;
  3033. #endif
  3034. struct internal_filehdr internal_f;
  3035. struct internal_aouthdr internal_a;
  3036. #ifdef COFF_LONG_SECTION_NAMES
  3037. size_t string_size = STRING_SIZE_SIZE;
  3038. #endif
  3039. bfd_set_error (bfd_error_system_call);
  3040. /* Make a pass through the symbol table to count line number entries and
  3041. put them into the correct asections. */
  3042. lnno_size = coff_count_linenumbers (abfd) * bfd_coff_linesz (abfd);
  3043. if (! abfd->output_has_begun)
  3044. {
  3045. if (! coff_compute_section_file_positions (abfd))
  3046. return false;
  3047. }
  3048. reloc_base = obj_relocbase (abfd);
  3049. /* Work out the size of the reloc and linno areas. */
  3050. for (current = abfd->sections; current != NULL; current =
  3051. current->next)
  3052. {
  3053. #ifdef COFF_WITH_EXTENDED_RELOC_COUNTER
  3054. /* We store the actual reloc count in the first reloc's addr. */
  3055. if ((obj_pe (abfd) || obj_go32 (abfd)) && current->reloc_count >= 0xffff)
  3056. reloc_count ++;
  3057. #endif
  3058. reloc_count += current->reloc_count;
  3059. }
  3060. reloc_size = reloc_count * bfd_coff_relsz (abfd);
  3061. lineno_base = reloc_base + reloc_size;
  3062. sym_base = lineno_base + lnno_size;
  3063. /* Indicate in each section->line_filepos its actual file address. */
  3064. for (current = abfd->sections; current != NULL; current =
  3065. current->next)
  3066. {
  3067. if (current->lineno_count)
  3068. {
  3069. current->line_filepos = lineno_base;
  3070. current->moving_line_filepos = lineno_base;
  3071. lineno_base += current->lineno_count * bfd_coff_linesz (abfd);
  3072. }
  3073. else
  3074. current->line_filepos = 0;
  3075. if (current->reloc_count)
  3076. {
  3077. current->rel_filepos = reloc_base;
  3078. reloc_base += current->reloc_count * bfd_coff_relsz (abfd);
  3079. #ifdef COFF_WITH_EXTENDED_RELOC_COUNTER
  3080. /* Extra reloc to hold real count. */
  3081. if ((obj_pe (abfd) || obj_go32 (abfd)) && current->reloc_count >= 0xffff)
  3082. reloc_base += bfd_coff_relsz (abfd);
  3083. #endif
  3084. }
  3085. else
  3086. current->rel_filepos = 0;
  3087. }
  3088. /* Write section headers to the file. */
  3089. internal_f.f_nscns = 0;
  3090. if ((abfd->flags & EXEC_P) != 0)
  3091. scn_base = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
  3092. else
  3093. {
  3094. scn_base = bfd_coff_filhsz (abfd);
  3095. #ifdef RS6000COFF_C
  3096. #ifndef XCOFF64
  3097. if (xcoff_data (abfd)->full_aouthdr)
  3098. scn_base += bfd_coff_aoutsz (abfd);
  3099. else
  3100. scn_base += SMALL_AOUTSZ;
  3101. #endif
  3102. #endif
  3103. }
  3104. if (bfd_seek (abfd, scn_base, SEEK_SET) != 0)
  3105. return false;
  3106. long_section_names = false;
  3107. for (current = abfd->sections;
  3108. current != NULL;
  3109. current = current->next)
  3110. {
  3111. struct internal_scnhdr section;
  3112. #ifdef COFF_IMAGE_WITH_PE
  3113. bool is_reloc_section = false;
  3114. if (strcmp (current->name, DOT_RELOC) == 0)
  3115. {
  3116. is_reloc_section = true;
  3117. hasrelocs = true;
  3118. pe_data (abfd)->has_reloc_section = 1;
  3119. }
  3120. #endif
  3121. internal_f.f_nscns++;
  3122. strncpy (section.s_name, current->name, SCNNMLEN);
  3123. #ifdef COFF_LONG_SECTION_NAMES
  3124. /* Handle long section names as in PE. This must be compatible
  3125. with the code in coff_write_symbols and _bfd_coff_final_link. */
  3126. if (bfd_coff_long_section_names (abfd))
  3127. {
  3128. size_t len;
  3129. len = strlen (current->name);
  3130. if (len > SCNNMLEN)
  3131. {
  3132. /* The s_name field is defined to be NUL-padded but need not be
  3133. NUL-terminated. We use a temporary buffer so that we can still
  3134. sprintf all eight chars without splatting a terminating NUL
  3135. over the first byte of the following member (s_paddr). */
  3136. /* PR 21096: The +20 is to stop a bogus warning from gcc7 about
  3137. a possible buffer overflow. */
  3138. char s_name_buf[SCNNMLEN + 1 + 20];
  3139. /* An inherent limitation of the /nnnnnnn notation used to indicate
  3140. the offset of the long name in the string table is that we
  3141. cannot address entries beyone the ten million byte boundary. */
  3142. if (string_size >= 10000000)
  3143. {
  3144. bfd_set_error (bfd_error_file_too_big);
  3145. _bfd_error_handler
  3146. /* xgettext:c-format */
  3147. (_("%pB: section %pA: string table overflow at offset %ld"),
  3148. abfd, current, (unsigned long) string_size);
  3149. return false;
  3150. }
  3151. /* We do not need to use snprintf here as we have already verfied
  3152. that string_size is not too big, plus we have an overlarge
  3153. buffer, just in case. */
  3154. sprintf (s_name_buf, "/%lu", (unsigned long) string_size);
  3155. /* Then strncpy takes care of any padding for us. */
  3156. strncpy (section.s_name, s_name_buf, SCNNMLEN);
  3157. string_size += len + 1;
  3158. long_section_names = true;
  3159. }
  3160. }
  3161. #endif
  3162. #ifdef _LIB
  3163. /* Always set s_vaddr of .lib to 0. This is right for SVR3.2
  3164. Ian Taylor <ian@cygnus.com>. */
  3165. if (strcmp (current->name, _LIB) == 0)
  3166. section.s_vaddr = 0;
  3167. else
  3168. #endif
  3169. section.s_vaddr = current->vma;
  3170. section.s_paddr = current->lma;
  3171. section.s_size = current->size;
  3172. #ifdef coff_get_section_load_page
  3173. section.s_page = coff_get_section_load_page (current);
  3174. #else
  3175. section.s_page = 0;
  3176. #endif
  3177. #ifdef COFF_WITH_PE
  3178. section.s_paddr = 0;
  3179. #endif
  3180. #ifdef COFF_IMAGE_WITH_PE
  3181. /* Reminder: s_paddr holds the virtual size of the section. */
  3182. if (coff_section_data (abfd, current) != NULL
  3183. && pei_section_data (abfd, current) != NULL)
  3184. section.s_paddr = pei_section_data (abfd, current)->virt_size;
  3185. else
  3186. section.s_paddr = 0;
  3187. #endif
  3188. /* If this section has no size or is unloadable then the scnptr
  3189. will be 0 too. */
  3190. if (current->size == 0
  3191. || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
  3192. section.s_scnptr = 0;
  3193. else
  3194. section.s_scnptr = current->filepos;
  3195. section.s_relptr = current->rel_filepos;
  3196. section.s_lnnoptr = current->line_filepos;
  3197. section.s_nreloc = current->reloc_count;
  3198. section.s_nlnno = current->lineno_count;
  3199. #ifndef COFF_IMAGE_WITH_PE
  3200. /* In PEI, relocs come in the .reloc section. */
  3201. if (current->reloc_count != 0)
  3202. hasrelocs = true;
  3203. #endif
  3204. if (current->lineno_count != 0)
  3205. haslinno = true;
  3206. #ifdef COFF_IMAGE_WITH_PE
  3207. if ((current->flags & SEC_DEBUGGING) != 0
  3208. && ! is_reloc_section)
  3209. hasdebug = true;
  3210. #endif
  3211. #ifdef RS6000COFF_C
  3212. #ifndef XCOFF64
  3213. /* Indicate the use of an XCOFF overflow section header. */
  3214. if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
  3215. {
  3216. section.s_nreloc = 0xffff;
  3217. section.s_nlnno = 0xffff;
  3218. }
  3219. #endif
  3220. #endif
  3221. section.s_flags = sec_to_styp_flags (current->name, current->flags);
  3222. if (!strcmp (current->name, _TEXT))
  3223. text_sec = current;
  3224. else if (!strcmp (current->name, _DATA))
  3225. data_sec = current;
  3226. else if (!strcmp (current->name, _BSS))
  3227. bss_sec = current;
  3228. #ifdef RS6000COFF_C
  3229. else if (!strcmp (current->name, _TDATA))
  3230. tdata_sec = current;
  3231. else if (!strcmp (current->name, _TBSS))
  3232. tbss_sec = current;
  3233. #endif
  3234. #ifdef COFF_ENCODE_ALIGNMENT
  3235. if (COFF_ENCODE_ALIGNMENT (abfd, section, current->alignment_power)
  3236. && (COFF_DECODE_ALIGNMENT (section.s_flags)
  3237. != current->alignment_power))
  3238. {
  3239. bool warn = (coff_data (abfd)->link_info
  3240. && !bfd_link_relocatable (coff_data (abfd)->link_info));
  3241. _bfd_error_handler
  3242. /* xgettext:c-format */
  3243. (_("%pB:%s section %s: alignment 2**%u not representable"),
  3244. abfd, warn ? " warning:" : "", current->name,
  3245. current->alignment_power);
  3246. if (!warn)
  3247. {
  3248. bfd_set_error (bfd_error_nonrepresentable_section);
  3249. return false;
  3250. }
  3251. }
  3252. #endif
  3253. #ifdef COFF_IMAGE_WITH_PE
  3254. /* Suppress output of the sections if they are null. ld
  3255. includes the bss and data sections even if there is no size
  3256. assigned to them. NT loader doesn't like it if these section
  3257. headers are included if the sections themselves are not
  3258. needed. See also coff_compute_section_file_positions. */
  3259. if (section.s_size == 0)
  3260. internal_f.f_nscns--;
  3261. else
  3262. #endif
  3263. {
  3264. SCNHDR buff;
  3265. bfd_size_type amt = bfd_coff_scnhsz (abfd);
  3266. if (bfd_coff_swap_scnhdr_out (abfd, &section, &buff) == 0
  3267. || bfd_bwrite (& buff, amt, abfd) != amt)
  3268. return false;
  3269. }
  3270. #ifdef COFF_WITH_PE
  3271. /* PE stores COMDAT section information in the symbol table. If
  3272. this section is supposed to have some COMDAT info, track down
  3273. the symbol in the symbol table and modify it. */
  3274. if ((current->flags & SEC_LINK_ONCE) != 0)
  3275. {
  3276. unsigned int i, count;
  3277. asymbol **psym;
  3278. coff_symbol_type *csym = NULL;
  3279. asymbol **psymsec;
  3280. psymsec = NULL;
  3281. count = bfd_get_symcount (abfd);
  3282. for (i = 0, psym = abfd->outsymbols; i < count; i++, psym++)
  3283. {
  3284. if ((*psym)->section != current)
  3285. continue;
  3286. /* Remember the location of the first symbol in this
  3287. section. */
  3288. if (psymsec == NULL)
  3289. psymsec = psym;
  3290. /* See if this is the section symbol. */
  3291. if (strcmp ((*psym)->name, current->name) == 0)
  3292. {
  3293. csym = coff_symbol_from (*psym);
  3294. if (csym == NULL
  3295. || csym->native == NULL
  3296. || ! csym->native->is_sym
  3297. || csym->native->u.syment.n_numaux < 1
  3298. || csym->native->u.syment.n_sclass != C_STAT
  3299. || csym->native->u.syment.n_type != T_NULL)
  3300. continue;
  3301. /* Here *PSYM is the section symbol for CURRENT. */
  3302. break;
  3303. }
  3304. }
  3305. /* Did we find it?
  3306. Note that we might not if we're converting the file from
  3307. some other object file format. */
  3308. if (i < count)
  3309. {
  3310. combined_entry_type *aux;
  3311. /* We don't touch the x_checksum field. The
  3312. x_associated field is not currently supported. */
  3313. aux = csym->native + 1;
  3314. BFD_ASSERT (! aux->is_sym);
  3315. switch (current->flags & SEC_LINK_DUPLICATES)
  3316. {
  3317. case SEC_LINK_DUPLICATES_DISCARD:
  3318. aux->u.auxent.x_scn.x_comdat = IMAGE_COMDAT_SELECT_ANY;
  3319. break;
  3320. case SEC_LINK_DUPLICATES_ONE_ONLY:
  3321. aux->u.auxent.x_scn.x_comdat =
  3322. IMAGE_COMDAT_SELECT_NODUPLICATES;
  3323. break;
  3324. case SEC_LINK_DUPLICATES_SAME_SIZE:
  3325. aux->u.auxent.x_scn.x_comdat =
  3326. IMAGE_COMDAT_SELECT_SAME_SIZE;
  3327. break;
  3328. case SEC_LINK_DUPLICATES_SAME_CONTENTS:
  3329. aux->u.auxent.x_scn.x_comdat =
  3330. IMAGE_COMDAT_SELECT_EXACT_MATCH;
  3331. break;
  3332. }
  3333. /* The COMDAT symbol must be the first symbol from this
  3334. section in the symbol table. In order to make this
  3335. work, we move the COMDAT symbol before the first
  3336. symbol we found in the search above. It's OK to
  3337. rearrange the symbol table at this point, because
  3338. coff_renumber_symbols is going to rearrange it
  3339. further and fix up all the aux entries. */
  3340. if (psym != psymsec)
  3341. {
  3342. asymbol *hold;
  3343. asymbol **pcopy;
  3344. hold = *psym;
  3345. for (pcopy = psym; pcopy > psymsec; pcopy--)
  3346. pcopy[0] = pcopy[-1];
  3347. *psymsec = hold;
  3348. }
  3349. }
  3350. }
  3351. #endif /* COFF_WITH_PE */
  3352. }
  3353. #ifdef RS6000COFF_C
  3354. #ifndef XCOFF64
  3355. /* XCOFF handles overflows in the reloc and line number count fields
  3356. by creating a new section header to hold the correct values. */
  3357. for (current = abfd->sections; current != NULL; current = current->next)
  3358. {
  3359. if (current->reloc_count >= 0xffff || current->lineno_count >= 0xffff)
  3360. {
  3361. struct internal_scnhdr scnhdr;
  3362. SCNHDR buff;
  3363. bfd_size_type amt;
  3364. internal_f.f_nscns++;
  3365. memcpy (scnhdr.s_name, ".ovrflo", 8);
  3366. scnhdr.s_paddr = current->reloc_count;
  3367. scnhdr.s_vaddr = current->lineno_count;
  3368. scnhdr.s_size = 0;
  3369. scnhdr.s_scnptr = 0;
  3370. scnhdr.s_relptr = current->rel_filepos;
  3371. scnhdr.s_lnnoptr = current->line_filepos;
  3372. scnhdr.s_nreloc = current->target_index;
  3373. scnhdr.s_nlnno = current->target_index;
  3374. scnhdr.s_flags = STYP_OVRFLO;
  3375. amt = bfd_coff_scnhsz (abfd);
  3376. if (bfd_coff_swap_scnhdr_out (abfd, &scnhdr, &buff) == 0
  3377. || bfd_bwrite (& buff, amt, abfd) != amt)
  3378. return false;
  3379. }
  3380. }
  3381. #endif
  3382. #endif
  3383. #if defined (COFF_GO32_EXE) || defined (COFF_GO32)
  3384. /* Pad section headers. */
  3385. if ((abfd->flags & EXEC_P) && abfd->sections != NULL)
  3386. {
  3387. file_ptr cur_ptr = scn_base
  3388. + abfd->section_count * bfd_coff_scnhsz (abfd);
  3389. long fill_size = (abfd->sections->filepos - cur_ptr);
  3390. bfd_byte *b = bfd_zmalloc (fill_size);
  3391. if (b)
  3392. {
  3393. bfd_bwrite ((PTR)b, fill_size, abfd);
  3394. free (b);
  3395. }
  3396. }
  3397. #endif
  3398. /* OK, now set up the filehdr... */
  3399. /* Don't include the internal abs section in the section count */
  3400. /* We will NOT put a fucking timestamp in the header here. Every time you
  3401. put it back, I will come in and take it out again. I'm sorry. This
  3402. field does not belong here. We fill it with a 0 so it compares the
  3403. same but is not a reasonable time. -- gnu@cygnus.com */
  3404. internal_f.f_timdat = 0;
  3405. internal_f.f_flags = 0;
  3406. if (abfd->flags & EXEC_P)
  3407. internal_f.f_opthdr = bfd_coff_aoutsz (abfd);
  3408. else
  3409. {
  3410. internal_f.f_opthdr = 0;
  3411. #ifdef RS6000COFF_C
  3412. #ifndef XCOFF64
  3413. if (xcoff_data (abfd)->full_aouthdr)
  3414. internal_f.f_opthdr = bfd_coff_aoutsz (abfd);
  3415. else
  3416. internal_f.f_opthdr = SMALL_AOUTSZ;
  3417. #endif
  3418. #endif
  3419. }
  3420. if (!hasrelocs)
  3421. internal_f.f_flags |= F_RELFLG;
  3422. if (!haslinno)
  3423. internal_f.f_flags |= F_LNNO;
  3424. if (abfd->flags & EXEC_P)
  3425. internal_f.f_flags |= F_EXEC;
  3426. #ifdef COFF_IMAGE_WITH_PE
  3427. if (! hasdebug)
  3428. internal_f.f_flags |= IMAGE_FILE_DEBUG_STRIPPED;
  3429. if (pe_data (abfd)->real_flags & IMAGE_FILE_LARGE_ADDRESS_AWARE)
  3430. internal_f.f_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
  3431. #endif
  3432. #if !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
  3433. #ifdef COFF_WITH_PE
  3434. internal_f.f_flags |= IMAGE_FILE_32BIT_MACHINE;
  3435. #else
  3436. if (bfd_little_endian (abfd))
  3437. internal_f.f_flags |= F_AR32WR;
  3438. else
  3439. internal_f.f_flags |= F_AR32W;
  3440. #endif
  3441. #endif
  3442. #ifdef TI_TARGET_ID
  3443. /* Target id is used in TI COFF v1 and later; COFF0 won't use this field,
  3444. but it doesn't hurt to set it internally. */
  3445. internal_f.f_target_id = TI_TARGET_ID;
  3446. #endif
  3447. /* FIXME, should do something about the other byte orders and
  3448. architectures. */
  3449. #ifdef RS6000COFF_C
  3450. if ((abfd->flags & DYNAMIC) != 0)
  3451. internal_f.f_flags |= F_SHROBJ;
  3452. if (bfd_get_section_by_name (abfd, _LOADER) != NULL)
  3453. internal_f.f_flags |= F_DYNLOAD;
  3454. #endif
  3455. memset (&internal_a, 0, sizeof internal_a);
  3456. /* Set up architecture-dependent stuff. */
  3457. {
  3458. unsigned int magic = 0;
  3459. unsigned short flags = 0;
  3460. coff_set_flags (abfd, &magic, &flags);
  3461. internal_f.f_magic = magic;
  3462. internal_f.f_flags |= flags;
  3463. /* ...and the "opt"hdr... */
  3464. #ifdef TICOFF_AOUT_MAGIC
  3465. internal_a.magic = TICOFF_AOUT_MAGIC;
  3466. #define __A_MAGIC_SET__
  3467. #endif
  3468. #if defined(ARM)
  3469. #define __A_MAGIC_SET__
  3470. internal_a.magic = ZMAGIC;
  3471. #endif
  3472. #if defined(AARCH64)
  3473. #define __A_MAGIC_SET__
  3474. internal_a.magic = ZMAGIC;
  3475. #endif
  3476. #if defined MCORE_PE
  3477. #define __A_MAGIC_SET__
  3478. internal_a.magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
  3479. #endif
  3480. #if defined(I386)
  3481. #define __A_MAGIC_SET__
  3482. #if defined LYNXOS
  3483. internal_a.magic = LYNXCOFFMAGIC;
  3484. #elif defined AMD64
  3485. internal_a.magic = IMAGE_NT_OPTIONAL_HDR64_MAGIC;
  3486. #else
  3487. internal_a.magic = ZMAGIC;
  3488. #endif
  3489. #endif /* I386 */
  3490. #if defined(IA64)
  3491. #define __A_MAGIC_SET__
  3492. internal_a.magic = PE32PMAGIC;
  3493. #endif /* IA64 */
  3494. #if defined(SPARC)
  3495. #define __A_MAGIC_SET__
  3496. #if defined(LYNXOS)
  3497. internal_a.magic = LYNXCOFFMAGIC;
  3498. #endif /* LYNXOS */
  3499. #endif /* SPARC */
  3500. #ifdef RS6000COFF_C
  3501. #define __A_MAGIC_SET__
  3502. internal_a.magic = (abfd->flags & D_PAGED) ? RS6K_AOUTHDR_ZMAGIC :
  3503. (abfd->flags & WP_TEXT) ? RS6K_AOUTHDR_NMAGIC :
  3504. RS6K_AOUTHDR_OMAGIC;
  3505. #endif
  3506. #if defined(SH) && defined(COFF_WITH_PE)
  3507. #define __A_MAGIC_SET__
  3508. internal_a.magic = SH_PE_MAGIC;
  3509. #endif
  3510. #if defined(MIPS) && defined(COFF_WITH_PE)
  3511. #define __A_MAGIC_SET__
  3512. internal_a.magic = MIPS_PE_MAGIC;
  3513. #endif
  3514. #ifndef __A_MAGIC_SET__
  3515. #include "Your aouthdr magic number is not being set!"
  3516. #else
  3517. #undef __A_MAGIC_SET__
  3518. #endif
  3519. }
  3520. #ifdef RS6000COFF_C
  3521. /* XCOFF 32bit needs this to have new behaviour for n_type field. */
  3522. internal_a.vstamp = 2;
  3523. #else
  3524. /* FIXME: Does anybody ever set this to another value? */
  3525. internal_a.vstamp = 0;
  3526. #endif
  3527. /* Now should write relocs, strings, syms. */
  3528. obj_sym_filepos (abfd) = sym_base;
  3529. if (bfd_get_symcount (abfd) != 0)
  3530. {
  3531. int firstundef;
  3532. if (!coff_renumber_symbols (abfd, &firstundef))
  3533. return false;
  3534. coff_mangle_symbols (abfd);
  3535. if (! coff_write_symbols (abfd))
  3536. return false;
  3537. if (! coff_write_linenumbers (abfd))
  3538. return false;
  3539. if (! coff_write_relocs (abfd, firstundef))
  3540. return false;
  3541. }
  3542. #ifdef COFF_LONG_SECTION_NAMES
  3543. else if (long_section_names && ! obj_coff_strings_written (abfd))
  3544. {
  3545. /* If we have long section names we have to write out the string
  3546. table even if there are no symbols. */
  3547. if (! coff_write_symbols (abfd))
  3548. return false;
  3549. }
  3550. #endif
  3551. /* If bfd_get_symcount (abfd) != 0, then we are not using the COFF
  3552. backend linker, and obj_raw_syment_count is not valid until after
  3553. coff_write_symbols is called. */
  3554. if (obj_raw_syment_count (abfd) != 0)
  3555. {
  3556. internal_f.f_symptr = sym_base;
  3557. #ifdef RS6000COFF_C
  3558. /* AIX appears to require that F_RELFLG not be set if there are
  3559. local symbols but no relocations. */
  3560. internal_f.f_flags &=~ F_RELFLG;
  3561. #endif
  3562. }
  3563. else
  3564. {
  3565. if (long_section_names)
  3566. internal_f.f_symptr = sym_base;
  3567. else
  3568. internal_f.f_symptr = 0;
  3569. internal_f.f_flags |= F_LSYMS;
  3570. }
  3571. if (text_sec)
  3572. {
  3573. internal_a.tsize = text_sec->size;
  3574. internal_a.text_start = internal_a.tsize ? text_sec->vma : 0;
  3575. }
  3576. if (data_sec)
  3577. {
  3578. internal_a.dsize = data_sec->size;
  3579. internal_a.data_start = internal_a.dsize ? data_sec->vma : 0;
  3580. }
  3581. if (bss_sec)
  3582. {
  3583. internal_a.bsize = bss_sec->size;
  3584. if (internal_a.bsize && bss_sec->vma < internal_a.data_start)
  3585. internal_a.data_start = bss_sec->vma;
  3586. }
  3587. internal_a.entry = bfd_get_start_address (abfd);
  3588. internal_f.f_nsyms = obj_raw_syment_count (abfd);
  3589. #ifdef RS6000COFF_C
  3590. if (xcoff_data (abfd)->full_aouthdr)
  3591. {
  3592. bfd_vma toc;
  3593. asection *loader_sec;
  3594. internal_a.vstamp = 2;
  3595. internal_a.o_snentry = xcoff_data (abfd)->snentry;
  3596. if (internal_a.o_snentry == 0)
  3597. internal_a.entry = (bfd_vma) -1;
  3598. if (text_sec != NULL)
  3599. {
  3600. internal_a.o_sntext = text_sec->target_index;
  3601. internal_a.o_algntext = bfd_section_alignment (text_sec);
  3602. }
  3603. else
  3604. {
  3605. internal_a.o_sntext = 0;
  3606. internal_a.o_algntext = 0;
  3607. }
  3608. if (data_sec != NULL)
  3609. {
  3610. internal_a.o_sndata = data_sec->target_index;
  3611. internal_a.o_algndata = bfd_section_alignment (data_sec);
  3612. }
  3613. else
  3614. {
  3615. internal_a.o_sndata = 0;
  3616. internal_a.o_algndata = 0;
  3617. }
  3618. loader_sec = bfd_get_section_by_name (abfd, ".loader");
  3619. if (loader_sec != NULL)
  3620. internal_a.o_snloader = loader_sec->target_index;
  3621. else
  3622. internal_a.o_snloader = 0;
  3623. if (bss_sec != NULL)
  3624. internal_a.o_snbss = bss_sec->target_index;
  3625. else
  3626. internal_a.o_snbss = 0;
  3627. if (tdata_sec != NULL)
  3628. {
  3629. internal_a.o_sntdata = tdata_sec->target_index;
  3630. /* TODO: o_flags should be set to RS6K_AOUTHDR_TLS_LE
  3631. if there is at least one R_TLS_LE relocations. */
  3632. internal_a.o_flags = 0;
  3633. #ifdef XCOFF64
  3634. internal_a.o_x64flags = 0;
  3635. #endif
  3636. }
  3637. else
  3638. {
  3639. internal_a.o_sntdata = 0;
  3640. internal_a.o_flags = 0;
  3641. #ifdef XCOFF64
  3642. internal_a.o_x64flags = 0;
  3643. #endif
  3644. }
  3645. if (tbss_sec != NULL)
  3646. internal_a.o_sntbss = tbss_sec->target_index;
  3647. else
  3648. internal_a.o_sntbss = 0;
  3649. toc = xcoff_data (abfd)->toc;
  3650. internal_a.o_toc = toc;
  3651. internal_a.o_sntoc = xcoff_data (abfd)->sntoc;
  3652. internal_a.o_modtype = xcoff_data (abfd)->modtype;
  3653. if (xcoff_data (abfd)->cputype != -1)
  3654. internal_a.o_cputype = xcoff_data (abfd)->cputype;
  3655. else
  3656. {
  3657. switch (bfd_get_arch (abfd))
  3658. {
  3659. case bfd_arch_rs6000:
  3660. internal_a.o_cputype = 4;
  3661. break;
  3662. case bfd_arch_powerpc:
  3663. if (bfd_get_mach (abfd) == bfd_mach_ppc)
  3664. internal_a.o_cputype = 3;
  3665. else if (bfd_get_mach (abfd) == bfd_mach_ppc_620)
  3666. internal_a.o_cputype = 2;
  3667. else
  3668. internal_a.o_cputype = 1;
  3669. break;
  3670. default:
  3671. abort ();
  3672. }
  3673. }
  3674. internal_a.o_maxstack = xcoff_data (abfd)->maxstack;
  3675. internal_a.o_maxdata = xcoff_data (abfd)->maxdata;
  3676. }
  3677. #endif
  3678. #ifdef COFF_WITH_PE
  3679. {
  3680. /* After object contents are finalized so we can compute a reasonable hash,
  3681. but before header is written so we can update it to point to debug directory. */
  3682. struct pe_tdata *pe = pe_data (abfd);
  3683. if (pe->build_id.after_write_object_contents != NULL)
  3684. (*pe->build_id.after_write_object_contents) (abfd);
  3685. }
  3686. #endif
  3687. /* Now write header. */
  3688. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  3689. return false;
  3690. {
  3691. char * buff;
  3692. bfd_size_type amount = bfd_coff_filhsz (abfd);
  3693. buff = (char *) bfd_malloc (amount);
  3694. if (buff == NULL)
  3695. return false;
  3696. bfd_coff_swap_filehdr_out (abfd, & internal_f, buff);
  3697. amount = bfd_bwrite (buff, amount, abfd);
  3698. free (buff);
  3699. if (amount != bfd_coff_filhsz (abfd))
  3700. return false;
  3701. }
  3702. if (abfd->flags & EXEC_P)
  3703. {
  3704. /* Note that peicode.h fills in a PEAOUTHDR, not an AOUTHDR.
  3705. include/coff/pe.h sets AOUTSZ == sizeof (PEAOUTHDR)). */
  3706. char * buff;
  3707. bfd_size_type amount = bfd_coff_aoutsz (abfd);
  3708. buff = (char *) bfd_malloc (amount);
  3709. if (buff == NULL)
  3710. return false;
  3711. coff_swap_aouthdr_out (abfd, & internal_a, buff);
  3712. amount = bfd_bwrite (buff, amount, abfd);
  3713. free (buff);
  3714. if (amount != bfd_coff_aoutsz (abfd))
  3715. return false;
  3716. #ifdef COFF_IMAGE_WITH_PE
  3717. if (! coff_apply_checksum (abfd))
  3718. return false;
  3719. #endif
  3720. }
  3721. #ifdef RS6000COFF_C
  3722. #ifndef XCOFF64
  3723. else
  3724. {
  3725. AOUTHDR buff;
  3726. size_t size;
  3727. /* XCOFF32 seems to always write at least a small a.out header. */
  3728. coff_swap_aouthdr_out (abfd, & internal_a, & buff);
  3729. if (xcoff_data (abfd)->full_aouthdr)
  3730. size = bfd_coff_aoutsz (abfd);
  3731. else
  3732. size = SMALL_AOUTSZ;
  3733. if (bfd_bwrite (& buff, (bfd_size_type) size, abfd) != size)
  3734. return false;
  3735. }
  3736. #endif
  3737. #endif
  3738. return true;
  3739. }
  3740. static bool
  3741. coff_set_section_contents (bfd * abfd,
  3742. sec_ptr section,
  3743. const void * location,
  3744. file_ptr offset,
  3745. bfd_size_type count)
  3746. {
  3747. if (! abfd->output_has_begun) /* Set by bfd.c handler. */
  3748. {
  3749. if (! coff_compute_section_file_positions (abfd))
  3750. return false;
  3751. }
  3752. #if defined(_LIB) && !defined(TARG_AUX)
  3753. /* The physical address field of a .lib section is used to hold the
  3754. number of shared libraries in the section. This code counts the
  3755. number of sections being written, and increments the lma field
  3756. with the number.
  3757. I have found no documentation on the contents of this section.
  3758. Experimentation indicates that the section contains zero or more
  3759. records, each of which has the following structure:
  3760. - a (four byte) word holding the length of this record, in words,
  3761. - a word that always seems to be set to "2",
  3762. - the path to a shared library, null-terminated and then padded
  3763. to a whole word boundary.
  3764. bfd_assert calls have been added to alert if an attempt is made
  3765. to write a section which doesn't follow these assumptions. The
  3766. code has been tested on ISC 4.1 by me, and on SCO by Robert Lipe
  3767. <robertl@arnet.com> (Thanks!).
  3768. Gvran Uddeborg <gvran@uddeborg.pp.se>. */
  3769. if (strcmp (section->name, _LIB) == 0)
  3770. {
  3771. bfd_byte *rec, *recend;
  3772. rec = (bfd_byte *) location;
  3773. recend = rec + count;
  3774. while (rec < recend)
  3775. {
  3776. ++section->lma;
  3777. rec += bfd_get_32 (abfd, rec) * 4;
  3778. }
  3779. BFD_ASSERT (rec == recend);
  3780. }
  3781. #endif
  3782. /* Don't write out bss sections - one way to do this is to
  3783. see if the filepos has not been set. */
  3784. if (section->filepos == 0)
  3785. return true;
  3786. if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
  3787. return false;
  3788. if (count == 0)
  3789. return true;
  3790. return bfd_bwrite (location, count, abfd) == count;
  3791. }
  3792. static void *
  3793. buy_and_read (bfd *abfd, file_ptr where,
  3794. bfd_size_type nmemb, bfd_size_type size)
  3795. {
  3796. size_t amt;
  3797. if (_bfd_mul_overflow (nmemb, size, &amt))
  3798. {
  3799. bfd_set_error (bfd_error_file_too_big);
  3800. return NULL;
  3801. }
  3802. if (bfd_seek (abfd, where, SEEK_SET) != 0)
  3803. return NULL;
  3804. return _bfd_malloc_and_read (abfd, amt, amt);
  3805. }
  3806. /*
  3807. SUBSUBSECTION
  3808. Reading linenumbers
  3809. Creating the linenumber table is done by reading in the entire
  3810. coff linenumber table, and creating another table for internal use.
  3811. A coff linenumber table is structured so that each function
  3812. is marked as having a line number of 0. Each line within the
  3813. function is an offset from the first line in the function. The
  3814. base of the line number information for the table is stored in
  3815. the symbol associated with the function.
  3816. Note: The PE format uses line number 0 for a flag indicating a
  3817. new source file.
  3818. The information is copied from the external to the internal
  3819. table, and each symbol which marks a function is marked by
  3820. pointing its...
  3821. How does this work ?
  3822. */
  3823. static int
  3824. coff_sort_func_alent (const void * arg1, const void * arg2)
  3825. {
  3826. const alent *al1 = *(const alent **) arg1;
  3827. const alent *al2 = *(const alent **) arg2;
  3828. const coff_symbol_type *s1 = (const coff_symbol_type *) (al1->u.sym);
  3829. const coff_symbol_type *s2 = (const coff_symbol_type *) (al2->u.sym);
  3830. if (s1 == NULL || s2 == NULL)
  3831. return 0;
  3832. if (s1->symbol.value < s2->symbol.value)
  3833. return -1;
  3834. else if (s1->symbol.value > s2->symbol.value)
  3835. return 1;
  3836. return 0;
  3837. }
  3838. static bool
  3839. coff_slurp_line_table (bfd *abfd, asection *asect)
  3840. {
  3841. LINENO *native_lineno;
  3842. alent *lineno_cache;
  3843. unsigned int counter;
  3844. alent *cache_ptr;
  3845. bfd_vma prev_offset = 0;
  3846. bool ordered = true;
  3847. unsigned int nbr_func;
  3848. LINENO *src;
  3849. bool have_func;
  3850. bool ret = true;
  3851. size_t amt;
  3852. if (asect->lineno_count == 0)
  3853. return true;
  3854. BFD_ASSERT (asect->lineno == NULL);
  3855. native_lineno = (LINENO *) buy_and_read (abfd, asect->line_filepos,
  3856. asect->lineno_count,
  3857. bfd_coff_linesz (abfd));
  3858. if (native_lineno == NULL)
  3859. {
  3860. _bfd_error_handler
  3861. (_("%pB: warning: line number table read failed"), abfd);
  3862. return false;
  3863. }
  3864. if (_bfd_mul_overflow (asect->lineno_count + 1, sizeof (alent), &amt))
  3865. {
  3866. bfd_set_error (bfd_error_file_too_big);
  3867. free (native_lineno);
  3868. return false;
  3869. }
  3870. lineno_cache = (alent *) bfd_alloc (abfd, amt);
  3871. if (lineno_cache == NULL)
  3872. {
  3873. free (native_lineno);
  3874. return false;
  3875. }
  3876. cache_ptr = lineno_cache;
  3877. asect->lineno = lineno_cache;
  3878. src = native_lineno;
  3879. nbr_func = 0;
  3880. have_func = false;
  3881. for (counter = 0; counter < asect->lineno_count; counter++, src++)
  3882. {
  3883. struct internal_lineno dst;
  3884. bfd_coff_swap_lineno_in (abfd, src, &dst);
  3885. cache_ptr->line_number = dst.l_lnno;
  3886. /* Appease memory checkers that get all excited about
  3887. uninitialised memory when copying alents if u.offset is
  3888. larger than u.sym. (64-bit BFD on 32-bit host.) */
  3889. memset (&cache_ptr->u, 0, sizeof (cache_ptr->u));
  3890. if (cache_ptr->line_number == 0)
  3891. {
  3892. combined_entry_type * ent;
  3893. unsigned long symndx;
  3894. coff_symbol_type *sym;
  3895. have_func = false;
  3896. symndx = dst.l_addr.l_symndx;
  3897. if (symndx >= obj_raw_syment_count (abfd))
  3898. {
  3899. _bfd_error_handler
  3900. /* xgettext:c-format */
  3901. (_("%pB: warning: illegal symbol index 0x%lx in line number entry %d"),
  3902. abfd, symndx, counter);
  3903. cache_ptr->line_number = -1;
  3904. ret = false;
  3905. continue;
  3906. }
  3907. ent = obj_raw_syments (abfd) + symndx;
  3908. /* FIXME: We should not be casting between ints and
  3909. pointers like this. */
  3910. if (! ent->is_sym)
  3911. {
  3912. _bfd_error_handler
  3913. /* xgettext:c-format */
  3914. (_("%pB: warning: illegal symbol index 0x%lx in line number entry %d"),
  3915. abfd, symndx, counter);
  3916. cache_ptr->line_number = -1;
  3917. ret = false;
  3918. continue;
  3919. }
  3920. sym = (coff_symbol_type *) (ent->u.syment._n._n_n._n_zeroes);
  3921. /* PR 17512 file: 078-10659-0.004 */
  3922. if (sym < obj_symbols (abfd)
  3923. || sym >= obj_symbols (abfd) + bfd_get_symcount (abfd))
  3924. {
  3925. _bfd_error_handler
  3926. /* xgettext:c-format */
  3927. (_("%pB: warning: illegal symbol in line number entry %d"),
  3928. abfd, counter);
  3929. cache_ptr->line_number = -1;
  3930. ret = false;
  3931. continue;
  3932. }
  3933. have_func = true;
  3934. nbr_func++;
  3935. cache_ptr->u.sym = (asymbol *) sym;
  3936. if (sym->lineno != NULL)
  3937. _bfd_error_handler
  3938. /* xgettext:c-format */
  3939. (_("%pB: warning: duplicate line number information for `%s'"),
  3940. abfd, bfd_asymbol_name (&sym->symbol));
  3941. sym->lineno = cache_ptr;
  3942. if (sym->symbol.value < prev_offset)
  3943. ordered = false;
  3944. prev_offset = sym->symbol.value;
  3945. }
  3946. else if (!have_func)
  3947. /* Drop line information that has no associated function.
  3948. PR 17521: file: 078-10659-0.004. */
  3949. continue;
  3950. else
  3951. cache_ptr->u.offset = dst.l_addr.l_paddr - bfd_section_vma (asect);
  3952. cache_ptr++;
  3953. }
  3954. asect->lineno_count = cache_ptr - lineno_cache;
  3955. memset (cache_ptr, 0, sizeof (*cache_ptr));
  3956. free (native_lineno);
  3957. /* On some systems (eg AIX5.3) the lineno table may not be sorted. */
  3958. if (!ordered)
  3959. {
  3960. /* Sort the table. */
  3961. alent **func_table;
  3962. alent *n_lineno_cache;
  3963. /* Create a table of functions. */
  3964. if (_bfd_mul_overflow (nbr_func, sizeof (alent *), &amt))
  3965. {
  3966. bfd_set_error (bfd_error_file_too_big);
  3967. ret = false;
  3968. }
  3969. else if ((func_table = (alent **) bfd_alloc (abfd, amt)) != NULL)
  3970. {
  3971. alent **p = func_table;
  3972. unsigned int i;
  3973. for (i = 0; i < asect->lineno_count; i++)
  3974. if (lineno_cache[i].line_number == 0)
  3975. *p++ = &lineno_cache[i];
  3976. BFD_ASSERT ((unsigned int) (p - func_table) == nbr_func);
  3977. /* Sort by functions. */
  3978. qsort (func_table, nbr_func, sizeof (alent *), coff_sort_func_alent);
  3979. /* Create the new sorted table. */
  3980. if (_bfd_mul_overflow (asect->lineno_count, sizeof (alent), &amt))
  3981. {
  3982. bfd_set_error (bfd_error_file_too_big);
  3983. ret = false;
  3984. }
  3985. else if ((n_lineno_cache = (alent *) bfd_alloc (abfd, amt)) != NULL)
  3986. {
  3987. alent *n_cache_ptr = n_lineno_cache;
  3988. for (i = 0; i < nbr_func; i++)
  3989. {
  3990. coff_symbol_type *sym;
  3991. alent *old_ptr = func_table[i];
  3992. /* Update the function entry. */
  3993. sym = (coff_symbol_type *) old_ptr->u.sym;
  3994. /* PR binutils/17512: Point the lineno to where
  3995. this entry will be after the memcpy below. */
  3996. sym->lineno = lineno_cache + (n_cache_ptr - n_lineno_cache);
  3997. /* Copy the function and line number entries. */
  3998. do
  3999. *n_cache_ptr++ = *old_ptr++;
  4000. while (old_ptr->line_number != 0);
  4001. }
  4002. memcpy (lineno_cache, n_lineno_cache,
  4003. asect->lineno_count * sizeof (alent));
  4004. }
  4005. else
  4006. ret = false;
  4007. bfd_release (abfd, func_table);
  4008. }
  4009. else
  4010. ret = false;
  4011. }
  4012. return ret;
  4013. }
  4014. /* Slurp in the symbol table, converting it to generic form. Note
  4015. that if coff_relocate_section is defined, the linker will read
  4016. symbols via coff_link_add_symbols, rather than via this routine. */
  4017. static bool
  4018. coff_slurp_symbol_table (bfd * abfd)
  4019. {
  4020. combined_entry_type *native_symbols;
  4021. coff_symbol_type *cached_area;
  4022. unsigned int *table_ptr;
  4023. unsigned int number_of_symbols = 0;
  4024. bool ret = true;
  4025. size_t amt;
  4026. if (obj_symbols (abfd))
  4027. return true;
  4028. /* Read in the symbol table. */
  4029. if ((native_symbols = coff_get_normalized_symtab (abfd)) == NULL)
  4030. return false;
  4031. /* Allocate enough room for all the symbols in cached form. */
  4032. if (_bfd_mul_overflow (obj_raw_syment_count (abfd),
  4033. sizeof (*cached_area), &amt))
  4034. {
  4035. bfd_set_error (bfd_error_file_too_big);
  4036. return false;
  4037. }
  4038. cached_area = (coff_symbol_type *) bfd_alloc (abfd, amt);
  4039. if (cached_area == NULL)
  4040. return false;
  4041. if (_bfd_mul_overflow (obj_raw_syment_count (abfd),
  4042. sizeof (*table_ptr), &amt))
  4043. {
  4044. bfd_set_error (bfd_error_file_too_big);
  4045. return false;
  4046. }
  4047. table_ptr = (unsigned int *) bfd_zalloc (abfd, amt);
  4048. if (table_ptr == NULL)
  4049. return false;
  4050. else
  4051. {
  4052. coff_symbol_type *dst = cached_area;
  4053. unsigned int last_native_index = obj_raw_syment_count (abfd);
  4054. unsigned int this_index = 0;
  4055. while (this_index < last_native_index)
  4056. {
  4057. combined_entry_type *src = native_symbols + this_index;
  4058. table_ptr[this_index] = number_of_symbols;
  4059. dst->symbol.the_bfd = abfd;
  4060. BFD_ASSERT (src->is_sym);
  4061. dst->symbol.name = (char *) (src->u.syment._n._n_n._n_offset);
  4062. /* We use the native name field to point to the cached field. */
  4063. src->u.syment._n._n_n._n_zeroes = (bfd_hostptr_t) dst;
  4064. dst->symbol.section = coff_section_from_bfd_index (abfd,
  4065. src->u.syment.n_scnum);
  4066. dst->symbol.flags = 0;
  4067. /* PR 17512: file: 079-7098-0.001:0.1. */
  4068. dst->symbol.value = 0;
  4069. dst->done_lineno = false;
  4070. switch (src->u.syment.n_sclass)
  4071. {
  4072. case C_EXT:
  4073. case C_WEAKEXT:
  4074. #if defined ARM
  4075. case C_THUMBEXT:
  4076. case C_THUMBEXTFUNC:
  4077. #endif
  4078. #ifdef RS6000COFF_C
  4079. case C_HIDEXT:
  4080. #if ! defined _AIX52 && ! defined AIX_WEAK_SUPPORT
  4081. case C_AIX_WEAKEXT:
  4082. #endif
  4083. #endif
  4084. #ifdef C_SYSTEM
  4085. case C_SYSTEM: /* System Wide variable. */
  4086. #endif
  4087. #ifdef COFF_WITH_PE
  4088. /* In PE, 0x68 (104) denotes a section symbol. */
  4089. case C_SECTION:
  4090. /* In PE, 0x69 (105) denotes a weak external symbol. */
  4091. case C_NT_WEAK:
  4092. #endif
  4093. switch (coff_classify_symbol (abfd, &src->u.syment))
  4094. {
  4095. case COFF_SYMBOL_GLOBAL:
  4096. dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
  4097. #if defined COFF_WITH_PE
  4098. /* PE sets the symbol to a value relative to the
  4099. start of the section. */
  4100. dst->symbol.value = src->u.syment.n_value;
  4101. #else
  4102. dst->symbol.value = (src->u.syment.n_value
  4103. - dst->symbol.section->vma);
  4104. #endif
  4105. if (ISFCN ((src->u.syment.n_type)))
  4106. /* A function ext does not go at the end of a
  4107. file. */
  4108. dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
  4109. break;
  4110. case COFF_SYMBOL_COMMON:
  4111. dst->symbol.section = bfd_com_section_ptr;
  4112. dst->symbol.value = src->u.syment.n_value;
  4113. break;
  4114. case COFF_SYMBOL_UNDEFINED:
  4115. dst->symbol.section = bfd_und_section_ptr;
  4116. dst->symbol.value = 0;
  4117. break;
  4118. case COFF_SYMBOL_PE_SECTION:
  4119. dst->symbol.flags |= BSF_EXPORT | BSF_SECTION_SYM;
  4120. dst->symbol.value = 0;
  4121. break;
  4122. case COFF_SYMBOL_LOCAL:
  4123. dst->symbol.flags = BSF_LOCAL;
  4124. #if defined COFF_WITH_PE
  4125. /* PE sets the symbol to a value relative to the
  4126. start of the section. */
  4127. dst->symbol.value = src->u.syment.n_value;
  4128. #else
  4129. dst->symbol.value = (src->u.syment.n_value
  4130. - dst->symbol.section->vma);
  4131. #endif
  4132. if (ISFCN ((src->u.syment.n_type)))
  4133. dst->symbol.flags |= BSF_NOT_AT_END | BSF_FUNCTION;
  4134. break;
  4135. }
  4136. #ifdef RS6000COFF_C
  4137. /* A symbol with a csect entry should not go at the end. */
  4138. if (src->u.syment.n_numaux > 0)
  4139. dst->symbol.flags |= BSF_NOT_AT_END;
  4140. #endif
  4141. #ifdef COFF_WITH_PE
  4142. if (src->u.syment.n_sclass == C_NT_WEAK)
  4143. dst->symbol.flags |= BSF_WEAK;
  4144. if (src->u.syment.n_sclass == C_SECTION
  4145. && src->u.syment.n_scnum > 0)
  4146. dst->symbol.flags = BSF_LOCAL;
  4147. #endif
  4148. if (src->u.syment.n_sclass == C_WEAKEXT
  4149. #ifdef RS6000COFF_C
  4150. || src->u.syment.n_sclass == C_AIX_WEAKEXT
  4151. #endif
  4152. )
  4153. dst->symbol.flags |= BSF_WEAK;
  4154. break;
  4155. case C_STAT: /* Static. */
  4156. #if defined ARM
  4157. case C_THUMBSTAT: /* Thumb static. */
  4158. case C_THUMBLABEL: /* Thumb label. */
  4159. case C_THUMBSTATFUNC:/* Thumb static function. */
  4160. #endif
  4161. #ifdef RS6000COFF_C
  4162. case C_DWARF: /* A label in a dwarf section. */
  4163. case C_INFO: /* A label in a comment section. */
  4164. #endif
  4165. case C_LABEL: /* Label. */
  4166. if (src->u.syment.n_scnum == N_DEBUG)
  4167. dst->symbol.flags = BSF_DEBUGGING;
  4168. else
  4169. dst->symbol.flags = BSF_LOCAL;
  4170. /* Base the value as an index from the base of the
  4171. section, if there is one. */
  4172. if (dst->symbol.section)
  4173. {
  4174. #if defined COFF_WITH_PE
  4175. /* PE sets the symbol to a value relative to the
  4176. start of the section. */
  4177. dst->symbol.value = src->u.syment.n_value;
  4178. #else
  4179. dst->symbol.value = (src->u.syment.n_value
  4180. - dst->symbol.section->vma);
  4181. #endif
  4182. }
  4183. else
  4184. dst->symbol.value = src->u.syment.n_value;
  4185. break;
  4186. case C_MOS: /* Member of structure. */
  4187. case C_EOS: /* End of structure. */
  4188. case C_REGPARM: /* Register parameter. */
  4189. case C_REG: /* register variable. */
  4190. /* C_AUTOARG conflicts with TI COFF C_UEXT. */
  4191. case C_TPDEF: /* Type definition. */
  4192. case C_ARG:
  4193. case C_AUTO: /* Automatic variable. */
  4194. case C_FIELD: /* Bit field. */
  4195. case C_ENTAG: /* Enumeration tag. */
  4196. case C_MOE: /* Member of enumeration. */
  4197. case C_MOU: /* Member of union. */
  4198. case C_UNTAG: /* Union tag. */
  4199. dst->symbol.flags = BSF_DEBUGGING;
  4200. dst->symbol.value = (src->u.syment.n_value);
  4201. break;
  4202. case C_FILE: /* File name. */
  4203. case C_STRTAG: /* Structure tag. */
  4204. #ifdef RS6000COFF_C
  4205. case C_GSYM:
  4206. case C_LSYM:
  4207. case C_PSYM:
  4208. case C_RSYM:
  4209. case C_RPSYM:
  4210. case C_STSYM:
  4211. case C_TCSYM:
  4212. case C_BCOMM:
  4213. case C_ECOML:
  4214. case C_ECOMM:
  4215. case C_DECL:
  4216. case C_ENTRY:
  4217. case C_FUN:
  4218. case C_ESTAT:
  4219. #endif
  4220. dst->symbol.flags = BSF_DEBUGGING;
  4221. dst->symbol.value = (src->u.syment.n_value);
  4222. break;
  4223. #ifdef RS6000COFF_C
  4224. case C_BINCL: /* Beginning of include file. */
  4225. case C_EINCL: /* Ending of include file. */
  4226. /* The value is actually a pointer into the line numbers
  4227. of the file. We locate the line number entry, and
  4228. set the section to the section which contains it, and
  4229. the value to the index in that section. */
  4230. {
  4231. asection *sec;
  4232. dst->symbol.flags = BSF_DEBUGGING;
  4233. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  4234. if (sec->line_filepos <= (file_ptr) src->u.syment.n_value
  4235. && ((file_ptr) (sec->line_filepos
  4236. + sec->lineno_count * bfd_coff_linesz (abfd))
  4237. > (file_ptr) src->u.syment.n_value))
  4238. break;
  4239. if (sec == NULL)
  4240. dst->symbol.value = 0;
  4241. else
  4242. {
  4243. dst->symbol.section = sec;
  4244. dst->symbol.value = ((src->u.syment.n_value
  4245. - sec->line_filepos)
  4246. / bfd_coff_linesz (abfd));
  4247. src->fix_line = 1;
  4248. }
  4249. }
  4250. break;
  4251. case C_BSTAT:
  4252. dst->symbol.flags = BSF_DEBUGGING;
  4253. /* The value is actually a symbol index. Save a pointer
  4254. to the symbol instead of the index. FIXME: This
  4255. should use a union. */
  4256. src->u.syment.n_value
  4257. = (bfd_hostptr_t) (native_symbols + src->u.syment.n_value);
  4258. dst->symbol.value = src->u.syment.n_value;
  4259. src->fix_value = 1;
  4260. break;
  4261. #endif
  4262. case C_BLOCK: /* ".bb" or ".eb". */
  4263. case C_FCN: /* ".bf" or ".ef" (or PE ".lf"). */
  4264. case C_EFCN: /* Physical end of function. */
  4265. #if defined COFF_WITH_PE
  4266. /* PE sets the symbol to a value relative to the start
  4267. of the section. */
  4268. dst->symbol.value = src->u.syment.n_value;
  4269. if (strcmp (dst->symbol.name, ".bf") != 0)
  4270. {
  4271. /* PE uses funny values for .ef and .lf; don't
  4272. relocate them. */
  4273. dst->symbol.flags = BSF_DEBUGGING;
  4274. }
  4275. else
  4276. dst->symbol.flags = BSF_DEBUGGING | BSF_DEBUGGING_RELOC;
  4277. #else
  4278. /* Base the value as an index from the base of the
  4279. section. */
  4280. dst->symbol.flags = BSF_LOCAL;
  4281. dst->symbol.value = (src->u.syment.n_value
  4282. - dst->symbol.section->vma);
  4283. #endif
  4284. break;
  4285. case C_STATLAB: /* Static load time label. */
  4286. dst->symbol.value = src->u.syment.n_value;
  4287. dst->symbol.flags = BSF_GLOBAL;
  4288. break;
  4289. case C_NULL:
  4290. /* PE DLLs sometimes have zeroed out symbols for some
  4291. reason. Just ignore them without a warning. */
  4292. if (src->u.syment.n_type == 0
  4293. && src->u.syment.n_value == 0
  4294. && src->u.syment.n_scnum == 0)
  4295. break;
  4296. #ifdef RS6000COFF_C
  4297. /* XCOFF specific: deleted entry. */
  4298. if (src->u.syment.n_value == C_NULL_VALUE)
  4299. break;
  4300. #endif
  4301. /* Fall through. */
  4302. case C_EXTDEF: /* External definition. */
  4303. case C_ULABEL: /* Undefined label. */
  4304. case C_USTATIC: /* Undefined static. */
  4305. #ifndef COFF_WITH_PE
  4306. /* C_LINE in regular coff is 0x68. NT has taken over this storage
  4307. class to represent a section symbol. */
  4308. case C_LINE: /* line # reformatted as symbol table entry. */
  4309. /* NT uses 0x67 for a weak symbol, not C_ALIAS. */
  4310. case C_ALIAS: /* Duplicate tag. */
  4311. #endif
  4312. /* New storage classes for TI COFF. */
  4313. #ifdef TICOFF
  4314. case C_UEXT: /* Tentative external definition. */
  4315. #endif
  4316. case C_EXTLAB: /* External load time label. */
  4317. default:
  4318. _bfd_error_handler
  4319. /* xgettext:c-format */
  4320. (_("%pB: unrecognized storage class %d for %s symbol `%s'"),
  4321. abfd, src->u.syment.n_sclass,
  4322. dst->symbol.section->name, dst->symbol.name);
  4323. ret = false;
  4324. /* Fall through. */
  4325. case C_HIDDEN: /* Ext symbol in dmert public lib. */
  4326. /* PR 20722: These symbols can also be generated by
  4327. building DLLs with --gc-sections enabled. */
  4328. dst->symbol.flags = BSF_DEBUGGING;
  4329. dst->symbol.value = (src->u.syment.n_value);
  4330. break;
  4331. }
  4332. dst->native = src;
  4333. dst->symbol.udata.i = 0;
  4334. dst->lineno = NULL;
  4335. this_index += (src->u.syment.n_numaux) + 1;
  4336. dst++;
  4337. number_of_symbols++;
  4338. }
  4339. }
  4340. obj_symbols (abfd) = cached_area;
  4341. obj_raw_syments (abfd) = native_symbols;
  4342. abfd->symcount = number_of_symbols;
  4343. obj_convert (abfd) = table_ptr;
  4344. /* Slurp the line tables for each section too. */
  4345. {
  4346. asection *p;
  4347. p = abfd->sections;
  4348. while (p)
  4349. {
  4350. if (! coff_slurp_line_table (abfd, p))
  4351. return false;
  4352. p = p->next;
  4353. }
  4354. }
  4355. return ret;
  4356. }
  4357. /* Classify a COFF symbol. A couple of targets have globally visible
  4358. symbols which are not class C_EXT, and this handles those. It also
  4359. recognizes some special PE cases. */
  4360. static enum coff_symbol_classification
  4361. coff_classify_symbol (bfd *abfd,
  4362. struct internal_syment *syment)
  4363. {
  4364. /* FIXME: This partially duplicates the switch in
  4365. coff_slurp_symbol_table. */
  4366. switch (syment->n_sclass)
  4367. {
  4368. case C_EXT:
  4369. case C_WEAKEXT:
  4370. #ifdef ARM
  4371. case C_THUMBEXT:
  4372. case C_THUMBEXTFUNC:
  4373. #endif
  4374. #ifdef RS6000COFF_C
  4375. case C_HIDEXT:
  4376. #if ! defined _AIX52 && ! defined AIX_WEAK_SUPPORT
  4377. case C_AIX_WEAKEXT:
  4378. #endif
  4379. #endif
  4380. #ifdef C_SYSTEM
  4381. case C_SYSTEM:
  4382. #endif
  4383. #ifdef COFF_WITH_PE
  4384. case C_NT_WEAK:
  4385. #endif
  4386. if (syment->n_scnum == 0)
  4387. {
  4388. if (syment->n_value == 0)
  4389. return COFF_SYMBOL_UNDEFINED;
  4390. else
  4391. return COFF_SYMBOL_COMMON;
  4392. }
  4393. #ifdef RS6000COFF_C
  4394. if (syment->n_sclass == C_HIDEXT)
  4395. return COFF_SYMBOL_LOCAL;
  4396. #endif
  4397. return COFF_SYMBOL_GLOBAL;
  4398. default:
  4399. break;
  4400. }
  4401. #ifdef COFF_WITH_PE
  4402. if (syment->n_sclass == C_STAT)
  4403. {
  4404. if (syment->n_scnum == 0)
  4405. /* The Microsoft compiler sometimes generates these if a
  4406. small static function is inlined every time it is used.
  4407. The function is discarded, but the symbol table entry
  4408. remains. */
  4409. return COFF_SYMBOL_LOCAL;
  4410. #ifdef STRICT_PE_FORMAT
  4411. /* This is correct for Microsoft generated objects, but it
  4412. breaks gas generated objects. */
  4413. if (syment->n_value == 0)
  4414. {
  4415. asection *sec;
  4416. char * name;
  4417. char buf[SYMNMLEN + 1];
  4418. name = _bfd_coff_internal_syment_name (abfd, syment, buf)
  4419. sec = coff_section_from_bfd_index (abfd, syment->n_scnum);
  4420. if (sec != NULL && name != NULL
  4421. && (strcmp (bfd_section_name (sec), name) == 0))
  4422. return COFF_SYMBOL_PE_SECTION;
  4423. }
  4424. #endif
  4425. return COFF_SYMBOL_LOCAL;
  4426. }
  4427. if (syment->n_sclass == C_SECTION)
  4428. {
  4429. /* In some cases in a DLL generated by the Microsoft linker, the
  4430. n_value field will contain garbage. FIXME: This should
  4431. probably be handled by the swapping function instead. */
  4432. syment->n_value = 0;
  4433. if (syment->n_scnum == 0)
  4434. return COFF_SYMBOL_UNDEFINED;
  4435. return COFF_SYMBOL_PE_SECTION;
  4436. }
  4437. #endif /* COFF_WITH_PE */
  4438. /* If it is not a global symbol, we presume it is a local symbol. */
  4439. if (syment->n_scnum == 0)
  4440. {
  4441. char buf[SYMNMLEN + 1];
  4442. _bfd_error_handler
  4443. /* xgettext:c-format */
  4444. (_("warning: %pB: local symbol `%s' has no section"),
  4445. abfd, _bfd_coff_internal_syment_name (abfd, syment, buf));
  4446. }
  4447. return COFF_SYMBOL_LOCAL;
  4448. }
  4449. /*
  4450. SUBSUBSECTION
  4451. Reading relocations
  4452. Coff relocations are easily transformed into the internal BFD form
  4453. (@code{arelent}).
  4454. Reading a coff relocation table is done in the following stages:
  4455. o Read the entire coff relocation table into memory.
  4456. o Process each relocation in turn; first swap it from the
  4457. external to the internal form.
  4458. o Turn the symbol referenced in the relocation's symbol index
  4459. into a pointer into the canonical symbol table.
  4460. This table is the same as the one returned by a call to
  4461. @code{bfd_canonicalize_symtab}. The back end will call that
  4462. routine and save the result if a canonicalization hasn't been done.
  4463. o The reloc index is turned into a pointer to a howto
  4464. structure, in a back end specific way. For instance, the 386
  4465. uses the @code{r_type} to directly produce an index
  4466. into a howto table vector.
  4467. */
  4468. #ifndef CALC_ADDEND
  4469. #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
  4470. { \
  4471. coff_symbol_type *coffsym = NULL; \
  4472. \
  4473. if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
  4474. coffsym = (obj_symbols (abfd) \
  4475. + (cache_ptr->sym_ptr_ptr - symbols)); \
  4476. else if (ptr) \
  4477. coffsym = coff_symbol_from (ptr); \
  4478. if (coffsym != NULL \
  4479. && coffsym->native->is_sym \
  4480. && coffsym->native->u.syment.n_scnum == 0) \
  4481. cache_ptr->addend = 0; \
  4482. else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
  4483. && ptr->section != NULL) \
  4484. cache_ptr->addend = - (ptr->section->vma + ptr->value); \
  4485. else \
  4486. cache_ptr->addend = 0; \
  4487. }
  4488. #endif
  4489. static bool
  4490. coff_slurp_reloc_table (bfd * abfd, sec_ptr asect, asymbol ** symbols)
  4491. {
  4492. bfd_byte *native_relocs;
  4493. arelent *reloc_cache;
  4494. arelent *cache_ptr;
  4495. unsigned int idx;
  4496. size_t amt;
  4497. if (asect->relocation)
  4498. return true;
  4499. if (asect->reloc_count == 0)
  4500. return true;
  4501. if (asect->flags & SEC_CONSTRUCTOR)
  4502. return true;
  4503. if (!coff_slurp_symbol_table (abfd))
  4504. return false;
  4505. native_relocs = (bfd_byte *) buy_and_read (abfd, asect->rel_filepos,
  4506. asect->reloc_count,
  4507. bfd_coff_relsz (abfd));
  4508. if (native_relocs == NULL)
  4509. return false;
  4510. if (_bfd_mul_overflow (asect->reloc_count, sizeof (arelent), &amt))
  4511. {
  4512. bfd_set_error (bfd_error_file_too_big);
  4513. return false;
  4514. }
  4515. reloc_cache = (arelent *) bfd_alloc (abfd, amt);
  4516. if (reloc_cache == NULL)
  4517. {
  4518. free (native_relocs);
  4519. return false;
  4520. }
  4521. for (idx = 0; idx < asect->reloc_count; idx++)
  4522. {
  4523. struct internal_reloc dst;
  4524. void *src;
  4525. #ifndef RELOC_PROCESSING
  4526. asymbol *ptr;
  4527. #endif
  4528. cache_ptr = reloc_cache + idx;
  4529. src = native_relocs + idx * (size_t) bfd_coff_relsz (abfd);
  4530. dst.r_offset = 0;
  4531. bfd_coff_swap_reloc_in (abfd, src, &dst);
  4532. #ifdef RELOC_PROCESSING
  4533. RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
  4534. #else
  4535. cache_ptr->address = dst.r_vaddr;
  4536. if (dst.r_symndx != -1 && symbols != NULL)
  4537. {
  4538. if (dst.r_symndx < 0 || dst.r_symndx >= obj_conv_table_size (abfd))
  4539. {
  4540. _bfd_error_handler
  4541. /* xgettext:c-format */
  4542. (_("%pB: warning: illegal symbol index %ld in relocs"),
  4543. abfd, dst.r_symndx);
  4544. cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  4545. ptr = NULL;
  4546. }
  4547. else
  4548. {
  4549. cache_ptr->sym_ptr_ptr = (symbols
  4550. + obj_convert (abfd)[dst.r_symndx]);
  4551. ptr = *(cache_ptr->sym_ptr_ptr);
  4552. }
  4553. }
  4554. else
  4555. {
  4556. cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  4557. ptr = NULL;
  4558. }
  4559. /* The symbols definitions that we have read in have been
  4560. relocated as if their sections started at 0. But the offsets
  4561. refering to the symbols in the raw data have not been
  4562. modified, so we have to have a negative addend to compensate.
  4563. Note that symbols which used to be common must be left alone. */
  4564. /* Calculate any reloc addend by looking at the symbol. */
  4565. CALC_ADDEND (abfd, ptr, dst, cache_ptr);
  4566. (void) ptr;
  4567. cache_ptr->address -= asect->vma;
  4568. /* !! cache_ptr->section = NULL;*/
  4569. /* Fill in the cache_ptr->howto field from dst.r_type. */
  4570. RTYPE2HOWTO (cache_ptr, &dst);
  4571. #endif /* RELOC_PROCESSING */
  4572. if (cache_ptr->howto == NULL)
  4573. {
  4574. _bfd_error_handler
  4575. /* xgettext:c-format */
  4576. (_("%pB: illegal relocation type %d at address %#" PRIx64),
  4577. abfd, dst.r_type, (uint64_t) dst.r_vaddr);
  4578. bfd_set_error (bfd_error_bad_value);
  4579. free (native_relocs);
  4580. return false;
  4581. }
  4582. }
  4583. free (native_relocs);
  4584. asect->relocation = reloc_cache;
  4585. return true;
  4586. }
  4587. #ifndef coff_rtype_to_howto
  4588. #ifdef RTYPE2HOWTO
  4589. /* Get the howto structure for a reloc. This is only used if the file
  4590. including this one defines coff_relocate_section to be
  4591. _bfd_coff_generic_relocate_section, so it is OK if it does not
  4592. always work. It is the responsibility of the including file to
  4593. make sure it is reasonable if it is needed. */
  4594. static reloc_howto_type *
  4595. coff_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
  4596. asection *sec ATTRIBUTE_UNUSED,
  4597. struct internal_reloc *rel ATTRIBUTE_UNUSED,
  4598. struct coff_link_hash_entry *h ATTRIBUTE_UNUSED,
  4599. struct internal_syment *sym ATTRIBUTE_UNUSED,
  4600. bfd_vma *addendp ATTRIBUTE_UNUSED)
  4601. {
  4602. arelent genrel;
  4603. genrel.howto = NULL;
  4604. RTYPE2HOWTO (&genrel, rel);
  4605. return genrel.howto;
  4606. }
  4607. #else /* ! defined (RTYPE2HOWTO) */
  4608. #define coff_rtype_to_howto NULL
  4609. #endif /* ! defined (RTYPE2HOWTO) */
  4610. #endif /* ! defined (coff_rtype_to_howto) */
  4611. /* This is stupid. This function should be a boolean predicate. */
  4612. static long
  4613. coff_canonicalize_reloc (bfd * abfd,
  4614. sec_ptr section,
  4615. arelent ** relptr,
  4616. asymbol ** symbols)
  4617. {
  4618. arelent *tblptr = section->relocation;
  4619. unsigned int count = 0;
  4620. if (section->flags & SEC_CONSTRUCTOR)
  4621. {
  4622. /* This section has relocs made up by us, they are not in the
  4623. file, so take them out of their chain and place them into
  4624. the data area provided. */
  4625. arelent_chain *chain = section->constructor_chain;
  4626. for (count = 0; count < section->reloc_count; count++)
  4627. {
  4628. *relptr++ = &chain->relent;
  4629. chain = chain->next;
  4630. }
  4631. }
  4632. else
  4633. {
  4634. if (! coff_slurp_reloc_table (abfd, section, symbols))
  4635. return -1;
  4636. tblptr = section->relocation;
  4637. for (; count++ < section->reloc_count;)
  4638. *relptr++ = tblptr++;
  4639. }
  4640. *relptr = 0;
  4641. return section->reloc_count;
  4642. }
  4643. #ifndef coff_set_reloc
  4644. #define coff_set_reloc _bfd_generic_set_reloc
  4645. #endif
  4646. #ifndef coff_reloc16_estimate
  4647. #define coff_reloc16_estimate dummy_reloc16_estimate
  4648. static int
  4649. dummy_reloc16_estimate (bfd *abfd ATTRIBUTE_UNUSED,
  4650. asection *input_section ATTRIBUTE_UNUSED,
  4651. arelent *reloc ATTRIBUTE_UNUSED,
  4652. unsigned int shrink ATTRIBUTE_UNUSED,
  4653. struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
  4654. {
  4655. abort ();
  4656. return 0;
  4657. }
  4658. #endif
  4659. #ifndef coff_reloc16_extra_cases
  4660. #define coff_reloc16_extra_cases dummy_reloc16_extra_cases
  4661. /* This works even if abort is not declared in any header file. */
  4662. static void
  4663. dummy_reloc16_extra_cases (bfd *abfd ATTRIBUTE_UNUSED,
  4664. struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
  4665. struct bfd_link_order *link_order ATTRIBUTE_UNUSED,
  4666. arelent *reloc ATTRIBUTE_UNUSED,
  4667. bfd_byte *data ATTRIBUTE_UNUSED,
  4668. unsigned int *src_ptr ATTRIBUTE_UNUSED,
  4669. unsigned int *dst_ptr ATTRIBUTE_UNUSED)
  4670. {
  4671. abort ();
  4672. }
  4673. #endif
  4674. /* If coff_relocate_section is defined, we can use the optimized COFF
  4675. backend linker. Otherwise we must continue to use the old linker. */
  4676. #ifdef coff_relocate_section
  4677. #ifndef coff_bfd_link_hash_table_create
  4678. #define coff_bfd_link_hash_table_create _bfd_coff_link_hash_table_create
  4679. #endif
  4680. #ifndef coff_bfd_link_add_symbols
  4681. #define coff_bfd_link_add_symbols _bfd_coff_link_add_symbols
  4682. #endif
  4683. #ifndef coff_bfd_final_link
  4684. #define coff_bfd_final_link _bfd_coff_final_link
  4685. #endif
  4686. #else /* ! defined (coff_relocate_section) */
  4687. #define coff_relocate_section NULL
  4688. #ifndef coff_bfd_link_hash_table_create
  4689. #define coff_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  4690. #endif
  4691. #ifndef coff_bfd_link_add_symbols
  4692. #define coff_bfd_link_add_symbols _bfd_generic_link_add_symbols
  4693. #endif
  4694. #define coff_bfd_final_link _bfd_generic_final_link
  4695. #endif /* ! defined (coff_relocate_section) */
  4696. #define coff_bfd_link_just_syms _bfd_generic_link_just_syms
  4697. #define coff_bfd_copy_link_hash_symbol_type \
  4698. _bfd_generic_copy_link_hash_symbol_type
  4699. #define coff_bfd_link_split_section _bfd_generic_link_split_section
  4700. #define coff_bfd_link_check_relocs _bfd_generic_link_check_relocs
  4701. #ifndef coff_start_final_link
  4702. #define coff_start_final_link NULL
  4703. #endif
  4704. #ifndef coff_adjust_symndx
  4705. #define coff_adjust_symndx NULL
  4706. #endif
  4707. #ifndef coff_link_add_one_symbol
  4708. #define coff_link_add_one_symbol _bfd_generic_link_add_one_symbol
  4709. #endif
  4710. #ifndef coff_link_output_has_begun
  4711. static bool
  4712. coff_link_output_has_begun (bfd * abfd,
  4713. struct coff_final_link_info * info ATTRIBUTE_UNUSED)
  4714. {
  4715. return abfd->output_has_begun;
  4716. }
  4717. #endif
  4718. #ifndef coff_final_link_postscript
  4719. static bool
  4720. coff_final_link_postscript (bfd * abfd ATTRIBUTE_UNUSED,
  4721. struct coff_final_link_info * pfinfo ATTRIBUTE_UNUSED)
  4722. {
  4723. return true;
  4724. }
  4725. #endif
  4726. #ifndef coff_SWAP_aux_in
  4727. #define coff_SWAP_aux_in coff_swap_aux_in
  4728. #endif
  4729. #ifndef coff_SWAP_sym_in
  4730. #define coff_SWAP_sym_in coff_swap_sym_in
  4731. #endif
  4732. #ifndef coff_SWAP_lineno_in
  4733. #define coff_SWAP_lineno_in coff_swap_lineno_in
  4734. #endif
  4735. #ifndef coff_SWAP_aux_out
  4736. #define coff_SWAP_aux_out coff_swap_aux_out
  4737. #endif
  4738. #ifndef coff_SWAP_sym_out
  4739. #define coff_SWAP_sym_out coff_swap_sym_out
  4740. #endif
  4741. #ifndef coff_SWAP_lineno_out
  4742. #define coff_SWAP_lineno_out coff_swap_lineno_out
  4743. #endif
  4744. #ifndef coff_SWAP_reloc_out
  4745. #define coff_SWAP_reloc_out coff_swap_reloc_out
  4746. #endif
  4747. #ifndef coff_SWAP_filehdr_out
  4748. #define coff_SWAP_filehdr_out coff_swap_filehdr_out
  4749. #endif
  4750. #ifndef coff_SWAP_aouthdr_out
  4751. #define coff_SWAP_aouthdr_out coff_swap_aouthdr_out
  4752. #endif
  4753. #ifndef coff_SWAP_scnhdr_out
  4754. #define coff_SWAP_scnhdr_out coff_swap_scnhdr_out
  4755. #endif
  4756. #ifndef coff_SWAP_reloc_in
  4757. #define coff_SWAP_reloc_in coff_swap_reloc_in
  4758. #endif
  4759. #ifndef coff_SWAP_filehdr_in
  4760. #define coff_SWAP_filehdr_in coff_swap_filehdr_in
  4761. #endif
  4762. #ifndef coff_SWAP_aouthdr_in
  4763. #define coff_SWAP_aouthdr_in coff_swap_aouthdr_in
  4764. #endif
  4765. #ifndef coff_SWAP_scnhdr_in
  4766. #define coff_SWAP_scnhdr_in coff_swap_scnhdr_in
  4767. #endif
  4768. static bfd_coff_backend_data bfd_coff_std_swap_table ATTRIBUTE_UNUSED =
  4769. {
  4770. coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
  4771. coff_SWAP_aux_out, coff_SWAP_sym_out,
  4772. coff_SWAP_lineno_out, coff_SWAP_reloc_out,
  4773. coff_SWAP_filehdr_out, coff_SWAP_aouthdr_out,
  4774. coff_SWAP_scnhdr_out,
  4775. FILHSZ, AOUTSZ, SCNHSZ, SYMESZ, AUXESZ, RELSZ, LINESZ, FILNMLEN,
  4776. #ifdef COFF_LONG_FILENAMES
  4777. true,
  4778. #else
  4779. false,
  4780. #endif
  4781. COFF_DEFAULT_LONG_SECTION_NAMES,
  4782. COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
  4783. #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
  4784. true,
  4785. #else
  4786. false,
  4787. #endif
  4788. #ifdef COFF_DEBUG_STRING_WIDE_PREFIX
  4789. 4,
  4790. #else
  4791. 2,
  4792. #endif
  4793. 32768,
  4794. coff_SWAP_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
  4795. coff_SWAP_reloc_in, coff_bad_format_hook, coff_set_arch_mach_hook,
  4796. coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
  4797. coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
  4798. coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
  4799. coff_classify_symbol, coff_compute_section_file_positions,
  4800. coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
  4801. coff_adjust_symndx, coff_link_add_one_symbol,
  4802. coff_link_output_has_begun, coff_final_link_postscript,
  4803. bfd_pe_print_pdata
  4804. };
  4805. #ifdef TICOFF
  4806. /* COFF0 differs in file/section header size and relocation entry size. */
  4807. static bfd_coff_backend_data ticoff0_swap_table =
  4808. {
  4809. coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
  4810. coff_SWAP_aux_out, coff_SWAP_sym_out,
  4811. coff_SWAP_lineno_out, coff_swap_reloc_v0_out,
  4812. coff_SWAP_filehdr_out, coff_SWAP_aouthdr_out,
  4813. coff_SWAP_scnhdr_out,
  4814. FILHSZ_V0, AOUTSZ, SCNHSZ_V01, SYMESZ, AUXESZ, RELSZ_V0, LINESZ, FILNMLEN,
  4815. #ifdef COFF_LONG_FILENAMES
  4816. true,
  4817. #else
  4818. false,
  4819. #endif
  4820. COFF_DEFAULT_LONG_SECTION_NAMES,
  4821. COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
  4822. #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
  4823. true,
  4824. #else
  4825. false,
  4826. #endif
  4827. #ifdef COFF_DEBUG_STRING_WIDE_PREFIX
  4828. 4,
  4829. #else
  4830. 2,
  4831. #endif
  4832. 32768,
  4833. coff_SWAP_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
  4834. coff_swap_reloc_v0_in, ticoff0_bad_format_hook, coff_set_arch_mach_hook,
  4835. coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
  4836. coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
  4837. coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
  4838. coff_classify_symbol, coff_compute_section_file_positions,
  4839. coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
  4840. coff_adjust_symndx, coff_link_add_one_symbol,
  4841. coff_link_output_has_begun, coff_final_link_postscript,
  4842. bfd_pe_print_pdata
  4843. };
  4844. #endif
  4845. #ifdef TICOFF
  4846. /* COFF1 differs in section header size. */
  4847. static bfd_coff_backend_data ticoff1_swap_table =
  4848. {
  4849. coff_SWAP_aux_in, coff_SWAP_sym_in, coff_SWAP_lineno_in,
  4850. coff_SWAP_aux_out, coff_SWAP_sym_out,
  4851. coff_SWAP_lineno_out, coff_SWAP_reloc_out,
  4852. coff_SWAP_filehdr_out, coff_SWAP_aouthdr_out,
  4853. coff_SWAP_scnhdr_out,
  4854. FILHSZ, AOUTSZ, SCNHSZ_V01, SYMESZ, AUXESZ, RELSZ, LINESZ, FILNMLEN,
  4855. #ifdef COFF_LONG_FILENAMES
  4856. true,
  4857. #else
  4858. false,
  4859. #endif
  4860. COFF_DEFAULT_LONG_SECTION_NAMES,
  4861. COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
  4862. #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
  4863. true,
  4864. #else
  4865. false,
  4866. #endif
  4867. #ifdef COFF_DEBUG_STRING_WIDE_PREFIX
  4868. 4,
  4869. #else
  4870. 2,
  4871. #endif
  4872. 32768,
  4873. coff_SWAP_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
  4874. coff_SWAP_reloc_in, ticoff1_bad_format_hook, coff_set_arch_mach_hook,
  4875. coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
  4876. coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
  4877. coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
  4878. coff_classify_symbol, coff_compute_section_file_positions,
  4879. coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
  4880. coff_adjust_symndx, coff_link_add_one_symbol,
  4881. coff_link_output_has_begun, coff_final_link_postscript,
  4882. bfd_pe_print_pdata /* huh */
  4883. };
  4884. #endif
  4885. #ifdef COFF_WITH_PE_BIGOBJ
  4886. /* The UID for bigobj files. */
  4887. static const char header_bigobj_classid[16] =
  4888. {
  4889. 0xC7, 0xA1, 0xBA, 0xD1,
  4890. 0xEE, 0xBA,
  4891. 0xa9, 0x4b,
  4892. 0xAF, 0x20,
  4893. 0xFA, 0xF6, 0x6A, 0xA4, 0xDC, 0xB8
  4894. };
  4895. /* Swap routines. */
  4896. static void
  4897. coff_bigobj_swap_filehdr_in (bfd * abfd, void * src, void * dst)
  4898. {
  4899. struct external_ANON_OBJECT_HEADER_BIGOBJ *filehdr_src =
  4900. (struct external_ANON_OBJECT_HEADER_BIGOBJ *) src;
  4901. struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
  4902. filehdr_dst->f_magic = H_GET_16 (abfd, filehdr_src->Machine);
  4903. filehdr_dst->f_nscns = H_GET_32 (abfd, filehdr_src->NumberOfSections);
  4904. filehdr_dst->f_timdat = H_GET_32 (abfd, filehdr_src->TimeDateStamp);
  4905. filehdr_dst->f_symptr =
  4906. GET_FILEHDR_SYMPTR (abfd, filehdr_src->PointerToSymbolTable);
  4907. filehdr_dst->f_nsyms = H_GET_32 (abfd, filehdr_src->NumberOfSymbols);
  4908. filehdr_dst->f_opthdr = 0;
  4909. filehdr_dst->f_flags = 0;
  4910. /* Check other magic numbers. */
  4911. if (H_GET_16 (abfd, filehdr_src->Sig1) != IMAGE_FILE_MACHINE_UNKNOWN
  4912. || H_GET_16 (abfd, filehdr_src->Sig2) != 0xffff
  4913. || H_GET_16 (abfd, filehdr_src->Version) != 2
  4914. || memcmp (filehdr_src->ClassID, header_bigobj_classid, 16) != 0)
  4915. filehdr_dst->f_opthdr = 0xffff;
  4916. /* Note that CLR metadata are ignored. */
  4917. }
  4918. static unsigned int
  4919. coff_bigobj_swap_filehdr_out (bfd *abfd, void * in, void * out)
  4920. {
  4921. struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
  4922. struct external_ANON_OBJECT_HEADER_BIGOBJ *filehdr_out =
  4923. (struct external_ANON_OBJECT_HEADER_BIGOBJ *) out;
  4924. memset (filehdr_out, 0, sizeof (*filehdr_out));
  4925. H_PUT_16 (abfd, IMAGE_FILE_MACHINE_UNKNOWN, filehdr_out->Sig1);
  4926. H_PUT_16 (abfd, 0xffff, filehdr_out->Sig2);
  4927. H_PUT_16 (abfd, 2, filehdr_out->Version);
  4928. memcpy (filehdr_out->ClassID, header_bigobj_classid, 16);
  4929. H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->Machine);
  4930. H_PUT_32 (abfd, filehdr_in->f_nscns, filehdr_out->NumberOfSections);
  4931. H_PUT_32 (abfd, filehdr_in->f_timdat, filehdr_out->TimeDateStamp);
  4932. PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr,
  4933. filehdr_out->PointerToSymbolTable);
  4934. H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->NumberOfSymbols);
  4935. return bfd_coff_filhsz (abfd);
  4936. }
  4937. static void
  4938. coff_bigobj_swap_sym_in (bfd * abfd, void * ext1, void * in1)
  4939. {
  4940. SYMENT_BIGOBJ *ext = (SYMENT_BIGOBJ *) ext1;
  4941. struct internal_syment *in = (struct internal_syment *) in1;
  4942. if (ext->e.e_name[0] == 0)
  4943. {
  4944. in->_n._n_n._n_zeroes = 0;
  4945. in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
  4946. }
  4947. else
  4948. {
  4949. #if SYMNMLEN != E_SYMNMLEN
  4950. #error we need to cope with truncating or extending SYMNMLEN
  4951. #else
  4952. memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
  4953. #endif
  4954. }
  4955. in->n_value = H_GET_32 (abfd, ext->e_value);
  4956. BFD_ASSERT (sizeof (in->n_scnum) >= 4);
  4957. in->n_scnum = H_GET_32 (abfd, ext->e_scnum);
  4958. in->n_type = H_GET_16 (abfd, ext->e_type);
  4959. in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
  4960. in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
  4961. }
  4962. static unsigned int
  4963. coff_bigobj_swap_sym_out (bfd * abfd, void * inp, void * extp)
  4964. {
  4965. struct internal_syment *in = (struct internal_syment *) inp;
  4966. SYMENT_BIGOBJ *ext = (SYMENT_BIGOBJ *) extp;
  4967. if (in->_n._n_name[0] == 0)
  4968. {
  4969. H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
  4970. H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
  4971. }
  4972. else
  4973. {
  4974. #if SYMNMLEN != E_SYMNMLEN
  4975. #error we need to cope with truncating or extending SYMNMLEN
  4976. #else
  4977. memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
  4978. #endif
  4979. }
  4980. H_PUT_32 (abfd, in->n_value, ext->e_value);
  4981. H_PUT_32 (abfd, in->n_scnum, ext->e_scnum);
  4982. H_PUT_16 (abfd, in->n_type, ext->e_type);
  4983. H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
  4984. H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
  4985. return SYMESZ_BIGOBJ;
  4986. }
  4987. static void
  4988. coff_bigobj_swap_aux_in (bfd *abfd,
  4989. void * ext1,
  4990. int type,
  4991. int in_class,
  4992. int indx,
  4993. int numaux,
  4994. void * in1)
  4995. {
  4996. AUXENT_BIGOBJ *ext = (AUXENT_BIGOBJ *) ext1;
  4997. union internal_auxent *in = (union internal_auxent *) in1;
  4998. /* Make sure that all fields in the aux structure are
  4999. initialised. */
  5000. memset (in, 0, sizeof * in);
  5001. switch (in_class)
  5002. {
  5003. case C_FILE:
  5004. if (numaux > 1)
  5005. {
  5006. if (indx == 0)
  5007. memcpy (in->x_file.x_n.x_fname, ext->File.Name,
  5008. numaux * sizeof (AUXENT_BIGOBJ));
  5009. }
  5010. else
  5011. memcpy (in->x_file.x_n.x_fname, ext->File.Name, sizeof (ext->File.Name));
  5012. break;
  5013. case C_STAT:
  5014. case C_LEAFSTAT:
  5015. case C_HIDDEN:
  5016. if (type == T_NULL)
  5017. {
  5018. in->x_scn.x_scnlen = H_GET_32 (abfd, ext->Section.Length);
  5019. in->x_scn.x_nreloc =
  5020. H_GET_16 (abfd, ext->Section.NumberOfRelocations);
  5021. in->x_scn.x_nlinno =
  5022. H_GET_16 (abfd, ext->Section.NumberOfLinenumbers);
  5023. in->x_scn.x_checksum = H_GET_32 (abfd, ext->Section.Checksum);
  5024. in->x_scn.x_associated = H_GET_16 (abfd, ext->Section.Number)
  5025. | (H_GET_16 (abfd, ext->Section.HighNumber) << 16);
  5026. in->x_scn.x_comdat = H_GET_8 (abfd, ext->Section.Selection);
  5027. return;
  5028. }
  5029. break;
  5030. default:
  5031. in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->Sym.WeakDefaultSymIndex);
  5032. /* Characteristics is ignored. */
  5033. break;
  5034. }
  5035. }
  5036. static unsigned int
  5037. coff_bigobj_swap_aux_out (bfd * abfd,
  5038. void * inp,
  5039. int type,
  5040. int in_class,
  5041. int indx ATTRIBUTE_UNUSED,
  5042. int numaux ATTRIBUTE_UNUSED,
  5043. void * extp)
  5044. {
  5045. union internal_auxent * in = (union internal_auxent *) inp;
  5046. AUXENT_BIGOBJ *ext = (AUXENT_BIGOBJ *) extp;
  5047. memset (ext, 0, AUXESZ);
  5048. switch (in_class)
  5049. {
  5050. case C_FILE:
  5051. memcpy (ext->File.Name, in->x_file.x_n.x_fname, sizeof (ext->File.Name));
  5052. return AUXESZ;
  5053. case C_STAT:
  5054. case C_LEAFSTAT:
  5055. case C_HIDDEN:
  5056. if (type == T_NULL)
  5057. {
  5058. H_PUT_32 (abfd, in->x_scn.x_scnlen, ext->Section.Length);
  5059. H_PUT_16 (abfd, in->x_scn.x_nreloc,
  5060. ext->Section.NumberOfRelocations);
  5061. H_PUT_16 (abfd, in->x_scn.x_nlinno,
  5062. ext->Section.NumberOfLinenumbers);
  5063. H_PUT_32 (abfd, in->x_scn.x_checksum, ext->Section.Checksum);
  5064. H_PUT_16 (abfd, in->x_scn.x_associated & 0xffff,
  5065. ext->Section.Number);
  5066. H_PUT_16 (abfd, (in->x_scn.x_associated >> 16),
  5067. ext->Section.HighNumber);
  5068. H_PUT_8 (abfd, in->x_scn.x_comdat, ext->Section.Selection);
  5069. return AUXESZ;
  5070. }
  5071. break;
  5072. }
  5073. H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->Sym.WeakDefaultSymIndex);
  5074. H_PUT_32 (abfd, 1, ext->Sym.WeakSearchType);
  5075. return AUXESZ;
  5076. }
  5077. static bfd_coff_backend_data bigobj_swap_table =
  5078. {
  5079. coff_bigobj_swap_aux_in, coff_bigobj_swap_sym_in, coff_SWAP_lineno_in,
  5080. coff_bigobj_swap_aux_out, coff_bigobj_swap_sym_out,
  5081. coff_SWAP_lineno_out, coff_SWAP_reloc_out,
  5082. coff_bigobj_swap_filehdr_out, coff_SWAP_aouthdr_out,
  5083. coff_SWAP_scnhdr_out,
  5084. FILHSZ_BIGOBJ, AOUTSZ, SCNHSZ, SYMESZ_BIGOBJ, AUXESZ_BIGOBJ,
  5085. RELSZ, LINESZ, FILNMLEN_BIGOBJ,
  5086. true,
  5087. COFF_DEFAULT_LONG_SECTION_NAMES,
  5088. COFF_DEFAULT_SECTION_ALIGNMENT_POWER,
  5089. false,
  5090. 2,
  5091. 1U << 31,
  5092. coff_bigobj_swap_filehdr_in, coff_SWAP_aouthdr_in, coff_SWAP_scnhdr_in,
  5093. coff_SWAP_reloc_in, coff_bad_format_hook, coff_set_arch_mach_hook,
  5094. coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
  5095. coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
  5096. coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
  5097. coff_classify_symbol, coff_compute_section_file_positions,
  5098. coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
  5099. coff_adjust_symndx, coff_link_add_one_symbol,
  5100. coff_link_output_has_begun, coff_final_link_postscript,
  5101. bfd_pe_print_pdata /* huh */
  5102. };
  5103. #endif /* COFF_WITH_PE_BIGOBJ */
  5104. #ifndef coff_close_and_cleanup
  5105. #define coff_close_and_cleanup _bfd_coff_close_and_cleanup
  5106. #endif
  5107. #ifndef coff_bfd_free_cached_info
  5108. #define coff_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  5109. #endif
  5110. #ifndef coff_get_section_contents
  5111. #define coff_get_section_contents _bfd_generic_get_section_contents
  5112. #endif
  5113. #ifndef coff_bfd_copy_private_symbol_data
  5114. #define coff_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
  5115. #endif
  5116. #ifndef coff_bfd_copy_private_header_data
  5117. #define coff_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
  5118. #endif
  5119. #ifndef coff_bfd_copy_private_section_data
  5120. #define coff_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
  5121. #endif
  5122. #ifndef coff_bfd_copy_private_bfd_data
  5123. #define coff_bfd_copy_private_bfd_data _bfd_generic_bfd_copy_private_bfd_data
  5124. #endif
  5125. #ifndef coff_bfd_merge_private_bfd_data
  5126. #define coff_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
  5127. #endif
  5128. #ifndef coff_bfd_set_private_flags
  5129. #define coff_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
  5130. #endif
  5131. #ifndef coff_bfd_print_private_bfd_data
  5132. #define coff_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
  5133. #endif
  5134. #ifndef coff_bfd_is_local_label_name
  5135. #define coff_bfd_is_local_label_name _bfd_coff_is_local_label_name
  5136. #endif
  5137. #ifndef coff_bfd_is_target_special_symbol
  5138. #define coff_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
  5139. #endif
  5140. #ifndef coff_read_minisymbols
  5141. #define coff_read_minisymbols _bfd_generic_read_minisymbols
  5142. #endif
  5143. #ifndef coff_minisymbol_to_symbol
  5144. #define coff_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
  5145. #endif
  5146. /* The reloc lookup routine must be supplied by each individual COFF
  5147. backend. */
  5148. #ifndef coff_bfd_reloc_type_lookup
  5149. #define coff_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
  5150. #endif
  5151. #ifndef coff_bfd_reloc_name_lookup
  5152. #define coff_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
  5153. #endif
  5154. #ifndef coff_bfd_get_relocated_section_contents
  5155. #define coff_bfd_get_relocated_section_contents \
  5156. bfd_generic_get_relocated_section_contents
  5157. #endif
  5158. #ifndef coff_bfd_relax_section
  5159. #define coff_bfd_relax_section bfd_generic_relax_section
  5160. #endif
  5161. #ifndef coff_bfd_gc_sections
  5162. #define coff_bfd_gc_sections bfd_coff_gc_sections
  5163. #endif
  5164. #ifndef coff_bfd_lookup_section_flags
  5165. #define coff_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  5166. #endif
  5167. #ifndef coff_bfd_merge_sections
  5168. #define coff_bfd_merge_sections bfd_generic_merge_sections
  5169. #endif
  5170. #ifndef coff_bfd_is_group_section
  5171. #define coff_bfd_is_group_section bfd_generic_is_group_section
  5172. #endif
  5173. #ifndef coff_bfd_group_name
  5174. #define coff_bfd_group_name bfd_coff_group_name
  5175. #endif
  5176. #ifndef coff_bfd_discard_group
  5177. #define coff_bfd_discard_group bfd_generic_discard_group
  5178. #endif
  5179. #ifndef coff_section_already_linked
  5180. #define coff_section_already_linked \
  5181. _bfd_coff_section_already_linked
  5182. #endif
  5183. #ifndef coff_bfd_define_common_symbol
  5184. #define coff_bfd_define_common_symbol bfd_generic_define_common_symbol
  5185. #endif
  5186. #ifndef coff_bfd_link_hide_symbol
  5187. #define coff_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
  5188. #endif
  5189. #ifndef coff_bfd_define_start_stop
  5190. #define coff_bfd_define_start_stop bfd_generic_define_start_stop
  5191. #endif
  5192. #define CREATE_BIG_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE, SWAP_TABLE) \
  5193. const bfd_target VAR = \
  5194. { \
  5195. NAME , \
  5196. bfd_target_coff_flavour, \
  5197. BFD_ENDIAN_BIG, /* Data byte order is big. */ \
  5198. BFD_ENDIAN_BIG, /* Header byte order is big. */ \
  5199. /* object flags */ \
  5200. (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | \
  5201. HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS), \
  5202. /* section flags */ \
  5203. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
  5204. UNDER, /* Leading symbol underscore. */ \
  5205. '/', /* AR_pad_char. */ \
  5206. 15, /* AR_max_namelen. */ \
  5207. 0, /* match priority. */ \
  5208. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */ \
  5209. \
  5210. /* Data conversion functions. */ \
  5211. bfd_getb64, bfd_getb_signed_64, bfd_putb64, \
  5212. bfd_getb32, bfd_getb_signed_32, bfd_putb32, \
  5213. bfd_getb16, bfd_getb_signed_16, bfd_putb16, \
  5214. \
  5215. /* Header conversion functions. */ \
  5216. bfd_getb64, bfd_getb_signed_64, bfd_putb64, \
  5217. bfd_getb32, bfd_getb_signed_32, bfd_putb32, \
  5218. bfd_getb16, bfd_getb_signed_16, bfd_putb16, \
  5219. \
  5220. { /* bfd_check_format. */ \
  5221. _bfd_dummy_target, \
  5222. coff_object_p, \
  5223. bfd_generic_archive_p, \
  5224. _bfd_dummy_target \
  5225. }, \
  5226. { /* bfd_set_format. */ \
  5227. _bfd_bool_bfd_false_error, \
  5228. coff_mkobject, \
  5229. _bfd_generic_mkarchive, \
  5230. _bfd_bool_bfd_false_error \
  5231. }, \
  5232. { /* bfd_write_contents. */ \
  5233. _bfd_bool_bfd_false_error, \
  5234. coff_write_object_contents, \
  5235. _bfd_write_archive_contents, \
  5236. _bfd_bool_bfd_false_error \
  5237. }, \
  5238. \
  5239. BFD_JUMP_TABLE_GENERIC (coff), \
  5240. BFD_JUMP_TABLE_COPY (coff), \
  5241. BFD_JUMP_TABLE_CORE (_bfd_nocore), \
  5242. BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), \
  5243. BFD_JUMP_TABLE_SYMBOLS (coff), \
  5244. BFD_JUMP_TABLE_RELOCS (coff), \
  5245. BFD_JUMP_TABLE_WRITE (coff), \
  5246. BFD_JUMP_TABLE_LINK (coff), \
  5247. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), \
  5248. \
  5249. ALTERNATIVE, \
  5250. \
  5251. SWAP_TABLE \
  5252. };
  5253. #define CREATE_BIGHDR_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE, SWAP_TABLE) \
  5254. const bfd_target VAR = \
  5255. { \
  5256. NAME , \
  5257. bfd_target_coff_flavour, \
  5258. BFD_ENDIAN_LITTLE, /* Data byte order is little. */ \
  5259. BFD_ENDIAN_BIG, /* Header byte order is big. */ \
  5260. /* object flags */ \
  5261. (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | \
  5262. HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS), \
  5263. /* section flags */ \
  5264. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
  5265. UNDER, /* Leading symbol underscore. */ \
  5266. '/', /* AR_pad_char. */ \
  5267. 15, /* AR_max_namelen. */ \
  5268. 0, /* match priority. */ \
  5269. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */ \
  5270. \
  5271. /* Data conversion functions. */ \
  5272. bfd_getb64, bfd_getb_signed_64, bfd_putb64, \
  5273. bfd_getb32, bfd_getb_signed_32, bfd_putb32, \
  5274. bfd_getb16, bfd_getb_signed_16, bfd_putb16, \
  5275. \
  5276. /* Header conversion functions. */ \
  5277. bfd_getb64, bfd_getb_signed_64, bfd_putb64, \
  5278. bfd_getb32, bfd_getb_signed_32, bfd_putb32, \
  5279. bfd_getb16, bfd_getb_signed_16, bfd_putb16, \
  5280. \
  5281. { /* bfd_check_format. */ \
  5282. _bfd_dummy_target, \
  5283. coff_object_p, \
  5284. bfd_generic_archive_p, \
  5285. _bfd_dummy_target \
  5286. }, \
  5287. { /* bfd_set_format. */ \
  5288. _bfd_bool_bfd_false_error, \
  5289. coff_mkobject, \
  5290. _bfd_generic_mkarchive, \
  5291. _bfd_bool_bfd_false_error \
  5292. }, \
  5293. { /* bfd_write_contents. */ \
  5294. _bfd_bool_bfd_false_error, \
  5295. coff_write_object_contents, \
  5296. _bfd_write_archive_contents, \
  5297. _bfd_bool_bfd_false_error \
  5298. }, \
  5299. \
  5300. BFD_JUMP_TABLE_GENERIC (coff), \
  5301. BFD_JUMP_TABLE_COPY (coff), \
  5302. BFD_JUMP_TABLE_CORE (_bfd_nocore), \
  5303. BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), \
  5304. BFD_JUMP_TABLE_SYMBOLS (coff), \
  5305. BFD_JUMP_TABLE_RELOCS (coff), \
  5306. BFD_JUMP_TABLE_WRITE (coff), \
  5307. BFD_JUMP_TABLE_LINK (coff), \
  5308. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), \
  5309. \
  5310. ALTERNATIVE, \
  5311. \
  5312. SWAP_TABLE \
  5313. };
  5314. #define CREATE_LITTLE_COFF_TARGET_VEC(VAR, NAME, EXTRA_O_FLAGS, EXTRA_S_FLAGS, UNDER, ALTERNATIVE, SWAP_TABLE) \
  5315. const bfd_target VAR = \
  5316. { \
  5317. NAME , \
  5318. bfd_target_coff_flavour, \
  5319. BFD_ENDIAN_LITTLE, /* Data byte order is little. */ \
  5320. BFD_ENDIAN_LITTLE, /* Header byte order is little. */ \
  5321. /* object flags */ \
  5322. (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | \
  5323. HAS_SYMS | HAS_LOCALS | WP_TEXT | EXTRA_O_FLAGS), \
  5324. /* section flags */ \
  5325. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | EXTRA_S_FLAGS),\
  5326. UNDER, /* Leading symbol underscore. */ \
  5327. '/', /* AR_pad_char. */ \
  5328. 15, /* AR_max_namelen. */ \
  5329. 0, /* match priority. */ \
  5330. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */ \
  5331. \
  5332. /* Data conversion functions. */ \
  5333. bfd_getl64, bfd_getl_signed_64, bfd_putl64, \
  5334. bfd_getl32, bfd_getl_signed_32, bfd_putl32, \
  5335. bfd_getl16, bfd_getl_signed_16, bfd_putl16, \
  5336. /* Header conversion functions. */ \
  5337. bfd_getl64, bfd_getl_signed_64, bfd_putl64, \
  5338. bfd_getl32, bfd_getl_signed_32, bfd_putl32, \
  5339. bfd_getl16, bfd_getl_signed_16, bfd_putl16, \
  5340. \
  5341. { /* bfd_check_format. */ \
  5342. _bfd_dummy_target, \
  5343. coff_object_p, \
  5344. bfd_generic_archive_p, \
  5345. _bfd_dummy_target \
  5346. }, \
  5347. { /* bfd_set_format. */ \
  5348. _bfd_bool_bfd_false_error, \
  5349. coff_mkobject, \
  5350. _bfd_generic_mkarchive, \
  5351. _bfd_bool_bfd_false_error \
  5352. }, \
  5353. { /* bfd_write_contents. */ \
  5354. _bfd_bool_bfd_false_error, \
  5355. coff_write_object_contents, \
  5356. _bfd_write_archive_contents, \
  5357. _bfd_bool_bfd_false_error \
  5358. }, \
  5359. \
  5360. BFD_JUMP_TABLE_GENERIC (coff), \
  5361. BFD_JUMP_TABLE_COPY (coff), \
  5362. BFD_JUMP_TABLE_CORE (_bfd_nocore), \
  5363. BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff), \
  5364. BFD_JUMP_TABLE_SYMBOLS (coff), \
  5365. BFD_JUMP_TABLE_RELOCS (coff), \
  5366. BFD_JUMP_TABLE_WRITE (coff), \
  5367. BFD_JUMP_TABLE_LINK (coff), \
  5368. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), \
  5369. \
  5370. ALTERNATIVE, \
  5371. \
  5372. SWAP_TABLE \
  5373. };