+static void
+v850_set_note (bfd * abfd, asection * s, enum v850_notes note, unsigned int val)
+{
+ bfd_byte * data = s->contents + ((note - 1) * SIZEOF_V850_NOTE);
+
+ bfd_put_32 (abfd, 4, data + 0);
+ bfd_put_32 (abfd, 4, data + 4);
+ bfd_put_32 (abfd, note, data + 8);
+ memcpy (data + 12, V850_NOTE_NAME, 4);
+ bfd_put_32 (abfd, val, data + 16);
+}
+
+/* Create the note section if not already present. This is done early so
+ that the linker maps the sections to the right place in the output. */
+
+static asection *
+v850_elf_make_note_section (bfd * abfd)
+{
+ asection *s;
+ bfd_byte *data;
+ flagword flags;
+ enum v850_notes id;
+
+ /* Make the note section. */
+ flags = SEC_READONLY | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_MERGE;
+
+ s = bfd_make_section_anyway_with_flags (abfd, V850_NOTE_SECNAME, flags);
+ if (s == NULL)
+ return NULL;
+
+ if (!bfd_set_section_alignment (s, 2))
+ return NULL;
+
+ /* Allocate space for all known notes. */
+ if (!bfd_set_section_size (s, NUM_V850_NOTES * SIZEOF_V850_NOTE))
+ return NULL;
+
+ data = bfd_zalloc (abfd, NUM_V850_NOTES * SIZEOF_V850_NOTE);
+ if (data == NULL)
+ return NULL;
+
+ s->contents = data;
+
+ /* Provide default (= uninitilaised) values for all of the notes. */
+ for (id = V850_NOTE_ALIGNMENT; id <= NUM_V850_NOTES; id++)
+ v850_set_note (abfd, s, id, 0);
+
+ return s;
+}
+
+/* Create the note section if not already present. This is done early so
+ that the linker maps the sections to the right place in the output. */
+
+bfd_boolean
+v850_elf_create_sections (struct bfd_link_info * info)
+{
+ bfd * ibfd;
+
+ /* If we already have a note section, do not make another. */
+ for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
+ if (bfd_get_section_by_name (ibfd, V850_NOTE_SECNAME) != NULL)
+ return TRUE;
+
+ return v850_elf_make_note_section (info->input_bfds) != NULL;
+}
+
+bfd_boolean
+v850_elf_set_note (bfd * abfd, enum v850_notes note, unsigned int val)
+{
+ asection * notes = bfd_get_section_by_name (abfd, V850_NOTE_SECNAME);
+
+ if (val > 2)
+ /* At the moment, no known note has a value over 2. */
+ return FALSE;
+
+ if (notes == NULL)
+ notes = v850_elf_make_note_section (abfd);
+ if (notes == NULL)
+ return FALSE;
+
+ v850_set_note (abfd, notes, note, val);
+ return TRUE;
+}
+
+/* Copy a v850 note section from one object module to another. */
+
+static void
+v850_elf_copy_notes (bfd *ibfd, bfd *obfd)
+{
+ asection * onotes;
+ asection * inotes;
+
+ /* If the output bfd does not have a note section, then
+ skip the merge. The normal input to output section
+ copying will take care of everythng for us. */
+ if ((onotes = bfd_get_section_by_name (obfd, V850_NOTE_SECNAME)) == NULL)
+ return;
+
+ if ((inotes = bfd_get_section_by_name (ibfd, V850_NOTE_SECNAME)) == NULL)
+ return;
+
+ if (bfd_section_size (inotes) == bfd_section_size (onotes))
+ {
+ bfd_byte * icont;
+ bfd_byte * ocont;
+
+ if ((icont = elf_section_data (inotes)->this_hdr.contents) == NULL)
+ BFD_ASSERT (bfd_malloc_and_get_section (ibfd, inotes, & icont));
+
+ if ((ocont = elf_section_data (onotes)->this_hdr.contents) == NULL)
+ /* If the output is being stripped then it is possible for
+ the notes section to disappear. In this case do nothing. */
+ return;
+
+ /* Copy/overwrite notes from the input to the output. */
+ memcpy (ocont, icont, bfd_section_size (onotes));
+ }
+}
+
+/* Copy backend specific data from one object module to another. */
+
+static bfd_boolean
+v850_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
+{
+ v850_elf_copy_notes (ibfd, obfd);
+ return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
+}
+#define bfd_elf32_bfd_copy_private_bfd_data v850_elf_copy_private_bfd_data
+
+static bfd_boolean
+v850_elf_merge_notes (bfd * ibfd, bfd *obfd)
+{
+ asection * onotes;
+ asection * inotes;
+ bfd_boolean result = TRUE;
+
+ /* If the output bfd does not have a note section, then
+ skip the merge. The normal input to output section
+ copying will take care of everythng for us. */
+ if ((onotes = bfd_get_section_by_name (obfd, V850_NOTE_SECNAME)) == NULL)
+ return TRUE;
+
+ if ((inotes = bfd_get_section_by_name (ibfd, V850_NOTE_SECNAME)) != NULL)
+ {
+ enum v850_notes id;
+ bfd_byte * icont;
+ bfd_byte * ocont;
+
+ BFD_ASSERT (bfd_section_size (inotes) == bfd_section_size (onotes));
+
+ if ((icont = elf_section_data (inotes)->this_hdr.contents) == NULL)
+ BFD_ASSERT (bfd_malloc_and_get_section (ibfd, inotes, & icont));
+
+ if ((ocont = elf_section_data (onotes)->this_hdr.contents) == NULL)
+ BFD_ASSERT (bfd_malloc_and_get_section (obfd, onotes, & ocont));
+
+ for (id = V850_NOTE_ALIGNMENT; id <= NUM_V850_NOTES; id++)
+ {
+ unsigned int ival;
+ unsigned int oval;
+ bfd_byte * idata = icont + ((id - 1) * SIZEOF_V850_NOTE) + 16;
+ bfd_byte * odata = ocont + ((id - 1) * SIZEOF_V850_NOTE) + 16;
+
+ ival = bfd_get_32 (ibfd, idata);
+ oval = bfd_get_32 (obfd, odata);
+
+ if (ival == 0 || ival == oval)
+ continue;
+
+ if (oval == 0)
+ {
+ bfd_put_32 (obfd, ival, odata);
+ v850_set_note (obfd, onotes, id, ival);
+ continue;
+ }
+
+ /* We have a mismatch. The ABI defines how to handle
+ this siutation on a per note type basis. */
+ switch (id)
+ {
+ case V850_NOTE_ALIGNMENT:
+ if (oval == EF_RH850_DATA_ALIGN4)
+ {
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("error: %pB needs 8-byte alignment but %pB is set for 4-byte alignment"),
+ ibfd, obfd);
+ result = FALSE;
+ }
+ else
+ /* ibfd uses 4-byte alignment, obfd uses 8-byte alignment.
+ Leave the obfd alignment as it is. */
+ BFD_ASSERT (oval == EF_RH850_DATA_ALIGN8);
+
+ break;
+
+ case V850_NOTE_DATA_SIZE:
+ if (oval == EF_RH850_DOUBLE32)
+ {
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("error: %pB uses 64-bit doubles but "
+ "%pB uses 32-bit doubles"), ibfd, obfd);
+ result = FALSE;
+ }
+ else
+ /* ibfd uses 32-bit doubles, obfd uses 64-bit doubles.
+ This is acceptable. Honest, that is what the ABI says. */
+ BFD_ASSERT (oval == EF_RH850_DOUBLE64);
+ break;
+
+ case V850_NOTE_FPU_INFO:
+ if (oval == EF_RH850_FPU20)
+ {
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("error: %pB uses FPU-3.0 but %pB only supports FPU-2.0"),
+ ibfd, obfd);
+ result = FALSE;
+ }
+ else
+ /* ibfd uses FPU-2.0, obfd uses FPU-3.0. Leave obfd as it is. */
+ BFD_ASSERT (oval == EF_RH850_FPU30);
+
+ break;
+
+ default:
+ /* None of the other conflicts matter.
+ Stick with the current output values. */
+ break;
+ }
+ }
+
+ /* FIXME: We should also check for conflicts between the notes
+ and the EF flags in the ELF header. */
+ }
+
+ return result;
+}
+
+static void
+print_v850_note (bfd * abfd, FILE * file, bfd_byte * data, enum v850_notes id)
+{
+ unsigned int value = bfd_get_32 (abfd, data + ((id - 1) * SIZEOF_V850_NOTE) + 16);
+
+ switch (id)
+ {
+ case V850_NOTE_ALIGNMENT:
+ fprintf (file, _(" alignment of 8-byte entities: "));
+ switch (value)
+ {
+ case EF_RH850_DATA_ALIGN4: fprintf (file, _("4-byte")); break;
+ case EF_RH850_DATA_ALIGN8: fprintf (file, _("8-byte")); break;
+ case 0: fprintf (file, _("not set")); break;
+ default: fprintf (file, _("unknown: %x"), value); break;
+ }
+ fputc ('\n', file);
+ break;
+
+ case V850_NOTE_DATA_SIZE:
+ fprintf (file, _(" size of doubles: "));
+ switch (value)
+ {
+ case EF_RH850_DOUBLE32: fprintf (file, _("4-bytes")); break;
+ case EF_RH850_DOUBLE64: fprintf (file, _("8-bytes")); break;
+ case 0: fprintf (file, _("not set")); break;
+ default: fprintf (file, _("unknown: %x"), value); break;
+ }
+ fputc ('\n', file);
+ break;
+
+ case V850_NOTE_FPU_INFO:
+ fprintf (file, _(" FPU support required: "));
+ switch (value)
+ {
+ case EF_RH850_FPU20: fprintf (file, _("FPU-2.0")); break;
+ case EF_RH850_FPU30: fprintf (file, _("FPU-3.0")); break;
+ case 0: fprintf (file, _("none")); break;
+ default: fprintf (file, _("unknown: %x"), value); break;
+ }
+ fputc ('\n', file);
+ break;
+
+ case V850_NOTE_SIMD_INFO:
+ fprintf (file, _("SIMD use: "));
+ switch (value)
+ {
+ case EF_RH850_SIMD: fprintf (file, _("yes")); break;
+ case 0: fprintf (file, _("no")); break;
+ default: fprintf (file, _("unknown: %x"), value); break;
+ }
+ fputc ('\n', file);
+ break;
+
+ case V850_NOTE_CACHE_INFO:
+ fprintf (file, _("CACHE use: "));
+ switch (value)
+ {
+ case EF_RH850_CACHE: fprintf (file, _("yes")); break;
+ case 0: fprintf (file, _("no")); break;
+ default: fprintf (file, _("unknown: %x"), value); break;
+ }
+ fputc ('\n', file);
+ break;
+
+ case V850_NOTE_MMU_INFO:
+ fprintf (file, _("MMU use: "));
+ switch (value)
+ {
+ case EF_RH850_MMU: fprintf (file, _("yes")); break;
+ case 0: fprintf (file, _("no")); break;
+ default: fprintf (file, _("unknown: %x"), value); break;
+ }
+ fputc ('\n', file);
+ break;
+
+ default:
+ BFD_ASSERT (0);
+ }
+}
+
+static void
+v850_elf_print_notes (bfd * abfd, FILE * file)
+{
+ asection * notes = bfd_get_section_by_name (abfd, V850_NOTE_SECNAME);
+ enum v850_notes id;
+
+ if (notes == NULL || notes->contents == NULL)
+ return;
+
+ BFD_ASSERT (bfd_section_size (notes) == NUM_V850_NOTES * SIZEOF_V850_NOTE);
+
+ for (id = V850_NOTE_ALIGNMENT; id <= NUM_V850_NOTES; id++)
+ print_v850_note (abfd, file, notes->contents, id);
+}
+