+fragment <<EOF
+
+static bfd_size_type
+id_note_section_size (bfd *abfd ATTRIBUTE_UNUSED)
+{
+ const char *style = emit_note_gnu_build_id;
+ bfd_size_type size;
+
+ size = offsetof (Elf_External_Note, name[sizeof "GNU"]);
+ size = (size + 3) & -(bfd_size_type) 4;
+
+ if (!strcmp (style, "md5") || !strcmp (style, "uuid"))
+ size += 128 / 8;
+ else if (!strcmp (style, "sha1"))
+ size += 160 / 8;
+ else if (!strncmp (style, "0x", 2))
+ {
+ /* ID is in string form (hex). Convert to bits. */
+ const char *id = style + 2;
+ do
+ {
+ if (ISXDIGIT (id[0]) && ISXDIGIT (id[1]))
+ {
+ ++size;
+ id += 2;
+ }
+ else if (*id == '-' || *id == ':')
+ ++id;
+ else
+ {
+ size = 0;
+ break;
+ }
+ } while (*id != '\0');
+ }
+ else
+ size = 0;
+
+ return size;
+}
+
+static unsigned char
+read_hex (const char xdigit)
+{
+ if (ISDIGIT (xdigit))
+ return xdigit - '0';
+ if (ISUPPER (xdigit))
+ return xdigit - 'A' + 0xa;
+ if (ISLOWER (xdigit))
+ return xdigit - 'a' + 0xa;
+ abort ();
+ return 0;
+}
+
+static bfd_boolean
+write_build_id (bfd *abfd)
+{
+ const struct elf_backend_data *bed = get_elf_backend_data (abfd);
+ struct elf_obj_tdata *t = elf_tdata (abfd);
+ const char *style;
+ asection *asec;
+ Elf_Internal_Shdr *i_shdr;
+ unsigned char *contents, *id_bits;
+ bfd_size_type size;
+ file_ptr position;
+ Elf_External_Note *e_note;
+ typedef void (*sum_fn) (const void *, size_t, void *);
+
+ style = t->o->build_id.style;
+ asec = t->o->build_id.sec;
+ if (bfd_is_abs_section (asec->output_section))
+ {
+ einfo (_("%P: warning: .note.gnu.build-id section discarded,"
+ " --build-id ignored.\n"));
+ return TRUE;
+ }
+ i_shdr = &elf_section_data (asec->output_section)->this_hdr;
+
+ if (i_shdr->contents == NULL)
+ {
+ if (asec->contents == NULL)
+ asec->contents = (unsigned char *) xmalloc (asec->size);
+ contents = asec->contents;
+ }
+ else
+ contents = i_shdr->contents + asec->output_offset;
+
+ e_note = (Elf_External_Note *) contents;
+ size = offsetof (Elf_External_Note, name[sizeof "GNU"]);
+ size = (size + 3) & -(bfd_size_type) 4;
+ id_bits = contents + size;
+ size = asec->size - size;
+
+ bfd_h_put_32 (abfd, sizeof "GNU", &e_note->namesz);
+ bfd_h_put_32 (abfd, size, &e_note->descsz);
+ bfd_h_put_32 (abfd, NT_GNU_BUILD_ID, &e_note->type);
+ memcpy (e_note->name, "GNU", sizeof "GNU");
+
+ if (strcmp (style, "md5") == 0)
+ {
+ struct md5_ctx ctx;
+
+ md5_init_ctx (&ctx);
+ if (!bed->s->checksum_contents (abfd, (sum_fn) &md5_process_bytes, &ctx))
+ return FALSE;
+ md5_finish_ctx (&ctx, id_bits);
+ }
+ else if (strcmp (style, "sha1") == 0)
+ {
+ struct sha1_ctx ctx;
+
+ sha1_init_ctx (&ctx);
+ if (!bed->s->checksum_contents (abfd, (sum_fn) &sha1_process_bytes, &ctx))
+ return FALSE;
+ sha1_finish_ctx (&ctx, id_bits);
+ }
+ else if (strcmp (style, "uuid") == 0)
+ {
+ int n;
+ int fd = open ("/dev/urandom", O_RDONLY);
+ if (fd < 0)
+ return FALSE;
+ n = read (fd, id_bits, size);
+ close (fd);
+ if (n < (int) size)
+ return FALSE;
+ }
+ else if (strncmp (style, "0x", 2) == 0)
+ {
+ /* ID is in string form (hex). Convert to bits. */
+ const char *id = style + 2;
+ size_t n = 0;
+ do
+ {
+ if (ISXDIGIT (id[0]) && ISXDIGIT (id[1]))
+ {
+ id_bits[n] = read_hex (*id++) << 4;
+ id_bits[n++] |= read_hex (*id++);
+ }
+ else if (*id == '-' || *id == ':')
+ ++id;
+ else
+ abort (); /* Should have been validated earlier. */
+ } while (*id != '\0');
+ }
+ else
+ abort (); /* Should have been validated earlier. */
+
+ position = i_shdr->sh_offset + asec->output_offset;
+ size = asec->size;
+ return (bfd_seek (abfd, position, SEEK_SET) == 0
+ && bfd_bwrite (contents, size, abfd) == size);
+}
+
+/* Make .note.gnu.build-id section, and set up elf_tdata->build_id. */
+
+static bfd_boolean
+setup_build_id (bfd *ibfd)
+{
+ asection *s;
+ bfd_size_type size;
+ flagword flags;
+
+ size = id_note_section_size (ibfd);
+ if (size == 0)
+ {
+ einfo ("%P: warning: unrecognized --build-id style ignored.\n");
+ return FALSE;
+ }
+
+ flags = (SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY
+ | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA);
+ s = bfd_make_section_with_flags (ibfd, ".note.gnu.build-id", flags);
+ if (s != NULL && bfd_set_section_alignment (ibfd, s, 2))
+ {
+ struct elf_obj_tdata *t = elf_tdata (link_info.output_bfd);
+ t->o->build_id.after_write_object_contents = &write_build_id;
+ t->o->build_id.style = emit_note_gnu_build_id;
+ t->o->build_id.sec = s;
+ elf_section_type (s) = SHT_NOTE;
+ s->size = size;
+ return TRUE;
+ }
+
+ einfo ("%P: warning: Cannot create .note.gnu.build-id section,"
+ " --build-id ignored.\n");
+ return FALSE;
+}