From 1dd1bc4daf99ec7ad1f9a6feea8cf4ebe715a92d Mon Sep 17 00:00:00 2001 From: Omar Majid Date: Tue, 23 Jul 2019 09:44:57 +0100 Subject: [PATCH] Allow objdump to recognise the System.Runtime.dll files that get shipped with .NET Core 2.1. include * coff/i386.h (IMAGE_FILE_MACHINE_NATIVE_APPLE_OVERRIDE): Define. (IMAGE_FILE_MACHINE_NATIVE_FREEBSD_OVERRIDE): Define. (IMAGE_FILE_MACHINE_NATIVE_LINUX_OVERRIDE): Define. (IMAGE_FILE_MACHINE_NATIVE_NETBSD_OVERRIDE): Define. (I386_APPLE_MAGIC): Define. (I386_FREEBSD_MAGIC): Define. (I386_LINUX_MAGIC): Define. (I386_NETBSD_MAGIC): Define. (I386BADMAG): Extend macro to allow new magic numbers. * coff/x86_64.h (IMAGE_FILE_MACHINE_NATIVE_APPLE_OVERRIDE): Define. (IMAGE_FILE_MACHINE_NATIVE_FREEBSD_OVERRIDE): Define. (IMAGE_FILE_MACHINE_NATIVE_LINUX_OVERRIDE): Define. (IMAGE_FILE_MACHINE_NATIVE_NETBSD_OVERRIDE): Define. (AMD64_APPLE_MAGIC): Define. (AMD64_FREEBSD_MAGIC): Define. (AMD64_LINUX_MAGIC): Define. (AMD64_NETBSD_MAGIC): Define. (AMD64BADMAG): Extend macro to allow new magic numbers. bfd * coffcode.h (coff_set_arch_mach_hook): Handle I386_APPLE_MAGIC, I386_FREEBSD_MAGIC, I386_LINUX_MAGIC, I386_NETBSD_MAGIC, AMD64_APPLE_MAGIC, AMD64_FREEBSD_MAGIC, AMD64_LINUX_MAGIC, AMD64_NETBSD_MAGIC. * peXXigen.c: Add comment about source of .NET magic numbers. binutils* Makefile.am (AUTOMAKE_OPTIONS): Add subdir-objects (GENTESTDLLSPROG): Define. (TEST_PROGS): Add GENTESTDLLSPROG. * Makefile.in: Regenerate. * testsuite/binutils-all/objdump.exp (test_objdump_dotnet_assemblies): New proc. Run the new proc. * testsuite/gentestdlls.c: New source file. --- bfd/ChangeLog | 8 ++ bfd/coffcode.h | 8 ++ bfd/peXXigen.c | 7 + binutils/ChangeLog | 11 ++ binutils/Makefile.am | 5 +- binutils/Makefile.in | 56 ++++++-- binutils/testsuite/binutils-all/objdump.exp | 43 ++++++ binutils/testsuite/gentestdlls.c | 152 ++++++++++++++++++++ include/ChangeLog | 21 +++ include/coff/i386.h | 19 +++ include/coff/x86_64.h | 22 ++- 11 files changed, 335 insertions(+), 17 deletions(-) create mode 100644 binutils/testsuite/gentestdlls.c diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 62e4beb66e..2beeb3dfe1 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,11 @@ +2019-07-23 Omar Majid + + * coffcode.h (coff_set_arch_mach_hook): Handle I386_APPLE_MAGIC, + I386_FREEBSD_MAGIC, I386_LINUX_MAGIC, I386_NETBSD_MAGIC, + AMD64_APPLE_MAGIC, AMD64_FREEBSD_MAGIC, AMD64_LINUX_MAGIC, + AMD64_NETBSD_MAGIC. + * peXXigen.c: Add comment about source of .NET magic numbers. + 2019-07-19 Alan Modra * reloc.c (BFD_RELOC_PPC64_TPREL34, BFD_RELOC_PPC64_DTPREL34), diff --git a/bfd/coffcode.h b/bfd/coffcode.h index c67bfbb0e6..c3f13300a4 100644 --- a/bfd/coffcode.h +++ b/bfd/coffcode.h @@ -2107,11 +2107,19 @@ coff_set_arch_mach_hook (bfd *abfd, void * filehdr) case I386PTXMAGIC: case I386AIXMAGIC: /* Danbury PS/2 AIX C Compiler. */ case LYNXCOFFMAGIC: + case I386_APPLE_MAGIC: + case I386_FREEBSD_MAGIC: + case I386_LINUX_MAGIC: + case I386_NETBSD_MAGIC: arch = bfd_arch_i386; break; #endif #ifdef AMD64MAGIC case AMD64MAGIC: + case AMD64_APPLE_MAGIC: + case AMD64_FREEBSD_MAGIC: + case AMD64_LINUX_MAGIC: + case AMD64_NETBSD_MAGIC: arch = bfd_arch_i386; machine = bfd_mach_x86_64; break; diff --git a/bfd/peXXigen.c b/bfd/peXXigen.c index 77fb4933e7..ee6da6480d 100644 --- a/bfd/peXXigen.c +++ b/bfd/peXXigen.c @@ -35,6 +35,13 @@ "Peering Inside the PE: A Tour of the Win32 Portable Executable File Format", MSJ 1994, Volume 9. + The PE/PEI format is also used by .NET. ECMA-335 describes this: + + "Standard ECMA-335 Common Language Infrastructure (CLI)", 6th Edition, June 2012. + + This is also available at + https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdf. + The *sole* difference between the pe format and the pei format is that the latter has an MSDOS 2.0 .exe header on the front that prints the message "This app must be run under Windows." (or some such). diff --git a/binutils/ChangeLog b/binutils/ChangeLog index d7231713e7..5238b47026 100644 --- a/binutils/ChangeLog +++ b/binutils/ChangeLog @@ -1,3 +1,14 @@ +2019-07-23 Omar Majid + + * Makefile.am (AUTOMAKE_OPTIONS): Add subdir-objects + (GENTESTDLLSPROG): Define. + (TEST_PROGS): Add GENTESTDLLSPROG. + * Makefile.in: Regenerate. + * testsuite/binutils-all/objdump.exp + (test_objdump_dotnet_assemblies): New proc. + Run the new proc. + * testsuite/gentestdlls.c: New source file. + 2019-06-28 Nick Clifton PR 24707 diff --git a/binutils/Makefile.am b/binutils/Makefile.am index 128494ca9e..de93ffa040 100644 --- a/binutils/Makefile.am +++ b/binutils/Makefile.am @@ -17,7 +17,7 @@ # . # -AUTOMAKE_OPTIONS = dejagnu no-dist foreign +AUTOMAKE_OPTIONS = dejagnu no-dist foreign subdir-objects ACLOCAL_AMFLAGS = -I .. -I ../config -I ../bfd SUBDIRS = doc po @@ -93,8 +93,9 @@ EXTRA_SCRIPTS = embedspu ## Test programs. BFDTEST1_PROG = bfdtest1 BFDTEST2_PROG = bfdtest2 +GENTESTDLLS_PROG = testsuite/gentestdlls -TEST_PROGS = $(BFDTEST1_PROG) $(BFDTEST2_PROG) +TEST_PROGS = $(BFDTEST1_PROG) $(BFDTEST2_PROG) $(GENTESTDLLS_PROG) ## We need a special rule to install the programs which are built with ## -new, and to rename cxxfilt to c++filt. diff --git a/binutils/Makefile.in b/binutils/Makefile.in index 13b3b19932..16c078fdd3 100644 --- a/binutils/Makefile.in +++ b/binutils/Makefile.in @@ -113,7 +113,7 @@ bin_PROGRAMS = $(am__EXEEXT_5) $(am__EXEEXT_6) $(am__EXEEXT_7) \ @BUILD_SRCONV@ @BUILD_DLLTOOL@ @BUILD_WINDRES@ @BUILD_WINDMC@ \ $(am__EXEEXT_11) $(am__EXEEXT_12) $(am__EXEEXT_13) \ @BUILD_DLLWRAP@ $(am__empty) -noinst_PROGRAMS = $(am__EXEEXT_17) $(am__EXEEXT_20) @BUILD_MISC@ +noinst_PROGRAMS = $(am__EXEEXT_17) $(am__EXEEXT_21) @BUILD_MISC@ EXTRA_PROGRAMS = srconv$(EXEEXT) sysdump$(EXEEXT) coffdump$(EXEEXT) \ $(am__EXEEXT_1) $(am__EXEEXT_2) $(am__EXEEXT_3) \ $(am__EXEEXT_4) @@ -169,7 +169,8 @@ am__EXEEXT_16 = cxxfilt$(EXEEXT) am__EXEEXT_17 = $(am__EXEEXT_14) $(am__EXEEXT_15) $(am__EXEEXT_16) am__EXEEXT_18 = bfdtest1$(EXEEXT) am__EXEEXT_19 = bfdtest2$(EXEEXT) -am__EXEEXT_20 = $(am__EXEEXT_18) $(am__EXEEXT_19) +am__EXEEXT_20 = testsuite/gentestdlls$(EXEEXT) +am__EXEEXT_21 = $(am__EXEEXT_18) $(am__EXEEXT_19) $(am__EXEEXT_20) PROGRAMS = $(bin_PROGRAMS) $(noinst_PROGRAMS) am__objects_1 = bucomm.$(OBJEXT) version.$(OBJEXT) filemode.$(OBJEXT) am_addr2line_OBJECTS = addr2line.$(OBJEXT) $(am__objects_1) @@ -243,6 +244,12 @@ strip_new_LDADD = $(LDADD) am_sysdump_OBJECTS = sysdump.$(OBJEXT) $(am__objects_1) sysdump_OBJECTS = $(am_sysdump_OBJECTS) sysdump_LDADD = $(LDADD) +testsuite_gentestdlls_SOURCES = testsuite/gentestdlls.c +am__dirstamp = $(am__leading_dot)dirstamp +testsuite_gentestdlls_OBJECTS = testsuite/gentestdlls.$(OBJEXT) +testsuite_gentestdlls_LDADD = $(LDADD) +testsuite_gentestdlls_DEPENDENCIES = $(BFDLIB) $(LIBIBERTY) \ + $(am__DEPENDENCIES_1) am_windmc_OBJECTS = windmc.$(OBJEXT) mcparse.$(OBJEXT) mclex.$(OBJEXT) \ winduni.$(OBJEXT) $(am__objects_1) windmc_OBJECTS = $(am_windmc_OBJECTS) @@ -338,8 +345,8 @@ SOURCES = $(addr2line_SOURCES) $(ar_SOURCES) $(EXTRA_ar_SOURCES) \ $(nm_new_SOURCES) $(objcopy_SOURCES) $(objdump_SOURCES) \ $(EXTRA_objdump_SOURCES) $(ranlib_SOURCES) $(readelf_SOURCES) \ $(size_SOURCES) $(srconv_SOURCES) $(strings_SOURCES) \ - $(strip_new_SOURCES) $(sysdump_SOURCES) $(windmc_SOURCES) \ - $(windres_SOURCES) + $(strip_new_SOURCES) $(sysdump_SOURCES) \ + testsuite/gentestdlls.c $(windmc_SOURCES) $(windres_SOURCES) RECURSIVE_TARGETS = all-recursive check-recursive cscopelist-recursive \ ctags-recursive dvi-recursive html-recursive info-recursive \ install-data-recursive install-dvi-recursive \ @@ -548,7 +555,7 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ zlibdir = @zlibdir@ zlibinc = @zlibinc@ -AUTOMAKE_OPTIONS = dejagnu no-dist foreign +AUTOMAKE_OPTIONS = dejagnu no-dist foreign subdir-objects ACLOCAL_AMFLAGS = -I .. -I ../config -I ../bfd SUBDIRS = doc po tooldir = $(exec_prefix)/$(target_alias) @@ -595,7 +602,8 @@ bin_SCRIPTS = @BUILD_INSTALL_MISC@ EXTRA_SCRIPTS = embedspu BFDTEST1_PROG = bfdtest1 BFDTEST2_PROG = bfdtest2 -TEST_PROGS = $(BFDTEST1_PROG) $(BFDTEST2_PROG) +GENTESTDLLS_PROG = testsuite/gentestdlls +TEST_PROGS = $(BFDTEST1_PROG) $(BFDTEST2_PROG) $(GENTESTDLLS_PROG) RENAMED_PROGS = $(NM_PROG) $(STRIP_PROG) $(DEMANGLER_PROG) # Stuff that goes in tooldir/ if appropriate. @@ -940,6 +948,18 @@ strip-new$(EXEEXT): $(strip_new_OBJECTS) $(strip_new_DEPENDENCIES) $(EXTRA_strip sysdump$(EXEEXT): $(sysdump_OBJECTS) $(sysdump_DEPENDENCIES) $(EXTRA_sysdump_DEPENDENCIES) @rm -f sysdump$(EXEEXT) $(AM_V_CCLD)$(LINK) $(sysdump_OBJECTS) $(sysdump_LDADD) $(LIBS) +testsuite/$(am__dirstamp): + @$(MKDIR_P) testsuite + @: > testsuite/$(am__dirstamp) +testsuite/$(DEPDIR)/$(am__dirstamp): + @$(MKDIR_P) testsuite/$(DEPDIR) + @: > testsuite/$(DEPDIR)/$(am__dirstamp) +testsuite/gentestdlls.$(OBJEXT): testsuite/$(am__dirstamp) \ + testsuite/$(DEPDIR)/$(am__dirstamp) + +testsuite/gentestdlls$(EXEEXT): $(testsuite_gentestdlls_OBJECTS) $(testsuite_gentestdlls_DEPENDENCIES) $(EXTRA_testsuite_gentestdlls_DEPENDENCIES) testsuite/$(am__dirstamp) + @rm -f testsuite/gentestdlls$(EXEEXT) + $(AM_V_CCLD)$(LINK) $(testsuite_gentestdlls_OBJECTS) $(testsuite_gentestdlls_LDADD) $(LIBS) mcparse.h: mcparse.c @if test ! -f $@; then rm -f mcparse.c; else :; fi @if test ! -f $@; then $(MAKE) $(AM_MAKEFLAGS) mcparse.c; else :; fi @@ -992,6 +1012,7 @@ uninstall-binSCRIPTS: mostlyclean-compile: -rm -f *.$(OBJEXT) + -rm -f testsuite/*.$(OBJEXT) distclean-compile: -rm -f *.tab.c @@ -1057,24 +1078,28 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/windres.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/winduni.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/wrstabs.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@testsuite/$(DEPDIR)/gentestdlls.Po@am__quote@ .c.o: -@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< -@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.o$$||'`;\ +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ $< &&\ +@am__fastdepCC_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Po @AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ $< .c.obj: -@am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` -@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.obj$$||'`;\ +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ `$(CYGPATH_W) '$<'` &&\ +@am__fastdepCC_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Po @AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c -o $@ `$(CYGPATH_W) '$<'` .c.lo: -@am__fastdepCC_TRUE@ $(AM_V_CC)$(LTCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< -@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo +@am__fastdepCC_TRUE@ $(AM_V_CC)depbase=`echo $@ | sed 's|[^/]*$$|$(DEPDIR)/&|;s|\.lo$$||'`;\ +@am__fastdepCC_TRUE@ $(LTCOMPILE) -MT $@ -MD -MP -MF $$depbase.Tpo -c -o $@ $< &&\ +@am__fastdepCC_TRUE@ $(am__mv) $$depbase.Tpo $$depbase.Plo @AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LTCOMPILE) -c -o $@ $< @@ -1090,6 +1115,7 @@ mostlyclean-libtool: clean-libtool: -rm -rf .libs _libs + -rm -rf testsuite/.libs testsuite/_libs distclean-libtool: -rm -f libtool config.lt @@ -1269,6 +1295,8 @@ clean-generic: distclean-generic: -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + -rm -f testsuite/$(DEPDIR)/$(am__dirstamp) + -rm -f testsuite/$(am__dirstamp) -test -z "$(DISTCLEANFILES)" || rm -f $(DISTCLEANFILES) maintainer-clean-generic: @@ -1292,7 +1320,7 @@ clean-am: clean-binPROGRAMS clean-generic clean-libtool \ distclean: distclean-recursive -rm -f $(am__CONFIG_DISTCLEAN_FILES) - -rm -rf ./$(DEPDIR) + -rm -rf ./$(DEPDIR) testsuite/$(DEPDIR) -rm -f Makefile distclean-am: clean-am distclean-DEJAGNU distclean-compile \ distclean-generic distclean-hdr distclean-libtool \ @@ -1342,7 +1370,7 @@ installcheck-am: installcheck-local maintainer-clean: maintainer-clean-recursive -rm -f $(am__CONFIG_DISTCLEAN_FILES) -rm -rf $(top_srcdir)/autom4te.cache - -rm -rf ./$(DEPDIR) + -rm -rf ./$(DEPDIR) testsuite/$(DEPDIR) -rm -f Makefile maintainer-clean-am: distclean-am maintainer-clean-generic diff --git a/binutils/testsuite/binutils-all/objdump.exp b/binutils/testsuite/binutils-all/objdump.exp index beaf44f954..b46fd0a8bb 100644 --- a/binutils/testsuite/binutils-all/objdump.exp +++ b/binutils/testsuite/binutils-all/objdump.exp @@ -732,6 +732,49 @@ if {[is_elf_format]} then { remote_file host delete $testfile3 } +# Test objdump on .NET assemblies (PE files) + +proc test_objdump_dotnet_assemblies {} { + global OBJDUMP + global base_dir + + set test "dotnet-assemblies" + + set got [binutils_run "$base_dir/testsuite/gentestdlls" "tmpdir"] + set want "wrote dotnet-linux-x86-64.dll" + if ![regexp $want $got] then { + unsupported "$test" + } + + set test "dotnet-assemblies (32-bit)" + set want "file format pei-i386" + set got [binutils_run $OBJDUMP "-x tmpdir/simple-i386.dll"] + if ![regexp $want $got] then { + if [regexp "file format not recognized" $got] then { + unsupported $test + } else { + fail "$test" + } + } else { + pass $test + } + + set test "dotnet-assemblies (64-bit)" + set want "file format pei-x86-64" + set got [binutils_run $OBJDUMP "-x tmpdir/dotnet-linux-x86-64.dll"] + if ![regexp $want $got] then { + if [regexp "file format not recognized" $got] then { + unsupported $test + } else { + fail "$test" + } + } else { + pass $test + } +} + +test_objdump_dotnet_assemblies + # Options which are not tested: -a -D -R -T -x -l --stabs # I don't see any generic way to test any of these other than -a. # Tests could be written for specific targets, and that should be done diff --git a/binutils/testsuite/gentestdlls.c b/binutils/testsuite/gentestdlls.c new file mode 100644 index 0000000000..b1463c0178 --- /dev/null +++ b/binutils/testsuite/gentestdlls.c @@ -0,0 +1,152 @@ +/* Copyright (C) 2019 Free Software Foundation, Inc. + + This file is part of 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. */ + + +/* This file generates a number of DLL (PE/COFF binaries traditionally + used on Windows) that we can then utilize in various tests to + ensure objdump can parse these file correctly. + + See: + https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdf */ + +#include +#include +#include +#include +#include + +static void +write_dos_header_and_stub (FILE* file) +{ + /* See ECMA-335 II.25.2.1. + Instead of lfanew, lets just hardcode the offset of the next byte + after this header (0x80). */ + char buffer[128] = + { + 0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, + 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, /* Last 4 bytes are precomputed lfanew. */ + 0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, + 0x21, 0xb8, 0x01, 0x4c, 0xcd, 0x21, 0x54, 0x68, + 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72, + 0x61, 0x6d, 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f, + 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6e, + 0x20, 0x69, 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20, + 0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x0d, 0x0d, 0x0a, + 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + fwrite (buffer, 1, 128, file); +} + +static void +write_pe_signature (FILE* file) +{ + char buffer[4]; + buffer[0] = 'P'; + buffer[1] = 'E'; + buffer[2] = 0; + buffer[3] = 0; + fwrite (buffer, 1, 4, file); +} + +static void +write_coff_header (FILE* file, uint16_t machine) +{ + char buffer[128]; + + memset (buffer, 0, sizeof (buffer)); + + /* Machine. ECMA-335 says this must be 0x14c but that's not true anymore. */ + buffer[0] = machine & 0xff; + buffer[1] = machine >> 0x8; + fwrite (buffer, 2, 1, file); + memset (buffer, 0, sizeof (buffer)); + /* NumberOfSections = 0 */ + fwrite (buffer, 2, 1, file); + /* TimeDateStamp = 0 */ + fwrite (buffer, 4, 1, file); + /* PointerToSymbolTable = 0 */ + fwrite (buffer, 4, 1, file); + /* NumberOfSymbols = 0 */ + fwrite (buffer, 4, 1, file); + /* OptionalHeaderSize = 0 */ + fwrite (buffer, 2, 1, file); + /* Characteristics = 0x2000 */ + buffer[0] = 0x00; + buffer[1] = 0x20; + fwrite (buffer, 2, 1, file); + memset (buffer, 0 , sizeof (buffer)); +} + +int +main (int argc, char** argv) +{ + FILE* file; + + if (argc < 2) + { + fprintf (stderr, "usage: %s output-directory\n", argv[0]); + exit (2); + } + if (chdir (argv[1]) != 0) + { + fprintf (stderr, "error: unable to change directory to %s\n", argv[0]); + exit (2); + } + + /* Generate a simple DLL file. */ + file = fopen ("simple-i386.dll", "w"); + if (file == NULL) + { + fprintf (stderr, "error: unable to open file for writing\n"); + exit (1); + } + + write_dos_header_and_stub (file); + write_pe_signature (file); + write_coff_header (file, 0x14c); + fclose (file); + printf ("wrote simple-i386.dll\n"); + + /* Generate a sample .NET Core on Linux dll file. As opposed to the + more common DLLs that contain bytecode (CIL/MSIL), many .NET Core + DLLs are pre-compiled for specific architectures and platforms. + See https://github.com/jbevain/cecil/issues/337 for an example of + this value being used in practice. */ + file = fopen ("dotnet-linux-x86-64.dll", "w"); + if (file == NULL) + { + fprintf (stderr, "error: unable to open file for writing\n"); + exit (1); + } + + write_dos_header_and_stub (file); + write_pe_signature (file); + write_coff_header (file, 0xfd1d /* x86-64 + Linux */); + fclose (file); + printf ("wrote dotnet-linux-x86-64.dll\n"); + + return 0; +} diff --git a/include/ChangeLog b/include/ChangeLog index bb5abb15d1..4c841e3595 100644 --- a/include/ChangeLog +++ b/include/ChangeLog @@ -1,3 +1,24 @@ +2019-07-23 Omar Majid + + * coff/i386.h (IMAGE_FILE_MACHINE_NATIVE_APPLE_OVERRIDE): Define. + (IMAGE_FILE_MACHINE_NATIVE_FREEBSD_OVERRIDE): Define. + (IMAGE_FILE_MACHINE_NATIVE_LINUX_OVERRIDE): Define. + (IMAGE_FILE_MACHINE_NATIVE_NETBSD_OVERRIDE): Define. + (I386_APPLE_MAGIC): Define. + (I386_FREEBSD_MAGIC): Define. + (I386_LINUX_MAGIC): Define. + (I386_NETBSD_MAGIC): Define. + (I386BADMAG): Extend macro to allow new magic numbers. + * coff/x86_64.h (IMAGE_FILE_MACHINE_NATIVE_APPLE_OVERRIDE): Define. + (IMAGE_FILE_MACHINE_NATIVE_FREEBSD_OVERRIDE): Define. + (IMAGE_FILE_MACHINE_NATIVE_LINUX_OVERRIDE): Define. + (IMAGE_FILE_MACHINE_NATIVE_NETBSD_OVERRIDE): Define. + (AMD64_APPLE_MAGIC): Define. + (AMD64_FREEBSD_MAGIC): Define. + (AMD64_LINUX_MAGIC): Define. + (AMD64_NETBSD_MAGIC): Define. + (AMD64BADMAG): Extend macro to allow new magic numbers. + 2019-07-19 Alan Modra * elf/ppc64.h (R_PPC64_TPREL34, R_PPC64_DTPREL34), diff --git a/include/coff/i386.h b/include/coff/i386.h index 1d2ccff6f7..8fd2b8d41d 100644 --- a/include/coff/i386.h +++ b/include/coff/i386.h @@ -43,7 +43,26 @@ #define LYNXCOFFMAGIC 0415 +/* .NET DLLs XOR the Machine number (above) with an override to + indicate that the DLL contains OS-specific machine code rather + than just IL or bytecode. See + https://github.com/dotnet/coreclr/blob/6f7aa7967c607b8c667518314ab937c0d7547025/src/inc/pedecoder.h#L94-L107. */ +#define IMAGE_FILE_MACHINE_NATIVE_APPLE_OVERRIDE 0x4644 +#define IMAGE_FILE_MACHINE_NATIVE_FREEBSD_OVERRIDE 0xadc4 +#define IMAGE_FILE_MACHINE_NATIVE_LINUX_OVERRIDE 0x7b79 +#define IMAGE_FILE_MACHINE_NATIVE_NETBSD_OVERRIDE 0x1993 + +/* Used in some .NET DLLs that target a specific OS. */ +#define I386_APPLE_MAGIC (I386MAGIC ^ IMAGE_FILE_MACHINE_NATIVE_APPLE_OVERRIDE) +#define I386_FREEBSD_MAGIC (I386MAGIC ^ IMAGE_FILE_MACHINE_NATIVE_FREEBSD_OVERRIDE) +#define I386_LINUX_MAGIC (I386MAGIC ^ IMAGE_FILE_MACHINE_NATIVE_LINUX_OVERRIDE) +#define I386_NETBSD_MAGIC (I386MAGIC ^ IMAGE_FILE_MACHINE_NATIVE_NETBSD_OVERRIDE) + #define I386BADMAG(x) ( ((x).f_magic != I386MAGIC) \ + && (x).f_magic != I386_APPLE_MAGIC \ + && (x).f_magic != I386_FREEBSD_MAGIC \ + && (x).f_magic != I386_LINUX_MAGIC \ + && (x).f_magic != I386_NETBSD_MAGIC \ && (x).f_magic != I386AIXMAGIC \ && (x).f_magic != I386PTXMAGIC \ && (x).f_magic != LYNXCOFFMAGIC) diff --git a/include/coff/x86_64.h b/include/coff/x86_64.h index 3d0e6f085d..c0e6de3b1f 100644 --- a/include/coff/x86_64.h +++ b/include/coff/x86_64.h @@ -29,7 +29,27 @@ #define AMD64MAGIC 0x8664 -#define AMD64BADMAG(x) ((x).f_magic != AMD64MAGIC) +/* .NET DLLs XOR the Machine number (above) with an override to + indicate that the DLL contains OS-specific machine code rather + than just IL or bytecode. See + https://github.com/dotnet/coreclr/blob/6f7aa7967c607b8c667518314ab937c0d7547025/src/inc/pedecoder.h#L94-L107. */ +#define IMAGE_FILE_MACHINE_NATIVE_APPLE_OVERRIDE 0x4644 +#define IMAGE_FILE_MACHINE_NATIVE_FREEBSD_OVERRIDE 0xadc4 +#define IMAGE_FILE_MACHINE_NATIVE_LINUX_OVERRIDE 0x7b79 +#define IMAGE_FILE_MACHINE_NATIVE_NETBSD_OVERRIDE 0x1993 + +/* Used in some .NET DLLs that target a specific OS. */ +#define AMD64_APPLE_MAGIC (AMD64MAGIC ^ IMAGE_FILE_MACHINE_NATIVE_APPLE_OVERRIDE) +#define AMD64_FREEBSD_MAGIC (AMD64MAGIC ^ IMAGE_FILE_MACHINE_NATIVE_FREEBSD_OVERRIDE) +#define AMD64_LINUX_MAGIC (AMD64MAGIC ^ IMAGE_FILE_MACHINE_NATIVE_LINUX_OVERRIDE) +#define AMD64_NETBSD_MAGIC (AMD64MAGIC ^ IMAGE_FILE_MACHINE_NATIVE_NETBSD_OVERRIDE) + +#define AMD64BADMAG(x) ( ((x).f_magic != AMD64MAGIC) \ + && ((x).f_magic != AMD64_APPLE_MAGIC) \ + && ((x).f_magic != AMD64_FREEBSD_MAGIC) \ + && ((x).f_magic != AMD64_LINUX_MAGIC) \ + && ((x).f_magic != AMD64_NETBSD_MAGIC)) + #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b #define OMAGIC 0404 /* Object files, eg as output. */ -- 2.34.1