Include ld-lib.exp from ctf-lib.exp
[binutils-gdb.git] / libctf / ctf-open-bfd.c
index 9fcce2fa9cdc7d98c4f2f8c312c2080c8de9e6fb..84e4af6a73a94e514291d46f97f7a7eb4b6d2c7d 100644 (file)
@@ -1,5 +1,5 @@
 /* Opening CTF files with BFD.
-   Copyright (C) 2019-2020 Free Software Foundation, Inc.
+   Copyright (C) 2019-2021 Free Software Foundation, Inc.
 
    This file is part of libctf.
 
@@ -40,7 +40,8 @@ ctf_bfdclose (struct ctf_archive_internal *arci)
 {
   if (arci->ctfi_abfd != NULL)
     if (!bfd_close_all_done (arci->ctfi_abfd))
-      ctf_dprintf ("Cannot close BFD: %s\n", bfd_errmsg (bfd_get_error()));
+      ctf_err_warn (NULL, 0, 0, _("cannot close BFD: %s"),
+                   bfd_errmsg (bfd_get_error ()));
 }
 
 /* Open a CTF file given the specified BFD.  */
@@ -62,8 +63,9 @@ ctf_bfdopen (struct bfd *abfd, int *errp)
 
   if (!bfd_malloc_and_get_section (abfd, ctf_asect, &contents))
     {
-      ctf_dprintf ("ctf_bfdopen(): cannot malloc CTF section: %s\n",
-                  bfd_errmsg (bfd_get_error()));
+      ctf_err_warn (NULL, 0, 0, _("ctf_bfdopen(): cannot malloc "
+                                 "CTF section: %s"),
+                   bfd_errmsg (bfd_get_error ()));
       return (ctf_set_open_errno (errp, ECTF_FMT));
     }
 
@@ -95,15 +97,40 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
   ctf_sect_t *strsectp = NULL;
   const char *bfderrstr = NULL;
   char *strtab_alloc = NULL;
+  int symsect_endianness = -1;
 
 #ifdef HAVE_BFD_ELF
   ctf_sect_t symsect, strsect;
-  Elf_Internal_Shdr *symhdr = &elf_symtab_hdr (abfd);
+  Elf_Internal_Shdr *symhdr;
   size_t symcount;
   Elf_Internal_Sym *isymbuf;
   bfd_byte *symtab = NULL;
+  const char *symtab_name;
   const char *strtab = NULL;
+  const char *strtab_name;
   size_t strsize;
+  const ctf_preamble_t *preamble;
+
+  if (ctfsect->cts_data == NULL)
+    {
+      bfderrstr = N_("CTF section is NULL");
+      goto err;
+    }
+  preamble = ctf_arc_bufpreamble (ctfsect);
+
+  if (preamble->ctp_flags & CTF_F_DYNSTR)
+    {
+      symhdr = &elf_tdata (abfd)->dynsymtab_hdr;
+      strtab_name = ".dynstr";
+      symtab_name = ".dynsym";
+    }
+  else
+    {
+      symhdr = &elf_tdata (abfd)->symtab_hdr;
+      strtab_name = ".strtab";
+      symtab_name = ".symtab";
+    }
+
   /* TODO: handle SYMTAB_SHNDX.  */
 
   /* Get the symtab, and the strtab associated with it.  */
@@ -112,7 +139,7 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
       symcount = symhdr->sh_size / symhdr->sh_entsize;
       if ((symtab = malloc (symhdr->sh_size)) == NULL)
        {
-         bfderrstr = "Cannot malloc symbol table";
+         bfderrstr = N_("cannot malloc symbol table");
          goto err;
        }
 
@@ -121,7 +148,7 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
       free (isymbuf);
       if (isymbuf == NULL)
        {
-         bfderrstr = "Cannot read symbol table";
+         bfderrstr = N_("cannot read symbol table");
          goto err_free_sym;
        }
 
@@ -135,7 +162,7 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
            {
              if ((strtab = bfd_elf_get_str_section (abfd, symhdr->sh_link)) == NULL)
                {
-                 bfderrstr = "Cannot read string table";
+                 bfderrstr = N_("cannot read string table");
                  goto err_free_sym;
                }
            }
@@ -143,12 +170,12 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
            strtab = (const char *) strhdr->contents;
        }
     }
-  else         /* No symtab: just try getting .strtab by name.  */
+  else         /* No symtab: just try getting .strtab or .dynstr by name.  */
     {
       bfd_byte *str_bcontents;
       asection *str_asect;
 
-      if ((str_asect = bfd_get_section_by_name (abfd, ".strtab")) != NULL)
+      if ((str_asect = bfd_get_section_by_name (abfd, strtab_name)) != NULL)
        {
          if (bfd_malloc_and_get_section (abfd, str_asect, &str_bcontents))
            {
@@ -166,7 +193,7 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
         use it for anything but debugging.  */
 
       strsect.cts_data = strtab;
-      strsect.cts_name = ".strtab";
+      strsect.cts_name = strtab_name;
       strsect.cts_size = strsize;
       strsectp = &strsect;
     }
@@ -174,12 +201,14 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
   if (symtab)
     {
       assert (symhdr->sh_entsize == get_elf_backend_data (abfd)->s->sizeof_sym);
-      symsect.cts_name = ".symtab";
+      symsect.cts_name = symtab_name;
       symsect.cts_entsize = symhdr->sh_entsize;
       symsect.cts_size = symhdr->sh_size;
       symsect.cts_data = symtab;
       symsectp = &symsect;
     }
+
+  symsect_endianness = bfd_little_endian (abfd);
 #endif
 
   arci = ctf_arc_bufopen (ctfsect, symsectp, strsectp, errp);
@@ -189,6 +218,10 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
       arci->ctfi_free_symsect = 1;
       if (strtab_alloc)
        arci->ctfi_free_strsect = 1;
+
+      /* Get the endianness right.  */
+      if (symsect_endianness > -1)
+       ctf_arc_symsect_endianness (arci, symsect_endianness);
       return arci;
     }
 #ifdef HAVE_BFD_ELF
@@ -199,7 +232,7 @@ ctf_bfdopen_ctfsect (struct bfd *abfd _libctf_unused_,
 err: _libctf_unused_;
   if (bfderrstr)
     {
-      ctf_dprintf ("ctf_bfdopen(): %s: %s\n", bfderrstr,
+      ctf_err_warn (NULL, 0, 0, "ctf_bfdopen(): %s: %s", gettext (bfderrstr),
                   bfd_errmsg (bfd_get_error()));
       ctf_set_open_errno (errp, ECTF_FMT);
     }
@@ -207,10 +240,10 @@ err: _libctf_unused_;
 }
 
 /* Open the specified file descriptor and return a pointer to a CTF archive that
-   contains one or more CTF containers.  The file can be an ELF file, a raw CTF
-   file, or a CTF archive.  The caller is responsible for closing the file
-   descriptor when it is no longer needed.  If this is an ELF file, TARGET, if
-   non-NULL, should be the name of a suitable BFD target.  */
+   contains one or more CTF dicts.  The file can be an ELF file, a file
+   containing raw CTF, or a CTF archive.  The caller is responsible for closing
+   the file descriptor when it is no longer needed.  If this is an ELF file,
+   TARGET, if non-NULL, should be the name of a suitable BFD target.  */
 
 ctf_archive_t *
 ctf_fdopen (int fd, const char *filename, const char *target, int *errp)
@@ -243,7 +276,7 @@ ctf_fdopen (int fd, const char *filename, const char *target, int *errp)
       && (ctfhdr.ctp_magic == CTF_MAGIC
          || ctfhdr.ctp_magic == bswap_16 (CTF_MAGIC)))
     {
-      ctf_file_t *fp = NULL;
+      ctf_dict_t *fp = NULL;
       void *data;
 
       if ((data = ctf_mmap (st.st_size, 0, fd)) == NULL)
@@ -283,18 +316,18 @@ ctf_fdopen (int fd, const char *filename, const char *target, int *errp)
 
   if ((abfd = bfd_fdopenr (filename, target, nfd)) == NULL)
     {
-      ctf_dprintf ("Cannot open BFD from %s: %s\n",
-                  filename ? filename : "(unknown file)",
-                  bfd_errmsg (bfd_get_error()));
+      ctf_err_warn (NULL, 0, 0, _("cannot open BFD from %s: %s"),
+                   filename ? filename : _("(unknown file)"),
+                   bfd_errmsg (bfd_get_error ()));
       return (ctf_set_open_errno (errp, ECTF_FMT));
     }
   bfd_set_cacheable (abfd, 1);
 
   if (!bfd_check_format (abfd, bfd_object))
     {
-      ctf_dprintf ("BFD format problem in %s: %s\n",
-                  filename ? filename : "(unknown file)",
-                  bfd_errmsg (bfd_get_error()));
+      ctf_err_warn (NULL, 0, 0, _("BFD format problem in %s: %s"),
+                   filename ? filename : _("(unknown file)"),
+                   bfd_errmsg (bfd_get_error ()));
       if (bfd_get_error() == bfd_error_file_ambiguously_recognized)
        return (ctf_set_open_errno (errp, ECTF_BFD_AMBIGUOUS));
       else
@@ -304,7 +337,8 @@ ctf_fdopen (int fd, const char *filename, const char *target, int *errp)
   if ((arci = ctf_bfdopen (abfd, errp)) == NULL)
     {
       if (!bfd_close_all_done (abfd))
-       ctf_dprintf ("Cannot close BFD: %s\n", bfd_errmsg (bfd_get_error()));
+       ctf_err_warn (NULL, 0, 0, _("cannot close BFD: %s"),
+                     bfd_errmsg (bfd_get_error ()));
       return NULL;                     /* errno is set for us.  */
     }
   arci->ctfi_bfd_close = ctf_bfdclose;
@@ -313,7 +347,7 @@ ctf_fdopen (int fd, const char *filename, const char *target, int *errp)
   return arci;
 }
 
-/* Open the specified file and return a pointer to a CTF container.  The file
+/* Open the specified file and return a pointer to a CTF dict.  The file
    can be either an ELF file or raw CTF file.  This is just a convenient
    wrapper around ctf_fdopen() for callers.  */