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
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
30 /* Read SIZE bytes starting at DATA as a big-endian value. */
33 read_value (char *data, unsigned int size)
39 for (i = 0; i < size; i++)
42 value += (unsigned char) data[i];
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,
75 string_within_bounds_p (const char *string, size_t size, char extra_terminator)
79 for (p = string; p < string + size; p++)
80 if (*p == '\0' || *p == extra_terminator)
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
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. */
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. */
108 read_xcoff_object (LDFILE *ldfile, size_t offset, size_t object_size,
111 struct internal_filehdr *internal;
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)
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))
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)))
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);
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)))
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);
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. */
171 read_archive_member (LDFILE *ldfile, size_t offset)
173 struct external_big_ar_member 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
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);
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. */
202 read_big_archive (LDFILE *ldfile)
204 struct external_big_ar_filehdr filehdr;
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. */
218 open_file (LDFILE *ldfile, const char *filename)
222 ldfile->fd = open (filename, O_RDONLY);
226 if (read_big_archive (ldfile))
229 if (fstat (ldfile->fd, &st) < 0)
232 return read_xcoff_object (ldfile, 0, st.st_size, -1);
235 /* Release the memory associated with the current object, if one has
239 free_object (LDFILE *ldfile)
242 munmap (ldfile->object - ldfile->page_offset,
243 ldfile->object_size + ldfile->page_offset);
246 /* Free LDFILE and all resources associated with it. */
249 free_ldfile (LDFILE *ldfile)
256 /* Implement the API-defined ldopen function. */
259 ldopen (char *filename, LDFILE *ldfile)
263 ldfile = XCNEW (LDFILE);
264 if (!open_file (ldfile, filename))
266 free_object (ldfile);
267 free_ldfile (ldfile);
274 /* Implement the API-defined ldtbread function. */
277 ldtbread (LDFILE *ldfile, long index, SYMENT *internal)
279 size_t offset, name_length;
282 /* Make sure that the symbol index is valid. */
283 if (index < 0 || index >= HEADER (ldfile).f_nsyms)
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)))
291 /* Read all the fields. The format differs between 32-bit and
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),
308 internal->n_name[name_length] = 0;
310 internal->n_value = READ_OBJECT (external, external_syment,
315 internal->n_zeroes = 0;
316 internal->n_offset = READ_OBJECT (external, external_syment,
318 internal->n_value = READ_OBJECT (external, external_syment,
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);
328 /* Implement the API-defined ldgetname function. */
331 ldgetname (LDFILE *ldfile, SYMENT *symbol)
336 /* If the zeroes field is nonzero, the name is in the symbol table
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)
346 if (offset >= ldfile->object_size)
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'))
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))
367 free_ldfile (ldfile);