* coffcode.h (coff_write_object_contents): Add ATTRIBUTE_UNUSED to
[platform/upstream/binutils.git] / bfd / coff-ia64.c
1 /* BFD back-end for HP/Intel IA-64 COFF files.
2    Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
3    Contributed by David Mosberger <davidm@hpl.hp.com>
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "coff/ia64.h"
25 #include "coff/internal.h"
26 #include "coff/pe.h"
27 #include "libcoff.h"
28
29 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
30 /* The page size is a guess based on ELF.  */
31
32 #define COFF_PAGE_SIZE 0x1000
33
34 static reloc_howto_type howto_table[] =
35 {
36   EMPTY_HOWTO (0),
37 };
38
39 #define BADMAG(x) IA64BADMAG(x)
40 #define IA64 1                  /* Customize coffcode.h */
41
42 #ifdef COFF_WITH_pep
43 # undef AOUTSZ
44 # define AOUTSZ         PEPAOUTSZ
45 # define PEAOUTHDR      PEPAOUTHDR
46 #endif
47
48 #define RTYPE2HOWTO(cache_ptr, dst) \
49             (cache_ptr)->howto = howto_table + (dst)->r_type;
50
51 #ifdef COFF_WITH_PE
52 /* Return true if this relocation should
53    appear in the output .reloc section.  */
54
55 static boolean in_reloc_p PARAMS ((bfd *, reloc_howto_type *));
56
57 static boolean
58 in_reloc_p(abfd, howto)
59      bfd * abfd ATTRIBUTE_UNUSED;
60      reloc_howto_type *howto ATTRIBUTE_UNUSED;
61 {
62   return 0;                     /* We don't do relocs for now...  */
63 }
64 #endif
65
66 #include "coffcode.h"
67
68 static const bfd_target *ia64coff_object_p PARAMS ((bfd *));
69
70 static const bfd_target *
71 ia64coff_object_p (abfd)
72      bfd *abfd;
73 {
74 #ifdef COFF_IMAGE_WITH_PE
75   {
76     struct external_PEI_DOS_hdr dos_hdr;
77     struct external_PEI_IMAGE_hdr image_hdr;
78     file_ptr offset;
79
80     if (bfd_seek (abfd, 0x00, SEEK_SET) != 0
81         || bfd_read (&dos_hdr, 1, sizeof (dos_hdr), abfd)
82            != sizeof (dos_hdr))
83       {
84         if (bfd_get_error () != bfd_error_system_call)
85           bfd_set_error (bfd_error_wrong_format);
86         return NULL;
87       }
88
89     /* There are really two magic numbers involved; the magic number
90        that says this is a NT executable (PEI) and the magic number
91        that determines the architecture.  The former is DOSMAGIC,
92        stored in the e_magic field.  The latter is stored in the
93        f_magic field.  If the NT magic number isn't valid, the
94        architecture magic number could be mimicked by some other
95        field (specifically, the number of relocs in section 3).  Since
96        this routine can only be called correctly for a PEI file, check
97        the e_magic number here, and, if it doesn't match, clobber the
98        f_magic number so that we don't get a false match.  */
99     if (bfd_h_get_16 (abfd, (bfd_byte *) dos_hdr.e_magic) != DOSMAGIC)
100       {
101         bfd_set_error (bfd_error_wrong_format);
102         return NULL;
103       }
104
105     offset = bfd_h_get_32 (abfd, (bfd_byte *) dos_hdr.e_lfanew);
106     if (bfd_seek (abfd, (file_ptr) offset, SEEK_SET) != 0
107         || bfd_read (&image_hdr, 1, sizeof (image_hdr), abfd)
108            != sizeof (image_hdr))
109       {
110         if (bfd_get_error () != bfd_error_system_call)
111           bfd_set_error (bfd_error_wrong_format);
112         return NULL;
113       }
114
115     if (bfd_h_get_32 (abfd, (bfd_byte *) image_hdr.nt_signature)
116         != 0x4550)
117       {
118         bfd_set_error (bfd_error_wrong_format);
119         return NULL;
120       }
121
122     /* Here is the hack.  coff_object_p wants to read filhsz bytes to
123        pick up the COFF header for PE, see "struct external_PEI_filehdr"
124        in include/coff/pe.h.  We adjust so that that will work. */
125     if (bfd_seek (abfd,
126                   (file_ptr) (offset - sizeof (dos_hdr)),
127                   SEEK_SET)
128         != 0)
129       {
130         if (bfd_get_error () != bfd_error_system_call)
131           bfd_set_error (bfd_error_wrong_format);
132         return NULL;
133       }
134   }
135 #endif
136
137   return coff_object_p (abfd);
138 }
139
140 const bfd_target
141 #ifdef TARGET_SYM
142   TARGET_SYM =
143 #else
144   ia64coff_vec =
145 #endif
146 {
147 #ifdef TARGET_NAME
148   TARGET_NAME,
149 #else
150   "coff-ia64",                  /* name */
151 #endif
152   bfd_target_coff_flavour,
153   BFD_ENDIAN_LITTLE,            /* data byte order is little */
154   BFD_ENDIAN_LITTLE,            /* header byte order is little */
155
156   (HAS_RELOC | EXEC_P |         /* object flags */
157    HAS_LINENO | HAS_DEBUG |
158    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
159
160 #ifndef COFF_WITH_PE
161   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
162    | SEC_CODE | SEC_DATA),
163 #else
164   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
165    | SEC_CODE | SEC_DATA
166    | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
167 #endif
168
169 #ifdef TARGET_UNDERSCORE
170   TARGET_UNDERSCORE,            /* leading underscore */
171 #else
172   0,                            /* leading underscore */
173 #endif
174   '/',                          /* ar_pad_char */
175   15,                           /* ar_max_namelen */
176
177   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
178      bfd_getl32, bfd_getl_signed_32, bfd_putl32,
179      bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
180   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
181      bfd_getl32, bfd_getl_signed_32, bfd_putl32,
182      bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
183
184 /* Note that we allow an object file to be treated as a core file as well.  */
185     {_bfd_dummy_target, ia64coff_object_p, /* bfd_check_format */
186        bfd_generic_archive_p, ia64coff_object_p},
187     {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
188        bfd_false},
189     {bfd_false, coff_write_object_contents, /* bfd_write_contents */
190        _bfd_write_archive_contents, bfd_false},
191
192      BFD_JUMP_TABLE_GENERIC (coff),
193      BFD_JUMP_TABLE_COPY (coff),
194      BFD_JUMP_TABLE_CORE (_bfd_nocore),
195      BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
196      BFD_JUMP_TABLE_SYMBOLS (coff),
197      BFD_JUMP_TABLE_RELOCS (coff),
198      BFD_JUMP_TABLE_WRITE (coff),
199      BFD_JUMP_TABLE_LINK (coff),
200      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
201
202   NULL,
203
204   COFF_SWAP_TABLE
205 };