xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/collect2-aix.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /* AIX cross support for collect2.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
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.
10 
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.
15 
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/>.  */
19 
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "collect2-aix.h"
25 
26 #ifdef CROSS_AIX_SUPPORT
27 
28 #include <sys/mman.h>
29 
30 /* Read SIZE bytes starting at DATA as a big-endian value.  */
31 
32 static inline bfd_vma
33 read_value (char *data, unsigned int size)
34 {
35   bfd_vma value;
36   unsigned int i;
37 
38   value = 0;
39   for (i = 0; i < size; i++)
40     {
41       value <<= 8;
42       value += (unsigned char) data[i];
43     }
44   return value;
45 }
46 
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))
50 
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)
55 
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)
59 
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))
64 
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))
69 
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.  */
73 
74 static bool
75 string_within_bounds_p (const char *string, size_t size, char extra_terminator)
76 {
77   const char *p;
78 
79   for (p = string; p < string + size; p++)
80     if (*p == '\0' || *p == extra_terminator)
81       return true;
82   return false;
83 }
84 
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))
91 
92 /* Check that LDFILE's current object has SIZE bytes starting at OFFSET.  */
93 
94 static inline bool
95 within_object_p (LDFILE *ldfile, size_t offset, size_t size)
96 {
97   return offset <= ldfile->object_size && offset + size <= ldfile->object_size;
98 }
99 
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.
104 
105    Return true on success, recording the object information in LDFILE.  */
106 
107 static bool
108 read_xcoff_object (LDFILE *ldfile, size_t offset, size_t object_size,
109 		   off_t next_member)
110 {
111   struct internal_filehdr *internal;
112   char *external;
113   void *map;
114   size_t page_size;
115 
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;
123 
124   /* Record the success.  */
125   ldfile->object = (char *) map + ldfile->page_offset;
126   ldfile->object_size = object_size;
127   ldfile->next_member = next_member;
128 
129   /* Read the magic value to determine the type of file.  */
130   if (!within_object_p (ldfile, 0, F_MAGIC_SIZE))
131     return false;
132 
133   internal = &ldfile->filehdr;
134   external = ldfile->object;
135   internal->f_magic = read_value (external, F_MAGIC_SIZE);
136   if (internal->f_magic == U802TOCMAGIC)
137     {
138       if (!within_object_p (ldfile, 0, sizeof (struct external_filehdr_32)))
139 	return false;
140 
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;
148     }
149   else if (internal->f_magic == U803XTOCMAGIC
150 	   || internal->f_magic == U64_TOCMAGIC)
151     {
152       if (!within_object_p (ldfile, 0, sizeof (struct external_filehdr_64)))
153 	return false;
154 
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;
162     }
163   return false;
164 }
165 
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.  */
169 
170 static bool
171 read_archive_member (LDFILE *ldfile, size_t offset)
172 {
173   struct external_big_ar_member member;
174   size_t namlen;
175   size_t size;
176   off_t next_member;
177 
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))
186     {
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);
193     }
194   return false;
195 }
196 
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.  */
200 
201 static bool
202 read_big_archive (LDFILE *ldfile)
203 {
204   struct external_big_ar_filehdr filehdr;
205   size_t offset;
206 
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));
212 }
213 
214 /* LDFILE is a zero-initialized structure.  Try to open FILENAME,
215    returning true on success.  */
216 
217 static bool
218 open_file (LDFILE *ldfile, const char *filename)
219 {
220   struct stat st;
221 
222   ldfile->fd = open (filename, O_RDONLY);
223   if (ldfile->fd < 0)
224     return false;
225 
226   if (read_big_archive (ldfile))
227     return true;
228 
229   if (fstat (ldfile->fd, &st) < 0)
230     return false;
231 
232   return read_xcoff_object (ldfile, 0, st.st_size, -1);
233 }
234 
235 /* Release the memory associated with the current object, if one has
236    been mapped.  */
237 
238 static void
239 free_object (LDFILE *ldfile)
240 {
241   if (ldfile->object)
242     munmap (ldfile->object - ldfile->page_offset,
243 	    ldfile->object_size + ldfile->page_offset);
244 }
245 
246 /* Free LDFILE and all resources associated with it.  */
247 
248 static void
249 free_ldfile (LDFILE *ldfile)
250 {
251   if (ldfile->fd >= 0)
252     close (ldfile->fd);
253   XDELETE (ldfile);
254 }
255 
256 /* Implement the API-defined ldopen function.  */
257 
258 LDFILE *
259 ldopen (char *filename, LDFILE *ldfile)
260 {
261   if (ldfile == NULL)
262     {
263       ldfile = XCNEW (LDFILE);
264       if (!open_file (ldfile, filename))
265 	{
266 	  free_object (ldfile);
267 	  free_ldfile (ldfile);
268 	  return NULL;
269 	}
270     }
271   return ldfile;
272 }
273 
274 /* Implement the API-defined ldtbread function.  */
275 
276 int
277 ldtbread (LDFILE *ldfile, long index, SYMENT *internal)
278 {
279   size_t offset, name_length;
280   char *external;
281 
282   /* Make sure that the symbol index is valid.  */
283   if (index < 0 || index >= HEADER (ldfile).f_nsyms)
284     return FAILURE;
285 
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;
290 
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)
295     {
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);
301 
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;
309 
310       internal->n_value = READ_OBJECT (external, external_syment,
311 				       u.xcoff32.n_value);
312     }
313   else
314     {
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);
320     }
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;
326 }
327 
328 /* Implement the API-defined ldgetname function.  */
329 
330 char *
331 ldgetname (LDFILE *ldfile, SYMENT *symbol)
332 {
333   char *name;
334   size_t offset;
335 
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;
340 
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;
348 
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;
353 
354   return name;
355 }
356 
357 /* Implement the API-defined ldclose function.  */
358 
359 int
360 ldclose (LDFILE *ldfile)
361 {
362   free_object (ldfile);
363   if (ldfile->next_member >= 0
364       && read_archive_member (ldfile, ldfile->next_member))
365     return FAILURE;
366 
367   free_ldfile (ldfile);
368   return SUCCESS;
369 }
370 
371 #endif
372