Changeset 86ffa27f in mainline for uspace/lib/c/generic


Ignore:
Timestamp:
2011-08-07T11:21:44Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
cc574511
Parents:
15f3c3f (diff), e8067c0 (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.
Message:

Merge mainline changes.

Location:
uspace/lib/c/generic
Files:
1 deleted
7 edited
1 moved

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/elf/elf_load.c

    r15f3c3f r86ffa27f  
    2929 */
    3030
    31 /** @addtogroup generic 
     31/** @addtogroup generic
    3232 * @{
    3333 */
     
    4949#include <assert.h>
    5050#include <as.h>
     51#include <elf/elf.h>
    5152#include <unistd.h>
    5253#include <fcntl.h>
     
    5556#include <entry_point.h>
    5657
    57 #include "elf.h"
    58 #include "elf_load.h"
     58#include <elf/elf_load.h>
    5959
    6060#define DPRINTF(...)
     
    7474static int load_segment(elf_ld_t *elf, elf_segment_header_t *entry);
    7575
    76 /** Read until the buffer is read in its entirety. */
    77 static int my_read(int fd, void *buf, size_t len)
    78 {
    79         int cnt = 0;
    80         do {
    81                 buf += cnt;
    82                 len -= cnt;
    83                 cnt = read(fd, buf, len);
    84         } while ((cnt > 0) && ((len - cnt) > 0));
    85 
    86         return cnt;
    87 }
    88 
    8976/** Load ELF binary from a file.
    9077 *
     
    160147        int i, rc;
    161148
    162         rc = my_read(elf->fd, header, sizeof(elf_header_t));
    163         if (rc < 0) {
     149        rc = read_all(elf->fd, header, sizeof(elf_header_t));
     150        if (rc != sizeof(elf_header_t)) {
    164151                DPRINTF("Read error.\n");
    165152                return EE_INVALID;
     
    222209                        + i * sizeof(elf_segment_header_t), SEEK_SET);
    223210
    224                 rc = my_read(elf->fd, &segment_hdr,
     211                rc = read_all(elf->fd, &segment_hdr,
    225212                    sizeof(elf_segment_header_t));
    226                 if (rc < 0) {
     213                if (rc != sizeof(elf_segment_header_t)) {
    227214                        DPRINTF("Read error.\n");
    228215                        return EE_INVALID;
     
    244231                    + i * sizeof(elf_section_header_t), SEEK_SET);
    245232
    246                 rc = my_read(elf->fd, &section_hdr,
     233                rc = read_all(elf->fd, &section_hdr,
    247234                    sizeof(elf_section_header_t));
    248                 if (rc < 0) {
     235                if (rc != sizeof(elf_section_header_t)) {
    249236                        DPRINTF("Read error.\n");
    250237                        return EE_INVALID;
     
    334321        uintptr_t seg_addr;
    335322        size_t mem_sz;
    336         int rc;
     323        ssize_t rc;
    337324
    338325        bias = elf->bias;
     
    412399                if (now > left) now = left;
    413400
    414                 rc = my_read(elf->fd, dp, now);
    415 
    416                 if (rc < 0) {
     401                rc = read_all(elf->fd, dp, now);
     402
     403                if (rc != (ssize_t) now) {
    417404                        DPRINTF("Read error.\n");
    418405                        return EE_INVALID;
  • uspace/lib/c/generic/io/console.c

    r15f3c3f r86ffa27f  
    7676bool console_kcon(void)
    7777{
    78 #if 0
    7978        return __SYSCALL0(SYS_DEBUG_ACTIVATE_CONSOLE);
    80 #endif
    81        
    82         return false;
    8379}
    8480
  • uspace/lib/c/generic/io/io.c

    r15f3c3f r86ffa27f  
    594594                }
    595595               
    596                 buf += now;
     596                data += now;
    597597                stream->buf_head += now;
    598598                buf_free -= now;
    599599                bytes_left -= now;
    600600                total_written += now;
     601                stream->buf_state = _bs_write;
    601602               
    602603                if (buf_free == 0) {
     
    606607                }
    607608        }
    608        
    609         if (total_written > 0)
    610                 stream->buf_state = _bs_write;
    611609
    612610        if (need_flush)
     
    714712off64_t ftell(FILE *stream)
    715713{
     714        _fflushbuf(stream);
    716715        return lseek(stream->fd, 0, SEEK_CUR);
    717716}
  • uspace/lib/c/generic/rtld/module.c

    r15f3c3f r86ffa27f  
    3535 */
    3636
     37#include <adt/list.h>
     38#include <elf/elf_load.h>
     39#include <fcntl.h>
     40#include <loader/pcb.h>
    3741#include <stdio.h>
    3842#include <stdlib.h>
    3943#include <unistd.h>
    40 #include <fcntl.h>
    41 #include <adt/list.h>
    42 #include <loader/pcb.h>
    4344
    4445#include <rtld/rtld.h>
     
    4748#include <rtld/rtld_arch.h>
    4849#include <rtld/module.h>
    49 #include <elf_load.h>
    5050
    5151/** (Eagerly) process all relocation tables in a module.
     
    9393module_t *module_find(const char *name)
    9494{
    95         link_t *head = &runtime_env->modules_head;
    96 
    97         link_t *cur;
    9895        module_t *m;
    9996        const char *p, *soname;
     
    110107
    111108        /* Traverse list of all modules. Not extremely fast, but simple */
    112         DPRINTF("head = %p\n", head);
    113         for (cur = head->next; cur != head; cur = cur->next) {
     109        list_foreach(runtime_env->modules, cur) {
    114110                DPRINTF("cur = %p\n", cur);
    115111                m = list_get_instance(cur, module_t, modules_link);
     
    177173
    178174        /* Insert into the list of loaded modules */
    179         list_append(&m->modules_link, &runtime_env->modules_head);
     175        list_append(&m->modules_link, &runtime_env->modules);
    180176
    181177        return m;
     
    249245void modules_process_relocs(module_t *start)
    250246{
    251         link_t *head = &runtime_env->modules_head;
    252 
    253         link_t *cur;
    254         module_t *m;
    255 
    256         for (cur = head->next; cur != head; cur = cur->next) {
     247        module_t *m;
     248
     249        list_foreach(runtime_env->modules, cur) {
    257250                m = list_get_instance(cur, module_t, modules_link);
    258251
     
    268261void modules_untag(void)
    269262{
    270         link_t *head = &runtime_env->modules_head;
    271 
    272         link_t *cur;
    273         module_t *m;
    274 
    275         for (cur = head->next; cur != head; cur = cur->next) {
     263        module_t *m;
     264
     265        list_foreach(runtime_env->modules, cur) {
    276266                m = list_get_instance(cur, module_t, modules_link);
    277267                m->bfs_tag = false;
  • uspace/lib/c/generic/rtld/rtld.c

    r15f3c3f r86ffa27f  
    4444{
    4545        runtime_env = &rt_env_static;
    46         list_initialize(&runtime_env->modules_head);
     46        list_initialize(&runtime_env->modules);
    4747        runtime_env->next_bias = 0x2000000;
    4848        runtime_env->program = NULL;
  • uspace/lib/c/generic/rtld/symbol.c

    r15f3c3f r86ffa27f  
    3838#include <stdlib.h>
    3939
     40#include <elf/elf.h>
    4041#include <rtld/rtld.h>
    4142#include <rtld/rtld_debug.h>
    4243#include <rtld/symbol.h>
    43 #include <elf.h>
    4444
    4545/*
     
    118118        module_t *m, *dm;
    119119        elf_symbol_t *sym, *s;
    120         link_t queue_head;
     120        list_t queue;
    121121        size_t i;
    122122
     
    132132
    133133        /* Insert root (the program) into the queue and tag it */
    134         list_initialize(&queue_head);
     134        list_initialize(&queue);
    135135        start->bfs_tag = true;
    136         list_append(&start->queue_link, &queue_head);
     136        list_append(&start->queue_link, &queue);
    137137
    138138        /* If the symbol is found, it will be stored in 'sym' */
     
    140140
    141141        /* While queue is not empty */
    142         while (!list_empty(&queue_head)) {
     142        while (!list_empty(&queue)) {
    143143                /* Pop first element from the queue */
    144                 m = list_get_instance(queue_head.next, module_t, queue_link);
     144                m = list_get_instance(list_first(&queue), module_t, queue_link);
    145145                list_remove(&m->queue_link);
    146146
     
    162162                        if (dm->bfs_tag == false) {
    163163                                dm->bfs_tag = true;
    164                                 list_append(&dm->queue_link, &queue_head);
     164                                list_append(&dm->queue_link, &queue);
    165165                        }
    166166                }
     
    168168
    169169        /* Empty the queue so that we leave it in a clean state */
    170         while (!list_empty(&queue_head))
    171                 list_remove(queue_head.next);
     170        while (!list_empty(&queue))
     171                list_remove(list_first(&queue));
    172172
    173173        if (!sym) {
  • uspace/lib/c/generic/str.c

    r15f3c3f r86ffa27f  
    544544       
    545545        str_cpy(dest + dstr_size, size - dstr_size, src);
     546}
     547
     548/** Convert space-padded ASCII to string.
     549 *
     550 * Common legacy text encoding in hardware is 7-bit ASCII fitted into
     551 * a fixed-with byte buffer (bit 7 always zero), right-padded with spaces
     552 * (ASCII 0x20). Convert space-padded ascii to string representation.
     553 *
     554 * If the text does not fit into the destination buffer, the function converts
     555 * as many characters as possible and returns EOVERFLOW.
     556 *
     557 * If the text contains non-ASCII bytes (with bit 7 set), the whole string is
     558 * converted anyway and invalid characters are replaced with question marks
     559 * (U_SPECIAL) and the function returns EIO.
     560 *
     561 * Regardless of return value upon return @a dest will always be well-formed.
     562 *
     563 * @param dest          Destination buffer
     564 * @param size          Size of destination buffer
     565 * @param src           Space-padded ASCII.
     566 * @param n             Size of the source buffer in bytes.
     567 *
     568 * @return              EOK on success, EOVERFLOW if the text does not fit
     569 *                      destination buffer, EIO if the text contains
     570 *                      non-ASCII bytes.
     571 */
     572int spascii_to_str(char *dest, size_t size, const uint8_t *src, size_t n)
     573{
     574        size_t sidx;
     575        size_t didx;
     576        size_t dlast;
     577        uint8_t byte;
     578        int rc;
     579        int result;
     580
     581        /* There must be space for a null terminator in the buffer. */
     582        assert(size > 0);
     583        result = EOK;
     584
     585        didx = 0;
     586        dlast = 0;
     587        for (sidx = 0; sidx < n; ++sidx) {
     588                byte = src[sidx];
     589                if (!ascii_check(byte)) {
     590                        byte = U_SPECIAL;
     591                        result = EIO;
     592                }
     593
     594                rc = chr_encode(byte, dest, &didx, size - 1);
     595                if (rc != EOK) {
     596                        assert(rc == EOVERFLOW);
     597                        dest[didx] = '\0';
     598                        return rc;
     599                }
     600
     601                /* Remember dest index after last non-empty character */
     602                if (byte != 0x20)
     603                        dlast = didx;
     604        }
     605
     606        /* Terminate string after last non-empty character */
     607        dest[dlast] = '\0';
     608        return result;
    546609}
    547610
  • uspace/lib/c/generic/vfs/vfs.c

    r15f3c3f r86ffa27f  
    417417}
    418418
     419/** Read entire buffer.
     420 *
     421 * In face of short reads this function continues reading until either
     422 * the entire buffer is read or no more data is available (at end of file).
     423 *
     424 * @param fildes        File descriptor
     425 * @param buf           Buffer, @a nbytes bytes long
     426 * @param nbytes        Number of bytes to read
     427 *
     428 * @return              On success, positive number of bytes read.
     429 *                      On failure, negative error code from read().
     430 */
     431ssize_t read_all(int fildes, void *buf, size_t nbyte)
     432{
     433        ssize_t cnt = 0;
     434        size_t nread = 0;
     435        uint8_t *bp = (uint8_t *) buf;
     436
     437        do {
     438                bp += cnt;
     439                nread += cnt;
     440                cnt = read(fildes, bp, nbyte - nread);
     441        } while (cnt > 0 && (nbyte - nread - cnt) > 0);
     442
     443        if (cnt < 0)
     444                return cnt;
     445
     446        return nread + cnt;
     447}
     448
     449/** Write entire buffer.
     450 *
     451 * This function fails if it cannot write exactly @a len bytes to the file.
     452 *
     453 * @param fildes        File descriptor
     454 * @param buf           Data, @a nbytes bytes long
     455 * @param nbytes        Number of bytes to write
     456 *
     457 * @return              EOK on error, return value from write() if writing
     458 *                      failed.
     459 */
     460ssize_t write_all(int fildes, const void *buf, size_t nbyte)
     461{
     462        ssize_t cnt = 0;
     463        ssize_t nwritten = 0;
     464        const uint8_t *bp = (uint8_t *) buf;
     465
     466        do {
     467                bp += cnt;
     468                nwritten += cnt;
     469                cnt = write(fildes, bp, nbyte - nwritten);
     470        } while (cnt > 0 && ((ssize_t )nbyte - nwritten - cnt) > 0);
     471
     472        if (cnt < 0)
     473                return cnt;
     474
     475        if ((ssize_t)nbyte - nwritten - cnt > 0)
     476                return EIO;
     477
     478        return nbyte;
     479}
     480
    419481int fsync(int fildes)
    420482{
Note: See TracChangeset for help on using the changeset viewer.