X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=bfd%2Felfxx-riscv.c;h=5dd36ab965a9e56fe778d3c408faa2ec289c1c2c;hb=1484644d3597a98be824220ca290de84408eeb3f;hp=c0f006f5e5a949db3885efc904db74611e9d6cf3;hpb=219d1afa89d0d53ca93a684cac341f16470f3ca0;p=deliverable%2Fbinutils-gdb.git diff --git a/bfd/elfxx-riscv.c b/bfd/elfxx-riscv.c index c0f006f5e5..5dd36ab965 100644 --- a/bfd/elfxx-riscv.c +++ b/bfd/elfxx-riscv.c @@ -1,5 +1,5 @@ /* RISC-V-specific support for ELF. - Copyright (C) 2011-2018 Free Software Foundation, Inc. + Copyright (C) 2011-2020 Free Software Foundation, Inc. Contributed by Andrew Waterman (andrew@sifive.com). Based on TILE-Gx and MIPS targets. @@ -28,7 +28,7 @@ #include "opcode/riscv.h" #include "libiberty.h" #include "elfxx-riscv.h" -#include +#include "safe-ctype.h" #define MINUS_ONE ((bfd_vma)0 - 1) @@ -948,11 +948,12 @@ riscv_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name) } reloc_howto_type * -riscv_elf_rtype_to_howto (unsigned int r_type) +riscv_elf_rtype_to_howto (bfd *abfd, unsigned int r_type) { if (r_type >= ARRAY_SIZE (howto_table)) { - (*_bfd_error_handler) (_("unrecognized relocation (0x%x)"), r_type); + (*_bfd_error_handler) (_("%pB: unsupported relocation type %#x"), + abfd, r_type); bfd_set_error (bfd_error_bad_value); return NULL; } @@ -1009,3 +1010,743 @@ riscv_elf_add_sub_reloc (bfd *abfd, return bfd_reloc_ok; } + +/* Parsing subset version. + + Return Value: + Points to the end of version + + Arguments: + `rps`: Hooks and status for parsing subset. + `march`: Full arch string. + `p`: Curent parsing position. + `major_version`: Parsing result of major version, using + default_major_version if version is not present in arch string. + `minor_version`: Parsing result of minor version, set to 0 if version is + not present in arch string, but set to `default_minor_version` if + `major_version` using default_major_version. + `std_ext_p`: True if parsing std extension. + `use_default_version`: Set it to True if we need the default version. */ + +static const char * +riscv_parsing_subset_version (riscv_parse_subset_t *rps, + const char *march, + const char *p, + unsigned *major_version, + unsigned *minor_version, + bfd_boolean std_ext_p, + bfd_boolean *use_default_version) +{ + bfd_boolean major_p = TRUE; + unsigned version = 0; + char np; + + *major_version = 0; + *minor_version = 0; + for (; *p; ++p) + { + if (*p == 'p') + { + np = *(p + 1); + + if (!ISDIGIT (np)) + { + /* Might be beginning of `p` extension. */ + if (std_ext_p) + { + *major_version = version; + *minor_version = 0; + return p; + } + else + { + rps->error_handler + (_("-march=%s: Expect number after `%dp'."), + march, version); + return NULL; + } + } + + *major_version = version; + major_p = FALSE; + version = 0; + } + else if (ISDIGIT (*p)) + version = (version * 10) + (*p - '0'); + else + break; + } + + if (major_p) + *major_version = version; + else + *minor_version = version; + + /* We can not find any version in string, need to parse default version. */ + if (use_default_version != NULL + && *major_version == 0 + && *minor_version == 0) + *use_default_version = TRUE; + return p; +} + +/* Return string which contain all supported standard extensions in + canonical order. */ + +const char * +riscv_supported_std_ext (void) +{ + return "mafdqlcbjtpvn"; +} + +/* Parsing function for standard extensions. + + Return Value: + Points to the end of extensions. + + Arguments: + `rps`: Hooks and status for parsing subset. + `march`: Full arch string. + `p`: Curent parsing position. */ + +static const char * +riscv_parse_std_ext (riscv_parse_subset_t *rps, + const char *march, + const char *p) +{ + const char *all_std_exts = riscv_supported_std_ext (); + const char *std_exts = all_std_exts; + unsigned major_version = 0; + unsigned minor_version = 0; + char std_ext = '\0'; + bfd_boolean use_default_version = FALSE; + + /* First letter must start with i, e or g. */ + switch (*p) + { + case 'i': + p = riscv_parsing_subset_version (rps, + march, + ++p, + &major_version, + &minor_version, + /* std_ext_p= */TRUE, + &use_default_version); + + /* Find the default version if needed. */ + if (use_default_version + && rps->get_default_version != NULL) + rps->get_default_version ("i", + &major_version, + &minor_version); + riscv_add_subset (rps->subset_list, "i", + major_version, minor_version); + break; + + case 'e': + p = riscv_parsing_subset_version (rps, + march, + ++p, + &major_version, + &minor_version, + /* std_ext_p= */TRUE, + &use_default_version); + + /* Find the default version if needed. */ + if (use_default_version + && rps->get_default_version != NULL) + rps->get_default_version ("e", + &major_version, + &minor_version); + riscv_add_subset (rps->subset_list, "e", + major_version, minor_version); + + /* i-ext must be enabled. */ + if (rps->get_default_version != NULL) + rps->get_default_version ("i", + &major_version, + &minor_version); + riscv_add_subset (rps->subset_list, "i", + major_version, minor_version); + + if (*rps->xlen > 32) + { + rps->error_handler + (_("-march=%s: rv%de is not a valid base ISA"), + march, *rps->xlen); + return NULL; + } + break; + + case 'g': + /* The g-ext shouldn't has the version, so we just + skip the setting if user set a version to it. */ + p = riscv_parsing_subset_version (rps, + march, + ++p, + &major_version, + &minor_version, + TRUE, + &use_default_version); + + /* i-ext must be enabled. */ + if (rps->get_default_version != NULL) + rps->get_default_version ("i", + &major_version, + &minor_version); + riscv_add_subset (rps->subset_list, "i", + major_version, minor_version); + + for ( ; *std_exts != 'q'; std_exts++) + { + const char subset[] = {*std_exts, '\0'}; + + if (rps->get_default_version != NULL) + rps->get_default_version (subset, + &major_version, + &minor_version); + riscv_add_subset (rps->subset_list, subset, + major_version, minor_version); + } + break; + + default: + rps->error_handler + (_("-march=%s: first ISA subset must be `e', `i' or `g'"), march); + return NULL; + } + + /* The riscv_parsing_subset_version may set `p` to NULL, so I think we should + skip parsing the string if `p` is NULL or value of `p` is `\0`. */ + while (p != NULL && *p != '\0') + { + char subset[2] = {0, 0}; + + if (*p == 'x' || *p == 's' || *p == 'z') + break; + + if (*p == '_') + { + p++; + continue; + } + + std_ext = *p; + + /* Checking canonical order. */ + while (*std_exts && std_ext != *std_exts) std_exts++; + + if (std_ext != *std_exts) + { + if (strchr (all_std_exts, std_ext) == NULL) + rps->error_handler + (_("-march=%s: unsupported ISA subset `%c'"), march, *p); + else + rps->error_handler + (_("-march=%s: ISA string is not in canonical order. `%c'"), + march, *p); + return NULL; + } + + std_exts++; + + use_default_version = FALSE; + subset[0] = std_ext; + p = riscv_parsing_subset_version (rps, + march, + ++p, + &major_version, + &minor_version, + TRUE, + &use_default_version); + + /* Find the default version if needed. */ + if (use_default_version + && rps->get_default_version != NULL) + rps->get_default_version (subset, + &major_version, + &minor_version); + riscv_add_subset (rps->subset_list, subset, + major_version, minor_version); + } + return p; +} + +/* Classify the argument 'arch' into one of riscv_isa_ext_class_t. */ + +riscv_isa_ext_class_t +riscv_get_prefix_class (const char *arch) +{ + switch (*arch) + { + case 's': return RV_ISA_CLASS_S; + case 'x': return RV_ISA_CLASS_X; + case 'z': return RV_ISA_CLASS_Z; + default: return RV_ISA_CLASS_UNKNOWN; + } +} + +/* Structure describing parameters to use when parsing a particular + riscv_isa_ext_class_t. One of these should be provided for each + possible class, except RV_ISA_CLASS_UNKNOWN. */ + +typedef struct riscv_parse_config +{ + /* Class of the extension. */ + riscv_isa_ext_class_t class; + + /* Lower-case prefix string for error printing + and internal parser usage, e.g. "z", "x". */ + const char *prefix; + + /* Predicate which is used for checking whether + this is a "known" extension. For 'x', + it always returns true (since they are by + definition non-standard and cannot be known. */ + bfd_boolean (*ext_valid_p) (const char *); +} riscv_parse_config_t; + +/* Parse a generic prefixed extension. + `rps`: Hooks and status for parsing subset. + `march`: The full architecture string as passed in by "-march=...". + `p`: Point from which to start parsing the -march string. + `config`: What class of extensions to parse, predicate funcs, + and strings to use in error reporting. */ + +static const char * +riscv_parse_prefixed_ext (riscv_parse_subset_t *rps, + const char *march, + const char *p, + const riscv_parse_config_t *config) +{ + unsigned major_version = 0; + unsigned minor_version = 0; + const char *last_name; + riscv_isa_ext_class_t class; + bfd_boolean use_default_version; + + while (*p) + { + if (*p == '_') + { + p++; + continue; + } + + /* Assert that the current extension specifier matches our parsing + class. */ + class = riscv_get_prefix_class (p); + if (class != config->class) + break; + + char *subset = xstrdup (p); + char *q = subset; + const char *end_of_version; + + while (*++q != '\0' && *q != '_' && !ISDIGIT (*q)) + ; + + use_default_version = FALSE; + end_of_version = + riscv_parsing_subset_version (rps, march, q, &major_version, + &minor_version, FALSE, + &use_default_version); + *q = '\0'; + + /* Check that the name is valid. + For 'x', anything goes but it cannot simply be 'x'. + For 's', it must be known from a list and cannot simply be 's'. + For 'z', it must be known from a list and cannot simply be 'z'. */ + + /* Check that the extension name is well-formed. */ + if (!config->ext_valid_p (subset)) + { + rps->error_handler + (_("-march=%s: Invalid or unknown %s ISA extension: '%s'"), + march, config->prefix, subset); + free (subset); + return NULL; + } + + /* Check that the last item is not the same as this. */ + last_name = rps->subset_list->tail->name; + if (!strcasecmp (last_name, subset)) + { + rps->error_handler + (_("-march=%s: Duplicate %s ISA extension: \'%s\'"), + march, config->prefix, subset); + free (subset); + return NULL; + } + + /* Check that we are in alphabetical order within the subset. */ + if (!strncasecmp (last_name, config->prefix, 1) + && strcasecmp (last_name, subset) > 0) + { + rps->error_handler + (_("\ +-march=%s: %s ISA extension not in alphabetical order: \'%s\' must come before \'%s\'."), + march, config->prefix, subset, last_name); + free (subset); + return NULL; + } + + /* Find the default version if needed. */ + if (use_default_version + && rps->get_default_version != NULL) + rps->get_default_version (subset, + &major_version, + &minor_version); + riscv_add_subset (rps->subset_list, subset, + major_version, minor_version); + + free (subset); + p += end_of_version - subset; + + if (*p != '\0' && *p != '_') + { + rps->error_handler (_("-march=%s: %s must separate with _"), + march, config->prefix); + return NULL; + } + } + + return p; +} + +/* List of Z-class extensions that binutils should know about. + Whether or not a particular entry is in this list will + dictate if gas/ld will accept its presence in the -march + string. + + Example: To add an extension called "Zbb" (bitmanip base extension), + add "zbb" string to the list (all lowercase). + + Keep this list alphabetically ordered. */ + +static const char * const riscv_std_z_ext_strtab[] = + { + "zicsr", NULL + }; + +/* Same as `riscv_std_z_ext_strtab', but for S-class extensions. */ + +static const char * const riscv_std_s_ext_strtab[] = + { + NULL + }; + +/* For the extension EXT, search through the list of known extensions + KNOWN_EXTS for a match, and return TRUE if found. */ + +static bfd_boolean +riscv_multi_letter_ext_valid_p (const char *ext, + const char *const *known_exts) +{ + size_t i; + + for (i = 0; known_exts[i]; ++i) + if (!strcmp (ext, known_exts[i])) + return TRUE; + + return FALSE; +} + +/* Predicator function for x-prefixed extensions. + Anything goes, except the literal 'x'. */ + +static bfd_boolean +riscv_ext_x_valid_p (const char *arg) +{ + if (!strcasecmp (arg, "x")) + return FALSE; + + return TRUE; +} + +/* Predicator functions for z-prefixed extensions. + Only known z-extensions are permitted. */ + +static bfd_boolean +riscv_ext_z_valid_p (const char *arg) +{ + return riscv_multi_letter_ext_valid_p (arg, riscv_std_z_ext_strtab); +} + +/* Predicator function for 's' prefixed extensions. + Must be either literal 's', or a known s-prefixed extension. */ + +static bfd_boolean +riscv_ext_s_valid_p (const char *arg) +{ + return riscv_multi_letter_ext_valid_p (arg, riscv_std_s_ext_strtab); +} + +/* Parsing order that is specified by the ISA manual. */ + +static const riscv_parse_config_t parse_config[] = +{ + {RV_ISA_CLASS_S, "s", riscv_ext_s_valid_p}, + {RV_ISA_CLASS_Z, "z", riscv_ext_z_valid_p}, + {RV_ISA_CLASS_X, "x", riscv_ext_x_valid_p}, + {RV_ISA_CLASS_UNKNOWN, NULL, NULL} +}; + +/* Function for parsing arch string. + + Return Value: + Return TRUE on success. + + Arguments: + `rps`: Hooks and status for parsing subset. + `arch`: Arch string. */ + +bfd_boolean +riscv_parse_subset (riscv_parse_subset_t *rps, + const char *arch) +{ + const char *p = arch; + size_t i; + + if (strncmp (p, "rv32", 4) == 0) + { + *rps->xlen = 32; + p += 4; + } + else if (strncmp (p, "rv64", 4) == 0) + { + *rps->xlen = 64; + p += 4; + } + else + { + rps->error_handler + (_("-march=%s: ISA string must begin with rv32 or rv64"), + arch); + return FALSE; + } + + /* Parsing standard extension. */ + p = riscv_parse_std_ext (rps, arch, p); + + if (p == NULL) + return FALSE; + + /* Parse the different classes of extensions in the specified order. */ + for (i = 0; i < ARRAY_SIZE (parse_config); ++i) { + p = riscv_parse_prefixed_ext (rps, arch, p, &parse_config[i]); + + if (p == NULL) + return FALSE; + } + + if (*p != '\0') + { + rps->error_handler (_("-march=%s: unexpected ISA string at end: %s"), + arch, p); + return FALSE; + } + + if (riscv_lookup_subset (rps->subset_list, "e") + && riscv_lookup_subset (rps->subset_list, "f")) + { + rps->error_handler + (_("-march=%s: rv32e does not support the `f' extension"), + arch); + return FALSE; + } + + if (riscv_lookup_subset (rps->subset_list, "d") + && !riscv_lookup_subset (rps->subset_list, "f")) + { + rps->error_handler + (_("-march=%s: `d' extension requires `f' extension"), + arch); + return FALSE; + } + + if (riscv_lookup_subset (rps->subset_list, "q") + && !riscv_lookup_subset (rps->subset_list, "d")) + { + rps->error_handler + (_("-march=%s: `q' extension requires `d' extension"), + arch); + return FALSE; + } + + if (riscv_lookup_subset (rps->subset_list, "q") && *rps->xlen < 64) + { + rps->error_handler + (_("-march=%s: rv32 does not support the `q' extension"), + arch); + return FALSE; + } + return TRUE; +} + +/* Add new subset to list. */ + +void +riscv_add_subset (riscv_subset_list_t *subset_list, + const char *subset, + int major, + int minor) +{ + riscv_subset_t *s = xmalloc (sizeof *s); + + if (subset_list->head == NULL) + subset_list->head = s; + + s->name = xstrdup (subset); + s->major_version = major; + s->minor_version = minor; + s->next = NULL; + + if (subset_list->tail != NULL) + subset_list->tail->next = s; + + subset_list->tail = s; +} + +/* Find subset in list without version checking, return NULL if not found. */ + +riscv_subset_t * +riscv_lookup_subset (const riscv_subset_list_t *subset_list, + const char *subset) +{ + return riscv_lookup_subset_version + (subset_list, subset, + RISCV_DONT_CARE_VERSION, + RISCV_DONT_CARE_VERSION); +} + +/* Find subset in list with version checking, return NULL if not found. */ + +riscv_subset_t * +riscv_lookup_subset_version (const riscv_subset_list_t *subset_list, + const char *subset, + int major, int minor) +{ + riscv_subset_t *s; + + for (s = subset_list->head; s != NULL; s = s->next) + if (strcasecmp (s->name, subset) == 0) + { + if ((major != RISCV_DONT_CARE_VERSION) + && (s->major_version != major)) + return NULL; + + if ((minor != RISCV_DONT_CARE_VERSION) + && (s->minor_version != minor)) + return NULL; + + return s; + } + + return NULL; +} + +/* Release subset list. */ + +void +riscv_release_subset_list (riscv_subset_list_t *subset_list) +{ + while (subset_list->head != NULL) + { + riscv_subset_t *next = subset_list->head->next; + free ((void *)subset_list->head->name); + free (subset_list->head); + subset_list->head = next; + } + + subset_list->tail = NULL; +} + +/* Return the number of digits for the input. */ + +size_t +riscv_estimate_digit (unsigned num) +{ + size_t digit = 0; + if (num == 0) + return 1; + + for (digit = 0; num ; num /= 10) + digit++; + + return digit; +} + +/* Auxiliary function to estimate string length of subset list. */ + +static size_t +riscv_estimate_arch_strlen1 (const riscv_subset_t *subset) +{ + if (subset == NULL) + return 6; /* For rv32/rv64/rv128 and string terminator. */ + + return riscv_estimate_arch_strlen1 (subset->next) + + strlen (subset->name) + + riscv_estimate_digit (subset->major_version) + + 1 /* For version seperator: 'p'. */ + + riscv_estimate_digit (subset->minor_version) + + 1 /* For underscore. */; +} + +/* Estimate the string length of this subset list. */ + +static size_t +riscv_estimate_arch_strlen (const riscv_subset_list_t *subset_list) +{ + return riscv_estimate_arch_strlen1 (subset_list->head); +} + +/* Auxiliary function to convert subset info to string. */ + +static void +riscv_arch_str1 (riscv_subset_t *subset, + char *attr_str, char *buf, size_t bufsz) +{ + const char *underline = "_"; + + if (subset == NULL) + return; + + /* No underline between rvXX and i/e. */ + if ((strcasecmp (subset->name, "i") == 0) + || (strcasecmp (subset->name, "e") == 0)) + underline = ""; + + snprintf (buf, bufsz, "%s%s%dp%d", + underline, + subset->name, + subset->major_version, + subset->minor_version); + + strncat (attr_str, buf, bufsz); + + /* Skip 'i' extension after 'e'. */ + if ((strcasecmp (subset->name, "e") == 0) + && subset->next + && (strcasecmp (subset->next->name, "i") == 0)) + riscv_arch_str1 (subset->next->next, attr_str, buf, bufsz); + else + riscv_arch_str1 (subset->next, attr_str, buf, bufsz); +} + +/* Convert subset info to string with explicit version info. */ + +char * +riscv_arch_str (unsigned xlen, const riscv_subset_list_t *subset) +{ + size_t arch_str_len = riscv_estimate_arch_strlen (subset); + char *attr_str = xmalloc (arch_str_len); + char *buf = xmalloc (arch_str_len); + + snprintf (attr_str, arch_str_len, "rv%u", xlen); + + riscv_arch_str1 (subset->head, attr_str, buf, arch_str_len); + free (buf); + + return attr_str; +}