1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2025-02-17 15:45:32 -05:00

Move ELF utils to separate source file

This commit is contained in:
Alex Kotov 2021-12-14 03:07:00 +05:00
parent 566fb46a94
commit e62cec65a5
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
4 changed files with 68 additions and 58 deletions

View file

@ -13,7 +13,6 @@ lib_LIBRARIES = libkernaux.a
TESTS = \
examples/printf \
examples/printf_va \
tests/test_elf \
tests/test_printf \
tests/test_stdlib
@ -42,6 +41,11 @@ if ENABLE_CONSOLE
libkernaux_a_SOURCES += src/console.c
endif
if ENABLE_ELF
libkernaux_a_SOURCES += src/elf.c
TESTS += tests/test_elf
endif
if ENABLE_MULTIBOOT2
libkernaux_a_SOURCES += \
src/multiboot2/helpers.c \

View file

@ -12,6 +12,7 @@ AC_CANONICAL_HOST
AC_ARG_ENABLE([cmdline], AS_HELP_STRING([--disable-cmdline], [disable command line parser]))
AC_ARG_ENABLE([console], AS_HELP_STRING([--disable-console], [disable serial console]))
AC_ARG_ENABLE([elf], AS_HELP_STRING([--disable-elf], [disable ELF utils]))
AC_ARG_ENABLE([multiboot2], AS_HELP_STRING([--disable-multiboot2], [disable Multiboot 2 information parser]))
AC_ARG_ENABLE([pfa], AS_HELP_STRING([--disable-pfa], [disable Page Frame Allocator]))
AC_ARG_ENABLE([units], AS_HELP_STRING([--disable-units], [disable measurement units utils]))
@ -20,6 +21,7 @@ AM_CONDITIONAL([ARCH_I386], [test "$host_cpu" = i386])
AM_CONDITIONAL([ARCH_X86_64], [test "$host_cpu" = x86_64])
AM_CONDITIONAL([ENABLE_CMDLINE], [test "$enable_cmdline" != no])
AM_CONDITIONAL([ENABLE_CONSOLE], [test "$enable_console" != no])
AM_CONDITIONAL([ENABLE_ELF], [test "$enable_elf" != no])
AM_CONDITIONAL([ENABLE_MULTIBOOT2], [test "$enable_multiboot2" != no])
AM_CONDITIONAL([ENABLE_PFA], [test "$enable_pfa" != no])
AM_CONDITIONAL([ENABLE_UNITS], [test "$enable_units" != no])
@ -28,9 +30,10 @@ AS_IF([test "$host_cpu" = i386], [AC_DEFINE([ARCH_I386], [1], [a
AS_IF([test "$host_cpu" = x86_64], [AC_DEFINE([ARCH_X86_64], [1], [architecture is x86_64])])
AS_IF([test "$enable_cmdline" != no], [AC_DEFINE([ENABLE_CMDLINE], [1], [enabled command line parser])])
AS_IF([test "$enable_console" != no], [AC_DEFINE([ENABLE_CONSOLE], [1], [enabled serial console])])
AS_IF([test "$enable_elf" != no], [AC_DEFINE([ENABLE_ELF], [1], [enabled ELF utils])])
AS_IF([test "$enable_multiboot2" != no], [AC_DEFINE([ENABLE_MULTIBOOT2], [1], [enabled Multiboot 2 information parser])])
AS_IF([test "$enable_pfa" != no], [AC_DEFINE([ENABLE_PFA], [1], [enabled Page Frame Allocator])])
AS_IF([test "$enable_units", != no], [AC_DEFINE([ENABLE_UNITS], [1], [enable measurement units utils])])
AS_IF([test "$enable_units", != no], [AC_DEFINE([ENABLE_UNITS], [1], [enabled measurement units utils])])
AM_INIT_AUTOMAKE([1.9 subdir-objects -Wall -Werror])

View file

@ -72,62 +72,7 @@ typedef struct KernAux_ELF_SectionEntry KernAux_ELF_SectionTable[];
typedef struct KernAux_ELF_RelocationEntry KernAux_ELF_RelocationTable[];
inline static bool KernAux_ELF_Header_is_valid(
const struct KernAux_ELF_Header *header
);
bool KernAux_ELF_Header_is_valid(
const struct KernAux_ELF_Header *const header
) {
if (!(
header->magic_0x7F == 0x7F &&
header->magic_E == 'E' &&
header->magic_L == 'L' &&
header->magic_F == 'F' &&
header->header_version == 1 &&
header->elf_version == 1
)) {
return false;
}
if (!(
header->bitness == 1 || // 32 bit
header->bitness == 2 // 64 bit
)) {
return false;
}
if (!(
header->endianness == 1 || // Little endian
header->endianness == 2 // Big endian
)) {
return false;
}
if (!(
header->os_abi >= 0 &&
header->os_abi <= 0x12 &&
header->os_abi != 0x05
)) {
return false;
}
if (!(
header->obj_type == 0x00 || // NONE
header->obj_type == 0x01 || // REL
header->obj_type == 0x02 || // EXEC
header->obj_type == 0x03 || // DYN
header->obj_type == 0x04 || // CORE
header->obj_type == 0xFE00 || // LOOS
header->obj_type == 0xFEFF || // HIOS
header->obj_type == 0xFF00 || // LOPROC
header->obj_type == 0xFFFF // HIPROC
)) {
return false;
}
return true;
}
bool KernAux_ELF_Header_is_valid(const struct KernAux_ELF_Header *header);
#ifdef __cplusplus
}

58
src/elf.c Normal file
View file

@ -0,0 +1,58 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <kernaux/elf.h>
bool KernAux_ELF_Header_is_valid(
const struct KernAux_ELF_Header *const header
) {
if (!(
header->magic_0x7F == 0x7F &&
header->magic_E == 'E' &&
header->magic_L == 'L' &&
header->magic_F == 'F' &&
header->header_version == 1 &&
header->elf_version == 1
)) {
return false;
}
if (!(
header->bitness == 1 || // 32 bit
header->bitness == 2 // 64 bit
)) {
return false;
}
if (!(
header->endianness == 1 || // Little endian
header->endianness == 2 // Big endian
)) {
return false;
}
if (!(
header->os_abi >= 0 &&
header->os_abi <= 0x12 &&
header->os_abi != 0x05
)) {
return false;
}
if (!(
header->obj_type == 0x00 || // NONE
header->obj_type == 0x01 || // REL
header->obj_type == 0x02 || // EXEC
header->obj_type == 0x03 || // DYN
header->obj_type == 0x04 || // CORE
header->obj_type == 0xFE00 || // LOOS
header->obj_type == 0xFEFF || // HIOS
header->obj_type == 0xFF00 || // LOPROC
header->obj_type == 0xFFFF // HIPROC
)) {
return false;
}
return true;
}