Don't use "long" in readelf for file offsets

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".
This commit is contained in:
Alan Modra
2022-11-23 07:33:29 +10:30
parent 63cf857e24
commit 26c527e62e
5 changed files with 531 additions and 498 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -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;
}

View File

@ -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. */

File diff suppressed because it is too large Load Diff