xref: /openbsd-src/gnu/usr.bin/binutils/include/aout/sun4.h (revision 5f210c2af8ad7a823d98e333e3a6e3d7999783f5)
1*5f210c2aSfgsch /* SPARC-specific values for a.out files
2*5f210c2aSfgsch 
3*5f210c2aSfgsch    Copyright 2001 Free Software Foundation, Inc.
4*5f210c2aSfgsch 
5*5f210c2aSfgsch    This program is free software; you can redistribute it and/or modify
6*5f210c2aSfgsch    it under the terms of the GNU General Public License as published by
7*5f210c2aSfgsch    the Free Software Foundation; either version 2 of the License, or
8*5f210c2aSfgsch    (at your option) any later version.
9*5f210c2aSfgsch 
10*5f210c2aSfgsch    This program is distributed in the hope that it will be useful,
11*5f210c2aSfgsch    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*5f210c2aSfgsch    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*5f210c2aSfgsch    GNU General Public License for more details.
14*5f210c2aSfgsch 
15*5f210c2aSfgsch    You should have received a copy of the GNU General Public License
16*5f210c2aSfgsch    along with this program; if not, write to the Free Software
17*5f210c2aSfgsch    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
182159047fSniklas 
192159047fSniklas /* Some systems, e.g., AIX, may have defined this in header files already
202159047fSniklas    included.  */
212159047fSniklas #undef  TARGET_PAGE_SIZE
222159047fSniklas #define TARGET_PAGE_SIZE	0x2000		/* 8K.  aka NBPG in <sys/param.h> */
232159047fSniklas /* Note that some SPARCs have 4K pages, some 8K, some others.  */
242159047fSniklas 
252159047fSniklas #define SEG_SIZE_SPARC	TARGET_PAGE_SIZE
262159047fSniklas #define	SEG_SIZE_SUN3	0x20000		/* Resolution of r/w protection hw */
272159047fSniklas 
282159047fSniklas #define TEXT_START_ADDR	TARGET_PAGE_SIZE	/* Location 0 is not accessible */
292159047fSniklas #define N_HEADER_IN_TEXT(x) 1
302159047fSniklas 
312159047fSniklas /* Non-default definitions of the accessor macros... */
322159047fSniklas 
332159047fSniklas /* Segment size varies on Sun-3 versus Sun-4.  */
342159047fSniklas 
352159047fSniklas #define N_SEGSIZE(x)	(N_MACHTYPE(x) == M_SPARC?	SEG_SIZE_SPARC:	\
362159047fSniklas 			 N_MACHTYPE(x) == M_68020?	SEG_SIZE_SUN3:	\
372159047fSniklas 			/* Guess? */			TARGET_PAGE_SIZE)
382159047fSniklas 
392159047fSniklas /* Virtual Address of text segment from the a.out file.  For OMAGIC,
402159047fSniklas    (almost always "unlinked .o's" these days), should be zero.
412159047fSniklas    Sun added a kludge so that shared libraries linked ZMAGIC get
422159047fSniklas    an address of zero if a_entry (!!!) is lower than the otherwise
432159047fSniklas    expected text address.  These kludges have gotta go!
442159047fSniklas    For linked files, should reflect reality if we know it.  */
452159047fSniklas 
462159047fSniklas /* This differs from the version in aout64.h (which we override by defining
472159047fSniklas    it here) only for NMAGIC (we return TEXT_START_ADDR+EXEC_BYTES_SIZE;
482159047fSniklas    they return 0).  */
492159047fSniklas 
502159047fSniklas #define N_TXTADDR(x) \
512159047fSniklas     (N_MAGIC(x)==OMAGIC? 0 \
522159047fSniklas      : (N_MAGIC(x) == ZMAGIC && (x).a_entry < TEXT_START_ADDR)? 0 \
532159047fSniklas      : TEXT_START_ADDR+EXEC_BYTES_SIZE)
542159047fSniklas 
552159047fSniklas /* When a file is linked against a shared library on SunOS 4, the
562159047fSniklas    dynamic bit in the exec header is set, and the first symbol in the
572159047fSniklas    symbol table is __DYNAMIC.  Its value is the address of the
582159047fSniklas    following structure.  */
592159047fSniklas 
602159047fSniklas struct external_sun4_dynamic
612159047fSniklas {
622159047fSniklas   /* The version number of the structure.  SunOS 4.1.x creates files
632159047fSniklas      with version number 3, which is what this structure is based on.
642159047fSniklas      According to gdb, version 2 is similar.  I believe that version 2
652159047fSniklas      used a different type of procedure linkage table, and there may
662159047fSniklas      have been other differences.  */
672159047fSniklas   bfd_byte ld_version[4];
682159047fSniklas   /* The virtual address of a 28 byte structure used in debugging.
692159047fSniklas      The contents are filled in at run time by ld.so.  */
702159047fSniklas   bfd_byte ldd[4];
712159047fSniklas   /* The virtual address of another structure with information about
722159047fSniklas      how to relocate the executable at run time.  */
732159047fSniklas   bfd_byte ld[4];
742159047fSniklas };
752159047fSniklas 
762159047fSniklas /* The size of the debugging structure pointed to by the debugger
772159047fSniklas    field of __DYNAMIC.  */
782159047fSniklas #define EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE (24)
792159047fSniklas 
802159047fSniklas /* The structure pointed to by the linker field of __DYNAMIC.  As far
812159047fSniklas    as I can tell, most of the addresses in this structure are offsets
822159047fSniklas    within the file, but some are actually virtual addresses.  */
832159047fSniklas 
842159047fSniklas struct internal_sun4_dynamic_link
852159047fSniklas {
862159047fSniklas   /* Linked list of loaded objects.  This is filled in at runtime by
872159047fSniklas      ld.so and probably by dlopen.  */
882159047fSniklas   unsigned long ld_loaded;
892159047fSniklas 
902159047fSniklas   /* The address of the list of names of shared objects which must be
912159047fSniklas      included at runtime.  Each entry in the list is 16 bytes: the 4
922159047fSniklas      byte address of the string naming the object (e.g., for -lc this
932159047fSniklas      is "c"); 4 bytes of flags--the high bit is whether to search for
942159047fSniklas      the object using the library path; the 2 byte major version
952159047fSniklas      number; the 2 byte minor version number; the 4 byte address of
962159047fSniklas      the next entry in the list (zero if this is the last entry).  The
972159047fSniklas      version numbers seem to only be non-zero when doing library
982159047fSniklas      searching.  */
992159047fSniklas   unsigned long ld_need;
1002159047fSniklas 
1012159047fSniklas   /* The address of the path to search for the shared objects which
1022159047fSniklas      must be included.  This points to a string in PATH format which
1032159047fSniklas      is generated from the -L arguments to the linker.  According to
1042159047fSniklas      the man page, ld.so implicitly adds ${LD_LIBRARY_PATH} to the
1052159047fSniklas      beginning of this string and /lib:/usr/lib:/usr/local/lib to the
1062159047fSniklas      end.  The string is terminated by a null byte.  This field is
1072159047fSniklas      zero if there is no additional path.  */
1082159047fSniklas   unsigned long ld_rules;
1092159047fSniklas 
1102159047fSniklas   /* The address of the global offset table.  This appears to be a
1112159047fSniklas      virtual address, not a file offset.  The first entry in the
1122159047fSniklas      global offset table seems to be the virtual address of the
1132159047fSniklas      sun4_dynamic structure (the same value as the __DYNAMIC symbol).
1142159047fSniklas      The global offset table is used for PIC code to hold the
1152159047fSniklas      addresses of variables.  A dynamically linked file which does not
1162159047fSniklas      itself contain PIC code has a four byte global offset table.  */
1172159047fSniklas   unsigned long ld_got;
1182159047fSniklas 
1192159047fSniklas   /* The address of the procedure linkage table.  This appears to be a
1202159047fSniklas      virtual address, not a file offset.
1212159047fSniklas 
1222159047fSniklas      On a SPARC, the table is composed of 12 byte entries, each of
1232159047fSniklas      which consists of three instructions.  The first entry is
1242159047fSniklas          sethi %hi(0),%g1
1252159047fSniklas 	 jmp %g1
1262159047fSniklas 	 nop
1272159047fSniklas      These instructions are changed by ld.so into a jump directly into
1282159047fSniklas      ld.so itself.  Each subsequent entry is
1292159047fSniklas          save %sp, -96, %sp
1302159047fSniklas 	 call <address of first entry in procedure linkage table>
1312159047fSniklas 	 <reloc_number | 0x01000000>
1322159047fSniklas      The reloc_number is the number of the reloc to use to resolve
1332159047fSniklas      this entry.  The reloc will be a JMP_SLOT reloc against some
1342159047fSniklas      symbol that is not defined in this object file but should be
1352159047fSniklas      defined in a shared object (if it is not, ld.so will report a
1362159047fSniklas      runtime error and exit).  The constant 0x010000000 turns the
1372159047fSniklas      reloc number into a sethi of %g0, which does nothing since %g0 is
1382159047fSniklas      hardwired to zero.
1392159047fSniklas 
1402159047fSniklas      When one of these entries is executed, it winds up calling into
1412159047fSniklas      ld.so.  ld.so looks at the reloc number, available via the return
1422159047fSniklas      address, to determine which entry this is.  It then looks at the
1432159047fSniklas      reloc and patches up the entry in the table into a sethi and jmp
1442159047fSniklas      to the real address followed by a nop.  This means that the reloc
1452159047fSniklas      lookup only has to happen once, and it also means that the
1462159047fSniklas      relocation only needs to be done if the function is actually
1472159047fSniklas      called.  The relocation is expensive because ld.so must look up
1482159047fSniklas      the symbol by name.
1492159047fSniklas 
1502159047fSniklas      The size of the procedure linkage table is given by the ld_plt_sz
1512159047fSniklas      field.  */
1522159047fSniklas   unsigned long ld_plt;
1532159047fSniklas 
1542159047fSniklas   /* The address of the relocs.  These are in the same format as
1552159047fSniklas      ordinary relocs.  Symbol index numbers refer to the symbols
1562159047fSniklas      pointed to by ld_stab.  I think the only way to determine the
1572159047fSniklas      number of relocs is to assume that all the bytes from ld_rel to
1582159047fSniklas      ld_hash contain reloc entries.  */
1592159047fSniklas   unsigned long ld_rel;
1602159047fSniklas 
1612159047fSniklas   /* The address of a hash table of symbols.  The hash table has
1622159047fSniklas      roughly the same number of entries as there are dynamic symbols;
1632159047fSniklas      I think the only way to get the exact size is to assume that
1642159047fSniklas      every byte from ld_hash to ld_stab is devoted to the hash table.
1652159047fSniklas 
1662159047fSniklas      Each entry in the hash table is eight bytes.  The first four
1672159047fSniklas      bytes are a symbol index into the dynamic symbols.  The second
1682159047fSniklas      four bytes are the index of the next hash table entry in the
1692159047fSniklas      bucket.  The ld_buckets field gives the number of buckets, say B.
1702159047fSniklas      The first B entries in the hash table each start a bucket which
1712159047fSniklas      is chained through the second four bytes of each entry.  A value
1722159047fSniklas      of zero ends the chain.
1732159047fSniklas 
1742159047fSniklas      The hash function is simply
1752159047fSniklas          h = 0;
1762159047fSniklas          while (*string != '\0')
1772159047fSniklas 	   h = (h << 1) + *string++;
1782159047fSniklas 	 h &= 0x7fffffff;
1792159047fSniklas 
1802159047fSniklas      To look up a symbol, compute the hash value of the name.  Take
1812159047fSniklas      the modulos of hash value and the number of buckets.  Start at
1822159047fSniklas      that entry in the hash table.  See if the symbol (from the first
1832159047fSniklas      four bytes of the hash table entry) has the name you are looking
1842159047fSniklas      for.  If not, use the chain field (the second four bytes of the
1852159047fSniklas      hash table entry) to move on to the next entry in this bucket.
1862159047fSniklas      If the chain field is zero you have reached the end of the
1872159047fSniklas      bucket, and the symbol is not in the hash table.  */
1882159047fSniklas   unsigned long ld_hash;
1892159047fSniklas 
1902159047fSniklas   /* The address of the symbol table.  This is a list of
1912159047fSniklas      external_nlist structures.  The string indices are relative to
1922159047fSniklas      the ld_symbols field.  I think the only way to determine the
1932159047fSniklas      number of symbols is to assume that all the bytes between ld_stab
1942159047fSniklas      and ld_symbols are external_nlist structures.  */
1952159047fSniklas   unsigned long ld_stab;
1962159047fSniklas 
1972159047fSniklas   /* I don't know what this is for.  It seems to always be zero.  */
1982159047fSniklas   unsigned long ld_stab_hash;
1992159047fSniklas 
2002159047fSniklas   /* The number of buckets in the hash table.  */
2012159047fSniklas   unsigned long ld_buckets;
2022159047fSniklas 
2032159047fSniklas   /* The address of the symbol string table.  The first string in this
2042159047fSniklas      string table need not be the empty string.  */
2052159047fSniklas   unsigned long ld_symbols;
2062159047fSniklas 
2072159047fSniklas   /* The size in bytes of the symbol string table.  */
2082159047fSniklas   unsigned long ld_symb_size;
2092159047fSniklas 
2102159047fSniklas   /* The size in bytes of the text segment.  */
2112159047fSniklas   unsigned long ld_text;
2122159047fSniklas 
2132159047fSniklas   /* The size in bytes of the procedure linkage table.  */
2142159047fSniklas   unsigned long ld_plt_sz;
2152159047fSniklas };
2162159047fSniklas 
2172159047fSniklas /* The external form of the structure.  */
2182159047fSniklas 
2192159047fSniklas struct external_sun4_dynamic_link
2202159047fSniklas {
2212159047fSniklas   bfd_byte ld_loaded[4];
2222159047fSniklas   bfd_byte ld_need[4];
2232159047fSniklas   bfd_byte ld_rules[4];
2242159047fSniklas   bfd_byte ld_got[4];
2252159047fSniklas   bfd_byte ld_plt[4];
2262159047fSniklas   bfd_byte ld_rel[4];
2272159047fSniklas   bfd_byte ld_hash[4];
2282159047fSniklas   bfd_byte ld_stab[4];
2292159047fSniklas   bfd_byte ld_stab_hash[4];
2302159047fSniklas   bfd_byte ld_buckets[4];
2312159047fSniklas   bfd_byte ld_symbols[4];
2322159047fSniklas   bfd_byte ld_symb_size[4];
2332159047fSniklas   bfd_byte ld_text[4];
2342159047fSniklas   bfd_byte ld_plt_sz[4];
2352159047fSniklas };
236