Changeset 3e828ea in mainline for kernel/generic/src/lib


Ignore:
Timestamp:
2019-09-23T12:49:29Z (6 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
9be2358
Parents:
9259d20 (diff), 1a4ec93f (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Jiri Svoboda <jiri@…> (2019-09-22 12:49:07)
git-committer:
Jiri Svoboda <jiri@…> (2019-09-23 12:49:29)
Message:

Merge changes from master, especially Meson build

Location:
kernel/generic/src/lib
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/lib/elf.c

    r9259d20 r3e828ea  
    5050#include <lib/elf_load.h>
    5151
    52 static const char *error_codes[] = {
    53         "no error",
    54         "invalid image",
    55         "address space error",
    56         "incompatible image",
    57         "unsupported image type",
    58         "irrecoverable error"
    59 };
    60 
    61 static int load_segment(elf_segment_header_t *, elf_header_t *, as_t *);
     52static errno_t load_segment(elf_segment_header_t *, elf_header_t *, as_t *);
    6253
    6354/** ELF loader
     
    6758 * @param flags  A combination of ELD_F_*
    6859 *
    69  * @return EE_OK on success
     60 * @return EOK on success
    7061 *
    7162 */
    72 unsigned int elf_load(elf_header_t *header, as_t *as)
     63errno_t elf_load(elf_header_t *header, as_t *as)
    7364{
    7465        /* Identify ELF */
     
    7768            (header->e_ident[EI_MAG2] != ELFMAG2) ||
    7869            (header->e_ident[EI_MAG3] != ELFMAG3))
    79                 return EE_INVALID;
     70                return EINVAL;
    8071
    8172        /* Identify ELF compatibility */
     
    8576            (header->e_version != EV_CURRENT) ||
    8677            (header->e_ident[EI_CLASS] != ELF_CLASS))
    87                 return EE_INCOMPATIBLE;
     78                return EINVAL;
    8879
    8980        if (header->e_phentsize != sizeof(elf_segment_header_t))
    90                 return EE_INCOMPATIBLE;
     81                return EINVAL;
    9182
    9283        /* Check if the object type is supported. */
    9384        if (header->e_type != ET_EXEC)
    94                 return EE_UNSUPPORTED;
     85                return ENOTSUP;
    9586
    9687        /* Check if the ELF image starts on a page boundary */
    9788        if (ALIGN_UP((uintptr_t) header, PAGE_SIZE) != (uintptr_t) header)
    98                 return EE_UNSUPPORTED;
     89                return ENOTSUP;
    9990
    10091        /* Walk through all segment headers and process them. */
     
    10899                        continue;
    109100
    110                 int rc = load_segment(seghdr, header, as);
    111                 if (rc != EE_OK)
     101                errno_t rc = load_segment(seghdr, header, as);
     102                if (rc != EOK)
    112103                        return rc;
    113104        }
    114105
    115         return EE_OK;
    116 }
    117 
    118 /** Print error message according to error code.
    119  *
    120  * @param rc Return code returned by elf_load().
    121  *
    122  * @return NULL terminated description of error.
    123  *
    124  */
    125 const char *elf_error(unsigned int rc)
    126 {
    127         assert(rc < sizeof(error_codes) / sizeof(char *));
    128 
    129         return error_codes[rc];
     106        return EOK;
    130107}
    131108
     
    136113 * @param as    Address space into wich the ELF is being loaded.
    137114 *
    138  * @return EE_OK on success, error code otherwise.
     115 * @return EOK on success, error code otherwise.
    139116 *
    140117 */
    141 int load_segment(elf_segment_header_t *entry, elf_header_t *elf, as_t *as)
     118errno_t load_segment(elf_segment_header_t *entry, elf_header_t *elf, as_t *as)
    142119{
    143120        mem_backend_data_t backend_data;
     
    146123                if ((entry->p_offset % entry->p_align) !=
    147124                    (entry->p_vaddr % entry->p_align))
    148                         return EE_INVALID;
     125                        return EINVAL;
    149126        }
    150127
     
    177154            AS_AREA_ATTR_NONE, &elf_backend, &backend_data, &base, 0);
    178155        if (!area)
    179                 return EE_MEMORY;
     156                return ENOMEM;
    180157
    181158        /*
     
    184161         */
    185162
    186         return EE_OK;
     163        return EOK;
    187164}
    188165
  • kernel/generic/src/lib/str.c

    r9259d20 r3e828ea  
    789789        str_ncpy(dest, size + 1, src, size);
    790790        return dest;
    791 }
    792 
    793 /** Convert string to uint64_t (internal variant).
    794  *
    795  * @param nptr   Pointer to string.
    796  * @param endptr Pointer to the first invalid character is stored here.
    797  * @param base   Zero or number between 2 and 36 inclusive.
    798  * @param neg    Indication of unary minus is stored here.
    799  * @apram result Result of the conversion.
    800  *
    801  * @return EOK if conversion was successful.
    802  *
    803  */
    804 static errno_t str_uint(const char *nptr, char **endptr, unsigned int base,
    805     bool *neg, uint64_t *result)
    806 {
    807         assert(endptr != NULL);
    808         assert(neg != NULL);
    809         assert(result != NULL);
    810 
    811         *neg = false;
    812         const char *str = nptr;
    813 
    814         /* Ignore leading whitespace */
    815         while (isspace(*str))
    816                 str++;
    817 
    818         if (*str == '-') {
    819                 *neg = true;
    820                 str++;
    821         } else if (*str == '+')
    822                 str++;
    823 
    824         if (base == 0) {
    825                 /* Decode base if not specified */
    826                 base = 10;
    827 
    828                 if (*str == '0') {
    829                         base = 8;
    830                         str++;
    831 
    832                         switch (*str) {
    833                         case 'b':
    834                         case 'B':
    835                                 base = 2;
    836                                 str++;
    837                                 break;
    838                         case 'o':
    839                         case 'O':
    840                                 base = 8;
    841                                 str++;
    842                                 break;
    843                         case 'd':
    844                         case 'D':
    845                         case 't':
    846                         case 'T':
    847                                 base = 10;
    848                                 str++;
    849                                 break;
    850                         case 'x':
    851                         case 'X':
    852                                 base = 16;
    853                                 str++;
    854                                 break;
    855                         default:
    856                                 str--;
    857                         }
    858                 }
    859         } else {
    860                 /* Check base range */
    861                 if ((base < 2) || (base > 36)) {
    862                         *endptr = (char *) str;
    863                         return EINVAL;
    864                 }
    865         }
    866 
    867         *result = 0;
    868         const char *startstr = str;
    869 
    870         while (*str != 0) {
    871                 unsigned int digit;
    872 
    873                 if ((*str >= 'a') && (*str <= 'z'))
    874                         digit = *str - 'a' + 10;
    875                 else if ((*str >= 'A') && (*str <= 'Z'))
    876                         digit = *str - 'A' + 10;
    877                 else if ((*str >= '0') && (*str <= '9'))
    878                         digit = *str - '0';
    879                 else
    880                         break;
    881 
    882                 if (digit >= base)
    883                         break;
    884 
    885                 uint64_t prev = *result;
    886                 *result = (*result) * base + digit;
    887 
    888                 if (*result < prev) {
    889                         /* Overflow */
    890                         *endptr = (char *) str;
    891                         return EOVERFLOW;
    892                 }
    893 
    894                 str++;
    895         }
    896 
    897         if (str == startstr) {
    898                 /*
    899                  * No digits were decoded => first invalid character is
    900                  * the first character of the string.
    901                  */
    902                 str = nptr;
    903         }
    904 
    905         *endptr = (char *) str;
    906 
    907         if (str == nptr)
    908                 return EINVAL;
    909 
    910         return EOK;
    911 }
    912 
    913 /** Convert string to uint64_t.
    914  *
    915  * @param nptr   Pointer to string.
    916  * @param endptr If not NULL, pointer to the first invalid character
    917  *               is stored here.
    918  * @param base   Zero or number between 2 and 36 inclusive.
    919  * @param strict Do not allow any trailing characters.
    920  * @param result Result of the conversion.
    921  *
    922  * @return EOK if conversion was successful.
    923  *
    924  */
    925 errno_t str_uint64_t(const char *nptr, char **endptr, unsigned int base,
    926     bool strict, uint64_t *result)
    927 {
    928         assert(result != NULL);
    929 
    930         bool neg;
    931         char *lendptr;
    932         errno_t ret = str_uint(nptr, &lendptr, base, &neg, result);
    933 
    934         if (endptr != NULL)
    935                 *endptr = (char *) lendptr;
    936 
    937         if (ret != EOK)
    938                 return ret;
    939 
    940         /* Do not allow negative values */
    941         if (neg)
    942                 return EINVAL;
    943 
    944         /*
    945          * Check whether we are at the end of
    946          * the string in strict mode
    947          */
    948         if ((strict) && (*lendptr != 0))
    949                 return EINVAL;
    950 
    951         return EOK;
    952791}
    953792
Note: See TracChangeset for help on using the changeset viewer.