public inbox for dwz@sourceware.org
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: Michael Matz <matz@suse.de>
Cc: Mark Wielaard <mark@klomp.org>, dwz@sourceware.org, jakub@redhat.com
Subject: [committed] Add -p native and -e native
Date: Mon, 12 Apr 2021 21:53:23 +0200	[thread overview]
Message-ID: <92482943-63aa-25f0-3877-0d9326bf366d@suse.de> (raw)
In-Reply-To: <283ca25e-336f-a184-7b7d-56325b83aa15@suse.de>

[-- Attachment #1: Type: text/plain, Size: 2108 bytes --]

On 4/12/21 5:11 PM, Tom de Vries wrote:
> On 4/12/21 2:33 PM, Michael Matz wrote:
>> Hello,
>>
>> On Fri, 9 Apr 2021, Tom de Vries wrote:
>>
>>>>> Except for this narrow multilib case, doesn't this actually make it 
>>>>> impossible to do a cross-arch build?
>>>>
>>>> For cross the term "native" doesn't make sense, so it would seem valid to 
>>>> simply not support that setting with a cross (not multilib) dwz.  I.e. if 
>>>> ./native can't be executed assume cross-ness and don't support -p native.
>>>
>>> I've tried yet another variant.  Instead of trying to generate an 
>>> executable and execute it, we generate an object and test properties 
>>> using readelf.
>>>
>>> This should no longer have the cross-build problem.
>>>
>>> WDYT?
>>
>> I think using readelf should work here.  But the patch has problems:
>>
>> +#if NATIVE_ENDIAN == little
>> +#define NATIVE_ENDIAN_VAL __ORDER_LITTLE_ENDIAN__
>> +#elif NATIVE_ENDIAN == big
>> +#define NATIVE_ENDIAN_VAL __ORDER_LITTLE_ENDIAN__
>> +#else
>> +#define NATIVE_ENDIAN not available
>> +#define NATIVE_ENDIAN_VAL 0
>> +#endif
>>
>> The preprocessor works different than you seem to assume.  Unknown tokens 
>> are replaced with 0, so what you've written is the same as:
>>
>> +#if NATIVE_ENDIAN == 0
>> +#define NATIVE_ENDIAN_VAL __ORDER_LITTLE_ENDIAN__
>> +#elif NATIVE_ENDIAN == 0
>>
>> (except if you have defined 'little' and 'big' somewhere, but I can't see 
>> that?)
> 
> Oops, thanks for pointing that out.
> 
> Fixed by no longer passing -DNATIVE_ENDIAN=little, but instead
> -DNATIVE_ENDIAN_VAL=ELFDATA2LSB.
> 
>> Also __ORDER_LITTLE_ENDIAN__ is defined by GCC since 4.6, you 
>> might want to check defined-ness of it before using it.
> 
> Fixed by using -DNATIVE_ENDIAN_VAL=ELFDATA2LSB, __ORDER_LITTLE_ENDIAN__
> is no longer used.
> 
>> And you're 
>> setting NATIVE_ENDIAN_VAL to little endian in both if arms.
> 
> Oops again, fixed now.
> 

Hmm, I seem to have forgotten to run the tests, so there was still an
issue that args-for-test.o needed the same CFLAGS update as args.o.

Fixed in attached patch, committed.

Thanks,
- Tom

[-- Attachment #2: 0001-Add-p-native-and-e-native.patch --]
[-- Type: text/x-patch, Size: 7497 bytes --]

Add -p native and -e native

Add option parameter native to options -p and -e.

We determine native as the result of readelf output on an object generated
by using CC without CFLAGS, such that if we build dwz with -m32 on
x86_64 like so:
...
$ make CFLAGS="-m32 -O2 -g" LDFLAGS=-m32
...
and we have:
...
$ file ./dwz
dwz: ELF 32-bit LSB executable, Intel 80386 <SNIP>
...
we still have:
...
$ ./dwz -?
  ...
  -p, --multifile-pointer-size <SIZE|auto|native>
                              Set pointer size of multifile, in number of bytes.
                              Native pointer size is 8.
                              Default value: auto.
...

2021-04-12  Tom de Vries  <tdevries@suse.de>

	* Makefile (args.o): Add dependency on native.o.
	(CFLAGS_FOR_SOURCE, NATIVE_ENDIAN, NATIVE_ENDIAN_LITTLE)
	(NATIVE_ENDIAN_BIG, NATIVE_ENDIAN_VAL): New var.
	(%.o: %.c): Add pattern rule.
	(clean): Update.
	(native.o): Add pattern rule.
	* util.h (XSTR, STR): New macro.
	* args.c (NATIVE_ENDIAN): New macro.
	(dwz_multi_file_options_help, usage): Mention -p native and -e native.
	(parse_args): Handle -p native and -e native.
	* dwz.1 (-p, -e): Mention native.
	* native.c: New file.

---
 Makefile | 25 +++++++++++++++++++++++--
 args.c   | 38 +++++++++++++++++++++++++++++++++-----
 dwz.1    | 10 ++++++----
 native.c |  5 +++++
 util.h   |  3 +++
 5 files changed, 70 insertions(+), 11 deletions(-)

diff --git a/Makefile b/Makefile
index 95fc80b..9394ef4 100644
--- a/Makefile
+++ b/Makefile
@@ -20,13 +20,30 @@ OBJECTS = args.o dwz.o hashtab.o pool.o sha1.o dwarfnames.o
 LIBS=-lelf
 dwz: $(OBJECTS)
 	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
+args.o: native.o
+args.o: CFLAGS_FOR_SOURCE = \
+	-DNATIVE_ENDIAN_VAL=$(NATIVE_ENDIAN_VAL) \
+	-DNATIVE_POINTER_SIZE=$(NATIVE_POINTER_SIZE)
+NATIVE_ENDIAN=$(shell readelf -h native.o \
+	| grep Data \
+	| sed 's/.*, //;s/ endian//')
+NATIVE_ENDIAN_LITTLE=$(findstring $(NATIVE_ENDIAN),$(findstring little,$(NATIVE_ENDIAN)))
+NATIVE_ENDIAN_BIG=$(findstring $(NATIVE_ENDIAN),$(findstring big,$(NATIVE_ENDIAN)))
+NATIVE_ENDIAN_VAL=$(if $(NATIVE_ENDIAN_LITTLE),ELFDATA2LSB,$(if $(NATIVE_ENDIAN_BIG),ELFDATA2MSB,ELFDATANONE))
+NATIVE_POINTER_SIZE=$(shell readelf -wi native.o \
+	| grep "Pointer Size:" \
+	| sed 's/.*: *//')
+%.o: %.c
+	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $< $(CFLAGS_FOR_SOURCE)
 install: dwz
 	install -D dwz $(DESTDIR)$(bindir)/dwz
 	install -D -m 644 $(srcdir)/dwz.1 $(DESTDIR)$(mandir)/man1/dwz.1
 clean:
 	rm -f $(OBJECTS) *~ core* dwz $(TEST_EXECS) $(DWZ_TEST_OBJECTS) \
-	  dwz.log dwz.sum
+	  dwz.log dwz.sum native.o
 	rm -Rf testsuite-bin tmp.*
+native.o: native.c
+	$(CC) -o $@ $< -c -g
 
 PWD:=$(shell pwd -P)
 
@@ -59,13 +76,17 @@ DWZ_TEST_OBJECTS := $(patsubst %.o,%-for-test.o,$(OBJECTS))
 dwz-for-test: $(DWZ_TEST_OBJECTS)
 	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
 	rm -f $(DWZ_TEST_OBJECTS)
+args-for-test.o: CFLAGS_FOR_SOURCE = \
+	-DNATIVE_ENDIAN_VAL=$(NATIVE_ENDIAN_VAL) \
+	-DNATIVE_POINTER_SIZE=$(NATIVE_POINTER_SIZE)
 $(DWZ_TEST_OBJECTS): %-for-test.o : %.c
 	$(CC) $< -o $@ -c \
 	  -DUSE_GNUC=0 -DDEVEL \
 	  -O2 -g \
 	  $(CFLAGS_COMMON) \
 	  -DDWZ_VERSION='"for-test"' \
-	  $(CFLAGS_COPYRIGHT)
+	  $(CFLAGS_COPYRIGHT) \
+	  $(CFLAGS_FOR_SOURCE)
 
 min:
 	$(CC) $(TEST_SRC)/min.c $(TEST_SRC)/min-2.c -o $@ -g
diff --git a/args.c b/args.c
index cb11f25..e93c24d 100644
--- a/args.c
+++ b/args.c
@@ -220,6 +220,14 @@ static struct option_help dwz_single_file_options_help[] =
     "Place the output in OUTFILE." }
 };
 
+#if NATIVE_ENDIAN_VAL == ELFDATA2MSB
+#define NATIVE_ENDIAN "big"
+#elif NATIVE_ENDIAN_VAL == ELFDATA2LSB
+#define NATIVE_ENDIAN "little"
+#else
+#define NATIVE_ENDIAN "not available"
+#endif
+
 /* Describe mult-file command line options.  */
 static struct option_help dwz_multi_file_options_help[] =
 {
@@ -236,10 +244,12 @@ static struct option_help dwz_multi_file_options_help[] =
   { "5", "dwarf-5", NULL, NULL,
     "Emit DWARF 5 standardized supplementary object files instead of"
     " GNU extension .debug_altlink." },
-  { "p", "multifile-pointer-size", "<SIZE|auto>", "auto",
-    "Set pointer size of multifile, in number of bytes." },
-  { "e", "multifile-endian", "<l|b|auto>", "auto",
-    "Set endianity of multifile." },
+  { "p", "multifile-pointer-size", "<SIZE|auto|native>", "auto",
+     "Set pointer size of multifile, in number of bytes."
+    " Native pointer size is " XSTR (NATIVE_POINTER_SIZE) "." },
+  { "e", "multifile-endian", "<l|b|auto|native>", "auto",
+    "Set endianity of multifile."
+    " Native endianity is " NATIVE_ENDIAN "." },
   { "j", "jobs", "<n>", "number of processors / 2",
     "Process <n> files in parallel." }
 };
@@ -385,7 +395,7 @@ usage (int failing)
   FILE *stream = failing ? stderr : stdout;
   const char *header_lines[] = {
     "dwz [common options] [-h] [-m COMMONFILE] [-M NAME | -r] [-5]",
-    "    [-p <SIZE|auto>] [-e <l|b|auto>] [-j N] [FILES]",
+    "    [-p <SIZE|auto|native>] [-e <l|b|auto|native>] [-j N] [FILES]",
     "dwz [common options] -o OUTFILE FILE",
     "dwz [ -v | -? ]"
   };
@@ -650,6 +660,11 @@ parse_args (int argc, char *argv[], bool *hardlink, const char **outfile)
 	      multifile_force_ptr_size = 0;
 	      break;
 	    }
+	  if (strcmp (optarg, "native") == 0)
+	    {
+	      multifile_force_ptr_size = NATIVE_POINTER_SIZE;
+	      break;
+	    }
 	  l = strtoul (optarg, &end, 0);
 	  if (*end != '\0' || optarg == end || (unsigned int) l != l)
 	    error (1, 0, "invalid argument -l %s", optarg);
@@ -662,6 +677,19 @@ parse_args (int argc, char *argv[], bool *hardlink, const char **outfile)
 	      multifile_force_endian = 0;
 	      break;
 	    }
+	  if (strcmp (optarg, "native") == 0)
+	    {
+	      switch (NATIVE_ENDIAN_VAL)
+		{
+		case ELFDATA2MSB:
+		case ELFDATA2LSB:
+		  multifile_force_endian = NATIVE_ENDIAN_VAL;
+		  break;
+		default:
+		  error (1, 0, "Cannot determine native endian");
+		}
+	      break;
+	    }
 	  if (strlen (optarg) != 1)
 	    error (1, 0, "invalid argument -l %s", optarg);
 	  switch (optarg[0])
diff --git a/dwz.1 b/dwz.1
index 6fec6ed..1cff329 100644
--- a/dwz.1
+++ b/dwz.1
@@ -77,13 +77,15 @@ the executable or shared library to the file named in the argument
 of the \fB-m\fR option.  Either \fB-M\fR or \fB-r\fR
 option can be specified, but not both.
 .TP
-.B \-p N \-\-multifile-pointer-size <N|auto>
+.B \-p N \-\-multifile-pointer-size <N|auto|native>
 Specify the pointer size of the multifile, in bytes.  If auto, use the
-pointer size of the files, provided they match.
+pointer size of the files, provided they match.  If native, use native pointer
+size, as specified in the help message.
 .TP
-.B \-p <l|b|auto> \-\-multifile-endian <l|b|auto>
+.B \-p <l|b|auto> \-\-multifile-endian <l|b|auto|native>
 Specify the endianity of the multifile.  If auto, use the endianity of
-the files, provided they match.
+the files, provided they match.  If native, use native endianity, as specified
+in the help message.
 .TP
 .B \-q \-\-quiet
 Silence up some of the most common messages.
diff --git a/native.c b/native.c
new file mode 100644
index 0000000..398ec67
--- /dev/null
+++ b/native.c
@@ -0,0 +1,5 @@
+int
+main (void)
+{
+  return 0;
+}
diff --git a/util.h b/util.h
index 7caac06..d542942 100644
--- a/util.h
+++ b/util.h
@@ -25,6 +25,9 @@
 #define MAX(A, B) ((A) > (B) ? (A) : (B))
 #define MIN(A, B) ((A) < (B) ? (A) : (B))
 
+#define XSTR(s) STR(s)
+#define STR(s) #s
+
 #ifndef USE_GNUC
 #ifdef __GNUC__
 #define USE_GNUC 1

  reply	other threads:[~2021-04-12 19:53 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-09  9:24 [PATCH] " Tom de Vries
2021-04-09  9:42 ` Mark Wielaard
2021-04-09 12:48   ` Tom de Vries
2021-04-09 13:03   ` Michael Matz
2021-04-09 15:58     ` Tom de Vries
2021-04-12 12:33       ` Michael Matz
2021-04-12 15:11         ` Tom de Vries
2021-04-12 19:53           ` Tom de Vries [this message]
2021-04-12 20:14       ` Mark Wielaard
2021-04-13  7:45         ` Tom de Vries
2021-04-13  8:33           ` Tom de Vries
2021-04-13 10:04             ` Mark Wielaard
2021-04-13 11:15               ` Tom de Vries

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=92482943-63aa-25f0-3877-0d9326bf366d@suse.de \
    --to=tdevries@suse.de \
    --cc=dwz@sourceware.org \
    --cc=jakub@redhat.com \
    --cc=mark@klomp.org \
    --cc=matz@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).