/* Copyright (c) 2019-2024 Griefer@Work                                       *
 *                                                                            *
 * This software is provided 'as-is', without any express or implied          *
 * warranty. In no event will the authors be held liable for any damages      *
 * arising from the use of this software.                                     *
 *                                                                            *
 * Permission is granted to anyone to use this software for any purpose,      *
 * including commercial applications, and to alter it and redistribute it     *
 * freely, subject to the following restrictions:                             *
 *                                                                            *
 * 1. The origin of this software must not be misrepresented; you must not    *
 *    claim that you wrote the original software. If you use this software    *
 *    in a product, an acknowledgement (see the following) in the product     *
 *    documentation is required:                                              *
 *    Portions Copyright (c) 2019-2024 Griefer@Work                           *
 * 2. Altered source versions must be plainly marked as such, and must not be *
 *    misrepresented as being the original software.                          *
 * 3. This notice may not be removed or altered from any source distribution. *
 */
#ifndef INTERN_SECTION
#error "Must #define INTERN_SECTION(name, type, flags, start, size, entsize, link, info) before #including this file"
#endif /* !INTERN_SECTION */

#include <debugger/config.h>

#include <libunwind/asm/features.h>

#ifndef EXPORT_SECTION
#define EXPORT_SECTION(export_name, name, type, flags, start, size, entsize, link, info) \
	INTERN_SECTION(export_name, name, type, flags, start, size, entsize, link, info)
#endif /* !EXPORT_SECTION */

#ifndef SECTION_GLOBALS_DEFINED
#define SECTION_GLOBALS_DEFINED 1
INTDEF byte_t __kernel_sect_text_start[];
INTDEF byte_t __kernel_sect_text_size[];
INTDEF byte_t __kernel_sect_rodata_start[];
INTDEF byte_t __kernel_sect_rodata_size[];
INTDEF byte_t __kernel_sect_gcc_except_table_start[];
INTDEF byte_t __kernel_sect_gcc_except_table_size[];
INTDEF byte_t __kernel_sect_eh_frame_start[];
INTDEF byte_t __kernel_sect_eh_frame_size[];
INTDEF byte_t __kernel_debug_line_start[];
INTDEF byte_t __kernel_debug_line_size[];
INTDEF byte_t __kernel_debug_info_start[];
INTDEF byte_t __kernel_debug_info_size[];
INTDEF byte_t __kernel_debug_aranges_start[];
INTDEF byte_t __kernel_debug_aranges_size[];
INTDEF byte_t __kernel_debug_abbrev_start[];
INTDEF byte_t __kernel_debug_abbrev_size[];
INTDEF byte_t __kernel_debug_str_start[];
INTDEF byte_t __kernel_debug_str_size[];
INTDEF byte_t __kernel_debug_line_str_start[];
INTDEF byte_t __kernel_debug_line_str_size[];
INTDEF byte_t __kernel_debug_rnglists_start[];
INTDEF byte_t __kernel_debug_rnglists_size[];
INTDEF byte_t __kernel_debug_loclists_start[];
INTDEF byte_t __kernel_debug_loclists_size[];
INTDEF byte_t __kernel_pertask_start[];
INTDEF byte_t __kernel_pertask_size[];
INTDEF byte_t __kernel_permman_start[];
INTDEF byte_t __kernel_permman_size[];
INTDEF byte_t __kernel_percpu_start[];
INTDEF byte_t __kernel_percpu_size[];
INTDEF byte_t __kernel_sect_data_start[];
INTDEF byte_t __kernel_sect_data_size[];
INTDEF byte_t __kernel_sect_bss_start[];
INTDEF byte_t __kernel_sect_bss_size[];
#ifdef CONFIG_HAVE_KERNEL_DEBUGGER
INTDEF byte_t __kernel_sect_dbg_hooks_start[];
INTDEF byte_t __kernel_sect_dbg_hooks_size[];
#endif /* CONFIG_HAVE_KERNEL_DEBUGGER */
#ifdef LIBUNWIND_HAVE_ARM_EXIDX
INTDEF byte_t __kernel_sect_ARM_exidx_start[];
INTDEF byte_t __kernel_sect_ARM_exidx_size[];
INTDEF byte_t __kernel_sect_ARM_extab_start[];
INTDEF byte_t __kernel_sect_ARM_extab_size[];
#endif /* LIBUNWIND_HAVE_ARM_EXIDX */
#endif /* !SECTION_GLOBALS_DEFINED */

EXPORT_SECTION(kernel_section_text, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, __kernel_sect_text_start, __kernel_sect_text_size, 0, 0, 0)
EXPORT_SECTION(kernel_section_rodata, ".rodata", SHT_PROGBITS, SHF_ALLOC, __kernel_sect_rodata_start, __kernel_sect_rodata_size, 0, 0, 0)
#ifdef CONFIG_HAVE_KERNEL_DEBUGGER
EXPORT_SECTION(kernel_section_dbg_hooks, ".dbg.hooks", SHT_PROGBITS, SHF_ALLOC, __kernel_sect_dbg_hooks_start, __kernel_sect_dbg_hooks_size, 0, 0, 0)
#endif /* CONFIG_HAVE_KERNEL_DEBUGGER */
EXPORT_SECTION(kernel_section_gcc_except_table, ".gcc_except_table", SHT_PROGBITS, SHF_ALLOC, __kernel_sect_gcc_except_table_start, __kernel_sect_gcc_except_table_size, 0, 0, 0)
EXPORT_SECTION(kernel_section_eh_frame, ".eh_frame", SHT_PROGBITS, SHF_ALLOC, __kernel_sect_eh_frame_start, __kernel_sect_eh_frame_size, 0, 0, 0)
#ifdef LIBUNWIND_HAVE_ARM_EXIDX
EXPORT_SECTION(kernel_section_ARM_exidx, ".ARM.exidx", SHT_ARM_EXIDX, SHF_ALLOC, __kernel_sect_ARM_exidx_start, __kernel_sect_ARM_exidx_size, 0, 0, 0)
EXPORT_SECTION(kernel_section_ARM_extab, ".ARM.extab", SHT_PROGBITS, SHF_ALLOC, __kernel_sect_ARM_extab_start, __kernel_sect_ARM_extab_size, 0, 0, 0)
#endif /* LIBUNWIND_HAVE_ARM_EXIDX */
INTERN_SECTION(kernel_section_debug_line, ".debug_line", SHT_PROGBITS, SHF_ALLOC | SHF_COMPRESSED, __kernel_debug_line_start, __kernel_debug_line_size, 0, 0, 0)
INTERN_SECTION(kernel_section_debug_info, ".debug_info", SHT_PROGBITS, SHF_ALLOC | SHF_COMPRESSED, __kernel_debug_info_start, __kernel_debug_info_size, 0, 0, 0)
INTERN_SECTION(kernel_section_debug_aranges, ".debug_aranges", SHT_PROGBITS, SHF_ALLOC | SHF_COMPRESSED, __kernel_debug_aranges_start, __kernel_debug_aranges_size, 0, 0, 0)
INTERN_SECTION(kernel_section_debug_abbrev, ".debug_abbrev", SHT_PROGBITS, SHF_ALLOC | SHF_COMPRESSED, __kernel_debug_abbrev_start, __kernel_debug_abbrev_size, 0, 0, 0)
INTERN_SECTION(kernel_section_debug_str, ".debug_str", SHT_PROGBITS, SHF_ALLOC | SHF_COMPRESSED, __kernel_debug_str_start, __kernel_debug_str_size, 0, 0, 0)
INTERN_SECTION(kernel_section_debug_line_str, ".debug_line_str", SHT_PROGBITS, SHF_ALLOC | SHF_COMPRESSED, __kernel_debug_line_str_start, __kernel_debug_line_str_size, 0, 0, 0)
INTERN_SECTION(kernel_section_debug_rnglists, ".debug_rnglists", SHT_PROGBITS, SHF_ALLOC | SHF_COMPRESSED, __kernel_debug_rnglists_start, __kernel_debug_rnglists_size, 0, 0, 0)
INTERN_SECTION(kernel_section_debug_loclists, ".debug_loclists", SHT_PROGBITS, SHF_ALLOC | SHF_COMPRESSED, __kernel_debug_loclists_start, __kernel_debug_loclists_size, 0, 0, 0)
INTERN_SECTION(kernel_section_pertask, ".pertask", SHT_PROGBITS, SHF_ALLOC | SHF_TLS, __kernel_pertask_start, __kernel_pertask_size, 0, 0, 0)
INTERN_SECTION(kernel_section_permman, ".permman", SHT_PROGBITS, SHF_ALLOC | SHF_TLS, __kernel_permman_start, __kernel_permman_size, 0, 0, 0)
INTERN_SECTION(kernel_section_percpu, ".percpu", SHT_PROGBITS, SHF_ALLOC | SHF_TLS, __kernel_percpu_start, __kernel_percpu_size, 0, 0, 0)
EXPORT_SECTION(kernel_section_data, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE, __kernel_sect_data_start, __kernel_sect_data_size, 0, 0, 0)
EXPORT_SECTION(kernel_section_bss, ".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE, __kernel_sect_bss_start, __kernel_sect_bss_size, 0, 0, 0)

/* NOTE: This section _must_ be last! */
INTERN_SECTION(kernel_section_shstrtab, ".shstrtab", SHT_STRTAB, SHF_ALLOC | SHF_STRINGS, &kernel_shstrtab_data, sizeof(struct kernel_shstrtab) - 1, 0, 0, 0)

#undef INTERN_SECTION
#undef EXPORT_SECTION
