+ return TRUE;
+}
+
+const bfd_target *
+nlm_object_p (bfd *abfd)
+{
+ struct nlm_obj_tdata *preserved_tdata = nlm_tdata (abfd);
+ bfd_boolean (*backend_object_p) (bfd *);
+ void * x_fxdhdr = NULL;
+ Nlm_Internal_Fixed_Header *i_fxdhdrp;
+ struct nlm_obj_tdata *new_tdata = NULL;
+ const char *signature;
+ enum bfd_architecture arch;
+ bfd_size_type amt;
+
+ /* Some NLM formats have a prefix before the standard NLM fixed
+ header. */
+ backend_object_p = nlm_backend_object_p_func (abfd);
+ if (backend_object_p)
+ {
+ if (!(*backend_object_p) (abfd))
+ goto got_wrong_format_error;
+ }
+
+ /* Read in the fixed length portion of the NLM header in external format. */
+ amt = nlm_fixed_header_size (abfd);
+ x_fxdhdr = bfd_malloc (amt);
+ if (x_fxdhdr == NULL)
+ goto got_no_match;
+
+ if (bfd_bread ((void *) x_fxdhdr, amt, abfd) != amt)
+ {
+ if (bfd_get_error () != bfd_error_system_call)
+ goto got_wrong_format_error;
+ else
+ goto got_no_match;
+ }
+
+ /* Allocate an instance of the nlm_obj_tdata structure and hook it up to
+ the tdata pointer in the bfd. */
+ amt = sizeof (struct nlm_obj_tdata);
+ new_tdata = bfd_zalloc (abfd, amt);
+ if (new_tdata == NULL)
+ goto got_no_match;
+
+ nlm_tdata (abfd) = new_tdata;
+
+ i_fxdhdrp = nlm_fixed_header (abfd);
+ nlm_swap_fixed_header_in (abfd, x_fxdhdr, i_fxdhdrp);
+ free (x_fxdhdr);
+ x_fxdhdr = NULL;
+
+ /* Check to see if we have an NLM file for this backend by matching
+ the NLM signature. */
+ signature = nlm_signature (abfd);
+ if (signature != NULL
+ && *signature != '\0'
+ && strncmp ((char *) i_fxdhdrp->signature, signature,
+ NLM_SIGNATURE_SIZE) != 0)
+ goto got_wrong_format_error;
+
+ /* There's no supported way to discover the endianness of an NLM, so test for
+ a sane version number after doing byte swapping appropriate for this
+ XVEC. (Hack alert!) */
+ if (i_fxdhdrp->version > 0xFFFF)
+ goto got_wrong_format_error;
+
+ /* There's no supported way to check for 32 bit versus 64 bit addresses,
+ so ignore this distinction for now. (FIXME) */
+ /* Swap in the rest of the required header. */
+ if (!nlm_swap_variable_header_in (abfd))
+ {
+ if (bfd_get_error () != bfd_error_system_call)
+ goto got_wrong_format_error;
+ else
+ goto got_no_match;
+ }
+
+ /* Add the sections supplied by all NLM's, and then read in the
+ auxiliary headers. Reading the auxiliary headers may create
+ additional sections described in the cygnus_ext header.
+ From this point on we assume that we have an NLM, and do not
+ treat errors as indicating the wrong format. */
+ if (!add_bfd_section (abfd, NLM_CODE_NAME,
+ i_fxdhdrp->codeImageOffset,
+ i_fxdhdrp->codeImageSize,
+ (SEC_CODE | SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
+ | SEC_RELOC))
+ || !add_bfd_section (abfd, NLM_INITIALIZED_DATA_NAME,
+ i_fxdhdrp->dataImageOffset,
+ i_fxdhdrp->dataImageSize,
+ (SEC_DATA | SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
+ | SEC_RELOC))
+ || !add_bfd_section (abfd, NLM_UNINITIALIZED_DATA_NAME,
+ (file_ptr) 0,
+ i_fxdhdrp->uninitializedDataSize,
+ SEC_ALLOC))
+ goto got_no_match;
+
+ if (!nlm_swap_auxiliary_headers_in (abfd))
+ goto got_no_match;
+
+ if (nlm_fixed_header (abfd)->numberOfRelocationFixups != 0
+ || nlm_fixed_header (abfd)->numberOfExternalReferences != 0)
+ abfd->flags |= HAS_RELOC;
+ if (nlm_fixed_header (abfd)->numberOfPublics != 0
+ || nlm_fixed_header (abfd)->numberOfDebugRecords != 0
+ || nlm_fixed_header (abfd)->numberOfExternalReferences != 0)
+ abfd->flags |= HAS_SYMS;
+
+ arch = nlm_architecture (abfd);
+ if (arch != bfd_arch_unknown)
+ bfd_default_set_arch_mach (abfd, arch, (unsigned long) 0);
+
+ abfd->flags |= EXEC_P;
+ bfd_get_start_address (abfd) = nlm_fixed_header (abfd)->codeStartOffset;
+
+ return abfd->xvec;
+
+got_wrong_format_error:
+ bfd_set_error (bfd_error_wrong_format);
+got_no_match:
+ nlm_tdata (abfd) = preserved_tdata;
+ if (new_tdata != NULL)
+ bfd_release (abfd, new_tdata);
+ if (x_fxdhdr != NULL)
+ free (x_fxdhdr);
+
+ return NULL;
+}
+
+/* Swap and write out the variable length header. All the fields must
+ exist in the NLM, and must exist in this order. */
+
+static bfd_boolean
+nlm_swap_variable_header_out (bfd *abfd)
+{
+ bfd_byte temp[NLM_TARGET_LONG_SIZE];
+ bfd_size_type amt;
+
+ /* Write the description length and text members. */
+ amt = sizeof (nlm_variable_header (abfd)->descriptionLength);
+ if (bfd_bwrite (& nlm_variable_header (abfd)->descriptionLength, amt,
+ abfd) != amt)
+ return FALSE;
+ amt = nlm_variable_header (abfd)->descriptionLength + 1;
+ if (bfd_bwrite ((void *) nlm_variable_header (abfd)->descriptionText, amt,
+ abfd) != amt)
+ return FALSE;
+
+ /* Convert and write the stackSize field. */
+ put_word (abfd, (bfd_vma) nlm_variable_header (abfd)->stackSize, temp);
+ amt = sizeof (temp);
+ if (bfd_bwrite (temp, amt, abfd) != amt)
+ return FALSE;
+
+ /* Convert and write the reserved field. */
+ put_word (abfd, (bfd_vma) nlm_variable_header (abfd)->reserved, temp);
+ amt = sizeof (temp);
+ if (bfd_bwrite (temp, amt, abfd) != amt)
+ return FALSE;
+
+ /* Write the oldThreadName field. This field is a fixed length string. */
+ amt = sizeof (nlm_variable_header (abfd)->oldThreadName);
+ if (bfd_bwrite (nlm_variable_header (abfd)->oldThreadName, amt,
+ abfd) != amt)
+ return FALSE;
+
+ /* Write the screen name length and text members. */
+ amt = sizeof (nlm_variable_header (abfd)->screenNameLength);
+ if (bfd_bwrite (& nlm_variable_header (abfd)->screenNameLength, amt,
+ abfd) != amt)
+ return FALSE;
+ amt = nlm_variable_header (abfd)->screenNameLength + 1;
+ if (bfd_bwrite (nlm_variable_header (abfd)->screenName, amt, abfd) != amt)
+ return FALSE;
+
+ /* Write the thread name length and text members. */
+ amt = sizeof (nlm_variable_header (abfd)->threadNameLength);
+ if (bfd_bwrite (& nlm_variable_header (abfd)->threadNameLength, amt,
+ abfd) != amt)
+ return FALSE;
+ amt = nlm_variable_header (abfd)->threadNameLength + 1;
+ if (bfd_bwrite (nlm_variable_header (abfd)->threadName, amt, abfd) != amt)
+ return FALSE;
+ return TRUE;