[PATCH] backends: add checks for _GLOBAL_OFFSET_TABLE_ on loongarch

2023-03-31 Thread Youling Tang
Add handling of _GLOBAL_OFFSET_TABLE_.

Before applying the patch:
$ ./src/elflint --gnu-ld ./src/elflint
section [35] '.symtab': _GLOBAL_OFFSET_TABLE_ symbol value 0x68548
does not match .got.plt section address 0x68238

After applying the patch:
$ ./src/elflint --gnu-ld ./src/elflint
No errors

Signed-off-by: Liwei Ge 
Signed-off-by: Youling Tang 
---
 backends/ChangeLog  |  4 
 backends/loongarch_init.c   |  1 +
 backends/loongarch_symbol.c | 35 +++
 3 files changed, 40 insertions(+)

diff --git a/backends/ChangeLog b/backends/ChangeLog
index 81f08314..41071953 100644
--- a/backends/ChangeLog
+++ b/backends/ChangeLog
@@ -1,3 +1,7 @@
+2023-04-01  Youling Tang 
+   * loongarch_init.c (loongarch_init): Hook check_special_symbol.
+   * loongarch_symbol.c (loongarch_check_special_symbol): New function.
+
 2023-02-07  Mark Wielaard  
 
* libebl_CPU.h (dwarf_peeled_die_type): Explicitly handle
diff --git a/backends/loongarch_init.c b/backends/loongarch_init.c
index 59d8cc3d..b641b07f 100644
--- a/backends/loongarch_init.c
+++ b/backends/loongarch_init.c
@@ -46,6 +46,7 @@ loongarch_init (Elf *elf __attribute__ ((unused)),
   loongarch_init_reloc (eh);
   HOOK (eh, reloc_simple_type);
   HOOK (eh, machine_flag_check);
+  HOOK (eh, check_special_symbol);
 
   return eh;
 }
diff --git a/backends/loongarch_symbol.c b/backends/loongarch_symbol.c
index 43306ab8..5ce55bad 100644
--- a/backends/loongarch_symbol.c
+++ b/backends/loongarch_symbol.c
@@ -79,3 +79,38 @@ loongarch_machine_flag_check (GElf_Word flags)
   return ((flags &~ (EF_LARCH_ABI_MODIFIER_MASK
 | EF_LARCH_OBJABI_V1)) == 0);
 }
+
+/* Check whether given symbol's st_value and st_size are OK despite failing
+   normal checks.  */
+bool
+loongarch_check_special_symbol (Elf *elf, const GElf_Sym *sym,
+   const char *name, const GElf_Shdr *destshdr)
+{
+  if (name != NULL
+  && strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
+{
+  size_t shstrndx;
+  if (elf_getshdrstrndx (elf, &shstrndx) != 0)
+   return false;
+  const char *sname = elf_strptr (elf, shstrndx, destshdr->sh_name);
+  if (sname != NULL
+ && (strcmp (sname, ".got") == 0 || strcmp (sname, ".got.plt") == 0))
+   {
+ Elf_Scn *scn = NULL;
+ while ((scn = elf_nextscn (elf, scn)) != NULL)
+   {
+ GElf_Shdr shdr_mem;
+ GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
+ if (shdr != NULL)
+   {
+ sname = elf_strptr (elf, shstrndx, shdr->sh_name);
+ if (sname != NULL && strcmp (sname, ".got") == 0)
+   return (sym->st_value >= shdr->sh_addr
+   && sym->st_value < shdr->sh_addr + shdr->sh_size);
+   }
+   }
+   }
+}
+
+  return false;
+}
-- 
2.37.1



[PATCH 2/2] backends: Add new relocation type handling for LoongArch

2023-03-31 Thread Youling Tang
Add new relocation type handling.

Signed-off-by: Youling Tang 
---
 backends/ChangeLog   |  4 
 backends/loongarch_reloc.def | 40 
 2 files changed, 44 insertions(+)

diff --git a/backends/ChangeLog b/backends/ChangeLog
index 41071953..cff82b01 100644
--- a/backends/ChangeLog
+++ b/backends/ChangeLog
@@ -1,3 +1,7 @@
+2023-04-01  Youling Tang  
+
+   * loongarch_reloc.def: Add RELOC_TYPE B16 to RELAX.
+
 2023-04-01  Youling Tang 
* loongarch_init.c (loongarch_init): Hook check_special_symbol.
* loongarch_symbol.c (loongarch_check_special_symbol): New function.
diff --git a/backends/loongarch_reloc.def b/backends/loongarch_reloc.def
index dd4a6b6d..fd204924 100644
--- a/backends/loongarch_reloc.def
+++ b/backends/loongarch_reloc.def
@@ -79,3 +79,43 @@ RELOC_TYPE (SUB32,   REL)
 RELOC_TYPE (SUB64, REL)
 RELOC_TYPE (GNU_VTINHERIT, REL)
 RELOC_TYPE (GNU_VTENTRY,   REL)
+
+/* reserved 59-63 */
+
+RELOC_TYPE (B16,   REL)
+RELOC_TYPE (B21,   REL)
+RELOC_TYPE (B26,   REL)
+RELOC_TYPE (ABS_HI20,  REL)
+RELOC_TYPE (ABS_LO12,  REL)
+RELOC_TYPE (ABS64_LO20,REL)
+RELOC_TYPE (ABS64_HI12,REL)
+RELOC_TYPE (PCALA_HI20,REL)
+RELOC_TYPE (PCALA_LO12,REL)
+RELOC_TYPE (PCALA64_LO20,  REL)
+RELOC_TYPE (PCALA64_HI12,  REL)
+RELOC_TYPE (GOT_PC_HI20,   REL)
+RELOC_TYPE (GOT_PC_LO12,   REL)
+RELOC_TYPE (GOT64_PC_LO20, REL)
+RELOC_TYPE (GOT64_PC_HI12, REL)
+RELOC_TYPE (GOT_HI20,  REL)
+RELOC_TYPE (GOT_LO12,  REL)
+RELOC_TYPE (GOT64_LO20,REL)
+RELOC_TYPE (GOT64_HI12,REL)
+RELOC_TYPE (TLS_LE_HI20,   REL)
+RELOC_TYPE (TLS_LE_LO12,   REL)
+RELOC_TYPE (TLS_LE64_LO20, REL)
+RELOC_TYPE (TLS_LE64_HI12, REL)
+RELOC_TYPE (TLS_IE_PC_HI20,REL)
+RELOC_TYPE (TLS_IE_PC_LO12,REL)
+RELOC_TYPE (TLS_IE64_PC_LO20,  REL)
+RELOC_TYPE (TLS_IE64_PC_HI12,  REL)
+RELOC_TYPE (TLS_IE_HI20,   REL)
+RELOC_TYPE (TLS_IE_LO12,   REL)
+RELOC_TYPE (TLS_IE64_LO20, REL)
+RELOC_TYPE (TLS_IE64_HI12, REL)
+RELOC_TYPE (TLS_LD_PC_HI20,REL)
+RELOC_TYPE (TLS_LD_HI20,   REL)
+RELOC_TYPE (TLS_GD_PC_HI20,REL)
+RELOC_TYPE (TLS_GD_HI20,   REL)
+RELOC_TYPE (32_PCREL,  REL)
+RELOC_TYPE (RELAX, REL)
-- 
2.37.1



[PATCH 1/2] libelf: Sync elf.h from glibc.

2023-03-31 Thread Youling Tang
Adds R_LARCH_*.

Signed-off-by: Youling Tang 
---
 libelf/ChangeLog |  4 
 libelf/elf.h | 42 +-
 2 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index 8cc3c53c..1d5178ca 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,3 +1,7 @@
+2023-04-01  Youling Tang  
+
+   * elf.h: Update from glibc.
+
 2023-03-03  Mark Wielaard  
 
* libelf.h: Define ELFCOMPRESS_ZSTD if undefined.
diff --git a/libelf/elf.h b/libelf/elf.h
index da41bad3..4bc0e429 100644
--- a/libelf/elf.h
+++ b/libelf/elf.h
@@ -1,5 +1,5 @@
 /* This file defines standard ELF types, structures, and macros.
-   Copyright (C) 1995-2022 Free Software Foundation, Inc.
+   Copyright (C) 1995-2023 Free Software Foundation, Inc.
This file is part of the GNU C Library.
 
The GNU C Library is free software; you can redistribute it and/or
@@ -4159,6 +4159,46 @@ enum
 #define R_LARCH_GNU_VTINHERIT  57
 #define R_LARCH_GNU_VTENTRY  58
 
+/* reserved 59-63 */
+
+#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
+
 /* ARC specific declarations.  */
 
 /* Processor specific flags for the Ehdr e_flags field.  */
-- 
2.37.1