be64e0d373d82fe351d57c4b4d96dda1940448d3
[dragonfly.git] / contrib / gdb-7 / bfd / archive64.c
1 /* Support for 64-bit ELF archives.
2    Copyright 1996-2013 Free Software Foundation, Inc.
3    Ian Lance Taylor, Cygnus Support
4    Linker support added by Mark Mitchell, CodeSourcery, LLC.
5    <mark@codesourcery.com>
6
7    This file is part of BFD, the Binary File Descriptor library.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22    MA 02110-1301, USA.  */
23
24 /* This file supports the 64-bit (MIPS) ELF archives.  */
25
26 #include "sysdep.h"
27 #include "bfd.h"
28 #include "libbfd.h"
29 #include "aout/ar.h"
30
31 /* Irix 6 defines a 64bit archive map format, so that they can
32    have archives more than 4 GB in size.  */
33
34 bfd_boolean bfd_elf64_archive_slurp_armap (bfd *);
35 bfd_boolean bfd_elf64_archive_write_armap
36   (bfd *, unsigned int, struct orl *, unsigned int, int);
37
38 /* Read an Irix 6 armap.  */
39
40 bfd_boolean
41 bfd_elf64_archive_slurp_armap (bfd *abfd)
42 {
43   struct artdata *ardata = bfd_ardata (abfd);
44   char nextname[17];
45   bfd_size_type i, parsed_size, nsymz, stringsize, carsym_size, ptrsize;
46   struct areltdata *mapdata;
47   bfd_byte int_buf[8];
48   char *stringbase;
49   bfd_byte *raw_armap = NULL;
50   carsym *carsyms;
51   bfd_size_type amt;
52
53   ardata->symdefs = NULL;
54
55   /* Get the name of the first element.  */
56   i = bfd_bread (nextname, 16, abfd);
57   if (i == 0)
58     return TRUE;
59   if (i != 16)
60     return FALSE;
61
62   if (bfd_seek (abfd, (file_ptr) - 16, SEEK_CUR) != 0)
63     return FALSE;
64
65   /* Archives with traditional armaps are still permitted.  */
66   if (CONST_STRNEQ (nextname, "/               "))
67     return bfd_slurp_armap (abfd);
68
69   if (! CONST_STRNEQ (nextname, "/SYM64/         "))
70     {
71       bfd_has_map (abfd) = FALSE;
72       return TRUE;
73     }
74
75   mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
76   if (mapdata == NULL)
77     return FALSE;
78   parsed_size = mapdata->parsed_size;
79   free (mapdata);
80
81   if (bfd_bread (int_buf, 8, abfd) != 8)
82     {
83       if (bfd_get_error () != bfd_error_system_call)
84         bfd_set_error (bfd_error_malformed_archive);
85       return FALSE;
86     }
87
88   nsymz = bfd_getb64 (int_buf);
89   stringsize = parsed_size - 8 * nsymz - 8;
90
91   carsym_size = nsymz * sizeof (carsym);
92   ptrsize = 8 * nsymz;
93
94   amt = carsym_size + stringsize + 1;
95   ardata->symdefs = (struct carsym *) bfd_zalloc (abfd, amt);
96   if (ardata->symdefs == NULL)
97     return FALSE;
98   carsyms = ardata->symdefs;
99   stringbase = ((char *) ardata->symdefs) + carsym_size;
100
101   raw_armap = (bfd_byte *) bfd_alloc (abfd, ptrsize);
102   if (raw_armap == NULL)
103     goto release_symdefs;
104
105   if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize
106       || bfd_bread (stringbase, stringsize, abfd) != stringsize)
107     {
108       if (bfd_get_error () != bfd_error_system_call)
109         bfd_set_error (bfd_error_malformed_archive);
110       goto release_raw_armap;
111     }
112
113   for (i = 0; i < nsymz; i++)
114     {
115       carsyms->file_offset = bfd_getb64 (raw_armap + i * 8);
116       carsyms->name = stringbase;
117       stringbase += strlen (stringbase) + 1;
118       ++carsyms;
119     }
120   *stringbase = '\0';
121
122   ardata->symdef_count = nsymz;
123   ardata->first_file_filepos = bfd_tell (abfd);
124   /* Pad to an even boundary if you have to.  */
125   ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
126
127   bfd_has_map (abfd) = TRUE;
128   bfd_release (abfd, raw_armap);
129
130   return TRUE;
131
132 release_raw_armap:
133   bfd_release (abfd, raw_armap);
134 release_symdefs:
135   bfd_release (abfd, ardata->symdefs);
136   return FALSE;
137 }
138
139 /* Write out an Irix 6 armap.  The Irix 6 tools are supposed to be
140    able to handle ordinary ELF armaps, but at least on Irix 6.2 the
141    linker crashes.  */
142
143 bfd_boolean
144 bfd_elf64_archive_write_armap (bfd *arch,
145                                unsigned int elength,
146                                struct orl *map,
147                                unsigned int symbol_count,
148                                int stridx)
149 {
150   unsigned int ranlibsize = (symbol_count * 8) + 8;
151   unsigned int stringsize = stridx;
152   unsigned int mapsize = stringsize + ranlibsize;
153   file_ptr archive_member_file_ptr;
154   bfd *current = arch->archive_head;
155   unsigned int count;
156   struct ar_hdr hdr;
157   int padding;
158   bfd_byte buf[8];
159
160   padding = BFD_ALIGN (mapsize, 8) - mapsize;
161   mapsize += padding;
162
163   /* work out where the first object file will go in the archive */
164   archive_member_file_ptr = (mapsize
165                              + elength
166                              + sizeof (struct ar_hdr)
167                              + SARMAG);
168
169   memset (&hdr, ' ', sizeof (struct ar_hdr));
170   memcpy (hdr.ar_name, "/SYM64/", strlen ("/SYM64/"));
171   if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize))
172     return FALSE;
173   _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
174                     time (NULL));
175   /* This, at least, is what Intel coff sets the values to.: */
176   _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0);
177   _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0);
178   _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0);
179   memcpy (hdr.ar_fmag, ARFMAG, 2);
180
181   /* Write the ar header for this item and the number of symbols */
182
183   if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
184       != sizeof (struct ar_hdr))
185     return FALSE;
186
187   bfd_putb64 ((bfd_vma) symbol_count, buf);
188   if (bfd_bwrite (buf, 8, arch) != 8)
189     return FALSE;
190
191   /* Two passes, first write the file offsets for each symbol -
192      remembering that each offset is on a two byte boundary.  */
193
194   /* Write out the file offset for the file associated with each
195      symbol, and remember to keep the offsets padded out.  */
196   count = 0;
197   for (current = arch->archive_head;
198        current != NULL && count < symbol_count;
199        current = current->archive_next)
200     {
201       /* For each symbol which is used defined in this object, write out
202          the object file's address in the archive.  */
203
204       for (;
205            count < symbol_count && map[count].u.abfd == current;
206            count++)
207         {
208           bfd_putb64 ((bfd_vma) archive_member_file_ptr, buf);
209           if (bfd_bwrite (buf, 8, arch) != 8)
210             return FALSE;
211         }
212
213       /* Add size of this archive entry */
214       archive_member_file_ptr += sizeof (struct ar_hdr);
215       if (! bfd_is_thin_archive (arch))
216         archive_member_file_ptr += arelt_size (current);
217       /* remember about the even alignment */
218       archive_member_file_ptr += archive_member_file_ptr % 2;
219     }
220
221   /* now write the strings themselves */
222   for (count = 0; count < symbol_count; count++)
223     {
224       size_t len = strlen (*map[count].name) + 1;
225
226       if (bfd_bwrite (*map[count].name, len, arch) != len)
227         return FALSE;
228     }
229
230   /* The spec says that this should be padded to an 8 byte boundary.
231      However, the Irix 6.2 tools do not appear to do this.  */
232   while (padding != 0)
233     {
234       if (bfd_bwrite ("", 1, arch) != 1)
235         return FALSE;
236       --padding;
237     }
238
239   return TRUE;
240 }