1/* AIX cross support for collect2.
2   Copyright (C) 2009-2015 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3.  If not see
18<http://www.gnu.org/licenses/>.  */
19
20#ifndef GCC_COLLECT2_AIX_H
21#define GCC_COLLECT2_AIX_H
22/* collect2-aix.c requires mmap support.  It should otherwise be
23   fairly portable.  */
24#if defined(CROSS_DIRECTORY_STRUCTURE) \
25    && defined(TARGET_AIX_VERSION) \
26    && HAVE_MMAP
27
28#define CROSS_AIX_SUPPORT 1
29
30/* -------------------------------------------------------------------------
31   Definitions adapted from bfd.  (Fairly heavily adapted in some cases.)
32   ------------------------------------------------------------------------- */
33
34/* Compatibility types for bfd.  */
35typedef unsigned HOST_WIDE_INT bfd_vma;
36
37/* The size of an archive's fl_magic field.  */
38#define FL_MAGIC_SIZE 8
39
40/* The expected contents of fl_magic for big archives.  */
41#define FL_MAGIC_BIG_AR "<bigaf>\012"
42
43/* The size of each offset string in the header of a big archive.  */
44#define AR_BIG_OFFSET_SIZE 20
45
46/* The format of the file header in a "big" XCOFF archive.  */
47struct external_big_ar_filehdr
48{
49  /* Magic string.  */
50  char fl_magic[FL_MAGIC_SIZE];
51
52  /* Offset of the member table (decimal ASCII string).  */
53  char fl_memoff[AR_BIG_OFFSET_SIZE];
54
55  /* Offset of the global symbol table for 32-bit objects (decimal ASCII
56     string).  */
57  char fl_symoff[AR_BIG_OFFSET_SIZE];
58
59  /* Offset of the global symbol table for 64-bit objects (decimal ASCII
60     string).  */
61  char fl_symoff64[AR_BIG_OFFSET_SIZE];
62
63  /* Offset of the first member in the archive (decimal ASCII string).  */
64  char fl_firstmemoff[AR_BIG_OFFSET_SIZE];
65
66  /* Offset of the last member in the archive (decimal ASCII string).  */
67  char fl_lastmemoff[AR_BIG_OFFSET_SIZE];
68
69  /* Offset of the first member on the free list (decimal ASCII
70     string).  */
71  char fl_freeoff[AR_BIG_OFFSET_SIZE];
72};
73
74/* Each archive name is followed by this many bytes of magic string.  */
75#define SXCOFFARFMAG 2
76
77/* The format of a member header in a "big" XCOFF archive.  */
78struct external_big_ar_member
79{
80  /* File size not including the header (decimal ASCII string).  */
81  char ar_size[AR_BIG_OFFSET_SIZE];
82
83  /* File offset of next archive member (decimal ASCII string).  */
84  char ar_nextoff[AR_BIG_OFFSET_SIZE];
85
86  /* File offset of previous archive member (decimal ASCII string).  */
87  char ar_prevoff[AR_BIG_OFFSET_SIZE];
88
89  /* File mtime (decimal ASCII string).  */
90  char ar_date[12];
91
92  /* File UID (decimal ASCII string).  */
93  char ar_uid[12];
94
95  /* File GID (decimal ASCII string).  */
96  char ar_gid[12];
97
98  /* File mode (octal ASCII string).  */
99  char ar_mode[12];
100
101  /* Length of file name (decimal ASCII string).  */
102  char ar_namlen[4];
103
104  /* This structure is followed by the file name.  The length of the
105     name is given in the namlen field.  If the length of the name is
106     odd, the name is followed by a null byte.  The name and optional
107     null byte are followed by XCOFFARFMAG, which is not included in
108     namlen.  The contents of the archive member follow; the number of
109     bytes is given in the size field.  */
110};
111
112/* The known values of f_magic in an XCOFF file header.  */
113#define U802WRMAGIC 0730	/* Writeable text segments.  */
114#define U802ROMAGIC 0735	/* Readonly sharable text segments.  */
115#define U802TOCMAGIC 0737	/* Readonly text segments and TOC.  */
116#define U803XTOCMAGIC 0757	/* Aix 4.3 64-bit XCOFF.  */
117#define U64_TOCMAGIC 0767	/* AIX 5+ 64-bit XCOFF.  */
118
119/* The number of bytes in an XCOFF file's f_magic field.  */
120#define F_MAGIC_SIZE 2
121
122/* The format of a 32-bit XCOFF file header.  */
123struct external_filehdr_32
124{
125  /* The magic number.  */
126  char f_magic[F_MAGIC_SIZE];
127
128  /* The number of sections.  */
129  char f_nscns[2];
130
131  /* Time & date stamp.  */
132  char f_timdat[4];
133
134  /* The offset of the symbol table from the start of the file.  */
135  char f_symptr[4];
136
137  /* The number of entries in the symbol table.  */
138  char f_nsyms[4];
139
140  /* The size of the auxiliary header.  */
141  char f_opthdr[2];
142
143  /* Flags.  */
144  char f_flags[2];
145};
146
147/* The format of a 64-bit XCOFF file header.  */
148struct external_filehdr_64
149{
150  /* The magic number.  */
151  char f_magic[F_MAGIC_SIZE];
152
153  /* The number of sections.  */
154  char f_nscns[2];
155
156  /* Time & date stamp.  */
157  char f_timdat[4];
158
159  /* The offset of the symbol table from the start of the file.  */
160  char f_symptr[8];
161
162  /* The size of the auxiliary header.  */
163  char f_opthdr[2];
164
165  /* Flags.  */
166  char f_flags[2];
167
168  /* The number of entries in the symbol table.  */
169  char f_nsyms[4];
170};
171
172/* An internal representation of the XCOFF file header.  */
173struct internal_filehdr
174{
175  unsigned short f_magic;
176  unsigned short f_nscns;
177  long f_timdat;
178  bfd_vma f_symptr;
179  long f_nsyms;
180  unsigned short f_opthdr;
181  unsigned short f_flags;
182};
183
184/* Symbol classes have their names in the debug section if this flag
185   is set.  */
186#define DBXMASK 0x80
187
188/* The format of an XCOFF symbol-table entry.  */
189struct external_syment
190{
191  union {
192    struct {
193      union {
194	/* The name of the symbol.  There is an implicit null character
195	   after the end of the array.  */
196	char n_name[8];
197	struct {
198	  /* If n_zeroes is zero, n_offset is the offset the name from
199	     the start of the string table.  */
200	  char n_zeroes[4];
201	  char n_offset[4];
202	} u;
203      } u;
204
205      /* The symbol's value.  */
206      char n_value[4];
207    } xcoff32;
208    struct {
209      /* The symbol's value.  */
210      char n_value[8];
211
212      /* The offset of the symbol from the start of the string table.  */
213      char n_offset[4];
214    } xcoff64;
215  } u;
216
217  /* The number of the section to which this symbol belongs.  */
218  char n_scnum[2];
219
220  /* The type of symbol.  (It can be interpreted as an n_lang
221     and an n_cpu byte, but we don't care about that here.)  */
222  char n_type[2];
223
224  /* The class of symbol (a C_* value).  */
225  char n_sclass[1];
226
227  /* The number of auxiliary symbols attached to this entry.  */
228  char n_numaux[1];
229};
230
231/* Definitions required by collect2.  */
232#define C_EXT 2
233
234#define F_SHROBJ    0x2000
235#define F_LOADONLY  0x4000
236
237#define N_UNDEF ((short) 0)
238#define N_TMASK 060
239#define N_BTSHFT 4
240
241#define DT_NON 0
242#define DT_FCN 2
243
244/* -------------------------------------------------------------------------
245   Local code.
246   ------------------------------------------------------------------------- */
247
248/* An internal representation of an XCOFF symbol-table entry,
249   which is associated with the API-defined SYMENT type.  */
250struct internal_syment
251{
252  char n_name[9];
253  unsigned int n_zeroes;
254  bfd_vma n_offset;
255  bfd_vma n_value;
256  short n_scnum;
257  unsigned short n_flags;
258  unsigned short n_type;
259  unsigned char n_sclass;
260  unsigned char n_numaux;
261};
262typedef struct internal_syment SYMENT;
263
264/* The internal representation of the API-defined LDFILE type.  */
265struct internal_ldfile
266{
267  /* The file handle for the associated file, or -1 if it hasn't been
268     opened yet.  */
269  int fd;
270
271  /* The start of the current XCOFF object, if one has been mapped
272     into memory.  Null otherwise.  */
273  char *object;
274
275  /* The offset of OBJECT from the start of the containing page.  */
276  size_t page_offset;
277
278  /* The size of the file pointed to by OBJECT.  Valid iff OFFSET
279     is nonnull.  */
280  size_t object_size;
281
282  /* The offset of the next member in an archive after OBJECT,
283     or -1 if this isn't an archive.  Valid iff OFFSET is nonnull.  */
284  off_t next_member;
285
286  /* The parsed version of the XCOFF file header.  */
287  struct internal_filehdr filehdr;
288};
289typedef struct internal_ldfile LDFILE;
290
291/* The API allows the file header to be directly accessed via this macro.  */
292#define HEADER(FILE) ((FILE)->filehdr)
293
294/* API-defined return codes.  SUCCESS must be > 0 and FAILURE must be <= 0.  */
295#define SUCCESS 1
296#define FAILURE 0
297
298/* API-defined functions.  */
299extern LDFILE *ldopen (char *, LDFILE *);
300extern char *ldgetname (LDFILE *, SYMENT *);
301extern int ldtbread (LDFILE *, long, SYMENT *);
302extern int ldclose (LDFILE *);
303
304#endif
305
306#endif /* GCC_COLLECT2_AIX_H */
307