pe-dll.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453
  1. /* Routines to help build PEI-format DLLs (Win32 etc)
  2. Copyright (C) 1998-2015 Free Software Foundation, Inc.
  3. Written by DJ Delorie <dj@cygnus.com>
  4. This file is part of the GNU Binutils.
  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. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "bfdlink.h"
  20. #include "libiberty.h"
  21. #include "filenames.h"
  22. #include "safe-ctype.h"
  23. #include <time.h>
  24. #include "ld.h"
  25. #include "ldexp.h"
  26. #include "ldlang.h"
  27. #include "ldwrite.h"
  28. #include "ldmisc.h"
  29. #include <ldgram.h>
  30. #include "ldmain.h"
  31. #include "ldfile.h"
  32. #include "ldemul.h"
  33. #include "coff/internal.h"
  34. #include "../bfd/libcoff.h"
  35. #include "deffile.h"
  36. #ifdef pe_use_x86_64
  37. #define PE_IDATA4_SIZE 8
  38. #define PE_IDATA5_SIZE 8
  39. #include "pep-dll.h"
  40. #undef AOUTSZ
  41. #define AOUTSZ PEPAOUTSZ
  42. #define PEAOUTHDR PEPAOUTHDR
  43. #else
  44. #include "pe-dll.h"
  45. #endif
  46. #ifndef PE_IDATA4_SIZE
  47. #define PE_IDATA4_SIZE 4
  48. #endif
  49. #ifndef PE_IDATA5_SIZE
  50. #define PE_IDATA5_SIZE 4
  51. #endif
  52. /* This file turns a regular Windows PE image into a DLL. Because of
  53. the complexity of this operation, it has been broken down into a
  54. number of separate modules which are all called by the main function
  55. at the end of this file. This function is not re-entrant and is
  56. normally only called once, so static variables are used to reduce
  57. the number of parameters and return values required.
  58. See also: ld/emultempl/pe.em and ld/emultempl/pep.em. */
  59. /* Auto-import feature by Paul Sokolovsky
  60. Quick facts:
  61. 1. With this feature on, DLL clients can import variables from DLL
  62. without any concern from their side (for example, without any source
  63. code modifications).
  64. 2. This is done completely in bounds of the PE specification (to be fair,
  65. there's a place where it pokes nose out of, but in practice it works).
  66. So, resulting module can be used with any other PE compiler/linker.
  67. 3. Auto-import is fully compatible with standard import method and they
  68. can be mixed together.
  69. 4. Overheads: space: 8 bytes per imported symbol, plus 20 for each
  70. reference to it; load time: negligible; virtual/physical memory: should be
  71. less than effect of DLL relocation, and I sincerely hope it doesn't affect
  72. DLL sharability (too much).
  73. Idea
  74. The obvious and only way to get rid of dllimport insanity is to make client
  75. access variable directly in the DLL, bypassing extra dereference. I.e.,
  76. whenever client contains something like
  77. mov dll_var,%eax,
  78. address of dll_var in the command should be relocated to point into loaded
  79. DLL. The aim is to make OS loader do so, and than make ld help with that.
  80. Import section of PE made following way: there's a vector of structures
  81. each describing imports from particular DLL. Each such structure points
  82. to two other parallel vectors: one holding imported names, and one which
  83. will hold address of corresponding imported name. So, the solution is
  84. de-vectorize these structures, making import locations be sparse and
  85. pointing directly into code. Before continuing, it is worth a note that,
  86. while authors strives to make PE act ELF-like, there're some other people
  87. make ELF act PE-like: elfvector, ;-) .
  88. Implementation
  89. For each reference of data symbol to be imported from DLL (to set of which
  90. belong symbols with name <sym>, if __imp_<sym> is found in implib), the
  91. import fixup entry is generated. That entry is of type
  92. IMAGE_IMPORT_DESCRIPTOR and stored in .idata$2 subsection. Each
  93. fixup entry contains pointer to symbol's address within .text section
  94. (marked with __fuN_<sym> symbol, where N is integer), pointer to DLL name
  95. (so, DLL name is referenced by multiple entries), and pointer to symbol
  96. name thunk. Symbol name thunk is singleton vector (__nm_th_<symbol>)
  97. pointing to IMAGE_IMPORT_BY_NAME structure (__nm_<symbol>) directly
  98. containing imported name. Here comes that "on the edge" problem mentioned
  99. above: PE specification rambles that name vector (OriginalFirstThunk)
  100. should run in parallel with addresses vector (FirstThunk), i.e. that they
  101. should have same number of elements and terminated with zero. We violate
  102. this, since FirstThunk points directly into machine code. But in practice,
  103. OS loader implemented the sane way: it goes thru OriginalFirstThunk and
  104. puts addresses to FirstThunk, not something else. It once again should be
  105. noted that dll and symbol name structures are reused across fixup entries
  106. and should be there anyway to support standard import stuff, so sustained
  107. overhead is 20 bytes per reference. Other question is whether having several
  108. IMAGE_IMPORT_DESCRIPTORS for the same DLL is possible. Answer is yes, it is
  109. done even by native compiler/linker (libth32's functions are in fact reside
  110. in windows9x kernel32.dll, so if you use it, you have two
  111. IMAGE_IMPORT_DESCRIPTORS for kernel32.dll). Yet other question is whether
  112. referencing the same PE structures several times is valid. The answer is why
  113. not, prohibiting that (detecting violation) would require more work on
  114. behalf of loader than not doing it.
  115. See also: ld/emultempl/pe.em and ld/emultempl/pep.em. */
  116. static void add_bfd_to_link (bfd *, const char *, struct bfd_link_info *);
  117. /* For emultempl/pe.em. */
  118. def_file * pe_def_file = 0;
  119. int pe_dll_export_everything = 0;
  120. int pe_dll_exclude_all_symbols = 0;
  121. int pe_dll_do_default_excludes = 1;
  122. int pe_dll_kill_ats = 0;
  123. int pe_dll_stdcall_aliases = 0;
  124. int pe_dll_warn_dup_exports = 0;
  125. int pe_dll_compat_implib = 0;
  126. int pe_dll_extra_pe_debug = 0;
  127. int pe_use_nul_prefixed_import_tables = 0;
  128. int pe_use_coff_long_section_names = -1;
  129. int pe_leading_underscore = -1;
  130. /* Static variables and types. */
  131. static bfd_vma image_base;
  132. static bfd *filler_bfd;
  133. static struct bfd_section *edata_s, *reloc_s;
  134. static unsigned char *edata_d, *reloc_d;
  135. static size_t edata_sz, reloc_sz;
  136. static int runtime_pseudo_relocs_created = 0;
  137. static int runtime_pseudp_reloc_v2_init = 0;
  138. typedef struct
  139. {
  140. const char *name;
  141. int len;
  142. }
  143. autofilter_entry_type;
  144. typedef struct
  145. {
  146. const char *target_name;
  147. const char *object_target;
  148. unsigned int imagebase_reloc;
  149. int pe_arch;
  150. int bfd_arch;
  151. bfd_boolean underscored;
  152. const autofilter_entry_type* autofilter_symbollist;
  153. }
  154. pe_details_type;
  155. static const autofilter_entry_type autofilter_symbollist_generic[] =
  156. {
  157. { STRING_COMMA_LEN ("_NULL_IMPORT_DESCRIPTOR") },
  158. /* Entry point symbols. */
  159. { STRING_COMMA_LEN ("DllMain") },
  160. { STRING_COMMA_LEN ("DllMainCRTStartup") },
  161. { STRING_COMMA_LEN ("_DllMainCRTStartup") },
  162. /* Runtime pseudo-reloc. */
  163. { STRING_COMMA_LEN ("_pei386_runtime_relocator") },
  164. { STRING_COMMA_LEN ("do_pseudo_reloc") },
  165. { NULL, 0 }
  166. };
  167. static const autofilter_entry_type autofilter_symbollist_i386[] =
  168. {
  169. { STRING_COMMA_LEN ("_NULL_IMPORT_DESCRIPTOR") },
  170. /* Entry point symbols, and entry hooks. */
  171. { STRING_COMMA_LEN ("cygwin_crt0") },
  172. #ifdef pe_use_x86_64
  173. { STRING_COMMA_LEN ("DllMain") },
  174. { STRING_COMMA_LEN ("DllEntryPoint") },
  175. { STRING_COMMA_LEN ("DllMainCRTStartup") },
  176. { STRING_COMMA_LEN ("_cygwin_dll_entry") },
  177. { STRING_COMMA_LEN ("_cygwin_crt0_common") },
  178. { STRING_COMMA_LEN ("_cygwin_noncygwin_dll_entry") },
  179. #else
  180. { STRING_COMMA_LEN ("DllMain@12") },
  181. { STRING_COMMA_LEN ("DllEntryPoint@0") },
  182. { STRING_COMMA_LEN ("DllMainCRTStartup@12") },
  183. { STRING_COMMA_LEN ("_cygwin_dll_entry@12") },
  184. { STRING_COMMA_LEN ("_cygwin_crt0_common@8") },
  185. { STRING_COMMA_LEN ("_cygwin_noncygwin_dll_entry@12") },
  186. { STRING_COMMA_LEN ("cygwin_attach_dll") },
  187. #endif
  188. { STRING_COMMA_LEN ("cygwin_premain0") },
  189. { STRING_COMMA_LEN ("cygwin_premain1") },
  190. { STRING_COMMA_LEN ("cygwin_premain2") },
  191. { STRING_COMMA_LEN ("cygwin_premain3") },
  192. /* Runtime pseudo-reloc. */
  193. { STRING_COMMA_LEN ("_pei386_runtime_relocator") },
  194. { STRING_COMMA_LEN ("do_pseudo_reloc") },
  195. /* Global vars that should not be exported. */
  196. { STRING_COMMA_LEN ("impure_ptr") },
  197. { STRING_COMMA_LEN ("_impure_ptr") },
  198. { STRING_COMMA_LEN ("_fmode") },
  199. { STRING_COMMA_LEN ("environ") },
  200. { STRING_COMMA_LEN ("__dso_handle") },
  201. { NULL, 0 }
  202. };
  203. #define PE_ARCH_i386 1
  204. #define PE_ARCH_sh 2
  205. #define PE_ARCH_mips 3
  206. #define PE_ARCH_arm 4
  207. #define PE_ARCH_arm_epoc 5
  208. #define PE_ARCH_arm_wince 6
  209. /* Don't make it constant as underscore mode gets possibly overriden
  210. by target or -(no-)leading-underscore option. */
  211. static pe_details_type pe_detail_list[] =
  212. {
  213. {
  214. #ifdef pe_use_x86_64
  215. "pei-x86-64",
  216. "pe-x86-64",
  217. 3 /* R_IMAGEBASE */,
  218. #else
  219. "pei-i386",
  220. "pe-i386",
  221. 7 /* R_IMAGEBASE */,
  222. #endif
  223. PE_ARCH_i386,
  224. bfd_arch_i386,
  225. #ifdef pe_use_x86_64
  226. FALSE,
  227. #else
  228. TRUE,
  229. #endif
  230. autofilter_symbollist_i386
  231. },
  232. {
  233. "pei-shl",
  234. "pe-shl",
  235. 16 /* R_SH_IMAGEBASE */,
  236. PE_ARCH_sh,
  237. bfd_arch_sh,
  238. TRUE,
  239. autofilter_symbollist_generic
  240. },
  241. {
  242. "pei-mips",
  243. "pe-mips",
  244. 34 /* MIPS_R_RVA */,
  245. PE_ARCH_mips,
  246. bfd_arch_mips,
  247. FALSE,
  248. autofilter_symbollist_generic
  249. },
  250. {
  251. "pei-arm-little",
  252. "pe-arm-little",
  253. 11 /* ARM_RVA32 */,
  254. PE_ARCH_arm,
  255. bfd_arch_arm,
  256. TRUE,
  257. autofilter_symbollist_generic
  258. },
  259. {
  260. "epoc-pei-arm-little",
  261. "epoc-pe-arm-little",
  262. 11 /* ARM_RVA32 */,
  263. PE_ARCH_arm_epoc,
  264. bfd_arch_arm,
  265. FALSE,
  266. autofilter_symbollist_generic
  267. },
  268. {
  269. "pei-arm-wince-little",
  270. "pe-arm-wince-little",
  271. 2, /* ARM_RVA32 on Windows CE, see bfd/coff-arm.c. */
  272. PE_ARCH_arm_wince,
  273. bfd_arch_arm,
  274. FALSE,
  275. autofilter_symbollist_generic
  276. },
  277. { NULL, NULL, 0, 0, 0, FALSE, NULL }
  278. };
  279. static const pe_details_type *pe_details;
  280. /* Do not specify library suffix explicitly, to allow for dllized versions. */
  281. static const autofilter_entry_type autofilter_liblist[] =
  282. {
  283. { STRING_COMMA_LEN ("libcegcc") },
  284. { STRING_COMMA_LEN ("libcygwin") },
  285. { STRING_COMMA_LEN ("libgcc") },
  286. { STRING_COMMA_LEN ("libgcc_s") },
  287. { STRING_COMMA_LEN ("libstdc++") },
  288. { STRING_COMMA_LEN ("libmingw32") },
  289. { STRING_COMMA_LEN ("libmingwex") },
  290. { STRING_COMMA_LEN ("libg2c") },
  291. { STRING_COMMA_LEN ("libsupc++") },
  292. { STRING_COMMA_LEN ("libobjc") },
  293. { STRING_COMMA_LEN ("libgcj") },
  294. { NULL, 0 }
  295. };
  296. /* Regardless of the suffix issue mentioned above, we must ensure that
  297. we do not falsely match on a leading substring, such as when libtool
  298. builds libstdc++ as a DLL using libsupc++convenience.a as an intermediate.
  299. This routine ensures that the leading part of the name matches and that
  300. it is followed by only an optional version suffix and a file extension,
  301. returning zero if so or -1 if not. */
  302. static int libnamencmp (const char *libname, const autofilter_entry_type *afptr)
  303. {
  304. if (filename_ncmp (libname, afptr->name, afptr->len))
  305. return -1;
  306. libname += afptr->len;
  307. /* Be liberal in interpreting what counts as a version suffix; we
  308. accept anything that has a dash to separate it from the name and
  309. begins with a digit. */
  310. if (libname[0] == '-')
  311. {
  312. if (!ISDIGIT (*++libname))
  313. return -1;
  314. /* Ensure the filename has an extension. */
  315. while (*++libname != '.')
  316. if (!*libname)
  317. return -1;
  318. }
  319. else if (libname[0] != '.')
  320. return -1;
  321. return 0;
  322. }
  323. static const autofilter_entry_type autofilter_objlist[] =
  324. {
  325. { STRING_COMMA_LEN ("crt0.o") },
  326. { STRING_COMMA_LEN ("crt1.o") },
  327. { STRING_COMMA_LEN ("crt2.o") },
  328. { STRING_COMMA_LEN ("dllcrt1.o") },
  329. { STRING_COMMA_LEN ("dllcrt2.o") },
  330. { STRING_COMMA_LEN ("gcrt0.o") },
  331. { STRING_COMMA_LEN ("gcrt1.o") },
  332. { STRING_COMMA_LEN ("gcrt2.o") },
  333. { STRING_COMMA_LEN ("crtbegin.o") },
  334. { STRING_COMMA_LEN ("crtend.o") },
  335. { NULL, 0 }
  336. };
  337. static const autofilter_entry_type autofilter_symbolprefixlist[] =
  338. {
  339. /* _imp_ is treated specially, as it is always underscored. */
  340. /* { STRING_COMMA_LEN ("_imp_") }, */
  341. /* Don't export some c++ symbols. */
  342. { STRING_COMMA_LEN ("__rtti_") },
  343. { STRING_COMMA_LEN ("__builtin_") },
  344. /* Don't re-export auto-imported symbols. */
  345. { STRING_COMMA_LEN ("__nm_") },
  346. /* Don't export symbols specifying internal DLL layout. */
  347. { STRING_COMMA_LEN ("_head_") },
  348. { STRING_COMMA_LEN ("_IMPORT_DESCRIPTOR_") },
  349. /* Don't export section labels or artificial symbols
  350. (eg ".weak.foo". */
  351. { STRING_COMMA_LEN (".") },
  352. { NULL, 0 }
  353. };
  354. static const autofilter_entry_type autofilter_symbolsuffixlist[] =
  355. {
  356. { STRING_COMMA_LEN ("_iname") },
  357. { STRING_COMMA_LEN ("_NULL_THUNK_DATA") },
  358. { NULL, 0 }
  359. };
  360. #define U(str) (pe_details->underscored ? "_" str : str)
  361. void
  362. pe_dll_id_target (const char *target)
  363. {
  364. int i;
  365. for (i = 0; pe_detail_list[i].target_name; i++)
  366. if (strcmp (pe_detail_list[i].target_name, target) == 0
  367. || strcmp (pe_detail_list[i].object_target, target) == 0)
  368. {
  369. int u = pe_leading_underscore; /* Underscoring mode. -1 for use default. */
  370. if (u == -1)
  371. bfd_get_target_info (target, NULL, NULL, &u, NULL);
  372. if (u == -1)
  373. abort ();
  374. pe_detail_list[i].underscored = (u != 0 ? TRUE : FALSE);
  375. pe_details = pe_detail_list + i;
  376. pe_leading_underscore = (u != 0 ? 1 : 0);
  377. return;
  378. }
  379. einfo (_("%XUnsupported PEI architecture: %s\n"), target);
  380. exit (1);
  381. }
  382. /* Helper functions for qsort. Relocs must be sorted so that we can write
  383. them out by pages. */
  384. typedef struct
  385. {
  386. bfd_vma vma;
  387. char type;
  388. short extra;
  389. }
  390. reloc_data_type;
  391. static int
  392. reloc_sort (const void *va, const void *vb)
  393. {
  394. bfd_vma a = ((const reloc_data_type *) va)->vma;
  395. bfd_vma b = ((const reloc_data_type *) vb)->vma;
  396. return (a > b) ? 1 : ((a < b) ? -1 : 0);
  397. }
  398. static int
  399. pe_export_sort (const void *va, const void *vb)
  400. {
  401. const def_file_export *a = va;
  402. const def_file_export *b = vb;
  403. char *an = a->name;
  404. char *bn = b->name;
  405. if (a->its_name)
  406. an = a->its_name;
  407. if (b->its_name)
  408. bn = b->its_name;
  409. return strcmp (an, bn);
  410. }
  411. /* Read and process the .DEF file. */
  412. /* These correspond to the entries in pe_def_file->exports[]. I use
  413. exported_symbol_sections[i] to tag whether or not the symbol was
  414. defined, since we can't export symbols we don't have. */
  415. static bfd_vma *exported_symbol_offsets;
  416. static struct bfd_section **exported_symbol_sections;
  417. static int export_table_size;
  418. static int count_exported;
  419. static int count_exported_byname;
  420. static int count_with_ordinals;
  421. static const char *dll_name;
  422. static int min_ordinal, max_ordinal;
  423. static int *exported_symbols;
  424. typedef struct exclude_list_struct
  425. {
  426. char *string;
  427. struct exclude_list_struct *next;
  428. exclude_type type;
  429. }
  430. exclude_list_struct;
  431. static struct exclude_list_struct *excludes = 0;
  432. void
  433. pe_dll_add_excludes (const char *new_excludes, const exclude_type type)
  434. {
  435. char *local_copy;
  436. char *exclude_string;
  437. local_copy = xstrdup (new_excludes);
  438. exclude_string = strtok (local_copy, ",:");
  439. for (; exclude_string; exclude_string = strtok (NULL, ",:"))
  440. {
  441. struct exclude_list_struct *new_exclude;
  442. new_exclude = xmalloc (sizeof (struct exclude_list_struct));
  443. new_exclude->string = xmalloc (strlen (exclude_string) + 1);
  444. strcpy (new_exclude->string, exclude_string);
  445. new_exclude->type = type;
  446. new_exclude->next = excludes;
  447. excludes = new_exclude;
  448. }
  449. free (local_copy);
  450. }
  451. static bfd_boolean
  452. is_import (const char* n)
  453. {
  454. return (CONST_STRNEQ (n, "__imp_"));
  455. }
  456. /* abfd is a bfd containing n (or NULL)
  457. It can be used for contextual checks. */
  458. static int
  459. auto_export (bfd *abfd, def_file *d, const char *n)
  460. {
  461. def_file_export key;
  462. struct exclude_list_struct *ex;
  463. const autofilter_entry_type *afptr;
  464. const char * libname = NULL;
  465. if (abfd && abfd->my_archive)
  466. libname = lbasename (abfd->my_archive->filename);
  467. key.name = key.its_name = (char *) n;
  468. /* Return false if n is in the d->exports table. */
  469. if (bsearch (&key, d->exports, d->num_exports,
  470. sizeof (pe_def_file->exports[0]), pe_export_sort))
  471. return 0;
  472. if (pe_dll_do_default_excludes)
  473. {
  474. const char * p;
  475. int len;
  476. if (pe_dll_extra_pe_debug)
  477. printf ("considering exporting: %s, abfd=%p, abfd->my_arc=%p\n",
  478. n, abfd, abfd->my_archive);
  479. /* First of all, make context checks:
  480. Don't export anything from standard libs. */
  481. if (libname)
  482. {
  483. afptr = autofilter_liblist;
  484. while (afptr->name)
  485. {
  486. if (libnamencmp (libname, afptr) == 0 )
  487. return 0;
  488. afptr++;
  489. }
  490. }
  491. /* Next, exclude symbols from certain startup objects. */
  492. if (abfd && (p = lbasename (abfd->filename)))
  493. {
  494. afptr = autofilter_objlist;
  495. while (afptr->name)
  496. {
  497. if (strcmp (p, afptr->name) == 0)
  498. return 0;
  499. afptr++;
  500. }
  501. }
  502. /* Don't try to blindly exclude all symbols
  503. that begin with '__'; this was tried and
  504. it is too restrictive. Instead we have
  505. a target specific list to use: */
  506. afptr = pe_details->autofilter_symbollist;
  507. while (afptr->name)
  508. {
  509. if (strcmp (n, afptr->name) == 0)
  510. return 0;
  511. afptr++;
  512. }
  513. /* Next, exclude symbols starting with ... */
  514. afptr = autofilter_symbolprefixlist;
  515. while (afptr->name)
  516. {
  517. if (strncmp (n, afptr->name, afptr->len) == 0)
  518. return 0;
  519. afptr++;
  520. }
  521. /* Finally, exclude symbols ending with ... */
  522. len = strlen (n);
  523. afptr = autofilter_symbolsuffixlist;
  524. while (afptr->name)
  525. {
  526. if ((len >= afptr->len)
  527. /* Add 1 to insure match with trailing '\0'. */
  528. && strncmp (n + len - afptr->len, afptr->name,
  529. afptr->len + 1) == 0)
  530. return 0;
  531. afptr++;
  532. }
  533. }
  534. for (ex = excludes; ex; ex = ex->next)
  535. {
  536. if (ex->type == EXCLUDELIBS)
  537. {
  538. if (libname
  539. && ((filename_cmp (libname, ex->string) == 0)
  540. || (strcasecmp ("ALL", ex->string) == 0)))
  541. return 0;
  542. }
  543. else if (ex->type == EXCLUDEFORIMPLIB)
  544. {
  545. if (filename_cmp (abfd->filename, ex->string) == 0)
  546. return 0;
  547. }
  548. else if (strcmp (n, ex->string) == 0)
  549. return 0;
  550. }
  551. return 1;
  552. }
  553. static void
  554. process_def_file_and_drectve (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
  555. {
  556. int i, j;
  557. struct bfd_link_hash_entry *blhe;
  558. bfd *b;
  559. struct bfd_section *s;
  560. def_file_export *e = 0;
  561. bfd_boolean resort_needed;
  562. if (!pe_def_file)
  563. pe_def_file = def_file_empty ();
  564. /* First, run around to all the objects looking for the .drectve
  565. sections, and push those into the def file too. */
  566. for (b = info->input_bfds; b; b = b->link.next)
  567. {
  568. s = bfd_get_section_by_name (b, ".drectve");
  569. if (s)
  570. {
  571. long size = s->size;
  572. char *buf = xmalloc (size);
  573. bfd_get_section_contents (b, s, buf, 0, size);
  574. def_file_add_directive (pe_def_file, buf, size);
  575. free (buf);
  576. }
  577. }
  578. /* Process aligned common symbol information from the
  579. .drectve sections now; common symbol allocation is
  580. done before final link, so it will be too late to
  581. process them in process_embedded_commands() called
  582. from _bfd_coff_link_input_bfd(). */
  583. if (pe_def_file->aligncomms)
  584. {
  585. def_file_aligncomm *ac = pe_def_file->aligncomms;
  586. while (ac)
  587. {
  588. struct coff_link_hash_entry *sym_hash;
  589. sym_hash = coff_link_hash_lookup (coff_hash_table (info),
  590. ac->symbol_name, FALSE, FALSE, FALSE);
  591. if (sym_hash && sym_hash->root.type == bfd_link_hash_common
  592. && sym_hash->root.u.c.p->alignment_power < (unsigned) ac->alignment)
  593. {
  594. sym_hash->root.u.c.p->alignment_power = (unsigned) ac->alignment;
  595. }
  596. ac = ac->next;
  597. }
  598. }
  599. /* If we are building an executable and there is nothing
  600. to export, we do not build an export table at all. */
  601. if (bfd_link_executable (info) && pe_def_file->num_exports == 0
  602. && (!pe_dll_export_everything || pe_dll_exclude_all_symbols))
  603. return;
  604. /* Now, maybe export everything else the default way. */
  605. if ((pe_dll_export_everything || pe_def_file->num_exports == 0)
  606. && !pe_dll_exclude_all_symbols)
  607. {
  608. for (b = info->input_bfds; b; b = b->link.next)
  609. {
  610. asymbol **symbols;
  611. int nsyms;
  612. if (!bfd_generic_link_read_symbols (b))
  613. {
  614. einfo (_("%B%F: could not read symbols: %E\n"), b);
  615. return;
  616. }
  617. symbols = bfd_get_outsymbols (b);
  618. nsyms = bfd_get_symcount (b);
  619. for (j = 0; j < nsyms; j++)
  620. {
  621. /* We should export symbols which are either global or not
  622. anything at all. (.bss data is the latter)
  623. We should not export undefined symbols. */
  624. bfd_boolean would_export
  625. = (symbols[j]->section != bfd_und_section_ptr
  626. && ((symbols[j]->flags & BSF_GLOBAL)
  627. || (symbols[j]->flags == 0)));
  628. if (link_info.version_info && would_export)
  629. would_export
  630. = !bfd_hide_sym_by_version (link_info.version_info,
  631. symbols[j]->name);
  632. if (would_export)
  633. {
  634. const char *sn = symbols[j]->name;
  635. /* We should not re-export imported stuff. */
  636. {
  637. char *name;
  638. if (is_import (sn))
  639. continue;
  640. name = xmalloc (strlen ("__imp_") + strlen (sn) + 1);
  641. sprintf (name, "%s%s", "__imp_", sn);
  642. blhe = bfd_link_hash_lookup (info->hash, name,
  643. FALSE, FALSE, FALSE);
  644. free (name);
  645. if (blhe && blhe->type == bfd_link_hash_defined)
  646. continue;
  647. }
  648. if (pe_details->underscored && *sn == '_')
  649. sn++;
  650. if (auto_export (b, pe_def_file, sn))
  651. {
  652. int is_dup = 0;
  653. def_file_export *p;
  654. p = def_file_add_export (pe_def_file, sn, 0, -1,
  655. NULL, &is_dup);
  656. /* Fill data flag properly, from dlltool.c. */
  657. if (!is_dup)
  658. p->flag_data = !(symbols[j]->flags & BSF_FUNCTION);
  659. }
  660. }
  661. }
  662. }
  663. }
  664. #undef NE
  665. #define NE pe_def_file->num_exports
  666. /* Don't create an empty export table. */
  667. if (NE == 0)
  668. return;
  669. resort_needed = FALSE;
  670. /* Canonicalize the export list. */
  671. if (pe_dll_kill_ats)
  672. {
  673. for (i = 0; i < NE; i++)
  674. {
  675. /* Check for fastcall/stdcall-decoration, but ignore
  676. C++ mangled names. */
  677. if (pe_def_file->exports[i].name[0] != '?'
  678. && strchr (pe_def_file->exports[i].name, '@'))
  679. {
  680. /* This will preserve internal_name, which may have been
  681. pointing to the same memory as name, or might not
  682. have. */
  683. int lead_at = (*pe_def_file->exports[i].name == '@');
  684. char *tmp = xstrdup (pe_def_file->exports[i].name + lead_at);
  685. char *tmp_at = strrchr (tmp, '@');
  686. if (tmp_at)
  687. *tmp_at = 0;
  688. else
  689. einfo (_("%XCannot export %s: invalid export name\n"),
  690. pe_def_file->exports[i].name);
  691. pe_def_file->exports[i].name = tmp;
  692. resort_needed = TRUE;
  693. }
  694. }
  695. }
  696. /* Re-sort the exports table as we have possibly changed the order
  697. by removing leading @. */
  698. if (resort_needed)
  699. qsort (pe_def_file->exports, NE, sizeof (pe_def_file->exports[0]),
  700. pe_export_sort);
  701. if (pe_dll_stdcall_aliases)
  702. {
  703. for (i = 0; i < NE; i++)
  704. {
  705. if (is_import (pe_def_file->exports[i].name))
  706. continue;
  707. if (strchr (pe_def_file->exports[i].name, '@'))
  708. {
  709. int is_dup = 1;
  710. int lead_at = (*pe_def_file->exports[i].name == '@');
  711. char *tmp = xstrdup (pe_def_file->exports[i].name + lead_at);
  712. *(strchr (tmp, '@')) = 0;
  713. if (auto_export (NULL, pe_def_file, tmp))
  714. def_file_add_export (pe_def_file, tmp,
  715. pe_def_file->exports[i].internal_name,
  716. -1, NULL, &is_dup);
  717. if (is_dup)
  718. free (tmp);
  719. }
  720. }
  721. }
  722. /* Convenience, but watch out for it changing. */
  723. e = pe_def_file->exports;
  724. for (i = 0, j = 0; i < NE; i++)
  725. {
  726. if (i > 0 && strcmp (e[i].name, e[i - 1].name) == 0)
  727. {
  728. /* This is a duplicate. */
  729. if (e[j - 1].ordinal != -1
  730. && e[i].ordinal != -1
  731. && e[j - 1].ordinal != e[i].ordinal)
  732. {
  733. if (pe_dll_warn_dup_exports)
  734. /* xgettext:c-format */
  735. einfo (_("%XError, duplicate EXPORT with ordinals: %s (%d vs %d)\n"),
  736. e[j - 1].name, e[j - 1].ordinal, e[i].ordinal);
  737. }
  738. else
  739. {
  740. if (pe_dll_warn_dup_exports)
  741. /* xgettext:c-format */
  742. einfo (_("Warning, duplicate EXPORT: %s\n"),
  743. e[j - 1].name);
  744. }
  745. if (e[i].ordinal != -1)
  746. e[j - 1].ordinal = e[i].ordinal;
  747. e[j - 1].flag_private |= e[i].flag_private;
  748. e[j - 1].flag_constant |= e[i].flag_constant;
  749. e[j - 1].flag_noname |= e[i].flag_noname;
  750. e[j - 1].flag_data |= e[i].flag_data;
  751. if (e[i].name)
  752. free (e[i].name);
  753. if (e[i].internal_name)
  754. free (e[i].internal_name);
  755. if (e[i].its_name)
  756. free (e[i].its_name);
  757. }
  758. else
  759. {
  760. if (i != j)
  761. e[j] = e[i];
  762. j++;
  763. }
  764. }
  765. pe_def_file->num_exports = j; /* == NE */
  766. exported_symbol_offsets = xmalloc (NE * sizeof (bfd_vma));
  767. exported_symbol_sections = xmalloc (NE * sizeof (struct bfd_section *));
  768. memset (exported_symbol_sections, 0, NE * sizeof (struct bfd_section *));
  769. max_ordinal = 0;
  770. min_ordinal = 65536;
  771. count_exported = 0;
  772. count_exported_byname = 0;
  773. count_with_ordinals = 0;
  774. for (i = 0; i < NE; i++)
  775. {
  776. char *name;
  777. name = xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
  778. if (pe_details->underscored
  779. && (*pe_def_file->exports[i].internal_name != '@'))
  780. {
  781. *name = '_';
  782. strcpy (name + 1, pe_def_file->exports[i].internal_name);
  783. }
  784. else
  785. strcpy (name, pe_def_file->exports[i].internal_name);
  786. blhe = bfd_link_hash_lookup (info->hash,
  787. name,
  788. FALSE, FALSE, TRUE);
  789. if (blhe
  790. && (blhe->type == bfd_link_hash_defined
  791. || (blhe->type == bfd_link_hash_common)))
  792. {
  793. count_exported++;
  794. if (!pe_def_file->exports[i].flag_noname)
  795. count_exported_byname++;
  796. /* Only fill in the sections. The actual offsets are computed
  797. in fill_exported_offsets() after common symbols are laid
  798. out. */
  799. if (blhe->type == bfd_link_hash_defined)
  800. exported_symbol_sections[i] = blhe->u.def.section;
  801. else
  802. exported_symbol_sections[i] = blhe->u.c.p->section;
  803. if (pe_def_file->exports[i].ordinal != -1)
  804. {
  805. if (max_ordinal < pe_def_file->exports[i].ordinal)
  806. max_ordinal = pe_def_file->exports[i].ordinal;
  807. if (min_ordinal > pe_def_file->exports[i].ordinal)
  808. min_ordinal = pe_def_file->exports[i].ordinal;
  809. count_with_ordinals++;
  810. }
  811. }
  812. /* Check for forward exports. These are indicated in DEF files by an
  813. export directive of the form NAME1 = MODULE-NAME.EXTERNAL-NAME
  814. but we must take care not to be fooled when the user wants to export
  815. a symbol that actually really has a dot in it, so we only check
  816. for them here, after real defined symbols have already been matched. */
  817. else if (strchr (pe_def_file->exports[i].internal_name, '.'))
  818. {
  819. count_exported++;
  820. if (!pe_def_file->exports[i].flag_noname)
  821. count_exported_byname++;
  822. pe_def_file->exports[i].flag_forward = 1;
  823. if (pe_def_file->exports[i].ordinal != -1)
  824. {
  825. if (max_ordinal < pe_def_file->exports[i].ordinal)
  826. max_ordinal = pe_def_file->exports[i].ordinal;
  827. if (min_ordinal > pe_def_file->exports[i].ordinal)
  828. min_ordinal = pe_def_file->exports[i].ordinal;
  829. count_with_ordinals++;
  830. }
  831. }
  832. else if (blhe && blhe->type == bfd_link_hash_undefined)
  833. {
  834. /* xgettext:c-format */
  835. einfo (_("%XCannot export %s: symbol not defined\n"),
  836. pe_def_file->exports[i].internal_name);
  837. }
  838. else if (blhe)
  839. {
  840. /* xgettext:c-format */
  841. einfo (_("%XCannot export %s: symbol wrong type (%d vs %d)\n"),
  842. pe_def_file->exports[i].internal_name,
  843. blhe->type, bfd_link_hash_defined);
  844. }
  845. else
  846. {
  847. /* xgettext:c-format */
  848. einfo (_("%XCannot export %s: symbol not found\n"),
  849. pe_def_file->exports[i].internal_name);
  850. }
  851. free (name);
  852. }
  853. }
  854. /* Build the bfd that will contain .edata and .reloc sections. */
  855. static void
  856. build_filler_bfd (int include_edata)
  857. {
  858. lang_input_statement_type *filler_file;
  859. filler_file = lang_add_input_file ("dll stuff",
  860. lang_input_file_is_fake_enum,
  861. NULL);
  862. filler_file->the_bfd = filler_bfd = bfd_create ("dll stuff",
  863. link_info.output_bfd);
  864. if (filler_bfd == NULL
  865. || !bfd_set_arch_mach (filler_bfd,
  866. bfd_get_arch (link_info.output_bfd),
  867. bfd_get_mach (link_info.output_bfd)))
  868. {
  869. einfo ("%X%P: can not create BFD: %E\n");
  870. return;
  871. }
  872. if (include_edata)
  873. {
  874. edata_s = bfd_make_section_old_way (filler_bfd, ".edata");
  875. if (edata_s == NULL
  876. || !bfd_set_section_flags (filler_bfd, edata_s,
  877. (SEC_HAS_CONTENTS
  878. | SEC_ALLOC
  879. | SEC_LOAD
  880. | SEC_KEEP
  881. | SEC_IN_MEMORY)))
  882. {
  883. einfo ("%X%P: can not create .edata section: %E\n");
  884. return;
  885. }
  886. bfd_set_section_size (filler_bfd, edata_s, edata_sz);
  887. }
  888. reloc_s = bfd_make_section_old_way (filler_bfd, ".reloc");
  889. if (reloc_s == NULL
  890. || !bfd_set_section_flags (filler_bfd, reloc_s,
  891. (SEC_HAS_CONTENTS
  892. | SEC_ALLOC
  893. | SEC_LOAD
  894. | SEC_KEEP
  895. | SEC_IN_MEMORY)))
  896. {
  897. einfo ("%X%P: can not create .reloc section: %E\n");
  898. return;
  899. }
  900. bfd_set_section_size (filler_bfd, reloc_s, 0);
  901. ldlang_add_file (filler_file);
  902. }
  903. /* Gather all the exported symbols and build the .edata section. */
  904. static void
  905. generate_edata (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
  906. {
  907. int i, next_ordinal;
  908. int name_table_size = 0;
  909. const char *dlnp;
  910. /* First, we need to know how many exported symbols there are,
  911. and what the range of ordinals is. */
  912. if (pe_def_file->name)
  913. dll_name = pe_def_file->name;
  914. else
  915. {
  916. dll_name = abfd->filename;
  917. for (dlnp = dll_name; *dlnp; dlnp++)
  918. if (*dlnp == '\\' || *dlnp == '/' || *dlnp == ':')
  919. dll_name = dlnp + 1;
  920. }
  921. if (count_with_ordinals && max_ordinal > count_exported)
  922. {
  923. if (min_ordinal > max_ordinal - count_exported + 1)
  924. min_ordinal = max_ordinal - count_exported + 1;
  925. }
  926. else
  927. {
  928. min_ordinal = 1;
  929. max_ordinal = count_exported;
  930. }
  931. export_table_size = max_ordinal - min_ordinal + 1;
  932. exported_symbols = xmalloc (export_table_size * sizeof (int));
  933. for (i = 0; i < export_table_size; i++)
  934. exported_symbols[i] = -1;
  935. /* Now we need to assign ordinals to those that don't have them. */
  936. for (i = 0; i < NE; i++)
  937. {
  938. if (exported_symbol_sections[i] ||
  939. pe_def_file->exports[i].flag_forward)
  940. {
  941. if (pe_def_file->exports[i].ordinal != -1)
  942. {
  943. int ei = pe_def_file->exports[i].ordinal - min_ordinal;
  944. int pi = exported_symbols[ei];
  945. if (pi != -1)
  946. {
  947. /* xgettext:c-format */
  948. einfo (_("%XError, ordinal used twice: %d (%s vs %s)\n"),
  949. pe_def_file->exports[i].ordinal,
  950. pe_def_file->exports[i].name,
  951. pe_def_file->exports[pi].name);
  952. }
  953. exported_symbols[ei] = i;
  954. }
  955. if (pe_def_file->exports[i].its_name)
  956. name_table_size += strlen (pe_def_file->exports[i].its_name) + 1;
  957. else
  958. name_table_size += strlen (pe_def_file->exports[i].name) + 1;
  959. }
  960. /* Reserve space for the forward name. */
  961. if (pe_def_file->exports[i].flag_forward)
  962. {
  963. name_table_size += strlen (pe_def_file->exports[i].internal_name) + 1;
  964. }
  965. }
  966. next_ordinal = min_ordinal;
  967. for (i = 0; i < NE; i++)
  968. if ((exported_symbol_sections[i] ||
  969. pe_def_file->exports[i].flag_forward) &&
  970. pe_def_file->exports[i].ordinal == -1)
  971. {
  972. while (exported_symbols[next_ordinal - min_ordinal] != -1)
  973. next_ordinal++;
  974. exported_symbols[next_ordinal - min_ordinal] = i;
  975. pe_def_file->exports[i].ordinal = next_ordinal;
  976. }
  977. /* OK, now we can allocate some memory. */
  978. edata_sz = (40 /* directory */
  979. + 4 * export_table_size /* addresses */
  980. + 4 * count_exported_byname /* name ptrs */
  981. + 2 * count_exported_byname /* ordinals */
  982. + name_table_size + strlen (dll_name) + 1);
  983. }
  984. /* Fill the exported symbol offsets. The preliminary work has already
  985. been done in process_def_file_and_drectve(). */
  986. static void
  987. fill_exported_offsets (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
  988. {
  989. int i;
  990. struct bfd_link_hash_entry *blhe;
  991. for (i = 0; i < pe_def_file->num_exports; i++)
  992. {
  993. char *name;
  994. name = xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
  995. if (pe_details->underscored
  996. && *pe_def_file->exports[i].internal_name != '@')
  997. {
  998. *name = '_';
  999. strcpy (name + 1, pe_def_file->exports[i].internal_name);
  1000. }
  1001. else
  1002. strcpy (name, pe_def_file->exports[i].internal_name);
  1003. blhe = bfd_link_hash_lookup (info->hash,
  1004. name,
  1005. FALSE, FALSE, TRUE);
  1006. if (blhe && blhe->type == bfd_link_hash_defined)
  1007. exported_symbol_offsets[i] = blhe->u.def.value;
  1008. free (name);
  1009. }
  1010. }
  1011. static void
  1012. fill_edata (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
  1013. {
  1014. int s, hint;
  1015. unsigned char *edirectory;
  1016. unsigned char *eaddresses;
  1017. unsigned char *enameptrs;
  1018. unsigned char *eordinals;
  1019. char *enamestr;
  1020. edata_d = xmalloc (edata_sz);
  1021. /* Note use of array pointer math here. */
  1022. edirectory = edata_d;
  1023. eaddresses = edirectory + 40;
  1024. enameptrs = eaddresses + 4 * export_table_size;
  1025. eordinals = enameptrs + 4 * count_exported_byname;
  1026. enamestr = (char *) eordinals + 2 * count_exported_byname;
  1027. #define ERVA(ptr) (((unsigned char *)(ptr) - edata_d) \
  1028. + edata_s->output_section->vma - image_base)
  1029. memset (edata_d, 0, edata_sz);
  1030. if (pe_data (abfd)->insert_timestamp)
  1031. H_PUT_32 (abfd, time (0), edata_d + 4);
  1032. if (pe_def_file->version_major != -1)
  1033. {
  1034. bfd_put_16 (abfd, pe_def_file->version_major, edata_d + 8);
  1035. bfd_put_16 (abfd, pe_def_file->version_minor, edata_d + 10);
  1036. }
  1037. bfd_put_32 (abfd, ERVA (enamestr), edata_d + 12);
  1038. strcpy (enamestr, dll_name);
  1039. enamestr += strlen (enamestr) + 1;
  1040. bfd_put_32 (abfd, min_ordinal, edata_d + 16);
  1041. bfd_put_32 (abfd, export_table_size, edata_d + 20);
  1042. bfd_put_32 (abfd, count_exported_byname, edata_d + 24);
  1043. bfd_put_32 (abfd, ERVA (eaddresses), edata_d + 28);
  1044. bfd_put_32 (abfd, ERVA (enameptrs), edata_d + 32);
  1045. bfd_put_32 (abfd, ERVA (eordinals), edata_d + 36);
  1046. fill_exported_offsets (abfd, info);
  1047. /* Ok, now for the filling in part.
  1048. Scan alphabetically - ie the ordering in the exports[] table,
  1049. rather than by ordinal - the ordering in the exported_symbol[]
  1050. table. See dlltool.c and:
  1051. http://sources.redhat.com/ml/binutils/2003-04/msg00379.html
  1052. for more information. */
  1053. hint = 0;
  1054. for (s = 0; s < NE; s++)
  1055. {
  1056. struct bfd_section *ssec = exported_symbol_sections[s];
  1057. if (pe_def_file->exports[s].ordinal != -1 &&
  1058. (pe_def_file->exports[s].flag_forward || ssec != NULL))
  1059. {
  1060. int ord = pe_def_file->exports[s].ordinal;
  1061. if (pe_def_file->exports[s].flag_forward)
  1062. {
  1063. bfd_put_32 (abfd, ERVA (enamestr),
  1064. eaddresses + 4 * (ord - min_ordinal));
  1065. strcpy (enamestr, pe_def_file->exports[s].internal_name);
  1066. enamestr += strlen (pe_def_file->exports[s].internal_name) + 1;
  1067. }
  1068. else
  1069. {
  1070. bfd_vma srva = (exported_symbol_offsets[s]
  1071. + ssec->output_section->vma
  1072. + ssec->output_offset);
  1073. bfd_put_32 (abfd, srva - image_base,
  1074. eaddresses + 4 * (ord - min_ordinal));
  1075. }
  1076. if (!pe_def_file->exports[s].flag_noname)
  1077. {
  1078. char *ename = pe_def_file->exports[s].name;
  1079. if (pe_def_file->exports[s].its_name)
  1080. ename = pe_def_file->exports[s].its_name;
  1081. bfd_put_32 (abfd, ERVA (enamestr), enameptrs);
  1082. enameptrs += 4;
  1083. strcpy (enamestr, ename);
  1084. enamestr += strlen (enamestr) + 1;
  1085. bfd_put_16 (abfd, ord - min_ordinal, eordinals);
  1086. eordinals += 2;
  1087. pe_def_file->exports[s].hint = hint++;
  1088. }
  1089. }
  1090. }
  1091. }
  1092. static struct bfd_section *current_sec;
  1093. void
  1094. pe_walk_relocs_of_symbol (struct bfd_link_info *info,
  1095. const char *name,
  1096. int (*cb) (arelent *, asection *))
  1097. {
  1098. bfd *b;
  1099. asection *s;
  1100. for (b = info->input_bfds; b; b = b->link.next)
  1101. {
  1102. asymbol **symbols;
  1103. if (!bfd_generic_link_read_symbols (b))
  1104. {
  1105. einfo (_("%B%F: could not read symbols: %E\n"), b);
  1106. return;
  1107. }
  1108. symbols = bfd_get_outsymbols (b);
  1109. for (s = b->sections; s; s = s->next)
  1110. {
  1111. arelent **relocs;
  1112. int relsize, nrelocs, i;
  1113. int flags = bfd_get_section_flags (b, s);
  1114. /* Skip discarded linkonce sections. */
  1115. if (flags & SEC_LINK_ONCE
  1116. && s->output_section == bfd_abs_section_ptr)
  1117. continue;
  1118. current_sec = s;
  1119. relsize = bfd_get_reloc_upper_bound (b, s);
  1120. relocs = xmalloc (relsize);
  1121. nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols);
  1122. for (i = 0; i < nrelocs; i++)
  1123. {
  1124. struct bfd_symbol *sym = *relocs[i]->sym_ptr_ptr;
  1125. if (!strcmp (name, sym->name))
  1126. cb (relocs[i], s);
  1127. }
  1128. free (relocs);
  1129. /* Warning: the allocated symbols are remembered in BFD and reused
  1130. later, so don't free them! */
  1131. /* free (symbols); */
  1132. }
  1133. }
  1134. }
  1135. /* Gather all the relocations and build the .reloc section. */
  1136. static void
  1137. generate_reloc (bfd *abfd, struct bfd_link_info *info)
  1138. {
  1139. /* For .reloc stuff. */
  1140. reloc_data_type *reloc_data;
  1141. int total_relocs = 0;
  1142. int i;
  1143. bfd_vma sec_page = (bfd_vma) -1;
  1144. bfd_vma page_ptr, page_count;
  1145. int bi;
  1146. bfd *b;
  1147. struct bfd_section *s;
  1148. total_relocs = 0;
  1149. for (b = info->input_bfds; b; b = b->link.next)
  1150. for (s = b->sections; s; s = s->next)
  1151. total_relocs += s->reloc_count;
  1152. reloc_data = xmalloc (total_relocs * sizeof (reloc_data_type));
  1153. total_relocs = 0;
  1154. bi = 0;
  1155. for (bi = 0, b = info->input_bfds; b; bi++, b = b->link.next)
  1156. {
  1157. arelent **relocs;
  1158. int relsize, nrelocs;
  1159. for (s = b->sections; s; s = s->next)
  1160. {
  1161. bfd_vma sec_vma = s->output_section->vma + s->output_offset;
  1162. asymbol **symbols;
  1163. /* If it's not loaded, we don't need to relocate it this way. */
  1164. if (!(s->output_section->flags & SEC_LOAD))
  1165. continue;
  1166. /* I don't know why there would be a reloc for these, but I've
  1167. seen it happen - DJ */
  1168. if (s->output_section == bfd_abs_section_ptr)
  1169. continue;
  1170. if (s->output_section->vma == 0)
  1171. {
  1172. /* Huh? Shouldn't happen, but punt if it does. */
  1173. einfo ("DJ: zero vma section reloc detected: `%s' #%d f=%d\n",
  1174. s->output_section->name, s->output_section->index,
  1175. s->output_section->flags);
  1176. continue;
  1177. }
  1178. if (!bfd_generic_link_read_symbols (b))
  1179. {
  1180. einfo (_("%B%F: could not read symbols: %E\n"), b);
  1181. return;
  1182. }
  1183. symbols = bfd_get_outsymbols (b);
  1184. relsize = bfd_get_reloc_upper_bound (b, s);
  1185. relocs = xmalloc (relsize);
  1186. nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols);
  1187. for (i = 0; i < nrelocs; i++)
  1188. {
  1189. if (pe_dll_extra_pe_debug)
  1190. {
  1191. struct bfd_symbol *sym = *relocs[i]->sym_ptr_ptr;
  1192. printf ("rel: %s\n", sym->name);
  1193. }
  1194. if (!relocs[i]->howto->pc_relative
  1195. && relocs[i]->howto->type != pe_details->imagebase_reloc)
  1196. {
  1197. struct bfd_symbol *sym = *relocs[i]->sym_ptr_ptr;
  1198. /* Don't create relocs for undefined weak symbols. */
  1199. if (sym->flags == BSF_WEAK)
  1200. {
  1201. struct bfd_link_hash_entry *blhe
  1202. = bfd_wrapped_link_hash_lookup (abfd, info, sym->name,
  1203. FALSE, FALSE, FALSE);
  1204. if (blhe && blhe->type == bfd_link_hash_undefweak)
  1205. {
  1206. /* Check aux sym and see if it is defined or not. */
  1207. struct coff_link_hash_entry *h, *h2;
  1208. h = (struct coff_link_hash_entry *)blhe;
  1209. if (h->symbol_class != C_NT_WEAK || h->numaux != 1)
  1210. continue;
  1211. h2 = h->auxbfd->tdata.coff_obj_data->sym_hashes
  1212. [h->aux->x_sym.x_tagndx.l];
  1213. /* We don't want a base reloc if the aux sym is not
  1214. found, undefined, or if it is the constant ABS
  1215. zero default value. (We broaden that slightly by
  1216. not testing the value, just the section; there's
  1217. no reason we'd want a reference to any absolute
  1218. address to get relocated during rebasing). */
  1219. if (!h2 || h2->root.type == bfd_link_hash_undefined
  1220. || h2->root.u.def.section == bfd_abs_section_ptr)
  1221. continue;
  1222. }
  1223. else if (!blhe || blhe->type != bfd_link_hash_defined)
  1224. continue;
  1225. }
  1226. /* Nor for Dwarf FDE references to discarded sections. */
  1227. else if (bfd_is_abs_section (sym->section->output_section))
  1228. {
  1229. /* We only ignore relocs from .eh_frame sections, as
  1230. they are discarded by the final link rather than
  1231. resolved against the kept section. */
  1232. if (!strcmp (s->name, ".eh_frame"))
  1233. continue;
  1234. }
  1235. reloc_data[total_relocs].vma = sec_vma + relocs[i]->address;
  1236. #define BITS_AND_SHIFT(bits, shift) (bits * 1000 | shift)
  1237. switch BITS_AND_SHIFT (relocs[i]->howto->bitsize,
  1238. relocs[i]->howto->rightshift)
  1239. {
  1240. #ifdef pe_use_x86_64
  1241. case BITS_AND_SHIFT (64, 0):
  1242. reloc_data[total_relocs].type = 10;
  1243. total_relocs++;
  1244. break;
  1245. #endif
  1246. case BITS_AND_SHIFT (32, 0):
  1247. reloc_data[total_relocs].type = 3;
  1248. total_relocs++;
  1249. break;
  1250. case BITS_AND_SHIFT (16, 0):
  1251. reloc_data[total_relocs].type = 2;
  1252. total_relocs++;
  1253. break;
  1254. case BITS_AND_SHIFT (16, 16):
  1255. reloc_data[total_relocs].type = 4;
  1256. /* FIXME: we can't know the symbol's right value
  1257. yet, but we probably can safely assume that
  1258. CE will relocate us in 64k blocks, so leaving
  1259. it zero is safe. */
  1260. reloc_data[total_relocs].extra = 0;
  1261. total_relocs++;
  1262. break;
  1263. case BITS_AND_SHIFT (26, 2):
  1264. reloc_data[total_relocs].type = 5;
  1265. total_relocs++;
  1266. break;
  1267. case BITS_AND_SHIFT (24, 2):
  1268. /* FIXME: 0 is ARM_26D, it is defined in bfd/coff-arm.c
  1269. Those ARM_xxx definitions should go in proper
  1270. header someday. */
  1271. if (relocs[i]->howto->type == 0
  1272. /* Older GNU linkers used 5 instead of 0 for this reloc. */
  1273. || relocs[i]->howto->type == 5)
  1274. /* This is an ARM_26D reloc, which is an ARM_26 reloc
  1275. that has already been fully processed during a
  1276. previous link stage, so ignore it here. */
  1277. break;
  1278. /* Fall through. */
  1279. default:
  1280. /* xgettext:c-format */
  1281. einfo (_("%XError: %d-bit reloc in dll\n"),
  1282. relocs[i]->howto->bitsize);
  1283. break;
  1284. }
  1285. }
  1286. }
  1287. free (relocs);
  1288. /* Warning: the allocated symbols are remembered in BFD and
  1289. reused later, so don't free them! */
  1290. }
  1291. }
  1292. /* At this point, we have total_relocs relocation addresses in
  1293. reloc_addresses, which are all suitable for the .reloc section.
  1294. We must now create the new sections. */
  1295. qsort (reloc_data, total_relocs, sizeof (*reloc_data), reloc_sort);
  1296. for (i = 0; i < total_relocs; i++)
  1297. {
  1298. bfd_vma this_page = (reloc_data[i].vma >> 12);
  1299. if (this_page != sec_page)
  1300. {
  1301. reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align. */
  1302. reloc_sz += 8;
  1303. sec_page = this_page;
  1304. }
  1305. reloc_sz += 2;
  1306. if (reloc_data[i].type == 4)
  1307. reloc_sz += 2;
  1308. }
  1309. reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align. */
  1310. reloc_d = xmalloc (reloc_sz);
  1311. sec_page = (bfd_vma) -1;
  1312. reloc_sz = 0;
  1313. page_ptr = (bfd_vma) -1;
  1314. page_count = 0;
  1315. for (i = 0; i < total_relocs; i++)
  1316. {
  1317. bfd_vma rva = reloc_data[i].vma - image_base;
  1318. bfd_vma this_page = (rva & ~0xfff);
  1319. if (this_page != sec_page)
  1320. {
  1321. while (reloc_sz & 3)
  1322. reloc_d[reloc_sz++] = 0;
  1323. if (page_ptr != (bfd_vma) -1)
  1324. bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
  1325. bfd_put_32 (abfd, this_page, reloc_d + reloc_sz);
  1326. page_ptr = reloc_sz;
  1327. reloc_sz += 8;
  1328. sec_page = this_page;
  1329. page_count = 0;
  1330. }
  1331. bfd_put_16 (abfd, (rva & 0xfff) + (reloc_data[i].type << 12),
  1332. reloc_d + reloc_sz);
  1333. reloc_sz += 2;
  1334. if (reloc_data[i].type == 4)
  1335. {
  1336. bfd_put_16 (abfd, reloc_data[i].extra, reloc_d + reloc_sz);
  1337. reloc_sz += 2;
  1338. }
  1339. page_count++;
  1340. }
  1341. while (reloc_sz & 3)
  1342. reloc_d[reloc_sz++] = 0;
  1343. if (page_ptr != (bfd_vma) -1)
  1344. bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
  1345. while (reloc_sz < reloc_s->size)
  1346. reloc_d[reloc_sz++] = 0;
  1347. }
  1348. /* Given the exiting def_file structure, print out a .DEF file that
  1349. corresponds to it. */
  1350. static void
  1351. quoteput (char *s, FILE *f, int needs_quotes)
  1352. {
  1353. char *cp;
  1354. for (cp = s; *cp; cp++)
  1355. if (*cp == '\''
  1356. || *cp == '"'
  1357. || *cp == '\\'
  1358. || ISSPACE (*cp)
  1359. || *cp == ','
  1360. || *cp == ';')
  1361. needs_quotes = 1;
  1362. if (needs_quotes)
  1363. {
  1364. putc ('"', f);
  1365. while (*s)
  1366. {
  1367. if (*s == '"' || *s == '\\')
  1368. putc ('\\', f);
  1369. putc (*s, f);
  1370. s++;
  1371. }
  1372. putc ('"', f);
  1373. }
  1374. else
  1375. fputs (s, f);
  1376. }
  1377. void
  1378. pe_dll_generate_def_file (const char *pe_out_def_filename)
  1379. {
  1380. int i;
  1381. FILE *out = fopen (pe_out_def_filename, "w");
  1382. if (out == NULL)
  1383. /* xgettext:c-format */
  1384. einfo (_("%s: Can't open output def file %s\n"),
  1385. program_name, pe_out_def_filename);
  1386. if (pe_def_file)
  1387. {
  1388. if (pe_def_file->name)
  1389. {
  1390. if (pe_def_file->is_dll)
  1391. fprintf (out, "LIBRARY ");
  1392. else
  1393. fprintf (out, "NAME ");
  1394. quoteput (pe_def_file->name, out, 1);
  1395. if (pe_data (link_info.output_bfd)->pe_opthdr.ImageBase)
  1396. {
  1397. fprintf (out, " BASE=0x");
  1398. fprintf_vma (out, ((bfd_vma) pe_data (link_info.output_bfd)->pe_opthdr.ImageBase));
  1399. }
  1400. fprintf (out, "\n");
  1401. }
  1402. if (pe_def_file->description)
  1403. {
  1404. fprintf (out, "DESCRIPTION ");
  1405. quoteput (pe_def_file->description, out, 1);
  1406. fprintf (out, "\n");
  1407. }
  1408. if (pe_def_file->version_minor != -1)
  1409. fprintf (out, "VERSION %d.%d\n", pe_def_file->version_major,
  1410. pe_def_file->version_minor);
  1411. else if (pe_def_file->version_major != -1)
  1412. fprintf (out, "VERSION %d\n", pe_def_file->version_major);
  1413. if (pe_def_file->stack_reserve != -1 || pe_def_file->heap_reserve != -1)
  1414. fprintf (out, "\n");
  1415. if (pe_def_file->stack_commit != -1)
  1416. fprintf (out, "STACKSIZE 0x%x,0x%x\n",
  1417. pe_def_file->stack_reserve, pe_def_file->stack_commit);
  1418. else if (pe_def_file->stack_reserve != -1)
  1419. fprintf (out, "STACKSIZE 0x%x\n", pe_def_file->stack_reserve);
  1420. if (pe_def_file->heap_commit != -1)
  1421. fprintf (out, "HEAPSIZE 0x%x,0x%x\n",
  1422. pe_def_file->heap_reserve, pe_def_file->heap_commit);
  1423. else if (pe_def_file->heap_reserve != -1)
  1424. fprintf (out, "HEAPSIZE 0x%x\n", pe_def_file->heap_reserve);
  1425. if (pe_def_file->num_section_defs > 0)
  1426. {
  1427. fprintf (out, "\nSECTIONS\n\n");
  1428. for (i = 0; i < pe_def_file->num_section_defs; i++)
  1429. {
  1430. fprintf (out, " ");
  1431. quoteput (pe_def_file->section_defs[i].name, out, 0);
  1432. if (pe_def_file->section_defs[i].class)
  1433. {
  1434. fprintf (out, " CLASS ");
  1435. quoteput (pe_def_file->section_defs[i].class, out, 0);
  1436. }
  1437. if (pe_def_file->section_defs[i].flag_read)
  1438. fprintf (out, " READ");
  1439. if (pe_def_file->section_defs[i].flag_write)
  1440. fprintf (out, " WRITE");
  1441. if (pe_def_file->section_defs[i].flag_execute)
  1442. fprintf (out, " EXECUTE");
  1443. if (pe_def_file->section_defs[i].flag_shared)
  1444. fprintf (out, " SHARED");
  1445. fprintf (out, "\n");
  1446. }
  1447. }
  1448. if (pe_def_file->num_exports > 0)
  1449. {
  1450. fprintf (out, "EXPORTS\n");
  1451. for (i = 0; i < pe_def_file->num_exports; i++)
  1452. {
  1453. def_file_export *e = pe_def_file->exports + i;
  1454. fprintf (out, " ");
  1455. quoteput (e->name, out, 0);
  1456. if (e->internal_name && strcmp (e->internal_name, e->name))
  1457. {
  1458. fprintf (out, " = ");
  1459. quoteput (e->internal_name, out, 0);
  1460. }
  1461. if (e->ordinal != -1)
  1462. fprintf (out, " @%d", e->ordinal);
  1463. if (e->flag_private)
  1464. fprintf (out, " PRIVATE");
  1465. if (e->flag_constant)
  1466. fprintf (out, " CONSTANT");
  1467. if (e->flag_noname)
  1468. fprintf (out, " NONAME");
  1469. if (e->flag_data)
  1470. fprintf (out, " DATA");
  1471. fprintf (out, "\n");
  1472. }
  1473. }
  1474. if (pe_def_file->num_imports > 0)
  1475. {
  1476. fprintf (out, "\nIMPORTS\n\n");
  1477. for (i = 0; i < pe_def_file->num_imports; i++)
  1478. {
  1479. def_file_import *im = pe_def_file->imports + i;
  1480. fprintf (out, " ");
  1481. if (im->internal_name
  1482. && (!im->name || strcmp (im->internal_name, im->name)))
  1483. {
  1484. quoteput (im->internal_name, out, 0);
  1485. fprintf (out, " = ");
  1486. }
  1487. quoteput (im->module->name, out, 0);
  1488. fprintf (out, ".");
  1489. if (im->name)
  1490. quoteput (im->name, out, 0);
  1491. else
  1492. fprintf (out, "%d", im->ordinal);
  1493. if (im->its_name)
  1494. {
  1495. fprintf (out, " == ");
  1496. quoteput (im->its_name, out, 0);
  1497. }
  1498. fprintf (out, "\n");
  1499. }
  1500. }
  1501. }
  1502. else
  1503. fprintf (out, _("; no contents available\n"));
  1504. if (fclose (out) == EOF)
  1505. /* xgettext:c-format */
  1506. einfo (_("%P: Error closing file `%s'\n"), pe_out_def_filename);
  1507. }
  1508. /* Generate the import library. */
  1509. static asymbol **symtab;
  1510. static int symptr;
  1511. static int tmp_seq;
  1512. static int tmp_seq2;
  1513. static const char *dll_filename;
  1514. static char *dll_symname;
  1515. #define UNDSEC bfd_und_section_ptr
  1516. static asection *
  1517. quick_section (bfd *abfd, const char *name, int flags, int align)
  1518. {
  1519. asection *sec;
  1520. asymbol *sym;
  1521. sec = bfd_make_section_old_way (abfd, name);
  1522. bfd_set_section_flags (abfd, sec, flags | SEC_ALLOC | SEC_LOAD | SEC_KEEP);
  1523. bfd_set_section_alignment (abfd, sec, align);
  1524. /* Remember to undo this before trying to link internally! */
  1525. sec->output_section = sec;
  1526. sym = bfd_make_empty_symbol (abfd);
  1527. symtab[symptr++] = sym;
  1528. sym->name = sec->name;
  1529. sym->section = sec;
  1530. sym->flags = BSF_LOCAL;
  1531. sym->value = 0;
  1532. return sec;
  1533. }
  1534. static void
  1535. quick_symbol (bfd *abfd,
  1536. const char *n1,
  1537. const char *n2,
  1538. const char *n3,
  1539. asection *sec,
  1540. int flags,
  1541. int addr)
  1542. {
  1543. asymbol *sym;
  1544. char *name = xmalloc (strlen (n1) + strlen (n2) + strlen (n3) + 1);
  1545. strcpy (name, n1);
  1546. strcat (name, n2);
  1547. strcat (name, n3);
  1548. sym = bfd_make_empty_symbol (abfd);
  1549. sym->name = name;
  1550. sym->section = sec;
  1551. sym->flags = flags;
  1552. sym->value = addr;
  1553. symtab[symptr++] = sym;
  1554. }
  1555. static arelent *reltab = 0;
  1556. static int relcount = 0, relsize = 0;
  1557. static void
  1558. quick_reloc (bfd *abfd, bfd_size_type address, int which_howto, int symidx)
  1559. {
  1560. if (relcount >= relsize - 1)
  1561. {
  1562. relsize += 10;
  1563. if (reltab)
  1564. reltab = xrealloc (reltab, relsize * sizeof (arelent));
  1565. else
  1566. reltab = xmalloc (relsize * sizeof (arelent));
  1567. }
  1568. reltab[relcount].address = address;
  1569. reltab[relcount].addend = 0;
  1570. reltab[relcount].howto = bfd_reloc_type_lookup (abfd, which_howto);
  1571. reltab[relcount].sym_ptr_ptr = symtab + symidx;
  1572. relcount++;
  1573. }
  1574. static void
  1575. save_relocs (asection *sec)
  1576. {
  1577. int i;
  1578. sec->relocation = reltab;
  1579. sec->reloc_count = relcount;
  1580. sec->orelocation = xmalloc ((relcount + 1) * sizeof (arelent *));
  1581. for (i = 0; i < relcount; i++)
  1582. sec->orelocation[i] = sec->relocation + i;
  1583. sec->orelocation[relcount] = 0;
  1584. sec->flags |= SEC_RELOC;
  1585. reltab = 0;
  1586. relcount = relsize = 0;
  1587. }
  1588. /* .section .idata$2
  1589. .global __head_my_dll
  1590. __head_my_dll:
  1591. .rva hname
  1592. .long 0
  1593. .long 0
  1594. .rva __my_dll_iname
  1595. .rva fthunk
  1596. .section .idata$5
  1597. .long 0
  1598. fthunk:
  1599. .section .idata$4
  1600. .long 0
  1601. hname: */
  1602. static bfd *
  1603. make_head (bfd *parent)
  1604. {
  1605. asection *id2, *id5, *id4;
  1606. unsigned char *d2, *d5, *d4;
  1607. char *oname;
  1608. bfd *abfd;
  1609. oname = xmalloc (20);
  1610. sprintf (oname, "d%06d.o", tmp_seq);
  1611. tmp_seq++;
  1612. abfd = bfd_create (oname, parent);
  1613. bfd_find_target (pe_details->object_target, abfd);
  1614. bfd_make_writable (abfd);
  1615. bfd_set_format (abfd, bfd_object);
  1616. bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
  1617. symptr = 0;
  1618. symtab = xmalloc (6 * sizeof (asymbol *));
  1619. id2 = quick_section (abfd, ".idata$2", SEC_HAS_CONTENTS, 2);
  1620. id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
  1621. id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
  1622. quick_symbol (abfd, U ("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0);
  1623. quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
  1624. /* OK, pay attention here. I got confused myself looking back at
  1625. it. We create a four-byte section to mark the beginning of the
  1626. list, and we include an offset of 4 in the section, so that the
  1627. pointer to the list points to the *end* of this section, which is
  1628. the start of the list of sections from other objects. */
  1629. bfd_set_section_size (abfd, id2, 20);
  1630. d2 = xmalloc (20);
  1631. id2->contents = d2;
  1632. memset (d2, 0, 20);
  1633. if (pe_use_nul_prefixed_import_tables)
  1634. d2[0] = d2[16] = PE_IDATA5_SIZE; /* Reloc addend. */
  1635. quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
  1636. quick_reloc (abfd, 12, BFD_RELOC_RVA, 4);
  1637. quick_reloc (abfd, 16, BFD_RELOC_RVA, 1);
  1638. save_relocs (id2);
  1639. if (pe_use_nul_prefixed_import_tables)
  1640. bfd_set_section_size (abfd, id5, PE_IDATA5_SIZE);
  1641. else
  1642. bfd_set_section_size (abfd, id5, 0);
  1643. d5 = xmalloc (PE_IDATA5_SIZE);
  1644. id5->contents = d5;
  1645. memset (d5, 0, PE_IDATA5_SIZE);
  1646. if (pe_use_nul_prefixed_import_tables)
  1647. bfd_set_section_size (abfd, id4, PE_IDATA4_SIZE);
  1648. else
  1649. bfd_set_section_size (abfd, id4, 0);
  1650. d4 = xmalloc (PE_IDATA4_SIZE);
  1651. id4->contents = d4;
  1652. memset (d4, 0, PE_IDATA4_SIZE);
  1653. bfd_set_symtab (abfd, symtab, symptr);
  1654. bfd_set_section_contents (abfd, id2, d2, 0, 20);
  1655. if (pe_use_nul_prefixed_import_tables)
  1656. {
  1657. bfd_set_section_contents (abfd, id5, d5, 0, PE_IDATA5_SIZE);
  1658. bfd_set_section_contents (abfd, id4, d4, 0, PE_IDATA4_SIZE);
  1659. }
  1660. else
  1661. {
  1662. bfd_set_section_contents (abfd, id5, d5, 0, 0);
  1663. bfd_set_section_contents (abfd, id4, d4, 0, 0);
  1664. }
  1665. bfd_make_readable (abfd);
  1666. return abfd;
  1667. }
  1668. /* .section .idata$4
  1669. .long 0
  1670. [.long 0] for PE+
  1671. .section .idata$5
  1672. .long 0
  1673. [.long 0] for PE+
  1674. .section idata$7
  1675. .global __my_dll_iname
  1676. __my_dll_iname:
  1677. .asciz "my.dll" */
  1678. static bfd *
  1679. make_tail (bfd *parent)
  1680. {
  1681. asection *id4, *id5, *id7;
  1682. unsigned char *d4, *d5, *d7;
  1683. int len;
  1684. char *oname;
  1685. bfd *abfd;
  1686. oname = xmalloc (20);
  1687. sprintf (oname, "d%06d.o", tmp_seq);
  1688. tmp_seq++;
  1689. abfd = bfd_create (oname, parent);
  1690. bfd_find_target (pe_details->object_target, abfd);
  1691. bfd_make_writable (abfd);
  1692. bfd_set_format (abfd, bfd_object);
  1693. bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
  1694. symptr = 0;
  1695. symtab = xmalloc (5 * sizeof (asymbol *));
  1696. id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
  1697. id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
  1698. id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
  1699. quick_symbol (abfd, U (""), dll_symname, "_iname", id7, BSF_GLOBAL, 0);
  1700. bfd_set_section_size (abfd, id4, PE_IDATA4_SIZE);
  1701. d4 = xmalloc (PE_IDATA4_SIZE);
  1702. id4->contents = d4;
  1703. memset (d4, 0, PE_IDATA4_SIZE);
  1704. bfd_set_section_size (abfd, id5, PE_IDATA5_SIZE);
  1705. d5 = xmalloc (PE_IDATA5_SIZE);
  1706. id5->contents = d5;
  1707. memset (d5, 0, PE_IDATA5_SIZE);
  1708. len = strlen (dll_filename) + 1;
  1709. if (len & 1)
  1710. len++;
  1711. bfd_set_section_size (abfd, id7, len);
  1712. d7 = xmalloc (len);
  1713. id7->contents = d7;
  1714. strcpy ((char *) d7, dll_filename);
  1715. /* If len was odd, the above
  1716. strcpy leaves behind an undefined byte. That is harmless,
  1717. but we set it to 0 just so the binary dumps are pretty. */
  1718. d7[len - 1] = 0;
  1719. bfd_set_symtab (abfd, symtab, symptr);
  1720. bfd_set_section_contents (abfd, id4, d4, 0, PE_IDATA4_SIZE);
  1721. bfd_set_section_contents (abfd, id5, d5, 0, PE_IDATA5_SIZE);
  1722. bfd_set_section_contents (abfd, id7, d7, 0, len);
  1723. bfd_make_readable (abfd);
  1724. return abfd;
  1725. }
  1726. /* .text
  1727. .global _function
  1728. .global ___imp_function
  1729. .global __imp__function
  1730. _function:
  1731. jmp *__imp__function:
  1732. .section idata$7
  1733. .long __head_my_dll
  1734. .section .idata$5
  1735. ___imp_function:
  1736. __imp__function:
  1737. iat?
  1738. .section .idata$4
  1739. iat?
  1740. .section .idata$6
  1741. ID<ordinal>:
  1742. .short <hint>
  1743. .asciz "function" xlate? (add underscore, kill at) */
  1744. static const unsigned char jmp_ix86_bytes[] =
  1745. {
  1746. 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90
  1747. };
  1748. /* _function:
  1749. mov.l ip+8,r0
  1750. mov.l @r0,r0
  1751. jmp @r0
  1752. nop
  1753. .dw __imp_function */
  1754. static const unsigned char jmp_sh_bytes[] =
  1755. {
  1756. 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00
  1757. };
  1758. /* _function:
  1759. lui $t0,<high:__imp_function>
  1760. lw $t0,<low:__imp_function>
  1761. jr $t0
  1762. nop */
  1763. static const unsigned char jmp_mips_bytes[] =
  1764. {
  1765. 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d,
  1766. 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00
  1767. };
  1768. static const unsigned char jmp_arm_bytes[] =
  1769. {
  1770. 0x00, 0xc0, 0x9f, 0xe5, /* ldr ip, [pc] */
  1771. 0x00, 0xf0, 0x9c, 0xe5, /* ldr pc, [ip] */
  1772. 0, 0, 0, 0
  1773. };
  1774. static bfd *
  1775. make_one (def_file_export *exp, bfd *parent, bfd_boolean include_jmp_stub)
  1776. {
  1777. asection *tx, *id7, *id5, *id4, *id6;
  1778. unsigned char *td = NULL, *d7, *d5, *d4, *d6 = NULL;
  1779. int len;
  1780. char *oname;
  1781. bfd *abfd;
  1782. const unsigned char *jmp_bytes = NULL;
  1783. int jmp_byte_count = 0;
  1784. /* Include the jump stub section only if it is needed. A jump
  1785. stub is needed if the symbol being imported <sym> is a function
  1786. symbol and there is at least one undefined reference to that
  1787. symbol. In other words, if all the import references to <sym> are
  1788. explicitly through _declspec(dllimport) then the jump stub is not
  1789. needed. */
  1790. if (include_jmp_stub)
  1791. {
  1792. switch (pe_details->pe_arch)
  1793. {
  1794. case PE_ARCH_i386:
  1795. jmp_bytes = jmp_ix86_bytes;
  1796. jmp_byte_count = sizeof (jmp_ix86_bytes);
  1797. break;
  1798. case PE_ARCH_sh:
  1799. jmp_bytes = jmp_sh_bytes;
  1800. jmp_byte_count = sizeof (jmp_sh_bytes);
  1801. break;
  1802. case PE_ARCH_mips:
  1803. jmp_bytes = jmp_mips_bytes;
  1804. jmp_byte_count = sizeof (jmp_mips_bytes);
  1805. break;
  1806. case PE_ARCH_arm:
  1807. case PE_ARCH_arm_epoc:
  1808. case PE_ARCH_arm_wince:
  1809. jmp_bytes = jmp_arm_bytes;
  1810. jmp_byte_count = sizeof (jmp_arm_bytes);
  1811. break;
  1812. default:
  1813. abort ();
  1814. }
  1815. }
  1816. oname = xmalloc (20);
  1817. sprintf (oname, "d%06d.o", tmp_seq);
  1818. tmp_seq++;
  1819. abfd = bfd_create (oname, parent);
  1820. bfd_find_target (pe_details->object_target, abfd);
  1821. bfd_make_writable (abfd);
  1822. bfd_set_format (abfd, bfd_object);
  1823. bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
  1824. symptr = 0;
  1825. symtab = xmalloc (12 * sizeof (asymbol *));
  1826. tx = quick_section (abfd, ".text", SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY, 2);
  1827. id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
  1828. id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
  1829. id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
  1830. id6 = quick_section (abfd, ".idata$6", SEC_HAS_CONTENTS, 2);
  1831. if (*exp->internal_name == '@')
  1832. {
  1833. quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC,
  1834. BSF_GLOBAL, 0);
  1835. if (include_jmp_stub)
  1836. quick_symbol (abfd, "", exp->internal_name, "", tx, BSF_GLOBAL, 0);
  1837. quick_symbol (abfd, "__imp_", exp->internal_name, "", id5,
  1838. BSF_GLOBAL, 0);
  1839. /* Fastcall applies only to functions,
  1840. so no need for auto-import symbol. */
  1841. }
  1842. else
  1843. {
  1844. quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC,
  1845. BSF_GLOBAL, 0);
  1846. if (include_jmp_stub)
  1847. quick_symbol (abfd, U (""), exp->internal_name, "", tx,
  1848. BSF_GLOBAL, 0);
  1849. quick_symbol (abfd, "__imp_", U (""), exp->internal_name, id5,
  1850. BSF_GLOBAL, 0);
  1851. /* Symbol to reference ord/name of imported
  1852. data symbol, used to implement auto-import. */
  1853. if (exp->flag_data)
  1854. quick_symbol (abfd, "__nm_", U (""), exp->internal_name, id6,
  1855. BSF_GLOBAL,0);
  1856. }
  1857. if (pe_dll_compat_implib)
  1858. quick_symbol (abfd, "___imp_", exp->internal_name, "", id5,
  1859. BSF_GLOBAL, 0);
  1860. if (include_jmp_stub)
  1861. {
  1862. bfd_set_section_size (abfd, tx, jmp_byte_count);
  1863. td = xmalloc (jmp_byte_count);
  1864. tx->contents = td;
  1865. memcpy (td, jmp_bytes, jmp_byte_count);
  1866. switch (pe_details->pe_arch)
  1867. {
  1868. case PE_ARCH_i386:
  1869. #ifdef pe_use_x86_64
  1870. quick_reloc (abfd, 2, BFD_RELOC_32_PCREL, 2);
  1871. #else
  1872. /* Mark this object as SAFESEH compatible. */
  1873. quick_symbol (abfd, "", "@feat.00", "", bfd_abs_section_ptr,
  1874. BSF_LOCAL, 1);
  1875. quick_reloc (abfd, 2, BFD_RELOC_32, 2);
  1876. #endif
  1877. break;
  1878. case PE_ARCH_sh:
  1879. quick_reloc (abfd, 8, BFD_RELOC_32, 2);
  1880. break;
  1881. case PE_ARCH_mips:
  1882. quick_reloc (abfd, 0, BFD_RELOC_HI16_S, 2);
  1883. quick_reloc (abfd, 0, BFD_RELOC_LO16, 0); /* MIPS_R_PAIR */
  1884. quick_reloc (abfd, 4, BFD_RELOC_LO16, 2);
  1885. break;
  1886. case PE_ARCH_arm:
  1887. case PE_ARCH_arm_epoc:
  1888. case PE_ARCH_arm_wince:
  1889. quick_reloc (abfd, 8, BFD_RELOC_32, 2);
  1890. break;
  1891. default:
  1892. abort ();
  1893. }
  1894. save_relocs (tx);
  1895. }
  1896. else
  1897. bfd_set_section_size (abfd, tx, 0);
  1898. bfd_set_section_size (abfd, id7, 4);
  1899. d7 = xmalloc (4);
  1900. id7->contents = d7;
  1901. memset (d7, 0, 4);
  1902. quick_reloc (abfd, 0, BFD_RELOC_RVA, 5);
  1903. save_relocs (id7);
  1904. bfd_set_section_size (abfd, id5, PE_IDATA5_SIZE);
  1905. d5 = xmalloc (PE_IDATA5_SIZE);
  1906. id5->contents = d5;
  1907. memset (d5, 0, PE_IDATA5_SIZE);
  1908. if (exp->flag_noname)
  1909. {
  1910. d5[0] = exp->ordinal;
  1911. d5[1] = exp->ordinal >> 8;
  1912. d5[PE_IDATA5_SIZE - 1] = 0x80;
  1913. }
  1914. else
  1915. {
  1916. quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
  1917. save_relocs (id5);
  1918. }
  1919. bfd_set_section_size (abfd, id4, PE_IDATA4_SIZE);
  1920. d4 = xmalloc (PE_IDATA4_SIZE);
  1921. id4->contents = d4;
  1922. memset (d4, 0, PE_IDATA4_SIZE);
  1923. if (exp->flag_noname)
  1924. {
  1925. d4[0] = exp->ordinal;
  1926. d4[1] = exp->ordinal >> 8;
  1927. d4[PE_IDATA4_SIZE - 1] = 0x80;
  1928. }
  1929. else
  1930. {
  1931. quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
  1932. save_relocs (id4);
  1933. }
  1934. if (exp->flag_noname)
  1935. {
  1936. len = 0;
  1937. bfd_set_section_size (abfd, id6, 0);
  1938. }
  1939. else
  1940. {
  1941. /* { short, asciz } */
  1942. if (exp->its_name)
  1943. len = 2 + strlen (exp->its_name) + 1;
  1944. else
  1945. len = 2 + strlen (exp->name) + 1;
  1946. if (len & 1)
  1947. len++;
  1948. bfd_set_section_size (abfd, id6, len);
  1949. d6 = xmalloc (len);
  1950. id6->contents = d6;
  1951. memset (d6, 0, len);
  1952. d6[0] = exp->hint & 0xff;
  1953. d6[1] = exp->hint >> 8;
  1954. if (exp->its_name)
  1955. strcpy ((char*) d6 + 2, exp->its_name);
  1956. else
  1957. strcpy ((char *) d6 + 2, exp->name);
  1958. }
  1959. bfd_set_symtab (abfd, symtab, symptr);
  1960. if (include_jmp_stub)
  1961. bfd_set_section_contents (abfd, tx, td, 0, jmp_byte_count);
  1962. bfd_set_section_contents (abfd, id7, d7, 0, 4);
  1963. bfd_set_section_contents (abfd, id5, d5, 0, PE_IDATA5_SIZE);
  1964. bfd_set_section_contents (abfd, id4, d4, 0, PE_IDATA4_SIZE);
  1965. if (!exp->flag_noname)
  1966. bfd_set_section_contents (abfd, id6, d6, 0, len);
  1967. bfd_make_readable (abfd);
  1968. return abfd;
  1969. }
  1970. static bfd *
  1971. make_singleton_name_imp (const char *import, bfd *parent)
  1972. {
  1973. /* Name thunks go to idata$4. */
  1974. asection *id5;
  1975. unsigned char *d5;
  1976. char *oname;
  1977. bfd *abfd;
  1978. oname = xmalloc (20);
  1979. sprintf (oname, "nmimp%06d.o", tmp_seq2);
  1980. tmp_seq2++;
  1981. abfd = bfd_create (oname, parent);
  1982. bfd_find_target (pe_details->object_target, abfd);
  1983. bfd_make_writable (abfd);
  1984. bfd_set_format (abfd, bfd_object);
  1985. bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
  1986. symptr = 0;
  1987. symtab = xmalloc (3 * sizeof (asymbol *));
  1988. id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
  1989. quick_symbol (abfd, "__imp_", import, "", id5, BSF_GLOBAL, 0);
  1990. /* We need space for the real thunk and for the null terminator. */
  1991. bfd_set_section_size (abfd, id5, PE_IDATA5_SIZE * 2);
  1992. d5 = xmalloc (PE_IDATA5_SIZE * 2);
  1993. id5->contents = d5;
  1994. memset (d5, 0, PE_IDATA5_SIZE * 2);
  1995. quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
  1996. save_relocs (id5);
  1997. bfd_set_symtab (abfd, symtab, symptr);
  1998. bfd_set_section_contents (abfd, id5, d5, 0, PE_IDATA4_SIZE * 2);
  1999. bfd_make_readable (abfd);
  2000. return abfd;
  2001. }
  2002. static bfd *
  2003. make_singleton_name_thunk (const char *import, bfd *parent)
  2004. {
  2005. /* Name thunks go to idata$4. */
  2006. asection *id4;
  2007. unsigned char *d4;
  2008. char *oname;
  2009. bfd *abfd;
  2010. oname = xmalloc (20);
  2011. sprintf (oname, "nmth%06d.o", tmp_seq);
  2012. tmp_seq++;
  2013. abfd = bfd_create (oname, parent);
  2014. bfd_find_target (pe_details->object_target, abfd);
  2015. bfd_make_writable (abfd);
  2016. bfd_set_format (abfd, bfd_object);
  2017. bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
  2018. symptr = 0;
  2019. symtab = xmalloc (3 * sizeof (asymbol *));
  2020. id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
  2021. quick_symbol (abfd, "__nm_thnk_", import, "", id4, BSF_GLOBAL, 0);
  2022. quick_symbol (abfd, "__nm_", import, "", UNDSEC, BSF_GLOBAL, 0);
  2023. /* We need space for the real thunk and for the null terminator. */
  2024. bfd_set_section_size (abfd, id4, PE_IDATA4_SIZE * 2);
  2025. d4 = xmalloc (PE_IDATA4_SIZE * 2);
  2026. id4->contents = d4;
  2027. memset (d4, 0, PE_IDATA4_SIZE * 2);
  2028. quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
  2029. save_relocs (id4);
  2030. bfd_set_symtab (abfd, symtab, symptr);
  2031. bfd_set_section_contents (abfd, id4, d4, 0, PE_IDATA4_SIZE * 2);
  2032. bfd_make_readable (abfd);
  2033. return abfd;
  2034. }
  2035. static char *
  2036. make_import_fixup_mark (arelent *rel)
  2037. {
  2038. /* We convert reloc to symbol, for later reference. */
  2039. static int counter;
  2040. static char *fixup_name = NULL;
  2041. static size_t buffer_len = 0;
  2042. struct bfd_symbol *sym = *rel->sym_ptr_ptr;
  2043. bfd *abfd = bfd_asymbol_bfd (sym);
  2044. struct bfd_link_hash_entry *bh;
  2045. if (!fixup_name)
  2046. {
  2047. fixup_name = xmalloc (384);
  2048. buffer_len = 384;
  2049. }
  2050. if (strlen (sym->name) + 25 > buffer_len)
  2051. /* Assume 25 chars for "__fu" + counter + "_". If counter is
  2052. bigger than 20 digits long, we've got worse problems than
  2053. overflowing this buffer... */
  2054. {
  2055. free (fixup_name);
  2056. /* New buffer size is length of symbol, plus 25, but
  2057. then rounded up to the nearest multiple of 128. */
  2058. buffer_len = ((strlen (sym->name) + 25) + 127) & ~127;
  2059. fixup_name = xmalloc (buffer_len);
  2060. }
  2061. sprintf (fixup_name, "__fu%d_%s", counter++, sym->name);
  2062. bh = NULL;
  2063. bfd_coff_link_add_one_symbol (&link_info, abfd, fixup_name, BSF_GLOBAL,
  2064. current_sec, /* sym->section, */
  2065. rel->address, NULL, TRUE, FALSE, &bh);
  2066. return fixup_name;
  2067. }
  2068. /* .section .idata$2
  2069. .rva __nm_thnk_SYM (singleton thunk with name of func)
  2070. .long 0
  2071. .long 0
  2072. .rva __my_dll_iname (name of dll)
  2073. .rva __fuNN_SYM (pointer to reference (address) in text) */
  2074. static bfd *
  2075. make_import_fixup_entry (const char *name,
  2076. const char *fixup_name,
  2077. const char *symname,
  2078. bfd *parent)
  2079. {
  2080. asection *id2;
  2081. unsigned char *d2;
  2082. char *oname;
  2083. bfd *abfd;
  2084. oname = xmalloc (20);
  2085. sprintf (oname, "fu%06d.o", tmp_seq);
  2086. tmp_seq++;
  2087. abfd = bfd_create (oname, parent);
  2088. bfd_find_target (pe_details->object_target, abfd);
  2089. bfd_make_writable (abfd);
  2090. bfd_set_format (abfd, bfd_object);
  2091. bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
  2092. symptr = 0;
  2093. symtab = xmalloc (6 * sizeof (asymbol *));
  2094. id2 = quick_section (abfd, ".idata$2", SEC_HAS_CONTENTS, 2);
  2095. quick_symbol (abfd, "__nm_thnk_", name, "", UNDSEC, BSF_GLOBAL, 0);
  2096. quick_symbol (abfd, U (""), symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
  2097. /* For relocator v2 we have to use the .idata$5 element and not
  2098. fixup_name. */
  2099. if (link_info.pei386_runtime_pseudo_reloc == 2)
  2100. quick_symbol (abfd, "__imp_", name, "", UNDSEC, BSF_GLOBAL, 0);
  2101. else
  2102. quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0);
  2103. bfd_set_section_size (abfd, id2, 20);
  2104. d2 = xmalloc (20);
  2105. id2->contents = d2;
  2106. memset (d2, 0, 20);
  2107. quick_reloc (abfd, 0, BFD_RELOC_RVA, 1);
  2108. quick_reloc (abfd, 12, BFD_RELOC_RVA, 2);
  2109. quick_reloc (abfd, 16, BFD_RELOC_RVA, 3);
  2110. save_relocs (id2);
  2111. bfd_set_symtab (abfd, symtab, symptr);
  2112. bfd_set_section_contents (abfd, id2, d2, 0, 20);
  2113. bfd_make_readable (abfd);
  2114. return abfd;
  2115. }
  2116. /* .section .rdata_runtime_pseudo_reloc
  2117. .long addend
  2118. .rva __fuNN_SYM (pointer to reference (address) in text) */
  2119. static bfd *
  2120. make_runtime_pseudo_reloc (const char *name ATTRIBUTE_UNUSED,
  2121. const char *fixup_name,
  2122. bfd_vma addend ATTRIBUTE_UNUSED,
  2123. bfd_vma bitsize,
  2124. bfd *parent)
  2125. {
  2126. asection *rt_rel;
  2127. unsigned char *rt_rel_d;
  2128. char *oname;
  2129. bfd *abfd;
  2130. oname = xmalloc (20);
  2131. sprintf (oname, "rtr%06d.o", tmp_seq);
  2132. tmp_seq++;
  2133. abfd = bfd_create (oname, parent);
  2134. bfd_find_target (pe_details->object_target, abfd);
  2135. bfd_make_writable (abfd);
  2136. bfd_set_format (abfd, bfd_object);
  2137. bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
  2138. symptr = 0;
  2139. if (link_info.pei386_runtime_pseudo_reloc == 2)
  2140. {
  2141. symtab = xmalloc ((runtime_pseudp_reloc_v2_init ? 3 : 6) * sizeof (asymbol *));
  2142. }
  2143. else
  2144. {
  2145. symtab = xmalloc (2 * sizeof (asymbol *));
  2146. }
  2147. rt_rel = quick_section (abfd, ".rdata_runtime_pseudo_reloc",
  2148. SEC_HAS_CONTENTS, 2);
  2149. quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0);
  2150. if (link_info.pei386_runtime_pseudo_reloc == 2)
  2151. {
  2152. size_t size = 12;
  2153. if (! runtime_pseudp_reloc_v2_init)
  2154. {
  2155. size += 12;
  2156. runtime_pseudp_reloc_v2_init = 1;
  2157. }
  2158. quick_symbol (abfd, "__imp_", name, "", UNDSEC, BSF_GLOBAL, 0);
  2159. bfd_set_section_size (abfd, rt_rel, size);
  2160. rt_rel_d = xmalloc (size);
  2161. rt_rel->contents = rt_rel_d;
  2162. memset (rt_rel_d, 0, size);
  2163. quick_reloc (abfd, size - 8, BFD_RELOC_RVA, 1);
  2164. quick_reloc (abfd, size - 12, BFD_RELOC_RVA, 2);
  2165. bfd_put_32 (abfd, bitsize, rt_rel_d + (size - 4));
  2166. if (size != 12)
  2167. bfd_put_32 (abfd, 1, rt_rel_d + 8);
  2168. save_relocs (rt_rel);
  2169. bfd_set_symtab (abfd, symtab, symptr);
  2170. bfd_set_section_contents (abfd, rt_rel, rt_rel_d, 0, size);
  2171. }
  2172. else
  2173. {
  2174. bfd_set_section_size (abfd, rt_rel, 8);
  2175. rt_rel_d = xmalloc (8);
  2176. rt_rel->contents = rt_rel_d;
  2177. memset (rt_rel_d, 0, 8);
  2178. bfd_put_32 (abfd, addend, rt_rel_d);
  2179. quick_reloc (abfd, 4, BFD_RELOC_RVA, 1);
  2180. save_relocs (rt_rel);
  2181. bfd_set_symtab (abfd, symtab, symptr);
  2182. bfd_set_section_contents (abfd, rt_rel, rt_rel_d, 0, 8);
  2183. }
  2184. bfd_make_readable (abfd);
  2185. return abfd;
  2186. }
  2187. /* .section .rdata
  2188. .rva __pei386_runtime_relocator */
  2189. static bfd *
  2190. pe_create_runtime_relocator_reference (bfd *parent)
  2191. {
  2192. asection *extern_rt_rel;
  2193. unsigned char *extern_rt_rel_d;
  2194. char *oname;
  2195. bfd *abfd;
  2196. oname = xmalloc (20);
  2197. sprintf (oname, "ertr%06d.o", tmp_seq);
  2198. tmp_seq++;
  2199. abfd = bfd_create (oname, parent);
  2200. bfd_find_target (pe_details->object_target, abfd);
  2201. bfd_make_writable (abfd);
  2202. bfd_set_format (abfd, bfd_object);
  2203. bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
  2204. symptr = 0;
  2205. symtab = xmalloc (2 * sizeof (asymbol *));
  2206. extern_rt_rel = quick_section (abfd, ".rdata", SEC_HAS_CONTENTS, 2);
  2207. quick_symbol (abfd, "", U ("_pei386_runtime_relocator"), "", UNDSEC,
  2208. BSF_NO_FLAGS, 0);
  2209. bfd_set_section_size (abfd, extern_rt_rel, PE_IDATA5_SIZE);
  2210. extern_rt_rel_d = xcalloc (1, PE_IDATA5_SIZE);
  2211. extern_rt_rel->contents = extern_rt_rel_d;
  2212. quick_reloc (abfd, 0, BFD_RELOC_RVA, 1);
  2213. save_relocs (extern_rt_rel);
  2214. bfd_set_symtab (abfd, symtab, symptr);
  2215. bfd_set_section_contents (abfd, extern_rt_rel, extern_rt_rel_d, 0, PE_IDATA5_SIZE);
  2216. bfd_make_readable (abfd);
  2217. return abfd;
  2218. }
  2219. void
  2220. pe_create_import_fixup (arelent *rel, asection *s, bfd_vma addend)
  2221. {
  2222. char buf[300];
  2223. struct bfd_symbol *sym = *rel->sym_ptr_ptr;
  2224. struct bfd_link_hash_entry *name_thunk_sym;
  2225. struct bfd_link_hash_entry *name_imp_sym;
  2226. const char *name = sym->name;
  2227. char *fixup_name = make_import_fixup_mark (rel);
  2228. bfd *b;
  2229. int need_import_table = 1;
  2230. sprintf (buf, "__imp_%s", name);
  2231. name_imp_sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
  2232. sprintf (buf, "__nm_thnk_%s", name);
  2233. name_thunk_sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
  2234. /* For version 2 pseudo relocation we don't need to add an import
  2235. if the import symbol is already present. */
  2236. if (link_info.pei386_runtime_pseudo_reloc == 2
  2237. && name_imp_sym
  2238. && name_imp_sym->type == bfd_link_hash_defined)
  2239. need_import_table = 0;
  2240. if (need_import_table == 1
  2241. && (!name_thunk_sym || name_thunk_sym->type != bfd_link_hash_defined))
  2242. {
  2243. b = make_singleton_name_thunk (name, link_info.output_bfd);
  2244. add_bfd_to_link (b, b->filename, &link_info);
  2245. /* If we ever use autoimport, we have to cast text section writable.
  2246. But not for version 2. */
  2247. if (link_info.pei386_runtime_pseudo_reloc != 2)
  2248. {
  2249. config.text_read_only = FALSE;
  2250. link_info.output_bfd->flags &= ~WP_TEXT;
  2251. }
  2252. if (link_info.pei386_runtime_pseudo_reloc == 2)
  2253. {
  2254. b = make_singleton_name_imp (name, link_info.output_bfd);
  2255. add_bfd_to_link (b, b->filename, &link_info);
  2256. }
  2257. }
  2258. if ((addend == 0 || link_info.pei386_runtime_pseudo_reloc)
  2259. && need_import_table == 1)
  2260. {
  2261. extern char * pe_data_import_dll;
  2262. char * symname = pe_data_import_dll ? pe_data_import_dll : "unknown";
  2263. b = make_import_fixup_entry (name, fixup_name, symname,
  2264. link_info.output_bfd);
  2265. add_bfd_to_link (b, b->filename, &link_info);
  2266. }
  2267. if ((link_info.pei386_runtime_pseudo_reloc != 0 && addend != 0)
  2268. || link_info.pei386_runtime_pseudo_reloc == 2)
  2269. {
  2270. if (pe_dll_extra_pe_debug)
  2271. printf ("creating runtime pseudo-reloc entry for %s (addend=%d)\n",
  2272. fixup_name, (int) addend);
  2273. b = make_runtime_pseudo_reloc (name, fixup_name, addend, rel->howto->bitsize,
  2274. link_info.output_bfd);
  2275. add_bfd_to_link (b, b->filename, &link_info);
  2276. if (runtime_pseudo_relocs_created == 0)
  2277. {
  2278. b = pe_create_runtime_relocator_reference (link_info.output_bfd);
  2279. add_bfd_to_link (b, b->filename, &link_info);
  2280. }
  2281. runtime_pseudo_relocs_created++;
  2282. }
  2283. else if (addend != 0)
  2284. {
  2285. einfo (_("%C: variable '%T' can't be auto-imported. Please read the documentation for ld's --enable-auto-import for details.\n"),
  2286. s->owner, s, rel->address, sym->name);
  2287. einfo ("%X");
  2288. }
  2289. }
  2290. void
  2291. pe_dll_generate_implib (def_file *def, const char *impfilename, struct bfd_link_info *info)
  2292. {
  2293. int i;
  2294. bfd *ar_head;
  2295. bfd *ar_tail;
  2296. bfd *outarch;
  2297. bfd *ibfd;
  2298. bfd *head = 0;
  2299. dll_filename = (def->name) ? def->name : dll_name;
  2300. dll_symname = xstrdup (dll_filename);
  2301. for (i = 0; dll_symname[i]; i++)
  2302. if (!ISALNUM (dll_symname[i]))
  2303. dll_symname[i] = '_';
  2304. unlink_if_ordinary (impfilename);
  2305. outarch = bfd_openw (impfilename, 0);
  2306. if (!outarch)
  2307. {
  2308. /* xgettext:c-format */
  2309. einfo (_("%XCan't open .lib file: %s\n"), impfilename);
  2310. return;
  2311. }
  2312. if (verbose)
  2313. /* xgettext:c-format */
  2314. info_msg (_("Creating library file: %s\n"), impfilename);
  2315. bfd_set_format (outarch, bfd_archive);
  2316. outarch->has_armap = 1;
  2317. /* Work out a reasonable size of things to put onto one line. */
  2318. ar_head = make_head (outarch);
  2319. /* Iterate the input BFDs, looking for exclude-modules-for-implib. */
  2320. for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
  2321. {
  2322. /* Iterate the exclude list. */
  2323. struct exclude_list_struct *ex;
  2324. char found;
  2325. for (ex = excludes, found = 0; ex && !found; ex = ex->next)
  2326. {
  2327. if (ex->type != EXCLUDEFORIMPLIB)
  2328. continue;
  2329. found = (filename_cmp (ex->string, ibfd->filename) == 0);
  2330. }
  2331. /* If it matched, we must open a fresh BFD for it (the original
  2332. input BFD is still needed for the DLL's final link) and add
  2333. it into the archive member chain. */
  2334. if (found)
  2335. {
  2336. bfd *newbfd = bfd_openr (ibfd->my_archive
  2337. ? ibfd->my_archive->filename : ibfd->filename, NULL);
  2338. if (!newbfd)
  2339. {
  2340. einfo (_("%Xbfd_openr %s: %E\n"), ibfd->filename);
  2341. return;
  2342. }
  2343. if (ibfd->my_archive)
  2344. {
  2345. /* Must now iterate through archive until we find the
  2346. required member. A minor shame that we'll open the
  2347. archive once per member that we require from it, and
  2348. leak those archive bfds rather than reuse them. */
  2349. bfd *arbfd = newbfd;
  2350. if (!bfd_check_format_matches (arbfd, bfd_archive, NULL))
  2351. {
  2352. einfo (_("%X%s(%s): can't find member in non-archive file"),
  2353. ibfd->my_archive->filename, ibfd->filename);
  2354. return;
  2355. }
  2356. newbfd = NULL;
  2357. while ((newbfd = bfd_openr_next_archived_file (arbfd, newbfd)) != 0)
  2358. {
  2359. if (filename_cmp (newbfd->filename, ibfd->filename) == 0)
  2360. break;
  2361. }
  2362. if (!newbfd)
  2363. {
  2364. einfo (_("%X%s(%s): can't find member in archive"),
  2365. ibfd->my_archive->filename, ibfd->filename);
  2366. return;
  2367. }
  2368. }
  2369. newbfd->archive_next = head;
  2370. head = newbfd;
  2371. }
  2372. }
  2373. for (i = 0; i < def->num_exports; i++)
  2374. {
  2375. /* The import library doesn't know about the internal name. */
  2376. char *internal = def->exports[i].internal_name;
  2377. bfd *n;
  2378. /* Don't add PRIVATE entries to import lib. */
  2379. if (pe_def_file->exports[i].flag_private)
  2380. continue;
  2381. def->exports[i].internal_name = def->exports[i].name;
  2382. n = make_one (def->exports + i, outarch,
  2383. ! (def->exports + i)->flag_data);
  2384. n->archive_next = head;
  2385. head = n;
  2386. def->exports[i].internal_name = internal;
  2387. }
  2388. ar_tail = make_tail (outarch);
  2389. if (ar_head == NULL || ar_tail == NULL)
  2390. return;
  2391. /* Now stick them all into the archive. */
  2392. ar_head->archive_next = head;
  2393. ar_tail->archive_next = ar_head;
  2394. head = ar_tail;
  2395. if (! bfd_set_archive_head (outarch, head))
  2396. einfo ("%Xbfd_set_archive_head: %E\n");
  2397. if (! bfd_close (outarch))
  2398. einfo ("%Xbfd_close %s: %E\n", impfilename);
  2399. while (head != NULL)
  2400. {
  2401. bfd *n = head->archive_next;
  2402. bfd_close (head);
  2403. head = n;
  2404. }
  2405. }
  2406. static int undef_count = 0;
  2407. struct key_value
  2408. {
  2409. char *key;
  2410. const char *oname;
  2411. };
  2412. static struct key_value *udef_table;
  2413. static int undef_sort_cmp (const void *l1, const void *r1)
  2414. {
  2415. const struct key_value *l = l1;
  2416. const struct key_value *r = r1;
  2417. return strcmp (l->key, r->key);
  2418. }
  2419. static struct bfd_link_hash_entry *
  2420. pe_find_cdecl_alias_match (struct bfd_link_info *linfo, char *name)
  2421. {
  2422. struct bfd_link_hash_entry *h = NULL;
  2423. struct key_value *kv;
  2424. struct key_value key;
  2425. char *at, *lname = (char *) alloca (strlen (name) + 3);
  2426. strcpy (lname, name);
  2427. at = strchr (lname + (lname[0] == '@'), '@');
  2428. if (at)
  2429. at[1] = 0;
  2430. key.key = lname;
  2431. kv = bsearch (&key, udef_table, undef_count, sizeof (struct key_value),
  2432. undef_sort_cmp);
  2433. if (kv)
  2434. {
  2435. h = bfd_link_hash_lookup (linfo->hash, kv->oname, FALSE, FALSE, FALSE);
  2436. if (h->type == bfd_link_hash_undefined)
  2437. return h;
  2438. }
  2439. if (lname[0] == '?')
  2440. return NULL;
  2441. if (at || lname[0] == '@')
  2442. {
  2443. if (lname[0] == '@')
  2444. {
  2445. if (pe_details->underscored)
  2446. lname[0] = '_';
  2447. else
  2448. strcpy (lname, lname + 1);
  2449. key.key = lname;
  2450. kv = bsearch (&key, udef_table, undef_count,
  2451. sizeof (struct key_value), undef_sort_cmp);
  2452. if (kv)
  2453. {
  2454. h = bfd_link_hash_lookup (linfo->hash, kv->oname, FALSE, FALSE, FALSE);
  2455. if (h->type == bfd_link_hash_undefined)
  2456. return h;
  2457. }
  2458. }
  2459. if (at)
  2460. *strchr (lname, '@') = 0;
  2461. key.key = lname;
  2462. kv = bsearch (&key, udef_table, undef_count,
  2463. sizeof (struct key_value), undef_sort_cmp);
  2464. if (kv)
  2465. {
  2466. h = bfd_link_hash_lookup (linfo->hash, kv->oname, FALSE, FALSE, FALSE);
  2467. if (h->type == bfd_link_hash_undefined)
  2468. return h;
  2469. }
  2470. return NULL;
  2471. }
  2472. strcat (lname, "@");
  2473. key.key = lname;
  2474. kv = bsearch (&key, udef_table, undef_count,
  2475. sizeof (struct key_value), undef_sort_cmp);
  2476. if (kv)
  2477. {
  2478. h = bfd_link_hash_lookup (linfo->hash, kv->oname, FALSE, FALSE, FALSE);
  2479. if (h->type == bfd_link_hash_undefined)
  2480. return h;
  2481. }
  2482. if (lname[0] == '_' && pe_details->underscored)
  2483. lname[0] = '@';
  2484. else
  2485. {
  2486. memmove (lname + 1, lname, strlen (lname) + 1);
  2487. lname[0] = '@';
  2488. }
  2489. key.key = lname;
  2490. kv = bsearch (&key, udef_table, undef_count,
  2491. sizeof (struct key_value), undef_sort_cmp);
  2492. if (kv)
  2493. {
  2494. h = bfd_link_hash_lookup (linfo->hash, kv->oname, FALSE, FALSE, FALSE);
  2495. if (h->type == bfd_link_hash_undefined)
  2496. return h;
  2497. }
  2498. return NULL;
  2499. }
  2500. static bfd_boolean
  2501. pe_undef_count (struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
  2502. void *inf ATTRIBUTE_UNUSED)
  2503. {
  2504. if (h->type == bfd_link_hash_undefined)
  2505. undef_count++;
  2506. return TRUE;
  2507. }
  2508. static bfd_boolean
  2509. pe_undef_fill (struct bfd_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
  2510. {
  2511. if (h->type == bfd_link_hash_undefined)
  2512. {
  2513. char *at;
  2514. udef_table[undef_count].key = xstrdup (h->root.string);
  2515. at = strchr (udef_table[undef_count].key
  2516. + (udef_table[undef_count].key[0] == '@'), '@');
  2517. if (at)
  2518. at[1] = 0;
  2519. udef_table[undef_count].oname = h->root.string;
  2520. undef_count++;
  2521. }
  2522. return TRUE;
  2523. }
  2524. static void
  2525. pe_create_undef_table (void)
  2526. {
  2527. undef_count = 0;
  2528. /* count undefined symbols */
  2529. bfd_link_hash_traverse (link_info.hash, pe_undef_count, "");
  2530. /* create and fill the corresponding table */
  2531. udef_table = xmalloc (undef_count * sizeof (struct key_value));
  2532. undef_count = 0;
  2533. bfd_link_hash_traverse (link_info.hash, pe_undef_fill, "");
  2534. /* sort items */
  2535. qsort (udef_table, undef_count, sizeof (struct key_value), undef_sort_cmp);
  2536. }
  2537. static void
  2538. add_bfd_to_link (bfd *abfd, const char *name, struct bfd_link_info *linfo)
  2539. {
  2540. lang_input_statement_type *fake_file;
  2541. fake_file = lang_add_input_file (name,
  2542. lang_input_file_is_fake_enum,
  2543. NULL);
  2544. fake_file->the_bfd = abfd;
  2545. ldlang_add_file (fake_file);
  2546. if (!bfd_link_add_symbols (abfd, linfo))
  2547. einfo ("%Xaddsym %s: %E\n", name);
  2548. }
  2549. void
  2550. pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *linfo)
  2551. {
  2552. int i, j;
  2553. def_file_module *module;
  2554. def_file_import *imp;
  2555. pe_dll_id_target (bfd_get_target (output_bfd));
  2556. if (!pe_def_file)
  2557. return;
  2558. imp = pe_def_file->imports;
  2559. pe_create_undef_table ();
  2560. for (module = pe_def_file->modules; module; module = module->next)
  2561. {
  2562. int do_this_dll = 0;
  2563. for (i = 0; i < pe_def_file->num_imports && imp[i].module != module; i++)
  2564. ;
  2565. if (i >= pe_def_file->num_imports)
  2566. continue;
  2567. dll_filename = module->name;
  2568. dll_symname = xstrdup (module->name);
  2569. for (j = 0; dll_symname[j]; j++)
  2570. if (!ISALNUM (dll_symname[j]))
  2571. dll_symname[j] = '_';
  2572. for (; i < pe_def_file->num_imports && imp[i].module == module; i++)
  2573. {
  2574. def_file_export exp;
  2575. struct bfd_link_hash_entry *blhe;
  2576. int lead_at = (*imp[i].internal_name == '@');
  2577. /* See if we need this import. */
  2578. size_t len = strlen (imp[i].internal_name);
  2579. char *name = xmalloc (len + 2 + 6);
  2580. bfd_boolean include_jmp_stub = FALSE;
  2581. bfd_boolean is_cdecl = FALSE;
  2582. bfd_boolean is_undef = FALSE;
  2583. if (!lead_at && strchr (imp[i].internal_name, '@') == NULL)
  2584. is_cdecl = TRUE;
  2585. if (lead_at)
  2586. sprintf (name, "%s", imp[i].internal_name);
  2587. else
  2588. sprintf (name, "%s%s",U (""), imp[i].internal_name);
  2589. blhe = bfd_link_hash_lookup (linfo->hash, name,
  2590. FALSE, FALSE, FALSE);
  2591. /* Include the jump stub for <sym> only if the <sym>
  2592. is undefined. */
  2593. if (!blhe || (blhe && blhe->type != bfd_link_hash_undefined))
  2594. {
  2595. if (lead_at)
  2596. sprintf (name, "%s%s", "__imp_", imp[i].internal_name);
  2597. else
  2598. sprintf (name, "%s%s%s", "__imp_", U (""),
  2599. imp[i].internal_name);
  2600. blhe = bfd_link_hash_lookup (linfo->hash, name,
  2601. FALSE, FALSE, FALSE);
  2602. if (blhe)
  2603. is_undef = (blhe->type == bfd_link_hash_undefined);
  2604. }
  2605. else
  2606. {
  2607. include_jmp_stub = TRUE;
  2608. is_undef = (blhe->type == bfd_link_hash_undefined);
  2609. }
  2610. if (is_cdecl && (!blhe || (blhe && blhe->type != bfd_link_hash_undefined)))
  2611. {
  2612. sprintf (name, "%s%s",U (""), imp[i].internal_name);
  2613. blhe = pe_find_cdecl_alias_match (linfo, name);
  2614. include_jmp_stub = TRUE;
  2615. if (blhe)
  2616. is_undef = (blhe->type == bfd_link_hash_undefined);
  2617. }
  2618. free (name);
  2619. if (is_undef)
  2620. {
  2621. bfd *one;
  2622. /* We do. */
  2623. if (!do_this_dll)
  2624. {
  2625. bfd *ar_head = make_head (output_bfd);
  2626. add_bfd_to_link (ar_head, ar_head->filename, linfo);
  2627. do_this_dll = 1;
  2628. }
  2629. exp.internal_name = imp[i].internal_name;
  2630. exp.name = imp[i].name;
  2631. exp.its_name = imp[i].its_name;
  2632. exp.ordinal = imp[i].ordinal;
  2633. exp.hint = exp.ordinal >= 0 ? exp.ordinal : 0;
  2634. exp.flag_private = 0;
  2635. exp.flag_constant = 0;
  2636. exp.flag_data = imp[i].data;
  2637. exp.flag_noname = exp.name ? 0 : 1;
  2638. one = make_one (&exp, output_bfd, (! exp.flag_data) && include_jmp_stub);
  2639. add_bfd_to_link (one, one->filename, linfo);
  2640. }
  2641. }
  2642. if (do_this_dll)
  2643. {
  2644. bfd *ar_tail = make_tail (output_bfd);
  2645. add_bfd_to_link (ar_tail, ar_tail->filename, linfo);
  2646. }
  2647. free (dll_symname);
  2648. }
  2649. while (undef_count)
  2650. {
  2651. --undef_count;
  2652. free (udef_table[undef_count].key);
  2653. }
  2654. free (udef_table);
  2655. }
  2656. /* We were handed a *.DLL file. Parse it and turn it into a set of
  2657. IMPORTS directives in the def file. Return TRUE if the file was
  2658. handled, FALSE if not. */
  2659. static unsigned int
  2660. pe_get16 (bfd *abfd, int where)
  2661. {
  2662. unsigned char b[2];
  2663. bfd_seek (abfd, (file_ptr) where, SEEK_SET);
  2664. bfd_bread (b, (bfd_size_type) 2, abfd);
  2665. return b[0] + (b[1] << 8);
  2666. }
  2667. static unsigned int
  2668. pe_get32 (bfd *abfd, int where)
  2669. {
  2670. unsigned char b[4];
  2671. bfd_seek (abfd, (file_ptr) where, SEEK_SET);
  2672. bfd_bread (b, (bfd_size_type) 4, abfd);
  2673. return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
  2674. }
  2675. static unsigned int
  2676. pe_as32 (void *ptr)
  2677. {
  2678. unsigned char *b = ptr;
  2679. return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
  2680. }
  2681. bfd_boolean
  2682. pe_implied_import_dll (const char *filename)
  2683. {
  2684. bfd *dll;
  2685. bfd_vma pe_header_offset, opthdr_ofs, num_entries, i;
  2686. bfd_vma export_rva, export_size, nsections, secptr, expptr;
  2687. bfd_vma exp_funcbase;
  2688. unsigned char *expdata;
  2689. char *erva;
  2690. bfd_vma name_rvas, nexp;
  2691. const char *dllname;
  2692. /* Initialization with start > end guarantees that is_data
  2693. will not be set by mistake, and avoids compiler warning. */
  2694. bfd_vma data_start = 1;
  2695. bfd_vma data_end = 0;
  2696. bfd_vma rdata_start = 1;
  2697. bfd_vma rdata_end = 0;
  2698. bfd_vma bss_start = 1;
  2699. bfd_vma bss_end = 0;
  2700. /* No, I can't use bfd here. kernel32.dll puts its export table in
  2701. the middle of the .rdata section. */
  2702. dll = bfd_openr (filename, pe_details->target_name);
  2703. if (!dll)
  2704. {
  2705. einfo ("%Xopen %s: %E\n", filename);
  2706. return FALSE;
  2707. }
  2708. /* PEI dlls seem to be bfd_objects. */
  2709. if (!bfd_check_format (dll, bfd_object))
  2710. {
  2711. einfo ("%X%s: this doesn't appear to be a DLL\n", filename);
  2712. return FALSE;
  2713. }
  2714. /* Get pe_header, optional header and numbers of directory entries. */
  2715. pe_header_offset = pe_get32 (dll, 0x3c);
  2716. opthdr_ofs = pe_header_offset + 4 + 20;
  2717. #ifdef pe_use_x86_64
  2718. num_entries = pe_get32 (dll, opthdr_ofs + 92 + 4 * 4); /* & NumberOfRvaAndSizes. */
  2719. #else
  2720. num_entries = pe_get32 (dll, opthdr_ofs + 92);
  2721. #endif
  2722. /* No import or export directory entry. */
  2723. if (num_entries < 1)
  2724. return FALSE;
  2725. #ifdef pe_use_x86_64
  2726. export_rva = pe_get32 (dll, opthdr_ofs + 96 + 4 * 4);
  2727. export_size = pe_get32 (dll, opthdr_ofs + 100 + 4 * 4);
  2728. #else
  2729. export_rva = pe_get32 (dll, opthdr_ofs + 96);
  2730. export_size = pe_get32 (dll, opthdr_ofs + 100);
  2731. #endif
  2732. /* No export table - nothing to export. */
  2733. if (export_size == 0)
  2734. return FALSE;
  2735. nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
  2736. secptr = (pe_header_offset + 4 + 20 +
  2737. pe_get16 (dll, pe_header_offset + 4 + 16));
  2738. expptr = 0;
  2739. /* Get the rva and size of the export section. */
  2740. for (i = 0; i < nsections; i++)
  2741. {
  2742. char sname[8];
  2743. bfd_vma secptr1 = secptr + 40 * i;
  2744. bfd_vma vaddr = pe_get32 (dll, secptr1 + 12);
  2745. bfd_vma vsize = pe_get32 (dll, secptr1 + 16);
  2746. bfd_vma fptr = pe_get32 (dll, secptr1 + 20);
  2747. bfd_seek (dll, (file_ptr) secptr1, SEEK_SET);
  2748. bfd_bread (sname, (bfd_size_type) 8, dll);
  2749. if (vaddr <= export_rva && vaddr + vsize > export_rva)
  2750. {
  2751. expptr = fptr + (export_rva - vaddr);
  2752. if (export_rva + export_size > vaddr + vsize)
  2753. export_size = vsize - (export_rva - vaddr);
  2754. break;
  2755. }
  2756. }
  2757. /* Scan sections and store the base and size of the
  2758. data and bss segments in data/base_start/end. */
  2759. for (i = 0; i < nsections; i++)
  2760. {
  2761. bfd_vma secptr1 = secptr + 40 * i;
  2762. bfd_vma vsize = pe_get32 (dll, secptr1 + 8);
  2763. bfd_vma vaddr = pe_get32 (dll, secptr1 + 12);
  2764. bfd_vma flags = pe_get32 (dll, secptr1 + 36);
  2765. char sec_name[9];
  2766. sec_name[8] = '\0';
  2767. bfd_seek (dll, (file_ptr) secptr1 + 0, SEEK_SET);
  2768. bfd_bread (sec_name, (bfd_size_type) 8, dll);
  2769. if (strcmp(sec_name,".data") == 0)
  2770. {
  2771. data_start = vaddr;
  2772. data_end = vaddr + vsize;
  2773. if (pe_dll_extra_pe_debug)
  2774. printf ("%s %s: 0x%08lx-0x%08lx (0x%08lx)\n",
  2775. __FUNCTION__, sec_name, (unsigned long) vaddr,
  2776. (unsigned long) (vaddr + vsize), (unsigned long) flags);
  2777. }
  2778. else if (strcmp(sec_name,".rdata") == 0)
  2779. {
  2780. rdata_start = vaddr;
  2781. rdata_end = vaddr + vsize;
  2782. if (pe_dll_extra_pe_debug)
  2783. printf ("%s %s: 0x%08lx-0x%08lx (0x%08lx)\n",
  2784. __FUNCTION__, sec_name, (unsigned long) vaddr,
  2785. (unsigned long) (vaddr + vsize), (unsigned long) flags);
  2786. }
  2787. else if (strcmp (sec_name,".bss") == 0)
  2788. {
  2789. bss_start = vaddr;
  2790. bss_end = vaddr + vsize;
  2791. if (pe_dll_extra_pe_debug)
  2792. printf ("%s %s: 0x%08lx-0x%08lx (0x%08lx)\n",
  2793. __FUNCTION__, sec_name, (unsigned long) vaddr,
  2794. (unsigned long) (vaddr + vsize), (unsigned long) flags);
  2795. }
  2796. }
  2797. expdata = xmalloc (export_size);
  2798. bfd_seek (dll, (file_ptr) expptr, SEEK_SET);
  2799. bfd_bread (expdata, (bfd_size_type) export_size, dll);
  2800. erva = (char *) expdata - export_rva;
  2801. if (pe_def_file == 0)
  2802. pe_def_file = def_file_empty ();
  2803. nexp = pe_as32 (expdata + 24);
  2804. name_rvas = pe_as32 (expdata + 32);
  2805. exp_funcbase = pe_as32 (expdata + 28);
  2806. /* Use internal dll name instead of filename
  2807. to enable symbolic dll linking. */
  2808. dllname = erva + pe_as32 (expdata + 12);
  2809. /* Check to see if the dll has already been added to
  2810. the definition list and if so return without error.
  2811. This avoids multiple symbol definitions. */
  2812. if (def_get_module (pe_def_file, dllname))
  2813. {
  2814. if (pe_dll_extra_pe_debug)
  2815. printf ("%s is already loaded\n", dllname);
  2816. return TRUE;
  2817. }
  2818. /* Iterate through the list of symbols. */
  2819. for (i = 0; i < nexp; i++)
  2820. {
  2821. /* Pointer to the names vector. */
  2822. bfd_vma name_rva = pe_as32 (erva + name_rvas + i * 4);
  2823. def_file_import *imp;
  2824. /* Pointer to the function address vector. */
  2825. bfd_vma func_rva = pe_as32 (erva + exp_funcbase + i * 4);
  2826. int is_data = 0;
  2827. /* Skip unwanted symbols, which are
  2828. exported in buggy auto-import releases. */
  2829. if (! CONST_STRNEQ (erva + name_rva, "__nm_"))
  2830. {
  2831. int is_dup = 0;
  2832. /* is_data is true if the address is in the data, rdata or bss
  2833. segment. */
  2834. is_data =
  2835. (func_rva >= data_start && func_rva < data_end)
  2836. || (func_rva >= rdata_start && func_rva < rdata_end)
  2837. || (func_rva >= bss_start && func_rva < bss_end);
  2838. imp = def_file_add_import (pe_def_file, erva + name_rva,
  2839. dllname, i, NULL, NULL, &is_dup);
  2840. /* Mark symbol type. */
  2841. if (!is_dup)
  2842. imp->data = is_data;
  2843. if (pe_dll_extra_pe_debug)
  2844. printf ("%s dll-name: %s sym: %s addr: 0x%lx %s\n",
  2845. __FUNCTION__, dllname, erva + name_rva,
  2846. (unsigned long) func_rva, is_data ? "(data)" : "");
  2847. }
  2848. }
  2849. return TRUE;
  2850. }
  2851. void
  2852. pe_output_file_set_long_section_names (bfd *abfd)
  2853. {
  2854. if (pe_use_coff_long_section_names < 0)
  2855. return;
  2856. if (!bfd_coff_set_long_section_names (abfd, pe_use_coff_long_section_names))
  2857. einfo (_("%XError: can't use long section names on this arch\n"));
  2858. }
  2859. /* These are the main functions, called from the emulation. The first
  2860. is called after the bfds are read, so we can guess at how much space
  2861. we need. The second is called after everything is placed, so we
  2862. can put the right values in place. */
  2863. void
  2864. pe_dll_build_sections (bfd *abfd, struct bfd_link_info *info)
  2865. {
  2866. pe_dll_id_target (bfd_get_target (abfd));
  2867. pe_output_file_set_long_section_names (abfd);
  2868. process_def_file_and_drectve (abfd, info);
  2869. if (pe_def_file->num_exports == 0 && !bfd_link_pic (info))
  2870. return;
  2871. generate_edata (abfd, info);
  2872. build_filler_bfd (1);
  2873. pe_output_file_set_long_section_names (filler_bfd);
  2874. }
  2875. void
  2876. pe_exe_build_sections (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
  2877. {
  2878. pe_dll_id_target (bfd_get_target (abfd));
  2879. pe_output_file_set_long_section_names (abfd);
  2880. build_filler_bfd (0);
  2881. pe_output_file_set_long_section_names (filler_bfd);
  2882. }
  2883. void
  2884. pe_dll_fill_sections (bfd *abfd, struct bfd_link_info *info)
  2885. {
  2886. pe_dll_id_target (bfd_get_target (abfd));
  2887. pe_output_file_set_long_section_names (abfd);
  2888. image_base = pe_data (abfd)->pe_opthdr.ImageBase;
  2889. generate_reloc (abfd, info);
  2890. if (reloc_sz > 0)
  2891. {
  2892. bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
  2893. /* Resize the sections. */
  2894. lang_reset_memory_regions ();
  2895. lang_size_sections (NULL, TRUE);
  2896. /* Redo special stuff. */
  2897. ldemul_after_allocation ();
  2898. /* Do the assignments again. */
  2899. lang_do_assignments (lang_final_phase_enum);
  2900. }
  2901. fill_edata (abfd, info);
  2902. if (bfd_link_dll (info))
  2903. pe_data (abfd)->dll = 1;
  2904. edata_s->contents = edata_d;
  2905. reloc_s->contents = reloc_d;
  2906. }
  2907. void
  2908. pe_exe_fill_sections (bfd *abfd, struct bfd_link_info *info)
  2909. {
  2910. pe_dll_id_target (bfd_get_target (abfd));
  2911. pe_output_file_set_long_section_names (abfd);
  2912. image_base = pe_data (abfd)->pe_opthdr.ImageBase;
  2913. generate_reloc (abfd, info);
  2914. if (reloc_sz > 0)
  2915. {
  2916. bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
  2917. /* Resize the sections. */
  2918. lang_reset_memory_regions ();
  2919. lang_size_sections (NULL, TRUE);
  2920. /* Redo special stuff. */
  2921. ldemul_after_allocation ();
  2922. /* Do the assignments again. */
  2923. lang_do_assignments (lang_final_phase_enum);
  2924. }
  2925. reloc_s->contents = reloc_d;
  2926. }
  2927. bfd_boolean
  2928. pe_bfd_is_dll (bfd *abfd)
  2929. {
  2930. return (bfd_get_format (abfd) == bfd_object
  2931. && obj_pe (abfd)
  2932. && pe_data (abfd)->dll);
  2933. }