From be7d229ad43c7bb1b75693b87790504894220b55 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Fri, 12 Aug 2022 17:38:08 +0930 Subject: [PATCH] Don't use bfd_size_type in readelf.c and dwarf.c Replacing bfd_size_type with dwarf_size_type or uint64_t is mostly cosmetic. The point of the change is to avoid use of a BFD type in readelf, where we'd like to keep as independent of BFD as possible. Also, the patch is a step towards using standard types. --- binutils/dwarf.c | 58 ++++++++++---------- binutils/readelf.c | 129 +++++++++++++++++++++------------------------ 2 files changed, 89 insertions(+), 98 deletions(-) diff --git a/binutils/dwarf.c b/binutils/dwarf.c index d862e16388b..fdbe1280ac6 100644 --- a/binutils/dwarf.c +++ b/binutils/dwarf.c @@ -5841,7 +5841,7 @@ display_debug_pubnames_worker (struct dwarf_section *section, while (1) { - bfd_size_type maxprint; + size_t maxprint; dwarf_vma offset; SAFE_BYTE_GET_AND_INC (offset, data, offset_size, start); @@ -8887,12 +8887,12 @@ frame_display_row (Frame_Chunk *fc, int *need_col_headers, unsigned int *max_reg static unsigned char * read_cie (unsigned char *start, unsigned char *end, Frame_Chunk **p_cie, int *p_version, - bfd_size_type *p_aug_len, unsigned char **p_aug) + dwarf_size_type *p_aug_len, unsigned char **p_aug) { int version; Frame_Chunk *fc; unsigned char *augmentation_data = NULL; - bfd_size_type augmentation_data_len = 0; + dwarf_size_type augmentation_data_len = 0; * p_cie = NULL; /* PR 17512: file: 001-228113-0.004. */ @@ -8975,7 +8975,7 @@ read_cie (unsigned char *start, unsigned char *end, READ_ULEB (augmentation_data_len, start, end); augmentation_data = start; /* PR 17512: file: 11042-2589-0.004. */ - if (augmentation_data_len > (bfd_size_type) (end - start)) + if (augmentation_data_len > (size_t) (end - start)) { warn (_("Augmentation data too long: 0x%s, expected at most %#lx\n"), dwarf_vmatoa ("x", augmentation_data_len), @@ -9038,9 +9038,7 @@ read_cie (unsigned char *start, unsigned char *end, output line. */ static void -display_data (bfd_size_type printed, - const unsigned char * data, - const bfd_size_type len) +display_data (size_t printed, const unsigned char *data, size_t len) { if (do_wide || len < ((80 - printed) / 3)) for (printed = 0; printed < len; ++printed) @@ -9060,9 +9058,9 @@ display_data (bfd_size_type printed, If do_wide is not enabled, then formats the output to fit into 80 columns. */ static void -display_augmentation_data (const unsigned char * data, const bfd_size_type len) +display_augmentation_data (const unsigned char * data, dwarf_size_type len) { - bfd_size_type i; + size_t i; i = printf (_(" Augmentation data: ")); display_data (i, data, len); @@ -9095,7 +9093,7 @@ display_debug_frames (struct dwarf_section *section, Frame_Chunk *cie; int need_col_headers = 1; unsigned char *augmentation_data = NULL; - bfd_size_type augmentation_data_len = 0; + dwarf_size_type augmentation_data_len = 0; unsigned int encoded_ptr_size = saved_eh_addr_size; unsigned int offset_size; bool all_nops; @@ -9348,7 +9346,7 @@ display_debug_frames (struct dwarf_section *section, READ_ULEB (augmentation_data_len, start, block_end); augmentation_data = start; /* PR 17512 file: 722-8446-0.004 and PR 22386. */ - if (augmentation_data_len > (bfd_size_type) (block_end - start)) + if (augmentation_data_len > (size_t) (block_end - start)) { warn (_("Augmentation data too long: 0x%s, " "expected at most %#lx\n"), @@ -10498,18 +10496,18 @@ display_debug_links (struct dwarf_section * section, } else /* startswith (section->name, ".gnu_debugaltlink") */ { - const unsigned char * build_id = section->start + filelen + 1; - bfd_size_type build_id_len = section->size - (filelen + 1); - bfd_size_type printed; + const unsigned char *build_id = section->start + filelen + 1; + size_t build_id_len = section->size - (filelen + 1); + size_t printed; /* FIXME: Should we support smaller build-id notes ? */ if (build_id_len < 0x14) { - warn (_("Build-ID is too short (%#lx bytes)\n"), (long) build_id_len); + warn (_("Build-ID is too short (%#zx bytes)\n"), build_id_len); return 0; } - printed = printf (_(" Build-ID (%#lx bytes):"), (long) build_id_len); + printed = printf (_(" Build-ID (%#zx bytes):"), build_id_len); display_data (printed, build_id, build_id_len); putchar ('\n'); } @@ -11236,9 +11234,9 @@ xcalloc2 (size_t nmemb, size_t size) } static unsigned long -calc_gnu_debuglink_crc32 (unsigned long crc, - const unsigned char * buf, - bfd_size_type len) +calc_gnu_debuglink_crc32 (unsigned long crc, + const unsigned char *buf, + size_t len) { static const unsigned long crc32_table[256] = { @@ -11309,11 +11307,11 @@ typedef const char *(* parse_func_type) (struct dwarf_section *, void *); static bool check_gnu_debuglink (const char * pathname, void * crc_pointer) { - static unsigned char buffer [8 * 1024]; - FILE * f; - bfd_size_type count; - unsigned long crc = 0; - void * sep_data; + static unsigned char buffer[8 * 1024]; + FILE *f; + size_t count; + unsigned long crc = 0; + void *sep_data; sep_data = open_debug_file (pathname); if (sep_data == NULL) @@ -11383,17 +11381,17 @@ check_gnu_debugaltlink (const char * filename, void * data ATTRIBUTE_UNUSED) typedef struct build_id_data { - bfd_size_type len; - const unsigned char * data; + size_t len; + const unsigned char *data; } Build_id_data; static const char * parse_gnu_debugaltlink (struct dwarf_section * section, void * data) { - const char * name; - bfd_size_type namelen; - bfd_size_type id_len; - Build_id_data * build_id_data; + const char *name; + size_t namelen; + size_t id_len; + Build_id_data *build_id_data; /* The name is first. The build-id follows immediately, with no padding, up to the section's end. */ diff --git a/binutils/readelf.c b/binutils/readelf.c index af443aa6a97..577a6a155f6 100644 --- a/binutils/readelf.c +++ b/binutils/readelf.c @@ -268,7 +268,7 @@ typedef struct filedata const char * file_name; bool is_separate; FILE * handle; - bfd_size_type file_size; + uint64_t file_size; Elf_Internal_Ehdr file_header; unsigned long archive_file_offset; unsigned long archive_file_size; @@ -278,7 +278,7 @@ typedef struct filedata char * string_table; unsigned long string_table_length; unsigned long dynamic_addr; - bfd_size_type dynamic_size; + uint64_t dynamic_size; size_t dynamic_nent; Elf_Internal_Dyn * dynamic_section; Elf_Internal_Shdr * dynamic_strtab_section; @@ -291,12 +291,12 @@ typedef struct filedata unsigned int dynamic_syminfo_nent; Elf_Internal_Syminfo * dynamic_syminfo; unsigned long dynamic_syminfo_offset; - bfd_size_type nbuckets; - bfd_size_type nchains; + uint64_t nbuckets; + uint64_t nchains; bfd_vma * buckets; bfd_vma * chains; - bfd_size_type ngnubuckets; - bfd_size_type ngnuchains; + uint64_t ngnubuckets; + uint64_t ngnuchains; bfd_vma * gnubuckets; bfd_vma * gnuchains; bfd_vma * mipsxlat; @@ -439,40 +439,32 @@ get_dynamic_name (const Filedata *filedata, size_t offset) message using REASON as part of the context. */ static void * -get_data (void * var, - Filedata * filedata, - unsigned long offset, - bfd_size_type size, - bfd_size_type nmemb, - const char * reason) +get_data (void *var, + Filedata *filedata, + unsigned long offset, + uint64_t size, + uint64_t nmemb, + const char *reason) { void * mvar; - bfd_size_type amt = size * nmemb; + uint64_t amt = size * nmemb; if (size == 0 || nmemb == 0) return NULL; - /* If the size_t type is smaller than the bfd_size_type, eg because - you are building a 32-bit tool on a 64-bit host, then make sure - that when the sizes are cast to (size_t) no information is lost. */ + /* If size_t is smaller than uint64_t, eg because you are building + on a 32-bit host, then make sure that when the sizes are cast to + size_t no information is lost. */ if ((size_t) size != size || (size_t) nmemb != nmemb - || (size_t) amt != amt) - { - if (reason) - error (_("Size truncation prevents reading %" PRIu64 - " elements of size %" PRIu64 " for %s\n"), - (uint64_t) nmemb, (uint64_t) size, reason); - return NULL; - } - - /* Check for size overflow. */ - if (amt / size != nmemb || (size_t) amt + 1 == 0) + || (size_t) amt != amt + || amt / size != nmemb + || (size_t) amt + 1 == 0) { if (reason) error (_("Size overflow prevents reading %" PRIu64 " elements of size %" PRIu64 " for %s\n"), - (uint64_t) nmemb, (uint64_t) size, reason); + nmemb, size, reason); return NULL; } @@ -484,7 +476,7 @@ get_data (void * var, { if (reason) error (_("Reading %" PRIu64 " bytes extends past end of file for %s\n"), - (uint64_t) amt, reason); + amt, reason); return NULL; } @@ -507,7 +499,7 @@ get_data (void * var, { if (reason) error (_("Out of memory allocating %" PRIu64 " bytes for %s\n"), - (uint64_t) amt, reason); + amt, reason); return NULL; } @@ -518,7 +510,7 @@ get_data (void * var, { if (reason) error (_("Unable to read in %" PRIu64 " bytes of %s\n"), - (uint64_t) amt, reason); + amt, reason); if (mvar != var) free (mvar); return NULL; @@ -2615,7 +2607,7 @@ static void locate_dynamic_section (Filedata *filedata) { unsigned long dynamic_addr = 0; - bfd_size_type dynamic_size = 0; + uint64_t dynamic_size = 0; if (filedata->file_header.e_phnum != 0 && get_program_headers (filedata)) @@ -6045,7 +6037,7 @@ process_program_headers (Filedata * filedata) } unsigned long dynamic_addr = 0; - bfd_size_type dynamic_size = 0; + uint64_t dynamic_size = 0; for (i = 0, segment = filedata->program_headers; i < filedata->file_header.e_phnum; i++, segment++) @@ -6293,7 +6285,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 -offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size) +offset_from_vma (Filedata * filedata, bfd_vma vma, uint64_t size) { Elf_Internal_Phdr * seg; @@ -7028,7 +7020,8 @@ get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags) } static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT -get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size) +get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, + uint64_t size) { if (is_32bit_elf) { @@ -7169,13 +7162,13 @@ process_section_headers (Filedata * filedata) #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \ do \ { \ - bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64; \ + uint64_t expected_entsize = is_32bit_elf ? size32 : size64; \ if (section->sh_entsize != expected_entsize) \ { \ error (_("Section %d has invalid sh_entsize of %" PRIx64 "\n"), \ i, (uint64_t) section->sh_entsize); \ error (_("(Using the expected size of %" PRIx64 " for the rest of this dump)\n"), \ - (uint64_t) expected_entsize); \ + expected_entsize); \ section->sh_entsize = expected_entsize; \ } \ } \ @@ -10920,20 +10913,20 @@ print_dynamic_flags (bfd_vma flags) } static bfd_vma * -get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size) +get_dynamic_data (Filedata * filedata, uint64_t number, unsigned int ent_size) { unsigned char * e_data; bfd_vma * i_data; - /* If the size_t type is smaller than the bfd_size_type, eg because - you are building a 32-bit tool on a 64-bit host, then make sure - that when (number) is cast to (size_t) no information is lost. */ - if (sizeof (size_t) < sizeof (bfd_size_type) - && (bfd_size_type) ((size_t) number) != number) + /* If size_t is smaller than uint64_t, eg because you are building + on a 32-bit host, then make sure that when number is cast to + size_t no information is lost. */ + if ((size_t) number != number + || ent_size * number / ent_size != number) { - error (_("Size truncation prevents reading %" PRIu64 + error (_("Size overflow prevents reading %" PRIu64 " elements of size %u\n"), - (uint64_t) number, ent_size); + number, ent_size); return NULL; } @@ -10942,7 +10935,7 @@ get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_si if (ent_size * number > filedata->file_size) { error (_("Invalid number of dynamic entries: %" PRIu64 "\n"), - (uint64_t) number); + number); return NULL; } @@ -10950,14 +10943,14 @@ get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_si if (e_data == NULL) { error (_("Out of memory reading %" PRIu64 " dynamic entries\n"), - (uint64_t) number); + number); return NULL; } if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number) { error (_("Unable to read in %" PRIu64 " bytes of dynamic data\n"), - (uint64_t) number * ent_size); + number * ent_size); free (e_data); return NULL; } @@ -10966,7 +10959,7 @@ get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_si if (i_data == NULL) { error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"), - (uint64_t) number); + number); free (e_data); return NULL; } @@ -11348,7 +11341,7 @@ the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n")); && filedata->dynamic_info[DT_STRSZ]) { unsigned long offset; - bfd_size_type str_tab_len = filedata->dynamic_info[DT_STRSZ]; + uint64_t str_tab_len = filedata->dynamic_info[DT_STRSZ]; offset = offset_from_vma (filedata, filedata->dynamic_info[DT_STRTAB], @@ -14872,12 +14865,12 @@ reloc_at (struct dwarf_section * dsec, dwarf_vma offset) more relocations ? */ static bool -apply_relocations (Filedata * filedata, - const Elf_Internal_Shdr * section, - unsigned char * start, - bfd_size_type size, - void ** relocs_return, - unsigned long * num_relocs_return) +apply_relocations (Filedata *filedata, + const Elf_Internal_Shdr *section, + unsigned char *start, + size_t size, + void **relocs_return, + unsigned long *num_relocs_return) { Elf_Internal_Shdr * relsec; unsigned char * end = start + size; @@ -15139,7 +15132,7 @@ disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata) static char * get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata) { - bfd_size_type num_bytes = section->sh_size; + uint64_t num_bytes = section->sh_size; if (num_bytes == 0 || section->sh_type == SHT_NOBITS) { @@ -15210,7 +15203,7 @@ static bool dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata) { Elf_Internal_Shdr *relsec; - bfd_size_type num_bytes; + uint64_t num_bytes; unsigned char *data; unsigned char *end; unsigned char *real_start; @@ -15425,13 +15418,13 @@ dump_section_as_bytes (Elf_Internal_Shdr *section, Filedata *filedata, bool relocate) { - Elf_Internal_Shdr * relsec; - bfd_size_type bytes; - bfd_size_type section_size; - bfd_vma addr; - unsigned char * data; - unsigned char * real_start; - unsigned char * start; + Elf_Internal_Shdr *relsec; + size_t bytes; + uint64_t section_size; + bfd_vma addr; + unsigned char *data; + unsigned char *real_start; + unsigned char *start; real_start = start = (unsigned char *) get_section_contents (section, filedata); if (start == NULL) @@ -16139,7 +16132,7 @@ display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * fileda const char *name = (section_name_valid (filedata, section) ? section_name (filedata, section) : ""); const char *print_name = printable_section_name (filedata, section); - bfd_size_type length; + uint64_t length; bool result = true; int i; @@ -22167,7 +22160,7 @@ open_file (const char * pathname, bool is_separate) if (filedata->handle == NULL) goto fail; - filedata->file_size = (bfd_size_type) statbuf.st_size; + filedata->file_size = statbuf.st_size; filedata->file_name = pathname; filedata->is_separate = is_separate; @@ -22727,7 +22720,7 @@ process_file (char * file_name) return false; } - filedata->file_size = (bfd_size_type) statbuf.st_size; + filedata->file_size = statbuf.st_size; filedata->is_separate = false; if (memcmp (armag, ARMAG, SARMAG) == 0) -- 2.30.2