/* Kernel entry function */
ENTRY(_start)

OUTPUT_FORMAT(elf32-i386)

SECTIONS {

    /DISCARD/ : {
        *(".text.inlined*")
        *(.comment)
        *(.eh_frame)
        *(.note.gnu.build-id)
    }

    /* Set kernel start address */
    . = 0x100000;

    /* Code and readonly data */
    .text : {
        /* fill gaps with int3 opcode to detect invalid jumps */
        FILL(0xcc)

        /* multiboot header */
        multiboot_header = .;
        KEEP (*(".rodata.multiboot"))

        /* /\* fixed address for IRQ handlers *\/ */
        /* . = 0x1000; */

        /* /\* start of interrupt handlers *\/ */
        /* _stext_irqs = .; */

        /* /\* IRQ Handlers *\/ */
        /* KEEP (*(".text.irqhandlers*")) /\* ASM *\/ */
        /* KEEP (*(".text.irq_handler*")) /\* C *\/ */
        /* *(".text.isrs*") /\* C *\/ */
        /* *(".text.isr_*") /\* C *\/ */
        /* KEEP (*(".text.OSEKOS_ISR*")) */
        /* KEEP (*(".text.idt")) /\* ASM *\/ */

        /* /\* sysenter handler *\/ */
        /* KEEP (*(".text.sysenter_syscall")) */

        /* _etext_irqs = .; */
        /* . += 16; /\* padding after data, workaround for import-trace *\/ */

        KEEP (*(".text.startup"))

        /* place before .text, otherwise they'll be caught by the wildcard */

        /* AOT binary array */
        _wamr_aot_start = .;
        *(".text.wamr_aot")
        _wamr_aot_end = .;

        /* The mmap_space memory used by os_mmap in baremetal platform */
        _wamr_mmap_start = .;
        *(".text.wamr_mmap")
        _wamr_mmap_end = .;

        /* The runtime memory pool used by Alloc_With_Usage */
        _wamr_runtime_pool_start = .;
        *(".text.wamr_runtime_pool")
        _wamr_runtime_pool_end = .;

        /* The linear memory pool used by Alloc_With_Usage */
        _wamr_linear_pool_start = .;
        *(".text.wamr_linear_pool")
        _wamr_linear_pool_end = .;

        /* The memory pool used by Alloc_With_Pool */
        _wamr_global_heap_start = .;
        *(".text.wamr_global_heap")
        _wamr_global_heap_end = .;

        /*
        The libiwasm.a contains the following objects:

        aot_intrinsic.c.obj
        aot_loader.c.obj
        aot_reloc_x86_32.c.obj
        aot_runtime.c.obj
        bh_assert.c.obj
        bh_bitmap.c.obj
        bh_common.c.obj
        bh_hashmap.c.obj
        bh_leb128.c.obj
        bh_list.c.obj
        bh_log.c.obj
        bh_queue.c.obj
        bh_vector.c.obj
        ems_alloc.c.obj
        ems_gc.c.obj
        ems_hmu.c.obj
        ems_kfc.c.obj
        invokeNative_ia32.s.obj
        libc_builtin_wrapper.c.obj
        mem_alloc.c.obj
        platform_init.c.obj
        runtime_timer.c.obj
        wasm_application.c.obj
        wasm_blocking_op.c.obj
        wasm_c_api.c.obj
        wasm_exec_env.c.obj
        wasm_interp_classic.c.obj
        wasm_loader.c.obj
        wasm_loader_common.c.obj
        wasm_memory.c.obj
        wasm_native.c.obj
        wasm_runtime.c.obj
        wasm_runtime_common.c.obj
        wasm_shared_memory.c.obj
        */

        /* Put the .text section from aot_runtime.c.obj from libiwasm.a here */
        _iwasm_aot_runtime_start = .;
        *aot_runtime.c.obj(.text .text.*)
        _iwasm_aot_runtime_end = .;

        /* Put the utility library here */
        _iwasm_bh_start = .;
        *bh_*.c.obj(.text .text.*)
        _iwasm_bh_end = .;

        /* Put the allocator here */
        _iwasm_mem_alloc_start = .;
        *mem_alloc.c.obj(.text .text.*)
        _iwasm_mem_alloc_end = .;

        /* Put the baremetal platform here */
        _iwasm_platform_init_start = .;
        *platform_init.c.obj(.text .text.*)
        _iwasm_platform_init_end = .;

        /* Put the exec_env here */
        _iwasm_exec_env_start = .;
        *wasm_exec_env.c.obj(.text .text.*)
        _iwasm_exec_env_end = .;

        /* Put the classic interpreter here */
        _iwasm_interp_classic_start = .;
        *wasm_interp_classic.c.obj(.text .text.*)
        _iwasm_interp_classic_end = .;

        /* Put the wasm memory here */
        _iwasm_memory_start = .;
        *wasm_memory.c.obj(.text .text.*)
        _iwasm_memory_end = .;

        /* Put the wasm native here */
        _iwasm_native_start = .;
        *wasm_native.c.obj(.text .text.*)
        _iwasm_native_end = .;

        /* Put the wasm runtime here */
        _iwasm_runtime_start = .;
        *wasm_runtime.c.obj(.text .text.*)
        *wasm_runtime_common.c.obj(.text .text.*)
        _iwasm_runtime_end = .;

        _text_start = .;
        *(".text*")
        _text_end = .;

        *(".rodata*")
    }

    /* Data and Stacks */
    /* NOTE: When including the WAMR mmap region inside .text, it has to be large */
    /* . = 0x200000; */
    . = ALIGN(4096);
    .data : {
        KEEP (*(".startup_stack"))
        KEEP (*(".kernel_stack"))
        *(".data*")
    }

    /* Uninitialized data */
    .bss : {
        _sbss = .;
        *(.bss*)
        *(COMMON)
        _ebss = .;
    }

    /* Align and mark end of all sections — heap starts here */
    . = ALIGN(4096);
    _end = .;

    /* Memory-mapped I/O APIC */
    _sioapic = 0xFEC00000;
    ioapic = 0xFEC00000;
    _eioapic = 0xFEC00FFF;

    /* Memory-mapped Local APIC */
    _slapic = 0xFEE00000;
    lapic = 0xFEE00000;
    _elapic = 0xFEE00FFF;
}
