peXXigen.c 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569
  1. /* Support for the generic parts of PE/PEI; the common executable parts.
  2. Copyright (C) 1995-2022 Free Software Foundation, Inc.
  3. Written by Cygnus Solutions.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /* Most of this hacked by Steve Chamberlain <sac@cygnus.com>.
  18. PE/PEI rearrangement (and code added): Donn Terry
  19. Softway Systems, Inc. */
  20. /* Hey look, some documentation [and in a place you expect to find it]!
  21. The main reference for the pei format is "Microsoft Portable Executable
  22. and Common Object File Format Specification 4.1". Get it if you need to
  23. do some serious hacking on this code.
  24. Another reference:
  25. "Peering Inside the PE: A Tour of the Win32 Portable Executable
  26. File Format", MSJ 1994, Volume 9.
  27. The PE/PEI format is also used by .NET. ECMA-335 describes this:
  28. "Standard ECMA-335 Common Language Infrastructure (CLI)", 6th Edition, June 2012.
  29. This is also available at
  30. https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdf.
  31. The *sole* difference between the pe format and the pei format is that the
  32. latter has an MSDOS 2.0 .exe header on the front that prints the message
  33. "This app must be run under Windows." (or some such).
  34. (FIXME: Whether that statement is *really* true or not is unknown.
  35. Are there more subtle differences between pe and pei formats?
  36. For now assume there aren't. If you find one, then for God sakes
  37. document it here!)
  38. The Microsoft docs use the word "image" instead of "executable" because
  39. the former can also refer to a DLL (shared library). Confusion can arise
  40. because the `i' in `pei' also refers to "image". The `pe' format can
  41. also create images (i.e. executables), it's just that to run on a win32
  42. system you need to use the pei format.
  43. FIXME: Please add more docs here so the next poor fool that has to hack
  44. on this code has a chance of getting something accomplished without
  45. wasting too much time. */
  46. /* This expands into COFF_WITH_pe, COFF_WITH_pep, COFF_WITH_pex64 or
  47. COFF_WITH_peAArch64 depending on whether we're compiling for straight
  48. PE or PE+. */
  49. #define COFF_WITH_XX
  50. #include "sysdep.h"
  51. #include "bfd.h"
  52. #include "libbfd.h"
  53. #include "coff/internal.h"
  54. #include "bfdver.h"
  55. #include "libiberty.h"
  56. #include <wchar.h>
  57. #include <wctype.h>
  58. /* NOTE: it's strange to be including an architecture specific header
  59. in what's supposed to be general (to PE/PEI) code. However, that's
  60. where the definitions are, and they don't vary per architecture
  61. within PE/PEI, so we get them from there. FIXME: The lack of
  62. variance is an assumption which may prove to be incorrect if new
  63. PE/PEI targets are created. */
  64. #if defined COFF_WITH_pex64
  65. # include "coff/x86_64.h"
  66. #elif defined COFF_WITH_pep
  67. # include "coff/ia64.h"
  68. #elif defined COFF_WITH_peAArch64
  69. # include "coff/aarch64.h"
  70. #else
  71. # include "coff/i386.h"
  72. #endif
  73. #include "coff/pe.h"
  74. #include "libcoff.h"
  75. #include "libpei.h"
  76. #include "safe-ctype.h"
  77. #if defined COFF_WITH_pep || defined COFF_WITH_pex64 || defined COFF_WITH_peAArch64
  78. # undef AOUTSZ
  79. # define AOUTSZ PEPAOUTSZ
  80. # define PEAOUTHDR PEPAOUTHDR
  81. #endif
  82. #define HighBitSet(val) ((val) & 0x80000000)
  83. #define SetHighBit(val) ((val) | 0x80000000)
  84. #define WithoutHighBit(val) ((val) & 0x7fffffff)
  85. void
  86. _bfd_XXi_swap_sym_in (bfd * abfd, void * ext1, void * in1)
  87. {
  88. SYMENT *ext = (SYMENT *) ext1;
  89. struct internal_syment *in = (struct internal_syment *) in1;
  90. if (ext->e.e_name[0] == 0)
  91. {
  92. in->_n._n_n._n_zeroes = 0;
  93. in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
  94. }
  95. else
  96. memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
  97. in->n_value = H_GET_32 (abfd, ext->e_value);
  98. in->n_scnum = (short) H_GET_16 (abfd, ext->e_scnum);
  99. if (sizeof (ext->e_type) == 2)
  100. in->n_type = H_GET_16 (abfd, ext->e_type);
  101. else
  102. in->n_type = H_GET_32 (abfd, ext->e_type);
  103. in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
  104. in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
  105. #ifndef STRICT_PE_FORMAT
  106. /* This is for Gnu-created DLLs. */
  107. /* The section symbols for the .idata$ sections have class 0x68
  108. (C_SECTION), which MS documentation indicates is a section
  109. symbol. Unfortunately, the value field in the symbol is simply a
  110. copy of the .idata section's flags rather than something useful.
  111. When these symbols are encountered, change the value to 0 so that
  112. they will be handled somewhat correctly in the bfd code. */
  113. if (in->n_sclass == C_SECTION)
  114. {
  115. char namebuf[SYMNMLEN + 1];
  116. const char *name = NULL;
  117. in->n_value = 0x0;
  118. /* Create synthetic empty sections as needed. DJ */
  119. if (in->n_scnum == 0)
  120. {
  121. asection *sec;
  122. name = _bfd_coff_internal_syment_name (abfd, in, namebuf);
  123. if (name == NULL)
  124. {
  125. _bfd_error_handler (_("%pB: unable to find name for empty section"),
  126. abfd);
  127. bfd_set_error (bfd_error_invalid_target);
  128. return;
  129. }
  130. sec = bfd_get_section_by_name (abfd, name);
  131. if (sec != NULL)
  132. in->n_scnum = sec->target_index;
  133. }
  134. if (in->n_scnum == 0)
  135. {
  136. int unused_section_number = 0;
  137. asection *sec;
  138. flagword flags;
  139. size_t name_len;
  140. char *sec_name;
  141. for (sec = abfd->sections; sec; sec = sec->next)
  142. if (unused_section_number <= sec->target_index)
  143. unused_section_number = sec->target_index + 1;
  144. name_len = strlen (name) + 1;
  145. sec_name = bfd_alloc (abfd, name_len);
  146. if (sec_name == NULL)
  147. {
  148. _bfd_error_handler (_("%pB: out of memory creating name "
  149. "for empty section"), abfd);
  150. return;
  151. }
  152. memcpy (sec_name, name, name_len);
  153. flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_DATA | SEC_LOAD;
  154. sec = bfd_make_section_anyway_with_flags (abfd, sec_name, flags);
  155. if (sec == NULL)
  156. {
  157. _bfd_error_handler (_("%pB: unable to create fake empty section"),
  158. abfd);
  159. return;
  160. }
  161. sec->vma = 0;
  162. sec->lma = 0;
  163. sec->size = 0;
  164. sec->filepos = 0;
  165. sec->rel_filepos = 0;
  166. sec->reloc_count = 0;
  167. sec->line_filepos = 0;
  168. sec->lineno_count = 0;
  169. sec->userdata = NULL;
  170. sec->next = NULL;
  171. sec->alignment_power = 2;
  172. sec->target_index = unused_section_number;
  173. in->n_scnum = unused_section_number;
  174. }
  175. in->n_sclass = C_STAT;
  176. }
  177. #endif
  178. }
  179. static bool
  180. abs_finder (bfd * abfd ATTRIBUTE_UNUSED, asection * sec, void * data)
  181. {
  182. bfd_vma abs_val = * (bfd_vma *) data;
  183. return (sec->vma <= abs_val) && ((sec->vma + (1ULL << 32)) > abs_val);
  184. }
  185. unsigned int
  186. _bfd_XXi_swap_sym_out (bfd * abfd, void * inp, void * extp)
  187. {
  188. struct internal_syment *in = (struct internal_syment *) inp;
  189. SYMENT *ext = (SYMENT *) extp;
  190. if (in->_n._n_name[0] == 0)
  191. {
  192. H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
  193. H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
  194. }
  195. else
  196. memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
  197. /* The PE32 and PE32+ formats only use 4 bytes to hold the value of a
  198. symbol. This is a problem on 64-bit targets where we can generate
  199. absolute symbols with values >= 1^32. We try to work around this
  200. problem by finding a section whose base address is sufficient to
  201. reduce the absolute value to < 1^32, and then transforming the
  202. symbol into a section relative symbol. This of course is a hack. */
  203. if (sizeof (in->n_value) > 4
  204. /* The strange computation of the shift amount is here in order to
  205. avoid a compile time warning about the comparison always being
  206. false. It does not matter if this test fails to work as expected
  207. as the worst that can happen is that some absolute symbols are
  208. needlessly converted into section relative symbols. */
  209. && in->n_value > ((1ULL << (sizeof (in->n_value) > 4 ? 32 : 31)) - 1)
  210. && in->n_scnum == N_ABS)
  211. {
  212. asection * sec;
  213. sec = bfd_sections_find_if (abfd, abs_finder, & in->n_value);
  214. if (sec)
  215. {
  216. in->n_value -= sec->vma;
  217. in->n_scnum = sec->target_index;
  218. }
  219. /* else: FIXME: The value is outside the range of any section. This
  220. happens for __image_base__ and __ImageBase and maybe some other
  221. symbols as well. We should find a way to handle these values. */
  222. }
  223. H_PUT_32 (abfd, in->n_value, ext->e_value);
  224. H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
  225. if (sizeof (ext->e_type) == 2)
  226. H_PUT_16 (abfd, in->n_type, ext->e_type);
  227. else
  228. H_PUT_32 (abfd, in->n_type, ext->e_type);
  229. H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
  230. H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
  231. return SYMESZ;
  232. }
  233. void
  234. _bfd_XXi_swap_aux_in (bfd * abfd,
  235. void * ext1,
  236. int type,
  237. int in_class,
  238. int indx ATTRIBUTE_UNUSED,
  239. int numaux ATTRIBUTE_UNUSED,
  240. void * in1)
  241. {
  242. AUXENT *ext = (AUXENT *) ext1;
  243. union internal_auxent *in = (union internal_auxent *) in1;
  244. /* PR 17521: Make sure that all fields in the aux structure
  245. are initialised. */
  246. memset (in, 0, sizeof * in);
  247. switch (in_class)
  248. {
  249. case C_FILE:
  250. if (ext->x_file.x_fname[0] == 0)
  251. {
  252. in->x_file.x_n.x_n.x_zeroes = 0;
  253. in->x_file.x_n.x_n.x_offset = H_GET_32 (abfd, ext->x_file.x_n.x_offset);
  254. }
  255. else
  256. memcpy (in->x_file.x_n.x_fname, ext->x_file.x_fname, FILNMLEN);
  257. return;
  258. case C_STAT:
  259. case C_LEAFSTAT:
  260. case C_HIDDEN:
  261. if (type == T_NULL)
  262. {
  263. in->x_scn.x_scnlen = GET_SCN_SCNLEN (abfd, ext);
  264. in->x_scn.x_nreloc = GET_SCN_NRELOC (abfd, ext);
  265. in->x_scn.x_nlinno = GET_SCN_NLINNO (abfd, ext);
  266. in->x_scn.x_checksum = H_GET_32 (abfd, ext->x_scn.x_checksum);
  267. in->x_scn.x_associated = H_GET_16 (abfd, ext->x_scn.x_associated);
  268. in->x_scn.x_comdat = H_GET_8 (abfd, ext->x_scn.x_comdat);
  269. return;
  270. }
  271. break;
  272. }
  273. in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->x_sym.x_tagndx);
  274. in->x_sym.x_tvndx = H_GET_16 (abfd, ext->x_sym.x_tvndx);
  275. if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
  276. || ISTAG (in_class))
  277. {
  278. in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR (abfd, ext);
  279. in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX (abfd, ext);
  280. }
  281. else
  282. {
  283. in->x_sym.x_fcnary.x_ary.x_dimen[0] =
  284. H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
  285. in->x_sym.x_fcnary.x_ary.x_dimen[1] =
  286. H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
  287. in->x_sym.x_fcnary.x_ary.x_dimen[2] =
  288. H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
  289. in->x_sym.x_fcnary.x_ary.x_dimen[3] =
  290. H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
  291. }
  292. if (ISFCN (type))
  293. {
  294. in->x_sym.x_misc.x_fsize = H_GET_32 (abfd, ext->x_sym.x_misc.x_fsize);
  295. }
  296. else
  297. {
  298. in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO (abfd, ext);
  299. in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE (abfd, ext);
  300. }
  301. }
  302. unsigned int
  303. _bfd_XXi_swap_aux_out (bfd * abfd,
  304. void * inp,
  305. int type,
  306. int in_class,
  307. int indx ATTRIBUTE_UNUSED,
  308. int numaux ATTRIBUTE_UNUSED,
  309. void * extp)
  310. {
  311. union internal_auxent *in = (union internal_auxent *) inp;
  312. AUXENT *ext = (AUXENT *) extp;
  313. memset (ext, 0, AUXESZ);
  314. switch (in_class)
  315. {
  316. case C_FILE:
  317. if (in->x_file.x_n.x_fname[0] == 0)
  318. {
  319. H_PUT_32 (abfd, 0, ext->x_file.x_n.x_zeroes);
  320. H_PUT_32 (abfd, in->x_file.x_n.x_n.x_offset, ext->x_file.x_n.x_offset);
  321. }
  322. else
  323. memcpy (ext->x_file.x_fname, in->x_file.x_n.x_fname, sizeof (ext->x_file.x_fname));
  324. return AUXESZ;
  325. case C_STAT:
  326. case C_LEAFSTAT:
  327. case C_HIDDEN:
  328. if (type == T_NULL)
  329. {
  330. PUT_SCN_SCNLEN (abfd, in->x_scn.x_scnlen, ext);
  331. PUT_SCN_NRELOC (abfd, in->x_scn.x_nreloc, ext);
  332. PUT_SCN_NLINNO (abfd, in->x_scn.x_nlinno, ext);
  333. H_PUT_32 (abfd, in->x_scn.x_checksum, ext->x_scn.x_checksum);
  334. H_PUT_16 (abfd, in->x_scn.x_associated, ext->x_scn.x_associated);
  335. H_PUT_8 (abfd, in->x_scn.x_comdat, ext->x_scn.x_comdat);
  336. return AUXESZ;
  337. }
  338. break;
  339. }
  340. H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->x_sym.x_tagndx);
  341. H_PUT_16 (abfd, in->x_sym.x_tvndx, ext->x_sym.x_tvndx);
  342. if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
  343. || ISTAG (in_class))
  344. {
  345. PUT_FCN_LNNOPTR (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr, ext);
  346. PUT_FCN_ENDNDX (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
  347. }
  348. else
  349. {
  350. H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
  351. ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
  352. H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
  353. ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
  354. H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
  355. ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
  356. H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
  357. ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
  358. }
  359. if (ISFCN (type))
  360. H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_sym.x_misc.x_fsize);
  361. else
  362. {
  363. PUT_LNSZ_LNNO (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext);
  364. PUT_LNSZ_SIZE (abfd, in->x_sym.x_misc.x_lnsz.x_size, ext);
  365. }
  366. return AUXESZ;
  367. }
  368. void
  369. _bfd_XXi_swap_lineno_in (bfd * abfd, void * ext1, void * in1)
  370. {
  371. LINENO *ext = (LINENO *) ext1;
  372. struct internal_lineno *in = (struct internal_lineno *) in1;
  373. in->l_addr.l_symndx = H_GET_32 (abfd, ext->l_addr.l_symndx);
  374. in->l_lnno = GET_LINENO_LNNO (abfd, ext);
  375. }
  376. unsigned int
  377. _bfd_XXi_swap_lineno_out (bfd * abfd, void * inp, void * outp)
  378. {
  379. struct internal_lineno *in = (struct internal_lineno *) inp;
  380. struct external_lineno *ext = (struct external_lineno *) outp;
  381. H_PUT_32 (abfd, in->l_addr.l_symndx, ext->l_addr.l_symndx);
  382. PUT_LINENO_LNNO (abfd, in->l_lnno, ext);
  383. return LINESZ;
  384. }
  385. void
  386. _bfd_XXi_swap_aouthdr_in (bfd * abfd,
  387. void * aouthdr_ext1,
  388. void * aouthdr_int1)
  389. {
  390. PEAOUTHDR * src = (PEAOUTHDR *) aouthdr_ext1;
  391. AOUTHDR * aouthdr_ext = (AOUTHDR *) aouthdr_ext1;
  392. struct internal_aouthdr *aouthdr_int
  393. = (struct internal_aouthdr *) aouthdr_int1;
  394. struct internal_extra_pe_aouthdr *a = &aouthdr_int->pe;
  395. aouthdr_int->magic = H_GET_16 (abfd, aouthdr_ext->magic);
  396. aouthdr_int->vstamp = H_GET_16 (abfd, aouthdr_ext->vstamp);
  397. aouthdr_int->tsize = GET_AOUTHDR_TSIZE (abfd, aouthdr_ext->tsize);
  398. aouthdr_int->dsize = GET_AOUTHDR_DSIZE (abfd, aouthdr_ext->dsize);
  399. aouthdr_int->bsize = GET_AOUTHDR_BSIZE (abfd, aouthdr_ext->bsize);
  400. aouthdr_int->entry = GET_AOUTHDR_ENTRY (abfd, aouthdr_ext->entry);
  401. aouthdr_int->text_start =
  402. GET_AOUTHDR_TEXT_START (abfd, aouthdr_ext->text_start);
  403. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
  404. /* PE32+ does not have data_start member! */
  405. aouthdr_int->data_start =
  406. GET_AOUTHDR_DATA_START (abfd, aouthdr_ext->data_start);
  407. a->BaseOfData = aouthdr_int->data_start;
  408. #endif
  409. a->Magic = aouthdr_int->magic;
  410. a->MajorLinkerVersion = H_GET_8 (abfd, aouthdr_ext->vstamp);
  411. a->MinorLinkerVersion = H_GET_8 (abfd, aouthdr_ext->vstamp + 1);
  412. a->SizeOfCode = aouthdr_int->tsize ;
  413. a->SizeOfInitializedData = aouthdr_int->dsize ;
  414. a->SizeOfUninitializedData = aouthdr_int->bsize ;
  415. a->AddressOfEntryPoint = aouthdr_int->entry;
  416. a->BaseOfCode = aouthdr_int->text_start;
  417. a->ImageBase = GET_OPTHDR_IMAGE_BASE (abfd, src->ImageBase);
  418. a->SectionAlignment = H_GET_32 (abfd, src->SectionAlignment);
  419. a->FileAlignment = H_GET_32 (abfd, src->FileAlignment);
  420. a->MajorOperatingSystemVersion =
  421. H_GET_16 (abfd, src->MajorOperatingSystemVersion);
  422. a->MinorOperatingSystemVersion =
  423. H_GET_16 (abfd, src->MinorOperatingSystemVersion);
  424. a->MajorImageVersion = H_GET_16 (abfd, src->MajorImageVersion);
  425. a->MinorImageVersion = H_GET_16 (abfd, src->MinorImageVersion);
  426. a->MajorSubsystemVersion = H_GET_16 (abfd, src->MajorSubsystemVersion);
  427. a->MinorSubsystemVersion = H_GET_16 (abfd, src->MinorSubsystemVersion);
  428. a->Reserved1 = H_GET_32 (abfd, src->Reserved1);
  429. a->SizeOfImage = H_GET_32 (abfd, src->SizeOfImage);
  430. a->SizeOfHeaders = H_GET_32 (abfd, src->SizeOfHeaders);
  431. a->CheckSum = H_GET_32 (abfd, src->CheckSum);
  432. a->Subsystem = H_GET_16 (abfd, src->Subsystem);
  433. a->DllCharacteristics = H_GET_16 (abfd, src->DllCharacteristics);
  434. a->SizeOfStackReserve =
  435. GET_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, src->SizeOfStackReserve);
  436. a->SizeOfStackCommit =
  437. GET_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, src->SizeOfStackCommit);
  438. a->SizeOfHeapReserve =
  439. GET_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, src->SizeOfHeapReserve);
  440. a->SizeOfHeapCommit =
  441. GET_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, src->SizeOfHeapCommit);
  442. a->LoaderFlags = H_GET_32 (abfd, src->LoaderFlags);
  443. a->NumberOfRvaAndSizes = H_GET_32 (abfd, src->NumberOfRvaAndSizes);
  444. {
  445. unsigned idx;
  446. /* PR 17512: Corrupt PE binaries can cause seg-faults. */
  447. if (a->NumberOfRvaAndSizes > IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
  448. {
  449. /* xgettext:c-format */
  450. _bfd_error_handler
  451. (_("%pB: aout header specifies an invalid number of"
  452. " data-directory entries: %u"), abfd, a->NumberOfRvaAndSizes);
  453. bfd_set_error (bfd_error_bad_value);
  454. /* Paranoia: If the number is corrupt, then assume that the
  455. actual entries themselves might be corrupt as well. */
  456. a->NumberOfRvaAndSizes = 0;
  457. }
  458. for (idx = 0; idx < a->NumberOfRvaAndSizes; idx++)
  459. {
  460. /* If data directory is empty, rva also should be 0. */
  461. int size =
  462. H_GET_32 (abfd, src->DataDirectory[idx][1]);
  463. a->DataDirectory[idx].Size = size;
  464. if (size)
  465. a->DataDirectory[idx].VirtualAddress =
  466. H_GET_32 (abfd, src->DataDirectory[idx][0]);
  467. else
  468. a->DataDirectory[idx].VirtualAddress = 0;
  469. }
  470. while (idx < IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
  471. {
  472. a->DataDirectory[idx].Size = 0;
  473. a->DataDirectory[idx].VirtualAddress = 0;
  474. idx ++;
  475. }
  476. }
  477. if (aouthdr_int->entry)
  478. {
  479. aouthdr_int->entry += a->ImageBase;
  480. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
  481. aouthdr_int->entry &= 0xffffffff;
  482. #endif
  483. }
  484. if (aouthdr_int->tsize)
  485. {
  486. aouthdr_int->text_start += a->ImageBase;
  487. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
  488. aouthdr_int->text_start &= 0xffffffff;
  489. #endif
  490. }
  491. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
  492. /* PE32+ does not have data_start member! */
  493. if (aouthdr_int->dsize)
  494. {
  495. aouthdr_int->data_start += a->ImageBase;
  496. aouthdr_int->data_start &= 0xffffffff;
  497. }
  498. #endif
  499. }
  500. /* A support function for below. */
  501. static void
  502. add_data_entry (bfd * abfd,
  503. struct internal_extra_pe_aouthdr *aout,
  504. int idx,
  505. char *name,
  506. bfd_vma base)
  507. {
  508. asection *sec = bfd_get_section_by_name (abfd, name);
  509. /* Add import directory information if it exists. */
  510. if ((sec != NULL)
  511. && (coff_section_data (abfd, sec) != NULL)
  512. && (pei_section_data (abfd, sec) != NULL))
  513. {
  514. /* If data directory is empty, rva also should be 0. */
  515. int size = pei_section_data (abfd, sec)->virt_size;
  516. aout->DataDirectory[idx].Size = size;
  517. if (size)
  518. {
  519. aout->DataDirectory[idx].VirtualAddress =
  520. (sec->vma - base) & 0xffffffff;
  521. sec->flags |= SEC_DATA;
  522. }
  523. }
  524. }
  525. unsigned int
  526. _bfd_XXi_swap_aouthdr_out (bfd * abfd, void * in, void * out)
  527. {
  528. struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *) in;
  529. pe_data_type *pe = pe_data (abfd);
  530. struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
  531. PEAOUTHDR *aouthdr_out = (PEAOUTHDR *) out;
  532. bfd_vma sa, fa, ib;
  533. IMAGE_DATA_DIRECTORY idata2, idata5, tls;
  534. sa = extra->SectionAlignment;
  535. fa = extra->FileAlignment;
  536. ib = extra->ImageBase;
  537. idata2 = pe->pe_opthdr.DataDirectory[PE_IMPORT_TABLE];
  538. idata5 = pe->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE];
  539. tls = pe->pe_opthdr.DataDirectory[PE_TLS_TABLE];
  540. if (aouthdr_in->tsize)
  541. {
  542. aouthdr_in->text_start -= ib;
  543. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
  544. aouthdr_in->text_start &= 0xffffffff;
  545. #endif
  546. }
  547. if (aouthdr_in->dsize)
  548. {
  549. aouthdr_in->data_start -= ib;
  550. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
  551. aouthdr_in->data_start &= 0xffffffff;
  552. #endif
  553. }
  554. if (aouthdr_in->entry)
  555. {
  556. aouthdr_in->entry -= ib;
  557. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
  558. aouthdr_in->entry &= 0xffffffff;
  559. #endif
  560. }
  561. #define FA(x) (((x) + fa -1 ) & (- fa))
  562. #define SA(x) (((x) + sa -1 ) & (- sa))
  563. /* We like to have the sizes aligned. */
  564. aouthdr_in->bsize = FA (aouthdr_in->bsize);
  565. extra->NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
  566. add_data_entry (abfd, extra, PE_EXPORT_TABLE, ".edata", ib);
  567. add_data_entry (abfd, extra, PE_RESOURCE_TABLE, ".rsrc", ib);
  568. add_data_entry (abfd, extra, PE_EXCEPTION_TABLE, ".pdata", ib);
  569. /* In theory we do not need to call add_data_entry for .idata$2 or
  570. .idata$5. It will be done in bfd_coff_final_link where all the
  571. required information is available. If however, we are not going
  572. to perform a final link, eg because we have been invoked by objcopy
  573. or strip, then we need to make sure that these Data Directory
  574. entries are initialised properly.
  575. So - we copy the input values into the output values, and then, if
  576. a final link is going to be performed, it can overwrite them. */
  577. extra->DataDirectory[PE_IMPORT_TABLE] = idata2;
  578. extra->DataDirectory[PE_IMPORT_ADDRESS_TABLE] = idata5;
  579. extra->DataDirectory[PE_TLS_TABLE] = tls;
  580. if (extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress == 0)
  581. /* Until other .idata fixes are made (pending patch), the entry for
  582. .idata is needed for backwards compatibility. FIXME. */
  583. add_data_entry (abfd, extra, PE_IMPORT_TABLE, ".idata", ib);
  584. /* For some reason, the virtual size (which is what's set by
  585. add_data_entry) for .reloc is not the same as the size recorded
  586. in this slot by MSVC; it doesn't seem to cause problems (so far),
  587. but since it's the best we've got, use it. It does do the right
  588. thing for .pdata. */
  589. if (pe->has_reloc_section)
  590. add_data_entry (abfd, extra, PE_BASE_RELOCATION_TABLE, ".reloc", ib);
  591. {
  592. asection *sec;
  593. bfd_vma hsize = 0;
  594. bfd_vma dsize = 0;
  595. bfd_vma isize = 0;
  596. bfd_vma tsize = 0;
  597. for (sec = abfd->sections; sec; sec = sec->next)
  598. {
  599. int rounded = FA (sec->size);
  600. if (rounded == 0)
  601. continue;
  602. /* The first non-zero section filepos is the header size.
  603. Sections without contents will have a filepos of 0. */
  604. if (hsize == 0)
  605. hsize = sec->filepos;
  606. if (sec->flags & SEC_DATA)
  607. dsize += rounded;
  608. if (sec->flags & SEC_CODE)
  609. tsize += rounded;
  610. /* The image size is the total VIRTUAL size (which is what is
  611. in the virt_size field). Files have been seen (from MSVC
  612. 5.0 link.exe) where the file size of the .data segment is
  613. quite small compared to the virtual size. Without this
  614. fix, strip munges the file.
  615. FIXME: We need to handle holes between sections, which may
  616. happpen when we covert from another format. We just use
  617. the virtual address and virtual size of the last section
  618. for the image size. */
  619. if (coff_section_data (abfd, sec) != NULL
  620. && pei_section_data (abfd, sec) != NULL)
  621. isize = (sec->vma - extra->ImageBase
  622. + SA (FA (pei_section_data (abfd, sec)->virt_size)));
  623. }
  624. aouthdr_in->dsize = dsize;
  625. aouthdr_in->tsize = tsize;
  626. extra->SizeOfHeaders = hsize;
  627. extra->SizeOfImage = isize;
  628. }
  629. H_PUT_16 (abfd, aouthdr_in->magic, aouthdr_out->standard.magic);
  630. /* e.g. 219510000 is linker version 2.19 */
  631. #define LINKER_VERSION ((short) (BFD_VERSION / 1000000))
  632. /* This piece of magic sets the "linker version" field to
  633. LINKER_VERSION. */
  634. H_PUT_16 (abfd, (LINKER_VERSION / 100 + (LINKER_VERSION % 100) * 256),
  635. aouthdr_out->standard.vstamp);
  636. PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, aouthdr_out->standard.tsize);
  637. PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, aouthdr_out->standard.dsize);
  638. PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, aouthdr_out->standard.bsize);
  639. PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, aouthdr_out->standard.entry);
  640. PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
  641. aouthdr_out->standard.text_start);
  642. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
  643. /* PE32+ does not have data_start member! */
  644. PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
  645. aouthdr_out->standard.data_start);
  646. #endif
  647. PUT_OPTHDR_IMAGE_BASE (abfd, extra->ImageBase, aouthdr_out->ImageBase);
  648. H_PUT_32 (abfd, extra->SectionAlignment, aouthdr_out->SectionAlignment);
  649. H_PUT_32 (abfd, extra->FileAlignment, aouthdr_out->FileAlignment);
  650. H_PUT_16 (abfd, extra->MajorOperatingSystemVersion,
  651. aouthdr_out->MajorOperatingSystemVersion);
  652. H_PUT_16 (abfd, extra->MinorOperatingSystemVersion,
  653. aouthdr_out->MinorOperatingSystemVersion);
  654. H_PUT_16 (abfd, extra->MajorImageVersion, aouthdr_out->MajorImageVersion);
  655. H_PUT_16 (abfd, extra->MinorImageVersion, aouthdr_out->MinorImageVersion);
  656. H_PUT_16 (abfd, extra->MajorSubsystemVersion,
  657. aouthdr_out->MajorSubsystemVersion);
  658. H_PUT_16 (abfd, extra->MinorSubsystemVersion,
  659. aouthdr_out->MinorSubsystemVersion);
  660. H_PUT_32 (abfd, extra->Reserved1, aouthdr_out->Reserved1);
  661. H_PUT_32 (abfd, extra->SizeOfImage, aouthdr_out->SizeOfImage);
  662. H_PUT_32 (abfd, extra->SizeOfHeaders, aouthdr_out->SizeOfHeaders);
  663. H_PUT_32 (abfd, extra->CheckSum, aouthdr_out->CheckSum);
  664. H_PUT_16 (abfd, extra->Subsystem, aouthdr_out->Subsystem);
  665. H_PUT_16 (abfd, extra->DllCharacteristics, aouthdr_out->DllCharacteristics);
  666. PUT_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, extra->SizeOfStackReserve,
  667. aouthdr_out->SizeOfStackReserve);
  668. PUT_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, extra->SizeOfStackCommit,
  669. aouthdr_out->SizeOfStackCommit);
  670. PUT_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, extra->SizeOfHeapReserve,
  671. aouthdr_out->SizeOfHeapReserve);
  672. PUT_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, extra->SizeOfHeapCommit,
  673. aouthdr_out->SizeOfHeapCommit);
  674. H_PUT_32 (abfd, extra->LoaderFlags, aouthdr_out->LoaderFlags);
  675. H_PUT_32 (abfd, extra->NumberOfRvaAndSizes,
  676. aouthdr_out->NumberOfRvaAndSizes);
  677. {
  678. int idx;
  679. for (idx = 0; idx < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; idx++)
  680. {
  681. H_PUT_32 (abfd, extra->DataDirectory[idx].VirtualAddress,
  682. aouthdr_out->DataDirectory[idx][0]);
  683. H_PUT_32 (abfd, extra->DataDirectory[idx].Size,
  684. aouthdr_out->DataDirectory[idx][1]);
  685. }
  686. }
  687. return AOUTSZ;
  688. }
  689. unsigned int
  690. _bfd_XXi_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
  691. {
  692. int idx;
  693. struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
  694. struct external_PEI_filehdr *filehdr_out = (struct external_PEI_filehdr *) out;
  695. if (pe_data (abfd)->has_reloc_section
  696. || pe_data (abfd)->dont_strip_reloc)
  697. filehdr_in->f_flags &= ~F_RELFLG;
  698. if (pe_data (abfd)->dll)
  699. filehdr_in->f_flags |= F_DLL;
  700. filehdr_in->pe.e_magic = IMAGE_DOS_SIGNATURE;
  701. filehdr_in->pe.e_cblp = 0x90;
  702. filehdr_in->pe.e_cp = 0x3;
  703. filehdr_in->pe.e_crlc = 0x0;
  704. filehdr_in->pe.e_cparhdr = 0x4;
  705. filehdr_in->pe.e_minalloc = 0x0;
  706. filehdr_in->pe.e_maxalloc = 0xffff;
  707. filehdr_in->pe.e_ss = 0x0;
  708. filehdr_in->pe.e_sp = 0xb8;
  709. filehdr_in->pe.e_csum = 0x0;
  710. filehdr_in->pe.e_ip = 0x0;
  711. filehdr_in->pe.e_cs = 0x0;
  712. filehdr_in->pe.e_lfarlc = 0x40;
  713. filehdr_in->pe.e_ovno = 0x0;
  714. for (idx = 0; idx < 4; idx++)
  715. filehdr_in->pe.e_res[idx] = 0x0;
  716. filehdr_in->pe.e_oemid = 0x0;
  717. filehdr_in->pe.e_oeminfo = 0x0;
  718. for (idx = 0; idx < 10; idx++)
  719. filehdr_in->pe.e_res2[idx] = 0x0;
  720. filehdr_in->pe.e_lfanew = 0x80;
  721. /* This next collection of data are mostly just characters. It
  722. appears to be constant within the headers put on NT exes. */
  723. memcpy (filehdr_in->pe.dos_message, pe_data (abfd)->dos_message,
  724. sizeof (filehdr_in->pe.dos_message));
  725. filehdr_in->pe.nt_signature = IMAGE_NT_SIGNATURE;
  726. H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
  727. H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
  728. /* Use a real timestamp by default, unless the no-insert-timestamp
  729. option was chosen. */
  730. if ((pe_data (abfd)->timestamp) == -1)
  731. H_PUT_32 (abfd, time (0), filehdr_out->f_timdat);
  732. else
  733. H_PUT_32 (abfd, pe_data (abfd)->timestamp, filehdr_out->f_timdat);
  734. PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr,
  735. filehdr_out->f_symptr);
  736. H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
  737. H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
  738. H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
  739. /* Put in extra dos header stuff. This data remains essentially
  740. constant, it just has to be tacked on to the beginning of all exes
  741. for NT. */
  742. H_PUT_16 (abfd, filehdr_in->pe.e_magic, filehdr_out->e_magic);
  743. H_PUT_16 (abfd, filehdr_in->pe.e_cblp, filehdr_out->e_cblp);
  744. H_PUT_16 (abfd, filehdr_in->pe.e_cp, filehdr_out->e_cp);
  745. H_PUT_16 (abfd, filehdr_in->pe.e_crlc, filehdr_out->e_crlc);
  746. H_PUT_16 (abfd, filehdr_in->pe.e_cparhdr, filehdr_out->e_cparhdr);
  747. H_PUT_16 (abfd, filehdr_in->pe.e_minalloc, filehdr_out->e_minalloc);
  748. H_PUT_16 (abfd, filehdr_in->pe.e_maxalloc, filehdr_out->e_maxalloc);
  749. H_PUT_16 (abfd, filehdr_in->pe.e_ss, filehdr_out->e_ss);
  750. H_PUT_16 (abfd, filehdr_in->pe.e_sp, filehdr_out->e_sp);
  751. H_PUT_16 (abfd, filehdr_in->pe.e_csum, filehdr_out->e_csum);
  752. H_PUT_16 (abfd, filehdr_in->pe.e_ip, filehdr_out->e_ip);
  753. H_PUT_16 (abfd, filehdr_in->pe.e_cs, filehdr_out->e_cs);
  754. H_PUT_16 (abfd, filehdr_in->pe.e_lfarlc, filehdr_out->e_lfarlc);
  755. H_PUT_16 (abfd, filehdr_in->pe.e_ovno, filehdr_out->e_ovno);
  756. for (idx = 0; idx < 4; idx++)
  757. H_PUT_16 (abfd, filehdr_in->pe.e_res[idx], filehdr_out->e_res[idx]);
  758. H_PUT_16 (abfd, filehdr_in->pe.e_oemid, filehdr_out->e_oemid);
  759. H_PUT_16 (abfd, filehdr_in->pe.e_oeminfo, filehdr_out->e_oeminfo);
  760. for (idx = 0; idx < 10; idx++)
  761. H_PUT_16 (abfd, filehdr_in->pe.e_res2[idx], filehdr_out->e_res2[idx]);
  762. H_PUT_32 (abfd, filehdr_in->pe.e_lfanew, filehdr_out->e_lfanew);
  763. for (idx = 0; idx < 16; idx++)
  764. H_PUT_32 (abfd, filehdr_in->pe.dos_message[idx],
  765. filehdr_out->dos_message[idx]);
  766. /* Also put in the NT signature. */
  767. H_PUT_32 (abfd, filehdr_in->pe.nt_signature, filehdr_out->nt_signature);
  768. return FILHSZ;
  769. }
  770. unsigned int
  771. _bfd_XX_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
  772. {
  773. struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
  774. FILHDR *filehdr_out = (FILHDR *) out;
  775. H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
  776. H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
  777. H_PUT_32 (abfd, filehdr_in->f_timdat, filehdr_out->f_timdat);
  778. PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr, filehdr_out->f_symptr);
  779. H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
  780. H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
  781. H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
  782. return FILHSZ;
  783. }
  784. unsigned int
  785. _bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out)
  786. {
  787. struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
  788. SCNHDR *scnhdr_ext = (SCNHDR *) out;
  789. unsigned int ret = SCNHSZ;
  790. bfd_vma ps;
  791. bfd_vma ss;
  792. memcpy (scnhdr_ext->s_name, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
  793. ss = scnhdr_int->s_vaddr - pe_data (abfd)->pe_opthdr.ImageBase;
  794. if (scnhdr_int->s_vaddr < pe_data (abfd)->pe_opthdr.ImageBase)
  795. _bfd_error_handler (_("%pB:%.8s: section below image base"),
  796. abfd, scnhdr_int->s_name);
  797. else if(ss != (ss & 0xffffffff))
  798. _bfd_error_handler (_("%pB:%.8s: RVA truncated"), abfd, scnhdr_int->s_name);
  799. PUT_SCNHDR_VADDR (abfd, ss & 0xffffffff, scnhdr_ext->s_vaddr);
  800. /* NT wants the size data to be rounded up to the next
  801. NT_FILE_ALIGNMENT, but zero if it has no content (as in .bss,
  802. sometimes). */
  803. if ((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0)
  804. {
  805. if (bfd_pei_p (abfd))
  806. {
  807. ps = scnhdr_int->s_size;
  808. ss = 0;
  809. }
  810. else
  811. {
  812. ps = 0;
  813. ss = scnhdr_int->s_size;
  814. }
  815. }
  816. else
  817. {
  818. if (bfd_pei_p (abfd))
  819. ps = scnhdr_int->s_paddr;
  820. else
  821. ps = 0;
  822. ss = scnhdr_int->s_size;
  823. }
  824. PUT_SCNHDR_SIZE (abfd, ss,
  825. scnhdr_ext->s_size);
  826. /* s_paddr in PE is really the virtual size. */
  827. PUT_SCNHDR_PADDR (abfd, ps, scnhdr_ext->s_paddr);
  828. PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr,
  829. scnhdr_ext->s_scnptr);
  830. PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr,
  831. scnhdr_ext->s_relptr);
  832. PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr,
  833. scnhdr_ext->s_lnnoptr);
  834. {
  835. /* Extra flags must be set when dealing with PE. All sections should also
  836. have the IMAGE_SCN_MEM_READ (0x40000000) flag set. In addition, the
  837. .text section must have IMAGE_SCN_MEM_EXECUTE (0x20000000) and the data
  838. sections (.idata, .data, .bss, .CRT) must have IMAGE_SCN_MEM_WRITE set
  839. (this is especially important when dealing with the .idata section since
  840. the addresses for routines from .dlls must be overwritten). If .reloc
  841. section data is ever generated, we must add IMAGE_SCN_MEM_DISCARDABLE
  842. (0x02000000). Also, the resource data should also be read and
  843. writable. */
  844. /* FIXME: Alignment is also encoded in this field, at least on
  845. ARM-WINCE. Although - how do we get the original alignment field
  846. back ? */
  847. typedef struct
  848. {
  849. char section_name[SCNNMLEN];
  850. unsigned long must_have;
  851. }
  852. pe_required_section_flags;
  853. pe_required_section_flags known_sections [] =
  854. {
  855. { ".arch", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_ALIGN_8BYTES },
  856. { ".bss", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
  857. { ".data", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
  858. { ".edata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
  859. { ".idata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
  860. { ".pdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
  861. { ".rdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
  862. { ".reloc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE },
  863. { ".rsrc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
  864. { ".text" , IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE },
  865. { ".tls", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
  866. { ".xdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
  867. };
  868. pe_required_section_flags * p;
  869. /* We have defaulted to adding the IMAGE_SCN_MEM_WRITE flag, but now
  870. we know exactly what this specific section wants so we remove it
  871. and then allow the must_have field to add it back in if necessary.
  872. However, we don't remove IMAGE_SCN_MEM_WRITE flag from .text if the
  873. default WP_TEXT file flag has been cleared. WP_TEXT may be cleared
  874. by ld --enable-auto-import (if auto-import is actually needed),
  875. by ld --omagic, or by obcopy --writable-text. */
  876. for (p = known_sections;
  877. p < known_sections + ARRAY_SIZE (known_sections);
  878. p++)
  879. if (memcmp (scnhdr_int->s_name, p->section_name, SCNNMLEN) == 0)
  880. {
  881. if (memcmp (scnhdr_int->s_name, ".text", sizeof ".text")
  882. || (bfd_get_file_flags (abfd) & WP_TEXT))
  883. scnhdr_int->s_flags &= ~IMAGE_SCN_MEM_WRITE;
  884. scnhdr_int->s_flags |= p->must_have;
  885. break;
  886. }
  887. H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
  888. }
  889. if (coff_data (abfd)->link_info
  890. && ! bfd_link_relocatable (coff_data (abfd)->link_info)
  891. && ! bfd_link_pic (coff_data (abfd)->link_info)
  892. && memcmp (scnhdr_int->s_name, ".text", sizeof ".text") == 0)
  893. {
  894. /* By inference from looking at MS output, the 32 bit field
  895. which is the combination of the number_of_relocs and
  896. number_of_linenos is used for the line number count in
  897. executables. A 16-bit field won't do for cc1. The MS
  898. document says that the number of relocs is zero for
  899. executables, but the 17-th bit has been observed to be there.
  900. Overflow is not an issue: a 4G-line program will overflow a
  901. bunch of other fields long before this! */
  902. H_PUT_16 (abfd, (scnhdr_int->s_nlnno & 0xffff), scnhdr_ext->s_nlnno);
  903. H_PUT_16 (abfd, (scnhdr_int->s_nlnno >> 16), scnhdr_ext->s_nreloc);
  904. }
  905. else
  906. {
  907. if (scnhdr_int->s_nlnno <= 0xffff)
  908. H_PUT_16 (abfd, scnhdr_int->s_nlnno, scnhdr_ext->s_nlnno);
  909. else
  910. {
  911. /* xgettext:c-format */
  912. _bfd_error_handler (_("%pB: line number overflow: 0x%lx > 0xffff"),
  913. abfd, scnhdr_int->s_nlnno);
  914. bfd_set_error (bfd_error_file_truncated);
  915. H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nlnno);
  916. ret = 0;
  917. }
  918. /* Although we could encode 0xffff relocs here, we do not, to be
  919. consistent with other parts of bfd. Also it lets us warn, as
  920. we should never see 0xffff here w/o having the overflow flag
  921. set. */
  922. if (scnhdr_int->s_nreloc < 0xffff)
  923. H_PUT_16 (abfd, scnhdr_int->s_nreloc, scnhdr_ext->s_nreloc);
  924. else
  925. {
  926. /* PE can deal with large #s of relocs, but not here. */
  927. H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nreloc);
  928. scnhdr_int->s_flags |= IMAGE_SCN_LNK_NRELOC_OVFL;
  929. H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
  930. }
  931. }
  932. return ret;
  933. }
  934. void
  935. _bfd_XXi_swap_debugdir_in (bfd * abfd, void * ext1, void * in1)
  936. {
  937. struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *) ext1;
  938. struct internal_IMAGE_DEBUG_DIRECTORY *in = (struct internal_IMAGE_DEBUG_DIRECTORY *) in1;
  939. in->Characteristics = H_GET_32(abfd, ext->Characteristics);
  940. in->TimeDateStamp = H_GET_32(abfd, ext->TimeDateStamp);
  941. in->MajorVersion = H_GET_16(abfd, ext->MajorVersion);
  942. in->MinorVersion = H_GET_16(abfd, ext->MinorVersion);
  943. in->Type = H_GET_32(abfd, ext->Type);
  944. in->SizeOfData = H_GET_32(abfd, ext->SizeOfData);
  945. in->AddressOfRawData = H_GET_32(abfd, ext->AddressOfRawData);
  946. in->PointerToRawData = H_GET_32(abfd, ext->PointerToRawData);
  947. }
  948. unsigned int
  949. _bfd_XXi_swap_debugdir_out (bfd * abfd, void * inp, void * extp)
  950. {
  951. struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *) extp;
  952. struct internal_IMAGE_DEBUG_DIRECTORY *in = (struct internal_IMAGE_DEBUG_DIRECTORY *) inp;
  953. H_PUT_32(abfd, in->Characteristics, ext->Characteristics);
  954. H_PUT_32(abfd, in->TimeDateStamp, ext->TimeDateStamp);
  955. H_PUT_16(abfd, in->MajorVersion, ext->MajorVersion);
  956. H_PUT_16(abfd, in->MinorVersion, ext->MinorVersion);
  957. H_PUT_32(abfd, in->Type, ext->Type);
  958. H_PUT_32(abfd, in->SizeOfData, ext->SizeOfData);
  959. H_PUT_32(abfd, in->AddressOfRawData, ext->AddressOfRawData);
  960. H_PUT_32(abfd, in->PointerToRawData, ext->PointerToRawData);
  961. return sizeof (struct external_IMAGE_DEBUG_DIRECTORY);
  962. }
  963. CODEVIEW_INFO *
  964. _bfd_XXi_slurp_codeview_record (bfd * abfd, file_ptr where, unsigned long length, CODEVIEW_INFO *cvinfo)
  965. {
  966. char buffer[256+1];
  967. bfd_size_type nread;
  968. if (bfd_seek (abfd, where, SEEK_SET) != 0)
  969. return NULL;
  970. if (length <= sizeof (CV_INFO_PDB70) && length <= sizeof (CV_INFO_PDB20))
  971. return NULL;
  972. if (length > 256)
  973. length = 256;
  974. nread = bfd_bread (buffer, length, abfd);
  975. if (length != nread)
  976. return NULL;
  977. /* Ensure null termination of filename. */
  978. memset (buffer + nread, 0, sizeof (buffer) - nread);
  979. cvinfo->CVSignature = H_GET_32 (abfd, buffer);
  980. cvinfo->Age = 0;
  981. if ((cvinfo->CVSignature == CVINFO_PDB70_CVSIGNATURE)
  982. && (length > sizeof (CV_INFO_PDB70)))
  983. {
  984. CV_INFO_PDB70 *cvinfo70 = (CV_INFO_PDB70 *)(buffer);
  985. cvinfo->Age = H_GET_32(abfd, cvinfo70->Age);
  986. /* A GUID consists of 4,2,2 byte values in little-endian order, followed
  987. by 8 single bytes. Byte swap them so we can conveniently treat the GUID
  988. as 16 bytes in big-endian order. */
  989. bfd_putb32 (bfd_getl32 (cvinfo70->Signature), cvinfo->Signature);
  990. bfd_putb16 (bfd_getl16 (&(cvinfo70->Signature[4])), &(cvinfo->Signature[4]));
  991. bfd_putb16 (bfd_getl16 (&(cvinfo70->Signature[6])), &(cvinfo->Signature[6]));
  992. memcpy (&(cvinfo->Signature[8]), &(cvinfo70->Signature[8]), 8);
  993. cvinfo->SignatureLength = CV_INFO_SIGNATURE_LENGTH;
  994. /* cvinfo->PdbFileName = cvinfo70->PdbFileName; */
  995. return cvinfo;
  996. }
  997. else if ((cvinfo->CVSignature == CVINFO_PDB20_CVSIGNATURE)
  998. && (length > sizeof (CV_INFO_PDB20)))
  999. {
  1000. CV_INFO_PDB20 *cvinfo20 = (CV_INFO_PDB20 *)(buffer);
  1001. cvinfo->Age = H_GET_32(abfd, cvinfo20->Age);
  1002. memcpy (cvinfo->Signature, cvinfo20->Signature, 4);
  1003. cvinfo->SignatureLength = 4;
  1004. /* cvinfo->PdbFileName = cvinfo20->PdbFileName; */
  1005. return cvinfo;
  1006. }
  1007. return NULL;
  1008. }
  1009. unsigned int
  1010. _bfd_XXi_write_codeview_record (bfd * abfd, file_ptr where, CODEVIEW_INFO *cvinfo)
  1011. {
  1012. const bfd_size_type size = sizeof (CV_INFO_PDB70) + 1;
  1013. bfd_size_type written;
  1014. CV_INFO_PDB70 *cvinfo70;
  1015. char * buffer;
  1016. if (bfd_seek (abfd, where, SEEK_SET) != 0)
  1017. return 0;
  1018. buffer = bfd_malloc (size);
  1019. if (buffer == NULL)
  1020. return 0;
  1021. cvinfo70 = (CV_INFO_PDB70 *) buffer;
  1022. H_PUT_32 (abfd, CVINFO_PDB70_CVSIGNATURE, cvinfo70->CvSignature);
  1023. /* Byte swap the GUID from 16 bytes in big-endian order to 4,2,2 byte values
  1024. in little-endian order, followed by 8 single bytes. */
  1025. bfd_putl32 (bfd_getb32 (cvinfo->Signature), cvinfo70->Signature);
  1026. bfd_putl16 (bfd_getb16 (&(cvinfo->Signature[4])), &(cvinfo70->Signature[4]));
  1027. bfd_putl16 (bfd_getb16 (&(cvinfo->Signature[6])), &(cvinfo70->Signature[6]));
  1028. memcpy (&(cvinfo70->Signature[8]), &(cvinfo->Signature[8]), 8);
  1029. H_PUT_32 (abfd, cvinfo->Age, cvinfo70->Age);
  1030. cvinfo70->PdbFileName[0] = '\0';
  1031. written = bfd_bwrite (buffer, size, abfd);
  1032. free (buffer);
  1033. return written == size ? size : 0;
  1034. }
  1035. static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
  1036. {
  1037. N_("Export Directory [.edata (or where ever we found it)]"),
  1038. N_("Import Directory [parts of .idata]"),
  1039. N_("Resource Directory [.rsrc]"),
  1040. N_("Exception Directory [.pdata]"),
  1041. N_("Security Directory"),
  1042. N_("Base Relocation Directory [.reloc]"),
  1043. N_("Debug Directory"),
  1044. N_("Description Directory"),
  1045. N_("Special Directory"),
  1046. N_("Thread Storage Directory [.tls]"),
  1047. N_("Load Configuration Directory"),
  1048. N_("Bound Import Directory"),
  1049. N_("Import Address Table Directory"),
  1050. N_("Delay Import Directory"),
  1051. N_("CLR Runtime Header"),
  1052. N_("Reserved")
  1053. };
  1054. static bool
  1055. pe_print_idata (bfd * abfd, void * vfile)
  1056. {
  1057. FILE *file = (FILE *) vfile;
  1058. bfd_byte *data;
  1059. asection *section;
  1060. bfd_signed_vma adj;
  1061. bfd_size_type datasize = 0;
  1062. bfd_size_type dataoff;
  1063. bfd_size_type i;
  1064. int onaline = 20;
  1065. pe_data_type *pe = pe_data (abfd);
  1066. struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
  1067. bfd_vma addr;
  1068. addr = extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress;
  1069. if (addr == 0 && extra->DataDirectory[PE_IMPORT_TABLE].Size == 0)
  1070. {
  1071. /* Maybe the extra header isn't there. Look for the section. */
  1072. section = bfd_get_section_by_name (abfd, ".idata");
  1073. if (section == NULL)
  1074. return true;
  1075. addr = section->vma;
  1076. datasize = section->size;
  1077. if (datasize == 0)
  1078. return true;
  1079. }
  1080. else
  1081. {
  1082. addr += extra->ImageBase;
  1083. for (section = abfd->sections; section != NULL; section = section->next)
  1084. {
  1085. datasize = section->size;
  1086. if (addr >= section->vma && addr < section->vma + datasize)
  1087. break;
  1088. }
  1089. if (section == NULL)
  1090. {
  1091. fprintf (file,
  1092. _("\nThere is an import table, but the section containing it could not be found\n"));
  1093. return true;
  1094. }
  1095. else if (!(section->flags & SEC_HAS_CONTENTS))
  1096. {
  1097. fprintf (file,
  1098. _("\nThere is an import table in %s, but that section has no contents\n"),
  1099. section->name);
  1100. return true;
  1101. }
  1102. }
  1103. /* xgettext:c-format */
  1104. fprintf (file, _("\nThere is an import table in %s at 0x%lx\n"),
  1105. section->name, (unsigned long) addr);
  1106. dataoff = addr - section->vma;
  1107. fprintf (file,
  1108. _("\nThe Import Tables (interpreted %s section contents)\n"),
  1109. section->name);
  1110. fprintf (file,
  1111. _("\
  1112. vma: Hint Time Forward DLL First\n\
  1113. Table Stamp Chain Name Thunk\n"));
  1114. /* Read the whole section. Some of the fields might be before dataoff. */
  1115. if (!bfd_malloc_and_get_section (abfd, section, &data))
  1116. {
  1117. free (data);
  1118. return false;
  1119. }
  1120. adj = section->vma - extra->ImageBase;
  1121. /* Print all image import descriptors. */
  1122. for (i = dataoff; i + onaline <= datasize; i += onaline)
  1123. {
  1124. bfd_vma hint_addr;
  1125. bfd_vma time_stamp;
  1126. bfd_vma forward_chain;
  1127. bfd_vma dll_name;
  1128. bfd_vma first_thunk;
  1129. int idx = 0;
  1130. bfd_size_type j;
  1131. char *dll;
  1132. /* Print (i + extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress). */
  1133. fprintf (file, " %08lx\t", (unsigned long) (i + adj));
  1134. hint_addr = bfd_get_32 (abfd, data + i);
  1135. time_stamp = bfd_get_32 (abfd, data + i + 4);
  1136. forward_chain = bfd_get_32 (abfd, data + i + 8);
  1137. dll_name = bfd_get_32 (abfd, data + i + 12);
  1138. first_thunk = bfd_get_32 (abfd, data + i + 16);
  1139. fprintf (file, "%08lx %08lx %08lx %08lx %08lx\n",
  1140. (unsigned long) hint_addr,
  1141. (unsigned long) time_stamp,
  1142. (unsigned long) forward_chain,
  1143. (unsigned long) dll_name,
  1144. (unsigned long) first_thunk);
  1145. if (hint_addr == 0 && first_thunk == 0)
  1146. break;
  1147. if (dll_name - adj >= section->size)
  1148. break;
  1149. dll = (char *) data + dll_name - adj;
  1150. /* PR 17512 file: 078-12277-0.004. */
  1151. bfd_size_type maxlen = (char *)(data + datasize) - dll - 1;
  1152. fprintf (file, _("\n\tDLL Name: %.*s\n"), (int) maxlen, dll);
  1153. /* PR 21546: When the Hint Address is zero,
  1154. we try the First Thunk instead. */
  1155. if (hint_addr == 0)
  1156. hint_addr = first_thunk;
  1157. if (hint_addr != 0 && hint_addr - adj < datasize)
  1158. {
  1159. bfd_byte *ft_data;
  1160. asection *ft_section;
  1161. bfd_vma ft_addr;
  1162. bfd_size_type ft_datasize;
  1163. int ft_idx;
  1164. int ft_allocated;
  1165. fprintf (file, _("\tvma: Hint/Ord Member-Name Bound-To\n"));
  1166. idx = hint_addr - adj;
  1167. ft_addr = first_thunk + extra->ImageBase;
  1168. ft_idx = first_thunk - adj;
  1169. ft_data = data + ft_idx;
  1170. ft_datasize = datasize - ft_idx;
  1171. ft_allocated = 0;
  1172. if (first_thunk != hint_addr)
  1173. {
  1174. /* Find the section which contains the first thunk. */
  1175. for (ft_section = abfd->sections;
  1176. ft_section != NULL;
  1177. ft_section = ft_section->next)
  1178. {
  1179. if (ft_addr >= ft_section->vma
  1180. && ft_addr < ft_section->vma + ft_section->size)
  1181. break;
  1182. }
  1183. if (ft_section == NULL)
  1184. {
  1185. fprintf (file,
  1186. _("\nThere is a first thunk, but the section containing it could not be found\n"));
  1187. continue;
  1188. }
  1189. /* Now check to see if this section is the same as our current
  1190. section. If it is not then we will have to load its data in. */
  1191. if (ft_section != section)
  1192. {
  1193. ft_idx = first_thunk - (ft_section->vma - extra->ImageBase);
  1194. ft_datasize = ft_section->size - ft_idx;
  1195. ft_data = (bfd_byte *) bfd_malloc (ft_datasize);
  1196. if (ft_data == NULL)
  1197. continue;
  1198. /* Read ft_datasize bytes starting at offset ft_idx. */
  1199. if (!bfd_get_section_contents (abfd, ft_section, ft_data,
  1200. (bfd_vma) ft_idx, ft_datasize))
  1201. {
  1202. free (ft_data);
  1203. continue;
  1204. }
  1205. ft_allocated = 1;
  1206. }
  1207. }
  1208. /* Print HintName vector entries. */
  1209. #ifdef COFF_WITH_pex64
  1210. for (j = 0; idx + j + 8 <= datasize; j += 8)
  1211. {
  1212. bfd_size_type amt;
  1213. unsigned long member = bfd_get_32 (abfd, data + idx + j);
  1214. unsigned long member_high = bfd_get_32 (abfd, data + idx + j + 4);
  1215. if (!member && !member_high)
  1216. break;
  1217. amt = member - adj;
  1218. if (HighBitSet (member_high))
  1219. fprintf (file, "\t%lx%08lx\t %4lx%08lx <none>",
  1220. member_high, member,
  1221. WithoutHighBit (member_high), member);
  1222. /* PR binutils/17512: Handle corrupt PE data. */
  1223. else if (amt >= datasize || amt + 2 >= datasize)
  1224. fprintf (file, _("\t<corrupt: 0x%04lx>"), member);
  1225. else
  1226. {
  1227. int ordinal;
  1228. char *member_name;
  1229. ordinal = bfd_get_16 (abfd, data + amt);
  1230. member_name = (char *) data + amt + 2;
  1231. fprintf (file, "\t%04lx\t %4d %.*s",member, ordinal,
  1232. (int) (datasize - (amt + 2)), member_name);
  1233. }
  1234. /* If the time stamp is not zero, the import address
  1235. table holds actual addresses. */
  1236. if (time_stamp != 0
  1237. && first_thunk != 0
  1238. && first_thunk != hint_addr
  1239. && j + 4 <= ft_datasize)
  1240. fprintf (file, "\t%04lx",
  1241. (unsigned long) bfd_get_32 (abfd, ft_data + j));
  1242. fprintf (file, "\n");
  1243. }
  1244. #else
  1245. for (j = 0; idx + j + 4 <= datasize; j += 4)
  1246. {
  1247. bfd_size_type amt;
  1248. unsigned long member = bfd_get_32 (abfd, data + idx + j);
  1249. /* Print single IMAGE_IMPORT_BY_NAME vector. */
  1250. if (member == 0)
  1251. break;
  1252. amt = member - adj;
  1253. if (HighBitSet (member))
  1254. fprintf (file, "\t%04lx\t %4lu <none>",
  1255. member, WithoutHighBit (member));
  1256. /* PR binutils/17512: Handle corrupt PE data. */
  1257. else if (amt >= datasize || amt + 2 >= datasize)
  1258. fprintf (file, _("\t<corrupt: 0x%04lx>"), member);
  1259. else
  1260. {
  1261. int ordinal;
  1262. char *member_name;
  1263. ordinal = bfd_get_16 (abfd, data + amt);
  1264. member_name = (char *) data + amt + 2;
  1265. fprintf (file, "\t%04lx\t %4d %.*s",
  1266. member, ordinal,
  1267. (int) (datasize - (amt + 2)), member_name);
  1268. }
  1269. /* If the time stamp is not zero, the import address
  1270. table holds actual addresses. */
  1271. if (time_stamp != 0
  1272. && first_thunk != 0
  1273. && first_thunk != hint_addr
  1274. && j + 4 <= ft_datasize)
  1275. fprintf (file, "\t%04lx",
  1276. (unsigned long) bfd_get_32 (abfd, ft_data + j));
  1277. fprintf (file, "\n");
  1278. }
  1279. #endif
  1280. if (ft_allocated)
  1281. free (ft_data);
  1282. }
  1283. fprintf (file, "\n");
  1284. }
  1285. free (data);
  1286. return true;
  1287. }
  1288. static bool
  1289. pe_print_edata (bfd * abfd, void * vfile)
  1290. {
  1291. FILE *file = (FILE *) vfile;
  1292. bfd_byte *data;
  1293. asection *section;
  1294. bfd_size_type datasize = 0;
  1295. bfd_size_type dataoff;
  1296. bfd_size_type i;
  1297. bfd_vma adj;
  1298. struct EDT_type
  1299. {
  1300. long export_flags; /* Reserved - should be zero. */
  1301. long time_stamp;
  1302. short major_ver;
  1303. short minor_ver;
  1304. bfd_vma name; /* RVA - relative to image base. */
  1305. long base; /* Ordinal base. */
  1306. unsigned long num_functions;/* Number in the export address table. */
  1307. unsigned long num_names; /* Number in the name pointer table. */
  1308. bfd_vma eat_addr; /* RVA to the export address table. */
  1309. bfd_vma npt_addr; /* RVA to the Export Name Pointer Table. */
  1310. bfd_vma ot_addr; /* RVA to the Ordinal Table. */
  1311. } edt;
  1312. pe_data_type *pe = pe_data (abfd);
  1313. struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
  1314. bfd_vma addr;
  1315. addr = extra->DataDirectory[PE_EXPORT_TABLE].VirtualAddress;
  1316. if (addr == 0 && extra->DataDirectory[PE_EXPORT_TABLE].Size == 0)
  1317. {
  1318. /* Maybe the extra header isn't there. Look for the section. */
  1319. section = bfd_get_section_by_name (abfd, ".edata");
  1320. if (section == NULL)
  1321. return true;
  1322. addr = section->vma;
  1323. dataoff = 0;
  1324. datasize = section->size;
  1325. if (datasize == 0)
  1326. return true;
  1327. }
  1328. else
  1329. {
  1330. addr += extra->ImageBase;
  1331. for (section = abfd->sections; section != NULL; section = section->next)
  1332. if (addr >= section->vma && addr < section->vma + section->size)
  1333. break;
  1334. if (section == NULL)
  1335. {
  1336. fprintf (file,
  1337. _("\nThere is an export table, but the section containing it could not be found\n"));
  1338. return true;
  1339. }
  1340. else if (!(section->flags & SEC_HAS_CONTENTS))
  1341. {
  1342. fprintf (file,
  1343. _("\nThere is an export table in %s, but that section has no contents\n"),
  1344. section->name);
  1345. return true;
  1346. }
  1347. dataoff = addr - section->vma;
  1348. datasize = extra->DataDirectory[PE_EXPORT_TABLE].Size;
  1349. if (dataoff > section->size
  1350. || datasize > section->size - dataoff)
  1351. {
  1352. fprintf (file,
  1353. _("\nThere is an export table in %s, but it does not fit into that section\n"),
  1354. section->name);
  1355. return true;
  1356. }
  1357. }
  1358. /* PR 17512: Handle corrupt PE binaries. */
  1359. if (datasize < 40)
  1360. {
  1361. fprintf (file,
  1362. /* xgettext:c-format */
  1363. _("\nThere is an export table in %s, but it is too small (%d)\n"),
  1364. section->name, (int) datasize);
  1365. return true;
  1366. }
  1367. /* xgettext:c-format */
  1368. fprintf (file, _("\nThere is an export table in %s at 0x%lx\n"),
  1369. section->name, (unsigned long) addr);
  1370. data = (bfd_byte *) bfd_malloc (datasize);
  1371. if (data == NULL)
  1372. return false;
  1373. if (! bfd_get_section_contents (abfd, section, data,
  1374. (file_ptr) dataoff, datasize))
  1375. return false;
  1376. /* Go get Export Directory Table. */
  1377. edt.export_flags = bfd_get_32 (abfd, data + 0);
  1378. edt.time_stamp = bfd_get_32 (abfd, data + 4);
  1379. edt.major_ver = bfd_get_16 (abfd, data + 8);
  1380. edt.minor_ver = bfd_get_16 (abfd, data + 10);
  1381. edt.name = bfd_get_32 (abfd, data + 12);
  1382. edt.base = bfd_get_32 (abfd, data + 16);
  1383. edt.num_functions = bfd_get_32 (abfd, data + 20);
  1384. edt.num_names = bfd_get_32 (abfd, data + 24);
  1385. edt.eat_addr = bfd_get_32 (abfd, data + 28);
  1386. edt.npt_addr = bfd_get_32 (abfd, data + 32);
  1387. edt.ot_addr = bfd_get_32 (abfd, data + 36);
  1388. adj = section->vma - extra->ImageBase + dataoff;
  1389. /* Dump the EDT first. */
  1390. fprintf (file,
  1391. _("\nThe Export Tables (interpreted %s section contents)\n\n"),
  1392. section->name);
  1393. fprintf (file,
  1394. _("Export Flags \t\t\t%lx\n"), (unsigned long) edt.export_flags);
  1395. fprintf (file,
  1396. _("Time/Date stamp \t\t%lx\n"), (unsigned long) edt.time_stamp);
  1397. fprintf (file,
  1398. /* xgettext:c-format */
  1399. _("Major/Minor \t\t\t%d/%d\n"), edt.major_ver, edt.minor_ver);
  1400. fprintf (file,
  1401. _("Name \t\t\t\t"));
  1402. bfd_fprintf_vma (abfd, file, edt.name);
  1403. if ((edt.name >= adj) && (edt.name < adj + datasize))
  1404. fprintf (file, " %.*s\n",
  1405. (int) (datasize - (edt.name - adj)),
  1406. data + edt.name - adj);
  1407. else
  1408. fprintf (file, "(outside .edata section)\n");
  1409. fprintf (file,
  1410. _("Ordinal Base \t\t\t%ld\n"), edt.base);
  1411. fprintf (file,
  1412. _("Number in:\n"));
  1413. fprintf (file,
  1414. _("\tExport Address Table \t\t%08lx\n"),
  1415. edt.num_functions);
  1416. fprintf (file,
  1417. _("\t[Name Pointer/Ordinal] Table\t%08lx\n"), edt.num_names);
  1418. fprintf (file,
  1419. _("Table Addresses\n"));
  1420. fprintf (file,
  1421. _("\tExport Address Table \t\t"));
  1422. bfd_fprintf_vma (abfd, file, edt.eat_addr);
  1423. fprintf (file, "\n");
  1424. fprintf (file,
  1425. _("\tName Pointer Table \t\t"));
  1426. bfd_fprintf_vma (abfd, file, edt.npt_addr);
  1427. fprintf (file, "\n");
  1428. fprintf (file,
  1429. _("\tOrdinal Table \t\t\t"));
  1430. bfd_fprintf_vma (abfd, file, edt.ot_addr);
  1431. fprintf (file, "\n");
  1432. /* The next table to find is the Export Address Table. It's basically
  1433. a list of pointers that either locate a function in this dll, or
  1434. forward the call to another dll. Something like:
  1435. typedef union
  1436. {
  1437. long export_rva;
  1438. long forwarder_rva;
  1439. } export_address_table_entry; */
  1440. fprintf (file,
  1441. _("\nExport Address Table -- Ordinal Base %ld\n"),
  1442. edt.base);
  1443. /* PR 17512: Handle corrupt PE binaries. */
  1444. /* PR 17512 file: 140-165018-0.004. */
  1445. if (edt.eat_addr - adj >= datasize
  1446. /* PR 17512: file: 092b1829 */
  1447. || (edt.num_functions + 1) * 4 < edt.num_functions
  1448. || edt.eat_addr - adj + (edt.num_functions + 1) * 4 > datasize)
  1449. fprintf (file, _("\tInvalid Export Address Table rva (0x%lx) or entry count (0x%lx)\n"),
  1450. (long) edt.eat_addr,
  1451. (long) edt.num_functions);
  1452. else for (i = 0; i < edt.num_functions; ++i)
  1453. {
  1454. bfd_vma eat_member = bfd_get_32 (abfd,
  1455. data + edt.eat_addr + (i * 4) - adj);
  1456. if (eat_member == 0)
  1457. continue;
  1458. if (eat_member - adj <= datasize)
  1459. {
  1460. /* This rva is to a name (forwarding function) in our section. */
  1461. /* Should locate a function descriptor. */
  1462. fprintf (file,
  1463. "\t[%4ld] +base[%4ld] %04lx %s -- %.*s\n",
  1464. (long) i,
  1465. (long) (i + edt.base),
  1466. (unsigned long) eat_member,
  1467. _("Forwarder RVA"),
  1468. (int)(datasize - (eat_member - adj)),
  1469. data + eat_member - adj);
  1470. }
  1471. else
  1472. {
  1473. /* Should locate a function descriptor in the reldata section. */
  1474. fprintf (file,
  1475. "\t[%4ld] +base[%4ld] %04lx %s\n",
  1476. (long) i,
  1477. (long) (i + edt.base),
  1478. (unsigned long) eat_member,
  1479. _("Export RVA"));
  1480. }
  1481. }
  1482. /* The Export Name Pointer Table is paired with the Export Ordinal Table. */
  1483. /* Dump them in parallel for clarity. */
  1484. fprintf (file,
  1485. _("\n[Ordinal/Name Pointer] Table\n"));
  1486. /* PR 17512: Handle corrupt PE binaries. */
  1487. if (edt.npt_addr + (edt.num_names * 4) - adj >= datasize
  1488. /* PR 17512: file: bb68816e. */
  1489. || edt.num_names * 4 < edt.num_names
  1490. || (data + edt.npt_addr - adj) < data)
  1491. /* xgettext:c-format */
  1492. fprintf (file, _("\tInvalid Name Pointer Table rva (0x%lx) or entry count (0x%lx)\n"),
  1493. (long) edt.npt_addr,
  1494. (long) edt.num_names);
  1495. /* PR 17512: file: 140-147171-0.004. */
  1496. else if (edt.ot_addr + (edt.num_names * 2) - adj >= datasize
  1497. || data + edt.ot_addr - adj < data)
  1498. /* xgettext:c-format */
  1499. fprintf (file, _("\tInvalid Ordinal Table rva (0x%lx) or entry count (0x%lx)\n"),
  1500. (long) edt.ot_addr,
  1501. (long) edt.num_names);
  1502. else for (i = 0; i < edt.num_names; ++i)
  1503. {
  1504. bfd_vma name_ptr;
  1505. bfd_vma ord;
  1506. ord = bfd_get_16 (abfd, data + edt.ot_addr + (i * 2) - adj);
  1507. name_ptr = bfd_get_32 (abfd, data + edt.npt_addr + (i * 4) - adj);
  1508. if ((name_ptr - adj) >= datasize)
  1509. {
  1510. /* xgettext:c-format */
  1511. fprintf (file, _("\t[%4ld] <corrupt offset: %lx>\n"),
  1512. (long) ord, (long) name_ptr);
  1513. }
  1514. else
  1515. {
  1516. char * name = (char *) data + name_ptr - adj;
  1517. fprintf (file, "\t[%4ld] %.*s\n", (long) ord,
  1518. (int)((char *)(data + datasize) - name), name);
  1519. }
  1520. }
  1521. free (data);
  1522. return true;
  1523. }
  1524. /* This really is architecture dependent. On IA-64, a .pdata entry
  1525. consists of three dwords containing relative virtual addresses that
  1526. specify the start and end address of the code range the entry
  1527. covers and the address of the corresponding unwind info data.
  1528. On ARM and SH-4, a compressed PDATA structure is used :
  1529. _IMAGE_CE_RUNTIME_FUNCTION_ENTRY, whereas MIPS is documented to use
  1530. _IMAGE_ALPHA_RUNTIME_FUNCTION_ENTRY.
  1531. See http://msdn2.microsoft.com/en-us/library/ms253988(VS.80).aspx .
  1532. This is the version for uncompressed data. */
  1533. static bool
  1534. pe_print_pdata (bfd * abfd, void * vfile)
  1535. {
  1536. #if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
  1537. # define PDATA_ROW_SIZE (3 * 8)
  1538. #else
  1539. # define PDATA_ROW_SIZE (5 * 4)
  1540. #endif
  1541. FILE *file = (FILE *) vfile;
  1542. bfd_byte *data = 0;
  1543. asection *section = bfd_get_section_by_name (abfd, ".pdata");
  1544. bfd_size_type datasize = 0;
  1545. bfd_size_type i;
  1546. bfd_size_type start, stop;
  1547. int onaline = PDATA_ROW_SIZE;
  1548. if (section == NULL
  1549. || coff_section_data (abfd, section) == NULL
  1550. || pei_section_data (abfd, section) == NULL)
  1551. return true;
  1552. stop = pei_section_data (abfd, section)->virt_size;
  1553. if ((stop % onaline) != 0)
  1554. fprintf (file,
  1555. /* xgettext:c-format */
  1556. _("warning, .pdata section size (%ld) is not a multiple of %d\n"),
  1557. (long) stop, onaline);
  1558. fprintf (file,
  1559. _("\nThe Function Table (interpreted .pdata section contents)\n"));
  1560. #if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
  1561. fprintf (file,
  1562. _(" vma:\t\t\tBegin Address End Address Unwind Info\n"));
  1563. #else
  1564. fprintf (file, _("\
  1565. vma:\t\tBegin End EH EH PrologEnd Exception\n\
  1566. \t\tAddress Address Handler Data Address Mask\n"));
  1567. #endif
  1568. datasize = section->size;
  1569. if (datasize == 0)
  1570. return true;
  1571. /* PR 17512: file: 002-193900-0.004. */
  1572. if (datasize < stop)
  1573. {
  1574. /* xgettext:c-format */
  1575. fprintf (file, _("Virtual size of .pdata section (%ld) larger than real size (%ld)\n"),
  1576. (long) stop, (long) datasize);
  1577. return false;
  1578. }
  1579. if (! bfd_malloc_and_get_section (abfd, section, &data))
  1580. {
  1581. free (data);
  1582. return false;
  1583. }
  1584. start = 0;
  1585. for (i = start; i < stop; i += onaline)
  1586. {
  1587. bfd_vma begin_addr;
  1588. bfd_vma end_addr;
  1589. bfd_vma eh_handler;
  1590. bfd_vma eh_data;
  1591. bfd_vma prolog_end_addr;
  1592. #if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64)
  1593. int em_data;
  1594. #endif
  1595. if (i + PDATA_ROW_SIZE > stop)
  1596. break;
  1597. begin_addr = GET_PDATA_ENTRY (abfd, data + i );
  1598. end_addr = GET_PDATA_ENTRY (abfd, data + i + 4);
  1599. eh_handler = GET_PDATA_ENTRY (abfd, data + i + 8);
  1600. eh_data = GET_PDATA_ENTRY (abfd, data + i + 12);
  1601. prolog_end_addr = GET_PDATA_ENTRY (abfd, data + i + 16);
  1602. if (begin_addr == 0 && end_addr == 0 && eh_handler == 0
  1603. && eh_data == 0 && prolog_end_addr == 0)
  1604. /* We are probably into the padding of the section now. */
  1605. break;
  1606. #if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64)
  1607. em_data = ((eh_handler & 0x1) << 2) | (prolog_end_addr & 0x3);
  1608. #endif
  1609. eh_handler &= ~(bfd_vma) 0x3;
  1610. prolog_end_addr &= ~(bfd_vma) 0x3;
  1611. fputc (' ', file);
  1612. bfd_fprintf_vma (abfd, file, i + section->vma); fputc ('\t', file);
  1613. bfd_fprintf_vma (abfd, file, begin_addr); fputc (' ', file);
  1614. bfd_fprintf_vma (abfd, file, end_addr); fputc (' ', file);
  1615. bfd_fprintf_vma (abfd, file, eh_handler);
  1616. #if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64)
  1617. fputc (' ', file);
  1618. bfd_fprintf_vma (abfd, file, eh_data); fputc (' ', file);
  1619. bfd_fprintf_vma (abfd, file, prolog_end_addr);
  1620. fprintf (file, " %x", em_data);
  1621. #endif
  1622. fprintf (file, "\n");
  1623. }
  1624. free (data);
  1625. return true;
  1626. #undef PDATA_ROW_SIZE
  1627. }
  1628. typedef struct sym_cache
  1629. {
  1630. int symcount;
  1631. asymbol ** syms;
  1632. } sym_cache;
  1633. static asymbol **
  1634. slurp_symtab (bfd *abfd, sym_cache *psc)
  1635. {
  1636. asymbol ** sy = NULL;
  1637. long storage;
  1638. if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
  1639. {
  1640. psc->symcount = 0;
  1641. return NULL;
  1642. }
  1643. storage = bfd_get_symtab_upper_bound (abfd);
  1644. if (storage < 0)
  1645. return NULL;
  1646. if (storage)
  1647. {
  1648. sy = (asymbol **) bfd_malloc (storage);
  1649. if (sy == NULL)
  1650. return NULL;
  1651. }
  1652. psc->symcount = bfd_canonicalize_symtab (abfd, sy);
  1653. if (psc->symcount < 0)
  1654. return NULL;
  1655. return sy;
  1656. }
  1657. static const char *
  1658. my_symbol_for_address (bfd *abfd, bfd_vma func, sym_cache *psc)
  1659. {
  1660. int i;
  1661. if (psc->syms == 0)
  1662. psc->syms = slurp_symtab (abfd, psc);
  1663. for (i = 0; i < psc->symcount; i++)
  1664. {
  1665. if (psc->syms[i]->section->vma + psc->syms[i]->value == func)
  1666. return psc->syms[i]->name;
  1667. }
  1668. return NULL;
  1669. }
  1670. static void
  1671. cleanup_syms (sym_cache *psc)
  1672. {
  1673. psc->symcount = 0;
  1674. free (psc->syms);
  1675. psc->syms = NULL;
  1676. }
  1677. /* This is the version for "compressed" pdata. */
  1678. bool
  1679. _bfd_XX_print_ce_compressed_pdata (bfd * abfd, void * vfile)
  1680. {
  1681. # define PDATA_ROW_SIZE (2 * 4)
  1682. FILE *file = (FILE *) vfile;
  1683. bfd_byte *data = NULL;
  1684. asection *section = bfd_get_section_by_name (abfd, ".pdata");
  1685. bfd_size_type datasize = 0;
  1686. bfd_size_type i;
  1687. bfd_size_type start, stop;
  1688. int onaline = PDATA_ROW_SIZE;
  1689. struct sym_cache cache = {0, 0} ;
  1690. if (section == NULL
  1691. || coff_section_data (abfd, section) == NULL
  1692. || pei_section_data (abfd, section) == NULL)
  1693. return true;
  1694. stop = pei_section_data (abfd, section)->virt_size;
  1695. if ((stop % onaline) != 0)
  1696. fprintf (file,
  1697. /* xgettext:c-format */
  1698. _("warning, .pdata section size (%ld) is not a multiple of %d\n"),
  1699. (long) stop, onaline);
  1700. fprintf (file,
  1701. _("\nThe Function Table (interpreted .pdata section contents)\n"));
  1702. fprintf (file, _("\
  1703. vma:\t\tBegin Prolog Function Flags Exception EH\n\
  1704. \t\tAddress Length Length 32b exc Handler Data\n"));
  1705. datasize = section->size;
  1706. if (datasize == 0)
  1707. return true;
  1708. if (! bfd_malloc_and_get_section (abfd, section, &data))
  1709. {
  1710. free (data);
  1711. return false;
  1712. }
  1713. start = 0;
  1714. for (i = start; i < stop; i += onaline)
  1715. {
  1716. bfd_vma begin_addr;
  1717. bfd_vma other_data;
  1718. bfd_vma prolog_length, function_length;
  1719. int flag32bit, exception_flag;
  1720. asection *tsection;
  1721. if (i + PDATA_ROW_SIZE > stop)
  1722. break;
  1723. begin_addr = GET_PDATA_ENTRY (abfd, data + i );
  1724. other_data = GET_PDATA_ENTRY (abfd, data + i + 4);
  1725. if (begin_addr == 0 && other_data == 0)
  1726. /* We are probably into the padding of the section now. */
  1727. break;
  1728. prolog_length = (other_data & 0x000000FF);
  1729. function_length = (other_data & 0x3FFFFF00) >> 8;
  1730. flag32bit = (int)((other_data & 0x40000000) >> 30);
  1731. exception_flag = (int)((other_data & 0x80000000) >> 31);
  1732. fputc (' ', file);
  1733. bfd_fprintf_vma (abfd, file, i + section->vma); fputc ('\t', file);
  1734. bfd_fprintf_vma (abfd, file, begin_addr); fputc (' ', file);
  1735. bfd_fprintf_vma (abfd, file, prolog_length); fputc (' ', file);
  1736. bfd_fprintf_vma (abfd, file, function_length); fputc (' ', file);
  1737. fprintf (file, "%2d %2d ", flag32bit, exception_flag);
  1738. /* Get the exception handler's address and the data passed from the
  1739. .text section. This is really the data that belongs with the .pdata
  1740. but got "compressed" out for the ARM and SH4 architectures. */
  1741. tsection = bfd_get_section_by_name (abfd, ".text");
  1742. if (tsection && coff_section_data (abfd, tsection)
  1743. && pei_section_data (abfd, tsection))
  1744. {
  1745. bfd_vma eh_off = (begin_addr - 8) - tsection->vma;
  1746. bfd_byte *tdata;
  1747. tdata = (bfd_byte *) bfd_malloc (8);
  1748. if (tdata)
  1749. {
  1750. if (bfd_get_section_contents (abfd, tsection, tdata, eh_off, 8))
  1751. {
  1752. bfd_vma eh, eh_data;
  1753. eh = bfd_get_32 (abfd, tdata);
  1754. eh_data = bfd_get_32 (abfd, tdata + 4);
  1755. fprintf (file, "%08x ", (unsigned int) eh);
  1756. fprintf (file, "%08x", (unsigned int) eh_data);
  1757. if (eh != 0)
  1758. {
  1759. const char *s = my_symbol_for_address (abfd, eh, &cache);
  1760. if (s)
  1761. fprintf (file, " (%s) ", s);
  1762. }
  1763. }
  1764. free (tdata);
  1765. }
  1766. }
  1767. fprintf (file, "\n");
  1768. }
  1769. free (data);
  1770. cleanup_syms (& cache);
  1771. return true;
  1772. #undef PDATA_ROW_SIZE
  1773. }
  1774. #define IMAGE_REL_BASED_HIGHADJ 4
  1775. static const char * const tbl[] =
  1776. {
  1777. "ABSOLUTE",
  1778. "HIGH",
  1779. "LOW",
  1780. "HIGHLOW",
  1781. "HIGHADJ",
  1782. "MIPS_JMPADDR",
  1783. "SECTION",
  1784. "REL32",
  1785. "RESERVED1",
  1786. "MIPS_JMPADDR16",
  1787. "DIR64",
  1788. "HIGH3ADJ",
  1789. "UNKNOWN", /* MUST be last. */
  1790. };
  1791. static bool
  1792. pe_print_reloc (bfd * abfd, void * vfile)
  1793. {
  1794. FILE *file = (FILE *) vfile;
  1795. bfd_byte *data = 0;
  1796. asection *section = bfd_get_section_by_name (abfd, ".reloc");
  1797. bfd_byte *p, *end;
  1798. if (section == NULL || section->size == 0 || !(section->flags & SEC_HAS_CONTENTS))
  1799. return true;
  1800. fprintf (file,
  1801. _("\n\nPE File Base Relocations (interpreted .reloc section contents)\n"));
  1802. if (! bfd_malloc_and_get_section (abfd, section, &data))
  1803. {
  1804. free (data);
  1805. return false;
  1806. }
  1807. p = data;
  1808. end = data + section->size;
  1809. while (p + 8 <= end)
  1810. {
  1811. int j;
  1812. bfd_vma virtual_address;
  1813. unsigned long number, size;
  1814. bfd_byte *chunk_end;
  1815. /* The .reloc section is a sequence of blocks, with a header consisting
  1816. of two 32 bit quantities, followed by a number of 16 bit entries. */
  1817. virtual_address = bfd_get_32 (abfd, p);
  1818. size = bfd_get_32 (abfd, p + 4);
  1819. p += 8;
  1820. number = (size - 8) / 2;
  1821. if (size == 0)
  1822. break;
  1823. fprintf (file,
  1824. /* xgettext:c-format */
  1825. _("\nVirtual Address: %08lx Chunk size %ld (0x%lx) Number of fixups %ld\n"),
  1826. (unsigned long) virtual_address, size, size, number);
  1827. chunk_end = p - 8 + size;
  1828. if (chunk_end > end)
  1829. chunk_end = end;
  1830. j = 0;
  1831. while (p + 2 <= chunk_end)
  1832. {
  1833. unsigned short e = bfd_get_16 (abfd, p);
  1834. unsigned int t = (e & 0xF000) >> 12;
  1835. int off = e & 0x0FFF;
  1836. if (t >= sizeof (tbl) / sizeof (tbl[0]))
  1837. t = (sizeof (tbl) / sizeof (tbl[0])) - 1;
  1838. fprintf (file,
  1839. /* xgettext:c-format */
  1840. _("\treloc %4d offset %4x [%4lx] %s"),
  1841. j, off, (unsigned long) (off + virtual_address), tbl[t]);
  1842. p += 2;
  1843. j++;
  1844. /* HIGHADJ takes an argument, - the next record *is* the
  1845. low 16 bits of addend. */
  1846. if (t == IMAGE_REL_BASED_HIGHADJ && p + 2 <= chunk_end)
  1847. {
  1848. fprintf (file, " (%4x)", (unsigned int) bfd_get_16 (abfd, p));
  1849. p += 2;
  1850. j++;
  1851. }
  1852. fprintf (file, "\n");
  1853. }
  1854. }
  1855. free (data);
  1856. return true;
  1857. }
  1858. /* A data structure describing the regions of a .rsrc section.
  1859. Some fields are filled in as the section is parsed. */
  1860. typedef struct rsrc_regions
  1861. {
  1862. bfd_byte * section_start;
  1863. bfd_byte * section_end;
  1864. bfd_byte * strings_start;
  1865. bfd_byte * resource_start;
  1866. } rsrc_regions;
  1867. static bfd_byte *
  1868. rsrc_print_resource_directory (FILE * , bfd *, unsigned int, bfd_byte *,
  1869. rsrc_regions *, bfd_vma);
  1870. /* Print the resource entry at DATA, with the text indented by INDENT.
  1871. Recusively calls rsrc_print_resource_directory to print the contents
  1872. of directory entries.
  1873. Returns the address of the end of the data associated with the entry
  1874. or section_end + 1 upon failure. */
  1875. static bfd_byte *
  1876. rsrc_print_resource_entries (FILE *file,
  1877. bfd *abfd,
  1878. unsigned int indent,
  1879. bool is_name,
  1880. bfd_byte *data,
  1881. rsrc_regions *regions,
  1882. bfd_vma rva_bias)
  1883. {
  1884. unsigned long entry, addr, size;
  1885. bfd_byte * leaf;
  1886. if (data + 8 >= regions->section_end)
  1887. return regions->section_end + 1;
  1888. /* xgettext:c-format */
  1889. fprintf (file, _("%03x %*.s Entry: "), (int)(data - regions->section_start), indent, " ");
  1890. entry = (unsigned long) bfd_get_32 (abfd, data);
  1891. if (is_name)
  1892. {
  1893. bfd_byte * name;
  1894. /* Note - the documentation says that this field is an RVA value
  1895. but windres appears to produce a section relative offset with
  1896. the top bit set. Support both styles for now. */
  1897. if (HighBitSet (entry))
  1898. name = regions->section_start + WithoutHighBit (entry);
  1899. else
  1900. name = regions->section_start + entry - rva_bias;
  1901. if (name + 2 < regions->section_end && name > regions->section_start)
  1902. {
  1903. unsigned int len;
  1904. if (regions->strings_start == NULL)
  1905. regions->strings_start = name;
  1906. len = bfd_get_16 (abfd, name);
  1907. fprintf (file, _("name: [val: %08lx len %d]: "), entry, len);
  1908. if (name + 2 + len * 2 < regions->section_end)
  1909. {
  1910. /* This strange loop is to cope with multibyte characters. */
  1911. while (len --)
  1912. {
  1913. char c;
  1914. name += 2;
  1915. c = * name;
  1916. /* Avoid printing control characters. */
  1917. if (c > 0 && c < 32)
  1918. fprintf (file, "^%c", c + 64);
  1919. else
  1920. fprintf (file, "%.1s", name);
  1921. }
  1922. }
  1923. else
  1924. {
  1925. fprintf (file, _("<corrupt string length: %#x>\n"), len);
  1926. /* PR binutils/17512: Do not try to continue decoding a
  1927. corrupted resource section. It is likely to end up with
  1928. reams of extraneous output. FIXME: We could probably
  1929. continue if we disable the printing of strings... */
  1930. return regions->section_end + 1;
  1931. }
  1932. }
  1933. else
  1934. {
  1935. fprintf (file, _("<corrupt string offset: %#lx>\n"), entry);
  1936. return regions->section_end + 1;
  1937. }
  1938. }
  1939. else
  1940. fprintf (file, _("ID: %#08lx"), entry);
  1941. entry = (long) bfd_get_32 (abfd, data + 4);
  1942. fprintf (file, _(", Value: %#08lx\n"), entry);
  1943. if (HighBitSet (entry))
  1944. {
  1945. data = regions->section_start + WithoutHighBit (entry);
  1946. if (data <= regions->section_start || data > regions->section_end)
  1947. return regions->section_end + 1;
  1948. /* FIXME: PR binutils/17512: A corrupt file could contain a loop
  1949. in the resource table. We need some way to detect this. */
  1950. return rsrc_print_resource_directory (file, abfd, indent + 1, data,
  1951. regions, rva_bias);
  1952. }
  1953. leaf = regions->section_start + entry;
  1954. if (leaf + 16 >= regions->section_end
  1955. /* PR 17512: file: 055dff7e. */
  1956. || leaf < regions->section_start)
  1957. return regions->section_end + 1;
  1958. /* xgettext:c-format */
  1959. fprintf (file, _("%03x %*.s Leaf: Addr: %#08lx, Size: %#08lx, Codepage: %d\n"),
  1960. (int) (entry), indent, " ",
  1961. addr = (long) bfd_get_32 (abfd, leaf),
  1962. size = (long) bfd_get_32 (abfd, leaf + 4),
  1963. (int) bfd_get_32 (abfd, leaf + 8));
  1964. /* Check that the reserved entry is 0. */
  1965. if (bfd_get_32 (abfd, leaf + 12) != 0
  1966. /* And that the data address/size is valid too. */
  1967. || (regions->section_start + (addr - rva_bias) + size > regions->section_end))
  1968. return regions->section_end + 1;
  1969. if (regions->resource_start == NULL)
  1970. regions->resource_start = regions->section_start + (addr - rva_bias);
  1971. return regions->section_start + (addr - rva_bias) + size;
  1972. }
  1973. #define max(a,b) ((a) > (b) ? (a) : (b))
  1974. #define min(a,b) ((a) < (b) ? (a) : (b))
  1975. static bfd_byte *
  1976. rsrc_print_resource_directory (FILE * file,
  1977. bfd * abfd,
  1978. unsigned int indent,
  1979. bfd_byte * data,
  1980. rsrc_regions * regions,
  1981. bfd_vma rva_bias)
  1982. {
  1983. unsigned int num_names, num_ids;
  1984. bfd_byte * highest_data = data;
  1985. if (data + 16 >= regions->section_end)
  1986. return regions->section_end + 1;
  1987. fprintf (file, "%03x %*.s ", (int)(data - regions->section_start), indent, " ");
  1988. switch (indent)
  1989. {
  1990. case 0: fprintf (file, "Type"); break;
  1991. case 2: fprintf (file, "Name"); break;
  1992. case 4: fprintf (file, "Language"); break;
  1993. default:
  1994. fprintf (file, _("<unknown directory type: %d>\n"), indent);
  1995. /* FIXME: For now we end the printing here. If in the
  1996. future more directory types are added to the RSRC spec
  1997. then we will need to change this. */
  1998. return regions->section_end + 1;
  1999. }
  2000. /* xgettext:c-format */
  2001. fprintf (file, _(" Table: Char: %d, Time: %08lx, Ver: %d/%d, Num Names: %d, IDs: %d\n"),
  2002. (int) bfd_get_32 (abfd, data),
  2003. (long) bfd_get_32 (abfd, data + 4),
  2004. (int) bfd_get_16 (abfd, data + 8),
  2005. (int) bfd_get_16 (abfd, data + 10),
  2006. num_names = (int) bfd_get_16 (abfd, data + 12),
  2007. num_ids = (int) bfd_get_16 (abfd, data + 14));
  2008. data += 16;
  2009. while (num_names --)
  2010. {
  2011. bfd_byte * entry_end;
  2012. entry_end = rsrc_print_resource_entries (file, abfd, indent + 1, true,
  2013. data, regions, rva_bias);
  2014. data += 8;
  2015. highest_data = max (highest_data, entry_end);
  2016. if (entry_end >= regions->section_end)
  2017. return entry_end;
  2018. }
  2019. while (num_ids --)
  2020. {
  2021. bfd_byte * entry_end;
  2022. entry_end = rsrc_print_resource_entries (file, abfd, indent + 1, false,
  2023. data, regions, rva_bias);
  2024. data += 8;
  2025. highest_data = max (highest_data, entry_end);
  2026. if (entry_end >= regions->section_end)
  2027. return entry_end;
  2028. }
  2029. return max (highest_data, data);
  2030. }
  2031. /* Display the contents of a .rsrc section. We do not try to
  2032. reproduce the resources, windres does that. Instead we dump
  2033. the tables in a human readable format. */
  2034. static bool
  2035. rsrc_print_section (bfd * abfd, void * vfile)
  2036. {
  2037. bfd_vma rva_bias;
  2038. pe_data_type * pe;
  2039. FILE * file = (FILE *) vfile;
  2040. bfd_size_type datasize;
  2041. asection * section;
  2042. bfd_byte * data;
  2043. rsrc_regions regions;
  2044. pe = pe_data (abfd);
  2045. if (pe == NULL)
  2046. return true;
  2047. section = bfd_get_section_by_name (abfd, ".rsrc");
  2048. if (section == NULL)
  2049. return true;
  2050. if (!(section->flags & SEC_HAS_CONTENTS))
  2051. return true;
  2052. datasize = section->size;
  2053. if (datasize == 0)
  2054. return true;
  2055. rva_bias = section->vma - pe->pe_opthdr.ImageBase;
  2056. if (! bfd_malloc_and_get_section (abfd, section, & data))
  2057. {
  2058. free (data);
  2059. return false;
  2060. }
  2061. regions.section_start = data;
  2062. regions.section_end = data + datasize;
  2063. regions.strings_start = NULL;
  2064. regions.resource_start = NULL;
  2065. fflush (file);
  2066. fprintf (file, "\nThe .rsrc Resource Directory section:\n");
  2067. while (data < regions.section_end)
  2068. {
  2069. bfd_byte * p = data;
  2070. data = rsrc_print_resource_directory (file, abfd, 0, data, & regions, rva_bias);
  2071. if (data == regions.section_end + 1)
  2072. fprintf (file, _("Corrupt .rsrc section detected!\n"));
  2073. else
  2074. {
  2075. /* Align data before continuing. */
  2076. int align = (1 << section->alignment_power) - 1;
  2077. data = (bfd_byte *) (((ptrdiff_t) (data + align)) & ~ align);
  2078. rva_bias += data - p;
  2079. /* For reasons that are unclear .rsrc sections are sometimes created
  2080. aligned to a 1^3 boundary even when their alignment is set at
  2081. 1^2. Catch that case here before we issue a spurious warning
  2082. message. */
  2083. if (data == (regions.section_end - 4))
  2084. data = regions.section_end;
  2085. else if (data < regions.section_end)
  2086. {
  2087. /* If the extra data is all zeros then do not complain.
  2088. This is just padding so that the section meets the
  2089. page size requirements. */
  2090. while (++ data < regions.section_end)
  2091. if (*data != 0)
  2092. break;
  2093. if (data < regions.section_end)
  2094. fprintf (file, _("\nWARNING: Extra data in .rsrc section - it will be ignored by Windows:\n"));
  2095. }
  2096. }
  2097. }
  2098. if (regions.strings_start != NULL)
  2099. fprintf (file, _(" String table starts at offset: %#03x\n"),
  2100. (int) (regions.strings_start - regions.section_start));
  2101. if (regions.resource_start != NULL)
  2102. fprintf (file, _(" Resources start at offset: %#03x\n"),
  2103. (int) (regions.resource_start - regions.section_start));
  2104. free (regions.section_start);
  2105. return true;
  2106. }
  2107. #define IMAGE_NUMBEROF_DEBUG_TYPES 17
  2108. static char * debug_type_names[IMAGE_NUMBEROF_DEBUG_TYPES] =
  2109. {
  2110. "Unknown",
  2111. "COFF",
  2112. "CodeView",
  2113. "FPO",
  2114. "Misc",
  2115. "Exception",
  2116. "Fixup",
  2117. "OMAP-to-SRC",
  2118. "OMAP-from-SRC",
  2119. "Borland",
  2120. "Reserved",
  2121. "CLSID",
  2122. "Feature",
  2123. "CoffGrp",
  2124. "ILTCG",
  2125. "MPX",
  2126. "Repro",
  2127. };
  2128. static bool
  2129. pe_print_debugdata (bfd * abfd, void * vfile)
  2130. {
  2131. FILE *file = (FILE *) vfile;
  2132. pe_data_type *pe = pe_data (abfd);
  2133. struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
  2134. asection *section;
  2135. bfd_byte *data = 0;
  2136. bfd_size_type dataoff;
  2137. unsigned int i, j;
  2138. bfd_vma addr = extra->DataDirectory[PE_DEBUG_DATA].VirtualAddress;
  2139. bfd_size_type size = extra->DataDirectory[PE_DEBUG_DATA].Size;
  2140. if (size == 0)
  2141. return true;
  2142. addr += extra->ImageBase;
  2143. for (section = abfd->sections; section != NULL; section = section->next)
  2144. {
  2145. if ((addr >= section->vma) && (addr < (section->vma + section->size)))
  2146. break;
  2147. }
  2148. if (section == NULL)
  2149. {
  2150. fprintf (file,
  2151. _("\nThere is a debug directory, but the section containing it could not be found\n"));
  2152. return true;
  2153. }
  2154. else if (!(section->flags & SEC_HAS_CONTENTS))
  2155. {
  2156. fprintf (file,
  2157. _("\nThere is a debug directory in %s, but that section has no contents\n"),
  2158. section->name);
  2159. return true;
  2160. }
  2161. else if (section->size < size)
  2162. {
  2163. fprintf (file,
  2164. _("\nError: section %s contains the debug data starting address but it is too small\n"),
  2165. section->name);
  2166. return false;
  2167. }
  2168. fprintf (file, _("\nThere is a debug directory in %s at 0x%lx\n\n"),
  2169. section->name, (unsigned long) addr);
  2170. dataoff = addr - section->vma;
  2171. if (size > (section->size - dataoff))
  2172. {
  2173. fprintf (file, _("The debug data size field in the data directory is too big for the section"));
  2174. return false;
  2175. }
  2176. fprintf (file,
  2177. _("Type Size Rva Offset\n"));
  2178. /* Read the whole section. */
  2179. if (!bfd_malloc_and_get_section (abfd, section, &data))
  2180. {
  2181. free (data);
  2182. return false;
  2183. }
  2184. for (i = 0; i < size / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
  2185. {
  2186. const char *type_name;
  2187. struct external_IMAGE_DEBUG_DIRECTORY *ext
  2188. = &((struct external_IMAGE_DEBUG_DIRECTORY *)(data + dataoff))[i];
  2189. struct internal_IMAGE_DEBUG_DIRECTORY idd;
  2190. _bfd_XXi_swap_debugdir_in (abfd, ext, &idd);
  2191. if ((idd.Type) >= IMAGE_NUMBEROF_DEBUG_TYPES)
  2192. type_name = debug_type_names[0];
  2193. else
  2194. type_name = debug_type_names[idd.Type];
  2195. fprintf (file, " %2ld %14s %08lx %08lx %08lx\n",
  2196. idd.Type, type_name, idd.SizeOfData,
  2197. idd.AddressOfRawData, idd.PointerToRawData);
  2198. if (idd.Type == PE_IMAGE_DEBUG_TYPE_CODEVIEW)
  2199. {
  2200. char signature[CV_INFO_SIGNATURE_LENGTH * 2 + 1];
  2201. /* PR 17512: file: 065-29434-0.001:0.1
  2202. We need to use a 32-bit aligned buffer
  2203. to safely read in a codeview record. */
  2204. char buffer[256 + 1] ATTRIBUTE_ALIGNED_ALIGNOF (CODEVIEW_INFO);
  2205. CODEVIEW_INFO *cvinfo = (CODEVIEW_INFO *) buffer;
  2206. /* The debug entry doesn't have to have to be in a section,
  2207. in which case AddressOfRawData is 0, so always use PointerToRawData. */
  2208. if (!_bfd_XXi_slurp_codeview_record (abfd, (file_ptr) idd.PointerToRawData,
  2209. idd.SizeOfData, cvinfo))
  2210. continue;
  2211. for (j = 0; j < cvinfo->SignatureLength; j++)
  2212. sprintf (&signature[j*2], "%02x", cvinfo->Signature[j] & 0xff);
  2213. /* xgettext:c-format */
  2214. fprintf (file, _("(format %c%c%c%c signature %s age %ld)\n"),
  2215. buffer[0], buffer[1], buffer[2], buffer[3],
  2216. signature, cvinfo->Age);
  2217. }
  2218. }
  2219. free(data);
  2220. if (size % sizeof (struct external_IMAGE_DEBUG_DIRECTORY) != 0)
  2221. fprintf (file,
  2222. _("The debug directory size is not a multiple of the debug directory entry size\n"));
  2223. return true;
  2224. }
  2225. static bool
  2226. pe_is_repro (bfd * abfd)
  2227. {
  2228. pe_data_type *pe = pe_data (abfd);
  2229. struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
  2230. asection *section;
  2231. bfd_byte *data = 0;
  2232. bfd_size_type dataoff;
  2233. unsigned int i;
  2234. bool res = false;
  2235. bfd_vma addr = extra->DataDirectory[PE_DEBUG_DATA].VirtualAddress;
  2236. bfd_size_type size = extra->DataDirectory[PE_DEBUG_DATA].Size;
  2237. if (size == 0)
  2238. return false;
  2239. addr += extra->ImageBase;
  2240. for (section = abfd->sections; section != NULL; section = section->next)
  2241. {
  2242. if ((addr >= section->vma) && (addr < (section->vma + section->size)))
  2243. break;
  2244. }
  2245. if ((section == NULL)
  2246. || (!(section->flags & SEC_HAS_CONTENTS))
  2247. || (section->size < size))
  2248. {
  2249. return false;
  2250. }
  2251. dataoff = addr - section->vma;
  2252. if (size > (section->size - dataoff))
  2253. {
  2254. return false;
  2255. }
  2256. if (!bfd_malloc_and_get_section (abfd, section, &data))
  2257. {
  2258. free (data);
  2259. return false;
  2260. }
  2261. for (i = 0; i < size / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
  2262. {
  2263. struct external_IMAGE_DEBUG_DIRECTORY *ext
  2264. = &((struct external_IMAGE_DEBUG_DIRECTORY *)(data + dataoff))[i];
  2265. struct internal_IMAGE_DEBUG_DIRECTORY idd;
  2266. _bfd_XXi_swap_debugdir_in (abfd, ext, &idd);
  2267. if (idd.Type == PE_IMAGE_DEBUG_TYPE_REPRO)
  2268. {
  2269. res = true;
  2270. break;
  2271. }
  2272. }
  2273. free(data);
  2274. return res;
  2275. }
  2276. /* Print out the program headers. */
  2277. bool
  2278. _bfd_XX_print_private_bfd_data_common (bfd * abfd, void * vfile)
  2279. {
  2280. FILE *file = (FILE *) vfile;
  2281. int j;
  2282. pe_data_type *pe = pe_data (abfd);
  2283. struct internal_extra_pe_aouthdr *i = &pe->pe_opthdr;
  2284. const char *subsystem_name = NULL;
  2285. const char *name;
  2286. /* The MS dumpbin program reportedly ands with 0xff0f before
  2287. printing the characteristics field. Not sure why. No reason to
  2288. emulate it here. */
  2289. fprintf (file, _("\nCharacteristics 0x%x\n"), pe->real_flags);
  2290. #undef PF
  2291. #define PF(x, y) if (pe->real_flags & x) { fprintf (file, "\t%s\n", y); }
  2292. PF (IMAGE_FILE_RELOCS_STRIPPED, "relocations stripped");
  2293. PF (IMAGE_FILE_EXECUTABLE_IMAGE, "executable");
  2294. PF (IMAGE_FILE_LINE_NUMS_STRIPPED, "line numbers stripped");
  2295. PF (IMAGE_FILE_LOCAL_SYMS_STRIPPED, "symbols stripped");
  2296. PF (IMAGE_FILE_LARGE_ADDRESS_AWARE, "large address aware");
  2297. PF (IMAGE_FILE_BYTES_REVERSED_LO, "little endian");
  2298. PF (IMAGE_FILE_32BIT_MACHINE, "32 bit words");
  2299. PF (IMAGE_FILE_DEBUG_STRIPPED, "debugging information removed");
  2300. PF (IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "copy to swap file if on removable media");
  2301. PF (IMAGE_FILE_NET_RUN_FROM_SWAP, "copy to swap file if on network media");
  2302. PF (IMAGE_FILE_SYSTEM, "system file");
  2303. PF (IMAGE_FILE_DLL, "DLL");
  2304. PF (IMAGE_FILE_UP_SYSTEM_ONLY, "run only on uniprocessor machine");
  2305. PF (IMAGE_FILE_BYTES_REVERSED_HI, "big endian");
  2306. #undef PF
  2307. /*
  2308. If a PE_IMAGE_DEBUG_TYPE_REPRO entry is present in the debug directory, the
  2309. timestamp is to be interpreted as the hash of a reproducible build.
  2310. */
  2311. if (pe_is_repro (abfd))
  2312. {
  2313. fprintf (file, "\nTime/Date\t\t%08lx", pe->coff.timestamp);
  2314. fprintf (file, "\t(This is a reproducible build file hash, not a timestamp)\n");
  2315. }
  2316. else
  2317. {
  2318. /* ctime implies '\n'. */
  2319. time_t t = pe->coff.timestamp;
  2320. fprintf (file, "\nTime/Date\t\t%s", ctime (&t));
  2321. }
  2322. #ifndef IMAGE_NT_OPTIONAL_HDR_MAGIC
  2323. # define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x10b
  2324. #endif
  2325. #ifndef IMAGE_NT_OPTIONAL_HDR64_MAGIC
  2326. # define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
  2327. #endif
  2328. #ifndef IMAGE_NT_OPTIONAL_HDRROM_MAGIC
  2329. # define IMAGE_NT_OPTIONAL_HDRROM_MAGIC 0x107
  2330. #endif
  2331. switch (i->Magic)
  2332. {
  2333. case IMAGE_NT_OPTIONAL_HDR_MAGIC:
  2334. name = "PE32";
  2335. break;
  2336. case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
  2337. name = "PE32+";
  2338. break;
  2339. case IMAGE_NT_OPTIONAL_HDRROM_MAGIC:
  2340. name = "ROM";
  2341. break;
  2342. default:
  2343. name = NULL;
  2344. break;
  2345. }
  2346. fprintf (file, "Magic\t\t\t%04x", i->Magic);
  2347. if (name)
  2348. fprintf (file, "\t(%s)",name);
  2349. fprintf (file, "\nMajorLinkerVersion\t%d\n", i->MajorLinkerVersion);
  2350. fprintf (file, "MinorLinkerVersion\t%d\n", i->MinorLinkerVersion);
  2351. fprintf (file, "SizeOfCode\t\t");
  2352. bfd_fprintf_vma (abfd, file, i->SizeOfCode);
  2353. fprintf (file, "\nSizeOfInitializedData\t");
  2354. bfd_fprintf_vma (abfd, file, i->SizeOfInitializedData);
  2355. fprintf (file, "\nSizeOfUninitializedData\t");
  2356. bfd_fprintf_vma (abfd, file, i->SizeOfUninitializedData);
  2357. fprintf (file, "\nAddressOfEntryPoint\t");
  2358. bfd_fprintf_vma (abfd, file, i->AddressOfEntryPoint);
  2359. fprintf (file, "\nBaseOfCode\t\t");
  2360. bfd_fprintf_vma (abfd, file, i->BaseOfCode);
  2361. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
  2362. /* PE32+ does not have BaseOfData member! */
  2363. fprintf (file, "\nBaseOfData\t\t");
  2364. bfd_fprintf_vma (abfd, file, i->BaseOfData);
  2365. #endif
  2366. fprintf (file, "\nImageBase\t\t");
  2367. bfd_fprintf_vma (abfd, file, i->ImageBase);
  2368. fprintf (file, "\nSectionAlignment\t%08x\n", i->SectionAlignment);
  2369. fprintf (file, "FileAlignment\t\t%08x\n", i->FileAlignment);
  2370. fprintf (file, "MajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion);
  2371. fprintf (file, "MinorOSystemVersion\t%d\n", i->MinorOperatingSystemVersion);
  2372. fprintf (file, "MajorImageVersion\t%d\n", i->MajorImageVersion);
  2373. fprintf (file, "MinorImageVersion\t%d\n", i->MinorImageVersion);
  2374. fprintf (file, "MajorSubsystemVersion\t%d\n", i->MajorSubsystemVersion);
  2375. fprintf (file, "MinorSubsystemVersion\t%d\n", i->MinorSubsystemVersion);
  2376. fprintf (file, "Win32Version\t\t%08x\n", i->Reserved1);
  2377. fprintf (file, "SizeOfImage\t\t%08x\n", i->SizeOfImage);
  2378. fprintf (file, "SizeOfHeaders\t\t%08x\n", i->SizeOfHeaders);
  2379. fprintf (file, "CheckSum\t\t%08x\n", i->CheckSum);
  2380. switch (i->Subsystem)
  2381. {
  2382. case IMAGE_SUBSYSTEM_UNKNOWN:
  2383. subsystem_name = "unspecified";
  2384. break;
  2385. case IMAGE_SUBSYSTEM_NATIVE:
  2386. subsystem_name = "NT native";
  2387. break;
  2388. case IMAGE_SUBSYSTEM_WINDOWS_GUI:
  2389. subsystem_name = "Windows GUI";
  2390. break;
  2391. case IMAGE_SUBSYSTEM_WINDOWS_CUI:
  2392. subsystem_name = "Windows CUI";
  2393. break;
  2394. case IMAGE_SUBSYSTEM_POSIX_CUI:
  2395. subsystem_name = "POSIX CUI";
  2396. break;
  2397. case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
  2398. subsystem_name = "Wince CUI";
  2399. break;
  2400. /* These are from UEFI Platform Initialization Specification 1.1. */
  2401. case IMAGE_SUBSYSTEM_EFI_APPLICATION:
  2402. subsystem_name = "EFI application";
  2403. break;
  2404. case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
  2405. subsystem_name = "EFI boot service driver";
  2406. break;
  2407. case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
  2408. subsystem_name = "EFI runtime driver";
  2409. break;
  2410. case IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:
  2411. subsystem_name = "SAL runtime driver";
  2412. break;
  2413. /* This is from revision 8.0 of the MS PE/COFF spec */
  2414. case IMAGE_SUBSYSTEM_XBOX:
  2415. subsystem_name = "XBOX";
  2416. break;
  2417. /* Added default case for clarity - subsystem_name is NULL anyway. */
  2418. default:
  2419. subsystem_name = NULL;
  2420. }
  2421. fprintf (file, "Subsystem\t\t%08x", i->Subsystem);
  2422. if (subsystem_name)
  2423. fprintf (file, "\t(%s)", subsystem_name);
  2424. fprintf (file, "\nDllCharacteristics\t%08x\n", i->DllCharacteristics);
  2425. if (i->DllCharacteristics)
  2426. {
  2427. unsigned short dllch = i->DllCharacteristics;
  2428. const char *indent = "\t\t\t\t\t";
  2429. if (dllch & IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA)
  2430. fprintf (file, "%sHIGH_ENTROPY_VA\n", indent);
  2431. if (dllch & IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE)
  2432. fprintf (file, "%sDYNAMIC_BASE\n", indent);
  2433. if (dllch & IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY)
  2434. fprintf (file, "%sFORCE_INTEGRITY\n", indent);
  2435. if (dllch & IMAGE_DLL_CHARACTERISTICS_NX_COMPAT)
  2436. fprintf (file, "%sNX_COMPAT\n", indent);
  2437. if (dllch & IMAGE_DLLCHARACTERISTICS_NO_ISOLATION)
  2438. fprintf (file, "%sNO_ISOLATION\n", indent);
  2439. if (dllch & IMAGE_DLLCHARACTERISTICS_NO_SEH)
  2440. fprintf (file, "%sNO_SEH\n", indent);
  2441. if (dllch & IMAGE_DLLCHARACTERISTICS_NO_BIND)
  2442. fprintf (file, "%sNO_BIND\n", indent);
  2443. if (dllch & IMAGE_DLLCHARACTERISTICS_APPCONTAINER)
  2444. fprintf (file, "%sAPPCONTAINER\n", indent);
  2445. if (dllch & IMAGE_DLLCHARACTERISTICS_WDM_DRIVER)
  2446. fprintf (file, "%sWDM_DRIVER\n", indent);
  2447. if (dllch & IMAGE_DLLCHARACTERISTICS_GUARD_CF)
  2448. fprintf (file, "%sGUARD_CF\n", indent);
  2449. if (dllch & IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE)
  2450. fprintf (file, "%sTERMINAL_SERVICE_AWARE\n", indent);
  2451. }
  2452. fprintf (file, "SizeOfStackReserve\t");
  2453. bfd_fprintf_vma (abfd, file, i->SizeOfStackReserve);
  2454. fprintf (file, "\nSizeOfStackCommit\t");
  2455. bfd_fprintf_vma (abfd, file, i->SizeOfStackCommit);
  2456. fprintf (file, "\nSizeOfHeapReserve\t");
  2457. bfd_fprintf_vma (abfd, file, i->SizeOfHeapReserve);
  2458. fprintf (file, "\nSizeOfHeapCommit\t");
  2459. bfd_fprintf_vma (abfd, file, i->SizeOfHeapCommit);
  2460. fprintf (file, "\nLoaderFlags\t\t%08lx\n", (unsigned long) i->LoaderFlags);
  2461. fprintf (file, "NumberOfRvaAndSizes\t%08lx\n",
  2462. (unsigned long) i->NumberOfRvaAndSizes);
  2463. fprintf (file, "\nThe Data Directory\n");
  2464. for (j = 0; j < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; j++)
  2465. {
  2466. fprintf (file, "Entry %1x ", j);
  2467. bfd_fprintf_vma (abfd, file, i->DataDirectory[j].VirtualAddress);
  2468. fprintf (file, " %08lx ", (unsigned long) i->DataDirectory[j].Size);
  2469. fprintf (file, "%s\n", dir_names[j]);
  2470. }
  2471. pe_print_idata (abfd, vfile);
  2472. pe_print_edata (abfd, vfile);
  2473. if (bfd_coff_have_print_pdata (abfd))
  2474. bfd_coff_print_pdata (abfd, vfile);
  2475. else
  2476. pe_print_pdata (abfd, vfile);
  2477. pe_print_reloc (abfd, vfile);
  2478. pe_print_debugdata (abfd, file);
  2479. rsrc_print_section (abfd, vfile);
  2480. return true;
  2481. }
  2482. static bool
  2483. is_vma_in_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
  2484. {
  2485. bfd_vma addr = * (bfd_vma *) obj;
  2486. return (addr >= sect->vma) && (addr < (sect->vma + sect->size));
  2487. }
  2488. static asection *
  2489. find_section_by_vma (bfd *abfd, bfd_vma addr)
  2490. {
  2491. return bfd_sections_find_if (abfd, is_vma_in_section, (void *) & addr);
  2492. }
  2493. /* Copy any private info we understand from the input bfd
  2494. to the output bfd. */
  2495. bool
  2496. _bfd_XX_bfd_copy_private_bfd_data_common (bfd * ibfd, bfd * obfd)
  2497. {
  2498. pe_data_type *ipe, *ope;
  2499. bfd_size_type size;
  2500. /* One day we may try to grok other private data. */
  2501. if (ibfd->xvec->flavour != bfd_target_coff_flavour
  2502. || obfd->xvec->flavour != bfd_target_coff_flavour)
  2503. return true;
  2504. ipe = pe_data (ibfd);
  2505. ope = pe_data (obfd);
  2506. /* pe_opthdr is copied in copy_object. */
  2507. ope->dll = ipe->dll;
  2508. /* Don't copy input subsystem if output is different from input. */
  2509. if (obfd->xvec != ibfd->xvec)
  2510. ope->pe_opthdr.Subsystem = IMAGE_SUBSYSTEM_UNKNOWN;
  2511. /* For strip: if we removed .reloc, we'll make a real mess of things
  2512. if we don't remove this entry as well. */
  2513. if (! pe_data (obfd)->has_reloc_section)
  2514. {
  2515. pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].VirtualAddress = 0;
  2516. pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].Size = 0;
  2517. }
  2518. /* For PIE, if there is .reloc, we won't add IMAGE_FILE_RELOCS_STRIPPED.
  2519. But there is no .reloc, we make sure that IMAGE_FILE_RELOCS_STRIPPED
  2520. won't be added. */
  2521. if (! pe_data (ibfd)->has_reloc_section
  2522. && ! (pe_data (ibfd)->real_flags & IMAGE_FILE_RELOCS_STRIPPED))
  2523. pe_data (obfd)->dont_strip_reloc = 1;
  2524. memcpy (ope->dos_message, ipe->dos_message, sizeof (ope->dos_message));
  2525. /* The file offsets contained in the debug directory need rewriting. */
  2526. size = ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size;
  2527. if (size != 0)
  2528. {
  2529. bfd_vma addr = ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress
  2530. + ope->pe_opthdr.ImageBase;
  2531. /* In particular a .buildid section may overlap (in VA space) with
  2532. whatever section comes ahead of it (largely because of section->size
  2533. representing s_size, not virt_size). Therefore don't look for the
  2534. section containing the first byte, but for that covering the last
  2535. one. */
  2536. bfd_vma last = addr + size - 1;
  2537. asection *section = find_section_by_vma (obfd, last);
  2538. if (section != NULL)
  2539. {
  2540. bfd_byte *data;
  2541. bfd_vma dataoff = addr - section->vma;
  2542. /* PR 17512: file: 0f15796a. */
  2543. if (addr < section->vma
  2544. || section->size < dataoff
  2545. || section->size - dataoff < size)
  2546. {
  2547. /* xgettext:c-format */
  2548. _bfd_error_handler
  2549. (_("%pB: Data Directory (%lx bytes at %" PRIx64 ") "
  2550. "extends across section boundary at %" PRIx64),
  2551. obfd, ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size,
  2552. (uint64_t) addr, (uint64_t) section->vma);
  2553. return false;
  2554. }
  2555. if (bfd_malloc_and_get_section (obfd, section, &data))
  2556. {
  2557. unsigned int i;
  2558. struct external_IMAGE_DEBUG_DIRECTORY *dd =
  2559. (struct external_IMAGE_DEBUG_DIRECTORY *)(data + dataoff);
  2560. for (i = 0; i < ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
  2561. / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
  2562. {
  2563. asection *ddsection;
  2564. struct external_IMAGE_DEBUG_DIRECTORY *edd = &(dd[i]);
  2565. struct internal_IMAGE_DEBUG_DIRECTORY idd;
  2566. bfd_vma idd_vma;
  2567. _bfd_XXi_swap_debugdir_in (obfd, edd, &idd);
  2568. /* RVA 0 means only offset is valid, not handled yet. */
  2569. if (idd.AddressOfRawData == 0)
  2570. continue;
  2571. idd_vma = idd.AddressOfRawData + ope->pe_opthdr.ImageBase;
  2572. ddsection = find_section_by_vma (obfd, idd_vma);
  2573. if (!ddsection)
  2574. continue; /* Not in a section! */
  2575. idd.PointerToRawData
  2576. = ddsection->filepos + idd_vma - ddsection->vma;
  2577. _bfd_XXi_swap_debugdir_out (obfd, &idd, edd);
  2578. }
  2579. if (!bfd_set_section_contents (obfd, section, data, 0,
  2580. section->size))
  2581. {
  2582. _bfd_error_handler (_("failed to update file offsets"
  2583. " in debug directory"));
  2584. free (data);
  2585. return false;
  2586. }
  2587. free (data);
  2588. }
  2589. else
  2590. {
  2591. _bfd_error_handler (_("%pB: failed to read "
  2592. "debug data section"), obfd);
  2593. return false;
  2594. }
  2595. }
  2596. }
  2597. return true;
  2598. }
  2599. /* Copy private section data. */
  2600. bool
  2601. _bfd_XX_bfd_copy_private_section_data (bfd *ibfd,
  2602. asection *isec,
  2603. bfd *obfd,
  2604. asection *osec)
  2605. {
  2606. if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour
  2607. || bfd_get_flavour (obfd) != bfd_target_coff_flavour)
  2608. return true;
  2609. if (coff_section_data (ibfd, isec) != NULL
  2610. && pei_section_data (ibfd, isec) != NULL)
  2611. {
  2612. if (coff_section_data (obfd, osec) == NULL)
  2613. {
  2614. size_t amt = sizeof (struct coff_section_tdata);
  2615. osec->used_by_bfd = bfd_zalloc (obfd, amt);
  2616. if (osec->used_by_bfd == NULL)
  2617. return false;
  2618. }
  2619. if (pei_section_data (obfd, osec) == NULL)
  2620. {
  2621. size_t amt = sizeof (struct pei_section_tdata);
  2622. coff_section_data (obfd, osec)->tdata = bfd_zalloc (obfd, amt);
  2623. if (coff_section_data (obfd, osec)->tdata == NULL)
  2624. return false;
  2625. }
  2626. pei_section_data (obfd, osec)->virt_size =
  2627. pei_section_data (ibfd, isec)->virt_size;
  2628. pei_section_data (obfd, osec)->pe_flags =
  2629. pei_section_data (ibfd, isec)->pe_flags;
  2630. }
  2631. return true;
  2632. }
  2633. void
  2634. _bfd_XX_get_symbol_info (bfd * abfd, asymbol *symbol, symbol_info *ret)
  2635. {
  2636. coff_get_symbol_info (abfd, symbol, ret);
  2637. }
  2638. #if !defined(COFF_WITH_pep) && (defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64))
  2639. static int
  2640. sort_x64_pdata (const void *l, const void *r)
  2641. {
  2642. const char *lp = (const char *) l;
  2643. const char *rp = (const char *) r;
  2644. bfd_vma vl, vr;
  2645. vl = bfd_getl32 (lp); vr = bfd_getl32 (rp);
  2646. if (vl != vr)
  2647. return (vl < vr ? -1 : 1);
  2648. /* We compare just begin address. */
  2649. return 0;
  2650. }
  2651. #endif
  2652. /* Functions to process a .rsrc section. */
  2653. static unsigned int sizeof_leaves;
  2654. static unsigned int sizeof_strings;
  2655. static unsigned int sizeof_tables_and_entries;
  2656. static bfd_byte *
  2657. rsrc_count_directory (bfd *, bfd_byte *, bfd_byte *, bfd_byte *, bfd_vma);
  2658. static bfd_byte *
  2659. rsrc_count_entries (bfd *abfd,
  2660. bool is_name,
  2661. bfd_byte *datastart,
  2662. bfd_byte *data,
  2663. bfd_byte *dataend,
  2664. bfd_vma rva_bias)
  2665. {
  2666. unsigned long entry, addr, size;
  2667. if (data + 8 >= dataend)
  2668. return dataend + 1;
  2669. if (is_name)
  2670. {
  2671. bfd_byte * name;
  2672. entry = (long) bfd_get_32 (abfd, data);
  2673. if (HighBitSet (entry))
  2674. name = datastart + WithoutHighBit (entry);
  2675. else
  2676. name = datastart + entry - rva_bias;
  2677. if (name + 2 >= dataend || name < datastart)
  2678. return dataend + 1;
  2679. unsigned int len = bfd_get_16 (abfd, name);
  2680. if (len == 0 || len > 256)
  2681. return dataend + 1;
  2682. }
  2683. entry = (long) bfd_get_32 (abfd, data + 4);
  2684. if (HighBitSet (entry))
  2685. {
  2686. data = datastart + WithoutHighBit (entry);
  2687. if (data <= datastart || data >= dataend)
  2688. return dataend + 1;
  2689. return rsrc_count_directory (abfd, datastart, data, dataend, rva_bias);
  2690. }
  2691. if (datastart + entry + 16 >= dataend)
  2692. return dataend + 1;
  2693. addr = (long) bfd_get_32 (abfd, datastart + entry);
  2694. size = (long) bfd_get_32 (abfd, datastart + entry + 4);
  2695. return datastart + addr - rva_bias + size;
  2696. }
  2697. static bfd_byte *
  2698. rsrc_count_directory (bfd * abfd,
  2699. bfd_byte * datastart,
  2700. bfd_byte * data,
  2701. bfd_byte * dataend,
  2702. bfd_vma rva_bias)
  2703. {
  2704. unsigned int num_entries, num_ids;
  2705. bfd_byte * highest_data = data;
  2706. if (data + 16 >= dataend)
  2707. return dataend + 1;
  2708. num_entries = (int) bfd_get_16 (abfd, data + 12);
  2709. num_ids = (int) bfd_get_16 (abfd, data + 14);
  2710. num_entries += num_ids;
  2711. data += 16;
  2712. while (num_entries --)
  2713. {
  2714. bfd_byte * entry_end;
  2715. entry_end = rsrc_count_entries (abfd, num_entries >= num_ids,
  2716. datastart, data, dataend, rva_bias);
  2717. data += 8;
  2718. highest_data = max (highest_data, entry_end);
  2719. if (entry_end >= dataend)
  2720. break;
  2721. }
  2722. return max (highest_data, data);
  2723. }
  2724. typedef struct rsrc_dir_chain
  2725. {
  2726. unsigned int num_entries;
  2727. struct rsrc_entry * first_entry;
  2728. struct rsrc_entry * last_entry;
  2729. } rsrc_dir_chain;
  2730. typedef struct rsrc_directory
  2731. {
  2732. unsigned int characteristics;
  2733. unsigned int time;
  2734. unsigned int major;
  2735. unsigned int minor;
  2736. rsrc_dir_chain names;
  2737. rsrc_dir_chain ids;
  2738. struct rsrc_entry * entry;
  2739. } rsrc_directory;
  2740. typedef struct rsrc_string
  2741. {
  2742. unsigned int len;
  2743. bfd_byte * string;
  2744. } rsrc_string;
  2745. typedef struct rsrc_leaf
  2746. {
  2747. unsigned int size;
  2748. unsigned int codepage;
  2749. bfd_byte * data;
  2750. } rsrc_leaf;
  2751. typedef struct rsrc_entry
  2752. {
  2753. bool is_name;
  2754. union
  2755. {
  2756. unsigned int id;
  2757. struct rsrc_string name;
  2758. } name_id;
  2759. bool is_dir;
  2760. union
  2761. {
  2762. struct rsrc_directory * directory;
  2763. struct rsrc_leaf * leaf;
  2764. } value;
  2765. struct rsrc_entry * next_entry;
  2766. struct rsrc_directory * parent;
  2767. } rsrc_entry;
  2768. static bfd_byte *
  2769. rsrc_parse_directory (bfd *, rsrc_directory *, bfd_byte *,
  2770. bfd_byte *, bfd_byte *, bfd_vma, rsrc_entry *);
  2771. static bfd_byte *
  2772. rsrc_parse_entry (bfd *abfd,
  2773. bool is_name,
  2774. rsrc_entry *entry,
  2775. bfd_byte *datastart,
  2776. bfd_byte * data,
  2777. bfd_byte *dataend,
  2778. bfd_vma rva_bias,
  2779. rsrc_directory *parent)
  2780. {
  2781. unsigned long val, addr, size;
  2782. val = bfd_get_32 (abfd, data);
  2783. entry->parent = parent;
  2784. entry->is_name = is_name;
  2785. if (is_name)
  2786. {
  2787. bfd_byte * address;
  2788. if (HighBitSet (val))
  2789. {
  2790. val = WithoutHighBit (val);
  2791. address = datastart + val;
  2792. }
  2793. else
  2794. {
  2795. address = datastart + val - rva_bias;
  2796. }
  2797. if (address + 3 > dataend)
  2798. return dataend;
  2799. entry->name_id.name.len = bfd_get_16 (abfd, address);
  2800. entry->name_id.name.string = address + 2;
  2801. }
  2802. else
  2803. entry->name_id.id = val;
  2804. val = bfd_get_32 (abfd, data + 4);
  2805. if (HighBitSet (val))
  2806. {
  2807. entry->is_dir = true;
  2808. entry->value.directory = bfd_malloc (sizeof * entry->value.directory);
  2809. if (entry->value.directory == NULL)
  2810. return dataend;
  2811. return rsrc_parse_directory (abfd, entry->value.directory,
  2812. datastart,
  2813. datastart + WithoutHighBit (val),
  2814. dataend, rva_bias, entry);
  2815. }
  2816. entry->is_dir = false;
  2817. entry->value.leaf = bfd_malloc (sizeof * entry->value.leaf);
  2818. if (entry->value.leaf == NULL)
  2819. return dataend;
  2820. data = datastart + val;
  2821. if (data < datastart || data >= dataend)
  2822. return dataend;
  2823. addr = bfd_get_32 (abfd, data);
  2824. size = entry->value.leaf->size = bfd_get_32 (abfd, data + 4);
  2825. entry->value.leaf->codepage = bfd_get_32 (abfd, data + 8);
  2826. /* FIXME: We assume that the reserved field (data + 12) is OK. */
  2827. entry->value.leaf->data = bfd_malloc (size);
  2828. if (entry->value.leaf->data == NULL)
  2829. return dataend;
  2830. memcpy (entry->value.leaf->data, datastart + addr - rva_bias, size);
  2831. return datastart + (addr - rva_bias) + size;
  2832. }
  2833. static bfd_byte *
  2834. rsrc_parse_entries (bfd *abfd,
  2835. rsrc_dir_chain *chain,
  2836. bool is_name,
  2837. bfd_byte *highest_data,
  2838. bfd_byte *datastart,
  2839. bfd_byte *data,
  2840. bfd_byte *dataend,
  2841. bfd_vma rva_bias,
  2842. rsrc_directory *parent)
  2843. {
  2844. unsigned int i;
  2845. rsrc_entry * entry;
  2846. if (chain->num_entries == 0)
  2847. {
  2848. chain->first_entry = chain->last_entry = NULL;
  2849. return highest_data;
  2850. }
  2851. entry = bfd_malloc (sizeof * entry);
  2852. if (entry == NULL)
  2853. return dataend;
  2854. chain->first_entry = entry;
  2855. for (i = chain->num_entries; i--;)
  2856. {
  2857. bfd_byte * entry_end;
  2858. entry_end = rsrc_parse_entry (abfd, is_name, entry, datastart,
  2859. data, dataend, rva_bias, parent);
  2860. data += 8;
  2861. highest_data = max (entry_end, highest_data);
  2862. if (entry_end > dataend)
  2863. return dataend;
  2864. if (i)
  2865. {
  2866. entry->next_entry = bfd_malloc (sizeof * entry);
  2867. entry = entry->next_entry;
  2868. if (entry == NULL)
  2869. return dataend;
  2870. }
  2871. else
  2872. entry->next_entry = NULL;
  2873. }
  2874. chain->last_entry = entry;
  2875. return highest_data;
  2876. }
  2877. static bfd_byte *
  2878. rsrc_parse_directory (bfd * abfd,
  2879. rsrc_directory * table,
  2880. bfd_byte * datastart,
  2881. bfd_byte * data,
  2882. bfd_byte * dataend,
  2883. bfd_vma rva_bias,
  2884. rsrc_entry * entry)
  2885. {
  2886. bfd_byte * highest_data = data;
  2887. if (table == NULL)
  2888. return dataend;
  2889. table->characteristics = bfd_get_32 (abfd, data);
  2890. table->time = bfd_get_32 (abfd, data + 4);
  2891. table->major = bfd_get_16 (abfd, data + 8);
  2892. table->minor = bfd_get_16 (abfd, data + 10);
  2893. table->names.num_entries = bfd_get_16 (abfd, data + 12);
  2894. table->ids.num_entries = bfd_get_16 (abfd, data + 14);
  2895. table->entry = entry;
  2896. data += 16;
  2897. highest_data = rsrc_parse_entries (abfd, & table->names, true, data,
  2898. datastart, data, dataend, rva_bias, table);
  2899. data += table->names.num_entries * 8;
  2900. highest_data = rsrc_parse_entries (abfd, & table->ids, false, highest_data,
  2901. datastart, data, dataend, rva_bias, table);
  2902. data += table->ids.num_entries * 8;
  2903. return max (highest_data, data);
  2904. }
  2905. typedef struct rsrc_write_data
  2906. {
  2907. bfd * abfd;
  2908. bfd_byte * datastart;
  2909. bfd_byte * next_table;
  2910. bfd_byte * next_leaf;
  2911. bfd_byte * next_string;
  2912. bfd_byte * next_data;
  2913. bfd_vma rva_bias;
  2914. } rsrc_write_data;
  2915. static void
  2916. rsrc_write_string (rsrc_write_data * data,
  2917. rsrc_string * string)
  2918. {
  2919. bfd_put_16 (data->abfd, string->len, data->next_string);
  2920. memcpy (data->next_string + 2, string->string, string->len * 2);
  2921. data->next_string += (string->len + 1) * 2;
  2922. }
  2923. static inline unsigned int
  2924. rsrc_compute_rva (rsrc_write_data * data,
  2925. bfd_byte * addr)
  2926. {
  2927. return (addr - data->datastart) + data->rva_bias;
  2928. }
  2929. static void
  2930. rsrc_write_leaf (rsrc_write_data * data,
  2931. rsrc_leaf * leaf)
  2932. {
  2933. bfd_put_32 (data->abfd, rsrc_compute_rva (data, data->next_data),
  2934. data->next_leaf);
  2935. bfd_put_32 (data->abfd, leaf->size, data->next_leaf + 4);
  2936. bfd_put_32 (data->abfd, leaf->codepage, data->next_leaf + 8);
  2937. bfd_put_32 (data->abfd, 0 /*reserved*/, data->next_leaf + 12);
  2938. data->next_leaf += 16;
  2939. memcpy (data->next_data, leaf->data, leaf->size);
  2940. /* An undocumented feature of Windows resources is that each unit
  2941. of raw data is 8-byte aligned... */
  2942. data->next_data += ((leaf->size + 7) & ~7);
  2943. }
  2944. static void rsrc_write_directory (rsrc_write_data *, rsrc_directory *);
  2945. static void
  2946. rsrc_write_entry (rsrc_write_data * data,
  2947. bfd_byte * where,
  2948. rsrc_entry * entry)
  2949. {
  2950. if (entry->is_name)
  2951. {
  2952. bfd_put_32 (data->abfd,
  2953. SetHighBit (data->next_string - data->datastart),
  2954. where);
  2955. rsrc_write_string (data, & entry->name_id.name);
  2956. }
  2957. else
  2958. bfd_put_32 (data->abfd, entry->name_id.id, where);
  2959. if (entry->is_dir)
  2960. {
  2961. bfd_put_32 (data->abfd,
  2962. SetHighBit (data->next_table - data->datastart),
  2963. where + 4);
  2964. rsrc_write_directory (data, entry->value.directory);
  2965. }
  2966. else
  2967. {
  2968. bfd_put_32 (data->abfd, data->next_leaf - data->datastart, where + 4);
  2969. rsrc_write_leaf (data, entry->value.leaf);
  2970. }
  2971. }
  2972. static void
  2973. rsrc_compute_region_sizes (rsrc_directory * dir)
  2974. {
  2975. struct rsrc_entry * entry;
  2976. if (dir == NULL)
  2977. return;
  2978. sizeof_tables_and_entries += 16;
  2979. for (entry = dir->names.first_entry; entry != NULL; entry = entry->next_entry)
  2980. {
  2981. sizeof_tables_and_entries += 8;
  2982. sizeof_strings += (entry->name_id.name.len + 1) * 2;
  2983. if (entry->is_dir)
  2984. rsrc_compute_region_sizes (entry->value.directory);
  2985. else
  2986. sizeof_leaves += 16;
  2987. }
  2988. for (entry = dir->ids.first_entry; entry != NULL; entry = entry->next_entry)
  2989. {
  2990. sizeof_tables_and_entries += 8;
  2991. if (entry->is_dir)
  2992. rsrc_compute_region_sizes (entry->value.directory);
  2993. else
  2994. sizeof_leaves += 16;
  2995. }
  2996. }
  2997. static void
  2998. rsrc_write_directory (rsrc_write_data * data,
  2999. rsrc_directory * dir)
  3000. {
  3001. rsrc_entry * entry;
  3002. unsigned int i;
  3003. bfd_byte * next_entry;
  3004. bfd_byte * nt;
  3005. bfd_put_32 (data->abfd, dir->characteristics, data->next_table);
  3006. bfd_put_32 (data->abfd, 0 /*dir->time*/, data->next_table + 4);
  3007. bfd_put_16 (data->abfd, dir->major, data->next_table + 8);
  3008. bfd_put_16 (data->abfd, dir->minor, data->next_table + 10);
  3009. bfd_put_16 (data->abfd, dir->names.num_entries, data->next_table + 12);
  3010. bfd_put_16 (data->abfd, dir->ids.num_entries, data->next_table + 14);
  3011. /* Compute where the entries and the next table will be placed. */
  3012. next_entry = data->next_table + 16;
  3013. data->next_table = next_entry + (dir->names.num_entries * 8)
  3014. + (dir->ids.num_entries * 8);
  3015. nt = data->next_table;
  3016. /* Write the entries. */
  3017. for (i = dir->names.num_entries, entry = dir->names.first_entry;
  3018. i > 0 && entry != NULL;
  3019. i--, entry = entry->next_entry)
  3020. {
  3021. BFD_ASSERT (entry->is_name);
  3022. rsrc_write_entry (data, next_entry, entry);
  3023. next_entry += 8;
  3024. }
  3025. BFD_ASSERT (i == 0);
  3026. BFD_ASSERT (entry == NULL);
  3027. for (i = dir->ids.num_entries, entry = dir->ids.first_entry;
  3028. i > 0 && entry != NULL;
  3029. i--, entry = entry->next_entry)
  3030. {
  3031. BFD_ASSERT (! entry->is_name);
  3032. rsrc_write_entry (data, next_entry, entry);
  3033. next_entry += 8;
  3034. }
  3035. BFD_ASSERT (i == 0);
  3036. BFD_ASSERT (entry == NULL);
  3037. BFD_ASSERT (nt == next_entry);
  3038. }
  3039. #if ! defined __CYGWIN__ && ! defined __MINGW32__
  3040. /* Return the length (number of units) of the first character in S,
  3041. putting its 'ucs4_t' representation in *PUC. */
  3042. static unsigned int
  3043. u16_mbtouc (wint_t * puc, const unsigned short * s, unsigned int n)
  3044. {
  3045. unsigned short c = * s;
  3046. if (c < 0xd800 || c >= 0xe000)
  3047. {
  3048. *puc = c;
  3049. return 1;
  3050. }
  3051. if (c < 0xdc00)
  3052. {
  3053. if (n >= 2)
  3054. {
  3055. if (s[1] >= 0xdc00 && s[1] < 0xe000)
  3056. {
  3057. *puc = 0x10000 + ((c - 0xd800) << 10) + (s[1] - 0xdc00);
  3058. return 2;
  3059. }
  3060. }
  3061. else
  3062. {
  3063. /* Incomplete multibyte character. */
  3064. *puc = 0xfffd;
  3065. return n;
  3066. }
  3067. }
  3068. /* Invalid multibyte character. */
  3069. *puc = 0xfffd;
  3070. return 1;
  3071. }
  3072. #endif /* not Cygwin/Mingw */
  3073. /* Perform a comparison of two entries. */
  3074. static signed int
  3075. rsrc_cmp (bool is_name, rsrc_entry * a, rsrc_entry * b)
  3076. {
  3077. signed int res;
  3078. bfd_byte * astring;
  3079. unsigned int alen;
  3080. bfd_byte * bstring;
  3081. unsigned int blen;
  3082. if (! is_name)
  3083. return a->name_id.id - b->name_id.id;
  3084. /* We have to perform a case insenstive, unicode string comparison... */
  3085. astring = a->name_id.name.string;
  3086. alen = a->name_id.name.len;
  3087. bstring = b->name_id.name.string;
  3088. blen = b->name_id.name.len;
  3089. #if defined __CYGWIN__ || defined __MINGW32__
  3090. /* Under Windows hosts (both Cygwin and Mingw types),
  3091. unicode == UTF-16 == wchar_t. The case insensitive string comparison
  3092. function however goes by different names in the two environments... */
  3093. #undef rscpcmp
  3094. #ifdef __CYGWIN__
  3095. #define rscpcmp wcsncasecmp
  3096. #endif
  3097. #ifdef __MINGW32__
  3098. #define rscpcmp wcsnicmp
  3099. #endif
  3100. res = rscpcmp ((const wchar_t *) astring, (const wchar_t *) bstring,
  3101. min (alen, blen));
  3102. #else
  3103. {
  3104. unsigned int i;
  3105. res = 0;
  3106. for (i = min (alen, blen); i--; astring += 2, bstring += 2)
  3107. {
  3108. wint_t awc;
  3109. wint_t bwc;
  3110. /* Convert UTF-16 unicode characters into wchar_t characters
  3111. so that we can then perform a case insensitive comparison. */
  3112. unsigned int Alen = u16_mbtouc (& awc, (const unsigned short *) astring, 2);
  3113. unsigned int Blen = u16_mbtouc (& bwc, (const unsigned short *) bstring, 2);
  3114. if (Alen != Blen)
  3115. return Alen - Blen;
  3116. awc = towlower (awc);
  3117. bwc = towlower (bwc);
  3118. res = awc - bwc;
  3119. if (res)
  3120. break;
  3121. }
  3122. }
  3123. #endif
  3124. if (res == 0)
  3125. res = alen - blen;
  3126. return res;
  3127. }
  3128. static void
  3129. rsrc_print_name (char * buffer, rsrc_string string)
  3130. {
  3131. unsigned int i;
  3132. bfd_byte * name = string.string;
  3133. for (i = string.len; i--; name += 2)
  3134. sprintf (buffer + strlen (buffer), "%.1s", name);
  3135. }
  3136. static const char *
  3137. rsrc_resource_name (rsrc_entry *entry, rsrc_directory *dir, char *buffer)
  3138. {
  3139. bool is_string = false;
  3140. buffer[0] = 0;
  3141. if (dir != NULL && dir->entry != NULL && dir->entry->parent != NULL
  3142. && dir->entry->parent->entry != NULL)
  3143. {
  3144. strcpy (buffer, "type: ");
  3145. if (dir->entry->parent->entry->is_name)
  3146. rsrc_print_name (buffer + strlen (buffer),
  3147. dir->entry->parent->entry->name_id.name);
  3148. else
  3149. {
  3150. unsigned int id = dir->entry->parent->entry->name_id.id;
  3151. sprintf (buffer + strlen (buffer), "%x", id);
  3152. switch (id)
  3153. {
  3154. case 1: strcat (buffer, " (CURSOR)"); break;
  3155. case 2: strcat (buffer, " (BITMAP)"); break;
  3156. case 3: strcat (buffer, " (ICON)"); break;
  3157. case 4: strcat (buffer, " (MENU)"); break;
  3158. case 5: strcat (buffer, " (DIALOG)"); break;
  3159. case 6: strcat (buffer, " (STRING)"); is_string = true; break;
  3160. case 7: strcat (buffer, " (FONTDIR)"); break;
  3161. case 8: strcat (buffer, " (FONT)"); break;
  3162. case 9: strcat (buffer, " (ACCELERATOR)"); break;
  3163. case 10: strcat (buffer, " (RCDATA)"); break;
  3164. case 11: strcat (buffer, " (MESSAGETABLE)"); break;
  3165. case 12: strcat (buffer, " (GROUP_CURSOR)"); break;
  3166. case 14: strcat (buffer, " (GROUP_ICON)"); break;
  3167. case 16: strcat (buffer, " (VERSION)"); break;
  3168. case 17: strcat (buffer, " (DLGINCLUDE)"); break;
  3169. case 19: strcat (buffer, " (PLUGPLAY)"); break;
  3170. case 20: strcat (buffer, " (VXD)"); break;
  3171. case 21: strcat (buffer, " (ANICURSOR)"); break;
  3172. case 22: strcat (buffer, " (ANIICON)"); break;
  3173. case 23: strcat (buffer, " (HTML)"); break;
  3174. case 24: strcat (buffer, " (MANIFEST)"); break;
  3175. case 240: strcat (buffer, " (DLGINIT)"); break;
  3176. case 241: strcat (buffer, " (TOOLBAR)"); break;
  3177. }
  3178. }
  3179. }
  3180. if (dir != NULL && dir->entry != NULL)
  3181. {
  3182. strcat (buffer, " name: ");
  3183. if (dir->entry->is_name)
  3184. rsrc_print_name (buffer + strlen (buffer), dir->entry->name_id.name);
  3185. else
  3186. {
  3187. unsigned int id = dir->entry->name_id.id;
  3188. sprintf (buffer + strlen (buffer), "%x", id);
  3189. if (is_string)
  3190. sprintf (buffer + strlen (buffer), " (resource id range: %d - %d)",
  3191. (id - 1) << 4, (id << 4) - 1);
  3192. }
  3193. }
  3194. if (entry != NULL)
  3195. {
  3196. strcat (buffer, " lang: ");
  3197. if (entry->is_name)
  3198. rsrc_print_name (buffer + strlen (buffer), entry->name_id.name);
  3199. else
  3200. sprintf (buffer + strlen (buffer), "%x", entry->name_id.id);
  3201. }
  3202. return buffer;
  3203. }
  3204. /* *sigh* Windows resource strings are special. Only the top 28-bits of
  3205. their ID is stored in the NAME entry. The bottom four bits are used as
  3206. an index into unicode string table that makes up the data of the leaf.
  3207. So identical type-name-lang string resources may not actually be
  3208. identical at all.
  3209. This function is called when we have detected two string resources with
  3210. match top-28-bit IDs. We have to scan the string tables inside the leaves
  3211. and discover if there are any real collisions. If there are then we report
  3212. them and return FALSE. Otherwise we copy any strings from B into A and
  3213. then return TRUE. */
  3214. static bool
  3215. rsrc_merge_string_entries (rsrc_entry * a ATTRIBUTE_UNUSED,
  3216. rsrc_entry * b ATTRIBUTE_UNUSED)
  3217. {
  3218. unsigned int copy_needed = 0;
  3219. unsigned int i;
  3220. bfd_byte * astring;
  3221. bfd_byte * bstring;
  3222. bfd_byte * new_data;
  3223. bfd_byte * nstring;
  3224. /* Step one: Find out what we have to do. */
  3225. BFD_ASSERT (! a->is_dir);
  3226. astring = a->value.leaf->data;
  3227. BFD_ASSERT (! b->is_dir);
  3228. bstring = b->value.leaf->data;
  3229. for (i = 0; i < 16; i++)
  3230. {
  3231. unsigned int alen = astring[0] + (astring[1] << 8);
  3232. unsigned int blen = bstring[0] + (bstring[1] << 8);
  3233. if (alen == 0)
  3234. {
  3235. copy_needed += blen * 2;
  3236. }
  3237. else if (blen == 0)
  3238. ;
  3239. else if (alen != blen)
  3240. /* FIXME: Should we continue the loop in order to report other duplicates ? */
  3241. break;
  3242. /* alen == blen != 0. We might have two identical strings. If so we
  3243. can ignore the second one. There is no need for wchar_t vs UTF-16
  3244. theatrics here - we are only interested in (case sensitive) equality. */
  3245. else if (memcmp (astring + 2, bstring + 2, alen * 2) != 0)
  3246. break;
  3247. astring += (alen + 1) * 2;
  3248. bstring += (blen + 1) * 2;
  3249. }
  3250. if (i != 16)
  3251. {
  3252. if (a->parent != NULL
  3253. && a->parent->entry != NULL
  3254. && !a->parent->entry->is_name)
  3255. _bfd_error_handler (_(".rsrc merge failure: duplicate string resource: %d"),
  3256. ((a->parent->entry->name_id.id - 1) << 4) + i);
  3257. return false;
  3258. }
  3259. if (copy_needed == 0)
  3260. return true;
  3261. /* If we reach here then A and B must both have non-colliding strings.
  3262. (We never get string resources with fully empty string tables).
  3263. We need to allocate an extra COPY_NEEDED bytes in A and then bring
  3264. in B's strings. */
  3265. new_data = bfd_malloc (a->value.leaf->size + copy_needed);
  3266. if (new_data == NULL)
  3267. return false;
  3268. nstring = new_data;
  3269. astring = a->value.leaf->data;
  3270. bstring = b->value.leaf->data;
  3271. for (i = 0; i < 16; i++)
  3272. {
  3273. unsigned int alen = astring[0] + (astring[1] << 8);
  3274. unsigned int blen = bstring[0] + (bstring[1] << 8);
  3275. if (alen != 0)
  3276. {
  3277. memcpy (nstring, astring, (alen + 1) * 2);
  3278. nstring += (alen + 1) * 2;
  3279. }
  3280. else if (blen != 0)
  3281. {
  3282. memcpy (nstring, bstring, (blen + 1) * 2);
  3283. nstring += (blen + 1) * 2;
  3284. }
  3285. else
  3286. {
  3287. * nstring++ = 0;
  3288. * nstring++ = 0;
  3289. }
  3290. astring += (alen + 1) * 2;
  3291. bstring += (blen + 1) * 2;
  3292. }
  3293. BFD_ASSERT (nstring - new_data == (signed) (a->value.leaf->size + copy_needed));
  3294. free (a->value.leaf->data);
  3295. a->value.leaf->data = new_data;
  3296. a->value.leaf->size += copy_needed;
  3297. return true;
  3298. }
  3299. static void rsrc_merge (rsrc_entry *, rsrc_entry *);
  3300. /* Sort the entries in given part of the directory.
  3301. We use an old fashioned bubble sort because we are dealing
  3302. with lists and we want to handle matches specially. */
  3303. static void
  3304. rsrc_sort_entries (rsrc_dir_chain *chain,
  3305. bool is_name,
  3306. rsrc_directory *dir)
  3307. {
  3308. rsrc_entry * entry;
  3309. rsrc_entry * next;
  3310. rsrc_entry ** points_to_entry;
  3311. bool swapped;
  3312. if (chain->num_entries < 2)
  3313. return;
  3314. do
  3315. {
  3316. swapped = false;
  3317. points_to_entry = & chain->first_entry;
  3318. entry = * points_to_entry;
  3319. next = entry->next_entry;
  3320. do
  3321. {
  3322. signed int cmp = rsrc_cmp (is_name, entry, next);
  3323. if (cmp > 0)
  3324. {
  3325. entry->next_entry = next->next_entry;
  3326. next->next_entry = entry;
  3327. * points_to_entry = next;
  3328. points_to_entry = & next->next_entry;
  3329. next = entry->next_entry;
  3330. swapped = true;
  3331. }
  3332. else if (cmp == 0)
  3333. {
  3334. if (entry->is_dir && next->is_dir)
  3335. {
  3336. /* When we encounter identical directory entries we have to
  3337. merge them together. The exception to this rule is for
  3338. resource manifests - there can only be one of these,
  3339. even if they differ in language. Zero-language manifests
  3340. are assumed to be default manifests (provided by the
  3341. Cygwin/MinGW build system) and these can be silently dropped,
  3342. unless that would reduce the number of manifests to zero.
  3343. There should only ever be one non-zero lang manifest -
  3344. if there are more it is an error. A non-zero lang
  3345. manifest takes precedence over a default manifest. */
  3346. if (!entry->is_name
  3347. && entry->name_id.id == 1
  3348. && dir != NULL
  3349. && dir->entry != NULL
  3350. && !dir->entry->is_name
  3351. && dir->entry->name_id.id == 0x18)
  3352. {
  3353. if (next->value.directory->names.num_entries == 0
  3354. && next->value.directory->ids.num_entries == 1
  3355. && !next->value.directory->ids.first_entry->is_name
  3356. && next->value.directory->ids.first_entry->name_id.id == 0)
  3357. /* Fall through so that NEXT is dropped. */
  3358. ;
  3359. else if (entry->value.directory->names.num_entries == 0
  3360. && entry->value.directory->ids.num_entries == 1
  3361. && !entry->value.directory->ids.first_entry->is_name
  3362. && entry->value.directory->ids.first_entry->name_id.id == 0)
  3363. {
  3364. /* Swap ENTRY and NEXT. Then fall through so that the old ENTRY is dropped. */
  3365. entry->next_entry = next->next_entry;
  3366. next->next_entry = entry;
  3367. * points_to_entry = next;
  3368. points_to_entry = & next->next_entry;
  3369. next = entry->next_entry;
  3370. swapped = true;
  3371. }
  3372. else
  3373. {
  3374. _bfd_error_handler (_(".rsrc merge failure: multiple non-default manifests"));
  3375. bfd_set_error (bfd_error_file_truncated);
  3376. return;
  3377. }
  3378. /* Unhook NEXT from the chain. */
  3379. /* FIXME: memory loss here. */
  3380. entry->next_entry = next->next_entry;
  3381. chain->num_entries --;
  3382. if (chain->num_entries < 2)
  3383. return;
  3384. next = next->next_entry;
  3385. }
  3386. else
  3387. rsrc_merge (entry, next);
  3388. }
  3389. else if (entry->is_dir != next->is_dir)
  3390. {
  3391. _bfd_error_handler (_(".rsrc merge failure: a directory matches a leaf"));
  3392. bfd_set_error (bfd_error_file_truncated);
  3393. return;
  3394. }
  3395. else
  3396. {
  3397. /* Otherwise with identical leaves we issue an error
  3398. message - because there should never be duplicates.
  3399. The exception is Type 18/Name 1/Lang 0 which is the
  3400. defaul manifest - this can just be dropped. */
  3401. if (!entry->is_name
  3402. && entry->name_id.id == 0
  3403. && dir != NULL
  3404. && dir->entry != NULL
  3405. && !dir->entry->is_name
  3406. && dir->entry->name_id.id == 1
  3407. && dir->entry->parent != NULL
  3408. && dir->entry->parent->entry != NULL
  3409. && !dir->entry->parent->entry->is_name
  3410. && dir->entry->parent->entry->name_id.id == 0x18 /* RT_MANIFEST */)
  3411. ;
  3412. else if (dir != NULL
  3413. && dir->entry != NULL
  3414. && dir->entry->parent != NULL
  3415. && dir->entry->parent->entry != NULL
  3416. && !dir->entry->parent->entry->is_name
  3417. && dir->entry->parent->entry->name_id.id == 0x6 /* RT_STRING */)
  3418. {
  3419. /* Strings need special handling. */
  3420. if (! rsrc_merge_string_entries (entry, next))
  3421. {
  3422. /* _bfd_error_handler should have been called inside merge_strings. */
  3423. bfd_set_error (bfd_error_file_truncated);
  3424. return;
  3425. }
  3426. }
  3427. else
  3428. {
  3429. if (dir == NULL
  3430. || dir->entry == NULL
  3431. || dir->entry->parent == NULL
  3432. || dir->entry->parent->entry == NULL)
  3433. _bfd_error_handler (_(".rsrc merge failure: duplicate leaf"));
  3434. else
  3435. {
  3436. char buff[256];
  3437. _bfd_error_handler (_(".rsrc merge failure: duplicate leaf: %s"),
  3438. rsrc_resource_name (entry, dir, buff));
  3439. }
  3440. bfd_set_error (bfd_error_file_truncated);
  3441. return;
  3442. }
  3443. }
  3444. /* Unhook NEXT from the chain. */
  3445. entry->next_entry = next->next_entry;
  3446. chain->num_entries --;
  3447. if (chain->num_entries < 2)
  3448. return;
  3449. next = next->next_entry;
  3450. }
  3451. else
  3452. {
  3453. points_to_entry = & entry->next_entry;
  3454. entry = next;
  3455. next = next->next_entry;
  3456. }
  3457. }
  3458. while (next);
  3459. chain->last_entry = entry;
  3460. }
  3461. while (swapped);
  3462. }
  3463. /* Attach B's chain onto A. */
  3464. static void
  3465. rsrc_attach_chain (rsrc_dir_chain * achain, rsrc_dir_chain * bchain)
  3466. {
  3467. if (bchain->num_entries == 0)
  3468. return;
  3469. achain->num_entries += bchain->num_entries;
  3470. if (achain->first_entry == NULL)
  3471. {
  3472. achain->first_entry = bchain->first_entry;
  3473. achain->last_entry = bchain->last_entry;
  3474. }
  3475. else
  3476. {
  3477. achain->last_entry->next_entry = bchain->first_entry;
  3478. achain->last_entry = bchain->last_entry;
  3479. }
  3480. bchain->num_entries = 0;
  3481. bchain->first_entry = bchain->last_entry = NULL;
  3482. }
  3483. static void
  3484. rsrc_merge (struct rsrc_entry * a, struct rsrc_entry * b)
  3485. {
  3486. rsrc_directory * adir;
  3487. rsrc_directory * bdir;
  3488. BFD_ASSERT (a->is_dir);
  3489. BFD_ASSERT (b->is_dir);
  3490. adir = a->value.directory;
  3491. bdir = b->value.directory;
  3492. if (adir->characteristics != bdir->characteristics)
  3493. {
  3494. _bfd_error_handler (_(".rsrc merge failure: dirs with differing characteristics"));
  3495. bfd_set_error (bfd_error_file_truncated);
  3496. return;
  3497. }
  3498. if (adir->major != bdir->major || adir->minor != bdir->minor)
  3499. {
  3500. _bfd_error_handler (_(".rsrc merge failure: differing directory versions"));
  3501. bfd_set_error (bfd_error_file_truncated);
  3502. return;
  3503. }
  3504. /* Attach B's name chain to A. */
  3505. rsrc_attach_chain (& adir->names, & bdir->names);
  3506. /* Attach B's ID chain to A. */
  3507. rsrc_attach_chain (& adir->ids, & bdir->ids);
  3508. /* Now sort A's entries. */
  3509. rsrc_sort_entries (& adir->names, true, adir);
  3510. rsrc_sort_entries (& adir->ids, false, adir);
  3511. }
  3512. /* Check the .rsrc section. If it contains multiple concatenated
  3513. resources then we must merge them properly. Otherwise Windows
  3514. will ignore all but the first set. */
  3515. static void
  3516. rsrc_process_section (bfd * abfd,
  3517. struct coff_final_link_info * pfinfo)
  3518. {
  3519. rsrc_directory new_table;
  3520. bfd_size_type size;
  3521. asection * sec;
  3522. pe_data_type * pe;
  3523. bfd_vma rva_bias;
  3524. bfd_byte * data;
  3525. bfd_byte * datastart;
  3526. bfd_byte * dataend;
  3527. bfd_byte * new_data;
  3528. unsigned int num_resource_sets;
  3529. rsrc_directory * type_tables;
  3530. rsrc_write_data write_data;
  3531. unsigned int indx;
  3532. bfd * input;
  3533. unsigned int num_input_rsrc = 0;
  3534. unsigned int max_num_input_rsrc = 4;
  3535. ptrdiff_t * rsrc_sizes = NULL;
  3536. new_table.names.num_entries = 0;
  3537. new_table.ids.num_entries = 0;
  3538. sec = bfd_get_section_by_name (abfd, ".rsrc");
  3539. if (sec == NULL || (size = sec->rawsize) == 0)
  3540. return;
  3541. pe = pe_data (abfd);
  3542. if (pe == NULL)
  3543. return;
  3544. rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
  3545. data = bfd_malloc (size);
  3546. if (data == NULL)
  3547. return;
  3548. datastart = data;
  3549. if (! bfd_get_section_contents (abfd, sec, data, 0, size))
  3550. goto end;
  3551. /* Step zero: Scan the input bfds looking for .rsrc sections and record
  3552. their lengths. Note - we rely upon the fact that the linker script
  3553. does *not* sort the input .rsrc sections, so that the order in the
  3554. linkinfo list matches the order in the output .rsrc section.
  3555. We need to know the lengths because each input .rsrc section has padding
  3556. at the end of a variable amount. (It does not appear to be based upon
  3557. the section alignment or the file alignment). We need to skip any
  3558. padding bytes when parsing the input .rsrc sections. */
  3559. rsrc_sizes = bfd_malloc (max_num_input_rsrc * sizeof * rsrc_sizes);
  3560. if (rsrc_sizes == NULL)
  3561. goto end;
  3562. for (input = pfinfo->info->input_bfds;
  3563. input != NULL;
  3564. input = input->link.next)
  3565. {
  3566. asection * rsrc_sec = bfd_get_section_by_name (input, ".rsrc");
  3567. /* PR 18372 - skip discarded .rsrc sections. */
  3568. if (rsrc_sec != NULL && !discarded_section (rsrc_sec))
  3569. {
  3570. if (num_input_rsrc == max_num_input_rsrc)
  3571. {
  3572. max_num_input_rsrc += 10;
  3573. rsrc_sizes = bfd_realloc (rsrc_sizes, max_num_input_rsrc
  3574. * sizeof * rsrc_sizes);
  3575. if (rsrc_sizes == NULL)
  3576. goto end;
  3577. }
  3578. BFD_ASSERT (rsrc_sec->size > 0);
  3579. rsrc_sizes [num_input_rsrc ++] = rsrc_sec->size;
  3580. }
  3581. }
  3582. if (num_input_rsrc < 2)
  3583. goto end;
  3584. /* Step one: Walk the section, computing the size of the tables,
  3585. leaves and data and decide if we need to do anything. */
  3586. dataend = data + size;
  3587. num_resource_sets = 0;
  3588. while (data < dataend)
  3589. {
  3590. bfd_byte * p = data;
  3591. data = rsrc_count_directory (abfd, data, data, dataend, rva_bias);
  3592. if (data > dataend)
  3593. {
  3594. /* Corrupted .rsrc section - cannot merge. */
  3595. _bfd_error_handler (_("%pB: .rsrc merge failure: corrupt .rsrc section"),
  3596. abfd);
  3597. bfd_set_error (bfd_error_file_truncated);
  3598. goto end;
  3599. }
  3600. if ((data - p) > rsrc_sizes [num_resource_sets])
  3601. {
  3602. _bfd_error_handler (_("%pB: .rsrc merge failure: unexpected .rsrc size"),
  3603. abfd);
  3604. bfd_set_error (bfd_error_file_truncated);
  3605. goto end;
  3606. }
  3607. /* FIXME: Should we add a check for "data - p" being much smaller
  3608. than rsrc_sizes[num_resource_sets] ? */
  3609. data = p + rsrc_sizes[num_resource_sets];
  3610. rva_bias += data - p;
  3611. ++ num_resource_sets;
  3612. }
  3613. BFD_ASSERT (num_resource_sets == num_input_rsrc);
  3614. /* Step two: Walk the data again, building trees of the resources. */
  3615. data = datastart;
  3616. rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
  3617. type_tables = bfd_malloc (num_resource_sets * sizeof * type_tables);
  3618. if (type_tables == NULL)
  3619. goto end;
  3620. indx = 0;
  3621. while (data < dataend)
  3622. {
  3623. bfd_byte * p = data;
  3624. (void) rsrc_parse_directory (abfd, type_tables + indx, data, data,
  3625. dataend, rva_bias, NULL);
  3626. data = p + rsrc_sizes[indx];
  3627. rva_bias += data - p;
  3628. ++ indx;
  3629. }
  3630. BFD_ASSERT (indx == num_resource_sets);
  3631. /* Step three: Merge the top level tables (there can be only one).
  3632. We must ensure that the merged entries are in ascending order.
  3633. We also thread the top level table entries from the old tree onto
  3634. the new table, so that they can be pulled off later. */
  3635. /* FIXME: Should we verify that all type tables are the same ? */
  3636. new_table.characteristics = type_tables[0].characteristics;
  3637. new_table.time = type_tables[0].time;
  3638. new_table.major = type_tables[0].major;
  3639. new_table.minor = type_tables[0].minor;
  3640. /* Chain the NAME entries onto the table. */
  3641. new_table.names.first_entry = NULL;
  3642. new_table.names.last_entry = NULL;
  3643. for (indx = 0; indx < num_resource_sets; indx++)
  3644. rsrc_attach_chain (& new_table.names, & type_tables[indx].names);
  3645. rsrc_sort_entries (& new_table.names, true, & new_table);
  3646. /* Chain the ID entries onto the table. */
  3647. new_table.ids.first_entry = NULL;
  3648. new_table.ids.last_entry = NULL;
  3649. for (indx = 0; indx < num_resource_sets; indx++)
  3650. rsrc_attach_chain (& new_table.ids, & type_tables[indx].ids);
  3651. rsrc_sort_entries (& new_table.ids, false, & new_table);
  3652. /* Step four: Create new contents for the .rsrc section. */
  3653. /* Step four point one: Compute the size of each region of the .rsrc section.
  3654. We do this now, rather than earlier, as the merging above may have dropped
  3655. some entries. */
  3656. sizeof_leaves = sizeof_strings = sizeof_tables_and_entries = 0;
  3657. rsrc_compute_region_sizes (& new_table);
  3658. /* We increment sizeof_strings to make sure that resource data
  3659. starts on an 8-byte boundary. FIXME: Is this correct ? */
  3660. sizeof_strings = (sizeof_strings + 7) & ~ 7;
  3661. new_data = bfd_zalloc (abfd, size);
  3662. if (new_data == NULL)
  3663. goto end;
  3664. write_data.abfd = abfd;
  3665. write_data.datastart = new_data;
  3666. write_data.next_table = new_data;
  3667. write_data.next_leaf = new_data + sizeof_tables_and_entries;
  3668. write_data.next_string = write_data.next_leaf + sizeof_leaves;
  3669. write_data.next_data = write_data.next_string + sizeof_strings;
  3670. write_data.rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
  3671. rsrc_write_directory (& write_data, & new_table);
  3672. /* Step five: Replace the old contents with the new.
  3673. We don't recompute the size as it's too late here to shrink section.
  3674. See PR ld/20193 for more details. */
  3675. bfd_set_section_contents (pfinfo->output_bfd, sec, new_data, 0, size);
  3676. sec->size = sec->rawsize = size;
  3677. end:
  3678. /* Step six: Free all the memory that we have used. */
  3679. /* FIXME: Free the resource tree, if we have one. */
  3680. free (datastart);
  3681. free (rsrc_sizes);
  3682. }
  3683. /* Handle the .idata section and other things that need symbol table
  3684. access. */
  3685. bool
  3686. _bfd_XXi_final_link_postscript (bfd * abfd, struct coff_final_link_info *pfinfo)
  3687. {
  3688. struct coff_link_hash_entry *h1;
  3689. struct bfd_link_info *info = pfinfo->info;
  3690. bool result = true;
  3691. /* There are a few fields that need to be filled in now while we
  3692. have symbol table access.
  3693. The .idata subsections aren't directly available as sections, but
  3694. they are in the symbol table, so get them from there. */
  3695. /* The import directory. This is the address of .idata$2, with size
  3696. of .idata$2 + .idata$3. */
  3697. h1 = coff_link_hash_lookup (coff_hash_table (info),
  3698. ".idata$2", false, false, true);
  3699. if (h1 != NULL)
  3700. {
  3701. /* PR ld/2729: We cannot rely upon all the output sections having been
  3702. created properly, so check before referencing them. Issue a warning
  3703. message for any sections tht could not be found. */
  3704. if ((h1->root.type == bfd_link_hash_defined
  3705. || h1->root.type == bfd_link_hash_defweak)
  3706. && h1->root.u.def.section != NULL
  3707. && h1->root.u.def.section->output_section != NULL)
  3708. pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress =
  3709. (h1->root.u.def.value
  3710. + h1->root.u.def.section->output_section->vma
  3711. + h1->root.u.def.section->output_offset);
  3712. else
  3713. {
  3714. _bfd_error_handler
  3715. (_("%pB: unable to fill in DataDictionary[1] because .idata$2 is missing"),
  3716. abfd);
  3717. result = false;
  3718. }
  3719. h1 = coff_link_hash_lookup (coff_hash_table (info),
  3720. ".idata$4", false, false, true);
  3721. if (h1 != NULL
  3722. && (h1->root.type == bfd_link_hash_defined
  3723. || h1->root.type == bfd_link_hash_defweak)
  3724. && h1->root.u.def.section != NULL
  3725. && h1->root.u.def.section->output_section != NULL)
  3726. pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].Size =
  3727. ((h1->root.u.def.value
  3728. + h1->root.u.def.section->output_section->vma
  3729. + h1->root.u.def.section->output_offset)
  3730. - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress);
  3731. else
  3732. {
  3733. _bfd_error_handler
  3734. (_("%pB: unable to fill in DataDictionary[1] because .idata$4 is missing"),
  3735. abfd);
  3736. result = false;
  3737. }
  3738. /* The import address table. This is the size/address of
  3739. .idata$5. */
  3740. h1 = coff_link_hash_lookup (coff_hash_table (info),
  3741. ".idata$5", false, false, true);
  3742. if (h1 != NULL
  3743. && (h1->root.type == bfd_link_hash_defined
  3744. || h1->root.type == bfd_link_hash_defweak)
  3745. && h1->root.u.def.section != NULL
  3746. && h1->root.u.def.section->output_section != NULL)
  3747. pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress =
  3748. (h1->root.u.def.value
  3749. + h1->root.u.def.section->output_section->vma
  3750. + h1->root.u.def.section->output_offset);
  3751. else
  3752. {
  3753. _bfd_error_handler
  3754. (_("%pB: unable to fill in DataDictionary[12] because .idata$5 is missing"),
  3755. abfd);
  3756. result = false;
  3757. }
  3758. h1 = coff_link_hash_lookup (coff_hash_table (info),
  3759. ".idata$6", false, false, true);
  3760. if (h1 != NULL
  3761. && (h1->root.type == bfd_link_hash_defined
  3762. || h1->root.type == bfd_link_hash_defweak)
  3763. && h1->root.u.def.section != NULL
  3764. && h1->root.u.def.section->output_section != NULL)
  3765. pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size =
  3766. ((h1->root.u.def.value
  3767. + h1->root.u.def.section->output_section->vma
  3768. + h1->root.u.def.section->output_offset)
  3769. - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress);
  3770. else
  3771. {
  3772. _bfd_error_handler
  3773. (_("%pB: unable to fill in DataDictionary[PE_IMPORT_ADDRESS_TABLE (12)] because .idata$6 is missing"),
  3774. abfd);
  3775. result = false;
  3776. }
  3777. }
  3778. else
  3779. {
  3780. h1 = coff_link_hash_lookup (coff_hash_table (info),
  3781. "__IAT_start__", false, false, true);
  3782. if (h1 != NULL
  3783. && (h1->root.type == bfd_link_hash_defined
  3784. || h1->root.type == bfd_link_hash_defweak)
  3785. && h1->root.u.def.section != NULL
  3786. && h1->root.u.def.section->output_section != NULL)
  3787. {
  3788. bfd_vma iat_va;
  3789. iat_va =
  3790. (h1->root.u.def.value
  3791. + h1->root.u.def.section->output_section->vma
  3792. + h1->root.u.def.section->output_offset);
  3793. h1 = coff_link_hash_lookup (coff_hash_table (info),
  3794. "__IAT_end__", false, false, true);
  3795. if (h1 != NULL
  3796. && (h1->root.type == bfd_link_hash_defined
  3797. || h1->root.type == bfd_link_hash_defweak)
  3798. && h1->root.u.def.section != NULL
  3799. && h1->root.u.def.section->output_section != NULL)
  3800. {
  3801. pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size =
  3802. ((h1->root.u.def.value
  3803. + h1->root.u.def.section->output_section->vma
  3804. + h1->root.u.def.section->output_offset)
  3805. - iat_va);
  3806. if (pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size != 0)
  3807. pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress =
  3808. iat_va - pe_data (abfd)->pe_opthdr.ImageBase;
  3809. }
  3810. else
  3811. {
  3812. _bfd_error_handler
  3813. (_("%pB: unable to fill in DataDictionary[PE_IMPORT_ADDRESS_TABLE(12)]"
  3814. " because .idata$6 is missing"), abfd);
  3815. result = false;
  3816. }
  3817. }
  3818. }
  3819. h1 = coff_link_hash_lookup (coff_hash_table (info),
  3820. (bfd_get_symbol_leading_char (abfd) != 0
  3821. ? "__tls_used" : "_tls_used"),
  3822. false, false, true);
  3823. if (h1 != NULL)
  3824. {
  3825. if ((h1->root.type == bfd_link_hash_defined
  3826. || h1->root.type == bfd_link_hash_defweak)
  3827. && h1->root.u.def.section != NULL
  3828. && h1->root.u.def.section->output_section != NULL)
  3829. pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].VirtualAddress =
  3830. (h1->root.u.def.value
  3831. + h1->root.u.def.section->output_section->vma
  3832. + h1->root.u.def.section->output_offset
  3833. - pe_data (abfd)->pe_opthdr.ImageBase);
  3834. else
  3835. {
  3836. _bfd_error_handler
  3837. (_("%pB: unable to fill in DataDictionary[9] because __tls_used is missing"),
  3838. abfd);
  3839. result = false;
  3840. }
  3841. /* According to PECOFF sepcifications by Microsoft version 8.2
  3842. the TLS data directory consists of 4 pointers, followed
  3843. by two 4-byte integer. This implies that the total size
  3844. is different for 32-bit and 64-bit executables. */
  3845. #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
  3846. pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x18;
  3847. #else
  3848. pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x28;
  3849. #endif
  3850. }
  3851. /* If there is a .pdata section and we have linked pdata finally, we
  3852. need to sort the entries ascending. */
  3853. #if !defined(COFF_WITH_pep) && (defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64))
  3854. {
  3855. asection *sec = bfd_get_section_by_name (abfd, ".pdata");
  3856. if (sec)
  3857. {
  3858. bfd_size_type x = sec->rawsize;
  3859. bfd_byte *tmp_data = NULL;
  3860. if (x)
  3861. tmp_data = bfd_malloc (x);
  3862. if (tmp_data != NULL)
  3863. {
  3864. if (bfd_get_section_contents (abfd, sec, tmp_data, 0, x))
  3865. {
  3866. qsort (tmp_data,
  3867. (size_t) (x / 12),
  3868. 12, sort_x64_pdata);
  3869. bfd_set_section_contents (pfinfo->output_bfd, sec,
  3870. tmp_data, 0, x);
  3871. }
  3872. free (tmp_data);
  3873. }
  3874. else
  3875. result = false;
  3876. }
  3877. }
  3878. #endif
  3879. rsrc_process_section (abfd, pfinfo);
  3880. /* If we couldn't find idata$2, we either have an excessively
  3881. trivial program or are in DEEP trouble; we have to assume trivial
  3882. program.... */
  3883. return result;
  3884. }