coff-ia64.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* BFD back-end for HP/Intel IA-64 COFF files.
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Contributed by David Mosberger <davidm@hpl.hp.com>
  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. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "coff/ia64.h"
  21. #include "coff/internal.h"
  22. #include "coff/pe.h"
  23. #include "libcoff.h"
  24. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
  25. /* Windows ia64 uses 8K page size. */
  26. #define COFF_PAGE_SIZE 0x2000
  27. static reloc_howto_type howto_table[] =
  28. {
  29. EMPTY_HOWTO (0),
  30. };
  31. #define BADMAG(x) IA64BADMAG(x)
  32. #define IA64 1 /* Customize coffcode.h */
  33. #ifdef COFF_WITH_pep
  34. # undef AOUTSZ
  35. # define AOUTSZ PEPAOUTSZ
  36. # define PEAOUTHDR PEPAOUTHDR
  37. #endif
  38. #define RTYPE2HOWTO(cache_ptr, dst) \
  39. (cache_ptr)->howto = howto_table;
  40. #ifdef COFF_WITH_PE
  41. /* Return TRUE if this relocation should
  42. appear in the output .reloc section. */
  43. static bool
  44. in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED,
  45. reloc_howto_type *howto ATTRIBUTE_UNUSED)
  46. {
  47. return false; /* We don't do relocs for now... */
  48. }
  49. #endif
  50. #ifndef bfd_pe_print_pdata
  51. #define bfd_pe_print_pdata NULL
  52. #endif
  53. #include "coffcode.h"
  54. static bfd_cleanup
  55. ia64coff_object_p (bfd *abfd)
  56. {
  57. #ifdef COFF_IMAGE_WITH_PE
  58. {
  59. struct external_DOS_hdr dos_hdr;
  60. struct external_PEI_IMAGE_hdr image_hdr;
  61. file_ptr offset;
  62. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  63. || (bfd_bread (&dos_hdr, (bfd_size_type) sizeof (dos_hdr), abfd)
  64. != sizeof (dos_hdr)))
  65. {
  66. if (bfd_get_error () != bfd_error_system_call)
  67. bfd_set_error (bfd_error_wrong_format);
  68. return NULL;
  69. }
  70. /* There are really two magic numbers involved; the magic number
  71. that says this is a NT executable (PEI) and the magic number
  72. that determines the architecture. The former is IMAGE_DOS_SIGNATURE,
  73. stored in the e_magic field. The latter is stored in the
  74. f_magic field. If the NT magic number isn't valid, the
  75. architecture magic number could be mimicked by some other
  76. field (specifically, the number of relocs in section 3). Since
  77. this routine can only be called correctly for a PEI file, check
  78. the e_magic number here, and, if it doesn't match, clobber the
  79. f_magic number so that we don't get a false match. */
  80. if (H_GET_16 (abfd, dos_hdr.e_magic) != IMAGE_DOS_SIGNATURE)
  81. {
  82. bfd_set_error (bfd_error_wrong_format);
  83. return NULL;
  84. }
  85. offset = H_GET_32 (abfd, dos_hdr.e_lfanew);
  86. if (bfd_seek (abfd, offset, SEEK_SET) != 0
  87. || (bfd_bread (&image_hdr, (bfd_size_type) sizeof (image_hdr), abfd)
  88. != sizeof (image_hdr)))
  89. {
  90. if (bfd_get_error () != bfd_error_system_call)
  91. bfd_set_error (bfd_error_wrong_format);
  92. return NULL;
  93. }
  94. if (H_GET_32 (abfd, image_hdr.nt_signature)
  95. != 0x4550)
  96. {
  97. bfd_set_error (bfd_error_wrong_format);
  98. return NULL;
  99. }
  100. /* Here is the hack. coff_object_p wants to read filhsz bytes to
  101. pick up the COFF header for PE, see "struct external_PEI_filehdr"
  102. in include/coff/pe.h. We adjust so that that will work. */
  103. if (bfd_seek (abfd, offset - sizeof (dos_hdr), SEEK_SET) != 0)
  104. {
  105. if (bfd_get_error () != bfd_error_system_call)
  106. bfd_set_error (bfd_error_wrong_format);
  107. return NULL;
  108. }
  109. }
  110. #endif
  111. return coff_object_p (abfd);
  112. }
  113. const bfd_target
  114. #ifdef TARGET_SYM
  115. TARGET_SYM =
  116. #else
  117. ia64coff_vec =
  118. #endif
  119. {
  120. #ifdef TARGET_NAME
  121. TARGET_NAME,
  122. #else
  123. "coff-ia64", /* name */
  124. #endif
  125. bfd_target_coff_flavour,
  126. BFD_ENDIAN_LITTLE, /* data byte order is little */
  127. BFD_ENDIAN_LITTLE, /* header byte order is little */
  128. (HAS_RELOC | EXEC_P /* object flags */
  129. | HAS_LINENO | HAS_DEBUG
  130. | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  131. #ifndef COFF_WITH_PE
  132. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
  133. | SEC_CODE | SEC_DATA),
  134. #else
  135. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
  136. | SEC_CODE | SEC_DATA
  137. | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
  138. #endif
  139. #ifdef TARGET_UNDERSCORE
  140. TARGET_UNDERSCORE, /* leading underscore */
  141. #else
  142. 0, /* leading underscore */
  143. #endif
  144. '/', /* ar_pad_char */
  145. 15, /* ar_max_namelen */
  146. 0, /* match priority. */
  147. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  148. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  149. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  150. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
  151. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  152. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  153. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
  154. /* Note that we allow an object file to be treated as a core file as well. */
  155. { /* bfd_check_format */
  156. _bfd_dummy_target,
  157. ia64coff_object_p,
  158. bfd_generic_archive_p,
  159. ia64coff_object_p
  160. },
  161. { /* bfd_set_format */
  162. _bfd_bool_bfd_false_error,
  163. coff_mkobject,
  164. _bfd_generic_mkarchive,
  165. _bfd_bool_bfd_false_error
  166. },
  167. { /* bfd_write_contents */
  168. _bfd_bool_bfd_false_error,
  169. coff_write_object_contents,
  170. _bfd_write_archive_contents,
  171. _bfd_bool_bfd_false_error
  172. },
  173. BFD_JUMP_TABLE_GENERIC (coff),
  174. BFD_JUMP_TABLE_COPY (coff),
  175. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  176. BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
  177. BFD_JUMP_TABLE_SYMBOLS (coff),
  178. BFD_JUMP_TABLE_RELOCS (coff),
  179. BFD_JUMP_TABLE_WRITE (coff),
  180. BFD_JUMP_TABLE_LINK (coff),
  181. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  182. NULL,
  183. COFF_SWAP_TABLE
  184. };