From: Luis Machado Date: Thu, 1 Oct 2020 18:59:23 +0000 (-0300) Subject: [Morello] Record mapping symbols and mark C64 function symbols as special X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=837afe9d9f60c5984d8934e3ea6a3380e15b7524;p=thirdparty%2Fbinutils-gdb.git [Morello] Record mapping symbols and mark C64 function symbols as special This patch teaches GDB about AArch64 mapping symbols and special minimal symbols. FIXME-Morello: This is currently not actively used, but will be used to detect capability mode functions later. gdb/ChangeLog: 2020-10-20 Luis Machado * aarch64-tdep.c: Include elf-bfd.h. (MSYMBOL_SET_SPECIAL, MSYMBOL_IS_SPECIAL): New constants. (aarch64_mapping_symbol): New struct. (aarch64_mapping_symbol_vec): New typedef. (aarch64_per_bfd): New struct. (aarch64_bfd_data_key): New static global. (aarch64_elf_make_msymbol_special, +aarch64_record_special_symbol): New function. (aarch64_gdbarch_init): Register hooks. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 8058c6b4e44..c480bc8077c 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,15 @@ +2020-10-20 Luis Machado + + * aarch64-tdep.c: Include elf-bfd.h. + (MSYMBOL_SET_SPECIAL, MSYMBOL_IS_SPECIAL): New constants. + (aarch64_mapping_symbol): New struct. + (aarch64_mapping_symbol_vec): New typedef. + (aarch64_per_bfd): New struct. + (aarch64_bfd_data_key): New static global. + (aarch64_elf_make_msymbol_special, +aarch64_record_special_symbol): New + function. + (aarch64_gdbarch_init): Register hooks. + 2020-10-20 Luis Machado * aarch64-tdep.c (aarch64_bfd_has_capabilities): New function. diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c index 6938acad28a..1cf1e9ccf23 100644 --- a/gdb/aarch64-tdep.c +++ b/gdb/aarch64-tdep.c @@ -58,6 +58,8 @@ /* For address/int to pointer conversions. */ #include "inferior.h" +#include "elf-bfd.h" + #define submask(x) ((1L << ((x) + 1)) - 1) #define bit(obj,st) (((obj) >> (st)) & 1) #define bits(obj,st,fn) (((obj) >> (st)) & submask ((fn) - (st))) @@ -69,6 +71,57 @@ /* All possible aarch64 target descriptors. */ struct target_desc *tdesc_aarch64_list[AARCH64_MAX_SVE_VQ + 1][2/*pauth*/][2 /* capability */]; +/* Macros for setting and testing a bit in a minimal symbol that marks + it as C64 function. The MSB of the minimal symbol's "info" field + is used for this purpose. + + MSYMBOL_SET_SPECIAL Actually sets the "special" bit. + MSYMBOL_IS_SPECIAL Tests the "special" bit in a minimal symbol. */ + +#define MSYMBOL_SET_SPECIAL(msym) \ + MSYMBOL_TARGET_FLAG_1 (msym) = 1 + +#define MSYMBOL_IS_SPECIAL(msym) \ + MSYMBOL_TARGET_FLAG_1 (msym) + +struct aarch64_mapping_symbol +{ + CORE_ADDR value; + char type; + + bool operator< (const aarch64_mapping_symbol &other) const + { return this->value < other.value; } +}; + +typedef std::vector aarch64_mapping_symbol_vec; + +struct aarch64_per_bfd +{ + explicit aarch64_per_bfd (size_t num_sections) + : section_maps (new aarch64_mapping_symbol_vec[num_sections]), + section_maps_sorted (new bool[num_sections] ()) + {} + + DISABLE_COPY_AND_ASSIGN (aarch64_per_bfd); + + /* Information about mapping symbols ($x, $c, $d) in the objfile. + + The format is an array of vectors of aarch64_mapping_symbols, there is one + vector for each section of the objfile (the array is index by BFD section + index). + + For each section, the vector of aarch64_mapping_symbol is sorted by + symbol value (address). */ + std::unique_ptr section_maps; + + /* For each corresponding element of section_maps above, is this vector + sorted. */ + std::unique_ptr section_maps_sorted; +}; + +/* Per-bfd data used for mapping symbols. */ +static bfd_key aarch64_bfd_data_key; + /* The list of available aarch64 set/show commands. */ static struct cmd_list_element *set_aarch64_cmdlist = NULL; static struct cmd_list_element *show_aarch64_cmdlist = NULL; @@ -3623,6 +3676,78 @@ aarch64_bfd_has_capabilities (bfd *abfd) return false; } +/* Test whether SYM corresponds to an address in C64 code. If so, + set a special bit in MSYM to indicate that it does. */ + +static void +aarch64_elf_make_msymbol_special(asymbol *sym, struct minimal_symbol *msym) +{ + if (aarch64_debug) + debug_printf ("%s: Entering\n", __func__); + + /* We are interested in symbols that represent functions whose addresses + have the LSB set. */ + if ((sym->flags & BSF_FUNCTION) + && (MSYMBOL_VALUE_RAW_ADDRESS (msym) & 1)) + { + /* Set the special bit and mask off the LSB. */ + MSYMBOL_SET_SPECIAL (msym); + SET_MSYMBOL_VALUE_ADDRESS (msym, MSYMBOL_VALUE_RAW_ADDRESS (msym) & ~1); + } + + if (aarch64_debug) + debug_printf ("%s: Symbol %s is %sspecial\n", __func__, + sym->name, MSYMBOL_IS_SPECIAL (msym)? "" : "not "); +} + +/* Record mapping symbols for Morello. From the documentation, those + can be: + + $x or $x.: Start of a sequence of A64 instructions + + $c or $c.: Start of a sequence of C64 instructions + + $d or $d.: Start of a sequence of data items (for example, a literal + pool) +*/ + +static void +aarch64_record_special_symbol (struct gdbarch *gdbarch, struct objfile *objfile, + asymbol *sym) +{ + if (aarch64_debug) + debug_printf ("%s: Entering\n", __func__); + + const char *name = bfd_asymbol_name (sym); + struct aarch64_per_bfd *data; + struct aarch64_mapping_symbol new_map_sym; + + gdb_assert (name[0] == '$'); + + if(aarch64_debug) + debug_printf ("%s: Checking symbol %s\n", __func__, name); + + if (name[1] != 'x' && name[1] != 'c' && name[1] != 'd') + return; + + data = aarch64_bfd_data_key.get (objfile->obfd); + if (data == NULL) + data = aarch64_bfd_data_key.emplace (objfile->obfd, + objfile->obfd->section_count); + aarch64_mapping_symbol_vec &map + = data->section_maps[bfd_asymbol_section (sym)->index]; + + new_map_sym.value = sym->value; + new_map_sym.type = name[1]; + + /* Insert at the end, the vector will be sorted on first use. */ + map.push_back (new_map_sym); + + if (aarch64_debug) + debug_printf ("%s: Symbol %s recorded as special.\n", __func__, + name); +} + /* Initialize the current architecture based on INFO. If possible, re-use an architecture from ARCHES, which is a list of architectures already created during this debugging session. @@ -3923,6 +4048,13 @@ aarch64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches) set_gdbarch_address_to_pointer (gdbarch, aarch64_address_to_pointer); set_gdbarch_integer_to_address (gdbarch, aarch64_integer_to_address); + /* For marking special symbols indicating a C64 region. */ + set_gdbarch_elf_make_msymbol_special (gdbarch, + aarch64_elf_make_msymbol_special); + /* For recording mapping symbols. */ + set_gdbarch_record_special_symbol (gdbarch, + aarch64_record_special_symbol); + /* Create the Morello register aliases. */ /* cip0 and cip1 */ aarch64_morello_register_aliases[0].regnum = tdep->cap_reg_base + 16;