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