00001 /* 00002 * Copyright 2003, 2004, 2005, 2006 PathScale, Inc. All Rights Reserved. 00003 */ 00004 00005 /* Generic BFD library interface and support routines. 00006 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 00007 2000, 2001, 2002, 2003, 2004, 2005 00008 Free Software Foundation, Inc. 00009 Written by Cygnus Support. 00010 00011 This file is part of BFD, the Binary File Descriptor library. 00012 00013 This program is free software; you can redistribute it and/or modify 00014 it under the terms of the GNU General Public License as published by 00015 the Free Software Foundation; either version 2 of the License, or 00016 (at your option) any later version. 00017 00018 This program is distributed in the hope that it will be useful, 00019 but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 GNU General Public License for more details. 00022 00023 You should have received a copy of the GNU General Public License 00024 along with this program; if not, write to the Free Software 00025 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 00026 00027 /* 00028 SECTION 00029 <<typedef bfd>> 00030 00031 A BFD has type <<bfd>>; objects of this type are the 00032 cornerstone of any application using BFD. Using BFD 00033 consists of making references though the BFD and to data in the BFD. 00034 00035 Here is the structure that defines the type <<bfd>>. It 00036 contains the major data about the file and pointers 00037 to the rest of the data. 00038 00039 CODE_FRAGMENT 00040 . 00041 .struct bfd 00042 .{ 00043 . {* A unique identifier of the BFD *} 00044 . unsigned int id; 00045 . 00046 . {* The filename the application opened the BFD with. *} 00047 . const char *filename; 00048 . 00049 . {* A pointer to the target jump table. *} 00050 . const struct bfd_target *xvec; 00051 . 00052 . {* The IOSTREAM, and corresponding IO vector that provide access 00053 . to the file backing the BFD. *} 00054 . void *iostream; 00055 . const struct bfd_iovec *iovec; 00056 . 00057 . {* Is the file descriptor being cached? That is, can it be closed as 00058 . needed, and re-opened when accessed later? *} 00059 . bfd_boolean cacheable; 00060 . 00061 . {* Marks whether there was a default target specified when the 00062 . BFD was opened. This is used to select which matching algorithm 00063 . to use to choose the back end. *} 00064 . bfd_boolean target_defaulted; 00065 . 00066 . {* The caching routines use these to maintain a 00067 . least-recently-used list of BFDs. *} 00068 . struct bfd *lru_prev, *lru_next; 00069 . 00070 . {* When a file is closed by the caching routines, BFD retains 00071 . state information on the file here... *} 00072 . ufile_ptr where; 00073 . 00074 . {* ... and here: (``once'' means at least once). *} 00075 . bfd_boolean opened_once; 00076 . 00077 . {* Set if we have a locally maintained mtime value, rather than 00078 . getting it from the file each time. *} 00079 . bfd_boolean mtime_set; 00080 . 00081 . {* File modified time, if mtime_set is TRUE. *} 00082 . long mtime; 00083 . 00084 . {* Reserved for an unimplemented file locking extension. *} 00085 . int ifd; 00086 . 00087 . {* The format which belongs to the BFD. (object, core, etc.) *} 00088 . bfd_format format; 00089 . 00090 . {* The direction with which the BFD was opened. *} 00091 . enum bfd_direction 00092 . { 00093 . no_direction = 0, 00094 . read_direction = 1, 00095 . write_direction = 2, 00096 . both_direction = 3 00097 . } 00098 . direction; 00099 . 00100 . {* Format_specific flags. *} 00101 . flagword flags; 00102 . 00103 . {* Currently my_archive is tested before adding origin to 00104 . anything. I believe that this can become always an add of 00105 . origin, with origin set to 0 for non archive files. *} 00106 . ufile_ptr origin; 00107 . 00108 . {* Remember when output has begun, to stop strange things 00109 . from happening. *} 00110 . bfd_boolean output_has_begun; 00111 . 00112 . {* A hash table for section names. *} 00113 . struct bfd_hash_table section_htab; 00114 . 00115 . {* Pointer to linked list of sections. *} 00116 . struct bfd_section *sections; 00117 . 00118 . {* The place where we add to the section list. *} 00119 . struct bfd_section **section_tail; 00120 . 00121 . {* The number of sections. *} 00122 . unsigned int section_count; 00123 . 00124 . {* Stuff only useful for object files: 00125 . The start address. *} 00126 . bfd_vma start_address; 00127 . 00128 . {* Used for input and output. *} 00129 . unsigned int symcount; 00130 . 00131 . {* Symbol table for output BFD (with symcount entries). *} 00132 . struct bfd_symbol **outsymbols; 00133 . 00134 . {* Used for slurped dynamic symbol tables. *} 00135 . unsigned int dynsymcount; 00136 . 00137 . {* Pointer to structure which contains architecture information. *} 00138 . const struct bfd_arch_info *arch_info; 00139 . 00140 . {* Flag set if symbols from this BFD should not be exported. *} 00141 . bfd_boolean no_export; 00142 . 00143 . {* Stuff only useful for archives. *} 00144 . void *arelt_data; 00145 . struct bfd *my_archive; {* The containing archive BFD. *} 00146 . struct bfd *next; {* The next BFD in the archive. *} 00147 . struct bfd *archive_head; {* The first BFD in the archive. *} 00148 . bfd_boolean has_armap; 00149 . 00150 . {* A chain of BFD structures involved in a link. *} 00151 . struct bfd *link_next; 00152 . 00153 . {* A field used by _bfd_generic_link_add_archive_symbols. This will 00154 . be used only for archive elements. *} 00155 . int archive_pass; 00156 . 00157 . {* Used by the back end to hold private data. *} 00158 . union 00159 . { 00160 . struct aout_data_struct *aout_data; 00161 . struct artdata *aout_ar_data; 00162 . struct _oasys_data *oasys_obj_data; 00163 . struct _oasys_ar_data *oasys_ar_data; 00164 . struct coff_tdata *coff_obj_data; 00165 . struct pe_tdata *pe_obj_data; 00166 . struct xcoff_tdata *xcoff_obj_data; 00167 . struct ecoff_tdata *ecoff_obj_data; 00168 . struct ieee_data_struct *ieee_data; 00169 . struct ieee_ar_data_struct *ieee_ar_data; 00170 . struct srec_data_struct *srec_data; 00171 . struct ihex_data_struct *ihex_data; 00172 . struct tekhex_data_struct *tekhex_data; 00173 . struct elf_obj_tdata *elf_obj_data; 00174 . struct nlm_obj_tdata *nlm_obj_data; 00175 . struct bout_data_struct *bout_data; 00176 . struct mmo_data_struct *mmo_data; 00177 . struct sun_core_struct *sun_core_data; 00178 . struct sco5_core_struct *sco5_core_data; 00179 . struct trad_core_struct *trad_core_data; 00180 . struct som_data_struct *som_data; 00181 . struct hpux_core_struct *hpux_core_data; 00182 . struct hppabsd_core_struct *hppabsd_core_data; 00183 . struct sgi_core_struct *sgi_core_data; 00184 . struct lynx_core_struct *lynx_core_data; 00185 . struct osf_core_struct *osf_core_data; 00186 . struct cisco_core_struct *cisco_core_data; 00187 . struct versados_data_struct *versados_data; 00188 . struct netbsd_core_struct *netbsd_core_data; 00189 . struct mach_o_data_struct *mach_o_data; 00190 . struct mach_o_fat_data_struct *mach_o_fat_data; 00191 . struct bfd_pef_data_struct *pef_data; 00192 . struct bfd_pef_xlib_data_struct *pef_xlib_data; 00193 . struct bfd_sym_data_struct *sym_data; 00194 . void *any; 00195 . } 00196 . tdata; 00197 . 00198 . {* Used by the application to hold private data. *} 00199 . void *usrdata; 00200 . 00201 . {* Where all the allocated stuff under this BFD goes. This is a 00202 . struct objalloc *, but we use void * to avoid requiring the inclusion 00203 . of objalloc.h. *} 00204 . void *memory; 00205 .}; 00206 . 00207 */ 00208 00209 #include "bfd.h" 00210 #include "bfdver.h" 00211 #include "sysdep.h" 00212 #include <stdarg.h> 00213 #include "libiberty.h" 00214 #include "safe-ctype.h" 00215 #include "bfdlink.h" 00216 #include "libbfd.h" 00217 #include "coff/internal.h" 00218 #include "coff/sym.h" 00219 #include "libcoff.h" 00220 #include "libecoff.h" 00221 #undef obj_symbols 00222 #include "elf-bfd.h" 00223 00224 /* provide storage for subsystem, stack and heap data which may have been 00225 passed in on the command line. Ld puts this data into a bfd_link_info 00226 struct which ultimately gets passed in to the bfd. When it arrives, copy 00227 it to the following struct so that the data will be available in coffcode.h 00228 where it is needed. The typedef's used are defined in bfd.h */ 00229 00230 /* 00231 SECTION 00232 Error reporting 00233 00234 Most BFD functions return nonzero on success (check their 00235 individual documentation for precise semantics). On an error, 00236 they call <<bfd_set_error>> to set an error condition that callers 00237 can check by calling <<bfd_get_error>>. 00238 If that returns <<bfd_error_system_call>>, then check 00239 <<errno>>. 00240 00241 The easiest way to report a BFD error to the user is to 00242 use <<bfd_perror>>. 00243 00244 SUBSECTION 00245 Type <<bfd_error_type>> 00246 00247 The values returned by <<bfd_get_error>> are defined by the 00248 enumerated type <<bfd_error_type>>. 00249 00250 CODE_FRAGMENT 00251 . 00252 .typedef enum bfd_error 00253 .{ 00254 . bfd_error_no_error = 0, 00255 . bfd_error_system_call, 00256 . bfd_error_invalid_target, 00257 . bfd_error_wrong_format, 00258 . bfd_error_wrong_object_format, 00259 . bfd_error_invalid_operation, 00260 . bfd_error_no_memory, 00261 . bfd_error_no_symbols, 00262 . bfd_error_no_armap, 00263 . bfd_error_no_more_archived_files, 00264 . bfd_error_malformed_archive, 00265 . bfd_error_file_not_recognized, 00266 . bfd_error_file_ambiguously_recognized, 00267 . bfd_error_no_contents, 00268 . bfd_error_nonrepresentable_section, 00269 . bfd_error_no_debug_section, 00270 . bfd_error_bad_value, 00271 . bfd_error_file_truncated, 00272 . bfd_error_file_too_big, 00273 . bfd_error_invalid_error_code 00274 .} 00275 .bfd_error_type; 00276 . 00277 */ 00278 00279 static bfd_error_type bfd_error = bfd_error_no_error; 00280 00281 const char *const bfd_errmsgs[] = 00282 { 00283 N_("No error"), 00284 N_("System call error"), 00285 N_("Invalid bfd target"), 00286 N_("File in wrong format"), 00287 N_("Archive object file in wrong format"), 00288 N_("Invalid operation"), 00289 N_("Memory exhausted"), 00290 N_("No symbols"), 00291 N_("Archive has no index; run ranlib to add one"), 00292 N_("No more archived files"), 00293 N_("Malformed archive"), 00294 N_("File format not recognized"), 00295 N_("File format is ambiguous"), 00296 N_("Section has no contents"), 00297 N_("Nonrepresentable section on output"), 00298 N_("Symbol needs debug section which does not exist"), 00299 N_("Bad value"), 00300 N_("File truncated"), 00301 N_("File too big"), 00302 N_("#<Invalid error code>") 00303 }; 00304 00305 /* 00306 FUNCTION 00307 bfd_get_error 00308 00309 SYNOPSIS 00310 bfd_error_type bfd_get_error (void); 00311 00312 DESCRIPTION 00313 Return the current BFD error condition. 00314 */ 00315 00316 bfd_error_type 00317 bfd_get_error (void) 00318 { 00319 return bfd_error; 00320 } 00321 00322 /* 00323 FUNCTION 00324 bfd_set_error 00325 00326 SYNOPSIS 00327 void bfd_set_error (bfd_error_type error_tag); 00328 00329 DESCRIPTION 00330 Set the BFD error condition to be @var{error_tag}. 00331 */ 00332 00333 void 00334 bfd_set_error (bfd_error_type error_tag) 00335 { 00336 bfd_error = error_tag; 00337 } 00338 00339 /* 00340 FUNCTION 00341 bfd_errmsg 00342 00343 SYNOPSIS 00344 const char *bfd_errmsg (bfd_error_type error_tag); 00345 00346 DESCRIPTION 00347 Return a string describing the error @var{error_tag}, or 00348 the system error if @var{error_tag} is <<bfd_error_system_call>>. 00349 */ 00350 00351 const char * 00352 bfd_errmsg (bfd_error_type error_tag) 00353 { 00354 #ifndef errno 00355 extern int errno; 00356 #endif 00357 if (error_tag == bfd_error_system_call) 00358 return xstrerror (errno); 00359 00360 if (error_tag > bfd_error_invalid_error_code) 00361 error_tag = bfd_error_invalid_error_code; /* sanity check */ 00362 00363 return _(bfd_errmsgs [error_tag]); 00364 } 00365 00366 /* 00367 FUNCTION 00368 bfd_perror 00369 00370 SYNOPSIS 00371 void bfd_perror (const char *message); 00372 00373 DESCRIPTION 00374 Print to the standard error stream a string describing the 00375 last BFD error that occurred, or the last system error if 00376 the last BFD error was a system call failure. If @var{message} 00377 is non-NULL and non-empty, the error string printed is preceded 00378 by @var{message}, a colon, and a space. It is followed by a newline. 00379 */ 00380 00381 void 00382 bfd_perror (const char *message) 00383 { 00384 if (bfd_get_error () == bfd_error_system_call) 00385 /* Must be a system error then. */ 00386 perror ((char *) message); 00387 else 00388 { 00389 if (message == NULL || *message == '\0') 00390 fprintf (stderr, "%s\n", bfd_errmsg (bfd_get_error ())); 00391 else 00392 fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_get_error ())); 00393 } 00394 } 00395 00396 /* 00397 SUBSECTION 00398 BFD error handler 00399 00400 Some BFD functions want to print messages describing the 00401 problem. They call a BFD error handler function. This 00402 function may be overridden by the program. 00403 00404 The BFD error handler acts like printf. 00405 00406 CODE_FRAGMENT 00407 . 00408 .typedef void (*bfd_error_handler_type) (const char *, ...); 00409 . 00410 */ 00411 00412 /* The program name used when printing BFD error messages. */ 00413 00414 static const char *_bfd_error_program_name; 00415 00416 /* This is the default routine to handle BFD error messages. 00417 Like fprintf (stderr, ...), but also handles some extra format specifiers. 00418 00419 %A section name from section. For group components, print group name too. 00420 %B file name from bfd. For archive components, prints archive too. 00421 */ 00422 00423 void 00424 _bfd_default_error_handler (const char *fmt, ...) 00425 { 00426 va_list ap; 00427 char *bufp; 00428 const char *new_fmt, *p; 00429 size_t avail = 1000; 00430 char buf[1000]; 00431 00432 if (_bfd_error_program_name != NULL) 00433 fprintf (stderr, "%s: ", _bfd_error_program_name); 00434 else 00435 fprintf (stderr, "BFD: "); 00436 00437 va_start (ap, fmt); 00438 new_fmt = fmt; 00439 bufp = buf; 00440 00441 /* Reserve enough space for the existing format string. */ 00442 avail -= strlen (fmt) + 1; 00443 if (avail > 1000) 00444 abort (); 00445 00446 p = fmt; 00447 while (1) 00448 { 00449 char *q; 00450 size_t len, extra, trim; 00451 00452 p = strchr (p, '%'); 00453 if (p == NULL || p[1] == '\0') 00454 { 00455 if (new_fmt == buf) 00456 { 00457 len = strlen (fmt); 00458 memcpy (bufp, fmt, len + 1); 00459 } 00460 break; 00461 } 00462 00463 if (p[1] == 'A' || p[1] == 'B') 00464 { 00465 len = p - fmt; 00466 memcpy (bufp, fmt, len); 00467 bufp += len; 00468 fmt = p + 2; 00469 new_fmt = buf; 00470 00471 /* If we run out of space, tough, you lose your ridiculously 00472 long file or section name. It's not safe to try to alloc 00473 memory here; We might be printing an out of memory message. */ 00474 if (avail == 0) 00475 { 00476 *bufp++ = '*'; 00477 *bufp++ = '*'; 00478 *bufp = '\0'; 00479 } 00480 else 00481 { 00482 if (p[1] == 'B') 00483 { 00484 bfd *abfd = va_arg (ap, bfd *); 00485 if (abfd->my_archive) 00486 snprintf (bufp, avail, "%s(%s)", 00487 abfd->my_archive->filename, abfd->filename); 00488 else 00489 snprintf (bufp, avail, "%s", abfd->filename); 00490 } 00491 else 00492 { 00493 asection *sec = va_arg (ap, asection *); 00494 bfd *abfd = sec->owner; 00495 const char *group = NULL; 00496 struct coff_comdat_info *ci; 00497 00498 if (abfd != NULL 00499 && bfd_get_flavour (abfd) == bfd_target_elf_flavour 00500 && elf_next_in_group (sec) != NULL 00501 && (sec->flags & SEC_GROUP) == 0) 00502 group = elf_group_name (sec); 00503 else if (abfd != NULL 00504 && bfd_get_flavour (abfd) == bfd_target_coff_flavour 00505 && (ci = bfd_coff_get_comdat_section (sec->owner, 00506 sec)) != NULL) 00507 group = ci->name; 00508 if (group != NULL) 00509 snprintf (bufp, avail, "%s[%s]", sec->name, group); 00510 else 00511 snprintf (bufp, avail, "%s", sec->name); 00512 } 00513 len = strlen (bufp); 00514 avail = avail - len + 2; 00515 00516 /* We need to replace any '%' we printed by "%%". 00517 First count how many. */ 00518 q = bufp; 00519 bufp += len; 00520 extra = 0; 00521 while ((q = strchr (q, '%')) != NULL) 00522 { 00523 ++q; 00524 ++extra; 00525 } 00526 00527 /* If there isn't room, trim off the end of the string. */ 00528 q = bufp; 00529 bufp += extra; 00530 if (extra > avail) 00531 { 00532 trim = extra - avail; 00533 bufp -= trim; 00534 do 00535 { 00536 if (*--q == '%') 00537 --extra; 00538 } 00539 while (--trim != 0); 00540 *q = '\0'; 00541 avail = extra; 00542 } 00543 avail -= extra; 00544 00545 /* Now double all '%' chars, shuffling the string as we go. */ 00546 while (extra != 0) 00547 { 00548 while ((q[extra] = *q) != '%') 00549 --q; 00550 q[--extra] = '%'; 00551 --q; 00552 } 00553 } 00554 } 00555 p = p + 2; 00556 } 00557 00558 vfprintf (stderr, new_fmt, ap); 00559 va_end (ap); 00560 00561 putc ('\n', stderr); 00562 } 00563 00564 /* This is a function pointer to the routine which should handle BFD 00565 error messages. It is called when a BFD routine encounters an 00566 error for which it wants to print a message. Going through a 00567 function pointer permits a program linked against BFD to intercept 00568 the messages and deal with them itself. */ 00569 00570 bfd_error_handler_type _bfd_error_handler = _bfd_default_error_handler; 00571 00572 /* 00573 FUNCTION 00574 bfd_set_error_handler 00575 00576 SYNOPSIS 00577 bfd_error_handler_type bfd_set_error_handler (bfd_error_handler_type); 00578 00579 DESCRIPTION 00580 Set the BFD error handler function. Returns the previous 00581 function. 00582 */ 00583 00584 bfd_error_handler_type 00585 bfd_set_error_handler (bfd_error_handler_type pnew) 00586 { 00587 bfd_error_handler_type pold; 00588 00589 pold = _bfd_error_handler; 00590 _bfd_error_handler = pnew; 00591 return pold; 00592 } 00593 00594 /* 00595 FUNCTION 00596 bfd_set_error_program_name 00597 00598 SYNOPSIS 00599 void bfd_set_error_program_name (const char *); 00600 00601 DESCRIPTION 00602 Set the program name to use when printing a BFD error. This 00603 is printed before the error message followed by a colon and 00604 space. The string must not be changed after it is passed to 00605 this function. 00606 */ 00607 00608 void 00609 bfd_set_error_program_name (const char *name) 00610 { 00611 _bfd_error_program_name = name; 00612 } 00613 00614 /* 00615 FUNCTION 00616 bfd_get_error_handler 00617 00618 SYNOPSIS 00619 bfd_error_handler_type bfd_get_error_handler (void); 00620 00621 DESCRIPTION 00622 Return the BFD error handler function. 00623 */ 00624 00625 bfd_error_handler_type 00626 bfd_get_error_handler (void) 00627 { 00628 return _bfd_error_handler; 00629 } 00630 00631 /* 00632 SECTION 00633 Symbols 00634 */ 00635 00636 /* 00637 FUNCTION 00638 bfd_get_reloc_upper_bound 00639 00640 SYNOPSIS 00641 long bfd_get_reloc_upper_bound (bfd *abfd, asection *sect); 00642 00643 DESCRIPTION 00644 Return the number of bytes required to store the 00645 relocation information associated with section @var{sect} 00646 attached to bfd @var{abfd}. If an error occurs, return -1. 00647 00648 */ 00649 00650 long 00651 bfd_get_reloc_upper_bound (bfd *abfd, sec_ptr asect) 00652 { 00653 if (abfd->format != bfd_object) 00654 { 00655 bfd_set_error (bfd_error_invalid_operation); 00656 return -1; 00657 } 00658 00659 return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect)); 00660 } 00661 00662 /* 00663 FUNCTION 00664 bfd_canonicalize_reloc 00665 00666 SYNOPSIS 00667 long bfd_canonicalize_reloc 00668 (bfd *abfd, asection *sec, arelent **loc, asymbol **syms); 00669 00670 DESCRIPTION 00671 Call the back end associated with the open BFD 00672 @var{abfd} and translate the external form of the relocation 00673 information attached to @var{sec} into the internal canonical 00674 form. Place the table into memory at @var{loc}, which has 00675 been preallocated, usually by a call to 00676 <<bfd_get_reloc_upper_bound>>. Returns the number of relocs, or 00677 -1 on error. 00678 00679 The @var{syms} table is also needed for horrible internal magic 00680 reasons. 00681 00682 */ 00683 long 00684 bfd_canonicalize_reloc (bfd *abfd, 00685 sec_ptr asect, 00686 arelent **location, 00687 asymbol **symbols) 00688 { 00689 if (abfd->format != bfd_object) 00690 { 00691 bfd_set_error (bfd_error_invalid_operation); 00692 return -1; 00693 } 00694 00695 return BFD_SEND (abfd, _bfd_canonicalize_reloc, 00696 (abfd, asect, location, symbols)); 00697 } 00698 00699 /* 00700 FUNCTION 00701 bfd_set_reloc 00702 00703 SYNOPSIS 00704 void bfd_set_reloc 00705 (bfd *abfd, asection *sec, arelent **rel, unsigned int count); 00706 00707 DESCRIPTION 00708 Set the relocation pointer and count within 00709 section @var{sec} to the values @var{rel} and @var{count}. 00710 The argument @var{abfd} is ignored. 00711 00712 */ 00713 00714 void 00715 bfd_set_reloc (bfd *ignore_abfd ATTRIBUTE_UNUSED, 00716 sec_ptr asect, 00717 arelent **location, 00718 unsigned int count) 00719 { 00720 asect->orelocation = location; 00721 asect->reloc_count = count; 00722 } 00723 00724 /* 00725 FUNCTION 00726 bfd_set_file_flags 00727 00728 SYNOPSIS 00729 bfd_boolean bfd_set_file_flags (bfd *abfd, flagword flags); 00730 00731 DESCRIPTION 00732 Set the flag word in the BFD @var{abfd} to the value @var{flags}. 00733 00734 Possible errors are: 00735 o <<bfd_error_wrong_format>> - The target bfd was not of object format. 00736 o <<bfd_error_invalid_operation>> - The target bfd was open for reading. 00737 o <<bfd_error_invalid_operation>> - 00738 The flag word contained a bit which was not applicable to the 00739 type of file. E.g., an attempt was made to set the <<D_PAGED>> bit 00740 on a BFD format which does not support demand paging. 00741 00742 */ 00743 00744 bfd_boolean 00745 bfd_set_file_flags (bfd *abfd, flagword flags) 00746 { 00747 if (abfd->format != bfd_object) 00748 { 00749 bfd_set_error (bfd_error_wrong_format); 00750 return FALSE; 00751 } 00752 00753 if (bfd_read_p (abfd)) 00754 { 00755 bfd_set_error (bfd_error_invalid_operation); 00756 return FALSE; 00757 } 00758 00759 bfd_get_file_flags (abfd) = flags; 00760 if ((flags & bfd_applicable_file_flags (abfd)) != flags) 00761 { 00762 bfd_set_error (bfd_error_invalid_operation); 00763 return FALSE; 00764 } 00765 00766 return TRUE; 00767 } 00768 00769 void 00770 bfd_assert (const char *file, int line) 00771 { 00772 (*_bfd_error_handler) (_("BFD %s assertion fail %s:%d"), 00773 BFD_VERSION_STRING, file, line); 00774 } 00775 00776 /* A more or less friendly abort message. In libbfd.h abort is 00777 defined to call this function. */ 00778 00779 #ifndef EXIT_FAILURE 00780 #define EXIT_FAILURE 1 00781 #endif 00782 00783 void 00784 _bfd_abort (const char *file, int line, const char *fn) 00785 { 00786 if (fn != NULL) 00787 (*_bfd_error_handler) 00788 (_("BFD %s internal error, aborting at %s line %d in %s\n"), 00789 BFD_VERSION_STRING, file, line, fn); 00790 else 00791 (*_bfd_error_handler) 00792 (_("BFD %s internal error, aborting at %s line %d\n"), 00793 BFD_VERSION_STRING, file, line); 00794 (*_bfd_error_handler) (_("Please report this bug.\n")); 00795 xexit (EXIT_FAILURE); 00796 } 00797 00798 /* 00799 FUNCTION 00800 bfd_get_arch_size 00801 00802 SYNOPSIS 00803 int bfd_get_arch_size (bfd *abfd); 00804 00805 DESCRIPTION 00806 Returns the architecture address size, in bits, as determined 00807 by the object file's format. For ELF, this information is 00808 included in the header. 00809 00810 RETURNS 00811 Returns the arch size in bits if known, <<-1>> otherwise. 00812 */ 00813 00814 int 00815 bfd_get_arch_size (bfd *abfd) 00816 { 00817 if (abfd->xvec->flavour == bfd_target_elf_flavour) 00818 return get_elf_backend_data (abfd)->s->arch_size; 00819 00820 return -1; 00821 } 00822 00823 /* 00824 FUNCTION 00825 bfd_get_sign_extend_vma 00826 00827 SYNOPSIS 00828 int bfd_get_sign_extend_vma (bfd *abfd); 00829 00830 DESCRIPTION 00831 Indicates if the target architecture "naturally" sign extends 00832 an address. Some architectures implicitly sign extend address 00833 values when they are converted to types larger than the size 00834 of an address. For instance, bfd_get_start_address() will 00835 return an address sign extended to fill a bfd_vma when this is 00836 the case. 00837 00838 RETURNS 00839 Returns <<1>> if the target architecture is known to sign 00840 extend addresses, <<0>> if the target architecture is known to 00841 not sign extend addresses, and <<-1>> otherwise. 00842 */ 00843 00844 int 00845 bfd_get_sign_extend_vma (bfd *abfd) 00846 { 00847 char *name; 00848 00849 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) 00850 return get_elf_backend_data (abfd)->sign_extend_vma; 00851 00852 name = bfd_get_target (abfd); 00853 00854 /* Return a proper value for DJGPP & PE COFF (x86 COFF variants). 00855 This function is required for DWARF2 support, but there is 00856 no place to store this information in the COFF back end. 00857 Should enough other COFF targets add support for DWARF2, 00858 a place will have to be found. Until then, this hack will do. */ 00859 if (strncmp (name, "coff-go32", sizeof ("coff-go32") - 1) == 0 00860 || strcmp (name, "pe-i386") == 0 00861 || strcmp (name, "pei-i386") == 0) 00862 return 1; 00863 00864 bfd_set_error (bfd_error_wrong_format); 00865 return -1; 00866 } 00867 00868 /* 00869 FUNCTION 00870 bfd_set_start_address 00871 00872 SYNOPSIS 00873 bfd_boolean bfd_set_start_address (bfd *abfd, bfd_vma vma); 00874 00875 DESCRIPTION 00876 Make @var{vma} the entry point of output BFD @var{abfd}. 00877 00878 RETURNS 00879 Returns <<TRUE>> on success, <<FALSE>> otherwise. 00880 */ 00881 00882 bfd_boolean 00883 bfd_set_start_address (bfd *abfd, bfd_vma vma) 00884 { 00885 abfd->start_address = vma; 00886 return TRUE; 00887 } 00888 00889 /* 00890 FUNCTION 00891 bfd_get_gp_size 00892 00893 SYNOPSIS 00894 unsigned int bfd_get_gp_size (bfd *abfd); 00895 00896 DESCRIPTION 00897 Return the maximum size of objects to be optimized using the GP 00898 register under MIPS ECOFF. This is typically set by the <<-G>> 00899 argument to the compiler, assembler or linker. 00900 */ 00901 00902 unsigned int 00903 bfd_get_gp_size (bfd *abfd) 00904 { 00905 if (abfd->format == bfd_object) 00906 { 00907 if (abfd->xvec->flavour == bfd_target_ecoff_flavour) 00908 return ecoff_data (abfd)->gp_size; 00909 else if (abfd->xvec->flavour == bfd_target_elf_flavour) 00910 return elf_gp_size (abfd); 00911 } 00912 return 0; 00913 } 00914 00915 /* 00916 FUNCTION 00917 bfd_set_gp_size 00918 00919 SYNOPSIS 00920 void bfd_set_gp_size (bfd *abfd, unsigned int i); 00921 00922 DESCRIPTION 00923 Set the maximum size of objects to be optimized using the GP 00924 register under ECOFF or MIPS ELF. This is typically set by 00925 the <<-G>> argument to the compiler, assembler or linker. 00926 */ 00927 00928 void 00929 bfd_set_gp_size (bfd *abfd, unsigned int i) 00930 { 00931 /* Don't try to set GP size on an archive or core file! */ 00932 if (abfd->format != bfd_object) 00933 return; 00934 00935 if (abfd->xvec->flavour == bfd_target_ecoff_flavour) 00936 ecoff_data (abfd)->gp_size = i; 00937 else if (abfd->xvec->flavour == bfd_target_elf_flavour) 00938 elf_gp_size (abfd) = i; 00939 } 00940 00941 /* Get the GP value. This is an internal function used by some of the 00942 relocation special_function routines on targets which support a GP 00943 register. */ 00944 00945 bfd_vma 00946 _bfd_get_gp_value (bfd *abfd) 00947 { 00948 if (! abfd) 00949 return 0; 00950 if (abfd->format != bfd_object) 00951 return 0; 00952 00953 if (abfd->xvec->flavour == bfd_target_ecoff_flavour) 00954 return ecoff_data (abfd)->gp; 00955 else if (abfd->xvec->flavour == bfd_target_elf_flavour) 00956 return elf_gp (abfd); 00957 00958 return 0; 00959 } 00960 00961 /* Set the GP value. */ 00962 00963 void 00964 _bfd_set_gp_value (bfd *abfd, bfd_vma v) 00965 { 00966 if (! abfd) 00967 BFD_FAIL (); 00968 if (abfd->format != bfd_object) 00969 return; 00970 00971 if (abfd->xvec->flavour == bfd_target_ecoff_flavour) 00972 ecoff_data (abfd)->gp = v; 00973 else if (abfd->xvec->flavour == bfd_target_elf_flavour) 00974 elf_gp (abfd) = v; 00975 } 00976 00977 /* 00978 FUNCTION 00979 bfd_scan_vma 00980 00981 SYNOPSIS 00982 bfd_vma bfd_scan_vma (const char *string, const char **end, int base); 00983 00984 DESCRIPTION 00985 Convert, like <<strtoul>>, a numerical expression 00986 @var{string} into a <<bfd_vma>> integer, and return that integer. 00987 (Though without as many bells and whistles as <<strtoul>>.) 00988 The expression is assumed to be unsigned (i.e., positive). 00989 If given a @var{base}, it is used as the base for conversion. 00990 A base of 0 causes the function to interpret the string 00991 in hex if a leading "0x" or "0X" is found, otherwise 00992 in octal if a leading zero is found, otherwise in decimal. 00993 00994 If the value would overflow, the maximum <<bfd_vma>> value is 00995 returned. 00996 */ 00997 00998 bfd_vma 00999 bfd_scan_vma (const char *string, const char **end, int base) 01000 { 01001 bfd_vma value; 01002 bfd_vma cutoff; 01003 unsigned int cutlim; 01004 int overflow; 01005 01006 /* Let the host do it if possible. */ 01007 if (sizeof (bfd_vma) <= sizeof (unsigned long)) 01008 return strtoul (string, (char **) end, base); 01009 01010 #ifdef HAVE_STRTOULL 01011 if (sizeof (bfd_vma) <= sizeof (unsigned long long)) 01012 return strtoull (string, (char **) end, base); 01013 #endif 01014 01015 if (base == 0) 01016 { 01017 if (string[0] == '0') 01018 { 01019 if ((string[1] == 'x') || (string[1] == 'X')) 01020 base = 16; 01021 else 01022 base = 8; 01023 } 01024 } 01025 01026 if ((base < 2) || (base > 36)) 01027 base = 10; 01028 01029 if (base == 16 01030 && string[0] == '0' 01031 && (string[1] == 'x' || string[1] == 'X') 01032 && ISXDIGIT (string[2])) 01033 { 01034 string += 2; 01035 } 01036 01037 cutoff = (~ (bfd_vma) 0) / (bfd_vma) base; 01038 cutlim = (~ (bfd_vma) 0) % (bfd_vma) base; 01039 value = 0; 01040 overflow = 0; 01041 while (1) 01042 { 01043 unsigned int digit; 01044 01045 digit = *string; 01046 if (ISDIGIT (digit)) 01047 digit = digit - '0'; 01048 else if (ISALPHA (digit)) 01049 digit = TOUPPER (digit) - 'A' + 10; 01050 else 01051 break; 01052 if (digit >= (unsigned int) base) 01053 break; 01054 if (value > cutoff || (value == cutoff && digit > cutlim)) 01055 overflow = 1; 01056 value = value * base + digit; 01057 ++string; 01058 } 01059 01060 if (overflow) 01061 value = ~ (bfd_vma) 0; 01062 01063 if (end != NULL) 01064 *end = string; 01065 01066 return value; 01067 } 01068 01069 /* 01070 FUNCTION 01071 bfd_copy_private_header_data 01072 01073 SYNOPSIS 01074 bfd_boolean bfd_copy_private_header_data (bfd *ibfd, bfd *obfd); 01075 01076 DESCRIPTION 01077 Copy private BFD header information from the BFD @var{ibfd} to the 01078 the BFD @var{obfd}. This copies information that may require 01079 sections to exist, but does not require symbol tables. Return 01080 <<true>> on success, <<false>> on error. 01081 Possible error returns are: 01082 01083 o <<bfd_error_no_memory>> - 01084 Not enough memory exists to create private data for @var{obfd}. 01085 01086 .#define bfd_copy_private_header_data(ibfd, obfd) \ 01087 . BFD_SEND (obfd, _bfd_copy_private_header_data, \ 01088 . (ibfd, obfd)) 01089 01090 */ 01091 01092 /* 01093 FUNCTION 01094 bfd_copy_private_bfd_data 01095 01096 SYNOPSIS 01097 bfd_boolean bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd); 01098 01099 DESCRIPTION 01100 Copy private BFD information from the BFD @var{ibfd} to the 01101 the BFD @var{obfd}. Return <<TRUE>> on success, <<FALSE>> on error. 01102 Possible error returns are: 01103 01104 o <<bfd_error_no_memory>> - 01105 Not enough memory exists to create private data for @var{obfd}. 01106 01107 .#define bfd_copy_private_bfd_data(ibfd, obfd) \ 01108 . BFD_SEND (obfd, _bfd_copy_private_bfd_data, \ 01109 . (ibfd, obfd)) 01110 01111 */ 01112 01113 /* 01114 FUNCTION 01115 bfd_merge_private_bfd_data 01116 01117 SYNOPSIS 01118 bfd_boolean bfd_merge_private_bfd_data (bfd *ibfd, bfd *obfd); 01119 01120 DESCRIPTION 01121 Merge private BFD information from the BFD @var{ibfd} to the 01122 the output file BFD @var{obfd} when linking. Return <<TRUE>> 01123 on success, <<FALSE>> on error. Possible error returns are: 01124 01125 o <<bfd_error_no_memory>> - 01126 Not enough memory exists to create private data for @var{obfd}. 01127 01128 .#define bfd_merge_private_bfd_data(ibfd, obfd) \ 01129 . BFD_SEND (obfd, _bfd_merge_private_bfd_data, \ 01130 . (ibfd, obfd)) 01131 01132 */ 01133 01134 /* 01135 FUNCTION 01136 bfd_set_private_flags 01137 01138 SYNOPSIS 01139 bfd_boolean bfd_set_private_flags (bfd *abfd, flagword flags); 01140 01141 DESCRIPTION 01142 Set private BFD flag information in the BFD @var{abfd}. 01143 Return <<TRUE>> on success, <<FALSE>> on error. Possible error 01144 returns are: 01145 01146 o <<bfd_error_no_memory>> - 01147 Not enough memory exists to create private data for @var{obfd}. 01148 01149 .#define bfd_set_private_flags(abfd, flags) \ 01150 . BFD_SEND (abfd, _bfd_set_private_flags, (abfd, flags)) 01151 01152 */ 01153 01154 /* 01155 FUNCTION 01156 Other functions 01157 01158 DESCRIPTION 01159 The following functions exist but have not yet been documented. 01160 01161 .#define bfd_sizeof_headers(abfd, reloc) \ 01162 . BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, reloc)) 01163 . 01164 .#define bfd_find_nearest_line(abfd, sec, syms, off, file, func, line) \ 01165 . BFD_SEND (abfd, _bfd_find_nearest_line, \ 01166 . (abfd, sec, syms, off, file, func, line)) 01167 . 01168 .#define bfd_debug_info_start(abfd) \ 01169 . BFD_SEND (abfd, _bfd_debug_info_start, (abfd)) 01170 . 01171 .#define bfd_debug_info_end(abfd) \ 01172 . BFD_SEND (abfd, _bfd_debug_info_end, (abfd)) 01173 . 01174 .#define bfd_debug_info_accumulate(abfd, section) \ 01175 . BFD_SEND (abfd, _bfd_debug_info_accumulate, (abfd, section)) 01176 . 01177 .#define bfd_stat_arch_elt(abfd, stat) \ 01178 . BFD_SEND (abfd, _bfd_stat_arch_elt,(abfd, stat)) 01179 . 01180 .#define bfd_update_armap_timestamp(abfd) \ 01181 . BFD_SEND (abfd, _bfd_update_armap_timestamp, (abfd)) 01182 . 01183 .#define bfd_set_arch_mach(abfd, arch, mach)\ 01184 . BFD_SEND ( abfd, _bfd_set_arch_mach, (abfd, arch, mach)) 01185 . 01186 .#define bfd_relax_section(abfd, section, link_info, again) \ 01187 . BFD_SEND (abfd, _bfd_relax_section, (abfd, section, link_info, again)) 01188 . 01189 .#define bfd_gc_sections(abfd, link_info) \ 01190 . BFD_SEND (abfd, _bfd_gc_sections, (abfd, link_info)) 01191 . 01192 .#define bfd_merge_sections(abfd, link_info) \ 01193 . BFD_SEND (abfd, _bfd_merge_sections, (abfd, link_info)) 01194 . 01195 .#define bfd_is_group_section(abfd, sec) \ 01196 . BFD_SEND (abfd, _bfd_is_group_section, (abfd, sec)) 01197 . 01198 .#define bfd_discard_group(abfd, sec) \ 01199 . BFD_SEND (abfd, _bfd_discard_group, (abfd, sec)) 01200 . 01201 .#define bfd_link_hash_table_create(abfd) \ 01202 . BFD_SEND (abfd, _bfd_link_hash_table_create, (abfd)) 01203 . 01204 .#define bfd_link_hash_table_free(abfd, hash) \ 01205 . BFD_SEND (abfd, _bfd_link_hash_table_free, (hash)) 01206 . 01207 .#define bfd_link_add_symbols(abfd, info) \ 01208 . BFD_SEND (abfd, _bfd_link_add_symbols, (abfd, info)) 01209 . 01210 .#define bfd_link_just_syms(abfd, sec, info) \ 01211 . BFD_SEND (abfd, _bfd_link_just_syms, (sec, info)) 01212 . 01213 .#define bfd_final_link(abfd, info) \ 01214 . BFD_SEND (abfd, _bfd_final_link, (abfd, info)) 01215 . 01216 .#define bfd_free_cached_info(abfd) \ 01217 . BFD_SEND (abfd, _bfd_free_cached_info, (abfd)) 01218 . 01219 .#define bfd_get_dynamic_symtab_upper_bound(abfd) \ 01220 . BFD_SEND (abfd, _bfd_get_dynamic_symtab_upper_bound, (abfd)) 01221 . 01222 .#define bfd_print_private_bfd_data(abfd, file)\ 01223 . BFD_SEND (abfd, _bfd_print_private_bfd_data, (abfd, file)) 01224 . 01225 .#define bfd_canonicalize_dynamic_symtab(abfd, asymbols) \ 01226 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_symtab, (abfd, asymbols)) 01227 . 01228 .#define bfd_get_synthetic_symtab(abfd, count, syms, dyncount, dynsyms, ret) \ 01229 . BFD_SEND (abfd, _bfd_get_synthetic_symtab, (abfd, count, syms, \ 01230 . dyncount, dynsyms, ret)) 01231 . 01232 .#define bfd_get_dynamic_reloc_upper_bound(abfd) \ 01233 . BFD_SEND (abfd, _bfd_get_dynamic_reloc_upper_bound, (abfd)) 01234 . 01235 .#define bfd_canonicalize_dynamic_reloc(abfd, arels, asyms) \ 01236 . BFD_SEND (abfd, _bfd_canonicalize_dynamic_reloc, (abfd, arels, asyms)) 01237 . 01238 .extern bfd_byte *bfd_get_relocated_section_contents 01239 . (bfd *, struct bfd_link_info *, struct bfd_link_order *, bfd_byte *, 01240 . bfd_boolean, asymbol **); 01241 . 01242 01243 */ 01244 01245 bfd_byte * 01246 bfd_get_relocated_section_contents (bfd *abfd, 01247 struct bfd_link_info *link_info, 01248 struct bfd_link_order *link_order, 01249 bfd_byte *data, 01250 bfd_boolean relocatable, 01251 asymbol **symbols) 01252 { 01253 bfd *abfd2; 01254 bfd_byte *(*fn) (bfd *, struct bfd_link_info *, struct bfd_link_order *, 01255 bfd_byte *, bfd_boolean, asymbol **); 01256 01257 if (link_order->type == bfd_indirect_link_order) 01258 { 01259 abfd2 = link_order->u.indirect.section->owner; 01260 if (abfd2 == NULL) 01261 abfd2 = abfd; 01262 } 01263 else 01264 abfd2 = abfd; 01265 01266 fn = abfd2->xvec->_bfd_get_relocated_section_contents; 01267 01268 return (*fn) (abfd, link_info, link_order, data, relocatable, symbols); 01269 } 01270 01271 /* Record information about an ELF program header. */ 01272 01273 bfd_boolean 01274 bfd_record_phdr (bfd *abfd, 01275 unsigned long type, 01276 bfd_boolean flags_valid, 01277 flagword flags, 01278 bfd_boolean at_valid, 01279 bfd_vma at, 01280 bfd_boolean includes_filehdr, 01281 bfd_boolean includes_phdrs, 01282 unsigned int count, 01283 asection **secs) 01284 { 01285 struct elf_segment_map *m, **pm; 01286 bfd_size_type amt; 01287 01288 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 01289 return TRUE; 01290 01291 amt = sizeof (struct elf_segment_map); 01292 amt += ((bfd_size_type) count - 1) * sizeof (asection *); 01293 m = bfd_alloc (abfd, amt); 01294 if (m == NULL) 01295 return FALSE; 01296 01297 m->next = NULL; 01298 m->p_type = type; 01299 m->p_flags = flags; 01300 m->p_paddr = at; 01301 m->p_flags_valid = flags_valid; 01302 m->p_paddr_valid = at_valid; 01303 m->includes_filehdr = includes_filehdr; 01304 m->includes_phdrs = includes_phdrs; 01305 m->count = count; 01306 if (count > 0) 01307 memcpy (m->sections, secs, count * sizeof (asection *)); 01308 01309 for (pm = &elf_tdata (abfd)->segment_map; *pm != NULL; pm = &(*pm)->next) 01310 ; 01311 *pm = m; 01312 01313 return TRUE; 01314 } 01315 01316 void 01317 bfd_sprintf_vma (bfd *abfd, char *buf, bfd_vma value) 01318 { 01319 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) 01320 get_elf_backend_data (abfd)->elf_backend_sprintf_vma (abfd, buf, value); 01321 else 01322 sprintf_vma (buf, value); 01323 } 01324 01325 void 01326 bfd_fprintf_vma (bfd *abfd, void *stream, bfd_vma value) 01327 { 01328 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) 01329 get_elf_backend_data (abfd)->elf_backend_fprintf_vma (abfd, stream, value); 01330 else 01331 fprintf_vma ((FILE *) stream, value); 01332 } 01333 01334 /* 01335 FUNCTION 01336 bfd_alt_mach_code 01337 01338 SYNOPSIS 01339 bfd_boolean bfd_alt_mach_code (bfd *abfd, int alternative); 01340 01341 DESCRIPTION 01342 01343 When more than one machine code number is available for the 01344 same machine type, this function can be used to switch between 01345 the preferred one (alternative == 0) and any others. Currently, 01346 only ELF supports this feature, with up to two alternate 01347 machine codes. 01348 */ 01349 01350 bfd_boolean 01351 bfd_alt_mach_code (bfd *abfd, int alternative) 01352 { 01353 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) 01354 { 01355 int code; 01356 01357 switch (alternative) 01358 { 01359 case 0: 01360 code = get_elf_backend_data (abfd)->elf_machine_code; 01361 break; 01362 01363 case 1: 01364 code = get_elf_backend_data (abfd)->elf_machine_alt1; 01365 if (code == 0) 01366 return FALSE; 01367 break; 01368 01369 case 2: 01370 code = get_elf_backend_data (abfd)->elf_machine_alt2; 01371 if (code == 0) 01372 return FALSE; 01373 break; 01374 01375 default: 01376 return FALSE; 01377 } 01378 01379 elf_elfheader (abfd)->e_machine = code; 01380 01381 return TRUE; 01382 } 01383 01384 return FALSE; 01385 } 01386 01387 /* 01388 CODE_FRAGMENT 01389 01390 .struct bfd_preserve 01391 .{ 01392 . void *marker; 01393 . void *tdata; 01394 . flagword flags; 01395 . const struct bfd_arch_info *arch_info; 01396 . struct bfd_section *sections; 01397 . struct bfd_section **section_tail; 01398 . unsigned int section_count; 01399 . struct bfd_hash_table section_htab; 01400 .}; 01401 . 01402 */ 01403 01404 /* 01405 FUNCTION 01406 bfd_preserve_save 01407 01408 SYNOPSIS 01409 bfd_boolean bfd_preserve_save (bfd *, struct bfd_preserve *); 01410 01411 DESCRIPTION 01412 When testing an object for compatibility with a particular 01413 target back-end, the back-end object_p function needs to set 01414 up certain fields in the bfd on successfully recognizing the 01415 object. This typically happens in a piecemeal fashion, with 01416 failures possible at many points. On failure, the bfd is 01417 supposed to be restored to its initial state, which is 01418 virtually impossible. However, restoring a subset of the bfd 01419 state works in practice. This function stores the subset and 01420 reinitializes the bfd. 01421 01422 */ 01423 01424 bfd_boolean 01425 bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve) 01426 { 01427 preserve->tdata = abfd->tdata.any; 01428 preserve->arch_info = abfd->arch_info; 01429 preserve->flags = abfd->flags; 01430 preserve->sections = abfd->sections; 01431 preserve->section_tail = abfd->section_tail; 01432 preserve->section_count = abfd->section_count; 01433 preserve->section_htab = abfd->section_htab; 01434 01435 if (! bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc)) 01436 return FALSE; 01437 01438 abfd->tdata.any = NULL; 01439 abfd->arch_info = &bfd_default_arch_struct; 01440 abfd->flags &= BFD_IN_MEMORY; 01441 abfd->sections = NULL; 01442 abfd->section_tail = &abfd->sections; 01443 abfd->section_count = 0; 01444 01445 return TRUE; 01446 } 01447 01448 /* 01449 FUNCTION 01450 bfd_preserve_restore 01451 01452 SYNOPSIS 01453 void bfd_preserve_restore (bfd *, struct bfd_preserve *); 01454 01455 DESCRIPTION 01456 This function restores bfd state saved by bfd_preserve_save. 01457 If MARKER is non-NULL in struct bfd_preserve then that block 01458 and all subsequently bfd_alloc'd memory is freed. 01459 01460 */ 01461 01462 void 01463 bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve) 01464 { 01465 bfd_hash_table_free (&abfd->section_htab); 01466 01467 abfd->tdata.any = preserve->tdata; 01468 abfd->arch_info = preserve->arch_info; 01469 abfd->flags = preserve->flags; 01470 abfd->section_htab = preserve->section_htab; 01471 abfd->sections = preserve->sections; 01472 abfd->section_tail = preserve->section_tail; 01473 abfd->section_count = preserve->section_count; 01474 01475 /* bfd_release frees all memory more recently bfd_alloc'd than 01476 its arg, as well as its arg. */ 01477 if (preserve->marker != NULL) 01478 { 01479 bfd_release (abfd, preserve->marker); 01480 preserve->marker = NULL; 01481 } 01482 } 01483 01484 /* 01485 FUNCTION 01486 bfd_preserve_finish 01487 01488 SYNOPSIS 01489 void bfd_preserve_finish (bfd *, struct bfd_preserve *); 01490 01491 DESCRIPTION 01492 This function should be called when the bfd state saved by 01493 bfd_preserve_save is no longer needed. ie. when the back-end 01494 object_p function returns with success. 01495 01496 */ 01497 01498 void 01499 bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve) 01500 { 01501 /* It would be nice to be able to free more memory here, eg. old 01502 tdata, but that's not possible since these blocks are sitting 01503 inside bfd_alloc'd memory. The section hash is on a separate 01504 objalloc. */ 01505 bfd_hash_table_free (&preserve->section_htab); 01506 }
1.5.6