From 522158913fe5dc63c23b6940a9c6b108eb4b6b28 Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Fri, 3 Jan 2025 21:51:17 +0200 Subject: [PATCH] kernel: Add .note section Signed-off-by: EnderIce2 --- Kernel/kernel.cpp | 70 ++++++++++++++++ tools/binutils-gdb.patch | 176 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 246 insertions(+) diff --git a/Kernel/kernel.cpp b/Kernel/kernel.cpp index a52708a7..82f24029 100644 --- a/Kernel/kernel.cpp +++ b/Kernel/kernel.cpp @@ -419,3 +419,73 @@ EXTERNC void TaskingPanic() if (TaskManager) TaskManager->Panic(); } + +#define HEX_DIGIT(c) (((c) >= '0' && (c) <= '9') ? ((c) - '0') : ((c) - 'a' + 10)) +#define CONVERT_TO_BYTE(h, l) ((HEX_DIGIT(h) << 4) | HEX_DIGIT(l)) +#define HASH_BYTES(hex) \ + {CONVERT_TO_BYTE(hex[0], hex[1]), \ + CONVERT_TO_BYTE(hex[2], hex[3]), \ + CONVERT_TO_BYTE(hex[4], hex[5]), \ + CONVERT_TO_BYTE(hex[6], hex[7]), \ + CONVERT_TO_BYTE(hex[8], hex[9]), \ + CONVERT_TO_BYTE(hex[10], hex[11]), \ + CONVERT_TO_BYTE(hex[12], hex[13]), \ + CONVERT_TO_BYTE(hex[14], hex[15]), \ + CONVERT_TO_BYTE(hex[16], hex[17]), \ + CONVERT_TO_BYTE(hex[18], hex[19]), \ + CONVERT_TO_BYTE(hex[20], hex[21]), \ + CONVERT_TO_BYTE(hex[22], hex[23]), \ + CONVERT_TO_BYTE(hex[24], hex[25]), \ + CONVERT_TO_BYTE(hex[26], hex[27]), \ + CONVERT_TO_BYTE(hex[28], hex[29]), \ + CONVERT_TO_BYTE(hex[30], hex[31]), \ + CONVERT_TO_BYTE(hex[32], hex[33]), \ + CONVERT_TO_BYTE(hex[34], hex[35]), \ + CONVERT_TO_BYTE(hex[36], hex[37]), \ + CONVERT_TO_BYTE(hex[38], hex[39])} + +/* These are declared in GNU ld */ +enum +{ + NT_FNX_ABI_TAG = 1, + NT_FNX_VERSION = 2, + NT_FNX_BUILD_ID = 3, + NT_FNX_ARCH = 4 +}; + +struct Elf_Nhdr +{ + __UINT32_TYPE__ n_namesz; + __UINT32_TYPE__ n_descsz; + __UINT32_TYPE__ n_type; +} __attribute__((packed)); + +const struct +{ + Elf_Nhdr header; + char name[4]; + __UINT32_TYPE__ desc[4]; +} __abi_tag __attribute__((aligned(4), section(".note.ABI-tag"))) = { + .header = { + .n_namesz = 4, /* "FNX" + '\0' */ + .n_descsz = sizeof(__UINT32_TYPE__) * 4, /* Description Size */ + .n_type = NT_FNX_ABI_TAG, /* Type */ + }, + .name = "FNX", + .desc = {0, 0, 0, 0}, +}; + +const struct +{ + Elf_Nhdr header; + char name[4]; + __UINT8_TYPE__ desc[20]; +} __build_id __attribute__((aligned(4), section(".note.build-id"))) = { + .header = { + .n_namesz = 4, /* "FNX" + '\0' */ + .n_descsz = sizeof(__UINT8_TYPE__) * 20, /* Description Size */ + .n_type = NT_FNX_BUILD_ID, /* Type */ + }, + .name = "FNX", + .desc = HASH_BYTES(GIT_COMMIT), +}; diff --git a/tools/binutils-gdb.patch b/tools/binutils-gdb.patch index 48df359b..6a74f6bf 100644 --- a/tools/binutils-gdb.patch +++ b/tools/binutils-gdb.patch @@ -125,3 +125,179 @@ index 00000000000..1509ec7fe53 +GENERATE_SHLIB_SCRIPT=yes +GENERATE_PIE_SCRIPT=yes +TEXT_START_ADDR=0x400000 +diff --git a/binutils/readelf.c b/binutils/readelf.c +index 5d1cf9c3..671a8081 100644 +--- a/binutils/readelf.c ++++ b/binutils/readelf.c +@@ -20909,6 +20909,30 @@ get_gnu_elf_note_type (unsigned e_type) + } + } + ++static const char * ++get_fnx_elf_note_type (unsigned e_type) ++{ ++ /* NB/ Keep this switch statement in sync with print_gnu_note (). */ ++ switch (e_type) ++ { ++ case NT_FNX_ABI_TAG: ++ return _("NT_FNX_ABI_TAG (ABI version tag)"); ++ case NT_FNX_VERSION: ++ return _("NT_FNX_VERSION (version)"); ++ case NT_FNX_BUILD_ID: ++ return _("NT_FNX_BUILD_ID (unique build ID bitstring)"); ++ case NT_FNX_ARCH: ++ return _("NT_FNX_ARCH (architecture)"); ++ default: ++ { ++ static char buff[64]; ++ ++ snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type); ++ return buff; ++ } ++ } ++} ++ + static void + decode_x86_compat_isa (unsigned int bitmask) + { +@@ -21646,6 +21670,91 @@ print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote) + return true; + } + ++static bool ++print_fnx_note (Filedata * filedata, Elf_Internal_Note *pnote) ++{ ++ /* NB/ Keep this switch statement in sync with get_fnx_elf_note_type (). */ ++ switch (pnote->type) ++ { ++ case NT_FNX_ABI_TAG: ++ { ++ unsigned int os, major, minor, subminor; ++ const char *osname; ++ ++ /* PR 17531: file: 030-599401-0.004. */ ++ if (pnote->descsz < 16) ++ { ++ printf (_(" \n")); ++ break; ++ } ++ ++ os = byte_get ((unsigned char *) pnote->descdata, 4); ++ major = byte_get ((unsigned char *) pnote->descdata + 4, 4); ++ minor = byte_get ((unsigned char *) pnote->descdata + 8, 4); ++ subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4); ++ ++ switch (os) ++ { ++ case FNX_ABI_TAG_FENNIX: ++ osname = "Fennix"; ++ break; ++ default: ++ osname = "Unknown"; ++ break; ++ } ++ ++ printf (_(" OS: %s, ABI: %d.%d.%d\n"), osname, ++ major, minor, subminor); ++ } ++ break; ++ ++ case NT_FNX_VERSION: ++ { ++ size_t i; ++ ++ printf (_(" Version: ")); ++ for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i) ++ printf ("%c", pnote->descdata[i]); ++ printf ("\n"); ++ } ++ break; ++ ++ case NT_FNX_BUILD_ID: ++ { ++ size_t i; ++ ++ printf (_(" Build ID: ")); ++ for (i = 0; i < pnote->descsz; ++i) ++ printf ("%02x", pnote->descdata[i] & 0xff); ++ printf ("\n"); ++ } ++ break; ++ ++ case NT_FNX_ARCH: ++ { ++ /* TODO */ ++ printf (_(" Unimplemented NT_FNX_ARCH note\n")); ++ } ++ break; ++ ++ default: ++ /* Handle unrecognised types. An error message should have already been ++ created by get_gnu_elf_note_type(), so all that we need to do is to ++ display the data. */ ++ { ++ size_t i; ++ ++ printf (_(" Description data: ")); ++ for (i = 0; i < pnote->descsz; ++i) ++ printf ("%02x ", pnote->descdata[i] & 0xff); ++ printf ("\n"); ++ } ++ break; ++ } ++ ++ return true; ++} ++ + static const char * + get_v850_elf_note_type (enum v850_notes n_type) + { +@@ -22925,6 +23034,10 @@ process_note (Elf_Internal_Note * pnote, + /* GNU-specific object file notes. */ + nt = get_gnu_elf_note_type (pnote->type); + ++ else if (startswith (pnote->namedata, "FNX")) ++ /* FNX-specific object file notes. */ ++ nt = get_fnx_elf_note_type (pnote->type); ++ + else if (startswith (pnote->namedata, "AMDGPU")) + /* AMDGPU-specific object file notes. */ + nt = get_amdgpu_elf_note_type (pnote->type); +@@ -22992,6 +23105,8 @@ process_note (Elf_Internal_Note * pnote, + return print_ia64_vms_note (pnote); + else if (startswith (pnote->namedata, "GNU")) + return print_gnu_note (filedata, pnote); ++ else if (startswith (pnote->namedata, "FNX")) ++ return print_fnx_note (filedata, pnote); + else if (startswith (pnote->namedata, "stapsdt")) + return print_stapsdt_note (pnote); + else if (startswith (pnote->namedata, "CORE")) +diff --git a/include/elf/common.h b/include/elf/common.h +index c9920e77..a714bb2a 100644 +--- a/include/elf/common.h ++++ b/include/elf/common.h +@@ -859,6 +859,15 @@ + #define NT_ARCH 2 /* Contains an architecture string. */ + #define NT_GO_BUILDID 4 /* Contains GO buildid data. */ + ++/* Values for notes in non-core files using name "FNX". */ ++#define NT_FNX_ABI_TAG 1 ++#define NT_FNX_VERSION 2 ++#define NT_FNX_BUILD_ID 3 ++#define NT_FNX_ARCH 4 ++ ++/* Values used in FNX .note.ABI-tag notes (NT_FNX_ABI_TAG). */ ++#define FNX_ABI_TAG_FENNIX 0 ++ + /* Values for notes in non-core files using name "GNU". */ + + #define NT_GNU_ABI_TAG 1 +diff --git a/.gitignore b/.gitignore +index 0a40764c..9aa17853 100644 +--- a/.gitignore ++++ b/.gitignore +@@ -74,3 +74,4 @@ stamp-* + /gmp* + /isl* + /gettext* ++/__build