diff options
author | Tiezhu Yang <yangtiezhu@loongson.cn> | 2022-02-11 20:12:30 +0800 |
---|---|---|
committer | Tiezhu Yang <yangtiezhu@loongson.cn> | 2022-02-11 20:12:30 +0800 |
commit | e74d08100dd3d1c5aee6eb67235465ebed194380 (patch) | |
tree | ae3e047cf6c2ec47d755317a569b7b4803ec621c /gdb/arch | |
parent | 7c1aa0090f673e29e0b54eae9975b2c1b72a49d2 (diff) | |
download | fsf-binutils-gdb-e74d08100dd3d1c5aee6eb67235465ebed194380.zip fsf-binutils-gdb-e74d08100dd3d1c5aee6eb67235465ebed194380.tar.gz fsf-binutils-gdb-e74d08100dd3d1c5aee6eb67235465ebed194380.tar.bz2 |
gdb: LoongArch: Add initial target description support
This commit adds initial target description support for LoongArch.
Signed-off-by: Zhensong Liu <liuzhensong@loongson.cn>
Signed-off-by: Qing zhang <zhangqing@loongson.cn>
Signed-off-by: Youling Tang <tangyouling@loongson.cn>
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
Diffstat (limited to 'gdb/arch')
-rw-r--r-- | gdb/arch/loongarch.c | 88 | ||||
-rw-r--r-- | gdb/arch/loongarch.h | 73 |
2 files changed, 161 insertions, 0 deletions
diff --git a/gdb/arch/loongarch.c b/gdb/arch/loongarch.c new file mode 100644 index 0000000..934f6e4 --- /dev/null +++ b/gdb/arch/loongarch.c @@ -0,0 +1,88 @@ +/* Copyright (C) 2022 Free Software Foundation, Inc. + + This file is part of GDB. + + This program 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 of the License, 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. If not, see <http://www.gnu.org/licenses/>. */ + +#include "gdbsupport/common-defs.h" +#include "loongarch.h" +#include <stdlib.h> +#include <unordered_map> + +/* Target description features. */ + +#include "../features/loongarch/base32.c" +#include "../features/loongarch/base64.c" + +static target_desc_up +loongarch_create_target_description (const struct loongarch_gdbarch_features features) +{ + /* Now we should create a new target description. */ + target_desc_up tdesc = allocate_target_description (); + + std::string arch_name = "loongarch"; + + if (features.xlen == 4) + arch_name.append ("32"); + else if (features.xlen == 8) + arch_name.append ("64"); + + set_tdesc_architecture (tdesc.get (), arch_name.c_str ()); + + long regnum = 0; + + /* For now we only support creating 32-bit or 64-bit x-registers. */ + if (features.xlen == 4) + regnum = create_feature_loongarch_base32 (tdesc.get (), regnum); + else if (features.xlen == 8) + regnum = create_feature_loongarch_base64 (tdesc.get (), regnum); + + return tdesc; +} + +/* Wrapper used by std::unordered_map to generate hash for feature set. */ +struct loongarch_gdbarch_features_hasher +{ + std::size_t + operator() (const loongarch_gdbarch_features &features) const noexcept + { + return features.hash (); + } +}; + +/* Cache of previously seen target descriptions, indexed by the feature set + that created them. */ +static std::unordered_map<loongarch_gdbarch_features, + const target_desc_up, + loongarch_gdbarch_features_hasher> loongarch_tdesc_cache; + +const target_desc * +loongarch_lookup_target_description (const struct loongarch_gdbarch_features features) +{ + /* Lookup in the cache. If we find it then return the pointer out of + the target_desc_up (which is a unique_ptr). This is safe as the + loongarch_tdesc_cache will exist until GDB exits. */ + const auto it = loongarch_tdesc_cache.find (features); + if (it != loongarch_tdesc_cache.end ()) + return it->second.get (); + + target_desc_up tdesc (loongarch_create_target_description (features)); + + /* Add to the cache, and return a pointer borrowed from the + target_desc_up. This is safe as the cache (and the pointers + contained within it) are not deleted until GDB exits. */ + target_desc *ptr = tdesc.get (); + loongarch_tdesc_cache.emplace (features, std::move (tdesc)); + return ptr; +} diff --git a/gdb/arch/loongarch.h b/gdb/arch/loongarch.h new file mode 100644 index 0000000..9e10df9 --- /dev/null +++ b/gdb/arch/loongarch.h @@ -0,0 +1,73 @@ +/* Common target-dependent functionality for LoongArch + + Copyright (C) 2022 Free Software Foundation, Inc. + + This file is part of GDB. + + This program 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 of the License, 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. If not, see <http://www.gnu.org/licenses/>. */ + +#ifndef ARCH_LOONGARCH_H +#define ARCH_LOONGARCH_H + +#include "gdbsupport/tdesc.h" + +/* The set of LoongArch architectural features that we track that impact how + we configure the actual gdbarch instance. We hold one of these in the + gdbarch_tdep structure, and use it to distinguish between different + LoongArch gdbarch instances. + + The information in here ideally comes from the target description, + however, if the target doesn't provide a target description then we will + create a default target description by first populating one of these + based on what we know about the binary being executed, and using that to + drive default target description creation. */ + +struct loongarch_gdbarch_features +{ + /* The size of the x-registers in bytes. This is either 4 (loongarch32) + or 8 (loongarch64). No other value is valid. Initialise to the invalid + 0 value so we can spot if one of these is used uninitialised. */ + int xlen = 0; + + /* Equality operator. */ + bool operator== (const struct loongarch_gdbarch_features &rhs) const + { + return (xlen == rhs.xlen); + } + + /* Inequality operator. */ + bool operator!= (const struct loongarch_gdbarch_features &rhs) const + { + return !((*this) == rhs); + } + + /* Used by std::unordered_map to hash feature sets. */ + std::size_t hash () const noexcept + { + std::size_t val = (xlen & 0x1f) << 5; + return val; + } +}; + +/* Lookup an already existing target description matching FEATURES, or + create a new target description if this is the first time we have seen + FEATURES. For the same FEATURES the same target_desc is always + returned. This is important when trying to lookup gdbarch objects as + GDBARCH_LIST_LOOKUP_BY_INFO performs a pointer comparison on target + descriptions to find candidate gdbarch objects. */ + +const target_desc *loongarch_lookup_target_description + (const struct loongarch_gdbarch_features features); + +#endif /* ARCH_LOONGARCH_H */ |