1 1.1 christos /* SPARC-specific values for a.out files 2 1.1 christos 3 1.11 christos Copyright (C) 2001-2024 Free Software Foundation, Inc. 4 1.1 christos 5 1.1 christos This program is free software; you can redistribute it and/or modify 6 1.1 christos it under the terms of the GNU General Public License as published by 7 1.1 christos the Free Software Foundation; either version 3 of the License, or 8 1.1 christos (at your option) any later version. 9 1.1 christos 10 1.1 christos This program is distributed in the hope that it will be useful, 11 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 12 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 1.1 christos GNU General Public License for more details. 14 1.1 christos 15 1.1 christos You should have received a copy of the GNU General Public License 16 1.1 christos along with this program; if not, write to the Free Software 17 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 18 1.1 christos MA 02110-1301, USA. */ 19 1.1 christos 20 1.1 christos /* Some systems, e.g., AIX, may have defined this in header files already 21 1.1 christos included. */ 22 1.1 christos #undef TARGET_PAGE_SIZE 23 1.1 christos #define TARGET_PAGE_SIZE 0x2000 /* 8K. aka NBPG in <sys/param.h> */ 24 1.1 christos /* Note that some SPARCs have 4K pages, some 8K, some others. */ 25 1.1 christos 26 1.1 christos #define SEG_SIZE_SPARC TARGET_PAGE_SIZE 27 1.1 christos #define SEG_SIZE_SUN3 0x20000 /* Resolution of r/w protection hw */ 28 1.1 christos 29 1.1 christos #define TEXT_START_ADDR TARGET_PAGE_SIZE /* Location 0 is not accessible */ 30 1.1 christos #define N_HEADER_IN_TEXT(x) 1 31 1.1 christos 32 1.1 christos /* Non-default definitions of the accessor macros... */ 33 1.1 christos 34 1.1 christos /* Segment size varies on Sun-3 versus Sun-4. */ 35 1.1 christos 36 1.1 christos #define N_SEGSIZE(x) (N_MACHTYPE(x) == M_SPARC? SEG_SIZE_SPARC: \ 37 1.1 christos N_MACHTYPE(x) == M_68020? SEG_SIZE_SUN3: \ 38 1.1 christos /* Guess? */ TARGET_PAGE_SIZE) 39 1.1 christos 40 1.1 christos /* Virtual Address of text segment from the a.out file. For OMAGIC, 41 1.1 christos (almost always "unlinked .o's" these days), should be zero. 42 1.1 christos Sun added a kludge so that shared libraries linked ZMAGIC get 43 1.1 christos an address of zero if a_entry (!!!) is lower than the otherwise 44 1.1 christos expected text address. These kludges have gotta go! 45 1.1 christos For linked files, should reflect reality if we know it. */ 46 1.1 christos 47 1.6 christos #define N_SHARED_LIB(x) ((x)->a_entry < TEXT_START_ADDR \ 48 1.6 christos && (x)->a_text >= EXEC_BYTES_SIZE) 49 1.1 christos 50 1.1 christos /* This differs from the version in aout64.h (which we override by defining 51 1.1 christos it here) only for NMAGIC (we return TEXT_START_ADDR+EXEC_BYTES_SIZE; 52 1.1 christos they return 0). */ 53 1.1 christos 54 1.1 christos #define N_TXTADDR(x) \ 55 1.1 christos (N_MAGIC(x)==OMAGIC? 0 \ 56 1.6 christos : (N_MAGIC(x) == ZMAGIC && (x)->a_entry < TEXT_START_ADDR)? 0 \ 57 1.1 christos : TEXT_START_ADDR+EXEC_BYTES_SIZE) 58 1.1 christos 59 1.1 christos /* When a file is linked against a shared library on SunOS 4, the 60 1.1 christos dynamic bit in the exec header is set, and the first symbol in the 61 1.1 christos symbol table is __DYNAMIC. Its value is the address of the 62 1.1 christos following structure. */ 63 1.1 christos 64 1.1 christos struct external_sun4_dynamic 65 1.1 christos { 66 1.1 christos /* The version number of the structure. SunOS 4.1.x creates files 67 1.1 christos with version number 3, which is what this structure is based on. 68 1.1 christos According to gdb, version 2 is similar. I believe that version 2 69 1.1 christos used a different type of procedure linkage table, and there may 70 1.1 christos have been other differences. */ 71 1.1 christos bfd_byte ld_version[4]; 72 1.1 christos /* The virtual address of a 28 byte structure used in debugging. 73 1.1 christos The contents are filled in at run time by ld.so. */ 74 1.1 christos bfd_byte ldd[4]; 75 1.1 christos /* The virtual address of another structure with information about 76 1.1 christos how to relocate the executable at run time. */ 77 1.1 christos bfd_byte ld[4]; 78 1.1 christos }; 79 1.1 christos 80 1.1 christos /* The size of the debugging structure pointed to by the debugger 81 1.1 christos field of __DYNAMIC. */ 82 1.1 christos #define EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE (24) 83 1.1 christos 84 1.1 christos /* The structure pointed to by the linker field of __DYNAMIC. As far 85 1.1 christos as I can tell, most of the addresses in this structure are offsets 86 1.1 christos within the file, but some are actually virtual addresses. */ 87 1.1 christos 88 1.1 christos struct internal_sun4_dynamic_link 89 1.1 christos { 90 1.1 christos /* Linked list of loaded objects. This is filled in at runtime by 91 1.1 christos ld.so and probably by dlopen. */ 92 1.1 christos unsigned long ld_loaded; 93 1.1 christos 94 1.1 christos /* The address of the list of names of shared objects which must be 95 1.1 christos included at runtime. Each entry in the list is 16 bytes: the 4 96 1.1 christos byte address of the string naming the object (e.g., for -lc this 97 1.1 christos is "c"); 4 bytes of flags--the high bit is whether to search for 98 1.1 christos the object using the library path; the 2 byte major version 99 1.1 christos number; the 2 byte minor version number; the 4 byte address of 100 1.1 christos the next entry in the list (zero if this is the last entry). The 101 1.1 christos version numbers seem to only be non-zero when doing library 102 1.1 christos searching. */ 103 1.1 christos unsigned long ld_need; 104 1.1 christos 105 1.1 christos /* The address of the path to search for the shared objects which 106 1.1 christos must be included. This points to a string in PATH format which 107 1.1 christos is generated from the -L arguments to the linker. According to 108 1.1 christos the man page, ld.so implicitly adds ${LD_LIBRARY_PATH} to the 109 1.1 christos beginning of this string and /lib:/usr/lib:/usr/local/lib to the 110 1.1 christos end. The string is terminated by a null byte. This field is 111 1.1 christos zero if there is no additional path. */ 112 1.1 christos unsigned long ld_rules; 113 1.1 christos 114 1.1 christos /* The address of the global offset table. This appears to be a 115 1.1 christos virtual address, not a file offset. The first entry in the 116 1.1 christos global offset table seems to be the virtual address of the 117 1.1 christos sun4_dynamic structure (the same value as the __DYNAMIC symbol). 118 1.1 christos The global offset table is used for PIC code to hold the 119 1.1 christos addresses of variables. A dynamically linked file which does not 120 1.1 christos itself contain PIC code has a four byte global offset table. */ 121 1.1 christos unsigned long ld_got; 122 1.1 christos 123 1.1 christos /* The address of the procedure linkage table. This appears to be a 124 1.1 christos virtual address, not a file offset. 125 1.1 christos 126 1.1 christos On a SPARC, the table is composed of 12 byte entries, each of 127 1.1 christos which consists of three instructions. The first entry is 128 1.1 christos sethi %hi(0),%g1 129 1.1 christos jmp %g1 130 1.1 christos nop 131 1.1 christos These instructions are changed by ld.so into a jump directly into 132 1.1 christos ld.so itself. Each subsequent entry is 133 1.1 christos save %sp, -96, %sp 134 1.1 christos call <address of first entry in procedure linkage table> 135 1.1 christos <reloc_number | 0x01000000> 136 1.1 christos The reloc_number is the number of the reloc to use to resolve 137 1.1 christos this entry. The reloc will be a JMP_SLOT reloc against some 138 1.1 christos symbol that is not defined in this object file but should be 139 1.1 christos defined in a shared object (if it is not, ld.so will report a 140 1.1 christos runtime error and exit). The constant 0x010000000 turns the 141 1.1 christos reloc number into a sethi of %g0, which does nothing since %g0 is 142 1.1 christos hardwired to zero. 143 1.1 christos 144 1.1 christos When one of these entries is executed, it winds up calling into 145 1.1 christos ld.so. ld.so looks at the reloc number, available via the return 146 1.1 christos address, to determine which entry this is. It then looks at the 147 1.1 christos reloc and patches up the entry in the table into a sethi and jmp 148 1.1 christos to the real address followed by a nop. This means that the reloc 149 1.1 christos lookup only has to happen once, and it also means that the 150 1.1 christos relocation only needs to be done if the function is actually 151 1.1 christos called. The relocation is expensive because ld.so must look up 152 1.1 christos the symbol by name. 153 1.1 christos 154 1.1 christos The size of the procedure linkage table is given by the ld_plt_sz 155 1.1 christos field. */ 156 1.1 christos unsigned long ld_plt; 157 1.1 christos 158 1.1 christos /* The address of the relocs. These are in the same format as 159 1.1 christos ordinary relocs. Symbol index numbers refer to the symbols 160 1.1 christos pointed to by ld_stab. I think the only way to determine the 161 1.1 christos number of relocs is to assume that all the bytes from ld_rel to 162 1.1 christos ld_hash contain reloc entries. */ 163 1.1 christos unsigned long ld_rel; 164 1.1 christos 165 1.1 christos /* The address of a hash table of symbols. The hash table has 166 1.1 christos roughly the same number of entries as there are dynamic symbols; 167 1.1 christos I think the only way to get the exact size is to assume that 168 1.1 christos every byte from ld_hash to ld_stab is devoted to the hash table. 169 1.1 christos 170 1.1 christos Each entry in the hash table is eight bytes. The first four 171 1.1 christos bytes are a symbol index into the dynamic symbols. The second 172 1.1 christos four bytes are the index of the next hash table entry in the 173 1.1 christos bucket. The ld_buckets field gives the number of buckets, say B. 174 1.1 christos The first B entries in the hash table each start a bucket which 175 1.1 christos is chained through the second four bytes of each entry. A value 176 1.1 christos of zero ends the chain. 177 1.1 christos 178 1.1 christos The hash function is simply 179 1.1 christos h = 0; 180 1.1 christos while (*string != '\0') 181 1.1 christos h = (h << 1) + *string++; 182 1.1 christos h &= 0x7fffffff; 183 1.1 christos 184 1.1 christos To look up a symbol, compute the hash value of the name. Take 185 1.1 christos the modulos of hash value and the number of buckets. Start at 186 1.1 christos that entry in the hash table. See if the symbol (from the first 187 1.1 christos four bytes of the hash table entry) has the name you are looking 188 1.1 christos for. If not, use the chain field (the second four bytes of the 189 1.1 christos hash table entry) to move on to the next entry in this bucket. 190 1.1 christos If the chain field is zero you have reached the end of the 191 1.1 christos bucket, and the symbol is not in the hash table. */ 192 1.1 christos unsigned long ld_hash; 193 1.1 christos 194 1.1 christos /* The address of the symbol table. This is a list of 195 1.1 christos external_nlist structures. The string indices are relative to 196 1.1 christos the ld_symbols field. I think the only way to determine the 197 1.1 christos number of symbols is to assume that all the bytes between ld_stab 198 1.1 christos and ld_symbols are external_nlist structures. */ 199 1.1 christos unsigned long ld_stab; 200 1.1 christos 201 1.1 christos /* I don't know what this is for. It seems to always be zero. */ 202 1.1 christos unsigned long ld_stab_hash; 203 1.1 christos 204 1.1 christos /* The number of buckets in the hash table. */ 205 1.1 christos unsigned long ld_buckets; 206 1.1 christos 207 1.1 christos /* The address of the symbol string table. The first string in this 208 1.1 christos string table need not be the empty string. */ 209 1.1 christos unsigned long ld_symbols; 210 1.1 christos 211 1.1 christos /* The size in bytes of the symbol string table. */ 212 1.1 christos unsigned long ld_symb_size; 213 1.1 christos 214 1.1 christos /* The size in bytes of the text segment. */ 215 1.1 christos unsigned long ld_text; 216 1.1 christos 217 1.1 christos /* The size in bytes of the procedure linkage table. */ 218 1.1 christos unsigned long ld_plt_sz; 219 1.1 christos }; 220 1.1 christos 221 1.1 christos /* The external form of the structure. */ 222 1.1 christos 223 1.1 christos struct external_sun4_dynamic_link 224 1.1 christos { 225 1.1 christos bfd_byte ld_loaded[4]; 226 1.1 christos bfd_byte ld_need[4]; 227 1.1 christos bfd_byte ld_rules[4]; 228 1.1 christos bfd_byte ld_got[4]; 229 1.1 christos bfd_byte ld_plt[4]; 230 1.1 christos bfd_byte ld_rel[4]; 231 1.1 christos bfd_byte ld_hash[4]; 232 1.1 christos bfd_byte ld_stab[4]; 233 1.1 christos bfd_byte ld_stab_hash[4]; 234 1.1 christos bfd_byte ld_buckets[4]; 235 1.1 christos bfd_byte ld_symbols[4]; 236 1.1 christos bfd_byte ld_symb_size[4]; 237 1.1 christos bfd_byte ld_text[4]; 238 1.1 christos bfd_byte ld_plt_sz[4]; 239 1.1 christos }; 240