1 /* Support for 64-bit archives.
2 Copyright (C) 1996-2023 Free Software Foundation, Inc.
3 Ian Lance Taylor, Cygnus Support
4 Linker support added by Mark Mitchell, CodeSourcery, LLC.
5 <mark@codesourcery.com>
7 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
24 /* This file supports the 64-bit archives. We use the same format as
25 the 64-bit (MIPS) ELF archives. */
32 /* Irix 6 defines a 64bit archive map format, so that they can
33 have archives more than 4 GB in size. */
35 /* Read an Irix 6 armap. */
38 _bfd_archive_64_bit_slurp_armap (bfd
*abfd
)
40 struct artdata
*ardata
= bfd_ardata (abfd
);
42 bfd_size_type i
, parsed_size
, nsymz
, stringsize
, carsym_size
, ptrsize
;
43 struct areltdata
*mapdata
;
47 bfd_byte
*raw_armap
= NULL
;
52 ardata
->symdefs
= NULL
;
54 /* Get the name of the first element. */
55 i
= bfd_bread (nextname
, 16, abfd
);
61 if (bfd_seek (abfd
, (file_ptr
) - 16, SEEK_CUR
) != 0)
64 /* Archives with traditional armaps are still permitted. */
65 if (startswith (nextname
, "/ "))
66 return bfd_slurp_armap (abfd
);
68 if (! startswith (nextname
, "/SYM64/ "))
70 abfd
->has_armap
= false;
74 mapdata
= (struct areltdata
*) _bfd_read_ar_hdr (abfd
);
77 parsed_size
= mapdata
->parsed_size
;
80 filesize
= bfd_get_file_size (abfd
);
81 if (filesize
!= 0 && parsed_size
> filesize
)
83 bfd_set_error (bfd_error_malformed_archive
);
87 if (bfd_bread (int_buf
, 8, abfd
) != 8)
89 if (bfd_get_error () != bfd_error_system_call
)
90 bfd_set_error (bfd_error_malformed_archive
);
94 nsymz
= bfd_getb64 (int_buf
);
95 stringsize
= parsed_size
- 8 * nsymz
- 8;
97 carsym_size
= nsymz
* sizeof (carsym
);
100 amt
= carsym_size
+ stringsize
+ 1;
101 if (/* Catch overflow in stringsize (and ptrsize) expression. */
102 nsymz
>= (bfd_size_type
) -1 / 8
103 || stringsize
> parsed_size
104 /* Catch overflow in carsym_size expression. */
105 || nsymz
> (bfd_size_type
) -1 / sizeof (carsym
)
106 /* Catch overflow in amt expression. */
107 || amt
<= carsym_size
108 || amt
<= stringsize
)
110 bfd_set_error (bfd_error_malformed_archive
);
113 ardata
->symdefs
= (struct carsym
*) bfd_alloc (abfd
, amt
);
114 if (ardata
->symdefs
== NULL
)
116 carsyms
= ardata
->symdefs
;
117 stringbase
= ((char *) ardata
->symdefs
) + carsym_size
;
119 raw_armap
= (bfd_byte
*) _bfd_alloc_and_read (abfd
, ptrsize
, ptrsize
);
120 if (raw_armap
== NULL
121 || bfd_bread (stringbase
, stringsize
, abfd
) != stringsize
)
123 if (bfd_get_error () != bfd_error_system_call
)
124 bfd_set_error (bfd_error_malformed_archive
);
125 goto release_symdefs
;
128 stringend
= stringbase
+ stringsize
;
130 for (i
= 0; i
< nsymz
; i
++)
132 carsyms
->file_offset
= bfd_getb64 (raw_armap
+ i
* 8);
133 carsyms
->name
= stringbase
;
134 stringbase
+= strlen (stringbase
);
135 if (stringbase
!= stringend
)
140 ardata
->symdef_count
= nsymz
;
141 ardata
->first_file_filepos
= bfd_tell (abfd
);
142 /* Pad to an even boundary if you have to. */
143 ardata
->first_file_filepos
+= (ardata
->first_file_filepos
) % 2;
145 abfd
->has_armap
= true;
146 bfd_release (abfd
, raw_armap
);
151 bfd_release (abfd
, ardata
->symdefs
);
155 /* Write out an Irix 6 armap. The Irix 6 tools are supposed to be
156 able to handle ordinary ELF armaps, but at least on Irix 6.2 the
160 _bfd_archive_64_bit_write_armap (bfd
*arch
,
161 unsigned int elength
,
163 unsigned int symbol_count
,
166 unsigned int ranlibsize
= (symbol_count
* 8) + 8;
167 unsigned int stringsize
= stridx
;
168 unsigned int mapsize
= stringsize
+ ranlibsize
;
169 file_ptr archive_member_file_ptr
;
170 bfd
*current
= arch
->archive_head
;
176 padding
= BFD_ALIGN (mapsize
, 8) - mapsize
;
179 /* work out where the first object file will go in the archive */
180 archive_member_file_ptr
= (mapsize
182 + sizeof (struct ar_hdr
)
185 memset (&hdr
, ' ', sizeof (struct ar_hdr
));
186 memcpy (hdr
.ar_name
, "/SYM64/", strlen ("/SYM64/"));
187 if (!_bfd_ar_sizepad (hdr
.ar_size
, sizeof (hdr
.ar_size
), mapsize
))
189 _bfd_ar_spacepad (hdr
.ar_date
, sizeof (hdr
.ar_date
), "%ld",
191 /* This, at least, is what Intel coff sets the values to.: */
192 _bfd_ar_spacepad (hdr
.ar_uid
, sizeof (hdr
.ar_uid
), "%ld", 0);
193 _bfd_ar_spacepad (hdr
.ar_gid
, sizeof (hdr
.ar_gid
), "%ld", 0);
194 _bfd_ar_spacepad (hdr
.ar_mode
, sizeof (hdr
.ar_mode
), "%-7lo", 0);
195 memcpy (hdr
.ar_fmag
, ARFMAG
, 2);
197 /* Write the ar header for this item and the number of symbols */
199 if (bfd_bwrite (&hdr
, sizeof (struct ar_hdr
), arch
)
200 != sizeof (struct ar_hdr
))
203 bfd_putb64 ((bfd_vma
) symbol_count
, buf
);
204 if (bfd_bwrite (buf
, 8, arch
) != 8)
207 /* Two passes, first write the file offsets for each symbol -
208 remembering that each offset is on a two byte boundary. */
210 /* Write out the file offset for the file associated with each
211 symbol, and remember to keep the offsets padded out. */
213 for (current
= arch
->archive_head
;
214 current
!= NULL
&& count
< symbol_count
;
215 current
= current
->archive_next
)
217 /* For each symbol which is used defined in this object, write out
218 the object file's address in the archive. */
221 count
< symbol_count
&& map
[count
].u
.abfd
== current
;
224 bfd_putb64 ((bfd_vma
) archive_member_file_ptr
, buf
);
225 if (bfd_bwrite (buf
, 8, arch
) != 8)
229 /* Add size of this archive entry */
230 archive_member_file_ptr
+= sizeof (struct ar_hdr
);
231 if (! bfd_is_thin_archive (arch
))
232 archive_member_file_ptr
+= arelt_size (current
);
233 /* remember about the even alignment */
234 archive_member_file_ptr
+= archive_member_file_ptr
% 2;
237 /* now write the strings themselves */
238 for (count
= 0; count
< symbol_count
; count
++)
240 size_t len
= strlen (*map
[count
].name
) + 1;
242 if (bfd_bwrite (*map
[count
].name
, len
, arch
) != len
)
246 /* The spec says that this should be padded to an 8 byte boundary.
247 However, the Irix 6.2 tools do not appear to do this. */
250 if (bfd_bwrite ("", 1, arch
) != 1)