summaryrefslogtreecommitdiff
path: root/libctf/swap.h
diff options
context:
space:
mode:
authorNick Alcock <nick.alcock@oracle.com>2019-04-24 10:17:13 +0100
committerNick Alcock <nick.alcock@oracle.com>2019-05-28 17:07:46 +0100
commit72f3392127e1892cb203a98092b4ae32485365fe (patch)
tree9c851214b2dd4f6f1967c7be277b536684923085 /libctf/swap.h
parent47d546f427d0d3bf9f503b5b118ae05b49d73d28 (diff)
downloadbinutils-gdb-72f3392127e1892cb203a98092b4ae32485365fe.tar.gz
libctf: opening
This fills in the other half of the opening/creation puzzle: opening of already-existing CTF files. Such files are always read-only: if you want to add to a CTF file opened with one of the opening functions in this file, use ctf_add_type(), in a later commit, to copy appropriate types into a newly ctf_create()d, writable container. The lowest-level opening functions are in here: ctf_bufopen(), which takes ctf_sect_t structures akin to ELF section headers, and ctf_simple_open(), which can be used if you don't have an entire ELF section header to work from. Both will malloc() new space for the buffers only if necessary, will mmap() directly from the file if requested, and will mprotect() it afterwards to prevent accidental corruption of the types. These functions are also used by ctf_update() when converting types in a writable container into read-only types that can be looked up using the lookup functions (in later commits). The files are always of the native endianness of the system that created them: at read time, the endianness of the header magic number is used to determine whether or not the file needs byte-swapping, and the entire thing is aggressively byte-swapped. The agggressive nature of this swapping avoids complicating the rest of the code with endianness conversions, while the native endianness introduces no byte-swapping overhead in the common case. (The endianness-independence code is also much newer than everything else in this file, and deserves closer scrutiny.) The accessors at the top of the file are there to transparently support older versions of the CTF file format, allowing translation from older formats that have different sizes for the structures in ctf.h: currently, these older formats are intermingled with the newer ones in ctf.h: they will probably migrate to a compatibility header in time, to ease readability. The ctf_set_base() function is split out for the same reason: when conversion code to a newer format is written, it would need to malloc() new storage for the entire ctf_file_t if a file format change causes it to grow, and for that we need ctf_set_base() to be a separate function. One pair of linked data structures supported by this file has no creation code in libctf yet: the data and function object sections read by init_symtab(). These will probably arrive soon, when the linker comes to need them. (init_symtab() has hardly been changed since 2009, but if any code in libctf has rotted over time, this will.) A few simple accessors are also present that can even be called on read-only containers because they don't actually modify them, since the relevant things are not stored in the container but merely change its operation: ctf_setmodel(), which lets you specify whether a container is LP64 or not (used to statically determine the sizes of a few types), ctf_import(), which is the only way to associate a parent container with a child container, and ctf_setspecific(), which lets the caller associate an arbitrary pointer with the CTF container for any use. If the user doesn't call these functions correctly, libctf will misbehave: this is particularly important for ctf_import(), since a container built against a given parent container will not be able to resolve types that depend on types in the parent unless it is ctf_import()ed with a parent container with the same set of types at the same IDs, or a superset. Possible future extensions (also noted in the ctf-hash.c file) include storing a count of things so that we don't need to do one pass over the CTF file counting everything, and computing a perfect hash at CTF creation time in some compact form, storing it in the CTF file, and using it to hash things so we don't need to do a second pass over the entire CTF file to set up the hashes used to go from names to type IDs. (There are multiple such hashes, one for each C type namespace: types, enums, structs, and unions.) libctf/ * ctf-open.c: New file. * swap.h: Likewise. include/ * ctf-api.h (ctf_file_close): New declaration. (ctf_getdatasect): Likewise. (ctf_parent_file): Likewise. (ctf_parent_name): Likewise. (ctf_parent_name_set): Likewise. (ctf_import): Likewise. (ctf_setmodel): Likewise. (ctf_getmodel): Likewise. (ctf_setspecific): Likewise. (ctf_getspecific): Likewise.
Diffstat (limited to 'libctf/swap.h')
-rw-r--r--libctf/swap.h60
1 files changed, 60 insertions, 0 deletions
diff --git a/libctf/swap.h b/libctf/swap.h
new file mode 100644
index 00000000000..06a93301813
--- /dev/null
+++ b/libctf/swap.h
@@ -0,0 +1,60 @@
+/* Interface to byteswapping functions.
+ Copyright (C) 2006-2019 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>
+
+#ifdef HAVE_BYTESWAP_H
+#include <byteswap.h>
+#else
+
+/* Provide our own versions of the byteswap functions. */
+inline uint16_t
+bswap_16(uint16_t v)
+{
+ return ((v >> 8) & 0xff) | ((v & 0xff) << 8);
+}
+
+inline uint32_t
+bswap_32(uint32_t v)
+{
+ return ( ((v & 0xff000000) >> 24)
+ | ((v & 0x00ff0000) >> 8)
+ | ((v & 0x0000ff00) << 8)
+ | ((v & 0x000000ff) << 24));
+}
+
+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 /* !defined(HAVE_BYTESWAP_H) */
+
+#endif /* !defined(_CTF_SWAP_H) */