binutils-gdb/libctf/swap.h
Nick Alcock 53651de80f 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.
2020-11-25 19:11:35 +00:00

91 lines
2.5 KiB
C

/* Interface to byteswapping functions.
Copyright (C) 2006-2020 Free Software Foundation, Inc.
This file is part of libctf.
libctf is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not see
<http://www.gnu.org/licenses/>. */
#ifndef _CTF_SWAP_H
#define _CTF_SWAP_H
#include "config.h"
#include <stdint.h>
#include <assert.h>
#ifdef HAVE_BYTESWAP_H
#include <byteswap.h>
#endif /* defined(HAVE_BYTESWAP_H) */
/* Provide our own versions of the byteswap functions. */
#if !HAVE_DECL_BSWAP_16
static inline uint16_t
bswap_16 (uint16_t v)
{
return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
}
#endif /* !HAVE_DECL_BSWAP16 */
#if !HAVE_DECL_BSWAP_32
static inline uint32_t
bswap_32 (uint32_t v)
{
return ( ((v & 0xff000000) >> 24)
| ((v & 0x00ff0000) >> 8)
| ((v & 0x0000ff00) << 8)
| ((v & 0x000000ff) << 24));
}
#endif /* !HAVE_DECL_BSWAP32 */
#if !HAVE_DECL_BSWAP_64
static inline uint64_t
bswap_64 (uint64_t v)
{
return ( ((v & 0xff00000000000000ULL) >> 56)
| ((v & 0x00ff000000000000ULL) >> 40)
| ((v & 0x0000ff0000000000ULL) >> 24)
| ((v & 0x000000ff00000000ULL) >> 8)
| ((v & 0x00000000ff000000ULL) << 8)
| ((v & 0x0000000000ff0000ULL) << 24)
| ((v & 0x000000000000ff00ULL) << 40)
| ((v & 0x00000000000000ffULL) << 56));
}
#endif /* !HAVE_DECL_BSWAP64 */
/* < 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);
#endif /* !defined(_CTF_SWAP_H) */