peicode.h 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  1. /* Support for the generic parts of PE/PEI, for BFD.
  2. Copyright (C) 1995-2022 Free Software Foundation, Inc.
  3. Written by Cygnus Solutions.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /* Most of this hacked by Steve Chamberlain,
  18. sac@cygnus.com
  19. PE/PEI rearrangement (and code added): Donn Terry
  20. Softway Systems, Inc. */
  21. /* Hey look, some documentation [and in a place you expect to find it]!
  22. The main reference for the pei format is "Microsoft Portable Executable
  23. and Common Object File Format Specification 4.1". Get it if you need to
  24. do some serious hacking on this code.
  25. Another reference:
  26. "Peering Inside the PE: A Tour of the Win32 Portable Executable
  27. File Format", MSJ 1994, Volume 9.
  28. The *sole* difference between the pe format and the pei format is that the
  29. latter has an MSDOS 2.0 .exe header on the front that prints the message
  30. "This app must be run under Windows." (or some such).
  31. (FIXME: Whether that statement is *really* true or not is unknown.
  32. Are there more subtle differences between pe and pei formats?
  33. For now assume there aren't. If you find one, then for God sakes
  34. document it here!)
  35. The Microsoft docs use the word "image" instead of "executable" because
  36. the former can also refer to a DLL (shared library). Confusion can arise
  37. because the `i' in `pei' also refers to "image". The `pe' format can
  38. also create images (i.e. executables), it's just that to run on a win32
  39. system you need to use the pei format.
  40. FIXME: Please add more docs here so the next poor fool that has to hack
  41. on this code has a chance of getting something accomplished without
  42. wasting too much time. */
  43. #include "libpei.h"
  44. static bool (*pe_saved_coff_bfd_print_private_bfd_data) (bfd *, void *) =
  45. #ifndef coff_bfd_print_private_bfd_data
  46. NULL;
  47. #else
  48. coff_bfd_print_private_bfd_data;
  49. #undef coff_bfd_print_private_bfd_data
  50. #endif
  51. static bool pe_print_private_bfd_data (bfd *, void *);
  52. #define coff_bfd_print_private_bfd_data pe_print_private_bfd_data
  53. static bool (*pe_saved_coff_bfd_copy_private_bfd_data) (bfd *, bfd *) =
  54. #ifndef coff_bfd_copy_private_bfd_data
  55. NULL;
  56. #else
  57. coff_bfd_copy_private_bfd_data;
  58. #undef coff_bfd_copy_private_bfd_data
  59. #endif
  60. static bool pe_bfd_copy_private_bfd_data (bfd *, bfd *);
  61. #define coff_bfd_copy_private_bfd_data pe_bfd_copy_private_bfd_data
  62. #define coff_mkobject pe_mkobject
  63. #define coff_mkobject_hook pe_mkobject_hook
  64. #ifdef COFF_IMAGE_WITH_PE
  65. /* This structure contains static variables used by the ILF code. */
  66. typedef asection * asection_ptr;
  67. typedef struct
  68. {
  69. bfd * abfd;
  70. bfd_byte * data;
  71. struct bfd_in_memory * bim;
  72. unsigned short magic;
  73. arelent * reltab;
  74. unsigned int relcount;
  75. coff_symbol_type * sym_cache;
  76. coff_symbol_type * sym_ptr;
  77. unsigned int sym_index;
  78. unsigned int * sym_table;
  79. unsigned int * table_ptr;
  80. combined_entry_type * native_syms;
  81. combined_entry_type * native_ptr;
  82. coff_symbol_type ** sym_ptr_table;
  83. coff_symbol_type ** sym_ptr_ptr;
  84. unsigned int sec_index;
  85. char * string_table;
  86. char * string_ptr;
  87. char * end_string_ptr;
  88. SYMENT * esym_table;
  89. SYMENT * esym_ptr;
  90. struct internal_reloc * int_reltab;
  91. }
  92. pe_ILF_vars;
  93. #endif /* COFF_IMAGE_WITH_PE */
  94. bfd_cleanup coff_real_object_p
  95. (bfd *, unsigned, struct internal_filehdr *, struct internal_aouthdr *);
  96. #ifndef NO_COFF_RELOCS
  97. static void
  98. coff_swap_reloc_in (bfd * abfd, void * src, void * dst)
  99. {
  100. RELOC *reloc_src = (RELOC *) src;
  101. struct internal_reloc *reloc_dst = (struct internal_reloc *) dst;
  102. reloc_dst->r_vaddr = H_GET_32 (abfd, reloc_src->r_vaddr);
  103. reloc_dst->r_symndx = H_GET_S32 (abfd, reloc_src->r_symndx);
  104. reloc_dst->r_type = H_GET_16 (abfd, reloc_src->r_type);
  105. #ifdef SWAP_IN_RELOC_OFFSET
  106. reloc_dst->r_offset = SWAP_IN_RELOC_OFFSET (abfd, reloc_src->r_offset);
  107. #endif
  108. }
  109. static unsigned int
  110. coff_swap_reloc_out (bfd * abfd, void * src, void * dst)
  111. {
  112. struct internal_reloc *reloc_src = (struct internal_reloc *) src;
  113. struct external_reloc *reloc_dst = (struct external_reloc *) dst;
  114. H_PUT_32 (abfd, reloc_src->r_vaddr, reloc_dst->r_vaddr);
  115. H_PUT_32 (abfd, reloc_src->r_symndx, reloc_dst->r_symndx);
  116. H_PUT_16 (abfd, reloc_src->r_type, reloc_dst->r_type);
  117. #ifdef SWAP_OUT_RELOC_OFFSET
  118. SWAP_OUT_RELOC_OFFSET (abfd, reloc_src->r_offset, reloc_dst->r_offset);
  119. #endif
  120. #ifdef SWAP_OUT_RELOC_EXTRA
  121. SWAP_OUT_RELOC_EXTRA (abfd, reloc_src, reloc_dst);
  122. #endif
  123. return RELSZ;
  124. }
  125. #endif /* not NO_COFF_RELOCS */
  126. #ifdef COFF_IMAGE_WITH_PE
  127. #undef FILHDR
  128. #define FILHDR struct external_PEI_IMAGE_hdr
  129. #endif
  130. static void
  131. coff_swap_filehdr_in (bfd * abfd, void * src, void * dst)
  132. {
  133. FILHDR *filehdr_src = (FILHDR *) src;
  134. struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
  135. filehdr_dst->f_magic = H_GET_16 (abfd, filehdr_src->f_magic);
  136. filehdr_dst->f_nscns = H_GET_16 (abfd, filehdr_src->f_nscns);
  137. filehdr_dst->f_timdat = H_GET_32 (abfd, filehdr_src->f_timdat);
  138. filehdr_dst->f_nsyms = H_GET_32 (abfd, filehdr_src->f_nsyms);
  139. filehdr_dst->f_flags = H_GET_16 (abfd, filehdr_src->f_flags);
  140. filehdr_dst->f_symptr = H_GET_32 (abfd, filehdr_src->f_symptr);
  141. /* Other people's tools sometimes generate headers with an nsyms but
  142. a zero symptr. */
  143. if (filehdr_dst->f_nsyms != 0 && filehdr_dst->f_symptr == 0)
  144. {
  145. filehdr_dst->f_nsyms = 0;
  146. filehdr_dst->f_flags |= F_LSYMS;
  147. }
  148. filehdr_dst->f_opthdr = H_GET_16 (abfd, filehdr_src-> f_opthdr);
  149. }
  150. #ifdef COFF_IMAGE_WITH_PE
  151. # define coff_swap_filehdr_out _bfd_XXi_only_swap_filehdr_out
  152. #elif defined COFF_WITH_pex64
  153. # define coff_swap_filehdr_out _bfd_pex64_only_swap_filehdr_out
  154. #elif defined COFF_WITH_pep
  155. # define coff_swap_filehdr_out _bfd_pep_only_swap_filehdr_out
  156. #else
  157. # define coff_swap_filehdr_out _bfd_pe_only_swap_filehdr_out
  158. #endif
  159. static void
  160. coff_swap_scnhdr_in (bfd * abfd, void * ext, void * in)
  161. {
  162. SCNHDR *scnhdr_ext = (SCNHDR *) ext;
  163. struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
  164. memcpy (scnhdr_int->s_name, scnhdr_ext->s_name, sizeof (scnhdr_int->s_name));
  165. scnhdr_int->s_vaddr = GET_SCNHDR_VADDR (abfd, scnhdr_ext->s_vaddr);
  166. scnhdr_int->s_paddr = GET_SCNHDR_PADDR (abfd, scnhdr_ext->s_paddr);
  167. scnhdr_int->s_size = GET_SCNHDR_SIZE (abfd, scnhdr_ext->s_size);
  168. scnhdr_int->s_scnptr = GET_SCNHDR_SCNPTR (abfd, scnhdr_ext->s_scnptr);
  169. scnhdr_int->s_relptr = GET_SCNHDR_RELPTR (abfd, scnhdr_ext->s_relptr);
  170. scnhdr_int->s_lnnoptr = GET_SCNHDR_LNNOPTR (abfd, scnhdr_ext->s_lnnoptr);
  171. scnhdr_int->s_flags = H_GET_32 (abfd, scnhdr_ext->s_flags);
  172. /* MS handles overflow of line numbers by carrying into the reloc
  173. field (it appears). Since it's supposed to be zero for PE
  174. *IMAGE* format, that's safe. This is still a bit iffy. */
  175. #ifdef COFF_IMAGE_WITH_PE
  176. scnhdr_int->s_nlnno = (H_GET_16 (abfd, scnhdr_ext->s_nlnno)
  177. + (H_GET_16 (abfd, scnhdr_ext->s_nreloc) << 16));
  178. scnhdr_int->s_nreloc = 0;
  179. #else
  180. scnhdr_int->s_nreloc = H_GET_16 (abfd, scnhdr_ext->s_nreloc);
  181. scnhdr_int->s_nlnno = H_GET_16 (abfd, scnhdr_ext->s_nlnno);
  182. #endif
  183. if (scnhdr_int->s_vaddr != 0)
  184. {
  185. scnhdr_int->s_vaddr += pe_data (abfd)->pe_opthdr.ImageBase;
  186. /* Do not cut upper 32-bits for 64-bit vma. */
  187. #if !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64)
  188. scnhdr_int->s_vaddr &= 0xffffffff;
  189. #endif
  190. }
  191. #ifndef COFF_NO_HACK_SCNHDR_SIZE
  192. /* If this section holds uninitialized data and is from an object file
  193. or from an executable image that has not initialized the field,
  194. or if the image is an executable file and the physical size is padded,
  195. use the virtual size (stored in s_paddr) instead. */
  196. if (scnhdr_int->s_paddr > 0
  197. && (((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0
  198. && (! bfd_pei_p (abfd) || scnhdr_int->s_size == 0))
  199. || (bfd_pei_p (abfd) && (scnhdr_int->s_size > scnhdr_int->s_paddr))))
  200. /* This code used to set scnhdr_int->s_paddr to 0. However,
  201. coff_set_alignment_hook stores s_paddr in virt_size, which
  202. only works if it correctly holds the virtual size of the
  203. section. */
  204. scnhdr_int->s_size = scnhdr_int->s_paddr;
  205. #endif
  206. }
  207. static bool
  208. pe_mkobject (bfd * abfd)
  209. {
  210. pe_data_type *pe;
  211. size_t amt = sizeof (pe_data_type);
  212. abfd->tdata.pe_obj_data = (struct pe_tdata *) bfd_zalloc (abfd, amt);
  213. if (abfd->tdata.pe_obj_data == 0)
  214. return false;
  215. pe = pe_data (abfd);
  216. pe->coff.pe = 1;
  217. /* in_reloc_p is architecture dependent. */
  218. pe->in_reloc_p = in_reloc_p;
  219. /* Default DOS message string. */
  220. pe->dos_message[0] = 0x0eba1f0e;
  221. pe->dos_message[1] = 0xcd09b400;
  222. pe->dos_message[2] = 0x4c01b821;
  223. pe->dos_message[3] = 0x685421cd;
  224. pe->dos_message[4] = 0x70207369;
  225. pe->dos_message[5] = 0x72676f72;
  226. pe->dos_message[6] = 0x63206d61;
  227. pe->dos_message[7] = 0x6f6e6e61;
  228. pe->dos_message[8] = 0x65622074;
  229. pe->dos_message[9] = 0x6e757220;
  230. pe->dos_message[10] = 0x206e6920;
  231. pe->dos_message[11] = 0x20534f44;
  232. pe->dos_message[12] = 0x65646f6d;
  233. pe->dos_message[13] = 0x0a0d0d2e;
  234. pe->dos_message[14] = 0x24;
  235. pe->dos_message[15] = 0x0;
  236. memset (& pe->pe_opthdr, 0, sizeof pe->pe_opthdr);
  237. return true;
  238. }
  239. /* Create the COFF backend specific information. */
  240. static void *
  241. pe_mkobject_hook (bfd * abfd,
  242. void * filehdr,
  243. void * aouthdr ATTRIBUTE_UNUSED)
  244. {
  245. struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  246. pe_data_type *pe;
  247. if (! pe_mkobject (abfd))
  248. return NULL;
  249. pe = pe_data (abfd);
  250. pe->coff.sym_filepos = internal_f->f_symptr;
  251. /* These members communicate important constants about the symbol
  252. table to GDB's symbol-reading code. These `constants'
  253. unfortunately vary among coff implementations... */
  254. pe->coff.local_n_btmask = N_BTMASK;
  255. pe->coff.local_n_btshft = N_BTSHFT;
  256. pe->coff.local_n_tmask = N_TMASK;
  257. pe->coff.local_n_tshift = N_TSHIFT;
  258. pe->coff.local_symesz = SYMESZ;
  259. pe->coff.local_auxesz = AUXESZ;
  260. pe->coff.local_linesz = LINESZ;
  261. pe->coff.timestamp = internal_f->f_timdat;
  262. obj_raw_syment_count (abfd) =
  263. obj_conv_table_size (abfd) =
  264. internal_f->f_nsyms;
  265. pe->real_flags = internal_f->f_flags;
  266. if ((internal_f->f_flags & F_DLL) != 0)
  267. pe->dll = 1;
  268. if ((internal_f->f_flags & IMAGE_FILE_DEBUG_STRIPPED) == 0)
  269. abfd->flags |= HAS_DEBUG;
  270. #ifdef COFF_IMAGE_WITH_PE
  271. if (aouthdr)
  272. pe->pe_opthdr = ((struct internal_aouthdr *) aouthdr)->pe;
  273. #endif
  274. #ifdef ARM
  275. if (! _bfd_coff_arm_set_private_flags (abfd, internal_f->f_flags))
  276. coff_data (abfd) ->flags = 0;
  277. #endif
  278. memcpy (pe->dos_message, internal_f->pe.dos_message,
  279. sizeof (pe->dos_message));
  280. return (void *) pe;
  281. }
  282. static bool
  283. pe_print_private_bfd_data (bfd *abfd, void * vfile)
  284. {
  285. FILE *file = (FILE *) vfile;
  286. if (!_bfd_XX_print_private_bfd_data_common (abfd, vfile))
  287. return false;
  288. if (pe_saved_coff_bfd_print_private_bfd_data == NULL)
  289. return true;
  290. fputc ('\n', file);
  291. return pe_saved_coff_bfd_print_private_bfd_data (abfd, vfile);
  292. }
  293. /* Copy any private info we understand from the input bfd
  294. to the output bfd. */
  295. static bool
  296. pe_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
  297. {
  298. /* PR binutils/716: Copy the large address aware flag.
  299. XXX: Should we be copying other flags or other fields in the pe_data()
  300. structure ? */
  301. if (pe_data (obfd) != NULL
  302. && pe_data (ibfd) != NULL
  303. && pe_data (ibfd)->real_flags & IMAGE_FILE_LARGE_ADDRESS_AWARE)
  304. pe_data (obfd)->real_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
  305. if (!_bfd_XX_bfd_copy_private_bfd_data_common (ibfd, obfd))
  306. return false;
  307. if (pe_saved_coff_bfd_copy_private_bfd_data)
  308. return pe_saved_coff_bfd_copy_private_bfd_data (ibfd, obfd);
  309. return true;
  310. }
  311. #define coff_bfd_copy_private_section_data \
  312. _bfd_XX_bfd_copy_private_section_data
  313. #define coff_get_symbol_info _bfd_XX_get_symbol_info
  314. #ifdef COFF_IMAGE_WITH_PE
  315. /* Code to handle Microsoft's Image Library Format.
  316. Also known as LINK6 format.
  317. Documentation about this format can be found at:
  318. http://msdn.microsoft.com/library/specs/pecoff_section8.htm */
  319. /* The following constants specify the sizes of the various data
  320. structures that we have to create in order to build a bfd describing
  321. an ILF object file. The final "+ 1" in the definitions of SIZEOF_IDATA6
  322. and SIZEOF_IDATA7 below is to allow for the possibility that we might
  323. need a padding byte in order to ensure 16 bit alignment for the section's
  324. contents.
  325. The value for SIZEOF_ILF_STRINGS is computed as follows:
  326. There will be NUM_ILF_SECTIONS section symbols. Allow 9 characters
  327. per symbol for their names (longest section name is .idata$x).
  328. There will be two symbols for the imported value, one the symbol name
  329. and one with _imp__ prefixed. Allowing for the terminating nul's this
  330. is strlen (symbol_name) * 2 + 8 + 21 + strlen (source_dll).
  331. The strings in the string table must start STRING__SIZE_SIZE bytes into
  332. the table in order to for the string lookup code in coffgen/coffcode to
  333. work. */
  334. #define NUM_ILF_RELOCS 8
  335. #define NUM_ILF_SECTIONS 6
  336. #define NUM_ILF_SYMS (2 + NUM_ILF_SECTIONS)
  337. #define SIZEOF_ILF_SYMS (NUM_ILF_SYMS * sizeof (* vars.sym_cache))
  338. #define SIZEOF_ILF_SYM_TABLE (NUM_ILF_SYMS * sizeof (* vars.sym_table))
  339. #define SIZEOF_ILF_NATIVE_SYMS (NUM_ILF_SYMS * sizeof (* vars.native_syms))
  340. #define SIZEOF_ILF_SYM_PTR_TABLE (NUM_ILF_SYMS * sizeof (* vars.sym_ptr_table))
  341. #define SIZEOF_ILF_EXT_SYMS (NUM_ILF_SYMS * sizeof (* vars.esym_table))
  342. #define SIZEOF_ILF_RELOCS (NUM_ILF_RELOCS * sizeof (* vars.reltab))
  343. #define SIZEOF_ILF_INT_RELOCS (NUM_ILF_RELOCS * sizeof (* vars.int_reltab))
  344. #define SIZEOF_ILF_STRINGS (strlen (symbol_name) * 2 + 8 \
  345. + 21 + strlen (source_dll) \
  346. + NUM_ILF_SECTIONS * 9 \
  347. + STRING_SIZE_SIZE)
  348. #define SIZEOF_IDATA2 (5 * 4)
  349. /* For PEx64 idata4 & 5 have thumb size of 8 bytes. */
  350. #ifdef COFF_WITH_pex64
  351. #define SIZEOF_IDATA4 (2 * 4)
  352. #define SIZEOF_IDATA5 (2 * 4)
  353. #else
  354. #define SIZEOF_IDATA4 (1 * 4)
  355. #define SIZEOF_IDATA5 (1 * 4)
  356. #endif
  357. #define SIZEOF_IDATA6 (2 + strlen (symbol_name) + 1 + 1)
  358. #define SIZEOF_IDATA7 (strlen (source_dll) + 1 + 1)
  359. #define SIZEOF_ILF_SECTIONS (NUM_ILF_SECTIONS * sizeof (struct coff_section_tdata))
  360. #define ILF_DATA_SIZE \
  361. + SIZEOF_ILF_SYMS \
  362. + SIZEOF_ILF_SYM_TABLE \
  363. + SIZEOF_ILF_NATIVE_SYMS \
  364. + SIZEOF_ILF_SYM_PTR_TABLE \
  365. + SIZEOF_ILF_EXT_SYMS \
  366. + SIZEOF_ILF_RELOCS \
  367. + SIZEOF_ILF_INT_RELOCS \
  368. + SIZEOF_ILF_STRINGS \
  369. + SIZEOF_IDATA2 \
  370. + SIZEOF_IDATA4 \
  371. + SIZEOF_IDATA5 \
  372. + SIZEOF_IDATA6 \
  373. + SIZEOF_IDATA7 \
  374. + SIZEOF_ILF_SECTIONS \
  375. + MAX_TEXT_SECTION_SIZE
  376. /* Create an empty relocation against the given symbol. */
  377. static void
  378. pe_ILF_make_a_symbol_reloc (pe_ILF_vars * vars,
  379. bfd_vma address,
  380. bfd_reloc_code_real_type reloc,
  381. struct bfd_symbol ** sym,
  382. unsigned int sym_index)
  383. {
  384. arelent * entry;
  385. struct internal_reloc * internal;
  386. entry = vars->reltab + vars->relcount;
  387. internal = vars->int_reltab + vars->relcount;
  388. entry->address = address;
  389. entry->addend = 0;
  390. entry->howto = bfd_reloc_type_lookup (vars->abfd, reloc);
  391. entry->sym_ptr_ptr = sym;
  392. internal->r_vaddr = address;
  393. internal->r_symndx = sym_index;
  394. internal->r_type = entry->howto ? entry->howto->type : 0;
  395. vars->relcount ++;
  396. BFD_ASSERT (vars->relcount <= NUM_ILF_RELOCS);
  397. }
  398. /* Create an empty relocation against the given section. */
  399. static void
  400. pe_ILF_make_a_reloc (pe_ILF_vars * vars,
  401. bfd_vma address,
  402. bfd_reloc_code_real_type reloc,
  403. asection_ptr sec)
  404. {
  405. pe_ILF_make_a_symbol_reloc (vars, address, reloc, sec->symbol_ptr_ptr,
  406. coff_section_data (vars->abfd, sec)->i);
  407. }
  408. /* Move the queued relocs into the given section. */
  409. static void
  410. pe_ILF_save_relocs (pe_ILF_vars * vars,
  411. asection_ptr sec)
  412. {
  413. /* Make sure that there is somewhere to store the internal relocs. */
  414. if (coff_section_data (vars->abfd, sec) == NULL)
  415. /* We should probably return an error indication here. */
  416. abort ();
  417. coff_section_data (vars->abfd, sec)->relocs = vars->int_reltab;
  418. coff_section_data (vars->abfd, sec)->keep_relocs = true;
  419. sec->relocation = vars->reltab;
  420. sec->reloc_count = vars->relcount;
  421. sec->flags |= SEC_RELOC;
  422. vars->reltab += vars->relcount;
  423. vars->int_reltab += vars->relcount;
  424. vars->relcount = 0;
  425. BFD_ASSERT ((bfd_byte *) vars->int_reltab < (bfd_byte *) vars->string_table);
  426. }
  427. /* Create a global symbol and add it to the relevant tables. */
  428. static void
  429. pe_ILF_make_a_symbol (pe_ILF_vars * vars,
  430. const char * prefix,
  431. const char * symbol_name,
  432. asection_ptr section,
  433. flagword extra_flags)
  434. {
  435. coff_symbol_type * sym;
  436. combined_entry_type * ent;
  437. SYMENT * esym;
  438. unsigned short sclass;
  439. if (extra_flags & BSF_LOCAL)
  440. sclass = C_STAT;
  441. else
  442. sclass = C_EXT;
  443. #ifdef THUMBPEMAGIC
  444. if (vars->magic == THUMBPEMAGIC)
  445. {
  446. if (extra_flags & BSF_FUNCTION)
  447. sclass = C_THUMBEXTFUNC;
  448. else if (extra_flags & BSF_LOCAL)
  449. sclass = C_THUMBSTAT;
  450. else
  451. sclass = C_THUMBEXT;
  452. }
  453. #endif
  454. BFD_ASSERT (vars->sym_index < NUM_ILF_SYMS);
  455. sym = vars->sym_ptr;
  456. ent = vars->native_ptr;
  457. esym = vars->esym_ptr;
  458. /* Copy the symbol's name into the string table. */
  459. sprintf (vars->string_ptr, "%s%s", prefix, symbol_name);
  460. if (section == NULL)
  461. section = bfd_und_section_ptr;
  462. /* Initialise the external symbol. */
  463. H_PUT_32 (vars->abfd, vars->string_ptr - vars->string_table,
  464. esym->e.e.e_offset);
  465. H_PUT_16 (vars->abfd, section->target_index, esym->e_scnum);
  466. esym->e_sclass[0] = sclass;
  467. /* The following initialisations are unnecessary - the memory is
  468. zero initialised. They are just kept here as reminders. */
  469. /* Initialise the internal symbol structure. */
  470. ent->u.syment.n_sclass = sclass;
  471. ent->u.syment.n_scnum = section->target_index;
  472. ent->u.syment._n._n_n._n_offset = (bfd_hostptr_t) sym;
  473. ent->is_sym = true;
  474. sym->symbol.the_bfd = vars->abfd;
  475. sym->symbol.name = vars->string_ptr;
  476. sym->symbol.flags = BSF_EXPORT | BSF_GLOBAL | extra_flags;
  477. sym->symbol.section = section;
  478. sym->native = ent;
  479. * vars->table_ptr = vars->sym_index;
  480. * vars->sym_ptr_ptr = sym;
  481. /* Adjust pointers for the next symbol. */
  482. vars->sym_index ++;
  483. vars->sym_ptr ++;
  484. vars->sym_ptr_ptr ++;
  485. vars->table_ptr ++;
  486. vars->native_ptr ++;
  487. vars->esym_ptr ++;
  488. vars->string_ptr += strlen (symbol_name) + strlen (prefix) + 1;
  489. BFD_ASSERT (vars->string_ptr < vars->end_string_ptr);
  490. }
  491. /* Create a section. */
  492. static asection_ptr
  493. pe_ILF_make_a_section (pe_ILF_vars * vars,
  494. const char * name,
  495. unsigned int size,
  496. flagword extra_flags)
  497. {
  498. asection_ptr sec;
  499. flagword flags;
  500. intptr_t alignment;
  501. sec = bfd_make_section_old_way (vars->abfd, name);
  502. if (sec == NULL)
  503. return NULL;
  504. flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_IN_MEMORY;
  505. bfd_set_section_flags (sec, flags | extra_flags);
  506. bfd_set_section_alignment (sec, 2);
  507. /* Check that we will not run out of space. */
  508. BFD_ASSERT (vars->data + size < vars->bim->buffer + vars->bim->size);
  509. /* Set the section size and contents. The actual
  510. contents are filled in by our parent. */
  511. bfd_set_section_size (sec, (bfd_size_type) size);
  512. sec->contents = vars->data;
  513. sec->target_index = vars->sec_index ++;
  514. /* Advance data pointer in the vars structure. */
  515. vars->data += size;
  516. /* Skip the padding byte if it was not needed.
  517. The logic here is that if the string length is odd,
  518. then the entire string length, including the null byte,
  519. is even and so the extra, padding byte, is not needed. */
  520. if (size & 1)
  521. vars->data --;
  522. /* PR 18758: See note in pe_ILF_buid_a_bfd. We must make sure that we
  523. preserve host alignment requirements. The BFD_ASSERTs in this
  524. functions will warn us if we run out of room, but we should
  525. already have enough padding built in to ILF_DATA_SIZE. */
  526. #if GCC_VERSION >= 3000
  527. alignment = __alignof__ (struct coff_section_tdata);
  528. #else
  529. alignment = 8;
  530. #endif
  531. vars->data
  532. = (bfd_byte *) (((intptr_t) vars->data + alignment - 1) & -alignment);
  533. /* Create a coff_section_tdata structure for our use. */
  534. sec->used_by_bfd = (struct coff_section_tdata *) vars->data;
  535. vars->data += sizeof (struct coff_section_tdata);
  536. BFD_ASSERT (vars->data <= vars->bim->buffer + vars->bim->size);
  537. /* Create a symbol to refer to this section. */
  538. pe_ILF_make_a_symbol (vars, "", name, sec, BSF_LOCAL);
  539. /* Cache the index to the symbol in the coff_section_data structure. */
  540. coff_section_data (vars->abfd, sec)->i = vars->sym_index - 1;
  541. return sec;
  542. }
  543. /* This structure contains the code that goes into the .text section
  544. in order to perform a jump into the DLL lookup table. The entries
  545. in the table are index by the magic number used to represent the
  546. machine type in the PE file. The contents of the data[] arrays in
  547. these entries are stolen from the jtab[] arrays in ld/pe-dll.c.
  548. The SIZE field says how many bytes in the DATA array are actually
  549. used. The OFFSET field says where in the data array the address
  550. of the .idata$5 section should be placed. */
  551. #define MAX_TEXT_SECTION_SIZE 32
  552. typedef struct
  553. {
  554. unsigned short magic;
  555. unsigned char data[MAX_TEXT_SECTION_SIZE];
  556. unsigned int size;
  557. unsigned int offset;
  558. }
  559. jump_table;
  560. static const jump_table jtab[] =
  561. {
  562. #ifdef I386MAGIC
  563. { I386MAGIC,
  564. { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 },
  565. 8, 2
  566. },
  567. #endif
  568. #ifdef AMD64MAGIC
  569. { AMD64MAGIC,
  570. { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 },
  571. 8, 2
  572. },
  573. #endif
  574. #ifdef MC68MAGIC
  575. { MC68MAGIC,
  576. { /* XXX fill me in */ },
  577. 0, 0
  578. },
  579. #endif
  580. #ifdef MIPS_ARCH_MAGIC_WINCE
  581. { MIPS_ARCH_MAGIC_WINCE,
  582. { 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d,
  583. 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 },
  584. 16, 0
  585. },
  586. #endif
  587. #ifdef SH_ARCH_MAGIC_WINCE
  588. { SH_ARCH_MAGIC_WINCE,
  589. { 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40,
  590. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00 },
  591. 12, 8
  592. },
  593. #endif
  594. #ifdef AARCH64MAGIC
  595. /* We don't currently support jumping to DLLs, so if
  596. someone does try emit a runtime trap. Through UDF #0. */
  597. { AARCH64MAGIC,
  598. { 0x00, 0x00, 0x00, 0x00 },
  599. 4, 0
  600. },
  601. #endif
  602. #ifdef ARMPEMAGIC
  603. { ARMPEMAGIC,
  604. { 0x00, 0xc0, 0x9f, 0xe5, 0x00, 0xf0,
  605. 0x9c, 0xe5, 0x00, 0x00, 0x00, 0x00},
  606. 12, 8
  607. },
  608. #endif
  609. #ifdef THUMBPEMAGIC
  610. { THUMBPEMAGIC,
  611. { 0x40, 0xb4, 0x02, 0x4e, 0x36, 0x68, 0xb4, 0x46,
  612. 0x40, 0xbc, 0x60, 0x47, 0x00, 0x00, 0x00, 0x00 },
  613. 16, 12
  614. },
  615. #endif
  616. { 0, { 0 }, 0, 0 }
  617. };
  618. #ifndef NUM_ENTRIES
  619. #define NUM_ENTRIES(a) (sizeof (a) / sizeof (a)[0])
  620. #endif
  621. /* Build a full BFD from the information supplied in a ILF object. */
  622. static bool
  623. pe_ILF_build_a_bfd (bfd * abfd,
  624. unsigned int magic,
  625. char * symbol_name,
  626. char * source_dll,
  627. unsigned int ordinal,
  628. unsigned int types)
  629. {
  630. bfd_byte * ptr;
  631. pe_ILF_vars vars;
  632. struct internal_filehdr internal_f;
  633. unsigned int import_type;
  634. unsigned int import_name_type;
  635. asection_ptr id4, id5, id6 = NULL, text = NULL;
  636. coff_symbol_type ** imp_sym;
  637. unsigned int imp_index;
  638. intptr_t alignment;
  639. /* Decode and verify the types field of the ILF structure. */
  640. import_type = types & 0x3;
  641. import_name_type = (types & 0x1c) >> 2;
  642. switch (import_type)
  643. {
  644. case IMPORT_CODE:
  645. case IMPORT_DATA:
  646. break;
  647. case IMPORT_CONST:
  648. /* XXX code yet to be written. */
  649. /* xgettext:c-format */
  650. _bfd_error_handler (_("%pB: unhandled import type; %x"),
  651. abfd, import_type);
  652. return false;
  653. default:
  654. /* xgettext:c-format */
  655. _bfd_error_handler (_("%pB: unrecognized import type; %x"),
  656. abfd, import_type);
  657. return false;
  658. }
  659. switch (import_name_type)
  660. {
  661. case IMPORT_ORDINAL:
  662. case IMPORT_NAME:
  663. case IMPORT_NAME_NOPREFIX:
  664. case IMPORT_NAME_UNDECORATE:
  665. break;
  666. default:
  667. /* xgettext:c-format */
  668. _bfd_error_handler (_("%pB: unrecognized import name type; %x"),
  669. abfd, import_name_type);
  670. return false;
  671. }
  672. /* Initialise local variables.
  673. Note these are kept in a structure rather than being
  674. declared as statics since bfd frowns on global variables.
  675. We are going to construct the contents of the BFD in memory,
  676. so allocate all the space that we will need right now. */
  677. vars.bim
  678. = (struct bfd_in_memory *) bfd_malloc ((bfd_size_type) sizeof (*vars.bim));
  679. if (vars.bim == NULL)
  680. return false;
  681. ptr = (bfd_byte *) bfd_zmalloc ((bfd_size_type) ILF_DATA_SIZE);
  682. vars.bim->buffer = ptr;
  683. vars.bim->size = ILF_DATA_SIZE;
  684. if (ptr == NULL)
  685. goto error_return;
  686. /* Initialise the pointers to regions of the memory and the
  687. other contents of the pe_ILF_vars structure as well. */
  688. vars.sym_cache = (coff_symbol_type *) ptr;
  689. vars.sym_ptr = (coff_symbol_type *) ptr;
  690. vars.sym_index = 0;
  691. ptr += SIZEOF_ILF_SYMS;
  692. vars.sym_table = (unsigned int *) ptr;
  693. vars.table_ptr = (unsigned int *) ptr;
  694. ptr += SIZEOF_ILF_SYM_TABLE;
  695. vars.native_syms = (combined_entry_type *) ptr;
  696. vars.native_ptr = (combined_entry_type *) ptr;
  697. ptr += SIZEOF_ILF_NATIVE_SYMS;
  698. vars.sym_ptr_table = (coff_symbol_type **) ptr;
  699. vars.sym_ptr_ptr = (coff_symbol_type **) ptr;
  700. ptr += SIZEOF_ILF_SYM_PTR_TABLE;
  701. vars.esym_table = (SYMENT *) ptr;
  702. vars.esym_ptr = (SYMENT *) ptr;
  703. ptr += SIZEOF_ILF_EXT_SYMS;
  704. vars.reltab = (arelent *) ptr;
  705. vars.relcount = 0;
  706. ptr += SIZEOF_ILF_RELOCS;
  707. vars.int_reltab = (struct internal_reloc *) ptr;
  708. ptr += SIZEOF_ILF_INT_RELOCS;
  709. vars.string_table = (char *) ptr;
  710. vars.string_ptr = (char *) ptr + STRING_SIZE_SIZE;
  711. ptr += SIZEOF_ILF_STRINGS;
  712. vars.end_string_ptr = (char *) ptr;
  713. /* The remaining space in bim->buffer is used
  714. by the pe_ILF_make_a_section() function. */
  715. /* PR 18758: Make sure that the data area is sufficiently aligned for
  716. struct coff_section_tdata. __alignof__ is a gcc extension, hence
  717. the test of GCC_VERSION. For other compilers we assume 8 byte
  718. alignment. */
  719. #if GCC_VERSION >= 3000
  720. alignment = __alignof__ (struct coff_section_tdata);
  721. #else
  722. alignment = 8;
  723. #endif
  724. ptr = (bfd_byte *) (((intptr_t) ptr + alignment - 1) & -alignment);
  725. vars.data = ptr;
  726. vars.abfd = abfd;
  727. vars.sec_index = 0;
  728. vars.magic = magic;
  729. /* Create the initial .idata$<n> sections:
  730. [.idata$2: Import Directory Table -- not needed]
  731. .idata$4: Import Lookup Table
  732. .idata$5: Import Address Table
  733. Note we do not create a .idata$3 section as this is
  734. created for us by the linker script. */
  735. id4 = pe_ILF_make_a_section (& vars, ".idata$4", SIZEOF_IDATA4, 0);
  736. id5 = pe_ILF_make_a_section (& vars, ".idata$5", SIZEOF_IDATA5, 0);
  737. if (id4 == NULL || id5 == NULL)
  738. goto error_return;
  739. /* Fill in the contents of these sections. */
  740. if (import_name_type == IMPORT_ORDINAL)
  741. {
  742. if (ordinal == 0)
  743. /* See PR 20907 for a reproducer. */
  744. goto error_return;
  745. #if defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64)
  746. ((unsigned int *) id4->contents)[0] = ordinal;
  747. ((unsigned int *) id4->contents)[1] = 0x80000000;
  748. ((unsigned int *) id5->contents)[0] = ordinal;
  749. ((unsigned int *) id5->contents)[1] = 0x80000000;
  750. #else
  751. * (unsigned int *) id4->contents = ordinal | 0x80000000;
  752. * (unsigned int *) id5->contents = ordinal | 0x80000000;
  753. #endif
  754. }
  755. else
  756. {
  757. char * symbol;
  758. unsigned int len;
  759. /* Create .idata$6 - the Hint Name Table. */
  760. id6 = pe_ILF_make_a_section (& vars, ".idata$6", SIZEOF_IDATA6, 0);
  761. if (id6 == NULL)
  762. goto error_return;
  763. /* If necessary, trim the import symbol name. */
  764. symbol = symbol_name;
  765. /* As used by MS compiler, '_', '@', and '?' are alternative
  766. forms of USER_LABEL_PREFIX, with '?' for c++ mangled names,
  767. '@' used for fastcall (in C), '_' everywhere else. Only one
  768. of these is used for a symbol. We strip this leading char for
  769. IMPORT_NAME_NOPREFIX and IMPORT_NAME_UNDECORATE as per the
  770. PE COFF 6.0 spec (section 8.3, Import Name Type). */
  771. if (import_name_type != IMPORT_NAME)
  772. {
  773. char c = symbol[0];
  774. /* Check that we don't remove for targets with empty
  775. USER_LABEL_PREFIX the leading underscore. */
  776. if ((c == '_' && abfd->xvec->symbol_leading_char != 0)
  777. || c == '@' || c == '?')
  778. symbol++;
  779. }
  780. len = strlen (symbol);
  781. if (import_name_type == IMPORT_NAME_UNDECORATE)
  782. {
  783. /* Truncate at the first '@'. */
  784. char *at = strchr (symbol, '@');
  785. if (at != NULL)
  786. len = at - symbol;
  787. }
  788. id6->contents[0] = ordinal & 0xff;
  789. id6->contents[1] = ordinal >> 8;
  790. memcpy ((char *) id6->contents + 2, symbol, len);
  791. id6->contents[len + 2] = '\0';
  792. }
  793. if (import_name_type != IMPORT_ORDINAL)
  794. {
  795. pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_RVA, id6);
  796. pe_ILF_save_relocs (&vars, id4);
  797. pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_RVA, id6);
  798. pe_ILF_save_relocs (&vars, id5);
  799. }
  800. /* Create an import symbol. */
  801. pe_ILF_make_a_symbol (& vars, "__imp_", symbol_name, id5, 0);
  802. imp_sym = vars.sym_ptr_ptr - 1;
  803. imp_index = vars.sym_index - 1;
  804. /* Create extra sections depending upon the type of import we are dealing with. */
  805. switch (import_type)
  806. {
  807. int i;
  808. case IMPORT_CODE:
  809. /* CODE functions are special, in that they get a trampoline that
  810. jumps to the main import symbol. Create a .text section to hold it.
  811. First we need to look up its contents in the jump table. */
  812. for (i = NUM_ENTRIES (jtab); i--;)
  813. {
  814. if (jtab[i].size == 0)
  815. continue;
  816. if (jtab[i].magic == magic)
  817. break;
  818. }
  819. /* If we did not find a matching entry something is wrong. */
  820. if (i < 0)
  821. abort ();
  822. /* Create the .text section. */
  823. text = pe_ILF_make_a_section (& vars, ".text", jtab[i].size, SEC_CODE);
  824. if (text == NULL)
  825. goto error_return;
  826. /* Copy in the jump code. */
  827. memcpy (text->contents, jtab[i].data, jtab[i].size);
  828. /* Create a reloc for the data in the text section. */
  829. #ifdef MIPS_ARCH_MAGIC_WINCE
  830. if (magic == MIPS_ARCH_MAGIC_WINCE)
  831. {
  832. pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) 0, BFD_RELOC_HI16_S,
  833. (struct bfd_symbol **) imp_sym,
  834. imp_index);
  835. pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_LO16, text);
  836. pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) 4, BFD_RELOC_LO16,
  837. (struct bfd_symbol **) imp_sym,
  838. imp_index);
  839. }
  840. else
  841. #endif
  842. #ifdef AMD64MAGIC
  843. if (magic == AMD64MAGIC)
  844. {
  845. pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) jtab[i].offset,
  846. BFD_RELOC_32_PCREL, (asymbol **) imp_sym,
  847. imp_index);
  848. }
  849. else
  850. #endif
  851. pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) jtab[i].offset,
  852. BFD_RELOC_32, (asymbol **) imp_sym,
  853. imp_index);
  854. pe_ILF_save_relocs (& vars, text);
  855. break;
  856. case IMPORT_DATA:
  857. break;
  858. default:
  859. /* XXX code not yet written. */
  860. abort ();
  861. }
  862. /* Initialise the bfd. */
  863. memset (& internal_f, 0, sizeof (internal_f));
  864. internal_f.f_magic = magic;
  865. internal_f.f_symptr = 0;
  866. internal_f.f_nsyms = 0;
  867. internal_f.f_flags = F_AR32WR | F_LNNO; /* XXX is this correct ? */
  868. if ( ! bfd_set_start_address (abfd, (bfd_vma) 0)
  869. || ! bfd_coff_set_arch_mach_hook (abfd, & internal_f))
  870. goto error_return;
  871. if (bfd_coff_mkobject_hook (abfd, (void *) & internal_f, NULL) == NULL)
  872. goto error_return;
  873. coff_data (abfd)->pe = 1;
  874. #ifdef THUMBPEMAGIC
  875. if (vars.magic == THUMBPEMAGIC)
  876. /* Stop some linker warnings about thumb code not supporting interworking. */
  877. coff_data (abfd)->flags |= F_INTERWORK | F_INTERWORK_SET;
  878. #endif
  879. /* Switch from file contents to memory contents. */
  880. bfd_cache_close (abfd);
  881. abfd->iostream = (void *) vars.bim;
  882. abfd->flags |= BFD_IN_MEMORY /* | HAS_LOCALS */;
  883. abfd->iovec = &_bfd_memory_iovec;
  884. abfd->where = 0;
  885. abfd->origin = 0;
  886. obj_sym_filepos (abfd) = 0;
  887. /* Now create a symbol describing the imported value. */
  888. switch (import_type)
  889. {
  890. case IMPORT_CODE:
  891. pe_ILF_make_a_symbol (& vars, "", symbol_name, text,
  892. BSF_NOT_AT_END | BSF_FUNCTION);
  893. break;
  894. case IMPORT_DATA:
  895. /* Nothing to do here. */
  896. break;
  897. default:
  898. /* XXX code not yet written. */
  899. abort ();
  900. }
  901. /* Create an import symbol for the DLL, without the .dll suffix. */
  902. ptr = (bfd_byte *) strrchr (source_dll, '.');
  903. if (ptr)
  904. * ptr = 0;
  905. pe_ILF_make_a_symbol (& vars, "__IMPORT_DESCRIPTOR_", source_dll, NULL, 0);
  906. if (ptr)
  907. * ptr = '.';
  908. /* Point the bfd at the symbol table. */
  909. obj_symbols (abfd) = vars.sym_cache;
  910. abfd->symcount = vars.sym_index;
  911. obj_raw_syments (abfd) = vars.native_syms;
  912. obj_raw_syment_count (abfd) = vars.sym_index;
  913. obj_coff_external_syms (abfd) = (void *) vars.esym_table;
  914. obj_coff_keep_syms (abfd) = true;
  915. obj_convert (abfd) = vars.sym_table;
  916. obj_conv_table_size (abfd) = vars.sym_index;
  917. obj_coff_strings (abfd) = vars.string_table;
  918. obj_coff_keep_strings (abfd) = true;
  919. abfd->flags |= HAS_SYMS;
  920. return true;
  921. error_return:
  922. free (vars.bim->buffer);
  923. free (vars.bim);
  924. return false;
  925. }
  926. /* We have detected a Image Library Format archive element.
  927. Decode the element and return the appropriate target. */
  928. static bfd_cleanup
  929. pe_ILF_object_p (bfd * abfd)
  930. {
  931. bfd_byte buffer[14];
  932. bfd_byte * ptr;
  933. char * symbol_name;
  934. char * source_dll;
  935. unsigned int machine;
  936. bfd_size_type size;
  937. unsigned int ordinal;
  938. unsigned int types;
  939. unsigned int magic;
  940. /* Upon entry the first six bytes of the ILF header have
  941. already been read. Now read the rest of the header. */
  942. if (bfd_bread (buffer, (bfd_size_type) 14, abfd) != 14)
  943. return NULL;
  944. ptr = buffer;
  945. machine = H_GET_16 (abfd, ptr);
  946. ptr += 2;
  947. /* Check that the machine type is recognised. */
  948. magic = 0;
  949. switch (machine)
  950. {
  951. case IMAGE_FILE_MACHINE_UNKNOWN:
  952. case IMAGE_FILE_MACHINE_ALPHA:
  953. case IMAGE_FILE_MACHINE_ALPHA64:
  954. case IMAGE_FILE_MACHINE_IA64:
  955. break;
  956. case IMAGE_FILE_MACHINE_I386:
  957. #ifdef I386MAGIC
  958. magic = I386MAGIC;
  959. #endif
  960. break;
  961. case IMAGE_FILE_MACHINE_AMD64:
  962. #ifdef AMD64MAGIC
  963. magic = AMD64MAGIC;
  964. #endif
  965. break;
  966. case IMAGE_FILE_MACHINE_R3000:
  967. case IMAGE_FILE_MACHINE_R4000:
  968. case IMAGE_FILE_MACHINE_R10000:
  969. case IMAGE_FILE_MACHINE_MIPS16:
  970. case IMAGE_FILE_MACHINE_MIPSFPU:
  971. case IMAGE_FILE_MACHINE_MIPSFPU16:
  972. #ifdef MIPS_ARCH_MAGIC_WINCE
  973. magic = MIPS_ARCH_MAGIC_WINCE;
  974. #endif
  975. break;
  976. case IMAGE_FILE_MACHINE_SH3:
  977. case IMAGE_FILE_MACHINE_SH4:
  978. #ifdef SH_ARCH_MAGIC_WINCE
  979. magic = SH_ARCH_MAGIC_WINCE;
  980. #endif
  981. break;
  982. case IMAGE_FILE_MACHINE_ARM:
  983. #ifdef ARMPEMAGIC
  984. magic = ARMPEMAGIC;
  985. #endif
  986. break;
  987. case IMAGE_FILE_MACHINE_ARM64:
  988. #ifdef AARCH64MAGIC
  989. magic = AARCH64MAGIC;
  990. #endif
  991. break;
  992. case IMAGE_FILE_MACHINE_THUMB:
  993. #ifdef THUMBPEMAGIC
  994. {
  995. extern const bfd_target TARGET_LITTLE_SYM;
  996. if (abfd->xvec == & TARGET_LITTLE_SYM)
  997. magic = THUMBPEMAGIC;
  998. }
  999. #endif
  1000. break;
  1001. case IMAGE_FILE_MACHINE_POWERPC:
  1002. /* We no longer support PowerPC. */
  1003. default:
  1004. _bfd_error_handler
  1005. /* xgettext:c-format */
  1006. (_("%pB: unrecognised machine type (0x%x)"
  1007. " in Import Library Format archive"),
  1008. abfd, machine);
  1009. bfd_set_error (bfd_error_malformed_archive);
  1010. return NULL;
  1011. break;
  1012. }
  1013. if (magic == 0)
  1014. {
  1015. _bfd_error_handler
  1016. /* xgettext:c-format */
  1017. (_("%pB: recognised but unhandled machine type (0x%x)"
  1018. " in Import Library Format archive"),
  1019. abfd, machine);
  1020. bfd_set_error (bfd_error_wrong_format);
  1021. return NULL;
  1022. }
  1023. /* We do not bother to check the date.
  1024. date = H_GET_32 (abfd, ptr); */
  1025. ptr += 4;
  1026. size = H_GET_32 (abfd, ptr);
  1027. ptr += 4;
  1028. if (size == 0)
  1029. {
  1030. _bfd_error_handler
  1031. (_("%pB: size field is zero in Import Library Format header"), abfd);
  1032. bfd_set_error (bfd_error_malformed_archive);
  1033. return NULL;
  1034. }
  1035. ordinal = H_GET_16 (abfd, ptr);
  1036. ptr += 2;
  1037. types = H_GET_16 (abfd, ptr);
  1038. /* ptr += 2; */
  1039. /* Now read in the two strings that follow. */
  1040. ptr = (bfd_byte *) _bfd_alloc_and_read (abfd, size, size);
  1041. if (ptr == NULL)
  1042. return NULL;
  1043. symbol_name = (char *) ptr;
  1044. /* See PR 20905 for an example of where the strnlen is necessary. */
  1045. source_dll = symbol_name + strnlen (symbol_name, size - 1) + 1;
  1046. /* Verify that the strings are null terminated. */
  1047. if (ptr[size - 1] != 0
  1048. || (bfd_size_type) ((bfd_byte *) source_dll - ptr) >= size)
  1049. {
  1050. _bfd_error_handler
  1051. (_("%pB: string not null terminated in ILF object file"), abfd);
  1052. bfd_set_error (bfd_error_malformed_archive);
  1053. bfd_release (abfd, ptr);
  1054. return NULL;
  1055. }
  1056. /* Now construct the bfd. */
  1057. if (! pe_ILF_build_a_bfd (abfd, magic, symbol_name,
  1058. source_dll, ordinal, types))
  1059. {
  1060. bfd_release (abfd, ptr);
  1061. return NULL;
  1062. }
  1063. return _bfd_no_cleanup;
  1064. }
  1065. static void
  1066. pe_bfd_read_buildid (bfd *abfd)
  1067. {
  1068. pe_data_type *pe = pe_data (abfd);
  1069. struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
  1070. asection *section;
  1071. bfd_byte *data = 0;
  1072. bfd_size_type dataoff;
  1073. unsigned int i;
  1074. bfd_vma addr = extra->DataDirectory[PE_DEBUG_DATA].VirtualAddress;
  1075. bfd_size_type size = extra->DataDirectory[PE_DEBUG_DATA].Size;
  1076. if (size == 0)
  1077. return;
  1078. addr += extra->ImageBase;
  1079. /* Search for the section containing the DebugDirectory. */
  1080. for (section = abfd->sections; section != NULL; section = section->next)
  1081. {
  1082. if ((addr >= section->vma) && (addr < (section->vma + section->size)))
  1083. break;
  1084. }
  1085. if (section == NULL)
  1086. return;
  1087. if (!(section->flags & SEC_HAS_CONTENTS))
  1088. return;
  1089. dataoff = addr - section->vma;
  1090. /* PR 20605 and 22373: Make sure that the data is really there.
  1091. Note - since we are dealing with unsigned quantities we have
  1092. to be careful to check for potential overflows. */
  1093. if (dataoff >= section->size
  1094. || size > section->size - dataoff)
  1095. {
  1096. _bfd_error_handler
  1097. (_("%pB: error: debug data ends beyond end of debug directory"),
  1098. abfd);
  1099. return;
  1100. }
  1101. /* Read the whole section. */
  1102. if (!bfd_malloc_and_get_section (abfd, section, &data))
  1103. {
  1104. free (data);
  1105. return;
  1106. }
  1107. /* Search for a CodeView entry in the DebugDirectory */
  1108. for (i = 0; i < size / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
  1109. {
  1110. struct external_IMAGE_DEBUG_DIRECTORY *ext
  1111. = &((struct external_IMAGE_DEBUG_DIRECTORY *)(data + dataoff))[i];
  1112. struct internal_IMAGE_DEBUG_DIRECTORY idd;
  1113. _bfd_XXi_swap_debugdir_in (abfd, ext, &idd);
  1114. if (idd.Type == PE_IMAGE_DEBUG_TYPE_CODEVIEW)
  1115. {
  1116. char buffer[256 + 1];
  1117. CODEVIEW_INFO *cvinfo = (CODEVIEW_INFO *) buffer;
  1118. /*
  1119. The debug entry doesn't have to have to be in a section, in which
  1120. case AddressOfRawData is 0, so always use PointerToRawData.
  1121. */
  1122. if (_bfd_XXi_slurp_codeview_record (abfd,
  1123. (file_ptr) idd.PointerToRawData,
  1124. idd.SizeOfData, cvinfo))
  1125. {
  1126. struct bfd_build_id* build_id = bfd_alloc (abfd,
  1127. sizeof (struct bfd_build_id) + cvinfo->SignatureLength);
  1128. if (build_id)
  1129. {
  1130. build_id->size = cvinfo->SignatureLength;
  1131. memcpy(build_id->data, cvinfo->Signature,
  1132. cvinfo->SignatureLength);
  1133. abfd->build_id = build_id;
  1134. }
  1135. }
  1136. break;
  1137. }
  1138. }
  1139. free (data);
  1140. }
  1141. static bfd_cleanup
  1142. pe_bfd_object_p (bfd * abfd)
  1143. {
  1144. bfd_byte buffer[6];
  1145. struct external_DOS_hdr dos_hdr;
  1146. struct external_PEI_IMAGE_hdr image_hdr;
  1147. struct internal_filehdr internal_f;
  1148. struct internal_aouthdr internal_a;
  1149. bfd_size_type opt_hdr_size;
  1150. file_ptr offset;
  1151. bfd_cleanup result;
  1152. /* Detect if this a Microsoft Import Library Format element. */
  1153. /* First read the beginning of the header. */
  1154. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  1155. || bfd_bread (buffer, (bfd_size_type) 6, abfd) != 6)
  1156. {
  1157. if (bfd_get_error () != bfd_error_system_call)
  1158. bfd_set_error (bfd_error_wrong_format);
  1159. return NULL;
  1160. }
  1161. /* Then check the magic and the version (only 0 is supported). */
  1162. if (H_GET_32 (abfd, buffer) == 0xffff0000
  1163. && H_GET_16 (abfd, buffer + 4) == 0)
  1164. return pe_ILF_object_p (abfd);
  1165. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  1166. || bfd_bread (&dos_hdr, (bfd_size_type) sizeof (dos_hdr), abfd)
  1167. != sizeof (dos_hdr))
  1168. {
  1169. if (bfd_get_error () != bfd_error_system_call)
  1170. bfd_set_error (bfd_error_wrong_format);
  1171. return NULL;
  1172. }
  1173. /* There are really two magic numbers involved; the magic number
  1174. that says this is a NT executable (PEI) and the magic number that
  1175. determines the architecture. The former is IMAGE_DOS_SIGNATURE, stored in
  1176. the e_magic field. The latter is stored in the f_magic field.
  1177. If the NT magic number isn't valid, the architecture magic number
  1178. could be mimicked by some other field (specifically, the number
  1179. of relocs in section 3). Since this routine can only be called
  1180. correctly for a PEI file, check the e_magic number here, and, if
  1181. it doesn't match, clobber the f_magic number so that we don't get
  1182. a false match. */
  1183. if (H_GET_16 (abfd, dos_hdr.e_magic) != IMAGE_DOS_SIGNATURE)
  1184. {
  1185. bfd_set_error (bfd_error_wrong_format);
  1186. return NULL;
  1187. }
  1188. offset = H_GET_32 (abfd, dos_hdr.e_lfanew);
  1189. if (bfd_seek (abfd, offset, SEEK_SET) != 0
  1190. || (bfd_bread (&image_hdr, (bfd_size_type) sizeof (image_hdr), abfd)
  1191. != sizeof (image_hdr)))
  1192. {
  1193. if (bfd_get_error () != bfd_error_system_call)
  1194. bfd_set_error (bfd_error_wrong_format);
  1195. return NULL;
  1196. }
  1197. if (H_GET_32 (abfd, image_hdr.nt_signature) != 0x4550)
  1198. {
  1199. bfd_set_error (bfd_error_wrong_format);
  1200. return NULL;
  1201. }
  1202. /* Swap file header, so that we get the location for calling
  1203. real_object_p. */
  1204. bfd_coff_swap_filehdr_in (abfd, &image_hdr, &internal_f);
  1205. if (! bfd_coff_bad_format_hook (abfd, &internal_f)
  1206. || internal_f.f_opthdr > bfd_coff_aoutsz (abfd))
  1207. {
  1208. bfd_set_error (bfd_error_wrong_format);
  1209. return NULL;
  1210. }
  1211. memcpy (internal_f.pe.dos_message, dos_hdr.dos_message,
  1212. sizeof (internal_f.pe.dos_message));
  1213. /* Read the optional header, which has variable size. */
  1214. opt_hdr_size = internal_f.f_opthdr;
  1215. if (opt_hdr_size != 0)
  1216. {
  1217. bfd_size_type amt = opt_hdr_size;
  1218. bfd_byte * opthdr;
  1219. /* PR 17521 file: 230-131433-0.004. */
  1220. if (amt < sizeof (PEAOUTHDR))
  1221. amt = sizeof (PEAOUTHDR);
  1222. opthdr = _bfd_alloc_and_read (abfd, amt, opt_hdr_size);
  1223. if (opthdr == NULL)
  1224. return NULL;
  1225. if (amt > opt_hdr_size)
  1226. memset (opthdr + opt_hdr_size, 0, amt - opt_hdr_size);
  1227. bfd_set_error (bfd_error_no_error);
  1228. bfd_coff_swap_aouthdr_in (abfd, opthdr, & internal_a);
  1229. if (bfd_get_error () != bfd_error_no_error)
  1230. return NULL;
  1231. }
  1232. result = coff_real_object_p (abfd, internal_f.f_nscns, &internal_f,
  1233. (opt_hdr_size != 0
  1234. ? &internal_a
  1235. : (struct internal_aouthdr *) NULL));
  1236. if (result)
  1237. {
  1238. /* Now the whole header has been processed, see if there is a build-id */
  1239. pe_bfd_read_buildid(abfd);
  1240. }
  1241. return result;
  1242. }
  1243. #define coff_object_p pe_bfd_object_p
  1244. #endif /* COFF_IMAGE_WITH_PE */