1 /* Run-time dynamic linker data structures for loaded ELF shared objects.
2    Copyright (C) 2000-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library.  If not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #ifndef _MIPS_LDSODEFS_H
20 #define _MIPS_LDSODEFS_H 1
21 
22 #include <elf.h>
23 
24 struct La_mips_32_regs;
25 struct La_mips_32_retval;
26 struct La_mips_64_regs;
27 struct La_mips_64_retval;
28 
29 /* Translate a processor specific dynamic tag to the index
30    in l_info array.  */
31 #define DT_MIPS(x) (DT_MIPS_##x - DT_LOPROC + DT_NUM)
32 
33 #define ELF_MACHINE_GNU_HASH_ADDRIDX (DT_MIPS_XHASH - DT_LOPROC + DT_NUM)
34 
35 /* Calculate the index of a symbol in MIPS xhash.  */
36 #define ELF_MACHINE_HASH_SYMIDX(map, hasharr) \
37   ((map)->l_mach.mips_xlat_zero[(hasharr) - (map)->l_gnu_chain_zero])
38 
39 /* Setup MIPS xhash.  */
40 #define ELF_MACHINE_XHASH_SETUP(hash32, symbias, map)			    \
41   do									    \
42     {									    \
43       (hash32) += (map)->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val - (symbias); \
44       (map)->l_mach.mips_xlat_zero = (hash32) - (symbias);		    \
45     }									    \
46   while (0)
47 
48 #define ARCH_PLTENTER_MEMBERS						    \
49     Elf32_Addr (*mips_o32_gnu_pltenter) (Elf32_Sym *, unsigned int,	    \
50 					 uintptr_t *, uintptr_t *,	    \
51 					 struct La_mips_32_regs *,	    \
52 					 unsigned int *, const char *name,  \
53 					 long int *framesizep);		    \
54     Elf32_Addr (*mips_n32_gnu_pltenter) (Elf32_Sym *, unsigned int,	    \
55 					 uintptr_t *, uintptr_t *,	    \
56 					 struct La_mips_64_regs *,	    \
57 					 unsigned int *, const char *name,  \
58 					 long int *framesizep);		    \
59     Elf64_Addr (*mips_n64_gnu_pltenter) (Elf64_Sym *, unsigned int,	    \
60 					 uintptr_t *, uintptr_t *,	    \
61 					 struct La_mips_64_regs *,	    \
62 					 unsigned int *, const char *name,  \
63 					 long int *framesizep);
64 
65 #define ARCH_PLTEXIT_MEMBERS						    \
66     unsigned int (*mips_o32_gnu_pltexit) (Elf32_Sym *, unsigned int,	    \
67 					  uintptr_t *, uintptr_t *,	    \
68 					  const struct La_mips_32_regs *,   \
69 					  struct La_mips_32_retval *,	    \
70 					  const char *);		    \
71     unsigned int (*mips_n32_gnu_pltexit) (Elf32_Sym *, unsigned int,	    \
72 					  uintptr_t *, uintptr_t *,	    \
73 					  const struct La_mips_64_regs *,   \
74 					  struct La_mips_64_retval *,	    \
75 					  const char *);		    \
76     unsigned int (*mips_n64_gnu_pltexit) (Elf64_Sym *, unsigned int,	    \
77 					  uintptr_t *, uintptr_t *,	    \
78 					  const struct La_mips_64_regs *,   \
79 					  struct La_mips_64_retval *,	    \
80 					  const char *);
81 
82 #include_next <ldsodefs.h>
83 
84 /* The 64-bit MIPS ELF ABI uses an unusual reloc format.  Each
85    relocation entry specifies up to three actual relocations, all at
86    the same address.  The first relocation which required a symbol
87    uses the symbol in the r_sym field.  The second relocation which
88    requires a symbol uses the symbol in the r_ssym field.  If all
89    three relocations require a symbol, the third one uses a zero
90    value.
91 
92    We define these structures in internal headers because we're not
93    sure we want to make them part of the ABI yet.  Eventually, some of
94    this may move into elf/elf.h.  */
95 
96 /* An entry in a 64 bit SHT_REL section.  */
97 
98 typedef struct
99 {
100   Elf32_Word    r_sym;		/* Symbol index */
101   unsigned char r_ssym;		/* Special symbol for 2nd relocation */
102   unsigned char r_type3;	/* 3rd relocation type */
103   unsigned char r_type2;	/* 2nd relocation type */
104   unsigned char r_type1;	/* 1st relocation type */
105 } _Elf64_Mips_R_Info;
106 
107 typedef union
108 {
109   Elf64_Xword	r_info_number;
110   _Elf64_Mips_R_Info r_info_fields;
111 } _Elf64_Mips_R_Info_union;
112 
113 typedef struct
114 {
115   Elf64_Addr	r_offset;		/* Address */
116   _Elf64_Mips_R_Info_union r_info;	/* Relocation type and symbol index */
117 } Elf64_Mips_Rel;
118 
119 typedef struct
120 {
121   Elf64_Addr	r_offset;		/* Address */
122   _Elf64_Mips_R_Info_union r_info;	/* Relocation type and symbol index */
123   Elf64_Sxword	r_addend;		/* Addend */
124 } Elf64_Mips_Rela;
125 
126 #define ELF64_MIPS_R_SYM(i) \
127   ((__extension__ (_Elf64_Mips_R_Info_union)(i)).r_info_fields.r_sym)
128 #define ELF64_MIPS_R_TYPE(i) \
129   (((_Elf64_Mips_R_Info_union)(i)).r_info_fields.r_type1 \
130    | ((Elf32_Word)(__extension__ (_Elf64_Mips_R_Info_union)(i) \
131 		   ).r_info_fields.r_type2 << 8) \
132    | ((Elf32_Word)(__extension__ (_Elf64_Mips_R_Info_union)(i) \
133 		   ).r_info_fields.r_type3 << 16) \
134    | ((Elf32_Word)(__extension__ (_Elf64_Mips_R_Info_union)(i) \
135 		   ).r_info_fields.r_ssym << 24))
136 #define ELF64_MIPS_R_INFO(sym, type) \
137   (__extension__ (_Elf64_Mips_R_Info_union) \
138    (__extension__ (_Elf64_Mips_R_Info) \
139    { (sym), ELF64_MIPS_R_SSYM (type), \
140        ELF64_MIPS_R_TYPE3 (type), \
141        ELF64_MIPS_R_TYPE2 (type), \
142        ELF64_MIPS_R_TYPE1 (type) \
143    }).r_info_number)
144 /* These macros decompose the value returned by ELF64_MIPS_R_TYPE, and
145    compose it back into a value that it can be used as an argument to
146    ELF64_MIPS_R_INFO.  */
147 #define ELF64_MIPS_R_SSYM(i) (((i) >> 24) & 0xff)
148 #define ELF64_MIPS_R_TYPE3(i) (((i) >> 16) & 0xff)
149 #define ELF64_MIPS_R_TYPE2(i) (((i) >> 8) & 0xff)
150 #define ELF64_MIPS_R_TYPE1(i) ((i) & 0xff)
151 #define ELF64_MIPS_R_TYPEENC(type1, type2, type3, ssym) \
152   ((type1) \
153    | ((Elf32_Word)(type2) << 8) \
154    | ((Elf32_Word)(type3) << 16) \
155    | ((Elf32_Word)(ssym) << 24))
156 
157 #undef ELF64_R_SYM
158 #define ELF64_R_SYM(i) ELF64_MIPS_R_SYM (i)
159 #undef ELF64_R_TYPE
160 #define ELF64_R_TYPE(i) ELF64_MIPS_R_TYPE (i)
161 #undef ELF64_R_INFO
162 #define ELF64_R_INFO(sym, type) ELF64_MIPS_R_INFO ((sym), (type))
163 
164 #endif
165