public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32
@ 2022-11-14 15:03 Brett Werling
  2022-11-14 15:30 ` Jan Beulich
                   ` (2 more replies)
  0 siblings, 3 replies; 24+ messages in thread
From: Brett Werling @ 2022-11-14 15:03 UTC (permalink / raw)
  To: binutils; +Cc: Brett Werling

Switch all fseek calls to fseeko and cast the given offset as an off_t
accordingly. When building readelf for x86_64-mingw32, a long will only
be 32 bits wide. If the elf file in question is >= 2 GiB, that is
greater than the max long value, and therefore fseek will fail
indicating that the offset is negative.

To work around this and support up to 4 GiB, we switch to using fseeko
and cast the unsigned long offsets as off_t values because the size of
off_t is 64 bits on x86_64-mingw32.
---
 binutils/readelf.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/binutils/readelf.c b/binutils/readelf.c
index 6b5bebe743f..a705850af35 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -424,7 +424,7 @@ get_data (void *         var,
       return NULL;
     }
 
-  if (fseek (filedata->handle, archive_file_offset + offset, SEEK_SET))
+  if (fseeko (filedata->handle, (off_t)archive_file_offset + offset, SEEK_SET))
     {
       if (reason)
 	error (_("Unable to seek to 0x%lx for %s\n"),
@@ -5343,7 +5343,7 @@ process_program_headers (Filedata * filedata)
 	  break;
 
 	case PT_INTERP:
-	  if (fseek (filedata->handle, archive_file_offset + (long) segment->p_offset,
+	  if (fseeko (filedata->handle, (off_t)archive_file_offset + (long) segment->p_offset,
 		     SEEK_SET))
 	    error (_("Unable to find program interpreter name\n"));
 	  else
@@ -11720,8 +11720,8 @@ process_symbol_table (Filedata * filedata)
 	  && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
 	hash_ent_size = 8;
 
-      if (fseek (filedata->handle,
-		 (archive_file_offset
+      if (fseeko (filedata->handle,
+		 ((off_t)archive_file_offset
 		  + offset_from_vma (filedata, dynamic_info[DT_HASH],
 				     sizeof nb + sizeof nc)),
 		 SEEK_SET))
@@ -11772,8 +11772,8 @@ process_symbol_table (Filedata * filedata)
       bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
       bfd_vma buckets_vma;
 
-      if (fseek (filedata->handle,
-		 (archive_file_offset
+      if (fseeko (filedata->handle,
+		 ((off_t)archive_file_offset
 		  + offset_from_vma (filedata, dynamic_info_DT_GNU_HASH,
 				     sizeof nb)),
 		 SEEK_SET))
@@ -11797,8 +11797,8 @@ process_symbol_table (Filedata * filedata)
       else
 	buckets_vma += bitmaskwords * 8;
 
-      if (fseek (filedata->handle,
-		 (archive_file_offset
+      if (fseeko (filedata->handle,
+		 ((off_t)archive_file_offset
 		  + offset_from_vma (filedata, buckets_vma, 4)),
 		 SEEK_SET))
 	{
@@ -11826,8 +11826,8 @@ process_symbol_table (Filedata * filedata)
 
       maxchain -= gnusymidx;
 
-      if (fseek (filedata->handle,
-		 (archive_file_offset
+      if (fseeko (filedata->handle,
+		 ((off_t)archive_file_offset
 		  + offset_from_vma (filedata, buckets_vma
 					   + 4 * (ngnubuckets + maxchain), 4)),
 		 SEEK_SET))
@@ -11851,8 +11851,8 @@ process_symbol_table (Filedata * filedata)
 	}
       while ((byte_get (nb, 4) & 1) == 0);
 
-      if (fseek (filedata->handle,
-		 (archive_file_offset
+      if (fseeko (filedata->handle,
+		 ((off_t)archive_file_offset
 		  + offset_from_vma (filedata, buckets_vma + 4 * ngnubuckets, 4)),
 		 SEEK_SET))
 	{
@@ -11868,8 +11868,8 @@ process_symbol_table (Filedata * filedata)
 
       if (dynamic_info_DT_MIPS_XHASH)
 	{
-	  if (fseek (filedata->handle,
-		     (archive_file_offset
+	  if (fseeko (filedata->handle,
+		     ((off_t)archive_file_offset
 		      + offset_from_vma (filedata, (buckets_vma
 						    + 4 * (ngnubuckets
 							   + maxchain)), 4)),
@@ -20185,7 +20185,7 @@ process_archive (Filedata * filedata, bfd_boolean is_thin_archive)
 	      ret = FALSE;
 	    }
 
-	  if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
+	  if (fseeko (filedata->handle, (off_t)current_pos, SEEK_SET) != 0)
 	    {
 	      error (_("%s: failed to seek back to start of object files in the archive\n"),
 		     filedata->file_name);
@@ -20211,7 +20211,7 @@ process_archive (Filedata * filedata, bfd_boolean is_thin_archive)
       char * qualified_name;
 
       /* Read the next archive header.  */
-      if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
+      if (fseeko (filedata->handle, (off_t)arch.next_arhdr_offset, SEEK_SET) != 0)
         {
           error (_("%s: failed to seek to next archive header\n"), arch.file_name);
           return FALSE;
@@ -20309,7 +20309,7 @@ process_archive (Filedata * filedata, bfd_boolean is_thin_archive)
 
           /* The nested archive file will have been opened and setup by
              get_archive_member_name.  */
-          if (fseek (nested_arch.file, archive_file_offset, SEEK_SET) != 0)
+          if (fseeko (nested_arch.file, (off_t)archive_file_offset, SEEK_SET) != 0)
             {
               error (_("%s: failed to seek to archive member.\n"), nested_arch.file_name);
               ret = FALSE;
-- 
2.38.1


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

* Re: [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32
  2022-11-14 15:03 [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32 Brett Werling
@ 2022-11-14 15:30 ` Jan Beulich
  2022-11-14 15:52   ` Brett Werling
  2022-11-15 14:57 ` [PATCH] readelf: use fseeko64 or fseeko if possible Brett Werling
  2022-11-17 14:34 ` Brett Werling
  2 siblings, 1 reply; 24+ messages in thread
From: Jan Beulich @ 2022-11-14 15:30 UTC (permalink / raw)
  To: Brett Werling; +Cc: binutils

On 14.11.2022 16:03, Brett Werling via Binutils wrote:
> Switch all fseek calls to fseeko and cast the given offset as an off_t
> accordingly. When building readelf for x86_64-mingw32, a long will only
> be 32 bits wide. If the elf file in question is >= 2 GiB, that is
> greater than the max long value, and therefore fseek will fail
> indicating that the offset is negative.
> 
> To work around this and support up to 4 GiB, we switch to using fseeko
> and cast the unsigned long offsets as off_t values because the size of
> off_t is 64 bits on x86_64-mingw32.

Is fseeko() uniformly available on all platforms binutils can be built
for? I'm afraid the answer is no, so at least you'd need to introduce
some configure logic for this plus some abstraction.

Jan


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

* Re: [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32
  2022-11-14 15:30 ` Jan Beulich
@ 2022-11-14 15:52   ` Brett Werling
  2022-11-14 21:42     ` Alan Modra
  0 siblings, 1 reply; 24+ messages in thread
From: Brett Werling @ 2022-11-14 15:52 UTC (permalink / raw)
  To: Jan Beulich; +Cc: binutils

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

On Mon, Nov 14, 2022 at 9:30 AM Jan Beulich <jbeulich@suse.com> wrote:

> On 14.11.2022 16:03, Brett Werling via Binutils wrote:
> > Switch all fseek calls to fseeko and cast the given offset as an off_t
> > accordingly. When building readelf for x86_64-mingw32, a long will only
> > be 32 bits wide. If the elf file in question is >= 2 GiB, that is
> > greater than the max long value, and therefore fseek will fail
> > indicating that the offset is negative.
> >
> > To work around this and support up to 4 GiB, we switch to using fseeko
> > and cast the unsigned long offsets as off_t values because the size of
> > off_t is 64 bits on x86_64-mingw32.
>
> Is fseeko() uniformly available on all platforms binutils can be built
> for? I'm afraid the answer is no, so at least you'd need to introduce
> some configure logic for this plus some abstraction.
>
> Jan
>

I think you are correct, this will need some conditional logic to be "safe"
to
include, and even then the casting to off_t would become a little more
complicated. I will look deeper into what can be done here.

I had also considered iterative calls on fseek() using longs and SEEK_CUR,
just ensuring that we don't blow past LONG_MAX. That didn't seem as
clean or efficient of a change, so I dropped it.

Brett

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

* Re: [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32
  2022-11-14 15:52   ` Brett Werling
@ 2022-11-14 21:42     ` Alan Modra
  2022-11-16 10:09       ` Mike Frysinger
  0 siblings, 1 reply; 24+ messages in thread
From: Alan Modra @ 2022-11-14 21:42 UTC (permalink / raw)
  To: Brett Werling; +Cc: Jan Beulich, binutils

On Mon, Nov 14, 2022 at 09:52:30AM -0600, Brett Werling via Binutils wrote:
> On Mon, Nov 14, 2022 at 9:30 AM Jan Beulich <jbeulich@suse.com> wrote:
> 
> > On 14.11.2022 16:03, Brett Werling via Binutils wrote:
> > > Switch all fseek calls to fseeko and cast the given offset as an off_t
> > > accordingly. When building readelf for x86_64-mingw32, a long will only
> > > be 32 bits wide. If the elf file in question is >= 2 GiB, that is
> > > greater than the max long value, and therefore fseek will fail
> > > indicating that the offset is negative.
> > >
> > > To work around this and support up to 4 GiB, we switch to using fseeko
> > > and cast the unsigned long offsets as off_t values because the size of
> > > off_t is 64 bits on x86_64-mingw32.
> >
> > Is fseeko() uniformly available on all platforms binutils can be built
> > for? I'm afraid the answer is no, so at least you'd need to introduce
> > some configure logic for this plus some abstraction.
> >
> > Jan
> >
> 
> I think you are correct, this will need some conditional logic to be "safe"
> to
> include, and even then the casting to off_t would become a little more
> complicated. I will look deeper into what can be done here.

See bfd/bfdio.c and bfd/configure.ac

> I had also considered iterative calls on fseek() using longs and SEEK_CUR,
> just ensuring that we don't blow past LONG_MAX. That didn't seem as
> clean or efficient of a change, so I dropped it.
> 
> Brett

-- 
Alan Modra
Australia Development Lab, IBM

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

* [PATCH] readelf: use fseeko64 or fseeko if possible
  2022-11-14 15:03 [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32 Brett Werling
  2022-11-14 15:30 ` Jan Beulich
@ 2022-11-15 14:57 ` Brett Werling
  2022-11-17  7:02   ` Alan Modra
  2022-11-17 14:34 ` Brett Werling
  2 siblings, 1 reply; 24+ messages in thread
From: Brett Werling @ 2022-11-15 14:57 UTC (permalink / raw)
  To: binutils; +Cc: Brett Werling

Changes readelf to make use first of fseeko64 and then fseeko,
depending on which of those is available. If neither is available,
reverts to the previous behavior of using fseek. In all cases, the
offset argument given is cast accoridngly.

This is necessary when building readelf for LLP64 systems, where a
long will only be 32 bits wide. If the elf file in question is >= 2 GiB,
that is greater than the max long value and therefore fseek will fail
indicating that the offset is negative. On such systems, making use of
fseeko64 or fseeko will result in the ability so seek past the 2 GiB
max long boundary.
---
 binutils/config.in |  6 ++++++
 binutils/configure |  2 +-
 binutils/readelf.c | 47 ++++++++++++++++++++++++++++------------------
 3 files changed, 36 insertions(+), 19 deletions(-)

diff --git a/binutils/config.in b/binutils/config.in
index 4d22a80971b..91fe00af777 100644
--- a/binutils/config.in
+++ b/binutils/config.in
@@ -67,6 +67,12 @@
 /* Define to 1 if you have the <fcntl.h> header file. */
 #undef HAVE_FCNTL_H
 
+/* Define to 1 if you have the `fseeko' function. */
+#undef HAVE_FSEEKO
+
+/* Define to 1 if you have the `fseeko64' function. */
+#undef HAVE_FSEEKO64
+
 /* Define to 1 if you have the `getc_unlocked' function. */
 #undef HAVE_GETC_UNLOCKED
 
diff --git a/binutils/configure b/binutils/configure
index 6176d699e57..46519a31701 100755
--- a/binutils/configure
+++ b/binutils/configure
@@ -13155,7 +13155,7 @@ $as_echo "#define HAVE_MMAP 1" >>confdefs.h
 fi
 rm -f conftest.mmap conftest.txt
 
-for ac_func in getc_unlocked mkdtemp mkstemp utimensat utimes
+for ac_func in getc_unlocked mkdtemp mkstemp utimensat utimes fseeko fseeko64
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/binutils/readelf.c b/binutils/readelf.c
index c8323539a21..3e5e4f29ff2 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -177,6 +177,17 @@
 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
 #endif
 
+#if defined (HAVE_FSEEKO64)
+#define FSEEK_FUNC fseeko64
+#define FSEEK_OFF_TYPE off64_t
+#elif defined (HAVE_FSEEKO)
+#define FSEEK_FUNC fseeko
+#define FSEEK_OFF_TYPE off_t
+#else
+#define FSEEK_FUNC fseek
+#define FSEEK_OFF_TYPE long
+#endif
+
 typedef struct elf_section_list
 {
   Elf_Internal_Shdr *        hdr;
@@ -479,7 +490,7 @@ get_data (void *var,
       return NULL;
     }
 
-  if (fseek (filedata->handle, filedata->archive_file_offset + offset,
+  if (FSEEK_FUNC (filedata->handle, (FSEEK_OFF_TYPE)filedata->archive_file_offset + offset,
 	     SEEK_SET))
     {
       if (reason)
@@ -6262,8 +6273,8 @@ the .dynamic section is not the same as the dynamic segment\n"));
 	  if (segment->p_offset >= filedata->file_size
 	      || segment->p_filesz > filedata->file_size - segment->p_offset
 	      || segment->p_filesz - 1 >= (size_t) -2
-	      || fseek (filedata->handle,
-			filedata->archive_file_offset + (long) segment->p_offset,
+	      || FSEEK_FUNC (filedata->handle,
+			(FSEEK_OFF_TYPE)filedata->archive_file_offset + (long) segment->p_offset,
 			SEEK_SET))
 	    error (_("Unable to find program interpreter name\n"));
 	  else
@@ -11035,8 +11046,8 @@ get_num_dynamic_syms (Filedata * filedata)
 	  && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
 	hash_ent_size = 8;
 
-      if (fseek (filedata->handle,
-		 (filedata->archive_file_offset
+      if (FSEEK_FUNC (filedata->handle,
+		 ((FSEEK_OFF_TYPE)filedata->archive_file_offset
 		  + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
 				     sizeof nb + sizeof nc)),
 		 SEEK_SET))
@@ -11088,8 +11099,8 @@ get_num_dynamic_syms (Filedata * filedata)
       uint64_t buckets_vma;
       unsigned long hn;
 
-      if (fseek (filedata->handle,
-		 (filedata->archive_file_offset
+      if (FSEEK_FUNC (filedata->handle,
+		 ((FSEEK_OFF_TYPE)filedata->archive_file_offset
 		  + offset_from_vma (filedata,
 				     filedata->dynamic_info_DT_GNU_HASH,
 				     sizeof nb)),
@@ -11114,8 +11125,8 @@ get_num_dynamic_syms (Filedata * filedata)
       else
 	buckets_vma += bitmaskwords * 8;
 
-      if (fseek (filedata->handle,
-		 (filedata->archive_file_offset
+      if (FSEEK_FUNC (filedata->handle,
+		 ((FSEEK_OFF_TYPE)filedata->archive_file_offset
 		  + offset_from_vma (filedata, buckets_vma, 4)),
 		 SEEK_SET))
 	{
@@ -11144,8 +11155,8 @@ get_num_dynamic_syms (Filedata * filedata)
 
       maxchain -= filedata->gnusymidx;
 
-      if (fseek (filedata->handle,
-		 (filedata->archive_file_offset
+      if (FSEEK_FUNC (filedata->handle,
+		 ((FSEEK_OFF_TYPE)filedata->archive_file_offset
 		  + offset_from_vma (filedata,
 				     buckets_vma + 4 * (filedata->ngnubuckets
 							+ maxchain),
@@ -11171,8 +11182,8 @@ get_num_dynamic_syms (Filedata * filedata)
 	}
       while ((byte_get (nb, 4) & 1) == 0);
 
-      if (fseek (filedata->handle,
-		 (filedata->archive_file_offset
+      if (FSEEK_FUNC (filedata->handle,
+		 ((FSEEK_OFF_TYPE)filedata->archive_file_offset
 		  + offset_from_vma (filedata, (buckets_vma
 						+ 4 * filedata->ngnubuckets),
 				     4)),
@@ -11190,8 +11201,8 @@ get_num_dynamic_syms (Filedata * filedata)
 
       if (filedata->dynamic_info_DT_MIPS_XHASH)
 	{
-	  if (fseek (filedata->handle,
-		     (filedata->archive_file_offset
+	  if (FSEEK_FUNC (filedata->handle,
+		     ((FSEEK_OFF_TYPE)filedata->archive_file_offset
 		      + offset_from_vma (filedata, (buckets_vma
 						    + 4 * (filedata->ngnubuckets
 							   + maxchain)), 4)),
@@ -22546,7 +22557,7 @@ process_archive (Filedata * filedata, bool is_thin_archive)
 	      ret = false;
 	    }
 
-	  if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
+	  if (FSEEK_FUNC (filedata->handle, (FSEEK_OFF_TYPE)current_pos, SEEK_SET) != 0)
 	    {
 	      error (_("%s: failed to seek back to start of object files "
 		       "in the archive\n"),
@@ -22573,7 +22584,7 @@ process_archive (Filedata * filedata, bool is_thin_archive)
       char * qualified_name;
 
       /* Read the next archive header.  */
-      if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
+      if (FSEEK_FUNC (filedata->handle, (FSEEK_OFF_TYPE)arch.next_arhdr_offset, SEEK_SET) != 0)
 	{
 	  error (_("%s: failed to seek to next archive header\n"),
 		 arch.file_name);
@@ -22683,7 +22694,7 @@ process_archive (Filedata * filedata, bool is_thin_archive)
 
 	  /* The nested archive file will have been opened and setup by
 	     get_archive_member_name.  */
-	  if (fseek (nested_arch.file, filedata->archive_file_offset,
+	  if (FSEEK_FUNC (nested_arch.file, (FSEEK_OFF_TYPE)filedata->archive_file_offset,
 		     SEEK_SET) != 0)
 	    {
 	      error (_("%s: failed to seek to archive member.\n"),
-- 
2.38.1


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

* Re: [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32
  2022-11-14 21:42     ` Alan Modra
@ 2022-11-16 10:09       ` Mike Frysinger
  2022-11-16 10:46         ` Jan Beulich
  0 siblings, 1 reply; 24+ messages in thread
From: Mike Frysinger @ 2022-11-16 10:09 UTC (permalink / raw)
  To: Alan Modra; +Cc: Brett Werling, Jan Beulich, binutils

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

On 15 Nov 2022 08:12, Alan Modra via Binutils wrote:
> On Mon, Nov 14, 2022 at 09:52:30AM -0600, Brett Werling via Binutils wrote:
> > On Mon, Nov 14, 2022 at 9:30 AM Jan Beulich <jbeulich@suse.com> wrote:
> > > On 14.11.2022 16:03, Brett Werling via Binutils wrote:
> > > > Switch all fseek calls to fseeko and cast the given offset as an off_t
> > > > accordingly. When building readelf for x86_64-mingw32, a long will only
> > > > be 32 bits wide. If the elf file in question is >= 2 GiB, that is
> > > > greater than the max long value, and therefore fseek will fail
> > > > indicating that the offset is negative.
> > > >
> > > > To work around this and support up to 4 GiB, we switch to using fseeko
> > > > and cast the unsigned long offsets as off_t values because the size of
> > > > off_t is 64 bits on x86_64-mingw32.
> > >
> > > Is fseeko() uniformly available on all platforms binutils can be built
> > > for? I'm afraid the answer is no, so at least you'd need to introduce
> > > some configure logic for this plus some abstraction.
> > 
> > I think you are correct, this will need some conditional logic to be "safe"
> > to
> > include, and even then the casting to off_t would become a little more
> > complicated. I will look deeper into what can be done here.
> 
> See bfd/bfdio.c and bfd/configure.ac

should we look at bfd using gnulib ?  growing our own portability layer sounds
like a lot of dupicative effort ...

at the very least, binutils/ should be able to without much trouble.
-mike

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32
  2022-11-16 10:09       ` Mike Frysinger
@ 2022-11-16 10:46         ` Jan Beulich
  2022-11-16 14:01           ` Mike Frysinger
  0 siblings, 1 reply; 24+ messages in thread
From: Jan Beulich @ 2022-11-16 10:46 UTC (permalink / raw)
  To: Mike Frysinger; +Cc: Alan Modra, Brett Werling, binutils

On 16.11.2022 11:09, Mike Frysinger wrote:
> On 15 Nov 2022 08:12, Alan Modra via Binutils wrote:
>> On Mon, Nov 14, 2022 at 09:52:30AM -0600, Brett Werling via Binutils wrote:
>>> On Mon, Nov 14, 2022 at 9:30 AM Jan Beulich <jbeulich@suse.com> wrote:
>>>> On 14.11.2022 16:03, Brett Werling via Binutils wrote:
>>>>> Switch all fseek calls to fseeko and cast the given offset as an off_t
>>>>> accordingly. When building readelf for x86_64-mingw32, a long will only
>>>>> be 32 bits wide. If the elf file in question is >= 2 GiB, that is
>>>>> greater than the max long value, and therefore fseek will fail
>>>>> indicating that the offset is negative.
>>>>>
>>>>> To work around this and support up to 4 GiB, we switch to using fseeko
>>>>> and cast the unsigned long offsets as off_t values because the size of
>>>>> off_t is 64 bits on x86_64-mingw32.
>>>>
>>>> Is fseeko() uniformly available on all platforms binutils can be built
>>>> for? I'm afraid the answer is no, so at least you'd need to introduce
>>>> some configure logic for this plus some abstraction.
>>>
>>> I think you are correct, this will need some conditional logic to be "safe"
>>> to
>>> include, and even then the casting to off_t would become a little more
>>> complicated. I will look deeper into what can be done here.
>>
>> See bfd/bfdio.c and bfd/configure.ac
> 
> should we look at bfd using gnulib ?  growing our own portability layer sounds
> like a lot of dupicative effort ...

Right now binutils can be built and run on pretty old distros. Unless a pretty
old gnulib would be taken as the baseline, this property would be lost with
the introduction of such a dependency.

Jan

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

* Re: [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32
  2022-11-16 10:46         ` Jan Beulich
@ 2022-11-16 14:01           ` Mike Frysinger
  2022-11-16 14:44             ` Michael Matz
  0 siblings, 1 reply; 24+ messages in thread
From: Mike Frysinger @ 2022-11-16 14:01 UTC (permalink / raw)
  To: Jan Beulich; +Cc: Alan Modra, Brett Werling, binutils

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

On 16 Nov 2022 11:46, Jan Beulich wrote:
> On 16.11.2022 11:09, Mike Frysinger wrote:
> > On 15 Nov 2022 08:12, Alan Modra via Binutils wrote:
> >> On Mon, Nov 14, 2022 at 09:52:30AM -0600, Brett Werling via Binutils wrote:
> >>> On Mon, Nov 14, 2022 at 9:30 AM Jan Beulich <jbeulich@suse.com> wrote:
> >>>> On 14.11.2022 16:03, Brett Werling via Binutils wrote:
> >>>>> Switch all fseek calls to fseeko and cast the given offset as an off_t
> >>>>> accordingly. When building readelf for x86_64-mingw32, a long will only
> >>>>> be 32 bits wide. If the elf file in question is >= 2 GiB, that is
> >>>>> greater than the max long value, and therefore fseek will fail
> >>>>> indicating that the offset is negative.
> >>>>>
> >>>>> To work around this and support up to 4 GiB, we switch to using fseeko
> >>>>> and cast the unsigned long offsets as off_t values because the size of
> >>>>> off_t is 64 bits on x86_64-mingw32.
> >>>>
> >>>> Is fseeko() uniformly available on all platforms binutils can be built
> >>>> for? I'm afraid the answer is no, so at least you'd need to introduce
> >>>> some configure logic for this plus some abstraction.
> >>>
> >>> I think you are correct, this will need some conditional logic to be "safe"
> >>> to
> >>> include, and even then the casting to off_t would become a little more
> >>> complicated. I will look deeper into what can be done here.
> >>
> >> See bfd/bfdio.c and bfd/configure.ac
> > 
> > should we look at bfd using gnulib ?  growing our own portability layer sounds
> > like a lot of dupicative effort ...
> 
> Right now binutils can be built and run on pretty old distros. Unless a pretty
> old gnulib would be taken as the baseline, this property would be lost with
> the introduction of such a dependency.

sounds like an issue to bring up with the gnulib folks then ?
it's already a hard requirement for gdb and related.

realistically, is anyone actually testing those old distros ?
-mike

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32
  2022-11-16 14:01           ` Mike Frysinger
@ 2022-11-16 14:44             ` Michael Matz
  2022-11-16 15:40               ` Mike Frysinger
  0 siblings, 1 reply; 24+ messages in thread
From: Michael Matz @ 2022-11-16 14:44 UTC (permalink / raw)
  To: Mike Frysinger; +Cc: Jan Beulich, Alan Modra, Brett Werling, binutils

Hello,

On Wed, 16 Nov 2022, Mike Frysinger via Binutils wrote:

> > >>> I think you are correct, this will need some conditional logic to be "safe"
> > >>> to
> > >>> include, and even then the casting to off_t would become a little more
> > >>> complicated. I will look deeper into what can be done here.
> > >>
> > >> See bfd/bfdio.c and bfd/configure.ac
> > > 
> > > should we look at bfd using gnulib ?

The patch is for readelf, which doesn't use libbfd.  (And if readelf 
should use gnulib for this?  I don't know, seems overkill for a single 
function).

> > > growing our own portability layer sounds
> > > like a lot of dupicative effort ...

I think "growing" and "lot of" don't describe the effort.  Rewriting to 
use gnulib (and the corresponding necessary testing) might be trivial as 
well (though I don't think so), but the returns are still very small.

> realistically, is anyone actually testing those old distros ?

Depends on the definition of old.  For instance our oldish enterprise 
stuff (SLES12) still has current binutils, so at least there it's tested 
relatively good.  OTOH it's only 9 years old, so some might say that 
doesn't qualify :)


Ciao,
Michael.

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

* Re: [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32
  2022-11-16 14:44             ` Michael Matz
@ 2022-11-16 15:40               ` Mike Frysinger
  2022-11-16 15:58                 ` Jose E. Marchesi
  2022-11-16 16:13                 ` Michael Matz
  0 siblings, 2 replies; 24+ messages in thread
From: Mike Frysinger @ 2022-11-16 15:40 UTC (permalink / raw)
  To: Michael Matz; +Cc: Jan Beulich, Alan Modra, Brett Werling, binutils

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

On 16 Nov 2022 14:44, Michael Matz wrote:
> On Wed, 16 Nov 2022, Mike Frysinger via Binutils wrote:
> > > >>> I think you are correct, this will need some conditional logic to be "safe"
> > > >>> to
> > > >>> include, and even then the casting to off_t would become a little more
> > > >>> complicated. I will look deeper into what can be done here.
> > > >>
> > > >> See bfd/bfdio.c and bfd/configure.ac
> > > > 
> > > > should we look at bfd using gnulib ?
> 
> The patch is for readelf, which doesn't use libbfd.

i'm aware.  my original message called this difference out.  using gnulib
in libbfd seems dicey when statically linking is common, and gnulib does
not provide symbol isolation, which means we'd see a lot of bleeding.

> (And if readelf 
> should use gnulib for this?  I don't know, seems overkill for a single 
> function).
> 
> > > > growing our own portability layer sounds
> > > > like a lot of dupicative effort ...
> 
> I think "growing" and "lot of" don't describe the effort.  Rewriting to 
> use gnulib (and the corresponding necessary testing) might be trivial as 
> well (though I don't think so), but the returns are still very small.

i disagree with your assessment.  the patch proposed isn't exactly trivial,
nor is it widely tested like gnulib.

further, if you read the existing binutils/configure.ac, there's a lot of
"copied from gnulib" logic in there that would get cleaned up.  there's
also logic for old systems that i doubt aren't actively tested (e.g. who
is actually building on Next 3.2?).

even further, readelf isn't the only file under binutils/ that uses fseek,
but it seems to be the only program you're trying to fix.  so the others
are still broken, which wouldn't be the case if gnulib was used.

even even further, gnulib is already in the tree.  it isn't like you have
to import & integrate its build logic or manage its autotool logic or its
header paths.  there are 3 top-level projects using it now, one of which
i converted, and it was pretty trivial.

> > realistically, is anyone actually testing those old distros ?
> 
> Depends on the definition of old.  For instance our oldish enterprise 
> stuff (SLES12) still has current binutils, so at least there it's tested 
> relatively good.  OTOH it's only 9 years old, so some might say that 
> doesn't qualify :)

assuming it shipped with a glibc version that supported POSIX 1003.1 (2013
edition), no, i don't consider that to be that old.  it's got all the fun
*at APIs and such.  i don't recall what the 2016 edition introduced, and i
don't think there are easy summaries to find, but i doubt much of it would
be relevant for binutils/.

are you claiming that current gnulib versions can't support even those old
versions of glibc ?  i'd find that extremely surprising, and really sounds
like a bug that should be taken up with the gnulib folks.
-mike

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32
  2022-11-16 15:40               ` Mike Frysinger
@ 2022-11-16 15:58                 ` Jose E. Marchesi
  2022-11-16 16:13                 ` Michael Matz
  1 sibling, 0 replies; 24+ messages in thread
From: Jose E. Marchesi @ 2022-11-16 15:58 UTC (permalink / raw)
  To: Mike Frysinger
  Cc: Jan Beulich, Alan Modra, Brett Werling, binutils, Michael Matz


> [... snip ...]
>> I think "growing" and "lot of" don't describe the effort.  Rewriting to 
>> use gnulib (and the corresponding necessary testing) might be trivial as 
>> well (though I don't think so), but the returns are still very small.
>
> i disagree with your assessment.  the patch proposed isn't exactly trivial,
> nor is it widely tested like gnulib.
>
> further, if you read the existing binutils/configure.ac, there's a lot of
> "copied from gnulib" logic in there that would get cleaned up.  there's
> also logic for old systems that i doubt aren't actively tested (e.g. who
> is actually building on Next 3.2?).
>
> even further, readelf isn't the only file under binutils/ that uses fseek,
> but it seems to be the only program you're trying to fix.  so the others
> are still broken, which wouldn't be the case if gnulib was used.
>
> even even further, gnulib is already in the tree.  it isn't like you have
> to import & integrate its build logic or manage its autotool logic or its
> header paths.  there are 3 top-level projects using it now, one of which
> i converted, and it was pretty trivial.

FWIW, we are considering making BFD to import a gnulib module with the
sframe encoders and decoders (we already submitted the new module to
bug-gnulib and it is under review) rather than having to maintain a
separated libsframe in the tree.

I also agree with Mike in that using gnulib in bfd would bring many
other advantages...

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

* Re: [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32
  2022-11-16 15:40               ` Mike Frysinger
  2022-11-16 15:58                 ` Jose E. Marchesi
@ 2022-11-16 16:13                 ` Michael Matz
  2022-11-16 17:09                   ` Mike Frysinger
  1 sibling, 1 reply; 24+ messages in thread
From: Michael Matz @ 2022-11-16 16:13 UTC (permalink / raw)
  To: Mike Frysinger; +Cc: Jan Beulich, Alan Modra, Brett Werling, binutils

Hello,

On Wed, 16 Nov 2022, Mike Frysinger wrote:

> are you claiming that current gnulib versions can't support even those old
> versions of glibc ?  i'd find that extremely surprising, and really sounds
> like a bug that should be taken up with the gnulib folks.

No.  I'm claiming that moving to gnulib is work (even if perhaps not 
much?) and change and I (personally) don't see the need to invest into 
that work and deal with the change.  Had you asked "should I look into 
using gnulib in bfd" my answer would have been "well, try it, why not?".  
But you asked "should _we_ look" (aka should someone do that), hence my 
answer was in the line of "perhaps, but why bother?".

So, I'm not at all doubting that gnulib would be fit for the purpose, it 
most likely is.  It just seems (to me!) like a solution in search for a 
problem.


Ciao,
Michael.

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

* Re: [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32
  2022-11-16 16:13                 ` Michael Matz
@ 2022-11-16 17:09                   ` Mike Frysinger
  2022-11-16 21:19                     ` Brett Werling
  2022-11-17 13:21                     ` Michael Matz
  0 siblings, 2 replies; 24+ messages in thread
From: Mike Frysinger @ 2022-11-16 17:09 UTC (permalink / raw)
  To: Michael Matz; +Cc: Jan Beulich, Alan Modra, Brett Werling, binutils

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

On 16 Nov 2022 16:13, Michael Matz wrote:
> On Wed, 16 Nov 2022, Mike Frysinger wrote:
> > are you claiming that current gnulib versions can't support even those old
> > versions of glibc ?  i'd find that extremely surprising, and really sounds
> > like a bug that should be taken up with the gnulib folks.
> 
> No.  I'm claiming that moving to gnulib is work (even if perhaps not 
> much?) and change and I (personally) don't see the need to invest into 
> that work and deal with the change.  Had you asked "should I look into 
> using gnulib in bfd" my answer would have been "well, try it, why not?".  
> But you asked "should _we_ look" (aka should someone do that), hence my 
> answer was in the line of "perhaps, but why bother?".

you're proposing adding a not insignificant amount of custom portability logic
and making the readelf code harder to understand & maintain while also not
fixing the same bug in other binutils/ tools.  it is perfectly reasonable to
ask you to not add tech debt & make the code base worse off for everyone else
to maintain, and instead focus on a common solution that the GNU project is
already heavily invested in.  this is, of course, the "GNU Binutils" project.
-mike

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32
  2022-11-16 17:09                   ` Mike Frysinger
@ 2022-11-16 21:19                     ` Brett Werling
  2022-11-17  8:02                       ` Mike Frysinger
  2022-11-17 13:21                     ` Michael Matz
  1 sibling, 1 reply; 24+ messages in thread
From: Brett Werling @ 2022-11-16 21:19 UTC (permalink / raw)
  To: Michael Matz, Jan Beulich, Alan Modra, Brett Werling, binutils

On Wed, Nov 16, 2022 at 11:09 AM Mike Frysinger <vapier@gentoo.org> wrote:
>
> On 16 Nov 2022 16:13, Michael Matz wrote:
> > On Wed, 16 Nov 2022, Mike Frysinger wrote:
> > > are you claiming that current gnulib versions can't support even those old
> > > versions of glibc ?  i'd find that extremely surprising, and really sounds
> > > like a bug that should be taken up with the gnulib folks.
> >
> > No.  I'm claiming that moving to gnulib is work (even if perhaps not
> > much?) and change and I (personally) don't see the need to invest into
> > that work and deal with the change.  Had you asked "should I look into
> > using gnulib in bfd" my answer would have been "well, try it, why not?".
> > But you asked "should _we_ look" (aka should someone do that), hence my
> > answer was in the line of "perhaps, but why bother?".
>
> you're proposing adding a not insignificant amount of custom portability logic
> and making the readelf code harder to understand & maintain while also not
> fixing the same bug in other binutils/ tools.  it is perfectly reasonable to
> ask you to not add tech debt & make the code base worse off for everyone else
> to maintain, and instead focus on a common solution that the GNU project is
> already heavily invested in.  this is, of course, the "GNU Binutils" project.
> -mike

I must admit, I'm quite unfamiliar with the history here and my
original intention
was to just provide a solution to a specific issue I found with
readelf. Anything
beyond that is probably outside the scope of what I'm comfortable addressing.
If someone else wants to take what I've started here and drive it to completion
as part of a larger effort, please feel free!

Also wanted to make sure that my second patch was seen? I think it addresses
the original concern raised by Jan, but perhaps more is still needed there.

Brett

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

* Re: [PATCH] readelf: use fseeko64 or fseeko if possible
  2022-11-15 14:57 ` [PATCH] readelf: use fseeko64 or fseeko if possible Brett Werling
@ 2022-11-17  7:02   ` Alan Modra
  2022-11-17 14:09     ` Brett Werling
  0 siblings, 1 reply; 24+ messages in thread
From: Alan Modra @ 2022-11-17  7:02 UTC (permalink / raw)
  To: Brett Werling; +Cc: binutils

On Tue, Nov 15, 2022 at 08:57:17AM -0600, Brett Werling via Binutils wrote:
> Changes readelf to make use first of fseeko64 and then fseeko,
> depending on which of those is available. If neither is available,
> reverts to the previous behavior of using fseek. In all cases, the
> offset argument given is cast accoridngly.
> 
> This is necessary when building readelf for LLP64 systems, where a
> long will only be 32 bits wide. If the elf file in question is >= 2 GiB,
> that is greater than the max long value and therefore fseek will fail
> indicating that the offset is negative. On such systems, making use of
> fseeko64 or fseeko will result in the ability so seek past the 2 GiB
> max long boundary.
> ---
>  binutils/config.in |  6 ++++++
>  binutils/configure |  2 +-
>  binutils/readelf.c | 47 ++++++++++++++++++++++++++++------------------
>  3 files changed, 36 insertions(+), 19 deletions(-)
> 
> diff --git a/binutils/config.in b/binutils/config.in
> index 4d22a80971b..91fe00af777 100644
> --- a/binutils/config.in
> +++ b/binutils/config.in
> @@ -67,6 +67,12 @@
>  /* Define to 1 if you have the <fcntl.h> header file. */
>  #undef HAVE_FCNTL_H
>  
> +/* Define to 1 if you have the `fseeko' function. */
> +#undef HAVE_FSEEKO
> +
> +/* Define to 1 if you have the `fseeko64' function. */
> +#undef HAVE_FSEEKO64
> +
>  /* Define to 1 if you have the `getc_unlocked' function. */
>  #undef HAVE_GETC_UNLOCKED
>  
> diff --git a/binutils/configure b/binutils/configure
> index 6176d699e57..46519a31701 100755
> --- a/binutils/configure
> +++ b/binutils/configure
> @@ -13155,7 +13155,7 @@ $as_echo "#define HAVE_MMAP 1" >>confdefs.h
>  fi
>  rm -f conftest.mmap conftest.txt
>  
> -for ac_func in getc_unlocked mkdtemp mkstemp utimensat utimes
> +for ac_func in getc_unlocked mkdtemp mkstemp utimensat utimes fseeko fseeko64
>  do :
>    as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
>  ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
> diff --git a/binutils/readelf.c b/binutils/readelf.c
> index c8323539a21..3e5e4f29ff2 100644
> --- a/binutils/readelf.c
> +++ b/binutils/readelf.c
> @@ -177,6 +177,17 @@
>  #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
>  #endif
>  
> +#if defined (HAVE_FSEEKO64)
> +#define FSEEK_FUNC fseeko64
> +#define FSEEK_OFF_TYPE off64_t
> +#elif defined (HAVE_FSEEKO)
> +#define FSEEK_FUNC fseeko
> +#define FSEEK_OFF_TYPE off_t
> +#else
> +#define FSEEK_FUNC fseek
> +#define FSEEK_OFF_TYPE long
> +#endif
> +
>  typedef struct elf_section_list
>  {
>    Elf_Internal_Shdr *        hdr;
> @@ -479,7 +490,7 @@ get_data (void *var,
>        return NULL;
>      }
>  
> -  if (fseek (filedata->handle, filedata->archive_file_offset + offset,
> +  if (FSEEK_FUNC (filedata->handle, (FSEEK_OFF_TYPE)filedata->archive_file_offset + offset,
>  	     SEEK_SET))
>      {

There shouldn't be a need for the FSEEK_OFF_TYPE cast.  Casts always
make me question the code, and indeed in this case you have a whole
lot of unsigned long variables and struct fields that will need to be
wider in order to support large archives.  Also, functions like
offset_from_vma will need changing.

>        if (reason)
> @@ -6262,8 +6273,8 @@ the .dynamic section is not the same as the dynamic segment\n"));
>  	  if (segment->p_offset >= filedata->file_size
>  	      || segment->p_filesz > filedata->file_size - segment->p_offset
>  	      || segment->p_filesz - 1 >= (size_t) -2
> -	      || fseek (filedata->handle,
> -			filedata->archive_file_offset + (long) segment->p_offset,
> +	      || FSEEK_FUNC (filedata->handle,
> +			(FSEEK_OFF_TYPE)filedata->archive_file_offset + (long) segment->p_offset,
>  			SEEK_SET))

and the (long) also should go.  I'm happy enough with the patch as-is,
except please remove all those FSEEK_OFF_TYPE casts.  Further fixes
can be done in followup patches.

-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32
  2022-11-16 21:19                     ` Brett Werling
@ 2022-11-17  8:02                       ` Mike Frysinger
  0 siblings, 0 replies; 24+ messages in thread
From: Mike Frysinger @ 2022-11-17  8:02 UTC (permalink / raw)
  To: Brett Werling; +Cc: Michael Matz, Jan Beulich, Alan Modra, binutils

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

On 16 Nov 2022 15:19, Brett Werling via Binutils wrote:
> On Wed, Nov 16, 2022 at 11:09 AM Mike Frysinger wrote:
> > On 16 Nov 2022 16:13, Michael Matz wrote:
> > > On Wed, 16 Nov 2022, Mike Frysinger wrote:
> > > > are you claiming that current gnulib versions can't support even those old
> > > > versions of glibc ?  i'd find that extremely surprising, and really sounds
> > > > like a bug that should be taken up with the gnulib folks.
> > >
> > > No.  I'm claiming that moving to gnulib is work (even if perhaps not
> > > much?) and change and I (personally) don't see the need to invest into
> > > that work and deal with the change.  Had you asked "should I look into
> > > using gnulib in bfd" my answer would have been "well, try it, why not?".
> > > But you asked "should _we_ look" (aka should someone do that), hence my
> > > answer was in the line of "perhaps, but why bother?".
> >
> > you're proposing adding a not insignificant amount of custom portability logic
> > and making the readelf code harder to understand & maintain while also not
> > fixing the same bug in other binutils/ tools.  it is perfectly reasonable to
> > ask you to not add tech debt & make the code base worse off for everyone else
> > to maintain, and instead focus on a common solution that the GNU project is
> > already heavily invested in.  this is, of course, the "GNU Binutils" project.
> 
> I must admit, I'm quite unfamiliar with the history here and my
> original intention
> was to just provide a solution to a specific issue I found with
> readelf. Anything
> beyond that is probably outside the scope of what I'm comfortable addressing.
> If someone else wants to take what I've started here and drive it to completion
> as part of a larger effort, please feel free!
> 
> Also wanted to make sure that my second patch was seen? I think it addresses
> the original concern raised by Jan, but perhaps more is still needed there.

the 2nd patch is what prompted me to follow up to the thread ;).  i grok that
it addresses the specific issue you've found, but it isn't a complete fix, nor
one that improves the codebase for the maintainers to maintain long term.
-mike

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32
  2022-11-16 17:09                   ` Mike Frysinger
  2022-11-16 21:19                     ` Brett Werling
@ 2022-11-17 13:21                     ` Michael Matz
  1 sibling, 0 replies; 24+ messages in thread
From: Michael Matz @ 2022-11-17 13:21 UTC (permalink / raw)
  To: Mike Frysinger; +Cc: Jan Beulich, Alan Modra, Brett Werling, binutils

Hello,

On Thu, 17 Nov 2022, Mike Frysinger wrote:

> > No.  I'm claiming that moving to gnulib is work (even if perhaps not 
> > much?) and change and I (personally) don't see the need to invest into 
> > that work and deal with the change.  Had you asked "should I look into 
> > using gnulib in bfd" my answer would have been "well, try it, why not?".  
> > But you asked "should _we_ look" (aka should someone do that), hence my 
> > answer was in the line of "perhaps, but why bother?".
> 
> you're proposing adding a not insignificant amount of custom portability 
> logic

I'm not proposing anything, but I guess I consider the amount of 
portability code to be actually insignificant in this case.

> and making the readelf code harder to understand & maintain while 
> also not fixing the same bug in other binutils/ tools.  it is perfectly 
> reasonable to ask you to not add tech debt & make the code base worse 
> off for everyone else to maintain, and instead focus on a common 
> solution that the GNU project is already heavily invested in.  this is, 
> of course, the "GNU Binutils" project.

Yeah, well, until someone does the work I guess we'll never know.


Ciao,
Michael.

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

* Re: [PATCH] readelf: use fseeko64 or fseeko if possible
  2022-11-17  7:02   ` Alan Modra
@ 2022-11-17 14:09     ` Brett Werling
  0 siblings, 0 replies; 24+ messages in thread
From: Brett Werling @ 2022-11-17 14:09 UTC (permalink / raw)
  To: Alan Modra; +Cc: binutils

> There shouldn't be a need for the FSEEK_OFF_TYPE cast.  Casts always
> make me question the code, and indeed in this case you have a whole
> lot of unsigned long variables and struct fields that will need to be
> wider in order to support large archives.  Also, functions like
> offset_from_vma will need changing.
>
> >        if (reason)
> > @@ -6262,8 +6273,8 @@ the .dynamic section is not the same as the dynamic segment\n"));
> >         if (segment->p_offset >= filedata->file_size
> >             || segment->p_filesz > filedata->file_size - segment->p_offset
> >             || segment->p_filesz - 1 >= (size_t) -2
> > -           || fseek (filedata->handle,
> > -                     filedata->archive_file_offset + (long) segment->p_offset,
> > +           || FSEEK_FUNC (filedata->handle,
> > +                     (FSEEK_OFF_TYPE)filedata->archive_file_offset + (long) segment->p_offset,
> >                       SEEK_SET))
>
> and the (long) also should go.  I'm happy enough with the patch as-is,
> except please remove all those FSEEK_OFF_TYPE casts.  Further fixes
> can be done in followup patches.
>
> --
> Alan Modra
> Australia Development Lab, IBM

I tend to agree in that I don't particularly like the casts myself. To be
honest, I only had them in there to try and ensure the math between the
"offset" operands was being done on an off64_t or off_t and try to avoid
the chance of a long. I have no problem removing them and I assume the
resulting code will be functionally equivalent. New patch incoming
shortly.

If others watching this change feel strongly enough that this needs to be
part of a wider effort, please indicate so and I will let this patch lie.
I mostly wanted to avoid having to carry it forward locally in my
particular use case.

Brett

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

* [PATCH] readelf: use fseeko64 or fseeko if possible
  2022-11-14 15:03 [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32 Brett Werling
  2022-11-14 15:30 ` Jan Beulich
  2022-11-15 14:57 ` [PATCH] readelf: use fseeko64 or fseeko if possible Brett Werling
@ 2022-11-17 14:34 ` Brett Werling
  2022-11-21 21:52   ` Alan Modra
  2022-11-22 21:57   ` Alan Modra
  2 siblings, 2 replies; 24+ messages in thread
From: Brett Werling @ 2022-11-17 14:34 UTC (permalink / raw)
  To: binutils; +Cc: Brett Werling

Changes readelf to make use first of fseeko64 and then fseeko,
depending on which of those is available. If neither is available,
reverts to the previous behavior of using fseek.

This is necessary when building readelf for LLP64 systems, where a
long will only be 32 bits wide. If the elf file in question is >= 2 GiB,
that is greater than the max long value and therefore fseek will fail
indicating that the offset is negative. On such systems, making use of
fseeko64 or fseeko will result in the ability so seek past the 2 GiB
max long boundary.
---
 binutils/config.in |  6 ++++++
 binutils/configure |  2 +-
 binutils/readelf.c | 30 +++++++++++++++++++-----------
 3 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/binutils/config.in b/binutils/config.in
index 4d22a80971b..91fe00af777 100644
--- a/binutils/config.in
+++ b/binutils/config.in
@@ -67,6 +67,12 @@
 /* Define to 1 if you have the <fcntl.h> header file. */
 #undef HAVE_FCNTL_H
 
+/* Define to 1 if you have the `fseeko' function. */
+#undef HAVE_FSEEKO
+
+/* Define to 1 if you have the `fseeko64' function. */
+#undef HAVE_FSEEKO64
+
 /* Define to 1 if you have the `getc_unlocked' function. */
 #undef HAVE_GETC_UNLOCKED
 
diff --git a/binutils/configure b/binutils/configure
index 6176d699e57..46519a31701 100755
--- a/binutils/configure
+++ b/binutils/configure
@@ -13155,7 +13155,7 @@ $as_echo "#define HAVE_MMAP 1" >>confdefs.h
 fi
 rm -f conftest.mmap conftest.txt
 
-for ac_func in getc_unlocked mkdtemp mkstemp utimensat utimes
+for ac_func in getc_unlocked mkdtemp mkstemp utimensat utimes fseeko fseeko64
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/binutils/readelf.c b/binutils/readelf.c
index 6b2cbbcbb1b..044022ec334 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -178,6 +178,14 @@
 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
 #endif
 
+#if defined (HAVE_FSEEKO64)
+#define FSEEK_FUNC fseeko64
+#elif defined (HAVE_FSEEKO)
+#define FSEEK_FUNC fseeko
+#else
+#define FSEEK_FUNC fseek
+#endif
+
 typedef struct elf_section_list
 {
   Elf_Internal_Shdr *        hdr;
@@ -482,7 +490,7 @@ get_data (void *var,
       return NULL;
     }
 
-  if (fseek (filedata->handle, filedata->archive_file_offset + offset,
+  if (FSEEK_FUNC (filedata->handle, filedata->archive_file_offset + offset,
 	     SEEK_SET))
     {
       if (reason)
@@ -6283,7 +6291,7 @@ the .dynamic section is not the same as the dynamic segment\n"));
 	  if (segment->p_offset >= filedata->file_size
 	      || segment->p_filesz > filedata->file_size - segment->p_offset
 	      || segment->p_filesz - 1 >= (size_t) -2
-	      || fseek (filedata->handle,
+	      || FSEEK_FUNC (filedata->handle,
 			filedata->archive_file_offset + (long) segment->p_offset,
 			SEEK_SET))
 	    error (_("Unable to find program interpreter name\n"));
@@ -11056,7 +11064,7 @@ get_num_dynamic_syms (Filedata * filedata)
 	  && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
 	hash_ent_size = 8;
 
-      if (fseek (filedata->handle,
+      if (FSEEK_FUNC (filedata->handle,
 		 (filedata->archive_file_offset
 		  + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
 				     sizeof nb + sizeof nc)),
@@ -11109,7 +11117,7 @@ get_num_dynamic_syms (Filedata * filedata)
       uint64_t buckets_vma;
       unsigned long hn;
 
-      if (fseek (filedata->handle,
+      if (FSEEK_FUNC (filedata->handle,
 		 (filedata->archive_file_offset
 		  + offset_from_vma (filedata,
 				     filedata->dynamic_info_DT_GNU_HASH,
@@ -11135,7 +11143,7 @@ get_num_dynamic_syms (Filedata * filedata)
       else
 	buckets_vma += bitmaskwords * 8;
 
-      if (fseek (filedata->handle,
+      if (FSEEK_FUNC (filedata->handle,
 		 (filedata->archive_file_offset
 		  + offset_from_vma (filedata, buckets_vma, 4)),
 		 SEEK_SET))
@@ -11165,7 +11173,7 @@ get_num_dynamic_syms (Filedata * filedata)
 
       maxchain -= filedata->gnusymidx;
 
-      if (fseek (filedata->handle,
+      if (FSEEK_FUNC (filedata->handle,
 		 (filedata->archive_file_offset
 		  + offset_from_vma (filedata,
 				     buckets_vma + 4 * (filedata->ngnubuckets
@@ -11192,7 +11200,7 @@ get_num_dynamic_syms (Filedata * filedata)
 	}
       while ((byte_get (nb, 4) & 1) == 0);
 
-      if (fseek (filedata->handle,
+      if (FSEEK_FUNC (filedata->handle,
 		 (filedata->archive_file_offset
 		  + offset_from_vma (filedata, (buckets_vma
 						+ 4 * filedata->ngnubuckets),
@@ -11211,7 +11219,7 @@ get_num_dynamic_syms (Filedata * filedata)
 
       if (filedata->dynamic_info_DT_MIPS_XHASH)
 	{
-	  if (fseek (filedata->handle,
+	  if (FSEEK_FUNC (filedata->handle,
 		     (filedata->archive_file_offset
 		      + offset_from_vma (filedata, (buckets_vma
 						    + 4 * (filedata->ngnubuckets
@@ -22610,7 +22618,7 @@ process_archive (Filedata * filedata, bool is_thin_archive)
 	      ret = false;
 	    }
 
-	  if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
+	  if (FSEEK_FUNC (filedata->handle, current_pos, SEEK_SET) != 0)
 	    {
 	      error (_("%s: failed to seek back to start of object files "
 		       "in the archive\n"),
@@ -22637,7 +22645,7 @@ process_archive (Filedata * filedata, bool is_thin_archive)
       char * qualified_name;
 
       /* Read the next archive header.  */
-      if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
+      if (FSEEK_FUNC (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
 	{
 	  error (_("%s: failed to seek to next archive header\n"),
 		 arch.file_name);
@@ -22747,7 +22755,7 @@ process_archive (Filedata * filedata, bool is_thin_archive)
 
 	  /* The nested archive file will have been opened and setup by
 	     get_archive_member_name.  */
-	  if (fseek (nested_arch.file, filedata->archive_file_offset,
+	  if (FSEEK_FUNC (nested_arch.file, filedata->archive_file_offset,
 		     SEEK_SET) != 0)
 	    {
 	      error (_("%s: failed to seek to archive member.\n"),
-- 
2.38.1


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

* Re: [PATCH] readelf: use fseeko64 or fseeko if possible
  2022-11-17 14:34 ` Brett Werling
@ 2022-11-21 21:52   ` Alan Modra
  2022-11-22 13:46     ` Michael Matz
  2022-11-22 21:57   ` Alan Modra
  1 sibling, 1 reply; 24+ messages in thread
From: Alan Modra @ 2022-11-21 21:52 UTC (permalink / raw)
  To: Brett Werling; +Cc: binutils

On Thu, Nov 17, 2022 at 08:34:19AM -0600, Brett Werling via Binutils wrote:
> Changes readelf to make use first of fseeko64 and then fseeko,
> depending on which of those is available. If neither is available,
> reverts to the previous behavior of using fseek.

Committed, thanks.

-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: [PATCH] readelf: use fseeko64 or fseeko if possible
  2022-11-21 21:52   ` Alan Modra
@ 2022-11-22 13:46     ` Michael Matz
  2022-11-22 21:55       ` Alan Modra
  0 siblings, 1 reply; 24+ messages in thread
From: Michael Matz @ 2022-11-22 13:46 UTC (permalink / raw)
  To: Alan Modra; +Cc: Brett Werling, binutils

Hey Alan,

On Tue, 22 Nov 2022, Alan Modra via Binutils wrote:

> On Thu, Nov 17, 2022 at 08:34:19AM -0600, Brett Werling via Binutils wrote:
> > Changes readelf to make use first of fseeko64 and then fseeko,
> > depending on which of those is available. If neither is available,
> > reverts to the previous behavior of using fseek.
> 
> Committed, thanks.

I've integrated the change into configure.ac and regen'ed 
config{ure,.in} and pushed d7229d6a561 for this.

(Brett: in case you wonder: configure and config.in are generated files, 
by autoconf, so editing them directly would eventually lose the changes on 
the next regeneration, configure.ac is the source file for them)


Ciao,
Michael.

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

* Re: [PATCH] readelf: use fseeko64 or fseeko if possible
  2022-11-22 13:46     ` Michael Matz
@ 2022-11-22 21:55       ` Alan Modra
  0 siblings, 0 replies; 24+ messages in thread
From: Alan Modra @ 2022-11-22 21:55 UTC (permalink / raw)
  To: Michael Matz; +Cc: Brett Werling, binutils

On Tue, Nov 22, 2022 at 01:46:56PM +0000, Michael Matz wrote:
> I've integrated the change into configure.ac and regen'ed 
> config{ure,.in} and pushed d7229d6a561 for this.

Thanks, I'll blame it on covid, which finally caught up with me last
week.

-- 
Alan Modra
Australia Development Lab, IBM

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

* Re: [PATCH] readelf: use fseeko64 or fseeko if possible
  2022-11-17 14:34 ` Brett Werling
  2022-11-21 21:52   ` Alan Modra
@ 2022-11-22 21:57   ` Alan Modra
  2022-11-22 21:59     ` Don't use "long" in readelf for file offsets Alan Modra
  1 sibling, 1 reply; 24+ messages in thread
From: Alan Modra @ 2022-11-22 21:57 UTC (permalink / raw)
  To: Brett Werling; +Cc: binutils

Replace the macros with a small wrapper function that verifies the fseek
offset arg isn't overlarge.

	* readelf.c (FSEEK_FUNC): Delete, replace uses with..
	(fseek64): ..this new function.
	(process_program_headers): Don't cast p_offset to long.

diff --git a/binutils/readelf.c b/binutils/readelf.c
index 044022ec334..291bc13e0d0 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -178,14 +178,6 @@
 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
 #endif
 
-#if defined (HAVE_FSEEKO64)
-#define FSEEK_FUNC fseeko64
-#elif defined (HAVE_FSEEKO)
-#define FSEEK_FUNC fseeko
-#else
-#define FSEEK_FUNC fseek
-#endif
-
 typedef struct elf_section_list
 {
   Elf_Internal_Shdr *        hdr;
@@ -373,6 +365,36 @@ enum versioned_symbol_info
   symbol_public
 };
 
+static int
+fseek64 (FILE *stream, int64_t offset, int whence)
+{
+#if defined (HAVE_FSEEKO64)
+  off64_t o = offset;
+  if (o != offset)
+    {
+      errno = EINVAL;
+      return -1;
+    }
+  return fseeko64 (stream, o, whence);
+#elif defined (HAVE_FSEEKO)
+  off_t o = offset;
+  if (o != offset)
+    {
+      errno = EINVAL;
+      return -1;
+    }
+  return fseeko (stream, o, whence);
+#else
+  long o = offset;
+  if (o != offset)
+    {
+      errno = EINVAL;
+      return -1;
+    }
+  return fseek (stream, o, whence);
+#endif
+}
+
 static const char * get_symbol_version_string
   (Filedata *, bool, const char *, unsigned long, unsigned,
    Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
@@ -490,8 +512,8 @@ get_data (void *var,
       return NULL;
     }
 
-  if (FSEEK_FUNC (filedata->handle, filedata->archive_file_offset + offset,
-	     SEEK_SET))
+  if (fseek64 (filedata->handle, filedata->archive_file_offset + offset,
+	       SEEK_SET))
     {
       if (reason)
 	error (_("Unable to seek to 0x%lx for %s\n"),
@@ -6291,9 +6313,9 @@ the .dynamic section is not the same as the dynamic segment\n"));
 	  if (segment->p_offset >= filedata->file_size
 	      || segment->p_filesz > filedata->file_size - segment->p_offset
 	      || segment->p_filesz - 1 >= (size_t) -2
-	      || FSEEK_FUNC (filedata->handle,
-			filedata->archive_file_offset + (long) segment->p_offset,
-			SEEK_SET))
+	      || fseek64 (filedata->handle,
+			  filedata->archive_file_offset + segment->p_offset,
+			  SEEK_SET))
 	    error (_("Unable to find program interpreter name\n"));
 	  else
 	    {
@@ -11064,11 +11086,12 @@ get_num_dynamic_syms (Filedata * filedata)
 	  && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
 	hash_ent_size = 8;
 
-      if (FSEEK_FUNC (filedata->handle,
-		 (filedata->archive_file_offset
-		  + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
-				     sizeof nb + sizeof nc)),
-		 SEEK_SET))
+      if (fseek64 (filedata->handle,
+		   (filedata->archive_file_offset
+		    + offset_from_vma (filedata,
+				       filedata->dynamic_info[DT_HASH],
+				       sizeof nb + sizeof nc)),
+		   SEEK_SET))
 	{
 	  error (_("Unable to seek to start of dynamic information\n"));
 	  goto no_hash;
@@ -11117,12 +11140,12 @@ get_num_dynamic_syms (Filedata * filedata)
       uint64_t buckets_vma;
       unsigned long hn;
 
-      if (FSEEK_FUNC (filedata->handle,
-		 (filedata->archive_file_offset
-		  + offset_from_vma (filedata,
-				     filedata->dynamic_info_DT_GNU_HASH,
-				     sizeof nb)),
-		 SEEK_SET))
+      if (fseek64 (filedata->handle,
+		   (filedata->archive_file_offset
+		    + offset_from_vma (filedata,
+				       filedata->dynamic_info_DT_GNU_HASH,
+				       sizeof nb)),
+		   SEEK_SET))
 	{
 	  error (_("Unable to seek to start of dynamic information\n"));
 	  goto no_gnu_hash;
@@ -11143,10 +11166,10 @@ get_num_dynamic_syms (Filedata * filedata)
       else
 	buckets_vma += bitmaskwords * 8;
 
-      if (FSEEK_FUNC (filedata->handle,
-		 (filedata->archive_file_offset
-		  + offset_from_vma (filedata, buckets_vma, 4)),
-		 SEEK_SET))
+      if (fseek64 (filedata->handle,
+		   (filedata->archive_file_offset
+		    + offset_from_vma (filedata, buckets_vma, 4)),
+		   SEEK_SET))
 	{
 	  error (_("Unable to seek to start of dynamic information\n"));
 	  goto no_gnu_hash;
@@ -11173,13 +11196,13 @@ get_num_dynamic_syms (Filedata * filedata)
 
       maxchain -= filedata->gnusymidx;
 
-      if (FSEEK_FUNC (filedata->handle,
-		 (filedata->archive_file_offset
-		  + offset_from_vma (filedata,
-				     buckets_vma + 4 * (filedata->ngnubuckets
-							+ maxchain),
-				      4)),
-		 SEEK_SET))
+      if (fseek64 (filedata->handle,
+		   (filedata->archive_file_offset
+		    + offset_from_vma (filedata,
+				       buckets_vma + 4 * (filedata->ngnubuckets
+							  + maxchain),
+				       4)),
+		   SEEK_SET))
 	{
 	  error (_("Unable to seek to start of dynamic information\n"));
 	  goto no_gnu_hash;
@@ -11200,12 +11223,12 @@ get_num_dynamic_syms (Filedata * filedata)
 	}
       while ((byte_get (nb, 4) & 1) == 0);
 
-      if (FSEEK_FUNC (filedata->handle,
-		 (filedata->archive_file_offset
-		  + offset_from_vma (filedata, (buckets_vma
-						+ 4 * filedata->ngnubuckets),
-				     4)),
-		 SEEK_SET))
+      if (fseek64 (filedata->handle,
+		   (filedata->archive_file_offset
+		    + offset_from_vma (filedata, (buckets_vma
+						  + 4 * filedata->ngnubuckets),
+				       4)),
+		   SEEK_SET))
 	{
 	  error (_("Unable to seek to start of dynamic information\n"));
 	  goto no_gnu_hash;
@@ -11219,12 +11242,12 @@ get_num_dynamic_syms (Filedata * filedata)
 
       if (filedata->dynamic_info_DT_MIPS_XHASH)
 	{
-	  if (FSEEK_FUNC (filedata->handle,
-		     (filedata->archive_file_offset
-		      + offset_from_vma (filedata, (buckets_vma
-						    + 4 * (filedata->ngnubuckets
-							   + maxchain)), 4)),
-		     SEEK_SET))
+	  if (fseek64 (filedata->handle,
+		       (filedata->archive_file_offset
+			+ offset_from_vma (filedata, (buckets_vma
+						      + 4 * (filedata->ngnubuckets
+							     + maxchain)), 4)),
+		       SEEK_SET))
 	    {
 	      error (_("Unable to seek to start of dynamic information\n"));
 	      goto no_gnu_hash;
@@ -22618,7 +22641,7 @@ process_archive (Filedata * filedata, bool is_thin_archive)
 	      ret = false;
 	    }
 
-	  if (FSEEK_FUNC (filedata->handle, current_pos, SEEK_SET) != 0)
+	  if (fseek64 (filedata->handle, current_pos, SEEK_SET) != 0)
 	    {
 	      error (_("%s: failed to seek back to start of object files "
 		       "in the archive\n"),
@@ -22645,7 +22668,7 @@ process_archive (Filedata * filedata, bool is_thin_archive)
       char * qualified_name;
 
       /* Read the next archive header.  */
-      if (FSEEK_FUNC (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
+      if (fseek64 (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
 	{
 	  error (_("%s: failed to seek to next archive header\n"),
 		 arch.file_name);
@@ -22755,8 +22778,8 @@ process_archive (Filedata * filedata, bool is_thin_archive)
 
 	  /* The nested archive file will have been opened and setup by
 	     get_archive_member_name.  */
-	  if (FSEEK_FUNC (nested_arch.file, filedata->archive_file_offset,
-		     SEEK_SET) != 0)
+	  if (fseek64 (nested_arch.file, filedata->archive_file_offset,
+		       SEEK_SET) != 0)
 	    {
 	      error (_("%s: failed to seek to archive member.\n"),
 		     nested_arch.file_name);

-- 
Alan Modra
Australia Development Lab, IBM

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

* Don't use "long" in readelf for file offsets
  2022-11-22 21:57   ` Alan Modra
@ 2022-11-22 21:59     ` Alan Modra
  0 siblings, 0 replies; 24+ messages in thread
From: Alan Modra @ 2022-11-22 21:59 UTC (permalink / raw)
  To: Brett Werling; +Cc: binutils

The aim here is to improve readelf handling of large 64-bit object
files on LLP64 hosts (Windows) where long is only 32 bits.  The patch
changes more than just file offsets.  Addresses and sizes are also
changed to avoid "long".  Most places get to use uint64_t even where
size_t may be more appropriate, because that allows some overflow
checks to be implemented easily (*alloc changes).

	* dwarf.c (cmalloc, xcmalloc, xcrealloc, xcalloc2): Make nmemb
	parameter uint64_t.
	* dwarf.h: Update prototypes.
	(struct dwarf_section): Make num_relocs uint64_t.
	* elfcomm.c (setup_archive): Update error format.
	* elfcomm.h (struct archive_info): Make sym_size, longnames_size,
	nested_member_origin, next_arhdr_offset uint64_t.
	* readelf.c (struct filedata): Make archive_file_offset,
	archive_file_size, string_table_length, dynamic_addr,
	dynamic_nent, dynamic_strings_length, num_dynamic_syms,
	dynamic_syminfo_offset uint64_t.
	(many functions): Replace uses of "unsigned long" with
	"uint64_t" or "size_t".

diff --git a/binutils/dwarf.c b/binutils/dwarf.c
index 4bba8dfb81a..404a0bdbac6 100644
--- a/binutils/dwarf.c
+++ b/binutils/dwarf.c
@@ -11082,7 +11082,7 @@ display_debug_not_supported (struct dwarf_section *section,
    Note: does *not* initialise the allocated memory to zero.  */
 
 void *
-cmalloc (size_t nmemb, size_t size)
+cmalloc (uint64_t nmemb, size_t size)
 {
   /* Check for overflow.  */
   if (nmemb >= ~(size_t) 0 / size)
@@ -11096,13 +11096,13 @@ cmalloc (size_t nmemb, size_t size)
    Note: does *not* initialise the allocated memory to zero.  */
 
 void *
-xcmalloc (size_t nmemb, size_t size)
+xcmalloc (uint64_t nmemb, size_t size)
 {
   /* Check for overflow.  */
   if (nmemb >= ~(size_t) 0 / size)
     {
       fprintf (stderr,
-	       _("Attempt to allocate an array with an excessive number of elements: %#zx\n"),
+	       _("Attempt to allocate an array with an excessive number of elements: %#" PRIx64 "\n"),
 	       nmemb);
       xexit (1);
     }
@@ -11115,12 +11115,12 @@ xcmalloc (size_t nmemb, size_t size)
    Note: does *not* initialise any new memory to zero.  */
 
 void *
-xcrealloc (void *ptr, size_t nmemb, size_t size)
+xcrealloc (void *ptr, uint64_t nmemb, size_t size)
 {
   /* Check for overflow.  */
   if (nmemb >= ~(size_t) 0 / size)
     {
-      error (_("Attempt to re-allocate an array with an excessive number of elements: %#zx\n"),
+      error (_("Attempt to re-allocate an array with an excessive number of elements: %#" PRIx64 "\n"),
 	     nmemb);
       xexit (1);
     }
@@ -11131,12 +11131,12 @@ xcrealloc (void *ptr, size_t nmemb, size_t size)
 /* Like xcalloc, but verifies that the first parameter is not too large.  */
 
 void *
-xcalloc2 (size_t nmemb, size_t size)
+xcalloc2 (uint64_t nmemb, size_t size)
 {
   /* Check for overflow.  */
   if (nmemb >= ~(size_t) 0 / size)
     {
-      error (_("Attempt to allocate a zero'ed array with an excessive number of elements: %#zx\n"),
+      error (_("Attempt to allocate a zero'ed array with an excessive number of elements: %#" PRIx64 "\n"),
 	     nmemb);
       xexit (1);
     }
diff --git a/binutils/dwarf.h b/binutils/dwarf.h
index bb6128e3b5b..9c0031355d2 100644
--- a/binutils/dwarf.h
+++ b/binutils/dwarf.h
@@ -144,7 +144,7 @@ struct dwarf_section
   enum dwarf_section_display_enum  abbrev_sec;
   /* Used by clients to help them implement the reloc_at callback.  */
   void *                           reloc_info;
-  unsigned long                    num_relocs;
+  uint64_t                         num_relocs;
 };
 
 /* A structure containing the name of a debug section
@@ -256,10 +256,10 @@ extern void dwarf_select_sections_all (void);
 
 extern unsigned int * find_cu_tu_set (void *, unsigned int);
 
-extern void * cmalloc (size_t, size_t);
-extern void * xcalloc2 (size_t, size_t);
-extern void * xcmalloc (size_t, size_t);
-extern void * xcrealloc (void *, size_t, size_t);
+extern void * cmalloc (uint64_t, size_t);
+extern void * xcalloc2 (uint64_t, size_t);
+extern void * xcmalloc (uint64_t, size_t);
+extern void * xcrealloc (void *, uint64_t, size_t);
 
 /* A callback into the client.  Returns TRUE if there is a
    relocation against the given debug section at the given
diff --git a/binutils/elfcomm.c b/binutils/elfcomm.c
index 0e7d0b57ac6..6070b6ee8d9 100644
--- a/binutils/elfcomm.c
+++ b/binutils/elfcomm.c
@@ -530,7 +530,7 @@ setup_archive (struct archive_info *arch, const char *file_name,
       /* PR 17531: file: 01068045.  */
       if (arch->longnames_size < 8)
 	{
-	  error (_("%s: long name table is too small, (size = %ld)\n"),
+	  error (_("%s: long name table is too small, (size = %" PRId64 ")\n"),
 		 file_name, arch->longnames_size);
 	  return 1;
 	}
@@ -538,7 +538,7 @@ setup_archive (struct archive_info *arch, const char *file_name,
       if ((off_t) arch->longnames_size > file_size
 	  || (signed long) arch->longnames_size < 0)
 	{
-	  error (_("%s: long name table is too big, (size = 0x%lx)\n"),
+	  error (_("%s: long name table is too big, (size = %#" PRIx64 ")\n"),
 		 file_name, arch->longnames_size);
 	  return 1;
 	}
diff --git a/binutils/elfcomm.h b/binutils/elfcomm.h
index bab46b03451..54ac4b36811 100644
--- a/binutils/elfcomm.h
+++ b/binutils/elfcomm.h
@@ -49,19 +49,19 @@ extern uint64_t byte_get_big_endian (const unsigned char *, unsigned int);
 
 struct archive_info
 {
-  char * file_name;                     /* Archive file name.  */
-  FILE * file;                          /* Open file descriptor.  */
-  uint64_t index_num;                   /* Number of symbols in table.  */
-  uint64_t * index_array;               /* The array of member offsets.  */
-  char * sym_table;                     /* The symbol table.  */
-  unsigned long sym_size;               /* Size of the symbol table.  */
-  char * longnames;                     /* The long file names table.  */
-  unsigned long longnames_size;         /* Size of the long file names table.  */
-  unsigned long nested_member_origin;   /* Origin in the nested archive of the current member.  */
-  unsigned long next_arhdr_offset;      /* Offset of the next archive header.  */
-  int is_thin_archive;                  /* 1 if this is a thin archive.  */
-  int uses_64bit_indices;               /* 1 if the index table uses 64bit entries.  */
-  struct ar_hdr arhdr;                  /* Current archive header.  */
+  char *file_name;               /* Archive file name.  */
+  FILE *file;                    /* Open file descriptor.  */
+  uint64_t index_num;            /* Number of symbols in table.  */
+  uint64_t *index_array;         /* The array of member offsets.  */
+  char *sym_table;               /* The symbol table.  */
+  uint64_t sym_size;             /* Size of the symbol table.  */
+  char *longnames;               /* The long file names table.  */
+  uint64_t longnames_size;       /* Size of the long file names table.  */
+  uint64_t nested_member_origin; /* Origin in the nested archive of the current member.  */
+  uint64_t next_arhdr_offset;    /* Offset of the next archive header.  */
+  int is_thin_archive;           /* 1 if this is a thin archive.  */
+  int uses_64bit_indices;        /* 1 if the index table uses 64bit entries.  */
+  struct ar_hdr arhdr;           /* Current archive header.  */
 };
 
 /* Return the path name for a proxy entry in a thin archive.  */
diff --git a/binutils/readelf.c b/binutils/readelf.c
index 291bc13e0d0..1bd6df9448a 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -272,27 +272,27 @@ typedef struct filedata
   FILE *               handle;
   uint64_t             file_size;
   Elf_Internal_Ehdr    file_header;
-  unsigned long        archive_file_offset;
-  unsigned long        archive_file_size;
+  uint64_t             archive_file_offset;
+  uint64_t             archive_file_size;
   /* Everything below this point is cleared out by free_filedata.  */
   Elf_Internal_Shdr *  section_headers;
   Elf_Internal_Phdr *  program_headers;
   char *               string_table;
-  unsigned long        string_table_length;
-  unsigned long        dynamic_addr;
+  uint64_t             string_table_length;
+  uint64_t             dynamic_addr;
   uint64_t             dynamic_size;
-  size_t               dynamic_nent;
+  uint64_t             dynamic_nent;
   Elf_Internal_Dyn *   dynamic_section;
   Elf_Internal_Shdr *  dynamic_strtab_section;
   char *               dynamic_strings;
-  unsigned long        dynamic_strings_length;
+  uint64_t             dynamic_strings_length;
   Elf_Internal_Shdr *  dynamic_symtab_section;
-  unsigned long        num_dynamic_syms;
+  uint64_t             num_dynamic_syms;
   Elf_Internal_Sym *   dynamic_symbols;
-  uint64_t              version_info[16];
+  uint64_t             version_info[16];
   unsigned int         dynamic_syminfo_nent;
   Elf_Internal_Syminfo * dynamic_syminfo;
-  unsigned long        dynamic_syminfo_offset;
+  uint64_t             dynamic_syminfo_offset;
   uint64_t             nbuckets;
   uint64_t             nchains;
   uint64_t *           buckets;
@@ -396,7 +396,7 @@ fseek64 (FILE *stream, int64_t offset, int whence)
 }
 
 static const char * get_symbol_version_string
-  (Filedata *, bool, const char *, unsigned long, unsigned,
+  (Filedata *, bool, const char *, size_t, unsigned,
    Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
 
 #define UNKNOWN -1
@@ -473,7 +473,7 @@ get_dynamic_name (const Filedata *filedata, size_t offset)
 static void *
 get_data (void *var,
 	  Filedata *filedata,
-	  unsigned long offset,
+	  uint64_t offset,
 	  uint64_t size,
 	  uint64_t nmemb,
 	  const char *reason)
@@ -516,7 +516,7 @@ get_data (void *var,
 	       SEEK_SET))
     {
       if (reason)
-	error (_("Unable to seek to 0x%lx for %s\n"),
+	error (_("Unable to seek to %#" PRIx64 " for %s\n"),
 	       filedata->archive_file_offset + offset, reason);
       return NULL;
     }
@@ -919,7 +919,7 @@ printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
 }
 
 static const char *
-printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
+printable_section_name_from_index (Filedata *filedata, size_t ndx)
 {
   if (ndx >= filedata->file_header.e_shnum)
     return _("<corrupt>");
@@ -1162,14 +1162,14 @@ guess_is_rela (unsigned int e_machine)
    responsibility to free the allocated buffer.  */
 
 static bool
-slurp_rela_relocs (Filedata *            filedata,
-		   unsigned long         rel_offset,
-		   unsigned long         rel_size,
-		   Elf_Internal_Rela **  relasp,
-		   unsigned long *       nrelasp)
+slurp_rela_relocs (Filedata *filedata,
+		   uint64_t rel_offset,
+		   uint64_t rel_size,
+		   Elf_Internal_Rela **relasp,
+		   uint64_t *nrelasp)
 {
   Elf_Internal_Rela * relas;
-  size_t nrelas;
+  uint64_t nrelas;
   unsigned int i;
 
   if (is_32bit_elf)
@@ -1262,14 +1262,14 @@ slurp_rela_relocs (Filedata *            filedata,
    responsibility to free the allocated buffer.  */
 
 static bool
-slurp_rel_relocs (Filedata *            filedata,
-		  unsigned long         rel_offset,
-		  unsigned long         rel_size,
-		  Elf_Internal_Rela **  relsp,
-		  unsigned long *       nrelsp)
+slurp_rel_relocs (Filedata *filedata,
+		  uint64_t rel_offset,
+		  uint64_t rel_size,
+		  Elf_Internal_Rela **relsp,
+		  uint64_t *nrelsp)
 {
   Elf_Internal_Rela * rels;
-  size_t nrels;
+  uint64_t nrels;
   unsigned int i;
 
   if (is_32bit_elf)
@@ -1354,11 +1354,11 @@ slurp_rel_relocs (Filedata *            filedata,
 }
 
 static bool
-slurp_relr_relocs (Filedata * filedata,
-		   unsigned long relr_offset,
-		   unsigned long relr_size,
-		   uint64_t ** relrsp,
-		   unsigned long * nrelrsp)
+slurp_relr_relocs (Filedata *filedata,
+		   uint64_t relr_offset,
+		   uint64_t relr_size,
+		   uint64_t **relrsp,
+		   uint64_t *nrelrsp)
 {
   void *relrs;
   size_t size = 0, nentries, i;
@@ -1468,17 +1468,17 @@ uses_msp430x_relocs (Filedata * filedata)
    offset.  */
 
 static bool
-dump_relocations (Filedata *          filedata,
-		  unsigned long       rel_offset,
-		  unsigned long       rel_size,
-		  Elf_Internal_Sym *  symtab,
-		  unsigned long       nsyms,
-		  char *              strtab,
-		  unsigned long       strtablen,
-		  relocation_type     rel_type,
-		  bool                is_dynsym)
-{
-  unsigned long i;
+dump_relocations (Filedata *filedata,
+		  uint64_t rel_offset,
+		  uint64_t rel_size,
+		  Elf_Internal_Sym *symtab,
+		  uint64_t nsyms,
+		  char *strtab,
+		  uint64_t strtablen,
+		  relocation_type rel_type,
+		  bool is_dynsym)
+{
+  size_t i;
   Elf_Internal_Rela * rels;
   bool res = true;
 
@@ -1505,7 +1505,8 @@ dump_relocations (Filedata *          filedata,
 			      &rel_size))
 	return false;
 
-      printf (ngettext ("  %lu offset\n", "  %lu offsets\n", rel_size),
+      printf (ngettext ("  %" PRIu64 " offset\n",
+			"  %" PRIu64 " offsets\n", rel_size),
 	      rel_size);
       for (i = 0; i < rel_size; i++)
 	printf (format, relrs[i]);
@@ -1944,8 +1945,8 @@ dump_relocations (Filedata *          filedata,
 	  else
 	    {
 	      putchar (' ');
-	      printf (_("<unknown addend: %lx>"),
-		      (unsigned long) rels[i].r_addend);
+	      printf (_("<unknown addend: %" PRIx64 ">"),
+		      rels[i].r_addend);
 	      res = false;
 	    }
 	}
@@ -2098,7 +2099,7 @@ dump_relocations (Filedata *          filedata,
       if (filedata->file_header.e_machine == EM_SPARCV9
 	  && rtype != NULL
 	  && streq (rtype, "R_SPARC_OLO10"))
-	printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
+	printf (" + %" PRIx64, ELF64_R_TYPE_DATA (inf));
 
       putchar ('\n');
 
@@ -2624,7 +2625,7 @@ static bool get_dynamic_section (Filedata *);
 static void
 locate_dynamic_section (Filedata *filedata)
 {
-  unsigned long dynamic_addr = 0;
+  uint64_t dynamic_addr = 0;
   uint64_t dynamic_size = 0;
 
   if (filedata->file_header.e_phnum != 0
@@ -6126,7 +6127,7 @@ process_program_headers (Filedata * filedata)
 	}
     }
 
-  unsigned long dynamic_addr = 0;
+  uint64_t dynamic_addr = 0;
   uint64_t dynamic_size = 0;
   for (i = 0, segment = filedata->program_headers;
        i < filedata->file_header.e_phnum;
@@ -6374,7 +6375,7 @@ the .dynamic section is not the same as the dynamic segment\n"));
 
 /* Find the file offset corresponding to VMA by using the program headers.  */
 
-static long
+static int64_t
 offset_from_vma (Filedata * filedata, uint64_t vma, uint64_t size)
 {
   Elf_Internal_Phdr * seg;
@@ -6397,9 +6398,9 @@ offset_from_vma (Filedata * filedata, uint64_t vma, uint64_t size)
 	return vma - seg->p_vaddr + seg->p_offset;
     }
 
-  warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
-	(unsigned long) vma);
-  return (long) vma;
+  warn (_("Virtual address %#" PRIx64
+	  " not located in any PT_LOAD segment.\n"), vma);
+  return vma;
 }
 
 
@@ -6560,11 +6561,11 @@ get_section_headers (Filedata *filedata, bool probe)
 }
 
 static Elf_Internal_Sym *
-get_32bit_elf_symbols (Filedata *           filedata,
-		       Elf_Internal_Shdr *  section,
-		       unsigned long *      num_syms_return)
+get_32bit_elf_symbols (Filedata *filedata,
+		       Elf_Internal_Shdr *section,
+		       uint64_t *num_syms_return)
 {
-  unsigned long number = 0;
+  uint64_t number = 0;
   Elf32_External_Sym * esyms = NULL;
   Elf_External_Sym_Shndx * shndx = NULL;
   Elf_Internal_Sym * isyms = NULL;
@@ -6582,17 +6583,17 @@ get_32bit_elf_symbols (Filedata *           filedata,
   /* Run some sanity checks first.  */
   if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
     {
-      error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
+      error (_("Section %s has an invalid sh_entsize of %#" PRIx64 "\n"),
 	     printable_section_name (filedata, section),
-	     (unsigned long) section->sh_entsize);
+	     section->sh_entsize);
       goto exit_point;
     }
 
   if (section->sh_size > filedata->file_size)
     {
-      error (_("Section %s has an invalid sh_size of 0x%lx\n"),
+      error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
 	     printable_section_name (filedata, section),
-	     (unsigned long) section->sh_size);
+	     section->sh_size);
       goto exit_point;
     }
 
@@ -6600,10 +6601,11 @@ get_32bit_elf_symbols (Filedata *           filedata,
 
   if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
     {
-      error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
-	     (unsigned long) section->sh_size,
+      error (_("Size (%#" PRIx64 ") of section %s "
+	       "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"),
+	     section->sh_size,
 	     printable_section_name (filedata, section),
-	     (unsigned long) section->sh_entsize);
+	     section->sh_entsize);
       goto exit_point;
     }
 
@@ -6615,7 +6617,7 @@ get_32bit_elf_symbols (Filedata *           filedata,
   shndx = NULL;
   for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
     {
-      if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
+      if (entry->hdr->sh_link != (size_t) (section - filedata->section_headers))
 	continue;
 
       if (shndx != NULL)
@@ -6634,10 +6636,10 @@ get_32bit_elf_symbols (Filedata *           filedata,
       /* PR17531: file: heap-buffer-overflow */
       if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
 	{
-	  error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
+	  error (_("Index section %s has an sh_size of %#" PRIx64 " - expected %#" PRIx64 "\n"),
 		 printable_section_name (filedata, entry->hdr),
-		 (unsigned long) entry->hdr->sh_size,
-		 (unsigned long) section->sh_size);
+		 entry->hdr->sh_size,
+		 section->sh_size);
 	  goto exit_point;
 	}
     }
@@ -6646,8 +6648,7 @@ get_32bit_elf_symbols (Filedata *           filedata,
 
   if (isyms == NULL)
     {
-      error (_("Out of memory reading %lu symbols\n"),
-	     (unsigned long) number);
+      error (_("Out of memory reading %" PRIu64 " symbols\n"), number);
       goto exit_point;
     }
 
@@ -6677,11 +6678,11 @@ get_32bit_elf_symbols (Filedata *           filedata,
 }
 
 static Elf_Internal_Sym *
-get_64bit_elf_symbols (Filedata *           filedata,
-		       Elf_Internal_Shdr *  section,
-		       unsigned long *      num_syms_return)
+get_64bit_elf_symbols (Filedata *filedata,
+		       Elf_Internal_Shdr *section,
+		       uint64_t *num_syms_return)
 {
-  unsigned long number = 0;
+  uint64_t number = 0;
   Elf64_External_Sym * esyms = NULL;
   Elf_External_Sym_Shndx * shndx = NULL;
   Elf_Internal_Sym * isyms = NULL;
@@ -6699,17 +6700,17 @@ get_64bit_elf_symbols (Filedata *           filedata,
   /* Run some sanity checks first.  */
   if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
     {
-      error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
+      error (_("Section %s has an invalid sh_entsize of %#" PRIx64 "\n"),
 	     printable_section_name (filedata, section),
-	     (unsigned long) section->sh_entsize);
+	     section->sh_entsize);
       goto exit_point;
     }
 
   if (section->sh_size > filedata->file_size)
     {
-      error (_("Section %s has an invalid sh_size of 0x%lx\n"),
+      error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
 	     printable_section_name (filedata, section),
-	     (unsigned long) section->sh_size);
+	     section->sh_size);
       goto exit_point;
     }
 
@@ -6717,10 +6718,11 @@ get_64bit_elf_symbols (Filedata *           filedata,
 
   if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
     {
-      error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
-	     (unsigned long) section->sh_size,
+      error (_("Size (%#" PRIx64 ") of section %s "
+	       "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"),
+	     section->sh_size,
 	     printable_section_name (filedata, section),
-	     (unsigned long) section->sh_entsize);
+	     section->sh_entsize);
       goto exit_point;
     }
 
@@ -6732,7 +6734,7 @@ get_64bit_elf_symbols (Filedata *           filedata,
   shndx = NULL;
   for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
     {
-      if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
+      if (entry->hdr->sh_link != (size_t) (section - filedata->section_headers))
 	continue;
 
       if (shndx != NULL)
@@ -6751,10 +6753,10 @@ get_64bit_elf_symbols (Filedata *           filedata,
       /* PR17531: file: heap-buffer-overflow */
       if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
 	{
-	  error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
+	  error (_("Index section %s has an sh_size of %#" PRIx64 " - expected %#" PRIx64 "\n"),
 		 printable_section_name (filedata, entry->hdr),
-		 (unsigned long) entry->hdr->sh_size,
-		 (unsigned long) section->sh_size);
+		 entry->hdr->sh_size,
+		 section->sh_size);
 	  goto exit_point;
 	}
     }
@@ -6763,8 +6765,7 @@ get_64bit_elf_symbols (Filedata *           filedata,
 
   if (isyms == NULL)
     {
-      error (_("Out of memory reading %lu symbols\n"),
-	     (unsigned long) number);
+      error (_("Out of memory reading %" PRIu64 " symbols\n"), number);
       goto exit_point;
     }
 
@@ -6798,7 +6799,7 @@ get_64bit_elf_symbols (Filedata *           filedata,
 static Elf_Internal_Sym *
 get_elf_symbols (Filedata *filedata,
 		 Elf_Internal_Shdr *section,
-		 unsigned long *num_syms_return)
+		 uint64_t *num_syms_return)
 {
   if (is_32bit_elf)
     return get_32bit_elf_symbols (filedata, section, num_syms_return);
@@ -7183,12 +7184,12 @@ process_section_headers (Filedata * filedata)
 	printf (_("In linked file '%s': "), filedata->file_name);
       if (! filedata->is_separate || process_links)
 	printf (ngettext ("There is %d section header, "
-			  "starting at offset 0x%lx:\n",
+			  "starting at offset %#" PRIx64 ":\n",
 			  "There are %d section headers, "
-			  "starting at offset 0x%lx:\n",
+			  "starting at offset %#" PRIx64 ":\n",
 			  filedata->file_header.e_shnum),
 		filedata->file_header.e_shnum,
-		(unsigned long) filedata->file_header.e_shoff);
+		filedata->file_header.e_shoff);
     }
 
   if (!get_section_headers (filedata, false))
@@ -7854,8 +7855,8 @@ process_section_headers (Filedata * filedata)
 
 static bool
 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
-	    Elf_Internal_Sym **symtab, unsigned long *nsyms,
-	    char **strtab, unsigned long *strtablen)
+	    Elf_Internal_Sym **symtab, uint64_t *nsyms,
+	    char **strtab, uint64_t *strtablen)
 {
   *strtab = NULL;
   *strtablen = 0;
@@ -7922,7 +7923,7 @@ process_section_groups (Filedata * filedata)
   Elf_Internal_Shdr * symtab_sec;
   Elf_Internal_Shdr * strtab_sec;
   Elf_Internal_Sym * symtab;
-  unsigned long num_syms;
+  uint64_t num_syms;
   char * strtab;
   size_t strtab_size;
 
@@ -7988,8 +7989,7 @@ process_section_groups (Filedata * filedata)
 
   if (filedata->section_groups == NULL)
     {
-      error (_("Out of memory reading %lu groups\n"),
-	     (unsigned long) filedata->group_count);
+      error (_("Out of memory reading %zu groups\n"), filedata->group_count);
       return false;
     }
 
@@ -8092,10 +8092,11 @@ process_section_groups (Filedata * filedata)
 	  /* PR 17531: file: loop.  */
 	  if (section->sh_entsize > section->sh_size)
 	    {
-	      error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
+	      error (_("Section %s has sh_entsize (%#" PRIx64 ")"
+		       " which is larger than its size (%#" PRIx64 ")\n"),
 		     printable_section_name (filedata, section),
-		     (unsigned long) section->sh_entsize,
-		     (unsigned long) section->sh_size);
+		     section->sh_entsize,
+		     section->sh_size);
 	      continue;
 	    }
 
@@ -8226,7 +8227,7 @@ dump_ia64_vms_dynamic_fixups (Filedata *                  filedata,
 			      unsigned int                strtab_sz)
 {
   Elf64_External_VMS_IMAGE_FIXUP * imfs;
-  long i;
+  size_t i;
   const char * lib_name;
 
   imfs = get_data (NULL, filedata,
@@ -8240,17 +8241,18 @@ dump_ia64_vms_dynamic_fixups (Filedata *                  filedata,
     lib_name = strtab + fixup->needed;
   else
     {
-      warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
-            (unsigned long) fixup->needed);
+      warn (_("corrupt library name index of %#" PRIx64
+	      " found in dynamic entry"), fixup->needed);
       lib_name = "???";
     }
 
-  printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
-	  (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
+  printf (_("\nImage fixups for needed library #%" PRId64
+	    ": %s - ident: %" PRIx64 "\n"),
+	  fixup->fixup_needed, lib_name, fixup->needed_ident);
   printf
     (_("Seg Offset           Type                             SymVec DataType\n"));
 
-  for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
+  for (i = 0; i < (size_t) fixup->fixup_rela_cnt; i++)
     {
       unsigned int type;
       const char *rtype;
@@ -8277,7 +8279,7 @@ static bool
 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
 {
   Elf64_External_VMS_IMAGE_RELA *imrs;
-  long i;
+  size_t i;
 
   imrs = get_data (NULL, filedata,
 		   filedata->dynamic_addr + imgrela->img_rela_off,
@@ -8290,7 +8292,7 @@ dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *i
   printf
     (_("Seg Offset   Type                            Addend            Seg Sym Off\n"));
 
-  for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
+  for (i = 0; i < (size_t) imgrela->img_rela_cnt; i++)
     {
       unsigned int type;
       const char *rtype;
@@ -8404,8 +8406,8 @@ static struct
 static bool
 process_relocs (Filedata * filedata)
 {
-  unsigned long rel_size;
-  unsigned long rel_offset;
+  uint64_t rel_size;
+  uint64_t rel_offset;
 
   if (!do_reloc)
     return true;
@@ -8447,11 +8449,13 @@ process_relocs (Filedata * filedata)
 	    {
 	      if (filedata->is_separate)
 		printf
-		  (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
+		  (_("\nIn linked file '%s' section '%s' at offset %#" PRIx64
+		     " contains %" PRId64 " bytes:\n"),
 		   filedata->file_name, name, rel_offset, rel_size);
 	      else
 		printf
-		  (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
+		  (_("\n'%s' relocation section at offset %#" PRIx64
+		     " contains %" PRId64 " bytes:\n"),
 		   name, rel_offset, rel_size);
 
 	      dump_relocations (filedata,
@@ -8481,7 +8485,7 @@ process_relocs (Filedata * filedata)
   else
     {
       Elf_Internal_Shdr * section;
-      unsigned long i;
+      size_t i;
       bool found = false;
 
       for (i = 0, section = filedata->section_headers;
@@ -8499,7 +8503,7 @@ process_relocs (Filedata * filedata)
 	  if (rel_size)
 	    {
 	      relocation_type rel_type;
-	      unsigned long num_rela;
+	      uint64_t num_rela;
 
 	      if (filedata->is_separate)
 		printf (_("\nIn linked file '%s' relocation section "),
@@ -8513,8 +8517,10 @@ process_relocs (Filedata * filedata)
 		printf ("'%s'", printable_section_name (filedata, section));
 
 	      num_rela = rel_size / section->sh_entsize;
-	      printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
-				" at offset 0x%lx contains %lu entries:\n",
+	      printf (ngettext (" at offset %#" PRIx64
+				" contains %" PRIu64 " entry:\n",
+				" at offset %#" PRIx64
+				" contains %" PRId64 " entries:\n",
 				num_rela),
 		      rel_offset, num_rela);
 
@@ -8524,11 +8530,11 @@ process_relocs (Filedata * filedata)
 	      if (section->sh_link != 0
 		  && section->sh_link < filedata->file_header.e_shnum)
 		{
-		  Elf_Internal_Shdr * symsec;
-		  Elf_Internal_Sym *  symtab;
-		  unsigned long nsyms;
-		  unsigned long strtablen = 0;
-		  char * strtab = NULL;
+		  Elf_Internal_Shdr *symsec;
+		  Elf_Internal_Sym *symtab;
+		  uint64_t nsyms;
+		  uint64_t strtablen = 0;
+		  char *strtab = NULL;
 
 		  symsec = filedata->section_headers + section->sh_link;
 		  if (symsec->sh_type != SHT_SYMTAB
@@ -8599,14 +8605,14 @@ struct absaddr
    name, if found, and the offset from the symbol to ADDR.  */
 
 static void
-find_symbol_for_address (Filedata *          filedata,
-			 Elf_Internal_Sym *  symtab,
-			 unsigned long       nsyms,
-			 const char *        strtab,
-			 unsigned long       strtab_size,
-			 struct absaddr      addr,
-			 const char **       symname,
-			 uint64_t *          offset)
+find_symbol_for_address (Filedata *filedata,
+			 Elf_Internal_Sym *symtab,
+			 uint64_t nsyms,
+			 const char *strtab,
+			 uint64_t strtab_size,
+			 struct absaddr addr,
+			 const char **symname,
+			 uint64_t *offset)
 {
   uint64_t dist = 0x100000;
   Elf_Internal_Sym * sym;
@@ -8679,24 +8685,24 @@ struct ia64_unw_table_entry
 struct ia64_unw_aux_info
 {
   struct ia64_unw_table_entry * table;		/* Unwind table.  */
-  unsigned long                 table_len;	/* Length of unwind table.  */
+  uint64_t                      table_len;	/* Length of unwind table.  */
   unsigned char *               info;		/* Unwind info.  */
-  unsigned long                 info_size;	/* Size of unwind info.  */
+  uint64_t                      info_size;	/* Size of unwind info.  */
   uint64_t                      info_addr;	/* Starting address of unwind info.  */
   uint64_t                      seg_base;	/* Starting address of segment.  */
   Elf_Internal_Sym *            symtab;		/* The symbol table.  */
-  unsigned long                 nsyms;		/* Number of symbols.  */
+  uint64_t                      nsyms;		/* Number of symbols.  */
   Elf_Internal_Sym *            funtab;		/* Sorted table of STT_FUNC symbols.  */
-  unsigned long                 nfuns;		/* Number of entries in funtab.  */
+  uint64_t                      nfuns;		/* Number of entries in funtab.  */
   char *                        strtab;		/* The string table.  */
-  unsigned long                 strtab_size;	/* Size of string table.  */
+  uint64_t                      strtab_size;	/* Size of string table.  */
 };
 
 static bool
 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
 {
   struct ia64_unw_table_entry * tp;
-  unsigned long j, nfuns;
+  size_t j, nfuns;
   int in_body;
   bool res = true;
 
@@ -8726,15 +8732,15 @@ dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
 	  fputs (procname, stdout);
 
 	  if (offset)
-	    printf ("+%lx", (unsigned long) offset);
+	    printf ("+%" PRIx64, offset);
 	}
 
       fputs (">: [", stdout);
       print_vma (tp->start.offset, PREFIX_HEX);
       fputc ('-', stdout);
       print_vma (tp->end.offset, PREFIX_HEX);
-      printf ("], info at +0x%lx\n",
-	      (unsigned long) (tp->info.offset - aux->seg_base));
+      printf ("], info at +0x%" PRIx64 "\n",
+	      tp->info.offset - aux->seg_base);
 
       /* PR 17531: file: 86232b32.  */
       if (aux->info == NULL)
@@ -8745,8 +8751,8 @@ dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
 	{
 	  if (tp->info.section >= filedata->file_header.e_shnum)
 	    {
-	      warn (_("Invalid section %u in table entry %ld\n"),
-		    tp->info.section, (long) (tp - aux->table));
+	      warn (_("Invalid section %u in table entry %td\n"),
+		    tp->info.section, tp - aux->table);
 	      res = false;
 	      continue;
 	    }
@@ -8757,8 +8763,8 @@ dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
       if (offset >= aux->info_size
 	  || aux->info_size - offset < 8)
 	{
-	  warn (_("Invalid offset %lx in table entry %ld\n"),
-		(long) tp->info.offset, (long) (tp - aux->table));
+	  warn (_("Invalid offset %" PRIx64 " in table entry %td\n"),
+		tp->info.offset, tp - aux->table);
 	  res = false;
 	  continue;
 	}
@@ -8798,7 +8804,7 @@ slurp_ia64_unwind_table (Filedata *                  filedata,
 			 struct ia64_unw_aux_info *  aux,
 			 Elf_Internal_Shdr *         sec)
 {
-  unsigned long size, nrelas, i;
+  uint64_t size, nrelas, i;
   Elf_Internal_Phdr * seg;
   struct ia64_unw_table_entry * tep;
   Elf_Internal_Shdr * relsec;
@@ -8904,7 +8910,8 @@ slurp_ia64_unwind_table (Filedata *                  filedata,
 	  /* PR 17531: file: 5bc8d9bf.  */
 	  if (i >= aux->table_len)
 	    {
-	      warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
+	      warn (_("Skipping reloc with overlarge offset: %#" PRIx64 "\n"),
+		    i);
 	      continue;
 	    }
 
@@ -8947,7 +8954,7 @@ ia64_process_unwind (Filedata * filedata)
 {
   Elf_Internal_Shdr * sec;
   Elf_Internal_Shdr * unwsec = NULL;
-  unsigned long i, unwcount = 0, unwstart = 0;
+  uint64_t i, unwcount = 0, unwstart = 0;
   struct ia64_unw_aux_info aux;
   bool res = true;
 
@@ -9082,9 +9089,9 @@ ia64_process_unwind (Filedata * filedata)
 	  else
 	    printf ("'%s'", printable_section_name (filedata, unwsec));
 
-	  printf (_(" at offset 0x%lx contains %lu entries:\n"),
-		  (unsigned long) unwsec->sh_offset,
-		  (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
+	  printf (_(" at offset %#" PRIx64 " contains %" PRIu64 " entries:\n"),
+		  unwsec->sh_offset,
+		  unwsec->sh_size / (3 * eh_addr_size));
 
 	  if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
 	      && aux.table_len > 0)
@@ -9143,21 +9150,21 @@ struct hppa_unw_table_entry
 struct hppa_unw_aux_info
 {
   struct hppa_unw_table_entry *  table;		/* Unwind table.  */
-  unsigned long                  table_len;	/* Length of unwind table.  */
+  uint64_t                       table_len;	/* Length of unwind table.  */
   uint64_t                       seg_base;	/* Starting address of segment.  */
   Elf_Internal_Sym *             symtab;	/* The symbol table.  */
-  unsigned long                  nsyms;		/* Number of symbols.  */
+  uint64_t                       nsyms;		/* Number of symbols.  */
   Elf_Internal_Sym *             funtab;	/* Sorted table of STT_FUNC symbols.  */
-  unsigned long                  nfuns;		/* Number of entries in funtab.  */
+  uint64_t                       nfuns;		/* Number of entries in funtab.  */
   char *                         strtab;	/* The string table.  */
-  unsigned long                  strtab_size;	/* Size of string table.  */
+  uint64_t                       strtab_size;	/* Size of string table.  */
 };
 
 static bool
 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
 {
   struct hppa_unw_table_entry * tp;
-  unsigned long j, nfuns;
+  uint64_t j, nfuns;
   bool res = true;
 
   aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
@@ -9183,7 +9190,7 @@ dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
 	  fputs (procname, stdout);
 
 	  if (offset)
-	    printf ("+%lx", (unsigned long) offset);
+	    printf ("+%" PRIx64, offset);
 	}
 
       fputs (">: [", stdout);
@@ -9237,7 +9244,7 @@ slurp_hppa_unwind_table (Filedata *                  filedata,
 			 struct hppa_unw_aux_info *  aux,
 			 Elf_Internal_Shdr *         sec)
 {
-  unsigned long size, unw_ent_size, nentries, nrelas, i;
+  uint64_t size, unw_ent_size, nentries, nrelas, i;
   Elf_Internal_Phdr * seg;
   struct hppa_unw_table_entry * tep;
   Elf_Internal_Shdr * relsec;
@@ -9372,7 +9379,8 @@ slurp_hppa_unwind_table (Filedata *                  filedata,
 	  i = rp->r_offset / unw_ent_size;
 	  if (i >= aux->table_len)
 	    {
-	      warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
+	      warn (_("Skipping reloc with overlarge offset: %#" PRIx64 "\n"),
+		    i);
 	      continue;
 	    }
 
@@ -9412,7 +9420,7 @@ hppa_process_unwind (Filedata * filedata)
   struct hppa_unw_aux_info aux;
   Elf_Internal_Shdr * unwsec = NULL;
   Elf_Internal_Shdr * sec;
-  unsigned long i;
+  size_t i;
   bool res = true;
 
   if (filedata->string_table == NULL)
@@ -9449,15 +9457,15 @@ hppa_process_unwind (Filedata * filedata)
       if (section_name_valid (filedata, sec)
 	  && streq (section_name (filedata, sec), ".PARISC.unwind"))
 	{
-	  unsigned long num_unwind = sec->sh_size / 16;
+	  uint64_t num_unwind = sec->sh_size / 16;
 
-	  printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
-			    "contains %lu entry:\n",
-			    "\nUnwind section '%s' at offset 0x%lx "
-			    "contains %lu entries:\n",
+	  printf (ngettext ("\nUnwind section '%s' at offset %#" PRIx64 " "
+			    "contains %" PRIu64 " entry:\n",
+			    "\nUnwind section '%s' at offset %#" PRIx64 " "
+			    "contains %" PRIu64 " entries:\n",
 			    num_unwind),
 		  printable_section_name (filedata, sec),
-		  (unsigned long) sec->sh_offset,
+		  sec->sh_offset,
 		  num_unwind);
 
           if (! slurp_hppa_unwind_table (filedata, &aux, sec))
@@ -9485,7 +9493,7 @@ struct arm_section
   unsigned char *      data;		/* The unwind data.  */
   Elf_Internal_Shdr *  sec;		/* The cached unwind section header.  */
   Elf_Internal_Rela *  rela;		/* The cached relocations for this section.  */
-  unsigned long        nrelas;		/* The number of relocations.  */
+  uint64_t             nrelas;		/* The number of relocations.  */
   unsigned int         rel_type;	/* REL or RELA ?  */
   Elf_Internal_Rela *  next_rela;	/* Cyclic pointer to the next reloc to process.  */
 };
@@ -9494,11 +9502,11 @@ struct arm_unw_aux_info
 {
   Filedata *          filedata;		/* The file containing the unwind sections.  */
   Elf_Internal_Sym *  symtab;		/* The file's symbol table.  */
-  unsigned long       nsyms;		/* Number of symbols.  */
+  uint64_t            nsyms;		/* Number of symbols.  */
   Elf_Internal_Sym *  funtab;		/* Sorted table of STT_FUNC symbols.  */
-  unsigned long       nfuns;		/* Number of these symbols.  */
+  uint64_t            nfuns;		/* Number of these symbols.  */
   char *              strtab;		/* The file's string table.  */
-  unsigned long       strtab_size;	/* Size of string table.  */
+  uint64_t            strtab_size;	/* Size of string table.  */
 };
 
 static const char *
@@ -9525,7 +9533,7 @@ arm_print_vma_and_name (Filedata *                 filedata,
       fputs (procname, stdout);
 
       if (sym_offset)
-	printf ("+0x%lx", (unsigned long) sym_offset);
+	printf ("+0x%" PRIx64, sym_offset);
       fputc ('>', stdout);
     }
 
@@ -9656,8 +9664,8 @@ get_unwind_section_word (Filedata *                 filedata,
 
       if (rp->r_offset & 3)
 	{
-	  warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
-		(unsigned long) rp->r_offset);
+	  warn (_("Skipping unexpected relocation at offset %#" PRIx64 "\n"),
+		rp->r_offset);
 	  continue;
 	}
 
@@ -9686,8 +9694,9 @@ get_unwind_section_word (Filedata *                 filedata,
       /* PR 17531 file: 027-1241568-0.004.  */
       if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
 	{
-	  error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
-		 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
+	  error (_("Bad symbol index in unwind relocation "
+		   "(%" PRIu64 " > %" PRIu64 ")\n"),
+		 ELF32_R_SYM (rp->r_info), aux->nsyms);
 	  break;
 	}
 
@@ -9930,7 +9939,7 @@ decode_arm_unwind_bytecode (Filedata *                 filedata,
 	{
 	  unsigned char buf[9];
 	  unsigned int i, len;
-	  unsigned long offset;
+	  uint64_t offset;
 
 	  for (i = 0; i < sizeof (buf); i++)
 	    {
@@ -9948,7 +9957,7 @@ decode_arm_unwind_bytecode (Filedata *                 filedata,
 	      offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
 	      assert (len == i + 1);
 	      offset = offset * 4 + 0x204;
-	      printf ("vsp = vsp + %ld", offset);
+	      printf ("vsp = vsp + %" PRId64, offset);
 	    }
 	}
       else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
@@ -10153,7 +10162,7 @@ decode_tic6x_unwind_bytecode (Filedata *                 filedata,
 	{
 	  unsigned char buf[9];
 	  unsigned int i, len;
-	  unsigned long offset;
+	  uint64_t offset;
 
 	  for (i = 0; i < sizeof (buf); i++)
 	    {
@@ -10171,7 +10180,7 @@ decode_tic6x_unwind_bytecode (Filedata *                 filedata,
 	  offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
 	  assert (len == i + 1);
 	  offset = offset * 8 + 0x408;
-	  printf (_("sp = sp + %ld"), offset);
+	  printf (_("sp = sp + %" PRId64), offset);
 	}
       else if ((op & 0xf0) == 0xe0)
 	{
@@ -10382,7 +10391,7 @@ dump_arm_unwind (Filedata *                 filedata,
 {
   struct arm_section exidx_arm_sec, extab_arm_sec;
   unsigned int i, exidx_len;
-  unsigned long j, nfuns;
+  uint64_t j, nfuns;
   bool res = true;
 
   memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
@@ -10459,8 +10468,8 @@ dump_arm_unwind (Filedata *                 filedata,
 	      /* PR 18879 */
 	      if (table_offset > table_sec->sh_size)
 		{
-		  warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
-			(unsigned long) table_offset,
+		  warn (_("Unwind entry contains corrupt offset (%#" PRIx64 ") into section %s\n"),
+			table_offset,
 			printable_section_name (filedata, table_sec));
 		  res = false;
 		  continue;
@@ -10475,8 +10484,8 @@ dump_arm_unwind (Filedata *                 filedata,
 
 	  if (table_sec == NULL)
 	    {
-	      warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
-		    (unsigned long) table);
+	      warn (_("Could not locate .ARM.extab section containing %#" PRIx64 ".\n"),
+		    table);
 	      res = false;
 	      continue;
 	    }
@@ -10504,7 +10513,7 @@ arm_process_unwind (Filedata * filedata)
   struct arm_unw_aux_info aux;
   Elf_Internal_Shdr *unwsec = NULL;
   Elf_Internal_Shdr *sec;
-  unsigned long i;
+  size_t i;
   unsigned int sec_type;
   bool res = true;
 
@@ -10557,14 +10566,14 @@ arm_process_unwind (Filedata * filedata)
       {
 	if (sec->sh_type == sec_type)
 	  {
-	    unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
-	    printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
-			      "contains %lu entry:\n",
-			      "\nUnwind section '%s' at offset 0x%lx "
-			      "contains %lu entries:\n",
+	    uint64_t num_unwind = sec->sh_size / (2 * eh_addr_size);
+	    printf (ngettext ("\nUnwind section '%s' at offset %#" PRIx64 " "
+			      "contains %" PRIu64 " entry:\n",
+			      "\nUnwind section '%s' at offset %#" PRIx64 " "
+			      "contains %" PRIu64 " entries:\n",
 			      num_unwind),
 		    printable_section_name (filedata, sec),
-		    (unsigned long) sec->sh_offset,
+		    sec->sh_offset,
 		    num_unwind);
 
 	    if (! dump_arm_unwind (filedata, &aux, sec))
@@ -10724,7 +10733,7 @@ dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
       {
 	static struct
 	{
-	  long int bit;
+	  unsigned int bit;
 	  const char * str;
 	}
 	flags[] =
@@ -10893,8 +10902,8 @@ get_32bit_dynamic_section (Filedata * filedata)
     = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
   if (filedata->dynamic_section == NULL)
     {
-      error (_("Out of memory allocating space for %lu dynamic entries\n"),
-	     (unsigned long) filedata->dynamic_nent);
+      error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
+	     filedata->dynamic_nent);
       free (edyn);
       return false;
     }
@@ -10944,8 +10953,8 @@ get_64bit_dynamic_section (Filedata * filedata)
     = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
   if (filedata->dynamic_section == NULL)
     {
-      error (_("Out of memory allocating space for %lu dynamic entries\n"),
-	     (unsigned long) filedata->dynamic_nent);
+      error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
+	     filedata->dynamic_nent);
       free (edyn);
       return false;
     }
@@ -11066,10 +11075,10 @@ get_dynamic_data (Filedata * filedata, uint64_t number, unsigned int ent_size)
   return i_data;
 }
 
-static unsigned long
+static uint64_t
 get_num_dynamic_syms (Filedata * filedata)
 {
-  unsigned long num_of_syms = 0;
+  uint64_t num_of_syms = 0;
 
   if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
     return num_of_syms;
@@ -11138,7 +11147,7 @@ get_num_dynamic_syms (Filedata * filedata)
       unsigned char nb[16];
       uint64_t i, maxchain = 0xffffffff, bitmaskwords;
       uint64_t buckets_vma;
-      unsigned long hn;
+      uint64_t hn;
 
       if (fseek64 (filedata->handle,
 		   (filedata->archive_file_offset
@@ -11327,7 +11336,7 @@ process_dynamic_section (Filedata * filedata)
   /* Find the appropriate symbol table.  */
   if (filedata->dynamic_symbols == NULL || do_histogram)
     {
-      unsigned long num_of_syms;
+      uint64_t num_of_syms;
 
       for (entry = filedata->dynamic_section;
 	   entry < filedata->dynamic_section + filedata->dynamic_nent;
@@ -11435,7 +11444,7 @@ the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
 	if (filedata->dynamic_info[DT_STRTAB]
 	    && filedata->dynamic_info[DT_STRSZ])
 	  {
-	    unsigned long offset;
+	    uint64_t offset;
 	    uint64_t str_tab_len = filedata->dynamic_info[DT_STRSZ];
 
 	    offset = offset_from_vma (filedata,
@@ -11467,7 +11476,7 @@ the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
   /* And find the syminfo section if available.  */
   if (filedata->dynamic_syminfo == NULL)
     {
-      unsigned long syminsz = 0;
+      uint64_t syminsz = 0;
 
       for (entry = filedata->dynamic_section;
 	   entry < filedata->dynamic_section + filedata->dynamic_nent;
@@ -11511,9 +11520,9 @@ the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
 	  filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
 	  if (filedata->dynamic_syminfo == NULL)
 	    {
-	      error (_("Out of memory allocating %lu bytes "
-		       "for dynamic symbol info\n"),
-		     (unsigned long) syminsz);
+	      error (_("Out of memory allocating %" PRIu64
+		       " bytes for dynamic symbol info\n"),
+		     syminsz);
 	      return false;
 	    }
 
@@ -11535,18 +11544,18 @@ the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
   if (do_dynamic && filedata->dynamic_addr)
     {
       if (filedata->is_separate)
-	printf (ngettext ("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entry:\n",
-			  "\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n",
-			  (unsigned long) filedata->dynamic_nent),
+	printf (ngettext ("\nIn linked file '%s' the dynamic section at offset %#" PRIx64 " contains %" PRIu64 " entry:\n",
+			  "\nIn linked file '%s' the dynamic section at offset %#" PRIx64 " contains %" PRIu64 " entries:\n",
+			  filedata->dynamic_nent),
 		filedata->file_name,
 		filedata->dynamic_addr,
-		(unsigned long) filedata->dynamic_nent);
+		filedata->dynamic_nent);
       else
-	printf (ngettext ("\nDynamic section at offset 0x%lx contains %lu entry:\n",
-			  "\nDynamic section at offset 0x%lx contains %lu entries:\n",
-			  (unsigned long) filedata->dynamic_nent),
+	printf (ngettext ("\nDynamic section at offset %#" PRIx64 " contains %" PRId64 " entry:\n",
+			  "\nDynamic section at offset %#" PRIx64 " contains %" PRIu64 " entries:\n",
+			  filedata->dynamic_nent),
 		filedata->dynamic_addr,
-		(unsigned long) filedata->dynamic_nent);
+		filedata->dynamic_nent);
     }
   if (do_dynamic)
     printf (_("  Tag        Type                         Name/Value\n"));
@@ -11624,7 +11633,7 @@ the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
 		printf (_(" None\n"));
 	      else
 		{
-		  unsigned long int val = entry->d_un.d_val;
+		  uint64_t val = entry->d_un.d_val;
 
 		  if (val & DTF_1_PARINIT)
 		    {
@@ -11637,7 +11646,7 @@ the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
 		      val ^= DTF_1_CONFEXP;
 		    }
 		  if (val != 0)
-		    printf (" %lx", val);
+		    printf (" %" PRIx64, val);
 		  puts ("");
 		}
 	    }
@@ -11652,7 +11661,7 @@ the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
 		printf (_(" None\n"));
 	      else
 		{
-		  unsigned long int val = entry->d_un.d_val;
+		  uint64_t val = entry->d_un.d_val;
 
 		  if (val & DF_P1_LAZYLOAD)
 		    {
@@ -11665,7 +11674,7 @@ the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
 		      val ^= DF_P1_GROUPPERM;
 		    }
 		  if (val != 0)
-		    printf (" %lx", val);
+		    printf (" %" PRIx64, val);
 		  puts ("");
 		}
 	    }
@@ -11679,7 +11688,7 @@ the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
 		printf (_(" None\n"));
 	      else
 		{
-		  unsigned long int val = entry->d_un.d_val;
+		  uint64_t val = entry->d_un.d_val;
 
 		  if (val & DF_1_NOW)
 		    {
@@ -11837,7 +11846,7 @@ the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
 		      val ^= DF_1_NOCOMMON;
 		    }
 		  if (val != 0)
-		    printf (" %lx", val);
+		    printf (" %" PRIx64, val);
 		  puts ("");
 		}
 	    }
@@ -11992,8 +12001,8 @@ the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
 	      tmp = gmtime (&atime);
 	      /* PR 17533 file: 041-1244816-0.004.  */
 	      if (tmp == NULL)
-		printf (_("<corrupt time val: %lx"),
-			(unsigned long) atime);
+		printf (_("<corrupt time val: %" PRIx64),
+			(uint64_t) atime);
 	      else
 		printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
@@ -12019,7 +12028,7 @@ the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
 		printf (_(" None\n"));
 	      else
 		{
-		  unsigned long int val = entry->d_un.d_val;
+		  uint64_t val = entry->d_un.d_val;
 
 		  if (val & DF_GNU_1_UNIQUE)
 		    {
@@ -12027,7 +12036,7 @@ the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
 		      val ^= DF_GNU_1_UNIQUE;
 		    }
 		  if (val != 0)
-		    printf (" %lx", val);
+		    printf (" %" PRIx64, val);
 		  puts ("");
 		}
 	    }
@@ -12128,8 +12137,8 @@ process_version_sections (Filedata * filedata)
 	case SHT_GNU_verdef:
 	  {
 	    Elf_External_Verdef * edefs;
-	    unsigned long idx;
-	    unsigned long cnt;
+	    size_t idx;
+	    size_t cnt;
 	    char * endbuf;
 
 	    found = true;
@@ -12151,8 +12160,8 @@ process_version_sections (Filedata * filedata)
 		      section->sh_info);
 
 	    printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
-	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
-		    (unsigned long) section->sh_offset, section->sh_link,
+	    printf (_("  Offset: 0x%08" PRIx64 "  Link: %u (%s)\n"),
+		    section->sh_offset, section->sh_link,
 		    printable_section_name_from_index (filedata, section->sh_link));
 
 	    edefs = (Elf_External_Verdef *)
@@ -12169,7 +12178,7 @@ process_version_sections (Filedata * filedata)
 		Elf_Internal_Verdef ent;
 		Elf_External_Verdaux * eaux;
 		Elf_Internal_Verdaux aux;
-		unsigned long isum;
+		size_t isum;
 		int j;
 
 		vstart = ((char *) edefs) + idx;
@@ -12186,7 +12195,7 @@ process_version_sections (Filedata * filedata)
 		ent.vd_aux     = BYTE_GET (edef->vd_aux);
 		ent.vd_next    = BYTE_GET (edef->vd_next);
 
-		printf (_("  %#06lx: Rev: %d  Flags: %s"),
+		printf (_("  %#06zx: Rev: %d  Flags: %s"),
 			idx, ent.vd_version, get_ver_flags (ent.vd_flags));
 
 		printf (_("  Index: %d  Cnt: %d  "),
@@ -12238,11 +12247,11 @@ process_version_sections (Filedata * filedata)
 		    aux.vda_next = BYTE_GET (eaux->vda_next);
 
 		    if (valid_dynamic_name (filedata, aux.vda_name))
-		      printf (_("  %#06lx: Parent %d: %s\n"),
+		      printf (_("  %#06zx: Parent %d: %s\n"),
 			      isum, j,
 			      get_dynamic_name (filedata, aux.vda_name));
 		    else
-		      printf (_("  %#06lx: Parent %d, name index: %ld\n"),
+		      printf (_("  %#06zx: Parent %d, name index: %ld\n"),
 			      isum, j, aux.vda_name);
 		  }
 
@@ -12274,8 +12283,8 @@ process_version_sections (Filedata * filedata)
 	case SHT_GNU_verneed:
 	  {
 	    Elf_External_Verneed * eneed;
-	    unsigned long idx;
-	    unsigned long cnt;
+	    size_t idx;
+	    size_t cnt;
 	    char * endbuf;
 
 	    found = true;
@@ -12297,8 +12306,8 @@ process_version_sections (Filedata * filedata)
 		      section->sh_info);
 
 	    printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
-	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
-		    (unsigned long) section->sh_offset, section->sh_link,
+	    printf (_("  Offset: 0x%08" PRIx64 "  Link: %u (%s)\n"),
+		    section->sh_offset, section->sh_link,
 		    printable_section_name_from_index (filedata, section->sh_link));
 
 	    eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
@@ -12313,7 +12322,7 @@ process_version_sections (Filedata * filedata)
 	      {
 		Elf_External_Verneed * entry;
 		Elf_Internal_Verneed ent;
-		unsigned long isum;
+		size_t isum;
 		int j;
 		char * vstart;
 
@@ -12329,7 +12338,7 @@ process_version_sections (Filedata * filedata)
 		ent.vn_aux     = BYTE_GET (entry->vn_aux);
 		ent.vn_next    = BYTE_GET (entry->vn_next);
 
-		printf (_("  %#06lx: Version: %d"), idx, ent.vn_version);
+		printf (_("  %#06zx: Version: %d"), idx, ent.vn_version);
 
 		if (valid_dynamic_name (filedata, ent.vn_file))
 		  printf (_("  File: %s"),
@@ -12360,10 +12369,10 @@ process_version_sections (Filedata * filedata)
 		    aux.vna_next  = BYTE_GET (eaux->vna_next);
 
 		    if (valid_dynamic_name (filedata, aux.vna_name))
-		      printf (_("  %#06lx:   Name: %s"),
+		      printf (_("  %#06zx:   Name: %s"),
 			      isum, get_dynamic_name (filedata, aux.vna_name));
 		    else
-		      printf (_("  %#06lx:   Name index: %lx"),
+		      printf (_("  %#06zx:   Name index: %lx"),
 			      isum, aux.vna_name);
 
 		    printf (_("  Flags: %s  Version: %d\n"),
@@ -12409,15 +12418,15 @@ process_version_sections (Filedata * filedata)
 	case SHT_GNU_versym:
 	  {
 	    Elf_Internal_Shdr * link_section;
-	    size_t total;
+	    uint64_t total;
 	    unsigned int cnt;
 	    unsigned char * edata;
 	    unsigned short * data;
 	    char * strtab;
 	    Elf_Internal_Sym * symbols;
 	    Elf_Internal_Shdr * string_sec;
-	    unsigned long num_syms;
-	    long off;
+	    uint64_t num_syms;
+	    uint64_t off;
 
 	    if (section->sh_link >= filedata->file_header.e_shnum)
 	      break;
@@ -12446,24 +12455,24 @@ process_version_sections (Filedata * filedata)
 	      }
 
 	    if (filedata->is_separate)
-	      printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
-				"\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
+	      printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %" PRIu64 " entry:\n",
+				"\nIn linked file '%s' the version symbols section '%s' contains %" PRIu64 " entries:\n",
 				total),
 		      filedata->file_name,
 		      printable_section_name (filedata, section),
-		      (unsigned long) total);
+		      total);
 	    else
 	      printf (ngettext ("\nVersion symbols section '%s' "
-				"contains %lu entry:\n",
+				"contains %" PRIu64 " entry:\n",
 				"\nVersion symbols section '%s' "
-				"contains %lu entries:\n",
+				"contains %" PRIu64 " entries:\n",
 				total),
 		      printable_section_name (filedata, section),
-		      (unsigned long) total);
+		      total);
 
 	    printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
-	    printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
-		    (unsigned long) section->sh_offset, section->sh_link,
+	    printf (_("  Offset: 0x%08" PRIx64 "  Link: %u (%s)\n"),
+		    section->sh_offset, section->sh_link,
 		    printable_section_name (filedata, link_section));
 
 	    off = offset_from_vma (filedata,
@@ -12512,7 +12521,7 @@ process_version_sections (Filedata * filedata)
 
 		      /* If this index value is greater than the size of the symbols
 		         array, break to avoid an out-of-bounds read.  */
-		      if ((unsigned long)(cnt + j) >= num_syms)
+		      if (cnt + j >= num_syms)
 		        {
 		          warn (_("invalid index into symbol array\n"));
 		          break;
@@ -12522,7 +12531,7 @@ process_version_sections (Filedata * filedata)
 		      if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
 			{
 			  Elf_Internal_Verneed ivn;
-			  unsigned long offset;
+			  uint64_t offset;
 
 			  offset = offset_from_vma
 			    (filedata,
@@ -12534,7 +12543,7 @@ process_version_sections (Filedata * filedata)
 			      Elf_Internal_Vernaux ivna;
 			      Elf_External_Verneed evn;
 			      Elf_External_Vernaux evna;
-			      unsigned long a_off;
+			      uint64_t a_off;
 
 			      if (get_data (&evn, filedata, offset, sizeof (evn), 1,
 					    _("version need")) == NULL)
@@ -12585,7 +12594,7 @@ process_version_sections (Filedata * filedata)
 			{
 			  Elf_Internal_Verdef ivd;
 			  Elf_External_Verdef evd;
-			  unsigned long offset;
+			  uint64_t offset;
 
 			  offset = offset_from_vma
 			    (filedata,
@@ -13017,18 +13026,18 @@ get_symbol_index_type (Filedata * filedata, unsigned int type)
 }
 
 static const char *
-get_symbol_version_string (Filedata *                   filedata,
-			   bool                         is_dynsym,
-			   const char *                 strtab,
-			   unsigned long int            strtab_size,
-			   unsigned int                 si,
-			   Elf_Internal_Sym *           psym,
-			   enum versioned_symbol_info * sym_info,
-			   unsigned short *             vna_other)
+get_symbol_version_string (Filedata *filedata,
+			   bool is_dynsym,
+			   const char *strtab,
+			   size_t strtab_size,
+			   unsigned int si,
+			   Elf_Internal_Sym *psym,
+			   enum versioned_symbol_info *sym_info,
+			   unsigned short *vna_other)
 {
   unsigned char data[2];
   unsigned short vers_data;
-  unsigned long offset;
+  uint64_t offset;
   unsigned short max_vd_ndx;
 
   if (!is_dynsym
@@ -13067,7 +13076,7 @@ get_symbol_version_string (Filedata *                   filedata,
       Elf_Internal_Verdef ivd;
       Elf_Internal_Verdaux ivda;
       Elf_External_Verdaux evda;
-      unsigned long off;
+      uint64_t off;
 
       off = offset_from_vma (filedata,
 			     filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
@@ -13131,7 +13140,7 @@ get_symbol_version_string (Filedata *                   filedata,
 				sizeof evn);
       do
 	{
-	  unsigned long vna_off;
+	  uint64_t vna_off;
 
 	  if (get_data (&evn, filedata, offset, sizeof (evn), 1,
 			_("version need")) == NULL)
@@ -13213,7 +13222,7 @@ print_dynamic_symbol_size (uint64_t vma, int base)
 }
 
 static void
-print_dynamic_symbol (Filedata *filedata, unsigned long si,
+print_dynamic_symbol (Filedata *filedata, uint64_t si,
 		      Elf_Internal_Sym *symtab,
 		      Elf_Internal_Shdr *section,
 		      char *strtab, size_t strtab_size)
@@ -13225,7 +13234,7 @@ print_dynamic_symbol (Filedata *filedata, unsigned long si,
   const char * sstr;
   Elf_Internal_Sym *psym = symtab + si;
 
-  printf ("%6ld: ", si);
+  printf ("%6" PRId64 ": ", si);
   print_vma (psym->st_value, LONG_HEX);
   putchar (' ');
   print_dynamic_symbol_size (psym->st_size, sym_base);
@@ -13306,7 +13315,7 @@ print_dynamic_symbol (Filedata *filedata, unsigned long si,
       /* Solaris binaries have been found to violate this requirement as
 	 well.  Not sure if this is a bug or an ABI requirement.  */
       && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
-    warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
+    warn (_("local symbol %" PRIu64 " found at index >= %s's sh_info value of %u\n"),
 	  si, printable_section_name (filedata, section), section->sh_info);
 }
 
@@ -13390,9 +13399,9 @@ display_lto_symtab (Filedata *           filedata,
 
   if (section->sh_size > filedata->file_size)
     {
-      error (_("Section %s has an invalid sh_size of 0x%lx\n"),
+      error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
 	     printable_section_name (filedata, section),
-	     (unsigned long) section->sh_size);
+	     section->sh_size);
       return false;
     }
 
@@ -13586,20 +13595,24 @@ process_symbol_table (Filedata * filedata)
       && filedata->dynamic_strings != NULL
       && filedata->dynamic_symbols != NULL)
     {
-      unsigned long si;
+      uint64_t si;
 
       if (filedata->is_separate)
 	{
-	  printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
-			    "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
+	  printf (ngettext ("\nIn linked file '%s' the dynamic symbol table"
+			    " contains %" PRIu64 " entry:\n",
+			    "\nIn linked file '%s' the dynamic symbol table"
+			    " contains %" PRIu64 " entries:\n",
 			    filedata->num_dynamic_syms),
 		  filedata->file_name,
 		  filedata->num_dynamic_syms);
 	}
       else
 	{
-	  printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
-			    "\nSymbol table for image contains %lu entries:\n",
+	  printf (ngettext ("\nSymbol table for image contains %" PRIu64
+			    " entry:\n",
+			    "\nSymbol table for image contains %" PRIu64
+			    " entries:\n",
 			    filedata->num_dynamic_syms),
 		  filedata->num_dynamic_syms);
 	}
@@ -13623,9 +13636,9 @@ process_symbol_table (Filedata * filedata)
 	   i++, section++)
 	{
 	  char * strtab = NULL;
-	  unsigned long int strtab_size = 0;
+	  uint64_t strtab_size = 0;
 	  Elf_Internal_Sym * symtab;
-	  unsigned long si, num_syms;
+	  uint64_t si, num_syms;
 
 	  if ((section->sh_type != SHT_SYMTAB
 	       && section->sh_type != SHT_DYNSYM)
@@ -13643,15 +13656,19 @@ process_symbol_table (Filedata * filedata)
 	  num_syms = section->sh_size / section->sh_entsize;
 
 	  if (filedata->is_separate)
-	    printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
-			      "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
+	    printf (ngettext ("\nIn linked file '%s' symbol section '%s'"
+			      " contains %" PRIu64 " entry:\n",
+			      "\nIn linked file '%s' symbol section '%s'"
+			      " contains %" PRIu64 " entries:\n",
 			      num_syms),
 		    filedata->file_name,
 		    printable_section_name (filedata, section),
 		    num_syms);
 	  else
-	    printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
-			      "\nSymbol table '%s' contains %lu entries:\n",
+	    printf (ngettext ("\nSymbol table '%s' contains %" PRIu64
+			      " entry:\n",
+			      "\nSymbol table '%s' contains %" PRIu64
+			      " entries:\n",
 			      num_syms),
 		    printable_section_name (filedata, section),
 		    num_syms);
@@ -13697,24 +13714,23 @@ process_symbol_table (Filedata * filedata)
 
   if (do_histogram && filedata->buckets != NULL)
     {
-      unsigned long * lengths;
-      unsigned long * counts;
-      unsigned long hn;
+      uint64_t *lengths;
+      uint64_t *counts;
+      uint64_t hn;
       uint64_t si;
-      unsigned long maxlength = 0;
-      unsigned long nzero_counts = 0;
-      unsigned long nsyms = 0;
+      uint64_t maxlength = 0;
+      uint64_t nzero_counts = 0;
+      uint64_t nsyms = 0;
       char *visited;
 
       printf (ngettext ("\nHistogram for bucket list length "
-			"(total of %lu bucket):\n",
+			"(total of %" PRIu64 " bucket):\n",
 			"\nHistogram for bucket list length "
-			"(total of %lu buckets):\n",
-			(unsigned long) filedata->nbuckets),
-	      (unsigned long) filedata->nbuckets);
+			"(total of %" PRIu64 " buckets):\n",
+			filedata->nbuckets),
+	      filedata->nbuckets);
 
-      lengths = (unsigned long *) calloc (filedata->nbuckets,
-					  sizeof (*lengths));
+      lengths = calloc (filedata->nbuckets, sizeof (*lengths));
       if (lengths == NULL)
 	{
 	  error (_("Out of memory allocating space for histogram buckets\n"));
@@ -13741,7 +13757,7 @@ process_symbol_table (Filedata * filedata)
 	}
       free (visited);
 
-      counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
+      counts = calloc (maxlength + 1, sizeof (*counts));
       if (counts == NULL)
 	{
 	  free (lengths);
@@ -13754,13 +13770,13 @@ process_symbol_table (Filedata * filedata)
 
       if (filedata->nbuckets > 0)
 	{
-	  unsigned long i;
-	  printf ("      0  %-10lu (%5.1f%%)\n",
+	  uint64_t i;
+	  printf ("      0  %-10" PRIu64 " (%5.1f%%)\n",
 		  counts[0], (counts[0] * 100.0) / filedata->nbuckets);
 	  for (i = 1; i <= maxlength; ++i)
 	    {
 	      nzero_counts += counts[i] * i;
-	      printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
+	      printf ("%7" PRIu64 "  %-10" PRIu64 " (%5.1f%%)    %5.1f%%\n",
 		      i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
 		      (nzero_counts * 100.0) / nsyms);
 	    }
@@ -13778,23 +13794,22 @@ process_symbol_table (Filedata * filedata)
 
   if (do_histogram && filedata->gnubuckets != NULL)
     {
-      unsigned long * lengths;
-      unsigned long * counts;
-      unsigned long hn;
-      unsigned long maxlength = 0;
-      unsigned long nzero_counts = 0;
-      unsigned long nsyms = 0;
+      uint64_t *lengths;
+      uint64_t *counts;
+      uint64_t hn;
+      uint64_t maxlength = 0;
+      uint64_t nzero_counts = 0;
+      uint64_t nsyms = 0;
 
       printf (ngettext ("\nHistogram for `%s' bucket list length "
-			"(total of %lu bucket):\n",
+			"(total of %" PRIu64 " bucket):\n",
 			"\nHistogram for `%s' bucket list length "
-			"(total of %lu buckets):\n",
-			(unsigned long) filedata->ngnubuckets),
+			"(total of %" PRIu64 " buckets):\n",
+			filedata->ngnubuckets),
 	      GNU_HASH_SECTION_NAME (filedata),
-	      (unsigned long) filedata->ngnubuckets);
+	      filedata->ngnubuckets);
 
-      lengths = (unsigned long *) calloc (filedata->ngnubuckets,
-					  sizeof (*lengths));
+      lengths = calloc (filedata->ngnubuckets, sizeof (*lengths));
       if (lengths == NULL)
 	{
 	  error (_("Out of memory allocating space for gnu histogram buckets\n"));
@@ -13820,7 +13835,7 @@ process_symbol_table (Filedata * filedata)
 	    nsyms += length;
 	  }
 
-      counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
+      counts = calloc (maxlength + 1, sizeof (*counts));
       if (counts == NULL)
 	{
 	  free (lengths);
@@ -13833,13 +13848,13 @@ process_symbol_table (Filedata * filedata)
 
       if (filedata->ngnubuckets > 0)
 	{
-	  unsigned long j;
-	  printf ("      0  %-10lu (%5.1f%%)\n",
+	  uint64_t j;
+	  printf ("      0  %-10" PRIu64 " (%5.1f%%)\n",
 		  counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
 	  for (j = 1; j <= maxlength; ++j)
 	    {
 	      nzero_counts += counts[j] * j;
-	      printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
+	      printf ("%7" PRIu64 "  %-10" PRIu64 " (%5.1f%%)    %5.1f%%\n",
 		      j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
 		      (nzero_counts * 100.0) / nsyms);
 	    }
@@ -13890,17 +13905,17 @@ process_syminfo (Filedata * filedata)
     return false;
 
   if (filedata->is_separate)
-    printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
-		      "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
+    printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset %#" PRIx64 " contains %d entry:\n",
+		      "\nIn linked file '%s: the dynamic info segment at offset %#" PRIx64 " contains %d entries:\n",
 		      filedata->dynamic_syminfo_nent),
 	    filedata->file_name,
 	    filedata->dynamic_syminfo_offset,
 	    filedata->dynamic_syminfo_nent);
   else
-    printf (ngettext ("\nDynamic info segment at offset 0x%lx "
-		      "contains %d entry:\n",
-		      "\nDynamic info segment at offset 0x%lx "
-		      "contains %d entries:\n",
+    printf (ngettext ("\nDynamic info segment at offset %#" PRIx64
+		      " contains %d entry:\n",
+		      "\nDynamic info segment at offset %#" PRIx64
+		      " contains %d entries:\n",
 		      filedata->dynamic_syminfo_nent),
 	    filedata->dynamic_syminfo_offset,
 	    filedata->dynamic_syminfo_nent);
@@ -13974,15 +13989,15 @@ process_syminfo (Filedata * filedata)
    discarded.  */
 
 static bool
-target_specific_reloc_handling (Filedata *           filedata,
-				Elf_Internal_Rela *  reloc,
-				unsigned char *      start,
-				unsigned char *      end,
-				Elf_Internal_Sym *   symtab,
-				unsigned long        num_syms)
+target_specific_reloc_handling (Filedata *filedata,
+				Elf_Internal_Rela *reloc,
+				unsigned char *start,
+				unsigned char *end,
+				Elf_Internal_Sym *symtab,
+				uint64_t num_syms)
 {
   unsigned int reloc_type = 0;
-  unsigned long sym_index = 0;
+  uint64_t sym_index = 0;
 
   if (reloc)
     {
@@ -14014,8 +14029,8 @@ target_specific_reloc_handling (Filedata *           filedata,
 	  case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
 	    /* PR 21139.  */
 	    if (sym_index >= num_syms)
-	      error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
-		     sym_index);
+	      error (_("MSP430 SYM_DIFF reloc contains invalid symbol index"
+		       " %" PRIu64 "\n"), sym_index);
 	    else
 	      saved_sym = symtab + sym_index;
 	    return true;
@@ -14061,11 +14076,12 @@ target_specific_reloc_handling (Filedata *           filedata,
 		  }
 
 		if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
-		  error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
-			   "ULEB128 value\n"),
-			 (long) reloc->r_offset);
+		  error (_("MSP430 ULEB128 field at %#" PRIx64
+			   " contains invalid ULEB128 value\n"),
+			 reloc->r_offset);
 		else if (sym_index >= num_syms)
-		  error (_("MSP430 reloc contains invalid symbol index %lu\n"),
+		  error (_("MSP430 reloc contains invalid symbol index "
+			   "%" PRIu64 "\n"),
 			 sym_index);
 		else
 		  {
@@ -14076,8 +14092,9 @@ target_specific_reloc_handling (Filedata *           filedata,
 		      byte_put (start + reloc->r_offset, value, reloc_size);
 		    else
 		      /* PR 21137 */
-		      error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
-			     (long) reloc->r_offset);
+		      error (_("MSP430 sym diff reloc contains invalid offset: "
+			       "%#" PRIx64 "\n"),
+			     reloc->r_offset);
 		  }
 
 		saved_sym = NULL;
@@ -14110,7 +14127,8 @@ target_specific_reloc_handling (Filedata *           filedata,
 	    return true;
 	  case 33: /* R_MN10300_SYM_DIFF */
 	    if (sym_index >= num_syms)
-	      error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
+	      error (_("MN10300_SYM_DIFF reloc contains invalid symbol index "
+		       "%" PRIu64 "\n"),
 		     sym_index);
 	    else
 	      saved_sym = symtab + sym_index;
@@ -14124,7 +14142,8 @@ target_specific_reloc_handling (Filedata *           filedata,
 		uint64_t value;
 
 		if (sym_index >= num_syms)
-		  error (_("MN10300 reloc contains invalid symbol index %lu\n"),
+		  error (_("MN10300 reloc contains invalid symbol index "
+			   "%" PRIu64 "\n"),
 			 sym_index);
 		else
 		  {
@@ -14134,8 +14153,9 @@ target_specific_reloc_handling (Filedata *           filedata,
 		    if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
 		      byte_put (start + reloc->r_offset, value, reloc_size);
 		    else
-		      error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
-			     (long) reloc->r_offset);
+		      error (_("MN10300 sym diff reloc contains invalid offset:"
+			       " %#" PRIx64 "\n"),
+			     reloc->r_offset);
 		  }
 
 		saved_sym = NULL;
@@ -14167,8 +14187,8 @@ target_specific_reloc_handling (Filedata *           filedata,
 	  case 0x80: /* R_RL78_SYM.  */
 	    saved_sym1 = saved_sym2;
 	    if (sym_index >= num_syms)
-	      error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
-		     sym_index);
+	      error (_("RL78_SYM reloc contains invalid symbol index "
+		       "%" PRIu64 "\n"), sym_index);
 	    else
 	      {
 		saved_sym2 = symtab[sym_index].st_value;
@@ -14186,8 +14206,9 @@ target_specific_reloc_handling (Filedata *           filedata,
 	    if (IN_RANGE (start, end, start + reloc->r_offset, 4))
 	      byte_put (start + reloc->r_offset, value, 4);
 	    else
-	      error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
-		     (long) reloc->r_offset);
+	      error (_("RL78 sym diff reloc contains invalid offset: "
+		       "%#" PRIx64 "\n"),
+		     reloc->r_offset);
 	    value = 0;
 	    return true;
 
@@ -14195,8 +14216,9 @@ target_specific_reloc_handling (Filedata *           filedata,
 	    if (IN_RANGE (start, end, start + reloc->r_offset, 2))
 	      byte_put (start + reloc->r_offset, value, 2);
 	    else
-	      error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
-		     (long) reloc->r_offset);
+	      error (_("RL78 sym diff reloc contains invalid offset: "
+		       "%#" PRIx64 "\n"),
+		     reloc->r_offset);
 	    value = 0;
 	    return true;
 
@@ -14965,7 +14987,7 @@ apply_relocations (Filedata *filedata,
 		   unsigned char *start,
 		   size_t size,
 		   void **relocs_return,
-		   unsigned long *num_relocs_return)
+		   uint64_t *num_relocs_return)
 {
   Elf_Internal_Shdr * relsec;
   unsigned char * end = start + size;
@@ -14986,12 +15008,12 @@ apply_relocations (Filedata *filedata,
        ++relsec)
     {
       bool is_rela;
-      unsigned long num_relocs;
+      uint64_t num_relocs;
       Elf_Internal_Rela * relocs;
       Elf_Internal_Rela * rp;
       Elf_Internal_Shdr * symsec;
       Elf_Internal_Sym * symtab;
-      unsigned long num_syms;
+      uint64_t num_syms;
       Elf_Internal_Sym * sym;
 
       if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
@@ -15035,7 +15057,7 @@ apply_relocations (Filedata *filedata,
 	  bool reloc_inplace = false;
 	  bool reloc_subtract = false;
 	  unsigned char *rloc;
-	  unsigned long sym_index;
+	  uint64_t sym_index;
 
 	  reloc_type = get_reloc_type (filedata, rp->r_info);
 
@@ -15104,16 +15126,18 @@ apply_relocations (Filedata *filedata,
 	  rloc = start + rp->r_offset;
 	  if (!IN_RANGE (start, end, rloc, reloc_size))
 	    {
-	      warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
-		    (unsigned long) rp->r_offset,
+	      warn (_("skipping invalid relocation offset %#" PRIx64
+		      " in section %s\n"),
+		    rp->r_offset,
 		    printable_section_name (filedata, section));
 	      continue;
 	    }
 
-	  sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
+	  sym_index = get_reloc_symindex (rp->r_info);
 	  if (sym_index >= num_syms)
 	    {
-	      warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
+	      warn (_("skipping invalid relocation symbol index %#" PRIx64
+		      " in section %s\n"),
 		    sym_index, printable_section_name (filedata, section));
 	      continue;
 	    }
@@ -15136,10 +15160,10 @@ apply_relocations (Filedata *filedata,
 	      && ELF_ST_TYPE (sym->st_info) != STT_COMMON
 	      && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
 	    {
-	      warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
+	      warn (_("skipping unexpected symbol type %s in section %s relocation %tu\n"),
 		    get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
 		    printable_section_name (filedata, relsec),
-		    (long int)(rp - relocs));
+		    rp - relocs);
 	      continue;
 	    }
 
@@ -15443,7 +15467,7 @@ dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
 	    }
 	  else
 	    {
-	      printf ("  [%6lx]  ", (unsigned long) (data - start));
+	      printf ("  [%6tx]  ", data - start);
 	    }
 
 	  if (maxlen > 0)
@@ -15660,7 +15684,7 @@ dump_section_as_bytes (Elf_Internal_Shdr *section,
 
       lbytes = (bytes > 16 ? 16 : bytes);
 
-      printf ("  0x%8.8lx ", (unsigned long) addr);
+      printf ("  0x%8.8" PRIx64 " ", addr);
 
       for (j = 0; j < 16; j++)
 	{
@@ -16079,7 +16103,7 @@ get_build_id (void * data)
 {
   Filedata * filedata = (Filedata *) data;
   Elf_Internal_Shdr * shdr;
-  unsigned long i;
+  size_t i;
 
   /* Iterate through notes to find note.gnu.build-id.
      FIXME: Only the first note in any note section is examined.  */
@@ -16186,7 +16210,7 @@ malformed note encountered in section %s whilst scanning for build-id note\n"),
           && startswith (inote.namedata, "GNU")
           && inote.type == NT_GNU_BUILD_ID)
         {
-          unsigned long j;
+          size_t j;
           char * build_id;
 
           build_id = malloc (inote.descsz * 2 + 1);
@@ -16517,7 +16541,7 @@ display_tag_value (signed int tag,
 		   unsigned char * p,
 		   const unsigned char * const end)
 {
-  unsigned long val;
+  uint64_t val;
 
   if (tag > 0)
     printf ("  Tag_unknown_%d: ", tag);
@@ -16547,7 +16571,7 @@ display_tag_value (signed int tag,
   else
     {
       READ_ULEB (val, p, end);
-      printf ("%ld (0x%lx)\n", val, val);
+      printf ("%" PRId64 " (0x%" PRIx64 ")\n", val, val);
     }
 
   assert (p <= end);
@@ -17679,7 +17703,7 @@ display_tic6x_attribute (unsigned char * p,
 static void
 display_raw_attribute (unsigned char * p, unsigned char const * const end)
 {
-  unsigned long addr = 0;
+  uint64_t addr = 0;
   size_t bytes = end - p;
 
   assert (end >= p);
@@ -17689,7 +17713,7 @@ display_raw_attribute (unsigned char * p, unsigned char const * const end)
       int k;
       int lbytes = (bytes > 16 ? 16 : bytes);
 
-      printf ("  0x%8.8lx ", addr);
+      printf ("  0x%8.8" PRIx64 " ", addr);
 
       for (j = 0; j < 16; j++)
 	{
@@ -17723,10 +17747,10 @@ display_raw_attribute (unsigned char * p, unsigned char const * const end)
 
 static unsigned char *
 display_msp430_attribute (unsigned char * p,
-			   const unsigned char * const end)
+			  const unsigned char * const end)
 {
-  unsigned int val;
-  unsigned int tag;
+  uint64_t val;
+  uint64_t tag;
 
   READ_ULEB (tag, p, end);
 
@@ -17740,7 +17764,7 @@ display_msp430_attribute (unsigned char * p,
 	case 0: printf (_("None\n")); break;
 	case 1: printf (_("MSP430\n")); break;
 	case 2: printf (_("MSP430X\n")); break;
-	default: printf ("??? (%d)\n", val); break;
+	default: printf ("??? (%" PRId64 ")\n", val); break;
 	}
       break;
 
@@ -17752,7 +17776,7 @@ display_msp430_attribute (unsigned char * p,
 	case 0: printf (_("None\n")); break;
 	case 1: printf (_("Small\n")); break;
 	case 2: printf (_("Large\n")); break;
-	default: printf ("??? (%d)\n", val); break;
+	default: printf ("??? (%" PRId64 ")\n", val); break;
 	}
       break;
 
@@ -17765,12 +17789,12 @@ display_msp430_attribute (unsigned char * p,
 	case 1: printf (_("Small\n")); break;
 	case 2: printf (_("Large\n")); break;
 	case 3: printf (_("Restricted Large\n")); break;
-	default: printf ("??? (%d)\n", val); break;
+	default: printf ("??? (%" PRId64 ")\n", val); break;
 	}
       break;
 
     default:
-      printf (_("  <unknown tag %d>: "), tag);
+      printf (_("  <unknown tag %" PRId64 ">: "), tag);
 
       if (tag & 1)
 	{
@@ -17792,7 +17816,7 @@ display_msp430_attribute (unsigned char * p,
       else
 	{
 	  READ_ULEB (val, p, end);
-	  printf ("%d (0x%x)\n", val, val);
+	  printf ("%" PRId64 " (0x%" PRIx64 ")\n", val, val);
 	}
       break;
    }
@@ -17808,7 +17832,7 @@ display_msp430_gnu_attribute (unsigned char * p,
 {
   if (tag == Tag_GNU_MSP430_Data_Region)
     {
-      unsigned int val;
+      uint64_t val;
 
       printf ("  Tag_GNU_MSP430_Data_Region: ");
       READ_ULEB (val, p, end);
@@ -17822,7 +17846,7 @@ display_msp430_gnu_attribute (unsigned char * p,
 	  printf (_("Lower Region Only\n"));
 	  break;
 	default:
-	  printf ("??? (%u)\n", val);
+	  printf ("??? (%" PRIu64 ")\n", val);
 	}
       return p;
     }
@@ -17850,8 +17874,8 @@ static unsigned char *
 display_riscv_attribute (unsigned char *p,
 			 const unsigned char * const end)
 {
-  unsigned int val;
-  unsigned int tag;
+  uint64_t val;
+  uint64_t tag;
   struct riscv_attr_tag_t *attr = NULL;
   unsigned i;
 
@@ -17878,7 +17902,7 @@ display_riscv_attribute (unsigned char *p,
     case Tag_RISCV_priv_spec_minor:
     case Tag_RISCV_priv_spec_revision:
       READ_ULEB (val, p, end);
-      printf (_("%u\n"), val);
+      printf ("%" PRIu64 "\n", val);
       break;
     case Tag_RISCV_unaligned_access:
       READ_ULEB (val, p, end);
@@ -17894,7 +17918,7 @@ display_riscv_attribute (unsigned char *p,
       break;
     case Tag_RISCV_stack_align:
       READ_ULEB (val, p, end);
-      printf (_("%u-bytes\n"), val);
+      printf (_("%" PRIu64 "-bytes\n"), val);
       break;
     case Tag_RISCV_arch:
       p = display_tag_value (-1, p, end);
@@ -17910,8 +17934,8 @@ static unsigned char *
 display_csky_attribute (unsigned char * p,
 			const unsigned char * const end)
 {
-  unsigned int tag;
-  unsigned int val;
+  uint64_t tag;
+  uint64_t val;
   READ_ULEB (tag, p, end);
 
   if (tag >= Tag_CSKY_MAX)
@@ -17947,7 +17971,7 @@ display_csky_attribute (unsigned char * p,
     case Tag_CSKY_VDSP_VERSION:
       printf ("  Tag_CSKY_VDSP_VERSION:\t");
       READ_ULEB (val, p, end);
-      printf ("VDSP Version %d\n", val);
+      printf ("VDSP Version %" PRId64 "\n", val);
       break;
 
     case Tag_CSKY_FPU_VERSION:
@@ -18640,10 +18664,10 @@ process_mips_specific (Filedata * filedata)
 					      _("liblist section data"));
       if (elib)
 	{
-	  printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
-			    "\nSection '.liblist' contains %lu entries:\n",
-			    (unsigned long) liblistno),
-		  (unsigned long) liblistno);
+	  printf (ngettext ("\nSection '.liblist' contains %zu entry:\n",
+			    "\nSection '.liblist' contains %zu entries:\n",
+			    liblistno),
+		  liblistno);
 	  fputs (_("     Library              Time Stamp          Checksum   Version Flags\n"),
 		 stdout);
 
@@ -18666,7 +18690,7 @@ process_mips_specific (Filedata * filedata)
 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
 			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
 
-	      printf ("%3lu: ", (unsigned long) cnt);
+	      printf ("%3zu: ", cnt);
 	      if (valid_dynamic_name (filedata, liblist.l_name))
 		print_symbol (20, get_dynamic_name (filedata, liblist.l_name));
 	      else
@@ -18978,8 +19002,8 @@ process_mips_specific (Filedata * filedata)
 	 if we are sure that the cmalloc will fail.  */
       if (conflictsno > filedata->file_size / sizeof (* iconf))
 	{
-	  error (_("Overlarge number of conflicts detected: %lx\n"),
-		 (long) conflictsno);
+	  error (_("Overlarge number of conflicts detected: %zx\n"),
+		 conflictsno);
 	  return false;
 	}
 
@@ -19027,15 +19051,15 @@ process_mips_specific (Filedata * filedata)
 	  free (econf64);
 	}
 
-      printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
-			"\nSection '.conflict' contains %lu entries:\n",
-			(unsigned long) conflictsno),
-	      (unsigned long) conflictsno);
+      printf (ngettext ("\nSection '.conflict' contains %zu entry:\n",
+			"\nSection '.conflict' contains %zu entries:\n",
+			conflictsno),
+	      conflictsno);
       puts (_("  Num:    Index       Value  Name"));
 
       for (cnt = 0; cnt < conflictsno; ++cnt)
 	{
-	  printf ("%5lu: %8lu  ", (unsigned long) cnt, iconf[cnt]);
+	  printf ("%5zu: %8lu  ", cnt, iconf[cnt]);
 
 	  if (iconf[cnt] >= filedata->num_dynamic_syms)
 	    printf (_("<corrupt symbol index>"));
@@ -19072,8 +19096,9 @@ process_mips_specific (Filedata * filedata)
       /* PR binutils/17533 file: 012-111227-0.004  */
       if (symtabno < gotsym)
 	{
-	  error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
-		 (unsigned long) gotsym, (unsigned long) symtabno);
+	  error (_("The GOT symbol offset (%" PRIu64
+		   ") is greater than the symbol table size (%" PRIu64 ")\n"),
+		 gotsym, symtabno);
 	  return false;
 	}
 
@@ -19081,7 +19106,7 @@ process_mips_specific (Filedata * filedata)
       /* PR 17531: file: 54c91a34.  */
       if (global_end < local_end)
 	{
-	  error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
+	  error (_("Too many GOT symbols: %" PRIu64 "\n"), symtabno);
 	  return false;
 	}
 
@@ -19178,8 +19203,8 @@ process_mips_specific (Filedata * filedata)
 		    printf (_("<corrupt: %14ld>"), psym->st_name);
 		}
 	      else
-		printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
-			(unsigned long) i);
+		printf (_("<symbol index %zu exceeds number of dynamic symbols>"),
+			i);
 
 	      printf ("\n");
 	      if (ent == (uint64_t) -1)
@@ -19195,8 +19220,8 @@ process_mips_specific (Filedata * filedata)
   if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
     {
       uint64_t ent, end;
-      size_t offset, rel_offset;
-      unsigned long count, i;
+      uint64_t offset, rel_offset;
+      uint64_t count, i;
       unsigned char * data;
       int addr_size, sym_width;
       Elf_Internal_Rela * rels;
@@ -19244,13 +19269,13 @@ process_mips_specific (Filedata * filedata)
       sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
       for (i = 0; i < count; i++)
 	{
-	  unsigned long idx = get_reloc_symindex (rels[i].r_info);
+	  uint64_t idx = get_reloc_symindex (rels[i].r_info);
 
 	  ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
 	  printf (" ");
 
 	  if (idx >= filedata->num_dynamic_syms)
-	    printf (_("<corrupt symbol index: %lu>"), idx);
+	    printf (_("<corrupt symbol index: %" PRIu64 ">"), idx);
 	  else
 	    {
 	      Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
@@ -19325,7 +19350,7 @@ process_gnu_liblist (Filedata * filedata)
   char * strtab;
   size_t strtab_size;
   size_t cnt;
-  unsigned long num_liblist;
+  uint64_t num_liblist;
   unsigned i;
   bool res = true;
 
@@ -19367,8 +19392,10 @@ process_gnu_liblist (Filedata * filedata)
 	  strtab_size = string_sec->sh_size;
 
 	  num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
-	  printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
-			    "\nLibrary list section '%s' contains %lu entries:\n",
+	  printf (ngettext ("\nLibrary list section '%s' contains %" PRIu64
+			    " entries:\n",
+			    "\nLibrary list section '%s' contains %" PRIu64
+			    " entries:\n",
 			    num_liblist),
 		  printable_section_name (filedata, section),
 		  num_liblist);
@@ -19395,7 +19422,7 @@ process_gnu_liblist (Filedata * filedata)
 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
 			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
 
-	      printf ("%3lu: ", (unsigned long) cnt);
+	      printf ("%3zu: ", cnt);
 	      if (do_wide)
 		printf ("%-20s", liblist.l_name < strtab_size
 			? strtab + liblist.l_name : _("<corrupt>"));
@@ -20232,7 +20259,7 @@ print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
 	      if (datasz != size)
 		printf (_("<corrupt length: %#x> "), datasz);
 	      else
-		printf ("%#lx", (unsigned long) byte_get (ptr, size));
+		printf ("%#" PRIx64, byte_get (ptr, size));
 	      goto next;
 
 	    case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
@@ -20310,7 +20337,7 @@ print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
     {
     case NT_GNU_BUILD_ID:
       {
-	unsigned long i;
+	size_t i;
 
 	printf (_("    Build ID: "));
 	for (i = 0; i < pnote->descsz; ++i)
@@ -20321,7 +20348,7 @@ print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
 
     case NT_GNU_ABI_TAG:
       {
-	unsigned long os, major, minor, subminor;
+	unsigned int os, major, minor, subminor;
 	const char *osname;
 
 	/* PR 17531: file: 030-599401-0.004.  */
@@ -20364,14 +20391,14 @@ print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
 	    break;
 	  }
 
-	printf (_("    OS: %s, ABI: %ld.%ld.%ld\n"), osname,
+	printf (_("    OS: %s, ABI: %d.%d.%d\n"), osname,
 		major, minor, subminor);
       }
       break;
 
     case NT_GNU_GOLD_VERSION:
       {
-	unsigned long i;
+	size_t i;
 
 	printf (_("    Version: "));
 	for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
@@ -20382,7 +20409,7 @@ print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
 
     case NT_GNU_HWCAP:
       {
-	unsigned long num_entries, mask;
+	unsigned int num_entries, mask;
 
 	/* Hardware capabilities information.  Word 0 is the number of entries.
 	   Word 1 is a bitmask of enabled entries.  The rest of the descriptor
@@ -20397,7 +20424,7 @@ print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
 	  }
 	num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
 	mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
-	printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
+	printf (_("num entries: %d, enabled mask: %x\n"), num_entries, mask);
 	/* FIXME: Add code to display the entries... */
       }
       break;
@@ -20411,7 +20438,7 @@ print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
 	 created by get_gnu_elf_note_type(), so all that we need to do is to
 	 display the data.  */
       {
-	unsigned long i;
+	size_t i;
 
 	printf (_("    Description data: "));
 	for (i = 0; i < pnote->descsz; ++i)
@@ -20713,7 +20740,7 @@ static bool
 print_stapsdt_note (Elf_Internal_Note *pnote)
 {
   size_t len, maxlen;
-  unsigned long addr_size = is_32bit_elf ? 4 : 8;
+  size_t addr_size = is_32bit_elf ? 4 : 8;
   char *data = pnote->descdata;
   char *data_end = pnote->descdata + pnote->descsz;
   uint64_t pc, base_addr, semaphore;
@@ -20841,9 +20868,9 @@ get_ia64_vms_note_type (unsigned e_type)
 static bool
 print_ia64_vms_note (Elf_Internal_Note * pnote)
 {
-  int maxlen = pnote->descsz;
+  unsigned int maxlen = pnote->descsz;
 
-  if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
+  if (maxlen < 2 || maxlen != pnote->descsz)
     goto desc_size_fail;
 
   switch (pnote->type)
@@ -20852,7 +20879,7 @@ print_ia64_vms_note (Elf_Internal_Note * pnote)
       if (maxlen <= 36)
 	goto desc_size_fail;
 
-      int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
+      size_t l = strnlen (pnote->descdata + 34, maxlen - 34);
 
       printf (_("    Creation date  : %.17s\n"), pnote->descdata);
       printf (_("    Last patch date: %.17s\n"), pnote->descdata + 17);
@@ -20953,9 +20980,9 @@ print_ia64_vms_note (Elf_Internal_Note * pnote)
 struct build_attr_cache {
   Filedata *filedata;
   char *strtab;
-  unsigned long strtablen;
+  uint64_t strtablen;
   Elf_Internal_Sym *symtab;
-  unsigned long nsyms;
+  uint64_t nsyms;
 } ba_cache;
 
 /* Find the symbol associated with a build attribute that is attached
@@ -20965,7 +20992,7 @@ struct build_attr_cache {
 
 static Elf_Internal_Sym *
 get_symbol_for_build_attribute (Filedata *filedata,
-				unsigned long offset,
+				uint64_t offset,
 				bool is_open_attr,
 				const char **pname)
 {
@@ -21079,7 +21106,7 @@ get_symbol_for_build_attribute (Filedata *filedata,
 /* Returns true iff addr1 and addr2 are in the same section.  */
 
 static bool
-same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
+same_section (Filedata * filedata, uint64_t addr1, uint64_t addr2)
 {
   Elf_Internal_Shdr * a1;
   Elf_Internal_Shdr * a2;
@@ -21094,15 +21121,15 @@ static bool
 print_gnu_build_attribute_description (Elf_Internal_Note *  pnote,
 				       Filedata *           filedata)
 {
-  static unsigned long global_offset = 0;
-  static unsigned long global_end = 0;
-  static unsigned long func_offset = 0;
-  static unsigned long func_end = 0;
+  static uint64_t global_offset = 0;
+  static uint64_t global_end = 0;
+  static uint64_t func_offset = 0;
+  static uint64_t func_end = 0;
 
   Elf_Internal_Sym *sym;
   const char *name;
-  unsigned long start;
-  unsigned long end;
+  uint64_t start;
+  uint64_t end;
   bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
 
   switch (pnote->descsz)
@@ -21113,17 +21140,20 @@ print_gnu_build_attribute_description (Elf_Internal_Note *  pnote,
       if (is_open_attr)
 	{
 	  if (global_end > global_offset)
-	    printf (_("    Applies to region from %#lx to %#lx\n"),
-		    global_offset, global_end);
+	    printf (_("    Applies to region from %#" PRIx64
+		      " to %#" PRIx64 "\n"), global_offset, global_end);
 	  else
-	    printf (_("    Applies to region from %#lx\n"), global_offset);
+	    printf (_("    Applies to region from %#" PRIx64
+		      "\n"), global_offset);
 	}
       else
 	{
 	  if (func_end > func_offset)
-	    printf (_("    Applies to region from %#lx to %#lx\n"), func_offset, func_end);
+	    printf (_("    Applies to region from %#" PRIx64
+		      " to %#" PRIx64 "\n"), func_offset, func_end);
 	  else
-	    printf (_("    Applies to region from %#lx\n"), func_offset);
+	    printf (_("    Applies to region from %#" PRIx64
+		      "\n"), func_offset);
 	}
       return true;
 
@@ -21170,26 +21200,27 @@ print_gnu_build_attribute_description (Elf_Internal_Note *  pnote,
 	     increasing address, but we should find the all of the notes
 	     for one section in the same place.  */
 	  && same_section (filedata, start, global_end))
-	warn (_("Gap in build notes detected from %#lx to %#lx\n"),
+	warn (_("Gap in build notes detected from %#" PRIx64
+		" to %#" PRIx64 "\n"),
 	      global_end + 1, start - 1);
 
-      printf (_("    Applies to region from %#lx"), start);
+      printf (_("    Applies to region from %#" PRIx64), start);
       global_offset = start;
 
       if (end)
 	{
-	  printf (_(" to %#lx"), end);
+	  printf (_(" to %#" PRIx64), end);
 	  global_end = end;
 	}
     }
   else
     {
-      printf (_("    Applies to region from %#lx"), start);
+      printf (_("    Applies to region from %#" PRIx64), start);
       func_offset = start;
 
       if (end)
 	{
-	  printf (_(" to %#lx"), end);
+	  printf (_(" to %#" PRIx64), end);
 	  func_end = end;
 	}
     }
@@ -21331,11 +21362,11 @@ print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
   if (strchr (expected_types, name_type) == NULL)
     warn (_("attribute does not have an expected type (%c)\n"), name_type);
 
-  if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
+  if ((size_t) (name - pnote->namedata) > pnote->namesz)
     {
-      error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
-	     (unsigned long) pnote->namesz,
-	     (long) (name - pnote->namedata));
+      error (_("corrupt name field: namesz: %lu but parsing gets to %td\n"),
+	     pnote->namesz,
+	     name - pnote->namedata);
       return false;
     }
 
@@ -21346,10 +21377,10 @@ print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
     {
     case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
       {
-	unsigned int        bytes;
-	unsigned long long  val = 0;
-	unsigned int        shift = 0;
-	char *              decoded = NULL;
+	unsigned int bytes;
+	uint64_t val = 0;
+	unsigned int shift = 0;
+	char *decoded = NULL;
 
 	bytes = pnote->namesz - (name - pnote->namedata);
 	if (bytes > 0)
@@ -21369,7 +21400,7 @@ print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
 
 	while (bytes --)
 	  {
-	    unsigned long long byte = *name++ & 0xff;
+	    uint64_t byte = *name++ & 0xff;
 
 	    val |= byte << shift;
 	    shift += 8;
@@ -21417,9 +21448,9 @@ print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
 	else
 	  {
 	    if (do_wide)
-	      left -= printf ("0x%llx", val);
+	      left -= printf ("0x%" PRIx64, val);
 	    else
-	      left -= printf ("0x%-.*llx", left, val);
+	      left -= printf ("0x%-.*" PRIx64, left, val);
 	  }
       }
       break;
@@ -21447,7 +21478,7 @@ print_note_contents_hex (Elf_Internal_Note *pnote)
 {
   if (pnote->descsz)
     {
-      unsigned long i;
+      size_t i;
 
       printf (_("   description data: "));
       for (i = 0; i < pnote->descsz; i++)
@@ -21759,8 +21790,9 @@ process_notes_at (Filedata *           filedata,
   if (section)
     printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
   else
-    printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
-	    (unsigned long) offset, (unsigned long) length);
+    printf (_("Displaying notes found at file offset 0x%08" PRIx64
+	      " with length 0x%08" PRIx64 ":\n"),
+	    offset, length);
 
   /* NB: Some note sections may have alignment value of 0 or 1.  gABI
      specifies that notes should be aligned to 4 bytes in 32-bit
@@ -21771,8 +21803,8 @@ process_notes_at (Filedata *           filedata,
     align = 4;
   else if (align != 4 && align != 8)
     {
-      warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
-	    (long) align);
+      warn (_("Corrupt note: alignment %" PRId64 ", expecting 4 or 8\n"),
+	    align);
       free (pnotes);
       return false;
     }
@@ -21795,12 +21827,12 @@ process_notes_at (Filedata *           filedata,
 	  min_notesz = offsetof (Elf_External_Note, name);
 	  if (data_remaining < min_notesz)
 	    {
-	      warn (ngettext ("Corrupt note: only %ld byte remains, "
+	      warn (ngettext ("Corrupt note: only %zd byte remains, "
 			      "not enough for a full note\n",
-			      "Corrupt note: only %ld bytes remain, "
+			      "Corrupt note: only %zd bytes remain, "
 			      "not enough for a full note\n",
 			      data_remaining),
-		    (long) data_remaining);
+		    data_remaining);
 	      break;
 	    }
 	  data_remaining -= min_notesz;
@@ -21824,12 +21856,12 @@ process_notes_at (Filedata *           filedata,
 	  min_notesz = offsetof (Elf64_External_VMS_Note, name);
 	  if (data_remaining < min_notesz)
 	    {
-	      warn (ngettext ("Corrupt note: only %ld byte remains, "
+	      warn (ngettext ("Corrupt note: only %zd byte remains, "
 			      "not enough for a full note\n",
-			      "Corrupt note: only %ld bytes remain, "
+			      "Corrupt note: only %zd bytes remain, "
 			      "not enough for a full note\n",
 			      data_remaining),
-		    (long) data_remaining);
+		    data_remaining);
 	      break;
 	    }
 	  data_remaining -= min_notesz;
@@ -21852,9 +21884,9 @@ process_notes_at (Filedata *           filedata,
 	  || ((size_t) (next - inote.descdata)
 	      > data_remaining - (size_t) (inote.descdata - inote.namedata)))
 	{
-	  warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
-		(unsigned long) ((char *) external - (char *) pnotes));
-	  warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
+	  warn (_("note with invalid namesz and/or descsz found at offset %#tx\n"),
+		(char *) external - (char *) pnotes);
+	  warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx, alignment: %u\n"),
 		inote.type, inote.namesz, inote.descsz, (int) align);
 	  break;
 	}
@@ -21937,8 +21969,9 @@ process_v850_notes (Filedata * filedata, uint64_t offset, uint64_t length)
   external = pnotes;
   end = (char*) pnotes + length;
 
-  printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
-	  (unsigned long) offset, (unsigned long) length);
+  printf (_("\nDisplaying contents of Renesas V850 notes section at offset"
+	    " %#" PRIx64 " with length %#" PRIx64 ":\n"),
+	  offset, length);
 
   while ((char *) external + sizeof (Elf_External_Note) < end)
     {
@@ -21964,9 +21997,9 @@ process_v850_notes (Filedata * filedata, uint64_t offset, uint64_t length)
       if (   ((char *) next > end)
 	  || ((char *) next <  (char *) pnotes))
 	{
-	  warn (_("corrupt descsz found in note at offset 0x%lx\n"),
-		(unsigned long) ((char *) external - (char *) pnotes));
-	  warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
+	  warn (_("corrupt descsz found in note at offset %#tx\n"),
+		(char *) external - (char *) pnotes);
+	  warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx\n"),
 		inote.type, inote.namesz, inote.descsz);
 	  break;
 	}
@@ -21977,9 +22010,9 @@ process_v850_notes (Filedata * filedata, uint64_t offset, uint64_t length)
       if (   inote.namedata + inote.namesz > end
 	  || inote.namedata + inote.namesz < inote.namedata)
         {
-          warn (_("corrupt namesz found in note at offset 0x%lx\n"),
-                (unsigned long) ((char *) external - (char *) pnotes));
-          warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
+          warn (_("corrupt namesz found in note at offset %#zx\n"),
+                (char *) external - (char *) pnotes);
+          warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx\n"),
                 inote.type, inote.namesz, inote.descsz);
           break;
         }
@@ -21989,7 +22022,7 @@ process_v850_notes (Filedata * filedata, uint64_t offset, uint64_t length)
       if (! print_v850_note (& inote))
 	{
 	  res = false;
-	  printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
+	  printf ("<corrupt sizes: namesz: %#lx, descsz: %#lx>\n",
 		  inote.namesz, inote.descsz);
 	}
     }
@@ -22003,7 +22036,7 @@ static bool
 process_note_sections (Filedata * filedata)
 {
   Elf_Internal_Shdr *section;
-  unsigned long i;
+  size_t i;
   unsigned int n = 0;
   bool res = true;
 
@@ -22572,12 +22605,12 @@ process_archive (Filedata * filedata, bool is_thin_archive)
 	       filedata->file_name);
       else
 	{
-	  unsigned long i, l;
-	  unsigned long current_pos;
+	  uint64_t i, l;
+	  uint64_t current_pos;
 
-	  printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
-		    "in the symbol table)\n"),
-		  filedata->file_name, (unsigned long) arch.index_num,
+	  printf (_("Index of archive %s: (%" PRIu64 " entries,"
+		    " %#" PRIx64 " bytes in the symbol table)\n"),
+		  filedata->file_name, arch.index_num,
 		  arch.sym_size);
 
 	  current_pos = ftell (filedata->handle);
@@ -22630,10 +22663,10 @@ process_archive (Filedata * filedata, bool is_thin_archive)
 
 	  if (l < arch.sym_size)
 	    {
-	      error (ngettext ("%s: %ld byte remains in the symbol table, "
+	      error (ngettext ("%s: %" PRId64 " byte remains in the symbol table, "
 			       "but without corresponding entries in "
 			       "the index table\n",
-			       "%s: %ld bytes remain in the symbol table, "
+			       "%s: %" PRId64 " bytes remain in the symbol table, "
 			       "but without corresponding entries in "
 			       "the index table\n",
 			       arch.sym_size - l),

-- 
Alan Modra
Australia Development Lab, IBM

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

end of thread, other threads:[~2022-11-22 21:59 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-14 15:03 [PATCH] readelf: use fseeko for elf files >= 2 GiB on x86_64-mingw32 Brett Werling
2022-11-14 15:30 ` Jan Beulich
2022-11-14 15:52   ` Brett Werling
2022-11-14 21:42     ` Alan Modra
2022-11-16 10:09       ` Mike Frysinger
2022-11-16 10:46         ` Jan Beulich
2022-11-16 14:01           ` Mike Frysinger
2022-11-16 14:44             ` Michael Matz
2022-11-16 15:40               ` Mike Frysinger
2022-11-16 15:58                 ` Jose E. Marchesi
2022-11-16 16:13                 ` Michael Matz
2022-11-16 17:09                   ` Mike Frysinger
2022-11-16 21:19                     ` Brett Werling
2022-11-17  8:02                       ` Mike Frysinger
2022-11-17 13:21                     ` Michael Matz
2022-11-15 14:57 ` [PATCH] readelf: use fseeko64 or fseeko if possible Brett Werling
2022-11-17  7:02   ` Alan Modra
2022-11-17 14:09     ` Brett Werling
2022-11-17 14:34 ` Brett Werling
2022-11-21 21:52   ` Alan Modra
2022-11-22 13:46     ` Michael Matz
2022-11-22 21:55       ` Alan Modra
2022-11-22 21:57   ` Alan Modra
2022-11-22 21:59     ` Don't use "long" in readelf for file offsets Alan Modra

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).