1/* Support for 64-bit archives.
2   Copyright (C) 1996-2020 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 archives.  We use the same format as
25   the 64-bit (MIPS) ELF archives.  */
26
27#include "sysdep.h"
28#include "bfd.h"
29#include "libbfd.h"
30#include "aout/ar.h"
31
32/* Irix 6 defines a 64bit archive map format, so that they can
33   have archives more than 4 GB in size.  */
34
35/* Read an Irix 6 armap.  */
36
37bfd_boolean
38_bfd_archive_64_bit_slurp_armap (bfd *abfd)
39{
40  struct artdata *ardata = bfd_ardata (abfd);
41  char nextname[17];
42  bfd_size_type i, parsed_size, nsymz, stringsize, carsym_size, ptrsize;
43  struct areltdata *mapdata;
44  bfd_byte int_buf[8];
45  char *stringbase;
46  char *stringend;
47  bfd_byte *raw_armap = NULL;
48  carsym *carsyms;
49  bfd_size_type amt;
50
51  ardata->symdefs = NULL;
52
53  /* Get the name of the first element.  */
54  i = bfd_bread (nextname, 16, abfd);
55  if (i == 0)
56    return TRUE;
57  if (i != 16)
58    return FALSE;
59
60  if (bfd_seek (abfd, (file_ptr) - 16, SEEK_CUR) != 0)
61    return FALSE;
62
63  /* Archives with traditional armaps are still permitted.  */
64  if (CONST_STRNEQ (nextname, "/               "))
65    return bfd_slurp_armap (abfd);
66
67  if (! CONST_STRNEQ (nextname, "/SYM64/         "))
68    {
69      abfd->has_armap = FALSE;
70      return TRUE;
71    }
72
73  mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
74  if (mapdata == NULL)
75    return FALSE;
76  parsed_size = mapdata->parsed_size;
77  free (mapdata);
78
79  if (bfd_bread (int_buf, 8, abfd) != 8)
80    {
81      if (bfd_get_error () != bfd_error_system_call)
82	bfd_set_error (bfd_error_malformed_archive);
83      return FALSE;
84    }
85
86  nsymz = bfd_getb64 (int_buf);
87  stringsize = parsed_size - 8 * nsymz - 8;
88
89  carsym_size = nsymz * sizeof (carsym);
90  ptrsize = 8 * nsymz;
91
92  amt = carsym_size + stringsize + 1;
93  if (/* Catch overflow in stringsize (and ptrsize) expression.  */
94      nsymz >= (bfd_size_type) -1 / 8
95      || stringsize > parsed_size
96      /* Catch overflow in carsym_size expression.  */
97      || nsymz > (bfd_size_type) -1 / sizeof (carsym)
98      /* Catch overflow in amt expression.  */
99      || amt <= carsym_size
100      || amt <= stringsize)
101    {
102      bfd_set_error (bfd_error_malformed_archive);
103      return FALSE;
104    }
105  ardata->symdefs = (struct carsym *) bfd_zalloc (abfd, amt);
106  if (ardata->symdefs == NULL)
107    return FALSE;
108  carsyms = ardata->symdefs;
109  stringbase = ((char *) ardata->symdefs) + carsym_size;
110
111  raw_armap = (bfd_byte *) bfd_alloc (abfd, ptrsize);
112  if (raw_armap == NULL)
113    goto release_symdefs;
114
115  if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize
116      || bfd_bread (stringbase, stringsize, abfd) != stringsize)
117    {
118      if (bfd_get_error () != bfd_error_system_call)
119	bfd_set_error (bfd_error_malformed_archive);
120      goto release_raw_armap;
121    }
122
123  stringend = stringbase + stringsize;
124  *stringend = 0;
125  for (i = 0; i < nsymz; i++)
126    {
127      carsyms->file_offset = bfd_getb64 (raw_armap + i * 8);
128      carsyms->name = stringbase;
129      stringbase += strlen (stringbase);
130      if (stringbase != stringend)
131	++stringbase;
132      ++carsyms;
133    }
134
135  ardata->symdef_count = nsymz;
136  ardata->first_file_filepos = bfd_tell (abfd);
137  /* Pad to an even boundary if you have to.  */
138  ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
139
140  abfd->has_armap = TRUE;
141  bfd_release (abfd, raw_armap);
142
143  return TRUE;
144
145release_raw_armap:
146  bfd_release (abfd, raw_armap);
147release_symdefs:
148  bfd_release (abfd, ardata->symdefs);
149  return FALSE;
150}
151
152/* Write out an Irix 6 armap.  The Irix 6 tools are supposed to be
153   able to handle ordinary ELF armaps, but at least on Irix 6.2 the
154   linker crashes.  */
155
156bfd_boolean
157_bfd_archive_64_bit_write_armap (bfd *arch,
158				 unsigned int elength,
159				 struct orl *map,
160				 unsigned int symbol_count,
161				 int stridx)
162{
163  unsigned int ranlibsize = (symbol_count * 8) + 8;
164  unsigned int stringsize = stridx;
165  unsigned int mapsize = stringsize + ranlibsize;
166  file_ptr archive_member_file_ptr;
167  bfd *current = arch->archive_head;
168  unsigned int count;
169  struct ar_hdr hdr;
170  int padding;
171  bfd_byte buf[8];
172
173  padding = BFD_ALIGN (mapsize, 8) - mapsize;
174  mapsize += padding;
175
176  /* work out where the first object file will go in the archive */
177  archive_member_file_ptr = (mapsize
178			     + elength
179			     + sizeof (struct ar_hdr)
180			     + SARMAG);
181
182  memset (&hdr, ' ', sizeof (struct ar_hdr));
183  memcpy (hdr.ar_name, "/SYM64/", strlen ("/SYM64/"));
184  if (!_bfd_ar_sizepad (hdr.ar_size, sizeof (hdr.ar_size), mapsize))
185    return FALSE;
186  _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
187		    time (NULL));
188  /* This, at least, is what Intel coff sets the values to.: */
189  _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0);
190  _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0);
191  _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0);
192  memcpy (hdr.ar_fmag, ARFMAG, 2);
193
194  /* Write the ar header for this item and the number of symbols */
195
196  if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
197      != sizeof (struct ar_hdr))
198    return FALSE;
199
200  bfd_putb64 ((bfd_vma) symbol_count, buf);
201  if (bfd_bwrite (buf, 8, arch) != 8)
202    return FALSE;
203
204  /* Two passes, first write the file offsets for each symbol -
205     remembering that each offset is on a two byte boundary.  */
206
207  /* Write out the file offset for the file associated with each
208     symbol, and remember to keep the offsets padded out.  */
209  count = 0;
210  for (current = arch->archive_head;
211       current != NULL && count < symbol_count;
212       current = current->archive_next)
213    {
214      /* For each symbol which is used defined in this object, write out
215	 the object file's address in the archive.  */
216
217      for (;
218	   count < symbol_count && map[count].u.abfd == current;
219	   count++)
220	{
221	  bfd_putb64 ((bfd_vma) archive_member_file_ptr, buf);
222	  if (bfd_bwrite (buf, 8, arch) != 8)
223	    return FALSE;
224	}
225
226      /* Add size of this archive entry */
227      archive_member_file_ptr += sizeof (struct ar_hdr);
228      if (! bfd_is_thin_archive (arch))
229	archive_member_file_ptr += arelt_size (current);
230      /* remember about the even alignment */
231      archive_member_file_ptr += archive_member_file_ptr % 2;
232    }
233
234  /* now write the strings themselves */
235  for (count = 0; count < symbol_count; count++)
236    {
237      size_t len = strlen (*map[count].name) + 1;
238
239      if (bfd_bwrite (*map[count].name, len, arch) != len)
240	return FALSE;
241    }
242
243  /* The spec says that this should be padded to an 8 byte boundary.
244     However, the Irix 6.2 tools do not appear to do this.  */
245  while (padding != 0)
246    {
247      if (bfd_bwrite ("", 1, arch) != 1)
248	return FALSE;
249      --padding;
250    }
251
252  return TRUE;
253}
254