public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Subject: Comment bfd_get_section_limit_octets and bfd_get_section_alloc_size
Date: Sun, 18 Dec 2022 20:42:12 +1030	[thread overview]
Message-ID: <Y57nfEDqt/784poP@squeak.grove.modra.org> (raw)

	* bfd.c (bfd_get_section_limit_octets): Add comment.
	(bfd_get_section_alloc_size): Likewise.
	* libbfd.c (_bfd_generic_get_section_contents): Use
	bfd_get_section_limit_octets.
	* section.c (bfd_get_section_contents): Likewise.
	* bfd-in2.h: Regenerate.

diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
index f4d531f5bf8..053eccf2837 100644
--- a/bfd/bfd-in2.h
+++ b/bfd/bfd-in2.h
@@ -6946,6 +6946,8 @@ bfd_set_asymbol_name (asymbol *sy, const char *name)
   sy->name = name;
 }
 
+/* For input sections return the original size on disk of the
+   section.  For output sections return the current size.  */
 static inline bfd_size_type
 bfd_get_section_limit_octets (const bfd *abfd, const asection *sec)
 {
@@ -6962,6 +6964,9 @@ bfd_get_section_limit (const bfd *abfd, const asection *sec)
           / bfd_octets_per_byte (abfd, sec));
 }
 
+/* For input sections return the larger of the current size and the
+   original size on disk of the section.  For output sections return
+   the current size.  */
 static inline bfd_size_type
 bfd_get_section_alloc_size (const bfd *abfd, const asection *sec)
 {
diff --git a/bfd/bfd.c b/bfd/bfd.c
index 97ce2b10450..e1759ed670e 100644
--- a/bfd/bfd.c
+++ b/bfd/bfd.c
@@ -502,6 +502,8 @@ CODE_FRAGMENT
 .  sy->name = name;
 .}
 .
+.{* For input sections return the original size on disk of the
+.   section.  For output sections return the current size.  *}
 .static inline bfd_size_type
 .bfd_get_section_limit_octets (const bfd *abfd, const asection *sec)
 .{
@@ -518,6 +520,9 @@ CODE_FRAGMENT
 .	   / bfd_octets_per_byte (abfd, sec));
 .}
 .
+.{* For input sections return the larger of the current size and the
+.   original size on disk of the section.  For output sections return
+.   the current size.  *}
 .static inline bfd_size_type
 .bfd_get_section_alloc_size (const bfd *abfd, const asection *sec)
 .{
diff --git a/bfd/libbfd.c b/bfd/libbfd.c
index d33f3416206..0026c377862 100644
--- a/bfd/libbfd.c
+++ b/bfd/libbfd.c
@@ -943,15 +943,7 @@ _bfd_generic_get_section_contents (bfd *abfd,
       return false;
     }
 
-  /* We do allow reading of a section after bfd_final_link has
-     written the contents out to disk.  In that situation, rawsize is
-     just a stale version of size, so ignore it.  Otherwise we must be
-     reading an input section, where rawsize, if different to size,
-     is the on-disk size.  */
-  if (abfd->direction != write_direction && section->rawsize != 0)
-    sz = section->rawsize;
-  else
-    sz = section->size;
+  sz = bfd_get_section_limit_octets (abfd, section);
   if (offset + count < count
       || offset + count > sz
       || (abfd->my_archive != NULL
diff --git a/bfd/section.c b/bfd/section.c
index a49778eb333..893fc91b3df 100644
--- a/bfd/section.c
+++ b/bfd/section.c
@@ -1550,10 +1550,7 @@ bfd_get_section_contents (bfd *abfd,
       return true;
     }
 
-  if (abfd->direction != write_direction && section->rawsize != 0)
-    sz = section->rawsize;
-  else
-    sz = section->size;
+  sz = bfd_get_section_limit_octets (abfd, section);
   if ((bfd_size_type) offset > sz
       || count > sz - offset
       || count != (size_t) count)

-- 
Alan Modra
Australia Development Lab, IBM

                 reply	other threads:[~2022-12-18 10:12 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y57nfEDqt/784poP@squeak.grove.modra.org \
    --to=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).