pe-dll.c 99 KB

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