[PATCH] Cache the BFD file size for readonly input
H.J. Lu
hjl.tools@gmail.com
Tue Jun 27 12:27:00 GMT 2017
bfd_get_file_size may be called many times on the same input file with
many sections. Cache the file size to avoid calling stat repeatedly
for non-archive members.
OK for master?
H.J.
---
PR ld/21679
* bfd.c (bfd): Add file_size_set and file_size.
* bfd-in2.h: Regenerated.
* bfdio.c (bfd_get_file_size): Cache the file size for readonly
input.
---
bfd/bfd-in2.h | 7 +++++++
bfd/bfd.c | 7 +++++++
bfd/bfdio.c | 14 +++++++++++++-
3 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
index 8c312e3..2141bcb 100644
--- a/bfd/bfd-in2.h
+++ b/bfd/bfd-in2.h
@@ -6866,6 +6866,10 @@ struct bfd
getting it from the file each time. */
unsigned int mtime_set : 1;
+ /* Set if we have a cached file size, rather than getting it from the
+ file each time. */
+ unsigned int file_size_set : 1;
+
/* Flag set if symbols from this BFD should not be exported. */
unsigned int no_export : 1;
@@ -6927,6 +6931,9 @@ struct bfd
be used only for archive elements. */
int archive_pass;
+ /* The file size. */
+ ufile_ptr file_size;
+
/* Stuff only useful for object files:
The start address. */
bfd_vma start_address;
diff --git a/bfd/bfd.c b/bfd/bfd.c
index c6fce45..946d0bc 100644
--- a/bfd/bfd.c
+++ b/bfd/bfd.c
@@ -206,6 +206,10 @@ CODE_FRAGMENT
. getting it from the file each time. *}
. unsigned int mtime_set : 1;
.
+. {* Set if we have a cached file size, rather than getting it from the
+. file each time. *}
+. unsigned int file_size_set : 1;
+.
. {* Flag set if symbols from this BFD should not be exported. *}
. unsigned int no_export : 1;
.
@@ -267,6 +271,9 @@ CODE_FRAGMENT
. be used only for archive elements. *}
. int archive_pass;
.
+. {* The file size. *}
+. ufile_ptr file_size;
+.
. {* Stuff only useful for object files:
. The start address. *}
. bfd_vma start_address;
diff --git a/bfd/bfdio.c b/bfd/bfdio.c
index e301570..51203e5 100644
--- a/bfd/bfdio.c
+++ b/bfd/bfdio.c
@@ -451,11 +451,23 @@ DESCRIPTION
file_ptr
bfd_get_file_size (bfd *abfd)
{
+ ufile_ptr file_size;
+
+ if (abfd->file_size_set)
+ return abfd->file_size;
+
if (abfd->my_archive != NULL
&& !bfd_is_thin_archive (abfd->my_archive))
return arelt_size (abfd);
- return bfd_get_size (abfd);
+ file_size = bfd_get_size (abfd);
+ if (abfd->direction == read_direction)
+ {
+ abfd->file_size = file_size;
+ abfd->file_size_set = TRUE;
+ }
+
+ return file_size;
}
/*
--
2.9.4
More information about the Binutils
mailing list