Changeset 04803bf in mainline for uspace/srv/loader


Ignore:
Timestamp:
2011-03-21T22:00:17Z (15 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
143932e3
Parents:
b50b5af2 (diff), 7308e84 (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 (needs fixes).

Location:
uspace/srv/loader
Files:
3 added
22 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/loader/Makefile

    rb50b5af2 r04803bf  
    11#
    22# Copyright (c) 2005 Martin Decky
    3 # Copyright (c) 2008 Jiri Svoboda
     3# Copyright (c) 2007 Jakub Jermar
    44# All rights reserved.
    55#
     
    2828#
    2929
    30 ## Setup toolchain
    31 #
     30USPACE_PREFIX = ../..
     31ROOT_PATH = $(USPACE_PREFIX)/..
    3232
    33 LIBC_PREFIX = ../../lib/libc
    34 SOFTINT_PREFIX = ../../lib/softint
     33COMMON_MAKEFILE = $(ROOT_PATH)/Makefile.common
     34CONFIG_MAKEFILE = $(ROOT_PATH)/Makefile.config
    3535
    36 include $(LIBC_PREFIX)/Makefile.toolchain
    37 include arch/$(UARCH)/Makefile.inc
     36-include $(COMMON_MAKEFILE)
     37-include $(CONFIG_MAKEFILE)
     38-include arch/$(UARCH)/Makefile.inc
    3839
    39 CFLAGS += -Iinclude
     40LINKER_SCRIPT = arch/$(UARCH)/_link.ld
     41EXTRA_CLEAN = $(LINKER_SCRIPT)
    4042
    41 LIBS = $(LIBC_PREFIX)/libc.a $(SOFTINT_PREFIX)/libsoftint.a
     43EXTRA_CFLAGS = -Iinclude
    4244
    43 ## Sources
    44 #
     45BINARY = loader
    4546
    46 OUTPUT = loader
    4747GENERIC_SOURCES = \
    4848        main.c \
     
    5050        interp.s
    5151
    52 SOURCES := $(GENERIC_SOURCES) $(ARCH_SOURCES)
    53 OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
     52SOURCES = \
     53        $(GENERIC_SOURCES) \
     54        $(ARCH_SOURCES)
    5455
    55 .PHONY: all clean depend disasm
     56include $(USPACE_PREFIX)/Makefile.common
    5657
    57 all: $(OUTPUT) disasm
    58 
    59 -include Makefile.depend
    60 
    61 clean:
    62         -rm -f $(OUTPUT) $(OBJECTS) $(OUTPUT).map $(OUTPUT).disasm arch/$(UARCH)/_link.ld Makefile.depend
    63 
    64 depend:
    65         $(CC) $(DEFS) $(CFLAGS) -M $(SOURCES) > Makefile.depend
    66 
    67 $(OUTPUT): $(OBJECTS) $(LIBS) arch/$(UARCH)/_link.ld
    68         $(LD) -T arch/$(UARCH)/_link.ld $(LFLAGS) $(OBJECTS) $(LIBS) -o $@ -Map $(OUTPUT).map
    69 
    70 disasm:
    71         $(OBJDUMP) -d $(OUTPUT) >$(OUTPUT).disasm
    72 
    73 arch/$(UARCH)/_link.ld: arch/$(UARCH)/_link.ld.in
    74         $(CC) $(DEFS) $(CFLAGS) -DLIBC_PREFIX=$(LIBC_PREFIX) -E -x c $< | grep -v "^\#" > $@
    75 
    76 %.o: %.S
    77         $(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@
    78 
    79 %.o: %.s
    80         $(AS) $(AFLAGS) $< -o $@
    81 
    82 %.o: %.c
    83         $(CC) $(DEFS) $(CFLAGS) -c $< -o $@
     58$(LINKER_SCRIPT): $(LINKER_SCRIPT).in
     59        $(GCC) $(DEFS) $(CFLAGS) -DLIBC_PREFIX=$(LIBC_PREFIX) -E -x c $< | grep -v "^\#" > $@
  • uspace/srv/loader/arch/amd64/Makefile.inc

    rb50b5af2 r04803bf  
    2727#
    2828
    29 CFLAGS += -D__64_BITS__
    30 ARCH_SOURCES := arch/$(UARCH)/amd64.s
     29ARCH_SOURCES = arch/$(UARCH)/amd64.s
  • uspace/srv/loader/arch/amd64/_link.ld.in

    rb50b5af2 r04803bf  
     1/*
     2 * The difference from _link.ld.in for regular statically-linked apps
     3 * is the base address and the special interp section.
     4 */
     5
    16STARTUP(LIBC_PREFIX/arch/UARCH/src/entry.o)
    27ENTRY(__entry)
     
    611        text PT_LOAD FLAGS(5);
    712        data PT_LOAD FLAGS(6);
     13        debug PT_NOTE;
    814}
    915
     
    1117        .interp : {
    1218                *(.interp);
    13         } : interp
    14 
    15         /* . = 0x0000700000001000;*/
     19        } :interp
     20       
     21        /* . = 0x0000700000001000; */
    1622        . = 0x70001000;
    1723       
     
    1925                *(.init);
    2026        } :text
     27       
    2128        .text : {
    2229                *(.text);
     
    2734                *(.data);
    2835        } :data
     36       
    2937        .tdata : {
    3038                _tdata_start = .;
     
    3240                _tdata_end = .;
    3341        } :data
     42       
    3443        .tbss : {
    3544                _tbss_start = .;
     
    3746                _tbss_end = .;
    3847        } :data
     48       
    3949        _tls_alignment = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss));
     50       
    4051        .bss : {
    4152                *(COMMON);
    4253                *(.bss);
    4354        } :data
    44 
    45         . = ALIGN(0x1000);
    46         _heap = .;
     55       
     56#ifdef CONFIG_LINE_DEBUG
     57        .comment 0 : { *(.comment); } :debug
     58        .debug_abbrev 0 : { *(.debug_abbrev); } :debug
     59        .debug_aranges 0 : { *(.debug_aranges); } :debug
     60        .debug_info 0 : { *(.debug_info); } :debug
     61        .debug_line 0 : { *(.debug_line); } :debug
     62        .debug_loc 0 : { *(.debug_loc); } :debug
     63        .debug_pubnames 0 : { *(.debug_pubnames); } :debug
     64        .debug_pubtypes 0 : { *(.debug_pubtypes); } :debug
     65        .debug_ranges 0 : { *(.debug_ranges); } :debug
     66        .debug_str 0 : { *(.debug_str); } :debug
     67#endif
    4768       
    4869        /DISCARD/ : {
    4970                *(*);
    5071        }
    51 
    5272}
  • uspace/srv/loader/arch/arm32/Makefile.inc

    rb50b5af2 r04803bf  
    2727#
    2828
    29 CFLAGS += -D__32_BITS__
    30 ARCH_SOURCES := arch/$(UARCH)/arm32.s
     29ARCH_SOURCES = arch/$(UARCH)/arm32.s
  • uspace/srv/loader/arch/arm32/_link.ld.in

    rb50b5af2 r04803bf  
    33 * is the base address.
    44 */
     5
    56STARTUP(LIBC_PREFIX/arch/UARCH/src/entry.o)
    67ENTRY(__entry)
     
    1617                *(.interp);
    1718        } : interp
    18 
     19       
    1920        . = 0x70001000;
    20 
     21       
    2122        .init ALIGN(0x1000): SUBALIGN(0x1000) {
    2223                *(.init);
    23         } : text
     24        } :text
     25       
    2426        .text : {
    2527                *(.text);
    26         *(.rodata*);
     28                *(.rodata*);
    2729        } :text
    2830       
     
    3234                *(.sdata);
    3335        } :data
     36       
    3437        .tdata : {
    3538                _tdata_start = .;
     
    3740                _tdata_end = .;
    3841        } :data
     42       
    3943        .tbss : {
    4044                _tbss_start = .;
     
    4246                _tbss_end = .;
    4347        } :data
     48       
    4449        _tls_alignment = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss));
     50       
    4551        .bss : {
    4652                *(.sbss);
    4753                *(.scommon);
    48         *(COMMON);
    49         *(.bss);
     54                *(COMMON);
     55                *(.bss);
    5056        } :data
    51        
    52         . = ALIGN(0x1000);
    53         _heap = .;
    5457       
    5558        /DISCARD/ : {
    5659                *(*);
    5760        }
    58 
    5961}
  • uspace/srv/loader/arch/arm32/arm32.s

    rb50b5af2 r04803bf  
    3636# Jump to a program entry point
    3737program_run:
     38        # load ras_page address to r2
     39        ldr r2, =ras_page
     40        ldr r2, [r2]
    3841        # pcb is passed to the entry point in r1 (where it already is)
    3942        mov r15, r0
  • uspace/srv/loader/arch/ia32/Makefile.inc

    rb50b5af2 r04803bf  
    2727#
    2828
    29 CFLAGS += -D__32_BITS__
    30 ARCH_SOURCES := arch/$(UARCH)/ia32.s
     29ARCH_SOURCES = arch/$(UARCH)/ia32.s
  • uspace/srv/loader/arch/ia32/_link.ld.in

    rb50b5af2 r04803bf  
    33 * is the base address and the special interp section.
    44 */
     5
    56STARTUP(LIBC_PREFIX/arch/UARCH/src/entry.o)
    67ENTRY(__entry)
     
    89PHDRS {
    910        interp PT_INTERP;
    10         text PT_LOAD FILEHDR PHDRS FLAGS(5);
     11        text PT_LOAD FILEHDR PHDRS FLAGS(5);
    1112        data PT_LOAD FLAGS(6);
     13        debug PT_NOTE;
    1214}
    1315
     
    1618                *(.interp);
    1719        } :interp
    18 
     20       
    1921        . = 0x70001000;
    20 
     22       
    2123        .init ALIGN(0x1000) : SUBALIGN(0x1000) {
    2224                *(.init);
    2325        } :text
     26       
    2427        .text : {
    2528                *(.text);
    26                 *(.rodata*);
     29                *(.rodata*);
    2730        } :text
    2831       
     
    3033                *(.data);
    3134        } :data
     35       
    3236        .tdata : {
    3337                _tdata_start = .;
    3438                *(.tdata);
     39                *(.gnu.linkonce.tb.*);
    3540                _tdata_end = .;
    3641        } :data
     42       
    3743        .tbss : {
    3844                _tbss_start = .;
     
    4046                _tbss_end = .;
    4147        } :data
     48       
    4249        _tls_alignment = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss));
     50       
    4351        .bss : {
    44                 *(COMMON);
    45                 *(.bss);
     52                *(COMMON);
     53                *(.bss);
    4654        } :data
    4755       
    48         . = ALIGN(0x1000);
    49         _heap = .;
     56#ifdef CONFIG_LINE_DEBUG
     57        .comment 0 : { *(.comment); } :debug
     58        .debug_abbrev 0 : { *(.debug_abbrev); } :debug
     59        .debug_aranges 0 : { *(.debug_aranges); } :debug
     60        .debug_info 0 : { *(.debug_info); } :debug
     61        .debug_line 0 : { *(.debug_line); } :debug
     62        .debug_loc 0 : { *(.debug_loc); } :debug
     63        .debug_pubnames 0 : { *(.debug_pubnames); } :debug
     64        .debug_pubtypes 0 : { *(.debug_pubtypes); } :debug
     65        .debug_ranges 0 : { *(.debug_ranges); } :debug
     66        .debug_str 0 : { *(.debug_str); } :debug
     67#endif
    5068       
    5169        /DISCARD/ : {
    5270                *(*);
    5371        }
    54 
    5572}
  • uspace/srv/loader/arch/ia32/ia32.s

    rb50b5af2 r04803bf  
    4040        movl 0x8(%ebp), %eax
    4141
    42         # %ebx := pcb
    43         # pcb is passed to the entry point int %ebx
    44         mov 0xc(%ebp), %ebx
     42        # %edi := pcb
     43        # pcb is passed to the entry point in %edi
     44        mov 0xc(%ebp), %edi
    4545
    4646        # Save a tiny bit of stack space
  • uspace/srv/loader/arch/ia64/Makefile.inc

    rb50b5af2 r04803bf  
    2727#
    2828
    29 CFLAGS += -D__64_BITS__
    30 ARCH_SOURCES := arch/$(UARCH)/ia64.s
     29ARCH_SOURCES = arch/$(UARCH)/ia64.s
    3130AFLAGS += -xexplicit
  • uspace/srv/loader/arch/ia64/_link.ld.in

    rb50b5af2 r04803bf  
    1212                *(.interp);
    1313        } :interp
    14 
     14       
    1515        /* On Itanium code sections must be aligned to 16 bytes. */
    1616        . = ALIGN(0x800000000 + SIZEOF_HEADERS, 16);
    17 
     17       
    1818        .init : {
    1919                *(.init);
    20         } : text
     20        } :text
     21       
    2122        .text : {
    2223                *(.text);
    2324                *(.rodata*);
    2425        } :text
    25 
     26       
    2627        . = . + 0x4000;
    27 
     28       
    2829        .got : {
    2930                _gp = .;
    3031                *(.got*);
    31         } :data
     32        } :data
     33       
    3234        .data : {
    3335                *(.opd);
     
    3537                *(.sdata);
    3638        } :data
     39       
    3740        .tdata : {
    3841                _tdata_start = .;
     
    4043                _tdata_end = .;
    4144        } :data
     45       
    4246        .tbss : {
    4347                _tbss_start = .;
     
    4549                _tbss_end = .;
    4650        } :data
     51       
    4752        _tls_alignment = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss));
     53       
    4854        .bss : {
    4955                *(.sbss);
     
    5258                *(.bss);
    5359        } :data
    54 
    55         . = ALIGN(0x4000);
    56         _heap = .;
    57  
     60       
    5861        /DISCARD/ : {
    5962                *(*);
    60         }
     63        }
    6164}
  • uspace/srv/loader/arch/mips32/Makefile.inc

    rb50b5af2 r04803bf  
    2727#
    2828
    29 CFLAGS += -D__32_BITS__
    30 ARCH_SOURCES := arch/$(UARCH)/mips32.s
     29ARCH_SOURCES = arch/$(UARCH)/mips32.s
  • uspace/srv/loader/arch/mips32/_link.ld.in

    rb50b5af2 r04803bf  
    33 * is the base address.
    44 */
     5
    56STARTUP(LIBC_PREFIX/arch/UARCH/src/entry.o)
    67ENTRY(__entry)
     
    1617                *(.interp);
    1718        } :interp
    18 
     19       
    1920        . = 0x70004000;
    2021       
     
    2223                *(.init);
    2324        } :text
     25       
    2426        .text : {
    25                 *(.text);
     27                *(.text);
    2628                *(.rodata*);
    2729        } :text
    28 
     30       
     31        . = . + 0x4000;
     32       
    2933        .data : {
    3034                *(.data);
    3135                *(.data.rel*);
    3236        } :data
    33 
     37       
    3438        .got : {
    3539                _gp = .;
    3640                *(.got);
    3741        } :data
    38 
     42       
    3943        .tdata : {
    4044                _tdata_start = .;
     
    4246                _tdata_end = .;
    4347        } :data
     48       
    4449        .tbss : {
    4550                _tbss_start = .;
     
    4752                _tbss_end = .;
    4853        } :data
     54       
    4955        _tls_alignment = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss));
    50 
     56       
    5157        .sbss : {
    5258                *(.scommon);
    5359                *(.sbss);
    54         }       
     60        }
     61       
    5562        .bss : {
    5663                *(.bss);
    5764                *(COMMON);
    5865        } :data
    59 
    60         . = ALIGN(0x4000);
    61         _heap = .;
    62 
     66       
    6367        /DISCARD/ : {
    6468                *(*);
  • uspace/srv/loader/arch/ppc32/Makefile.inc

    rb50b5af2 r04803bf  
    2727#
    2828
    29 CFLAGS += -D__32_BITS__
    30 ARCH_SOURCES := arch/$(UARCH)/ppc32.s
     29ARCH_SOURCES = arch/$(UARCH)/ppc32.s
  • uspace/srv/loader/arch/ppc32/_link.ld.in

    rb50b5af2 r04803bf  
    33 * is the base address.
    44 */
     5
    56STARTUP(LIBC_PREFIX/arch/UARCH/src/entry.o)
    67ENTRY(__entry)
     
    1617                *(.interp);
    1718        } :interp
    18 
     19       
    1920        . = 0x70001000;
    20 
     21       
    2122        .init ALIGN(0x1000) : SUBALIGN(0x1000) {
    2223                *(.init);
    2324        } :text
     25       
    2426        .text : {
    2527                *(.text);
     
    3133                *(.sdata);
    3234        } :data
     35       
    3336        .tdata : {
    3437                _tdata_start = .;
     
    3639                _tdata_end = .;
    3740        } :data
     41       
    3842        .tbss : {
    3943                _tbss_start = .;
     
    4145                _tbss_end = .;
    4246        } :data
     47       
    4348        _tls_alignment = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss));
     49       
    4450        .bss : {
    4551                *(.sbss);
     
    4753                *(.bss);
    4854        } :data
    49 
    50         . = ALIGN(0x1000);
    51         _heap = .;
    5255       
    5356        /DISCARD/ : {
    5457                *(*);
    5558        }
    56 
    5759}
  • uspace/srv/loader/arch/sparc64/Makefile.inc

    rb50b5af2 r04803bf  
    2727#
    2828
    29 CFLAGS += -D__64_BITS__
    30 ARCH_SOURCES := arch/$(UARCH)/sparc64.s
     29ARCH_SOURCES = arch/$(UARCH)/sparc64.s
  • uspace/srv/loader/arch/sparc64/_link.ld.in

    rb50b5af2 r04803bf  
    1212                *(.interp);
    1313        } :interp
    14 
     14       
    1515        . = 0x70004000 + SIZEOF_HEADERS;
    16 
     16       
    1717        .init : {
    1818                *(.init);
    1919        } :text
     20       
    2021        .text : {
    2122                *(.text);
    2223                *(.rodata*);
    2324        } :text
    24 
     25       
    2526        . = . + 0x4000;
    26 
     27       
    2728        .got : {
    2829                 _gp = .;
    2930                 *(.got*);
    3031        } :data
     32       
    3133        .data : {
    3234                *(.data);
    3335                *(.sdata);
    3436        } :data
     37       
    3538        .tdata : {
    3639                _tdata_start = .;
     
    3841                _tdata_end = .;
    3942        } :data
     43       
    4044        .tbss : {
    4145                _tbss_start = .;
     
    4347                _tbss_end = .;
    4448        } :data
     49       
    4550        _tls_alignment = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss));
     51       
    4652        .bss : {
    4753                *(.sbss);
     
    4955                *(.bss);
    5056        } :data
    51 
    52         . = ALIGN(0x4000);
    53         _heap = .;
    5457       
    5558        /DISCARD/ : {
    5659                *(*);
    5760        }
    58 
    5961}
  • uspace/srv/loader/elf_load.c

    rb50b5af2 r04803bf  
    6060#define DPRINTF(...)
    6161
    62 static char *error_codes[] = {
     62static const char *error_codes[] = {
    6363        "no error",
    6464        "invalid image",
     
    9595 * pointed to by @a info.
    9696 *
    97  * @param file_name     Path to the ELF file.
    98  * @param so_bias       Bias to use if the file is a shared object.
    99  * @param info          Pointer to a structure for storing information
    100  *                      extracted from the binary.
     97 * @param file_name Path to the ELF file.
     98 * @param so_bias   Bias to use if the file is a shared object.
     99 * @param info      Pointer to a structure for storing information
     100 *                  extracted from the binary.
    101101 *
    102102 * @return EOK on success or negative error code.
    103  */
    104 int elf_load_file(char *file_name, size_t so_bias, eld_flags_t flags,
     103 *
     104 */
     105int elf_load_file(const char *file_name, size_t so_bias, eld_flags_t flags,
    105106    elf_info_t *info)
    106107{
     
    109110        int fd;
    110111        int rc;
    111 
     112       
    112113        fd = open(file_name, O_RDONLY);
    113114        if (fd < 0) {
     
    269270 * @return NULL terminated description of error.
    270271 */
    271 char *elf_error(unsigned int rc)
     272const char *elf_error(unsigned int rc)
    272273{
    273274        assert(rc < sizeof(error_codes) / sizeof(char *));
     
    287288        case PT_NULL:
    288289        case PT_PHDR:
     290        case PT_NOTE:
    289291                break;
    290292        case PT_LOAD:
     
    306308                break;
    307309        case PT_SHLIB:
    308         case PT_NOTE:
    309310//      case PT_LOPROC:
    310311//      case PT_HIPROC:
     
    340341        seg_ptr = (void *) seg_addr;
    341342
    342         DPRINTF("Load segment at addr 0x%x, size 0x%x\n", seg_addr,
    343                 entry->p_memsz);       
     343        DPRINTF("Load segment at addr %p, size 0x%x\n", (void *) seg_addr,
     344                entry->p_memsz);
    344345
    345346        if (entry->p_align > 1) {
     
    368369        mem_sz = entry->p_memsz + (entry->p_vaddr - base);
    369370
    370         DPRINTF("Map to seg_addr=0x%x-0x%x.\n", seg_addr,
    371         entry->p_vaddr + bias + ALIGN_UP(entry->p_memsz, PAGE_SIZE));
     371        DPRINTF("Map to seg_addr=%p-%p.\n", (void *) seg_addr,
     372            (void *) (entry->p_vaddr + bias +
     373            ALIGN_UP(entry->p_memsz, PAGE_SIZE)));
    372374
    373375        /*
     
    383385        }
    384386
    385         DPRINTF("as_area_create(0x%lx, 0x%x, %d) -> 0x%lx\n",
    386                 base + bias, mem_sz, flags, (uintptr_t)a);
     387        DPRINTF("as_area_create(%p, %#zx, %d) -> %p\n",
     388            (void *) (base + bias), mem_sz, flags, (void *) a);
    387389
    388390        /*
     
    464466                break;
    465467        case SHT_DYNAMIC:
     468                /* Record pointer to dynamic section into info structure */
     469                elf->info->dynamic =
     470                    (void *)((uint8_t *)entry->sh_addr + elf->bias);
     471                DPRINTF("Dynamic section found at %p.\n",
     472                    (void *) elf->info->dynamic);
    466473                break;
    467474        default:
  • uspace/srv/loader/include/arch.h

    rb50b5af2 r04803bf  
    3737#define LOADER_ARCH_H_
    3838
    39 void program_run(void *entry_point, void *pcb);
     39extern void program_run(void *entry_point, void *pcb);
    4040
    4141#endif
  • uspace/srv/loader/include/elf.h

    rb50b5af2 r04803bf  
    337337#endif
    338338
    339 extern char *elf_error(unsigned int rc);
     339extern const char *elf_error(unsigned int rc);
    340340
    341341#endif
  • uspace/srv/loader/include/elf_load.h

    rb50b5af2 r04803bf  
    2727 */
    2828
    29 /** @addtogroup generic 
     29/** @addtogroup generic
    3030 * @{
    3131 */
     
    5656
    5757        /** ELF interpreter name or NULL if statically-linked */
    58         char *interp;
     58        const char *interp;
    5959
    6060        /** Pointer to the dynamic section */
     
    8282} elf_ld_t;
    8383
    84 int elf_load_file(char *file_name, size_t so_bias, eld_flags_t flags,
     84int elf_load_file(const char *file_name, size_t so_bias, eld_flags_t flags,
    8585    elf_info_t *info);
    8686void elf_create_pcb(elf_info_t *info, pcb_t *pcb);
  • uspace/srv/loader/main.c

    rb50b5af2 r04803bf  
    5050#include <fcntl.h>
    5151#include <sys/types.h>
    52 #include <ipc/ipc.h>
    5352#include <ipc/services.h>
    5453#include <ipc/loader.h>
     
    5857#include <errno.h>
    5958#include <async.h>
    60 #include <string.h>
     59#include <str.h>
    6160#include <as.h>
    6261
     
    7372/** The Program control block */
    7473static pcb_t pcb;
     74
     75/** Current working directory */
     76static char *cwd = NULL;
    7577
    7678/** Number of arguments */
     
    9496
    9597/** Used to limit number of connections to one. */
    96 static bool connected;
     98static bool connected = false;
    9799
    98100static void ldr_get_taskid(ipc_callid_t rid, ipc_call_t *request)
     
    104106        task_id = task_get_id();
    105107       
    106         if (!ipc_data_read_receive(&callid, &len)) {
    107                 ipc_answer_0(callid, EINVAL);
    108                 ipc_answer_0(rid, EINVAL);
     108        if (!async_data_read_receive(&callid, &len)) {
     109                async_answer_0(callid, EINVAL);
     110                async_answer_0(rid, EINVAL);
    109111                return;
    110112        }
     
    113115                len = sizeof(task_id);
    114116       
    115         ipc_data_read_finalize(callid, &task_id, len);
    116         ipc_answer_0(rid, EOK);
    117 }
    118 
     117        async_data_read_finalize(callid, &task_id, len);
     118        async_answer_0(rid, EOK);
     119}
     120
     121/** Receive a call setting the current working directory.
     122 *
     123 * @param rid
     124 * @param request
     125 */
     126static void ldr_set_cwd(ipc_callid_t rid, ipc_call_t *request)
     127{
     128        char *buf;
     129        int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, NULL);
     130       
     131        if (rc == EOK) {
     132                if (cwd != NULL)
     133                        free(cwd);
     134               
     135                cwd = buf;
     136        }
     137       
     138        async_answer_0(rid, rc);
     139}
    119140
    120141/** Receive a call setting pathname of the program to execute.
     
    125146static void ldr_set_pathname(ipc_callid_t rid, ipc_call_t *request)
    126147{
    127         ipc_callid_t callid;
    128         size_t len;
    129         char *name_buf;
    130        
    131         if (!ipc_data_write_receive(&callid, &len)) {
    132                 ipc_answer_0(callid, EINVAL);
    133                 ipc_answer_0(rid, EINVAL);
    134                 return;
    135         }
    136        
    137         name_buf = malloc(len + 1);
    138         if (!name_buf) {
    139                 ipc_answer_0(callid, ENOMEM);
    140                 ipc_answer_0(rid, ENOMEM);
    141                 return;
    142         }
    143        
    144         ipc_data_write_finalize(callid, name_buf, len);
    145         ipc_answer_0(rid, EOK);
    146        
    147         if (pathname != NULL) {
    148                 free(pathname);
    149                 pathname = NULL;
    150         }
    151        
    152         name_buf[len] = '\0';
    153         pathname = name_buf;
     148        char *buf;
     149        int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, NULL);
     150       
     151        if (rc == EOK) {
     152                if (pathname != NULL)
     153                        free(pathname);
     154               
     155                pathname = buf;
     156        }
     157       
     158        async_answer_0(rid, rc);
    154159}
    155160
     
    161166static void ldr_set_args(ipc_callid_t rid, ipc_call_t *request)
    162167{
    163         ipc_callid_t callid;
    164         size_t buf_size, arg_size;
    165         char *p;
    166         int n;
    167        
    168         if (!ipc_data_write_receive(&callid, &buf_size)) {
    169                 ipc_answer_0(callid, EINVAL);
    170                 ipc_answer_0(rid, EINVAL);
    171                 return;
    172         }
    173        
    174         if (arg_buf != NULL) {
    175                 free(arg_buf);
    176                 arg_buf = NULL;
    177         }
    178        
    179         if (argv != NULL) {
    180                 free(argv);
    181                 argv = NULL;
    182         }
    183        
    184         arg_buf = malloc(buf_size + 1);
    185         if (!arg_buf) {
    186                 ipc_answer_0(callid, ENOMEM);
    187                 ipc_answer_0(rid, ENOMEM);
    188                 return;
    189         }
    190        
    191         ipc_data_write_finalize(callid, arg_buf, buf_size);
    192        
    193         arg_buf[buf_size] = '\0';
    194        
    195         /*
    196          * Count number of arguments
    197          */
    198         p = arg_buf;
    199         n = 0;
    200         while (p < arg_buf + buf_size) {
    201                 arg_size = str_size(p);
    202                 p = p + arg_size + 1;
    203                 ++n;
    204         }
    205        
    206         /* Allocate argv */
    207         argv = malloc((n + 1) * sizeof(char *));
    208        
    209         if (argv == NULL) {
    210                 free(arg_buf);
    211                 ipc_answer_0(rid, ENOMEM);
    212                 return;
    213         }
    214 
    215         /*
    216          * Fill argv with argument pointers
    217          */
    218         p = arg_buf;
    219         n = 0;
    220         while (p < arg_buf + buf_size) {
    221                 argv[n] = p;
    222                
    223                 arg_size = str_size(p);
    224                 p = p + arg_size + 1;
    225                 ++n;
    226         }
    227        
    228         argc = n;
    229         argv[n] = NULL;
    230 
    231         ipc_answer_0(rid, EOK);
     168        char *buf;
     169        size_t buf_size;
     170        int rc = async_data_write_accept((void **) &buf, true, 0, 0, 0, &buf_size);
     171       
     172        if (rc == EOK) {
     173                /*
     174                 * Count number of arguments
     175                 */
     176                char *cur = buf;
     177                int count = 0;
     178               
     179                while (cur < buf + buf_size) {
     180                        size_t arg_size = str_size(cur);
     181                        cur += arg_size + 1;
     182                        count++;
     183                }
     184               
     185                /*
     186                 * Allocate new argv
     187                 */
     188                char **_argv = (char **) malloc((count + 1) * sizeof(char *));
     189                if (_argv == NULL) {
     190                        free(buf);
     191                        async_answer_0(rid, ENOMEM);
     192                        return;
     193                }
     194               
     195                /*
     196                 * Fill the new argv with argument pointers
     197                 */
     198                cur = buf;
     199                count = 0;
     200                while (cur < buf + buf_size) {
     201                        _argv[count] = cur;
     202                       
     203                        size_t arg_size = str_size(cur);
     204                        cur += arg_size + 1;
     205                        count++;
     206                }
     207                _argv[count] = NULL;
     208               
     209                /*
     210                 * Copy temporary data to global variables
     211                 */
     212                if (arg_buf != NULL)
     213                        free(arg_buf);
     214               
     215                if (argv != NULL)
     216                        free(argv);
     217               
     218                argc = count;
     219                arg_buf = buf;
     220                argv = _argv;
     221        }
     222       
     223        async_answer_0(rid, rc);
    232224}
    233225
     
    239231static void ldr_set_files(ipc_callid_t rid, ipc_call_t *request)
    240232{
    241         ipc_callid_t callid;
     233        fdi_node_t *buf;
    242234        size_t buf_size;
    243         if (!ipc_data_write_receive(&callid, &buf_size)) {
    244                 ipc_answer_0(callid, EINVAL);
    245                 ipc_answer_0(rid, EINVAL);
    246                 return;
    247         }
    248        
    249         if ((buf_size % sizeof(fdi_node_t)) != 0) {
    250                 ipc_answer_0(callid, EINVAL);
    251                 ipc_answer_0(rid, EINVAL);
    252                 return;
    253         }
    254        
    255         if (fil_buf != NULL) {
    256                 free(fil_buf);
    257                 fil_buf = NULL;
    258         }
    259        
    260         if (filv != NULL) {
    261                 free(filv);
    262                 filv = NULL;
    263         }
    264        
    265         fil_buf = malloc(buf_size);
    266         if (!fil_buf) {
    267                 ipc_answer_0(callid, ENOMEM);
    268                 ipc_answer_0(rid, ENOMEM);
    269                 return;
    270         }
    271        
    272         ipc_data_write_finalize(callid, fil_buf, buf_size);
    273        
    274         int count = buf_size / sizeof(fdi_node_t);
    275        
    276         /* Allocate filvv */
    277         filv = malloc((count + 1) * sizeof(fdi_node_t *));
    278        
    279         if (filv == NULL) {
    280                 free(fil_buf);
    281                 ipc_answer_0(rid, ENOMEM);
    282                 return;
    283         }
    284        
    285         /*
    286          * Fill filv with argument pointers
    287          */
    288         int i;
    289         for (i = 0; i < count; i++)
    290                 filv[i] = &fil_buf[i];
    291        
    292         filc = count;
    293         filv[count] = NULL;
    294        
    295         ipc_answer_0(rid, EOK);
     235        int rc = async_data_write_accept((void **) &buf, false, 0, 0,
     236            sizeof(fdi_node_t), &buf_size);
     237       
     238        if (rc == EOK) {
     239                int count = buf_size / sizeof(fdi_node_t);
     240               
     241                /*
     242                 * Allocate new filv
     243                 */
     244                fdi_node_t **_filv = (fdi_node_t **) calloc(count + 1, sizeof(fdi_node_t *));
     245                if (_filv == NULL) {
     246                        free(buf);
     247                        async_answer_0(rid, ENOMEM);
     248                        return;
     249                }
     250               
     251                /*
     252                 * Fill the new filv with argument pointers
     253                 */
     254                int i;
     255                for (i = 0; i < count; i++)
     256                        _filv[i] = &buf[i];
     257               
     258                _filv[count] = NULL;
     259               
     260                /*
     261                 * Copy temporary data to global variables
     262                 */
     263                if (fil_buf != NULL)
     264                        free(fil_buf);
     265               
     266                if (filv != NULL)
     267                        free(filv);
     268               
     269                filc = count;
     270                fil_buf = buf;
     271                filv = _filv;
     272        }
     273       
     274        async_answer_0(rid, EOK);
    296275}
    297276
     
    309288        if (rc != EE_OK) {
    310289                DPRINTF("Failed to load executable '%s'.\n", pathname);
    311                 ipc_answer_0(rid, EINVAL);
     290                async_answer_0(rid, EINVAL);
    312291                return 1;
    313292        }
    314293       
    315294        elf_create_pcb(&prog_info, &pcb);
     295       
     296        pcb.cwd = cwd;
    316297       
    317298        pcb.argc = argc;
     
    324305                /* Statically linked program */
    325306                is_dyn_linked = false;
    326                 ipc_answer_0(rid, EOK);
     307                async_answer_0(rid, EOK);
    327308                return 0;
    328309        }
     
    333314                DPRINTF("Failed to load interpreter '%s.'\n",
    334315                    prog_info.interp);
    335                 ipc_answer_0(rid, EINVAL);
     316                async_answer_0(rid, EINVAL);
    336317                return 1;
    337318        }
    338319       
    339320        printf("Run interpreter.\n");
    340         printf("entry point: 0x%lx\n", interp_info.entry);
    341         printf("pcb address: 0x%lx\n", &pcb);
    342         printf("prog dynamic: 0x%lx\n", prog_info.dynamic);
     321        printf("entry point: 0x%lx\n", (unsigned long) interp_info.entry);
     322        printf("pcb address: 0x%lx\n", (unsigned long) &pcb);
     323        printf("prog dynamic: 0x%lx\n", (unsigned long) prog_info.dynamic);
    343324
    344325        is_dyn_linked = true;
    345         ipc_answer_0(rid, EOK);
     326        async_answer_0(rid, EOK);
    346327       
    347328        return 0;
     
    367348                /* Dynamically linked program */
    368349                DPRINTF("Run ELF interpreter.\n");
    369                 DPRINTF("Entry point: 0x%lx\n", interp_info.entry);
    370                
    371                 ipc_answer_0(rid, EOK);
     350                DPRINTF("Entry point: %p\n", interp_info.entry);
     351               
     352                async_answer_0(rid, EOK);
    372353                program_run(interp_info.entry, &pcb);
    373354        } else {
    374355                /* Statically linked program */
    375                 ipc_answer_0(rid, EOK);
     356                async_answer_0(rid, EOK);
    376357                program_run(prog_info.entry, &pcb);
    377358        }
     
    393374        /* Already have a connection? */
    394375        if (connected) {
    395                 ipc_answer_0(iid, ELIMIT);
     376                async_answer_0(iid, ELIMIT);
    396377                return;
    397378        }
     
    400381       
    401382        /* Accept the connection */
    402         ipc_answer_0(iid, EOK);
     383        async_answer_0(iid, EOK);
    403384       
    404385        /* Ignore parameters, the connection is already open */
     
    409390                callid = async_get_call(&call);
    410391               
    411                 switch (IPC_GET_METHOD(call)) {
     392                switch (IPC_GET_IMETHOD(call)) {
    412393                case IPC_M_PHONE_HUNGUP:
    413394                        exit(0);
    414395                case LOADER_GET_TASKID:
    415396                        ldr_get_taskid(callid, &call);
     397                        continue;
     398                case LOADER_SET_CWD:
     399                        ldr_set_cwd(callid, &call);
    416400                        continue;
    417401                case LOADER_SET_PATHNAME:
     
    434418                        break;
    435419                }
    436                 if ((callid & IPC_CALLID_NOTIFICATION) == 0 &&
    437                     IPC_GET_METHOD(call) != IPC_M_PHONE_HUNGUP) {
     420                if (IPC_GET_IMETHOD(call) != IPC_M_PHONE_HUNGUP) {
    438421                        DPRINTF("Responding EINVAL to method %d.\n",
    439                             IPC_GET_METHOD(call));
    440                         ipc_answer_0(callid, EINVAL);
     422                            IPC_GET_IMETHOD(call));
     423                        async_answer_0(callid, EINVAL);
    441424                }
    442425        }
     
    447430int main(int argc, char *argv[])
    448431{
    449         ipcarg_t phonead;
    450         task_id_t id;
    451         int rc;
    452 
    453         connected = false;
    454 
     432        /* Set a handler of incomming connections. */
     433        async_set_client_connection(ldr_connection);
     434       
    455435        /* Introduce this task to the NS (give it our task ID). */
    456         id = task_get_id();
    457         rc = async_req_2_0(PHONE_NS, NS_ID_INTRO, LOWER32(id), UPPER32(id));
     436        task_id_t id = task_get_id();
     437        int rc = async_req_2_0(PHONE_NS, NS_ID_INTRO, LOWER32(id), UPPER32(id));
    458438        if (rc != EOK)
    459439                return -1;
    460 
    461         /* Set a handler of incomming connections. */
    462         async_set_client_connection(ldr_connection);
    463440       
    464441        /* Register at naming service. */
    465         if (ipc_connect_to_me(PHONE_NS, SERVICE_LOAD, 0, 0, &phonead) != 0)
     442        if (service_register(SERVICE_LOAD) != EOK)
    466443                return -2;
    467 
     444       
    468445        async_manager();
    469446       
Note: See TracChangeset for help on using the changeset viewer.