objcopy.c 150 KB

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