-/* for error messages */
-char *
-bfd_format_string (format)
- bfd_format format;
-{
- if (((int)format <(int) bfd_unknown) || ((int)format >=(int) bfd_type_end)) return "invalid";
-
- switch (format) {
- case bfd_object: return "object"; /* linker/assember/compiler output */
- case bfd_archive: return "archive"; /* object archive file */
- case bfd_core: return "core"; /* core dump */
- default: return "unknown";
- }
-}
-\f
-/** Target configurations */
-
-extern bfd_target *target_vector[];
-
-/* Returns a pointer to the transfer vector for the object target
- named target_name. If target_name is NULL, chooses the one in the
- environment variable GNUTARGET; if that is null or not defined then
- the first entry in the target list is chosen. Passing in the
- string "default" or setting the environment variable to "default"
- will cause the first entry in the target list to be returned. */
-
-bfd_target *
-bfd_find_target (target_name)
- char *target_name;
-{
- bfd_target **target;
- extern char *getenv ();
- char *targname = (target_name ? target_name : getenv ("GNUTARGET"));
-
- /* This is safe; the vector cannot be null */
- if (targname == NULL || !strcmp (targname, "default"))
- return target_vector[0];
-
- for (target = &target_vector[0]; *target != NULL; target++) {
- if (!strcmp (targname, (*target)->name))
- return *target;
- }
-
- bfd_error = invalid_target;
- return NULL;
-}
-
-/* Returns a freshly-consed, NULL-terminated vector of the names of all the
- valid bfd targets. Do not modify the names */
-
-char **
-bfd_target_list ()
-{
- int vec_length= 0;
- bfd_target **target;
- char **name_list, **name_ptr;
-
- for (target = &target_vector[0]; *target != NULL; target++)
- vec_length++;
-
- name_ptr = name_list = (char **) zalloc ((vec_length + 1) * sizeof (char **));
-
- if (name_list == NULL) {
- bfd_error = no_memory;
- return NULL;
- }
-
- for (target = &target_vector[0]; *target != NULL; target++)
- *(name_ptr++) = (*target)->name;
-
- return name_list;
-}
-\f
-/** Init a bfd for read of the proper format.
- */
-
-/* We should be able to find out if the target was defaulted or user-specified.
- If the user specified the target explicitly then we should do no search.
- I guess the best way to do this is to pass an extra argument which specifies
- the DWIM. */
-
-/* I have chanegd this always to set the filepos to the origin before
- guessing. -- Gumby, 14 Februar 1991*/
-
-boolean
-bfd_check_format (abfd, format)
- bfd *abfd;
- bfd_format format;
-{
-#if obsolete
- file_ptr filepos;
-#endif
- bfd_target **target, *save_targ, *right_targ;
- int match_count;
-
- if (!bfd_read_p (abfd) ||
- ((int)(abfd->format) < (int)bfd_unknown) ||
- ((int)(abfd->format) >= (int)bfd_type_end)) {
- bfd_error = invalid_operation;
- return false;
- }
-
- if (abfd->format != bfd_unknown) return (abfd->format == format) ? true:false;
-
- /* presume the answer is yes */
- abfd->format = format;
-
-#if obsolete
- filepos = bfd_tell (abfd);
-#endif
- bfd_seek (abfd, (file_ptr)0, SEEK_SET); /* instead, rewind! */
-
-
- right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
- if (right_targ) {
- abfd->xvec = right_targ; /* Set the target as returned */
- return true; /* File position has moved, BTW */
- }
-
- /* This isn't a <format> file in the specified or defaulted target type.
- See if we recognize it for any other target type. (We check them
- all to make sure it's uniquely recognized.) */
-
- save_targ = abfd->xvec;
- match_count = 0;
- right_targ = 0;
-
- for (target = target_vector; *target != NULL; target++) {
- bfd_target *temp;
-
- abfd->xvec = *target; /* Change BFD's target temporarily */
-#if obsolete
- bfd_seek (abfd, filepos, SEEK_SET); /* Restore original file position */
-#endif
- bfd_seek (abfd, (file_ptr)0, SEEK_SET);
- temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
- if (temp) { /* This format checks out as ok! */
- right_targ = temp;
- match_count++;
- }
- }
-
- if (match_count == 1) {
- abfd->xvec = right_targ; /* Change BFD's target permanently */
- return true; /* File position has moved, BTW */
- }
-
- abfd->xvec = save_targ; /* Restore original target type */
- abfd->format = bfd_unknown; /* Restore original format */
- bfd_error = ((match_count == 0) ? file_not_recognized :
- file_ambiguously_recognized);
-#if obsolete
- bfd_seek (abfd, filepos, SEEK_SET); /* Restore original file position */
-#endif
- return false;
-}
-
-boolean
-bfd_set_format (abfd, format)
- bfd *abfd;
- bfd_format format;
-{
- file_ptr filepos;
-
- if (bfd_read_p (abfd) ||
- ((int)abfd->format < (int)bfd_unknown) ||
- ((int)abfd->format >= (int)bfd_type_end)) {
- bfd_error = invalid_operation;
- return false;
- }
-
- if (abfd->format != bfd_unknown) return (abfd->format == format) ? true:false;
-
- /* presume the answer is yes */
- abfd->format = format;
-
- filepos = bfd_tell (abfd);
-
- if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd))) {
- abfd->format = bfd_unknown;
- bfd_seek (abfd, filepos, SEEK_SET);
- return false;
- }
-
- return true;
-}
-\f
-/* Hack object and core file sections */
-
-sec_ptr
-bfd_get_section_by_name (abfd, name)
- bfd *abfd;
- char *name;
-{
- asection *sect;
-
- for (sect = abfd->sections; sect != NULL; sect = sect->next)
- if (!strcmp (sect->name, name)) return sect;
- return NULL;
-}
-
-/* If you try to create a section with a name which is already in use,
- returns the old section by that name instead. */
-sec_ptr
-bfd_make_section (abfd, name)
- bfd *abfd;
- char *name;
-{
- asection *newsect;
- asection ** prev = &abfd->sections;
- asection * sect = abfd->sections;
-
- if (abfd->output_has_begun) {
- bfd_error = invalid_operation;
- return NULL;
- }
-
- while (sect) {
- if (!strcmp(sect->name, name)) return sect;
- prev = §->next;
- sect = sect->next;
- }
-
- newsect = (asection *) zalloc (sizeof (asection));
- if (newsect == NULL) {
- bfd_error = no_memory;
- return NULL;
- }
-
- newsect->name = name;
- newsect->index = abfd->section_count++;
- newsect->flags = SEC_NO_FLAGS;
-
-#if ignore /* the compiler doesn't know that zalloc clears the storage */
- newsect->userdata = 0;
- newsect->next = (asection *)NULL;
- newsect->relocation = (arelent *)NULL;
- newsect->reloc_count = 0;
- newsect->line_filepos =0;
-#endif
- if (BFD_SEND (abfd, _new_section_hook, (abfd, newsect)) != true) {
- free (newsect);
- return NULL;
- }