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