sim: use build-gnulib with build-time tools [PR sim/30882]

Some of our build-time tools are fairly complicated and require some
POSIX APIs that the build system doesn't provide.  Leverage the new
build-gnulib tree to provide fallback implementations automatically.
diff --git a/sim/Makefile.am b/sim/Makefile.am
index b429260..7c20ad7 100644
--- a/sim/Makefile.am
+++ b/sim/Makefile.am
@@ -20,6 +20,7 @@
 
 GNULIB_PARENT_DIR = ..
 @am__include@ @am__quote@$(GNULIB_PARENT_DIR)/gnulib/Makefile.gnulib.inc@am__quote@
+@am__include@ @am__quote@$(GNULIB_PARENT_DIR)/build-$(build)/gnulib/Makefile.gnulib.inc@am__quote@
 
 srccom = $(srcdir)/common
 srcroot = $(srcdir)/..
@@ -66,11 +67,14 @@
 	$(AM_CPPFLAGS_$(subst -,_,$(@D)_$(@F)))
 
 AM_CPPFLAGS_FOR_BUILD = \
+	$(BUILD_INCGNU) \
+	-I../build-$(build) \
 	-I$(srcroot)/include \
 	$(SIM_HW_CFLAGS) \
 	$(SIM_INLINE)
 COMPILE_FOR_BUILD = $(CC_FOR_BUILD) $(AM_CPPFLAGS_FOR_BUILD) $(CPPFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD)
 LINK_FOR_BUILD = $(CC_FOR_BUILD) $(CFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) -o $@
+LIBS_FOR_BUILD = $(BUILD_LIBGNU) $(BUILD_LIBGNU_EXTRA_LIBS)
 
 ## Deps to add to the all-recursive target.  These are built before descending
 ## into any subdirs.
diff --git a/sim/Makefile.in b/sim/Makefile.in
index cc6ee92..82cb041 100644
--- a/sim/Makefile.in
+++ b/sim/Makefile.in
@@ -980,7 +980,8 @@
 @SIM_ENABLE_ARCH_cr16_TRUE@	cr16/gencode.$(OBJEXT)
 cr16_gencode_OBJECTS = $(am_cr16_gencode_OBJECTS)
 @SIM_ENABLE_ARCH_cr16_TRUE@cr16_gencode_DEPENDENCIES =  \
-@SIM_ENABLE_ARCH_cr16_TRUE@	cr16/cr16-opc.o
+@SIM_ENABLE_ARCH_cr16_TRUE@	cr16/cr16-opc.o \
+@SIM_ENABLE_ARCH_cr16_TRUE@	$(am__DEPENDENCIES_1)
 am_cr16_run_OBJECTS =
 cr16_run_OBJECTS = $(am_cr16_run_OBJECTS)
 @SIM_ENABLE_ARCH_cr16_TRUE@cr16_run_DEPENDENCIES = cr16/nrun.o \
@@ -998,7 +999,8 @@
 @SIM_ENABLE_ARCH_d10v_TRUE@	d10v/gencode.$(OBJEXT)
 d10v_gencode_OBJECTS = $(am_d10v_gencode_OBJECTS)
 @SIM_ENABLE_ARCH_d10v_TRUE@d10v_gencode_DEPENDENCIES =  \
-@SIM_ENABLE_ARCH_d10v_TRUE@	d10v/d10v-opc.o
+@SIM_ENABLE_ARCH_d10v_TRUE@	d10v/d10v-opc.o \
+@SIM_ENABLE_ARCH_d10v_TRUE@	$(am__DEPENDENCIES_1)
 am_d10v_run_OBJECTS =
 d10v_run_OBJECTS = $(am_d10v_run_OBJECTS)
 @SIM_ENABLE_ARCH_d10v_TRUE@d10v_run_DEPENDENCIES = d10v/nrun.o \
@@ -1040,7 +1042,7 @@
 igen_gen_DEPENDENCIES = igen/gen-main.o igen/libigen.a
 am_igen_igen_OBJECTS = igen/igen.$(OBJEXT)
 igen_igen_OBJECTS = $(am_igen_igen_OBJECTS)
-igen_igen_DEPENDENCIES = igen/libigen.a
+igen_igen_DEPENDENCIES = igen/libigen.a $(am__DEPENDENCIES_1)
 am_igen_ld_cache_OBJECTS =
 igen_ld_cache_OBJECTS = $(am_igen_ld_cache_OBJECTS)
 igen_ld_cache_DEPENDENCIES = igen/ld-cache-main.o igen/libigen.a
@@ -1065,7 +1067,8 @@
 @SIM_ENABLE_ARCH_m32c_TRUE@am_m32c_opc2c_OBJECTS =  \
 @SIM_ENABLE_ARCH_m32c_TRUE@	m32c/opc2c.$(OBJEXT)
 m32c_opc2c_OBJECTS = $(am_m32c_opc2c_OBJECTS)
-m32c_opc2c_LDADD = $(LDADD)
+@SIM_ENABLE_ARCH_m32c_TRUE@m32c_opc2c_DEPENDENCIES =  \
+@SIM_ENABLE_ARCH_m32c_TRUE@	$(am__DEPENDENCIES_1)
 am_m32c_run_OBJECTS =
 m32c_run_OBJECTS = $(am_m32c_run_OBJECTS)
 @SIM_ENABLE_ARCH_m32c_TRUE@m32c_run_DEPENDENCIES = m32c/main.o \
@@ -1077,7 +1080,8 @@
 @SIM_ENABLE_ARCH_m68hc11_TRUE@am_m68hc11_gencode_OBJECTS =  \
 @SIM_ENABLE_ARCH_m68hc11_TRUE@	m68hc11/gencode.$(OBJEXT)
 m68hc11_gencode_OBJECTS = $(am_m68hc11_gencode_OBJECTS)
-m68hc11_gencode_LDADD = $(LDADD)
+@SIM_ENABLE_ARCH_m68hc11_TRUE@m68hc11_gencode_DEPENDENCIES =  \
+@SIM_ENABLE_ARCH_m68hc11_TRUE@	$(am__DEPENDENCIES_1)
 am_m68hc11_run_OBJECTS =
 m68hc11_run_OBJECTS = $(am_m68hc11_run_OBJECTS)
 @SIM_ENABLE_ARCH_m68hc11_TRUE@m68hc11_run_DEPENDENCIES =  \
@@ -1159,7 +1163,8 @@
 @SIM_ENABLE_ARCH_rx_TRUE@	$(am__DEPENDENCIES_4)
 @SIM_ENABLE_ARCH_sh_TRUE@am_sh_gencode_OBJECTS = sh/gencode.$(OBJEXT)
 sh_gencode_OBJECTS = $(am_sh_gencode_OBJECTS)
-sh_gencode_LDADD = $(LDADD)
+@SIM_ENABLE_ARCH_sh_TRUE@sh_gencode_DEPENDENCIES =  \
+@SIM_ENABLE_ARCH_sh_TRUE@	$(am__DEPENDENCIES_1)
 am_sh_run_OBJECTS =
 sh_run_OBJECTS = $(am_sh_run_OBJECTS)
 @SIM_ENABLE_ARCH_sh_TRUE@sh_run_DEPENDENCIES = sh/nrun.o sh/libsim.a \
@@ -1860,10 +1865,12 @@
 	-I.. -I$(@D) -I$(srcdir)/$(@D) $(SIM_HW_CFLAGS) $(SIM_INLINE) \
 	$(AM_CPPFLAGS_$(subst -,_,$(@D))) $(AM_CPPFLAGS_$(subst \
 	-,_,$(@D)_$(@F))) -I$(srcdir)/common -DSIM_TOPDIR_BUILD
-AM_CPPFLAGS_FOR_BUILD = -I$(srcroot)/include $(SIM_HW_CFLAGS) \
-	$(SIM_INLINE) -I$(srcdir)/common
+AM_CPPFLAGS_FOR_BUILD = $(BUILD_INCGNU) -I../build-$(build) \
+	-I$(srcroot)/include $(SIM_HW_CFLAGS) $(SIM_INLINE) \
+	-I$(srcdir)/common
 COMPILE_FOR_BUILD = $(CC_FOR_BUILD) $(AM_CPPFLAGS_FOR_BUILD) $(CPPFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD)
 LINK_FOR_BUILD = $(CC_FOR_BUILD) $(CFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) -o $@
+LIBS_FOR_BUILD = $(BUILD_LIBGNU) $(BUILD_LIBGNU_EXTRA_LIBS)
 SIM_ALL_RECURSIVE_DEPS = $(am__append_89)
 SIM_INSTALL_DATA_LOCAL_DEPS = 
 SIM_INSTALL_EXEC_LOCAL_DEPS = $(am__append_30)
@@ -2007,7 +2014,7 @@
 	igen/gen.c
 
 igen_igen_SOURCES = igen/igen.c
-igen_igen_LDADD = igen/libigen.a
+igen_igen_LDADD = igen/libigen.a $(LIBS_FOR_BUILD)
 igen_filter_SOURCES = 
 igen_filter_LDADD = igen/filter-main.o igen/libigen.a
 igen_gen_SOURCES = 
@@ -2210,7 +2217,7 @@
 @SIM_ENABLE_ARCH_cr16_TRUE@	cr16/table.c
 
 @SIM_ENABLE_ARCH_cr16_TRUE@cr16_gencode_SOURCES = cr16/gencode.c
-@SIM_ENABLE_ARCH_cr16_TRUE@cr16_gencode_LDADD = cr16/cr16-opc.o
+@SIM_ENABLE_ARCH_cr16_TRUE@cr16_gencode_LDADD = cr16/cr16-opc.o $(LIBS_FOR_BUILD)
 @SIM_ENABLE_ARCH_cris_TRUE@AM_CFLAGS_cris_mloopv10f.o = $(SIM_CFLAG_WNO_UNUSED_BUT_SET_VARIABLE) \
 @SIM_ENABLE_ARCH_cris_TRUE@	$(SIM_CFLAG_WNO_SHADOW_LOCAL)
 @SIM_ENABLE_ARCH_cris_TRUE@AM_CFLAGS_cris_mloopv32f.o = $(SIM_CFLAG_WNO_UNUSED_BUT_SET_VARIABLE)
@@ -2286,7 +2293,7 @@
 @SIM_ENABLE_ARCH_d10v_TRUE@	d10v/table.c
 
 @SIM_ENABLE_ARCH_d10v_TRUE@d10v_gencode_SOURCES = d10v/gencode.c
-@SIM_ENABLE_ARCH_d10v_TRUE@d10v_gencode_LDADD = d10v/d10v-opc.o
+@SIM_ENABLE_ARCH_d10v_TRUE@d10v_gencode_LDADD = d10v/d10v-opc.o $(LIBS_FOR_BUILD)
 @SIM_ENABLE_ARCH_erc32_TRUE@READLINE_SRC = $(srcroot)/readline/readline
 @SIM_ENABLE_ARCH_erc32_TRUE@AM_CPPFLAGS_erc32 = $(READLINE_CFLAGS) \
 @SIM_ENABLE_ARCH_erc32_TRUE@	-DFAST_UART
@@ -2528,6 +2535,7 @@
 @SIM_ENABLE_ARCH_m32c_TRUE@	m32c/r8c.c
 
 @SIM_ENABLE_ARCH_m32c_TRUE@m32c_opc2c_SOURCES = m32c/opc2c.c
+@SIM_ENABLE_ARCH_m32c_TRUE@m32c_opc2c_LDADD = $(LIBS_FOR_BUILD)
 
 # opc2c leaks memory, and therefore makes AddressSanitizer unhappy.  Disable
 # leak detection while running it.
@@ -2625,6 +2633,7 @@
 @SIM_ENABLE_ARCH_m68hc11_TRUE@	m68hc11/m68hc12int.c
 
 @SIM_ENABLE_ARCH_m68hc11_TRUE@m68hc11_gencode_SOURCES = m68hc11/gencode.c
+@SIM_ENABLE_ARCH_m68hc11_TRUE@m68hc11_gencode_LDADD = $(LIBS_FOR_BUILD)
 @SIM_ENABLE_ARCH_mcore_TRUE@nodist_mcore_libsim_a_SOURCES = \
 @SIM_ENABLE_ARCH_mcore_TRUE@	mcore/modules.c
 
@@ -3053,6 +3062,7 @@
 @SIM_ENABLE_ARCH_sh_TRUE@	sh/table.c
 
 @SIM_ENABLE_ARCH_sh_TRUE@sh_gencode_SOURCES = sh/gencode.c
+@SIM_ENABLE_ARCH_sh_TRUE@sh_gencode_LDADD = $(LIBS_FOR_BUILD)
 @SIM_ENABLE_ARCH_v850_TRUE@AM_CPPFLAGS_v850 = -DWITH_TARGET_WORD_BITSIZE=32 -DWITH_TARGET_WORD_MSB=31
 @SIM_ENABLE_ARCH_v850_TRUE@nodist_v850_libsim_a_SOURCES = \
 @SIM_ENABLE_ARCH_v850_TRUE@	v850/modules.c
@@ -4909,6 +4919,7 @@
 .PRECIOUS: Makefile
 
 @am__include@ @am__quote@$(GNULIB_PARENT_DIR)/gnulib/Makefile.gnulib.inc@am__quote@
+@am__include@ @am__quote@$(GNULIB_PARENT_DIR)/build-$(build)/gnulib/Makefile.gnulib.inc@am__quote@
 
 # Generate target constants for newlib/libgloss from its source tree.
 # This file is shipped with distributions so we build in the source dir.
diff --git a/sim/cr16/gencode.c b/sim/cr16/gencode.c
index b0bb67b..dbe1b9e 100644
--- a/sim/cr16/gencode.c
+++ b/sim/cr16/gencode.c
@@ -18,7 +18,7 @@
    along with this program. If not, see <http://www.gnu.org/licenses/>.  */
 
 /* This must come before any other includes.  */
-#include "defs.h"
+#include "gnulib/config.h"
 
 #include <stdio.h>
 #include <ctype.h>
diff --git a/sim/cr16/local.mk b/sim/cr16/local.mk
index 10533a4..9eb464f 100644
--- a/sim/cr16/local.mk
+++ b/sim/cr16/local.mk
@@ -55,7 +55,7 @@
 %D%/modules.c: | $(%C%_BUILD_OUTPUTS)
 
 %C%_gencode_SOURCES = %D%/gencode.c
-%C%_gencode_LDADD = %D%/cr16-opc.o
+%C%_gencode_LDADD = %D%/cr16-opc.o $(LIBS_FOR_BUILD)
 
 # These rules are copied from automake, but tweaked to use FOR_BUILD variables.
 %D%/gencode$(EXEEXT): $(%C%_gencode_OBJECTS) $(%C%_gencode_DEPENDENCIES) %D%/$(am__dirstamp)
diff --git a/sim/d10v/gencode.c b/sim/d10v/gencode.c
index 3a37bac..42d655d 100644
--- a/sim/d10v/gencode.c
+++ b/sim/d10v/gencode.c
@@ -1,3 +1,6 @@
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
+
 #include <stdio.h>
 #include <ctype.h>
 #include <limits.h>
diff --git a/sim/d10v/local.mk b/sim/d10v/local.mk
index a65635e..0a9aa8f 100644
--- a/sim/d10v/local.mk
+++ b/sim/d10v/local.mk
@@ -56,7 +56,7 @@
 %D%/modules.c: | $(%C%_BUILD_OUTPUTS)
 
 %C%_gencode_SOURCES = %D%/gencode.c
-%C%_gencode_LDADD = %D%/d10v-opc.o
+%C%_gencode_LDADD = %D%/d10v-opc.o $(LIBS_FOR_BUILD)
 
 # These rules are copied from automake, but tweaked to use FOR_BUILD variables.
 %D%/gencode$(EXEEXT): $(%C%_gencode_OBJECTS) $(%C%_gencode_DEPENDENCIES) %D%/$(am__dirstamp)
diff --git a/sim/igen/filter.c b/sim/igen/filter.c
index 71b5482..2e9ae79 100644
--- a/sim/igen/filter.c
+++ b/sim/igen/filter.c
@@ -19,6 +19,8 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
 
 #include <stdio.h>
 #include <string.h>
diff --git a/sim/igen/filter_host.c b/sim/igen/filter_host.c
index ce5aa4b..526d6a4 100644
--- a/sim/igen/filter_host.c
+++ b/sim/igen/filter_host.c
@@ -19,6 +19,9 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
+
 #include "filter_host.h"
 
 /* Shorten traces by eliminating the directory component to filenames.  */
diff --git a/sim/igen/gen-engine.c b/sim/igen/gen-engine.c
index 29eb333..83d4292 100644
--- a/sim/igen/gen-engine.c
+++ b/sim/igen/gen-engine.c
@@ -19,6 +19,9 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
+
 #include "misc.h"
 #include "lf.h"
 #include "table.h"
diff --git a/sim/igen/gen-icache.c b/sim/igen/gen-icache.c
index 366ba93..aa0cd66 100644
--- a/sim/igen/gen-icache.c
+++ b/sim/igen/gen-icache.c
@@ -19,6 +19,9 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
+
 #include <stdlib.h>
 
 #include "misc.h"
diff --git a/sim/igen/gen-idecode.c b/sim/igen/gen-idecode.c
index 0059c1c..d6be10b 100644
--- a/sim/igen/gen-idecode.c
+++ b/sim/igen/gen-idecode.c
@@ -19,6 +19,8 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
 
 #include "misc.h"
 #include "lf.h"
diff --git a/sim/igen/gen-itable.c b/sim/igen/gen-itable.c
index bea06e8..c13307b 100644
--- a/sim/igen/gen-itable.c
+++ b/sim/igen/gen-itable.c
@@ -19,7 +19,8 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
 
 #include "misc.h"
 #include "lf.h"
diff --git a/sim/igen/gen-model.c b/sim/igen/gen-model.c
index 31f56bc..3731a2e 100644
--- a/sim/igen/gen-model.c
+++ b/sim/igen/gen-model.c
@@ -19,6 +19,8 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
 
 #include "misc.h"
 #include "lf.h"
diff --git a/sim/igen/gen-semantics.c b/sim/igen/gen-semantics.c
index 4557029..b216347 100644
--- a/sim/igen/gen-semantics.c
+++ b/sim/igen/gen-semantics.c
@@ -19,7 +19,8 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
 
 #include "misc.h"
 #include "lf.h"
diff --git a/sim/igen/gen-support.c b/sim/igen/gen-support.c
index 63b2932..78ace20 100644
--- a/sim/igen/gen-support.c
+++ b/sim/igen/gen-support.c
@@ -19,6 +19,8 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
 
 #include "misc.h"
 #include "lf.h"
diff --git a/sim/igen/gen.c b/sim/igen/gen.c
index 2176b60..ac4ca6b 100644
--- a/sim/igen/gen.c
+++ b/sim/igen/gen.c
@@ -19,6 +19,8 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
 
 #include "misc.h"
 #include "lf.h"
diff --git a/sim/igen/igen.c b/sim/igen/igen.c
index b9919a9..eaf69b9 100644
--- a/sim/igen/igen.c
+++ b/sim/igen/igen.c
@@ -19,6 +19,9 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
+
 #include <getopt.h>
 #include <stdlib.h>
 
diff --git a/sim/igen/ld-cache.c b/sim/igen/ld-cache.c
index 3c00f34..b5f5b9d 100644
--- a/sim/igen/ld-cache.c
+++ b/sim/igen/ld-cache.c
@@ -19,7 +19,8 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
 
 #include "misc.h"
 #include "lf.h"
diff --git a/sim/igen/ld-decode.c b/sim/igen/ld-decode.c
index 6edd7c8..ad0e64f5 100644
--- a/sim/igen/ld-decode.c
+++ b/sim/igen/ld-decode.c
@@ -19,9 +19,11 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-
 /* load the opcode stat structure */
 
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
+
 #include "misc.h"
 #include "lf.h"
 #include "table.h"
diff --git a/sim/igen/ld-insn.c b/sim/igen/ld-insn.c
index af639df..fa5b46b 100644
--- a/sim/igen/ld-insn.c
+++ b/sim/igen/ld-insn.c
@@ -19,6 +19,8 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
 
 #include "misc.h"
 #include "lf.h"
diff --git a/sim/igen/lf.c b/sim/igen/lf.c
index 46b1f6b..e664fbc 100644
--- a/sim/igen/lf.c
+++ b/sim/igen/lf.c
@@ -19,6 +19,8 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
 
 #include <stdbool.h>
 #include <stdio.h>
diff --git a/sim/igen/local.mk b/sim/igen/local.mk
index f4487a8..f9a2cf8 100644
--- a/sim/igen/local.mk
+++ b/sim/igen/local.mk
@@ -47,7 +47,7 @@
 	%D%/gen.c
 
 %C%_igen_SOURCES = %D%/igen.c
-%C%_igen_LDADD = %D%/libigen.a
+%C%_igen_LDADD = %D%/libigen.a $(LIBS_FOR_BUILD)
 
 # These rules are copied from automake, but tweaked to use FOR_BUILD variables.
 %D%/libigen.a: $(igen_libigen_a_OBJECTS) $(igen_libigen_a_DEPENDENCIES) $(EXTRA_igen_libigen_a_DEPENDENCIES) %D%/$(am__dirstamp)
diff --git a/sim/igen/misc.c b/sim/igen/misc.c
index fface6f..e949b6c 100644
--- a/sim/igen/misc.c
+++ b/sim/igen/misc.c
@@ -19,7 +19,8 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
 
 #include <stdio.h>
 #include <stdarg.h>
diff --git a/sim/igen/table.c b/sim/igen/table.c
index fdca9cb..d5c7068 100644
--- a/sim/igen/table.c
+++ b/sim/igen/table.c
@@ -19,7 +19,8 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
 
 #include <sys/types.h>
 #include <sys/stat.h>
diff --git a/sim/m32c/local.mk b/sim/m32c/local.mk
index 39620b5..f8efd07 100644
--- a/sim/m32c/local.mk
+++ b/sim/m32c/local.mk
@@ -61,6 +61,7 @@
 %D%/modules.c: | $(%C%_BUILD_OUTPUTS)
 
 %C%_opc2c_SOURCES = %D%/opc2c.c
+%C%_opc2c_LDADD = $(LIBS_FOR_BUILD)
 
 # These rules are copied from automake, but tweaked to use FOR_BUILD variables.
 %D%/opc2c$(EXEEXT): $(%C%_opc2c_OBJECTS) $(%C%_opc2c_DEPENDENCIES) %D%/$(am__dirstamp)
diff --git a/sim/m32c/opc2c.c b/sim/m32c/opc2c.c
index 113e390..1b2b133 100644
--- a/sim/m32c/opc2c.c
+++ b/sim/m32c/opc2c.c
@@ -19,7 +19,7 @@
 along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 /* This must come before any other includes.  */
-#include "defs.h"
+#include "gnulib/config.h"
 
 #include <stdio.h>
 #include <string.h>
diff --git a/sim/m68hc11/gencode.c b/sim/m68hc11/gencode.c
index 4e50770..4862a6d 100644
--- a/sim/m68hc11/gencode.c
+++ b/sim/m68hc11/gencode.c
@@ -18,7 +18,7 @@
 along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 /* This must come before any other includes.  */
-#include "defs.h"
+#include "gnulib/config.h"
 
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/sim/m68hc11/local.mk b/sim/m68hc11/local.mk
index 36c503e..1ec60d7 100644
--- a/sim/m68hc11/local.mk
+++ b/sim/m68hc11/local.mk
@@ -71,6 +71,7 @@
 %D%/modules.c: | $(%C%_BUILD_OUTPUTS)
 
 %C%_gencode_SOURCES = %D%/gencode.c
+%C%_gencode_LDADD = $(LIBS_FOR_BUILD)
 
 # These rules are copied from automake, but tweaked to use FOR_BUILD variables.
 %D%/gencode$(EXEEXT): $(%C%_gencode_OBJECTS) $(%C%_gencode_DEPENDENCIES) %D%/$(am__dirstamp)
diff --git a/sim/sh/gencode.c b/sim/sh/gencode.c
index c121d53..99b0225 100644
--- a/sim/sh/gencode.c
+++ b/sim/sh/gencode.c
@@ -30,6 +30,9 @@
 
 */
 
+/* This must come before any other includes.  */
+#include "gnulib/config.h"
+
 #include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/sim/sh/local.mk b/sim/sh/local.mk
index 6cbd3a7..0d8a28a 100644
--- a/sim/sh/local.mk
+++ b/sim/sh/local.mk
@@ -57,6 +57,7 @@
 %D%/modules.c: | $(%C%_BUILD_OUTPUTS)
 
 %C%_gencode_SOURCES = %D%/gencode.c
+%C%_gencode_LDADD = $(LIBS_FOR_BUILD)
 
 # These rules are copied from automake, but tweaked to use FOR_BUILD variables.
 %D%/gencode$(EXEEXT): $(%C%_gencode_OBJECTS) $(%C%_gencode_DEPENDENCIES) %D%/$(am__dirstamp)