public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
* Missing CFLAGS for some objects
@ 2023-04-21 11:45 Tomasz Dziendzielski
  2023-04-21 16:14 ` Florian Weimer
  0 siblings, 1 reply; 3+ messages in thread
From: Tomasz Dziendzielski @ 2023-04-21 11:45 UTC (permalink / raw)
  To: libc-help


[-- Attachment #1.1: Type: text/plain, Size: 1036 bytes --]

Hello,
one of our developers reported that when we build libc in our environment
using Yocto Project we end up with corrupted debug symbols even though the
-g flag is passed to the CFLAGS variable. I noticed that in fact some gcc
commands (about ~300) are missing -g flag in the invocation. So now my
question is: is there some reason why some objects are built without CFLAGS?
You can get the list of objects without debug symbols (and CFLAGS) by
setting CFLAGS=" -g " for make and using grep for gcc or cross gcc in the
make execution log file and grep with an invert match for ' -g '.

I found some solution which I'm not sure if is correct. Simply apply the
patch from the attachment and run the build and there are 0 gcc invocations
without -g flag. With that patch in our environment I was able to get
functional libc debug symbols.

Looking forward to getting the answer on my questions and the review if my
patch is correct and should be applied in the glibc or if there is another
solution.

Best regards,
Tomasz Dziendzielski

[-- Attachment #2: 0001-Add-missing-CFLAGS-to-compiler.patch --]
[-- Type: text/x-patch, Size: 5621 bytes --]

From 1cc6ade643a85de21b2d7805f03752995c4f5ae1 Mon Sep 17 00:00:00 2001
From: Tomasz Dziendzielski <tomasz.dziendzielski@gmail.com>
Date: Fri, 21 Apr 2023 13:25:41 +0200
Subject: [PATCH] Add missing $(CFLAGS) to compiler

It seems for ~300 objects CFLAGS are not passed at all. This leads to incorrect
results, for example if '-g' is passed then libc debug symbols might be broken
or incomplete on some systems.

To fix this I added $(CFLAGS) to the places where flags were missing. With the
patch there is 0 gcc invocations that were lacking cflags.

Signed-off-by: Tomasz Dziendzielski <tomasz.dziendzielski@gmail.com>
---
 Makeconfig   | 6 +++---
 Makerules    | 6 +++---
 csu/Makefile | 2 +-
 elf/Makefile | 6 +++---
 4 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/Makeconfig b/Makeconfig
index 2514db35f6b83f6577d77a1a3068a75834f7d8bd..4ceac5d86c4ad1c7b4419349bf3021b7ab7b97dd 100644
--- a/Makeconfig
+++ b/Makeconfig
@@ -436,7 +436,7 @@ ifndef +link-pie
 	     $(relro-LDFLAGS) $(hashstyle-LDFLAGS) \
 	     $(firstword $(CRT-$(@F)) $(csu-objpfx)S$(start-installed-name)) \
 	     $(+preinit) $(+prectorS)
-+link-pie-before-libc = -o $@ $(+link-pie-before-inputs) \
++link-pie-before-libc = $(CFLAGS) -o $@ $(+link-pie-before-inputs) \
 	     $(filter-out $(addprefix $(csu-objpfx),start.o \
 						    S$(start-installed-name))\
 			  $(+preinit) $(link-extra-libs) \
@@ -467,7 +467,7 @@ ifndef +link-static
 	      $(sysdep-LDFLAGS) $(LDFLAGS) $(LDFLAGS-$(@F))  \
 	      $(firstword $(CRT-$(@F)) $(csu-objpfx)$(real-static-start-installed-name)) \
 	      $(+preinit) $(+prectorT)
-+link-static-before-libc = -o $@ $(+link-static-before-inputs) \
++link-static-before-libc = $(CFLAGS) -o $@ $(+link-static-before-inputs) \
 	      $(filter-out $(addprefix $(csu-objpfx),start.o \
 						     $(start-installed-name))\
 			   $(+preinit) $(link-extra-libs-static) \
@@ -503,7 +503,7 @@ else  # not build-pie-default
 	      $(relro-LDFLAGS) $(hashstyle-LDFLAGS) \
 	      $(firstword $(CRT-$(@F)) $(csu-objpfx)$(start-installed-name)) \
 	      $(+preinit) $(+prector)
-+link-before-libc = -o $@ $(+link-before-inputs) \
++link-before-libc = $(CFLAGS) -o $@ $(+link-before-inputs) \
 	      $(filter-out $(addprefix $(csu-objpfx),start.o \
 						     $(start-installed-name))\
 			   $(+preinit) $(link-extra-libs) \
diff --git a/Makerules b/Makerules
index 31064470e52c8393d8a52d811cec117ba4a56af2..1db8427dd51da63b72788f0a1109fc13bf7fb0aa 100644
--- a/Makerules
+++ b/Makerules
@@ -534,7 +534,7 @@ lib%.so: lib%_pic.a $(+preinit) $(+postinit) $(link-libc-deps)
 	$(call after-link,$@)
 
 define build-shlib-helper
-$(LINK.o) -shared -static-libgcc -Wl,-O1 $(sysdep-LDFLAGS) \
+$(LINK.o) -shared -static-libgcc $(CFLAGS) -Wl,-O1 $(sysdep-LDFLAGS) \
 	  $(if $($(@F)-no-z-defs)$(no-z-defs),,-Wl,-z,defs) $(rtld-LDFLAGS) \
 	  $(if $($(@F)-no-dt-relr),$(no-dt-relr-ldflag),$(dt-relr-ldflag)) \
 	  $(extra-B-$(@F:lib%.so=%).so) -B$(csu-objpfx) \
@@ -550,7 +550,7 @@ $(build-shlib-helper) -o $@ \
 endef
 
 define build-module-helper
-$(LINK.o) -shared -static-libgcc $(sysdep-LDFLAGS) $(rtld-LDFLAGS) \
+$(LINK.o) -shared -static-libgcc $(CFLAGS) $(sysdep-LDFLAGS) $(rtld-LDFLAGS) \
 	  $(if $($(@F)-no-z-defs)$(no-z-defs),,-Wl,-z,defs) \
 	  $(if $($(@F)-no-dt-relr),$(no-dt-relr-ldflag),$(dt-relr-ldflag)) \
 	  -B$(csu-objpfx) $(load-map-file) \
@@ -598,7 +598,7 @@ LDLIBS-c.so += $(libc.so-gnulib)
 LDFLAGS-c.so += -e __libc_main
 # Pre-link the objects of libc_pic.a for .gnu.glibc-stub.* processing.
 $(common-objpfx)libc_pic.os: $(common-objpfx)libc_pic.a
-	$(LINK.o) -nostdlib -nostartfiles -r -o $@ \
+	$(LINK.o) -nostdlib -nostartfiles -r $(CFLAGS) -o $@ \
 	$(LDFLAGS-c_pic.os) $(whole-archive) $^ -o $@
 
 # Generate a list of -R options to excise .gnu.glibc-stub.* sections.
diff --git a/csu/Makefile b/csu/Makefile
index fcd277e6d388ddaeae4d3a053715bb5879c43ff9..6680a6a5b278e287f0a553d3f625727a2820e6e9 100644
--- a/csu/Makefile
+++ b/csu/Makefile
@@ -129,7 +129,7 @@ include ../Rules
 subdir_lib: $(extra-objs:%=$(objpfx)%)
 
 define link-relocatable
-$(CC) -nostdlib -nostartfiles -r -o $@ $^
+$(CC) -nostdlib -nostartfiles -r $(CFLAGS) -o $@ $^
 endef
 
 ifndef start-installed-name-rule
diff --git a/elf/Makefile b/elf/Makefile
index 396ec51424dfae55f8ee9d9c21b39edfb1e55763..9d25a581ff89cbd5117c495a4430c444c1f6e478 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -1233,7 +1233,7 @@ lib-noranlib: $(objpfx)$(rtld-installed-name) \
 endif
 
 # Command to link into a larger single relocatable object.
-reloc-link = $(LINK.o) -nostdlib -nostartfiles -r
+reloc-link = $(LINK.o) -nostdlib -nostartfiles $(CFLAGS) -r
 
 $(objpfx)dl-allobjs.os: $(all-rtld-routines:%=$(objpfx)%.os)
 	$(reloc-link) -o $@ $^
@@ -1305,7 +1305,7 @@ $(objpfx)rtld-libc.a: $(objpfx)librtld.mk FORCE
 	$(MAKE) -f $< -f rtld-Rules
 
 $(objpfx)librtld.os: $(objpfx)dl-allobjs.os $(objpfx)rtld-libc.a
-	$(LINK.o) -nostdlib -nostartfiles -r -o $@ '-Wl,-(' $^ -lgcc '-Wl,-)' \
+	$(LINK.o) -nostdlib -nostartfiles -r $(CFLAGS) -o $@ '-Wl,-(' $^ -lgcc '-Wl,-)' \
 		  -Wl,-Map,$@.map
 
 generated += librtld.map librtld.mk rtld-libc.a librtld.os.map
@@ -1315,7 +1315,7 @@ z-now-yes = -Wl,-z,now
 $(objpfx)ld.so: $(objpfx)librtld.os $(ld-map)
 # Link into a temporary file so that we don't touch $@ at all
 # if the sanity check below fails.
-	$(LINK.o) -nostdlib -nostartfiles -shared -o $@.new		\
+	$(LINK.o) -nostdlib -nostartfiles -shared $(CFLAGS) -o $@.new		\
 		  $(LDFLAGS-rtld) -Wl,-z,defs $(z-now-$(bind-now))	\
 		  $(dt-relr-ldflag) \
 		  $(filter-out $(map-file),$^) $(load-map-file)		\
-- 
2.39.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Missing CFLAGS for some objects
  2023-04-21 11:45 Missing CFLAGS for some objects Tomasz Dziendzielski
@ 2023-04-21 16:14 ` Florian Weimer
       [not found]   ` <CAJkqNs1dTX4rtgfhRrAhRXXCS2LY3tf4poj=ojAv+Chpd6dkxg@mail.gmail.com>
  0 siblings, 1 reply; 3+ messages in thread
From: Florian Weimer @ 2023-04-21 16:14 UTC (permalink / raw)
  To: Tomasz Dziendzielski via Libc-help; +Cc: Tomasz Dziendzielski

* Tomasz Dziendzielski via Libc-help:

> Hello,
> one of our developers reported that when we build libc in our environment
> using Yocto Project we end up with corrupted debug symbols even though the
> -g flag is passed to the CFLAGS variable. I noticed that in fact some gcc
> commands (about ~300) are missing -g flag in the invocation. So now my
> question is: is there some reason why some objects are built without CFLAGS?
> You can get the list of objects without debug symbols (and CFLAGS) by
> setting CFLAGS=" -g " for make and using grep for gcc or cross gcc in the
> make execution log file and grep with an invert match for ' -g '.

Those are all linker invocations as far as I can see?  The -g flag will
be dropped by the compiler anyway.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Missing CFLAGS for some objects
       [not found]   ` <CAJkqNs1dTX4rtgfhRrAhRXXCS2LY3tf4poj=ojAv+Chpd6dkxg@mail.gmail.com>
@ 2023-04-27 15:01     ` Adhemerval Zanella Netto
  0 siblings, 0 replies; 3+ messages in thread
From: Adhemerval Zanella Netto @ 2023-04-27 15:01 UTC (permalink / raw)
  To: Tomasz Dziendzielski, Florian Weimer; +Cc: Tomasz Dziendzielski via Libc-help



On 21/04/23 17:22, Tomasz Dziendzielski via Libc-help wrote:
> Hi Florian,
> thank you for replying.
> 
>> Those are all linker invocations as far as I can see?  The -g flag will
>> be dropped by the compiler anyway.
> 
> Well, that's true those are linker invocations but it doesn't mean the -g
> flag is ignored. In fact I can see the objdump -Cd with my patch is
> different and gives the correct (at least as stated as one of our
> developer) debug symbols after applying. Please see the attachments.
> I'm sorry it's png but it's the only form I have it at the moment.
> 

I have tested your patch and the generated libc.so with and without
it is essentially the same:

$ sha256sum aarch64-linux-gnu/libc.so
e04ab40662d2ae5cd80ba5791c7d27f6197d7f20f275764a7bccb19a191156bb  aarch64-linux-gnu-master/libc.so
$ sha256sum aarch64-linux-gnu-patch/libc.so
e04ab40662d2ae5cd80ba5791c7d27f6197d7f20f275764a7bccb19a191156bb  aarch64-linux-gnu/libc.so

$ size aarch64-linux-gnu/libc.so
   text    data     bss     dec     hex filename
1758667  410520   51704 2220891  21e35b aarch64-linux-gnu-master/libc.so
$ size aarch64-linux-gnu-patch/libc.so
   text    data     bss     dec     hex filename
1758667  410520   51704 2220891  21e35b aarch64-linux-gnu/libc.so

I built used the default cflags, which is the -O2 -g.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-04-27 15:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-21 11:45 Missing CFLAGS for some objects Tomasz Dziendzielski
2023-04-21 16:14 ` Florian Weimer
     [not found]   ` <CAJkqNs1dTX4rtgfhRrAhRXXCS2LY3tf4poj=ojAv+Chpd6dkxg@mail.gmail.com>
2023-04-27 15:01     ` Adhemerval Zanella Netto

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).