objcopy.c 168 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066
  1. /* objcopy.c -- copy object file from input to output, optionally massaging it.
  2. Copyright (C) 1991-2022 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "progress.h"
  19. #include "getopt.h"
  20. #include "libiberty.h"
  21. #include "bucomm.h"
  22. #include "budbg.h"
  23. #include "filenames.h"
  24. #include "fnmatch.h"
  25. #include "elf-bfd.h"
  26. #include "coff/internal.h"
  27. #include "libcoff.h"
  28. #include "safe-ctype.h"
  29. /* FIXME: See bfd/peXXigen.c for why we include an architecture specific
  30. header in generic PE code. */
  31. #include "coff/i386.h"
  32. #include "coff/pe.h"
  33. static bfd_vma pe_file_alignment = (bfd_vma) -1;
  34. static bfd_vma pe_heap_commit = (bfd_vma) -1;
  35. static bfd_vma pe_heap_reserve = (bfd_vma) -1;
  36. static bfd_vma pe_image_base = (bfd_vma) -1;
  37. static bfd_vma pe_section_alignment = (bfd_vma) -1;
  38. static bfd_vma pe_stack_commit = (bfd_vma) -1;
  39. static bfd_vma pe_stack_reserve = (bfd_vma) -1;
  40. static short pe_subsystem = -1;
  41. static short pe_major_subsystem_version = -1;
  42. static short pe_minor_subsystem_version = -1;
  43. struct is_specified_symbol_predicate_data
  44. {
  45. const char *name;
  46. bool found;
  47. };
  48. /* A node includes symbol name mapping to support redefine_sym. */
  49. struct redefine_node
  50. {
  51. char *source;
  52. char *target;
  53. };
  54. struct addsym_node
  55. {
  56. struct addsym_node *next;
  57. char * symdef;
  58. long symval;
  59. flagword flags;
  60. char * section;
  61. const char * othersym;
  62. };
  63. typedef struct section_rename
  64. {
  65. const char * old_name;
  66. const char * new_name;
  67. flagword flags;
  68. struct section_rename * next;
  69. }
  70. section_rename;
  71. /* List of sections to be renamed. */
  72. static section_rename *section_rename_list;
  73. static asymbol **isympp = NULL; /* Input symbols. */
  74. static asymbol **osympp = NULL; /* Output symbols that survive stripping. */
  75. /* If `copy_byte' >= 0, copy 'copy_width' byte(s) of every `interleave' bytes. */
  76. static int copy_byte = -1;
  77. static int interleave = 0; /* Initialised to 4 in copy_main(). */
  78. static int copy_width = 1;
  79. static bool keep_section_symbols = false ;/* True if section symbols should be retained. */
  80. static bool verbose; /* Print file and target names. */
  81. static bool preserve_dates; /* Preserve input file timestamp. */
  82. static int deterministic = -1; /* Enable deterministic archives. */
  83. static int status = 0; /* Exit status. */
  84. static bool merge_notes = false; /* Merge note sections. */
  85. typedef struct merged_note_section
  86. {
  87. asection * sec; /* The section that is being merged. */
  88. bfd_byte * contents;/* New contents of the section. */
  89. bfd_size_type size; /* New size of the section. */
  90. struct merged_note_section * next; /* Link to next merged note section. */
  91. } merged_note_section;
  92. enum strip_action
  93. {
  94. STRIP_UNDEF,
  95. STRIP_NONE, /* Don't strip. */
  96. STRIP_DEBUG, /* Strip all debugger symbols. */
  97. STRIP_UNNEEDED, /* Strip unnecessary symbols. */
  98. STRIP_NONDEBUG, /* Strip everything but debug info. */
  99. STRIP_DWO, /* Strip all DWO info. */
  100. STRIP_NONDWO, /* Strip everything but DWO info. */
  101. STRIP_ALL /* Strip all symbols. */
  102. };
  103. /* Which symbols to remove. */
  104. static enum strip_action strip_symbols = STRIP_UNDEF;
  105. enum locals_action
  106. {
  107. LOCALS_UNDEF,
  108. LOCALS_START_L, /* Discard locals starting with L. */
  109. LOCALS_ALL /* Discard all locals. */
  110. };
  111. /* Which local symbols to remove. Overrides STRIP_ALL. */
  112. static enum locals_action discard_locals;
  113. /* Structure used to hold lists of sections and actions to take. */
  114. struct section_list
  115. {
  116. struct section_list *next; /* Next section to change. */
  117. const char *pattern; /* Section name pattern. */
  118. bool used; /* Whether this entry was used. */
  119. unsigned int context; /* What to do with matching sections. */
  120. /* Flag bits used in the context field.
  121. COPY and REMOVE are mutually exlusive.
  122. SET and ALTER are mutually exclusive. */
  123. #define SECTION_CONTEXT_REMOVE (1 << 0) /* Remove this section. */
  124. #define SECTION_CONTEXT_COPY (1 << 1) /* Copy this section, delete all non-copied section. */
  125. #define SECTION_CONTEXT_KEEP (1 << 2) /* Keep this section. */
  126. #define SECTION_CONTEXT_SET_VMA (1 << 3) /* Set the sections' VMA address. */
  127. #define SECTION_CONTEXT_ALTER_VMA (1 << 4) /* Increment or decrement the section's VMA address. */
  128. #define SECTION_CONTEXT_SET_LMA (1 << 5) /* Set the sections' LMA address. */
  129. #define SECTION_CONTEXT_ALTER_LMA (1 << 6) /* Increment or decrement the section's LMA address. */
  130. #define SECTION_CONTEXT_SET_FLAGS (1 << 7) /* Set the section's flags. */
  131. #define SECTION_CONTEXT_REMOVE_RELOCS (1 << 8) /* Remove relocations for this section. */
  132. #define SECTION_CONTEXT_SET_ALIGNMENT (1 << 9) /* Set alignment for section. */
  133. bfd_vma vma_val; /* Amount to change by or set to. */
  134. bfd_vma lma_val; /* Amount to change by or set to. */
  135. flagword flags; /* What to set the section flags to. */
  136. unsigned int alignment; /* Alignment of output section. */
  137. };
  138. static struct section_list *change_sections;
  139. /* TRUE if some sections are to be removed. */
  140. static bool sections_removed;
  141. /* TRUE if only some sections are to be copied. */
  142. static bool sections_copied;
  143. /* Changes to the start address. */
  144. static bfd_vma change_start = 0;
  145. static bool set_start_set = false;
  146. static bfd_vma set_start;
  147. /* Changes to section addresses. */
  148. static bfd_vma change_section_address = 0;
  149. /* Filling gaps between sections. */
  150. static bool gap_fill_set = false;
  151. static bfd_byte gap_fill = 0;
  152. /* Pad to a given address. */
  153. static bool pad_to_set = false;
  154. static bfd_vma pad_to;
  155. /* Use alternative machine code? */
  156. static unsigned long use_alt_mach_code = 0;
  157. /* Output BFD flags user wants to set or clear */
  158. static flagword bfd_flags_to_set;
  159. static flagword bfd_flags_to_clear;
  160. /* List of sections to add. */
  161. struct section_add
  162. {
  163. /* Next section to add. */
  164. struct section_add *next;
  165. /* Name of section to add. */
  166. const char *name;
  167. /* Name of file holding section contents. */
  168. const char *filename;
  169. /* Size of file. */
  170. size_t size;
  171. /* Contents of file. */
  172. bfd_byte *contents;
  173. /* BFD section, after it has been added. */
  174. asection *section;
  175. };
  176. /* List of sections to add to the output BFD. */
  177. static struct section_add *add_sections;
  178. /* List of sections to update in the output BFD. */
  179. static struct section_add *update_sections;
  180. /* List of sections to dump from the output BFD. */
  181. static struct section_add *dump_sections;
  182. /* If non-NULL the argument to --add-gnu-debuglink.
  183. This should be the filename to store in the .gnu_debuglink section. */
  184. static const char * gnu_debuglink_filename = NULL;
  185. /* Whether to convert debugging information. */
  186. static bool convert_debugging = false;
  187. /* Whether to compress/decompress DWARF debug sections. */
  188. static enum
  189. {
  190. nothing = 0,
  191. compress = 1 << 0,
  192. compress_zlib = compress | 1 << 1,
  193. compress_gnu_zlib = compress | 1 << 2,
  194. compress_gabi_zlib = compress | 1 << 3,
  195. decompress = 1 << 4
  196. } do_debug_sections = nothing;
  197. /* Whether to generate ELF common symbols with the STT_COMMON type. */
  198. static enum bfd_link_elf_stt_common do_elf_stt_common = unchanged;
  199. /* Whether to change the leading character in symbol names. */
  200. static bool change_leading_char = false;
  201. /* Whether to remove the leading character from global symbol names. */
  202. static bool remove_leading_char = false;
  203. /* Whether to permit wildcard in symbol comparison. */
  204. static bool wildcard = false;
  205. /* True if --localize-hidden is in effect. */
  206. static bool localize_hidden = false;
  207. /* List of symbols to strip, keep, localize, keep-global, weaken,
  208. or redefine. */
  209. static htab_t strip_specific_htab = NULL;
  210. static htab_t strip_unneeded_htab = NULL;
  211. static htab_t keep_specific_htab = NULL;
  212. static htab_t localize_specific_htab = NULL;
  213. static htab_t globalize_specific_htab = NULL;
  214. static htab_t keepglobal_specific_htab = NULL;
  215. static htab_t weaken_specific_htab = NULL;
  216. static htab_t redefine_specific_htab = NULL;
  217. static htab_t redefine_specific_reverse_htab = NULL;
  218. static struct addsym_node *add_sym_list = NULL, **add_sym_tail = &add_sym_list;
  219. static int add_symbols = 0;
  220. static char *strip_specific_buffer = NULL;
  221. static char *strip_unneeded_buffer = NULL;
  222. static char *keep_specific_buffer = NULL;
  223. static char *localize_specific_buffer = NULL;
  224. static char *globalize_specific_buffer = NULL;
  225. static char *keepglobal_specific_buffer = NULL;
  226. static char *weaken_specific_buffer = NULL;
  227. /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */
  228. static bool weaken = false;
  229. /* If this is TRUE, we retain BSF_FILE symbols. */
  230. static bool keep_file_symbols = false;
  231. /* Prefix symbols/sections. */
  232. static char *prefix_symbols_string = 0;
  233. static char *prefix_sections_string = 0;
  234. static char *prefix_alloc_sections_string = 0;
  235. /* True if --extract-symbol was passed on the command line. */
  236. static bool extract_symbol = false;
  237. /* If `reverse_bytes' is nonzero, then reverse the order of every chunk
  238. of <reverse_bytes> bytes within each output section. */
  239. static int reverse_bytes = 0;
  240. /* For Coff objects, we may want to allow or disallow long section names,
  241. or preserve them where found in the inputs. Debug info relies on them. */
  242. enum long_section_name_handling
  243. {
  244. DISABLE,
  245. ENABLE,
  246. KEEP
  247. };
  248. /* The default long section handling mode is to preserve them.
  249. This is also the only behaviour for 'strip'. */
  250. static enum long_section_name_handling long_section_names = KEEP;
  251. /* 150 isn't special; it's just an arbitrary non-ASCII char value. */
  252. enum command_line_switch
  253. {
  254. OPTION_ADD_SECTION=150,
  255. OPTION_ADD_GNU_DEBUGLINK,
  256. OPTION_ADD_SYMBOL,
  257. OPTION_ALT_MACH_CODE,
  258. OPTION_CHANGE_ADDRESSES,
  259. OPTION_CHANGE_LEADING_CHAR,
  260. OPTION_CHANGE_SECTION_ADDRESS,
  261. OPTION_CHANGE_SECTION_LMA,
  262. OPTION_CHANGE_SECTION_VMA,
  263. OPTION_CHANGE_START,
  264. OPTION_CHANGE_WARNINGS,
  265. OPTION_COMPRESS_DEBUG_SECTIONS,
  266. OPTION_DEBUGGING,
  267. OPTION_DECOMPRESS_DEBUG_SECTIONS,
  268. OPTION_DUMP_SECTION,
  269. OPTION_ELF_STT_COMMON,
  270. OPTION_EXTRACT_DWO,
  271. OPTION_EXTRACT_SYMBOL,
  272. OPTION_FILE_ALIGNMENT,
  273. OPTION_FORMATS_INFO,
  274. OPTION_GAP_FILL,
  275. OPTION_GLOBALIZE_SYMBOL,
  276. OPTION_GLOBALIZE_SYMBOLS,
  277. OPTION_HEAP,
  278. OPTION_IMAGE_BASE,
  279. OPTION_IMPURE,
  280. OPTION_INTERLEAVE_WIDTH,
  281. OPTION_KEEPGLOBAL_SYMBOLS,
  282. OPTION_KEEP_FILE_SYMBOLS,
  283. OPTION_KEEP_SECTION,
  284. OPTION_KEEP_SYMBOLS,
  285. OPTION_KEEP_SECTION_SYMBOLS,
  286. OPTION_LOCALIZE_HIDDEN,
  287. OPTION_LOCALIZE_SYMBOLS,
  288. OPTION_LONG_SECTION_NAMES,
  289. OPTION_MERGE_NOTES,
  290. OPTION_NO_MERGE_NOTES,
  291. OPTION_NO_CHANGE_WARNINGS,
  292. OPTION_ONLY_KEEP_DEBUG,
  293. OPTION_PAD_TO,
  294. OPTION_PREFIX_ALLOC_SECTIONS,
  295. OPTION_PREFIX_SECTIONS,
  296. OPTION_PREFIX_SYMBOLS,
  297. OPTION_PURE,
  298. OPTION_READONLY_TEXT,
  299. OPTION_REDEFINE_SYM,
  300. OPTION_REDEFINE_SYMS,
  301. OPTION_REMOVE_LEADING_CHAR,
  302. OPTION_REMOVE_RELOCS,
  303. OPTION_RENAME_SECTION,
  304. OPTION_REVERSE_BYTES,
  305. OPTION_PE_SECTION_ALIGNMENT,
  306. OPTION_SET_SECTION_FLAGS,
  307. OPTION_SET_SECTION_ALIGNMENT,
  308. OPTION_SET_START,
  309. OPTION_SREC_FORCES3,
  310. OPTION_SREC_LEN,
  311. OPTION_STACK,
  312. OPTION_STRIP_DWO,
  313. OPTION_STRIP_SYMBOLS,
  314. OPTION_STRIP_UNNEEDED,
  315. OPTION_STRIP_UNNEEDED_SYMBOL,
  316. OPTION_STRIP_UNNEEDED_SYMBOLS,
  317. OPTION_SUBSYSTEM,
  318. OPTION_UPDATE_SECTION,
  319. OPTION_VERILOG_DATA_WIDTH,
  320. OPTION_WEAKEN,
  321. OPTION_WEAKEN_SYMBOLS,
  322. OPTION_WRITABLE_TEXT
  323. };
  324. /* Options to handle if running as "strip". */
  325. static struct option strip_options[] =
  326. {
  327. {"disable-deterministic-archives", no_argument, 0, 'U'},
  328. {"discard-all", no_argument, 0, 'x'},
  329. {"discard-locals", no_argument, 0, 'X'},
  330. {"enable-deterministic-archives", no_argument, 0, 'D'},
  331. {"format", required_argument, 0, 'F'}, /* Obsolete */
  332. {"help", no_argument, 0, 'h'},
  333. {"info", no_argument, 0, OPTION_FORMATS_INFO},
  334. {"input-format", required_argument, 0, 'I'}, /* Obsolete */
  335. {"input-target", required_argument, 0, 'I'},
  336. {"keep-section-symbols", no_argument, 0, OPTION_KEEP_SECTION_SYMBOLS},
  337. {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
  338. {"keep-section", required_argument, 0, OPTION_KEEP_SECTION},
  339. {"keep-symbol", required_argument, 0, 'K'},
  340. {"merge-notes", no_argument, 0, 'M'},
  341. {"no-merge-notes", no_argument, 0, OPTION_NO_MERGE_NOTES},
  342. {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
  343. {"output-file", required_argument, 0, 'o'},
  344. {"output-format", required_argument, 0, 'O'}, /* Obsolete */
  345. {"output-target", required_argument, 0, 'O'},
  346. {"preserve-dates", no_argument, 0, 'p'},
  347. {"remove-section", required_argument, 0, 'R'},
  348. {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS},
  349. {"strip-all", no_argument, 0, 's'},
  350. {"strip-debug", no_argument, 0, 'S'},
  351. {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
  352. {"strip-symbol", required_argument, 0, 'N'},
  353. {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
  354. {"target", required_argument, 0, 'F'},
  355. {"verbose", no_argument, 0, 'v'},
  356. {"version", no_argument, 0, 'V'},
  357. {"wildcard", no_argument, 0, 'w'},
  358. {0, no_argument, 0, 0}
  359. };
  360. /* Options to handle if running as "objcopy". */
  361. static struct option copy_options[] =
  362. {
  363. {"add-gnu-debuglink", required_argument, 0, OPTION_ADD_GNU_DEBUGLINK},
  364. {"add-section", required_argument, 0, OPTION_ADD_SECTION},
  365. {"add-symbol", required_argument, 0, OPTION_ADD_SYMBOL},
  366. {"adjust-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
  367. {"adjust-start", required_argument, 0, OPTION_CHANGE_START},
  368. {"adjust-vma", required_argument, 0, OPTION_CHANGE_ADDRESSES},
  369. {"adjust-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
  370. {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE},
  371. {"binary-architecture", required_argument, 0, 'B'},
  372. {"byte", required_argument, 0, 'b'},
  373. {"change-addresses", required_argument, 0, OPTION_CHANGE_ADDRESSES},
  374. {"change-leading-char", no_argument, 0, OPTION_CHANGE_LEADING_CHAR},
  375. {"change-section-address", required_argument, 0, OPTION_CHANGE_SECTION_ADDRESS},
  376. {"change-section-lma", required_argument, 0, OPTION_CHANGE_SECTION_LMA},
  377. {"change-section-vma", required_argument, 0, OPTION_CHANGE_SECTION_VMA},
  378. {"change-start", required_argument, 0, OPTION_CHANGE_START},
  379. {"change-warnings", no_argument, 0, OPTION_CHANGE_WARNINGS},
  380. {"compress-debug-sections", optional_argument, 0, OPTION_COMPRESS_DEBUG_SECTIONS},
  381. {"debugging", no_argument, 0, OPTION_DEBUGGING},
  382. {"decompress-debug-sections", no_argument, 0, OPTION_DECOMPRESS_DEBUG_SECTIONS},
  383. {"disable-deterministic-archives", no_argument, 0, 'U'},
  384. {"discard-all", no_argument, 0, 'x'},
  385. {"discard-locals", no_argument, 0, 'X'},
  386. {"dump-section", required_argument, 0, OPTION_DUMP_SECTION},
  387. {"elf-stt-common", required_argument, 0, OPTION_ELF_STT_COMMON},
  388. {"enable-deterministic-archives", no_argument, 0, 'D'},
  389. {"extract-dwo", no_argument, 0, OPTION_EXTRACT_DWO},
  390. {"extract-symbol", no_argument, 0, OPTION_EXTRACT_SYMBOL},
  391. {"file-alignment", required_argument, 0, OPTION_FILE_ALIGNMENT},
  392. {"format", required_argument, 0, 'F'}, /* Obsolete */
  393. {"gap-fill", required_argument, 0, OPTION_GAP_FILL},
  394. {"globalize-symbol", required_argument, 0, OPTION_GLOBALIZE_SYMBOL},
  395. {"globalize-symbols", required_argument, 0, OPTION_GLOBALIZE_SYMBOLS},
  396. {"heap", required_argument, 0, OPTION_HEAP},
  397. {"help", no_argument, 0, 'h'},
  398. {"image-base", required_argument, 0 , OPTION_IMAGE_BASE},
  399. {"impure", no_argument, 0, OPTION_IMPURE},
  400. {"info", no_argument, 0, OPTION_FORMATS_INFO},
  401. {"input-format", required_argument, 0, 'I'}, /* Obsolete */
  402. {"input-target", required_argument, 0, 'I'},
  403. {"interleave", optional_argument, 0, 'i'},
  404. {"interleave-width", required_argument, 0, OPTION_INTERLEAVE_WIDTH},
  405. {"keep-file-symbols", no_argument, 0, OPTION_KEEP_FILE_SYMBOLS},
  406. {"keep-global-symbol", required_argument, 0, 'G'},
  407. {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS},
  408. {"keep-section", required_argument, 0, OPTION_KEEP_SECTION},
  409. {"keep-symbol", required_argument, 0, 'K'},
  410. {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS},
  411. {"keep-section-symbols", required_argument, 0, OPTION_KEEP_SECTION_SYMBOLS},
  412. {"localize-hidden", no_argument, 0, OPTION_LOCALIZE_HIDDEN},
  413. {"localize-symbol", required_argument, 0, 'L'},
  414. {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS},
  415. {"long-section-names", required_argument, 0, OPTION_LONG_SECTION_NAMES},
  416. {"merge-notes", no_argument, 0, 'M'},
  417. {"no-merge-notes", no_argument, 0, OPTION_NO_MERGE_NOTES},
  418. {"no-adjust-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
  419. {"no-change-warnings", no_argument, 0, OPTION_NO_CHANGE_WARNINGS},
  420. {"only-keep-debug", no_argument, 0, OPTION_ONLY_KEEP_DEBUG},
  421. {"only-section", required_argument, 0, 'j'},
  422. {"output-format", required_argument, 0, 'O'}, /* Obsolete */
  423. {"output-target", required_argument, 0, 'O'},
  424. {"pad-to", required_argument, 0, OPTION_PAD_TO},
  425. {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS},
  426. {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS},
  427. {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS},
  428. {"preserve-dates", no_argument, 0, 'p'},
  429. {"pure", no_argument, 0, OPTION_PURE},
  430. {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
  431. {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
  432. {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
  433. {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
  434. {"remove-section", required_argument, 0, 'R'},
  435. {"remove-relocations", required_argument, 0, OPTION_REMOVE_RELOCS},
  436. {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
  437. {"reverse-bytes", required_argument, 0, OPTION_REVERSE_BYTES},
  438. {"section-alignment", required_argument, 0, OPTION_PE_SECTION_ALIGNMENT},
  439. {"set-section-flags", required_argument, 0, OPTION_SET_SECTION_FLAGS},
  440. {"set-section-alignment", required_argument, 0, OPTION_SET_SECTION_ALIGNMENT},
  441. {"set-start", required_argument, 0, OPTION_SET_START},
  442. {"srec-forceS3", no_argument, 0, OPTION_SREC_FORCES3},
  443. {"srec-len", required_argument, 0, OPTION_SREC_LEN},
  444. {"stack", required_argument, 0, OPTION_STACK},
  445. {"strip-all", no_argument, 0, 'S'},
  446. {"strip-debug", no_argument, 0, 'g'},
  447. {"strip-dwo", no_argument, 0, OPTION_STRIP_DWO},
  448. {"strip-symbol", required_argument, 0, 'N'},
  449. {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS},
  450. {"strip-unneeded", no_argument, 0, OPTION_STRIP_UNNEEDED},
  451. {"strip-unneeded-symbol", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOL},
  452. {"strip-unneeded-symbols", required_argument, 0, OPTION_STRIP_UNNEEDED_SYMBOLS},
  453. {"subsystem", required_argument, 0, OPTION_SUBSYSTEM},
  454. {"target", required_argument, 0, 'F'},
  455. {"update-section", required_argument, 0, OPTION_UPDATE_SECTION},
  456. {"verbose", no_argument, 0, 'v'},
  457. {"verilog-data-width", required_argument, 0, OPTION_VERILOG_DATA_WIDTH},
  458. {"version", no_argument, 0, 'V'},
  459. {"weaken", no_argument, 0, OPTION_WEAKEN},
  460. {"weaken-symbol", required_argument, 0, 'W'},
  461. {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS},
  462. {"wildcard", no_argument, 0, 'w'},
  463. {"writable-text", no_argument, 0, OPTION_WRITABLE_TEXT},
  464. {0, no_argument, 0, 0}
  465. };
  466. /* IMPORTS */
  467. extern char *program_name;
  468. /* This flag distinguishes between strip and objcopy:
  469. 1 means this is 'strip'; 0 means this is 'objcopy'.
  470. -1 means if we should use argv[0] to decide. */
  471. extern int is_strip;
  472. /* The maximum length of an S record. This variable is defined in srec.c
  473. and can be modified by the --srec-len parameter. */
  474. extern unsigned int _bfd_srec_len;
  475. /* Restrict the generation of Srecords to type S3 only.
  476. This variable is defined in bfd/srec.c and can be toggled
  477. on by the --srec-forceS3 command line switch. */
  478. extern bool _bfd_srec_forceS3;
  479. /* Width of data in bytes for verilog output.
  480. This variable is declared in bfd/verilog.c and can be modified by
  481. the --verilog-data-width parameter. */
  482. extern unsigned int VerilogDataWidth;
  483. /* Forward declarations. */
  484. static void setup_section (bfd *, asection *, void *);
  485. static void setup_bfd_headers (bfd *, bfd *);
  486. static void copy_relocations_in_section (bfd *, asection *, void *);
  487. static void copy_section (bfd *, asection *, void *);
  488. static void get_sections (bfd *, asection *, void *);
  489. static int compare_section_lma (const void *, const void *);
  490. static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
  491. static bool write_debugging_info (bfd *, void *, long *, asymbol ***);
  492. static const char *lookup_sym_redefinition (const char *);
  493. static const char *find_section_rename (const char *, flagword *);
  494. ATTRIBUTE_NORETURN static void
  495. copy_usage (FILE *stream, int exit_status)
  496. {
  497. fprintf (stream, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
  498. fprintf (stream, _(" Copies a binary file, possibly transforming it in the process\n"));
  499. fprintf (stream, _(" The options are:\n"));
  500. fprintf (stream, _("\
  501. -I --input-target <bfdname> Assume input file is in format <bfdname>\n\
  502. -O --output-target <bfdname> Create an output file in format <bfdname>\n\
  503. -B --binary-architecture <arch> Set output arch, when input is arch-less\n\
  504. -F --target <bfdname> Set both input and output format to <bfdname>\n\
  505. --debugging Convert debugging information, if possible\n\
  506. -p --preserve-dates Copy modified/access timestamps to the output\n"));
  507. if (DEFAULT_AR_DETERMINISTIC)
  508. fprintf (stream, _("\
  509. -D --enable-deterministic-archives\n\
  510. Produce deterministic output when stripping archives (default)\n\
  511. -U --disable-deterministic-archives\n\
  512. Disable -D behavior\n"));
  513. else
  514. fprintf (stream, _("\
  515. -D --enable-deterministic-archives\n\
  516. Produce deterministic output when stripping archives\n\
  517. -U --disable-deterministic-archives\n\
  518. Disable -D behavior (default)\n"));
  519. fprintf (stream, _("\
  520. -j --only-section <name> Only copy section <name> into the output\n\
  521. --add-gnu-debuglink=<file> Add section .gnu_debuglink linking to <file>\n\
  522. -R --remove-section <name> Remove section <name> from the output\n\
  523. --remove-relocations <name> Remove relocations from section <name>\n\
  524. -S --strip-all Remove all symbol and relocation information\n\
  525. -g --strip-debug Remove all debugging symbols & sections\n\
  526. --strip-dwo Remove all DWO sections\n\
  527. --strip-unneeded Remove all symbols not needed by relocations\n\
  528. -N --strip-symbol <name> Do not copy symbol <name>\n\
  529. --strip-unneeded-symbol <name>\n\
  530. Do not copy symbol <name> unless needed by\n\
  531. relocations\n\
  532. --only-keep-debug Strip everything but the debug information\n\
  533. --extract-dwo Copy only DWO sections\n\
  534. --extract-symbol Remove section contents but keep symbols\n\
  535. --keep-section <name> Do not strip section <name>\n\
  536. -K --keep-symbol <name> Do not strip symbol <name>\n\
  537. --keep-section-symbols Do not strip section symbols\n\
  538. --keep-file-symbols Do not strip file symbol(s)\n\
  539. --localize-hidden Turn all ELF hidden symbols into locals\n\
  540. -L --localize-symbol <name> Force symbol <name> to be marked as a local\n\
  541. --globalize-symbol <name> Force symbol <name> to be marked as a global\n\
  542. -G --keep-global-symbol <name> Localize all symbols except <name>\n\
  543. -W --weaken-symbol <name> Force symbol <name> to be marked as a weak\n\
  544. --weaken Force all global symbols to be marked as weak\n\
  545. -w --wildcard Permit wildcard in symbol comparison\n\
  546. -x --discard-all Remove all non-global symbols\n\
  547. -X --discard-locals Remove any compiler-generated symbols\n\
  548. -i --interleave[=<number>] Only copy N out of every <number> bytes\n\
  549. --interleave-width <number> Set N for --interleave\n\
  550. -b --byte <num> Select byte <num> in every interleaved block\n\
  551. --gap-fill <val> Fill gaps between sections with <val>\n\
  552. --pad-to <addr> Pad the last section up to address <addr>\n\
  553. --set-start <addr> Set the start address to <addr>\n\
  554. {--change-start|--adjust-start} <incr>\n\
  555. Add <incr> to the start address\n\
  556. {--change-addresses|--adjust-vma} <incr>\n\
  557. Add <incr> to LMA, VMA and start addresses\n\
  558. {--change-section-address|--adjust-section-vma} <name>{=|+|-}<val>\n\
  559. Change LMA and VMA of section <name> by <val>\n\
  560. --change-section-lma <name>{=|+|-}<val>\n\
  561. Change the LMA of section <name> by <val>\n\
  562. --change-section-vma <name>{=|+|-}<val>\n\
  563. Change the VMA of section <name> by <val>\n\
  564. {--[no-]change-warnings|--[no-]adjust-warnings}\n\
  565. Warn if a named section does not exist\n\
  566. --set-section-flags <name>=<flags>\n\
  567. Set section <name>'s properties to <flags>\n\
  568. --set-section-alignment <name>=<align>\n\
  569. Set section <name>'s alignment to <align> bytes\n\
  570. --add-section <name>=<file> Add section <name> found in <file> to output\n\
  571. --update-section <name>=<file>\n\
  572. Update contents of section <name> with\n\
  573. contents found in <file>\n\
  574. --dump-section <name>=<file> Dump the contents of section <name> into <file>\n\
  575. --rename-section <old>=<new>[,<flags>] Rename section <old> to <new>\n\
  576. --long-section-names {enable|disable|keep}\n\
  577. Handle long section names in Coff objects.\n\
  578. --change-leading-char Force output format's leading character style\n\
  579. --remove-leading-char Remove leading character from global symbols\n\
  580. --reverse-bytes=<num> Reverse <num> bytes at a time, in output sections with content\n\
  581. --redefine-sym <old>=<new> Redefine symbol name <old> to <new>\n\
  582. --redefine-syms <file> --redefine-sym for all symbol pairs \n\
  583. listed in <file>\n\
  584. --srec-len <number> Restrict the length of generated Srecords\n\
  585. --srec-forceS3 Restrict the type of generated Srecords to S3\n\
  586. --strip-symbols <file> -N for all symbols listed in <file>\n\
  587. --strip-unneeded-symbols <file>\n\
  588. --strip-unneeded-symbol for all symbols listed\n\
  589. in <file>\n\
  590. --keep-symbols <file> -K for all symbols listed in <file>\n\
  591. --localize-symbols <file> -L for all symbols listed in <file>\n\
  592. --globalize-symbols <file> --globalize-symbol for all in <file>\n\
  593. --keep-global-symbols <file> -G for all symbols listed in <file>\n\
  594. --weaken-symbols <file> -W for all symbols listed in <file>\n\
  595. --add-symbol <name>=[<section>:]<value>[,<flags>] Add a symbol\n\
  596. --alt-machine-code <index> Use the target's <index>'th alternative machine\n\
  597. --writable-text Mark the output text as writable\n\
  598. --readonly-text Make the output text write protected\n\
  599. --pure Mark the output file as demand paged\n\
  600. --impure Mark the output file as impure\n\
  601. --prefix-symbols <prefix> Add <prefix> to start of every symbol name\n\
  602. --prefix-sections <prefix> Add <prefix> to start of every section name\n\
  603. --prefix-alloc-sections <prefix>\n\
  604. Add <prefix> to start of every allocatable\n\
  605. section name\n\
  606. --file-alignment <num> Set PE file alignment to <num>\n\
  607. --heap <reserve>[,<commit>] Set PE reserve/commit heap to <reserve>/\n\
  608. <commit>\n\
  609. --image-base <address> Set PE image base to <address>\n\
  610. --section-alignment <num> Set PE section alignment to <num>\n\
  611. --stack <reserve>[,<commit>] Set PE reserve/commit stack to <reserve>/\n\
  612. <commit>\n\
  613. --subsystem <name>[:<version>]\n\
  614. Set PE subsystem to <name> [& <version>]\n\
  615. --compress-debug-sections[={none|zlib|zlib-gnu|zlib-gabi}]\n\
  616. Compress DWARF debug sections using zlib\n\
  617. --decompress-debug-sections Decompress DWARF debug sections using zlib\n\
  618. --elf-stt-common=[yes|no] Generate ELF common symbols with STT_COMMON\n\
  619. type\n\
  620. --verilog-data-width <number> Specifies data width, in bytes, for verilog output\n\
  621. -M --merge-notes Remove redundant entries in note sections\n\
  622. --no-merge-notes Do not attempt to remove redundant notes (default)\n\
  623. -v --verbose List all object files modified\n\
  624. @<file> Read options from <file>\n\
  625. -V --version Display this program's version number\n\
  626. -h --help Display this output\n\
  627. --info List object formats & architectures supported\n\
  628. "));
  629. list_supported_targets (program_name, stream);
  630. if (REPORT_BUGS_TO[0] && exit_status == 0)
  631. fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  632. exit (exit_status);
  633. }
  634. ATTRIBUTE_NORETURN static void
  635. strip_usage (FILE *stream, int exit_status)
  636. {
  637. fprintf (stream, _("Usage: %s <option(s)> in-file(s)\n"), program_name);
  638. fprintf (stream, _(" Removes symbols and sections from files\n"));
  639. fprintf (stream, _(" The options are:\n"));
  640. fprintf (stream, _("\
  641. -I --input-target=<bfdname> Assume input file is in format <bfdname>\n\
  642. -O --output-target=<bfdname> Create an output file in format <bfdname>\n\
  643. -F --target=<bfdname> Set both input and output format to <bfdname>\n\
  644. -p --preserve-dates Copy modified/access timestamps to the output\n\
  645. "));
  646. if (DEFAULT_AR_DETERMINISTIC)
  647. fprintf (stream, _("\
  648. -D --enable-deterministic-archives\n\
  649. Produce deterministic output when stripping archives (default)\n\
  650. -U --disable-deterministic-archives\n\
  651. Disable -D behavior\n"));
  652. else
  653. fprintf (stream, _("\
  654. -D --enable-deterministic-archives\n\
  655. Produce deterministic output when stripping archives\n\
  656. -U --disable-deterministic-archives\n\
  657. Disable -D behavior (default)\n"));
  658. fprintf (stream, _("\
  659. -R --remove-section=<name> Also remove section <name> from the output\n\
  660. --remove-relocations <name> Remove relocations from section <name>\n\
  661. -s --strip-all Remove all symbol and relocation information\n\
  662. -g -S -d --strip-debug Remove all debugging symbols & sections\n\
  663. --strip-dwo Remove all DWO sections\n\
  664. --strip-unneeded Remove all symbols not needed by relocations\n\
  665. --only-keep-debug Strip everything but the debug information\n\
  666. -M --merge-notes Remove redundant entries in note sections (default)\n\
  667. --no-merge-notes Do not attempt to remove redundant notes\n\
  668. -N --strip-symbol=<name> Do not copy symbol <name>\n\
  669. --keep-section=<name> Do not strip section <name>\n\
  670. -K --keep-symbol=<name> Do not strip symbol <name>\n\
  671. --keep-section-symbols Do not strip section symbols\n\
  672. --keep-file-symbols Do not strip file symbol(s)\n\
  673. -w --wildcard Permit wildcard in symbol comparison\n\
  674. -x --discard-all Remove all non-global symbols\n\
  675. -X --discard-locals Remove any compiler-generated symbols\n\
  676. -v --verbose List all object files modified\n\
  677. -V --version Display this program's version number\n\
  678. -h --help Display this output\n\
  679. --info List object formats & architectures supported\n\
  680. -o <file> Place stripped output into <file>\n\
  681. "));
  682. list_supported_targets (program_name, stream);
  683. if (REPORT_BUGS_TO[0] && exit_status == 0)
  684. fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  685. exit (exit_status);
  686. }
  687. /* Parse section flags into a flagword, with a fatal error if the
  688. string can't be parsed. */
  689. static flagword
  690. parse_flags (const char *s)
  691. {
  692. flagword ret;
  693. const char *snext;
  694. int len;
  695. ret = SEC_NO_FLAGS;
  696. do
  697. {
  698. snext = strchr (s, ',');
  699. if (snext == NULL)
  700. len = strlen (s);
  701. else
  702. {
  703. len = snext - s;
  704. ++snext;
  705. }
  706. if (0) ;
  707. #define PARSE_FLAG(fname,fval) \
  708. else if (strncasecmp (fname, s, len) == 0) ret |= fval
  709. PARSE_FLAG ("alloc", SEC_ALLOC);
  710. PARSE_FLAG ("load", SEC_LOAD);
  711. PARSE_FLAG ("noload", SEC_NEVER_LOAD);
  712. PARSE_FLAG ("readonly", SEC_READONLY);
  713. PARSE_FLAG ("debug", SEC_DEBUGGING);
  714. PARSE_FLAG ("code", SEC_CODE);
  715. PARSE_FLAG ("data", SEC_DATA);
  716. PARSE_FLAG ("rom", SEC_ROM);
  717. PARSE_FLAG ("exclude", SEC_EXCLUDE);
  718. PARSE_FLAG ("share", SEC_COFF_SHARED);
  719. PARSE_FLAG ("contents", SEC_HAS_CONTENTS);
  720. PARSE_FLAG ("merge", SEC_MERGE);
  721. PARSE_FLAG ("strings", SEC_STRINGS);
  722. #undef PARSE_FLAG
  723. else
  724. {
  725. char *copy;
  726. copy = (char *) xmalloc (len + 1);
  727. strncpy (copy, s, len);
  728. copy[len] = '\0';
  729. non_fatal (_("unrecognized section flag `%s'"), copy);
  730. fatal (_("supported flags: %s"),
  731. "alloc, load, noload, readonly, debug, code, data, rom, exclude, share, contents, merge, strings");
  732. }
  733. s = snext;
  734. }
  735. while (s != NULL);
  736. return ret;
  737. }
  738. /* Parse symbol flags into a flagword, with a fatal error if the
  739. string can't be parsed. */
  740. static flagword
  741. parse_symflags (const char *s, const char **other)
  742. {
  743. flagword ret;
  744. const char *snext;
  745. size_t len;
  746. ret = BSF_NO_FLAGS;
  747. do
  748. {
  749. snext = strchr (s, ',');
  750. if (snext == NULL)
  751. len = strlen (s);
  752. else
  753. {
  754. len = snext - s;
  755. ++snext;
  756. }
  757. #define PARSE_FLAG(fname, fval) \
  758. else if (len == sizeof fname - 1 \
  759. && strncasecmp (fname, s, len) == 0) \
  760. ret |= fval
  761. #define PARSE_OTHER(fname, fval) \
  762. else if (len >= sizeof fname \
  763. && strncasecmp (fname, s, sizeof fname - 1) == 0) \
  764. fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1)
  765. if (0) ;
  766. PARSE_FLAG ("local", BSF_LOCAL);
  767. PARSE_FLAG ("global", BSF_GLOBAL);
  768. PARSE_FLAG ("export", BSF_EXPORT);
  769. PARSE_FLAG ("debug", BSF_DEBUGGING);
  770. PARSE_FLAG ("function", BSF_FUNCTION);
  771. PARSE_FLAG ("weak", BSF_WEAK);
  772. PARSE_FLAG ("section", BSF_SECTION_SYM);
  773. PARSE_FLAG ("constructor", BSF_CONSTRUCTOR);
  774. PARSE_FLAG ("warning", BSF_WARNING);
  775. PARSE_FLAG ("indirect", BSF_INDIRECT);
  776. PARSE_FLAG ("file", BSF_FILE);
  777. PARSE_FLAG ("object", BSF_OBJECT);
  778. PARSE_FLAG ("synthetic", BSF_SYNTHETIC);
  779. PARSE_FLAG ("indirect-function", BSF_GNU_INDIRECT_FUNCTION | BSF_FUNCTION);
  780. PARSE_FLAG ("unique-object", BSF_GNU_UNIQUE | BSF_OBJECT);
  781. PARSE_OTHER ("before=", *other);
  782. #undef PARSE_FLAG
  783. #undef PARSE_OTHER
  784. else
  785. {
  786. char *copy;
  787. copy = (char *) xmalloc (len + 1);
  788. strncpy (copy, s, len);
  789. copy[len] = '\0';
  790. non_fatal (_("unrecognized symbol flag `%s'"), copy);
  791. fatal (_("supported flags: %s"),
  792. "local, global, export, debug, function, weak, section, "
  793. "constructor, warning, indirect, file, object, synthetic, "
  794. "indirect-function, unique-object, before=<othersym>");
  795. }
  796. s = snext;
  797. }
  798. while (s != NULL);
  799. return ret;
  800. }
  801. /* Find and optionally add an entry in the change_sections list.
  802. We need to be careful in how we match section names because of the support
  803. for wildcard characters. For example suppose that the user has invoked
  804. objcopy like this:
  805. --set-section-flags .debug_*=debug
  806. --set-section-flags .debug_str=readonly,debug
  807. --change-section-address .debug_*ranges=0x1000
  808. With the idea that all debug sections will receive the DEBUG flag, the
  809. .debug_str section will also receive the READONLY flag and the
  810. .debug_ranges and .debug_aranges sections will have their address set to
  811. 0x1000. (This may not make much sense, but it is just an example).
  812. When adding the section name patterns to the section list we need to make
  813. sure that previous entries do not match with the new entry, unless the
  814. match is exact. (In which case we assume that the user is overriding
  815. the previous entry with the new context).
  816. When matching real section names to the section list we make use of the
  817. wildcard characters, but we must do so in context. Eg if we are setting
  818. section addresses then we match for .debug_ranges but not for .debug_info.
  819. Finally, if ADD is false and we do find a match, we mark the section list
  820. entry as used. */
  821. static struct section_list *
  822. find_section_list (const char *name, bool add, unsigned int context)
  823. {
  824. struct section_list *p, *match = NULL;
  825. /* assert ((context & ((1 << 7) - 1)) != 0); */
  826. for (p = change_sections; p != NULL; p = p->next)
  827. {
  828. if (add)
  829. {
  830. if (strcmp (p->pattern, name) == 0)
  831. {
  832. /* Check for context conflicts. */
  833. if (((p->context & SECTION_CONTEXT_REMOVE)
  834. && (context & SECTION_CONTEXT_COPY))
  835. || ((context & SECTION_CONTEXT_REMOVE)
  836. && (p->context & SECTION_CONTEXT_COPY)))
  837. fatal (_("error: %s both copied and removed"), name);
  838. if (((p->context & SECTION_CONTEXT_SET_VMA)
  839. && (context & SECTION_CONTEXT_ALTER_VMA))
  840. || ((context & SECTION_CONTEXT_SET_VMA)
  841. && (context & SECTION_CONTEXT_ALTER_VMA)))
  842. fatal (_("error: %s both sets and alters VMA"), name);
  843. if (((p->context & SECTION_CONTEXT_SET_LMA)
  844. && (context & SECTION_CONTEXT_ALTER_LMA))
  845. || ((context & SECTION_CONTEXT_SET_LMA)
  846. && (context & SECTION_CONTEXT_ALTER_LMA)))
  847. fatal (_("error: %s both sets and alters LMA"), name);
  848. /* Extend the context. */
  849. p->context |= context;
  850. return p;
  851. }
  852. }
  853. /* If we are not adding a new name/pattern then
  854. only check for a match if the context applies. */
  855. else if (p->context & context)
  856. {
  857. /* We could check for the presence of wildchar characters
  858. first and choose between calling strcmp and fnmatch,
  859. but is that really worth it ? */
  860. if (p->pattern [0] == '!')
  861. {
  862. if (fnmatch (p->pattern + 1, name, 0) == 0)
  863. {
  864. p->used = true;
  865. return NULL;
  866. }
  867. }
  868. else
  869. {
  870. if (fnmatch (p->pattern, name, 0) == 0)
  871. {
  872. if (match == NULL)
  873. match = p;
  874. }
  875. }
  876. }
  877. }
  878. if (! add)
  879. {
  880. if (match != NULL)
  881. match->used = true;
  882. return match;
  883. }
  884. p = (struct section_list *) xmalloc (sizeof (struct section_list));
  885. p->pattern = name;
  886. p->used = false;
  887. p->context = context;
  888. p->vma_val = 0;
  889. p->lma_val = 0;
  890. p->flags = 0;
  891. p->alignment = 0;
  892. p->next = change_sections;
  893. change_sections = p;
  894. return p;
  895. }
  896. /* S1 is the entry node already in the table, S2 is the key node. */
  897. static int
  898. eq_string_redefnode (const void *s1, const void *s2)
  899. {
  900. struct redefine_node *node1 = (struct redefine_node *) s1;
  901. struct redefine_node *node2 = (struct redefine_node *) s2;
  902. return !strcmp ((const char *) node1->source, (const char *) node2->source);
  903. }
  904. /* P is redefine node. Hash value is generated from its "source" filed. */
  905. static hashval_t
  906. htab_hash_redefnode (const void *p)
  907. {
  908. struct redefine_node *redefnode = (struct redefine_node *) p;
  909. return htab_hash_string (redefnode->source);
  910. }
  911. /* Create hashtab used for redefine node. */
  912. static htab_t
  913. create_symbol2redef_htab (void)
  914. {
  915. return htab_create_alloc (16, htab_hash_redefnode, eq_string_redefnode, NULL,
  916. xcalloc, free);
  917. }
  918. static htab_t
  919. create_symbol_htab (void)
  920. {
  921. return htab_create_alloc (16, htab_hash_string, htab_eq_string, NULL,
  922. xcalloc, free);
  923. }
  924. static void
  925. create_symbol_htabs (void)
  926. {
  927. strip_specific_htab = create_symbol_htab ();
  928. strip_unneeded_htab = create_symbol_htab ();
  929. keep_specific_htab = create_symbol_htab ();
  930. localize_specific_htab = create_symbol_htab ();
  931. globalize_specific_htab = create_symbol_htab ();
  932. keepglobal_specific_htab = create_symbol_htab ();
  933. weaken_specific_htab = create_symbol_htab ();
  934. redefine_specific_htab = create_symbol2redef_htab ();
  935. /* As there is no bidirectional hash table in libiberty, need a reverse table
  936. to check duplicated target string. */
  937. redefine_specific_reverse_htab = create_symbol_htab ();
  938. }
  939. /* Add a symbol to strip_specific_list. */
  940. static void
  941. add_specific_symbol (const char *name, htab_t htab)
  942. {
  943. *htab_find_slot (htab, name, INSERT) = (char *) name;
  944. }
  945. /* Like add_specific_symbol, but the element type is void *. */
  946. static void
  947. add_specific_symbol_node (const void *node, htab_t htab)
  948. {
  949. *htab_find_slot (htab, node, INSERT) = (void *) node;
  950. }
  951. /* Add symbols listed in `filename' to strip_specific_list. */
  952. #define IS_WHITESPACE(c) ((c) == ' ' || (c) == '\t')
  953. #define IS_LINE_TERMINATOR(c) ((c) == '\n' || (c) == '\r' || (c) == '\0')
  954. static void
  955. add_specific_symbols (const char *filename, htab_t htab, char **buffer_p)
  956. {
  957. off_t size;
  958. FILE * f;
  959. char * line;
  960. char * buffer;
  961. unsigned int line_count;
  962. size = get_file_size (filename);
  963. if (size == 0)
  964. {
  965. status = 1;
  966. return;
  967. }
  968. buffer = (char *) xmalloc (size + 2);
  969. f = fopen (filename, FOPEN_RT);
  970. if (f == NULL)
  971. fatal (_("cannot open '%s': %s"), filename, strerror (errno));
  972. if (fread (buffer, 1, size, f) == 0 || ferror (f))
  973. fatal (_("%s: fread failed"), filename);
  974. fclose (f);
  975. buffer [size] = '\n';
  976. buffer [size + 1] = '\0';
  977. line_count = 1;
  978. for (line = buffer; * line != '\0'; line ++)
  979. {
  980. char * eol;
  981. char * name;
  982. char * name_end;
  983. int finished = false;
  984. for (eol = line;; eol ++)
  985. {
  986. switch (* eol)
  987. {
  988. case '\n':
  989. * eol = '\0';
  990. /* Cope with \n\r. */
  991. if (eol[1] == '\r')
  992. ++ eol;
  993. finished = true;
  994. break;
  995. case '\r':
  996. * eol = '\0';
  997. /* Cope with \r\n. */
  998. if (eol[1] == '\n')
  999. ++ eol;
  1000. finished = true;
  1001. break;
  1002. case 0:
  1003. finished = true;
  1004. break;
  1005. case '#':
  1006. /* Line comment, Terminate the line here, in case a
  1007. name is present and then allow the rest of the
  1008. loop to find the real end of the line. */
  1009. * eol = '\0';
  1010. break;
  1011. default:
  1012. break;
  1013. }
  1014. if (finished)
  1015. break;
  1016. }
  1017. /* A name may now exist somewhere between 'line' and 'eol'.
  1018. Strip off leading whitespace and trailing whitespace,
  1019. then add it to the list. */
  1020. for (name = line; IS_WHITESPACE (* name); name ++)
  1021. ;
  1022. for (name_end = name;
  1023. (! IS_WHITESPACE (* name_end))
  1024. && (! IS_LINE_TERMINATOR (* name_end));
  1025. name_end ++)
  1026. ;
  1027. if (! IS_LINE_TERMINATOR (* name_end))
  1028. {
  1029. char * extra;
  1030. for (extra = name_end + 1; IS_WHITESPACE (* extra); extra ++)
  1031. ;
  1032. if (! IS_LINE_TERMINATOR (* extra))
  1033. non_fatal (_("%s:%d: Ignoring rubbish found on this line"),
  1034. filename, line_count);
  1035. }
  1036. * name_end = '\0';
  1037. if (name_end > name)
  1038. add_specific_symbol (name, htab);
  1039. /* Advance line pointer to end of line. The 'eol ++' in the for
  1040. loop above will then advance us to the start of the next line. */
  1041. line = eol;
  1042. line_count ++;
  1043. }
  1044. /* Do not free the buffer. Parts of it will have been referenced
  1045. in the calls to add_specific_symbol. */
  1046. *buffer_p = buffer;
  1047. }
  1048. /* See whether a symbol should be stripped or kept
  1049. based on strip_specific_list and keep_symbols. */
  1050. static int
  1051. is_specified_symbol_predicate (void **slot, void *data)
  1052. {
  1053. struct is_specified_symbol_predicate_data *d =
  1054. (struct is_specified_symbol_predicate_data *) data;
  1055. const char *slot_name = (char *) *slot;
  1056. if (*slot_name != '!')
  1057. {
  1058. if (! fnmatch (slot_name, d->name, 0))
  1059. {
  1060. d->found = true;
  1061. /* Continue traversal, there might be a non-match rule. */
  1062. return 1;
  1063. }
  1064. }
  1065. else
  1066. {
  1067. if (! fnmatch (slot_name + 1, d->name, 0))
  1068. {
  1069. d->found = false;
  1070. /* Stop traversal. */
  1071. return 0;
  1072. }
  1073. }
  1074. /* Continue traversal. */
  1075. return 1;
  1076. }
  1077. static bool
  1078. is_specified_symbol (const char *name, htab_t htab)
  1079. {
  1080. if (wildcard)
  1081. {
  1082. struct is_specified_symbol_predicate_data data;
  1083. data.name = name;
  1084. data.found = false;
  1085. htab_traverse (htab, is_specified_symbol_predicate, &data);
  1086. return data.found;
  1087. }
  1088. return htab_find (htab, name) != NULL;
  1089. }
  1090. /* Return a pointer to the symbol used as a signature for GROUP. */
  1091. static asymbol *
  1092. group_signature (asection *group)
  1093. {
  1094. bfd *abfd = group->owner;
  1095. Elf_Internal_Shdr *ghdr;
  1096. /* PR 20089: An earlier error may have prevented us from loading the symbol table. */
  1097. if (isympp == NULL)
  1098. return NULL;
  1099. if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  1100. return NULL;
  1101. ghdr = &elf_section_data (group)->this_hdr;
  1102. if (ghdr->sh_link == elf_onesymtab (abfd))
  1103. {
  1104. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  1105. Elf_Internal_Shdr *symhdr = &elf_symtab_hdr (abfd);
  1106. if (ghdr->sh_info > 0
  1107. && ghdr->sh_info < symhdr->sh_size / bed->s->sizeof_sym)
  1108. return isympp[ghdr->sh_info - 1];
  1109. }
  1110. return NULL;
  1111. }
  1112. /* Return TRUE if the section is a DWO section. */
  1113. static bool
  1114. is_dwo_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
  1115. {
  1116. const char *name;
  1117. int len;
  1118. if (sec == NULL || (name = bfd_section_name (sec)) == NULL)
  1119. return false;
  1120. len = strlen (name);
  1121. if (len < 5)
  1122. return false;
  1123. return startswith (name + len - 4, ".dwo");
  1124. }
  1125. /* Return TRUE if section SEC is in the update list. */
  1126. static bool
  1127. is_update_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
  1128. {
  1129. if (update_sections != NULL)
  1130. {
  1131. struct section_add *pupdate;
  1132. for (pupdate = update_sections;
  1133. pupdate != NULL;
  1134. pupdate = pupdate->next)
  1135. {
  1136. if (strcmp (sec->name, pupdate->name) == 0)
  1137. return true;
  1138. }
  1139. }
  1140. return false;
  1141. }
  1142. static bool
  1143. is_mergeable_note_section (bfd * abfd, asection * sec)
  1144. {
  1145. if (merge_notes
  1146. && bfd_get_flavour (abfd) == bfd_target_elf_flavour
  1147. && elf_section_data (sec)->this_hdr.sh_type == SHT_NOTE
  1148. /* FIXME: We currently only support merging GNU_BUILD_NOTEs.
  1149. We should add support for more note types. */
  1150. && (startswith (sec->name, GNU_BUILD_ATTRS_SECTION_NAME)))
  1151. return true;
  1152. return false;
  1153. }
  1154. /* See if a non-group section is being removed. */
  1155. static bool
  1156. is_strip_section_1 (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
  1157. {
  1158. if (find_section_list (bfd_section_name (sec), false, SECTION_CONTEXT_KEEP)
  1159. != NULL)
  1160. return false;
  1161. if (sections_removed || sections_copied)
  1162. {
  1163. struct section_list *p;
  1164. struct section_list *q;
  1165. p = find_section_list (bfd_section_name (sec), false,
  1166. SECTION_CONTEXT_REMOVE);
  1167. q = find_section_list (bfd_section_name (sec), false,
  1168. SECTION_CONTEXT_COPY);
  1169. if (p && q)
  1170. fatal (_("error: section %s matches both remove and copy options"),
  1171. bfd_section_name (sec));
  1172. if (p && is_update_section (abfd, sec))
  1173. fatal (_("error: section %s matches both update and remove options"),
  1174. bfd_section_name (sec));
  1175. if (p != NULL)
  1176. return true;
  1177. if (sections_copied && q == NULL)
  1178. return true;
  1179. }
  1180. if ((bfd_section_flags (sec) & SEC_DEBUGGING) != 0)
  1181. {
  1182. if (strip_symbols == STRIP_DEBUG
  1183. || strip_symbols == STRIP_UNNEEDED
  1184. || strip_symbols == STRIP_ALL
  1185. || discard_locals == LOCALS_ALL
  1186. || convert_debugging)
  1187. {
  1188. /* By default we don't want to strip .reloc section.
  1189. This section has for pe-coff special meaning. See
  1190. pe-dll.c file in ld, and peXXigen.c in bfd for details. */
  1191. if (strcmp (bfd_section_name (sec), ".reloc") != 0)
  1192. return true;
  1193. }
  1194. if (strip_symbols == STRIP_DWO)
  1195. return is_dwo_section (abfd, sec);
  1196. if (strip_symbols == STRIP_NONDEBUG)
  1197. return false;
  1198. }
  1199. if (strip_symbols == STRIP_NONDWO)
  1200. return !is_dwo_section (abfd, sec);
  1201. return false;
  1202. }
  1203. /* See if a section is being removed. */
  1204. static bool
  1205. is_strip_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
  1206. {
  1207. if (is_strip_section_1 (abfd, sec))
  1208. return true;
  1209. if ((bfd_section_flags (sec) & SEC_GROUP) != 0)
  1210. {
  1211. asymbol *gsym;
  1212. const char *gname;
  1213. asection *elt, *first;
  1214. gsym = group_signature (sec);
  1215. /* Strip groups without a valid signature. */
  1216. if (gsym == NULL)
  1217. return true;
  1218. /* PR binutils/3181
  1219. If we are going to strip the group signature symbol, then
  1220. strip the group section too. */
  1221. gname = gsym->name;
  1222. if ((strip_symbols == STRIP_ALL
  1223. && !is_specified_symbol (gname, keep_specific_htab))
  1224. || is_specified_symbol (gname, strip_specific_htab))
  1225. return true;
  1226. /* Remove the group section if all members are removed. */
  1227. first = elt = elf_next_in_group (sec);
  1228. while (elt != NULL)
  1229. {
  1230. if (!is_strip_section_1 (abfd, elt))
  1231. return false;
  1232. elt = elf_next_in_group (elt);
  1233. if (elt == first)
  1234. break;
  1235. }
  1236. return true;
  1237. }
  1238. return false;
  1239. }
  1240. static bool
  1241. is_nondebug_keep_contents_section (bfd *ibfd, asection *isection)
  1242. {
  1243. /* Always keep ELF note sections. */
  1244. if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour)
  1245. return elf_section_type (isection) == SHT_NOTE;
  1246. /* Always keep the .buildid section for PE/COFF.
  1247. Strictly, this should be written "always keep the section storing the debug
  1248. directory", but that may be the .text section for objects produced by some
  1249. tools, which it is not sensible to keep. */
  1250. if (bfd_get_flavour (ibfd) == bfd_target_coff_flavour)
  1251. return strcmp (bfd_section_name (isection), ".buildid") == 0;
  1252. return false;
  1253. }
  1254. /* Return true if SYM is a hidden symbol. */
  1255. static bool
  1256. is_hidden_symbol (asymbol *sym)
  1257. {
  1258. elf_symbol_type *elf_sym;
  1259. elf_sym = elf_symbol_from (sym);
  1260. if (elf_sym != NULL)
  1261. switch (ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other))
  1262. {
  1263. case STV_HIDDEN:
  1264. case STV_INTERNAL:
  1265. return true;
  1266. }
  1267. return false;
  1268. }
  1269. /* Empty name is hopefully never a valid symbol name. */
  1270. static const char * empty_name = "";
  1271. static bool
  1272. need_sym_before (struct addsym_node **node, const char *sym)
  1273. {
  1274. int count;
  1275. struct addsym_node *ptr = add_sym_list;
  1276. /* 'othersym' symbols are at the front of the list. */
  1277. for (count = 0; count < add_symbols; count++)
  1278. {
  1279. if (!ptr->othersym)
  1280. break;
  1281. if (ptr->othersym == empty_name)
  1282. continue;
  1283. else if (strcmp (ptr->othersym, sym) == 0)
  1284. {
  1285. free ((char *) ptr->othersym);
  1286. ptr->othersym = empty_name;
  1287. *node = ptr;
  1288. return true;
  1289. }
  1290. ptr = ptr->next;
  1291. }
  1292. return false;
  1293. }
  1294. static asymbol *
  1295. create_new_symbol (struct addsym_node *ptr, bfd *obfd)
  1296. {
  1297. asymbol *sym = bfd_make_empty_symbol (obfd);
  1298. bfd_set_asymbol_name (sym, ptr->symdef);
  1299. sym->value = ptr->symval;
  1300. sym->flags = ptr->flags;
  1301. if (ptr->section)
  1302. {
  1303. asection *sec = bfd_get_section_by_name (obfd, ptr->section);
  1304. if (!sec)
  1305. fatal (_("Section %s not found"), ptr->section);
  1306. sym->section = sec;
  1307. }
  1308. else
  1309. sym->section = bfd_abs_section_ptr;
  1310. return sym;
  1311. }
  1312. /* Choose which symbol entries to copy; put the result in OSYMS.
  1313. We don't copy in place, because that confuses the relocs.
  1314. Return the number of symbols to print. */
  1315. static unsigned int
  1316. filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
  1317. asymbol **isyms, long symcount)
  1318. {
  1319. asymbol **from = isyms, **to = osyms;
  1320. long src_count = 0, dst_count = 0;
  1321. int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
  1322. for (; src_count < symcount; src_count++)
  1323. {
  1324. asymbol *sym = from[src_count];
  1325. flagword flags = sym->flags;
  1326. char *name = (char *) bfd_asymbol_name (sym);
  1327. bool keep;
  1328. bool used_in_reloc = false;
  1329. bool undefined;
  1330. bool rem_leading_char;
  1331. bool add_leading_char;
  1332. undefined = bfd_is_und_section (bfd_asymbol_section (sym));
  1333. if (add_sym_list)
  1334. {
  1335. struct addsym_node *ptr;
  1336. if (need_sym_before (&ptr, name))
  1337. to[dst_count++] = create_new_symbol (ptr, obfd);
  1338. }
  1339. if (htab_elements (redefine_specific_htab) || section_rename_list)
  1340. {
  1341. char *new_name;
  1342. if (name != NULL
  1343. && name[0] == '_'
  1344. && name[1] == '_'
  1345. && strcmp (name + (name[2] == '_'), "__gnu_lto_slim") == 0)
  1346. {
  1347. fatal (_("redefining symbols does not work on LTO-compiled object files"));
  1348. }
  1349. new_name = (char *) lookup_sym_redefinition (name);
  1350. if (new_name == name
  1351. && (flags & BSF_SECTION_SYM) != 0)
  1352. new_name = (char *) find_section_rename (name, NULL);
  1353. bfd_set_asymbol_name (sym, new_name);
  1354. name = new_name;
  1355. }
  1356. /* Check if we will remove the current leading character. */
  1357. rem_leading_char =
  1358. (name[0] != '\0'
  1359. && name[0] == bfd_get_symbol_leading_char (abfd)
  1360. && (change_leading_char
  1361. || (remove_leading_char
  1362. && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
  1363. || undefined
  1364. || bfd_is_com_section (bfd_asymbol_section (sym))))));
  1365. /* Check if we will add a new leading character. */
  1366. add_leading_char =
  1367. change_leading_char
  1368. && (bfd_get_symbol_leading_char (obfd) != '\0')
  1369. && (bfd_get_symbol_leading_char (abfd) == '\0'
  1370. || (name[0] == bfd_get_symbol_leading_char (abfd)));
  1371. /* Short circuit for change_leading_char if we can do it in-place. */
  1372. if (rem_leading_char && add_leading_char && !prefix_symbols_string)
  1373. {
  1374. name[0] = bfd_get_symbol_leading_char (obfd);
  1375. bfd_set_asymbol_name (sym, name);
  1376. rem_leading_char = false;
  1377. add_leading_char = false;
  1378. }
  1379. /* Remove leading char. */
  1380. if (rem_leading_char)
  1381. bfd_set_asymbol_name (sym, ++name);
  1382. /* Add new leading char and/or prefix. */
  1383. if (add_leading_char || prefix_symbols_string)
  1384. {
  1385. char *n, *ptr;
  1386. size_t len = strlen (name) + 1;
  1387. if (add_leading_char)
  1388. len++;
  1389. if (prefix_symbols_string)
  1390. len += strlen (prefix_symbols_string);
  1391. ptr = n = (char *) xmalloc (len);
  1392. if (add_leading_char)
  1393. *ptr++ = bfd_get_symbol_leading_char (obfd);
  1394. if (prefix_symbols_string)
  1395. {
  1396. strcpy (ptr, prefix_symbols_string);
  1397. ptr += strlen (prefix_symbols_string);
  1398. }
  1399. strcpy (ptr, name);
  1400. bfd_set_asymbol_name (sym, n);
  1401. name = n;
  1402. }
  1403. if (strip_symbols == STRIP_ALL)
  1404. keep = false;
  1405. else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */
  1406. || ((flags & BSF_SECTION_SYM) != 0
  1407. && ((*bfd_asymbol_section (sym)->symbol_ptr_ptr)->flags
  1408. & BSF_KEEP) != 0))
  1409. {
  1410. keep = true;
  1411. used_in_reloc = true;
  1412. }
  1413. else if (relocatable /* Relocatable file. */
  1414. && ((flags & (BSF_GLOBAL | BSF_WEAK)) != 0
  1415. || bfd_is_com_section (bfd_asymbol_section (sym))))
  1416. keep = true;
  1417. else if (bfd_decode_symclass (sym) == 'I')
  1418. /* Global symbols in $idata sections need to be retained
  1419. even if relocatable is FALSE. External users of the
  1420. library containing the $idata section may reference these
  1421. symbols. */
  1422. keep = true;
  1423. else if ((flags & BSF_GLOBAL) != 0 /* Global symbol. */
  1424. || (flags & BSF_WEAK) != 0
  1425. || undefined
  1426. || bfd_is_com_section (bfd_asymbol_section (sym)))
  1427. keep = strip_symbols != STRIP_UNNEEDED;
  1428. else if ((flags & BSF_DEBUGGING) != 0) /* Debugging symbol. */
  1429. keep = (strip_symbols != STRIP_DEBUG
  1430. && strip_symbols != STRIP_UNNEEDED
  1431. && ! convert_debugging);
  1432. else if (bfd_coff_get_comdat_section (abfd, bfd_asymbol_section (sym)))
  1433. /* COMDAT sections store special information in local
  1434. symbols, so we cannot risk stripping any of them. */
  1435. keep = true;
  1436. else /* Local symbol. */
  1437. keep = (strip_symbols != STRIP_UNNEEDED
  1438. && (discard_locals != LOCALS_ALL
  1439. && (discard_locals != LOCALS_START_L
  1440. || ! bfd_is_local_label (abfd, sym))));
  1441. if (keep && is_specified_symbol (name, strip_specific_htab))
  1442. {
  1443. /* There are multiple ways to set 'keep' above, but if it
  1444. was the relocatable symbol case, then that's an error. */
  1445. if (used_in_reloc)
  1446. {
  1447. non_fatal (_("not stripping symbol `%s' because it is named in a relocation"), name);
  1448. status = 1;
  1449. }
  1450. else
  1451. keep = false;
  1452. }
  1453. if (keep
  1454. && !(flags & BSF_KEEP)
  1455. && is_specified_symbol (name, strip_unneeded_htab))
  1456. keep = false;
  1457. if (!keep
  1458. && ((keep_file_symbols && (flags & BSF_FILE))
  1459. || is_specified_symbol (name, keep_specific_htab)))
  1460. keep = true;
  1461. if (keep && is_strip_section (abfd, bfd_asymbol_section (sym)))
  1462. keep = false;
  1463. if (keep)
  1464. {
  1465. if (((flags & (BSF_GLOBAL | BSF_GNU_UNIQUE))
  1466. || undefined)
  1467. && (weaken || is_specified_symbol (name, weaken_specific_htab)))
  1468. {
  1469. sym->flags &= ~ (BSF_GLOBAL | BSF_GNU_UNIQUE);
  1470. sym->flags |= BSF_WEAK;
  1471. }
  1472. if (!undefined
  1473. && (flags & (BSF_GLOBAL | BSF_WEAK))
  1474. && (is_specified_symbol (name, localize_specific_htab)
  1475. || (htab_elements (keepglobal_specific_htab) != 0
  1476. && ! is_specified_symbol (name, keepglobal_specific_htab))
  1477. || (localize_hidden && is_hidden_symbol (sym))))
  1478. {
  1479. sym->flags &= ~ (BSF_GLOBAL | BSF_WEAK);
  1480. sym->flags |= BSF_LOCAL;
  1481. }
  1482. if (!undefined
  1483. && (flags & BSF_LOCAL)
  1484. && is_specified_symbol (name, globalize_specific_htab))
  1485. {
  1486. sym->flags &= ~ BSF_LOCAL;
  1487. sym->flags |= BSF_GLOBAL;
  1488. }
  1489. to[dst_count++] = sym;
  1490. }
  1491. }
  1492. if (add_sym_list)
  1493. {
  1494. struct addsym_node *ptr = add_sym_list;
  1495. for (src_count = 0; src_count < add_symbols; src_count++)
  1496. {
  1497. if (ptr->othersym)
  1498. {
  1499. if (ptr->othersym != empty_name)
  1500. fatal (_("'before=%s' not found"), ptr->othersym);
  1501. }
  1502. else
  1503. to[dst_count++] = create_new_symbol (ptr, obfd);
  1504. ptr = ptr->next;
  1505. }
  1506. }
  1507. to[dst_count] = NULL;
  1508. return dst_count;
  1509. }
  1510. /* Find the redefined name of symbol SOURCE. */
  1511. static const char *
  1512. lookup_sym_redefinition (const char *source)
  1513. {
  1514. struct redefine_node key_node = {(char *) source, NULL};
  1515. struct redefine_node *redef_node
  1516. = (struct redefine_node *) htab_find (redefine_specific_htab, &key_node);
  1517. return redef_node == NULL ? source : redef_node->target;
  1518. }
  1519. /* Insert a node into symbol redefine hash tabel. */
  1520. static void
  1521. add_redefine_and_check (const char *cause, const char *source,
  1522. const char *target)
  1523. {
  1524. struct redefine_node *new_node
  1525. = (struct redefine_node *) xmalloc (sizeof (struct redefine_node));
  1526. new_node->source = strdup (source);
  1527. new_node->target = strdup (target);
  1528. if (htab_find (redefine_specific_htab, new_node) != HTAB_EMPTY_ENTRY)
  1529. fatal (_("%s: Multiple redefinition of symbol \"%s\""),
  1530. cause, source);
  1531. if (htab_find (redefine_specific_reverse_htab, target) != HTAB_EMPTY_ENTRY)
  1532. fatal (_("%s: Symbol \"%s\" is target of more than one redefinition"),
  1533. cause, target);
  1534. /* Insert the NEW_NODE into hash table for quick search. */
  1535. add_specific_symbol_node (new_node, redefine_specific_htab);
  1536. /* Insert the target string into the reverse hash table, this is needed for
  1537. duplicated target string check. */
  1538. add_specific_symbol (new_node->target, redefine_specific_reverse_htab);
  1539. }
  1540. /* Handle the --redefine-syms option. Read lines containing "old new"
  1541. from the file, and add them to the symbol redefine list. */
  1542. static void
  1543. add_redefine_syms_file (const char *filename)
  1544. {
  1545. FILE *file;
  1546. char *buf;
  1547. size_t bufsize;
  1548. size_t len;
  1549. size_t outsym_off;
  1550. int c, lineno;
  1551. file = fopen (filename, "r");
  1552. if (file == NULL)
  1553. fatal (_("couldn't open symbol redefinition file %s (error: %s)"),
  1554. filename, strerror (errno));
  1555. bufsize = 100;
  1556. buf = (char *) xmalloc (bufsize + 1 /* For the terminating NUL. */);
  1557. lineno = 1;
  1558. c = getc (file);
  1559. len = 0;
  1560. outsym_off = 0;
  1561. while (c != EOF)
  1562. {
  1563. /* Collect the input symbol name. */
  1564. while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
  1565. {
  1566. if (c == '#')
  1567. goto comment;
  1568. buf[len++] = c;
  1569. if (len >= bufsize)
  1570. {
  1571. bufsize *= 2;
  1572. buf = (char *) xrealloc (buf, bufsize + 1);
  1573. }
  1574. c = getc (file);
  1575. }
  1576. buf[len++] = '\0';
  1577. if (c == EOF)
  1578. break;
  1579. /* Eat white space between the symbol names. */
  1580. while (IS_WHITESPACE (c))
  1581. c = getc (file);
  1582. if (c == '#' || IS_LINE_TERMINATOR (c))
  1583. goto comment;
  1584. if (c == EOF)
  1585. break;
  1586. /* Collect the output symbol name. */
  1587. outsym_off = len;
  1588. while (! IS_WHITESPACE (c) && ! IS_LINE_TERMINATOR (c) && c != EOF)
  1589. {
  1590. if (c == '#')
  1591. goto comment;
  1592. buf[len++] = c;
  1593. if (len >= bufsize)
  1594. {
  1595. bufsize *= 2;
  1596. buf = (char *) xrealloc (buf, bufsize + 1);
  1597. }
  1598. c = getc (file);
  1599. }
  1600. buf[len++] = '\0';
  1601. if (c == EOF)
  1602. break;
  1603. /* Eat white space at end of line. */
  1604. while (! IS_LINE_TERMINATOR(c) && c != EOF && IS_WHITESPACE (c))
  1605. c = getc (file);
  1606. if (c == '#')
  1607. goto comment;
  1608. /* Handle \r\n. */
  1609. if ((c == '\r' && (c = getc (file)) == '\n')
  1610. || c == '\n' || c == EOF)
  1611. {
  1612. end_of_line:
  1613. /* Append the redefinition to the list. */
  1614. if (buf[0] != '\0')
  1615. add_redefine_and_check (filename, &buf[0], &buf[outsym_off]);
  1616. lineno++;
  1617. len = 0;
  1618. outsym_off = 0;
  1619. if (c == EOF)
  1620. break;
  1621. c = getc (file);
  1622. continue;
  1623. }
  1624. else
  1625. fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
  1626. comment:
  1627. if (len != 0 && (outsym_off == 0 || outsym_off == len))
  1628. fatal (_("%s:%d: missing new symbol name"), filename, lineno);
  1629. buf[len++] = '\0';
  1630. /* Eat the rest of the line and finish it. */
  1631. while (c != '\n' && c != EOF)
  1632. c = getc (file);
  1633. goto end_of_line;
  1634. }
  1635. if (len != 0)
  1636. fatal (_("%s:%d: premature end of file"), filename, lineno);
  1637. free (buf);
  1638. fclose (file);
  1639. }
  1640. /* Copy unknown object file IBFD onto OBFD.
  1641. Returns TRUE upon success, FALSE otherwise. */
  1642. static bool
  1643. copy_unknown_object (bfd *ibfd, bfd *obfd)
  1644. {
  1645. char *cbuf;
  1646. bfd_size_type tocopy;
  1647. off_t size;
  1648. struct stat buf;
  1649. if (bfd_stat_arch_elt (ibfd, &buf) != 0)
  1650. {
  1651. bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
  1652. return false;
  1653. }
  1654. size = buf.st_size;
  1655. if (size < 0)
  1656. {
  1657. non_fatal (_("stat returns negative size for `%s'"),
  1658. bfd_get_archive_filename (ibfd));
  1659. return false;
  1660. }
  1661. if (bfd_seek (ibfd, (file_ptr) 0, SEEK_SET) != 0)
  1662. {
  1663. bfd_nonfatal (bfd_get_archive_filename (ibfd));
  1664. return false;
  1665. }
  1666. if (verbose)
  1667. printf (_("copy from `%s' [unknown] to `%s' [unknown]\n"),
  1668. bfd_get_archive_filename (ibfd), bfd_get_filename (obfd));
  1669. cbuf = (char *) xmalloc (BUFSIZE);
  1670. while (size != 0)
  1671. {
  1672. if (size > BUFSIZE)
  1673. tocopy = BUFSIZE;
  1674. else
  1675. tocopy = size;
  1676. if (bfd_bread (cbuf, tocopy, ibfd) != tocopy)
  1677. {
  1678. bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
  1679. free (cbuf);
  1680. return false;
  1681. }
  1682. if (bfd_bwrite (cbuf, tocopy, obfd) != tocopy)
  1683. {
  1684. bfd_nonfatal_message (NULL, obfd, NULL, NULL);
  1685. free (cbuf);
  1686. return false;
  1687. }
  1688. size -= tocopy;
  1689. }
  1690. /* We should at least to be able to read it back when copying an
  1691. unknown object in an archive. */
  1692. chmod (bfd_get_filename (obfd), buf.st_mode | S_IRUSR);
  1693. free (cbuf);
  1694. return true;
  1695. }
  1696. typedef struct objcopy_internal_note
  1697. {
  1698. Elf_Internal_Note note;
  1699. unsigned long padded_namesz;
  1700. bfd_vma start;
  1701. bfd_vma end;
  1702. } objcopy_internal_note;
  1703. #define DEBUG_MERGE 0
  1704. #if DEBUG_MERGE
  1705. #define merge_debug(format, ...) fprintf (stderr, format, ## __VA_ARGS__)
  1706. #else
  1707. #define merge_debug(format, ...)
  1708. #endif
  1709. /* Returns TRUE iff PNOTE1 overlaps or adjoins PNOTE2. */
  1710. static bool
  1711. overlaps_or_adjoins (objcopy_internal_note * pnote1,
  1712. objcopy_internal_note * pnote2)
  1713. {
  1714. if (pnote1->end < pnote2->start)
  1715. /* FIXME: Alignment of 16 bytes taken from x86_64 binaries.
  1716. Really we should extract the alignment of the section
  1717. covered by the notes. */
  1718. return BFD_ALIGN (pnote1->end, 16) < pnote2->start;
  1719. if (pnote2->end < pnote2->start)
  1720. return BFD_ALIGN (pnote2->end, 16) < pnote1->start;
  1721. if (pnote1->end < pnote2->end)
  1722. return true;
  1723. if (pnote2->end < pnote1->end)
  1724. return true;
  1725. return false;
  1726. }
  1727. /* Returns TRUE iff NEEDLE is fully contained by HAYSTACK. */
  1728. static bool
  1729. contained_by (objcopy_internal_note * needle,
  1730. objcopy_internal_note * haystack)
  1731. {
  1732. return needle->start >= haystack->start && needle->end <= haystack->end;
  1733. }
  1734. static bool
  1735. is_open_note (objcopy_internal_note * pnote)
  1736. {
  1737. return pnote->note.type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
  1738. }
  1739. static bool
  1740. is_func_note (objcopy_internal_note * pnote)
  1741. {
  1742. return pnote->note.type == NT_GNU_BUILD_ATTRIBUTE_FUNC;
  1743. }
  1744. static bool
  1745. is_deleted_note (objcopy_internal_note * pnote)
  1746. {
  1747. return pnote->note.type == 0;
  1748. }
  1749. static bool
  1750. is_version_note (objcopy_internal_note * pnote)
  1751. {
  1752. return (pnote->note.namesz > 4
  1753. && pnote->note.namedata[0] == 'G'
  1754. && pnote->note.namedata[1] == 'A'
  1755. && pnote->note.namedata[2] == '$'
  1756. && pnote->note.namedata[3] == GNU_BUILD_ATTRIBUTE_VERSION);
  1757. }
  1758. static bool
  1759. is_64bit (bfd * abfd)
  1760. {
  1761. /* Should never happen, but let's be paranoid. */
  1762. if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  1763. return false;
  1764. return elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64;
  1765. }
  1766. /* This sorting function is used to get the notes into an order
  1767. that makes merging easy. */
  1768. static int
  1769. compare_gnu_build_notes (const void * data1, const void * data2)
  1770. {
  1771. objcopy_internal_note * pnote1 = (objcopy_internal_note *) data1;
  1772. objcopy_internal_note * pnote2 = (objcopy_internal_note *) data2;
  1773. /* Sort notes based upon the attribute they record. */
  1774. int cmp = memcmp (pnote1->note.namedata + 3,
  1775. pnote2->note.namedata + 3,
  1776. pnote1->note.namesz < pnote2->note.namesz ?
  1777. pnote1->note.namesz - 3 : pnote2->note.namesz - 3);
  1778. if (cmp)
  1779. return cmp;
  1780. if (pnote1->end < pnote2->start)
  1781. return -1;
  1782. if (pnote1->start > pnote2->end)
  1783. return 1;
  1784. /* Overlaps - we should merge the two ranges. */
  1785. if (pnote1->start < pnote2->start)
  1786. return -1;
  1787. if (pnote1->end > pnote2->end)
  1788. return 1;
  1789. if (pnote1->end < pnote2->end)
  1790. return -1;
  1791. /* Put OPEN notes before function notes. */
  1792. if (is_open_note (pnote1) && ! is_open_note (pnote2))
  1793. return -1;
  1794. if (! is_open_note (pnote1) && is_open_note (pnote2))
  1795. return 1;
  1796. return 0;
  1797. }
  1798. /* This sorting function is used to get the notes into an order
  1799. that makes eliminating address ranges easier. */
  1800. static int
  1801. sort_gnu_build_notes (const void * data1, const void * data2)
  1802. {
  1803. objcopy_internal_note * pnote1 = (objcopy_internal_note *) data1;
  1804. objcopy_internal_note * pnote2 = (objcopy_internal_note *) data2;
  1805. if (pnote1->note.type != pnote2->note.type)
  1806. {
  1807. /* Move deleted notes to the end. */
  1808. if (is_deleted_note (pnote1)) /* 1: OFD 2: OFD */
  1809. return 1;
  1810. /* Move OPEN notes to the start. */
  1811. if (is_open_note (pnote1)) /* 1: OF 2: OFD */
  1812. return -1;
  1813. if (is_deleted_note (pnote2)) /* 1: F 2: O D */
  1814. return -1;
  1815. return 1; /* 1: F 2: O */
  1816. }
  1817. /* Sort by starting address. */
  1818. if (pnote1->start < pnote2->start)
  1819. return -1;
  1820. if (pnote1->start > pnote2->start)
  1821. return 1;
  1822. /* Then by end address (bigger range first). */
  1823. if (pnote1->end > pnote2->end)
  1824. return -1;
  1825. if (pnote1->end < pnote2->end)
  1826. return 1;
  1827. /* Then by attribute type. */
  1828. if (pnote1->note.namesz > 4
  1829. && pnote2->note.namesz > 4
  1830. && pnote1->note.namedata[3] != pnote2->note.namedata[3])
  1831. return pnote1->note.namedata[3] - pnote2->note.namedata[3];
  1832. return 0;
  1833. }
  1834. /* Merge the notes on SEC, removing redundant entries.
  1835. Returns the new, smaller size of the section upon success. */
  1836. static bfd_size_type
  1837. merge_gnu_build_notes (bfd * abfd,
  1838. asection * sec,
  1839. bfd_size_type size,
  1840. bfd_byte * contents)
  1841. {
  1842. objcopy_internal_note * pnotes_end;
  1843. objcopy_internal_note * pnotes = NULL;
  1844. objcopy_internal_note * pnote;
  1845. bfd_size_type remain = size;
  1846. unsigned version_1_seen = 0;
  1847. unsigned version_2_seen = 0;
  1848. unsigned version_3_seen = 0;
  1849. const char * err = NULL;
  1850. bfd_byte * in = contents;
  1851. unsigned long previous_func_start = 0;
  1852. unsigned long previous_open_start = 0;
  1853. unsigned long previous_func_end = 0;
  1854. unsigned long previous_open_end = 0;
  1855. long relsize;
  1856. relsize = bfd_get_reloc_upper_bound (abfd, sec);
  1857. if (relsize > 0)
  1858. {
  1859. arelent ** relpp;
  1860. long relcount;
  1861. /* If there are relocs associated with this section then we
  1862. cannot safely merge it. */
  1863. relpp = (arelent **) xmalloc (relsize);
  1864. relcount = bfd_canonicalize_reloc (abfd, sec, relpp, isympp);
  1865. free (relpp);
  1866. if (relcount != 0)
  1867. {
  1868. if (! is_strip)
  1869. non_fatal (_("%s[%s]: Cannot merge - there are relocations against this section"),
  1870. bfd_get_filename (abfd), bfd_section_name (sec));
  1871. goto done;
  1872. }
  1873. }
  1874. /* Make a copy of the notes and convert to our internal format.
  1875. Minimum size of a note is 12 bytes. Also locate the version
  1876. notes and check them. */
  1877. pnote = pnotes = (objcopy_internal_note *)
  1878. xcalloc ((size / 12), sizeof (* pnote));
  1879. while (remain >= 12)
  1880. {
  1881. bfd_vma start, end;
  1882. pnote->note.namesz = bfd_get_32 (abfd, in);
  1883. pnote->note.descsz = bfd_get_32 (abfd, in + 4);
  1884. pnote->note.type = bfd_get_32 (abfd, in + 8);
  1885. pnote->padded_namesz = (pnote->note.namesz + 3) & ~3;
  1886. if (((pnote->note.descsz + 3) & ~3) != pnote->note.descsz)
  1887. {
  1888. err = _("corrupt GNU build attribute note: description size not a factor of 4");
  1889. goto done;
  1890. }
  1891. if (pnote->note.type != NT_GNU_BUILD_ATTRIBUTE_OPEN
  1892. && pnote->note.type != NT_GNU_BUILD_ATTRIBUTE_FUNC)
  1893. {
  1894. err = _("corrupt GNU build attribute note: wrong note type");
  1895. goto done;
  1896. }
  1897. if (pnote->padded_namesz + pnote->note.descsz + 12 > remain)
  1898. {
  1899. err = _("corrupt GNU build attribute note: note too big");
  1900. goto done;
  1901. }
  1902. if (pnote->note.namesz < 2)
  1903. {
  1904. err = _("corrupt GNU build attribute note: name too small");
  1905. goto done;
  1906. }
  1907. pnote->note.namedata = (char *)(in + 12);
  1908. pnote->note.descdata = (char *)(in + 12 + pnote->padded_namesz);
  1909. remain -= 12 + pnote->padded_namesz + pnote->note.descsz;
  1910. in += 12 + pnote->padded_namesz + pnote->note.descsz;
  1911. if (pnote->note.namesz > 2
  1912. && pnote->note.namedata[0] == '$'
  1913. && pnote->note.namedata[1] == GNU_BUILD_ATTRIBUTE_VERSION
  1914. && pnote->note.namedata[2] == '1')
  1915. ++ version_1_seen;
  1916. else if (is_version_note (pnote))
  1917. {
  1918. if (pnote->note.namedata[4] == '2')
  1919. ++ version_2_seen;
  1920. else if (pnote->note.namedata[4] == '3')
  1921. ++ version_3_seen;
  1922. else
  1923. {
  1924. err = _("corrupt GNU build attribute note: unsupported version");
  1925. goto done;
  1926. }
  1927. }
  1928. switch (pnote->note.descsz)
  1929. {
  1930. case 0:
  1931. start = end = 0;
  1932. break;
  1933. case 4:
  1934. start = bfd_get_32 (abfd, pnote->note.descdata);
  1935. /* FIXME: For version 1 and 2 notes we should try to
  1936. calculate the end address by finding a symbol whose
  1937. value is START, and then adding in its size.
  1938. For now though, since v1 and v2 was not intended to
  1939. handle gaps, we chose an artificially large end
  1940. address. */
  1941. end = (bfd_vma) -1;
  1942. break;
  1943. case 8:
  1944. start = bfd_get_32 (abfd, pnote->note.descdata);
  1945. end = bfd_get_32 (abfd, pnote->note.descdata + 4);
  1946. break;
  1947. case 16:
  1948. start = bfd_get_64 (abfd, pnote->note.descdata);
  1949. end = bfd_get_64 (abfd, pnote->note.descdata + 8);
  1950. break;
  1951. default:
  1952. err = _("corrupt GNU build attribute note: bad description size");
  1953. goto done;
  1954. }
  1955. if (start > end)
  1956. /* This can happen with PPC64LE binaries where empty notes are
  1957. encoded as start = end + 4. */
  1958. start = end;
  1959. if (is_open_note (pnote))
  1960. {
  1961. if (start)
  1962. previous_open_start = start;
  1963. pnote->start = previous_open_start;
  1964. if (end)
  1965. previous_open_end = end;
  1966. pnote->end = previous_open_end;
  1967. }
  1968. else
  1969. {
  1970. if (start)
  1971. previous_func_start = start;
  1972. pnote->start = previous_func_start;
  1973. if (end)
  1974. previous_func_end = end;
  1975. pnote->end = previous_func_end;
  1976. }
  1977. if (pnote->note.namedata[pnote->note.namesz - 1] != 0)
  1978. {
  1979. err = _("corrupt GNU build attribute note: name not NUL terminated");
  1980. goto done;
  1981. }
  1982. pnote ++;
  1983. }
  1984. pnotes_end = pnote;
  1985. /* Check that the notes are valid. */
  1986. if (remain != 0)
  1987. {
  1988. err = _("corrupt GNU build attribute notes: excess data at end");
  1989. goto done;
  1990. }
  1991. if (version_1_seen == 0 && version_2_seen == 0 && version_3_seen == 0)
  1992. {
  1993. #if 0
  1994. err = _("bad GNU build attribute notes: no known versions detected");
  1995. goto done;
  1996. #else
  1997. /* This happens with glibc. No idea why. */
  1998. non_fatal (_("%s[%s]: Warning: version note missing - assuming version 3"),
  1999. bfd_get_filename (abfd), bfd_section_name (sec));
  2000. version_3_seen = 2;
  2001. #endif
  2002. }
  2003. if ( (version_1_seen > 0 && version_2_seen > 0)
  2004. || (version_1_seen > 0 && version_3_seen > 0)
  2005. || (version_2_seen > 0 && version_3_seen > 0))
  2006. {
  2007. err = _("bad GNU build attribute notes: multiple different versions");
  2008. goto done;
  2009. }
  2010. /* We are now only supporting the merging v3+ notes
  2011. - it makes things much simpler. */
  2012. if (version_3_seen == 0)
  2013. {
  2014. merge_debug ("%s: skipping merge - not using v3 notes", bfd_section_name (sec));
  2015. goto done;
  2016. }
  2017. merge_debug ("Merging section %s which contains %ld notes\n",
  2018. sec->name, pnotes_end - pnotes);
  2019. /* Sort the notes. */
  2020. qsort (pnotes, pnotes_end - pnotes, sizeof (* pnotes),
  2021. compare_gnu_build_notes);
  2022. #if DEBUG_MERGE
  2023. merge_debug ("Results of initial sort:\n");
  2024. for (pnote = pnotes; pnote < pnotes_end; pnote ++)
  2025. merge_debug ("offset %#08lx range %#08lx..%#08lx type %ld attribute %d namesz %ld\n",
  2026. (pnote->note.namedata - (char *) contents) - 12,
  2027. pnote->start, pnote->end,
  2028. pnote->note.type,
  2029. pnote->note.namedata[3],
  2030. pnote->note.namesz
  2031. );
  2032. #endif
  2033. /* Now merge the notes. The rules are:
  2034. 1. If a note has a zero range, it can be eliminated.
  2035. 2. If two notes have the same namedata then:
  2036. 2a. If one note's range is fully covered by the other note
  2037. then it can be deleted.
  2038. 2b. If one note's range partially overlaps or adjoins the
  2039. other note then if they are both of the same type (open
  2040. or func) then they can be merged and one deleted. If
  2041. they are of different types then they cannot be merged. */
  2042. for (pnote = pnotes; pnote < pnotes_end; pnote ++)
  2043. {
  2044. /* Skip already deleted notes.
  2045. FIXME: Can this happen ? We are scanning forwards and
  2046. deleting backwards after all. */
  2047. if (is_deleted_note (pnote))
  2048. continue;
  2049. /* Rule 1 - delete 0-range notes. */
  2050. if (pnote->start == pnote->end)
  2051. {
  2052. merge_debug ("Delete note at offset %#08lx - empty range\n",
  2053. (pnote->note.namedata - (char *) contents) - 12);
  2054. pnote->note.type = 0;
  2055. continue;
  2056. }
  2057. int iter;
  2058. objcopy_internal_note * back;
  2059. /* Rule 2: Check to see if there is an identical previous note. */
  2060. for (iter = 0, back = pnote - 1; back >= pnotes; back --)
  2061. {
  2062. if (is_deleted_note (back))
  2063. continue;
  2064. /* Our sorting function should have placed all identically
  2065. attributed notes together, so if we see a note of a different
  2066. attribute type stop searching. */
  2067. if (back->note.namesz != pnote->note.namesz
  2068. || memcmp (back->note.namedata,
  2069. pnote->note.namedata, pnote->note.namesz) != 0)
  2070. break;
  2071. if (back->start == pnote->start
  2072. && back->end == pnote->end)
  2073. {
  2074. merge_debug ("Delete note at offset %#08lx - duplicate of note at offset %#08lx\n",
  2075. (pnote->note.namedata - (char *) contents) - 12,
  2076. (back->note.namedata - (char *) contents) - 12);
  2077. pnote->note.type = 0;
  2078. break;
  2079. }
  2080. /* Rule 2a. */
  2081. if (contained_by (pnote, back))
  2082. {
  2083. merge_debug ("Delete note at offset %#08lx - fully contained by note at %#08lx\n",
  2084. (pnote->note.namedata - (char *) contents) - 12,
  2085. (back->note.namedata - (char *) contents) - 12);
  2086. pnote->note.type = 0;
  2087. break;
  2088. }
  2089. #if DEBUG_MERGE
  2090. /* This should not happen as we have sorted the
  2091. notes with earlier starting addresses first. */
  2092. if (contained_by (back, pnote))
  2093. merge_debug ("ERROR: UNEXPECTED CONTAINMENT\n");
  2094. #endif
  2095. /* Rule 2b. */
  2096. if (overlaps_or_adjoins (back, pnote)
  2097. && is_func_note (back) == is_func_note (pnote))
  2098. {
  2099. merge_debug ("Delete note at offset %#08lx - merge into note at %#08lx\n",
  2100. (pnote->note.namedata - (char *) contents) - 12,
  2101. (back->note.namedata - (char *) contents) - 12);
  2102. back->end = back->end > pnote->end ? back->end : pnote->end;
  2103. back->start = back->start < pnote->start ? back->start : pnote->start;
  2104. pnote->note.type = 0;
  2105. break;
  2106. }
  2107. /* Don't scan too far back however. */
  2108. if (iter ++ > 16)
  2109. {
  2110. /* FIXME: Not sure if this can ever be triggered. */
  2111. merge_debug ("ITERATION LIMIT REACHED\n");
  2112. break;
  2113. }
  2114. }
  2115. #if DEBUG_MERGE
  2116. if (! is_deleted_note (pnote))
  2117. merge_debug ("Unable to do anything with note at %#08lx\n",
  2118. (pnote->note.namedata - (char *) contents) - 12);
  2119. #endif
  2120. }
  2121. /* Resort the notes. */
  2122. merge_debug ("Final sorting of notes\n");
  2123. qsort (pnotes, pnotes_end - pnotes, sizeof (* pnotes), sort_gnu_build_notes);
  2124. /* Reconstruct the ELF notes. */
  2125. bfd_byte * new_contents;
  2126. bfd_byte * old;
  2127. bfd_byte * new;
  2128. bfd_size_type new_size;
  2129. bfd_vma prev_start = 0;
  2130. bfd_vma prev_end = 0;
  2131. /* Not sure how, but the notes might grow in size.
  2132. (eg see PR 1774507). Allow for this here. */
  2133. new = new_contents = xmalloc (size * 2);
  2134. for (pnote = pnotes, old = contents;
  2135. pnote < pnotes_end;
  2136. pnote ++)
  2137. {
  2138. bfd_size_type note_size = 12 + pnote->padded_namesz + pnote->note.descsz;
  2139. if (! is_deleted_note (pnote))
  2140. {
  2141. /* Create the note, potentially using the
  2142. address range of the previous note. */
  2143. if (pnote->start == prev_start && pnote->end == prev_end)
  2144. {
  2145. bfd_put_32 (abfd, pnote->note.namesz, new);
  2146. bfd_put_32 (abfd, 0, new + 4);
  2147. bfd_put_32 (abfd, pnote->note.type, new + 8);
  2148. new += 12;
  2149. memcpy (new, pnote->note.namedata, pnote->note.namesz);
  2150. if (pnote->note.namesz < pnote->padded_namesz)
  2151. memset (new + pnote->note.namesz, 0, pnote->padded_namesz - pnote->note.namesz);
  2152. new += pnote->padded_namesz;
  2153. }
  2154. else
  2155. {
  2156. bfd_put_32 (abfd, pnote->note.namesz, new);
  2157. bfd_put_32 (abfd, is_64bit (abfd) ? 16 : 8, new + 4);
  2158. bfd_put_32 (abfd, pnote->note.type, new + 8);
  2159. new += 12;
  2160. memcpy (new, pnote->note.namedata, pnote->note.namesz);
  2161. if (pnote->note.namesz < pnote->padded_namesz)
  2162. memset (new + pnote->note.namesz, 0, pnote->padded_namesz - pnote->note.namesz);
  2163. new += pnote->padded_namesz;
  2164. if (is_64bit (abfd))
  2165. {
  2166. bfd_put_64 (abfd, pnote->start, new);
  2167. bfd_put_64 (abfd, pnote->end, new + 8);
  2168. new += 16;
  2169. }
  2170. else
  2171. {
  2172. bfd_put_32 (abfd, pnote->start, new);
  2173. bfd_put_32 (abfd, pnote->end, new + 4);
  2174. new += 8;
  2175. }
  2176. prev_start = pnote->start;
  2177. prev_end = pnote->end;
  2178. }
  2179. }
  2180. old += note_size;
  2181. }
  2182. #if DEBUG_MERGE
  2183. merge_debug ("Results of merge:\n");
  2184. for (pnote = pnotes; pnote < pnotes_end; pnote ++)
  2185. if (! is_deleted_note (pnote))
  2186. merge_debug ("offset %#08lx range %#08lx..%#08lx type %ld attribute %d namesz %ld\n",
  2187. (pnote->note.namedata - (char *) contents) - 12,
  2188. pnote->start, pnote->end,
  2189. pnote->note.type,
  2190. pnote->note.namedata[3],
  2191. pnote->note.namesz
  2192. );
  2193. #endif
  2194. new_size = new - new_contents;
  2195. if (new_size < size)
  2196. {
  2197. memcpy (contents, new_contents, new_size);
  2198. size = new_size;
  2199. }
  2200. free (new_contents);
  2201. done:
  2202. if (err)
  2203. {
  2204. bfd_set_error (bfd_error_bad_value);
  2205. bfd_nonfatal_message (NULL, abfd, sec, err);
  2206. status = 1;
  2207. }
  2208. free (pnotes);
  2209. return size;
  2210. }
  2211. static flagword
  2212. check_new_section_flags (flagword flags, bfd * abfd, const char * secname)
  2213. {
  2214. /* Only set the SEC_COFF_SHARED flag on COFF files.
  2215. The same bit value is used by ELF targets to indicate
  2216. compressed sections, and setting that flag here breaks
  2217. things. */
  2218. if ((flags & SEC_COFF_SHARED)
  2219. && bfd_get_flavour (abfd) != bfd_target_coff_flavour)
  2220. {
  2221. non_fatal (_("%s[%s]: Note - dropping 'share' flag as output format is not COFF"),
  2222. bfd_get_filename (abfd), secname);
  2223. flags &= ~ SEC_COFF_SHARED;
  2224. }
  2225. return flags;
  2226. }
  2227. /* Copy object file IBFD onto OBFD.
  2228. Returns TRUE upon success, FALSE otherwise. */
  2229. static bool
  2230. copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
  2231. {
  2232. bfd_vma start;
  2233. long symcount;
  2234. asection **osections = NULL;
  2235. asection *osec;
  2236. asection *gnu_debuglink_section = NULL;
  2237. bfd_size_type *gaps = NULL;
  2238. bfd_size_type max_gap = 0;
  2239. long symsize;
  2240. void *dhandle;
  2241. enum bfd_architecture iarch;
  2242. unsigned int imach;
  2243. unsigned int num_sec, i;
  2244. if (ibfd->xvec->byteorder != obfd->xvec->byteorder
  2245. && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
  2246. && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
  2247. {
  2248. /* PR 17636: Call non-fatal so that we return to our parent who
  2249. may need to tidy temporary files. */
  2250. non_fatal (_("unable to change endianness of '%s'"),
  2251. bfd_get_archive_filename (ibfd));
  2252. return false;
  2253. }
  2254. if (ibfd->read_only)
  2255. {
  2256. non_fatal (_("unable to modify '%s' due to errors"),
  2257. bfd_get_archive_filename (ibfd));
  2258. return false;
  2259. }
  2260. if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
  2261. {
  2262. bfd_nonfatal_message (NULL, obfd, NULL, NULL);
  2263. return false;
  2264. }
  2265. if (ibfd->sections == NULL)
  2266. {
  2267. non_fatal (_("error: the input file '%s' has no sections"),
  2268. bfd_get_archive_filename (ibfd));
  2269. return false;
  2270. }
  2271. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
  2272. {
  2273. if ((do_debug_sections & compress) != 0
  2274. && do_debug_sections != compress)
  2275. {
  2276. non_fatal (_("--compress-debug-sections=[zlib|zlib-gnu|zlib-gabi] is unsupported on `%s'"),
  2277. bfd_get_archive_filename (ibfd));
  2278. return false;
  2279. }
  2280. if (do_elf_stt_common)
  2281. {
  2282. non_fatal (_("--elf-stt-common=[yes|no] is unsupported on `%s'"),
  2283. bfd_get_archive_filename (ibfd));
  2284. return false;
  2285. }
  2286. }
  2287. if (verbose)
  2288. printf (_("copy from `%s' [%s] to `%s' [%s]\n"),
  2289. bfd_get_archive_filename (ibfd), bfd_get_target (ibfd),
  2290. bfd_get_filename (obfd), bfd_get_target (obfd));
  2291. if (extract_symbol)
  2292. start = 0;
  2293. else
  2294. {
  2295. if (set_start_set)
  2296. start = set_start;
  2297. else
  2298. start = bfd_get_start_address (ibfd);
  2299. start += change_start;
  2300. }
  2301. /* Neither the start address nor the flags
  2302. need to be set for a core file. */
  2303. if (bfd_get_format (obfd) != bfd_core)
  2304. {
  2305. flagword flags;
  2306. flags = bfd_get_file_flags (ibfd);
  2307. flags |= bfd_flags_to_set;
  2308. flags &= ~bfd_flags_to_clear;
  2309. flags &= bfd_applicable_file_flags (obfd);
  2310. if (strip_symbols == STRIP_ALL)
  2311. flags &= ~HAS_RELOC;
  2312. if (!bfd_set_start_address (obfd, start)
  2313. || !bfd_set_file_flags (obfd, flags))
  2314. {
  2315. bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
  2316. return false;
  2317. }
  2318. }
  2319. /* Copy architecture of input file to output file. */
  2320. iarch = bfd_get_arch (ibfd);
  2321. imach = bfd_get_mach (ibfd);
  2322. if (input_arch)
  2323. {
  2324. if (iarch == bfd_arch_unknown)
  2325. {
  2326. iarch = input_arch->arch;
  2327. imach = input_arch->mach;
  2328. }
  2329. else
  2330. non_fatal (_("Input file `%s' ignores binary architecture parameter."),
  2331. bfd_get_archive_filename (ibfd));
  2332. }
  2333. if (iarch == bfd_arch_unknown
  2334. && bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  2335. && bfd_get_flavour (obfd) == bfd_target_elf_flavour)
  2336. {
  2337. const struct elf_backend_data *bed = get_elf_backend_data (obfd);
  2338. iarch = bed->arch;
  2339. imach = 0;
  2340. }
  2341. if (!bfd_set_arch_mach (obfd, iarch, imach)
  2342. && (ibfd->target_defaulted
  2343. || bfd_get_arch (ibfd) != bfd_get_arch (obfd)))
  2344. {
  2345. if (bfd_get_arch (ibfd) == bfd_arch_unknown)
  2346. non_fatal (_("Unable to recognise the format of the input file `%s'"),
  2347. bfd_get_archive_filename (ibfd));
  2348. else
  2349. non_fatal (_("Output file cannot represent architecture `%s'"),
  2350. bfd_printable_arch_mach (bfd_get_arch (ibfd),
  2351. bfd_get_mach (ibfd)));
  2352. return false;
  2353. }
  2354. if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
  2355. {
  2356. bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
  2357. return false;
  2358. }
  2359. if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
  2360. && bfd_pei_p (obfd))
  2361. {
  2362. /* Set up PE parameters. */
  2363. pe_data_type *pe = pe_data (obfd);
  2364. /* Copy PE parameters before changing them. */
  2365. if (bfd_get_flavour (ibfd) == bfd_target_coff_flavour
  2366. && bfd_pei_p (ibfd))
  2367. pe->pe_opthdr = pe_data (ibfd)->pe_opthdr;
  2368. if (pe_file_alignment != (bfd_vma) -1)
  2369. pe->pe_opthdr.FileAlignment = pe_file_alignment;
  2370. else
  2371. pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
  2372. if (pe_heap_commit != (bfd_vma) -1)
  2373. pe->pe_opthdr.SizeOfHeapCommit = pe_heap_commit;
  2374. if (pe_heap_reserve != (bfd_vma) -1)
  2375. pe->pe_opthdr.SizeOfHeapCommit = pe_heap_reserve;
  2376. if (pe_image_base != (bfd_vma) -1)
  2377. pe->pe_opthdr.ImageBase = pe_image_base;
  2378. if (pe_section_alignment != (bfd_vma) -1)
  2379. pe->pe_opthdr.SectionAlignment = pe_section_alignment;
  2380. else
  2381. pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
  2382. if (pe_stack_commit != (bfd_vma) -1)
  2383. pe->pe_opthdr.SizeOfStackCommit = pe_stack_commit;
  2384. if (pe_stack_reserve != (bfd_vma) -1)
  2385. pe->pe_opthdr.SizeOfStackCommit = pe_stack_reserve;
  2386. if (pe_subsystem != -1)
  2387. pe->pe_opthdr.Subsystem = pe_subsystem;
  2388. if (pe_major_subsystem_version != -1)
  2389. pe->pe_opthdr.MajorSubsystemVersion = pe_major_subsystem_version;
  2390. if (pe_minor_subsystem_version != -1)
  2391. pe->pe_opthdr.MinorSubsystemVersion = pe_minor_subsystem_version;
  2392. if (pe_file_alignment > pe_section_alignment)
  2393. {
  2394. char file_alignment[20], section_alignment[20];
  2395. sprintf_vma (file_alignment, pe_file_alignment);
  2396. sprintf_vma (section_alignment, pe_section_alignment);
  2397. non_fatal (_("warning: file alignment (0x%s) > section alignment (0x%s)"),
  2398. file_alignment, section_alignment);
  2399. }
  2400. if (preserve_dates
  2401. && bfd_get_flavour (ibfd) == bfd_target_coff_flavour
  2402. && bfd_pei_p (ibfd))
  2403. pe->timestamp = pe_data (ibfd)->coff.timestamp;
  2404. }
  2405. free (isympp);
  2406. if (osympp != isympp)
  2407. free (osympp);
  2408. isympp = NULL;
  2409. osympp = NULL;
  2410. symsize = bfd_get_symtab_upper_bound (ibfd);
  2411. if (symsize < 0)
  2412. {
  2413. bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
  2414. return false;
  2415. }
  2416. osympp = isympp = (asymbol **) xmalloc (symsize);
  2417. symcount = bfd_canonicalize_symtab (ibfd, isympp);
  2418. if (symcount < 0)
  2419. {
  2420. bfd_nonfatal_message (NULL, ibfd, NULL, NULL);
  2421. return false;
  2422. }
  2423. /* PR 17512: file: d6323821
  2424. If the symbol table could not be loaded do not pretend that we have
  2425. any symbols. This trips us up later on when we load the relocs. */
  2426. if (symcount == 0)
  2427. {
  2428. free (isympp);
  2429. osympp = isympp = NULL;
  2430. }
  2431. /* BFD mandates that all output sections be created and sizes set before
  2432. any output is done. Thus, we traverse all sections multiple times. */
  2433. bfd_map_over_sections (ibfd, setup_section, obfd);
  2434. if (!extract_symbol)
  2435. setup_bfd_headers (ibfd, obfd);
  2436. if (add_sections != NULL)
  2437. {
  2438. struct section_add *padd;
  2439. struct section_list *pset;
  2440. for (padd = add_sections; padd != NULL; padd = padd->next)
  2441. {
  2442. flagword flags;
  2443. pset = find_section_list (padd->name, false,
  2444. SECTION_CONTEXT_SET_FLAGS);
  2445. if (pset != NULL)
  2446. {
  2447. flags = pset->flags | SEC_HAS_CONTENTS;
  2448. flags = check_new_section_flags (flags, obfd, padd->name);
  2449. }
  2450. else
  2451. flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DATA;
  2452. /* bfd_make_section_with_flags() does not return very helpful
  2453. error codes, so check for the most likely user error first. */
  2454. if (bfd_get_section_by_name (obfd, padd->name))
  2455. {
  2456. bfd_nonfatal_message (NULL, obfd, NULL,
  2457. _("can't add section '%s'"), padd->name);
  2458. return false;
  2459. }
  2460. else
  2461. {
  2462. /* We use LINKER_CREATED here so that the backend hooks
  2463. will create any special section type information,
  2464. instead of presuming we know what we're doing merely
  2465. because we set the flags. */
  2466. padd->section = bfd_make_section_with_flags
  2467. (obfd, padd->name, flags | SEC_LINKER_CREATED);
  2468. if (padd->section == NULL)
  2469. {
  2470. bfd_nonfatal_message (NULL, obfd, NULL,
  2471. _("can't create section `%s'"),
  2472. padd->name);
  2473. return false;
  2474. }
  2475. }
  2476. if (!bfd_set_section_size (padd->section, padd->size))
  2477. {
  2478. bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
  2479. return false;
  2480. }
  2481. pset = find_section_list (padd->name, false,
  2482. SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA);
  2483. if (pset != NULL
  2484. && !bfd_set_section_vma (padd->section, pset->vma_val))
  2485. {
  2486. bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
  2487. return false;
  2488. }
  2489. pset = find_section_list (padd->name, false,
  2490. SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA);
  2491. if (pset != NULL)
  2492. {
  2493. padd->section->lma = pset->lma_val;
  2494. if (!bfd_set_section_alignment
  2495. (padd->section, bfd_section_alignment (padd->section)))
  2496. {
  2497. bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
  2498. return false;
  2499. }
  2500. }
  2501. }
  2502. }
  2503. if (update_sections != NULL)
  2504. {
  2505. struct section_add *pupdate;
  2506. for (pupdate = update_sections;
  2507. pupdate != NULL;
  2508. pupdate = pupdate->next)
  2509. {
  2510. pupdate->section = bfd_get_section_by_name (ibfd, pupdate->name);
  2511. if (pupdate->section == NULL)
  2512. {
  2513. non_fatal (_("error: %s not found, can't be updated"), pupdate->name);
  2514. return false;
  2515. }
  2516. osec = pupdate->section->output_section;
  2517. if (!bfd_set_section_size (osec, pupdate->size))
  2518. {
  2519. bfd_nonfatal_message (NULL, obfd, osec, NULL);
  2520. return false;
  2521. }
  2522. }
  2523. }
  2524. merged_note_section * merged_note_sections = NULL;
  2525. if (merge_notes)
  2526. {
  2527. /* This palaver is necessary because we must set the output
  2528. section size first, before its contents are ready. */
  2529. for (osec = ibfd->sections; osec != NULL; osec = osec->next)
  2530. {
  2531. if (! is_mergeable_note_section (ibfd, osec))
  2532. continue;
  2533. /* If the section is going to be completly deleted then
  2534. do not bother to merge it. */
  2535. if (osec->output_section == NULL)
  2536. continue;
  2537. bfd_size_type size = bfd_section_size (osec);
  2538. if (size == 0)
  2539. {
  2540. bfd_nonfatal_message (NULL, ibfd, osec,
  2541. _("warning: note section is empty"));
  2542. continue;
  2543. }
  2544. merged_note_section * merged = xmalloc (sizeof * merged);
  2545. merged->contents = NULL;
  2546. if (! bfd_get_full_section_contents (ibfd, osec, & merged->contents))
  2547. {
  2548. bfd_nonfatal_message (NULL, ibfd, osec,
  2549. _("warning: could not load note section"));
  2550. free (merged);
  2551. continue;
  2552. }
  2553. merged->size = merge_gnu_build_notes (ibfd, osec, size,
  2554. merged->contents);
  2555. /* FIXME: Once we have read the contents in, we must write
  2556. them out again. So even if the mergeing has achieved
  2557. nothing we still add this entry to the merge list. */
  2558. if (size != merged->size
  2559. && !bfd_set_section_size (osec->output_section, merged->size))
  2560. {
  2561. bfd_nonfatal_message (NULL, obfd, osec,
  2562. _("warning: failed to set merged notes size"));
  2563. free (merged->contents);
  2564. free (merged);
  2565. continue;
  2566. }
  2567. /* Add section to list of merged sections. */
  2568. merged->sec = osec;
  2569. merged->next = merged_note_sections;
  2570. merged_note_sections = merged;
  2571. }
  2572. }
  2573. if (dump_sections != NULL)
  2574. {
  2575. struct section_add * pdump;
  2576. for (pdump = dump_sections; pdump != NULL; pdump = pdump->next)
  2577. {
  2578. FILE * f;
  2579. bfd_byte *contents;
  2580. osec = bfd_get_section_by_name (ibfd, pdump->name);
  2581. if (osec == NULL)
  2582. {
  2583. bfd_nonfatal_message (NULL, ibfd, NULL,
  2584. _("can't dump section '%s' - it does not exist"),
  2585. pdump->name);
  2586. continue;
  2587. }
  2588. if ((bfd_section_flags (osec) & SEC_HAS_CONTENTS) == 0)
  2589. {
  2590. bfd_nonfatal_message (NULL, ibfd, osec,
  2591. _("can't dump section - it has no contents"));
  2592. continue;
  2593. }
  2594. bfd_size_type size = bfd_section_size (osec);
  2595. /* Note - we allow the dumping of zero-sized sections,
  2596. creating an empty file. */
  2597. f = fopen (pdump->filename, FOPEN_WB);
  2598. if (f == NULL)
  2599. {
  2600. bfd_nonfatal_message (pdump->filename, NULL, NULL,
  2601. _("could not open section dump file"));
  2602. continue;
  2603. }
  2604. if (bfd_malloc_and_get_section (ibfd, osec, &contents))
  2605. {
  2606. if (size != 0 && fwrite (contents, 1, size, f) != size)
  2607. {
  2608. non_fatal (_("error writing section contents to %s (error: %s)"),
  2609. pdump->filename,
  2610. strerror (errno));
  2611. free (contents);
  2612. fclose (f);
  2613. return false;
  2614. }
  2615. }
  2616. else
  2617. bfd_nonfatal_message (NULL, ibfd, osec,
  2618. _("could not retrieve section contents"));
  2619. fclose (f);
  2620. free (contents);
  2621. }
  2622. }
  2623. if (gnu_debuglink_filename != NULL)
  2624. {
  2625. /* PR 15125: Give a helpful warning message if
  2626. the debuglink section already exists, and
  2627. allow the rest of the copy to complete. */
  2628. if (bfd_get_section_by_name (obfd, ".gnu_debuglink"))
  2629. {
  2630. non_fatal (_("%s: debuglink section already exists"),
  2631. bfd_get_filename (obfd));
  2632. gnu_debuglink_filename = NULL;
  2633. }
  2634. else
  2635. {
  2636. gnu_debuglink_section = bfd_create_gnu_debuglink_section
  2637. (obfd, gnu_debuglink_filename);
  2638. if (gnu_debuglink_section == NULL)
  2639. {
  2640. bfd_nonfatal_message (NULL, obfd, NULL,
  2641. _("cannot create debug link section `%s'"),
  2642. gnu_debuglink_filename);
  2643. return false;
  2644. }
  2645. /* Special processing for PE format files. We
  2646. have no way to distinguish PE from COFF here. */
  2647. if (bfd_get_flavour (obfd) == bfd_target_coff_flavour)
  2648. {
  2649. bfd_vma debuglink_vma;
  2650. asection * highest_section;
  2651. /* The PE spec requires that all sections be adjacent and sorted
  2652. in ascending order of VMA. It also specifies that debug
  2653. sections should be last. This is despite the fact that debug
  2654. sections are not loaded into memory and so in theory have no
  2655. use for a VMA.
  2656. This means that the debuglink section must be given a non-zero
  2657. VMA which makes it contiguous with other debug sections. So
  2658. walk the current section list, find the section with the
  2659. highest VMA and start the debuglink section after that one. */
  2660. for (osec = obfd->sections, highest_section = NULL;
  2661. osec != NULL;
  2662. osec = osec->next)
  2663. if (osec->vma > 0
  2664. && (highest_section == NULL
  2665. || osec->vma > highest_section->vma))
  2666. highest_section = osec;
  2667. if (highest_section)
  2668. debuglink_vma = BFD_ALIGN (highest_section->vma
  2669. + highest_section->size,
  2670. /* FIXME: We ought to be using
  2671. COFF_PAGE_SIZE here or maybe
  2672. bfd_section_alignment() (if it
  2673. was set) but since this is for PE
  2674. and we know the required alignment
  2675. it is easier just to hard code it. */
  2676. 0x1000);
  2677. else
  2678. /* Umm, not sure what to do in this case. */
  2679. debuglink_vma = 0x1000;
  2680. bfd_set_section_vma (gnu_debuglink_section, debuglink_vma);
  2681. }
  2682. }
  2683. }
  2684. num_sec = bfd_count_sections (obfd);
  2685. if (num_sec != 0
  2686. && (gap_fill_set || pad_to_set))
  2687. {
  2688. asection **set;
  2689. /* We must fill in gaps between the sections and/or we must pad
  2690. the last section to a specified address. We do this by
  2691. grabbing a list of the sections, sorting them by VMA, and
  2692. increasing the section sizes as required to fill the gaps.
  2693. We write out the gap contents below. */
  2694. osections = xmalloc (num_sec * sizeof (*osections));
  2695. set = osections;
  2696. bfd_map_over_sections (obfd, get_sections, &set);
  2697. qsort (osections, num_sec, sizeof (*osections), compare_section_lma);
  2698. gaps = xmalloc (num_sec * sizeof (*gaps));
  2699. memset (gaps, 0, num_sec * sizeof (*gaps));
  2700. if (gap_fill_set)
  2701. {
  2702. for (i = 0; i < num_sec - 1; i++)
  2703. {
  2704. flagword flags;
  2705. bfd_size_type size; /* Octets. */
  2706. bfd_vma gap_start, gap_stop; /* Octets. */
  2707. unsigned int opb1 = bfd_octets_per_byte (obfd, osections[i]);
  2708. unsigned int opb2 = bfd_octets_per_byte (obfd, osections[i+1]);
  2709. flags = bfd_section_flags (osections[i]);
  2710. if ((flags & SEC_HAS_CONTENTS) == 0
  2711. || (flags & SEC_LOAD) == 0)
  2712. continue;
  2713. size = bfd_section_size (osections[i]);
  2714. gap_start = bfd_section_lma (osections[i]) * opb1 + size;
  2715. gap_stop = bfd_section_lma (osections[i + 1]) * opb2;
  2716. if (gap_start < gap_stop)
  2717. {
  2718. if (!bfd_set_section_size (osections[i],
  2719. size + (gap_stop - gap_start)))
  2720. {
  2721. bfd_nonfatal_message (NULL, obfd, osections[i],
  2722. _("Can't fill gap after section"));
  2723. status = 1;
  2724. break;
  2725. }
  2726. gaps[i] = gap_stop - gap_start;
  2727. if (max_gap < gap_stop - gap_start)
  2728. max_gap = gap_stop - gap_start;
  2729. }
  2730. }
  2731. }
  2732. if (pad_to_set)
  2733. {
  2734. bfd_vma lma; /* Octets. */
  2735. bfd_size_type size; /* Octets. */
  2736. unsigned int opb = bfd_octets_per_byte (obfd, osections[num_sec - 1]);
  2737. bfd_vma _pad_to = pad_to * opb;
  2738. lma = bfd_section_lma (osections[num_sec - 1]) * opb;
  2739. size = bfd_section_size (osections[num_sec - 1]);
  2740. if (lma + size < _pad_to)
  2741. {
  2742. if (!bfd_set_section_size (osections[num_sec - 1], _pad_to - lma))
  2743. {
  2744. bfd_nonfatal_message (NULL, obfd, osections[num_sec - 1],
  2745. _("can't add padding"));
  2746. status = 1;
  2747. }
  2748. else
  2749. {
  2750. gaps[num_sec - 1] = _pad_to - (lma + size);
  2751. if (max_gap < _pad_to - (lma + size))
  2752. max_gap = _pad_to - (lma + size);
  2753. }
  2754. }
  2755. }
  2756. }
  2757. /* Symbol filtering must happen after the output sections
  2758. have been created, but before their contents are set. */
  2759. dhandle = NULL;
  2760. if (convert_debugging)
  2761. dhandle = read_debugging_info (ibfd, isympp, symcount, false);
  2762. if ((obfd->flags & (EXEC_P | DYNAMIC)) != 0
  2763. && (obfd->flags & HAS_RELOC) == 0)
  2764. {
  2765. if (bfd_keep_unused_section_symbols (obfd) || keep_section_symbols)
  2766. {
  2767. /* Non-relocatable inputs may not have the unused section
  2768. symbols. Mark all section symbols as used to generate
  2769. section symbols. */
  2770. asection *asect;
  2771. for (asect = obfd->sections; asect != NULL; asect = asect->next)
  2772. if (asect->symbol)
  2773. asect->symbol->flags |= BSF_SECTION_SYM_USED;
  2774. }
  2775. else
  2776. {
  2777. /* Non-relocatable inputs may have the unused section symbols.
  2778. Mark all section symbols as unused to excluded them. */
  2779. long s;
  2780. for (s = 0; s < symcount; s++)
  2781. if ((isympp[s]->flags & BSF_SECTION_SYM_USED))
  2782. isympp[s]->flags &= ~BSF_SECTION_SYM_USED;
  2783. }
  2784. }
  2785. if (strip_symbols == STRIP_DEBUG
  2786. || strip_symbols == STRIP_ALL
  2787. || strip_symbols == STRIP_UNNEEDED
  2788. || strip_symbols == STRIP_NONDEBUG
  2789. || strip_symbols == STRIP_DWO
  2790. || strip_symbols == STRIP_NONDWO
  2791. || discard_locals != LOCALS_UNDEF
  2792. || localize_hidden
  2793. || htab_elements (strip_specific_htab) != 0
  2794. || htab_elements (keep_specific_htab) != 0
  2795. || htab_elements (localize_specific_htab) != 0
  2796. || htab_elements (globalize_specific_htab) != 0
  2797. || htab_elements (keepglobal_specific_htab) != 0
  2798. || htab_elements (weaken_specific_htab) != 0
  2799. || htab_elements (redefine_specific_htab) != 0
  2800. || prefix_symbols_string
  2801. || sections_removed
  2802. || sections_copied
  2803. || convert_debugging
  2804. || change_leading_char
  2805. || remove_leading_char
  2806. || section_rename_list
  2807. || weaken
  2808. || add_symbols)
  2809. {
  2810. /* Mark symbols used in output relocations so that they
  2811. are kept, even if they are local labels or static symbols.
  2812. Note we iterate over the input sections examining their
  2813. relocations since the relocations for the output sections
  2814. haven't been set yet. mark_symbols_used_in_relocations will
  2815. ignore input sections which have no corresponding output
  2816. section. */
  2817. if (strip_symbols != STRIP_ALL)
  2818. {
  2819. bfd_set_error (bfd_error_no_error);
  2820. bfd_map_over_sections (ibfd,
  2821. mark_symbols_used_in_relocations,
  2822. isympp);
  2823. if (bfd_get_error () != bfd_error_no_error)
  2824. {
  2825. status = 1;
  2826. return false;
  2827. }
  2828. }
  2829. osympp = (asymbol **) xmalloc ((symcount + add_symbols + 1) * sizeof (asymbol *));
  2830. symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
  2831. }
  2832. if (convert_debugging && dhandle != NULL)
  2833. {
  2834. bool res;
  2835. res = write_debugging_info (obfd, dhandle, &symcount, &osympp);
  2836. free (dhandle);
  2837. dhandle = NULL; /* Paranoia... */
  2838. if (! res)
  2839. {
  2840. status = 1;
  2841. return false;
  2842. }
  2843. }
  2844. bfd_set_symtab (obfd, osympp, symcount);
  2845. /* This has to happen before section positions are set. */
  2846. bfd_map_over_sections (ibfd, copy_relocations_in_section, obfd);
  2847. /* This has to happen after the symbol table has been set. */
  2848. bfd_map_over_sections (ibfd, copy_section, obfd);
  2849. if (add_sections != NULL)
  2850. {
  2851. struct section_add *padd;
  2852. for (padd = add_sections; padd != NULL; padd = padd->next)
  2853. {
  2854. if (! bfd_set_section_contents (obfd, padd->section, padd->contents,
  2855. 0, padd->size))
  2856. {
  2857. bfd_nonfatal_message (NULL, obfd, padd->section, NULL);
  2858. return false;
  2859. }
  2860. }
  2861. }
  2862. if (update_sections != NULL)
  2863. {
  2864. struct section_add *pupdate;
  2865. for (pupdate = update_sections;
  2866. pupdate != NULL;
  2867. pupdate = pupdate->next)
  2868. {
  2869. osec = pupdate->section->output_section;
  2870. if (! bfd_set_section_contents (obfd, osec, pupdate->contents,
  2871. 0, pupdate->size))
  2872. {
  2873. bfd_nonfatal_message (NULL, obfd, osec, NULL);
  2874. return false;
  2875. }
  2876. }
  2877. }
  2878. if (merged_note_sections != NULL)
  2879. {
  2880. merged_note_section * merged = NULL;
  2881. for (osec = obfd->sections; osec != NULL; osec = osec->next)
  2882. {
  2883. if (! is_mergeable_note_section (obfd, osec))
  2884. continue;
  2885. if (merged == NULL)
  2886. merged = merged_note_sections;
  2887. /* It is likely that output sections are in the same order
  2888. as the input sections, but do not assume that this is
  2889. the case. */
  2890. if (merged->sec->output_section != osec)
  2891. {
  2892. for (merged = merged_note_sections;
  2893. merged != NULL;
  2894. merged = merged->next)
  2895. if (merged->sec->output_section == osec)
  2896. break;
  2897. if (merged == NULL)
  2898. {
  2899. bfd_nonfatal_message
  2900. (NULL, obfd, osec,
  2901. _("error: failed to locate merged notes"));
  2902. continue;
  2903. }
  2904. }
  2905. if (merged->contents == NULL)
  2906. {
  2907. bfd_nonfatal_message
  2908. (NULL, obfd, osec,
  2909. _("error: failed to merge notes"));
  2910. continue;
  2911. }
  2912. if (! bfd_set_section_contents (obfd, osec, merged->contents, 0,
  2913. merged->size))
  2914. {
  2915. bfd_nonfatal_message
  2916. (NULL, obfd, osec,
  2917. _("error: failed to copy merged notes into output"));
  2918. return false;
  2919. }
  2920. merged = merged->next;
  2921. }
  2922. /* Free the memory. */
  2923. merged_note_section * next;
  2924. for (merged = merged_note_sections; merged != NULL; merged = next)
  2925. {
  2926. next = merged->next;
  2927. free (merged->contents);
  2928. free (merged);
  2929. }
  2930. }
  2931. else if (merge_notes && ! is_strip)
  2932. non_fatal (_("%s: Could not find any mergeable note sections"),
  2933. bfd_get_filename (ibfd));
  2934. if (gnu_debuglink_filename != NULL)
  2935. {
  2936. if (! bfd_fill_in_gnu_debuglink_section
  2937. (obfd, gnu_debuglink_section, gnu_debuglink_filename))
  2938. {
  2939. bfd_nonfatal_message (NULL, obfd, NULL,
  2940. _("cannot fill debug link section `%s'"),
  2941. gnu_debuglink_filename);
  2942. return false;
  2943. }
  2944. }
  2945. if (gaps != NULL)
  2946. {
  2947. bfd_byte *buf;
  2948. /* Fill in the gaps. */
  2949. if (max_gap > 8192)
  2950. max_gap = 8192;
  2951. buf = (bfd_byte *) xmalloc (max_gap);
  2952. memset (buf, gap_fill, max_gap);
  2953. for (i = 0; i < num_sec; i++)
  2954. {
  2955. if (gaps[i] != 0)
  2956. {
  2957. bfd_size_type left;
  2958. file_ptr off;
  2959. left = gaps[i];
  2960. off = bfd_section_size (osections[i]) - left;
  2961. while (left > 0)
  2962. {
  2963. bfd_size_type now;
  2964. if (left > 8192)
  2965. now = 8192;
  2966. else
  2967. now = left;
  2968. if (! bfd_set_section_contents (obfd, osections[i], buf,
  2969. off, now))
  2970. {
  2971. bfd_nonfatal_message (NULL, obfd, osections[i], NULL);
  2972. free (buf);
  2973. return false;
  2974. }
  2975. left -= now;
  2976. off += now;
  2977. }
  2978. }
  2979. }
  2980. free (buf);
  2981. free (gaps);
  2982. gaps = NULL;
  2983. }
  2984. /* Allow the BFD backend to copy any private data it understands
  2985. from the input BFD to the output BFD. This is done last to
  2986. permit the routine to look at the filtered symbol table, which is
  2987. important for the ECOFF code at least. */
  2988. if (! bfd_copy_private_bfd_data (ibfd, obfd))
  2989. {
  2990. bfd_nonfatal_message (NULL, obfd, NULL,
  2991. _("error copying private BFD data"));
  2992. return false;
  2993. }
  2994. /* Switch to the alternate machine code. We have to do this at the
  2995. very end, because we only initialize the header when we create
  2996. the first section. */
  2997. if (use_alt_mach_code != 0)
  2998. {
  2999. if (! bfd_alt_mach_code (obfd, use_alt_mach_code))
  3000. {
  3001. non_fatal (_("this target does not support %lu alternative machine codes"),
  3002. use_alt_mach_code);
  3003. if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
  3004. {
  3005. non_fatal (_("treating that number as an absolute e_machine value instead"));
  3006. elf_elfheader (obfd)->e_machine = use_alt_mach_code;
  3007. }
  3008. else
  3009. non_fatal (_("ignoring the alternative value"));
  3010. }
  3011. }
  3012. return true;
  3013. }
  3014. /* Read each archive element in turn from IBFD, copy the
  3015. contents to temp file, and keep the temp file handle.
  3016. If 'force_output_target' is TRUE then make sure that
  3017. all elements in the new archive are of the type
  3018. 'output_target'. */
  3019. static void
  3020. copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
  3021. bool force_output_target,
  3022. const bfd_arch_info_type *input_arch)
  3023. {
  3024. struct name_list
  3025. {
  3026. struct name_list *next;
  3027. const char *name;
  3028. bfd *obfd;
  3029. } *list, *l;
  3030. bfd **ptr = &obfd->archive_head;
  3031. bfd *this_element;
  3032. char *dir;
  3033. const char *filename;
  3034. /* PR 24281: It is not clear what should happen when copying a thin archive.
  3035. One part is straight forward - if the output archive is in a different
  3036. directory from the input archive then any relative paths in the library
  3037. should be adjusted to the new location. But if any transformation
  3038. options are active (eg strip, rename, add, etc) then the implication is
  3039. that these should be applied to the files pointed to by the archive.
  3040. But since objcopy is not destructive, this means that new files must be
  3041. created, and there is no guidance for the names of the new files. (Plus
  3042. this conflicts with one of the goals of thin libraries - only taking up
  3043. a minimal amount of space in the file system).
  3044. So for now we fail if an attempt is made to copy such libraries. */
  3045. if (ibfd->is_thin_archive)
  3046. {
  3047. status = 1;
  3048. bfd_set_error (bfd_error_invalid_operation);
  3049. bfd_nonfatal_message (NULL, ibfd, NULL,
  3050. _("sorry: copying thin archives is not currently supported"));
  3051. return;
  3052. }
  3053. /* Make a temp directory to hold the contents. */
  3054. dir = make_tempdir (bfd_get_filename (obfd));
  3055. if (dir == NULL)
  3056. fatal (_("cannot create tempdir for archive copying (error: %s)"),
  3057. strerror (errno));
  3058. if (strip_symbols == STRIP_ALL)
  3059. obfd->has_armap = false;
  3060. else
  3061. obfd->has_armap = ibfd->has_armap;
  3062. obfd->is_thin_archive = ibfd->is_thin_archive;
  3063. if (deterministic)
  3064. obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
  3065. list = NULL;
  3066. this_element = bfd_openr_next_archived_file (ibfd, NULL);
  3067. if (!bfd_set_format (obfd, bfd_get_format (ibfd)))
  3068. {
  3069. status = 1;
  3070. bfd_nonfatal_message (NULL, obfd, NULL, NULL);
  3071. goto cleanup_and_exit;
  3072. }
  3073. while (!status && this_element != NULL)
  3074. {
  3075. char *output_name;
  3076. bfd *output_bfd;
  3077. bfd *last_element;
  3078. struct stat buf;
  3079. int stat_status = 0;
  3080. bool del = true;
  3081. bool ok_object;
  3082. /* PR binutils/17533: Do not allow directory traversal
  3083. outside of the current directory tree by archive members. */
  3084. if (! is_valid_archive_path (bfd_get_filename (this_element)))
  3085. {
  3086. non_fatal (_("illegal pathname found in archive member: %s"),
  3087. bfd_get_filename (this_element));
  3088. status = 1;
  3089. goto cleanup_and_exit;
  3090. }
  3091. /* Create an output file for this member. */
  3092. output_name = concat (dir, "/",
  3093. bfd_get_filename (this_element), (char *) 0);
  3094. /* If the file already exists, make another temp dir. */
  3095. if (stat (output_name, &buf) >= 0)
  3096. {
  3097. char * tmpdir = make_tempdir (output_name);
  3098. free (output_name);
  3099. if (tmpdir == NULL)
  3100. {
  3101. non_fatal (_("cannot create tempdir for archive copying (error: %s)"),
  3102. strerror (errno));
  3103. status = 1;
  3104. goto cleanup_and_exit;
  3105. }
  3106. l = (struct name_list *) xmalloc (sizeof (struct name_list));
  3107. l->name = tmpdir;
  3108. l->next = list;
  3109. l->obfd = NULL;
  3110. list = l;
  3111. output_name = concat (tmpdir, "/",
  3112. bfd_get_filename (this_element), (char *) 0);
  3113. }
  3114. if (preserve_dates)
  3115. {
  3116. memset (&buf, 0, sizeof (buf));
  3117. stat_status = bfd_stat_arch_elt (this_element, &buf);
  3118. if (stat_status != 0)
  3119. non_fatal (_("internal stat error on %s"),
  3120. bfd_get_filename (this_element));
  3121. }
  3122. l = (struct name_list *) xmalloc (sizeof (struct name_list));
  3123. l->name = output_name;
  3124. l->next = list;
  3125. l->obfd = NULL;
  3126. list = l;
  3127. ok_object = bfd_check_format (this_element, bfd_object);
  3128. if (!ok_object)
  3129. bfd_nonfatal_message (NULL, this_element, NULL,
  3130. _("Unable to recognise the format of file"));
  3131. /* PR binutils/3110: Cope with archives
  3132. containing multiple target types. */
  3133. if (force_output_target || !ok_object)
  3134. output_bfd = bfd_openw (output_name, output_target);
  3135. else
  3136. output_bfd = bfd_openw (output_name, bfd_get_target (this_element));
  3137. if (output_bfd == NULL)
  3138. {
  3139. bfd_nonfatal_message (output_name, NULL, NULL, NULL);
  3140. status = 1;
  3141. goto cleanup_and_exit;
  3142. }
  3143. if (ok_object)
  3144. {
  3145. del = !copy_object (this_element, output_bfd, input_arch);
  3146. if (del && bfd_get_arch (this_element) == bfd_arch_unknown)
  3147. /* Try again as an unknown object file. */
  3148. ok_object = false;
  3149. else if (!bfd_close (output_bfd))
  3150. {
  3151. bfd_nonfatal_message (output_name, NULL, NULL, NULL);
  3152. /* Error in new object file. Don't change archive. */
  3153. status = 1;
  3154. }
  3155. }
  3156. if (!ok_object)
  3157. {
  3158. del = !copy_unknown_object (this_element, output_bfd);
  3159. if (!bfd_close_all_done (output_bfd))
  3160. {
  3161. bfd_nonfatal_message (output_name, NULL, NULL, NULL);
  3162. /* Error in new object file. Don't change archive. */
  3163. status = 1;
  3164. }
  3165. }
  3166. if (del)
  3167. {
  3168. unlink (output_name);
  3169. status = 1;
  3170. }
  3171. else
  3172. {
  3173. if (preserve_dates && stat_status == 0)
  3174. set_times (output_name, &buf);
  3175. /* Open the newly output file and attach to our list. */
  3176. output_bfd = bfd_openr (output_name, output_target);
  3177. l->obfd = output_bfd;
  3178. *ptr = output_bfd;
  3179. ptr = &output_bfd->archive_next;
  3180. last_element = this_element;
  3181. this_element = bfd_openr_next_archived_file (ibfd, last_element);
  3182. bfd_close (last_element);
  3183. }
  3184. }
  3185. *ptr = NULL;
  3186. filename = bfd_get_filename (obfd);
  3187. if (!bfd_close (obfd))
  3188. {
  3189. status = 1;
  3190. bfd_nonfatal_message (filename, NULL, NULL, NULL);
  3191. }
  3192. filename = bfd_get_filename (ibfd);
  3193. if (!bfd_close (ibfd))
  3194. {
  3195. status = 1;
  3196. bfd_nonfatal_message (filename, NULL, NULL, NULL);
  3197. }
  3198. cleanup_and_exit:
  3199. /* Delete all the files that we opened. */
  3200. {
  3201. struct name_list * next;
  3202. for (l = list; l != NULL; l = next)
  3203. {
  3204. if (l->obfd == NULL)
  3205. rmdir (l->name);
  3206. else
  3207. {
  3208. bfd_close (l->obfd);
  3209. unlink (l->name);
  3210. }
  3211. next = l->next;
  3212. free (l);
  3213. }
  3214. }
  3215. rmdir (dir);
  3216. }
  3217. static void
  3218. set_long_section_mode (bfd *output_bfd, bfd *input_bfd, enum long_section_name_handling style)
  3219. {
  3220. /* This is only relevant to Coff targets. */
  3221. if (bfd_get_flavour (output_bfd) == bfd_target_coff_flavour)
  3222. {
  3223. if (style == KEEP
  3224. && bfd_get_flavour (input_bfd) == bfd_target_coff_flavour)
  3225. style = bfd_coff_long_section_names (input_bfd) ? ENABLE : DISABLE;
  3226. bfd_coff_set_long_section_names (output_bfd, style != DISABLE);
  3227. }
  3228. }
  3229. /* The top-level control. */
  3230. static void
  3231. copy_file (const char *input_filename, const char *output_filename, int ofd,
  3232. struct stat *in_stat, const char *input_target,
  3233. const char *output_target, const bfd_arch_info_type *input_arch)
  3234. {
  3235. bfd *ibfd;
  3236. char **obj_matching;
  3237. char **core_matching;
  3238. off_t size = get_file_size (input_filename);
  3239. if (size < 1)
  3240. {
  3241. if (size == 0)
  3242. non_fatal (_("error: the input file '%s' is empty"),
  3243. input_filename);
  3244. status = 1;
  3245. return;
  3246. }
  3247. /* To allow us to do "strip *" without dying on the first
  3248. non-object file, failures are nonfatal. */
  3249. ibfd = bfd_openr (input_filename, input_target);
  3250. if (ibfd == NULL || bfd_stat (ibfd, in_stat) != 0)
  3251. {
  3252. bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
  3253. status = 1;
  3254. return;
  3255. }
  3256. switch (do_debug_sections)
  3257. {
  3258. case compress:
  3259. case compress_zlib:
  3260. case compress_gnu_zlib:
  3261. case compress_gabi_zlib:
  3262. ibfd->flags |= BFD_COMPRESS;
  3263. /* Don't check if input is ELF here since this information is
  3264. only available after bfd_check_format_matches is called. */
  3265. if (do_debug_sections != compress_gnu_zlib)
  3266. ibfd->flags |= BFD_COMPRESS_GABI;
  3267. break;
  3268. case decompress:
  3269. ibfd->flags |= BFD_DECOMPRESS;
  3270. break;
  3271. default:
  3272. break;
  3273. }
  3274. switch (do_elf_stt_common)
  3275. {
  3276. case elf_stt_common:
  3277. ibfd->flags |= BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON;
  3278. break;
  3279. break;
  3280. case no_elf_stt_common:
  3281. ibfd->flags |= BFD_CONVERT_ELF_COMMON;
  3282. break;
  3283. default:
  3284. break;
  3285. }
  3286. if (bfd_check_format (ibfd, bfd_archive))
  3287. {
  3288. bool force_output_target;
  3289. bfd *obfd;
  3290. /* bfd_get_target does not return the correct value until
  3291. bfd_check_format succeeds. */
  3292. if (output_target == NULL)
  3293. {
  3294. output_target = bfd_get_target (ibfd);
  3295. force_output_target = false;
  3296. }
  3297. else
  3298. force_output_target = true;
  3299. if (ofd >= 0)
  3300. obfd = bfd_fdopenw (output_filename, output_target, ofd);
  3301. else
  3302. obfd = bfd_openw (output_filename, output_target);
  3303. if (obfd == NULL)
  3304. {
  3305. close (ofd);
  3306. bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
  3307. status = 1;
  3308. return;
  3309. }
  3310. if (gnu_debuglink_filename != NULL)
  3311. {
  3312. non_fatal (_("--add-gnu-debuglink ignored for archive %s"),
  3313. bfd_get_filename (ibfd));
  3314. gnu_debuglink_filename = NULL;
  3315. }
  3316. /* This is a no-op on non-Coff targets. */
  3317. set_long_section_mode (obfd, ibfd, long_section_names);
  3318. copy_archive (ibfd, obfd, output_target, force_output_target, input_arch);
  3319. }
  3320. else if (bfd_check_format_matches (ibfd, bfd_object, &obj_matching))
  3321. {
  3322. bfd *obfd;
  3323. do_copy:
  3324. /* bfd_get_target does not return the correct value until
  3325. bfd_check_format succeeds. */
  3326. if (output_target == NULL)
  3327. output_target = bfd_get_target (ibfd);
  3328. if (ofd >= 0)
  3329. obfd = bfd_fdopenw (output_filename, output_target, ofd);
  3330. else
  3331. obfd = bfd_openw (output_filename, output_target);
  3332. if (obfd == NULL)
  3333. {
  3334. close (ofd);
  3335. bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
  3336. status = 1;
  3337. return;
  3338. }
  3339. /* This is a no-op on non-Coff targets. */
  3340. set_long_section_mode (obfd, ibfd, long_section_names);
  3341. if (! copy_object (ibfd, obfd, input_arch))
  3342. status = 1;
  3343. /* PR 17512: file: 0f15796a.
  3344. If the file could not be copied it may not be in a writeable
  3345. state. So use bfd_close_all_done to avoid the possibility of
  3346. writing uninitialised data into the file. */
  3347. if (! (status ? bfd_close_all_done (obfd) : bfd_close (obfd)))
  3348. {
  3349. status = 1;
  3350. bfd_nonfatal_message (output_filename, NULL, NULL, NULL);
  3351. return;
  3352. }
  3353. if (!bfd_close (ibfd))
  3354. {
  3355. status = 1;
  3356. bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
  3357. return;
  3358. }
  3359. }
  3360. else
  3361. {
  3362. bfd_error_type obj_error = bfd_get_error ();
  3363. bfd_error_type core_error;
  3364. if (bfd_check_format_matches (ibfd, bfd_core, &core_matching))
  3365. {
  3366. /* This probably can't happen.. */
  3367. if (obj_error == bfd_error_file_ambiguously_recognized)
  3368. free (obj_matching);
  3369. goto do_copy;
  3370. }
  3371. core_error = bfd_get_error ();
  3372. /* Report the object error in preference to the core error. */
  3373. if (obj_error != core_error)
  3374. bfd_set_error (obj_error);
  3375. bfd_nonfatal_message (input_filename, NULL, NULL, NULL);
  3376. if (obj_error == bfd_error_file_ambiguously_recognized)
  3377. {
  3378. list_matching_formats (obj_matching);
  3379. free (obj_matching);
  3380. }
  3381. if (core_error == bfd_error_file_ambiguously_recognized)
  3382. {
  3383. list_matching_formats (core_matching);
  3384. free (core_matching);
  3385. }
  3386. status = 1;
  3387. }
  3388. }
  3389. /* Add a name to the section renaming list. */
  3390. static void
  3391. add_section_rename (const char * old_name, const char * new_name,
  3392. flagword flags)
  3393. {
  3394. section_rename * srename;
  3395. /* Check for conflicts first. */
  3396. for (srename = section_rename_list; srename != NULL; srename = srename->next)
  3397. if (strcmp (srename->old_name, old_name) == 0)
  3398. {
  3399. /* Silently ignore duplicate definitions. */
  3400. if (strcmp (srename->new_name, new_name) == 0
  3401. && srename->flags == flags)
  3402. return;
  3403. fatal (_("Multiple renames of section %s"), old_name);
  3404. }
  3405. srename = (section_rename *) xmalloc (sizeof (* srename));
  3406. srename->old_name = old_name;
  3407. srename->new_name = new_name;
  3408. srename->flags = flags;
  3409. srename->next = section_rename_list;
  3410. section_rename_list = srename;
  3411. }
  3412. /* Check the section rename list for a new name of the input section
  3413. called OLD_NAME. Returns the new name if one is found and sets
  3414. RETURNED_FLAGS if non-NULL to the flags to be used for this section. */
  3415. static const char *
  3416. find_section_rename (const char *old_name, flagword *returned_flags)
  3417. {
  3418. const section_rename *srename;
  3419. for (srename = section_rename_list; srename != NULL; srename = srename->next)
  3420. if (strcmp (srename->old_name, old_name) == 0)
  3421. {
  3422. if (returned_flags != NULL && srename->flags != (flagword) -1)
  3423. *returned_flags = srename->flags;
  3424. return srename->new_name;
  3425. }
  3426. return old_name;
  3427. }
  3428. /* Once each of the sections is copied, we may still need to do some
  3429. finalization work for private section headers. Do that here. */
  3430. static void
  3431. setup_bfd_headers (bfd *ibfd, bfd *obfd)
  3432. {
  3433. /* Allow the BFD backend to copy any private data it understands
  3434. from the input section to the output section. */
  3435. if (! bfd_copy_private_header_data (ibfd, obfd))
  3436. {
  3437. status = 1;
  3438. bfd_nonfatal_message (NULL, ibfd, NULL,
  3439. _("error in private header data"));
  3440. return;
  3441. }
  3442. /* All went well. */
  3443. return;
  3444. }
  3445. /* Create a section in OBFD with the same
  3446. name and attributes as ISECTION in IBFD. */
  3447. static void
  3448. setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
  3449. {
  3450. bfd *obfd = (bfd *) obfdarg;
  3451. struct section_list *p;
  3452. sec_ptr osection;
  3453. bfd_size_type size;
  3454. bfd_vma vma;
  3455. bfd_vma lma;
  3456. flagword flags;
  3457. const char *err;
  3458. const char * name;
  3459. const char * new_name;
  3460. char *prefix = NULL;
  3461. bool make_nobits;
  3462. unsigned int alignment;
  3463. if (is_strip_section (ibfd, isection))
  3464. return;
  3465. /* Get the, possibly new, name of the output section. */
  3466. name = bfd_section_name (isection);
  3467. flags = bfd_section_flags (isection);
  3468. if (bfd_get_flavour (ibfd) != bfd_get_flavour (obfd))
  3469. {
  3470. flags &= bfd_applicable_section_flags (ibfd);
  3471. flags &= bfd_applicable_section_flags (obfd);
  3472. }
  3473. new_name = find_section_rename (name, &flags);
  3474. if (new_name != name)
  3475. {
  3476. name = new_name;
  3477. flags = check_new_section_flags (flags, obfd, name);
  3478. }
  3479. /* Prefix sections. */
  3480. if (prefix_alloc_sections_string
  3481. && (bfd_section_flags (isection) & SEC_ALLOC) != 0)
  3482. prefix = prefix_alloc_sections_string;
  3483. else if (prefix_sections_string)
  3484. prefix = prefix_sections_string;
  3485. if (prefix)
  3486. {
  3487. char *n;
  3488. n = (char *) xmalloc (strlen (prefix) + strlen (name) + 1);
  3489. strcpy (n, prefix);
  3490. strcat (n, name);
  3491. name = n;
  3492. }
  3493. make_nobits = false;
  3494. p = find_section_list (bfd_section_name (isection), false,
  3495. SECTION_CONTEXT_SET_FLAGS);
  3496. if (p != NULL)
  3497. {
  3498. flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
  3499. flags = check_new_section_flags (flags, obfd, bfd_section_name (isection));
  3500. }
  3501. else if (strip_symbols == STRIP_NONDEBUG
  3502. && (flags & (SEC_ALLOC | SEC_GROUP)) != 0
  3503. && !is_nondebug_keep_contents_section (ibfd, isection))
  3504. {
  3505. flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
  3506. if (bfd_get_flavour (obfd) == bfd_target_elf_flavour)
  3507. {
  3508. make_nobits = true;
  3509. /* Twiddle the input section flags so that it seems to
  3510. elf.c:copy_private_bfd_data that section flags have not
  3511. changed between input and output sections. This hack
  3512. prevents wholesale rewriting of the program headers. */
  3513. isection->flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
  3514. }
  3515. }
  3516. osection = bfd_make_section_anyway_with_flags (obfd, name, flags);
  3517. if (osection == NULL)
  3518. {
  3519. err = _("failed to create output section");
  3520. goto loser;
  3521. }
  3522. size = bfd_section_size (isection);
  3523. size = bfd_convert_section_size (ibfd, isection, obfd, size);
  3524. if (copy_byte >= 0)
  3525. size = (size + interleave - 1) / interleave * copy_width;
  3526. else if (extract_symbol)
  3527. size = 0;
  3528. if (!bfd_set_section_size (osection, size))
  3529. {
  3530. err = _("failed to set size");
  3531. goto loser;
  3532. }
  3533. vma = bfd_section_vma (isection);
  3534. p = find_section_list (bfd_section_name (isection), false,
  3535. SECTION_CONTEXT_ALTER_VMA | SECTION_CONTEXT_SET_VMA);
  3536. if (p != NULL)
  3537. {
  3538. if (p->context & SECTION_CONTEXT_SET_VMA)
  3539. vma = p->vma_val;
  3540. else
  3541. vma += p->vma_val;
  3542. }
  3543. else
  3544. vma += change_section_address;
  3545. if (!bfd_set_section_vma (osection, vma))
  3546. {
  3547. err = _("failed to set vma");
  3548. goto loser;
  3549. }
  3550. lma = isection->lma;
  3551. p = find_section_list (bfd_section_name (isection), false,
  3552. SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_SET_LMA);
  3553. if (p != NULL)
  3554. {
  3555. if (p->context & SECTION_CONTEXT_ALTER_LMA)
  3556. lma += p->lma_val;
  3557. else
  3558. lma = p->lma_val;
  3559. }
  3560. else
  3561. lma += change_section_address;
  3562. osection->lma = lma;
  3563. p = find_section_list (bfd_section_name (isection), false,
  3564. SECTION_CONTEXT_SET_ALIGNMENT);
  3565. if (p != NULL)
  3566. alignment = p->alignment;
  3567. else
  3568. alignment = bfd_section_alignment (isection);
  3569. /* FIXME: This is probably not enough. If we change the LMA we
  3570. may have to recompute the header for the file as well. */
  3571. if (!bfd_set_section_alignment (osection, alignment))
  3572. {
  3573. err = _("failed to set alignment");
  3574. goto loser;
  3575. }
  3576. /* Copy merge entity size. */
  3577. osection->entsize = isection->entsize;
  3578. /* Copy compress status. */
  3579. osection->compress_status = isection->compress_status;
  3580. /* This used to be mangle_section; we do here to avoid using
  3581. bfd_get_section_by_name since some formats allow multiple
  3582. sections with the same name. */
  3583. isection->output_section = osection;
  3584. isection->output_offset = 0;
  3585. if ((isection->flags & SEC_GROUP) != 0)
  3586. {
  3587. asymbol *gsym = group_signature (isection);
  3588. if (gsym != NULL)
  3589. {
  3590. gsym->flags |= BSF_KEEP;
  3591. if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour)
  3592. elf_group_id (isection) = gsym;
  3593. }
  3594. }
  3595. /* Allow the BFD backend to copy any private data it understands
  3596. from the input section to the output section. */
  3597. if (!bfd_copy_private_section_data (ibfd, isection, obfd, osection))
  3598. {
  3599. err = _("failed to copy private data");
  3600. goto loser;
  3601. }
  3602. if (make_nobits)
  3603. elf_section_type (osection) = SHT_NOBITS;
  3604. /* All went well. */
  3605. return;
  3606. loser:
  3607. status = 1;
  3608. bfd_nonfatal_message (NULL, obfd, osection, err);
  3609. }
  3610. /* Return TRUE if input section ISECTION should be skipped. */
  3611. static bool
  3612. skip_section (bfd *ibfd, sec_ptr isection, bool skip_copy)
  3613. {
  3614. sec_ptr osection;
  3615. bfd_size_type size;
  3616. flagword flags;
  3617. /* If we have already failed earlier on,
  3618. do not keep on generating complaints now. */
  3619. if (status != 0)
  3620. return true;
  3621. if (extract_symbol)
  3622. return true;
  3623. if (is_strip_section (ibfd, isection))
  3624. return true;
  3625. if (is_update_section (ibfd, isection))
  3626. return true;
  3627. /* When merging a note section we skip the copying of the contents,
  3628. but not the copying of the relocs associated with the contents. */
  3629. if (skip_copy && is_mergeable_note_section (ibfd, isection))
  3630. return true;
  3631. flags = bfd_section_flags (isection);
  3632. if ((flags & SEC_GROUP) != 0)
  3633. return true;
  3634. osection = isection->output_section;
  3635. size = bfd_section_size (isection);
  3636. if (size == 0 || osection == 0)
  3637. return true;
  3638. return false;
  3639. }
  3640. /* Add section SECTION_PATTERN to the list of sections that will have their
  3641. relocations removed. */
  3642. static void
  3643. handle_remove_relocations_option (const char *section_pattern)
  3644. {
  3645. find_section_list (section_pattern, true, SECTION_CONTEXT_REMOVE_RELOCS);
  3646. }
  3647. /* Return TRUE if ISECTION from IBFD should have its relocations removed,
  3648. otherwise return FALSE. If the user has requested that relocations be
  3649. removed from a section that does not have relocations then this
  3650. function will still return TRUE. */
  3651. static bool
  3652. discard_relocations (bfd *ibfd ATTRIBUTE_UNUSED, asection *isection)
  3653. {
  3654. return (find_section_list (bfd_section_name (isection), false,
  3655. SECTION_CONTEXT_REMOVE_RELOCS) != NULL);
  3656. }
  3657. /* Wrapper for dealing with --remove-section (-R) command line arguments.
  3658. A special case is detected here, if the user asks to remove a relocation
  3659. section (one starting with ".rela" or ".rel") then this removal must
  3660. be done using a different technique in a relocatable object. */
  3661. static void
  3662. handle_remove_section_option (const char *section_pattern)
  3663. {
  3664. find_section_list (section_pattern, true, SECTION_CONTEXT_REMOVE);
  3665. if (startswith (section_pattern, ".rel"))
  3666. {
  3667. section_pattern += 4;
  3668. if (*section_pattern == 'a')
  3669. section_pattern++;
  3670. if (*section_pattern)
  3671. handle_remove_relocations_option (section_pattern);
  3672. }
  3673. sections_removed = true;
  3674. }
  3675. /* Copy relocations in input section ISECTION of IBFD to an output
  3676. section with the same name in OBFDARG. If stripping then don't
  3677. copy any relocation info. */
  3678. static void
  3679. copy_relocations_in_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
  3680. {
  3681. bfd *obfd = (bfd *) obfdarg;
  3682. long relsize;
  3683. arelent **relpp;
  3684. long relcount;
  3685. sec_ptr osection;
  3686. if (skip_section (ibfd, isection, false))
  3687. return;
  3688. osection = isection->output_section;
  3689. /* Core files and DWO files do not need to be relocated. */
  3690. if (bfd_get_format (obfd) == bfd_core
  3691. || strip_symbols == STRIP_NONDWO
  3692. || discard_relocations (ibfd, isection))
  3693. relsize = 0;
  3694. else
  3695. {
  3696. relsize = bfd_get_reloc_upper_bound (ibfd, isection);
  3697. if (relsize < 0)
  3698. {
  3699. /* Do not complain if the target does not support relocations. */
  3700. if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
  3701. relsize = 0;
  3702. else
  3703. {
  3704. status = 1;
  3705. bfd_nonfatal_message (NULL, ibfd, isection, NULL);
  3706. return;
  3707. }
  3708. }
  3709. }
  3710. if (relsize == 0)
  3711. {
  3712. bfd_set_reloc (obfd, osection, NULL, 0);
  3713. osection->flags &= ~SEC_RELOC;
  3714. }
  3715. else
  3716. {
  3717. if (isection->orelocation != NULL)
  3718. {
  3719. /* Some other function has already set up the output relocs
  3720. for us, so scan those instead of the default relocs. */
  3721. relcount = isection->reloc_count;
  3722. relpp = isection->orelocation;
  3723. }
  3724. else
  3725. {
  3726. relpp = (arelent **) xmalloc (relsize);
  3727. relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, isympp);
  3728. if (relcount < 0)
  3729. {
  3730. status = 1;
  3731. bfd_nonfatal_message (NULL, ibfd, isection,
  3732. _("relocation count is negative"));
  3733. free (relpp);
  3734. return;
  3735. }
  3736. }
  3737. if (strip_symbols == STRIP_ALL)
  3738. {
  3739. /* Remove relocations which are not in
  3740. keep_strip_specific_list. */
  3741. arelent **temp_relpp;
  3742. long temp_relcount = 0;
  3743. long i;
  3744. temp_relpp = (arelent **) xmalloc (relsize);
  3745. for (i = 0; i < relcount; i++)
  3746. {
  3747. /* PR 17512: file: 9e907e0c. */
  3748. if (relpp[i]->sym_ptr_ptr
  3749. /* PR 20096 */
  3750. && * relpp[i]->sym_ptr_ptr)
  3751. if (is_specified_symbol (bfd_asymbol_name (*relpp[i]->sym_ptr_ptr),
  3752. keep_specific_htab))
  3753. temp_relpp [temp_relcount++] = relpp [i];
  3754. }
  3755. relcount = temp_relcount;
  3756. if (relpp != isection->orelocation)
  3757. free (relpp);
  3758. relpp = temp_relpp;
  3759. }
  3760. bfd_set_reloc (obfd, osection, relcount == 0 ? NULL : relpp, relcount);
  3761. if (relcount == 0)
  3762. {
  3763. osection->flags &= ~SEC_RELOC;
  3764. if (relpp != isection->orelocation)
  3765. free (relpp);
  3766. }
  3767. }
  3768. }
  3769. /* Copy the data of input section ISECTION of IBFD
  3770. to an output section with the same name in OBFD. */
  3771. static void
  3772. copy_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
  3773. {
  3774. bfd *obfd = (bfd *) obfdarg;
  3775. struct section_list *p;
  3776. sec_ptr osection;
  3777. bfd_size_type size;
  3778. if (skip_section (ibfd, isection, true))
  3779. return;
  3780. osection = isection->output_section;
  3781. /* The output SHF_COMPRESSED section size is different from input if
  3782. ELF classes of input and output aren't the same. We can't use
  3783. the output section size since --interleave will shrink the output
  3784. section. Size will be updated if the section is converted. */
  3785. size = bfd_section_size (isection);
  3786. if (bfd_section_flags (isection) & SEC_HAS_CONTENTS
  3787. && bfd_section_flags (osection) & SEC_HAS_CONTENTS)
  3788. {
  3789. bfd_byte *memhunk = NULL;
  3790. if (!bfd_get_full_section_contents (ibfd, isection, &memhunk)
  3791. || !bfd_convert_section_contents (ibfd, isection, obfd,
  3792. &memhunk, &size))
  3793. {
  3794. status = 1;
  3795. bfd_nonfatal_message (NULL, ibfd, isection, NULL);
  3796. free (memhunk);
  3797. return;
  3798. }
  3799. if (reverse_bytes)
  3800. {
  3801. /* We don't handle leftover bytes (too many possible behaviors,
  3802. and we don't know what the user wants). The section length
  3803. must be a multiple of the number of bytes to swap. */
  3804. if ((size % reverse_bytes) == 0)
  3805. {
  3806. unsigned long i, j;
  3807. bfd_byte b;
  3808. for (i = 0; i < size; i += reverse_bytes)
  3809. for (j = 0; j < (unsigned long)(reverse_bytes / 2); j++)
  3810. {
  3811. bfd_byte *m = (bfd_byte *) memhunk;
  3812. b = m[i + j];
  3813. m[i + j] = m[(i + reverse_bytes) - (j + 1)];
  3814. m[(i + reverse_bytes) - (j + 1)] = b;
  3815. }
  3816. }
  3817. else
  3818. /* User must pad the section up in order to do this. */
  3819. fatal (_("cannot reverse bytes: length of section %s must be evenly divisible by %d"),
  3820. bfd_section_name (isection), reverse_bytes);
  3821. }
  3822. if (copy_byte >= 0)
  3823. {
  3824. /* Keep only every `copy_byte'th byte in MEMHUNK. */
  3825. char *from = (char *) memhunk + copy_byte;
  3826. char *to = (char *) memhunk;
  3827. char *end = (char *) memhunk + size;
  3828. int i;
  3829. /* If the section address is not exactly divisible by the interleave,
  3830. then we must bias the from address. If the copy_byte is less than
  3831. the bias, then we must skip forward one interleave, and increment
  3832. the final lma. */
  3833. int extra = isection->lma % interleave;
  3834. from -= extra;
  3835. if (copy_byte < extra)
  3836. from += interleave;
  3837. for (; from < end; from += interleave)
  3838. for (i = 0; i < copy_width; i++)
  3839. {
  3840. if (&from[i] >= end)
  3841. break;
  3842. *to++ = from[i];
  3843. }
  3844. size = (size + interleave - 1 - copy_byte) / interleave * copy_width;
  3845. osection->lma /= interleave;
  3846. if (copy_byte < extra)
  3847. osection->lma++;
  3848. }
  3849. if (!bfd_set_section_contents (obfd, osection, memhunk, 0, size))
  3850. {
  3851. status = 1;
  3852. bfd_nonfatal_message (NULL, obfd, osection, NULL);
  3853. free (memhunk);
  3854. return;
  3855. }
  3856. free (memhunk);
  3857. }
  3858. else if ((p = find_section_list (bfd_section_name (isection),
  3859. false, SECTION_CONTEXT_SET_FLAGS)) != NULL
  3860. && (p->flags & SEC_HAS_CONTENTS) != 0)
  3861. {
  3862. void *memhunk = xmalloc (size);
  3863. /* We don't permit the user to turn off the SEC_HAS_CONTENTS
  3864. flag--they can just remove the section entirely and add it
  3865. back again. However, we do permit them to turn on the
  3866. SEC_HAS_CONTENTS flag, and take it to mean that the section
  3867. contents should be zeroed out. */
  3868. memset (memhunk, 0, size);
  3869. if (! bfd_set_section_contents (obfd, osection, memhunk, 0, size))
  3870. {
  3871. status = 1;
  3872. bfd_nonfatal_message (NULL, obfd, osection, NULL);
  3873. free (memhunk);
  3874. return;
  3875. }
  3876. free (memhunk);
  3877. }
  3878. }
  3879. /* Get all the sections. This is used when --gap-fill or --pad-to is
  3880. used. */
  3881. static void
  3882. get_sections (bfd *obfd ATTRIBUTE_UNUSED, asection *osection, void *secppparg)
  3883. {
  3884. asection ***secppp = (asection ***) secppparg;
  3885. **secppp = osection;
  3886. ++(*secppp);
  3887. }
  3888. /* Sort sections by LMA. This is called via qsort, and is used when
  3889. --gap-fill or --pad-to is used. We force non loadable or empty
  3890. sections to the front, where they are easier to ignore. */
  3891. static int
  3892. compare_section_lma (const void *arg1, const void *arg2)
  3893. {
  3894. const asection *sec1 = *(const asection **) arg1;
  3895. const asection *sec2 = *(const asection **) arg2;
  3896. flagword flags1, flags2;
  3897. /* Sort non loadable sections to the front. */
  3898. flags1 = sec1->flags;
  3899. flags2 = sec2->flags;
  3900. if ((flags1 & SEC_HAS_CONTENTS) == 0
  3901. || (flags1 & SEC_LOAD) == 0)
  3902. {
  3903. if ((flags2 & SEC_HAS_CONTENTS) != 0
  3904. && (flags2 & SEC_LOAD) != 0)
  3905. return -1;
  3906. }
  3907. else
  3908. {
  3909. if ((flags2 & SEC_HAS_CONTENTS) == 0
  3910. || (flags2 & SEC_LOAD) == 0)
  3911. return 1;
  3912. }
  3913. /* Sort sections by LMA. */
  3914. if (sec1->lma > sec2->lma)
  3915. return 1;
  3916. if (sec1->lma < sec2->lma)
  3917. return -1;
  3918. /* Sort sections with the same LMA by size. */
  3919. if (bfd_section_size (sec1) > bfd_section_size (sec2))
  3920. return 1;
  3921. if (bfd_section_size (sec1) < bfd_section_size (sec2))
  3922. return -1;
  3923. if (sec1->id > sec2->id)
  3924. return 1;
  3925. if (sec1->id < sec2->id)
  3926. return -1;
  3927. return 0;
  3928. }
  3929. /* Mark all the symbols which will be used in output relocations with
  3930. the BSF_KEEP flag so that those symbols will not be stripped.
  3931. Ignore relocations which will not appear in the output file. */
  3932. static void
  3933. mark_symbols_used_in_relocations (bfd *ibfd, sec_ptr isection, void *symbolsarg)
  3934. {
  3935. asymbol **symbols = (asymbol **) symbolsarg;
  3936. long relsize;
  3937. arelent **relpp;
  3938. long relcount, i;
  3939. /* Ignore an input section with no corresponding output section. */
  3940. if (isection->output_section == NULL)
  3941. return;
  3942. relsize = bfd_get_reloc_upper_bound (ibfd, isection);
  3943. if (relsize < 0)
  3944. {
  3945. /* Do not complain if the target does not support relocations. */
  3946. if (relsize == -1 && bfd_get_error () == bfd_error_invalid_operation)
  3947. return;
  3948. bfd_fatal (bfd_get_filename (ibfd));
  3949. }
  3950. if (relsize == 0)
  3951. return;
  3952. relpp = (arelent **) xmalloc (relsize);
  3953. relcount = bfd_canonicalize_reloc (ibfd, isection, relpp, symbols);
  3954. if (relcount < 0)
  3955. bfd_fatal (bfd_get_filename (ibfd));
  3956. /* Examine each symbol used in a relocation. If it's not one of the
  3957. special bfd section symbols, then mark it with BSF_KEEP. */
  3958. for (i = 0; i < relcount; i++)
  3959. {
  3960. /* See PRs 20923 and 20930 for reproducers for the NULL tests. */
  3961. if (relpp[i]->sym_ptr_ptr != NULL
  3962. && * relpp[i]->sym_ptr_ptr != NULL
  3963. && *relpp[i]->sym_ptr_ptr != bfd_com_section_ptr->symbol
  3964. && *relpp[i]->sym_ptr_ptr != bfd_abs_section_ptr->symbol
  3965. && *relpp[i]->sym_ptr_ptr != bfd_und_section_ptr->symbol)
  3966. (*relpp[i]->sym_ptr_ptr)->flags |= BSF_KEEP;
  3967. }
  3968. free (relpp);
  3969. }
  3970. /* Write out debugging information. */
  3971. static bool
  3972. write_debugging_info (bfd *obfd, void *dhandle,
  3973. long *symcountp ATTRIBUTE_UNUSED,
  3974. asymbol ***symppp ATTRIBUTE_UNUSED)
  3975. {
  3976. if (bfd_get_flavour (obfd) == bfd_target_coff_flavour
  3977. || bfd_get_flavour (obfd) == bfd_target_elf_flavour)
  3978. {
  3979. bfd_byte *syms, *strings = NULL;
  3980. bfd_size_type symsize, stringsize;
  3981. asection *stabsec, *stabstrsec;
  3982. flagword flags;
  3983. if (! write_stabs_in_sections_debugging_info (obfd, dhandle, &syms,
  3984. &symsize, &strings,
  3985. &stringsize))
  3986. return false;
  3987. flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
  3988. stabsec = bfd_make_section_with_flags (obfd, ".stab", flags);
  3989. stabstrsec = bfd_make_section_with_flags (obfd, ".stabstr", flags);
  3990. if (stabsec == NULL
  3991. || stabstrsec == NULL
  3992. || !bfd_set_section_size (stabsec, symsize)
  3993. || !bfd_set_section_size (stabstrsec, stringsize)
  3994. || !bfd_set_section_alignment (stabsec, 2)
  3995. || !bfd_set_section_alignment (stabstrsec, 0))
  3996. {
  3997. bfd_nonfatal_message (NULL, obfd, NULL,
  3998. _("can't create debugging section"));
  3999. free (strings);
  4000. return false;
  4001. }
  4002. /* We can get away with setting the section contents now because
  4003. the next thing the caller is going to do is copy over the
  4004. real sections. We may someday have to split the contents
  4005. setting out of this function. */
  4006. if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
  4007. || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
  4008. stringsize))
  4009. {
  4010. bfd_nonfatal_message (NULL, obfd, NULL,
  4011. _("can't set debugging section contents"));
  4012. free (strings);
  4013. return false;
  4014. }
  4015. return true;
  4016. }
  4017. bfd_nonfatal_message (NULL, obfd, NULL,
  4018. _("don't know how to write debugging information for %s"),
  4019. bfd_get_target (obfd));
  4020. return false;
  4021. }
  4022. /* If neither -D nor -U was specified explicitly,
  4023. then use the configured default. */
  4024. static void
  4025. default_deterministic (void)
  4026. {
  4027. if (deterministic < 0)
  4028. deterministic = DEFAULT_AR_DETERMINISTIC;
  4029. }
  4030. static int
  4031. strip_main (int argc, char *argv[])
  4032. {
  4033. char *input_target = NULL;
  4034. char *output_target = NULL;
  4035. bool show_version = false;
  4036. bool formats_info = false;
  4037. int c;
  4038. int i;
  4039. char *output_file = NULL;
  4040. bool merge_notes_set = false;
  4041. while ((c = getopt_long (argc, argv, "I:O:F:K:MN:R:o:sSpdgxXHhVvwDU",
  4042. strip_options, (int *) 0)) != EOF)
  4043. {
  4044. switch (c)
  4045. {
  4046. case 'I':
  4047. input_target = optarg;
  4048. break;
  4049. case 'O':
  4050. output_target = optarg;
  4051. break;
  4052. case 'F':
  4053. input_target = output_target = optarg;
  4054. break;
  4055. case 'R':
  4056. handle_remove_section_option (optarg);
  4057. break;
  4058. case OPTION_KEEP_SECTION:
  4059. find_section_list (optarg, true, SECTION_CONTEXT_KEEP);
  4060. break;
  4061. case OPTION_REMOVE_RELOCS:
  4062. handle_remove_relocations_option (optarg);
  4063. break;
  4064. case 's':
  4065. strip_symbols = STRIP_ALL;
  4066. break;
  4067. case 'S':
  4068. case 'g':
  4069. case 'd': /* Historic BSD alias for -g. Used by early NetBSD. */
  4070. strip_symbols = STRIP_DEBUG;
  4071. break;
  4072. case OPTION_STRIP_DWO:
  4073. strip_symbols = STRIP_DWO;
  4074. break;
  4075. case OPTION_STRIP_UNNEEDED:
  4076. strip_symbols = STRIP_UNNEEDED;
  4077. break;
  4078. case 'K':
  4079. add_specific_symbol (optarg, keep_specific_htab);
  4080. break;
  4081. case 'M':
  4082. merge_notes = true;
  4083. merge_notes_set = true;
  4084. break;
  4085. case OPTION_NO_MERGE_NOTES:
  4086. merge_notes = false;
  4087. merge_notes_set = true;
  4088. break;
  4089. case 'N':
  4090. add_specific_symbol (optarg, strip_specific_htab);
  4091. break;
  4092. case 'o':
  4093. output_file = optarg;
  4094. break;
  4095. case 'p':
  4096. preserve_dates = true;
  4097. break;
  4098. case 'D':
  4099. deterministic = true;
  4100. break;
  4101. case 'U':
  4102. deterministic = false;
  4103. break;
  4104. case 'x':
  4105. discard_locals = LOCALS_ALL;
  4106. break;
  4107. case 'X':
  4108. discard_locals = LOCALS_START_L;
  4109. break;
  4110. case 'v':
  4111. verbose = true;
  4112. break;
  4113. case 'V':
  4114. show_version = true;
  4115. break;
  4116. case OPTION_FORMATS_INFO:
  4117. formats_info = true;
  4118. break;
  4119. case OPTION_ONLY_KEEP_DEBUG:
  4120. strip_symbols = STRIP_NONDEBUG;
  4121. break;
  4122. case OPTION_KEEP_FILE_SYMBOLS:
  4123. keep_file_symbols = 1;
  4124. break;
  4125. case OPTION_KEEP_SECTION_SYMBOLS:
  4126. keep_section_symbols = true;
  4127. break;
  4128. case 0:
  4129. /* We've been given a long option. */
  4130. break;
  4131. case 'w':
  4132. wildcard = true;
  4133. break;
  4134. case 'H':
  4135. case 'h':
  4136. strip_usage (stdout, 0);
  4137. default:
  4138. strip_usage (stderr, 1);
  4139. }
  4140. }
  4141. /* If the user has not expressly chosen to merge/not-merge ELF notes
  4142. then enable the merging unless we are stripping debug or dwo info. */
  4143. if (! merge_notes_set
  4144. && (strip_symbols == STRIP_UNDEF
  4145. || strip_symbols == STRIP_ALL
  4146. || strip_symbols == STRIP_UNNEEDED
  4147. || strip_symbols == STRIP_NONDEBUG
  4148. || strip_symbols == STRIP_NONDWO))
  4149. merge_notes = true;
  4150. if (formats_info)
  4151. {
  4152. display_info ();
  4153. return 0;
  4154. }
  4155. if (show_version)
  4156. print_version ("strip");
  4157. default_deterministic ();
  4158. /* Default is to strip all symbols. */
  4159. if (strip_symbols == STRIP_UNDEF
  4160. && discard_locals == LOCALS_UNDEF
  4161. && htab_elements (strip_specific_htab) == 0)
  4162. strip_symbols = STRIP_ALL;
  4163. if (output_target == NULL)
  4164. output_target = input_target;
  4165. i = optind;
  4166. if (i == argc
  4167. || (output_file != NULL && (i + 1) < argc))
  4168. strip_usage (stderr, 1);
  4169. for (; i < argc; i++)
  4170. {
  4171. int hold_status = status;
  4172. struct stat statbuf;
  4173. char *tmpname;
  4174. int tmpfd = -1;
  4175. int copyfd = -1;
  4176. if (get_file_size (argv[i]) < 1)
  4177. {
  4178. status = 1;
  4179. continue;
  4180. }
  4181. if (output_file == NULL
  4182. || filename_cmp (argv[i], output_file) == 0)
  4183. {
  4184. tmpname = make_tempname (argv[i], &tmpfd);
  4185. if (tmpfd >= 0)
  4186. copyfd = dup (tmpfd);
  4187. }
  4188. else
  4189. tmpname = output_file;
  4190. if (tmpname == NULL)
  4191. {
  4192. bfd_nonfatal_message (argv[i], NULL, NULL,
  4193. _("could not create temporary file to hold stripped copy"));
  4194. status = 1;
  4195. continue;
  4196. }
  4197. status = 0;
  4198. copy_file (argv[i], tmpname, tmpfd, &statbuf, input_target,
  4199. output_target, NULL);
  4200. if (status == 0)
  4201. {
  4202. const char *oname = output_file ? output_file : argv[i];
  4203. status = smart_rename (tmpname, oname, copyfd,
  4204. &statbuf, preserve_dates) != 0;
  4205. if (status == 0)
  4206. status = hold_status;
  4207. }
  4208. else
  4209. {
  4210. if (copyfd >= 0)
  4211. close (copyfd);
  4212. unlink_if_ordinary (tmpname);
  4213. }
  4214. if (output_file != tmpname)
  4215. free (tmpname);
  4216. }
  4217. return status;
  4218. }
  4219. /* Set up PE subsystem. */
  4220. static void
  4221. set_pe_subsystem (const char *s)
  4222. {
  4223. const char *version, *subsystem;
  4224. size_t i;
  4225. static const struct
  4226. {
  4227. const char *name;
  4228. const char set_def;
  4229. const short value;
  4230. }
  4231. v[] =
  4232. {
  4233. { "native", 0, IMAGE_SUBSYSTEM_NATIVE },
  4234. { "windows", 0, IMAGE_SUBSYSTEM_WINDOWS_GUI },
  4235. { "console", 0, IMAGE_SUBSYSTEM_WINDOWS_CUI },
  4236. { "posix", 0, IMAGE_SUBSYSTEM_POSIX_CUI },
  4237. { "wince", 0, IMAGE_SUBSYSTEM_WINDOWS_CE_GUI },
  4238. { "efi-app", 1, IMAGE_SUBSYSTEM_EFI_APPLICATION },
  4239. { "efi-bsd", 1, IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER },
  4240. { "efi-rtd", 1, IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER },
  4241. { "sal-rtd", 1, IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER },
  4242. { "xbox", 0, IMAGE_SUBSYSTEM_XBOX }
  4243. };
  4244. short value;
  4245. char *copy;
  4246. int set_def = -1;
  4247. /* Check for the presence of a version number. */
  4248. version = strchr (s, ':');
  4249. if (version == NULL)
  4250. subsystem = s;
  4251. else
  4252. {
  4253. int len = version - s;
  4254. copy = xstrdup (s);
  4255. subsystem = copy;
  4256. copy[len] = '\0';
  4257. version = copy + 1 + len;
  4258. pe_major_subsystem_version = strtoul (version, &copy, 0);
  4259. if (*copy == '.')
  4260. pe_minor_subsystem_version = strtoul (copy + 1, &copy, 0);
  4261. if (*copy != '\0')
  4262. non_fatal (_("%s: bad version in PE subsystem"), s);
  4263. }
  4264. /* Check for numeric subsystem. */
  4265. value = (short) strtol (subsystem, &copy, 0);
  4266. if (*copy == '\0')
  4267. {
  4268. for (i = 0; i < ARRAY_SIZE (v); i++)
  4269. if (v[i].value == value)
  4270. {
  4271. pe_subsystem = value;
  4272. set_def = v[i].set_def;
  4273. break;
  4274. }
  4275. }
  4276. else
  4277. {
  4278. /* Search for subsystem by name. */
  4279. for (i = 0; i < ARRAY_SIZE (v); i++)
  4280. if (strcmp (subsystem, v[i].name) == 0)
  4281. {
  4282. pe_subsystem = v[i].value;
  4283. set_def = v[i].set_def;
  4284. break;
  4285. }
  4286. }
  4287. switch (set_def)
  4288. {
  4289. case -1:
  4290. fatal (_("unknown PE subsystem: %s"), s);
  4291. break;
  4292. case 0:
  4293. break;
  4294. default:
  4295. if (pe_file_alignment == (bfd_vma) -1)
  4296. pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
  4297. if (pe_section_alignment == (bfd_vma) -1)
  4298. pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
  4299. break;
  4300. }
  4301. if (s != subsystem)
  4302. free ((char *) subsystem);
  4303. }
  4304. /* Convert EFI target to PEI target. */
  4305. static int
  4306. convert_efi_target (char **targ)
  4307. {
  4308. size_t len;
  4309. char *pei;
  4310. char *efi = *targ + 4;
  4311. int subsys = -1;
  4312. if (startswith (efi, "app-"))
  4313. subsys = IMAGE_SUBSYSTEM_EFI_APPLICATION;
  4314. else if (startswith (efi, "bsdrv-"))
  4315. {
  4316. subsys = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER;
  4317. efi += 2;
  4318. }
  4319. else if (startswith (efi, "rtdrv-"))
  4320. {
  4321. subsys = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER;
  4322. efi += 2;
  4323. }
  4324. else
  4325. return subsys;
  4326. len = strlen (efi);
  4327. pei = xmalloc (len + sizeof ("-little"));
  4328. memcpy (pei, efi, len + 1);
  4329. pei[0] = 'p';
  4330. pei[1] = 'e';
  4331. pei[2] = 'i';
  4332. if (strcmp (efi + 4, "ia32") == 0)
  4333. {
  4334. /* Change ia32 to i386. */
  4335. pei[5]= '3';
  4336. pei[6]= '8';
  4337. pei[7]= '6';
  4338. }
  4339. else if (strcmp (efi + 4, "x86_64") == 0)
  4340. {
  4341. /* Change x86_64 to x86-64. */
  4342. pei[7] = '-';
  4343. }
  4344. else if (strcmp (efi + 4, "aarch64") == 0)
  4345. {
  4346. /* Change aarch64 to aarch64-little. */
  4347. memcpy (pei + 4 + sizeof ("aarch64") - 1, "-little", sizeof ("-little"));
  4348. }
  4349. *targ = pei;
  4350. return subsys;
  4351. }
  4352. /* Allocate and return a pointer to a struct section_add, initializing the
  4353. structure using ARG, a string in the format "sectionname=filename".
  4354. The returned structure will have its next pointer set to NEXT. The
  4355. OPTION field is the name of the command line option currently being
  4356. parsed, and is only used if an error needs to be reported. */
  4357. static struct section_add *
  4358. init_section_add (const char *arg,
  4359. struct section_add *next,
  4360. const char *option)
  4361. {
  4362. struct section_add *pa;
  4363. const char *s;
  4364. s = strchr (arg, '=');
  4365. if (s == NULL)
  4366. fatal (_("bad format for %s"), option);
  4367. pa = (struct section_add *) xmalloc (sizeof (struct section_add));
  4368. pa->name = xstrndup (arg, s - arg);
  4369. pa->filename = s + 1;
  4370. pa->next = next;
  4371. pa->contents = NULL;
  4372. pa->size = 0;
  4373. return pa;
  4374. }
  4375. /* Load the file specified in PA, allocating memory to hold the file
  4376. contents, and store a pointer to the allocated memory in the contents
  4377. field of PA. The size field of PA is also updated. All errors call
  4378. FATAL. */
  4379. static void
  4380. section_add_load_file (struct section_add *pa)
  4381. {
  4382. size_t off, alloc;
  4383. FILE *f;
  4384. /* We don't use get_file_size so that we can do
  4385. --add-section .note.GNU_stack=/dev/null
  4386. get_file_size doesn't work on /dev/null. */
  4387. f = fopen (pa->filename, FOPEN_RB);
  4388. if (f == NULL)
  4389. fatal (_("cannot open: %s: %s"),
  4390. pa->filename, strerror (errno));
  4391. off = 0;
  4392. alloc = 4096;
  4393. pa->contents = (bfd_byte *) xmalloc (alloc);
  4394. while (!feof (f))
  4395. {
  4396. off_t got;
  4397. if (off == alloc)
  4398. {
  4399. alloc <<= 1;
  4400. pa->contents = (bfd_byte *) xrealloc (pa->contents, alloc);
  4401. }
  4402. got = fread (pa->contents + off, 1, alloc - off, f);
  4403. if (ferror (f))
  4404. fatal (_("%s: fread failed"), pa->filename);
  4405. off += got;
  4406. }
  4407. pa->size = off;
  4408. fclose (f);
  4409. }
  4410. static int
  4411. copy_main (int argc, char *argv[])
  4412. {
  4413. char *input_filename = NULL;
  4414. char *output_filename = NULL;
  4415. char *tmpname;
  4416. char *input_target = NULL;
  4417. char *output_target = NULL;
  4418. bool show_version = false;
  4419. bool change_warn = true;
  4420. bool formats_info = false;
  4421. bool use_globalize = false;
  4422. bool use_keep_global = false;
  4423. int c;
  4424. int tmpfd = -1;
  4425. int copyfd;
  4426. struct stat statbuf;
  4427. const bfd_arch_info_type *input_arch = NULL;
  4428. while ((c = getopt_long (argc, argv, "b:B:i:I:j:K:MN:s:O:d:F:L:G:R:SpgxXHhVvW:wDU",
  4429. copy_options, (int *) 0)) != EOF)
  4430. {
  4431. switch (c)
  4432. {
  4433. case 'b':
  4434. copy_byte = atoi (optarg);
  4435. if (copy_byte < 0)
  4436. fatal (_("byte number must be non-negative"));
  4437. break;
  4438. case 'B':
  4439. input_arch = bfd_scan_arch (optarg);
  4440. if (input_arch == NULL)
  4441. fatal (_("architecture %s unknown"), optarg);
  4442. break;
  4443. case 'i':
  4444. if (optarg)
  4445. {
  4446. interleave = atoi (optarg);
  4447. if (interleave < 1)
  4448. fatal (_("interleave must be positive"));
  4449. }
  4450. else
  4451. interleave = 4;
  4452. break;
  4453. case OPTION_INTERLEAVE_WIDTH:
  4454. copy_width = atoi (optarg);
  4455. if (copy_width < 1)
  4456. fatal(_("interleave width must be positive"));
  4457. break;
  4458. case 'I':
  4459. case 's': /* "source" - 'I' is preferred */
  4460. input_target = optarg;
  4461. break;
  4462. case 'O':
  4463. case 'd': /* "destination" - 'O' is preferred */
  4464. output_target = optarg;
  4465. break;
  4466. case 'F':
  4467. input_target = output_target = optarg;
  4468. break;
  4469. case 'j':
  4470. find_section_list (optarg, true, SECTION_CONTEXT_COPY);
  4471. sections_copied = true;
  4472. break;
  4473. case 'R':
  4474. handle_remove_section_option (optarg);
  4475. break;
  4476. case OPTION_KEEP_SECTION:
  4477. find_section_list (optarg, true, SECTION_CONTEXT_KEEP);
  4478. break;
  4479. case OPTION_REMOVE_RELOCS:
  4480. handle_remove_relocations_option (optarg);
  4481. break;
  4482. case 'S':
  4483. strip_symbols = STRIP_ALL;
  4484. break;
  4485. case 'g':
  4486. strip_symbols = STRIP_DEBUG;
  4487. break;
  4488. case OPTION_STRIP_DWO:
  4489. strip_symbols = STRIP_DWO;
  4490. break;
  4491. case OPTION_STRIP_UNNEEDED:
  4492. strip_symbols = STRIP_UNNEEDED;
  4493. break;
  4494. case OPTION_ONLY_KEEP_DEBUG:
  4495. strip_symbols = STRIP_NONDEBUG;
  4496. break;
  4497. case OPTION_KEEP_FILE_SYMBOLS:
  4498. keep_file_symbols = 1;
  4499. break;
  4500. case OPTION_ADD_GNU_DEBUGLINK:
  4501. long_section_names = ENABLE ;
  4502. gnu_debuglink_filename = optarg;
  4503. break;
  4504. case 'K':
  4505. add_specific_symbol (optarg, keep_specific_htab);
  4506. break;
  4507. case 'M':
  4508. merge_notes = true;
  4509. break;
  4510. case OPTION_NO_MERGE_NOTES:
  4511. merge_notes = false;
  4512. break;
  4513. case 'N':
  4514. add_specific_symbol (optarg, strip_specific_htab);
  4515. break;
  4516. case OPTION_STRIP_UNNEEDED_SYMBOL:
  4517. add_specific_symbol (optarg, strip_unneeded_htab);
  4518. break;
  4519. case 'L':
  4520. add_specific_symbol (optarg, localize_specific_htab);
  4521. break;
  4522. case OPTION_GLOBALIZE_SYMBOL:
  4523. use_globalize = true;
  4524. add_specific_symbol (optarg, globalize_specific_htab);
  4525. break;
  4526. case 'G':
  4527. use_keep_global = true;
  4528. add_specific_symbol (optarg, keepglobal_specific_htab);
  4529. break;
  4530. case 'W':
  4531. add_specific_symbol (optarg, weaken_specific_htab);
  4532. break;
  4533. case 'p':
  4534. preserve_dates = true;
  4535. break;
  4536. case 'D':
  4537. deterministic = true;
  4538. break;
  4539. case 'U':
  4540. deterministic = false;
  4541. break;
  4542. case 'w':
  4543. wildcard = true;
  4544. break;
  4545. case 'x':
  4546. discard_locals = LOCALS_ALL;
  4547. break;
  4548. case 'X':
  4549. discard_locals = LOCALS_START_L;
  4550. break;
  4551. case 'v':
  4552. verbose = true;
  4553. break;
  4554. case 'V':
  4555. show_version = true;
  4556. break;
  4557. case OPTION_FORMATS_INFO:
  4558. formats_info = true;
  4559. break;
  4560. case OPTION_WEAKEN:
  4561. weaken = true;
  4562. break;
  4563. case OPTION_ADD_SECTION:
  4564. add_sections = init_section_add (optarg, add_sections,
  4565. "--add-section");
  4566. section_add_load_file (add_sections);
  4567. break;
  4568. case OPTION_UPDATE_SECTION:
  4569. update_sections = init_section_add (optarg, update_sections,
  4570. "--update-section");
  4571. section_add_load_file (update_sections);
  4572. break;
  4573. case OPTION_DUMP_SECTION:
  4574. dump_sections = init_section_add (optarg, dump_sections,
  4575. "--dump-section");
  4576. break;
  4577. case OPTION_ADD_SYMBOL:
  4578. {
  4579. char *s, *t;
  4580. struct addsym_node *newsym = xmalloc (sizeof *newsym);
  4581. newsym->next = NULL;
  4582. s = strchr (optarg, '=');
  4583. if (s == NULL)
  4584. fatal (_("bad format for %s"), "--add-symbol");
  4585. t = strchr (s + 1, ':');
  4586. newsym->symdef = xstrndup (optarg, s - optarg);
  4587. if (t)
  4588. {
  4589. newsym->section = xstrndup (s + 1, t - (s + 1));
  4590. newsym->symval = strtol (t + 1, NULL, 0);
  4591. }
  4592. else
  4593. {
  4594. newsym->section = NULL;
  4595. newsym->symval = strtol (s + 1, NULL, 0);
  4596. t = s;
  4597. }
  4598. t = strchr (t + 1, ',');
  4599. newsym->othersym = NULL;
  4600. if (t)
  4601. newsym->flags = parse_symflags (t+1, &newsym->othersym);
  4602. else
  4603. newsym->flags = BSF_GLOBAL;
  4604. /* Keep 'othersym' symbols at the front of the list. */
  4605. if (newsym->othersym)
  4606. {
  4607. newsym->next = add_sym_list;
  4608. if (!add_sym_list)
  4609. add_sym_tail = &newsym->next;
  4610. add_sym_list = newsym;
  4611. }
  4612. else
  4613. {
  4614. *add_sym_tail = newsym;
  4615. add_sym_tail = &newsym->next;
  4616. }
  4617. add_symbols++;
  4618. }
  4619. break;
  4620. case OPTION_CHANGE_START:
  4621. change_start = parse_vma (optarg, "--change-start");
  4622. break;
  4623. case OPTION_CHANGE_SECTION_ADDRESS:
  4624. case OPTION_CHANGE_SECTION_LMA:
  4625. case OPTION_CHANGE_SECTION_VMA:
  4626. {
  4627. struct section_list * p;
  4628. unsigned int context = 0;
  4629. const char *s;
  4630. int len;
  4631. char *name;
  4632. char *option = NULL;
  4633. bfd_vma val;
  4634. switch (c)
  4635. {
  4636. case OPTION_CHANGE_SECTION_ADDRESS:
  4637. option = "--change-section-address";
  4638. context = SECTION_CONTEXT_ALTER_LMA | SECTION_CONTEXT_ALTER_VMA;
  4639. break;
  4640. case OPTION_CHANGE_SECTION_LMA:
  4641. option = "--change-section-lma";
  4642. context = SECTION_CONTEXT_ALTER_LMA;
  4643. break;
  4644. case OPTION_CHANGE_SECTION_VMA:
  4645. option = "--change-section-vma";
  4646. context = SECTION_CONTEXT_ALTER_VMA;
  4647. break;
  4648. }
  4649. s = strchr (optarg, '=');
  4650. if (s == NULL)
  4651. {
  4652. s = strchr (optarg, '+');
  4653. if (s == NULL)
  4654. {
  4655. s = strchr (optarg, '-');
  4656. if (s == NULL)
  4657. fatal (_("bad format for %s"), option);
  4658. }
  4659. }
  4660. else
  4661. {
  4662. /* Correct the context. */
  4663. switch (c)
  4664. {
  4665. case OPTION_CHANGE_SECTION_ADDRESS:
  4666. context = SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_SET_VMA;
  4667. break;
  4668. case OPTION_CHANGE_SECTION_LMA:
  4669. context = SECTION_CONTEXT_SET_LMA;
  4670. break;
  4671. case OPTION_CHANGE_SECTION_VMA:
  4672. context = SECTION_CONTEXT_SET_VMA;
  4673. break;
  4674. }
  4675. }
  4676. len = s - optarg;
  4677. name = (char *) xmalloc (len + 1);
  4678. strncpy (name, optarg, len);
  4679. name[len] = '\0';
  4680. p = find_section_list (name, true, context);
  4681. val = parse_vma (s + 1, option);
  4682. if (*s == '-')
  4683. val = - val;
  4684. switch (c)
  4685. {
  4686. case OPTION_CHANGE_SECTION_ADDRESS:
  4687. p->vma_val = val;
  4688. /* Fall through. */
  4689. case OPTION_CHANGE_SECTION_LMA:
  4690. p->lma_val = val;
  4691. break;
  4692. case OPTION_CHANGE_SECTION_VMA:
  4693. p->vma_val = val;
  4694. break;
  4695. }
  4696. }
  4697. break;
  4698. case OPTION_CHANGE_ADDRESSES:
  4699. change_section_address = parse_vma (optarg, "--change-addresses");
  4700. change_start = change_section_address;
  4701. break;
  4702. case OPTION_CHANGE_WARNINGS:
  4703. change_warn = true;
  4704. break;
  4705. case OPTION_CHANGE_LEADING_CHAR:
  4706. change_leading_char = true;
  4707. break;
  4708. case OPTION_COMPRESS_DEBUG_SECTIONS:
  4709. if (optarg)
  4710. {
  4711. if (strcasecmp (optarg, "none") == 0)
  4712. do_debug_sections = decompress;
  4713. else if (strcasecmp (optarg, "zlib") == 0)
  4714. do_debug_sections = compress_zlib;
  4715. else if (strcasecmp (optarg, "zlib-gnu") == 0)
  4716. do_debug_sections = compress_gnu_zlib;
  4717. else if (strcasecmp (optarg, "zlib-gabi") == 0)
  4718. do_debug_sections = compress_gabi_zlib;
  4719. else
  4720. fatal (_("unrecognized --compress-debug-sections type `%s'"),
  4721. optarg);
  4722. }
  4723. else
  4724. do_debug_sections = compress;
  4725. break;
  4726. case OPTION_DEBUGGING:
  4727. convert_debugging = true;
  4728. break;
  4729. case OPTION_DECOMPRESS_DEBUG_SECTIONS:
  4730. do_debug_sections = decompress;
  4731. break;
  4732. case OPTION_ELF_STT_COMMON:
  4733. if (strcasecmp (optarg, "yes") == 0)
  4734. do_elf_stt_common = elf_stt_common;
  4735. else if (strcasecmp (optarg, "no") == 0)
  4736. do_elf_stt_common = no_elf_stt_common;
  4737. else
  4738. fatal (_("unrecognized --elf-stt-common= option `%s'"),
  4739. optarg);
  4740. break;
  4741. case OPTION_GAP_FILL:
  4742. {
  4743. bfd_vma gap_fill_vma;
  4744. gap_fill_vma = parse_vma (optarg, "--gap-fill");
  4745. gap_fill = (bfd_byte) gap_fill_vma;
  4746. if ((bfd_vma) gap_fill != gap_fill_vma)
  4747. {
  4748. char buff[20];
  4749. sprintf_vma (buff, gap_fill_vma);
  4750. non_fatal (_("Warning: truncating gap-fill from 0x%s to 0x%x"),
  4751. buff, gap_fill);
  4752. }
  4753. gap_fill_set = true;
  4754. }
  4755. break;
  4756. case OPTION_NO_CHANGE_WARNINGS:
  4757. change_warn = false;
  4758. break;
  4759. case OPTION_PAD_TO:
  4760. pad_to = parse_vma (optarg, "--pad-to");
  4761. pad_to_set = true;
  4762. break;
  4763. case OPTION_REMOVE_LEADING_CHAR:
  4764. remove_leading_char = true;
  4765. break;
  4766. case OPTION_REDEFINE_SYM:
  4767. {
  4768. /* Insert this redefinition onto redefine_specific_htab. */
  4769. int len;
  4770. const char *s;
  4771. const char *nextarg;
  4772. char *source, *target;
  4773. s = strchr (optarg, '=');
  4774. if (s == NULL)
  4775. fatal (_("bad format for %s"), "--redefine-sym");
  4776. len = s - optarg;
  4777. source = (char *) xmalloc (len + 1);
  4778. strncpy (source, optarg, len);
  4779. source[len] = '\0';
  4780. nextarg = s + 1;
  4781. len = strlen (nextarg);
  4782. target = (char *) xmalloc (len + 1);
  4783. strcpy (target, nextarg);
  4784. add_redefine_and_check ("--redefine-sym", source, target);
  4785. free (source);
  4786. free (target);
  4787. }
  4788. break;
  4789. case OPTION_REDEFINE_SYMS:
  4790. add_redefine_syms_file (optarg);
  4791. break;
  4792. case OPTION_SET_SECTION_FLAGS:
  4793. {
  4794. struct section_list *p;
  4795. const char *s;
  4796. int len;
  4797. char *name;
  4798. s = strchr (optarg, '=');
  4799. if (s == NULL)
  4800. fatal (_("bad format for %s"), "--set-section-flags");
  4801. len = s - optarg;
  4802. name = (char *) xmalloc (len + 1);
  4803. strncpy (name, optarg, len);
  4804. name[len] = '\0';
  4805. p = find_section_list (name, true, SECTION_CONTEXT_SET_FLAGS);
  4806. p->flags = parse_flags (s + 1);
  4807. }
  4808. break;
  4809. case OPTION_SET_SECTION_ALIGNMENT:
  4810. {
  4811. struct section_list *p;
  4812. const char *s;
  4813. int len;
  4814. char *name;
  4815. int palign, align;
  4816. s = strchr (optarg, '=');
  4817. if (s == NULL)
  4818. fatal (_("bad format for --set-section-alignment: argument needed"));
  4819. align = atoi (s + 1);
  4820. if (align <= 0)
  4821. fatal (_("bad format for --set-section-alignment: numeric argument needed"));
  4822. /* Convert integer alignment into a power-of-two alignment. */
  4823. palign = 0;
  4824. while ((align & 1) == 0)
  4825. {
  4826. align >>= 1;
  4827. ++palign;
  4828. }
  4829. if (align != 1)
  4830. /* Number has more than on 1, i.e. wasn't a power of 2. */
  4831. fatal (_("bad format for --set-section-alignment: alignment is not a power of two"));
  4832. /* Add the alignment setting to the section list. */
  4833. len = s - optarg;
  4834. name = (char *) xmalloc (len + 1);
  4835. strncpy (name, optarg, len);
  4836. name[len] = '\0';
  4837. p = find_section_list (name, true, SECTION_CONTEXT_SET_ALIGNMENT);
  4838. if (p)
  4839. p->alignment = palign;
  4840. }
  4841. break;
  4842. case OPTION_RENAME_SECTION:
  4843. {
  4844. flagword flags;
  4845. const char *eq, *fl;
  4846. char *old_name;
  4847. char *new_name;
  4848. unsigned int len;
  4849. eq = strchr (optarg, '=');
  4850. if (eq == NULL)
  4851. fatal (_("bad format for %s"), "--rename-section");
  4852. len = eq - optarg;
  4853. if (len == 0)
  4854. fatal (_("bad format for %s"), "--rename-section");
  4855. old_name = (char *) xmalloc (len + 1);
  4856. strncpy (old_name, optarg, len);
  4857. old_name[len] = 0;
  4858. eq++;
  4859. fl = strchr (eq, ',');
  4860. if (fl)
  4861. {
  4862. flags = parse_flags (fl + 1);
  4863. len = fl - eq;
  4864. }
  4865. else
  4866. {
  4867. flags = -1;
  4868. len = strlen (eq);
  4869. }
  4870. if (len == 0)
  4871. fatal (_("bad format for %s"), "--rename-section");
  4872. new_name = (char *) xmalloc (len + 1);
  4873. strncpy (new_name, eq, len);
  4874. new_name[len] = 0;
  4875. add_section_rename (old_name, new_name, flags);
  4876. }
  4877. break;
  4878. case OPTION_SET_START:
  4879. set_start = parse_vma (optarg, "--set-start");
  4880. set_start_set = true;
  4881. break;
  4882. case OPTION_SREC_LEN:
  4883. _bfd_srec_len = parse_vma (optarg, "--srec-len");
  4884. break;
  4885. case OPTION_SREC_FORCES3:
  4886. _bfd_srec_forceS3 = true;
  4887. break;
  4888. case OPTION_STRIP_SYMBOLS:
  4889. add_specific_symbols (optarg, strip_specific_htab,
  4890. &strip_specific_buffer);
  4891. break;
  4892. case OPTION_STRIP_UNNEEDED_SYMBOLS:
  4893. add_specific_symbols (optarg, strip_unneeded_htab,
  4894. &strip_unneeded_buffer);
  4895. break;
  4896. case OPTION_KEEP_SYMBOLS:
  4897. add_specific_symbols (optarg, keep_specific_htab,
  4898. &keep_specific_buffer);
  4899. break;
  4900. case OPTION_KEEP_SECTION_SYMBOLS:
  4901. keep_section_symbols = true;
  4902. break;
  4903. case OPTION_LOCALIZE_HIDDEN:
  4904. localize_hidden = true;
  4905. break;
  4906. case OPTION_LOCALIZE_SYMBOLS:
  4907. add_specific_symbols (optarg, localize_specific_htab,
  4908. &localize_specific_buffer);
  4909. break;
  4910. case OPTION_LONG_SECTION_NAMES:
  4911. if (!strcmp ("enable", optarg))
  4912. long_section_names = ENABLE;
  4913. else if (!strcmp ("disable", optarg))
  4914. long_section_names = DISABLE;
  4915. else if (!strcmp ("keep", optarg))
  4916. long_section_names = KEEP;
  4917. else
  4918. fatal (_("unknown long section names option '%s'"), optarg);
  4919. break;
  4920. case OPTION_GLOBALIZE_SYMBOLS:
  4921. use_globalize = true;
  4922. add_specific_symbols (optarg, globalize_specific_htab,
  4923. &globalize_specific_buffer);
  4924. break;
  4925. case OPTION_KEEPGLOBAL_SYMBOLS:
  4926. use_keep_global = true;
  4927. add_specific_symbols (optarg, keepglobal_specific_htab,
  4928. &keepglobal_specific_buffer);
  4929. break;
  4930. case OPTION_WEAKEN_SYMBOLS:
  4931. add_specific_symbols (optarg, weaken_specific_htab,
  4932. &weaken_specific_buffer);
  4933. break;
  4934. case OPTION_ALT_MACH_CODE:
  4935. use_alt_mach_code = strtoul (optarg, NULL, 0);
  4936. if (use_alt_mach_code == 0)
  4937. fatal (_("unable to parse alternative machine code"));
  4938. break;
  4939. case OPTION_PREFIX_SYMBOLS:
  4940. prefix_symbols_string = optarg;
  4941. break;
  4942. case OPTION_PREFIX_SECTIONS:
  4943. prefix_sections_string = optarg;
  4944. break;
  4945. case OPTION_PREFIX_ALLOC_SECTIONS:
  4946. prefix_alloc_sections_string = optarg;
  4947. break;
  4948. case OPTION_READONLY_TEXT:
  4949. bfd_flags_to_set |= WP_TEXT;
  4950. bfd_flags_to_clear &= ~WP_TEXT;
  4951. break;
  4952. case OPTION_WRITABLE_TEXT:
  4953. bfd_flags_to_clear |= WP_TEXT;
  4954. bfd_flags_to_set &= ~WP_TEXT;
  4955. break;
  4956. case OPTION_PURE:
  4957. bfd_flags_to_set |= D_PAGED;
  4958. bfd_flags_to_clear &= ~D_PAGED;
  4959. break;
  4960. case OPTION_IMPURE:
  4961. bfd_flags_to_clear |= D_PAGED;
  4962. bfd_flags_to_set &= ~D_PAGED;
  4963. break;
  4964. case OPTION_EXTRACT_DWO:
  4965. strip_symbols = STRIP_NONDWO;
  4966. break;
  4967. case OPTION_EXTRACT_SYMBOL:
  4968. extract_symbol = true;
  4969. break;
  4970. case OPTION_REVERSE_BYTES:
  4971. {
  4972. int prev = reverse_bytes;
  4973. reverse_bytes = atoi (optarg);
  4974. if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0))
  4975. fatal (_("number of bytes to reverse must be positive and even"));
  4976. if (prev && prev != reverse_bytes)
  4977. non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
  4978. prev);
  4979. break;
  4980. }
  4981. case OPTION_FILE_ALIGNMENT:
  4982. pe_file_alignment = parse_vma (optarg, "--file-alignment");
  4983. break;
  4984. case OPTION_HEAP:
  4985. {
  4986. char *end;
  4987. pe_heap_reserve = strtoul (optarg, &end, 0);
  4988. if (end == optarg
  4989. || (*end != '.' && *end != '\0'))
  4990. non_fatal (_("%s: invalid reserve value for --heap"),
  4991. optarg);
  4992. else if (*end != '\0')
  4993. {
  4994. pe_heap_commit = strtoul (end + 1, &end, 0);
  4995. if (*end != '\0')
  4996. non_fatal (_("%s: invalid commit value for --heap"),
  4997. optarg);
  4998. }
  4999. }
  5000. break;
  5001. case OPTION_IMAGE_BASE:
  5002. pe_image_base = parse_vma (optarg, "--image-base");
  5003. break;
  5004. case OPTION_PE_SECTION_ALIGNMENT:
  5005. pe_section_alignment = parse_vma (optarg,
  5006. "--section-alignment");
  5007. break;
  5008. case OPTION_SUBSYSTEM:
  5009. set_pe_subsystem (optarg);
  5010. break;
  5011. case OPTION_STACK:
  5012. {
  5013. char *end;
  5014. pe_stack_reserve = strtoul (optarg, &end, 0);
  5015. if (end == optarg
  5016. || (*end != '.' && *end != '\0'))
  5017. non_fatal (_("%s: invalid reserve value for --stack"),
  5018. optarg);
  5019. else if (*end != '\0')
  5020. {
  5021. pe_stack_commit = strtoul (end + 1, &end, 0);
  5022. if (*end != '\0')
  5023. non_fatal (_("%s: invalid commit value for --stack"),
  5024. optarg);
  5025. }
  5026. }
  5027. break;
  5028. case OPTION_VERILOG_DATA_WIDTH:
  5029. VerilogDataWidth = parse_vma (optarg, "--verilog-data-width");
  5030. if (VerilogDataWidth < 1)
  5031. fatal (_("verilog data width must be at least 1 byte"));
  5032. break;
  5033. case 0:
  5034. /* We've been given a long option. */
  5035. break;
  5036. case 'H':
  5037. case 'h':
  5038. copy_usage (stdout, 0);
  5039. default:
  5040. copy_usage (stderr, 1);
  5041. }
  5042. }
  5043. if (use_globalize && use_keep_global)
  5044. fatal(_("--globalize-symbol(s) is incompatible with -G/--keep-global-symbol(s)"));
  5045. if (formats_info)
  5046. {
  5047. display_info ();
  5048. return 0;
  5049. }
  5050. if (show_version)
  5051. print_version ("objcopy");
  5052. if (interleave && copy_byte == -1)
  5053. fatal (_("interleave start byte must be set with --byte"));
  5054. if (copy_byte >= interleave)
  5055. fatal (_("byte number must be less than interleave"));
  5056. if (copy_width > interleave - copy_byte)
  5057. fatal (_("interleave width must be less than or equal to interleave - byte`"));
  5058. if (optind == argc || optind + 2 < argc)
  5059. copy_usage (stderr, 1);
  5060. input_filename = argv[optind];
  5061. if (optind + 1 < argc)
  5062. output_filename = argv[optind + 1];
  5063. default_deterministic ();
  5064. /* Default is to strip no symbols. */
  5065. if (strip_symbols == STRIP_UNDEF && discard_locals == LOCALS_UNDEF)
  5066. strip_symbols = STRIP_NONE;
  5067. if (output_target == NULL)
  5068. output_target = input_target;
  5069. /* Convert input EFI target to PEI target. */
  5070. if (input_target != NULL
  5071. && startswith (input_target, "efi-"))
  5072. {
  5073. if (convert_efi_target (&input_target) < 0)
  5074. fatal (_("unknown input EFI target: %s"), input_target);
  5075. }
  5076. /* Convert output EFI target to PEI target. */
  5077. if (output_target != NULL
  5078. && startswith (output_target, "efi-"))
  5079. {
  5080. int subsys = convert_efi_target (&output_target);
  5081. if (subsys < 0)
  5082. fatal (_("unknown output EFI target: %s"), output_target);
  5083. if (pe_subsystem == -1)
  5084. pe_subsystem = subsys;
  5085. if (pe_file_alignment == (bfd_vma) -1)
  5086. pe_file_alignment = PE_DEF_FILE_ALIGNMENT;
  5087. if (pe_section_alignment == (bfd_vma) -1)
  5088. pe_section_alignment = PE_DEF_SECTION_ALIGNMENT;
  5089. }
  5090. /* If there is no destination file, or the source and destination files
  5091. are the same, then create a temp and copy the result into the input. */
  5092. copyfd = -1;
  5093. if (output_filename == NULL
  5094. || filename_cmp (input_filename, output_filename) == 0)
  5095. {
  5096. tmpname = make_tempname (input_filename, &tmpfd);
  5097. if (tmpfd >= 0)
  5098. copyfd = dup (tmpfd);
  5099. }
  5100. else
  5101. tmpname = output_filename;
  5102. if (tmpname == NULL)
  5103. {
  5104. fatal (_("warning: could not create temporary file whilst copying '%s', (error: %s)"),
  5105. input_filename, strerror (errno));
  5106. }
  5107. copy_file (input_filename, tmpname, tmpfd, &statbuf, input_target,
  5108. output_target, input_arch);
  5109. if (status == 0)
  5110. {
  5111. const char *oname = output_filename ? output_filename : input_filename;
  5112. status = smart_rename (tmpname, oname, copyfd,
  5113. &statbuf, preserve_dates) != 0;
  5114. }
  5115. else
  5116. {
  5117. if (copyfd >= 0)
  5118. close (copyfd);
  5119. unlink_if_ordinary (tmpname);
  5120. }
  5121. if (tmpname != output_filename)
  5122. free (tmpname);
  5123. if (change_warn)
  5124. {
  5125. struct section_list *p;
  5126. for (p = change_sections; p != NULL; p = p->next)
  5127. {
  5128. if (! p->used)
  5129. {
  5130. if (p->context & (SECTION_CONTEXT_SET_VMA | SECTION_CONTEXT_ALTER_VMA))
  5131. {
  5132. char buff [20];
  5133. sprintf_vma (buff, p->vma_val);
  5134. /* xgettext:c-format */
  5135. non_fatal (_("%s %s%c0x%s never used"),
  5136. "--change-section-vma",
  5137. p->pattern,
  5138. p->context & SECTION_CONTEXT_SET_VMA ? '=' : '+',
  5139. buff);
  5140. }
  5141. if (p->context & (SECTION_CONTEXT_SET_LMA | SECTION_CONTEXT_ALTER_LMA))
  5142. {
  5143. char buff [20];
  5144. sprintf_vma (buff, p->lma_val);
  5145. /* xgettext:c-format */
  5146. non_fatal (_("%s %s%c0x%s never used"),
  5147. "--change-section-lma",
  5148. p->pattern,
  5149. p->context & SECTION_CONTEXT_SET_LMA ? '=' : '+',
  5150. buff);
  5151. }
  5152. }
  5153. }
  5154. }
  5155. free (strip_specific_buffer);
  5156. free (strip_unneeded_buffer);
  5157. free (keep_specific_buffer);
  5158. free (localize_specific_buffer);
  5159. free (globalize_specific_buffer);
  5160. free (keepglobal_specific_buffer);
  5161. free (weaken_specific_buffer);
  5162. return 0;
  5163. }
  5164. int
  5165. main (int argc, char *argv[])
  5166. {
  5167. #ifdef HAVE_LC_MESSAGES
  5168. setlocale (LC_MESSAGES, "");
  5169. #endif
  5170. setlocale (LC_CTYPE, "");
  5171. bindtextdomain (PACKAGE, LOCALEDIR);
  5172. textdomain (PACKAGE);
  5173. program_name = argv[0];
  5174. xmalloc_set_program_name (program_name);
  5175. START_PROGRESS (program_name, 0);
  5176. expandargv (&argc, &argv);
  5177. strip_symbols = STRIP_UNDEF;
  5178. discard_locals = LOCALS_UNDEF;
  5179. if (bfd_init () != BFD_INIT_MAGIC)
  5180. fatal (_("fatal error: libbfd ABI mismatch"));
  5181. set_default_bfd_target ();
  5182. if (is_strip < 0)
  5183. {
  5184. int i = strlen (program_name);
  5185. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  5186. /* Drop the .exe suffix, if any. */
  5187. if (i > 4 && FILENAME_CMP (program_name + i - 4, ".exe") == 0)
  5188. {
  5189. i -= 4;
  5190. program_name[i] = '\0';
  5191. }
  5192. #endif
  5193. is_strip = (i >= 5 && FILENAME_CMP (program_name + i - 5, "strip") == 0);
  5194. }
  5195. create_symbol_htabs ();
  5196. if (argv != NULL)
  5197. bfd_set_error_program_name (argv[0]);
  5198. if (is_strip)
  5199. strip_main (argc, argv);
  5200. else
  5201. copy_main (argc, argv);
  5202. END_PROGRESS (program_name);
  5203. return status;
  5204. }