public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: "H.J. Lu" <hjl.tools@gmail.com>
To: Alan Modra <amodra@gmail.com>
Cc: binutils@sourceware.org, goldstein.w.n@gmail.com
Subject: Re: [PATCH v4 4/6] elf: Add _bfd_elf_mmap_section and _bfd_elf_munmap_section_contents
Date: Fri, 8 Mar 2024 07:28:52 -0800	[thread overview]
Message-ID: <CAMe9rOpLFTYm4Pnvm3c_t0cNOyHgKEPoR5q0xgP691w2GKNj_A@mail.gmail.com> (raw)
In-Reply-To: <ZepepKHUCUVZgGY8@squeak.grove.modra.org>

On Thu, Mar 7, 2024 at 4:41 PM Alan Modra <amodra@gmail.com> wrote:
>
> On Wed, Mar 06, 2024 at 03:23:59PM -0800, H.J. Lu wrote:
> > +void
> > +_bfd_elf_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED,
> > +                               void *contents)
> > +{
> > +  /* NB: Since _bfd_elf_munmap_section_contents is called like free,
> > +     CONTENTS may be NULL.  */
> > +  if (contents == NULL)
> > +    return;
> > +
> > +#ifdef USE_MMAP
> > +  if (sec->mmapped_p)
> > +    {
> > +      /* NB: Don't free CONTENTS if it has been cached.  */
> > +      if (elf_section_data (sec)->this_hdr.contents == contents)
> > +     return;
>
> Why the above test?  This function must have such a test before it is
> called if the contents might be cached.  Hmm, unless you have some
> place where contents are cached only when mmapped?  (I didn't see such
> a case.)

When _bfd_elf_munmap_section_contents is called from
_bfd_elf_parse_eh_frame, the section contents may be cached.
I updated the comments to clarify it.

> > +      /* When bfd_mmap_local returns (void *) -1 on an unknown input,
> > +      CONTENTS is malloced and CONTENTS_ADDR is set to NULL.  */
>
> The above comment adds confusion.  As far as I can see there isn't a
> case where bfd_mmap_local returns -1 causing contents to be malloc'd.
> Delete the comment, or mention _bfd_elf_mmap_section behaviour here.

Changed to

     /* When _bfd_elf_mmap_section_contents returns CONTENTS as
         malloced, CONTENTS_ADDR is set to NULL.  */

> BTW, why does this function have _contents in the name while
> _bfd_elf_mmap_section does not?

Changed to _bfd_elf_mmap_section_contents.

> > +      if (elf_section_data (sec)->contents_addr != NULL)
> > +     {
> > +       /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
> > +       if (munmap (elf_section_data (sec)->contents_addr,
> > +                   elf_section_data (sec)->contents_size) != 0)
> > +         abort ();
> > +       sec->mmapped_p = 0;
> > +       sec->contents = NULL;
> > +       elf_section_data (sec)->contents_addr = NULL;
> > +       elf_section_data (sec)->contents_size = 0;
> > +       return;
> > +     }
> > +    }
> > +#endif
> > +
> > +  free (contents);
> > +}
>
> > diff --git a/bfd/libbfd.c b/bfd/libbfd.c
> > index 237f91e5c97..c847c4f0180 100644
> > --- a/bfd/libbfd.c
> > +++ b/bfd/libbfd.c
> > @@ -21,6 +21,7 @@
> >
> >  #include "sysdep.h"
> >  #include "bfd.h"
> > +#include "elf-bfd.h"
> >  #include "libbfd.h"
> >  #include "objalloc.h"
> >
> > @@ -1202,6 +1203,19 @@ _bfd_generic_get_section_contents (bfd *abfd,
> >        return false;
> >      }
> >
> > +#ifdef USE_MMAP
> > +  if (section->mmapped_p
> > +      && (section->contents != NULL || location != NULL))
> > +    {
> > +      _bfd_error_handler
> > +     /* xgettext:c-format */
> > +     (_("%pB: mapped section %pA has non-NULLL buffer"),
>
> typo

Fixed.

>
> > +      abfd, section);
> > +      bfd_set_error (bfd_error_invalid_operation);
> > +      return false;
> > +    }
> > +#endif
> > +
> >    sz = bfd_get_section_limit_octets (abfd, section);
> >    if (offset + count < count
> >        || offset + count > sz
> > @@ -1214,8 +1228,49 @@ _bfd_generic_get_section_contents (bfd *abfd,
> >        return false;
> >      }
> >
> > -  if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
> > -      || bfd_read (location, count, abfd) != count)
> > +  if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
> > +    return false;
> > +
> > +#ifdef USE_MMAP
> > +  if (section->mmapped_p)
> > +    {
> > +      if (location != 0
> > +       || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
> > +     abort ();
> > +
> > +      int prot = ((section->reloc_count == 0)
> > +               ? PROT_READ : PROT_READ | PROT_WRITE);
> > +
> > +      location = bfd_mmap_local
> > +     (abfd, count, prot, &elf_section_data (section)->contents_addr,
> > +      &elf_section_data (section)->contents_size);
> > +
> > +      if (location == NULL)
> > +     return false;
> > +
> > +      /* Check for the unknown input file.  */
>
> Not "the unknown input file" but rather "iovec not supporting mmap".

Fixed.

> > +      if (location != (void *) -1)
> > +     {
> > +       section->contents = location;
> > +       return true;
> > +     }
> > +
> > +      /* Malloc the buffer and call bfd_read.  */
> > +      location = (bfd_byte *) bfd_malloc (count);
> > +      if (location == NULL)
> > +     {
> > +       if (bfd_get_error () == bfd_error_no_memory)
> > +         _bfd_error_handler
> > +           /* xgettext:c-format */
> > +           (_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
> > +            abfd, section, (uint64_t) count);
> > +       return false;
> > +     }
> > +      section->contents = location;
> > +    }
> > +#endif
> > +
> > +  if (bfd_read (location, count, abfd) != count)
> >      return false;
> >
> >    return true;
>
> OK with those things fixed.
>
> --
> Alan Modra
> Australia Development Lab, IBM

Thanks.

-- 
H.J.

  reply	other threads:[~2024-03-08 15:29 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-06 23:23 [PATCH v4 0/6] elf: Use mmap to map in section contents H.J. Lu
2024-03-06 23:23 ` [PATCH v4 1/6] bfd: Don't hard-code BFD_JUMP_TABLE_COPY H.J. Lu
2024-03-07 22:46   ` Alan Modra
2024-03-06 23:23 ` [PATCH v4 2/6] bfd: Change the --with-mmap default to true H.J. Lu
2024-03-07 22:46   ` Alan Modra
2024-03-06 23:23 ` [PATCH v4 3/6] elf: Use mmap to map in read-only sections H.J. Lu
2024-03-08  0:40   ` Alan Modra
2024-03-08 15:25     ` H.J. Lu
2024-03-06 23:23 ` [PATCH v4 4/6] elf: Add _bfd_elf_mmap_section and _bfd_elf_munmap_section_contents H.J. Lu
2024-03-08  0:41   ` Alan Modra
2024-03-08 15:28     ` H.J. Lu [this message]
2024-03-08 17:03       ` H.J. Lu
2024-03-08  0:50   ` Sam James
2024-03-08  3:33     ` Alan Modra
2024-03-06 23:24 ` [PATCH v4 5/6] elf: Use mmap to map in symbol and relocation tables H.J. Lu
2024-03-08  0:42   ` Alan Modra
2024-03-08 15:36     ` H.J. Lu
2024-03-09 16:34       ` H.J. Lu
2024-03-06 23:24 ` [PATCH v4 6/6] elf: Don't cache symbol nor relocation tables with mmap H.J. Lu
2024-03-08  0:43   ` Alan Modra
2024-03-08  0:48   ` Sam James
2024-03-08 15:31     ` H.J. Lu

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=CAMe9rOpLFTYm4Pnvm3c_t0cNOyHgKEPoR5q0xgP691w2GKNj_A@mail.gmail.com \
    --to=hjl.tools@gmail.com \
    --cc=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=goldstein.w.n@gmail.com \
    /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).