xref: /netbsd-src/external/gpl3/binutils/dist/include/aout/sun4.h (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
12a6b7db3Sskrll /* SPARC-specific values for a.out files
22a6b7db3Sskrll 
3*cb63e24eSchristos    Copyright (C) 2001-2024 Free Software Foundation, Inc.
42a6b7db3Sskrll 
52a6b7db3Sskrll    This program is free software; you can redistribute it and/or modify
62a6b7db3Sskrll    it under the terms of the GNU General Public License as published by
745548106Schristos    the Free Software Foundation; either version 3 of the License, or
82a6b7db3Sskrll    (at your option) any later version.
92a6b7db3Sskrll 
102a6b7db3Sskrll    This program is distributed in the hope that it will be useful,
112a6b7db3Sskrll    but WITHOUT ANY WARRANTY; without even the implied warranty of
122a6b7db3Sskrll    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
132a6b7db3Sskrll    GNU General Public License for more details.
142a6b7db3Sskrll 
152a6b7db3Sskrll    You should have received a copy of the GNU General Public License
162a6b7db3Sskrll    along with this program; if not, write to the Free Software
1745548106Schristos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
1845548106Schristos    MA 02110-1301, USA.  */
192a6b7db3Sskrll 
202a6b7db3Sskrll /* Some systems, e.g., AIX, may have defined this in header files already
212a6b7db3Sskrll    included.  */
222a6b7db3Sskrll #undef  TARGET_PAGE_SIZE
232a6b7db3Sskrll #define TARGET_PAGE_SIZE	0x2000		/* 8K.  aka NBPG in <sys/param.h> */
242a6b7db3Sskrll /* Note that some SPARCs have 4K pages, some 8K, some others.  */
252a6b7db3Sskrll 
262a6b7db3Sskrll #define SEG_SIZE_SPARC	TARGET_PAGE_SIZE
272a6b7db3Sskrll #define	SEG_SIZE_SUN3	0x20000		/* Resolution of r/w protection hw */
282a6b7db3Sskrll 
292a6b7db3Sskrll #define TEXT_START_ADDR	TARGET_PAGE_SIZE	/* Location 0 is not accessible */
302a6b7db3Sskrll #define N_HEADER_IN_TEXT(x) 1
312a6b7db3Sskrll 
322a6b7db3Sskrll /* Non-default definitions of the accessor macros... */
332a6b7db3Sskrll 
342a6b7db3Sskrll /* Segment size varies on Sun-3 versus Sun-4.  */
352a6b7db3Sskrll 
362a6b7db3Sskrll #define N_SEGSIZE(x)	(N_MACHTYPE(x) == M_SPARC?	SEG_SIZE_SPARC:	\
372a6b7db3Sskrll 			 N_MACHTYPE(x) == M_68020?	SEG_SIZE_SUN3:	\
382a6b7db3Sskrll 			/* Guess? */			TARGET_PAGE_SIZE)
392a6b7db3Sskrll 
402a6b7db3Sskrll /* Virtual Address of text segment from the a.out file.  For OMAGIC,
412a6b7db3Sskrll    (almost always "unlinked .o's" these days), should be zero.
422a6b7db3Sskrll    Sun added a kludge so that shared libraries linked ZMAGIC get
432a6b7db3Sskrll    an address of zero if a_entry (!!!) is lower than the otherwise
442a6b7db3Sskrll    expected text address.  These kludges have gotta go!
452a6b7db3Sskrll    For linked files, should reflect reality if we know it.  */
462a6b7db3Sskrll 
478cbf5cb7Schristos #define N_SHARED_LIB(x) ((x)->a_entry < TEXT_START_ADDR \
488cbf5cb7Schristos 			 && (x)->a_text >= EXEC_BYTES_SIZE)
4945548106Schristos 
502a6b7db3Sskrll /* This differs from the version in aout64.h (which we override by defining
512a6b7db3Sskrll    it here) only for NMAGIC (we return TEXT_START_ADDR+EXEC_BYTES_SIZE;
522a6b7db3Sskrll    they return 0).  */
532a6b7db3Sskrll 
542a6b7db3Sskrll #define N_TXTADDR(x) \
552a6b7db3Sskrll     (N_MAGIC(x)==OMAGIC? 0 \
568cbf5cb7Schristos      : (N_MAGIC(x) == ZMAGIC && (x)->a_entry < TEXT_START_ADDR)? 0 \
572a6b7db3Sskrll      : TEXT_START_ADDR+EXEC_BYTES_SIZE)
582a6b7db3Sskrll 
592a6b7db3Sskrll /* When a file is linked against a shared library on SunOS 4, the
602a6b7db3Sskrll    dynamic bit in the exec header is set, and the first symbol in the
612a6b7db3Sskrll    symbol table is __DYNAMIC.  Its value is the address of the
622a6b7db3Sskrll    following structure.  */
632a6b7db3Sskrll 
642a6b7db3Sskrll struct external_sun4_dynamic
652a6b7db3Sskrll {
662a6b7db3Sskrll   /* The version number of the structure.  SunOS 4.1.x creates files
672a6b7db3Sskrll      with version number 3, which is what this structure is based on.
682a6b7db3Sskrll      According to gdb, version 2 is similar.  I believe that version 2
692a6b7db3Sskrll      used a different type of procedure linkage table, and there may
702a6b7db3Sskrll      have been other differences.  */
712a6b7db3Sskrll   bfd_byte ld_version[4];
722a6b7db3Sskrll   /* The virtual address of a 28 byte structure used in debugging.
732a6b7db3Sskrll      The contents are filled in at run time by ld.so.  */
742a6b7db3Sskrll   bfd_byte ldd[4];
752a6b7db3Sskrll   /* The virtual address of another structure with information about
762a6b7db3Sskrll      how to relocate the executable at run time.  */
772a6b7db3Sskrll   bfd_byte ld[4];
782a6b7db3Sskrll };
792a6b7db3Sskrll 
802a6b7db3Sskrll /* The size of the debugging structure pointed to by the debugger
812a6b7db3Sskrll    field of __DYNAMIC.  */
822a6b7db3Sskrll #define EXTERNAL_SUN4_DYNAMIC_DEBUGGER_SIZE (24)
832a6b7db3Sskrll 
842a6b7db3Sskrll /* The structure pointed to by the linker field of __DYNAMIC.  As far
852a6b7db3Sskrll    as I can tell, most of the addresses in this structure are offsets
862a6b7db3Sskrll    within the file, but some are actually virtual addresses.  */
872a6b7db3Sskrll 
882a6b7db3Sskrll struct internal_sun4_dynamic_link
892a6b7db3Sskrll {
902a6b7db3Sskrll   /* Linked list of loaded objects.  This is filled in at runtime by
912a6b7db3Sskrll      ld.so and probably by dlopen.  */
922a6b7db3Sskrll   unsigned long ld_loaded;
932a6b7db3Sskrll 
942a6b7db3Sskrll   /* The address of the list of names of shared objects which must be
952a6b7db3Sskrll      included at runtime.  Each entry in the list is 16 bytes: the 4
962a6b7db3Sskrll      byte address of the string naming the object (e.g., for -lc this
972a6b7db3Sskrll      is "c"); 4 bytes of flags--the high bit is whether to search for
982a6b7db3Sskrll      the object using the library path; the 2 byte major version
992a6b7db3Sskrll      number; the 2 byte minor version number; the 4 byte address of
1002a6b7db3Sskrll      the next entry in the list (zero if this is the last entry).  The
1012a6b7db3Sskrll      version numbers seem to only be non-zero when doing library
1022a6b7db3Sskrll      searching.  */
1032a6b7db3Sskrll   unsigned long ld_need;
1042a6b7db3Sskrll 
1052a6b7db3Sskrll   /* The address of the path to search for the shared objects which
1062a6b7db3Sskrll      must be included.  This points to a string in PATH format which
1072a6b7db3Sskrll      is generated from the -L arguments to the linker.  According to
1082a6b7db3Sskrll      the man page, ld.so implicitly adds ${LD_LIBRARY_PATH} to the
1092a6b7db3Sskrll      beginning of this string and /lib:/usr/lib:/usr/local/lib to the
1102a6b7db3Sskrll      end.  The string is terminated by a null byte.  This field is
1112a6b7db3Sskrll      zero if there is no additional path.  */
1122a6b7db3Sskrll   unsigned long ld_rules;
1132a6b7db3Sskrll 
1142a6b7db3Sskrll   /* The address of the global offset table.  This appears to be a
1152a6b7db3Sskrll      virtual address, not a file offset.  The first entry in the
1162a6b7db3Sskrll      global offset table seems to be the virtual address of the
1172a6b7db3Sskrll      sun4_dynamic structure (the same value as the __DYNAMIC symbol).
1182a6b7db3Sskrll      The global offset table is used for PIC code to hold the
1192a6b7db3Sskrll      addresses of variables.  A dynamically linked file which does not
1202a6b7db3Sskrll      itself contain PIC code has a four byte global offset table.  */
1212a6b7db3Sskrll   unsigned long ld_got;
1222a6b7db3Sskrll 
1232a6b7db3Sskrll   /* The address of the procedure linkage table.  This appears to be a
1242a6b7db3Sskrll      virtual address, not a file offset.
1252a6b7db3Sskrll 
1262a6b7db3Sskrll      On a SPARC, the table is composed of 12 byte entries, each of
1272a6b7db3Sskrll      which consists of three instructions.  The first entry is
1282a6b7db3Sskrll          sethi %hi(0),%g1
1292a6b7db3Sskrll 	 jmp %g1
1302a6b7db3Sskrll 	 nop
1312a6b7db3Sskrll      These instructions are changed by ld.so into a jump directly into
1322a6b7db3Sskrll      ld.so itself.  Each subsequent entry is
1332a6b7db3Sskrll          save %sp, -96, %sp
1342a6b7db3Sskrll 	 call <address of first entry in procedure linkage table>
1352a6b7db3Sskrll 	 <reloc_number | 0x01000000>
1362a6b7db3Sskrll      The reloc_number is the number of the reloc to use to resolve
1372a6b7db3Sskrll      this entry.  The reloc will be a JMP_SLOT reloc against some
1382a6b7db3Sskrll      symbol that is not defined in this object file but should be
1392a6b7db3Sskrll      defined in a shared object (if it is not, ld.so will report a
1402a6b7db3Sskrll      runtime error and exit).  The constant 0x010000000 turns the
1412a6b7db3Sskrll      reloc number into a sethi of %g0, which does nothing since %g0 is
1422a6b7db3Sskrll      hardwired to zero.
1432a6b7db3Sskrll 
1442a6b7db3Sskrll      When one of these entries is executed, it winds up calling into
1452a6b7db3Sskrll      ld.so.  ld.so looks at the reloc number, available via the return
1462a6b7db3Sskrll      address, to determine which entry this is.  It then looks at the
1472a6b7db3Sskrll      reloc and patches up the entry in the table into a sethi and jmp
1482a6b7db3Sskrll      to the real address followed by a nop.  This means that the reloc
1492a6b7db3Sskrll      lookup only has to happen once, and it also means that the
1502a6b7db3Sskrll      relocation only needs to be done if the function is actually
1512a6b7db3Sskrll      called.  The relocation is expensive because ld.so must look up
1522a6b7db3Sskrll      the symbol by name.
1532a6b7db3Sskrll 
1542a6b7db3Sskrll      The size of the procedure linkage table is given by the ld_plt_sz
1552a6b7db3Sskrll      field.  */
1562a6b7db3Sskrll   unsigned long ld_plt;
1572a6b7db3Sskrll 
1582a6b7db3Sskrll   /* The address of the relocs.  These are in the same format as
1592a6b7db3Sskrll      ordinary relocs.  Symbol index numbers refer to the symbols
1602a6b7db3Sskrll      pointed to by ld_stab.  I think the only way to determine the
1612a6b7db3Sskrll      number of relocs is to assume that all the bytes from ld_rel to
1622a6b7db3Sskrll      ld_hash contain reloc entries.  */
1632a6b7db3Sskrll   unsigned long ld_rel;
1642a6b7db3Sskrll 
1652a6b7db3Sskrll   /* The address of a hash table of symbols.  The hash table has
1662a6b7db3Sskrll      roughly the same number of entries as there are dynamic symbols;
1672a6b7db3Sskrll      I think the only way to get the exact size is to assume that
1682a6b7db3Sskrll      every byte from ld_hash to ld_stab is devoted to the hash table.
1692a6b7db3Sskrll 
1702a6b7db3Sskrll      Each entry in the hash table is eight bytes.  The first four
1712a6b7db3Sskrll      bytes are a symbol index into the dynamic symbols.  The second
1722a6b7db3Sskrll      four bytes are the index of the next hash table entry in the
1732a6b7db3Sskrll      bucket.  The ld_buckets field gives the number of buckets, say B.
1742a6b7db3Sskrll      The first B entries in the hash table each start a bucket which
1752a6b7db3Sskrll      is chained through the second four bytes of each entry.  A value
1762a6b7db3Sskrll      of zero ends the chain.
1772a6b7db3Sskrll 
1782a6b7db3Sskrll      The hash function is simply
1792a6b7db3Sskrll          h = 0;
1802a6b7db3Sskrll          while (*string != '\0')
1812a6b7db3Sskrll 	   h = (h << 1) + *string++;
1822a6b7db3Sskrll 	 h &= 0x7fffffff;
1832a6b7db3Sskrll 
1842a6b7db3Sskrll      To look up a symbol, compute the hash value of the name.  Take
1852a6b7db3Sskrll      the modulos of hash value and the number of buckets.  Start at
1862a6b7db3Sskrll      that entry in the hash table.  See if the symbol (from the first
1872a6b7db3Sskrll      four bytes of the hash table entry) has the name you are looking
1882a6b7db3Sskrll      for.  If not, use the chain field (the second four bytes of the
1892a6b7db3Sskrll      hash table entry) to move on to the next entry in this bucket.
1902a6b7db3Sskrll      If the chain field is zero you have reached the end of the
1912a6b7db3Sskrll      bucket, and the symbol is not in the hash table.  */
1922a6b7db3Sskrll   unsigned long ld_hash;
1932a6b7db3Sskrll 
1942a6b7db3Sskrll   /* The address of the symbol table.  This is a list of
1952a6b7db3Sskrll      external_nlist structures.  The string indices are relative to
1962a6b7db3Sskrll      the ld_symbols field.  I think the only way to determine the
1972a6b7db3Sskrll      number of symbols is to assume that all the bytes between ld_stab
1982a6b7db3Sskrll      and ld_symbols are external_nlist structures.  */
1992a6b7db3Sskrll   unsigned long ld_stab;
2002a6b7db3Sskrll 
2012a6b7db3Sskrll   /* I don't know what this is for.  It seems to always be zero.  */
2022a6b7db3Sskrll   unsigned long ld_stab_hash;
2032a6b7db3Sskrll 
2042a6b7db3Sskrll   /* The number of buckets in the hash table.  */
2052a6b7db3Sskrll   unsigned long ld_buckets;
2062a6b7db3Sskrll 
2072a6b7db3Sskrll   /* The address of the symbol string table.  The first string in this
2082a6b7db3Sskrll      string table need not be the empty string.  */
2092a6b7db3Sskrll   unsigned long ld_symbols;
2102a6b7db3Sskrll 
2112a6b7db3Sskrll   /* The size in bytes of the symbol string table.  */
2122a6b7db3Sskrll   unsigned long ld_symb_size;
2132a6b7db3Sskrll 
2142a6b7db3Sskrll   /* The size in bytes of the text segment.  */
2152a6b7db3Sskrll   unsigned long ld_text;
2162a6b7db3Sskrll 
2172a6b7db3Sskrll   /* The size in bytes of the procedure linkage table.  */
2182a6b7db3Sskrll   unsigned long ld_plt_sz;
2192a6b7db3Sskrll };
2202a6b7db3Sskrll 
2212a6b7db3Sskrll /* The external form of the structure.  */
2222a6b7db3Sskrll 
2232a6b7db3Sskrll struct external_sun4_dynamic_link
2242a6b7db3Sskrll {
2252a6b7db3Sskrll   bfd_byte ld_loaded[4];
2262a6b7db3Sskrll   bfd_byte ld_need[4];
2272a6b7db3Sskrll   bfd_byte ld_rules[4];
2282a6b7db3Sskrll   bfd_byte ld_got[4];
2292a6b7db3Sskrll   bfd_byte ld_plt[4];
2302a6b7db3Sskrll   bfd_byte ld_rel[4];
2312a6b7db3Sskrll   bfd_byte ld_hash[4];
2322a6b7db3Sskrll   bfd_byte ld_stab[4];
2332a6b7db3Sskrll   bfd_byte ld_stab_hash[4];
2342a6b7db3Sskrll   bfd_byte ld_buckets[4];
2352a6b7db3Sskrll   bfd_byte ld_symbols[4];
2362a6b7db3Sskrll   bfd_byte ld_symb_size[4];
2372a6b7db3Sskrll   bfd_byte ld_text[4];
2382a6b7db3Sskrll   bfd_byte ld_plt_sz[4];
2392a6b7db3Sskrll };
240