Fixed dates in ChangeLogs for my last commit - apologies
[official-gcc.git] / gcc / collect2-aix.c
blob7d25e7e6b89342a9fc66c3a0c43ace775fded15e
1 /* AIX cross support for collect2.
2 Copyright (C) 2009 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
9 version.
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
14 for more details.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "collect2-aix.h"
26 #ifdef CROSS_AIX_SUPPORT
28 #include <sys/mman.h>
30 /* Read SIZE bytes starting at DATA as a big-endian value. */
32 static inline bfd_vma
33 read_value (char *data, unsigned int size)
35 bfd_vma value;
36 unsigned int i;
38 value = 0;
39 for (i = 0; i < size; i++)
41 value <<= 8;
42 value += (unsigned char) data[i];
44 return value;
47 /* FIELD is a char array. Read the contents as a big-endian integer. */
48 #define READ_FIELD(FIELD) \
49 read_value (FIELD, sizeof (FIELD))
51 /* OBJECT is a char pointer to an in-file object of type struct TYPE.
52 Return the address of field FIELD. */
53 #define OBJECT_FIELD(OBJECT, TYPE, FIELD) \
54 (OBJECT) + offsetof (struct TYPE, FIELD)
56 /* Return the size of FIELD, which is a field of struct TYPE. */
57 #define FIELD_SIZE(TYPE, FIELD) \
58 sizeof (((struct TYPE *) (0))->FIELD)
60 /* OBJECT is a char pointer to an in-file object of type struct TYPE.
61 Read the value of field FIELD as a big-endian integer. */
62 #define READ_OBJECT(OBJECT, TYPE, FIELD) \
63 read_value (OBJECT_FIELD (OBJECT, TYPE, FIELD), FIELD_SIZE (TYPE, FIELD))
65 /* Copy FIELD from an external structure of type TYPE at address FROM
66 to an internal structure pointed to by TO. */
67 #define COPY_FIELD(TO, FROM, TYPE, FIELD) \
68 ((TO)->FIELD = READ_OBJECT (FROM, TYPE, FIELD))
70 /* Return true if STRING is less than SIZE bytes long. EXTRA_TERMINATOR
71 is another character (besides '\0') that acts as a terminator,
72 or '\0' if none. */
74 static bool
75 string_within_bounds_p (const char *string, size_t size, char extra_terminator)
77 const char *p;
79 for (p = string; p < string + size; p++)
80 if (*p == '\0' || *p == extra_terminator)
81 return true;
82 return false;
85 /* STRING is a pointer to a char array. Try to read its value as an
86 ASCII-encoded integer. On success, return true and store the result
87 in TARGET. */
88 #define PARSE_INTEGER(TARGET, STRING) \
89 (string_within_bounds_p (&(STRING)[0], sizeof (STRING), ' ') \
90 && ((TARGET) = strtoul (STRING, NULL, 0), true))
92 /* Check that LDFILE's current object has SIZE bytes starting at OFFSET. */
94 static inline bool
95 within_object_p (LDFILE *ldfile, size_t offset, size_t size)
97 return offset <= ldfile->object_size && offset + size <= ldfile->object_size;
100 /* Try to read the file header for an XCOFF object at OFFSET bytes into
101 LDFILE. The object is expected to be OBJECT_SIZE bytes in size.
102 If the object is a member of an archive, NEXT_MEMBER is the offset
103 of the next member, otherwise it is -1.
105 Return true on success, recording the object information in LDFILE. */
107 static bool
108 read_xcoff_object (LDFILE *ldfile, size_t offset, size_t object_size,
109 off_t next_member)
111 struct internal_filehdr *internal;
112 char *external;
113 void *map;
114 size_t page_size;
116 /* First try to map the file into memory. */
117 page_size = getpagesize ();
118 ldfile->page_offset = offset & (page_size - 1);
119 map = mmap (NULL, object_size + ldfile->page_offset, PROT_READ,
120 MAP_SHARED, ldfile->fd, offset - ldfile->page_offset);
121 if (map == MAP_FAILED)
122 return false;
124 /* Record the success. */
125 ldfile->object = (char *) map + ldfile->page_offset;
126 ldfile->object_size = object_size;
127 ldfile->next_member = next_member;
129 /* Read the magic value to determine the type of file. */
130 if (!within_object_p (ldfile, 0, F_MAGIC_SIZE))
131 return false;
133 internal = &ldfile->filehdr;
134 external = ldfile->object;
135 internal->f_magic = read_value (external, F_MAGIC_SIZE);
136 if (internal->f_magic == U802TOCMAGIC)
138 if (!within_object_p (ldfile, 0, sizeof (struct external_filehdr_32)))
139 return false;
141 COPY_FIELD (internal, external, external_filehdr_32, f_nscns);
142 COPY_FIELD (internal, external, external_filehdr_32, f_timdat);
143 COPY_FIELD (internal, external, external_filehdr_32, f_symptr);
144 COPY_FIELD (internal, external, external_filehdr_32, f_nsyms);
145 COPY_FIELD (internal, external, external_filehdr_32, f_opthdr);
146 COPY_FIELD (internal, external, external_filehdr_32, f_flags);
147 return true;
149 else if (internal->f_magic == U803XTOCMAGIC
150 || internal->f_magic == U64_TOCMAGIC)
152 if (!within_object_p (ldfile, 0, sizeof (struct external_filehdr_64)))
153 return false;
155 COPY_FIELD (internal, external, external_filehdr_64, f_nscns);
156 COPY_FIELD (internal, external, external_filehdr_64, f_timdat);
157 COPY_FIELD (internal, external, external_filehdr_64, f_symptr);
158 COPY_FIELD (internal, external, external_filehdr_64, f_nsyms);
159 COPY_FIELD (internal, external, external_filehdr_64, f_opthdr);
160 COPY_FIELD (internal, external, external_filehdr_64, f_flags);
161 return true;
163 return false;
166 /* Try to read an archive member at OFFSET bytes into LDFILE.
167 Return true on success, recording the member and object
168 information in LDFILE. */
170 static bool
171 read_archive_member (LDFILE *ldfile, size_t offset)
173 struct external_big_ar_member member;
174 size_t namlen;
175 size_t size;
176 off_t next_member;
178 if (lseek (ldfile->fd, offset, SEEK_SET) >= 0
179 && read (ldfile->fd, &member, sizeof (member)) == sizeof (member)
180 && PARSE_INTEGER (namlen, member.ar_namlen)
181 /* Stop once we reach the member table entry, which has a name
182 of length 0. */
183 && namlen > 0
184 && PARSE_INTEGER (size, member.ar_size)
185 && PARSE_INTEGER (next_member, member.ar_nextoff))
187 /* The archive is followed by an even-padded name, then by
188 a magic string of length SXCOFFARFMAG. The object itself
189 starts after that. */
190 offset += sizeof (member) + namlen + SXCOFFARFMAG;
191 offset += offset & 1;
192 return read_xcoff_object (ldfile, offset, size, next_member);
194 return false;
197 /* Try to treat LDFILE as a non-empty big archive. Return true
198 on success, storing the member and object information for
199 the first member in LDFILE. */
201 static bool
202 read_big_archive (LDFILE *ldfile)
204 struct external_big_ar_filehdr filehdr;
205 size_t offset;
207 return (lseek (ldfile->fd, 0L, SEEK_SET) == 0
208 && read (ldfile->fd, &filehdr, sizeof (filehdr)) == sizeof (filehdr)
209 && memcmp (filehdr.fl_magic, FL_MAGIC_BIG_AR, FL_MAGIC_SIZE) == 0
210 && PARSE_INTEGER (offset, filehdr.fl_firstmemoff)
211 && read_archive_member (ldfile, offset));
214 /* LDFILE is a zero-initialized structure. Try to open FILENAME,
215 returning true on success. */
217 static bool
218 open_file (LDFILE *ldfile, const char *filename)
220 struct stat st;
222 ldfile->fd = open (filename, O_RDONLY);
223 if (ldfile->fd < 0)
224 return false;
226 if (read_big_archive (ldfile))
227 return true;
229 if (fstat (ldfile->fd, &st) < 0)
230 return false;
232 return read_xcoff_object (ldfile, 0, st.st_size, -1);
235 /* Release the memory associated with the current object, if one has
236 been mapped. */
238 static void
239 free_object (LDFILE *ldfile)
241 if (ldfile->object)
242 munmap (ldfile->object - ldfile->page_offset,
243 ldfile->object_size + ldfile->page_offset);
246 /* Free LDFILE and all resources associated with it. */
248 static void
249 free_ldfile (LDFILE *ldfile)
251 if (ldfile->fd >= 0)
252 close (ldfile->fd);
253 XDELETE (ldfile);
256 /* Implement the API-defined ldopen function. */
258 LDFILE *
259 ldopen (char *filename, LDFILE *ldfile)
261 if (ldfile == NULL)
263 ldfile = XCNEW (LDFILE);
264 if (!open_file (ldfile, filename))
266 free_object (ldfile);
267 free_ldfile (ldfile);
268 return NULL;
271 return ldfile;
274 /* Implement the API-defined ldtbread function. */
277 ldtbread (LDFILE *ldfile, long index, SYMENT *internal)
279 size_t offset, name_length;
280 char *external;
282 /* Make sure that the symbol index is valid. */
283 if (index < 0 || index >= HEADER (ldfile).f_nsyms)
284 return FAILURE;
286 /* Work out the offset of the symbol table entry. */
287 offset = HEADER (ldfile).f_symptr + index * sizeof (struct external_syment);
288 if (!within_object_p (ldfile, offset, sizeof (struct external_syment)))
289 return FAILURE;
291 /* Read all the fields. The format differs between 32-bit and
292 64-bit files. */
293 external = ldfile->object + offset;
294 if (HEADER (ldfile).f_magic == U802TOCMAGIC)
296 /* Copy the n_zeroes/n_offset interpretation. */
297 internal->n_zeroes = READ_OBJECT (external, external_syment,
298 u.xcoff32.u.u.n_zeroes);
299 internal->n_offset = READ_OBJECT (external, external_syment,
300 u.xcoff32.u.u.n_offset);
302 /* Copy the n_name interpretation. The internal version has room
303 for a null terminator. */
304 name_length = FIELD_SIZE (external_syment, u.xcoff32.u.n_name);
305 memcpy (internal->n_name,
306 external + offsetof (struct external_syment, u.xcoff32.u.n_name),
307 name_length);
308 internal->n_name[name_length] = 0;
310 internal->n_value = READ_OBJECT (external, external_syment,
311 u.xcoff32.n_value);
313 else
315 internal->n_zeroes = 0;
316 internal->n_offset = READ_OBJECT (external, external_syment,
317 u.xcoff64.n_offset);
318 internal->n_value = READ_OBJECT (external, external_syment,
319 u.xcoff64.n_value);
321 COPY_FIELD (internal, external, external_syment, n_scnum);
322 COPY_FIELD (internal, external, external_syment, n_type);
323 COPY_FIELD (internal, external, external_syment, n_sclass);
324 COPY_FIELD (internal, external, external_syment, n_numaux);
325 return SUCCESS;
328 /* Implement the API-defined ldgetname function. */
330 char *
331 ldgetname (LDFILE *ldfile, SYMENT *symbol)
333 char *name;
334 size_t offset;
336 /* If the zeroes field is nonzero, the name is in the symbol table
337 entry itself. */
338 if (symbol->n_zeroes != 0)
339 return symbol->n_name;
341 /* Otherwise, the symbol table entry contains an offset into the
342 string table, which starts after the end of the symbol table. */
343 offset = (HEADER (ldfile).f_symptr
344 + HEADER (ldfile).f_nsyms * sizeof (struct external_syment)
345 + symbol->n_offset);
346 if (offset >= ldfile->object_size)
347 return NULL;
349 /* Make sure that the name is entirely contained within the object. */
350 name = ldfile->object + offset;
351 if (!string_within_bounds_p (name, ldfile->object_size - offset, '\0'))
352 return NULL;
354 return name;
357 /* Implement the API-defined ldclose function. */
360 ldclose (LDFILE *ldfile)
362 free_object (ldfile);
363 if (ldfile->next_member >= 0
364 && read_archive_member (ldfile, ldfile->next_member))
365 return FAILURE;
367 free_ldfile (ldfile);
368 return SUCCESS;
371 #endif