Coverage Report

Created: 2026-03-10 08:46

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