aboutsummaryrefslogtreecommitdiff
path: root/libctf/ctf-open.c
diff options
context:
space:
mode:
authorNick Alcock <nick.alcock@oracle.com>2020-11-23 21:17:44 +0000
committerNick Alcock <nick.alcock@oracle.com>2020-11-25 19:11:35 +0000
commit53651de80f811495262a5b17b774a486dd37b326 (patch)
tree597993b81493a294506b5b721204adebf995b0d9 /libctf/ctf-open.c
parentcbfa382abb360307c0c6b750e4c550a0d2b702d6 (diff)
downloadgdb-53651de80f811495262a5b17b774a486dd37b326.zip
gdb-53651de80f811495262a5b17b774a486dd37b326.tar.gz
gdb-53651de80f811495262a5b17b774a486dd37b326.tar.bz2
libctf, include: support foreign-endianness symtabs with CTF
The CTF symbol lookup machinery added recently has one deficit: it assumes the symtab is in the machine's native endianness. This is always true when the linker is writing out symtabs (because cross linkers byteswap symbols only after libctf has been called on them), but may be untrue in the cross case when the linker or another tool (objdump, etc) is reading them. Unfortunately the easy way to model this to the caller, as an endianness field in the ctf_sect_t, is precluded because doing so would change the size of the ctf_sect_t, which would be an ABI break. So, instead, allow the endianness of the symtab to be set after open time, by calling one of the two new API functions ctf_symsect_endianness (for ctf_dict_t's) or ctf_arc_symsect_endianness (for entire ctf_archive_t's). libctf calls these functions automatically for objects opened via any of the BFD-aware mechanisms (ctf_bfdopen, ctf_bfdopen_ctfsect, ctf_fdopen, ctf_open, or ctf_arc_open), but the various mechanisms that just take raw ctf_sect_t's will assume the symtab is in native endianness and need a later call to ctf_*symsect_endianness to adjust it if needed. (This call is basically free if the endianness is actually native: it only costs anything if the symtab endianness was previously guessed wrong, and there is a symtab, and we are using it directly rather than using symtab indexing.) Obviously, calling ctf_lookup_by_symbol or ctf_symbol_next before the symtab endianness is correctly set will probably give wrong answers -- but you can set it at any time as long as it is before then. include/ChangeLog 2020-11-23 Nick Alcock <nick.alcock@oracle.com> * ctf-api.h: Style nit: remove () on function names in comments. (ctf_sect_t): Mention endianness concerns. (ctf_symsect_endianness): New declaration. (ctf_arc_symsect_endianness): Likewise. libctf/ChangeLog 2020-11-23 Nick Alcock <nick.alcock@oracle.com> * ctf-impl.h (ctf_dict_t) <ctf_symtab_little_endian>: New. (struct ctf_archive_internal) <ctfi_symsect_little_endian>: Likewise. * ctf-create.c (ctf_serialize): Adjust for new field. * ctf-open.c (init_symtab): Note the semantics of repeated calls. (ctf_symsect_endianness): New. (ctf_bufopen_internal): Set ctf_symtab_little_endian suitably for the native endianness. (_Static_assert): Moved... (swap_thing): ... with this... * swap.h: ... to here. * ctf-util.c (ctf_elf32_to_link_sym): Use it, byteswapping the Elf32_Sym if the ctf_symtab_little_endian demands it. (ctf_elf64_to_link_sym): Likewise swap the Elf64_Sym if needed. * ctf-archive.c (ctf_arc_symsect_endianness): New, set the endianness of the symtab used by the dicts in an archive. (ctf_archive_iter_internal): Initialize to unknown (assumed native, do not call ctf_symsect_endianness). (ctf_dict_open_by_offset): Call ctf_symsect_endianness if need be. (ctf_dict_open_internal): Propagate the endianness down. (ctf_dict_open_sections): Likewise. * ctf-open-bfd.c (ctf_bfdopen_ctfsect): Get the endianness from the struct bfd and pass it down to the archive. * libctf.ver: Add ctf_symsect_endianness and ctf_arc_symsect_endianness.
Diffstat (limited to 'libctf/ctf-open.c')
-rw-r--r--libctf/ctf-open.c56
1 files changed, 31 insertions, 25 deletions
diff --git a/libctf/ctf-open.c b/libctf/ctf-open.c
index ecf27ee..7816fd0 100644
--- a/libctf/ctf-open.c
+++ b/libctf/ctf-open.c
@@ -22,7 +22,6 @@
#include <string.h>
#include <sys/types.h>
#include <elf.h>
-#include <assert.h>
#include "swap.h"
#include <bfd.h>
#include <zlib.h>
@@ -227,7 +226,10 @@ static const ctf_dictops_t ctf_dictops[] = {
symtypetabs come from the compiler, and all the linker does is iteration over
all entries, which doesn't need this initialization.)
- The SP symbol table section may be NULL if there is no symtab. */
+ The SP symbol table section may be NULL if there is no symtab.
+
+ If init_symtab works on one call, it cannot fail on future calls to the same
+ fp: ctf_symsect_endianness relies on this. */
static int
init_symtab (ctf_dict_t *fp, const ctf_header_t *hp, const ctf_sect_t *sp)
@@ -290,6 +292,10 @@ init_symtab (ctf_dict_t *fp, const ctf_header_t *hp, const ctf_sect_t *sp)
return ECTF_SYMTAB;
}
+ /* This call may be led astray if our idea of the symtab's endianness is
+ wrong, but when this is fixed by a call to ctf_symsect_endianness,
+ init_symtab will be called again with the right endianness in
+ force. */
if (ctf_symtab_skippable (&sym))
{
*xp = -1u;
@@ -974,28 +980,6 @@ init_types (ctf_dict_t *fp, ctf_header_t *cth)
We flip everything, mindlessly, even 1-byte entities, so that future
expansions do not require changes to this code. */
-/* < C11? define away static assertions. */
-
-#if !defined (__STDC_VERSION__) || __STDC_VERSION__ < 201112L
-#define _Static_assert(cond, err)
-#endif
-
-/* Swap the endianness of something. */
-
-#define swap_thing(x) \
- do { \
- _Static_assert (sizeof (x) == 1 || (sizeof (x) % 2 == 0 \
- && sizeof (x) <= 8), \
- "Invalid size, update endianness code"); \
- switch (sizeof (x)) { \
- case 2: x = bswap_16 (x); break; \
- case 4: x = bswap_32 (x); break; \
- case 8: x = bswap_64 (x); break; \
- case 1: /* Nothing needs doing */ \
- break; \
- } \
- } while (0);
-
/* Flip the endianness of the CTF header. */
static void
@@ -1652,7 +1636,13 @@ ctf_bufopen_internal (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect,
large for the actual size of the object and function info sections: if so,
ctf_nsyms will be adjusted and the excess will never be used. It's
possible to do indexed symbol lookups even without a symbol table, so check
- even in that case. */
+ even in that case. Initially, we assume the symtab is native-endian: if it
+ isn't, the caller will inform us later by calling ctf_symsect_endianness. */
+#ifdef WORDS_BIGENDIAN
+ fp->ctf_symsect_little_endian = 0;
+#else
+ fp->ctf_symsect_little_endian = 1;
+#endif
if (symsect != NULL)
{
@@ -1866,6 +1856,22 @@ ctf_getstrsect (const ctf_dict_t *fp)
return fp->ctf_strtab;
}
+/* Set the endianness of the symbol table attached to FP. */
+void
+ctf_symsect_endianness (ctf_dict_t *fp, int little_endian)
+{
+ int old_endianness = fp->ctf_symsect_little_endian;
+
+ fp->ctf_symsect_little_endian = !!little_endian;
+
+ /* If we already have a symtab translation table, we need to repopulate it if
+ our idea of the endianness has changed. */
+
+ if (old_endianness != fp->ctf_symsect_little_endian
+ && fp->ctf_sxlate != NULL && fp->ctf_symtab.cts_data != NULL)
+ assert (init_symtab (fp, fp->ctf_header, &fp->ctf_symtab) == 0);
+}
+
/* Return the CTF handle for the parent CTF dict, if one exists. Otherwise
return NULL to indicate this dict has no imported parent. */
ctf_dict_t *