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 ... we still have: ... $ ./dwz -? ... -p, --multifile-pointer-size Set pointer size of multifile, in number of bytes. Native pointer size is 8. Default value: auto. ... 2021-04-12 Tom de Vries * Makefile (args.o): Add pattern rule. (native.o): New target. (clean): Update. * util.h (XSTR, STR): New macro. * args.c (NATIVE_ENDIAN_STRING): 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 | 20 +++++++++++++++++++- args.c | 38 +++++++++++++++++++++++++++++++++----- dwz.1 | 10 ++++++---- native.c | 5 +++++ util.h | 3 +++ 5 files changed, 66 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 95fc80b..23c1ebb 100644 --- a/Makefile +++ b/Makefile @@ -20,13 +20,31 @@ 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: %.o: %.c + $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $< \ + -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 $@ $< 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) 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", "", "auto", - "Set pointer size of multifile, in number of bytes." }, - { "e", "multifile-endian", "", "auto", - "Set endianity of multifile." }, + { "p", "multifile-pointer-size", "", "auto", + "Set pointer size of multifile, in number of bytes." + " Native pointer size is " XSTR (NATIVE_POINTER_SIZE) "." }, + { "e", "multifile-endian", "", "auto", + "Set endianity of multifile." + " Native endianity is " NATIVE_ENDIAN "." }, { "j", "jobs", "", "number of processors / 2", "Process 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 ] [-e ] [-j N] [FILES]", + " [-p ] [-e ] [-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 +.B \-p N \-\-multifile-pointer-size 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 \-\-multifile-endian +.B \-p \-\-multifile-endian 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