git: 2bb61497ca76 - main - elf_common.h: Add definitions for LoongArch ELF files

From: Ed Maste <emaste_at_FreeBSD.org>
Date: Mon, 02 Dec 2024 14:20:04 UTC
The branch main has been updated by emaste:

URL: https://cgit.FreeBSD.org/src/commit/?id=2bb61497ca76646c92f25127b8d4d64281123400

commit 2bb61497ca76646c92f25127b8d4d64281123400
Author:     Haowu Ge <gehaowu@bitmoe.com>
AuthorDate: 2024-12-02 11:36:28 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2024-12-02 14:19:33 +0000

    elf_common.h: Add definitions for LoongArch ELF files
    
    Including e_machine, e_flags and relocation types.
    
    References:
    
      Loongson documentation:
        https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc
    
      LLVM reviews / commits:
        https://reviews.llvm.org/D134601
        https://reviews.llvm.org/D115859
        https://reviews.llvm.org/D131467
        https://reviews.llvm.org/D152184
        https://github.com/llvm/llvm-project/pull/73345
        https://github.com/llvm/llvm-project/pull/77039
    
      Binutils commits:
        6d13722a97cee3fd397e116bde3bcedbb1e220be
        9801120721c3a702ce3bd50433ef920f92a83502
        57a930e3bfe4b2c7fd6463ed39311e1938513138
    
    Reviewed by:    markj, emaste
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/1532
---
 sys/sys/elf_common.h | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 176 insertions(+)

diff --git a/sys/sys/elf_common.h b/sys/sys/elf_common.h
index ec5bbbf9f0e6..766ff6ede51b 100644
--- a/sys/sys/elf_common.h
+++ b/sys/sys/elf_common.h
@@ -306,6 +306,7 @@ typedef struct {
 				   and MPRC of Peking University */
 #define	EM_AARCH64	183	/* AArch64 (64-bit ARM) */
 #define	EM_RISCV	243	/* RISC-V */
+#define	EM_LOONGARCH    258     /* Loongson LoongArch */
 
 /* Non-standard or deprecated. */
 #define	EM_486		6	/* Intel i486. */
@@ -382,6 +383,25 @@ typedef struct {
 #define	EF_RISCV_RVE		0x00000008
 #define	EF_RISCV_TSO		0x00000010
 
+/*
+ * Loongson LoongArch Specific e_flags
+ *
+ * Definitions from LoongArch ELF psABI v2.01.
+ * Reference: https://github.com/loongson/LoongArch-Documentation
+ * (commit hash 296de4def055c871809068e0816325a4ac04eb12)
+ */
+
+/* LoongArch Base ABI Modifiers */
+#define	EF_LOONGARCH_ABI_SOFT_FLOAT     0x00000001
+#define	EF_LOONGARCH_ABI_SINGLE_FLOAT   0x00000002
+#define	EF_LOONGARCH_ABI_DOUBLE_FLOAT   0x00000003
+#define	EF_LOONGARCH_ABI_MODIFIER_MASK  0x00000007
+
+/* LoongArch Object file ABI versions */
+#define	EF_LOONGARCH_OBJABI_V0          0x00000000
+#define	EF_LOONGARCH_OBJABI_V1          0x00000040
+#define	EF_LOONGARCH_OBJABI_MASK        0x000000C0
+
 #define	EF_SPARC_EXT_MASK	0x00ffff00
 #define	EF_SPARC_32PLUS		0x00000100
 #define	EF_SPARC_SUN_US1	0x00000200
@@ -1380,6 +1400,162 @@ typedef struct {
 #define	R_RISCV_32_PCREL	57
 #define	R_RISCV_IRELATIVE	58
 
+/*
+ * Loongson LoongArch relocation types.
+ *
+ * LoongArch ELF psABI: https://github.com/loongson/LoongArch-Documentation
+ * (commit hash 9b3bd9f4a497115913c22f1a2a47863798fbc02a)
+ */
+
+/* Relocation types used by the dynamic linker */
+#define	R_LARCH_NONE                         0
+#define	R_LARCH_32                           1
+#define	R_LARCH_64                           2
+#define	R_LARCH_RELATIVE                     3
+#define	R_LARCH_COPY                         4
+#define	R_LARCH_JUMP_SLOT                    5
+#define	R_LARCH_TLS_DTPMOD32                 6
+#define	R_LARCH_TLS_DTPMOD64                 7
+#define	R_LARCH_TLS_DTPREL32                 8
+#define	R_LARCH_TLS_DTPREL64                 9
+#define	R_LARCH_TLS_TPREL32                 10
+#define	R_LARCH_TLS_TPREL64                 11
+#define	R_LARCH_IRELATIVE                   12
+#define	R_LARCH_MARK_LA                     20
+#define	R_LARCH_MARK_PCREL                  21
+#define	R_LARCH_SOP_PUSH_PCREL              22
+#define	R_LARCH_SOP_PUSH_ABSOLUTE           23
+#define	R_LARCH_SOP_PUSH_DUP                24
+#define	R_LARCH_SOP_PUSH_GPREL              25
+#define	R_LARCH_SOP_PUSH_TLS_TPREL          26
+#define	R_LARCH_SOP_PUSH_TLS_GOT            27
+#define	R_LARCH_SOP_PUSH_TLS_GD             28
+#define	R_LARCH_SOP_PUSH_PLT_PCREL          29
+#define	R_LARCH_SOP_ASSERT                  30
+#define	R_LARCH_SOP_NOT                     31
+#define	R_LARCH_SOP_SUB                     32
+#define	R_LARCH_SOP_SL                      33
+#define	R_LARCH_SOP_SR                      34
+#define	R_LARCH_SOP_ADD                     35
+#define	R_LARCH_SOP_AND                     36
+#define	R_LARCH_SOP_IF_ELSE                 37
+#define	R_LARCH_SOP_POP_32_S_10_5           38
+#define	R_LARCH_SOP_POP_32_U_10_12          39
+#define	R_LARCH_SOP_POP_32_S_10_12          40
+#define	R_LARCH_SOP_POP_32_S_10_16          41
+#define	R_LARCH_SOP_POP_32_S_10_16_S2       42
+#define	R_LARCH_SOP_POP_32_S_5_20           43
+#define	R_LARCH_SOP_POP_32_S_0_5_10_16_S2   44
+#define	R_LARCH_SOP_POP_32_S_0_10_10_16_S2  45
+#define	R_LARCH_SOP_POP_32_U                46
+#define	R_LARCH_ADD8                        47
+#define	R_LARCH_ADD16                       48
+#define	R_LARCH_ADD24                       49
+#define	R_LARCH_ADD32                       50
+#define	R_LARCH_ADD64                       51
+#define	R_LARCH_SUB8                        52
+#define	R_LARCH_SUB16                       53
+#define	R_LARCH_SUB24                       54
+#define	R_LARCH_SUB32                       55
+#define	R_LARCH_SUB64                       56
+#define	R_LARCH_GNU_VTINHERIT               57
+#define	R_LARCH_GNU_VTENTRY                 58
+
+/*
+ * Relocs whose processing do not require a stack machine.
+ *
+ * Spec addition: https://github.com/loongson/LoongArch-Documentation/pull/57
+ */
+#define	R_LARCH_B16                         64
+#define	R_LARCH_B21                         65
+#define	R_LARCH_B26                         66
+#define	R_LARCH_ABS_HI20                    67
+#define	R_LARCH_ABS_LO12                    68
+#define	R_LARCH_ABS64_LO20                  69
+#define	R_LARCH_ABS64_HI12                  70
+#define	R_LARCH_PCALA_HI20                  71
+#define	R_LARCH_PCALA_LO12                  72
+#define	R_LARCH_PCALA64_LO20                73
+#define	R_LARCH_PCALA64_HI12                74
+#define	R_LARCH_GOT_PC_HI20                 75
+#define	R_LARCH_GOT_PC_LO12                 76
+#define	R_LARCH_GOT64_PC_LO20               77
+#define	R_LARCH_GOT64_PC_HI12               78
+#define	R_LARCH_GOT_HI20                    79
+#define	R_LARCH_GOT_LO12                    80
+#define	R_LARCH_GOT64_LO20                  81
+#define	R_LARCH_GOT64_HI12                  82
+#define	R_LARCH_TLS_LE_HI20                 83
+#define	R_LARCH_TLS_LE_LO12                 84
+#define	R_LARCH_TLS_LE64_LO20               85
+#define	R_LARCH_TLS_LE64_HI12               86
+#define	R_LARCH_TLS_IE_PC_HI20              87
+#define	R_LARCH_TLS_IE_PC_LO12              88
+#define	R_LARCH_TLS_IE64_PC_LO20            89
+#define	R_LARCH_TLS_IE64_PC_HI12            90
+#define	R_LARCH_TLS_IE_HI20                 91
+#define	R_LARCH_TLS_IE_LO12                 92
+#define	R_LARCH_TLS_IE64_LO20               93
+#define	R_LARCH_TLS_IE64_HI12               94
+#define	R_LARCH_TLS_LD_PC_HI20              95
+#define	R_LARCH_TLS_LD_HI20                 96
+#define	R_LARCH_TLS_GD_PC_HI20              97
+#define	R_LARCH_TLS_GD_HI20                 98
+#define	R_LARCH_32_PCREL                    99
+#define	R_LARCH_RELAX                      100
+
+/*
+ * Relocs added in ELF for the LoongArchâ„¢ Architecture v20230519, part of the
+ * v2.10 LoongArch ABI specs.
+ *
+ * Spec addition: https://github.com/loongson/la-abi-specs/pull/1
+ *
+ * Note that the 101 and 104 relocation numbers are defined as R_LARCH_DELETE
+ * and R_LARCH_CFA respectively in psABI 2.10. But they are marked as reserved
+ * in psABI v2.20 because they were proved not necessary to be exposed outside
+ * of the linker.
+ */
+#define	R_LARCH_ALIGN                      102
+#define	R_LARCH_PCREL20_S2                 103
+#define	R_LARCH_ADD6                       105
+#define	R_LARCH_SUB6                       106
+#define	R_LARCH_ADD_ULEB128                107
+#define	R_LARCH_SUB_ULEB128                108
+#define	R_LARCH_64_PCREL                   109
+
+/*
+ * Relocs added in ELF for the LoongArchâ„¢ Architecture v20231102, part of the
+ * v2.20 LoongArch ABI specs.
+ *
+ * Spec addition: https://github.com/loongson/la-abi-specs/pull/4
+ */
+#define	R_LARCH_CALL36                     110
+
+/*
+ * Relocs added in ELF for the LoongArchâ„¢ Architecture v20231219, part of the
+ * v2.30 LoongArch ABI specs.
+ *
+ * Spec addition: https://github.com/loongson/la-abi-specs/pull/5
+ */
+#define	R_LARCH_TLS_DESC32                  13
+#define	R_LARCH_TLS_DESC64                  14
+#define	R_LARCH_TLS_DESC_PC_HI20           111
+#define	R_LARCH_TLS_DESC_PC_LO12           112
+#define	R_LARCH_TLS_DESC64_PC_LO20         113
+#define	R_LARCH_TLS_DESC64_PC_HI12         114
+#define	R_LARCH_TLS_DESC_HI20              115
+#define	R_LARCH_TLS_DESC_LO12              116
+#define	R_LARCH_TLS_DESC64_LO20            117
+#define	R_LARCH_TLS_DESC64_HI12            118
+#define	R_LARCH_TLS_DESC_LD                119
+#define	R_LARCH_TLS_DESC_CALL              120
+#define	R_LARCH_TLS_LE_HI20_R              121
+#define	R_LARCH_TLS_LE_ADD_R               122
+#define	R_LARCH_TLS_LE_LO12_R              123
+#define	R_LARCH_TLS_LD_PCREL20_S2          124
+#define	R_LARCH_TLS_GD_PCREL20_S2          125
+#define	R_LARCH_TLS_DESC_PCREL20_S2        126
+
 #define	R_SPARC_NONE		0
 #define	R_SPARC_8		1
 #define	R_SPARC_16		2