Use CC instead of LD when linking

Nathan Phillip Brink ohnobinki at ohnopublishing.net
Tue Jan 25 04:17:09 UTC 2011


Hello,

I have attached a patch which lets busybox without ever directly
calling $(LD). This would be convenient for me since it lets me do
cool stuff like:

make CFLAGS=-m32

and end up with a 32-bit busybox, for example. I know that this isn't
useful in itself, but it also fixes busybox's buildsystem ignoring any
LDFLAGS which have -Wl in them by (IMO) correctly using CC to perform
the linking instead of LD.

Is this the correct place to try to push changes to busybox's
buildsystem? Am I supposed to myself push these changes to wherever
busybox gets its Kbuild from? If I have to do that, I don't really
have any idea where to start ;-).

Thanks for any comments or suggestsions :-).

-- 
binki

Look out for missing apostrophes!
-------------- next part --------------
diff --git a/Makefile b/Makefile
index d9204f4..e9b4700 100644
--- a/Makefile
+++ b/Makefile
@@ -308,7 +308,8 @@ CHECKFLAGS     := -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ -Wbitwise $(C
 MODFLAGS	= -DMODULE
 CFLAGS_MODULE   = $(MODFLAGS)
 AFLAGS_MODULE   = $(MODFLAGS)
-LDFLAGS_MODULE  = -r
+LDFLAGS_r	= -r -nostdlib
+LDFLAGS_MODULE  = $(LDFLAGS_r)
 CFLAGS_KERNEL	=
 AFLAGS_KERNEL	=
 
@@ -330,7 +331,7 @@ KERNELVERSION = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
 export	VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION \
 	ARCH CONFIG_SHELL HOSTCC HOSTCFLAGS CROSS_COMPILE AS LD CC \
 	CPP AR NM STRIP OBJCOPY OBJDUMP MAKE AWK GENKSYMS PERL UTS_MACHINE \
-	HOSTCXX HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS
+	HOSTCXX HOSTCXXFLAGS LDFLAGS_r LDFLAGS_MODULE CHECK CHECKFLAGS
 
 export CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS LDFLAGS
 export CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 5685b5b..5e733f8 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -174,7 +174,7 @@ cmd_modversions =							\
 		| $(GENKSYMS) -a $(ARCH)				\
 		> $(@D)/.tmp_$(@F:.o=.ver);				\
 									\
-		$(LD) $(LDFLAGS) -r -o $@ $(@D)/.tmp_$(@F) 		\
+		$(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_r) -o $@ $(@D)/.tmp_$(@F)	\
 			-T $(@D)/.tmp_$(@F:.o=.ver);			\
 		rm -f $(@D)/.tmp_$(@F) $(@D)/.tmp_$(@F:.o=.ver);	\
 	else								\
@@ -256,7 +256,7 @@ ifdef builtin-target
 quiet_cmd_link_o_target = LD      $@
 # If the list of objects to link is empty, just create an empty built-in.o
 cmd_link_o_target = $(if $(strip $(obj-y)),\
-		$(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^),\
+		$(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_r) -o $@ $(filter $(obj-y), $^),\
 		rm -f $@; $(AR) rcs $@)
 
 $(builtin-target): $(obj-y) FORCE
@@ -291,10 +291,10 @@ $($(subst $(obj)/,,$(@:.o=-objs)))    \
 $($(subst $(obj)/,,$(@:.o=-y)))), $^)
 
 quiet_cmd_link_multi-y = LD      $@
-cmd_link_multi-y = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps)
+cmd_link_multi-y = $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_r) -o $@ $(link_multi_deps)
 
 quiet_cmd_link_multi-m = LD [M]  $@
-cmd_link_multi-m = $(LD) $(ld_flags) $(LDFLAGS_MODULE) -o $@ $(link_multi_deps)
+cmd_link_multi-m = $(CC) $(CFLAGS) $(ld_flags) $(LDFLAGS_MODULE) -o $@ $(link_multi_deps)
 
 # We would rather have a list of rules like
 # 	foo.o: $(foo-objs)
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 3e54ea7..342b0d6 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -117,12 +117,7 @@ a_flags        = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
 
 cpp_flags      = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(__cpp_flags)
 
-# Seems to be a wrong thing to do. LDFLAGS contains gcc's flags,
-# yet ld_flags is fed to ld.
-#ld_flags       = $(LDFLAGS) $(EXTRA_LDFLAGS)
-# Remove the -Wl, prefix from linker options normally passed through gcc
-ld_flags       = $(filter-out -Wl$(comma)%,$(LDFLAGS) $(EXTRA_LDFLAGS))
-
+ld_flags       = $(LDFLAGS) $(EXTRA_LDFLAGS)
 
 # Finds the multi-part object the current object will be linked into
 modname-multi = $(sort $(foreach m,$(multi-used),\
@@ -151,10 +146,8 @@ $(obj)/%:: $(src)/%_shipped
 # Linking
 # ---------------------------------------------------------------------------
 
-# TODO: LDFLAGS usually is supposed to contain gcc's flags, not ld's.
-# but here we feed them to ld!
-quiet_cmd_ld = LD      $@
-cmd_ld = $(LD) $(LDFLAGS) $(EXTRA_LDFLAGS) $(LDFLAGS_$(@F)) \
+quiet_cmd_ld = CCLD    $@
+cmd_ld = $(CC) $(CFLAGS) $(ld_flags) $(LDFLAGS_$(@F)) \
 	       $(filter-out FORCE,$^) -o $@
 
 # Objcopy
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20110124/f115c4b9/attachment.asc>


More information about the busybox mailing list