1 /* AIX cross support for collect2.
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
24 #include "collect2-aix.h"
26 #ifdef CROSS_AIX_SUPPORT
28 /* Read SIZE bytes starting at DATA as a big-endian value. */
31 read_value (char *data
, unsigned int size
)
37 for (i
= 0; i
< size
; i
++)
40 value
+= (unsigned char) data
[i
];
45 /* FIELD is a char array. Read the contents as a big-endian integer. */
46 #define READ_FIELD(FIELD) \
47 read_value (FIELD, sizeof (FIELD))
49 /* OBJECT is a char pointer to an in-file object of type struct TYPE.
50 Return the address of field FIELD. */
51 #define OBJECT_FIELD(OBJECT, TYPE, FIELD) \
52 (OBJECT) + offsetof (struct TYPE, FIELD)
54 /* Return the size of FIELD, which is a field of struct TYPE. */
55 #define FIELD_SIZE(TYPE, FIELD) \
56 sizeof (((struct TYPE *) (0))->FIELD)
58 /* OBJECT is a char pointer to an in-file object of type struct TYPE.
59 Read the value of field FIELD as a big-endian integer. */
60 #define READ_OBJECT(OBJECT, TYPE, FIELD) \
61 read_value (OBJECT_FIELD (OBJECT, TYPE, FIELD), FIELD_SIZE (TYPE, FIELD))
63 /* Copy FIELD from an external structure of type TYPE at address FROM
64 to an internal structure pointed to by TO. */
65 #define COPY_FIELD(TO, FROM, TYPE, FIELD) \
66 ((TO)->FIELD = READ_OBJECT (FROM, TYPE, FIELD))
68 /* Return true if STRING is less than SIZE bytes long. EXTRA_TERMINATOR
69 is another character (besides '\0') that acts as a terminator,
73 string_within_bounds_p (const char *string
, size_t size
, char extra_terminator
)
77 for (p
= string
; p
< string
+ size
; p
++)
78 if (*p
== '\0' || *p
== extra_terminator
)
83 /* STRING is a pointer to a char array. Try to read its value as an
84 ASCII-encoded integer. On success, return true and store the result
86 #define PARSE_INTEGER(TARGET, STRING) \
87 (string_within_bounds_p (&(STRING)[0], sizeof (STRING), ' ') \
88 && ((TARGET) = strtoul (STRING, NULL, 0), true))
90 /* Check that LDFILE's current object has SIZE bytes starting at OFFSET. */
93 within_object_p (LDFILE
*ldfile
, size_t offset
, size_t size
)
95 return offset
<= ldfile
->object_size
&& offset
+ size
<= ldfile
->object_size
;
98 /* Try to read the file header for an XCOFF object at OFFSET bytes into
99 LDFILE. The object is expected to be OBJECT_SIZE bytes in size.
100 If the object is a member of an archive, NEXT_MEMBER is the offset
101 of the next member, otherwise it is -1.
103 Return true on success, recording the object information in LDFILE. */
106 read_xcoff_object (LDFILE
*ldfile
, size_t offset
, size_t object_size
,
109 struct internal_filehdr
*internal
;
114 /* First try to map the file into memory. */
115 page_size
= getpagesize ();
116 ldfile
->page_offset
= offset
& (page_size
- 1);
117 map
= mmap (NULL
, object_size
+ ldfile
->page_offset
, PROT_READ
,
118 MAP_SHARED
, ldfile
->fd
, offset
- ldfile
->page_offset
);
119 if (map
== MAP_FAILED
)
122 /* Record the success. */
123 ldfile
->object
= (char *) map
+ ldfile
->page_offset
;
124 ldfile
->object_size
= object_size
;
125 ldfile
->next_member
= next_member
;
127 /* Read the magic value to determine the type of file. */
128 if (!within_object_p (ldfile
, 0, F_MAGIC_SIZE
))
131 internal
= &ldfile
->filehdr
;
132 external
= ldfile
->object
;
133 internal
->f_magic
= read_value (external
, F_MAGIC_SIZE
);
134 if (internal
->f_magic
== U802TOCMAGIC
)
136 if (!within_object_p (ldfile
, 0, sizeof (struct external_filehdr_32
)))
139 COPY_FIELD (internal
, external
, external_filehdr_32
, f_nscns
);
140 COPY_FIELD (internal
, external
, external_filehdr_32
, f_timdat
);
141 COPY_FIELD (internal
, external
, external_filehdr_32
, f_symptr
);
142 COPY_FIELD (internal
, external
, external_filehdr_32
, f_nsyms
);
143 COPY_FIELD (internal
, external
, external_filehdr_32
, f_opthdr
);
144 COPY_FIELD (internal
, external
, external_filehdr_32
, f_flags
);
147 else if (internal
->f_magic
== U803XTOCMAGIC
148 || internal
->f_magic
== U64_TOCMAGIC
)
150 if (!within_object_p (ldfile
, 0, sizeof (struct external_filehdr_64
)))
153 COPY_FIELD (internal
, external
, external_filehdr_64
, f_nscns
);
154 COPY_FIELD (internal
, external
, external_filehdr_64
, f_timdat
);
155 COPY_FIELD (internal
, external
, external_filehdr_64
, f_symptr
);
156 COPY_FIELD (internal
, external
, external_filehdr_64
, f_nsyms
);
157 COPY_FIELD (internal
, external
, external_filehdr_64
, f_opthdr
);
158 COPY_FIELD (internal
, external
, external_filehdr_64
, f_flags
);
164 /* Try to read an archive member at OFFSET bytes into LDFILE.
165 Return true on success, recording the member and object
166 information in LDFILE. */
169 read_archive_member (LDFILE
*ldfile
, size_t offset
)
171 struct external_big_ar_member member
;
176 if (lseek (ldfile
->fd
, offset
, SEEK_SET
) >= 0
177 && read (ldfile
->fd
, &member
, sizeof (member
)) == sizeof (member
)
178 && PARSE_INTEGER (namlen
, member
.ar_namlen
)
179 /* Stop once we reach the member table entry, which has a name
182 && PARSE_INTEGER (size
, member
.ar_size
)
183 && PARSE_INTEGER (next_member
, member
.ar_nextoff
))
185 /* The archive is followed by an even-padded name, then by
186 a magic string of length SXCOFFARFMAG. The object itself
187 starts after that. */
188 offset
+= sizeof (member
) + namlen
+ SXCOFFARFMAG
;
189 offset
+= offset
& 1;
190 return read_xcoff_object (ldfile
, offset
, size
, next_member
);
195 /* Try to treat LDFILE as a non-empty big archive. Return true
196 on success, storing the member and object information for
197 the first member in LDFILE. */
200 read_big_archive (LDFILE
*ldfile
)
202 struct external_big_ar_filehdr filehdr
;
205 return (lseek (ldfile
->fd
, 0L, SEEK_SET
) == 0
206 && read (ldfile
->fd
, &filehdr
, sizeof (filehdr
)) == sizeof (filehdr
)
207 && memcmp (filehdr
.fl_magic
, FL_MAGIC_BIG_AR
, FL_MAGIC_SIZE
) == 0
208 && PARSE_INTEGER (offset
, filehdr
.fl_firstmemoff
)
209 && read_archive_member (ldfile
, offset
));
212 /* LDFILE is a zero-initialized structure. Try to open FILENAME,
213 returning true on success. */
216 open_file (LDFILE
*ldfile
, const char *filename
)
220 ldfile
->fd
= open (filename
, O_RDONLY
);
224 if (read_big_archive (ldfile
))
227 if (fstat (ldfile
->fd
, &st
) < 0)
230 return read_xcoff_object (ldfile
, 0, st
.st_size
, -1);
233 /* Release the memory associated with the current object, if one has
237 free_object (LDFILE
*ldfile
)
240 munmap (ldfile
->object
- ldfile
->page_offset
,
241 ldfile
->object_size
+ ldfile
->page_offset
);
244 /* Free LDFILE and all resources associated with it. */
247 free_ldfile (LDFILE
*ldfile
)
254 /* Implement the API-defined ldopen function. */
257 ldopen (char *filename
, LDFILE
*ldfile
)
261 ldfile
= XCNEW (LDFILE
);
262 if (!open_file (ldfile
, filename
))
264 free_object (ldfile
);
265 free_ldfile (ldfile
);
272 /* Implement the API-defined ldtbread function. */
275 ldtbread (LDFILE
*ldfile
, long index
, SYMENT
*internal
)
277 size_t offset
, name_length
;
280 /* Make sure that the symbol index is valid. */
281 if (index
< 0 || index
>= HEADER (ldfile
).f_nsyms
)
284 /* Work out the offset of the symbol table entry. */
285 offset
= HEADER (ldfile
).f_symptr
+ index
* sizeof (struct external_syment
);
286 if (!within_object_p (ldfile
, offset
, sizeof (struct external_syment
)))
289 /* Read all the fields. The format differs between 32-bit and
291 external
= ldfile
->object
+ offset
;
292 if (HEADER (ldfile
).f_magic
== U802TOCMAGIC
)
294 /* Copy the n_zeroes/n_offset interpretation. */
295 internal
->n_zeroes
= READ_OBJECT (external
, external_syment
,
296 u
.xcoff32
.u
.u
.n_zeroes
);
297 internal
->n_offset
= READ_OBJECT (external
, external_syment
,
298 u
.xcoff32
.u
.u
.n_offset
);
300 /* Copy the n_name interpretation. The internal version has room
301 for a null terminator. */
302 name_length
= FIELD_SIZE (external_syment
, u
.xcoff32
.u
.n_name
);
303 memcpy (internal
->n_name
,
304 external
+ offsetof (struct external_syment
, u
.xcoff32
.u
.n_name
),
306 internal
->n_name
[name_length
] = 0;
308 internal
->n_value
= READ_OBJECT (external
, external_syment
,
313 internal
->n_zeroes
= 0;
314 internal
->n_offset
= READ_OBJECT (external
, external_syment
,
316 internal
->n_value
= READ_OBJECT (external
, external_syment
,
319 COPY_FIELD (internal
, external
, external_syment
, n_scnum
);
320 COPY_FIELD (internal
, external
, external_syment
, n_type
);
321 COPY_FIELD (internal
, external
, external_syment
, n_sclass
);
322 COPY_FIELD (internal
, external
, external_syment
, n_numaux
);
326 /* Implement the API-defined ldgetname function. */
329 ldgetname (LDFILE
*ldfile
, SYMENT
*symbol
)
334 /* If the zeroes field is nonzero, the name is in the symbol table
336 if (symbol
->n_zeroes
!= 0)
337 return symbol
->n_name
;
339 /* Otherwise, the symbol table entry contains an offset into the
340 string table, which starts after the end of the symbol table. */
341 offset
= (HEADER (ldfile
).f_symptr
342 + HEADER (ldfile
).f_nsyms
* sizeof (struct external_syment
)
344 if (offset
>= ldfile
->object_size
)
347 /* Make sure that the name is entirely contained within the object. */
348 name
= ldfile
->object
+ offset
;
349 if (!string_within_bounds_p (name
, ldfile
->object_size
- offset
, '\0'))
355 /* Implement the API-defined ldclose function. */
358 ldclose (LDFILE
*ldfile
)
360 free_object (ldfile
);
361 if (ldfile
->next_member
>= 0
362 && read_archive_member (ldfile
, ldfile
->next_member
))
365 free_ldfile (ldfile
);