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