From 61e2488cd8497d158303a78563ad40f51f5c3f8e Mon Sep 17 00:00:00 2001 From: Jon TURNEY Date: Tue, 8 Apr 2014 10:59:43 +0100 Subject: Add support for generating and inserting build IDs into COFF binaries. * peXXigen.c (pe_print_debugdata): New function: Displays the contents of the debug directory and decodes codeview entries. (_bfd_XXi_swap_debugdir_in, _bfd_XXi_swap_debugdir_out) (_bfd_XXi_slurp_codeview_record, _bfd_XXi_write_codeview_record): Add functions for reading and writing debugdir and codeview records. * libpei.h (_bfd_XXi_swap_debugdir_in, _bfd_XXi_swap_debugdir_out) (_bfd_XXi_write_codeview_record): Add prototypes and macros. * libcoff-in.h (pe_tdata): Add build-id data. * libcoff.h: Regenerate. * coffcode.h (coff_write_object_contents): Run build_id after_write_object_contents hook. * pe.h (external_IMAGE_DEBUG_DIRECTORY, _CV_INFO_PDB70) (_CV_INFO_PDB20): Add structures and constants for debug directory and codeview records. * internal.h (internal_IMAGE_DEBUG_DIRECTORY, CODEVIEW_INFO): Add structures and constants for internal representation of debug directory and codeview records. * emultempl/elf32.em (id_note_section_size, read_hex, write_build_id): Move code for parsing build-id option and calculating the build-id to... * ldbuildid.c: New file. * ldbuildid.h: New file. * Makefile.am (CFILES, HFILES, OFILES, ld_new_SOURCES): Add new files. * Makefile.in: Regenerate. * ld.texinfo: Update --build-id description to mention COFF support. * NEWS: Mention support for COFF build ids. * emultempl/pe.em (gld${EMULATION_NAME}_handle_option): (pecoff_checksum_contents, write_build_id, setup_build_id) (gld_${EMULATION_NAME}_after_open): Handle and implement build-id option. * emultempl/pep.em: Likewise. --- ld/ChangeLog | 18 +++ ld/Makefile.am | 10 +- ld/Makefile.in | 13 +- ld/NEWS | 4 + ld/emultempl/elf32.em | 96 +------------- ld/emultempl/pe.em | 233 ++++++++++++++++++++++++++++++++-- ld/emultempl/pep.em | 234 +++++++++++++++++++++++++++++++++-- ld/ld.texinfo | 20 +-- ld/ldbuildid.c | 158 +++++++++++++++++++++++ ld/ldbuildid.h | 39 ++++++ ld/ldmain.c | 7 ++ ld/testsuite/ld-pe/longsecn-3.d | 3 +- ld/testsuite/ld-pe/longsecn-4.d | 3 +- ld/testsuite/ld-pe/longsecn-5.d | 3 +- ld/testsuite/ld-pe/non-c-lang-syms.s | 1 - ld/testsuite/ld-pe/orphana_nu.s | 2 + 16 files changed, 702 insertions(+), 142 deletions(-) create mode 100644 ld/ldbuildid.c create mode 100644 ld/ldbuildid.h (limited to 'ld') diff --git a/ld/ChangeLog b/ld/ChangeLog index 0a02f90..cdba250 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,21 @@ +2014-04-08 Jon TURNEY + + * emultempl/elf32.em (id_note_section_size, read_hex, write_build_id): + Move code for parsing build-id option and calculating the build-id to... + * ldbuildid.c: New file. + * ldbuildid.h: New file. + * Makefile.am (CFILES, HFILES, OFILES, ld_new_SOURCES): Add new + files. + * Makefile.in: Regenerate. + * ld.texinfo: Update --build-id description to mention COFF + support. + * NEWS: Mention support for COFF build ids. + * emultempl/pe.em (gld${EMULATION_NAME}_handle_option): + (pecoff_checksum_contents, write_build_id, setup_build_id) + (gld_${EMULATION_NAME}_after_open): Handle and implement + build-id option. + * emultempl/pep.em: Likewise. + 2014-04-04 Cary Coutant PR gold/16804 diff --git a/ld/Makefile.am b/ld/Makefile.am index 795663f..3e2dc1a 100644 --- a/ld/Makefile.am +++ b/ld/Makefile.am @@ -496,12 +496,12 @@ ALL_EMUL_EXTRA_BINARIES = CFILES = ldctor.c ldemul.c ldexp.c ldfile.c ldlang.c \ ldmain.c ldmisc.c ldver.c ldwrite.c lexsup.c \ mri.c ldcref.c pe-dll.c pep-dll.c ldlex-wrapper.c \ - $(PLUGIN_C) + $(PLUGIN_C) ldbuildid.c HFILES = ld.h ldctor.h ldemul.h ldexp.h ldfile.h \ ldlang.h ldlex.h ldmain.h ldmisc.h ldver.h \ ldwrite.h mri.h deffile.h pe-dll.h pep-dll.h \ - elf-hints-local.h $(PLUGIN_H) + elf-hints-local.h $(PLUGIN_H) ldbuildid.h GENERATED_CFILES = ldgram.c ldlex.c deffilep.c GENERATED_HFILES = ldgram.h ldemul-list.h deffilep.h @@ -513,7 +513,8 @@ BUILT_SOURCES = $(GENERATED_HFILES) OFILES = ldgram.@OBJEXT@ ldlex-wrapper.@OBJEXT@ lexsup.@OBJEXT@ ldlang.@OBJEXT@ \ mri.@OBJEXT@ ldctor.@OBJEXT@ ldmain.@OBJEXT@ $(PLUGIN_OBJECT) \ ldwrite.@OBJEXT@ ldexp.@OBJEXT@ ldemul.@OBJEXT@ ldver.@OBJEXT@ ldmisc.@OBJEXT@ \ - ldfile.@OBJEXT@ ldcref.@OBJEXT@ ${EMULATION_OFILES} ${EMUL_EXTRA_OFILES} + ldfile.@OBJEXT@ ldcref.@OBJEXT@ ${EMULATION_OFILES} ${EMUL_EXTRA_OFILES} \ + ldbuildid.@OBJEXT@ STAGESTUFF = *.@OBJEXT@ ldscripts/* e*.c @@ -1935,7 +1936,8 @@ EXTRA_ld_new_SOURCES = deffilep.y ldlex.l EXTRA_ld_new_SOURCES += pep-dll.c pe-dll.c ld_new_SOURCES = ldgram.y ldlex-wrapper.c lexsup.c ldlang.c mri.c ldctor.c ldmain.c \ - ldwrite.c ldexp.c ldemul.c ldver.c ldmisc.c ldfile.c ldcref.c $(PLUGIN_C) + ldwrite.c ldexp.c ldemul.c ldver.c ldmisc.c ldfile.c ldcref.c $(PLUGIN_C) \ + ldbuildid.c ld_new_DEPENDENCIES = $(EMULATION_OFILES) $(EMUL_EXTRA_OFILES) $(EMUL_EXTRA_BINARIES) \ $(BFDLIB) $(LIBIBERTY) $(LIBINTL_DEP) ld_new_LDADD = $(EMULATION_OFILES) $(EMUL_EXTRA_OFILES) $(BFDLIB) $(LIBIBERTY) $(LIBINTL) diff --git a/ld/Makefile.in b/ld/Makefile.in index 3c9f8f4..4f54c26 100644 --- a/ld/Makefile.in +++ b/ld/Makefile.in @@ -109,7 +109,7 @@ am_ld_new_OBJECTS = ldgram.$(OBJEXT) ldlex-wrapper.$(OBJEXT) \ ldctor.$(OBJEXT) ldmain.$(OBJEXT) ldwrite.$(OBJEXT) \ ldexp.$(OBJEXT) ldemul.$(OBJEXT) ldver.$(OBJEXT) \ ldmisc.$(OBJEXT) ldfile.$(OBJEXT) ldcref.$(OBJEXT) \ - $(am__objects_1) + $(am__objects_1) ldbuildid.$(OBJEXT) ld_new_OBJECTS = $(am_ld_new_OBJECTS) am__DEPENDENCIES_1 = DEFAULT_INCLUDES = -I.@am__isrc@ @@ -800,12 +800,12 @@ ALL_EMUL_EXTRA_BINARIES = CFILES = ldctor.c ldemul.c ldexp.c ldfile.c ldlang.c \ ldmain.c ldmisc.c ldver.c ldwrite.c lexsup.c \ mri.c ldcref.c pe-dll.c pep-dll.c ldlex-wrapper.c \ - $(PLUGIN_C) + $(PLUGIN_C) ldbuildid.c HFILES = ld.h ldctor.h ldemul.h ldexp.h ldfile.h \ ldlang.h ldlex.h ldmain.h ldmisc.h ldver.h \ ldwrite.h mri.h deffile.h pe-dll.h pep-dll.h \ - elf-hints-local.h $(PLUGIN_H) + elf-hints-local.h $(PLUGIN_H) ldbuildid.h GENERATED_CFILES = ldgram.c ldlex.c deffilep.c GENERATED_HFILES = ldgram.h ldemul-list.h deffilep.h @@ -816,7 +816,8 @@ BUILT_SOURCES = $(GENERATED_HFILES) OFILES = ldgram.@OBJEXT@ ldlex-wrapper.@OBJEXT@ lexsup.@OBJEXT@ ldlang.@OBJEXT@ \ mri.@OBJEXT@ ldctor.@OBJEXT@ ldmain.@OBJEXT@ $(PLUGIN_OBJECT) \ ldwrite.@OBJEXT@ ldexp.@OBJEXT@ ldemul.@OBJEXT@ ldver.@OBJEXT@ ldmisc.@OBJEXT@ \ - ldfile.@OBJEXT@ ldcref.@OBJEXT@ ${EMULATION_OFILES} ${EMUL_EXTRA_OFILES} + ldfile.@OBJEXT@ ldcref.@OBJEXT@ ${EMULATION_OFILES} ${EMUL_EXTRA_OFILES} \ + ldbuildid.@OBJEXT@ STAGESTUFF = *.@OBJEXT@ ldscripts/* e*.c @@ -838,7 +839,8 @@ ELF_GEN_DEPS = $(srcdir)/emultempl/generic.em $(srcdir)/emultempl/elf-generic.em EXTRA_ld_new_SOURCES = deffilep.y ldlex.l pep-dll.c pe-dll.c \ $(ALL_EMULATION_SOURCES) $(ALL_64_EMULATION_SOURCES) ld_new_SOURCES = ldgram.y ldlex-wrapper.c lexsup.c ldlang.c mri.c ldctor.c ldmain.c \ - ldwrite.c ldexp.c ldemul.c ldver.c ldmisc.c ldfile.c ldcref.c $(PLUGIN_C) + ldwrite.c ldexp.c ldemul.c ldver.c ldmisc.c ldfile.c ldcref.c $(PLUGIN_C) \ + ldbuildid.c ld_new_DEPENDENCIES = $(EMULATION_OFILES) $(EMUL_EXTRA_OFILES) $(EMUL_EXTRA_BINARIES) \ $(BFDLIB) $(LIBIBERTY) $(LIBINTL_DEP) @@ -1365,6 +1367,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ez80.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ez8001.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ez8002.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ldbuildid.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ldcref.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ldctor.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ldemul.Po@am__quote@ diff --git a/ld/NEWS b/ld/NEWS index 94d086e..a9124b6 100644 --- a/ld/NEWS +++ b/ld/NEWS @@ -1,5 +1,9 @@ -*- text -*- +* Add support for the --build-id command line option to COFF based targets. + +* x86/x86_64 pe-coff now supports the --build-id option. + * Add support for the Andes NDS32. Changes in 2.24: diff --git a/ld/emultempl/elf32.em b/ld/emultempl/elf32.em index de460a2..0173b66 100644 --- a/ld/emultempl/elf32.em +++ b/ld/emultempl/elf32.em @@ -39,10 +39,7 @@ fragment < #include "bfdlink.h" @@ -54,6 +51,7 @@ fragment < #include "elf/common.h" #include "elf-bfd.h" @@ -895,53 +893,20 @@ id_note_section_size (bfd *abfd ATTRIBUTE_UNUSED) { const char *style = emit_note_gnu_build_id; bfd_size_type size; + bfd_size_type build_id_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'); - } + build_id_size = compute_build_id_size (style); + if (build_id_size) + size += build_id_size; 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) { @@ -954,7 +919,6 @@ write_build_id (bfd *abfd) 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; @@ -986,55 +950,7 @@ write_build_id (bfd *abfd) 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. */ + generate_build_id (abfd, style, bed->s->checksum_contents, id_bits, size); position = i_shdr->sh_offset + asec->output_offset; size = asec->size; diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em index c18cb26..ba51cc0 100644 --- a/ld/emultempl/pe.em +++ b/ld/emultempl/pe.em @@ -66,6 +66,7 @@ fragment <sections; asec != NULL; asec = asec->next) + { + struct bfd_link_order *l = NULL; + for (l = asec->map_head.link_order; l != NULL; l = l->next) + { + if ((l->type == bfd_indirect_link_order)) + { + if (l->u.indirect.section == t->build_id.sec) + { + link_order = l; + break; + } + } + } + + if (link_order) + break; + } + + if (!link_order) + { + einfo (_("%P: warning: .build-id section discarded," + " --build-id ignored.\n")); + return TRUE; + } + + if (t->build_id.sec->contents == NULL) + t->build_id.sec->contents = (unsigned char *) xmalloc (t->build_id.sec->size); + contents = t->build_id.sec->contents; + size = t->build_id.sec->size; + + build_id_size = compute_build_id_size (t->build_id.style); + build_id = xmalloc (build_id_size); + generate_build_id (abfd, t->build_id.style, pecoff_checksum_contents, build_id, build_id_size); + + bfd_vma ib = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase; + + /* Construct a debug directory entry which points to an immediately following CodeView record. */ + struct internal_IMAGE_DEBUG_DIRECTORY idd; + idd.Characteristics = 0; + idd.TimeDateStamp = 0; + idd.MajorVersion = 0; + idd.MinorVersion = 0; + idd.Type = PE_IMAGE_DEBUG_TYPE_CODEVIEW; + idd.SizeOfData = sizeof (CV_INFO_PDB70) + 1; + idd.AddressOfRawData = asec->vma - ib + link_order->offset + + sizeof (struct external_IMAGE_DEBUG_DIRECTORY); + idd.PointerToRawData = asec->filepos + link_order->offset + + sizeof (struct external_IMAGE_DEBUG_DIRECTORY); + + struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *)contents; + _bfd_XXi_swap_debugdir_out (abfd, &idd, ext); + + /* Write the debug directory entry. */ + if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0) + return 0; + + if ((bfd_bwrite (contents, size, abfd) != size)) + return 0; + + /* Construct the CodeView record. */ + CODEVIEW_INFO cvinfo; + cvinfo.CVSignature = CVINFO_PDB70_CVSIGNATURE; + cvinfo.Age = 1; + + /* Zero pad or truncate the generated build_id to fit in the CodeView record. */ + memset (&(cvinfo.Signature), 0, CV_INFO_SIGNATURE_LENGTH); + memcpy (&(cvinfo.Signature), build_id, (build_id_size > CV_INFO_SIGNATURE_LENGTH) + ? CV_INFO_SIGNATURE_LENGTH : build_id_size); + + free (build_id); + + /* Write the codeview record. */ + if (_bfd_XXi_write_codeview_record (abfd, idd.PointerToRawData, &cvinfo) == 0) + return 0; + + /* Record the location of the debug directory in the data directory. */ + pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress + = asec->vma - ib + link_order->offset; + pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size + = sizeof (struct external_IMAGE_DEBUG_DIRECTORY); + + return TRUE; +} + +/* Make .build-id section, and set up coff_tdata->build_id. */ +static bfd_boolean +setup_build_id (bfd *ibfd) +{ + asection *s; + flagword flags; + + if (!validate_build_id_style (emit_build_id)) + { + einfo ("%P: warning: unrecognized --build-id style ignored.\n"); + return FALSE; + } + + flags = (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY + | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA); + s = bfd_make_section_anyway_with_flags (ibfd, ".build-id", flags); + if (s != NULL) + { + struct pe_tdata *t = pe_data (link_info.output_bfd); + t->build_id.after_write_object_contents = &write_build_id; + t->build_id.style = emit_build_id; + t->build_id.sec = s; + + /* Section is a fixed size: + One IMAGE_DEBUG_DIRECTORY entry, of type IMAGE_DEBUG_TYPE_CODEVIEW, + pointing at a CV_INFO_PDB70 record containing the build-id, with a + null byte for PdbFileName. */ + s->size = sizeof (struct external_IMAGE_DEBUG_DIRECTORY) + + sizeof (CV_INFO_PDB70) + 1; + + return TRUE; + } + + einfo ("%P: warning: Cannot create .build-id section," + " --build-id ignored.\n"); + return FALSE; +} + static void gld_${EMULATION_NAME}_after_open (void) { @@ -1257,6 +1438,26 @@ gld_${EMULATION_NAME}_after_open (void) } #endif + if (emit_build_id != NULL) + { + bfd *abfd; + + /* Find a COFF input. */ + for (abfd = link_info.input_bfds; + abfd != (bfd *) NULL; abfd = abfd->link_next) + if (bfd_get_flavour (abfd) == bfd_target_coff_flavour) + break; + + /* If there are no COFF input files do not try to + add a build-id section. */ + if (abfd == NULL + || !setup_build_id (abfd)) + { + free ((char *) emit_build_id); + emit_build_id = NULL; + } + } + /* Pass the wacky PE command line options into the output bfd. FIXME: This should be done via a function, rather than by including an internal BFD header. */ @@ -1279,17 +1480,23 @@ gld_${EMULATION_NAME}_after_open (void) find it, so enable it in that case. */ if (pe_use_coff_long_section_names < 0 && link_info.strip == strip_none) { - /* Iterate over all sections of all input BFDs, checking - for any that begin 'debug_' and are long names. */ - LANG_FOR_EACH_INPUT_STATEMENT (is) + if (link_info.relocatable) + pe_use_coff_long_section_names = 1; + else { - int found_debug = 0; - bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug); - if (found_debug) - { - pe_use_coff_long_section_names = 1; - break; - } + /* Iterate over all sections of all input BFDs, checking + for any that begin 'debug_' and are long names. */ + LANG_FOR_EACH_INPUT_STATEMENT (is) + { + int found_debug = 0; + + bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug); + if (found_debug) + { + pe_use_coff_long_section_names = 1; + break; + } + } } } diff --git a/ld/emultempl/pep.em b/ld/emultempl/pep.em index dca36cc..d1575e2 100644 --- a/ld/emultempl/pep.em +++ b/ld/emultempl/pep.em @@ -64,6 +64,7 @@ fragment <name, sizeof (".debug_") - 1) == 0) *found = 1; } +static bfd_boolean +pecoff_checksum_contents (bfd *abfd, + void (*process) (const void *, size_t, void *), + void *arg) +{ + file_ptr filepos = (file_ptr) 0; + + while (1) + { + unsigned char b; + int status; + + if (bfd_seek (abfd, filepos, SEEK_SET) != 0) + return 0; + + status = bfd_bread (&b, (bfd_size_type) 1, abfd); + if (status < 1) + { + break; + } + + (*process) (&b, 1, arg); + filepos += 1; + } + + return TRUE; +} + +static bfd_boolean +write_build_id (bfd *abfd) +{ + struct pe_tdata *t = pe_data (abfd); + asection *asec; + struct bfd_link_order *link_order = NULL; + unsigned char *contents; + bfd_size_type size; + bfd_size_type build_id_size; + unsigned char *build_id; + + /* Find the section the .build-id output section has been merged info. */ + for (asec = abfd->sections; asec != NULL; asec = asec->next) + { + struct bfd_link_order *l = NULL; + for (l = asec->map_head.link_order; l != NULL; l = l->next) + { + if ((l->type == bfd_indirect_link_order)) + { + if (l->u.indirect.section == t->build_id.sec) + { + link_order = l; + break; + } + } + } + + if (link_order) + break; + } + + if (!link_order) + { + einfo (_("%P: warning: .build-id section discarded," + " --build-id ignored.\n")); + return TRUE; + } + + if (t->build_id.sec->contents == NULL) + t->build_id.sec->contents = (unsigned char *) xmalloc (t->build_id.sec->size); + contents = t->build_id.sec->contents; + size = t->build_id.sec->size; + + build_id_size = compute_build_id_size (t->build_id.style); + build_id = xmalloc (build_id_size); + generate_build_id (abfd, t->build_id.style, pecoff_checksum_contents, build_id, build_id_size); + + bfd_vma ib = pe_data (link_info.output_bfd)->pe_opthdr.ImageBase; + + /* Construct a debug directory entry which points to an immediately following CodeView record. */ + struct internal_IMAGE_DEBUG_DIRECTORY idd; + idd.Characteristics = 0; + idd.TimeDateStamp = 0; + idd.MajorVersion = 0; + idd.MinorVersion = 0; + idd.Type = PE_IMAGE_DEBUG_TYPE_CODEVIEW; + idd.SizeOfData = sizeof (CV_INFO_PDB70) + 1; + idd.AddressOfRawData = asec->vma - ib + link_order->offset + + sizeof (struct external_IMAGE_DEBUG_DIRECTORY); + idd.PointerToRawData = asec->filepos + link_order->offset + + sizeof (struct external_IMAGE_DEBUG_DIRECTORY); + + struct external_IMAGE_DEBUG_DIRECTORY *ext = (struct external_IMAGE_DEBUG_DIRECTORY *)contents; + _bfd_XXi_swap_debugdir_out (abfd, &idd, ext); + + /* Write the debug directory enttry */ + if (bfd_seek (abfd, asec->filepos + link_order->offset, SEEK_SET) != 0) + return 0; + + if ((bfd_bwrite (contents, size, abfd) != size)) + return 0; + + /* Construct the CodeView record. */ + CODEVIEW_INFO cvinfo; + cvinfo.CVSignature = CVINFO_PDB70_CVSIGNATURE; + cvinfo.Age = 1; + + /* Zero pad or truncate the generated build_id to fit in the CodeView record. */ + memset (&(cvinfo.Signature), 0, CV_INFO_SIGNATURE_LENGTH); + memcpy (&(cvinfo.Signature), build_id, (build_id_size > CV_INFO_SIGNATURE_LENGTH) + ? CV_INFO_SIGNATURE_LENGTH : build_id_size); + + free (build_id); + + /* Write the codeview record. */ + if (_bfd_XXi_write_codeview_record (abfd, idd.PointerToRawData, &cvinfo) == 0) + return 0; + + /* Record the location of the debug directory in the data directory. */ + pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress + = asec->vma - ib + link_order->offset; + pe_data (link_info.output_bfd)->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size + = sizeof (struct external_IMAGE_DEBUG_DIRECTORY); + + return TRUE; +} + +/* Make .build-id section, and set up coff_tdata->build_id. */ +static bfd_boolean +setup_build_id (bfd *ibfd) +{ + asection *s; + flagword flags; + + if (!validate_build_id_style (emit_build_id)) + { + einfo ("%P: warning: unrecognized --build-id style ignored.\n"); + return FALSE; + } + + flags = (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_IN_MEMORY + | SEC_LINKER_CREATED | SEC_READONLY | SEC_DATA); + s = bfd_make_section_anyway_with_flags (ibfd, ".build-id", flags); + if (s != NULL) + { + struct pe_tdata *t = pe_data (link_info.output_bfd); + t->build_id.after_write_object_contents = &write_build_id; + t->build_id.style = emit_build_id; + t->build_id.sec = s; + + /* Section is a fixed size: + One IMAGE_DEBUG_DIRECTORY entry, of type IMAGE_DEBUG_TYPE_CODEVIEW, + pointing at a CV_INFO_PDB70 record containing the build-id, with a + null byte for PdbFileName. */ + s->size = sizeof (struct external_IMAGE_DEBUG_DIRECTORY) + + sizeof (CV_INFO_PDB70) + 1; + + return TRUE; + } + + einfo ("%P: warning: Cannot create .build-id section," + " --build-id ignored.\n"); + return FALSE; +} + static void gld_${EMULATION_NAME}_after_open (void) { @@ -1214,6 +1396,26 @@ gld_${EMULATION_NAME}_after_open (void) } #endif + if (emit_build_id != NULL) + { + bfd *abfd; + + /* Find a COFF input. */ + for (abfd = link_info.input_bfds; + abfd != (bfd *) NULL; abfd = abfd->link_next) + if (bfd_get_flavour (abfd) == bfd_target_coff_flavour) + break; + + /* If there are no COFF input files do not try to + add a build-id section. */ + if (abfd == NULL + || !setup_build_id (abfd)) + { + free ((char *) emit_build_id); + emit_build_id = NULL; + } + } + /* Pass the wacky PE command line options into the output bfd. FIXME: This should be done via a function, rather than by including an internal BFD header. */ @@ -1236,17 +1438,23 @@ gld_${EMULATION_NAME}_after_open (void) find it, so enable it in that case. */ if (pep_use_coff_long_section_names < 0 && link_info.strip == strip_none) { - /* Iterate over all sections of all input BFDs, checking - for any that begin 'debug_' and are long names. */ - LANG_FOR_EACH_INPUT_STATEMENT (is) + if (link_info.relocatable) + pep_use_coff_long_section_names = 1; + else { - int found_debug = 0; - bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug); - if (found_debug) - { - pep_use_coff_long_section_names = 1; - break; - } + /* Iterate over all sections of all input BFDs, checking + for any that begin 'debug_' and are long names. */ + LANG_FOR_EACH_INPUT_STATEMENT (is) + { + int found_debug = 0; + + bfd_map_over_sections (is->the_bfd, debug_section_p, &found_debug); + if (found_debug) + { + pep_use_coff_long_section_names = 1; + break; + } + } } } diff --git a/ld/ld.texinfo b/ld/ld.texinfo index 62d8aa7..7d2de3b 100644 --- a/ld/ld.texinfo +++ b/ld/ld.texinfo @@ -2160,16 +2160,16 @@ enable other tradeoffs in future versions of the linker. @kindex --build-id=@var{style} @item --build-id @itemx --build-id=@var{style} -Request creation of @code{.note.gnu.build-id} ELF note section. -The contents of the note are unique bits identifying this linked -file. @var{style} can be @code{uuid} to use 128 random bits, -@code{sha1} to use a 160-bit @sc{SHA1} hash on the normative -parts of the output contents, @code{md5} to use a 128-bit -@sc{MD5} hash on the normative parts of the output contents, or -@code{0x@var{hexstring}} to use a chosen bit string specified as -an even number of hexadecimal digits (@code{-} and @code{:} -characters between digit pairs are ignored). If @var{style} is -omitted, @code{sha1} is used. +Request the creation of a @code{.note.gnu.build-id} ELF note section +or a @code{.build-id} COFF section. The contents of the note are +unique bits identifying this linked file. @var{style} can be +@code{uuid} to use 128 random bits, @code{sha1} to use a 160-bit +@sc{SHA1} hash on the normative parts of the output contents, +@code{md5} to use a 128-bit @sc{MD5} hash on the normative parts of +the output contents, or @code{0x@var{hexstring}} to use a chosen bit +string specified as an even number of hexadecimal digits (@code{-} and +@code{:} characters between digit pairs are ignored). If @var{style} +is omitted, @code{sha1} is used. The @code{md5} and @code{sha1} styles produces an identifier that is always the same in an identical output file, but will be diff --git a/ld/ldbuildid.c b/ld/ldbuildid.c new file mode 100644 index 0000000..1214789 --- /dev/null +++ b/ld/ldbuildid.c @@ -0,0 +1,158 @@ +/* ldbuildid.c - Build Id support routines + Copyright 2013, 2014 Free Software Foundation, Inc. + + This file is part of the GNU Binutils. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, + MA 02110-1301, USA. */ + +#include "sysdep.h" +#include "bfd.h" +#include "safe-ctype.h" +#include "md5.h" +#include "sha1.h" +#include "ldbuildid.h" + +#define streq(a,b) strcmp ((a), (b)) == 0 +#define strneq(a,b,n) strncmp ((a), (b), (n)) == 0 + +bfd_boolean +validate_build_id_style (const char *style) +{ + if ((streq (style, "md5")) || (streq (style, "sha1")) +#ifndef __MINGW32__ + || (streq (style, "uuid")) +#endif + || (strneq (style, "0x", 2))) + return TRUE; + + return FALSE; +} + +bfd_size_type +compute_build_id_size (const char *style) +{ + if (streq (style, "md5") || streq (style, "uuid")) + return 128 / 8; + + if (streq (style, "sha1")) + return 160 / 8; + + if (strneq (style, "0x", 2)) + { + bfd_size_type size = 0; + /* ID is in string form (hex). Count the bytes. */ + 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'); + return size; + } + + return 0; +} + +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; +} + +bfd_boolean +generate_build_id (bfd *abfd, + const char *style, + checksum_fn checksum_contents, + unsigned char *id_bits, + int size) +{ + if (streq (style, "md5")) + { + struct md5_ctx ctx; + + md5_init_ctx (&ctx); + if (!(*checksum_contents) (abfd, (sum_fn) &md5_process_bytes, &ctx)) + return FALSE; + md5_finish_ctx (&ctx, id_bits); + } + else if (streq (style, "sha1")) + { + struct sha1_ctx ctx; + + sha1_init_ctx (&ctx); + if (!(*checksum_contents) (abfd, (sum_fn) &sha1_process_bytes, &ctx)) + return FALSE; + sha1_finish_ctx (&ctx, id_bits); + } +#ifndef __MINGW32__ + else if (streq (style, "uuid")) + { + int n; + int fd = open ("/dev/urandom", O_RDONLY); + + if (fd < 0) + return FALSE; + n = read (fd, id_bits, size); + close (fd); + if (n < size) + return FALSE; + } +#endif + else if (strneq (style, "0x", 2)) + { + /* 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. */ + + return TRUE; +} diff --git a/ld/ldbuildid.h b/ld/ldbuildid.h new file mode 100644 index 0000000..a91ac1a --- /dev/null +++ b/ld/ldbuildid.h @@ -0,0 +1,39 @@ +/* ldbuildid.h - + Copyright 2013, 2014 Free Software Foundation, Inc. + + This file is part of the GNU Binutils. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, + MA 02110-1301, USA. */ + +#ifndef LDBUILDID_H +#define LDBUILDID_H + +extern bfd_boolean +validate_build_id_style (const char *); + +extern bfd_size_type +compute_build_id_size (const char *); + +typedef void (*sum_fn) (const void *, size_t, void *); + +typedef bfd_boolean (*checksum_fn) (bfd *, + void (*) (const void *, size_t, void *), + void *); + +extern bfd_boolean +generate_build_id (bfd *, const char *, checksum_fn, unsigned char *, int); + +#endif /* LDBUILDID_H */ diff --git a/ld/ldmain.c b/ld/ldmain.c index 14253a6..b132dae 100644 --- a/ld/ldmain.c +++ b/ld/ldmain.c @@ -378,6 +378,13 @@ main (int argc, char **argv) lang_final (); + /* If the only command line argument has been -v or --version or --verbose + then ignore any input files provided by linker scripts and exit now. + We do not want to create an output file when the linker is just invoked + to provide version information. */ + if (argc == 2 && version_printed) + xexit (0); + if (!lang_has_input_file) { if (version_printed || command_line.print_output_format) diff --git a/ld/testsuite/ld-pe/longsecn-3.d b/ld/testsuite/ld-pe/longsecn-3.d index 0317be3..c86a828 100644 --- a/ld/testsuite/ld-pe/longsecn-3.d +++ b/ld/testsuite/ld-pe/longsecn-3.d @@ -37,5 +37,4 @@ Idx Name Size VMA +LMA +File off Algn CONTENTS, ALLOC, LOAD, DATA 14 \.rodata\.very\.long\.section\$1234 [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ 2\*\*[0-9] CONTENTS, ALLOC, LOAD, DATA - 15 \.(bss |text) [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ 2\*\*[0-9] - (ALLOC|CONTENTS, ALLOC, LOAD, (READONLY, )?CODE) +#... diff --git a/ld/testsuite/ld-pe/longsecn-4.d b/ld/testsuite/ld-pe/longsecn-4.d index 565ef38..e326d98 100644 --- a/ld/testsuite/ld-pe/longsecn-4.d +++ b/ld/testsuite/ld-pe/longsecn-4.d @@ -37,5 +37,4 @@ Idx Name Size VMA +LMA +File off Algn CONTENTS, ALLOC, LOAD, DATA 14 \.rodata\. [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ 2\*\*[0-9] CONTENTS, ALLOC, LOAD, DATA - 15 \.(bss |text) [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ 2\*\*[0-9] - (ALLOC|CONTENTS, ALLOC, LOAD, (READONLY, )?CODE) +#... diff --git a/ld/testsuite/ld-pe/longsecn-5.d b/ld/testsuite/ld-pe/longsecn-5.d index 82d94b8..f3ef22b 100644 --- a/ld/testsuite/ld-pe/longsecn-5.d +++ b/ld/testsuite/ld-pe/longsecn-5.d @@ -37,5 +37,4 @@ Idx Name Size VMA +LMA +File off Algn CONTENTS, ALLOC, LOAD, DATA 14 \.rodata\.very\.long\.section\$1234 [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ 2\*\*[0-9] CONTENTS, ALLOC, LOAD, DATA - 15 \.(bss |text) [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ [0-9a-fA-F]+ 2\*\*[0-9] - (ALLOC|CONTENTS, ALLOC, LOAD, (READONLY, )?CODE) +#... diff --git a/ld/testsuite/ld-pe/non-c-lang-syms.s b/ld/testsuite/ld-pe/non-c-lang-syms.s index e849d9e..28006a1 100644 --- a/ld/testsuite/ld-pe/non-c-lang-syms.s +++ b/ld/testsuite/ld-pe/non-c-lang-syms.s @@ -1,4 +1,3 @@ - main: _main: nop diff --git a/ld/testsuite/ld-pe/orphana_nu.s b/ld/testsuite/ld-pe/orphana_nu.s index d3c564f..618789c 100644 --- a/ld/testsuite/ld-pe/orphana_nu.s +++ b/ld/testsuite/ld-pe/orphana_nu.s @@ -1,6 +1,8 @@ + .globl _mainCRTStartup .globl mainCRTStartup .globl start .text +_mainCRTStartup: mainCRTStartup: start: -- cgit v1.1