| 1 | /* Copyright (C) 1990, 1991 Free Software Foundation, Inc. |
| 2 | |
| 3 | This file is part of BFD, the Binary File Diddler. |
| 4 | |
| 5 | BFD is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 1, or (at your option) |
| 8 | any later version. |
| 9 | |
| 10 | BFD is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with BFD; see the file COPYING. If not, write to |
| 17 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 18 | |
| 19 | /* $Id$ */ |
| 20 | |
| 21 | /*** libbfd.c -- random bfd support routines used internally only. */ |
| 22 | #include <sysdep.h> |
| 23 | #include "bfd.h" |
| 24 | #include "libbfd.h" |
| 25 | |
| 26 | |
| 27 | \f |
| 28 | /** Dummies for targets that don't want or need to implement |
| 29 | certain operations */ |
| 30 | |
| 31 | boolean |
| 32 | _bfd_dummy_new_section_hook (ignore, ignore_newsect) |
| 33 | bfd *ignore; |
| 34 | asection *ignore_newsect; |
| 35 | { |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | boolean |
| 40 | bfd_false (ignore) |
| 41 | bfd *ignore; |
| 42 | { |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | boolean |
| 47 | bfd_true (ignore) |
| 48 | bfd *ignore; |
| 49 | { |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | PTR |
| 54 | bfd_nullvoidptr(ignore) |
| 55 | bfd *ignore; |
| 56 | { |
| 57 | return (PTR)NULL; |
| 58 | } |
| 59 | |
| 60 | int |
| 61 | bfd_0(ignore) |
| 62 | bfd *ignore; |
| 63 | { |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | unsigned int |
| 68 | bfd_0u(ignore) |
| 69 | bfd *ignore; |
| 70 | { |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | bfd_void(ignore) |
| 76 | bfd *ignore; |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | boolean |
| 81 | _bfd_dummy_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd) |
| 82 | bfd *ignore_core_bfd; |
| 83 | bfd *ignore_exec_bfd; |
| 84 | { |
| 85 | bfd_error = invalid_operation; |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | /* of course you can't initialize a function to be the same as another, grr */ |
| 90 | |
| 91 | char * |
| 92 | _bfd_dummy_core_file_failing_command (ignore_abfd) |
| 93 | bfd *ignore_abfd; |
| 94 | { |
| 95 | return (char *)NULL; |
| 96 | } |
| 97 | |
| 98 | int |
| 99 | _bfd_dummy_core_file_failing_signal (ignore_abfd) |
| 100 | bfd *ignore_abfd; |
| 101 | { |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | bfd_target * |
| 106 | _bfd_dummy_target (ignore_abfd) |
| 107 | bfd *ignore_abfd; |
| 108 | { |
| 109 | return 0; |
| 110 | } |
| 111 | \f |
| 112 | /** zalloc -- allocate and clear storage */ |
| 113 | |
| 114 | |
| 115 | #ifndef zalloc |
| 116 | char * |
| 117 | zalloc (size) |
| 118 | bfd_size_type size; |
| 119 | { |
| 120 | char *ptr = (char *) malloc ((int)size); |
| 121 | |
| 122 | if ((ptr != NULL) && (size != 0)) |
| 123 | memset(ptr,0, size); |
| 124 | |
| 125 | return ptr; |
| 126 | } |
| 127 | #endif |
| 128 | \f |
| 129 | /* Some IO code */ |
| 130 | |
| 131 | |
| 132 | /* Note that archive entries don't have streams; they share their parent's. |
| 133 | This allows someone to play with the iostream behind bfd's back. |
| 134 | |
| 135 | Also, note that the origin pointer points to the beginning of a file's |
| 136 | contents (0 for non-archive elements). For archive entries this is the |
| 137 | first octet in the file, NOT the beginning of the archive header. */ |
| 138 | |
| 139 | static |
| 140 | int DEFUN(real_read,(where, a,b, file), |
| 141 | PTR where AND |
| 142 | int a AND |
| 143 | int b AND |
| 144 | FILE *file) |
| 145 | { |
| 146 | return fread(where, a,b,file); |
| 147 | } |
| 148 | bfd_size_type |
| 149 | DEFUN(bfd_read,(ptr, size, nitems, abfd), |
| 150 | PTR ptr AND |
| 151 | bfd_size_type size AND |
| 152 | bfd_size_type nitems AND |
| 153 | bfd *abfd) |
| 154 | { |
| 155 | return (bfd_size_type)real_read (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd)); |
| 156 | } |
| 157 | |
| 158 | bfd_size_type |
| 159 | DEFUN(bfd_write,(ptr, size, nitems, abfd), |
| 160 | PTR ptr AND |
| 161 | bfd_size_type size AND |
| 162 | bfd_size_type nitems AND |
| 163 | bfd *abfd) |
| 164 | { |
| 165 | return fwrite (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd)); |
| 166 | } |
| 167 | |
| 168 | int |
| 169 | DEFUN(bfd_seek,(abfd, position, direction), |
| 170 | bfd * CONST abfd AND |
| 171 | CONST file_ptr position AND |
| 172 | CONST int direction) |
| 173 | { |
| 174 | /* For the time being, a bfd may not seek to it's end. The |
| 175 | problem is that we don't easily have a way to recognize |
| 176 | the end of an element in an archive. */ |
| 177 | |
| 178 | BFD_ASSERT(direction == SEEK_SET |
| 179 | || direction == SEEK_CUR); |
| 180 | |
| 181 | if (direction == SEEK_SET && abfd->my_archive != NULL) |
| 182 | { |
| 183 | /* This is a set within an archive, so we need to |
| 184 | add the base of the object within the archive */ |
| 185 | return(fseek(bfd_cache_lookup(abfd), |
| 186 | position + abfd->origin, |
| 187 | direction)); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | return(fseek(bfd_cache_lookup(abfd), position, direction)); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | long |
| 196 | bfd_tell (abfd) |
| 197 | bfd *abfd; |
| 198 | { |
| 199 | file_ptr ptr; |
| 200 | |
| 201 | ptr = ftell (bfd_cache_lookup(abfd)); |
| 202 | |
| 203 | if (abfd->my_archive) |
| 204 | ptr -= abfd->origin; |
| 205 | return ptr; |
| 206 | } |
| 207 | \f |
| 208 | /** Make a string table */ |
| 209 | |
| 210 | /* Add string to table pointed to by table, at location starting with free_ptr. |
| 211 | resizes the table if necessary (if it's NULL, creates it, ignoring |
| 212 | table_length). Updates free_ptr, table, table_length */ |
| 213 | |
| 214 | boolean |
| 215 | bfd_add_to_string_table (table, new_string, table_length, free_ptr) |
| 216 | char **table, **free_ptr; |
| 217 | char *new_string; |
| 218 | unsigned int *table_length; |
| 219 | { |
| 220 | size_t string_length = strlen (new_string) + 1; /* include null here */ |
| 221 | char *base = *table; |
| 222 | size_t space_length = *table_length; |
| 223 | unsigned int offset = (base ? *free_ptr - base : 0); |
| 224 | |
| 225 | if (base == NULL) { |
| 226 | /* Avoid a useless regrow if we can (but of course we still |
| 227 | take it next time */ |
| 228 | space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ? |
| 229 | DEFAULT_STRING_SPACE_SIZE : string_length+1); |
| 230 | base = zalloc (space_length); |
| 231 | |
| 232 | if (base == NULL) { |
| 233 | bfd_error = no_memory; |
| 234 | return false; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if ((size_t)(offset + string_length) >= space_length) { |
| 239 | /* Make sure we will have enough space */ |
| 240 | while ((size_t)(offset + string_length) >= space_length) |
| 241 | space_length += space_length/2; /* grow by 50% */ |
| 242 | |
| 243 | base = (char *) realloc (base, space_length); |
| 244 | if (base == NULL) { |
| 245 | bfd_error = no_memory; |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | } |
| 250 | |
| 251 | memcpy (base + offset, new_string, string_length); |
| 252 | *table = base; |
| 253 | *table_length = space_length; |
| 254 | *free_ptr = base + offset + string_length; |
| 255 | |
| 256 | return true; |
| 257 | } |
| 258 | \f |
| 259 | /** The do-it-yourself (byte) sex-change kit */ |
| 260 | |
| 261 | /* The middle letter e.g. get<b>short indicates Big or Little endian |
| 262 | target machine. It doesn't matter what the byte order of the host |
| 263 | machine is; these routines work for either. */ |
| 264 | |
| 265 | /* FIXME: Should these take a count argument? |
| 266 | Answer (gnu@cygnus.com): No, but perhaps they should be inline |
| 267 | functions in swap.h #ifdef __GNUC__. |
| 268 | Gprof them later and find out. */ |
| 269 | |
| 270 | unsigned int |
| 271 | _do_getb16 (addr) |
| 272 | register bfd_byte *addr; |
| 273 | { |
| 274 | return (addr[0] << 8) | addr[1]; |
| 275 | } |
| 276 | |
| 277 | unsigned int |
| 278 | _do_getl16 (addr) |
| 279 | register bfd_byte *addr; |
| 280 | { |
| 281 | return (addr[1] << 8) | addr[0]; |
| 282 | } |
| 283 | |
| 284 | void |
| 285 | _do_putb16 (data, addr) |
| 286 | int data; /* Actually short, but ansi C sucks */ |
| 287 | register bfd_byte *addr; |
| 288 | { |
| 289 | addr[0] = (bfd_byte)(data >> 8); |
| 290 | addr[1] = (bfd_byte )data; |
| 291 | } |
| 292 | |
| 293 | void |
| 294 | _do_putl16 (data, addr) |
| 295 | int data; /* Actually short, but ansi C sucks */ |
| 296 | register bfd_byte *addr; |
| 297 | { |
| 298 | addr[0] = (bfd_byte )data; |
| 299 | addr[1] = (bfd_byte)(data >> 8); |
| 300 | } |
| 301 | |
| 302 | unsigned int |
| 303 | _do_getb32 (addr) |
| 304 | register bfd_byte *addr; |
| 305 | { |
| 306 | return ((((addr[0] << 8) | addr[1]) << 8) | addr[2]) << 8 | addr[3]; |
| 307 | } |
| 308 | |
| 309 | unsigned int |
| 310 | _do_getl32 (addr) |
| 311 | register bfd_byte *addr; |
| 312 | { |
| 313 | return ((((addr[3] << 8) | addr[2]) << 8) | addr[1]) << 8 | addr[0]; |
| 314 | } |
| 315 | |
| 316 | bfd_64_type |
| 317 | _do_getb64(addr) |
| 318 | register bfd_byte *addr; |
| 319 | { |
| 320 | bfd_64_type low, high; |
| 321 | #ifdef __GNUC__ |
| 322 | high= ((((((((addr[0]) << 8) | |
| 323 | addr[1]) << 8) | |
| 324 | addr[2]) << 8) | |
| 325 | addr[3]) ); |
| 326 | |
| 327 | low = ((((((((addr[4]) << 8) | |
| 328 | addr[5]) << 8) | |
| 329 | addr[6]) << 8) | |
| 330 | addr[7])); |
| 331 | |
| 332 | return high << 32 | low; |
| 333 | #else |
| 334 | BFD_FAIL(); |
| 335 | #endif |
| 336 | |
| 337 | } |
| 338 | |
| 339 | bfd_64_type |
| 340 | _do_getl64 (addr) |
| 341 | register bfd_byte *addr; |
| 342 | { |
| 343 | bfd_64_type low, high; |
| 344 | #ifdef __GNUC__ |
| 345 | high= (((((((addr[7] << 8) | |
| 346 | addr[6]) << 8) | |
| 347 | addr[5]) << 8) | |
| 348 | addr[4])); |
| 349 | |
| 350 | low = (((((((addr[3] << 8) | |
| 351 | addr[2]) << 8) | |
| 352 | addr[1]) << 8) | |
| 353 | addr[0]) ); |
| 354 | |
| 355 | return high << 32 | low; |
| 356 | #else |
| 357 | BFD_FAIL(); |
| 358 | #endif |
| 359 | } |
| 360 | |
| 361 | void |
| 362 | _do_putb32 (data, addr) |
| 363 | unsigned long data; |
| 364 | register bfd_byte *addr; |
| 365 | { |
| 366 | addr[0] = (bfd_byte)(data >> 24); |
| 367 | addr[1] = (bfd_byte)(data >> 16); |
| 368 | addr[2] = (bfd_byte)(data >> 8); |
| 369 | addr[3] = (bfd_byte)data; |
| 370 | } |
| 371 | |
| 372 | void |
| 373 | _do_putl32 (data, addr) |
| 374 | unsigned long data; |
| 375 | register bfd_byte *addr; |
| 376 | { |
| 377 | addr[0] = (bfd_byte)data; |
| 378 | addr[1] = (bfd_byte)(data >> 8); |
| 379 | addr[2] = (bfd_byte)(data >> 16); |
| 380 | addr[3] = (bfd_byte)(data >> 24); |
| 381 | } |
| 382 | void |
| 383 | _do_putb64 (data, addr) |
| 384 | bfd_64_type data; |
| 385 | register bfd_byte *addr; |
| 386 | { |
| 387 | #ifdef __GNUC__ |
| 388 | addr[0] = (bfd_byte)(data >> (7*8)); |
| 389 | addr[1] = (bfd_byte)(data >> (6*8)); |
| 390 | addr[2] = (bfd_byte)(data >> (5*8)); |
| 391 | addr[3] = (bfd_byte)(data >> (4*8)); |
| 392 | addr[4] = (bfd_byte)(data >> (3*8)); |
| 393 | addr[5] = (bfd_byte)(data >> (2*8)); |
| 394 | addr[6] = (bfd_byte)(data >> (1*8)); |
| 395 | addr[7] = (bfd_byte)(data >> (0*8)); |
| 396 | #else |
| 397 | BFD_FAIL(); |
| 398 | #endif |
| 399 | |
| 400 | } |
| 401 | |
| 402 | void |
| 403 | _do_putl64 (data, addr) |
| 404 | bfd_64_type data; |
| 405 | register bfd_byte *addr; |
| 406 | { |
| 407 | #ifdef __GNUC__ |
| 408 | addr[7] = (bfd_byte)(data >> (7*8)); |
| 409 | addr[6] = (bfd_byte)(data >> (6*8)); |
| 410 | addr[5] = (bfd_byte)(data >> (5*8)); |
| 411 | addr[4] = (bfd_byte)(data >> (4*8)); |
| 412 | addr[3] = (bfd_byte)(data >> (3*8)); |
| 413 | addr[2] = (bfd_byte)(data >> (2*8)); |
| 414 | addr[1] = (bfd_byte)(data >> (1*8)); |
| 415 | addr[0] = (bfd_byte)(data >> (0*8)); |
| 416 | #else |
| 417 | BFD_FAIL(); |
| 418 | #endif |
| 419 | |
| 420 | } |
| 421 | |
| 422 | \f |
| 423 | /* Default implementation */ |
| 424 | |
| 425 | boolean |
| 426 | DEFUN(bfd_generic_get_section_contents, (abfd, section, location, offset, count), |
| 427 | bfd *abfd AND |
| 428 | sec_ptr section AND |
| 429 | PTR location AND |
| 430 | file_ptr offset AND |
| 431 | bfd_size_type count) |
| 432 | { |
| 433 | if (count == 0) |
| 434 | return true; |
| 435 | if ((bfd_size_type)offset >= section->size |
| 436 | || bfd_seek(abfd,(file_ptr)( section->filepos + offset), SEEK_SET) == -1 |
| 437 | || bfd_read(location, (bfd_size_type)1, count, abfd) != count) |
| 438 | return (false); /* on error */ |
| 439 | return (true); |
| 440 | } |