189857Sobrien/* SPARC-specific values for a.out files 259024Sobrien 389857Sobrien Copyright 2001 Free Software Foundation, Inc. 489857Sobrien 589857Sobrien This program is free software; you can redistribute it and/or modify 689857Sobrien it under the terms of the GNU General Public License as published by 789857Sobrien the Free Software Foundation; either version 2 of the License, or 889857Sobrien (at your option) any later version. 989857Sobrien 1089857Sobrien This program is distributed in the hope that it will be useful, 1189857Sobrien but WITHOUT ANY WARRANTY; without even the implied warranty of 1289857Sobrien MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1389857Sobrien GNU General Public License for more details. 1489857Sobrien 1589857Sobrien You should have received a copy of the GNU General Public License 1689857Sobrien along with this program; if not, write to the Free Software 17218822Sdim Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 1889857Sobrien 1959024Sobrien/* Some systems, e.g., AIX, may have defined this in header files already 2059024Sobrien included. */ 2189857Sobrien#undef TARGET_PAGE_SIZE 2259024Sobrien#define TARGET_PAGE_SIZE 0x2000 /* 8K. aka NBPG in <sys/param.h> */ 2359024Sobrien/* Note that some SPARCs have 4K pages, some 8K, some others. */ 2459024Sobrien 2559024Sobrien#define SEG_SIZE_SPARC TARGET_PAGE_SIZE 2659024Sobrien#define SEG_SIZE_SUN3 0x20000 /* Resolution of r/w protection hw */ 2759024Sobrien 2859024Sobrien#define TEXT_START_ADDR TARGET_PAGE_SIZE /* Location 0 is not accessible */ 2959024Sobrien#define N_HEADER_IN_TEXT(x) 1 3059024Sobrien 3159024Sobrien/* Non-default definitions of the accessor macros... */ 3259024Sobrien 3359024Sobrien/* Segment size varies on Sun-3 versus Sun-4. */ 3459024Sobrien 3559024Sobrien#define N_SEGSIZE(x) (N_MACHTYPE(x) == M_SPARC? SEG_SIZE_SPARC: \ 3659024Sobrien N_MACHTYPE(x) == M_68020? SEG_SIZE_SUN3: \ 3759024Sobrien /* Guess? */ TARGET_PAGE_SIZE) 3859024Sobrien 3959024Sobrien/* Virtual Address of text segment from the a.out file. For OMAGIC, 4059024Sobrien (almost always "unlinked .o's" these days), should be zero. 4159024Sobrien Sun added a kludge so that shared libraries linked ZMAGIC get 4259024Sobrien an address of zero if a_entry (!!!) is lower than the otherwise 4359024Sobrien expected text address. These kludges have gotta go! 4459024Sobrien For linked files, should reflect reality if we know it. */ 4559024Sobrien 4659024Sobrien/* This differs from the version in aout64.h (which we override by defining 4759024Sobrien it here) only for NMAGIC (we return TEXT_START_ADDR+EXEC_BYTES_SIZE; 4859024Sobrien they return 0). */ 4959024Sobrien 5059024Sobrien#define N_TXTADDR(x) \ 5159024Sobrien (N_MAGIC(x)==OMAGIC? 0 \ 5259024Sobrien : (N_MAGIC(x) == ZMAGIC && (x).a_entry < TEXT_START_ADDR)? 0 \ 5359024Sobrien : TEXT_START_ADDR+EXEC_BYTES_SIZE) 5459024Sobrien 5559024Sobrien/* When a file is linked against a shared library on SunOS 4, the 5659024Sobrien dynamic bit in the exec header is set, and the first symbol in the 5759024Sobrien symbol table is __DYNAMIC. Its value is the address of the 5859024Sobrien following structure. */ 5959024Sobrien 6059024Sobrienstruct external_sun4_dynamic 6159024Sobrien{ 6259024Sobrien /* The version number of the structure. SunOS 4.1.x creates files 6359024Sobrien with version number 3, which is what this structure is based on. 6459024Sobrien According to gdb, version 2 is similar. I believe that version 2 6559024Sobrien used a different type of procedure linkage table, and there may 6659024Sobrien have been other differences. */ 6759024Sobrien bfd_byte ld_version[4]; 6859024Sobrien /* The virtual address of a 28 byte structure used in debugging. 6959024Sobrien The contents are filled in at run time by ld.so. */ 7059024Sobrien bfd_byte ldd[4]; 7159024Sobrien /* The virtual address of another structure with information about 7259024Sobrien how to relocate the executable at run time. */ 7359024Sobrien bfd_byte ld[4]; 7459024Sobrien}; 7559024Sobrien 7659024Sobrien/* The size of the debugging structure pointed to by the debugger 7759024Sobrien field of __DYNAMIC. */ 7859024Sobrien#define EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE (24) 7959024Sobrien 8059024Sobrien/* The structure pointed to by the linker field of __DYNAMIC. As far 8159024Sobrien as I can tell, most of the addresses in this structure are offsets 8259024Sobrien within the file, but some are actually virtual addresses. */ 8359024Sobrien 8459024Sobrienstruct internal_sun4_dynamic_link 8559024Sobrien{ 8659024Sobrien /* Linked list of loaded objects. This is filled in at runtime by 8759024Sobrien ld.so and probably by dlopen. */ 8859024Sobrien unsigned long ld_loaded; 8959024Sobrien 9059024Sobrien /* The address of the list of names of shared objects which must be 9159024Sobrien included at runtime. Each entry in the list is 16 bytes: the 4 9259024Sobrien byte address of the string naming the object (e.g., for -lc this 9359024Sobrien is "c"); 4 bytes of flags--the high bit is whether to search for 9459024Sobrien the object using the library path; the 2 byte major version 9559024Sobrien number; the 2 byte minor version number; the 4 byte address of 9659024Sobrien the next entry in the list (zero if this is the last entry). The 9759024Sobrien version numbers seem to only be non-zero when doing library 9859024Sobrien searching. */ 9959024Sobrien unsigned long ld_need; 10059024Sobrien 10159024Sobrien /* The address of the path to search for the shared objects which 10259024Sobrien must be included. This points to a string in PATH format which 10359024Sobrien is generated from the -L arguments to the linker. According to 10459024Sobrien the man page, ld.so implicitly adds ${LD_LIBRARY_PATH} to the 10559024Sobrien beginning of this string and /lib:/usr/lib:/usr/local/lib to the 10659024Sobrien end. The string is terminated by a null byte. This field is 10759024Sobrien zero if there is no additional path. */ 10859024Sobrien unsigned long ld_rules; 10959024Sobrien 11059024Sobrien /* The address of the global offset table. This appears to be a 11159024Sobrien virtual address, not a file offset. The first entry in the 11259024Sobrien global offset table seems to be the virtual address of the 11359024Sobrien sun4_dynamic structure (the same value as the __DYNAMIC symbol). 11459024Sobrien The global offset table is used for PIC code to hold the 11559024Sobrien addresses of variables. A dynamically linked file which does not 11659024Sobrien itself contain PIC code has a four byte global offset table. */ 11759024Sobrien unsigned long ld_got; 11859024Sobrien 11959024Sobrien /* The address of the procedure linkage table. This appears to be a 12059024Sobrien virtual address, not a file offset. 12159024Sobrien 12259024Sobrien On a SPARC, the table is composed of 12 byte entries, each of 12359024Sobrien which consists of three instructions. The first entry is 12459024Sobrien sethi %hi(0),%g1 12559024Sobrien jmp %g1 12659024Sobrien nop 12759024Sobrien These instructions are changed by ld.so into a jump directly into 12859024Sobrien ld.so itself. Each subsequent entry is 12959024Sobrien save %sp, -96, %sp 13059024Sobrien call <address of first entry in procedure linkage table> 13159024Sobrien <reloc_number | 0x01000000> 13259024Sobrien The reloc_number is the number of the reloc to use to resolve 13359024Sobrien this entry. The reloc will be a JMP_SLOT reloc against some 13459024Sobrien symbol that is not defined in this object file but should be 13559024Sobrien defined in a shared object (if it is not, ld.so will report a 13659024Sobrien runtime error and exit). The constant 0x010000000 turns the 13759024Sobrien reloc number into a sethi of %g0, which does nothing since %g0 is 13859024Sobrien hardwired to zero. 13959024Sobrien 14059024Sobrien When one of these entries is executed, it winds up calling into 14159024Sobrien ld.so. ld.so looks at the reloc number, available via the return 14259024Sobrien address, to determine which entry this is. It then looks at the 14359024Sobrien reloc and patches up the entry in the table into a sethi and jmp 14459024Sobrien to the real address followed by a nop. This means that the reloc 14559024Sobrien lookup only has to happen once, and it also means that the 14659024Sobrien relocation only needs to be done if the function is actually 14759024Sobrien called. The relocation is expensive because ld.so must look up 14859024Sobrien the symbol by name. 14959024Sobrien 15059024Sobrien The size of the procedure linkage table is given by the ld_plt_sz 15159024Sobrien field. */ 15259024Sobrien unsigned long ld_plt; 15359024Sobrien 15459024Sobrien /* The address of the relocs. These are in the same format as 15559024Sobrien ordinary relocs. Symbol index numbers refer to the symbols 15659024Sobrien pointed to by ld_stab. I think the only way to determine the 15759024Sobrien number of relocs is to assume that all the bytes from ld_rel to 15859024Sobrien ld_hash contain reloc entries. */ 15959024Sobrien unsigned long ld_rel; 16059024Sobrien 16159024Sobrien /* The address of a hash table of symbols. The hash table has 16259024Sobrien roughly the same number of entries as there are dynamic symbols; 16359024Sobrien I think the only way to get the exact size is to assume that 16459024Sobrien every byte from ld_hash to ld_stab is devoted to the hash table. 16559024Sobrien 16659024Sobrien Each entry in the hash table is eight bytes. The first four 16759024Sobrien bytes are a symbol index into the dynamic symbols. The second 16859024Sobrien four bytes are the index of the next hash table entry in the 16959024Sobrien bucket. The ld_buckets field gives the number of buckets, say B. 17059024Sobrien The first B entries in the hash table each start a bucket which 17159024Sobrien is chained through the second four bytes of each entry. A value 17259024Sobrien of zero ends the chain. 17359024Sobrien 17459024Sobrien The hash function is simply 17559024Sobrien h = 0; 17659024Sobrien while (*string != '\0') 17759024Sobrien h = (h << 1) + *string++; 17859024Sobrien h &= 0x7fffffff; 17959024Sobrien 18059024Sobrien To look up a symbol, compute the hash value of the name. Take 18159024Sobrien the modulos of hash value and the number of buckets. Start at 18259024Sobrien that entry in the hash table. See if the symbol (from the first 18359024Sobrien four bytes of the hash table entry) has the name you are looking 18459024Sobrien for. If not, use the chain field (the second four bytes of the 18559024Sobrien hash table entry) to move on to the next entry in this bucket. 18659024Sobrien If the chain field is zero you have reached the end of the 18759024Sobrien bucket, and the symbol is not in the hash table. */ 18859024Sobrien unsigned long ld_hash; 18959024Sobrien 19059024Sobrien /* The address of the symbol table. This is a list of 19159024Sobrien external_nlist structures. The string indices are relative to 19259024Sobrien the ld_symbols field. I think the only way to determine the 19359024Sobrien number of symbols is to assume that all the bytes between ld_stab 19459024Sobrien and ld_symbols are external_nlist structures. */ 19559024Sobrien unsigned long ld_stab; 19659024Sobrien 19759024Sobrien /* I don't know what this is for. It seems to always be zero. */ 19859024Sobrien unsigned long ld_stab_hash; 19959024Sobrien 20059024Sobrien /* The number of buckets in the hash table. */ 20159024Sobrien unsigned long ld_buckets; 20259024Sobrien 20359024Sobrien /* The address of the symbol string table. The first string in this 20459024Sobrien string table need not be the empty string. */ 20559024Sobrien unsigned long ld_symbols; 20659024Sobrien 20759024Sobrien /* The size in bytes of the symbol string table. */ 20859024Sobrien unsigned long ld_symb_size; 20959024Sobrien 21059024Sobrien /* The size in bytes of the text segment. */ 21159024Sobrien unsigned long ld_text; 21259024Sobrien 21359024Sobrien /* The size in bytes of the procedure linkage table. */ 21459024Sobrien unsigned long ld_plt_sz; 21559024Sobrien}; 21659024Sobrien 21759024Sobrien/* The external form of the structure. */ 21859024Sobrien 21959024Sobrienstruct external_sun4_dynamic_link 22059024Sobrien{ 22159024Sobrien bfd_byte ld_loaded[4]; 22259024Sobrien bfd_byte ld_need[4]; 22359024Sobrien bfd_byte ld_rules[4]; 22459024Sobrien bfd_byte ld_got[4]; 22559024Sobrien bfd_byte ld_plt[4]; 22659024Sobrien bfd_byte ld_rel[4]; 22759024Sobrien bfd_byte ld_hash[4]; 22859024Sobrien bfd_byte ld_stab[4]; 22959024Sobrien bfd_byte ld_stab_hash[4]; 23059024Sobrien bfd_byte ld_buckets[4]; 23159024Sobrien bfd_byte ld_symbols[4]; 23259024Sobrien bfd_byte ld_symb_size[4]; 23359024Sobrien bfd_byte ld_text[4]; 23459024Sobrien bfd_byte ld_plt_sz[4]; 23559024Sobrien}; 236