xref: /netbsd/external/gpl3/gdb.old/dist/bfd/libbfd.c (revision 184b2d41)
1a1ba9ba4Schristos /* Assorted BFD support routines, only used internally.
2*184b2d41Schristos    Copyright (C) 1990-2020 Free Software Foundation, Inc.
3a1ba9ba4Schristos    Written by Cygnus Support.
4a1ba9ba4Schristos 
5a1ba9ba4Schristos    This file is part of BFD, the Binary File Descriptor library.
6a1ba9ba4Schristos 
7a1ba9ba4Schristos    This program is free software; you can redistribute it and/or modify
8a1ba9ba4Schristos    it under the terms of the GNU General Public License as published by
9a1ba9ba4Schristos    the Free Software Foundation; either version 3 of the License, or
10a1ba9ba4Schristos    (at your option) any later version.
11a1ba9ba4Schristos 
12a1ba9ba4Schristos    This program is distributed in the hope that it will be useful,
13a1ba9ba4Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
14a1ba9ba4Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15a1ba9ba4Schristos    GNU General Public License for more details.
16a1ba9ba4Schristos 
17a1ba9ba4Schristos    You should have received a copy of the GNU General Public License
18a1ba9ba4Schristos    along with this program; if not, write to the Free Software
19a1ba9ba4Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20a1ba9ba4Schristos    MA 02110-1301, USA.  */
21a1ba9ba4Schristos 
22a1ba9ba4Schristos #include "sysdep.h"
23a1ba9ba4Schristos #include "bfd.h"
24a1ba9ba4Schristos #include "libbfd.h"
25a1ba9ba4Schristos 
26a1ba9ba4Schristos #ifndef HAVE_GETPAGESIZE
27a1ba9ba4Schristos #define getpagesize() 2048
28a1ba9ba4Schristos #endif
29a1ba9ba4Schristos 
30a1ba9ba4Schristos /*
31a1ba9ba4Schristos SECTION
32a1ba9ba4Schristos 	Implementation details
33a1ba9ba4Schristos 
34a1ba9ba4Schristos SUBSECTION
35a1ba9ba4Schristos 	Internal functions
36a1ba9ba4Schristos 
37a1ba9ba4Schristos DESCRIPTION
38a1ba9ba4Schristos 	These routines are used within BFD.
39a1ba9ba4Schristos 	They are not intended for export, but are documented here for
40a1ba9ba4Schristos 	completeness.
41a1ba9ba4Schristos */
42a1ba9ba4Schristos 
43051580eeSchristos bfd_boolean
_bfd_bool_bfd_false(bfd * abfd ATTRIBUTE_UNUSED)44051580eeSchristos _bfd_bool_bfd_false (bfd *abfd ATTRIBUTE_UNUSED)
45051580eeSchristos {
46051580eeSchristos   return FALSE;
47051580eeSchristos }
48051580eeSchristos 
49051580eeSchristos bfd_boolean
_bfd_bool_bfd_asymbol_false(bfd * abfd ATTRIBUTE_UNUSED,asymbol * sym ATTRIBUTE_UNUSED)50051580eeSchristos _bfd_bool_bfd_asymbol_false (bfd *abfd ATTRIBUTE_UNUSED,
51051580eeSchristos 			     asymbol *sym ATTRIBUTE_UNUSED)
52051580eeSchristos {
53051580eeSchristos   return FALSE;
54051580eeSchristos }
55051580eeSchristos 
56a1ba9ba4Schristos /* A routine which is used in target vectors for unsupported
57a1ba9ba4Schristos    operations.  */
58a1ba9ba4Schristos 
59a1ba9ba4Schristos bfd_boolean
_bfd_bool_bfd_false_error(bfd * ignore ATTRIBUTE_UNUSED)60051580eeSchristos _bfd_bool_bfd_false_error (bfd *ignore ATTRIBUTE_UNUSED)
61a1ba9ba4Schristos {
62a1ba9ba4Schristos   bfd_set_error (bfd_error_invalid_operation);
63a1ba9ba4Schristos   return FALSE;
64a1ba9ba4Schristos }
65a1ba9ba4Schristos 
66051580eeSchristos bfd_boolean
_bfd_bool_bfd_link_false_error(bfd * abfd,struct bfd_link_info * info ATTRIBUTE_UNUSED)67051580eeSchristos _bfd_bool_bfd_link_false_error (bfd *abfd,
68051580eeSchristos 				struct bfd_link_info *info ATTRIBUTE_UNUSED)
69051580eeSchristos {
70051580eeSchristos   return _bfd_bool_bfd_false_error (abfd);
71051580eeSchristos }
72051580eeSchristos 
73a1ba9ba4Schristos /* A routine which is used in target vectors for supported operations
74a1ba9ba4Schristos    which do not actually do anything.  */
75a1ba9ba4Schristos 
76a1ba9ba4Schristos bfd_boolean
_bfd_bool_bfd_true(bfd * ignore ATTRIBUTE_UNUSED)77051580eeSchristos _bfd_bool_bfd_true (bfd *ignore ATTRIBUTE_UNUSED)
78051580eeSchristos {
79051580eeSchristos   return TRUE;
80051580eeSchristos }
81051580eeSchristos 
82051580eeSchristos bfd_boolean
_bfd_bool_bfd_link_true(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info ATTRIBUTE_UNUSED)83051580eeSchristos _bfd_bool_bfd_link_true (bfd *abfd ATTRIBUTE_UNUSED,
84051580eeSchristos 			 struct bfd_link_info *info ATTRIBUTE_UNUSED)
85051580eeSchristos {
86051580eeSchristos   return TRUE;
87051580eeSchristos }
88051580eeSchristos 
89051580eeSchristos bfd_boolean
_bfd_bool_bfd_bfd_true(bfd * ibfd ATTRIBUTE_UNUSED,bfd * obfd ATTRIBUTE_UNUSED)90051580eeSchristos _bfd_bool_bfd_bfd_true (bfd *ibfd ATTRIBUTE_UNUSED,
91051580eeSchristos 			bfd *obfd ATTRIBUTE_UNUSED)
92051580eeSchristos {
93051580eeSchristos   return TRUE;
94051580eeSchristos }
95051580eeSchristos 
96051580eeSchristos bfd_boolean
_bfd_bool_bfd_uint_true(bfd * abfd ATTRIBUTE_UNUSED,unsigned int flags ATTRIBUTE_UNUSED)97051580eeSchristos _bfd_bool_bfd_uint_true (bfd *abfd ATTRIBUTE_UNUSED,
98051580eeSchristos 			 unsigned int flags ATTRIBUTE_UNUSED)
99051580eeSchristos {
100051580eeSchristos   return TRUE;
101051580eeSchristos }
102051580eeSchristos 
103051580eeSchristos bfd_boolean
_bfd_bool_bfd_asection_bfd_asection_true(bfd * ibfd ATTRIBUTE_UNUSED,asection * isec ATTRIBUTE_UNUSED,bfd * obfd ATTRIBUTE_UNUSED,asection * osec ATTRIBUTE_UNUSED)104051580eeSchristos _bfd_bool_bfd_asection_bfd_asection_true (bfd *ibfd ATTRIBUTE_UNUSED,
105051580eeSchristos 					  asection *isec ATTRIBUTE_UNUSED,
106051580eeSchristos 					  bfd *obfd ATTRIBUTE_UNUSED,
107051580eeSchristos 					  asection *osec ATTRIBUTE_UNUSED)
108051580eeSchristos {
109051580eeSchristos   return TRUE;
110051580eeSchristos }
111051580eeSchristos 
112051580eeSchristos bfd_boolean
_bfd_bool_bfd_asymbol_bfd_asymbol_true(bfd * ibfd ATTRIBUTE_UNUSED,asymbol * isym ATTRIBUTE_UNUSED,bfd * obfd ATTRIBUTE_UNUSED,asymbol * osym ATTRIBUTE_UNUSED)113051580eeSchristos _bfd_bool_bfd_asymbol_bfd_asymbol_true (bfd *ibfd ATTRIBUTE_UNUSED,
114051580eeSchristos 					asymbol *isym ATTRIBUTE_UNUSED,
115051580eeSchristos 					bfd *obfd ATTRIBUTE_UNUSED,
116051580eeSchristos 					asymbol *osym ATTRIBUTE_UNUSED)
117051580eeSchristos {
118051580eeSchristos   return TRUE;
119051580eeSchristos }
120051580eeSchristos 
121051580eeSchristos bfd_boolean
_bfd_bool_bfd_ptr_true(bfd * abfd ATTRIBUTE_UNUSED,void * ptr ATTRIBUTE_UNUSED)122051580eeSchristos _bfd_bool_bfd_ptr_true (bfd *abfd ATTRIBUTE_UNUSED,
123051580eeSchristos 			void *ptr ATTRIBUTE_UNUSED)
124a1ba9ba4Schristos {
125a1ba9ba4Schristos   return TRUE;
126a1ba9ba4Schristos }
127a1ba9ba4Schristos 
128a1ba9ba4Schristos /* A routine which is used in target vectors for unsupported
129a1ba9ba4Schristos    operations which return a pointer value.  */
130a1ba9ba4Schristos 
131a1ba9ba4Schristos void *
_bfd_ptr_bfd_null_error(bfd * ignore ATTRIBUTE_UNUSED)132051580eeSchristos _bfd_ptr_bfd_null_error (bfd *ignore ATTRIBUTE_UNUSED)
133a1ba9ba4Schristos {
134a1ba9ba4Schristos   bfd_set_error (bfd_error_invalid_operation);
135a1ba9ba4Schristos   return NULL;
136a1ba9ba4Schristos }
137a1ba9ba4Schristos 
138a1ba9ba4Schristos int
_bfd_int_bfd_0(bfd * ignore ATTRIBUTE_UNUSED)139051580eeSchristos _bfd_int_bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
140a1ba9ba4Schristos {
141a1ba9ba4Schristos   return 0;
142a1ba9ba4Schristos }
143a1ba9ba4Schristos 
144a1ba9ba4Schristos unsigned int
_bfd_uint_bfd_0(bfd * ignore ATTRIBUTE_UNUSED)145051580eeSchristos _bfd_uint_bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
146a1ba9ba4Schristos {
147a1ba9ba4Schristos    return 0;
148a1ba9ba4Schristos }
149a1ba9ba4Schristos 
150a1ba9ba4Schristos long
_bfd_long_bfd_0(bfd * ignore ATTRIBUTE_UNUSED)151051580eeSchristos _bfd_long_bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
152a1ba9ba4Schristos {
153a1ba9ba4Schristos   return 0;
154a1ba9ba4Schristos }
155a1ba9ba4Schristos 
156a1ba9ba4Schristos /* A routine which is used in target vectors for unsupported
157a1ba9ba4Schristos    operations which return -1 on error.  */
158a1ba9ba4Schristos 
159a1ba9ba4Schristos long
_bfd_long_bfd_n1_error(bfd * ignore_abfd ATTRIBUTE_UNUSED)160051580eeSchristos _bfd_long_bfd_n1_error (bfd *ignore_abfd ATTRIBUTE_UNUSED)
161a1ba9ba4Schristos {
162a1ba9ba4Schristos   bfd_set_error (bfd_error_invalid_operation);
163a1ba9ba4Schristos   return -1;
164a1ba9ba4Schristos }
165a1ba9ba4Schristos 
166a1ba9ba4Schristos void
_bfd_void_bfd(bfd * ignore ATTRIBUTE_UNUSED)167051580eeSchristos _bfd_void_bfd (bfd *ignore ATTRIBUTE_UNUSED)
168051580eeSchristos {
169051580eeSchristos }
170051580eeSchristos 
171051580eeSchristos void
_bfd_void_bfd_link(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info ATTRIBUTE_UNUSED)172051580eeSchristos _bfd_void_bfd_link (bfd *abfd ATTRIBUTE_UNUSED,
173051580eeSchristos 		    struct bfd_link_info *info ATTRIBUTE_UNUSED)
174051580eeSchristos {
175051580eeSchristos }
176051580eeSchristos 
177051580eeSchristos void
_bfd_void_bfd_asection(bfd * abfd ATTRIBUTE_UNUSED,asection * sec ATTRIBUTE_UNUSED)178051580eeSchristos _bfd_void_bfd_asection (bfd *abfd ATTRIBUTE_UNUSED,
179051580eeSchristos 			asection *sec ATTRIBUTE_UNUSED)
180a1ba9ba4Schristos {
181a1ba9ba4Schristos }
182a1ba9ba4Schristos 
183a1ba9ba4Schristos long
_bfd_norelocs_get_reloc_upper_bound(bfd * abfd ATTRIBUTE_UNUSED,asection * sec ATTRIBUTE_UNUSED)184a1ba9ba4Schristos _bfd_norelocs_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
185a1ba9ba4Schristos 				     asection *sec ATTRIBUTE_UNUSED)
186a1ba9ba4Schristos {
187a1ba9ba4Schristos   return sizeof (arelent *);
188a1ba9ba4Schristos }
189a1ba9ba4Schristos 
190a1ba9ba4Schristos long
_bfd_norelocs_canonicalize_reloc(bfd * abfd ATTRIBUTE_UNUSED,asection * sec ATTRIBUTE_UNUSED,arelent ** relptr,asymbol ** symbols ATTRIBUTE_UNUSED)191a1ba9ba4Schristos _bfd_norelocs_canonicalize_reloc (bfd *abfd ATTRIBUTE_UNUSED,
192a1ba9ba4Schristos 				  asection *sec ATTRIBUTE_UNUSED,
193a1ba9ba4Schristos 				  arelent **relptr,
194a1ba9ba4Schristos 				  asymbol **symbols ATTRIBUTE_UNUSED)
195a1ba9ba4Schristos {
196a1ba9ba4Schristos   *relptr = NULL;
197a1ba9ba4Schristos   return 0;
198a1ba9ba4Schristos }
199a1ba9ba4Schristos 
200051580eeSchristos void
_bfd_norelocs_set_reloc(bfd * abfd ATTRIBUTE_UNUSED,asection * sec ATTRIBUTE_UNUSED,arelent ** relptr ATTRIBUTE_UNUSED,unsigned int count ATTRIBUTE_UNUSED)201051580eeSchristos _bfd_norelocs_set_reloc (bfd *abfd ATTRIBUTE_UNUSED,
202051580eeSchristos 			 asection *sec ATTRIBUTE_UNUSED,
203051580eeSchristos 			 arelent **relptr ATTRIBUTE_UNUSED,
204051580eeSchristos 			 unsigned int count ATTRIBUTE_UNUSED)
205051580eeSchristos {
206051580eeSchristos   /* Do nothing.  */
207051580eeSchristos }
208051580eeSchristos 
209a1ba9ba4Schristos bfd_boolean
_bfd_nocore_core_file_matches_executable_p(bfd * ignore_core_bfd ATTRIBUTE_UNUSED,bfd * ignore_exec_bfd ATTRIBUTE_UNUSED)210a1ba9ba4Schristos _bfd_nocore_core_file_matches_executable_p
211a1ba9ba4Schristos   (bfd *ignore_core_bfd ATTRIBUTE_UNUSED,
212a1ba9ba4Schristos    bfd *ignore_exec_bfd ATTRIBUTE_UNUSED)
213a1ba9ba4Schristos {
214a1ba9ba4Schristos   bfd_set_error (bfd_error_invalid_operation);
215a1ba9ba4Schristos   return FALSE;
216a1ba9ba4Schristos }
217a1ba9ba4Schristos 
218a1ba9ba4Schristos /* Routine to handle core_file_failing_command entry point for targets
219a1ba9ba4Schristos    without core file support.  */
220a1ba9ba4Schristos 
221a1ba9ba4Schristos char *
_bfd_nocore_core_file_failing_command(bfd * ignore_abfd ATTRIBUTE_UNUSED)222a1ba9ba4Schristos _bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED)
223a1ba9ba4Schristos {
224a1ba9ba4Schristos   bfd_set_error (bfd_error_invalid_operation);
225a1ba9ba4Schristos   return NULL;
226a1ba9ba4Schristos }
227a1ba9ba4Schristos 
228a1ba9ba4Schristos /* Routine to handle core_file_failing_signal entry point for targets
229a1ba9ba4Schristos    without core file support.  */
230a1ba9ba4Schristos 
231a1ba9ba4Schristos int
_bfd_nocore_core_file_failing_signal(bfd * ignore_abfd ATTRIBUTE_UNUSED)232a1ba9ba4Schristos _bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED)
233a1ba9ba4Schristos {
234a1ba9ba4Schristos   bfd_set_error (bfd_error_invalid_operation);
235a1ba9ba4Schristos   return 0;
236a1ba9ba4Schristos }
237a1ba9ba4Schristos 
238a1ba9ba4Schristos /* Routine to handle the core_file_pid entry point for targets without
239a1ba9ba4Schristos    core file support.  */
240a1ba9ba4Schristos 
241a1ba9ba4Schristos int
_bfd_nocore_core_file_pid(bfd * ignore_abfd ATTRIBUTE_UNUSED)242a1ba9ba4Schristos _bfd_nocore_core_file_pid (bfd *ignore_abfd ATTRIBUTE_UNUSED)
243a1ba9ba4Schristos {
244a1ba9ba4Schristos   bfd_set_error (bfd_error_invalid_operation);
245a1ba9ba4Schristos   return 0;
246a1ba9ba4Schristos }
247a1ba9ba4Schristos 
248*184b2d41Schristos bfd_cleanup
_bfd_dummy_target(bfd * ignore_abfd ATTRIBUTE_UNUSED)249a1ba9ba4Schristos _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
250a1ba9ba4Schristos {
251a1ba9ba4Schristos   bfd_set_error (bfd_error_wrong_format);
252a1ba9ba4Schristos   return 0;
253a1ba9ba4Schristos }
254a1ba9ba4Schristos 
255a1ba9ba4Schristos /* Allocate memory using malloc.  */
256a1ba9ba4Schristos 
257051580eeSchristos #ifndef SSIZE_MAX
258051580eeSchristos #define SSIZE_MAX ((size_t) -1 >> 1)
259051580eeSchristos #endif
260051580eeSchristos 
261a1ba9ba4Schristos void *
bfd_malloc(bfd_size_type size)262a1ba9ba4Schristos bfd_malloc (bfd_size_type size)
263a1ba9ba4Schristos {
264a1ba9ba4Schristos   void *ptr;
265a1ba9ba4Schristos   size_t sz = (size_t) size;
266a1ba9ba4Schristos 
267a1ba9ba4Schristos   if (size != sz
268a1ba9ba4Schristos       /* This is to pacify memory checkers like valgrind.  */
269051580eeSchristos       || sz > SSIZE_MAX)
270a1ba9ba4Schristos     {
271a1ba9ba4Schristos       bfd_set_error (bfd_error_no_memory);
272a1ba9ba4Schristos       return NULL;
273a1ba9ba4Schristos     }
274a1ba9ba4Schristos 
275a1ba9ba4Schristos   ptr = malloc (sz);
276a1ba9ba4Schristos   if (ptr == NULL && sz != 0)
277a1ba9ba4Schristos     bfd_set_error (bfd_error_no_memory);
278a1ba9ba4Schristos 
279a1ba9ba4Schristos   return ptr;
280a1ba9ba4Schristos }
281a1ba9ba4Schristos 
282a1ba9ba4Schristos void *
bfd_realloc(void * ptr,bfd_size_type size)283a1ba9ba4Schristos bfd_realloc (void *ptr, bfd_size_type size)
284a1ba9ba4Schristos {
285a1ba9ba4Schristos   void *ret;
286a1ba9ba4Schristos   size_t sz = (size_t) size;
287a1ba9ba4Schristos 
288a1ba9ba4Schristos   if (ptr == NULL)
289a1ba9ba4Schristos     return bfd_malloc (size);
290a1ba9ba4Schristos 
291a1ba9ba4Schristos   if (size != sz
292a1ba9ba4Schristos       /* This is to pacify memory checkers like valgrind.  */
293051580eeSchristos       || sz > SSIZE_MAX)
294a1ba9ba4Schristos     {
295a1ba9ba4Schristos       bfd_set_error (bfd_error_no_memory);
296a1ba9ba4Schristos       return NULL;
297a1ba9ba4Schristos     }
298a1ba9ba4Schristos 
299a1ba9ba4Schristos   ret = realloc (ptr, sz);
300a1ba9ba4Schristos 
301a1ba9ba4Schristos   if (ret == NULL && sz != 0)
302a1ba9ba4Schristos     bfd_set_error (bfd_error_no_memory);
303a1ba9ba4Schristos 
304a1ba9ba4Schristos   return ret;
305a1ba9ba4Schristos }
306a1ba9ba4Schristos 
307a1ba9ba4Schristos /* Reallocate memory using realloc.
308a1ba9ba4Schristos    If this fails the pointer is freed before returning.  */
309a1ba9ba4Schristos 
310a1ba9ba4Schristos void *
bfd_realloc_or_free(void * ptr,bfd_size_type size)311a1ba9ba4Schristos bfd_realloc_or_free (void *ptr, bfd_size_type size)
312a1ba9ba4Schristos {
313a1ba9ba4Schristos   void *ret = bfd_realloc (ptr, size);
314a1ba9ba4Schristos 
315*184b2d41Schristos   if (ret == NULL)
316a1ba9ba4Schristos     free (ptr);
317a1ba9ba4Schristos 
318a1ba9ba4Schristos   return ret;
319a1ba9ba4Schristos }
320a1ba9ba4Schristos 
321a1ba9ba4Schristos /* Allocate memory using malloc and clear it.  */
322a1ba9ba4Schristos 
323a1ba9ba4Schristos void *
bfd_zmalloc(bfd_size_type size)324a1ba9ba4Schristos bfd_zmalloc (bfd_size_type size)
325a1ba9ba4Schristos {
326a1ba9ba4Schristos   void *ptr = bfd_malloc (size);
327a1ba9ba4Schristos 
328a1ba9ba4Schristos   if (ptr != NULL && size > 0)
329a1ba9ba4Schristos     memset (ptr, 0, (size_t) size);
330a1ba9ba4Schristos 
331a1ba9ba4Schristos   return ptr;
332a1ba9ba4Schristos }
333a1ba9ba4Schristos 
334a1ba9ba4Schristos /*
335a1ba9ba4Schristos INTERNAL_FUNCTION
336a1ba9ba4Schristos 	bfd_write_bigendian_4byte_int
337a1ba9ba4Schristos 
338a1ba9ba4Schristos SYNOPSIS
339a1ba9ba4Schristos 	bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
340a1ba9ba4Schristos 
341a1ba9ba4Schristos DESCRIPTION
342a1ba9ba4Schristos 	Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
343a1ba9ba4Schristos 	endian order regardless of what else is going on.  This is useful in
344a1ba9ba4Schristos 	archives.
345a1ba9ba4Schristos 
346a1ba9ba4Schristos */
347a1ba9ba4Schristos bfd_boolean
bfd_write_bigendian_4byte_int(bfd * abfd,unsigned int i)348a1ba9ba4Schristos bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i)
349a1ba9ba4Schristos {
350a1ba9ba4Schristos   bfd_byte buffer[4];
351a1ba9ba4Schristos   bfd_putb32 ((bfd_vma) i, buffer);
352a1ba9ba4Schristos   return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4;
353a1ba9ba4Schristos }
354a1ba9ba4Schristos 
355a1ba9ba4Schristos 
356a1ba9ba4Schristos /** The do-it-yourself (byte) sex-change kit */
357a1ba9ba4Schristos 
358a1ba9ba4Schristos /* The middle letter e.g. get<b>short indicates Big or Little endian
359a1ba9ba4Schristos    target machine.  It doesn't matter what the byte order of the host
360a1ba9ba4Schristos    machine is; these routines work for either.  */
361a1ba9ba4Schristos 
362a1ba9ba4Schristos /* FIXME: Should these take a count argument?
363a1ba9ba4Schristos    Answer (gnu@cygnus.com):  No, but perhaps they should be inline
364a1ba9ba4Schristos 			     functions in swap.h #ifdef __GNUC__.
365a1ba9ba4Schristos 			     Gprof them later and find out.  */
366a1ba9ba4Schristos 
367a1ba9ba4Schristos /*
368a1ba9ba4Schristos FUNCTION
369a1ba9ba4Schristos 	bfd_put_size
370a1ba9ba4Schristos FUNCTION
371a1ba9ba4Schristos 	bfd_get_size
372a1ba9ba4Schristos 
373a1ba9ba4Schristos DESCRIPTION
374a1ba9ba4Schristos 	These macros as used for reading and writing raw data in
375a1ba9ba4Schristos 	sections; each access (except for bytes) is vectored through
376a1ba9ba4Schristos 	the target format of the BFD and mangled accordingly. The
377a1ba9ba4Schristos 	mangling performs any necessary endian translations and
378a1ba9ba4Schristos 	removes alignment restrictions.  Note that types accepted and
379a1ba9ba4Schristos 	returned by these macros are identical so they can be swapped
380a1ba9ba4Schristos 	around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
381a1ba9ba4Schristos 	to either <<bfd_get_32>> or <<bfd_get_64>>.
382a1ba9ba4Schristos 
383a1ba9ba4Schristos 	In the put routines, @var{val} must be a <<bfd_vma>>.  If we are on a
384a1ba9ba4Schristos 	system without prototypes, the caller is responsible for making
385a1ba9ba4Schristos 	sure that is true, with a cast if necessary.  We don't cast
386a1ba9ba4Schristos 	them in the macro definitions because that would prevent <<lint>>
387a1ba9ba4Schristos 	or <<gcc -Wall>> from detecting sins such as passing a pointer.
388a1ba9ba4Schristos 	To detect calling these with less than a <<bfd_vma>>, use
389a1ba9ba4Schristos 	<<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
390a1ba9ba4Schristos 
391a1ba9ba4Schristos .
392a1ba9ba4Schristos .{* Byte swapping macros for user section data.  *}
393a1ba9ba4Schristos .
394a1ba9ba4Schristos .#define bfd_put_8(abfd, val, ptr) \
395a1ba9ba4Schristos .  ((void) (*((unsigned char *) (ptr)) = (val) & 0xff))
396a1ba9ba4Schristos .#define bfd_put_signed_8 \
397a1ba9ba4Schristos .  bfd_put_8
398a1ba9ba4Schristos .#define bfd_get_8(abfd, ptr) \
399*184b2d41Schristos .  ((bfd_vma) *(const unsigned char *) (ptr) & 0xff)
400a1ba9ba4Schristos .#define bfd_get_signed_8(abfd, ptr) \
401*184b2d41Schristos .  ((((bfd_signed_vma) *(const unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
402a1ba9ba4Schristos .
403a1ba9ba4Schristos .#define bfd_put_16(abfd, val, ptr) \
404a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_putx16, ((val),(ptr)))
405a1ba9ba4Schristos .#define bfd_put_signed_16 \
406a1ba9ba4Schristos .  bfd_put_16
407a1ba9ba4Schristos .#define bfd_get_16(abfd, ptr) \
408a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_getx16, (ptr))
409a1ba9ba4Schristos .#define bfd_get_signed_16(abfd, ptr) \
410a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
411a1ba9ba4Schristos .
412051580eeSchristos .#define bfd_put_24(abfd, val, ptr) \
413051580eeSchristos .  do					\
414051580eeSchristos .    if (bfd_big_endian (abfd))		\
415051580eeSchristos .      bfd_putb24 ((val), (ptr));	\
416051580eeSchristos .    else				\
417051580eeSchristos .      bfd_putl24 ((val), (ptr));	\
418051580eeSchristos .  while (0)
419051580eeSchristos .
420051580eeSchristos .bfd_vma bfd_getb24 (const void *p);
421051580eeSchristos .bfd_vma bfd_getl24 (const void *p);
422051580eeSchristos .
423051580eeSchristos .#define bfd_get_24(abfd, ptr) \
424051580eeSchristos .  (bfd_big_endian (abfd) ? bfd_getb24 (ptr) : bfd_getl24 (ptr))
425051580eeSchristos .
426a1ba9ba4Schristos .#define bfd_put_32(abfd, val, ptr) \
427a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_putx32, ((val),(ptr)))
428a1ba9ba4Schristos .#define bfd_put_signed_32 \
429a1ba9ba4Schristos .  bfd_put_32
430a1ba9ba4Schristos .#define bfd_get_32(abfd, ptr) \
431a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_getx32, (ptr))
432a1ba9ba4Schristos .#define bfd_get_signed_32(abfd, ptr) \
433a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_getx_signed_32, (ptr))
434a1ba9ba4Schristos .
435a1ba9ba4Schristos .#define bfd_put_64(abfd, val, ptr) \
436a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_putx64, ((val), (ptr)))
437a1ba9ba4Schristos .#define bfd_put_signed_64 \
438a1ba9ba4Schristos .  bfd_put_64
439a1ba9ba4Schristos .#define bfd_get_64(abfd, ptr) \
440a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_getx64, (ptr))
441a1ba9ba4Schristos .#define bfd_get_signed_64(abfd, ptr) \
442a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_getx_signed_64, (ptr))
443a1ba9ba4Schristos .
444a1ba9ba4Schristos .#define bfd_get(bits, abfd, ptr)			\
445*184b2d41Schristos .  ((bits) == 8 ? bfd_get_8 (abfd, ptr)			\
446a1ba9ba4Schristos .   : (bits) == 16 ? bfd_get_16 (abfd, ptr)		\
447a1ba9ba4Schristos .   : (bits) == 32 ? bfd_get_32 (abfd, ptr)		\
448a1ba9ba4Schristos .   : (bits) == 64 ? bfd_get_64 (abfd, ptr)		\
449a1ba9ba4Schristos .   : (abort (), (bfd_vma) - 1))
450a1ba9ba4Schristos .
451a1ba9ba4Schristos .#define bfd_put(bits, abfd, val, ptr)			\
452a1ba9ba4Schristos .  ((bits) == 8 ? bfd_put_8  (abfd, val, ptr)		\
453a1ba9ba4Schristos .   : (bits) == 16 ? bfd_put_16 (abfd, val, ptr)	\
454a1ba9ba4Schristos .   : (bits) == 32 ? bfd_put_32 (abfd, val, ptr)	\
455a1ba9ba4Schristos .   : (bits) == 64 ? bfd_put_64 (abfd, val, ptr)	\
456a1ba9ba4Schristos .   : (abort (), (void) 0))
457a1ba9ba4Schristos .
458a1ba9ba4Schristos */
459a1ba9ba4Schristos 
460a1ba9ba4Schristos /*
461a1ba9ba4Schristos FUNCTION
462a1ba9ba4Schristos 	bfd_h_put_size
463a1ba9ba4Schristos 	bfd_h_get_size
464a1ba9ba4Schristos 
465a1ba9ba4Schristos DESCRIPTION
466a1ba9ba4Schristos 	These macros have the same function as their <<bfd_get_x>>
467a1ba9ba4Schristos 	brethren, except that they are used for removing information
468a1ba9ba4Schristos 	for the header records of object files. Believe it or not,
469a1ba9ba4Schristos 	some object files keep their header records in big endian
470a1ba9ba4Schristos 	order and their data in little endian order.
471a1ba9ba4Schristos .
472a1ba9ba4Schristos .{* Byte swapping macros for file header data.  *}
473a1ba9ba4Schristos .
474a1ba9ba4Schristos .#define bfd_h_put_8(abfd, val, ptr) \
475a1ba9ba4Schristos .  bfd_put_8 (abfd, val, ptr)
476a1ba9ba4Schristos .#define bfd_h_put_signed_8(abfd, val, ptr) \
477a1ba9ba4Schristos .  bfd_put_8 (abfd, val, ptr)
478a1ba9ba4Schristos .#define bfd_h_get_8(abfd, ptr) \
479a1ba9ba4Schristos .  bfd_get_8 (abfd, ptr)
480a1ba9ba4Schristos .#define bfd_h_get_signed_8(abfd, ptr) \
481a1ba9ba4Schristos .  bfd_get_signed_8 (abfd, ptr)
482a1ba9ba4Schristos .
483a1ba9ba4Schristos .#define bfd_h_put_16(abfd, val, ptr) \
484a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
485a1ba9ba4Schristos .#define bfd_h_put_signed_16 \
486a1ba9ba4Schristos .  bfd_h_put_16
487a1ba9ba4Schristos .#define bfd_h_get_16(abfd, ptr) \
488a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_h_getx16, (ptr))
489a1ba9ba4Schristos .#define bfd_h_get_signed_16(abfd, ptr) \
490a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
491a1ba9ba4Schristos .
492a1ba9ba4Schristos .#define bfd_h_put_32(abfd, val, ptr) \
493a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
494a1ba9ba4Schristos .#define bfd_h_put_signed_32 \
495a1ba9ba4Schristos .  bfd_h_put_32
496a1ba9ba4Schristos .#define bfd_h_get_32(abfd, ptr) \
497a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_h_getx32, (ptr))
498a1ba9ba4Schristos .#define bfd_h_get_signed_32(abfd, ptr) \
499a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
500a1ba9ba4Schristos .
501a1ba9ba4Schristos .#define bfd_h_put_64(abfd, val, ptr) \
502a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
503a1ba9ba4Schristos .#define bfd_h_put_signed_64 \
504a1ba9ba4Schristos .  bfd_h_put_64
505a1ba9ba4Schristos .#define bfd_h_get_64(abfd, ptr) \
506a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_h_getx64, (ptr))
507a1ba9ba4Schristos .#define bfd_h_get_signed_64(abfd, ptr) \
508a1ba9ba4Schristos .  BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
509a1ba9ba4Schristos .
510a1ba9ba4Schristos .{* Aliases for the above, which should eventually go away.  *}
511a1ba9ba4Schristos .
512a1ba9ba4Schristos .#define H_PUT_64  bfd_h_put_64
513a1ba9ba4Schristos .#define H_PUT_32  bfd_h_put_32
514a1ba9ba4Schristos .#define H_PUT_16  bfd_h_put_16
515a1ba9ba4Schristos .#define H_PUT_8   bfd_h_put_8
516a1ba9ba4Schristos .#define H_PUT_S64 bfd_h_put_signed_64
517a1ba9ba4Schristos .#define H_PUT_S32 bfd_h_put_signed_32
518a1ba9ba4Schristos .#define H_PUT_S16 bfd_h_put_signed_16
519a1ba9ba4Schristos .#define H_PUT_S8  bfd_h_put_signed_8
520a1ba9ba4Schristos .#define H_GET_64  bfd_h_get_64
521a1ba9ba4Schristos .#define H_GET_32  bfd_h_get_32
522a1ba9ba4Schristos .#define H_GET_16  bfd_h_get_16
523a1ba9ba4Schristos .#define H_GET_8   bfd_h_get_8
524a1ba9ba4Schristos .#define H_GET_S64 bfd_h_get_signed_64
525a1ba9ba4Schristos .#define H_GET_S32 bfd_h_get_signed_32
526a1ba9ba4Schristos .#define H_GET_S16 bfd_h_get_signed_16
527a1ba9ba4Schristos .#define H_GET_S8  bfd_h_get_signed_8
528a1ba9ba4Schristos .
529a1ba9ba4Schristos .*/
530a1ba9ba4Schristos 
531a1ba9ba4Schristos /* Sign extension to bfd_signed_vma.  */
532a1ba9ba4Schristos #define COERCE16(x) (((bfd_vma) (x) ^ 0x8000) - 0x8000)
533a1ba9ba4Schristos #define COERCE32(x) (((bfd_vma) (x) ^ 0x80000000) - 0x80000000)
534a1ba9ba4Schristos #define COERCE64(x) \
535a1ba9ba4Schristos   (((bfd_uint64_t) (x) ^ ((bfd_uint64_t) 1 << 63)) - ((bfd_uint64_t) 1 << 63))
536a1ba9ba4Schristos 
537a1ba9ba4Schristos bfd_vma
bfd_getb16(const void * p)538a1ba9ba4Schristos bfd_getb16 (const void *p)
539a1ba9ba4Schristos {
540a1ba9ba4Schristos   const bfd_byte *addr = (const bfd_byte *) p;
541a1ba9ba4Schristos   return (addr[0] << 8) | addr[1];
542a1ba9ba4Schristos }
543a1ba9ba4Schristos 
544a1ba9ba4Schristos bfd_vma
bfd_getl16(const void * p)545a1ba9ba4Schristos bfd_getl16 (const void *p)
546a1ba9ba4Schristos {
547a1ba9ba4Schristos   const bfd_byte *addr = (const bfd_byte *) p;
548a1ba9ba4Schristos   return (addr[1] << 8) | addr[0];
549a1ba9ba4Schristos }
550a1ba9ba4Schristos 
551a1ba9ba4Schristos bfd_signed_vma
bfd_getb_signed_16(const void * p)552a1ba9ba4Schristos bfd_getb_signed_16 (const void *p)
553a1ba9ba4Schristos {
554a1ba9ba4Schristos   const bfd_byte *addr = (const bfd_byte *) p;
555a1ba9ba4Schristos   return COERCE16 ((addr[0] << 8) | addr[1]);
556a1ba9ba4Schristos }
557a1ba9ba4Schristos 
558a1ba9ba4Schristos bfd_signed_vma
bfd_getl_signed_16(const void * p)559a1ba9ba4Schristos bfd_getl_signed_16 (const void *p)
560a1ba9ba4Schristos {
561a1ba9ba4Schristos   const bfd_byte *addr = (const bfd_byte *) p;
562a1ba9ba4Schristos   return COERCE16 ((addr[1] << 8) | addr[0]);
563a1ba9ba4Schristos }
564a1ba9ba4Schristos 
565a1ba9ba4Schristos void
bfd_putb16(bfd_vma data,void * p)566a1ba9ba4Schristos bfd_putb16 (bfd_vma data, void *p)
567a1ba9ba4Schristos {
568a1ba9ba4Schristos   bfd_byte *addr = (bfd_byte *) p;
569a1ba9ba4Schristos   addr[0] = (data >> 8) & 0xff;
570a1ba9ba4Schristos   addr[1] = data & 0xff;
571a1ba9ba4Schristos }
572a1ba9ba4Schristos 
573a1ba9ba4Schristos void
bfd_putl16(bfd_vma data,void * p)574a1ba9ba4Schristos bfd_putl16 (bfd_vma data, void *p)
575a1ba9ba4Schristos {
576a1ba9ba4Schristos   bfd_byte *addr = (bfd_byte *) p;
577a1ba9ba4Schristos   addr[0] = data & 0xff;
578a1ba9ba4Schristos   addr[1] = (data >> 8) & 0xff;
579a1ba9ba4Schristos }
580a1ba9ba4Schristos 
581051580eeSchristos void
bfd_putb24(bfd_vma data,void * p)582051580eeSchristos bfd_putb24 (bfd_vma data, void *p)
583051580eeSchristos {
584051580eeSchristos   bfd_byte *addr = (bfd_byte *) p;
585051580eeSchristos   addr[0] = (data >> 16) & 0xff;
586051580eeSchristos   addr[1] = (data >> 8) & 0xff;
587051580eeSchristos   addr[2] = data & 0xff;
588051580eeSchristos }
589051580eeSchristos 
590051580eeSchristos void
bfd_putl24(bfd_vma data,void * p)591051580eeSchristos bfd_putl24 (bfd_vma data, void *p)
592051580eeSchristos {
593051580eeSchristos   bfd_byte *addr = (bfd_byte *) p;
594051580eeSchristos   addr[0] = data & 0xff;
595051580eeSchristos   addr[1] = (data >> 8) & 0xff;
596051580eeSchristos   addr[2] = (data >> 16) & 0xff;
597051580eeSchristos }
598051580eeSchristos 
599051580eeSchristos bfd_vma
bfd_getb24(const void * p)600051580eeSchristos bfd_getb24 (const void *p)
601051580eeSchristos {
602051580eeSchristos   const bfd_byte *addr = (const bfd_byte *) p;
603051580eeSchristos   unsigned long v;
604051580eeSchristos 
605051580eeSchristos   v =  (unsigned long) addr[0] << 16;
606051580eeSchristos   v |= (unsigned long) addr[1] << 8;
607051580eeSchristos   v |= (unsigned long) addr[2];
608051580eeSchristos   return v;
609051580eeSchristos }
610051580eeSchristos 
611051580eeSchristos bfd_vma
bfd_getl24(const void * p)612051580eeSchristos bfd_getl24 (const void *p)
613051580eeSchristos {
614051580eeSchristos   const bfd_byte *addr = (const bfd_byte *) p;
615051580eeSchristos   unsigned long v;
616051580eeSchristos 
617051580eeSchristos   v = (unsigned long) addr[0];
618051580eeSchristos   v |= (unsigned long) addr[1] << 8;
619051580eeSchristos   v |= (unsigned long) addr[2] << 16;
620051580eeSchristos   return v;
621051580eeSchristos }
622051580eeSchristos 
623a1ba9ba4Schristos bfd_vma
bfd_getb32(const void * p)624a1ba9ba4Schristos bfd_getb32 (const void *p)
625a1ba9ba4Schristos {
626a1ba9ba4Schristos   const bfd_byte *addr = (const bfd_byte *) p;
627a1ba9ba4Schristos   unsigned long v;
628a1ba9ba4Schristos 
629a1ba9ba4Schristos   v = (unsigned long) addr[0] << 24;
630a1ba9ba4Schristos   v |= (unsigned long) addr[1] << 16;
631a1ba9ba4Schristos   v |= (unsigned long) addr[2] << 8;
632a1ba9ba4Schristos   v |= (unsigned long) addr[3];
633a1ba9ba4Schristos   return v;
634a1ba9ba4Schristos }
635a1ba9ba4Schristos 
636a1ba9ba4Schristos bfd_vma
bfd_getl32(const void * p)637a1ba9ba4Schristos bfd_getl32 (const void *p)
638a1ba9ba4Schristos {
639a1ba9ba4Schristos   const bfd_byte *addr = (const bfd_byte *) p;
640a1ba9ba4Schristos   unsigned long v;
641a1ba9ba4Schristos 
642a1ba9ba4Schristos   v = (unsigned long) addr[0];
643a1ba9ba4Schristos   v |= (unsigned long) addr[1] << 8;
644a1ba9ba4Schristos   v |= (unsigned long) addr[2] << 16;
645a1ba9ba4Schristos   v |= (unsigned long) addr[3] << 24;
646a1ba9ba4Schristos   return v;
647a1ba9ba4Schristos }
648a1ba9ba4Schristos 
649a1ba9ba4Schristos bfd_signed_vma
bfd_getb_signed_32(const void * p)650a1ba9ba4Schristos bfd_getb_signed_32 (const void *p)
651a1ba9ba4Schristos {
652a1ba9ba4Schristos   const bfd_byte *addr = (const bfd_byte *) p;
653a1ba9ba4Schristos   unsigned long v;
654a1ba9ba4Schristos 
655a1ba9ba4Schristos   v = (unsigned long) addr[0] << 24;
656a1ba9ba4Schristos   v |= (unsigned long) addr[1] << 16;
657a1ba9ba4Schristos   v |= (unsigned long) addr[2] << 8;
658a1ba9ba4Schristos   v |= (unsigned long) addr[3];
659a1ba9ba4Schristos   return COERCE32 (v);
660a1ba9ba4Schristos }
661a1ba9ba4Schristos 
662a1ba9ba4Schristos bfd_signed_vma
bfd_getl_signed_32(const void * p)663a1ba9ba4Schristos bfd_getl_signed_32 (const void *p)
664a1ba9ba4Schristos {
665a1ba9ba4Schristos   const bfd_byte *addr = (const bfd_byte *) p;
666a1ba9ba4Schristos   unsigned long v;
667a1ba9ba4Schristos 
668a1ba9ba4Schristos   v = (unsigned long) addr[0];
669a1ba9ba4Schristos   v |= (unsigned long) addr[1] << 8;
670a1ba9ba4Schristos   v |= (unsigned long) addr[2] << 16;
671a1ba9ba4Schristos   v |= (unsigned long) addr[3] << 24;
672a1ba9ba4Schristos   return COERCE32 (v);
673a1ba9ba4Schristos }
674a1ba9ba4Schristos 
675a1ba9ba4Schristos bfd_uint64_t
bfd_getb64(const void * p ATTRIBUTE_UNUSED)676a1ba9ba4Schristos bfd_getb64 (const void *p ATTRIBUTE_UNUSED)
677a1ba9ba4Schristos {
678a1ba9ba4Schristos #ifdef BFD_HOST_64_BIT
679a1ba9ba4Schristos   const bfd_byte *addr = (const bfd_byte *) p;
680a1ba9ba4Schristos   bfd_uint64_t v;
681a1ba9ba4Schristos 
682a1ba9ba4Schristos   v  = addr[0]; v <<= 8;
683a1ba9ba4Schristos   v |= addr[1]; v <<= 8;
684a1ba9ba4Schristos   v |= addr[2]; v <<= 8;
685a1ba9ba4Schristos   v |= addr[3]; v <<= 8;
686a1ba9ba4Schristos   v |= addr[4]; v <<= 8;
687a1ba9ba4Schristos   v |= addr[5]; v <<= 8;
688a1ba9ba4Schristos   v |= addr[6]; v <<= 8;
689a1ba9ba4Schristos   v |= addr[7];
690a1ba9ba4Schristos 
691a1ba9ba4Schristos   return v;
692a1ba9ba4Schristos #else
693a1ba9ba4Schristos   BFD_FAIL();
694a1ba9ba4Schristos   return 0;
695a1ba9ba4Schristos #endif
696a1ba9ba4Schristos }
697a1ba9ba4Schristos 
698a1ba9ba4Schristos bfd_uint64_t
bfd_getl64(const void * p ATTRIBUTE_UNUSED)699a1ba9ba4Schristos bfd_getl64 (const void *p ATTRIBUTE_UNUSED)
700a1ba9ba4Schristos {
701a1ba9ba4Schristos #ifdef BFD_HOST_64_BIT
702a1ba9ba4Schristos   const bfd_byte *addr = (const bfd_byte *) p;
703a1ba9ba4Schristos   bfd_uint64_t v;
704a1ba9ba4Schristos 
705a1ba9ba4Schristos   v  = addr[7]; v <<= 8;
706a1ba9ba4Schristos   v |= addr[6]; v <<= 8;
707a1ba9ba4Schristos   v |= addr[5]; v <<= 8;
708a1ba9ba4Schristos   v |= addr[4]; v <<= 8;
709a1ba9ba4Schristos   v |= addr[3]; v <<= 8;
710a1ba9ba4Schristos   v |= addr[2]; v <<= 8;
711a1ba9ba4Schristos   v |= addr[1]; v <<= 8;
712a1ba9ba4Schristos   v |= addr[0];
713a1ba9ba4Schristos 
714a1ba9ba4Schristos   return v;
715a1ba9ba4Schristos #else
716a1ba9ba4Schristos   BFD_FAIL();
717a1ba9ba4Schristos   return 0;
718a1ba9ba4Schristos #endif
719a1ba9ba4Schristos 
720a1ba9ba4Schristos }
721a1ba9ba4Schristos 
722a1ba9ba4Schristos bfd_int64_t
bfd_getb_signed_64(const void * p ATTRIBUTE_UNUSED)723a1ba9ba4Schristos bfd_getb_signed_64 (const void *p ATTRIBUTE_UNUSED)
724a1ba9ba4Schristos {
725a1ba9ba4Schristos #ifdef BFD_HOST_64_BIT
726a1ba9ba4Schristos   const bfd_byte *addr = (const bfd_byte *) p;
727a1ba9ba4Schristos   bfd_uint64_t v;
728a1ba9ba4Schristos 
729a1ba9ba4Schristos   v  = addr[0]; v <<= 8;
730a1ba9ba4Schristos   v |= addr[1]; v <<= 8;
731a1ba9ba4Schristos   v |= addr[2]; v <<= 8;
732a1ba9ba4Schristos   v |= addr[3]; v <<= 8;
733a1ba9ba4Schristos   v |= addr[4]; v <<= 8;
734a1ba9ba4Schristos   v |= addr[5]; v <<= 8;
735a1ba9ba4Schristos   v |= addr[6]; v <<= 8;
736a1ba9ba4Schristos   v |= addr[7];
737a1ba9ba4Schristos 
738a1ba9ba4Schristos   return COERCE64 (v);
739a1ba9ba4Schristos #else
740a1ba9ba4Schristos   BFD_FAIL();
741a1ba9ba4Schristos   return 0;
742a1ba9ba4Schristos #endif
743a1ba9ba4Schristos }
744a1ba9ba4Schristos 
745a1ba9ba4Schristos bfd_int64_t
bfd_getl_signed_64(const void * p ATTRIBUTE_UNUSED)746a1ba9ba4Schristos bfd_getl_signed_64 (const void *p ATTRIBUTE_UNUSED)
747a1ba9ba4Schristos {
748a1ba9ba4Schristos #ifdef BFD_HOST_64_BIT
749a1ba9ba4Schristos   const bfd_byte *addr = (const bfd_byte *) p;
750a1ba9ba4Schristos   bfd_uint64_t v;
751a1ba9ba4Schristos 
752a1ba9ba4Schristos   v  = addr[7]; v <<= 8;
753a1ba9ba4Schristos   v |= addr[6]; v <<= 8;
754a1ba9ba4Schristos   v |= addr[5]; v <<= 8;
755a1ba9ba4Schristos   v |= addr[4]; v <<= 8;
756a1ba9ba4Schristos   v |= addr[3]; v <<= 8;
757a1ba9ba4Schristos   v |= addr[2]; v <<= 8;
758a1ba9ba4Schristos   v |= addr[1]; v <<= 8;
759a1ba9ba4Schristos   v |= addr[0];
760a1ba9ba4Schristos 
761a1ba9ba4Schristos   return COERCE64 (v);
762a1ba9ba4Schristos #else
763a1ba9ba4Schristos   BFD_FAIL();
764a1ba9ba4Schristos   return 0;
765a1ba9ba4Schristos #endif
766a1ba9ba4Schristos }
767a1ba9ba4Schristos 
768a1ba9ba4Schristos void
bfd_putb32(bfd_vma data,void * p)769a1ba9ba4Schristos bfd_putb32 (bfd_vma data, void *p)
770a1ba9ba4Schristos {
771a1ba9ba4Schristos   bfd_byte *addr = (bfd_byte *) p;
772a1ba9ba4Schristos   addr[0] = (data >> 24) & 0xff;
773a1ba9ba4Schristos   addr[1] = (data >> 16) & 0xff;
774a1ba9ba4Schristos   addr[2] = (data >>  8) & 0xff;
775a1ba9ba4Schristos   addr[3] = data & 0xff;
776a1ba9ba4Schristos }
777a1ba9ba4Schristos 
778a1ba9ba4Schristos void
bfd_putl32(bfd_vma data,void * p)779a1ba9ba4Schristos bfd_putl32 (bfd_vma data, void *p)
780a1ba9ba4Schristos {
781a1ba9ba4Schristos   bfd_byte *addr = (bfd_byte *) p;
782a1ba9ba4Schristos   addr[0] = data & 0xff;
783a1ba9ba4Schristos   addr[1] = (data >>  8) & 0xff;
784a1ba9ba4Schristos   addr[2] = (data >> 16) & 0xff;
785a1ba9ba4Schristos   addr[3] = (data >> 24) & 0xff;
786a1ba9ba4Schristos }
787a1ba9ba4Schristos 
788a1ba9ba4Schristos void
bfd_putb64(bfd_uint64_t data ATTRIBUTE_UNUSED,void * p ATTRIBUTE_UNUSED)789a1ba9ba4Schristos bfd_putb64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
790a1ba9ba4Schristos {
791a1ba9ba4Schristos #ifdef BFD_HOST_64_BIT
792a1ba9ba4Schristos   bfd_byte *addr = (bfd_byte *) p;
793a1ba9ba4Schristos   addr[0] = (data >> (7*8)) & 0xff;
794a1ba9ba4Schristos   addr[1] = (data >> (6*8)) & 0xff;
795a1ba9ba4Schristos   addr[2] = (data >> (5*8)) & 0xff;
796a1ba9ba4Schristos   addr[3] = (data >> (4*8)) & 0xff;
797a1ba9ba4Schristos   addr[4] = (data >> (3*8)) & 0xff;
798a1ba9ba4Schristos   addr[5] = (data >> (2*8)) & 0xff;
799a1ba9ba4Schristos   addr[6] = (data >> (1*8)) & 0xff;
800a1ba9ba4Schristos   addr[7] = (data >> (0*8)) & 0xff;
801a1ba9ba4Schristos #else
802a1ba9ba4Schristos   BFD_FAIL();
803a1ba9ba4Schristos #endif
804a1ba9ba4Schristos }
805a1ba9ba4Schristos 
806a1ba9ba4Schristos void
bfd_putl64(bfd_uint64_t data ATTRIBUTE_UNUSED,void * p ATTRIBUTE_UNUSED)807a1ba9ba4Schristos bfd_putl64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
808a1ba9ba4Schristos {
809a1ba9ba4Schristos #ifdef BFD_HOST_64_BIT
810a1ba9ba4Schristos   bfd_byte *addr = (bfd_byte *) p;
811a1ba9ba4Schristos   addr[7] = (data >> (7*8)) & 0xff;
812a1ba9ba4Schristos   addr[6] = (data >> (6*8)) & 0xff;
813a1ba9ba4Schristos   addr[5] = (data >> (5*8)) & 0xff;
814a1ba9ba4Schristos   addr[4] = (data >> (4*8)) & 0xff;
815a1ba9ba4Schristos   addr[3] = (data >> (3*8)) & 0xff;
816a1ba9ba4Schristos   addr[2] = (data >> (2*8)) & 0xff;
817a1ba9ba4Schristos   addr[1] = (data >> (1*8)) & 0xff;
818a1ba9ba4Schristos   addr[0] = (data >> (0*8)) & 0xff;
819a1ba9ba4Schristos #else
820a1ba9ba4Schristos   BFD_FAIL();
821a1ba9ba4Schristos #endif
822a1ba9ba4Schristos }
823a1ba9ba4Schristos 
824a1ba9ba4Schristos void
bfd_put_bits(bfd_uint64_t data,void * p,int bits,bfd_boolean big_p)825a1ba9ba4Schristos bfd_put_bits (bfd_uint64_t data, void *p, int bits, bfd_boolean big_p)
826a1ba9ba4Schristos {
827a1ba9ba4Schristos   bfd_byte *addr = (bfd_byte *) p;
828a1ba9ba4Schristos   int i;
829a1ba9ba4Schristos   int bytes;
830a1ba9ba4Schristos 
831a1ba9ba4Schristos   if (bits % 8 != 0)
832a1ba9ba4Schristos     abort ();
833a1ba9ba4Schristos 
834a1ba9ba4Schristos   bytes = bits / 8;
835a1ba9ba4Schristos   for (i = 0; i < bytes; i++)
836a1ba9ba4Schristos     {
837a1ba9ba4Schristos       int addr_index = big_p ? bytes - i - 1 : i;
838a1ba9ba4Schristos 
839a1ba9ba4Schristos       addr[addr_index] = data & 0xff;
840a1ba9ba4Schristos       data >>= 8;
841a1ba9ba4Schristos     }
842a1ba9ba4Schristos }
843a1ba9ba4Schristos 
844a1ba9ba4Schristos bfd_uint64_t
bfd_get_bits(const void * p,int bits,bfd_boolean big_p)845a1ba9ba4Schristos bfd_get_bits (const void *p, int bits, bfd_boolean big_p)
846a1ba9ba4Schristos {
847a1ba9ba4Schristos   const bfd_byte *addr = (const bfd_byte *) p;
848a1ba9ba4Schristos   bfd_uint64_t data;
849a1ba9ba4Schristos   int i;
850a1ba9ba4Schristos   int bytes;
851a1ba9ba4Schristos 
852a1ba9ba4Schristos   if (bits % 8 != 0)
853a1ba9ba4Schristos     abort ();
854a1ba9ba4Schristos 
855a1ba9ba4Schristos   data = 0;
856a1ba9ba4Schristos   bytes = bits / 8;
857a1ba9ba4Schristos   for (i = 0; i < bytes; i++)
858a1ba9ba4Schristos     {
859a1ba9ba4Schristos       int addr_index = big_p ? i : bytes - i - 1;
860a1ba9ba4Schristos 
861a1ba9ba4Schristos       data = (data << 8) | addr[addr_index];
862a1ba9ba4Schristos     }
863a1ba9ba4Schristos 
864a1ba9ba4Schristos   return data;
865a1ba9ba4Schristos }
866a1ba9ba4Schristos 
867a1ba9ba4Schristos /* Default implementation */
868a1ba9ba4Schristos 
869a1ba9ba4Schristos bfd_boolean
_bfd_generic_get_section_contents(bfd * abfd,sec_ptr section,void * location,file_ptr offset,bfd_size_type count)870a1ba9ba4Schristos _bfd_generic_get_section_contents (bfd *abfd,
871a1ba9ba4Schristos 				   sec_ptr section,
872a1ba9ba4Schristos 				   void *location,
873a1ba9ba4Schristos 				   file_ptr offset,
874a1ba9ba4Schristos 				   bfd_size_type count)
875a1ba9ba4Schristos {
876a1ba9ba4Schristos   bfd_size_type sz;
877a1ba9ba4Schristos   if (count == 0)
878a1ba9ba4Schristos     return TRUE;
879a1ba9ba4Schristos 
880a1ba9ba4Schristos   if (section->compress_status != COMPRESS_SECTION_NONE)
881a1ba9ba4Schristos     {
88215d8e94aSchristos       _bfd_error_handler
88315d8e94aSchristos 	/* xgettext:c-format */
884051580eeSchristos 	(_("%pB: unable to get decompressed section %pA"),
885a1ba9ba4Schristos 	 abfd, section);
886a1ba9ba4Schristos       bfd_set_error (bfd_error_invalid_operation);
887a1ba9ba4Schristos       return FALSE;
888a1ba9ba4Schristos     }
889a1ba9ba4Schristos 
890a1ba9ba4Schristos   /* We do allow reading of a section after bfd_final_link has
891a1ba9ba4Schristos      written the contents out to disk.  In that situation, rawsize is
892a1ba9ba4Schristos      just a stale version of size, so ignore it.  Otherwise we must be
893a1ba9ba4Schristos      reading an input section, where rawsize, if different to size,
894a1ba9ba4Schristos      is the on-disk size.  */
895a1ba9ba4Schristos   if (abfd->direction != write_direction && section->rawsize != 0)
896a1ba9ba4Schristos     sz = section->rawsize;
897a1ba9ba4Schristos   else
898a1ba9ba4Schristos     sz = section->size;
899a1ba9ba4Schristos   if (offset + count < count
900051580eeSchristos       || offset + count > sz
901051580eeSchristos       || (abfd->my_archive != NULL
902051580eeSchristos 	  && !bfd_is_thin_archive (abfd->my_archive)
903051580eeSchristos 	  && ((ufile_ptr) section->filepos + offset + count
904051580eeSchristos 	      > arelt_size (abfd))))
905a1ba9ba4Schristos     {
906a1ba9ba4Schristos       bfd_set_error (bfd_error_invalid_operation);
907a1ba9ba4Schristos       return FALSE;
908a1ba9ba4Schristos     }
909a1ba9ba4Schristos 
910a1ba9ba4Schristos   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
911a1ba9ba4Schristos       || bfd_bread (location, count, abfd) != count)
912a1ba9ba4Schristos     return FALSE;
913a1ba9ba4Schristos 
914a1ba9ba4Schristos   return TRUE;
915a1ba9ba4Schristos }
916a1ba9ba4Schristos 
917a1ba9ba4Schristos bfd_boolean
_bfd_generic_get_section_contents_in_window(bfd * abfd ATTRIBUTE_UNUSED,sec_ptr section ATTRIBUTE_UNUSED,bfd_window * w ATTRIBUTE_UNUSED,file_ptr offset ATTRIBUTE_UNUSED,bfd_size_type count ATTRIBUTE_UNUSED)918a1ba9ba4Schristos _bfd_generic_get_section_contents_in_window
919a1ba9ba4Schristos   (bfd *abfd ATTRIBUTE_UNUSED,
920a1ba9ba4Schristos    sec_ptr section ATTRIBUTE_UNUSED,
921a1ba9ba4Schristos    bfd_window *w ATTRIBUTE_UNUSED,
922a1ba9ba4Schristos    file_ptr offset ATTRIBUTE_UNUSED,
923a1ba9ba4Schristos    bfd_size_type count ATTRIBUTE_UNUSED)
924a1ba9ba4Schristos {
925a1ba9ba4Schristos #ifdef USE_MMAP
926a1ba9ba4Schristos   bfd_size_type sz;
927a1ba9ba4Schristos 
928a1ba9ba4Schristos   if (count == 0)
929a1ba9ba4Schristos     return TRUE;
930a1ba9ba4Schristos   if (abfd->xvec->_bfd_get_section_contents
931a1ba9ba4Schristos       != _bfd_generic_get_section_contents)
932a1ba9ba4Schristos     {
933a1ba9ba4Schristos       /* We don't know what changes the bfd's get_section_contents
934a1ba9ba4Schristos 	 method may have to make.  So punt trying to map the file
935a1ba9ba4Schristos 	 window, and let get_section_contents do its thing.  */
936a1ba9ba4Schristos       /* @@ FIXME : If the internal window has a refcount of 1 and was
937a1ba9ba4Schristos 	 allocated with malloc instead of mmap, just reuse it.  */
938a1ba9ba4Schristos       bfd_free_window (w);
939a1ba9ba4Schristos       w->i = bfd_zmalloc (sizeof (bfd_window_internal));
940a1ba9ba4Schristos       if (w->i == NULL)
941a1ba9ba4Schristos 	return FALSE;
942a1ba9ba4Schristos       w->i->data = bfd_malloc (count);
943a1ba9ba4Schristos       if (w->i->data == NULL)
944a1ba9ba4Schristos 	{
945a1ba9ba4Schristos 	  free (w->i);
946a1ba9ba4Schristos 	  w->i = NULL;
947a1ba9ba4Schristos 	  return FALSE;
948a1ba9ba4Schristos 	}
949a1ba9ba4Schristos       w->i->mapped = 0;
950a1ba9ba4Schristos       w->i->refcount = 1;
951a1ba9ba4Schristos       w->size = w->i->size = count;
952a1ba9ba4Schristos       w->data = w->i->data;
953a1ba9ba4Schristos       return bfd_get_section_contents (abfd, section, w->data, offset, count);
954a1ba9ba4Schristos     }
955a1ba9ba4Schristos   if (abfd->direction != write_direction && section->rawsize != 0)
956a1ba9ba4Schristos     sz = section->rawsize;
957a1ba9ba4Schristos   else
958a1ba9ba4Schristos     sz = section->size;
959051580eeSchristos   if (offset + count < count
960051580eeSchristos       || offset + count > sz
961051580eeSchristos       || (abfd->my_archive != NULL
962051580eeSchristos 	  && !bfd_is_thin_archive (abfd->my_archive)
963051580eeSchristos 	  && ((ufile_ptr) section->filepos + offset + count
964051580eeSchristos 	      > arelt_size (abfd)))
965a1ba9ba4Schristos       || ! bfd_get_file_window (abfd, section->filepos + offset, count, w,
966a1ba9ba4Schristos 				TRUE))
967a1ba9ba4Schristos     return FALSE;
968a1ba9ba4Schristos   return TRUE;
969a1ba9ba4Schristos #else
970a1ba9ba4Schristos   abort ();
971a1ba9ba4Schristos #endif
972a1ba9ba4Schristos }
973a1ba9ba4Schristos 
974a1ba9ba4Schristos /* This generic function can only be used in implementations where creating
975a1ba9ba4Schristos    NEW sections is disallowed.  It is useful in patching existing sections
976a1ba9ba4Schristos    in read-write files, though.  See other set_section_contents functions
977a1ba9ba4Schristos    to see why it doesn't work for new sections.  */
978a1ba9ba4Schristos bfd_boolean
_bfd_generic_set_section_contents(bfd * abfd,sec_ptr section,const void * location,file_ptr offset,bfd_size_type count)979a1ba9ba4Schristos _bfd_generic_set_section_contents (bfd *abfd,
980a1ba9ba4Schristos 				   sec_ptr section,
981a1ba9ba4Schristos 				   const void *location,
982a1ba9ba4Schristos 				   file_ptr offset,
983a1ba9ba4Schristos 				   bfd_size_type count)
984a1ba9ba4Schristos {
985a1ba9ba4Schristos   if (count == 0)
986a1ba9ba4Schristos     return TRUE;
987a1ba9ba4Schristos 
988a1ba9ba4Schristos   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
989a1ba9ba4Schristos       || bfd_bwrite (location, count, abfd) != count)
990a1ba9ba4Schristos     return FALSE;
991a1ba9ba4Schristos 
992a1ba9ba4Schristos   return TRUE;
993a1ba9ba4Schristos }
994a1ba9ba4Schristos 
995a1ba9ba4Schristos /*
996a1ba9ba4Schristos INTERNAL_FUNCTION
997a1ba9ba4Schristos 	bfd_log2
998a1ba9ba4Schristos 
999a1ba9ba4Schristos SYNOPSIS
1000a1ba9ba4Schristos 	unsigned int bfd_log2 (bfd_vma x);
1001a1ba9ba4Schristos 
1002a1ba9ba4Schristos DESCRIPTION
1003a1ba9ba4Schristos 	Return the log base 2 of the value supplied, rounded up.  E.g., an
1004a1ba9ba4Schristos 	@var{x} of 1025 returns 11.  A @var{x} of 0 returns 0.
1005a1ba9ba4Schristos */
1006a1ba9ba4Schristos 
1007a1ba9ba4Schristos unsigned int
bfd_log2(bfd_vma x)1008a1ba9ba4Schristos bfd_log2 (bfd_vma x)
1009a1ba9ba4Schristos {
1010a1ba9ba4Schristos   unsigned int result = 0;
1011a1ba9ba4Schristos 
1012a1ba9ba4Schristos   if (x <= 1)
1013a1ba9ba4Schristos     return result;
1014a1ba9ba4Schristos   --x;
1015a1ba9ba4Schristos   do
1016a1ba9ba4Schristos     ++result;
1017a1ba9ba4Schristos   while ((x >>= 1) != 0);
1018a1ba9ba4Schristos   return result;
1019a1ba9ba4Schristos }
1020a1ba9ba4Schristos 
1021a1ba9ba4Schristos bfd_boolean
bfd_generic_is_local_label_name(bfd * abfd,const char * name)1022a1ba9ba4Schristos bfd_generic_is_local_label_name (bfd *abfd, const char *name)
1023a1ba9ba4Schristos {
1024a1ba9ba4Schristos   char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
1025a1ba9ba4Schristos 
1026a1ba9ba4Schristos   return name[0] == locals_prefix;
1027a1ba9ba4Schristos }
1028a1ba9ba4Schristos 
1029a1ba9ba4Schristos /* Give a warning at runtime if someone compiles code which calls
1030a1ba9ba4Schristos    old routines.  */
1031a1ba9ba4Schristos 
1032a1ba9ba4Schristos void
_bfd_warn_deprecated(const char * what,const char * file,int line,const char * func)103315d8e94aSchristos _bfd_warn_deprecated (const char *what,
1034a1ba9ba4Schristos 		      const char *file,
1035a1ba9ba4Schristos 		      int line,
1036a1ba9ba4Schristos 		      const char *func)
1037a1ba9ba4Schristos {
1038a1ba9ba4Schristos   /* Poor man's tracking of functions we've already warned about.  */
1039a1ba9ba4Schristos   static size_t mask = 0;
1040a1ba9ba4Schristos 
1041a1ba9ba4Schristos   if (~(size_t) func & ~mask)
1042a1ba9ba4Schristos     {
1043a1ba9ba4Schristos       fflush (stdout);
1044a1ba9ba4Schristos       /* Note: separate sentences in order to allow
1045a1ba9ba4Schristos 	 for translation into other languages.  */
1046a1ba9ba4Schristos       if (func)
104715d8e94aSchristos 	/* xgettext:c-format */
1048a1ba9ba4Schristos 	fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"),
1049a1ba9ba4Schristos 		 what, file, line, func);
1050a1ba9ba4Schristos       else
1051a1ba9ba4Schristos 	fprintf (stderr, _("Deprecated %s called\n"), what);
1052a1ba9ba4Schristos       fflush (stderr);
1053a1ba9ba4Schristos       mask |= ~(size_t) func;
1054a1ba9ba4Schristos     }
1055a1ba9ba4Schristos }
1056a1ba9ba4Schristos 
1057a1ba9ba4Schristos /* Helper function for reading uleb128 encoded data.  */
1058a1ba9ba4Schristos 
1059a1ba9ba4Schristos bfd_vma
_bfd_read_unsigned_leb128(bfd * abfd ATTRIBUTE_UNUSED,bfd_byte * buf,unsigned int * bytes_read_ptr)106015d8e94aSchristos _bfd_read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1061a1ba9ba4Schristos 			   bfd_byte *buf,
1062a1ba9ba4Schristos 			   unsigned int *bytes_read_ptr)
1063a1ba9ba4Schristos {
1064a1ba9ba4Schristos   bfd_vma result;
1065a1ba9ba4Schristos   unsigned int num_read;
1066a1ba9ba4Schristos   unsigned int shift;
1067a1ba9ba4Schristos   unsigned char byte;
1068a1ba9ba4Schristos 
1069a1ba9ba4Schristos   result = 0;
1070a1ba9ba4Schristos   shift = 0;
1071a1ba9ba4Schristos   num_read = 0;
1072a1ba9ba4Schristos   do
1073a1ba9ba4Schristos     {
1074a1ba9ba4Schristos       byte = bfd_get_8 (abfd, buf);
1075a1ba9ba4Schristos       buf++;
1076a1ba9ba4Schristos       num_read++;
1077a1ba9ba4Schristos       result |= (((bfd_vma) byte & 0x7f) << shift);
1078a1ba9ba4Schristos       shift += 7;
1079a1ba9ba4Schristos     }
1080a1ba9ba4Schristos   while (byte & 0x80);
1081a1ba9ba4Schristos   *bytes_read_ptr = num_read;
1082a1ba9ba4Schristos   return result;
1083a1ba9ba4Schristos }
1084a1ba9ba4Schristos 
1085a1ba9ba4Schristos /* Read in a LEB128 encoded value from ABFD starting at DATA.
1086a1ba9ba4Schristos    If SIGN is true, return a signed LEB128 value.
1087a1ba9ba4Schristos    If LENGTH_RETURN is not NULL, return in it the number of bytes read.
1088a1ba9ba4Schristos    No bytes will be read at address END or beyond.  */
1089a1ba9ba4Schristos 
1090a1ba9ba4Schristos bfd_vma
_bfd_safe_read_leb128(bfd * abfd ATTRIBUTE_UNUSED,bfd_byte * data,unsigned int * length_return,bfd_boolean sign,const bfd_byte * const end)109115d8e94aSchristos _bfd_safe_read_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1092a1ba9ba4Schristos 		       bfd_byte *data,
1093a1ba9ba4Schristos 		       unsigned int *length_return,
1094a1ba9ba4Schristos 		       bfd_boolean sign,
1095a1ba9ba4Schristos 		       const bfd_byte * const end)
1096a1ba9ba4Schristos {
1097a1ba9ba4Schristos   bfd_vma result = 0;
1098a1ba9ba4Schristos   unsigned int num_read = 0;
1099a1ba9ba4Schristos   unsigned int shift = 0;
1100a1ba9ba4Schristos   unsigned char byte = 0;
1101a1ba9ba4Schristos 
1102a1ba9ba4Schristos   while (data < end)
1103a1ba9ba4Schristos     {
1104a1ba9ba4Schristos       byte = bfd_get_8 (abfd, data);
1105a1ba9ba4Schristos       data++;
1106a1ba9ba4Schristos       num_read++;
1107a1ba9ba4Schristos 
1108a1ba9ba4Schristos       result |= ((bfd_vma) (byte & 0x7f)) << shift;
1109a1ba9ba4Schristos 
1110a1ba9ba4Schristos       shift += 7;
1111a1ba9ba4Schristos       if ((byte & 0x80) == 0)
1112a1ba9ba4Schristos 	break;
1113a1ba9ba4Schristos     }
1114a1ba9ba4Schristos 
1115a1ba9ba4Schristos   if (length_return != NULL)
1116a1ba9ba4Schristos     *length_return = num_read;
1117a1ba9ba4Schristos 
1118a1ba9ba4Schristos   if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
1119b2396a7bSchristos     result |= -((bfd_vma) 1 << shift);
1120a1ba9ba4Schristos 
1121a1ba9ba4Schristos   return result;
1122a1ba9ba4Schristos }
1123a1ba9ba4Schristos 
1124a1ba9ba4Schristos /* Helper function for reading sleb128 encoded data.  */
1125a1ba9ba4Schristos 
1126a1ba9ba4Schristos bfd_signed_vma
_bfd_read_signed_leb128(bfd * abfd ATTRIBUTE_UNUSED,bfd_byte * buf,unsigned int * bytes_read_ptr)112715d8e94aSchristos _bfd_read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED,
1128a1ba9ba4Schristos 			 bfd_byte *buf,
1129a1ba9ba4Schristos 			 unsigned int *bytes_read_ptr)
1130a1ba9ba4Schristos {
1131a1ba9ba4Schristos   bfd_vma result;
1132a1ba9ba4Schristos   unsigned int shift;
1133a1ba9ba4Schristos   unsigned int num_read;
1134a1ba9ba4Schristos   unsigned char byte;
1135a1ba9ba4Schristos 
1136a1ba9ba4Schristos   result = 0;
1137a1ba9ba4Schristos   shift = 0;
1138a1ba9ba4Schristos   num_read = 0;
1139a1ba9ba4Schristos   do
1140a1ba9ba4Schristos     {
1141a1ba9ba4Schristos       byte = bfd_get_8 (abfd, buf);
1142a1ba9ba4Schristos       buf ++;
1143a1ba9ba4Schristos       num_read ++;
1144a1ba9ba4Schristos       result |= (((bfd_vma) byte & 0x7f) << shift);
1145a1ba9ba4Schristos       shift += 7;
1146a1ba9ba4Schristos     }
1147a1ba9ba4Schristos   while (byte & 0x80);
1148a1ba9ba4Schristos   if (shift < 8 * sizeof (result) && (byte & 0x40))
1149a1ba9ba4Schristos     result |= (((bfd_vma) -1) << shift);
1150a1ba9ba4Schristos   *bytes_read_ptr = num_read;
1151a1ba9ba4Schristos   return result;
1152a1ba9ba4Schristos }
1153a1ba9ba4Schristos 
1154*184b2d41Schristos /* Write VAL in uleb128 format to P.
1155*184b2d41Schristos    END indicates the last byte of allocated space for the uleb128 value to fit
1156*184b2d41Schristos    in.
1157*184b2d41Schristos    Return a pointer to the byte following the last byte that was written, or
1158*184b2d41Schristos    NULL if the uleb128 value does not fit in the allocated space between P and
1159*184b2d41Schristos    END.  */
1160*184b2d41Schristos bfd_byte *
_bfd_write_unsigned_leb128(bfd_byte * p,bfd_byte * end,bfd_vma val)1161*184b2d41Schristos _bfd_write_unsigned_leb128 (bfd_byte *p, bfd_byte *end, bfd_vma val)
1162*184b2d41Schristos {
1163*184b2d41Schristos   bfd_byte c;
1164*184b2d41Schristos   do
1165*184b2d41Schristos     {
1166*184b2d41Schristos       if (p > end)
1167*184b2d41Schristos 	return NULL;
1168*184b2d41Schristos       c = val & 0x7f;
1169*184b2d41Schristos       val >>= 7;
1170*184b2d41Schristos       if (val)
1171*184b2d41Schristos 	c |= 0x80;
1172*184b2d41Schristos       *(p++) = c;
1173*184b2d41Schristos     }
1174*184b2d41Schristos   while (val);
1175*184b2d41Schristos   return p;
1176*184b2d41Schristos }
1177*184b2d41Schristos 
1178a1ba9ba4Schristos bfd_boolean
_bfd_generic_init_private_section_data(bfd * ibfd ATTRIBUTE_UNUSED,asection * isec ATTRIBUTE_UNUSED,bfd * obfd ATTRIBUTE_UNUSED,asection * osec ATTRIBUTE_UNUSED,struct bfd_link_info * link_info ATTRIBUTE_UNUSED)1179a1ba9ba4Schristos _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
1180a1ba9ba4Schristos 					asection *isec ATTRIBUTE_UNUSED,
1181a1ba9ba4Schristos 					bfd *obfd ATTRIBUTE_UNUSED,
1182a1ba9ba4Schristos 					asection *osec ATTRIBUTE_UNUSED,
1183a1ba9ba4Schristos 					struct bfd_link_info *link_info ATTRIBUTE_UNUSED)
1184a1ba9ba4Schristos {
1185a1ba9ba4Schristos   return TRUE;
1186a1ba9ba4Schristos }
1187